blob: 88bfdf1347a777b7f3623eec4067fbade0af1239 [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002/*
3 *
4 * Generic Bluetooth USB driver
5 *
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02006 * Copyright (C) 2005-2008 Marcel Holtmann <marcel@holtmann.org>
Marcel Holtmann5e23b922007-10-20 14:12:34 +02007 */
8
Hans de Goede1fdb9262018-02-20 09:06:18 +01009#include <linux/dmi.h>
Marcel Holtmann5e23b922007-10-20 14:12:34 +020010#include <linux/module.h>
Marcel Holtmann5e23b922007-10-20 14:12:34 +020011#include <linux/usb.h>
Hans de Goede61f5ace2018-01-08 10:44:16 +010012#include <linux/usb/quirks.h>
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -070013#include <linux/firmware.h>
Sean Wanga1c49c432019-06-02 08:02:48 +080014#include <linux/iopoll.h>
Rajat Jainfd913ef2017-02-01 14:24:09 -080015#include <linux/of_device.h>
16#include <linux/of_irq.h>
Jeffy Chen017789f2017-02-24 14:24:29 +080017#include <linux/suspend.h>
Rajat Jaindc786b22019-01-24 15:28:14 -080018#include <linux/gpio/consumer.h>
Daniel Drakea2698a92015-04-16 14:09:55 -060019#include <asm/unaligned.h>
Marcel Holtmann5e23b922007-10-20 14:12:34 +020020
21#include <net/bluetooth/bluetooth.h>
22#include <net/bluetooth/hci_core.h>
23
Marcel Holtmann4185a0f2015-04-06 00:52:12 -070024#include "btintel.h"
Marcel Holtmann1df1f592015-04-05 22:52:11 -070025#include "btbcm.h"
Carlo Caionedb33c772015-05-14 10:49:09 +020026#include "btrtl.h"
Sean Wang8c0d17b2021-10-19 05:30:12 +080027#include "btmtk.h"
Marcel Holtmann1df1f592015-04-05 22:52:11 -070028
Marcel Holtmann34dced9b2015-04-05 22:52:16 -070029#define VERSION "0.8"
Marcel Holtmanncfeb4142008-08-07 22:26:56 +020030
Rusty Russell90ab5ee2012-01-13 09:32:20 +103031static bool disable_scofix;
32static bool force_scofix;
Hans de Goedeeff2d682017-11-13 14:44:16 +010033static bool enable_autosuspend = IS_ENABLED(CONFIG_BT_HCIBTUSB_AUTOSUSPEND);
Marcel Holtmann7a9d4022008-11-30 12:17:26 +010034
Shailendra Verma917a3332015-05-25 23:53:40 +053035static bool reset = true;
Marcel Holtmanncfeb4142008-08-07 22:26:56 +020036
37static struct usb_driver btusb_driver;
38
39#define BTUSB_IGNORE 0x01
Marcel Holtmann7a9d4022008-11-30 12:17:26 +010040#define BTUSB_DIGIANSWER 0x02
41#define BTUSB_CSR 0x04
42#define BTUSB_SNIFFER 0x08
43#define BTUSB_BCM92035 0x10
44#define BTUSB_BROKEN_ISOC 0x20
45#define BTUSB_WRONG_SCO_MTU 0x40
Steven.Li2d25f8b2011-07-01 14:02:36 +080046#define BTUSB_ATH3012 0x80
Tedd Ho-Jeong An83f2daf2021-08-04 17:32:10 -070047#define BTUSB_INTEL_COMBINED 0x100
Marcel Holtmann40df7832014-07-06 13:29:58 +020048#define BTUSB_INTEL_BOOT 0x200
49#define BTUSB_BCM_PATCHRAM 0x400
Amitkumar Karwarae8df492014-07-18 14:47:06 -070050#define BTUSB_MARVELL 0x800
Marcel Holtmann4fcef8e2015-01-01 17:34:37 -080051#define BTUSB_SWAVE 0x1000
Marcel Holtmann893ba542015-01-28 20:27:34 -080052#define BTUSB_AMP 0x4000
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +000053#define BTUSB_QCA_ROME 0x8000
Marcel Holtmann17b27722015-03-22 15:52:38 +010054#define BTUSB_BCM_APPLE 0x10000
Daniel Drakea2698a92015-04-16 14:09:55 -060055#define BTUSB_REALTEK 0x20000
Marcel Holtmann6c9d4352015-10-17 14:39:27 +020056#define BTUSB_BCM2045 0x40000
Marcel Holtmann22f8e9d2015-10-20 00:53:33 +020057#define BTUSB_IFNUM_2 0x80000
Szymon Janc418678b2016-09-01 17:22:37 +020058#define BTUSB_CW6622 0x100000
Sean Wanga1c49c432019-06-02 08:02:48 +080059#define BTUSB_MEDIATEK 0x200000
Alain Michaud3e4e3f72020-02-27 18:29:37 +000060#define BTUSB_WIDEBAND_SPEECH 0x400000
Alain Michaud220915852020-04-23 14:43:27 +000061#define BTUSB_VALID_LE_STATES 0x800000
Rocky Liaob40f58b2020-09-29 12:23:51 +080062#define BTUSB_QCA_WCN6855 0x1000000
Tedd Ho-Jeong Anea7c4c02021-08-04 17:32:12 -070063#define BTUSB_INTEL_BROKEN_INITIAL_NCMD 0x4000000
Marcel Holtmann5e23b922007-10-20 14:12:34 +020064
Marcel Holtmann54265202013-10-11 07:46:18 -070065static const struct usb_device_id btusb_table[] = {
Marcel Holtmann5e23b922007-10-20 14:12:34 +020066 /* Generic Bluetooth USB device */
67 { USB_DEVICE_INFO(0xe0, 0x01, 0x01) },
68
Marcel Holtmann893ba542015-01-28 20:27:34 -080069 /* Generic Bluetooth AMP device */
70 { USB_DEVICE_INFO(0xe0, 0x01, 0x04), .driver_info = BTUSB_AMP },
71
Daniel Draked63b2822015-07-17 11:12:25 -060072 /* Generic Bluetooth USB interface */
73 { USB_INTERFACE_INFO(0xe0, 0x01, 0x01) },
74
Henrik Rydberg1fa65352012-08-25 19:28:06 +020075 /* Apple-specific (Broadcom) devices */
Marcel Holtmann17b27722015-03-22 15:52:38 +010076 { USB_VENDOR_AND_INTERFACE_INFO(0x05ac, 0xff, 0x01, 0x01),
Marcel Holtmann22f8e9d2015-10-20 00:53:33 +020077 .driver_info = BTUSB_BCM_APPLE | BTUSB_IFNUM_2 },
Henrik Rydberg1fa65352012-08-25 19:28:06 +020078
Cho, Yu-Chen178c0592013-06-04 21:40:26 +080079 /* MediaTek MT76x0E */
80 { USB_DEVICE(0x0e8d, 0x763f) },
81
Oliver Neukumc510eae2011-09-21 11:41:45 +020082 /* Broadcom SoftSailing reporting vendor specific */
Don Zickus2e8b50632012-03-28 16:41:11 -040083 { USB_DEVICE(0x0a5c, 0x21e1) },
Oliver Neukumc510eae2011-09-21 11:41:45 +020084
Nobuhiro Iwamatsu3cd01972010-08-20 16:24:07 +090085 /* Apple MacBookPro 7,1 */
86 { USB_DEVICE(0x05ac, 0x8213) },
87
Cyril Lacoux0a79f672010-07-14 10:29:27 +040088 /* Apple iMac11,1 */
89 { USB_DEVICE(0x05ac, 0x8215) },
90
Nobuhiro Iwamatsu9c047152010-08-20 16:24:06 +090091 /* Apple MacBookPro6,2 */
92 { USB_DEVICE(0x05ac, 0x8218) },
93
Edgar (gimli) Hucek3e3ede72010-11-04 08:04:33 +010094 /* Apple MacBookAir3,1, MacBookAir3,2 */
95 { USB_DEVICE(0x05ac, 0x821b) },
96
Pieter-Augustijn Van Malleghema63b7232011-09-07 02:28:10 -040097 /* Apple MacBookAir4,1 */
98 { USB_DEVICE(0x05ac, 0x821f) },
99
Marc-Antoine Perennou88d377b2011-03-24 14:51:21 -0300100 /* Apple MacBookPro8,2 */
101 { USB_DEVICE(0x05ac, 0x821a) },
102
Jurgen Kramerf78b68262011-09-04 18:01:42 +0200103 /* Apple MacMini5,1 */
104 { USB_DEVICE(0x05ac, 0x8281) },
105
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200106 /* AVM BlueFRITZ! USB v2.0 */
Marcel Holtmann4fcef8e2015-01-01 17:34:37 -0800107 { USB_DEVICE(0x057c, 0x3800), .driver_info = BTUSB_SWAVE },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200108
109 /* Bluetooth Ultraport Module from IBM */
110 { USB_DEVICE(0x04bf, 0x030a) },
111
112 /* ALPS Modules with non-standard id */
113 { USB_DEVICE(0x044e, 0x3001) },
114 { USB_DEVICE(0x044e, 0x3002) },
115
116 /* Ericsson with non-standard id */
117 { USB_DEVICE(0x0bdb, 0x1002) },
118
119 /* Canyon CN-BTU1 with HID interfaces */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100120 { USB_DEVICE(0x0c10, 0x0000) },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200121
Marcel Holtmannd049f4e2015-01-26 20:35:32 -0800122 /* Broadcom BCM20702B0 (Dynex/Insignia) */
123 { USB_DEVICE(0x19ff, 0x0239), .driver_info = BTUSB_BCM_PATCHRAM },
124
Santtu Rekilä2faf71c2015-10-05 15:45:27 +0300125 /* Broadcom BCM43142A0 (Foxconn/Lenovo) */
Dmitry Tunin628c26b2017-07-06 14:41:13 +0300126 { USB_VENDOR_AND_INTERFACE_INFO(0x105b, 0xff, 0x01, 0x01),
127 .driver_info = BTUSB_BCM_PATCHRAM },
Santtu Rekilä2faf71c2015-10-05 15:45:27 +0300128
Christoph Haaga57bac42017-02-10 14:02:45 +0100129 /* Broadcom BCM920703 (HTC Vive) */
130 { USB_VENDOR_AND_INTERFACE_INFO(0x0bb4, 0xff, 0x01, 0x01),
131 .driver_info = BTUSB_BCM_PATCHRAM },
132
Steven Harms98514032012-04-13 14:45:55 -0400133 /* Foxconn - Hon Hai */
Heinrich Siebmanns6029ddc2014-12-03 19:32:22 +0100134 { USB_VENDOR_AND_INTERFACE_INFO(0x0489, 0xff, 0x01, 0x01),
135 .driver_info = BTUSB_BCM_PATCHRAM },
Steven Harms98514032012-04-13 14:45:55 -0400136
Matej Dubovy8f0c3042015-02-02 18:50:14 +0100137 /* Lite-On Technology - Broadcom based */
138 { USB_VENDOR_AND_INTERFACE_INFO(0x04ca, 0xff, 0x01, 0x01),
139 .driver_info = BTUSB_BCM_PATCHRAM },
140
Andy Shevchenko0b880062014-02-18 18:26:19 +0200141 /* Broadcom devices with vendor specific id */
Petri Gynther10d4c672014-05-08 15:50:01 -0700142 { USB_VENDOR_AND_INTERFACE_INFO(0x0a5c, 0xff, 0x01, 0x01),
143 .driver_info = BTUSB_BCM_PATCHRAM },
Gustavo Padovan92c385f2012-08-06 15:36:49 -0300144
Marcel Holtmannc2aef6e2014-07-21 14:02:33 +0200145 /* ASUSTek Computer - Broadcom based */
Rick Dunn9a5abdaa2015-01-17 05:29:12 +0100146 { USB_VENDOR_AND_INTERFACE_INFO(0x0b05, 0xff, 0x01, 0x01),
147 .driver_info = BTUSB_BCM_PATCHRAM },
Marcel Holtmannc2aef6e2014-07-21 14:02:33 +0200148
Ken O'Brien5bcecf32013-09-21 19:14:43 +0100149 /* Belkin F8065bf - Broadcom based */
Marcel Holtmann6331c682015-03-27 15:11:41 -0700150 { USB_VENDOR_AND_INTERFACE_INFO(0x050d, 0xff, 0x01, 0x01),
151 .driver_info = BTUSB_BCM_PATCHRAM },
Ken O'Brien5bcecf32013-09-21 19:14:43 +0100152
Jurgen Kramer9113bfd2014-02-15 12:01:09 +0100153 /* IMC Networks - Broadcom based */
Marcel Holtmann6331c682015-03-27 15:11:41 -0700154 { USB_VENDOR_AND_INTERFACE_INFO(0x13d3, 0xff, 0x01, 0x01),
155 .driver_info = BTUSB_BCM_PATCHRAM },
Jurgen Kramer9113bfd2014-02-15 12:01:09 +0100156
Wen-chien Jesse Sungfdfddc62017-01-10 15:41:13 +0800157 /* Dell Computer - Broadcom based */
158 { USB_VENDOR_AND_INTERFACE_INFO(0x413c, 0xff, 0x01, 0x01),
159 .driver_info = BTUSB_BCM_PATCHRAM },
160
Dmitry Tunin1623d0b2015-12-05 14:09:36 +0300161 /* Toshiba Corp - Broadcom based */
162 { USB_VENDOR_AND_INTERFACE_INFO(0x0930, 0xff, 0x01, 0x01),
163 .driver_info = BTUSB_BCM_PATCHRAM },
164
Marcel Holtmann40df7832014-07-06 13:29:58 +0200165 /* Intel Bluetooth USB Bootloader (RAM module) */
Marcel Holtmannd92f2df2014-07-06 14:53:55 +0200166 { USB_DEVICE(0x8087, 0x0a5a),
167 .driver_info = BTUSB_INTEL_BOOT | BTUSB_BROKEN_ISOC },
Marcel Holtmann40df7832014-07-06 13:29:58 +0200168
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200169 { } /* Terminating entry */
170};
171
172MODULE_DEVICE_TABLE(usb, btusb_table);
173
Marcel Holtmann54265202013-10-11 07:46:18 -0700174static const struct usb_device_id blacklist_table[] = {
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200175 /* CSR BlueCore devices */
176 { USB_DEVICE(0x0a12, 0x0001), .driver_info = BTUSB_CSR },
177
178 /* Broadcom BCM2033 without firmware */
179 { USB_DEVICE(0x0a5c, 0x2033), .driver_info = BTUSB_IGNORE },
180
Marcel Holtmann6c9d4352015-10-17 14:39:27 +0200181 /* Broadcom BCM2045 devices */
182 { USB_DEVICE(0x0a5c, 0x2045), .driver_info = BTUSB_BCM2045 },
183
Bala Shanmugambe931122010-11-26 17:35:46 +0530184 /* Atheros 3011 with sflash firmware */
Andy Shevchenko0b880062014-02-18 18:26:19 +0200185 { USB_DEVICE(0x0489, 0xe027), .driver_info = BTUSB_IGNORE },
186 { USB_DEVICE(0x0489, 0xe03d), .driver_info = BTUSB_IGNORE },
Alexander Ploumistos2eeff0b2015-02-13 21:05:11 +0200187 { USB_DEVICE(0x04f2, 0xaff1), .driver_info = BTUSB_IGNORE },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200188 { USB_DEVICE(0x0930, 0x0215), .driver_info = BTUSB_IGNORE },
Bala Shanmugambe931122010-11-26 17:35:46 +0530189 { USB_DEVICE(0x0cf3, 0x3002), .driver_info = BTUSB_IGNORE },
Marek Vasut6eda5412012-06-08 14:32:50 +0200190 { USB_DEVICE(0x0cf3, 0xe019), .driver_info = BTUSB_IGNORE },
Andy Ross2a7bccc2011-05-09 16:11:16 -0700191 { USB_DEVICE(0x13d3, 0x3304), .driver_info = BTUSB_IGNORE },
Bala Shanmugambe931122010-11-26 17:35:46 +0530192
Cho, Yu-Chen509e7862011-01-26 17:10:59 +0800193 /* Atheros AR9285 Malbec with sflash firmware */
194 { USB_DEVICE(0x03f0, 0x311d), .driver_info = BTUSB_IGNORE },
195
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530196 /* Atheros 3012 with sflash firmware */
Andy Shevchenko0b880062014-02-18 18:26:19 +0200197 { USB_DEVICE(0x0489, 0xe04d), .driver_info = BTUSB_ATH3012 },
198 { USB_DEVICE(0x0489, 0xe04e), .driver_info = BTUSB_ATH3012 },
199 { USB_DEVICE(0x0489, 0xe056), .driver_info = BTUSB_ATH3012 },
200 { USB_DEVICE(0x0489, 0xe057), .driver_info = BTUSB_ATH3012 },
201 { USB_DEVICE(0x0489, 0xe05f), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin692c0622015-06-06 20:25:40 +0300202 { USB_DEVICE(0x0489, 0xe076), .driver_info = BTUSB_ATH3012 },
Anantha Krishnan4b552bc2014-10-06 16:31:49 +0530203 { USB_DEVICE(0x0489, 0xe078), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin28c971d2016-02-10 00:49:11 +0300204 { USB_DEVICE(0x0489, 0xe095), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200205 { USB_DEVICE(0x04c5, 0x1330), .driver_info = BTUSB_ATH3012 },
206 { USB_DEVICE(0x04ca, 0x3004), .driver_info = BTUSB_ATH3012 },
207 { USB_DEVICE(0x04ca, 0x3005), .driver_info = BTUSB_ATH3012 },
208 { USB_DEVICE(0x04ca, 0x3006), .driver_info = BTUSB_ATH3012 },
Mohammed Habibulla1fb4e092014-04-17 11:37:13 -0700209 { USB_DEVICE(0x04ca, 0x3007), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200210 { USB_DEVICE(0x04ca, 0x3008), .driver_info = BTUSB_ATH3012 },
211 { USB_DEVICE(0x04ca, 0x300b), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin7e730c7f2015-06-18 20:41:51 +0300212 { USB_DEVICE(0x04ca, 0x300d), .driver_info = BTUSB_ATH3012 },
Dmitry Tuninec0810d2015-05-02 13:36:58 +0300213 { USB_DEVICE(0x04ca, 0x300f), .driver_info = BTUSB_ATH3012 },
Janne Heikkinen134d3b32014-12-09 07:44:51 +0200214 { USB_DEVICE(0x04ca, 0x3010), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin81d90442016-02-28 11:04:06 +0300215 { USB_DEVICE(0x04ca, 0x3014), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin441ad622017-01-05 13:19:53 +0300216 { USB_DEVICE(0x04ca, 0x3018), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200217 { USB_DEVICE(0x0930, 0x0219), .driver_info = BTUSB_ATH3012 },
Dmitry Tunincd355ff2015-10-05 19:29:33 +0300218 { USB_DEVICE(0x0930, 0x021c), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200219 { USB_DEVICE(0x0930, 0x0220), .driver_info = BTUSB_ATH3012 },
Vincent Zwanenburg89d29752014-08-08 12:33:56 +0100220 { USB_DEVICE(0x0930, 0x0227), .driver_info = BTUSB_ATH3012 },
Andy Shevchenkoa735f9e2014-02-18 18:26:20 +0200221 { USB_DEVICE(0x0b05, 0x17d0), .driver_info = BTUSB_ATH3012 },
Ming Leid66629c2013-03-15 11:00:39 +0800222 { USB_DEVICE(0x0cf3, 0x0036), .driver_info = BTUSB_ATH3012 },
Hans de Goede544a5912018-04-26 14:18:19 +0200223 { USB_DEVICE(0x0cf3, 0x3004), .driver_info = BTUSB_ATH3012 },
Sunguk Lee94a32d102013-03-12 04:41:58 +0900224 { USB_DEVICE(0x0cf3, 0x3008), .driver_info = BTUSB_ATH3012 },
Cho, Yu-Chen07c0ea82012-03-14 22:01:21 +0200225 { USB_DEVICE(0x0cf3, 0x311d), .driver_info = BTUSB_ATH3012 },
Oliver Neukumb1312372014-01-16 15:37:11 +0100226 { USB_DEVICE(0x0cf3, 0x311e), .driver_info = BTUSB_ATH3012 },
Oliver Neukum1e56f1e2014-01-16 16:02:58 +0100227 { USB_DEVICE(0x0cf3, 0x311f), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200228 { USB_DEVICE(0x0cf3, 0x3121), .driver_info = BTUSB_ATH3012 },
Ming Leiebaf5792013-03-18 23:45:11 +0800229 { USB_DEVICE(0x0cf3, 0x817a), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin18e0afa2015-10-16 11:45:26 +0300230 { USB_DEVICE(0x0cf3, 0x817b), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200231 { USB_DEVICE(0x0cf3, 0xe003), .driver_info = BTUSB_ATH3012 },
AceLan Kaoac713112012-04-19 14:53:45 +0800232 { USB_DEVICE(0x0cf3, 0xe004), .driver_info = BTUSB_ATH3012 },
Peng Chen0a3658c2013-08-30 17:41:40 +0800233 { USB_DEVICE(0x0cf3, 0xe005), .driver_info = BTUSB_ATH3012 },
Wen-chien Jesse Sungca79f232015-05-13 11:39:24 +0800234 { USB_DEVICE(0x0cf3, 0xe006), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200235 { USB_DEVICE(0x13d3, 0x3362), .driver_info = BTUSB_ATH3012 },
236 { USB_DEVICE(0x13d3, 0x3375), .driver_info = BTUSB_ATH3012 },
AceLan Kaoeed307e2012-12-11 11:41:20 +0800237 { USB_DEVICE(0x13d3, 0x3393), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin609574e2016-02-10 15:33:17 +0300238 { USB_DEVICE(0x13d3, 0x3395), .driver_info = BTUSB_ATH3012 },
Sujith Manoharan5b77a1f2013-07-15 09:29:03 +0530239 { USB_DEVICE(0x13d3, 0x3402), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin3bb30a7c2014-11-25 20:19:52 +0300240 { USB_DEVICE(0x13d3, 0x3408), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin033efa92015-01-18 00:16:51 +0300241 { USB_DEVICE(0x13d3, 0x3423), .driver_info = BTUSB_ATH3012 },
Anantha Krishnanfa2f1392014-07-08 19:25:08 +0530242 { USB_DEVICE(0x13d3, 0x3432), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin75c6aca2016-03-04 01:32:19 +0300243 { USB_DEVICE(0x13d3, 0x3472), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin0d0cef62015-06-06 20:29:25 +0300244 { USB_DEVICE(0x13d3, 0x3474), .driver_info = BTUSB_ATH3012 },
Lauro Costa72f9f8b2016-05-09 17:36:11 -0300245 { USB_DEVICE(0x13d3, 0x3487), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin12d86892016-07-12 01:35:18 +0300246 { USB_DEVICE(0x13d3, 0x3490), .driver_info = BTUSB_ATH3012 },
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530247
Cho, Yu-Chene9036e32011-02-15 10:20:07 +0800248 /* Atheros AR5BBU12 with sflash firmware */
249 { USB_DEVICE(0x0489, 0xe02c), .driver_info = BTUSB_IGNORE },
250
Michael Gruetzner85d59722012-05-02 22:33:40 +0200251 /* Atheros AR5BBU12 with sflash firmware */
Yevgeniy Melnichukbc21fde2012-08-07 19:48:10 +0530252 { USB_DEVICE(0x0489, 0xe036), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200253 { USB_DEVICE(0x0489, 0xe03c), .driver_info = BTUSB_ATH3012 },
Michael Gruetzner85d59722012-05-02 22:33:40 +0200254
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +0000255 /* QCA ROME chipset */
Rocky Liaoe99f38b2020-09-17 21:09:23 +0800256 { USB_DEVICE(0x0cf3, 0x535b), .driver_info = BTUSB_QCA_ROME |
257 BTUSB_WIDEBAND_SPEECH },
258 { USB_DEVICE(0x0cf3, 0xe007), .driver_info = BTUSB_QCA_ROME |
259 BTUSB_WIDEBAND_SPEECH },
260 { USB_DEVICE(0x0cf3, 0xe009), .driver_info = BTUSB_QCA_ROME |
261 BTUSB_WIDEBAND_SPEECH },
262 { USB_DEVICE(0x0cf3, 0xe010), .driver_info = BTUSB_QCA_ROME |
263 BTUSB_WIDEBAND_SPEECH },
264 { USB_DEVICE(0x0cf3, 0xe300), .driver_info = BTUSB_QCA_ROME |
265 BTUSB_WIDEBAND_SPEECH },
266 { USB_DEVICE(0x0cf3, 0xe301), .driver_info = BTUSB_QCA_ROME |
267 BTUSB_WIDEBAND_SPEECH },
268 { USB_DEVICE(0x0cf3, 0xe360), .driver_info = BTUSB_QCA_ROME |
269 BTUSB_WIDEBAND_SPEECH },
Daniel Lenski0324d192021-05-18 10:25:46 -0700270 { USB_DEVICE(0x0cf3, 0xe500), .driver_info = BTUSB_QCA_ROME |
271 BTUSB_WIDEBAND_SPEECH },
Rocky Liaoe99f38b2020-09-17 21:09:23 +0800272 { USB_DEVICE(0x0489, 0xe092), .driver_info = BTUSB_QCA_ROME |
273 BTUSB_WIDEBAND_SPEECH },
274 { USB_DEVICE(0x0489, 0xe09f), .driver_info = BTUSB_QCA_ROME |
275 BTUSB_WIDEBAND_SPEECH },
276 { USB_DEVICE(0x0489, 0xe0a2), .driver_info = BTUSB_QCA_ROME |
277 BTUSB_WIDEBAND_SPEECH },
278 { USB_DEVICE(0x04ca, 0x3011), .driver_info = BTUSB_QCA_ROME |
279 BTUSB_WIDEBAND_SPEECH },
280 { USB_DEVICE(0x04ca, 0x3015), .driver_info = BTUSB_QCA_ROME |
281 BTUSB_WIDEBAND_SPEECH },
282 { USB_DEVICE(0x04ca, 0x3016), .driver_info = BTUSB_QCA_ROME |
283 BTUSB_WIDEBAND_SPEECH },
284 { USB_DEVICE(0x04ca, 0x301a), .driver_info = BTUSB_QCA_ROME |
285 BTUSB_WIDEBAND_SPEECH },
286 { USB_DEVICE(0x04ca, 0x3021), .driver_info = BTUSB_QCA_ROME |
287 BTUSB_WIDEBAND_SPEECH },
288 { USB_DEVICE(0x13d3, 0x3491), .driver_info = BTUSB_QCA_ROME |
289 BTUSB_WIDEBAND_SPEECH },
290 { USB_DEVICE(0x13d3, 0x3496), .driver_info = BTUSB_QCA_ROME |
291 BTUSB_WIDEBAND_SPEECH },
292 { USB_DEVICE(0x13d3, 0x3501), .driver_info = BTUSB_QCA_ROME |
293 BTUSB_WIDEBAND_SPEECH },
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +0000294
Rocky Liaob40f58b2020-09-29 12:23:51 +0800295 /* QCA WCN6855 chipset */
296 { USB_DEVICE(0x0cf3, 0xe600), .driver_info = BTUSB_QCA_WCN6855 |
Michael Sunacd5aea2021-07-23 17:17:31 -0700297 BTUSB_WIDEBAND_SPEECH |
298 BTUSB_VALID_LE_STATES },
Rocky Liaob40f58b2020-09-29 12:23:51 +0800299
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200300 /* Broadcom BCM2035 */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100301 { USB_DEVICE(0x0a5c, 0x2009), .driver_info = BTUSB_BCM92035 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200302 { USB_DEVICE(0x0a5c, 0x200a), .driver_info = BTUSB_WRONG_SCO_MTU },
303 { USB_DEVICE(0x0a5c, 0x2035), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200304
305 /* Broadcom BCM2045 */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100306 { USB_DEVICE(0x0a5c, 0x2039), .driver_info = BTUSB_WRONG_SCO_MTU },
307 { USB_DEVICE(0x0a5c, 0x2101), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmannbdbef3d2008-09-23 00:16:35 +0200308
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200309 /* IBM/Lenovo ThinkPad with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100310 { USB_DEVICE(0x0a5c, 0x201e), .driver_info = BTUSB_WRONG_SCO_MTU },
311 { USB_DEVICE(0x0a5c, 0x2110), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200312
313 /* HP laptop with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100314 { USB_DEVICE(0x03f0, 0x171d), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200315
316 /* Dell laptop with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100317 { USB_DEVICE(0x413c, 0x8126), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200318
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100319 /* Dell Wireless 370 and 410 devices */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100320 { USB_DEVICE(0x413c, 0x8152), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100321 { USB_DEVICE(0x413c, 0x8156), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200322
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100323 /* Belkin F8T012 and F8T013 devices */
324 { USB_DEVICE(0x050d, 0x0012), .driver_info = BTUSB_WRONG_SCO_MTU },
325 { USB_DEVICE(0x050d, 0x0013), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200326
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100327 /* Asus WL-BTD202 device */
328 { USB_DEVICE(0x0b05, 0x1715), .driver_info = BTUSB_WRONG_SCO_MTU },
329
330 /* Kensington Bluetooth USB adapter */
331 { USB_DEVICE(0x047d, 0x105e), .driver_info = BTUSB_WRONG_SCO_MTU },
332
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200333 /* RTX Telecom based adapters with buggy SCO support */
334 { USB_DEVICE(0x0400, 0x0807), .driver_info = BTUSB_BROKEN_ISOC },
335 { USB_DEVICE(0x0400, 0x080a), .driver_info = BTUSB_BROKEN_ISOC },
336
337 /* CONWISE Technology based adapters with buggy SCO support */
Szymon Janc418678b2016-09-01 17:22:37 +0200338 { USB_DEVICE(0x0e5e, 0x6622),
339 .driver_info = BTUSB_BROKEN_ISOC | BTUSB_CW6622},
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200340
Marcel Holtmann4fcef8e2015-01-01 17:34:37 -0800341 /* Roper Class 1 Bluetooth Dongle (Silicon Wave based) */
Aleksei Volkov2eeac872015-06-08 12:02:10 +0300342 { USB_DEVICE(0x1310, 0x0001), .driver_info = BTUSB_SWAVE },
Marcel Holtmann4fcef8e2015-01-01 17:34:37 -0800343
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200344 /* Digianswer devices */
345 { USB_DEVICE(0x08fd, 0x0001), .driver_info = BTUSB_DIGIANSWER },
346 { USB_DEVICE(0x08fd, 0x0002), .driver_info = BTUSB_IGNORE },
347
348 /* CSR BlueCore Bluetooth Sniffer */
Marcel Holtmann4f64fa82014-07-07 00:12:04 +0200349 { USB_DEVICE(0x0a12, 0x0002),
350 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200351
352 /* Frontline ComProbe Bluetooth Sniffer */
Marcel Holtmann4f64fa82014-07-07 00:12:04 +0200353 { USB_DEVICE(0x16d3, 0x0002),
354 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200355
Marcel Holtmanncb1ee892015-01-28 19:41:42 -0800356 /* Marvell Bluetooth devices */
357 { USB_DEVICE(0x1286, 0x2044), .driver_info = BTUSB_MARVELL },
358 { USB_DEVICE(0x1286, 0x2046), .driver_info = BTUSB_MARVELL },
Amitkumar Karwar1165df02016-09-28 16:18:35 +0530359 { USB_DEVICE(0x1286, 0x204e), .driver_info = BTUSB_MARVELL },
Marcel Holtmanncb1ee892015-01-28 19:41:42 -0800360
Marcel Holtmannd0ac9eb2015-01-28 19:41:43 -0800361 /* Intel Bluetooth devices */
Tedd Ho-Jeong An3df4dfb2021-08-04 17:32:16 -0700362 { USB_DEVICE(0x8087, 0x0025), .driver_info = BTUSB_INTEL_COMBINED },
363 { USB_DEVICE(0x8087, 0x0026), .driver_info = BTUSB_INTEL_COMBINED },
364 { USB_DEVICE(0x8087, 0x0029), .driver_info = BTUSB_INTEL_COMBINED },
365 { USB_DEVICE(0x8087, 0x0032), .driver_info = BTUSB_INTEL_COMBINED },
366 { USB_DEVICE(0x8087, 0x0033), .driver_info = BTUSB_INTEL_COMBINED },
Marcel Holtmann407550f2015-02-22 15:41:18 -0800367 { USB_DEVICE(0x8087, 0x07da), .driver_info = BTUSB_CSR },
Tedd Ho-Jeong Anea7c4c02021-08-04 17:32:12 -0700368 { USB_DEVICE(0x8087, 0x07dc), .driver_info = BTUSB_INTEL_COMBINED |
369 BTUSB_INTEL_BROKEN_INITIAL_NCMD },
Tedd Ho-Jeong An83f2daf2021-08-04 17:32:10 -0700370 { USB_DEVICE(0x8087, 0x0a2a), .driver_info = BTUSB_INTEL_COMBINED },
Tedd Ho-Jeong An3df4dfb2021-08-04 17:32:16 -0700371 { USB_DEVICE(0x8087, 0x0a2b), .driver_info = BTUSB_INTEL_COMBINED },
372 { USB_DEVICE(0x8087, 0x0aa7), .driver_info = BTUSB_INTEL_COMBINED },
373 { USB_DEVICE(0x8087, 0x0aaa), .driver_info = BTUSB_INTEL_COMBINED },
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -0700374
Marcel Holtmannd0ac9eb2015-01-28 19:41:43 -0800375 /* Other Intel Bluetooth devices */
376 { USB_VENDOR_AND_INTERFACE_INFO(0x8087, 0xe0, 0x01, 0x01),
377 .driver_info = BTUSB_IGNORE },
Amitkumar Karwarae8df492014-07-18 14:47:06 -0700378
Joseph Hwang33bfd942020-07-13 15:45:29 +0800379 /* Realtek 8822CE Bluetooth devices */
380 { USB_DEVICE(0x0bda, 0xb00c), .driver_info = BTUSB_REALTEK |
381 BTUSB_WIDEBAND_SPEECH },
Larry Finger4d96d3b2021-06-01 14:04:18 -0500382 { USB_DEVICE(0x0bda, 0xc822), .driver_info = BTUSB_REALTEK |
383 BTUSB_WIDEBAND_SPEECH },
Joseph Hwang33bfd942020-07-13 15:45:29 +0800384
Max Chou0d484db2020-11-10 11:38:37 +0800385 /* Realtek 8852AE Bluetooth devices */
386 { USB_DEVICE(0x0bda, 0xc852), .driver_info = BTUSB_REALTEK |
387 BTUSB_WIDEBAND_SPEECH },
Larry Fingerd1dcbf62021-09-28 13:45:20 -0500388 { USB_DEVICE(0x0bda, 0x4852), .driver_info = BTUSB_REALTEK |
389 BTUSB_WIDEBAND_SPEECH },
Max Chou9bba1282021-09-01 15:48:45 +0800390 { USB_DEVICE(0x04c5, 0x165c), .driver_info = BTUSB_REALTEK |
391 BTUSB_WIDEBAND_SPEECH },
Larry Fingerd1dcbf62021-09-28 13:45:20 -0500392 { USB_DEVICE(0x04ca, 0x4006), .driver_info = BTUSB_REALTEK |
393 BTUSB_WIDEBAND_SPEECH },
Max Chou0d484db2020-11-10 11:38:37 +0800394
Daniel Drakea2698a92015-04-16 14:09:55 -0600395 /* Realtek Bluetooth devices */
396 { USB_VENDOR_AND_INTERFACE_INFO(0x0bda, 0xe0, 0x01, 0x01),
397 .driver_info = BTUSB_REALTEK },
398
Sean Wanga1c49c432019-06-02 08:02:48 +0800399 /* MediaTek Bluetooth devices */
400 { USB_VENDOR_AND_INTERFACE_INFO(0x0e8d, 0xe0, 0x01, 0x01),
mark-yw.chen27e554a2021-03-29 01:18:33 +0800401 .driver_info = BTUSB_MEDIATEK |
402 BTUSB_WIDEBAND_SPEECH |
403 BTUSB_VALID_LE_STATES },
Sean Wanga1c49c432019-06-02 08:02:48 +0800404
Chris Chiu3a567b92020-11-10 11:40:10 +0800405 /* Additional MediaTek MT7615E Bluetooth devices */
406 { USB_DEVICE(0x13d3, 0x3560), .driver_info = BTUSB_MEDIATEK},
407
Forest Crossman37356822021-07-12 02:32:20 -0500408 /* Additional MediaTek MT7668 Bluetooth devices */
409 { USB_DEVICE(0x043e, 0x3109), .driver_info = BTUSB_MEDIATEK |
410 BTUSB_WIDEBAND_SPEECH |
411 BTUSB_VALID_LE_STATES },
412
mark-yw.chen393dc5d2021-04-12 23:06:27 +0800413 /* Additional MediaTek MT7921 Bluetooth devices */
414 { USB_DEVICE(0x04ca, 0x3802), .driver_info = BTUSB_MEDIATEK |
415 BTUSB_WIDEBAND_SPEECH |
416 BTUSB_VALID_LE_STATES },
Wai Paulo Valerio Wang2cdff8c2021-07-08 04:00:59 +0800417 { USB_DEVICE(0x13d3, 0x3563), .driver_info = BTUSB_MEDIATEK |
418 BTUSB_WIDEBAND_SPEECH |
419 BTUSB_VALID_LE_STATES },
mark-yw.chen15957ca2021-09-01 19:30:15 +0800420 { USB_DEVICE(0x13d3, 0x3564), .driver_info = BTUSB_MEDIATEK |
421 BTUSB_WIDEBAND_SPEECH |
422 BTUSB_VALID_LE_STATES },
Aaron Ma64832df2021-07-23 01:17:18 +0800423 { USB_DEVICE(0x0489, 0xe0cd), .driver_info = BTUSB_MEDIATEK |
424 BTUSB_WIDEBAND_SPEECH |
425 BTUSB_VALID_LE_STATES },
mark-yw.chen393dc5d2021-04-12 23:06:27 +0800426
Daniel Drakea2698a92015-04-16 14:09:55 -0600427 /* Additional Realtek 8723AE Bluetooth devices */
428 { USB_DEVICE(0x0930, 0x021d), .driver_info = BTUSB_REALTEK },
429 { USB_DEVICE(0x13d3, 0x3394), .driver_info = BTUSB_REALTEK },
430
431 /* Additional Realtek 8723BE Bluetooth devices */
432 { USB_DEVICE(0x0489, 0xe085), .driver_info = BTUSB_REALTEK },
433 { USB_DEVICE(0x0489, 0xe08b), .driver_info = BTUSB_REALTEK },
434 { USB_DEVICE(0x13d3, 0x3410), .driver_info = BTUSB_REALTEK },
435 { USB_DEVICE(0x13d3, 0x3416), .driver_info = BTUSB_REALTEK },
436 { USB_DEVICE(0x13d3, 0x3459), .driver_info = BTUSB_REALTEK },
Dmitry Tunina81d72d2017-08-08 14:09:02 +0300437 { USB_DEVICE(0x13d3, 0x3494), .driver_info = BTUSB_REALTEK },
Daniel Drakea2698a92015-04-16 14:09:55 -0600438
Vicente Bergasa41e0792018-03-20 19:41:10 +0100439 /* Additional Realtek 8723BU Bluetooth devices */
440 { USB_DEVICE(0x7392, 0xa611), .driver_info = BTUSB_REALTEK },
441
Jian-Hong Pan66d99752018-05-21 18:09:20 +0800442 /* Additional Realtek 8723DE Bluetooth devices */
Jian-Hong Pan45ae68b2018-05-25 17:54:52 +0800443 { USB_DEVICE(0x0bda, 0xb009), .driver_info = BTUSB_REALTEK },
Jian-Hong Pan66d99752018-05-21 18:09:20 +0800444 { USB_DEVICE(0x2ff8, 0xb011), .driver_info = BTUSB_REALTEK },
445
Nicholas Flintham4fd6d492021-09-30 09:22:39 +0100446 /* Additional Realtek 8761B Bluetooth devices */
447 { USB_DEVICE(0x2357, 0x0604), .driver_info = BTUSB_REALTEK |
448 BTUSB_WIDEBAND_SPEECH },
449
Joakim Tjernlund33404382021-05-28 17:26:45 +0200450 /* Additional Realtek 8761BU Bluetooth devices */
451 { USB_DEVICE(0x0b05, 0x190e), .driver_info = BTUSB_REALTEK |
452 BTUSB_WIDEBAND_SPEECH },
453
Daniel Drakea2698a92015-04-16 14:09:55 -0600454 /* Additional Realtek 8821AE Bluetooth devices */
455 { USB_DEVICE(0x0b05, 0x17dc), .driver_info = BTUSB_REALTEK },
456 { USB_DEVICE(0x13d3, 0x3414), .driver_info = BTUSB_REALTEK },
457 { USB_DEVICE(0x13d3, 0x3458), .driver_info = BTUSB_REALTEK },
458 { USB_DEVICE(0x13d3, 0x3461), .driver_info = BTUSB_REALTEK },
459 { USB_DEVICE(0x13d3, 0x3462), .driver_info = BTUSB_REALTEK },
460
Larry Fingerfed03fe2018-02-11 12:24:32 -0600461 /* Additional Realtek 8822BE Bluetooth devices */
Artiom Vaskov1cd2fab2018-05-30 11:23:00 +0300462 { USB_DEVICE(0x13d3, 0x3526), .driver_info = BTUSB_REALTEK },
Larry Fingerfed03fe2018-02-11 12:24:32 -0600463 { USB_DEVICE(0x0b05, 0x185c), .driver_info = BTUSB_REALTEK },
464
Jian-Hong Pan6d0762b2019-09-03 17:10:42 +0800465 /* Additional Realtek 8822CE Bluetooth devices */
Max Chou73280f12020-11-05 18:54:48 +0800466 { USB_DEVICE(0x04ca, 0x4005), .driver_info = BTUSB_REALTEK |
467 BTUSB_WIDEBAND_SPEECH },
468 { USB_DEVICE(0x04c5, 0x161f), .driver_info = BTUSB_REALTEK |
469 BTUSB_WIDEBAND_SPEECH },
470 { USB_DEVICE(0x0b05, 0x18ef), .driver_info = BTUSB_REALTEK |
471 BTUSB_WIDEBAND_SPEECH },
472 { USB_DEVICE(0x13d3, 0x3548), .driver_info = BTUSB_REALTEK |
473 BTUSB_WIDEBAND_SPEECH },
474 { USB_DEVICE(0x13d3, 0x3549), .driver_info = BTUSB_REALTEK |
475 BTUSB_WIDEBAND_SPEECH },
476 { USB_DEVICE(0x13d3, 0x3553), .driver_info = BTUSB_REALTEK |
477 BTUSB_WIDEBAND_SPEECH },
478 { USB_DEVICE(0x13d3, 0x3555), .driver_info = BTUSB_REALTEK |
479 BTUSB_WIDEBAND_SPEECH },
480 { USB_DEVICE(0x2ff8, 0x3051), .driver_info = BTUSB_REALTEK |
481 BTUSB_WIDEBAND_SPEECH },
Julian Pidancetf4962972020-11-09 17:47:22 +0100482 { USB_DEVICE(0x1358, 0xc123), .driver_info = BTUSB_REALTEK |
483 BTUSB_WIDEBAND_SPEECH },
Cadel Watson24d6a6d2020-11-19 16:16:25 +1100484 { USB_DEVICE(0x0bda, 0xc123), .driver_info = BTUSB_REALTEK |
485 BTUSB_WIDEBAND_SPEECH },
Rasmus Moorats3edc5782021-02-18 13:11:24 +0200486 { USB_DEVICE(0x0cb5, 0xc547), .driver_info = BTUSB_REALTEK |
487 BTUSB_WIDEBAND_SPEECH },
Jian-Hong Pan6d0762b2019-09-03 17:10:42 +0800488
Peter Poklop4481c072015-08-15 20:47:09 +0200489 /* Silicon Wave based devices */
490 { USB_DEVICE(0x0c10, 0x0000), .driver_info = BTUSB_SWAVE },
491
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200492 { } /* Terminating entry */
493};
494
Hans de Goede1fdb9262018-02-20 09:06:18 +0100495/* The Bluetooth USB module build into some devices needs to be reset on resume,
496 * this is a problem with the platform (likely shutting off all power) not with
497 * the module itself. So we use a DMI list to match known broken platforms.
498 */
499static const struct dmi_system_id btusb_needs_reset_resume_table[] = {
500 {
Kai-Heng Feng0c6e5262018-03-01 13:42:52 +0800501 /* Dell OptiPlex 3060 (QCA ROME device 0cf3:e007) */
Hans de Goede1fdb9262018-02-20 09:06:18 +0100502 .matches = {
Kai-Heng Feng0c6e5262018-03-01 13:42:52 +0800503 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
504 DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 3060"),
Hans de Goede1fdb9262018-02-20 09:06:18 +0100505 },
506 },
Hans de Goede596b07a2018-04-26 20:52:06 +0200507 {
508 /* Dell XPS 9360 (QCA ROME device 0cf3:e300) */
509 .matches = {
510 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
511 DMI_MATCH(DMI_PRODUCT_NAME, "XPS 13 9360"),
512 },
513 },
Hans de Goede939bc6c2018-05-22 09:34:10 +0200514 {
515 /* Dell Inspiron 5565 (QCA ROME device 0cf3:e009) */
516 .matches = {
517 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
518 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5565"),
519 },
520 },
Hans de Goede1fdb9262018-02-20 09:06:18 +0100521 {}
522};
523
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200524#define BTUSB_MAX_ISOC_FRAMES 10
525
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200526#define BTUSB_INTR_RUNNING 0
527#define BTUSB_BULK_RUNNING 1
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200528#define BTUSB_ISOC_RUNNING 2
Oliver Neukum7bee5492009-08-24 23:44:59 +0200529#define BTUSB_SUSPENDING 3
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -0300530#define BTUSB_DID_ISO_RESUME 4
Marcel Holtmanncda0dd72015-01-26 21:33:48 -0800531#define BTUSB_BOOTLOADER 5
532#define BTUSB_DOWNLOADING 6
Marcel Holtmannce6bb922015-01-28 01:58:40 -0800533#define BTUSB_FIRMWARE_LOADED 7
Marcel Holtmanncda0dd72015-01-26 21:33:48 -0800534#define BTUSB_FIRMWARE_FAILED 8
Marcel Holtmannce6bb922015-01-28 01:58:40 -0800535#define BTUSB_BOOTING 9
Hans de Goede61f5ace2018-01-08 10:44:16 +0100536#define BTUSB_DIAG_RUNNING 10
537#define BTUSB_OOB_WAKE_ENABLED 11
Rajat Jaindc786b22019-01-24 15:28:14 -0800538#define BTUSB_HW_RESET_ACTIVE 12
Sean Wanga1c49c432019-06-02 08:02:48 +0800539#define BTUSB_TX_WAIT_VND_EVT 13
Max Chou8274db02021-08-17 11:03:12 +0800540#define BTUSB_WAKEUP_AUTOSUSPEND 14
Pauli Virtanen55981d32021-07-26 21:02:06 +0300541#define BTUSB_USE_ALT3_FOR_WBS 15
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200542
543struct btusb_data {
544 struct hci_dev *hdev;
545 struct usb_device *udev;
Marcel Holtmann5fbcd262008-09-23 00:16:36 +0200546 struct usb_interface *intf;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200547 struct usb_interface *isoc;
Marcel Holtmann9d08f502015-10-08 20:23:08 +0200548 struct usb_interface *diag;
Marcel Holtmann459232f2017-10-24 19:42:45 +0200549 unsigned isoc_ifnum;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200550
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200551 unsigned long flags;
552
553 struct work_struct work;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200554 struct work_struct waker;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200555
Marcel Holtmann803b5832014-09-16 08:00:29 +0200556 struct usb_anchor deferred;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200557 struct usb_anchor tx_anchor;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200558 int tx_in_flight;
559 spinlock_t txlock;
560
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200561 struct usb_anchor intr_anchor;
562 struct usb_anchor bulk_anchor;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200563 struct usb_anchor isoc_anchor;
Marcel Holtmann9d08f502015-10-08 20:23:08 +0200564 struct usb_anchor diag_anchor;
Sean Wanga1c49c432019-06-02 08:02:48 +0800565 struct usb_anchor ctrl_anchor;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200566 spinlock_t rxlock;
567
568 struct sk_buff *evt_skb;
569 struct sk_buff *acl_skb;
570 struct sk_buff *sco_skb;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200571
572 struct usb_endpoint_descriptor *intr_ep;
573 struct usb_endpoint_descriptor *bulk_tx_ep;
574 struct usb_endpoint_descriptor *bulk_rx_ep;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200575 struct usb_endpoint_descriptor *isoc_tx_ep;
576 struct usb_endpoint_descriptor *isoc_rx_ep;
Marcel Holtmann9d08f502015-10-08 20:23:08 +0200577 struct usb_endpoint_descriptor *diag_tx_ep;
578 struct usb_endpoint_descriptor *diag_rx_ep;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200579
Rajat Jaindc786b22019-01-24 15:28:14 -0800580 struct gpio_desc *reset_gpio;
581
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100582 __u8 cmdreq_type;
Marcel Holtmann893ba542015-01-28 20:27:34 -0800583 __u8 cmdreq;
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100584
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100585 unsigned int sco_num;
Sathish Narasimmanbaac6272020-04-03 21:43:59 +0200586 unsigned int air_mode;
587 bool usb_alt6_packet_flow;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200588 int isoc_altsetting;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +0100589 int suspend_count;
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +0100590
Marcel Holtmann97307f52015-01-12 13:51:10 -0800591 int (*recv_event)(struct hci_dev *hdev, struct sk_buff *skb);
mark-yw.chen0b10c8c2021-08-04 17:03:15 +0800592 int (*recv_acl)(struct hci_dev *hdev, struct sk_buff *skb);
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +0100593 int (*recv_bulk)(struct btusb_data *data, void *buffer, int count);
Kim, Ben Young Taeace31982015-02-15 23:06:14 +0000594
595 int (*setup_on_usb)(struct hci_dev *hdev);
Rajat Jainfd913ef2017-02-01 14:24:09 -0800596
597 int oob_wake_irq; /* irq for out-of-band wake-on-bt */
Rajat Jaindc786b22019-01-24 15:28:14 -0800598 unsigned cmd_timeout_cnt;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200599};
600
Rajat Jaindc786b22019-01-24 15:28:14 -0800601static void btusb_intel_cmd_timeout(struct hci_dev *hdev)
602{
603 struct btusb_data *data = hci_get_drvdata(hdev);
604 struct gpio_desc *reset_gpio = data->reset_gpio;
605
606 if (++data->cmd_timeout_cnt < 5)
607 return;
608
609 if (!reset_gpio) {
610 bt_dev_err(hdev, "No way to reset. Ignoring and continuing");
611 return;
612 }
613
614 /*
615 * Toggle the hard reset line if the platform provides one. The reset
616 * is going to yank the device off the USB and then replug. So doing
617 * once is enough. The cleanup is handled correctly on the way out
618 * (standard USB disconnect), and the new device is detected cleanly
619 * and bound to the driver again like it should be.
620 */
621 if (test_and_set_bit(BTUSB_HW_RESET_ACTIVE, &data->flags)) {
622 bt_dev_err(hdev, "last reset failed? Not resetting again");
623 return;
624 }
625
626 bt_dev_err(hdev, "Initiating HW reset via gpio");
Rajat Jain2de66bb82019-01-28 15:08:09 -0800627 gpiod_set_value_cansleep(reset_gpio, 1);
628 msleep(100);
629 gpiod_set_value_cansleep(reset_gpio, 0);
Rajat Jaindc786b22019-01-24 15:28:14 -0800630}
631
Alex Lud7ef0d12019-09-05 10:36:31 +0800632static void btusb_rtl_cmd_timeout(struct hci_dev *hdev)
633{
634 struct btusb_data *data = hci_get_drvdata(hdev);
635 struct gpio_desc *reset_gpio = data->reset_gpio;
636
637 if (++data->cmd_timeout_cnt < 5)
638 return;
639
640 if (!reset_gpio) {
641 bt_dev_err(hdev, "No gpio to reset Realtek device, ignoring");
642 return;
643 }
644
645 /* Toggle the hard reset line. The Realtek device is going to
646 * yank itself off the USB and then replug. The cleanup is handled
647 * correctly on the way out (standard USB disconnect), and the new
648 * device is detected cleanly and bound to the driver again like
649 * it should be.
650 */
651 if (test_and_set_bit(BTUSB_HW_RESET_ACTIVE, &data->flags)) {
652 bt_dev_err(hdev, "last reset failed? Not resetting again");
653 return;
654 }
655
656 bt_dev_err(hdev, "Reset Realtek device via gpio");
Alex Lud7ef0d12019-09-05 10:36:31 +0800657 gpiod_set_value_cansleep(reset_gpio, 1);
Max Chou34682112019-11-27 11:01:07 +0800658 msleep(200);
659 gpiod_set_value_cansleep(reset_gpio, 0);
Alex Lud7ef0d12019-09-05 10:36:31 +0800660}
661
Abhishek Pandit-Subedi69335682020-06-24 11:11:44 -0700662static void btusb_qca_cmd_timeout(struct hci_dev *hdev)
663{
664 struct btusb_data *data = hci_get_drvdata(hdev);
tjiang@codeaurora.org24ff62a2021-09-16 16:41:06 +0800665 struct gpio_desc *reset_gpio = data->reset_gpio;
Abhishek Pandit-Subedi69335682020-06-24 11:11:44 -0700666 int err;
667
668 if (++data->cmd_timeout_cnt < 5)
669 return;
670
tjiang@codeaurora.org24ff62a2021-09-16 16:41:06 +0800671 if (reset_gpio) {
672 bt_dev_err(hdev, "Reset qca device via bt_en gpio");
673
674 /* Toggle the hard reset line. The qca bt device is going to
675 * yank itself off the USB and then replug. The cleanup is handled
676 * correctly on the way out (standard USB disconnect), and the new
677 * device is detected cleanly and bound to the driver again like
678 * it should be.
679 */
680 if (test_and_set_bit(BTUSB_HW_RESET_ACTIVE, &data->flags)) {
681 bt_dev_err(hdev, "last reset failed? Not resetting again");
682 return;
683 }
684
685 gpiod_set_value_cansleep(reset_gpio, 0);
686 msleep(200);
687 gpiod_set_value_cansleep(reset_gpio, 1);
688
689 return;
690 }
691
Abhishek Pandit-Subedi69335682020-06-24 11:11:44 -0700692 bt_dev_err(hdev, "Multiple cmd timeouts seen. Resetting usb device.");
Abhishek Pandit-Subedib980d472020-06-25 16:26:27 -0700693 /* This is not an unbalanced PM reference since the device will reset */
Abhishek Pandit-Subedi69335682020-06-24 11:11:44 -0700694 err = usb_autopm_get_interface(data->intf);
695 if (!err)
696 usb_queue_reset_device(data->intf);
697 else
698 bt_dev_err(hdev, "Failed usb_autopm_get_interface with %d", err);
699}
700
Marcel Holtmann803b5832014-09-16 08:00:29 +0200701static inline void btusb_free_frags(struct btusb_data *data)
702{
703 unsigned long flags;
704
705 spin_lock_irqsave(&data->rxlock, flags);
706
707 kfree_skb(data->evt_skb);
708 data->evt_skb = NULL;
709
710 kfree_skb(data->acl_skb);
711 data->acl_skb = NULL;
712
713 kfree_skb(data->sco_skb);
714 data->sco_skb = NULL;
715
716 spin_unlock_irqrestore(&data->rxlock, flags);
717}
718
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200719static int btusb_recv_intr(struct btusb_data *data, void *buffer, int count)
720{
Marcel Holtmann803b5832014-09-16 08:00:29 +0200721 struct sk_buff *skb;
Sebastian Andrzej Siewiora5e50d52018-06-19 23:56:57 +0200722 unsigned long flags;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200723 int err = 0;
724
Sebastian Andrzej Siewiora5e50d52018-06-19 23:56:57 +0200725 spin_lock_irqsave(&data->rxlock, flags);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200726 skb = data->evt_skb;
727
728 while (count) {
729 int len;
730
731 if (!skb) {
732 skb = bt_skb_alloc(HCI_MAX_EVENT_SIZE, GFP_ATOMIC);
733 if (!skb) {
734 err = -ENOMEM;
735 break;
736 }
737
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100738 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
739 hci_skb_expect(skb) = HCI_EVENT_HDR_SIZE;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200740 }
741
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100742 len = min_t(uint, hci_skb_expect(skb), count);
Johannes Berg59ae1d12017-06-16 14:29:20 +0200743 skb_put_data(skb, buffer, len);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200744
745 count -= len;
746 buffer += len;
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100747 hci_skb_expect(skb) -= len;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200748
749 if (skb->len == HCI_EVENT_HDR_SIZE) {
750 /* Complete event header */
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100751 hci_skb_expect(skb) = hci_event_hdr(skb)->plen;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200752
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100753 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
Marcel Holtmann803b5832014-09-16 08:00:29 +0200754 kfree_skb(skb);
755 skb = NULL;
756
757 err = -EILSEQ;
758 break;
759 }
760 }
761
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100762 if (!hci_skb_expect(skb)) {
Marcel Holtmann803b5832014-09-16 08:00:29 +0200763 /* Complete frame */
Marcel Holtmann97307f52015-01-12 13:51:10 -0800764 data->recv_event(data->hdev, skb);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200765 skb = NULL;
766 }
767 }
768
769 data->evt_skb = skb;
Sebastian Andrzej Siewiora5e50d52018-06-19 23:56:57 +0200770 spin_unlock_irqrestore(&data->rxlock, flags);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200771
772 return err;
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200773}
774
775static int btusb_recv_bulk(struct btusb_data *data, void *buffer, int count)
776{
Marcel Holtmann803b5832014-09-16 08:00:29 +0200777 struct sk_buff *skb;
Sebastian Andrzej Siewiora5e50d52018-06-19 23:56:57 +0200778 unsigned long flags;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200779 int err = 0;
780
Sebastian Andrzej Siewiora5e50d52018-06-19 23:56:57 +0200781 spin_lock_irqsave(&data->rxlock, flags);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200782 skb = data->acl_skb;
783
784 while (count) {
785 int len;
786
787 if (!skb) {
788 skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
789 if (!skb) {
790 err = -ENOMEM;
791 break;
792 }
793
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100794 hci_skb_pkt_type(skb) = HCI_ACLDATA_PKT;
795 hci_skb_expect(skb) = HCI_ACL_HDR_SIZE;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200796 }
797
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100798 len = min_t(uint, hci_skb_expect(skb), count);
Johannes Berg59ae1d12017-06-16 14:29:20 +0200799 skb_put_data(skb, buffer, len);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200800
801 count -= len;
802 buffer += len;
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100803 hci_skb_expect(skb) -= len;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200804
805 if (skb->len == HCI_ACL_HDR_SIZE) {
806 __le16 dlen = hci_acl_hdr(skb)->dlen;
807
808 /* Complete ACL header */
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100809 hci_skb_expect(skb) = __le16_to_cpu(dlen);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200810
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100811 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
Marcel Holtmann803b5832014-09-16 08:00:29 +0200812 kfree_skb(skb);
813 skb = NULL;
814
815 err = -EILSEQ;
816 break;
817 }
818 }
819
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100820 if (!hci_skb_expect(skb)) {
Marcel Holtmann803b5832014-09-16 08:00:29 +0200821 /* Complete frame */
mark-yw.chen0b10c8c2021-08-04 17:03:15 +0800822 data->recv_acl(data->hdev, skb);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200823 skb = NULL;
824 }
825 }
826
827 data->acl_skb = skb;
Sebastian Andrzej Siewiora5e50d52018-06-19 23:56:57 +0200828 spin_unlock_irqrestore(&data->rxlock, flags);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200829
830 return err;
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200831}
832
833static int btusb_recv_isoc(struct btusb_data *data, void *buffer, int count)
834{
Marcel Holtmann803b5832014-09-16 08:00:29 +0200835 struct sk_buff *skb;
Sebastian Andrzej Siewiora5e50d52018-06-19 23:56:57 +0200836 unsigned long flags;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200837 int err = 0;
838
Sebastian Andrzej Siewiora5e50d52018-06-19 23:56:57 +0200839 spin_lock_irqsave(&data->rxlock, flags);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200840 skb = data->sco_skb;
841
842 while (count) {
843 int len;
844
845 if (!skb) {
846 skb = bt_skb_alloc(HCI_MAX_SCO_SIZE, GFP_ATOMIC);
847 if (!skb) {
848 err = -ENOMEM;
849 break;
850 }
851
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100852 hci_skb_pkt_type(skb) = HCI_SCODATA_PKT;
853 hci_skb_expect(skb) = HCI_SCO_HDR_SIZE;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200854 }
855
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100856 len = min_t(uint, hci_skb_expect(skb), count);
Johannes Berg59ae1d12017-06-16 14:29:20 +0200857 skb_put_data(skb, buffer, len);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200858
859 count -= len;
860 buffer += len;
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100861 hci_skb_expect(skb) -= len;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200862
863 if (skb->len == HCI_SCO_HDR_SIZE) {
864 /* Complete SCO header */
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100865 hci_skb_expect(skb) = hci_sco_hdr(skb)->dlen;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200866
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100867 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
Marcel Holtmann803b5832014-09-16 08:00:29 +0200868 kfree_skb(skb);
869 skb = NULL;
870
871 err = -EILSEQ;
872 break;
873 }
874 }
875
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100876 if (!hci_skb_expect(skb)) {
Marcel Holtmann803b5832014-09-16 08:00:29 +0200877 /* Complete frame */
878 hci_recv_frame(data->hdev, skb);
879 skb = NULL;
880 }
881 }
882
883 data->sco_skb = skb;
Sebastian Andrzej Siewiora5e50d52018-06-19 23:56:57 +0200884 spin_unlock_irqrestore(&data->rxlock, flags);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200885
886 return err;
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200887}
888
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200889static void btusb_intr_complete(struct urb *urb)
890{
891 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +0100892 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200893 int err;
894
Marcel Holtmann89e75332014-09-16 04:44:50 +0200895 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
896 urb->actual_length);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200897
898 if (!test_bit(HCI_RUNNING, &hdev->flags))
899 return;
900
901 if (urb->status == 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200902 hdev->stat.byte_rx += urb->actual_length;
903
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200904 if (btusb_recv_intr(data, urb->transfer_buffer,
905 urb->actual_length) < 0) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +0100906 bt_dev_err(hdev, "corrupted event packet");
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200907 hdev->stat.err_rx++;
908 }
Champion Chen85560c42014-09-06 14:06:08 -0500909 } else if (urb->status == -ENOENT) {
910 /* Avoid suspend failed when usb_kill_urb */
911 return;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200912 }
913
914 if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
915 return;
916
Oliver Neukum7bee5492009-08-24 23:44:59 +0200917 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200918 usb_anchor_urb(urb, &data->intr_anchor);
919
920 err = usb_submit_urb(urb, GFP_ATOMIC);
921 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200922 /* -EPERM: urb is being killed;
Derek Robsond98422c2017-07-22 13:47:07 +1200923 * -ENODEV: device got disconnected
924 */
Paul Bolle4935f1c2011-08-09 17:16:28 +0200925 if (err != -EPERM && err != -ENODEV)
Marcel Holtmann2064ee32017-10-30 10:42:59 +0100926 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
927 urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200928 usb_unanchor_urb(urb);
929 }
930}
931
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100932static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200933{
David Herrmann155961e2012-02-09 21:58:32 +0100934 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200935 struct urb *urb;
936 unsigned char *buf;
937 unsigned int pipe;
938 int err, size;
939
940 BT_DBG("%s", hdev->name);
941
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200942 if (!data->intr_ep)
943 return -ENODEV;
944
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100945 urb = usb_alloc_urb(0, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200946 if (!urb)
947 return -ENOMEM;
948
949 size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
950
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100951 buf = kmalloc(size, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200952 if (!buf) {
953 usb_free_urb(urb);
954 return -ENOMEM;
955 }
956
957 pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
958
959 usb_fill_int_urb(urb, data->udev, pipe, buf, size,
Marcel Holtmann89e75332014-09-16 04:44:50 +0200960 btusb_intr_complete, hdev, data->intr_ep->bInterval);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200961
962 urb->transfer_flags |= URB_FREE_BUFFER;
963
964 usb_anchor_urb(urb, &data->intr_anchor);
965
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100966 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200967 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +0200968 if (err != -EPERM && err != -ENODEV)
Marcel Holtmann2064ee32017-10-30 10:42:59 +0100969 bt_dev_err(hdev, "urb %p submission failed (%d)",
970 urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200971 usb_unanchor_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200972 }
973
974 usb_free_urb(urb);
975
976 return err;
977}
978
979static void btusb_bulk_complete(struct urb *urb)
980{
981 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +0100982 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200983 int err;
984
Marcel Holtmann89e75332014-09-16 04:44:50 +0200985 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
986 urb->actual_length);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200987
988 if (!test_bit(HCI_RUNNING, &hdev->flags))
989 return;
990
991 if (urb->status == 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200992 hdev->stat.byte_rx += urb->actual_length;
993
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +0100994 if (data->recv_bulk(data, urb->transfer_buffer,
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200995 urb->actual_length) < 0) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +0100996 bt_dev_err(hdev, "corrupted ACL packet");
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200997 hdev->stat.err_rx++;
998 }
Champion Chen85560c42014-09-06 14:06:08 -0500999 } else if (urb->status == -ENOENT) {
1000 /* Avoid suspend failed when usb_kill_urb */
1001 return;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001002 }
1003
1004 if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
1005 return;
1006
1007 usb_anchor_urb(urb, &data->bulk_anchor);
Oliver Neukum652fd782009-12-16 19:23:43 +01001008 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001009
1010 err = usb_submit_urb(urb, GFP_ATOMIC);
1011 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +02001012 /* -EPERM: urb is being killed;
Derek Robsond98422c2017-07-22 13:47:07 +12001013 * -ENODEV: device got disconnected
1014 */
Paul Bolle4935f1c2011-08-09 17:16:28 +02001015 if (err != -EPERM && err != -ENODEV)
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001016 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
1017 urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001018 usb_unanchor_urb(urb);
1019 }
1020}
1021
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001022static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001023{
David Herrmann155961e2012-02-09 21:58:32 +01001024 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001025 struct urb *urb;
1026 unsigned char *buf;
1027 unsigned int pipe;
Vikram Kandukuri290ba202009-07-02 14:31:59 +05301028 int err, size = HCI_MAX_FRAME_SIZE;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001029
1030 BT_DBG("%s", hdev->name);
1031
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001032 if (!data->bulk_rx_ep)
1033 return -ENODEV;
1034
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001035 urb = usb_alloc_urb(0, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001036 if (!urb)
1037 return -ENOMEM;
1038
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001039 buf = kmalloc(size, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001040 if (!buf) {
1041 usb_free_urb(urb);
1042 return -ENOMEM;
1043 }
1044
1045 pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
1046
Marcel Holtmann89e75332014-09-16 04:44:50 +02001047 usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
1048 btusb_bulk_complete, hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001049
1050 urb->transfer_flags |= URB_FREE_BUFFER;
1051
Oliver Neukum7bee5492009-08-24 23:44:59 +02001052 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001053 usb_anchor_urb(urb, &data->bulk_anchor);
1054
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001055 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001056 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +02001057 if (err != -EPERM && err != -ENODEV)
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001058 bt_dev_err(hdev, "urb %p submission failed (%d)",
1059 urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001060 usb_unanchor_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001061 }
1062
1063 usb_free_urb(urb);
1064
1065 return err;
1066}
1067
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001068static void btusb_isoc_complete(struct urb *urb)
1069{
1070 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +01001071 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001072 int i, err;
1073
Marcel Holtmann89e75332014-09-16 04:44:50 +02001074 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1075 urb->actual_length);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001076
1077 if (!test_bit(HCI_RUNNING, &hdev->flags))
1078 return;
1079
1080 if (urb->status == 0) {
1081 for (i = 0; i < urb->number_of_packets; i++) {
1082 unsigned int offset = urb->iso_frame_desc[i].offset;
1083 unsigned int length = urb->iso_frame_desc[i].actual_length;
1084
1085 if (urb->iso_frame_desc[i].status)
1086 continue;
1087
1088 hdev->stat.byte_rx += length;
1089
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +02001090 if (btusb_recv_isoc(data, urb->transfer_buffer + offset,
1091 length) < 0) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001092 bt_dev_err(hdev, "corrupted SCO packet");
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001093 hdev->stat.err_rx++;
1094 }
1095 }
Champion Chen85560c42014-09-06 14:06:08 -05001096 } else if (urb->status == -ENOENT) {
1097 /* Avoid suspend failed when usb_kill_urb */
1098 return;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001099 }
1100
1101 if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags))
1102 return;
1103
1104 usb_anchor_urb(urb, &data->isoc_anchor);
1105
1106 err = usb_submit_urb(urb, GFP_ATOMIC);
1107 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +02001108 /* -EPERM: urb is being killed;
Derek Robsond98422c2017-07-22 13:47:07 +12001109 * -ENODEV: device got disconnected
1110 */
Paul Bolle4935f1c2011-08-09 17:16:28 +02001111 if (err != -EPERM && err != -ENODEV)
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001112 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
1113 urb, -err);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001114 usb_unanchor_urb(urb);
1115 }
1116}
1117
Sathish Narasimmanbaac6272020-04-03 21:43:59 +02001118static inline void __fill_isoc_descriptor_msbc(struct urb *urb, int len,
1119 int mtu, struct btusb_data *data)
1120{
1121 int i, offset = 0;
1122 unsigned int interval;
1123
1124 BT_DBG("len %d mtu %d", len, mtu);
1125
1126 /* For mSBC ALT 6 setting the host will send the packet at continuous
1127 * flow. As per core spec 5, vol 4, part B, table 2.1. For ALT setting
1128 * 6 the HCI PACKET INTERVAL should be 7.5ms for every usb packets.
1129 * To maintain the rate we send 63bytes of usb packets alternatively for
1130 * 7ms and 8ms to maintain the rate as 7.5ms.
1131 */
1132 if (data->usb_alt6_packet_flow) {
1133 interval = 7;
1134 data->usb_alt6_packet_flow = false;
1135 } else {
1136 interval = 6;
1137 data->usb_alt6_packet_flow = true;
1138 }
1139
1140 for (i = 0; i < interval; i++) {
1141 urb->iso_frame_desc[i].offset = offset;
1142 urb->iso_frame_desc[i].length = offset;
1143 }
1144
1145 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
1146 urb->iso_frame_desc[i].offset = offset;
1147 urb->iso_frame_desc[i].length = len;
1148 i++;
1149 }
1150
1151 urb->number_of_packets = i;
1152}
1153
Jesper Juhl42b16b32011-01-17 00:09:38 +01001154static inline void __fill_isoc_descriptor(struct urb *urb, int len, int mtu)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001155{
1156 int i, offset = 0;
1157
1158 BT_DBG("len %d mtu %d", len, mtu);
1159
1160 for (i = 0; i < BTUSB_MAX_ISOC_FRAMES && len >= mtu;
1161 i++, offset += mtu, len -= mtu) {
1162 urb->iso_frame_desc[i].offset = offset;
1163 urb->iso_frame_desc[i].length = mtu;
1164 }
1165
1166 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
1167 urb->iso_frame_desc[i].offset = offset;
1168 urb->iso_frame_desc[i].length = len;
1169 i++;
1170 }
1171
1172 urb->number_of_packets = i;
1173}
1174
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001175static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001176{
David Herrmann155961e2012-02-09 21:58:32 +01001177 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001178 struct urb *urb;
1179 unsigned char *buf;
1180 unsigned int pipe;
1181 int err, size;
1182
1183 BT_DBG("%s", hdev->name);
1184
1185 if (!data->isoc_rx_ep)
1186 return -ENODEV;
1187
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001188 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001189 if (!urb)
1190 return -ENOMEM;
1191
1192 size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) *
1193 BTUSB_MAX_ISOC_FRAMES;
1194
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001195 buf = kmalloc(size, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001196 if (!buf) {
1197 usb_free_urb(urb);
1198 return -ENOMEM;
1199 }
1200
1201 pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress);
1202
Bing Zhaofa0fb932011-12-20 18:19:00 -08001203 usb_fill_int_urb(urb, data->udev, pipe, buf, size, btusb_isoc_complete,
Marcel Holtmann89e75332014-09-16 04:44:50 +02001204 hdev, data->isoc_rx_ep->bInterval);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001205
Marcel Holtmann89e75332014-09-16 04:44:50 +02001206 urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001207
1208 __fill_isoc_descriptor(urb, size,
Marcel Holtmann89e75332014-09-16 04:44:50 +02001209 le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize));
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001210
1211 usb_anchor_urb(urb, &data->isoc_anchor);
1212
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001213 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001214 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +02001215 if (err != -EPERM && err != -ENODEV)
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001216 bt_dev_err(hdev, "urb %p submission failed (%d)",
1217 urb, -err);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001218 usb_unanchor_urb(urb);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001219 }
1220
1221 usb_free_urb(urb);
1222
1223 return err;
1224}
1225
Marcel Holtmann9d08f502015-10-08 20:23:08 +02001226static void btusb_diag_complete(struct urb *urb)
1227{
1228 struct hci_dev *hdev = urb->context;
1229 struct btusb_data *data = hci_get_drvdata(hdev);
1230 int err;
1231
1232 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1233 urb->actual_length);
1234
1235 if (urb->status == 0) {
1236 struct sk_buff *skb;
1237
1238 skb = bt_skb_alloc(urb->actual_length, GFP_ATOMIC);
1239 if (skb) {
Johannes Berg59ae1d12017-06-16 14:29:20 +02001240 skb_put_data(skb, urb->transfer_buffer,
1241 urb->actual_length);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02001242 hci_recv_diag(hdev, skb);
1243 }
1244 } else if (urb->status == -ENOENT) {
1245 /* Avoid suspend failed when usb_kill_urb */
1246 return;
1247 }
1248
1249 if (!test_bit(BTUSB_DIAG_RUNNING, &data->flags))
1250 return;
1251
1252 usb_anchor_urb(urb, &data->diag_anchor);
1253 usb_mark_last_busy(data->udev);
1254
1255 err = usb_submit_urb(urb, GFP_ATOMIC);
1256 if (err < 0) {
1257 /* -EPERM: urb is being killed;
Derek Robsond98422c2017-07-22 13:47:07 +12001258 * -ENODEV: device got disconnected
1259 */
Marcel Holtmann9d08f502015-10-08 20:23:08 +02001260 if (err != -EPERM && err != -ENODEV)
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001261 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
1262 urb, -err);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02001263 usb_unanchor_urb(urb);
1264 }
1265}
1266
1267static int btusb_submit_diag_urb(struct hci_dev *hdev, gfp_t mem_flags)
1268{
1269 struct btusb_data *data = hci_get_drvdata(hdev);
1270 struct urb *urb;
1271 unsigned char *buf;
1272 unsigned int pipe;
1273 int err, size = HCI_MAX_FRAME_SIZE;
1274
1275 BT_DBG("%s", hdev->name);
1276
1277 if (!data->diag_rx_ep)
1278 return -ENODEV;
1279
1280 urb = usb_alloc_urb(0, mem_flags);
1281 if (!urb)
1282 return -ENOMEM;
1283
1284 buf = kmalloc(size, mem_flags);
1285 if (!buf) {
1286 usb_free_urb(urb);
1287 return -ENOMEM;
1288 }
1289
1290 pipe = usb_rcvbulkpipe(data->udev, data->diag_rx_ep->bEndpointAddress);
1291
1292 usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
1293 btusb_diag_complete, hdev);
1294
1295 urb->transfer_flags |= URB_FREE_BUFFER;
1296
1297 usb_mark_last_busy(data->udev);
1298 usb_anchor_urb(urb, &data->diag_anchor);
1299
1300 err = usb_submit_urb(urb, mem_flags);
1301 if (err < 0) {
1302 if (err != -EPERM && err != -ENODEV)
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001303 bt_dev_err(hdev, "urb %p submission failed (%d)",
1304 urb, -err);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02001305 usb_unanchor_urb(urb);
1306 }
1307
1308 usb_free_urb(urb);
1309
1310 return err;
1311}
1312
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001313static void btusb_tx_complete(struct urb *urb)
1314{
1315 struct sk_buff *skb = urb->context;
Marcel Holtmann89e75332014-09-16 04:44:50 +02001316 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
David Herrmann155961e2012-02-09 21:58:32 +01001317 struct btusb_data *data = hci_get_drvdata(hdev);
Sebastian Andrzej Siewiora5e50d52018-06-19 23:56:57 +02001318 unsigned long flags;
Oliver Neukum7bee5492009-08-24 23:44:59 +02001319
Marcel Holtmann89e75332014-09-16 04:44:50 +02001320 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1321 urb->actual_length);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001322
1323 if (!test_bit(HCI_RUNNING, &hdev->flags))
1324 goto done;
1325
1326 if (!urb->status)
1327 hdev->stat.byte_tx += urb->transfer_buffer_length;
1328 else
1329 hdev->stat.err_tx++;
1330
1331done:
Sebastian Andrzej Siewiora5e50d52018-06-19 23:56:57 +02001332 spin_lock_irqsave(&data->txlock, flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001333 data->tx_in_flight--;
Sebastian Andrzej Siewiora5e50d52018-06-19 23:56:57 +02001334 spin_unlock_irqrestore(&data->txlock, flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001335
1336 kfree(urb->setup_packet);
1337
1338 kfree_skb(skb);
1339}
1340
1341static void btusb_isoc_tx_complete(struct urb *urb)
1342{
1343 struct sk_buff *skb = urb->context;
Marcel Holtmann89e75332014-09-16 04:44:50 +02001344 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001345
Marcel Holtmann89e75332014-09-16 04:44:50 +02001346 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1347 urb->actual_length);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001348
1349 if (!test_bit(HCI_RUNNING, &hdev->flags))
1350 goto done;
1351
1352 if (!urb->status)
1353 hdev->stat.byte_tx += urb->transfer_buffer_length;
1354 else
1355 hdev->stat.err_tx++;
1356
1357done:
1358 kfree(urb->setup_packet);
1359
1360 kfree_skb(skb);
1361}
1362
1363static int btusb_open(struct hci_dev *hdev)
1364{
David Herrmann155961e2012-02-09 21:58:32 +01001365 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001366 int err;
1367
1368 BT_DBG("%s", hdev->name);
1369
Ethan Hsiehc7e163f2016-10-07 12:06:42 +08001370 err = usb_autopm_get_interface(data->intf);
1371 if (err < 0)
1372 return err;
1373
Kim, Ben Young Taeace31982015-02-15 23:06:14 +00001374 /* Patching USB firmware files prior to starting any URBs of HCI path
1375 * It is more safe to use USB bulk channel for downloading USB patch
1376 */
1377 if (data->setup_on_usb) {
1378 err = data->setup_on_usb(hdev);
Marcel Holtmanneb500422015-04-16 23:15:50 +02001379 if (err < 0)
Oliver Neukum3d44a6f2019-11-14 16:01:18 +01001380 goto setup_fail;
Kim, Ben Young Taeace31982015-02-15 23:06:14 +00001381 }
1382
Oliver Neukum7bee5492009-08-24 23:44:59 +02001383 data->intf->needs_remote_wakeup = 1;
1384
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001385 if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +02001386 goto done;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001387
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001388 err = btusb_submit_intr_urb(hdev, GFP_KERNEL);
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001389 if (err < 0)
1390 goto failed;
1391
1392 err = btusb_submit_bulk_urb(hdev, GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001393 if (err < 0) {
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001394 usb_kill_anchored_urbs(&data->intr_anchor);
1395 goto failed;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001396 }
1397
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001398 set_bit(BTUSB_BULK_RUNNING, &data->flags);
1399 btusb_submit_bulk_urb(hdev, GFP_KERNEL);
1400
Marcel Holtmann9d08f502015-10-08 20:23:08 +02001401 if (data->diag) {
1402 if (!btusb_submit_diag_urb(hdev, GFP_KERNEL))
1403 set_bit(BTUSB_DIAG_RUNNING, &data->flags);
1404 }
1405
Oliver Neukum7bee5492009-08-24 23:44:59 +02001406done:
1407 usb_autopm_put_interface(data->intf);
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001408 return 0;
1409
1410failed:
1411 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Oliver Neukum3d44a6f2019-11-14 16:01:18 +01001412setup_fail:
Oliver Neukum7bee5492009-08-24 23:44:59 +02001413 usb_autopm_put_interface(data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001414 return err;
1415}
1416
Oliver Neukum7bee5492009-08-24 23:44:59 +02001417static void btusb_stop_traffic(struct btusb_data *data)
1418{
1419 usb_kill_anchored_urbs(&data->intr_anchor);
1420 usb_kill_anchored_urbs(&data->bulk_anchor);
1421 usb_kill_anchored_urbs(&data->isoc_anchor);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02001422 usb_kill_anchored_urbs(&data->diag_anchor);
Sean Wanga1c49c432019-06-02 08:02:48 +08001423 usb_kill_anchored_urbs(&data->ctrl_anchor);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001424}
1425
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001426static int btusb_close(struct hci_dev *hdev)
1427{
David Herrmann155961e2012-02-09 21:58:32 +01001428 struct btusb_data *data = hci_get_drvdata(hdev);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001429 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001430
1431 BT_DBG("%s", hdev->name);
1432
Marcel Holtmanne8c3c3d2008-09-23 00:16:36 +02001433 cancel_work_sync(&data->work);
Linus Torvalds404291a2009-11-11 13:32:29 -08001434 cancel_work_sync(&data->waker);
Marcel Holtmanne8c3c3d2008-09-23 00:16:36 +02001435
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001436 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001437 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001438 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02001439 clear_bit(BTUSB_DIAG_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001440
1441 btusb_stop_traffic(data);
Marcel Holtmann803b5832014-09-16 08:00:29 +02001442 btusb_free_frags(data);
1443
Oliver Neukum7bee5492009-08-24 23:44:59 +02001444 err = usb_autopm_get_interface(data->intf);
1445 if (err < 0)
Oliver Neukum7b8e2c12009-11-13 14:26:23 +01001446 goto failed;
Oliver Neukum7bee5492009-08-24 23:44:59 +02001447
1448 data->intf->needs_remote_wakeup = 0;
Alex Lu9e455242019-08-14 20:02:52 +08001449
1450 /* Enable remote wake up for auto-suspend */
Max Chou8274db02021-08-17 11:03:12 +08001451 if (test_bit(BTUSB_WAKEUP_AUTOSUSPEND, &data->flags))
Alex Lu9e455242019-08-14 20:02:52 +08001452 data->intf->needs_remote_wakeup = 1;
1453
Oliver Neukum7bee5492009-08-24 23:44:59 +02001454 usb_autopm_put_interface(data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001455
Oliver Neukum7b8e2c12009-11-13 14:26:23 +01001456failed:
1457 usb_scuttle_anchored_urbs(&data->deferred);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001458 return 0;
1459}
1460
1461static int btusb_flush(struct hci_dev *hdev)
1462{
David Herrmann155961e2012-02-09 21:58:32 +01001463 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001464
1465 BT_DBG("%s", hdev->name);
1466
1467 usb_kill_anchored_urbs(&data->tx_anchor);
Marcel Holtmann803b5832014-09-16 08:00:29 +02001468 btusb_free_frags(data);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001469
1470 return 0;
1471}
1472
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001473static struct urb *alloc_ctrl_urb(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001474{
David Herrmann155961e2012-02-09 21:58:32 +01001475 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001476 struct usb_ctrlrequest *dr;
1477 struct urb *urb;
1478 unsigned int pipe;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001479
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001480 urb = usb_alloc_urb(0, GFP_KERNEL);
1481 if (!urb)
1482 return ERR_PTR(-ENOMEM);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001483
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001484 dr = kmalloc(sizeof(*dr), GFP_KERNEL);
1485 if (!dr) {
1486 usb_free_urb(urb);
1487 return ERR_PTR(-ENOMEM);
1488 }
1489
1490 dr->bRequestType = data->cmdreq_type;
Marcel Holtmann893ba542015-01-28 20:27:34 -08001491 dr->bRequest = data->cmdreq;
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001492 dr->wIndex = 0;
1493 dr->wValue = 0;
1494 dr->wLength = __cpu_to_le16(skb->len);
1495
1496 pipe = usb_sndctrlpipe(data->udev, 0x00);
1497
Marcel Holtmann89e75332014-09-16 04:44:50 +02001498 usb_fill_control_urb(urb, data->udev, pipe, (void *)dr,
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001499 skb->data, skb->len, btusb_tx_complete, skb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001500
Marcel Holtmann89e75332014-09-16 04:44:50 +02001501 skb->dev = (void *)hdev;
Marcel Holtmann7bd8f092013-10-11 06:19:18 -07001502
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001503 return urb;
1504}
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001505
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001506static struct urb *alloc_bulk_urb(struct hci_dev *hdev, struct sk_buff *skb)
1507{
1508 struct btusb_data *data = hci_get_drvdata(hdev);
1509 struct urb *urb;
1510 unsigned int pipe;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001511
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001512 if (!data->bulk_tx_ep)
1513 return ERR_PTR(-ENODEV);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001514
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001515 urb = usb_alloc_urb(0, GFP_KERNEL);
1516 if (!urb)
1517 return ERR_PTR(-ENOMEM);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001518
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001519 pipe = usb_sndbulkpipe(data->udev, data->bulk_tx_ep->bEndpointAddress);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001520
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001521 usb_fill_bulk_urb(urb, data->udev, pipe,
1522 skb->data, skb->len, btusb_tx_complete, skb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001523
Marcel Holtmann89e75332014-09-16 04:44:50 +02001524 skb->dev = (void *)hdev;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001525
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001526 return urb;
1527}
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001528
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001529static struct urb *alloc_isoc_urb(struct hci_dev *hdev, struct sk_buff *skb)
1530{
1531 struct btusb_data *data = hci_get_drvdata(hdev);
1532 struct urb *urb;
1533 unsigned int pipe;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001534
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001535 if (!data->isoc_tx_ep)
1536 return ERR_PTR(-ENODEV);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001537
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001538 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_KERNEL);
1539 if (!urb)
1540 return ERR_PTR(-ENOMEM);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001541
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001542 pipe = usb_sndisocpipe(data->udev, data->isoc_tx_ep->bEndpointAddress);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001543
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001544 usb_fill_int_urb(urb, data->udev, pipe,
1545 skb->data, skb->len, btusb_isoc_tx_complete,
1546 skb, data->isoc_tx_ep->bInterval);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001547
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001548 urb->transfer_flags = URB_ISO_ASAP;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001549
Sathish Narasimmanbaac6272020-04-03 21:43:59 +02001550 if (data->isoc_altsetting == 6)
1551 __fill_isoc_descriptor_msbc(urb, skb->len,
1552 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize),
1553 data);
1554 else
1555 __fill_isoc_descriptor(urb, skb->len,
1556 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
Marcel Holtmann89e75332014-09-16 04:44:50 +02001557 skb->dev = (void *)hdev;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001558
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001559 return urb;
1560}
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001561
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001562static int submit_tx_urb(struct hci_dev *hdev, struct urb *urb)
1563{
1564 struct btusb_data *data = hci_get_drvdata(hdev);
1565 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001566
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001567 usb_anchor_urb(urb, &data->tx_anchor);
1568
Johan Hedberge9753ef2014-09-14 08:49:34 +03001569 err = usb_submit_urb(urb, GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001570 if (err < 0) {
Paul Bolle5a9b80e2011-10-09 12:12:16 +02001571 if (err != -EPERM && err != -ENODEV)
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001572 bt_dev_err(hdev, "urb %p submission failed (%d)",
1573 urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001574 kfree(urb->setup_packet);
1575 usb_unanchor_urb(urb);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001576 } else {
1577 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001578 }
1579
Cong Wang54a8a792011-11-22 09:32:57 +08001580 usb_free_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001581 return err;
1582}
1583
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001584static int submit_or_queue_tx_urb(struct hci_dev *hdev, struct urb *urb)
1585{
1586 struct btusb_data *data = hci_get_drvdata(hdev);
1587 unsigned long flags;
1588 bool suspending;
1589
1590 spin_lock_irqsave(&data->txlock, flags);
1591 suspending = test_bit(BTUSB_SUSPENDING, &data->flags);
1592 if (!suspending)
1593 data->tx_in_flight++;
1594 spin_unlock_irqrestore(&data->txlock, flags);
1595
1596 if (!suspending)
1597 return submit_tx_urb(hdev, urb);
1598
1599 usb_anchor_urb(urb, &data->deferred);
1600 schedule_work(&data->waker);
1601
1602 usb_free_urb(urb);
1603 return 0;
1604}
1605
1606static int btusb_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
1607{
1608 struct urb *urb;
1609
1610 BT_DBG("%s", hdev->name);
1611
Marcel Holtmann618e8bc2015-11-05 07:33:56 +01001612 switch (hci_skb_pkt_type(skb)) {
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001613 case HCI_COMMAND_PKT:
1614 urb = alloc_ctrl_urb(hdev, skb);
1615 if (IS_ERR(urb))
1616 return PTR_ERR(urb);
1617
1618 hdev->stat.cmd_tx++;
1619 return submit_or_queue_tx_urb(hdev, urb);
1620
1621 case HCI_ACLDATA_PKT:
1622 urb = alloc_bulk_urb(hdev, skb);
1623 if (IS_ERR(urb))
1624 return PTR_ERR(urb);
1625
1626 hdev->stat.acl_tx++;
1627 return submit_or_queue_tx_urb(hdev, urb);
1628
1629 case HCI_SCODATA_PKT:
1630 if (hci_conn_num(hdev, SCO_LINK) < 1)
1631 return -ENODEV;
1632
1633 urb = alloc_isoc_urb(hdev, skb);
1634 if (IS_ERR(urb))
1635 return PTR_ERR(urb);
1636
1637 hdev->stat.sco_tx++;
1638 return submit_tx_urb(hdev, urb);
1639 }
1640
1641 return -EILSEQ;
1642}
1643
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001644static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
1645{
David Herrmann155961e2012-02-09 21:58:32 +01001646 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001647
1648 BT_DBG("%s evt %d", hdev->name, evt);
1649
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001650 if (hci_conn_num(hdev, SCO_LINK) != data->sco_num) {
1651 data->sco_num = hci_conn_num(hdev, SCO_LINK);
Sathish Narasimmanbaac6272020-04-03 21:43:59 +02001652 data->air_mode = evt;
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001653 schedule_work(&data->work);
Marcel Holtmanna780efa2008-11-30 12:17:12 +01001654 }
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001655}
1656
Jesper Juhl42b16b32011-01-17 00:09:38 +01001657static inline int __set_isoc_interface(struct hci_dev *hdev, int altsetting)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001658{
David Herrmann155961e2012-02-09 21:58:32 +01001659 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001660 struct usb_interface *intf = data->isoc;
1661 struct usb_endpoint_descriptor *ep_desc;
1662 int i, err;
1663
1664 if (!data->isoc)
1665 return -ENODEV;
1666
Marcel Holtmann459232f2017-10-24 19:42:45 +02001667 err = usb_set_interface(data->udev, data->isoc_ifnum, altsetting);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001668 if (err < 0) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001669 bt_dev_err(hdev, "setting interface failed (%d)", -err);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001670 return err;
1671 }
1672
1673 data->isoc_altsetting = altsetting;
1674
1675 data->isoc_tx_ep = NULL;
1676 data->isoc_rx_ep = NULL;
1677
1678 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
1679 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
1680
1681 if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) {
1682 data->isoc_tx_ep = ep_desc;
1683 continue;
1684 }
1685
1686 if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) {
1687 data->isoc_rx_ep = ep_desc;
1688 continue;
1689 }
1690 }
1691
1692 if (!data->isoc_tx_ep || !data->isoc_rx_ep) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001693 bt_dev_err(hdev, "invalid SCO descriptors");
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001694 return -ENODEV;
1695 }
1696
1697 return 0;
1698}
1699
Sathish Narasimmanbaac6272020-04-03 21:43:59 +02001700static int btusb_switch_alt_setting(struct hci_dev *hdev, int new_alts)
1701{
1702 struct btusb_data *data = hci_get_drvdata(hdev);
1703 int err;
1704
1705 if (data->isoc_altsetting != new_alts) {
1706 unsigned long flags;
1707
1708 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1709 usb_kill_anchored_urbs(&data->isoc_anchor);
1710
1711 /* When isochronous alternate setting needs to be
1712 * changed, because SCO connection has been added
1713 * or removed, a packet fragment may be left in the
1714 * reassembling state. This could lead to wrongly
1715 * assembled fragments.
1716 *
1717 * Clear outstanding fragment when selecting a new
1718 * alternate setting.
1719 */
1720 spin_lock_irqsave(&data->rxlock, flags);
1721 kfree_skb(data->sco_skb);
1722 data->sco_skb = NULL;
1723 spin_unlock_irqrestore(&data->rxlock, flags);
1724
1725 err = __set_isoc_interface(hdev, new_alts);
1726 if (err < 0)
1727 return err;
1728 }
1729
1730 if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
1731 if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0)
1732 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1733 else
1734 btusb_submit_isoc_urb(hdev, GFP_KERNEL);
1735 }
1736
1737 return 0;
1738}
1739
1740static struct usb_host_interface *btusb_find_altsetting(struct btusb_data *data,
1741 int alt)
1742{
1743 struct usb_interface *intf = data->isoc;
1744 int i;
1745
1746 BT_DBG("Looking for Alt no :%d", alt);
1747
Sathish Narasimmanfcd156ee2020-04-08 10:57:03 +05301748 if (!intf)
1749 return NULL;
1750
Sathish Narasimmanbaac6272020-04-03 21:43:59 +02001751 for (i = 0; i < intf->num_altsetting; i++) {
1752 if (intf->altsetting[i].desc.bAlternateSetting == alt)
1753 return &intf->altsetting[i];
1754 }
1755
1756 return NULL;
1757}
1758
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001759static void btusb_work(struct work_struct *work)
1760{
1761 struct btusb_data *data = container_of(work, struct btusb_data, work);
1762 struct hci_dev *hdev = data->hdev;
Sathish Narasimmanbaac6272020-04-03 21:43:59 +02001763 int new_alts = 0;
Oliver Neukum7bee5492009-08-24 23:44:59 +02001764 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001765
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001766 if (data->sco_num > 0) {
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -03001767 if (!test_bit(BTUSB_DID_ISO_RESUME, &data->flags)) {
Oliver Neukum8efdd0c2011-02-11 13:00:06 +01001768 err = usb_autopm_get_interface(data->isoc ? data->isoc : data->intf);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001769 if (err < 0) {
1770 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1771 usb_kill_anchored_urbs(&data->isoc_anchor);
1772 return;
1773 }
1774
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -03001775 set_bit(BTUSB_DID_ISO_RESUME, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001776 }
Mikel Astizf4001d22012-04-11 08:48:51 +02001777
Sathish Narasimmanbaac6272020-04-03 21:43:59 +02001778 if (data->air_mode == HCI_NOTIFY_ENABLE_SCO_CVSD) {
1779 if (hdev->voice_setting & 0x0020) {
1780 static const int alts[3] = { 2, 4, 5 };
Marcel Holtmann89e75332014-09-16 04:44:50 +02001781
Sathish Narasimmanbaac6272020-04-03 21:43:59 +02001782 new_alts = alts[data->sco_num - 1];
1783 } else {
1784 new_alts = data->sco_num;
1785 }
1786 } else if (data->air_mode == HCI_NOTIFY_ENABLE_SCO_TRANSP) {
Trent Piepho517b6932020-12-09 17:20:03 -08001787 /* Bluetooth USB spec recommends alt 6 (63 bytes), but
1788 * many adapters do not support it. Alt 1 appears to
1789 * work for all adapters that do not have alt 6, and
Pauli Virtanen55981d32021-07-26 21:02:06 +03001790 * which work with WBS at all. Some devices prefer
1791 * alt 3 (HCI payload >= 60 Bytes let air packet
1792 * data satisfy 60 bytes), requiring
1793 * MTU >= 3 (packets) * 25 (size) - 3 (headers) = 72
1794 * see also Core spec 5, vol 4, B 2.1.1 & Table 2.1.
Trent Piepho517b6932020-12-09 17:20:03 -08001795 */
Pauli Virtanen55981d32021-07-26 21:02:06 +03001796 if (btusb_find_altsetting(data, 6))
1797 new_alts = 6;
1798 else if (btusb_find_altsetting(data, 3) &&
1799 hdev->sco_mtu >= 72 &&
1800 test_bit(BTUSB_USE_ALT3_FOR_WBS, &data->flags))
Hilda Wue848dbd2021-05-14 11:19:01 +08001801 new_alts = 3;
Pauli Virtanen55981d32021-07-26 21:02:06 +03001802 else
1803 new_alts = 1;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001804 }
Sathish Narasimmanbaac6272020-04-03 21:43:59 +02001805
1806 if (btusb_switch_alt_setting(hdev, new_alts) < 0)
1807 bt_dev_err(hdev, "set USB alt:(%d) failed!", new_alts);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001808 } else {
1809 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1810 usb_kill_anchored_urbs(&data->isoc_anchor);
1811
1812 __set_isoc_interface(hdev, 0);
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -03001813 if (test_and_clear_bit(BTUSB_DID_ISO_RESUME, &data->flags))
Oliver Neukum8efdd0c2011-02-11 13:00:06 +01001814 usb_autopm_put_interface(data->isoc ? data->isoc : data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001815 }
1816}
1817
Oliver Neukum7bee5492009-08-24 23:44:59 +02001818static void btusb_waker(struct work_struct *work)
1819{
1820 struct btusb_data *data = container_of(work, struct btusb_data, waker);
1821 int err;
1822
1823 err = usb_autopm_get_interface(data->intf);
1824 if (err < 0)
1825 return;
1826
1827 usb_autopm_put_interface(data->intf);
1828}
1829
Marcel Holtmann9f8f9622013-04-10 08:11:35 -07001830static int btusb_setup_bcm92035(struct hci_dev *hdev)
1831{
1832 struct sk_buff *skb;
1833 u8 val = 0x00;
1834
1835 BT_DBG("%s", hdev->name);
1836
1837 skb = __hci_cmd_sync(hdev, 0xfc3b, 1, &val, HCI_INIT_TIMEOUT);
1838 if (IS_ERR(skb))
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001839 bt_dev_err(hdev, "BCM92035 command failed (%ld)", PTR_ERR(skb));
Marcel Holtmann9f8f9622013-04-10 08:11:35 -07001840 else
1841 kfree_skb(skb);
1842
1843 return 0;
1844}
1845
Marcel Holtmann81cac642014-01-03 03:02:36 -08001846static int btusb_setup_csr(struct hci_dev *hdev)
1847{
Hans de Goeded74e0ae2020-12-05 16:02:00 +01001848 struct btusb_data *data = hci_get_drvdata(hdev);
1849 u16 bcdDevice = le16_to_cpu(data->udev->descriptor.bcdDevice);
Marcel Holtmann81cac642014-01-03 03:02:36 -08001850 struct hci_rp_read_local_version *rp;
1851 struct sk_buff *skb;
Ismael Ferreras Morezuelascde1a8a2020-07-26 23:12:28 +02001852 bool is_fake = false;
Hans de Goede0671c0662020-12-05 16:02:01 +01001853 int ret;
Marcel Holtmann81cac642014-01-03 03:02:36 -08001854
1855 BT_DBG("%s", hdev->name);
1856
Marcel Holtmann7cd84d72015-06-07 10:01:02 +02001857 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
1858 HCI_INIT_TIMEOUT);
1859 if (IS_ERR(skb)) {
1860 int err = PTR_ERR(skb);
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001861 bt_dev_err(hdev, "CSR: Local version failed (%d)", err);
Marcel Holtmann7cd84d72015-06-07 10:01:02 +02001862 return err;
1863 }
1864
1865 if (skb->len != sizeof(struct hci_rp_read_local_version)) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001866 bt_dev_err(hdev, "CSR: Local version length mismatch");
Marcel Holtmann7cd84d72015-06-07 10:01:02 +02001867 kfree_skb(skb);
1868 return -EIO;
1869 }
Marcel Holtmann81cac642014-01-03 03:02:36 -08001870
Marcel Holtmann89e75332014-09-16 04:44:50 +02001871 rp = (struct hci_rp_read_local_version *)skb->data;
Marcel Holtmann81cac642014-01-03 03:02:36 -08001872
Ismael Ferreras Morezuelascde1a8a2020-07-26 23:12:28 +02001873 /* Detect a wide host of Chinese controllers that aren't CSR.
1874 *
1875 * Known fake bcdDevices: 0x0100, 0x0134, 0x1915, 0x2520, 0x7558, 0x8891
1876 *
1877 * The main thing they have in common is that these are really popular low-cost
1878 * options that support newer Bluetooth versions but rely on heavy VID/PID
1879 * squatting of this poor old Bluetooth 1.1 device. Even sold as such.
1880 *
1881 * We detect actual CSR devices by checking that the HCI manufacturer code
1882 * is Cambridge Silicon Radio (10) and ensuring that LMP sub-version and
1883 * HCI rev values always match. As they both store the firmware number.
1884 */
Johan Hedberg6cafcd92015-08-30 21:47:21 +03001885 if (le16_to_cpu(rp->manufacturer) != 10 ||
Ismael Ferreras Morezuelascde1a8a2020-07-26 23:12:28 +02001886 le16_to_cpu(rp->hci_rev) != le16_to_cpu(rp->lmp_subver))
1887 is_fake = true;
1888
1889 /* Known legit CSR firmware build numbers and their supported BT versions:
1890 * - 1.1 (0x1) -> 0x0073, 0x020d, 0x033c, 0x034e
1891 * - 1.2 (0x2) -> 0x04d9, 0x0529
1892 * - 2.0 (0x3) -> 0x07a6, 0x07ad, 0x0c5c
1893 * - 2.1 (0x4) -> 0x149c, 0x1735, 0x1899 (0x1899 is a BlueCore4-External)
1894 * - 4.0 (0x6) -> 0x1d86, 0x2031, 0x22bb
1895 *
1896 * e.g. Real CSR dongles with LMP subversion 0x73 are old enough that
1897 * support BT 1.1 only; so it's a dead giveaway when some
1898 * third-party BT 4.0 dongle reuses it.
1899 */
1900 else if (le16_to_cpu(rp->lmp_subver) <= 0x034e &&
1901 le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_1_1)
1902 is_fake = true;
1903
1904 else if (le16_to_cpu(rp->lmp_subver) <= 0x0529 &&
1905 le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_1_2)
1906 is_fake = true;
1907
1908 else if (le16_to_cpu(rp->lmp_subver) <= 0x0c5c &&
1909 le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_2_0)
1910 is_fake = true;
1911
1912 else if (le16_to_cpu(rp->lmp_subver) <= 0x1899 &&
1913 le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_2_1)
1914 is_fake = true;
1915
1916 else if (le16_to_cpu(rp->lmp_subver) <= 0x22bb &&
1917 le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_4_0)
1918 is_fake = true;
1919
Hans de Goeded74e0ae2020-12-05 16:02:00 +01001920 /* Other clones which beat all the above checks */
1921 else if (bcdDevice == 0x0134 &&
1922 le16_to_cpu(rp->lmp_subver) == 0x0c5c &&
1923 le16_to_cpu(rp->hci_ver) == BLUETOOTH_VER_2_0)
1924 is_fake = true;
1925
Ismael Ferreras Morezuelascde1a8a2020-07-26 23:12:28 +02001926 if (is_fake) {
Ismael Ferreras Morezuelasf4292e22021-07-17 01:21:43 +02001927 bt_dev_warn(hdev, "CSR: Unbranded CSR clone detected; adding workarounds and force-suspending once...");
Ismael Ferreras Morezuelascde1a8a2020-07-26 23:12:28 +02001928
1929 /* Generally these clones have big discrepancies between
1930 * advertised features and what's actually supported.
1931 * Probably will need to be expanded in the future;
1932 * without these the controller will lock up.
1933 */
1934 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
1935 set_bit(HCI_QUIRK_BROKEN_ERR_DATA_REPORTING, &hdev->quirks);
1936
Marcel Holtmann9641d342015-06-07 10:01:01 +02001937 /* Clear the reset quirk since this is not an actual
1938 * early Bluetooth 1.1 device from CSR.
1939 */
1940 clear_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Ismael Ferreras Morezuelascde1a8a2020-07-26 23:12:28 +02001941 clear_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
Hans de Goede0671c0662020-12-05 16:02:01 +01001942
1943 /*
Ismael Ferreras Morezuelasf4292e22021-07-17 01:21:43 +02001944 * Special workaround for these BT 4.0 chip clones, and potentially more:
1945 *
1946 * - 0x0134: a Barrot 8041a02 (HCI rev: 0x1012 sub: 0x0810)
1947 * - 0x7558: IC markings FR3191AHAL 749H15143 (HCI rev/sub-version: 0x0709)
1948 *
1949 * These controllers are really messed-up.
1950 *
1951 * 1. Their bulk RX endpoint will never report any data unless
1952 * the device was suspended at least once (yes, really).
Hans de Goede0671c0662020-12-05 16:02:01 +01001953 * 2. They will not wakeup when autosuspended and receiving data
Ismael Ferreras Morezuelasf4292e22021-07-17 01:21:43 +02001954 * on their bulk RX endpoint from e.g. a keyboard or mouse
Hans de Goede0671c0662020-12-05 16:02:01 +01001955 * (IOW remote-wakeup support is broken for the bulk endpoint).
1956 *
1957 * To fix 1. enable runtime-suspend, force-suspend the
Ismael Ferreras Morezuelasf4292e22021-07-17 01:21:43 +02001958 * HCI and then wake-it up by disabling runtime-suspend.
Hans de Goede0671c0662020-12-05 16:02:01 +01001959 *
Ismael Ferreras Morezuelasf4292e22021-07-17 01:21:43 +02001960 * To fix 2. clear the HCI's can_wake flag, this way the HCI
Hans de Goede0671c0662020-12-05 16:02:01 +01001961 * will still be autosuspended when it is not open.
Ismael Ferreras Morezuelasf4292e22021-07-17 01:21:43 +02001962 *
1963 * --
1964 *
1965 * Because these are widespread problems we prefer generic solutions; so
1966 * apply this initialization quirk to every controller that gets here,
1967 * it should be harmless. The alternative is to not work at all.
Hans de Goede0671c0662020-12-05 16:02:01 +01001968 */
Ismael Ferreras Morezuelasf4292e22021-07-17 01:21:43 +02001969 pm_runtime_allow(&data->udev->dev);
Hans de Goede0671c0662020-12-05 16:02:01 +01001970
Ismael Ferreras Morezuelasf4292e22021-07-17 01:21:43 +02001971 ret = pm_runtime_suspend(&data->udev->dev);
1972 if (ret >= 0)
1973 msleep(200);
1974 else
1975 bt_dev_err(hdev, "CSR: Failed to suspend the device for our Barrot 8041a02 receive-issue workaround");
Hans de Goede0671c0662020-12-05 16:02:01 +01001976
Ismael Ferreras Morezuelasf4292e22021-07-17 01:21:43 +02001977 pm_runtime_forbid(&data->udev->dev);
Hans de Goede0671c0662020-12-05 16:02:01 +01001978
Ismael Ferreras Morezuelasf4292e22021-07-17 01:21:43 +02001979 device_set_wakeup_capable(&data->udev->dev, false);
Hans de Goede0671c0662020-12-05 16:02:01 +01001980
Ismael Ferreras Morezuelasf4292e22021-07-17 01:21:43 +02001981 /* Re-enable autosuspend if this was requested */
1982 if (enable_autosuspend)
1983 usb_enable_autosuspend(data->udev);
Marcel Holtmann81cac642014-01-03 03:02:36 -08001984 }
1985
Marcel Holtmann81cac642014-01-03 03:02:36 -08001986 kfree_skb(skb);
1987
Marcel Holtmann9641d342015-06-07 10:01:01 +02001988 return 0;
Marcel Holtmann81cac642014-01-03 03:02:36 -08001989}
1990
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001991static int inject_cmd_complete(struct hci_dev *hdev, __u16 opcode)
1992{
1993 struct sk_buff *skb;
1994 struct hci_event_hdr *hdr;
1995 struct hci_ev_cmd_complete *evt;
1996
Jia-Ju Baicf07e342018-07-23 11:38:51 +08001997 skb = bt_skb_alloc(sizeof(*hdr) + sizeof(*evt) + 1, GFP_KERNEL);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001998 if (!skb)
1999 return -ENOMEM;
2000
Johannes Berg4df864c2017-06-16 14:29:21 +02002001 hdr = skb_put(skb, sizeof(*hdr));
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002002 hdr->evt = HCI_EV_CMD_COMPLETE;
2003 hdr->plen = sizeof(*evt) + 1;
2004
Johannes Berg4df864c2017-06-16 14:29:21 +02002005 evt = skb_put(skb, sizeof(*evt));
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002006 evt->ncmd = 0x01;
2007 evt->opcode = cpu_to_le16(opcode);
2008
Johannes Berg634fef62017-06-16 14:29:24 +02002009 skb_put_u8(skb, 0x00);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002010
Marcel Holtmann618e8bc2015-11-05 07:33:56 +01002011 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002012
2013 return hci_recv_frame(hdev, skb);
2014}
2015
2016static int btusb_recv_bulk_intel(struct btusb_data *data, void *buffer,
2017 int count)
2018{
Tedd Ho-Jeong An019a1ca2021-08-04 17:32:15 -07002019 struct hci_dev *hdev = data->hdev;
2020
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002021 /* When the device is in bootloader mode, then it can send
2022 * events via the bulk endpoint. These events are treated the
2023 * same way as the ones received from the interrupt endpoint.
2024 */
Tedd Ho-Jeong An019a1ca2021-08-04 17:32:15 -07002025 if (btintel_test_flag(hdev, INTEL_BOOTLOADER))
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002026 return btusb_recv_intr(data, buffer, count);
2027
2028 return btusb_recv_bulk(data, buffer, count);
2029}
2030
2031static int btusb_recv_event_intel(struct hci_dev *hdev, struct sk_buff *skb)
2032{
Tedd Ho-Jeong An019a1ca2021-08-04 17:32:15 -07002033 if (btintel_test_flag(hdev, INTEL_BOOTLOADER)) {
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002034 struct hci_event_hdr *hdr = (void *)skb->data;
2035
Marcel Holtmannccd6da22015-04-09 00:35:19 -07002036 if (skb->len > HCI_EVENT_HDR_SIZE && hdr->evt == 0xff &&
2037 hdr->plen > 0) {
2038 const void *ptr = skb->data + HCI_EVENT_HDR_SIZE + 1;
2039 unsigned int len = skb->len - HCI_EVENT_HDR_SIZE - 1;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002040
Marcel Holtmannccd6da22015-04-09 00:35:19 -07002041 switch (skb->data[2]) {
2042 case 0x02:
2043 /* When switching to the operational firmware
2044 * the device sends a vendor specific event
2045 * indicating that the bootup completed.
2046 */
Tedd Ho-Jeong An019a1ca2021-08-04 17:32:15 -07002047 btintel_bootup(hdev, ptr, len);
Marcel Holtmannccd6da22015-04-09 00:35:19 -07002048 break;
2049 case 0x06:
2050 /* When the firmware loading completes the
2051 * device sends out a vendor specific event
2052 * indicating the result of the firmware
2053 * loading.
2054 */
Tedd Ho-Jeong An019a1ca2021-08-04 17:32:15 -07002055 btintel_secure_send_result(hdev, ptr, len);
Marcel Holtmannccd6da22015-04-09 00:35:19 -07002056 break;
Johan Hedbergfad70972015-01-30 10:58:55 +02002057 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002058 }
2059 }
2060
2061 return hci_recv_frame(hdev, skb);
2062}
2063
2064static int btusb_send_frame_intel(struct hci_dev *hdev, struct sk_buff *skb)
2065{
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002066 struct urb *urb;
2067
2068 BT_DBG("%s", hdev->name);
2069
Marcel Holtmann618e8bc2015-11-05 07:33:56 +01002070 switch (hci_skb_pkt_type(skb)) {
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002071 case HCI_COMMAND_PKT:
Tedd Ho-Jeong An019a1ca2021-08-04 17:32:15 -07002072 if (btintel_test_flag(hdev, INTEL_BOOTLOADER)) {
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002073 struct hci_command_hdr *cmd = (void *)skb->data;
2074 __u16 opcode = le16_to_cpu(cmd->opcode);
2075
2076 /* When in bootloader mode and the command 0xfc09
2077 * is received, it needs to be send down the
2078 * bulk endpoint. So allocate a bulk URB instead.
2079 */
2080 if (opcode == 0xfc09)
2081 urb = alloc_bulk_urb(hdev, skb);
2082 else
2083 urb = alloc_ctrl_urb(hdev, skb);
2084
2085 /* When the 0xfc01 command is issued to boot into
2086 * the operational firmware, it will actually not
2087 * send a command complete event. To keep the flow
2088 * control working inject that event here.
2089 */
2090 if (opcode == 0xfc01)
2091 inject_cmd_complete(hdev, opcode);
2092 } else {
2093 urb = alloc_ctrl_urb(hdev, skb);
2094 }
2095 if (IS_ERR(urb))
2096 return PTR_ERR(urb);
2097
2098 hdev->stat.cmd_tx++;
2099 return submit_or_queue_tx_urb(hdev, urb);
2100
2101 case HCI_ACLDATA_PKT:
2102 urb = alloc_bulk_urb(hdev, skb);
2103 if (IS_ERR(urb))
2104 return PTR_ERR(urb);
2105
2106 hdev->stat.acl_tx++;
2107 return submit_or_queue_tx_urb(hdev, urb);
2108
2109 case HCI_SCODATA_PKT:
2110 if (hci_conn_num(hdev, SCO_LINK) < 1)
2111 return -ENODEV;
2112
2113 urb = alloc_isoc_urb(hdev, skb);
2114 if (IS_ERR(urb))
2115 return PTR_ERR(urb);
2116
2117 hdev->stat.sco_tx++;
2118 return submit_tx_urb(hdev, urb);
2119 }
2120
2121 return -EILSEQ;
2122}
2123
mark-yw.chene9478022021-08-04 17:06:47 +08002124/* UHW CR mapping */
2125#define MTK_BT_MISC 0x70002510
2126#define MTK_BT_SUBSYS_RST 0x70002610
2127#define MTK_UDMA_INT_STA_BT 0x74000024
2128#define MTK_UDMA_INT_STA_BT1 0x74000308
2129#define MTK_BT_WDT_STATUS 0x740003A0
2130#define MTK_EP_RST_OPT 0x74011890
2131#define MTK_EP_RST_IN_OUT_OPT 0x00010001
2132#define MTK_BT_RST_DONE 0x00000100
2133#define MTK_BT_RESET_WAIT_MS 100
2134#define MTK_BT_RESET_NUM_TRIES 10
mark-yw.chen5a876792021-09-01 11:32:25 +08002135
Sean Wanga1c49c432019-06-02 08:02:48 +08002136static void btusb_mtk_wmt_recv(struct urb *urb)
2137{
2138 struct hci_dev *hdev = urb->context;
2139 struct btusb_data *data = hci_get_drvdata(hdev);
2140 struct hci_event_hdr *hdr;
2141 struct sk_buff *skb;
2142 int err;
2143
2144 if (urb->status == 0 && urb->actual_length > 0) {
2145 hdev->stat.byte_rx += urb->actual_length;
2146
2147 /* WMT event shouldn't be fragmented and the size should be
2148 * less than HCI_WMT_MAX_EVENT_SIZE.
2149 */
2150 skb = bt_skb_alloc(HCI_WMT_MAX_EVENT_SIZE, GFP_ATOMIC);
2151 if (!skb) {
2152 hdev->stat.err_rx++;
Mark-YW.Chen60c6a632021-10-14 00:22:04 +08002153 kfree(urb->setup_packet);
Jupeng Zhongde71a6c2021-02-02 09:39:13 +08002154 return;
Sean Wanga1c49c432019-06-02 08:02:48 +08002155 }
2156
2157 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
2158 skb_put_data(skb, urb->transfer_buffer, urb->actual_length);
2159
2160 hdr = (void *)skb->data;
2161 /* Fix up the vendor event id with 0xff for vendor specific
2162 * instead of 0xe4 so that event send via monitoring socket can
2163 * be parsed properly.
2164 */
2165 hdr->evt = 0xff;
2166
2167 /* When someone waits for the WMT event, the skb is being cloned
2168 * and being processed the events from there then.
2169 */
2170 if (test_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags)) {
Johan Hovold22cc6b72019-11-28 19:24:27 +01002171 data->evt_skb = skb_clone(skb, GFP_ATOMIC);
Jupeng Zhongde71a6c2021-02-02 09:39:13 +08002172 if (!data->evt_skb) {
2173 kfree_skb(skb);
Mark-YW.Chen60c6a632021-10-14 00:22:04 +08002174 kfree(urb->setup_packet);
Jupeng Zhongde71a6c2021-02-02 09:39:13 +08002175 return;
2176 }
Sean Wanga1c49c432019-06-02 08:02:48 +08002177 }
2178
2179 err = hci_recv_frame(hdev, skb);
Jupeng Zhongde71a6c2021-02-02 09:39:13 +08002180 if (err < 0) {
2181 kfree_skb(data->evt_skb);
2182 data->evt_skb = NULL;
Mark-YW.Chen60c6a632021-10-14 00:22:04 +08002183 kfree(urb->setup_packet);
Jupeng Zhongde71a6c2021-02-02 09:39:13 +08002184 return;
2185 }
Sean Wanga1c49c432019-06-02 08:02:48 +08002186
2187 if (test_and_clear_bit(BTUSB_TX_WAIT_VND_EVT,
2188 &data->flags)) {
2189 /* Barrier to sync with other CPUs */
2190 smp_mb__after_atomic();
2191 wake_up_bit(&data->flags,
2192 BTUSB_TX_WAIT_VND_EVT);
2193 }
Mark-YW.Chen60c6a632021-10-14 00:22:04 +08002194 kfree(urb->setup_packet);
Sean Wanga1c49c432019-06-02 08:02:48 +08002195 return;
2196 } else if (urb->status == -ENOENT) {
2197 /* Avoid suspend failed when usb_kill_urb */
2198 return;
2199 }
2200
2201 usb_mark_last_busy(data->udev);
2202
2203 /* The URB complete handler is still called with urb->actual_length = 0
2204 * when the event is not available, so we should keep re-submitting
2205 * URB until WMT event returns, Also, It's necessary to wait some time
2206 * between the two consecutive control URBs to relax the target device
2207 * to generate the event. Otherwise, the WMT event cannot return from
2208 * the device successfully.
2209 */
Mark Chen48c13302021-02-02 18:26:17 +08002210 udelay(500);
Sean Wanga1c49c432019-06-02 08:02:48 +08002211
2212 usb_anchor_urb(urb, &data->ctrl_anchor);
2213 err = usb_submit_urb(urb, GFP_ATOMIC);
2214 if (err < 0) {
Mark-YW.Chen60c6a632021-10-14 00:22:04 +08002215 kfree(urb->setup_packet);
Sean Wanga1c49c432019-06-02 08:02:48 +08002216 /* -EPERM: urb is being killed;
2217 * -ENODEV: device got disconnected
2218 */
2219 if (err != -EPERM && err != -ENODEV)
2220 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
2221 urb, -err);
2222 usb_unanchor_urb(urb);
2223 }
2224}
2225
2226static int btusb_mtk_submit_wmt_recv_urb(struct hci_dev *hdev)
2227{
2228 struct btusb_data *data = hci_get_drvdata(hdev);
2229 struct usb_ctrlrequest *dr;
2230 unsigned char *buf;
2231 int err, size = 64;
2232 unsigned int pipe;
2233 struct urb *urb;
2234
2235 urb = usb_alloc_urb(0, GFP_KERNEL);
2236 if (!urb)
2237 return -ENOMEM;
2238
2239 dr = kmalloc(sizeof(*dr), GFP_KERNEL);
2240 if (!dr) {
2241 usb_free_urb(urb);
2242 return -ENOMEM;
2243 }
2244
2245 dr->bRequestType = USB_TYPE_VENDOR | USB_DIR_IN;
2246 dr->bRequest = 1;
2247 dr->wIndex = cpu_to_le16(0);
2248 dr->wValue = cpu_to_le16(48);
2249 dr->wLength = cpu_to_le16(size);
2250
2251 buf = kmalloc(size, GFP_KERNEL);
2252 if (!buf) {
2253 kfree(dr);
Dinghao Liud33fe772020-08-23 15:44:21 +08002254 usb_free_urb(urb);
Sean Wanga1c49c432019-06-02 08:02:48 +08002255 return -ENOMEM;
2256 }
2257
2258 pipe = usb_rcvctrlpipe(data->udev, 0);
2259
2260 usb_fill_control_urb(urb, data->udev, pipe, (void *)dr,
2261 buf, size, btusb_mtk_wmt_recv, hdev);
2262
2263 urb->transfer_flags |= URB_FREE_BUFFER;
2264
2265 usb_anchor_urb(urb, &data->ctrl_anchor);
2266 err = usb_submit_urb(urb, GFP_KERNEL);
2267 if (err < 0) {
2268 if (err != -EPERM && err != -ENODEV)
2269 bt_dev_err(hdev, "urb %p submission failed (%d)",
2270 urb, -err);
2271 usb_unanchor_urb(urb);
2272 }
2273
2274 usb_free_urb(urb);
2275
2276 return err;
2277}
2278
2279static int btusb_mtk_hci_wmt_sync(struct hci_dev *hdev,
2280 struct btmtk_hci_wmt_params *wmt_params)
2281{
2282 struct btusb_data *data = hci_get_drvdata(hdev);
2283 struct btmtk_hci_wmt_evt_funcc *wmt_evt_funcc;
2284 u32 hlen, status = BTMTK_WMT_INVALID;
2285 struct btmtk_hci_wmt_evt *wmt_evt;
Arnd Bergmann10888142021-02-04 16:47:07 +01002286 struct btmtk_hci_wmt_cmd *wc;
Sean Wanga1c49c432019-06-02 08:02:48 +08002287 struct btmtk_wmt_hdr *hdr;
2288 int err;
2289
Sean Wanga1c49c432019-06-02 08:02:48 +08002290 /* Send the WMT command and wait until the WMT event returns */
2291 hlen = sizeof(*hdr) + wmt_params->dlen;
2292 if (hlen > 255)
2293 return -EINVAL;
2294
Arnd Bergmann10888142021-02-04 16:47:07 +01002295 wc = kzalloc(hlen, GFP_KERNEL);
2296 if (!wc)
2297 return -ENOMEM;
2298
2299 hdr = &wc->hdr;
Sean Wanga1c49c432019-06-02 08:02:48 +08002300 hdr->dir = 1;
2301 hdr->op = wmt_params->op;
2302 hdr->dlen = cpu_to_le16(wmt_params->dlen + 1);
2303 hdr->flag = wmt_params->flag;
Arnd Bergmann10888142021-02-04 16:47:07 +01002304 memcpy(wc->data, wmt_params->data, wmt_params->dlen);
Sean Wanga1c49c432019-06-02 08:02:48 +08002305
2306 set_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
2307
Arnd Bergmann10888142021-02-04 16:47:07 +01002308 err = __hci_cmd_send(hdev, 0xfc6f, hlen, wc);
Sean Wanga1c49c432019-06-02 08:02:48 +08002309
2310 if (err < 0) {
2311 clear_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
Arnd Bergmann10888142021-02-04 16:47:07 +01002312 goto err_free_wc;
Sean Wanga1c49c432019-06-02 08:02:48 +08002313 }
2314
mark-yw.chen8454ed92021-04-12 23:06:26 +08002315 /* Submit control IN URB on demand to process the WMT event */
2316 err = btusb_mtk_submit_wmt_recv_urb(hdev);
2317 if (err < 0)
Muhammad Usama Anjum44e936d2021-04-13 22:52:08 +05002318 goto err_free_wc;
mark-yw.chen8454ed92021-04-12 23:06:26 +08002319
Sean Wanga1c49c432019-06-02 08:02:48 +08002320 /* The vendor specific WMT commands are all answered by a vendor
2321 * specific event and will have the Command Status or Command
2322 * Complete as with usual HCI command flow control.
2323 *
2324 * After sending the command, wait for BTUSB_TX_WAIT_VND_EVT
2325 * state to be cleared. The driver specific event receive routine
2326 * will clear that state and with that indicate completion of the
2327 * WMT command.
2328 */
2329 err = wait_on_bit_timeout(&data->flags, BTUSB_TX_WAIT_VND_EVT,
2330 TASK_INTERRUPTIBLE, HCI_INIT_TIMEOUT);
2331 if (err == -EINTR) {
2332 bt_dev_err(hdev, "Execution of wmt command interrupted");
2333 clear_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
Arnd Bergmann10888142021-02-04 16:47:07 +01002334 goto err_free_wc;
Sean Wanga1c49c432019-06-02 08:02:48 +08002335 }
2336
2337 if (err) {
2338 bt_dev_err(hdev, "Execution of wmt command timed out");
2339 clear_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
Arnd Bergmann10888142021-02-04 16:47:07 +01002340 err = -ETIMEDOUT;
2341 goto err_free_wc;
Sean Wanga1c49c432019-06-02 08:02:48 +08002342 }
2343
2344 /* Parse and handle the return WMT event */
2345 wmt_evt = (struct btmtk_hci_wmt_evt *)data->evt_skb->data;
2346 if (wmt_evt->whdr.op != hdr->op) {
2347 bt_dev_err(hdev, "Wrong op received %d expected %d",
2348 wmt_evt->whdr.op, hdr->op);
2349 err = -EIO;
2350 goto err_free_skb;
2351 }
2352
2353 switch (wmt_evt->whdr.op) {
2354 case BTMTK_WMT_SEMAPHORE:
2355 if (wmt_evt->whdr.flag == 2)
2356 status = BTMTK_WMT_PATCH_UNDONE;
2357 else
2358 status = BTMTK_WMT_PATCH_DONE;
2359 break;
2360 case BTMTK_WMT_FUNC_CTRL:
2361 wmt_evt_funcc = (struct btmtk_hci_wmt_evt_funcc *)wmt_evt;
2362 if (be16_to_cpu(wmt_evt_funcc->status) == 0x404)
2363 status = BTMTK_WMT_ON_DONE;
2364 else if (be16_to_cpu(wmt_evt_funcc->status) == 0x420)
2365 status = BTMTK_WMT_ON_PROGRESS;
2366 else
2367 status = BTMTK_WMT_ON_UNDONE;
2368 break;
Mark Chenfc342c42021-02-02 18:26:18 +08002369 case BTMTK_WMT_PATCH_DWNLD:
2370 if (wmt_evt->whdr.flag == 2)
2371 status = BTMTK_WMT_PATCH_DONE;
2372 else if (wmt_evt->whdr.flag == 1)
2373 status = BTMTK_WMT_PATCH_PROGRESS;
2374 else
2375 status = BTMTK_WMT_PATCH_UNDONE;
2376 break;
Sean Wanga1c49c432019-06-02 08:02:48 +08002377 }
2378
2379 if (wmt_params->status)
2380 *wmt_params->status = status;
2381
2382err_free_skb:
2383 kfree_skb(data->evt_skb);
2384 data->evt_skb = NULL;
Arnd Bergmann10888142021-02-04 16:47:07 +01002385err_free_wc:
2386 kfree(wc);
Sean Wanga1c49c432019-06-02 08:02:48 +08002387 return err;
2388}
2389
Sean Wanga1c49c432019-06-02 08:02:48 +08002390static int btusb_mtk_func_query(struct hci_dev *hdev)
2391{
2392 struct btmtk_hci_wmt_params wmt_params;
2393 int status, err;
2394 u8 param = 0;
2395
2396 /* Query whether the function is enabled */
2397 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
2398 wmt_params.flag = 4;
2399 wmt_params.dlen = sizeof(param);
2400 wmt_params.data = &param;
2401 wmt_params.status = &status;
2402
2403 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
2404 if (err < 0) {
2405 bt_dev_err(hdev, "Failed to query function status (%d)", err);
2406 return err;
2407 }
2408
2409 return status;
2410}
2411
mark-yw.chene9478022021-08-04 17:06:47 +08002412static int btusb_mtk_uhw_reg_write(struct btusb_data *data, u32 reg, u32 val)
2413{
2414 struct hci_dev *hdev = data->hdev;
2415 int pipe, err;
2416 void *buf;
2417
2418 buf = kzalloc(4, GFP_KERNEL);
2419 if (!buf)
2420 return -ENOMEM;
2421
2422 put_unaligned_le32(val, buf);
2423
2424 pipe = usb_sndctrlpipe(data->udev, 0);
2425 err = usb_control_msg(data->udev, pipe, 0x02,
2426 0x5E,
2427 reg >> 16, reg & 0xffff,
2428 buf, 4, USB_CTRL_SET_TIMEOUT);
2429 if (err < 0) {
2430 bt_dev_err(hdev, "Failed to write uhw reg(%d)", err);
2431 goto err_free_buf;
2432 }
2433
2434err_free_buf:
2435 kfree(buf);
2436
2437 return err;
2438}
2439
2440static int btusb_mtk_uhw_reg_read(struct btusb_data *data, u32 reg, u32 *val)
2441{
2442 struct hci_dev *hdev = data->hdev;
2443 int pipe, err;
2444 void *buf;
2445
2446 buf = kzalloc(4, GFP_KERNEL);
2447 if (!buf)
2448 return -ENOMEM;
2449
2450 pipe = usb_rcvctrlpipe(data->udev, 0);
2451 err = usb_control_msg(data->udev, pipe, 0x01,
2452 0xDE,
2453 reg >> 16, reg & 0xffff,
2454 buf, 4, USB_CTRL_SET_TIMEOUT);
2455 if (err < 0) {
2456 bt_dev_err(hdev, "Failed to read uhw reg(%d)", err);
2457 goto err_free_buf;
2458 }
2459
2460 *val = get_unaligned_le32(buf);
2461 bt_dev_dbg(hdev, "reg=%x, value=0x%08x", reg, *val);
2462
2463err_free_buf:
2464 kfree(buf);
2465
2466 return err;
2467}
2468
Sean Wanga1c49c432019-06-02 08:02:48 +08002469static int btusb_mtk_reg_read(struct btusb_data *data, u32 reg, u32 *val)
2470{
2471 int pipe, err, size = sizeof(u32);
2472 void *buf;
2473
2474 buf = kzalloc(size, GFP_KERNEL);
2475 if (!buf)
2476 return -ENOMEM;
2477
2478 pipe = usb_rcvctrlpipe(data->udev, 0);
2479 err = usb_control_msg(data->udev, pipe, 0x63,
2480 USB_TYPE_VENDOR | USB_DIR_IN,
2481 reg >> 16, reg & 0xffff,
2482 buf, size, USB_CTRL_SET_TIMEOUT);
2483 if (err < 0)
2484 goto err_free_buf;
2485
2486 *val = get_unaligned_le32(buf);
2487
2488err_free_buf:
2489 kfree(buf);
2490
2491 return err;
2492}
2493
Mark Chen48c13302021-02-02 18:26:17 +08002494static int btusb_mtk_id_get(struct btusb_data *data, u32 reg, u32 *id)
Sean Wanga1c49c432019-06-02 08:02:48 +08002495{
Mark Chen48c13302021-02-02 18:26:17 +08002496 return btusb_mtk_reg_read(data, reg, id);
Sean Wanga1c49c432019-06-02 08:02:48 +08002497}
2498
2499static int btusb_mtk_setup(struct hci_dev *hdev)
2500{
2501 struct btusb_data *data = hci_get_drvdata(hdev);
2502 struct btmtk_hci_wmt_params wmt_params;
2503 ktime_t calltime, delta, rettime;
2504 struct btmtk_tci_sleep tci_sleep;
2505 unsigned long long duration;
2506 struct sk_buff *skb;
2507 const char *fwname;
2508 int err, status;
2509 u32 dev_id;
Mark Chenfc342c42021-02-02 18:26:18 +08002510 char fw_bin_name[64];
mark-yw.chen201cf392021-02-23 14:27:40 +08002511 u32 fw_version = 0;
Sean Wanga1c49c432019-06-02 08:02:48 +08002512 u8 param;
2513
2514 calltime = ktime_get();
2515
Mark Chen48c13302021-02-02 18:26:17 +08002516 err = btusb_mtk_id_get(data, 0x80000008, &dev_id);
Sean Wanga1c49c432019-06-02 08:02:48 +08002517 if (err < 0) {
2518 bt_dev_err(hdev, "Failed to get device id (%d)", err);
2519 return err;
2520 }
2521
Mark Chenfc342c42021-02-02 18:26:18 +08002522 if (!dev_id) {
2523 err = btusb_mtk_id_get(data, 0x70010200, &dev_id);
2524 if (err < 0) {
2525 bt_dev_err(hdev, "Failed to get device id (%d)", err);
2526 return err;
2527 }
2528 err = btusb_mtk_id_get(data, 0x80021004, &fw_version);
2529 if (err < 0) {
2530 bt_dev_err(hdev, "Failed to get fw version (%d)", err);
2531 return err;
2532 }
2533 }
2534
Sean Wanga1c49c432019-06-02 08:02:48 +08002535 switch (dev_id) {
Sean Wang9ce67c32019-06-02 08:02:49 +08002536 case 0x7663:
2537 fwname = FIRMWARE_MT7663;
2538 break;
Sean Wanga1c49c432019-06-02 08:02:48 +08002539 case 0x7668:
2540 fwname = FIRMWARE_MT7668;
2541 break;
mark-yw.chen09a19d62021-09-01 11:32:26 +08002542 case 0x7922:
Mark Chenfc342c42021-02-02 18:26:18 +08002543 case 0x7961:
2544 snprintf(fw_bin_name, sizeof(fw_bin_name),
2545 "mediatek/BT_RAM_CODE_MT%04x_1_%x_hdr.bin",
2546 dev_id & 0xffff, (fw_version & 0xff) + 1);
Sean Wang8c0d17b2021-10-19 05:30:12 +08002547 err = btmtk_setup_firmware_79xx(hdev, fw_bin_name,
2548 btusb_mtk_hci_wmt_sync);
Mark Chenfc342c42021-02-02 18:26:18 +08002549
mark-yw.chene9478022021-08-04 17:06:47 +08002550 /* It's Device EndPoint Reset Option Register */
2551 btusb_mtk_uhw_reg_write(data, MTK_EP_RST_OPT, MTK_EP_RST_IN_OUT_OPT);
2552
Mark Chenfc342c42021-02-02 18:26:18 +08002553 /* Enable Bluetooth protocol */
2554 param = 1;
2555 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
2556 wmt_params.flag = 0;
2557 wmt_params.dlen = sizeof(param);
2558 wmt_params.data = &param;
2559 wmt_params.status = NULL;
2560
2561 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
2562 if (err < 0) {
2563 bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
2564 return err;
2565 }
mark-yw.chen654e6f72021-08-02 20:59:41 +08002566
2567 hci_set_msft_opcode(hdev, 0xFD30);
Mark Chenfc342c42021-02-02 18:26:18 +08002568 goto done;
Sean Wanga1c49c432019-06-02 08:02:48 +08002569 default:
Jupeng Zhonga297f562021-02-03 22:28:46 +08002570 bt_dev_err(hdev, "Unsupported hardware variant (%08x)",
Sean Wanga1c49c432019-06-02 08:02:48 +08002571 dev_id);
2572 return -ENODEV;
2573 }
2574
2575 /* Query whether the firmware is already download */
2576 wmt_params.op = BTMTK_WMT_SEMAPHORE;
2577 wmt_params.flag = 1;
2578 wmt_params.dlen = 0;
2579 wmt_params.data = NULL;
2580 wmt_params.status = &status;
2581
2582 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
2583 if (err < 0) {
2584 bt_dev_err(hdev, "Failed to query firmware status (%d)", err);
2585 return err;
2586 }
2587
2588 if (status == BTMTK_WMT_PATCH_DONE) {
2589 bt_dev_info(hdev, "firmware already downloaded");
2590 goto ignore_setup_fw;
2591 }
2592
2593 /* Setup a firmware which the device definitely requires */
Sean Wang8c0d17b2021-10-19 05:30:12 +08002594 err = btmtk_setup_firmware(hdev, fwname,
2595 btusb_mtk_hci_wmt_sync);
Sean Wanga1c49c432019-06-02 08:02:48 +08002596 if (err < 0)
2597 return err;
2598
2599ignore_setup_fw:
2600 err = readx_poll_timeout(btusb_mtk_func_query, hdev, status,
2601 status < 0 || status != BTMTK_WMT_ON_PROGRESS,
2602 2000, 5000000);
2603 /* -ETIMEDOUT happens */
2604 if (err < 0)
2605 return err;
2606
2607 /* The other errors happen in btusb_mtk_func_query */
2608 if (status < 0)
2609 return status;
2610
2611 if (status == BTMTK_WMT_ON_DONE) {
2612 bt_dev_info(hdev, "function already on");
2613 goto ignore_func_on;
2614 }
2615
2616 /* Enable Bluetooth protocol */
2617 param = 1;
2618 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
2619 wmt_params.flag = 0;
2620 wmt_params.dlen = sizeof(param);
2621 wmt_params.data = &param;
2622 wmt_params.status = NULL;
2623
2624 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
2625 if (err < 0) {
2626 bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
2627 return err;
2628 }
2629
2630ignore_func_on:
2631 /* Apply the low power environment setup */
2632 tci_sleep.mode = 0x5;
2633 tci_sleep.duration = cpu_to_le16(0x640);
2634 tci_sleep.host_duration = cpu_to_le16(0x640);
2635 tci_sleep.host_wakeup_pin = 0;
2636 tci_sleep.time_compensation = 0;
2637
2638 skb = __hci_cmd_sync(hdev, 0xfc7a, sizeof(tci_sleep), &tci_sleep,
2639 HCI_INIT_TIMEOUT);
2640 if (IS_ERR(skb)) {
2641 err = PTR_ERR(skb);
2642 bt_dev_err(hdev, "Failed to apply low power setting (%d)", err);
2643 return err;
2644 }
2645 kfree_skb(skb);
2646
Mark Chenfc342c42021-02-02 18:26:18 +08002647done:
Sean Wanga1c49c432019-06-02 08:02:48 +08002648 rettime = ktime_get();
2649 delta = ktime_sub(rettime, calltime);
2650 duration = (unsigned long long)ktime_to_ns(delta) >> 10;
2651
2652 bt_dev_info(hdev, "Device setup in %llu usecs", duration);
2653
2654 return 0;
2655}
2656
2657static int btusb_mtk_shutdown(struct hci_dev *hdev)
2658{
2659 struct btmtk_hci_wmt_params wmt_params;
2660 u8 param = 0;
2661 int err;
2662
2663 /* Disable the device */
2664 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
2665 wmt_params.flag = 0;
2666 wmt_params.dlen = sizeof(param);
2667 wmt_params.data = &param;
2668 wmt_params.status = NULL;
2669
2670 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
2671 if (err < 0) {
2672 bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
2673 return err;
2674 }
2675
2676 return 0;
2677}
2678
mark-yw.chene9478022021-08-04 17:06:47 +08002679static void btusb_mtk_cmd_timeout(struct hci_dev *hdev)
2680{
2681 struct btusb_data *data = hci_get_drvdata(hdev);
2682 u32 val;
2683 int err, retry = 0;
2684
2685 /* It's MediaTek specific bluetooth reset mechanism via USB */
2686 if (test_and_set_bit(BTUSB_HW_RESET_ACTIVE, &data->flags)) {
2687 bt_dev_err(hdev, "last reset failed? Not resetting again");
2688 return;
2689 }
2690
2691 err = usb_autopm_get_interface(data->intf);
2692 if (err < 0)
2693 return;
2694
2695 btusb_stop_traffic(data);
2696 usb_kill_anchored_urbs(&data->tx_anchor);
2697
2698 /* It's Device EndPoint Reset Option Register */
2699 bt_dev_dbg(hdev, "Initiating reset mechanism via uhw");
2700 btusb_mtk_uhw_reg_write(data, MTK_EP_RST_OPT, MTK_EP_RST_IN_OUT_OPT);
2701 btusb_mtk_uhw_reg_read(data, MTK_BT_WDT_STATUS, &val);
2702
2703 /* Reset the bluetooth chip via USB interface. */
2704 btusb_mtk_uhw_reg_write(data, MTK_BT_SUBSYS_RST, 1);
2705 btusb_mtk_uhw_reg_write(data, MTK_UDMA_INT_STA_BT, 0x000000FF);
2706 btusb_mtk_uhw_reg_read(data, MTK_UDMA_INT_STA_BT, &val);
2707 btusb_mtk_uhw_reg_write(data, MTK_UDMA_INT_STA_BT1, 0x000000FF);
2708 btusb_mtk_uhw_reg_read(data, MTK_UDMA_INT_STA_BT1, &val);
2709 /* MT7921 need to delay 20ms between toggle reset bit */
2710 msleep(20);
2711 btusb_mtk_uhw_reg_write(data, MTK_BT_SUBSYS_RST, 0);
2712 btusb_mtk_uhw_reg_read(data, MTK_BT_SUBSYS_RST, &val);
2713
2714 /* Poll the register until reset is completed */
2715 do {
2716 btusb_mtk_uhw_reg_read(data, MTK_BT_MISC, &val);
2717 if (val & MTK_BT_RST_DONE) {
2718 bt_dev_dbg(hdev, "Bluetooth Reset Successfully");
2719 break;
2720 }
2721
2722 bt_dev_dbg(hdev, "Polling Bluetooth Reset CR");
2723 retry++;
2724 msleep(MTK_BT_RESET_WAIT_MS);
2725 } while (retry < MTK_BT_RESET_NUM_TRIES);
2726
2727 btusb_mtk_id_get(data, 0x70010200, &val);
2728 if (!val)
2729 bt_dev_err(hdev, "Can't get device id, subsys reset fail.");
2730
2731 usb_queue_reset_device(data->intf);
2732
2733 clear_bit(BTUSB_HW_RESET_ACTIVE, &data->flags);
2734}
2735
mark-yw.chen0b10c8c2021-08-04 17:03:15 +08002736static int btusb_recv_acl_mtk(struct hci_dev *hdev, struct sk_buff *skb)
2737{
2738 struct btusb_data *data = hci_get_drvdata(hdev);
2739 u16 handle = le16_to_cpu(hci_acl_hdr(skb)->handle);
2740
2741 switch (handle) {
2742 case 0xfc6f: /* Firmware dump from device */
2743 /* When the firmware hangs, the device can no longer
2744 * suspend and thus disable auto-suspend.
2745 */
2746 usb_disable_autosuspend(data->udev);
mark-yw.chen146af222021-08-06 09:35:16 +08002747 fallthrough;
mark-yw.chen0b10c8c2021-08-04 17:03:15 +08002748 case 0x05ff: /* Firmware debug logging 1 */
2749 case 0x05fe: /* Firmware debug logging 2 */
2750 return hci_recv_diag(hdev, skb);
2751 }
2752
2753 return hci_recv_frame(hdev, skb);
2754}
2755
Rajat Jaina4ccc9e2017-02-01 14:24:10 -08002756#ifdef CONFIG_PM
2757/* Configure an out-of-band gpio as wake-up pin, if specified in device tree */
2758static int marvell_config_oob_wake(struct hci_dev *hdev)
2759{
2760 struct sk_buff *skb;
2761 struct btusb_data *data = hci_get_drvdata(hdev);
2762 struct device *dev = &data->udev->dev;
2763 u16 pin, gap, opcode;
2764 int ret;
2765 u8 cmd[5];
2766
2767 /* Move on if no wakeup pin specified */
2768 if (of_property_read_u16(dev->of_node, "marvell,wakeup-pin", &pin) ||
2769 of_property_read_u16(dev->of_node, "marvell,wakeup-gap-ms", &gap))
2770 return 0;
2771
2772 /* Vendor specific command to configure a GPIO as wake-up pin */
2773 opcode = hci_opcode_pack(0x3F, 0x59);
2774 cmd[0] = opcode & 0xFF;
2775 cmd[1] = opcode >> 8;
2776 cmd[2] = 2; /* length of parameters that follow */
2777 cmd[3] = pin;
2778 cmd[4] = gap; /* time in ms, for which wakeup pin should be asserted */
2779
2780 skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL);
2781 if (!skb) {
Joe Perchesd84fc2c2020-12-22 02:31:49 -08002782 bt_dev_err(hdev, "%s: No memory", __func__);
Rajat Jaina4ccc9e2017-02-01 14:24:10 -08002783 return -ENOMEM;
2784 }
2785
Johannes Berg59ae1d12017-06-16 14:29:20 +02002786 skb_put_data(skb, cmd, sizeof(cmd));
Rajat Jaina4ccc9e2017-02-01 14:24:10 -08002787 hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
2788
2789 ret = btusb_send_frame(hdev, skb);
2790 if (ret) {
Joe Perchesd84fc2c2020-12-22 02:31:49 -08002791 bt_dev_err(hdev, "%s: configuration failed", __func__);
Rajat Jaina4ccc9e2017-02-01 14:24:10 -08002792 kfree_skb(skb);
2793 return ret;
2794 }
2795
2796 return 0;
2797}
2798#endif
2799
Amitkumar Karwarae8df492014-07-18 14:47:06 -07002800static int btusb_set_bdaddr_marvell(struct hci_dev *hdev,
2801 const bdaddr_t *bdaddr)
2802{
2803 struct sk_buff *skb;
2804 u8 buf[8];
2805 long ret;
2806
2807 buf[0] = 0xfe;
2808 buf[1] = sizeof(bdaddr_t);
2809 memcpy(buf + 2, bdaddr, sizeof(bdaddr_t));
2810
2811 skb = __hci_cmd_sync(hdev, 0xfc22, sizeof(buf), buf, HCI_INIT_TIMEOUT);
2812 if (IS_ERR(skb)) {
2813 ret = PTR_ERR(skb);
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002814 bt_dev_err(hdev, "changing Marvell device address failed (%ld)",
2815 ret);
Amitkumar Karwarae8df492014-07-18 14:47:06 -07002816 return ret;
2817 }
2818 kfree_skb(skb);
2819
2820 return 0;
2821}
2822
Toshi Kikuchi58592232014-12-12 10:58:05 -08002823static int btusb_set_bdaddr_ath3012(struct hci_dev *hdev,
2824 const bdaddr_t *bdaddr)
2825{
2826 struct sk_buff *skb;
2827 u8 buf[10];
2828 long ret;
2829
2830 buf[0] = 0x01;
2831 buf[1] = 0x01;
2832 buf[2] = 0x00;
2833 buf[3] = sizeof(bdaddr_t);
2834 memcpy(buf + 4, bdaddr, sizeof(bdaddr_t));
2835
2836 skb = __hci_cmd_sync(hdev, 0xfc0b, sizeof(buf), buf, HCI_INIT_TIMEOUT);
2837 if (IS_ERR(skb)) {
2838 ret = PTR_ERR(skb);
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002839 bt_dev_err(hdev, "Change address command failed (%ld)", ret);
Toshi Kikuchi58592232014-12-12 10:58:05 -08002840 return ret;
2841 }
2842 kfree_skb(skb);
2843
2844 return 0;
2845}
2846
Rocky Liaob40f58b2020-09-29 12:23:51 +08002847static int btusb_set_bdaddr_wcn6855(struct hci_dev *hdev,
2848 const bdaddr_t *bdaddr)
2849{
2850 struct sk_buff *skb;
2851 u8 buf[6];
2852 long ret;
2853
2854 memcpy(buf, bdaddr, sizeof(bdaddr_t));
2855
2856 skb = __hci_cmd_sync_ev(hdev, 0xfc14, sizeof(buf), buf,
2857 HCI_EV_CMD_COMPLETE, HCI_INIT_TIMEOUT);
2858 if (IS_ERR(skb)) {
2859 ret = PTR_ERR(skb);
2860 bt_dev_err(hdev, "Change address command failed (%ld)", ret);
2861 return ret;
2862 }
2863 kfree_skb(skb);
2864
2865 return 0;
2866}
2867
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002868#define QCA_DFU_PACKET_LEN 4096
2869
2870#define QCA_GET_TARGET_VERSION 0x09
2871#define QCA_CHECK_STATUS 0x05
2872#define QCA_DFU_DOWNLOAD 0x01
2873
2874#define QCA_SYSCFG_UPDATED 0x40
2875#define QCA_PATCH_UPDATED 0x80
2876#define QCA_DFU_TIMEOUT 3000
Tim Jiangef2862a2020-12-03 12:57:14 +08002877#define QCA_FLAG_MULTI_NVM 0x80
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002878
2879struct qca_version {
2880 __le32 rom_version;
2881 __le32 patch_version;
2882 __le32 ram_version;
Tim Jiangef2862a2020-12-03 12:57:14 +08002883 __le16 board_id;
2884 __le16 flag;
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002885 __u8 reserved[4];
2886} __packed;
2887
2888struct qca_rampatch_version {
Rocky Liaob40f58b2020-09-29 12:23:51 +08002889 __le16 rom_version_high;
2890 __le16 rom_version_low;
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002891 __le16 patch_version;
2892} __packed;
2893
2894struct qca_device_info {
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002895 u32 rom_version;
2896 u8 rampatch_hdr; /* length of header in rampatch */
2897 u8 nvm_hdr; /* length of header in NVM */
2898 u8 ver_offset; /* offset of version structure in rampatch */
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002899};
2900
2901static const struct qca_device_info qca_devices_table[] = {
Rocky Liaob40f58b2020-09-29 12:23:51 +08002902 { 0x00000100, 20, 4, 8 }, /* Rome 1.0 */
2903 { 0x00000101, 20, 4, 8 }, /* Rome 1.1 */
2904 { 0x00000200, 28, 4, 16 }, /* Rome 2.0 */
2905 { 0x00000201, 28, 4, 16 }, /* Rome 2.1 */
2906 { 0x00000300, 28, 4, 16 }, /* Rome 3.0 */
2907 { 0x00000302, 28, 4, 16 }, /* Rome 3.2 */
2908 { 0x00130100, 40, 4, 16 }, /* WCN6855 1.0 */
2909 { 0x00130200, 40, 4, 16 }, /* WCN6855 2.0 */
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002910};
2911
Takashi Iwai803cdb82018-05-21 22:34:52 +02002912static int btusb_qca_send_vendor_req(struct usb_device *udev, u8 request,
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002913 void *data, u16 size)
2914{
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002915 int pipe, err;
2916 u8 *buf;
2917
2918 buf = kmalloc(size, GFP_KERNEL);
2919 if (!buf)
2920 return -ENOMEM;
2921
2922 /* Found some of USB hosts have IOT issues with ours so that we should
2923 * not wait until HCI layer is ready.
2924 */
2925 pipe = usb_rcvctrlpipe(udev, 0);
2926 err = usb_control_msg(udev, pipe, request, USB_TYPE_VENDOR | USB_DIR_IN,
2927 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
2928 if (err < 0) {
Takashi Iwai803cdb82018-05-21 22:34:52 +02002929 dev_err(&udev->dev, "Failed to access otp area (%d)", err);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002930 goto done;
2931 }
2932
2933 memcpy(data, buf, size);
2934
2935done:
2936 kfree(buf);
2937
2938 return err;
2939}
2940
2941static int btusb_setup_qca_download_fw(struct hci_dev *hdev,
2942 const struct firmware *firmware,
2943 size_t hdr_size)
2944{
2945 struct btusb_data *btdata = hci_get_drvdata(hdev);
2946 struct usb_device *udev = btdata->udev;
2947 size_t count, size, sent = 0;
2948 int pipe, len, err;
2949 u8 *buf;
2950
2951 buf = kmalloc(QCA_DFU_PACKET_LEN, GFP_KERNEL);
2952 if (!buf)
2953 return -ENOMEM;
2954
2955 count = firmware->size;
2956
2957 size = min_t(size_t, count, hdr_size);
2958 memcpy(buf, firmware->data, size);
2959
2960 /* USB patches should go down to controller through USB path
2961 * because binary format fits to go down through USB channel.
2962 * USB control path is for patching headers and USB bulk is for
2963 * patch body.
2964 */
2965 pipe = usb_sndctrlpipe(udev, 0);
2966 err = usb_control_msg(udev, pipe, QCA_DFU_DOWNLOAD, USB_TYPE_VENDOR,
2967 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
2968 if (err < 0) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002969 bt_dev_err(hdev, "Failed to send headers (%d)", err);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002970 goto done;
2971 }
2972
2973 sent += size;
2974 count -= size;
2975
Tim Jiang4f00bfb2021-06-01 17:57:10 +08002976 /* ep2 need time to switch from function acl to function dfu,
2977 * so we add 20ms delay here.
2978 */
2979 msleep(20);
2980
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002981 while (count) {
2982 size = min_t(size_t, count, QCA_DFU_PACKET_LEN);
2983
2984 memcpy(buf, firmware->data + sent, size);
2985
2986 pipe = usb_sndbulkpipe(udev, 0x02);
2987 err = usb_bulk_msg(udev, pipe, buf, size, &len,
2988 QCA_DFU_TIMEOUT);
2989 if (err < 0) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002990 bt_dev_err(hdev, "Failed to send body at %zd of %zd (%d)",
2991 sent, firmware->size, err);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002992 break;
2993 }
2994
2995 if (size != len) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002996 bt_dev_err(hdev, "Failed to get bulk buffer");
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002997 err = -EILSEQ;
2998 break;
2999 }
3000
3001 sent += size;
3002 count -= size;
3003 }
3004
3005done:
3006 kfree(buf);
3007 return err;
3008}
3009
3010static int btusb_setup_qca_load_rampatch(struct hci_dev *hdev,
3011 struct qca_version *ver,
3012 const struct qca_device_info *info)
3013{
3014 struct qca_rampatch_version *rver;
3015 const struct firmware *fw;
Rocky Liaob40f58b2020-09-29 12:23:51 +08003016 u32 ver_rom, ver_patch, rver_rom;
3017 u16 rver_rom_low, rver_rom_high, rver_patch;
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003018 char fwname[64];
3019 int err;
3020
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00003021 ver_rom = le32_to_cpu(ver->rom_version);
3022 ver_patch = le32_to_cpu(ver->patch_version);
3023
3024 snprintf(fwname, sizeof(fwname), "qca/rampatch_usb_%08x.bin", ver_rom);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003025
3026 err = request_firmware(&fw, fwname, &hdev->dev);
3027 if (err) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01003028 bt_dev_err(hdev, "failed to request rampatch file: %s (%d)",
3029 fwname, err);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003030 return err;
3031 }
3032
Marcel Holtmann2064ee32017-10-30 10:42:59 +01003033 bt_dev_info(hdev, "using rampatch file: %s", fwname);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003034
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00003035 rver = (struct qca_rampatch_version *)(fw->data + info->ver_offset);
Rocky Liaob40f58b2020-09-29 12:23:51 +08003036 rver_rom_low = le16_to_cpu(rver->rom_version_low);
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00003037 rver_patch = le16_to_cpu(rver->patch_version);
3038
Rocky Liaob40f58b2020-09-29 12:23:51 +08003039 if (ver_rom & ~0xffffU) {
3040 rver_rom_high = le16_to_cpu(rver->rom_version_high);
3041 rver_rom = le32_to_cpu(rver_rom_high << 16 | rver_rom_low);
3042 } else {
3043 rver_rom = rver_rom_low;
3044 }
3045
Marcel Holtmann2064ee32017-10-30 10:42:59 +01003046 bt_dev_info(hdev, "QCA: patch rome 0x%x build 0x%x, "
3047 "firmware rome 0x%x build 0x%x",
3048 rver_rom, rver_patch, ver_rom, ver_patch);
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00003049
3050 if (rver_rom != ver_rom || rver_patch <= ver_patch) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01003051 bt_dev_err(hdev, "rampatch file version did not match with firmware");
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003052 err = -EINVAL;
3053 goto done;
3054 }
3055
3056 err = btusb_setup_qca_download_fw(hdev, fw, info->rampatch_hdr);
3057
3058done:
3059 release_firmware(fw);
3060
3061 return err;
3062}
3063
3064static int btusb_setup_qca_load_nvm(struct hci_dev *hdev,
3065 struct qca_version *ver,
3066 const struct qca_device_info *info)
3067{
3068 const struct firmware *fw;
3069 char fwname[64];
3070 int err;
3071
Tim Jiangef2862a2020-12-03 12:57:14 +08003072 if (((ver->flag >> 8) & 0xff) == QCA_FLAG_MULTI_NVM) {
Tim Jiangca17a5c2021-06-01 17:57:25 +08003073 /* if boardid equal 0, use default nvm without surfix */
3074 if (le16_to_cpu(ver->board_id) == 0x0) {
3075 snprintf(fwname, sizeof(fwname), "qca/nvm_usb_%08x.bin",
3076 le32_to_cpu(ver->rom_version));
3077 } else {
3078 snprintf(fwname, sizeof(fwname), "qca/nvm_usb_%08x_%04x.bin",
3079 le32_to_cpu(ver->rom_version),
3080 le16_to_cpu(ver->board_id));
3081 }
Tim Jiangef2862a2020-12-03 12:57:14 +08003082 } else {
3083 snprintf(fwname, sizeof(fwname), "qca/nvm_usb_%08x.bin",
3084 le32_to_cpu(ver->rom_version));
3085 }
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003086
3087 err = request_firmware(&fw, fwname, &hdev->dev);
3088 if (err) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01003089 bt_dev_err(hdev, "failed to request NVM file: %s (%d)",
3090 fwname, err);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003091 return err;
3092 }
3093
Marcel Holtmann2064ee32017-10-30 10:42:59 +01003094 bt_dev_info(hdev, "using NVM file: %s", fwname);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003095
3096 err = btusb_setup_qca_download_fw(hdev, fw, info->nvm_hdr);
3097
3098 release_firmware(fw);
3099
3100 return err;
3101}
3102
Takashi Iwai803cdb82018-05-21 22:34:52 +02003103/* identify the ROM version and check whether patches are needed */
3104static bool btusb_qca_need_patch(struct usb_device *udev)
3105{
3106 struct qca_version ver;
3107
3108 if (btusb_qca_send_vendor_req(udev, QCA_GET_TARGET_VERSION, &ver,
3109 sizeof(ver)) < 0)
3110 return false;
3111 /* only low ROM versions need patches */
3112 return !(le32_to_cpu(ver.rom_version) & ~0xffffU);
3113}
3114
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003115static int btusb_setup_qca(struct hci_dev *hdev)
3116{
Takashi Iwai803cdb82018-05-21 22:34:52 +02003117 struct btusb_data *btdata = hci_get_drvdata(hdev);
3118 struct usb_device *udev = btdata->udev;
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003119 const struct qca_device_info *info = NULL;
3120 struct qca_version ver;
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00003121 u32 ver_rom;
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003122 u8 status;
3123 int i, err;
3124
Takashi Iwai803cdb82018-05-21 22:34:52 +02003125 err = btusb_qca_send_vendor_req(udev, QCA_GET_TARGET_VERSION, &ver,
Marcel Holtmanneb500422015-04-16 23:15:50 +02003126 sizeof(ver));
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003127 if (err < 0)
3128 return err;
3129
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00003130 ver_rom = le32_to_cpu(ver.rom_version);
Takashi Iwai803cdb82018-05-21 22:34:52 +02003131
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003132 for (i = 0; i < ARRAY_SIZE(qca_devices_table); i++) {
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00003133 if (ver_rom == qca_devices_table[i].rom_version)
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003134 info = &qca_devices_table[i];
3135 }
3136 if (!info) {
Hui Wang234f4142021-02-08 13:02:37 +08003137 /* If the rom_version is not matched in the qca_devices_table
3138 * and the high ROM version is not zero, we assume this chip no
3139 * need to load the rampatch and nvm.
3140 */
3141 if (ver_rom & ~0xffffU)
3142 return 0;
3143
Marcel Holtmann2064ee32017-10-30 10:42:59 +01003144 bt_dev_err(hdev, "don't support firmware rome 0x%x", ver_rom);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003145 return -ENODEV;
3146 }
3147
Takashi Iwai803cdb82018-05-21 22:34:52 +02003148 err = btusb_qca_send_vendor_req(udev, QCA_CHECK_STATUS, &status,
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003149 sizeof(status));
3150 if (err < 0)
3151 return err;
3152
3153 if (!(status & QCA_PATCH_UPDATED)) {
3154 err = btusb_setup_qca_load_rampatch(hdev, &ver, info);
3155 if (err < 0)
3156 return err;
3157 }
3158
Tim Jiangef2862a2020-12-03 12:57:14 +08003159 err = btusb_qca_send_vendor_req(udev, QCA_GET_TARGET_VERSION, &ver,
3160 sizeof(ver));
3161 if (err < 0)
3162 return err;
3163
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003164 if (!(status & QCA_SYSCFG_UPDATED)) {
3165 err = btusb_setup_qca_load_nvm(hdev, &ver, info);
3166 if (err < 0)
3167 return err;
3168 }
3169
3170 return 0;
3171}
3172
Marcel Holtmann9d08f502015-10-08 20:23:08 +02003173static inline int __set_diag_interface(struct hci_dev *hdev)
3174{
3175 struct btusb_data *data = hci_get_drvdata(hdev);
3176 struct usb_interface *intf = data->diag;
3177 int i;
3178
3179 if (!data->diag)
3180 return -ENODEV;
3181
3182 data->diag_tx_ep = NULL;
3183 data->diag_rx_ep = NULL;
3184
3185 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
3186 struct usb_endpoint_descriptor *ep_desc;
3187
3188 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
3189
3190 if (!data->diag_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
3191 data->diag_tx_ep = ep_desc;
3192 continue;
3193 }
3194
3195 if (!data->diag_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
3196 data->diag_rx_ep = ep_desc;
3197 continue;
3198 }
3199 }
3200
3201 if (!data->diag_tx_ep || !data->diag_rx_ep) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01003202 bt_dev_err(hdev, "invalid diagnostic descriptors");
Marcel Holtmann9d08f502015-10-08 20:23:08 +02003203 return -ENODEV;
3204 }
3205
3206 return 0;
3207}
3208
3209static struct urb *alloc_diag_urb(struct hci_dev *hdev, bool enable)
3210{
3211 struct btusb_data *data = hci_get_drvdata(hdev);
3212 struct sk_buff *skb;
3213 struct urb *urb;
3214 unsigned int pipe;
3215
3216 if (!data->diag_tx_ep)
3217 return ERR_PTR(-ENODEV);
3218
3219 urb = usb_alloc_urb(0, GFP_KERNEL);
3220 if (!urb)
3221 return ERR_PTR(-ENOMEM);
3222
3223 skb = bt_skb_alloc(2, GFP_KERNEL);
3224 if (!skb) {
3225 usb_free_urb(urb);
3226 return ERR_PTR(-ENOMEM);
3227 }
3228
Johannes Berg634fef62017-06-16 14:29:24 +02003229 skb_put_u8(skb, 0xf0);
3230 skb_put_u8(skb, enable);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02003231
3232 pipe = usb_sndbulkpipe(data->udev, data->diag_tx_ep->bEndpointAddress);
3233
3234 usb_fill_bulk_urb(urb, data->udev, pipe,
3235 skb->data, skb->len, btusb_tx_complete, skb);
3236
3237 skb->dev = (void *)hdev;
3238
3239 return urb;
3240}
3241
3242static int btusb_bcm_set_diag(struct hci_dev *hdev, bool enable)
3243{
3244 struct btusb_data *data = hci_get_drvdata(hdev);
3245 struct urb *urb;
3246
3247 if (!data->diag)
3248 return -ENODEV;
3249
3250 if (!test_bit(HCI_RUNNING, &hdev->flags))
3251 return -ENETDOWN;
3252
3253 urb = alloc_diag_urb(hdev, enable);
3254 if (IS_ERR(urb))
3255 return PTR_ERR(urb);
3256
3257 return submit_or_queue_tx_urb(hdev, urb);
3258}
Marcel Holtmann9d08f502015-10-08 20:23:08 +02003259
Rajat Jainfd913ef2017-02-01 14:24:09 -08003260#ifdef CONFIG_PM
3261static irqreturn_t btusb_oob_wake_handler(int irq, void *priv)
3262{
3263 struct btusb_data *data = priv;
3264
3265 pm_wakeup_event(&data->udev->dev, 0);
Jeffy Chen017789f2017-02-24 14:24:29 +08003266 pm_system_wakeup();
Rajat Jainfd913ef2017-02-01 14:24:09 -08003267
3268 /* Disable only if not already disabled (keep it balanced) */
3269 if (test_and_clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags)) {
3270 disable_irq_nosync(irq);
3271 disable_irq_wake(irq);
3272 }
3273 return IRQ_HANDLED;
3274}
3275
3276static const struct of_device_id btusb_match_table[] = {
3277 { .compatible = "usb1286,204e" },
Brian Norris4c409af2019-02-22 14:53:43 -08003278 { .compatible = "usbcf3,e300" }, /* QCA6174A */
3279 { .compatible = "usb4ca,301a" }, /* QCA6174A (Lite-On) */
Rajat Jainfd913ef2017-02-01 14:24:09 -08003280 { }
3281};
3282MODULE_DEVICE_TABLE(of, btusb_match_table);
3283
3284/* Use an oob wakeup pin? */
3285static int btusb_config_oob_wake(struct hci_dev *hdev)
3286{
3287 struct btusb_data *data = hci_get_drvdata(hdev);
3288 struct device *dev = &data->udev->dev;
3289 int irq, ret;
3290
3291 clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags);
3292
3293 if (!of_match_device(btusb_match_table, dev))
3294 return 0;
3295
3296 /* Move on if no IRQ specified */
3297 irq = of_irq_get_byname(dev->of_node, "wakeup");
3298 if (irq <= 0) {
3299 bt_dev_dbg(hdev, "%s: no OOB Wakeup IRQ in DT", __func__);
3300 return 0;
3301 }
3302
Brian Norris771acc72019-04-09 11:49:17 -07003303 irq_set_status_flags(irq, IRQ_NOAUTOEN);
Rajat Jainfd913ef2017-02-01 14:24:09 -08003304 ret = devm_request_irq(&hdev->dev, irq, btusb_oob_wake_handler,
3305 0, "OOB Wake-on-BT", data);
3306 if (ret) {
3307 bt_dev_err(hdev, "%s: IRQ request failed", __func__);
3308 return ret;
3309 }
3310
3311 ret = device_init_wakeup(dev, true);
3312 if (ret) {
3313 bt_dev_err(hdev, "%s: failed to init_wakeup", __func__);
3314 return ret;
3315 }
3316
3317 data->oob_wake_irq = irq;
Rajat Jainfd913ef2017-02-01 14:24:09 -08003318 bt_dev_info(hdev, "OOB Wake-on-BT configured at IRQ %u", irq);
3319 return 0;
3320}
3321#endif
3322
Hans de Goedefc549102018-04-27 11:26:43 +02003323static void btusb_check_needs_reset_resume(struct usb_interface *intf)
3324{
3325 if (dmi_check_system(btusb_needs_reset_resume_table))
3326 interface_to_usbdev(intf)->quirks |= USB_QUIRK_RESET_RESUME;
3327}
3328
Luiz Augusto von Dentz4539ca62021-10-01 13:22:31 -07003329static bool btusb_wakeup(struct hci_dev *hdev)
Abhishek Pandit-Subedib7d0bf12020-05-12 19:19:27 -07003330{
3331 struct btusb_data *data = hci_get_drvdata(hdev);
3332
Luiz Augusto von Dentz4539ca62021-10-01 13:22:31 -07003333 return device_may_wakeup(&data->udev->dev);
Abhishek Pandit-Subedib7d0bf12020-05-12 19:19:27 -07003334}
3335
Tim Jiang3b0d5252020-12-18 18:12:11 +08003336static int btusb_shutdown_qca(struct hci_dev *hdev)
3337{
3338 struct sk_buff *skb;
3339
3340 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
3341 if (IS_ERR(skb)) {
3342 bt_dev_err(hdev, "HCI reset during shutdown failed");
3343 return PTR_ERR(skb);
3344 }
3345 kfree_skb(skb);
3346
3347 return 0;
3348}
3349
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003350static int btusb_probe(struct usb_interface *intf,
Marcel Holtmann89e75332014-09-16 04:44:50 +02003351 const struct usb_device_id *id)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003352{
3353 struct usb_endpoint_descriptor *ep_desc;
Rajat Jaindc786b22019-01-24 15:28:14 -08003354 struct gpio_desc *reset_gpio;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003355 struct btusb_data *data;
3356 struct hci_dev *hdev;
Marcel Holtmann22f8e9d2015-10-20 00:53:33 +02003357 unsigned ifnum_base;
Tedd Ho-Jeong An53492a62021-08-04 17:32:11 -07003358 int i, err, priv_size;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003359
3360 BT_DBG("intf %p id %p", intf, id);
3361
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02003362 /* interface numbers are hardcoded in the spec */
Marcel Holtmann22f8e9d2015-10-20 00:53:33 +02003363 if (intf->cur_altsetting->desc.bInterfaceNumber != 0) {
3364 if (!(id->driver_info & BTUSB_IFNUM_2))
3365 return -ENODEV;
3366 if (intf->cur_altsetting->desc.bInterfaceNumber != 2)
3367 return -ENODEV;
3368 }
3369
3370 ifnum_base = intf->cur_altsetting->desc.bInterfaceNumber;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003371
3372 if (!id->driver_info) {
3373 const struct usb_device_id *match;
Marcel Holtmann89e75332014-09-16 04:44:50 +02003374
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003375 match = usb_match_id(intf, blacklist_table);
3376 if (match)
3377 id = match;
3378 }
3379
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02003380 if (id->driver_info == BTUSB_IGNORE)
3381 return -ENODEV;
3382
Steven.Li2d25f8b2011-07-01 14:02:36 +08003383 if (id->driver_info & BTUSB_ATH3012) {
3384 struct usb_device *udev = interface_to_usbdev(intf);
3385
3386 /* Old firmware would otherwise let ath3k driver load
Derek Robsond98422c2017-07-22 13:47:07 +12003387 * patch and sysconfig files
3388 */
Takashi Iwai803cdb82018-05-21 22:34:52 +02003389 if (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x0001 &&
3390 !btusb_qca_need_patch(udev))
Steven.Li2d25f8b2011-07-01 14:02:36 +08003391 return -ENODEV;
3392 }
3393
Sachin Kamat98921db2012-07-27 12:38:39 +05303394 data = devm_kzalloc(&intf->dev, sizeof(*data), GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003395 if (!data)
3396 return -ENOMEM;
3397
3398 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
3399 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
3400
3401 if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
3402 data->intr_ep = ep_desc;
3403 continue;
3404 }
3405
3406 if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
3407 data->bulk_tx_ep = ep_desc;
3408 continue;
3409 }
3410
3411 if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
3412 data->bulk_rx_ep = ep_desc;
3413 continue;
3414 }
3415 }
3416
Sachin Kamat98921db2012-07-27 12:38:39 +05303417 if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003418 return -ENODEV;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003419
Marcel Holtmann893ba542015-01-28 20:27:34 -08003420 if (id->driver_info & BTUSB_AMP) {
3421 data->cmdreq_type = USB_TYPE_CLASS | 0x01;
3422 data->cmdreq = 0x2b;
3423 } else {
3424 data->cmdreq_type = USB_TYPE_CLASS;
3425 data->cmdreq = 0x00;
3426 }
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01003427
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003428 data->udev = interface_to_usbdev(intf);
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02003429 data->intf = intf;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003430
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003431 INIT_WORK(&data->work, btusb_work);
Oliver Neukum7bee5492009-08-24 23:44:59 +02003432 INIT_WORK(&data->waker, btusb_waker);
Marcel Holtmann803b5832014-09-16 08:00:29 +02003433 init_usb_anchor(&data->deferred);
3434 init_usb_anchor(&data->tx_anchor);
Oliver Neukum7bee5492009-08-24 23:44:59 +02003435 spin_lock_init(&data->txlock);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003436
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003437 init_usb_anchor(&data->intr_anchor);
3438 init_usb_anchor(&data->bulk_anchor);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02003439 init_usb_anchor(&data->isoc_anchor);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02003440 init_usb_anchor(&data->diag_anchor);
Sean Wanga1c49c432019-06-02 08:02:48 +08003441 init_usb_anchor(&data->ctrl_anchor);
Marcel Holtmann803b5832014-09-16 08:00:29 +02003442 spin_lock_init(&data->rxlock);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003443
Tedd Ho-Jeong An53492a62021-08-04 17:32:11 -07003444 priv_size = 0;
3445
Tedd Ho-Jeong An019a1ca2021-08-04 17:32:15 -07003446 data->recv_event = hci_recv_frame;
3447 data->recv_bulk = btusb_recv_bulk;
3448
Tedd Ho-Jeong An53492a62021-08-04 17:32:11 -07003449 if (id->driver_info & BTUSB_INTEL_COMBINED) {
3450 /* Allocate extra space for Intel device */
3451 priv_size += sizeof(struct btintel_data);
Tedd Ho-Jeong An53492a62021-08-04 17:32:11 -07003452
Tedd Ho-Jeong An019a1ca2021-08-04 17:32:15 -07003453 /* Override the rx handlers */
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08003454 data->recv_event = btusb_recv_event_intel;
3455 data->recv_bulk = btusb_recv_bulk_intel;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08003456 }
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +01003457
mark-yw.chen0b10c8c2021-08-04 17:03:15 +08003458 data->recv_acl = hci_recv_frame;
3459
Tedd Ho-Jeong An53492a62021-08-04 17:32:11 -07003460 hdev = hci_alloc_dev_priv(priv_size);
Sachin Kamat98921db2012-07-27 12:38:39 +05303461 if (!hdev)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003462 return -ENOMEM;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003463
Marcel Holtmannc13854c2010-02-08 15:27:07 +01003464 hdev->bus = HCI_USB;
David Herrmann155961e2012-02-09 21:58:32 +01003465 hci_set_drvdata(hdev, data);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003466
Marcel Holtmann893ba542015-01-28 20:27:34 -08003467 if (id->driver_info & BTUSB_AMP)
3468 hdev->dev_type = HCI_AMP;
3469 else
Marcel Holtmannca8bee52016-07-05 14:30:14 +02003470 hdev->dev_type = HCI_PRIMARY;
Marcel Holtmann893ba542015-01-28 20:27:34 -08003471
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003472 data->hdev = hdev;
3473
3474 SET_HCIDEV_DEV(hdev, &intf->dev);
3475
Rajat Jaindc786b22019-01-24 15:28:14 -08003476 reset_gpio = gpiod_get_optional(&data->udev->dev, "reset",
3477 GPIOD_OUT_LOW);
3478 if (IS_ERR(reset_gpio)) {
3479 err = PTR_ERR(reset_gpio);
3480 goto out_free_dev;
3481 } else if (reset_gpio) {
3482 data->reset_gpio = reset_gpio;
3483 }
3484
Marcel Holtmann9f8f9622013-04-10 08:11:35 -07003485 hdev->open = btusb_open;
3486 hdev->close = btusb_close;
3487 hdev->flush = btusb_flush;
3488 hdev->send = btusb_send_frame;
3489 hdev->notify = btusb_notify;
Luiz Augusto von Dentz4539ca62021-10-01 13:22:31 -07003490 hdev->wakeup = btusb_wakeup;
Marcel Holtmann9f8f9622013-04-10 08:11:35 -07003491
Rajat Jainfd913ef2017-02-01 14:24:09 -08003492#ifdef CONFIG_PM
3493 err = btusb_config_oob_wake(hdev);
3494 if (err)
3495 goto out_free_dev;
Rajat Jaina4ccc9e2017-02-01 14:24:10 -08003496
3497 /* Marvell devices may need a specific chip configuration */
3498 if (id->driver_info & BTUSB_MARVELL && data->oob_wake_irq) {
3499 err = marvell_config_oob_wake(hdev);
3500 if (err)
3501 goto out_free_dev;
3502 }
Rajat Jainfd913ef2017-02-01 14:24:09 -08003503#endif
Szymon Janc418678b2016-09-01 17:22:37 +02003504 if (id->driver_info & BTUSB_CW6622)
3505 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
3506
Marcel Holtmann6c9d4352015-10-17 14:39:27 +02003507 if (id->driver_info & BTUSB_BCM2045)
3508 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
3509
Marcel Holtmann9f8f9622013-04-10 08:11:35 -07003510 if (id->driver_info & BTUSB_BCM92035)
3511 hdev->setup = btusb_setup_bcm92035;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003512
Marcel Holtmannc0a21a52019-09-27 08:48:58 +02003513 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_BCM) &&
3514 (id->driver_info & BTUSB_BCM_PATCHRAM)) {
Marcel Holtmann49a5f782015-10-20 02:30:48 +02003515 hdev->manufacturer = 15;
Marcel Holtmannc2bfb102015-04-05 22:52:14 -07003516 hdev->setup = btbcm_setup_patchram;
Marcel Holtmann9d08f502015-10-08 20:23:08 +02003517 hdev->set_diag = btusb_bcm_set_diag;
Marcel Holtmann1df1f592015-04-05 22:52:11 -07003518 hdev->set_bdaddr = btbcm_set_bdaddr;
Marcel Holtmann9d08f502015-10-08 20:23:08 +02003519
3520 /* Broadcom LM_DIAG Interface numbers are hardcoded */
Marcel Holtmann22f8e9d2015-10-20 00:53:33 +02003521 data->diag = usb_ifnum_to_if(data->udev, ifnum_base + 2);
Marcel Holtmannabbaf502014-07-02 00:53:48 +02003522 }
Petri Gynther10d4c672014-05-08 15:50:01 -07003523
Marcel Holtmannc0a21a52019-09-27 08:48:58 +02003524 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_BCM) &&
3525 (id->driver_info & BTUSB_BCM_APPLE)) {
Marcel Holtmann49a5f782015-10-20 02:30:48 +02003526 hdev->manufacturer = 15;
Marcel Holtmannc2bfb102015-04-05 22:52:14 -07003527 hdev->setup = btbcm_setup_apple;
Marcel Holtmann9d08f502015-10-08 20:23:08 +02003528 hdev->set_diag = btusb_bcm_set_diag;
3529
3530 /* Broadcom LM_DIAG Interface numbers are hardcoded */
Marcel Holtmann22f8e9d2015-10-20 00:53:33 +02003531 data->diag = usb_ifnum_to_if(data->udev, ifnum_base + 2);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02003532 }
Marcel Holtmann17b27722015-03-22 15:52:38 +01003533
Tedd Ho-Jeong An83f2daf2021-08-04 17:32:10 -07003534 /* Combined Intel Device setup to support multiple setup routine */
3535 if (id->driver_info & BTUSB_INTEL_COMBINED) {
3536 err = btintel_configure_setup(hdev);
3537 if (err)
3538 goto out_free_dev;
3539
3540 /* Transport specific configuration */
Tedd Ho-Jeong An019a1ca2021-08-04 17:32:15 -07003541 hdev->send = btusb_send_frame_intel;
Rajat Jaindc786b22019-01-24 15:28:14 -08003542 hdev->cmd_timeout = btusb_intel_cmd_timeout;
Tedd Ho-Jeong An83f2daf2021-08-04 17:32:10 -07003543
Tedd Ho-Jeong Anea7c4c02021-08-04 17:32:12 -07003544 if (id->driver_info & BTUSB_INTEL_BROKEN_INITIAL_NCMD)
3545 btintel_set_flag(hdev, INTEL_BROKEN_INITIAL_NCMD);
Marcel Holtmanncb8d6592014-07-02 11:25:25 +02003546 }
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07003547
Amitkumar Karwarae8df492014-07-18 14:47:06 -07003548 if (id->driver_info & BTUSB_MARVELL)
3549 hdev->set_bdaddr = btusb_set_bdaddr_marvell;
3550
Marcel Holtmannc0a21a52019-09-27 08:48:58 +02003551 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_MTK) &&
3552 (id->driver_info & BTUSB_MEDIATEK)) {
Sean Wanga1c49c432019-06-02 08:02:48 +08003553 hdev->setup = btusb_mtk_setup;
3554 hdev->shutdown = btusb_mtk_shutdown;
3555 hdev->manufacturer = 70;
mark-yw.chene9478022021-08-04 17:06:47 +08003556 hdev->cmd_timeout = btusb_mtk_cmd_timeout;
Sean Wang8c0d17b2021-10-19 05:30:12 +08003557 hdev->set_bdaddr = btmtk_set_bdaddr;
Sean Wanga1c49c432019-06-02 08:02:48 +08003558 set_bit(HCI_QUIRK_NON_PERSISTENT_SETUP, &hdev->quirks);
mark-yw.chen0b10c8c2021-08-04 17:03:15 +08003559 data->recv_acl = btusb_recv_acl_mtk;
Sean Wanga1c49c432019-06-02 08:02:48 +08003560 }
Sean Wanga1c49c432019-06-02 08:02:48 +08003561
Marcel Holtmann661cf882015-01-02 23:35:20 -08003562 if (id->driver_info & BTUSB_SWAVE) {
3563 set_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks);
Marcel Holtmannd57dbe72014-12-26 04:42:33 +01003564 set_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS, &hdev->quirks);
Marcel Holtmann661cf882015-01-02 23:35:20 -08003565 }
Marcel Holtmannd57dbe72014-12-26 04:42:33 +01003566
Marcel Holtmanne4c534b2015-10-21 01:31:45 +02003567 if (id->driver_info & BTUSB_INTEL_BOOT) {
3568 hdev->manufacturer = 2;
Marcel Holtmann40df7832014-07-06 13:29:58 +02003569 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
Marcel Holtmanne4c534b2015-10-21 01:31:45 +02003570 }
Marcel Holtmann40df7832014-07-06 13:29:58 +02003571
Jakub Pawlowski79f0c872015-01-29 10:38:34 -08003572 if (id->driver_info & BTUSB_ATH3012) {
Takashi Iwai803cdb82018-05-21 22:34:52 +02003573 data->setup_on_usb = btusb_setup_qca;
Toshi Kikuchi58592232014-12-12 10:58:05 -08003574 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
Jakub Pawlowski3d50d512015-03-17 09:04:15 -07003575 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
Jakub Pawlowski79f0c872015-01-29 10:38:34 -08003576 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
3577 }
Toshi Kikuchi58592232014-12-12 10:58:05 -08003578
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003579 if (id->driver_info & BTUSB_QCA_ROME) {
3580 data->setup_on_usb = btusb_setup_qca;
3581 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
Abhishek Pandit-Subedi69335682020-06-24 11:11:44 -07003582 hdev->cmd_timeout = btusb_qca_cmd_timeout;
Vic Wei96e58d32018-03-28 08:28:47 -07003583 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
Hans de Goedefc549102018-04-27 11:26:43 +02003584 btusb_check_needs_reset_resume(intf);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003585 }
3586
Rocky Liaob40f58b2020-09-29 12:23:51 +08003587 if (id->driver_info & BTUSB_QCA_WCN6855) {
3588 data->setup_on_usb = btusb_setup_qca;
Tim Jiang3b0d5252020-12-18 18:12:11 +08003589 hdev->shutdown = btusb_shutdown_qca;
Rocky Liaob40f58b2020-09-29 12:23:51 +08003590 hdev->set_bdaddr = btusb_set_bdaddr_wcn6855;
3591 hdev->cmd_timeout = btusb_qca_cmd_timeout;
3592 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
Michael Suncbe6a042021-07-29 10:10:59 -07003593 hci_set_msft_opcode(hdev, 0xFD70);
Rocky Liaob40f58b2020-09-29 12:23:51 +08003594 }
3595
Hilda Wu461f95f2020-06-30 21:09:40 +08003596 if (id->driver_info & BTUSB_AMP) {
3597 /* AMP controllers do not support SCO packets */
3598 data->isoc = NULL;
3599 } else {
3600 /* Interface orders are hardcoded in the specification */
3601 data->isoc = usb_ifnum_to_if(data->udev, ifnum_base + 1);
3602 data->isoc_ifnum = ifnum_base + 1;
3603 }
3604
Arnd Bergmann42d22092019-09-18 21:59:02 +02003605 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_RTL) &&
3606 (id->driver_info & BTUSB_REALTEK)) {
Carlo Caionedb33c772015-05-14 10:49:09 +02003607 hdev->setup = btrtl_setup_realtek;
Jian-Hong Pan7af3f5582019-06-25 16:30:51 +08003608 hdev->shutdown = btrtl_shutdown_realtek;
Alex Lud7ef0d12019-09-05 10:36:31 +08003609 hdev->cmd_timeout = btusb_rtl_cmd_timeout;
Daniel Drake04b8c812015-05-21 08:23:50 -06003610
Max Chou8274db02021-08-17 11:03:12 +08003611 /* Realtek devices need to set remote wakeup on auto-suspend */
3612 set_bit(BTUSB_WAKEUP_AUTOSUSPEND, &data->flags);
Pauli Virtanen55981d32021-07-26 21:02:06 +03003613 set_bit(BTUSB_USE_ALT3_FOR_WBS, &data->flags);
Daniel Drake04b8c812015-05-21 08:23:50 -06003614 }
Daniel Drakea2698a92015-04-16 14:09:55 -06003615
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01003616 if (!reset)
Szymon Janca6c511c2012-05-23 12:35:46 +02003617 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02003618
3619 if (force_scofix || id->driver_info & BTUSB_WRONG_SCO_MTU) {
3620 if (!disable_scofix)
3621 set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
3622 }
3623
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02003624 if (id->driver_info & BTUSB_BROKEN_ISOC)
3625 data->isoc = NULL;
3626
Alain Michaud4b127bd2020-02-27 18:29:39 +00003627 if (id->driver_info & BTUSB_WIDEBAND_SPEECH)
Alain Michaud00bce3f2020-03-05 16:14:59 +00003628 set_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED, &hdev->quirks);
Alain Michaud4b127bd2020-02-27 18:29:39 +00003629
Alain Michaudaff8c482020-04-23 14:43:31 +00003630 if (id->driver_info & BTUSB_VALID_LE_STATES)
3631 set_bit(HCI_QUIRK_VALID_LE_STATES, &hdev->quirks);
3632
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01003633 if (id->driver_info & BTUSB_DIGIANSWER) {
3634 data->cmdreq_type = USB_TYPE_VENDOR;
Szymon Janca6c511c2012-05-23 12:35:46 +02003635 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01003636 }
3637
3638 if (id->driver_info & BTUSB_CSR) {
3639 struct usb_device *udev = data->udev;
Marcel Holtmann81cac642014-01-03 03:02:36 -08003640 u16 bcdDevice = le16_to_cpu(udev->descriptor.bcdDevice);
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01003641
3642 /* Old firmware would otherwise execute USB reset */
Marcel Holtmann81cac642014-01-03 03:02:36 -08003643 if (bcdDevice < 0x117)
Szymon Janca6c511c2012-05-23 12:35:46 +02003644 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmann81cac642014-01-03 03:02:36 -08003645
Ismael Ferreras Morezuelascde1a8a2020-07-26 23:12:28 +02003646 /* This must be set first in case we disable it for fakes */
Jakub Pawlowski49c989a2015-03-17 09:04:17 -07003647 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
Ismael Ferreras Morezuelascde1a8a2020-07-26 23:12:28 +02003648
3649 /* Fake CSR devices with broken commands */
3650 if (le16_to_cpu(udev->descriptor.idVendor) == 0x0a12 &&
3651 le16_to_cpu(udev->descriptor.idProduct) == 0x0001)
3652 hdev->setup = btusb_setup_csr;
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01003653 }
3654
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02003655 if (id->driver_info & BTUSB_SNIFFER) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02003656 struct usb_device *udev = data->udev;
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02003657
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01003658 /* New sniffer firmware has crippled HCI interface */
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02003659 if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
3660 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
3661 }
3662
Marcel Holtmann3a5ef202014-07-06 14:53:54 +02003663 if (id->driver_info & BTUSB_INTEL_BOOT) {
3664 /* A bug in the bootloader causes that interrupt interface is
3665 * only enabled after receiving SetInterface(0, AltSetting=0).
3666 */
3667 err = usb_set_interface(data->udev, 0, 0);
3668 if (err < 0) {
3669 BT_ERR("failed to set interface 0, alt 0 %d", err);
Rajat Jain10ab1332017-02-01 14:24:08 -08003670 goto out_free_dev;
Marcel Holtmann3a5ef202014-07-06 14:53:54 +02003671 }
3672 }
3673
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02003674 if (data->isoc) {
3675 err = usb_driver_claim_interface(&btusb_driver,
Marcel Holtmann89e75332014-09-16 04:44:50 +02003676 data->isoc, data);
Rajat Jain10ab1332017-02-01 14:24:08 -08003677 if (err < 0)
3678 goto out_free_dev;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02003679 }
3680
Marcel Holtmannc0a21a52019-09-27 08:48:58 +02003681 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_BCM) && data->diag) {
Marcel Holtmann9d08f502015-10-08 20:23:08 +02003682 if (!usb_driver_claim_interface(&btusb_driver,
3683 data->diag, data))
3684 __set_diag_interface(hdev);
3685 else
3686 data->diag = NULL;
3687 }
Marcel Holtmann9d08f502015-10-08 20:23:08 +02003688
Hans de Goede89591522021-04-09 15:58:50 +02003689 if (enable_autosuspend)
3690 usb_enable_autosuspend(data->udev);
Hans de Goedeeff2d682017-11-13 14:44:16 +01003691
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003692 err = hci_register_dev(hdev);
Rajat Jain10ab1332017-02-01 14:24:08 -08003693 if (err < 0)
3694 goto out_free_dev;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003695
3696 usb_set_intfdata(intf, data);
3697
3698 return 0;
Rajat Jain10ab1332017-02-01 14:24:08 -08003699
3700out_free_dev:
Rajat Jaindc786b22019-01-24 15:28:14 -08003701 if (data->reset_gpio)
3702 gpiod_put(data->reset_gpio);
Rajat Jain10ab1332017-02-01 14:24:08 -08003703 hci_free_dev(hdev);
3704 return err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003705}
3706
3707static void btusb_disconnect(struct usb_interface *intf)
3708{
3709 struct btusb_data *data = usb_get_intfdata(intf);
3710 struct hci_dev *hdev;
3711
3712 BT_DBG("intf %p", intf);
3713
3714 if (!data)
3715 return;
3716
3717 hdev = data->hdev;
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02003718 usb_set_intfdata(data->intf, NULL);
3719
3720 if (data->isoc)
3721 usb_set_intfdata(data->isoc, NULL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003722
Marcel Holtmann9d08f502015-10-08 20:23:08 +02003723 if (data->diag)
3724 usb_set_intfdata(data->diag, NULL);
3725
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003726 hci_unregister_dev(hdev);
3727
Marcel Holtmann9d08f502015-10-08 20:23:08 +02003728 if (intf == data->intf) {
3729 if (data->isoc)
3730 usb_driver_release_interface(&btusb_driver, data->isoc);
3731 if (data->diag)
3732 usb_driver_release_interface(&btusb_driver, data->diag);
3733 } else if (intf == data->isoc) {
3734 if (data->diag)
3735 usb_driver_release_interface(&btusb_driver, data->diag);
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02003736 usb_driver_release_interface(&btusb_driver, data->intf);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02003737 } else if (intf == data->diag) {
3738 usb_driver_release_interface(&btusb_driver, data->intf);
3739 if (data->isoc)
3740 usb_driver_release_interface(&btusb_driver, data->isoc);
3741 }
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02003742
Rajat Jainfd913ef2017-02-01 14:24:09 -08003743 if (data->oob_wake_irq)
3744 device_init_wakeup(&data->udev->dev, false);
3745
Rajat Jaindc786b22019-01-24 15:28:14 -08003746 if (data->reset_gpio)
3747 gpiod_put(data->reset_gpio);
3748
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003749 hci_free_dev(hdev);
3750}
3751
Oliver Neukum7bee5492009-08-24 23:44:59 +02003752#ifdef CONFIG_PM
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003753static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
3754{
3755 struct btusb_data *data = usb_get_intfdata(intf);
3756
3757 BT_DBG("intf %p", intf);
3758
3759 if (data->suspend_count++)
3760 return 0;
3761
Oliver Neukum7bee5492009-08-24 23:44:59 +02003762 spin_lock_irq(&data->txlock);
Alan Stern5b1b0b82011-08-19 23:49:48 +02003763 if (!(PMSG_IS_AUTO(message) && data->tx_in_flight)) {
Oliver Neukum7bee5492009-08-24 23:44:59 +02003764 set_bit(BTUSB_SUSPENDING, &data->flags);
3765 spin_unlock_irq(&data->txlock);
3766 } else {
3767 spin_unlock_irq(&data->txlock);
3768 data->suspend_count--;
3769 return -EBUSY;
3770 }
3771
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003772 cancel_work_sync(&data->work);
3773
Oliver Neukum7bee5492009-08-24 23:44:59 +02003774 btusb_stop_traffic(data);
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003775 usb_kill_anchored_urbs(&data->tx_anchor);
3776
Rajat Jainfd913ef2017-02-01 14:24:09 -08003777 if (data->oob_wake_irq && device_may_wakeup(&data->udev->dev)) {
3778 set_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags);
3779 enable_irq_wake(data->oob_wake_irq);
3780 enable_irq(data->oob_wake_irq);
3781 }
3782
Alex Lu9e455242019-08-14 20:02:52 +08003783 /* For global suspend, Realtek devices lose the loaded fw
3784 * in them. But for autosuspend, firmware should remain.
3785 * Actually, it depends on whether the usb host sends
3786 * set feature (enable wakeup) or not.
3787 */
Max Chou8274db02021-08-17 11:03:12 +08003788 if (test_bit(BTUSB_WAKEUP_AUTOSUSPEND, &data->flags)) {
Alex Lu9e455242019-08-14 20:02:52 +08003789 if (PMSG_IS_AUTO(message) &&
3790 device_can_wakeup(&data->udev->dev))
3791 data->udev->do_remote_wakeup = 1;
Max Chou8274db02021-08-17 11:03:12 +08003792 else if (!PMSG_IS_AUTO(message) &&
3793 !device_may_wakeup(&data->udev->dev)) {
3794 data->udev->do_remote_wakeup = 0;
Alex Lu9e455242019-08-14 20:02:52 +08003795 data->udev->reset_resume = 1;
Max Chou8274db02021-08-17 11:03:12 +08003796 }
Alex Lu9e455242019-08-14 20:02:52 +08003797 }
3798
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003799 return 0;
3800}
3801
Oliver Neukum7bee5492009-08-24 23:44:59 +02003802static void play_deferred(struct btusb_data *data)
3803{
3804 struct urb *urb;
3805 int err;
3806
3807 while ((urb = usb_get_from_anchor(&data->deferred))) {
Jeffy Chen19cfe912017-07-20 18:53:50 +08003808 usb_anchor_urb(urb, &data->tx_anchor);
3809
Oliver Neukum7bee5492009-08-24 23:44:59 +02003810 err = usb_submit_urb(urb, GFP_ATOMIC);
Jeffy Chen19cfe912017-07-20 18:53:50 +08003811 if (err < 0) {
3812 if (err != -EPERM && err != -ENODEV)
3813 BT_ERR("%s urb %p submission failed (%d)",
3814 data->hdev->name, urb, -err);
3815 kfree(urb->setup_packet);
3816 usb_unanchor_urb(urb);
3817 usb_free_urb(urb);
Oliver Neukum7bee5492009-08-24 23:44:59 +02003818 break;
Jeffy Chen19cfe912017-07-20 18:53:50 +08003819 }
Oliver Neukum7bee5492009-08-24 23:44:59 +02003820
3821 data->tx_in_flight++;
Jeffy Chen19cfe912017-07-20 18:53:50 +08003822 usb_free_urb(urb);
Oliver Neukum7bee5492009-08-24 23:44:59 +02003823 }
Jeffy Chen19cfe912017-07-20 18:53:50 +08003824
3825 /* Cleanup the rest deferred urbs. */
3826 while ((urb = usb_get_from_anchor(&data->deferred))) {
3827 kfree(urb->setup_packet);
3828 usb_free_urb(urb);
3829 }
Oliver Neukum7bee5492009-08-24 23:44:59 +02003830}
3831
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003832static int btusb_resume(struct usb_interface *intf)
3833{
3834 struct btusb_data *data = usb_get_intfdata(intf);
3835 struct hci_dev *hdev = data->hdev;
Oliver Neukum7bee5492009-08-24 23:44:59 +02003836 int err = 0;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003837
3838 BT_DBG("intf %p", intf);
3839
3840 if (--data->suspend_count)
3841 return 0;
3842
Rajat Jainfd913ef2017-02-01 14:24:09 -08003843 /* Disable only if not already disabled (keep it balanced) */
3844 if (test_and_clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags)) {
3845 disable_irq(data->oob_wake_irq);
3846 disable_irq_wake(data->oob_wake_irq);
3847 }
3848
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003849 if (!test_bit(HCI_RUNNING, &hdev->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +02003850 goto done;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003851
3852 if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
3853 err = btusb_submit_intr_urb(hdev, GFP_NOIO);
3854 if (err < 0) {
3855 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02003856 goto failed;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003857 }
3858 }
3859
3860 if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) {
Marcel Holtmann43c2e572009-02-04 17:41:38 +01003861 err = btusb_submit_bulk_urb(hdev, GFP_NOIO);
3862 if (err < 0) {
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003863 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02003864 goto failed;
3865 }
3866
3867 btusb_submit_bulk_urb(hdev, GFP_NOIO);
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003868 }
3869
3870 if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
3871 if (btusb_submit_isoc_urb(hdev, GFP_NOIO) < 0)
3872 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
3873 else
3874 btusb_submit_isoc_urb(hdev, GFP_NOIO);
3875 }
3876
Oliver Neukum7bee5492009-08-24 23:44:59 +02003877 spin_lock_irq(&data->txlock);
3878 play_deferred(data);
3879 clear_bit(BTUSB_SUSPENDING, &data->flags);
3880 spin_unlock_irq(&data->txlock);
3881 schedule_work(&data->work);
3882
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003883 return 0;
Oliver Neukum7bee5492009-08-24 23:44:59 +02003884
3885failed:
3886 usb_scuttle_anchored_urbs(&data->deferred);
3887done:
3888 spin_lock_irq(&data->txlock);
3889 clear_bit(BTUSB_SUSPENDING, &data->flags);
3890 spin_unlock_irq(&data->txlock);
3891
3892 return err;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003893}
Oliver Neukum7bee5492009-08-24 23:44:59 +02003894#endif
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003895
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003896static struct usb_driver btusb_driver = {
3897 .name = "btusb",
3898 .probe = btusb_probe,
3899 .disconnect = btusb_disconnect,
Oliver Neukum7bee5492009-08-24 23:44:59 +02003900#ifdef CONFIG_PM
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003901 .suspend = btusb_suspend,
3902 .resume = btusb_resume,
Oliver Neukum7bee5492009-08-24 23:44:59 +02003903#endif
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003904 .id_table = btusb_table,
Oliver Neukum7bee5492009-08-24 23:44:59 +02003905 .supports_autosuspend = 1,
Sarah Sharpe1f12eb2012-04-23 10:08:51 -07003906 .disable_hub_initiated_lpm = 1,
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003907};
3908
Greg Kroah-Hartman93f15082011-11-18 09:47:34 -08003909module_usb_driver(btusb_driver);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003910
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02003911module_param(disable_scofix, bool, 0644);
3912MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
3913
3914module_param(force_scofix, bool, 0644);
3915MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
3916
Hans de Goedeeff2d682017-11-13 14:44:16 +01003917module_param(enable_autosuspend, bool, 0644);
3918MODULE_PARM_DESC(enable_autosuspend, "Enable USB autosuspend by default");
3919
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02003920module_param(reset, bool, 0644);
3921MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
3922
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003923MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
3924MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
3925MODULE_VERSION(VERSION);
3926MODULE_LICENSE("GPL");