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, 0x300f), .driver_info
= BTUSB_ATH3012
},
190 { USB_DEVICE(0x04ca, 0x3010), .driver_info
= BTUSB_ATH3012
},
191 { USB_DEVICE(0x0930, 0x0219), .driver_info
= BTUSB_ATH3012
},
192 { USB_DEVICE(0x0930, 0x0220), .driver_info
= BTUSB_ATH3012
},
193 { USB_DEVICE(0x0930, 0x0227), .driver_info
= BTUSB_ATH3012
},
194 { USB_DEVICE(0x0b05, 0x17d0), .driver_info
= BTUSB_ATH3012
},
195 { USB_DEVICE(0x0cf3, 0x0036), .driver_info
= BTUSB_ATH3012
},
196 { USB_DEVICE(0x0cf3, 0x3004), .driver_info
= BTUSB_ATH3012
},
197 { USB_DEVICE(0x0cf3, 0x3008), .driver_info
= BTUSB_ATH3012
},
198 { USB_DEVICE(0x0cf3, 0x311d), .driver_info
= BTUSB_ATH3012
},
199 { USB_DEVICE(0x0cf3, 0x311e), .driver_info
= BTUSB_ATH3012
},
200 { USB_DEVICE(0x0cf3, 0x311f), .driver_info
= BTUSB_ATH3012
},
201 { USB_DEVICE(0x0cf3, 0x3121), .driver_info
= BTUSB_ATH3012
},
202 { USB_DEVICE(0x0cf3, 0x817a), .driver_info
= BTUSB_ATH3012
},
203 { USB_DEVICE(0x0cf3, 0xe003), .driver_info
= BTUSB_ATH3012
},
204 { USB_DEVICE(0x0cf3, 0xe004), .driver_info
= BTUSB_ATH3012
},
205 { USB_DEVICE(0x0cf3, 0xe005), .driver_info
= BTUSB_ATH3012
},
206 { USB_DEVICE(0x0cf3, 0xe006), .driver_info
= BTUSB_ATH3012
},
207 { USB_DEVICE(0x13d3, 0x3362), .driver_info
= BTUSB_ATH3012
},
208 { USB_DEVICE(0x13d3, 0x3375), .driver_info
= BTUSB_ATH3012
},
209 { USB_DEVICE(0x13d3, 0x3393), .driver_info
= BTUSB_ATH3012
},
210 { USB_DEVICE(0x13d3, 0x3402), .driver_info
= BTUSB_ATH3012
},
211 { USB_DEVICE(0x13d3, 0x3408), .driver_info
= BTUSB_ATH3012
},
212 { USB_DEVICE(0x13d3, 0x3423), .driver_info
= BTUSB_ATH3012
},
213 { USB_DEVICE(0x13d3, 0x3432), .driver_info
= BTUSB_ATH3012
},
215 /* Atheros AR5BBU12 with sflash firmware */
216 { USB_DEVICE(0x0489, 0xe02c), .driver_info
= BTUSB_IGNORE
},
218 /* Atheros AR5BBU12 with sflash firmware */
219 { USB_DEVICE(0x0489, 0xe036), .driver_info
= BTUSB_ATH3012
},
220 { USB_DEVICE(0x0489, 0xe03c), .driver_info
= BTUSB_ATH3012
},
222 /* QCA ROME chipset */
223 { USB_DEVICE(0x0cf3, 0xe007), .driver_info
= BTUSB_QCA_ROME
},
224 { USB_DEVICE(0x0cf3, 0xe300), .driver_info
= BTUSB_QCA_ROME
},
225 { USB_DEVICE(0x0cf3, 0xe360), .driver_info
= BTUSB_QCA_ROME
},
227 /* Broadcom BCM2035 */
228 { USB_DEVICE(0x0a5c, 0x2009), .driver_info
= BTUSB_BCM92035
},
229 { USB_DEVICE(0x0a5c, 0x200a), .driver_info
= BTUSB_WRONG_SCO_MTU
},
230 { USB_DEVICE(0x0a5c, 0x2035), .driver_info
= BTUSB_WRONG_SCO_MTU
},
232 /* Broadcom BCM2045 */
233 { USB_DEVICE(0x0a5c, 0x2039), .driver_info
= BTUSB_WRONG_SCO_MTU
},
234 { USB_DEVICE(0x0a5c, 0x2101), .driver_info
= BTUSB_WRONG_SCO_MTU
},
236 /* IBM/Lenovo ThinkPad with Broadcom chip */
237 { USB_DEVICE(0x0a5c, 0x201e), .driver_info
= BTUSB_WRONG_SCO_MTU
},
238 { USB_DEVICE(0x0a5c, 0x2110), .driver_info
= BTUSB_WRONG_SCO_MTU
},
240 /* HP laptop with Broadcom chip */
241 { USB_DEVICE(0x03f0, 0x171d), .driver_info
= BTUSB_WRONG_SCO_MTU
},
243 /* Dell laptop with Broadcom chip */
244 { USB_DEVICE(0x413c, 0x8126), .driver_info
= BTUSB_WRONG_SCO_MTU
},
246 /* Dell Wireless 370 and 410 devices */
247 { USB_DEVICE(0x413c, 0x8152), .driver_info
= BTUSB_WRONG_SCO_MTU
},
248 { USB_DEVICE(0x413c, 0x8156), .driver_info
= BTUSB_WRONG_SCO_MTU
},
250 /* Belkin F8T012 and F8T013 devices */
251 { USB_DEVICE(0x050d, 0x0012), .driver_info
= BTUSB_WRONG_SCO_MTU
},
252 { USB_DEVICE(0x050d, 0x0013), .driver_info
= BTUSB_WRONG_SCO_MTU
},
254 /* Asus WL-BTD202 device */
255 { USB_DEVICE(0x0b05, 0x1715), .driver_info
= BTUSB_WRONG_SCO_MTU
},
257 /* Kensington Bluetooth USB adapter */
258 { USB_DEVICE(0x047d, 0x105e), .driver_info
= BTUSB_WRONG_SCO_MTU
},
260 /* RTX Telecom based adapters with buggy SCO support */
261 { USB_DEVICE(0x0400, 0x0807), .driver_info
= BTUSB_BROKEN_ISOC
},
262 { USB_DEVICE(0x0400, 0x080a), .driver_info
= BTUSB_BROKEN_ISOC
},
264 /* CONWISE Technology based adapters with buggy SCO support */
265 { USB_DEVICE(0x0e5e, 0x6622), .driver_info
= BTUSB_BROKEN_ISOC
},
267 /* Roper Class 1 Bluetooth Dongle (Silicon Wave based) */
268 { USB_DEVICE(0x1300, 0x0001), .driver_info
= BTUSB_SWAVE
},
270 /* Digianswer devices */
271 { USB_DEVICE(0x08fd, 0x0001), .driver_info
= BTUSB_DIGIANSWER
},
272 { USB_DEVICE(0x08fd, 0x0002), .driver_info
= BTUSB_IGNORE
},
274 /* CSR BlueCore Bluetooth Sniffer */
275 { USB_DEVICE(0x0a12, 0x0002),
276 .driver_info
= BTUSB_SNIFFER
| BTUSB_BROKEN_ISOC
},
278 /* Frontline ComProbe Bluetooth Sniffer */
279 { USB_DEVICE(0x16d3, 0x0002),
280 .driver_info
= BTUSB_SNIFFER
| BTUSB_BROKEN_ISOC
},
282 /* Marvell Bluetooth devices */
283 { USB_DEVICE(0x1286, 0x2044), .driver_info
= BTUSB_MARVELL
},
284 { USB_DEVICE(0x1286, 0x2046), .driver_info
= BTUSB_MARVELL
},
286 /* Intel Bluetooth devices */
287 { USB_DEVICE(0x8087, 0x07da), .driver_info
= BTUSB_CSR
},
288 { USB_DEVICE(0x8087, 0x07dc), .driver_info
= BTUSB_INTEL
},
289 { USB_DEVICE(0x8087, 0x0a2a), .driver_info
= BTUSB_INTEL
},
290 { USB_DEVICE(0x8087, 0x0a2b), .driver_info
= BTUSB_INTEL_NEW
},
292 /* Other Intel Bluetooth devices */
293 { USB_VENDOR_AND_INTERFACE_INFO(0x8087, 0xe0, 0x01, 0x01),
294 .driver_info
= BTUSB_IGNORE
},
296 /* Realtek Bluetooth devices */
297 { USB_VENDOR_AND_INTERFACE_INFO(0x0bda, 0xe0, 0x01, 0x01),
298 .driver_info
= BTUSB_REALTEK
},
300 /* Additional Realtek 8723AE Bluetooth devices */
301 { USB_DEVICE(0x0930, 0x021d), .driver_info
= BTUSB_REALTEK
},
302 { USB_DEVICE(0x13d3, 0x3394), .driver_info
= BTUSB_REALTEK
},
304 /* Additional Realtek 8723BE Bluetooth devices */
305 { USB_DEVICE(0x0489, 0xe085), .driver_info
= BTUSB_REALTEK
},
306 { USB_DEVICE(0x0489, 0xe08b), .driver_info
= BTUSB_REALTEK
},
307 { USB_DEVICE(0x13d3, 0x3410), .driver_info
= BTUSB_REALTEK
},
308 { USB_DEVICE(0x13d3, 0x3416), .driver_info
= BTUSB_REALTEK
},
309 { USB_DEVICE(0x13d3, 0x3459), .driver_info
= BTUSB_REALTEK
},
311 /* Additional Realtek 8821AE Bluetooth devices */
312 { USB_DEVICE(0x0b05, 0x17dc), .driver_info
= BTUSB_REALTEK
},
313 { USB_DEVICE(0x13d3, 0x3414), .driver_info
= BTUSB_REALTEK
},
314 { USB_DEVICE(0x13d3, 0x3458), .driver_info
= BTUSB_REALTEK
},
315 { USB_DEVICE(0x13d3, 0x3461), .driver_info
= BTUSB_REALTEK
},
316 { USB_DEVICE(0x13d3, 0x3462), .driver_info
= BTUSB_REALTEK
},
318 { } /* Terminating entry */
321 #define BTUSB_MAX_ISOC_FRAMES 10
323 #define BTUSB_INTR_RUNNING 0
324 #define BTUSB_BULK_RUNNING 1
325 #define BTUSB_ISOC_RUNNING 2
326 #define BTUSB_SUSPENDING 3
327 #define BTUSB_DID_ISO_RESUME 4
328 #define BTUSB_BOOTLOADER 5
329 #define BTUSB_DOWNLOADING 6
330 #define BTUSB_FIRMWARE_LOADED 7
331 #define BTUSB_FIRMWARE_FAILED 8
332 #define BTUSB_BOOTING 9
335 struct hci_dev
*hdev
;
336 struct usb_device
*udev
;
337 struct usb_interface
*intf
;
338 struct usb_interface
*isoc
;
342 struct work_struct work
;
343 struct work_struct waker
;
345 struct usb_anchor deferred
;
346 struct usb_anchor tx_anchor
;
350 struct usb_anchor intr_anchor
;
351 struct usb_anchor bulk_anchor
;
352 struct usb_anchor isoc_anchor
;
355 struct sk_buff
*evt_skb
;
356 struct sk_buff
*acl_skb
;
357 struct sk_buff
*sco_skb
;
359 struct usb_endpoint_descriptor
*intr_ep
;
360 struct usb_endpoint_descriptor
*bulk_tx_ep
;
361 struct usb_endpoint_descriptor
*bulk_rx_ep
;
362 struct usb_endpoint_descriptor
*isoc_tx_ep
;
363 struct usb_endpoint_descriptor
*isoc_rx_ep
;
368 unsigned int sco_num
;
372 int (*recv_event
)(struct hci_dev
*hdev
, struct sk_buff
*skb
);
373 int (*recv_bulk
)(struct btusb_data
*data
, void *buffer
, int count
);
375 int (*setup_on_usb
)(struct hci_dev
*hdev
);
378 static inline void btusb_free_frags(struct btusb_data
*data
)
382 spin_lock_irqsave(&data
->rxlock
, flags
);
384 kfree_skb(data
->evt_skb
);
385 data
->evt_skb
= NULL
;
387 kfree_skb(data
->acl_skb
);
388 data
->acl_skb
= NULL
;
390 kfree_skb(data
->sco_skb
);
391 data
->sco_skb
= NULL
;
393 spin_unlock_irqrestore(&data
->rxlock
, flags
);
396 static int btusb_recv_intr(struct btusb_data
*data
, void *buffer
, int count
)
401 spin_lock(&data
->rxlock
);
408 skb
= bt_skb_alloc(HCI_MAX_EVENT_SIZE
, GFP_ATOMIC
);
414 bt_cb(skb
)->pkt_type
= HCI_EVENT_PKT
;
415 bt_cb(skb
)->expect
= HCI_EVENT_HDR_SIZE
;
418 len
= min_t(uint
, bt_cb(skb
)->expect
, count
);
419 memcpy(skb_put(skb
, len
), buffer
, len
);
423 bt_cb(skb
)->expect
-= len
;
425 if (skb
->len
== HCI_EVENT_HDR_SIZE
) {
426 /* Complete event header */
427 bt_cb(skb
)->expect
= hci_event_hdr(skb
)->plen
;
429 if (skb_tailroom(skb
) < bt_cb(skb
)->expect
) {
438 if (bt_cb(skb
)->expect
== 0) {
440 data
->recv_event(data
->hdev
, skb
);
446 spin_unlock(&data
->rxlock
);
451 static int btusb_recv_bulk(struct btusb_data
*data
, void *buffer
, int count
)
456 spin_lock(&data
->rxlock
);
463 skb
= bt_skb_alloc(HCI_MAX_FRAME_SIZE
, GFP_ATOMIC
);
469 bt_cb(skb
)->pkt_type
= HCI_ACLDATA_PKT
;
470 bt_cb(skb
)->expect
= HCI_ACL_HDR_SIZE
;
473 len
= min_t(uint
, bt_cb(skb
)->expect
, count
);
474 memcpy(skb_put(skb
, len
), buffer
, len
);
478 bt_cb(skb
)->expect
-= len
;
480 if (skb
->len
== HCI_ACL_HDR_SIZE
) {
481 __le16 dlen
= hci_acl_hdr(skb
)->dlen
;
483 /* Complete ACL header */
484 bt_cb(skb
)->expect
= __le16_to_cpu(dlen
);
486 if (skb_tailroom(skb
) < bt_cb(skb
)->expect
) {
495 if (bt_cb(skb
)->expect
== 0) {
497 hci_recv_frame(data
->hdev
, skb
);
503 spin_unlock(&data
->rxlock
);
508 static int btusb_recv_isoc(struct btusb_data
*data
, void *buffer
, int count
)
513 spin_lock(&data
->rxlock
);
520 skb
= bt_skb_alloc(HCI_MAX_SCO_SIZE
, GFP_ATOMIC
);
526 bt_cb(skb
)->pkt_type
= HCI_SCODATA_PKT
;
527 bt_cb(skb
)->expect
= HCI_SCO_HDR_SIZE
;
530 len
= min_t(uint
, bt_cb(skb
)->expect
, count
);
531 memcpy(skb_put(skb
, len
), buffer
, len
);
535 bt_cb(skb
)->expect
-= len
;
537 if (skb
->len
== HCI_SCO_HDR_SIZE
) {
538 /* Complete SCO header */
539 bt_cb(skb
)->expect
= hci_sco_hdr(skb
)->dlen
;
541 if (skb_tailroom(skb
) < bt_cb(skb
)->expect
) {
550 if (bt_cb(skb
)->expect
== 0) {
552 hci_recv_frame(data
->hdev
, skb
);
558 spin_unlock(&data
->rxlock
);
563 static void btusb_intr_complete(struct urb
*urb
)
565 struct hci_dev
*hdev
= urb
->context
;
566 struct btusb_data
*data
= hci_get_drvdata(hdev
);
569 BT_DBG("%s urb %p status %d count %d", hdev
->name
, urb
, urb
->status
,
572 if (!test_bit(HCI_RUNNING
, &hdev
->flags
))
575 if (urb
->status
== 0) {
576 hdev
->stat
.byte_rx
+= urb
->actual_length
;
578 if (btusb_recv_intr(data
, urb
->transfer_buffer
,
579 urb
->actual_length
) < 0) {
580 BT_ERR("%s corrupted event packet", hdev
->name
);
583 } else if (urb
->status
== -ENOENT
) {
584 /* Avoid suspend failed when usb_kill_urb */
588 if (!test_bit(BTUSB_INTR_RUNNING
, &data
->flags
))
591 usb_mark_last_busy(data
->udev
);
592 usb_anchor_urb(urb
, &data
->intr_anchor
);
594 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
596 /* -EPERM: urb is being killed;
597 * -ENODEV: device got disconnected */
598 if (err
!= -EPERM
&& err
!= -ENODEV
)
599 BT_ERR("%s urb %p failed to resubmit (%d)",
600 hdev
->name
, urb
, -err
);
601 usb_unanchor_urb(urb
);
605 static int btusb_submit_intr_urb(struct hci_dev
*hdev
, gfp_t mem_flags
)
607 struct btusb_data
*data
= hci_get_drvdata(hdev
);
613 BT_DBG("%s", hdev
->name
);
618 urb
= usb_alloc_urb(0, mem_flags
);
622 size
= le16_to_cpu(data
->intr_ep
->wMaxPacketSize
);
624 buf
= kmalloc(size
, mem_flags
);
630 pipe
= usb_rcvintpipe(data
->udev
, data
->intr_ep
->bEndpointAddress
);
632 usb_fill_int_urb(urb
, data
->udev
, pipe
, buf
, size
,
633 btusb_intr_complete
, hdev
, data
->intr_ep
->bInterval
);
635 urb
->transfer_flags
|= URB_FREE_BUFFER
;
637 usb_anchor_urb(urb
, &data
->intr_anchor
);
639 err
= usb_submit_urb(urb
, mem_flags
);
641 if (err
!= -EPERM
&& err
!= -ENODEV
)
642 BT_ERR("%s urb %p submission failed (%d)",
643 hdev
->name
, urb
, -err
);
644 usb_unanchor_urb(urb
);
652 static void btusb_bulk_complete(struct urb
*urb
)
654 struct hci_dev
*hdev
= urb
->context
;
655 struct btusb_data
*data
= hci_get_drvdata(hdev
);
658 BT_DBG("%s urb %p status %d count %d", hdev
->name
, urb
, urb
->status
,
661 if (!test_bit(HCI_RUNNING
, &hdev
->flags
))
664 if (urb
->status
== 0) {
665 hdev
->stat
.byte_rx
+= urb
->actual_length
;
667 if (data
->recv_bulk(data
, urb
->transfer_buffer
,
668 urb
->actual_length
) < 0) {
669 BT_ERR("%s corrupted ACL packet", hdev
->name
);
672 } else if (urb
->status
== -ENOENT
) {
673 /* Avoid suspend failed when usb_kill_urb */
677 if (!test_bit(BTUSB_BULK_RUNNING
, &data
->flags
))
680 usb_anchor_urb(urb
, &data
->bulk_anchor
);
681 usb_mark_last_busy(data
->udev
);
683 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
685 /* -EPERM: urb is being killed;
686 * -ENODEV: device got disconnected */
687 if (err
!= -EPERM
&& err
!= -ENODEV
)
688 BT_ERR("%s urb %p failed to resubmit (%d)",
689 hdev
->name
, urb
, -err
);
690 usb_unanchor_urb(urb
);
694 static int btusb_submit_bulk_urb(struct hci_dev
*hdev
, gfp_t mem_flags
)
696 struct btusb_data
*data
= hci_get_drvdata(hdev
);
700 int err
, size
= HCI_MAX_FRAME_SIZE
;
702 BT_DBG("%s", hdev
->name
);
704 if (!data
->bulk_rx_ep
)
707 urb
= usb_alloc_urb(0, mem_flags
);
711 buf
= kmalloc(size
, mem_flags
);
717 pipe
= usb_rcvbulkpipe(data
->udev
, data
->bulk_rx_ep
->bEndpointAddress
);
719 usb_fill_bulk_urb(urb
, data
->udev
, pipe
, buf
, size
,
720 btusb_bulk_complete
, hdev
);
722 urb
->transfer_flags
|= URB_FREE_BUFFER
;
724 usb_mark_last_busy(data
->udev
);
725 usb_anchor_urb(urb
, &data
->bulk_anchor
);
727 err
= usb_submit_urb(urb
, mem_flags
);
729 if (err
!= -EPERM
&& err
!= -ENODEV
)
730 BT_ERR("%s urb %p submission failed (%d)",
731 hdev
->name
, urb
, -err
);
732 usb_unanchor_urb(urb
);
740 static void btusb_isoc_complete(struct urb
*urb
)
742 struct hci_dev
*hdev
= urb
->context
;
743 struct btusb_data
*data
= hci_get_drvdata(hdev
);
746 BT_DBG("%s urb %p status %d count %d", hdev
->name
, urb
, urb
->status
,
749 if (!test_bit(HCI_RUNNING
, &hdev
->flags
))
752 if (urb
->status
== 0) {
753 for (i
= 0; i
< urb
->number_of_packets
; i
++) {
754 unsigned int offset
= urb
->iso_frame_desc
[i
].offset
;
755 unsigned int length
= urb
->iso_frame_desc
[i
].actual_length
;
757 if (urb
->iso_frame_desc
[i
].status
)
760 hdev
->stat
.byte_rx
+= length
;
762 if (btusb_recv_isoc(data
, urb
->transfer_buffer
+ offset
,
764 BT_ERR("%s corrupted SCO packet", hdev
->name
);
768 } else if (urb
->status
== -ENOENT
) {
769 /* Avoid suspend failed when usb_kill_urb */
773 if (!test_bit(BTUSB_ISOC_RUNNING
, &data
->flags
))
776 usb_anchor_urb(urb
, &data
->isoc_anchor
);
778 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
780 /* -EPERM: urb is being killed;
781 * -ENODEV: device got disconnected */
782 if (err
!= -EPERM
&& err
!= -ENODEV
)
783 BT_ERR("%s urb %p failed to resubmit (%d)",
784 hdev
->name
, urb
, -err
);
785 usb_unanchor_urb(urb
);
789 static inline void __fill_isoc_descriptor(struct urb
*urb
, int len
, int mtu
)
793 BT_DBG("len %d mtu %d", len
, mtu
);
795 for (i
= 0; i
< BTUSB_MAX_ISOC_FRAMES
&& len
>= mtu
;
796 i
++, offset
+= mtu
, len
-= mtu
) {
797 urb
->iso_frame_desc
[i
].offset
= offset
;
798 urb
->iso_frame_desc
[i
].length
= mtu
;
801 if (len
&& i
< BTUSB_MAX_ISOC_FRAMES
) {
802 urb
->iso_frame_desc
[i
].offset
= offset
;
803 urb
->iso_frame_desc
[i
].length
= len
;
807 urb
->number_of_packets
= i
;
810 static int btusb_submit_isoc_urb(struct hci_dev
*hdev
, gfp_t mem_flags
)
812 struct btusb_data
*data
= hci_get_drvdata(hdev
);
818 BT_DBG("%s", hdev
->name
);
820 if (!data
->isoc_rx_ep
)
823 urb
= usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES
, mem_flags
);
827 size
= le16_to_cpu(data
->isoc_rx_ep
->wMaxPacketSize
) *
828 BTUSB_MAX_ISOC_FRAMES
;
830 buf
= kmalloc(size
, mem_flags
);
836 pipe
= usb_rcvisocpipe(data
->udev
, data
->isoc_rx_ep
->bEndpointAddress
);
838 usb_fill_int_urb(urb
, data
->udev
, pipe
, buf
, size
, btusb_isoc_complete
,
839 hdev
, data
->isoc_rx_ep
->bInterval
);
841 urb
->transfer_flags
= URB_FREE_BUFFER
| URB_ISO_ASAP
;
843 __fill_isoc_descriptor(urb
, size
,
844 le16_to_cpu(data
->isoc_rx_ep
->wMaxPacketSize
));
846 usb_anchor_urb(urb
, &data
->isoc_anchor
);
848 err
= usb_submit_urb(urb
, mem_flags
);
850 if (err
!= -EPERM
&& err
!= -ENODEV
)
851 BT_ERR("%s urb %p submission failed (%d)",
852 hdev
->name
, urb
, -err
);
853 usb_unanchor_urb(urb
);
861 static void btusb_tx_complete(struct urb
*urb
)
863 struct sk_buff
*skb
= urb
->context
;
864 struct hci_dev
*hdev
= (struct hci_dev
*)skb
->dev
;
865 struct btusb_data
*data
= hci_get_drvdata(hdev
);
867 BT_DBG("%s urb %p status %d count %d", hdev
->name
, urb
, urb
->status
,
870 if (!test_bit(HCI_RUNNING
, &hdev
->flags
))
874 hdev
->stat
.byte_tx
+= urb
->transfer_buffer_length
;
879 spin_lock(&data
->txlock
);
880 data
->tx_in_flight
--;
881 spin_unlock(&data
->txlock
);
883 kfree(urb
->setup_packet
);
888 static void btusb_isoc_tx_complete(struct urb
*urb
)
890 struct sk_buff
*skb
= urb
->context
;
891 struct hci_dev
*hdev
= (struct hci_dev
*)skb
->dev
;
893 BT_DBG("%s urb %p status %d count %d", hdev
->name
, urb
, urb
->status
,
896 if (!test_bit(HCI_RUNNING
, &hdev
->flags
))
900 hdev
->stat
.byte_tx
+= urb
->transfer_buffer_length
;
905 kfree(urb
->setup_packet
);
910 static int btusb_open(struct hci_dev
*hdev
)
912 struct btusb_data
*data
= hci_get_drvdata(hdev
);
915 BT_DBG("%s", hdev
->name
);
917 /* Patching USB firmware files prior to starting any URBs of HCI path
918 * It is more safe to use USB bulk channel for downloading USB patch
920 if (data
->setup_on_usb
) {
921 err
= data
->setup_on_usb(hdev
);
926 err
= usb_autopm_get_interface(data
->intf
);
930 data
->intf
->needs_remote_wakeup
= 1;
932 if (test_and_set_bit(HCI_RUNNING
, &hdev
->flags
))
935 if (test_and_set_bit(BTUSB_INTR_RUNNING
, &data
->flags
))
938 err
= btusb_submit_intr_urb(hdev
, GFP_KERNEL
);
942 err
= btusb_submit_bulk_urb(hdev
, GFP_KERNEL
);
944 usb_kill_anchored_urbs(&data
->intr_anchor
);
948 set_bit(BTUSB_BULK_RUNNING
, &data
->flags
);
949 btusb_submit_bulk_urb(hdev
, GFP_KERNEL
);
952 usb_autopm_put_interface(data
->intf
);
956 clear_bit(BTUSB_INTR_RUNNING
, &data
->flags
);
957 clear_bit(HCI_RUNNING
, &hdev
->flags
);
958 usb_autopm_put_interface(data
->intf
);
962 static void btusb_stop_traffic(struct btusb_data
*data
)
964 usb_kill_anchored_urbs(&data
->intr_anchor
);
965 usb_kill_anchored_urbs(&data
->bulk_anchor
);
966 usb_kill_anchored_urbs(&data
->isoc_anchor
);
969 static int btusb_close(struct hci_dev
*hdev
)
971 struct btusb_data
*data
= hci_get_drvdata(hdev
);
974 BT_DBG("%s", hdev
->name
);
976 if (!test_and_clear_bit(HCI_RUNNING
, &hdev
->flags
))
979 cancel_work_sync(&data
->work
);
980 cancel_work_sync(&data
->waker
);
982 clear_bit(BTUSB_ISOC_RUNNING
, &data
->flags
);
983 clear_bit(BTUSB_BULK_RUNNING
, &data
->flags
);
984 clear_bit(BTUSB_INTR_RUNNING
, &data
->flags
);
986 btusb_stop_traffic(data
);
987 btusb_free_frags(data
);
989 err
= usb_autopm_get_interface(data
->intf
);
993 data
->intf
->needs_remote_wakeup
= 0;
994 usb_autopm_put_interface(data
->intf
);
997 usb_scuttle_anchored_urbs(&data
->deferred
);
1001 static int btusb_flush(struct hci_dev
*hdev
)
1003 struct btusb_data
*data
= hci_get_drvdata(hdev
);
1005 BT_DBG("%s", hdev
->name
);
1007 usb_kill_anchored_urbs(&data
->tx_anchor
);
1008 btusb_free_frags(data
);
1013 static struct urb
*alloc_ctrl_urb(struct hci_dev
*hdev
, struct sk_buff
*skb
)
1015 struct btusb_data
*data
= hci_get_drvdata(hdev
);
1016 struct usb_ctrlrequest
*dr
;
1020 urb
= usb_alloc_urb(0, GFP_KERNEL
);
1022 return ERR_PTR(-ENOMEM
);
1024 dr
= kmalloc(sizeof(*dr
), GFP_KERNEL
);
1027 return ERR_PTR(-ENOMEM
);
1030 dr
->bRequestType
= data
->cmdreq_type
;
1031 dr
->bRequest
= data
->cmdreq
;
1034 dr
->wLength
= __cpu_to_le16(skb
->len
);
1036 pipe
= usb_sndctrlpipe(data
->udev
, 0x00);
1038 usb_fill_control_urb(urb
, data
->udev
, pipe
, (void *)dr
,
1039 skb
->data
, skb
->len
, btusb_tx_complete
, skb
);
1041 skb
->dev
= (void *)hdev
;
1046 static struct urb
*alloc_bulk_urb(struct hci_dev
*hdev
, struct sk_buff
*skb
)
1048 struct btusb_data
*data
= hci_get_drvdata(hdev
);
1052 if (!data
->bulk_tx_ep
)
1053 return ERR_PTR(-ENODEV
);
1055 urb
= usb_alloc_urb(0, GFP_KERNEL
);
1057 return ERR_PTR(-ENOMEM
);
1059 pipe
= usb_sndbulkpipe(data
->udev
, data
->bulk_tx_ep
->bEndpointAddress
);
1061 usb_fill_bulk_urb(urb
, data
->udev
, pipe
,
1062 skb
->data
, skb
->len
, btusb_tx_complete
, skb
);
1064 skb
->dev
= (void *)hdev
;
1069 static struct urb
*alloc_isoc_urb(struct hci_dev
*hdev
, struct sk_buff
*skb
)
1071 struct btusb_data
*data
= hci_get_drvdata(hdev
);
1075 if (!data
->isoc_tx_ep
)
1076 return ERR_PTR(-ENODEV
);
1078 urb
= usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES
, GFP_KERNEL
);
1080 return ERR_PTR(-ENOMEM
);
1082 pipe
= usb_sndisocpipe(data
->udev
, data
->isoc_tx_ep
->bEndpointAddress
);
1084 usb_fill_int_urb(urb
, data
->udev
, pipe
,
1085 skb
->data
, skb
->len
, btusb_isoc_tx_complete
,
1086 skb
, data
->isoc_tx_ep
->bInterval
);
1088 urb
->transfer_flags
= URB_ISO_ASAP
;
1090 __fill_isoc_descriptor(urb
, skb
->len
,
1091 le16_to_cpu(data
->isoc_tx_ep
->wMaxPacketSize
));
1093 skb
->dev
= (void *)hdev
;
1098 static int submit_tx_urb(struct hci_dev
*hdev
, struct urb
*urb
)
1100 struct btusb_data
*data
= hci_get_drvdata(hdev
);
1103 usb_anchor_urb(urb
, &data
->tx_anchor
);
1105 err
= usb_submit_urb(urb
, GFP_KERNEL
);
1107 if (err
!= -EPERM
&& err
!= -ENODEV
)
1108 BT_ERR("%s urb %p submission failed (%d)",
1109 hdev
->name
, urb
, -err
);
1110 kfree(urb
->setup_packet
);
1111 usb_unanchor_urb(urb
);
1113 usb_mark_last_busy(data
->udev
);
1120 static int submit_or_queue_tx_urb(struct hci_dev
*hdev
, struct urb
*urb
)
1122 struct btusb_data
*data
= hci_get_drvdata(hdev
);
1123 unsigned long flags
;
1126 spin_lock_irqsave(&data
->txlock
, flags
);
1127 suspending
= test_bit(BTUSB_SUSPENDING
, &data
->flags
);
1129 data
->tx_in_flight
++;
1130 spin_unlock_irqrestore(&data
->txlock
, flags
);
1133 return submit_tx_urb(hdev
, urb
);
1135 usb_anchor_urb(urb
, &data
->deferred
);
1136 schedule_work(&data
->waker
);
1142 static int btusb_send_frame(struct hci_dev
*hdev
, struct sk_buff
*skb
)
1146 BT_DBG("%s", hdev
->name
);
1148 if (!test_bit(HCI_RUNNING
, &hdev
->flags
))
1151 switch (bt_cb(skb
)->pkt_type
) {
1152 case HCI_COMMAND_PKT
:
1153 urb
= alloc_ctrl_urb(hdev
, skb
);
1155 return PTR_ERR(urb
);
1157 hdev
->stat
.cmd_tx
++;
1158 return submit_or_queue_tx_urb(hdev
, urb
);
1160 case HCI_ACLDATA_PKT
:
1161 urb
= alloc_bulk_urb(hdev
, skb
);
1163 return PTR_ERR(urb
);
1165 hdev
->stat
.acl_tx
++;
1166 return submit_or_queue_tx_urb(hdev
, urb
);
1168 case HCI_SCODATA_PKT
:
1169 if (hci_conn_num(hdev
, SCO_LINK
) < 1)
1172 urb
= alloc_isoc_urb(hdev
, skb
);
1174 return PTR_ERR(urb
);
1176 hdev
->stat
.sco_tx
++;
1177 return submit_tx_urb(hdev
, urb
);
1183 static void btusb_notify(struct hci_dev
*hdev
, unsigned int evt
)
1185 struct btusb_data
*data
= hci_get_drvdata(hdev
);
1187 BT_DBG("%s evt %d", hdev
->name
, evt
);
1189 if (hci_conn_num(hdev
, SCO_LINK
) != data
->sco_num
) {
1190 data
->sco_num
= hci_conn_num(hdev
, SCO_LINK
);
1191 schedule_work(&data
->work
);
1195 static inline int __set_isoc_interface(struct hci_dev
*hdev
, int altsetting
)
1197 struct btusb_data
*data
= hci_get_drvdata(hdev
);
1198 struct usb_interface
*intf
= data
->isoc
;
1199 struct usb_endpoint_descriptor
*ep_desc
;
1205 err
= usb_set_interface(data
->udev
, 1, altsetting
);
1207 BT_ERR("%s setting interface failed (%d)", hdev
->name
, -err
);
1211 data
->isoc_altsetting
= altsetting
;
1213 data
->isoc_tx_ep
= NULL
;
1214 data
->isoc_rx_ep
= NULL
;
1216 for (i
= 0; i
< intf
->cur_altsetting
->desc
.bNumEndpoints
; i
++) {
1217 ep_desc
= &intf
->cur_altsetting
->endpoint
[i
].desc
;
1219 if (!data
->isoc_tx_ep
&& usb_endpoint_is_isoc_out(ep_desc
)) {
1220 data
->isoc_tx_ep
= ep_desc
;
1224 if (!data
->isoc_rx_ep
&& usb_endpoint_is_isoc_in(ep_desc
)) {
1225 data
->isoc_rx_ep
= ep_desc
;
1230 if (!data
->isoc_tx_ep
|| !data
->isoc_rx_ep
) {
1231 BT_ERR("%s invalid SCO descriptors", hdev
->name
);
1238 static void btusb_work(struct work_struct
*work
)
1240 struct btusb_data
*data
= container_of(work
, struct btusb_data
, work
);
1241 struct hci_dev
*hdev
= data
->hdev
;
1245 if (data
->sco_num
> 0) {
1246 if (!test_bit(BTUSB_DID_ISO_RESUME
, &data
->flags
)) {
1247 err
= usb_autopm_get_interface(data
->isoc
? data
->isoc
: data
->intf
);
1249 clear_bit(BTUSB_ISOC_RUNNING
, &data
->flags
);
1250 usb_kill_anchored_urbs(&data
->isoc_anchor
);
1254 set_bit(BTUSB_DID_ISO_RESUME
, &data
->flags
);
1257 if (hdev
->voice_setting
& 0x0020) {
1258 static const int alts
[3] = { 2, 4, 5 };
1260 new_alts
= alts
[data
->sco_num
- 1];
1262 new_alts
= data
->sco_num
;
1265 if (data
->isoc_altsetting
!= new_alts
) {
1266 clear_bit(BTUSB_ISOC_RUNNING
, &data
->flags
);
1267 usb_kill_anchored_urbs(&data
->isoc_anchor
);
1269 if (__set_isoc_interface(hdev
, new_alts
) < 0)
1273 if (!test_and_set_bit(BTUSB_ISOC_RUNNING
, &data
->flags
)) {
1274 if (btusb_submit_isoc_urb(hdev
, GFP_KERNEL
) < 0)
1275 clear_bit(BTUSB_ISOC_RUNNING
, &data
->flags
);
1277 btusb_submit_isoc_urb(hdev
, GFP_KERNEL
);
1280 clear_bit(BTUSB_ISOC_RUNNING
, &data
->flags
);
1281 usb_kill_anchored_urbs(&data
->isoc_anchor
);
1283 __set_isoc_interface(hdev
, 0);
1284 if (test_and_clear_bit(BTUSB_DID_ISO_RESUME
, &data
->flags
))
1285 usb_autopm_put_interface(data
->isoc
? data
->isoc
: data
->intf
);
1289 static void btusb_waker(struct work_struct
*work
)
1291 struct btusb_data
*data
= container_of(work
, struct btusb_data
, waker
);
1294 err
= usb_autopm_get_interface(data
->intf
);
1298 usb_autopm_put_interface(data
->intf
);
1301 static struct sk_buff
*btusb_read_local_version(struct hci_dev
*hdev
)
1303 struct sk_buff
*skb
;
1305 skb
= __hci_cmd_sync(hdev
, HCI_OP_READ_LOCAL_VERSION
, 0, NULL
,
1308 BT_ERR("%s: HCI_OP_READ_LOCAL_VERSION failed (%ld)",
1309 hdev
->name
, PTR_ERR(skb
));
1313 if (skb
->len
!= sizeof(struct hci_rp_read_local_version
)) {
1314 BT_ERR("%s: HCI_OP_READ_LOCAL_VERSION event length mismatch",
1317 return ERR_PTR(-EIO
);
1323 static int btusb_setup_bcm92035(struct hci_dev
*hdev
)
1325 struct sk_buff
*skb
;
1328 BT_DBG("%s", hdev
->name
);
1330 skb
= __hci_cmd_sync(hdev
, 0xfc3b, 1, &val
, HCI_INIT_TIMEOUT
);
1332 BT_ERR("BCM92035 command failed (%ld)", -PTR_ERR(skb
));
1339 static int btusb_setup_csr(struct hci_dev
*hdev
)
1341 struct hci_rp_read_local_version
*rp
;
1342 struct sk_buff
*skb
;
1345 BT_DBG("%s", hdev
->name
);
1347 skb
= btusb_read_local_version(hdev
);
1349 return -PTR_ERR(skb
);
1351 rp
= (struct hci_rp_read_local_version
*)skb
->data
;
1354 if (le16_to_cpu(rp
->manufacturer
) != 10) {
1355 /* Clear the reset quirk since this is not an actual
1356 * early Bluetooth 1.1 device from CSR.
1358 clear_bit(HCI_QUIRK_RESET_ON_CLOSE
, &hdev
->quirks
);
1360 /* These fake CSR controllers have all a broken
1361 * stored link key handling and so just disable it.
1363 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY
,
1368 ret
= -bt_to_errno(rp
->status
);
1375 #define RTL_FRAG_LEN 252
1377 struct rtl_download_cmd
{
1379 __u8 data
[RTL_FRAG_LEN
];
1382 struct rtl_download_response
{
1387 struct rtl_rom_version_evt
{
1392 struct rtl_epatch_header
{
1398 #define RTL_EPATCH_SIGNATURE "Realtech"
1399 #define RTL_ROM_LMP_3499 0x3499
1400 #define RTL_ROM_LMP_8723A 0x1200
1401 #define RTL_ROM_LMP_8723B 0x8723
1402 #define RTL_ROM_LMP_8821A 0x8821
1403 #define RTL_ROM_LMP_8761A 0x8761
1405 static int rtl_read_rom_version(struct hci_dev
*hdev
, u8
*version
)
1407 struct rtl_rom_version_evt
*rom_version
;
1408 struct sk_buff
*skb
;
1411 /* Read RTL ROM version command */
1412 skb
= __hci_cmd_sync(hdev
, 0xfc6d, 0, NULL
, HCI_INIT_TIMEOUT
);
1414 BT_ERR("%s: Read ROM version failed (%ld)",
1415 hdev
->name
, PTR_ERR(skb
));
1416 return PTR_ERR(skb
);
1419 if (skb
->len
!= sizeof(*rom_version
)) {
1420 BT_ERR("%s: RTL version event length mismatch", hdev
->name
);
1425 rom_version
= (struct rtl_rom_version_evt
*)skb
->data
;
1426 BT_INFO("%s: rom_version status=%x version=%x",
1427 hdev
->name
, rom_version
->status
, rom_version
->version
);
1429 ret
= rom_version
->status
;
1431 *version
= rom_version
->version
;
1437 static int rtl8723b_parse_firmware(struct hci_dev
*hdev
, u16 lmp_subver
,
1438 const struct firmware
*fw
,
1439 unsigned char **_buf
)
1441 const u8 extension_sig
[] = { 0x51, 0x04, 0xfd, 0x77 };
1442 struct rtl_epatch_header
*epatch_info
;
1446 u8 opcode
, length
, data
, rom_version
= 0;
1447 int project_id
= -1;
1448 const unsigned char *fwptr
, *chip_id_base
;
1449 const unsigned char *patch_length_base
, *patch_offset_base
;
1450 u32 patch_offset
= 0;
1451 u16 patch_length
, num_patches
;
1452 const u16 project_id_to_lmp_subver
[] = {
1459 ret
= rtl_read_rom_version(hdev
, &rom_version
);
1461 return -bt_to_errno(ret
);
1463 min_size
= sizeof(struct rtl_epatch_header
) + sizeof(extension_sig
) + 3;
1464 if (fw
->size
< min_size
)
1467 fwptr
= fw
->data
+ fw
->size
- sizeof(extension_sig
);
1468 if (memcmp(fwptr
, extension_sig
, sizeof(extension_sig
)) != 0) {
1469 BT_ERR("%s: extension section signature mismatch", hdev
->name
);
1473 /* Loop from the end of the firmware parsing instructions, until
1474 * we find an instruction that identifies the "project ID" for the
1475 * hardware supported by this firwmare file.
1476 * Once we have that, we double-check that that project_id is suitable
1477 * for the hardware we are working with.
1479 while (fwptr
>= fw
->data
+ (sizeof(struct rtl_epatch_header
) + 3)) {
1484 BT_DBG("check op=%x len=%x data=%x", opcode
, length
, data
);
1486 if (opcode
== 0xff) /* EOF */
1490 BT_ERR("%s: found instruction with length 0",
1495 if (opcode
== 0 && length
== 1) {
1503 if (project_id
< 0) {
1504 BT_ERR("%s: failed to find version instruction", hdev
->name
);
1508 if (project_id
>= ARRAY_SIZE(project_id_to_lmp_subver
)) {
1509 BT_ERR("%s: unknown project id %d", hdev
->name
, project_id
);
1513 if (lmp_subver
!= project_id_to_lmp_subver
[project_id
]) {
1514 BT_ERR("%s: firmware is for %x but this is a %x", hdev
->name
,
1515 project_id_to_lmp_subver
[project_id
], lmp_subver
);
1519 epatch_info
= (struct rtl_epatch_header
*)fw
->data
;
1520 if (memcmp(epatch_info
->signature
, RTL_EPATCH_SIGNATURE
, 8) != 0) {
1521 BT_ERR("%s: bad EPATCH signature", hdev
->name
);
1525 num_patches
= le16_to_cpu(epatch_info
->num_patches
);
1526 BT_DBG("fw_version=%x, num_patches=%d",
1527 le32_to_cpu(epatch_info
->fw_version
), num_patches
);
1529 /* After the rtl_epatch_header there is a funky patch metadata section.
1530 * Assuming 2 patches, the layout is:
1531 * ChipID1 ChipID2 PatchLength1 PatchLength2 PatchOffset1 PatchOffset2
1533 * Find the right patch for this chip.
1535 min_size
+= 8 * num_patches
;
1536 if (fw
->size
< min_size
)
1539 chip_id_base
= fw
->data
+ sizeof(struct rtl_epatch_header
);
1540 patch_length_base
= chip_id_base
+ (sizeof(u16
) * num_patches
);
1541 patch_offset_base
= patch_length_base
+ (sizeof(u16
) * num_patches
);
1542 for (i
= 0; i
< num_patches
; i
++) {
1543 u16 chip_id
= get_unaligned_le16(chip_id_base
+
1545 if (chip_id
== rom_version
+ 1) {
1546 patch_length
= get_unaligned_le16(patch_length_base
+
1548 patch_offset
= get_unaligned_le32(patch_offset_base
+
1554 if (!patch_offset
) {
1555 BT_ERR("%s: didn't find patch for chip id %d",
1556 hdev
->name
, rom_version
);
1560 BT_DBG("length=%x offset=%x index %d", patch_length
, patch_offset
, i
);
1561 min_size
= patch_offset
+ patch_length
;
1562 if (fw
->size
< min_size
)
1565 /* Copy the firmware into a new buffer and write the version at
1569 buf
= kmemdup(fw
->data
+ patch_offset
, patch_length
, GFP_KERNEL
);
1573 memcpy(buf
+ patch_length
- 4, &epatch_info
->fw_version
, 4);
1579 static int rtl_download_firmware(struct hci_dev
*hdev
,
1580 const unsigned char *data
, int fw_len
)
1582 struct rtl_download_cmd
*dl_cmd
;
1583 int frag_num
= fw_len
/ RTL_FRAG_LEN
+ 1;
1584 int frag_len
= RTL_FRAG_LEN
;
1588 dl_cmd
= kmalloc(sizeof(struct rtl_download_cmd
), GFP_KERNEL
);
1592 for (i
= 0; i
< frag_num
; i
++) {
1593 struct rtl_download_response
*dl_resp
;
1594 struct sk_buff
*skb
;
1596 BT_DBG("download fw (%d/%d)", i
, frag_num
);
1599 if (i
== (frag_num
- 1)) {
1600 dl_cmd
->index
|= 0x80; /* data end */
1601 frag_len
= fw_len
% RTL_FRAG_LEN
;
1603 memcpy(dl_cmd
->data
, data
, frag_len
);
1605 /* Send download command */
1606 skb
= __hci_cmd_sync(hdev
, 0xfc20, frag_len
+ 1, dl_cmd
,
1609 BT_ERR("%s: download fw command failed (%ld)",
1610 hdev
->name
, PTR_ERR(skb
));
1611 ret
= -PTR_ERR(skb
);
1615 if (skb
->len
!= sizeof(*dl_resp
)) {
1616 BT_ERR("%s: download fw event length mismatch",
1623 dl_resp
= (struct rtl_download_response
*)skb
->data
;
1624 if (dl_resp
->status
!= 0) {
1626 ret
= bt_to_errno(dl_resp
->status
);
1631 data
+= RTL_FRAG_LEN
;
1639 static int btusb_setup_rtl8723a(struct hci_dev
*hdev
)
1641 struct btusb_data
*data
= dev_get_drvdata(&hdev
->dev
);
1642 struct usb_device
*udev
= interface_to_usbdev(data
->intf
);
1643 const struct firmware
*fw
;
1646 BT_INFO("%s: rtl: loading rtl_bt/rtl8723a_fw.bin", hdev
->name
);
1647 ret
= request_firmware(&fw
, "rtl_bt/rtl8723a_fw.bin", &udev
->dev
);
1649 BT_ERR("%s: Failed to load rtl_bt/rtl8723a_fw.bin", hdev
->name
);
1658 /* Check that the firmware doesn't have the epatch signature
1659 * (which is only for RTL8723B and newer).
1661 if (!memcmp(fw
->data
, RTL_EPATCH_SIGNATURE
, 8)) {
1662 BT_ERR("%s: unexpected EPATCH signature!", hdev
->name
);
1667 ret
= rtl_download_firmware(hdev
, fw
->data
, fw
->size
);
1670 release_firmware(fw
);
1674 static int btusb_setup_rtl8723b(struct hci_dev
*hdev
, u16 lmp_subver
,
1675 const char *fw_name
)
1677 struct btusb_data
*data
= dev_get_drvdata(&hdev
->dev
);
1678 struct usb_device
*udev
= interface_to_usbdev(data
->intf
);
1679 unsigned char *fw_data
= NULL
;
1680 const struct firmware
*fw
;
1683 BT_INFO("%s: rtl: loading %s", hdev
->name
, fw_name
);
1684 ret
= request_firmware(&fw
, fw_name
, &udev
->dev
);
1686 BT_ERR("%s: Failed to load %s", hdev
->name
, fw_name
);
1690 ret
= rtl8723b_parse_firmware(hdev
, lmp_subver
, fw
, &fw_data
);
1694 ret
= rtl_download_firmware(hdev
, fw_data
, ret
);
1700 release_firmware(fw
);
1704 static int btusb_setup_realtek(struct hci_dev
*hdev
)
1706 struct sk_buff
*skb
;
1707 struct hci_rp_read_local_version
*resp
;
1710 skb
= btusb_read_local_version(hdev
);
1712 return -PTR_ERR(skb
);
1714 resp
= (struct hci_rp_read_local_version
*)skb
->data
;
1715 BT_INFO("%s: rtl: examining hci_ver=%02x hci_rev=%04x lmp_ver=%02x "
1716 "lmp_subver=%04x", hdev
->name
, resp
->hci_ver
, resp
->hci_rev
,
1717 resp
->lmp_ver
, resp
->lmp_subver
);
1719 lmp_subver
= le16_to_cpu(resp
->lmp_subver
);
1722 /* Match a set of subver values that correspond to stock firmware,
1723 * which is not compatible with standard btusb.
1724 * If matched, upload an alternative firmware that does conform to
1725 * standard btusb. Once that firmware is uploaded, the subver changes
1726 * to a different value.
1728 switch (lmp_subver
) {
1729 case RTL_ROM_LMP_8723A
:
1730 case RTL_ROM_LMP_3499
:
1731 return btusb_setup_rtl8723a(hdev
);
1732 case RTL_ROM_LMP_8723B
:
1733 return btusb_setup_rtl8723b(hdev
, lmp_subver
,
1734 "rtl_bt/rtl8723b_fw.bin");
1735 case RTL_ROM_LMP_8821A
:
1736 return btusb_setup_rtl8723b(hdev
, lmp_subver
,
1737 "rtl_bt/rtl8821a_fw.bin");
1738 case RTL_ROM_LMP_8761A
:
1739 return btusb_setup_rtl8723b(hdev
, lmp_subver
,
1740 "rtl_bt/rtl8761a_fw.bin");
1742 BT_INFO("rtl: assuming no firmware upload needed.");
1747 static const struct firmware
*btusb_setup_intel_get_fw(struct hci_dev
*hdev
,
1748 struct intel_version
*ver
)
1750 const struct firmware
*fw
;
1754 snprintf(fwname
, sizeof(fwname
),
1755 "intel/ibt-hw-%x.%x.%x-fw-%x.%x.%x.%x.%x.bseq",
1756 ver
->hw_platform
, ver
->hw_variant
, ver
->hw_revision
,
1757 ver
->fw_variant
, ver
->fw_revision
, ver
->fw_build_num
,
1758 ver
->fw_build_ww
, ver
->fw_build_yy
);
1760 ret
= request_firmware(&fw
, fwname
, &hdev
->dev
);
1762 if (ret
== -EINVAL
) {
1763 BT_ERR("%s Intel firmware file request failed (%d)",
1768 BT_ERR("%s failed to open Intel firmware file: %s(%d)",
1769 hdev
->name
, fwname
, ret
);
1771 /* If the correct firmware patch file is not found, use the
1772 * default firmware patch file instead
1774 snprintf(fwname
, sizeof(fwname
), "intel/ibt-hw-%x.%x.bseq",
1775 ver
->hw_platform
, ver
->hw_variant
);
1776 if (request_firmware(&fw
, fwname
, &hdev
->dev
) < 0) {
1777 BT_ERR("%s failed to open default Intel fw file: %s",
1778 hdev
->name
, fwname
);
1783 BT_INFO("%s: Intel Bluetooth firmware file: %s", hdev
->name
, fwname
);
1788 static int btusb_setup_intel_patching(struct hci_dev
*hdev
,
1789 const struct firmware
*fw
,
1790 const u8
**fw_ptr
, int *disable_patch
)
1792 struct sk_buff
*skb
;
1793 struct hci_command_hdr
*cmd
;
1794 const u8
*cmd_param
;
1795 struct hci_event_hdr
*evt
= NULL
;
1796 const u8
*evt_param
= NULL
;
1797 int remain
= fw
->size
- (*fw_ptr
- fw
->data
);
1799 /* The first byte indicates the types of the patch command or event.
1800 * 0x01 means HCI command and 0x02 is HCI event. If the first bytes
1801 * in the current firmware buffer doesn't start with 0x01 or
1802 * the size of remain buffer is smaller than HCI command header,
1803 * the firmware file is corrupted and it should stop the patching
1806 if (remain
> HCI_COMMAND_HDR_SIZE
&& *fw_ptr
[0] != 0x01) {
1807 BT_ERR("%s Intel fw corrupted: invalid cmd read", hdev
->name
);
1813 cmd
= (struct hci_command_hdr
*)(*fw_ptr
);
1814 *fw_ptr
+= sizeof(*cmd
);
1815 remain
-= sizeof(*cmd
);
1817 /* Ensure that the remain firmware data is long enough than the length
1818 * of command parameter. If not, the firmware file is corrupted.
1820 if (remain
< cmd
->plen
) {
1821 BT_ERR("%s Intel fw corrupted: invalid cmd len", hdev
->name
);
1825 /* If there is a command that loads a patch in the firmware
1826 * file, then enable the patch upon success, otherwise just
1827 * disable the manufacturer mode, for example patch activation
1828 * is not required when the default firmware patch file is used
1829 * because there are no patch data to load.
1831 if (*disable_patch
&& le16_to_cpu(cmd
->opcode
) == 0xfc8e)
1834 cmd_param
= *fw_ptr
;
1835 *fw_ptr
+= cmd
->plen
;
1836 remain
-= cmd
->plen
;
1838 /* This reads the expected events when the above command is sent to the
1839 * device. Some vendor commands expects more than one events, for
1840 * example command status event followed by vendor specific event.
1841 * For this case, it only keeps the last expected event. so the command
1842 * can be sent with __hci_cmd_sync_ev() which returns the sk_buff of
1843 * last expected event.
1845 while (remain
> HCI_EVENT_HDR_SIZE
&& *fw_ptr
[0] == 0x02) {
1849 evt
= (struct hci_event_hdr
*)(*fw_ptr
);
1850 *fw_ptr
+= sizeof(*evt
);
1851 remain
-= sizeof(*evt
);
1853 if (remain
< evt
->plen
) {
1854 BT_ERR("%s Intel fw corrupted: invalid evt len",
1859 evt_param
= *fw_ptr
;
1860 *fw_ptr
+= evt
->plen
;
1861 remain
-= evt
->plen
;
1864 /* Every HCI commands in the firmware file has its correspond event.
1865 * If event is not found or remain is smaller than zero, the firmware
1866 * file is corrupted.
1868 if (!evt
|| !evt_param
|| remain
< 0) {
1869 BT_ERR("%s Intel fw corrupted: invalid evt read", hdev
->name
);
1873 skb
= __hci_cmd_sync_ev(hdev
, le16_to_cpu(cmd
->opcode
), cmd
->plen
,
1874 cmd_param
, evt
->evt
, HCI_INIT_TIMEOUT
);
1876 BT_ERR("%s sending Intel patch command (0x%4.4x) failed (%ld)",
1877 hdev
->name
, cmd
->opcode
, PTR_ERR(skb
));
1878 return PTR_ERR(skb
);
1881 /* It ensures that the returned event matches the event data read from
1882 * the firmware file. At fist, it checks the length and then
1883 * the contents of the event.
1885 if (skb
->len
!= evt
->plen
) {
1886 BT_ERR("%s mismatch event length (opcode 0x%4.4x)", hdev
->name
,
1887 le16_to_cpu(cmd
->opcode
));
1892 if (memcmp(skb
->data
, evt_param
, evt
->plen
)) {
1893 BT_ERR("%s mismatch event parameter (opcode 0x%4.4x)",
1894 hdev
->name
, le16_to_cpu(cmd
->opcode
));
1903 static int btusb_setup_intel(struct hci_dev
*hdev
)
1905 struct sk_buff
*skb
;
1906 const struct firmware
*fw
;
1909 struct intel_version
*ver
;
1911 const u8 mfg_enable
[] = { 0x01, 0x00 };
1912 const u8 mfg_disable
[] = { 0x00, 0x00 };
1913 const u8 mfg_reset_deactivate
[] = { 0x00, 0x01 };
1914 const u8 mfg_reset_activate
[] = { 0x00, 0x02 };
1916 BT_DBG("%s", hdev
->name
);
1918 /* The controller has a bug with the first HCI command sent to it
1919 * returning number of completed commands as zero. This would stall the
1920 * command processing in the Bluetooth core.
1922 * As a workaround, send HCI Reset command first which will reset the
1923 * number of completed commands and allow normal command processing
1926 skb
= __hci_cmd_sync(hdev
, HCI_OP_RESET
, 0, NULL
, HCI_INIT_TIMEOUT
);
1928 BT_ERR("%s sending initial HCI reset command failed (%ld)",
1929 hdev
->name
, PTR_ERR(skb
));
1930 return PTR_ERR(skb
);
1934 /* Read Intel specific controller version first to allow selection of
1935 * which firmware file to load.
1937 * The returned information are hardware variant and revision plus
1938 * firmware variant, revision and build number.
1940 skb
= __hci_cmd_sync(hdev
, 0xfc05, 0, NULL
, HCI_INIT_TIMEOUT
);
1942 BT_ERR("%s reading Intel fw version command failed (%ld)",
1943 hdev
->name
, PTR_ERR(skb
));
1944 return PTR_ERR(skb
);
1947 if (skb
->len
!= sizeof(*ver
)) {
1948 BT_ERR("%s Intel version event length mismatch", hdev
->name
);
1953 ver
= (struct intel_version
*)skb
->data
;
1955 BT_ERR("%s Intel fw version event failed (%02x)", hdev
->name
,
1958 return -bt_to_errno(ver
->status
);
1961 BT_INFO("%s: read Intel version: %02x%02x%02x%02x%02x%02x%02x%02x%02x",
1962 hdev
->name
, ver
->hw_platform
, ver
->hw_variant
,
1963 ver
->hw_revision
, ver
->fw_variant
, ver
->fw_revision
,
1964 ver
->fw_build_num
, ver
->fw_build_ww
, ver
->fw_build_yy
,
1967 /* fw_patch_num indicates the version of patch the device currently
1968 * have. If there is no patch data in the device, it is always 0x00.
1969 * So, if it is other than 0x00, no need to patch the deivce again.
1971 if (ver
->fw_patch_num
) {
1972 BT_INFO("%s: Intel device is already patched. patch num: %02x",
1973 hdev
->name
, ver
->fw_patch_num
);
1975 btintel_check_bdaddr(hdev
);
1979 /* Opens the firmware patch file based on the firmware version read
1980 * from the controller. If it fails to open the matching firmware
1981 * patch file, it tries to open the default firmware patch file.
1982 * If no patch file is found, allow the device to operate without
1985 fw
= btusb_setup_intel_get_fw(hdev
, ver
);
1988 btintel_check_bdaddr(hdev
);
1993 /* This Intel specific command enables the manufacturer mode of the
1996 * Only while this mode is enabled, the driver can download the
1997 * firmware patch data and configuration parameters.
1999 skb
= __hci_cmd_sync(hdev
, 0xfc11, 2, mfg_enable
, HCI_INIT_TIMEOUT
);
2001 BT_ERR("%s entering Intel manufacturer mode failed (%ld)",
2002 hdev
->name
, PTR_ERR(skb
));
2003 release_firmware(fw
);
2004 return PTR_ERR(skb
);
2008 u8 evt_status
= skb
->data
[0];
2010 BT_ERR("%s enable Intel manufacturer mode event failed (%02x)",
2011 hdev
->name
, evt_status
);
2013 release_firmware(fw
);
2014 return -bt_to_errno(evt_status
);
2020 /* The firmware data file consists of list of Intel specific HCI
2021 * commands and its expected events. The first byte indicates the
2022 * type of the message, either HCI command or HCI event.
2024 * It reads the command and its expected event from the firmware file,
2025 * and send to the controller. Once __hci_cmd_sync_ev() returns,
2026 * the returned event is compared with the event read from the firmware
2027 * file and it will continue until all the messages are downloaded to
2030 * Once the firmware patching is completed successfully,
2031 * the manufacturer mode is disabled with reset and activating the
2034 * If the firmware patching fails, the manufacturer mode is
2035 * disabled with reset and deactivating the patch.
2037 * If the default patch file is used, no reset is done when disabling
2040 while (fw
->size
> fw_ptr
- fw
->data
) {
2043 ret
= btusb_setup_intel_patching(hdev
, fw
, &fw_ptr
,
2046 goto exit_mfg_deactivate
;
2049 release_firmware(fw
);
2052 goto exit_mfg_disable
;
2054 /* Patching completed successfully and disable the manufacturer mode
2055 * with reset and activate the downloaded firmware patches.
2057 skb
= __hci_cmd_sync(hdev
, 0xfc11, sizeof(mfg_reset_activate
),
2058 mfg_reset_activate
, HCI_INIT_TIMEOUT
);
2060 BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
2061 hdev
->name
, PTR_ERR(skb
));
2062 return PTR_ERR(skb
);
2066 BT_INFO("%s: Intel Bluetooth firmware patch completed and activated",
2069 btintel_check_bdaddr(hdev
);
2073 /* Disable the manufacturer mode without reset */
2074 skb
= __hci_cmd_sync(hdev
, 0xfc11, sizeof(mfg_disable
), mfg_disable
,
2077 BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
2078 hdev
->name
, PTR_ERR(skb
));
2079 return PTR_ERR(skb
);
2083 BT_INFO("%s: Intel Bluetooth firmware patch completed", hdev
->name
);
2085 btintel_check_bdaddr(hdev
);
2088 exit_mfg_deactivate
:
2089 release_firmware(fw
);
2091 /* Patching failed. Disable the manufacturer mode with reset and
2092 * deactivate the downloaded firmware patches.
2094 skb
= __hci_cmd_sync(hdev
, 0xfc11, sizeof(mfg_reset_deactivate
),
2095 mfg_reset_deactivate
, HCI_INIT_TIMEOUT
);
2097 BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
2098 hdev
->name
, PTR_ERR(skb
));
2099 return PTR_ERR(skb
);
2103 BT_INFO("%s: Intel Bluetooth firmware patch completed and deactivated",
2106 btintel_check_bdaddr(hdev
);
2110 static int inject_cmd_complete(struct hci_dev
*hdev
, __u16 opcode
)
2112 struct sk_buff
*skb
;
2113 struct hci_event_hdr
*hdr
;
2114 struct hci_ev_cmd_complete
*evt
;
2116 skb
= bt_skb_alloc(sizeof(*hdr
) + sizeof(*evt
) + 1, GFP_ATOMIC
);
2120 hdr
= (struct hci_event_hdr
*)skb_put(skb
, sizeof(*hdr
));
2121 hdr
->evt
= HCI_EV_CMD_COMPLETE
;
2122 hdr
->plen
= sizeof(*evt
) + 1;
2124 evt
= (struct hci_ev_cmd_complete
*)skb_put(skb
, sizeof(*evt
));
2126 evt
->opcode
= cpu_to_le16(opcode
);
2128 *skb_put(skb
, 1) = 0x00;
2130 bt_cb(skb
)->pkt_type
= HCI_EVENT_PKT
;
2132 return hci_recv_frame(hdev
, skb
);
2135 static int btusb_recv_bulk_intel(struct btusb_data
*data
, void *buffer
,
2138 /* When the device is in bootloader mode, then it can send
2139 * events via the bulk endpoint. These events are treated the
2140 * same way as the ones received from the interrupt endpoint.
2142 if (test_bit(BTUSB_BOOTLOADER
, &data
->flags
))
2143 return btusb_recv_intr(data
, buffer
, count
);
2145 return btusb_recv_bulk(data
, buffer
, count
);
2148 static void btusb_intel_bootup(struct btusb_data
*data
, const void *ptr
,
2151 const struct intel_bootup
*evt
= ptr
;
2153 if (len
!= sizeof(*evt
))
2156 if (test_and_clear_bit(BTUSB_BOOTING
, &data
->flags
)) {
2157 smp_mb__after_atomic();
2158 wake_up_bit(&data
->flags
, BTUSB_BOOTING
);
2162 static void btusb_intel_secure_send_result(struct btusb_data
*data
,
2163 const void *ptr
, unsigned int len
)
2165 const struct intel_secure_send_result
*evt
= ptr
;
2167 if (len
!= sizeof(*evt
))
2171 set_bit(BTUSB_FIRMWARE_FAILED
, &data
->flags
);
2173 if (test_and_clear_bit(BTUSB_DOWNLOADING
, &data
->flags
) &&
2174 test_bit(BTUSB_FIRMWARE_LOADED
, &data
->flags
)) {
2175 smp_mb__after_atomic();
2176 wake_up_bit(&data
->flags
, BTUSB_DOWNLOADING
);
2180 static int btusb_recv_event_intel(struct hci_dev
*hdev
, struct sk_buff
*skb
)
2182 struct btusb_data
*data
= hci_get_drvdata(hdev
);
2184 if (test_bit(BTUSB_BOOTLOADER
, &data
->flags
)) {
2185 struct hci_event_hdr
*hdr
= (void *)skb
->data
;
2187 if (skb
->len
> HCI_EVENT_HDR_SIZE
&& hdr
->evt
== 0xff &&
2189 const void *ptr
= skb
->data
+ HCI_EVENT_HDR_SIZE
+ 1;
2190 unsigned int len
= skb
->len
- HCI_EVENT_HDR_SIZE
- 1;
2192 switch (skb
->data
[2]) {
2194 /* When switching to the operational firmware
2195 * the device sends a vendor specific event
2196 * indicating that the bootup completed.
2198 btusb_intel_bootup(data
, ptr
, len
);
2201 /* When the firmware loading completes the
2202 * device sends out a vendor specific event
2203 * indicating the result of the firmware
2206 btusb_intel_secure_send_result(data
, ptr
, len
);
2212 return hci_recv_frame(hdev
, skb
);
2215 static int btusb_send_frame_intel(struct hci_dev
*hdev
, struct sk_buff
*skb
)
2217 struct btusb_data
*data
= hci_get_drvdata(hdev
);
2220 BT_DBG("%s", hdev
->name
);
2222 if (!test_bit(HCI_RUNNING
, &hdev
->flags
))
2225 switch (bt_cb(skb
)->pkt_type
) {
2226 case HCI_COMMAND_PKT
:
2227 if (test_bit(BTUSB_BOOTLOADER
, &data
->flags
)) {
2228 struct hci_command_hdr
*cmd
= (void *)skb
->data
;
2229 __u16 opcode
= le16_to_cpu(cmd
->opcode
);
2231 /* When in bootloader mode and the command 0xfc09
2232 * is received, it needs to be send down the
2233 * bulk endpoint. So allocate a bulk URB instead.
2235 if (opcode
== 0xfc09)
2236 urb
= alloc_bulk_urb(hdev
, skb
);
2238 urb
= alloc_ctrl_urb(hdev
, skb
);
2240 /* When the 0xfc01 command is issued to boot into
2241 * the operational firmware, it will actually not
2242 * send a command complete event. To keep the flow
2243 * control working inject that event here.
2245 if (opcode
== 0xfc01)
2246 inject_cmd_complete(hdev
, opcode
);
2248 urb
= alloc_ctrl_urb(hdev
, skb
);
2251 return PTR_ERR(urb
);
2253 hdev
->stat
.cmd_tx
++;
2254 return submit_or_queue_tx_urb(hdev
, urb
);
2256 case HCI_ACLDATA_PKT
:
2257 urb
= alloc_bulk_urb(hdev
, skb
);
2259 return PTR_ERR(urb
);
2261 hdev
->stat
.acl_tx
++;
2262 return submit_or_queue_tx_urb(hdev
, urb
);
2264 case HCI_SCODATA_PKT
:
2265 if (hci_conn_num(hdev
, SCO_LINK
) < 1)
2268 urb
= alloc_isoc_urb(hdev
, skb
);
2270 return PTR_ERR(urb
);
2272 hdev
->stat
.sco_tx
++;
2273 return submit_tx_urb(hdev
, urb
);
2279 static int btusb_intel_secure_send(struct hci_dev
*hdev
, u8 fragment_type
,
2280 u32 plen
, const void *param
)
2283 struct sk_buff
*skb
;
2284 u8 cmd_param
[253], fragment_len
= (plen
> 252) ? 252 : plen
;
2286 cmd_param
[0] = fragment_type
;
2287 memcpy(cmd_param
+ 1, param
, fragment_len
);
2289 skb
= __hci_cmd_sync(hdev
, 0xfc09, fragment_len
+ 1,
2290 cmd_param
, HCI_INIT_TIMEOUT
);
2292 return PTR_ERR(skb
);
2296 plen
-= fragment_len
;
2297 param
+= fragment_len
;
2303 static void btusb_intel_version_info(struct hci_dev
*hdev
,
2304 struct intel_version
*ver
)
2306 const char *variant
;
2308 switch (ver
->fw_variant
) {
2310 variant
= "Bootloader";
2313 variant
= "Firmware";
2319 BT_INFO("%s: %s revision %u.%u build %u week %u %u", hdev
->name
,
2320 variant
, ver
->fw_revision
>> 4, ver
->fw_revision
& 0x0f,
2321 ver
->fw_build_num
, ver
->fw_build_ww
, 2000 + ver
->fw_build_yy
);
2324 static int btusb_setup_intel_new(struct hci_dev
*hdev
)
2326 static const u8 reset_param
[] = { 0x00, 0x01, 0x00, 0x01,
2327 0x00, 0x08, 0x04, 0x00 };
2328 struct btusb_data
*data
= hci_get_drvdata(hdev
);
2329 struct sk_buff
*skb
;
2330 struct intel_version
*ver
;
2331 struct intel_boot_params
*params
;
2332 const struct firmware
*fw
;
2335 ktime_t calltime
, delta
, rettime
;
2336 unsigned long long duration
;
2339 BT_DBG("%s", hdev
->name
);
2341 calltime
= ktime_get();
2343 /* Read the Intel version information to determine if the device
2344 * is in bootloader mode or if it already has operational firmware
2347 skb
= __hci_cmd_sync(hdev
, 0xfc05, 0, NULL
, HCI_INIT_TIMEOUT
);
2349 BT_ERR("%s: Reading Intel version information failed (%ld)",
2350 hdev
->name
, PTR_ERR(skb
));
2351 return PTR_ERR(skb
);
2354 if (skb
->len
!= sizeof(*ver
)) {
2355 BT_ERR("%s: Intel version event size mismatch", hdev
->name
);
2360 ver
= (struct intel_version
*)skb
->data
;
2362 BT_ERR("%s: Intel version command failure (%02x)",
2363 hdev
->name
, ver
->status
);
2364 err
= -bt_to_errno(ver
->status
);
2369 /* The hardware platform number has a fixed value of 0x37 and
2370 * for now only accept this single value.
2372 if (ver
->hw_platform
!= 0x37) {
2373 BT_ERR("%s: Unsupported Intel hardware platform (%u)",
2374 hdev
->name
, ver
->hw_platform
);
2379 /* At the moment only the hardware variant iBT 3.0 (LnP/SfP) is
2380 * supported by this firmware loading method. This check has been
2381 * put in place to ensure correct forward compatibility options
2382 * when newer hardware variants come along.
2384 if (ver
->hw_variant
!= 0x0b) {
2385 BT_ERR("%s: Unsupported Intel hardware variant (%u)",
2386 hdev
->name
, ver
->hw_variant
);
2391 btusb_intel_version_info(hdev
, ver
);
2393 /* The firmware variant determines if the device is in bootloader
2394 * mode or is running operational firmware. The value 0x06 identifies
2395 * the bootloader and the value 0x23 identifies the operational
2398 * When the operational firmware is already present, then only
2399 * the check for valid Bluetooth device address is needed. This
2400 * determines if the device will be added as configured or
2401 * unconfigured controller.
2403 * It is not possible to use the Secure Boot Parameters in this
2404 * case since that command is only available in bootloader mode.
2406 if (ver
->fw_variant
== 0x23) {
2408 clear_bit(BTUSB_BOOTLOADER
, &data
->flags
);
2409 btintel_check_bdaddr(hdev
);
2413 /* If the device is not in bootloader mode, then the only possible
2414 * choice is to return an error and abort the device initialization.
2416 if (ver
->fw_variant
!= 0x06) {
2417 BT_ERR("%s: Unsupported Intel firmware variant (%u)",
2418 hdev
->name
, ver
->fw_variant
);
2425 /* Read the secure boot parameters to identify the operating
2426 * details of the bootloader.
2428 skb
= __hci_cmd_sync(hdev
, 0xfc0d, 0, NULL
, HCI_INIT_TIMEOUT
);
2430 BT_ERR("%s: Reading Intel boot parameters failed (%ld)",
2431 hdev
->name
, PTR_ERR(skb
));
2432 return PTR_ERR(skb
);
2435 if (skb
->len
!= sizeof(*params
)) {
2436 BT_ERR("%s: Intel boot parameters size mismatch", hdev
->name
);
2441 params
= (struct intel_boot_params
*)skb
->data
;
2442 if (params
->status
) {
2443 BT_ERR("%s: Intel boot parameters command failure (%02x)",
2444 hdev
->name
, params
->status
);
2445 err
= -bt_to_errno(params
->status
);
2450 BT_INFO("%s: Device revision is %u", hdev
->name
,
2451 le16_to_cpu(params
->dev_revid
));
2453 BT_INFO("%s: Secure boot is %s", hdev
->name
,
2454 params
->secure_boot
? "enabled" : "disabled");
2456 BT_INFO("%s: Minimum firmware build %u week %u %u", hdev
->name
,
2457 params
->min_fw_build_nn
, params
->min_fw_build_cw
,
2458 2000 + params
->min_fw_build_yy
);
2460 /* It is required that every single firmware fragment is acknowledged
2461 * with a command complete event. If the boot parameters indicate
2462 * that this bootloader does not send them, then abort the setup.
2464 if (params
->limited_cce
!= 0x00) {
2465 BT_ERR("%s: Unsupported Intel firmware loading method (%u)",
2466 hdev
->name
, params
->limited_cce
);
2471 /* If the OTP has no valid Bluetooth device address, then there will
2472 * also be no valid address for the operational firmware.
2474 if (!bacmp(¶ms
->otp_bdaddr
, BDADDR_ANY
)) {
2475 BT_INFO("%s: No device address configured", hdev
->name
);
2476 set_bit(HCI_QUIRK_INVALID_BDADDR
, &hdev
->quirks
);
2479 /* With this Intel bootloader only the hardware variant and device
2480 * revision information are used to select the right firmware.
2482 * Currently this bootloader support is limited to hardware variant
2483 * iBT 3.0 (LnP/SfP) which is identified by the value 11 (0x0b).
2485 snprintf(fwname
, sizeof(fwname
), "intel/ibt-11-%u.sfi",
2486 le16_to_cpu(params
->dev_revid
));
2488 err
= request_firmware(&fw
, fwname
, &hdev
->dev
);
2490 BT_ERR("%s: Failed to load Intel firmware file (%d)",
2496 BT_INFO("%s: Found device firmware: %s", hdev
->name
, fwname
);
2500 if (fw
->size
< 644) {
2501 BT_ERR("%s: Invalid size of firmware file (%zu)",
2502 hdev
->name
, fw
->size
);
2507 set_bit(BTUSB_DOWNLOADING
, &data
->flags
);
2509 /* Start the firmware download transaction with the Init fragment
2510 * represented by the 128 bytes of CSS header.
2512 err
= btusb_intel_secure_send(hdev
, 0x00, 128, fw
->data
);
2514 BT_ERR("%s: Failed to send firmware header (%d)",
2519 /* Send the 256 bytes of public key information from the firmware
2520 * as the PKey fragment.
2522 err
= btusb_intel_secure_send(hdev
, 0x03, 256, fw
->data
+ 128);
2524 BT_ERR("%s: Failed to send firmware public key (%d)",
2529 /* Send the 256 bytes of signature information from the firmware
2530 * as the Sign fragment.
2532 err
= btusb_intel_secure_send(hdev
, 0x02, 256, fw
->data
+ 388);
2534 BT_ERR("%s: Failed to send firmware signature (%d)",
2539 fw_ptr
= fw
->data
+ 644;
2541 while (fw_ptr
- fw
->data
< fw
->size
) {
2542 struct hci_command_hdr
*cmd
= (void *)fw_ptr
;
2545 cmd_len
= sizeof(*cmd
) + cmd
->plen
;
2547 /* Send each command from the firmware data buffer as
2548 * a single Data fragment.
2550 err
= btusb_intel_secure_send(hdev
, 0x01, cmd_len
, fw_ptr
);
2552 BT_ERR("%s: Failed to send firmware data (%d)",
2560 set_bit(BTUSB_FIRMWARE_LOADED
, &data
->flags
);
2562 BT_INFO("%s: Waiting for firmware download to complete", hdev
->name
);
2564 /* Before switching the device into operational mode and with that
2565 * booting the loaded firmware, wait for the bootloader notification
2566 * that all fragments have been successfully received.
2568 * When the event processing receives the notification, then the
2569 * BTUSB_DOWNLOADING flag will be cleared.
2571 * The firmware loading should not take longer than 5 seconds
2572 * and thus just timeout if that happens and fail the setup
2575 err
= wait_on_bit_timeout(&data
->flags
, BTUSB_DOWNLOADING
,
2577 msecs_to_jiffies(5000));
2579 BT_ERR("%s: Firmware loading interrupted", hdev
->name
);
2585 BT_ERR("%s: Firmware loading timeout", hdev
->name
);
2590 if (test_bit(BTUSB_FIRMWARE_FAILED
, &data
->flags
)) {
2591 BT_ERR("%s: Firmware loading failed", hdev
->name
);
2596 rettime
= ktime_get();
2597 delta
= ktime_sub(rettime
, calltime
);
2598 duration
= (unsigned long long) ktime_to_ns(delta
) >> 10;
2600 BT_INFO("%s: Firmware loaded in %llu usecs", hdev
->name
, duration
);
2603 release_firmware(fw
);
2608 calltime
= ktime_get();
2610 set_bit(BTUSB_BOOTING
, &data
->flags
);
2612 skb
= __hci_cmd_sync(hdev
, 0xfc01, sizeof(reset_param
), reset_param
,
2615 return PTR_ERR(skb
);
2619 /* The bootloader will not indicate when the device is ready. This
2620 * is done by the operational firmware sending bootup notification.
2622 * Booting into operational firmware should not take longer than
2623 * 1 second. However if that happens, then just fail the setup
2624 * since something went wrong.
2626 BT_INFO("%s: Waiting for device to boot", hdev
->name
);
2628 err
= wait_on_bit_timeout(&data
->flags
, BTUSB_BOOTING
,
2630 msecs_to_jiffies(1000));
2633 BT_ERR("%s: Device boot interrupted", hdev
->name
);
2638 BT_ERR("%s: Device boot timeout", hdev
->name
);
2642 rettime
= ktime_get();
2643 delta
= ktime_sub(rettime
, calltime
);
2644 duration
= (unsigned long long) ktime_to_ns(delta
) >> 10;
2646 BT_INFO("%s: Device booted in %llu usecs", hdev
->name
, duration
);
2648 clear_bit(BTUSB_BOOTLOADER
, &data
->flags
);
2653 static void btusb_hw_error_intel(struct hci_dev
*hdev
, u8 code
)
2655 struct sk_buff
*skb
;
2658 BT_ERR("%s: Hardware error 0x%2.2x", hdev
->name
, code
);
2660 skb
= __hci_cmd_sync(hdev
, HCI_OP_RESET
, 0, NULL
, HCI_INIT_TIMEOUT
);
2662 BT_ERR("%s: Reset after hardware error failed (%ld)",
2663 hdev
->name
, PTR_ERR(skb
));
2668 skb
= __hci_cmd_sync(hdev
, 0xfc22, 1, &type
, HCI_INIT_TIMEOUT
);
2670 BT_ERR("%s: Retrieving Intel exception info failed (%ld)",
2671 hdev
->name
, PTR_ERR(skb
));
2675 if (skb
->len
!= 13) {
2676 BT_ERR("%s: Exception info size mismatch", hdev
->name
);
2681 if (skb
->data
[0] != 0x00) {
2682 BT_ERR("%s: Exception info command failure (%02x)",
2683 hdev
->name
, skb
->data
[0]);
2688 BT_ERR("%s: Exception info %s", hdev
->name
, (char *)(skb
->data
+ 1));
2693 static int btusb_shutdown_intel(struct hci_dev
*hdev
)
2695 struct sk_buff
*skb
;
2698 /* Some platforms have an issue with BT LED when the interface is
2699 * down or BT radio is turned off, which takes 5 seconds to BT LED
2700 * goes off. This command turns off the BT LED immediately.
2702 skb
= __hci_cmd_sync(hdev
, 0xfc3f, 0, NULL
, HCI_INIT_TIMEOUT
);
2705 BT_ERR("%s: turning off Intel device LED failed (%ld)",
2714 static int btusb_set_bdaddr_marvell(struct hci_dev
*hdev
,
2715 const bdaddr_t
*bdaddr
)
2717 struct sk_buff
*skb
;
2722 buf
[1] = sizeof(bdaddr_t
);
2723 memcpy(buf
+ 2, bdaddr
, sizeof(bdaddr_t
));
2725 skb
= __hci_cmd_sync(hdev
, 0xfc22, sizeof(buf
), buf
, HCI_INIT_TIMEOUT
);
2728 BT_ERR("%s: changing Marvell device address failed (%ld)",
2737 static int btusb_set_bdaddr_ath3012(struct hci_dev
*hdev
,
2738 const bdaddr_t
*bdaddr
)
2740 struct sk_buff
*skb
;
2747 buf
[3] = sizeof(bdaddr_t
);
2748 memcpy(buf
+ 4, bdaddr
, sizeof(bdaddr_t
));
2750 skb
= __hci_cmd_sync(hdev
, 0xfc0b, sizeof(buf
), buf
, HCI_INIT_TIMEOUT
);
2753 BT_ERR("%s: Change address command failed (%ld)",
2762 #define QCA_DFU_PACKET_LEN 4096
2764 #define QCA_GET_TARGET_VERSION 0x09
2765 #define QCA_CHECK_STATUS 0x05
2766 #define QCA_DFU_DOWNLOAD 0x01
2768 #define QCA_SYSCFG_UPDATED 0x40
2769 #define QCA_PATCH_UPDATED 0x80
2770 #define QCA_DFU_TIMEOUT 3000
2772 struct qca_version
{
2774 __le32 patch_version
;
2780 struct qca_rampatch_version
{
2782 __le16 patch_version
;
2785 struct qca_device_info
{
2787 u8 rampatch_hdr
; /* length of header in rampatch */
2788 u8 nvm_hdr
; /* length of header in NVM */
2789 u8 ver_offset
; /* offset of version structure in rampatch */
2792 static const struct qca_device_info qca_devices_table
[] = {
2793 { 0x00000100, 20, 4, 10 }, /* Rome 1.0 */
2794 { 0x00000101, 20, 4, 10 }, /* Rome 1.1 */
2795 { 0x00000201, 28, 4, 18 }, /* Rome 2.1 */
2796 { 0x00000300, 28, 4, 18 }, /* Rome 3.0 */
2797 { 0x00000302, 28, 4, 18 }, /* Rome 3.2 */
2800 static int btusb_qca_send_vendor_req(struct hci_dev
*hdev
, u8 request
,
2801 void *data
, u16 size
)
2803 struct btusb_data
*btdata
= hci_get_drvdata(hdev
);
2804 struct usb_device
*udev
= btdata
->udev
;
2808 buf
= kmalloc(size
, GFP_KERNEL
);
2812 /* Found some of USB hosts have IOT issues with ours so that we should
2813 * not wait until HCI layer is ready.
2815 pipe
= usb_rcvctrlpipe(udev
, 0);
2816 err
= usb_control_msg(udev
, pipe
, request
, USB_TYPE_VENDOR
| USB_DIR_IN
,
2817 0, 0, buf
, size
, USB_CTRL_SET_TIMEOUT
);
2819 BT_ERR("%s: Failed to access otp area (%d)", hdev
->name
, err
);
2823 memcpy(data
, buf
, size
);
2831 static int btusb_setup_qca_download_fw(struct hci_dev
*hdev
,
2832 const struct firmware
*firmware
,
2835 struct btusb_data
*btdata
= hci_get_drvdata(hdev
);
2836 struct usb_device
*udev
= btdata
->udev
;
2837 size_t count
, size
, sent
= 0;
2841 buf
= kmalloc(QCA_DFU_PACKET_LEN
, GFP_KERNEL
);
2845 count
= firmware
->size
;
2847 size
= min_t(size_t, count
, hdr_size
);
2848 memcpy(buf
, firmware
->data
, size
);
2850 /* USB patches should go down to controller through USB path
2851 * because binary format fits to go down through USB channel.
2852 * USB control path is for patching headers and USB bulk is for
2855 pipe
= usb_sndctrlpipe(udev
, 0);
2856 err
= usb_control_msg(udev
, pipe
, QCA_DFU_DOWNLOAD
, USB_TYPE_VENDOR
,
2857 0, 0, buf
, size
, USB_CTRL_SET_TIMEOUT
);
2859 BT_ERR("%s: Failed to send headers (%d)", hdev
->name
, err
);
2867 size
= min_t(size_t, count
, QCA_DFU_PACKET_LEN
);
2869 memcpy(buf
, firmware
->data
+ sent
, size
);
2871 pipe
= usb_sndbulkpipe(udev
, 0x02);
2872 err
= usb_bulk_msg(udev
, pipe
, buf
, size
, &len
,
2875 BT_ERR("%s: Failed to send body at %zd of %zd (%d)",
2876 hdev
->name
, sent
, firmware
->size
, err
);
2881 BT_ERR("%s: Failed to get bulk buffer", hdev
->name
);
2895 static int btusb_setup_qca_load_rampatch(struct hci_dev
*hdev
,
2896 struct qca_version
*ver
,
2897 const struct qca_device_info
*info
)
2899 struct qca_rampatch_version
*rver
;
2900 const struct firmware
*fw
;
2901 u32 ver_rom
, ver_patch
;
2902 u16 rver_rom
, rver_patch
;
2906 ver_rom
= le32_to_cpu(ver
->rom_version
);
2907 ver_patch
= le32_to_cpu(ver
->patch_version
);
2909 snprintf(fwname
, sizeof(fwname
), "qca/rampatch_usb_%08x.bin", ver_rom
);
2911 err
= request_firmware(&fw
, fwname
, &hdev
->dev
);
2913 BT_ERR("%s: failed to request rampatch file: %s (%d)",
2914 hdev
->name
, fwname
, err
);
2918 BT_INFO("%s: using rampatch file: %s", hdev
->name
, fwname
);
2920 rver
= (struct qca_rampatch_version
*)(fw
->data
+ info
->ver_offset
);
2921 rver_rom
= le16_to_cpu(rver
->rom_version
);
2922 rver_patch
= le16_to_cpu(rver
->patch_version
);
2924 BT_INFO("%s: QCA: patch rome 0x%x build 0x%x, firmware rome 0x%x "
2925 "build 0x%x", hdev
->name
, rver_rom
, rver_patch
, ver_rom
,
2928 if (rver_rom
!= ver_rom
|| rver_patch
<= ver_patch
) {
2929 BT_ERR("%s: rampatch file version did not match with firmware",
2935 err
= btusb_setup_qca_download_fw(hdev
, fw
, info
->rampatch_hdr
);
2938 release_firmware(fw
);
2943 static int btusb_setup_qca_load_nvm(struct hci_dev
*hdev
,
2944 struct qca_version
*ver
,
2945 const struct qca_device_info
*info
)
2947 const struct firmware
*fw
;
2951 snprintf(fwname
, sizeof(fwname
), "qca/nvm_usb_%08x.bin",
2952 le32_to_cpu(ver
->rom_version
));
2954 err
= request_firmware(&fw
, fwname
, &hdev
->dev
);
2956 BT_ERR("%s: failed to request NVM file: %s (%d)",
2957 hdev
->name
, fwname
, err
);
2961 BT_INFO("%s: using NVM file: %s", hdev
->name
, fwname
);
2963 err
= btusb_setup_qca_download_fw(hdev
, fw
, info
->nvm_hdr
);
2965 release_firmware(fw
);
2970 static int btusb_setup_qca(struct hci_dev
*hdev
)
2972 const struct qca_device_info
*info
= NULL
;
2973 struct qca_version ver
;
2978 err
= btusb_qca_send_vendor_req(hdev
, QCA_GET_TARGET_VERSION
, &ver
,
2983 ver_rom
= le32_to_cpu(ver
.rom_version
);
2984 for (i
= 0; i
< ARRAY_SIZE(qca_devices_table
); i
++) {
2985 if (ver_rom
== qca_devices_table
[i
].rom_version
)
2986 info
= &qca_devices_table
[i
];
2989 BT_ERR("%s: don't support firmware rome 0x%x", hdev
->name
,
2994 err
= btusb_qca_send_vendor_req(hdev
, QCA_CHECK_STATUS
, &status
,
2999 if (!(status
& QCA_PATCH_UPDATED
)) {
3000 err
= btusb_setup_qca_load_rampatch(hdev
, &ver
, info
);
3005 if (!(status
& QCA_SYSCFG_UPDATED
)) {
3006 err
= btusb_setup_qca_load_nvm(hdev
, &ver
, info
);
3014 static int btusb_probe(struct usb_interface
*intf
,
3015 const struct usb_device_id
*id
)
3017 struct usb_endpoint_descriptor
*ep_desc
;
3018 struct btusb_data
*data
;
3019 struct hci_dev
*hdev
;
3022 BT_DBG("intf %p id %p", intf
, id
);
3024 /* interface numbers are hardcoded in the spec */
3025 if (intf
->cur_altsetting
->desc
.bInterfaceNumber
!= 0)
3028 if (!id
->driver_info
) {
3029 const struct usb_device_id
*match
;
3031 match
= usb_match_id(intf
, blacklist_table
);
3036 if (id
->driver_info
== BTUSB_IGNORE
)
3039 if (id
->driver_info
& BTUSB_ATH3012
) {
3040 struct usb_device
*udev
= interface_to_usbdev(intf
);
3042 /* Old firmware would otherwise let ath3k driver load
3043 * patch and sysconfig files */
3044 if (le16_to_cpu(udev
->descriptor
.bcdDevice
) <= 0x0001)
3048 data
= devm_kzalloc(&intf
->dev
, sizeof(*data
), GFP_KERNEL
);
3052 for (i
= 0; i
< intf
->cur_altsetting
->desc
.bNumEndpoints
; i
++) {
3053 ep_desc
= &intf
->cur_altsetting
->endpoint
[i
].desc
;
3055 if (!data
->intr_ep
&& usb_endpoint_is_int_in(ep_desc
)) {
3056 data
->intr_ep
= ep_desc
;
3060 if (!data
->bulk_tx_ep
&& usb_endpoint_is_bulk_out(ep_desc
)) {
3061 data
->bulk_tx_ep
= ep_desc
;
3065 if (!data
->bulk_rx_ep
&& usb_endpoint_is_bulk_in(ep_desc
)) {
3066 data
->bulk_rx_ep
= ep_desc
;
3071 if (!data
->intr_ep
|| !data
->bulk_tx_ep
|| !data
->bulk_rx_ep
)
3074 if (id
->driver_info
& BTUSB_AMP
) {
3075 data
->cmdreq_type
= USB_TYPE_CLASS
| 0x01;
3076 data
->cmdreq
= 0x2b;
3078 data
->cmdreq_type
= USB_TYPE_CLASS
;
3079 data
->cmdreq
= 0x00;
3082 data
->udev
= interface_to_usbdev(intf
);
3085 INIT_WORK(&data
->work
, btusb_work
);
3086 INIT_WORK(&data
->waker
, btusb_waker
);
3087 init_usb_anchor(&data
->deferred
);
3088 init_usb_anchor(&data
->tx_anchor
);
3089 spin_lock_init(&data
->txlock
);
3091 init_usb_anchor(&data
->intr_anchor
);
3092 init_usb_anchor(&data
->bulk_anchor
);
3093 init_usb_anchor(&data
->isoc_anchor
);
3094 spin_lock_init(&data
->rxlock
);
3096 if (id
->driver_info
& BTUSB_INTEL_NEW
) {
3097 data
->recv_event
= btusb_recv_event_intel
;
3098 data
->recv_bulk
= btusb_recv_bulk_intel
;
3099 set_bit(BTUSB_BOOTLOADER
, &data
->flags
);
3101 data
->recv_event
= hci_recv_frame
;
3102 data
->recv_bulk
= btusb_recv_bulk
;
3105 hdev
= hci_alloc_dev();
3109 hdev
->bus
= HCI_USB
;
3110 hci_set_drvdata(hdev
, data
);
3112 if (id
->driver_info
& BTUSB_AMP
)
3113 hdev
->dev_type
= HCI_AMP
;
3115 hdev
->dev_type
= HCI_BREDR
;
3119 SET_HCIDEV_DEV(hdev
, &intf
->dev
);
3121 hdev
->open
= btusb_open
;
3122 hdev
->close
= btusb_close
;
3123 hdev
->flush
= btusb_flush
;
3124 hdev
->send
= btusb_send_frame
;
3125 hdev
->notify
= btusb_notify
;
3127 if (id
->driver_info
& BTUSB_BCM92035
)
3128 hdev
->setup
= btusb_setup_bcm92035
;
3130 #ifdef CONFIG_BT_HCIBTUSB_BCM
3131 if (id
->driver_info
& BTUSB_BCM_PATCHRAM
) {
3132 hdev
->setup
= btbcm_setup_patchram
;
3133 hdev
->set_bdaddr
= btbcm_set_bdaddr
;
3136 if (id
->driver_info
& BTUSB_BCM_APPLE
)
3137 hdev
->setup
= btbcm_setup_apple
;
3140 if (id
->driver_info
& BTUSB_INTEL
) {
3141 hdev
->setup
= btusb_setup_intel
;
3142 hdev
->shutdown
= btusb_shutdown_intel
;
3143 hdev
->set_bdaddr
= btintel_set_bdaddr
;
3144 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER
, &hdev
->quirks
);
3145 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY
, &hdev
->quirks
);
3148 if (id
->driver_info
& BTUSB_INTEL_NEW
) {
3149 hdev
->send
= btusb_send_frame_intel
;
3150 hdev
->setup
= btusb_setup_intel_new
;
3151 hdev
->hw_error
= btusb_hw_error_intel
;
3152 hdev
->set_bdaddr
= btintel_set_bdaddr
;
3153 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER
, &hdev
->quirks
);
3156 if (id
->driver_info
& BTUSB_MARVELL
)
3157 hdev
->set_bdaddr
= btusb_set_bdaddr_marvell
;
3159 if (id
->driver_info
& BTUSB_SWAVE
) {
3160 set_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE
, &hdev
->quirks
);
3161 set_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS
, &hdev
->quirks
);
3164 if (id
->driver_info
& BTUSB_INTEL_BOOT
)
3165 set_bit(HCI_QUIRK_RAW_DEVICE
, &hdev
->quirks
);
3167 if (id
->driver_info
& BTUSB_ATH3012
) {
3168 hdev
->set_bdaddr
= btusb_set_bdaddr_ath3012
;
3169 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY
, &hdev
->quirks
);
3170 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER
, &hdev
->quirks
);
3173 if (id
->driver_info
& BTUSB_QCA_ROME
) {
3174 data
->setup_on_usb
= btusb_setup_qca
;
3175 hdev
->set_bdaddr
= btusb_set_bdaddr_ath3012
;
3178 if (id
->driver_info
& BTUSB_REALTEK
)
3179 hdev
->setup
= btusb_setup_realtek
;
3181 if (id
->driver_info
& BTUSB_AMP
) {
3182 /* AMP controllers do not support SCO packets */
3185 /* Interface numbers are hardcoded in the specification */
3186 data
->isoc
= usb_ifnum_to_if(data
->udev
, 1);
3190 set_bit(HCI_QUIRK_RESET_ON_CLOSE
, &hdev
->quirks
);
3192 if (force_scofix
|| id
->driver_info
& BTUSB_WRONG_SCO_MTU
) {
3193 if (!disable_scofix
)
3194 set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE
, &hdev
->quirks
);
3197 if (id
->driver_info
& BTUSB_BROKEN_ISOC
)
3200 if (id
->driver_info
& BTUSB_DIGIANSWER
) {
3201 data
->cmdreq_type
= USB_TYPE_VENDOR
;
3202 set_bit(HCI_QUIRK_RESET_ON_CLOSE
, &hdev
->quirks
);
3205 if (id
->driver_info
& BTUSB_CSR
) {
3206 struct usb_device
*udev
= data
->udev
;
3207 u16 bcdDevice
= le16_to_cpu(udev
->descriptor
.bcdDevice
);
3209 /* Old firmware would otherwise execute USB reset */
3210 if (bcdDevice
< 0x117)
3211 set_bit(HCI_QUIRK_RESET_ON_CLOSE
, &hdev
->quirks
);
3213 /* Fake CSR devices with broken commands */
3214 if (bcdDevice
<= 0x100)
3215 hdev
->setup
= btusb_setup_csr
;
3217 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY
, &hdev
->quirks
);
3220 if (id
->driver_info
& BTUSB_SNIFFER
) {
3221 struct usb_device
*udev
= data
->udev
;
3223 /* New sniffer firmware has crippled HCI interface */
3224 if (le16_to_cpu(udev
->descriptor
.bcdDevice
) > 0x997)
3225 set_bit(HCI_QUIRK_RAW_DEVICE
, &hdev
->quirks
);
3228 if (id
->driver_info
& BTUSB_INTEL_BOOT
) {
3229 /* A bug in the bootloader causes that interrupt interface is
3230 * only enabled after receiving SetInterface(0, AltSetting=0).
3232 err
= usb_set_interface(data
->udev
, 0, 0);
3234 BT_ERR("failed to set interface 0, alt 0 %d", err
);
3241 err
= usb_driver_claim_interface(&btusb_driver
,
3249 err
= hci_register_dev(hdev
);
3255 usb_set_intfdata(intf
, data
);
3260 static void btusb_disconnect(struct usb_interface
*intf
)
3262 struct btusb_data
*data
= usb_get_intfdata(intf
);
3263 struct hci_dev
*hdev
;
3265 BT_DBG("intf %p", intf
);
3271 usb_set_intfdata(data
->intf
, NULL
);
3274 usb_set_intfdata(data
->isoc
, NULL
);
3276 hci_unregister_dev(hdev
);
3278 if (intf
== data
->isoc
)
3279 usb_driver_release_interface(&btusb_driver
, data
->intf
);
3280 else if (data
->isoc
)
3281 usb_driver_release_interface(&btusb_driver
, data
->isoc
);
3287 static int btusb_suspend(struct usb_interface
*intf
, pm_message_t message
)
3289 struct btusb_data
*data
= usb_get_intfdata(intf
);
3291 BT_DBG("intf %p", intf
);
3293 if (data
->suspend_count
++)
3296 spin_lock_irq(&data
->txlock
);
3297 if (!(PMSG_IS_AUTO(message
) && data
->tx_in_flight
)) {
3298 set_bit(BTUSB_SUSPENDING
, &data
->flags
);
3299 spin_unlock_irq(&data
->txlock
);
3301 spin_unlock_irq(&data
->txlock
);
3302 data
->suspend_count
--;
3306 cancel_work_sync(&data
->work
);
3308 btusb_stop_traffic(data
);
3309 usb_kill_anchored_urbs(&data
->tx_anchor
);
3314 static void play_deferred(struct btusb_data
*data
)
3319 while ((urb
= usb_get_from_anchor(&data
->deferred
))) {
3320 err
= usb_submit_urb(urb
, GFP_ATOMIC
);
3324 data
->tx_in_flight
++;
3326 usb_scuttle_anchored_urbs(&data
->deferred
);
3329 static int btusb_resume(struct usb_interface
*intf
)
3331 struct btusb_data
*data
= usb_get_intfdata(intf
);
3332 struct hci_dev
*hdev
= data
->hdev
;
3335 BT_DBG("intf %p", intf
);
3337 if (--data
->suspend_count
)
3340 if (!test_bit(HCI_RUNNING
, &hdev
->flags
))
3343 if (test_bit(BTUSB_INTR_RUNNING
, &data
->flags
)) {
3344 err
= btusb_submit_intr_urb(hdev
, GFP_NOIO
);
3346 clear_bit(BTUSB_INTR_RUNNING
, &data
->flags
);
3351 if (test_bit(BTUSB_BULK_RUNNING
, &data
->flags
)) {
3352 err
= btusb_submit_bulk_urb(hdev
, GFP_NOIO
);
3354 clear_bit(BTUSB_BULK_RUNNING
, &data
->flags
);
3358 btusb_submit_bulk_urb(hdev
, GFP_NOIO
);
3361 if (test_bit(BTUSB_ISOC_RUNNING
, &data
->flags
)) {
3362 if (btusb_submit_isoc_urb(hdev
, GFP_NOIO
) < 0)
3363 clear_bit(BTUSB_ISOC_RUNNING
, &data
->flags
);
3365 btusb_submit_isoc_urb(hdev
, GFP_NOIO
);
3368 spin_lock_irq(&data
->txlock
);
3369 play_deferred(data
);
3370 clear_bit(BTUSB_SUSPENDING
, &data
->flags
);
3371 spin_unlock_irq(&data
->txlock
);
3372 schedule_work(&data
->work
);
3377 usb_scuttle_anchored_urbs(&data
->deferred
);
3379 spin_lock_irq(&data
->txlock
);
3380 clear_bit(BTUSB_SUSPENDING
, &data
->flags
);
3381 spin_unlock_irq(&data
->txlock
);
3387 static struct usb_driver btusb_driver
= {
3389 .probe
= btusb_probe
,
3390 .disconnect
= btusb_disconnect
,
3392 .suspend
= btusb_suspend
,
3393 .resume
= btusb_resume
,
3395 .id_table
= btusb_table
,
3396 .supports_autosuspend
= 1,
3397 .disable_hub_initiated_lpm
= 1,
3400 module_usb_driver(btusb_driver
);
3402 module_param(disable_scofix
, bool, 0644);
3403 MODULE_PARM_DESC(disable_scofix
, "Disable fixup of wrong SCO buffer size");
3405 module_param(force_scofix
, bool, 0644);
3406 MODULE_PARM_DESC(force_scofix
, "Force fixup of wrong SCO buffers size");
3408 module_param(reset
, bool, 0644);
3409 MODULE_PARM_DESC(reset
, "Send HCI reset command on initialization");
3411 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
3412 MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION
);
3413 MODULE_VERSION(VERSION
);
3414 MODULE_LICENSE("GPL");