forked from ~ljy/RK356X_SDK_RELEASE

hc
2023-12-09 95099d4622f8cb224d94e314c7a8e0df60b13f87
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,77 @@
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 8822CE Bluetooth devices */
397
+ { USB_DEVICE(0x0bda, 0xb00c), .driver_info = BTUSB_REALTEK |
398
+ BTUSB_WIDEBAND_SPEECH },
399
+ { USB_DEVICE(0x0bda, 0xc822), .driver_info = BTUSB_REALTEK |
400
+ BTUSB_WIDEBAND_SPEECH },
401
+
402
+ /* Realtek 8852CE Bluetooth devices */
403
+ { USB_DEVICE(0x04ca, 0x4007), .driver_info = BTUSB_REALTEK |
404
+ BTUSB_WIDEBAND_SPEECH },
405
+ { USB_DEVICE(0x04c5, 0x1675), .driver_info = BTUSB_REALTEK |
406
+ BTUSB_WIDEBAND_SPEECH },
407
+ { USB_DEVICE(0x0cb8, 0xc558), .driver_info = BTUSB_REALTEK |
408
+ BTUSB_WIDEBAND_SPEECH },
409
+ { USB_DEVICE(0x13d3, 0x3587), .driver_info = BTUSB_REALTEK |
410
+ BTUSB_WIDEBAND_SPEECH },
411
+ { USB_DEVICE(0x13d3, 0x3586), .driver_info = BTUSB_REALTEK |
412
+ BTUSB_WIDEBAND_SPEECH },
413
+
360414 /* Realtek Bluetooth devices */
361415 { USB_VENDOR_AND_INTERFACE_INFO(0x0bda, 0xe0, 0x01, 0x01),
362416 .driver_info = BTUSB_REALTEK },
417
+
418
+ /* MediaTek Bluetooth devices */
419
+ { USB_VENDOR_AND_INTERFACE_INFO(0x0e8d, 0xe0, 0x01, 0x01),
420
+ .driver_info = BTUSB_MEDIATEK |
421
+ BTUSB_WIDEBAND_SPEECH |
422
+ BTUSB_VALID_LE_STATES },
423
+
424
+ /* MediaTek MT7922A Bluetooth devices */
425
+ { USB_DEVICE(0x0489, 0xe0d8), .driver_info = BTUSB_MEDIATEK |
426
+ BTUSB_WIDEBAND_SPEECH |
427
+ BTUSB_VALID_LE_STATES },
428
+ { USB_DEVICE(0x0489, 0xe0d9), .driver_info = BTUSB_MEDIATEK |
429
+ BTUSB_WIDEBAND_SPEECH |
430
+ BTUSB_VALID_LE_STATES },
431
+ { USB_DEVICE(0x13d3, 0x3568), .driver_info = BTUSB_MEDIATEK |
432
+ BTUSB_WIDEBAND_SPEECH |
433
+ BTUSB_VALID_LE_STATES },
363434
364435 /* Additional Realtek 8723AE Bluetooth devices */
365436 { USB_DEVICE(0x0930, 0x021d), .driver_info = BTUSB_REALTEK },
....@@ -393,6 +464,7 @@
393464
394465 /* Additional Realtek 8822CE Bluetooth devices */
395466 { USB_DEVICE(0x04ca, 0x4005), .driver_info = BTUSB_REALTEK },
467
+ { USB_DEVICE(0x13d3, 0x3548), .driver_info = BTUSB_REALTEK },
396468
397469 /* Silicon Wave based devices */
398470 { USB_DEVICE(0x0c10, 0x0000), .driver_info = BTUSB_SWAVE },
....@@ -443,6 +515,10 @@
443515 #define BTUSB_BOOTING 9
444516 #define BTUSB_DIAG_RUNNING 10
445517 #define BTUSB_OOB_WAKE_ENABLED 11
518
+#define BTUSB_HW_RESET_ACTIVE 12
519
+#define BTUSB_TX_WAIT_VND_EVT 13
520
+#define BTUSB_WAKEUP_DISABLE 14
521
+#define BTUSB_USE_ALT3_FOR_WBS 15
446522
447523 struct btusb_data {
448524 struct hci_dev *hdev;
....@@ -466,6 +542,7 @@
466542 struct usb_anchor bulk_anchor;
467543 struct usb_anchor isoc_anchor;
468544 struct usb_anchor diag_anchor;
545
+ struct usb_anchor ctrl_anchor;
469546 spinlock_t rxlock;
470547
471548 struct sk_buff *evt_skb;
....@@ -480,10 +557,14 @@
480557 struct usb_endpoint_descriptor *diag_tx_ep;
481558 struct usb_endpoint_descriptor *diag_rx_ep;
482559
560
+ struct gpio_desc *reset_gpio;
561
+
483562 __u8 cmdreq_type;
484563 __u8 cmdreq;
485564
486565 unsigned int sco_num;
566
+ unsigned int air_mode;
567
+ bool usb_alt6_packet_flow;
487568 int isoc_altsetting;
488569 int suspend_count;
489570
....@@ -493,7 +574,86 @@
493574 int (*setup_on_usb)(struct hci_dev *hdev);
494575
495576 int oob_wake_irq; /* irq for out-of-band wake-on-bt */
577
+ unsigned cmd_timeout_cnt;
496578 };
579
+
580
+static void btusb_intel_cmd_timeout(struct hci_dev *hdev)
581
+{
582
+ struct btusb_data *data = hci_get_drvdata(hdev);
583
+ struct gpio_desc *reset_gpio = data->reset_gpio;
584
+
585
+ if (++data->cmd_timeout_cnt < 5)
586
+ return;
587
+
588
+ if (!reset_gpio) {
589
+ bt_dev_err(hdev, "No way to reset. Ignoring and continuing");
590
+ return;
591
+ }
592
+
593
+ /*
594
+ * Toggle the hard reset line if the platform provides one. The reset
595
+ * is going to yank the device off the USB and then replug. So doing
596
+ * once is enough. The cleanup is handled correctly on the way out
597
+ * (standard USB disconnect), and the new device is detected cleanly
598
+ * and bound to the driver again like it should be.
599
+ */
600
+ if (test_and_set_bit(BTUSB_HW_RESET_ACTIVE, &data->flags)) {
601
+ bt_dev_err(hdev, "last reset failed? Not resetting again");
602
+ return;
603
+ }
604
+
605
+ bt_dev_err(hdev, "Initiating HW reset via gpio");
606
+ gpiod_set_value_cansleep(reset_gpio, 1);
607
+ msleep(100);
608
+ gpiod_set_value_cansleep(reset_gpio, 0);
609
+}
610
+
611
+static void btusb_rtl_cmd_timeout(struct hci_dev *hdev)
612
+{
613
+ struct btusb_data *data = hci_get_drvdata(hdev);
614
+ struct gpio_desc *reset_gpio = data->reset_gpio;
615
+
616
+ if (++data->cmd_timeout_cnt < 5)
617
+ return;
618
+
619
+ if (!reset_gpio) {
620
+ bt_dev_err(hdev, "No gpio to reset Realtek device, ignoring");
621
+ return;
622
+ }
623
+
624
+ /* Toggle the hard reset line. The Realtek device is going to
625
+ * yank itself off the USB and then replug. The cleanup is handled
626
+ * correctly on the way out (standard USB disconnect), and the new
627
+ * device is detected cleanly and bound to the driver again like
628
+ * it should be.
629
+ */
630
+ if (test_and_set_bit(BTUSB_HW_RESET_ACTIVE, &data->flags)) {
631
+ bt_dev_err(hdev, "last reset failed? Not resetting again");
632
+ return;
633
+ }
634
+
635
+ bt_dev_err(hdev, "Reset Realtek device via gpio");
636
+ gpiod_set_value_cansleep(reset_gpio, 1);
637
+ msleep(200);
638
+ gpiod_set_value_cansleep(reset_gpio, 0);
639
+}
640
+
641
+static void btusb_qca_cmd_timeout(struct hci_dev *hdev)
642
+{
643
+ struct btusb_data *data = hci_get_drvdata(hdev);
644
+ int err;
645
+
646
+ if (++data->cmd_timeout_cnt < 5)
647
+ return;
648
+
649
+ bt_dev_err(hdev, "Multiple cmd timeouts seen. Resetting usb device.");
650
+ /* This is not an unbalanced PM reference since the device will reset */
651
+ err = usb_autopm_get_interface(data->intf);
652
+ if (!err)
653
+ usb_queue_reset_device(data->intf);
654
+ else
655
+ bt_dev_err(hdev, "Failed usb_autopm_get_interface with %d", err);
656
+}
497657
498658 static inline void btusb_free_frags(struct btusb_data *data)
499659 {
....@@ -912,6 +1072,42 @@
9121072 }
9131073 }
9141074
1075
+static inline void __fill_isoc_descriptor_msbc(struct urb *urb, int len,
1076
+ int mtu, struct btusb_data *data)
1077
+{
1078
+ int i, offset = 0;
1079
+ unsigned int interval;
1080
+
1081
+ BT_DBG("len %d mtu %d", len, mtu);
1082
+
1083
+ /* For mSBC ALT 6 setting the host will send the packet at continuous
1084
+ * flow. As per core spec 5, vol 4, part B, table 2.1. For ALT setting
1085
+ * 6 the HCI PACKET INTERVAL should be 7.5ms for every usb packets.
1086
+ * To maintain the rate we send 63bytes of usb packets alternatively for
1087
+ * 7ms and 8ms to maintain the rate as 7.5ms.
1088
+ */
1089
+ if (data->usb_alt6_packet_flow) {
1090
+ interval = 7;
1091
+ data->usb_alt6_packet_flow = false;
1092
+ } else {
1093
+ interval = 6;
1094
+ data->usb_alt6_packet_flow = true;
1095
+ }
1096
+
1097
+ for (i = 0; i < interval; i++) {
1098
+ urb->iso_frame_desc[i].offset = offset;
1099
+ urb->iso_frame_desc[i].length = offset;
1100
+ }
1101
+
1102
+ if (len && i < BTUSB_MAX_ISOC_FRAMES) {
1103
+ urb->iso_frame_desc[i].offset = offset;
1104
+ urb->iso_frame_desc[i].length = len;
1105
+ i++;
1106
+ }
1107
+
1108
+ urb->number_of_packets = i;
1109
+}
1110
+
9151111 static inline void __fill_isoc_descriptor(struct urb *urb, int len, int mtu)
9161112 {
9171113 int i, offset = 0;
....@@ -1143,6 +1339,13 @@
11431339
11441340 data->intf->needs_remote_wakeup = 1;
11451341
1342
+ /* Disable device remote wakeup when host is suspended
1343
+ * For Realtek chips, global suspend without
1344
+ * SET_FEATURE (DEVICE_REMOTE_WAKEUP) can save more power in device.
1345
+ */
1346
+ if (test_bit(BTUSB_WAKEUP_DISABLE, &data->flags))
1347
+ device_wakeup_disable(&data->udev->dev);
1348
+
11461349 if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
11471350 goto done;
11481351
....@@ -1181,6 +1384,7 @@
11811384 usb_kill_anchored_urbs(&data->bulk_anchor);
11821385 usb_kill_anchored_urbs(&data->isoc_anchor);
11831386 usb_kill_anchored_urbs(&data->diag_anchor);
1387
+ usb_kill_anchored_urbs(&data->ctrl_anchor);
11841388 }
11851389
11861390 static int btusb_close(struct hci_dev *hdev)
....@@ -1206,6 +1410,11 @@
12061410 goto failed;
12071411
12081412 data->intf->needs_remote_wakeup = 0;
1413
+
1414
+ /* Enable remote wake up for auto-suspend */
1415
+ if (test_bit(BTUSB_WAKEUP_DISABLE, &data->flags))
1416
+ data->intf->needs_remote_wakeup = 1;
1417
+
12091418 usb_autopm_put_interface(data->intf);
12101419
12111420 failed:
....@@ -1302,9 +1511,13 @@
13021511
13031512 urb->transfer_flags = URB_ISO_ASAP;
13041513
1305
- __fill_isoc_descriptor(urb, skb->len,
1306
- le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
1307
-
1514
+ if (data->isoc_altsetting == 6)
1515
+ __fill_isoc_descriptor_msbc(urb, skb->len,
1516
+ le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize),
1517
+ data);
1518
+ else
1519
+ __fill_isoc_descriptor(urb, skb->len,
1520
+ le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
13081521 skb->dev = (void *)hdev;
13091522
13101523 return urb;
....@@ -1400,6 +1613,7 @@
14001613
14011614 if (hci_conn_num(hdev, SCO_LINK) != data->sco_num) {
14021615 data->sco_num = hci_conn_num(hdev, SCO_LINK);
1616
+ data->air_mode = evt;
14031617 schedule_work(&data->work);
14041618 }
14051619 }
....@@ -1447,11 +1661,70 @@
14471661 return 0;
14481662 }
14491663
1664
+static int btusb_switch_alt_setting(struct hci_dev *hdev, int new_alts)
1665
+{
1666
+ struct btusb_data *data = hci_get_drvdata(hdev);
1667
+ int err;
1668
+
1669
+ if (data->isoc_altsetting != new_alts) {
1670
+ unsigned long flags;
1671
+
1672
+ clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1673
+ usb_kill_anchored_urbs(&data->isoc_anchor);
1674
+
1675
+ /* When isochronous alternate setting needs to be
1676
+ * changed, because SCO connection has been added
1677
+ * or removed, a packet fragment may be left in the
1678
+ * reassembling state. This could lead to wrongly
1679
+ * assembled fragments.
1680
+ *
1681
+ * Clear outstanding fragment when selecting a new
1682
+ * alternate setting.
1683
+ */
1684
+ spin_lock_irqsave(&data->rxlock, flags);
1685
+ kfree_skb(data->sco_skb);
1686
+ data->sco_skb = NULL;
1687
+ spin_unlock_irqrestore(&data->rxlock, flags);
1688
+
1689
+ err = __set_isoc_interface(hdev, new_alts);
1690
+ if (err < 0)
1691
+ return err;
1692
+ }
1693
+
1694
+ if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
1695
+ if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0)
1696
+ clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1697
+ else
1698
+ btusb_submit_isoc_urb(hdev, GFP_KERNEL);
1699
+ }
1700
+
1701
+ return 0;
1702
+}
1703
+
1704
+static struct usb_host_interface *btusb_find_altsetting(struct btusb_data *data,
1705
+ int alt)
1706
+{
1707
+ struct usb_interface *intf = data->isoc;
1708
+ int i;
1709
+
1710
+ BT_DBG("Looking for Alt no :%d", alt);
1711
+
1712
+ if (!intf)
1713
+ return NULL;
1714
+
1715
+ for (i = 0; i < intf->num_altsetting; i++) {
1716
+ if (intf->altsetting[i].desc.bAlternateSetting == alt)
1717
+ return &intf->altsetting[i];
1718
+ }
1719
+
1720
+ return NULL;
1721
+}
1722
+
14501723 static void btusb_work(struct work_struct *work)
14511724 {
14521725 struct btusb_data *data = container_of(work, struct btusb_data, work);
14531726 struct hci_dev *hdev = data->hdev;
1454
- int new_alts;
1727
+ int new_alts = 0;
14551728 int err;
14561729
14571730 if (data->sco_num > 0) {
....@@ -1466,44 +1739,36 @@
14661739 set_bit(BTUSB_DID_ISO_RESUME, &data->flags);
14671740 }
14681741
1469
- if (hdev->voice_setting & 0x0020) {
1470
- static const int alts[3] = { 2, 4, 5 };
1742
+ if (data->air_mode == HCI_NOTIFY_ENABLE_SCO_CVSD) {
1743
+ if (hdev->voice_setting & 0x0020) {
1744
+ static const int alts[3] = { 2, 4, 5 };
14711745
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.
1746
+ new_alts = alts[data->sco_num - 1];
1747
+ } else {
1748
+ new_alts = data->sco_num;
1749
+ }
1750
+ } else if (data->air_mode == HCI_NOTIFY_ENABLE_SCO_TRANSP) {
1751
+ /* Bluetooth USB spec recommends alt 6 (63 bytes), but
1752
+ * many adapters do not support it. Alt 1 appears to
1753
+ * work for all adapters that do not have alt 6, and
1754
+ * which work with WBS at all. Some devices prefer
1755
+ * alt 3 (HCI payload >= 60 Bytes let air packet
1756
+ * data satisfy 60 bytes), requiring
1757
+ * MTU >= 3 (packets) * 25 (size) - 3 (headers) = 72
1758
+ * see also Core spec 5, vol 4, B 2.1.1 & Table 2.1.
14911759 */
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);
1760
+ if (btusb_find_altsetting(data, 6))
1761
+ new_alts = 6;
1762
+ else if (btusb_find_altsetting(data, 3) &&
1763
+ hdev->sco_mtu >= 72 &&
1764
+ test_bit(BTUSB_USE_ALT3_FOR_WBS, &data->flags))
1765
+ new_alts = 3;
15041766 else
1505
- btusb_submit_isoc_urb(hdev, GFP_KERNEL);
1767
+ new_alts = 1;
15061768 }
1769
+
1770
+ if (btusb_switch_alt_setting(hdev, new_alts) < 0)
1771
+ bt_dev_err(hdev, "set USB alt:(%d) failed!", new_alts);
15071772 } else {
15081773 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
15091774 usb_kill_anchored_urbs(&data->isoc_anchor);
....@@ -1544,8 +1809,11 @@
15441809
15451810 static int btusb_setup_csr(struct hci_dev *hdev)
15461811 {
1812
+ struct btusb_data *data = hci_get_drvdata(hdev);
1813
+ u16 bcdDevice = le16_to_cpu(data->udev->descriptor.bcdDevice);
15471814 struct hci_rp_read_local_version *rp;
15481815 struct sk_buff *skb;
1816
+ bool is_fake = false;
15491817
15501818 BT_DBG("%s", hdev->name);
15511819
....@@ -1565,18 +1833,80 @@
15651833
15661834 rp = (struct hci_rp_read_local_version *)skb->data;
15671835
1568
- /* Detect controllers which aren't real CSR ones. */
1836
+ bt_dev_info(hdev, "CSR: Setting up dongle with HCI ver=%u rev=%04x; LMP ver=%u subver=%04x; manufacturer=%u",
1837
+ le16_to_cpu(rp->hci_ver), le16_to_cpu(rp->hci_rev),
1838
+ le16_to_cpu(rp->lmp_ver), le16_to_cpu(rp->lmp_subver),
1839
+ le16_to_cpu(rp->manufacturer));
1840
+
1841
+ /* Detect a wide host of Chinese controllers that aren't CSR.
1842
+ *
1843
+ * Known fake bcdDevices: 0x0100, 0x0134, 0x1915, 0x2520, 0x7558, 0x8891
1844
+ *
1845
+ * The main thing they have in common is that these are really popular low-cost
1846
+ * options that support newer Bluetooth versions but rely on heavy VID/PID
1847
+ * squatting of this poor old Bluetooth 1.1 device. Even sold as such.
1848
+ *
1849
+ * We detect actual CSR devices by checking that the HCI manufacturer code
1850
+ * is Cambridge Silicon Radio (10) and ensuring that LMP sub-version and
1851
+ * HCI rev values always match. As they both store the firmware number.
1852
+ */
15691853 if (le16_to_cpu(rp->manufacturer) != 10 ||
1570
- le16_to_cpu(rp->lmp_subver) == 0x0c5c) {
1854
+ le16_to_cpu(rp->hci_rev) != le16_to_cpu(rp->lmp_subver))
1855
+ is_fake = true;
1856
+
1857
+ /* Known legit CSR firmware build numbers and their supported BT versions:
1858
+ * - 1.1 (0x1) -> 0x0073, 0x020d, 0x033c, 0x034e
1859
+ * - 1.2 (0x2) -> 0x04d9, 0x0529
1860
+ * - 2.0 (0x3) -> 0x07a6, 0x07ad, 0x0c5c
1861
+ * - 2.1 (0x4) -> 0x149c, 0x1735, 0x1899 (0x1899 is a BlueCore4-External)
1862
+ * - 4.0 (0x6) -> 0x1d86, 0x2031, 0x22bb
1863
+ *
1864
+ * e.g. Real CSR dongles with LMP subversion 0x73 are old enough that
1865
+ * support BT 1.1 only; so it's a dead giveaway when some
1866
+ * third-party BT 4.0 dongle reuses it.
1867
+ */
1868
+ else if (le16_to_cpu(rp->lmp_subver) <= 0x034e &&
1869
+ le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_1_1)
1870
+ is_fake = true;
1871
+
1872
+ else if (le16_to_cpu(rp->lmp_subver) <= 0x0529 &&
1873
+ le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_1_2)
1874
+ is_fake = true;
1875
+
1876
+ else if (le16_to_cpu(rp->lmp_subver) <= 0x0c5c &&
1877
+ le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_2_0)
1878
+ is_fake = true;
1879
+
1880
+ else if (le16_to_cpu(rp->lmp_subver) <= 0x1899 &&
1881
+ le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_2_1)
1882
+ is_fake = true;
1883
+
1884
+ else if (le16_to_cpu(rp->lmp_subver) <= 0x22bb &&
1885
+ le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_4_0)
1886
+ is_fake = true;
1887
+
1888
+ /* Other clones which beat all the above checks */
1889
+ else if (bcdDevice == 0x0134 &&
1890
+ le16_to_cpu(rp->lmp_subver) == 0x0c5c &&
1891
+ le16_to_cpu(rp->hci_ver) == BLUETOOTH_VER_2_0)
1892
+ is_fake = true;
1893
+
1894
+ if (is_fake) {
1895
+ bt_dev_warn(hdev, "CSR: Unbranded CSR clone detected; adding workarounds...");
1896
+
1897
+ /* Generally these clones have big discrepancies between
1898
+ * advertised features and what's actually supported.
1899
+ * Probably will need to be expanded in the future;
1900
+ * without these the controller will lock up.
1901
+ */
1902
+ set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
1903
+ set_bit(HCI_QUIRK_BROKEN_ERR_DATA_REPORTING, &hdev->quirks);
1904
+
15711905 /* Clear the reset quirk since this is not an actual
15721906 * early Bluetooth 1.1 device from CSR.
15731907 */
15741908 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);
1909
+ clear_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
15801910 }
15811911
15821912 kfree_skb(skb);
....@@ -1854,7 +2184,14 @@
18542184 if (err)
18552185 return err;
18562186
1857
- bt_dev_info(hdev, "Intel firmware patch completed and activated");
2187
+ /* Need build number for downloaded fw patches in
2188
+ * every power-on boot
2189
+ */
2190
+ err = btintel_read_version(hdev, &ver);
2191
+ if (err)
2192
+ return err;
2193
+ bt_dev_info(hdev, "Intel BT fw patch 0x%02x completed & activated",
2194
+ ver.fw_patch_num);
18582195
18592196 goto complete;
18602197
....@@ -1936,10 +2273,8 @@
19362273 if (len != sizeof(*evt))
19372274 return;
19382275
1939
- if (test_and_clear_bit(BTUSB_BOOTING, &data->flags)) {
1940
- smp_mb__after_atomic();
2276
+ if (test_and_clear_bit(BTUSB_BOOTING, &data->flags))
19412277 wake_up_bit(&data->flags, BTUSB_BOOTING);
1942
- }
19432278 }
19442279
19452280 static void btusb_intel_secure_send_result(struct btusb_data *data,
....@@ -1954,10 +2289,8 @@
19542289 set_bit(BTUSB_FIRMWARE_FAILED, &data->flags);
19552290
19562291 if (test_and_clear_bit(BTUSB_DOWNLOADING, &data->flags) &&
1957
- test_bit(BTUSB_FIRMWARE_LOADED, &data->flags)) {
1958
- smp_mb__after_atomic();
2292
+ test_bit(BTUSB_FIRMWARE_LOADED, &data->flags))
19592293 wake_up_bit(&data->flags, BTUSB_DOWNLOADING);
1960
- }
19612294 }
19622295
19632296 static int btusb_recv_event_intel(struct hci_dev *hdev, struct sk_buff *skb)
....@@ -2085,42 +2418,25 @@
20852418 return true;
20862419 }
20872420
2088
-static int btusb_setup_intel_new(struct hci_dev *hdev)
2421
+static int btusb_intel_download_firmware(struct hci_dev *hdev,
2422
+ struct intel_version *ver,
2423
+ struct intel_boot_params *params,
2424
+ u32 *boot_param)
20892425 {
2090
- struct btusb_data *data = hci_get_drvdata(hdev);
2091
- struct intel_version ver;
2092
- struct intel_boot_params params;
20932426 const struct firmware *fw;
2094
- u32 boot_param;
20952427 char fwname[64];
2096
- ktime_t calltime, delta, rettime;
2097
- unsigned long long duration;
20982428 int err;
2429
+ struct btusb_data *data = hci_get_drvdata(hdev);
20992430
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;
2431
+ if (!ver || !params)
2432
+ return -EINVAL;
21172433
21182434 /* The hardware platform number has a fixed value of 0x37 and
21192435 * for now only accept this single value.
21202436 */
2121
- if (ver.hw_platform != 0x37) {
2437
+ if (ver->hw_platform != 0x37) {
21222438 bt_dev_err(hdev, "Unsupported Intel hardware platform (%u)",
2123
- ver.hw_platform);
2439
+ ver->hw_platform);
21242440 return -EINVAL;
21252441 }
21262442
....@@ -2130,7 +2446,7 @@
21302446 * This check has been put in place to ensure correct forward
21312447 * compatibility options when newer hardware variants come along.
21322448 */
2133
- switch (ver.hw_variant) {
2449
+ switch (ver->hw_variant) {
21342450 case 0x0b: /* SfP */
21352451 case 0x0c: /* WsP */
21362452 case 0x11: /* JfP */
....@@ -2140,11 +2456,11 @@
21402456 break;
21412457 default:
21422458 bt_dev_err(hdev, "Unsupported Intel hardware variant (%u)",
2143
- ver.hw_variant);
2459
+ ver->hw_variant);
21442460 return -EINVAL;
21452461 }
21462462
2147
- btintel_version_info(hdev, &ver);
2463
+ btintel_version_info(hdev, ver);
21482464
21492465 /* The firmware variant determines if the device is in bootloader
21502466 * mode or is running operational firmware. The value 0x06 identifies
....@@ -2159,7 +2475,7 @@
21592475 * It is not possible to use the Secure Boot Parameters in this
21602476 * case since that command is only available in bootloader mode.
21612477 */
2162
- if (ver.fw_variant == 0x23) {
2478
+ if (ver->fw_variant == 0x23) {
21632479 clear_bit(BTUSB_BOOTLOADER, &data->flags);
21642480 btintel_check_bdaddr(hdev);
21652481 return 0;
....@@ -2168,16 +2484,16 @@
21682484 /* If the device is not in bootloader mode, then the only possible
21692485 * choice is to return an error and abort the device initialization.
21702486 */
2171
- if (ver.fw_variant != 0x06) {
2487
+ if (ver->fw_variant != 0x06) {
21722488 bt_dev_err(hdev, "Unsupported Intel firmware variant (%u)",
2173
- ver.fw_variant);
2489
+ ver->fw_variant);
21742490 return -ENODEV;
21752491 }
21762492
21772493 /* Read the secure boot parameters to identify the operating
21782494 * details of the bootloader.
21792495 */
2180
- err = btintel_read_boot_params(hdev, &params);
2496
+ err = btintel_read_boot_params(hdev, params);
21812497 if (err)
21822498 return err;
21832499
....@@ -2185,16 +2501,16 @@
21852501 * with a command complete event. If the boot parameters indicate
21862502 * that this bootloader does not send them, then abort the setup.
21872503 */
2188
- if (params.limited_cce != 0x00) {
2504
+ if (params->limited_cce != 0x00) {
21892505 bt_dev_err(hdev, "Unsupported Intel firmware loading method (%u)",
2190
- params.limited_cce);
2506
+ params->limited_cce);
21912507 return -EINVAL;
21922508 }
21932509
21942510 /* If the OTP has no valid Bluetooth device address, then there will
21952511 * also be no valid address for the operational firmware.
21962512 */
2197
- if (!bacmp(&params.otp_bdaddr, BDADDR_ANY)) {
2513
+ if (!bacmp(&params->otp_bdaddr, BDADDR_ANY)) {
21982514 bt_dev_info(hdev, "No device address configured");
21992515 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
22002516 }
....@@ -2220,7 +2536,7 @@
22202536 * ibt-<hw_variant>-<hw_revision>-<fw_revision>.sfi.
22212537 *
22222538 */
2223
- err = btusb_setup_intel_new_get_fw_name(&ver, &params, fwname,
2539
+ err = btusb_setup_intel_new_get_fw_name(ver, params, fwname,
22242540 sizeof(fwname), "sfi");
22252541 if (!err) {
22262542 bt_dev_err(hdev, "Unsupported Intel firmware naming");
....@@ -2235,16 +2551,6 @@
22352551
22362552 bt_dev_info(hdev, "Found device firmware: %s", fwname);
22372553
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
-
22482554 if (fw->size < 644) {
22492555 bt_dev_err(hdev, "Invalid size of firmware file (%zu)",
22502556 fw->size);
....@@ -2255,10 +2561,14 @@
22552561 set_bit(BTUSB_DOWNLOADING, &data->flags);
22562562
22572563 /* Start firmware downloading and get boot parameter */
2258
- err = btintel_download_firmware(hdev, fw, &boot_param);
2259
- if (err < 0)
2564
+ err = btintel_download_firmware(hdev, fw, boot_param);
2565
+ if (err < 0) {
2566
+ /* When FW download fails, send Intel Reset to retry
2567
+ * FW download.
2568
+ */
2569
+ btintel_reset_to_bootloader(hdev);
22602570 goto done;
2261
-
2571
+ }
22622572 set_bit(BTUSB_FIRMWARE_LOADED, &data->flags);
22632573
22642574 bt_dev_info(hdev, "Waiting for firmware download to complete");
....@@ -2285,6 +2595,7 @@
22852595 if (err) {
22862596 bt_dev_err(hdev, "Firmware loading timeout");
22872597 err = -ETIMEDOUT;
2598
+ btintel_reset_to_bootloader(hdev);
22882599 goto done;
22892600 }
22902601
....@@ -2294,25 +2605,68 @@
22942605 goto done;
22952606 }
22962607
2608
+done:
2609
+ release_firmware(fw);
2610
+ return err;
2611
+}
2612
+
2613
+static int btusb_setup_intel_new(struct hci_dev *hdev)
2614
+{
2615
+ struct btusb_data *data = hci_get_drvdata(hdev);
2616
+ struct intel_version ver;
2617
+ struct intel_boot_params params;
2618
+ u32 boot_param;
2619
+ char ddcname[64];
2620
+ ktime_t calltime, delta, rettime;
2621
+ unsigned long long duration;
2622
+ int err;
2623
+ struct intel_debug_features features;
2624
+
2625
+ BT_DBG("%s", hdev->name);
2626
+
2627
+ /* Set the default boot parameter to 0x0 and it is updated to
2628
+ * SKU specific boot parameter after reading Intel_Write_Boot_Params
2629
+ * command while downloading the firmware.
2630
+ */
2631
+ boot_param = 0x00000000;
2632
+
2633
+ calltime = ktime_get();
2634
+
2635
+ /* Read the Intel version information to determine if the device
2636
+ * is in bootloader mode or if it already has operational firmware
2637
+ * loaded.
2638
+ */
2639
+ err = btintel_read_version(hdev, &ver);
2640
+ if (err) {
2641
+ bt_dev_err(hdev, "Intel Read version failed (%d)", err);
2642
+ btintel_reset_to_bootloader(hdev);
2643
+ return err;
2644
+ }
2645
+
2646
+ err = btusb_intel_download_firmware(hdev, &ver, &params, &boot_param);
2647
+ if (err)
2648
+ return err;
2649
+
2650
+ /* controller is already having an operational firmware */
2651
+ if (ver.fw_variant == 0x23)
2652
+ goto finish;
2653
+
22972654 rettime = ktime_get();
22982655 delta = ktime_sub(rettime, calltime);
22992656 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
23002657
23012658 bt_dev_info(hdev, "Firmware loaded in %llu usecs", duration);
23022659
2303
-done:
2304
- release_firmware(fw);
2305
-
2306
- if (err < 0)
2307
- return err;
2308
-
23092660 calltime = ktime_get();
23102661
23112662 set_bit(BTUSB_BOOTING, &data->flags);
23122663
23132664 err = btintel_send_intel_reset(hdev, boot_param);
2314
- if (err)
2665
+ if (err) {
2666
+ bt_dev_err(hdev, "Intel Soft Reset failed (%d)", err);
2667
+ btintel_reset_to_bootloader(hdev);
23152668 return err;
2669
+ }
23162670
23172671 /* The bootloader will not indicate when the device is ready. This
23182672 * is done by the operational firmware sending bootup notification.
....@@ -2334,6 +2688,7 @@
23342688
23352689 if (err) {
23362690 bt_dev_err(hdev, "Device boot timeout");
2691
+ btintel_reset_to_bootloader(hdev);
23372692 return -ETIMEDOUT;
23382693 }
23392694
....@@ -2345,13 +2700,45 @@
23452700
23462701 clear_bit(BTUSB_BOOTLOADER, &data->flags);
23472702
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.
2703
+ err = btusb_setup_intel_new_get_fw_name(&ver, &params, ddcname,
2704
+ sizeof(ddcname), "ddc");
2705
+
2706
+ if (!err) {
2707
+ bt_dev_err(hdev, "Unsupported Intel firmware naming");
2708
+ } else {
2709
+ /* Once the device is running in operational mode, it needs to
2710
+ * apply the device configuration (DDC) parameters.
2711
+ *
2712
+ * The device can work without DDC parameters, so even if it
2713
+ * fails to load the file, no need to fail the setup.
2714
+ */
2715
+ btintel_load_ddc_config(hdev, ddcname);
2716
+ }
2717
+
2718
+ /* Read the Intel supported features and if new exception formats
2719
+ * supported, need to load the additional DDC config to enable.
23532720 */
2354
- btintel_load_ddc_config(hdev, fwname);
2721
+ btintel_read_debug_features(hdev, &features);
2722
+
2723
+ /* Set DDC mask for available debug features */
2724
+ btintel_set_debug_features(hdev, &features);
2725
+
2726
+ /* Read the Intel version information after loading the FW */
2727
+ err = btintel_read_version(hdev, &ver);
2728
+ if (err)
2729
+ return err;
2730
+
2731
+ btintel_version_info(hdev, &ver);
2732
+
2733
+finish:
2734
+ /* All Intel controllers that support the Microsoft vendor
2735
+ * extension are using 0xFC1E for VsMsftOpCode.
2736
+ */
2737
+ switch (ver.hw_variant) {
2738
+ case 0x12: /* ThP */
2739
+ hci_set_msft_opcode(hdev, 0xFC1E);
2740
+ break;
2741
+ }
23552742
23562743 /* Set the event mask for Intel specific vendor events. This enables
23572744 * a few extra events that are useful during general operation. It
....@@ -2400,6 +2787,626 @@
24002787
24012788 return 0;
24022789 }
2790
+
2791
+static int btusb_shutdown_intel_new(struct hci_dev *hdev)
2792
+{
2793
+ struct sk_buff *skb;
2794
+
2795
+ /* Send HCI Reset to the controller to stop any BT activity which
2796
+ * were triggered. This will help to save power and maintain the
2797
+ * sync b/w Host and controller
2798
+ */
2799
+ skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
2800
+ if (IS_ERR(skb)) {
2801
+ bt_dev_err(hdev, "HCI reset during shutdown failed");
2802
+ return PTR_ERR(skb);
2803
+ }
2804
+ kfree_skb(skb);
2805
+
2806
+ return 0;
2807
+}
2808
+
2809
+#define FIRMWARE_MT7663 "mediatek/mt7663pr2h.bin"
2810
+#define FIRMWARE_MT7668 "mediatek/mt7668pr2h.bin"
2811
+
2812
+#define HCI_WMT_MAX_EVENT_SIZE 64
2813
+
2814
+enum {
2815
+ BTMTK_WMT_PATCH_DWNLD = 0x1,
2816
+ BTMTK_WMT_FUNC_CTRL = 0x6,
2817
+ BTMTK_WMT_RST = 0x7,
2818
+ BTMTK_WMT_SEMAPHORE = 0x17,
2819
+};
2820
+
2821
+enum {
2822
+ BTMTK_WMT_INVALID,
2823
+ BTMTK_WMT_PATCH_UNDONE,
2824
+ BTMTK_WMT_PATCH_PROGRESS,
2825
+ BTMTK_WMT_PATCH_DONE,
2826
+ BTMTK_WMT_ON_UNDONE,
2827
+ BTMTK_WMT_ON_DONE,
2828
+ BTMTK_WMT_ON_PROGRESS,
2829
+};
2830
+
2831
+struct btmtk_wmt_hdr {
2832
+ u8 dir;
2833
+ u8 op;
2834
+ __le16 dlen;
2835
+ u8 flag;
2836
+} __packed;
2837
+
2838
+struct btmtk_hci_wmt_cmd {
2839
+ struct btmtk_wmt_hdr hdr;
2840
+ u8 data[];
2841
+} __packed;
2842
+
2843
+struct btmtk_hci_wmt_evt {
2844
+ struct hci_event_hdr hhdr;
2845
+ struct btmtk_wmt_hdr whdr;
2846
+} __packed;
2847
+
2848
+struct btmtk_hci_wmt_evt_funcc {
2849
+ struct btmtk_hci_wmt_evt hwhdr;
2850
+ __be16 status;
2851
+} __packed;
2852
+
2853
+struct btmtk_tci_sleep {
2854
+ u8 mode;
2855
+ __le16 duration;
2856
+ __le16 host_duration;
2857
+ u8 host_wakeup_pin;
2858
+ u8 time_compensation;
2859
+} __packed;
2860
+
2861
+struct btmtk_hci_wmt_params {
2862
+ u8 op;
2863
+ u8 flag;
2864
+ u16 dlen;
2865
+ const void *data;
2866
+ u32 *status;
2867
+};
2868
+
2869
+static void btusb_mtk_wmt_recv(struct urb *urb)
2870
+{
2871
+ struct hci_dev *hdev = urb->context;
2872
+ struct btusb_data *data = hci_get_drvdata(hdev);
2873
+ struct hci_event_hdr *hdr;
2874
+ struct sk_buff *skb;
2875
+ int err;
2876
+
2877
+ if (urb->status == 0 && urb->actual_length > 0) {
2878
+ hdev->stat.byte_rx += urb->actual_length;
2879
+
2880
+ /* WMT event shouldn't be fragmented and the size should be
2881
+ * less than HCI_WMT_MAX_EVENT_SIZE.
2882
+ */
2883
+ skb = bt_skb_alloc(HCI_WMT_MAX_EVENT_SIZE, GFP_ATOMIC);
2884
+ if (!skb) {
2885
+ hdev->stat.err_rx++;
2886
+ kfree(urb->setup_packet);
2887
+ return;
2888
+ }
2889
+
2890
+ hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
2891
+ skb_put_data(skb, urb->transfer_buffer, urb->actual_length);
2892
+
2893
+ hdr = (void *)skb->data;
2894
+ /* Fix up the vendor event id with 0xff for vendor specific
2895
+ * instead of 0xe4 so that event send via monitoring socket can
2896
+ * be parsed properly.
2897
+ */
2898
+ hdr->evt = 0xff;
2899
+
2900
+ /* When someone waits for the WMT event, the skb is being cloned
2901
+ * and being processed the events from there then.
2902
+ */
2903
+ if (test_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags)) {
2904
+ data->evt_skb = skb_clone(skb, GFP_ATOMIC);
2905
+ if (!data->evt_skb) {
2906
+ kfree_skb(skb);
2907
+ kfree(urb->setup_packet);
2908
+ return;
2909
+ }
2910
+ }
2911
+
2912
+ err = hci_recv_frame(hdev, skb);
2913
+ if (err < 0) {
2914
+ kfree_skb(data->evt_skb);
2915
+ data->evt_skb = NULL;
2916
+ kfree(urb->setup_packet);
2917
+ return;
2918
+ }
2919
+
2920
+ if (test_and_clear_bit(BTUSB_TX_WAIT_VND_EVT,
2921
+ &data->flags)) {
2922
+ /* Barrier to sync with other CPUs */
2923
+ smp_mb__after_atomic();
2924
+ wake_up_bit(&data->flags,
2925
+ BTUSB_TX_WAIT_VND_EVT);
2926
+ }
2927
+ kfree(urb->setup_packet);
2928
+ return;
2929
+ } else if (urb->status == -ENOENT) {
2930
+ /* Avoid suspend failed when usb_kill_urb */
2931
+ return;
2932
+ }
2933
+
2934
+ usb_mark_last_busy(data->udev);
2935
+
2936
+ /* The URB complete handler is still called with urb->actual_length = 0
2937
+ * when the event is not available, so we should keep re-submitting
2938
+ * URB until WMT event returns, Also, It's necessary to wait some time
2939
+ * between the two consecutive control URBs to relax the target device
2940
+ * to generate the event. Otherwise, the WMT event cannot return from
2941
+ * the device successfully.
2942
+ */
2943
+ udelay(500);
2944
+
2945
+ usb_anchor_urb(urb, &data->ctrl_anchor);
2946
+ err = usb_submit_urb(urb, GFP_ATOMIC);
2947
+ if (err < 0) {
2948
+ kfree(urb->setup_packet);
2949
+ /* -EPERM: urb is being killed;
2950
+ * -ENODEV: device got disconnected
2951
+ */
2952
+ if (err != -EPERM && err != -ENODEV)
2953
+ bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
2954
+ urb, -err);
2955
+ usb_unanchor_urb(urb);
2956
+ }
2957
+}
2958
+
2959
+static int btusb_mtk_submit_wmt_recv_urb(struct hci_dev *hdev)
2960
+{
2961
+ struct btusb_data *data = hci_get_drvdata(hdev);
2962
+ struct usb_ctrlrequest *dr;
2963
+ unsigned char *buf;
2964
+ int err, size = 64;
2965
+ unsigned int pipe;
2966
+ struct urb *urb;
2967
+
2968
+ urb = usb_alloc_urb(0, GFP_KERNEL);
2969
+ if (!urb)
2970
+ return -ENOMEM;
2971
+
2972
+ dr = kmalloc(sizeof(*dr), GFP_KERNEL);
2973
+ if (!dr) {
2974
+ usb_free_urb(urb);
2975
+ return -ENOMEM;
2976
+ }
2977
+
2978
+ dr->bRequestType = USB_TYPE_VENDOR | USB_DIR_IN;
2979
+ dr->bRequest = 1;
2980
+ dr->wIndex = cpu_to_le16(0);
2981
+ dr->wValue = cpu_to_le16(48);
2982
+ dr->wLength = cpu_to_le16(size);
2983
+
2984
+ buf = kmalloc(size, GFP_KERNEL);
2985
+ if (!buf) {
2986
+ kfree(dr);
2987
+ usb_free_urb(urb);
2988
+ return -ENOMEM;
2989
+ }
2990
+
2991
+ pipe = usb_rcvctrlpipe(data->udev, 0);
2992
+
2993
+ usb_fill_control_urb(urb, data->udev, pipe, (void *)dr,
2994
+ buf, size, btusb_mtk_wmt_recv, hdev);
2995
+
2996
+ urb->transfer_flags |= URB_FREE_BUFFER;
2997
+
2998
+ usb_anchor_urb(urb, &data->ctrl_anchor);
2999
+ err = usb_submit_urb(urb, GFP_KERNEL);
3000
+ if (err < 0) {
3001
+ if (err != -EPERM && err != -ENODEV)
3002
+ bt_dev_err(hdev, "urb %p submission failed (%d)",
3003
+ urb, -err);
3004
+ usb_unanchor_urb(urb);
3005
+ }
3006
+
3007
+ usb_free_urb(urb);
3008
+
3009
+ return err;
3010
+}
3011
+
3012
+static int btusb_mtk_hci_wmt_sync(struct hci_dev *hdev,
3013
+ struct btmtk_hci_wmt_params *wmt_params)
3014
+{
3015
+ struct btusb_data *data = hci_get_drvdata(hdev);
3016
+ struct btmtk_hci_wmt_evt_funcc *wmt_evt_funcc;
3017
+ u32 hlen, status = BTMTK_WMT_INVALID;
3018
+ struct btmtk_hci_wmt_evt *wmt_evt;
3019
+ struct btmtk_hci_wmt_cmd *wc;
3020
+ struct btmtk_wmt_hdr *hdr;
3021
+ int err;
3022
+
3023
+ /* Send the WMT command and wait until the WMT event returns */
3024
+ hlen = sizeof(*hdr) + wmt_params->dlen;
3025
+ if (hlen > 255)
3026
+ return -EINVAL;
3027
+
3028
+ wc = kzalloc(hlen, GFP_KERNEL);
3029
+ if (!wc)
3030
+ return -ENOMEM;
3031
+
3032
+ hdr = &wc->hdr;
3033
+ hdr->dir = 1;
3034
+ hdr->op = wmt_params->op;
3035
+ hdr->dlen = cpu_to_le16(wmt_params->dlen + 1);
3036
+ hdr->flag = wmt_params->flag;
3037
+ memcpy(wc->data, wmt_params->data, wmt_params->dlen);
3038
+
3039
+ set_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
3040
+
3041
+ /* WMT cmd/event doesn't follow up the generic HCI cmd/event handling,
3042
+ * it needs constantly polling control pipe until the host received the
3043
+ * WMT event, thus, we should require to specifically acquire PM counter
3044
+ * on the USB to prevent the interface from entering auto suspended
3045
+ * while WMT cmd/event in progress.
3046
+ */
3047
+ err = usb_autopm_get_interface(data->intf);
3048
+ if (err < 0)
3049
+ goto err_free_wc;
3050
+
3051
+ err = __hci_cmd_send(hdev, 0xfc6f, hlen, wc);
3052
+
3053
+ if (err < 0) {
3054
+ clear_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
3055
+ usb_autopm_put_interface(data->intf);
3056
+ goto err_free_wc;
3057
+ }
3058
+
3059
+ /* Submit control IN URB on demand to process the WMT event */
3060
+ err = btusb_mtk_submit_wmt_recv_urb(hdev);
3061
+
3062
+ usb_autopm_put_interface(data->intf);
3063
+
3064
+ if (err < 0)
3065
+ return err;
3066
+
3067
+ /* The vendor specific WMT commands are all answered by a vendor
3068
+ * specific event and will have the Command Status or Command
3069
+ * Complete as with usual HCI command flow control.
3070
+ *
3071
+ * After sending the command, wait for BTUSB_TX_WAIT_VND_EVT
3072
+ * state to be cleared. The driver specific event receive routine
3073
+ * will clear that state and with that indicate completion of the
3074
+ * WMT command.
3075
+ */
3076
+ err = wait_on_bit_timeout(&data->flags, BTUSB_TX_WAIT_VND_EVT,
3077
+ TASK_INTERRUPTIBLE, HCI_INIT_TIMEOUT);
3078
+ if (err == -EINTR) {
3079
+ bt_dev_err(hdev, "Execution of wmt command interrupted");
3080
+ clear_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
3081
+ goto err_free_wc;
3082
+ }
3083
+
3084
+ if (err) {
3085
+ bt_dev_err(hdev, "Execution of wmt command timed out");
3086
+ clear_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
3087
+ err = -ETIMEDOUT;
3088
+ goto err_free_wc;
3089
+ }
3090
+
3091
+ /* Parse and handle the return WMT event */
3092
+ wmt_evt = (struct btmtk_hci_wmt_evt *)data->evt_skb->data;
3093
+ if (wmt_evt->whdr.op != hdr->op) {
3094
+ bt_dev_err(hdev, "Wrong op received %d expected %d",
3095
+ wmt_evt->whdr.op, hdr->op);
3096
+ err = -EIO;
3097
+ goto err_free_skb;
3098
+ }
3099
+
3100
+ switch (wmt_evt->whdr.op) {
3101
+ case BTMTK_WMT_SEMAPHORE:
3102
+ if (wmt_evt->whdr.flag == 2)
3103
+ status = BTMTK_WMT_PATCH_UNDONE;
3104
+ else
3105
+ status = BTMTK_WMT_PATCH_DONE;
3106
+ break;
3107
+ case BTMTK_WMT_FUNC_CTRL:
3108
+ wmt_evt_funcc = (struct btmtk_hci_wmt_evt_funcc *)wmt_evt;
3109
+ if (be16_to_cpu(wmt_evt_funcc->status) == 0x404)
3110
+ status = BTMTK_WMT_ON_DONE;
3111
+ else if (be16_to_cpu(wmt_evt_funcc->status) == 0x420)
3112
+ status = BTMTK_WMT_ON_PROGRESS;
3113
+ else
3114
+ status = BTMTK_WMT_ON_UNDONE;
3115
+ break;
3116
+ }
3117
+
3118
+ if (wmt_params->status)
3119
+ *wmt_params->status = status;
3120
+
3121
+err_free_skb:
3122
+ kfree_skb(data->evt_skb);
3123
+ data->evt_skb = NULL;
3124
+err_free_wc:
3125
+ kfree(wc);
3126
+ return err;
3127
+}
3128
+
3129
+static int btusb_mtk_setup_firmware(struct hci_dev *hdev, const char *fwname)
3130
+{
3131
+ struct btmtk_hci_wmt_params wmt_params;
3132
+ const struct firmware *fw;
3133
+ const u8 *fw_ptr;
3134
+ size_t fw_size;
3135
+ int err, dlen;
3136
+ u8 flag, param;
3137
+
3138
+ err = request_firmware(&fw, fwname, &hdev->dev);
3139
+ if (err < 0) {
3140
+ bt_dev_err(hdev, "Failed to load firmware file (%d)", err);
3141
+ return err;
3142
+ }
3143
+
3144
+ /* Power on data RAM the firmware relies on. */
3145
+ param = 1;
3146
+ wmt_params.op = BTMTK_WMT_FUNC_CTRL;
3147
+ wmt_params.flag = 3;
3148
+ wmt_params.dlen = sizeof(param);
3149
+ wmt_params.data = &param;
3150
+ wmt_params.status = NULL;
3151
+
3152
+ err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3153
+ if (err < 0) {
3154
+ bt_dev_err(hdev, "Failed to power on data RAM (%d)", err);
3155
+ goto err_release_fw;
3156
+ }
3157
+
3158
+ fw_ptr = fw->data;
3159
+ fw_size = fw->size;
3160
+
3161
+ /* The size of patch header is 30 bytes, should be skip */
3162
+ if (fw_size < 30) {
3163
+ err = -EINVAL;
3164
+ goto err_release_fw;
3165
+ }
3166
+
3167
+ fw_size -= 30;
3168
+ fw_ptr += 30;
3169
+ flag = 1;
3170
+
3171
+ wmt_params.op = BTMTK_WMT_PATCH_DWNLD;
3172
+ wmt_params.status = NULL;
3173
+
3174
+ while (fw_size > 0) {
3175
+ dlen = min_t(int, 250, fw_size);
3176
+
3177
+ /* Tell deivice the position in sequence */
3178
+ if (fw_size - dlen <= 0)
3179
+ flag = 3;
3180
+ else if (fw_size < fw->size - 30)
3181
+ flag = 2;
3182
+
3183
+ wmt_params.flag = flag;
3184
+ wmt_params.dlen = dlen;
3185
+ wmt_params.data = fw_ptr;
3186
+
3187
+ err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3188
+ if (err < 0) {
3189
+ bt_dev_err(hdev, "Failed to send wmt patch dwnld (%d)",
3190
+ err);
3191
+ goto err_release_fw;
3192
+ }
3193
+
3194
+ fw_size -= dlen;
3195
+ fw_ptr += dlen;
3196
+ }
3197
+
3198
+ wmt_params.op = BTMTK_WMT_RST;
3199
+ wmt_params.flag = 4;
3200
+ wmt_params.dlen = 0;
3201
+ wmt_params.data = NULL;
3202
+ wmt_params.status = NULL;
3203
+
3204
+ /* Activate funciton the firmware providing to */
3205
+ err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3206
+ if (err < 0) {
3207
+ bt_dev_err(hdev, "Failed to send wmt rst (%d)", err);
3208
+ goto err_release_fw;
3209
+ }
3210
+
3211
+ /* Wait a few moments for firmware activation done */
3212
+ usleep_range(10000, 12000);
3213
+
3214
+err_release_fw:
3215
+ release_firmware(fw);
3216
+
3217
+ return err;
3218
+}
3219
+
3220
+static int btusb_mtk_func_query(struct hci_dev *hdev)
3221
+{
3222
+ struct btmtk_hci_wmt_params wmt_params;
3223
+ int status, err;
3224
+ u8 param = 0;
3225
+
3226
+ /* Query whether the function is enabled */
3227
+ wmt_params.op = BTMTK_WMT_FUNC_CTRL;
3228
+ wmt_params.flag = 4;
3229
+ wmt_params.dlen = sizeof(param);
3230
+ wmt_params.data = &param;
3231
+ wmt_params.status = &status;
3232
+
3233
+ err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3234
+ if (err < 0) {
3235
+ bt_dev_err(hdev, "Failed to query function status (%d)", err);
3236
+ return err;
3237
+ }
3238
+
3239
+ return status;
3240
+}
3241
+
3242
+static int btusb_mtk_reg_read(struct btusb_data *data, u32 reg, u32 *val)
3243
+{
3244
+ int pipe, err, size = sizeof(u32);
3245
+ void *buf;
3246
+
3247
+ buf = kzalloc(size, GFP_KERNEL);
3248
+ if (!buf)
3249
+ return -ENOMEM;
3250
+
3251
+ pipe = usb_rcvctrlpipe(data->udev, 0);
3252
+ err = usb_control_msg(data->udev, pipe, 0x63,
3253
+ USB_TYPE_VENDOR | USB_DIR_IN,
3254
+ reg >> 16, reg & 0xffff,
3255
+ buf, size, USB_CTRL_SET_TIMEOUT);
3256
+ if (err < 0)
3257
+ goto err_free_buf;
3258
+
3259
+ *val = get_unaligned_le32(buf);
3260
+
3261
+err_free_buf:
3262
+ kfree(buf);
3263
+
3264
+ return err;
3265
+}
3266
+
3267
+static int btusb_mtk_id_get(struct btusb_data *data, u32 reg, u32 *id)
3268
+{
3269
+ return btusb_mtk_reg_read(data, reg, id);
3270
+}
3271
+
3272
+static int btusb_mtk_setup(struct hci_dev *hdev)
3273
+{
3274
+ struct btusb_data *data = hci_get_drvdata(hdev);
3275
+ struct btmtk_hci_wmt_params wmt_params;
3276
+ ktime_t calltime, delta, rettime;
3277
+ struct btmtk_tci_sleep tci_sleep;
3278
+ unsigned long long duration;
3279
+ struct sk_buff *skb;
3280
+ const char *fwname;
3281
+ int err, status;
3282
+ u32 dev_id;
3283
+ u8 param;
3284
+
3285
+ calltime = ktime_get();
3286
+
3287
+ err = btusb_mtk_id_get(data, 0x80000008, &dev_id);
3288
+ if (err < 0) {
3289
+ bt_dev_err(hdev, "Failed to get device id (%d)", err);
3290
+ return err;
3291
+ }
3292
+
3293
+ switch (dev_id) {
3294
+ case 0x7663:
3295
+ fwname = FIRMWARE_MT7663;
3296
+ break;
3297
+ case 0x7668:
3298
+ fwname = FIRMWARE_MT7668;
3299
+ break;
3300
+ default:
3301
+ bt_dev_err(hdev, "Unsupported support hardware variant (%08x)",
3302
+ dev_id);
3303
+ return -ENODEV;
3304
+ }
3305
+
3306
+ /* Query whether the firmware is already download */
3307
+ wmt_params.op = BTMTK_WMT_SEMAPHORE;
3308
+ wmt_params.flag = 1;
3309
+ wmt_params.dlen = 0;
3310
+ wmt_params.data = NULL;
3311
+ wmt_params.status = &status;
3312
+
3313
+ err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3314
+ if (err < 0) {
3315
+ bt_dev_err(hdev, "Failed to query firmware status (%d)", err);
3316
+ return err;
3317
+ }
3318
+
3319
+ if (status == BTMTK_WMT_PATCH_DONE) {
3320
+ bt_dev_info(hdev, "firmware already downloaded");
3321
+ goto ignore_setup_fw;
3322
+ }
3323
+
3324
+ /* Setup a firmware which the device definitely requires */
3325
+ err = btusb_mtk_setup_firmware(hdev, fwname);
3326
+ if (err < 0)
3327
+ return err;
3328
+
3329
+ignore_setup_fw:
3330
+ err = readx_poll_timeout(btusb_mtk_func_query, hdev, status,
3331
+ status < 0 || status != BTMTK_WMT_ON_PROGRESS,
3332
+ 2000, 5000000);
3333
+ /* -ETIMEDOUT happens */
3334
+ if (err < 0)
3335
+ return err;
3336
+
3337
+ /* The other errors happen in btusb_mtk_func_query */
3338
+ if (status < 0)
3339
+ return status;
3340
+
3341
+ if (status == BTMTK_WMT_ON_DONE) {
3342
+ bt_dev_info(hdev, "function already on");
3343
+ goto ignore_func_on;
3344
+ }
3345
+
3346
+ /* Enable Bluetooth protocol */
3347
+ param = 1;
3348
+ wmt_params.op = BTMTK_WMT_FUNC_CTRL;
3349
+ wmt_params.flag = 0;
3350
+ wmt_params.dlen = sizeof(param);
3351
+ wmt_params.data = &param;
3352
+ wmt_params.status = NULL;
3353
+
3354
+ err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3355
+ if (err < 0) {
3356
+ bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
3357
+ return err;
3358
+ }
3359
+
3360
+ignore_func_on:
3361
+ /* Apply the low power environment setup */
3362
+ tci_sleep.mode = 0x5;
3363
+ tci_sleep.duration = cpu_to_le16(0x640);
3364
+ tci_sleep.host_duration = cpu_to_le16(0x640);
3365
+ tci_sleep.host_wakeup_pin = 0;
3366
+ tci_sleep.time_compensation = 0;
3367
+
3368
+ skb = __hci_cmd_sync(hdev, 0xfc7a, sizeof(tci_sleep), &tci_sleep,
3369
+ HCI_INIT_TIMEOUT);
3370
+ if (IS_ERR(skb)) {
3371
+ err = PTR_ERR(skb);
3372
+ bt_dev_err(hdev, "Failed to apply low power setting (%d)", err);
3373
+ return err;
3374
+ }
3375
+ kfree_skb(skb);
3376
+
3377
+ rettime = ktime_get();
3378
+ delta = ktime_sub(rettime, calltime);
3379
+ duration = (unsigned long long)ktime_to_ns(delta) >> 10;
3380
+
3381
+ bt_dev_info(hdev, "Device setup in %llu usecs", duration);
3382
+
3383
+ return 0;
3384
+}
3385
+
3386
+static int btusb_mtk_shutdown(struct hci_dev *hdev)
3387
+{
3388
+ struct btmtk_hci_wmt_params wmt_params;
3389
+ u8 param = 0;
3390
+ int err;
3391
+
3392
+ /* Disable the device */
3393
+ wmt_params.op = BTMTK_WMT_FUNC_CTRL;
3394
+ wmt_params.flag = 0;
3395
+ wmt_params.dlen = sizeof(param);
3396
+ wmt_params.data = &param;
3397
+ wmt_params.status = NULL;
3398
+
3399
+ err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3400
+ if (err < 0) {
3401
+ bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
3402
+ return err;
3403
+ }
3404
+
3405
+ return 0;
3406
+}
3407
+
3408
+MODULE_FIRMWARE(FIRMWARE_MT7663);
3409
+MODULE_FIRMWARE(FIRMWARE_MT7668);
24033410
24043411 #ifdef CONFIG_PM
24053412 /* Configure an out-of-band gpio as wake-up pin, if specified in device tree */
....@@ -2492,6 +3499,27 @@
24923499 return 0;
24933500 }
24943501
3502
+static int btusb_set_bdaddr_wcn6855(struct hci_dev *hdev,
3503
+ const bdaddr_t *bdaddr)
3504
+{
3505
+ struct sk_buff *skb;
3506
+ u8 buf[6];
3507
+ long ret;
3508
+
3509
+ memcpy(buf, bdaddr, sizeof(bdaddr_t));
3510
+
3511
+ skb = __hci_cmd_sync_ev(hdev, 0xfc14, sizeof(buf), buf,
3512
+ HCI_EV_CMD_COMPLETE, HCI_INIT_TIMEOUT);
3513
+ if (IS_ERR(skb)) {
3514
+ ret = PTR_ERR(skb);
3515
+ bt_dev_err(hdev, "Change address command failed (%ld)", ret);
3516
+ return ret;
3517
+ }
3518
+ kfree_skb(skb);
3519
+
3520
+ return 0;
3521
+}
3522
+
24953523 #define QCA_DFU_PACKET_LEN 4096
24963524
24973525 #define QCA_GET_TARGET_VERSION 0x09
....@@ -2511,7 +3539,8 @@
25113539 } __packed;
25123540
25133541 struct qca_rampatch_version {
2514
- __le16 rom_version;
3542
+ __le16 rom_version_high;
3543
+ __le16 rom_version_low;
25153544 __le16 patch_version;
25163545 } __packed;
25173546
....@@ -2523,12 +3552,14 @@
25233552 };
25243553
25253554 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 */
3555
+ { 0x00000100, 20, 4, 8 }, /* Rome 1.0 */
3556
+ { 0x00000101, 20, 4, 8 }, /* Rome 1.1 */
3557
+ { 0x00000200, 28, 4, 16 }, /* Rome 2.0 */
3558
+ { 0x00000201, 28, 4, 16 }, /* Rome 2.1 */
3559
+ { 0x00000300, 28, 4, 16 }, /* Rome 3.0 */
3560
+ { 0x00000302, 28, 4, 16 }, /* Rome 3.2 */
3561
+ { 0x00130100, 40, 4, 16 }, /* WCN6855 1.0 */
3562
+ { 0x00130200, 40, 4, 16 }, /* WCN6855 2.0 */
25323563 };
25333564
25343565 static int btusb_qca_send_vendor_req(struct usb_device *udev, u8 request,
....@@ -2635,8 +3666,8 @@
26353666 {
26363667 struct qca_rampatch_version *rver;
26373668 const struct firmware *fw;
2638
- u32 ver_rom, ver_patch;
2639
- u16 rver_rom, rver_patch;
3669
+ u32 ver_rom, ver_patch, rver_rom;
3670
+ u16 rver_rom_low, rver_rom_high, rver_patch;
26403671 char fwname[64];
26413672 int err;
26423673
....@@ -2655,8 +3686,15 @@
26553686 bt_dev_info(hdev, "using rampatch file: %s", fwname);
26563687
26573688 rver = (struct qca_rampatch_version *)(fw->data + info->ver_offset);
2658
- rver_rom = le16_to_cpu(rver->rom_version);
3689
+ rver_rom_low = le16_to_cpu(rver->rom_version_low);
26593690 rver_patch = le16_to_cpu(rver->patch_version);
3691
+
3692
+ if (ver_rom & ~0xffffU) {
3693
+ rver_rom_high = le16_to_cpu(rver->rom_version_high);
3694
+ rver_rom = le32_to_cpu(rver_rom_high << 16 | rver_rom_low);
3695
+ } else {
3696
+ rver_rom = rver_rom_low;
3697
+ }
26603698
26613699 bt_dev_info(hdev, "QCA: patch rome 0x%x build 0x%x, "
26623700 "firmware rome 0x%x build 0x%x",
....@@ -2731,15 +3769,19 @@
27313769 return err;
27323770
27333771 ver_rom = le32_to_cpu(ver.rom_version);
2734
- /* Don't care about high ROM versions */
2735
- if (ver_rom & ~0xffffU)
2736
- return 0;
27373772
27383773 for (i = 0; i < ARRAY_SIZE(qca_devices_table); i++) {
27393774 if (ver_rom == qca_devices_table[i].rom_version)
27403775 info = &qca_devices_table[i];
27413776 }
27423777 if (!info) {
3778
+ /* If the rom_version is not matched in the qca_devices_table
3779
+ * and the high ROM version is not zero, we assume this chip no
3780
+ * need to load the rampatch and nvm.
3781
+ */
3782
+ if (ver_rom & ~0xffffU)
3783
+ return 0;
3784
+
27433785 bt_dev_err(hdev, "don't support firmware rome 0x%x", ver_rom);
27443786 return -ENODEV;
27453787 }
....@@ -2764,7 +3806,6 @@
27643806 return 0;
27653807 }
27663808
2767
-#ifdef CONFIG_BT_HCIBTUSB_BCM
27683809 static inline int __set_diag_interface(struct hci_dev *hdev)
27693810 {
27703811 struct btusb_data *data = hci_get_drvdata(hdev);
....@@ -2851,7 +3892,6 @@
28513892
28523893 return submit_or_queue_tx_urb(hdev, urb);
28533894 }
2854
-#endif
28553895
28563896 #ifdef CONFIG_PM
28573897 static irqreturn_t btusb_oob_wake_handler(int irq, void *priv)
....@@ -2871,6 +3911,8 @@
28713911
28723912 static const struct of_device_id btusb_match_table[] = {
28733913 { .compatible = "usb1286,204e" },
3914
+ { .compatible = "usbcf3,e300" }, /* QCA6174A */
3915
+ { .compatible = "usb4ca,301a" }, /* QCA6174A (Lite-On) */
28743916 { }
28753917 };
28763918 MODULE_DEVICE_TABLE(of, btusb_match_table);
....@@ -2920,10 +3962,21 @@
29203962 interface_to_usbdev(intf)->quirks |= USB_QUIRK_RESET_RESUME;
29213963 }
29223964
3965
+static bool btusb_prevent_wake(struct hci_dev *hdev)
3966
+{
3967
+ struct btusb_data *data = hci_get_drvdata(hdev);
3968
+
3969
+ if (test_bit(BTUSB_WAKEUP_DISABLE, &data->flags))
3970
+ return true;
3971
+
3972
+ return !device_may_wakeup(&data->udev->dev);
3973
+}
3974
+
29233975 static int btusb_probe(struct usb_interface *intf,
29243976 const struct usb_device_id *id)
29253977 {
29263978 struct usb_endpoint_descriptor *ep_desc;
3979
+ struct gpio_desc *reset_gpio;
29273980 struct btusb_data *data;
29283981 struct hci_dev *hdev;
29293982 unsigned ifnum_base;
....@@ -3010,6 +4063,7 @@
30104063 init_usb_anchor(&data->bulk_anchor);
30114064 init_usb_anchor(&data->isoc_anchor);
30124065 init_usb_anchor(&data->diag_anchor);
4066
+ init_usb_anchor(&data->ctrl_anchor);
30134067 spin_lock_init(&data->rxlock);
30144068
30154069 if (id->driver_info & BTUSB_INTEL_NEW) {
....@@ -3037,11 +4091,21 @@
30374091
30384092 SET_HCIDEV_DEV(hdev, &intf->dev);
30394093
4094
+ reset_gpio = gpiod_get_optional(&data->udev->dev, "reset",
4095
+ GPIOD_OUT_LOW);
4096
+ if (IS_ERR(reset_gpio)) {
4097
+ err = PTR_ERR(reset_gpio);
4098
+ goto out_free_dev;
4099
+ } else if (reset_gpio) {
4100
+ data->reset_gpio = reset_gpio;
4101
+ }
4102
+
30404103 hdev->open = btusb_open;
30414104 hdev->close = btusb_close;
30424105 hdev->flush = btusb_flush;
30434106 hdev->send = btusb_send_frame;
30444107 hdev->notify = btusb_notify;
4108
+ hdev->prevent_wake = btusb_prevent_wake;
30454109
30464110 #ifdef CONFIG_PM
30474111 err = btusb_config_oob_wake(hdev);
....@@ -3064,8 +4128,8 @@
30644128 if (id->driver_info & BTUSB_BCM92035)
30654129 hdev->setup = btusb_setup_bcm92035;
30664130
3067
-#ifdef CONFIG_BT_HCIBTUSB_BCM
3068
- if (id->driver_info & BTUSB_BCM_PATCHRAM) {
4131
+ if (IS_ENABLED(CONFIG_BT_HCIBTUSB_BCM) &&
4132
+ (id->driver_info & BTUSB_BCM_PATCHRAM)) {
30694133 hdev->manufacturer = 15;
30704134 hdev->setup = btbcm_setup_patchram;
30714135 hdev->set_diag = btusb_bcm_set_diag;
....@@ -3075,7 +4139,8 @@
30754139 data->diag = usb_ifnum_to_if(data->udev, ifnum_base + 2);
30764140 }
30774141
3078
- if (id->driver_info & BTUSB_BCM_APPLE) {
4142
+ if (IS_ENABLED(CONFIG_BT_HCIBTUSB_BCM) &&
4143
+ (id->driver_info & BTUSB_BCM_APPLE)) {
30794144 hdev->manufacturer = 15;
30804145 hdev->setup = btbcm_setup_apple;
30814146 hdev->set_diag = btusb_bcm_set_diag;
....@@ -3083,7 +4148,6 @@
30834148 /* Broadcom LM_DIAG Interface numbers are hardcoded */
30844149 data->diag = usb_ifnum_to_if(data->udev, ifnum_base + 2);
30854150 }
3086
-#endif
30874151
30884152 if (id->driver_info & BTUSB_INTEL) {
30894153 hdev->manufacturer = 2;
....@@ -3091,6 +4155,7 @@
30914155 hdev->shutdown = btusb_shutdown_intel;
30924156 hdev->set_diag = btintel_set_diag_mfg;
30934157 hdev->set_bdaddr = btintel_set_bdaddr;
4158
+ hdev->cmd_timeout = btusb_intel_cmd_timeout;
30944159 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
30954160 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
30964161 set_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks);
....@@ -3100,15 +4165,26 @@
31004165 hdev->manufacturer = 2;
31014166 hdev->send = btusb_send_frame_intel;
31024167 hdev->setup = btusb_setup_intel_new;
4168
+ hdev->shutdown = btusb_shutdown_intel_new;
31034169 hdev->hw_error = btintel_hw_error;
31044170 hdev->set_diag = btintel_set_diag;
31054171 hdev->set_bdaddr = btintel_set_bdaddr;
4172
+ hdev->cmd_timeout = btusb_intel_cmd_timeout;
31064173 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
4174
+ set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
31074175 set_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks);
31084176 }
31094177
31104178 if (id->driver_info & BTUSB_MARVELL)
31114179 hdev->set_bdaddr = btusb_set_bdaddr_marvell;
4180
+
4181
+ if (IS_ENABLED(CONFIG_BT_HCIBTUSB_MTK) &&
4182
+ (id->driver_info & BTUSB_MEDIATEK)) {
4183
+ hdev->setup = btusb_mtk_setup;
4184
+ hdev->shutdown = btusb_mtk_shutdown;
4185
+ hdev->manufacturer = 70;
4186
+ set_bit(HCI_QUIRK_NON_PERSISTENT_SETUP, &hdev->quirks);
4187
+ }
31124188
31134189 if (id->driver_info & BTUSB_SWAVE) {
31144190 set_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks);
....@@ -3130,22 +4206,17 @@
31304206 if (id->driver_info & BTUSB_QCA_ROME) {
31314207 data->setup_on_usb = btusb_setup_qca;
31324208 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
4209
+ hdev->cmd_timeout = btusb_qca_cmd_timeout;
31334210 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
31344211 btusb_check_needs_reset_resume(intf);
31354212 }
31364213
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;
4214
+ if (id->driver_info & BTUSB_QCA_WCN6855) {
4215
+ data->setup_on_usb = btusb_setup_qca;
4216
+ hdev->set_bdaddr = btusb_set_bdaddr_wcn6855;
4217
+ hdev->cmd_timeout = btusb_qca_cmd_timeout;
4218
+ set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
31474219 }
3148
-#endif
31494220
31504221 if (id->driver_info & BTUSB_AMP) {
31514222 /* AMP controllers do not support SCO packets */
....@@ -3154,6 +4225,20 @@
31544225 /* Interface orders are hardcoded in the specification */
31554226 data->isoc = usb_ifnum_to_if(data->udev, ifnum_base + 1);
31564227 data->isoc_ifnum = ifnum_base + 1;
4228
+ }
4229
+
4230
+ if (IS_ENABLED(CONFIG_BT_HCIBTUSB_RTL) &&
4231
+ (id->driver_info & BTUSB_REALTEK)) {
4232
+ hdev->setup = btrtl_setup_realtek;
4233
+ hdev->shutdown = btrtl_shutdown_realtek;
4234
+ hdev->cmd_timeout = btusb_rtl_cmd_timeout;
4235
+
4236
+ /* Realtek devices lose their updated firmware over global
4237
+ * suspend that means host doesn't send SET_FEATURE
4238
+ * (DEVICE_REMOTE_WAKEUP)
4239
+ */
4240
+ set_bit(BTUSB_WAKEUP_DISABLE, &data->flags);
4241
+ set_bit(BTUSB_USE_ALT3_FOR_WBS, &data->flags);
31574242 }
31584243
31594244 if (!reset)
....@@ -3166,6 +4251,12 @@
31664251
31674252 if (id->driver_info & BTUSB_BROKEN_ISOC)
31684253 data->isoc = NULL;
4254
+
4255
+ if (id->driver_info & BTUSB_WIDEBAND_SPEECH)
4256
+ set_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED, &hdev->quirks);
4257
+
4258
+ if (id->driver_info & BTUSB_VALID_LE_STATES)
4259
+ set_bit(HCI_QUIRK_VALID_LE_STATES, &hdev->quirks);
31694260
31704261 if (id->driver_info & BTUSB_DIGIANSWER) {
31714262 data->cmdreq_type = USB_TYPE_VENDOR;
....@@ -3180,11 +4271,13 @@
31804271 if (bcdDevice < 0x117)
31814272 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
31824273
3183
- /* Fake CSR devices with broken commands */
3184
- if (bcdDevice <= 0x100 || bcdDevice == 0x134)
3185
- hdev->setup = btusb_setup_csr;
3186
-
4274
+ /* This must be set first in case we disable it for fakes */
31874275 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
4276
+
4277
+ /* Fake CSR devices with broken commands */
4278
+ if (le16_to_cpu(udev->descriptor.idVendor) == 0x0a12 &&
4279
+ le16_to_cpu(udev->descriptor.idProduct) == 0x0001)
4280
+ hdev->setup = btusb_setup_csr;
31884281 }
31894282
31904283 if (id->driver_info & BTUSB_SNIFFER) {
....@@ -3213,15 +4306,13 @@
32134306 goto out_free_dev;
32144307 }
32154308
3216
-#ifdef CONFIG_BT_HCIBTUSB_BCM
3217
- if (data->diag) {
4309
+ if (IS_ENABLED(CONFIG_BT_HCIBTUSB_BCM) && data->diag) {
32184310 if (!usb_driver_claim_interface(&btusb_driver,
32194311 data->diag, data))
32204312 __set_diag_interface(hdev);
32214313 else
32224314 data->diag = NULL;
32234315 }
3224
-#endif
32254316
32264317 if (enable_autosuspend)
32274318 usb_enable_autosuspend(data->udev);
....@@ -3235,6 +4326,8 @@
32354326 return 0;
32364327
32374328 out_free_dev:
4329
+ if (data->reset_gpio)
4330
+ gpiod_put(data->reset_gpio);
32384331 hci_free_dev(hdev);
32394332 return err;
32404333 }
....@@ -3278,6 +4371,9 @@
32784371 if (data->oob_wake_irq)
32794372 device_init_wakeup(&data->udev->dev, false);
32804373
4374
+ if (data->reset_gpio)
4375
+ gpiod_put(data->reset_gpio);
4376
+
32814377 hci_free_dev(hdev);
32824378 }
32834379
....@@ -3312,6 +4408,19 @@
33124408 enable_irq(data->oob_wake_irq);
33134409 }
33144410
4411
+ /* For global suspend, Realtek devices lose the loaded fw
4412
+ * in them. But for autosuspend, firmware should remain.
4413
+ * Actually, it depends on whether the usb host sends
4414
+ * set feature (enable wakeup) or not.
4415
+ */
4416
+ if (test_bit(BTUSB_WAKEUP_DISABLE, &data->flags)) {
4417
+ if (PMSG_IS_AUTO(message) &&
4418
+ device_can_wakeup(&data->udev->dev))
4419
+ data->udev->do_remote_wakeup = 1;
4420
+ else if (!PMSG_IS_AUTO(message))
4421
+ data->udev->reset_resume = 1;
4422
+ }
4423
+
33154424 return 0;
33164425 }
33174426