HID: hiddev: Fix slab-out-of-bounds write in hiddev_ioctl_usage()
[linux/fpc-iii.git] / drivers / bluetooth / btusb.c
blob7039a58a6a4ec840dd100da97f2bb9ea85957701
1 /*
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/usb/quirks.h>
27 #include <linux/firmware.h>
28 #include <asm/unaligned.h>
30 #include <net/bluetooth/bluetooth.h>
31 #include <net/bluetooth/hci_core.h>
33 #include "btintel.h"
34 #include "btbcm.h"
35 #include "btrtl.h"
37 #define VERSION "0.8"
39 static bool disable_scofix;
40 static bool force_scofix;
42 static bool reset = true;
44 static struct usb_driver btusb_driver;
46 #define BTUSB_IGNORE 0x01
47 #define BTUSB_DIGIANSWER 0x02
48 #define BTUSB_CSR 0x04
49 #define BTUSB_SNIFFER 0x08
50 #define BTUSB_BCM92035 0x10
51 #define BTUSB_BROKEN_ISOC 0x20
52 #define BTUSB_WRONG_SCO_MTU 0x40
53 #define BTUSB_ATH3012 0x80
54 #define BTUSB_INTEL 0x100
55 #define BTUSB_INTEL_BOOT 0x200
56 #define BTUSB_BCM_PATCHRAM 0x400
57 #define BTUSB_MARVELL 0x800
58 #define BTUSB_SWAVE 0x1000
59 #define BTUSB_INTEL_NEW 0x2000
60 #define BTUSB_AMP 0x4000
61 #define BTUSB_QCA_ROME 0x8000
62 #define BTUSB_BCM_APPLE 0x10000
63 #define BTUSB_REALTEK 0x20000
64 #define BTUSB_BCM2045 0x40000
65 #define BTUSB_IFNUM_2 0x80000
67 static const struct usb_device_id btusb_table[] = {
68 /* Generic Bluetooth USB device */
69 { USB_DEVICE_INFO(0xe0, 0x01, 0x01) },
71 /* Generic Bluetooth AMP device */
72 { USB_DEVICE_INFO(0xe0, 0x01, 0x04), .driver_info = BTUSB_AMP },
74 /* Generic Bluetooth USB interface */
75 { USB_INTERFACE_INFO(0xe0, 0x01, 0x01) },
77 /* Apple-specific (Broadcom) devices */
78 { USB_VENDOR_AND_INTERFACE_INFO(0x05ac, 0xff, 0x01, 0x01),
79 .driver_info = BTUSB_BCM_APPLE | BTUSB_IFNUM_2 },
81 /* MediaTek MT76x0E */
82 { USB_DEVICE(0x0e8d, 0x763f) },
84 /* Broadcom SoftSailing reporting vendor specific */
85 { USB_DEVICE(0x0a5c, 0x21e1) },
87 /* Apple MacBookPro 7,1 */
88 { USB_DEVICE(0x05ac, 0x8213) },
90 /* Apple iMac11,1 */
91 { USB_DEVICE(0x05ac, 0x8215) },
93 /* Apple MacBookPro6,2 */
94 { USB_DEVICE(0x05ac, 0x8218) },
96 /* Apple MacBookAir3,1, MacBookAir3,2 */
97 { USB_DEVICE(0x05ac, 0x821b) },
99 /* Apple MacBookAir4,1 */
100 { USB_DEVICE(0x05ac, 0x821f) },
102 /* Apple MacBookPro8,2 */
103 { USB_DEVICE(0x05ac, 0x821a) },
105 /* Apple MacMini5,1 */
106 { USB_DEVICE(0x05ac, 0x8281) },
108 /* AVM BlueFRITZ! USB v2.0 */
109 { USB_DEVICE(0x057c, 0x3800), .driver_info = BTUSB_SWAVE },
111 /* Bluetooth Ultraport Module from IBM */
112 { USB_DEVICE(0x04bf, 0x030a) },
114 /* ALPS Modules with non-standard id */
115 { USB_DEVICE(0x044e, 0x3001) },
116 { USB_DEVICE(0x044e, 0x3002) },
118 /* Ericsson with non-standard id */
119 { USB_DEVICE(0x0bdb, 0x1002) },
121 /* Canyon CN-BTU1 with HID interfaces */
122 { USB_DEVICE(0x0c10, 0x0000) },
124 /* Broadcom BCM20702A0 */
125 { USB_DEVICE(0x413c, 0x8197) },
127 /* Broadcom BCM20702B0 (Dynex/Insignia) */
128 { USB_DEVICE(0x19ff, 0x0239), .driver_info = BTUSB_BCM_PATCHRAM },
130 /* Broadcom BCM43142A0 (Foxconn/Lenovo) */
131 { USB_DEVICE(0x105b, 0xe065), .driver_info = BTUSB_BCM_PATCHRAM },
133 /* Foxconn - Hon Hai */
134 { USB_VENDOR_AND_INTERFACE_INFO(0x0489, 0xff, 0x01, 0x01),
135 .driver_info = BTUSB_BCM_PATCHRAM },
137 /* Lite-On Technology - Broadcom based */
138 { USB_VENDOR_AND_INTERFACE_INFO(0x04ca, 0xff, 0x01, 0x01),
139 .driver_info = BTUSB_BCM_PATCHRAM },
141 /* Broadcom devices with vendor specific id */
142 { USB_VENDOR_AND_INTERFACE_INFO(0x0a5c, 0xff, 0x01, 0x01),
143 .driver_info = BTUSB_BCM_PATCHRAM },
145 /* ASUSTek Computer - Broadcom based */
146 { USB_VENDOR_AND_INTERFACE_INFO(0x0b05, 0xff, 0x01, 0x01),
147 .driver_info = BTUSB_BCM_PATCHRAM },
149 /* Belkin F8065bf - Broadcom based */
150 { USB_VENDOR_AND_INTERFACE_INFO(0x050d, 0xff, 0x01, 0x01),
151 .driver_info = BTUSB_BCM_PATCHRAM },
153 /* IMC Networks - Broadcom based */
154 { USB_VENDOR_AND_INTERFACE_INFO(0x13d3, 0xff, 0x01, 0x01),
155 .driver_info = BTUSB_BCM_PATCHRAM },
157 /* Toshiba Corp - Broadcom based */
158 { USB_VENDOR_AND_INTERFACE_INFO(0x0930, 0xff, 0x01, 0x01),
159 .driver_info = BTUSB_BCM_PATCHRAM },
161 /* Intel Bluetooth USB Bootloader (RAM module) */
162 { USB_DEVICE(0x8087, 0x0a5a),
163 .driver_info = BTUSB_INTEL_BOOT | BTUSB_BROKEN_ISOC },
165 { } /* Terminating entry */
168 MODULE_DEVICE_TABLE(usb, btusb_table);
170 static const struct usb_device_id blacklist_table[] = {
171 /* CSR BlueCore devices */
172 { USB_DEVICE(0x0a12, 0x0001), .driver_info = BTUSB_CSR },
174 /* Broadcom BCM2033 without firmware */
175 { USB_DEVICE(0x0a5c, 0x2033), .driver_info = BTUSB_IGNORE },
177 /* Broadcom BCM2045 devices */
178 { USB_DEVICE(0x0a5c, 0x2045), .driver_info = BTUSB_BCM2045 },
180 /* Atheros 3011 with sflash firmware */
181 { USB_DEVICE(0x0489, 0xe027), .driver_info = BTUSB_IGNORE },
182 { USB_DEVICE(0x0489, 0xe03d), .driver_info = BTUSB_IGNORE },
183 { USB_DEVICE(0x04f2, 0xaff1), .driver_info = BTUSB_IGNORE },
184 { USB_DEVICE(0x0930, 0x0215), .driver_info = BTUSB_IGNORE },
185 { USB_DEVICE(0x0cf3, 0x3002), .driver_info = BTUSB_IGNORE },
186 { USB_DEVICE(0x0cf3, 0xe019), .driver_info = BTUSB_IGNORE },
187 { USB_DEVICE(0x13d3, 0x3304), .driver_info = BTUSB_IGNORE },
189 /* Atheros AR9285 Malbec with sflash firmware */
190 { USB_DEVICE(0x03f0, 0x311d), .driver_info = BTUSB_IGNORE },
192 /* Atheros 3012 with sflash firmware */
193 { USB_DEVICE(0x0489, 0xe04d), .driver_info = BTUSB_ATH3012 },
194 { USB_DEVICE(0x0489, 0xe04e), .driver_info = BTUSB_ATH3012 },
195 { USB_DEVICE(0x0489, 0xe056), .driver_info = BTUSB_ATH3012 },
196 { USB_DEVICE(0x0489, 0xe057), .driver_info = BTUSB_ATH3012 },
197 { USB_DEVICE(0x0489, 0xe05f), .driver_info = BTUSB_ATH3012 },
198 { USB_DEVICE(0x0489, 0xe076), .driver_info = BTUSB_ATH3012 },
199 { USB_DEVICE(0x0489, 0xe078), .driver_info = BTUSB_ATH3012 },
200 { USB_DEVICE(0x0489, 0xe095), .driver_info = BTUSB_ATH3012 },
201 { USB_DEVICE(0x04c5, 0x1330), .driver_info = BTUSB_ATH3012 },
202 { USB_DEVICE(0x04ca, 0x3004), .driver_info = BTUSB_ATH3012 },
203 { USB_DEVICE(0x04ca, 0x3005), .driver_info = BTUSB_ATH3012 },
204 { USB_DEVICE(0x04ca, 0x3006), .driver_info = BTUSB_ATH3012 },
205 { USB_DEVICE(0x04ca, 0x3007), .driver_info = BTUSB_ATH3012 },
206 { USB_DEVICE(0x04ca, 0x3008), .driver_info = BTUSB_ATH3012 },
207 { USB_DEVICE(0x04ca, 0x300b), .driver_info = BTUSB_ATH3012 },
208 { USB_DEVICE(0x04ca, 0x300d), .driver_info = BTUSB_ATH3012 },
209 { USB_DEVICE(0x04ca, 0x300f), .driver_info = BTUSB_ATH3012 },
210 { USB_DEVICE(0x04ca, 0x3010), .driver_info = BTUSB_ATH3012 },
211 { USB_DEVICE(0x04ca, 0x3014), .driver_info = BTUSB_ATH3012 },
212 { USB_DEVICE(0x04ca, 0x3018), .driver_info = BTUSB_ATH3012 },
213 { USB_DEVICE(0x0930, 0x0219), .driver_info = BTUSB_ATH3012 },
214 { USB_DEVICE(0x0930, 0x021c), .driver_info = BTUSB_ATH3012 },
215 { USB_DEVICE(0x0930, 0x0220), .driver_info = BTUSB_ATH3012 },
216 { USB_DEVICE(0x0930, 0x0227), .driver_info = BTUSB_ATH3012 },
217 { USB_DEVICE(0x0b05, 0x17d0), .driver_info = BTUSB_ATH3012 },
218 { USB_DEVICE(0x0cf3, 0x0036), .driver_info = BTUSB_ATH3012 },
219 { USB_DEVICE(0x0cf3, 0x3004), .driver_info = BTUSB_ATH3012 },
220 { USB_DEVICE(0x0cf3, 0x3008), .driver_info = BTUSB_ATH3012 },
221 { USB_DEVICE(0x0cf3, 0x311d), .driver_info = BTUSB_ATH3012 },
222 { USB_DEVICE(0x0cf3, 0x311e), .driver_info = BTUSB_ATH3012 },
223 { USB_DEVICE(0x0cf3, 0x311f), .driver_info = BTUSB_ATH3012 },
224 { USB_DEVICE(0x0cf3, 0x3121), .driver_info = BTUSB_ATH3012 },
225 { USB_DEVICE(0x0cf3, 0x817a), .driver_info = BTUSB_ATH3012 },
226 { USB_DEVICE(0x0cf3, 0x817b), .driver_info = BTUSB_ATH3012 },
227 { USB_DEVICE(0x0cf3, 0xe003), .driver_info = BTUSB_ATH3012 },
228 { USB_DEVICE(0x0cf3, 0xe004), .driver_info = BTUSB_ATH3012 },
229 { USB_DEVICE(0x0cf3, 0xe005), .driver_info = BTUSB_ATH3012 },
230 { USB_DEVICE(0x0cf3, 0xe006), .driver_info = BTUSB_ATH3012 },
231 { USB_DEVICE(0x13d3, 0x3362), .driver_info = BTUSB_ATH3012 },
232 { USB_DEVICE(0x13d3, 0x3375), .driver_info = BTUSB_ATH3012 },
233 { USB_DEVICE(0x13d3, 0x3393), .driver_info = BTUSB_ATH3012 },
234 { USB_DEVICE(0x13d3, 0x3395), .driver_info = BTUSB_ATH3012 },
235 { USB_DEVICE(0x13d3, 0x3402), .driver_info = BTUSB_ATH3012 },
236 { USB_DEVICE(0x13d3, 0x3408), .driver_info = BTUSB_ATH3012 },
237 { USB_DEVICE(0x13d3, 0x3423), .driver_info = BTUSB_ATH3012 },
238 { USB_DEVICE(0x13d3, 0x3432), .driver_info = BTUSB_ATH3012 },
239 { USB_DEVICE(0x13d3, 0x3472), .driver_info = BTUSB_ATH3012 },
240 { USB_DEVICE(0x13d3, 0x3474), .driver_info = BTUSB_ATH3012 },
242 /* Atheros AR5BBU12 with sflash firmware */
243 { USB_DEVICE(0x0489, 0xe02c), .driver_info = BTUSB_IGNORE },
245 /* Atheros AR5BBU12 with sflash firmware */
246 { USB_DEVICE(0x0489, 0xe036), .driver_info = BTUSB_ATH3012 },
247 { USB_DEVICE(0x0489, 0xe03c), .driver_info = BTUSB_ATH3012 },
249 /* QCA ROME chipset */
250 { USB_DEVICE(0x0cf3, 0xe007), .driver_info = BTUSB_QCA_ROME },
251 { USB_DEVICE(0x0cf3, 0xe300), .driver_info = BTUSB_QCA_ROME },
252 { USB_DEVICE(0x0cf3, 0xe360), .driver_info = BTUSB_QCA_ROME },
254 /* Broadcom BCM2035 */
255 { USB_DEVICE(0x0a5c, 0x2009), .driver_info = BTUSB_BCM92035 },
256 { USB_DEVICE(0x0a5c, 0x200a), .driver_info = BTUSB_WRONG_SCO_MTU },
257 { USB_DEVICE(0x0a5c, 0x2035), .driver_info = BTUSB_WRONG_SCO_MTU },
259 /* Broadcom BCM2045 */
260 { USB_DEVICE(0x0a5c, 0x2039), .driver_info = BTUSB_WRONG_SCO_MTU },
261 { USB_DEVICE(0x0a5c, 0x2101), .driver_info = BTUSB_WRONG_SCO_MTU },
263 /* IBM/Lenovo ThinkPad with Broadcom chip */
264 { USB_DEVICE(0x0a5c, 0x201e), .driver_info = BTUSB_WRONG_SCO_MTU },
265 { USB_DEVICE(0x0a5c, 0x2110), .driver_info = BTUSB_WRONG_SCO_MTU },
267 /* HP laptop with Broadcom chip */
268 { USB_DEVICE(0x03f0, 0x171d), .driver_info = BTUSB_WRONG_SCO_MTU },
270 /* Dell laptop with Broadcom chip */
271 { USB_DEVICE(0x413c, 0x8126), .driver_info = BTUSB_WRONG_SCO_MTU },
273 /* Dell Wireless 370 and 410 devices */
274 { USB_DEVICE(0x413c, 0x8152), .driver_info = BTUSB_WRONG_SCO_MTU },
275 { USB_DEVICE(0x413c, 0x8156), .driver_info = BTUSB_WRONG_SCO_MTU },
277 /* Belkin F8T012 and F8T013 devices */
278 { USB_DEVICE(0x050d, 0x0012), .driver_info = BTUSB_WRONG_SCO_MTU },
279 { USB_DEVICE(0x050d, 0x0013), .driver_info = BTUSB_WRONG_SCO_MTU },
281 /* Asus WL-BTD202 device */
282 { USB_DEVICE(0x0b05, 0x1715), .driver_info = BTUSB_WRONG_SCO_MTU },
284 /* Kensington Bluetooth USB adapter */
285 { USB_DEVICE(0x047d, 0x105e), .driver_info = BTUSB_WRONG_SCO_MTU },
287 /* RTX Telecom based adapters with buggy SCO support */
288 { USB_DEVICE(0x0400, 0x0807), .driver_info = BTUSB_BROKEN_ISOC },
289 { USB_DEVICE(0x0400, 0x080a), .driver_info = BTUSB_BROKEN_ISOC },
291 /* CONWISE Technology based adapters with buggy SCO support */
292 { USB_DEVICE(0x0e5e, 0x6622), .driver_info = BTUSB_BROKEN_ISOC },
294 /* Roper Class 1 Bluetooth Dongle (Silicon Wave based) */
295 { USB_DEVICE(0x1310, 0x0001), .driver_info = BTUSB_SWAVE },
297 /* Digianswer devices */
298 { USB_DEVICE(0x08fd, 0x0001), .driver_info = BTUSB_DIGIANSWER },
299 { USB_DEVICE(0x08fd, 0x0002), .driver_info = BTUSB_IGNORE },
301 /* CSR BlueCore Bluetooth Sniffer */
302 { USB_DEVICE(0x0a12, 0x0002),
303 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
305 /* Frontline ComProbe Bluetooth Sniffer */
306 { USB_DEVICE(0x16d3, 0x0002),
307 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
309 /* Marvell Bluetooth devices */
310 { USB_DEVICE(0x1286, 0x2044), .driver_info = BTUSB_MARVELL },
311 { USB_DEVICE(0x1286, 0x2046), .driver_info = BTUSB_MARVELL },
313 /* Intel Bluetooth devices */
314 { USB_DEVICE(0x8087, 0x07da), .driver_info = BTUSB_CSR },
315 { USB_DEVICE(0x8087, 0x07dc), .driver_info = BTUSB_INTEL },
316 { USB_DEVICE(0x8087, 0x0a2a), .driver_info = BTUSB_INTEL },
317 { USB_DEVICE(0x8087, 0x0a2b), .driver_info = BTUSB_INTEL_NEW },
319 /* Other Intel Bluetooth devices */
320 { USB_VENDOR_AND_INTERFACE_INFO(0x8087, 0xe0, 0x01, 0x01),
321 .driver_info = BTUSB_IGNORE },
323 /* Realtek Bluetooth devices */
324 { USB_VENDOR_AND_INTERFACE_INFO(0x0bda, 0xe0, 0x01, 0x01),
325 .driver_info = BTUSB_REALTEK },
327 /* Additional Realtek 8723AE Bluetooth devices */
328 { USB_DEVICE(0x0930, 0x021d), .driver_info = BTUSB_REALTEK },
329 { USB_DEVICE(0x13d3, 0x3394), .driver_info = BTUSB_REALTEK },
331 /* Additional Realtek 8723BE Bluetooth devices */
332 { USB_DEVICE(0x0489, 0xe085), .driver_info = BTUSB_REALTEK },
333 { USB_DEVICE(0x0489, 0xe08b), .driver_info = BTUSB_REALTEK },
334 { USB_DEVICE(0x13d3, 0x3410), .driver_info = BTUSB_REALTEK },
335 { USB_DEVICE(0x13d3, 0x3416), .driver_info = BTUSB_REALTEK },
336 { USB_DEVICE(0x13d3, 0x3459), .driver_info = BTUSB_REALTEK },
337 { USB_DEVICE(0x13d3, 0x3494), .driver_info = BTUSB_REALTEK },
339 /* Additional Realtek 8723BU Bluetooth devices */
340 { USB_DEVICE(0x7392, 0xa611), .driver_info = BTUSB_REALTEK },
342 /* Additional Realtek 8723DE Bluetooth devices */
343 { USB_DEVICE(0x0bda, 0xb009), .driver_info = BTUSB_REALTEK },
344 { USB_DEVICE(0x2ff8, 0xb011), .driver_info = BTUSB_REALTEK },
346 /* Additional Realtek 8821AE Bluetooth devices */
347 { USB_DEVICE(0x0b05, 0x17dc), .driver_info = BTUSB_REALTEK },
348 { USB_DEVICE(0x13d3, 0x3414), .driver_info = BTUSB_REALTEK },
349 { USB_DEVICE(0x13d3, 0x3458), .driver_info = BTUSB_REALTEK },
350 { USB_DEVICE(0x13d3, 0x3461), .driver_info = BTUSB_REALTEK },
351 { USB_DEVICE(0x13d3, 0x3462), .driver_info = BTUSB_REALTEK },
353 /* Additional Realtek 8822BE Bluetooth devices */
354 { USB_DEVICE(0x0b05, 0x185c), .driver_info = BTUSB_REALTEK },
356 /* Additional Realtek 8822CE Bluetooth devices */
357 { USB_DEVICE(0x04ca, 0x4005), .driver_info = BTUSB_REALTEK },
359 /* Silicon Wave based devices */
360 { USB_DEVICE(0x0c10, 0x0000), .driver_info = BTUSB_SWAVE },
362 { } /* Terminating entry */
365 #define BTUSB_MAX_ISOC_FRAMES 10
367 #define BTUSB_INTR_RUNNING 0
368 #define BTUSB_BULK_RUNNING 1
369 #define BTUSB_ISOC_RUNNING 2
370 #define BTUSB_SUSPENDING 3
371 #define BTUSB_DID_ISO_RESUME 4
372 #define BTUSB_BOOTLOADER 5
373 #define BTUSB_DOWNLOADING 6
374 #define BTUSB_FIRMWARE_LOADED 7
375 #define BTUSB_FIRMWARE_FAILED 8
376 #define BTUSB_BOOTING 9
377 #define BTUSB_DIAG_RUNNING 10
378 #define BTUSB_OOB_WAKE_ENABLED 11
380 struct btusb_data {
381 struct hci_dev *hdev;
382 struct usb_device *udev;
383 struct usb_interface *intf;
384 struct usb_interface *isoc;
385 struct usb_interface *diag;
387 unsigned long flags;
389 struct work_struct work;
390 struct work_struct waker;
392 struct usb_anchor deferred;
393 struct usb_anchor tx_anchor;
394 int tx_in_flight;
395 spinlock_t txlock;
397 struct usb_anchor intr_anchor;
398 struct usb_anchor bulk_anchor;
399 struct usb_anchor isoc_anchor;
400 struct usb_anchor diag_anchor;
401 spinlock_t rxlock;
403 struct sk_buff *evt_skb;
404 struct sk_buff *acl_skb;
405 struct sk_buff *sco_skb;
407 struct usb_endpoint_descriptor *intr_ep;
408 struct usb_endpoint_descriptor *bulk_tx_ep;
409 struct usb_endpoint_descriptor *bulk_rx_ep;
410 struct usb_endpoint_descriptor *isoc_tx_ep;
411 struct usb_endpoint_descriptor *isoc_rx_ep;
412 struct usb_endpoint_descriptor *diag_tx_ep;
413 struct usb_endpoint_descriptor *diag_rx_ep;
415 __u8 cmdreq_type;
416 __u8 cmdreq;
418 unsigned int sco_num;
419 int isoc_altsetting;
420 int suspend_count;
422 int (*recv_event)(struct hci_dev *hdev, struct sk_buff *skb);
423 int (*recv_bulk)(struct btusb_data *data, void *buffer, int count);
425 int (*setup_on_usb)(struct hci_dev *hdev);
428 static inline void btusb_free_frags(struct btusb_data *data)
430 unsigned long flags;
432 spin_lock_irqsave(&data->rxlock, flags);
434 kfree_skb(data->evt_skb);
435 data->evt_skb = NULL;
437 kfree_skb(data->acl_skb);
438 data->acl_skb = NULL;
440 kfree_skb(data->sco_skb);
441 data->sco_skb = NULL;
443 spin_unlock_irqrestore(&data->rxlock, flags);
446 static int btusb_recv_intr(struct btusb_data *data, void *buffer, int count)
448 struct sk_buff *skb;
449 int err = 0;
451 spin_lock(&data->rxlock);
452 skb = data->evt_skb;
454 while (count) {
455 int len;
457 if (!skb) {
458 skb = bt_skb_alloc(HCI_MAX_EVENT_SIZE, GFP_ATOMIC);
459 if (!skb) {
460 err = -ENOMEM;
461 break;
464 bt_cb(skb)->pkt_type = HCI_EVENT_PKT;
465 bt_cb(skb)->expect = HCI_EVENT_HDR_SIZE;
468 len = min_t(uint, bt_cb(skb)->expect, count);
469 memcpy(skb_put(skb, len), buffer, len);
471 count -= len;
472 buffer += len;
473 bt_cb(skb)->expect -= len;
475 if (skb->len == HCI_EVENT_HDR_SIZE) {
476 /* Complete event header */
477 bt_cb(skb)->expect = hci_event_hdr(skb)->plen;
479 if (skb_tailroom(skb) < bt_cb(skb)->expect) {
480 kfree_skb(skb);
481 skb = NULL;
483 err = -EILSEQ;
484 break;
488 if (bt_cb(skb)->expect == 0) {
489 /* Complete frame */
490 data->recv_event(data->hdev, skb);
491 skb = NULL;
495 data->evt_skb = skb;
496 spin_unlock(&data->rxlock);
498 return err;
501 static int btusb_recv_bulk(struct btusb_data *data, void *buffer, int count)
503 struct sk_buff *skb;
504 int err = 0;
506 spin_lock(&data->rxlock);
507 skb = data->acl_skb;
509 while (count) {
510 int len;
512 if (!skb) {
513 skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
514 if (!skb) {
515 err = -ENOMEM;
516 break;
519 bt_cb(skb)->pkt_type = HCI_ACLDATA_PKT;
520 bt_cb(skb)->expect = HCI_ACL_HDR_SIZE;
523 len = min_t(uint, bt_cb(skb)->expect, count);
524 memcpy(skb_put(skb, len), buffer, len);
526 count -= len;
527 buffer += len;
528 bt_cb(skb)->expect -= len;
530 if (skb->len == HCI_ACL_HDR_SIZE) {
531 __le16 dlen = hci_acl_hdr(skb)->dlen;
533 /* Complete ACL header */
534 bt_cb(skb)->expect = __le16_to_cpu(dlen);
536 if (skb_tailroom(skb) < bt_cb(skb)->expect) {
537 kfree_skb(skb);
538 skb = NULL;
540 err = -EILSEQ;
541 break;
545 if (bt_cb(skb)->expect == 0) {
546 /* Complete frame */
547 hci_recv_frame(data->hdev, skb);
548 skb = NULL;
552 data->acl_skb = skb;
553 spin_unlock(&data->rxlock);
555 return err;
558 static int btusb_recv_isoc(struct btusb_data *data, void *buffer, int count)
560 struct sk_buff *skb;
561 int err = 0;
563 spin_lock(&data->rxlock);
564 skb = data->sco_skb;
566 while (count) {
567 int len;
569 if (!skb) {
570 skb = bt_skb_alloc(HCI_MAX_SCO_SIZE, GFP_ATOMIC);
571 if (!skb) {
572 err = -ENOMEM;
573 break;
576 bt_cb(skb)->pkt_type = HCI_SCODATA_PKT;
577 bt_cb(skb)->expect = HCI_SCO_HDR_SIZE;
580 len = min_t(uint, bt_cb(skb)->expect, count);
581 memcpy(skb_put(skb, len), buffer, len);
583 count -= len;
584 buffer += len;
585 bt_cb(skb)->expect -= len;
587 if (skb->len == HCI_SCO_HDR_SIZE) {
588 /* Complete SCO header */
589 bt_cb(skb)->expect = hci_sco_hdr(skb)->dlen;
591 if (skb_tailroom(skb) < bt_cb(skb)->expect) {
592 kfree_skb(skb);
593 skb = NULL;
595 err = -EILSEQ;
596 break;
600 if (bt_cb(skb)->expect == 0) {
601 /* Complete frame */
602 hci_recv_frame(data->hdev, skb);
603 skb = NULL;
607 data->sco_skb = skb;
608 spin_unlock(&data->rxlock);
610 return err;
613 static void btusb_intr_complete(struct urb *urb)
615 struct hci_dev *hdev = urb->context;
616 struct btusb_data *data = hci_get_drvdata(hdev);
617 int err;
619 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
620 urb->actual_length);
622 if (!test_bit(HCI_RUNNING, &hdev->flags))
623 return;
625 if (urb->status == 0) {
626 hdev->stat.byte_rx += urb->actual_length;
628 if (btusb_recv_intr(data, urb->transfer_buffer,
629 urb->actual_length) < 0) {
630 BT_ERR("%s corrupted event packet", hdev->name);
631 hdev->stat.err_rx++;
633 } else if (urb->status == -ENOENT) {
634 /* Avoid suspend failed when usb_kill_urb */
635 return;
638 if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
639 return;
641 usb_mark_last_busy(data->udev);
642 usb_anchor_urb(urb, &data->intr_anchor);
644 err = usb_submit_urb(urb, GFP_ATOMIC);
645 if (err < 0) {
646 /* -EPERM: urb is being killed;
647 * -ENODEV: device got disconnected */
648 if (err != -EPERM && err != -ENODEV)
649 BT_ERR("%s urb %p failed to resubmit (%d)",
650 hdev->name, urb, -err);
651 usb_unanchor_urb(urb);
655 static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags)
657 struct btusb_data *data = hci_get_drvdata(hdev);
658 struct urb *urb;
659 unsigned char *buf;
660 unsigned int pipe;
661 int err, size;
663 BT_DBG("%s", hdev->name);
665 if (!data->intr_ep)
666 return -ENODEV;
668 urb = usb_alloc_urb(0, mem_flags);
669 if (!urb)
670 return -ENOMEM;
672 size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
674 buf = kmalloc(size, mem_flags);
675 if (!buf) {
676 usb_free_urb(urb);
677 return -ENOMEM;
680 pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
682 usb_fill_int_urb(urb, data->udev, pipe, buf, size,
683 btusb_intr_complete, hdev, data->intr_ep->bInterval);
685 urb->transfer_flags |= URB_FREE_BUFFER;
687 usb_anchor_urb(urb, &data->intr_anchor);
689 err = usb_submit_urb(urb, mem_flags);
690 if (err < 0) {
691 if (err != -EPERM && err != -ENODEV)
692 BT_ERR("%s urb %p submission failed (%d)",
693 hdev->name, urb, -err);
694 usb_unanchor_urb(urb);
697 usb_free_urb(urb);
699 return err;
702 static void btusb_bulk_complete(struct urb *urb)
704 struct hci_dev *hdev = urb->context;
705 struct btusb_data *data = hci_get_drvdata(hdev);
706 int err;
708 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
709 urb->actual_length);
711 if (!test_bit(HCI_RUNNING, &hdev->flags))
712 return;
714 if (urb->status == 0) {
715 hdev->stat.byte_rx += urb->actual_length;
717 if (data->recv_bulk(data, urb->transfer_buffer,
718 urb->actual_length) < 0) {
719 BT_ERR("%s corrupted ACL packet", hdev->name);
720 hdev->stat.err_rx++;
722 } else if (urb->status == -ENOENT) {
723 /* Avoid suspend failed when usb_kill_urb */
724 return;
727 if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
728 return;
730 usb_anchor_urb(urb, &data->bulk_anchor);
731 usb_mark_last_busy(data->udev);
733 err = usb_submit_urb(urb, GFP_ATOMIC);
734 if (err < 0) {
735 /* -EPERM: urb is being killed;
736 * -ENODEV: device got disconnected */
737 if (err != -EPERM && err != -ENODEV)
738 BT_ERR("%s urb %p failed to resubmit (%d)",
739 hdev->name, urb, -err);
740 usb_unanchor_urb(urb);
744 static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
746 struct btusb_data *data = hci_get_drvdata(hdev);
747 struct urb *urb;
748 unsigned char *buf;
749 unsigned int pipe;
750 int err, size = HCI_MAX_FRAME_SIZE;
752 BT_DBG("%s", hdev->name);
754 if (!data->bulk_rx_ep)
755 return -ENODEV;
757 urb = usb_alloc_urb(0, mem_flags);
758 if (!urb)
759 return -ENOMEM;
761 buf = kmalloc(size, mem_flags);
762 if (!buf) {
763 usb_free_urb(urb);
764 return -ENOMEM;
767 pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
769 usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
770 btusb_bulk_complete, hdev);
772 urb->transfer_flags |= URB_FREE_BUFFER;
774 usb_mark_last_busy(data->udev);
775 usb_anchor_urb(urb, &data->bulk_anchor);
777 err = usb_submit_urb(urb, mem_flags);
778 if (err < 0) {
779 if (err != -EPERM && err != -ENODEV)
780 BT_ERR("%s urb %p submission failed (%d)",
781 hdev->name, urb, -err);
782 usb_unanchor_urb(urb);
785 usb_free_urb(urb);
787 return err;
790 static void btusb_isoc_complete(struct urb *urb)
792 struct hci_dev *hdev = urb->context;
793 struct btusb_data *data = hci_get_drvdata(hdev);
794 int i, err;
796 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
797 urb->actual_length);
799 if (!test_bit(HCI_RUNNING, &hdev->flags))
800 return;
802 if (urb->status == 0) {
803 for (i = 0; i < urb->number_of_packets; i++) {
804 unsigned int offset = urb->iso_frame_desc[i].offset;
805 unsigned int length = urb->iso_frame_desc[i].actual_length;
807 if (urb->iso_frame_desc[i].status)
808 continue;
810 hdev->stat.byte_rx += length;
812 if (btusb_recv_isoc(data, urb->transfer_buffer + offset,
813 length) < 0) {
814 BT_ERR("%s corrupted SCO packet", hdev->name);
815 hdev->stat.err_rx++;
818 } else if (urb->status == -ENOENT) {
819 /* Avoid suspend failed when usb_kill_urb */
820 return;
823 if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags))
824 return;
826 usb_anchor_urb(urb, &data->isoc_anchor);
828 err = usb_submit_urb(urb, GFP_ATOMIC);
829 if (err < 0) {
830 /* -EPERM: urb is being killed;
831 * -ENODEV: device got disconnected */
832 if (err != -EPERM && err != -ENODEV)
833 BT_ERR("%s urb %p failed to resubmit (%d)",
834 hdev->name, urb, -err);
835 usb_unanchor_urb(urb);
839 static inline void __fill_isoc_descriptor(struct urb *urb, int len, int mtu)
841 int i, offset = 0;
843 BT_DBG("len %d mtu %d", len, mtu);
845 for (i = 0; i < BTUSB_MAX_ISOC_FRAMES && len >= mtu;
846 i++, offset += mtu, len -= mtu) {
847 urb->iso_frame_desc[i].offset = offset;
848 urb->iso_frame_desc[i].length = mtu;
851 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
852 urb->iso_frame_desc[i].offset = offset;
853 urb->iso_frame_desc[i].length = len;
854 i++;
857 urb->number_of_packets = i;
860 static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags)
862 struct btusb_data *data = hci_get_drvdata(hdev);
863 struct urb *urb;
864 unsigned char *buf;
865 unsigned int pipe;
866 int err, size;
868 BT_DBG("%s", hdev->name);
870 if (!data->isoc_rx_ep)
871 return -ENODEV;
873 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags);
874 if (!urb)
875 return -ENOMEM;
877 size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) *
878 BTUSB_MAX_ISOC_FRAMES;
880 buf = kmalloc(size, mem_flags);
881 if (!buf) {
882 usb_free_urb(urb);
883 return -ENOMEM;
886 pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress);
888 usb_fill_int_urb(urb, data->udev, pipe, buf, size, btusb_isoc_complete,
889 hdev, data->isoc_rx_ep->bInterval);
891 urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP;
893 __fill_isoc_descriptor(urb, size,
894 le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize));
896 usb_anchor_urb(urb, &data->isoc_anchor);
898 err = usb_submit_urb(urb, mem_flags);
899 if (err < 0) {
900 if (err != -EPERM && err != -ENODEV)
901 BT_ERR("%s urb %p submission failed (%d)",
902 hdev->name, urb, -err);
903 usb_unanchor_urb(urb);
906 usb_free_urb(urb);
908 return err;
911 static void btusb_diag_complete(struct urb *urb)
913 struct hci_dev *hdev = urb->context;
914 struct btusb_data *data = hci_get_drvdata(hdev);
915 int err;
917 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
918 urb->actual_length);
920 if (urb->status == 0) {
921 struct sk_buff *skb;
923 skb = bt_skb_alloc(urb->actual_length, GFP_ATOMIC);
924 if (skb) {
925 memcpy(skb_put(skb, urb->actual_length),
926 urb->transfer_buffer, urb->actual_length);
927 hci_recv_diag(hdev, skb);
929 } else if (urb->status == -ENOENT) {
930 /* Avoid suspend failed when usb_kill_urb */
931 return;
934 if (!test_bit(BTUSB_DIAG_RUNNING, &data->flags))
935 return;
937 usb_anchor_urb(urb, &data->diag_anchor);
938 usb_mark_last_busy(data->udev);
940 err = usb_submit_urb(urb, GFP_ATOMIC);
941 if (err < 0) {
942 /* -EPERM: urb is being killed;
943 * -ENODEV: device got disconnected */
944 if (err != -EPERM && err != -ENODEV)
945 BT_ERR("%s urb %p failed to resubmit (%d)",
946 hdev->name, urb, -err);
947 usb_unanchor_urb(urb);
951 static int btusb_submit_diag_urb(struct hci_dev *hdev, gfp_t mem_flags)
953 struct btusb_data *data = hci_get_drvdata(hdev);
954 struct urb *urb;
955 unsigned char *buf;
956 unsigned int pipe;
957 int err, size = HCI_MAX_FRAME_SIZE;
959 BT_DBG("%s", hdev->name);
961 if (!data->diag_rx_ep)
962 return -ENODEV;
964 urb = usb_alloc_urb(0, mem_flags);
965 if (!urb)
966 return -ENOMEM;
968 buf = kmalloc(size, mem_flags);
969 if (!buf) {
970 usb_free_urb(urb);
971 return -ENOMEM;
974 pipe = usb_rcvbulkpipe(data->udev, data->diag_rx_ep->bEndpointAddress);
976 usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
977 btusb_diag_complete, hdev);
979 urb->transfer_flags |= URB_FREE_BUFFER;
981 usb_mark_last_busy(data->udev);
982 usb_anchor_urb(urb, &data->diag_anchor);
984 err = usb_submit_urb(urb, mem_flags);
985 if (err < 0) {
986 if (err != -EPERM && err != -ENODEV)
987 BT_ERR("%s urb %p submission failed (%d)",
988 hdev->name, urb, -err);
989 usb_unanchor_urb(urb);
992 usb_free_urb(urb);
994 return err;
997 static void btusb_tx_complete(struct urb *urb)
999 struct sk_buff *skb = urb->context;
1000 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
1001 struct btusb_data *data = hci_get_drvdata(hdev);
1003 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1004 urb->actual_length);
1006 if (!test_bit(HCI_RUNNING, &hdev->flags))
1007 goto done;
1009 if (!urb->status)
1010 hdev->stat.byte_tx += urb->transfer_buffer_length;
1011 else
1012 hdev->stat.err_tx++;
1014 done:
1015 spin_lock(&data->txlock);
1016 data->tx_in_flight--;
1017 spin_unlock(&data->txlock);
1019 kfree(urb->setup_packet);
1021 kfree_skb(skb);
1024 static void btusb_isoc_tx_complete(struct urb *urb)
1026 struct sk_buff *skb = urb->context;
1027 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
1029 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1030 urb->actual_length);
1032 if (!test_bit(HCI_RUNNING, &hdev->flags))
1033 goto done;
1035 if (!urb->status)
1036 hdev->stat.byte_tx += urb->transfer_buffer_length;
1037 else
1038 hdev->stat.err_tx++;
1040 done:
1041 kfree(urb->setup_packet);
1043 kfree_skb(skb);
1046 static int btusb_open(struct hci_dev *hdev)
1048 struct btusb_data *data = hci_get_drvdata(hdev);
1049 int err;
1051 BT_DBG("%s", hdev->name);
1053 /* Patching USB firmware files prior to starting any URBs of HCI path
1054 * It is more safe to use USB bulk channel for downloading USB patch
1056 if (data->setup_on_usb) {
1057 err = data->setup_on_usb(hdev);
1058 if (err < 0)
1059 goto setup_fail;
1062 err = usb_autopm_get_interface(data->intf);
1063 if (err < 0)
1064 return err;
1066 data->intf->needs_remote_wakeup = 1;
1068 if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
1069 goto done;
1071 err = btusb_submit_intr_urb(hdev, GFP_KERNEL);
1072 if (err < 0)
1073 goto failed;
1075 err = btusb_submit_bulk_urb(hdev, GFP_KERNEL);
1076 if (err < 0) {
1077 usb_kill_anchored_urbs(&data->intr_anchor);
1078 goto failed;
1081 set_bit(BTUSB_BULK_RUNNING, &data->flags);
1082 btusb_submit_bulk_urb(hdev, GFP_KERNEL);
1084 if (data->diag) {
1085 if (!btusb_submit_diag_urb(hdev, GFP_KERNEL))
1086 set_bit(BTUSB_DIAG_RUNNING, &data->flags);
1089 done:
1090 usb_autopm_put_interface(data->intf);
1091 return 0;
1093 failed:
1094 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
1095 setup_fail:
1096 usb_autopm_put_interface(data->intf);
1097 return err;
1100 static void btusb_stop_traffic(struct btusb_data *data)
1102 usb_kill_anchored_urbs(&data->intr_anchor);
1103 usb_kill_anchored_urbs(&data->bulk_anchor);
1104 usb_kill_anchored_urbs(&data->isoc_anchor);
1105 usb_kill_anchored_urbs(&data->diag_anchor);
1108 static int btusb_close(struct hci_dev *hdev)
1110 struct btusb_data *data = hci_get_drvdata(hdev);
1111 int err;
1113 BT_DBG("%s", hdev->name);
1115 cancel_work_sync(&data->work);
1116 cancel_work_sync(&data->waker);
1118 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1119 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
1120 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
1121 clear_bit(BTUSB_DIAG_RUNNING, &data->flags);
1123 btusb_stop_traffic(data);
1124 btusb_free_frags(data);
1126 err = usb_autopm_get_interface(data->intf);
1127 if (err < 0)
1128 goto failed;
1130 data->intf->needs_remote_wakeup = 0;
1131 usb_autopm_put_interface(data->intf);
1133 failed:
1134 usb_scuttle_anchored_urbs(&data->deferred);
1135 return 0;
1138 static int btusb_flush(struct hci_dev *hdev)
1140 struct btusb_data *data = hci_get_drvdata(hdev);
1142 BT_DBG("%s", hdev->name);
1144 usb_kill_anchored_urbs(&data->tx_anchor);
1145 btusb_free_frags(data);
1147 return 0;
1150 static struct urb *alloc_ctrl_urb(struct hci_dev *hdev, struct sk_buff *skb)
1152 struct btusb_data *data = hci_get_drvdata(hdev);
1153 struct usb_ctrlrequest *dr;
1154 struct urb *urb;
1155 unsigned int pipe;
1157 urb = usb_alloc_urb(0, GFP_KERNEL);
1158 if (!urb)
1159 return ERR_PTR(-ENOMEM);
1161 dr = kmalloc(sizeof(*dr), GFP_KERNEL);
1162 if (!dr) {
1163 usb_free_urb(urb);
1164 return ERR_PTR(-ENOMEM);
1167 dr->bRequestType = data->cmdreq_type;
1168 dr->bRequest = data->cmdreq;
1169 dr->wIndex = 0;
1170 dr->wValue = 0;
1171 dr->wLength = __cpu_to_le16(skb->len);
1173 pipe = usb_sndctrlpipe(data->udev, 0x00);
1175 usb_fill_control_urb(urb, data->udev, pipe, (void *)dr,
1176 skb->data, skb->len, btusb_tx_complete, skb);
1178 skb->dev = (void *)hdev;
1180 return urb;
1183 static struct urb *alloc_bulk_urb(struct hci_dev *hdev, struct sk_buff *skb)
1185 struct btusb_data *data = hci_get_drvdata(hdev);
1186 struct urb *urb;
1187 unsigned int pipe;
1189 if (!data->bulk_tx_ep)
1190 return ERR_PTR(-ENODEV);
1192 urb = usb_alloc_urb(0, GFP_KERNEL);
1193 if (!urb)
1194 return ERR_PTR(-ENOMEM);
1196 pipe = usb_sndbulkpipe(data->udev, data->bulk_tx_ep->bEndpointAddress);
1198 usb_fill_bulk_urb(urb, data->udev, pipe,
1199 skb->data, skb->len, btusb_tx_complete, skb);
1201 skb->dev = (void *)hdev;
1203 return urb;
1206 static struct urb *alloc_isoc_urb(struct hci_dev *hdev, struct sk_buff *skb)
1208 struct btusb_data *data = hci_get_drvdata(hdev);
1209 struct urb *urb;
1210 unsigned int pipe;
1212 if (!data->isoc_tx_ep)
1213 return ERR_PTR(-ENODEV);
1215 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_KERNEL);
1216 if (!urb)
1217 return ERR_PTR(-ENOMEM);
1219 pipe = usb_sndisocpipe(data->udev, data->isoc_tx_ep->bEndpointAddress);
1221 usb_fill_int_urb(urb, data->udev, pipe,
1222 skb->data, skb->len, btusb_isoc_tx_complete,
1223 skb, data->isoc_tx_ep->bInterval);
1225 urb->transfer_flags = URB_ISO_ASAP;
1227 __fill_isoc_descriptor(urb, skb->len,
1228 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
1230 skb->dev = (void *)hdev;
1232 return urb;
1235 static int submit_tx_urb(struct hci_dev *hdev, struct urb *urb)
1237 struct btusb_data *data = hci_get_drvdata(hdev);
1238 int err;
1240 usb_anchor_urb(urb, &data->tx_anchor);
1242 err = usb_submit_urb(urb, GFP_KERNEL);
1243 if (err < 0) {
1244 if (err != -EPERM && err != -ENODEV)
1245 BT_ERR("%s urb %p submission failed (%d)",
1246 hdev->name, urb, -err);
1247 kfree(urb->setup_packet);
1248 usb_unanchor_urb(urb);
1249 } else {
1250 usb_mark_last_busy(data->udev);
1253 usb_free_urb(urb);
1254 return err;
1257 static int submit_or_queue_tx_urb(struct hci_dev *hdev, struct urb *urb)
1259 struct btusb_data *data = hci_get_drvdata(hdev);
1260 unsigned long flags;
1261 bool suspending;
1263 spin_lock_irqsave(&data->txlock, flags);
1264 suspending = test_bit(BTUSB_SUSPENDING, &data->flags);
1265 if (!suspending)
1266 data->tx_in_flight++;
1267 spin_unlock_irqrestore(&data->txlock, flags);
1269 if (!suspending)
1270 return submit_tx_urb(hdev, urb);
1272 usb_anchor_urb(urb, &data->deferred);
1273 schedule_work(&data->waker);
1275 usb_free_urb(urb);
1276 return 0;
1279 static int btusb_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
1281 struct urb *urb;
1283 BT_DBG("%s", hdev->name);
1285 switch (bt_cb(skb)->pkt_type) {
1286 case HCI_COMMAND_PKT:
1287 urb = alloc_ctrl_urb(hdev, skb);
1288 if (IS_ERR(urb))
1289 return PTR_ERR(urb);
1291 hdev->stat.cmd_tx++;
1292 return submit_or_queue_tx_urb(hdev, urb);
1294 case HCI_ACLDATA_PKT:
1295 urb = alloc_bulk_urb(hdev, skb);
1296 if (IS_ERR(urb))
1297 return PTR_ERR(urb);
1299 hdev->stat.acl_tx++;
1300 return submit_or_queue_tx_urb(hdev, urb);
1302 case HCI_SCODATA_PKT:
1303 if (hci_conn_num(hdev, SCO_LINK) < 1)
1304 return -ENODEV;
1306 urb = alloc_isoc_urb(hdev, skb);
1307 if (IS_ERR(urb))
1308 return PTR_ERR(urb);
1310 hdev->stat.sco_tx++;
1311 return submit_tx_urb(hdev, urb);
1314 return -EILSEQ;
1317 static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
1319 struct btusb_data *data = hci_get_drvdata(hdev);
1321 BT_DBG("%s evt %d", hdev->name, evt);
1323 if (hci_conn_num(hdev, SCO_LINK) != data->sco_num) {
1324 data->sco_num = hci_conn_num(hdev, SCO_LINK);
1325 schedule_work(&data->work);
1329 static inline int __set_isoc_interface(struct hci_dev *hdev, int altsetting)
1331 struct btusb_data *data = hci_get_drvdata(hdev);
1332 struct usb_interface *intf = data->isoc;
1333 struct usb_endpoint_descriptor *ep_desc;
1334 int i, err;
1336 if (!data->isoc)
1337 return -ENODEV;
1339 err = usb_set_interface(data->udev, 1, altsetting);
1340 if (err < 0) {
1341 BT_ERR("%s setting interface failed (%d)", hdev->name, -err);
1342 return err;
1345 data->isoc_altsetting = altsetting;
1347 data->isoc_tx_ep = NULL;
1348 data->isoc_rx_ep = NULL;
1350 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
1351 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
1353 if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) {
1354 data->isoc_tx_ep = ep_desc;
1355 continue;
1358 if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) {
1359 data->isoc_rx_ep = ep_desc;
1360 continue;
1364 if (!data->isoc_tx_ep || !data->isoc_rx_ep) {
1365 BT_ERR("%s invalid SCO descriptors", hdev->name);
1366 return -ENODEV;
1369 return 0;
1372 static void btusb_work(struct work_struct *work)
1374 struct btusb_data *data = container_of(work, struct btusb_data, work);
1375 struct hci_dev *hdev = data->hdev;
1376 int new_alts;
1377 int err;
1379 if (data->sco_num > 0) {
1380 if (!test_bit(BTUSB_DID_ISO_RESUME, &data->flags)) {
1381 err = usb_autopm_get_interface(data->isoc ? data->isoc : data->intf);
1382 if (err < 0) {
1383 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1384 usb_kill_anchored_urbs(&data->isoc_anchor);
1385 return;
1388 set_bit(BTUSB_DID_ISO_RESUME, &data->flags);
1391 if (hdev->voice_setting & 0x0020) {
1392 static const int alts[3] = { 2, 4, 5 };
1394 new_alts = alts[data->sco_num - 1];
1395 } else {
1396 new_alts = data->sco_num;
1399 if (data->isoc_altsetting != new_alts) {
1400 unsigned long flags;
1402 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1403 usb_kill_anchored_urbs(&data->isoc_anchor);
1405 /* When isochronous alternate setting needs to be
1406 * changed, because SCO connection has been added
1407 * or removed, a packet fragment may be left in the
1408 * reassembling state. This could lead to wrongly
1409 * assembled fragments.
1411 * Clear outstanding fragment when selecting a new
1412 * alternate setting.
1414 spin_lock_irqsave(&data->rxlock, flags);
1415 kfree_skb(data->sco_skb);
1416 data->sco_skb = NULL;
1417 spin_unlock_irqrestore(&data->rxlock, flags);
1419 if (__set_isoc_interface(hdev, new_alts) < 0)
1420 return;
1423 if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
1424 if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0)
1425 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1426 else
1427 btusb_submit_isoc_urb(hdev, GFP_KERNEL);
1429 } else {
1430 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1431 usb_kill_anchored_urbs(&data->isoc_anchor);
1433 __set_isoc_interface(hdev, 0);
1434 if (test_and_clear_bit(BTUSB_DID_ISO_RESUME, &data->flags))
1435 usb_autopm_put_interface(data->isoc ? data->isoc : data->intf);
1439 static void btusb_waker(struct work_struct *work)
1441 struct btusb_data *data = container_of(work, struct btusb_data, waker);
1442 int err;
1444 err = usb_autopm_get_interface(data->intf);
1445 if (err < 0)
1446 return;
1448 usb_autopm_put_interface(data->intf);
1451 static int btusb_setup_bcm92035(struct hci_dev *hdev)
1453 struct sk_buff *skb;
1454 u8 val = 0x00;
1456 BT_DBG("%s", hdev->name);
1458 skb = __hci_cmd_sync(hdev, 0xfc3b, 1, &val, HCI_INIT_TIMEOUT);
1459 if (IS_ERR(skb))
1460 BT_ERR("BCM92035 command failed (%ld)", -PTR_ERR(skb));
1461 else
1462 kfree_skb(skb);
1464 return 0;
1467 static int btusb_setup_csr(struct hci_dev *hdev)
1469 struct hci_rp_read_local_version *rp;
1470 struct sk_buff *skb;
1472 BT_DBG("%s", hdev->name);
1474 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
1475 HCI_INIT_TIMEOUT);
1476 if (IS_ERR(skb)) {
1477 int err = PTR_ERR(skb);
1478 BT_ERR("%s: CSR: Local version failed (%d)", hdev->name, err);
1479 return err;
1482 if (skb->len != sizeof(struct hci_rp_read_local_version)) {
1483 BT_ERR("%s: CSR: Local version length mismatch", hdev->name);
1484 kfree_skb(skb);
1485 return -EIO;
1488 rp = (struct hci_rp_read_local_version *)skb->data;
1490 /* Detect controllers which aren't real CSR ones. */
1491 if (le16_to_cpu(rp->manufacturer) != 10 ||
1492 le16_to_cpu(rp->lmp_subver) == 0x0c5c) {
1493 /* Clear the reset quirk since this is not an actual
1494 * early Bluetooth 1.1 device from CSR.
1496 clear_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
1498 /* These fake CSR controllers have all a broken
1499 * stored link key handling and so just disable it.
1501 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
1504 kfree_skb(skb);
1506 return 0;
1509 static const struct firmware *btusb_setup_intel_get_fw(struct hci_dev *hdev,
1510 struct intel_version *ver)
1512 const struct firmware *fw;
1513 char fwname[64];
1514 int ret;
1516 snprintf(fwname, sizeof(fwname),
1517 "intel/ibt-hw-%x.%x.%x-fw-%x.%x.%x.%x.%x.bseq",
1518 ver->hw_platform, ver->hw_variant, ver->hw_revision,
1519 ver->fw_variant, ver->fw_revision, ver->fw_build_num,
1520 ver->fw_build_ww, ver->fw_build_yy);
1522 ret = request_firmware(&fw, fwname, &hdev->dev);
1523 if (ret < 0) {
1524 if (ret == -EINVAL) {
1525 BT_ERR("%s Intel firmware file request failed (%d)",
1526 hdev->name, ret);
1527 return NULL;
1530 BT_ERR("%s failed to open Intel firmware file: %s(%d)",
1531 hdev->name, fwname, ret);
1533 /* If the correct firmware patch file is not found, use the
1534 * default firmware patch file instead
1536 snprintf(fwname, sizeof(fwname), "intel/ibt-hw-%x.%x.bseq",
1537 ver->hw_platform, ver->hw_variant);
1538 if (request_firmware(&fw, fwname, &hdev->dev) < 0) {
1539 BT_ERR("%s failed to open default Intel fw file: %s",
1540 hdev->name, fwname);
1541 return NULL;
1545 BT_INFO("%s: Intel Bluetooth firmware file: %s", hdev->name, fwname);
1547 return fw;
1550 static int btusb_setup_intel_patching(struct hci_dev *hdev,
1551 const struct firmware *fw,
1552 const u8 **fw_ptr, int *disable_patch)
1554 struct sk_buff *skb;
1555 struct hci_command_hdr *cmd;
1556 const u8 *cmd_param;
1557 struct hci_event_hdr *evt = NULL;
1558 const u8 *evt_param = NULL;
1559 int remain = fw->size - (*fw_ptr - fw->data);
1561 /* The first byte indicates the types of the patch command or event.
1562 * 0x01 means HCI command and 0x02 is HCI event. If the first bytes
1563 * in the current firmware buffer doesn't start with 0x01 or
1564 * the size of remain buffer is smaller than HCI command header,
1565 * the firmware file is corrupted and it should stop the patching
1566 * process.
1568 if (remain > HCI_COMMAND_HDR_SIZE && *fw_ptr[0] != 0x01) {
1569 BT_ERR("%s Intel fw corrupted: invalid cmd read", hdev->name);
1570 return -EINVAL;
1572 (*fw_ptr)++;
1573 remain--;
1575 cmd = (struct hci_command_hdr *)(*fw_ptr);
1576 *fw_ptr += sizeof(*cmd);
1577 remain -= sizeof(*cmd);
1579 /* Ensure that the remain firmware data is long enough than the length
1580 * of command parameter. If not, the firmware file is corrupted.
1582 if (remain < cmd->plen) {
1583 BT_ERR("%s Intel fw corrupted: invalid cmd len", hdev->name);
1584 return -EFAULT;
1587 /* If there is a command that loads a patch in the firmware
1588 * file, then enable the patch upon success, otherwise just
1589 * disable the manufacturer mode, for example patch activation
1590 * is not required when the default firmware patch file is used
1591 * because there are no patch data to load.
1593 if (*disable_patch && le16_to_cpu(cmd->opcode) == 0xfc8e)
1594 *disable_patch = 0;
1596 cmd_param = *fw_ptr;
1597 *fw_ptr += cmd->plen;
1598 remain -= cmd->plen;
1600 /* This reads the expected events when the above command is sent to the
1601 * device. Some vendor commands expects more than one events, for
1602 * example command status event followed by vendor specific event.
1603 * For this case, it only keeps the last expected event. so the command
1604 * can be sent with __hci_cmd_sync_ev() which returns the sk_buff of
1605 * last expected event.
1607 while (remain > HCI_EVENT_HDR_SIZE && *fw_ptr[0] == 0x02) {
1608 (*fw_ptr)++;
1609 remain--;
1611 evt = (struct hci_event_hdr *)(*fw_ptr);
1612 *fw_ptr += sizeof(*evt);
1613 remain -= sizeof(*evt);
1615 if (remain < evt->plen) {
1616 BT_ERR("%s Intel fw corrupted: invalid evt len",
1617 hdev->name);
1618 return -EFAULT;
1621 evt_param = *fw_ptr;
1622 *fw_ptr += evt->plen;
1623 remain -= evt->plen;
1626 /* Every HCI commands in the firmware file has its correspond event.
1627 * If event is not found or remain is smaller than zero, the firmware
1628 * file is corrupted.
1630 if (!evt || !evt_param || remain < 0) {
1631 BT_ERR("%s Intel fw corrupted: invalid evt read", hdev->name);
1632 return -EFAULT;
1635 skb = __hci_cmd_sync_ev(hdev, le16_to_cpu(cmd->opcode), cmd->plen,
1636 cmd_param, evt->evt, HCI_INIT_TIMEOUT);
1637 if (IS_ERR(skb)) {
1638 BT_ERR("%s sending Intel patch command (0x%4.4x) failed (%ld)",
1639 hdev->name, cmd->opcode, PTR_ERR(skb));
1640 return PTR_ERR(skb);
1643 /* It ensures that the returned event matches the event data read from
1644 * the firmware file. At fist, it checks the length and then
1645 * the contents of the event.
1647 if (skb->len != evt->plen) {
1648 BT_ERR("%s mismatch event length (opcode 0x%4.4x)", hdev->name,
1649 le16_to_cpu(cmd->opcode));
1650 kfree_skb(skb);
1651 return -EFAULT;
1654 if (memcmp(skb->data, evt_param, evt->plen)) {
1655 BT_ERR("%s mismatch event parameter (opcode 0x%4.4x)",
1656 hdev->name, le16_to_cpu(cmd->opcode));
1657 kfree_skb(skb);
1658 return -EFAULT;
1660 kfree_skb(skb);
1662 return 0;
1665 static int btusb_setup_intel(struct hci_dev *hdev)
1667 struct sk_buff *skb;
1668 const struct firmware *fw;
1669 const u8 *fw_ptr;
1670 int disable_patch;
1671 struct intel_version *ver;
1673 const u8 mfg_enable[] = { 0x01, 0x00 };
1674 const u8 mfg_disable[] = { 0x00, 0x00 };
1675 const u8 mfg_reset_deactivate[] = { 0x00, 0x01 };
1676 const u8 mfg_reset_activate[] = { 0x00, 0x02 };
1678 BT_DBG("%s", hdev->name);
1680 /* The controller has a bug with the first HCI command sent to it
1681 * returning number of completed commands as zero. This would stall the
1682 * command processing in the Bluetooth core.
1684 * As a workaround, send HCI Reset command first which will reset the
1685 * number of completed commands and allow normal command processing
1686 * from now on.
1688 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
1689 if (IS_ERR(skb)) {
1690 BT_ERR("%s sending initial HCI reset command failed (%ld)",
1691 hdev->name, PTR_ERR(skb));
1692 return PTR_ERR(skb);
1694 kfree_skb(skb);
1696 /* Read Intel specific controller version first to allow selection of
1697 * which firmware file to load.
1699 * The returned information are hardware variant and revision plus
1700 * firmware variant, revision and build number.
1702 skb = __hci_cmd_sync(hdev, 0xfc05, 0, NULL, HCI_INIT_TIMEOUT);
1703 if (IS_ERR(skb)) {
1704 BT_ERR("%s reading Intel fw version command failed (%ld)",
1705 hdev->name, PTR_ERR(skb));
1706 return PTR_ERR(skb);
1709 if (skb->len != sizeof(*ver)) {
1710 BT_ERR("%s Intel version event length mismatch", hdev->name);
1711 kfree_skb(skb);
1712 return -EIO;
1715 ver = (struct intel_version *)skb->data;
1717 BT_INFO("%s: read Intel version: %02x%02x%02x%02x%02x%02x%02x%02x%02x",
1718 hdev->name, ver->hw_platform, ver->hw_variant,
1719 ver->hw_revision, ver->fw_variant, ver->fw_revision,
1720 ver->fw_build_num, ver->fw_build_ww, ver->fw_build_yy,
1721 ver->fw_patch_num);
1723 /* fw_patch_num indicates the version of patch the device currently
1724 * have. If there is no patch data in the device, it is always 0x00.
1725 * So, if it is other than 0x00, no need to patch the device again.
1727 if (ver->fw_patch_num) {
1728 BT_INFO("%s: Intel device is already patched. patch num: %02x",
1729 hdev->name, ver->fw_patch_num);
1730 kfree_skb(skb);
1731 goto complete;
1734 /* Opens the firmware patch file based on the firmware version read
1735 * from the controller. If it fails to open the matching firmware
1736 * patch file, it tries to open the default firmware patch file.
1737 * If no patch file is found, allow the device to operate without
1738 * a patch.
1740 fw = btusb_setup_intel_get_fw(hdev, ver);
1741 if (!fw) {
1742 kfree_skb(skb);
1743 goto complete;
1745 fw_ptr = fw->data;
1747 kfree_skb(skb);
1749 /* This Intel specific command enables the manufacturer mode of the
1750 * controller.
1752 * Only while this mode is enabled, the driver can download the
1753 * firmware patch data and configuration parameters.
1755 skb = __hci_cmd_sync(hdev, 0xfc11, 2, mfg_enable, HCI_INIT_TIMEOUT);
1756 if (IS_ERR(skb)) {
1757 BT_ERR("%s entering Intel manufacturer mode failed (%ld)",
1758 hdev->name, PTR_ERR(skb));
1759 release_firmware(fw);
1760 return PTR_ERR(skb);
1763 kfree_skb(skb);
1765 disable_patch = 1;
1767 /* The firmware data file consists of list of Intel specific HCI
1768 * commands and its expected events. The first byte indicates the
1769 * type of the message, either HCI command or HCI event.
1771 * It reads the command and its expected event from the firmware file,
1772 * and send to the controller. Once __hci_cmd_sync_ev() returns,
1773 * the returned event is compared with the event read from the firmware
1774 * file and it will continue until all the messages are downloaded to
1775 * the controller.
1777 * Once the firmware patching is completed successfully,
1778 * the manufacturer mode is disabled with reset and activating the
1779 * downloaded patch.
1781 * If the firmware patching fails, the manufacturer mode is
1782 * disabled with reset and deactivating the patch.
1784 * If the default patch file is used, no reset is done when disabling
1785 * the manufacturer.
1787 while (fw->size > fw_ptr - fw->data) {
1788 int ret;
1790 ret = btusb_setup_intel_patching(hdev, fw, &fw_ptr,
1791 &disable_patch);
1792 if (ret < 0)
1793 goto exit_mfg_deactivate;
1796 release_firmware(fw);
1798 if (disable_patch)
1799 goto exit_mfg_disable;
1801 /* Patching completed successfully and disable the manufacturer mode
1802 * with reset and activate the downloaded firmware patches.
1804 skb = __hci_cmd_sync(hdev, 0xfc11, sizeof(mfg_reset_activate),
1805 mfg_reset_activate, HCI_INIT_TIMEOUT);
1806 if (IS_ERR(skb)) {
1807 BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
1808 hdev->name, PTR_ERR(skb));
1809 return PTR_ERR(skb);
1811 kfree_skb(skb);
1813 BT_INFO("%s: Intel Bluetooth firmware patch completed and activated",
1814 hdev->name);
1816 goto complete;
1818 exit_mfg_disable:
1819 /* Disable the manufacturer mode without reset */
1820 skb = __hci_cmd_sync(hdev, 0xfc11, sizeof(mfg_disable), mfg_disable,
1821 HCI_INIT_TIMEOUT);
1822 if (IS_ERR(skb)) {
1823 BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
1824 hdev->name, PTR_ERR(skb));
1825 return PTR_ERR(skb);
1827 kfree_skb(skb);
1829 BT_INFO("%s: Intel Bluetooth firmware patch completed", hdev->name);
1831 goto complete;
1833 exit_mfg_deactivate:
1834 release_firmware(fw);
1836 /* Patching failed. Disable the manufacturer mode with reset and
1837 * deactivate the downloaded firmware patches.
1839 skb = __hci_cmd_sync(hdev, 0xfc11, sizeof(mfg_reset_deactivate),
1840 mfg_reset_deactivate, HCI_INIT_TIMEOUT);
1841 if (IS_ERR(skb)) {
1842 BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
1843 hdev->name, PTR_ERR(skb));
1844 return PTR_ERR(skb);
1846 kfree_skb(skb);
1848 BT_INFO("%s: Intel Bluetooth firmware patch completed and deactivated",
1849 hdev->name);
1851 complete:
1852 /* Set the event mask for Intel specific vendor events. This enables
1853 * a few extra events that are useful during general operation.
1855 btintel_set_event_mask_mfg(hdev, false);
1857 btintel_check_bdaddr(hdev);
1858 return 0;
1861 static int inject_cmd_complete(struct hci_dev *hdev, __u16 opcode)
1863 struct sk_buff *skb;
1864 struct hci_event_hdr *hdr;
1865 struct hci_ev_cmd_complete *evt;
1867 skb = bt_skb_alloc(sizeof(*hdr) + sizeof(*evt) + 1, GFP_ATOMIC);
1868 if (!skb)
1869 return -ENOMEM;
1871 hdr = (struct hci_event_hdr *)skb_put(skb, sizeof(*hdr));
1872 hdr->evt = HCI_EV_CMD_COMPLETE;
1873 hdr->plen = sizeof(*evt) + 1;
1875 evt = (struct hci_ev_cmd_complete *)skb_put(skb, sizeof(*evt));
1876 evt->ncmd = 0x01;
1877 evt->opcode = cpu_to_le16(opcode);
1879 *skb_put(skb, 1) = 0x00;
1881 bt_cb(skb)->pkt_type = HCI_EVENT_PKT;
1883 return hci_recv_frame(hdev, skb);
1886 static int btusb_recv_bulk_intel(struct btusb_data *data, void *buffer,
1887 int count)
1889 /* When the device is in bootloader mode, then it can send
1890 * events via the bulk endpoint. These events are treated the
1891 * same way as the ones received from the interrupt endpoint.
1893 if (test_bit(BTUSB_BOOTLOADER, &data->flags))
1894 return btusb_recv_intr(data, buffer, count);
1896 return btusb_recv_bulk(data, buffer, count);
1899 static void btusb_intel_bootup(struct btusb_data *data, const void *ptr,
1900 unsigned int len)
1902 const struct intel_bootup *evt = ptr;
1904 if (len != sizeof(*evt))
1905 return;
1907 if (test_and_clear_bit(BTUSB_BOOTING, &data->flags)) {
1908 smp_mb__after_atomic();
1909 wake_up_bit(&data->flags, BTUSB_BOOTING);
1913 static void btusb_intel_secure_send_result(struct btusb_data *data,
1914 const void *ptr, unsigned int len)
1916 const struct intel_secure_send_result *evt = ptr;
1918 if (len != sizeof(*evt))
1919 return;
1921 if (evt->result)
1922 set_bit(BTUSB_FIRMWARE_FAILED, &data->flags);
1924 if (test_and_clear_bit(BTUSB_DOWNLOADING, &data->flags) &&
1925 test_bit(BTUSB_FIRMWARE_LOADED, &data->flags)) {
1926 smp_mb__after_atomic();
1927 wake_up_bit(&data->flags, BTUSB_DOWNLOADING);
1931 static int btusb_recv_event_intel(struct hci_dev *hdev, struct sk_buff *skb)
1933 struct btusb_data *data = hci_get_drvdata(hdev);
1935 if (test_bit(BTUSB_BOOTLOADER, &data->flags)) {
1936 struct hci_event_hdr *hdr = (void *)skb->data;
1938 if (skb->len > HCI_EVENT_HDR_SIZE && hdr->evt == 0xff &&
1939 hdr->plen > 0) {
1940 const void *ptr = skb->data + HCI_EVENT_HDR_SIZE + 1;
1941 unsigned int len = skb->len - HCI_EVENT_HDR_SIZE - 1;
1943 switch (skb->data[2]) {
1944 case 0x02:
1945 /* When switching to the operational firmware
1946 * the device sends a vendor specific event
1947 * indicating that the bootup completed.
1949 btusb_intel_bootup(data, ptr, len);
1950 break;
1951 case 0x06:
1952 /* When the firmware loading completes the
1953 * device sends out a vendor specific event
1954 * indicating the result of the firmware
1955 * loading.
1957 btusb_intel_secure_send_result(data, ptr, len);
1958 break;
1963 return hci_recv_frame(hdev, skb);
1966 static int btusb_send_frame_intel(struct hci_dev *hdev, struct sk_buff *skb)
1968 struct btusb_data *data = hci_get_drvdata(hdev);
1969 struct urb *urb;
1971 BT_DBG("%s", hdev->name);
1973 switch (bt_cb(skb)->pkt_type) {
1974 case HCI_COMMAND_PKT:
1975 if (test_bit(BTUSB_BOOTLOADER, &data->flags)) {
1976 struct hci_command_hdr *cmd = (void *)skb->data;
1977 __u16 opcode = le16_to_cpu(cmd->opcode);
1979 /* When in bootloader mode and the command 0xfc09
1980 * is received, it needs to be send down the
1981 * bulk endpoint. So allocate a bulk URB instead.
1983 if (opcode == 0xfc09)
1984 urb = alloc_bulk_urb(hdev, skb);
1985 else
1986 urb = alloc_ctrl_urb(hdev, skb);
1988 /* When the 0xfc01 command is issued to boot into
1989 * the operational firmware, it will actually not
1990 * send a command complete event. To keep the flow
1991 * control working inject that event here.
1993 if (opcode == 0xfc01)
1994 inject_cmd_complete(hdev, opcode);
1995 } else {
1996 urb = alloc_ctrl_urb(hdev, skb);
1998 if (IS_ERR(urb))
1999 return PTR_ERR(urb);
2001 hdev->stat.cmd_tx++;
2002 return submit_or_queue_tx_urb(hdev, urb);
2004 case HCI_ACLDATA_PKT:
2005 urb = alloc_bulk_urb(hdev, skb);
2006 if (IS_ERR(urb))
2007 return PTR_ERR(urb);
2009 hdev->stat.acl_tx++;
2010 return submit_or_queue_tx_urb(hdev, urb);
2012 case HCI_SCODATA_PKT:
2013 if (hci_conn_num(hdev, SCO_LINK) < 1)
2014 return -ENODEV;
2016 urb = alloc_isoc_urb(hdev, skb);
2017 if (IS_ERR(urb))
2018 return PTR_ERR(urb);
2020 hdev->stat.sco_tx++;
2021 return submit_tx_urb(hdev, urb);
2024 return -EILSEQ;
2027 static int btusb_setup_intel_new(struct hci_dev *hdev)
2029 static const u8 reset_param[] = { 0x00, 0x01, 0x00, 0x01,
2030 0x00, 0x08, 0x04, 0x00 };
2031 struct btusb_data *data = hci_get_drvdata(hdev);
2032 struct sk_buff *skb;
2033 struct intel_version *ver;
2034 struct intel_boot_params *params;
2035 const struct firmware *fw;
2036 const u8 *fw_ptr;
2037 u32 frag_len;
2038 char fwname[64];
2039 ktime_t calltime, delta, rettime;
2040 unsigned long long duration;
2041 int err;
2043 BT_DBG("%s", hdev->name);
2045 calltime = ktime_get();
2047 /* Read the Intel version information to determine if the device
2048 * is in bootloader mode or if it already has operational firmware
2049 * loaded.
2051 skb = __hci_cmd_sync(hdev, 0xfc05, 0, NULL, HCI_INIT_TIMEOUT);
2052 if (IS_ERR(skb)) {
2053 BT_ERR("%s: Reading Intel version information failed (%ld)",
2054 hdev->name, PTR_ERR(skb));
2055 return PTR_ERR(skb);
2058 if (skb->len != sizeof(*ver)) {
2059 BT_ERR("%s: Intel version event size mismatch", hdev->name);
2060 kfree_skb(skb);
2061 return -EILSEQ;
2064 ver = (struct intel_version *)skb->data;
2066 /* The hardware platform number has a fixed value of 0x37 and
2067 * for now only accept this single value.
2069 if (ver->hw_platform != 0x37) {
2070 BT_ERR("%s: Unsupported Intel hardware platform (%u)",
2071 hdev->name, ver->hw_platform);
2072 kfree_skb(skb);
2073 return -EINVAL;
2076 /* At the moment the iBT 3.0 hardware variants 0x0b (LnP/SfP)
2077 * and 0x0c (WsP) are supported by this firmware loading method.
2079 * This check has been put in place to ensure correct forward
2080 * compatibility options when newer hardware variants come along.
2082 if (ver->hw_variant != 0x0b && ver->hw_variant != 0x0c) {
2083 BT_ERR("%s: Unsupported Intel hardware variant (%u)",
2084 hdev->name, ver->hw_variant);
2085 kfree_skb(skb);
2086 return -EINVAL;
2089 btintel_version_info(hdev, ver);
2091 /* The firmware variant determines if the device is in bootloader
2092 * mode or is running operational firmware. The value 0x06 identifies
2093 * the bootloader and the value 0x23 identifies the operational
2094 * firmware.
2096 * When the operational firmware is already present, then only
2097 * the check for valid Bluetooth device address is needed. This
2098 * determines if the device will be added as configured or
2099 * unconfigured controller.
2101 * It is not possible to use the Secure Boot Parameters in this
2102 * case since that command is only available in bootloader mode.
2104 if (ver->fw_variant == 0x23) {
2105 kfree_skb(skb);
2106 clear_bit(BTUSB_BOOTLOADER, &data->flags);
2107 btintel_check_bdaddr(hdev);
2108 return 0;
2111 /* If the device is not in bootloader mode, then the only possible
2112 * choice is to return an error and abort the device initialization.
2114 if (ver->fw_variant != 0x06) {
2115 BT_ERR("%s: Unsupported Intel firmware variant (%u)",
2116 hdev->name, ver->fw_variant);
2117 kfree_skb(skb);
2118 return -ENODEV;
2121 kfree_skb(skb);
2123 /* Read the secure boot parameters to identify the operating
2124 * details of the bootloader.
2126 skb = __hci_cmd_sync(hdev, 0xfc0d, 0, NULL, HCI_INIT_TIMEOUT);
2127 if (IS_ERR(skb)) {
2128 BT_ERR("%s: Reading Intel boot parameters failed (%ld)",
2129 hdev->name, PTR_ERR(skb));
2130 return PTR_ERR(skb);
2133 if (skb->len != sizeof(*params)) {
2134 BT_ERR("%s: Intel boot parameters size mismatch", hdev->name);
2135 kfree_skb(skb);
2136 return -EILSEQ;
2139 params = (struct intel_boot_params *)skb->data;
2141 BT_INFO("%s: Device revision is %u", hdev->name,
2142 le16_to_cpu(params->dev_revid));
2144 BT_INFO("%s: Secure boot is %s", hdev->name,
2145 params->secure_boot ? "enabled" : "disabled");
2147 BT_INFO("%s: OTP lock is %s", hdev->name,
2148 params->otp_lock ? "enabled" : "disabled");
2150 BT_INFO("%s: API lock is %s", hdev->name,
2151 params->api_lock ? "enabled" : "disabled");
2153 BT_INFO("%s: Debug lock is %s", hdev->name,
2154 params->debug_lock ? "enabled" : "disabled");
2156 BT_INFO("%s: Minimum firmware build %u week %u %u", hdev->name,
2157 params->min_fw_build_nn, params->min_fw_build_cw,
2158 2000 + params->min_fw_build_yy);
2160 /* It is required that every single firmware fragment is acknowledged
2161 * with a command complete event. If the boot parameters indicate
2162 * that this bootloader does not send them, then abort the setup.
2164 if (params->limited_cce != 0x00) {
2165 BT_ERR("%s: Unsupported Intel firmware loading method (%u)",
2166 hdev->name, params->limited_cce);
2167 kfree_skb(skb);
2168 return -EINVAL;
2171 /* If the OTP has no valid Bluetooth device address, then there will
2172 * also be no valid address for the operational firmware.
2174 if (!bacmp(&params->otp_bdaddr, BDADDR_ANY)) {
2175 BT_INFO("%s: No device address configured", hdev->name);
2176 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
2179 /* With this Intel bootloader only the hardware variant and device
2180 * revision information are used to select the right firmware.
2182 * Currently this bootloader support is limited to hardware variant
2183 * iBT 3.0 (LnP/SfP) which is identified by the value 11 (0x0b).
2185 snprintf(fwname, sizeof(fwname), "intel/ibt-11-%u.sfi",
2186 le16_to_cpu(params->dev_revid));
2188 err = request_firmware(&fw, fwname, &hdev->dev);
2189 if (err < 0) {
2190 BT_ERR("%s: Failed to load Intel firmware file (%d)",
2191 hdev->name, err);
2192 kfree_skb(skb);
2193 return err;
2196 BT_INFO("%s: Found device firmware: %s", hdev->name, fwname);
2198 /* Save the DDC file name for later use to apply once the firmware
2199 * downloading is done.
2201 snprintf(fwname, sizeof(fwname), "intel/ibt-11-%u.ddc",
2202 le16_to_cpu(params->dev_revid));
2204 kfree_skb(skb);
2206 if (fw->size < 644) {
2207 BT_ERR("%s: Invalid size of firmware file (%zu)",
2208 hdev->name, fw->size);
2209 err = -EBADF;
2210 goto done;
2213 set_bit(BTUSB_DOWNLOADING, &data->flags);
2215 /* Start the firmware download transaction with the Init fragment
2216 * represented by the 128 bytes of CSS header.
2218 err = btintel_secure_send(hdev, 0x00, 128, fw->data);
2219 if (err < 0) {
2220 BT_ERR("%s: Failed to send firmware header (%d)",
2221 hdev->name, err);
2222 goto done;
2225 /* Send the 256 bytes of public key information from the firmware
2226 * as the PKey fragment.
2228 err = btintel_secure_send(hdev, 0x03, 256, fw->data + 128);
2229 if (err < 0) {
2230 BT_ERR("%s: Failed to send firmware public key (%d)",
2231 hdev->name, err);
2232 goto done;
2235 /* Send the 256 bytes of signature information from the firmware
2236 * as the Sign fragment.
2238 err = btintel_secure_send(hdev, 0x02, 256, fw->data + 388);
2239 if (err < 0) {
2240 BT_ERR("%s: Failed to send firmware signature (%d)",
2241 hdev->name, err);
2242 goto done;
2245 fw_ptr = fw->data + 644;
2246 frag_len = 0;
2248 while (fw_ptr - fw->data < fw->size) {
2249 struct hci_command_hdr *cmd = (void *)(fw_ptr + frag_len);
2251 frag_len += sizeof(*cmd) + cmd->plen;
2253 /* The parameter length of the secure send command requires
2254 * a 4 byte alignment. It happens so that the firmware file
2255 * contains proper Intel_NOP commands to align the fragments
2256 * as needed.
2258 * Send set of commands with 4 byte alignment from the
2259 * firmware data buffer as a single Data fragement.
2261 if (!(frag_len % 4)) {
2262 err = btintel_secure_send(hdev, 0x01, frag_len, fw_ptr);
2263 if (err < 0) {
2264 BT_ERR("%s: Failed to send firmware data (%d)",
2265 hdev->name, err);
2266 goto done;
2269 fw_ptr += frag_len;
2270 frag_len = 0;
2274 set_bit(BTUSB_FIRMWARE_LOADED, &data->flags);
2276 BT_INFO("%s: Waiting for firmware download to complete", hdev->name);
2278 /* Before switching the device into operational mode and with that
2279 * booting the loaded firmware, wait for the bootloader notification
2280 * that all fragments have been successfully received.
2282 * When the event processing receives the notification, then the
2283 * BTUSB_DOWNLOADING flag will be cleared.
2285 * The firmware loading should not take longer than 5 seconds
2286 * and thus just timeout if that happens and fail the setup
2287 * of this device.
2289 err = wait_on_bit_timeout(&data->flags, BTUSB_DOWNLOADING,
2290 TASK_INTERRUPTIBLE,
2291 msecs_to_jiffies(5000));
2292 if (err == 1) {
2293 BT_ERR("%s: Firmware loading interrupted", hdev->name);
2294 err = -EINTR;
2295 goto done;
2298 if (err) {
2299 BT_ERR("%s: Firmware loading timeout", hdev->name);
2300 err = -ETIMEDOUT;
2301 goto done;
2304 if (test_bit(BTUSB_FIRMWARE_FAILED, &data->flags)) {
2305 BT_ERR("%s: Firmware loading failed", hdev->name);
2306 err = -ENOEXEC;
2307 goto done;
2310 rettime = ktime_get();
2311 delta = ktime_sub(rettime, calltime);
2312 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
2314 BT_INFO("%s: Firmware loaded in %llu usecs", hdev->name, duration);
2316 done:
2317 release_firmware(fw);
2319 if (err < 0)
2320 return err;
2322 calltime = ktime_get();
2324 set_bit(BTUSB_BOOTING, &data->flags);
2326 skb = __hci_cmd_sync(hdev, 0xfc01, sizeof(reset_param), reset_param,
2327 HCI_INIT_TIMEOUT);
2328 if (IS_ERR(skb))
2329 return PTR_ERR(skb);
2331 kfree_skb(skb);
2333 /* The bootloader will not indicate when the device is ready. This
2334 * is done by the operational firmware sending bootup notification.
2336 * Booting into operational firmware should not take longer than
2337 * 1 second. However if that happens, then just fail the setup
2338 * since something went wrong.
2340 BT_INFO("%s: Waiting for device to boot", hdev->name);
2342 err = wait_on_bit_timeout(&data->flags, BTUSB_BOOTING,
2343 TASK_INTERRUPTIBLE,
2344 msecs_to_jiffies(1000));
2346 if (err == 1) {
2347 BT_ERR("%s: Device boot interrupted", hdev->name);
2348 return -EINTR;
2351 if (err) {
2352 BT_ERR("%s: Device boot timeout", hdev->name);
2353 return -ETIMEDOUT;
2356 rettime = ktime_get();
2357 delta = ktime_sub(rettime, calltime);
2358 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
2360 BT_INFO("%s: Device booted in %llu usecs", hdev->name, duration);
2362 clear_bit(BTUSB_BOOTLOADER, &data->flags);
2364 /* Once the device is running in operational mode, it needs to apply
2365 * the device configuration (DDC) parameters.
2367 * The device can work without DDC parameters, so even if it fails
2368 * to load the file, no need to fail the setup.
2370 btintel_load_ddc_config(hdev, fwname);
2372 /* Set the event mask for Intel specific vendor events. This enables
2373 * a few extra events that are useful during general operation. It
2374 * does not enable any debugging related events.
2376 * The device will function correctly without these events enabled
2377 * and thus no need to fail the setup.
2379 btintel_set_event_mask(hdev, false);
2381 return 0;
2384 static int btusb_shutdown_intel(struct hci_dev *hdev)
2386 struct sk_buff *skb;
2387 long ret;
2389 /* Some platforms have an issue with BT LED when the interface is
2390 * down or BT radio is turned off, which takes 5 seconds to BT LED
2391 * goes off. This command turns off the BT LED immediately.
2393 skb = __hci_cmd_sync(hdev, 0xfc3f, 0, NULL, HCI_INIT_TIMEOUT);
2394 if (IS_ERR(skb)) {
2395 ret = PTR_ERR(skb);
2396 BT_ERR("%s: turning off Intel device LED failed (%ld)",
2397 hdev->name, ret);
2398 return ret;
2400 kfree_skb(skb);
2402 return 0;
2405 static int btusb_set_bdaddr_marvell(struct hci_dev *hdev,
2406 const bdaddr_t *bdaddr)
2408 struct sk_buff *skb;
2409 u8 buf[8];
2410 long ret;
2412 buf[0] = 0xfe;
2413 buf[1] = sizeof(bdaddr_t);
2414 memcpy(buf + 2, bdaddr, sizeof(bdaddr_t));
2416 skb = __hci_cmd_sync(hdev, 0xfc22, sizeof(buf), buf, HCI_INIT_TIMEOUT);
2417 if (IS_ERR(skb)) {
2418 ret = PTR_ERR(skb);
2419 BT_ERR("%s: changing Marvell device address failed (%ld)",
2420 hdev->name, ret);
2421 return ret;
2423 kfree_skb(skb);
2425 return 0;
2428 static int btusb_set_bdaddr_ath3012(struct hci_dev *hdev,
2429 const bdaddr_t *bdaddr)
2431 struct sk_buff *skb;
2432 u8 buf[10];
2433 long ret;
2435 buf[0] = 0x01;
2436 buf[1] = 0x01;
2437 buf[2] = 0x00;
2438 buf[3] = sizeof(bdaddr_t);
2439 memcpy(buf + 4, bdaddr, sizeof(bdaddr_t));
2441 skb = __hci_cmd_sync(hdev, 0xfc0b, sizeof(buf), buf, HCI_INIT_TIMEOUT);
2442 if (IS_ERR(skb)) {
2443 ret = PTR_ERR(skb);
2444 BT_ERR("%s: Change address command failed (%ld)",
2445 hdev->name, ret);
2446 return ret;
2448 kfree_skb(skb);
2450 return 0;
2453 #define QCA_DFU_PACKET_LEN 4096
2455 #define QCA_GET_TARGET_VERSION 0x09
2456 #define QCA_CHECK_STATUS 0x05
2457 #define QCA_DFU_DOWNLOAD 0x01
2459 #define QCA_SYSCFG_UPDATED 0x40
2460 #define QCA_PATCH_UPDATED 0x80
2461 #define QCA_DFU_TIMEOUT 3000
2463 struct qca_version {
2464 __le32 rom_version;
2465 __le32 patch_version;
2466 __le32 ram_version;
2467 __le32 ref_clock;
2468 __u8 reserved[4];
2469 } __packed;
2471 struct qca_rampatch_version {
2472 __le16 rom_version;
2473 __le16 patch_version;
2474 } __packed;
2476 struct qca_device_info {
2477 u32 rom_version;
2478 u8 rampatch_hdr; /* length of header in rampatch */
2479 u8 nvm_hdr; /* length of header in NVM */
2480 u8 ver_offset; /* offset of version structure in rampatch */
2483 static const struct qca_device_info qca_devices_table[] = {
2484 { 0x00000100, 20, 4, 10 }, /* Rome 1.0 */
2485 { 0x00000101, 20, 4, 10 }, /* Rome 1.1 */
2486 { 0x00000200, 28, 4, 18 }, /* Rome 2.0 */
2487 { 0x00000201, 28, 4, 18 }, /* Rome 2.1 */
2488 { 0x00000300, 28, 4, 18 }, /* Rome 3.0 */
2489 { 0x00000302, 28, 4, 18 }, /* Rome 3.2 */
2492 static int btusb_qca_send_vendor_req(struct hci_dev *hdev, u8 request,
2493 void *data, u16 size)
2495 struct btusb_data *btdata = hci_get_drvdata(hdev);
2496 struct usb_device *udev = btdata->udev;
2497 int pipe, err;
2498 u8 *buf;
2500 buf = kmalloc(size, GFP_KERNEL);
2501 if (!buf)
2502 return -ENOMEM;
2504 /* Found some of USB hosts have IOT issues with ours so that we should
2505 * not wait until HCI layer is ready.
2507 pipe = usb_rcvctrlpipe(udev, 0);
2508 err = usb_control_msg(udev, pipe, request, USB_TYPE_VENDOR | USB_DIR_IN,
2509 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
2510 if (err < 0) {
2511 BT_ERR("%s: Failed to access otp area (%d)", hdev->name, err);
2512 goto done;
2515 memcpy(data, buf, size);
2517 done:
2518 kfree(buf);
2520 return err;
2523 static int btusb_setup_qca_download_fw(struct hci_dev *hdev,
2524 const struct firmware *firmware,
2525 size_t hdr_size)
2527 struct btusb_data *btdata = hci_get_drvdata(hdev);
2528 struct usb_device *udev = btdata->udev;
2529 size_t count, size, sent = 0;
2530 int pipe, len, err;
2531 u8 *buf;
2533 buf = kmalloc(QCA_DFU_PACKET_LEN, GFP_KERNEL);
2534 if (!buf)
2535 return -ENOMEM;
2537 count = firmware->size;
2539 size = min_t(size_t, count, hdr_size);
2540 memcpy(buf, firmware->data, size);
2542 /* USB patches should go down to controller through USB path
2543 * because binary format fits to go down through USB channel.
2544 * USB control path is for patching headers and USB bulk is for
2545 * patch body.
2547 pipe = usb_sndctrlpipe(udev, 0);
2548 err = usb_control_msg(udev, pipe, QCA_DFU_DOWNLOAD, USB_TYPE_VENDOR,
2549 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
2550 if (err < 0) {
2551 BT_ERR("%s: Failed to send headers (%d)", hdev->name, err);
2552 goto done;
2555 sent += size;
2556 count -= size;
2558 while (count) {
2559 size = min_t(size_t, count, QCA_DFU_PACKET_LEN);
2561 memcpy(buf, firmware->data + sent, size);
2563 pipe = usb_sndbulkpipe(udev, 0x02);
2564 err = usb_bulk_msg(udev, pipe, buf, size, &len,
2565 QCA_DFU_TIMEOUT);
2566 if (err < 0) {
2567 BT_ERR("%s: Failed to send body at %zd of %zd (%d)",
2568 hdev->name, sent, firmware->size, err);
2569 break;
2572 if (size != len) {
2573 BT_ERR("%s: Failed to get bulk buffer", hdev->name);
2574 err = -EILSEQ;
2575 break;
2578 sent += size;
2579 count -= size;
2582 done:
2583 kfree(buf);
2584 return err;
2587 static int btusb_setup_qca_load_rampatch(struct hci_dev *hdev,
2588 struct qca_version *ver,
2589 const struct qca_device_info *info)
2591 struct qca_rampatch_version *rver;
2592 const struct firmware *fw;
2593 u32 ver_rom, ver_patch;
2594 u16 rver_rom, rver_patch;
2595 char fwname[64];
2596 int err;
2598 ver_rom = le32_to_cpu(ver->rom_version);
2599 ver_patch = le32_to_cpu(ver->patch_version);
2601 snprintf(fwname, sizeof(fwname), "qca/rampatch_usb_%08x.bin", ver_rom);
2603 err = request_firmware(&fw, fwname, &hdev->dev);
2604 if (err) {
2605 BT_ERR("%s: failed to request rampatch file: %s (%d)",
2606 hdev->name, fwname, err);
2607 return err;
2610 BT_INFO("%s: using rampatch file: %s", hdev->name, fwname);
2612 rver = (struct qca_rampatch_version *)(fw->data + info->ver_offset);
2613 rver_rom = le16_to_cpu(rver->rom_version);
2614 rver_patch = le16_to_cpu(rver->patch_version);
2616 BT_INFO("%s: QCA: patch rome 0x%x build 0x%x, firmware rome 0x%x "
2617 "build 0x%x", hdev->name, rver_rom, rver_patch, ver_rom,
2618 ver_patch);
2620 if (rver_rom != ver_rom || rver_patch <= ver_patch) {
2621 BT_ERR("%s: rampatch file version did not match with firmware",
2622 hdev->name);
2623 err = -EINVAL;
2624 goto done;
2627 err = btusb_setup_qca_download_fw(hdev, fw, info->rampatch_hdr);
2629 done:
2630 release_firmware(fw);
2632 return err;
2635 static int btusb_setup_qca_load_nvm(struct hci_dev *hdev,
2636 struct qca_version *ver,
2637 const struct qca_device_info *info)
2639 const struct firmware *fw;
2640 char fwname[64];
2641 int err;
2643 snprintf(fwname, sizeof(fwname), "qca/nvm_usb_%08x.bin",
2644 le32_to_cpu(ver->rom_version));
2646 err = request_firmware(&fw, fwname, &hdev->dev);
2647 if (err) {
2648 BT_ERR("%s: failed to request NVM file: %s (%d)",
2649 hdev->name, fwname, err);
2650 return err;
2653 BT_INFO("%s: using NVM file: %s", hdev->name, fwname);
2655 err = btusb_setup_qca_download_fw(hdev, fw, info->nvm_hdr);
2657 release_firmware(fw);
2659 return err;
2662 static int btusb_setup_qca(struct hci_dev *hdev)
2664 const struct qca_device_info *info = NULL;
2665 struct qca_version ver;
2666 u32 ver_rom;
2667 u8 status;
2668 int i, err;
2670 err = btusb_qca_send_vendor_req(hdev, QCA_GET_TARGET_VERSION, &ver,
2671 sizeof(ver));
2672 if (err < 0)
2673 return err;
2675 ver_rom = le32_to_cpu(ver.rom_version);
2676 for (i = 0; i < ARRAY_SIZE(qca_devices_table); i++) {
2677 if (ver_rom == qca_devices_table[i].rom_version)
2678 info = &qca_devices_table[i];
2680 if (!info) {
2681 BT_ERR("%s: don't support firmware rome 0x%x", hdev->name,
2682 ver_rom);
2683 return -ENODEV;
2686 err = btusb_qca_send_vendor_req(hdev, QCA_CHECK_STATUS, &status,
2687 sizeof(status));
2688 if (err < 0)
2689 return err;
2691 if (!(status & QCA_PATCH_UPDATED)) {
2692 err = btusb_setup_qca_load_rampatch(hdev, &ver, info);
2693 if (err < 0)
2694 return err;
2697 if (!(status & QCA_SYSCFG_UPDATED)) {
2698 err = btusb_setup_qca_load_nvm(hdev, &ver, info);
2699 if (err < 0)
2700 return err;
2703 return 0;
2706 #ifdef CONFIG_BT_HCIBTUSB_BCM
2707 static inline int __set_diag_interface(struct hci_dev *hdev)
2709 struct btusb_data *data = hci_get_drvdata(hdev);
2710 struct usb_interface *intf = data->diag;
2711 int i;
2713 if (!data->diag)
2714 return -ENODEV;
2716 data->diag_tx_ep = NULL;
2717 data->diag_rx_ep = NULL;
2719 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
2720 struct usb_endpoint_descriptor *ep_desc;
2722 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
2724 if (!data->diag_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
2725 data->diag_tx_ep = ep_desc;
2726 continue;
2729 if (!data->diag_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
2730 data->diag_rx_ep = ep_desc;
2731 continue;
2735 if (!data->diag_tx_ep || !data->diag_rx_ep) {
2736 BT_ERR("%s invalid diagnostic descriptors", hdev->name);
2737 return -ENODEV;
2740 return 0;
2743 static struct urb *alloc_diag_urb(struct hci_dev *hdev, bool enable)
2745 struct btusb_data *data = hci_get_drvdata(hdev);
2746 struct sk_buff *skb;
2747 struct urb *urb;
2748 unsigned int pipe;
2750 if (!data->diag_tx_ep)
2751 return ERR_PTR(-ENODEV);
2753 urb = usb_alloc_urb(0, GFP_KERNEL);
2754 if (!urb)
2755 return ERR_PTR(-ENOMEM);
2757 skb = bt_skb_alloc(2, GFP_KERNEL);
2758 if (!skb) {
2759 usb_free_urb(urb);
2760 return ERR_PTR(-ENOMEM);
2763 *skb_put(skb, 1) = 0xf0;
2764 *skb_put(skb, 1) = enable;
2766 pipe = usb_sndbulkpipe(data->udev, data->diag_tx_ep->bEndpointAddress);
2768 usb_fill_bulk_urb(urb, data->udev, pipe,
2769 skb->data, skb->len, btusb_tx_complete, skb);
2771 skb->dev = (void *)hdev;
2773 return urb;
2776 static int btusb_bcm_set_diag(struct hci_dev *hdev, bool enable)
2778 struct btusb_data *data = hci_get_drvdata(hdev);
2779 struct urb *urb;
2781 if (!data->diag)
2782 return -ENODEV;
2784 if (!test_bit(HCI_RUNNING, &hdev->flags))
2785 return -ENETDOWN;
2787 urb = alloc_diag_urb(hdev, enable);
2788 if (IS_ERR(urb))
2789 return PTR_ERR(urb);
2791 return submit_or_queue_tx_urb(hdev, urb);
2793 #endif
2795 static int btusb_probe(struct usb_interface *intf,
2796 const struct usb_device_id *id)
2798 struct usb_endpoint_descriptor *ep_desc;
2799 struct btusb_data *data;
2800 struct hci_dev *hdev;
2801 unsigned ifnum_base;
2802 int i, err;
2804 BT_DBG("intf %p id %p", intf, id);
2806 /* interface numbers are hardcoded in the spec */
2807 if (intf->cur_altsetting->desc.bInterfaceNumber != 0) {
2808 if (!(id->driver_info & BTUSB_IFNUM_2))
2809 return -ENODEV;
2810 if (intf->cur_altsetting->desc.bInterfaceNumber != 2)
2811 return -ENODEV;
2814 ifnum_base = intf->cur_altsetting->desc.bInterfaceNumber;
2816 if (!id->driver_info) {
2817 const struct usb_device_id *match;
2819 match = usb_match_id(intf, blacklist_table);
2820 if (match)
2821 id = match;
2824 if (id->driver_info == BTUSB_IGNORE)
2825 return -ENODEV;
2827 if (id->driver_info & BTUSB_ATH3012) {
2828 struct usb_device *udev = interface_to_usbdev(intf);
2830 /* Old firmware would otherwise let ath3k driver load
2831 * patch and sysconfig files */
2832 if (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x0001)
2833 return -ENODEV;
2836 data = devm_kzalloc(&intf->dev, sizeof(*data), GFP_KERNEL);
2837 if (!data)
2838 return -ENOMEM;
2840 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
2841 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
2843 if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
2844 data->intr_ep = ep_desc;
2845 continue;
2848 if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
2849 data->bulk_tx_ep = ep_desc;
2850 continue;
2853 if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
2854 data->bulk_rx_ep = ep_desc;
2855 continue;
2859 if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep)
2860 return -ENODEV;
2862 if (id->driver_info & BTUSB_AMP) {
2863 data->cmdreq_type = USB_TYPE_CLASS | 0x01;
2864 data->cmdreq = 0x2b;
2865 } else {
2866 data->cmdreq_type = USB_TYPE_CLASS;
2867 data->cmdreq = 0x00;
2870 data->udev = interface_to_usbdev(intf);
2871 data->intf = intf;
2873 INIT_WORK(&data->work, btusb_work);
2874 INIT_WORK(&data->waker, btusb_waker);
2875 init_usb_anchor(&data->deferred);
2876 init_usb_anchor(&data->tx_anchor);
2877 spin_lock_init(&data->txlock);
2879 init_usb_anchor(&data->intr_anchor);
2880 init_usb_anchor(&data->bulk_anchor);
2881 init_usb_anchor(&data->isoc_anchor);
2882 init_usb_anchor(&data->diag_anchor);
2883 spin_lock_init(&data->rxlock);
2885 if (id->driver_info & BTUSB_INTEL_NEW) {
2886 data->recv_event = btusb_recv_event_intel;
2887 data->recv_bulk = btusb_recv_bulk_intel;
2888 set_bit(BTUSB_BOOTLOADER, &data->flags);
2889 } else {
2890 data->recv_event = hci_recv_frame;
2891 data->recv_bulk = btusb_recv_bulk;
2894 hdev = hci_alloc_dev();
2895 if (!hdev)
2896 return -ENOMEM;
2898 hdev->bus = HCI_USB;
2899 hci_set_drvdata(hdev, data);
2901 if (id->driver_info & BTUSB_AMP)
2902 hdev->dev_type = HCI_AMP;
2903 else
2904 hdev->dev_type = HCI_BREDR;
2906 data->hdev = hdev;
2908 SET_HCIDEV_DEV(hdev, &intf->dev);
2910 hdev->open = btusb_open;
2911 hdev->close = btusb_close;
2912 hdev->flush = btusb_flush;
2913 hdev->send = btusb_send_frame;
2914 hdev->notify = btusb_notify;
2916 if (id->driver_info & BTUSB_BCM2045)
2917 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
2919 if (id->driver_info & BTUSB_BCM92035)
2920 hdev->setup = btusb_setup_bcm92035;
2922 #ifdef CONFIG_BT_HCIBTUSB_BCM
2923 if (id->driver_info & BTUSB_BCM_PATCHRAM) {
2924 hdev->manufacturer = 15;
2925 hdev->setup = btbcm_setup_patchram;
2926 hdev->set_diag = btusb_bcm_set_diag;
2927 hdev->set_bdaddr = btbcm_set_bdaddr;
2929 /* Broadcom LM_DIAG Interface numbers are hardcoded */
2930 data->diag = usb_ifnum_to_if(data->udev, ifnum_base + 2);
2933 if (id->driver_info & BTUSB_BCM_APPLE) {
2934 hdev->manufacturer = 15;
2935 hdev->setup = btbcm_setup_apple;
2936 hdev->set_diag = btusb_bcm_set_diag;
2938 /* Broadcom LM_DIAG Interface numbers are hardcoded */
2939 data->diag = usb_ifnum_to_if(data->udev, ifnum_base + 2);
2941 #endif
2943 if (id->driver_info & BTUSB_INTEL) {
2944 hdev->manufacturer = 2;
2945 hdev->setup = btusb_setup_intel;
2946 hdev->shutdown = btusb_shutdown_intel;
2947 hdev->set_diag = btintel_set_diag_mfg;
2948 hdev->set_bdaddr = btintel_set_bdaddr;
2949 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
2950 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
2951 set_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks);
2954 if (id->driver_info & BTUSB_INTEL_NEW) {
2955 hdev->manufacturer = 2;
2956 hdev->send = btusb_send_frame_intel;
2957 hdev->setup = btusb_setup_intel_new;
2958 hdev->hw_error = btintel_hw_error;
2959 hdev->set_diag = btintel_set_diag;
2960 hdev->set_bdaddr = btintel_set_bdaddr;
2961 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
2962 set_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks);
2965 if (id->driver_info & BTUSB_MARVELL)
2966 hdev->set_bdaddr = btusb_set_bdaddr_marvell;
2968 if (id->driver_info & BTUSB_SWAVE) {
2969 set_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks);
2970 set_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS, &hdev->quirks);
2973 if (id->driver_info & BTUSB_INTEL_BOOT) {
2974 hdev->manufacturer = 2;
2975 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
2978 if (id->driver_info & BTUSB_ATH3012) {
2979 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
2980 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
2981 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
2984 if (id->driver_info & BTUSB_QCA_ROME) {
2985 data->setup_on_usb = btusb_setup_qca;
2986 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
2988 /* QCA Rome devices lose their updated firmware over suspend,
2989 * but the USB hub doesn't notice any status change.
2990 * explicitly request a device reset on resume.
2992 interface_to_usbdev(intf)->quirks |= USB_QUIRK_RESET_RESUME;
2995 #ifdef CONFIG_BT_HCIBTUSB_RTL
2996 if (id->driver_info & BTUSB_REALTEK) {
2997 hdev->setup = btrtl_setup_realtek;
2999 /* Realtek devices lose their updated firmware over suspend,
3000 * but the USB hub doesn't notice any status change.
3001 * Explicitly request a device reset on resume.
3003 interface_to_usbdev(intf)->quirks |= USB_QUIRK_RESET_RESUME;
3005 #endif
3007 if (id->driver_info & BTUSB_AMP) {
3008 /* AMP controllers do not support SCO packets */
3009 data->isoc = NULL;
3010 } else {
3011 /* Interface orders are hardcoded in the specification */
3012 data->isoc = usb_ifnum_to_if(data->udev, ifnum_base + 1);
3015 if (!reset)
3016 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
3018 if (force_scofix || id->driver_info & BTUSB_WRONG_SCO_MTU) {
3019 if (!disable_scofix)
3020 set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
3023 if (id->driver_info & BTUSB_BROKEN_ISOC)
3024 data->isoc = NULL;
3026 if (id->driver_info & BTUSB_DIGIANSWER) {
3027 data->cmdreq_type = USB_TYPE_VENDOR;
3028 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
3031 if (id->driver_info & BTUSB_CSR) {
3032 struct usb_device *udev = data->udev;
3033 u16 bcdDevice = le16_to_cpu(udev->descriptor.bcdDevice);
3035 /* Old firmware would otherwise execute USB reset */
3036 if (bcdDevice < 0x117)
3037 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
3039 /* Fake CSR devices with broken commands */
3040 if (bcdDevice <= 0x100 || bcdDevice == 0x134)
3041 hdev->setup = btusb_setup_csr;
3043 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
3046 if (id->driver_info & BTUSB_SNIFFER) {
3047 struct usb_device *udev = data->udev;
3049 /* New sniffer firmware has crippled HCI interface */
3050 if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
3051 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
3054 if (id->driver_info & BTUSB_INTEL_BOOT) {
3055 /* A bug in the bootloader causes that interrupt interface is
3056 * only enabled after receiving SetInterface(0, AltSetting=0).
3058 err = usb_set_interface(data->udev, 0, 0);
3059 if (err < 0) {
3060 BT_ERR("failed to set interface 0, alt 0 %d", err);
3061 hci_free_dev(hdev);
3062 return err;
3066 if (data->isoc) {
3067 err = usb_driver_claim_interface(&btusb_driver,
3068 data->isoc, data);
3069 if (err < 0) {
3070 hci_free_dev(hdev);
3071 return err;
3075 #ifdef CONFIG_BT_HCIBTUSB_BCM
3076 if (data->diag) {
3077 if (!usb_driver_claim_interface(&btusb_driver,
3078 data->diag, data))
3079 __set_diag_interface(hdev);
3080 else
3081 data->diag = NULL;
3083 #endif
3085 err = hci_register_dev(hdev);
3086 if (err < 0) {
3087 hci_free_dev(hdev);
3088 return err;
3091 usb_set_intfdata(intf, data);
3093 return 0;
3096 static void btusb_disconnect(struct usb_interface *intf)
3098 struct btusb_data *data = usb_get_intfdata(intf);
3099 struct hci_dev *hdev;
3101 BT_DBG("intf %p", intf);
3103 if (!data)
3104 return;
3106 hdev = data->hdev;
3107 usb_set_intfdata(data->intf, NULL);
3109 if (data->isoc)
3110 usb_set_intfdata(data->isoc, NULL);
3112 if (data->diag)
3113 usb_set_intfdata(data->diag, NULL);
3115 hci_unregister_dev(hdev);
3117 if (intf == data->intf) {
3118 if (data->isoc)
3119 usb_driver_release_interface(&btusb_driver, data->isoc);
3120 if (data->diag)
3121 usb_driver_release_interface(&btusb_driver, data->diag);
3122 } else if (intf == data->isoc) {
3123 if (data->diag)
3124 usb_driver_release_interface(&btusb_driver, data->diag);
3125 usb_driver_release_interface(&btusb_driver, data->intf);
3126 } else if (intf == data->diag) {
3127 usb_driver_release_interface(&btusb_driver, data->intf);
3128 if (data->isoc)
3129 usb_driver_release_interface(&btusb_driver, data->isoc);
3132 hci_free_dev(hdev);
3135 #ifdef CONFIG_PM
3136 static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
3138 struct btusb_data *data = usb_get_intfdata(intf);
3140 BT_DBG("intf %p", intf);
3142 if (data->suspend_count++)
3143 return 0;
3145 spin_lock_irq(&data->txlock);
3146 if (!(PMSG_IS_AUTO(message) && data->tx_in_flight)) {
3147 set_bit(BTUSB_SUSPENDING, &data->flags);
3148 spin_unlock_irq(&data->txlock);
3149 } else {
3150 spin_unlock_irq(&data->txlock);
3151 data->suspend_count--;
3152 return -EBUSY;
3155 cancel_work_sync(&data->work);
3157 btusb_stop_traffic(data);
3158 usb_kill_anchored_urbs(&data->tx_anchor);
3160 return 0;
3163 static void play_deferred(struct btusb_data *data)
3165 struct urb *urb;
3166 int err;
3168 while ((urb = usb_get_from_anchor(&data->deferred))) {
3169 err = usb_submit_urb(urb, GFP_ATOMIC);
3170 if (err < 0)
3171 break;
3173 data->tx_in_flight++;
3175 usb_scuttle_anchored_urbs(&data->deferred);
3178 static int btusb_resume(struct usb_interface *intf)
3180 struct btusb_data *data = usb_get_intfdata(intf);
3181 struct hci_dev *hdev = data->hdev;
3182 int err = 0;
3184 BT_DBG("intf %p", intf);
3186 if (--data->suspend_count)
3187 return 0;
3189 if (!test_bit(HCI_RUNNING, &hdev->flags))
3190 goto done;
3192 if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
3193 err = btusb_submit_intr_urb(hdev, GFP_NOIO);
3194 if (err < 0) {
3195 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
3196 goto failed;
3200 if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) {
3201 err = btusb_submit_bulk_urb(hdev, GFP_NOIO);
3202 if (err < 0) {
3203 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
3204 goto failed;
3207 btusb_submit_bulk_urb(hdev, GFP_NOIO);
3210 if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
3211 if (btusb_submit_isoc_urb(hdev, GFP_NOIO) < 0)
3212 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
3213 else
3214 btusb_submit_isoc_urb(hdev, GFP_NOIO);
3217 spin_lock_irq(&data->txlock);
3218 play_deferred(data);
3219 clear_bit(BTUSB_SUSPENDING, &data->flags);
3220 spin_unlock_irq(&data->txlock);
3221 schedule_work(&data->work);
3223 return 0;
3225 failed:
3226 usb_scuttle_anchored_urbs(&data->deferred);
3227 done:
3228 spin_lock_irq(&data->txlock);
3229 clear_bit(BTUSB_SUSPENDING, &data->flags);
3230 spin_unlock_irq(&data->txlock);
3232 return err;
3234 #endif
3236 static struct usb_driver btusb_driver = {
3237 .name = "btusb",
3238 .probe = btusb_probe,
3239 .disconnect = btusb_disconnect,
3240 #ifdef CONFIG_PM
3241 .suspend = btusb_suspend,
3242 .resume = btusb_resume,
3243 #endif
3244 .id_table = btusb_table,
3245 .supports_autosuspend = 1,
3246 .disable_hub_initiated_lpm = 1,
3249 module_usb_driver(btusb_driver);
3251 module_param(disable_scofix, bool, 0644);
3252 MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
3254 module_param(force_scofix, bool, 0644);
3255 MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
3257 module_param(reset, bool, 0644);
3258 MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
3260 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
3261 MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
3262 MODULE_VERSION(VERSION);
3263 MODULE_LICENSE("GPL");