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