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
54 #define BTUSB_AMP 0x4000
56 static const struct usb_device_id btusb_table[] = {
57 /* Generic Bluetooth USB device */
58 { USB_DEVICE_INFO(0xe0, 0x01, 0x01) },
60 /* Generic Bluetooth AMP device */
61 { USB_DEVICE_INFO(0xe0, 0x01, 0x04), .driver_info = BTUSB_AMP },
63 /* Apple-specific (Broadcom) devices */
64 { USB_VENDOR_AND_INTERFACE_INFO(0x05ac, 0xff, 0x01, 0x01) },
66 /* MediaTek MT76x0E */
67 { USB_DEVICE(0x0e8d, 0x763f) },
69 /* Broadcom SoftSailing reporting vendor specific */
70 { USB_DEVICE(0x0a5c, 0x21e1) },
72 /* Apple MacBookPro 7,1 */
73 { USB_DEVICE(0x05ac, 0x8213) },
76 { USB_DEVICE(0x05ac, 0x8215) },
78 /* Apple MacBookPro6,2 */
79 { USB_DEVICE(0x05ac, 0x8218) },
81 /* Apple MacBookAir3,1, MacBookAir3,2 */
82 { USB_DEVICE(0x05ac, 0x821b) },
84 /* Apple MacBookAir4,1 */
85 { USB_DEVICE(0x05ac, 0x821f) },
87 /* Apple MacBookPro8,2 */
88 { USB_DEVICE(0x05ac, 0x821a) },
90 /* Apple MacMini5,1 */
91 { USB_DEVICE(0x05ac, 0x8281) },
93 /* AVM BlueFRITZ! USB v2.0 */
94 { USB_DEVICE(0x057c, 0x3800), .driver_info = BTUSB_SWAVE },
96 /* Bluetooth Ultraport Module from IBM */
97 { USB_DEVICE(0x04bf, 0x030a) },
99 /* ALPS Modules with non-standard id */
100 { USB_DEVICE(0x044e, 0x3001) },
101 { USB_DEVICE(0x044e, 0x3002) },
103 /* Ericsson with non-standard id */
104 { USB_DEVICE(0x0bdb, 0x1002) },
106 /* Canyon CN-BTU1 with HID interfaces */
107 { USB_DEVICE(0x0c10, 0x0000) },
109 /* Broadcom BCM20702A0 */
110 { USB_DEVICE(0x0489, 0xe042) },
111 { USB_DEVICE(0x04ca, 0x2003) },
112 { USB_DEVICE(0x0b05, 0x17b5) },
113 { USB_DEVICE(0x0b05, 0x17cb) },
114 { USB_DEVICE(0x413c, 0x8197) },
115 { USB_DEVICE(0x13d3, 0x3404),
116 .driver_info = BTUSB_BCM_PATCHRAM },
118 /* Broadcom BCM20702B0 (Dynex/Insignia) */
119 { USB_DEVICE(0x19ff, 0x0239), .driver_info = BTUSB_BCM_PATCHRAM },
121 /* Foxconn - Hon Hai */
122 { USB_VENDOR_AND_INTERFACE_INFO(0x0489, 0xff, 0x01, 0x01),
123 .driver_info = BTUSB_BCM_PATCHRAM },
125 /* Broadcom devices with vendor specific id */
126 { USB_VENDOR_AND_INTERFACE_INFO(0x0a5c, 0xff, 0x01, 0x01),
127 .driver_info = BTUSB_BCM_PATCHRAM },
129 /* ASUSTek Computer - Broadcom based */
130 { USB_VENDOR_AND_INTERFACE_INFO(0x0b05, 0xff, 0x01, 0x01),
131 .driver_info = BTUSB_BCM_PATCHRAM },
133 /* Belkin F8065bf - Broadcom based */
134 { USB_VENDOR_AND_INTERFACE_INFO(0x050d, 0xff, 0x01, 0x01) },
136 /* IMC Networks - Broadcom based */
137 { USB_VENDOR_AND_INTERFACE_INFO(0x13d3, 0xff, 0x01, 0x01) },
139 /* Intel Bluetooth USB Bootloader (RAM module) */
140 { USB_DEVICE(0x8087, 0x0a5a),
141 .driver_info = BTUSB_INTEL_BOOT | BTUSB_BROKEN_ISOC },
143 { } /* Terminating entry */
146 MODULE_DEVICE_TABLE(usb, btusb_table);
148 static const struct usb_device_id blacklist_table[] = {
149 /* CSR BlueCore devices */
150 { USB_DEVICE(0x0a12, 0x0001), .driver_info = BTUSB_CSR },
152 /* Broadcom BCM2033 without firmware */
153 { USB_DEVICE(0x0a5c, 0x2033), .driver_info = BTUSB_IGNORE },
155 /* Atheros 3011 with sflash firmware */
156 { USB_DEVICE(0x0489, 0xe027), .driver_info = BTUSB_IGNORE },
157 { USB_DEVICE(0x0489, 0xe03d), .driver_info = BTUSB_IGNORE },
158 { USB_DEVICE(0x0930, 0x0215), .driver_info = BTUSB_IGNORE },
159 { USB_DEVICE(0x0cf3, 0x3002), .driver_info = BTUSB_IGNORE },
160 { USB_DEVICE(0x0cf3, 0xe019), .driver_info = BTUSB_IGNORE },
161 { USB_DEVICE(0x13d3, 0x3304), .driver_info = BTUSB_IGNORE },
163 /* Atheros AR9285 Malbec with sflash firmware */
164 { USB_DEVICE(0x03f0, 0x311d), .driver_info = BTUSB_IGNORE },
166 /* Atheros 3012 with sflash firmware */
167 { USB_DEVICE(0x0489, 0xe04d), .driver_info = BTUSB_ATH3012 },
168 { USB_DEVICE(0x0489, 0xe04e), .driver_info = BTUSB_ATH3012 },
169 { USB_DEVICE(0x0489, 0xe056), .driver_info = BTUSB_ATH3012 },
170 { USB_DEVICE(0x0489, 0xe057), .driver_info = BTUSB_ATH3012 },
171 { USB_DEVICE(0x0489, 0xe05f), .driver_info = BTUSB_ATH3012 },
172 { USB_DEVICE(0x0489, 0xe078), .driver_info = BTUSB_ATH3012 },
173 { USB_DEVICE(0x04c5, 0x1330), .driver_info = BTUSB_ATH3012 },
174 { USB_DEVICE(0x04ca, 0x3004), .driver_info = BTUSB_ATH3012 },
175 { USB_DEVICE(0x04ca, 0x3005), .driver_info = BTUSB_ATH3012 },
176 { USB_DEVICE(0x04ca, 0x3006), .driver_info = BTUSB_ATH3012 },
177 { USB_DEVICE(0x04ca, 0x3007), .driver_info = BTUSB_ATH3012 },
178 { USB_DEVICE(0x04ca, 0x3008), .driver_info = BTUSB_ATH3012 },
179 { USB_DEVICE(0x04ca, 0x300b), .driver_info = BTUSB_ATH3012 },
180 { USB_DEVICE(0x04ca, 0x3010), .driver_info = BTUSB_ATH3012 },
181 { USB_DEVICE(0x0930, 0x0219), .driver_info = BTUSB_ATH3012 },
182 { USB_DEVICE(0x0930, 0x0220), .driver_info = BTUSB_ATH3012 },
183 { USB_DEVICE(0x0930, 0x0227), .driver_info = BTUSB_ATH3012 },
184 { USB_DEVICE(0x0b05, 0x17d0), .driver_info = BTUSB_ATH3012 },
185 { USB_DEVICE(0x0cf3, 0x0036), .driver_info = BTUSB_ATH3012 },
186 { USB_DEVICE(0x0cf3, 0x3004), .driver_info = BTUSB_ATH3012 },
187 { USB_DEVICE(0x0cf3, 0x3008), .driver_info = BTUSB_ATH3012 },
188 { USB_DEVICE(0x0cf3, 0x311d), .driver_info = BTUSB_ATH3012 },
189 { USB_DEVICE(0x0cf3, 0x311e), .driver_info = BTUSB_ATH3012 },
190 { USB_DEVICE(0x0cf3, 0x311f), .driver_info = BTUSB_ATH3012 },
191 { USB_DEVICE(0x0cf3, 0x3121), .driver_info = BTUSB_ATH3012 },
192 { USB_DEVICE(0x0cf3, 0x817a), .driver_info = BTUSB_ATH3012 },
193 { USB_DEVICE(0x0cf3, 0xe003), .driver_info = BTUSB_ATH3012 },
194 { USB_DEVICE(0x0cf3, 0xe004), .driver_info = BTUSB_ATH3012 },
195 { USB_DEVICE(0x0cf3, 0xe005), .driver_info = BTUSB_ATH3012 },
196 { USB_DEVICE(0x13d3, 0x3362), .driver_info = BTUSB_ATH3012 },
197 { USB_DEVICE(0x13d3, 0x3375), .driver_info = BTUSB_ATH3012 },
198 { USB_DEVICE(0x13d3, 0x3393), .driver_info = BTUSB_ATH3012 },
199 { USB_DEVICE(0x13d3, 0x3402), .driver_info = BTUSB_ATH3012 },
200 { USB_DEVICE(0x13d3, 0x3408), .driver_info = BTUSB_ATH3012 },
201 { USB_DEVICE(0x13d3, 0x3423), .driver_info = BTUSB_ATH3012 },
202 { USB_DEVICE(0x13d3, 0x3432), .driver_info = BTUSB_ATH3012 },
204 /* Atheros AR5BBU12 with sflash firmware */
205 { USB_DEVICE(0x0489, 0xe02c), .driver_info = BTUSB_IGNORE },
207 /* Atheros AR5BBU12 with sflash firmware */
208 { USB_DEVICE(0x0489, 0xe036), .driver_info = BTUSB_ATH3012 },
209 { USB_DEVICE(0x0489, 0xe03c), .driver_info = BTUSB_ATH3012 },
211 /* Broadcom BCM2035 */
212 { USB_DEVICE(0x0a5c, 0x2009), .driver_info = BTUSB_BCM92035 },
213 { USB_DEVICE(0x0a5c, 0x200a), .driver_info = BTUSB_WRONG_SCO_MTU },
214 { USB_DEVICE(0x0a5c, 0x2035), .driver_info = BTUSB_WRONG_SCO_MTU },
216 /* Broadcom BCM2045 */
217 { USB_DEVICE(0x0a5c, 0x2039), .driver_info = BTUSB_WRONG_SCO_MTU },
218 { USB_DEVICE(0x0a5c, 0x2101), .driver_info = BTUSB_WRONG_SCO_MTU },
220 /* IBM/Lenovo ThinkPad with Broadcom chip */
221 { USB_DEVICE(0x0a5c, 0x201e), .driver_info = BTUSB_WRONG_SCO_MTU },
222 { USB_DEVICE(0x0a5c, 0x2110), .driver_info = BTUSB_WRONG_SCO_MTU },
224 /* HP laptop with Broadcom chip */
225 { USB_DEVICE(0x03f0, 0x171d), .driver_info = BTUSB_WRONG_SCO_MTU },
227 /* Dell laptop with Broadcom chip */
228 { USB_DEVICE(0x413c, 0x8126), .driver_info = BTUSB_WRONG_SCO_MTU },
230 /* Dell Wireless 370 and 410 devices */
231 { USB_DEVICE(0x413c, 0x8152), .driver_info = BTUSB_WRONG_SCO_MTU },
232 { USB_DEVICE(0x413c, 0x8156), .driver_info = BTUSB_WRONG_SCO_MTU },
234 /* Belkin F8T012 and F8T013 devices */
235 { USB_DEVICE(0x050d, 0x0012), .driver_info = BTUSB_WRONG_SCO_MTU },
236 { USB_DEVICE(0x050d, 0x0013), .driver_info = BTUSB_WRONG_SCO_MTU },
238 /* Asus WL-BTD202 device */
239 { USB_DEVICE(0x0b05, 0x1715), .driver_info = BTUSB_WRONG_SCO_MTU },
241 /* Kensington Bluetooth USB adapter */
242 { USB_DEVICE(0x047d, 0x105e), .driver_info = BTUSB_WRONG_SCO_MTU },
244 /* RTX Telecom based adapters with buggy SCO support */
245 { USB_DEVICE(0x0400, 0x0807), .driver_info = BTUSB_BROKEN_ISOC },
246 { USB_DEVICE(0x0400, 0x080a), .driver_info = BTUSB_BROKEN_ISOC },
248 /* CONWISE Technology based adapters with buggy SCO support */
249 { USB_DEVICE(0x0e5e, 0x6622), .driver_info = BTUSB_BROKEN_ISOC },
251 /* Roper Class 1 Bluetooth Dongle (Silicon Wave based) */
252 { USB_DEVICE(0x1300, 0x0001), .driver_info = BTUSB_SWAVE },
254 /* Digianswer devices */
255 { USB_DEVICE(0x08fd, 0x0001), .driver_info = BTUSB_DIGIANSWER },
256 { USB_DEVICE(0x08fd, 0x0002), .driver_info = BTUSB_IGNORE },
258 /* CSR BlueCore Bluetooth Sniffer */
259 { USB_DEVICE(0x0a12, 0x0002),
260 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
262 /* Frontline ComProbe Bluetooth Sniffer */
263 { USB_DEVICE(0x16d3, 0x0002),
264 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
266 /* Marvell Bluetooth devices */
267 { USB_DEVICE(0x1286, 0x2044), .driver_info = BTUSB_MARVELL },
268 { USB_DEVICE(0x1286, 0x2046), .driver_info = BTUSB_MARVELL },
270 /* Intel Bluetooth devices */
271 { USB_DEVICE(0x8087, 0x07dc), .driver_info = BTUSB_INTEL },
272 { USB_DEVICE(0x8087, 0x0a2a), .driver_info = BTUSB_INTEL },
273 { USB_DEVICE(0x8087, 0x0a2b), .driver_info = BTUSB_INTEL_NEW },
275 /* Other Intel Bluetooth devices */
276 { USB_VENDOR_AND_INTERFACE_INFO(0x8087, 0xe0, 0x01, 0x01),
277 .driver_info = BTUSB_IGNORE },
279 { } /* Terminating entry */
282 #define BTUSB_MAX_ISOC_FRAMES 10
284 #define BTUSB_INTR_RUNNING 0
285 #define BTUSB_BULK_RUNNING 1
286 #define BTUSB_ISOC_RUNNING 2
287 #define BTUSB_SUSPENDING 3
288 #define BTUSB_DID_ISO_RESUME 4
289 #define BTUSB_BOOTLOADER 5
290 #define BTUSB_DOWNLOADING 6
291 #define BTUSB_FIRMWARE_LOADED 7
292 #define BTUSB_FIRMWARE_FAILED 8
293 #define BTUSB_BOOTING 9
296 struct hci_dev *hdev;
297 struct usb_device *udev;
298 struct usb_interface *intf;
299 struct usb_interface *isoc;
303 struct work_struct work;
304 struct work_struct waker;
306 struct usb_anchor deferred;
307 struct usb_anchor tx_anchor;
311 struct usb_anchor intr_anchor;
312 struct usb_anchor bulk_anchor;
313 struct usb_anchor isoc_anchor;
316 struct sk_buff *evt_skb;
317 struct sk_buff *acl_skb;
318 struct sk_buff *sco_skb;
320 struct usb_endpoint_descriptor *intr_ep;
321 struct usb_endpoint_descriptor *bulk_tx_ep;
322 struct usb_endpoint_descriptor *bulk_rx_ep;
323 struct usb_endpoint_descriptor *isoc_tx_ep;
324 struct usb_endpoint_descriptor *isoc_rx_ep;
329 unsigned int sco_num;
333 int (*recv_event)(struct hci_dev *hdev, struct sk_buff *skb);
334 int (*recv_bulk)(struct btusb_data *data, void *buffer, int count);
337 static int btusb_wait_on_bit_timeout(void *word, int bit, unsigned long timeout,
341 if (!test_bit(bit, word))
343 return out_of_line_wait_on_bit_timeout(word, bit, bit_wait_timeout,
347 static inline void btusb_free_frags(struct btusb_data *data)
351 spin_lock_irqsave(&data->rxlock, flags);
353 kfree_skb(data->evt_skb);
354 data->evt_skb = NULL;
356 kfree_skb(data->acl_skb);
357 data->acl_skb = NULL;
359 kfree_skb(data->sco_skb);
360 data->sco_skb = NULL;
362 spin_unlock_irqrestore(&data->rxlock, flags);
365 static int btusb_recv_intr(struct btusb_data *data, void *buffer, int count)
370 spin_lock(&data->rxlock);
377 skb = bt_skb_alloc(HCI_MAX_EVENT_SIZE, GFP_ATOMIC);
383 bt_cb(skb)->pkt_type = HCI_EVENT_PKT;
384 bt_cb(skb)->expect = HCI_EVENT_HDR_SIZE;
387 len = min_t(uint, bt_cb(skb)->expect, count);
388 memcpy(skb_put(skb, len), buffer, len);
392 bt_cb(skb)->expect -= len;
394 if (skb->len == HCI_EVENT_HDR_SIZE) {
395 /* Complete event header */
396 bt_cb(skb)->expect = hci_event_hdr(skb)->plen;
398 if (skb_tailroom(skb) < bt_cb(skb)->expect) {
407 if (bt_cb(skb)->expect == 0) {
409 data->recv_event(data->hdev, skb);
415 spin_unlock(&data->rxlock);
420 static int btusb_recv_bulk(struct btusb_data *data, void *buffer, int count)
425 spin_lock(&data->rxlock);
432 skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
438 bt_cb(skb)->pkt_type = HCI_ACLDATA_PKT;
439 bt_cb(skb)->expect = HCI_ACL_HDR_SIZE;
442 len = min_t(uint, bt_cb(skb)->expect, count);
443 memcpy(skb_put(skb, len), buffer, len);
447 bt_cb(skb)->expect -= len;
449 if (skb->len == HCI_ACL_HDR_SIZE) {
450 __le16 dlen = hci_acl_hdr(skb)->dlen;
452 /* Complete ACL header */
453 bt_cb(skb)->expect = __le16_to_cpu(dlen);
455 if (skb_tailroom(skb) < bt_cb(skb)->expect) {
464 if (bt_cb(skb)->expect == 0) {
466 hci_recv_frame(data->hdev, skb);
472 spin_unlock(&data->rxlock);
477 static int btusb_recv_isoc(struct btusb_data *data, void *buffer, int count)
482 spin_lock(&data->rxlock);
489 skb = bt_skb_alloc(HCI_MAX_SCO_SIZE, GFP_ATOMIC);
495 bt_cb(skb)->pkt_type = HCI_SCODATA_PKT;
496 bt_cb(skb)->expect = HCI_SCO_HDR_SIZE;
499 len = min_t(uint, bt_cb(skb)->expect, count);
500 memcpy(skb_put(skb, len), buffer, len);
504 bt_cb(skb)->expect -= len;
506 if (skb->len == HCI_SCO_HDR_SIZE) {
507 /* Complete SCO header */
508 bt_cb(skb)->expect = hci_sco_hdr(skb)->dlen;
510 if (skb_tailroom(skb) < bt_cb(skb)->expect) {
519 if (bt_cb(skb)->expect == 0) {
521 hci_recv_frame(data->hdev, skb);
527 spin_unlock(&data->rxlock);
532 static void btusb_intr_complete(struct urb *urb)
534 struct hci_dev *hdev = urb->context;
535 struct btusb_data *data = hci_get_drvdata(hdev);
538 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
541 if (!test_bit(HCI_RUNNING, &hdev->flags))
544 if (urb->status == 0) {
545 hdev->stat.byte_rx += urb->actual_length;
547 if (btusb_recv_intr(data, urb->transfer_buffer,
548 urb->actual_length) < 0) {
549 BT_ERR("%s corrupted event packet", hdev->name);
552 } else if (urb->status == -ENOENT) {
553 /* Avoid suspend failed when usb_kill_urb */
557 if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
560 usb_mark_last_busy(data->udev);
561 usb_anchor_urb(urb, &data->intr_anchor);
563 err = usb_submit_urb(urb, GFP_ATOMIC);
565 /* -EPERM: urb is being killed;
566 * -ENODEV: device got disconnected */
567 if (err != -EPERM && err != -ENODEV)
568 BT_ERR("%s urb %p failed to resubmit (%d)",
569 hdev->name, urb, -err);
570 usb_unanchor_urb(urb);
574 static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags)
576 struct btusb_data *data = hci_get_drvdata(hdev);
582 BT_DBG("%s", hdev->name);
587 urb = usb_alloc_urb(0, mem_flags);
591 size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
593 buf = kmalloc(size, mem_flags);
599 pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
601 usb_fill_int_urb(urb, data->udev, pipe, buf, size,
602 btusb_intr_complete, hdev, data->intr_ep->bInterval);
604 urb->transfer_flags |= URB_FREE_BUFFER;
606 usb_anchor_urb(urb, &data->intr_anchor);
608 err = usb_submit_urb(urb, mem_flags);
610 if (err != -EPERM && err != -ENODEV)
611 BT_ERR("%s urb %p submission failed (%d)",
612 hdev->name, urb, -err);
613 usb_unanchor_urb(urb);
621 static void btusb_bulk_complete(struct urb *urb)
623 struct hci_dev *hdev = urb->context;
624 struct btusb_data *data = hci_get_drvdata(hdev);
627 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
630 if (!test_bit(HCI_RUNNING, &hdev->flags))
633 if (urb->status == 0) {
634 hdev->stat.byte_rx += urb->actual_length;
636 if (data->recv_bulk(data, urb->transfer_buffer,
637 urb->actual_length) < 0) {
638 BT_ERR("%s corrupted ACL packet", hdev->name);
641 } else if (urb->status == -ENOENT) {
642 /* Avoid suspend failed when usb_kill_urb */
646 if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
649 usb_anchor_urb(urb, &data->bulk_anchor);
650 usb_mark_last_busy(data->udev);
652 err = usb_submit_urb(urb, GFP_ATOMIC);
654 /* -EPERM: urb is being killed;
655 * -ENODEV: device got disconnected */
656 if (err != -EPERM && err != -ENODEV)
657 BT_ERR("%s urb %p failed to resubmit (%d)",
658 hdev->name, urb, -err);
659 usb_unanchor_urb(urb);
663 static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
665 struct btusb_data *data = hci_get_drvdata(hdev);
669 int err, size = HCI_MAX_FRAME_SIZE;
671 BT_DBG("%s", hdev->name);
673 if (!data->bulk_rx_ep)
676 urb = usb_alloc_urb(0, mem_flags);
680 buf = kmalloc(size, mem_flags);
686 pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
688 usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
689 btusb_bulk_complete, hdev);
691 urb->transfer_flags |= URB_FREE_BUFFER;
693 usb_mark_last_busy(data->udev);
694 usb_anchor_urb(urb, &data->bulk_anchor);
696 err = usb_submit_urb(urb, mem_flags);
698 if (err != -EPERM && err != -ENODEV)
699 BT_ERR("%s urb %p submission failed (%d)",
700 hdev->name, urb, -err);
701 usb_unanchor_urb(urb);
709 static void btusb_isoc_complete(struct urb *urb)
711 struct hci_dev *hdev = urb->context;
712 struct btusb_data *data = hci_get_drvdata(hdev);
715 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
718 if (!test_bit(HCI_RUNNING, &hdev->flags))
721 if (urb->status == 0) {
722 for (i = 0; i < urb->number_of_packets; i++) {
723 unsigned int offset = urb->iso_frame_desc[i].offset;
724 unsigned int length = urb->iso_frame_desc[i].actual_length;
726 if (urb->iso_frame_desc[i].status)
729 hdev->stat.byte_rx += length;
731 if (btusb_recv_isoc(data, urb->transfer_buffer + offset,
733 BT_ERR("%s corrupted SCO packet", hdev->name);
737 } else if (urb->status == -ENOENT) {
738 /* Avoid suspend failed when usb_kill_urb */
742 if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags))
745 usb_anchor_urb(urb, &data->isoc_anchor);
747 err = usb_submit_urb(urb, GFP_ATOMIC);
749 /* -EPERM: urb is being killed;
750 * -ENODEV: device got disconnected */
751 if (err != -EPERM && err != -ENODEV)
752 BT_ERR("%s urb %p failed to resubmit (%d)",
753 hdev->name, urb, -err);
754 usb_unanchor_urb(urb);
758 static inline void __fill_isoc_descriptor(struct urb *urb, int len, int mtu)
762 BT_DBG("len %d mtu %d", len, mtu);
764 for (i = 0; i < BTUSB_MAX_ISOC_FRAMES && len >= mtu;
765 i++, offset += mtu, len -= mtu) {
766 urb->iso_frame_desc[i].offset = offset;
767 urb->iso_frame_desc[i].length = mtu;
770 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
771 urb->iso_frame_desc[i].offset = offset;
772 urb->iso_frame_desc[i].length = len;
776 urb->number_of_packets = i;
779 static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags)
781 struct btusb_data *data = hci_get_drvdata(hdev);
787 BT_DBG("%s", hdev->name);
789 if (!data->isoc_rx_ep)
792 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags);
796 size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) *
797 BTUSB_MAX_ISOC_FRAMES;
799 buf = kmalloc(size, mem_flags);
805 pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress);
807 usb_fill_int_urb(urb, data->udev, pipe, buf, size, btusb_isoc_complete,
808 hdev, data->isoc_rx_ep->bInterval);
810 urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP;
812 __fill_isoc_descriptor(urb, size,
813 le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize));
815 usb_anchor_urb(urb, &data->isoc_anchor);
817 err = usb_submit_urb(urb, mem_flags);
819 if (err != -EPERM && err != -ENODEV)
820 BT_ERR("%s urb %p submission failed (%d)",
821 hdev->name, urb, -err);
822 usb_unanchor_urb(urb);
830 static void btusb_tx_complete(struct urb *urb)
832 struct sk_buff *skb = urb->context;
833 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
834 struct btusb_data *data = hci_get_drvdata(hdev);
836 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
839 if (!test_bit(HCI_RUNNING, &hdev->flags))
843 hdev->stat.byte_tx += urb->transfer_buffer_length;
848 spin_lock(&data->txlock);
849 data->tx_in_flight--;
850 spin_unlock(&data->txlock);
852 kfree(urb->setup_packet);
857 static void btusb_isoc_tx_complete(struct urb *urb)
859 struct sk_buff *skb = urb->context;
860 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
862 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
865 if (!test_bit(HCI_RUNNING, &hdev->flags))
869 hdev->stat.byte_tx += urb->transfer_buffer_length;
874 kfree(urb->setup_packet);
879 static int btusb_open(struct hci_dev *hdev)
881 struct btusb_data *data = hci_get_drvdata(hdev);
884 BT_DBG("%s", hdev->name);
886 err = usb_autopm_get_interface(data->intf);
890 data->intf->needs_remote_wakeup = 1;
892 if (test_and_set_bit(HCI_RUNNING, &hdev->flags))
895 if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
898 err = btusb_submit_intr_urb(hdev, GFP_KERNEL);
902 err = btusb_submit_bulk_urb(hdev, GFP_KERNEL);
904 usb_kill_anchored_urbs(&data->intr_anchor);
908 set_bit(BTUSB_BULK_RUNNING, &data->flags);
909 btusb_submit_bulk_urb(hdev, GFP_KERNEL);
912 usb_autopm_put_interface(data->intf);
916 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
917 clear_bit(HCI_RUNNING, &hdev->flags);
918 usb_autopm_put_interface(data->intf);
922 static void btusb_stop_traffic(struct btusb_data *data)
924 usb_kill_anchored_urbs(&data->intr_anchor);
925 usb_kill_anchored_urbs(&data->bulk_anchor);
926 usb_kill_anchored_urbs(&data->isoc_anchor);
929 static int btusb_close(struct hci_dev *hdev)
931 struct btusb_data *data = hci_get_drvdata(hdev);
934 BT_DBG("%s", hdev->name);
936 if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
939 cancel_work_sync(&data->work);
940 cancel_work_sync(&data->waker);
942 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
943 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
944 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
946 btusb_stop_traffic(data);
947 btusb_free_frags(data);
949 err = usb_autopm_get_interface(data->intf);
953 data->intf->needs_remote_wakeup = 0;
954 usb_autopm_put_interface(data->intf);
957 usb_scuttle_anchored_urbs(&data->deferred);
961 static int btusb_flush(struct hci_dev *hdev)
963 struct btusb_data *data = hci_get_drvdata(hdev);
965 BT_DBG("%s", hdev->name);
967 usb_kill_anchored_urbs(&data->tx_anchor);
968 btusb_free_frags(data);
973 static struct urb *alloc_ctrl_urb(struct hci_dev *hdev, struct sk_buff *skb)
975 struct btusb_data *data = hci_get_drvdata(hdev);
976 struct usb_ctrlrequest *dr;
980 urb = usb_alloc_urb(0, GFP_KERNEL);
982 return ERR_PTR(-ENOMEM);
984 dr = kmalloc(sizeof(*dr), GFP_KERNEL);
987 return ERR_PTR(-ENOMEM);
990 dr->bRequestType = data->cmdreq_type;
991 dr->bRequest = data->cmdreq;
994 dr->wLength = __cpu_to_le16(skb->len);
996 pipe = usb_sndctrlpipe(data->udev, 0x00);
998 usb_fill_control_urb(urb, data->udev, pipe, (void *)dr,
999 skb->data, skb->len, btusb_tx_complete, skb);
1001 skb->dev = (void *)hdev;
1006 static struct urb *alloc_bulk_urb(struct hci_dev *hdev, struct sk_buff *skb)
1008 struct btusb_data *data = hci_get_drvdata(hdev);
1012 if (!data->bulk_tx_ep)
1013 return ERR_PTR(-ENODEV);
1015 urb = usb_alloc_urb(0, GFP_KERNEL);
1017 return ERR_PTR(-ENOMEM);
1019 pipe = usb_sndbulkpipe(data->udev, data->bulk_tx_ep->bEndpointAddress);
1021 usb_fill_bulk_urb(urb, data->udev, pipe,
1022 skb->data, skb->len, btusb_tx_complete, skb);
1024 skb->dev = (void *)hdev;
1029 static struct urb *alloc_isoc_urb(struct hci_dev *hdev, struct sk_buff *skb)
1031 struct btusb_data *data = hci_get_drvdata(hdev);
1035 if (!data->isoc_tx_ep)
1036 return ERR_PTR(-ENODEV);
1038 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_KERNEL);
1040 return ERR_PTR(-ENOMEM);
1042 pipe = usb_sndisocpipe(data->udev, data->isoc_tx_ep->bEndpointAddress);
1044 usb_fill_int_urb(urb, data->udev, pipe,
1045 skb->data, skb->len, btusb_isoc_tx_complete,
1046 skb, data->isoc_tx_ep->bInterval);
1048 urb->transfer_flags = URB_ISO_ASAP;
1050 __fill_isoc_descriptor(urb, skb->len,
1051 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
1053 skb->dev = (void *)hdev;
1058 static int submit_tx_urb(struct hci_dev *hdev, struct urb *urb)
1060 struct btusb_data *data = hci_get_drvdata(hdev);
1063 usb_anchor_urb(urb, &data->tx_anchor);
1065 err = usb_submit_urb(urb, GFP_KERNEL);
1067 if (err != -EPERM && err != -ENODEV)
1068 BT_ERR("%s urb %p submission failed (%d)",
1069 hdev->name, urb, -err);
1070 kfree(urb->setup_packet);
1071 usb_unanchor_urb(urb);
1073 usb_mark_last_busy(data->udev);
1080 static int submit_or_queue_tx_urb(struct hci_dev *hdev, struct urb *urb)
1082 struct btusb_data *data = hci_get_drvdata(hdev);
1083 unsigned long flags;
1086 spin_lock_irqsave(&data->txlock, flags);
1087 suspending = test_bit(BTUSB_SUSPENDING, &data->flags);
1089 data->tx_in_flight++;
1090 spin_unlock_irqrestore(&data->txlock, flags);
1093 return submit_tx_urb(hdev, urb);
1095 usb_anchor_urb(urb, &data->deferred);
1096 schedule_work(&data->waker);
1102 static int btusb_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
1106 BT_DBG("%s", hdev->name);
1108 if (!test_bit(HCI_RUNNING, &hdev->flags))
1111 switch (bt_cb(skb)->pkt_type) {
1112 case HCI_COMMAND_PKT:
1113 urb = alloc_ctrl_urb(hdev, skb);
1115 return PTR_ERR(urb);
1117 hdev->stat.cmd_tx++;
1118 return submit_or_queue_tx_urb(hdev, urb);
1120 case HCI_ACLDATA_PKT:
1121 urb = alloc_bulk_urb(hdev, skb);
1123 return PTR_ERR(urb);
1125 hdev->stat.acl_tx++;
1126 return submit_or_queue_tx_urb(hdev, urb);
1128 case HCI_SCODATA_PKT:
1129 if (hci_conn_num(hdev, SCO_LINK) < 1)
1132 urb = alloc_isoc_urb(hdev, skb);
1134 return PTR_ERR(urb);
1136 hdev->stat.sco_tx++;
1137 return submit_tx_urb(hdev, urb);
1143 static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
1145 struct btusb_data *data = hci_get_drvdata(hdev);
1147 BT_DBG("%s evt %d", hdev->name, evt);
1149 if (hci_conn_num(hdev, SCO_LINK) != data->sco_num) {
1150 data->sco_num = hci_conn_num(hdev, SCO_LINK);
1151 schedule_work(&data->work);
1155 static inline int __set_isoc_interface(struct hci_dev *hdev, int altsetting)
1157 struct btusb_data *data = hci_get_drvdata(hdev);
1158 struct usb_interface *intf = data->isoc;
1159 struct usb_endpoint_descriptor *ep_desc;
1165 err = usb_set_interface(data->udev, 1, altsetting);
1167 BT_ERR("%s setting interface failed (%d)", hdev->name, -err);
1171 data->isoc_altsetting = altsetting;
1173 data->isoc_tx_ep = NULL;
1174 data->isoc_rx_ep = NULL;
1176 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
1177 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
1179 if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) {
1180 data->isoc_tx_ep = ep_desc;
1184 if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) {
1185 data->isoc_rx_ep = ep_desc;
1190 if (!data->isoc_tx_ep || !data->isoc_rx_ep) {
1191 BT_ERR("%s invalid SCO descriptors", hdev->name);
1198 static void btusb_work(struct work_struct *work)
1200 struct btusb_data *data = container_of(work, struct btusb_data, work);
1201 struct hci_dev *hdev = data->hdev;
1205 if (data->sco_num > 0) {
1206 if (!test_bit(BTUSB_DID_ISO_RESUME, &data->flags)) {
1207 err = usb_autopm_get_interface(data->isoc ? data->isoc : data->intf);
1209 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1210 usb_kill_anchored_urbs(&data->isoc_anchor);
1214 set_bit(BTUSB_DID_ISO_RESUME, &data->flags);
1217 if (hdev->voice_setting & 0x0020) {
1218 static const int alts[3] = { 2, 4, 5 };
1220 new_alts = alts[data->sco_num - 1];
1222 new_alts = data->sco_num;
1225 if (data->isoc_altsetting != new_alts) {
1226 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1227 usb_kill_anchored_urbs(&data->isoc_anchor);
1229 if (__set_isoc_interface(hdev, new_alts) < 0)
1233 if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
1234 if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0)
1235 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1237 btusb_submit_isoc_urb(hdev, GFP_KERNEL);
1240 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1241 usb_kill_anchored_urbs(&data->isoc_anchor);
1243 __set_isoc_interface(hdev, 0);
1244 if (test_and_clear_bit(BTUSB_DID_ISO_RESUME, &data->flags))
1245 usb_autopm_put_interface(data->isoc ? data->isoc : data->intf);
1249 static void btusb_waker(struct work_struct *work)
1251 struct btusb_data *data = container_of(work, struct btusb_data, waker);
1254 err = usb_autopm_get_interface(data->intf);
1258 usb_autopm_put_interface(data->intf);
1261 static int btusb_setup_bcm92035(struct hci_dev *hdev)
1263 struct sk_buff *skb;
1266 BT_DBG("%s", hdev->name);
1268 skb = __hci_cmd_sync(hdev, 0xfc3b, 1, &val, HCI_INIT_TIMEOUT);
1270 BT_ERR("BCM92035 command failed (%ld)", -PTR_ERR(skb));
1277 static int btusb_setup_csr(struct hci_dev *hdev)
1279 struct hci_rp_read_local_version *rp;
1280 struct sk_buff *skb;
1283 BT_DBG("%s", hdev->name);
1285 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
1288 BT_ERR("Reading local version failed (%ld)", -PTR_ERR(skb));
1289 return -PTR_ERR(skb);
1292 rp = (struct hci_rp_read_local_version *)skb->data;
1295 if (le16_to_cpu(rp->manufacturer) != 10) {
1296 /* Clear the reset quirk since this is not an actual
1297 * early Bluetooth 1.1 device from CSR.
1299 clear_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
1301 /* These fake CSR controllers have all a broken
1302 * stored link key handling and so just disable it.
1304 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY,
1309 ret = -bt_to_errno(rp->status);
1316 struct intel_version {
1329 struct intel_boot_params {
1341 bdaddr_t otp_bdaddr;
1342 __u8 min_fw_build_nn;
1343 __u8 min_fw_build_cw;
1344 __u8 min_fw_build_yy;
1346 __u8 unlocked_state;
1349 static const struct firmware *btusb_setup_intel_get_fw(struct hci_dev *hdev,
1350 struct intel_version *ver)
1352 const struct firmware *fw;
1356 snprintf(fwname, sizeof(fwname),
1357 "intel/ibt-hw-%x.%x.%x-fw-%x.%x.%x.%x.%x.bseq",
1358 ver->hw_platform, ver->hw_variant, ver->hw_revision,
1359 ver->fw_variant, ver->fw_revision, ver->fw_build_num,
1360 ver->fw_build_ww, ver->fw_build_yy);
1362 ret = request_firmware(&fw, fwname, &hdev->dev);
1364 if (ret == -EINVAL) {
1365 BT_ERR("%s Intel firmware file request failed (%d)",
1370 BT_ERR("%s failed to open Intel firmware file: %s(%d)",
1371 hdev->name, fwname, ret);
1373 /* If the correct firmware patch file is not found, use the
1374 * default firmware patch file instead
1376 snprintf(fwname, sizeof(fwname), "intel/ibt-hw-%x.%x.bseq",
1377 ver->hw_platform, ver->hw_variant);
1378 if (request_firmware(&fw, fwname, &hdev->dev) < 0) {
1379 BT_ERR("%s failed to open default Intel fw file: %s",
1380 hdev->name, fwname);
1385 BT_INFO("%s: Intel Bluetooth firmware file: %s", hdev->name, fwname);
1390 static int btusb_setup_intel_patching(struct hci_dev *hdev,
1391 const struct firmware *fw,
1392 const u8 **fw_ptr, int *disable_patch)
1394 struct sk_buff *skb;
1395 struct hci_command_hdr *cmd;
1396 const u8 *cmd_param;
1397 struct hci_event_hdr *evt = NULL;
1398 const u8 *evt_param = NULL;
1399 int remain = fw->size - (*fw_ptr - fw->data);
1401 /* The first byte indicates the types of the patch command or event.
1402 * 0x01 means HCI command and 0x02 is HCI event. If the first bytes
1403 * in the current firmware buffer doesn't start with 0x01 or
1404 * the size of remain buffer is smaller than HCI command header,
1405 * the firmware file is corrupted and it should stop the patching
1408 if (remain > HCI_COMMAND_HDR_SIZE && *fw_ptr[0] != 0x01) {
1409 BT_ERR("%s Intel fw corrupted: invalid cmd read", hdev->name);
1415 cmd = (struct hci_command_hdr *)(*fw_ptr);
1416 *fw_ptr += sizeof(*cmd);
1417 remain -= sizeof(*cmd);
1419 /* Ensure that the remain firmware data is long enough than the length
1420 * of command parameter. If not, the firmware file is corrupted.
1422 if (remain < cmd->plen) {
1423 BT_ERR("%s Intel fw corrupted: invalid cmd len", hdev->name);
1427 /* If there is a command that loads a patch in the firmware
1428 * file, then enable the patch upon success, otherwise just
1429 * disable the manufacturer mode, for example patch activation
1430 * is not required when the default firmware patch file is used
1431 * because there are no patch data to load.
1433 if (*disable_patch && le16_to_cpu(cmd->opcode) == 0xfc8e)
1436 cmd_param = *fw_ptr;
1437 *fw_ptr += cmd->plen;
1438 remain -= cmd->plen;
1440 /* This reads the expected events when the above command is sent to the
1441 * device. Some vendor commands expects more than one events, for
1442 * example command status event followed by vendor specific event.
1443 * For this case, it only keeps the last expected event. so the command
1444 * can be sent with __hci_cmd_sync_ev() which returns the sk_buff of
1445 * last expected event.
1447 while (remain > HCI_EVENT_HDR_SIZE && *fw_ptr[0] == 0x02) {
1451 evt = (struct hci_event_hdr *)(*fw_ptr);
1452 *fw_ptr += sizeof(*evt);
1453 remain -= sizeof(*evt);
1455 if (remain < evt->plen) {
1456 BT_ERR("%s Intel fw corrupted: invalid evt len",
1461 evt_param = *fw_ptr;
1462 *fw_ptr += evt->plen;
1463 remain -= evt->plen;
1466 /* Every HCI commands in the firmware file has its correspond event.
1467 * If event is not found or remain is smaller than zero, the firmware
1468 * file is corrupted.
1470 if (!evt || !evt_param || remain < 0) {
1471 BT_ERR("%s Intel fw corrupted: invalid evt read", hdev->name);
1475 skb = __hci_cmd_sync_ev(hdev, le16_to_cpu(cmd->opcode), cmd->plen,
1476 cmd_param, evt->evt, HCI_INIT_TIMEOUT);
1478 BT_ERR("%s sending Intel patch command (0x%4.4x) failed (%ld)",
1479 hdev->name, cmd->opcode, PTR_ERR(skb));
1480 return PTR_ERR(skb);
1483 /* It ensures that the returned event matches the event data read from
1484 * the firmware file. At fist, it checks the length and then
1485 * the contents of the event.
1487 if (skb->len != evt->plen) {
1488 BT_ERR("%s mismatch event length (opcode 0x%4.4x)", hdev->name,
1489 le16_to_cpu(cmd->opcode));
1494 if (memcmp(skb->data, evt_param, evt->plen)) {
1495 BT_ERR("%s mismatch event parameter (opcode 0x%4.4x)",
1496 hdev->name, le16_to_cpu(cmd->opcode));
1505 #define BDADDR_INTEL (&(bdaddr_t) {{0x00, 0x8b, 0x9e, 0x19, 0x03, 0x00}})
1507 static int btusb_check_bdaddr_intel(struct hci_dev *hdev)
1509 struct sk_buff *skb;
1510 struct hci_rp_read_bd_addr *rp;
1512 skb = __hci_cmd_sync(hdev, HCI_OP_READ_BD_ADDR, 0, NULL,
1515 BT_ERR("%s reading Intel device address failed (%ld)",
1516 hdev->name, PTR_ERR(skb));
1517 return PTR_ERR(skb);
1520 if (skb->len != sizeof(*rp)) {
1521 BT_ERR("%s Intel device address length mismatch", hdev->name);
1526 rp = (struct hci_rp_read_bd_addr *)skb->data;
1528 BT_ERR("%s Intel device address result failed (%02x)",
1529 hdev->name, rp->status);
1531 return -bt_to_errno(rp->status);
1534 /* For some Intel based controllers, the default Bluetooth device
1535 * address 00:03:19:9E:8B:00 can be found. These controllers are
1536 * fully operational, but have the danger of duplicate addresses
1537 * and that in turn can cause problems with Bluetooth operation.
1539 if (!bacmp(&rp->bdaddr, BDADDR_INTEL)) {
1540 BT_ERR("%s found Intel default device address (%pMR)",
1541 hdev->name, &rp->bdaddr);
1542 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
1550 static int btusb_setup_intel(struct hci_dev *hdev)
1552 struct sk_buff *skb;
1553 const struct firmware *fw;
1556 struct intel_version *ver;
1558 const u8 mfg_enable[] = { 0x01, 0x00 };
1559 const u8 mfg_disable[] = { 0x00, 0x00 };
1560 const u8 mfg_reset_deactivate[] = { 0x00, 0x01 };
1561 const u8 mfg_reset_activate[] = { 0x00, 0x02 };
1563 BT_DBG("%s", hdev->name);
1565 /* The controller has a bug with the first HCI command sent to it
1566 * returning number of completed commands as zero. This would stall the
1567 * command processing in the Bluetooth core.
1569 * As a workaround, send HCI Reset command first which will reset the
1570 * number of completed commands and allow normal command processing
1573 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
1575 BT_ERR("%s sending initial HCI reset command failed (%ld)",
1576 hdev->name, PTR_ERR(skb));
1577 return PTR_ERR(skb);
1581 /* Read Intel specific controller version first to allow selection of
1582 * which firmware file to load.
1584 * The returned information are hardware variant and revision plus
1585 * firmware variant, revision and build number.
1587 skb = __hci_cmd_sync(hdev, 0xfc05, 0, NULL, HCI_INIT_TIMEOUT);
1589 BT_ERR("%s reading Intel fw version command failed (%ld)",
1590 hdev->name, PTR_ERR(skb));
1591 return PTR_ERR(skb);
1594 if (skb->len != sizeof(*ver)) {
1595 BT_ERR("%s Intel version event length mismatch", hdev->name);
1600 ver = (struct intel_version *)skb->data;
1602 BT_ERR("%s Intel fw version event failed (%02x)", hdev->name,
1605 return -bt_to_errno(ver->status);
1608 BT_INFO("%s: read Intel version: %02x%02x%02x%02x%02x%02x%02x%02x%02x",
1609 hdev->name, ver->hw_platform, ver->hw_variant,
1610 ver->hw_revision, ver->fw_variant, ver->fw_revision,
1611 ver->fw_build_num, ver->fw_build_ww, ver->fw_build_yy,
1614 /* fw_patch_num indicates the version of patch the device currently
1615 * have. If there is no patch data in the device, it is always 0x00.
1616 * So, if it is other than 0x00, no need to patch the deivce again.
1618 if (ver->fw_patch_num) {
1619 BT_INFO("%s: Intel device is already patched. patch num: %02x",
1620 hdev->name, ver->fw_patch_num);
1622 btusb_check_bdaddr_intel(hdev);
1626 /* Opens the firmware patch file based on the firmware version read
1627 * from the controller. If it fails to open the matching firmware
1628 * patch file, it tries to open the default firmware patch file.
1629 * If no patch file is found, allow the device to operate without
1632 fw = btusb_setup_intel_get_fw(hdev, ver);
1635 btusb_check_bdaddr_intel(hdev);
1640 /* This Intel specific command enables the manufacturer mode of the
1643 * Only while this mode is enabled, the driver can download the
1644 * firmware patch data and configuration parameters.
1646 skb = __hci_cmd_sync(hdev, 0xfc11, 2, mfg_enable, HCI_INIT_TIMEOUT);
1648 BT_ERR("%s entering Intel manufacturer mode failed (%ld)",
1649 hdev->name, PTR_ERR(skb));
1650 release_firmware(fw);
1651 return PTR_ERR(skb);
1655 u8 evt_status = skb->data[0];
1657 BT_ERR("%s enable Intel manufacturer mode event failed (%02x)",
1658 hdev->name, evt_status);
1660 release_firmware(fw);
1661 return -bt_to_errno(evt_status);
1667 /* The firmware data file consists of list of Intel specific HCI
1668 * commands and its expected events. The first byte indicates the
1669 * type of the message, either HCI command or HCI event.
1671 * It reads the command and its expected event from the firmware file,
1672 * and send to the controller. Once __hci_cmd_sync_ev() returns,
1673 * the returned event is compared with the event read from the firmware
1674 * file and it will continue until all the messages are downloaded to
1677 * Once the firmware patching is completed successfully,
1678 * the manufacturer mode is disabled with reset and activating the
1681 * If the firmware patching fails, the manufacturer mode is
1682 * disabled with reset and deactivating the patch.
1684 * If the default patch file is used, no reset is done when disabling
1687 while (fw->size > fw_ptr - fw->data) {
1690 ret = btusb_setup_intel_patching(hdev, fw, &fw_ptr,
1693 goto exit_mfg_deactivate;
1696 release_firmware(fw);
1699 goto exit_mfg_disable;
1701 /* Patching completed successfully and disable the manufacturer mode
1702 * with reset and activate the downloaded firmware patches.
1704 skb = __hci_cmd_sync(hdev, 0xfc11, sizeof(mfg_reset_activate),
1705 mfg_reset_activate, HCI_INIT_TIMEOUT);
1707 BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
1708 hdev->name, PTR_ERR(skb));
1709 return PTR_ERR(skb);
1713 BT_INFO("%s: Intel Bluetooth firmware patch completed and activated",
1716 btusb_check_bdaddr_intel(hdev);
1720 /* Disable the manufacturer mode without reset */
1721 skb = __hci_cmd_sync(hdev, 0xfc11, sizeof(mfg_disable), mfg_disable,
1724 BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
1725 hdev->name, PTR_ERR(skb));
1726 return PTR_ERR(skb);
1730 BT_INFO("%s: Intel Bluetooth firmware patch completed", hdev->name);
1732 btusb_check_bdaddr_intel(hdev);
1735 exit_mfg_deactivate:
1736 release_firmware(fw);
1738 /* Patching failed. Disable the manufacturer mode with reset and
1739 * deactivate the downloaded firmware patches.
1741 skb = __hci_cmd_sync(hdev, 0xfc11, sizeof(mfg_reset_deactivate),
1742 mfg_reset_deactivate, HCI_INIT_TIMEOUT);
1744 BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
1745 hdev->name, PTR_ERR(skb));
1746 return PTR_ERR(skb);
1750 BT_INFO("%s: Intel Bluetooth firmware patch completed and deactivated",
1753 btusb_check_bdaddr_intel(hdev);
1757 static int inject_cmd_complete(struct hci_dev *hdev, __u16 opcode)
1759 struct sk_buff *skb;
1760 struct hci_event_hdr *hdr;
1761 struct hci_ev_cmd_complete *evt;
1763 skb = bt_skb_alloc(sizeof(*hdr) + sizeof(*evt) + 1, GFP_ATOMIC);
1767 hdr = (struct hci_event_hdr *)skb_put(skb, sizeof(*hdr));
1768 hdr->evt = HCI_EV_CMD_COMPLETE;
1769 hdr->plen = sizeof(*evt) + 1;
1771 evt = (struct hci_ev_cmd_complete *)skb_put(skb, sizeof(*evt));
1773 evt->opcode = cpu_to_le16(opcode);
1775 *skb_put(skb, 1) = 0x00;
1777 bt_cb(skb)->pkt_type = HCI_EVENT_PKT;
1779 return hci_recv_frame(hdev, skb);
1782 static int btusb_recv_bulk_intel(struct btusb_data *data, void *buffer,
1785 /* When the device is in bootloader mode, then it can send
1786 * events via the bulk endpoint. These events are treated the
1787 * same way as the ones received from the interrupt endpoint.
1789 if (test_bit(BTUSB_BOOTLOADER, &data->flags))
1790 return btusb_recv_intr(data, buffer, count);
1792 return btusb_recv_bulk(data, buffer, count);
1795 static int btusb_recv_event_intel(struct hci_dev *hdev, struct sk_buff *skb)
1797 struct btusb_data *data = hci_get_drvdata(hdev);
1799 if (test_bit(BTUSB_BOOTLOADER, &data->flags)) {
1800 struct hci_event_hdr *hdr = (void *)skb->data;
1802 /* When the firmware loading completes the device sends
1803 * out a vendor specific event indicating the result of
1804 * the firmware loading.
1806 if (skb->len == 7 && hdr->evt == 0xff && hdr->plen == 0x05 &&
1807 skb->data[2] == 0x06) {
1808 if (skb->data[3] != 0x00)
1809 test_bit(BTUSB_FIRMWARE_FAILED, &data->flags);
1811 if (test_and_clear_bit(BTUSB_DOWNLOADING,
1813 test_bit(BTUSB_FIRMWARE_LOADED, &data->flags)) {
1814 smp_mb__after_atomic();
1815 wake_up_bit(&data->flags, BTUSB_DOWNLOADING);
1819 /* When switching to the operational firmware the device
1820 * sends a vendor specific event indicating that the bootup
1823 if (skb->len == 9 && hdr->evt == 0xff && hdr->plen == 0x07 &&
1824 skb->data[2] == 0x02) {
1825 if (test_and_clear_bit(BTUSB_BOOTING, &data->flags)) {
1826 smp_mb__after_atomic();
1827 wake_up_bit(&data->flags, BTUSB_BOOTING);
1832 return hci_recv_frame(hdev, skb);
1835 static int btusb_send_frame_intel(struct hci_dev *hdev, struct sk_buff *skb)
1837 struct btusb_data *data = hci_get_drvdata(hdev);
1840 BT_DBG("%s", hdev->name);
1842 if (!test_bit(HCI_RUNNING, &hdev->flags))
1845 switch (bt_cb(skb)->pkt_type) {
1846 case HCI_COMMAND_PKT:
1847 if (test_bit(BTUSB_BOOTLOADER, &data->flags)) {
1848 struct hci_command_hdr *cmd = (void *)skb->data;
1849 __u16 opcode = le16_to_cpu(cmd->opcode);
1851 /* When in bootloader mode and the command 0xfc09
1852 * is received, it needs to be send down the
1853 * bulk endpoint. So allocate a bulk URB instead.
1855 if (opcode == 0xfc09)
1856 urb = alloc_bulk_urb(hdev, skb);
1858 urb = alloc_ctrl_urb(hdev, skb);
1860 /* When the 0xfc01 command is issued to boot into
1861 * the operational firmware, it will actually not
1862 * send a command complete event. To keep the flow
1863 * control working inject that event here.
1865 if (opcode == 0xfc01)
1866 inject_cmd_complete(hdev, opcode);
1868 urb = alloc_ctrl_urb(hdev, skb);
1871 return PTR_ERR(urb);
1873 hdev->stat.cmd_tx++;
1874 return submit_or_queue_tx_urb(hdev, urb);
1876 case HCI_ACLDATA_PKT:
1877 urb = alloc_bulk_urb(hdev, skb);
1879 return PTR_ERR(urb);
1881 hdev->stat.acl_tx++;
1882 return submit_or_queue_tx_urb(hdev, urb);
1884 case HCI_SCODATA_PKT:
1885 if (hci_conn_num(hdev, SCO_LINK) < 1)
1888 urb = alloc_isoc_urb(hdev, skb);
1890 return PTR_ERR(urb);
1892 hdev->stat.sco_tx++;
1893 return submit_tx_urb(hdev, urb);
1899 static int btusb_intel_secure_send(struct hci_dev *hdev, u8 fragment_type,
1900 u32 plen, const void *param)
1903 struct sk_buff *skb;
1904 u8 cmd_param[253], fragment_len = (plen > 252) ? 252 : plen;
1906 cmd_param[0] = fragment_type;
1907 memcpy(cmd_param + 1, param, fragment_len);
1909 skb = __hci_cmd_sync(hdev, 0xfc09, fragment_len + 1,
1910 cmd_param, HCI_INIT_TIMEOUT);
1912 return PTR_ERR(skb);
1916 plen -= fragment_len;
1917 param += fragment_len;
1923 static void btusb_intel_version_info(struct hci_dev *hdev,
1924 struct intel_version *ver)
1926 const char *variant;
1928 switch (ver->fw_variant) {
1930 variant = "Bootloader";
1933 variant = "Firmware";
1939 BT_INFO("%s: %s revision %u.%u build %u week %u %u", hdev->name,
1940 variant, ver->fw_revision >> 4, ver->fw_revision & 0x0f,
1941 ver->fw_build_num, ver->fw_build_ww, 2000 + ver->fw_build_yy);
1944 static int btusb_setup_intel_new(struct hci_dev *hdev)
1946 static const u8 reset_param[] = { 0x00, 0x01, 0x00, 0x01,
1947 0x00, 0x08, 0x04, 0x00 };
1948 struct btusb_data *data = hci_get_drvdata(hdev);
1949 struct sk_buff *skb;
1950 struct intel_version *ver;
1951 struct intel_boot_params *params;
1952 const struct firmware *fw;
1955 ktime_t calltime, delta, rettime;
1956 unsigned long long duration;
1959 BT_DBG("%s", hdev->name);
1961 calltime = ktime_get();
1963 /* Read the Intel version information to determine if the device
1964 * is in bootloader mode or if it already has operational firmware
1967 skb = __hci_cmd_sync(hdev, 0xfc05, 0, NULL, HCI_INIT_TIMEOUT);
1969 BT_ERR("%s: Reading Intel version information failed (%ld)",
1970 hdev->name, PTR_ERR(skb));
1971 return PTR_ERR(skb);
1974 if (skb->len != sizeof(*ver)) {
1975 BT_ERR("%s: Intel version event size mismatch", hdev->name);
1980 ver = (struct intel_version *)skb->data;
1982 BT_ERR("%s: Intel version command failure (%02x)",
1983 hdev->name, ver->status);
1984 err = -bt_to_errno(ver->status);
1989 /* The hardware platform number has a fixed value of 0x37 and
1990 * for now only accept this single value.
1992 if (ver->hw_platform != 0x37) {
1993 BT_ERR("%s: Unsupported Intel hardware platform (%u)",
1994 hdev->name, ver->hw_platform);
1999 /* At the moment only the hardware variant iBT 3.0 (LnP/SfP) is
2000 * supported by this firmware loading method. This check has been
2001 * put in place to ensure correct forward compatibility options
2002 * when newer hardware variants come along.
2004 if (ver->hw_variant != 0x0b) {
2005 BT_ERR("%s: Unsupported Intel hardware variant (%u)",
2006 hdev->name, ver->hw_variant);
2011 btusb_intel_version_info(hdev, ver);
2013 /* The firmware variant determines if the device is in bootloader
2014 * mode or is running operational firmware. The value 0x06 identifies
2015 * the bootloader and the value 0x23 identifies the operational
2018 * When the operational firmware is already present, then only
2019 * the check for valid Bluetooth device address is needed. This
2020 * determines if the device will be added as configured or
2021 * unconfigured controller.
2023 * It is not possible to use the Secure Boot Parameters in this
2024 * case since that command is only available in bootloader mode.
2026 if (ver->fw_variant == 0x23) {
2028 clear_bit(BTUSB_BOOTLOADER, &data->flags);
2029 btusb_check_bdaddr_intel(hdev);
2033 /* If the device is not in bootloader mode, then the only possible
2034 * choice is to return an error and abort the device initialization.
2036 if (ver->fw_variant != 0x06) {
2037 BT_ERR("%s: Unsupported Intel firmware variant (%u)",
2038 hdev->name, ver->fw_variant);
2045 /* Read the secure boot parameters to identify the operating
2046 * details of the bootloader.
2048 skb = __hci_cmd_sync(hdev, 0xfc0d, 0, NULL, HCI_INIT_TIMEOUT);
2050 BT_ERR("%s: Reading Intel boot parameters failed (%ld)",
2051 hdev->name, PTR_ERR(skb));
2052 return PTR_ERR(skb);
2055 if (skb->len != sizeof(*params)) {
2056 BT_ERR("%s: Intel boot parameters size mismatch", hdev->name);
2061 params = (struct intel_boot_params *)skb->data;
2062 if (params->status) {
2063 BT_ERR("%s: Intel boot parameters command failure (%02x)",
2064 hdev->name, params->status);
2065 err = -bt_to_errno(params->status);
2070 BT_INFO("%s: Device revision is %u", hdev->name,
2071 le16_to_cpu(params->dev_revid));
2073 BT_INFO("%s: Secure boot is %s", hdev->name,
2074 params->secure_boot ? "enabled" : "disabled");
2076 BT_INFO("%s: Minimum firmware build %u week %u %u", hdev->name,
2077 params->min_fw_build_nn, params->min_fw_build_cw,
2078 2000 + params->min_fw_build_yy);
2080 /* It is required that every single firmware fragment is acknowledged
2081 * with a command complete event. If the boot parameters indicate
2082 * that this bootloader does not send them, then abort the setup.
2084 if (params->limited_cce != 0x00) {
2085 BT_ERR("%s: Unsupported Intel firmware loading method (%u)",
2086 hdev->name, params->limited_cce);
2091 /* If the OTP has no valid Bluetooth device address, then there will
2092 * also be no valid address for the operational firmware.
2094 if (!bacmp(¶ms->otp_bdaddr, BDADDR_ANY)) {
2095 BT_INFO("%s: No device address configured", hdev->name);
2096 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
2099 /* With this Intel bootloader only the hardware variant and device
2100 * revision information are used to select the right firmware.
2102 * Currently this bootloader support is limited to hardware variant
2103 * iBT 3.0 (LnP/SfP) which is identified by the value 11 (0x0b).
2105 snprintf(fwname, sizeof(fwname), "intel/ibt-11-%u.sfi",
2106 le16_to_cpu(params->dev_revid));
2108 err = request_firmware(&fw, fwname, &hdev->dev);
2110 BT_ERR("%s: Failed to load Intel firmware file (%d)",
2116 BT_INFO("%s: Found device firmware: %s", hdev->name, fwname);
2120 if (fw->size < 644) {
2121 BT_ERR("%s: Invalid size of firmware file (%zu)",
2122 hdev->name, fw->size);
2127 set_bit(BTUSB_DOWNLOADING, &data->flags);
2129 /* Start the firmware download transaction with the Init fragment
2130 * represented by the 128 bytes of CSS header.
2132 err = btusb_intel_secure_send(hdev, 0x00, 128, fw->data);
2134 BT_ERR("%s: Failed to send firmware header (%d)",
2139 /* Send the 256 bytes of public key information from the firmware
2140 * as the PKey fragment.
2142 err = btusb_intel_secure_send(hdev, 0x03, 256, fw->data + 128);
2144 BT_ERR("%s: Failed to send firmware public key (%d)",
2149 /* Send the 256 bytes of signature information from the firmware
2150 * as the Sign fragment.
2152 err = btusb_intel_secure_send(hdev, 0x02, 256, fw->data + 388);
2154 BT_ERR("%s: Failed to send firmware signature (%d)",
2159 fw_ptr = fw->data + 644;
2161 while (fw_ptr - fw->data < fw->size) {
2162 struct hci_command_hdr *cmd = (void *)fw_ptr;
2165 cmd_len = sizeof(*cmd) + cmd->plen;
2167 /* Send each command from the firmware data buffer as
2168 * a single Data fragment.
2170 err = btusb_intel_secure_send(hdev, 0x01, cmd_len, fw_ptr);
2172 BT_ERR("%s: Failed to send firmware data (%d)",
2180 set_bit(BTUSB_FIRMWARE_LOADED, &data->flags);
2182 BT_INFO("%s: Waiting for firmware download to complete", hdev->name);
2184 /* Before switching the device into operational mode and with that
2185 * booting the loaded firmware, wait for the bootloader notification
2186 * that all fragments have been successfully received.
2188 * When the event processing receives the notification, then the
2189 * BTUSB_DOWNLOADING flag will be cleared.
2191 * The firmware loading should not take longer than 5 seconds
2192 * and thus just timeout if that happens and fail the setup
2195 err = btusb_wait_on_bit_timeout(&data->flags, BTUSB_DOWNLOADING,
2196 msecs_to_jiffies(5000),
2197 TASK_INTERRUPTIBLE);
2199 BT_ERR("%s: Firmware loading interrupted", hdev->name);
2205 BT_ERR("%s: Firmware loading timeout", hdev->name);
2210 if (test_bit(BTUSB_FIRMWARE_FAILED, &data->flags)) {
2211 BT_ERR("%s: Firmware loading failed", hdev->name);
2216 rettime = ktime_get();
2217 delta = ktime_sub(rettime, calltime);
2218 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
2220 BT_INFO("%s: Firmware loaded in %llu usecs", hdev->name, duration);
2223 release_firmware(fw);
2228 calltime = ktime_get();
2230 set_bit(BTUSB_BOOTING, &data->flags);
2232 skb = __hci_cmd_sync(hdev, 0xfc01, sizeof(reset_param), reset_param,
2235 return PTR_ERR(skb);
2239 /* The bootloader will not indicate when the device is ready. This
2240 * is done by the operational firmware sending bootup notification.
2242 * Booting into operational firmware should not take longer than
2243 * 1 second. However if that happens, then just fail the setup
2244 * since something went wrong.
2246 BT_INFO("%s: Waiting for device to boot", hdev->name);
2248 err = btusb_wait_on_bit_timeout(&data->flags, BTUSB_BOOTING,
2249 msecs_to_jiffies(1000),
2250 TASK_INTERRUPTIBLE);
2253 BT_ERR("%s: Device boot interrupted", hdev->name);
2258 BT_ERR("%s: Device boot timeout", hdev->name);
2262 rettime = ktime_get();
2263 delta = ktime_sub(rettime, calltime);
2264 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
2266 BT_INFO("%s: Device booted in %llu usecs", hdev->name, duration);
2268 clear_bit(BTUSB_BOOTLOADER, &data->flags);
2273 static void btusb_hw_error_intel(struct hci_dev *hdev, u8 code)
2275 struct sk_buff *skb;
2278 BT_ERR("%s: Hardware error 0x%2.2x", hdev->name, code);
2280 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
2282 BT_ERR("%s: Reset after hardware error failed (%ld)",
2283 hdev->name, PTR_ERR(skb));
2288 skb = __hci_cmd_sync(hdev, 0xfc22, 1, &type, HCI_INIT_TIMEOUT);
2290 BT_ERR("%s: Retrieving Intel exception info failed (%ld)",
2291 hdev->name, PTR_ERR(skb));
2295 if (skb->len != 13) {
2296 BT_ERR("%s: Exception info size mismatch", hdev->name);
2301 if (skb->data[0] != 0x00) {
2302 BT_ERR("%s: Exception info command failure (%02x)",
2303 hdev->name, skb->data[0]);
2308 BT_ERR("%s: Exception info %s", hdev->name, (char *)(skb->data + 1));
2313 static int btusb_set_bdaddr_intel(struct hci_dev *hdev, const bdaddr_t *bdaddr)
2315 struct sk_buff *skb;
2318 skb = __hci_cmd_sync(hdev, 0xfc31, 6, bdaddr, HCI_INIT_TIMEOUT);
2321 BT_ERR("%s: changing Intel device address failed (%ld)",
2330 static int btusb_set_bdaddr_marvell(struct hci_dev *hdev,
2331 const bdaddr_t *bdaddr)
2333 struct sk_buff *skb;
2338 buf[1] = sizeof(bdaddr_t);
2339 memcpy(buf + 2, bdaddr, sizeof(bdaddr_t));
2341 skb = __hci_cmd_sync(hdev, 0xfc22, sizeof(buf), buf, HCI_INIT_TIMEOUT);
2344 BT_ERR("%s: changing Marvell device address failed (%ld)",
2353 #define BDADDR_BCM20702A0 (&(bdaddr_t) {{0x00, 0xa0, 0x02, 0x70, 0x20, 0x00}})
2355 static int btusb_setup_bcm_patchram(struct hci_dev *hdev)
2357 struct btusb_data *data = hci_get_drvdata(hdev);
2358 struct usb_device *udev = data->udev;
2360 const struct firmware *fw;
2363 const struct hci_command_hdr *cmd;
2364 const u8 *cmd_param;
2366 struct sk_buff *skb;
2367 struct hci_rp_read_local_version *ver;
2368 struct hci_rp_read_bd_addr *bda;
2371 snprintf(fw_name, sizeof(fw_name), "brcm/%s-%04x-%04x.hcd",
2372 udev->product ? udev->product : "BCM",
2373 le16_to_cpu(udev->descriptor.idVendor),
2374 le16_to_cpu(udev->descriptor.idProduct));
2376 ret = request_firmware(&fw, fw_name, &hdev->dev);
2378 BT_INFO("%s: BCM: patch %s not found", hdev->name, fw_name);
2383 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
2386 BT_ERR("%s: HCI_OP_RESET failed (%ld)", hdev->name, ret);
2391 /* Read Local Version Info */
2392 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
2396 BT_ERR("%s: HCI_OP_READ_LOCAL_VERSION failed (%ld)",
2401 if (skb->len != sizeof(*ver)) {
2402 BT_ERR("%s: HCI_OP_READ_LOCAL_VERSION event length mismatch",
2409 ver = (struct hci_rp_read_local_version *)skb->data;
2410 BT_INFO("%s: BCM: patching hci_ver=%02x hci_rev=%04x lmp_ver=%02x "
2411 "lmp_subver=%04x", hdev->name, ver->hci_ver, ver->hci_rev,
2412 ver->lmp_ver, ver->lmp_subver);
2415 /* Start Download */
2416 skb = __hci_cmd_sync(hdev, 0xfc2e, 0, NULL, HCI_INIT_TIMEOUT);
2419 BT_ERR("%s: BCM: Download Minidrv command failed (%ld)",
2425 /* 50 msec delay after Download Minidrv completes */
2431 while (fw_size >= sizeof(*cmd)) {
2432 cmd = (struct hci_command_hdr *)fw_ptr;
2433 fw_ptr += sizeof(*cmd);
2434 fw_size -= sizeof(*cmd);
2436 if (fw_size < cmd->plen) {
2437 BT_ERR("%s: BCM: patch %s is corrupted",
2438 hdev->name, fw_name);
2444 fw_ptr += cmd->plen;
2445 fw_size -= cmd->plen;
2447 opcode = le16_to_cpu(cmd->opcode);
2449 skb = __hci_cmd_sync(hdev, opcode, cmd->plen, cmd_param,
2453 BT_ERR("%s: BCM: patch command %04x failed (%ld)",
2454 hdev->name, opcode, ret);
2460 /* 250 msec delay after Launch Ram completes */
2465 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
2468 BT_ERR("%s: HCI_OP_RESET failed (%ld)", hdev->name, ret);
2473 /* Read Local Version Info */
2474 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
2478 BT_ERR("%s: HCI_OP_READ_LOCAL_VERSION failed (%ld)",
2483 if (skb->len != sizeof(*ver)) {
2484 BT_ERR("%s: HCI_OP_READ_LOCAL_VERSION event length mismatch",
2491 ver = (struct hci_rp_read_local_version *)skb->data;
2492 BT_INFO("%s: BCM: firmware hci_ver=%02x hci_rev=%04x lmp_ver=%02x "
2493 "lmp_subver=%04x", hdev->name, ver->hci_ver, ver->hci_rev,
2494 ver->lmp_ver, ver->lmp_subver);
2497 /* Read BD Address */
2498 skb = __hci_cmd_sync(hdev, HCI_OP_READ_BD_ADDR, 0, NULL,
2502 BT_ERR("%s: HCI_OP_READ_BD_ADDR failed (%ld)",
2507 if (skb->len != sizeof(*bda)) {
2508 BT_ERR("%s: HCI_OP_READ_BD_ADDR event length mismatch",
2515 bda = (struct hci_rp_read_bd_addr *)skb->data;
2517 BT_ERR("%s: HCI_OP_READ_BD_ADDR error status (%02x)",
2518 hdev->name, bda->status);
2520 ret = -bt_to_errno(bda->status);
2524 /* The address 00:20:70:02:A0:00 indicates a BCM20702A0 controller
2525 * with no configured address.
2527 if (!bacmp(&bda->bdaddr, BDADDR_BCM20702A0)) {
2528 BT_INFO("%s: BCM: using default device address (%pMR)",
2529 hdev->name, &bda->bdaddr);
2530 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
2536 release_firmware(fw);
2541 static int btusb_set_bdaddr_bcm(struct hci_dev *hdev, const bdaddr_t *bdaddr)
2543 struct sk_buff *skb;
2546 skb = __hci_cmd_sync(hdev, 0xfc01, 6, bdaddr, HCI_INIT_TIMEOUT);
2549 BT_ERR("%s: BCM: Change address command failed (%ld)",
2558 static int btusb_set_bdaddr_ath3012(struct hci_dev *hdev,
2559 const bdaddr_t *bdaddr)
2561 struct sk_buff *skb;
2568 buf[3] = sizeof(bdaddr_t);
2569 memcpy(buf + 4, bdaddr, sizeof(bdaddr_t));
2571 skb = __hci_cmd_sync(hdev, 0xfc0b, sizeof(buf), buf, HCI_INIT_TIMEOUT);
2574 BT_ERR("%s: Change address command failed (%ld)",
2583 static int btusb_probe(struct usb_interface *intf,
2584 const struct usb_device_id *id)
2586 struct usb_endpoint_descriptor *ep_desc;
2587 struct btusb_data *data;
2588 struct hci_dev *hdev;
2591 BT_DBG("intf %p id %p", intf, id);
2593 /* interface numbers are hardcoded in the spec */
2594 if (intf->cur_altsetting->desc.bInterfaceNumber != 0)
2597 if (!id->driver_info) {
2598 const struct usb_device_id *match;
2600 match = usb_match_id(intf, blacklist_table);
2605 if (id->driver_info == BTUSB_IGNORE)
2608 if (id->driver_info & BTUSB_ATH3012) {
2609 struct usb_device *udev = interface_to_usbdev(intf);
2611 /* Old firmware would otherwise let ath3k driver load
2612 * patch and sysconfig files */
2613 if (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x0001)
2617 data = devm_kzalloc(&intf->dev, sizeof(*data), GFP_KERNEL);
2621 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
2622 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
2624 if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
2625 data->intr_ep = ep_desc;
2629 if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
2630 data->bulk_tx_ep = ep_desc;
2634 if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
2635 data->bulk_rx_ep = ep_desc;
2640 if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep)
2643 if (id->driver_info & BTUSB_AMP) {
2644 data->cmdreq_type = USB_TYPE_CLASS | 0x01;
2645 data->cmdreq = 0x2b;
2647 data->cmdreq_type = USB_TYPE_CLASS;
2648 data->cmdreq = 0x00;
2651 data->udev = interface_to_usbdev(intf);
2654 INIT_WORK(&data->work, btusb_work);
2655 INIT_WORK(&data->waker, btusb_waker);
2656 init_usb_anchor(&data->deferred);
2657 init_usb_anchor(&data->tx_anchor);
2658 spin_lock_init(&data->txlock);
2660 init_usb_anchor(&data->intr_anchor);
2661 init_usb_anchor(&data->bulk_anchor);
2662 init_usb_anchor(&data->isoc_anchor);
2663 spin_lock_init(&data->rxlock);
2665 if (id->driver_info & BTUSB_INTEL_NEW) {
2666 data->recv_event = btusb_recv_event_intel;
2667 data->recv_bulk = btusb_recv_bulk_intel;
2668 set_bit(BTUSB_BOOTLOADER, &data->flags);
2670 data->recv_event = hci_recv_frame;
2671 data->recv_bulk = btusb_recv_bulk;
2674 hdev = hci_alloc_dev();
2678 hdev->bus = HCI_USB;
2679 hci_set_drvdata(hdev, data);
2681 if (id->driver_info & BTUSB_AMP)
2682 hdev->dev_type = HCI_AMP;
2684 hdev->dev_type = HCI_BREDR;
2688 SET_HCIDEV_DEV(hdev, &intf->dev);
2690 hdev->open = btusb_open;
2691 hdev->close = btusb_close;
2692 hdev->flush = btusb_flush;
2693 hdev->send = btusb_send_frame;
2694 hdev->notify = btusb_notify;
2696 if (id->driver_info & BTUSB_BCM92035)
2697 hdev->setup = btusb_setup_bcm92035;
2699 if (id->driver_info & BTUSB_BCM_PATCHRAM) {
2700 hdev->setup = btusb_setup_bcm_patchram;
2701 hdev->set_bdaddr = btusb_set_bdaddr_bcm;
2702 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
2705 if (id->driver_info & BTUSB_INTEL) {
2706 hdev->setup = btusb_setup_intel;
2707 hdev->set_bdaddr = btusb_set_bdaddr_intel;
2708 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
2711 if (id->driver_info & BTUSB_INTEL_NEW) {
2712 hdev->send = btusb_send_frame_intel;
2713 hdev->setup = btusb_setup_intel_new;
2714 hdev->hw_error = btusb_hw_error_intel;
2715 hdev->set_bdaddr = btusb_set_bdaddr_intel;
2716 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
2719 if (id->driver_info & BTUSB_MARVELL)
2720 hdev->set_bdaddr = btusb_set_bdaddr_marvell;
2722 if (id->driver_info & BTUSB_SWAVE) {
2723 set_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks);
2724 set_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS, &hdev->quirks);
2727 if (id->driver_info & BTUSB_INTEL_BOOT)
2728 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
2730 if (id->driver_info & BTUSB_ATH3012) {
2731 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
2732 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
2735 if (id->driver_info & BTUSB_AMP) {
2736 /* AMP controllers do not support SCO packets */
2739 /* Interface numbers are hardcoded in the specification */
2740 data->isoc = usb_ifnum_to_if(data->udev, 1);
2744 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
2746 if (force_scofix || id->driver_info & BTUSB_WRONG_SCO_MTU) {
2747 if (!disable_scofix)
2748 set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
2751 if (id->driver_info & BTUSB_BROKEN_ISOC)
2754 if (id->driver_info & BTUSB_DIGIANSWER) {
2755 data->cmdreq_type = USB_TYPE_VENDOR;
2756 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
2759 if (id->driver_info & BTUSB_CSR) {
2760 struct usb_device *udev = data->udev;
2761 u16 bcdDevice = le16_to_cpu(udev->descriptor.bcdDevice);
2763 /* Old firmware would otherwise execute USB reset */
2764 if (bcdDevice < 0x117)
2765 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
2767 /* Fake CSR devices with broken commands */
2768 if (bcdDevice <= 0x100)
2769 hdev->setup = btusb_setup_csr;
2772 if (id->driver_info & BTUSB_SNIFFER) {
2773 struct usb_device *udev = data->udev;
2775 /* New sniffer firmware has crippled HCI interface */
2776 if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
2777 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
2780 if (id->driver_info & BTUSB_INTEL_BOOT) {
2781 /* A bug in the bootloader causes that interrupt interface is
2782 * only enabled after receiving SetInterface(0, AltSetting=0).
2784 err = usb_set_interface(data->udev, 0, 0);
2786 BT_ERR("failed to set interface 0, alt 0 %d", err);
2793 err = usb_driver_claim_interface(&btusb_driver,
2801 err = hci_register_dev(hdev);
2807 usb_set_intfdata(intf, data);
2812 static void btusb_disconnect(struct usb_interface *intf)
2814 struct btusb_data *data = usb_get_intfdata(intf);
2815 struct hci_dev *hdev;
2817 BT_DBG("intf %p", intf);
2823 usb_set_intfdata(data->intf, NULL);
2826 usb_set_intfdata(data->isoc, NULL);
2828 hci_unregister_dev(hdev);
2830 if (intf == data->isoc)
2831 usb_driver_release_interface(&btusb_driver, data->intf);
2832 else if (data->isoc)
2833 usb_driver_release_interface(&btusb_driver, data->isoc);
2839 static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
2841 struct btusb_data *data = usb_get_intfdata(intf);
2843 BT_DBG("intf %p", intf);
2845 if (data->suspend_count++)
2848 spin_lock_irq(&data->txlock);
2849 if (!(PMSG_IS_AUTO(message) && data->tx_in_flight)) {
2850 set_bit(BTUSB_SUSPENDING, &data->flags);
2851 spin_unlock_irq(&data->txlock);
2853 spin_unlock_irq(&data->txlock);
2854 data->suspend_count--;
2858 cancel_work_sync(&data->work);
2860 btusb_stop_traffic(data);
2861 usb_kill_anchored_urbs(&data->tx_anchor);
2866 static void play_deferred(struct btusb_data *data)
2871 while ((urb = usb_get_from_anchor(&data->deferred))) {
2872 err = usb_submit_urb(urb, GFP_ATOMIC);
2876 data->tx_in_flight++;
2878 usb_scuttle_anchored_urbs(&data->deferred);
2881 static int btusb_resume(struct usb_interface *intf)
2883 struct btusb_data *data = usb_get_intfdata(intf);
2884 struct hci_dev *hdev = data->hdev;
2887 BT_DBG("intf %p", intf);
2889 if (--data->suspend_count)
2892 if (!test_bit(HCI_RUNNING, &hdev->flags))
2895 if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
2896 err = btusb_submit_intr_urb(hdev, GFP_NOIO);
2898 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
2903 if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) {
2904 err = btusb_submit_bulk_urb(hdev, GFP_NOIO);
2906 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
2910 btusb_submit_bulk_urb(hdev, GFP_NOIO);
2913 if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
2914 if (btusb_submit_isoc_urb(hdev, GFP_NOIO) < 0)
2915 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
2917 btusb_submit_isoc_urb(hdev, GFP_NOIO);
2920 spin_lock_irq(&data->txlock);
2921 play_deferred(data);
2922 clear_bit(BTUSB_SUSPENDING, &data->flags);
2923 spin_unlock_irq(&data->txlock);
2924 schedule_work(&data->work);
2929 usb_scuttle_anchored_urbs(&data->deferred);
2931 spin_lock_irq(&data->txlock);
2932 clear_bit(BTUSB_SUSPENDING, &data->flags);
2933 spin_unlock_irq(&data->txlock);
2939 static struct usb_driver btusb_driver = {
2941 .probe = btusb_probe,
2942 .disconnect = btusb_disconnect,
2944 .suspend = btusb_suspend,
2945 .resume = btusb_resume,
2947 .id_table = btusb_table,
2948 .supports_autosuspend = 1,
2949 .disable_hub_initiated_lpm = 1,
2952 module_usb_driver(btusb_driver);
2954 module_param(disable_scofix, bool, 0644);
2955 MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
2957 module_param(force_scofix, bool, 0644);
2958 MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
2960 module_param(reset, bool, 0644);
2961 MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
2963 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
2964 MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
2965 MODULE_VERSION(VERSION);
2966 MODULE_LICENSE("GPL");