3 * Generic Bluetooth USB driver
5 * Copyright (C) 2005-2008 Marcel Holtmann <marcel@holtmann.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include <linux/module.h>
25 #include <linux/usb.h>
26 #include <linux/firmware.h>
27 #include <asm/unaligned.h>
29 #include <net/bluetooth/bluetooth.h>
30 #include <net/bluetooth/hci_core.h>
37 static bool disable_scofix
;
38 static bool force_scofix
;
40 static bool reset
= 1;
42 static struct usb_driver btusb_driver
;
44 #define BTUSB_IGNORE 0x01
45 #define BTUSB_DIGIANSWER 0x02
46 #define BTUSB_CSR 0x04
47 #define BTUSB_SNIFFER 0x08
48 #define BTUSB_BCM92035 0x10
49 #define BTUSB_BROKEN_ISOC 0x20
50 #define BTUSB_WRONG_SCO_MTU 0x40
51 #define BTUSB_ATH3012 0x80
52 #define BTUSB_INTEL 0x100
53 #define BTUSB_INTEL_BOOT 0x200
54 #define BTUSB_BCM_PATCHRAM 0x400
55 #define BTUSB_MARVELL 0x800
56 #define BTUSB_SWAVE 0x1000
57 #define BTUSB_INTEL_NEW 0x2000
58 #define BTUSB_AMP 0x4000
59 #define BTUSB_QCA_ROME 0x8000
60 #define BTUSB_BCM_APPLE 0x10000
61 #define BTUSB_REALTEK 0x20000
63 static const struct usb_device_id btusb_table
[] = {
64 /* Generic Bluetooth USB device */
65 { USB_DEVICE_INFO(0xe0, 0x01, 0x01) },
67 /* Generic Bluetooth AMP device */
68 { USB_DEVICE_INFO(0xe0, 0x01, 0x04), .driver_info
= BTUSB_AMP
},
70 /* Apple-specific (Broadcom) devices */
71 { USB_VENDOR_AND_INTERFACE_INFO(0x05ac, 0xff, 0x01, 0x01),
72 .driver_info
= BTUSB_BCM_APPLE
},
74 /* MediaTek MT76x0E */
75 { USB_DEVICE(0x0e8d, 0x763f) },
77 /* Broadcom SoftSailing reporting vendor specific */
78 { USB_DEVICE(0x0a5c, 0x21e1) },
80 /* Apple MacBookPro 7,1 */
81 { USB_DEVICE(0x05ac, 0x8213) },
84 { USB_DEVICE(0x05ac, 0x8215) },
86 /* Apple MacBookPro6,2 */
87 { USB_DEVICE(0x05ac, 0x8218) },
89 /* Apple MacBookAir3,1, MacBookAir3,2 */
90 { USB_DEVICE(0x05ac, 0x821b) },
92 /* Apple MacBookAir4,1 */
93 { USB_DEVICE(0x05ac, 0x821f) },
95 /* Apple MacBookPro8,2 */
96 { USB_DEVICE(0x05ac, 0x821a) },
98 /* Apple MacMini5,1 */
99 { USB_DEVICE(0x05ac, 0x8281) },
101 /* AVM BlueFRITZ! USB v2.0 */
102 { USB_DEVICE(0x057c, 0x3800), .driver_info
= BTUSB_SWAVE
},
104 /* Bluetooth Ultraport Module from IBM */
105 { USB_DEVICE(0x04bf, 0x030a) },
107 /* ALPS Modules with non-standard id */
108 { USB_DEVICE(0x044e, 0x3001) },
109 { USB_DEVICE(0x044e, 0x3002) },
111 /* Ericsson with non-standard id */
112 { USB_DEVICE(0x0bdb, 0x1002) },
114 /* Canyon CN-BTU1 with HID interfaces */
115 { USB_DEVICE(0x0c10, 0x0000) },
117 /* Broadcom BCM20702A0 */
118 { USB_DEVICE(0x413c, 0x8197) },
120 /* Broadcom BCM20702B0 (Dynex/Insignia) */
121 { USB_DEVICE(0x19ff, 0x0239), .driver_info
= BTUSB_BCM_PATCHRAM
},
123 /* Foxconn - Hon Hai */
124 { USB_VENDOR_AND_INTERFACE_INFO(0x0489, 0xff, 0x01, 0x01),
125 .driver_info
= BTUSB_BCM_PATCHRAM
},
127 /* Lite-On Technology - Broadcom based */
128 { USB_VENDOR_AND_INTERFACE_INFO(0x04ca, 0xff, 0x01, 0x01),
129 .driver_info
= BTUSB_BCM_PATCHRAM
},
131 /* Broadcom devices with vendor specific id */
132 { USB_VENDOR_AND_INTERFACE_INFO(0x0a5c, 0xff, 0x01, 0x01),
133 .driver_info
= BTUSB_BCM_PATCHRAM
},
135 /* ASUSTek Computer - Broadcom based */
136 { USB_VENDOR_AND_INTERFACE_INFO(0x0b05, 0xff, 0x01, 0x01),
137 .driver_info
= BTUSB_BCM_PATCHRAM
},
139 /* Belkin F8065bf - Broadcom based */
140 { USB_VENDOR_AND_INTERFACE_INFO(0x050d, 0xff, 0x01, 0x01),
141 .driver_info
= BTUSB_BCM_PATCHRAM
},
143 /* IMC Networks - Broadcom based */
144 { USB_VENDOR_AND_INTERFACE_INFO(0x13d3, 0xff, 0x01, 0x01),
145 .driver_info
= BTUSB_BCM_PATCHRAM
},
147 /* Intel Bluetooth USB Bootloader (RAM module) */
148 { USB_DEVICE(0x8087, 0x0a5a),
149 .driver_info
= BTUSB_INTEL_BOOT
| BTUSB_BROKEN_ISOC
},
151 { } /* Terminating entry */
154 MODULE_DEVICE_TABLE(usb
, btusb_table
);
156 static const struct usb_device_id blacklist_table
[] = {
157 /* CSR BlueCore devices */
158 { USB_DEVICE(0x0a12, 0x0001), .driver_info
= BTUSB_CSR
},
160 /* Broadcom BCM2033 without firmware */
161 { USB_DEVICE(0x0a5c, 0x2033), .driver_info
= BTUSB_IGNORE
},
163 /* Atheros 3011 with sflash firmware */
164 { USB_DEVICE(0x0489, 0xe027), .driver_info
= BTUSB_IGNORE
},
165 { USB_DEVICE(0x0489, 0xe03d), .driver_info
= BTUSB_IGNORE
},
166 { USB_DEVICE(0x04f2, 0xaff1), .driver_info
= BTUSB_IGNORE
},
167 { USB_DEVICE(0x0930, 0x0215), .driver_info
= BTUSB_IGNORE
},
168 { USB_DEVICE(0x0cf3, 0x3002), .driver_info
= BTUSB_IGNORE
},
169 { USB_DEVICE(0x0cf3, 0xe019), .driver_info
= BTUSB_IGNORE
},
170 { USB_DEVICE(0x13d3, 0x3304), .driver_info
= BTUSB_IGNORE
},
172 /* Atheros AR9285 Malbec with sflash firmware */
173 { USB_DEVICE(0x03f0, 0x311d), .driver_info
= BTUSB_IGNORE
},
175 /* Atheros 3012 with sflash firmware */
176 { USB_DEVICE(0x0489, 0xe04d), .driver_info
= BTUSB_ATH3012
},
177 { USB_DEVICE(0x0489, 0xe04e), .driver_info
= BTUSB_ATH3012
},
178 { USB_DEVICE(0x0489, 0xe056), .driver_info
= BTUSB_ATH3012
},
179 { USB_DEVICE(0x0489, 0xe057), .driver_info
= BTUSB_ATH3012
},
180 { USB_DEVICE(0x0489, 0xe05f), .driver_info
= BTUSB_ATH3012
},
181 { USB_DEVICE(0x0489, 0xe078), .driver_info
= BTUSB_ATH3012
},
182 { USB_DEVICE(0x04c5, 0x1330), .driver_info
= BTUSB_ATH3012
},
183 { USB_DEVICE(0x04ca, 0x3004), .driver_info
= BTUSB_ATH3012
},
184 { USB_DEVICE(0x04ca, 0x3005), .driver_info
= BTUSB_ATH3012
},
185 { USB_DEVICE(0x04ca, 0x3006), .driver_info
= BTUSB_ATH3012
},
186 { USB_DEVICE(0x04ca, 0x3007), .driver_info
= BTUSB_ATH3012
},
187 { USB_DEVICE(0x04ca, 0x3008), .driver_info
= BTUSB_ATH3012
},
188 { USB_DEVICE(0x04ca, 0x300b), .driver_info
= BTUSB_ATH3012
},
189 { USB_DEVICE(0x04ca, 0x3010), .driver_info
= BTUSB_ATH3012
},
190 { USB_DEVICE(0x0930, 0x0219), .driver_info
= BTUSB_ATH3012
},
191 { USB_DEVICE(0x0930, 0x0220), .driver_info
= BTUSB_ATH3012
},
192 { USB_DEVICE(0x0930, 0x0227), .driver_info
= BTUSB_ATH3012
},
193 { USB_DEVICE(0x0b05, 0x17d0), .driver_info
= BTUSB_ATH3012
},
194 { USB_DEVICE(0x0cf3, 0x0036), .driver_info
= BTUSB_ATH3012
},
195 { USB_DEVICE(0x0cf3, 0x3004), .driver_info
= BTUSB_ATH3012
},
196 { USB_DEVICE(0x0cf3, 0x3008), .driver_info
= BTUSB_ATH3012
},
197 { USB_DEVICE(0x0cf3, 0x311d), .driver_info
= BTUSB_ATH3012
},
198 { USB_DEVICE(0x0cf3, 0x311e), .driver_info
= BTUSB_ATH3012
},
199 { USB_DEVICE(0x0cf3, 0x311f), .driver_info
= BTUSB_ATH3012
},
200 { USB_DEVICE(0x0cf3, 0x3121), .driver_info
= BTUSB_ATH3012
},
201 { USB_DEVICE(0x0cf3, 0x817a), .driver_info
= BTUSB_ATH3012
},
202 { USB_DEVICE(0x0cf3, 0xe003), .driver_info
= BTUSB_ATH3012
},
203 { USB_DEVICE(0x0cf3, 0xe004), .driver_info
= BTUSB_ATH3012
},
204 { USB_DEVICE(0x0cf3, 0xe005), .driver_info
= BTUSB_ATH3012
},
205 { USB_DEVICE(0x13d3, 0x3362), .driver_info
= BTUSB_ATH3012
},
206 { USB_DEVICE(0x13d3, 0x3375), .driver_info
= BTUSB_ATH3012
},
207 { USB_DEVICE(0x13d3, 0x3393), .driver_info
= BTUSB_ATH3012
},
208 { USB_DEVICE(0x13d3, 0x3402), .driver_info
= BTUSB_ATH3012
},
209 { USB_DEVICE(0x13d3, 0x3408), .driver_info
= BTUSB_ATH3012
},
210 { USB_DEVICE(0x13d3, 0x3423), .driver_info
= BTUSB_ATH3012
},
211 { USB_DEVICE(0x13d3, 0x3432), .driver_info
= BTUSB_ATH3012
},
213 /* Atheros AR5BBU12 with sflash firmware */
214 { USB_DEVICE(0x0489, 0xe02c), .driver_info
= BTUSB_IGNORE
},
216 /* Atheros AR5BBU12 with sflash firmware */
217 { USB_DEVICE(0x0489, 0xe036), .driver_info
= BTUSB_ATH3012
},
218 { USB_DEVICE(0x0489, 0xe03c), .driver_info
= BTUSB_ATH3012
},
220 /* QCA ROME chipset */
221 { USB_DEVICE(0x0cf3, 0xe300), .driver_info
= BTUSB_QCA_ROME
},
222 { USB_DEVICE(0x0cf3, 0xe360), .driver_info
= BTUSB_QCA_ROME
},
224 /* Broadcom BCM2035 */
225 { USB_DEVICE(0x0a5c, 0x2009), .driver_info
= BTUSB_BCM92035
},
226 { USB_DEVICE(0x0a5c, 0x200a), .driver_info
= BTUSB_WRONG_SCO_MTU
},
227 { USB_DEVICE(0x0a5c, 0x2035), .driver_info
= BTUSB_WRONG_SCO_MTU
},
229 /* Broadcom BCM2045 */
230 { USB_DEVICE(0x0a5c, 0x2039), .driver_info
= BTUSB_WRONG_SCO_MTU
},
231 { USB_DEVICE(0x0a5c, 0x2101), .driver_info
= BTUSB_WRONG_SCO_MTU
},
233 /* IBM/Lenovo ThinkPad with Broadcom chip */
234 { USB_DEVICE(0x0a5c, 0x201e), .driver_info
= BTUSB_WRONG_SCO_MTU
},
235 { USB_DEVICE(0x0a5c, 0x2110), .driver_info
= BTUSB_WRONG_SCO_MTU
},
237 /* HP laptop with Broadcom chip */
238 { USB_DEVICE(0x03f0, 0x171d), .driver_info
= BTUSB_WRONG_SCO_MTU
},
240 /* Dell laptop with Broadcom chip */
241 { USB_DEVICE(0x413c, 0x8126), .driver_info
= BTUSB_WRONG_SCO_MTU
},
243 /* Dell Wireless 370 and 410 devices */
244 { USB_DEVICE(0x413c, 0x8152), .driver_info
= BTUSB_WRONG_SCO_MTU
},
245 { USB_DEVICE(0x413c, 0x8156), .driver_info
= BTUSB_WRONG_SCO_MTU
},
247 /* Belkin F8T012 and F8T013 devices */
248 { USB_DEVICE(0x050d, 0x0012), .driver_info
= BTUSB_WRONG_SCO_MTU
},
249 { USB_DEVICE(0x050d, 0x0013), .driver_info
= BTUSB_WRONG_SCO_MTU
},
251 /* Asus WL-BTD202 device */
252 { USB_DEVICE(0x0b05, 0x1715), .driver_info
= BTUSB_WRONG_SCO_MTU
},
254 /* Kensington Bluetooth USB adapter */
255 { USB_DEVICE(0x047d, 0x105e), .driver_info
= BTUSB_WRONG_SCO_MTU
},
257 /* RTX Telecom based adapters with buggy SCO support */
258 { USB_DEVICE(0x0400, 0x0807), .driver_info
= BTUSB_BROKEN_ISOC
},
259 { USB_DEVICE(0x0400, 0x080a), .driver_info
= BTUSB_BROKEN_ISOC
},
261 /* CONWISE Technology based adapters with buggy SCO support */
262 { USB_DEVICE(0x0e5e, 0x6622), .driver_info
= BTUSB_BROKEN_ISOC
},
264 /* Roper Class 1 Bluetooth Dongle (Silicon Wave based) */
265 { USB_DEVICE(0x1300, 0x0001), .driver_info
= BTUSB_SWAVE
},
267 /* Digianswer devices */
268 { USB_DEVICE(0x08fd, 0x0001), .driver_info
= BTUSB_DIGIANSWER
},
269 { USB_DEVICE(0x08fd, 0x0002), .driver_info
= BTUSB_IGNORE
},
271 /* CSR BlueCore Bluetooth Sniffer */
272 { USB_DEVICE(0x0a12, 0x0002),
273 .driver_info
= BTUSB_SNIFFER
| BTUSB_BROKEN_ISOC
},
275 /* Frontline ComProbe Bluetooth Sniffer */
276 { USB_DEVICE(0x16d3, 0x0002),
277 .driver_info
= BTUSB_SNIFFER
| BTUSB_BROKEN_ISOC
},
279 /* Marvell Bluetooth devices */
280 { USB_DEVICE(0x1286, 0x2044), .driver_info
= BTUSB_MARVELL
},
281 { USB_DEVICE(0x1286, 0x2046), .driver_info
= BTUSB_MARVELL
},
283 /* Intel Bluetooth devices */
284 { USB_DEVICE(0x8087, 0x07da), .driver_info
= BTUSB_CSR
},
285 { USB_DEVICE(0x8087, 0x07dc), .driver_info
= BTUSB_INTEL
},
286 { USB_DEVICE(0x8087, 0x0a2a), .driver_info
= BTUSB_INTEL
},
287 { USB_DEVICE(0x8087, 0x0a2b), .driver_info
= BTUSB_INTEL_NEW
},
289 /* Other Intel Bluetooth devices */
290 { USB_VENDOR_AND_INTERFACE_INFO(0x8087, 0xe0, 0x01, 0x01),
291 .driver_info
= BTUSB_IGNORE
},
293 /* Realtek Bluetooth devices */
294 { USB_VENDOR_AND_INTERFACE_INFO(0x0bda, 0xe0, 0x01, 0x01),
295 .driver_info
= BTUSB_REALTEK
},
297 /* Additional Realtek 8723AE Bluetooth devices */
298 { USB_DEVICE(0x0930, 0x021d), .driver_info
= BTUSB_REALTEK
},
299 { USB_DEVICE(0x13d3, 0x3394), .driver_info
= BTUSB_REALTEK
},
301 /* Additional Realtek 8723BE Bluetooth devices */
302 { USB_DEVICE(0x0489, 0xe085), .driver_info
= BTUSB_REALTEK
},
303 { USB_DEVICE(0x0489, 0xe08b), .driver_info
= BTUSB_REALTEK
},
304 { USB_DEVICE(0x13d3, 0x3410), .driver_info
= BTUSB_REALTEK
},
305 { USB_DEVICE(0x13d3, 0x3416), .driver_info
= BTUSB_REALTEK
},
306 { USB_DEVICE(0x13d3, 0x3459), .driver_info
= BTUSB_REALTEK
},
308 /* Additional Realtek 8821AE Bluetooth devices */
309 { USB_DEVICE(0x0b05, 0x17dc), .driver_info
= BTUSB_REALTEK
},
310 { USB_DEVICE(0x13d3, 0x3414), .driver_info
= BTUSB_REALTEK
},
311 { USB_DEVICE(0x13d3, 0x3458), .driver_info
= BTUSB_REALTEK
},
312 { USB_DEVICE(0x13d3, 0x3461), .driver_info
= BTUSB_REALTEK
},
313 { USB_DEVICE(0x13d3, 0x3462), .driver_info
= BTUSB_REALTEK
},
315 { } /* Terminating entry */
318 #define BTUSB_MAX_ISOC_FRAMES 10
320 #define BTUSB_INTR_RUNNING 0
321 #define BTUSB_BULK_RUNNING 1
322 #define BTUSB_ISOC_RUNNING 2
323 #define BTUSB_SUSPENDING 3
324 #define BTUSB_DID_ISO_RESUME 4
325 #define BTUSB_BOOTLOADER 5
326 #define BTUSB_DOWNLOADING 6
327 #define BTUSB_FIRMWARE_LOADED 7
328 #define BTUSB_FIRMWARE_FAILED 8
329 #define BTUSB_BOOTING 9
332 struct hci_dev
*hdev
;
333 struct usb_device
*udev
;
334 struct usb_interface
*intf
;
335 struct usb_interface
*isoc
;
339 struct work_struct work
;
340 struct work_struct waker
;
342 struct usb_anchor deferred
;
343 struct usb_anchor tx_anchor
;
347 struct usb_anchor intr_anchor
;
348 struct usb_anchor bulk_anchor
;
349 struct usb_anchor isoc_anchor
;
352 struct sk_buff
*evt_skb
;
353 struct sk_buff
*acl_skb
;
354 struct sk_buff
*sco_skb
;
356 struct usb_endpoint_descriptor
*intr_ep
;
357 struct usb_endpoint_descriptor
*bulk_tx_ep
;
358 struct usb_endpoint_descriptor
*bulk_rx_ep
;
359 struct usb_endpoint_descriptor
*isoc_tx_ep
;
360 struct usb_endpoint_descriptor
*isoc_rx_ep
;
365 unsigned int sco_num
;
369 int (*recv_event
)(struct hci_dev
*hdev
, struct sk_buff
*skb
);
370 int (*recv_bulk
)(struct btusb_data
*data
, void *buffer
, int count
);
372 int (*setup_on_usb
)(struct hci_dev
*hdev
);
375 static inline void btusb_free_frags(struct btusb_data
*data
)
379 spin_lock_irqsave(&data
->rxlock
, flags
);
381 kfree_skb(data
->evt_skb
);
382 data
->evt_skb
= NULL
;
384 kfree_skb(data
->acl_skb
);
385 data
->acl_skb
= NULL
;
387 kfree_skb(data
->sco_skb
);
388 data
->sco_skb
= NULL
;
390 spin_unlock_irqrestore(&data
->rxlock
, flags
);
393 static int btusb_recv_intr(struct btusb_data
*data
, void *buffer
, int count
)
398 spin_lock(&data
->rxlock
);
405 skb
= bt_skb_alloc(HCI_MAX_EVENT_SIZE
, GFP_ATOMIC
);
411 bt_cb(skb
)->pkt_type
= HCI_EVENT_PKT
;
412 bt_cb(skb
)->expect
= HCI_EVENT_HDR_SIZE
;
415 len
= min_t(uint
, bt_cb(skb
)->expect
, count
);
416 memcpy(skb_put(skb
, len
), buffer
, len
);
420 bt_cb(skb
)->expect
-= len
;
422 if (skb
->len
== HCI_EVENT_HDR_SIZE
) {
423 /* Complete event header */
424 bt_cb(skb
)->expect
= hci_event_hdr(skb
)->plen
;
426 if (skb_tailroom(skb
) < bt_cb(skb
)->expect
) {
435 if (bt_cb(skb
)->expect
== 0) {
437 data
->recv_event(data
->hdev
, skb
);
443 spin_unlock(&data
->rxlock
);
448 static int btusb_recv_bulk(struct btusb_data
*data
, void *buffer
, int count
)
453 spin_lock(&data
->rxlock
);
460 skb
= bt_skb_alloc(HCI_MAX_FRAME_SIZE
, GFP_ATOMIC
);
466 bt_cb(skb
)->pkt_type
= HCI_ACLDATA_PKT
;
467 bt_cb(skb
)->expect
= HCI_ACL_HDR_SIZE
;
470 len
= min_t(uint
, bt_cb(skb
)->expect
, count
);
471 memcpy(skb_put(skb
, len
), buffer
, len
);
475 bt_cb(skb
)->expect
-= len
;
477 if (skb
->len
== HCI_ACL_HDR_SIZE
) {
478 __le16 dlen
= hci_acl_hdr(skb
)->dlen
;
480 /* Complete ACL header */
481 bt_cb(skb
)->expect
= __le16_to_cpu(dlen
);
483 if (skb_tailroom(skb
) < bt_cb(skb
)->expect
) {
492 if (bt_cb(skb
)->expect
== 0) {
494 hci_recv_frame(data
->hdev
, skb
);
500 spin_unlock(&data
->rxlock
);
505 static int btusb_recv_isoc(struct btusb_data
*data
, void *buffer
, int count
)
510 spin_lock(&data
->rxlock
);
517 skb
= bt_skb_alloc(HCI_MAX_SCO_SIZE
, GFP_ATOMIC
);
523 bt_cb(skb
)->pkt_type
= HCI_SCODATA_PKT
;
524 bt_cb(skb
)->expect
= HCI_SCO_HDR_SIZE
;
527 len
= min_t(uint
, bt_cb(skb
)->expect
, count
);
528 memcpy(skb_put(skb
, len
), buffer
, len
);
532 bt_cb(skb
)->expect
-= len
;
534 if (skb
->len
== HCI_SCO_HDR_SIZE
) {
535 /* Complete SCO header */
536 bt_cb(skb
)->expect
= hci_sco_hdr(skb
)->dlen
;
538 if (skb_tailroom(skb
) < bt_cb(skb
)->expect
) {
547 if (bt_cb(skb
)->expect
== 0) {
549 hci_recv_frame(data
->hdev
, skb
);
555 spin_unlock(&data
->rxlock
);
560 static void btusb_intr_complete(struct urb
*urb
)
562 struct hci_dev
*hdev
= urb
->context
;
563 struct btusb_data
*data
= hci_get_drvdata(hdev
);
566 BT_DBG("%s urb %p status %d count %d", hdev
->name
, urb
, urb
->status
,
569 if (!test_bit(HCI_RUNNING
, &hdev
->flags
))
572 if (urb
->status
== 0) {
573 hdev
->stat
.byte_rx
+= urb
->actual_length
;
575 if (btusb_recv_intr(data
, urb
->transfer_buffer
,
576 urb
->actual_length
) < 0) {
577 BT_ERR("%s corrupted event packet", hdev
->name
);
580 } else if (urb
->status
== -ENOENT
) {
581 /* Avoid suspend failed when usb_kill_urb */
585 if (!test_bit(BTUSB_INTR_RUNNING
, &data
->flags
))
588 usb_mark_last_busy(data
->udev
);
589 usb_anchor_urb(urb
, &data
->intr_anchor
);
591 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
593 /* -EPERM: urb is being killed;
594 * -ENODEV: device got disconnected */
595 if (err
!= -EPERM
&& err
!= -ENODEV
)
596 BT_ERR("%s urb %p failed to resubmit (%d)",
597 hdev
->name
, urb
, -err
);
598 usb_unanchor_urb(urb
);
602 static int btusb_submit_intr_urb(struct hci_dev
*hdev
, gfp_t mem_flags
)
604 struct btusb_data
*data
= hci_get_drvdata(hdev
);
610 BT_DBG("%s", hdev
->name
);
615 urb
= usb_alloc_urb(0, mem_flags
);
619 size
= le16_to_cpu(data
->intr_ep
->wMaxPacketSize
);
621 buf
= kmalloc(size
, mem_flags
);
627 pipe
= usb_rcvintpipe(data
->udev
, data
->intr_ep
->bEndpointAddress
);
629 usb_fill_int_urb(urb
, data
->udev
, pipe
, buf
, size
,
630 btusb_intr_complete
, hdev
, data
->intr_ep
->bInterval
);
632 urb
->transfer_flags
|= URB_FREE_BUFFER
;
634 usb_anchor_urb(urb
, &data
->intr_anchor
);
636 err
= usb_submit_urb(urb
, mem_flags
);
638 if (err
!= -EPERM
&& err
!= -ENODEV
)
639 BT_ERR("%s urb %p submission failed (%d)",
640 hdev
->name
, urb
, -err
);
641 usb_unanchor_urb(urb
);
649 static void btusb_bulk_complete(struct urb
*urb
)
651 struct hci_dev
*hdev
= urb
->context
;
652 struct btusb_data
*data
= hci_get_drvdata(hdev
);
655 BT_DBG("%s urb %p status %d count %d", hdev
->name
, urb
, urb
->status
,
658 if (!test_bit(HCI_RUNNING
, &hdev
->flags
))
661 if (urb
->status
== 0) {
662 hdev
->stat
.byte_rx
+= urb
->actual_length
;
664 if (data
->recv_bulk(data
, urb
->transfer_buffer
,
665 urb
->actual_length
) < 0) {
666 BT_ERR("%s corrupted ACL packet", hdev
->name
);
669 } else if (urb
->status
== -ENOENT
) {
670 /* Avoid suspend failed when usb_kill_urb */
674 if (!test_bit(BTUSB_BULK_RUNNING
, &data
->flags
))
677 usb_anchor_urb(urb
, &data
->bulk_anchor
);
678 usb_mark_last_busy(data
->udev
);
680 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
682 /* -EPERM: urb is being killed;
683 * -ENODEV: device got disconnected */
684 if (err
!= -EPERM
&& err
!= -ENODEV
)
685 BT_ERR("%s urb %p failed to resubmit (%d)",
686 hdev
->name
, urb
, -err
);
687 usb_unanchor_urb(urb
);
691 static int btusb_submit_bulk_urb(struct hci_dev
*hdev
, gfp_t mem_flags
)
693 struct btusb_data
*data
= hci_get_drvdata(hdev
);
697 int err
, size
= HCI_MAX_FRAME_SIZE
;
699 BT_DBG("%s", hdev
->name
);
701 if (!data
->bulk_rx_ep
)
704 urb
= usb_alloc_urb(0, mem_flags
);
708 buf
= kmalloc(size
, mem_flags
);
714 pipe
= usb_rcvbulkpipe(data
->udev
, data
->bulk_rx_ep
->bEndpointAddress
);
716 usb_fill_bulk_urb(urb
, data
->udev
, pipe
, buf
, size
,
717 btusb_bulk_complete
, hdev
);
719 urb
->transfer_flags
|= URB_FREE_BUFFER
;
721 usb_mark_last_busy(data
->udev
);
722 usb_anchor_urb(urb
, &data
->bulk_anchor
);
724 err
= usb_submit_urb(urb
, mem_flags
);
726 if (err
!= -EPERM
&& err
!= -ENODEV
)
727 BT_ERR("%s urb %p submission failed (%d)",
728 hdev
->name
, urb
, -err
);
729 usb_unanchor_urb(urb
);
737 static void btusb_isoc_complete(struct urb
*urb
)
739 struct hci_dev
*hdev
= urb
->context
;
740 struct btusb_data
*data
= hci_get_drvdata(hdev
);
743 BT_DBG("%s urb %p status %d count %d", hdev
->name
, urb
, urb
->status
,
746 if (!test_bit(HCI_RUNNING
, &hdev
->flags
))
749 if (urb
->status
== 0) {
750 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
751 unsigned int offset
= urb
->iso_frame_desc
[i
].offset
;
752 unsigned int length
= urb
->iso_frame_desc
[i
].actual_length
;
754 if (urb
->iso_frame_desc
[i
].status
)
757 hdev
->stat
.byte_rx
+= length
;
759 if (btusb_recv_isoc(data
, urb
->transfer_buffer
+ offset
,
761 BT_ERR("%s corrupted SCO packet", hdev
->name
);
765 } else if (urb
->status
== -ENOENT
) {
766 /* Avoid suspend failed when usb_kill_urb */
770 if (!test_bit(BTUSB_ISOC_RUNNING
, &data
->flags
))
773 usb_anchor_urb(urb
, &data
->isoc_anchor
);
775 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
777 /* -EPERM: urb is being killed;
778 * -ENODEV: device got disconnected */
779 if (err
!= -EPERM
&& err
!= -ENODEV
)
780 BT_ERR("%s urb %p failed to resubmit (%d)",
781 hdev
->name
, urb
, -err
);
782 usb_unanchor_urb(urb
);
786 static inline void __fill_isoc_descriptor(struct urb
*urb
, int len
, int mtu
)
790 BT_DBG("len %d mtu %d", len
, mtu
);
792 for (i
= 0; i
< BTUSB_MAX_ISOC_FRAMES
&& len
>= mtu
;
793 i
++, offset
+= mtu
, len
-= mtu
) {
794 urb
->iso_frame_desc
[i
].offset
= offset
;
795 urb
->iso_frame_desc
[i
].length
= mtu
;
798 if (len
&& i
< BTUSB_MAX_ISOC_FRAMES
) {
799 urb
->iso_frame_desc
[i
].offset
= offset
;
800 urb
->iso_frame_desc
[i
].length
= len
;
804 urb
->number_of_packets
= i
;
807 static int btusb_submit_isoc_urb(struct hci_dev
*hdev
, gfp_t mem_flags
)
809 struct btusb_data
*data
= hci_get_drvdata(hdev
);
815 BT_DBG("%s", hdev
->name
);
817 if (!data
->isoc_rx_ep
)
820 urb
= usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES
, mem_flags
);
824 size
= le16_to_cpu(data
->isoc_rx_ep
->wMaxPacketSize
) *
825 BTUSB_MAX_ISOC_FRAMES
;
827 buf
= kmalloc(size
, mem_flags
);
833 pipe
= usb_rcvisocpipe(data
->udev
, data
->isoc_rx_ep
->bEndpointAddress
);
835 usb_fill_int_urb(urb
, data
->udev
, pipe
, buf
, size
, btusb_isoc_complete
,
836 hdev
, data
->isoc_rx_ep
->bInterval
);
838 urb
->transfer_flags
= URB_FREE_BUFFER
| URB_ISO_ASAP
;
840 __fill_isoc_descriptor(urb
, size
,
841 le16_to_cpu(data
->isoc_rx_ep
->wMaxPacketSize
));
843 usb_anchor_urb(urb
, &data
->isoc_anchor
);
845 err
= usb_submit_urb(urb
, mem_flags
);
847 if (err
!= -EPERM
&& err
!= -ENODEV
)
848 BT_ERR("%s urb %p submission failed (%d)",
849 hdev
->name
, urb
, -err
);
850 usb_unanchor_urb(urb
);
858 static void btusb_tx_complete(struct urb
*urb
)
860 struct sk_buff
*skb
= urb
->context
;
861 struct hci_dev
*hdev
= (struct hci_dev
*)skb
->dev
;
862 struct btusb_data
*data
= hci_get_drvdata(hdev
);
864 BT_DBG("%s urb %p status %d count %d", hdev
->name
, urb
, urb
->status
,
867 if (!test_bit(HCI_RUNNING
, &hdev
->flags
))
871 hdev
->stat
.byte_tx
+= urb
->transfer_buffer_length
;
876 spin_lock(&data
->txlock
);
877 data
->tx_in_flight
--;
878 spin_unlock(&data
->txlock
);
880 kfree(urb
->setup_packet
);
885 static void btusb_isoc_tx_complete(struct urb
*urb
)
887 struct sk_buff
*skb
= urb
->context
;
888 struct hci_dev
*hdev
= (struct hci_dev
*)skb
->dev
;
890 BT_DBG("%s urb %p status %d count %d", hdev
->name
, urb
, urb
->status
,
893 if (!test_bit(HCI_RUNNING
, &hdev
->flags
))
897 hdev
->stat
.byte_tx
+= urb
->transfer_buffer_length
;
902 kfree(urb
->setup_packet
);
907 static int btusb_open(struct hci_dev
*hdev
)
909 struct btusb_data
*data
= hci_get_drvdata(hdev
);
912 BT_DBG("%s", hdev
->name
);
914 /* Patching USB firmware files prior to starting any URBs of HCI path
915 * It is more safe to use USB bulk channel for downloading USB patch
917 if (data
->setup_on_usb
) {
918 err
= data
->setup_on_usb(hdev
);
923 err
= usb_autopm_get_interface(data
->intf
);
927 data
->intf
->needs_remote_wakeup
= 1;
929 if (test_and_set_bit(HCI_RUNNING
, &hdev
->flags
))
932 if (test_and_set_bit(BTUSB_INTR_RUNNING
, &data
->flags
))
935 err
= btusb_submit_intr_urb(hdev
, GFP_KERNEL
);
939 err
= btusb_submit_bulk_urb(hdev
, GFP_KERNEL
);
941 usb_kill_anchored_urbs(&data
->intr_anchor
);
945 set_bit(BTUSB_BULK_RUNNING
, &data
->flags
);
946 btusb_submit_bulk_urb(hdev
, GFP_KERNEL
);
949 usb_autopm_put_interface(data
->intf
);
953 clear_bit(BTUSB_INTR_RUNNING
, &data
->flags
);
954 clear_bit(HCI_RUNNING
, &hdev
->flags
);
955 usb_autopm_put_interface(data
->intf
);
959 static void btusb_stop_traffic(struct btusb_data
*data
)
961 usb_kill_anchored_urbs(&data
->intr_anchor
);
962 usb_kill_anchored_urbs(&data
->bulk_anchor
);
963 usb_kill_anchored_urbs(&data
->isoc_anchor
);
966 static int btusb_close(struct hci_dev
*hdev
)
968 struct btusb_data
*data
= hci_get_drvdata(hdev
);
971 BT_DBG("%s", hdev
->name
);
973 if (!test_and_clear_bit(HCI_RUNNING
, &hdev
->flags
))
976 cancel_work_sync(&data
->work
);
977 cancel_work_sync(&data
->waker
);
979 clear_bit(BTUSB_ISOC_RUNNING
, &data
->flags
);
980 clear_bit(BTUSB_BULK_RUNNING
, &data
->flags
);
981 clear_bit(BTUSB_INTR_RUNNING
, &data
->flags
);
983 btusb_stop_traffic(data
);
984 btusb_free_frags(data
);
986 err
= usb_autopm_get_interface(data
->intf
);
990 data
->intf
->needs_remote_wakeup
= 0;
991 usb_autopm_put_interface(data
->intf
);
994 usb_scuttle_anchored_urbs(&data
->deferred
);
998 static int btusb_flush(struct hci_dev
*hdev
)
1000 struct btusb_data
*data
= hci_get_drvdata(hdev
);
1002 BT_DBG("%s", hdev
->name
);
1004 usb_kill_anchored_urbs(&data
->tx_anchor
);
1005 btusb_free_frags(data
);
1010 static struct urb
*alloc_ctrl_urb(struct hci_dev
*hdev
, struct sk_buff
*skb
)
1012 struct btusb_data
*data
= hci_get_drvdata(hdev
);
1013 struct usb_ctrlrequest
*dr
;
1017 urb
= usb_alloc_urb(0, GFP_KERNEL
);
1019 return ERR_PTR(-ENOMEM
);
1021 dr
= kmalloc(sizeof(*dr
), GFP_KERNEL
);
1024 return ERR_PTR(-ENOMEM
);
1027 dr
->bRequestType
= data
->cmdreq_type
;
1028 dr
->bRequest
= data
->cmdreq
;
1031 dr
->wLength
= __cpu_to_le16(skb
->len
);
1033 pipe
= usb_sndctrlpipe(data
->udev
, 0x00);
1035 usb_fill_control_urb(urb
, data
->udev
, pipe
, (void *)dr
,
1036 skb
->data
, skb
->len
, btusb_tx_complete
, skb
);
1038 skb
->dev
= (void *)hdev
;
1043 static struct urb
*alloc_bulk_urb(struct hci_dev
*hdev
, struct sk_buff
*skb
)
1045 struct btusb_data
*data
= hci_get_drvdata(hdev
);
1049 if (!data
->bulk_tx_ep
)
1050 return ERR_PTR(-ENODEV
);
1052 urb
= usb_alloc_urb(0, GFP_KERNEL
);
1054 return ERR_PTR(-ENOMEM
);
1056 pipe
= usb_sndbulkpipe(data
->udev
, data
->bulk_tx_ep
->bEndpointAddress
);
1058 usb_fill_bulk_urb(urb
, data
->udev
, pipe
,
1059 skb
->data
, skb
->len
, btusb_tx_complete
, skb
);
1061 skb
->dev
= (void *)hdev
;
1066 static struct urb
*alloc_isoc_urb(struct hci_dev
*hdev
, struct sk_buff
*skb
)
1068 struct btusb_data
*data
= hci_get_drvdata(hdev
);
1072 if (!data
->isoc_tx_ep
)
1073 return ERR_PTR(-ENODEV
);
1075 urb
= usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES
, GFP_KERNEL
);
1077 return ERR_PTR(-ENOMEM
);
1079 pipe
= usb_sndisocpipe(data
->udev
, data
->isoc_tx_ep
->bEndpointAddress
);
1081 usb_fill_int_urb(urb
, data
->udev
, pipe
,
1082 skb
->data
, skb
->len
, btusb_isoc_tx_complete
,
1083 skb
, data
->isoc_tx_ep
->bInterval
);
1085 urb
->transfer_flags
= URB_ISO_ASAP
;
1087 __fill_isoc_descriptor(urb
, skb
->len
,
1088 le16_to_cpu(data
->isoc_tx_ep
->wMaxPacketSize
));
1090 skb
->dev
= (void *)hdev
;
1095 static int submit_tx_urb(struct hci_dev
*hdev
, struct urb
*urb
)
1097 struct btusb_data
*data
= hci_get_drvdata(hdev
);
1100 usb_anchor_urb(urb
, &data
->tx_anchor
);
1102 err
= usb_submit_urb(urb
, GFP_KERNEL
);
1104 if (err
!= -EPERM
&& err
!= -ENODEV
)
1105 BT_ERR("%s urb %p submission failed (%d)",
1106 hdev
->name
, urb
, -err
);
1107 kfree(urb
->setup_packet
);
1108 usb_unanchor_urb(urb
);
1110 usb_mark_last_busy(data
->udev
);
1117 static int submit_or_queue_tx_urb(struct hci_dev
*hdev
, struct urb
*urb
)
1119 struct btusb_data
*data
= hci_get_drvdata(hdev
);
1120 unsigned long flags
;
1123 spin_lock_irqsave(&data
->txlock
, flags
);
1124 suspending
= test_bit(BTUSB_SUSPENDING
, &data
->flags
);
1126 data
->tx_in_flight
++;
1127 spin_unlock_irqrestore(&data
->txlock
, flags
);
1130 return submit_tx_urb(hdev
, urb
);
1132 usb_anchor_urb(urb
, &data
->deferred
);
1133 schedule_work(&data
->waker
);
1139 static int btusb_send_frame(struct hci_dev
*hdev
, struct sk_buff
*skb
)
1143 BT_DBG("%s", hdev
->name
);
1145 if (!test_bit(HCI_RUNNING
, &hdev
->flags
))
1148 switch (bt_cb(skb
)->pkt_type
) {
1149 case HCI_COMMAND_PKT
:
1150 urb
= alloc_ctrl_urb(hdev
, skb
);
1152 return PTR_ERR(urb
);
1154 hdev
->stat
.cmd_tx
++;
1155 return submit_or_queue_tx_urb(hdev
, urb
);
1157 case HCI_ACLDATA_PKT
:
1158 urb
= alloc_bulk_urb(hdev
, skb
);
1160 return PTR_ERR(urb
);
1162 hdev
->stat
.acl_tx
++;
1163 return submit_or_queue_tx_urb(hdev
, urb
);
1165 case HCI_SCODATA_PKT
:
1166 if (hci_conn_num(hdev
, SCO_LINK
) < 1)
1169 urb
= alloc_isoc_urb(hdev
, skb
);
1171 return PTR_ERR(urb
);
1173 hdev
->stat
.sco_tx
++;
1174 return submit_tx_urb(hdev
, urb
);
1180 static void btusb_notify(struct hci_dev
*hdev
, unsigned int evt
)
1182 struct btusb_data
*data
= hci_get_drvdata(hdev
);
1184 BT_DBG("%s evt %d", hdev
->name
, evt
);
1186 if (hci_conn_num(hdev
, SCO_LINK
) != data
->sco_num
) {
1187 data
->sco_num
= hci_conn_num(hdev
, SCO_LINK
);
1188 schedule_work(&data
->work
);
1192 static inline int __set_isoc_interface(struct hci_dev
*hdev
, int altsetting
)
1194 struct btusb_data
*data
= hci_get_drvdata(hdev
);
1195 struct usb_interface
*intf
= data
->isoc
;
1196 struct usb_endpoint_descriptor
*ep_desc
;
1202 err
= usb_set_interface(data
->udev
, 1, altsetting
);
1204 BT_ERR("%s setting interface failed (%d)", hdev
->name
, -err
);
1208 data
->isoc_altsetting
= altsetting
;
1210 data
->isoc_tx_ep
= NULL
;
1211 data
->isoc_rx_ep
= NULL
;
1213 for (i
= 0; i
< intf
->cur_altsetting
->desc
.bNumEndpoints
; i
++) {
1214 ep_desc
= &intf
->cur_altsetting
->endpoint
[i
].desc
;
1216 if (!data
->isoc_tx_ep
&& usb_endpoint_is_isoc_out(ep_desc
)) {
1217 data
->isoc_tx_ep
= ep_desc
;
1221 if (!data
->isoc_rx_ep
&& usb_endpoint_is_isoc_in(ep_desc
)) {
1222 data
->isoc_rx_ep
= ep_desc
;
1227 if (!data
->isoc_tx_ep
|| !data
->isoc_rx_ep
) {
1228 BT_ERR("%s invalid SCO descriptors", hdev
->name
);
1235 static void btusb_work(struct work_struct
*work
)
1237 struct btusb_data
*data
= container_of(work
, struct btusb_data
, work
);
1238 struct hci_dev
*hdev
= data
->hdev
;
1242 if (data
->sco_num
> 0) {
1243 if (!test_bit(BTUSB_DID_ISO_RESUME
, &data
->flags
)) {
1244 err
= usb_autopm_get_interface(data
->isoc
? data
->isoc
: data
->intf
);
1246 clear_bit(BTUSB_ISOC_RUNNING
, &data
->flags
);
1247 usb_kill_anchored_urbs(&data
->isoc_anchor
);
1251 set_bit(BTUSB_DID_ISO_RESUME
, &data
->flags
);
1254 if (hdev
->voice_setting
& 0x0020) {
1255 static const int alts
[3] = { 2, 4, 5 };
1257 new_alts
= alts
[data
->sco_num
- 1];
1259 new_alts
= data
->sco_num
;
1262 if (data
->isoc_altsetting
!= new_alts
) {
1263 clear_bit(BTUSB_ISOC_RUNNING
, &data
->flags
);
1264 usb_kill_anchored_urbs(&data
->isoc_anchor
);
1266 if (__set_isoc_interface(hdev
, new_alts
) < 0)
1270 if (!test_and_set_bit(BTUSB_ISOC_RUNNING
, &data
->flags
)) {
1271 if (btusb_submit_isoc_urb(hdev
, GFP_KERNEL
) < 0)
1272 clear_bit(BTUSB_ISOC_RUNNING
, &data
->flags
);
1274 btusb_submit_isoc_urb(hdev
, GFP_KERNEL
);
1277 clear_bit(BTUSB_ISOC_RUNNING
, &data
->flags
);
1278 usb_kill_anchored_urbs(&data
->isoc_anchor
);
1280 __set_isoc_interface(hdev
, 0);
1281 if (test_and_clear_bit(BTUSB_DID_ISO_RESUME
, &data
->flags
))
1282 usb_autopm_put_interface(data
->isoc
? data
->isoc
: data
->intf
);
1286 static void btusb_waker(struct work_struct
*work
)
1288 struct btusb_data
*data
= container_of(work
, struct btusb_data
, waker
);
1291 err
= usb_autopm_get_interface(data
->intf
);
1295 usb_autopm_put_interface(data
->intf
);
1298 static struct sk_buff
*btusb_read_local_version(struct hci_dev
*hdev
)
1300 struct sk_buff
*skb
;
1302 skb
= __hci_cmd_sync(hdev
, HCI_OP_READ_LOCAL_VERSION
, 0, NULL
,
1305 BT_ERR("%s: HCI_OP_READ_LOCAL_VERSION failed (%ld)",
1306 hdev
->name
, PTR_ERR(skb
));
1310 if (skb
->len
!= sizeof(struct hci_rp_read_local_version
)) {
1311 BT_ERR("%s: HCI_OP_READ_LOCAL_VERSION event length mismatch",
1314 return ERR_PTR(-EIO
);
1320 static int btusb_setup_bcm92035(struct hci_dev
*hdev
)
1322 struct sk_buff
*skb
;
1325 BT_DBG("%s", hdev
->name
);
1327 skb
= __hci_cmd_sync(hdev
, 0xfc3b, 1, &val
, HCI_INIT_TIMEOUT
);
1329 BT_ERR("BCM92035 command failed (%ld)", -PTR_ERR(skb
));
1336 static int btusb_setup_csr(struct hci_dev
*hdev
)
1338 struct hci_rp_read_local_version
*rp
;
1339 struct sk_buff
*skb
;
1342 BT_DBG("%s", hdev
->name
);
1344 skb
= btusb_read_local_version(hdev
);
1346 return -PTR_ERR(skb
);
1348 rp
= (struct hci_rp_read_local_version
*)skb
->data
;
1351 if (le16_to_cpu(rp
->manufacturer
) != 10) {
1352 /* Clear the reset quirk since this is not an actual
1353 * early Bluetooth 1.1 device from CSR.
1355 clear_bit(HCI_QUIRK_RESET_ON_CLOSE
, &hdev
->quirks
);
1357 /* These fake CSR controllers have all a broken
1358 * stored link key handling and so just disable it.
1360 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY
,
1365 ret
= -bt_to_errno(rp
->status
);
1372 #define RTL_FRAG_LEN 252
1374 struct rtl_download_cmd
{
1376 __u8 data
[RTL_FRAG_LEN
];
1379 struct rtl_download_response
{
1384 struct rtl_rom_version_evt
{
1389 struct rtl_epatch_header
{
1395 #define RTL_EPATCH_SIGNATURE "Realtech"
1396 #define RTL_ROM_LMP_3499 0x3499
1397 #define RTL_ROM_LMP_8723A 0x1200
1398 #define RTL_ROM_LMP_8723B 0x8723
1399 #define RTL_ROM_LMP_8821A 0x8821
1400 #define RTL_ROM_LMP_8761A 0x8761
1402 static int rtl_read_rom_version(struct hci_dev
*hdev
, u8
*version
)
1404 struct rtl_rom_version_evt
*rom_version
;
1405 struct sk_buff
*skb
;
1408 /* Read RTL ROM version command */
1409 skb
= __hci_cmd_sync(hdev
, 0xfc6d, 0, NULL
, HCI_INIT_TIMEOUT
);
1411 BT_ERR("%s: Read ROM version failed (%ld)",
1412 hdev
->name
, PTR_ERR(skb
));
1413 return PTR_ERR(skb
);
1416 if (skb
->len
!= sizeof(*rom_version
)) {
1417 BT_ERR("%s: RTL version event length mismatch", hdev
->name
);
1422 rom_version
= (struct rtl_rom_version_evt
*)skb
->data
;
1423 BT_INFO("%s: rom_version status=%x version=%x",
1424 hdev
->name
, rom_version
->status
, rom_version
->version
);
1426 ret
= rom_version
->status
;
1428 *version
= rom_version
->version
;
1434 static int rtl8723b_parse_firmware(struct hci_dev
*hdev
, u16 lmp_subver
,
1435 const struct firmware
*fw
,
1436 unsigned char **_buf
)
1438 const u8 extension_sig
[] = { 0x51, 0x04, 0xfd, 0x77 };
1439 struct rtl_epatch_header
*epatch_info
;
1443 u8 opcode
, length
, data
, rom_version
= 0;
1444 int project_id
= -1;
1445 const unsigned char *fwptr
, *chip_id_base
;
1446 const unsigned char *patch_length_base
, *patch_offset_base
;
1447 u32 patch_offset
= 0;
1448 u16 patch_length
, num_patches
;
1449 const u16 project_id_to_lmp_subver
[] = {
1456 ret
= rtl_read_rom_version(hdev
, &rom_version
);
1458 return -bt_to_errno(ret
);
1460 min_size
= sizeof(struct rtl_epatch_header
) + sizeof(extension_sig
) + 3;
1461 if (fw
->size
< min_size
)
1464 fwptr
= fw
->data
+ fw
->size
- sizeof(extension_sig
);
1465 if (memcmp(fwptr
, extension_sig
, sizeof(extension_sig
)) != 0) {
1466 BT_ERR("%s: extension section signature mismatch", hdev
->name
);
1470 /* Loop from the end of the firmware parsing instructions, until
1471 * we find an instruction that identifies the "project ID" for the
1472 * hardware supported by this firwmare file.
1473 * Once we have that, we double-check that that project_id is suitable
1474 * for the hardware we are working with.
1476 while (fwptr
>= fw
->data
+ (sizeof(struct rtl_epatch_header
) + 3)) {
1481 BT_DBG("check op=%x len=%x data=%x", opcode
, length
, data
);
1483 if (opcode
== 0xff) /* EOF */
1487 BT_ERR("%s: found instruction with length 0",
1492 if (opcode
== 0 && length
== 1) {
1500 if (project_id
< 0) {
1501 BT_ERR("%s: failed to find version instruction", hdev
->name
);
1505 if (project_id
>= ARRAY_SIZE(project_id_to_lmp_subver
)) {
1506 BT_ERR("%s: unknown project id %d", hdev
->name
, project_id
);
1510 if (lmp_subver
!= project_id_to_lmp_subver
[project_id
]) {
1511 BT_ERR("%s: firmware is for %x but this is a %x", hdev
->name
,
1512 project_id_to_lmp_subver
[project_id
], lmp_subver
);
1516 epatch_info
= (struct rtl_epatch_header
*)fw
->data
;
1517 if (memcmp(epatch_info
->signature
, RTL_EPATCH_SIGNATURE
, 8) != 0) {
1518 BT_ERR("%s: bad EPATCH signature", hdev
->name
);
1522 num_patches
= le16_to_cpu(epatch_info
->num_patches
);
1523 BT_DBG("fw_version=%x, num_patches=%d",
1524 le32_to_cpu(epatch_info
->fw_version
), num_patches
);
1526 /* After the rtl_epatch_header there is a funky patch metadata section.
1527 * Assuming 2 patches, the layout is:
1528 * ChipID1 ChipID2 PatchLength1 PatchLength2 PatchOffset1 PatchOffset2
1530 * Find the right patch for this chip.
1532 min_size
+= 8 * num_patches
;
1533 if (fw
->size
< min_size
)
1536 chip_id_base
= fw
->data
+ sizeof(struct rtl_epatch_header
);
1537 patch_length_base
= chip_id_base
+ (sizeof(u16
) * num_patches
);
1538 patch_offset_base
= patch_length_base
+ (sizeof(u16
) * num_patches
);
1539 for (i
= 0; i
< num_patches
; i
++) {
1540 u16 chip_id
= get_unaligned_le16(chip_id_base
+
1542 if (chip_id
== rom_version
+ 1) {
1543 patch_length
= get_unaligned_le16(patch_length_base
+
1545 patch_offset
= get_unaligned_le32(patch_offset_base
+
1551 if (!patch_offset
) {
1552 BT_ERR("%s: didn't find patch for chip id %d",
1553 hdev
->name
, rom_version
);
1557 BT_DBG("length=%x offset=%x index %d", patch_length
, patch_offset
, i
);
1558 min_size
= patch_offset
+ patch_length
;
1559 if (fw
->size
< min_size
)
1562 /* Copy the firmware into a new buffer and write the version at
1566 buf
= kmemdup(fw
->data
+ patch_offset
, patch_length
, GFP_KERNEL
);
1570 memcpy(buf
+ patch_length
- 4, &epatch_info
->fw_version
, 4);
1576 static int rtl_download_firmware(struct hci_dev
*hdev
,
1577 const unsigned char *data
, int fw_len
)
1579 struct rtl_download_cmd
*dl_cmd
;
1580 int frag_num
= fw_len
/ RTL_FRAG_LEN
+ 1;
1581 int frag_len
= RTL_FRAG_LEN
;
1585 dl_cmd
= kmalloc(sizeof(struct rtl_download_cmd
), GFP_KERNEL
);
1589 for (i
= 0; i
< frag_num
; i
++) {
1590 struct rtl_download_response
*dl_resp
;
1591 struct sk_buff
*skb
;
1593 BT_DBG("download fw (%d/%d)", i
, frag_num
);
1596 if (i
== (frag_num
- 1)) {
1597 dl_cmd
->index
|= 0x80; /* data end */
1598 frag_len
= fw_len
% RTL_FRAG_LEN
;
1600 memcpy(dl_cmd
->data
, data
, frag_len
);
1602 /* Send download command */
1603 skb
= __hci_cmd_sync(hdev
, 0xfc20, frag_len
+ 1, dl_cmd
,
1606 BT_ERR("%s: download fw command failed (%ld)",
1607 hdev
->name
, PTR_ERR(skb
));
1608 ret
= -PTR_ERR(skb
);
1612 if (skb
->len
!= sizeof(*dl_resp
)) {
1613 BT_ERR("%s: download fw event length mismatch",
1620 dl_resp
= (struct rtl_download_response
*)skb
->data
;
1621 if (dl_resp
->status
!= 0) {
1623 ret
= bt_to_errno(dl_resp
->status
);
1628 data
+= RTL_FRAG_LEN
;
1636 static int btusb_setup_rtl8723a(struct hci_dev
*hdev
)
1638 struct btusb_data
*data
= dev_get_drvdata(&hdev
->dev
);
1639 struct usb_device
*udev
= interface_to_usbdev(data
->intf
);
1640 const struct firmware
*fw
;
1643 BT_INFO("%s: rtl: loading rtl_bt/rtl8723a_fw.bin", hdev
->name
);
1644 ret
= request_firmware(&fw
, "rtl_bt/rtl8723a_fw.bin", &udev
->dev
);
1646 BT_ERR("%s: Failed to load rtl_bt/rtl8723a_fw.bin", hdev
->name
);
1655 /* Check that the firmware doesn't have the epatch signature
1656 * (which is only for RTL8723B and newer).
1658 if (!memcmp(fw
->data
, RTL_EPATCH_SIGNATURE
, 8)) {
1659 BT_ERR("%s: unexpected EPATCH signature!", hdev
->name
);
1664 ret
= rtl_download_firmware(hdev
, fw
->data
, fw
->size
);
1667 release_firmware(fw
);
1671 static int btusb_setup_rtl8723b(struct hci_dev
*hdev
, u16 lmp_subver
,
1672 const char *fw_name
)
1674 struct btusb_data
*data
= dev_get_drvdata(&hdev
->dev
);
1675 struct usb_device
*udev
= interface_to_usbdev(data
->intf
);
1676 unsigned char *fw_data
= NULL
;
1677 const struct firmware
*fw
;
1680 BT_INFO("%s: rtl: loading %s", hdev
->name
, fw_name
);
1681 ret
= request_firmware(&fw
, fw_name
, &udev
->dev
);
1683 BT_ERR("%s: Failed to load %s", hdev
->name
, fw_name
);
1687 ret
= rtl8723b_parse_firmware(hdev
, lmp_subver
, fw
, &fw_data
);
1691 ret
= rtl_download_firmware(hdev
, fw_data
, ret
);
1697 release_firmware(fw
);
1701 static int btusb_setup_realtek(struct hci_dev
*hdev
)
1703 struct sk_buff
*skb
;
1704 struct hci_rp_read_local_version
*resp
;
1707 skb
= btusb_read_local_version(hdev
);
1709 return -PTR_ERR(skb
);
1711 resp
= (struct hci_rp_read_local_version
*)skb
->data
;
1712 BT_INFO("%s: rtl: examining hci_ver=%02x hci_rev=%04x lmp_ver=%02x "
1713 "lmp_subver=%04x", hdev
->name
, resp
->hci_ver
, resp
->hci_rev
,
1714 resp
->lmp_ver
, resp
->lmp_subver
);
1716 lmp_subver
= le16_to_cpu(resp
->lmp_subver
);
1719 /* Match a set of subver values that correspond to stock firmware,
1720 * which is not compatible with standard btusb.
1721 * If matched, upload an alternative firmware that does conform to
1722 * standard btusb. Once that firmware is uploaded, the subver changes
1723 * to a different value.
1725 switch (lmp_subver
) {
1726 case RTL_ROM_LMP_8723A
:
1727 case RTL_ROM_LMP_3499
:
1728 return btusb_setup_rtl8723a(hdev
);
1729 case RTL_ROM_LMP_8723B
:
1730 return btusb_setup_rtl8723b(hdev
, lmp_subver
,
1731 "rtl_bt/rtl8723b_fw.bin");
1732 case RTL_ROM_LMP_8821A
:
1733 return btusb_setup_rtl8723b(hdev
, lmp_subver
,
1734 "rtl_bt/rtl8821a_fw.bin");
1735 case RTL_ROM_LMP_8761A
:
1736 return btusb_setup_rtl8723b(hdev
, lmp_subver
,
1737 "rtl_bt/rtl8761a_fw.bin");
1739 BT_INFO("rtl: assuming no firmware upload needed.");
1744 static const struct firmware
*btusb_setup_intel_get_fw(struct hci_dev
*hdev
,
1745 struct intel_version
*ver
)
1747 const struct firmware
*fw
;
1751 snprintf(fwname
, sizeof(fwname
),
1752 "intel/ibt-hw-%x.%x.%x-fw-%x.%x.%x.%x.%x.bseq",
1753 ver
->hw_platform
, ver
->hw_variant
, ver
->hw_revision
,
1754 ver
->fw_variant
, ver
->fw_revision
, ver
->fw_build_num
,
1755 ver
->fw_build_ww
, ver
->fw_build_yy
);
1757 ret
= request_firmware(&fw
, fwname
, &hdev
->dev
);
1759 if (ret
== -EINVAL
) {
1760 BT_ERR("%s Intel firmware file request failed (%d)",
1765 BT_ERR("%s failed to open Intel firmware file: %s(%d)",
1766 hdev
->name
, fwname
, ret
);
1768 /* If the correct firmware patch file is not found, use the
1769 * default firmware patch file instead
1771 snprintf(fwname
, sizeof(fwname
), "intel/ibt-hw-%x.%x.bseq",
1772 ver
->hw_platform
, ver
->hw_variant
);
1773 if (request_firmware(&fw
, fwname
, &hdev
->dev
) < 0) {
1774 BT_ERR("%s failed to open default Intel fw file: %s",
1775 hdev
->name
, fwname
);
1780 BT_INFO("%s: Intel Bluetooth firmware file: %s", hdev
->name
, fwname
);
1785 static int btusb_setup_intel_patching(struct hci_dev
*hdev
,
1786 const struct firmware
*fw
,
1787 const u8
**fw_ptr
, int *disable_patch
)
1789 struct sk_buff
*skb
;
1790 struct hci_command_hdr
*cmd
;
1791 const u8
*cmd_param
;
1792 struct hci_event_hdr
*evt
= NULL
;
1793 const u8
*evt_param
= NULL
;
1794 int remain
= fw
->size
- (*fw_ptr
- fw
->data
);
1796 /* The first byte indicates the types of the patch command or event.
1797 * 0x01 means HCI command and 0x02 is HCI event. If the first bytes
1798 * in the current firmware buffer doesn't start with 0x01 or
1799 * the size of remain buffer is smaller than HCI command header,
1800 * the firmware file is corrupted and it should stop the patching
1803 if (remain
> HCI_COMMAND_HDR_SIZE
&& *fw_ptr
[0] != 0x01) {
1804 BT_ERR("%s Intel fw corrupted: invalid cmd read", hdev
->name
);
1810 cmd
= (struct hci_command_hdr
*)(*fw_ptr
);
1811 *fw_ptr
+= sizeof(*cmd
);
1812 remain
-= sizeof(*cmd
);
1814 /* Ensure that the remain firmware data is long enough than the length
1815 * of command parameter. If not, the firmware file is corrupted.
1817 if (remain
< cmd
->plen
) {
1818 BT_ERR("%s Intel fw corrupted: invalid cmd len", hdev
->name
);
1822 /* If there is a command that loads a patch in the firmware
1823 * file, then enable the patch upon success, otherwise just
1824 * disable the manufacturer mode, for example patch activation
1825 * is not required when the default firmware patch file is used
1826 * because there are no patch data to load.
1828 if (*disable_patch
&& le16_to_cpu(cmd
->opcode
) == 0xfc8e)
1831 cmd_param
= *fw_ptr
;
1832 *fw_ptr
+= cmd
->plen
;
1833 remain
-= cmd
->plen
;
1835 /* This reads the expected events when the above command is sent to the
1836 * device. Some vendor commands expects more than one events, for
1837 * example command status event followed by vendor specific event.
1838 * For this case, it only keeps the last expected event. so the command
1839 * can be sent with __hci_cmd_sync_ev() which returns the sk_buff of
1840 * last expected event.
1842 while (remain
> HCI_EVENT_HDR_SIZE
&& *fw_ptr
[0] == 0x02) {
1846 evt
= (struct hci_event_hdr
*)(*fw_ptr
);
1847 *fw_ptr
+= sizeof(*evt
);
1848 remain
-= sizeof(*evt
);
1850 if (remain
< evt
->plen
) {
1851 BT_ERR("%s Intel fw corrupted: invalid evt len",
1856 evt_param
= *fw_ptr
;
1857 *fw_ptr
+= evt
->plen
;
1858 remain
-= evt
->plen
;
1861 /* Every HCI commands in the firmware file has its correspond event.
1862 * If event is not found or remain is smaller than zero, the firmware
1863 * file is corrupted.
1865 if (!evt
|| !evt_param
|| remain
< 0) {
1866 BT_ERR("%s Intel fw corrupted: invalid evt read", hdev
->name
);
1870 skb
= __hci_cmd_sync_ev(hdev
, le16_to_cpu(cmd
->opcode
), cmd
->plen
,
1871 cmd_param
, evt
->evt
, HCI_INIT_TIMEOUT
);
1873 BT_ERR("%s sending Intel patch command (0x%4.4x) failed (%ld)",
1874 hdev
->name
, cmd
->opcode
, PTR_ERR(skb
));
1875 return PTR_ERR(skb
);
1878 /* It ensures that the returned event matches the event data read from
1879 * the firmware file. At fist, it checks the length and then
1880 * the contents of the event.
1882 if (skb
->len
!= evt
->plen
) {
1883 BT_ERR("%s mismatch event length (opcode 0x%4.4x)", hdev
->name
,
1884 le16_to_cpu(cmd
->opcode
));
1889 if (memcmp(skb
->data
, evt_param
, evt
->plen
)) {
1890 BT_ERR("%s mismatch event parameter (opcode 0x%4.4x)",
1891 hdev
->name
, le16_to_cpu(cmd
->opcode
));
1900 static int btusb_setup_intel(struct hci_dev
*hdev
)
1902 struct sk_buff
*skb
;
1903 const struct firmware
*fw
;
1906 struct intel_version
*ver
;
1908 const u8 mfg_enable
[] = { 0x01, 0x00 };
1909 const u8 mfg_disable
[] = { 0x00, 0x00 };
1910 const u8 mfg_reset_deactivate
[] = { 0x00, 0x01 };
1911 const u8 mfg_reset_activate
[] = { 0x00, 0x02 };
1913 BT_DBG("%s", hdev
->name
);
1915 /* The controller has a bug with the first HCI command sent to it
1916 * returning number of completed commands as zero. This would stall the
1917 * command processing in the Bluetooth core.
1919 * As a workaround, send HCI Reset command first which will reset the
1920 * number of completed commands and allow normal command processing
1923 skb
= __hci_cmd_sync(hdev
, HCI_OP_RESET
, 0, NULL
, HCI_INIT_TIMEOUT
);
1925 BT_ERR("%s sending initial HCI reset command failed (%ld)",
1926 hdev
->name
, PTR_ERR(skb
));
1927 return PTR_ERR(skb
);
1931 /* Read Intel specific controller version first to allow selection of
1932 * which firmware file to load.
1934 * The returned information are hardware variant and revision plus
1935 * firmware variant, revision and build number.
1937 skb
= __hci_cmd_sync(hdev
, 0xfc05, 0, NULL
, HCI_INIT_TIMEOUT
);
1939 BT_ERR("%s reading Intel fw version command failed (%ld)",
1940 hdev
->name
, PTR_ERR(skb
));
1941 return PTR_ERR(skb
);
1944 if (skb
->len
!= sizeof(*ver
)) {
1945 BT_ERR("%s Intel version event length mismatch", hdev
->name
);
1950 ver
= (struct intel_version
*)skb
->data
;
1952 BT_ERR("%s Intel fw version event failed (%02x)", hdev
->name
,
1955 return -bt_to_errno(ver
->status
);
1958 BT_INFO("%s: read Intel version: %02x%02x%02x%02x%02x%02x%02x%02x%02x",
1959 hdev
->name
, ver
->hw_platform
, ver
->hw_variant
,
1960 ver
->hw_revision
, ver
->fw_variant
, ver
->fw_revision
,
1961 ver
->fw_build_num
, ver
->fw_build_ww
, ver
->fw_build_yy
,
1964 /* fw_patch_num indicates the version of patch the device currently
1965 * have. If there is no patch data in the device, it is always 0x00.
1966 * So, if it is other than 0x00, no need to patch the deivce again.
1968 if (ver
->fw_patch_num
) {
1969 BT_INFO("%s: Intel device is already patched. patch num: %02x",
1970 hdev
->name
, ver
->fw_patch_num
);
1972 btintel_check_bdaddr(hdev
);
1976 /* Opens the firmware patch file based on the firmware version read
1977 * from the controller. If it fails to open the matching firmware
1978 * patch file, it tries to open the default firmware patch file.
1979 * If no patch file is found, allow the device to operate without
1982 fw
= btusb_setup_intel_get_fw(hdev
, ver
);
1985 btintel_check_bdaddr(hdev
);
1990 /* This Intel specific command enables the manufacturer mode of the
1993 * Only while this mode is enabled, the driver can download the
1994 * firmware patch data and configuration parameters.
1996 skb
= __hci_cmd_sync(hdev
, 0xfc11, 2, mfg_enable
, HCI_INIT_TIMEOUT
);
1998 BT_ERR("%s entering Intel manufacturer mode failed (%ld)",
1999 hdev
->name
, PTR_ERR(skb
));
2000 release_firmware(fw
);
2001 return PTR_ERR(skb
);
2005 u8 evt_status
= skb
->data
[0];
2007 BT_ERR("%s enable Intel manufacturer mode event failed (%02x)",
2008 hdev
->name
, evt_status
);
2010 release_firmware(fw
);
2011 return -bt_to_errno(evt_status
);
2017 /* The firmware data file consists of list of Intel specific HCI
2018 * commands and its expected events. The first byte indicates the
2019 * type of the message, either HCI command or HCI event.
2021 * It reads the command and its expected event from the firmware file,
2022 * and send to the controller. Once __hci_cmd_sync_ev() returns,
2023 * the returned event is compared with the event read from the firmware
2024 * file and it will continue until all the messages are downloaded to
2027 * Once the firmware patching is completed successfully,
2028 * the manufacturer mode is disabled with reset and activating the
2031 * If the firmware patching fails, the manufacturer mode is
2032 * disabled with reset and deactivating the patch.
2034 * If the default patch file is used, no reset is done when disabling
2037 while (fw
->size
> fw_ptr
- fw
->data
) {
2040 ret
= btusb_setup_intel_patching(hdev
, fw
, &fw_ptr
,
2043 goto exit_mfg_deactivate
;
2046 release_firmware(fw
);
2049 goto exit_mfg_disable
;
2051 /* Patching completed successfully and disable the manufacturer mode
2052 * with reset and activate the downloaded firmware patches.
2054 skb
= __hci_cmd_sync(hdev
, 0xfc11, sizeof(mfg_reset_activate
),
2055 mfg_reset_activate
, HCI_INIT_TIMEOUT
);
2057 BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
2058 hdev
->name
, PTR_ERR(skb
));
2059 return PTR_ERR(skb
);
2063 BT_INFO("%s: Intel Bluetooth firmware patch completed and activated",
2066 btintel_check_bdaddr(hdev
);
2070 /* Disable the manufacturer mode without reset */
2071 skb
= __hci_cmd_sync(hdev
, 0xfc11, sizeof(mfg_disable
), mfg_disable
,
2074 BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
2075 hdev
->name
, PTR_ERR(skb
));
2076 return PTR_ERR(skb
);
2080 BT_INFO("%s: Intel Bluetooth firmware patch completed", hdev
->name
);
2082 btintel_check_bdaddr(hdev
);
2085 exit_mfg_deactivate
:
2086 release_firmware(fw
);
2088 /* Patching failed. Disable the manufacturer mode with reset and
2089 * deactivate the downloaded firmware patches.
2091 skb
= __hci_cmd_sync(hdev
, 0xfc11, sizeof(mfg_reset_deactivate
),
2092 mfg_reset_deactivate
, HCI_INIT_TIMEOUT
);
2094 BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
2095 hdev
->name
, PTR_ERR(skb
));
2096 return PTR_ERR(skb
);
2100 BT_INFO("%s: Intel Bluetooth firmware patch completed and deactivated",
2103 btintel_check_bdaddr(hdev
);
2107 static int inject_cmd_complete(struct hci_dev
*hdev
, __u16 opcode
)
2109 struct sk_buff
*skb
;
2110 struct hci_event_hdr
*hdr
;
2111 struct hci_ev_cmd_complete
*evt
;
2113 skb
= bt_skb_alloc(sizeof(*hdr
) + sizeof(*evt
) + 1, GFP_ATOMIC
);
2117 hdr
= (struct hci_event_hdr
*)skb_put(skb
, sizeof(*hdr
));
2118 hdr
->evt
= HCI_EV_CMD_COMPLETE
;
2119 hdr
->plen
= sizeof(*evt
) + 1;
2121 evt
= (struct hci_ev_cmd_complete
*)skb_put(skb
, sizeof(*evt
));
2123 evt
->opcode
= cpu_to_le16(opcode
);
2125 *skb_put(skb
, 1) = 0x00;
2127 bt_cb(skb
)->pkt_type
= HCI_EVENT_PKT
;
2129 return hci_recv_frame(hdev
, skb
);
2132 static int btusb_recv_bulk_intel(struct btusb_data
*data
, void *buffer
,
2135 /* When the device is in bootloader mode, then it can send
2136 * events via the bulk endpoint. These events are treated the
2137 * same way as the ones received from the interrupt endpoint.
2139 if (test_bit(BTUSB_BOOTLOADER
, &data
->flags
))
2140 return btusb_recv_intr(data
, buffer
, count
);
2142 return btusb_recv_bulk(data
, buffer
, count
);
2145 static void btusb_intel_bootup(struct btusb_data
*data
, const void *ptr
,
2148 const struct intel_bootup
*evt
= ptr
;
2150 if (len
!= sizeof(*evt
))
2153 if (test_and_clear_bit(BTUSB_BOOTING
, &data
->flags
)) {
2154 smp_mb__after_atomic();
2155 wake_up_bit(&data
->flags
, BTUSB_BOOTING
);
2159 static void btusb_intel_secure_send_result(struct btusb_data
*data
,
2160 const void *ptr
, unsigned int len
)
2162 const struct intel_secure_send_result
*evt
= ptr
;
2164 if (len
!= sizeof(*evt
))
2168 set_bit(BTUSB_FIRMWARE_FAILED
, &data
->flags
);
2170 if (test_and_clear_bit(BTUSB_DOWNLOADING
, &data
->flags
) &&
2171 test_bit(BTUSB_FIRMWARE_LOADED
, &data
->flags
)) {
2172 smp_mb__after_atomic();
2173 wake_up_bit(&data
->flags
, BTUSB_DOWNLOADING
);
2177 static int btusb_recv_event_intel(struct hci_dev
*hdev
, struct sk_buff
*skb
)
2179 struct btusb_data
*data
= hci_get_drvdata(hdev
);
2181 if (test_bit(BTUSB_BOOTLOADER
, &data
->flags
)) {
2182 struct hci_event_hdr
*hdr
= (void *)skb
->data
;
2184 if (skb
->len
> HCI_EVENT_HDR_SIZE
&& hdr
->evt
== 0xff &&
2186 const void *ptr
= skb
->data
+ HCI_EVENT_HDR_SIZE
+ 1;
2187 unsigned int len
= skb
->len
- HCI_EVENT_HDR_SIZE
- 1;
2189 switch (skb
->data
[2]) {
2191 /* When switching to the operational firmware
2192 * the device sends a vendor specific event
2193 * indicating that the bootup completed.
2195 btusb_intel_bootup(data
, ptr
, len
);
2198 /* When the firmware loading completes the
2199 * device sends out a vendor specific event
2200 * indicating the result of the firmware
2203 btusb_intel_secure_send_result(data
, ptr
, len
);
2209 return hci_recv_frame(hdev
, skb
);
2212 static int btusb_send_frame_intel(struct hci_dev
*hdev
, struct sk_buff
*skb
)
2214 struct btusb_data
*data
= hci_get_drvdata(hdev
);
2217 BT_DBG("%s", hdev
->name
);
2219 if (!test_bit(HCI_RUNNING
, &hdev
->flags
))
2222 switch (bt_cb(skb
)->pkt_type
) {
2223 case HCI_COMMAND_PKT
:
2224 if (test_bit(BTUSB_BOOTLOADER
, &data
->flags
)) {
2225 struct hci_command_hdr
*cmd
= (void *)skb
->data
;
2226 __u16 opcode
= le16_to_cpu(cmd
->opcode
);
2228 /* When in bootloader mode and the command 0xfc09
2229 * is received, it needs to be send down the
2230 * bulk endpoint. So allocate a bulk URB instead.
2232 if (opcode
== 0xfc09)
2233 urb
= alloc_bulk_urb(hdev
, skb
);
2235 urb
= alloc_ctrl_urb(hdev
, skb
);
2237 /* When the 0xfc01 command is issued to boot into
2238 * the operational firmware, it will actually not
2239 * send a command complete event. To keep the flow
2240 * control working inject that event here.
2242 if (opcode
== 0xfc01)
2243 inject_cmd_complete(hdev
, opcode
);
2245 urb
= alloc_ctrl_urb(hdev
, skb
);
2248 return PTR_ERR(urb
);
2250 hdev
->stat
.cmd_tx
++;
2251 return submit_or_queue_tx_urb(hdev
, urb
);
2253 case HCI_ACLDATA_PKT
:
2254 urb
= alloc_bulk_urb(hdev
, skb
);
2256 return PTR_ERR(urb
);
2258 hdev
->stat
.acl_tx
++;
2259 return submit_or_queue_tx_urb(hdev
, urb
);
2261 case HCI_SCODATA_PKT
:
2262 if (hci_conn_num(hdev
, SCO_LINK
) < 1)
2265 urb
= alloc_isoc_urb(hdev
, skb
);
2267 return PTR_ERR(urb
);
2269 hdev
->stat
.sco_tx
++;
2270 return submit_tx_urb(hdev
, urb
);
2276 static int btusb_intel_secure_send(struct hci_dev
*hdev
, u8 fragment_type
,
2277 u32 plen
, const void *param
)
2280 struct sk_buff
*skb
;
2281 u8 cmd_param
[253], fragment_len
= (plen
> 252) ? 252 : plen
;
2283 cmd_param
[0] = fragment_type
;
2284 memcpy(cmd_param
+ 1, param
, fragment_len
);
2286 skb
= __hci_cmd_sync(hdev
, 0xfc09, fragment_len
+ 1,
2287 cmd_param
, HCI_INIT_TIMEOUT
);
2289 return PTR_ERR(skb
);
2293 plen
-= fragment_len
;
2294 param
+= fragment_len
;
2300 static void btusb_intel_version_info(struct hci_dev
*hdev
,
2301 struct intel_version
*ver
)
2303 const char *variant
;
2305 switch (ver
->fw_variant
) {
2307 variant
= "Bootloader";
2310 variant
= "Firmware";
2316 BT_INFO("%s: %s revision %u.%u build %u week %u %u", hdev
->name
,
2317 variant
, ver
->fw_revision
>> 4, ver
->fw_revision
& 0x0f,
2318 ver
->fw_build_num
, ver
->fw_build_ww
, 2000 + ver
->fw_build_yy
);
2321 static int btusb_setup_intel_new(struct hci_dev
*hdev
)
2323 static const u8 reset_param
[] = { 0x00, 0x01, 0x00, 0x01,
2324 0x00, 0x08, 0x04, 0x00 };
2325 struct btusb_data
*data
= hci_get_drvdata(hdev
);
2326 struct sk_buff
*skb
;
2327 struct intel_version
*ver
;
2328 struct intel_boot_params
*params
;
2329 const struct firmware
*fw
;
2332 ktime_t calltime
, delta
, rettime
;
2333 unsigned long long duration
;
2336 BT_DBG("%s", hdev
->name
);
2338 calltime
= ktime_get();
2340 /* Read the Intel version information to determine if the device
2341 * is in bootloader mode or if it already has operational firmware
2344 skb
= __hci_cmd_sync(hdev
, 0xfc05, 0, NULL
, HCI_INIT_TIMEOUT
);
2346 BT_ERR("%s: Reading Intel version information failed (%ld)",
2347 hdev
->name
, PTR_ERR(skb
));
2348 return PTR_ERR(skb
);
2351 if (skb
->len
!= sizeof(*ver
)) {
2352 BT_ERR("%s: Intel version event size mismatch", hdev
->name
);
2357 ver
= (struct intel_version
*)skb
->data
;
2359 BT_ERR("%s: Intel version command failure (%02x)",
2360 hdev
->name
, ver
->status
);
2361 err
= -bt_to_errno(ver
->status
);
2366 /* The hardware platform number has a fixed value of 0x37 and
2367 * for now only accept this single value.
2369 if (ver
->hw_platform
!= 0x37) {
2370 BT_ERR("%s: Unsupported Intel hardware platform (%u)",
2371 hdev
->name
, ver
->hw_platform
);
2376 /* At the moment only the hardware variant iBT 3.0 (LnP/SfP) is
2377 * supported by this firmware loading method. This check has been
2378 * put in place to ensure correct forward compatibility options
2379 * when newer hardware variants come along.
2381 if (ver
->hw_variant
!= 0x0b) {
2382 BT_ERR("%s: Unsupported Intel hardware variant (%u)",
2383 hdev
->name
, ver
->hw_variant
);
2388 btusb_intel_version_info(hdev
, ver
);
2390 /* The firmware variant determines if the device is in bootloader
2391 * mode or is running operational firmware. The value 0x06 identifies
2392 * the bootloader and the value 0x23 identifies the operational
2395 * When the operational firmware is already present, then only
2396 * the check for valid Bluetooth device address is needed. This
2397 * determines if the device will be added as configured or
2398 * unconfigured controller.
2400 * It is not possible to use the Secure Boot Parameters in this
2401 * case since that command is only available in bootloader mode.
2403 if (ver
->fw_variant
== 0x23) {
2405 clear_bit(BTUSB_BOOTLOADER
, &data
->flags
);
2406 btintel_check_bdaddr(hdev
);
2410 /* If the device is not in bootloader mode, then the only possible
2411 * choice is to return an error and abort the device initialization.
2413 if (ver
->fw_variant
!= 0x06) {
2414 BT_ERR("%s: Unsupported Intel firmware variant (%u)",
2415 hdev
->name
, ver
->fw_variant
);
2422 /* Read the secure boot parameters to identify the operating
2423 * details of the bootloader.
2425 skb
= __hci_cmd_sync(hdev
, 0xfc0d, 0, NULL
, HCI_INIT_TIMEOUT
);
2427 BT_ERR("%s: Reading Intel boot parameters failed (%ld)",
2428 hdev
->name
, PTR_ERR(skb
));
2429 return PTR_ERR(skb
);
2432 if (skb
->len
!= sizeof(*params
)) {
2433 BT_ERR("%s: Intel boot parameters size mismatch", hdev
->name
);
2438 params
= (struct intel_boot_params
*)skb
->data
;
2439 if (params
->status
) {
2440 BT_ERR("%s: Intel boot parameters command failure (%02x)",
2441 hdev
->name
, params
->status
);
2442 err
= -bt_to_errno(params
->status
);
2447 BT_INFO("%s: Device revision is %u", hdev
->name
,
2448 le16_to_cpu(params
->dev_revid
));
2450 BT_INFO("%s: Secure boot is %s", hdev
->name
,
2451 params
->secure_boot
? "enabled" : "disabled");
2453 BT_INFO("%s: Minimum firmware build %u week %u %u", hdev
->name
,
2454 params
->min_fw_build_nn
, params
->min_fw_build_cw
,
2455 2000 + params
->min_fw_build_yy
);
2457 /* It is required that every single firmware fragment is acknowledged
2458 * with a command complete event. If the boot parameters indicate
2459 * that this bootloader does not send them, then abort the setup.
2461 if (params
->limited_cce
!= 0x00) {
2462 BT_ERR("%s: Unsupported Intel firmware loading method (%u)",
2463 hdev
->name
, params
->limited_cce
);
2468 /* If the OTP has no valid Bluetooth device address, then there will
2469 * also be no valid address for the operational firmware.
2471 if (!bacmp(¶ms
->otp_bdaddr
, BDADDR_ANY
)) {
2472 BT_INFO("%s: No device address configured", hdev
->name
);
2473 set_bit(HCI_QUIRK_INVALID_BDADDR
, &hdev
->quirks
);
2476 /* With this Intel bootloader only the hardware variant and device
2477 * revision information are used to select the right firmware.
2479 * Currently this bootloader support is limited to hardware variant
2480 * iBT 3.0 (LnP/SfP) which is identified by the value 11 (0x0b).
2482 snprintf(fwname
, sizeof(fwname
), "intel/ibt-11-%u.sfi",
2483 le16_to_cpu(params
->dev_revid
));
2485 err
= request_firmware(&fw
, fwname
, &hdev
->dev
);
2487 BT_ERR("%s: Failed to load Intel firmware file (%d)",
2493 BT_INFO("%s: Found device firmware: %s", hdev
->name
, fwname
);
2497 if (fw
->size
< 644) {
2498 BT_ERR("%s: Invalid size of firmware file (%zu)",
2499 hdev
->name
, fw
->size
);
2504 set_bit(BTUSB_DOWNLOADING
, &data
->flags
);
2506 /* Start the firmware download transaction with the Init fragment
2507 * represented by the 128 bytes of CSS header.
2509 err
= btusb_intel_secure_send(hdev
, 0x00, 128, fw
->data
);
2511 BT_ERR("%s: Failed to send firmware header (%d)",
2516 /* Send the 256 bytes of public key information from the firmware
2517 * as the PKey fragment.
2519 err
= btusb_intel_secure_send(hdev
, 0x03, 256, fw
->data
+ 128);
2521 BT_ERR("%s: Failed to send firmware public key (%d)",
2526 /* Send the 256 bytes of signature information from the firmware
2527 * as the Sign fragment.
2529 err
= btusb_intel_secure_send(hdev
, 0x02, 256, fw
->data
+ 388);
2531 BT_ERR("%s: Failed to send firmware signature (%d)",
2536 fw_ptr
= fw
->data
+ 644;
2538 while (fw_ptr
- fw
->data
< fw
->size
) {
2539 struct hci_command_hdr
*cmd
= (void *)fw_ptr
;
2542 cmd_len
= sizeof(*cmd
) + cmd
->plen
;
2544 /* Send each command from the firmware data buffer as
2545 * a single Data fragment.
2547 err
= btusb_intel_secure_send(hdev
, 0x01, cmd_len
, fw_ptr
);
2549 BT_ERR("%s: Failed to send firmware data (%d)",
2557 set_bit(BTUSB_FIRMWARE_LOADED
, &data
->flags
);
2559 BT_INFO("%s: Waiting for firmware download to complete", hdev
->name
);
2561 /* Before switching the device into operational mode and with that
2562 * booting the loaded firmware, wait for the bootloader notification
2563 * that all fragments have been successfully received.
2565 * When the event processing receives the notification, then the
2566 * BTUSB_DOWNLOADING flag will be cleared.
2568 * The firmware loading should not take longer than 5 seconds
2569 * and thus just timeout if that happens and fail the setup
2572 err
= wait_on_bit_timeout(&data
->flags
, BTUSB_DOWNLOADING
,
2574 msecs_to_jiffies(5000));
2576 BT_ERR("%s: Firmware loading interrupted", hdev
->name
);
2582 BT_ERR("%s: Firmware loading timeout", hdev
->name
);
2587 if (test_bit(BTUSB_FIRMWARE_FAILED
, &data
->flags
)) {
2588 BT_ERR("%s: Firmware loading failed", hdev
->name
);
2593 rettime
= ktime_get();
2594 delta
= ktime_sub(rettime
, calltime
);
2595 duration
= (unsigned long long) ktime_to_ns(delta
) >> 10;
2597 BT_INFO("%s: Firmware loaded in %llu usecs", hdev
->name
, duration
);
2600 release_firmware(fw
);
2605 calltime
= ktime_get();
2607 set_bit(BTUSB_BOOTING
, &data
->flags
);
2609 skb
= __hci_cmd_sync(hdev
, 0xfc01, sizeof(reset_param
), reset_param
,
2612 return PTR_ERR(skb
);
2616 /* The bootloader will not indicate when the device is ready. This
2617 * is done by the operational firmware sending bootup notification.
2619 * Booting into operational firmware should not take longer than
2620 * 1 second. However if that happens, then just fail the setup
2621 * since something went wrong.
2623 BT_INFO("%s: Waiting for device to boot", hdev
->name
);
2625 err
= wait_on_bit_timeout(&data
->flags
, BTUSB_BOOTING
,
2627 msecs_to_jiffies(1000));
2630 BT_ERR("%s: Device boot interrupted", hdev
->name
);
2635 BT_ERR("%s: Device boot timeout", hdev
->name
);
2639 rettime
= ktime_get();
2640 delta
= ktime_sub(rettime
, calltime
);
2641 duration
= (unsigned long long) ktime_to_ns(delta
) >> 10;
2643 BT_INFO("%s: Device booted in %llu usecs", hdev
->name
, duration
);
2645 clear_bit(BTUSB_BOOTLOADER
, &data
->flags
);
2650 static void btusb_hw_error_intel(struct hci_dev
*hdev
, u8 code
)
2652 struct sk_buff
*skb
;
2655 BT_ERR("%s: Hardware error 0x%2.2x", hdev
->name
, code
);
2657 skb
= __hci_cmd_sync(hdev
, HCI_OP_RESET
, 0, NULL
, HCI_INIT_TIMEOUT
);
2659 BT_ERR("%s: Reset after hardware error failed (%ld)",
2660 hdev
->name
, PTR_ERR(skb
));
2665 skb
= __hci_cmd_sync(hdev
, 0xfc22, 1, &type
, HCI_INIT_TIMEOUT
);
2667 BT_ERR("%s: Retrieving Intel exception info failed (%ld)",
2668 hdev
->name
, PTR_ERR(skb
));
2672 if (skb
->len
!= 13) {
2673 BT_ERR("%s: Exception info size mismatch", hdev
->name
);
2678 if (skb
->data
[0] != 0x00) {
2679 BT_ERR("%s: Exception info command failure (%02x)",
2680 hdev
->name
, skb
->data
[0]);
2685 BT_ERR("%s: Exception info %s", hdev
->name
, (char *)(skb
->data
+ 1));
2690 static int btusb_shutdown_intel(struct hci_dev
*hdev
)
2692 struct sk_buff
*skb
;
2695 /* Some platforms have an issue with BT LED when the interface is
2696 * down or BT radio is turned off, which takes 5 seconds to BT LED
2697 * goes off. This command turns off the BT LED immediately.
2699 skb
= __hci_cmd_sync(hdev
, 0xfc3f, 0, NULL
, HCI_INIT_TIMEOUT
);
2702 BT_ERR("%s: turning off Intel device LED failed (%ld)",
2711 static int btusb_set_bdaddr_marvell(struct hci_dev
*hdev
,
2712 const bdaddr_t
*bdaddr
)
2714 struct sk_buff
*skb
;
2719 buf
[1] = sizeof(bdaddr_t
);
2720 memcpy(buf
+ 2, bdaddr
, sizeof(bdaddr_t
));
2722 skb
= __hci_cmd_sync(hdev
, 0xfc22, sizeof(buf
), buf
, HCI_INIT_TIMEOUT
);
2725 BT_ERR("%s: changing Marvell device address failed (%ld)",
2734 static int btusb_set_bdaddr_ath3012(struct hci_dev
*hdev
,
2735 const bdaddr_t
*bdaddr
)
2737 struct sk_buff
*skb
;
2744 buf
[3] = sizeof(bdaddr_t
);
2745 memcpy(buf
+ 4, bdaddr
, sizeof(bdaddr_t
));
2747 skb
= __hci_cmd_sync(hdev
, 0xfc0b, sizeof(buf
), buf
, HCI_INIT_TIMEOUT
);
2750 BT_ERR("%s: Change address command failed (%ld)",
2759 #define QCA_DFU_PACKET_LEN 4096
2761 #define QCA_GET_TARGET_VERSION 0x09
2762 #define QCA_CHECK_STATUS 0x05
2763 #define QCA_DFU_DOWNLOAD 0x01
2765 #define QCA_SYSCFG_UPDATED 0x40
2766 #define QCA_PATCH_UPDATED 0x80
2767 #define QCA_DFU_TIMEOUT 3000
2769 struct qca_version
{
2771 __le32 patch_version
;
2777 struct qca_rampatch_version
{
2779 __le16 patch_version
;
2782 struct qca_device_info
{
2784 u8 rampatch_hdr
; /* length of header in rampatch */
2785 u8 nvm_hdr
; /* length of header in NVM */
2786 u8 ver_offset
; /* offset of version structure in rampatch */
2789 static const struct qca_device_info qca_devices_table
[] = {
2790 { 0x00000100, 20, 4, 10 }, /* Rome 1.0 */
2791 { 0x00000101, 20, 4, 10 }, /* Rome 1.1 */
2792 { 0x00000201, 28, 4, 18 }, /* Rome 2.1 */
2793 { 0x00000300, 28, 4, 18 }, /* Rome 3.0 */
2794 { 0x00000302, 28, 4, 18 }, /* Rome 3.2 */
2797 static int btusb_qca_send_vendor_req(struct hci_dev
*hdev
, u8 request
,
2798 void *data
, u16 size
)
2800 struct btusb_data
*btdata
= hci_get_drvdata(hdev
);
2801 struct usb_device
*udev
= btdata
->udev
;
2805 buf
= kmalloc(size
, GFP_KERNEL
);
2809 /* Found some of USB hosts have IOT issues with ours so that we should
2810 * not wait until HCI layer is ready.
2812 pipe
= usb_rcvctrlpipe(udev
, 0);
2813 err
= usb_control_msg(udev
, pipe
, request
, USB_TYPE_VENDOR
| USB_DIR_IN
,
2814 0, 0, buf
, size
, USB_CTRL_SET_TIMEOUT
);
2816 BT_ERR("%s: Failed to access otp area (%d)", hdev
->name
, err
);
2820 memcpy(data
, buf
, size
);
2828 static int btusb_setup_qca_download_fw(struct hci_dev
*hdev
,
2829 const struct firmware
*firmware
,
2832 struct btusb_data
*btdata
= hci_get_drvdata(hdev
);
2833 struct usb_device
*udev
= btdata
->udev
;
2834 size_t count
, size
, sent
= 0;
2838 buf
= kmalloc(QCA_DFU_PACKET_LEN
, GFP_KERNEL
);
2842 count
= firmware
->size
;
2844 size
= min_t(size_t, count
, hdr_size
);
2845 memcpy(buf
, firmware
->data
, size
);
2847 /* USB patches should go down to controller through USB path
2848 * because binary format fits to go down through USB channel.
2849 * USB control path is for patching headers and USB bulk is for
2852 pipe
= usb_sndctrlpipe(udev
, 0);
2853 err
= usb_control_msg(udev
, pipe
, QCA_DFU_DOWNLOAD
, USB_TYPE_VENDOR
,
2854 0, 0, buf
, size
, USB_CTRL_SET_TIMEOUT
);
2856 BT_ERR("%s: Failed to send headers (%d)", hdev
->name
, err
);
2864 size
= min_t(size_t, count
, QCA_DFU_PACKET_LEN
);
2866 memcpy(buf
, firmware
->data
+ sent
, size
);
2868 pipe
= usb_sndbulkpipe(udev
, 0x02);
2869 err
= usb_bulk_msg(udev
, pipe
, buf
, size
, &len
,
2872 BT_ERR("%s: Failed to send body at %zd of %zd (%d)",
2873 hdev
->name
, sent
, firmware
->size
, err
);
2878 BT_ERR("%s: Failed to get bulk buffer", hdev
->name
);
2892 static int btusb_setup_qca_load_rampatch(struct hci_dev
*hdev
,
2893 struct qca_version
*ver
,
2894 const struct qca_device_info
*info
)
2896 struct qca_rampatch_version
*rver
;
2897 const struct firmware
*fw
;
2898 u32 ver_rom
, ver_patch
;
2899 u16 rver_rom
, rver_patch
;
2903 ver_rom
= le32_to_cpu(ver
->rom_version
);
2904 ver_patch
= le32_to_cpu(ver
->patch_version
);
2906 snprintf(fwname
, sizeof(fwname
), "qca/rampatch_usb_%08x.bin", ver_rom
);
2908 err
= request_firmware(&fw
, fwname
, &hdev
->dev
);
2910 BT_ERR("%s: failed to request rampatch file: %s (%d)",
2911 hdev
->name
, fwname
, err
);
2915 BT_INFO("%s: using rampatch file: %s", hdev
->name
, fwname
);
2917 rver
= (struct qca_rampatch_version
*)(fw
->data
+ info
->ver_offset
);
2918 rver_rom
= le16_to_cpu(rver
->rom_version
);
2919 rver_patch
= le16_to_cpu(rver
->patch_version
);
2921 BT_INFO("%s: QCA: patch rome 0x%x build 0x%x, firmware rome 0x%x "
2922 "build 0x%x", hdev
->name
, rver_rom
, rver_patch
, ver_rom
,
2925 if (rver_rom
!= ver_rom
|| rver_patch
<= ver_patch
) {
2926 BT_ERR("%s: rampatch file version did not match with firmware",
2932 err
= btusb_setup_qca_download_fw(hdev
, fw
, info
->rampatch_hdr
);
2935 release_firmware(fw
);
2940 static int btusb_setup_qca_load_nvm(struct hci_dev
*hdev
,
2941 struct qca_version
*ver
,
2942 const struct qca_device_info
*info
)
2944 const struct firmware
*fw
;
2948 snprintf(fwname
, sizeof(fwname
), "qca/nvm_usb_%08x.bin",
2949 le32_to_cpu(ver
->rom_version
));
2951 err
= request_firmware(&fw
, fwname
, &hdev
->dev
);
2953 BT_ERR("%s: failed to request NVM file: %s (%d)",
2954 hdev
->name
, fwname
, err
);
2958 BT_INFO("%s: using NVM file: %s", hdev
->name
, fwname
);
2960 err
= btusb_setup_qca_download_fw(hdev
, fw
, info
->nvm_hdr
);
2962 release_firmware(fw
);
2967 static int btusb_setup_qca(struct hci_dev
*hdev
)
2969 const struct qca_device_info
*info
= NULL
;
2970 struct qca_version ver
;
2975 err
= btusb_qca_send_vendor_req(hdev
, QCA_GET_TARGET_VERSION
, &ver
,
2980 ver_rom
= le32_to_cpu(ver
.rom_version
);
2981 for (i
= 0; i
< ARRAY_SIZE(qca_devices_table
); i
++) {
2982 if (ver_rom
== qca_devices_table
[i
].rom_version
)
2983 info
= &qca_devices_table
[i
];
2986 BT_ERR("%s: don't support firmware rome 0x%x", hdev
->name
,
2991 err
= btusb_qca_send_vendor_req(hdev
, QCA_CHECK_STATUS
, &status
,
2996 if (!(status
& QCA_PATCH_UPDATED
)) {
2997 err
= btusb_setup_qca_load_rampatch(hdev
, &ver
, info
);
3002 if (!(status
& QCA_SYSCFG_UPDATED
)) {
3003 err
= btusb_setup_qca_load_nvm(hdev
, &ver
, info
);
3011 static int btusb_probe(struct usb_interface
*intf
,
3012 const struct usb_device_id
*id
)
3014 struct usb_endpoint_descriptor
*ep_desc
;
3015 struct btusb_data
*data
;
3016 struct hci_dev
*hdev
;
3019 BT_DBG("intf %p id %p", intf
, id
);
3021 /* interface numbers are hardcoded in the spec */
3022 if (intf
->cur_altsetting
->desc
.bInterfaceNumber
!= 0)
3025 if (!id
->driver_info
) {
3026 const struct usb_device_id
*match
;
3028 match
= usb_match_id(intf
, blacklist_table
);
3033 if (id
->driver_info
== BTUSB_IGNORE
)
3036 if (id
->driver_info
& BTUSB_ATH3012
) {
3037 struct usb_device
*udev
= interface_to_usbdev(intf
);
3039 /* Old firmware would otherwise let ath3k driver load
3040 * patch and sysconfig files */
3041 if (le16_to_cpu(udev
->descriptor
.bcdDevice
) <= 0x0001)
3045 data
= devm_kzalloc(&intf
->dev
, sizeof(*data
), GFP_KERNEL
);
3049 for (i
= 0; i
< intf
->cur_altsetting
->desc
.bNumEndpoints
; i
++) {
3050 ep_desc
= &intf
->cur_altsetting
->endpoint
[i
].desc
;
3052 if (!data
->intr_ep
&& usb_endpoint_is_int_in(ep_desc
)) {
3053 data
->intr_ep
= ep_desc
;
3057 if (!data
->bulk_tx_ep
&& usb_endpoint_is_bulk_out(ep_desc
)) {
3058 data
->bulk_tx_ep
= ep_desc
;
3062 if (!data
->bulk_rx_ep
&& usb_endpoint_is_bulk_in(ep_desc
)) {
3063 data
->bulk_rx_ep
= ep_desc
;
3068 if (!data
->intr_ep
|| !data
->bulk_tx_ep
|| !data
->bulk_rx_ep
)
3071 if (id
->driver_info
& BTUSB_AMP
) {
3072 data
->cmdreq_type
= USB_TYPE_CLASS
| 0x01;
3073 data
->cmdreq
= 0x2b;
3075 data
->cmdreq_type
= USB_TYPE_CLASS
;
3076 data
->cmdreq
= 0x00;
3079 data
->udev
= interface_to_usbdev(intf
);
3082 INIT_WORK(&data
->work
, btusb_work
);
3083 INIT_WORK(&data
->waker
, btusb_waker
);
3084 init_usb_anchor(&data
->deferred
);
3085 init_usb_anchor(&data
->tx_anchor
);
3086 spin_lock_init(&data
->txlock
);
3088 init_usb_anchor(&data
->intr_anchor
);
3089 init_usb_anchor(&data
->bulk_anchor
);
3090 init_usb_anchor(&data
->isoc_anchor
);
3091 spin_lock_init(&data
->rxlock
);
3093 if (id
->driver_info
& BTUSB_INTEL_NEW
) {
3094 data
->recv_event
= btusb_recv_event_intel
;
3095 data
->recv_bulk
= btusb_recv_bulk_intel
;
3096 set_bit(BTUSB_BOOTLOADER
, &data
->flags
);
3098 data
->recv_event
= hci_recv_frame
;
3099 data
->recv_bulk
= btusb_recv_bulk
;
3102 hdev
= hci_alloc_dev();
3106 hdev
->bus
= HCI_USB
;
3107 hci_set_drvdata(hdev
, data
);
3109 if (id
->driver_info
& BTUSB_AMP
)
3110 hdev
->dev_type
= HCI_AMP
;
3112 hdev
->dev_type
= HCI_BREDR
;
3116 SET_HCIDEV_DEV(hdev
, &intf
->dev
);
3118 hdev
->open
= btusb_open
;
3119 hdev
->close
= btusb_close
;
3120 hdev
->flush
= btusb_flush
;
3121 hdev
->send
= btusb_send_frame
;
3122 hdev
->notify
= btusb_notify
;
3124 if (id
->driver_info
& BTUSB_BCM92035
)
3125 hdev
->setup
= btusb_setup_bcm92035
;
3127 #ifdef CONFIG_BT_HCIBTUSB_BCM
3128 if (id
->driver_info
& BTUSB_BCM_PATCHRAM
) {
3129 hdev
->setup
= btbcm_setup_patchram
;
3130 hdev
->set_bdaddr
= btbcm_set_bdaddr
;
3133 if (id
->driver_info
& BTUSB_BCM_APPLE
)
3134 hdev
->setup
= btbcm_setup_apple
;
3137 if (id
->driver_info
& BTUSB_INTEL
) {
3138 hdev
->setup
= btusb_setup_intel
;
3139 hdev
->shutdown
= btusb_shutdown_intel
;
3140 hdev
->set_bdaddr
= btintel_set_bdaddr
;
3141 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER
, &hdev
->quirks
);
3142 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY
, &hdev
->quirks
);
3145 if (id
->driver_info
& BTUSB_INTEL_NEW
) {
3146 hdev
->send
= btusb_send_frame_intel
;
3147 hdev
->setup
= btusb_setup_intel_new
;
3148 hdev
->hw_error
= btusb_hw_error_intel
;
3149 hdev
->set_bdaddr
= btintel_set_bdaddr
;
3150 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER
, &hdev
->quirks
);
3153 if (id
->driver_info
& BTUSB_MARVELL
)
3154 hdev
->set_bdaddr
= btusb_set_bdaddr_marvell
;
3156 if (id
->driver_info
& BTUSB_SWAVE
) {
3157 set_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE
, &hdev
->quirks
);
3158 set_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS
, &hdev
->quirks
);
3161 if (id
->driver_info
& BTUSB_INTEL_BOOT
)
3162 set_bit(HCI_QUIRK_RAW_DEVICE
, &hdev
->quirks
);
3164 if (id
->driver_info
& BTUSB_ATH3012
) {
3165 hdev
->set_bdaddr
= btusb_set_bdaddr_ath3012
;
3166 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY
, &hdev
->quirks
);
3167 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER
, &hdev
->quirks
);
3170 if (id
->driver_info
& BTUSB_QCA_ROME
) {
3171 data
->setup_on_usb
= btusb_setup_qca
;
3172 hdev
->set_bdaddr
= btusb_set_bdaddr_ath3012
;
3175 if (id
->driver_info
& BTUSB_REALTEK
)
3176 hdev
->setup
= btusb_setup_realtek
;
3178 if (id
->driver_info
& BTUSB_AMP
) {
3179 /* AMP controllers do not support SCO packets */
3182 /* Interface numbers are hardcoded in the specification */
3183 data
->isoc
= usb_ifnum_to_if(data
->udev
, 1);
3187 set_bit(HCI_QUIRK_RESET_ON_CLOSE
, &hdev
->quirks
);
3189 if (force_scofix
|| id
->driver_info
& BTUSB_WRONG_SCO_MTU
) {
3190 if (!disable_scofix
)
3191 set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE
, &hdev
->quirks
);
3194 if (id
->driver_info
& BTUSB_BROKEN_ISOC
)
3197 if (id
->driver_info
& BTUSB_DIGIANSWER
) {
3198 data
->cmdreq_type
= USB_TYPE_VENDOR
;
3199 set_bit(HCI_QUIRK_RESET_ON_CLOSE
, &hdev
->quirks
);
3202 if (id
->driver_info
& BTUSB_CSR
) {
3203 struct usb_device
*udev
= data
->udev
;
3204 u16 bcdDevice
= le16_to_cpu(udev
->descriptor
.bcdDevice
);
3206 /* Old firmware would otherwise execute USB reset */
3207 if (bcdDevice
< 0x117)
3208 set_bit(HCI_QUIRK_RESET_ON_CLOSE
, &hdev
->quirks
);
3210 /* Fake CSR devices with broken commands */
3211 if (bcdDevice
<= 0x100)
3212 hdev
->setup
= btusb_setup_csr
;
3214 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY
, &hdev
->quirks
);
3217 if (id
->driver_info
& BTUSB_SNIFFER
) {
3218 struct usb_device
*udev
= data
->udev
;
3220 /* New sniffer firmware has crippled HCI interface */
3221 if (le16_to_cpu(udev
->descriptor
.bcdDevice
) > 0x997)
3222 set_bit(HCI_QUIRK_RAW_DEVICE
, &hdev
->quirks
);
3225 if (id
->driver_info
& BTUSB_INTEL_BOOT
) {
3226 /* A bug in the bootloader causes that interrupt interface is
3227 * only enabled after receiving SetInterface(0, AltSetting=0).
3229 err
= usb_set_interface(data
->udev
, 0, 0);
3231 BT_ERR("failed to set interface 0, alt 0 %d", err
);
3238 err
= usb_driver_claim_interface(&btusb_driver
,
3246 err
= hci_register_dev(hdev
);
3252 usb_set_intfdata(intf
, data
);
3257 static void btusb_disconnect(struct usb_interface
*intf
)
3259 struct btusb_data
*data
= usb_get_intfdata(intf
);
3260 struct hci_dev
*hdev
;
3262 BT_DBG("intf %p", intf
);
3268 usb_set_intfdata(data
->intf
, NULL
);
3271 usb_set_intfdata(data
->isoc
, NULL
);
3273 hci_unregister_dev(hdev
);
3275 if (intf
== data
->isoc
)
3276 usb_driver_release_interface(&btusb_driver
, data
->intf
);
3277 else if (data
->isoc
)
3278 usb_driver_release_interface(&btusb_driver
, data
->isoc
);
3284 static int btusb_suspend(struct usb_interface
*intf
, pm_message_t message
)
3286 struct btusb_data
*data
= usb_get_intfdata(intf
);
3288 BT_DBG("intf %p", intf
);
3290 if (data
->suspend_count
++)
3293 spin_lock_irq(&data
->txlock
);
3294 if (!(PMSG_IS_AUTO(message
) && data
->tx_in_flight
)) {
3295 set_bit(BTUSB_SUSPENDING
, &data
->flags
);
3296 spin_unlock_irq(&data
->txlock
);
3298 spin_unlock_irq(&data
->txlock
);
3299 data
->suspend_count
--;
3303 cancel_work_sync(&data
->work
);
3305 btusb_stop_traffic(data
);
3306 usb_kill_anchored_urbs(&data
->tx_anchor
);
3311 static void play_deferred(struct btusb_data
*data
)
3316 while ((urb
= usb_get_from_anchor(&data
->deferred
))) {
3317 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
3321 data
->tx_in_flight
++;
3323 usb_scuttle_anchored_urbs(&data
->deferred
);
3326 static int btusb_resume(struct usb_interface
*intf
)
3328 struct btusb_data
*data
= usb_get_intfdata(intf
);
3329 struct hci_dev
*hdev
= data
->hdev
;
3332 BT_DBG("intf %p", intf
);
3334 if (--data
->suspend_count
)
3337 if (!test_bit(HCI_RUNNING
, &hdev
->flags
))
3340 if (test_bit(BTUSB_INTR_RUNNING
, &data
->flags
)) {
3341 err
= btusb_submit_intr_urb(hdev
, GFP_NOIO
);
3343 clear_bit(BTUSB_INTR_RUNNING
, &data
->flags
);
3348 if (test_bit(BTUSB_BULK_RUNNING
, &data
->flags
)) {
3349 err
= btusb_submit_bulk_urb(hdev
, GFP_NOIO
);
3351 clear_bit(BTUSB_BULK_RUNNING
, &data
->flags
);
3355 btusb_submit_bulk_urb(hdev
, GFP_NOIO
);
3358 if (test_bit(BTUSB_ISOC_RUNNING
, &data
->flags
)) {
3359 if (btusb_submit_isoc_urb(hdev
, GFP_NOIO
) < 0)
3360 clear_bit(BTUSB_ISOC_RUNNING
, &data
->flags
);
3362 btusb_submit_isoc_urb(hdev
, GFP_NOIO
);
3365 spin_lock_irq(&data
->txlock
);
3366 play_deferred(data
);
3367 clear_bit(BTUSB_SUSPENDING
, &data
->flags
);
3368 spin_unlock_irq(&data
->txlock
);
3369 schedule_work(&data
->work
);
3374 usb_scuttle_anchored_urbs(&data
->deferred
);
3376 spin_lock_irq(&data
->txlock
);
3377 clear_bit(BTUSB_SUSPENDING
, &data
->flags
);
3378 spin_unlock_irq(&data
->txlock
);
3384 static struct usb_driver btusb_driver
= {
3386 .probe
= btusb_probe
,
3387 .disconnect
= btusb_disconnect
,
3389 .suspend
= btusb_suspend
,
3390 .resume
= btusb_resume
,
3392 .id_table
= btusb_table
,
3393 .supports_autosuspend
= 1,
3394 .disable_hub_initiated_lpm
= 1,
3397 module_usb_driver(btusb_driver
);
3399 module_param(disable_scofix
, bool, 0644);
3400 MODULE_PARM_DESC(disable_scofix
, "Disable fixup of wrong SCO buffer size");
3402 module_param(force_scofix
, bool, 0644);
3403 MODULE_PARM_DESC(force_scofix
, "Force fixup of wrong SCO buffers size");
3405 module_param(reset
, bool, 0644);
3406 MODULE_PARM_DESC(reset
, "Send HCI reset command on initialization");
3408 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
3409 MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION
);
3410 MODULE_VERSION(VERSION
);
3411 MODULE_LICENSE("GPL");