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