]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - drivers/bluetooth/btusb.c
net: vrf: Reset rt6i_idev in local dst after put
[mirror_ubuntu-zesty-kernel.git] / drivers / bluetooth / btusb.c
1 /*
2 *
3 * Generic Bluetooth USB driver
4 *
5 * Copyright (C) 2005-2008 Marcel Holtmann <marcel@holtmann.org>
6 *
7 *
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.
12 *
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.
17 *
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
21 *
22 */
23
24 #include <linux/module.h>
25 #include <linux/usb.h>
26 #include <linux/firmware.h>
27 #include <asm/unaligned.h>
28
29 #include <net/bluetooth/bluetooth.h>
30 #include <net/bluetooth/hci_core.h>
31
32 #include "btintel.h"
33 #include "btbcm.h"
34 #include "btrtl.h"
35
36 #define VERSION "0.8"
37
38 static bool disable_scofix;
39 static bool force_scofix;
40
41 static bool reset = true;
42
43 static struct usb_driver btusb_driver;
44
45 #define BTUSB_IGNORE 0x01
46 #define BTUSB_DIGIANSWER 0x02
47 #define BTUSB_CSR 0x04
48 #define BTUSB_SNIFFER 0x08
49 #define BTUSB_BCM92035 0x10
50 #define BTUSB_BROKEN_ISOC 0x20
51 #define BTUSB_WRONG_SCO_MTU 0x40
52 #define BTUSB_ATH3012 0x80
53 #define BTUSB_INTEL 0x100
54 #define BTUSB_INTEL_BOOT 0x200
55 #define BTUSB_BCM_PATCHRAM 0x400
56 #define BTUSB_MARVELL 0x800
57 #define BTUSB_SWAVE 0x1000
58 #define BTUSB_INTEL_NEW 0x2000
59 #define BTUSB_AMP 0x4000
60 #define BTUSB_QCA_ROME 0x8000
61 #define BTUSB_BCM_APPLE 0x10000
62 #define BTUSB_REALTEK 0x20000
63 #define BTUSB_BCM2045 0x40000
64 #define BTUSB_IFNUM_2 0x80000
65 #define BTUSB_CW6622 0x100000
66
67 static const struct usb_device_id btusb_table[] = {
68 /* Generic Bluetooth USB device */
69 { USB_DEVICE_INFO(0xe0, 0x01, 0x01) },
70
71 /* Generic Bluetooth AMP device */
72 { USB_DEVICE_INFO(0xe0, 0x01, 0x04), .driver_info = BTUSB_AMP },
73
74 /* Generic Bluetooth USB interface */
75 { USB_INTERFACE_INFO(0xe0, 0x01, 0x01) },
76
77 /* Apple-specific (Broadcom) devices */
78 { USB_VENDOR_AND_INTERFACE_INFO(0x05ac, 0xff, 0x01, 0x01),
79 .driver_info = BTUSB_BCM_APPLE | BTUSB_IFNUM_2 },
80
81 /* MediaTek MT76x0E */
82 { USB_DEVICE(0x0e8d, 0x763f) },
83
84 /* Broadcom SoftSailing reporting vendor specific */
85 { USB_DEVICE(0x0a5c, 0x21e1) },
86
87 /* Apple MacBookPro 7,1 */
88 { USB_DEVICE(0x05ac, 0x8213) },
89
90 /* Apple iMac11,1 */
91 { USB_DEVICE(0x05ac, 0x8215) },
92
93 /* Apple MacBookPro6,2 */
94 { USB_DEVICE(0x05ac, 0x8218) },
95
96 /* Apple MacBookAir3,1, MacBookAir3,2 */
97 { USB_DEVICE(0x05ac, 0x821b) },
98
99 /* Apple MacBookAir4,1 */
100 { USB_DEVICE(0x05ac, 0x821f) },
101
102 /* Apple MacBookPro8,2 */
103 { USB_DEVICE(0x05ac, 0x821a) },
104
105 /* Apple MacMini5,1 */
106 { USB_DEVICE(0x05ac, 0x8281) },
107
108 /* AVM BlueFRITZ! USB v2.0 */
109 { USB_DEVICE(0x057c, 0x3800), .driver_info = BTUSB_SWAVE },
110
111 /* Bluetooth Ultraport Module from IBM */
112 { USB_DEVICE(0x04bf, 0x030a) },
113
114 /* ALPS Modules with non-standard id */
115 { USB_DEVICE(0x044e, 0x3001) },
116 { USB_DEVICE(0x044e, 0x3002) },
117
118 /* Ericsson with non-standard id */
119 { USB_DEVICE(0x0bdb, 0x1002) },
120
121 /* Canyon CN-BTU1 with HID interfaces */
122 { USB_DEVICE(0x0c10, 0x0000) },
123
124 /* Broadcom BCM20702A0 */
125 { USB_DEVICE(0x413c, 0x8197) },
126
127 /* Broadcom BCM20702B0 (Dynex/Insignia) */
128 { USB_DEVICE(0x19ff, 0x0239), .driver_info = BTUSB_BCM_PATCHRAM },
129
130 /* Broadcom BCM43142A0 (Foxconn/Lenovo) */
131 { USB_DEVICE(0x105b, 0xe065), .driver_info = BTUSB_BCM_PATCHRAM },
132
133 /* Foxconn - Hon Hai */
134 { USB_VENDOR_AND_INTERFACE_INFO(0x0489, 0xff, 0x01, 0x01),
135 .driver_info = BTUSB_BCM_PATCHRAM },
136
137 /* Lite-On Technology - Broadcom based */
138 { USB_VENDOR_AND_INTERFACE_INFO(0x04ca, 0xff, 0x01, 0x01),
139 .driver_info = BTUSB_BCM_PATCHRAM },
140
141 /* Broadcom devices with vendor specific id */
142 { USB_VENDOR_AND_INTERFACE_INFO(0x0a5c, 0xff, 0x01, 0x01),
143 .driver_info = BTUSB_BCM_PATCHRAM },
144
145 /* ASUSTek Computer - Broadcom based */
146 { USB_VENDOR_AND_INTERFACE_INFO(0x0b05, 0xff, 0x01, 0x01),
147 .driver_info = BTUSB_BCM_PATCHRAM },
148
149 /* Belkin F8065bf - Broadcom based */
150 { USB_VENDOR_AND_INTERFACE_INFO(0x050d, 0xff, 0x01, 0x01),
151 .driver_info = BTUSB_BCM_PATCHRAM },
152
153 /* IMC Networks - Broadcom based */
154 { USB_VENDOR_AND_INTERFACE_INFO(0x13d3, 0xff, 0x01, 0x01),
155 .driver_info = BTUSB_BCM_PATCHRAM },
156
157 /* Dell Computer - Broadcom based */
158 { USB_VENDOR_AND_INTERFACE_INFO(0x413c, 0xff, 0x01, 0x01),
159 .driver_info = BTUSB_BCM_PATCHRAM },
160
161 /* Toshiba Corp - Broadcom based */
162 { USB_VENDOR_AND_INTERFACE_INFO(0x0930, 0xff, 0x01, 0x01),
163 .driver_info = BTUSB_BCM_PATCHRAM },
164
165 /* Intel Bluetooth USB Bootloader (RAM module) */
166 { USB_DEVICE(0x8087, 0x0a5a),
167 .driver_info = BTUSB_INTEL_BOOT | BTUSB_BROKEN_ISOC },
168
169 { } /* Terminating entry */
170 };
171
172 MODULE_DEVICE_TABLE(usb, btusb_table);
173
174 static const struct usb_device_id blacklist_table[] = {
175 /* CSR BlueCore devices */
176 { USB_DEVICE(0x0a12, 0x0001), .driver_info = BTUSB_CSR },
177
178 /* Broadcom BCM2033 without firmware */
179 { USB_DEVICE(0x0a5c, 0x2033), .driver_info = BTUSB_IGNORE },
180
181 /* Broadcom BCM2045 devices */
182 { USB_DEVICE(0x0a5c, 0x2045), .driver_info = BTUSB_BCM2045 },
183
184 /* Atheros 3011 with sflash firmware */
185 { USB_DEVICE(0x0489, 0xe027), .driver_info = BTUSB_IGNORE },
186 { USB_DEVICE(0x0489, 0xe03d), .driver_info = BTUSB_IGNORE },
187 { USB_DEVICE(0x04f2, 0xaff1), .driver_info = BTUSB_IGNORE },
188 { USB_DEVICE(0x0930, 0x0215), .driver_info = BTUSB_IGNORE },
189 { USB_DEVICE(0x0cf3, 0x3002), .driver_info = BTUSB_IGNORE },
190 { USB_DEVICE(0x0cf3, 0xe019), .driver_info = BTUSB_IGNORE },
191 { USB_DEVICE(0x13d3, 0x3304), .driver_info = BTUSB_IGNORE },
192
193 /* Atheros AR9285 Malbec with sflash firmware */
194 { USB_DEVICE(0x03f0, 0x311d), .driver_info = BTUSB_IGNORE },
195
196 /* Atheros 3012 with sflash firmware */
197 { USB_DEVICE(0x0489, 0xe04d), .driver_info = BTUSB_ATH3012 },
198 { USB_DEVICE(0x0489, 0xe04e), .driver_info = BTUSB_ATH3012 },
199 { USB_DEVICE(0x0489, 0xe056), .driver_info = BTUSB_ATH3012 },
200 { USB_DEVICE(0x0489, 0xe057), .driver_info = BTUSB_ATH3012 },
201 { USB_DEVICE(0x0489, 0xe05f), .driver_info = BTUSB_ATH3012 },
202 { USB_DEVICE(0x0489, 0xe076), .driver_info = BTUSB_ATH3012 },
203 { USB_DEVICE(0x0489, 0xe078), .driver_info = BTUSB_ATH3012 },
204 { USB_DEVICE(0x0489, 0xe095), .driver_info = BTUSB_ATH3012 },
205 { USB_DEVICE(0x04c5, 0x1330), .driver_info = BTUSB_ATH3012 },
206 { USB_DEVICE(0x04ca, 0x3004), .driver_info = BTUSB_ATH3012 },
207 { USB_DEVICE(0x04ca, 0x3005), .driver_info = BTUSB_ATH3012 },
208 { USB_DEVICE(0x04ca, 0x3006), .driver_info = BTUSB_ATH3012 },
209 { USB_DEVICE(0x04ca, 0x3007), .driver_info = BTUSB_ATH3012 },
210 { USB_DEVICE(0x04ca, 0x3008), .driver_info = BTUSB_ATH3012 },
211 { USB_DEVICE(0x04ca, 0x300b), .driver_info = BTUSB_ATH3012 },
212 { USB_DEVICE(0x04ca, 0x300d), .driver_info = BTUSB_ATH3012 },
213 { USB_DEVICE(0x04ca, 0x300f), .driver_info = BTUSB_ATH3012 },
214 { USB_DEVICE(0x04ca, 0x3010), .driver_info = BTUSB_ATH3012 },
215 { USB_DEVICE(0x04ca, 0x3014), .driver_info = BTUSB_ATH3012 },
216 { USB_DEVICE(0x04ca, 0x3018), .driver_info = BTUSB_ATH3012 },
217 { USB_DEVICE(0x0930, 0x0219), .driver_info = BTUSB_ATH3012 },
218 { USB_DEVICE(0x0930, 0x021c), .driver_info = BTUSB_ATH3012 },
219 { USB_DEVICE(0x0930, 0x0220), .driver_info = BTUSB_ATH3012 },
220 { USB_DEVICE(0x0930, 0x0227), .driver_info = BTUSB_ATH3012 },
221 { USB_DEVICE(0x0b05, 0x17d0), .driver_info = BTUSB_ATH3012 },
222 { USB_DEVICE(0x0cf3, 0x0036), .driver_info = BTUSB_ATH3012 },
223 { USB_DEVICE(0x0cf3, 0x3004), .driver_info = BTUSB_ATH3012 },
224 { USB_DEVICE(0x0cf3, 0x3008), .driver_info = BTUSB_ATH3012 },
225 { USB_DEVICE(0x0cf3, 0x311d), .driver_info = BTUSB_ATH3012 },
226 { USB_DEVICE(0x0cf3, 0x311e), .driver_info = BTUSB_ATH3012 },
227 { USB_DEVICE(0x0cf3, 0x311f), .driver_info = BTUSB_ATH3012 },
228 { USB_DEVICE(0x0cf3, 0x3121), .driver_info = BTUSB_ATH3012 },
229 { USB_DEVICE(0x0cf3, 0x817a), .driver_info = BTUSB_ATH3012 },
230 { USB_DEVICE(0x0cf3, 0x817b), .driver_info = BTUSB_ATH3012 },
231 { USB_DEVICE(0x0cf3, 0xe003), .driver_info = BTUSB_ATH3012 },
232 { USB_DEVICE(0x0cf3, 0xe004), .driver_info = BTUSB_ATH3012 },
233 { USB_DEVICE(0x0cf3, 0xe005), .driver_info = BTUSB_ATH3012 },
234 { USB_DEVICE(0x0cf3, 0xe006), .driver_info = BTUSB_ATH3012 },
235 { USB_DEVICE(0x13d3, 0x3362), .driver_info = BTUSB_ATH3012 },
236 { USB_DEVICE(0x13d3, 0x3375), .driver_info = BTUSB_ATH3012 },
237 { USB_DEVICE(0x13d3, 0x3393), .driver_info = BTUSB_ATH3012 },
238 { USB_DEVICE(0x13d3, 0x3395), .driver_info = BTUSB_ATH3012 },
239 { USB_DEVICE(0x13d3, 0x3402), .driver_info = BTUSB_ATH3012 },
240 { USB_DEVICE(0x13d3, 0x3408), .driver_info = BTUSB_ATH3012 },
241 { USB_DEVICE(0x13d3, 0x3423), .driver_info = BTUSB_ATH3012 },
242 { USB_DEVICE(0x13d3, 0x3432), .driver_info = BTUSB_ATH3012 },
243 { USB_DEVICE(0x13d3, 0x3472), .driver_info = BTUSB_ATH3012 },
244 { USB_DEVICE(0x13d3, 0x3474), .driver_info = BTUSB_ATH3012 },
245 { USB_DEVICE(0x13d3, 0x3487), .driver_info = BTUSB_ATH3012 },
246 { USB_DEVICE(0x13d3, 0x3490), .driver_info = BTUSB_ATH3012 },
247
248 /* Atheros AR5BBU12 with sflash firmware */
249 { USB_DEVICE(0x0489, 0xe02c), .driver_info = BTUSB_IGNORE },
250
251 /* Atheros AR5BBU12 with sflash firmware */
252 { USB_DEVICE(0x0489, 0xe036), .driver_info = BTUSB_ATH3012 },
253 { USB_DEVICE(0x0489, 0xe03c), .driver_info = BTUSB_ATH3012 },
254
255 /* QCA ROME chipset */
256 { USB_DEVICE(0x0cf3, 0xe007), .driver_info = BTUSB_QCA_ROME },
257 { USB_DEVICE(0x0cf3, 0xe009), .driver_info = BTUSB_QCA_ROME },
258 { USB_DEVICE(0x0cf3, 0xe300), .driver_info = BTUSB_QCA_ROME },
259 { USB_DEVICE(0x0cf3, 0xe360), .driver_info = BTUSB_QCA_ROME },
260 { USB_DEVICE(0x0489, 0xe092), .driver_info = BTUSB_QCA_ROME },
261 { USB_DEVICE(0x04ca, 0x3011), .driver_info = BTUSB_QCA_ROME },
262
263 /* Broadcom BCM2035 */
264 { USB_DEVICE(0x0a5c, 0x2009), .driver_info = BTUSB_BCM92035 },
265 { USB_DEVICE(0x0a5c, 0x200a), .driver_info = BTUSB_WRONG_SCO_MTU },
266 { USB_DEVICE(0x0a5c, 0x2035), .driver_info = BTUSB_WRONG_SCO_MTU },
267
268 /* Broadcom BCM2045 */
269 { USB_DEVICE(0x0a5c, 0x2039), .driver_info = BTUSB_WRONG_SCO_MTU },
270 { USB_DEVICE(0x0a5c, 0x2101), .driver_info = BTUSB_WRONG_SCO_MTU },
271
272 /* IBM/Lenovo ThinkPad with Broadcom chip */
273 { USB_DEVICE(0x0a5c, 0x201e), .driver_info = BTUSB_WRONG_SCO_MTU },
274 { USB_DEVICE(0x0a5c, 0x2110), .driver_info = BTUSB_WRONG_SCO_MTU },
275
276 /* HP laptop with Broadcom chip */
277 { USB_DEVICE(0x03f0, 0x171d), .driver_info = BTUSB_WRONG_SCO_MTU },
278
279 /* Dell laptop with Broadcom chip */
280 { USB_DEVICE(0x413c, 0x8126), .driver_info = BTUSB_WRONG_SCO_MTU },
281
282 /* Dell Wireless 370 and 410 devices */
283 { USB_DEVICE(0x413c, 0x8152), .driver_info = BTUSB_WRONG_SCO_MTU },
284 { USB_DEVICE(0x413c, 0x8156), .driver_info = BTUSB_WRONG_SCO_MTU },
285
286 /* Belkin F8T012 and F8T013 devices */
287 { USB_DEVICE(0x050d, 0x0012), .driver_info = BTUSB_WRONG_SCO_MTU },
288 { USB_DEVICE(0x050d, 0x0013), .driver_info = BTUSB_WRONG_SCO_MTU },
289
290 /* Asus WL-BTD202 device */
291 { USB_DEVICE(0x0b05, 0x1715), .driver_info = BTUSB_WRONG_SCO_MTU },
292
293 /* Kensington Bluetooth USB adapter */
294 { USB_DEVICE(0x047d, 0x105e), .driver_info = BTUSB_WRONG_SCO_MTU },
295
296 /* RTX Telecom based adapters with buggy SCO support */
297 { USB_DEVICE(0x0400, 0x0807), .driver_info = BTUSB_BROKEN_ISOC },
298 { USB_DEVICE(0x0400, 0x080a), .driver_info = BTUSB_BROKEN_ISOC },
299
300 /* CONWISE Technology based adapters with buggy SCO support */
301 { USB_DEVICE(0x0e5e, 0x6622),
302 .driver_info = BTUSB_BROKEN_ISOC | BTUSB_CW6622},
303
304 /* Roper Class 1 Bluetooth Dongle (Silicon Wave based) */
305 { USB_DEVICE(0x1310, 0x0001), .driver_info = BTUSB_SWAVE },
306
307 /* Digianswer devices */
308 { USB_DEVICE(0x08fd, 0x0001), .driver_info = BTUSB_DIGIANSWER },
309 { USB_DEVICE(0x08fd, 0x0002), .driver_info = BTUSB_IGNORE },
310
311 /* CSR BlueCore Bluetooth Sniffer */
312 { USB_DEVICE(0x0a12, 0x0002),
313 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
314
315 /* Frontline ComProbe Bluetooth Sniffer */
316 { USB_DEVICE(0x16d3, 0x0002),
317 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
318
319 /* Marvell Bluetooth devices */
320 { USB_DEVICE(0x1286, 0x2044), .driver_info = BTUSB_MARVELL },
321 { USB_DEVICE(0x1286, 0x2046), .driver_info = BTUSB_MARVELL },
322 { USB_DEVICE(0x1286, 0x204e), .driver_info = BTUSB_MARVELL },
323
324 /* Intel Bluetooth devices */
325 { USB_DEVICE(0x8087, 0x07da), .driver_info = BTUSB_CSR },
326 { USB_DEVICE(0x8087, 0x07dc), .driver_info = BTUSB_INTEL },
327 { USB_DEVICE(0x8087, 0x0a2a), .driver_info = BTUSB_INTEL },
328 { USB_DEVICE(0x8087, 0x0a2b), .driver_info = BTUSB_INTEL_NEW },
329 { USB_DEVICE(0x8087, 0x0aa7), .driver_info = BTUSB_INTEL },
330
331 /* Other Intel Bluetooth devices */
332 { USB_VENDOR_AND_INTERFACE_INFO(0x8087, 0xe0, 0x01, 0x01),
333 .driver_info = BTUSB_IGNORE },
334
335 /* Realtek Bluetooth devices */
336 { USB_VENDOR_AND_INTERFACE_INFO(0x0bda, 0xe0, 0x01, 0x01),
337 .driver_info = BTUSB_REALTEK },
338
339 /* Additional Realtek 8723AE Bluetooth devices */
340 { USB_DEVICE(0x0930, 0x021d), .driver_info = BTUSB_REALTEK },
341 { USB_DEVICE(0x13d3, 0x3394), .driver_info = BTUSB_REALTEK },
342
343 /* Additional Realtek 8723BE Bluetooth devices */
344 { USB_DEVICE(0x0489, 0xe085), .driver_info = BTUSB_REALTEK },
345 { USB_DEVICE(0x0489, 0xe08b), .driver_info = BTUSB_REALTEK },
346 { USB_DEVICE(0x13d3, 0x3410), .driver_info = BTUSB_REALTEK },
347 { USB_DEVICE(0x13d3, 0x3416), .driver_info = BTUSB_REALTEK },
348 { USB_DEVICE(0x13d3, 0x3459), .driver_info = BTUSB_REALTEK },
349
350 /* Additional Realtek 8821AE Bluetooth devices */
351 { USB_DEVICE(0x0b05, 0x17dc), .driver_info = BTUSB_REALTEK },
352 { USB_DEVICE(0x13d3, 0x3414), .driver_info = BTUSB_REALTEK },
353 { USB_DEVICE(0x13d3, 0x3458), .driver_info = BTUSB_REALTEK },
354 { USB_DEVICE(0x13d3, 0x3461), .driver_info = BTUSB_REALTEK },
355 { USB_DEVICE(0x13d3, 0x3462), .driver_info = BTUSB_REALTEK },
356
357 /* Silicon Wave based devices */
358 { USB_DEVICE(0x0c10, 0x0000), .driver_info = BTUSB_SWAVE },
359
360 { } /* Terminating entry */
361 };
362
363 #define BTUSB_MAX_ISOC_FRAMES 10
364
365 #define BTUSB_INTR_RUNNING 0
366 #define BTUSB_BULK_RUNNING 1
367 #define BTUSB_ISOC_RUNNING 2
368 #define BTUSB_SUSPENDING 3
369 #define BTUSB_DID_ISO_RESUME 4
370 #define BTUSB_BOOTLOADER 5
371 #define BTUSB_DOWNLOADING 6
372 #define BTUSB_FIRMWARE_LOADED 7
373 #define BTUSB_FIRMWARE_FAILED 8
374 #define BTUSB_BOOTING 9
375 #define BTUSB_RESET_RESUME 10
376 #define BTUSB_DIAG_RUNNING 11
377
378 struct btusb_data {
379 struct hci_dev *hdev;
380 struct usb_device *udev;
381 struct usb_interface *intf;
382 struct usb_interface *isoc;
383 struct usb_interface *diag;
384
385 unsigned long flags;
386
387 struct work_struct work;
388 struct work_struct waker;
389
390 struct usb_anchor deferred;
391 struct usb_anchor tx_anchor;
392 int tx_in_flight;
393 spinlock_t txlock;
394
395 struct usb_anchor intr_anchor;
396 struct usb_anchor bulk_anchor;
397 struct usb_anchor isoc_anchor;
398 struct usb_anchor diag_anchor;
399 spinlock_t rxlock;
400
401 struct sk_buff *evt_skb;
402 struct sk_buff *acl_skb;
403 struct sk_buff *sco_skb;
404
405 struct usb_endpoint_descriptor *intr_ep;
406 struct usb_endpoint_descriptor *bulk_tx_ep;
407 struct usb_endpoint_descriptor *bulk_rx_ep;
408 struct usb_endpoint_descriptor *isoc_tx_ep;
409 struct usb_endpoint_descriptor *isoc_rx_ep;
410 struct usb_endpoint_descriptor *diag_tx_ep;
411 struct usb_endpoint_descriptor *diag_rx_ep;
412
413 __u8 cmdreq_type;
414 __u8 cmdreq;
415
416 unsigned int sco_num;
417 int isoc_altsetting;
418 int suspend_count;
419
420 int (*recv_event)(struct hci_dev *hdev, struct sk_buff *skb);
421 int (*recv_bulk)(struct btusb_data *data, void *buffer, int count);
422
423 int (*setup_on_usb)(struct hci_dev *hdev);
424 };
425
426 static inline void btusb_free_frags(struct btusb_data *data)
427 {
428 unsigned long flags;
429
430 spin_lock_irqsave(&data->rxlock, flags);
431
432 kfree_skb(data->evt_skb);
433 data->evt_skb = NULL;
434
435 kfree_skb(data->acl_skb);
436 data->acl_skb = NULL;
437
438 kfree_skb(data->sco_skb);
439 data->sco_skb = NULL;
440
441 spin_unlock_irqrestore(&data->rxlock, flags);
442 }
443
444 static int btusb_recv_intr(struct btusb_data *data, void *buffer, int count)
445 {
446 struct sk_buff *skb;
447 int err = 0;
448
449 spin_lock(&data->rxlock);
450 skb = data->evt_skb;
451
452 while (count) {
453 int len;
454
455 if (!skb) {
456 skb = bt_skb_alloc(HCI_MAX_EVENT_SIZE, GFP_ATOMIC);
457 if (!skb) {
458 err = -ENOMEM;
459 break;
460 }
461
462 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
463 hci_skb_expect(skb) = HCI_EVENT_HDR_SIZE;
464 }
465
466 len = min_t(uint, hci_skb_expect(skb), count);
467 memcpy(skb_put(skb, len), buffer, len);
468
469 count -= len;
470 buffer += len;
471 hci_skb_expect(skb) -= len;
472
473 if (skb->len == HCI_EVENT_HDR_SIZE) {
474 /* Complete event header */
475 hci_skb_expect(skb) = hci_event_hdr(skb)->plen;
476
477 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
478 kfree_skb(skb);
479 skb = NULL;
480
481 err = -EILSEQ;
482 break;
483 }
484 }
485
486 if (!hci_skb_expect(skb)) {
487 /* Complete frame */
488 data->recv_event(data->hdev, skb);
489 skb = NULL;
490 }
491 }
492
493 data->evt_skb = skb;
494 spin_unlock(&data->rxlock);
495
496 return err;
497 }
498
499 static int btusb_recv_bulk(struct btusb_data *data, void *buffer, int count)
500 {
501 struct sk_buff *skb;
502 int err = 0;
503
504 spin_lock(&data->rxlock);
505 skb = data->acl_skb;
506
507 while (count) {
508 int len;
509
510 if (!skb) {
511 skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
512 if (!skb) {
513 err = -ENOMEM;
514 break;
515 }
516
517 hci_skb_pkt_type(skb) = HCI_ACLDATA_PKT;
518 hci_skb_expect(skb) = HCI_ACL_HDR_SIZE;
519 }
520
521 len = min_t(uint, hci_skb_expect(skb), count);
522 memcpy(skb_put(skb, len), buffer, len);
523
524 count -= len;
525 buffer += len;
526 hci_skb_expect(skb) -= len;
527
528 if (skb->len == HCI_ACL_HDR_SIZE) {
529 __le16 dlen = hci_acl_hdr(skb)->dlen;
530
531 /* Complete ACL header */
532 hci_skb_expect(skb) = __le16_to_cpu(dlen);
533
534 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
535 kfree_skb(skb);
536 skb = NULL;
537
538 err = -EILSEQ;
539 break;
540 }
541 }
542
543 if (!hci_skb_expect(skb)) {
544 /* Complete frame */
545 hci_recv_frame(data->hdev, skb);
546 skb = NULL;
547 }
548 }
549
550 data->acl_skb = skb;
551 spin_unlock(&data->rxlock);
552
553 return err;
554 }
555
556 static int btusb_recv_isoc(struct btusb_data *data, void *buffer, int count)
557 {
558 struct sk_buff *skb;
559 int err = 0;
560
561 spin_lock(&data->rxlock);
562 skb = data->sco_skb;
563
564 while (count) {
565 int len;
566
567 if (!skb) {
568 skb = bt_skb_alloc(HCI_MAX_SCO_SIZE, GFP_ATOMIC);
569 if (!skb) {
570 err = -ENOMEM;
571 break;
572 }
573
574 hci_skb_pkt_type(skb) = HCI_SCODATA_PKT;
575 hci_skb_expect(skb) = HCI_SCO_HDR_SIZE;
576 }
577
578 len = min_t(uint, hci_skb_expect(skb), count);
579 memcpy(skb_put(skb, len), buffer, len);
580
581 count -= len;
582 buffer += len;
583 hci_skb_expect(skb) -= len;
584
585 if (skb->len == HCI_SCO_HDR_SIZE) {
586 /* Complete SCO header */
587 hci_skb_expect(skb) = hci_sco_hdr(skb)->dlen;
588
589 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
590 kfree_skb(skb);
591 skb = NULL;
592
593 err = -EILSEQ;
594 break;
595 }
596 }
597
598 if (!hci_skb_expect(skb)) {
599 /* Complete frame */
600 hci_recv_frame(data->hdev, skb);
601 skb = NULL;
602 }
603 }
604
605 data->sco_skb = skb;
606 spin_unlock(&data->rxlock);
607
608 return err;
609 }
610
611 static void btusb_intr_complete(struct urb *urb)
612 {
613 struct hci_dev *hdev = urb->context;
614 struct btusb_data *data = hci_get_drvdata(hdev);
615 int err;
616
617 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
618 urb->actual_length);
619
620 if (!test_bit(HCI_RUNNING, &hdev->flags))
621 return;
622
623 if (urb->status == 0) {
624 hdev->stat.byte_rx += urb->actual_length;
625
626 if (btusb_recv_intr(data, urb->transfer_buffer,
627 urb->actual_length) < 0) {
628 BT_ERR("%s corrupted event packet", hdev->name);
629 hdev->stat.err_rx++;
630 }
631 } else if (urb->status == -ENOENT) {
632 /* Avoid suspend failed when usb_kill_urb */
633 return;
634 }
635
636 if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
637 return;
638
639 usb_mark_last_busy(data->udev);
640 usb_anchor_urb(urb, &data->intr_anchor);
641
642 err = usb_submit_urb(urb, GFP_ATOMIC);
643 if (err < 0) {
644 /* -EPERM: urb is being killed;
645 * -ENODEV: device got disconnected */
646 if (err != -EPERM && err != -ENODEV)
647 BT_ERR("%s urb %p failed to resubmit (%d)",
648 hdev->name, urb, -err);
649 usb_unanchor_urb(urb);
650 }
651 }
652
653 static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags)
654 {
655 struct btusb_data *data = hci_get_drvdata(hdev);
656 struct urb *urb;
657 unsigned char *buf;
658 unsigned int pipe;
659 int err, size;
660
661 BT_DBG("%s", hdev->name);
662
663 if (!data->intr_ep)
664 return -ENODEV;
665
666 urb = usb_alloc_urb(0, mem_flags);
667 if (!urb)
668 return -ENOMEM;
669
670 size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
671
672 buf = kmalloc(size, mem_flags);
673 if (!buf) {
674 usb_free_urb(urb);
675 return -ENOMEM;
676 }
677
678 pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
679
680 usb_fill_int_urb(urb, data->udev, pipe, buf, size,
681 btusb_intr_complete, hdev, data->intr_ep->bInterval);
682
683 urb->transfer_flags |= URB_FREE_BUFFER;
684
685 usb_anchor_urb(urb, &data->intr_anchor);
686
687 err = usb_submit_urb(urb, mem_flags);
688 if (err < 0) {
689 if (err != -EPERM && err != -ENODEV)
690 BT_ERR("%s urb %p submission failed (%d)",
691 hdev->name, urb, -err);
692 usb_unanchor_urb(urb);
693 }
694
695 usb_free_urb(urb);
696
697 return err;
698 }
699
700 static void btusb_bulk_complete(struct urb *urb)
701 {
702 struct hci_dev *hdev = urb->context;
703 struct btusb_data *data = hci_get_drvdata(hdev);
704 int err;
705
706 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
707 urb->actual_length);
708
709 if (!test_bit(HCI_RUNNING, &hdev->flags))
710 return;
711
712 if (urb->status == 0) {
713 hdev->stat.byte_rx += urb->actual_length;
714
715 if (data->recv_bulk(data, urb->transfer_buffer,
716 urb->actual_length) < 0) {
717 BT_ERR("%s corrupted ACL packet", hdev->name);
718 hdev->stat.err_rx++;
719 }
720 } else if (urb->status == -ENOENT) {
721 /* Avoid suspend failed when usb_kill_urb */
722 return;
723 }
724
725 if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
726 return;
727
728 usb_anchor_urb(urb, &data->bulk_anchor);
729 usb_mark_last_busy(data->udev);
730
731 err = usb_submit_urb(urb, GFP_ATOMIC);
732 if (err < 0) {
733 /* -EPERM: urb is being killed;
734 * -ENODEV: device got disconnected */
735 if (err != -EPERM && err != -ENODEV)
736 BT_ERR("%s urb %p failed to resubmit (%d)",
737 hdev->name, urb, -err);
738 usb_unanchor_urb(urb);
739 }
740 }
741
742 static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
743 {
744 struct btusb_data *data = hci_get_drvdata(hdev);
745 struct urb *urb;
746 unsigned char *buf;
747 unsigned int pipe;
748 int err, size = HCI_MAX_FRAME_SIZE;
749
750 BT_DBG("%s", hdev->name);
751
752 if (!data->bulk_rx_ep)
753 return -ENODEV;
754
755 urb = usb_alloc_urb(0, mem_flags);
756 if (!urb)
757 return -ENOMEM;
758
759 buf = kmalloc(size, mem_flags);
760 if (!buf) {
761 usb_free_urb(urb);
762 return -ENOMEM;
763 }
764
765 pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
766
767 usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
768 btusb_bulk_complete, hdev);
769
770 urb->transfer_flags |= URB_FREE_BUFFER;
771
772 usb_mark_last_busy(data->udev);
773 usb_anchor_urb(urb, &data->bulk_anchor);
774
775 err = usb_submit_urb(urb, mem_flags);
776 if (err < 0) {
777 if (err != -EPERM && err != -ENODEV)
778 BT_ERR("%s urb %p submission failed (%d)",
779 hdev->name, urb, -err);
780 usb_unanchor_urb(urb);
781 }
782
783 usb_free_urb(urb);
784
785 return err;
786 }
787
788 static void btusb_isoc_complete(struct urb *urb)
789 {
790 struct hci_dev *hdev = urb->context;
791 struct btusb_data *data = hci_get_drvdata(hdev);
792 int i, err;
793
794 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
795 urb->actual_length);
796
797 if (!test_bit(HCI_RUNNING, &hdev->flags))
798 return;
799
800 if (urb->status == 0) {
801 for (i = 0; i < urb->number_of_packets; i++) {
802 unsigned int offset = urb->iso_frame_desc[i].offset;
803 unsigned int length = urb->iso_frame_desc[i].actual_length;
804
805 if (urb->iso_frame_desc[i].status)
806 continue;
807
808 hdev->stat.byte_rx += length;
809
810 if (btusb_recv_isoc(data, urb->transfer_buffer + offset,
811 length) < 0) {
812 BT_ERR("%s corrupted SCO packet", hdev->name);
813 hdev->stat.err_rx++;
814 }
815 }
816 } else if (urb->status == -ENOENT) {
817 /* Avoid suspend failed when usb_kill_urb */
818 return;
819 }
820
821 if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags))
822 return;
823
824 usb_anchor_urb(urb, &data->isoc_anchor);
825
826 err = usb_submit_urb(urb, GFP_ATOMIC);
827 if (err < 0) {
828 /* -EPERM: urb is being killed;
829 * -ENODEV: device got disconnected */
830 if (err != -EPERM && err != -ENODEV)
831 BT_ERR("%s urb %p failed to resubmit (%d)",
832 hdev->name, urb, -err);
833 usb_unanchor_urb(urb);
834 }
835 }
836
837 static inline void __fill_isoc_descriptor(struct urb *urb, int len, int mtu)
838 {
839 int i, offset = 0;
840
841 BT_DBG("len %d mtu %d", len, mtu);
842
843 for (i = 0; i < BTUSB_MAX_ISOC_FRAMES && len >= mtu;
844 i++, offset += mtu, len -= mtu) {
845 urb->iso_frame_desc[i].offset = offset;
846 urb->iso_frame_desc[i].length = mtu;
847 }
848
849 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
850 urb->iso_frame_desc[i].offset = offset;
851 urb->iso_frame_desc[i].length = len;
852 i++;
853 }
854
855 urb->number_of_packets = i;
856 }
857
858 static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags)
859 {
860 struct btusb_data *data = hci_get_drvdata(hdev);
861 struct urb *urb;
862 unsigned char *buf;
863 unsigned int pipe;
864 int err, size;
865
866 BT_DBG("%s", hdev->name);
867
868 if (!data->isoc_rx_ep)
869 return -ENODEV;
870
871 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags);
872 if (!urb)
873 return -ENOMEM;
874
875 size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) *
876 BTUSB_MAX_ISOC_FRAMES;
877
878 buf = kmalloc(size, mem_flags);
879 if (!buf) {
880 usb_free_urb(urb);
881 return -ENOMEM;
882 }
883
884 pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress);
885
886 usb_fill_int_urb(urb, data->udev, pipe, buf, size, btusb_isoc_complete,
887 hdev, data->isoc_rx_ep->bInterval);
888
889 urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP;
890
891 __fill_isoc_descriptor(urb, size,
892 le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize));
893
894 usb_anchor_urb(urb, &data->isoc_anchor);
895
896 err = usb_submit_urb(urb, mem_flags);
897 if (err < 0) {
898 if (err != -EPERM && err != -ENODEV)
899 BT_ERR("%s urb %p submission failed (%d)",
900 hdev->name, urb, -err);
901 usb_unanchor_urb(urb);
902 }
903
904 usb_free_urb(urb);
905
906 return err;
907 }
908
909 static void btusb_diag_complete(struct urb *urb)
910 {
911 struct hci_dev *hdev = urb->context;
912 struct btusb_data *data = hci_get_drvdata(hdev);
913 int err;
914
915 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
916 urb->actual_length);
917
918 if (urb->status == 0) {
919 struct sk_buff *skb;
920
921 skb = bt_skb_alloc(urb->actual_length, GFP_ATOMIC);
922 if (skb) {
923 memcpy(skb_put(skb, urb->actual_length),
924 urb->transfer_buffer, urb->actual_length);
925 hci_recv_diag(hdev, skb);
926 }
927 } else if (urb->status == -ENOENT) {
928 /* Avoid suspend failed when usb_kill_urb */
929 return;
930 }
931
932 if (!test_bit(BTUSB_DIAG_RUNNING, &data->flags))
933 return;
934
935 usb_anchor_urb(urb, &data->diag_anchor);
936 usb_mark_last_busy(data->udev);
937
938 err = usb_submit_urb(urb, GFP_ATOMIC);
939 if (err < 0) {
940 /* -EPERM: urb is being killed;
941 * -ENODEV: device got disconnected */
942 if (err != -EPERM && err != -ENODEV)
943 BT_ERR("%s urb %p failed to resubmit (%d)",
944 hdev->name, urb, -err);
945 usb_unanchor_urb(urb);
946 }
947 }
948
949 static int btusb_submit_diag_urb(struct hci_dev *hdev, gfp_t mem_flags)
950 {
951 struct btusb_data *data = hci_get_drvdata(hdev);
952 struct urb *urb;
953 unsigned char *buf;
954 unsigned int pipe;
955 int err, size = HCI_MAX_FRAME_SIZE;
956
957 BT_DBG("%s", hdev->name);
958
959 if (!data->diag_rx_ep)
960 return -ENODEV;
961
962 urb = usb_alloc_urb(0, mem_flags);
963 if (!urb)
964 return -ENOMEM;
965
966 buf = kmalloc(size, mem_flags);
967 if (!buf) {
968 usb_free_urb(urb);
969 return -ENOMEM;
970 }
971
972 pipe = usb_rcvbulkpipe(data->udev, data->diag_rx_ep->bEndpointAddress);
973
974 usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
975 btusb_diag_complete, hdev);
976
977 urb->transfer_flags |= URB_FREE_BUFFER;
978
979 usb_mark_last_busy(data->udev);
980 usb_anchor_urb(urb, &data->diag_anchor);
981
982 err = usb_submit_urb(urb, mem_flags);
983 if (err < 0) {
984 if (err != -EPERM && err != -ENODEV)
985 BT_ERR("%s urb %p submission failed (%d)",
986 hdev->name, urb, -err);
987 usb_unanchor_urb(urb);
988 }
989
990 usb_free_urb(urb);
991
992 return err;
993 }
994
995 static void btusb_tx_complete(struct urb *urb)
996 {
997 struct sk_buff *skb = urb->context;
998 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
999 struct btusb_data *data = hci_get_drvdata(hdev);
1000
1001 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1002 urb->actual_length);
1003
1004 if (!test_bit(HCI_RUNNING, &hdev->flags))
1005 goto done;
1006
1007 if (!urb->status)
1008 hdev->stat.byte_tx += urb->transfer_buffer_length;
1009 else
1010 hdev->stat.err_tx++;
1011
1012 done:
1013 spin_lock(&data->txlock);
1014 data->tx_in_flight--;
1015 spin_unlock(&data->txlock);
1016
1017 kfree(urb->setup_packet);
1018
1019 kfree_skb(skb);
1020 }
1021
1022 static void btusb_isoc_tx_complete(struct urb *urb)
1023 {
1024 struct sk_buff *skb = urb->context;
1025 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
1026
1027 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1028 urb->actual_length);
1029
1030 if (!test_bit(HCI_RUNNING, &hdev->flags))
1031 goto done;
1032
1033 if (!urb->status)
1034 hdev->stat.byte_tx += urb->transfer_buffer_length;
1035 else
1036 hdev->stat.err_tx++;
1037
1038 done:
1039 kfree(urb->setup_packet);
1040
1041 kfree_skb(skb);
1042 }
1043
1044 static int btusb_open(struct hci_dev *hdev)
1045 {
1046 struct btusb_data *data = hci_get_drvdata(hdev);
1047 int err;
1048
1049 BT_DBG("%s", hdev->name);
1050
1051 err = usb_autopm_get_interface(data->intf);
1052 if (err < 0)
1053 return err;
1054
1055 /* Patching USB firmware files prior to starting any URBs of HCI path
1056 * It is more safe to use USB bulk channel for downloading USB patch
1057 */
1058 if (data->setup_on_usb) {
1059 err = data->setup_on_usb(hdev);
1060 if (err < 0)
1061 return err;
1062 }
1063
1064 data->intf->needs_remote_wakeup = 1;
1065
1066 if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
1067 goto done;
1068
1069 err = btusb_submit_intr_urb(hdev, GFP_KERNEL);
1070 if (err < 0)
1071 goto failed;
1072
1073 err = btusb_submit_bulk_urb(hdev, GFP_KERNEL);
1074 if (err < 0) {
1075 usb_kill_anchored_urbs(&data->intr_anchor);
1076 goto failed;
1077 }
1078
1079 set_bit(BTUSB_BULK_RUNNING, &data->flags);
1080 btusb_submit_bulk_urb(hdev, GFP_KERNEL);
1081
1082 if (data->diag) {
1083 if (!btusb_submit_diag_urb(hdev, GFP_KERNEL))
1084 set_bit(BTUSB_DIAG_RUNNING, &data->flags);
1085 }
1086
1087 done:
1088 usb_autopm_put_interface(data->intf);
1089 return 0;
1090
1091 failed:
1092 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
1093 usb_autopm_put_interface(data->intf);
1094 return err;
1095 }
1096
1097 static void btusb_stop_traffic(struct btusb_data *data)
1098 {
1099 usb_kill_anchored_urbs(&data->intr_anchor);
1100 usb_kill_anchored_urbs(&data->bulk_anchor);
1101 usb_kill_anchored_urbs(&data->isoc_anchor);
1102 usb_kill_anchored_urbs(&data->diag_anchor);
1103 }
1104
1105 static int btusb_close(struct hci_dev *hdev)
1106 {
1107 struct btusb_data *data = hci_get_drvdata(hdev);
1108 int err;
1109
1110 BT_DBG("%s", hdev->name);
1111
1112 cancel_work_sync(&data->work);
1113 cancel_work_sync(&data->waker);
1114
1115 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1116 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
1117 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
1118 clear_bit(BTUSB_DIAG_RUNNING, &data->flags);
1119
1120 btusb_stop_traffic(data);
1121 btusb_free_frags(data);
1122
1123 err = usb_autopm_get_interface(data->intf);
1124 if (err < 0)
1125 goto failed;
1126
1127 data->intf->needs_remote_wakeup = 0;
1128 usb_autopm_put_interface(data->intf);
1129
1130 failed:
1131 usb_scuttle_anchored_urbs(&data->deferred);
1132 return 0;
1133 }
1134
1135 static int btusb_flush(struct hci_dev *hdev)
1136 {
1137 struct btusb_data *data = hci_get_drvdata(hdev);
1138
1139 BT_DBG("%s", hdev->name);
1140
1141 usb_kill_anchored_urbs(&data->tx_anchor);
1142 btusb_free_frags(data);
1143
1144 return 0;
1145 }
1146
1147 static struct urb *alloc_ctrl_urb(struct hci_dev *hdev, struct sk_buff *skb)
1148 {
1149 struct btusb_data *data = hci_get_drvdata(hdev);
1150 struct usb_ctrlrequest *dr;
1151 struct urb *urb;
1152 unsigned int pipe;
1153
1154 urb = usb_alloc_urb(0, GFP_KERNEL);
1155 if (!urb)
1156 return ERR_PTR(-ENOMEM);
1157
1158 dr = kmalloc(sizeof(*dr), GFP_KERNEL);
1159 if (!dr) {
1160 usb_free_urb(urb);
1161 return ERR_PTR(-ENOMEM);
1162 }
1163
1164 dr->bRequestType = data->cmdreq_type;
1165 dr->bRequest = data->cmdreq;
1166 dr->wIndex = 0;
1167 dr->wValue = 0;
1168 dr->wLength = __cpu_to_le16(skb->len);
1169
1170 pipe = usb_sndctrlpipe(data->udev, 0x00);
1171
1172 usb_fill_control_urb(urb, data->udev, pipe, (void *)dr,
1173 skb->data, skb->len, btusb_tx_complete, skb);
1174
1175 skb->dev = (void *)hdev;
1176
1177 return urb;
1178 }
1179
1180 static struct urb *alloc_bulk_urb(struct hci_dev *hdev, struct sk_buff *skb)
1181 {
1182 struct btusb_data *data = hci_get_drvdata(hdev);
1183 struct urb *urb;
1184 unsigned int pipe;
1185
1186 if (!data->bulk_tx_ep)
1187 return ERR_PTR(-ENODEV);
1188
1189 urb = usb_alloc_urb(0, GFP_KERNEL);
1190 if (!urb)
1191 return ERR_PTR(-ENOMEM);
1192
1193 pipe = usb_sndbulkpipe(data->udev, data->bulk_tx_ep->bEndpointAddress);
1194
1195 usb_fill_bulk_urb(urb, data->udev, pipe,
1196 skb->data, skb->len, btusb_tx_complete, skb);
1197
1198 skb->dev = (void *)hdev;
1199
1200 return urb;
1201 }
1202
1203 static struct urb *alloc_isoc_urb(struct hci_dev *hdev, struct sk_buff *skb)
1204 {
1205 struct btusb_data *data = hci_get_drvdata(hdev);
1206 struct urb *urb;
1207 unsigned int pipe;
1208
1209 if (!data->isoc_tx_ep)
1210 return ERR_PTR(-ENODEV);
1211
1212 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_KERNEL);
1213 if (!urb)
1214 return ERR_PTR(-ENOMEM);
1215
1216 pipe = usb_sndisocpipe(data->udev, data->isoc_tx_ep->bEndpointAddress);
1217
1218 usb_fill_int_urb(urb, data->udev, pipe,
1219 skb->data, skb->len, btusb_isoc_tx_complete,
1220 skb, data->isoc_tx_ep->bInterval);
1221
1222 urb->transfer_flags = URB_ISO_ASAP;
1223
1224 __fill_isoc_descriptor(urb, skb->len,
1225 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
1226
1227 skb->dev = (void *)hdev;
1228
1229 return urb;
1230 }
1231
1232 static int submit_tx_urb(struct hci_dev *hdev, struct urb *urb)
1233 {
1234 struct btusb_data *data = hci_get_drvdata(hdev);
1235 int err;
1236
1237 usb_anchor_urb(urb, &data->tx_anchor);
1238
1239 err = usb_submit_urb(urb, GFP_KERNEL);
1240 if (err < 0) {
1241 if (err != -EPERM && err != -ENODEV)
1242 BT_ERR("%s urb %p submission failed (%d)",
1243 hdev->name, urb, -err);
1244 kfree(urb->setup_packet);
1245 usb_unanchor_urb(urb);
1246 } else {
1247 usb_mark_last_busy(data->udev);
1248 }
1249
1250 usb_free_urb(urb);
1251 return err;
1252 }
1253
1254 static int submit_or_queue_tx_urb(struct hci_dev *hdev, struct urb *urb)
1255 {
1256 struct btusb_data *data = hci_get_drvdata(hdev);
1257 unsigned long flags;
1258 bool suspending;
1259
1260 spin_lock_irqsave(&data->txlock, flags);
1261 suspending = test_bit(BTUSB_SUSPENDING, &data->flags);
1262 if (!suspending)
1263 data->tx_in_flight++;
1264 spin_unlock_irqrestore(&data->txlock, flags);
1265
1266 if (!suspending)
1267 return submit_tx_urb(hdev, urb);
1268
1269 usb_anchor_urb(urb, &data->deferred);
1270 schedule_work(&data->waker);
1271
1272 usb_free_urb(urb);
1273 return 0;
1274 }
1275
1276 static int btusb_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
1277 {
1278 struct urb *urb;
1279
1280 BT_DBG("%s", hdev->name);
1281
1282 switch (hci_skb_pkt_type(skb)) {
1283 case HCI_COMMAND_PKT:
1284 urb = alloc_ctrl_urb(hdev, skb);
1285 if (IS_ERR(urb))
1286 return PTR_ERR(urb);
1287
1288 hdev->stat.cmd_tx++;
1289 return submit_or_queue_tx_urb(hdev, urb);
1290
1291 case HCI_ACLDATA_PKT:
1292 urb = alloc_bulk_urb(hdev, skb);
1293 if (IS_ERR(urb))
1294 return PTR_ERR(urb);
1295
1296 hdev->stat.acl_tx++;
1297 return submit_or_queue_tx_urb(hdev, urb);
1298
1299 case HCI_SCODATA_PKT:
1300 if (hci_conn_num(hdev, SCO_LINK) < 1)
1301 return -ENODEV;
1302
1303 urb = alloc_isoc_urb(hdev, skb);
1304 if (IS_ERR(urb))
1305 return PTR_ERR(urb);
1306
1307 hdev->stat.sco_tx++;
1308 return submit_tx_urb(hdev, urb);
1309 }
1310
1311 return -EILSEQ;
1312 }
1313
1314 static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
1315 {
1316 struct btusb_data *data = hci_get_drvdata(hdev);
1317
1318 BT_DBG("%s evt %d", hdev->name, evt);
1319
1320 if (hci_conn_num(hdev, SCO_LINK) != data->sco_num) {
1321 data->sco_num = hci_conn_num(hdev, SCO_LINK);
1322 schedule_work(&data->work);
1323 }
1324 }
1325
1326 static inline int __set_isoc_interface(struct hci_dev *hdev, int altsetting)
1327 {
1328 struct btusb_data *data = hci_get_drvdata(hdev);
1329 struct usb_interface *intf = data->isoc;
1330 struct usb_endpoint_descriptor *ep_desc;
1331 int i, err;
1332
1333 if (!data->isoc)
1334 return -ENODEV;
1335
1336 err = usb_set_interface(data->udev, 1, altsetting);
1337 if (err < 0) {
1338 BT_ERR("%s setting interface failed (%d)", hdev->name, -err);
1339 return err;
1340 }
1341
1342 data->isoc_altsetting = altsetting;
1343
1344 data->isoc_tx_ep = NULL;
1345 data->isoc_rx_ep = NULL;
1346
1347 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
1348 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
1349
1350 if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) {
1351 data->isoc_tx_ep = ep_desc;
1352 continue;
1353 }
1354
1355 if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) {
1356 data->isoc_rx_ep = ep_desc;
1357 continue;
1358 }
1359 }
1360
1361 if (!data->isoc_tx_ep || !data->isoc_rx_ep) {
1362 BT_ERR("%s invalid SCO descriptors", hdev->name);
1363 return -ENODEV;
1364 }
1365
1366 return 0;
1367 }
1368
1369 static void btusb_work(struct work_struct *work)
1370 {
1371 struct btusb_data *data = container_of(work, struct btusb_data, work);
1372 struct hci_dev *hdev = data->hdev;
1373 int new_alts;
1374 int err;
1375
1376 if (data->sco_num > 0) {
1377 if (!test_bit(BTUSB_DID_ISO_RESUME, &data->flags)) {
1378 err = usb_autopm_get_interface(data->isoc ? data->isoc : data->intf);
1379 if (err < 0) {
1380 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1381 usb_kill_anchored_urbs(&data->isoc_anchor);
1382 return;
1383 }
1384
1385 set_bit(BTUSB_DID_ISO_RESUME, &data->flags);
1386 }
1387
1388 if (hdev->voice_setting & 0x0020) {
1389 static const int alts[3] = { 2, 4, 5 };
1390
1391 new_alts = alts[data->sco_num - 1];
1392 } else {
1393 new_alts = data->sco_num;
1394 }
1395
1396 if (data->isoc_altsetting != new_alts) {
1397 unsigned long flags;
1398
1399 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1400 usb_kill_anchored_urbs(&data->isoc_anchor);
1401
1402 /* When isochronous alternate setting needs to be
1403 * changed, because SCO connection has been added
1404 * or removed, a packet fragment may be left in the
1405 * reassembling state. This could lead to wrongly
1406 * assembled fragments.
1407 *
1408 * Clear outstanding fragment when selecting a new
1409 * alternate setting.
1410 */
1411 spin_lock_irqsave(&data->rxlock, flags);
1412 kfree_skb(data->sco_skb);
1413 data->sco_skb = NULL;
1414 spin_unlock_irqrestore(&data->rxlock, flags);
1415
1416 if (__set_isoc_interface(hdev, new_alts) < 0)
1417 return;
1418 }
1419
1420 if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
1421 if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0)
1422 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1423 else
1424 btusb_submit_isoc_urb(hdev, GFP_KERNEL);
1425 }
1426 } else {
1427 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1428 usb_kill_anchored_urbs(&data->isoc_anchor);
1429
1430 __set_isoc_interface(hdev, 0);
1431 if (test_and_clear_bit(BTUSB_DID_ISO_RESUME, &data->flags))
1432 usb_autopm_put_interface(data->isoc ? data->isoc : data->intf);
1433 }
1434 }
1435
1436 static void btusb_waker(struct work_struct *work)
1437 {
1438 struct btusb_data *data = container_of(work, struct btusb_data, waker);
1439 int err;
1440
1441 err = usb_autopm_get_interface(data->intf);
1442 if (err < 0)
1443 return;
1444
1445 usb_autopm_put_interface(data->intf);
1446 }
1447
1448 static int btusb_setup_bcm92035(struct hci_dev *hdev)
1449 {
1450 struct sk_buff *skb;
1451 u8 val = 0x00;
1452
1453 BT_DBG("%s", hdev->name);
1454
1455 skb = __hci_cmd_sync(hdev, 0xfc3b, 1, &val, HCI_INIT_TIMEOUT);
1456 if (IS_ERR(skb))
1457 BT_ERR("BCM92035 command failed (%ld)", -PTR_ERR(skb));
1458 else
1459 kfree_skb(skb);
1460
1461 return 0;
1462 }
1463
1464 static int btusb_setup_csr(struct hci_dev *hdev)
1465 {
1466 struct hci_rp_read_local_version *rp;
1467 struct sk_buff *skb;
1468
1469 BT_DBG("%s", hdev->name);
1470
1471 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
1472 HCI_INIT_TIMEOUT);
1473 if (IS_ERR(skb)) {
1474 int err = PTR_ERR(skb);
1475 BT_ERR("%s: CSR: Local version failed (%d)", hdev->name, err);
1476 return err;
1477 }
1478
1479 if (skb->len != sizeof(struct hci_rp_read_local_version)) {
1480 BT_ERR("%s: CSR: Local version length mismatch", hdev->name);
1481 kfree_skb(skb);
1482 return -EIO;
1483 }
1484
1485 rp = (struct hci_rp_read_local_version *)skb->data;
1486
1487 /* Detect controllers which aren't real CSR ones. */
1488 if (le16_to_cpu(rp->manufacturer) != 10 ||
1489 le16_to_cpu(rp->lmp_subver) == 0x0c5c) {
1490 /* Clear the reset quirk since this is not an actual
1491 * early Bluetooth 1.1 device from CSR.
1492 */
1493 clear_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
1494
1495 /* These fake CSR controllers have all a broken
1496 * stored link key handling and so just disable it.
1497 */
1498 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
1499 }
1500
1501 kfree_skb(skb);
1502
1503 return 0;
1504 }
1505
1506 static const struct firmware *btusb_setup_intel_get_fw(struct hci_dev *hdev,
1507 struct intel_version *ver)
1508 {
1509 const struct firmware *fw;
1510 char fwname[64];
1511 int ret;
1512
1513 snprintf(fwname, sizeof(fwname),
1514 "intel/ibt-hw-%x.%x.%x-fw-%x.%x.%x.%x.%x.bseq",
1515 ver->hw_platform, ver->hw_variant, ver->hw_revision,
1516 ver->fw_variant, ver->fw_revision, ver->fw_build_num,
1517 ver->fw_build_ww, ver->fw_build_yy);
1518
1519 ret = request_firmware(&fw, fwname, &hdev->dev);
1520 if (ret < 0) {
1521 if (ret == -EINVAL) {
1522 BT_ERR("%s Intel firmware file request failed (%d)",
1523 hdev->name, ret);
1524 return NULL;
1525 }
1526
1527 BT_ERR("%s failed to open Intel firmware file: %s(%d)",
1528 hdev->name, fwname, ret);
1529
1530 /* If the correct firmware patch file is not found, use the
1531 * default firmware patch file instead
1532 */
1533 snprintf(fwname, sizeof(fwname), "intel/ibt-hw-%x.%x.bseq",
1534 ver->hw_platform, ver->hw_variant);
1535 if (request_firmware(&fw, fwname, &hdev->dev) < 0) {
1536 BT_ERR("%s failed to open default Intel fw file: %s",
1537 hdev->name, fwname);
1538 return NULL;
1539 }
1540 }
1541
1542 BT_INFO("%s: Intel Bluetooth firmware file: %s", hdev->name, fwname);
1543
1544 return fw;
1545 }
1546
1547 static int btusb_setup_intel_patching(struct hci_dev *hdev,
1548 const struct firmware *fw,
1549 const u8 **fw_ptr, int *disable_patch)
1550 {
1551 struct sk_buff *skb;
1552 struct hci_command_hdr *cmd;
1553 const u8 *cmd_param;
1554 struct hci_event_hdr *evt = NULL;
1555 const u8 *evt_param = NULL;
1556 int remain = fw->size - (*fw_ptr - fw->data);
1557
1558 /* The first byte indicates the types of the patch command or event.
1559 * 0x01 means HCI command and 0x02 is HCI event. If the first bytes
1560 * in the current firmware buffer doesn't start with 0x01 or
1561 * the size of remain buffer is smaller than HCI command header,
1562 * the firmware file is corrupted and it should stop the patching
1563 * process.
1564 */
1565 if (remain > HCI_COMMAND_HDR_SIZE && *fw_ptr[0] != 0x01) {
1566 BT_ERR("%s Intel fw corrupted: invalid cmd read", hdev->name);
1567 return -EINVAL;
1568 }
1569 (*fw_ptr)++;
1570 remain--;
1571
1572 cmd = (struct hci_command_hdr *)(*fw_ptr);
1573 *fw_ptr += sizeof(*cmd);
1574 remain -= sizeof(*cmd);
1575
1576 /* Ensure that the remain firmware data is long enough than the length
1577 * of command parameter. If not, the firmware file is corrupted.
1578 */
1579 if (remain < cmd->plen) {
1580 BT_ERR("%s Intel fw corrupted: invalid cmd len", hdev->name);
1581 return -EFAULT;
1582 }
1583
1584 /* If there is a command that loads a patch in the firmware
1585 * file, then enable the patch upon success, otherwise just
1586 * disable the manufacturer mode, for example patch activation
1587 * is not required when the default firmware patch file is used
1588 * because there are no patch data to load.
1589 */
1590 if (*disable_patch && le16_to_cpu(cmd->opcode) == 0xfc8e)
1591 *disable_patch = 0;
1592
1593 cmd_param = *fw_ptr;
1594 *fw_ptr += cmd->plen;
1595 remain -= cmd->plen;
1596
1597 /* This reads the expected events when the above command is sent to the
1598 * device. Some vendor commands expects more than one events, for
1599 * example command status event followed by vendor specific event.
1600 * For this case, it only keeps the last expected event. so the command
1601 * can be sent with __hci_cmd_sync_ev() which returns the sk_buff of
1602 * last expected event.
1603 */
1604 while (remain > HCI_EVENT_HDR_SIZE && *fw_ptr[0] == 0x02) {
1605 (*fw_ptr)++;
1606 remain--;
1607
1608 evt = (struct hci_event_hdr *)(*fw_ptr);
1609 *fw_ptr += sizeof(*evt);
1610 remain -= sizeof(*evt);
1611
1612 if (remain < evt->plen) {
1613 BT_ERR("%s Intel fw corrupted: invalid evt len",
1614 hdev->name);
1615 return -EFAULT;
1616 }
1617
1618 evt_param = *fw_ptr;
1619 *fw_ptr += evt->plen;
1620 remain -= evt->plen;
1621 }
1622
1623 /* Every HCI commands in the firmware file has its correspond event.
1624 * If event is not found or remain is smaller than zero, the firmware
1625 * file is corrupted.
1626 */
1627 if (!evt || !evt_param || remain < 0) {
1628 BT_ERR("%s Intel fw corrupted: invalid evt read", hdev->name);
1629 return -EFAULT;
1630 }
1631
1632 skb = __hci_cmd_sync_ev(hdev, le16_to_cpu(cmd->opcode), cmd->plen,
1633 cmd_param, evt->evt, HCI_INIT_TIMEOUT);
1634 if (IS_ERR(skb)) {
1635 BT_ERR("%s sending Intel patch command (0x%4.4x) failed (%ld)",
1636 hdev->name, cmd->opcode, PTR_ERR(skb));
1637 return PTR_ERR(skb);
1638 }
1639
1640 /* It ensures that the returned event matches the event data read from
1641 * the firmware file. At fist, it checks the length and then
1642 * the contents of the event.
1643 */
1644 if (skb->len != evt->plen) {
1645 BT_ERR("%s mismatch event length (opcode 0x%4.4x)", hdev->name,
1646 le16_to_cpu(cmd->opcode));
1647 kfree_skb(skb);
1648 return -EFAULT;
1649 }
1650
1651 if (memcmp(skb->data, evt_param, evt->plen)) {
1652 BT_ERR("%s mismatch event parameter (opcode 0x%4.4x)",
1653 hdev->name, le16_to_cpu(cmd->opcode));
1654 kfree_skb(skb);
1655 return -EFAULT;
1656 }
1657 kfree_skb(skb);
1658
1659 return 0;
1660 }
1661
1662 static int btusb_setup_intel(struct hci_dev *hdev)
1663 {
1664 struct sk_buff *skb;
1665 const struct firmware *fw;
1666 const u8 *fw_ptr;
1667 int disable_patch, err;
1668 struct intel_version ver;
1669
1670 BT_DBG("%s", hdev->name);
1671
1672 /* The controller has a bug with the first HCI command sent to it
1673 * returning number of completed commands as zero. This would stall the
1674 * command processing in the Bluetooth core.
1675 *
1676 * As a workaround, send HCI Reset command first which will reset the
1677 * number of completed commands and allow normal command processing
1678 * from now on.
1679 */
1680 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
1681 if (IS_ERR(skb)) {
1682 BT_ERR("%s sending initial HCI reset command failed (%ld)",
1683 hdev->name, PTR_ERR(skb));
1684 return PTR_ERR(skb);
1685 }
1686 kfree_skb(skb);
1687
1688 /* Read Intel specific controller version first to allow selection of
1689 * which firmware file to load.
1690 *
1691 * The returned information are hardware variant and revision plus
1692 * firmware variant, revision and build number.
1693 */
1694 err = btintel_read_version(hdev, &ver);
1695 if (err)
1696 return err;
1697
1698 BT_INFO("%s: read Intel version: %02x%02x%02x%02x%02x%02x%02x%02x%02x",
1699 hdev->name, ver.hw_platform, ver.hw_variant, ver.hw_revision,
1700 ver.fw_variant, ver.fw_revision, ver.fw_build_num,
1701 ver.fw_build_ww, ver.fw_build_yy, ver.fw_patch_num);
1702
1703 /* fw_patch_num indicates the version of patch the device currently
1704 * have. If there is no patch data in the device, it is always 0x00.
1705 * So, if it is other than 0x00, no need to patch the device again.
1706 */
1707 if (ver.fw_patch_num) {
1708 BT_INFO("%s: Intel device is already patched. patch num: %02x",
1709 hdev->name, ver.fw_patch_num);
1710 goto complete;
1711 }
1712
1713 /* Opens the firmware patch file based on the firmware version read
1714 * from the controller. If it fails to open the matching firmware
1715 * patch file, it tries to open the default firmware patch file.
1716 * If no patch file is found, allow the device to operate without
1717 * a patch.
1718 */
1719 fw = btusb_setup_intel_get_fw(hdev, &ver);
1720 if (!fw)
1721 goto complete;
1722 fw_ptr = fw->data;
1723
1724 /* Enable the manufacturer mode of the controller.
1725 * Only while this mode is enabled, the driver can download the
1726 * firmware patch data and configuration parameters.
1727 */
1728 err = btintel_enter_mfg(hdev);
1729 if (err) {
1730 release_firmware(fw);
1731 return err;
1732 }
1733
1734 disable_patch = 1;
1735
1736 /* The firmware data file consists of list of Intel specific HCI
1737 * commands and its expected events. The first byte indicates the
1738 * type of the message, either HCI command or HCI event.
1739 *
1740 * It reads the command and its expected event from the firmware file,
1741 * and send to the controller. Once __hci_cmd_sync_ev() returns,
1742 * the returned event is compared with the event read from the firmware
1743 * file and it will continue until all the messages are downloaded to
1744 * the controller.
1745 *
1746 * Once the firmware patching is completed successfully,
1747 * the manufacturer mode is disabled with reset and activating the
1748 * downloaded patch.
1749 *
1750 * If the firmware patching fails, the manufacturer mode is
1751 * disabled with reset and deactivating the patch.
1752 *
1753 * If the default patch file is used, no reset is done when disabling
1754 * the manufacturer.
1755 */
1756 while (fw->size > fw_ptr - fw->data) {
1757 int ret;
1758
1759 ret = btusb_setup_intel_patching(hdev, fw, &fw_ptr,
1760 &disable_patch);
1761 if (ret < 0)
1762 goto exit_mfg_deactivate;
1763 }
1764
1765 release_firmware(fw);
1766
1767 if (disable_patch)
1768 goto exit_mfg_disable;
1769
1770 /* Patching completed successfully and disable the manufacturer mode
1771 * with reset and activate the downloaded firmware patches.
1772 */
1773 err = btintel_exit_mfg(hdev, true, true);
1774 if (err)
1775 return err;
1776
1777 BT_INFO("%s: Intel Bluetooth firmware patch completed and activated",
1778 hdev->name);
1779
1780 goto complete;
1781
1782 exit_mfg_disable:
1783 /* Disable the manufacturer mode without reset */
1784 err = btintel_exit_mfg(hdev, false, false);
1785 if (err)
1786 return err;
1787
1788 BT_INFO("%s: Intel Bluetooth firmware patch completed", hdev->name);
1789
1790 goto complete;
1791
1792 exit_mfg_deactivate:
1793 release_firmware(fw);
1794
1795 /* Patching failed. Disable the manufacturer mode with reset and
1796 * deactivate the downloaded firmware patches.
1797 */
1798 err = btintel_exit_mfg(hdev, true, false);
1799 if (err)
1800 return err;
1801
1802 BT_INFO("%s: Intel Bluetooth firmware patch completed and deactivated",
1803 hdev->name);
1804
1805 complete:
1806 /* Set the event mask for Intel specific vendor events. This enables
1807 * a few extra events that are useful during general operation.
1808 */
1809 btintel_set_event_mask_mfg(hdev, false);
1810
1811 btintel_check_bdaddr(hdev);
1812 return 0;
1813 }
1814
1815 static int inject_cmd_complete(struct hci_dev *hdev, __u16 opcode)
1816 {
1817 struct sk_buff *skb;
1818 struct hci_event_hdr *hdr;
1819 struct hci_ev_cmd_complete *evt;
1820
1821 skb = bt_skb_alloc(sizeof(*hdr) + sizeof(*evt) + 1, GFP_ATOMIC);
1822 if (!skb)
1823 return -ENOMEM;
1824
1825 hdr = (struct hci_event_hdr *)skb_put(skb, sizeof(*hdr));
1826 hdr->evt = HCI_EV_CMD_COMPLETE;
1827 hdr->plen = sizeof(*evt) + 1;
1828
1829 evt = (struct hci_ev_cmd_complete *)skb_put(skb, sizeof(*evt));
1830 evt->ncmd = 0x01;
1831 evt->opcode = cpu_to_le16(opcode);
1832
1833 *skb_put(skb, 1) = 0x00;
1834
1835 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
1836
1837 return hci_recv_frame(hdev, skb);
1838 }
1839
1840 static int btusb_recv_bulk_intel(struct btusb_data *data, void *buffer,
1841 int count)
1842 {
1843 /* When the device is in bootloader mode, then it can send
1844 * events via the bulk endpoint. These events are treated the
1845 * same way as the ones received from the interrupt endpoint.
1846 */
1847 if (test_bit(BTUSB_BOOTLOADER, &data->flags))
1848 return btusb_recv_intr(data, buffer, count);
1849
1850 return btusb_recv_bulk(data, buffer, count);
1851 }
1852
1853 static void btusb_intel_bootup(struct btusb_data *data, const void *ptr,
1854 unsigned int len)
1855 {
1856 const struct intel_bootup *evt = ptr;
1857
1858 if (len != sizeof(*evt))
1859 return;
1860
1861 if (test_and_clear_bit(BTUSB_BOOTING, &data->flags)) {
1862 smp_mb__after_atomic();
1863 wake_up_bit(&data->flags, BTUSB_BOOTING);
1864 }
1865 }
1866
1867 static void btusb_intel_secure_send_result(struct btusb_data *data,
1868 const void *ptr, unsigned int len)
1869 {
1870 const struct intel_secure_send_result *evt = ptr;
1871
1872 if (len != sizeof(*evt))
1873 return;
1874
1875 if (evt->result)
1876 set_bit(BTUSB_FIRMWARE_FAILED, &data->flags);
1877
1878 if (test_and_clear_bit(BTUSB_DOWNLOADING, &data->flags) &&
1879 test_bit(BTUSB_FIRMWARE_LOADED, &data->flags)) {
1880 smp_mb__after_atomic();
1881 wake_up_bit(&data->flags, BTUSB_DOWNLOADING);
1882 }
1883 }
1884
1885 static int btusb_recv_event_intel(struct hci_dev *hdev, struct sk_buff *skb)
1886 {
1887 struct btusb_data *data = hci_get_drvdata(hdev);
1888
1889 if (test_bit(BTUSB_BOOTLOADER, &data->flags)) {
1890 struct hci_event_hdr *hdr = (void *)skb->data;
1891
1892 if (skb->len > HCI_EVENT_HDR_SIZE && hdr->evt == 0xff &&
1893 hdr->plen > 0) {
1894 const void *ptr = skb->data + HCI_EVENT_HDR_SIZE + 1;
1895 unsigned int len = skb->len - HCI_EVENT_HDR_SIZE - 1;
1896
1897 switch (skb->data[2]) {
1898 case 0x02:
1899 /* When switching to the operational firmware
1900 * the device sends a vendor specific event
1901 * indicating that the bootup completed.
1902 */
1903 btusb_intel_bootup(data, ptr, len);
1904 break;
1905 case 0x06:
1906 /* When the firmware loading completes the
1907 * device sends out a vendor specific event
1908 * indicating the result of the firmware
1909 * loading.
1910 */
1911 btusb_intel_secure_send_result(data, ptr, len);
1912 break;
1913 }
1914 }
1915 }
1916
1917 return hci_recv_frame(hdev, skb);
1918 }
1919
1920 static int btusb_send_frame_intel(struct hci_dev *hdev, struct sk_buff *skb)
1921 {
1922 struct btusb_data *data = hci_get_drvdata(hdev);
1923 struct urb *urb;
1924
1925 BT_DBG("%s", hdev->name);
1926
1927 switch (hci_skb_pkt_type(skb)) {
1928 case HCI_COMMAND_PKT:
1929 if (test_bit(BTUSB_BOOTLOADER, &data->flags)) {
1930 struct hci_command_hdr *cmd = (void *)skb->data;
1931 __u16 opcode = le16_to_cpu(cmd->opcode);
1932
1933 /* When in bootloader mode and the command 0xfc09
1934 * is received, it needs to be send down the
1935 * bulk endpoint. So allocate a bulk URB instead.
1936 */
1937 if (opcode == 0xfc09)
1938 urb = alloc_bulk_urb(hdev, skb);
1939 else
1940 urb = alloc_ctrl_urb(hdev, skb);
1941
1942 /* When the 0xfc01 command is issued to boot into
1943 * the operational firmware, it will actually not
1944 * send a command complete event. To keep the flow
1945 * control working inject that event here.
1946 */
1947 if (opcode == 0xfc01)
1948 inject_cmd_complete(hdev, opcode);
1949 } else {
1950 urb = alloc_ctrl_urb(hdev, skb);
1951 }
1952 if (IS_ERR(urb))
1953 return PTR_ERR(urb);
1954
1955 hdev->stat.cmd_tx++;
1956 return submit_or_queue_tx_urb(hdev, urb);
1957
1958 case HCI_ACLDATA_PKT:
1959 urb = alloc_bulk_urb(hdev, skb);
1960 if (IS_ERR(urb))
1961 return PTR_ERR(urb);
1962
1963 hdev->stat.acl_tx++;
1964 return submit_or_queue_tx_urb(hdev, urb);
1965
1966 case HCI_SCODATA_PKT:
1967 if (hci_conn_num(hdev, SCO_LINK) < 1)
1968 return -ENODEV;
1969
1970 urb = alloc_isoc_urb(hdev, skb);
1971 if (IS_ERR(urb))
1972 return PTR_ERR(urb);
1973
1974 hdev->stat.sco_tx++;
1975 return submit_tx_urb(hdev, urb);
1976 }
1977
1978 return -EILSEQ;
1979 }
1980
1981 static int btusb_setup_intel_new(struct hci_dev *hdev)
1982 {
1983 static const u8 reset_param[] = { 0x00, 0x01, 0x00, 0x01,
1984 0x00, 0x08, 0x04, 0x00 };
1985 struct btusb_data *data = hci_get_drvdata(hdev);
1986 struct sk_buff *skb;
1987 struct intel_version ver;
1988 struct intel_boot_params *params;
1989 const struct firmware *fw;
1990 const u8 *fw_ptr;
1991 u32 frag_len;
1992 char fwname[64];
1993 ktime_t calltime, delta, rettime;
1994 unsigned long long duration;
1995 int err;
1996
1997 BT_DBG("%s", hdev->name);
1998
1999 calltime = ktime_get();
2000
2001 /* Read the Intel version information to determine if the device
2002 * is in bootloader mode or if it already has operational firmware
2003 * loaded.
2004 */
2005 err = btintel_read_version(hdev, &ver);
2006 if (err)
2007 return err;
2008
2009 /* The hardware platform number has a fixed value of 0x37 and
2010 * for now only accept this single value.
2011 */
2012 if (ver.hw_platform != 0x37) {
2013 BT_ERR("%s: Unsupported Intel hardware platform (%u)",
2014 hdev->name, ver.hw_platform);
2015 return -EINVAL;
2016 }
2017
2018 /* At the moment the iBT 3.0 hardware variants 0x0b (LnP/SfP)
2019 * and 0x0c (WsP) are supported by this firmware loading method.
2020 *
2021 * This check has been put in place to ensure correct forward
2022 * compatibility options when newer hardware variants come along.
2023 */
2024 if (ver.hw_variant != 0x0b && ver.hw_variant != 0x0c) {
2025 BT_ERR("%s: Unsupported Intel hardware variant (%u)",
2026 hdev->name, ver.hw_variant);
2027 return -EINVAL;
2028 }
2029
2030 btintel_version_info(hdev, &ver);
2031
2032 /* The firmware variant determines if the device is in bootloader
2033 * mode or is running operational firmware. The value 0x06 identifies
2034 * the bootloader and the value 0x23 identifies the operational
2035 * firmware.
2036 *
2037 * When the operational firmware is already present, then only
2038 * the check for valid Bluetooth device address is needed. This
2039 * determines if the device will be added as configured or
2040 * unconfigured controller.
2041 *
2042 * It is not possible to use the Secure Boot Parameters in this
2043 * case since that command is only available in bootloader mode.
2044 */
2045 if (ver.fw_variant == 0x23) {
2046 clear_bit(BTUSB_BOOTLOADER, &data->flags);
2047 btintel_check_bdaddr(hdev);
2048 return 0;
2049 }
2050
2051 /* If the device is not in bootloader mode, then the only possible
2052 * choice is to return an error and abort the device initialization.
2053 */
2054 if (ver.fw_variant != 0x06) {
2055 BT_ERR("%s: Unsupported Intel firmware variant (%u)",
2056 hdev->name, ver.fw_variant);
2057 return -ENODEV;
2058 }
2059
2060 /* Read the secure boot parameters to identify the operating
2061 * details of the bootloader.
2062 */
2063 skb = __hci_cmd_sync(hdev, 0xfc0d, 0, NULL, HCI_INIT_TIMEOUT);
2064 if (IS_ERR(skb)) {
2065 BT_ERR("%s: Reading Intel boot parameters failed (%ld)",
2066 hdev->name, PTR_ERR(skb));
2067 return PTR_ERR(skb);
2068 }
2069
2070 if (skb->len != sizeof(*params)) {
2071 BT_ERR("%s: Intel boot parameters size mismatch", hdev->name);
2072 kfree_skb(skb);
2073 return -EILSEQ;
2074 }
2075
2076 params = (struct intel_boot_params *)skb->data;
2077
2078 BT_INFO("%s: Device revision is %u", hdev->name,
2079 le16_to_cpu(params->dev_revid));
2080
2081 BT_INFO("%s: Secure boot is %s", hdev->name,
2082 params->secure_boot ? "enabled" : "disabled");
2083
2084 BT_INFO("%s: OTP lock is %s", hdev->name,
2085 params->otp_lock ? "enabled" : "disabled");
2086
2087 BT_INFO("%s: API lock is %s", hdev->name,
2088 params->api_lock ? "enabled" : "disabled");
2089
2090 BT_INFO("%s: Debug lock is %s", hdev->name,
2091 params->debug_lock ? "enabled" : "disabled");
2092
2093 BT_INFO("%s: Minimum firmware build %u week %u %u", hdev->name,
2094 params->min_fw_build_nn, params->min_fw_build_cw,
2095 2000 + params->min_fw_build_yy);
2096
2097 /* It is required that every single firmware fragment is acknowledged
2098 * with a command complete event. If the boot parameters indicate
2099 * that this bootloader does not send them, then abort the setup.
2100 */
2101 if (params->limited_cce != 0x00) {
2102 BT_ERR("%s: Unsupported Intel firmware loading method (%u)",
2103 hdev->name, params->limited_cce);
2104 kfree_skb(skb);
2105 return -EINVAL;
2106 }
2107
2108 /* If the OTP has no valid Bluetooth device address, then there will
2109 * also be no valid address for the operational firmware.
2110 */
2111 if (!bacmp(&params->otp_bdaddr, BDADDR_ANY)) {
2112 BT_INFO("%s: No device address configured", hdev->name);
2113 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
2114 }
2115
2116 /* With this Intel bootloader only the hardware variant and device
2117 * revision information are used to select the right firmware.
2118 *
2119 * The firmware filename is ibt-<hw_variant>-<dev_revid>.sfi.
2120 *
2121 * Currently the supported hardware variants are:
2122 * 11 (0x0b) for iBT3.0 (LnP/SfP)
2123 * 12 (0x0c) for iBT3.5 (WsP)
2124 */
2125 snprintf(fwname, sizeof(fwname), "intel/ibt-%u-%u.sfi",
2126 le16_to_cpu(ver.hw_variant),
2127 le16_to_cpu(params->dev_revid));
2128
2129 err = request_firmware(&fw, fwname, &hdev->dev);
2130 if (err < 0) {
2131 BT_ERR("%s: Failed to load Intel firmware file (%d)",
2132 hdev->name, err);
2133 kfree_skb(skb);
2134 return err;
2135 }
2136
2137 BT_INFO("%s: Found device firmware: %s", hdev->name, fwname);
2138
2139 /* Save the DDC file name for later use to apply once the firmware
2140 * downloading is done.
2141 */
2142 snprintf(fwname, sizeof(fwname), "intel/ibt-%u-%u.ddc",
2143 le16_to_cpu(ver.hw_variant),
2144 le16_to_cpu(params->dev_revid));
2145
2146 kfree_skb(skb);
2147
2148 if (fw->size < 644) {
2149 BT_ERR("%s: Invalid size of firmware file (%zu)",
2150 hdev->name, fw->size);
2151 err = -EBADF;
2152 goto done;
2153 }
2154
2155 set_bit(BTUSB_DOWNLOADING, &data->flags);
2156
2157 /* Start the firmware download transaction with the Init fragment
2158 * represented by the 128 bytes of CSS header.
2159 */
2160 err = btintel_secure_send(hdev, 0x00, 128, fw->data);
2161 if (err < 0) {
2162 BT_ERR("%s: Failed to send firmware header (%d)",
2163 hdev->name, err);
2164 goto done;
2165 }
2166
2167 /* Send the 256 bytes of public key information from the firmware
2168 * as the PKey fragment.
2169 */
2170 err = btintel_secure_send(hdev, 0x03, 256, fw->data + 128);
2171 if (err < 0) {
2172 BT_ERR("%s: Failed to send firmware public key (%d)",
2173 hdev->name, err);
2174 goto done;
2175 }
2176
2177 /* Send the 256 bytes of signature information from the firmware
2178 * as the Sign fragment.
2179 */
2180 err = btintel_secure_send(hdev, 0x02, 256, fw->data + 388);
2181 if (err < 0) {
2182 BT_ERR("%s: Failed to send firmware signature (%d)",
2183 hdev->name, err);
2184 goto done;
2185 }
2186
2187 fw_ptr = fw->data + 644;
2188 frag_len = 0;
2189
2190 while (fw_ptr - fw->data < fw->size) {
2191 struct hci_command_hdr *cmd = (void *)(fw_ptr + frag_len);
2192
2193 frag_len += sizeof(*cmd) + cmd->plen;
2194
2195 /* The parameter length of the secure send command requires
2196 * a 4 byte alignment. It happens so that the firmware file
2197 * contains proper Intel_NOP commands to align the fragments
2198 * as needed.
2199 *
2200 * Send set of commands with 4 byte alignment from the
2201 * firmware data buffer as a single Data fragement.
2202 */
2203 if (!(frag_len % 4)) {
2204 err = btintel_secure_send(hdev, 0x01, frag_len, fw_ptr);
2205 if (err < 0) {
2206 BT_ERR("%s: Failed to send firmware data (%d)",
2207 hdev->name, err);
2208 goto done;
2209 }
2210
2211 fw_ptr += frag_len;
2212 frag_len = 0;
2213 }
2214 }
2215
2216 set_bit(BTUSB_FIRMWARE_LOADED, &data->flags);
2217
2218 BT_INFO("%s: Waiting for firmware download to complete", hdev->name);
2219
2220 /* Before switching the device into operational mode and with that
2221 * booting the loaded firmware, wait for the bootloader notification
2222 * that all fragments have been successfully received.
2223 *
2224 * When the event processing receives the notification, then the
2225 * BTUSB_DOWNLOADING flag will be cleared.
2226 *
2227 * The firmware loading should not take longer than 5 seconds
2228 * and thus just timeout if that happens and fail the setup
2229 * of this device.
2230 */
2231 err = wait_on_bit_timeout(&data->flags, BTUSB_DOWNLOADING,
2232 TASK_INTERRUPTIBLE,
2233 msecs_to_jiffies(5000));
2234 if (err == -EINTR) {
2235 BT_ERR("%s: Firmware loading interrupted", hdev->name);
2236 goto done;
2237 }
2238
2239 if (err) {
2240 BT_ERR("%s: Firmware loading timeout", hdev->name);
2241 err = -ETIMEDOUT;
2242 goto done;
2243 }
2244
2245 if (test_bit(BTUSB_FIRMWARE_FAILED, &data->flags)) {
2246 BT_ERR("%s: Firmware loading failed", hdev->name);
2247 err = -ENOEXEC;
2248 goto done;
2249 }
2250
2251 rettime = ktime_get();
2252 delta = ktime_sub(rettime, calltime);
2253 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
2254
2255 BT_INFO("%s: Firmware loaded in %llu usecs", hdev->name, duration);
2256
2257 done:
2258 release_firmware(fw);
2259
2260 if (err < 0)
2261 return err;
2262
2263 calltime = ktime_get();
2264
2265 set_bit(BTUSB_BOOTING, &data->flags);
2266
2267 skb = __hci_cmd_sync(hdev, 0xfc01, sizeof(reset_param), reset_param,
2268 HCI_INIT_TIMEOUT);
2269 if (IS_ERR(skb))
2270 return PTR_ERR(skb);
2271
2272 kfree_skb(skb);
2273
2274 /* The bootloader will not indicate when the device is ready. This
2275 * is done by the operational firmware sending bootup notification.
2276 *
2277 * Booting into operational firmware should not take longer than
2278 * 1 second. However if that happens, then just fail the setup
2279 * since something went wrong.
2280 */
2281 BT_INFO("%s: Waiting for device to boot", hdev->name);
2282
2283 err = wait_on_bit_timeout(&data->flags, BTUSB_BOOTING,
2284 TASK_INTERRUPTIBLE,
2285 msecs_to_jiffies(1000));
2286
2287 if (err == -EINTR) {
2288 BT_ERR("%s: Device boot interrupted", hdev->name);
2289 return -EINTR;
2290 }
2291
2292 if (err) {
2293 BT_ERR("%s: Device boot timeout", hdev->name);
2294 return -ETIMEDOUT;
2295 }
2296
2297 rettime = ktime_get();
2298 delta = ktime_sub(rettime, calltime);
2299 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
2300
2301 BT_INFO("%s: Device booted in %llu usecs", hdev->name, duration);
2302
2303 clear_bit(BTUSB_BOOTLOADER, &data->flags);
2304
2305 /* Once the device is running in operational mode, it needs to apply
2306 * the device configuration (DDC) parameters.
2307 *
2308 * The device can work without DDC parameters, so even if it fails
2309 * to load the file, no need to fail the setup.
2310 */
2311 btintel_load_ddc_config(hdev, fwname);
2312
2313 /* Set the event mask for Intel specific vendor events. This enables
2314 * a few extra events that are useful during general operation. It
2315 * does not enable any debugging related events.
2316 *
2317 * The device will function correctly without these events enabled
2318 * and thus no need to fail the setup.
2319 */
2320 btintel_set_event_mask(hdev, false);
2321
2322 return 0;
2323 }
2324
2325 static int btusb_shutdown_intel(struct hci_dev *hdev)
2326 {
2327 struct sk_buff *skb;
2328 long ret;
2329
2330 /* Some platforms have an issue with BT LED when the interface is
2331 * down or BT radio is turned off, which takes 5 seconds to BT LED
2332 * goes off. This command turns off the BT LED immediately.
2333 */
2334 skb = __hci_cmd_sync(hdev, 0xfc3f, 0, NULL, HCI_INIT_TIMEOUT);
2335 if (IS_ERR(skb)) {
2336 ret = PTR_ERR(skb);
2337 BT_ERR("%s: turning off Intel device LED failed (%ld)",
2338 hdev->name, ret);
2339 return ret;
2340 }
2341 kfree_skb(skb);
2342
2343 return 0;
2344 }
2345
2346 static int btusb_set_bdaddr_marvell(struct hci_dev *hdev,
2347 const bdaddr_t *bdaddr)
2348 {
2349 struct sk_buff *skb;
2350 u8 buf[8];
2351 long ret;
2352
2353 buf[0] = 0xfe;
2354 buf[1] = sizeof(bdaddr_t);
2355 memcpy(buf + 2, bdaddr, sizeof(bdaddr_t));
2356
2357 skb = __hci_cmd_sync(hdev, 0xfc22, sizeof(buf), buf, HCI_INIT_TIMEOUT);
2358 if (IS_ERR(skb)) {
2359 ret = PTR_ERR(skb);
2360 BT_ERR("%s: changing Marvell device address failed (%ld)",
2361 hdev->name, ret);
2362 return ret;
2363 }
2364 kfree_skb(skb);
2365
2366 return 0;
2367 }
2368
2369 static int btusb_set_bdaddr_ath3012(struct hci_dev *hdev,
2370 const bdaddr_t *bdaddr)
2371 {
2372 struct sk_buff *skb;
2373 u8 buf[10];
2374 long ret;
2375
2376 buf[0] = 0x01;
2377 buf[1] = 0x01;
2378 buf[2] = 0x00;
2379 buf[3] = sizeof(bdaddr_t);
2380 memcpy(buf + 4, bdaddr, sizeof(bdaddr_t));
2381
2382 skb = __hci_cmd_sync(hdev, 0xfc0b, sizeof(buf), buf, HCI_INIT_TIMEOUT);
2383 if (IS_ERR(skb)) {
2384 ret = PTR_ERR(skb);
2385 BT_ERR("%s: Change address command failed (%ld)",
2386 hdev->name, ret);
2387 return ret;
2388 }
2389 kfree_skb(skb);
2390
2391 return 0;
2392 }
2393
2394 #define QCA_DFU_PACKET_LEN 4096
2395
2396 #define QCA_GET_TARGET_VERSION 0x09
2397 #define QCA_CHECK_STATUS 0x05
2398 #define QCA_DFU_DOWNLOAD 0x01
2399
2400 #define QCA_SYSCFG_UPDATED 0x40
2401 #define QCA_PATCH_UPDATED 0x80
2402 #define QCA_DFU_TIMEOUT 3000
2403
2404 struct qca_version {
2405 __le32 rom_version;
2406 __le32 patch_version;
2407 __le32 ram_version;
2408 __le32 ref_clock;
2409 __u8 reserved[4];
2410 } __packed;
2411
2412 struct qca_rampatch_version {
2413 __le16 rom_version;
2414 __le16 patch_version;
2415 } __packed;
2416
2417 struct qca_device_info {
2418 u32 rom_version;
2419 u8 rampatch_hdr; /* length of header in rampatch */
2420 u8 nvm_hdr; /* length of header in NVM */
2421 u8 ver_offset; /* offset of version structure in rampatch */
2422 };
2423
2424 static const struct qca_device_info qca_devices_table[] = {
2425 { 0x00000100, 20, 4, 10 }, /* Rome 1.0 */
2426 { 0x00000101, 20, 4, 10 }, /* Rome 1.1 */
2427 { 0x00000200, 28, 4, 18 }, /* Rome 2.0 */
2428 { 0x00000201, 28, 4, 18 }, /* Rome 2.1 */
2429 { 0x00000300, 28, 4, 18 }, /* Rome 3.0 */
2430 { 0x00000302, 28, 4, 18 }, /* Rome 3.2 */
2431 };
2432
2433 static int btusb_qca_send_vendor_req(struct hci_dev *hdev, u8 request,
2434 void *data, u16 size)
2435 {
2436 struct btusb_data *btdata = hci_get_drvdata(hdev);
2437 struct usb_device *udev = btdata->udev;
2438 int pipe, err;
2439 u8 *buf;
2440
2441 buf = kmalloc(size, GFP_KERNEL);
2442 if (!buf)
2443 return -ENOMEM;
2444
2445 /* Found some of USB hosts have IOT issues with ours so that we should
2446 * not wait until HCI layer is ready.
2447 */
2448 pipe = usb_rcvctrlpipe(udev, 0);
2449 err = usb_control_msg(udev, pipe, request, USB_TYPE_VENDOR | USB_DIR_IN,
2450 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
2451 if (err < 0) {
2452 BT_ERR("%s: Failed to access otp area (%d)", hdev->name, err);
2453 goto done;
2454 }
2455
2456 memcpy(data, buf, size);
2457
2458 done:
2459 kfree(buf);
2460
2461 return err;
2462 }
2463
2464 static int btusb_setup_qca_download_fw(struct hci_dev *hdev,
2465 const struct firmware *firmware,
2466 size_t hdr_size)
2467 {
2468 struct btusb_data *btdata = hci_get_drvdata(hdev);
2469 struct usb_device *udev = btdata->udev;
2470 size_t count, size, sent = 0;
2471 int pipe, len, err;
2472 u8 *buf;
2473
2474 buf = kmalloc(QCA_DFU_PACKET_LEN, GFP_KERNEL);
2475 if (!buf)
2476 return -ENOMEM;
2477
2478 count = firmware->size;
2479
2480 size = min_t(size_t, count, hdr_size);
2481 memcpy(buf, firmware->data, size);
2482
2483 /* USB patches should go down to controller through USB path
2484 * because binary format fits to go down through USB channel.
2485 * USB control path is for patching headers and USB bulk is for
2486 * patch body.
2487 */
2488 pipe = usb_sndctrlpipe(udev, 0);
2489 err = usb_control_msg(udev, pipe, QCA_DFU_DOWNLOAD, USB_TYPE_VENDOR,
2490 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
2491 if (err < 0) {
2492 BT_ERR("%s: Failed to send headers (%d)", hdev->name, err);
2493 goto done;
2494 }
2495
2496 sent += size;
2497 count -= size;
2498
2499 while (count) {
2500 size = min_t(size_t, count, QCA_DFU_PACKET_LEN);
2501
2502 memcpy(buf, firmware->data + sent, size);
2503
2504 pipe = usb_sndbulkpipe(udev, 0x02);
2505 err = usb_bulk_msg(udev, pipe, buf, size, &len,
2506 QCA_DFU_TIMEOUT);
2507 if (err < 0) {
2508 BT_ERR("%s: Failed to send body at %zd of %zd (%d)",
2509 hdev->name, sent, firmware->size, err);
2510 break;
2511 }
2512
2513 if (size != len) {
2514 BT_ERR("%s: Failed to get bulk buffer", hdev->name);
2515 err = -EILSEQ;
2516 break;
2517 }
2518
2519 sent += size;
2520 count -= size;
2521 }
2522
2523 done:
2524 kfree(buf);
2525 return err;
2526 }
2527
2528 static int btusb_setup_qca_load_rampatch(struct hci_dev *hdev,
2529 struct qca_version *ver,
2530 const struct qca_device_info *info)
2531 {
2532 struct qca_rampatch_version *rver;
2533 const struct firmware *fw;
2534 u32 ver_rom, ver_patch;
2535 u16 rver_rom, rver_patch;
2536 char fwname[64];
2537 int err;
2538
2539 ver_rom = le32_to_cpu(ver->rom_version);
2540 ver_patch = le32_to_cpu(ver->patch_version);
2541
2542 snprintf(fwname, sizeof(fwname), "qca/rampatch_usb_%08x.bin", ver_rom);
2543
2544 err = request_firmware(&fw, fwname, &hdev->dev);
2545 if (err) {
2546 BT_ERR("%s: failed to request rampatch file: %s (%d)",
2547 hdev->name, fwname, err);
2548 return err;
2549 }
2550
2551 BT_INFO("%s: using rampatch file: %s", hdev->name, fwname);
2552
2553 rver = (struct qca_rampatch_version *)(fw->data + info->ver_offset);
2554 rver_rom = le16_to_cpu(rver->rom_version);
2555 rver_patch = le16_to_cpu(rver->patch_version);
2556
2557 BT_INFO("%s: QCA: patch rome 0x%x build 0x%x, firmware rome 0x%x "
2558 "build 0x%x", hdev->name, rver_rom, rver_patch, ver_rom,
2559 ver_patch);
2560
2561 if (rver_rom != ver_rom || rver_patch <= ver_patch) {
2562 BT_ERR("%s: rampatch file version did not match with firmware",
2563 hdev->name);
2564 err = -EINVAL;
2565 goto done;
2566 }
2567
2568 err = btusb_setup_qca_download_fw(hdev, fw, info->rampatch_hdr);
2569
2570 done:
2571 release_firmware(fw);
2572
2573 return err;
2574 }
2575
2576 static int btusb_setup_qca_load_nvm(struct hci_dev *hdev,
2577 struct qca_version *ver,
2578 const struct qca_device_info *info)
2579 {
2580 const struct firmware *fw;
2581 char fwname[64];
2582 int err;
2583
2584 snprintf(fwname, sizeof(fwname), "qca/nvm_usb_%08x.bin",
2585 le32_to_cpu(ver->rom_version));
2586
2587 err = request_firmware(&fw, fwname, &hdev->dev);
2588 if (err) {
2589 BT_ERR("%s: failed to request NVM file: %s (%d)",
2590 hdev->name, fwname, err);
2591 return err;
2592 }
2593
2594 BT_INFO("%s: using NVM file: %s", hdev->name, fwname);
2595
2596 err = btusb_setup_qca_download_fw(hdev, fw, info->nvm_hdr);
2597
2598 release_firmware(fw);
2599
2600 return err;
2601 }
2602
2603 static int btusb_setup_qca(struct hci_dev *hdev)
2604 {
2605 const struct qca_device_info *info = NULL;
2606 struct qca_version ver;
2607 u32 ver_rom;
2608 u8 status;
2609 int i, err;
2610
2611 err = btusb_qca_send_vendor_req(hdev, QCA_GET_TARGET_VERSION, &ver,
2612 sizeof(ver));
2613 if (err < 0)
2614 return err;
2615
2616 ver_rom = le32_to_cpu(ver.rom_version);
2617 for (i = 0; i < ARRAY_SIZE(qca_devices_table); i++) {
2618 if (ver_rom == qca_devices_table[i].rom_version)
2619 info = &qca_devices_table[i];
2620 }
2621 if (!info) {
2622 BT_ERR("%s: don't support firmware rome 0x%x", hdev->name,
2623 ver_rom);
2624 return -ENODEV;
2625 }
2626
2627 err = btusb_qca_send_vendor_req(hdev, QCA_CHECK_STATUS, &status,
2628 sizeof(status));
2629 if (err < 0)
2630 return err;
2631
2632 if (!(status & QCA_PATCH_UPDATED)) {
2633 err = btusb_setup_qca_load_rampatch(hdev, &ver, info);
2634 if (err < 0)
2635 return err;
2636 }
2637
2638 if (!(status & QCA_SYSCFG_UPDATED)) {
2639 err = btusb_setup_qca_load_nvm(hdev, &ver, info);
2640 if (err < 0)
2641 return err;
2642 }
2643
2644 return 0;
2645 }
2646
2647 #ifdef CONFIG_BT_HCIBTUSB_BCM
2648 static inline int __set_diag_interface(struct hci_dev *hdev)
2649 {
2650 struct btusb_data *data = hci_get_drvdata(hdev);
2651 struct usb_interface *intf = data->diag;
2652 int i;
2653
2654 if (!data->diag)
2655 return -ENODEV;
2656
2657 data->diag_tx_ep = NULL;
2658 data->diag_rx_ep = NULL;
2659
2660 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
2661 struct usb_endpoint_descriptor *ep_desc;
2662
2663 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
2664
2665 if (!data->diag_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
2666 data->diag_tx_ep = ep_desc;
2667 continue;
2668 }
2669
2670 if (!data->diag_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
2671 data->diag_rx_ep = ep_desc;
2672 continue;
2673 }
2674 }
2675
2676 if (!data->diag_tx_ep || !data->diag_rx_ep) {
2677 BT_ERR("%s invalid diagnostic descriptors", hdev->name);
2678 return -ENODEV;
2679 }
2680
2681 return 0;
2682 }
2683
2684 static struct urb *alloc_diag_urb(struct hci_dev *hdev, bool enable)
2685 {
2686 struct btusb_data *data = hci_get_drvdata(hdev);
2687 struct sk_buff *skb;
2688 struct urb *urb;
2689 unsigned int pipe;
2690
2691 if (!data->diag_tx_ep)
2692 return ERR_PTR(-ENODEV);
2693
2694 urb = usb_alloc_urb(0, GFP_KERNEL);
2695 if (!urb)
2696 return ERR_PTR(-ENOMEM);
2697
2698 skb = bt_skb_alloc(2, GFP_KERNEL);
2699 if (!skb) {
2700 usb_free_urb(urb);
2701 return ERR_PTR(-ENOMEM);
2702 }
2703
2704 *skb_put(skb, 1) = 0xf0;
2705 *skb_put(skb, 1) = enable;
2706
2707 pipe = usb_sndbulkpipe(data->udev, data->diag_tx_ep->bEndpointAddress);
2708
2709 usb_fill_bulk_urb(urb, data->udev, pipe,
2710 skb->data, skb->len, btusb_tx_complete, skb);
2711
2712 skb->dev = (void *)hdev;
2713
2714 return urb;
2715 }
2716
2717 static int btusb_bcm_set_diag(struct hci_dev *hdev, bool enable)
2718 {
2719 struct btusb_data *data = hci_get_drvdata(hdev);
2720 struct urb *urb;
2721
2722 if (!data->diag)
2723 return -ENODEV;
2724
2725 if (!test_bit(HCI_RUNNING, &hdev->flags))
2726 return -ENETDOWN;
2727
2728 urb = alloc_diag_urb(hdev, enable);
2729 if (IS_ERR(urb))
2730 return PTR_ERR(urb);
2731
2732 return submit_or_queue_tx_urb(hdev, urb);
2733 }
2734 #endif
2735
2736 static int btusb_probe(struct usb_interface *intf,
2737 const struct usb_device_id *id)
2738 {
2739 struct usb_endpoint_descriptor *ep_desc;
2740 struct btusb_data *data;
2741 struct hci_dev *hdev;
2742 unsigned ifnum_base;
2743 int i, err;
2744
2745 BT_DBG("intf %p id %p", intf, id);
2746
2747 /* interface numbers are hardcoded in the spec */
2748 if (intf->cur_altsetting->desc.bInterfaceNumber != 0) {
2749 if (!(id->driver_info & BTUSB_IFNUM_2))
2750 return -ENODEV;
2751 if (intf->cur_altsetting->desc.bInterfaceNumber != 2)
2752 return -ENODEV;
2753 }
2754
2755 ifnum_base = intf->cur_altsetting->desc.bInterfaceNumber;
2756
2757 if (!id->driver_info) {
2758 const struct usb_device_id *match;
2759
2760 match = usb_match_id(intf, blacklist_table);
2761 if (match)
2762 id = match;
2763 }
2764
2765 if (id->driver_info == BTUSB_IGNORE)
2766 return -ENODEV;
2767
2768 if (id->driver_info & BTUSB_ATH3012) {
2769 struct usb_device *udev = interface_to_usbdev(intf);
2770
2771 /* Old firmware would otherwise let ath3k driver load
2772 * patch and sysconfig files */
2773 if (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x0001)
2774 return -ENODEV;
2775 }
2776
2777 data = devm_kzalloc(&intf->dev, sizeof(*data), GFP_KERNEL);
2778 if (!data)
2779 return -ENOMEM;
2780
2781 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
2782 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
2783
2784 if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
2785 data->intr_ep = ep_desc;
2786 continue;
2787 }
2788
2789 if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
2790 data->bulk_tx_ep = ep_desc;
2791 continue;
2792 }
2793
2794 if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
2795 data->bulk_rx_ep = ep_desc;
2796 continue;
2797 }
2798 }
2799
2800 if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep)
2801 return -ENODEV;
2802
2803 if (id->driver_info & BTUSB_AMP) {
2804 data->cmdreq_type = USB_TYPE_CLASS | 0x01;
2805 data->cmdreq = 0x2b;
2806 } else {
2807 data->cmdreq_type = USB_TYPE_CLASS;
2808 data->cmdreq = 0x00;
2809 }
2810
2811 data->udev = interface_to_usbdev(intf);
2812 data->intf = intf;
2813
2814 INIT_WORK(&data->work, btusb_work);
2815 INIT_WORK(&data->waker, btusb_waker);
2816 init_usb_anchor(&data->deferred);
2817 init_usb_anchor(&data->tx_anchor);
2818 spin_lock_init(&data->txlock);
2819
2820 init_usb_anchor(&data->intr_anchor);
2821 init_usb_anchor(&data->bulk_anchor);
2822 init_usb_anchor(&data->isoc_anchor);
2823 init_usb_anchor(&data->diag_anchor);
2824 spin_lock_init(&data->rxlock);
2825
2826 if (id->driver_info & BTUSB_INTEL_NEW) {
2827 data->recv_event = btusb_recv_event_intel;
2828 data->recv_bulk = btusb_recv_bulk_intel;
2829 set_bit(BTUSB_BOOTLOADER, &data->flags);
2830 } else {
2831 data->recv_event = hci_recv_frame;
2832 data->recv_bulk = btusb_recv_bulk;
2833 }
2834
2835 hdev = hci_alloc_dev();
2836 if (!hdev)
2837 return -ENOMEM;
2838
2839 hdev->bus = HCI_USB;
2840 hci_set_drvdata(hdev, data);
2841
2842 if (id->driver_info & BTUSB_AMP)
2843 hdev->dev_type = HCI_AMP;
2844 else
2845 hdev->dev_type = HCI_PRIMARY;
2846
2847 data->hdev = hdev;
2848
2849 SET_HCIDEV_DEV(hdev, &intf->dev);
2850
2851 hdev->open = btusb_open;
2852 hdev->close = btusb_close;
2853 hdev->flush = btusb_flush;
2854 hdev->send = btusb_send_frame;
2855 hdev->notify = btusb_notify;
2856
2857 if (id->driver_info & BTUSB_CW6622)
2858 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
2859
2860 if (id->driver_info & BTUSB_BCM2045)
2861 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
2862
2863 if (id->driver_info & BTUSB_BCM92035)
2864 hdev->setup = btusb_setup_bcm92035;
2865
2866 #ifdef CONFIG_BT_HCIBTUSB_BCM
2867 if (id->driver_info & BTUSB_BCM_PATCHRAM) {
2868 hdev->manufacturer = 15;
2869 hdev->setup = btbcm_setup_patchram;
2870 hdev->set_diag = btusb_bcm_set_diag;
2871 hdev->set_bdaddr = btbcm_set_bdaddr;
2872
2873 /* Broadcom LM_DIAG Interface numbers are hardcoded */
2874 data->diag = usb_ifnum_to_if(data->udev, ifnum_base + 2);
2875 }
2876
2877 if (id->driver_info & BTUSB_BCM_APPLE) {
2878 hdev->manufacturer = 15;
2879 hdev->setup = btbcm_setup_apple;
2880 hdev->set_diag = btusb_bcm_set_diag;
2881
2882 /* Broadcom LM_DIAG Interface numbers are hardcoded */
2883 data->diag = usb_ifnum_to_if(data->udev, ifnum_base + 2);
2884 }
2885 #endif
2886
2887 if (id->driver_info & BTUSB_INTEL) {
2888 hdev->manufacturer = 2;
2889 hdev->setup = btusb_setup_intel;
2890 hdev->shutdown = btusb_shutdown_intel;
2891 hdev->set_diag = btintel_set_diag_mfg;
2892 hdev->set_bdaddr = btintel_set_bdaddr;
2893 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
2894 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
2895 set_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks);
2896 }
2897
2898 if (id->driver_info & BTUSB_INTEL_NEW) {
2899 hdev->manufacturer = 2;
2900 hdev->send = btusb_send_frame_intel;
2901 hdev->setup = btusb_setup_intel_new;
2902 hdev->hw_error = btintel_hw_error;
2903 hdev->set_diag = btintel_set_diag;
2904 hdev->set_bdaddr = btintel_set_bdaddr;
2905 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
2906 set_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks);
2907 }
2908
2909 if (id->driver_info & BTUSB_MARVELL)
2910 hdev->set_bdaddr = btusb_set_bdaddr_marvell;
2911
2912 if (id->driver_info & BTUSB_SWAVE) {
2913 set_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks);
2914 set_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS, &hdev->quirks);
2915 }
2916
2917 if (id->driver_info & BTUSB_INTEL_BOOT) {
2918 hdev->manufacturer = 2;
2919 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
2920 }
2921
2922 if (id->driver_info & BTUSB_ATH3012) {
2923 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
2924 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
2925 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
2926 }
2927
2928 if (id->driver_info & BTUSB_QCA_ROME) {
2929 data->setup_on_usb = btusb_setup_qca;
2930 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
2931 }
2932
2933 #ifdef CONFIG_BT_HCIBTUSB_RTL
2934 if (id->driver_info & BTUSB_REALTEK) {
2935 hdev->setup = btrtl_setup_realtek;
2936
2937 /* Realtek devices lose their updated firmware over suspend,
2938 * but the USB hub doesn't notice any status change.
2939 * Explicitly request a device reset on resume.
2940 */
2941 set_bit(BTUSB_RESET_RESUME, &data->flags);
2942 }
2943 #endif
2944
2945 if (id->driver_info & BTUSB_AMP) {
2946 /* AMP controllers do not support SCO packets */
2947 data->isoc = NULL;
2948 } else {
2949 /* Interface orders are hardcoded in the specification */
2950 data->isoc = usb_ifnum_to_if(data->udev, ifnum_base + 1);
2951 }
2952
2953 if (!reset)
2954 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
2955
2956 if (force_scofix || id->driver_info & BTUSB_WRONG_SCO_MTU) {
2957 if (!disable_scofix)
2958 set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
2959 }
2960
2961 if (id->driver_info & BTUSB_BROKEN_ISOC)
2962 data->isoc = NULL;
2963
2964 if (id->driver_info & BTUSB_DIGIANSWER) {
2965 data->cmdreq_type = USB_TYPE_VENDOR;
2966 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
2967 }
2968
2969 if (id->driver_info & BTUSB_CSR) {
2970 struct usb_device *udev = data->udev;
2971 u16 bcdDevice = le16_to_cpu(udev->descriptor.bcdDevice);
2972
2973 /* Old firmware would otherwise execute USB reset */
2974 if (bcdDevice < 0x117)
2975 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
2976
2977 /* Fake CSR devices with broken commands */
2978 if (bcdDevice <= 0x100 || bcdDevice == 0x134)
2979 hdev->setup = btusb_setup_csr;
2980
2981 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
2982 }
2983
2984 if (id->driver_info & BTUSB_SNIFFER) {
2985 struct usb_device *udev = data->udev;
2986
2987 /* New sniffer firmware has crippled HCI interface */
2988 if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
2989 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
2990 }
2991
2992 if (id->driver_info & BTUSB_INTEL_BOOT) {
2993 /* A bug in the bootloader causes that interrupt interface is
2994 * only enabled after receiving SetInterface(0, AltSetting=0).
2995 */
2996 err = usb_set_interface(data->udev, 0, 0);
2997 if (err < 0) {
2998 BT_ERR("failed to set interface 0, alt 0 %d", err);
2999 hci_free_dev(hdev);
3000 return err;
3001 }
3002 }
3003
3004 if (data->isoc) {
3005 err = usb_driver_claim_interface(&btusb_driver,
3006 data->isoc, data);
3007 if (err < 0) {
3008 hci_free_dev(hdev);
3009 return err;
3010 }
3011 }
3012
3013 #ifdef CONFIG_BT_HCIBTUSB_BCM
3014 if (data->diag) {
3015 if (!usb_driver_claim_interface(&btusb_driver,
3016 data->diag, data))
3017 __set_diag_interface(hdev);
3018 else
3019 data->diag = NULL;
3020 }
3021 #endif
3022
3023 err = hci_register_dev(hdev);
3024 if (err < 0) {
3025 hci_free_dev(hdev);
3026 return err;
3027 }
3028
3029 usb_set_intfdata(intf, data);
3030
3031 return 0;
3032 }
3033
3034 static void btusb_disconnect(struct usb_interface *intf)
3035 {
3036 struct btusb_data *data = usb_get_intfdata(intf);
3037 struct hci_dev *hdev;
3038
3039 BT_DBG("intf %p", intf);
3040
3041 if (!data)
3042 return;
3043
3044 hdev = data->hdev;
3045 usb_set_intfdata(data->intf, NULL);
3046
3047 if (data->isoc)
3048 usb_set_intfdata(data->isoc, NULL);
3049
3050 if (data->diag)
3051 usb_set_intfdata(data->diag, NULL);
3052
3053 hci_unregister_dev(hdev);
3054
3055 if (intf == data->intf) {
3056 if (data->isoc)
3057 usb_driver_release_interface(&btusb_driver, data->isoc);
3058 if (data->diag)
3059 usb_driver_release_interface(&btusb_driver, data->diag);
3060 } else if (intf == data->isoc) {
3061 if (data->diag)
3062 usb_driver_release_interface(&btusb_driver, data->diag);
3063 usb_driver_release_interface(&btusb_driver, data->intf);
3064 } else if (intf == data->diag) {
3065 usb_driver_release_interface(&btusb_driver, data->intf);
3066 if (data->isoc)
3067 usb_driver_release_interface(&btusb_driver, data->isoc);
3068 }
3069
3070 hci_free_dev(hdev);
3071 }
3072
3073 #ifdef CONFIG_PM
3074 static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
3075 {
3076 struct btusb_data *data = usb_get_intfdata(intf);
3077
3078 BT_DBG("intf %p", intf);
3079
3080 if (data->suspend_count++)
3081 return 0;
3082
3083 spin_lock_irq(&data->txlock);
3084 if (!(PMSG_IS_AUTO(message) && data->tx_in_flight)) {
3085 set_bit(BTUSB_SUSPENDING, &data->flags);
3086 spin_unlock_irq(&data->txlock);
3087 } else {
3088 spin_unlock_irq(&data->txlock);
3089 data->suspend_count--;
3090 return -EBUSY;
3091 }
3092
3093 cancel_work_sync(&data->work);
3094
3095 btusb_stop_traffic(data);
3096 usb_kill_anchored_urbs(&data->tx_anchor);
3097
3098 /* Optionally request a device reset on resume, but only when
3099 * wakeups are disabled. If wakeups are enabled we assume the
3100 * device will stay powered up throughout suspend.
3101 */
3102 if (test_bit(BTUSB_RESET_RESUME, &data->flags) &&
3103 !device_may_wakeup(&data->udev->dev))
3104 data->udev->reset_resume = 1;
3105
3106 return 0;
3107 }
3108
3109 static void play_deferred(struct btusb_data *data)
3110 {
3111 struct urb *urb;
3112 int err;
3113
3114 while ((urb = usb_get_from_anchor(&data->deferred))) {
3115 err = usb_submit_urb(urb, GFP_ATOMIC);
3116 if (err < 0)
3117 break;
3118
3119 data->tx_in_flight++;
3120 }
3121 usb_scuttle_anchored_urbs(&data->deferred);
3122 }
3123
3124 static int btusb_resume(struct usb_interface *intf)
3125 {
3126 struct btusb_data *data = usb_get_intfdata(intf);
3127 struct hci_dev *hdev = data->hdev;
3128 int err = 0;
3129
3130 BT_DBG("intf %p", intf);
3131
3132 if (--data->suspend_count)
3133 return 0;
3134
3135 if (!test_bit(HCI_RUNNING, &hdev->flags))
3136 goto done;
3137
3138 if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
3139 err = btusb_submit_intr_urb(hdev, GFP_NOIO);
3140 if (err < 0) {
3141 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
3142 goto failed;
3143 }
3144 }
3145
3146 if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) {
3147 err = btusb_submit_bulk_urb(hdev, GFP_NOIO);
3148 if (err < 0) {
3149 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
3150 goto failed;
3151 }
3152
3153 btusb_submit_bulk_urb(hdev, GFP_NOIO);
3154 }
3155
3156 if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
3157 if (btusb_submit_isoc_urb(hdev, GFP_NOIO) < 0)
3158 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
3159 else
3160 btusb_submit_isoc_urb(hdev, GFP_NOIO);
3161 }
3162
3163 spin_lock_irq(&data->txlock);
3164 play_deferred(data);
3165 clear_bit(BTUSB_SUSPENDING, &data->flags);
3166 spin_unlock_irq(&data->txlock);
3167 schedule_work(&data->work);
3168
3169 return 0;
3170
3171 failed:
3172 usb_scuttle_anchored_urbs(&data->deferred);
3173 done:
3174 spin_lock_irq(&data->txlock);
3175 clear_bit(BTUSB_SUSPENDING, &data->flags);
3176 spin_unlock_irq(&data->txlock);
3177
3178 return err;
3179 }
3180 #endif
3181
3182 static struct usb_driver btusb_driver = {
3183 .name = "btusb",
3184 .probe = btusb_probe,
3185 .disconnect = btusb_disconnect,
3186 #ifdef CONFIG_PM
3187 .suspend = btusb_suspend,
3188 .resume = btusb_resume,
3189 #endif
3190 .id_table = btusb_table,
3191 .supports_autosuspend = 1,
3192 .disable_hub_initiated_lpm = 1,
3193 };
3194
3195 module_usb_driver(btusb_driver);
3196
3197 module_param(disable_scofix, bool, 0644);
3198 MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
3199
3200 module_param(force_scofix, bool, 0644);
3201 MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
3202
3203 module_param(reset, bool, 0644);
3204 MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
3205
3206 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
3207 MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
3208 MODULE_VERSION(VERSION);
3209 MODULE_LICENSE("GPL");