File Index
Files and directories
Sign in
FILE

btusb.c

File preview with syntax highlighting.

linux-6.19.14/drivers/bluetooth/btusb.c C
   1// SPDX-License-Identifier: GPL-2.0-or-later
   2/*
   3 *
   4 *  Generic Bluetooth USB driver
   5 *
   6 *  Copyright (C) 2005-2008  Marcel Holtmann <marcel@holtmann.org>
   7 */
   8
   9#include <linux/dmi.h>
  10#include <linux/module.h>
  11#include <linux/usb.h>
  12#include <linux/usb/quirks.h>
  13#include <linux/firmware.h>
  14#include <linux/iopoll.h>
  15#include <linux/of_device.h>
  16#include <linux/of_irq.h>
  17#include <linux/suspend.h>
  18#include <linux/gpio/consumer.h>
  19#include <linux/debugfs.h>
  20#include <linux/unaligned.h>
  21
  22#include <net/bluetooth/bluetooth.h>
  23#include <net/bluetooth/hci_core.h>
  24#include <net/bluetooth/hci_drv.h>
  25
  26#include "btintel.h"
  27#include "btbcm.h"
  28#include "btrtl.h"
  29#include "btmtk.h"
  30
  31#define VERSION "0.8"
  32
  33static bool disable_scofix;
  34static bool force_scofix;
  35static bool enable_autosuspend = IS_ENABLED(CONFIG_BT_HCIBTUSB_AUTOSUSPEND);
  36static bool enable_poll_sync = IS_ENABLED(CONFIG_BT_HCIBTUSB_POLL_SYNC);
  37static bool reset = true;
  38
  39static struct usb_driver btusb_driver;
  40
  41#define BTUSB_IGNORE			BIT(0)
  42#define BTUSB_DIGIANSWER		BIT(1)
  43#define BTUSB_CSR			BIT(2)
  44#define BTUSB_SNIFFER			BIT(3)
  45#define BTUSB_BCM92035			BIT(4)
  46#define BTUSB_BROKEN_ISOC		BIT(5)
  47#define BTUSB_WRONG_SCO_MTU		BIT(6)
  48#define BTUSB_ATH3012			BIT(7)
  49#define BTUSB_INTEL_COMBINED		BIT(8)
  50#define BTUSB_INTEL_BOOT		BIT(9)
  51#define BTUSB_BCM_PATCHRAM		BIT(10)
  52#define BTUSB_MARVELL			BIT(11)
  53#define BTUSB_SWAVE			BIT(12)
  54#define BTUSB_AMP			BIT(13)
  55#define BTUSB_QCA_ROME			BIT(14)
  56#define BTUSB_BCM_APPLE			BIT(15)
  57#define BTUSB_REALTEK			BIT(16)
  58#define BTUSB_BCM2045			BIT(17)
  59#define BTUSB_IFNUM_2			BIT(18)
  60#define BTUSB_CW6622			BIT(19)
  61#define BTUSB_MEDIATEK			BIT(20)
  62#define BTUSB_WIDEBAND_SPEECH		BIT(21)
  63#define BTUSB_INVALID_LE_STATES		BIT(22)
  64#define BTUSB_QCA_WCN6855		BIT(23)
  65#define BTUSB_INTEL_BROKEN_SHUTDOWN_LED	BIT(24)
  66#define BTUSB_INTEL_BROKEN_INITIAL_NCMD BIT(25)
  67#define BTUSB_INTEL_NO_WBS_SUPPORT	BIT(26)
  68#define BTUSB_ACTIONS_SEMI		BIT(27)
  69#define BTUSB_BARROT			BIT(28)
  70
  71static const struct usb_device_id btusb_table[] = {
  72	/* Generic Bluetooth USB device */
  73	{ USB_DEVICE_INFO(0xe0, 0x01, 0x01) },
  74
  75	/* Generic Bluetooth AMP device */
  76	{ USB_DEVICE_INFO(0xe0, 0x01, 0x04), .driver_info = BTUSB_AMP },
  77
  78	/* Generic Bluetooth USB interface */
  79	{ USB_INTERFACE_INFO(0xe0, 0x01, 0x01) },
  80
  81	/* Apple-specific (Broadcom) devices */
  82	{ USB_VENDOR_AND_INTERFACE_INFO(0x05ac, 0xff, 0x01, 0x01),
  83	  .driver_info = BTUSB_BCM_APPLE | BTUSB_IFNUM_2 },
  84
  85	/* MediaTek MT76x0E */
  86	{ USB_DEVICE(0x0e8d, 0x763f) },
  87
  88	/* Broadcom SoftSailing reporting vendor specific */
  89	{ USB_DEVICE(0x0a5c, 0x21e1) },
  90
  91	/* Apple MacBookPro 7,1 */
  92	{ USB_DEVICE(0x05ac, 0x8213) },
  93
  94	/* Apple iMac11,1 */
  95	{ USB_DEVICE(0x05ac, 0x8215) },
  96
  97	/* Apple MacBookPro6,2 */
  98	{ USB_DEVICE(0x05ac, 0x8218) },
  99
 100	/* Apple MacBookAir3,1, MacBookAir3,2 */
 101	{ USB_DEVICE(0x05ac, 0x821b) },
 102
 103	/* Apple MacBookAir4,1 */
 104	{ USB_DEVICE(0x05ac, 0x821f) },
 105
 106	/* Apple MacBookPro8,2 */
 107	{ USB_DEVICE(0x05ac, 0x821a) },
 108
 109	/* Apple MacMini5,1 */
 110	{ USB_DEVICE(0x05ac, 0x8281) },
 111
 112	/* AVM BlueFRITZ! USB v2.0 */
 113	{ USB_DEVICE(0x057c, 0x3800), .driver_info = BTUSB_SWAVE },
 114
 115	/* Bluetooth Ultraport Module from IBM */
 116	{ USB_DEVICE(0x04bf, 0x030a) },
 117
 118	/* ALPS Modules with non-standard id */
 119	{ USB_DEVICE(0x044e, 0x3001) },
 120	{ USB_DEVICE(0x044e, 0x3002) },
 121
 122	/* Ericsson with non-standard id */
 123	{ USB_DEVICE(0x0bdb, 0x1002) },
 124
 125	/* Canyon CN-BTU1 with HID interfaces */
 126	{ USB_DEVICE(0x0c10, 0x0000) },
 127
 128	/* Broadcom BCM20702B0 (Dynex/Insignia) */
 129	{ USB_DEVICE(0x19ff, 0x0239), .driver_info = BTUSB_BCM_PATCHRAM },
 130
 131	/* Broadcom BCM43142A0 (Foxconn/Lenovo) */
 132	{ USB_VENDOR_AND_INTERFACE_INFO(0x105b, 0xff, 0x01, 0x01),
 133	  .driver_info = BTUSB_BCM_PATCHRAM },
 134
 135	/* Broadcom BCM920703 (HTC Vive) */
 136	{ USB_VENDOR_AND_INTERFACE_INFO(0x0bb4, 0xff, 0x01, 0x01),
 137	  .driver_info = BTUSB_BCM_PATCHRAM },
 138
 139	/* Foxconn - Hon Hai */
 140	{ USB_VENDOR_AND_INTERFACE_INFO(0x0489, 0xff, 0x01, 0x01),
 141	  .driver_info = BTUSB_BCM_PATCHRAM },
 142
 143	/* Lite-On Technology - Broadcom based */
 144	{ USB_VENDOR_AND_INTERFACE_INFO(0x04ca, 0xff, 0x01, 0x01),
 145	  .driver_info = BTUSB_BCM_PATCHRAM },
 146
 147	/* Broadcom devices with vendor specific id */
 148	{ USB_VENDOR_AND_INTERFACE_INFO(0x0a5c, 0xff, 0x01, 0x01),
 149	  .driver_info = BTUSB_BCM_PATCHRAM },
 150
 151	/* ASUSTek Computer - Broadcom based */
 152	{ USB_VENDOR_AND_INTERFACE_INFO(0x0b05, 0xff, 0x01, 0x01),
 153	  .driver_info = BTUSB_BCM_PATCHRAM },
 154
 155	/* Belkin F8065bf - Broadcom based */
 156	{ USB_VENDOR_AND_INTERFACE_INFO(0x050d, 0xff, 0x01, 0x01),
 157	  .driver_info = BTUSB_BCM_PATCHRAM },
 158
 159	/* IMC Networks - Broadcom based */
 160	{ USB_VENDOR_AND_INTERFACE_INFO(0x13d3, 0xff, 0x01, 0x01),
 161	  .driver_info = BTUSB_BCM_PATCHRAM },
 162
 163	/* Dell Computer - Broadcom based  */
 164	{ USB_VENDOR_AND_INTERFACE_INFO(0x413c, 0xff, 0x01, 0x01),
 165	  .driver_info = BTUSB_BCM_PATCHRAM },
 166
 167	/* Toshiba Corp - Broadcom based */
 168	{ USB_VENDOR_AND_INTERFACE_INFO(0x0930, 0xff, 0x01, 0x01),
 169	  .driver_info = BTUSB_BCM_PATCHRAM },
 170
 171	/* Intel Bluetooth USB Bootloader (RAM module) */
 172	{ USB_DEVICE(0x8087, 0x0a5a),
 173	  .driver_info = BTUSB_INTEL_BOOT | BTUSB_BROKEN_ISOC },
 174
 175	{ }	/* Terminating entry */
 176};
 177
 178MODULE_DEVICE_TABLE(usb, btusb_table);
 179
 180static const struct usb_device_id quirks_table[] = {
 181	/* CSR BlueCore devices */
 182	{ USB_DEVICE(0x0a12, 0x0001), .driver_info = BTUSB_CSR },
 183
 184	/* Broadcom BCM2033 without firmware */
 185	{ USB_DEVICE(0x0a5c, 0x2033), .driver_info = BTUSB_IGNORE },
 186
 187	/* Broadcom BCM2045 devices */
 188	{ USB_DEVICE(0x0a5c, 0x2045), .driver_info = BTUSB_BCM2045 },
 189
 190	/* Atheros 3011 with sflash firmware */
 191	{ USB_DEVICE(0x0489, 0xe027), .driver_info = BTUSB_IGNORE },
 192	{ USB_DEVICE(0x0489, 0xe03d), .driver_info = BTUSB_IGNORE },
 193	{ USB_DEVICE(0x04f2, 0xaff1), .driver_info = BTUSB_IGNORE },
 194	{ USB_DEVICE(0x0930, 0x0215), .driver_info = BTUSB_IGNORE },
 195	{ USB_DEVICE(0x0cf3, 0x3002), .driver_info = BTUSB_IGNORE },
 196	{ USB_DEVICE(0x0cf3, 0xe019), .driver_info = BTUSB_IGNORE },
 197	{ USB_DEVICE(0x13d3, 0x3304), .driver_info = BTUSB_IGNORE },
 198
 199	/* Atheros AR9285 Malbec with sflash firmware */
 200	{ USB_DEVICE(0x03f0, 0x311d), .driver_info = BTUSB_IGNORE },
 201
 202	/* Atheros 3012 with sflash firmware */
 203	{ USB_DEVICE(0x0489, 0xe04d), .driver_info = BTUSB_ATH3012 },
 204	{ USB_DEVICE(0x0489, 0xe04e), .driver_info = BTUSB_ATH3012 },
 205	{ USB_DEVICE(0x0489, 0xe056), .driver_info = BTUSB_ATH3012 },
 206	{ USB_DEVICE(0x0489, 0xe057), .driver_info = BTUSB_ATH3012 },
 207	{ USB_DEVICE(0x0489, 0xe05f), .driver_info = BTUSB_ATH3012 },
 208	{ USB_DEVICE(0x0489, 0xe076), .driver_info = BTUSB_ATH3012 },
 209	{ USB_DEVICE(0x0489, 0xe078), .driver_info = BTUSB_ATH3012 },
 210	{ USB_DEVICE(0x0489, 0xe095), .driver_info = BTUSB_ATH3012 },
 211	{ USB_DEVICE(0x04c5, 0x1330), .driver_info = BTUSB_ATH3012 },
 212	{ USB_DEVICE(0x04ca, 0x3004), .driver_info = BTUSB_ATH3012 },
 213	{ USB_DEVICE(0x04ca, 0x3005), .driver_info = BTUSB_ATH3012 },
 214	{ USB_DEVICE(0x04ca, 0x3006), .driver_info = BTUSB_ATH3012 },
 215	{ USB_DEVICE(0x04ca, 0x3007), .driver_info = BTUSB_ATH3012 },
 216	{ USB_DEVICE(0x04ca, 0x3008), .driver_info = BTUSB_ATH3012 },
 217	{ USB_DEVICE(0x04ca, 0x300b), .driver_info = BTUSB_ATH3012 },
 218	{ USB_DEVICE(0x04ca, 0x300d), .driver_info = BTUSB_ATH3012 },
 219	{ USB_DEVICE(0x04ca, 0x300f), .driver_info = BTUSB_ATH3012 },
 220	{ USB_DEVICE(0x04ca, 0x3010), .driver_info = BTUSB_ATH3012 },
 221	{ USB_DEVICE(0x04ca, 0x3014), .driver_info = BTUSB_ATH3012 },
 222	{ USB_DEVICE(0x04ca, 0x3018), .driver_info = BTUSB_ATH3012 },
 223	{ USB_DEVICE(0x0930, 0x0219), .driver_info = BTUSB_ATH3012 },
 224	{ USB_DEVICE(0x0930, 0x021c), .driver_info = BTUSB_ATH3012 },
 225	{ USB_DEVICE(0x0930, 0x0220), .driver_info = BTUSB_ATH3012 },
 226	{ USB_DEVICE(0x0930, 0x0227), .driver_info = BTUSB_ATH3012 },
 227	{ USB_DEVICE(0x0b05, 0x17d0), .driver_info = BTUSB_ATH3012 },
 228	{ USB_DEVICE(0x0cf3, 0x0036), .driver_info = BTUSB_ATH3012 },
 229	{ USB_DEVICE(0x0cf3, 0x3004), .driver_info = BTUSB_ATH3012 },
 230	{ USB_DEVICE(0x0cf3, 0x3008), .driver_info = BTUSB_ATH3012 },
 231	{ USB_DEVICE(0x0cf3, 0x311d), .driver_info = BTUSB_ATH3012 },
 232	{ USB_DEVICE(0x0cf3, 0x311e), .driver_info = BTUSB_ATH3012 },
 233	{ USB_DEVICE(0x0cf3, 0x311f), .driver_info = BTUSB_ATH3012 },
 234	{ USB_DEVICE(0x0cf3, 0x3121), .driver_info = BTUSB_ATH3012 },
 235	{ USB_DEVICE(0x0cf3, 0x817a), .driver_info = BTUSB_ATH3012 },
 236	{ USB_DEVICE(0x0cf3, 0x817b), .driver_info = BTUSB_ATH3012 },
 237	{ USB_DEVICE(0x0cf3, 0xe003), .driver_info = BTUSB_ATH3012 },
 238	{ USB_DEVICE(0x0cf3, 0xe004), .driver_info = BTUSB_ATH3012 },
 239	{ USB_DEVICE(0x0cf3, 0xe005), .driver_info = BTUSB_ATH3012 },
 240	{ USB_DEVICE(0x0cf3, 0xe006), .driver_info = BTUSB_ATH3012 },
 241	{ USB_DEVICE(0x13d3, 0x3362), .driver_info = BTUSB_ATH3012 },
 242	{ USB_DEVICE(0x13d3, 0x3375), .driver_info = BTUSB_ATH3012 },
 243	{ USB_DEVICE(0x13d3, 0x3393), .driver_info = BTUSB_ATH3012 },
 244	{ USB_DEVICE(0x13d3, 0x3395), .driver_info = BTUSB_ATH3012 },
 245	{ USB_DEVICE(0x13d3, 0x3402), .driver_info = BTUSB_ATH3012 },
 246	{ USB_DEVICE(0x13d3, 0x3408), .driver_info = BTUSB_ATH3012 },
 247	{ USB_DEVICE(0x13d3, 0x3423), .driver_info = BTUSB_ATH3012 },
 248	{ USB_DEVICE(0x13d3, 0x3432), .driver_info = BTUSB_ATH3012 },
 249	{ USB_DEVICE(0x13d3, 0x3472), .driver_info = BTUSB_ATH3012 },
 250	{ USB_DEVICE(0x13d3, 0x3474), .driver_info = BTUSB_ATH3012 },
 251	{ USB_DEVICE(0x13d3, 0x3487), .driver_info = BTUSB_ATH3012 },
 252	{ USB_DEVICE(0x13d3, 0x3490), .driver_info = BTUSB_ATH3012 },
 253
 254	/* Atheros AR5BBU12 with sflash firmware */
 255	{ USB_DEVICE(0x0489, 0xe02c), .driver_info = BTUSB_IGNORE },
 256
 257	/* Atheros AR5BBU12 with sflash firmware */
 258	{ USB_DEVICE(0x0489, 0xe036), .driver_info = BTUSB_ATH3012 },
 259	{ USB_DEVICE(0x0489, 0xe03c), .driver_info = BTUSB_ATH3012 },
 260
 261	/* QCA ROME chipset */
 262	{ USB_DEVICE(0x0cf3, 0x535b), .driver_info = BTUSB_QCA_ROME |
 263						     BTUSB_WIDEBAND_SPEECH },
 264	{ USB_DEVICE(0x0cf3, 0xe007), .driver_info = BTUSB_QCA_ROME |
 265						     BTUSB_WIDEBAND_SPEECH },
 266	{ USB_DEVICE(0x0cf3, 0xe009), .driver_info = BTUSB_QCA_ROME |
 267						     BTUSB_WIDEBAND_SPEECH },
 268	{ USB_DEVICE(0x0cf3, 0xe010), .driver_info = BTUSB_QCA_ROME |
 269						     BTUSB_WIDEBAND_SPEECH },
 270	{ USB_DEVICE(0x0cf3, 0xe300), .driver_info = BTUSB_QCA_ROME |
 271						     BTUSB_WIDEBAND_SPEECH },
 272	{ USB_DEVICE(0x0cf3, 0xe301), .driver_info = BTUSB_QCA_ROME |
 273						     BTUSB_WIDEBAND_SPEECH },
 274	{ USB_DEVICE(0x0cf3, 0xe360), .driver_info = BTUSB_QCA_ROME |
 275						     BTUSB_WIDEBAND_SPEECH },
 276	{ USB_DEVICE(0x0cf3, 0xe500), .driver_info = BTUSB_QCA_ROME |
 277						     BTUSB_WIDEBAND_SPEECH },
 278	{ USB_DEVICE(0x0489, 0xe092), .driver_info = BTUSB_QCA_ROME |
 279						     BTUSB_WIDEBAND_SPEECH },
 280	{ USB_DEVICE(0x0489, 0xe09f), .driver_info = BTUSB_QCA_ROME |
 281						     BTUSB_WIDEBAND_SPEECH },
 282	{ USB_DEVICE(0x0489, 0xe0a2), .driver_info = BTUSB_QCA_ROME |
 283						     BTUSB_WIDEBAND_SPEECH },
 284	{ USB_DEVICE(0x04ca, 0x3011), .driver_info = BTUSB_QCA_ROME |
 285						     BTUSB_WIDEBAND_SPEECH },
 286	{ USB_DEVICE(0x04ca, 0x3015), .driver_info = BTUSB_QCA_ROME |
 287						     BTUSB_WIDEBAND_SPEECH },
 288	{ USB_DEVICE(0x04ca, 0x3016), .driver_info = BTUSB_QCA_ROME |
 289						     BTUSB_WIDEBAND_SPEECH },
 290	{ USB_DEVICE(0x04ca, 0x301a), .driver_info = BTUSB_QCA_ROME |
 291						     BTUSB_WIDEBAND_SPEECH },
 292	{ USB_DEVICE(0x04ca, 0x3021), .driver_info = BTUSB_QCA_ROME |
 293						     BTUSB_WIDEBAND_SPEECH },
 294	{ USB_DEVICE(0x13d3, 0x3491), .driver_info = BTUSB_QCA_ROME |
 295						     BTUSB_WIDEBAND_SPEECH },
 296	{ USB_DEVICE(0x13d3, 0x3496), .driver_info = BTUSB_QCA_ROME |
 297						     BTUSB_WIDEBAND_SPEECH },
 298	{ USB_DEVICE(0x13d3, 0x3501), .driver_info = BTUSB_QCA_ROME |
 299						     BTUSB_WIDEBAND_SPEECH },
 300
 301	/* QCA WCN6855 chipset */
 302	{ USB_DEVICE(0x0489, 0xe0c7), .driver_info = BTUSB_QCA_WCN6855 |
 303						     BTUSB_WIDEBAND_SPEECH },
 304	{ USB_DEVICE(0x0489, 0xe0c9), .driver_info = BTUSB_QCA_WCN6855 |
 305						     BTUSB_WIDEBAND_SPEECH },
 306	{ USB_DEVICE(0x0489, 0xe0ca), .driver_info = BTUSB_QCA_WCN6855 |
 307						     BTUSB_WIDEBAND_SPEECH },
 308	{ USB_DEVICE(0x0489, 0xe0cb), .driver_info = BTUSB_QCA_WCN6855 |
 309						     BTUSB_WIDEBAND_SPEECH },
 310	{ USB_DEVICE(0x0489, 0xe0cc), .driver_info = BTUSB_QCA_WCN6855 |
 311						     BTUSB_WIDEBAND_SPEECH },
 312	{ USB_DEVICE(0x0489, 0xe0ce), .driver_info = BTUSB_QCA_WCN6855 |
 313						     BTUSB_WIDEBAND_SPEECH },
 314	{ USB_DEVICE(0x0489, 0xe0d0), .driver_info = BTUSB_QCA_WCN6855 |
 315						     BTUSB_WIDEBAND_SPEECH },
 316	{ USB_DEVICE(0x0489, 0xe0d6), .driver_info = BTUSB_QCA_WCN6855 |
 317						     BTUSB_WIDEBAND_SPEECH },
 318	{ USB_DEVICE(0x0489, 0xe0de), .driver_info = BTUSB_QCA_WCN6855 |
 319						     BTUSB_WIDEBAND_SPEECH },
 320	{ USB_DEVICE(0x0489, 0xe0df), .driver_info = BTUSB_QCA_WCN6855 |
 321						     BTUSB_WIDEBAND_SPEECH },
 322	{ USB_DEVICE(0x0489, 0xe0e1), .driver_info = BTUSB_QCA_WCN6855 |
 323						     BTUSB_WIDEBAND_SPEECH },
 324	{ USB_DEVICE(0x0489, 0xe0e3), .driver_info = BTUSB_QCA_WCN6855 |
 325						     BTUSB_WIDEBAND_SPEECH },
 326	{ USB_DEVICE(0x0489, 0xe0ea), .driver_info = BTUSB_QCA_WCN6855 |
 327						     BTUSB_WIDEBAND_SPEECH },
 328	{ USB_DEVICE(0x0489, 0xe0ec), .driver_info = BTUSB_QCA_WCN6855 |
 329						     BTUSB_WIDEBAND_SPEECH },
 330	{ USB_DEVICE(0x04ca, 0x3022), .driver_info = BTUSB_QCA_WCN6855 |
 331						     BTUSB_WIDEBAND_SPEECH },
 332	{ USB_DEVICE(0x04ca, 0x3023), .driver_info = BTUSB_QCA_WCN6855 |
 333						     BTUSB_WIDEBAND_SPEECH },
 334	{ USB_DEVICE(0x04ca, 0x3024), .driver_info = BTUSB_QCA_WCN6855 |
 335						     BTUSB_WIDEBAND_SPEECH },
 336	{ USB_DEVICE(0x04ca, 0x3a22), .driver_info = BTUSB_QCA_WCN6855 |
 337						     BTUSB_WIDEBAND_SPEECH },
 338	{ USB_DEVICE(0x04ca, 0x3a24), .driver_info = BTUSB_QCA_WCN6855 |
 339						     BTUSB_WIDEBAND_SPEECH },
 340	{ USB_DEVICE(0x04ca, 0x3a26), .driver_info = BTUSB_QCA_WCN6855 |
 341						     BTUSB_WIDEBAND_SPEECH },
 342	{ USB_DEVICE(0x04ca, 0x3a27), .driver_info = BTUSB_QCA_WCN6855 |
 343						     BTUSB_WIDEBAND_SPEECH },
 344	{ USB_DEVICE(0x0cf3, 0xe600), .driver_info = BTUSB_QCA_WCN6855 |
 345						     BTUSB_WIDEBAND_SPEECH },
 346	{ USB_DEVICE(0x10ab, 0x9108), .driver_info = BTUSB_QCA_WCN6855 |
 347						     BTUSB_WIDEBAND_SPEECH },
 348	{ USB_DEVICE(0x10ab, 0x9109), .driver_info = BTUSB_QCA_WCN6855 |
 349						     BTUSB_WIDEBAND_SPEECH },
 350	{ USB_DEVICE(0x10ab, 0x9208), .driver_info = BTUSB_QCA_WCN6855 |
 351						     BTUSB_WIDEBAND_SPEECH },
 352	{ USB_DEVICE(0x10ab, 0x9209), .driver_info = BTUSB_QCA_WCN6855 |
 353						     BTUSB_WIDEBAND_SPEECH },
 354	{ USB_DEVICE(0x10ab, 0x9308), .driver_info = BTUSB_QCA_WCN6855 |
 355						     BTUSB_WIDEBAND_SPEECH },
 356	{ USB_DEVICE(0x10ab, 0x9309), .driver_info = BTUSB_QCA_WCN6855 |
 357						     BTUSB_WIDEBAND_SPEECH },
 358	{ USB_DEVICE(0x10ab, 0x9408), .driver_info = BTUSB_QCA_WCN6855 |
 359						     BTUSB_WIDEBAND_SPEECH },
 360	{ USB_DEVICE(0x10ab, 0x9409), .driver_info = BTUSB_QCA_WCN6855 |
 361						     BTUSB_WIDEBAND_SPEECH },
 362	{ USB_DEVICE(0x10ab, 0x9508), .driver_info = BTUSB_QCA_WCN6855 |
 363						     BTUSB_WIDEBAND_SPEECH },
 364	{ USB_DEVICE(0x10ab, 0x9509), .driver_info = BTUSB_QCA_WCN6855 |
 365						     BTUSB_WIDEBAND_SPEECH },
 366	{ USB_DEVICE(0x10ab, 0x9608), .driver_info = BTUSB_QCA_WCN6855 |
 367						     BTUSB_WIDEBAND_SPEECH },
 368	{ USB_DEVICE(0x10ab, 0x9609), .driver_info = BTUSB_QCA_WCN6855 |
 369						     BTUSB_WIDEBAND_SPEECH },
 370	{ USB_DEVICE(0x10ab, 0x9f09), .driver_info = BTUSB_QCA_WCN6855 |
 371						     BTUSB_WIDEBAND_SPEECH },
 372	{ USB_DEVICE(0x28de, 0x1401), .driver_info = BTUSB_QCA_WCN6855 |
 373						     BTUSB_WIDEBAND_SPEECH },
 374
 375	/* QCA WCN785x chipset */
 376	{ USB_DEVICE(0x0cf3, 0xe700), .driver_info = BTUSB_QCA_WCN6855 |
 377						     BTUSB_WIDEBAND_SPEECH },
 378	{ USB_DEVICE(0x0489, 0xe0fc), .driver_info = BTUSB_QCA_WCN6855 |
 379						     BTUSB_WIDEBAND_SPEECH },
 380	{ USB_DEVICE(0x0489, 0xe0f3), .driver_info = BTUSB_QCA_WCN6855 |
 381						     BTUSB_WIDEBAND_SPEECH },
 382	{ USB_DEVICE(0x0489, 0xe100), .driver_info = BTUSB_QCA_WCN6855 |
 383						     BTUSB_WIDEBAND_SPEECH },
 384	{ USB_DEVICE(0x0489, 0xe103), .driver_info = BTUSB_QCA_WCN6855 |
 385						     BTUSB_WIDEBAND_SPEECH },
 386	{ USB_DEVICE(0x0489, 0xe10a), .driver_info = BTUSB_QCA_WCN6855 |
 387						     BTUSB_WIDEBAND_SPEECH },
 388	{ USB_DEVICE(0x0489, 0xe10d), .driver_info = BTUSB_QCA_WCN6855 |
 389						     BTUSB_WIDEBAND_SPEECH },
 390	{ USB_DEVICE(0x0489, 0xe11b), .driver_info = BTUSB_QCA_WCN6855 |
 391						     BTUSB_WIDEBAND_SPEECH },
 392	{ USB_DEVICE(0x0489, 0xe11c), .driver_info = BTUSB_QCA_WCN6855 |
 393						     BTUSB_WIDEBAND_SPEECH },
 394	{ USB_DEVICE(0x0489, 0xe11f), .driver_info = BTUSB_QCA_WCN6855 |
 395						     BTUSB_WIDEBAND_SPEECH },
 396	{ USB_DEVICE(0x0489, 0xe141), .driver_info = BTUSB_QCA_WCN6855 |
 397						     BTUSB_WIDEBAND_SPEECH },
 398	{ USB_DEVICE(0x0489, 0xe14a), .driver_info = BTUSB_QCA_WCN6855 |
 399						     BTUSB_WIDEBAND_SPEECH },
 400	{ USB_DEVICE(0x0489, 0xe14b), .driver_info = BTUSB_QCA_WCN6855 |
 401						     BTUSB_WIDEBAND_SPEECH },
 402	{ USB_DEVICE(0x0489, 0xe14d), .driver_info = BTUSB_QCA_WCN6855 |
 403						     BTUSB_WIDEBAND_SPEECH },
 404	{ USB_DEVICE(0x13d3, 0x3623), .driver_info = BTUSB_QCA_WCN6855 |
 405						     BTUSB_WIDEBAND_SPEECH },
 406	{ USB_DEVICE(0x13d3, 0x3624), .driver_info = BTUSB_QCA_WCN6855 |
 407						     BTUSB_WIDEBAND_SPEECH },
 408	{ USB_DEVICE(0x2c7c, 0x0130), .driver_info = BTUSB_QCA_WCN6855 |
 409						     BTUSB_WIDEBAND_SPEECH },
 410	{ USB_DEVICE(0x2c7c, 0x0131), .driver_info = BTUSB_QCA_WCN6855 |
 411						     BTUSB_WIDEBAND_SPEECH },
 412	{ USB_DEVICE(0x2c7c, 0x0132), .driver_info = BTUSB_QCA_WCN6855 |
 413						     BTUSB_WIDEBAND_SPEECH },
 414
 415	/* Broadcom BCM2035 */
 416	{ USB_DEVICE(0x0a5c, 0x2009), .driver_info = BTUSB_BCM92035 },
 417	{ USB_DEVICE(0x0a5c, 0x200a), .driver_info = BTUSB_WRONG_SCO_MTU },
 418	{ USB_DEVICE(0x0a5c, 0x2035), .driver_info = BTUSB_WRONG_SCO_MTU },
 419
 420	/* Broadcom BCM2045 */
 421	{ USB_DEVICE(0x0a5c, 0x2039), .driver_info = BTUSB_WRONG_SCO_MTU },
 422	{ USB_DEVICE(0x0a5c, 0x2101), .driver_info = BTUSB_WRONG_SCO_MTU },
 423
 424	/* IBM/Lenovo ThinkPad with Broadcom chip */
 425	{ USB_DEVICE(0x0a5c, 0x201e), .driver_info = BTUSB_WRONG_SCO_MTU },
 426	{ USB_DEVICE(0x0a5c, 0x2110), .driver_info = BTUSB_WRONG_SCO_MTU },
 427
 428	/* HP laptop with Broadcom chip */
 429	{ USB_DEVICE(0x03f0, 0x171d), .driver_info = BTUSB_WRONG_SCO_MTU },
 430
 431	/* Dell laptop with Broadcom chip */
 432	{ USB_DEVICE(0x413c, 0x8126), .driver_info = BTUSB_WRONG_SCO_MTU },
 433
 434	/* Dell Wireless 370 and 410 devices */
 435	{ USB_DEVICE(0x413c, 0x8152), .driver_info = BTUSB_WRONG_SCO_MTU },
 436	{ USB_DEVICE(0x413c, 0x8156), .driver_info = BTUSB_WRONG_SCO_MTU },
 437
 438	/* Belkin F8T012 and F8T013 devices */
 439	{ USB_DEVICE(0x050d, 0x0012), .driver_info = BTUSB_WRONG_SCO_MTU },
 440	{ USB_DEVICE(0x050d, 0x0013), .driver_info = BTUSB_WRONG_SCO_MTU },
 441
 442	/* Asus WL-BTD202 device */
 443	{ USB_DEVICE(0x0b05, 0x1715), .driver_info = BTUSB_WRONG_SCO_MTU },
 444
 445	/* Kensington Bluetooth USB adapter */
 446	{ USB_DEVICE(0x047d, 0x105e), .driver_info = BTUSB_WRONG_SCO_MTU },
 447
 448	/* RTX Telecom based adapters with buggy SCO support */
 449	{ USB_DEVICE(0x0400, 0x0807), .driver_info = BTUSB_BROKEN_ISOC },
 450	{ USB_DEVICE(0x0400, 0x080a), .driver_info = BTUSB_BROKEN_ISOC },
 451
 452	/* CONWISE Technology based adapters with buggy SCO support */
 453	{ USB_DEVICE(0x0e5e, 0x6622),
 454	  .driver_info = BTUSB_BROKEN_ISOC | BTUSB_CW6622},
 455
 456	/* Roper Class 1 Bluetooth Dongle (Silicon Wave based) */
 457	{ USB_DEVICE(0x1310, 0x0001), .driver_info = BTUSB_SWAVE },
 458
 459	/* Digianswer devices */
 460	{ USB_DEVICE(0x08fd, 0x0001), .driver_info = BTUSB_DIGIANSWER },
 461	{ USB_DEVICE(0x08fd, 0x0002), .driver_info = BTUSB_IGNORE },
 462
 463	/* CSR BlueCore Bluetooth Sniffer */
 464	{ USB_DEVICE(0x0a12, 0x0002),
 465	  .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
 466
 467	/* Frontline ComProbe Bluetooth Sniffer */
 468	{ USB_DEVICE(0x16d3, 0x0002),
 469	  .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
 470
 471	/* Marvell Bluetooth devices */
 472	{ USB_DEVICE(0x1286, 0x2044), .driver_info = BTUSB_MARVELL },
 473	{ USB_DEVICE(0x1286, 0x2046), .driver_info = BTUSB_MARVELL },
 474	{ USB_DEVICE(0x1286, 0x204e), .driver_info = BTUSB_MARVELL },
 475
 476	/* Intel Bluetooth devices */
 477	{ USB_DEVICE(0x8087, 0x0025), .driver_info = BTUSB_INTEL_COMBINED },
 478	{ USB_DEVICE(0x8087, 0x0026), .driver_info = BTUSB_INTEL_COMBINED },
 479	{ USB_DEVICE(0x8087, 0x0029), .driver_info = BTUSB_INTEL_COMBINED },
 480	{ USB_DEVICE(0x8087, 0x0032), .driver_info = BTUSB_INTEL_COMBINED },
 481	{ USB_DEVICE(0x8087, 0x0033), .driver_info = BTUSB_INTEL_COMBINED },
 482	{ USB_DEVICE(0x8087, 0x0035), .driver_info = BTUSB_INTEL_COMBINED },
 483	{ USB_DEVICE(0x8087, 0x0036), .driver_info = BTUSB_INTEL_COMBINED },
 484	{ USB_DEVICE(0x8087, 0x0037), .driver_info = BTUSB_INTEL_COMBINED },
 485	{ USB_DEVICE(0x8087, 0x0038), .driver_info = BTUSB_INTEL_COMBINED },
 486	{ USB_DEVICE(0x8087, 0x0039), .driver_info = BTUSB_INTEL_COMBINED },
 487	{ USB_DEVICE(0x8087, 0x07da), .driver_info = BTUSB_CSR },
 488	{ USB_DEVICE(0x8087, 0x07dc), .driver_info = BTUSB_INTEL_COMBINED |
 489						     BTUSB_INTEL_NO_WBS_SUPPORT |
 490						     BTUSB_INTEL_BROKEN_INITIAL_NCMD |
 491						     BTUSB_INTEL_BROKEN_SHUTDOWN_LED },
 492	{ USB_DEVICE(0x8087, 0x0a2a), .driver_info = BTUSB_INTEL_COMBINED |
 493						     BTUSB_INTEL_NO_WBS_SUPPORT |
 494						     BTUSB_INTEL_BROKEN_SHUTDOWN_LED },
 495	{ USB_DEVICE(0x8087, 0x0a2b), .driver_info = BTUSB_INTEL_COMBINED },
 496	{ USB_DEVICE(0x8087, 0x0aa7), .driver_info = BTUSB_INTEL_COMBINED |
 497						     BTUSB_INTEL_BROKEN_SHUTDOWN_LED },
 498	{ USB_DEVICE(0x8087, 0x0aaa), .driver_info = BTUSB_INTEL_COMBINED },
 499
 500	/* Other Intel Bluetooth devices */
 501	{ USB_VENDOR_AND_INTERFACE_INFO(0x8087, 0xe0, 0x01, 0x01),
 502	  .driver_info = BTUSB_IGNORE },
 503
 504	/* Realtek 8821CE Bluetooth devices */
 505	{ USB_DEVICE(0x13d3, 0x3529), .driver_info = BTUSB_REALTEK |
 506						     BTUSB_WIDEBAND_SPEECH },
 507	{ USB_DEVICE(0x13d3, 0x3533), .driver_info = BTUSB_REALTEK |
 508						     BTUSB_WIDEBAND_SPEECH },
 509
 510	/* Realtek 8822CE Bluetooth devices */
 511	{ USB_DEVICE(0x0bda, 0xb00c), .driver_info = BTUSB_REALTEK |
 512						     BTUSB_WIDEBAND_SPEECH },
 513	{ USB_DEVICE(0x0bda, 0xc822), .driver_info = BTUSB_REALTEK |
 514						     BTUSB_WIDEBAND_SPEECH },
 515
 516	/* Realtek 8822CU Bluetooth devices */
 517	{ USB_DEVICE(0x13d3, 0x3549), .driver_info = BTUSB_REALTEK |
 518						     BTUSB_WIDEBAND_SPEECH },
 519
 520	/* Realtek 8851BE Bluetooth devices */
 521	{ USB_DEVICE(0x0bda, 0xb850), .driver_info = BTUSB_REALTEK },
 522	{ USB_DEVICE(0x13d3, 0x3600), .driver_info = BTUSB_REALTEK },
 523	{ USB_DEVICE(0x13d3, 0x3601), .driver_info = BTUSB_REALTEK },
 524	{ USB_DEVICE(0x0489, 0xe112), .driver_info = BTUSB_REALTEK |
 525						     BTUSB_WIDEBAND_SPEECH },
 526
 527	/* Realtek 8851BU Bluetooth devices */
 528	{ USB_DEVICE(0x3625, 0x010b), .driver_info = BTUSB_REALTEK |
 529						     BTUSB_WIDEBAND_SPEECH },
 530	{ USB_DEVICE(0x2001, 0x332a), .driver_info = BTUSB_REALTEK |
 531						     BTUSB_WIDEBAND_SPEECH },
 532	{ USB_DEVICE(0x7392, 0xe611), .driver_info = BTUSB_REALTEK |
 533						     BTUSB_WIDEBAND_SPEECH },
 534
 535	/* Realtek 8852AE Bluetooth devices */
 536	{ USB_DEVICE(0x0bda, 0x2852), .driver_info = BTUSB_REALTEK |
 537						     BTUSB_WIDEBAND_SPEECH },
 538	{ USB_DEVICE(0x0bda, 0xc852), .driver_info = BTUSB_REALTEK |
 539						     BTUSB_WIDEBAND_SPEECH },
 540	{ USB_DEVICE(0x0bda, 0x385a), .driver_info = BTUSB_REALTEK |
 541						     BTUSB_WIDEBAND_SPEECH },
 542	{ USB_DEVICE(0x0bda, 0x4852), .driver_info = BTUSB_REALTEK |
 543						     BTUSB_WIDEBAND_SPEECH },
 544	{ USB_DEVICE(0x04c5, 0x165c), .driver_info = BTUSB_REALTEK |
 545						     BTUSB_WIDEBAND_SPEECH },
 546	{ USB_DEVICE(0x04ca, 0x4006), .driver_info = BTUSB_REALTEK |
 547						     BTUSB_WIDEBAND_SPEECH },
 548	{ USB_DEVICE(0x0cb8, 0xc549), .driver_info = BTUSB_REALTEK |
 549						     BTUSB_WIDEBAND_SPEECH },
 550
 551	/* Realtek 8852CE Bluetooth devices */
 552	{ USB_DEVICE(0x04ca, 0x4007), .driver_info = BTUSB_REALTEK |
 553						     BTUSB_WIDEBAND_SPEECH },
 554	{ USB_DEVICE(0x04c5, 0x1675), .driver_info = BTUSB_REALTEK |
 555						     BTUSB_WIDEBAND_SPEECH },
 556	{ USB_DEVICE(0x0cb8, 0xc558), .driver_info = BTUSB_REALTEK |
 557						     BTUSB_WIDEBAND_SPEECH },
 558	{ USB_DEVICE(0x13d3, 0x3587), .driver_info = BTUSB_REALTEK |
 559						     BTUSB_WIDEBAND_SPEECH },
 560	{ USB_DEVICE(0x13d3, 0x3586), .driver_info = BTUSB_REALTEK |
 561						     BTUSB_WIDEBAND_SPEECH },
 562	{ USB_DEVICE(0x13d3, 0x3592), .driver_info = BTUSB_REALTEK |
 563						     BTUSB_WIDEBAND_SPEECH },
 564	{ USB_DEVICE(0x13d3, 0x3612), .driver_info = BTUSB_REALTEK |
 565						     BTUSB_WIDEBAND_SPEECH },
 566	{ USB_DEVICE(0x0489, 0xe122), .driver_info = BTUSB_REALTEK |
 567						     BTUSB_WIDEBAND_SPEECH },
 568
 569	/* Realtek 8852BE Bluetooth devices */
 570	{ USB_DEVICE(0x0cb8, 0xc559), .driver_info = BTUSB_REALTEK |
 571						     BTUSB_WIDEBAND_SPEECH },
 572	{ USB_DEVICE(0x0bda, 0x4853), .driver_info = BTUSB_REALTEK |
 573						     BTUSB_WIDEBAND_SPEECH },
 574	{ USB_DEVICE(0x0bda, 0x887b), .driver_info = BTUSB_REALTEK |
 575						     BTUSB_WIDEBAND_SPEECH },
 576	{ USB_DEVICE(0x0bda, 0xb85b), .driver_info = BTUSB_REALTEK |
 577						     BTUSB_WIDEBAND_SPEECH },
 578	{ USB_DEVICE(0x13d3, 0x3570), .driver_info = BTUSB_REALTEK |
 579						     BTUSB_WIDEBAND_SPEECH },
 580	{ USB_DEVICE(0x13d3, 0x3571), .driver_info = BTUSB_REALTEK |
 581						     BTUSB_WIDEBAND_SPEECH },
 582	{ USB_DEVICE(0x13d3, 0x3572), .driver_info = BTUSB_REALTEK |
 583						     BTUSB_WIDEBAND_SPEECH },
 584	{ USB_DEVICE(0x13d3, 0x3591), .driver_info = BTUSB_REALTEK |
 585						     BTUSB_WIDEBAND_SPEECH },
 586	{ USB_DEVICE(0x13d3, 0x3618), .driver_info = BTUSB_REALTEK |
 587						     BTUSB_WIDEBAND_SPEECH },
 588	{ USB_DEVICE(0x0489, 0xe123), .driver_info = BTUSB_REALTEK |
 589						     BTUSB_WIDEBAND_SPEECH },
 590	{ USB_DEVICE(0x0489, 0xe125), .driver_info = BTUSB_REALTEK |
 591						     BTUSB_WIDEBAND_SPEECH },
 592
 593	/* Realtek 8852BT/8852BE-VT Bluetooth devices */
 594	{ USB_DEVICE(0x0bda, 0x8520), .driver_info = BTUSB_REALTEK |
 595						     BTUSB_WIDEBAND_SPEECH },
 596	{ USB_DEVICE(0x0489, 0xe12f), .driver_info = BTUSB_REALTEK |
 597						     BTUSB_WIDEBAND_SPEECH },
 598	{ USB_DEVICE(0x13d3, 0x3618), .driver_info = BTUSB_REALTEK |
 599						     BTUSB_WIDEBAND_SPEECH },
 600	{ USB_DEVICE(0x13d3, 0x3619), .driver_info = BTUSB_REALTEK |
 601						     BTUSB_WIDEBAND_SPEECH },
 602
 603	/* Realtek 8922AE Bluetooth devices */
 604	{ USB_DEVICE(0x0bda, 0x8922), .driver_info = BTUSB_REALTEK |
 605						     BTUSB_WIDEBAND_SPEECH },
 606	{ USB_DEVICE(0x13d3, 0x3617), .driver_info = BTUSB_REALTEK |
 607						     BTUSB_WIDEBAND_SPEECH },
 608	{ USB_DEVICE(0x13d3, 0x3616), .driver_info = BTUSB_REALTEK |
 609						     BTUSB_WIDEBAND_SPEECH },
 610	{ USB_DEVICE(0x0489, 0xe130), .driver_info = BTUSB_REALTEK |
 611						     BTUSB_WIDEBAND_SPEECH },
 612
 613	/* Realtek Bluetooth devices */
 614	{ USB_VENDOR_AND_INTERFACE_INFO(0x0bda, 0xe0, 0x01, 0x01),
 615	  .driver_info = BTUSB_REALTEK },
 616
 617	/* MediaTek Bluetooth devices */
 618	{ USB_VENDOR_AND_INTERFACE_INFO(0x0e8d, 0xe0, 0x01, 0x01),
 619	  .driver_info = BTUSB_MEDIATEK |
 620			 BTUSB_WIDEBAND_SPEECH },
 621
 622	/* Additional MediaTek MT7615E Bluetooth devices */
 623	{ USB_DEVICE(0x13d3, 0x3560), .driver_info = BTUSB_MEDIATEK},
 624
 625	/* Additional MediaTek MT7663 Bluetooth devices */
 626	{ USB_DEVICE(0x043e, 0x310c), .driver_info = BTUSB_MEDIATEK |
 627						     BTUSB_WIDEBAND_SPEECH },
 628	{ USB_DEVICE(0x04ca, 0x3801), .driver_info = BTUSB_MEDIATEK |
 629						     BTUSB_WIDEBAND_SPEECH },
 630
 631	/* Additional MediaTek MT7668 Bluetooth devices */
 632	{ USB_DEVICE(0x043e, 0x3109), .driver_info = BTUSB_MEDIATEK |
 633						     BTUSB_WIDEBAND_SPEECH },
 634
 635	/* Additional MediaTek MT7920 Bluetooth devices */
 636	{ USB_DEVICE(0x0489, 0xe134), .driver_info = BTUSB_MEDIATEK |
 637						     BTUSB_WIDEBAND_SPEECH },
 638	{ USB_DEVICE(0x0489, 0xe135), .driver_info = BTUSB_MEDIATEK |
 639						     BTUSB_WIDEBAND_SPEECH },
 640	{ USB_DEVICE(0x13d3, 0x3620), .driver_info = BTUSB_MEDIATEK |
 641						     BTUSB_WIDEBAND_SPEECH },
 642	{ USB_DEVICE(0x13d3, 0x3621), .driver_info = BTUSB_MEDIATEK |
 643						     BTUSB_WIDEBAND_SPEECH },
 644	{ USB_DEVICE(0x13d3, 0x3622), .driver_info = BTUSB_MEDIATEK |
 645						     BTUSB_WIDEBAND_SPEECH },
 646	{ USB_DEVICE(0x0489, 0xe158), .driver_info = BTUSB_MEDIATEK |
 647						     BTUSB_WIDEBAND_SPEECH },
 648
 649	/* Additional MediaTek MT7921 Bluetooth devices */
 650	{ USB_DEVICE(0x0489, 0xe0c8), .driver_info = BTUSB_MEDIATEK |
 651						     BTUSB_WIDEBAND_SPEECH },
 652	{ USB_DEVICE(0x0489, 0xe0cd), .driver_info = BTUSB_MEDIATEK |
 653						     BTUSB_WIDEBAND_SPEECH },
 654	{ USB_DEVICE(0x0489, 0xe0e0), .driver_info = BTUSB_MEDIATEK |
 655						     BTUSB_WIDEBAND_SPEECH },
 656	{ USB_DEVICE(0x0489, 0xe0f2), .driver_info = BTUSB_MEDIATEK |
 657						     BTUSB_WIDEBAND_SPEECH },
 658	{ USB_DEVICE(0x04ca, 0x3802), .driver_info = BTUSB_MEDIATEK |
 659						     BTUSB_WIDEBAND_SPEECH },
 660	{ USB_DEVICE(0x0e8d, 0x0608), .driver_info = BTUSB_MEDIATEK |
 661						     BTUSB_WIDEBAND_SPEECH },
 662	{ USB_DEVICE(0x13d3, 0x3563), .driver_info = BTUSB_MEDIATEK |
 663						     BTUSB_WIDEBAND_SPEECH },
 664	{ USB_DEVICE(0x13d3, 0x3564), .driver_info = BTUSB_MEDIATEK |
 665						     BTUSB_WIDEBAND_SPEECH },
 666	{ USB_DEVICE(0x13d3, 0x3567), .driver_info = BTUSB_MEDIATEK |
 667						     BTUSB_WIDEBAND_SPEECH },
 668	{ USB_DEVICE(0x13d3, 0x3576), .driver_info = BTUSB_MEDIATEK |
 669						     BTUSB_WIDEBAND_SPEECH },
 670	{ USB_DEVICE(0x13d3, 0x3578), .driver_info = BTUSB_MEDIATEK |
 671						     BTUSB_WIDEBAND_SPEECH },
 672	{ USB_DEVICE(0x13d3, 0x3583), .driver_info = BTUSB_MEDIATEK |
 673						     BTUSB_WIDEBAND_SPEECH },
 674	{ USB_DEVICE(0x13d3, 0x3606), .driver_info = BTUSB_MEDIATEK |
 675						     BTUSB_WIDEBAND_SPEECH },
 676
 677	/* MediaTek MT7922 Bluetooth devices */
 678	{ USB_DEVICE(0x13d3, 0x3585), .driver_info = BTUSB_MEDIATEK |
 679						     BTUSB_WIDEBAND_SPEECH },
 680	{ USB_DEVICE(0x13d3, 0x3610), .driver_info = BTUSB_MEDIATEK |
 681						     BTUSB_WIDEBAND_SPEECH },
 682
 683	/* MediaTek MT7922A Bluetooth devices */
 684	{ USB_DEVICE(0x0489, 0xe0d8), .driver_info = BTUSB_MEDIATEK |
 685						     BTUSB_WIDEBAND_SPEECH },
 686	{ USB_DEVICE(0x0489, 0xe0d9), .driver_info = BTUSB_MEDIATEK |
 687						     BTUSB_WIDEBAND_SPEECH },
 688	{ USB_DEVICE(0x0489, 0xe0e2), .driver_info = BTUSB_MEDIATEK |
 689						     BTUSB_WIDEBAND_SPEECH },
 690	{ USB_DEVICE(0x0489, 0xe0e4), .driver_info = BTUSB_MEDIATEK |
 691						     BTUSB_WIDEBAND_SPEECH },
 692	{ USB_DEVICE(0x0489, 0xe0f1), .driver_info = BTUSB_MEDIATEK |
 693						     BTUSB_WIDEBAND_SPEECH },
 694	{ USB_DEVICE(0x0489, 0xe0f2), .driver_info = BTUSB_MEDIATEK |
 695						     BTUSB_WIDEBAND_SPEECH },
 696	{ USB_DEVICE(0x0489, 0xe0f5), .driver_info = BTUSB_MEDIATEK |
 697						     BTUSB_WIDEBAND_SPEECH },
 698	{ USB_DEVICE(0x0489, 0xe0f6), .driver_info = BTUSB_MEDIATEK |
 699						     BTUSB_WIDEBAND_SPEECH },
 700	{ USB_DEVICE(0x0489, 0xe102), .driver_info = BTUSB_MEDIATEK |
 701						     BTUSB_WIDEBAND_SPEECH },
 702	{ USB_DEVICE(0x0489, 0xe152), .driver_info = BTUSB_MEDIATEK |
 703						     BTUSB_WIDEBAND_SPEECH },
 704	{ USB_DEVICE(0x0489, 0xe153), .driver_info = BTUSB_MEDIATEK |
 705						     BTUSB_WIDEBAND_SPEECH },
 706	{ USB_DEVICE(0x0489, 0xe170), .driver_info = BTUSB_MEDIATEK |
 707						     BTUSB_WIDEBAND_SPEECH },
 708	{ USB_DEVICE(0x04ca, 0x3804), .driver_info = BTUSB_MEDIATEK |
 709						     BTUSB_WIDEBAND_SPEECH },
 710	{ USB_DEVICE(0x04ca, 0x38e4), .driver_info = BTUSB_MEDIATEK |
 711						     BTUSB_WIDEBAND_SPEECH },
 712	{ USB_DEVICE(0x13d3, 0x3568), .driver_info = BTUSB_MEDIATEK |
 713						     BTUSB_WIDEBAND_SPEECH },
 714	{ USB_DEVICE(0x13d3, 0x3584), .driver_info = BTUSB_MEDIATEK |
 715						     BTUSB_WIDEBAND_SPEECH },
 716	{ USB_DEVICE(0x13d3, 0x3605), .driver_info = BTUSB_MEDIATEK |
 717						     BTUSB_WIDEBAND_SPEECH },
 718	{ USB_DEVICE(0x13d3, 0x3607), .driver_info = BTUSB_MEDIATEK |
 719						     BTUSB_WIDEBAND_SPEECH },
 720	{ USB_DEVICE(0x13d3, 0x3614), .driver_info = BTUSB_MEDIATEK |
 721						     BTUSB_WIDEBAND_SPEECH },
 722	{ USB_DEVICE(0x13d3, 0x3615), .driver_info = BTUSB_MEDIATEK |
 723						     BTUSB_WIDEBAND_SPEECH },
 724	{ USB_DEVICE(0x13d3, 0x3633), .driver_info = BTUSB_MEDIATEK |
 725						     BTUSB_WIDEBAND_SPEECH },
 726	{ USB_DEVICE(0x35f5, 0x7922), .driver_info = BTUSB_MEDIATEK |
 727						     BTUSB_WIDEBAND_SPEECH },
 728
 729	/* Additional MediaTek MT7925 Bluetooth devices */
 730	{ USB_DEVICE(0x0489, 0xe111), .driver_info = BTUSB_MEDIATEK |
 731						     BTUSB_WIDEBAND_SPEECH },
 732	{ USB_DEVICE(0x0489, 0xe113), .driver_info = BTUSB_MEDIATEK |
 733						     BTUSB_WIDEBAND_SPEECH },
 734	{ USB_DEVICE(0x0489, 0xe118), .driver_info = BTUSB_MEDIATEK |
 735						     BTUSB_WIDEBAND_SPEECH },
 736	{ USB_DEVICE(0x0489, 0xe11e), .driver_info = BTUSB_MEDIATEK |
 737						     BTUSB_WIDEBAND_SPEECH },
 738	{ USB_DEVICE(0x0489, 0xe124), .driver_info = BTUSB_MEDIATEK |
 739						     BTUSB_WIDEBAND_SPEECH },
 740	{ USB_DEVICE(0x0489, 0xe139), .driver_info = BTUSB_MEDIATEK |
 741						     BTUSB_WIDEBAND_SPEECH },
 742	{ USB_DEVICE(0x0489, 0xe14e), .driver_info = BTUSB_MEDIATEK |
 743						     BTUSB_WIDEBAND_SPEECH },
 744	{ USB_DEVICE(0x0489, 0xe14f), .driver_info = BTUSB_MEDIATEK |
 745						     BTUSB_WIDEBAND_SPEECH },
 746	{ USB_DEVICE(0x0489, 0xe150), .driver_info = BTUSB_MEDIATEK |
 747						     BTUSB_WIDEBAND_SPEECH },
 748	{ USB_DEVICE(0x0489, 0xe151), .driver_info = BTUSB_MEDIATEK |
 749						     BTUSB_WIDEBAND_SPEECH },
 750	{ USB_DEVICE(0x13d3, 0x3602), .driver_info = BTUSB_MEDIATEK |
 751						     BTUSB_WIDEBAND_SPEECH },
 752	{ USB_DEVICE(0x13d3, 0x3603), .driver_info = BTUSB_MEDIATEK |
 753						     BTUSB_WIDEBAND_SPEECH },
 754	{ USB_DEVICE(0x13d3, 0x3604), .driver_info = BTUSB_MEDIATEK |
 755						     BTUSB_WIDEBAND_SPEECH },
 756	{ USB_DEVICE(0x13d3, 0x3608), .driver_info = BTUSB_MEDIATEK |
 757						     BTUSB_WIDEBAND_SPEECH },
 758	{ USB_DEVICE(0x13d3, 0x3613), .driver_info = BTUSB_MEDIATEK |
 759						     BTUSB_WIDEBAND_SPEECH },
 760	{ USB_DEVICE(0x13d3, 0x3627), .driver_info = BTUSB_MEDIATEK |
 761						     BTUSB_WIDEBAND_SPEECH },
 762	{ USB_DEVICE(0x13d3, 0x3628), .driver_info = BTUSB_MEDIATEK |
 763						     BTUSB_WIDEBAND_SPEECH },
 764	{ USB_DEVICE(0x13d3, 0x3630), .driver_info = BTUSB_MEDIATEK |
 765						     BTUSB_WIDEBAND_SPEECH },
 766	{ USB_DEVICE(0x2c7c, 0x7009), .driver_info = BTUSB_MEDIATEK |
 767						     BTUSB_WIDEBAND_SPEECH },
 768
 769	/* Additional Realtek 8723AE Bluetooth devices */
 770	{ USB_DEVICE(0x0930, 0x021d), .driver_info = BTUSB_REALTEK },
 771	{ USB_DEVICE(0x13d3, 0x3394), .driver_info = BTUSB_REALTEK },
 772
 773	/* Additional Realtek 8723BE Bluetooth devices */
 774	{ USB_DEVICE(0x0489, 0xe085), .driver_info = BTUSB_REALTEK },
 775	{ USB_DEVICE(0x0489, 0xe08b), .driver_info = BTUSB_REALTEK },
 776	{ USB_DEVICE(0x04f2, 0xb49f), .driver_info = BTUSB_REALTEK },
 777	{ USB_DEVICE(0x13d3, 0x3410), .driver_info = BTUSB_REALTEK },
 778	{ USB_DEVICE(0x13d3, 0x3416), .driver_info = BTUSB_REALTEK },
 779	{ USB_DEVICE(0x13d3, 0x3459), .driver_info = BTUSB_REALTEK },
 780	{ USB_DEVICE(0x13d3, 0x3494), .driver_info = BTUSB_REALTEK },
 781
 782	/* Additional Realtek 8723BU Bluetooth devices */
 783	{ USB_DEVICE(0x7392, 0xa611), .driver_info = BTUSB_REALTEK },
 784	{ USB_DEVICE(0x2c0a, 0x8761), .driver_info = BTUSB_REALTEK },
 785
 786	/* Additional Realtek 8723DE Bluetooth devices */
 787	{ USB_DEVICE(0x0bda, 0xb009), .driver_info = BTUSB_REALTEK },
 788	{ USB_DEVICE(0x2ff8, 0xb011), .driver_info = BTUSB_REALTEK },
 789
 790	/* Additional Realtek 8761BUV Bluetooth devices */
 791	{ USB_DEVICE(0x2357, 0x0604), .driver_info = BTUSB_REALTEK |
 792						     BTUSB_WIDEBAND_SPEECH },
 793	{ USB_DEVICE(0x0b05, 0x190e), .driver_info = BTUSB_REALTEK |
 794	  					     BTUSB_WIDEBAND_SPEECH },
 795	{ USB_DEVICE(0x2550, 0x8761), .driver_info = BTUSB_REALTEK |
 796						     BTUSB_WIDEBAND_SPEECH },
 797	{ USB_DEVICE(0x0bda, 0x8771), .driver_info = BTUSB_REALTEK |
 798						     BTUSB_WIDEBAND_SPEECH },
 799	{ USB_DEVICE(0x6655, 0x8771), .driver_info = BTUSB_REALTEK |
 800						     BTUSB_WIDEBAND_SPEECH },
 801	{ USB_DEVICE(0x7392, 0xc611), .driver_info = BTUSB_REALTEK |
 802						     BTUSB_WIDEBAND_SPEECH },
 803	{ USB_DEVICE(0x2b89, 0x8761), .driver_info = BTUSB_REALTEK |
 804						     BTUSB_WIDEBAND_SPEECH },
 805	{ USB_DEVICE(0x2b89, 0x6275), .driver_info = BTUSB_REALTEK |
 806						     BTUSB_WIDEBAND_SPEECH },
 807
 808	/* Additional Realtek 8821AE Bluetooth devices */
 809	{ USB_DEVICE(0x0b05, 0x17dc), .driver_info = BTUSB_REALTEK },
 810	{ USB_DEVICE(0x13d3, 0x3414), .driver_info = BTUSB_REALTEK },
 811	{ USB_DEVICE(0x13d3, 0x3458), .driver_info = BTUSB_REALTEK },
 812	{ USB_DEVICE(0x13d3, 0x3461), .driver_info = BTUSB_REALTEK },
 813	{ USB_DEVICE(0x13d3, 0x3462), .driver_info = BTUSB_REALTEK },
 814
 815	/* Additional Realtek 8822BE Bluetooth devices */
 816	{ USB_DEVICE(0x13d3, 0x3526), .driver_info = BTUSB_REALTEK },
 817	{ USB_DEVICE(0x0b05, 0x185c), .driver_info = BTUSB_REALTEK },
 818
 819	/* Additional Realtek 8822CE Bluetooth devices */
 820	{ USB_DEVICE(0x04ca, 0x4005), .driver_info = BTUSB_REALTEK |
 821						     BTUSB_WIDEBAND_SPEECH },
 822	{ USB_DEVICE(0x04c5, 0x161f), .driver_info = BTUSB_REALTEK |
 823						     BTUSB_WIDEBAND_SPEECH },
 824	{ USB_DEVICE(0x0b05, 0x18ef), .driver_info = BTUSB_REALTEK |
 825						     BTUSB_WIDEBAND_SPEECH },
 826	{ USB_DEVICE(0x13d3, 0x3548), .driver_info = BTUSB_REALTEK |
 827						     BTUSB_WIDEBAND_SPEECH },
 828	{ USB_DEVICE(0x13d3, 0x3549), .driver_info = BTUSB_REALTEK |
 829						     BTUSB_WIDEBAND_SPEECH },
 830	{ USB_DEVICE(0x13d3, 0x3553), .driver_info = BTUSB_REALTEK |
 831						     BTUSB_WIDEBAND_SPEECH },
 832	{ USB_DEVICE(0x13d3, 0x3555), .driver_info = BTUSB_REALTEK |
 833						     BTUSB_WIDEBAND_SPEECH },
 834	{ USB_DEVICE(0x2ff8, 0x3051), .driver_info = BTUSB_REALTEK |
 835						     BTUSB_WIDEBAND_SPEECH },
 836	{ USB_DEVICE(0x1358, 0xc123), .driver_info = BTUSB_REALTEK |
 837						     BTUSB_WIDEBAND_SPEECH },
 838	{ USB_DEVICE(0x0bda, 0xc123), .driver_info = BTUSB_REALTEK |
 839						     BTUSB_WIDEBAND_SPEECH },
 840	{ USB_DEVICE(0x0cb5, 0xc547), .driver_info = BTUSB_REALTEK |
 841						     BTUSB_WIDEBAND_SPEECH },
 842
 843	/* Barrot Technology Bluetooth devices */
 844	{ USB_DEVICE(0x33fa, 0x0010), .driver_info = BTUSB_BARROT },
 845	{ USB_DEVICE(0x33fa, 0x0012), .driver_info = BTUSB_BARROT },
 846
 847	/* Actions Semiconductor ATS2851 based devices */
 848	{ USB_DEVICE(0x10d7, 0xb012), .driver_info = BTUSB_ACTIONS_SEMI },
 849
 850	/* Silicon Wave based devices */
 851	{ USB_DEVICE(0x0c10, 0x0000), .driver_info = BTUSB_SWAVE },
 852
 853	{ }	/* Terminating entry */
 854};
 855
 856/* The Bluetooth USB module build into some devices needs to be reset on resume,
 857 * this is a problem with the platform (likely shutting off all power) not with
 858 * the module itself. So we use a DMI list to match known broken platforms.
 859 */
 860static const struct dmi_system_id btusb_needs_reset_resume_table[] = {
 861	{
 862		/* Dell OptiPlex 3060 (QCA ROME device 0cf3:e007) */
 863		.matches = {
 864			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
 865			DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 3060"),
 866		},
 867	},
 868	{
 869		/* Dell XPS 9360 (QCA ROME device 0cf3:e300) */
 870		.matches = {
 871			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
 872			DMI_MATCH(DMI_PRODUCT_NAME, "XPS 13 9360"),
 873		},
 874	},
 875	{
 876		/* Dell Inspiron 5565 (QCA ROME device 0cf3:e009) */
 877		.matches = {
 878			DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
 879			DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5565"),
 880		},
 881	},
 882	{}
 883};
 884
 885struct qca_dump_info {
 886	/* fields for dump collection */
 887	u16 id_vendor;
 888	u16 id_product;
 889	u32 fw_version;
 890	u32 controller_id;
 891	u32 ram_dump_size;
 892	u16 ram_dump_seqno;
 893};
 894
 895#define BTUSB_MAX_ISOC_FRAMES	10
 896
 897#define BTUSB_INTR_RUNNING	0
 898#define BTUSB_BULK_RUNNING	1
 899#define BTUSB_ISOC_RUNNING	2
 900#define BTUSB_SUSPENDING	3
 901#define BTUSB_DID_ISO_RESUME	4
 902#define BTUSB_BOOTLOADER	5
 903#define BTUSB_DOWNLOADING	6
 904#define BTUSB_FIRMWARE_LOADED	7
 905#define BTUSB_FIRMWARE_FAILED	8
 906#define BTUSB_BOOTING		9
 907#define BTUSB_DIAG_RUNNING	10
 908#define BTUSB_OOB_WAKE_ENABLED	11
 909#define BTUSB_HW_RESET_ACTIVE	12
 910#define BTUSB_TX_WAIT_VND_EVT	13
 911#define BTUSB_WAKEUP_AUTOSUSPEND	14
 912#define BTUSB_USE_ALT3_FOR_WBS	15
 913#define BTUSB_ALT6_CONTINUOUS_TX	16
 914#define BTUSB_HW_SSR_ACTIVE	17
 915
 916struct btusb_data {
 917	struct hci_dev       *hdev;
 918	struct usb_device    *udev;
 919	struct usb_interface *intf;
 920	struct usb_interface *isoc;
 921	struct usb_interface *diag;
 922	unsigned isoc_ifnum;
 923
 924	unsigned long flags;
 925
 926	bool poll_sync;
 927	int intr_interval;
 928	struct work_struct  work;
 929	struct work_struct  waker;
 930	struct delayed_work rx_work;
 931
 932	struct sk_buff_head acl_q;
 933
 934	struct usb_anchor deferred;
 935	struct usb_anchor tx_anchor;
 936	int tx_in_flight;
 937	spinlock_t txlock;
 938
 939	struct usb_anchor intr_anchor;
 940	struct usb_anchor bulk_anchor;
 941	struct usb_anchor isoc_anchor;
 942	struct usb_anchor diag_anchor;
 943	struct usb_anchor ctrl_anchor;
 944	spinlock_t rxlock;
 945
 946	struct sk_buff *evt_skb;
 947	struct sk_buff *acl_skb;
 948	struct sk_buff *sco_skb;
 949
 950	struct usb_endpoint_descriptor *intr_ep;
 951	struct usb_endpoint_descriptor *bulk_tx_ep;
 952	struct usb_endpoint_descriptor *bulk_rx_ep;
 953	struct usb_endpoint_descriptor *isoc_tx_ep;
 954	struct usb_endpoint_descriptor *isoc_rx_ep;
 955	struct usb_endpoint_descriptor *diag_tx_ep;
 956	struct usb_endpoint_descriptor *diag_rx_ep;
 957
 958	struct gpio_desc *reset_gpio;
 959
 960	__u8 cmdreq_type;
 961	__u8 cmdreq;
 962
 963	unsigned int sco_num;
 964	unsigned int air_mode;
 965	bool usb_alt6_packet_flow;
 966	int isoc_altsetting;
 967	int suspend_count;
 968
 969	int (*recv_event)(struct hci_dev *hdev, struct sk_buff *skb);
 970	int (*recv_acl)(struct hci_dev *hdev, struct sk_buff *skb);
 971	int (*recv_bulk)(struct btusb_data *data, void *buffer, int count);
 972
 973	int (*setup_on_usb)(struct hci_dev *hdev);
 974
 975	int (*suspend)(struct hci_dev *hdev);
 976	int (*resume)(struct hci_dev *hdev);
 977	int (*disconnect)(struct hci_dev *hdev);
 978
 979	int oob_wake_irq;   /* irq for out-of-band wake-on-bt */
 980
 981	struct qca_dump_info qca_dump;
 982};
 983
 984static void btusb_reset(struct hci_dev *hdev)
 985{
 986	struct btusb_data *data;
 987	int err;
 988
 989	data = hci_get_drvdata(hdev);
 990	/* This is not an unbalanced PM reference since the device will reset */
 991	err = usb_autopm_get_interface(data->intf);
 992	if (err) {
 993		bt_dev_err(hdev, "Failed usb_autopm_get_interface: %d", err);
 994		return;
 995	}
 996
 997	bt_dev_err(hdev, "Resetting usb device.");
 998	usb_queue_reset_device(data->intf);
 999}
1000
1001static void btusb_intel_reset(struct hci_dev *hdev)
1002{
1003	struct btusb_data *data = hci_get_drvdata(hdev);
1004	struct gpio_desc *reset_gpio = data->reset_gpio;
1005	struct btintel_data *intel_data = hci_get_priv(hdev);
1006
1007	if (intel_data->acpi_reset_method) {
1008		if (test_and_set_bit(INTEL_ACPI_RESET_ACTIVE, intel_data->flags)) {
1009			bt_dev_err(hdev, "acpi: last reset failed ? Not resetting again");
1010			return;
1011		}
1012
1013		bt_dev_err(hdev, "Initiating acpi reset method");
1014		/* If ACPI reset method fails, lets try with legacy GPIO
1015		 * toggling
1016		 */
1017		if (!intel_data->acpi_reset_method(hdev)) {
1018			return;
1019		}
1020	}
1021
1022	if (!reset_gpio) {
1023		btusb_reset(hdev);
1024		return;
1025	}
1026
1027	/*
1028	 * Toggle the hard reset line if the platform provides one. The reset
1029	 * is going to yank the device off the USB and then replug. So doing
1030	 * once is enough. The cleanup is handled correctly on the way out
1031	 * (standard USB disconnect), and the new device is detected cleanly
1032	 * and bound to the driver again like it should be.
1033	 */
1034	if (test_and_set_bit(BTUSB_HW_RESET_ACTIVE, &data->flags)) {
1035		bt_dev_err(hdev, "last reset failed? Not resetting again");
1036		return;
1037	}
1038
1039	bt_dev_err(hdev, "Initiating HW reset via gpio");
1040	gpiod_set_value_cansleep(reset_gpio, 1);
1041	msleep(100);
1042	gpiod_set_value_cansleep(reset_gpio, 0);
1043}
1044
1045#define RTK_DEVCOREDUMP_CODE_MEMDUMP		0x01
1046#define RTK_DEVCOREDUMP_CODE_HW_ERR		0x02
1047#define RTK_DEVCOREDUMP_CODE_CMD_TIMEOUT	0x03
1048
1049#define RTK_SUB_EVENT_CODE_COREDUMP		0x34
1050
1051struct rtk_dev_coredump_hdr {
1052	u8 type;
1053	u8 code;
1054	u8 reserved[2];
1055} __packed;
1056
1057static inline void btusb_rtl_alloc_devcoredump(struct hci_dev *hdev,
1058		struct rtk_dev_coredump_hdr *hdr, u8 *buf, u32 len)
1059{
1060	struct sk_buff *skb;
1061
1062	skb = alloc_skb(len + sizeof(*hdr), GFP_ATOMIC);
1063	if (!skb)
1064		return;
1065
1066	skb_put_data(skb, hdr, sizeof(*hdr));
1067	if (len)
1068		skb_put_data(skb, buf, len);
1069
1070	if (!hci_devcd_init(hdev, skb->len)) {
1071		hci_devcd_append(hdev, skb);
1072		hci_devcd_complete(hdev);
1073	} else {
1074		bt_dev_err(hdev, "RTL: Failed to generate devcoredump");
1075		kfree_skb(skb);
1076	}
1077}
1078
1079static void btusb_rtl_reset(struct hci_dev *hdev)
1080{
1081	struct btusb_data *data = hci_get_drvdata(hdev);
1082	struct gpio_desc *reset_gpio = data->reset_gpio;
1083	struct rtk_dev_coredump_hdr hdr = {
1084		.type = RTK_DEVCOREDUMP_CODE_CMD_TIMEOUT,
1085	};
1086
1087	btusb_rtl_alloc_devcoredump(hdev, &hdr, NULL, 0);
1088
1089	if (!reset_gpio) {
1090		btusb_reset(hdev);
1091		return;
1092	}
1093
1094	/* Toggle the hard reset line. The Realtek device is going to
1095	 * yank itself off the USB and then replug. The cleanup is handled
1096	 * correctly on the way out (standard USB disconnect), and the new
1097	 * device is detected cleanly and bound to the driver again like
1098	 * it should be.
1099	 */
1100	if (test_and_set_bit(BTUSB_HW_RESET_ACTIVE, &data->flags)) {
1101		bt_dev_err(hdev, "last reset failed? Not resetting again");
1102		return;
1103	}
1104
1105	bt_dev_err(hdev, "Reset Realtek device via gpio");
1106	gpiod_set_value_cansleep(reset_gpio, 1);
1107	msleep(200);
1108	gpiod_set_value_cansleep(reset_gpio, 0);
1109}
1110
1111static void btusb_rtl_hw_error(struct hci_dev *hdev, u8 code)
1112{
1113	struct rtk_dev_coredump_hdr hdr = {
1114		.type = RTK_DEVCOREDUMP_CODE_HW_ERR,
1115		.code = code,
1116	};
1117
1118	bt_dev_err(hdev, "RTL: hw err, trigger devcoredump (%d)", code);
1119
1120	btusb_rtl_alloc_devcoredump(hdev, &hdr, NULL, 0);
1121}
1122
1123static void btusb_qca_reset(struct hci_dev *hdev)
1124{
1125	struct btusb_data *data = hci_get_drvdata(hdev);
1126	struct gpio_desc *reset_gpio = data->reset_gpio;
1127
1128	if (test_bit(BTUSB_HW_SSR_ACTIVE, &data->flags)) {
1129		bt_dev_info(hdev, "Ramdump in progress, defer reset");
1130		return;
1131	}
1132
1133	if (reset_gpio) {
1134		bt_dev_err(hdev, "Reset qca device via bt_en gpio");
1135
1136		/* Toggle the hard reset line. The qca bt device is going to
1137		 * yank itself off the USB and then replug. The cleanup is handled
1138		 * correctly on the way out (standard USB disconnect), and the new
1139		 * device is detected cleanly and bound to the driver again like
1140		 * it should be.
1141		 */
1142		if (test_and_set_bit(BTUSB_HW_RESET_ACTIVE, &data->flags)) {
1143			bt_dev_err(hdev, "last reset failed? Not resetting again");
1144			return;
1145		}
1146
1147		gpiod_set_value_cansleep(reset_gpio, 0);
1148		msleep(200);
1149		gpiod_set_value_cansleep(reset_gpio, 1);
1150
1151		return;
1152	}
1153
1154	btusb_reset(hdev);
1155}
1156
1157static u8 btusb_classify_qca_pkt_type(struct hci_dev *hdev, struct sk_buff *skb)
1158{
1159	/* Some Qualcomm controllers, e.g., QCNFA765 with WCN6855 chip, send debug
1160	 * packets as ACL frames with connection handle 0x2EDC. These are not real
1161	 * ACL packets and should be reclassified as HCI_DIAG_PKT to prevent
1162	 * "ACL packet for unknown connection handle 3804" errors.
1163	 */
1164	if (skb->len >= 2) {
1165		u16 handle = get_unaligned_le16(skb->data);
1166
1167		if (handle == 0x2EDC)
1168			return HCI_DIAG_PKT;
1169	}
1170
1171	/* Use default packet type for other packets */
1172	return hci_skb_pkt_type(skb);
1173}
1174
1175static inline void btusb_free_frags(struct btusb_data *data)
1176{
1177	unsigned long flags;
1178
1179	spin_lock_irqsave(&data->rxlock, flags);
1180
1181	dev_kfree_skb_irq(data->evt_skb);
1182	data->evt_skb = NULL;
1183
1184	dev_kfree_skb_irq(data->acl_skb);
1185	data->acl_skb = NULL;
1186
1187	dev_kfree_skb_irq(data->sco_skb);
1188	data->sco_skb = NULL;
1189
1190	spin_unlock_irqrestore(&data->rxlock, flags);
1191}
1192
1193static int btusb_recv_event(struct btusb_data *data, struct sk_buff *skb)
1194{
1195	if (data->intr_interval) {
1196		/* Trigger dequeue immediately if an event is received */
1197		schedule_delayed_work(&data->rx_work, 0);
1198	}
1199
1200	return data->recv_event(data->hdev, skb);
1201}
1202
1203static int btusb_recv_intr(struct btusb_data *data, void *buffer, int count)
1204{
1205	struct sk_buff *skb;
1206	unsigned long flags;
1207	int err = 0;
1208
1209	spin_lock_irqsave(&data->rxlock, flags);
1210	skb = data->evt_skb;
1211
1212	while (count) {
1213		int len;
1214
1215		if (!skb) {
1216			skb = bt_skb_alloc(HCI_MAX_EVENT_SIZE, GFP_ATOMIC);
1217			if (!skb) {
1218				err = -ENOMEM;
1219				break;
1220			}
1221
1222			hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
1223			hci_skb_expect(skb) = HCI_EVENT_HDR_SIZE;
1224		}
1225
1226		len = min_t(uint, hci_skb_expect(skb), count);
1227		skb_put_data(skb, buffer, len);
1228
1229		count -= len;
1230		buffer += len;
1231		hci_skb_expect(skb) -= len;
1232
1233		if (skb->len == HCI_EVENT_HDR_SIZE) {
1234			/* Complete event header */
1235			hci_skb_expect(skb) = hci_event_hdr(skb)->plen;
1236
1237			if (skb_tailroom(skb) < hci_skb_expect(skb)) {
1238				kfree_skb(skb);
1239				skb = NULL;
1240
1241				err = -EILSEQ;
1242				break;
1243			}
1244		}
1245
1246		if (!hci_skb_expect(skb)) {
1247			/* Each chunk should correspond to at least 1 or more
1248			 * events so if there are still bytes left that doesn't
1249			 * constitute a new event this is likely a bug in the
1250			 * controller.
1251			 */
1252			if (count && count < HCI_EVENT_HDR_SIZE) {
1253				bt_dev_warn(data->hdev,
1254					"Unexpected continuation: %d bytes",
1255					count);
1256				count = 0;
1257			}
1258
1259			/* Complete frame */
1260			btusb_recv_event(data, skb);
1261			skb = NULL;
1262		}
1263	}
1264
1265	data->evt_skb = skb;
1266	spin_unlock_irqrestore(&data->rxlock, flags);
1267
1268	return err;
1269}
1270
1271static int btusb_recv_acl(struct btusb_data *data, struct sk_buff *skb)
1272{
1273	/* Only queue ACL packet if intr_interval is set as it means
1274	 * force_poll_sync has been enabled.
1275	 */
1276	if (!data->intr_interval)
1277		return data->recv_acl(data->hdev, skb);
1278
1279	skb_queue_tail(&data->acl_q, skb);
1280	schedule_delayed_work(&data->rx_work, data->intr_interval);
1281
1282	return 0;
1283}
1284
1285static int btusb_recv_bulk(struct btusb_data *data, void *buffer, int count)
1286{
1287	struct sk_buff *skb;
1288	unsigned long flags;
1289	int err = 0;
1290
1291	spin_lock_irqsave(&data->rxlock, flags);
1292	skb = data->acl_skb;
1293
1294	while (count) {
1295		int len;
1296
1297		if (!skb) {
1298			skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
1299			if (!skb) {
1300				err = -ENOMEM;
1301				break;
1302			}
1303
1304			hci_skb_pkt_type(skb) = HCI_ACLDATA_PKT;
1305			hci_skb_expect(skb) = HCI_ACL_HDR_SIZE;
1306		}
1307
1308		len = min_t(uint, hci_skb_expect(skb), count);
1309		skb_put_data(skb, buffer, len);
1310
1311		count -= len;
1312		buffer += len;
1313		hci_skb_expect(skb) -= len;
1314
1315		if (skb->len == HCI_ACL_HDR_SIZE) {
1316			__le16 dlen = hci_acl_hdr(skb)->dlen;
1317
1318			/* Complete ACL header */
1319			hci_skb_expect(skb) = __le16_to_cpu(dlen);
1320
1321			if (skb_tailroom(skb) < hci_skb_expect(skb)) {
1322				kfree_skb(skb);
1323				skb = NULL;
1324
1325				err = -EILSEQ;
1326				break;
1327			}
1328		}
1329
1330		if (!hci_skb_expect(skb)) {
1331			/* Complete frame */
1332			btusb_recv_acl(data, skb);
1333			skb = NULL;
1334		}
1335	}
1336
1337	data->acl_skb = skb;
1338	spin_unlock_irqrestore(&data->rxlock, flags);
1339
1340	return err;
1341}
1342
1343static bool btusb_validate_sco_handle(struct hci_dev *hdev,
1344				      struct hci_sco_hdr *hdr)
1345{
1346	__u16 handle;
1347
1348	if (hci_dev_test_flag(hdev, HCI_USER_CHANNEL))
1349		// Can't validate, userspace controls everything.
1350		return true;
1351
1352	/*
1353	 * USB isochronous transfers are not designed to be reliable and may
1354	 * lose fragments.  When this happens, the next first fragment
1355	 * encountered might actually be a continuation fragment.
1356	 * Validate the handle to detect it and drop it, or else the upper
1357	 * layer will get garbage for a while.
1358	 */
1359
1360	handle = hci_handle(__le16_to_cpu(hdr->handle));
1361
1362	switch (hci_conn_lookup_type(hdev, handle)) {
1363	case SCO_LINK:
1364	case ESCO_LINK:
1365		return true;
1366	default:
1367		return false;
1368	}
1369}
1370
1371static int btusb_recv_isoc(struct btusb_data *data, void *buffer, int count)
1372{
1373	struct sk_buff *skb;
1374	unsigned long flags;
1375	int err = 0;
1376
1377	spin_lock_irqsave(&data->rxlock, flags);
1378	skb = data->sco_skb;
1379
1380	while (count) {
1381		int len;
1382
1383		if (!skb) {
1384			skb = bt_skb_alloc(HCI_MAX_SCO_SIZE, GFP_ATOMIC);
1385			if (!skb) {
1386				err = -ENOMEM;
1387				break;
1388			}
1389
1390			hci_skb_pkt_type(skb) = HCI_SCODATA_PKT;
1391			hci_skb_expect(skb) = HCI_SCO_HDR_SIZE;
1392		}
1393
1394		len = min_t(uint, hci_skb_expect(skb), count);
1395		skb_put_data(skb, buffer, len);
1396
1397		count -= len;
1398		buffer += len;
1399		hci_skb_expect(skb) -= len;
1400
1401		if (skb->len == HCI_SCO_HDR_SIZE) {
1402			/* Complete SCO header */
1403			struct hci_sco_hdr *hdr = hci_sco_hdr(skb);
1404
1405			hci_skb_expect(skb) = hdr->dlen;
1406
1407			if (skb_tailroom(skb) < hci_skb_expect(skb) ||
1408			    !btusb_validate_sco_handle(data->hdev, hdr)) {
1409				kfree_skb(skb);
1410				skb = NULL;
1411
1412				err = -EILSEQ;
1413				break;
1414			}
1415		}
1416
1417		if (!hci_skb_expect(skb)) {
1418			/* Complete frame */
1419			hci_recv_frame(data->hdev, skb);
1420			skb = NULL;
1421		}
1422	}
1423
1424	data->sco_skb = skb;
1425	spin_unlock_irqrestore(&data->rxlock, flags);
1426
1427	return err;
1428}
1429
1430static void btusb_intr_complete(struct urb *urb)
1431{
1432	struct hci_dev *hdev = urb->context;
1433	struct btusb_data *data = hci_get_drvdata(hdev);
1434	int err;
1435
1436	BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1437	       urb->actual_length);
1438
1439	if (!test_bit(HCI_RUNNING, &hdev->flags))
1440		return;
1441
1442	if (urb->status == 0) {
1443		hdev->stat.byte_rx += urb->actual_length;
1444
1445		if (btusb_recv_intr(data, urb->transfer_buffer,
1446				    urb->actual_length) < 0) {
1447			bt_dev_err(hdev, "corrupted event packet");
1448			hdev->stat.err_rx++;
1449		}
1450	} else if (urb->status == -ENOENT) {
1451		/* Avoid suspend failed when usb_kill_urb */
1452		return;
1453	}
1454
1455	if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
1456		return;
1457
1458	usb_mark_last_busy(data->udev);
1459	usb_anchor_urb(urb, &data->intr_anchor);
1460
1461	err = usb_submit_urb(urb, GFP_ATOMIC);
1462	if (err < 0) {
1463		/* -EPERM: urb is being killed;
1464		 * -ENODEV: device got disconnected
1465		 */
1466		if (err != -EPERM && err != -ENODEV)
1467			bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
1468				   urb, -err);
1469		if (err != -EPERM)
1470			hci_cmd_sync_cancel(hdev, -err);
1471		usb_unanchor_urb(urb);
1472	}
1473}
1474
1475static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags)
1476{
1477	struct btusb_data *data = hci_get_drvdata(hdev);
1478	struct urb *urb;
1479	unsigned char *buf;
1480	unsigned int pipe;
1481	int err, size;
1482
1483	BT_DBG("%s", hdev->name);
1484
1485	if (!data->intr_ep)
1486		return -ENODEV;
1487
1488	urb = usb_alloc_urb(0, mem_flags);
1489	if (!urb)
1490		return -ENOMEM;
1491
1492	if (le16_to_cpu(data->udev->descriptor.idVendor)  == 0x0a12 &&
1493	    le16_to_cpu(data->udev->descriptor.idProduct) == 0x0001)
1494		/* Fake CSR devices don't seem to support sort-transter */
1495		size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
1496	else
1497		/* Use maximum HCI Event size so the USB stack handles
1498		 * ZPL/short-transfer automatically.
1499		 */
1500		size = HCI_MAX_EVENT_SIZE;
1501
1502	buf = kmalloc(size, mem_flags);
1503	if (!buf) {
1504		usb_free_urb(urb);
1505		return -ENOMEM;
1506	}
1507
1508	pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
1509
1510	usb_fill_int_urb(urb, data->udev, pipe, buf, size,
1511			 btusb_intr_complete, hdev, data->intr_ep->bInterval);
1512
1513	urb->transfer_flags |= URB_FREE_BUFFER;
1514
1515	usb_anchor_urb(urb, &data->intr_anchor);
1516
1517	err = usb_submit_urb(urb, mem_flags);
1518	if (err < 0) {
1519		if (err != -EPERM && err != -ENODEV)
1520			bt_dev_err(hdev, "urb %p submission failed (%d)",
1521				   urb, -err);
1522		if (err != -EPERM)
1523			hci_cmd_sync_cancel(hdev, -err);
1524		usb_unanchor_urb(urb);
1525	}
1526
1527	/* Only initialize intr_interval if URB poll sync is enabled */
1528	if (!data->poll_sync)
1529		goto done;
1530
1531	/* The units are frames (milliseconds) for full and low speed devices,
1532	 * and microframes (1/8 millisecond) for highspeed and SuperSpeed
1533	 * devices.
1534	 *
1535	 * This is done once on open/resume so it shouldn't change even if
1536	 * force_poll_sync changes.
1537	 */
1538	switch (urb->dev->speed) {
1539	case USB_SPEED_SUPER_PLUS:
1540	case USB_SPEED_SUPER:	/* units are 125us */
1541		data->intr_interval = usecs_to_jiffies(urb->interval * 125);
1542		break;
1543	default:
1544		data->intr_interval = msecs_to_jiffies(urb->interval);
1545		break;
1546	}
1547
1548done:
1549	usb_free_urb(urb);
1550
1551	return err;
1552}
1553
1554static void btusb_bulk_complete(struct urb *urb)
1555{
1556	struct hci_dev *hdev = urb->context;
1557	struct btusb_data *data = hci_get_drvdata(hdev);
1558	int err;
1559
1560	BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1561	       urb->actual_length);
1562
1563	if (!test_bit(HCI_RUNNING, &hdev->flags))
1564		return;
1565
1566	if (urb->status == 0) {
1567		hdev->stat.byte_rx += urb->actual_length;
1568
1569		if (data->recv_bulk(data, urb->transfer_buffer,
1570				    urb->actual_length) < 0) {
1571			bt_dev_err(hdev, "corrupted ACL packet");
1572			hdev->stat.err_rx++;
1573		}
1574	} else if (urb->status == -ENOENT) {
1575		/* Avoid suspend failed when usb_kill_urb */
1576		return;
1577	}
1578
1579	if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
1580		return;
1581
1582	usb_anchor_urb(urb, &data->bulk_anchor);
1583	usb_mark_last_busy(data->udev);
1584
1585	err = usb_submit_urb(urb, GFP_ATOMIC);
1586	if (err < 0) {
1587		/* -EPERM: urb is being killed;
1588		 * -ENODEV: device got disconnected
1589		 */
1590		if (err != -EPERM && err != -ENODEV)
1591			bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
1592				   urb, -err);
1593		usb_unanchor_urb(urb);
1594	}
1595}
1596
1597static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
1598{
1599	struct btusb_data *data = hci_get_drvdata(hdev);
1600	struct urb *urb;
1601	unsigned char *buf;
1602	unsigned int pipe;
1603	int err, size = HCI_MAX_FRAME_SIZE;
1604
1605	BT_DBG("%s", hdev->name);
1606
1607	if (!data->bulk_rx_ep)
1608		return -ENODEV;
1609
1610	urb = usb_alloc_urb(0, mem_flags);
1611	if (!urb)
1612		return -ENOMEM;
1613
1614	buf = kmalloc(size, mem_flags);
1615	if (!buf) {
1616		usb_free_urb(urb);
1617		return -ENOMEM;
1618	}
1619
1620	pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
1621
1622	usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
1623			  btusb_bulk_complete, hdev);
1624
1625	urb->transfer_flags |= URB_FREE_BUFFER;
1626
1627	usb_mark_last_busy(data->udev);
1628	usb_anchor_urb(urb, &data->bulk_anchor);
1629
1630	err = usb_submit_urb(urb, mem_flags);
1631	if (err < 0) {
1632		if (err != -EPERM && err != -ENODEV)
1633			bt_dev_err(hdev, "urb %p submission failed (%d)",
1634				   urb, -err);
1635		usb_unanchor_urb(urb);
1636	}
1637
1638	usb_free_urb(urb);
1639
1640	return err;
1641}
1642
1643static void btusb_isoc_complete(struct urb *urb)
1644{
1645	struct hci_dev *hdev = urb->context;
1646	struct btusb_data *data = hci_get_drvdata(hdev);
1647	int i, err;
1648
1649	BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1650	       urb->actual_length);
1651
1652	if (!test_bit(HCI_RUNNING, &hdev->flags))
1653		return;
1654
1655	if (urb->status == 0) {
1656		for (i = 0; i < urb->number_of_packets; i++) {
1657			unsigned int offset = urb->iso_frame_desc[i].offset;
1658			unsigned int length = urb->iso_frame_desc[i].actual_length;
1659
1660			if (urb->iso_frame_desc[i].status)
1661				continue;
1662
1663			hdev->stat.byte_rx += length;
1664
1665			if (btusb_recv_isoc(data, urb->transfer_buffer + offset,
1666					    length) < 0) {
1667				bt_dev_err(hdev, "corrupted SCO packet");
1668				hdev->stat.err_rx++;
1669			}
1670		}
1671	} else if (urb->status == -ENOENT) {
1672		/* Avoid suspend failed when usb_kill_urb */
1673		return;
1674	}
1675
1676	if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags))
1677		return;
1678
1679	usb_anchor_urb(urb, &data->isoc_anchor);
1680
1681	err = usb_submit_urb(urb, GFP_ATOMIC);
1682	if (err < 0) {
1683		/* -EPERM: urb is being killed;
1684		 * -ENODEV: device got disconnected
1685		 */
1686		if (err != -EPERM && err != -ENODEV)
1687			bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
1688				   urb, -err);
1689		usb_unanchor_urb(urb);
1690	}
1691}
1692
1693static inline void __fill_isoc_descriptor_msbc(struct urb *urb, int len,
1694					       int mtu, struct btusb_data *data)
1695{
1696	int i = 0, offset = 0;
1697	unsigned int interval;
1698
1699	BT_DBG("len %d mtu %d", len, mtu);
1700
1701	/* For mSBC ALT 6 settings some chips need to transmit the data
1702	 * continuously without the zero length of USB packets.
1703	 */
1704	if (test_bit(BTUSB_ALT6_CONTINUOUS_TX, &data->flags))
1705		goto ignore_usb_alt6_packet_flow;
1706
1707	/* For mSBC ALT 6 setting the host will send the packet at continuous
1708	 * flow. As per core spec 5, vol 4, part B, table 2.1. For ALT setting
1709	 * 6 the HCI PACKET INTERVAL should be 7.5ms for every usb packets.
1710	 * To maintain the rate we send 63bytes of usb packets alternatively for
1711	 * 7ms and 8ms to maintain the rate as 7.5ms.
1712	 */
1713	if (data->usb_alt6_packet_flow) {
1714		interval = 7;
1715		data->usb_alt6_packet_flow = false;
1716	} else {
1717		interval = 6;
1718		data->usb_alt6_packet_flow = true;
1719	}
1720
1721	for (i = 0; i < interval; i++) {
1722		urb->iso_frame_desc[i].offset = offset;
1723		urb->iso_frame_desc[i].length = offset;
1724	}
1725
1726ignore_usb_alt6_packet_flow:
1727	if (len && i < BTUSB_MAX_ISOC_FRAMES) {
1728		urb->iso_frame_desc[i].offset = offset;
1729		urb->iso_frame_desc[i].length = len;
1730		i++;
1731	}
1732
1733	urb->number_of_packets = i;
1734}
1735
1736static inline void __fill_isoc_descriptor(struct urb *urb, int len, int mtu)
1737{
1738	int i, offset = 0;
1739
1740	BT_DBG("len %d mtu %d", len, mtu);
1741
1742	for (i = 0; i < BTUSB_MAX_ISOC_FRAMES && len >= mtu;
1743					i++, offset += mtu, len -= mtu) {
1744		urb->iso_frame_desc[i].offset = offset;
1745		urb->iso_frame_desc[i].length = mtu;
1746	}
1747
1748	if (len && i < BTUSB_MAX_ISOC_FRAMES) {
1749		urb->iso_frame_desc[i].offset = offset;
1750		urb->iso_frame_desc[i].length = len;
1751		i++;
1752	}
1753
1754	urb->number_of_packets = i;
1755}
1756
1757static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags)
1758{
1759	struct btusb_data *data = hci_get_drvdata(hdev);
1760	struct urb *urb;
1761	unsigned char *buf;
1762	unsigned int pipe;
1763	int err, size;
1764
1765	BT_DBG("%s", hdev->name);
1766
1767	if (!data->isoc_rx_ep)
1768		return -ENODEV;
1769
1770	urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags);
1771	if (!urb)
1772		return -ENOMEM;
1773
1774	size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) *
1775						BTUSB_MAX_ISOC_FRAMES;
1776
1777	buf = kmalloc(size, mem_flags);
1778	if (!buf) {
1779		usb_free_urb(urb);
1780		return -ENOMEM;
1781	}
1782
1783	pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress);
1784
1785	usb_fill_int_urb(urb, data->udev, pipe, buf, size, btusb_isoc_complete,
1786			 hdev, data->isoc_rx_ep->bInterval);
1787
1788	urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP;
1789
1790	__fill_isoc_descriptor(urb, size,
1791			       le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize));
1792
1793	usb_anchor_urb(urb, &data->isoc_anchor);
1794
1795	err = usb_submit_urb(urb, mem_flags);
1796	if (err < 0) {
1797		if (err != -EPERM && err != -ENODEV)
1798			bt_dev_err(hdev, "urb %p submission failed (%d)",
1799				   urb, -err);
1800		usb_unanchor_urb(urb);
1801	}
1802
1803	usb_free_urb(urb);
1804
1805	return err;
1806}
1807
1808static void btusb_diag_complete(struct urb *urb)
1809{
1810	struct hci_dev *hdev = urb->context;
1811	struct btusb_data *data = hci_get_drvdata(hdev);
1812	int err;
1813
1814	BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1815	       urb->actual_length);
1816
1817	if (urb->status == 0) {
1818		struct sk_buff *skb;
1819
1820		skb = bt_skb_alloc(urb->actual_length, GFP_ATOMIC);
1821		if (skb) {
1822			skb_put_data(skb, urb->transfer_buffer,
1823				     urb->actual_length);
1824			hci_recv_diag(hdev, skb);
1825		}
1826	} else if (urb->status == -ENOENT) {
1827		/* Avoid suspend failed when usb_kill_urb */
1828		return;
1829	}
1830
1831	if (!test_bit(BTUSB_DIAG_RUNNING, &data->flags))
1832		return;
1833
1834	usb_anchor_urb(urb, &data->diag_anchor);
1835	usb_mark_last_busy(data->udev);
1836
1837	err = usb_submit_urb(urb, GFP_ATOMIC);
1838	if (err < 0) {
1839		/* -EPERM: urb is being killed;
1840		 * -ENODEV: device got disconnected
1841		 */
1842		if (err != -EPERM && err != -ENODEV)
1843			bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
1844				   urb, -err);
1845		usb_unanchor_urb(urb);
1846	}
1847}
1848
1849static int btusb_submit_diag_urb(struct hci_dev *hdev, gfp_t mem_flags)
1850{
1851	struct btusb_data *data = hci_get_drvdata(hdev);
1852	struct urb *urb;
1853	unsigned char *buf;
1854	unsigned int pipe;
1855	int err, size = HCI_MAX_FRAME_SIZE;
1856
1857	BT_DBG("%s", hdev->name);
1858
1859	if (!data->diag_rx_ep)
1860		return -ENODEV;
1861
1862	urb = usb_alloc_urb(0, mem_flags);
1863	if (!urb)
1864		return -ENOMEM;
1865
1866	buf = kmalloc(size, mem_flags);
1867	if (!buf) {
1868		usb_free_urb(urb);
1869		return -ENOMEM;
1870	}
1871
1872	pipe = usb_rcvbulkpipe(data->udev, data->diag_rx_ep->bEndpointAddress);
1873
1874	usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
1875			  btusb_diag_complete, hdev);
1876
1877	urb->transfer_flags |= URB_FREE_BUFFER;
1878
1879	usb_mark_last_busy(data->udev);
1880	usb_anchor_urb(urb, &data->diag_anchor);
1881
1882	err = usb_submit_urb(urb, mem_flags);
1883	if (err < 0) {
1884		if (err != -EPERM && err != -ENODEV)
1885			bt_dev_err(hdev, "urb %p submission failed (%d)",
1886				   urb, -err);
1887		usb_unanchor_urb(urb);
1888	}
1889
1890	usb_free_urb(urb);
1891
1892	return err;
1893}
1894
1895static void btusb_tx_complete(struct urb *urb)
1896{
1897	struct sk_buff *skb = urb->context;
1898	struct hci_dev *hdev = (struct hci_dev *)skb->dev;
1899	struct btusb_data *data = hci_get_drvdata(hdev);
1900	unsigned long flags;
1901
1902	BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1903	       urb->actual_length);
1904
1905	if (!test_bit(HCI_RUNNING, &hdev->flags))
1906		goto done;
1907
1908	if (!urb->status) {
1909		hdev->stat.byte_tx += urb->transfer_buffer_length;
1910	} else {
1911		if (hci_skb_pkt_type(skb) == HCI_COMMAND_PKT)
1912			hci_cmd_sync_cancel(hdev, -urb->status);
1913		hdev->stat.err_tx++;
1914	}
1915
1916done:
1917	spin_lock_irqsave(&data->txlock, flags);
1918	data->tx_in_flight--;
1919	spin_unlock_irqrestore(&data->txlock, flags);
1920
1921	kfree(urb->setup_packet);
1922
1923	kfree_skb(skb);
1924}
1925
1926static void btusb_isoc_tx_complete(struct urb *urb)
1927{
1928	struct sk_buff *skb = urb->context;
1929	struct hci_dev *hdev = (struct hci_dev *)skb->dev;
1930
1931	BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1932	       urb->actual_length);
1933
1934	if (!test_bit(HCI_RUNNING, &hdev->flags))
1935		goto done;
1936
1937	if (!urb->status)
1938		hdev->stat.byte_tx += urb->transfer_buffer_length;
1939	else
1940		hdev->stat.err_tx++;
1941
1942done:
1943	kfree(urb->setup_packet);
1944
1945	kfree_skb(skb);
1946}
1947
1948static int btusb_open(struct hci_dev *hdev)
1949{
1950	struct btusb_data *data = hci_get_drvdata(hdev);
1951	int err;
1952
1953	BT_DBG("%s", hdev->name);
1954
1955	err = usb_autopm_get_interface(data->intf);
1956	if (err < 0)
1957		return err;
1958
1959	/* Patching USB firmware files prior to starting any URBs of HCI path
1960	 * It is more safe to use USB bulk channel for downloading USB patch
1961	 */
1962	if (data->setup_on_usb) {
1963		err = data->setup_on_usb(hdev);
1964		if (err < 0)
1965			goto setup_fail;
1966	}
1967
1968	data->intf->needs_remote_wakeup = 1;
1969
1970	if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
1971		goto done;
1972
1973	err = btusb_submit_intr_urb(hdev, GFP_KERNEL);
1974	if (err < 0)
1975		goto failed;
1976
1977	err = btusb_submit_bulk_urb(hdev, GFP_KERNEL);
1978	if (err < 0) {
1979		usb_kill_anchored_urbs(&data->intr_anchor);
1980		goto failed;
1981	}
1982
1983	set_bit(BTUSB_BULK_RUNNING, &data->flags);
1984	btusb_submit_bulk_urb(hdev, GFP_KERNEL);
1985
1986	if (data->diag) {
1987		if (!btusb_submit_diag_urb(hdev, GFP_KERNEL))
1988			set_bit(BTUSB_DIAG_RUNNING, &data->flags);
1989	}
1990
1991done:
1992	usb_autopm_put_interface(data->intf);
1993	return 0;
1994
1995failed:
1996	clear_bit(BTUSB_INTR_RUNNING, &data->flags);
1997setup_fail:
1998	usb_autopm_put_interface(data->intf);
1999	return err;
2000}
2001
2002static void btusb_stop_traffic(struct btusb_data *data)
2003{
2004	usb_kill_anchored_urbs(&data->intr_anchor);
2005	usb_kill_anchored_urbs(&data->bulk_anchor);
2006	usb_kill_anchored_urbs(&data->isoc_anchor);
2007	usb_kill_anchored_urbs(&data->diag_anchor);
2008	usb_kill_anchored_urbs(&data->ctrl_anchor);
2009}
2010
2011static int btusb_close(struct hci_dev *hdev)
2012{
2013	struct btusb_data *data = hci_get_drvdata(hdev);
2014	int err;
2015
2016	BT_DBG("%s", hdev->name);
2017
2018	cancel_delayed_work(&data->rx_work);
2019	cancel_work_sync(&data->work);
2020	cancel_work_sync(&data->waker);
2021
2022	skb_queue_purge(&data->acl_q);
2023
2024	clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
2025	clear_bit(BTUSB_BULK_RUNNING, &data->flags);
2026	clear_bit(BTUSB_INTR_RUNNING, &data->flags);
2027	clear_bit(BTUSB_DIAG_RUNNING, &data->flags);
2028
2029	btusb_stop_traffic(data);
2030	btusb_free_frags(data);
2031
2032	err = usb_autopm_get_interface(data->intf);
2033	if (err < 0)
2034		goto failed;
2035
2036	data->intf->needs_remote_wakeup = 0;
2037
2038	/* Enable remote wake up for auto-suspend */
2039	if (test_bit(BTUSB_WAKEUP_AUTOSUSPEND, &data->flags))
2040		data->intf->needs_remote_wakeup = 1;
2041
2042	usb_autopm_put_interface(data->intf);
2043
2044failed:
2045	usb_scuttle_anchored_urbs(&data->deferred);
2046	return 0;
2047}
2048
2049static int btusb_flush(struct hci_dev *hdev)
2050{
2051	struct btusb_data *data = hci_get_drvdata(hdev);
2052
2053	BT_DBG("%s", hdev->name);
2054
2055	cancel_delayed_work(&data->rx_work);
2056
2057	skb_queue_purge(&data->acl_q);
2058
2059	usb_kill_anchored_urbs(&data->tx_anchor);
2060	btusb_free_frags(data);
2061
2062	return 0;
2063}
2064
2065static struct urb *alloc_ctrl_urb(struct hci_dev *hdev, struct sk_buff *skb)
2066{
2067	struct btusb_data *data = hci_get_drvdata(hdev);
2068	struct usb_ctrlrequest *dr;
2069	struct urb *urb;
2070	unsigned int pipe;
2071
2072	urb = usb_alloc_urb(0, GFP_KERNEL);
2073	if (!urb)
2074		return ERR_PTR(-ENOMEM);
2075
2076	dr = kmalloc(sizeof(*dr), GFP_KERNEL);
2077	if (!dr) {
2078		usb_free_urb(urb);
2079		return ERR_PTR(-ENOMEM);
2080	}
2081
2082	dr->bRequestType = data->cmdreq_type;
2083	dr->bRequest     = data->cmdreq;
2084	dr->wIndex       = 0;
2085	dr->wValue       = 0;
2086	dr->wLength      = __cpu_to_le16(skb->len);
2087
2088	pipe = usb_sndctrlpipe(data->udev, 0x00);
2089
2090	usb_fill_control_urb(urb, data->udev, pipe, (void *)dr,
2091			     skb->data, skb->len, btusb_tx_complete, skb);
2092
2093	skb->dev = (void *)hdev;
2094
2095	return urb;
2096}
2097
2098static struct urb *alloc_bulk_urb(struct hci_dev *hdev, struct sk_buff *skb)
2099{
2100	struct btusb_data *data = hci_get_drvdata(hdev);
2101	struct urb *urb;
2102	unsigned int pipe;
2103
2104	if (!data->bulk_tx_ep)
2105		return ERR_PTR(-ENODEV);
2106
2107	urb = usb_alloc_urb(0, GFP_KERNEL);
2108	if (!urb)
2109		return ERR_PTR(-ENOMEM);
2110
2111	pipe = usb_sndbulkpipe(data->udev, data->bulk_tx_ep->bEndpointAddress);
2112
2113	usb_fill_bulk_urb(urb, data->udev, pipe,
2114			  skb->data, skb->len, btusb_tx_complete, skb);
2115
2116	skb->dev = (void *)hdev;
2117
2118	return urb;
2119}
2120
2121static struct urb *alloc_isoc_urb(struct hci_dev *hdev, struct sk_buff *skb)
2122{
2123	struct btusb_data *data = hci_get_drvdata(hdev);
2124	struct urb *urb;
2125	unsigned int pipe;
2126
2127	if (!data->isoc_tx_ep)
2128		return ERR_PTR(-ENODEV);
2129
2130	urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_KERNEL);
2131	if (!urb)
2132		return ERR_PTR(-ENOMEM);
2133
2134	pipe = usb_sndisocpipe(data->udev, data->isoc_tx_ep->bEndpointAddress);
2135
2136	usb_fill_int_urb(urb, data->udev, pipe,
2137			 skb->data, skb->len, btusb_isoc_tx_complete,
2138			 skb, data->isoc_tx_ep->bInterval);
2139
2140	urb->transfer_flags  = URB_ISO_ASAP;
2141
2142	if (data->isoc_altsetting == 6)
2143		__fill_isoc_descriptor_msbc(urb, skb->len,
2144					    le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize),
2145					    data);
2146	else
2147		__fill_isoc_descriptor(urb, skb->len,
2148				       le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
2149	skb->dev = (void *)hdev;
2150
2151	return urb;
2152}
2153
2154static int submit_tx_urb(struct hci_dev *hdev, struct urb *urb)
2155{
2156	struct btusb_data *data = hci_get_drvdata(hdev);
2157	int err;
2158
2159	usb_anchor_urb(urb, &data->tx_anchor);
2160
2161	err = usb_submit_urb(urb, GFP_KERNEL);
2162	if (err < 0) {
2163		if (err != -EPERM && err != -ENODEV)
2164			bt_dev_err(hdev, "urb %p submission failed (%d)",
2165				   urb, -err);
2166		kfree(urb->setup_packet);
2167		usb_unanchor_urb(urb);
2168	} else {
2169		usb_mark_last_busy(data->udev);
2170	}
2171
2172	usb_free_urb(urb);
2173	return err;
2174}
2175
2176static int submit_or_queue_tx_urb(struct hci_dev *hdev, struct urb *urb)
2177{
2178	struct btusb_data *data = hci_get_drvdata(hdev);
2179	unsigned long flags;
2180	bool suspending;
2181
2182	spin_lock_irqsave(&data->txlock, flags);
2183	suspending = test_bit(BTUSB_SUSPENDING, &data->flags);
2184	if (!suspending)
2185		data->tx_in_flight++;
2186	spin_unlock_irqrestore(&data->txlock, flags);
2187
2188	if (!suspending)
2189		return submit_tx_urb(hdev, urb);
2190
2191	usb_anchor_urb(urb, &data->deferred);
2192	schedule_work(&data->waker);
2193
2194	usb_free_urb(urb);
2195	return 0;
2196}
2197
2198static int btusb_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
2199{
2200	struct urb *urb;
2201
2202	BT_DBG("%s", hdev->name);
2203
2204	switch (hci_skb_pkt_type(skb)) {
2205	case HCI_COMMAND_PKT:
2206		urb = alloc_ctrl_urb(hdev, skb);
2207		if (IS_ERR(urb))
2208			return PTR_ERR(urb);
2209
2210		hdev->stat.cmd_tx++;
2211		return submit_or_queue_tx_urb(hdev, urb);
2212
2213	case HCI_ACLDATA_PKT:
2214		urb = alloc_bulk_urb(hdev, skb);
2215		if (IS_ERR(urb))
2216			return PTR_ERR(urb);
2217
2218		hdev->stat.acl_tx++;
2219		return submit_or_queue_tx_urb(hdev, urb);
2220
2221	case HCI_SCODATA_PKT:
2222		if (!hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
2223		    hci_conn_num(hdev, SCO_LINK) < 1)
2224			return -ENODEV;
2225
2226		urb = alloc_isoc_urb(hdev, skb);
2227		if (IS_ERR(urb))
2228			return PTR_ERR(urb);
2229
2230		hdev->stat.sco_tx++;
2231		return submit_tx_urb(hdev, urb);
2232
2233	case HCI_ISODATA_PKT:
2234		urb = alloc_bulk_urb(hdev, skb);
2235		if (IS_ERR(urb))
2236			return PTR_ERR(urb);
2237
2238		return submit_or_queue_tx_urb(hdev, urb);
2239	}
2240
2241	return -EILSEQ;
2242}
2243
2244static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
2245{
2246	struct btusb_data *data = hci_get_drvdata(hdev);
2247
2248	BT_DBG("%s evt %d", hdev->name, evt);
2249
2250	if (hci_conn_num(hdev, SCO_LINK) != data->sco_num) {
2251		data->sco_num = hci_conn_num(hdev, SCO_LINK);
2252		data->air_mode = evt;
2253		schedule_work(&data->work);
2254	}
2255}
2256
2257static inline int __set_isoc_interface(struct hci_dev *hdev, int altsetting)
2258{
2259	struct btusb_data *data = hci_get_drvdata(hdev);
2260	struct usb_interface *intf = data->isoc;
2261	struct usb_endpoint_descriptor *ep_desc;
2262	int i, err;
2263
2264	if (!data->isoc)
2265		return -ENODEV;
2266
2267	err = usb_set_interface(data->udev, data->isoc_ifnum, altsetting);
2268	if (err < 0) {
2269		bt_dev_err(hdev, "setting interface failed (%d)", -err);
2270		return err;
2271	}
2272
2273	data->isoc_altsetting = altsetting;
2274
2275	data->isoc_tx_ep = NULL;
2276	data->isoc_rx_ep = NULL;
2277
2278	for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
2279		ep_desc = &intf->cur_altsetting->endpoint[i].desc;
2280
2281		if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) {
2282			data->isoc_tx_ep = ep_desc;
2283			continue;
2284		}
2285
2286		if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) {
2287			data->isoc_rx_ep = ep_desc;
2288			continue;
2289		}
2290	}
2291
2292	if (!data->isoc_tx_ep || !data->isoc_rx_ep) {
2293		bt_dev_err(hdev, "invalid SCO descriptors");
2294		return -ENODEV;
2295	}
2296
2297	return 0;
2298}
2299
2300static int btusb_switch_alt_setting(struct hci_dev *hdev, int new_alts)
2301{
2302	struct btusb_data *data = hci_get_drvdata(hdev);
2303	int err;
2304
2305	if (data->isoc_altsetting != new_alts) {
2306		unsigned long flags;
2307
2308		clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
2309		usb_kill_anchored_urbs(&data->isoc_anchor);
2310
2311		/* When isochronous alternate setting needs to be
2312		 * changed, because SCO connection has been added
2313		 * or removed, a packet fragment may be left in the
2314		 * reassembling state. This could lead to wrongly
2315		 * assembled fragments.
2316		 *
2317		 * Clear outstanding fragment when selecting a new
2318		 * alternate setting.
2319		 */
2320		spin_lock_irqsave(&data->rxlock, flags);
2321		dev_kfree_skb_irq(data->sco_skb);
2322		data->sco_skb = NULL;
2323		spin_unlock_irqrestore(&data->rxlock, flags);
2324
2325		err = __set_isoc_interface(hdev, new_alts);
2326		if (err < 0)
2327			return err;
2328	}
2329
2330	if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
2331		if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0)
2332			clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
2333		else
2334			btusb_submit_isoc_urb(hdev, GFP_KERNEL);
2335	}
2336
2337	return 0;
2338}
2339
2340static struct usb_host_interface *btusb_find_altsetting(struct btusb_data *data,
2341							int alt)
2342{
2343	struct usb_interface *intf = data->isoc;
2344	int i;
2345
2346	BT_DBG("Looking for Alt no :%d", alt);
2347
2348	if (!intf)
2349		return NULL;
2350
2351	for (i = 0; i < intf->num_altsetting; i++) {
2352		if (intf->altsetting[i].desc.bAlternateSetting == alt)
2353			return &intf->altsetting[i];
2354	}
2355
2356	return NULL;
2357}
2358
2359static void btusb_work(struct work_struct *work)
2360{
2361	struct btusb_data *data = container_of(work, struct btusb_data, work);
2362	struct hci_dev *hdev = data->hdev;
2363	int new_alts = 0;
2364	int err;
2365
2366	if (data->sco_num > 0) {
2367		if (!test_bit(BTUSB_DID_ISO_RESUME, &data->flags)) {
2368			err = usb_autopm_get_interface(data->isoc ? data->isoc : data->intf);
2369			if (err < 0) {
2370				clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
2371				usb_kill_anchored_urbs(&data->isoc_anchor);
2372				return;
2373			}
2374
2375			set_bit(BTUSB_DID_ISO_RESUME, &data->flags);
2376		}
2377
2378		if (data->air_mode == HCI_NOTIFY_ENABLE_SCO_CVSD) {
2379			if (hdev->voice_setting & 0x0020) {
2380				static const int alts[3] = { 2, 4, 5 };
2381				unsigned int sco_idx;
2382
2383				sco_idx = min_t(unsigned int, data->sco_num - 1,
2384						ARRAY_SIZE(alts) - 1);
2385				new_alts = alts[sco_idx];
2386			} else {
2387				new_alts = data->sco_num;
2388			}
2389		} else if (data->air_mode == HCI_NOTIFY_ENABLE_SCO_TRANSP) {
2390			/* Bluetooth USB spec recommends alt 6 (63 bytes), but
2391			 * many adapters do not support it.  Alt 1 appears to
2392			 * work for all adapters that do not have alt 6, and
2393			 * which work with WBS at all.  Some devices prefer
2394			 * alt 3 (HCI payload >= 60 Bytes let air packet
2395			 * data satisfy 60 bytes), requiring
2396			 * MTU >= 3 (packets) * 25 (size) - 3 (headers) = 72
2397			 * see also Core spec 5, vol 4, B 2.1.1 & Table 2.1.
2398			 */
2399			if (btusb_find_altsetting(data, 6))
2400				new_alts = 6;
2401			else if (btusb_find_altsetting(data, 3) &&
2402				 hdev->sco_mtu >= 72 &&
2403				 test_bit(BTUSB_USE_ALT3_FOR_WBS, &data->flags))
2404				new_alts = 3;
2405			else
2406				new_alts = 1;
2407		}
2408
2409		if (btusb_switch_alt_setting(hdev, new_alts) < 0)
2410			bt_dev_err(hdev, "set USB alt:(%d) failed!", new_alts);
2411	} else {
2412		usb_kill_anchored_urbs(&data->isoc_anchor);
2413
2414		if (test_and_clear_bit(BTUSB_ISOC_RUNNING, &data->flags))
2415			__set_isoc_interface(hdev, 0);
2416
2417		if (test_and_clear_bit(BTUSB_DID_ISO_RESUME, &data->flags))
2418			usb_autopm_put_interface(data->isoc ? data->isoc : data->intf);
2419	}
2420}
2421
2422static void btusb_waker(struct work_struct *work)
2423{
2424	struct btusb_data *data = container_of(work, struct btusb_data, waker);
2425	int err;
2426
2427	err = usb_autopm_get_interface(data->intf);
2428	if (err < 0)
2429		return;
2430
2431	usb_autopm_put_interface(data->intf);
2432}
2433
2434static void btusb_rx_work(struct work_struct *work)
2435{
2436	struct btusb_data *data = container_of(work, struct btusb_data,
2437					       rx_work.work);
2438	struct sk_buff *skb;
2439
2440	/* Dequeue ACL data received during the interval */
2441	while ((skb = skb_dequeue(&data->acl_q)))
2442		data->recv_acl(data->hdev, skb);
2443}
2444
2445static int btusb_setup_bcm92035(struct hci_dev *hdev)
2446{
2447	struct sk_buff *skb;
2448	u8 val = 0x00;
2449
2450	BT_DBG("%s", hdev->name);
2451
2452	skb = __hci_cmd_sync(hdev, 0xfc3b, 1, &val, HCI_INIT_TIMEOUT);
2453	if (IS_ERR(skb))
2454		bt_dev_err(hdev, "BCM92035 command failed (%ld)", PTR_ERR(skb));
2455	else
2456		kfree_skb(skb);
2457
2458	return 0;
2459}
2460
2461static int btusb_setup_csr(struct hci_dev *hdev)
2462{
2463	struct btusb_data *data = hci_get_drvdata(hdev);
2464	u16 bcdDevice = le16_to_cpu(data->udev->descriptor.bcdDevice);
2465	struct hci_rp_read_local_version *rp;
2466	struct sk_buff *skb;
2467	bool is_fake = false;
2468	int ret;
2469
2470	BT_DBG("%s", hdev->name);
2471
2472	skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
2473			     HCI_INIT_TIMEOUT);
2474	if (IS_ERR(skb)) {
2475		int err = PTR_ERR(skb);
2476		bt_dev_err(hdev, "CSR: Local version failed (%d)", err);
2477		return err;
2478	}
2479
2480	rp = skb_pull_data(skb, sizeof(*rp));
2481	if (!rp) {
2482		bt_dev_err(hdev, "CSR: Local version length mismatch");
2483		kfree_skb(skb);
2484		return -EIO;
2485	}
2486
2487	bt_dev_info(hdev, "CSR: Setting up dongle with HCI ver=%u rev=%04x",
2488		    rp->hci_ver, le16_to_cpu(rp->hci_rev));
2489
2490	bt_dev_info(hdev, "LMP ver=%u subver=%04x; manufacturer=%u",
2491		    rp->lmp_ver, le16_to_cpu(rp->lmp_subver),
2492		    le16_to_cpu(rp->manufacturer));
2493
2494	/* Detect a wide host of Chinese controllers that aren't CSR.
2495	 *
2496	 * Known fake bcdDevices: 0x0100, 0x0134, 0x1915, 0x2520, 0x7558, 0x8891
2497	 *
2498	 * The main thing they have in common is that these are really popular low-cost
2499	 * options that support newer Bluetooth versions but rely on heavy VID/PID
2500	 * squatting of this poor old Bluetooth 1.1 device. Even sold as such.
2501	 *
2502	 * We detect actual CSR devices by checking that the HCI manufacturer code
2503	 * is Cambridge Silicon Radio (10) and ensuring that LMP sub-version and
2504	 * HCI rev values always match. As they both store the firmware number.
2505	 */
2506	if (le16_to_cpu(rp->manufacturer) != 10 ||
2507	    le16_to_cpu(rp->hci_rev) != le16_to_cpu(rp->lmp_subver))
2508		is_fake = true;
2509
2510	/* Known legit CSR firmware build numbers and their supported BT versions:
2511	 * - 1.1 (0x1) -> 0x0073, 0x020d, 0x033c, 0x034e
2512	 * - 1.2 (0x2) ->                 0x04d9, 0x0529
2513	 * - 2.0 (0x3) ->         0x07a6, 0x07ad, 0x0c5c
2514	 * - 2.1 (0x4) ->         0x149c, 0x1735, 0x1899 (0x1899 is a BlueCore4-External)
2515	 * - 4.0 (0x6) ->         0x1d86, 0x2031, 0x22bb
2516	 *
2517	 * e.g. Real CSR dongles with LMP subversion 0x73 are old enough that
2518	 *      support BT 1.1 only; so it's a dead giveaway when some
2519	 *      third-party BT 4.0 dongle reuses it.
2520	 */
2521	else if (le16_to_cpu(rp->lmp_subver) <= 0x034e &&
2522		 rp->hci_ver > BLUETOOTH_VER_1_1)
2523		is_fake = true;
2524
2525	else if (le16_to_cpu(rp->lmp_subver) <= 0x0529 &&
2526		 rp->hci_ver > BLUETOOTH_VER_1_2)
2527		is_fake = true;
2528
2529	else if (le16_to_cpu(rp->lmp_subver) <= 0x0c5c &&
2530		 rp->hci_ver > BLUETOOTH_VER_2_0)
2531		is_fake = true;
2532
2533	else if (le16_to_cpu(rp->lmp_subver) <= 0x1899 &&
2534		 rp->hci_ver > BLUETOOTH_VER_2_1)
2535		is_fake = true;
2536
2537	else if (le16_to_cpu(rp->lmp_subver) <= 0x22bb &&
2538		 rp->hci_ver > BLUETOOTH_VER_4_0)
2539		is_fake = true;
2540
2541	/* Other clones which beat all the above checks */
2542	else if (bcdDevice == 0x0134 &&
2543		 le16_to_cpu(rp->lmp_subver) == 0x0c5c &&
2544		 rp->hci_ver == BLUETOOTH_VER_2_0)
2545		is_fake = true;
2546
2547	if (is_fake) {
2548		bt_dev_warn(hdev, "CSR: Unbranded CSR clone detected; adding workarounds and force-suspending once...");
2549
2550		/* Generally these clones have big discrepancies between
2551		 * advertised features and what's actually supported.
2552		 * Probably will need to be expanded in the future;
2553		 * without these the controller will lock up.
2554		 */
2555		hci_set_quirk(hdev, HCI_QUIRK_BROKEN_STORED_LINK_KEY);
2556		hci_set_quirk(hdev, HCI_QUIRK_BROKEN_ERR_DATA_REPORTING);
2557		hci_set_quirk(hdev, HCI_QUIRK_BROKEN_FILTER_CLEAR_ALL);
2558		hci_set_quirk(hdev, HCI_QUIRK_NO_SUSPEND_NOTIFIER);
2559		hci_set_quirk(hdev, HCI_QUIRK_BROKEN_READ_VOICE_SETTING);
2560		hci_set_quirk(hdev, HCI_QUIRK_BROKEN_READ_PAGE_SCAN_TYPE);
2561
2562		/* Clear the reset quirk since this is not an actual
2563		 * early Bluetooth 1.1 device from CSR.
2564		 */
2565		hci_clear_quirk(hdev, HCI_QUIRK_RESET_ON_CLOSE);
2566		hci_clear_quirk(hdev, HCI_QUIRK_SIMULTANEOUS_DISCOVERY);
2567
2568		/*
2569		 * Special workaround for these BT 4.0 chip clones, and potentially more:
2570		 *
2571		 * - 0x0134: a Barrot 8041a02                 (HCI rev: 0x0810 sub: 0x1012)
2572		 * - 0x7558: IC markings FR3191AHAL 749H15143 (HCI rev/sub-version: 0x0709)
2573		 *
2574		 * These controllers are really messed-up.
2575		 *
2576		 * 1. Their bulk RX endpoint will never report any data unless
2577		 *    the device was suspended at least once (yes, really).
2578		 * 2. They will not wakeup when autosuspended and receiving data
2579		 *    on their bulk RX endpoint from e.g. a keyboard or mouse
2580		 *    (IOW remote-wakeup support is broken for the bulk endpoint).
2581		 *
2582		 * To fix 1. enable runtime-suspend, force-suspend the
2583		 * HCI and then wake-it up by disabling runtime-suspend.
2584		 *
2585		 * To fix 2. clear the HCI's can_wake flag, this way the HCI
2586		 * will still be autosuspended when it is not open.
2587		 *
2588		 * --
2589		 *
2590		 * Because these are widespread problems we prefer generic solutions; so
2591		 * apply this initialization quirk to every controller that gets here,
2592		 * it should be harmless. The alternative is to not work at all.
2593		 */
2594		pm_runtime_allow(&data->udev->dev);
2595
2596		ret = pm_runtime_suspend(&data->udev->dev);
2597		if (ret >= 0)
2598			msleep(200);
2599		else
2600			bt_dev_warn(hdev, "CSR: Couldn't suspend the device for our Barrot 8041a02 receive-issue workaround");
2601
2602		pm_runtime_forbid(&data->udev->dev);
2603
2604		device_set_wakeup_capable(&data->udev->dev, false);
2605
2606		/* Re-enable autosuspend if this was requested */
2607		if (enable_autosuspend)
2608			usb_enable_autosuspend(data->udev);
2609	}
2610
2611	kfree_skb(skb);
2612
2613	return 0;
2614}
2615
2616static int inject_cmd_complete(struct hci_dev *hdev, __u16 opcode)
2617{
2618	struct sk_buff *skb;
2619	struct hci_event_hdr *hdr;
2620	struct hci_ev_cmd_complete *evt;
2621
2622	skb = bt_skb_alloc(sizeof(*hdr) + sizeof(*evt) + 1, GFP_KERNEL);
2623	if (!skb)
2624		return -ENOMEM;
2625
2626	hdr = skb_put(skb, sizeof(*hdr));
2627	hdr->evt = HCI_EV_CMD_COMPLETE;
2628	hdr->plen = sizeof(*evt) + 1;
2629
2630	evt = skb_put(skb, sizeof(*evt));
2631	evt->ncmd = 0x01;
2632	evt->opcode = cpu_to_le16(opcode);
2633
2634	skb_put_u8(skb, 0x00);
2635
2636	hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
2637
2638	return hci_recv_frame(hdev, skb);
2639}
2640
2641static int btusb_recv_bulk_intel(struct btusb_data *data, void *buffer,
2642				 int count)
2643{
2644	struct hci_dev *hdev = data->hdev;
2645
2646	/* When the device is in bootloader mode, then it can send
2647	 * events via the bulk endpoint. These events are treated the
2648	 * same way as the ones received from the interrupt endpoint.
2649	 */
2650	if (btintel_test_flag(hdev, INTEL_BOOTLOADER))
2651		return btusb_recv_intr(data, buffer, count);
2652
2653	return btusb_recv_bulk(data, buffer, count);
2654}
2655
2656static int btusb_send_frame_intel(struct hci_dev *hdev, struct sk_buff *skb)
2657{
2658	struct urb *urb;
2659
2660	BT_DBG("%s", hdev->name);
2661
2662	switch (hci_skb_pkt_type(skb)) {
2663	case HCI_COMMAND_PKT:
2664		if (btintel_test_flag(hdev, INTEL_BOOTLOADER)) {
2665			struct hci_command_hdr *cmd = (void *)skb->data;
2666			__u16 opcode = le16_to_cpu(cmd->opcode);
2667
2668			/* When in bootloader mode and the command 0xfc09
2669			 * is received, it needs to be send down the
2670			 * bulk endpoint. So allocate a bulk URB instead.
2671			 */
2672			if (opcode == 0xfc09)
2673				urb = alloc_bulk_urb(hdev, skb);
2674			else
2675				urb = alloc_ctrl_urb(hdev, skb);
2676
2677			/* When the BTINTEL_HCI_OP_RESET command is issued to
2678			 * boot into the operational firmware, it will actually
2679			 * not send a command complete event. To keep the flow
2680			 * control working inject that event here.
2681			 */
2682			if (opcode == BTINTEL_HCI_OP_RESET)
2683				inject_cmd_complete(hdev, opcode);
2684		} else {
2685			urb = alloc_ctrl_urb(hdev, skb);
2686		}
2687		if (IS_ERR(urb))
2688			return PTR_ERR(urb);
2689
2690		hdev->stat.cmd_tx++;
2691		return submit_or_queue_tx_urb(hdev, urb);
2692
2693	case HCI_ACLDATA_PKT:
2694		urb = alloc_bulk_urb(hdev, skb);
2695		if (IS_ERR(urb))
2696			return PTR_ERR(urb);
2697
2698		hdev->stat.acl_tx++;
2699		return submit_or_queue_tx_urb(hdev, urb);
2700
2701	case HCI_SCODATA_PKT:
2702		if (!hci_dev_test_flag(hdev, HCI_USER_CHANNEL) &&
2703		    hci_conn_num(hdev, SCO_LINK) < 1)
2704			return -ENODEV;
2705
2706		urb = alloc_isoc_urb(hdev, skb);
2707		if (IS_ERR(urb))
2708			return PTR_ERR(urb);
2709
2710		hdev->stat.sco_tx++;
2711		return submit_tx_urb(hdev, urb);
2712
2713	case HCI_ISODATA_PKT:
2714		urb = alloc_bulk_urb(hdev, skb);
2715		if (IS_ERR(urb))
2716			return PTR_ERR(urb);
2717
2718		return submit_or_queue_tx_urb(hdev, urb);
2719	}
2720
2721	return -EILSEQ;
2722}
2723
2724static int btusb_setup_realtek(struct hci_dev *hdev)
2725{
2726	struct btusb_data *data = hci_get_drvdata(hdev);
2727	int ret;
2728
2729	ret = btrtl_setup_realtek(hdev);
2730
2731	if (btrealtek_test_flag(data->hdev, REALTEK_ALT6_CONTINUOUS_TX_CHIP))
2732		set_bit(BTUSB_ALT6_CONTINUOUS_TX, &data->flags);
2733
2734	return ret;
2735}
2736
2737static int btusb_recv_event_realtek(struct hci_dev *hdev, struct sk_buff *skb)
2738{
2739	if (skb->data[0] == HCI_VENDOR_PKT && skb->data[2] == RTK_SUB_EVENT_CODE_COREDUMP) {
2740		struct rtk_dev_coredump_hdr hdr = {
2741			.code = RTK_DEVCOREDUMP_CODE_MEMDUMP,
2742		};
2743
2744		bt_dev_dbg(hdev, "RTL: received coredump vendor evt, len %u",
2745			skb->len);
2746
2747		btusb_rtl_alloc_devcoredump(hdev, &hdr, skb->data, skb->len);
2748		kfree_skb(skb);
2749
2750		return 0;
2751	}
2752
2753	return hci_recv_frame(hdev, skb);
2754}
2755
2756static void btusb_mtk_claim_iso_intf(struct btusb_data *data)
2757{
2758	struct btmtk_data *btmtk_data;
2759	int err;
2760
2761	if (!data->hdev)
2762		return;
2763
2764	btmtk_data = hci_get_priv(data->hdev);
2765	if (!btmtk_data)
2766		return;
2767
2768	if (!btmtk_data->isopkt_intf) {
2769		bt_dev_err(data->hdev, "Can't claim NULL iso interface");
2770		return;
2771	}
2772
2773	/*
2774	 * The function usb_driver_claim_interface() is documented to need
2775	 * locks held if it's not called from a probe routine. The code here
2776	 * is called from the hci_power_on workqueue, so grab the lock.
2777	 */
2778	device_lock(&btmtk_data->isopkt_intf->dev);
2779	err = usb_driver_claim_interface(&btusb_driver,
2780					 btmtk_data->isopkt_intf, data);
2781	device_unlock(&btmtk_data->isopkt_intf->dev);
2782	if (err < 0) {
2783		btmtk_data->isopkt_intf = NULL;
2784		bt_dev_err(data->hdev, "Failed to claim iso interface: %d", err);
2785		return;
2786	}
2787
2788	set_bit(BTMTK_ISOPKT_OVER_INTR, &btmtk_data->flags);
2789	init_usb_anchor(&btmtk_data->isopkt_anchor);
2790}
2791
2792static void btusb_mtk_release_iso_intf(struct hci_dev *hdev)
2793{
2794	struct btmtk_data *btmtk_data;
2795
2796	if (!hdev)
2797		return;
2798
2799	btmtk_data = hci_get_priv(hdev);
2800	if (!btmtk_data)
2801		return;
2802
2803	if (test_bit(BTMTK_ISOPKT_OVER_INTR, &btmtk_data->flags)) {
2804		usb_kill_anchored_urbs(&btmtk_data->isopkt_anchor);
2805		clear_bit(BTMTK_ISOPKT_RUNNING, &btmtk_data->flags);
2806
2807		if (btmtk_data->isopkt_skb) {
2808			dev_kfree_skb_irq(btmtk_data->isopkt_skb);
2809			btmtk_data->isopkt_skb = NULL;
2810		}
2811
2812		if (btmtk_data->isopkt_intf) {
2813			usb_set_intfdata(btmtk_data->isopkt_intf, NULL);
2814			usb_driver_release_interface(&btusb_driver,
2815						     btmtk_data->isopkt_intf);
2816			btmtk_data->isopkt_intf = NULL;
2817		}
2818	}
2819
2820	clear_bit(BTMTK_ISOPKT_OVER_INTR, &btmtk_data->flags);
2821}
2822
2823static int btusb_mtk_disconnect(struct hci_dev *hdev)
2824{
2825	/* This function describes the specific additional steps taken by MediaTek
2826	 * when Bluetooth usb driver's resume function is called.
2827	 */
2828	btusb_mtk_release_iso_intf(hdev);
2829
2830	return 0;
2831}
2832
2833static int btusb_mtk_reset(struct hci_dev *hdev, void *rst_data)
2834{
2835	struct btusb_data *data = hci_get_drvdata(hdev);
2836	struct btmtk_data *btmtk_data = hci_get_priv(hdev);
2837	int err;
2838
2839	/* It's MediaTek specific bluetooth reset mechanism via USB */
2840	if (test_and_set_bit(BTMTK_HW_RESET_ACTIVE, &btmtk_data->flags)) {
2841		bt_dev_err(hdev, "last reset failed? Not resetting again");
2842		return -EBUSY;
2843	}
2844
2845	err = usb_autopm_get_interface(data->intf);
2846	if (err < 0)
2847		return err;
2848
2849	/* Release MediaTek ISO data interface */
2850	btusb_mtk_release_iso_intf(hdev);
2851
2852	btusb_stop_traffic(data);
2853	usb_kill_anchored_urbs(&data->tx_anchor);
2854
2855	/* Toggle the hard reset line. The MediaTek device is going to
2856	 * yank itself off the USB and then replug. The cleanup is handled
2857	 * correctly on the way out (standard USB disconnect), and the new
2858	 * device is detected cleanly and bound to the driver again like
2859	 * it should be.
2860	 */
2861	if (data->reset_gpio) {
2862		gpiod_set_value_cansleep(data->reset_gpio, 1);
2863		msleep(200);
2864		gpiod_set_value_cansleep(data->reset_gpio, 0);
2865		return 0;
2866	}
2867
2868	err = btmtk_usb_subsys_reset(hdev, btmtk_data->dev_id);
2869
2870	usb_queue_reset_device(data->intf);
2871	clear_bit(BTMTK_HW_RESET_ACTIVE, &btmtk_data->flags);
2872
2873	return err;
2874}
2875
2876static int btusb_send_frame_mtk(struct hci_dev *hdev, struct sk_buff *skb)
2877{
2878	struct urb *urb;
2879
2880	BT_DBG("%s", hdev->name);
2881
2882	if (hci_skb_pkt_type(skb) == HCI_ISODATA_PKT) {
2883		urb = alloc_mtk_intr_urb(hdev, skb, btusb_tx_complete);
2884		if (IS_ERR(urb))
2885			return PTR_ERR(urb);
2886
2887		return submit_or_queue_tx_urb(hdev, urb);
2888	} else {
2889		return btusb_send_frame(hdev, skb);
2890	}
2891}
2892
2893static int btusb_mtk_setup(struct hci_dev *hdev)
2894{
2895	struct btusb_data *data = hci_get_drvdata(hdev);
2896	struct btmtk_data *btmtk_data = hci_get_priv(hdev);
2897
2898	/* MediaTek WMT vendor cmd requiring below USB resources to
2899	 * complete the handshake.
2900	 */
2901	btmtk_data->drv_name = btusb_driver.name;
2902	btmtk_data->intf = data->intf;
2903	btmtk_data->udev = data->udev;
2904	btmtk_data->ctrl_anchor = &data->ctrl_anchor;
2905	btmtk_data->reset_sync = btusb_mtk_reset;
2906
2907	/* Claim ISO data interface and endpoint */
2908	if (!test_bit(BTMTK_ISOPKT_OVER_INTR, &btmtk_data->flags)) {
2909		btmtk_data->isopkt_intf = usb_ifnum_to_if(data->udev, MTK_ISO_IFNUM);
2910		btusb_mtk_claim_iso_intf(data);
2911	}
2912
2913	return btmtk_usb_setup(hdev);
2914}
2915
2916static int btusb_mtk_shutdown(struct hci_dev *hdev)
2917{
2918	int ret;
2919
2920	ret = btmtk_usb_shutdown(hdev);
2921
2922	/* Release MediaTek iso interface after shutdown */
2923	btusb_mtk_release_iso_intf(hdev);
2924
2925	return ret;
2926}
2927
2928#ifdef CONFIG_PM
2929/* Configure an out-of-band gpio as wake-up pin, if specified in device tree */
2930static int marvell_config_oob_wake(struct hci_dev *hdev)
2931{
2932	struct sk_buff *skb;
2933	struct btusb_data *data = hci_get_drvdata(hdev);
2934	struct device *dev = &data->udev->dev;
2935	u16 pin, gap, opcode;
2936	int ret;
2937	u8 cmd[5];
2938
2939	/* Move on if no wakeup pin specified */
2940	if (of_property_read_u16(dev->of_node, "marvell,wakeup-pin", &pin) ||
2941	    of_property_read_u16(dev->of_node, "marvell,wakeup-gap-ms", &gap))
2942		return 0;
2943
2944	/* Vendor specific command to configure a GPIO as wake-up pin */
2945	opcode = hci_opcode_pack(0x3F, 0x59);
2946	cmd[0] = opcode & 0xFF;
2947	cmd[1] = opcode >> 8;
2948	cmd[2] = 2; /* length of parameters that follow */
2949	cmd[3] = pin;
2950	cmd[4] = gap; /* time in ms, for which wakeup pin should be asserted */
2951
2952	skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL);
2953	if (!skb) {
2954		bt_dev_err(hdev, "%s: No memory", __func__);
2955		return -ENOMEM;
2956	}
2957
2958	skb_put_data(skb, cmd, sizeof(cmd));
2959	hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
2960
2961	ret = btusb_send_frame(hdev, skb);
2962	if (ret) {
2963		bt_dev_err(hdev, "%s: configuration failed", __func__);
2964		kfree_skb(skb);
2965		return ret;
2966	}
2967
2968	return 0;
2969}
2970#endif
2971
2972static int btusb_set_bdaddr_marvell(struct hci_dev *hdev,
2973				    const bdaddr_t *bdaddr)
2974{
2975	struct sk_buff *skb;
2976	u8 buf[8];
2977	long ret;
2978
2979	buf[0] = 0xfe;
2980	buf[1] = sizeof(bdaddr_t);
2981	memcpy(buf + 2, bdaddr, sizeof(bdaddr_t));
2982
2983	skb = __hci_cmd_sync(hdev, 0xfc22, sizeof(buf), buf, HCI_INIT_TIMEOUT);
2984	if (IS_ERR(skb)) {
2985		ret = PTR_ERR(skb);
2986		bt_dev_err(hdev, "changing Marvell device address failed (%ld)",
2987			   ret);
2988		return ret;
2989	}
2990	kfree_skb(skb);
2991
2992	return 0;
2993}
2994
2995static int btusb_set_bdaddr_ath3012(struct hci_dev *hdev,
2996				    const bdaddr_t *bdaddr)
2997{
2998	struct sk_buff *skb;
2999	u8 buf[10];
3000	long ret;
3001
3002	buf[0] = 0x01;
3003	buf[1] = 0x01;
3004	buf[2] = 0x00;
3005	buf[3] = sizeof(bdaddr_t);
3006	memcpy(buf + 4, bdaddr, sizeof(bdaddr_t));
3007
3008	skb = __hci_cmd_sync(hdev, 0xfc0b, sizeof(buf), buf, HCI_INIT_TIMEOUT);
3009	if (IS_ERR(skb)) {
3010		ret = PTR_ERR(skb);
3011		bt_dev_err(hdev, "Change address command failed (%ld)", ret);
3012		return ret;
3013	}
3014	kfree_skb(skb);
3015
3016	return 0;
3017}
3018
3019static int btusb_set_bdaddr_wcn6855(struct hci_dev *hdev,
3020				const bdaddr_t *bdaddr)
3021{
3022	struct sk_buff *skb;
3023	u8 buf[6];
3024	long ret;
3025
3026	memcpy(buf, bdaddr, sizeof(bdaddr_t));
3027
3028	skb = __hci_cmd_sync_ev(hdev, 0xfc14, sizeof(buf), buf,
3029				HCI_EV_CMD_COMPLETE, HCI_INIT_TIMEOUT);
3030	if (IS_ERR(skb)) {
3031		ret = PTR_ERR(skb);
3032		bt_dev_err(hdev, "Change address command failed (%ld)", ret);
3033		return ret;
3034	}
3035	kfree_skb(skb);
3036
3037	return 0;
3038}
3039
3040#define QCA_MEMDUMP_ACL_HANDLE 0x2EDD
3041#define QCA_MEMDUMP_SIZE_MAX  0x100000
3042#define QCA_MEMDUMP_VSE_CLASS 0x01
3043#define QCA_MEMDUMP_MSG_TYPE 0x08
3044#define QCA_MEMDUMP_PKT_SIZE 248
3045#define QCA_LAST_SEQUENCE_NUM 0xffff
3046
3047struct qca_dump_hdr {
3048	u8 vse_class;
3049	u8 msg_type;
3050	__le16 seqno;
3051	u8 reserved;
3052	union {
3053		u8 data[0];
3054		struct {
3055			__le32 ram_dump_size;
3056			u8 data0[0];
3057		} __packed;
3058	};
3059} __packed;
3060
3061
3062static void btusb_dump_hdr_qca(struct hci_dev *hdev, struct sk_buff *skb)
3063{
3064	char buf[128];
3065	struct btusb_data *btdata = hci_get_drvdata(hdev);
3066
3067	snprintf(buf, sizeof(buf), "Controller Name: 0x%x\n",
3068			btdata->qca_dump.controller_id);
3069	skb_put_data(skb, buf, strlen(buf));
3070
3071	snprintf(buf, sizeof(buf), "Firmware Version: 0x%x\n",
3072			btdata->qca_dump.fw_version);
3073	skb_put_data(skb, buf, strlen(buf));
3074
3075	snprintf(buf, sizeof(buf), "Driver: %s\nVendor: qca\n",
3076			btusb_driver.name);
3077	skb_put_data(skb, buf, strlen(buf));
3078
3079	snprintf(buf, sizeof(buf), "VID: 0x%x\nPID:0x%x\n",
3080			btdata->qca_dump.id_vendor, btdata->qca_dump.id_product);
3081	skb_put_data(skb, buf, strlen(buf));
3082
3083	snprintf(buf, sizeof(buf), "Lmp Subversion: 0x%x\n",
3084			hdev->lmp_subver);
3085	skb_put_data(skb, buf, strlen(buf));
3086}
3087
3088static void btusb_coredump_qca(struct hci_dev *hdev)
3089{
3090	int err;
3091	static const u8 param[] = { 0x26 };
3092
3093	err = __hci_cmd_send(hdev, 0xfc0c, 1, param);
3094	if (err < 0)
3095		bt_dev_err(hdev, "%s: triggle crash failed (%d)", __func__, err);
3096}
3097
3098/* Return: 0 on success, negative errno on failure. */
3099static int handle_dump_pkt_qca(struct hci_dev *hdev, struct sk_buff *skb)
3100{
3101	int ret = 0;
3102	unsigned int skip = 0;
3103	u8 pkt_type;
3104	u16 seqno;
3105	u32 dump_size;
3106
3107	struct qca_dump_hdr *dump_hdr;
3108	struct btusb_data *btdata = hci_get_drvdata(hdev);
3109	struct usb_device *udev = btdata->udev;
3110
3111	pkt_type = hci_skb_pkt_type(skb);
3112	skip = sizeof(struct hci_event_hdr);
3113	if (pkt_type == HCI_ACLDATA_PKT)
3114		skip += sizeof(struct hci_acl_hdr);
3115
3116	skb_pull(skb, skip);
3117	dump_hdr = (struct qca_dump_hdr *)skb->data;
3118
3119	seqno = le16_to_cpu(dump_hdr->seqno);
3120	if (seqno == 0) {
3121		set_bit(BTUSB_HW_SSR_ACTIVE, &btdata->flags);
3122		dump_size = le32_to_cpu(dump_hdr->ram_dump_size);
3123		if (!dump_size || (dump_size > QCA_MEMDUMP_SIZE_MAX)) {
3124			ret = -EILSEQ;
3125			bt_dev_err(hdev, "Invalid memdump size(%u)",
3126				   dump_size);
3127			goto out;
3128		}
3129
3130		ret = hci_devcd_init(hdev, dump_size);
3131		if (ret < 0) {
3132			bt_dev_err(hdev, "memdump init error(%d)", ret);
3133			goto out;
3134		}
3135
3136		btdata->qca_dump.ram_dump_size = dump_size;
3137		btdata->qca_dump.ram_dump_seqno = 0;
3138
3139		skb_pull(skb, offsetof(struct qca_dump_hdr, data0));
3140
3141		usb_disable_autosuspend(udev);
3142		bt_dev_info(hdev, "%s memdump size(%u)\n",
3143			    (pkt_type == HCI_ACLDATA_PKT) ? "ACL" : "event",
3144			    dump_size);
3145	} else {
3146		skb_pull(skb, offsetof(struct qca_dump_hdr, data));
3147	}
3148
3149	if (!btdata->qca_dump.ram_dump_size) {
3150		ret = -EINVAL;
3151		bt_dev_err(hdev, "memdump is not active");
3152		goto out;
3153	}
3154
3155	if ((seqno > btdata->qca_dump.ram_dump_seqno + 1) && (seqno != QCA_LAST_SEQUENCE_NUM)) {
3156		dump_size = QCA_MEMDUMP_PKT_SIZE * (seqno - btdata->qca_dump.ram_dump_seqno - 1);
3157		hci_devcd_append_pattern(hdev, 0x0, dump_size);
3158		bt_dev_err(hdev,
3159			   "expected memdump seqno(%u) is not received(%u)\n",
3160			   btdata->qca_dump.ram_dump_seqno, seqno);
3161		btdata->qca_dump.ram_dump_seqno = seqno;
3162		kfree_skb(skb);
3163		return ret;
3164	}
3165
3166	hci_devcd_append(hdev, skb);
3167	btdata->qca_dump.ram_dump_seqno++;
3168	if (seqno == QCA_LAST_SEQUENCE_NUM) {
3169		bt_dev_info(hdev,
3170				"memdump done: pkts(%u), total(%u)\n",
3171				btdata->qca_dump.ram_dump_seqno, btdata->qca_dump.ram_dump_size);
3172
3173		hci_devcd_complete(hdev);
3174		goto out;
3175	}
3176	return ret;
3177
3178out:
3179	if (btdata->qca_dump.ram_dump_size)
3180		usb_enable_autosuspend(udev);
3181	btdata->qca_dump.ram_dump_size = 0;
3182	btdata->qca_dump.ram_dump_seqno = 0;
3183	clear_bit(BTUSB_HW_SSR_ACTIVE, &btdata->flags);
3184
3185	if (ret < 0)
3186		kfree_skb(skb);
3187	return ret;
3188}
3189
3190/* Return: true if the ACL packet is a dump packet, false otherwise. */
3191static bool acl_pkt_is_dump_qca(struct hci_dev *hdev, struct sk_buff *skb)
3192{
3193	struct hci_event_hdr *event_hdr;
3194	struct hci_acl_hdr *acl_hdr;
3195	struct qca_dump_hdr *dump_hdr;
3196	struct sk_buff *clone = skb_clone(skb, GFP_ATOMIC);
3197	bool is_dump = false;
3198
3199	if (!clone)
3200		return false;
3201
3202	acl_hdr = skb_pull_data(clone, sizeof(*acl_hdr));
3203	if (!acl_hdr || (le16_to_cpu(acl_hdr->handle) != QCA_MEMDUMP_ACL_HANDLE))
3204		goto out;
3205
3206	event_hdr = skb_pull_data(clone, sizeof(*event_hdr));
3207	if (!event_hdr || (event_hdr->evt != HCI_VENDOR_PKT))
3208		goto out;
3209
3210	dump_hdr = skb_pull_data(clone, sizeof(*dump_hdr));
3211	if (!dump_hdr || (dump_hdr->vse_class != QCA_MEMDUMP_VSE_CLASS) ||
3212	   (dump_hdr->msg_type != QCA_MEMDUMP_MSG_TYPE))
3213		goto out;
3214
3215	is_dump = true;
3216out:
3217	consume_skb(clone);
3218	return is_dump;
3219}
3220
3221/* Return: true if the event packet is a dump packet, false otherwise. */
3222static bool evt_pkt_is_dump_qca(struct hci_dev *hdev, struct sk_buff *skb)
3223{
3224	struct hci_event_hdr *event_hdr;
3225	struct qca_dump_hdr *dump_hdr;
3226	struct sk_buff *clone = skb_clone(skb, GFP_ATOMIC);
3227	bool is_dump = false;
3228
3229	if (!clone)
3230		return false;
3231
3232	event_hdr = skb_pull_data(clone, sizeof(*event_hdr));
3233	if (!event_hdr || (event_hdr->evt != HCI_VENDOR_PKT))
3234		goto out;
3235
3236	dump_hdr = skb_pull_data(clone, sizeof(*dump_hdr));
3237	if (!dump_hdr || (dump_hdr->vse_class != QCA_MEMDUMP_VSE_CLASS) ||
3238	   (dump_hdr->msg_type != QCA_MEMDUMP_MSG_TYPE))
3239		goto out;
3240
3241	is_dump = true;
3242out:
3243	consume_skb(clone);
3244	return is_dump;
3245}
3246
3247static int btusb_recv_acl_qca(struct hci_dev *hdev, struct sk_buff *skb)
3248{
3249	if (acl_pkt_is_dump_qca(hdev, skb))
3250		return handle_dump_pkt_qca(hdev, skb);
3251	return hci_recv_frame(hdev, skb);
3252}
3253
3254static int btusb_recv_evt_qca(struct hci_dev *hdev, struct sk_buff *skb)
3255{
3256	if (evt_pkt_is_dump_qca(hdev, skb))
3257		return handle_dump_pkt_qca(hdev, skb);
3258	return hci_recv_frame(hdev, skb);
3259}
3260
3261
3262#define QCA_DFU_PACKET_LEN	4096
3263
3264#define QCA_GET_TARGET_VERSION	0x09
3265#define QCA_CHECK_STATUS	0x05
3266#define QCA_DFU_DOWNLOAD	0x01
3267
3268#define QCA_SYSCFG_UPDATED	0x40
3269#define QCA_PATCH_UPDATED	0x80
3270#define QCA_DFU_TIMEOUT		3000
3271#define QCA_FLAG_MULTI_NVM      0x80
3272#define QCA_BT_RESET_WAIT_MS    100
3273
3274#define WCN6855_2_0_RAM_VERSION_GF 0x400c1200
3275#define WCN6855_2_1_RAM_VERSION_GF 0x400c1211
3276
3277struct qca_version {
3278	__le32	rom_version;
3279	__le32	patch_version;
3280	__le32	ram_version;
3281	__u8	chip_id;
3282	__u8	platform_id;
3283	__le16	flag;
3284	__u8	reserved[4];
3285} __packed;
3286
3287struct qca_rampatch_version {
3288	__le16	rom_version_high;
3289	__le16  rom_version_low;
3290	__le16	patch_version;
3291} __packed;
3292
3293struct qca_device_info {
3294	u32	rom_version;
3295	u8	rampatch_hdr;	/* length of header in rampatch */
3296	u8	nvm_hdr;	/* length of header in NVM */
3297	u8	ver_offset;	/* offset of version structure in rampatch */
3298};
3299
3300struct qca_custom_firmware {
3301	u32 rom_version;
3302	u16 board_id;
3303	const char *subdirectory;
3304};
3305
3306static const struct qca_device_info qca_devices_table[] = {
3307	{ 0x00000100, 20, 4,  8 }, /* Rome 1.0 */
3308	{ 0x00000101, 20, 4,  8 }, /* Rome 1.1 */
3309	{ 0x00000200, 28, 4, 16 }, /* Rome 2.0 */
3310	{ 0x00000201, 28, 4, 16 }, /* Rome 2.1 */
3311	{ 0x00000300, 28, 4, 16 }, /* Rome 3.0 */
3312	{ 0x00000302, 28, 4, 16 }, /* Rome 3.2 */
3313	{ 0x00130100, 40, 4, 16 }, /* WCN6855 1.0 */
3314	{ 0x00130200, 40, 4, 16 }, /* WCN6855 2.0 */
3315	{ 0x00130201, 40, 4, 16 }, /* WCN6855 2.1 */
3316	{ 0x00190200, 40, 4, 16 }, /* WCN785x 2.0 */
3317};
3318
3319static const struct qca_custom_firmware qca_custom_btfws[] = {
3320	{ 0x00130201, 0x030A, "QCA2066" },
3321	{ 0x00130201, 0x030B, "QCA2066" },
3322	{ },
3323};
3324
3325static u16 qca_extract_board_id(const struct qca_version *ver)
3326{
3327	u16 flag = le16_to_cpu(ver->flag);
3328	u16 board_id = 0;
3329
3330	if (((flag >> 8) & 0xff) == QCA_FLAG_MULTI_NVM) {
3331		/* The board_id should be split into two bytes
3332		 * The 1st byte is chip ID, and the 2nd byte is platform ID
3333		 * For example, board ID 0x010A, 0x01 is platform ID. 0x0A is chip ID
3334		 * we have several platforms, and platform IDs are continuously added
3335		 * Platform ID:
3336		 * 0x00 is for Mobile
3337		 * 0x01 is for X86
3338		 * 0x02 is for Automotive
3339		 * 0x03 is for Consumer electronic
3340		 */
3341		board_id = (ver->chip_id << 8) + ver->platform_id;
3342	}
3343
3344	/* Take 0xffff as invalid board ID */
3345	if (board_id == 0xffff)
3346		board_id = 0;
3347
3348	return board_id;
3349}
3350
3351static const char *qca_get_fw_subdirectory(const struct qca_version *ver)
3352{
3353	const struct qca_custom_firmware *ptr;
3354	u32 rom_ver;
3355	u16 board_id;
3356
3357	rom_ver = le32_to_cpu(ver->rom_version);
3358	board_id = qca_extract_board_id(ver);
3359	if (!board_id)
3360		return NULL;
3361
3362	for (ptr = qca_custom_btfws; ptr->rom_version; ptr++) {
3363		if (ptr->rom_version == rom_ver &&
3364		    ptr->board_id == board_id)
3365			return ptr->subdirectory;
3366	}
3367
3368	return NULL;
3369}
3370
3371static int btusb_qca_send_vendor_req(struct usb_device *udev, u8 request,
3372				     void *data, u16 size)
3373{
3374	int pipe, err;
3375	u8 *buf;
3376
3377	buf = kmalloc(size, GFP_KERNEL);
3378	if (!buf)
3379		return -ENOMEM;
3380
3381	/* Found some of USB hosts have IOT issues with ours so that we should
3382	 * not wait until HCI layer is ready.
3383	 */
3384	pipe = usb_rcvctrlpipe(udev, 0);
3385	err = usb_control_msg(udev, pipe, request, USB_TYPE_VENDOR | USB_DIR_IN,
3386			      0, 0, buf, size, USB_CTRL_GET_TIMEOUT);
3387	if (err < 0) {
3388		dev_err(&udev->dev, "Failed to access otp area (%d)", err);
3389		goto done;
3390	}
3391
3392	memcpy(data, buf, size);
3393
3394done:
3395	kfree(buf);
3396
3397	return err;
3398}
3399
3400static int btusb_setup_qca_download_fw(struct hci_dev *hdev,
3401				       const struct firmware *firmware,
3402				       size_t hdr_size)
3403{
3404	struct btusb_data *btdata = hci_get_drvdata(hdev);
3405	struct usb_device *udev = btdata->udev;
3406	size_t count, size, sent = 0;
3407	int pipe, len, err;
3408	u8 *buf;
3409
3410	buf = kmalloc(QCA_DFU_PACKET_LEN, GFP_KERNEL);
3411	if (!buf)
3412		return -ENOMEM;
3413
3414	count = firmware->size;
3415
3416	size = min_t(size_t, count, hdr_size);
3417	memcpy(buf, firmware->data, size);
3418
3419	/* USB patches should go down to controller through USB path
3420	 * because binary format fits to go down through USB channel.
3421	 * USB control path is for patching headers and USB bulk is for
3422	 * patch body.
3423	 */
3424	pipe = usb_sndctrlpipe(udev, 0);
3425	err = usb_control_msg(udev, pipe, QCA_DFU_DOWNLOAD, USB_TYPE_VENDOR,
3426			      0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
3427	if (err < 0) {
3428		bt_dev_err(hdev, "Failed to send headers (%d)", err);
3429		goto done;
3430	}
3431
3432	sent += size;
3433	count -= size;
3434
3435	/* ep2 need time to switch from function acl to function dfu,
3436	 * so we add 20ms delay here.
3437	 */
3438	msleep(20);
3439
3440	while (count) {
3441		size = min_t(size_t, count, QCA_DFU_PACKET_LEN);
3442
3443		memcpy(buf, firmware->data + sent, size);
3444
3445		pipe = usb_sndbulkpipe(udev, 0x02);
3446		err = usb_bulk_msg(udev, pipe, buf, size, &len,
3447				   QCA_DFU_TIMEOUT);
3448		if (err < 0) {
3449			bt_dev_err(hdev, "Failed to send body at %zd of %zd (%d)",
3450				   sent, firmware->size, err);
3451			break;
3452		}
3453
3454		if (size != len) {
3455			bt_dev_err(hdev, "Failed to get bulk buffer");
3456			err = -EILSEQ;
3457			break;
3458		}
3459
3460		sent  += size;
3461		count -= size;
3462	}
3463
3464done:
3465	kfree(buf);
3466	return err;
3467}
3468
3469static int btusb_setup_qca_load_rampatch(struct hci_dev *hdev,
3470					 struct qca_version *ver,
3471					 const struct qca_device_info *info)
3472{
3473	struct qca_rampatch_version *rver;
3474	const struct firmware *fw;
3475	const char *fw_subdir;
3476	u32 ver_rom, ver_patch, rver_rom;
3477	u16 rver_rom_low, rver_rom_high, rver_patch;
3478	char fwname[80];
3479	int err;
3480
3481	ver_rom = le32_to_cpu(ver->rom_version);
3482	ver_patch = le32_to_cpu(ver->patch_version);
3483
3484	fw_subdir = qca_get_fw_subdirectory(ver);
3485	if (fw_subdir)
3486		snprintf(fwname, sizeof(fwname), "qca/%s/rampatch_usb_%08x.bin",
3487			 fw_subdir, ver_rom);
3488	else
3489		snprintf(fwname, sizeof(fwname), "qca/rampatch_usb_%08x.bin",
3490			 ver_rom);
3491
3492	err = request_firmware(&fw, fwname, &hdev->dev);
3493	if (err) {
3494		bt_dev_err(hdev, "failed to request rampatch file: %s (%d)",
3495			   fwname, err);
3496		return err;
3497	}
3498
3499	bt_dev_info(hdev, "using rampatch file: %s", fwname);
3500
3501	rver = (struct qca_rampatch_version *)(fw->data + info->ver_offset);
3502	rver_rom_low = le16_to_cpu(rver->rom_version_low);
3503	rver_patch = le16_to_cpu(rver->patch_version);
3504
3505	if (ver_rom & ~0xffffU) {
3506		rver_rom_high = le16_to_cpu(rver->rom_version_high);
3507		rver_rom = rver_rom_high << 16 | rver_rom_low;
3508	} else {
3509		rver_rom = rver_rom_low;
3510	}
3511
3512	bt_dev_info(hdev, "QCA: patch rome 0x%x build 0x%x, "
3513		    "firmware rome 0x%x build 0x%x",
3514		    rver_rom, rver_patch, ver_rom, ver_patch);
3515
3516	if (rver_rom != ver_rom || rver_patch <= ver_patch) {
3517		bt_dev_err(hdev, "rampatch file version did not match with firmware");
3518		err = -EINVAL;
3519		goto done;
3520	}
3521
3522	err = btusb_setup_qca_download_fw(hdev, fw, info->rampatch_hdr);
3523
3524done:
3525	release_firmware(fw);
3526
3527	return err;
3528}
3529
3530static void btusb_generate_qca_nvm_name(char *fwname, size_t max_size,
3531					const struct qca_version *ver)
3532{
3533	u32 rom_version = le32_to_cpu(ver->rom_version);
3534	const char *variant, *fw_subdir;
3535	int len;
3536	u16 board_id;
3537
3538	fw_subdir = qca_get_fw_subdirectory(ver);
3539	board_id = qca_extract_board_id(ver);
3540
3541	switch (le32_to_cpu(ver->ram_version)) {
3542	case WCN6855_2_0_RAM_VERSION_GF:
3543	case WCN6855_2_1_RAM_VERSION_GF:
3544		variant = "_gf";
3545		break;
3546	default:
3547		variant = NULL;
3548		break;
3549	}
3550
3551	if (fw_subdir)
3552		len = snprintf(fwname, max_size, "qca/%s/nvm_usb_%08x",
3553			       fw_subdir, rom_version);
3554	else
3555		len = snprintf(fwname, max_size, "qca/nvm_usb_%08x",
3556			       rom_version);
3557	if (variant)
3558		len += snprintf(fwname + len, max_size - len, "%s", variant);
3559	if (board_id)
3560		len += snprintf(fwname + len, max_size - len, "_%04x", board_id);
3561	len += snprintf(fwname + len, max_size - len, ".bin");
3562}
3563
3564static int btusb_setup_qca_load_nvm(struct hci_dev *hdev,
3565				    struct qca_version *ver,
3566				    const struct qca_device_info *info)
3567{
3568	const struct firmware *fw;
3569	char fwname[80];
3570	int err;
3571
3572	btusb_generate_qca_nvm_name(fwname, sizeof(fwname), ver);
3573
3574	err = request_firmware(&fw, fwname, &hdev->dev);
3575	if (err) {
3576		bt_dev_err(hdev, "failed to request NVM file: %s (%d)",
3577			   fwname, err);
3578		return err;
3579	}
3580
3581	bt_dev_info(hdev, "using NVM file: %s", fwname);
3582
3583	err = btusb_setup_qca_download_fw(hdev, fw, info->nvm_hdr);
3584
3585	release_firmware(fw);
3586
3587	return err;
3588}
3589
3590/* identify the ROM version and check whether patches are needed */
3591static bool btusb_qca_need_patch(struct usb_device *udev)
3592{
3593	struct qca_version ver;
3594
3595	if (btusb_qca_send_vendor_req(udev, QCA_GET_TARGET_VERSION, &ver,
3596				      sizeof(ver)) < 0)
3597		return false;
3598	/* only low ROM versions need patches */
3599	return !(le32_to_cpu(ver.rom_version) & ~0xffffU);
3600}
3601
3602static int btusb_setup_qca(struct hci_dev *hdev)
3603{
3604	struct btusb_data *btdata = hci_get_drvdata(hdev);
3605	struct usb_device *udev = btdata->udev;
3606	const struct qca_device_info *info = NULL;
3607	struct qca_version ver;
3608	u32 ver_rom;
3609	u8 status;
3610	int i, err;
3611
3612	err = btusb_qca_send_vendor_req(udev, QCA_GET_TARGET_VERSION, &ver,
3613					sizeof(ver));
3614	if (err < 0)
3615		return err;
3616
3617	ver_rom = le32_to_cpu(ver.rom_version);
3618
3619	for (i = 0; i < ARRAY_SIZE(qca_devices_table); i++) {
3620		if (ver_rom == qca_devices_table[i].rom_version)
3621			info = &qca_devices_table[i];
3622	}
3623	if (!info) {
3624		/* If the rom_version is not matched in the qca_devices_table
3625		 * and the high ROM version is not zero, we assume this chip no
3626		 * need to load the rampatch and nvm.
3627		 */
3628		if (ver_rom & ~0xffffU)
3629			return 0;
3630
3631		bt_dev_err(hdev, "don't support firmware rome 0x%x", ver_rom);
3632		return -ENODEV;
3633	}
3634
3635	err = btusb_qca_send_vendor_req(udev, QCA_CHECK_STATUS, &status,
3636					sizeof(status));
3637	if (err < 0)
3638		return err;
3639
3640	if (!(status & QCA_PATCH_UPDATED)) {
3641		err = btusb_setup_qca_load_rampatch(hdev, &ver, info);
3642		if (err < 0)
3643			return err;
3644	}
3645
3646	err = btusb_qca_send_vendor_req(udev, QCA_GET_TARGET_VERSION, &ver,
3647					sizeof(ver));
3648	if (err < 0)
3649		return err;
3650
3651	btdata->qca_dump.fw_version = le32_to_cpu(ver.patch_version);
3652	btdata->qca_dump.controller_id = le32_to_cpu(ver.rom_version);
3653
3654	if (!(status & QCA_SYSCFG_UPDATED)) {
3655		err = btusb_setup_qca_load_nvm(hdev, &ver, info);
3656		if (err < 0)
3657			return err;
3658
3659		/* WCN6855 2.1 and later will reset to apply firmware downloaded here, so
3660		 * wait ~100ms for reset Done then go ahead, otherwise, it maybe
3661		 * cause potential enable failure.
3662		 */
3663		if (info->rom_version >= 0x00130201)
3664			msleep(QCA_BT_RESET_WAIT_MS);
3665	}
3666
3667	/* Mark HCI_OP_ENHANCED_SETUP_SYNC_CONN as broken as it doesn't seem to
3668	 * work with the likes of HSP/HFP mSBC.
3669	 */
3670	hci_set_quirk(hdev, HCI_QUIRK_BROKEN_ENHANCED_SETUP_SYNC_CONN);
3671
3672	return 0;
3673}
3674
3675static inline int __set_diag_interface(struct hci_dev *hdev)
3676{
3677	struct btusb_data *data = hci_get_drvdata(hdev);
3678	struct usb_interface *intf = data->diag;
3679	int i;
3680
3681	if (!data->diag)
3682		return -ENODEV;
3683
3684	data->diag_tx_ep = NULL;
3685	data->diag_rx_ep = NULL;
3686
3687	for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
3688		struct usb_endpoint_descriptor *ep_desc;
3689
3690		ep_desc = &intf->cur_altsetting->endpoint[i].desc;
3691
3692		if (!data->diag_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
3693			data->diag_tx_ep = ep_desc;
3694			continue;
3695		}
3696
3697		if (!data->diag_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
3698			data->diag_rx_ep = ep_desc;
3699			continue;
3700		}
3701	}
3702
3703	if (!data->diag_tx_ep || !data->diag_rx_ep) {
3704		bt_dev_err(hdev, "invalid diagnostic descriptors");
3705		return -ENODEV;
3706	}
3707
3708	return 0;
3709}
3710
3711static struct urb *alloc_diag_urb(struct hci_dev *hdev, bool enable)
3712{
3713	struct btusb_data *data = hci_get_drvdata(hdev);
3714	struct sk_buff *skb;
3715	struct urb *urb;
3716	unsigned int pipe;
3717
3718	if (!data->diag_tx_ep)
3719		return ERR_PTR(-ENODEV);
3720
3721	urb = usb_alloc_urb(0, GFP_KERNEL);
3722	if (!urb)
3723		return ERR_PTR(-ENOMEM);
3724
3725	skb = bt_skb_alloc(2, GFP_KERNEL);
3726	if (!skb) {
3727		usb_free_urb(urb);
3728		return ERR_PTR(-ENOMEM);
3729	}
3730
3731	skb_put_u8(skb, 0xf0);
3732	skb_put_u8(skb, enable);
3733
3734	pipe = usb_sndbulkpipe(data->udev, data->diag_tx_ep->bEndpointAddress);
3735
3736	usb_fill_bulk_urb(urb, data->udev, pipe,
3737			  skb->data, skb->len, btusb_tx_complete, skb);
3738
3739	skb->dev = (void *)hdev;
3740
3741	return urb;
3742}
3743
3744static int btusb_bcm_set_diag(struct hci_dev *hdev, bool enable)
3745{
3746	struct btusb_data *data = hci_get_drvdata(hdev);
3747	struct urb *urb;
3748
3749	if (!data->diag)
3750		return -ENODEV;
3751
3752	if (!test_bit(HCI_RUNNING, &hdev->flags))
3753		return -ENETDOWN;
3754
3755	urb = alloc_diag_urb(hdev, enable);
3756	if (IS_ERR(urb))
3757		return PTR_ERR(urb);
3758
3759	return submit_or_queue_tx_urb(hdev, urb);
3760}
3761
3762#ifdef CONFIG_PM
3763static irqreturn_t btusb_oob_wake_handler(int irq, void *priv)
3764{
3765	struct btusb_data *data = priv;
3766
3767	pm_wakeup_event(&data->udev->dev, 0);
3768	pm_system_wakeup();
3769
3770	/* Disable only if not already disabled (keep it balanced) */
3771	if (test_and_clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags)) {
3772		disable_irq_nosync(irq);
3773		disable_irq_wake(irq);
3774	}
3775	return IRQ_HANDLED;
3776}
3777
3778static const struct of_device_id btusb_match_table[] = {
3779	{ .compatible = "usb1286,204e" },
3780	{ .compatible = "usbcf3,e300" }, /* QCA6174A */
3781	{ .compatible = "usb4ca,301a" }, /* QCA6174A (Lite-On) */
3782	{ }
3783};
3784MODULE_DEVICE_TABLE(of, btusb_match_table);
3785
3786/* Use an oob wakeup pin? */
3787static int btusb_config_oob_wake(struct hci_dev *hdev)
3788{
3789	struct btusb_data *data = hci_get_drvdata(hdev);
3790	struct device *dev = &data->udev->dev;
3791	int irq, ret;
3792
3793	clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags);
3794
3795	if (!of_match_device(btusb_match_table, dev))
3796		return 0;
3797
3798	/* Move on if no IRQ specified */
3799	irq = of_irq_get_byname(dev->of_node, "wakeup");
3800	if (irq <= 0) {
3801		bt_dev_dbg(hdev, "%s: no OOB Wakeup IRQ in DT", __func__);
3802		return 0;
3803	}
3804
3805	irq_set_status_flags(irq, IRQ_NOAUTOEN);
3806	ret = devm_request_irq(&hdev->dev, irq, btusb_oob_wake_handler,
3807			       0, "OOB Wake-on-BT", data);
3808	if (ret) {
3809		bt_dev_err(hdev, "%s: IRQ request failed", __func__);
3810		return ret;
3811	}
3812
3813	ret = device_init_wakeup(dev, true);
3814	if (ret) {
3815		bt_dev_err(hdev, "%s: failed to init_wakeup", __func__);
3816		return ret;
3817	}
3818
3819	data->oob_wake_irq = irq;
3820	bt_dev_info(hdev, "OOB Wake-on-BT configured at IRQ %u", irq);
3821	return 0;
3822}
3823#endif
3824
3825static void btusb_check_needs_reset_resume(struct usb_interface *intf)
3826{
3827	if (dmi_check_system(btusb_needs_reset_resume_table))
3828		interface_to_usbdev(intf)->quirks |= USB_QUIRK_RESET_RESUME;
3829}
3830
3831static bool btusb_wakeup(struct hci_dev *hdev)
3832{
3833	struct btusb_data *data = hci_get_drvdata(hdev);
3834
3835	return device_may_wakeup(&data->udev->dev);
3836}
3837
3838static int btusb_shutdown_qca(struct hci_dev *hdev)
3839{
3840	struct sk_buff *skb;
3841
3842	skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
3843	if (IS_ERR(skb)) {
3844		bt_dev_err(hdev, "HCI reset during shutdown failed");
3845		return PTR_ERR(skb);
3846	}
3847	kfree_skb(skb);
3848
3849	return 0;
3850}
3851
3852static ssize_t force_poll_sync_read(struct file *file, char __user *user_buf,
3853				    size_t count, loff_t *ppos)
3854{
3855	struct btusb_data *data = file->private_data;
3856	char buf[3];
3857
3858	buf[0] = data->poll_sync ? 'Y' : 'N';
3859	buf[1] = '\n';
3860	buf[2] = '\0';
3861	return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
3862}
3863
3864static ssize_t force_poll_sync_write(struct file *file,
3865				     const char __user *user_buf,
3866				     size_t count, loff_t *ppos)
3867{
3868	struct btusb_data *data = file->private_data;
3869	bool enable;
3870	int err;
3871
3872	err = kstrtobool_from_user(user_buf, count, &enable);
3873	if (err)
3874		return err;
3875
3876	/* Only allow changes while the adapter is down */
3877	if (test_bit(HCI_UP, &data->hdev->flags))
3878		return -EPERM;
3879
3880	if (data->poll_sync == enable)
3881		return -EALREADY;
3882
3883	data->poll_sync = enable;
3884
3885	return count;
3886}
3887
3888static const struct file_operations force_poll_sync_fops = {
3889	.owner		= THIS_MODULE,
3890	.open		= simple_open,
3891	.read		= force_poll_sync_read,
3892	.write		= force_poll_sync_write,
3893	.llseek		= default_llseek,
3894};
3895
3896#define BTUSB_HCI_DRV_OP_SUPPORTED_ALTSETTINGS \
3897		hci_opcode_pack(HCI_DRV_OGF_DRIVER_SPECIFIC, 0x0000)
3898#define BTUSB_HCI_DRV_SUPPORTED_ALTSETTINGS_SIZE	0
3899struct btusb_hci_drv_rp_supported_altsettings {
3900	__u8	num;
3901	__u8	altsettings[];
3902} __packed;
3903
3904#define BTUSB_HCI_DRV_OP_SWITCH_ALTSETTING \
3905		hci_opcode_pack(HCI_DRV_OGF_DRIVER_SPECIFIC, 0x0001)
3906#define BTUSB_HCI_DRV_SWITCH_ALTSETTING_SIZE		1
3907struct btusb_hci_drv_cmd_switch_altsetting {
3908	__u8	altsetting;
3909} __packed;
3910
3911static const struct {
3912	u16 opcode;
3913	const char *desc;
3914} btusb_hci_drv_supported_commands[] = {
3915	/* Common commands */
3916	{ HCI_DRV_OP_READ_INFO, "Read Info" },
3917
3918	/* Driver specific commands */
3919	{ BTUSB_HCI_DRV_OP_SUPPORTED_ALTSETTINGS, "Supported Altsettings" },
3920	{ BTUSB_HCI_DRV_OP_SWITCH_ALTSETTING,     "Switch Altsetting" },
3921};
3922static int btusb_hci_drv_read_info(struct hci_dev *hdev, void *data,
3923				   u16 data_len)
3924{
3925	struct hci_drv_rp_read_info *rp;
3926	size_t rp_size;
3927	int err, i;
3928	u16 opcode, num_supported_commands =
3929		ARRAY_SIZE(btusb_hci_drv_supported_commands);
3930
3931	rp_size = sizeof(*rp) + num_supported_commands * 2;
3932
3933	rp = kmalloc(rp_size, GFP_KERNEL);
3934	if (!rp)
3935		return -ENOMEM;
3936
3937	strscpy_pad(rp->driver_name, btusb_driver.name);
3938
3939	rp->num_supported_commands = cpu_to_le16(num_supported_commands);
3940	for (i = 0; i < num_supported_commands; i++) {
3941		opcode = btusb_hci_drv_supported_commands[i].opcode;
3942		bt_dev_info(hdev,
3943			    "Supported HCI Drv command (0x%02x|0x%04x): %s",
3944			    hci_opcode_ogf(opcode),
3945			    hci_opcode_ocf(opcode),
3946			    btusb_hci_drv_supported_commands[i].desc);
3947		rp->supported_commands[i] = cpu_to_le16(opcode);
3948	}
3949
3950	err = hci_drv_cmd_complete(hdev, HCI_DRV_OP_READ_INFO,
3951				   HCI_DRV_STATUS_SUCCESS, rp, rp_size);
3952
3953	kfree(rp);
3954	return err;
3955}
3956
3957static int btusb_hci_drv_supported_altsettings(struct hci_dev *hdev, void *data,
3958					       u16 data_len)
3959{
3960	struct btusb_data *drvdata = hci_get_drvdata(hdev);
3961	struct btusb_hci_drv_rp_supported_altsettings *rp;
3962	size_t rp_size;
3963	int err;
3964	u8 i;
3965
3966	/* There are at most 7 alt (0 - 6) */
3967	rp = kmalloc(sizeof(*rp) + 7, GFP_KERNEL);
3968	if (!rp)
3969		return -ENOMEM;
3970
3971	rp->num = 0;
3972	if (!drvdata->isoc)
3973		goto done;
3974
3975	for (i = 0; i <= 6; i++) {
3976		if (btusb_find_altsetting(drvdata, i))
3977			rp->altsettings[rp->num++] = i;
3978	}
3979
3980done:
3981	rp_size = sizeof(*rp) + rp->num;
3982
3983	err = hci_drv_cmd_complete(hdev, BTUSB_HCI_DRV_OP_SUPPORTED_ALTSETTINGS,
3984				   HCI_DRV_STATUS_SUCCESS, rp, rp_size);
3985	kfree(rp);
3986	return err;
3987}
3988
3989static int btusb_hci_drv_switch_altsetting(struct hci_dev *hdev, void *data,
3990					   u16 data_len)
3991{
3992	struct btusb_hci_drv_cmd_switch_altsetting *cmd = data;
3993	u8 status;
3994
3995	if (cmd->altsetting > 6) {
3996		status = HCI_DRV_STATUS_INVALID_PARAMETERS;
3997	} else {
3998		if (btusb_switch_alt_setting(hdev, cmd->altsetting))
3999			status = HCI_DRV_STATUS_UNSPECIFIED_ERROR;
4000		else
4001			status = HCI_DRV_STATUS_SUCCESS;
4002	}
4003
4004	return hci_drv_cmd_status(hdev, BTUSB_HCI_DRV_OP_SWITCH_ALTSETTING,
4005				  status);
4006}
4007
4008static const struct hci_drv_handler btusb_hci_drv_common_handlers[] = {
4009	{ btusb_hci_drv_read_info,	HCI_DRV_READ_INFO_SIZE },
4010};
4011
4012static const struct hci_drv_handler btusb_hci_drv_specific_handlers[] = {
4013	{ btusb_hci_drv_supported_altsettings,
4014				BTUSB_HCI_DRV_SUPPORTED_ALTSETTINGS_SIZE },
4015	{ btusb_hci_drv_switch_altsetting,
4016				BTUSB_HCI_DRV_SWITCH_ALTSETTING_SIZE },
4017};
4018
4019static struct hci_drv btusb_hci_drv = {
4020	.common_handler_count	= ARRAY_SIZE(btusb_hci_drv_common_handlers),
4021	.common_handlers	= btusb_hci_drv_common_handlers,
4022	.specific_handler_count	= ARRAY_SIZE(btusb_hci_drv_specific_handlers),
4023	.specific_handlers	= btusb_hci_drv_specific_handlers,
4024};
4025
4026static int btusb_probe(struct usb_interface *intf,
4027		       const struct usb_device_id *id)
4028{
4029	struct usb_endpoint_descriptor *ep_desc;
4030	struct gpio_desc *reset_gpio;
4031	struct btusb_data *data;
4032	struct hci_dev *hdev;
4033	unsigned ifnum_base;
4034	int i, err, priv_size;
4035
4036	BT_DBG("intf %p id %p", intf, id);
4037
4038	if ((id->driver_info & BTUSB_IFNUM_2) &&
4039	    (intf->cur_altsetting->desc.bInterfaceNumber != 0) &&
4040	    (intf->cur_altsetting->desc.bInterfaceNumber != 2))
4041		return -ENODEV;
4042
4043	ifnum_base = intf->cur_altsetting->desc.bInterfaceNumber;
4044
4045	if (!id->driver_info) {
4046		const struct usb_device_id *match;
4047
4048		match = usb_match_id(intf, quirks_table);
4049		if (match)
4050			id = match;
4051	}
4052
4053	if (id->driver_info == BTUSB_IGNORE)
4054		return -ENODEV;
4055
4056	if (id->driver_info & BTUSB_ATH3012) {
4057		struct usb_device *udev = interface_to_usbdev(intf);
4058
4059		/* Old firmware would otherwise let ath3k driver load
4060		 * patch and sysconfig files
4061		 */
4062		if (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x0001 &&
4063		    !btusb_qca_need_patch(udev))
4064			return -ENODEV;
4065	}
4066
4067	data = kzalloc(sizeof(*data), GFP_KERNEL);
4068	if (!data)
4069		return -ENOMEM;
4070
4071	for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
4072		ep_desc = &intf->cur_altsetting->endpoint[i].desc;
4073
4074		if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
4075			data->intr_ep = ep_desc;
4076			continue;
4077		}
4078
4079		if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
4080			data->bulk_tx_ep = ep_desc;
4081			continue;
4082		}
4083
4084		if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
4085			data->bulk_rx_ep = ep_desc;
4086			continue;
4087		}
4088	}
4089
4090	if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep) {
4091		kfree(data);
4092		return -ENODEV;
4093	}
4094
4095	if (id->driver_info & BTUSB_AMP) {
4096		data->cmdreq_type = USB_TYPE_CLASS | 0x01;
4097		data->cmdreq = 0x2b;
4098	} else {
4099		data->cmdreq_type = USB_TYPE_CLASS;
4100		data->cmdreq = 0x00;
4101	}
4102
4103	data->udev = interface_to_usbdev(intf);
4104	data->intf = intf;
4105
4106	INIT_WORK(&data->work, btusb_work);
4107	INIT_WORK(&data->waker, btusb_waker);
4108	INIT_DELAYED_WORK(&data->rx_work, btusb_rx_work);
4109
4110	skb_queue_head_init(&data->acl_q);
4111
4112	init_usb_anchor(&data->deferred);
4113	init_usb_anchor(&data->tx_anchor);
4114	spin_lock_init(&data->txlock);
4115
4116	init_usb_anchor(&data->intr_anchor);
4117	init_usb_anchor(&data->bulk_anchor);
4118	init_usb_anchor(&data->isoc_anchor);
4119	init_usb_anchor(&data->diag_anchor);
4120	init_usb_anchor(&data->ctrl_anchor);
4121	spin_lock_init(&data->rxlock);
4122
4123	priv_size = 0;
4124
4125	data->recv_event = hci_recv_frame;
4126	data->recv_bulk = btusb_recv_bulk;
4127
4128	if (id->driver_info & BTUSB_INTEL_COMBINED) {
4129		/* Allocate extra space for Intel device */
4130		priv_size += sizeof(struct btintel_data);
4131
4132		/* Override the rx handlers */
4133		data->recv_event = btintel_recv_event;
4134		data->recv_bulk = btusb_recv_bulk_intel;
4135	} else if (id->driver_info & BTUSB_REALTEK) {
4136		/* Allocate extra space for Realtek device */
4137		priv_size += sizeof(struct btrealtek_data);
4138
4139		data->recv_event = btusb_recv_event_realtek;
4140	} else if (id->driver_info & BTUSB_MEDIATEK) {
4141		/* Allocate extra space for Mediatek device */
4142		priv_size += sizeof(struct btmtk_data);
4143	}
4144
4145	data->recv_acl = hci_recv_frame;
4146
4147	hdev = hci_alloc_dev_priv(priv_size);
4148	if (!hdev) {
4149		kfree(data);
4150		return -ENOMEM;
4151	}
4152
4153	hdev->bus = HCI_USB;
4154	hci_set_drvdata(hdev, data);
4155
4156	data->hdev = hdev;
4157
4158	SET_HCIDEV_DEV(hdev, &intf->dev);
4159
4160	reset_gpio = gpiod_get_optional(&data->udev->dev, "reset",
4161					GPIOD_OUT_LOW);
4162	if (IS_ERR(reset_gpio)) {
4163		err = PTR_ERR(reset_gpio);
4164		goto out_free_dev;
4165	} else if (reset_gpio) {
4166		data->reset_gpio = reset_gpio;
4167	}
4168
4169	hdev->open    = btusb_open;
4170	hdev->close   = btusb_close;
4171	hdev->flush   = btusb_flush;
4172	hdev->send    = btusb_send_frame;
4173	hdev->notify  = btusb_notify;
4174	hdev->wakeup  = btusb_wakeup;
4175	hdev->hci_drv = &btusb_hci_drv;
4176
4177#ifdef CONFIG_PM
4178	err = btusb_config_oob_wake(hdev);
4179	if (err)
4180		goto out_free_dev;
4181
4182	/* Marvell devices may need a specific chip configuration */
4183	if (id->driver_info & BTUSB_MARVELL && data->oob_wake_irq) {
4184		err = marvell_config_oob_wake(hdev);
4185		if (err)
4186			goto out_free_dev;
4187	}
4188#endif
4189	if (id->driver_info & BTUSB_CW6622)
4190		hci_set_quirk(hdev, HCI_QUIRK_BROKEN_STORED_LINK_KEY);
4191
4192	if (id->driver_info & BTUSB_BCM2045)
4193		hci_set_quirk(hdev, HCI_QUIRK_BROKEN_STORED_LINK_KEY);
4194
4195	if (id->driver_info & BTUSB_BCM92035)
4196		hdev->setup = btusb_setup_bcm92035;
4197
4198	if (IS_ENABLED(CONFIG_BT_HCIBTUSB_BCM) &&
4199	    (id->driver_info & BTUSB_BCM_PATCHRAM)) {
4200		hdev->manufacturer = 15;
4201		hdev->setup = btbcm_setup_patchram;
4202		hdev->set_diag = btusb_bcm_set_diag;
4203		hdev->set_bdaddr = btbcm_set_bdaddr;
4204
4205		/* Broadcom LM_DIAG Interface numbers are hardcoded */
4206		data->diag = usb_ifnum_to_if(data->udev, ifnum_base + 2);
4207	}
4208
4209	if (IS_ENABLED(CONFIG_BT_HCIBTUSB_BCM) &&
4210	    (id->driver_info & BTUSB_BCM_APPLE)) {
4211		hdev->manufacturer = 15;
4212		hdev->setup = btbcm_setup_apple;
4213		hdev->set_diag = btusb_bcm_set_diag;
4214
4215		/* Broadcom LM_DIAG Interface numbers are hardcoded */
4216		data->diag = usb_ifnum_to_if(data->udev, ifnum_base + 2);
4217	}
4218
4219	/* Combined Intel Device setup to support multiple setup routine */
4220	if (id->driver_info & BTUSB_INTEL_COMBINED) {
4221		err = btintel_configure_setup(hdev, btusb_driver.name);
4222		if (err)
4223			goto out_free_dev;
4224
4225		/* Transport specific configuration */
4226		hdev->send = btusb_send_frame_intel;
4227		hdev->reset = btusb_intel_reset;
4228
4229		if (id->driver_info & BTUSB_INTEL_NO_WBS_SUPPORT)
4230			btintel_set_flag(hdev, INTEL_ROM_LEGACY_NO_WBS_SUPPORT);
4231
4232		if (id->driver_info & BTUSB_INTEL_BROKEN_INITIAL_NCMD)
4233			btintel_set_flag(hdev, INTEL_BROKEN_INITIAL_NCMD);
4234
4235		if (id->driver_info & BTUSB_INTEL_BROKEN_SHUTDOWN_LED)
4236			btintel_set_flag(hdev, INTEL_BROKEN_SHUTDOWN_LED);
4237	}
4238
4239	if (id->driver_info & BTUSB_MARVELL)
4240		hdev->set_bdaddr = btusb_set_bdaddr_marvell;
4241
4242	if (IS_ENABLED(CONFIG_BT_HCIBTUSB_MTK) &&
4243	    (id->driver_info & BTUSB_MEDIATEK)) {
4244		hdev->setup = btusb_mtk_setup;
4245		hdev->shutdown = btusb_mtk_shutdown;
4246		hdev->manufacturer = 70;
4247		hdev->reset = btmtk_reset_sync;
4248		hdev->set_bdaddr = btmtk_set_bdaddr;
4249		hdev->send = btusb_send_frame_mtk;
4250		hci_set_quirk(hdev, HCI_QUIRK_BROKEN_ENHANCED_SETUP_SYNC_CONN);
4251		hci_set_quirk(hdev, HCI_QUIRK_NON_PERSISTENT_SETUP);
4252		data->recv_acl = btmtk_usb_recv_acl;
4253		data->suspend = btmtk_usb_suspend;
4254		data->resume = btmtk_usb_resume;
4255		data->disconnect = btusb_mtk_disconnect;
4256	}
4257
4258	if (id->driver_info & BTUSB_SWAVE) {
4259		hci_set_quirk(hdev, HCI_QUIRK_FIXUP_INQUIRY_MODE);
4260		hci_set_quirk(hdev, HCI_QUIRK_BROKEN_LOCAL_COMMANDS);
4261	}
4262
4263	if (id->driver_info & BTUSB_INTEL_BOOT) {
4264		hdev->manufacturer = 2;
4265		hci_set_quirk(hdev, HCI_QUIRK_RAW_DEVICE);
4266	}
4267
4268	if (id->driver_info & BTUSB_ATH3012) {
4269		data->setup_on_usb = btusb_setup_qca;
4270		hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
4271		hci_set_quirk(hdev, HCI_QUIRK_SIMULTANEOUS_DISCOVERY);
4272		hci_set_quirk(hdev, HCI_QUIRK_STRICT_DUPLICATE_FILTER);
4273	}
4274
4275	if (id->driver_info & BTUSB_QCA_ROME) {
4276		data->setup_on_usb = btusb_setup_qca;
4277		hdev->shutdown = btusb_shutdown_qca;
4278		hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
4279		hdev->reset = btusb_qca_reset;
4280		hci_set_quirk(hdev, HCI_QUIRK_SIMULTANEOUS_DISCOVERY);
4281		btusb_check_needs_reset_resume(intf);
4282	}
4283
4284	if (id->driver_info & BTUSB_QCA_WCN6855) {
4285		data->qca_dump.id_vendor = id->idVendor;
4286		data->qca_dump.id_product = id->idProduct;
4287		data->recv_event = btusb_recv_evt_qca;
4288		data->recv_acl = btusb_recv_acl_qca;
4289		hci_devcd_register(hdev, btusb_coredump_qca, btusb_dump_hdr_qca, NULL);
4290		data->setup_on_usb = btusb_setup_qca;
4291		hdev->classify_pkt_type = btusb_classify_qca_pkt_type;
4292		hdev->shutdown = btusb_shutdown_qca;
4293		hdev->set_bdaddr = btusb_set_bdaddr_wcn6855;
4294		hdev->reset = btusb_qca_reset;
4295		hci_set_quirk(hdev, HCI_QUIRK_SIMULTANEOUS_DISCOVERY);
4296		hci_set_msft_opcode(hdev, 0xFD70);
4297	}
4298
4299	if (id->driver_info & BTUSB_AMP) {
4300		/* AMP controllers do not support SCO packets */
4301		data->isoc = NULL;
4302	} else {
4303		/* Interface orders are hardcoded in the specification */
4304		data->isoc = usb_ifnum_to_if(data->udev, ifnum_base + 1);
4305		data->isoc_ifnum = ifnum_base + 1;
4306	}
4307
4308	if (IS_ENABLED(CONFIG_BT_HCIBTUSB_RTL) &&
4309	    (id->driver_info & BTUSB_REALTEK)) {
4310		btrtl_set_driver_name(hdev, btusb_driver.name);
4311		hdev->setup = btusb_setup_realtek;
4312		hdev->shutdown = btrtl_shutdown_realtek;
4313		hdev->reset = btusb_rtl_reset;
4314		hdev->hw_error = btusb_rtl_hw_error;
4315
4316		/* Realtek devices need to set remote wakeup on auto-suspend */
4317		set_bit(BTUSB_WAKEUP_AUTOSUSPEND, &data->flags);
4318		set_bit(BTUSB_USE_ALT3_FOR_WBS, &data->flags);
4319	}
4320
4321	if (id->driver_info & BTUSB_ACTIONS_SEMI) {
4322		/* Support is advertised, but not implemented */
4323		hci_set_quirk(hdev, HCI_QUIRK_BROKEN_ERR_DATA_REPORTING);
4324		hci_set_quirk(hdev, HCI_QUIRK_BROKEN_READ_TRANSMIT_POWER);
4325		hci_set_quirk(hdev, HCI_QUIRK_BROKEN_SET_RPA_TIMEOUT);
4326		hci_set_quirk(hdev, HCI_QUIRK_BROKEN_EXT_SCAN);
4327		hci_set_quirk(hdev, HCI_QUIRK_BROKEN_READ_ENC_KEY_SIZE);
4328		hci_set_quirk(hdev, HCI_QUIRK_BROKEN_EXT_CREATE_CONN);
4329		hci_set_quirk(hdev, HCI_QUIRK_BROKEN_WRITE_AUTH_PAYLOAD_TIMEOUT);
4330	}
4331
4332	if (!reset)
4333		hci_set_quirk(hdev, HCI_QUIRK_RESET_ON_CLOSE);
4334
4335	if (force_scofix || id->driver_info & BTUSB_WRONG_SCO_MTU) {
4336		if (!disable_scofix)
4337			hci_set_quirk(hdev, HCI_QUIRK_FIXUP_BUFFER_SIZE);
4338	}
4339
4340	if (id->driver_info & BTUSB_BROKEN_ISOC)
4341		data->isoc = NULL;
4342
4343	if (id->driver_info & BTUSB_WIDEBAND_SPEECH)
4344		hci_set_quirk(hdev, HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED);
4345
4346	if (id->driver_info & BTUSB_INVALID_LE_STATES)
4347		hci_set_quirk(hdev, HCI_QUIRK_BROKEN_LE_STATES);
4348
4349	if (id->driver_info & BTUSB_DIGIANSWER) {
4350		data->cmdreq_type = USB_TYPE_VENDOR;
4351		hci_set_quirk(hdev, HCI_QUIRK_RESET_ON_CLOSE);
4352	}
4353
4354	if (id->driver_info & BTUSB_CSR) {
4355		struct usb_device *udev = data->udev;
4356		u16 bcdDevice = le16_to_cpu(udev->descriptor.bcdDevice);
4357
4358		/* Old firmware would otherwise execute USB reset */
4359		if (bcdDevice < 0x117)
4360			hci_set_quirk(hdev, HCI_QUIRK_RESET_ON_CLOSE);
4361
4362		/* This must be set first in case we disable it for fakes */
4363		hci_set_quirk(hdev, HCI_QUIRK_SIMULTANEOUS_DISCOVERY);
4364
4365		/* Fake CSR devices with broken commands */
4366		if (le16_to_cpu(udev->descriptor.idVendor)  == 0x0a12 &&
4367		    le16_to_cpu(udev->descriptor.idProduct) == 0x0001)
4368			hdev->setup = btusb_setup_csr;
4369	}
4370
4371	if (id->driver_info & BTUSB_SNIFFER) {
4372		struct usb_device *udev = data->udev;
4373
4374		/* New sniffer firmware has crippled HCI interface */
4375		if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
4376			hci_set_quirk(hdev, HCI_QUIRK_RAW_DEVICE);
4377	}
4378
4379	if (id->driver_info & BTUSB_INTEL_BOOT) {
4380		/* A bug in the bootloader causes that interrupt interface is
4381		 * only enabled after receiving SetInterface(0, AltSetting=0).
4382		 */
4383		err = usb_set_interface(data->udev, 0, 0);
4384		if (err < 0) {
4385			BT_ERR("failed to set interface 0, alt 0 %d", err);
4386			goto out_free_dev;
4387		}
4388	}
4389
4390	if (data->isoc) {
4391		err = usb_driver_claim_interface(&btusb_driver,
4392						 data->isoc, data);
4393		if (err < 0)
4394			goto out_free_dev;
4395	}
4396
4397	if (IS_ENABLED(CONFIG_BT_HCIBTUSB_BCM) && data->diag) {
4398		if (!usb_driver_claim_interface(&btusb_driver,
4399						data->diag, data))
4400			__set_diag_interface(hdev);
4401		else
4402			data->diag = NULL;
4403	}
4404
4405	if (enable_autosuspend)
4406		usb_enable_autosuspend(data->udev);
4407
4408	data->poll_sync = enable_poll_sync;
4409
4410	err = hci_register_dev(hdev);
4411	if (err < 0)
4412		goto out_free_dev;
4413
4414	usb_set_intfdata(intf, data);
4415
4416	debugfs_create_file("force_poll_sync", 0644, hdev->debugfs, data,
4417			    &force_poll_sync_fops);
4418
4419	return 0;
4420
4421out_free_dev:
4422	if (data->reset_gpio)
4423		gpiod_put(data->reset_gpio);
4424	hci_free_dev(hdev);
4425	kfree(data);
4426	return err;
4427}
4428
4429static void btusb_disconnect(struct usb_interface *intf)
4430{
4431	struct btusb_data *data = usb_get_intfdata(intf);
4432	struct hci_dev *hdev;
4433
4434	BT_DBG("intf %p", intf);
4435
4436	if (!data)
4437		return;
4438
4439	hdev = data->hdev;
4440	usb_set_intfdata(data->intf, NULL);
4441
4442	if (data->isoc)
4443		usb_set_intfdata(data->isoc, NULL);
4444
4445	if (data->diag)
4446		usb_set_intfdata(data->diag, NULL);
4447
4448	if (data->disconnect)
4449		data->disconnect(hdev);
4450
4451	hci_unregister_dev(hdev);
4452
4453	if (data->oob_wake_irq)
4454		device_init_wakeup(&data->udev->dev, false);
4455	if (data->reset_gpio)
4456		gpiod_put(data->reset_gpio);
4457
4458	if (intf == data->intf) {
4459		if (data->isoc)
4460			usb_driver_release_interface(&btusb_driver, data->isoc);
4461		if (data->diag)
4462			usb_driver_release_interface(&btusb_driver, data->diag);
4463	} else if (intf == data->isoc) {
4464		if (data->diag)
4465			usb_driver_release_interface(&btusb_driver, data->diag);
4466		usb_driver_release_interface(&btusb_driver, data->intf);
4467	} else if (intf == data->diag) {
4468		if (data->isoc)
4469			usb_driver_release_interface(&btusb_driver, data->isoc);
4470		usb_driver_release_interface(&btusb_driver, data->intf);
4471	}
4472
4473	hci_free_dev(hdev);
4474	kfree(data);
4475}
4476
4477#ifdef CONFIG_PM
4478static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
4479{
4480	struct btusb_data *data = usb_get_intfdata(intf);
4481
4482	BT_DBG("intf %p", intf);
4483
4484	/* Don't auto-suspend if there are connections; external suspend calls
4485	 * shall never fail.
4486	 */
4487	if (PMSG_IS_AUTO(message) && hci_conn_count(data->hdev))
4488		return -EBUSY;
4489
4490	if (data->suspend_count++)
4491		return 0;
4492
4493	spin_lock_irq(&data->txlock);
4494	if (!(PMSG_IS_AUTO(message) && data->tx_in_flight)) {
4495		set_bit(BTUSB_SUSPENDING, &data->flags);
4496		spin_unlock_irq(&data->txlock);
4497	} else {
4498		spin_unlock_irq(&data->txlock);
4499		data->suspend_count--;
4500		return -EBUSY;
4501	}
4502
4503	cancel_work_sync(&data->work);
4504
4505	if (data->suspend)
4506		data->suspend(data->hdev);
4507
4508	btusb_stop_traffic(data);
4509	usb_kill_anchored_urbs(&data->tx_anchor);
4510
4511	if (data->oob_wake_irq && device_may_wakeup(&data->udev->dev)) {
4512		set_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags);
4513		enable_irq_wake(data->oob_wake_irq);
4514		enable_irq(data->oob_wake_irq);
4515	}
4516
4517	/* For global suspend, Realtek devices lose the loaded fw
4518	 * in them. But for autosuspend, firmware should remain.
4519	 * Actually, it depends on whether the usb host sends
4520	 * set feature (enable wakeup) or not.
4521	 */
4522	if (test_bit(BTUSB_WAKEUP_AUTOSUSPEND, &data->flags)) {
4523		if (PMSG_IS_AUTO(message) &&
4524		    device_can_wakeup(&data->udev->dev))
4525			data->udev->do_remote_wakeup = 1;
4526		else if (!PMSG_IS_AUTO(message) &&
4527			 !device_may_wakeup(&data->udev->dev)) {
4528			data->udev->do_remote_wakeup = 0;
4529			data->udev->reset_resume = 1;
4530		}
4531	}
4532
4533	return 0;
4534}
4535
4536static void play_deferred(struct btusb_data *data)
4537{
4538	struct urb *urb;
4539	int err;
4540
4541	while ((urb = usb_get_from_anchor(&data->deferred))) {
4542		usb_anchor_urb(urb, &data->tx_anchor);
4543
4544		err = usb_submit_urb(urb, GFP_ATOMIC);
4545		if (err < 0) {
4546			if (err != -EPERM && err != -ENODEV)
4547				BT_ERR("%s urb %p submission failed (%d)",
4548				       data->hdev->name, urb, -err);
4549			kfree(urb->setup_packet);
4550			usb_unanchor_urb(urb);
4551			usb_free_urb(urb);
4552			break;
4553		}
4554
4555		data->tx_in_flight++;
4556		usb_free_urb(urb);
4557	}
4558
4559	/* Cleanup the rest deferred urbs. */
4560	while ((urb = usb_get_from_anchor(&data->deferred))) {
4561		kfree(urb->setup_packet);
4562		usb_free_urb(urb);
4563	}
4564}
4565
4566static int btusb_resume(struct usb_interface *intf)
4567{
4568	struct btusb_data *data = usb_get_intfdata(intf);
4569	struct hci_dev *hdev = data->hdev;
4570	int err = 0;
4571
4572	BT_DBG("intf %p", intf);
4573
4574	if (--data->suspend_count)
4575		return 0;
4576
4577	/* Disable only if not already disabled (keep it balanced) */
4578	if (test_and_clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags)) {
4579		disable_irq(data->oob_wake_irq);
4580		disable_irq_wake(data->oob_wake_irq);
4581	}
4582
4583	if (!test_bit(HCI_RUNNING, &hdev->flags))
4584		goto done;
4585
4586	if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
4587		err = btusb_submit_intr_urb(hdev, GFP_NOIO);
4588		if (err < 0) {
4589			clear_bit(BTUSB_INTR_RUNNING, &data->flags);
4590			goto failed;
4591		}
4592	}
4593
4594	if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) {
4595		err = btusb_submit_bulk_urb(hdev, GFP_NOIO);
4596		if (err < 0) {
4597			clear_bit(BTUSB_BULK_RUNNING, &data->flags);
4598			goto failed;
4599		}
4600
4601		btusb_submit_bulk_urb(hdev, GFP_NOIO);
4602	}
4603
4604	if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
4605		if (btusb_submit_isoc_urb(hdev, GFP_NOIO) < 0)
4606			clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
4607		else
4608			btusb_submit_isoc_urb(hdev, GFP_NOIO);
4609	}
4610
4611	if (data->resume)
4612		data->resume(hdev);
4613
4614	spin_lock_irq(&data->txlock);
4615	play_deferred(data);
4616	clear_bit(BTUSB_SUSPENDING, &data->flags);
4617	spin_unlock_irq(&data->txlock);
4618	schedule_work(&data->work);
4619
4620	return 0;
4621
4622failed:
4623	usb_scuttle_anchored_urbs(&data->deferred);
4624done:
4625	spin_lock_irq(&data->txlock);
4626	clear_bit(BTUSB_SUSPENDING, &data->flags);
4627	spin_unlock_irq(&data->txlock);
4628
4629	return err;
4630}
4631#endif
4632
4633#ifdef CONFIG_DEV_COREDUMP
4634static void btusb_coredump(struct device *dev)
4635{
4636	struct btusb_data *data = dev_get_drvdata(dev);
4637	struct hci_dev *hdev = data->hdev;
4638
4639	if (hdev->dump.coredump)
4640		hdev->dump.coredump(hdev);
4641}
4642#endif
4643
4644static struct usb_driver btusb_driver = {
4645	.name		= "btusb",
4646	.probe		= btusb_probe,
4647	.disconnect	= btusb_disconnect,
4648#ifdef CONFIG_PM
4649	.suspend	= btusb_suspend,
4650	.resume		= btusb_resume,
4651#endif
4652	.id_table	= btusb_table,
4653	.supports_autosuspend = 1,
4654	.disable_hub_initiated_lpm = 1,
4655
4656#ifdef CONFIG_DEV_COREDUMP
4657	.driver = {
4658		.coredump = btusb_coredump,
4659	},
4660#endif
4661};
4662
4663module_usb_driver(btusb_driver);
4664
4665module_param(disable_scofix, bool, 0644);
4666MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
4667
4668module_param(force_scofix, bool, 0644);
4669MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
4670
4671module_param(enable_autosuspend, bool, 0644);
4672MODULE_PARM_DESC(enable_autosuspend, "Enable USB autosuspend by default");
4673
4674module_param(reset, bool, 0644);
4675MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
4676
4677MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
4678MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
4679MODULE_VERSION(VERSION);
4680MODULE_LICENSE("GPL");
~/ Read-only file preview.