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