Version:  2.0.40 2.2.26 2.4.37 3.12 3.13 3.14 3.15 3.16 3.17 3.18 3.19 4.0 4.1 4.2 4.3 4.4 4.5 4.6 4.7 4.8 4.9

Linux/drivers/bluetooth/btusb.c

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

This page was automatically generated by LXR 0.3.1 (source).  •  Linux is a registered trademark of Linus Torvalds  •  Contact us