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 /* Lite-On Technology - Broadcom based */
126 { USB_VENDOR_AND_INTERFACE_INFO(0x04ca, 0xff, 0x01, 0x01),
127 .driver_info
= BTUSB_BCM_PATCHRAM
},
129 /* Broadcom devices with vendor specific id */
130 { USB_VENDOR_AND_INTERFACE_INFO(0x0a5c, 0xff, 0x01, 0x01),
131 .driver_info
= BTUSB_BCM_PATCHRAM
},
133 /* ASUSTek Computer - Broadcom based */
134 { USB_VENDOR_AND_INTERFACE_INFO(0x0b05, 0xff, 0x01, 0x01),
135 .driver_info
= BTUSB_BCM_PATCHRAM
},
137 /* Belkin F8065bf - Broadcom based */
138 { USB_VENDOR_AND_INTERFACE_INFO(0x050d, 0xff, 0x01, 0x01) },
140 /* IMC Networks - Broadcom based */
141 { USB_VENDOR_AND_INTERFACE_INFO(0x13d3, 0xff, 0x01, 0x01) },
143 /* Intel Bluetooth USB Bootloader (RAM module) */
144 { USB_DEVICE(0x8087, 0x0a5a),
145 .driver_info
= BTUSB_INTEL_BOOT
| BTUSB_BROKEN_ISOC
},
147 { } /* Terminating entry */
150 MODULE_DEVICE_TABLE(usb
, btusb_table
);
152 static const struct usb_device_id blacklist_table
[] = {
153 /* CSR BlueCore devices */
154 { USB_DEVICE(0x0a12, 0x0001), .driver_info
= BTUSB_CSR
},
156 /* Broadcom BCM2033 without firmware */
157 { USB_DEVICE(0x0a5c, 0x2033), .driver_info
= BTUSB_IGNORE
},
159 /* Atheros 3011 with sflash firmware */
160 { USB_DEVICE(0x0489, 0xe027), .driver_info
= BTUSB_IGNORE
},
161 { USB_DEVICE(0x0489, 0xe03d), .driver_info
= BTUSB_IGNORE
},
162 { USB_DEVICE(0x0930, 0x0215), .driver_info
= BTUSB_IGNORE
},
163 { USB_DEVICE(0x0cf3, 0x3002), .driver_info
= BTUSB_IGNORE
},
164 { USB_DEVICE(0x0cf3, 0xe019), .driver_info
= BTUSB_IGNORE
},
165 { USB_DEVICE(0x13d3, 0x3304), .driver_info
= BTUSB_IGNORE
},
167 /* Atheros AR9285 Malbec with sflash firmware */
168 { USB_DEVICE(0x03f0, 0x311d), .driver_info
= BTUSB_IGNORE
},
170 /* Atheros 3012 with sflash firmware */
171 { USB_DEVICE(0x0489, 0xe04d), .driver_info
= BTUSB_ATH3012
},
172 { USB_DEVICE(0x0489, 0xe04e), .driver_info
= BTUSB_ATH3012
},
173 { USB_DEVICE(0x0489, 0xe056), .driver_info
= BTUSB_ATH3012
},
174 { USB_DEVICE(0x0489, 0xe057), .driver_info
= BTUSB_ATH3012
},
175 { USB_DEVICE(0x0489, 0xe05f), .driver_info
= BTUSB_ATH3012
},
176 { USB_DEVICE(0x0489, 0xe078), .driver_info
= BTUSB_ATH3012
},
177 { USB_DEVICE(0x04c5, 0x1330), .driver_info
= BTUSB_ATH3012
},
178 { USB_DEVICE(0x04ca, 0x3004), .driver_info
= BTUSB_ATH3012
},
179 { USB_DEVICE(0x04ca, 0x3005), .driver_info
= BTUSB_ATH3012
},
180 { USB_DEVICE(0x04ca, 0x3006), .driver_info
= BTUSB_ATH3012
},
181 { USB_DEVICE(0x04ca, 0x3007), .driver_info
= BTUSB_ATH3012
},
182 { USB_DEVICE(0x04ca, 0x3008), .driver_info
= BTUSB_ATH3012
},
183 { USB_DEVICE(0x04ca, 0x300b), .driver_info
= BTUSB_ATH3012
},
184 { USB_DEVICE(0x04ca, 0x3010), .driver_info
= BTUSB_ATH3012
},
185 { USB_DEVICE(0x0930, 0x0219), .driver_info
= BTUSB_ATH3012
},
186 { USB_DEVICE(0x0930, 0x0220), .driver_info
= BTUSB_ATH3012
},
187 { USB_DEVICE(0x0930, 0x0227), .driver_info
= BTUSB_ATH3012
},
188 { USB_DEVICE(0x0b05, 0x17d0), .driver_info
= BTUSB_ATH3012
},
189 { USB_DEVICE(0x0cf3, 0x0036), .driver_info
= BTUSB_ATH3012
},
190 { USB_DEVICE(0x0cf3, 0x3004), .driver_info
= BTUSB_ATH3012
},
191 { USB_DEVICE(0x0cf3, 0x3008), .driver_info
= BTUSB_ATH3012
},
192 { USB_DEVICE(0x0cf3, 0x311d), .driver_info
= BTUSB_ATH3012
},
193 { USB_DEVICE(0x0cf3, 0x311e), .driver_info
= BTUSB_ATH3012
},
194 { USB_DEVICE(0x0cf3, 0x311f), .driver_info
= BTUSB_ATH3012
},
195 { USB_DEVICE(0x0cf3, 0x3121), .driver_info
= BTUSB_ATH3012
},
196 { USB_DEVICE(0x0cf3, 0x817a), .driver_info
= BTUSB_ATH3012
},
197 { USB_DEVICE(0x0cf3, 0xe003), .driver_info
= BTUSB_ATH3012
},
198 { USB_DEVICE(0x0cf3, 0xe004), .driver_info
= BTUSB_ATH3012
},
199 { USB_DEVICE(0x0cf3, 0xe005), .driver_info
= BTUSB_ATH3012
},
200 { USB_DEVICE(0x13d3, 0x3362), .driver_info
= BTUSB_ATH3012
},
201 { USB_DEVICE(0x13d3, 0x3375), .driver_info
= BTUSB_ATH3012
},
202 { USB_DEVICE(0x13d3, 0x3393), .driver_info
= BTUSB_ATH3012
},
203 { USB_DEVICE(0x13d3, 0x3402), .driver_info
= BTUSB_ATH3012
},
204 { USB_DEVICE(0x13d3, 0x3408), .driver_info
= BTUSB_ATH3012
},
205 { USB_DEVICE(0x13d3, 0x3423), .driver_info
= BTUSB_ATH3012
},
206 { USB_DEVICE(0x13d3, 0x3432), .driver_info
= BTUSB_ATH3012
},
208 /* Atheros AR5BBU12 with sflash firmware */
209 { USB_DEVICE(0x0489, 0xe02c), .driver_info
= BTUSB_IGNORE
},
211 /* Atheros AR5BBU12 with sflash firmware */
212 { USB_DEVICE(0x0489, 0xe036), .driver_info
= BTUSB_ATH3012
},
213 { USB_DEVICE(0x0489, 0xe03c), .driver_info
= BTUSB_ATH3012
},
215 /* Broadcom BCM2035 */
216 { USB_DEVICE(0x0a5c, 0x2009), .driver_info
= BTUSB_BCM92035
},
217 { USB_DEVICE(0x0a5c, 0x200a), .driver_info
= BTUSB_WRONG_SCO_MTU
},
218 { USB_DEVICE(0x0a5c, 0x2035), .driver_info
= BTUSB_WRONG_SCO_MTU
},
220 /* Broadcom BCM2045 */
221 { USB_DEVICE(0x0a5c, 0x2039), .driver_info
= BTUSB_WRONG_SCO_MTU
},
222 { USB_DEVICE(0x0a5c, 0x2101), .driver_info
= BTUSB_WRONG_SCO_MTU
},
224 /* IBM/Lenovo ThinkPad with Broadcom chip */
225 { USB_DEVICE(0x0a5c, 0x201e), .driver_info
= BTUSB_WRONG_SCO_MTU
},
226 { USB_DEVICE(0x0a5c, 0x2110), .driver_info
= BTUSB_WRONG_SCO_MTU
},
228 /* HP laptop with Broadcom chip */
229 { USB_DEVICE(0x03f0, 0x171d), .driver_info
= BTUSB_WRONG_SCO_MTU
},
231 /* Dell laptop with Broadcom chip */
232 { USB_DEVICE(0x413c, 0x8126), .driver_info
= BTUSB_WRONG_SCO_MTU
},
234 /* Dell Wireless 370 and 410 devices */
235 { USB_DEVICE(0x413c, 0x8152), .driver_info
= BTUSB_WRONG_SCO_MTU
},
236 { USB_DEVICE(0x413c, 0x8156), .driver_info
= BTUSB_WRONG_SCO_MTU
},
238 /* Belkin F8T012 and F8T013 devices */
239 { USB_DEVICE(0x050d, 0x0012), .driver_info
= BTUSB_WRONG_SCO_MTU
},
240 { USB_DEVICE(0x050d, 0x0013), .driver_info
= BTUSB_WRONG_SCO_MTU
},
242 /* Asus WL-BTD202 device */
243 { USB_DEVICE(0x0b05, 0x1715), .driver_info
= BTUSB_WRONG_SCO_MTU
},
245 /* Kensington Bluetooth USB adapter */
246 { USB_DEVICE(0x047d, 0x105e), .driver_info
= BTUSB_WRONG_SCO_MTU
},
248 /* RTX Telecom based adapters with buggy SCO support */
249 { USB_DEVICE(0x0400, 0x0807), .driver_info
= BTUSB_BROKEN_ISOC
},
250 { USB_DEVICE(0x0400, 0x080a), .driver_info
= BTUSB_BROKEN_ISOC
},
252 /* CONWISE Technology based adapters with buggy SCO support */
253 { USB_DEVICE(0x0e5e, 0x6622), .driver_info
= BTUSB_BROKEN_ISOC
},
255 /* Roper Class 1 Bluetooth Dongle (Silicon Wave based) */
256 { USB_DEVICE(0x1300, 0x0001), .driver_info
= BTUSB_SWAVE
},
258 /* Digianswer devices */
259 { USB_DEVICE(0x08fd, 0x0001), .driver_info
= BTUSB_DIGIANSWER
},
260 { USB_DEVICE(0x08fd, 0x0002), .driver_info
= BTUSB_IGNORE
},
262 /* CSR BlueCore Bluetooth Sniffer */
263 { USB_DEVICE(0x0a12, 0x0002),
264 .driver_info
= BTUSB_SNIFFER
| BTUSB_BROKEN_ISOC
},
266 /* Frontline ComProbe Bluetooth Sniffer */
267 { USB_DEVICE(0x16d3, 0x0002),
268 .driver_info
= BTUSB_SNIFFER
| BTUSB_BROKEN_ISOC
},
270 /* Marvell Bluetooth devices */
271 { USB_DEVICE(0x1286, 0x2044), .driver_info
= BTUSB_MARVELL
},
272 { USB_DEVICE(0x1286, 0x2046), .driver_info
= BTUSB_MARVELL
},
274 /* Intel Bluetooth devices */
275 { USB_DEVICE(0x8087, 0x07dc), .driver_info
= BTUSB_INTEL
},
276 { USB_DEVICE(0x8087, 0x0a2a), .driver_info
= BTUSB_INTEL
},
277 { USB_DEVICE(0x8087, 0x0a2b), .driver_info
= BTUSB_INTEL_NEW
},
279 /* Other Intel Bluetooth devices */
280 { USB_VENDOR_AND_INTERFACE_INFO(0x8087, 0xe0, 0x01, 0x01),
281 .driver_info
= BTUSB_IGNORE
},
283 { } /* Terminating entry */
286 #define BTUSB_MAX_ISOC_FRAMES 10
288 #define BTUSB_INTR_RUNNING 0
289 #define BTUSB_BULK_RUNNING 1
290 #define BTUSB_ISOC_RUNNING 2
291 #define BTUSB_SUSPENDING 3
292 #define BTUSB_DID_ISO_RESUME 4
293 #define BTUSB_BOOTLOADER 5
294 #define BTUSB_DOWNLOADING 6
295 #define BTUSB_FIRMWARE_LOADED 7
296 #define BTUSB_FIRMWARE_FAILED 8
297 #define BTUSB_BOOTING 9
300 struct hci_dev
*hdev
;
301 struct usb_device
*udev
;
302 struct usb_interface
*intf
;
303 struct usb_interface
*isoc
;
307 struct work_struct work
;
308 struct work_struct waker
;
310 struct usb_anchor deferred
;
311 struct usb_anchor tx_anchor
;
315 struct usb_anchor intr_anchor
;
316 struct usb_anchor bulk_anchor
;
317 struct usb_anchor isoc_anchor
;
320 struct sk_buff
*evt_skb
;
321 struct sk_buff
*acl_skb
;
322 struct sk_buff
*sco_skb
;
324 struct usb_endpoint_descriptor
*intr_ep
;
325 struct usb_endpoint_descriptor
*bulk_tx_ep
;
326 struct usb_endpoint_descriptor
*bulk_rx_ep
;
327 struct usb_endpoint_descriptor
*isoc_tx_ep
;
328 struct usb_endpoint_descriptor
*isoc_rx_ep
;
333 unsigned int sco_num
;
337 int (*recv_event
)(struct hci_dev
*hdev
, struct sk_buff
*skb
);
338 int (*recv_bulk
)(struct btusb_data
*data
, void *buffer
, int count
);
341 static int btusb_wait_on_bit_timeout(void *word
, int bit
, unsigned long timeout
,
345 if (!test_bit(bit
, word
))
347 return out_of_line_wait_on_bit_timeout(word
, bit
, bit_wait_timeout
,
351 static inline void btusb_free_frags(struct btusb_data
*data
)
355 spin_lock_irqsave(&data
->rxlock
, flags
);
357 kfree_skb(data
->evt_skb
);
358 data
->evt_skb
= NULL
;
360 kfree_skb(data
->acl_skb
);
361 data
->acl_skb
= NULL
;
363 kfree_skb(data
->sco_skb
);
364 data
->sco_skb
= NULL
;
366 spin_unlock_irqrestore(&data
->rxlock
, flags
);
369 static int btusb_recv_intr(struct btusb_data
*data
, void *buffer
, int count
)
374 spin_lock(&data
->rxlock
);
381 skb
= bt_skb_alloc(HCI_MAX_EVENT_SIZE
, GFP_ATOMIC
);
387 bt_cb(skb
)->pkt_type
= HCI_EVENT_PKT
;
388 bt_cb(skb
)->expect
= HCI_EVENT_HDR_SIZE
;
391 len
= min_t(uint
, bt_cb(skb
)->expect
, count
);
392 memcpy(skb_put(skb
, len
), buffer
, len
);
396 bt_cb(skb
)->expect
-= len
;
398 if (skb
->len
== HCI_EVENT_HDR_SIZE
) {
399 /* Complete event header */
400 bt_cb(skb
)->expect
= hci_event_hdr(skb
)->plen
;
402 if (skb_tailroom(skb
) < bt_cb(skb
)->expect
) {
411 if (bt_cb(skb
)->expect
== 0) {
413 data
->recv_event(data
->hdev
, skb
);
419 spin_unlock(&data
->rxlock
);
424 static int btusb_recv_bulk(struct btusb_data
*data
, void *buffer
, int count
)
429 spin_lock(&data
->rxlock
);
436 skb
= bt_skb_alloc(HCI_MAX_FRAME_SIZE
, GFP_ATOMIC
);
442 bt_cb(skb
)->pkt_type
= HCI_ACLDATA_PKT
;
443 bt_cb(skb
)->expect
= HCI_ACL_HDR_SIZE
;
446 len
= min_t(uint
, bt_cb(skb
)->expect
, count
);
447 memcpy(skb_put(skb
, len
), buffer
, len
);
451 bt_cb(skb
)->expect
-= len
;
453 if (skb
->len
== HCI_ACL_HDR_SIZE
) {
454 __le16 dlen
= hci_acl_hdr(skb
)->dlen
;
456 /* Complete ACL header */
457 bt_cb(skb
)->expect
= __le16_to_cpu(dlen
);
459 if (skb_tailroom(skb
) < bt_cb(skb
)->expect
) {
468 if (bt_cb(skb
)->expect
== 0) {
470 hci_recv_frame(data
->hdev
, skb
);
476 spin_unlock(&data
->rxlock
);
481 static int btusb_recv_isoc(struct btusb_data
*data
, void *buffer
, int count
)
486 spin_lock(&data
->rxlock
);
493 skb
= bt_skb_alloc(HCI_MAX_SCO_SIZE
, GFP_ATOMIC
);
499 bt_cb(skb
)->pkt_type
= HCI_SCODATA_PKT
;
500 bt_cb(skb
)->expect
= HCI_SCO_HDR_SIZE
;
503 len
= min_t(uint
, bt_cb(skb
)->expect
, count
);
504 memcpy(skb_put(skb
, len
), buffer
, len
);
508 bt_cb(skb
)->expect
-= len
;
510 if (skb
->len
== HCI_SCO_HDR_SIZE
) {
511 /* Complete SCO header */
512 bt_cb(skb
)->expect
= hci_sco_hdr(skb
)->dlen
;
514 if (skb_tailroom(skb
) < bt_cb(skb
)->expect
) {
523 if (bt_cb(skb
)->expect
== 0) {
525 hci_recv_frame(data
->hdev
, skb
);
531 spin_unlock(&data
->rxlock
);
536 static void btusb_intr_complete(struct urb
*urb
)
538 struct hci_dev
*hdev
= urb
->context
;
539 struct btusb_data
*data
= hci_get_drvdata(hdev
);
542 BT_DBG("%s urb %p status %d count %d", hdev
->name
, urb
, urb
->status
,
545 if (!test_bit(HCI_RUNNING
, &hdev
->flags
))
548 if (urb
->status
== 0) {
549 hdev
->stat
.byte_rx
+= urb
->actual_length
;
551 if (btusb_recv_intr(data
, urb
->transfer_buffer
,
552 urb
->actual_length
) < 0) {
553 BT_ERR("%s corrupted event packet", hdev
->name
);
556 } else if (urb
->status
== -ENOENT
) {
557 /* Avoid suspend failed when usb_kill_urb */
561 if (!test_bit(BTUSB_INTR_RUNNING
, &data
->flags
))
564 usb_mark_last_busy(data
->udev
);
565 usb_anchor_urb(urb
, &data
->intr_anchor
);
567 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
569 /* -EPERM: urb is being killed;
570 * -ENODEV: device got disconnected */
571 if (err
!= -EPERM
&& err
!= -ENODEV
)
572 BT_ERR("%s urb %p failed to resubmit (%d)",
573 hdev
->name
, urb
, -err
);
574 usb_unanchor_urb(urb
);
578 static int btusb_submit_intr_urb(struct hci_dev
*hdev
, gfp_t mem_flags
)
580 struct btusb_data
*data
= hci_get_drvdata(hdev
);
586 BT_DBG("%s", hdev
->name
);
591 urb
= usb_alloc_urb(0, mem_flags
);
595 size
= le16_to_cpu(data
->intr_ep
->wMaxPacketSize
);
597 buf
= kmalloc(size
, mem_flags
);
603 pipe
= usb_rcvintpipe(data
->udev
, data
->intr_ep
->bEndpointAddress
);
605 usb_fill_int_urb(urb
, data
->udev
, pipe
, buf
, size
,
606 btusb_intr_complete
, hdev
, data
->intr_ep
->bInterval
);
608 urb
->transfer_flags
|= URB_FREE_BUFFER
;
610 usb_anchor_urb(urb
, &data
->intr_anchor
);
612 err
= usb_submit_urb(urb
, mem_flags
);
614 if (err
!= -EPERM
&& err
!= -ENODEV
)
615 BT_ERR("%s urb %p submission failed (%d)",
616 hdev
->name
, urb
, -err
);
617 usb_unanchor_urb(urb
);
625 static void btusb_bulk_complete(struct urb
*urb
)
627 struct hci_dev
*hdev
= urb
->context
;
628 struct btusb_data
*data
= hci_get_drvdata(hdev
);
631 BT_DBG("%s urb %p status %d count %d", hdev
->name
, urb
, urb
->status
,
634 if (!test_bit(HCI_RUNNING
, &hdev
->flags
))
637 if (urb
->status
== 0) {
638 hdev
->stat
.byte_rx
+= urb
->actual_length
;
640 if (data
->recv_bulk(data
, urb
->transfer_buffer
,
641 urb
->actual_length
) < 0) {
642 BT_ERR("%s corrupted ACL packet", hdev
->name
);
645 } else if (urb
->status
== -ENOENT
) {
646 /* Avoid suspend failed when usb_kill_urb */
650 if (!test_bit(BTUSB_BULK_RUNNING
, &data
->flags
))
653 usb_anchor_urb(urb
, &data
->bulk_anchor
);
654 usb_mark_last_busy(data
->udev
);
656 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
658 /* -EPERM: urb is being killed;
659 * -ENODEV: device got disconnected */
660 if (err
!= -EPERM
&& err
!= -ENODEV
)
661 BT_ERR("%s urb %p failed to resubmit (%d)",
662 hdev
->name
, urb
, -err
);
663 usb_unanchor_urb(urb
);
667 static int btusb_submit_bulk_urb(struct hci_dev
*hdev
, gfp_t mem_flags
)
669 struct btusb_data
*data
= hci_get_drvdata(hdev
);
673 int err
, size
= HCI_MAX_FRAME_SIZE
;
675 BT_DBG("%s", hdev
->name
);
677 if (!data
->bulk_rx_ep
)
680 urb
= usb_alloc_urb(0, mem_flags
);
684 buf
= kmalloc(size
, mem_flags
);
690 pipe
= usb_rcvbulkpipe(data
->udev
, data
->bulk_rx_ep
->bEndpointAddress
);
692 usb_fill_bulk_urb(urb
, data
->udev
, pipe
, buf
, size
,
693 btusb_bulk_complete
, hdev
);
695 urb
->transfer_flags
|= URB_FREE_BUFFER
;
697 usb_mark_last_busy(data
->udev
);
698 usb_anchor_urb(urb
, &data
->bulk_anchor
);
700 err
= usb_submit_urb(urb
, mem_flags
);
702 if (err
!= -EPERM
&& err
!= -ENODEV
)
703 BT_ERR("%s urb %p submission failed (%d)",
704 hdev
->name
, urb
, -err
);
705 usb_unanchor_urb(urb
);
713 static void btusb_isoc_complete(struct urb
*urb
)
715 struct hci_dev
*hdev
= urb
->context
;
716 struct btusb_data
*data
= hci_get_drvdata(hdev
);
719 BT_DBG("%s urb %p status %d count %d", hdev
->name
, urb
, urb
->status
,
722 if (!test_bit(HCI_RUNNING
, &hdev
->flags
))
725 if (urb
->status
== 0) {
726 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
727 unsigned int offset
= urb
->iso_frame_desc
[i
].offset
;
728 unsigned int length
= urb
->iso_frame_desc
[i
].actual_length
;
730 if (urb
->iso_frame_desc
[i
].status
)
733 hdev
->stat
.byte_rx
+= length
;
735 if (btusb_recv_isoc(data
, urb
->transfer_buffer
+ offset
,
737 BT_ERR("%s corrupted SCO packet", hdev
->name
);
741 } else if (urb
->status
== -ENOENT
) {
742 /* Avoid suspend failed when usb_kill_urb */
746 if (!test_bit(BTUSB_ISOC_RUNNING
, &data
->flags
))
749 usb_anchor_urb(urb
, &data
->isoc_anchor
);
751 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
753 /* -EPERM: urb is being killed;
754 * -ENODEV: device got disconnected */
755 if (err
!= -EPERM
&& err
!= -ENODEV
)
756 BT_ERR("%s urb %p failed to resubmit (%d)",
757 hdev
->name
, urb
, -err
);
758 usb_unanchor_urb(urb
);
762 static inline void __fill_isoc_descriptor(struct urb
*urb
, int len
, int mtu
)
766 BT_DBG("len %d mtu %d", len
, mtu
);
768 for (i
= 0; i
< BTUSB_MAX_ISOC_FRAMES
&& len
>= mtu
;
769 i
++, offset
+= mtu
, len
-= mtu
) {
770 urb
->iso_frame_desc
[i
].offset
= offset
;
771 urb
->iso_frame_desc
[i
].length
= mtu
;
774 if (len
&& i
< BTUSB_MAX_ISOC_FRAMES
) {
775 urb
->iso_frame_desc
[i
].offset
= offset
;
776 urb
->iso_frame_desc
[i
].length
= len
;
780 urb
->number_of_packets
= i
;
783 static int btusb_submit_isoc_urb(struct hci_dev
*hdev
, gfp_t mem_flags
)
785 struct btusb_data
*data
= hci_get_drvdata(hdev
);
791 BT_DBG("%s", hdev
->name
);
793 if (!data
->isoc_rx_ep
)
796 urb
= usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES
, mem_flags
);
800 size
= le16_to_cpu(data
->isoc_rx_ep
->wMaxPacketSize
) *
801 BTUSB_MAX_ISOC_FRAMES
;
803 buf
= kmalloc(size
, mem_flags
);
809 pipe
= usb_rcvisocpipe(data
->udev
, data
->isoc_rx_ep
->bEndpointAddress
);
811 usb_fill_int_urb(urb
, data
->udev
, pipe
, buf
, size
, btusb_isoc_complete
,
812 hdev
, data
->isoc_rx_ep
->bInterval
);
814 urb
->transfer_flags
= URB_FREE_BUFFER
| URB_ISO_ASAP
;
816 __fill_isoc_descriptor(urb
, size
,
817 le16_to_cpu(data
->isoc_rx_ep
->wMaxPacketSize
));
819 usb_anchor_urb(urb
, &data
->isoc_anchor
);
821 err
= usb_submit_urb(urb
, mem_flags
);
823 if (err
!= -EPERM
&& err
!= -ENODEV
)
824 BT_ERR("%s urb %p submission failed (%d)",
825 hdev
->name
, urb
, -err
);
826 usb_unanchor_urb(urb
);
834 static void btusb_tx_complete(struct urb
*urb
)
836 struct sk_buff
*skb
= urb
->context
;
837 struct hci_dev
*hdev
= (struct hci_dev
*)skb
->dev
;
838 struct btusb_data
*data
= hci_get_drvdata(hdev
);
840 BT_DBG("%s urb %p status %d count %d", hdev
->name
, urb
, urb
->status
,
843 if (!test_bit(HCI_RUNNING
, &hdev
->flags
))
847 hdev
->stat
.byte_tx
+= urb
->transfer_buffer_length
;
852 spin_lock(&data
->txlock
);
853 data
->tx_in_flight
--;
854 spin_unlock(&data
->txlock
);
856 kfree(urb
->setup_packet
);
861 static void btusb_isoc_tx_complete(struct urb
*urb
)
863 struct sk_buff
*skb
= urb
->context
;
864 struct hci_dev
*hdev
= (struct hci_dev
*)skb
->dev
;
866 BT_DBG("%s urb %p status %d count %d", hdev
->name
, urb
, urb
->status
,
869 if (!test_bit(HCI_RUNNING
, &hdev
->flags
))
873 hdev
->stat
.byte_tx
+= urb
->transfer_buffer_length
;
878 kfree(urb
->setup_packet
);
883 static int btusb_open(struct hci_dev
*hdev
)
885 struct btusb_data
*data
= hci_get_drvdata(hdev
);
888 BT_DBG("%s", hdev
->name
);
890 err
= usb_autopm_get_interface(data
->intf
);
894 data
->intf
->needs_remote_wakeup
= 1;
896 if (test_and_set_bit(HCI_RUNNING
, &hdev
->flags
))
899 if (test_and_set_bit(BTUSB_INTR_RUNNING
, &data
->flags
))
902 err
= btusb_submit_intr_urb(hdev
, GFP_KERNEL
);
906 err
= btusb_submit_bulk_urb(hdev
, GFP_KERNEL
);
908 usb_kill_anchored_urbs(&data
->intr_anchor
);
912 set_bit(BTUSB_BULK_RUNNING
, &data
->flags
);
913 btusb_submit_bulk_urb(hdev
, GFP_KERNEL
);
916 usb_autopm_put_interface(data
->intf
);
920 clear_bit(BTUSB_INTR_RUNNING
, &data
->flags
);
921 clear_bit(HCI_RUNNING
, &hdev
->flags
);
922 usb_autopm_put_interface(data
->intf
);
926 static void btusb_stop_traffic(struct btusb_data
*data
)
928 usb_kill_anchored_urbs(&data
->intr_anchor
);
929 usb_kill_anchored_urbs(&data
->bulk_anchor
);
930 usb_kill_anchored_urbs(&data
->isoc_anchor
);
933 static int btusb_close(struct hci_dev
*hdev
)
935 struct btusb_data
*data
= hci_get_drvdata(hdev
);
938 BT_DBG("%s", hdev
->name
);
940 if (!test_and_clear_bit(HCI_RUNNING
, &hdev
->flags
))
943 cancel_work_sync(&data
->work
);
944 cancel_work_sync(&data
->waker
);
946 clear_bit(BTUSB_ISOC_RUNNING
, &data
->flags
);
947 clear_bit(BTUSB_BULK_RUNNING
, &data
->flags
);
948 clear_bit(BTUSB_INTR_RUNNING
, &data
->flags
);
950 btusb_stop_traffic(data
);
951 btusb_free_frags(data
);
953 err
= usb_autopm_get_interface(data
->intf
);
957 data
->intf
->needs_remote_wakeup
= 0;
958 usb_autopm_put_interface(data
->intf
);
961 usb_scuttle_anchored_urbs(&data
->deferred
);
965 static int btusb_flush(struct hci_dev
*hdev
)
967 struct btusb_data
*data
= hci_get_drvdata(hdev
);
969 BT_DBG("%s", hdev
->name
);
971 usb_kill_anchored_urbs(&data
->tx_anchor
);
972 btusb_free_frags(data
);
977 static struct urb
*alloc_ctrl_urb(struct hci_dev
*hdev
, struct sk_buff
*skb
)
979 struct btusb_data
*data
= hci_get_drvdata(hdev
);
980 struct usb_ctrlrequest
*dr
;
984 urb
= usb_alloc_urb(0, GFP_KERNEL
);
986 return ERR_PTR(-ENOMEM
);
988 dr
= kmalloc(sizeof(*dr
), GFP_KERNEL
);
991 return ERR_PTR(-ENOMEM
);
994 dr
->bRequestType
= data
->cmdreq_type
;
995 dr
->bRequest
= data
->cmdreq
;
998 dr
->wLength
= __cpu_to_le16(skb
->len
);
1000 pipe
= usb_sndctrlpipe(data
->udev
, 0x00);
1002 usb_fill_control_urb(urb
, data
->udev
, pipe
, (void *)dr
,
1003 skb
->data
, skb
->len
, btusb_tx_complete
, skb
);
1005 skb
->dev
= (void *)hdev
;
1010 static struct urb
*alloc_bulk_urb(struct hci_dev
*hdev
, struct sk_buff
*skb
)
1012 struct btusb_data
*data
= hci_get_drvdata(hdev
);
1016 if (!data
->bulk_tx_ep
)
1017 return ERR_PTR(-ENODEV
);
1019 urb
= usb_alloc_urb(0, GFP_KERNEL
);
1021 return ERR_PTR(-ENOMEM
);
1023 pipe
= usb_sndbulkpipe(data
->udev
, data
->bulk_tx_ep
->bEndpointAddress
);
1025 usb_fill_bulk_urb(urb
, data
->udev
, pipe
,
1026 skb
->data
, skb
->len
, btusb_tx_complete
, skb
);
1028 skb
->dev
= (void *)hdev
;
1033 static struct urb
*alloc_isoc_urb(struct hci_dev
*hdev
, struct sk_buff
*skb
)
1035 struct btusb_data
*data
= hci_get_drvdata(hdev
);
1039 if (!data
->isoc_tx_ep
)
1040 return ERR_PTR(-ENODEV
);
1042 urb
= usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES
, GFP_KERNEL
);
1044 return ERR_PTR(-ENOMEM
);
1046 pipe
= usb_sndisocpipe(data
->udev
, data
->isoc_tx_ep
->bEndpointAddress
);
1048 usb_fill_int_urb(urb
, data
->udev
, pipe
,
1049 skb
->data
, skb
->len
, btusb_isoc_tx_complete
,
1050 skb
, data
->isoc_tx_ep
->bInterval
);
1052 urb
->transfer_flags
= URB_ISO_ASAP
;
1054 __fill_isoc_descriptor(urb
, skb
->len
,
1055 le16_to_cpu(data
->isoc_tx_ep
->wMaxPacketSize
));
1057 skb
->dev
= (void *)hdev
;
1062 static int submit_tx_urb(struct hci_dev
*hdev
, struct urb
*urb
)
1064 struct btusb_data
*data
= hci_get_drvdata(hdev
);
1067 usb_anchor_urb(urb
, &data
->tx_anchor
);
1069 err
= usb_submit_urb(urb
, GFP_KERNEL
);
1071 if (err
!= -EPERM
&& err
!= -ENODEV
)
1072 BT_ERR("%s urb %p submission failed (%d)",
1073 hdev
->name
, urb
, -err
);
1074 kfree(urb
->setup_packet
);
1075 usb_unanchor_urb(urb
);
1077 usb_mark_last_busy(data
->udev
);
1084 static int submit_or_queue_tx_urb(struct hci_dev
*hdev
, struct urb
*urb
)
1086 struct btusb_data
*data
= hci_get_drvdata(hdev
);
1087 unsigned long flags
;
1090 spin_lock_irqsave(&data
->txlock
, flags
);
1091 suspending
= test_bit(BTUSB_SUSPENDING
, &data
->flags
);
1093 data
->tx_in_flight
++;
1094 spin_unlock_irqrestore(&data
->txlock
, flags
);
1097 return submit_tx_urb(hdev
, urb
);
1099 usb_anchor_urb(urb
, &data
->deferred
);
1100 schedule_work(&data
->waker
);
1106 static int btusb_send_frame(struct hci_dev
*hdev
, struct sk_buff
*skb
)
1110 BT_DBG("%s", hdev
->name
);
1112 if (!test_bit(HCI_RUNNING
, &hdev
->flags
))
1115 switch (bt_cb(skb
)->pkt_type
) {
1116 case HCI_COMMAND_PKT
:
1117 urb
= alloc_ctrl_urb(hdev
, skb
);
1119 return PTR_ERR(urb
);
1121 hdev
->stat
.cmd_tx
++;
1122 return submit_or_queue_tx_urb(hdev
, urb
);
1124 case HCI_ACLDATA_PKT
:
1125 urb
= alloc_bulk_urb(hdev
, skb
);
1127 return PTR_ERR(urb
);
1129 hdev
->stat
.acl_tx
++;
1130 return submit_or_queue_tx_urb(hdev
, urb
);
1132 case HCI_SCODATA_PKT
:
1133 if (hci_conn_num(hdev
, SCO_LINK
) < 1)
1136 urb
= alloc_isoc_urb(hdev
, skb
);
1138 return PTR_ERR(urb
);
1140 hdev
->stat
.sco_tx
++;
1141 return submit_tx_urb(hdev
, urb
);
1147 static void btusb_notify(struct hci_dev
*hdev
, unsigned int evt
)
1149 struct btusb_data
*data
= hci_get_drvdata(hdev
);
1151 BT_DBG("%s evt %d", hdev
->name
, evt
);
1153 if (hci_conn_num(hdev
, SCO_LINK
) != data
->sco_num
) {
1154 data
->sco_num
= hci_conn_num(hdev
, SCO_LINK
);
1155 schedule_work(&data
->work
);
1159 static inline int __set_isoc_interface(struct hci_dev
*hdev
, int altsetting
)
1161 struct btusb_data
*data
= hci_get_drvdata(hdev
);
1162 struct usb_interface
*intf
= data
->isoc
;
1163 struct usb_endpoint_descriptor
*ep_desc
;
1169 err
= usb_set_interface(data
->udev
, 1, altsetting
);
1171 BT_ERR("%s setting interface failed (%d)", hdev
->name
, -err
);
1175 data
->isoc_altsetting
= altsetting
;
1177 data
->isoc_tx_ep
= NULL
;
1178 data
->isoc_rx_ep
= NULL
;
1180 for (i
= 0; i
< intf
->cur_altsetting
->desc
.bNumEndpoints
; i
++) {
1181 ep_desc
= &intf
->cur_altsetting
->endpoint
[i
].desc
;
1183 if (!data
->isoc_tx_ep
&& usb_endpoint_is_isoc_out(ep_desc
)) {
1184 data
->isoc_tx_ep
= ep_desc
;
1188 if (!data
->isoc_rx_ep
&& usb_endpoint_is_isoc_in(ep_desc
)) {
1189 data
->isoc_rx_ep
= ep_desc
;
1194 if (!data
->isoc_tx_ep
|| !data
->isoc_rx_ep
) {
1195 BT_ERR("%s invalid SCO descriptors", hdev
->name
);
1202 static void btusb_work(struct work_struct
*work
)
1204 struct btusb_data
*data
= container_of(work
, struct btusb_data
, work
);
1205 struct hci_dev
*hdev
= data
->hdev
;
1209 if (data
->sco_num
> 0) {
1210 if (!test_bit(BTUSB_DID_ISO_RESUME
, &data
->flags
)) {
1211 err
= usb_autopm_get_interface(data
->isoc
? data
->isoc
: data
->intf
);
1213 clear_bit(BTUSB_ISOC_RUNNING
, &data
->flags
);
1214 usb_kill_anchored_urbs(&data
->isoc_anchor
);
1218 set_bit(BTUSB_DID_ISO_RESUME
, &data
->flags
);
1221 if (hdev
->voice_setting
& 0x0020) {
1222 static const int alts
[3] = { 2, 4, 5 };
1224 new_alts
= alts
[data
->sco_num
- 1];
1226 new_alts
= data
->sco_num
;
1229 if (data
->isoc_altsetting
!= new_alts
) {
1230 clear_bit(BTUSB_ISOC_RUNNING
, &data
->flags
);
1231 usb_kill_anchored_urbs(&data
->isoc_anchor
);
1233 if (__set_isoc_interface(hdev
, new_alts
) < 0)
1237 if (!test_and_set_bit(BTUSB_ISOC_RUNNING
, &data
->flags
)) {
1238 if (btusb_submit_isoc_urb(hdev
, GFP_KERNEL
) < 0)
1239 clear_bit(BTUSB_ISOC_RUNNING
, &data
->flags
);
1241 btusb_submit_isoc_urb(hdev
, GFP_KERNEL
);
1244 clear_bit(BTUSB_ISOC_RUNNING
, &data
->flags
);
1245 usb_kill_anchored_urbs(&data
->isoc_anchor
);
1247 __set_isoc_interface(hdev
, 0);
1248 if (test_and_clear_bit(BTUSB_DID_ISO_RESUME
, &data
->flags
))
1249 usb_autopm_put_interface(data
->isoc
? data
->isoc
: data
->intf
);
1253 static void btusb_waker(struct work_struct
*work
)
1255 struct btusb_data
*data
= container_of(work
, struct btusb_data
, waker
);
1258 err
= usb_autopm_get_interface(data
->intf
);
1262 usb_autopm_put_interface(data
->intf
);
1265 static int btusb_setup_bcm92035(struct hci_dev
*hdev
)
1267 struct sk_buff
*skb
;
1270 BT_DBG("%s", hdev
->name
);
1272 skb
= __hci_cmd_sync(hdev
, 0xfc3b, 1, &val
, HCI_INIT_TIMEOUT
);
1274 BT_ERR("BCM92035 command failed (%ld)", -PTR_ERR(skb
));
1281 static int btusb_setup_csr(struct hci_dev
*hdev
)
1283 struct hci_rp_read_local_version
*rp
;
1284 struct sk_buff
*skb
;
1287 BT_DBG("%s", hdev
->name
);
1289 skb
= __hci_cmd_sync(hdev
, HCI_OP_READ_LOCAL_VERSION
, 0, NULL
,
1292 BT_ERR("Reading local version failed (%ld)", -PTR_ERR(skb
));
1293 return -PTR_ERR(skb
);
1296 rp
= (struct hci_rp_read_local_version
*)skb
->data
;
1299 if (le16_to_cpu(rp
->manufacturer
) != 10) {
1300 /* Clear the reset quirk since this is not an actual
1301 * early Bluetooth 1.1 device from CSR.
1303 clear_bit(HCI_QUIRK_RESET_ON_CLOSE
, &hdev
->quirks
);
1305 /* These fake CSR controllers have all a broken
1306 * stored link key handling and so just disable it.
1308 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY
,
1313 ret
= -bt_to_errno(rp
->status
);
1320 struct intel_version
{
1333 struct intel_boot_params
{
1345 bdaddr_t otp_bdaddr
;
1346 __u8 min_fw_build_nn
;
1347 __u8 min_fw_build_cw
;
1348 __u8 min_fw_build_yy
;
1350 __u8 unlocked_state
;
1353 static const struct firmware
*btusb_setup_intel_get_fw(struct hci_dev
*hdev
,
1354 struct intel_version
*ver
)
1356 const struct firmware
*fw
;
1360 snprintf(fwname
, sizeof(fwname
),
1361 "intel/ibt-hw-%x.%x.%x-fw-%x.%x.%x.%x.%x.bseq",
1362 ver
->hw_platform
, ver
->hw_variant
, ver
->hw_revision
,
1363 ver
->fw_variant
, ver
->fw_revision
, ver
->fw_build_num
,
1364 ver
->fw_build_ww
, ver
->fw_build_yy
);
1366 ret
= request_firmware(&fw
, fwname
, &hdev
->dev
);
1368 if (ret
== -EINVAL
) {
1369 BT_ERR("%s Intel firmware file request failed (%d)",
1374 BT_ERR("%s failed to open Intel firmware file: %s(%d)",
1375 hdev
->name
, fwname
, ret
);
1377 /* If the correct firmware patch file is not found, use the
1378 * default firmware patch file instead
1380 snprintf(fwname
, sizeof(fwname
), "intel/ibt-hw-%x.%x.bseq",
1381 ver
->hw_platform
, ver
->hw_variant
);
1382 if (request_firmware(&fw
, fwname
, &hdev
->dev
) < 0) {
1383 BT_ERR("%s failed to open default Intel fw file: %s",
1384 hdev
->name
, fwname
);
1389 BT_INFO("%s: Intel Bluetooth firmware file: %s", hdev
->name
, fwname
);
1394 static int btusb_setup_intel_patching(struct hci_dev
*hdev
,
1395 const struct firmware
*fw
,
1396 const u8
**fw_ptr
, int *disable_patch
)
1398 struct sk_buff
*skb
;
1399 struct hci_command_hdr
*cmd
;
1400 const u8
*cmd_param
;
1401 struct hci_event_hdr
*evt
= NULL
;
1402 const u8
*evt_param
= NULL
;
1403 int remain
= fw
->size
- (*fw_ptr
- fw
->data
);
1405 /* The first byte indicates the types of the patch command or event.
1406 * 0x01 means HCI command and 0x02 is HCI event. If the first bytes
1407 * in the current firmware buffer doesn't start with 0x01 or
1408 * the size of remain buffer is smaller than HCI command header,
1409 * the firmware file is corrupted and it should stop the patching
1412 if (remain
> HCI_COMMAND_HDR_SIZE
&& *fw_ptr
[0] != 0x01) {
1413 BT_ERR("%s Intel fw corrupted: invalid cmd read", hdev
->name
);
1419 cmd
= (struct hci_command_hdr
*)(*fw_ptr
);
1420 *fw_ptr
+= sizeof(*cmd
);
1421 remain
-= sizeof(*cmd
);
1423 /* Ensure that the remain firmware data is long enough than the length
1424 * of command parameter. If not, the firmware file is corrupted.
1426 if (remain
< cmd
->plen
) {
1427 BT_ERR("%s Intel fw corrupted: invalid cmd len", hdev
->name
);
1431 /* If there is a command that loads a patch in the firmware
1432 * file, then enable the patch upon success, otherwise just
1433 * disable the manufacturer mode, for example patch activation
1434 * is not required when the default firmware patch file is used
1435 * because there are no patch data to load.
1437 if (*disable_patch
&& le16_to_cpu(cmd
->opcode
) == 0xfc8e)
1440 cmd_param
= *fw_ptr
;
1441 *fw_ptr
+= cmd
->plen
;
1442 remain
-= cmd
->plen
;
1444 /* This reads the expected events when the above command is sent to the
1445 * device. Some vendor commands expects more than one events, for
1446 * example command status event followed by vendor specific event.
1447 * For this case, it only keeps the last expected event. so the command
1448 * can be sent with __hci_cmd_sync_ev() which returns the sk_buff of
1449 * last expected event.
1451 while (remain
> HCI_EVENT_HDR_SIZE
&& *fw_ptr
[0] == 0x02) {
1455 evt
= (struct hci_event_hdr
*)(*fw_ptr
);
1456 *fw_ptr
+= sizeof(*evt
);
1457 remain
-= sizeof(*evt
);
1459 if (remain
< evt
->plen
) {
1460 BT_ERR("%s Intel fw corrupted: invalid evt len",
1465 evt_param
= *fw_ptr
;
1466 *fw_ptr
+= evt
->plen
;
1467 remain
-= evt
->plen
;
1470 /* Every HCI commands in the firmware file has its correspond event.
1471 * If event is not found or remain is smaller than zero, the firmware
1472 * file is corrupted.
1474 if (!evt
|| !evt_param
|| remain
< 0) {
1475 BT_ERR("%s Intel fw corrupted: invalid evt read", hdev
->name
);
1479 skb
= __hci_cmd_sync_ev(hdev
, le16_to_cpu(cmd
->opcode
), cmd
->plen
,
1480 cmd_param
, evt
->evt
, HCI_INIT_TIMEOUT
);
1482 BT_ERR("%s sending Intel patch command (0x%4.4x) failed (%ld)",
1483 hdev
->name
, cmd
->opcode
, PTR_ERR(skb
));
1484 return PTR_ERR(skb
);
1487 /* It ensures that the returned event matches the event data read from
1488 * the firmware file. At fist, it checks the length and then
1489 * the contents of the event.
1491 if (skb
->len
!= evt
->plen
) {
1492 BT_ERR("%s mismatch event length (opcode 0x%4.4x)", hdev
->name
,
1493 le16_to_cpu(cmd
->opcode
));
1498 if (memcmp(skb
->data
, evt_param
, evt
->plen
)) {
1499 BT_ERR("%s mismatch event parameter (opcode 0x%4.4x)",
1500 hdev
->name
, le16_to_cpu(cmd
->opcode
));
1509 #define BDADDR_INTEL (&(bdaddr_t) {{0x00, 0x8b, 0x9e, 0x19, 0x03, 0x00}})
1511 static int btusb_check_bdaddr_intel(struct hci_dev
*hdev
)
1513 struct sk_buff
*skb
;
1514 struct hci_rp_read_bd_addr
*rp
;
1516 skb
= __hci_cmd_sync(hdev
, HCI_OP_READ_BD_ADDR
, 0, NULL
,
1519 BT_ERR("%s reading Intel device address failed (%ld)",
1520 hdev
->name
, PTR_ERR(skb
));
1521 return PTR_ERR(skb
);
1524 if (skb
->len
!= sizeof(*rp
)) {
1525 BT_ERR("%s Intel device address length mismatch", hdev
->name
);
1530 rp
= (struct hci_rp_read_bd_addr
*)skb
->data
;
1532 BT_ERR("%s Intel device address result failed (%02x)",
1533 hdev
->name
, rp
->status
);
1535 return -bt_to_errno(rp
->status
);
1538 /* For some Intel based controllers, the default Bluetooth device
1539 * address 00:03:19:9E:8B:00 can be found. These controllers are
1540 * fully operational, but have the danger of duplicate addresses
1541 * and that in turn can cause problems with Bluetooth operation.
1543 if (!bacmp(&rp
->bdaddr
, BDADDR_INTEL
)) {
1544 BT_ERR("%s found Intel default device address (%pMR)",
1545 hdev
->name
, &rp
->bdaddr
);
1546 set_bit(HCI_QUIRK_INVALID_BDADDR
, &hdev
->quirks
);
1554 static int btusb_setup_intel(struct hci_dev
*hdev
)
1556 struct sk_buff
*skb
;
1557 const struct firmware
*fw
;
1560 struct intel_version
*ver
;
1562 const u8 mfg_enable
[] = { 0x01, 0x00 };
1563 const u8 mfg_disable
[] = { 0x00, 0x00 };
1564 const u8 mfg_reset_deactivate
[] = { 0x00, 0x01 };
1565 const u8 mfg_reset_activate
[] = { 0x00, 0x02 };
1567 BT_DBG("%s", hdev
->name
);
1569 /* The controller has a bug with the first HCI command sent to it
1570 * returning number of completed commands as zero. This would stall the
1571 * command processing in the Bluetooth core.
1573 * As a workaround, send HCI Reset command first which will reset the
1574 * number of completed commands and allow normal command processing
1577 skb
= __hci_cmd_sync(hdev
, HCI_OP_RESET
, 0, NULL
, HCI_INIT_TIMEOUT
);
1579 BT_ERR("%s sending initial HCI reset command failed (%ld)",
1580 hdev
->name
, PTR_ERR(skb
));
1581 return PTR_ERR(skb
);
1585 /* Read Intel specific controller version first to allow selection of
1586 * which firmware file to load.
1588 * The returned information are hardware variant and revision plus
1589 * firmware variant, revision and build number.
1591 skb
= __hci_cmd_sync(hdev
, 0xfc05, 0, NULL
, HCI_INIT_TIMEOUT
);
1593 BT_ERR("%s reading Intel fw version command failed (%ld)",
1594 hdev
->name
, PTR_ERR(skb
));
1595 return PTR_ERR(skb
);
1598 if (skb
->len
!= sizeof(*ver
)) {
1599 BT_ERR("%s Intel version event length mismatch", hdev
->name
);
1604 ver
= (struct intel_version
*)skb
->data
;
1606 BT_ERR("%s Intel fw version event failed (%02x)", hdev
->name
,
1609 return -bt_to_errno(ver
->status
);
1612 BT_INFO("%s: read Intel version: %02x%02x%02x%02x%02x%02x%02x%02x%02x",
1613 hdev
->name
, ver
->hw_platform
, ver
->hw_variant
,
1614 ver
->hw_revision
, ver
->fw_variant
, ver
->fw_revision
,
1615 ver
->fw_build_num
, ver
->fw_build_ww
, ver
->fw_build_yy
,
1618 /* fw_patch_num indicates the version of patch the device currently
1619 * have. If there is no patch data in the device, it is always 0x00.
1620 * So, if it is other than 0x00, no need to patch the deivce again.
1622 if (ver
->fw_patch_num
) {
1623 BT_INFO("%s: Intel device is already patched. patch num: %02x",
1624 hdev
->name
, ver
->fw_patch_num
);
1626 btusb_check_bdaddr_intel(hdev
);
1630 /* Opens the firmware patch file based on the firmware version read
1631 * from the controller. If it fails to open the matching firmware
1632 * patch file, it tries to open the default firmware patch file.
1633 * If no patch file is found, allow the device to operate without
1636 fw
= btusb_setup_intel_get_fw(hdev
, ver
);
1639 btusb_check_bdaddr_intel(hdev
);
1644 /* This Intel specific command enables the manufacturer mode of the
1647 * Only while this mode is enabled, the driver can download the
1648 * firmware patch data and configuration parameters.
1650 skb
= __hci_cmd_sync(hdev
, 0xfc11, 2, mfg_enable
, HCI_INIT_TIMEOUT
);
1652 BT_ERR("%s entering Intel manufacturer mode failed (%ld)",
1653 hdev
->name
, PTR_ERR(skb
));
1654 release_firmware(fw
);
1655 return PTR_ERR(skb
);
1659 u8 evt_status
= skb
->data
[0];
1661 BT_ERR("%s enable Intel manufacturer mode event failed (%02x)",
1662 hdev
->name
, evt_status
);
1664 release_firmware(fw
);
1665 return -bt_to_errno(evt_status
);
1671 /* The firmware data file consists of list of Intel specific HCI
1672 * commands and its expected events. The first byte indicates the
1673 * type of the message, either HCI command or HCI event.
1675 * It reads the command and its expected event from the firmware file,
1676 * and send to the controller. Once __hci_cmd_sync_ev() returns,
1677 * the returned event is compared with the event read from the firmware
1678 * file and it will continue until all the messages are downloaded to
1681 * Once the firmware patching is completed successfully,
1682 * the manufacturer mode is disabled with reset and activating the
1685 * If the firmware patching fails, the manufacturer mode is
1686 * disabled with reset and deactivating the patch.
1688 * If the default patch file is used, no reset is done when disabling
1691 while (fw
->size
> fw_ptr
- fw
->data
) {
1694 ret
= btusb_setup_intel_patching(hdev
, fw
, &fw_ptr
,
1697 goto exit_mfg_deactivate
;
1700 release_firmware(fw
);
1703 goto exit_mfg_disable
;
1705 /* Patching completed successfully and disable the manufacturer mode
1706 * with reset and activate the downloaded firmware patches.
1708 skb
= __hci_cmd_sync(hdev
, 0xfc11, sizeof(mfg_reset_activate
),
1709 mfg_reset_activate
, HCI_INIT_TIMEOUT
);
1711 BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
1712 hdev
->name
, PTR_ERR(skb
));
1713 return PTR_ERR(skb
);
1717 BT_INFO("%s: Intel Bluetooth firmware patch completed and activated",
1720 btusb_check_bdaddr_intel(hdev
);
1724 /* Disable the manufacturer mode without reset */
1725 skb
= __hci_cmd_sync(hdev
, 0xfc11, sizeof(mfg_disable
), mfg_disable
,
1728 BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
1729 hdev
->name
, PTR_ERR(skb
));
1730 return PTR_ERR(skb
);
1734 BT_INFO("%s: Intel Bluetooth firmware patch completed", hdev
->name
);
1736 btusb_check_bdaddr_intel(hdev
);
1739 exit_mfg_deactivate
:
1740 release_firmware(fw
);
1742 /* Patching failed. Disable the manufacturer mode with reset and
1743 * deactivate the downloaded firmware patches.
1745 skb
= __hci_cmd_sync(hdev
, 0xfc11, sizeof(mfg_reset_deactivate
),
1746 mfg_reset_deactivate
, HCI_INIT_TIMEOUT
);
1748 BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
1749 hdev
->name
, PTR_ERR(skb
));
1750 return PTR_ERR(skb
);
1754 BT_INFO("%s: Intel Bluetooth firmware patch completed and deactivated",
1757 btusb_check_bdaddr_intel(hdev
);
1761 static int inject_cmd_complete(struct hci_dev
*hdev
, __u16 opcode
)
1763 struct sk_buff
*skb
;
1764 struct hci_event_hdr
*hdr
;
1765 struct hci_ev_cmd_complete
*evt
;
1767 skb
= bt_skb_alloc(sizeof(*hdr
) + sizeof(*evt
) + 1, GFP_ATOMIC
);
1771 hdr
= (struct hci_event_hdr
*)skb_put(skb
, sizeof(*hdr
));
1772 hdr
->evt
= HCI_EV_CMD_COMPLETE
;
1773 hdr
->plen
= sizeof(*evt
) + 1;
1775 evt
= (struct hci_ev_cmd_complete
*)skb_put(skb
, sizeof(*evt
));
1777 evt
->opcode
= cpu_to_le16(opcode
);
1779 *skb_put(skb
, 1) = 0x00;
1781 bt_cb(skb
)->pkt_type
= HCI_EVENT_PKT
;
1783 return hci_recv_frame(hdev
, skb
);
1786 static int btusb_recv_bulk_intel(struct btusb_data
*data
, void *buffer
,
1789 /* When the device is in bootloader mode, then it can send
1790 * events via the bulk endpoint. These events are treated the
1791 * same way as the ones received from the interrupt endpoint.
1793 if (test_bit(BTUSB_BOOTLOADER
, &data
->flags
))
1794 return btusb_recv_intr(data
, buffer
, count
);
1796 return btusb_recv_bulk(data
, buffer
, count
);
1799 static int btusb_recv_event_intel(struct hci_dev
*hdev
, struct sk_buff
*skb
)
1801 struct btusb_data
*data
= hci_get_drvdata(hdev
);
1803 if (test_bit(BTUSB_BOOTLOADER
, &data
->flags
)) {
1804 struct hci_event_hdr
*hdr
= (void *)skb
->data
;
1806 /* When the firmware loading completes the device sends
1807 * out a vendor specific event indicating the result of
1808 * the firmware loading.
1810 if (skb
->len
== 7 && hdr
->evt
== 0xff && hdr
->plen
== 0x05 &&
1811 skb
->data
[2] == 0x06) {
1812 if (skb
->data
[3] != 0x00)
1813 test_bit(BTUSB_FIRMWARE_FAILED
, &data
->flags
);
1815 if (test_and_clear_bit(BTUSB_DOWNLOADING
,
1817 test_bit(BTUSB_FIRMWARE_LOADED
, &data
->flags
)) {
1818 smp_mb__after_atomic();
1819 wake_up_bit(&data
->flags
, BTUSB_DOWNLOADING
);
1823 /* When switching to the operational firmware the device
1824 * sends a vendor specific event indicating that the bootup
1827 if (skb
->len
== 9 && hdr
->evt
== 0xff && hdr
->plen
== 0x07 &&
1828 skb
->data
[2] == 0x02) {
1829 if (test_and_clear_bit(BTUSB_BOOTING
, &data
->flags
)) {
1830 smp_mb__after_atomic();
1831 wake_up_bit(&data
->flags
, BTUSB_BOOTING
);
1836 return hci_recv_frame(hdev
, skb
);
1839 static int btusb_send_frame_intel(struct hci_dev
*hdev
, struct sk_buff
*skb
)
1841 struct btusb_data
*data
= hci_get_drvdata(hdev
);
1844 BT_DBG("%s", hdev
->name
);
1846 if (!test_bit(HCI_RUNNING
, &hdev
->flags
))
1849 switch (bt_cb(skb
)->pkt_type
) {
1850 case HCI_COMMAND_PKT
:
1851 if (test_bit(BTUSB_BOOTLOADER
, &data
->flags
)) {
1852 struct hci_command_hdr
*cmd
= (void *)skb
->data
;
1853 __u16 opcode
= le16_to_cpu(cmd
->opcode
);
1855 /* When in bootloader mode and the command 0xfc09
1856 * is received, it needs to be send down the
1857 * bulk endpoint. So allocate a bulk URB instead.
1859 if (opcode
== 0xfc09)
1860 urb
= alloc_bulk_urb(hdev
, skb
);
1862 urb
= alloc_ctrl_urb(hdev
, skb
);
1864 /* When the 0xfc01 command is issued to boot into
1865 * the operational firmware, it will actually not
1866 * send a command complete event. To keep the flow
1867 * control working inject that event here.
1869 if (opcode
== 0xfc01)
1870 inject_cmd_complete(hdev
, opcode
);
1872 urb
= alloc_ctrl_urb(hdev
, skb
);
1875 return PTR_ERR(urb
);
1877 hdev
->stat
.cmd_tx
++;
1878 return submit_or_queue_tx_urb(hdev
, urb
);
1880 case HCI_ACLDATA_PKT
:
1881 urb
= alloc_bulk_urb(hdev
, skb
);
1883 return PTR_ERR(urb
);
1885 hdev
->stat
.acl_tx
++;
1886 return submit_or_queue_tx_urb(hdev
, urb
);
1888 case HCI_SCODATA_PKT
:
1889 if (hci_conn_num(hdev
, SCO_LINK
) < 1)
1892 urb
= alloc_isoc_urb(hdev
, skb
);
1894 return PTR_ERR(urb
);
1896 hdev
->stat
.sco_tx
++;
1897 return submit_tx_urb(hdev
, urb
);
1903 static int btusb_intel_secure_send(struct hci_dev
*hdev
, u8 fragment_type
,
1904 u32 plen
, const void *param
)
1907 struct sk_buff
*skb
;
1908 u8 cmd_param
[253], fragment_len
= (plen
> 252) ? 252 : plen
;
1910 cmd_param
[0] = fragment_type
;
1911 memcpy(cmd_param
+ 1, param
, fragment_len
);
1913 skb
= __hci_cmd_sync(hdev
, 0xfc09, fragment_len
+ 1,
1914 cmd_param
, HCI_INIT_TIMEOUT
);
1916 return PTR_ERR(skb
);
1920 plen
-= fragment_len
;
1921 param
+= fragment_len
;
1927 static void btusb_intel_version_info(struct hci_dev
*hdev
,
1928 struct intel_version
*ver
)
1930 const char *variant
;
1932 switch (ver
->fw_variant
) {
1934 variant
= "Bootloader";
1937 variant
= "Firmware";
1943 BT_INFO("%s: %s revision %u.%u build %u week %u %u", hdev
->name
,
1944 variant
, ver
->fw_revision
>> 4, ver
->fw_revision
& 0x0f,
1945 ver
->fw_build_num
, ver
->fw_build_ww
, 2000 + ver
->fw_build_yy
);
1948 static int btusb_setup_intel_new(struct hci_dev
*hdev
)
1950 static const u8 reset_param
[] = { 0x00, 0x01, 0x00, 0x01,
1951 0x00, 0x08, 0x04, 0x00 };
1952 struct btusb_data
*data
= hci_get_drvdata(hdev
);
1953 struct sk_buff
*skb
;
1954 struct intel_version
*ver
;
1955 struct intel_boot_params
*params
;
1956 const struct firmware
*fw
;
1959 ktime_t calltime
, delta
, rettime
;
1960 unsigned long long duration
;
1963 BT_DBG("%s", hdev
->name
);
1965 calltime
= ktime_get();
1967 /* Read the Intel version information to determine if the device
1968 * is in bootloader mode or if it already has operational firmware
1971 skb
= __hci_cmd_sync(hdev
, 0xfc05, 0, NULL
, HCI_INIT_TIMEOUT
);
1973 BT_ERR("%s: Reading Intel version information failed (%ld)",
1974 hdev
->name
, PTR_ERR(skb
));
1975 return PTR_ERR(skb
);
1978 if (skb
->len
!= sizeof(*ver
)) {
1979 BT_ERR("%s: Intel version event size mismatch", hdev
->name
);
1984 ver
= (struct intel_version
*)skb
->data
;
1986 BT_ERR("%s: Intel version command failure (%02x)",
1987 hdev
->name
, ver
->status
);
1988 err
= -bt_to_errno(ver
->status
);
1993 /* The hardware platform number has a fixed value of 0x37 and
1994 * for now only accept this single value.
1996 if (ver
->hw_platform
!= 0x37) {
1997 BT_ERR("%s: Unsupported Intel hardware platform (%u)",
1998 hdev
->name
, ver
->hw_platform
);
2003 /* At the moment only the hardware variant iBT 3.0 (LnP/SfP) is
2004 * supported by this firmware loading method. This check has been
2005 * put in place to ensure correct forward compatibility options
2006 * when newer hardware variants come along.
2008 if (ver
->hw_variant
!= 0x0b) {
2009 BT_ERR("%s: Unsupported Intel hardware variant (%u)",
2010 hdev
->name
, ver
->hw_variant
);
2015 btusb_intel_version_info(hdev
, ver
);
2017 /* The firmware variant determines if the device is in bootloader
2018 * mode or is running operational firmware. The value 0x06 identifies
2019 * the bootloader and the value 0x23 identifies the operational
2022 * When the operational firmware is already present, then only
2023 * the check for valid Bluetooth device address is needed. This
2024 * determines if the device will be added as configured or
2025 * unconfigured controller.
2027 * It is not possible to use the Secure Boot Parameters in this
2028 * case since that command is only available in bootloader mode.
2030 if (ver
->fw_variant
== 0x23) {
2032 clear_bit(BTUSB_BOOTLOADER
, &data
->flags
);
2033 btusb_check_bdaddr_intel(hdev
);
2037 /* If the device is not in bootloader mode, then the only possible
2038 * choice is to return an error and abort the device initialization.
2040 if (ver
->fw_variant
!= 0x06) {
2041 BT_ERR("%s: Unsupported Intel firmware variant (%u)",
2042 hdev
->name
, ver
->fw_variant
);
2049 /* Read the secure boot parameters to identify the operating
2050 * details of the bootloader.
2052 skb
= __hci_cmd_sync(hdev
, 0xfc0d, 0, NULL
, HCI_INIT_TIMEOUT
);
2054 BT_ERR("%s: Reading Intel boot parameters failed (%ld)",
2055 hdev
->name
, PTR_ERR(skb
));
2056 return PTR_ERR(skb
);
2059 if (skb
->len
!= sizeof(*params
)) {
2060 BT_ERR("%s: Intel boot parameters size mismatch", hdev
->name
);
2065 params
= (struct intel_boot_params
*)skb
->data
;
2066 if (params
->status
) {
2067 BT_ERR("%s: Intel boot parameters command failure (%02x)",
2068 hdev
->name
, params
->status
);
2069 err
= -bt_to_errno(params
->status
);
2074 BT_INFO("%s: Device revision is %u", hdev
->name
,
2075 le16_to_cpu(params
->dev_revid
));
2077 BT_INFO("%s: Secure boot is %s", hdev
->name
,
2078 params
->secure_boot
? "enabled" : "disabled");
2080 BT_INFO("%s: Minimum firmware build %u week %u %u", hdev
->name
,
2081 params
->min_fw_build_nn
, params
->min_fw_build_cw
,
2082 2000 + params
->min_fw_build_yy
);
2084 /* It is required that every single firmware fragment is acknowledged
2085 * with a command complete event. If the boot parameters indicate
2086 * that this bootloader does not send them, then abort the setup.
2088 if (params
->limited_cce
!= 0x00) {
2089 BT_ERR("%s: Unsupported Intel firmware loading method (%u)",
2090 hdev
->name
, params
->limited_cce
);
2095 /* If the OTP has no valid Bluetooth device address, then there will
2096 * also be no valid address for the operational firmware.
2098 if (!bacmp(¶ms
->otp_bdaddr
, BDADDR_ANY
)) {
2099 BT_INFO("%s: No device address configured", hdev
->name
);
2100 set_bit(HCI_QUIRK_INVALID_BDADDR
, &hdev
->quirks
);
2103 /* With this Intel bootloader only the hardware variant and device
2104 * revision information are used to select the right firmware.
2106 * Currently this bootloader support is limited to hardware variant
2107 * iBT 3.0 (LnP/SfP) which is identified by the value 11 (0x0b).
2109 snprintf(fwname
, sizeof(fwname
), "intel/ibt-11-%u.sfi",
2110 le16_to_cpu(params
->dev_revid
));
2112 err
= request_firmware(&fw
, fwname
, &hdev
->dev
);
2114 BT_ERR("%s: Failed to load Intel firmware file (%d)",
2120 BT_INFO("%s: Found device firmware: %s", hdev
->name
, fwname
);
2124 if (fw
->size
< 644) {
2125 BT_ERR("%s: Invalid size of firmware file (%zu)",
2126 hdev
->name
, fw
->size
);
2131 set_bit(BTUSB_DOWNLOADING
, &data
->flags
);
2133 /* Start the firmware download transaction with the Init fragment
2134 * represented by the 128 bytes of CSS header.
2136 err
= btusb_intel_secure_send(hdev
, 0x00, 128, fw
->data
);
2138 BT_ERR("%s: Failed to send firmware header (%d)",
2143 /* Send the 256 bytes of public key information from the firmware
2144 * as the PKey fragment.
2146 err
= btusb_intel_secure_send(hdev
, 0x03, 256, fw
->data
+ 128);
2148 BT_ERR("%s: Failed to send firmware public key (%d)",
2153 /* Send the 256 bytes of signature information from the firmware
2154 * as the Sign fragment.
2156 err
= btusb_intel_secure_send(hdev
, 0x02, 256, fw
->data
+ 388);
2158 BT_ERR("%s: Failed to send firmware signature (%d)",
2163 fw_ptr
= fw
->data
+ 644;
2165 while (fw_ptr
- fw
->data
< fw
->size
) {
2166 struct hci_command_hdr
*cmd
= (void *)fw_ptr
;
2169 cmd_len
= sizeof(*cmd
) + cmd
->plen
;
2171 /* Send each command from the firmware data buffer as
2172 * a single Data fragment.
2174 err
= btusb_intel_secure_send(hdev
, 0x01, cmd_len
, fw_ptr
);
2176 BT_ERR("%s: Failed to send firmware data (%d)",
2184 set_bit(BTUSB_FIRMWARE_LOADED
, &data
->flags
);
2186 BT_INFO("%s: Waiting for firmware download to complete", hdev
->name
);
2188 /* Before switching the device into operational mode and with that
2189 * booting the loaded firmware, wait for the bootloader notification
2190 * that all fragments have been successfully received.
2192 * When the event processing receives the notification, then the
2193 * BTUSB_DOWNLOADING flag will be cleared.
2195 * The firmware loading should not take longer than 5 seconds
2196 * and thus just timeout if that happens and fail the setup
2199 err
= btusb_wait_on_bit_timeout(&data
->flags
, BTUSB_DOWNLOADING
,
2200 msecs_to_jiffies(5000),
2201 TASK_INTERRUPTIBLE
);
2203 BT_ERR("%s: Firmware loading interrupted", hdev
->name
);
2209 BT_ERR("%s: Firmware loading timeout", hdev
->name
);
2214 if (test_bit(BTUSB_FIRMWARE_FAILED
, &data
->flags
)) {
2215 BT_ERR("%s: Firmware loading failed", hdev
->name
);
2220 rettime
= ktime_get();
2221 delta
= ktime_sub(rettime
, calltime
);
2222 duration
= (unsigned long long) ktime_to_ns(delta
) >> 10;
2224 BT_INFO("%s: Firmware loaded in %llu usecs", hdev
->name
, duration
);
2227 release_firmware(fw
);
2232 calltime
= ktime_get();
2234 set_bit(BTUSB_BOOTING
, &data
->flags
);
2236 skb
= __hci_cmd_sync(hdev
, 0xfc01, sizeof(reset_param
), reset_param
,
2239 return PTR_ERR(skb
);
2243 /* The bootloader will not indicate when the device is ready. This
2244 * is done by the operational firmware sending bootup notification.
2246 * Booting into operational firmware should not take longer than
2247 * 1 second. However if that happens, then just fail the setup
2248 * since something went wrong.
2250 BT_INFO("%s: Waiting for device to boot", hdev
->name
);
2252 err
= btusb_wait_on_bit_timeout(&data
->flags
, BTUSB_BOOTING
,
2253 msecs_to_jiffies(1000),
2254 TASK_INTERRUPTIBLE
);
2257 BT_ERR("%s: Device boot interrupted", hdev
->name
);
2262 BT_ERR("%s: Device boot timeout", hdev
->name
);
2266 rettime
= ktime_get();
2267 delta
= ktime_sub(rettime
, calltime
);
2268 duration
= (unsigned long long) ktime_to_ns(delta
) >> 10;
2270 BT_INFO("%s: Device booted in %llu usecs", hdev
->name
, duration
);
2272 clear_bit(BTUSB_BOOTLOADER
, &data
->flags
);
2277 static void btusb_hw_error_intel(struct hci_dev
*hdev
, u8 code
)
2279 struct sk_buff
*skb
;
2282 BT_ERR("%s: Hardware error 0x%2.2x", hdev
->name
, code
);
2284 skb
= __hci_cmd_sync(hdev
, HCI_OP_RESET
, 0, NULL
, HCI_INIT_TIMEOUT
);
2286 BT_ERR("%s: Reset after hardware error failed (%ld)",
2287 hdev
->name
, PTR_ERR(skb
));
2292 skb
= __hci_cmd_sync(hdev
, 0xfc22, 1, &type
, HCI_INIT_TIMEOUT
);
2294 BT_ERR("%s: Retrieving Intel exception info failed (%ld)",
2295 hdev
->name
, PTR_ERR(skb
));
2299 if (skb
->len
!= 13) {
2300 BT_ERR("%s: Exception info size mismatch", hdev
->name
);
2305 if (skb
->data
[0] != 0x00) {
2306 BT_ERR("%s: Exception info command failure (%02x)",
2307 hdev
->name
, skb
->data
[0]);
2312 BT_ERR("%s: Exception info %s", hdev
->name
, (char *)(skb
->data
+ 1));
2317 static int btusb_set_bdaddr_intel(struct hci_dev
*hdev
, const bdaddr_t
*bdaddr
)
2319 struct sk_buff
*skb
;
2322 skb
= __hci_cmd_sync(hdev
, 0xfc31, 6, bdaddr
, HCI_INIT_TIMEOUT
);
2325 BT_ERR("%s: changing Intel device address failed (%ld)",
2334 static int btusb_set_bdaddr_marvell(struct hci_dev
*hdev
,
2335 const bdaddr_t
*bdaddr
)
2337 struct sk_buff
*skb
;
2342 buf
[1] = sizeof(bdaddr_t
);
2343 memcpy(buf
+ 2, bdaddr
, sizeof(bdaddr_t
));
2345 skb
= __hci_cmd_sync(hdev
, 0xfc22, sizeof(buf
), buf
, HCI_INIT_TIMEOUT
);
2348 BT_ERR("%s: changing Marvell device address failed (%ld)",
2357 #define BDADDR_BCM20702A0 (&(bdaddr_t) {{0x00, 0xa0, 0x02, 0x70, 0x20, 0x00}})
2359 static int btusb_setup_bcm_patchram(struct hci_dev
*hdev
)
2361 struct btusb_data
*data
= hci_get_drvdata(hdev
);
2362 struct usb_device
*udev
= data
->udev
;
2364 const struct firmware
*fw
;
2367 const struct hci_command_hdr
*cmd
;
2368 const u8
*cmd_param
;
2370 struct sk_buff
*skb
;
2371 struct hci_rp_read_local_version
*ver
;
2372 struct hci_rp_read_bd_addr
*bda
;
2375 snprintf(fw_name
, sizeof(fw_name
), "brcm/%s-%04x-%04x.hcd",
2376 udev
->product
? udev
->product
: "BCM",
2377 le16_to_cpu(udev
->descriptor
.idVendor
),
2378 le16_to_cpu(udev
->descriptor
.idProduct
));
2380 ret
= request_firmware(&fw
, fw_name
, &hdev
->dev
);
2382 BT_INFO("%s: BCM: patch %s not found", hdev
->name
, fw_name
);
2387 skb
= __hci_cmd_sync(hdev
, HCI_OP_RESET
, 0, NULL
, HCI_INIT_TIMEOUT
);
2390 BT_ERR("%s: HCI_OP_RESET failed (%ld)", hdev
->name
, ret
);
2395 /* Read Local Version Info */
2396 skb
= __hci_cmd_sync(hdev
, HCI_OP_READ_LOCAL_VERSION
, 0, NULL
,
2400 BT_ERR("%s: HCI_OP_READ_LOCAL_VERSION failed (%ld)",
2405 if (skb
->len
!= sizeof(*ver
)) {
2406 BT_ERR("%s: HCI_OP_READ_LOCAL_VERSION event length mismatch",
2413 ver
= (struct hci_rp_read_local_version
*)skb
->data
;
2414 BT_INFO("%s: BCM: patching hci_ver=%02x hci_rev=%04x lmp_ver=%02x "
2415 "lmp_subver=%04x", hdev
->name
, ver
->hci_ver
, ver
->hci_rev
,
2416 ver
->lmp_ver
, ver
->lmp_subver
);
2419 /* Start Download */
2420 skb
= __hci_cmd_sync(hdev
, 0xfc2e, 0, NULL
, HCI_INIT_TIMEOUT
);
2423 BT_ERR("%s: BCM: Download Minidrv command failed (%ld)",
2429 /* 50 msec delay after Download Minidrv completes */
2435 while (fw_size
>= sizeof(*cmd
)) {
2436 cmd
= (struct hci_command_hdr
*)fw_ptr
;
2437 fw_ptr
+= sizeof(*cmd
);
2438 fw_size
-= sizeof(*cmd
);
2440 if (fw_size
< cmd
->plen
) {
2441 BT_ERR("%s: BCM: patch %s is corrupted",
2442 hdev
->name
, fw_name
);
2448 fw_ptr
+= cmd
->plen
;
2449 fw_size
-= cmd
->plen
;
2451 opcode
= le16_to_cpu(cmd
->opcode
);
2453 skb
= __hci_cmd_sync(hdev
, opcode
, cmd
->plen
, cmd_param
,
2457 BT_ERR("%s: BCM: patch command %04x failed (%ld)",
2458 hdev
->name
, opcode
, ret
);
2464 /* 250 msec delay after Launch Ram completes */
2469 skb
= __hci_cmd_sync(hdev
, HCI_OP_RESET
, 0, NULL
, HCI_INIT_TIMEOUT
);
2472 BT_ERR("%s: HCI_OP_RESET failed (%ld)", hdev
->name
, ret
);
2477 /* Read Local Version Info */
2478 skb
= __hci_cmd_sync(hdev
, HCI_OP_READ_LOCAL_VERSION
, 0, NULL
,
2482 BT_ERR("%s: HCI_OP_READ_LOCAL_VERSION failed (%ld)",
2487 if (skb
->len
!= sizeof(*ver
)) {
2488 BT_ERR("%s: HCI_OP_READ_LOCAL_VERSION event length mismatch",
2495 ver
= (struct hci_rp_read_local_version
*)skb
->data
;
2496 BT_INFO("%s: BCM: firmware hci_ver=%02x hci_rev=%04x lmp_ver=%02x "
2497 "lmp_subver=%04x", hdev
->name
, ver
->hci_ver
, ver
->hci_rev
,
2498 ver
->lmp_ver
, ver
->lmp_subver
);
2501 /* Read BD Address */
2502 skb
= __hci_cmd_sync(hdev
, HCI_OP_READ_BD_ADDR
, 0, NULL
,
2506 BT_ERR("%s: HCI_OP_READ_BD_ADDR failed (%ld)",
2511 if (skb
->len
!= sizeof(*bda
)) {
2512 BT_ERR("%s: HCI_OP_READ_BD_ADDR event length mismatch",
2519 bda
= (struct hci_rp_read_bd_addr
*)skb
->data
;
2521 BT_ERR("%s: HCI_OP_READ_BD_ADDR error status (%02x)",
2522 hdev
->name
, bda
->status
);
2524 ret
= -bt_to_errno(bda
->status
);
2528 /* The address 00:20:70:02:A0:00 indicates a BCM20702A0 controller
2529 * with no configured address.
2531 if (!bacmp(&bda
->bdaddr
, BDADDR_BCM20702A0
)) {
2532 BT_INFO("%s: BCM: using default device address (%pMR)",
2533 hdev
->name
, &bda
->bdaddr
);
2534 set_bit(HCI_QUIRK_INVALID_BDADDR
, &hdev
->quirks
);
2540 release_firmware(fw
);
2545 static int btusb_set_bdaddr_bcm(struct hci_dev
*hdev
, const bdaddr_t
*bdaddr
)
2547 struct sk_buff
*skb
;
2550 skb
= __hci_cmd_sync(hdev
, 0xfc01, 6, bdaddr
, HCI_INIT_TIMEOUT
);
2553 BT_ERR("%s: BCM: Change address command failed (%ld)",
2562 static int btusb_set_bdaddr_ath3012(struct hci_dev
*hdev
,
2563 const bdaddr_t
*bdaddr
)
2565 struct sk_buff
*skb
;
2572 buf
[3] = sizeof(bdaddr_t
);
2573 memcpy(buf
+ 4, bdaddr
, sizeof(bdaddr_t
));
2575 skb
= __hci_cmd_sync(hdev
, 0xfc0b, sizeof(buf
), buf
, HCI_INIT_TIMEOUT
);
2578 BT_ERR("%s: Change address command failed (%ld)",
2587 static int btusb_probe(struct usb_interface
*intf
,
2588 const struct usb_device_id
*id
)
2590 struct usb_endpoint_descriptor
*ep_desc
;
2591 struct btusb_data
*data
;
2592 struct hci_dev
*hdev
;
2595 BT_DBG("intf %p id %p", intf
, id
);
2597 /* interface numbers are hardcoded in the spec */
2598 if (intf
->cur_altsetting
->desc
.bInterfaceNumber
!= 0)
2601 if (!id
->driver_info
) {
2602 const struct usb_device_id
*match
;
2604 match
= usb_match_id(intf
, blacklist_table
);
2609 if (id
->driver_info
== BTUSB_IGNORE
)
2612 if (id
->driver_info
& BTUSB_ATH3012
) {
2613 struct usb_device
*udev
= interface_to_usbdev(intf
);
2615 /* Old firmware would otherwise let ath3k driver load
2616 * patch and sysconfig files */
2617 if (le16_to_cpu(udev
->descriptor
.bcdDevice
) <= 0x0001)
2621 data
= devm_kzalloc(&intf
->dev
, sizeof(*data
), GFP_KERNEL
);
2625 for (i
= 0; i
< intf
->cur_altsetting
->desc
.bNumEndpoints
; i
++) {
2626 ep_desc
= &intf
->cur_altsetting
->endpoint
[i
].desc
;
2628 if (!data
->intr_ep
&& usb_endpoint_is_int_in(ep_desc
)) {
2629 data
->intr_ep
= ep_desc
;
2633 if (!data
->bulk_tx_ep
&& usb_endpoint_is_bulk_out(ep_desc
)) {
2634 data
->bulk_tx_ep
= ep_desc
;
2638 if (!data
->bulk_rx_ep
&& usb_endpoint_is_bulk_in(ep_desc
)) {
2639 data
->bulk_rx_ep
= ep_desc
;
2644 if (!data
->intr_ep
|| !data
->bulk_tx_ep
|| !data
->bulk_rx_ep
)
2647 if (id
->driver_info
& BTUSB_AMP
) {
2648 data
->cmdreq_type
= USB_TYPE_CLASS
| 0x01;
2649 data
->cmdreq
= 0x2b;
2651 data
->cmdreq_type
= USB_TYPE_CLASS
;
2652 data
->cmdreq
= 0x00;
2655 data
->udev
= interface_to_usbdev(intf
);
2658 INIT_WORK(&data
->work
, btusb_work
);
2659 INIT_WORK(&data
->waker
, btusb_waker
);
2660 init_usb_anchor(&data
->deferred
);
2661 init_usb_anchor(&data
->tx_anchor
);
2662 spin_lock_init(&data
->txlock
);
2664 init_usb_anchor(&data
->intr_anchor
);
2665 init_usb_anchor(&data
->bulk_anchor
);
2666 init_usb_anchor(&data
->isoc_anchor
);
2667 spin_lock_init(&data
->rxlock
);
2669 if (id
->driver_info
& BTUSB_INTEL_NEW
) {
2670 data
->recv_event
= btusb_recv_event_intel
;
2671 data
->recv_bulk
= btusb_recv_bulk_intel
;
2672 set_bit(BTUSB_BOOTLOADER
, &data
->flags
);
2674 data
->recv_event
= hci_recv_frame
;
2675 data
->recv_bulk
= btusb_recv_bulk
;
2678 hdev
= hci_alloc_dev();
2682 hdev
->bus
= HCI_USB
;
2683 hci_set_drvdata(hdev
, data
);
2685 if (id
->driver_info
& BTUSB_AMP
)
2686 hdev
->dev_type
= HCI_AMP
;
2688 hdev
->dev_type
= HCI_BREDR
;
2692 SET_HCIDEV_DEV(hdev
, &intf
->dev
);
2694 hdev
->open
= btusb_open
;
2695 hdev
->close
= btusb_close
;
2696 hdev
->flush
= btusb_flush
;
2697 hdev
->send
= btusb_send_frame
;
2698 hdev
->notify
= btusb_notify
;
2700 if (id
->driver_info
& BTUSB_BCM92035
)
2701 hdev
->setup
= btusb_setup_bcm92035
;
2703 if (id
->driver_info
& BTUSB_BCM_PATCHRAM
) {
2704 hdev
->setup
= btusb_setup_bcm_patchram
;
2705 hdev
->set_bdaddr
= btusb_set_bdaddr_bcm
;
2706 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER
, &hdev
->quirks
);
2709 if (id
->driver_info
& BTUSB_INTEL
) {
2710 hdev
->setup
= btusb_setup_intel
;
2711 hdev
->set_bdaddr
= btusb_set_bdaddr_intel
;
2712 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER
, &hdev
->quirks
);
2715 if (id
->driver_info
& BTUSB_INTEL_NEW
) {
2716 hdev
->send
= btusb_send_frame_intel
;
2717 hdev
->setup
= btusb_setup_intel_new
;
2718 hdev
->hw_error
= btusb_hw_error_intel
;
2719 hdev
->set_bdaddr
= btusb_set_bdaddr_intel
;
2720 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER
, &hdev
->quirks
);
2723 if (id
->driver_info
& BTUSB_MARVELL
)
2724 hdev
->set_bdaddr
= btusb_set_bdaddr_marvell
;
2726 if (id
->driver_info
& BTUSB_SWAVE
) {
2727 set_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE
, &hdev
->quirks
);
2728 set_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS
, &hdev
->quirks
);
2731 if (id
->driver_info
& BTUSB_INTEL_BOOT
)
2732 set_bit(HCI_QUIRK_RAW_DEVICE
, &hdev
->quirks
);
2734 if (id
->driver_info
& BTUSB_ATH3012
) {
2735 hdev
->set_bdaddr
= btusb_set_bdaddr_ath3012
;
2736 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER
, &hdev
->quirks
);
2739 if (id
->driver_info
& BTUSB_AMP
) {
2740 /* AMP controllers do not support SCO packets */
2743 /* Interface numbers are hardcoded in the specification */
2744 data
->isoc
= usb_ifnum_to_if(data
->udev
, 1);
2748 set_bit(HCI_QUIRK_RESET_ON_CLOSE
, &hdev
->quirks
);
2750 if (force_scofix
|| id
->driver_info
& BTUSB_WRONG_SCO_MTU
) {
2751 if (!disable_scofix
)
2752 set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE
, &hdev
->quirks
);
2755 if (id
->driver_info
& BTUSB_BROKEN_ISOC
)
2758 if (id
->driver_info
& BTUSB_DIGIANSWER
) {
2759 data
->cmdreq_type
= USB_TYPE_VENDOR
;
2760 set_bit(HCI_QUIRK_RESET_ON_CLOSE
, &hdev
->quirks
);
2763 if (id
->driver_info
& BTUSB_CSR
) {
2764 struct usb_device
*udev
= data
->udev
;
2765 u16 bcdDevice
= le16_to_cpu(udev
->descriptor
.bcdDevice
);
2767 /* Old firmware would otherwise execute USB reset */
2768 if (bcdDevice
< 0x117)
2769 set_bit(HCI_QUIRK_RESET_ON_CLOSE
, &hdev
->quirks
);
2771 /* Fake CSR devices with broken commands */
2772 if (bcdDevice
<= 0x100)
2773 hdev
->setup
= btusb_setup_csr
;
2776 if (id
->driver_info
& BTUSB_SNIFFER
) {
2777 struct usb_device
*udev
= data
->udev
;
2779 /* New sniffer firmware has crippled HCI interface */
2780 if (le16_to_cpu(udev
->descriptor
.bcdDevice
) > 0x997)
2781 set_bit(HCI_QUIRK_RAW_DEVICE
, &hdev
->quirks
);
2784 if (id
->driver_info
& BTUSB_INTEL_BOOT
) {
2785 /* A bug in the bootloader causes that interrupt interface is
2786 * only enabled after receiving SetInterface(0, AltSetting=0).
2788 err
= usb_set_interface(data
->udev
, 0, 0);
2790 BT_ERR("failed to set interface 0, alt 0 %d", err
);
2797 err
= usb_driver_claim_interface(&btusb_driver
,
2805 err
= hci_register_dev(hdev
);
2811 usb_set_intfdata(intf
, data
);
2816 static void btusb_disconnect(struct usb_interface
*intf
)
2818 struct btusb_data
*data
= usb_get_intfdata(intf
);
2819 struct hci_dev
*hdev
;
2821 BT_DBG("intf %p", intf
);
2827 usb_set_intfdata(data
->intf
, NULL
);
2830 usb_set_intfdata(data
->isoc
, NULL
);
2832 hci_unregister_dev(hdev
);
2834 if (intf
== data
->isoc
)
2835 usb_driver_release_interface(&btusb_driver
, data
->intf
);
2836 else if (data
->isoc
)
2837 usb_driver_release_interface(&btusb_driver
, data
->isoc
);
2843 static int btusb_suspend(struct usb_interface
*intf
, pm_message_t message
)
2845 struct btusb_data
*data
= usb_get_intfdata(intf
);
2847 BT_DBG("intf %p", intf
);
2849 if (data
->suspend_count
++)
2852 spin_lock_irq(&data
->txlock
);
2853 if (!(PMSG_IS_AUTO(message
) && data
->tx_in_flight
)) {
2854 set_bit(BTUSB_SUSPENDING
, &data
->flags
);
2855 spin_unlock_irq(&data
->txlock
);
2857 spin_unlock_irq(&data
->txlock
);
2858 data
->suspend_count
--;
2862 cancel_work_sync(&data
->work
);
2864 btusb_stop_traffic(data
);
2865 usb_kill_anchored_urbs(&data
->tx_anchor
);
2870 static void play_deferred(struct btusb_data
*data
)
2875 while ((urb
= usb_get_from_anchor(&data
->deferred
))) {
2876 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
2880 data
->tx_in_flight
++;
2882 usb_scuttle_anchored_urbs(&data
->deferred
);
2885 static int btusb_resume(struct usb_interface
*intf
)
2887 struct btusb_data
*data
= usb_get_intfdata(intf
);
2888 struct hci_dev
*hdev
= data
->hdev
;
2891 BT_DBG("intf %p", intf
);
2893 if (--data
->suspend_count
)
2896 if (!test_bit(HCI_RUNNING
, &hdev
->flags
))
2899 if (test_bit(BTUSB_INTR_RUNNING
, &data
->flags
)) {
2900 err
= btusb_submit_intr_urb(hdev
, GFP_NOIO
);
2902 clear_bit(BTUSB_INTR_RUNNING
, &data
->flags
);
2907 if (test_bit(BTUSB_BULK_RUNNING
, &data
->flags
)) {
2908 err
= btusb_submit_bulk_urb(hdev
, GFP_NOIO
);
2910 clear_bit(BTUSB_BULK_RUNNING
, &data
->flags
);
2914 btusb_submit_bulk_urb(hdev
, GFP_NOIO
);
2917 if (test_bit(BTUSB_ISOC_RUNNING
, &data
->flags
)) {
2918 if (btusb_submit_isoc_urb(hdev
, GFP_NOIO
) < 0)
2919 clear_bit(BTUSB_ISOC_RUNNING
, &data
->flags
);
2921 btusb_submit_isoc_urb(hdev
, GFP_NOIO
);
2924 spin_lock_irq(&data
->txlock
);
2925 play_deferred(data
);
2926 clear_bit(BTUSB_SUSPENDING
, &data
->flags
);
2927 spin_unlock_irq(&data
->txlock
);
2928 schedule_work(&data
->work
);
2933 usb_scuttle_anchored_urbs(&data
->deferred
);
2935 spin_lock_irq(&data
->txlock
);
2936 clear_bit(BTUSB_SUSPENDING
, &data
->flags
);
2937 spin_unlock_irq(&data
->txlock
);
2943 static struct usb_driver btusb_driver
= {
2945 .probe
= btusb_probe
,
2946 .disconnect
= btusb_disconnect
,
2948 .suspend
= btusb_suspend
,
2949 .resume
= btusb_resume
,
2951 .id_table
= btusb_table
,
2952 .supports_autosuspend
= 1,
2953 .disable_hub_initiated_lpm
= 1,
2956 module_usb_driver(btusb_driver
);
2958 module_param(disable_scofix
, bool, 0644);
2959 MODULE_PARM_DESC(disable_scofix
, "Disable fixup of wrong SCO buffer size");
2961 module_param(force_scofix
, bool, 0644);
2962 MODULE_PARM_DESC(force_scofix
, "Force fixup of wrong SCO buffers size");
2964 module_param(reset
, bool, 0644);
2965 MODULE_PARM_DESC(reset
, "Send HCI reset command on initialization");
2967 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
2968 MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION
);
2969 MODULE_VERSION(VERSION
);
2970 MODULE_LICENSE("GPL");