blob: c30d131da7847c23d4d0166f0cb349c0f29f089a [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>
Luiz Augusto von Dentz800fe5e2021-12-03 14:38:13 -080019#include <linux/debugfs.h>
Daniel Drakea2698a92015-04-16 14:09:55 -060020#include <asm/unaligned.h>
Marcel Holtmann5e23b922007-10-20 14:12:34 +020021
22#include <net/bluetooth/bluetooth.h>
23#include <net/bluetooth/hci_core.h>
24
Marcel Holtmann4185a0f2015-04-06 00:52:12 -070025#include "btintel.h"
Marcel Holtmann1df1f592015-04-05 22:52:11 -070026#include "btbcm.h"
Carlo Caionedb33c772015-05-14 10:49:09 +020027#include "btrtl.h"
Sean Wang8c0d17b2021-10-19 05:30:12 +080028#include "btmtk.h"
Marcel Holtmann1df1f592015-04-05 22:52:11 -070029
Marcel Holtmann34dced9b2015-04-05 22:52:16 -070030#define VERSION "0.8"
Marcel Holtmanncfeb4142008-08-07 22:26:56 +020031
Rusty Russell90ab5ee2012-01-13 09:32:20 +103032static bool disable_scofix;
33static bool force_scofix;
Hans de Goedeeff2d682017-11-13 14:44:16 +010034static bool enable_autosuspend = IS_ENABLED(CONFIG_BT_HCIBTUSB_AUTOSUSPEND);
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 An95655452022-01-06 16:34:54 -080063#define BTUSB_INTEL_BROKEN_SHUTDOWN_LED 0x2000000
Tedd Ho-Jeong Anea7c4c02021-08-04 17:32:12 -070064#define BTUSB_INTEL_BROKEN_INITIAL_NCMD 0x4000000
Marcel Holtmann5e23b922007-10-20 14:12:34 +020065
Marcel Holtmann54265202013-10-11 07:46:18 -070066static const struct usb_device_id btusb_table[] = {
Marcel Holtmann5e23b922007-10-20 14:12:34 +020067 /* Generic Bluetooth USB device */
68 { USB_DEVICE_INFO(0xe0, 0x01, 0x01) },
69
Marcel Holtmann893ba542015-01-28 20:27:34 -080070 /* Generic Bluetooth AMP device */
71 { USB_DEVICE_INFO(0xe0, 0x01, 0x04), .driver_info = BTUSB_AMP },
72
Daniel Draked63b2822015-07-17 11:12:25 -060073 /* Generic Bluetooth USB interface */
74 { USB_INTERFACE_INFO(0xe0, 0x01, 0x01) },
75
Henrik Rydberg1fa65352012-08-25 19:28:06 +020076 /* Apple-specific (Broadcom) devices */
Marcel Holtmann17b27722015-03-22 15:52:38 +010077 { USB_VENDOR_AND_INTERFACE_INFO(0x05ac, 0xff, 0x01, 0x01),
Marcel Holtmann22f8e9d2015-10-20 00:53:33 +020078 .driver_info = BTUSB_BCM_APPLE | BTUSB_IFNUM_2 },
Henrik Rydberg1fa65352012-08-25 19:28:06 +020079
Cho, Yu-Chen178c0592013-06-04 21:40:26 +080080 /* MediaTek MT76x0E */
81 { USB_DEVICE(0x0e8d, 0x763f) },
82
Oliver Neukumc510eae2011-09-21 11:41:45 +020083 /* Broadcom SoftSailing reporting vendor specific */
Don Zickus2e8b50632012-03-28 16:41:11 -040084 { USB_DEVICE(0x0a5c, 0x21e1) },
Oliver Neukumc510eae2011-09-21 11:41:45 +020085
Nobuhiro Iwamatsu3cd01972010-08-20 16:24:07 +090086 /* Apple MacBookPro 7,1 */
87 { USB_DEVICE(0x05ac, 0x8213) },
88
Cyril Lacoux0a79f672010-07-14 10:29:27 +040089 /* Apple iMac11,1 */
90 { USB_DEVICE(0x05ac, 0x8215) },
91
Nobuhiro Iwamatsu9c047152010-08-20 16:24:06 +090092 /* Apple MacBookPro6,2 */
93 { USB_DEVICE(0x05ac, 0x8218) },
94
Edgar (gimli) Hucek3e3ede72010-11-04 08:04:33 +010095 /* Apple MacBookAir3,1, MacBookAir3,2 */
96 { USB_DEVICE(0x05ac, 0x821b) },
97
Pieter-Augustijn Van Malleghema63b7232011-09-07 02:28:10 -040098 /* Apple MacBookAir4,1 */
99 { USB_DEVICE(0x05ac, 0x821f) },
100
Marc-Antoine Perennou88d377b2011-03-24 14:51:21 -0300101 /* Apple MacBookPro8,2 */
102 { USB_DEVICE(0x05ac, 0x821a) },
103
Jurgen Kramerf78b68262011-09-04 18:01:42 +0200104 /* Apple MacMini5,1 */
105 { USB_DEVICE(0x05ac, 0x8281) },
106
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200107 /* AVM BlueFRITZ! USB v2.0 */
Marcel Holtmann4fcef8e2015-01-01 17:34:37 -0800108 { USB_DEVICE(0x057c, 0x3800), .driver_info = BTUSB_SWAVE },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200109
110 /* Bluetooth Ultraport Module from IBM */
111 { USB_DEVICE(0x04bf, 0x030a) },
112
113 /* ALPS Modules with non-standard id */
114 { USB_DEVICE(0x044e, 0x3001) },
115 { USB_DEVICE(0x044e, 0x3002) },
116
117 /* Ericsson with non-standard id */
118 { USB_DEVICE(0x0bdb, 0x1002) },
119
120 /* Canyon CN-BTU1 with HID interfaces */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100121 { USB_DEVICE(0x0c10, 0x0000) },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200122
Marcel Holtmannd049f4e2015-01-26 20:35:32 -0800123 /* Broadcom BCM20702B0 (Dynex/Insignia) */
124 { USB_DEVICE(0x19ff, 0x0239), .driver_info = BTUSB_BCM_PATCHRAM },
125
Santtu Rekilä2faf71c2015-10-05 15:45:27 +0300126 /* Broadcom BCM43142A0 (Foxconn/Lenovo) */
Dmitry Tunin628c26b2017-07-06 14:41:13 +0300127 { USB_VENDOR_AND_INTERFACE_INFO(0x105b, 0xff, 0x01, 0x01),
128 .driver_info = BTUSB_BCM_PATCHRAM },
Santtu Rekilä2faf71c2015-10-05 15:45:27 +0300129
Christoph Haaga57bac42017-02-10 14:02:45 +0100130 /* Broadcom BCM920703 (HTC Vive) */
131 { USB_VENDOR_AND_INTERFACE_INFO(0x0bb4, 0xff, 0x01, 0x01),
132 .driver_info = BTUSB_BCM_PATCHRAM },
133
Steven Harms98514032012-04-13 14:45:55 -0400134 /* Foxconn - Hon Hai */
Heinrich Siebmanns6029ddc2014-12-03 19:32:22 +0100135 { USB_VENDOR_AND_INTERFACE_INFO(0x0489, 0xff, 0x01, 0x01),
136 .driver_info = BTUSB_BCM_PATCHRAM },
Steven Harms98514032012-04-13 14:45:55 -0400137
Matej Dubovy8f0c3042015-02-02 18:50:14 +0100138 /* Lite-On Technology - Broadcom based */
139 { USB_VENDOR_AND_INTERFACE_INFO(0x04ca, 0xff, 0x01, 0x01),
140 .driver_info = BTUSB_BCM_PATCHRAM },
141
Andy Shevchenko0b880062014-02-18 18:26:19 +0200142 /* Broadcom devices with vendor specific id */
Petri Gynther10d4c672014-05-08 15:50:01 -0700143 { USB_VENDOR_AND_INTERFACE_INFO(0x0a5c, 0xff, 0x01, 0x01),
144 .driver_info = BTUSB_BCM_PATCHRAM },
Gustavo Padovan92c385f2012-08-06 15:36:49 -0300145
Marcel Holtmannc2aef6e2014-07-21 14:02:33 +0200146 /* ASUSTek Computer - Broadcom based */
Rick Dunn9a5abdaa2015-01-17 05:29:12 +0100147 { USB_VENDOR_AND_INTERFACE_INFO(0x0b05, 0xff, 0x01, 0x01),
148 .driver_info = BTUSB_BCM_PATCHRAM },
Marcel Holtmannc2aef6e2014-07-21 14:02:33 +0200149
Ken O'Brien5bcecf32013-09-21 19:14:43 +0100150 /* Belkin F8065bf - Broadcom based */
Marcel Holtmann6331c682015-03-27 15:11:41 -0700151 { USB_VENDOR_AND_INTERFACE_INFO(0x050d, 0xff, 0x01, 0x01),
152 .driver_info = BTUSB_BCM_PATCHRAM },
Ken O'Brien5bcecf32013-09-21 19:14:43 +0100153
Jurgen Kramer9113bfd2014-02-15 12:01:09 +0100154 /* IMC Networks - Broadcom based */
Marcel Holtmann6331c682015-03-27 15:11:41 -0700155 { USB_VENDOR_AND_INTERFACE_INFO(0x13d3, 0xff, 0x01, 0x01),
156 .driver_info = BTUSB_BCM_PATCHRAM },
Jurgen Kramer9113bfd2014-02-15 12:01:09 +0100157
Wen-chien Jesse Sungfdfddc62017-01-10 15:41:13 +0800158 /* Dell Computer - Broadcom based */
159 { USB_VENDOR_AND_INTERFACE_INFO(0x413c, 0xff, 0x01, 0x01),
160 .driver_info = BTUSB_BCM_PATCHRAM },
161
Dmitry Tunin1623d0b2015-12-05 14:09:36 +0300162 /* Toshiba Corp - Broadcom based */
163 { USB_VENDOR_AND_INTERFACE_INFO(0x0930, 0xff, 0x01, 0x01),
164 .driver_info = BTUSB_BCM_PATCHRAM },
165
Marcel Holtmann40df7832014-07-06 13:29:58 +0200166 /* Intel Bluetooth USB Bootloader (RAM module) */
Marcel Holtmannd92f2df2014-07-06 14:53:55 +0200167 { USB_DEVICE(0x8087, 0x0a5a),
168 .driver_info = BTUSB_INTEL_BOOT | BTUSB_BROKEN_ISOC },
Marcel Holtmann40df7832014-07-06 13:29:58 +0200169
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200170 { } /* Terminating entry */
171};
172
173MODULE_DEVICE_TABLE(usb, btusb_table);
174
Marcel Holtmann54265202013-10-11 07:46:18 -0700175static const struct usb_device_id blacklist_table[] = {
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200176 /* CSR BlueCore devices */
177 { USB_DEVICE(0x0a12, 0x0001), .driver_info = BTUSB_CSR },
178
179 /* Broadcom BCM2033 without firmware */
180 { USB_DEVICE(0x0a5c, 0x2033), .driver_info = BTUSB_IGNORE },
181
Marcel Holtmann6c9d4352015-10-17 14:39:27 +0200182 /* Broadcom BCM2045 devices */
183 { USB_DEVICE(0x0a5c, 0x2045), .driver_info = BTUSB_BCM2045 },
184
Bala Shanmugambe931122010-11-26 17:35:46 +0530185 /* Atheros 3011 with sflash firmware */
Andy Shevchenko0b880062014-02-18 18:26:19 +0200186 { USB_DEVICE(0x0489, 0xe027), .driver_info = BTUSB_IGNORE },
187 { USB_DEVICE(0x0489, 0xe03d), .driver_info = BTUSB_IGNORE },
Alexander Ploumistos2eeff0b2015-02-13 21:05:11 +0200188 { USB_DEVICE(0x04f2, 0xaff1), .driver_info = BTUSB_IGNORE },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200189 { USB_DEVICE(0x0930, 0x0215), .driver_info = BTUSB_IGNORE },
Bala Shanmugambe931122010-11-26 17:35:46 +0530190 { USB_DEVICE(0x0cf3, 0x3002), .driver_info = BTUSB_IGNORE },
Marek Vasut6eda5412012-06-08 14:32:50 +0200191 { USB_DEVICE(0x0cf3, 0xe019), .driver_info = BTUSB_IGNORE },
Andy Ross2a7bccc2011-05-09 16:11:16 -0700192 { USB_DEVICE(0x13d3, 0x3304), .driver_info = BTUSB_IGNORE },
Bala Shanmugambe931122010-11-26 17:35:46 +0530193
Cho, Yu-Chen509e7862011-01-26 17:10:59 +0800194 /* Atheros AR9285 Malbec with sflash firmware */
195 { USB_DEVICE(0x03f0, 0x311d), .driver_info = BTUSB_IGNORE },
196
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530197 /* Atheros 3012 with sflash firmware */
Andy Shevchenko0b880062014-02-18 18:26:19 +0200198 { USB_DEVICE(0x0489, 0xe04d), .driver_info = BTUSB_ATH3012 },
199 { USB_DEVICE(0x0489, 0xe04e), .driver_info = BTUSB_ATH3012 },
200 { USB_DEVICE(0x0489, 0xe056), .driver_info = BTUSB_ATH3012 },
201 { USB_DEVICE(0x0489, 0xe057), .driver_info = BTUSB_ATH3012 },
202 { USB_DEVICE(0x0489, 0xe05f), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin692c0622015-06-06 20:25:40 +0300203 { USB_DEVICE(0x0489, 0xe076), .driver_info = BTUSB_ATH3012 },
Anantha Krishnan4b552bc2014-10-06 16:31:49 +0530204 { USB_DEVICE(0x0489, 0xe078), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin28c971d2016-02-10 00:49:11 +0300205 { USB_DEVICE(0x0489, 0xe095), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200206 { USB_DEVICE(0x04c5, 0x1330), .driver_info = BTUSB_ATH3012 },
207 { USB_DEVICE(0x04ca, 0x3004), .driver_info = BTUSB_ATH3012 },
208 { USB_DEVICE(0x04ca, 0x3005), .driver_info = BTUSB_ATH3012 },
209 { USB_DEVICE(0x04ca, 0x3006), .driver_info = BTUSB_ATH3012 },
Mohammed Habibulla1fb4e092014-04-17 11:37:13 -0700210 { USB_DEVICE(0x04ca, 0x3007), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200211 { USB_DEVICE(0x04ca, 0x3008), .driver_info = BTUSB_ATH3012 },
212 { USB_DEVICE(0x04ca, 0x300b), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin7e730c7f2015-06-18 20:41:51 +0300213 { USB_DEVICE(0x04ca, 0x300d), .driver_info = BTUSB_ATH3012 },
Dmitry Tuninec0810d2015-05-02 13:36:58 +0300214 { USB_DEVICE(0x04ca, 0x300f), .driver_info = BTUSB_ATH3012 },
Janne Heikkinen134d3b32014-12-09 07:44:51 +0200215 { USB_DEVICE(0x04ca, 0x3010), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin81d90442016-02-28 11:04:06 +0300216 { USB_DEVICE(0x04ca, 0x3014), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin441ad622017-01-05 13:19:53 +0300217 { USB_DEVICE(0x04ca, 0x3018), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200218 { USB_DEVICE(0x0930, 0x0219), .driver_info = BTUSB_ATH3012 },
Dmitry Tunincd355ff2015-10-05 19:29:33 +0300219 { USB_DEVICE(0x0930, 0x021c), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200220 { USB_DEVICE(0x0930, 0x0220), .driver_info = BTUSB_ATH3012 },
Vincent Zwanenburg89d29752014-08-08 12:33:56 +0100221 { USB_DEVICE(0x0930, 0x0227), .driver_info = BTUSB_ATH3012 },
Andy Shevchenkoa735f9e2014-02-18 18:26:20 +0200222 { USB_DEVICE(0x0b05, 0x17d0), .driver_info = BTUSB_ATH3012 },
Ming Leid66629c2013-03-15 11:00:39 +0800223 { USB_DEVICE(0x0cf3, 0x0036), .driver_info = BTUSB_ATH3012 },
Hans de Goede544a5912018-04-26 14:18:19 +0200224 { USB_DEVICE(0x0cf3, 0x3004), .driver_info = BTUSB_ATH3012 },
Sunguk Lee94a32d102013-03-12 04:41:58 +0900225 { USB_DEVICE(0x0cf3, 0x3008), .driver_info = BTUSB_ATH3012 },
Cho, Yu-Chen07c0ea82012-03-14 22:01:21 +0200226 { USB_DEVICE(0x0cf3, 0x311d), .driver_info = BTUSB_ATH3012 },
Oliver Neukumb1312372014-01-16 15:37:11 +0100227 { USB_DEVICE(0x0cf3, 0x311e), .driver_info = BTUSB_ATH3012 },
Oliver Neukum1e56f1e2014-01-16 16:02:58 +0100228 { USB_DEVICE(0x0cf3, 0x311f), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200229 { USB_DEVICE(0x0cf3, 0x3121), .driver_info = BTUSB_ATH3012 },
Ming Leiebaf5792013-03-18 23:45:11 +0800230 { USB_DEVICE(0x0cf3, 0x817a), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin18e0afa2015-10-16 11:45:26 +0300231 { USB_DEVICE(0x0cf3, 0x817b), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200232 { USB_DEVICE(0x0cf3, 0xe003), .driver_info = BTUSB_ATH3012 },
AceLan Kaoac713112012-04-19 14:53:45 +0800233 { USB_DEVICE(0x0cf3, 0xe004), .driver_info = BTUSB_ATH3012 },
Peng Chen0a3658c2013-08-30 17:41:40 +0800234 { USB_DEVICE(0x0cf3, 0xe005), .driver_info = BTUSB_ATH3012 },
Wen-chien Jesse Sungca79f232015-05-13 11:39:24 +0800235 { USB_DEVICE(0x0cf3, 0xe006), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200236 { USB_DEVICE(0x13d3, 0x3362), .driver_info = BTUSB_ATH3012 },
237 { USB_DEVICE(0x13d3, 0x3375), .driver_info = BTUSB_ATH3012 },
AceLan Kaoeed307e2012-12-11 11:41:20 +0800238 { USB_DEVICE(0x13d3, 0x3393), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin609574e2016-02-10 15:33:17 +0300239 { USB_DEVICE(0x13d3, 0x3395), .driver_info = BTUSB_ATH3012 },
Sujith Manoharan5b77a1f2013-07-15 09:29:03 +0530240 { USB_DEVICE(0x13d3, 0x3402), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin3bb30a7c2014-11-25 20:19:52 +0300241 { USB_DEVICE(0x13d3, 0x3408), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin033efa92015-01-18 00:16:51 +0300242 { USB_DEVICE(0x13d3, 0x3423), .driver_info = BTUSB_ATH3012 },
Anantha Krishnanfa2f1392014-07-08 19:25:08 +0530243 { USB_DEVICE(0x13d3, 0x3432), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin75c6aca2016-03-04 01:32:19 +0300244 { USB_DEVICE(0x13d3, 0x3472), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin0d0cef62015-06-06 20:29:25 +0300245 { USB_DEVICE(0x13d3, 0x3474), .driver_info = BTUSB_ATH3012 },
Lauro Costa72f9f8b2016-05-09 17:36:11 -0300246 { USB_DEVICE(0x13d3, 0x3487), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin12d86892016-07-12 01:35:18 +0300247 { USB_DEVICE(0x13d3, 0x3490), .driver_info = BTUSB_ATH3012 },
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530248
Cho, Yu-Chene9036e32011-02-15 10:20:07 +0800249 /* Atheros AR5BBU12 with sflash firmware */
250 { USB_DEVICE(0x0489, 0xe02c), .driver_info = BTUSB_IGNORE },
251
Michael Gruetzner85d59722012-05-02 22:33:40 +0200252 /* Atheros AR5BBU12 with sflash firmware */
Yevgeniy Melnichukbc21fde2012-08-07 19:48:10 +0530253 { USB_DEVICE(0x0489, 0xe036), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200254 { USB_DEVICE(0x0489, 0xe03c), .driver_info = BTUSB_ATH3012 },
Michael Gruetzner85d59722012-05-02 22:33:40 +0200255
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +0000256 /* QCA ROME chipset */
Rocky Liaoe99f38b2020-09-17 21:09:23 +0800257 { USB_DEVICE(0x0cf3, 0x535b), .driver_info = BTUSB_QCA_ROME |
258 BTUSB_WIDEBAND_SPEECH },
259 { USB_DEVICE(0x0cf3, 0xe007), .driver_info = BTUSB_QCA_ROME |
260 BTUSB_WIDEBAND_SPEECH },
261 { USB_DEVICE(0x0cf3, 0xe009), .driver_info = BTUSB_QCA_ROME |
262 BTUSB_WIDEBAND_SPEECH },
263 { USB_DEVICE(0x0cf3, 0xe010), .driver_info = BTUSB_QCA_ROME |
264 BTUSB_WIDEBAND_SPEECH },
265 { USB_DEVICE(0x0cf3, 0xe300), .driver_info = BTUSB_QCA_ROME |
266 BTUSB_WIDEBAND_SPEECH },
267 { USB_DEVICE(0x0cf3, 0xe301), .driver_info = BTUSB_QCA_ROME |
268 BTUSB_WIDEBAND_SPEECH },
269 { USB_DEVICE(0x0cf3, 0xe360), .driver_info = BTUSB_QCA_ROME |
270 BTUSB_WIDEBAND_SPEECH },
Daniel Lenski0324d192021-05-18 10:25:46 -0700271 { USB_DEVICE(0x0cf3, 0xe500), .driver_info = BTUSB_QCA_ROME |
272 BTUSB_WIDEBAND_SPEECH },
Rocky Liaoe99f38b2020-09-17 21:09:23 +0800273 { USB_DEVICE(0x0489, 0xe092), .driver_info = BTUSB_QCA_ROME |
274 BTUSB_WIDEBAND_SPEECH },
275 { USB_DEVICE(0x0489, 0xe09f), .driver_info = BTUSB_QCA_ROME |
276 BTUSB_WIDEBAND_SPEECH },
277 { USB_DEVICE(0x0489, 0xe0a2), .driver_info = BTUSB_QCA_ROME |
278 BTUSB_WIDEBAND_SPEECH },
279 { USB_DEVICE(0x04ca, 0x3011), .driver_info = BTUSB_QCA_ROME |
280 BTUSB_WIDEBAND_SPEECH },
281 { USB_DEVICE(0x04ca, 0x3015), .driver_info = BTUSB_QCA_ROME |
282 BTUSB_WIDEBAND_SPEECH },
283 { USB_DEVICE(0x04ca, 0x3016), .driver_info = BTUSB_QCA_ROME |
284 BTUSB_WIDEBAND_SPEECH },
285 { USB_DEVICE(0x04ca, 0x301a), .driver_info = BTUSB_QCA_ROME |
286 BTUSB_WIDEBAND_SPEECH },
287 { USB_DEVICE(0x04ca, 0x3021), .driver_info = BTUSB_QCA_ROME |
288 BTUSB_WIDEBAND_SPEECH },
289 { USB_DEVICE(0x13d3, 0x3491), .driver_info = BTUSB_QCA_ROME |
290 BTUSB_WIDEBAND_SPEECH },
291 { USB_DEVICE(0x13d3, 0x3496), .driver_info = BTUSB_QCA_ROME |
292 BTUSB_WIDEBAND_SPEECH },
293 { USB_DEVICE(0x13d3, 0x3501), .driver_info = BTUSB_QCA_ROME |
294 BTUSB_WIDEBAND_SPEECH },
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +0000295
Rocky Liaob40f58b2020-09-29 12:23:51 +0800296 /* QCA WCN6855 chipset */
297 { USB_DEVICE(0x0cf3, 0xe600), .driver_info = BTUSB_QCA_WCN6855 |
Michael Sunacd5aea2021-07-23 17:17:31 -0700298 BTUSB_WIDEBAND_SPEECH |
299 BTUSB_VALID_LE_STATES },
tjiang@codeaurora.org21a241b2021-11-16 19:02:16 +0800300 { USB_DEVICE(0x0489, 0xe0cc), .driver_info = BTUSB_QCA_WCN6855 |
301 BTUSB_WIDEBAND_SPEECH |
302 BTUSB_VALID_LE_STATES },
303 { USB_DEVICE(0x0489, 0xe0d6), .driver_info = BTUSB_QCA_WCN6855 |
304 BTUSB_WIDEBAND_SPEECH |
305 BTUSB_VALID_LE_STATES },
Zijun Hue8c42582021-12-03 18:01:37 +0800306 { USB_DEVICE(0x0489, 0xe0e3), .driver_info = BTUSB_QCA_WCN6855 |
307 BTUSB_WIDEBAND_SPEECH |
308 BTUSB_VALID_LE_STATES },
Zijun Hud2666be2021-12-09 14:34:01 +0800309 { USB_DEVICE(0x10ab, 0x9309), .driver_info = BTUSB_QCA_WCN6855 |
310 BTUSB_WIDEBAND_SPEECH |
311 BTUSB_VALID_LE_STATES },
312 { USB_DEVICE(0x10ab, 0x9409), .driver_info = BTUSB_QCA_WCN6855 |
313 BTUSB_WIDEBAND_SPEECH |
314 BTUSB_VALID_LE_STATES },
Aaron Ma1cd563e2022-01-07 11:59:09 +0800315 { USB_DEVICE(0x0489, 0xe0d0), .driver_info = BTUSB_QCA_WCN6855 |
316 BTUSB_WIDEBAND_SPEECH |
317 BTUSB_VALID_LE_STATES },
Rocky Liaob40f58b2020-09-29 12:23:51 +0800318
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200319 /* Broadcom BCM2035 */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100320 { USB_DEVICE(0x0a5c, 0x2009), .driver_info = BTUSB_BCM92035 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200321 { USB_DEVICE(0x0a5c, 0x200a), .driver_info = BTUSB_WRONG_SCO_MTU },
322 { USB_DEVICE(0x0a5c, 0x2035), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200323
324 /* Broadcom BCM2045 */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100325 { USB_DEVICE(0x0a5c, 0x2039), .driver_info = BTUSB_WRONG_SCO_MTU },
326 { USB_DEVICE(0x0a5c, 0x2101), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmannbdbef3d2008-09-23 00:16:35 +0200327
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200328 /* IBM/Lenovo ThinkPad with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100329 { USB_DEVICE(0x0a5c, 0x201e), .driver_info = BTUSB_WRONG_SCO_MTU },
330 { USB_DEVICE(0x0a5c, 0x2110), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200331
332 /* HP laptop with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100333 { USB_DEVICE(0x03f0, 0x171d), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200334
335 /* Dell laptop with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100336 { USB_DEVICE(0x413c, 0x8126), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200337
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100338 /* Dell Wireless 370 and 410 devices */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100339 { USB_DEVICE(0x413c, 0x8152), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100340 { USB_DEVICE(0x413c, 0x8156), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200341
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100342 /* Belkin F8T012 and F8T013 devices */
343 { USB_DEVICE(0x050d, 0x0012), .driver_info = BTUSB_WRONG_SCO_MTU },
344 { USB_DEVICE(0x050d, 0x0013), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200345
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100346 /* Asus WL-BTD202 device */
347 { USB_DEVICE(0x0b05, 0x1715), .driver_info = BTUSB_WRONG_SCO_MTU },
348
349 /* Kensington Bluetooth USB adapter */
350 { USB_DEVICE(0x047d, 0x105e), .driver_info = BTUSB_WRONG_SCO_MTU },
351
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200352 /* RTX Telecom based adapters with buggy SCO support */
353 { USB_DEVICE(0x0400, 0x0807), .driver_info = BTUSB_BROKEN_ISOC },
354 { USB_DEVICE(0x0400, 0x080a), .driver_info = BTUSB_BROKEN_ISOC },
355
356 /* CONWISE Technology based adapters with buggy SCO support */
Szymon Janc418678b2016-09-01 17:22:37 +0200357 { USB_DEVICE(0x0e5e, 0x6622),
358 .driver_info = BTUSB_BROKEN_ISOC | BTUSB_CW6622},
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200359
Marcel Holtmann4fcef8e2015-01-01 17:34:37 -0800360 /* Roper Class 1 Bluetooth Dongle (Silicon Wave based) */
Aleksei Volkov2eeac872015-06-08 12:02:10 +0300361 { USB_DEVICE(0x1310, 0x0001), .driver_info = BTUSB_SWAVE },
Marcel Holtmann4fcef8e2015-01-01 17:34:37 -0800362
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200363 /* Digianswer devices */
364 { USB_DEVICE(0x08fd, 0x0001), .driver_info = BTUSB_DIGIANSWER },
365 { USB_DEVICE(0x08fd, 0x0002), .driver_info = BTUSB_IGNORE },
366
367 /* CSR BlueCore Bluetooth Sniffer */
Marcel Holtmann4f64fa82014-07-07 00:12:04 +0200368 { USB_DEVICE(0x0a12, 0x0002),
369 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200370
371 /* Frontline ComProbe Bluetooth Sniffer */
Marcel Holtmann4f64fa82014-07-07 00:12:04 +0200372 { USB_DEVICE(0x16d3, 0x0002),
373 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200374
Marcel Holtmanncb1ee892015-01-28 19:41:42 -0800375 /* Marvell Bluetooth devices */
376 { USB_DEVICE(0x1286, 0x2044), .driver_info = BTUSB_MARVELL },
377 { USB_DEVICE(0x1286, 0x2046), .driver_info = BTUSB_MARVELL },
Amitkumar Karwar1165df02016-09-28 16:18:35 +0530378 { USB_DEVICE(0x1286, 0x204e), .driver_info = BTUSB_MARVELL },
Marcel Holtmanncb1ee892015-01-28 19:41:42 -0800379
Marcel Holtmannd0ac9eb2015-01-28 19:41:43 -0800380 /* Intel Bluetooth devices */
Tedd Ho-Jeong An3df4dfb2021-08-04 17:32:16 -0700381 { USB_DEVICE(0x8087, 0x0025), .driver_info = BTUSB_INTEL_COMBINED },
382 { USB_DEVICE(0x8087, 0x0026), .driver_info = BTUSB_INTEL_COMBINED },
383 { USB_DEVICE(0x8087, 0x0029), .driver_info = BTUSB_INTEL_COMBINED },
384 { USB_DEVICE(0x8087, 0x0032), .driver_info = BTUSB_INTEL_COMBINED },
385 { USB_DEVICE(0x8087, 0x0033), .driver_info = BTUSB_INTEL_COMBINED },
Marcel Holtmann407550f2015-02-22 15:41:18 -0800386 { USB_DEVICE(0x8087, 0x07da), .driver_info = BTUSB_CSR },
Tedd Ho-Jeong Anea7c4c02021-08-04 17:32:12 -0700387 { USB_DEVICE(0x8087, 0x07dc), .driver_info = BTUSB_INTEL_COMBINED |
Tedd Ho-Jeong An95655452022-01-06 16:34:54 -0800388 BTUSB_INTEL_BROKEN_INITIAL_NCMD |
389 BTUSB_INTEL_BROKEN_SHUTDOWN_LED },
390 { USB_DEVICE(0x8087, 0x0a2a), .driver_info = BTUSB_INTEL_COMBINED |
391 BTUSB_INTEL_BROKEN_SHUTDOWN_LED },
Tedd Ho-Jeong An3df4dfb2021-08-04 17:32:16 -0700392 { USB_DEVICE(0x8087, 0x0a2b), .driver_info = BTUSB_INTEL_COMBINED },
Tedd Ho-Jeong An95655452022-01-06 16:34:54 -0800393 { USB_DEVICE(0x8087, 0x0aa7), .driver_info = BTUSB_INTEL_COMBINED |
394 BTUSB_INTEL_BROKEN_SHUTDOWN_LED },
Tedd Ho-Jeong An3df4dfb2021-08-04 17:32:16 -0700395 { USB_DEVICE(0x8087, 0x0aaa), .driver_info = BTUSB_INTEL_COMBINED },
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -0700396
Marcel Holtmannd0ac9eb2015-01-28 19:41:43 -0800397 /* Other Intel Bluetooth devices */
398 { USB_VENDOR_AND_INTERFACE_INFO(0x8087, 0xe0, 0x01, 0x01),
399 .driver_info = BTUSB_IGNORE },
Amitkumar Karwarae8df492014-07-18 14:47:06 -0700400
Joseph Hwang33bfd942020-07-13 15:45:29 +0800401 /* Realtek 8822CE Bluetooth devices */
402 { USB_DEVICE(0x0bda, 0xb00c), .driver_info = BTUSB_REALTEK |
403 BTUSB_WIDEBAND_SPEECH },
Larry Finger4d96d3b2021-06-01 14:04:18 -0500404 { USB_DEVICE(0x0bda, 0xc822), .driver_info = BTUSB_REALTEK |
405 BTUSB_WIDEBAND_SPEECH },
Joseph Hwang33bfd942020-07-13 15:45:29 +0800406
Max Chou0d484db2020-11-10 11:38:37 +0800407 /* Realtek 8852AE Bluetooth devices */
408 { USB_DEVICE(0x0bda, 0xc852), .driver_info = BTUSB_REALTEK |
409 BTUSB_WIDEBAND_SPEECH },
Larry Finger27fe0972021-11-21 10:51:48 -0600410 { USB_DEVICE(0x0bda, 0x385a), .driver_info = BTUSB_REALTEK |
411 BTUSB_WIDEBAND_SPEECH },
Larry Fingerd1dcbf62021-09-28 13:45:20 -0500412 { USB_DEVICE(0x0bda, 0x4852), .driver_info = BTUSB_REALTEK |
413 BTUSB_WIDEBAND_SPEECH },
Max Chou9bba1282021-09-01 15:48:45 +0800414 { USB_DEVICE(0x04c5, 0x165c), .driver_info = BTUSB_REALTEK |
415 BTUSB_WIDEBAND_SPEECH },
Larry Fingerd1dcbf62021-09-28 13:45:20 -0500416 { USB_DEVICE(0x04ca, 0x4006), .driver_info = BTUSB_REALTEK |
417 BTUSB_WIDEBAND_SPEECH },
Max Chou0d484db2020-11-10 11:38:37 +0800418
Daniel Drakea2698a92015-04-16 14:09:55 -0600419 /* Realtek Bluetooth devices */
420 { USB_VENDOR_AND_INTERFACE_INFO(0x0bda, 0xe0, 0x01, 0x01),
421 .driver_info = BTUSB_REALTEK },
422
Sean Wanga1c49c432019-06-02 08:02:48 +0800423 /* MediaTek Bluetooth devices */
424 { USB_VENDOR_AND_INTERFACE_INFO(0x0e8d, 0xe0, 0x01, 0x01),
mark-yw.chen27e554a2021-03-29 01:18:33 +0800425 .driver_info = BTUSB_MEDIATEK |
426 BTUSB_WIDEBAND_SPEECH |
427 BTUSB_VALID_LE_STATES },
Sean Wanga1c49c432019-06-02 08:02:48 +0800428
Chris Chiu3a567b92020-11-10 11:40:10 +0800429 /* Additional MediaTek MT7615E Bluetooth devices */
430 { USB_DEVICE(0x13d3, 0x3560), .driver_info = BTUSB_MEDIATEK},
431
Forest Crossman37356822021-07-12 02:32:20 -0500432 /* Additional MediaTek MT7668 Bluetooth devices */
433 { USB_DEVICE(0x043e, 0x3109), .driver_info = BTUSB_MEDIATEK |
434 BTUSB_WIDEBAND_SPEECH |
435 BTUSB_VALID_LE_STATES },
436
mark-yw.chen393dc5d2021-04-12 23:06:27 +0800437 /* Additional MediaTek MT7921 Bluetooth devices */
438 { USB_DEVICE(0x04ca, 0x3802), .driver_info = BTUSB_MEDIATEK |
439 BTUSB_WIDEBAND_SPEECH |
440 BTUSB_VALID_LE_STATES },
Wai Paulo Valerio Wang2cdff8c2021-07-08 04:00:59 +0800441 { USB_DEVICE(0x13d3, 0x3563), .driver_info = BTUSB_MEDIATEK |
442 BTUSB_WIDEBAND_SPEECH |
443 BTUSB_VALID_LE_STATES },
mark-yw.chen15957ca2021-09-01 19:30:15 +0800444 { USB_DEVICE(0x13d3, 0x3564), .driver_info = BTUSB_MEDIATEK |
445 BTUSB_WIDEBAND_SPEECH |
446 BTUSB_VALID_LE_STATES },
Aaron Ma64832df2021-07-23 01:17:18 +0800447 { USB_DEVICE(0x0489, 0xe0cd), .driver_info = BTUSB_MEDIATEK |
448 BTUSB_WIDEBAND_SPEECH |
449 BTUSB_VALID_LE_STATES },
mark-yw.chen393dc5d2021-04-12 23:06:27 +0800450
Aaron Ma69326272021-12-17 17:51:50 +0800451 /* MediaTek MT7922A Bluetooth devices */
452 { USB_DEVICE(0x0489, 0xe0d8), .driver_info = BTUSB_MEDIATEK |
453 BTUSB_WIDEBAND_SPEECH |
454 BTUSB_VALID_LE_STATES },
455 { USB_DEVICE(0x0489, 0xe0d9), .driver_info = BTUSB_MEDIATEK |
456 BTUSB_WIDEBAND_SPEECH |
457 BTUSB_VALID_LE_STATES },
458
Daniel Drakea2698a92015-04-16 14:09:55 -0600459 /* Additional Realtek 8723AE Bluetooth devices */
460 { USB_DEVICE(0x0930, 0x021d), .driver_info = BTUSB_REALTEK },
461 { USB_DEVICE(0x13d3, 0x3394), .driver_info = BTUSB_REALTEK },
462
463 /* Additional Realtek 8723BE Bluetooth devices */
464 { USB_DEVICE(0x0489, 0xe085), .driver_info = BTUSB_REALTEK },
465 { USB_DEVICE(0x0489, 0xe08b), .driver_info = BTUSB_REALTEK },
466 { USB_DEVICE(0x13d3, 0x3410), .driver_info = BTUSB_REALTEK },
467 { USB_DEVICE(0x13d3, 0x3416), .driver_info = BTUSB_REALTEK },
468 { USB_DEVICE(0x13d3, 0x3459), .driver_info = BTUSB_REALTEK },
Dmitry Tunina81d72d2017-08-08 14:09:02 +0300469 { USB_DEVICE(0x13d3, 0x3494), .driver_info = BTUSB_REALTEK },
Daniel Drakea2698a92015-04-16 14:09:55 -0600470
Vicente Bergasa41e0792018-03-20 19:41:10 +0100471 /* Additional Realtek 8723BU Bluetooth devices */
472 { USB_DEVICE(0x7392, 0xa611), .driver_info = BTUSB_REALTEK },
473
Jian-Hong Pan66d99752018-05-21 18:09:20 +0800474 /* Additional Realtek 8723DE Bluetooth devices */
Jian-Hong Pan45ae68b2018-05-25 17:54:52 +0800475 { USB_DEVICE(0x0bda, 0xb009), .driver_info = BTUSB_REALTEK },
Jian-Hong Pan66d99752018-05-21 18:09:20 +0800476 { USB_DEVICE(0x2ff8, 0xb011), .driver_info = BTUSB_REALTEK },
477
Nicholas Flintham4fd6d492021-09-30 09:22:39 +0100478 /* Additional Realtek 8761B Bluetooth devices */
479 { USB_DEVICE(0x2357, 0x0604), .driver_info = BTUSB_REALTEK |
480 BTUSB_WIDEBAND_SPEECH },
481
Joakim Tjernlund33404382021-05-28 17:26:45 +0200482 /* Additional Realtek 8761BU Bluetooth devices */
483 { USB_DEVICE(0x0b05, 0x190e), .driver_info = BTUSB_REALTEK |
484 BTUSB_WIDEBAND_SPEECH },
485
Daniel Drakea2698a92015-04-16 14:09:55 -0600486 /* Additional Realtek 8821AE Bluetooth devices */
487 { USB_DEVICE(0x0b05, 0x17dc), .driver_info = BTUSB_REALTEK },
488 { USB_DEVICE(0x13d3, 0x3414), .driver_info = BTUSB_REALTEK },
489 { USB_DEVICE(0x13d3, 0x3458), .driver_info = BTUSB_REALTEK },
490 { USB_DEVICE(0x13d3, 0x3461), .driver_info = BTUSB_REALTEK },
491 { USB_DEVICE(0x13d3, 0x3462), .driver_info = BTUSB_REALTEK },
492
Larry Fingerfed03fe2018-02-11 12:24:32 -0600493 /* Additional Realtek 8822BE Bluetooth devices */
Artiom Vaskov1cd2fab2018-05-30 11:23:00 +0300494 { USB_DEVICE(0x13d3, 0x3526), .driver_info = BTUSB_REALTEK },
Larry Fingerfed03fe2018-02-11 12:24:32 -0600495 { USB_DEVICE(0x0b05, 0x185c), .driver_info = BTUSB_REALTEK },
496
Jian-Hong Pan6d0762b2019-09-03 17:10:42 +0800497 /* Additional Realtek 8822CE Bluetooth devices */
Max Chou73280f12020-11-05 18:54:48 +0800498 { USB_DEVICE(0x04ca, 0x4005), .driver_info = BTUSB_REALTEK |
499 BTUSB_WIDEBAND_SPEECH },
500 { USB_DEVICE(0x04c5, 0x161f), .driver_info = BTUSB_REALTEK |
501 BTUSB_WIDEBAND_SPEECH },
502 { USB_DEVICE(0x0b05, 0x18ef), .driver_info = BTUSB_REALTEK |
503 BTUSB_WIDEBAND_SPEECH },
504 { USB_DEVICE(0x13d3, 0x3548), .driver_info = BTUSB_REALTEK |
505 BTUSB_WIDEBAND_SPEECH },
506 { USB_DEVICE(0x13d3, 0x3549), .driver_info = BTUSB_REALTEK |
507 BTUSB_WIDEBAND_SPEECH },
508 { USB_DEVICE(0x13d3, 0x3553), .driver_info = BTUSB_REALTEK |
509 BTUSB_WIDEBAND_SPEECH },
510 { USB_DEVICE(0x13d3, 0x3555), .driver_info = BTUSB_REALTEK |
511 BTUSB_WIDEBAND_SPEECH },
512 { USB_DEVICE(0x2ff8, 0x3051), .driver_info = BTUSB_REALTEK |
513 BTUSB_WIDEBAND_SPEECH },
Julian Pidancetf4962972020-11-09 17:47:22 +0100514 { USB_DEVICE(0x1358, 0xc123), .driver_info = BTUSB_REALTEK |
515 BTUSB_WIDEBAND_SPEECH },
Cadel Watson24d6a6d2020-11-19 16:16:25 +1100516 { USB_DEVICE(0x0bda, 0xc123), .driver_info = BTUSB_REALTEK |
517 BTUSB_WIDEBAND_SPEECH },
Rasmus Moorats3edc5782021-02-18 13:11:24 +0200518 { USB_DEVICE(0x0cb5, 0xc547), .driver_info = BTUSB_REALTEK |
519 BTUSB_WIDEBAND_SPEECH },
Jian-Hong Pan6d0762b2019-09-03 17:10:42 +0800520
Peter Poklop4481c072015-08-15 20:47:09 +0200521 /* Silicon Wave based devices */
522 { USB_DEVICE(0x0c10, 0x0000), .driver_info = BTUSB_SWAVE },
523
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200524 { } /* Terminating entry */
525};
526
Hans de Goede1fdb9262018-02-20 09:06:18 +0100527/* The Bluetooth USB module build into some devices needs to be reset on resume,
528 * this is a problem with the platform (likely shutting off all power) not with
529 * the module itself. So we use a DMI list to match known broken platforms.
530 */
531static const struct dmi_system_id btusb_needs_reset_resume_table[] = {
532 {
Kai-Heng Feng0c6e5262018-03-01 13:42:52 +0800533 /* Dell OptiPlex 3060 (QCA ROME device 0cf3:e007) */
Hans de Goede1fdb9262018-02-20 09:06:18 +0100534 .matches = {
Kai-Heng Feng0c6e5262018-03-01 13:42:52 +0800535 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
536 DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 3060"),
Hans de Goede1fdb9262018-02-20 09:06:18 +0100537 },
538 },
Hans de Goede596b07a2018-04-26 20:52:06 +0200539 {
540 /* Dell XPS 9360 (QCA ROME device 0cf3:e300) */
541 .matches = {
542 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
543 DMI_MATCH(DMI_PRODUCT_NAME, "XPS 13 9360"),
544 },
545 },
Hans de Goede939bc6c2018-05-22 09:34:10 +0200546 {
547 /* Dell Inspiron 5565 (QCA ROME device 0cf3:e009) */
548 .matches = {
549 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
550 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5565"),
551 },
552 },
Hans de Goede1fdb9262018-02-20 09:06:18 +0100553 {}
554};
555
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200556#define BTUSB_MAX_ISOC_FRAMES 10
557
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200558#define BTUSB_INTR_RUNNING 0
559#define BTUSB_BULK_RUNNING 1
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200560#define BTUSB_ISOC_RUNNING 2
Oliver Neukum7bee5492009-08-24 23:44:59 +0200561#define BTUSB_SUSPENDING 3
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -0300562#define BTUSB_DID_ISO_RESUME 4
Marcel Holtmanncda0dd72015-01-26 21:33:48 -0800563#define BTUSB_BOOTLOADER 5
564#define BTUSB_DOWNLOADING 6
Marcel Holtmannce6bb922015-01-28 01:58:40 -0800565#define BTUSB_FIRMWARE_LOADED 7
Marcel Holtmanncda0dd72015-01-26 21:33:48 -0800566#define BTUSB_FIRMWARE_FAILED 8
Marcel Holtmannce6bb922015-01-28 01:58:40 -0800567#define BTUSB_BOOTING 9
Hans de Goede61f5ace2018-01-08 10:44:16 +0100568#define BTUSB_DIAG_RUNNING 10
569#define BTUSB_OOB_WAKE_ENABLED 11
Rajat Jaindc786b22019-01-24 15:28:14 -0800570#define BTUSB_HW_RESET_ACTIVE 12
Sean Wanga1c49c432019-06-02 08:02:48 +0800571#define BTUSB_TX_WAIT_VND_EVT 13
Max Chou8274db02021-08-17 11:03:12 +0800572#define BTUSB_WAKEUP_AUTOSUSPEND 14
Pauli Virtanen55981d32021-07-26 21:02:06 +0300573#define BTUSB_USE_ALT3_FOR_WBS 15
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200574
575struct btusb_data {
576 struct hci_dev *hdev;
577 struct usb_device *udev;
Marcel Holtmann5fbcd262008-09-23 00:16:36 +0200578 struct usb_interface *intf;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200579 struct usb_interface *isoc;
Marcel Holtmann9d08f502015-10-08 20:23:08 +0200580 struct usb_interface *diag;
Marcel Holtmann459232f2017-10-24 19:42:45 +0200581 unsigned isoc_ifnum;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200582
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200583 unsigned long flags;
584
Luiz Augusto von Dentz800fe5e2021-12-03 14:38:13 -0800585 bool poll_sync;
586 int intr_interval;
587 struct work_struct work;
588 struct work_struct waker;
589 struct delayed_work rx_work;
590
591 struct sk_buff_head acl_q;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200592
Marcel Holtmann803b5832014-09-16 08:00:29 +0200593 struct usb_anchor deferred;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200594 struct usb_anchor tx_anchor;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200595 int tx_in_flight;
596 spinlock_t txlock;
597
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200598 struct usb_anchor intr_anchor;
599 struct usb_anchor bulk_anchor;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200600 struct usb_anchor isoc_anchor;
Marcel Holtmann9d08f502015-10-08 20:23:08 +0200601 struct usb_anchor diag_anchor;
Sean Wanga1c49c432019-06-02 08:02:48 +0800602 struct usb_anchor ctrl_anchor;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200603 spinlock_t rxlock;
604
605 struct sk_buff *evt_skb;
606 struct sk_buff *acl_skb;
607 struct sk_buff *sco_skb;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200608
609 struct usb_endpoint_descriptor *intr_ep;
610 struct usb_endpoint_descriptor *bulk_tx_ep;
611 struct usb_endpoint_descriptor *bulk_rx_ep;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200612 struct usb_endpoint_descriptor *isoc_tx_ep;
613 struct usb_endpoint_descriptor *isoc_rx_ep;
Marcel Holtmann9d08f502015-10-08 20:23:08 +0200614 struct usb_endpoint_descriptor *diag_tx_ep;
615 struct usb_endpoint_descriptor *diag_rx_ep;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200616
Rajat Jaindc786b22019-01-24 15:28:14 -0800617 struct gpio_desc *reset_gpio;
618
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100619 __u8 cmdreq_type;
Marcel Holtmann893ba542015-01-28 20:27:34 -0800620 __u8 cmdreq;
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100621
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100622 unsigned int sco_num;
Sathish Narasimmanbaac6272020-04-03 21:43:59 +0200623 unsigned int air_mode;
624 bool usb_alt6_packet_flow;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200625 int isoc_altsetting;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +0100626 int suspend_count;
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +0100627
Marcel Holtmann97307f52015-01-12 13:51:10 -0800628 int (*recv_event)(struct hci_dev *hdev, struct sk_buff *skb);
mark-yw.chen0b10c8c2021-08-04 17:03:15 +0800629 int (*recv_acl)(struct hci_dev *hdev, struct sk_buff *skb);
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +0100630 int (*recv_bulk)(struct btusb_data *data, void *buffer, int count);
Kim, Ben Young Taeace31982015-02-15 23:06:14 +0000631
632 int (*setup_on_usb)(struct hci_dev *hdev);
Rajat Jainfd913ef2017-02-01 14:24:09 -0800633
634 int oob_wake_irq; /* irq for out-of-band wake-on-bt */
Rajat Jaindc786b22019-01-24 15:28:14 -0800635 unsigned cmd_timeout_cnt;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200636};
637
Rajat Jaindc786b22019-01-24 15:28:14 -0800638static void btusb_intel_cmd_timeout(struct hci_dev *hdev)
639{
640 struct btusb_data *data = hci_get_drvdata(hdev);
641 struct gpio_desc *reset_gpio = data->reset_gpio;
642
643 if (++data->cmd_timeout_cnt < 5)
644 return;
645
646 if (!reset_gpio) {
647 bt_dev_err(hdev, "No way to reset. Ignoring and continuing");
648 return;
649 }
650
651 /*
652 * Toggle the hard reset line if the platform provides one. The reset
653 * is going to yank the device off the USB and then replug. So doing
654 * once is enough. The cleanup is handled correctly on the way out
655 * (standard USB disconnect), and the new device is detected cleanly
656 * and bound to the driver again like it should be.
657 */
658 if (test_and_set_bit(BTUSB_HW_RESET_ACTIVE, &data->flags)) {
659 bt_dev_err(hdev, "last reset failed? Not resetting again");
660 return;
661 }
662
663 bt_dev_err(hdev, "Initiating HW reset via gpio");
Rajat Jain2de66bb82019-01-28 15:08:09 -0800664 gpiod_set_value_cansleep(reset_gpio, 1);
665 msleep(100);
666 gpiod_set_value_cansleep(reset_gpio, 0);
Rajat Jaindc786b22019-01-24 15:28:14 -0800667}
668
Alex Lud7ef0d12019-09-05 10:36:31 +0800669static void btusb_rtl_cmd_timeout(struct hci_dev *hdev)
670{
671 struct btusb_data *data = hci_get_drvdata(hdev);
672 struct gpio_desc *reset_gpio = data->reset_gpio;
673
674 if (++data->cmd_timeout_cnt < 5)
675 return;
676
677 if (!reset_gpio) {
678 bt_dev_err(hdev, "No gpio to reset Realtek device, ignoring");
679 return;
680 }
681
682 /* Toggle the hard reset line. The Realtek device is going to
683 * yank itself off the USB and then replug. The cleanup is handled
684 * correctly on the way out (standard USB disconnect), and the new
685 * device is detected cleanly and bound to the driver again like
686 * it should be.
687 */
688 if (test_and_set_bit(BTUSB_HW_RESET_ACTIVE, &data->flags)) {
689 bt_dev_err(hdev, "last reset failed? Not resetting again");
690 return;
691 }
692
693 bt_dev_err(hdev, "Reset Realtek device via gpio");
Alex Lud7ef0d12019-09-05 10:36:31 +0800694 gpiod_set_value_cansleep(reset_gpio, 1);
Max Chou34682112019-11-27 11:01:07 +0800695 msleep(200);
696 gpiod_set_value_cansleep(reset_gpio, 0);
Alex Lud7ef0d12019-09-05 10:36:31 +0800697}
698
Abhishek Pandit-Subedi69335682020-06-24 11:11:44 -0700699static void btusb_qca_cmd_timeout(struct hci_dev *hdev)
700{
701 struct btusb_data *data = hci_get_drvdata(hdev);
tjiang@codeaurora.org24ff62a2021-09-16 16:41:06 +0800702 struct gpio_desc *reset_gpio = data->reset_gpio;
Abhishek Pandit-Subedi69335682020-06-24 11:11:44 -0700703 int err;
704
705 if (++data->cmd_timeout_cnt < 5)
706 return;
707
tjiang@codeaurora.org24ff62a2021-09-16 16:41:06 +0800708 if (reset_gpio) {
709 bt_dev_err(hdev, "Reset qca device via bt_en gpio");
710
711 /* Toggle the hard reset line. The qca bt device is going to
712 * yank itself off the USB and then replug. The cleanup is handled
713 * correctly on the way out (standard USB disconnect), and the new
714 * device is detected cleanly and bound to the driver again like
715 * it should be.
716 */
717 if (test_and_set_bit(BTUSB_HW_RESET_ACTIVE, &data->flags)) {
718 bt_dev_err(hdev, "last reset failed? Not resetting again");
719 return;
720 }
721
722 gpiod_set_value_cansleep(reset_gpio, 0);
723 msleep(200);
724 gpiod_set_value_cansleep(reset_gpio, 1);
725
726 return;
727 }
728
Abhishek Pandit-Subedi69335682020-06-24 11:11:44 -0700729 bt_dev_err(hdev, "Multiple cmd timeouts seen. Resetting usb device.");
Abhishek Pandit-Subedib980d472020-06-25 16:26:27 -0700730 /* This is not an unbalanced PM reference since the device will reset */
Abhishek Pandit-Subedi69335682020-06-24 11:11:44 -0700731 err = usb_autopm_get_interface(data->intf);
732 if (!err)
733 usb_queue_reset_device(data->intf);
734 else
735 bt_dev_err(hdev, "Failed usb_autopm_get_interface with %d", err);
736}
737
Marcel Holtmann803b5832014-09-16 08:00:29 +0200738static inline void btusb_free_frags(struct btusb_data *data)
739{
740 unsigned long flags;
741
742 spin_lock_irqsave(&data->rxlock, flags);
743
744 kfree_skb(data->evt_skb);
745 data->evt_skb = NULL;
746
747 kfree_skb(data->acl_skb);
748 data->acl_skb = NULL;
749
750 kfree_skb(data->sco_skb);
751 data->sco_skb = NULL;
752
753 spin_unlock_irqrestore(&data->rxlock, flags);
754}
755
Luiz Augusto von Dentz800fe5e2021-12-03 14:38:13 -0800756static int btusb_recv_event(struct btusb_data *data, struct sk_buff *skb)
757{
758 if (data->intr_interval) {
759 /* Trigger dequeue immediatelly if an event is received */
760 schedule_delayed_work(&data->rx_work, 0);
761 }
762
763 return data->recv_event(data->hdev, skb);
764}
765
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200766static int btusb_recv_intr(struct btusb_data *data, void *buffer, int count)
767{
Marcel Holtmann803b5832014-09-16 08:00:29 +0200768 struct sk_buff *skb;
Sebastian Andrzej Siewiora5e50d52018-06-19 23:56:57 +0200769 unsigned long flags;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200770 int err = 0;
771
Sebastian Andrzej Siewiora5e50d52018-06-19 23:56:57 +0200772 spin_lock_irqsave(&data->rxlock, flags);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200773 skb = data->evt_skb;
774
775 while (count) {
776 int len;
777
778 if (!skb) {
779 skb = bt_skb_alloc(HCI_MAX_EVENT_SIZE, GFP_ATOMIC);
780 if (!skb) {
781 err = -ENOMEM;
782 break;
783 }
784
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100785 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
786 hci_skb_expect(skb) = HCI_EVENT_HDR_SIZE;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200787 }
788
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100789 len = min_t(uint, hci_skb_expect(skb), count);
Johannes Berg59ae1d12017-06-16 14:29:20 +0200790 skb_put_data(skb, buffer, len);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200791
792 count -= len;
793 buffer += len;
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100794 hci_skb_expect(skb) -= len;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200795
796 if (skb->len == HCI_EVENT_HDR_SIZE) {
797 /* Complete event header */
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100798 hci_skb_expect(skb) = hci_event_hdr(skb)->plen;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200799
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100800 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
Marcel Holtmann803b5832014-09-16 08:00:29 +0200801 kfree_skb(skb);
802 skb = NULL;
803
804 err = -EILSEQ;
805 break;
806 }
807 }
808
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100809 if (!hci_skb_expect(skb)) {
Marcel Holtmann803b5832014-09-16 08:00:29 +0200810 /* Complete frame */
Luiz Augusto von Dentz800fe5e2021-12-03 14:38:13 -0800811 btusb_recv_event(data, skb);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200812 skb = NULL;
813 }
814 }
815
816 data->evt_skb = skb;
Sebastian Andrzej Siewiora5e50d52018-06-19 23:56:57 +0200817 spin_unlock_irqrestore(&data->rxlock, flags);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200818
819 return err;
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200820}
821
Luiz Augusto von Dentz800fe5e2021-12-03 14:38:13 -0800822static int btusb_recv_acl(struct btusb_data *data, struct sk_buff *skb)
823{
824 /* Only queue ACL packet if intr_interval is set as it means
825 * force_poll_sync has been enabled.
826 */
827 if (!data->intr_interval)
828 return data->recv_acl(data->hdev, skb);
829
830 skb_queue_tail(&data->acl_q, skb);
831 schedule_delayed_work(&data->rx_work, data->intr_interval);
832
833 return 0;
834}
835
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200836static int btusb_recv_bulk(struct btusb_data *data, void *buffer, int count)
837{
Marcel Holtmann803b5832014-09-16 08:00:29 +0200838 struct sk_buff *skb;
Sebastian Andrzej Siewiora5e50d52018-06-19 23:56:57 +0200839 unsigned long flags;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200840 int err = 0;
841
Sebastian Andrzej Siewiora5e50d52018-06-19 23:56:57 +0200842 spin_lock_irqsave(&data->rxlock, flags);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200843 skb = data->acl_skb;
844
845 while (count) {
846 int len;
847
848 if (!skb) {
849 skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
850 if (!skb) {
851 err = -ENOMEM;
852 break;
853 }
854
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100855 hci_skb_pkt_type(skb) = HCI_ACLDATA_PKT;
856 hci_skb_expect(skb) = HCI_ACL_HDR_SIZE;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200857 }
858
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100859 len = min_t(uint, hci_skb_expect(skb), count);
Johannes Berg59ae1d12017-06-16 14:29:20 +0200860 skb_put_data(skb, buffer, len);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200861
862 count -= len;
863 buffer += len;
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100864 hci_skb_expect(skb) -= len;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200865
866 if (skb->len == HCI_ACL_HDR_SIZE) {
867 __le16 dlen = hci_acl_hdr(skb)->dlen;
868
869 /* Complete ACL header */
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100870 hci_skb_expect(skb) = __le16_to_cpu(dlen);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200871
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100872 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
Marcel Holtmann803b5832014-09-16 08:00:29 +0200873 kfree_skb(skb);
874 skb = NULL;
875
876 err = -EILSEQ;
877 break;
878 }
879 }
880
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100881 if (!hci_skb_expect(skb)) {
Marcel Holtmann803b5832014-09-16 08:00:29 +0200882 /* Complete frame */
Luiz Augusto von Dentz800fe5e2021-12-03 14:38:13 -0800883 btusb_recv_acl(data, skb);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200884 skb = NULL;
885 }
886 }
887
888 data->acl_skb = skb;
Sebastian Andrzej Siewiora5e50d52018-06-19 23:56:57 +0200889 spin_unlock_irqrestore(&data->rxlock, flags);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200890
891 return err;
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200892}
893
894static int btusb_recv_isoc(struct btusb_data *data, void *buffer, int count)
895{
Marcel Holtmann803b5832014-09-16 08:00:29 +0200896 struct sk_buff *skb;
Sebastian Andrzej Siewiora5e50d52018-06-19 23:56:57 +0200897 unsigned long flags;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200898 int err = 0;
899
Sebastian Andrzej Siewiora5e50d52018-06-19 23:56:57 +0200900 spin_lock_irqsave(&data->rxlock, flags);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200901 skb = data->sco_skb;
902
903 while (count) {
904 int len;
905
906 if (!skb) {
907 skb = bt_skb_alloc(HCI_MAX_SCO_SIZE, GFP_ATOMIC);
908 if (!skb) {
909 err = -ENOMEM;
910 break;
911 }
912
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100913 hci_skb_pkt_type(skb) = HCI_SCODATA_PKT;
914 hci_skb_expect(skb) = HCI_SCO_HDR_SIZE;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200915 }
916
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100917 len = min_t(uint, hci_skb_expect(skb), count);
Johannes Berg59ae1d12017-06-16 14:29:20 +0200918 skb_put_data(skb, buffer, len);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200919
920 count -= len;
921 buffer += len;
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100922 hci_skb_expect(skb) -= len;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200923
924 if (skb->len == HCI_SCO_HDR_SIZE) {
925 /* Complete SCO header */
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100926 hci_skb_expect(skb) = hci_sco_hdr(skb)->dlen;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200927
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100928 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
Marcel Holtmann803b5832014-09-16 08:00:29 +0200929 kfree_skb(skb);
930 skb = NULL;
931
932 err = -EILSEQ;
933 break;
934 }
935 }
936
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100937 if (!hci_skb_expect(skb)) {
Marcel Holtmann803b5832014-09-16 08:00:29 +0200938 /* Complete frame */
939 hci_recv_frame(data->hdev, skb);
940 skb = NULL;
941 }
942 }
943
944 data->sco_skb = skb;
Sebastian Andrzej Siewiora5e50d52018-06-19 23:56:57 +0200945 spin_unlock_irqrestore(&data->rxlock, flags);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200946
947 return err;
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200948}
949
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200950static void btusb_intr_complete(struct urb *urb)
951{
952 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +0100953 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200954 int err;
955
Marcel Holtmann89e75332014-09-16 04:44:50 +0200956 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
957 urb->actual_length);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200958
959 if (!test_bit(HCI_RUNNING, &hdev->flags))
960 return;
961
962 if (urb->status == 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200963 hdev->stat.byte_rx += urb->actual_length;
964
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200965 if (btusb_recv_intr(data, urb->transfer_buffer,
966 urb->actual_length) < 0) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +0100967 bt_dev_err(hdev, "corrupted event packet");
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200968 hdev->stat.err_rx++;
969 }
Champion Chen85560c42014-09-06 14:06:08 -0500970 } else if (urb->status == -ENOENT) {
971 /* Avoid suspend failed when usb_kill_urb */
972 return;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200973 }
974
975 if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
976 return;
977
Oliver Neukum7bee5492009-08-24 23:44:59 +0200978 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200979 usb_anchor_urb(urb, &data->intr_anchor);
980
981 err = usb_submit_urb(urb, GFP_ATOMIC);
982 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200983 /* -EPERM: urb is being killed;
Derek Robsond98422c2017-07-22 13:47:07 +1200984 * -ENODEV: device got disconnected
985 */
Paul Bolle4935f1c2011-08-09 17:16:28 +0200986 if (err != -EPERM && err != -ENODEV)
Marcel Holtmann2064ee32017-10-30 10:42:59 +0100987 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
988 urb, -err);
Benjamin Bergc97a7472021-12-03 15:59:02 +0100989 if (err != -EPERM)
990 hci_cmd_sync_cancel(hdev, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200991 usb_unanchor_urb(urb);
992 }
993}
994
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100995static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200996{
David Herrmann155961e2012-02-09 21:58:32 +0100997 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200998 struct urb *urb;
999 unsigned char *buf;
1000 unsigned int pipe;
1001 int err, size;
1002
1003 BT_DBG("%s", hdev->name);
1004
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001005 if (!data->intr_ep)
1006 return -ENODEV;
1007
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001008 urb = usb_alloc_urb(0, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001009 if (!urb)
1010 return -ENOMEM;
1011
1012 size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
1013
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001014 buf = kmalloc(size, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001015 if (!buf) {
1016 usb_free_urb(urb);
1017 return -ENOMEM;
1018 }
1019
1020 pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
1021
1022 usb_fill_int_urb(urb, data->udev, pipe, buf, size,
Marcel Holtmann89e75332014-09-16 04:44:50 +02001023 btusb_intr_complete, hdev, data->intr_ep->bInterval);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001024
1025 urb->transfer_flags |= URB_FREE_BUFFER;
1026
1027 usb_anchor_urb(urb, &data->intr_anchor);
1028
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001029 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001030 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +02001031 if (err != -EPERM && err != -ENODEV)
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001032 bt_dev_err(hdev, "urb %p submission failed (%d)",
1033 urb, -err);
Benjamin Bergc97a7472021-12-03 15:59:02 +01001034 if (err != -EPERM)
1035 hci_cmd_sync_cancel(hdev, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001036 usb_unanchor_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001037 }
1038
Luiz Augusto von Dentz800fe5e2021-12-03 14:38:13 -08001039 /* Only initialize intr_interval if URB poll sync is enabled */
1040 if (!data->poll_sync)
1041 goto done;
1042
1043 /* The units are frames (milliseconds) for full and low speed devices,
1044 * and microframes (1/8 millisecond) for highspeed and SuperSpeed
1045 * devices.
1046 *
1047 * This is done once on open/resume so it shouldn't change even if
1048 * force_poll_sync changes.
1049 */
1050 switch (urb->dev->speed) {
1051 case USB_SPEED_SUPER_PLUS:
1052 case USB_SPEED_SUPER: /* units are 125us */
1053 data->intr_interval = usecs_to_jiffies(urb->interval * 125);
1054 break;
1055 default:
1056 data->intr_interval = msecs_to_jiffies(urb->interval);
1057 break;
1058 }
1059
1060done:
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001061 usb_free_urb(urb);
1062
1063 return err;
1064}
1065
1066static void btusb_bulk_complete(struct urb *urb)
1067{
1068 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +01001069 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001070 int err;
1071
Marcel Holtmann89e75332014-09-16 04:44:50 +02001072 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1073 urb->actual_length);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001074
1075 if (!test_bit(HCI_RUNNING, &hdev->flags))
1076 return;
1077
1078 if (urb->status == 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001079 hdev->stat.byte_rx += urb->actual_length;
1080
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +01001081 if (data->recv_bulk(data, urb->transfer_buffer,
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +02001082 urb->actual_length) < 0) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001083 bt_dev_err(hdev, "corrupted ACL packet");
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001084 hdev->stat.err_rx++;
1085 }
Champion Chen85560c42014-09-06 14:06:08 -05001086 } else if (urb->status == -ENOENT) {
1087 /* Avoid suspend failed when usb_kill_urb */
1088 return;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001089 }
1090
1091 if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
1092 return;
1093
1094 usb_anchor_urb(urb, &data->bulk_anchor);
Oliver Neukum652fd782009-12-16 19:23:43 +01001095 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001096
1097 err = usb_submit_urb(urb, GFP_ATOMIC);
1098 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +02001099 /* -EPERM: urb is being killed;
Derek Robsond98422c2017-07-22 13:47:07 +12001100 * -ENODEV: device got disconnected
1101 */
Paul Bolle4935f1c2011-08-09 17:16:28 +02001102 if (err != -EPERM && err != -ENODEV)
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001103 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
1104 urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001105 usb_unanchor_urb(urb);
1106 }
1107}
1108
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001109static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001110{
David Herrmann155961e2012-02-09 21:58:32 +01001111 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001112 struct urb *urb;
1113 unsigned char *buf;
1114 unsigned int pipe;
Vikram Kandukuri290ba202009-07-02 14:31:59 +05301115 int err, size = HCI_MAX_FRAME_SIZE;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001116
1117 BT_DBG("%s", hdev->name);
1118
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001119 if (!data->bulk_rx_ep)
1120 return -ENODEV;
1121
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001122 urb = usb_alloc_urb(0, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001123 if (!urb)
1124 return -ENOMEM;
1125
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001126 buf = kmalloc(size, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001127 if (!buf) {
1128 usb_free_urb(urb);
1129 return -ENOMEM;
1130 }
1131
1132 pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
1133
Marcel Holtmann89e75332014-09-16 04:44:50 +02001134 usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
1135 btusb_bulk_complete, hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001136
1137 urb->transfer_flags |= URB_FREE_BUFFER;
1138
Oliver Neukum7bee5492009-08-24 23:44:59 +02001139 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001140 usb_anchor_urb(urb, &data->bulk_anchor);
1141
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001142 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001143 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +02001144 if (err != -EPERM && err != -ENODEV)
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001145 bt_dev_err(hdev, "urb %p submission failed (%d)",
1146 urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001147 usb_unanchor_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001148 }
1149
1150 usb_free_urb(urb);
1151
1152 return err;
1153}
1154
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001155static void btusb_isoc_complete(struct urb *urb)
1156{
1157 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +01001158 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001159 int i, err;
1160
Marcel Holtmann89e75332014-09-16 04:44:50 +02001161 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1162 urb->actual_length);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001163
1164 if (!test_bit(HCI_RUNNING, &hdev->flags))
1165 return;
1166
1167 if (urb->status == 0) {
1168 for (i = 0; i < urb->number_of_packets; i++) {
1169 unsigned int offset = urb->iso_frame_desc[i].offset;
1170 unsigned int length = urb->iso_frame_desc[i].actual_length;
1171
1172 if (urb->iso_frame_desc[i].status)
1173 continue;
1174
1175 hdev->stat.byte_rx += length;
1176
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +02001177 if (btusb_recv_isoc(data, urb->transfer_buffer + offset,
1178 length) < 0) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001179 bt_dev_err(hdev, "corrupted SCO packet");
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001180 hdev->stat.err_rx++;
1181 }
1182 }
Champion Chen85560c42014-09-06 14:06:08 -05001183 } else if (urb->status == -ENOENT) {
1184 /* Avoid suspend failed when usb_kill_urb */
1185 return;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001186 }
1187
1188 if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags))
1189 return;
1190
1191 usb_anchor_urb(urb, &data->isoc_anchor);
1192
1193 err = usb_submit_urb(urb, GFP_ATOMIC);
1194 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +02001195 /* -EPERM: urb is being killed;
Derek Robsond98422c2017-07-22 13:47:07 +12001196 * -ENODEV: device got disconnected
1197 */
Paul Bolle4935f1c2011-08-09 17:16:28 +02001198 if (err != -EPERM && err != -ENODEV)
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001199 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
1200 urb, -err);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001201 usb_unanchor_urb(urb);
1202 }
1203}
1204
Sathish Narasimmanbaac6272020-04-03 21:43:59 +02001205static inline void __fill_isoc_descriptor_msbc(struct urb *urb, int len,
1206 int mtu, struct btusb_data *data)
1207{
1208 int i, offset = 0;
1209 unsigned int interval;
1210
1211 BT_DBG("len %d mtu %d", len, mtu);
1212
1213 /* For mSBC ALT 6 setting the host will send the packet at continuous
1214 * flow. As per core spec 5, vol 4, part B, table 2.1. For ALT setting
1215 * 6 the HCI PACKET INTERVAL should be 7.5ms for every usb packets.
1216 * To maintain the rate we send 63bytes of usb packets alternatively for
1217 * 7ms and 8ms to maintain the rate as 7.5ms.
1218 */
1219 if (data->usb_alt6_packet_flow) {
1220 interval = 7;
1221 data->usb_alt6_packet_flow = false;
1222 } else {
1223 interval = 6;
1224 data->usb_alt6_packet_flow = true;
1225 }
1226
1227 for (i = 0; i < interval; i++) {
1228 urb->iso_frame_desc[i].offset = offset;
1229 urb->iso_frame_desc[i].length = offset;
1230 }
1231
1232 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
1233 urb->iso_frame_desc[i].offset = offset;
1234 urb->iso_frame_desc[i].length = len;
1235 i++;
1236 }
1237
1238 urb->number_of_packets = i;
1239}
1240
Jesper Juhl42b16b32011-01-17 00:09:38 +01001241static inline void __fill_isoc_descriptor(struct urb *urb, int len, int mtu)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001242{
1243 int i, offset = 0;
1244
1245 BT_DBG("len %d mtu %d", len, mtu);
1246
1247 for (i = 0; i < BTUSB_MAX_ISOC_FRAMES && len >= mtu;
1248 i++, offset += mtu, len -= mtu) {
1249 urb->iso_frame_desc[i].offset = offset;
1250 urb->iso_frame_desc[i].length = mtu;
1251 }
1252
1253 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
1254 urb->iso_frame_desc[i].offset = offset;
1255 urb->iso_frame_desc[i].length = len;
1256 i++;
1257 }
1258
1259 urb->number_of_packets = i;
1260}
1261
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001262static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001263{
David Herrmann155961e2012-02-09 21:58:32 +01001264 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001265 struct urb *urb;
1266 unsigned char *buf;
1267 unsigned int pipe;
1268 int err, size;
1269
1270 BT_DBG("%s", hdev->name);
1271
1272 if (!data->isoc_rx_ep)
1273 return -ENODEV;
1274
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001275 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001276 if (!urb)
1277 return -ENOMEM;
1278
1279 size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) *
1280 BTUSB_MAX_ISOC_FRAMES;
1281
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001282 buf = kmalloc(size, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001283 if (!buf) {
1284 usb_free_urb(urb);
1285 return -ENOMEM;
1286 }
1287
1288 pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress);
1289
Bing Zhaofa0fb932011-12-20 18:19:00 -08001290 usb_fill_int_urb(urb, data->udev, pipe, buf, size, btusb_isoc_complete,
Marcel Holtmann89e75332014-09-16 04:44:50 +02001291 hdev, data->isoc_rx_ep->bInterval);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001292
Marcel Holtmann89e75332014-09-16 04:44:50 +02001293 urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001294
1295 __fill_isoc_descriptor(urb, size,
Marcel Holtmann89e75332014-09-16 04:44:50 +02001296 le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize));
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001297
1298 usb_anchor_urb(urb, &data->isoc_anchor);
1299
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001300 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001301 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +02001302 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 Holtmann9bfa35f2008-08-18 13:23:52 +02001305 usb_unanchor_urb(urb);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001306 }
1307
1308 usb_free_urb(urb);
1309
1310 return err;
1311}
1312
Marcel Holtmann9d08f502015-10-08 20:23:08 +02001313static void btusb_diag_complete(struct urb *urb)
1314{
1315 struct hci_dev *hdev = urb->context;
1316 struct btusb_data *data = hci_get_drvdata(hdev);
1317 int err;
1318
1319 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1320 urb->actual_length);
1321
1322 if (urb->status == 0) {
1323 struct sk_buff *skb;
1324
1325 skb = bt_skb_alloc(urb->actual_length, GFP_ATOMIC);
1326 if (skb) {
Johannes Berg59ae1d12017-06-16 14:29:20 +02001327 skb_put_data(skb, urb->transfer_buffer,
1328 urb->actual_length);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02001329 hci_recv_diag(hdev, skb);
1330 }
1331 } else if (urb->status == -ENOENT) {
1332 /* Avoid suspend failed when usb_kill_urb */
1333 return;
1334 }
1335
1336 if (!test_bit(BTUSB_DIAG_RUNNING, &data->flags))
1337 return;
1338
1339 usb_anchor_urb(urb, &data->diag_anchor);
1340 usb_mark_last_busy(data->udev);
1341
1342 err = usb_submit_urb(urb, GFP_ATOMIC);
1343 if (err < 0) {
1344 /* -EPERM: urb is being killed;
Derek Robsond98422c2017-07-22 13:47:07 +12001345 * -ENODEV: device got disconnected
1346 */
Marcel Holtmann9d08f502015-10-08 20:23:08 +02001347 if (err != -EPERM && err != -ENODEV)
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001348 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
1349 urb, -err);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02001350 usb_unanchor_urb(urb);
1351 }
1352}
1353
1354static int btusb_submit_diag_urb(struct hci_dev *hdev, gfp_t mem_flags)
1355{
1356 struct btusb_data *data = hci_get_drvdata(hdev);
1357 struct urb *urb;
1358 unsigned char *buf;
1359 unsigned int pipe;
1360 int err, size = HCI_MAX_FRAME_SIZE;
1361
1362 BT_DBG("%s", hdev->name);
1363
1364 if (!data->diag_rx_ep)
1365 return -ENODEV;
1366
1367 urb = usb_alloc_urb(0, mem_flags);
1368 if (!urb)
1369 return -ENOMEM;
1370
1371 buf = kmalloc(size, mem_flags);
1372 if (!buf) {
1373 usb_free_urb(urb);
1374 return -ENOMEM;
1375 }
1376
1377 pipe = usb_rcvbulkpipe(data->udev, data->diag_rx_ep->bEndpointAddress);
1378
1379 usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
1380 btusb_diag_complete, hdev);
1381
1382 urb->transfer_flags |= URB_FREE_BUFFER;
1383
1384 usb_mark_last_busy(data->udev);
1385 usb_anchor_urb(urb, &data->diag_anchor);
1386
1387 err = usb_submit_urb(urb, mem_flags);
1388 if (err < 0) {
1389 if (err != -EPERM && err != -ENODEV)
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001390 bt_dev_err(hdev, "urb %p submission failed (%d)",
1391 urb, -err);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02001392 usb_unanchor_urb(urb);
1393 }
1394
1395 usb_free_urb(urb);
1396
1397 return err;
1398}
1399
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001400static void btusb_tx_complete(struct urb *urb)
1401{
1402 struct sk_buff *skb = urb->context;
Marcel Holtmann89e75332014-09-16 04:44:50 +02001403 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
David Herrmann155961e2012-02-09 21:58:32 +01001404 struct btusb_data *data = hci_get_drvdata(hdev);
Sebastian Andrzej Siewiora5e50d52018-06-19 23:56:57 +02001405 unsigned long flags;
Oliver Neukum7bee5492009-08-24 23:44:59 +02001406
Marcel Holtmann89e75332014-09-16 04:44:50 +02001407 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1408 urb->actual_length);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001409
1410 if (!test_bit(HCI_RUNNING, &hdev->flags))
1411 goto done;
1412
Benjamin Bergc97a7472021-12-03 15:59:02 +01001413 if (!urb->status) {
Oliver Neukum7bee5492009-08-24 23:44:59 +02001414 hdev->stat.byte_tx += urb->transfer_buffer_length;
Benjamin Bergc97a7472021-12-03 15:59:02 +01001415 } else {
1416 if (hci_skb_pkt_type(skb) == HCI_COMMAND_PKT)
1417 hci_cmd_sync_cancel(hdev, -urb->status);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001418 hdev->stat.err_tx++;
Benjamin Bergc97a7472021-12-03 15:59:02 +01001419 }
Oliver Neukum7bee5492009-08-24 23:44:59 +02001420
1421done:
Sebastian Andrzej Siewiora5e50d52018-06-19 23:56:57 +02001422 spin_lock_irqsave(&data->txlock, flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001423 data->tx_in_flight--;
Sebastian Andrzej Siewiora5e50d52018-06-19 23:56:57 +02001424 spin_unlock_irqrestore(&data->txlock, flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001425
1426 kfree(urb->setup_packet);
1427
1428 kfree_skb(skb);
1429}
1430
1431static void btusb_isoc_tx_complete(struct urb *urb)
1432{
1433 struct sk_buff *skb = urb->context;
Marcel Holtmann89e75332014-09-16 04:44:50 +02001434 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001435
Marcel Holtmann89e75332014-09-16 04:44:50 +02001436 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1437 urb->actual_length);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001438
1439 if (!test_bit(HCI_RUNNING, &hdev->flags))
1440 goto done;
1441
1442 if (!urb->status)
1443 hdev->stat.byte_tx += urb->transfer_buffer_length;
1444 else
1445 hdev->stat.err_tx++;
1446
1447done:
1448 kfree(urb->setup_packet);
1449
1450 kfree_skb(skb);
1451}
1452
1453static int btusb_open(struct hci_dev *hdev)
1454{
David Herrmann155961e2012-02-09 21:58:32 +01001455 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001456 int err;
1457
1458 BT_DBG("%s", hdev->name);
1459
Ethan Hsiehc7e163f2016-10-07 12:06:42 +08001460 err = usb_autopm_get_interface(data->intf);
1461 if (err < 0)
1462 return err;
1463
Kim, Ben Young Taeace31982015-02-15 23:06:14 +00001464 /* Patching USB firmware files prior to starting any URBs of HCI path
1465 * It is more safe to use USB bulk channel for downloading USB patch
1466 */
1467 if (data->setup_on_usb) {
1468 err = data->setup_on_usb(hdev);
Marcel Holtmanneb500422015-04-16 23:15:50 +02001469 if (err < 0)
Oliver Neukum3d44a6f2019-11-14 16:01:18 +01001470 goto setup_fail;
Kim, Ben Young Taeace31982015-02-15 23:06:14 +00001471 }
1472
Oliver Neukum7bee5492009-08-24 23:44:59 +02001473 data->intf->needs_remote_wakeup = 1;
1474
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001475 if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +02001476 goto done;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001477
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001478 err = btusb_submit_intr_urb(hdev, GFP_KERNEL);
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001479 if (err < 0)
1480 goto failed;
1481
1482 err = btusb_submit_bulk_urb(hdev, GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001483 if (err < 0) {
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001484 usb_kill_anchored_urbs(&data->intr_anchor);
1485 goto failed;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001486 }
1487
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001488 set_bit(BTUSB_BULK_RUNNING, &data->flags);
1489 btusb_submit_bulk_urb(hdev, GFP_KERNEL);
1490
Marcel Holtmann9d08f502015-10-08 20:23:08 +02001491 if (data->diag) {
1492 if (!btusb_submit_diag_urb(hdev, GFP_KERNEL))
1493 set_bit(BTUSB_DIAG_RUNNING, &data->flags);
1494 }
1495
Oliver Neukum7bee5492009-08-24 23:44:59 +02001496done:
1497 usb_autopm_put_interface(data->intf);
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001498 return 0;
1499
1500failed:
1501 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Oliver Neukum3d44a6f2019-11-14 16:01:18 +01001502setup_fail:
Oliver Neukum7bee5492009-08-24 23:44:59 +02001503 usb_autopm_put_interface(data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001504 return err;
1505}
1506
Oliver Neukum7bee5492009-08-24 23:44:59 +02001507static void btusb_stop_traffic(struct btusb_data *data)
1508{
1509 usb_kill_anchored_urbs(&data->intr_anchor);
1510 usb_kill_anchored_urbs(&data->bulk_anchor);
1511 usb_kill_anchored_urbs(&data->isoc_anchor);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02001512 usb_kill_anchored_urbs(&data->diag_anchor);
Sean Wanga1c49c432019-06-02 08:02:48 +08001513 usb_kill_anchored_urbs(&data->ctrl_anchor);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001514}
1515
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001516static int btusb_close(struct hci_dev *hdev)
1517{
David Herrmann155961e2012-02-09 21:58:32 +01001518 struct btusb_data *data = hci_get_drvdata(hdev);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001519 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001520
1521 BT_DBG("%s", hdev->name);
1522
Luiz Augusto von Dentz800fe5e2021-12-03 14:38:13 -08001523 cancel_delayed_work(&data->rx_work);
Marcel Holtmanne8c3c3d2008-09-23 00:16:36 +02001524 cancel_work_sync(&data->work);
Linus Torvalds404291a2009-11-11 13:32:29 -08001525 cancel_work_sync(&data->waker);
Marcel Holtmanne8c3c3d2008-09-23 00:16:36 +02001526
Luiz Augusto von Dentz800fe5e2021-12-03 14:38:13 -08001527 skb_queue_purge(&data->acl_q);
1528
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001529 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001530 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001531 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02001532 clear_bit(BTUSB_DIAG_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001533
1534 btusb_stop_traffic(data);
Marcel Holtmann803b5832014-09-16 08:00:29 +02001535 btusb_free_frags(data);
1536
Oliver Neukum7bee5492009-08-24 23:44:59 +02001537 err = usb_autopm_get_interface(data->intf);
1538 if (err < 0)
Oliver Neukum7b8e2c12009-11-13 14:26:23 +01001539 goto failed;
Oliver Neukum7bee5492009-08-24 23:44:59 +02001540
1541 data->intf->needs_remote_wakeup = 0;
Alex Lu9e455242019-08-14 20:02:52 +08001542
1543 /* Enable remote wake up for auto-suspend */
Max Chou8274db02021-08-17 11:03:12 +08001544 if (test_bit(BTUSB_WAKEUP_AUTOSUSPEND, &data->flags))
Alex Lu9e455242019-08-14 20:02:52 +08001545 data->intf->needs_remote_wakeup = 1;
1546
Oliver Neukum7bee5492009-08-24 23:44:59 +02001547 usb_autopm_put_interface(data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001548
Oliver Neukum7b8e2c12009-11-13 14:26:23 +01001549failed:
1550 usb_scuttle_anchored_urbs(&data->deferred);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001551 return 0;
1552}
1553
1554static int btusb_flush(struct hci_dev *hdev)
1555{
David Herrmann155961e2012-02-09 21:58:32 +01001556 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001557
1558 BT_DBG("%s", hdev->name);
1559
Luiz Augusto von Dentz800fe5e2021-12-03 14:38:13 -08001560 cancel_delayed_work(&data->rx_work);
1561
1562 skb_queue_purge(&data->acl_q);
1563
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001564 usb_kill_anchored_urbs(&data->tx_anchor);
Marcel Holtmann803b5832014-09-16 08:00:29 +02001565 btusb_free_frags(data);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001566
1567 return 0;
1568}
1569
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001570static struct urb *alloc_ctrl_urb(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001571{
David Herrmann155961e2012-02-09 21:58:32 +01001572 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001573 struct usb_ctrlrequest *dr;
1574 struct urb *urb;
1575 unsigned int pipe;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001576
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001577 urb = usb_alloc_urb(0, GFP_KERNEL);
1578 if (!urb)
1579 return ERR_PTR(-ENOMEM);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001580
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001581 dr = kmalloc(sizeof(*dr), GFP_KERNEL);
1582 if (!dr) {
1583 usb_free_urb(urb);
1584 return ERR_PTR(-ENOMEM);
1585 }
1586
1587 dr->bRequestType = data->cmdreq_type;
Marcel Holtmann893ba542015-01-28 20:27:34 -08001588 dr->bRequest = data->cmdreq;
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001589 dr->wIndex = 0;
1590 dr->wValue = 0;
1591 dr->wLength = __cpu_to_le16(skb->len);
1592
1593 pipe = usb_sndctrlpipe(data->udev, 0x00);
1594
Marcel Holtmann89e75332014-09-16 04:44:50 +02001595 usb_fill_control_urb(urb, data->udev, pipe, (void *)dr,
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001596 skb->data, skb->len, btusb_tx_complete, skb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001597
Marcel Holtmann89e75332014-09-16 04:44:50 +02001598 skb->dev = (void *)hdev;
Marcel Holtmann7bd8f092013-10-11 06:19:18 -07001599
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001600 return urb;
1601}
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001602
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001603static struct urb *alloc_bulk_urb(struct hci_dev *hdev, struct sk_buff *skb)
1604{
1605 struct btusb_data *data = hci_get_drvdata(hdev);
1606 struct urb *urb;
1607 unsigned int pipe;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001608
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001609 if (!data->bulk_tx_ep)
1610 return ERR_PTR(-ENODEV);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001611
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001612 urb = usb_alloc_urb(0, GFP_KERNEL);
1613 if (!urb)
1614 return ERR_PTR(-ENOMEM);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001615
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001616 pipe = usb_sndbulkpipe(data->udev, data->bulk_tx_ep->bEndpointAddress);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001617
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001618 usb_fill_bulk_urb(urb, data->udev, pipe,
1619 skb->data, skb->len, btusb_tx_complete, skb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001620
Marcel Holtmann89e75332014-09-16 04:44:50 +02001621 skb->dev = (void *)hdev;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001622
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001623 return urb;
1624}
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001625
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001626static struct urb *alloc_isoc_urb(struct hci_dev *hdev, struct sk_buff *skb)
1627{
1628 struct btusb_data *data = hci_get_drvdata(hdev);
1629 struct urb *urb;
1630 unsigned int pipe;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001631
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001632 if (!data->isoc_tx_ep)
1633 return ERR_PTR(-ENODEV);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001634
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001635 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_KERNEL);
1636 if (!urb)
1637 return ERR_PTR(-ENOMEM);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001638
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001639 pipe = usb_sndisocpipe(data->udev, data->isoc_tx_ep->bEndpointAddress);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001640
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001641 usb_fill_int_urb(urb, data->udev, pipe,
1642 skb->data, skb->len, btusb_isoc_tx_complete,
1643 skb, data->isoc_tx_ep->bInterval);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001644
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001645 urb->transfer_flags = URB_ISO_ASAP;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001646
Sathish Narasimmanbaac6272020-04-03 21:43:59 +02001647 if (data->isoc_altsetting == 6)
1648 __fill_isoc_descriptor_msbc(urb, skb->len,
1649 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize),
1650 data);
1651 else
1652 __fill_isoc_descriptor(urb, skb->len,
1653 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
Marcel Holtmann89e75332014-09-16 04:44:50 +02001654 skb->dev = (void *)hdev;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001655
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001656 return urb;
1657}
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001658
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001659static int submit_tx_urb(struct hci_dev *hdev, struct urb *urb)
1660{
1661 struct btusb_data *data = hci_get_drvdata(hdev);
1662 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001663
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001664 usb_anchor_urb(urb, &data->tx_anchor);
1665
Johan Hedberge9753ef2014-09-14 08:49:34 +03001666 err = usb_submit_urb(urb, GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001667 if (err < 0) {
Paul Bolle5a9b80e2011-10-09 12:12:16 +02001668 if (err != -EPERM && err != -ENODEV)
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001669 bt_dev_err(hdev, "urb %p submission failed (%d)",
1670 urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001671 kfree(urb->setup_packet);
1672 usb_unanchor_urb(urb);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001673 } else {
1674 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001675 }
1676
Cong Wang54a8a792011-11-22 09:32:57 +08001677 usb_free_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001678 return err;
1679}
1680
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001681static int submit_or_queue_tx_urb(struct hci_dev *hdev, struct urb *urb)
1682{
1683 struct btusb_data *data = hci_get_drvdata(hdev);
1684 unsigned long flags;
1685 bool suspending;
1686
1687 spin_lock_irqsave(&data->txlock, flags);
1688 suspending = test_bit(BTUSB_SUSPENDING, &data->flags);
1689 if (!suspending)
1690 data->tx_in_flight++;
1691 spin_unlock_irqrestore(&data->txlock, flags);
1692
1693 if (!suspending)
1694 return submit_tx_urb(hdev, urb);
1695
1696 usb_anchor_urb(urb, &data->deferred);
1697 schedule_work(&data->waker);
1698
1699 usb_free_urb(urb);
1700 return 0;
1701}
1702
1703static int btusb_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
1704{
1705 struct urb *urb;
1706
1707 BT_DBG("%s", hdev->name);
1708
Marcel Holtmann618e8bc2015-11-05 07:33:56 +01001709 switch (hci_skb_pkt_type(skb)) {
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001710 case HCI_COMMAND_PKT:
1711 urb = alloc_ctrl_urb(hdev, skb);
1712 if (IS_ERR(urb))
1713 return PTR_ERR(urb);
1714
1715 hdev->stat.cmd_tx++;
1716 return submit_or_queue_tx_urb(hdev, urb);
1717
1718 case HCI_ACLDATA_PKT:
1719 urb = alloc_bulk_urb(hdev, skb);
1720 if (IS_ERR(urb))
1721 return PTR_ERR(urb);
1722
1723 hdev->stat.acl_tx++;
1724 return submit_or_queue_tx_urb(hdev, urb);
1725
1726 case HCI_SCODATA_PKT:
1727 if (hci_conn_num(hdev, SCO_LINK) < 1)
1728 return -ENODEV;
1729
1730 urb = alloc_isoc_urb(hdev, skb);
1731 if (IS_ERR(urb))
1732 return PTR_ERR(urb);
1733
1734 hdev->stat.sco_tx++;
1735 return submit_tx_urb(hdev, urb);
1736 }
1737
1738 return -EILSEQ;
1739}
1740
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001741static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
1742{
David Herrmann155961e2012-02-09 21:58:32 +01001743 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001744
1745 BT_DBG("%s evt %d", hdev->name, evt);
1746
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001747 if (hci_conn_num(hdev, SCO_LINK) != data->sco_num) {
1748 data->sco_num = hci_conn_num(hdev, SCO_LINK);
Sathish Narasimmanbaac6272020-04-03 21:43:59 +02001749 data->air_mode = evt;
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001750 schedule_work(&data->work);
Marcel Holtmanna780efa2008-11-30 12:17:12 +01001751 }
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001752}
1753
Jesper Juhl42b16b32011-01-17 00:09:38 +01001754static inline int __set_isoc_interface(struct hci_dev *hdev, int altsetting)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001755{
David Herrmann155961e2012-02-09 21:58:32 +01001756 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001757 struct usb_interface *intf = data->isoc;
1758 struct usb_endpoint_descriptor *ep_desc;
1759 int i, err;
1760
1761 if (!data->isoc)
1762 return -ENODEV;
1763
Marcel Holtmann459232f2017-10-24 19:42:45 +02001764 err = usb_set_interface(data->udev, data->isoc_ifnum, altsetting);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001765 if (err < 0) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001766 bt_dev_err(hdev, "setting interface failed (%d)", -err);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001767 return err;
1768 }
1769
1770 data->isoc_altsetting = altsetting;
1771
1772 data->isoc_tx_ep = NULL;
1773 data->isoc_rx_ep = NULL;
1774
1775 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
1776 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
1777
1778 if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) {
1779 data->isoc_tx_ep = ep_desc;
1780 continue;
1781 }
1782
1783 if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) {
1784 data->isoc_rx_ep = ep_desc;
1785 continue;
1786 }
1787 }
1788
1789 if (!data->isoc_tx_ep || !data->isoc_rx_ep) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001790 bt_dev_err(hdev, "invalid SCO descriptors");
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001791 return -ENODEV;
1792 }
1793
1794 return 0;
1795}
1796
Sathish Narasimmanbaac6272020-04-03 21:43:59 +02001797static int btusb_switch_alt_setting(struct hci_dev *hdev, int new_alts)
1798{
1799 struct btusb_data *data = hci_get_drvdata(hdev);
1800 int err;
1801
1802 if (data->isoc_altsetting != new_alts) {
1803 unsigned long flags;
1804
1805 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1806 usb_kill_anchored_urbs(&data->isoc_anchor);
1807
1808 /* When isochronous alternate setting needs to be
1809 * changed, because SCO connection has been added
1810 * or removed, a packet fragment may be left in the
1811 * reassembling state. This could lead to wrongly
1812 * assembled fragments.
1813 *
1814 * Clear outstanding fragment when selecting a new
1815 * alternate setting.
1816 */
1817 spin_lock_irqsave(&data->rxlock, flags);
1818 kfree_skb(data->sco_skb);
1819 data->sco_skb = NULL;
1820 spin_unlock_irqrestore(&data->rxlock, flags);
1821
1822 err = __set_isoc_interface(hdev, new_alts);
1823 if (err < 0)
1824 return err;
1825 }
1826
1827 if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
1828 if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0)
1829 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1830 else
1831 btusb_submit_isoc_urb(hdev, GFP_KERNEL);
1832 }
1833
1834 return 0;
1835}
1836
1837static struct usb_host_interface *btusb_find_altsetting(struct btusb_data *data,
1838 int alt)
1839{
1840 struct usb_interface *intf = data->isoc;
1841 int i;
1842
1843 BT_DBG("Looking for Alt no :%d", alt);
1844
Sathish Narasimmanfcd156ee2020-04-08 10:57:03 +05301845 if (!intf)
1846 return NULL;
1847
Sathish Narasimmanbaac6272020-04-03 21:43:59 +02001848 for (i = 0; i < intf->num_altsetting; i++) {
1849 if (intf->altsetting[i].desc.bAlternateSetting == alt)
1850 return &intf->altsetting[i];
1851 }
1852
1853 return NULL;
1854}
1855
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001856static void btusb_work(struct work_struct *work)
1857{
1858 struct btusb_data *data = container_of(work, struct btusb_data, work);
1859 struct hci_dev *hdev = data->hdev;
Sathish Narasimmanbaac6272020-04-03 21:43:59 +02001860 int new_alts = 0;
Oliver Neukum7bee5492009-08-24 23:44:59 +02001861 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001862
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001863 if (data->sco_num > 0) {
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -03001864 if (!test_bit(BTUSB_DID_ISO_RESUME, &data->flags)) {
Oliver Neukum8efdd0c2011-02-11 13:00:06 +01001865 err = usb_autopm_get_interface(data->isoc ? data->isoc : data->intf);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001866 if (err < 0) {
1867 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1868 usb_kill_anchored_urbs(&data->isoc_anchor);
1869 return;
1870 }
1871
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -03001872 set_bit(BTUSB_DID_ISO_RESUME, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001873 }
Mikel Astizf4001d22012-04-11 08:48:51 +02001874
Sathish Narasimmanbaac6272020-04-03 21:43:59 +02001875 if (data->air_mode == HCI_NOTIFY_ENABLE_SCO_CVSD) {
1876 if (hdev->voice_setting & 0x0020) {
1877 static const int alts[3] = { 2, 4, 5 };
Marcel Holtmann89e75332014-09-16 04:44:50 +02001878
Sathish Narasimmanbaac6272020-04-03 21:43:59 +02001879 new_alts = alts[data->sco_num - 1];
1880 } else {
1881 new_alts = data->sco_num;
1882 }
1883 } else if (data->air_mode == HCI_NOTIFY_ENABLE_SCO_TRANSP) {
Trent Piepho517b6932020-12-09 17:20:03 -08001884 /* Bluetooth USB spec recommends alt 6 (63 bytes), but
1885 * many adapters do not support it. Alt 1 appears to
1886 * work for all adapters that do not have alt 6, and
Pauli Virtanen55981d32021-07-26 21:02:06 +03001887 * which work with WBS at all. Some devices prefer
1888 * alt 3 (HCI payload >= 60 Bytes let air packet
1889 * data satisfy 60 bytes), requiring
1890 * MTU >= 3 (packets) * 25 (size) - 3 (headers) = 72
1891 * see also Core spec 5, vol 4, B 2.1.1 & Table 2.1.
Trent Piepho517b6932020-12-09 17:20:03 -08001892 */
Pauli Virtanen55981d32021-07-26 21:02:06 +03001893 if (btusb_find_altsetting(data, 6))
1894 new_alts = 6;
1895 else if (btusb_find_altsetting(data, 3) &&
1896 hdev->sco_mtu >= 72 &&
1897 test_bit(BTUSB_USE_ALT3_FOR_WBS, &data->flags))
Hilda Wue848dbd2021-05-14 11:19:01 +08001898 new_alts = 3;
Pauli Virtanen55981d32021-07-26 21:02:06 +03001899 else
1900 new_alts = 1;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001901 }
Sathish Narasimmanbaac6272020-04-03 21:43:59 +02001902
1903 if (btusb_switch_alt_setting(hdev, new_alts) < 0)
1904 bt_dev_err(hdev, "set USB alt:(%d) failed!", new_alts);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001905 } else {
1906 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1907 usb_kill_anchored_urbs(&data->isoc_anchor);
1908
1909 __set_isoc_interface(hdev, 0);
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -03001910 if (test_and_clear_bit(BTUSB_DID_ISO_RESUME, &data->flags))
Oliver Neukum8efdd0c2011-02-11 13:00:06 +01001911 usb_autopm_put_interface(data->isoc ? data->isoc : data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001912 }
1913}
1914
Oliver Neukum7bee5492009-08-24 23:44:59 +02001915static void btusb_waker(struct work_struct *work)
1916{
1917 struct btusb_data *data = container_of(work, struct btusb_data, waker);
1918 int err;
1919
1920 err = usb_autopm_get_interface(data->intf);
1921 if (err < 0)
1922 return;
1923
1924 usb_autopm_put_interface(data->intf);
1925}
1926
Luiz Augusto von Dentz800fe5e2021-12-03 14:38:13 -08001927static void btusb_rx_work(struct work_struct *work)
1928{
1929 struct btusb_data *data = container_of(work, struct btusb_data,
1930 rx_work.work);
1931 struct sk_buff *skb;
1932
1933 /* Dequeue ACL data received during the interval */
1934 while ((skb = skb_dequeue(&data->acl_q)))
1935 data->recv_acl(data->hdev, skb);
1936}
1937
Marcel Holtmann9f8f9622013-04-10 08:11:35 -07001938static int btusb_setup_bcm92035(struct hci_dev *hdev)
1939{
1940 struct sk_buff *skb;
1941 u8 val = 0x00;
1942
1943 BT_DBG("%s", hdev->name);
1944
1945 skb = __hci_cmd_sync(hdev, 0xfc3b, 1, &val, HCI_INIT_TIMEOUT);
1946 if (IS_ERR(skb))
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001947 bt_dev_err(hdev, "BCM92035 command failed (%ld)", PTR_ERR(skb));
Marcel Holtmann9f8f9622013-04-10 08:11:35 -07001948 else
1949 kfree_skb(skb);
1950
1951 return 0;
1952}
1953
Marcel Holtmann81cac642014-01-03 03:02:36 -08001954static int btusb_setup_csr(struct hci_dev *hdev)
1955{
Hans de Goeded74e0ae2020-12-05 16:02:00 +01001956 struct btusb_data *data = hci_get_drvdata(hdev);
1957 u16 bcdDevice = le16_to_cpu(data->udev->descriptor.bcdDevice);
Marcel Holtmann81cac642014-01-03 03:02:36 -08001958 struct hci_rp_read_local_version *rp;
1959 struct sk_buff *skb;
Ismael Ferreras Morezuelascde1a8a2020-07-26 23:12:28 +02001960 bool is_fake = false;
Hans de Goede0671c0662020-12-05 16:02:01 +01001961 int ret;
Marcel Holtmann81cac642014-01-03 03:02:36 -08001962
1963 BT_DBG("%s", hdev->name);
1964
Marcel Holtmann7cd84d72015-06-07 10:01:02 +02001965 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
1966 HCI_INIT_TIMEOUT);
1967 if (IS_ERR(skb)) {
1968 int err = PTR_ERR(skb);
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001969 bt_dev_err(hdev, "CSR: Local version failed (%d)", err);
Marcel Holtmann7cd84d72015-06-07 10:01:02 +02001970 return err;
1971 }
1972
1973 if (skb->len != sizeof(struct hci_rp_read_local_version)) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001974 bt_dev_err(hdev, "CSR: Local version length mismatch");
Marcel Holtmann7cd84d72015-06-07 10:01:02 +02001975 kfree_skb(skb);
1976 return -EIO;
1977 }
Marcel Holtmann81cac642014-01-03 03:02:36 -08001978
Marcel Holtmann89e75332014-09-16 04:44:50 +02001979 rp = (struct hci_rp_read_local_version *)skb->data;
Marcel Holtmann81cac642014-01-03 03:02:36 -08001980
Ismael Ferreras Morezuelascde1a8a2020-07-26 23:12:28 +02001981 /* Detect a wide host of Chinese controllers that aren't CSR.
1982 *
1983 * Known fake bcdDevices: 0x0100, 0x0134, 0x1915, 0x2520, 0x7558, 0x8891
1984 *
1985 * The main thing they have in common is that these are really popular low-cost
1986 * options that support newer Bluetooth versions but rely on heavy VID/PID
1987 * squatting of this poor old Bluetooth 1.1 device. Even sold as such.
1988 *
1989 * We detect actual CSR devices by checking that the HCI manufacturer code
1990 * is Cambridge Silicon Radio (10) and ensuring that LMP sub-version and
1991 * HCI rev values always match. As they both store the firmware number.
1992 */
Johan Hedberg6cafcd92015-08-30 21:47:21 +03001993 if (le16_to_cpu(rp->manufacturer) != 10 ||
Ismael Ferreras Morezuelascde1a8a2020-07-26 23:12:28 +02001994 le16_to_cpu(rp->hci_rev) != le16_to_cpu(rp->lmp_subver))
1995 is_fake = true;
1996
1997 /* Known legit CSR firmware build numbers and their supported BT versions:
1998 * - 1.1 (0x1) -> 0x0073, 0x020d, 0x033c, 0x034e
1999 * - 1.2 (0x2) -> 0x04d9, 0x0529
2000 * - 2.0 (0x3) -> 0x07a6, 0x07ad, 0x0c5c
2001 * - 2.1 (0x4) -> 0x149c, 0x1735, 0x1899 (0x1899 is a BlueCore4-External)
2002 * - 4.0 (0x6) -> 0x1d86, 0x2031, 0x22bb
2003 *
2004 * e.g. Real CSR dongles with LMP subversion 0x73 are old enough that
2005 * support BT 1.1 only; so it's a dead giveaway when some
2006 * third-party BT 4.0 dongle reuses it.
2007 */
2008 else if (le16_to_cpu(rp->lmp_subver) <= 0x034e &&
2009 le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_1_1)
2010 is_fake = true;
2011
2012 else if (le16_to_cpu(rp->lmp_subver) <= 0x0529 &&
2013 le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_1_2)
2014 is_fake = true;
2015
2016 else if (le16_to_cpu(rp->lmp_subver) <= 0x0c5c &&
2017 le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_2_0)
2018 is_fake = true;
2019
2020 else if (le16_to_cpu(rp->lmp_subver) <= 0x1899 &&
2021 le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_2_1)
2022 is_fake = true;
2023
2024 else if (le16_to_cpu(rp->lmp_subver) <= 0x22bb &&
2025 le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_4_0)
2026 is_fake = true;
2027
Hans de Goeded74e0ae2020-12-05 16:02:00 +01002028 /* Other clones which beat all the above checks */
2029 else if (bcdDevice == 0x0134 &&
2030 le16_to_cpu(rp->lmp_subver) == 0x0c5c &&
2031 le16_to_cpu(rp->hci_ver) == BLUETOOTH_VER_2_0)
2032 is_fake = true;
2033
Ismael Ferreras Morezuelascde1a8a2020-07-26 23:12:28 +02002034 if (is_fake) {
Ismael Ferreras Morezuelasf4292e22021-07-17 01:21:43 +02002035 bt_dev_warn(hdev, "CSR: Unbranded CSR clone detected; adding workarounds and force-suspending once...");
Ismael Ferreras Morezuelascde1a8a2020-07-26 23:12:28 +02002036
2037 /* Generally these clones have big discrepancies between
2038 * advertised features and what's actually supported.
2039 * Probably will need to be expanded in the future;
2040 * without these the controller will lock up.
2041 */
2042 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
2043 set_bit(HCI_QUIRK_BROKEN_ERR_DATA_REPORTING, &hdev->quirks);
2044
Marcel Holtmann9641d342015-06-07 10:01:01 +02002045 /* Clear the reset quirk since this is not an actual
2046 * early Bluetooth 1.1 device from CSR.
2047 */
2048 clear_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Ismael Ferreras Morezuelascde1a8a2020-07-26 23:12:28 +02002049 clear_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
Hans de Goede0671c0662020-12-05 16:02:01 +01002050
2051 /*
Ismael Ferreras Morezuelasf4292e22021-07-17 01:21:43 +02002052 * Special workaround for these BT 4.0 chip clones, and potentially more:
2053 *
2054 * - 0x0134: a Barrot 8041a02 (HCI rev: 0x1012 sub: 0x0810)
2055 * - 0x7558: IC markings FR3191AHAL 749H15143 (HCI rev/sub-version: 0x0709)
2056 *
2057 * These controllers are really messed-up.
2058 *
2059 * 1. Their bulk RX endpoint will never report any data unless
2060 * the device was suspended at least once (yes, really).
Hans de Goede0671c0662020-12-05 16:02:01 +01002061 * 2. They will not wakeup when autosuspended and receiving data
Ismael Ferreras Morezuelasf4292e22021-07-17 01:21:43 +02002062 * on their bulk RX endpoint from e.g. a keyboard or mouse
Hans de Goede0671c0662020-12-05 16:02:01 +01002063 * (IOW remote-wakeup support is broken for the bulk endpoint).
2064 *
2065 * To fix 1. enable runtime-suspend, force-suspend the
Ismael Ferreras Morezuelasf4292e22021-07-17 01:21:43 +02002066 * HCI and then wake-it up by disabling runtime-suspend.
Hans de Goede0671c0662020-12-05 16:02:01 +01002067 *
Ismael Ferreras Morezuelasf4292e22021-07-17 01:21:43 +02002068 * To fix 2. clear the HCI's can_wake flag, this way the HCI
Hans de Goede0671c0662020-12-05 16:02:01 +01002069 * will still be autosuspended when it is not open.
Ismael Ferreras Morezuelasf4292e22021-07-17 01:21:43 +02002070 *
2071 * --
2072 *
2073 * Because these are widespread problems we prefer generic solutions; so
2074 * apply this initialization quirk to every controller that gets here,
2075 * it should be harmless. The alternative is to not work at all.
Hans de Goede0671c0662020-12-05 16:02:01 +01002076 */
Ismael Ferreras Morezuelasf4292e22021-07-17 01:21:43 +02002077 pm_runtime_allow(&data->udev->dev);
Hans de Goede0671c0662020-12-05 16:02:01 +01002078
Ismael Ferreras Morezuelasf4292e22021-07-17 01:21:43 +02002079 ret = pm_runtime_suspend(&data->udev->dev);
2080 if (ret >= 0)
2081 msleep(200);
2082 else
2083 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 +01002084
Ismael Ferreras Morezuelasf4292e22021-07-17 01:21:43 +02002085 pm_runtime_forbid(&data->udev->dev);
Hans de Goede0671c0662020-12-05 16:02:01 +01002086
Ismael Ferreras Morezuelasf4292e22021-07-17 01:21:43 +02002087 device_set_wakeup_capable(&data->udev->dev, false);
Hans de Goede0671c0662020-12-05 16:02:01 +01002088
Ismael Ferreras Morezuelasf4292e22021-07-17 01:21:43 +02002089 /* Re-enable autosuspend if this was requested */
2090 if (enable_autosuspend)
2091 usb_enable_autosuspend(data->udev);
Marcel Holtmann81cac642014-01-03 03:02:36 -08002092 }
2093
Marcel Holtmann81cac642014-01-03 03:02:36 -08002094 kfree_skb(skb);
2095
Marcel Holtmann9641d342015-06-07 10:01:01 +02002096 return 0;
Marcel Holtmann81cac642014-01-03 03:02:36 -08002097}
2098
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002099static int inject_cmd_complete(struct hci_dev *hdev, __u16 opcode)
2100{
2101 struct sk_buff *skb;
2102 struct hci_event_hdr *hdr;
2103 struct hci_ev_cmd_complete *evt;
2104
Jia-Ju Baicf07e342018-07-23 11:38:51 +08002105 skb = bt_skb_alloc(sizeof(*hdr) + sizeof(*evt) + 1, GFP_KERNEL);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002106 if (!skb)
2107 return -ENOMEM;
2108
Johannes Berg4df864c2017-06-16 14:29:21 +02002109 hdr = skb_put(skb, sizeof(*hdr));
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002110 hdr->evt = HCI_EV_CMD_COMPLETE;
2111 hdr->plen = sizeof(*evt) + 1;
2112
Johannes Berg4df864c2017-06-16 14:29:21 +02002113 evt = skb_put(skb, sizeof(*evt));
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002114 evt->ncmd = 0x01;
2115 evt->opcode = cpu_to_le16(opcode);
2116
Johannes Berg634fef62017-06-16 14:29:24 +02002117 skb_put_u8(skb, 0x00);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002118
Marcel Holtmann618e8bc2015-11-05 07:33:56 +01002119 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002120
2121 return hci_recv_frame(hdev, skb);
2122}
2123
2124static int btusb_recv_bulk_intel(struct btusb_data *data, void *buffer,
2125 int count)
2126{
Tedd Ho-Jeong An019a1ca2021-08-04 17:32:15 -07002127 struct hci_dev *hdev = data->hdev;
2128
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002129 /* When the device is in bootloader mode, then it can send
2130 * events via the bulk endpoint. These events are treated the
2131 * same way as the ones received from the interrupt endpoint.
2132 */
Tedd Ho-Jeong An019a1ca2021-08-04 17:32:15 -07002133 if (btintel_test_flag(hdev, INTEL_BOOTLOADER))
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002134 return btusb_recv_intr(data, buffer, count);
2135
2136 return btusb_recv_bulk(data, buffer, count);
2137}
2138
2139static int btusb_recv_event_intel(struct hci_dev *hdev, struct sk_buff *skb)
2140{
Tedd Ho-Jeong An019a1ca2021-08-04 17:32:15 -07002141 if (btintel_test_flag(hdev, INTEL_BOOTLOADER)) {
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002142 struct hci_event_hdr *hdr = (void *)skb->data;
2143
Marcel Holtmannccd6da22015-04-09 00:35:19 -07002144 if (skb->len > HCI_EVENT_HDR_SIZE && hdr->evt == 0xff &&
2145 hdr->plen > 0) {
2146 const void *ptr = skb->data + HCI_EVENT_HDR_SIZE + 1;
2147 unsigned int len = skb->len - HCI_EVENT_HDR_SIZE - 1;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002148
Marcel Holtmannccd6da22015-04-09 00:35:19 -07002149 switch (skb->data[2]) {
2150 case 0x02:
2151 /* When switching to the operational firmware
2152 * the device sends a vendor specific event
2153 * indicating that the bootup completed.
2154 */
Tedd Ho-Jeong An019a1ca2021-08-04 17:32:15 -07002155 btintel_bootup(hdev, ptr, len);
Marcel Holtmannccd6da22015-04-09 00:35:19 -07002156 break;
2157 case 0x06:
2158 /* When the firmware loading completes the
2159 * device sends out a vendor specific event
2160 * indicating the result of the firmware
2161 * loading.
2162 */
Tedd Ho-Jeong An019a1ca2021-08-04 17:32:15 -07002163 btintel_secure_send_result(hdev, ptr, len);
Marcel Holtmannccd6da22015-04-09 00:35:19 -07002164 break;
Johan Hedbergfad70972015-01-30 10:58:55 +02002165 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002166 }
2167 }
2168
2169 return hci_recv_frame(hdev, skb);
2170}
2171
2172static int btusb_send_frame_intel(struct hci_dev *hdev, struct sk_buff *skb)
2173{
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002174 struct urb *urb;
2175
2176 BT_DBG("%s", hdev->name);
2177
Marcel Holtmann618e8bc2015-11-05 07:33:56 +01002178 switch (hci_skb_pkt_type(skb)) {
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002179 case HCI_COMMAND_PKT:
Tedd Ho-Jeong An019a1ca2021-08-04 17:32:15 -07002180 if (btintel_test_flag(hdev, INTEL_BOOTLOADER)) {
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002181 struct hci_command_hdr *cmd = (void *)skb->data;
2182 __u16 opcode = le16_to_cpu(cmd->opcode);
2183
2184 /* When in bootloader mode and the command 0xfc09
2185 * is received, it needs to be send down the
2186 * bulk endpoint. So allocate a bulk URB instead.
2187 */
2188 if (opcode == 0xfc09)
2189 urb = alloc_bulk_urb(hdev, skb);
2190 else
2191 urb = alloc_ctrl_urb(hdev, skb);
2192
2193 /* When the 0xfc01 command is issued to boot into
2194 * the operational firmware, it will actually not
2195 * send a command complete event. To keep the flow
2196 * control working inject that event here.
2197 */
2198 if (opcode == 0xfc01)
2199 inject_cmd_complete(hdev, opcode);
2200 } else {
2201 urb = alloc_ctrl_urb(hdev, skb);
2202 }
2203 if (IS_ERR(urb))
2204 return PTR_ERR(urb);
2205
2206 hdev->stat.cmd_tx++;
2207 return submit_or_queue_tx_urb(hdev, urb);
2208
2209 case HCI_ACLDATA_PKT:
2210 urb = alloc_bulk_urb(hdev, skb);
2211 if (IS_ERR(urb))
2212 return PTR_ERR(urb);
2213
2214 hdev->stat.acl_tx++;
2215 return submit_or_queue_tx_urb(hdev, urb);
2216
2217 case HCI_SCODATA_PKT:
2218 if (hci_conn_num(hdev, SCO_LINK) < 1)
2219 return -ENODEV;
2220
2221 urb = alloc_isoc_urb(hdev, skb);
2222 if (IS_ERR(urb))
2223 return PTR_ERR(urb);
2224
2225 hdev->stat.sco_tx++;
2226 return submit_tx_urb(hdev, urb);
2227 }
2228
2229 return -EILSEQ;
2230}
2231
mark-yw.chene9478022021-08-04 17:06:47 +08002232/* UHW CR mapping */
2233#define MTK_BT_MISC 0x70002510
2234#define MTK_BT_SUBSYS_RST 0x70002610
2235#define MTK_UDMA_INT_STA_BT 0x74000024
2236#define MTK_UDMA_INT_STA_BT1 0x74000308
2237#define MTK_BT_WDT_STATUS 0x740003A0
2238#define MTK_EP_RST_OPT 0x74011890
2239#define MTK_EP_RST_IN_OUT_OPT 0x00010001
2240#define MTK_BT_RST_DONE 0x00000100
2241#define MTK_BT_RESET_WAIT_MS 100
2242#define MTK_BT_RESET_NUM_TRIES 10
mark-yw.chen5a876792021-09-01 11:32:25 +08002243
Sean Wanga1c49c432019-06-02 08:02:48 +08002244static void btusb_mtk_wmt_recv(struct urb *urb)
2245{
2246 struct hci_dev *hdev = urb->context;
2247 struct btusb_data *data = hci_get_drvdata(hdev);
2248 struct hci_event_hdr *hdr;
2249 struct sk_buff *skb;
2250 int err;
2251
2252 if (urb->status == 0 && urb->actual_length > 0) {
2253 hdev->stat.byte_rx += urb->actual_length;
2254
2255 /* WMT event shouldn't be fragmented and the size should be
2256 * less than HCI_WMT_MAX_EVENT_SIZE.
2257 */
2258 skb = bt_skb_alloc(HCI_WMT_MAX_EVENT_SIZE, GFP_ATOMIC);
2259 if (!skb) {
2260 hdev->stat.err_rx++;
Mark-YW.Chen60c6a632021-10-14 00:22:04 +08002261 kfree(urb->setup_packet);
Jupeng Zhongde71a6c2021-02-02 09:39:13 +08002262 return;
Sean Wanga1c49c432019-06-02 08:02:48 +08002263 }
2264
2265 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
2266 skb_put_data(skb, urb->transfer_buffer, urb->actual_length);
2267
2268 hdr = (void *)skb->data;
2269 /* Fix up the vendor event id with 0xff for vendor specific
2270 * instead of 0xe4 so that event send via monitoring socket can
2271 * be parsed properly.
2272 */
2273 hdr->evt = 0xff;
2274
2275 /* When someone waits for the WMT event, the skb is being cloned
2276 * and being processed the events from there then.
2277 */
2278 if (test_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags)) {
Johan Hovold22cc6b72019-11-28 19:24:27 +01002279 data->evt_skb = skb_clone(skb, GFP_ATOMIC);
Jupeng Zhongde71a6c2021-02-02 09:39:13 +08002280 if (!data->evt_skb) {
2281 kfree_skb(skb);
Mark-YW.Chen60c6a632021-10-14 00:22:04 +08002282 kfree(urb->setup_packet);
Jupeng Zhongde71a6c2021-02-02 09:39:13 +08002283 return;
2284 }
Sean Wanga1c49c432019-06-02 08:02:48 +08002285 }
2286
2287 err = hci_recv_frame(hdev, skb);
Jupeng Zhongde71a6c2021-02-02 09:39:13 +08002288 if (err < 0) {
2289 kfree_skb(data->evt_skb);
2290 data->evt_skb = NULL;
Mark-YW.Chen60c6a632021-10-14 00:22:04 +08002291 kfree(urb->setup_packet);
Jupeng Zhongde71a6c2021-02-02 09:39:13 +08002292 return;
2293 }
Sean Wanga1c49c432019-06-02 08:02:48 +08002294
2295 if (test_and_clear_bit(BTUSB_TX_WAIT_VND_EVT,
2296 &data->flags)) {
2297 /* Barrier to sync with other CPUs */
2298 smp_mb__after_atomic();
2299 wake_up_bit(&data->flags,
2300 BTUSB_TX_WAIT_VND_EVT);
2301 }
Mark-YW.Chen60c6a632021-10-14 00:22:04 +08002302 kfree(urb->setup_packet);
Sean Wanga1c49c432019-06-02 08:02:48 +08002303 return;
2304 } else if (urb->status == -ENOENT) {
2305 /* Avoid suspend failed when usb_kill_urb */
2306 return;
2307 }
2308
2309 usb_mark_last_busy(data->udev);
2310
2311 /* The URB complete handler is still called with urb->actual_length = 0
2312 * when the event is not available, so we should keep re-submitting
2313 * URB until WMT event returns, Also, It's necessary to wait some time
2314 * between the two consecutive control URBs to relax the target device
2315 * to generate the event. Otherwise, the WMT event cannot return from
2316 * the device successfully.
2317 */
Mark Chen48c13302021-02-02 18:26:17 +08002318 udelay(500);
Sean Wanga1c49c432019-06-02 08:02:48 +08002319
2320 usb_anchor_urb(urb, &data->ctrl_anchor);
2321 err = usb_submit_urb(urb, GFP_ATOMIC);
2322 if (err < 0) {
Mark-YW.Chen60c6a632021-10-14 00:22:04 +08002323 kfree(urb->setup_packet);
Sean Wanga1c49c432019-06-02 08:02:48 +08002324 /* -EPERM: urb is being killed;
2325 * -ENODEV: device got disconnected
2326 */
2327 if (err != -EPERM && err != -ENODEV)
2328 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
2329 urb, -err);
2330 usb_unanchor_urb(urb);
2331 }
2332}
2333
2334static int btusb_mtk_submit_wmt_recv_urb(struct hci_dev *hdev)
2335{
2336 struct btusb_data *data = hci_get_drvdata(hdev);
2337 struct usb_ctrlrequest *dr;
2338 unsigned char *buf;
2339 int err, size = 64;
2340 unsigned int pipe;
2341 struct urb *urb;
2342
2343 urb = usb_alloc_urb(0, GFP_KERNEL);
2344 if (!urb)
2345 return -ENOMEM;
2346
2347 dr = kmalloc(sizeof(*dr), GFP_KERNEL);
2348 if (!dr) {
2349 usb_free_urb(urb);
2350 return -ENOMEM;
2351 }
2352
2353 dr->bRequestType = USB_TYPE_VENDOR | USB_DIR_IN;
2354 dr->bRequest = 1;
2355 dr->wIndex = cpu_to_le16(0);
2356 dr->wValue = cpu_to_le16(48);
2357 dr->wLength = cpu_to_le16(size);
2358
2359 buf = kmalloc(size, GFP_KERNEL);
2360 if (!buf) {
2361 kfree(dr);
Dinghao Liud33fe772020-08-23 15:44:21 +08002362 usb_free_urb(urb);
Sean Wanga1c49c432019-06-02 08:02:48 +08002363 return -ENOMEM;
2364 }
2365
2366 pipe = usb_rcvctrlpipe(data->udev, 0);
2367
2368 usb_fill_control_urb(urb, data->udev, pipe, (void *)dr,
2369 buf, size, btusb_mtk_wmt_recv, hdev);
2370
2371 urb->transfer_flags |= URB_FREE_BUFFER;
2372
2373 usb_anchor_urb(urb, &data->ctrl_anchor);
2374 err = usb_submit_urb(urb, GFP_KERNEL);
2375 if (err < 0) {
2376 if (err != -EPERM && err != -ENODEV)
2377 bt_dev_err(hdev, "urb %p submission failed (%d)",
2378 urb, -err);
2379 usb_unanchor_urb(urb);
2380 }
2381
2382 usb_free_urb(urb);
2383
2384 return err;
2385}
2386
2387static int btusb_mtk_hci_wmt_sync(struct hci_dev *hdev,
2388 struct btmtk_hci_wmt_params *wmt_params)
2389{
2390 struct btusb_data *data = hci_get_drvdata(hdev);
2391 struct btmtk_hci_wmt_evt_funcc *wmt_evt_funcc;
2392 u32 hlen, status = BTMTK_WMT_INVALID;
2393 struct btmtk_hci_wmt_evt *wmt_evt;
Arnd Bergmann10888142021-02-04 16:47:07 +01002394 struct btmtk_hci_wmt_cmd *wc;
Sean Wanga1c49c432019-06-02 08:02:48 +08002395 struct btmtk_wmt_hdr *hdr;
2396 int err;
2397
Sean Wanga1c49c432019-06-02 08:02:48 +08002398 /* Send the WMT command and wait until the WMT event returns */
2399 hlen = sizeof(*hdr) + wmt_params->dlen;
2400 if (hlen > 255)
2401 return -EINVAL;
2402
Arnd Bergmann10888142021-02-04 16:47:07 +01002403 wc = kzalloc(hlen, GFP_KERNEL);
2404 if (!wc)
2405 return -ENOMEM;
2406
2407 hdr = &wc->hdr;
Sean Wanga1c49c432019-06-02 08:02:48 +08002408 hdr->dir = 1;
2409 hdr->op = wmt_params->op;
2410 hdr->dlen = cpu_to_le16(wmt_params->dlen + 1);
2411 hdr->flag = wmt_params->flag;
Arnd Bergmann10888142021-02-04 16:47:07 +01002412 memcpy(wc->data, wmt_params->data, wmt_params->dlen);
Sean Wanga1c49c432019-06-02 08:02:48 +08002413
2414 set_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
2415
Arnd Bergmann10888142021-02-04 16:47:07 +01002416 err = __hci_cmd_send(hdev, 0xfc6f, hlen, wc);
Sean Wanga1c49c432019-06-02 08:02:48 +08002417
2418 if (err < 0) {
2419 clear_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
Arnd Bergmann10888142021-02-04 16:47:07 +01002420 goto err_free_wc;
Sean Wanga1c49c432019-06-02 08:02:48 +08002421 }
2422
mark-yw.chen8454ed92021-04-12 23:06:26 +08002423 /* Submit control IN URB on demand to process the WMT event */
2424 err = btusb_mtk_submit_wmt_recv_urb(hdev);
2425 if (err < 0)
Muhammad Usama Anjum44e936d2021-04-13 22:52:08 +05002426 goto err_free_wc;
mark-yw.chen8454ed92021-04-12 23:06:26 +08002427
Sean Wanga1c49c432019-06-02 08:02:48 +08002428 /* The vendor specific WMT commands are all answered by a vendor
2429 * specific event and will have the Command Status or Command
2430 * Complete as with usual HCI command flow control.
2431 *
2432 * After sending the command, wait for BTUSB_TX_WAIT_VND_EVT
2433 * state to be cleared. The driver specific event receive routine
2434 * will clear that state and with that indicate completion of the
2435 * WMT command.
2436 */
2437 err = wait_on_bit_timeout(&data->flags, BTUSB_TX_WAIT_VND_EVT,
2438 TASK_INTERRUPTIBLE, HCI_INIT_TIMEOUT);
2439 if (err == -EINTR) {
2440 bt_dev_err(hdev, "Execution of wmt command interrupted");
2441 clear_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
Arnd Bergmann10888142021-02-04 16:47:07 +01002442 goto err_free_wc;
Sean Wanga1c49c432019-06-02 08:02:48 +08002443 }
2444
2445 if (err) {
2446 bt_dev_err(hdev, "Execution of wmt command timed out");
2447 clear_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
Arnd Bergmann10888142021-02-04 16:47:07 +01002448 err = -ETIMEDOUT;
2449 goto err_free_wc;
Sean Wanga1c49c432019-06-02 08:02:48 +08002450 }
2451
2452 /* Parse and handle the return WMT event */
2453 wmt_evt = (struct btmtk_hci_wmt_evt *)data->evt_skb->data;
2454 if (wmt_evt->whdr.op != hdr->op) {
2455 bt_dev_err(hdev, "Wrong op received %d expected %d",
2456 wmt_evt->whdr.op, hdr->op);
2457 err = -EIO;
2458 goto err_free_skb;
2459 }
2460
2461 switch (wmt_evt->whdr.op) {
2462 case BTMTK_WMT_SEMAPHORE:
2463 if (wmt_evt->whdr.flag == 2)
2464 status = BTMTK_WMT_PATCH_UNDONE;
2465 else
2466 status = BTMTK_WMT_PATCH_DONE;
2467 break;
2468 case BTMTK_WMT_FUNC_CTRL:
2469 wmt_evt_funcc = (struct btmtk_hci_wmt_evt_funcc *)wmt_evt;
2470 if (be16_to_cpu(wmt_evt_funcc->status) == 0x404)
2471 status = BTMTK_WMT_ON_DONE;
2472 else if (be16_to_cpu(wmt_evt_funcc->status) == 0x420)
2473 status = BTMTK_WMT_ON_PROGRESS;
2474 else
2475 status = BTMTK_WMT_ON_UNDONE;
2476 break;
Mark Chenfc342c42021-02-02 18:26:18 +08002477 case BTMTK_WMT_PATCH_DWNLD:
2478 if (wmt_evt->whdr.flag == 2)
2479 status = BTMTK_WMT_PATCH_DONE;
2480 else if (wmt_evt->whdr.flag == 1)
2481 status = BTMTK_WMT_PATCH_PROGRESS;
2482 else
2483 status = BTMTK_WMT_PATCH_UNDONE;
2484 break;
Sean Wanga1c49c432019-06-02 08:02:48 +08002485 }
2486
2487 if (wmt_params->status)
2488 *wmt_params->status = status;
2489
2490err_free_skb:
2491 kfree_skb(data->evt_skb);
2492 data->evt_skb = NULL;
Arnd Bergmann10888142021-02-04 16:47:07 +01002493err_free_wc:
2494 kfree(wc);
Sean Wanga1c49c432019-06-02 08:02:48 +08002495 return err;
2496}
2497
Sean Wanga1c49c432019-06-02 08:02:48 +08002498static int btusb_mtk_func_query(struct hci_dev *hdev)
2499{
2500 struct btmtk_hci_wmt_params wmt_params;
2501 int status, err;
2502 u8 param = 0;
2503
2504 /* Query whether the function is enabled */
2505 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
2506 wmt_params.flag = 4;
2507 wmt_params.dlen = sizeof(param);
2508 wmt_params.data = &param;
2509 wmt_params.status = &status;
2510
2511 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
2512 if (err < 0) {
2513 bt_dev_err(hdev, "Failed to query function status (%d)", err);
2514 return err;
2515 }
2516
2517 return status;
2518}
2519
mark-yw.chene9478022021-08-04 17:06:47 +08002520static int btusb_mtk_uhw_reg_write(struct btusb_data *data, u32 reg, u32 val)
2521{
2522 struct hci_dev *hdev = data->hdev;
2523 int pipe, err;
2524 void *buf;
2525
2526 buf = kzalloc(4, GFP_KERNEL);
2527 if (!buf)
2528 return -ENOMEM;
2529
2530 put_unaligned_le32(val, buf);
2531
2532 pipe = usb_sndctrlpipe(data->udev, 0);
2533 err = usb_control_msg(data->udev, pipe, 0x02,
2534 0x5E,
2535 reg >> 16, reg & 0xffff,
2536 buf, 4, USB_CTRL_SET_TIMEOUT);
2537 if (err < 0) {
2538 bt_dev_err(hdev, "Failed to write uhw reg(%d)", err);
2539 goto err_free_buf;
2540 }
2541
2542err_free_buf:
2543 kfree(buf);
2544
2545 return err;
2546}
2547
2548static int btusb_mtk_uhw_reg_read(struct btusb_data *data, u32 reg, u32 *val)
2549{
2550 struct hci_dev *hdev = data->hdev;
2551 int pipe, err;
2552 void *buf;
2553
2554 buf = kzalloc(4, GFP_KERNEL);
2555 if (!buf)
2556 return -ENOMEM;
2557
2558 pipe = usb_rcvctrlpipe(data->udev, 0);
2559 err = usb_control_msg(data->udev, pipe, 0x01,
2560 0xDE,
2561 reg >> 16, reg & 0xffff,
2562 buf, 4, USB_CTRL_SET_TIMEOUT);
2563 if (err < 0) {
2564 bt_dev_err(hdev, "Failed to read uhw reg(%d)", err);
2565 goto err_free_buf;
2566 }
2567
2568 *val = get_unaligned_le32(buf);
2569 bt_dev_dbg(hdev, "reg=%x, value=0x%08x", reg, *val);
2570
2571err_free_buf:
2572 kfree(buf);
2573
2574 return err;
2575}
2576
Sean Wanga1c49c432019-06-02 08:02:48 +08002577static int btusb_mtk_reg_read(struct btusb_data *data, u32 reg, u32 *val)
2578{
2579 int pipe, err, size = sizeof(u32);
2580 void *buf;
2581
2582 buf = kzalloc(size, GFP_KERNEL);
2583 if (!buf)
2584 return -ENOMEM;
2585
2586 pipe = usb_rcvctrlpipe(data->udev, 0);
2587 err = usb_control_msg(data->udev, pipe, 0x63,
2588 USB_TYPE_VENDOR | USB_DIR_IN,
2589 reg >> 16, reg & 0xffff,
2590 buf, size, USB_CTRL_SET_TIMEOUT);
2591 if (err < 0)
2592 goto err_free_buf;
2593
2594 *val = get_unaligned_le32(buf);
2595
2596err_free_buf:
2597 kfree(buf);
2598
2599 return err;
2600}
2601
Mark Chen48c13302021-02-02 18:26:17 +08002602static int btusb_mtk_id_get(struct btusb_data *data, u32 reg, u32 *id)
Sean Wanga1c49c432019-06-02 08:02:48 +08002603{
Mark Chen48c13302021-02-02 18:26:17 +08002604 return btusb_mtk_reg_read(data, reg, id);
Sean Wanga1c49c432019-06-02 08:02:48 +08002605}
2606
2607static int btusb_mtk_setup(struct hci_dev *hdev)
2608{
2609 struct btusb_data *data = hci_get_drvdata(hdev);
2610 struct btmtk_hci_wmt_params wmt_params;
2611 ktime_t calltime, delta, rettime;
2612 struct btmtk_tci_sleep tci_sleep;
2613 unsigned long long duration;
2614 struct sk_buff *skb;
2615 const char *fwname;
2616 int err, status;
2617 u32 dev_id;
Mark Chenfc342c42021-02-02 18:26:18 +08002618 char fw_bin_name[64];
mark-yw.chen201cf392021-02-23 14:27:40 +08002619 u32 fw_version = 0;
Sean Wanga1c49c432019-06-02 08:02:48 +08002620 u8 param;
2621
2622 calltime = ktime_get();
2623
Mark Chen48c13302021-02-02 18:26:17 +08002624 err = btusb_mtk_id_get(data, 0x80000008, &dev_id);
Sean Wanga1c49c432019-06-02 08:02:48 +08002625 if (err < 0) {
2626 bt_dev_err(hdev, "Failed to get device id (%d)", err);
2627 return err;
2628 }
2629
Mark Chenfc342c42021-02-02 18:26:18 +08002630 if (!dev_id) {
2631 err = btusb_mtk_id_get(data, 0x70010200, &dev_id);
2632 if (err < 0) {
2633 bt_dev_err(hdev, "Failed to get device id (%d)", err);
2634 return err;
2635 }
2636 err = btusb_mtk_id_get(data, 0x80021004, &fw_version);
2637 if (err < 0) {
2638 bt_dev_err(hdev, "Failed to get fw version (%d)", err);
2639 return err;
2640 }
2641 }
2642
Sean Wanga1c49c432019-06-02 08:02:48 +08002643 switch (dev_id) {
Sean Wang9ce67c32019-06-02 08:02:49 +08002644 case 0x7663:
2645 fwname = FIRMWARE_MT7663;
2646 break;
Sean Wanga1c49c432019-06-02 08:02:48 +08002647 case 0x7668:
2648 fwname = FIRMWARE_MT7668;
2649 break;
mark-yw.chen09a19d62021-09-01 11:32:26 +08002650 case 0x7922:
Mark Chenfc342c42021-02-02 18:26:18 +08002651 case 0x7961:
2652 snprintf(fw_bin_name, sizeof(fw_bin_name),
2653 "mediatek/BT_RAM_CODE_MT%04x_1_%x_hdr.bin",
2654 dev_id & 0xffff, (fw_version & 0xff) + 1);
Sean Wang8c0d17b2021-10-19 05:30:12 +08002655 err = btmtk_setup_firmware_79xx(hdev, fw_bin_name,
2656 btusb_mtk_hci_wmt_sync);
Mark Chen00c0ee92021-12-07 01:33:42 +08002657 if (err < 0) {
2658 bt_dev_err(hdev, "Failed to set up firmware (%d)", err);
2659 return err;
2660 }
Mark Chenfc342c42021-02-02 18:26:18 +08002661
mark-yw.chene9478022021-08-04 17:06:47 +08002662 /* It's Device EndPoint Reset Option Register */
2663 btusb_mtk_uhw_reg_write(data, MTK_EP_RST_OPT, MTK_EP_RST_IN_OUT_OPT);
2664
Mark Chenfc342c42021-02-02 18:26:18 +08002665 /* Enable Bluetooth protocol */
2666 param = 1;
2667 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
2668 wmt_params.flag = 0;
2669 wmt_params.dlen = sizeof(param);
2670 wmt_params.data = &param;
2671 wmt_params.status = NULL;
2672
2673 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
2674 if (err < 0) {
2675 bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
2676 return err;
2677 }
mark-yw.chen654e6f72021-08-02 20:59:41 +08002678
2679 hci_set_msft_opcode(hdev, 0xFD30);
mark-yw.chen28491d72021-11-05 02:26:05 +08002680 hci_set_aosp_capable(hdev);
Mark Chenfc342c42021-02-02 18:26:18 +08002681 goto done;
Sean Wanga1c49c432019-06-02 08:02:48 +08002682 default:
Jupeng Zhonga297f562021-02-03 22:28:46 +08002683 bt_dev_err(hdev, "Unsupported hardware variant (%08x)",
Sean Wanga1c49c432019-06-02 08:02:48 +08002684 dev_id);
2685 return -ENODEV;
2686 }
2687
2688 /* Query whether the firmware is already download */
2689 wmt_params.op = BTMTK_WMT_SEMAPHORE;
2690 wmt_params.flag = 1;
2691 wmt_params.dlen = 0;
2692 wmt_params.data = NULL;
2693 wmt_params.status = &status;
2694
2695 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
2696 if (err < 0) {
2697 bt_dev_err(hdev, "Failed to query firmware status (%d)", err);
2698 return err;
2699 }
2700
2701 if (status == BTMTK_WMT_PATCH_DONE) {
2702 bt_dev_info(hdev, "firmware already downloaded");
2703 goto ignore_setup_fw;
2704 }
2705
2706 /* Setup a firmware which the device definitely requires */
Sean Wang8c0d17b2021-10-19 05:30:12 +08002707 err = btmtk_setup_firmware(hdev, fwname,
2708 btusb_mtk_hci_wmt_sync);
Sean Wanga1c49c432019-06-02 08:02:48 +08002709 if (err < 0)
2710 return err;
2711
2712ignore_setup_fw:
2713 err = readx_poll_timeout(btusb_mtk_func_query, hdev, status,
2714 status < 0 || status != BTMTK_WMT_ON_PROGRESS,
2715 2000, 5000000);
2716 /* -ETIMEDOUT happens */
2717 if (err < 0)
2718 return err;
2719
2720 /* The other errors happen in btusb_mtk_func_query */
2721 if (status < 0)
2722 return status;
2723
2724 if (status == BTMTK_WMT_ON_DONE) {
2725 bt_dev_info(hdev, "function already on");
2726 goto ignore_func_on;
2727 }
2728
2729 /* Enable Bluetooth protocol */
2730 param = 1;
2731 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
2732 wmt_params.flag = 0;
2733 wmt_params.dlen = sizeof(param);
2734 wmt_params.data = &param;
2735 wmt_params.status = NULL;
2736
2737 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
2738 if (err < 0) {
2739 bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
2740 return err;
2741 }
2742
2743ignore_func_on:
2744 /* Apply the low power environment setup */
2745 tci_sleep.mode = 0x5;
2746 tci_sleep.duration = cpu_to_le16(0x640);
2747 tci_sleep.host_duration = cpu_to_le16(0x640);
2748 tci_sleep.host_wakeup_pin = 0;
2749 tci_sleep.time_compensation = 0;
2750
2751 skb = __hci_cmd_sync(hdev, 0xfc7a, sizeof(tci_sleep), &tci_sleep,
2752 HCI_INIT_TIMEOUT);
2753 if (IS_ERR(skb)) {
2754 err = PTR_ERR(skb);
2755 bt_dev_err(hdev, "Failed to apply low power setting (%d)", err);
2756 return err;
2757 }
2758 kfree_skb(skb);
2759
Mark Chenfc342c42021-02-02 18:26:18 +08002760done:
Sean Wanga1c49c432019-06-02 08:02:48 +08002761 rettime = ktime_get();
2762 delta = ktime_sub(rettime, calltime);
2763 duration = (unsigned long long)ktime_to_ns(delta) >> 10;
2764
2765 bt_dev_info(hdev, "Device setup in %llu usecs", duration);
2766
2767 return 0;
2768}
2769
2770static int btusb_mtk_shutdown(struct hci_dev *hdev)
2771{
2772 struct btmtk_hci_wmt_params wmt_params;
2773 u8 param = 0;
2774 int err;
2775
2776 /* Disable the device */
2777 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
2778 wmt_params.flag = 0;
2779 wmt_params.dlen = sizeof(param);
2780 wmt_params.data = &param;
2781 wmt_params.status = NULL;
2782
2783 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
2784 if (err < 0) {
2785 bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
2786 return err;
2787 }
2788
2789 return 0;
2790}
2791
mark-yw.chene9478022021-08-04 17:06:47 +08002792static void btusb_mtk_cmd_timeout(struct hci_dev *hdev)
2793{
2794 struct btusb_data *data = hci_get_drvdata(hdev);
2795 u32 val;
2796 int err, retry = 0;
2797
2798 /* It's MediaTek specific bluetooth reset mechanism via USB */
2799 if (test_and_set_bit(BTUSB_HW_RESET_ACTIVE, &data->flags)) {
2800 bt_dev_err(hdev, "last reset failed? Not resetting again");
2801 return;
2802 }
2803
2804 err = usb_autopm_get_interface(data->intf);
2805 if (err < 0)
2806 return;
2807
2808 btusb_stop_traffic(data);
2809 usb_kill_anchored_urbs(&data->tx_anchor);
2810
2811 /* It's Device EndPoint Reset Option Register */
2812 bt_dev_dbg(hdev, "Initiating reset mechanism via uhw");
2813 btusb_mtk_uhw_reg_write(data, MTK_EP_RST_OPT, MTK_EP_RST_IN_OUT_OPT);
2814 btusb_mtk_uhw_reg_read(data, MTK_BT_WDT_STATUS, &val);
2815
2816 /* Reset the bluetooth chip via USB interface. */
2817 btusb_mtk_uhw_reg_write(data, MTK_BT_SUBSYS_RST, 1);
2818 btusb_mtk_uhw_reg_write(data, MTK_UDMA_INT_STA_BT, 0x000000FF);
2819 btusb_mtk_uhw_reg_read(data, MTK_UDMA_INT_STA_BT, &val);
2820 btusb_mtk_uhw_reg_write(data, MTK_UDMA_INT_STA_BT1, 0x000000FF);
2821 btusb_mtk_uhw_reg_read(data, MTK_UDMA_INT_STA_BT1, &val);
2822 /* MT7921 need to delay 20ms between toggle reset bit */
2823 msleep(20);
2824 btusb_mtk_uhw_reg_write(data, MTK_BT_SUBSYS_RST, 0);
2825 btusb_mtk_uhw_reg_read(data, MTK_BT_SUBSYS_RST, &val);
2826
2827 /* Poll the register until reset is completed */
2828 do {
2829 btusb_mtk_uhw_reg_read(data, MTK_BT_MISC, &val);
2830 if (val & MTK_BT_RST_DONE) {
2831 bt_dev_dbg(hdev, "Bluetooth Reset Successfully");
2832 break;
2833 }
2834
2835 bt_dev_dbg(hdev, "Polling Bluetooth Reset CR");
2836 retry++;
2837 msleep(MTK_BT_RESET_WAIT_MS);
2838 } while (retry < MTK_BT_RESET_NUM_TRIES);
2839
2840 btusb_mtk_id_get(data, 0x70010200, &val);
2841 if (!val)
2842 bt_dev_err(hdev, "Can't get device id, subsys reset fail.");
2843
2844 usb_queue_reset_device(data->intf);
2845
2846 clear_bit(BTUSB_HW_RESET_ACTIVE, &data->flags);
2847}
2848
mark-yw.chen0b10c8c2021-08-04 17:03:15 +08002849static int btusb_recv_acl_mtk(struct hci_dev *hdev, struct sk_buff *skb)
2850{
2851 struct btusb_data *data = hci_get_drvdata(hdev);
2852 u16 handle = le16_to_cpu(hci_acl_hdr(skb)->handle);
2853
2854 switch (handle) {
2855 case 0xfc6f: /* Firmware dump from device */
2856 /* When the firmware hangs, the device can no longer
2857 * suspend and thus disable auto-suspend.
2858 */
2859 usb_disable_autosuspend(data->udev);
mark-yw.chen146af222021-08-06 09:35:16 +08002860 fallthrough;
mark-yw.chen0b10c8c2021-08-04 17:03:15 +08002861 case 0x05ff: /* Firmware debug logging 1 */
2862 case 0x05fe: /* Firmware debug logging 2 */
2863 return hci_recv_diag(hdev, skb);
2864 }
2865
2866 return hci_recv_frame(hdev, skb);
2867}
2868
Rajat Jaina4ccc9e2017-02-01 14:24:10 -08002869#ifdef CONFIG_PM
2870/* Configure an out-of-band gpio as wake-up pin, if specified in device tree */
2871static int marvell_config_oob_wake(struct hci_dev *hdev)
2872{
2873 struct sk_buff *skb;
2874 struct btusb_data *data = hci_get_drvdata(hdev);
2875 struct device *dev = &data->udev->dev;
2876 u16 pin, gap, opcode;
2877 int ret;
2878 u8 cmd[5];
2879
2880 /* Move on if no wakeup pin specified */
2881 if (of_property_read_u16(dev->of_node, "marvell,wakeup-pin", &pin) ||
2882 of_property_read_u16(dev->of_node, "marvell,wakeup-gap-ms", &gap))
2883 return 0;
2884
2885 /* Vendor specific command to configure a GPIO as wake-up pin */
2886 opcode = hci_opcode_pack(0x3F, 0x59);
2887 cmd[0] = opcode & 0xFF;
2888 cmd[1] = opcode >> 8;
2889 cmd[2] = 2; /* length of parameters that follow */
2890 cmd[3] = pin;
2891 cmd[4] = gap; /* time in ms, for which wakeup pin should be asserted */
2892
2893 skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL);
2894 if (!skb) {
Joe Perchesd84fc2c2020-12-22 02:31:49 -08002895 bt_dev_err(hdev, "%s: No memory", __func__);
Rajat Jaina4ccc9e2017-02-01 14:24:10 -08002896 return -ENOMEM;
2897 }
2898
Johannes Berg59ae1d12017-06-16 14:29:20 +02002899 skb_put_data(skb, cmd, sizeof(cmd));
Rajat Jaina4ccc9e2017-02-01 14:24:10 -08002900 hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
2901
2902 ret = btusb_send_frame(hdev, skb);
2903 if (ret) {
Joe Perchesd84fc2c2020-12-22 02:31:49 -08002904 bt_dev_err(hdev, "%s: configuration failed", __func__);
Rajat Jaina4ccc9e2017-02-01 14:24:10 -08002905 kfree_skb(skb);
2906 return ret;
2907 }
2908
2909 return 0;
2910}
2911#endif
2912
Amitkumar Karwarae8df492014-07-18 14:47:06 -07002913static int btusb_set_bdaddr_marvell(struct hci_dev *hdev,
2914 const bdaddr_t *bdaddr)
2915{
2916 struct sk_buff *skb;
2917 u8 buf[8];
2918 long ret;
2919
2920 buf[0] = 0xfe;
2921 buf[1] = sizeof(bdaddr_t);
2922 memcpy(buf + 2, bdaddr, sizeof(bdaddr_t));
2923
2924 skb = __hci_cmd_sync(hdev, 0xfc22, sizeof(buf), buf, HCI_INIT_TIMEOUT);
2925 if (IS_ERR(skb)) {
2926 ret = PTR_ERR(skb);
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002927 bt_dev_err(hdev, "changing Marvell device address failed (%ld)",
2928 ret);
Amitkumar Karwarae8df492014-07-18 14:47:06 -07002929 return ret;
2930 }
2931 kfree_skb(skb);
2932
2933 return 0;
2934}
2935
Toshi Kikuchi58592232014-12-12 10:58:05 -08002936static int btusb_set_bdaddr_ath3012(struct hci_dev *hdev,
2937 const bdaddr_t *bdaddr)
2938{
2939 struct sk_buff *skb;
2940 u8 buf[10];
2941 long ret;
2942
2943 buf[0] = 0x01;
2944 buf[1] = 0x01;
2945 buf[2] = 0x00;
2946 buf[3] = sizeof(bdaddr_t);
2947 memcpy(buf + 4, bdaddr, sizeof(bdaddr_t));
2948
2949 skb = __hci_cmd_sync(hdev, 0xfc0b, sizeof(buf), buf, HCI_INIT_TIMEOUT);
2950 if (IS_ERR(skb)) {
2951 ret = PTR_ERR(skb);
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002952 bt_dev_err(hdev, "Change address command failed (%ld)", ret);
Toshi Kikuchi58592232014-12-12 10:58:05 -08002953 return ret;
2954 }
2955 kfree_skb(skb);
2956
2957 return 0;
2958}
2959
Rocky Liaob40f58b2020-09-29 12:23:51 +08002960static int btusb_set_bdaddr_wcn6855(struct hci_dev *hdev,
2961 const bdaddr_t *bdaddr)
2962{
2963 struct sk_buff *skb;
2964 u8 buf[6];
2965 long ret;
2966
2967 memcpy(buf, bdaddr, sizeof(bdaddr_t));
2968
2969 skb = __hci_cmd_sync_ev(hdev, 0xfc14, sizeof(buf), buf,
2970 HCI_EV_CMD_COMPLETE, HCI_INIT_TIMEOUT);
2971 if (IS_ERR(skb)) {
2972 ret = PTR_ERR(skb);
2973 bt_dev_err(hdev, "Change address command failed (%ld)", ret);
2974 return ret;
2975 }
2976 kfree_skb(skb);
2977
2978 return 0;
2979}
2980
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002981#define QCA_DFU_PACKET_LEN 4096
2982
2983#define QCA_GET_TARGET_VERSION 0x09
2984#define QCA_CHECK_STATUS 0x05
2985#define QCA_DFU_DOWNLOAD 0x01
2986
2987#define QCA_SYSCFG_UPDATED 0x40
2988#define QCA_PATCH_UPDATED 0x80
2989#define QCA_DFU_TIMEOUT 3000
Tim Jiangef2862a2020-12-03 12:57:14 +08002990#define QCA_FLAG_MULTI_NVM 0x80
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002991
Tim Jiangd6cba4e2021-10-29 11:21:21 +08002992#define WCN6855_2_0_RAM_VERSION_GF 0x400c1200
2993#define WCN6855_2_1_RAM_VERSION_GF 0x400c1211
2994
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002995struct qca_version {
2996 __le32 rom_version;
2997 __le32 patch_version;
2998 __le32 ram_version;
tjiang@codeaurora.org741268a2021-11-16 12:48:49 +08002999 __u8 chip_id;
3000 __u8 platform_id;
Tim Jiangef2862a2020-12-03 12:57:14 +08003001 __le16 flag;
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003002 __u8 reserved[4];
3003} __packed;
3004
3005struct qca_rampatch_version {
Rocky Liaob40f58b2020-09-29 12:23:51 +08003006 __le16 rom_version_high;
3007 __le16 rom_version_low;
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003008 __le16 patch_version;
3009} __packed;
3010
3011struct qca_device_info {
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00003012 u32 rom_version;
3013 u8 rampatch_hdr; /* length of header in rampatch */
3014 u8 nvm_hdr; /* length of header in NVM */
3015 u8 ver_offset; /* offset of version structure in rampatch */
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003016};
3017
3018static const struct qca_device_info qca_devices_table[] = {
Rocky Liaob40f58b2020-09-29 12:23:51 +08003019 { 0x00000100, 20, 4, 8 }, /* Rome 1.0 */
3020 { 0x00000101, 20, 4, 8 }, /* Rome 1.1 */
3021 { 0x00000200, 28, 4, 16 }, /* Rome 2.0 */
3022 { 0x00000201, 28, 4, 16 }, /* Rome 2.1 */
3023 { 0x00000300, 28, 4, 16 }, /* Rome 3.0 */
3024 { 0x00000302, 28, 4, 16 }, /* Rome 3.2 */
3025 { 0x00130100, 40, 4, 16 }, /* WCN6855 1.0 */
3026 { 0x00130200, 40, 4, 16 }, /* WCN6855 2.0 */
Tim Jiangd6cba4e2021-10-29 11:21:21 +08003027 { 0x00130201, 40, 4, 16 }, /* WCN6855 2.1 */
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003028};
3029
Takashi Iwai803cdb82018-05-21 22:34:52 +02003030static int btusb_qca_send_vendor_req(struct usb_device *udev, u8 request,
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003031 void *data, u16 size)
3032{
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003033 int pipe, err;
3034 u8 *buf;
3035
3036 buf = kmalloc(size, GFP_KERNEL);
3037 if (!buf)
3038 return -ENOMEM;
3039
3040 /* Found some of USB hosts have IOT issues with ours so that we should
3041 * not wait until HCI layer is ready.
3042 */
3043 pipe = usb_rcvctrlpipe(udev, 0);
3044 err = usb_control_msg(udev, pipe, request, USB_TYPE_VENDOR | USB_DIR_IN,
3045 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
3046 if (err < 0) {
Takashi Iwai803cdb82018-05-21 22:34:52 +02003047 dev_err(&udev->dev, "Failed to access otp area (%d)", err);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003048 goto done;
3049 }
3050
3051 memcpy(data, buf, size);
3052
3053done:
3054 kfree(buf);
3055
3056 return err;
3057}
3058
3059static int btusb_setup_qca_download_fw(struct hci_dev *hdev,
3060 const struct firmware *firmware,
3061 size_t hdr_size)
3062{
3063 struct btusb_data *btdata = hci_get_drvdata(hdev);
3064 struct usb_device *udev = btdata->udev;
3065 size_t count, size, sent = 0;
3066 int pipe, len, err;
3067 u8 *buf;
3068
3069 buf = kmalloc(QCA_DFU_PACKET_LEN, GFP_KERNEL);
3070 if (!buf)
3071 return -ENOMEM;
3072
3073 count = firmware->size;
3074
3075 size = min_t(size_t, count, hdr_size);
3076 memcpy(buf, firmware->data, size);
3077
3078 /* USB patches should go down to controller through USB path
3079 * because binary format fits to go down through USB channel.
3080 * USB control path is for patching headers and USB bulk is for
3081 * patch body.
3082 */
3083 pipe = usb_sndctrlpipe(udev, 0);
3084 err = usb_control_msg(udev, pipe, QCA_DFU_DOWNLOAD, USB_TYPE_VENDOR,
3085 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
3086 if (err < 0) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01003087 bt_dev_err(hdev, "Failed to send headers (%d)", err);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003088 goto done;
3089 }
3090
3091 sent += size;
3092 count -= size;
3093
Tim Jiang4f00bfb2021-06-01 17:57:10 +08003094 /* ep2 need time to switch from function acl to function dfu,
3095 * so we add 20ms delay here.
3096 */
3097 msleep(20);
3098
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003099 while (count) {
3100 size = min_t(size_t, count, QCA_DFU_PACKET_LEN);
3101
3102 memcpy(buf, firmware->data + sent, size);
3103
3104 pipe = usb_sndbulkpipe(udev, 0x02);
3105 err = usb_bulk_msg(udev, pipe, buf, size, &len,
3106 QCA_DFU_TIMEOUT);
3107 if (err < 0) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01003108 bt_dev_err(hdev, "Failed to send body at %zd of %zd (%d)",
3109 sent, firmware->size, err);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003110 break;
3111 }
3112
3113 if (size != len) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01003114 bt_dev_err(hdev, "Failed to get bulk buffer");
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003115 err = -EILSEQ;
3116 break;
3117 }
3118
3119 sent += size;
3120 count -= size;
3121 }
3122
3123done:
3124 kfree(buf);
3125 return err;
3126}
3127
3128static int btusb_setup_qca_load_rampatch(struct hci_dev *hdev,
3129 struct qca_version *ver,
3130 const struct qca_device_info *info)
3131{
3132 struct qca_rampatch_version *rver;
3133 const struct firmware *fw;
Rocky Liaob40f58b2020-09-29 12:23:51 +08003134 u32 ver_rom, ver_patch, rver_rom;
3135 u16 rver_rom_low, rver_rom_high, rver_patch;
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003136 char fwname[64];
3137 int err;
3138
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00003139 ver_rom = le32_to_cpu(ver->rom_version);
3140 ver_patch = le32_to_cpu(ver->patch_version);
3141
3142 snprintf(fwname, sizeof(fwname), "qca/rampatch_usb_%08x.bin", ver_rom);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003143
3144 err = request_firmware(&fw, fwname, &hdev->dev);
3145 if (err) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01003146 bt_dev_err(hdev, "failed to request rampatch file: %s (%d)",
3147 fwname, err);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003148 return err;
3149 }
3150
Marcel Holtmann2064ee32017-10-30 10:42:59 +01003151 bt_dev_info(hdev, "using rampatch file: %s", fwname);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003152
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00003153 rver = (struct qca_rampatch_version *)(fw->data + info->ver_offset);
Rocky Liaob40f58b2020-09-29 12:23:51 +08003154 rver_rom_low = le16_to_cpu(rver->rom_version_low);
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00003155 rver_patch = le16_to_cpu(rver->patch_version);
3156
Rocky Liaob40f58b2020-09-29 12:23:51 +08003157 if (ver_rom & ~0xffffU) {
3158 rver_rom_high = le16_to_cpu(rver->rom_version_high);
3159 rver_rom = le32_to_cpu(rver_rom_high << 16 | rver_rom_low);
3160 } else {
3161 rver_rom = rver_rom_low;
3162 }
3163
Marcel Holtmann2064ee32017-10-30 10:42:59 +01003164 bt_dev_info(hdev, "QCA: patch rome 0x%x build 0x%x, "
3165 "firmware rome 0x%x build 0x%x",
3166 rver_rom, rver_patch, ver_rom, ver_patch);
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00003167
3168 if (rver_rom != ver_rom || rver_patch <= ver_patch) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01003169 bt_dev_err(hdev, "rampatch file version did not match with firmware");
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003170 err = -EINVAL;
3171 goto done;
3172 }
3173
3174 err = btusb_setup_qca_download_fw(hdev, fw, info->rampatch_hdr);
3175
3176done:
3177 release_firmware(fw);
3178
3179 return err;
3180}
3181
Tim Jiangd6cba4e2021-10-29 11:21:21 +08003182static void btusb_generate_qca_nvm_name(char *fwname, size_t max_size,
3183 const struct qca_version *ver)
3184{
3185 u32 rom_version = le32_to_cpu(ver->rom_version);
3186 u16 flag = le16_to_cpu(ver->flag);
3187
3188 if (((flag >> 8) & 0xff) == QCA_FLAG_MULTI_NVM) {
tjiang@codeaurora.org741268a2021-11-16 12:48:49 +08003189 /* The board_id should be split into two bytes
3190 * The 1st byte is chip ID, and the 2nd byte is platform ID
3191 * For example, board ID 0x010A, 0x01 is platform ID. 0x0A is chip ID
tjiang@codeaurora.org21a241b2021-11-16 19:02:16 +08003192 * we have several platforms, and platform IDs are continuously added
tjiang@codeaurora.org741268a2021-11-16 12:48:49 +08003193 * Platform ID:
3194 * 0x00 is for Mobile
3195 * 0x01 is for X86
3196 * 0x02 is for Automotive
3197 * 0x03 is for Consumer electronic
3198 */
3199 u16 board_id = (ver->chip_id << 8) + ver->platform_id;
Tim Jiangd6cba4e2021-10-29 11:21:21 +08003200 const char *variant;
3201
3202 switch (le32_to_cpu(ver->ram_version)) {
3203 case WCN6855_2_0_RAM_VERSION_GF:
3204 case WCN6855_2_1_RAM_VERSION_GF:
3205 variant = "_gf";
3206 break;
3207 default:
3208 variant = "";
3209 break;
3210 }
3211
3212 if (board_id == 0) {
3213 snprintf(fwname, max_size, "qca/nvm_usb_%08x%s.bin",
3214 rom_version, variant);
3215 } else {
3216 snprintf(fwname, max_size, "qca/nvm_usb_%08x%s_%04x.bin",
3217 rom_version, variant, board_id);
3218 }
3219 } else {
3220 snprintf(fwname, max_size, "qca/nvm_usb_%08x.bin",
3221 rom_version);
3222 }
3223
3224}
3225
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003226static int btusb_setup_qca_load_nvm(struct hci_dev *hdev,
3227 struct qca_version *ver,
3228 const struct qca_device_info *info)
3229{
3230 const struct firmware *fw;
3231 char fwname[64];
3232 int err;
3233
Tim Jiangd6cba4e2021-10-29 11:21:21 +08003234 btusb_generate_qca_nvm_name(fwname, sizeof(fwname), ver);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003235
3236 err = request_firmware(&fw, fwname, &hdev->dev);
3237 if (err) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01003238 bt_dev_err(hdev, "failed to request NVM file: %s (%d)",
3239 fwname, err);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003240 return err;
3241 }
3242
Marcel Holtmann2064ee32017-10-30 10:42:59 +01003243 bt_dev_info(hdev, "using NVM file: %s", fwname);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003244
3245 err = btusb_setup_qca_download_fw(hdev, fw, info->nvm_hdr);
3246
3247 release_firmware(fw);
3248
3249 return err;
3250}
3251
Takashi Iwai803cdb82018-05-21 22:34:52 +02003252/* identify the ROM version and check whether patches are needed */
3253static bool btusb_qca_need_patch(struct usb_device *udev)
3254{
3255 struct qca_version ver;
3256
3257 if (btusb_qca_send_vendor_req(udev, QCA_GET_TARGET_VERSION, &ver,
3258 sizeof(ver)) < 0)
3259 return false;
3260 /* only low ROM versions need patches */
3261 return !(le32_to_cpu(ver.rom_version) & ~0xffffU);
3262}
3263
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003264static int btusb_setup_qca(struct hci_dev *hdev)
3265{
Takashi Iwai803cdb82018-05-21 22:34:52 +02003266 struct btusb_data *btdata = hci_get_drvdata(hdev);
3267 struct usb_device *udev = btdata->udev;
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003268 const struct qca_device_info *info = NULL;
3269 struct qca_version ver;
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00003270 u32 ver_rom;
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003271 u8 status;
3272 int i, err;
3273
Takashi Iwai803cdb82018-05-21 22:34:52 +02003274 err = btusb_qca_send_vendor_req(udev, QCA_GET_TARGET_VERSION, &ver,
Marcel Holtmanneb500422015-04-16 23:15:50 +02003275 sizeof(ver));
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003276 if (err < 0)
3277 return err;
3278
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00003279 ver_rom = le32_to_cpu(ver.rom_version);
Takashi Iwai803cdb82018-05-21 22:34:52 +02003280
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003281 for (i = 0; i < ARRAY_SIZE(qca_devices_table); i++) {
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00003282 if (ver_rom == qca_devices_table[i].rom_version)
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003283 info = &qca_devices_table[i];
3284 }
3285 if (!info) {
Hui Wang234f4142021-02-08 13:02:37 +08003286 /* If the rom_version is not matched in the qca_devices_table
3287 * and the high ROM version is not zero, we assume this chip no
3288 * need to load the rampatch and nvm.
3289 */
3290 if (ver_rom & ~0xffffU)
3291 return 0;
3292
Marcel Holtmann2064ee32017-10-30 10:42:59 +01003293 bt_dev_err(hdev, "don't support firmware rome 0x%x", ver_rom);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003294 return -ENODEV;
3295 }
3296
Takashi Iwai803cdb82018-05-21 22:34:52 +02003297 err = btusb_qca_send_vendor_req(udev, QCA_CHECK_STATUS, &status,
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003298 sizeof(status));
3299 if (err < 0)
3300 return err;
3301
3302 if (!(status & QCA_PATCH_UPDATED)) {
3303 err = btusb_setup_qca_load_rampatch(hdev, &ver, info);
3304 if (err < 0)
3305 return err;
3306 }
3307
Tim Jiangef2862a2020-12-03 12:57:14 +08003308 err = btusb_qca_send_vendor_req(udev, QCA_GET_TARGET_VERSION, &ver,
3309 sizeof(ver));
3310 if (err < 0)
3311 return err;
3312
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003313 if (!(status & QCA_SYSCFG_UPDATED)) {
3314 err = btusb_setup_qca_load_nvm(hdev, &ver, info);
3315 if (err < 0)
3316 return err;
3317 }
3318
3319 return 0;
3320}
3321
Marcel Holtmann9d08f502015-10-08 20:23:08 +02003322static inline int __set_diag_interface(struct hci_dev *hdev)
3323{
3324 struct btusb_data *data = hci_get_drvdata(hdev);
3325 struct usb_interface *intf = data->diag;
3326 int i;
3327
3328 if (!data->diag)
3329 return -ENODEV;
3330
3331 data->diag_tx_ep = NULL;
3332 data->diag_rx_ep = NULL;
3333
3334 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
3335 struct usb_endpoint_descriptor *ep_desc;
3336
3337 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
3338
3339 if (!data->diag_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
3340 data->diag_tx_ep = ep_desc;
3341 continue;
3342 }
3343
3344 if (!data->diag_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
3345 data->diag_rx_ep = ep_desc;
3346 continue;
3347 }
3348 }
3349
3350 if (!data->diag_tx_ep || !data->diag_rx_ep) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01003351 bt_dev_err(hdev, "invalid diagnostic descriptors");
Marcel Holtmann9d08f502015-10-08 20:23:08 +02003352 return -ENODEV;
3353 }
3354
3355 return 0;
3356}
3357
3358static struct urb *alloc_diag_urb(struct hci_dev *hdev, bool enable)
3359{
3360 struct btusb_data *data = hci_get_drvdata(hdev);
3361 struct sk_buff *skb;
3362 struct urb *urb;
3363 unsigned int pipe;
3364
3365 if (!data->diag_tx_ep)
3366 return ERR_PTR(-ENODEV);
3367
3368 urb = usb_alloc_urb(0, GFP_KERNEL);
3369 if (!urb)
3370 return ERR_PTR(-ENOMEM);
3371
3372 skb = bt_skb_alloc(2, GFP_KERNEL);
3373 if (!skb) {
3374 usb_free_urb(urb);
3375 return ERR_PTR(-ENOMEM);
3376 }
3377
Johannes Berg634fef62017-06-16 14:29:24 +02003378 skb_put_u8(skb, 0xf0);
3379 skb_put_u8(skb, enable);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02003380
3381 pipe = usb_sndbulkpipe(data->udev, data->diag_tx_ep->bEndpointAddress);
3382
3383 usb_fill_bulk_urb(urb, data->udev, pipe,
3384 skb->data, skb->len, btusb_tx_complete, skb);
3385
3386 skb->dev = (void *)hdev;
3387
3388 return urb;
3389}
3390
3391static int btusb_bcm_set_diag(struct hci_dev *hdev, bool enable)
3392{
3393 struct btusb_data *data = hci_get_drvdata(hdev);
3394 struct urb *urb;
3395
3396 if (!data->diag)
3397 return -ENODEV;
3398
3399 if (!test_bit(HCI_RUNNING, &hdev->flags))
3400 return -ENETDOWN;
3401
3402 urb = alloc_diag_urb(hdev, enable);
3403 if (IS_ERR(urb))
3404 return PTR_ERR(urb);
3405
3406 return submit_or_queue_tx_urb(hdev, urb);
3407}
Marcel Holtmann9d08f502015-10-08 20:23:08 +02003408
Rajat Jainfd913ef2017-02-01 14:24:09 -08003409#ifdef CONFIG_PM
3410static irqreturn_t btusb_oob_wake_handler(int irq, void *priv)
3411{
3412 struct btusb_data *data = priv;
3413
3414 pm_wakeup_event(&data->udev->dev, 0);
Jeffy Chen017789f2017-02-24 14:24:29 +08003415 pm_system_wakeup();
Rajat Jainfd913ef2017-02-01 14:24:09 -08003416
3417 /* Disable only if not already disabled (keep it balanced) */
3418 if (test_and_clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags)) {
3419 disable_irq_nosync(irq);
3420 disable_irq_wake(irq);
3421 }
3422 return IRQ_HANDLED;
3423}
3424
3425static const struct of_device_id btusb_match_table[] = {
3426 { .compatible = "usb1286,204e" },
Brian Norris4c409af2019-02-22 14:53:43 -08003427 { .compatible = "usbcf3,e300" }, /* QCA6174A */
3428 { .compatible = "usb4ca,301a" }, /* QCA6174A (Lite-On) */
Rajat Jainfd913ef2017-02-01 14:24:09 -08003429 { }
3430};
3431MODULE_DEVICE_TABLE(of, btusb_match_table);
3432
3433/* Use an oob wakeup pin? */
3434static int btusb_config_oob_wake(struct hci_dev *hdev)
3435{
3436 struct btusb_data *data = hci_get_drvdata(hdev);
3437 struct device *dev = &data->udev->dev;
3438 int irq, ret;
3439
3440 clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags);
3441
3442 if (!of_match_device(btusb_match_table, dev))
3443 return 0;
3444
3445 /* Move on if no IRQ specified */
3446 irq = of_irq_get_byname(dev->of_node, "wakeup");
3447 if (irq <= 0) {
3448 bt_dev_dbg(hdev, "%s: no OOB Wakeup IRQ in DT", __func__);
3449 return 0;
3450 }
3451
Brian Norris771acc72019-04-09 11:49:17 -07003452 irq_set_status_flags(irq, IRQ_NOAUTOEN);
Rajat Jainfd913ef2017-02-01 14:24:09 -08003453 ret = devm_request_irq(&hdev->dev, irq, btusb_oob_wake_handler,
3454 0, "OOB Wake-on-BT", data);
3455 if (ret) {
3456 bt_dev_err(hdev, "%s: IRQ request failed", __func__);
3457 return ret;
3458 }
3459
3460 ret = device_init_wakeup(dev, true);
3461 if (ret) {
3462 bt_dev_err(hdev, "%s: failed to init_wakeup", __func__);
3463 return ret;
3464 }
3465
3466 data->oob_wake_irq = irq;
Rajat Jainfd913ef2017-02-01 14:24:09 -08003467 bt_dev_info(hdev, "OOB Wake-on-BT configured at IRQ %u", irq);
3468 return 0;
3469}
3470#endif
3471
Hans de Goedefc549102018-04-27 11:26:43 +02003472static void btusb_check_needs_reset_resume(struct usb_interface *intf)
3473{
3474 if (dmi_check_system(btusb_needs_reset_resume_table))
3475 interface_to_usbdev(intf)->quirks |= USB_QUIRK_RESET_RESUME;
3476}
3477
Luiz Augusto von Dentz4539ca62021-10-01 13:22:31 -07003478static bool btusb_wakeup(struct hci_dev *hdev)
Abhishek Pandit-Subedib7d0bf12020-05-12 19:19:27 -07003479{
3480 struct btusb_data *data = hci_get_drvdata(hdev);
3481
Luiz Augusto von Dentz4539ca62021-10-01 13:22:31 -07003482 return device_may_wakeup(&data->udev->dev);
Abhishek Pandit-Subedib7d0bf12020-05-12 19:19:27 -07003483}
3484
Tim Jiang3b0d5252020-12-18 18:12:11 +08003485static int btusb_shutdown_qca(struct hci_dev *hdev)
3486{
3487 struct sk_buff *skb;
3488
3489 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
3490 if (IS_ERR(skb)) {
3491 bt_dev_err(hdev, "HCI reset during shutdown failed");
3492 return PTR_ERR(skb);
3493 }
3494 kfree_skb(skb);
3495
3496 return 0;
3497}
3498
Luiz Augusto von Dentz800fe5e2021-12-03 14:38:13 -08003499static ssize_t force_poll_sync_read(struct file *file, char __user *user_buf,
3500 size_t count, loff_t *ppos)
3501{
3502 struct btusb_data *data = file->private_data;
3503 char buf[3];
3504
3505 buf[0] = data->poll_sync ? 'Y' : 'N';
3506 buf[1] = '\n';
3507 buf[2] = '\0';
3508 return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
3509}
3510
3511static ssize_t force_poll_sync_write(struct file *file,
3512 const char __user *user_buf,
3513 size_t count, loff_t *ppos)
3514{
3515 struct btusb_data *data = file->private_data;
3516 bool enable;
3517 int err;
3518
3519 err = kstrtobool_from_user(user_buf, count, &enable);
3520 if (err)
3521 return err;
3522
3523 /* Only allow changes while the adapter is down */
3524 if (test_bit(HCI_UP, &data->hdev->flags))
3525 return -EPERM;
3526
3527 if (data->poll_sync == enable)
3528 return -EALREADY;
3529
3530 data->poll_sync = enable;
3531
3532 return count;
3533}
3534
3535static const struct file_operations force_poll_sync_fops = {
3536 .open = simple_open,
3537 .read = force_poll_sync_read,
3538 .write = force_poll_sync_write,
3539 .llseek = default_llseek,
3540};
3541
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003542static int btusb_probe(struct usb_interface *intf,
Marcel Holtmann89e75332014-09-16 04:44:50 +02003543 const struct usb_device_id *id)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003544{
3545 struct usb_endpoint_descriptor *ep_desc;
Rajat Jaindc786b22019-01-24 15:28:14 -08003546 struct gpio_desc *reset_gpio;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003547 struct btusb_data *data;
3548 struct hci_dev *hdev;
Marcel Holtmann22f8e9d2015-10-20 00:53:33 +02003549 unsigned ifnum_base;
Tedd Ho-Jeong An53492a62021-08-04 17:32:11 -07003550 int i, err, priv_size;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003551
3552 BT_DBG("intf %p id %p", intf, id);
3553
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02003554 /* interface numbers are hardcoded in the spec */
Marcel Holtmann22f8e9d2015-10-20 00:53:33 +02003555 if (intf->cur_altsetting->desc.bInterfaceNumber != 0) {
3556 if (!(id->driver_info & BTUSB_IFNUM_2))
3557 return -ENODEV;
3558 if (intf->cur_altsetting->desc.bInterfaceNumber != 2)
3559 return -ENODEV;
3560 }
3561
3562 ifnum_base = intf->cur_altsetting->desc.bInterfaceNumber;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003563
3564 if (!id->driver_info) {
3565 const struct usb_device_id *match;
Marcel Holtmann89e75332014-09-16 04:44:50 +02003566
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003567 match = usb_match_id(intf, blacklist_table);
3568 if (match)
3569 id = match;
3570 }
3571
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02003572 if (id->driver_info == BTUSB_IGNORE)
3573 return -ENODEV;
3574
Steven.Li2d25f8b2011-07-01 14:02:36 +08003575 if (id->driver_info & BTUSB_ATH3012) {
3576 struct usb_device *udev = interface_to_usbdev(intf);
3577
3578 /* Old firmware would otherwise let ath3k driver load
Derek Robsond98422c2017-07-22 13:47:07 +12003579 * patch and sysconfig files
3580 */
Takashi Iwai803cdb82018-05-21 22:34:52 +02003581 if (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x0001 &&
3582 !btusb_qca_need_patch(udev))
Steven.Li2d25f8b2011-07-01 14:02:36 +08003583 return -ENODEV;
3584 }
3585
Sachin Kamat98921db2012-07-27 12:38:39 +05303586 data = devm_kzalloc(&intf->dev, sizeof(*data), GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003587 if (!data)
3588 return -ENOMEM;
3589
3590 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
3591 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
3592
3593 if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
3594 data->intr_ep = ep_desc;
3595 continue;
3596 }
3597
3598 if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
3599 data->bulk_tx_ep = ep_desc;
3600 continue;
3601 }
3602
3603 if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
3604 data->bulk_rx_ep = ep_desc;
3605 continue;
3606 }
3607 }
3608
Sachin Kamat98921db2012-07-27 12:38:39 +05303609 if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003610 return -ENODEV;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003611
Marcel Holtmann893ba542015-01-28 20:27:34 -08003612 if (id->driver_info & BTUSB_AMP) {
3613 data->cmdreq_type = USB_TYPE_CLASS | 0x01;
3614 data->cmdreq = 0x2b;
3615 } else {
3616 data->cmdreq_type = USB_TYPE_CLASS;
3617 data->cmdreq = 0x00;
3618 }
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01003619
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003620 data->udev = interface_to_usbdev(intf);
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02003621 data->intf = intf;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003622
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003623 INIT_WORK(&data->work, btusb_work);
Oliver Neukum7bee5492009-08-24 23:44:59 +02003624 INIT_WORK(&data->waker, btusb_waker);
Luiz Augusto von Dentz800fe5e2021-12-03 14:38:13 -08003625 INIT_DELAYED_WORK(&data->rx_work, btusb_rx_work);
3626
3627 skb_queue_head_init(&data->acl_q);
3628
Marcel Holtmann803b5832014-09-16 08:00:29 +02003629 init_usb_anchor(&data->deferred);
3630 init_usb_anchor(&data->tx_anchor);
Oliver Neukum7bee5492009-08-24 23:44:59 +02003631 spin_lock_init(&data->txlock);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003632
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003633 init_usb_anchor(&data->intr_anchor);
3634 init_usb_anchor(&data->bulk_anchor);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02003635 init_usb_anchor(&data->isoc_anchor);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02003636 init_usb_anchor(&data->diag_anchor);
Sean Wanga1c49c432019-06-02 08:02:48 +08003637 init_usb_anchor(&data->ctrl_anchor);
Marcel Holtmann803b5832014-09-16 08:00:29 +02003638 spin_lock_init(&data->rxlock);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003639
Tedd Ho-Jeong An53492a62021-08-04 17:32:11 -07003640 priv_size = 0;
3641
Tedd Ho-Jeong An019a1ca2021-08-04 17:32:15 -07003642 data->recv_event = hci_recv_frame;
3643 data->recv_bulk = btusb_recv_bulk;
3644
Tedd Ho-Jeong An53492a62021-08-04 17:32:11 -07003645 if (id->driver_info & BTUSB_INTEL_COMBINED) {
3646 /* Allocate extra space for Intel device */
3647 priv_size += sizeof(struct btintel_data);
Tedd Ho-Jeong An53492a62021-08-04 17:32:11 -07003648
Tedd Ho-Jeong An019a1ca2021-08-04 17:32:15 -07003649 /* Override the rx handlers */
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08003650 data->recv_event = btusb_recv_event_intel;
3651 data->recv_bulk = btusb_recv_bulk_intel;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08003652 }
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +01003653
mark-yw.chen0b10c8c2021-08-04 17:03:15 +08003654 data->recv_acl = hci_recv_frame;
3655
Tedd Ho-Jeong An53492a62021-08-04 17:32:11 -07003656 hdev = hci_alloc_dev_priv(priv_size);
Sachin Kamat98921db2012-07-27 12:38:39 +05303657 if (!hdev)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003658 return -ENOMEM;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003659
Marcel Holtmannc13854c2010-02-08 15:27:07 +01003660 hdev->bus = HCI_USB;
David Herrmann155961e2012-02-09 21:58:32 +01003661 hci_set_drvdata(hdev, data);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003662
Marcel Holtmann893ba542015-01-28 20:27:34 -08003663 if (id->driver_info & BTUSB_AMP)
3664 hdev->dev_type = HCI_AMP;
3665 else
Marcel Holtmannca8bee52016-07-05 14:30:14 +02003666 hdev->dev_type = HCI_PRIMARY;
Marcel Holtmann893ba542015-01-28 20:27:34 -08003667
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003668 data->hdev = hdev;
3669
3670 SET_HCIDEV_DEV(hdev, &intf->dev);
3671
Rajat Jaindc786b22019-01-24 15:28:14 -08003672 reset_gpio = gpiod_get_optional(&data->udev->dev, "reset",
3673 GPIOD_OUT_LOW);
3674 if (IS_ERR(reset_gpio)) {
3675 err = PTR_ERR(reset_gpio);
3676 goto out_free_dev;
3677 } else if (reset_gpio) {
3678 data->reset_gpio = reset_gpio;
3679 }
3680
Marcel Holtmann9f8f9622013-04-10 08:11:35 -07003681 hdev->open = btusb_open;
3682 hdev->close = btusb_close;
3683 hdev->flush = btusb_flush;
3684 hdev->send = btusb_send_frame;
3685 hdev->notify = btusb_notify;
Luiz Augusto von Dentz4539ca62021-10-01 13:22:31 -07003686 hdev->wakeup = btusb_wakeup;
Marcel Holtmann9f8f9622013-04-10 08:11:35 -07003687
Rajat Jainfd913ef2017-02-01 14:24:09 -08003688#ifdef CONFIG_PM
3689 err = btusb_config_oob_wake(hdev);
3690 if (err)
3691 goto out_free_dev;
Rajat Jaina4ccc9e2017-02-01 14:24:10 -08003692
3693 /* Marvell devices may need a specific chip configuration */
3694 if (id->driver_info & BTUSB_MARVELL && data->oob_wake_irq) {
3695 err = marvell_config_oob_wake(hdev);
3696 if (err)
3697 goto out_free_dev;
3698 }
Rajat Jainfd913ef2017-02-01 14:24:09 -08003699#endif
Szymon Janc418678b2016-09-01 17:22:37 +02003700 if (id->driver_info & BTUSB_CW6622)
3701 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
3702
Marcel Holtmann6c9d4352015-10-17 14:39:27 +02003703 if (id->driver_info & BTUSB_BCM2045)
3704 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
3705
Marcel Holtmann9f8f9622013-04-10 08:11:35 -07003706 if (id->driver_info & BTUSB_BCM92035)
3707 hdev->setup = btusb_setup_bcm92035;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003708
Marcel Holtmannc0a21a52019-09-27 08:48:58 +02003709 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_BCM) &&
3710 (id->driver_info & BTUSB_BCM_PATCHRAM)) {
Marcel Holtmann49a5f782015-10-20 02:30:48 +02003711 hdev->manufacturer = 15;
Marcel Holtmannc2bfb102015-04-05 22:52:14 -07003712 hdev->setup = btbcm_setup_patchram;
Marcel Holtmann9d08f502015-10-08 20:23:08 +02003713 hdev->set_diag = btusb_bcm_set_diag;
Marcel Holtmann1df1f592015-04-05 22:52:11 -07003714 hdev->set_bdaddr = btbcm_set_bdaddr;
Marcel Holtmann9d08f502015-10-08 20:23:08 +02003715
3716 /* Broadcom LM_DIAG Interface numbers are hardcoded */
Marcel Holtmann22f8e9d2015-10-20 00:53:33 +02003717 data->diag = usb_ifnum_to_if(data->udev, ifnum_base + 2);
Marcel Holtmannabbaf502014-07-02 00:53:48 +02003718 }
Petri Gynther10d4c672014-05-08 15:50:01 -07003719
Marcel Holtmannc0a21a52019-09-27 08:48:58 +02003720 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_BCM) &&
3721 (id->driver_info & BTUSB_BCM_APPLE)) {
Marcel Holtmann49a5f782015-10-20 02:30:48 +02003722 hdev->manufacturer = 15;
Marcel Holtmannc2bfb102015-04-05 22:52:14 -07003723 hdev->setup = btbcm_setup_apple;
Marcel Holtmann9d08f502015-10-08 20:23:08 +02003724 hdev->set_diag = btusb_bcm_set_diag;
3725
3726 /* Broadcom LM_DIAG Interface numbers are hardcoded */
Marcel Holtmann22f8e9d2015-10-20 00:53:33 +02003727 data->diag = usb_ifnum_to_if(data->udev, ifnum_base + 2);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02003728 }
Marcel Holtmann17b27722015-03-22 15:52:38 +01003729
Tedd Ho-Jeong An83f2daf2021-08-04 17:32:10 -07003730 /* Combined Intel Device setup to support multiple setup routine */
3731 if (id->driver_info & BTUSB_INTEL_COMBINED) {
3732 err = btintel_configure_setup(hdev);
3733 if (err)
3734 goto out_free_dev;
3735
3736 /* Transport specific configuration */
Tedd Ho-Jeong An019a1ca2021-08-04 17:32:15 -07003737 hdev->send = btusb_send_frame_intel;
Rajat Jaindc786b22019-01-24 15:28:14 -08003738 hdev->cmd_timeout = btusb_intel_cmd_timeout;
Tedd Ho-Jeong An83f2daf2021-08-04 17:32:10 -07003739
Tedd Ho-Jeong Anea7c4c02021-08-04 17:32:12 -07003740 if (id->driver_info & BTUSB_INTEL_BROKEN_INITIAL_NCMD)
3741 btintel_set_flag(hdev, INTEL_BROKEN_INITIAL_NCMD);
Tedd Ho-Jeong An95655452022-01-06 16:34:54 -08003742
3743 if (id->driver_info & BTUSB_INTEL_BROKEN_SHUTDOWN_LED)
3744 btintel_set_flag(hdev, INTEL_BROKEN_SHUTDOWN_LED);
Marcel Holtmanncb8d6592014-07-02 11:25:25 +02003745 }
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07003746
Amitkumar Karwarae8df492014-07-18 14:47:06 -07003747 if (id->driver_info & BTUSB_MARVELL)
3748 hdev->set_bdaddr = btusb_set_bdaddr_marvell;
3749
Marcel Holtmannc0a21a52019-09-27 08:48:58 +02003750 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_MTK) &&
3751 (id->driver_info & BTUSB_MEDIATEK)) {
Sean Wanga1c49c432019-06-02 08:02:48 +08003752 hdev->setup = btusb_mtk_setup;
3753 hdev->shutdown = btusb_mtk_shutdown;
3754 hdev->manufacturer = 70;
mark-yw.chene9478022021-08-04 17:06:47 +08003755 hdev->cmd_timeout = btusb_mtk_cmd_timeout;
Sean Wang8c0d17b2021-10-19 05:30:12 +08003756 hdev->set_bdaddr = btmtk_set_bdaddr;
Sean Wanga1c49c432019-06-02 08:02:48 +08003757 set_bit(HCI_QUIRK_NON_PERSISTENT_SETUP, &hdev->quirks);
mark-yw.chen0b10c8c2021-08-04 17:03:15 +08003758 data->recv_acl = btusb_recv_acl_mtk;
Sean Wanga1c49c432019-06-02 08:02:48 +08003759 }
Sean Wanga1c49c432019-06-02 08:02:48 +08003760
Marcel Holtmann661cf882015-01-02 23:35:20 -08003761 if (id->driver_info & BTUSB_SWAVE) {
3762 set_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks);
Marcel Holtmannd57dbe72014-12-26 04:42:33 +01003763 set_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS, &hdev->quirks);
Marcel Holtmann661cf882015-01-02 23:35:20 -08003764 }
Marcel Holtmannd57dbe72014-12-26 04:42:33 +01003765
Marcel Holtmanne4c534b2015-10-21 01:31:45 +02003766 if (id->driver_info & BTUSB_INTEL_BOOT) {
3767 hdev->manufacturer = 2;
Marcel Holtmann40df7832014-07-06 13:29:58 +02003768 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
Marcel Holtmanne4c534b2015-10-21 01:31:45 +02003769 }
Marcel Holtmann40df7832014-07-06 13:29:58 +02003770
Jakub Pawlowski79f0c872015-01-29 10:38:34 -08003771 if (id->driver_info & BTUSB_ATH3012) {
Takashi Iwai803cdb82018-05-21 22:34:52 +02003772 data->setup_on_usb = btusb_setup_qca;
Toshi Kikuchi58592232014-12-12 10:58:05 -08003773 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
Jakub Pawlowski3d50d512015-03-17 09:04:15 -07003774 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
Jakub Pawlowski79f0c872015-01-29 10:38:34 -08003775 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
3776 }
Toshi Kikuchi58592232014-12-12 10:58:05 -08003777
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003778 if (id->driver_info & BTUSB_QCA_ROME) {
3779 data->setup_on_usb = btusb_setup_qca;
3780 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
Abhishek Pandit-Subedi69335682020-06-24 11:11:44 -07003781 hdev->cmd_timeout = btusb_qca_cmd_timeout;
Vic Wei96e58d32018-03-28 08:28:47 -07003782 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
Hans de Goedefc549102018-04-27 11:26:43 +02003783 btusb_check_needs_reset_resume(intf);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003784 }
3785
Rocky Liaob40f58b2020-09-29 12:23:51 +08003786 if (id->driver_info & BTUSB_QCA_WCN6855) {
3787 data->setup_on_usb = btusb_setup_qca;
Tim Jiang3b0d5252020-12-18 18:12:11 +08003788 hdev->shutdown = btusb_shutdown_qca;
Rocky Liaob40f58b2020-09-29 12:23:51 +08003789 hdev->set_bdaddr = btusb_set_bdaddr_wcn6855;
3790 hdev->cmd_timeout = btusb_qca_cmd_timeout;
3791 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
Michael Suncbe6a042021-07-29 10:10:59 -07003792 hci_set_msft_opcode(hdev, 0xFD70);
Rocky Liaob40f58b2020-09-29 12:23:51 +08003793 }
3794
Hilda Wu461f95f2020-06-30 21:09:40 +08003795 if (id->driver_info & BTUSB_AMP) {
3796 /* AMP controllers do not support SCO packets */
3797 data->isoc = NULL;
3798 } else {
3799 /* Interface orders are hardcoded in the specification */
3800 data->isoc = usb_ifnum_to_if(data->udev, ifnum_base + 1);
3801 data->isoc_ifnum = ifnum_base + 1;
3802 }
3803
Arnd Bergmann42d22092019-09-18 21:59:02 +02003804 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_RTL) &&
3805 (id->driver_info & BTUSB_REALTEK)) {
Carlo Caionedb33c772015-05-14 10:49:09 +02003806 hdev->setup = btrtl_setup_realtek;
Jian-Hong Pan7af3f5582019-06-25 16:30:51 +08003807 hdev->shutdown = btrtl_shutdown_realtek;
Alex Lud7ef0d12019-09-05 10:36:31 +08003808 hdev->cmd_timeout = btusb_rtl_cmd_timeout;
Daniel Drake04b8c812015-05-21 08:23:50 -06003809
Max Chou8274db02021-08-17 11:03:12 +08003810 /* Realtek devices need to set remote wakeup on auto-suspend */
3811 set_bit(BTUSB_WAKEUP_AUTOSUSPEND, &data->flags);
Pauli Virtanen55981d32021-07-26 21:02:06 +03003812 set_bit(BTUSB_USE_ALT3_FOR_WBS, &data->flags);
Daniel Drake04b8c812015-05-21 08:23:50 -06003813 }
Daniel Drakea2698a92015-04-16 14:09:55 -06003814
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01003815 if (!reset)
Szymon Janca6c511c2012-05-23 12:35:46 +02003816 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02003817
3818 if (force_scofix || id->driver_info & BTUSB_WRONG_SCO_MTU) {
3819 if (!disable_scofix)
3820 set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
3821 }
3822
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02003823 if (id->driver_info & BTUSB_BROKEN_ISOC)
3824 data->isoc = NULL;
3825
Alain Michaud4b127bd2020-02-27 18:29:39 +00003826 if (id->driver_info & BTUSB_WIDEBAND_SPEECH)
Alain Michaud00bce3f2020-03-05 16:14:59 +00003827 set_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED, &hdev->quirks);
Alain Michaud4b127bd2020-02-27 18:29:39 +00003828
Alain Michaudaff8c482020-04-23 14:43:31 +00003829 if (id->driver_info & BTUSB_VALID_LE_STATES)
3830 set_bit(HCI_QUIRK_VALID_LE_STATES, &hdev->quirks);
3831
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01003832 if (id->driver_info & BTUSB_DIGIANSWER) {
3833 data->cmdreq_type = USB_TYPE_VENDOR;
Szymon Janca6c511c2012-05-23 12:35:46 +02003834 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01003835 }
3836
3837 if (id->driver_info & BTUSB_CSR) {
3838 struct usb_device *udev = data->udev;
Marcel Holtmann81cac642014-01-03 03:02:36 -08003839 u16 bcdDevice = le16_to_cpu(udev->descriptor.bcdDevice);
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01003840
3841 /* Old firmware would otherwise execute USB reset */
Marcel Holtmann81cac642014-01-03 03:02:36 -08003842 if (bcdDevice < 0x117)
Szymon Janca6c511c2012-05-23 12:35:46 +02003843 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmann81cac642014-01-03 03:02:36 -08003844
Ismael Ferreras Morezuelascde1a8a2020-07-26 23:12:28 +02003845 /* This must be set first in case we disable it for fakes */
Jakub Pawlowski49c989a2015-03-17 09:04:17 -07003846 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
Ismael Ferreras Morezuelascde1a8a2020-07-26 23:12:28 +02003847
3848 /* Fake CSR devices with broken commands */
3849 if (le16_to_cpu(udev->descriptor.idVendor) == 0x0a12 &&
3850 le16_to_cpu(udev->descriptor.idProduct) == 0x0001)
3851 hdev->setup = btusb_setup_csr;
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01003852 }
3853
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02003854 if (id->driver_info & BTUSB_SNIFFER) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02003855 struct usb_device *udev = data->udev;
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02003856
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01003857 /* New sniffer firmware has crippled HCI interface */
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02003858 if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
3859 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
3860 }
3861
Marcel Holtmann3a5ef202014-07-06 14:53:54 +02003862 if (id->driver_info & BTUSB_INTEL_BOOT) {
3863 /* A bug in the bootloader causes that interrupt interface is
3864 * only enabled after receiving SetInterface(0, AltSetting=0).
3865 */
3866 err = usb_set_interface(data->udev, 0, 0);
3867 if (err < 0) {
3868 BT_ERR("failed to set interface 0, alt 0 %d", err);
Rajat Jain10ab1332017-02-01 14:24:08 -08003869 goto out_free_dev;
Marcel Holtmann3a5ef202014-07-06 14:53:54 +02003870 }
3871 }
3872
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02003873 if (data->isoc) {
3874 err = usb_driver_claim_interface(&btusb_driver,
Marcel Holtmann89e75332014-09-16 04:44:50 +02003875 data->isoc, data);
Rajat Jain10ab1332017-02-01 14:24:08 -08003876 if (err < 0)
3877 goto out_free_dev;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02003878 }
3879
Marcel Holtmannc0a21a52019-09-27 08:48:58 +02003880 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_BCM) && data->diag) {
Marcel Holtmann9d08f502015-10-08 20:23:08 +02003881 if (!usb_driver_claim_interface(&btusb_driver,
3882 data->diag, data))
3883 __set_diag_interface(hdev);
3884 else
3885 data->diag = NULL;
3886 }
Marcel Holtmann9d08f502015-10-08 20:23:08 +02003887
Hans de Goede89591522021-04-09 15:58:50 +02003888 if (enable_autosuspend)
3889 usb_enable_autosuspend(data->udev);
Hans de Goedeeff2d682017-11-13 14:44:16 +01003890
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003891 err = hci_register_dev(hdev);
Rajat Jain10ab1332017-02-01 14:24:08 -08003892 if (err < 0)
3893 goto out_free_dev;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003894
3895 usb_set_intfdata(intf, data);
3896
Luiz Augusto von Dentz800fe5e2021-12-03 14:38:13 -08003897 debugfs_create_file("force_poll_sync", 0644, hdev->debugfs, data,
3898 &force_poll_sync_fops);
3899
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003900 return 0;
Rajat Jain10ab1332017-02-01 14:24:08 -08003901
3902out_free_dev:
Rajat Jaindc786b22019-01-24 15:28:14 -08003903 if (data->reset_gpio)
3904 gpiod_put(data->reset_gpio);
Rajat Jain10ab1332017-02-01 14:24:08 -08003905 hci_free_dev(hdev);
3906 return err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003907}
3908
3909static void btusb_disconnect(struct usb_interface *intf)
3910{
3911 struct btusb_data *data = usb_get_intfdata(intf);
3912 struct hci_dev *hdev;
3913
3914 BT_DBG("intf %p", intf);
3915
3916 if (!data)
3917 return;
3918
3919 hdev = data->hdev;
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02003920 usb_set_intfdata(data->intf, NULL);
3921
3922 if (data->isoc)
3923 usb_set_intfdata(data->isoc, NULL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003924
Marcel Holtmann9d08f502015-10-08 20:23:08 +02003925 if (data->diag)
3926 usb_set_intfdata(data->diag, NULL);
3927
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003928 hci_unregister_dev(hdev);
3929
Marcel Holtmann9d08f502015-10-08 20:23:08 +02003930 if (intf == data->intf) {
3931 if (data->isoc)
3932 usb_driver_release_interface(&btusb_driver, data->isoc);
3933 if (data->diag)
3934 usb_driver_release_interface(&btusb_driver, data->diag);
3935 } else if (intf == data->isoc) {
3936 if (data->diag)
3937 usb_driver_release_interface(&btusb_driver, data->diag);
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02003938 usb_driver_release_interface(&btusb_driver, data->intf);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02003939 } else if (intf == data->diag) {
3940 usb_driver_release_interface(&btusb_driver, data->intf);
3941 if (data->isoc)
3942 usb_driver_release_interface(&btusb_driver, data->isoc);
3943 }
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02003944
Rajat Jainfd913ef2017-02-01 14:24:09 -08003945 if (data->oob_wake_irq)
3946 device_init_wakeup(&data->udev->dev, false);
3947
Rajat Jaindc786b22019-01-24 15:28:14 -08003948 if (data->reset_gpio)
3949 gpiod_put(data->reset_gpio);
3950
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003951 hci_free_dev(hdev);
3952}
3953
Oliver Neukum7bee5492009-08-24 23:44:59 +02003954#ifdef CONFIG_PM
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003955static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
3956{
3957 struct btusb_data *data = usb_get_intfdata(intf);
3958
3959 BT_DBG("intf %p", intf);
3960
3961 if (data->suspend_count++)
3962 return 0;
3963
Oliver Neukum7bee5492009-08-24 23:44:59 +02003964 spin_lock_irq(&data->txlock);
Alan Stern5b1b0b82011-08-19 23:49:48 +02003965 if (!(PMSG_IS_AUTO(message) && data->tx_in_flight)) {
Oliver Neukum7bee5492009-08-24 23:44:59 +02003966 set_bit(BTUSB_SUSPENDING, &data->flags);
3967 spin_unlock_irq(&data->txlock);
3968 } else {
3969 spin_unlock_irq(&data->txlock);
3970 data->suspend_count--;
3971 return -EBUSY;
3972 }
3973
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003974 cancel_work_sync(&data->work);
3975
Oliver Neukum7bee5492009-08-24 23:44:59 +02003976 btusb_stop_traffic(data);
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003977 usb_kill_anchored_urbs(&data->tx_anchor);
3978
Rajat Jainfd913ef2017-02-01 14:24:09 -08003979 if (data->oob_wake_irq && device_may_wakeup(&data->udev->dev)) {
3980 set_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags);
3981 enable_irq_wake(data->oob_wake_irq);
3982 enable_irq(data->oob_wake_irq);
3983 }
3984
Alex Lu9e455242019-08-14 20:02:52 +08003985 /* For global suspend, Realtek devices lose the loaded fw
3986 * in them. But for autosuspend, firmware should remain.
3987 * Actually, it depends on whether the usb host sends
3988 * set feature (enable wakeup) or not.
3989 */
Max Chou8274db02021-08-17 11:03:12 +08003990 if (test_bit(BTUSB_WAKEUP_AUTOSUSPEND, &data->flags)) {
Alex Lu9e455242019-08-14 20:02:52 +08003991 if (PMSG_IS_AUTO(message) &&
3992 device_can_wakeup(&data->udev->dev))
3993 data->udev->do_remote_wakeup = 1;
Max Chou8274db02021-08-17 11:03:12 +08003994 else if (!PMSG_IS_AUTO(message) &&
3995 !device_may_wakeup(&data->udev->dev)) {
3996 data->udev->do_remote_wakeup = 0;
Alex Lu9e455242019-08-14 20:02:52 +08003997 data->udev->reset_resume = 1;
Max Chou8274db02021-08-17 11:03:12 +08003998 }
Alex Lu9e455242019-08-14 20:02:52 +08003999 }
4000
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01004001 return 0;
4002}
4003
Oliver Neukum7bee5492009-08-24 23:44:59 +02004004static void play_deferred(struct btusb_data *data)
4005{
4006 struct urb *urb;
4007 int err;
4008
4009 while ((urb = usb_get_from_anchor(&data->deferred))) {
Jeffy Chen19cfe912017-07-20 18:53:50 +08004010 usb_anchor_urb(urb, &data->tx_anchor);
4011
Oliver Neukum7bee5492009-08-24 23:44:59 +02004012 err = usb_submit_urb(urb, GFP_ATOMIC);
Jeffy Chen19cfe912017-07-20 18:53:50 +08004013 if (err < 0) {
4014 if (err != -EPERM && err != -ENODEV)
4015 BT_ERR("%s urb %p submission failed (%d)",
4016 data->hdev->name, urb, -err);
4017 kfree(urb->setup_packet);
4018 usb_unanchor_urb(urb);
4019 usb_free_urb(urb);
Oliver Neukum7bee5492009-08-24 23:44:59 +02004020 break;
Jeffy Chen19cfe912017-07-20 18:53:50 +08004021 }
Oliver Neukum7bee5492009-08-24 23:44:59 +02004022
4023 data->tx_in_flight++;
Jeffy Chen19cfe912017-07-20 18:53:50 +08004024 usb_free_urb(urb);
Oliver Neukum7bee5492009-08-24 23:44:59 +02004025 }
Jeffy Chen19cfe912017-07-20 18:53:50 +08004026
4027 /* Cleanup the rest deferred urbs. */
4028 while ((urb = usb_get_from_anchor(&data->deferred))) {
4029 kfree(urb->setup_packet);
4030 usb_free_urb(urb);
4031 }
Oliver Neukum7bee5492009-08-24 23:44:59 +02004032}
4033
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01004034static int btusb_resume(struct usb_interface *intf)
4035{
4036 struct btusb_data *data = usb_get_intfdata(intf);
4037 struct hci_dev *hdev = data->hdev;
Oliver Neukum7bee5492009-08-24 23:44:59 +02004038 int err = 0;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01004039
4040 BT_DBG("intf %p", intf);
4041
4042 if (--data->suspend_count)
4043 return 0;
4044
Rajat Jainfd913ef2017-02-01 14:24:09 -08004045 /* Disable only if not already disabled (keep it balanced) */
4046 if (test_and_clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags)) {
4047 disable_irq(data->oob_wake_irq);
4048 disable_irq_wake(data->oob_wake_irq);
4049 }
4050
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01004051 if (!test_bit(HCI_RUNNING, &hdev->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +02004052 goto done;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01004053
4054 if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
4055 err = btusb_submit_intr_urb(hdev, GFP_NOIO);
4056 if (err < 0) {
4057 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02004058 goto failed;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01004059 }
4060 }
4061
4062 if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) {
Marcel Holtmann43c2e572009-02-04 17:41:38 +01004063 err = btusb_submit_bulk_urb(hdev, GFP_NOIO);
4064 if (err < 0) {
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01004065 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02004066 goto failed;
4067 }
4068
4069 btusb_submit_bulk_urb(hdev, GFP_NOIO);
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01004070 }
4071
4072 if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
4073 if (btusb_submit_isoc_urb(hdev, GFP_NOIO) < 0)
4074 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
4075 else
4076 btusb_submit_isoc_urb(hdev, GFP_NOIO);
4077 }
4078
Oliver Neukum7bee5492009-08-24 23:44:59 +02004079 spin_lock_irq(&data->txlock);
4080 play_deferred(data);
4081 clear_bit(BTUSB_SUSPENDING, &data->flags);
4082 spin_unlock_irq(&data->txlock);
4083 schedule_work(&data->work);
4084
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01004085 return 0;
Oliver Neukum7bee5492009-08-24 23:44:59 +02004086
4087failed:
4088 usb_scuttle_anchored_urbs(&data->deferred);
4089done:
4090 spin_lock_irq(&data->txlock);
4091 clear_bit(BTUSB_SUSPENDING, &data->flags);
4092 spin_unlock_irq(&data->txlock);
4093
4094 return err;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01004095}
Oliver Neukum7bee5492009-08-24 23:44:59 +02004096#endif
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01004097
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004098static struct usb_driver btusb_driver = {
4099 .name = "btusb",
4100 .probe = btusb_probe,
4101 .disconnect = btusb_disconnect,
Oliver Neukum7bee5492009-08-24 23:44:59 +02004102#ifdef CONFIG_PM
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01004103 .suspend = btusb_suspend,
4104 .resume = btusb_resume,
Oliver Neukum7bee5492009-08-24 23:44:59 +02004105#endif
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004106 .id_table = btusb_table,
Oliver Neukum7bee5492009-08-24 23:44:59 +02004107 .supports_autosuspend = 1,
Sarah Sharpe1f12eb2012-04-23 10:08:51 -07004108 .disable_hub_initiated_lpm = 1,
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004109};
4110
Greg Kroah-Hartman93f15082011-11-18 09:47:34 -08004111module_usb_driver(btusb_driver);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004112
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02004113module_param(disable_scofix, bool, 0644);
4114MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
4115
4116module_param(force_scofix, bool, 0644);
4117MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
4118
Hans de Goedeeff2d682017-11-13 14:44:16 +01004119module_param(enable_autosuspend, bool, 0644);
4120MODULE_PARM_DESC(enable_autosuspend, "Enable USB autosuspend by default");
4121
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02004122module_param(reset, bool, 0644);
4123MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
4124
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004125MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
4126MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
4127MODULE_VERSION(VERSION);
4128MODULE_LICENSE("GPL");