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