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