hc
2024-02-20 102a0743326a03cd1a1202ceda21e175b7d3575c
kernel/drivers/bluetooth/btusb.c
....@@ -1,24 +1,9 @@
1
+// SPDX-License-Identifier: GPL-2.0-or-later
12 /*
23 *
34 * Generic Bluetooth USB driver
45 *
56 * 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
- *
227 */
238
249 #include <linux/dmi.h>
....@@ -26,9 +11,11 @@
2611 #include <linux/usb.h>
2712 #include <linux/usb/quirks.h>
2813 #include <linux/firmware.h>
14
+#include <linux/iopoll.h>
2915 #include <linux/of_device.h>
3016 #include <linux/of_irq.h>
3117 #include <linux/suspend.h>
18
+#include <linux/gpio/consumer.h>
3219 #include <asm/unaligned.h>
3320
3421 #include <net/bluetooth/bluetooth.h>
....@@ -69,6 +56,10 @@
6956 #define BTUSB_BCM2045 0x40000
7057 #define BTUSB_IFNUM_2 0x80000
7158 #define BTUSB_CW6622 0x100000
59
+#define BTUSB_MEDIATEK 0x200000
60
+#define BTUSB_WIDEBAND_SPEECH 0x400000
61
+#define BTUSB_VALID_LE_STATES 0x800000
62
+#define BTUSB_QCA_WCN6855 0x1000000
7263
7364 static const struct usb_device_id btusb_table[] = {
7465 /* Generic Bluetooth USB device */
....@@ -264,22 +255,48 @@
264255 { USB_DEVICE(0x0489, 0xe03c), .driver_info = BTUSB_ATH3012 },
265256
266257 /* QCA ROME chipset */
267
- { USB_DEVICE(0x0cf3, 0xe007), .driver_info = BTUSB_QCA_ROME },
268
- { USB_DEVICE(0x0cf3, 0xe009), .driver_info = BTUSB_QCA_ROME },
269
- { USB_DEVICE(0x0cf3, 0xe010), .driver_info = BTUSB_QCA_ROME },
270
- { USB_DEVICE(0x0cf3, 0xe300), .driver_info = BTUSB_QCA_ROME },
271
- { USB_DEVICE(0x0cf3, 0xe301), .driver_info = BTUSB_QCA_ROME },
272
- { USB_DEVICE(0x0cf3, 0xe360), .driver_info = BTUSB_QCA_ROME },
273
- { USB_DEVICE(0x0489, 0xe092), .driver_info = BTUSB_QCA_ROME },
274
- { USB_DEVICE(0x0489, 0xe09f), .driver_info = BTUSB_QCA_ROME },
275
- { USB_DEVICE(0x0489, 0xe0a2), .driver_info = BTUSB_QCA_ROME },
276
- { USB_DEVICE(0x04ca, 0x3011), .driver_info = BTUSB_QCA_ROME },
277
- { USB_DEVICE(0x04ca, 0x3015), .driver_info = BTUSB_QCA_ROME },
278
- { USB_DEVICE(0x04ca, 0x3016), .driver_info = BTUSB_QCA_ROME },
279
- { USB_DEVICE(0x04ca, 0x301a), .driver_info = BTUSB_QCA_ROME },
280
- { USB_DEVICE(0x13d3, 0x3491), .driver_info = BTUSB_QCA_ROME },
281
- { USB_DEVICE(0x13d3, 0x3496), .driver_info = BTUSB_QCA_ROME },
282
- { USB_DEVICE(0x13d3, 0x3501), .driver_info = BTUSB_QCA_ROME },
258
+ { USB_DEVICE(0x0cf3, 0x535b), .driver_info = BTUSB_QCA_ROME |
259
+ BTUSB_WIDEBAND_SPEECH },
260
+ { USB_DEVICE(0x0cf3, 0xe007), .driver_info = BTUSB_QCA_ROME |
261
+ BTUSB_WIDEBAND_SPEECH },
262
+ { USB_DEVICE(0x0cf3, 0xe009), .driver_info = BTUSB_QCA_ROME |
263
+ BTUSB_WIDEBAND_SPEECH },
264
+ { USB_DEVICE(0x0cf3, 0xe010), .driver_info = BTUSB_QCA_ROME |
265
+ BTUSB_WIDEBAND_SPEECH },
266
+ { USB_DEVICE(0x0cf3, 0xe300), .driver_info = BTUSB_QCA_ROME |
267
+ BTUSB_WIDEBAND_SPEECH },
268
+ { USB_DEVICE(0x0cf3, 0xe301), .driver_info = BTUSB_QCA_ROME |
269
+ BTUSB_WIDEBAND_SPEECH },
270
+ { USB_DEVICE(0x0cf3, 0xe360), .driver_info = BTUSB_QCA_ROME |
271
+ BTUSB_WIDEBAND_SPEECH },
272
+ { USB_DEVICE(0x0cf3, 0xe500), .driver_info = BTUSB_QCA_ROME |
273
+ BTUSB_WIDEBAND_SPEECH },
274
+ { USB_DEVICE(0x0489, 0xe092), .driver_info = BTUSB_QCA_ROME |
275
+ BTUSB_WIDEBAND_SPEECH },
276
+ { USB_DEVICE(0x0489, 0xe09f), .driver_info = BTUSB_QCA_ROME |
277
+ BTUSB_WIDEBAND_SPEECH },
278
+ { USB_DEVICE(0x0489, 0xe0a2), .driver_info = BTUSB_QCA_ROME |
279
+ BTUSB_WIDEBAND_SPEECH },
280
+ { USB_DEVICE(0x04ca, 0x3011), .driver_info = BTUSB_QCA_ROME |
281
+ BTUSB_WIDEBAND_SPEECH },
282
+ { USB_DEVICE(0x04ca, 0x3015), .driver_info = BTUSB_QCA_ROME |
283
+ BTUSB_WIDEBAND_SPEECH },
284
+ { USB_DEVICE(0x04ca, 0x3016), .driver_info = BTUSB_QCA_ROME |
285
+ BTUSB_WIDEBAND_SPEECH },
286
+ { USB_DEVICE(0x04ca, 0x301a), .driver_info = BTUSB_QCA_ROME |
287
+ BTUSB_WIDEBAND_SPEECH },
288
+ { USB_DEVICE(0x04ca, 0x3021), .driver_info = BTUSB_QCA_ROME |
289
+ BTUSB_WIDEBAND_SPEECH },
290
+ { USB_DEVICE(0x13d3, 0x3491), .driver_info = BTUSB_QCA_ROME |
291
+ BTUSB_WIDEBAND_SPEECH },
292
+ { USB_DEVICE(0x13d3, 0x3496), .driver_info = BTUSB_QCA_ROME |
293
+ BTUSB_WIDEBAND_SPEECH },
294
+ { USB_DEVICE(0x13d3, 0x3501), .driver_info = BTUSB_QCA_ROME |
295
+ BTUSB_WIDEBAND_SPEECH },
296
+
297
+ /* QCA WCN6855 chipset */
298
+ { USB_DEVICE(0x0cf3, 0xe600), .driver_info = BTUSB_QCA_WCN6855 |
299
+ BTUSB_WIDEBAND_SPEECH },
283300
284301 /* Broadcom BCM2035 */
285302 { USB_DEVICE(0x0a5c, 0x2009), .driver_info = BTUSB_BCM92035 },
....@@ -343,23 +360,84 @@
343360 { USB_DEVICE(0x1286, 0x204e), .driver_info = BTUSB_MARVELL },
344361
345362 /* Intel Bluetooth devices */
346
- { USB_DEVICE(0x8087, 0x0025), .driver_info = BTUSB_INTEL_NEW },
347
- { USB_DEVICE(0x8087, 0x0026), .driver_info = BTUSB_INTEL_NEW },
348
- { USB_DEVICE(0x8087, 0x0029), .driver_info = BTUSB_INTEL_NEW },
363
+ { USB_DEVICE(0x8087, 0x0025), .driver_info = BTUSB_INTEL_NEW |
364
+ BTUSB_WIDEBAND_SPEECH |
365
+ BTUSB_VALID_LE_STATES },
366
+ { USB_DEVICE(0x8087, 0x0026), .driver_info = BTUSB_INTEL_NEW |
367
+ BTUSB_WIDEBAND_SPEECH },
368
+ { USB_DEVICE(0x8087, 0x0029), .driver_info = BTUSB_INTEL_NEW |
369
+ BTUSB_WIDEBAND_SPEECH },
370
+ { USB_DEVICE(0x8087, 0x0032), .driver_info = BTUSB_INTEL_NEW |
371
+ BTUSB_WIDEBAND_SPEECH},
349372 { USB_DEVICE(0x8087, 0x07da), .driver_info = BTUSB_CSR },
350373 { USB_DEVICE(0x8087, 0x07dc), .driver_info = BTUSB_INTEL },
351374 { USB_DEVICE(0x8087, 0x0a2a), .driver_info = BTUSB_INTEL },
352
- { USB_DEVICE(0x8087, 0x0a2b), .driver_info = BTUSB_INTEL_NEW },
353
- { USB_DEVICE(0x8087, 0x0aa7), .driver_info = BTUSB_INTEL },
354
- { USB_DEVICE(0x8087, 0x0aaa), .driver_info = BTUSB_INTEL_NEW },
375
+ { USB_DEVICE(0x8087, 0x0a2b), .driver_info = BTUSB_INTEL_NEW |
376
+ BTUSB_WIDEBAND_SPEECH },
377
+ { USB_DEVICE(0x8087, 0x0aa7), .driver_info = BTUSB_INTEL |
378
+ BTUSB_WIDEBAND_SPEECH },
379
+ { USB_DEVICE(0x8087, 0x0aaa), .driver_info = BTUSB_INTEL_NEW |
380
+ BTUSB_WIDEBAND_SPEECH |
381
+ BTUSB_VALID_LE_STATES },
382
+ { USB_DEVICE(0x10ab, 0x9309), .driver_info = BTUSB_QCA_WCN6855 |
383
+ BTUSB_WIDEBAND_SPEECH |
384
+ BTUSB_VALID_LE_STATES },
385
+ { USB_DEVICE(0x10ab, 0x9409), .driver_info = BTUSB_QCA_WCN6855 |
386
+ BTUSB_WIDEBAND_SPEECH |
387
+ BTUSB_VALID_LE_STATES },
388
+ { USB_DEVICE(0x0489, 0xe0d0), .driver_info = BTUSB_QCA_WCN6855 |
389
+ BTUSB_WIDEBAND_SPEECH |
390
+ BTUSB_VALID_LE_STATES },
355391
356392 /* Other Intel Bluetooth devices */
357393 { USB_VENDOR_AND_INTERFACE_INFO(0x8087, 0xe0, 0x01, 0x01),
358394 .driver_info = BTUSB_IGNORE },
359395
396
+ /* Realtek 8821CE Bluetooth devices */
397
+ { USB_DEVICE(0x13d3, 0x3529), .driver_info = BTUSB_REALTEK |
398
+ BTUSB_WIDEBAND_SPEECH },
399
+
400
+ /* Realtek 8822CE Bluetooth devices */
401
+ { USB_DEVICE(0x0bda, 0xb00c), .driver_info = BTUSB_REALTEK |
402
+ BTUSB_WIDEBAND_SPEECH },
403
+ { USB_DEVICE(0x0bda, 0xc822), .driver_info = BTUSB_REALTEK |
404
+ BTUSB_WIDEBAND_SPEECH },
405
+
406
+ /* Realtek 8852CE Bluetooth devices */
407
+ { USB_DEVICE(0x04ca, 0x4007), .driver_info = BTUSB_REALTEK |
408
+ BTUSB_WIDEBAND_SPEECH },
409
+ { USB_DEVICE(0x04c5, 0x1675), .driver_info = BTUSB_REALTEK |
410
+ BTUSB_WIDEBAND_SPEECH },
411
+ { USB_DEVICE(0x0cb8, 0xc558), .driver_info = BTUSB_REALTEK |
412
+ BTUSB_WIDEBAND_SPEECH },
413
+ { USB_DEVICE(0x13d3, 0x3587), .driver_info = BTUSB_REALTEK |
414
+ BTUSB_WIDEBAND_SPEECH },
415
+ { USB_DEVICE(0x13d3, 0x3586), .driver_info = BTUSB_REALTEK |
416
+ BTUSB_WIDEBAND_SPEECH },
417
+
360418 /* Realtek Bluetooth devices */
361419 { USB_VENDOR_AND_INTERFACE_INFO(0x0bda, 0xe0, 0x01, 0x01),
362420 .driver_info = BTUSB_REALTEK },
421
+
422
+ /* MediaTek Bluetooth devices */
423
+ { USB_VENDOR_AND_INTERFACE_INFO(0x0e8d, 0xe0, 0x01, 0x01),
424
+ .driver_info = BTUSB_MEDIATEK |
425
+ BTUSB_WIDEBAND_SPEECH |
426
+ BTUSB_VALID_LE_STATES },
427
+
428
+ /* MediaTek MT7922A Bluetooth devices */
429
+ { USB_DEVICE(0x0489, 0xe0d8), .driver_info = BTUSB_MEDIATEK |
430
+ BTUSB_WIDEBAND_SPEECH |
431
+ BTUSB_VALID_LE_STATES },
432
+ { USB_DEVICE(0x0489, 0xe0d9), .driver_info = BTUSB_MEDIATEK |
433
+ BTUSB_WIDEBAND_SPEECH |
434
+ BTUSB_VALID_LE_STATES },
435
+ { USB_DEVICE(0x0489, 0xe0f5), .driver_info = BTUSB_MEDIATEK |
436
+ BTUSB_WIDEBAND_SPEECH |
437
+ BTUSB_VALID_LE_STATES },
438
+ { USB_DEVICE(0x13d3, 0x3568), .driver_info = BTUSB_MEDIATEK |
439
+ BTUSB_WIDEBAND_SPEECH |
440
+ BTUSB_VALID_LE_STATES },
363441
364442 /* Additional Realtek 8723AE Bluetooth devices */
365443 { USB_DEVICE(0x0930, 0x021d), .driver_info = BTUSB_REALTEK },
....@@ -393,6 +471,7 @@
393471
394472 /* Additional Realtek 8822CE Bluetooth devices */
395473 { USB_DEVICE(0x04ca, 0x4005), .driver_info = BTUSB_REALTEK },
474
+ { USB_DEVICE(0x13d3, 0x3548), .driver_info = BTUSB_REALTEK },
396475
397476 /* Silicon Wave based devices */
398477 { USB_DEVICE(0x0c10, 0x0000), .driver_info = BTUSB_SWAVE },
....@@ -443,6 +522,10 @@
443522 #define BTUSB_BOOTING 9
444523 #define BTUSB_DIAG_RUNNING 10
445524 #define BTUSB_OOB_WAKE_ENABLED 11
525
+#define BTUSB_HW_RESET_ACTIVE 12
526
+#define BTUSB_TX_WAIT_VND_EVT 13
527
+#define BTUSB_WAKEUP_DISABLE 14
528
+#define BTUSB_USE_ALT3_FOR_WBS 15
446529
447530 struct btusb_data {
448531 struct hci_dev *hdev;
....@@ -466,6 +549,7 @@
466549 struct usb_anchor bulk_anchor;
467550 struct usb_anchor isoc_anchor;
468551 struct usb_anchor diag_anchor;
552
+ struct usb_anchor ctrl_anchor;
469553 spinlock_t rxlock;
470554
471555 struct sk_buff *evt_skb;
....@@ -480,10 +564,14 @@
480564 struct usb_endpoint_descriptor *diag_tx_ep;
481565 struct usb_endpoint_descriptor *diag_rx_ep;
482566
567
+ struct gpio_desc *reset_gpio;
568
+
483569 __u8 cmdreq_type;
484570 __u8 cmdreq;
485571
486572 unsigned int sco_num;
573
+ unsigned int air_mode;
574
+ bool usb_alt6_packet_flow;
487575 int isoc_altsetting;
488576 int suspend_count;
489577
....@@ -493,7 +581,86 @@
493581 int (*setup_on_usb)(struct hci_dev *hdev);
494582
495583 int oob_wake_irq; /* irq for out-of-band wake-on-bt */
584
+ unsigned cmd_timeout_cnt;
496585 };
586
+
587
+static void btusb_intel_cmd_timeout(struct hci_dev *hdev)
588
+{
589
+ struct btusb_data *data = hci_get_drvdata(hdev);
590
+ struct gpio_desc *reset_gpio = data->reset_gpio;
591
+
592
+ if (++data->cmd_timeout_cnt < 5)
593
+ return;
594
+
595
+ if (!reset_gpio) {
596
+ bt_dev_err(hdev, "No way to reset. Ignoring and continuing");
597
+ return;
598
+ }
599
+
600
+ /*
601
+ * Toggle the hard reset line if the platform provides one. The reset
602
+ * is going to yank the device off the USB and then replug. So doing
603
+ * once is enough. The cleanup is handled correctly on the way out
604
+ * (standard USB disconnect), and the new device is detected cleanly
605
+ * and bound to the driver again like it should be.
606
+ */
607
+ if (test_and_set_bit(BTUSB_HW_RESET_ACTIVE, &data->flags)) {
608
+ bt_dev_err(hdev, "last reset failed? Not resetting again");
609
+ return;
610
+ }
611
+
612
+ bt_dev_err(hdev, "Initiating HW reset via gpio");
613
+ gpiod_set_value_cansleep(reset_gpio, 1);
614
+ msleep(100);
615
+ gpiod_set_value_cansleep(reset_gpio, 0);
616
+}
617
+
618
+static void btusb_rtl_cmd_timeout(struct hci_dev *hdev)
619
+{
620
+ struct btusb_data *data = hci_get_drvdata(hdev);
621
+ struct gpio_desc *reset_gpio = data->reset_gpio;
622
+
623
+ if (++data->cmd_timeout_cnt < 5)
624
+ return;
625
+
626
+ if (!reset_gpio) {
627
+ bt_dev_err(hdev, "No gpio to reset Realtek device, ignoring");
628
+ return;
629
+ }
630
+
631
+ /* Toggle the hard reset line. The Realtek device is going to
632
+ * yank itself off the USB and then replug. The cleanup is handled
633
+ * correctly on the way out (standard USB disconnect), and the new
634
+ * device is detected cleanly and bound to the driver again like
635
+ * it should be.
636
+ */
637
+ if (test_and_set_bit(BTUSB_HW_RESET_ACTIVE, &data->flags)) {
638
+ bt_dev_err(hdev, "last reset failed? Not resetting again");
639
+ return;
640
+ }
641
+
642
+ bt_dev_err(hdev, "Reset Realtek device via gpio");
643
+ gpiod_set_value_cansleep(reset_gpio, 1);
644
+ msleep(200);
645
+ gpiod_set_value_cansleep(reset_gpio, 0);
646
+}
647
+
648
+static void btusb_qca_cmd_timeout(struct hci_dev *hdev)
649
+{
650
+ struct btusb_data *data = hci_get_drvdata(hdev);
651
+ int err;
652
+
653
+ if (++data->cmd_timeout_cnt < 5)
654
+ return;
655
+
656
+ bt_dev_err(hdev, "Multiple cmd timeouts seen. Resetting usb device.");
657
+ /* This is not an unbalanced PM reference since the device will reset */
658
+ err = usb_autopm_get_interface(data->intf);
659
+ if (!err)
660
+ usb_queue_reset_device(data->intf);
661
+ else
662
+ bt_dev_err(hdev, "Failed usb_autopm_get_interface with %d", err);
663
+}
497664
498665 static inline void btusb_free_frags(struct btusb_data *data)
499666 {
....@@ -501,13 +668,13 @@
501668
502669 spin_lock_irqsave(&data->rxlock, flags);
503670
504
- kfree_skb(data->evt_skb);
671
+ dev_kfree_skb_irq(data->evt_skb);
505672 data->evt_skb = NULL;
506673
507
- kfree_skb(data->acl_skb);
674
+ dev_kfree_skb_irq(data->acl_skb);
508675 data->acl_skb = NULL;
509676
510
- kfree_skb(data->sco_skb);
677
+ dev_kfree_skb_irq(data->sco_skb);
511678 data->sco_skb = NULL;
512679
513680 spin_unlock_irqrestore(&data->rxlock, flags);
....@@ -912,6 +1079,42 @@
9121079 }
9131080 }
9141081
1082
+static inline void __fill_isoc_descriptor_msbc(struct urb *urb, int len,
1083
+ int mtu, struct btusb_data *data)
1084
+{
1085
+ int i, offset = 0;
1086
+ unsigned int interval;
1087
+
1088
+ BT_DBG("len %d mtu %d", len, mtu);
1089
+
1090
+ /* For mSBC ALT 6 setting the host will send the packet at continuous
1091
+ * flow. As per core spec 5, vol 4, part B, table 2.1. For ALT setting
1092
+ * 6 the HCI PACKET INTERVAL should be 7.5ms for every usb packets.
1093
+ * To maintain the rate we send 63bytes of usb packets alternatively for
1094
+ * 7ms and 8ms to maintain the rate as 7.5ms.
1095
+ */
1096
+ if (data->usb_alt6_packet_flow) {
1097
+ interval = 7;
1098
+ data->usb_alt6_packet_flow = false;
1099
+ } else {
1100
+ interval = 6;
1101
+ data->usb_alt6_packet_flow = true;
1102
+ }
1103
+
1104
+ for (i = 0; i < interval; i++) {
1105
+ urb->iso_frame_desc[i].offset = offset;
1106
+ urb->iso_frame_desc[i].length = offset;
1107
+ }
1108
+
1109
+ if (len && i < BTUSB_MAX_ISOC_FRAMES) {
1110
+ urb->iso_frame_desc[i].offset = offset;
1111
+ urb->iso_frame_desc[i].length = len;
1112
+ i++;
1113
+ }
1114
+
1115
+ urb->number_of_packets = i;
1116
+}
1117
+
9151118 static inline void __fill_isoc_descriptor(struct urb *urb, int len, int mtu)
9161119 {
9171120 int i, offset = 0;
....@@ -1143,6 +1346,13 @@
11431346
11441347 data->intf->needs_remote_wakeup = 1;
11451348
1349
+ /* Disable device remote wakeup when host is suspended
1350
+ * For Realtek chips, global suspend without
1351
+ * SET_FEATURE (DEVICE_REMOTE_WAKEUP) can save more power in device.
1352
+ */
1353
+ if (test_bit(BTUSB_WAKEUP_DISABLE, &data->flags))
1354
+ device_wakeup_disable(&data->udev->dev);
1355
+
11461356 if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
11471357 goto done;
11481358
....@@ -1181,6 +1391,7 @@
11811391 usb_kill_anchored_urbs(&data->bulk_anchor);
11821392 usb_kill_anchored_urbs(&data->isoc_anchor);
11831393 usb_kill_anchored_urbs(&data->diag_anchor);
1394
+ usb_kill_anchored_urbs(&data->ctrl_anchor);
11841395 }
11851396
11861397 static int btusb_close(struct hci_dev *hdev)
....@@ -1206,6 +1417,11 @@
12061417 goto failed;
12071418
12081419 data->intf->needs_remote_wakeup = 0;
1420
+
1421
+ /* Enable remote wake up for auto-suspend */
1422
+ if (test_bit(BTUSB_WAKEUP_DISABLE, &data->flags))
1423
+ data->intf->needs_remote_wakeup = 1;
1424
+
12091425 usb_autopm_put_interface(data->intf);
12101426
12111427 failed:
....@@ -1302,9 +1518,13 @@
13021518
13031519 urb->transfer_flags = URB_ISO_ASAP;
13041520
1305
- __fill_isoc_descriptor(urb, skb->len,
1306
- le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
1307
-
1521
+ if (data->isoc_altsetting == 6)
1522
+ __fill_isoc_descriptor_msbc(urb, skb->len,
1523
+ le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize),
1524
+ data);
1525
+ else
1526
+ __fill_isoc_descriptor(urb, skb->len,
1527
+ le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
13081528 skb->dev = (void *)hdev;
13091529
13101530 return urb;
....@@ -1400,6 +1620,7 @@
14001620
14011621 if (hci_conn_num(hdev, SCO_LINK) != data->sco_num) {
14021622 data->sco_num = hci_conn_num(hdev, SCO_LINK);
1623
+ data->air_mode = evt;
14031624 schedule_work(&data->work);
14041625 }
14051626 }
....@@ -1447,11 +1668,70 @@
14471668 return 0;
14481669 }
14491670
1671
+static int btusb_switch_alt_setting(struct hci_dev *hdev, int new_alts)
1672
+{
1673
+ struct btusb_data *data = hci_get_drvdata(hdev);
1674
+ int err;
1675
+
1676
+ if (data->isoc_altsetting != new_alts) {
1677
+ unsigned long flags;
1678
+
1679
+ clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1680
+ usb_kill_anchored_urbs(&data->isoc_anchor);
1681
+
1682
+ /* When isochronous alternate setting needs to be
1683
+ * changed, because SCO connection has been added
1684
+ * or removed, a packet fragment may be left in the
1685
+ * reassembling state. This could lead to wrongly
1686
+ * assembled fragments.
1687
+ *
1688
+ * Clear outstanding fragment when selecting a new
1689
+ * alternate setting.
1690
+ */
1691
+ spin_lock_irqsave(&data->rxlock, flags);
1692
+ dev_kfree_skb_irq(data->sco_skb);
1693
+ data->sco_skb = NULL;
1694
+ spin_unlock_irqrestore(&data->rxlock, flags);
1695
+
1696
+ err = __set_isoc_interface(hdev, new_alts);
1697
+ if (err < 0)
1698
+ return err;
1699
+ }
1700
+
1701
+ if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
1702
+ if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0)
1703
+ clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1704
+ else
1705
+ btusb_submit_isoc_urb(hdev, GFP_KERNEL);
1706
+ }
1707
+
1708
+ return 0;
1709
+}
1710
+
1711
+static struct usb_host_interface *btusb_find_altsetting(struct btusb_data *data,
1712
+ int alt)
1713
+{
1714
+ struct usb_interface *intf = data->isoc;
1715
+ int i;
1716
+
1717
+ BT_DBG("Looking for Alt no :%d", alt);
1718
+
1719
+ if (!intf)
1720
+ return NULL;
1721
+
1722
+ for (i = 0; i < intf->num_altsetting; i++) {
1723
+ if (intf->altsetting[i].desc.bAlternateSetting == alt)
1724
+ return &intf->altsetting[i];
1725
+ }
1726
+
1727
+ return NULL;
1728
+}
1729
+
14501730 static void btusb_work(struct work_struct *work)
14511731 {
14521732 struct btusb_data *data = container_of(work, struct btusb_data, work);
14531733 struct hci_dev *hdev = data->hdev;
1454
- int new_alts;
1734
+ int new_alts = 0;
14551735 int err;
14561736
14571737 if (data->sco_num > 0) {
....@@ -1466,44 +1746,36 @@
14661746 set_bit(BTUSB_DID_ISO_RESUME, &data->flags);
14671747 }
14681748
1469
- if (hdev->voice_setting & 0x0020) {
1470
- static const int alts[3] = { 2, 4, 5 };
1749
+ if (data->air_mode == HCI_NOTIFY_ENABLE_SCO_CVSD) {
1750
+ if (hdev->voice_setting & 0x0020) {
1751
+ static const int alts[3] = { 2, 4, 5 };
14711752
1472
- new_alts = alts[data->sco_num - 1];
1473
- } else {
1474
- new_alts = data->sco_num;
1475
- }
1476
-
1477
- if (data->isoc_altsetting != new_alts) {
1478
- unsigned long flags;
1479
-
1480
- clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1481
- usb_kill_anchored_urbs(&data->isoc_anchor);
1482
-
1483
- /* When isochronous alternate setting needs to be
1484
- * changed, because SCO connection has been added
1485
- * or removed, a packet fragment may be left in the
1486
- * reassembling state. This could lead to wrongly
1487
- * assembled fragments.
1488
- *
1489
- * Clear outstanding fragment when selecting a new
1490
- * alternate setting.
1753
+ new_alts = alts[data->sco_num - 1];
1754
+ } else {
1755
+ new_alts = data->sco_num;
1756
+ }
1757
+ } else if (data->air_mode == HCI_NOTIFY_ENABLE_SCO_TRANSP) {
1758
+ /* Bluetooth USB spec recommends alt 6 (63 bytes), but
1759
+ * many adapters do not support it. Alt 1 appears to
1760
+ * work for all adapters that do not have alt 6, and
1761
+ * which work with WBS at all. Some devices prefer
1762
+ * alt 3 (HCI payload >= 60 Bytes let air packet
1763
+ * data satisfy 60 bytes), requiring
1764
+ * MTU >= 3 (packets) * 25 (size) - 3 (headers) = 72
1765
+ * see also Core spec 5, vol 4, B 2.1.1 & Table 2.1.
14911766 */
1492
- spin_lock_irqsave(&data->rxlock, flags);
1493
- kfree_skb(data->sco_skb);
1494
- data->sco_skb = NULL;
1495
- spin_unlock_irqrestore(&data->rxlock, flags);
1496
-
1497
- if (__set_isoc_interface(hdev, new_alts) < 0)
1498
- return;
1499
- }
1500
-
1501
- if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
1502
- if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0)
1503
- clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1767
+ if (btusb_find_altsetting(data, 6))
1768
+ new_alts = 6;
1769
+ else if (btusb_find_altsetting(data, 3) &&
1770
+ hdev->sco_mtu >= 72 &&
1771
+ test_bit(BTUSB_USE_ALT3_FOR_WBS, &data->flags))
1772
+ new_alts = 3;
15041773 else
1505
- btusb_submit_isoc_urb(hdev, GFP_KERNEL);
1774
+ new_alts = 1;
15061775 }
1776
+
1777
+ if (btusb_switch_alt_setting(hdev, new_alts) < 0)
1778
+ bt_dev_err(hdev, "set USB alt:(%d) failed!", new_alts);
15071779 } else {
15081780 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
15091781 usb_kill_anchored_urbs(&data->isoc_anchor);
....@@ -1544,8 +1816,11 @@
15441816
15451817 static int btusb_setup_csr(struct hci_dev *hdev)
15461818 {
1819
+ struct btusb_data *data = hci_get_drvdata(hdev);
1820
+ u16 bcdDevice = le16_to_cpu(data->udev->descriptor.bcdDevice);
15471821 struct hci_rp_read_local_version *rp;
15481822 struct sk_buff *skb;
1823
+ bool is_fake = false;
15491824
15501825 BT_DBG("%s", hdev->name);
15511826
....@@ -1565,18 +1840,80 @@
15651840
15661841 rp = (struct hci_rp_read_local_version *)skb->data;
15671842
1568
- /* Detect controllers which aren't real CSR ones. */
1843
+ bt_dev_info(hdev, "CSR: Setting up dongle with HCI ver=%u rev=%04x; LMP ver=%u subver=%04x; manufacturer=%u",
1844
+ le16_to_cpu(rp->hci_ver), le16_to_cpu(rp->hci_rev),
1845
+ le16_to_cpu(rp->lmp_ver), le16_to_cpu(rp->lmp_subver),
1846
+ le16_to_cpu(rp->manufacturer));
1847
+
1848
+ /* Detect a wide host of Chinese controllers that aren't CSR.
1849
+ *
1850
+ * Known fake bcdDevices: 0x0100, 0x0134, 0x1915, 0x2520, 0x7558, 0x8891
1851
+ *
1852
+ * The main thing they have in common is that these are really popular low-cost
1853
+ * options that support newer Bluetooth versions but rely on heavy VID/PID
1854
+ * squatting of this poor old Bluetooth 1.1 device. Even sold as such.
1855
+ *
1856
+ * We detect actual CSR devices by checking that the HCI manufacturer code
1857
+ * is Cambridge Silicon Radio (10) and ensuring that LMP sub-version and
1858
+ * HCI rev values always match. As they both store the firmware number.
1859
+ */
15691860 if (le16_to_cpu(rp->manufacturer) != 10 ||
1570
- le16_to_cpu(rp->lmp_subver) == 0x0c5c) {
1861
+ le16_to_cpu(rp->hci_rev) != le16_to_cpu(rp->lmp_subver))
1862
+ is_fake = true;
1863
+
1864
+ /* Known legit CSR firmware build numbers and their supported BT versions:
1865
+ * - 1.1 (0x1) -> 0x0073, 0x020d, 0x033c, 0x034e
1866
+ * - 1.2 (0x2) -> 0x04d9, 0x0529
1867
+ * - 2.0 (0x3) -> 0x07a6, 0x07ad, 0x0c5c
1868
+ * - 2.1 (0x4) -> 0x149c, 0x1735, 0x1899 (0x1899 is a BlueCore4-External)
1869
+ * - 4.0 (0x6) -> 0x1d86, 0x2031, 0x22bb
1870
+ *
1871
+ * e.g. Real CSR dongles with LMP subversion 0x73 are old enough that
1872
+ * support BT 1.1 only; so it's a dead giveaway when some
1873
+ * third-party BT 4.0 dongle reuses it.
1874
+ */
1875
+ else if (le16_to_cpu(rp->lmp_subver) <= 0x034e &&
1876
+ le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_1_1)
1877
+ is_fake = true;
1878
+
1879
+ else if (le16_to_cpu(rp->lmp_subver) <= 0x0529 &&
1880
+ le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_1_2)
1881
+ is_fake = true;
1882
+
1883
+ else if (le16_to_cpu(rp->lmp_subver) <= 0x0c5c &&
1884
+ le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_2_0)
1885
+ is_fake = true;
1886
+
1887
+ else if (le16_to_cpu(rp->lmp_subver) <= 0x1899 &&
1888
+ le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_2_1)
1889
+ is_fake = true;
1890
+
1891
+ else if (le16_to_cpu(rp->lmp_subver) <= 0x22bb &&
1892
+ le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_4_0)
1893
+ is_fake = true;
1894
+
1895
+ /* Other clones which beat all the above checks */
1896
+ else if (bcdDevice == 0x0134 &&
1897
+ le16_to_cpu(rp->lmp_subver) == 0x0c5c &&
1898
+ le16_to_cpu(rp->hci_ver) == BLUETOOTH_VER_2_0)
1899
+ is_fake = true;
1900
+
1901
+ if (is_fake) {
1902
+ bt_dev_warn(hdev, "CSR: Unbranded CSR clone detected; adding workarounds...");
1903
+
1904
+ /* Generally these clones have big discrepancies between
1905
+ * advertised features and what's actually supported.
1906
+ * Probably will need to be expanded in the future;
1907
+ * without these the controller will lock up.
1908
+ */
1909
+ set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
1910
+ set_bit(HCI_QUIRK_BROKEN_ERR_DATA_REPORTING, &hdev->quirks);
1911
+
15711912 /* Clear the reset quirk since this is not an actual
15721913 * early Bluetooth 1.1 device from CSR.
15731914 */
15741915 clear_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
1575
-
1576
- /* These fake CSR controllers have all a broken
1577
- * stored link key handling and so just disable it.
1578
- */
1579
- set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
1916
+ clear_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
15801917 }
15811918
15821919 kfree_skb(skb);
....@@ -1854,7 +2191,14 @@
18542191 if (err)
18552192 return err;
18562193
1857
- bt_dev_info(hdev, "Intel firmware patch completed and activated");
2194
+ /* Need build number for downloaded fw patches in
2195
+ * every power-on boot
2196
+ */
2197
+ err = btintel_read_version(hdev, &ver);
2198
+ if (err)
2199
+ return err;
2200
+ bt_dev_info(hdev, "Intel BT fw patch 0x%02x completed & activated",
2201
+ ver.fw_patch_num);
18582202
18592203 goto complete;
18602204
....@@ -1936,10 +2280,8 @@
19362280 if (len != sizeof(*evt))
19372281 return;
19382282
1939
- if (test_and_clear_bit(BTUSB_BOOTING, &data->flags)) {
1940
- smp_mb__after_atomic();
2283
+ if (test_and_clear_bit(BTUSB_BOOTING, &data->flags))
19412284 wake_up_bit(&data->flags, BTUSB_BOOTING);
1942
- }
19432285 }
19442286
19452287 static void btusb_intel_secure_send_result(struct btusb_data *data,
....@@ -1954,10 +2296,8 @@
19542296 set_bit(BTUSB_FIRMWARE_FAILED, &data->flags);
19552297
19562298 if (test_and_clear_bit(BTUSB_DOWNLOADING, &data->flags) &&
1957
- test_bit(BTUSB_FIRMWARE_LOADED, &data->flags)) {
1958
- smp_mb__after_atomic();
2299
+ test_bit(BTUSB_FIRMWARE_LOADED, &data->flags))
19592300 wake_up_bit(&data->flags, BTUSB_DOWNLOADING);
1960
- }
19612301 }
19622302
19632303 static int btusb_recv_event_intel(struct hci_dev *hdev, struct sk_buff *skb)
....@@ -2085,42 +2425,25 @@
20852425 return true;
20862426 }
20872427
2088
-static int btusb_setup_intel_new(struct hci_dev *hdev)
2428
+static int btusb_intel_download_firmware(struct hci_dev *hdev,
2429
+ struct intel_version *ver,
2430
+ struct intel_boot_params *params,
2431
+ u32 *boot_param)
20892432 {
2090
- struct btusb_data *data = hci_get_drvdata(hdev);
2091
- struct intel_version ver;
2092
- struct intel_boot_params params;
20932433 const struct firmware *fw;
2094
- u32 boot_param;
20952434 char fwname[64];
2096
- ktime_t calltime, delta, rettime;
2097
- unsigned long long duration;
20982435 int err;
2436
+ struct btusb_data *data = hci_get_drvdata(hdev);
20992437
2100
- BT_DBG("%s", hdev->name);
2101
-
2102
- /* Set the default boot parameter to 0x0 and it is updated to
2103
- * SKU specific boot parameter after reading Intel_Write_Boot_Params
2104
- * command while downloading the firmware.
2105
- */
2106
- boot_param = 0x00000000;
2107
-
2108
- calltime = ktime_get();
2109
-
2110
- /* Read the Intel version information to determine if the device
2111
- * is in bootloader mode or if it already has operational firmware
2112
- * loaded.
2113
- */
2114
- err = btintel_read_version(hdev, &ver);
2115
- if (err)
2116
- return err;
2438
+ if (!ver || !params)
2439
+ return -EINVAL;
21172440
21182441 /* The hardware platform number has a fixed value of 0x37 and
21192442 * for now only accept this single value.
21202443 */
2121
- if (ver.hw_platform != 0x37) {
2444
+ if (ver->hw_platform != 0x37) {
21222445 bt_dev_err(hdev, "Unsupported Intel hardware platform (%u)",
2123
- ver.hw_platform);
2446
+ ver->hw_platform);
21242447 return -EINVAL;
21252448 }
21262449
....@@ -2130,7 +2453,7 @@
21302453 * This check has been put in place to ensure correct forward
21312454 * compatibility options when newer hardware variants come along.
21322455 */
2133
- switch (ver.hw_variant) {
2456
+ switch (ver->hw_variant) {
21342457 case 0x0b: /* SfP */
21352458 case 0x0c: /* WsP */
21362459 case 0x11: /* JfP */
....@@ -2140,11 +2463,11 @@
21402463 break;
21412464 default:
21422465 bt_dev_err(hdev, "Unsupported Intel hardware variant (%u)",
2143
- ver.hw_variant);
2466
+ ver->hw_variant);
21442467 return -EINVAL;
21452468 }
21462469
2147
- btintel_version_info(hdev, &ver);
2470
+ btintel_version_info(hdev, ver);
21482471
21492472 /* The firmware variant determines if the device is in bootloader
21502473 * mode or is running operational firmware. The value 0x06 identifies
....@@ -2159,7 +2482,7 @@
21592482 * It is not possible to use the Secure Boot Parameters in this
21602483 * case since that command is only available in bootloader mode.
21612484 */
2162
- if (ver.fw_variant == 0x23) {
2485
+ if (ver->fw_variant == 0x23) {
21632486 clear_bit(BTUSB_BOOTLOADER, &data->flags);
21642487 btintel_check_bdaddr(hdev);
21652488 return 0;
....@@ -2168,16 +2491,16 @@
21682491 /* If the device is not in bootloader mode, then the only possible
21692492 * choice is to return an error and abort the device initialization.
21702493 */
2171
- if (ver.fw_variant != 0x06) {
2494
+ if (ver->fw_variant != 0x06) {
21722495 bt_dev_err(hdev, "Unsupported Intel firmware variant (%u)",
2173
- ver.fw_variant);
2496
+ ver->fw_variant);
21742497 return -ENODEV;
21752498 }
21762499
21772500 /* Read the secure boot parameters to identify the operating
21782501 * details of the bootloader.
21792502 */
2180
- err = btintel_read_boot_params(hdev, &params);
2503
+ err = btintel_read_boot_params(hdev, params);
21812504 if (err)
21822505 return err;
21832506
....@@ -2185,16 +2508,16 @@
21852508 * with a command complete event. If the boot parameters indicate
21862509 * that this bootloader does not send them, then abort the setup.
21872510 */
2188
- if (params.limited_cce != 0x00) {
2511
+ if (params->limited_cce != 0x00) {
21892512 bt_dev_err(hdev, "Unsupported Intel firmware loading method (%u)",
2190
- params.limited_cce);
2513
+ params->limited_cce);
21912514 return -EINVAL;
21922515 }
21932516
21942517 /* If the OTP has no valid Bluetooth device address, then there will
21952518 * also be no valid address for the operational firmware.
21962519 */
2197
- if (!bacmp(&params.otp_bdaddr, BDADDR_ANY)) {
2520
+ if (!bacmp(&params->otp_bdaddr, BDADDR_ANY)) {
21982521 bt_dev_info(hdev, "No device address configured");
21992522 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
22002523 }
....@@ -2220,7 +2543,7 @@
22202543 * ibt-<hw_variant>-<hw_revision>-<fw_revision>.sfi.
22212544 *
22222545 */
2223
- err = btusb_setup_intel_new_get_fw_name(&ver, &params, fwname,
2546
+ err = btusb_setup_intel_new_get_fw_name(ver, params, fwname,
22242547 sizeof(fwname), "sfi");
22252548 if (!err) {
22262549 bt_dev_err(hdev, "Unsupported Intel firmware naming");
....@@ -2235,16 +2558,6 @@
22352558
22362559 bt_dev_info(hdev, "Found device firmware: %s", fwname);
22372560
2238
- /* Save the DDC file name for later use to apply once the firmware
2239
- * downloading is done.
2240
- */
2241
- err = btusb_setup_intel_new_get_fw_name(&ver, &params, fwname,
2242
- sizeof(fwname), "ddc");
2243
- if (!err) {
2244
- bt_dev_err(hdev, "Unsupported Intel firmware naming");
2245
- return -EINVAL;
2246
- }
2247
-
22482561 if (fw->size < 644) {
22492562 bt_dev_err(hdev, "Invalid size of firmware file (%zu)",
22502563 fw->size);
....@@ -2255,10 +2568,14 @@
22552568 set_bit(BTUSB_DOWNLOADING, &data->flags);
22562569
22572570 /* Start firmware downloading and get boot parameter */
2258
- err = btintel_download_firmware(hdev, fw, &boot_param);
2259
- if (err < 0)
2571
+ err = btintel_download_firmware(hdev, fw, boot_param);
2572
+ if (err < 0) {
2573
+ /* When FW download fails, send Intel Reset to retry
2574
+ * FW download.
2575
+ */
2576
+ btintel_reset_to_bootloader(hdev);
22602577 goto done;
2261
-
2578
+ }
22622579 set_bit(BTUSB_FIRMWARE_LOADED, &data->flags);
22632580
22642581 bt_dev_info(hdev, "Waiting for firmware download to complete");
....@@ -2285,6 +2602,7 @@
22852602 if (err) {
22862603 bt_dev_err(hdev, "Firmware loading timeout");
22872604 err = -ETIMEDOUT;
2605
+ btintel_reset_to_bootloader(hdev);
22882606 goto done;
22892607 }
22902608
....@@ -2294,25 +2612,68 @@
22942612 goto done;
22952613 }
22962614
2615
+done:
2616
+ release_firmware(fw);
2617
+ return err;
2618
+}
2619
+
2620
+static int btusb_setup_intel_new(struct hci_dev *hdev)
2621
+{
2622
+ struct btusb_data *data = hci_get_drvdata(hdev);
2623
+ struct intel_version ver;
2624
+ struct intel_boot_params params;
2625
+ u32 boot_param;
2626
+ char ddcname[64];
2627
+ ktime_t calltime, delta, rettime;
2628
+ unsigned long long duration;
2629
+ int err;
2630
+ struct intel_debug_features features;
2631
+
2632
+ BT_DBG("%s", hdev->name);
2633
+
2634
+ /* Set the default boot parameter to 0x0 and it is updated to
2635
+ * SKU specific boot parameter after reading Intel_Write_Boot_Params
2636
+ * command while downloading the firmware.
2637
+ */
2638
+ boot_param = 0x00000000;
2639
+
2640
+ calltime = ktime_get();
2641
+
2642
+ /* Read the Intel version information to determine if the device
2643
+ * is in bootloader mode or if it already has operational firmware
2644
+ * loaded.
2645
+ */
2646
+ err = btintel_read_version(hdev, &ver);
2647
+ if (err) {
2648
+ bt_dev_err(hdev, "Intel Read version failed (%d)", err);
2649
+ btintel_reset_to_bootloader(hdev);
2650
+ return err;
2651
+ }
2652
+
2653
+ err = btusb_intel_download_firmware(hdev, &ver, &params, &boot_param);
2654
+ if (err)
2655
+ return err;
2656
+
2657
+ /* controller is already having an operational firmware */
2658
+ if (ver.fw_variant == 0x23)
2659
+ goto finish;
2660
+
22972661 rettime = ktime_get();
22982662 delta = ktime_sub(rettime, calltime);
22992663 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
23002664
23012665 bt_dev_info(hdev, "Firmware loaded in %llu usecs", duration);
23022666
2303
-done:
2304
- release_firmware(fw);
2305
-
2306
- if (err < 0)
2307
- return err;
2308
-
23092667 calltime = ktime_get();
23102668
23112669 set_bit(BTUSB_BOOTING, &data->flags);
23122670
23132671 err = btintel_send_intel_reset(hdev, boot_param);
2314
- if (err)
2672
+ if (err) {
2673
+ bt_dev_err(hdev, "Intel Soft Reset failed (%d)", err);
2674
+ btintel_reset_to_bootloader(hdev);
23152675 return err;
2676
+ }
23162677
23172678 /* The bootloader will not indicate when the device is ready. This
23182679 * is done by the operational firmware sending bootup notification.
....@@ -2334,6 +2695,7 @@
23342695
23352696 if (err) {
23362697 bt_dev_err(hdev, "Device boot timeout");
2698
+ btintel_reset_to_bootloader(hdev);
23372699 return -ETIMEDOUT;
23382700 }
23392701
....@@ -2345,13 +2707,45 @@
23452707
23462708 clear_bit(BTUSB_BOOTLOADER, &data->flags);
23472709
2348
- /* Once the device is running in operational mode, it needs to apply
2349
- * the device configuration (DDC) parameters.
2350
- *
2351
- * The device can work without DDC parameters, so even if it fails
2352
- * to load the file, no need to fail the setup.
2710
+ err = btusb_setup_intel_new_get_fw_name(&ver, &params, ddcname,
2711
+ sizeof(ddcname), "ddc");
2712
+
2713
+ if (!err) {
2714
+ bt_dev_err(hdev, "Unsupported Intel firmware naming");
2715
+ } else {
2716
+ /* Once the device is running in operational mode, it needs to
2717
+ * apply the device configuration (DDC) parameters.
2718
+ *
2719
+ * The device can work without DDC parameters, so even if it
2720
+ * fails to load the file, no need to fail the setup.
2721
+ */
2722
+ btintel_load_ddc_config(hdev, ddcname);
2723
+ }
2724
+
2725
+ /* Read the Intel supported features and if new exception formats
2726
+ * supported, need to load the additional DDC config to enable.
23532727 */
2354
- btintel_load_ddc_config(hdev, fwname);
2728
+ btintel_read_debug_features(hdev, &features);
2729
+
2730
+ /* Set DDC mask for available debug features */
2731
+ btintel_set_debug_features(hdev, &features);
2732
+
2733
+ /* Read the Intel version information after loading the FW */
2734
+ err = btintel_read_version(hdev, &ver);
2735
+ if (err)
2736
+ return err;
2737
+
2738
+ btintel_version_info(hdev, &ver);
2739
+
2740
+finish:
2741
+ /* All Intel controllers that support the Microsoft vendor
2742
+ * extension are using 0xFC1E for VsMsftOpCode.
2743
+ */
2744
+ switch (ver.hw_variant) {
2745
+ case 0x12: /* ThP */
2746
+ hci_set_msft_opcode(hdev, 0xFC1E);
2747
+ break;
2748
+ }
23552749
23562750 /* Set the event mask for Intel specific vendor events. This enables
23572751 * a few extra events that are useful during general operation. It
....@@ -2400,6 +2794,626 @@
24002794
24012795 return 0;
24022796 }
2797
+
2798
+static int btusb_shutdown_intel_new(struct hci_dev *hdev)
2799
+{
2800
+ struct sk_buff *skb;
2801
+
2802
+ /* Send HCI Reset to the controller to stop any BT activity which
2803
+ * were triggered. This will help to save power and maintain the
2804
+ * sync b/w Host and controller
2805
+ */
2806
+ skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
2807
+ if (IS_ERR(skb)) {
2808
+ bt_dev_err(hdev, "HCI reset during shutdown failed");
2809
+ return PTR_ERR(skb);
2810
+ }
2811
+ kfree_skb(skb);
2812
+
2813
+ return 0;
2814
+}
2815
+
2816
+#define FIRMWARE_MT7663 "mediatek/mt7663pr2h.bin"
2817
+#define FIRMWARE_MT7668 "mediatek/mt7668pr2h.bin"
2818
+
2819
+#define HCI_WMT_MAX_EVENT_SIZE 64
2820
+
2821
+enum {
2822
+ BTMTK_WMT_PATCH_DWNLD = 0x1,
2823
+ BTMTK_WMT_FUNC_CTRL = 0x6,
2824
+ BTMTK_WMT_RST = 0x7,
2825
+ BTMTK_WMT_SEMAPHORE = 0x17,
2826
+};
2827
+
2828
+enum {
2829
+ BTMTK_WMT_INVALID,
2830
+ BTMTK_WMT_PATCH_UNDONE,
2831
+ BTMTK_WMT_PATCH_PROGRESS,
2832
+ BTMTK_WMT_PATCH_DONE,
2833
+ BTMTK_WMT_ON_UNDONE,
2834
+ BTMTK_WMT_ON_DONE,
2835
+ BTMTK_WMT_ON_PROGRESS,
2836
+};
2837
+
2838
+struct btmtk_wmt_hdr {
2839
+ u8 dir;
2840
+ u8 op;
2841
+ __le16 dlen;
2842
+ u8 flag;
2843
+} __packed;
2844
+
2845
+struct btmtk_hci_wmt_cmd {
2846
+ struct btmtk_wmt_hdr hdr;
2847
+ u8 data[];
2848
+} __packed;
2849
+
2850
+struct btmtk_hci_wmt_evt {
2851
+ struct hci_event_hdr hhdr;
2852
+ struct btmtk_wmt_hdr whdr;
2853
+} __packed;
2854
+
2855
+struct btmtk_hci_wmt_evt_funcc {
2856
+ struct btmtk_hci_wmt_evt hwhdr;
2857
+ __be16 status;
2858
+} __packed;
2859
+
2860
+struct btmtk_tci_sleep {
2861
+ u8 mode;
2862
+ __le16 duration;
2863
+ __le16 host_duration;
2864
+ u8 host_wakeup_pin;
2865
+ u8 time_compensation;
2866
+} __packed;
2867
+
2868
+struct btmtk_hci_wmt_params {
2869
+ u8 op;
2870
+ u8 flag;
2871
+ u16 dlen;
2872
+ const void *data;
2873
+ u32 *status;
2874
+};
2875
+
2876
+static void btusb_mtk_wmt_recv(struct urb *urb)
2877
+{
2878
+ struct hci_dev *hdev = urb->context;
2879
+ struct btusb_data *data = hci_get_drvdata(hdev);
2880
+ struct hci_event_hdr *hdr;
2881
+ struct sk_buff *skb;
2882
+ int err;
2883
+
2884
+ if (urb->status == 0 && urb->actual_length > 0) {
2885
+ hdev->stat.byte_rx += urb->actual_length;
2886
+
2887
+ /* WMT event shouldn't be fragmented and the size should be
2888
+ * less than HCI_WMT_MAX_EVENT_SIZE.
2889
+ */
2890
+ skb = bt_skb_alloc(HCI_WMT_MAX_EVENT_SIZE, GFP_ATOMIC);
2891
+ if (!skb) {
2892
+ hdev->stat.err_rx++;
2893
+ kfree(urb->setup_packet);
2894
+ return;
2895
+ }
2896
+
2897
+ hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
2898
+ skb_put_data(skb, urb->transfer_buffer, urb->actual_length);
2899
+
2900
+ hdr = (void *)skb->data;
2901
+ /* Fix up the vendor event id with 0xff for vendor specific
2902
+ * instead of 0xe4 so that event send via monitoring socket can
2903
+ * be parsed properly.
2904
+ */
2905
+ hdr->evt = 0xff;
2906
+
2907
+ /* When someone waits for the WMT event, the skb is being cloned
2908
+ * and being processed the events from there then.
2909
+ */
2910
+ if (test_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags)) {
2911
+ data->evt_skb = skb_clone(skb, GFP_ATOMIC);
2912
+ if (!data->evt_skb) {
2913
+ kfree_skb(skb);
2914
+ kfree(urb->setup_packet);
2915
+ return;
2916
+ }
2917
+ }
2918
+
2919
+ err = hci_recv_frame(hdev, skb);
2920
+ if (err < 0) {
2921
+ kfree_skb(data->evt_skb);
2922
+ data->evt_skb = NULL;
2923
+ kfree(urb->setup_packet);
2924
+ return;
2925
+ }
2926
+
2927
+ if (test_and_clear_bit(BTUSB_TX_WAIT_VND_EVT,
2928
+ &data->flags)) {
2929
+ /* Barrier to sync with other CPUs */
2930
+ smp_mb__after_atomic();
2931
+ wake_up_bit(&data->flags,
2932
+ BTUSB_TX_WAIT_VND_EVT);
2933
+ }
2934
+ kfree(urb->setup_packet);
2935
+ return;
2936
+ } else if (urb->status == -ENOENT) {
2937
+ /* Avoid suspend failed when usb_kill_urb */
2938
+ return;
2939
+ }
2940
+
2941
+ usb_mark_last_busy(data->udev);
2942
+
2943
+ /* The URB complete handler is still called with urb->actual_length = 0
2944
+ * when the event is not available, so we should keep re-submitting
2945
+ * URB until WMT event returns, Also, It's necessary to wait some time
2946
+ * between the two consecutive control URBs to relax the target device
2947
+ * to generate the event. Otherwise, the WMT event cannot return from
2948
+ * the device successfully.
2949
+ */
2950
+ udelay(500);
2951
+
2952
+ usb_anchor_urb(urb, &data->ctrl_anchor);
2953
+ err = usb_submit_urb(urb, GFP_ATOMIC);
2954
+ if (err < 0) {
2955
+ kfree(urb->setup_packet);
2956
+ /* -EPERM: urb is being killed;
2957
+ * -ENODEV: device got disconnected
2958
+ */
2959
+ if (err != -EPERM && err != -ENODEV)
2960
+ bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
2961
+ urb, -err);
2962
+ usb_unanchor_urb(urb);
2963
+ }
2964
+}
2965
+
2966
+static int btusb_mtk_submit_wmt_recv_urb(struct hci_dev *hdev)
2967
+{
2968
+ struct btusb_data *data = hci_get_drvdata(hdev);
2969
+ struct usb_ctrlrequest *dr;
2970
+ unsigned char *buf;
2971
+ int err, size = 64;
2972
+ unsigned int pipe;
2973
+ struct urb *urb;
2974
+
2975
+ urb = usb_alloc_urb(0, GFP_KERNEL);
2976
+ if (!urb)
2977
+ return -ENOMEM;
2978
+
2979
+ dr = kmalloc(sizeof(*dr), GFP_KERNEL);
2980
+ if (!dr) {
2981
+ usb_free_urb(urb);
2982
+ return -ENOMEM;
2983
+ }
2984
+
2985
+ dr->bRequestType = USB_TYPE_VENDOR | USB_DIR_IN;
2986
+ dr->bRequest = 1;
2987
+ dr->wIndex = cpu_to_le16(0);
2988
+ dr->wValue = cpu_to_le16(48);
2989
+ dr->wLength = cpu_to_le16(size);
2990
+
2991
+ buf = kmalloc(size, GFP_KERNEL);
2992
+ if (!buf) {
2993
+ kfree(dr);
2994
+ usb_free_urb(urb);
2995
+ return -ENOMEM;
2996
+ }
2997
+
2998
+ pipe = usb_rcvctrlpipe(data->udev, 0);
2999
+
3000
+ usb_fill_control_urb(urb, data->udev, pipe, (void *)dr,
3001
+ buf, size, btusb_mtk_wmt_recv, hdev);
3002
+
3003
+ urb->transfer_flags |= URB_FREE_BUFFER;
3004
+
3005
+ usb_anchor_urb(urb, &data->ctrl_anchor);
3006
+ err = usb_submit_urb(urb, GFP_KERNEL);
3007
+ if (err < 0) {
3008
+ if (err != -EPERM && err != -ENODEV)
3009
+ bt_dev_err(hdev, "urb %p submission failed (%d)",
3010
+ urb, -err);
3011
+ usb_unanchor_urb(urb);
3012
+ }
3013
+
3014
+ usb_free_urb(urb);
3015
+
3016
+ return err;
3017
+}
3018
+
3019
+static int btusb_mtk_hci_wmt_sync(struct hci_dev *hdev,
3020
+ struct btmtk_hci_wmt_params *wmt_params)
3021
+{
3022
+ struct btusb_data *data = hci_get_drvdata(hdev);
3023
+ struct btmtk_hci_wmt_evt_funcc *wmt_evt_funcc;
3024
+ u32 hlen, status = BTMTK_WMT_INVALID;
3025
+ struct btmtk_hci_wmt_evt *wmt_evt;
3026
+ struct btmtk_hci_wmt_cmd *wc;
3027
+ struct btmtk_wmt_hdr *hdr;
3028
+ int err;
3029
+
3030
+ /* Send the WMT command and wait until the WMT event returns */
3031
+ hlen = sizeof(*hdr) + wmt_params->dlen;
3032
+ if (hlen > 255)
3033
+ return -EINVAL;
3034
+
3035
+ wc = kzalloc(hlen, GFP_KERNEL);
3036
+ if (!wc)
3037
+ return -ENOMEM;
3038
+
3039
+ hdr = &wc->hdr;
3040
+ hdr->dir = 1;
3041
+ hdr->op = wmt_params->op;
3042
+ hdr->dlen = cpu_to_le16(wmt_params->dlen + 1);
3043
+ hdr->flag = wmt_params->flag;
3044
+ memcpy(wc->data, wmt_params->data, wmt_params->dlen);
3045
+
3046
+ set_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
3047
+
3048
+ /* WMT cmd/event doesn't follow up the generic HCI cmd/event handling,
3049
+ * it needs constantly polling control pipe until the host received the
3050
+ * WMT event, thus, we should require to specifically acquire PM counter
3051
+ * on the USB to prevent the interface from entering auto suspended
3052
+ * while WMT cmd/event in progress.
3053
+ */
3054
+ err = usb_autopm_get_interface(data->intf);
3055
+ if (err < 0)
3056
+ goto err_free_wc;
3057
+
3058
+ err = __hci_cmd_send(hdev, 0xfc6f, hlen, wc);
3059
+
3060
+ if (err < 0) {
3061
+ clear_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
3062
+ usb_autopm_put_interface(data->intf);
3063
+ goto err_free_wc;
3064
+ }
3065
+
3066
+ /* Submit control IN URB on demand to process the WMT event */
3067
+ err = btusb_mtk_submit_wmt_recv_urb(hdev);
3068
+
3069
+ usb_autopm_put_interface(data->intf);
3070
+
3071
+ if (err < 0)
3072
+ return err;
3073
+
3074
+ /* The vendor specific WMT commands are all answered by a vendor
3075
+ * specific event and will have the Command Status or Command
3076
+ * Complete as with usual HCI command flow control.
3077
+ *
3078
+ * After sending the command, wait for BTUSB_TX_WAIT_VND_EVT
3079
+ * state to be cleared. The driver specific event receive routine
3080
+ * will clear that state and with that indicate completion of the
3081
+ * WMT command.
3082
+ */
3083
+ err = wait_on_bit_timeout(&data->flags, BTUSB_TX_WAIT_VND_EVT,
3084
+ TASK_INTERRUPTIBLE, HCI_INIT_TIMEOUT);
3085
+ if (err == -EINTR) {
3086
+ bt_dev_err(hdev, "Execution of wmt command interrupted");
3087
+ clear_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
3088
+ goto err_free_wc;
3089
+ }
3090
+
3091
+ if (err) {
3092
+ bt_dev_err(hdev, "Execution of wmt command timed out");
3093
+ clear_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
3094
+ err = -ETIMEDOUT;
3095
+ goto err_free_wc;
3096
+ }
3097
+
3098
+ /* Parse and handle the return WMT event */
3099
+ wmt_evt = (struct btmtk_hci_wmt_evt *)data->evt_skb->data;
3100
+ if (wmt_evt->whdr.op != hdr->op) {
3101
+ bt_dev_err(hdev, "Wrong op received %d expected %d",
3102
+ wmt_evt->whdr.op, hdr->op);
3103
+ err = -EIO;
3104
+ goto err_free_skb;
3105
+ }
3106
+
3107
+ switch (wmt_evt->whdr.op) {
3108
+ case BTMTK_WMT_SEMAPHORE:
3109
+ if (wmt_evt->whdr.flag == 2)
3110
+ status = BTMTK_WMT_PATCH_UNDONE;
3111
+ else
3112
+ status = BTMTK_WMT_PATCH_DONE;
3113
+ break;
3114
+ case BTMTK_WMT_FUNC_CTRL:
3115
+ wmt_evt_funcc = (struct btmtk_hci_wmt_evt_funcc *)wmt_evt;
3116
+ if (be16_to_cpu(wmt_evt_funcc->status) == 0x404)
3117
+ status = BTMTK_WMT_ON_DONE;
3118
+ else if (be16_to_cpu(wmt_evt_funcc->status) == 0x420)
3119
+ status = BTMTK_WMT_ON_PROGRESS;
3120
+ else
3121
+ status = BTMTK_WMT_ON_UNDONE;
3122
+ break;
3123
+ }
3124
+
3125
+ if (wmt_params->status)
3126
+ *wmt_params->status = status;
3127
+
3128
+err_free_skb:
3129
+ kfree_skb(data->evt_skb);
3130
+ data->evt_skb = NULL;
3131
+err_free_wc:
3132
+ kfree(wc);
3133
+ return err;
3134
+}
3135
+
3136
+static int btusb_mtk_setup_firmware(struct hci_dev *hdev, const char *fwname)
3137
+{
3138
+ struct btmtk_hci_wmt_params wmt_params;
3139
+ const struct firmware *fw;
3140
+ const u8 *fw_ptr;
3141
+ size_t fw_size;
3142
+ int err, dlen;
3143
+ u8 flag, param;
3144
+
3145
+ err = request_firmware(&fw, fwname, &hdev->dev);
3146
+ if (err < 0) {
3147
+ bt_dev_err(hdev, "Failed to load firmware file (%d)", err);
3148
+ return err;
3149
+ }
3150
+
3151
+ /* Power on data RAM the firmware relies on. */
3152
+ param = 1;
3153
+ wmt_params.op = BTMTK_WMT_FUNC_CTRL;
3154
+ wmt_params.flag = 3;
3155
+ wmt_params.dlen = sizeof(param);
3156
+ wmt_params.data = &param;
3157
+ wmt_params.status = NULL;
3158
+
3159
+ err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3160
+ if (err < 0) {
3161
+ bt_dev_err(hdev, "Failed to power on data RAM (%d)", err);
3162
+ goto err_release_fw;
3163
+ }
3164
+
3165
+ fw_ptr = fw->data;
3166
+ fw_size = fw->size;
3167
+
3168
+ /* The size of patch header is 30 bytes, should be skip */
3169
+ if (fw_size < 30) {
3170
+ err = -EINVAL;
3171
+ goto err_release_fw;
3172
+ }
3173
+
3174
+ fw_size -= 30;
3175
+ fw_ptr += 30;
3176
+ flag = 1;
3177
+
3178
+ wmt_params.op = BTMTK_WMT_PATCH_DWNLD;
3179
+ wmt_params.status = NULL;
3180
+
3181
+ while (fw_size > 0) {
3182
+ dlen = min_t(int, 250, fw_size);
3183
+
3184
+ /* Tell deivice the position in sequence */
3185
+ if (fw_size - dlen <= 0)
3186
+ flag = 3;
3187
+ else if (fw_size < fw->size - 30)
3188
+ flag = 2;
3189
+
3190
+ wmt_params.flag = flag;
3191
+ wmt_params.dlen = dlen;
3192
+ wmt_params.data = fw_ptr;
3193
+
3194
+ err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3195
+ if (err < 0) {
3196
+ bt_dev_err(hdev, "Failed to send wmt patch dwnld (%d)",
3197
+ err);
3198
+ goto err_release_fw;
3199
+ }
3200
+
3201
+ fw_size -= dlen;
3202
+ fw_ptr += dlen;
3203
+ }
3204
+
3205
+ wmt_params.op = BTMTK_WMT_RST;
3206
+ wmt_params.flag = 4;
3207
+ wmt_params.dlen = 0;
3208
+ wmt_params.data = NULL;
3209
+ wmt_params.status = NULL;
3210
+
3211
+ /* Activate funciton the firmware providing to */
3212
+ err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3213
+ if (err < 0) {
3214
+ bt_dev_err(hdev, "Failed to send wmt rst (%d)", err);
3215
+ goto err_release_fw;
3216
+ }
3217
+
3218
+ /* Wait a few moments for firmware activation done */
3219
+ usleep_range(10000, 12000);
3220
+
3221
+err_release_fw:
3222
+ release_firmware(fw);
3223
+
3224
+ return err;
3225
+}
3226
+
3227
+static int btusb_mtk_func_query(struct hci_dev *hdev)
3228
+{
3229
+ struct btmtk_hci_wmt_params wmt_params;
3230
+ int status, err;
3231
+ u8 param = 0;
3232
+
3233
+ /* Query whether the function is enabled */
3234
+ wmt_params.op = BTMTK_WMT_FUNC_CTRL;
3235
+ wmt_params.flag = 4;
3236
+ wmt_params.dlen = sizeof(param);
3237
+ wmt_params.data = &param;
3238
+ wmt_params.status = &status;
3239
+
3240
+ err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3241
+ if (err < 0) {
3242
+ bt_dev_err(hdev, "Failed to query function status (%d)", err);
3243
+ return err;
3244
+ }
3245
+
3246
+ return status;
3247
+}
3248
+
3249
+static int btusb_mtk_reg_read(struct btusb_data *data, u32 reg, u32 *val)
3250
+{
3251
+ int pipe, err, size = sizeof(u32);
3252
+ void *buf;
3253
+
3254
+ buf = kzalloc(size, GFP_KERNEL);
3255
+ if (!buf)
3256
+ return -ENOMEM;
3257
+
3258
+ pipe = usb_rcvctrlpipe(data->udev, 0);
3259
+ err = usb_control_msg(data->udev, pipe, 0x63,
3260
+ USB_TYPE_VENDOR | USB_DIR_IN,
3261
+ reg >> 16, reg & 0xffff,
3262
+ buf, size, USB_CTRL_SET_TIMEOUT);
3263
+ if (err < 0)
3264
+ goto err_free_buf;
3265
+
3266
+ *val = get_unaligned_le32(buf);
3267
+
3268
+err_free_buf:
3269
+ kfree(buf);
3270
+
3271
+ return err;
3272
+}
3273
+
3274
+static int btusb_mtk_id_get(struct btusb_data *data, u32 reg, u32 *id)
3275
+{
3276
+ return btusb_mtk_reg_read(data, reg, id);
3277
+}
3278
+
3279
+static int btusb_mtk_setup(struct hci_dev *hdev)
3280
+{
3281
+ struct btusb_data *data = hci_get_drvdata(hdev);
3282
+ struct btmtk_hci_wmt_params wmt_params;
3283
+ ktime_t calltime, delta, rettime;
3284
+ struct btmtk_tci_sleep tci_sleep;
3285
+ unsigned long long duration;
3286
+ struct sk_buff *skb;
3287
+ const char *fwname;
3288
+ int err, status;
3289
+ u32 dev_id;
3290
+ u8 param;
3291
+
3292
+ calltime = ktime_get();
3293
+
3294
+ err = btusb_mtk_id_get(data, 0x80000008, &dev_id);
3295
+ if (err < 0) {
3296
+ bt_dev_err(hdev, "Failed to get device id (%d)", err);
3297
+ return err;
3298
+ }
3299
+
3300
+ switch (dev_id) {
3301
+ case 0x7663:
3302
+ fwname = FIRMWARE_MT7663;
3303
+ break;
3304
+ case 0x7668:
3305
+ fwname = FIRMWARE_MT7668;
3306
+ break;
3307
+ default:
3308
+ bt_dev_err(hdev, "Unsupported support hardware variant (%08x)",
3309
+ dev_id);
3310
+ return -ENODEV;
3311
+ }
3312
+
3313
+ /* Query whether the firmware is already download */
3314
+ wmt_params.op = BTMTK_WMT_SEMAPHORE;
3315
+ wmt_params.flag = 1;
3316
+ wmt_params.dlen = 0;
3317
+ wmt_params.data = NULL;
3318
+ wmt_params.status = &status;
3319
+
3320
+ err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3321
+ if (err < 0) {
3322
+ bt_dev_err(hdev, "Failed to query firmware status (%d)", err);
3323
+ return err;
3324
+ }
3325
+
3326
+ if (status == BTMTK_WMT_PATCH_DONE) {
3327
+ bt_dev_info(hdev, "firmware already downloaded");
3328
+ goto ignore_setup_fw;
3329
+ }
3330
+
3331
+ /* Setup a firmware which the device definitely requires */
3332
+ err = btusb_mtk_setup_firmware(hdev, fwname);
3333
+ if (err < 0)
3334
+ return err;
3335
+
3336
+ignore_setup_fw:
3337
+ err = readx_poll_timeout(btusb_mtk_func_query, hdev, status,
3338
+ status < 0 || status != BTMTK_WMT_ON_PROGRESS,
3339
+ 2000, 5000000);
3340
+ /* -ETIMEDOUT happens */
3341
+ if (err < 0)
3342
+ return err;
3343
+
3344
+ /* The other errors happen in btusb_mtk_func_query */
3345
+ if (status < 0)
3346
+ return status;
3347
+
3348
+ if (status == BTMTK_WMT_ON_DONE) {
3349
+ bt_dev_info(hdev, "function already on");
3350
+ goto ignore_func_on;
3351
+ }
3352
+
3353
+ /* Enable Bluetooth protocol */
3354
+ param = 1;
3355
+ wmt_params.op = BTMTK_WMT_FUNC_CTRL;
3356
+ wmt_params.flag = 0;
3357
+ wmt_params.dlen = sizeof(param);
3358
+ wmt_params.data = &param;
3359
+ wmt_params.status = NULL;
3360
+
3361
+ err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3362
+ if (err < 0) {
3363
+ bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
3364
+ return err;
3365
+ }
3366
+
3367
+ignore_func_on:
3368
+ /* Apply the low power environment setup */
3369
+ tci_sleep.mode = 0x5;
3370
+ tci_sleep.duration = cpu_to_le16(0x640);
3371
+ tci_sleep.host_duration = cpu_to_le16(0x640);
3372
+ tci_sleep.host_wakeup_pin = 0;
3373
+ tci_sleep.time_compensation = 0;
3374
+
3375
+ skb = __hci_cmd_sync(hdev, 0xfc7a, sizeof(tci_sleep), &tci_sleep,
3376
+ HCI_INIT_TIMEOUT);
3377
+ if (IS_ERR(skb)) {
3378
+ err = PTR_ERR(skb);
3379
+ bt_dev_err(hdev, "Failed to apply low power setting (%d)", err);
3380
+ return err;
3381
+ }
3382
+ kfree_skb(skb);
3383
+
3384
+ rettime = ktime_get();
3385
+ delta = ktime_sub(rettime, calltime);
3386
+ duration = (unsigned long long)ktime_to_ns(delta) >> 10;
3387
+
3388
+ bt_dev_info(hdev, "Device setup in %llu usecs", duration);
3389
+
3390
+ return 0;
3391
+}
3392
+
3393
+static int btusb_mtk_shutdown(struct hci_dev *hdev)
3394
+{
3395
+ struct btmtk_hci_wmt_params wmt_params;
3396
+ u8 param = 0;
3397
+ int err;
3398
+
3399
+ /* Disable the device */
3400
+ wmt_params.op = BTMTK_WMT_FUNC_CTRL;
3401
+ wmt_params.flag = 0;
3402
+ wmt_params.dlen = sizeof(param);
3403
+ wmt_params.data = &param;
3404
+ wmt_params.status = NULL;
3405
+
3406
+ err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3407
+ if (err < 0) {
3408
+ bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
3409
+ return err;
3410
+ }
3411
+
3412
+ return 0;
3413
+}
3414
+
3415
+MODULE_FIRMWARE(FIRMWARE_MT7663);
3416
+MODULE_FIRMWARE(FIRMWARE_MT7668);
24033417
24043418 #ifdef CONFIG_PM
24053419 /* Configure an out-of-band gpio as wake-up pin, if specified in device tree */
....@@ -2492,6 +3506,27 @@
24923506 return 0;
24933507 }
24943508
3509
+static int btusb_set_bdaddr_wcn6855(struct hci_dev *hdev,
3510
+ const bdaddr_t *bdaddr)
3511
+{
3512
+ struct sk_buff *skb;
3513
+ u8 buf[6];
3514
+ long ret;
3515
+
3516
+ memcpy(buf, bdaddr, sizeof(bdaddr_t));
3517
+
3518
+ skb = __hci_cmd_sync_ev(hdev, 0xfc14, sizeof(buf), buf,
3519
+ HCI_EV_CMD_COMPLETE, HCI_INIT_TIMEOUT);
3520
+ if (IS_ERR(skb)) {
3521
+ ret = PTR_ERR(skb);
3522
+ bt_dev_err(hdev, "Change address command failed (%ld)", ret);
3523
+ return ret;
3524
+ }
3525
+ kfree_skb(skb);
3526
+
3527
+ return 0;
3528
+}
3529
+
24953530 #define QCA_DFU_PACKET_LEN 4096
24963531
24973532 #define QCA_GET_TARGET_VERSION 0x09
....@@ -2511,7 +3546,8 @@
25113546 } __packed;
25123547
25133548 struct qca_rampatch_version {
2514
- __le16 rom_version;
3549
+ __le16 rom_version_high;
3550
+ __le16 rom_version_low;
25153551 __le16 patch_version;
25163552 } __packed;
25173553
....@@ -2523,12 +3559,14 @@
25233559 };
25243560
25253561 static const struct qca_device_info qca_devices_table[] = {
2526
- { 0x00000100, 20, 4, 10 }, /* Rome 1.0 */
2527
- { 0x00000101, 20, 4, 10 }, /* Rome 1.1 */
2528
- { 0x00000200, 28, 4, 18 }, /* Rome 2.0 */
2529
- { 0x00000201, 28, 4, 18 }, /* Rome 2.1 */
2530
- { 0x00000300, 28, 4, 18 }, /* Rome 3.0 */
2531
- { 0x00000302, 28, 4, 18 }, /* Rome 3.2 */
3562
+ { 0x00000100, 20, 4, 8 }, /* Rome 1.0 */
3563
+ { 0x00000101, 20, 4, 8 }, /* Rome 1.1 */
3564
+ { 0x00000200, 28, 4, 16 }, /* Rome 2.0 */
3565
+ { 0x00000201, 28, 4, 16 }, /* Rome 2.1 */
3566
+ { 0x00000300, 28, 4, 16 }, /* Rome 3.0 */
3567
+ { 0x00000302, 28, 4, 16 }, /* Rome 3.2 */
3568
+ { 0x00130100, 40, 4, 16 }, /* WCN6855 1.0 */
3569
+ { 0x00130200, 40, 4, 16 }, /* WCN6855 2.0 */
25323570 };
25333571
25343572 static int btusb_qca_send_vendor_req(struct usb_device *udev, u8 request,
....@@ -2635,8 +3673,8 @@
26353673 {
26363674 struct qca_rampatch_version *rver;
26373675 const struct firmware *fw;
2638
- u32 ver_rom, ver_patch;
2639
- u16 rver_rom, rver_patch;
3676
+ u32 ver_rom, ver_patch, rver_rom;
3677
+ u16 rver_rom_low, rver_rom_high, rver_patch;
26403678 char fwname[64];
26413679 int err;
26423680
....@@ -2655,8 +3693,15 @@
26553693 bt_dev_info(hdev, "using rampatch file: %s", fwname);
26563694
26573695 rver = (struct qca_rampatch_version *)(fw->data + info->ver_offset);
2658
- rver_rom = le16_to_cpu(rver->rom_version);
3696
+ rver_rom_low = le16_to_cpu(rver->rom_version_low);
26593697 rver_patch = le16_to_cpu(rver->patch_version);
3698
+
3699
+ if (ver_rom & ~0xffffU) {
3700
+ rver_rom_high = le16_to_cpu(rver->rom_version_high);
3701
+ rver_rom = le32_to_cpu(rver_rom_high << 16 | rver_rom_low);
3702
+ } else {
3703
+ rver_rom = rver_rom_low;
3704
+ }
26603705
26613706 bt_dev_info(hdev, "QCA: patch rome 0x%x build 0x%x, "
26623707 "firmware rome 0x%x build 0x%x",
....@@ -2731,15 +3776,19 @@
27313776 return err;
27323777
27333778 ver_rom = le32_to_cpu(ver.rom_version);
2734
- /* Don't care about high ROM versions */
2735
- if (ver_rom & ~0xffffU)
2736
- return 0;
27373779
27383780 for (i = 0; i < ARRAY_SIZE(qca_devices_table); i++) {
27393781 if (ver_rom == qca_devices_table[i].rom_version)
27403782 info = &qca_devices_table[i];
27413783 }
27423784 if (!info) {
3785
+ /* If the rom_version is not matched in the qca_devices_table
3786
+ * and the high ROM version is not zero, we assume this chip no
3787
+ * need to load the rampatch and nvm.
3788
+ */
3789
+ if (ver_rom & ~0xffffU)
3790
+ return 0;
3791
+
27433792 bt_dev_err(hdev, "don't support firmware rome 0x%x", ver_rom);
27443793 return -ENODEV;
27453794 }
....@@ -2764,7 +3813,6 @@
27643813 return 0;
27653814 }
27663815
2767
-#ifdef CONFIG_BT_HCIBTUSB_BCM
27683816 static inline int __set_diag_interface(struct hci_dev *hdev)
27693817 {
27703818 struct btusb_data *data = hci_get_drvdata(hdev);
....@@ -2851,7 +3899,6 @@
28513899
28523900 return submit_or_queue_tx_urb(hdev, urb);
28533901 }
2854
-#endif
28553902
28563903 #ifdef CONFIG_PM
28573904 static irqreturn_t btusb_oob_wake_handler(int irq, void *priv)
....@@ -2871,6 +3918,8 @@
28713918
28723919 static const struct of_device_id btusb_match_table[] = {
28733920 { .compatible = "usb1286,204e" },
3921
+ { .compatible = "usbcf3,e300" }, /* QCA6174A */
3922
+ { .compatible = "usb4ca,301a" }, /* QCA6174A (Lite-On) */
28743923 { }
28753924 };
28763925 MODULE_DEVICE_TABLE(of, btusb_match_table);
....@@ -2920,10 +3969,21 @@
29203969 interface_to_usbdev(intf)->quirks |= USB_QUIRK_RESET_RESUME;
29213970 }
29223971
3972
+static bool btusb_prevent_wake(struct hci_dev *hdev)
3973
+{
3974
+ struct btusb_data *data = hci_get_drvdata(hdev);
3975
+
3976
+ if (test_bit(BTUSB_WAKEUP_DISABLE, &data->flags))
3977
+ return true;
3978
+
3979
+ return !device_may_wakeup(&data->udev->dev);
3980
+}
3981
+
29233982 static int btusb_probe(struct usb_interface *intf,
29243983 const struct usb_device_id *id)
29253984 {
29263985 struct usb_endpoint_descriptor *ep_desc;
3986
+ struct gpio_desc *reset_gpio;
29273987 struct btusb_data *data;
29283988 struct hci_dev *hdev;
29293989 unsigned ifnum_base;
....@@ -3010,6 +4070,7 @@
30104070 init_usb_anchor(&data->bulk_anchor);
30114071 init_usb_anchor(&data->isoc_anchor);
30124072 init_usb_anchor(&data->diag_anchor);
4073
+ init_usb_anchor(&data->ctrl_anchor);
30134074 spin_lock_init(&data->rxlock);
30144075
30154076 if (id->driver_info & BTUSB_INTEL_NEW) {
....@@ -3037,11 +4098,21 @@
30374098
30384099 SET_HCIDEV_DEV(hdev, &intf->dev);
30394100
4101
+ reset_gpio = gpiod_get_optional(&data->udev->dev, "reset",
4102
+ GPIOD_OUT_LOW);
4103
+ if (IS_ERR(reset_gpio)) {
4104
+ err = PTR_ERR(reset_gpio);
4105
+ goto out_free_dev;
4106
+ } else if (reset_gpio) {
4107
+ data->reset_gpio = reset_gpio;
4108
+ }
4109
+
30404110 hdev->open = btusb_open;
30414111 hdev->close = btusb_close;
30424112 hdev->flush = btusb_flush;
30434113 hdev->send = btusb_send_frame;
30444114 hdev->notify = btusb_notify;
4115
+ hdev->prevent_wake = btusb_prevent_wake;
30454116
30464117 #ifdef CONFIG_PM
30474118 err = btusb_config_oob_wake(hdev);
....@@ -3064,8 +4135,8 @@
30644135 if (id->driver_info & BTUSB_BCM92035)
30654136 hdev->setup = btusb_setup_bcm92035;
30664137
3067
-#ifdef CONFIG_BT_HCIBTUSB_BCM
3068
- if (id->driver_info & BTUSB_BCM_PATCHRAM) {
4138
+ if (IS_ENABLED(CONFIG_BT_HCIBTUSB_BCM) &&
4139
+ (id->driver_info & BTUSB_BCM_PATCHRAM)) {
30694140 hdev->manufacturer = 15;
30704141 hdev->setup = btbcm_setup_patchram;
30714142 hdev->set_diag = btusb_bcm_set_diag;
....@@ -3075,7 +4146,8 @@
30754146 data->diag = usb_ifnum_to_if(data->udev, ifnum_base + 2);
30764147 }
30774148
3078
- if (id->driver_info & BTUSB_BCM_APPLE) {
4149
+ if (IS_ENABLED(CONFIG_BT_HCIBTUSB_BCM) &&
4150
+ (id->driver_info & BTUSB_BCM_APPLE)) {
30794151 hdev->manufacturer = 15;
30804152 hdev->setup = btbcm_setup_apple;
30814153 hdev->set_diag = btusb_bcm_set_diag;
....@@ -3083,7 +4155,6 @@
30834155 /* Broadcom LM_DIAG Interface numbers are hardcoded */
30844156 data->diag = usb_ifnum_to_if(data->udev, ifnum_base + 2);
30854157 }
3086
-#endif
30874158
30884159 if (id->driver_info & BTUSB_INTEL) {
30894160 hdev->manufacturer = 2;
....@@ -3091,6 +4162,7 @@
30914162 hdev->shutdown = btusb_shutdown_intel;
30924163 hdev->set_diag = btintel_set_diag_mfg;
30934164 hdev->set_bdaddr = btintel_set_bdaddr;
4165
+ hdev->cmd_timeout = btusb_intel_cmd_timeout;
30944166 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
30954167 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
30964168 set_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks);
....@@ -3100,15 +4172,26 @@
31004172 hdev->manufacturer = 2;
31014173 hdev->send = btusb_send_frame_intel;
31024174 hdev->setup = btusb_setup_intel_new;
4175
+ hdev->shutdown = btusb_shutdown_intel_new;
31034176 hdev->hw_error = btintel_hw_error;
31044177 hdev->set_diag = btintel_set_diag;
31054178 hdev->set_bdaddr = btintel_set_bdaddr;
4179
+ hdev->cmd_timeout = btusb_intel_cmd_timeout;
31064180 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
4181
+ set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
31074182 set_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks);
31084183 }
31094184
31104185 if (id->driver_info & BTUSB_MARVELL)
31114186 hdev->set_bdaddr = btusb_set_bdaddr_marvell;
4187
+
4188
+ if (IS_ENABLED(CONFIG_BT_HCIBTUSB_MTK) &&
4189
+ (id->driver_info & BTUSB_MEDIATEK)) {
4190
+ hdev->setup = btusb_mtk_setup;
4191
+ hdev->shutdown = btusb_mtk_shutdown;
4192
+ hdev->manufacturer = 70;
4193
+ set_bit(HCI_QUIRK_NON_PERSISTENT_SETUP, &hdev->quirks);
4194
+ }
31124195
31134196 if (id->driver_info & BTUSB_SWAVE) {
31144197 set_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks);
....@@ -3130,22 +4213,17 @@
31304213 if (id->driver_info & BTUSB_QCA_ROME) {
31314214 data->setup_on_usb = btusb_setup_qca;
31324215 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
4216
+ hdev->cmd_timeout = btusb_qca_cmd_timeout;
31334217 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
31344218 btusb_check_needs_reset_resume(intf);
31354219 }
31364220
3137
-#ifdef CONFIG_BT_HCIBTUSB_RTL
3138
- if (id->driver_info & BTUSB_REALTEK) {
3139
- hdev->setup = btrtl_setup_realtek;
3140
- hdev->shutdown = btrtl_shutdown_realtek;
3141
-
3142
- /* Realtek devices lose their updated firmware over suspend,
3143
- * but the USB hub doesn't notice any status change.
3144
- * Explicitly request a device reset on resume.
3145
- */
3146
- interface_to_usbdev(intf)->quirks |= USB_QUIRK_RESET_RESUME;
4221
+ if (id->driver_info & BTUSB_QCA_WCN6855) {
4222
+ data->setup_on_usb = btusb_setup_qca;
4223
+ hdev->set_bdaddr = btusb_set_bdaddr_wcn6855;
4224
+ hdev->cmd_timeout = btusb_qca_cmd_timeout;
4225
+ set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
31474226 }
3148
-#endif
31494227
31504228 if (id->driver_info & BTUSB_AMP) {
31514229 /* AMP controllers do not support SCO packets */
....@@ -3154,6 +4232,20 @@
31544232 /* Interface orders are hardcoded in the specification */
31554233 data->isoc = usb_ifnum_to_if(data->udev, ifnum_base + 1);
31564234 data->isoc_ifnum = ifnum_base + 1;
4235
+ }
4236
+
4237
+ if (IS_ENABLED(CONFIG_BT_HCIBTUSB_RTL) &&
4238
+ (id->driver_info & BTUSB_REALTEK)) {
4239
+ hdev->setup = btrtl_setup_realtek;
4240
+ hdev->shutdown = btrtl_shutdown_realtek;
4241
+ hdev->cmd_timeout = btusb_rtl_cmd_timeout;
4242
+
4243
+ /* Realtek devices lose their updated firmware over global
4244
+ * suspend that means host doesn't send SET_FEATURE
4245
+ * (DEVICE_REMOTE_WAKEUP)
4246
+ */
4247
+ set_bit(BTUSB_WAKEUP_DISABLE, &data->flags);
4248
+ set_bit(BTUSB_USE_ALT3_FOR_WBS, &data->flags);
31574249 }
31584250
31594251 if (!reset)
....@@ -3166,6 +4258,12 @@
31664258
31674259 if (id->driver_info & BTUSB_BROKEN_ISOC)
31684260 data->isoc = NULL;
4261
+
4262
+ if (id->driver_info & BTUSB_WIDEBAND_SPEECH)
4263
+ set_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED, &hdev->quirks);
4264
+
4265
+ if (id->driver_info & BTUSB_VALID_LE_STATES)
4266
+ set_bit(HCI_QUIRK_VALID_LE_STATES, &hdev->quirks);
31694267
31704268 if (id->driver_info & BTUSB_DIGIANSWER) {
31714269 data->cmdreq_type = USB_TYPE_VENDOR;
....@@ -3180,11 +4278,13 @@
31804278 if (bcdDevice < 0x117)
31814279 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
31824280
3183
- /* Fake CSR devices with broken commands */
3184
- if (bcdDevice <= 0x100 || bcdDevice == 0x134)
3185
- hdev->setup = btusb_setup_csr;
3186
-
4281
+ /* This must be set first in case we disable it for fakes */
31874282 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
4283
+
4284
+ /* Fake CSR devices with broken commands */
4285
+ if (le16_to_cpu(udev->descriptor.idVendor) == 0x0a12 &&
4286
+ le16_to_cpu(udev->descriptor.idProduct) == 0x0001)
4287
+ hdev->setup = btusb_setup_csr;
31884288 }
31894289
31904290 if (id->driver_info & BTUSB_SNIFFER) {
....@@ -3213,15 +4313,13 @@
32134313 goto out_free_dev;
32144314 }
32154315
3216
-#ifdef CONFIG_BT_HCIBTUSB_BCM
3217
- if (data->diag) {
4316
+ if (IS_ENABLED(CONFIG_BT_HCIBTUSB_BCM) && data->diag) {
32184317 if (!usb_driver_claim_interface(&btusb_driver,
32194318 data->diag, data))
32204319 __set_diag_interface(hdev);
32214320 else
32224321 data->diag = NULL;
32234322 }
3224
-#endif
32254323
32264324 if (enable_autosuspend)
32274325 usb_enable_autosuspend(data->udev);
....@@ -3235,6 +4333,8 @@
32354333 return 0;
32364334
32374335 out_free_dev:
4336
+ if (data->reset_gpio)
4337
+ gpiod_put(data->reset_gpio);
32384338 hci_free_dev(hdev);
32394339 return err;
32404340 }
....@@ -3278,6 +4378,9 @@
32784378 if (data->oob_wake_irq)
32794379 device_init_wakeup(&data->udev->dev, false);
32804380
4381
+ if (data->reset_gpio)
4382
+ gpiod_put(data->reset_gpio);
4383
+
32814384 hci_free_dev(hdev);
32824385 }
32834386
....@@ -3312,6 +4415,19 @@
33124415 enable_irq(data->oob_wake_irq);
33134416 }
33144417
4418
+ /* For global suspend, Realtek devices lose the loaded fw
4419
+ * in them. But for autosuspend, firmware should remain.
4420
+ * Actually, it depends on whether the usb host sends
4421
+ * set feature (enable wakeup) or not.
4422
+ */
4423
+ if (test_bit(BTUSB_WAKEUP_DISABLE, &data->flags)) {
4424
+ if (PMSG_IS_AUTO(message) &&
4425
+ device_can_wakeup(&data->udev->dev))
4426
+ data->udev->do_remote_wakeup = 1;
4427
+ else if (!PMSG_IS_AUTO(message))
4428
+ data->udev->reset_resume = 1;
4429
+ }
4430
+
33154431 return 0;
33164432 }
33174433