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