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 <linux/of_device.h>
28 #include <linux/of_irq.h>
29 #include <asm/unaligned.h>
31 #include <net/bluetooth/bluetooth.h>
32 #include <net/bluetooth/hci_core.h>
40 static bool disable_scofix;
41 static bool force_scofix;
43 static bool reset = true;
45 static struct usb_driver btusb_driver;
47 #define BTUSB_IGNORE 0x01
48 #define BTUSB_DIGIANSWER 0x02
49 #define BTUSB_CSR 0x04
50 #define BTUSB_SNIFFER 0x08
51 #define BTUSB_BCM92035 0x10
52 #define BTUSB_BROKEN_ISOC 0x20
53 #define BTUSB_WRONG_SCO_MTU 0x40
54 #define BTUSB_ATH3012 0x80
55 #define BTUSB_INTEL 0x100
56 #define BTUSB_INTEL_BOOT 0x200
57 #define BTUSB_BCM_PATCHRAM 0x400
58 #define BTUSB_MARVELL 0x800
59 #define BTUSB_SWAVE 0x1000
60 #define BTUSB_INTEL_NEW 0x2000
61 #define BTUSB_AMP 0x4000
62 #define BTUSB_QCA_ROME 0x8000
63 #define BTUSB_BCM_APPLE 0x10000
64 #define BTUSB_REALTEK 0x20000
65 #define BTUSB_BCM2045 0x40000
66 #define BTUSB_IFNUM_2 0x80000
67 #define BTUSB_CW6622 0x100000
69 static const struct usb_device_id btusb_table[] = {
70 /* Generic Bluetooth USB device */
71 { USB_DEVICE_INFO(0xe0, 0x01, 0x01) },
73 /* Generic Bluetooth AMP device */
74 { USB_DEVICE_INFO(0xe0, 0x01, 0x04), .driver_info = BTUSB_AMP },
76 /* Generic Bluetooth USB interface */
77 { USB_INTERFACE_INFO(0xe0, 0x01, 0x01) },
79 /* Apple-specific (Broadcom) devices */
80 { USB_VENDOR_AND_INTERFACE_INFO(0x05ac, 0xff, 0x01, 0x01),
81 .driver_info = BTUSB_BCM_APPLE | BTUSB_IFNUM_2 },
83 /* MediaTek MT76x0E */
84 { USB_DEVICE(0x0e8d, 0x763f) },
86 /* Broadcom SoftSailing reporting vendor specific */
87 { USB_DEVICE(0x0a5c, 0x21e1) },
89 /* Apple MacBookPro 7,1 */
90 { USB_DEVICE(0x05ac, 0x8213) },
93 { USB_DEVICE(0x05ac, 0x8215) },
95 /* Apple MacBookPro6,2 */
96 { USB_DEVICE(0x05ac, 0x8218) },
98 /* Apple MacBookAir3,1, MacBookAir3,2 */
99 { USB_DEVICE(0x05ac, 0x821b) },
101 /* Apple MacBookAir4,1 */
102 { USB_DEVICE(0x05ac, 0x821f) },
104 /* Apple MacBookPro8,2 */
105 { USB_DEVICE(0x05ac, 0x821a) },
107 /* Apple MacMini5,1 */
108 { USB_DEVICE(0x05ac, 0x8281) },
110 /* AVM BlueFRITZ! USB v2.0 */
111 { USB_DEVICE(0x057c, 0x3800), .driver_info = BTUSB_SWAVE },
113 /* Bluetooth Ultraport Module from IBM */
114 { USB_DEVICE(0x04bf, 0x030a) },
116 /* ALPS Modules with non-standard id */
117 { USB_DEVICE(0x044e, 0x3001) },
118 { USB_DEVICE(0x044e, 0x3002) },
120 /* Ericsson with non-standard id */
121 { USB_DEVICE(0x0bdb, 0x1002) },
123 /* Canyon CN-BTU1 with HID interfaces */
124 { USB_DEVICE(0x0c10, 0x0000) },
126 /* Broadcom BCM20702A0 */
127 { USB_DEVICE(0x413c, 0x8197) },
129 /* Broadcom BCM20702B0 (Dynex/Insignia) */
130 { USB_DEVICE(0x19ff, 0x0239), .driver_info = BTUSB_BCM_PATCHRAM },
132 /* Broadcom BCM43142A0 (Foxconn/Lenovo) */
133 { USB_DEVICE(0x105b, 0xe065), .driver_info = BTUSB_BCM_PATCHRAM },
135 /* Broadcom BCM920703 (HTC Vive) */
136 { USB_VENDOR_AND_INTERFACE_INFO(0x0bb4, 0xff, 0x01, 0x01),
137 .driver_info = BTUSB_BCM_PATCHRAM },
139 /* Foxconn - Hon Hai */
140 { USB_VENDOR_AND_INTERFACE_INFO(0x0489, 0xff, 0x01, 0x01),
141 .driver_info = BTUSB_BCM_PATCHRAM },
143 /* Lite-On Technology - Broadcom based */
144 { USB_VENDOR_AND_INTERFACE_INFO(0x04ca, 0xff, 0x01, 0x01),
145 .driver_info = BTUSB_BCM_PATCHRAM },
147 /* Broadcom devices with vendor specific id */
148 { USB_VENDOR_AND_INTERFACE_INFO(0x0a5c, 0xff, 0x01, 0x01),
149 .driver_info = BTUSB_BCM_PATCHRAM },
151 /* ASUSTek Computer - Broadcom based */
152 { USB_VENDOR_AND_INTERFACE_INFO(0x0b05, 0xff, 0x01, 0x01),
153 .driver_info = BTUSB_BCM_PATCHRAM },
155 /* Belkin F8065bf - Broadcom based */
156 { USB_VENDOR_AND_INTERFACE_INFO(0x050d, 0xff, 0x01, 0x01),
157 .driver_info = BTUSB_BCM_PATCHRAM },
159 /* IMC Networks - Broadcom based */
160 { USB_VENDOR_AND_INTERFACE_INFO(0x13d3, 0xff, 0x01, 0x01),
161 .driver_info = BTUSB_BCM_PATCHRAM },
163 /* Dell Computer - Broadcom based */
164 { USB_VENDOR_AND_INTERFACE_INFO(0x413c, 0xff, 0x01, 0x01),
165 .driver_info = BTUSB_BCM_PATCHRAM },
167 /* Toshiba Corp - Broadcom based */
168 { USB_VENDOR_AND_INTERFACE_INFO(0x0930, 0xff, 0x01, 0x01),
169 .driver_info = BTUSB_BCM_PATCHRAM },
171 /* Intel Bluetooth USB Bootloader (RAM module) */
172 { USB_DEVICE(0x8087, 0x0a5a),
173 .driver_info = BTUSB_INTEL_BOOT | BTUSB_BROKEN_ISOC },
175 { } /* Terminating entry */
178 MODULE_DEVICE_TABLE(usb, btusb_table);
180 static const struct usb_device_id blacklist_table[] = {
181 /* CSR BlueCore devices */
182 { USB_DEVICE(0x0a12, 0x0001), .driver_info = BTUSB_CSR },
184 /* Broadcom BCM2033 without firmware */
185 { USB_DEVICE(0x0a5c, 0x2033), .driver_info = BTUSB_IGNORE },
187 /* Broadcom BCM2045 devices */
188 { USB_DEVICE(0x0a5c, 0x2045), .driver_info = BTUSB_BCM2045 },
190 /* Atheros 3011 with sflash firmware */
191 { USB_DEVICE(0x0489, 0xe027), .driver_info = BTUSB_IGNORE },
192 { USB_DEVICE(0x0489, 0xe03d), .driver_info = BTUSB_IGNORE },
193 { USB_DEVICE(0x04f2, 0xaff1), .driver_info = BTUSB_IGNORE },
194 { USB_DEVICE(0x0930, 0x0215), .driver_info = BTUSB_IGNORE },
195 { USB_DEVICE(0x0cf3, 0x3002), .driver_info = BTUSB_IGNORE },
196 { USB_DEVICE(0x0cf3, 0xe019), .driver_info = BTUSB_IGNORE },
197 { USB_DEVICE(0x13d3, 0x3304), .driver_info = BTUSB_IGNORE },
199 /* Atheros AR9285 Malbec with sflash firmware */
200 { USB_DEVICE(0x03f0, 0x311d), .driver_info = BTUSB_IGNORE },
202 /* Atheros 3012 with sflash firmware */
203 { USB_DEVICE(0x0489, 0xe04d), .driver_info = BTUSB_ATH3012 },
204 { USB_DEVICE(0x0489, 0xe04e), .driver_info = BTUSB_ATH3012 },
205 { USB_DEVICE(0x0489, 0xe056), .driver_info = BTUSB_ATH3012 },
206 { USB_DEVICE(0x0489, 0xe057), .driver_info = BTUSB_ATH3012 },
207 { USB_DEVICE(0x0489, 0xe05f), .driver_info = BTUSB_ATH3012 },
208 { USB_DEVICE(0x0489, 0xe076), .driver_info = BTUSB_ATH3012 },
209 { USB_DEVICE(0x0489, 0xe078), .driver_info = BTUSB_ATH3012 },
210 { USB_DEVICE(0x0489, 0xe095), .driver_info = BTUSB_ATH3012 },
211 { USB_DEVICE(0x04c5, 0x1330), .driver_info = BTUSB_ATH3012 },
212 { USB_DEVICE(0x04ca, 0x3004), .driver_info = BTUSB_ATH3012 },
213 { USB_DEVICE(0x04ca, 0x3005), .driver_info = BTUSB_ATH3012 },
214 { USB_DEVICE(0x04ca, 0x3006), .driver_info = BTUSB_ATH3012 },
215 { USB_DEVICE(0x04ca, 0x3007), .driver_info = BTUSB_ATH3012 },
216 { USB_DEVICE(0x04ca, 0x3008), .driver_info = BTUSB_ATH3012 },
217 { USB_DEVICE(0x04ca, 0x300b), .driver_info = BTUSB_ATH3012 },
218 { USB_DEVICE(0x04ca, 0x300d), .driver_info = BTUSB_ATH3012 },
219 { USB_DEVICE(0x04ca, 0x300f), .driver_info = BTUSB_ATH3012 },
220 { USB_DEVICE(0x04ca, 0x3010), .driver_info = BTUSB_ATH3012 },
221 { USB_DEVICE(0x04ca, 0x3014), .driver_info = BTUSB_ATH3012 },
222 { USB_DEVICE(0x04ca, 0x3018), .driver_info = BTUSB_ATH3012 },
223 { USB_DEVICE(0x0930, 0x0219), .driver_info = BTUSB_ATH3012 },
224 { USB_DEVICE(0x0930, 0x021c), .driver_info = BTUSB_ATH3012 },
225 { USB_DEVICE(0x0930, 0x0220), .driver_info = BTUSB_ATH3012 },
226 { USB_DEVICE(0x0930, 0x0227), .driver_info = BTUSB_ATH3012 },
227 { USB_DEVICE(0x0b05, 0x17d0), .driver_info = BTUSB_ATH3012 },
228 { USB_DEVICE(0x0cf3, 0x0036), .driver_info = BTUSB_ATH3012 },
229 { USB_DEVICE(0x0cf3, 0x3004), .driver_info = BTUSB_ATH3012 },
230 { USB_DEVICE(0x0cf3, 0x3008), .driver_info = BTUSB_ATH3012 },
231 { USB_DEVICE(0x0cf3, 0x311d), .driver_info = BTUSB_ATH3012 },
232 { USB_DEVICE(0x0cf3, 0x311e), .driver_info = BTUSB_ATH3012 },
233 { USB_DEVICE(0x0cf3, 0x311f), .driver_info = BTUSB_ATH3012 },
234 { USB_DEVICE(0x0cf3, 0x3121), .driver_info = BTUSB_ATH3012 },
235 { USB_DEVICE(0x0cf3, 0x817a), .driver_info = BTUSB_ATH3012 },
236 { USB_DEVICE(0x0cf3, 0x817b), .driver_info = BTUSB_ATH3012 },
237 { USB_DEVICE(0x0cf3, 0xe003), .driver_info = BTUSB_ATH3012 },
238 { USB_DEVICE(0x0cf3, 0xe004), .driver_info = BTUSB_ATH3012 },
239 { USB_DEVICE(0x0cf3, 0xe005), .driver_info = BTUSB_ATH3012 },
240 { USB_DEVICE(0x0cf3, 0xe006), .driver_info = BTUSB_ATH3012 },
241 { USB_DEVICE(0x13d3, 0x3362), .driver_info = BTUSB_ATH3012 },
242 { USB_DEVICE(0x13d3, 0x3375), .driver_info = BTUSB_ATH3012 },
243 { USB_DEVICE(0x13d3, 0x3393), .driver_info = BTUSB_ATH3012 },
244 { USB_DEVICE(0x13d3, 0x3395), .driver_info = BTUSB_ATH3012 },
245 { USB_DEVICE(0x13d3, 0x3402), .driver_info = BTUSB_ATH3012 },
246 { USB_DEVICE(0x13d3, 0x3408), .driver_info = BTUSB_ATH3012 },
247 { USB_DEVICE(0x13d3, 0x3423), .driver_info = BTUSB_ATH3012 },
248 { USB_DEVICE(0x13d3, 0x3432), .driver_info = BTUSB_ATH3012 },
249 { USB_DEVICE(0x13d3, 0x3472), .driver_info = BTUSB_ATH3012 },
250 { USB_DEVICE(0x13d3, 0x3474), .driver_info = BTUSB_ATH3012 },
251 { USB_DEVICE(0x13d3, 0x3487), .driver_info = BTUSB_ATH3012 },
252 { USB_DEVICE(0x13d3, 0x3490), .driver_info = BTUSB_ATH3012 },
254 /* Atheros AR5BBU12 with sflash firmware */
255 { USB_DEVICE(0x0489, 0xe02c), .driver_info = BTUSB_IGNORE },
257 /* Atheros AR5BBU12 with sflash firmware */
258 { USB_DEVICE(0x0489, 0xe036), .driver_info = BTUSB_ATH3012 },
259 { USB_DEVICE(0x0489, 0xe03c), .driver_info = BTUSB_ATH3012 },
261 /* QCA ROME chipset */
262 { USB_DEVICE(0x0cf3, 0xe007), .driver_info = BTUSB_QCA_ROME },
263 { USB_DEVICE(0x0cf3, 0xe009), .driver_info = BTUSB_QCA_ROME },
264 { USB_DEVICE(0x0cf3, 0xe300), .driver_info = BTUSB_QCA_ROME },
265 { USB_DEVICE(0x0cf3, 0xe360), .driver_info = BTUSB_QCA_ROME },
266 { USB_DEVICE(0x0489, 0xe092), .driver_info = BTUSB_QCA_ROME },
267 { USB_DEVICE(0x04ca, 0x3011), .driver_info = BTUSB_QCA_ROME },
269 /* Broadcom BCM2035 */
270 { USB_DEVICE(0x0a5c, 0x2009), .driver_info = BTUSB_BCM92035 },
271 { USB_DEVICE(0x0a5c, 0x200a), .driver_info = BTUSB_WRONG_SCO_MTU },
272 { USB_DEVICE(0x0a5c, 0x2035), .driver_info = BTUSB_WRONG_SCO_MTU },
274 /* Broadcom BCM2045 */
275 { USB_DEVICE(0x0a5c, 0x2039), .driver_info = BTUSB_WRONG_SCO_MTU },
276 { USB_DEVICE(0x0a5c, 0x2101), .driver_info = BTUSB_WRONG_SCO_MTU },
278 /* IBM/Lenovo ThinkPad with Broadcom chip */
279 { USB_DEVICE(0x0a5c, 0x201e), .driver_info = BTUSB_WRONG_SCO_MTU },
280 { USB_DEVICE(0x0a5c, 0x2110), .driver_info = BTUSB_WRONG_SCO_MTU },
282 /* HP laptop with Broadcom chip */
283 { USB_DEVICE(0x03f0, 0x171d), .driver_info = BTUSB_WRONG_SCO_MTU },
285 /* Dell laptop with Broadcom chip */
286 { USB_DEVICE(0x413c, 0x8126), .driver_info = BTUSB_WRONG_SCO_MTU },
288 /* Dell Wireless 370 and 410 devices */
289 { USB_DEVICE(0x413c, 0x8152), .driver_info = BTUSB_WRONG_SCO_MTU },
290 { USB_DEVICE(0x413c, 0x8156), .driver_info = BTUSB_WRONG_SCO_MTU },
292 /* Belkin F8T012 and F8T013 devices */
293 { USB_DEVICE(0x050d, 0x0012), .driver_info = BTUSB_WRONG_SCO_MTU },
294 { USB_DEVICE(0x050d, 0x0013), .driver_info = BTUSB_WRONG_SCO_MTU },
296 /* Asus WL-BTD202 device */
297 { USB_DEVICE(0x0b05, 0x1715), .driver_info = BTUSB_WRONG_SCO_MTU },
299 /* Kensington Bluetooth USB adapter */
300 { USB_DEVICE(0x047d, 0x105e), .driver_info = BTUSB_WRONG_SCO_MTU },
302 /* RTX Telecom based adapters with buggy SCO support */
303 { USB_DEVICE(0x0400, 0x0807), .driver_info = BTUSB_BROKEN_ISOC },
304 { USB_DEVICE(0x0400, 0x080a), .driver_info = BTUSB_BROKEN_ISOC },
306 /* CONWISE Technology based adapters with buggy SCO support */
307 { USB_DEVICE(0x0e5e, 0x6622),
308 .driver_info = BTUSB_BROKEN_ISOC | BTUSB_CW6622},
310 /* Roper Class 1 Bluetooth Dongle (Silicon Wave based) */
311 { USB_DEVICE(0x1310, 0x0001), .driver_info = BTUSB_SWAVE },
313 /* Digianswer devices */
314 { USB_DEVICE(0x08fd, 0x0001), .driver_info = BTUSB_DIGIANSWER },
315 { USB_DEVICE(0x08fd, 0x0002), .driver_info = BTUSB_IGNORE },
317 /* CSR BlueCore Bluetooth Sniffer */
318 { USB_DEVICE(0x0a12, 0x0002),
319 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
321 /* Frontline ComProbe Bluetooth Sniffer */
322 { USB_DEVICE(0x16d3, 0x0002),
323 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
325 /* Marvell Bluetooth devices */
326 { USB_DEVICE(0x1286, 0x2044), .driver_info = BTUSB_MARVELL },
327 { USB_DEVICE(0x1286, 0x2046), .driver_info = BTUSB_MARVELL },
328 { USB_DEVICE(0x1286, 0x204e), .driver_info = BTUSB_MARVELL },
330 /* Intel Bluetooth devices */
331 { USB_DEVICE(0x8087, 0x0025), .driver_info = BTUSB_INTEL_NEW },
332 { USB_DEVICE(0x8087, 0x07da), .driver_info = BTUSB_CSR },
333 { USB_DEVICE(0x8087, 0x07dc), .driver_info = BTUSB_INTEL },
334 { USB_DEVICE(0x8087, 0x0a2a), .driver_info = BTUSB_INTEL },
335 { USB_DEVICE(0x8087, 0x0a2b), .driver_info = BTUSB_INTEL_NEW },
336 { USB_DEVICE(0x8087, 0x0aa7), .driver_info = BTUSB_INTEL },
338 /* Other Intel Bluetooth devices */
339 { USB_VENDOR_AND_INTERFACE_INFO(0x8087, 0xe0, 0x01, 0x01),
340 .driver_info = BTUSB_IGNORE },
342 /* Realtek Bluetooth devices */
343 { USB_VENDOR_AND_INTERFACE_INFO(0x0bda, 0xe0, 0x01, 0x01),
344 .driver_info = BTUSB_REALTEK },
346 /* Additional Realtek 8723AE Bluetooth devices */
347 { USB_DEVICE(0x0930, 0x021d), .driver_info = BTUSB_REALTEK },
348 { USB_DEVICE(0x13d3, 0x3394), .driver_info = BTUSB_REALTEK },
350 /* Additional Realtek 8723BE Bluetooth devices */
351 { USB_DEVICE(0x0489, 0xe085), .driver_info = BTUSB_REALTEK },
352 { USB_DEVICE(0x0489, 0xe08b), .driver_info = BTUSB_REALTEK },
353 { USB_DEVICE(0x13d3, 0x3410), .driver_info = BTUSB_REALTEK },
354 { USB_DEVICE(0x13d3, 0x3416), .driver_info = BTUSB_REALTEK },
355 { USB_DEVICE(0x13d3, 0x3459), .driver_info = BTUSB_REALTEK },
357 /* Additional Realtek 8821AE Bluetooth devices */
358 { USB_DEVICE(0x0b05, 0x17dc), .driver_info = BTUSB_REALTEK },
359 { USB_DEVICE(0x13d3, 0x3414), .driver_info = BTUSB_REALTEK },
360 { USB_DEVICE(0x13d3, 0x3458), .driver_info = BTUSB_REALTEK },
361 { USB_DEVICE(0x13d3, 0x3461), .driver_info = BTUSB_REALTEK },
362 { USB_DEVICE(0x13d3, 0x3462), .driver_info = BTUSB_REALTEK },
364 /* Silicon Wave based devices */
365 { USB_DEVICE(0x0c10, 0x0000), .driver_info = BTUSB_SWAVE },
367 { } /* Terminating entry */
370 #define BTUSB_MAX_ISOC_FRAMES 10
372 #define BTUSB_INTR_RUNNING 0
373 #define BTUSB_BULK_RUNNING 1
374 #define BTUSB_ISOC_RUNNING 2
375 #define BTUSB_SUSPENDING 3
376 #define BTUSB_DID_ISO_RESUME 4
377 #define BTUSB_BOOTLOADER 5
378 #define BTUSB_DOWNLOADING 6
379 #define BTUSB_FIRMWARE_LOADED 7
380 #define BTUSB_FIRMWARE_FAILED 8
381 #define BTUSB_BOOTING 9
382 #define BTUSB_RESET_RESUME 10
383 #define BTUSB_DIAG_RUNNING 11
384 #define BTUSB_OOB_WAKE_ENABLED 12
387 struct hci_dev *hdev;
388 struct usb_device *udev;
389 struct usb_interface *intf;
390 struct usb_interface *isoc;
391 struct usb_interface *diag;
395 struct work_struct work;
396 struct work_struct waker;
398 struct usb_anchor deferred;
399 struct usb_anchor tx_anchor;
403 struct usb_anchor intr_anchor;
404 struct usb_anchor bulk_anchor;
405 struct usb_anchor isoc_anchor;
406 struct usb_anchor diag_anchor;
409 struct sk_buff *evt_skb;
410 struct sk_buff *acl_skb;
411 struct sk_buff *sco_skb;
413 struct usb_endpoint_descriptor *intr_ep;
414 struct usb_endpoint_descriptor *bulk_tx_ep;
415 struct usb_endpoint_descriptor *bulk_rx_ep;
416 struct usb_endpoint_descriptor *isoc_tx_ep;
417 struct usb_endpoint_descriptor *isoc_rx_ep;
418 struct usb_endpoint_descriptor *diag_tx_ep;
419 struct usb_endpoint_descriptor *diag_rx_ep;
424 unsigned int sco_num;
428 int (*recv_event)(struct hci_dev *hdev, struct sk_buff *skb);
429 int (*recv_bulk)(struct btusb_data *data, void *buffer, int count);
431 int (*setup_on_usb)(struct hci_dev *hdev);
433 int oob_wake_irq; /* irq for out-of-band wake-on-bt */
436 static inline void btusb_free_frags(struct btusb_data *data)
440 spin_lock_irqsave(&data->rxlock, flags);
442 kfree_skb(data->evt_skb);
443 data->evt_skb = NULL;
445 kfree_skb(data->acl_skb);
446 data->acl_skb = NULL;
448 kfree_skb(data->sco_skb);
449 data->sco_skb = NULL;
451 spin_unlock_irqrestore(&data->rxlock, flags);
454 static int btusb_recv_intr(struct btusb_data *data, void *buffer, int count)
459 spin_lock(&data->rxlock);
466 skb = bt_skb_alloc(HCI_MAX_EVENT_SIZE, GFP_ATOMIC);
472 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
473 hci_skb_expect(skb) = HCI_EVENT_HDR_SIZE;
476 len = min_t(uint, hci_skb_expect(skb), count);
477 memcpy(skb_put(skb, len), buffer, len);
481 hci_skb_expect(skb) -= len;
483 if (skb->len == HCI_EVENT_HDR_SIZE) {
484 /* Complete event header */
485 hci_skb_expect(skb) = hci_event_hdr(skb)->plen;
487 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
496 if (!hci_skb_expect(skb)) {
498 data->recv_event(data->hdev, skb);
504 spin_unlock(&data->rxlock);
509 static int btusb_recv_bulk(struct btusb_data *data, void *buffer, int count)
514 spin_lock(&data->rxlock);
521 skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
527 hci_skb_pkt_type(skb) = HCI_ACLDATA_PKT;
528 hci_skb_expect(skb) = HCI_ACL_HDR_SIZE;
531 len = min_t(uint, hci_skb_expect(skb), count);
532 memcpy(skb_put(skb, len), buffer, len);
536 hci_skb_expect(skb) -= len;
538 if (skb->len == HCI_ACL_HDR_SIZE) {
539 __le16 dlen = hci_acl_hdr(skb)->dlen;
541 /* Complete ACL header */
542 hci_skb_expect(skb) = __le16_to_cpu(dlen);
544 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
553 if (!hci_skb_expect(skb)) {
555 hci_recv_frame(data->hdev, skb);
561 spin_unlock(&data->rxlock);
566 static int btusb_recv_isoc(struct btusb_data *data, void *buffer, int count)
571 spin_lock(&data->rxlock);
578 skb = bt_skb_alloc(HCI_MAX_SCO_SIZE, GFP_ATOMIC);
584 hci_skb_pkt_type(skb) = HCI_SCODATA_PKT;
585 hci_skb_expect(skb) = HCI_SCO_HDR_SIZE;
588 len = min_t(uint, hci_skb_expect(skb), count);
589 memcpy(skb_put(skb, len), buffer, len);
593 hci_skb_expect(skb) -= len;
595 if (skb->len == HCI_SCO_HDR_SIZE) {
596 /* Complete SCO header */
597 hci_skb_expect(skb) = hci_sco_hdr(skb)->dlen;
599 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
608 if (!hci_skb_expect(skb)) {
610 hci_recv_frame(data->hdev, skb);
616 spin_unlock(&data->rxlock);
621 static void btusb_intr_complete(struct urb *urb)
623 struct hci_dev *hdev = urb->context;
624 struct btusb_data *data = hci_get_drvdata(hdev);
627 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
630 if (!test_bit(HCI_RUNNING, &hdev->flags))
633 if (urb->status == 0) {
634 hdev->stat.byte_rx += urb->actual_length;
636 if (btusb_recv_intr(data, urb->transfer_buffer,
637 urb->actual_length) < 0) {
638 BT_ERR("%s corrupted event packet", hdev->name);
641 } else if (urb->status == -ENOENT) {
642 /* Avoid suspend failed when usb_kill_urb */
646 if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
649 usb_mark_last_busy(data->udev);
650 usb_anchor_urb(urb, &data->intr_anchor);
652 err = usb_submit_urb(urb, GFP_ATOMIC);
654 /* -EPERM: urb is being killed;
655 * -ENODEV: device got disconnected */
656 if (err != -EPERM && err != -ENODEV)
657 BT_ERR("%s urb %p failed to resubmit (%d)",
658 hdev->name, urb, -err);
659 usb_unanchor_urb(urb);
663 static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags)
665 struct btusb_data *data = hci_get_drvdata(hdev);
671 BT_DBG("%s", hdev->name);
676 urb = usb_alloc_urb(0, mem_flags);
680 size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
682 buf = kmalloc(size, mem_flags);
688 pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
690 usb_fill_int_urb(urb, data->udev, pipe, buf, size,
691 btusb_intr_complete, hdev, data->intr_ep->bInterval);
693 urb->transfer_flags |= URB_FREE_BUFFER;
695 usb_anchor_urb(urb, &data->intr_anchor);
697 err = usb_submit_urb(urb, mem_flags);
699 if (err != -EPERM && err != -ENODEV)
700 BT_ERR("%s urb %p submission failed (%d)",
701 hdev->name, urb, -err);
702 usb_unanchor_urb(urb);
710 static void btusb_bulk_complete(struct urb *urb)
712 struct hci_dev *hdev = urb->context;
713 struct btusb_data *data = hci_get_drvdata(hdev);
716 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
719 if (!test_bit(HCI_RUNNING, &hdev->flags))
722 if (urb->status == 0) {
723 hdev->stat.byte_rx += urb->actual_length;
725 if (data->recv_bulk(data, urb->transfer_buffer,
726 urb->actual_length) < 0) {
727 BT_ERR("%s corrupted ACL packet", hdev->name);
730 } else if (urb->status == -ENOENT) {
731 /* Avoid suspend failed when usb_kill_urb */
735 if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
738 usb_anchor_urb(urb, &data->bulk_anchor);
739 usb_mark_last_busy(data->udev);
741 err = usb_submit_urb(urb, GFP_ATOMIC);
743 /* -EPERM: urb is being killed;
744 * -ENODEV: device got disconnected */
745 if (err != -EPERM && err != -ENODEV)
746 BT_ERR("%s urb %p failed to resubmit (%d)",
747 hdev->name, urb, -err);
748 usb_unanchor_urb(urb);
752 static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
754 struct btusb_data *data = hci_get_drvdata(hdev);
758 int err, size = HCI_MAX_FRAME_SIZE;
760 BT_DBG("%s", hdev->name);
762 if (!data->bulk_rx_ep)
765 urb = usb_alloc_urb(0, mem_flags);
769 buf = kmalloc(size, mem_flags);
775 pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
777 usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
778 btusb_bulk_complete, hdev);
780 urb->transfer_flags |= URB_FREE_BUFFER;
782 usb_mark_last_busy(data->udev);
783 usb_anchor_urb(urb, &data->bulk_anchor);
785 err = usb_submit_urb(urb, mem_flags);
787 if (err != -EPERM && err != -ENODEV)
788 BT_ERR("%s urb %p submission failed (%d)",
789 hdev->name, urb, -err);
790 usb_unanchor_urb(urb);
798 static void btusb_isoc_complete(struct urb *urb)
800 struct hci_dev *hdev = urb->context;
801 struct btusb_data *data = hci_get_drvdata(hdev);
804 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
807 if (!test_bit(HCI_RUNNING, &hdev->flags))
810 if (urb->status == 0) {
811 for (i = 0; i < urb->number_of_packets; i++) {
812 unsigned int offset = urb->iso_frame_desc[i].offset;
813 unsigned int length = urb->iso_frame_desc[i].actual_length;
815 if (urb->iso_frame_desc[i].status)
818 hdev->stat.byte_rx += length;
820 if (btusb_recv_isoc(data, urb->transfer_buffer + offset,
822 BT_ERR("%s corrupted SCO packet", hdev->name);
826 } else if (urb->status == -ENOENT) {
827 /* Avoid suspend failed when usb_kill_urb */
831 if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags))
834 usb_anchor_urb(urb, &data->isoc_anchor);
836 err = usb_submit_urb(urb, GFP_ATOMIC);
838 /* -EPERM: urb is being killed;
839 * -ENODEV: device got disconnected */
840 if (err != -EPERM && err != -ENODEV)
841 BT_ERR("%s urb %p failed to resubmit (%d)",
842 hdev->name, urb, -err);
843 usb_unanchor_urb(urb);
847 static inline void __fill_isoc_descriptor(struct urb *urb, int len, int mtu)
851 BT_DBG("len %d mtu %d", len, mtu);
853 for (i = 0; i < BTUSB_MAX_ISOC_FRAMES && len >= mtu;
854 i++, offset += mtu, len -= mtu) {
855 urb->iso_frame_desc[i].offset = offset;
856 urb->iso_frame_desc[i].length = mtu;
859 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
860 urb->iso_frame_desc[i].offset = offset;
861 urb->iso_frame_desc[i].length = len;
865 urb->number_of_packets = i;
868 static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags)
870 struct btusb_data *data = hci_get_drvdata(hdev);
876 BT_DBG("%s", hdev->name);
878 if (!data->isoc_rx_ep)
881 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags);
885 size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) *
886 BTUSB_MAX_ISOC_FRAMES;
888 buf = kmalloc(size, mem_flags);
894 pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress);
896 usb_fill_int_urb(urb, data->udev, pipe, buf, size, btusb_isoc_complete,
897 hdev, data->isoc_rx_ep->bInterval);
899 urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP;
901 __fill_isoc_descriptor(urb, size,
902 le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize));
904 usb_anchor_urb(urb, &data->isoc_anchor);
906 err = usb_submit_urb(urb, mem_flags);
908 if (err != -EPERM && err != -ENODEV)
909 BT_ERR("%s urb %p submission failed (%d)",
910 hdev->name, urb, -err);
911 usb_unanchor_urb(urb);
919 static void btusb_diag_complete(struct urb *urb)
921 struct hci_dev *hdev = urb->context;
922 struct btusb_data *data = hci_get_drvdata(hdev);
925 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
928 if (urb->status == 0) {
931 skb = bt_skb_alloc(urb->actual_length, GFP_ATOMIC);
933 memcpy(skb_put(skb, urb->actual_length),
934 urb->transfer_buffer, urb->actual_length);
935 hci_recv_diag(hdev, skb);
937 } else if (urb->status == -ENOENT) {
938 /* Avoid suspend failed when usb_kill_urb */
942 if (!test_bit(BTUSB_DIAG_RUNNING, &data->flags))
945 usb_anchor_urb(urb, &data->diag_anchor);
946 usb_mark_last_busy(data->udev);
948 err = usb_submit_urb(urb, GFP_ATOMIC);
950 /* -EPERM: urb is being killed;
951 * -ENODEV: device got disconnected */
952 if (err != -EPERM && err != -ENODEV)
953 BT_ERR("%s urb %p failed to resubmit (%d)",
954 hdev->name, urb, -err);
955 usb_unanchor_urb(urb);
959 static int btusb_submit_diag_urb(struct hci_dev *hdev, gfp_t mem_flags)
961 struct btusb_data *data = hci_get_drvdata(hdev);
965 int err, size = HCI_MAX_FRAME_SIZE;
967 BT_DBG("%s", hdev->name);
969 if (!data->diag_rx_ep)
972 urb = usb_alloc_urb(0, mem_flags);
976 buf = kmalloc(size, mem_flags);
982 pipe = usb_rcvbulkpipe(data->udev, data->diag_rx_ep->bEndpointAddress);
984 usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
985 btusb_diag_complete, hdev);
987 urb->transfer_flags |= URB_FREE_BUFFER;
989 usb_mark_last_busy(data->udev);
990 usb_anchor_urb(urb, &data->diag_anchor);
992 err = usb_submit_urb(urb, mem_flags);
994 if (err != -EPERM && err != -ENODEV)
995 BT_ERR("%s urb %p submission failed (%d)",
996 hdev->name, urb, -err);
997 usb_unanchor_urb(urb);
1005 static void btusb_tx_complete(struct urb *urb)
1007 struct sk_buff *skb = urb->context;
1008 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
1009 struct btusb_data *data = hci_get_drvdata(hdev);
1011 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1012 urb->actual_length);
1014 if (!test_bit(HCI_RUNNING, &hdev->flags))
1018 hdev->stat.byte_tx += urb->transfer_buffer_length;
1020 hdev->stat.err_tx++;
1023 spin_lock(&data->txlock);
1024 data->tx_in_flight--;
1025 spin_unlock(&data->txlock);
1027 kfree(urb->setup_packet);
1032 static void btusb_isoc_tx_complete(struct urb *urb)
1034 struct sk_buff *skb = urb->context;
1035 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
1037 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1038 urb->actual_length);
1040 if (!test_bit(HCI_RUNNING, &hdev->flags))
1044 hdev->stat.byte_tx += urb->transfer_buffer_length;
1046 hdev->stat.err_tx++;
1049 kfree(urb->setup_packet);
1054 static int btusb_open(struct hci_dev *hdev)
1056 struct btusb_data *data = hci_get_drvdata(hdev);
1059 BT_DBG("%s", hdev->name);
1061 err = usb_autopm_get_interface(data->intf);
1065 /* Patching USB firmware files prior to starting any URBs of HCI path
1066 * It is more safe to use USB bulk channel for downloading USB patch
1068 if (data->setup_on_usb) {
1069 err = data->setup_on_usb(hdev);
1074 data->intf->needs_remote_wakeup = 1;
1076 if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
1079 err = btusb_submit_intr_urb(hdev, GFP_KERNEL);
1083 err = btusb_submit_bulk_urb(hdev, GFP_KERNEL);
1085 usb_kill_anchored_urbs(&data->intr_anchor);
1089 set_bit(BTUSB_BULK_RUNNING, &data->flags);
1090 btusb_submit_bulk_urb(hdev, GFP_KERNEL);
1093 if (!btusb_submit_diag_urb(hdev, GFP_KERNEL))
1094 set_bit(BTUSB_DIAG_RUNNING, &data->flags);
1098 usb_autopm_put_interface(data->intf);
1102 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
1103 usb_autopm_put_interface(data->intf);
1107 static void btusb_stop_traffic(struct btusb_data *data)
1109 usb_kill_anchored_urbs(&data->intr_anchor);
1110 usb_kill_anchored_urbs(&data->bulk_anchor);
1111 usb_kill_anchored_urbs(&data->isoc_anchor);
1112 usb_kill_anchored_urbs(&data->diag_anchor);
1115 static int btusb_close(struct hci_dev *hdev)
1117 struct btusb_data *data = hci_get_drvdata(hdev);
1120 BT_DBG("%s", hdev->name);
1122 cancel_work_sync(&data->work);
1123 cancel_work_sync(&data->waker);
1125 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1126 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
1127 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
1128 clear_bit(BTUSB_DIAG_RUNNING, &data->flags);
1130 btusb_stop_traffic(data);
1131 btusb_free_frags(data);
1133 err = usb_autopm_get_interface(data->intf);
1137 data->intf->needs_remote_wakeup = 0;
1138 usb_autopm_put_interface(data->intf);
1141 usb_scuttle_anchored_urbs(&data->deferred);
1145 static int btusb_flush(struct hci_dev *hdev)
1147 struct btusb_data *data = hci_get_drvdata(hdev);
1149 BT_DBG("%s", hdev->name);
1151 usb_kill_anchored_urbs(&data->tx_anchor);
1152 btusb_free_frags(data);
1157 static struct urb *alloc_ctrl_urb(struct hci_dev *hdev, struct sk_buff *skb)
1159 struct btusb_data *data = hci_get_drvdata(hdev);
1160 struct usb_ctrlrequest *dr;
1164 urb = usb_alloc_urb(0, GFP_KERNEL);
1166 return ERR_PTR(-ENOMEM);
1168 dr = kmalloc(sizeof(*dr), GFP_KERNEL);
1171 return ERR_PTR(-ENOMEM);
1174 dr->bRequestType = data->cmdreq_type;
1175 dr->bRequest = data->cmdreq;
1178 dr->wLength = __cpu_to_le16(skb->len);
1180 pipe = usb_sndctrlpipe(data->udev, 0x00);
1182 usb_fill_control_urb(urb, data->udev, pipe, (void *)dr,
1183 skb->data, skb->len, btusb_tx_complete, skb);
1185 skb->dev = (void *)hdev;
1190 static struct urb *alloc_bulk_urb(struct hci_dev *hdev, struct sk_buff *skb)
1192 struct btusb_data *data = hci_get_drvdata(hdev);
1196 if (!data->bulk_tx_ep)
1197 return ERR_PTR(-ENODEV);
1199 urb = usb_alloc_urb(0, GFP_KERNEL);
1201 return ERR_PTR(-ENOMEM);
1203 pipe = usb_sndbulkpipe(data->udev, data->bulk_tx_ep->bEndpointAddress);
1205 usb_fill_bulk_urb(urb, data->udev, pipe,
1206 skb->data, skb->len, btusb_tx_complete, skb);
1208 skb->dev = (void *)hdev;
1213 static struct urb *alloc_isoc_urb(struct hci_dev *hdev, struct sk_buff *skb)
1215 struct btusb_data *data = hci_get_drvdata(hdev);
1219 if (!data->isoc_tx_ep)
1220 return ERR_PTR(-ENODEV);
1222 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_KERNEL);
1224 return ERR_PTR(-ENOMEM);
1226 pipe = usb_sndisocpipe(data->udev, data->isoc_tx_ep->bEndpointAddress);
1228 usb_fill_int_urb(urb, data->udev, pipe,
1229 skb->data, skb->len, btusb_isoc_tx_complete,
1230 skb, data->isoc_tx_ep->bInterval);
1232 urb->transfer_flags = URB_ISO_ASAP;
1234 __fill_isoc_descriptor(urb, skb->len,
1235 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
1237 skb->dev = (void *)hdev;
1242 static int submit_tx_urb(struct hci_dev *hdev, struct urb *urb)
1244 struct btusb_data *data = hci_get_drvdata(hdev);
1247 usb_anchor_urb(urb, &data->tx_anchor);
1249 err = usb_submit_urb(urb, GFP_KERNEL);
1251 if (err != -EPERM && err != -ENODEV)
1252 BT_ERR("%s urb %p submission failed (%d)",
1253 hdev->name, urb, -err);
1254 kfree(urb->setup_packet);
1255 usb_unanchor_urb(urb);
1257 usb_mark_last_busy(data->udev);
1264 static int submit_or_queue_tx_urb(struct hci_dev *hdev, struct urb *urb)
1266 struct btusb_data *data = hci_get_drvdata(hdev);
1267 unsigned long flags;
1270 spin_lock_irqsave(&data->txlock, flags);
1271 suspending = test_bit(BTUSB_SUSPENDING, &data->flags);
1273 data->tx_in_flight++;
1274 spin_unlock_irqrestore(&data->txlock, flags);
1277 return submit_tx_urb(hdev, urb);
1279 usb_anchor_urb(urb, &data->deferred);
1280 schedule_work(&data->waker);
1286 static int btusb_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
1290 BT_DBG("%s", hdev->name);
1292 switch (hci_skb_pkt_type(skb)) {
1293 case HCI_COMMAND_PKT:
1294 urb = alloc_ctrl_urb(hdev, skb);
1296 return PTR_ERR(urb);
1298 hdev->stat.cmd_tx++;
1299 return submit_or_queue_tx_urb(hdev, urb);
1301 case HCI_ACLDATA_PKT:
1302 urb = alloc_bulk_urb(hdev, skb);
1304 return PTR_ERR(urb);
1306 hdev->stat.acl_tx++;
1307 return submit_or_queue_tx_urb(hdev, urb);
1309 case HCI_SCODATA_PKT:
1310 if (hci_conn_num(hdev, SCO_LINK) < 1)
1313 urb = alloc_isoc_urb(hdev, skb);
1315 return PTR_ERR(urb);
1317 hdev->stat.sco_tx++;
1318 return submit_tx_urb(hdev, urb);
1324 static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
1326 struct btusb_data *data = hci_get_drvdata(hdev);
1328 BT_DBG("%s evt %d", hdev->name, evt);
1330 if (hci_conn_num(hdev, SCO_LINK) != data->sco_num) {
1331 data->sco_num = hci_conn_num(hdev, SCO_LINK);
1332 schedule_work(&data->work);
1336 static inline int __set_isoc_interface(struct hci_dev *hdev, int altsetting)
1338 struct btusb_data *data = hci_get_drvdata(hdev);
1339 struct usb_interface *intf = data->isoc;
1340 struct usb_endpoint_descriptor *ep_desc;
1346 err = usb_set_interface(data->udev, 1, altsetting);
1348 BT_ERR("%s setting interface failed (%d)", hdev->name, -err);
1352 data->isoc_altsetting = altsetting;
1354 data->isoc_tx_ep = NULL;
1355 data->isoc_rx_ep = NULL;
1357 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
1358 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
1360 if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) {
1361 data->isoc_tx_ep = ep_desc;
1365 if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) {
1366 data->isoc_rx_ep = ep_desc;
1371 if (!data->isoc_tx_ep || !data->isoc_rx_ep) {
1372 BT_ERR("%s invalid SCO descriptors", hdev->name);
1379 static void btusb_work(struct work_struct *work)
1381 struct btusb_data *data = container_of(work, struct btusb_data, work);
1382 struct hci_dev *hdev = data->hdev;
1386 if (data->sco_num > 0) {
1387 if (!test_bit(BTUSB_DID_ISO_RESUME, &data->flags)) {
1388 err = usb_autopm_get_interface(data->isoc ? data->isoc : data->intf);
1390 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1391 usb_kill_anchored_urbs(&data->isoc_anchor);
1395 set_bit(BTUSB_DID_ISO_RESUME, &data->flags);
1398 if (hdev->voice_setting & 0x0020) {
1399 static const int alts[3] = { 2, 4, 5 };
1401 new_alts = alts[data->sco_num - 1];
1403 new_alts = data->sco_num;
1406 if (data->isoc_altsetting != new_alts) {
1407 unsigned long flags;
1409 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1410 usb_kill_anchored_urbs(&data->isoc_anchor);
1412 /* When isochronous alternate setting needs to be
1413 * changed, because SCO connection has been added
1414 * or removed, a packet fragment may be left in the
1415 * reassembling state. This could lead to wrongly
1416 * assembled fragments.
1418 * Clear outstanding fragment when selecting a new
1419 * alternate setting.
1421 spin_lock_irqsave(&data->rxlock, flags);
1422 kfree_skb(data->sco_skb);
1423 data->sco_skb = NULL;
1424 spin_unlock_irqrestore(&data->rxlock, flags);
1426 if (__set_isoc_interface(hdev, new_alts) < 0)
1430 if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
1431 if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0)
1432 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1434 btusb_submit_isoc_urb(hdev, GFP_KERNEL);
1437 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1438 usb_kill_anchored_urbs(&data->isoc_anchor);
1440 __set_isoc_interface(hdev, 0);
1441 if (test_and_clear_bit(BTUSB_DID_ISO_RESUME, &data->flags))
1442 usb_autopm_put_interface(data->isoc ? data->isoc : data->intf);
1446 static void btusb_waker(struct work_struct *work)
1448 struct btusb_data *data = container_of(work, struct btusb_data, waker);
1451 err = usb_autopm_get_interface(data->intf);
1455 usb_autopm_put_interface(data->intf);
1458 static int btusb_setup_bcm92035(struct hci_dev *hdev)
1460 struct sk_buff *skb;
1463 BT_DBG("%s", hdev->name);
1465 skb = __hci_cmd_sync(hdev, 0xfc3b, 1, &val, HCI_INIT_TIMEOUT);
1467 BT_ERR("BCM92035 command failed (%ld)", -PTR_ERR(skb));
1474 static int btusb_setup_csr(struct hci_dev *hdev)
1476 struct hci_rp_read_local_version *rp;
1477 struct sk_buff *skb;
1479 BT_DBG("%s", hdev->name);
1481 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
1484 int err = PTR_ERR(skb);
1485 BT_ERR("%s: CSR: Local version failed (%d)", hdev->name, err);
1489 if (skb->len != sizeof(struct hci_rp_read_local_version)) {
1490 BT_ERR("%s: CSR: Local version length mismatch", hdev->name);
1495 rp = (struct hci_rp_read_local_version *)skb->data;
1497 /* Detect controllers which aren't real CSR ones. */
1498 if (le16_to_cpu(rp->manufacturer) != 10 ||
1499 le16_to_cpu(rp->lmp_subver) == 0x0c5c) {
1500 /* Clear the reset quirk since this is not an actual
1501 * early Bluetooth 1.1 device from CSR.
1503 clear_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
1505 /* These fake CSR controllers have all a broken
1506 * stored link key handling and so just disable it.
1508 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
1516 static const struct firmware *btusb_setup_intel_get_fw(struct hci_dev *hdev,
1517 struct intel_version *ver)
1519 const struct firmware *fw;
1523 snprintf(fwname, sizeof(fwname),
1524 "intel/ibt-hw-%x.%x.%x-fw-%x.%x.%x.%x.%x.bseq",
1525 ver->hw_platform, ver->hw_variant, ver->hw_revision,
1526 ver->fw_variant, ver->fw_revision, ver->fw_build_num,
1527 ver->fw_build_ww, ver->fw_build_yy);
1529 ret = request_firmware(&fw, fwname, &hdev->dev);
1531 if (ret == -EINVAL) {
1532 BT_ERR("%s Intel firmware file request failed (%d)",
1537 BT_ERR("%s failed to open Intel firmware file: %s(%d)",
1538 hdev->name, fwname, ret);
1540 /* If the correct firmware patch file is not found, use the
1541 * default firmware patch file instead
1543 snprintf(fwname, sizeof(fwname), "intel/ibt-hw-%x.%x.bseq",
1544 ver->hw_platform, ver->hw_variant);
1545 if (request_firmware(&fw, fwname, &hdev->dev) < 0) {
1546 BT_ERR("%s failed to open default Intel fw file: %s",
1547 hdev->name, fwname);
1552 BT_INFO("%s: Intel Bluetooth firmware file: %s", hdev->name, fwname);
1557 static int btusb_setup_intel_patching(struct hci_dev *hdev,
1558 const struct firmware *fw,
1559 const u8 **fw_ptr, int *disable_patch)
1561 struct sk_buff *skb;
1562 struct hci_command_hdr *cmd;
1563 const u8 *cmd_param;
1564 struct hci_event_hdr *evt = NULL;
1565 const u8 *evt_param = NULL;
1566 int remain = fw->size - (*fw_ptr - fw->data);
1568 /* The first byte indicates the types of the patch command or event.
1569 * 0x01 means HCI command and 0x02 is HCI event. If the first bytes
1570 * in the current firmware buffer doesn't start with 0x01 or
1571 * the size of remain buffer is smaller than HCI command header,
1572 * the firmware file is corrupted and it should stop the patching
1575 if (remain > HCI_COMMAND_HDR_SIZE && *fw_ptr[0] != 0x01) {
1576 BT_ERR("%s Intel fw corrupted: invalid cmd read", hdev->name);
1582 cmd = (struct hci_command_hdr *)(*fw_ptr);
1583 *fw_ptr += sizeof(*cmd);
1584 remain -= sizeof(*cmd);
1586 /* Ensure that the remain firmware data is long enough than the length
1587 * of command parameter. If not, the firmware file is corrupted.
1589 if (remain < cmd->plen) {
1590 BT_ERR("%s Intel fw corrupted: invalid cmd len", hdev->name);
1594 /* If there is a command that loads a patch in the firmware
1595 * file, then enable the patch upon success, otherwise just
1596 * disable the manufacturer mode, for example patch activation
1597 * is not required when the default firmware patch file is used
1598 * because there are no patch data to load.
1600 if (*disable_patch && le16_to_cpu(cmd->opcode) == 0xfc8e)
1603 cmd_param = *fw_ptr;
1604 *fw_ptr += cmd->plen;
1605 remain -= cmd->plen;
1607 /* This reads the expected events when the above command is sent to the
1608 * device. Some vendor commands expects more than one events, for
1609 * example command status event followed by vendor specific event.
1610 * For this case, it only keeps the last expected event. so the command
1611 * can be sent with __hci_cmd_sync_ev() which returns the sk_buff of
1612 * last expected event.
1614 while (remain > HCI_EVENT_HDR_SIZE && *fw_ptr[0] == 0x02) {
1618 evt = (struct hci_event_hdr *)(*fw_ptr);
1619 *fw_ptr += sizeof(*evt);
1620 remain -= sizeof(*evt);
1622 if (remain < evt->plen) {
1623 BT_ERR("%s Intel fw corrupted: invalid evt len",
1628 evt_param = *fw_ptr;
1629 *fw_ptr += evt->plen;
1630 remain -= evt->plen;
1633 /* Every HCI commands in the firmware file has its correspond event.
1634 * If event is not found or remain is smaller than zero, the firmware
1635 * file is corrupted.
1637 if (!evt || !evt_param || remain < 0) {
1638 BT_ERR("%s Intel fw corrupted: invalid evt read", hdev->name);
1642 skb = __hci_cmd_sync_ev(hdev, le16_to_cpu(cmd->opcode), cmd->plen,
1643 cmd_param, evt->evt, HCI_INIT_TIMEOUT);
1645 BT_ERR("%s sending Intel patch command (0x%4.4x) failed (%ld)",
1646 hdev->name, cmd->opcode, PTR_ERR(skb));
1647 return PTR_ERR(skb);
1650 /* It ensures that the returned event matches the event data read from
1651 * the firmware file. At fist, it checks the length and then
1652 * the contents of the event.
1654 if (skb->len != evt->plen) {
1655 BT_ERR("%s mismatch event length (opcode 0x%4.4x)", hdev->name,
1656 le16_to_cpu(cmd->opcode));
1661 if (memcmp(skb->data, evt_param, evt->plen)) {
1662 BT_ERR("%s mismatch event parameter (opcode 0x%4.4x)",
1663 hdev->name, le16_to_cpu(cmd->opcode));
1672 static int btusb_setup_intel(struct hci_dev *hdev)
1674 struct sk_buff *skb;
1675 const struct firmware *fw;
1677 int disable_patch, err;
1678 struct intel_version ver;
1680 BT_DBG("%s", hdev->name);
1682 /* The controller has a bug with the first HCI command sent to it
1683 * returning number of completed commands as zero. This would stall the
1684 * command processing in the Bluetooth core.
1686 * As a workaround, send HCI Reset command first which will reset the
1687 * number of completed commands and allow normal command processing
1690 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
1692 BT_ERR("%s sending initial HCI reset command failed (%ld)",
1693 hdev->name, PTR_ERR(skb));
1694 return PTR_ERR(skb);
1698 /* Read Intel specific controller version first to allow selection of
1699 * which firmware file to load.
1701 * The returned information are hardware variant and revision plus
1702 * firmware variant, revision and build number.
1704 err = btintel_read_version(hdev, &ver);
1708 BT_INFO("%s: read Intel version: %02x%02x%02x%02x%02x%02x%02x%02x%02x",
1709 hdev->name, ver.hw_platform, ver.hw_variant, ver.hw_revision,
1710 ver.fw_variant, ver.fw_revision, ver.fw_build_num,
1711 ver.fw_build_ww, ver.fw_build_yy, ver.fw_patch_num);
1713 /* fw_patch_num indicates the version of patch the device currently
1714 * have. If there is no patch data in the device, it is always 0x00.
1715 * So, if it is other than 0x00, no need to patch the device again.
1717 if (ver.fw_patch_num) {
1718 BT_INFO("%s: Intel device is already patched. patch num: %02x",
1719 hdev->name, ver.fw_patch_num);
1723 /* Opens the firmware patch file based on the firmware version read
1724 * from the controller. If it fails to open the matching firmware
1725 * patch file, it tries to open the default firmware patch file.
1726 * If no patch file is found, allow the device to operate without
1729 fw = btusb_setup_intel_get_fw(hdev, &ver);
1734 /* Enable the manufacturer mode of the controller.
1735 * Only while this mode is enabled, the driver can download the
1736 * firmware patch data and configuration parameters.
1738 err = btintel_enter_mfg(hdev);
1740 release_firmware(fw);
1746 /* The firmware data file consists of list of Intel specific HCI
1747 * commands and its expected events. The first byte indicates the
1748 * type of the message, either HCI command or HCI event.
1750 * It reads the command and its expected event from the firmware file,
1751 * and send to the controller. Once __hci_cmd_sync_ev() returns,
1752 * the returned event is compared with the event read from the firmware
1753 * file and it will continue until all the messages are downloaded to
1756 * Once the firmware patching is completed successfully,
1757 * the manufacturer mode is disabled with reset and activating the
1760 * If the firmware patching fails, the manufacturer mode is
1761 * disabled with reset and deactivating the patch.
1763 * If the default patch file is used, no reset is done when disabling
1766 while (fw->size > fw_ptr - fw->data) {
1769 ret = btusb_setup_intel_patching(hdev, fw, &fw_ptr,
1772 goto exit_mfg_deactivate;
1775 release_firmware(fw);
1778 goto exit_mfg_disable;
1780 /* Patching completed successfully and disable the manufacturer mode
1781 * with reset and activate the downloaded firmware patches.
1783 err = btintel_exit_mfg(hdev, true, true);
1787 BT_INFO("%s: Intel Bluetooth firmware patch completed and activated",
1793 /* Disable the manufacturer mode without reset */
1794 err = btintel_exit_mfg(hdev, false, false);
1798 BT_INFO("%s: Intel Bluetooth firmware patch completed", hdev->name);
1802 exit_mfg_deactivate:
1803 release_firmware(fw);
1805 /* Patching failed. Disable the manufacturer mode with reset and
1806 * deactivate the downloaded firmware patches.
1808 err = btintel_exit_mfg(hdev, true, false);
1812 BT_INFO("%s: Intel Bluetooth firmware patch completed and deactivated",
1816 /* Set the event mask for Intel specific vendor events. This enables
1817 * a few extra events that are useful during general operation.
1819 btintel_set_event_mask_mfg(hdev, false);
1821 btintel_check_bdaddr(hdev);
1825 static int inject_cmd_complete(struct hci_dev *hdev, __u16 opcode)
1827 struct sk_buff *skb;
1828 struct hci_event_hdr *hdr;
1829 struct hci_ev_cmd_complete *evt;
1831 skb = bt_skb_alloc(sizeof(*hdr) + sizeof(*evt) + 1, GFP_ATOMIC);
1835 hdr = (struct hci_event_hdr *)skb_put(skb, sizeof(*hdr));
1836 hdr->evt = HCI_EV_CMD_COMPLETE;
1837 hdr->plen = sizeof(*evt) + 1;
1839 evt = (struct hci_ev_cmd_complete *)skb_put(skb, sizeof(*evt));
1841 evt->opcode = cpu_to_le16(opcode);
1843 *skb_put(skb, 1) = 0x00;
1845 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
1847 return hci_recv_frame(hdev, skb);
1850 static int btusb_recv_bulk_intel(struct btusb_data *data, void *buffer,
1853 /* When the device is in bootloader mode, then it can send
1854 * events via the bulk endpoint. These events are treated the
1855 * same way as the ones received from the interrupt endpoint.
1857 if (test_bit(BTUSB_BOOTLOADER, &data->flags))
1858 return btusb_recv_intr(data, buffer, count);
1860 return btusb_recv_bulk(data, buffer, count);
1863 static void btusb_intel_bootup(struct btusb_data *data, const void *ptr,
1866 const struct intel_bootup *evt = ptr;
1868 if (len != sizeof(*evt))
1871 if (test_and_clear_bit(BTUSB_BOOTING, &data->flags)) {
1872 smp_mb__after_atomic();
1873 wake_up_bit(&data->flags, BTUSB_BOOTING);
1877 static void btusb_intel_secure_send_result(struct btusb_data *data,
1878 const void *ptr, unsigned int len)
1880 const struct intel_secure_send_result *evt = ptr;
1882 if (len != sizeof(*evt))
1886 set_bit(BTUSB_FIRMWARE_FAILED, &data->flags);
1888 if (test_and_clear_bit(BTUSB_DOWNLOADING, &data->flags) &&
1889 test_bit(BTUSB_FIRMWARE_LOADED, &data->flags)) {
1890 smp_mb__after_atomic();
1891 wake_up_bit(&data->flags, BTUSB_DOWNLOADING);
1895 static int btusb_recv_event_intel(struct hci_dev *hdev, struct sk_buff *skb)
1897 struct btusb_data *data = hci_get_drvdata(hdev);
1899 if (test_bit(BTUSB_BOOTLOADER, &data->flags)) {
1900 struct hci_event_hdr *hdr = (void *)skb->data;
1902 if (skb->len > HCI_EVENT_HDR_SIZE && hdr->evt == 0xff &&
1904 const void *ptr = skb->data + HCI_EVENT_HDR_SIZE + 1;
1905 unsigned int len = skb->len - HCI_EVENT_HDR_SIZE - 1;
1907 switch (skb->data[2]) {
1909 /* When switching to the operational firmware
1910 * the device sends a vendor specific event
1911 * indicating that the bootup completed.
1913 btusb_intel_bootup(data, ptr, len);
1916 /* When the firmware loading completes the
1917 * device sends out a vendor specific event
1918 * indicating the result of the firmware
1921 btusb_intel_secure_send_result(data, ptr, len);
1927 return hci_recv_frame(hdev, skb);
1930 static int btusb_send_frame_intel(struct hci_dev *hdev, struct sk_buff *skb)
1932 struct btusb_data *data = hci_get_drvdata(hdev);
1935 BT_DBG("%s", hdev->name);
1937 switch (hci_skb_pkt_type(skb)) {
1938 case HCI_COMMAND_PKT:
1939 if (test_bit(BTUSB_BOOTLOADER, &data->flags)) {
1940 struct hci_command_hdr *cmd = (void *)skb->data;
1941 __u16 opcode = le16_to_cpu(cmd->opcode);
1943 /* When in bootloader mode and the command 0xfc09
1944 * is received, it needs to be send down the
1945 * bulk endpoint. So allocate a bulk URB instead.
1947 if (opcode == 0xfc09)
1948 urb = alloc_bulk_urb(hdev, skb);
1950 urb = alloc_ctrl_urb(hdev, skb);
1952 /* When the 0xfc01 command is issued to boot into
1953 * the operational firmware, it will actually not
1954 * send a command complete event. To keep the flow
1955 * control working inject that event here.
1957 if (opcode == 0xfc01)
1958 inject_cmd_complete(hdev, opcode);
1960 urb = alloc_ctrl_urb(hdev, skb);
1963 return PTR_ERR(urb);
1965 hdev->stat.cmd_tx++;
1966 return submit_or_queue_tx_urb(hdev, urb);
1968 case HCI_ACLDATA_PKT:
1969 urb = alloc_bulk_urb(hdev, skb);
1971 return PTR_ERR(urb);
1973 hdev->stat.acl_tx++;
1974 return submit_or_queue_tx_urb(hdev, urb);
1976 case HCI_SCODATA_PKT:
1977 if (hci_conn_num(hdev, SCO_LINK) < 1)
1980 urb = alloc_isoc_urb(hdev, skb);
1982 return PTR_ERR(urb);
1984 hdev->stat.sco_tx++;
1985 return submit_tx_urb(hdev, urb);
1991 static int btusb_setup_intel_new(struct hci_dev *hdev)
1993 static const u8 reset_param[] = { 0x00, 0x01, 0x00, 0x01,
1994 0x00, 0x08, 0x04, 0x00 };
1995 struct btusb_data *data = hci_get_drvdata(hdev);
1996 struct sk_buff *skb;
1997 struct intel_version ver;
1998 struct intel_boot_params *params;
1999 const struct firmware *fw;
2003 ktime_t calltime, delta, rettime;
2004 unsigned long long duration;
2007 BT_DBG("%s", hdev->name);
2009 calltime = ktime_get();
2011 /* Read the Intel version information to determine if the device
2012 * is in bootloader mode or if it already has operational firmware
2015 err = btintel_read_version(hdev, &ver);
2019 /* The hardware platform number has a fixed value of 0x37 and
2020 * for now only accept this single value.
2022 if (ver.hw_platform != 0x37) {
2023 BT_ERR("%s: Unsupported Intel hardware platform (%u)",
2024 hdev->name, ver.hw_platform);
2028 /* Check for supported iBT hardware variants of this firmware
2031 * This check has been put in place to ensure correct forward
2032 * compatibility options when newer hardware variants come along.
2034 switch (ver.hw_variant) {
2035 case 0x0b: /* SfP */
2036 case 0x0c: /* WsP */
2037 case 0x12: /* ThP */
2040 BT_ERR("%s: Unsupported Intel hardware variant (%u)",
2041 hdev->name, ver.hw_variant);
2045 btintel_version_info(hdev, &ver);
2047 /* The firmware variant determines if the device is in bootloader
2048 * mode or is running operational firmware. The value 0x06 identifies
2049 * the bootloader and the value 0x23 identifies the operational
2052 * When the operational firmware is already present, then only
2053 * the check for valid Bluetooth device address is needed. This
2054 * determines if the device will be added as configured or
2055 * unconfigured controller.
2057 * It is not possible to use the Secure Boot Parameters in this
2058 * case since that command is only available in bootloader mode.
2060 if (ver.fw_variant == 0x23) {
2061 clear_bit(BTUSB_BOOTLOADER, &data->flags);
2062 btintel_check_bdaddr(hdev);
2066 /* If the device is not in bootloader mode, then the only possible
2067 * choice is to return an error and abort the device initialization.
2069 if (ver.fw_variant != 0x06) {
2070 BT_ERR("%s: Unsupported Intel firmware variant (%u)",
2071 hdev->name, ver.fw_variant);
2075 /* Read the secure boot parameters to identify the operating
2076 * details of the bootloader.
2078 skb = __hci_cmd_sync(hdev, 0xfc0d, 0, NULL, HCI_INIT_TIMEOUT);
2080 BT_ERR("%s: Reading Intel boot parameters failed (%ld)",
2081 hdev->name, PTR_ERR(skb));
2082 return PTR_ERR(skb);
2085 if (skb->len != sizeof(*params)) {
2086 BT_ERR("%s: Intel boot parameters size mismatch", hdev->name);
2091 params = (struct intel_boot_params *)skb->data;
2093 BT_INFO("%s: Device revision is %u", hdev->name,
2094 le16_to_cpu(params->dev_revid));
2096 BT_INFO("%s: Secure boot is %s", hdev->name,
2097 params->secure_boot ? "enabled" : "disabled");
2099 BT_INFO("%s: OTP lock is %s", hdev->name,
2100 params->otp_lock ? "enabled" : "disabled");
2102 BT_INFO("%s: API lock is %s", hdev->name,
2103 params->api_lock ? "enabled" : "disabled");
2105 BT_INFO("%s: Debug lock is %s", hdev->name,
2106 params->debug_lock ? "enabled" : "disabled");
2108 BT_INFO("%s: Minimum firmware build %u week %u %u", hdev->name,
2109 params->min_fw_build_nn, params->min_fw_build_cw,
2110 2000 + params->min_fw_build_yy);
2112 /* It is required that every single firmware fragment is acknowledged
2113 * with a command complete event. If the boot parameters indicate
2114 * that this bootloader does not send them, then abort the setup.
2116 if (params->limited_cce != 0x00) {
2117 BT_ERR("%s: Unsupported Intel firmware loading method (%u)",
2118 hdev->name, params->limited_cce);
2123 /* If the OTP has no valid Bluetooth device address, then there will
2124 * also be no valid address for the operational firmware.
2126 if (!bacmp(¶ms->otp_bdaddr, BDADDR_ANY)) {
2127 BT_INFO("%s: No device address configured", hdev->name);
2128 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
2131 /* With this Intel bootloader only the hardware variant and device
2132 * revision information are used to select the right firmware.
2134 * The firmware filename is ibt-<hw_variant>-<dev_revid>.sfi.
2136 * Currently the supported hardware variants are:
2137 * 11 (0x0b) for iBT3.0 (LnP/SfP)
2138 * 12 (0x0c) for iBT3.5 (WsP)
2140 snprintf(fwname, sizeof(fwname), "intel/ibt-%u-%u.sfi",
2141 le16_to_cpu(ver.hw_variant),
2142 le16_to_cpu(params->dev_revid));
2144 err = request_firmware(&fw, fwname, &hdev->dev);
2146 BT_ERR("%s: Failed to load Intel firmware file (%d)",
2152 BT_INFO("%s: Found device firmware: %s", hdev->name, fwname);
2154 /* Save the DDC file name for later use to apply once the firmware
2155 * downloading is done.
2157 snprintf(fwname, sizeof(fwname), "intel/ibt-%u-%u.ddc",
2158 le16_to_cpu(ver.hw_variant),
2159 le16_to_cpu(params->dev_revid));
2163 if (fw->size < 644) {
2164 BT_ERR("%s: Invalid size of firmware file (%zu)",
2165 hdev->name, fw->size);
2170 set_bit(BTUSB_DOWNLOADING, &data->flags);
2172 /* Start the firmware download transaction with the Init fragment
2173 * represented by the 128 bytes of CSS header.
2175 err = btintel_secure_send(hdev, 0x00, 128, fw->data);
2177 BT_ERR("%s: Failed to send firmware header (%d)",
2182 /* Send the 256 bytes of public key information from the firmware
2183 * as the PKey fragment.
2185 err = btintel_secure_send(hdev, 0x03, 256, fw->data + 128);
2187 BT_ERR("%s: Failed to send firmware public key (%d)",
2192 /* Send the 256 bytes of signature information from the firmware
2193 * as the Sign fragment.
2195 err = btintel_secure_send(hdev, 0x02, 256, fw->data + 388);
2197 BT_ERR("%s: Failed to send firmware signature (%d)",
2202 fw_ptr = fw->data + 644;
2205 while (fw_ptr - fw->data < fw->size) {
2206 struct hci_command_hdr *cmd = (void *)(fw_ptr + frag_len);
2208 frag_len += sizeof(*cmd) + cmd->plen;
2210 /* The parameter length of the secure send command requires
2211 * a 4 byte alignment. It happens so that the firmware file
2212 * contains proper Intel_NOP commands to align the fragments
2215 * Send set of commands with 4 byte alignment from the
2216 * firmware data buffer as a single Data fragement.
2218 if (!(frag_len % 4)) {
2219 err = btintel_secure_send(hdev, 0x01, frag_len, fw_ptr);
2221 BT_ERR("%s: Failed to send firmware data (%d)",
2231 set_bit(BTUSB_FIRMWARE_LOADED, &data->flags);
2233 BT_INFO("%s: Waiting for firmware download to complete", hdev->name);
2235 /* Before switching the device into operational mode and with that
2236 * booting the loaded firmware, wait for the bootloader notification
2237 * that all fragments have been successfully received.
2239 * When the event processing receives the notification, then the
2240 * BTUSB_DOWNLOADING flag will be cleared.
2242 * The firmware loading should not take longer than 5 seconds
2243 * and thus just timeout if that happens and fail the setup
2246 err = wait_on_bit_timeout(&data->flags, BTUSB_DOWNLOADING,
2248 msecs_to_jiffies(5000));
2249 if (err == -EINTR) {
2250 BT_ERR("%s: Firmware loading interrupted", hdev->name);
2255 BT_ERR("%s: Firmware loading timeout", hdev->name);
2260 if (test_bit(BTUSB_FIRMWARE_FAILED, &data->flags)) {
2261 BT_ERR("%s: Firmware loading failed", hdev->name);
2266 rettime = ktime_get();
2267 delta = ktime_sub(rettime, calltime);
2268 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
2270 BT_INFO("%s: Firmware loaded in %llu usecs", hdev->name, duration);
2273 release_firmware(fw);
2278 calltime = ktime_get();
2280 set_bit(BTUSB_BOOTING, &data->flags);
2282 skb = __hci_cmd_sync(hdev, 0xfc01, sizeof(reset_param), reset_param,
2285 return PTR_ERR(skb);
2289 /* The bootloader will not indicate when the device is ready. This
2290 * is done by the operational firmware sending bootup notification.
2292 * Booting into operational firmware should not take longer than
2293 * 1 second. However if that happens, then just fail the setup
2294 * since something went wrong.
2296 BT_INFO("%s: Waiting for device to boot", hdev->name);
2298 err = wait_on_bit_timeout(&data->flags, BTUSB_BOOTING,
2300 msecs_to_jiffies(1000));
2302 if (err == -EINTR) {
2303 BT_ERR("%s: Device boot interrupted", hdev->name);
2308 BT_ERR("%s: Device boot timeout", hdev->name);
2312 rettime = ktime_get();
2313 delta = ktime_sub(rettime, calltime);
2314 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
2316 BT_INFO("%s: Device booted in %llu usecs", hdev->name, duration);
2318 clear_bit(BTUSB_BOOTLOADER, &data->flags);
2320 /* Once the device is running in operational mode, it needs to apply
2321 * the device configuration (DDC) parameters.
2323 * The device can work without DDC parameters, so even if it fails
2324 * to load the file, no need to fail the setup.
2326 btintel_load_ddc_config(hdev, fwname);
2328 /* Set the event mask for Intel specific vendor events. This enables
2329 * a few extra events that are useful during general operation. It
2330 * does not enable any debugging related events.
2332 * The device will function correctly without these events enabled
2333 * and thus no need to fail the setup.
2335 btintel_set_event_mask(hdev, false);
2340 static int btusb_shutdown_intel(struct hci_dev *hdev)
2342 struct sk_buff *skb;
2345 /* Some platforms have an issue with BT LED when the interface is
2346 * down or BT radio is turned off, which takes 5 seconds to BT LED
2347 * goes off. This command turns off the BT LED immediately.
2349 skb = __hci_cmd_sync(hdev, 0xfc3f, 0, NULL, HCI_INIT_TIMEOUT);
2352 BT_ERR("%s: turning off Intel device LED failed (%ld)",
2362 /* Configure an out-of-band gpio as wake-up pin, if specified in device tree */
2363 static int marvell_config_oob_wake(struct hci_dev *hdev)
2365 struct sk_buff *skb;
2366 struct btusb_data *data = hci_get_drvdata(hdev);
2367 struct device *dev = &data->udev->dev;
2368 u16 pin, gap, opcode;
2372 /* Move on if no wakeup pin specified */
2373 if (of_property_read_u16(dev->of_node, "marvell,wakeup-pin", &pin) ||
2374 of_property_read_u16(dev->of_node, "marvell,wakeup-gap-ms", &gap))
2377 /* Vendor specific command to configure a GPIO as wake-up pin */
2378 opcode = hci_opcode_pack(0x3F, 0x59);
2379 cmd[0] = opcode & 0xFF;
2380 cmd[1] = opcode >> 8;
2381 cmd[2] = 2; /* length of parameters that follow */
2383 cmd[4] = gap; /* time in ms, for which wakeup pin should be asserted */
2385 skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL);
2387 bt_dev_err(hdev, "%s: No memory\n", __func__);
2391 memcpy(skb_put(skb, sizeof(cmd)), cmd, sizeof(cmd));
2392 hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
2394 ret = btusb_send_frame(hdev, skb);
2396 bt_dev_err(hdev, "%s: configuration failed\n", __func__);
2405 static int btusb_set_bdaddr_marvell(struct hci_dev *hdev,
2406 const bdaddr_t *bdaddr)
2408 struct sk_buff *skb;
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);
2419 BT_ERR("%s: changing Marvell device address failed (%ld)",
2428 static int btusb_set_bdaddr_ath3012(struct hci_dev *hdev,
2429 const bdaddr_t *bdaddr)
2431 struct sk_buff *skb;
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);
2444 BT_ERR("%s: Change address command failed (%ld)",
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 {
2465 __le32 patch_version;
2471 struct qca_rampatch_version {
2473 __le16 patch_version;
2476 struct qca_device_info {
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;
2500 buf = kmalloc(size, GFP_KERNEL);
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);
2511 BT_ERR("%s: Failed to access otp area (%d)", hdev->name, err);
2515 memcpy(data, buf, size);
2523 static int btusb_setup_qca_download_fw(struct hci_dev *hdev,
2524 const struct firmware *firmware,
2527 struct btusb_data *btdata = hci_get_drvdata(hdev);
2528 struct usb_device *udev = btdata->udev;
2529 size_t count, size, sent = 0;
2533 buf = kmalloc(QCA_DFU_PACKET_LEN, GFP_KERNEL);
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
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);
2551 BT_ERR("%s: Failed to send headers (%d)", hdev->name, err);
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,
2567 BT_ERR("%s: Failed to send body at %zd of %zd (%d)",
2568 hdev->name, sent, firmware->size, err);
2573 BT_ERR("%s: Failed to get bulk buffer", hdev->name);
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;
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);
2605 BT_ERR("%s: failed to request rampatch file: %s (%d)",
2606 hdev->name, fwname, 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,
2620 if (rver_rom != ver_rom || rver_patch <= ver_patch) {
2621 BT_ERR("%s: rampatch file version did not match with firmware",
2627 err = btusb_setup_qca_download_fw(hdev, fw, info->rampatch_hdr);
2630 release_firmware(fw);
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;
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);
2648 BT_ERR("%s: failed to request NVM file: %s (%d)",
2649 hdev->name, fwname, 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);
2662 static int btusb_setup_qca(struct hci_dev *hdev)
2664 const struct qca_device_info *info = NULL;
2665 struct qca_version ver;
2670 err = btusb_qca_send_vendor_req(hdev, QCA_GET_TARGET_VERSION, &ver,
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];
2681 BT_ERR("%s: don't support firmware rome 0x%x", hdev->name,
2686 err = btusb_qca_send_vendor_req(hdev, QCA_CHECK_STATUS, &status,
2691 if (!(status & QCA_PATCH_UPDATED)) {
2692 err = btusb_setup_qca_load_rampatch(hdev, &ver, info);
2697 if (!(status & QCA_SYSCFG_UPDATED)) {
2698 err = btusb_setup_qca_load_nvm(hdev, &ver, info);
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;
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;
2729 if (!data->diag_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
2730 data->diag_rx_ep = ep_desc;
2735 if (!data->diag_tx_ep || !data->diag_rx_ep) {
2736 BT_ERR("%s invalid diagnostic descriptors", hdev->name);
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;
2750 if (!data->diag_tx_ep)
2751 return ERR_PTR(-ENODEV);
2753 urb = usb_alloc_urb(0, GFP_KERNEL);
2755 return ERR_PTR(-ENOMEM);
2757 skb = bt_skb_alloc(2, GFP_KERNEL);
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;
2776 static int btusb_bcm_set_diag(struct hci_dev *hdev, bool enable)
2778 struct btusb_data *data = hci_get_drvdata(hdev);
2784 if (!test_bit(HCI_RUNNING, &hdev->flags))
2787 urb = alloc_diag_urb(hdev, enable);
2789 return PTR_ERR(urb);
2791 return submit_or_queue_tx_urb(hdev, urb);
2796 static irqreturn_t btusb_oob_wake_handler(int irq, void *priv)
2798 struct btusb_data *data = priv;
2800 pm_wakeup_event(&data->udev->dev, 0);
2802 /* Disable only if not already disabled (keep it balanced) */
2803 if (test_and_clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags)) {
2804 disable_irq_nosync(irq);
2805 disable_irq_wake(irq);
2810 static const struct of_device_id btusb_match_table[] = {
2811 { .compatible = "usb1286,204e" },
2814 MODULE_DEVICE_TABLE(of, btusb_match_table);
2816 /* Use an oob wakeup pin? */
2817 static int btusb_config_oob_wake(struct hci_dev *hdev)
2819 struct btusb_data *data = hci_get_drvdata(hdev);
2820 struct device *dev = &data->udev->dev;
2823 clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags);
2825 if (!of_match_device(btusb_match_table, dev))
2828 /* Move on if no IRQ specified */
2829 irq = of_irq_get_byname(dev->of_node, "wakeup");
2831 bt_dev_dbg(hdev, "%s: no OOB Wakeup IRQ in DT", __func__);
2835 ret = devm_request_irq(&hdev->dev, irq, btusb_oob_wake_handler,
2836 0, "OOB Wake-on-BT", data);
2838 bt_dev_err(hdev, "%s: IRQ request failed", __func__);
2842 ret = device_init_wakeup(dev, true);
2844 bt_dev_err(hdev, "%s: failed to init_wakeup", __func__);
2848 data->oob_wake_irq = irq;
2850 bt_dev_info(hdev, "OOB Wake-on-BT configured at IRQ %u", irq);
2855 static int btusb_probe(struct usb_interface *intf,
2856 const struct usb_device_id *id)
2858 struct usb_endpoint_descriptor *ep_desc;
2859 struct btusb_data *data;
2860 struct hci_dev *hdev;
2861 unsigned ifnum_base;
2864 BT_DBG("intf %p id %p", intf, id);
2866 /* interface numbers are hardcoded in the spec */
2867 if (intf->cur_altsetting->desc.bInterfaceNumber != 0) {
2868 if (!(id->driver_info & BTUSB_IFNUM_2))
2870 if (intf->cur_altsetting->desc.bInterfaceNumber != 2)
2874 ifnum_base = intf->cur_altsetting->desc.bInterfaceNumber;
2876 if (!id->driver_info) {
2877 const struct usb_device_id *match;
2879 match = usb_match_id(intf, blacklist_table);
2884 if (id->driver_info == BTUSB_IGNORE)
2887 if (id->driver_info & BTUSB_ATH3012) {
2888 struct usb_device *udev = interface_to_usbdev(intf);
2890 /* Old firmware would otherwise let ath3k driver load
2891 * patch and sysconfig files */
2892 if (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x0001)
2896 data = devm_kzalloc(&intf->dev, sizeof(*data), GFP_KERNEL);
2900 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
2901 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
2903 if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
2904 data->intr_ep = ep_desc;
2908 if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
2909 data->bulk_tx_ep = ep_desc;
2913 if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
2914 data->bulk_rx_ep = ep_desc;
2919 if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep)
2922 if (id->driver_info & BTUSB_AMP) {
2923 data->cmdreq_type = USB_TYPE_CLASS | 0x01;
2924 data->cmdreq = 0x2b;
2926 data->cmdreq_type = USB_TYPE_CLASS;
2927 data->cmdreq = 0x00;
2930 data->udev = interface_to_usbdev(intf);
2933 INIT_WORK(&data->work, btusb_work);
2934 INIT_WORK(&data->waker, btusb_waker);
2935 init_usb_anchor(&data->deferred);
2936 init_usb_anchor(&data->tx_anchor);
2937 spin_lock_init(&data->txlock);
2939 init_usb_anchor(&data->intr_anchor);
2940 init_usb_anchor(&data->bulk_anchor);
2941 init_usb_anchor(&data->isoc_anchor);
2942 init_usb_anchor(&data->diag_anchor);
2943 spin_lock_init(&data->rxlock);
2945 if (id->driver_info & BTUSB_INTEL_NEW) {
2946 data->recv_event = btusb_recv_event_intel;
2947 data->recv_bulk = btusb_recv_bulk_intel;
2948 set_bit(BTUSB_BOOTLOADER, &data->flags);
2950 data->recv_event = hci_recv_frame;
2951 data->recv_bulk = btusb_recv_bulk;
2954 hdev = hci_alloc_dev();
2958 hdev->bus = HCI_USB;
2959 hci_set_drvdata(hdev, data);
2961 if (id->driver_info & BTUSB_AMP)
2962 hdev->dev_type = HCI_AMP;
2964 hdev->dev_type = HCI_PRIMARY;
2968 SET_HCIDEV_DEV(hdev, &intf->dev);
2970 hdev->open = btusb_open;
2971 hdev->close = btusb_close;
2972 hdev->flush = btusb_flush;
2973 hdev->send = btusb_send_frame;
2974 hdev->notify = btusb_notify;
2977 err = btusb_config_oob_wake(hdev);
2981 /* Marvell devices may need a specific chip configuration */
2982 if (id->driver_info & BTUSB_MARVELL && data->oob_wake_irq) {
2983 err = marvell_config_oob_wake(hdev);
2988 if (id->driver_info & BTUSB_CW6622)
2989 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
2991 if (id->driver_info & BTUSB_BCM2045)
2992 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
2994 if (id->driver_info & BTUSB_BCM92035)
2995 hdev->setup = btusb_setup_bcm92035;
2997 #ifdef CONFIG_BT_HCIBTUSB_BCM
2998 if (id->driver_info & BTUSB_BCM_PATCHRAM) {
2999 hdev->manufacturer = 15;
3000 hdev->setup = btbcm_setup_patchram;
3001 hdev->set_diag = btusb_bcm_set_diag;
3002 hdev->set_bdaddr = btbcm_set_bdaddr;
3004 /* Broadcom LM_DIAG Interface numbers are hardcoded */
3005 data->diag = usb_ifnum_to_if(data->udev, ifnum_base + 2);
3008 if (id->driver_info & BTUSB_BCM_APPLE) {
3009 hdev->manufacturer = 15;
3010 hdev->setup = btbcm_setup_apple;
3011 hdev->set_diag = btusb_bcm_set_diag;
3013 /* Broadcom LM_DIAG Interface numbers are hardcoded */
3014 data->diag = usb_ifnum_to_if(data->udev, ifnum_base + 2);
3018 if (id->driver_info & BTUSB_INTEL) {
3019 hdev->manufacturer = 2;
3020 hdev->setup = btusb_setup_intel;
3021 hdev->shutdown = btusb_shutdown_intel;
3022 hdev->set_diag = btintel_set_diag_mfg;
3023 hdev->set_bdaddr = btintel_set_bdaddr;
3024 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
3025 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
3026 set_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks);
3029 if (id->driver_info & BTUSB_INTEL_NEW) {
3030 hdev->manufacturer = 2;
3031 hdev->send = btusb_send_frame_intel;
3032 hdev->setup = btusb_setup_intel_new;
3033 hdev->hw_error = btintel_hw_error;
3034 hdev->set_diag = btintel_set_diag;
3035 hdev->set_bdaddr = btintel_set_bdaddr;
3036 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
3037 set_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks);
3040 if (id->driver_info & BTUSB_MARVELL)
3041 hdev->set_bdaddr = btusb_set_bdaddr_marvell;
3043 if (id->driver_info & BTUSB_SWAVE) {
3044 set_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks);
3045 set_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS, &hdev->quirks);
3048 if (id->driver_info & BTUSB_INTEL_BOOT) {
3049 hdev->manufacturer = 2;
3050 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
3053 if (id->driver_info & BTUSB_ATH3012) {
3054 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
3055 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
3056 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
3059 if (id->driver_info & BTUSB_QCA_ROME) {
3060 data->setup_on_usb = btusb_setup_qca;
3061 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
3064 #ifdef CONFIG_BT_HCIBTUSB_RTL
3065 if (id->driver_info & BTUSB_REALTEK) {
3066 hdev->setup = btrtl_setup_realtek;
3068 /* Realtek devices lose their updated firmware over suspend,
3069 * but the USB hub doesn't notice any status change.
3070 * Explicitly request a device reset on resume.
3072 set_bit(BTUSB_RESET_RESUME, &data->flags);
3076 if (id->driver_info & BTUSB_AMP) {
3077 /* AMP controllers do not support SCO packets */
3080 /* Interface orders are hardcoded in the specification */
3081 data->isoc = usb_ifnum_to_if(data->udev, ifnum_base + 1);
3085 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
3087 if (force_scofix || id->driver_info & BTUSB_WRONG_SCO_MTU) {
3088 if (!disable_scofix)
3089 set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
3092 if (id->driver_info & BTUSB_BROKEN_ISOC)
3095 if (id->driver_info & BTUSB_DIGIANSWER) {
3096 data->cmdreq_type = USB_TYPE_VENDOR;
3097 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
3100 if (id->driver_info & BTUSB_CSR) {
3101 struct usb_device *udev = data->udev;
3102 u16 bcdDevice = le16_to_cpu(udev->descriptor.bcdDevice);
3104 /* Old firmware would otherwise execute USB reset */
3105 if (bcdDevice < 0x117)
3106 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
3108 /* Fake CSR devices with broken commands */
3109 if (bcdDevice <= 0x100 || bcdDevice == 0x134)
3110 hdev->setup = btusb_setup_csr;
3112 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
3115 if (id->driver_info & BTUSB_SNIFFER) {
3116 struct usb_device *udev = data->udev;
3118 /* New sniffer firmware has crippled HCI interface */
3119 if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
3120 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
3123 if (id->driver_info & BTUSB_INTEL_BOOT) {
3124 /* A bug in the bootloader causes that interrupt interface is
3125 * only enabled after receiving SetInterface(0, AltSetting=0).
3127 err = usb_set_interface(data->udev, 0, 0);
3129 BT_ERR("failed to set interface 0, alt 0 %d", err);
3135 err = usb_driver_claim_interface(&btusb_driver,
3141 #ifdef CONFIG_BT_HCIBTUSB_BCM
3143 if (!usb_driver_claim_interface(&btusb_driver,
3145 __set_diag_interface(hdev);
3151 err = hci_register_dev(hdev);
3155 usb_set_intfdata(intf, data);
3164 static void btusb_disconnect(struct usb_interface *intf)
3166 struct btusb_data *data = usb_get_intfdata(intf);
3167 struct hci_dev *hdev;
3169 BT_DBG("intf %p", intf);
3175 usb_set_intfdata(data->intf, NULL);
3178 usb_set_intfdata(data->isoc, NULL);
3181 usb_set_intfdata(data->diag, NULL);
3183 hci_unregister_dev(hdev);
3185 if (intf == data->intf) {
3187 usb_driver_release_interface(&btusb_driver, data->isoc);
3189 usb_driver_release_interface(&btusb_driver, data->diag);
3190 } else if (intf == data->isoc) {
3192 usb_driver_release_interface(&btusb_driver, data->diag);
3193 usb_driver_release_interface(&btusb_driver, data->intf);
3194 } else if (intf == data->diag) {
3195 usb_driver_release_interface(&btusb_driver, data->intf);
3197 usb_driver_release_interface(&btusb_driver, data->isoc);
3200 if (data->oob_wake_irq)
3201 device_init_wakeup(&data->udev->dev, false);
3207 static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
3209 struct btusb_data *data = usb_get_intfdata(intf);
3211 BT_DBG("intf %p", intf);
3213 if (data->suspend_count++)
3216 spin_lock_irq(&data->txlock);
3217 if (!(PMSG_IS_AUTO(message) && data->tx_in_flight)) {
3218 set_bit(BTUSB_SUSPENDING, &data->flags);
3219 spin_unlock_irq(&data->txlock);
3221 spin_unlock_irq(&data->txlock);
3222 data->suspend_count--;
3226 cancel_work_sync(&data->work);
3228 btusb_stop_traffic(data);
3229 usb_kill_anchored_urbs(&data->tx_anchor);
3231 if (data->oob_wake_irq && device_may_wakeup(&data->udev->dev)) {
3232 set_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags);
3233 enable_irq_wake(data->oob_wake_irq);
3234 enable_irq(data->oob_wake_irq);
3237 /* Optionally request a device reset on resume, but only when
3238 * wakeups are disabled. If wakeups are enabled we assume the
3239 * device will stay powered up throughout suspend.
3241 if (test_bit(BTUSB_RESET_RESUME, &data->flags) &&
3242 !device_may_wakeup(&data->udev->dev))
3243 data->udev->reset_resume = 1;
3248 static void play_deferred(struct btusb_data *data)
3253 while ((urb = usb_get_from_anchor(&data->deferred))) {
3254 err = usb_submit_urb(urb, GFP_ATOMIC);
3258 data->tx_in_flight++;
3260 usb_scuttle_anchored_urbs(&data->deferred);
3263 static int btusb_resume(struct usb_interface *intf)
3265 struct btusb_data *data = usb_get_intfdata(intf);
3266 struct hci_dev *hdev = data->hdev;
3269 BT_DBG("intf %p", intf);
3271 if (--data->suspend_count)
3274 /* Disable only if not already disabled (keep it balanced) */
3275 if (test_and_clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags)) {
3276 disable_irq(data->oob_wake_irq);
3277 disable_irq_wake(data->oob_wake_irq);
3280 if (!test_bit(HCI_RUNNING, &hdev->flags))
3283 if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
3284 err = btusb_submit_intr_urb(hdev, GFP_NOIO);
3286 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
3291 if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) {
3292 err = btusb_submit_bulk_urb(hdev, GFP_NOIO);
3294 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
3298 btusb_submit_bulk_urb(hdev, GFP_NOIO);
3301 if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
3302 if (btusb_submit_isoc_urb(hdev, GFP_NOIO) < 0)
3303 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
3305 btusb_submit_isoc_urb(hdev, GFP_NOIO);
3308 spin_lock_irq(&data->txlock);
3309 play_deferred(data);
3310 clear_bit(BTUSB_SUSPENDING, &data->flags);
3311 spin_unlock_irq(&data->txlock);
3312 schedule_work(&data->work);
3317 usb_scuttle_anchored_urbs(&data->deferred);
3319 spin_lock_irq(&data->txlock);
3320 clear_bit(BTUSB_SUSPENDING, &data->flags);
3321 spin_unlock_irq(&data->txlock);
3327 static struct usb_driver btusb_driver = {
3329 .probe = btusb_probe,
3330 .disconnect = btusb_disconnect,
3332 .suspend = btusb_suspend,
3333 .resume = btusb_resume,
3335 .id_table = btusb_table,
3336 .supports_autosuspend = 1,
3337 .disable_hub_initiated_lpm = 1,
3340 module_usb_driver(btusb_driver);
3342 module_param(disable_scofix, bool, 0644);
3343 MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
3345 module_param(force_scofix, bool, 0644);
3346 MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
3348 module_param(reset, bool, 0644);
3349 MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
3351 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
3352 MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
3353 MODULE_VERSION(VERSION);
3354 MODULE_LICENSE("GPL");