blob: 3864db1c6cd5d56713d285354cd31c0861fd8119 [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002/*
3 *
4 * Generic Bluetooth USB driver
5 *
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02006 * Copyright (C) 2005-2008 Marcel Holtmann <marcel@holtmann.org>
Marcel Holtmann5e23b922007-10-20 14:12:34 +02007 */
8
Hans de Goede1fdb9262018-02-20 09:06:18 +01009#include <linux/dmi.h>
Marcel Holtmann5e23b922007-10-20 14:12:34 +020010#include <linux/module.h>
Marcel Holtmann5e23b922007-10-20 14:12:34 +020011#include <linux/usb.h>
Hans de Goede61f5ace2018-01-08 10:44:16 +010012#include <linux/usb/quirks.h>
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -070013#include <linux/firmware.h>
Sean Wanga1c49c432019-06-02 08:02:48 +080014#include <linux/iopoll.h>
Rajat Jainfd913ef2017-02-01 14:24:09 -080015#include <linux/of_device.h>
16#include <linux/of_irq.h>
Jeffy Chen017789f2017-02-24 14:24:29 +080017#include <linux/suspend.h>
Rajat Jaindc786b22019-01-24 15:28:14 -080018#include <linux/gpio/consumer.h>
Daniel Drakea2698a92015-04-16 14:09:55 -060019#include <asm/unaligned.h>
Marcel Holtmann5e23b922007-10-20 14:12:34 +020020
21#include <net/bluetooth/bluetooth.h>
22#include <net/bluetooth/hci_core.h>
23
Marcel Holtmann4185a0f2015-04-06 00:52:12 -070024#include "btintel.h"
Marcel Holtmann1df1f592015-04-05 22:52:11 -070025#include "btbcm.h"
Carlo Caionedb33c772015-05-14 10:49:09 +020026#include "btrtl.h"
Marcel Holtmann1df1f592015-04-05 22:52:11 -070027
Marcel Holtmann34dced9b2015-04-05 22:52:16 -070028#define VERSION "0.8"
Marcel Holtmanncfeb4142008-08-07 22:26:56 +020029
Rusty Russell90ab5ee2012-01-13 09:32:20 +103030static bool disable_scofix;
31static bool force_scofix;
Hans de Goedeeff2d682017-11-13 14:44:16 +010032static bool enable_autosuspend = IS_ENABLED(CONFIG_BT_HCIBTUSB_AUTOSUSPEND);
Marcel Holtmann7a9d4022008-11-30 12:17:26 +010033
Shailendra Verma917a3332015-05-25 23:53:40 +053034static bool reset = true;
Marcel Holtmanncfeb4142008-08-07 22:26:56 +020035
36static struct usb_driver btusb_driver;
37
38#define BTUSB_IGNORE 0x01
Marcel Holtmann7a9d4022008-11-30 12:17:26 +010039#define BTUSB_DIGIANSWER 0x02
40#define BTUSB_CSR 0x04
41#define BTUSB_SNIFFER 0x08
42#define BTUSB_BCM92035 0x10
43#define BTUSB_BROKEN_ISOC 0x20
44#define BTUSB_WRONG_SCO_MTU 0x40
Steven.Li2d25f8b2011-07-01 14:02:36 +080045#define BTUSB_ATH3012 0x80
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -070046#define BTUSB_INTEL 0x100
Marcel Holtmann40df7832014-07-06 13:29:58 +020047#define BTUSB_INTEL_BOOT 0x200
48#define BTUSB_BCM_PATCHRAM 0x400
Amitkumar Karwarae8df492014-07-18 14:47:06 -070049#define BTUSB_MARVELL 0x800
Marcel Holtmann4fcef8e2015-01-01 17:34:37 -080050#define BTUSB_SWAVE 0x1000
Marcel Holtmanncda0dd72015-01-26 21:33:48 -080051#define BTUSB_INTEL_NEW 0x2000
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
Kiran K0a3c1d42020-11-19 17:09:40 +053063#define BTUSB_INTEL_NEWGEN 0x2000000
Marcel Holtmann5e23b922007-10-20 14:12:34 +020064
Marcel Holtmann54265202013-10-11 07:46:18 -070065static const struct usb_device_id btusb_table[] = {
Marcel Holtmann5e23b922007-10-20 14:12:34 +020066 /* Generic Bluetooth USB device */
67 { USB_DEVICE_INFO(0xe0, 0x01, 0x01) },
68
Marcel Holtmann893ba542015-01-28 20:27:34 -080069 /* Generic Bluetooth AMP device */
70 { USB_DEVICE_INFO(0xe0, 0x01, 0x04), .driver_info = BTUSB_AMP },
71
Daniel Draked63b2822015-07-17 11:12:25 -060072 /* Generic Bluetooth USB interface */
73 { USB_INTERFACE_INFO(0xe0, 0x01, 0x01) },
74
Henrik Rydberg1fa65352012-08-25 19:28:06 +020075 /* Apple-specific (Broadcom) devices */
Marcel Holtmann17b27722015-03-22 15:52:38 +010076 { USB_VENDOR_AND_INTERFACE_INFO(0x05ac, 0xff, 0x01, 0x01),
Marcel Holtmann22f8e9d2015-10-20 00:53:33 +020077 .driver_info = BTUSB_BCM_APPLE | BTUSB_IFNUM_2 },
Henrik Rydberg1fa65352012-08-25 19:28:06 +020078
Cho, Yu-Chen178c0592013-06-04 21:40:26 +080079 /* MediaTek MT76x0E */
80 { USB_DEVICE(0x0e8d, 0x763f) },
81
Oliver Neukumc510eae2011-09-21 11:41:45 +020082 /* Broadcom SoftSailing reporting vendor specific */
Don Zickus2e8b50632012-03-28 16:41:11 -040083 { USB_DEVICE(0x0a5c, 0x21e1) },
Oliver Neukumc510eae2011-09-21 11:41:45 +020084
Nobuhiro Iwamatsu3cd01972010-08-20 16:24:07 +090085 /* Apple MacBookPro 7,1 */
86 { USB_DEVICE(0x05ac, 0x8213) },
87
Cyril Lacoux0a79f672010-07-14 10:29:27 +040088 /* Apple iMac11,1 */
89 { USB_DEVICE(0x05ac, 0x8215) },
90
Nobuhiro Iwamatsu9c047152010-08-20 16:24:06 +090091 /* Apple MacBookPro6,2 */
92 { USB_DEVICE(0x05ac, 0x8218) },
93
Edgar (gimli) Hucek3e3ede72010-11-04 08:04:33 +010094 /* Apple MacBookAir3,1, MacBookAir3,2 */
95 { USB_DEVICE(0x05ac, 0x821b) },
96
Pieter-Augustijn Van Malleghema63b7232011-09-07 02:28:10 -040097 /* Apple MacBookAir4,1 */
98 { USB_DEVICE(0x05ac, 0x821f) },
99
Marc-Antoine Perennou88d377b2011-03-24 14:51:21 -0300100 /* Apple MacBookPro8,2 */
101 { USB_DEVICE(0x05ac, 0x821a) },
102
Jurgen Kramerf78b68262011-09-04 18:01:42 +0200103 /* Apple MacMini5,1 */
104 { USB_DEVICE(0x05ac, 0x8281) },
105
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200106 /* AVM BlueFRITZ! USB v2.0 */
Marcel Holtmann4fcef8e2015-01-01 17:34:37 -0800107 { USB_DEVICE(0x057c, 0x3800), .driver_info = BTUSB_SWAVE },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200108
109 /* Bluetooth Ultraport Module from IBM */
110 { USB_DEVICE(0x04bf, 0x030a) },
111
112 /* ALPS Modules with non-standard id */
113 { USB_DEVICE(0x044e, 0x3001) },
114 { USB_DEVICE(0x044e, 0x3002) },
115
116 /* Ericsson with non-standard id */
117 { USB_DEVICE(0x0bdb, 0x1002) },
118
119 /* Canyon CN-BTU1 with HID interfaces */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100120 { USB_DEVICE(0x0c10, 0x0000) },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200121
Wen-chien Jesse Sungd13431c2011-11-08 14:30:22 +0800122 /* Broadcom BCM20702A0 */
123 { USB_DEVICE(0x413c, 0x8197) },
124
Marcel Holtmannd049f4e2015-01-26 20:35:32 -0800125 /* Broadcom BCM20702B0 (Dynex/Insignia) */
126 { USB_DEVICE(0x19ff, 0x0239), .driver_info = BTUSB_BCM_PATCHRAM },
127
Santtu Rekilä2faf71c2015-10-05 15:45:27 +0300128 /* Broadcom BCM43142A0 (Foxconn/Lenovo) */
Dmitry Tunin628c26b2017-07-06 14:41:13 +0300129 { USB_VENDOR_AND_INTERFACE_INFO(0x105b, 0xff, 0x01, 0x01),
130 .driver_info = BTUSB_BCM_PATCHRAM },
Santtu Rekilä2faf71c2015-10-05 15:45:27 +0300131
Christoph Haaga57bac42017-02-10 14:02:45 +0100132 /* Broadcom BCM920703 (HTC Vive) */
133 { USB_VENDOR_AND_INTERFACE_INFO(0x0bb4, 0xff, 0x01, 0x01),
134 .driver_info = BTUSB_BCM_PATCHRAM },
135
Steven Harms98514032012-04-13 14:45:55 -0400136 /* Foxconn - Hon Hai */
Heinrich Siebmanns6029ddc2014-12-03 19:32:22 +0100137 { USB_VENDOR_AND_INTERFACE_INFO(0x0489, 0xff, 0x01, 0x01),
138 .driver_info = BTUSB_BCM_PATCHRAM },
Steven Harms98514032012-04-13 14:45:55 -0400139
Matej Dubovy8f0c3042015-02-02 18:50:14 +0100140 /* Lite-On Technology - Broadcom based */
141 { USB_VENDOR_AND_INTERFACE_INFO(0x04ca, 0xff, 0x01, 0x01),
142 .driver_info = BTUSB_BCM_PATCHRAM },
143
Andy Shevchenko0b880062014-02-18 18:26:19 +0200144 /* Broadcom devices with vendor specific id */
Petri Gynther10d4c672014-05-08 15:50:01 -0700145 { USB_VENDOR_AND_INTERFACE_INFO(0x0a5c, 0xff, 0x01, 0x01),
146 .driver_info = BTUSB_BCM_PATCHRAM },
Gustavo Padovan92c385f2012-08-06 15:36:49 -0300147
Marcel Holtmannc2aef6e2014-07-21 14:02:33 +0200148 /* ASUSTek Computer - Broadcom based */
Rick Dunn9a5abdaa2015-01-17 05:29:12 +0100149 { USB_VENDOR_AND_INTERFACE_INFO(0x0b05, 0xff, 0x01, 0x01),
150 .driver_info = BTUSB_BCM_PATCHRAM },
Marcel Holtmannc2aef6e2014-07-21 14:02:33 +0200151
Ken O'Brien5bcecf32013-09-21 19:14:43 +0100152 /* Belkin F8065bf - Broadcom based */
Marcel Holtmann6331c682015-03-27 15:11:41 -0700153 { USB_VENDOR_AND_INTERFACE_INFO(0x050d, 0xff, 0x01, 0x01),
154 .driver_info = BTUSB_BCM_PATCHRAM },
Ken O'Brien5bcecf32013-09-21 19:14:43 +0100155
Jurgen Kramer9113bfd2014-02-15 12:01:09 +0100156 /* IMC Networks - Broadcom based */
Marcel Holtmann6331c682015-03-27 15:11:41 -0700157 { USB_VENDOR_AND_INTERFACE_INFO(0x13d3, 0xff, 0x01, 0x01),
158 .driver_info = BTUSB_BCM_PATCHRAM },
Jurgen Kramer9113bfd2014-02-15 12:01:09 +0100159
Wen-chien Jesse Sungfdfddc62017-01-10 15:41:13 +0800160 /* Dell Computer - Broadcom based */
161 { USB_VENDOR_AND_INTERFACE_INFO(0x413c, 0xff, 0x01, 0x01),
162 .driver_info = BTUSB_BCM_PATCHRAM },
163
Dmitry Tunin1623d0b2015-12-05 14:09:36 +0300164 /* Toshiba Corp - Broadcom based */
165 { USB_VENDOR_AND_INTERFACE_INFO(0x0930, 0xff, 0x01, 0x01),
166 .driver_info = BTUSB_BCM_PATCHRAM },
167
Marcel Holtmann40df7832014-07-06 13:29:58 +0200168 /* Intel Bluetooth USB Bootloader (RAM module) */
Marcel Holtmannd92f2df2014-07-06 14:53:55 +0200169 { USB_DEVICE(0x8087, 0x0a5a),
170 .driver_info = BTUSB_INTEL_BOOT | BTUSB_BROKEN_ISOC },
Marcel Holtmann40df7832014-07-06 13:29:58 +0200171
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200172 { } /* Terminating entry */
173};
174
175MODULE_DEVICE_TABLE(usb, btusb_table);
176
Marcel Holtmann54265202013-10-11 07:46:18 -0700177static const struct usb_device_id blacklist_table[] = {
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200178 /* CSR BlueCore devices */
179 { USB_DEVICE(0x0a12, 0x0001), .driver_info = BTUSB_CSR },
180
181 /* Broadcom BCM2033 without firmware */
182 { USB_DEVICE(0x0a5c, 0x2033), .driver_info = BTUSB_IGNORE },
183
Marcel Holtmann6c9d4352015-10-17 14:39:27 +0200184 /* Broadcom BCM2045 devices */
185 { USB_DEVICE(0x0a5c, 0x2045), .driver_info = BTUSB_BCM2045 },
186
Bala Shanmugambe931122010-11-26 17:35:46 +0530187 /* Atheros 3011 with sflash firmware */
Andy Shevchenko0b880062014-02-18 18:26:19 +0200188 { USB_DEVICE(0x0489, 0xe027), .driver_info = BTUSB_IGNORE },
189 { USB_DEVICE(0x0489, 0xe03d), .driver_info = BTUSB_IGNORE },
Alexander Ploumistos2eeff0b2015-02-13 21:05:11 +0200190 { USB_DEVICE(0x04f2, 0xaff1), .driver_info = BTUSB_IGNORE },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200191 { USB_DEVICE(0x0930, 0x0215), .driver_info = BTUSB_IGNORE },
Bala Shanmugambe931122010-11-26 17:35:46 +0530192 { USB_DEVICE(0x0cf3, 0x3002), .driver_info = BTUSB_IGNORE },
Marek Vasut6eda5412012-06-08 14:32:50 +0200193 { USB_DEVICE(0x0cf3, 0xe019), .driver_info = BTUSB_IGNORE },
Andy Ross2a7bccc2011-05-09 16:11:16 -0700194 { USB_DEVICE(0x13d3, 0x3304), .driver_info = BTUSB_IGNORE },
Bala Shanmugambe931122010-11-26 17:35:46 +0530195
Cho, Yu-Chen509e7862011-01-26 17:10:59 +0800196 /* Atheros AR9285 Malbec with sflash firmware */
197 { USB_DEVICE(0x03f0, 0x311d), .driver_info = BTUSB_IGNORE },
198
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530199 /* Atheros 3012 with sflash firmware */
Andy Shevchenko0b880062014-02-18 18:26:19 +0200200 { USB_DEVICE(0x0489, 0xe04d), .driver_info = BTUSB_ATH3012 },
201 { USB_DEVICE(0x0489, 0xe04e), .driver_info = BTUSB_ATH3012 },
202 { USB_DEVICE(0x0489, 0xe056), .driver_info = BTUSB_ATH3012 },
203 { USB_DEVICE(0x0489, 0xe057), .driver_info = BTUSB_ATH3012 },
204 { USB_DEVICE(0x0489, 0xe05f), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin692c0622015-06-06 20:25:40 +0300205 { USB_DEVICE(0x0489, 0xe076), .driver_info = BTUSB_ATH3012 },
Anantha Krishnan4b552bc2014-10-06 16:31:49 +0530206 { USB_DEVICE(0x0489, 0xe078), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin28c971d2016-02-10 00:49:11 +0300207 { USB_DEVICE(0x0489, 0xe095), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200208 { USB_DEVICE(0x04c5, 0x1330), .driver_info = BTUSB_ATH3012 },
209 { USB_DEVICE(0x04ca, 0x3004), .driver_info = BTUSB_ATH3012 },
210 { USB_DEVICE(0x04ca, 0x3005), .driver_info = BTUSB_ATH3012 },
211 { USB_DEVICE(0x04ca, 0x3006), .driver_info = BTUSB_ATH3012 },
Mohammed Habibulla1fb4e092014-04-17 11:37:13 -0700212 { USB_DEVICE(0x04ca, 0x3007), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200213 { USB_DEVICE(0x04ca, 0x3008), .driver_info = BTUSB_ATH3012 },
214 { USB_DEVICE(0x04ca, 0x300b), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin7e730c7f2015-06-18 20:41:51 +0300215 { USB_DEVICE(0x04ca, 0x300d), .driver_info = BTUSB_ATH3012 },
Dmitry Tuninec0810d2015-05-02 13:36:58 +0300216 { USB_DEVICE(0x04ca, 0x300f), .driver_info = BTUSB_ATH3012 },
Janne Heikkinen134d3b32014-12-09 07:44:51 +0200217 { USB_DEVICE(0x04ca, 0x3010), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin81d90442016-02-28 11:04:06 +0300218 { USB_DEVICE(0x04ca, 0x3014), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin441ad622017-01-05 13:19:53 +0300219 { USB_DEVICE(0x04ca, 0x3018), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200220 { USB_DEVICE(0x0930, 0x0219), .driver_info = BTUSB_ATH3012 },
Dmitry Tunincd355ff2015-10-05 19:29:33 +0300221 { USB_DEVICE(0x0930, 0x021c), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200222 { USB_DEVICE(0x0930, 0x0220), .driver_info = BTUSB_ATH3012 },
Vincent Zwanenburg89d29752014-08-08 12:33:56 +0100223 { USB_DEVICE(0x0930, 0x0227), .driver_info = BTUSB_ATH3012 },
Andy Shevchenkoa735f9e2014-02-18 18:26:20 +0200224 { USB_DEVICE(0x0b05, 0x17d0), .driver_info = BTUSB_ATH3012 },
Ming Leid66629c2013-03-15 11:00:39 +0800225 { USB_DEVICE(0x0cf3, 0x0036), .driver_info = BTUSB_ATH3012 },
Hans de Goede544a5912018-04-26 14:18:19 +0200226 { USB_DEVICE(0x0cf3, 0x3004), .driver_info = BTUSB_ATH3012 },
Sunguk Lee94a32d102013-03-12 04:41:58 +0900227 { USB_DEVICE(0x0cf3, 0x3008), .driver_info = BTUSB_ATH3012 },
Cho, Yu-Chen07c0ea82012-03-14 22:01:21 +0200228 { USB_DEVICE(0x0cf3, 0x311d), .driver_info = BTUSB_ATH3012 },
Oliver Neukumb1312372014-01-16 15:37:11 +0100229 { USB_DEVICE(0x0cf3, 0x311e), .driver_info = BTUSB_ATH3012 },
Oliver Neukum1e56f1e2014-01-16 16:02:58 +0100230 { USB_DEVICE(0x0cf3, 0x311f), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200231 { USB_DEVICE(0x0cf3, 0x3121), .driver_info = BTUSB_ATH3012 },
Ming Leiebaf5792013-03-18 23:45:11 +0800232 { USB_DEVICE(0x0cf3, 0x817a), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin18e0afa2015-10-16 11:45:26 +0300233 { USB_DEVICE(0x0cf3, 0x817b), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200234 { USB_DEVICE(0x0cf3, 0xe003), .driver_info = BTUSB_ATH3012 },
AceLan Kaoac713112012-04-19 14:53:45 +0800235 { USB_DEVICE(0x0cf3, 0xe004), .driver_info = BTUSB_ATH3012 },
Peng Chen0a3658c2013-08-30 17:41:40 +0800236 { USB_DEVICE(0x0cf3, 0xe005), .driver_info = BTUSB_ATH3012 },
Wen-chien Jesse Sungca79f232015-05-13 11:39:24 +0800237 { USB_DEVICE(0x0cf3, 0xe006), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200238 { USB_DEVICE(0x13d3, 0x3362), .driver_info = BTUSB_ATH3012 },
239 { USB_DEVICE(0x13d3, 0x3375), .driver_info = BTUSB_ATH3012 },
AceLan Kaoeed307e2012-12-11 11:41:20 +0800240 { USB_DEVICE(0x13d3, 0x3393), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin609574e2016-02-10 15:33:17 +0300241 { USB_DEVICE(0x13d3, 0x3395), .driver_info = BTUSB_ATH3012 },
Sujith Manoharan5b77a1f2013-07-15 09:29:03 +0530242 { USB_DEVICE(0x13d3, 0x3402), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin3bb30a7c2014-11-25 20:19:52 +0300243 { USB_DEVICE(0x13d3, 0x3408), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin033efa92015-01-18 00:16:51 +0300244 { USB_DEVICE(0x13d3, 0x3423), .driver_info = BTUSB_ATH3012 },
Anantha Krishnanfa2f1392014-07-08 19:25:08 +0530245 { USB_DEVICE(0x13d3, 0x3432), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin75c6aca2016-03-04 01:32:19 +0300246 { USB_DEVICE(0x13d3, 0x3472), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin0d0cef62015-06-06 20:29:25 +0300247 { USB_DEVICE(0x13d3, 0x3474), .driver_info = BTUSB_ATH3012 },
Lauro Costa72f9f8b2016-05-09 17:36:11 -0300248 { USB_DEVICE(0x13d3, 0x3487), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin12d86892016-07-12 01:35:18 +0300249 { USB_DEVICE(0x13d3, 0x3490), .driver_info = BTUSB_ATH3012 },
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530250
Cho, Yu-Chene9036e32011-02-15 10:20:07 +0800251 /* Atheros AR5BBU12 with sflash firmware */
252 { USB_DEVICE(0x0489, 0xe02c), .driver_info = BTUSB_IGNORE },
253
Michael Gruetzner85d59722012-05-02 22:33:40 +0200254 /* Atheros AR5BBU12 with sflash firmware */
Yevgeniy Melnichukbc21fde2012-08-07 19:48:10 +0530255 { USB_DEVICE(0x0489, 0xe036), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200256 { USB_DEVICE(0x0489, 0xe03c), .driver_info = BTUSB_ATH3012 },
Michael Gruetzner85d59722012-05-02 22:33:40 +0200257
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +0000258 /* QCA ROME chipset */
Rocky Liaoe99f38b2020-09-17 21:09:23 +0800259 { USB_DEVICE(0x0cf3, 0x535b), .driver_info = BTUSB_QCA_ROME |
260 BTUSB_WIDEBAND_SPEECH },
261 { USB_DEVICE(0x0cf3, 0xe007), .driver_info = BTUSB_QCA_ROME |
262 BTUSB_WIDEBAND_SPEECH },
263 { USB_DEVICE(0x0cf3, 0xe009), .driver_info = BTUSB_QCA_ROME |
264 BTUSB_WIDEBAND_SPEECH },
265 { USB_DEVICE(0x0cf3, 0xe010), .driver_info = BTUSB_QCA_ROME |
266 BTUSB_WIDEBAND_SPEECH },
267 { USB_DEVICE(0x0cf3, 0xe300), .driver_info = BTUSB_QCA_ROME |
268 BTUSB_WIDEBAND_SPEECH },
269 { USB_DEVICE(0x0cf3, 0xe301), .driver_info = BTUSB_QCA_ROME |
270 BTUSB_WIDEBAND_SPEECH },
271 { USB_DEVICE(0x0cf3, 0xe360), .driver_info = BTUSB_QCA_ROME |
272 BTUSB_WIDEBAND_SPEECH },
273 { 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 |
298 BTUSB_WIDEBAND_SPEECH },
299
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200300 /* Broadcom BCM2035 */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100301 { USB_DEVICE(0x0a5c, 0x2009), .driver_info = BTUSB_BCM92035 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200302 { USB_DEVICE(0x0a5c, 0x200a), .driver_info = BTUSB_WRONG_SCO_MTU },
303 { USB_DEVICE(0x0a5c, 0x2035), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200304
305 /* Broadcom BCM2045 */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100306 { USB_DEVICE(0x0a5c, 0x2039), .driver_info = BTUSB_WRONG_SCO_MTU },
307 { USB_DEVICE(0x0a5c, 0x2101), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmannbdbef3d2008-09-23 00:16:35 +0200308
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200309 /* IBM/Lenovo ThinkPad with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100310 { USB_DEVICE(0x0a5c, 0x201e), .driver_info = BTUSB_WRONG_SCO_MTU },
311 { USB_DEVICE(0x0a5c, 0x2110), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200312
313 /* HP laptop with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100314 { USB_DEVICE(0x03f0, 0x171d), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200315
316 /* Dell laptop with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100317 { USB_DEVICE(0x413c, 0x8126), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200318
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100319 /* Dell Wireless 370 and 410 devices */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100320 { USB_DEVICE(0x413c, 0x8152), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100321 { USB_DEVICE(0x413c, 0x8156), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200322
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100323 /* Belkin F8T012 and F8T013 devices */
324 { USB_DEVICE(0x050d, 0x0012), .driver_info = BTUSB_WRONG_SCO_MTU },
325 { USB_DEVICE(0x050d, 0x0013), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200326
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100327 /* Asus WL-BTD202 device */
328 { USB_DEVICE(0x0b05, 0x1715), .driver_info = BTUSB_WRONG_SCO_MTU },
329
330 /* Kensington Bluetooth USB adapter */
331 { USB_DEVICE(0x047d, 0x105e), .driver_info = BTUSB_WRONG_SCO_MTU },
332
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200333 /* RTX Telecom based adapters with buggy SCO support */
334 { USB_DEVICE(0x0400, 0x0807), .driver_info = BTUSB_BROKEN_ISOC },
335 { USB_DEVICE(0x0400, 0x080a), .driver_info = BTUSB_BROKEN_ISOC },
336
337 /* CONWISE Technology based adapters with buggy SCO support */
Szymon Janc418678b2016-09-01 17:22:37 +0200338 { USB_DEVICE(0x0e5e, 0x6622),
339 .driver_info = BTUSB_BROKEN_ISOC | BTUSB_CW6622},
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200340
Marcel Holtmann4fcef8e2015-01-01 17:34:37 -0800341 /* Roper Class 1 Bluetooth Dongle (Silicon Wave based) */
Aleksei Volkov2eeac872015-06-08 12:02:10 +0300342 { USB_DEVICE(0x1310, 0x0001), .driver_info = BTUSB_SWAVE },
Marcel Holtmann4fcef8e2015-01-01 17:34:37 -0800343
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200344 /* Digianswer devices */
345 { USB_DEVICE(0x08fd, 0x0001), .driver_info = BTUSB_DIGIANSWER },
346 { USB_DEVICE(0x08fd, 0x0002), .driver_info = BTUSB_IGNORE },
347
348 /* CSR BlueCore Bluetooth Sniffer */
Marcel Holtmann4f64fa82014-07-07 00:12:04 +0200349 { USB_DEVICE(0x0a12, 0x0002),
350 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200351
352 /* Frontline ComProbe Bluetooth Sniffer */
Marcel Holtmann4f64fa82014-07-07 00:12:04 +0200353 { USB_DEVICE(0x16d3, 0x0002),
354 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200355
Marcel Holtmanncb1ee892015-01-28 19:41:42 -0800356 /* Marvell Bluetooth devices */
357 { USB_DEVICE(0x1286, 0x2044), .driver_info = BTUSB_MARVELL },
358 { USB_DEVICE(0x1286, 0x2046), .driver_info = BTUSB_MARVELL },
Amitkumar Karwar1165df02016-09-28 16:18:35 +0530359 { USB_DEVICE(0x1286, 0x204e), .driver_info = BTUSB_MARVELL },
Marcel Holtmanncb1ee892015-01-28 19:41:42 -0800360
Marcel Holtmannd0ac9eb2015-01-28 19:41:43 -0800361 /* Intel Bluetooth devices */
Alain Michaud3e4e3f72020-02-27 18:29:37 +0000362 { USB_DEVICE(0x8087, 0x0025), .driver_info = BTUSB_INTEL_NEW |
Alain Michaudaff8c482020-04-23 14:43:31 +0000363 BTUSB_WIDEBAND_SPEECH |
364 BTUSB_VALID_LE_STATES },
Alain Michaud3e4e3f72020-02-27 18:29:37 +0000365 { USB_DEVICE(0x8087, 0x0026), .driver_info = BTUSB_INTEL_NEW |
366 BTUSB_WIDEBAND_SPEECH },
367 { USB_DEVICE(0x8087, 0x0029), .driver_info = BTUSB_INTEL_NEW |
368 BTUSB_WIDEBAND_SPEECH },
Raghuram Hegde875e167592020-05-11 16:40:40 +0530369 { USB_DEVICE(0x8087, 0x0032), .driver_info = BTUSB_INTEL_NEW |
370 BTUSB_WIDEBAND_SPEECH},
Marcel Holtmann407550f2015-02-22 15:41:18 -0800371 { USB_DEVICE(0x8087, 0x07da), .driver_info = BTUSB_CSR },
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -0700372 { USB_DEVICE(0x8087, 0x07dc), .driver_info = BTUSB_INTEL },
Tedd Ho-Jeong Anef4e5e42013-11-12 13:10:58 -0800373 { USB_DEVICE(0x8087, 0x0a2a), .driver_info = BTUSB_INTEL },
Alain Michaud3e4e3f72020-02-27 18:29:37 +0000374 { USB_DEVICE(0x8087, 0x0a2b), .driver_info = BTUSB_INTEL_NEW |
375 BTUSB_WIDEBAND_SPEECH },
376 { USB_DEVICE(0x8087, 0x0aa7), .driver_info = BTUSB_INTEL |
377 BTUSB_WIDEBAND_SPEECH },
378 { USB_DEVICE(0x8087, 0x0aaa), .driver_info = BTUSB_INTEL_NEW |
Alain Michaudaff8c482020-04-23 14:43:31 +0000379 BTUSB_WIDEBAND_SPEECH |
380 BTUSB_VALID_LE_STATES },
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -0700381
Marcel Holtmannd0ac9eb2015-01-28 19:41:43 -0800382 /* Other Intel Bluetooth devices */
383 { USB_VENDOR_AND_INTERFACE_INFO(0x8087, 0xe0, 0x01, 0x01),
384 .driver_info = BTUSB_IGNORE },
Amitkumar Karwarae8df492014-07-18 14:47:06 -0700385
Joseph Hwang33bfd942020-07-13 15:45:29 +0800386 /* Realtek 8822CE Bluetooth devices */
387 { USB_DEVICE(0x0bda, 0xb00c), .driver_info = BTUSB_REALTEK |
388 BTUSB_WIDEBAND_SPEECH },
389
Max Chou0d484db2020-11-10 11:38:37 +0800390 /* Realtek 8852AE Bluetooth devices */
391 { USB_DEVICE(0x0bda, 0xc852), .driver_info = BTUSB_REALTEK |
392 BTUSB_WIDEBAND_SPEECH },
393
Daniel Drakea2698a92015-04-16 14:09:55 -0600394 /* Realtek Bluetooth devices */
395 { USB_VENDOR_AND_INTERFACE_INFO(0x0bda, 0xe0, 0x01, 0x01),
396 .driver_info = BTUSB_REALTEK },
397
Sean Wanga1c49c432019-06-02 08:02:48 +0800398 /* MediaTek Bluetooth devices */
399 { USB_VENDOR_AND_INTERFACE_INFO(0x0e8d, 0xe0, 0x01, 0x01),
400 .driver_info = BTUSB_MEDIATEK },
401
Chris Chiu3a567b92020-11-10 11:40:10 +0800402 /* Additional MediaTek MT7615E Bluetooth devices */
403 { USB_DEVICE(0x13d3, 0x3560), .driver_info = BTUSB_MEDIATEK},
404
Daniel Drakea2698a92015-04-16 14:09:55 -0600405 /* Additional Realtek 8723AE Bluetooth devices */
406 { USB_DEVICE(0x0930, 0x021d), .driver_info = BTUSB_REALTEK },
407 { USB_DEVICE(0x13d3, 0x3394), .driver_info = BTUSB_REALTEK },
408
409 /* Additional Realtek 8723BE Bluetooth devices */
410 { USB_DEVICE(0x0489, 0xe085), .driver_info = BTUSB_REALTEK },
411 { USB_DEVICE(0x0489, 0xe08b), .driver_info = BTUSB_REALTEK },
412 { USB_DEVICE(0x13d3, 0x3410), .driver_info = BTUSB_REALTEK },
413 { USB_DEVICE(0x13d3, 0x3416), .driver_info = BTUSB_REALTEK },
414 { USB_DEVICE(0x13d3, 0x3459), .driver_info = BTUSB_REALTEK },
Dmitry Tunina81d72d2017-08-08 14:09:02 +0300415 { USB_DEVICE(0x13d3, 0x3494), .driver_info = BTUSB_REALTEK },
Daniel Drakea2698a92015-04-16 14:09:55 -0600416
Vicente Bergasa41e0792018-03-20 19:41:10 +0100417 /* Additional Realtek 8723BU Bluetooth devices */
418 { USB_DEVICE(0x7392, 0xa611), .driver_info = BTUSB_REALTEK },
419
Jian-Hong Pan66d99752018-05-21 18:09:20 +0800420 /* Additional Realtek 8723DE Bluetooth devices */
Jian-Hong Pan45ae68b2018-05-25 17:54:52 +0800421 { USB_DEVICE(0x0bda, 0xb009), .driver_info = BTUSB_REALTEK },
Jian-Hong Pan66d99752018-05-21 18:09:20 +0800422 { USB_DEVICE(0x2ff8, 0xb011), .driver_info = BTUSB_REALTEK },
423
Daniel Drakea2698a92015-04-16 14:09:55 -0600424 /* Additional Realtek 8821AE Bluetooth devices */
425 { USB_DEVICE(0x0b05, 0x17dc), .driver_info = BTUSB_REALTEK },
426 { USB_DEVICE(0x13d3, 0x3414), .driver_info = BTUSB_REALTEK },
427 { USB_DEVICE(0x13d3, 0x3458), .driver_info = BTUSB_REALTEK },
428 { USB_DEVICE(0x13d3, 0x3461), .driver_info = BTUSB_REALTEK },
429 { USB_DEVICE(0x13d3, 0x3462), .driver_info = BTUSB_REALTEK },
430
Larry Fingerfed03fe2018-02-11 12:24:32 -0600431 /* Additional Realtek 8822BE Bluetooth devices */
Artiom Vaskov1cd2fab2018-05-30 11:23:00 +0300432 { USB_DEVICE(0x13d3, 0x3526), .driver_info = BTUSB_REALTEK },
Larry Fingerfed03fe2018-02-11 12:24:32 -0600433 { USB_DEVICE(0x0b05, 0x185c), .driver_info = BTUSB_REALTEK },
434
Jian-Hong Pan6d0762b2019-09-03 17:10:42 +0800435 /* Additional Realtek 8822CE Bluetooth devices */
Max Chou73280f12020-11-05 18:54:48 +0800436 { USB_DEVICE(0x04ca, 0x4005), .driver_info = BTUSB_REALTEK |
437 BTUSB_WIDEBAND_SPEECH },
438 { USB_DEVICE(0x04c5, 0x161f), .driver_info = BTUSB_REALTEK |
439 BTUSB_WIDEBAND_SPEECH },
440 { USB_DEVICE(0x0b05, 0x18ef), .driver_info = BTUSB_REALTEK |
441 BTUSB_WIDEBAND_SPEECH },
442 { USB_DEVICE(0x13d3, 0x3548), .driver_info = BTUSB_REALTEK |
443 BTUSB_WIDEBAND_SPEECH },
444 { USB_DEVICE(0x13d3, 0x3549), .driver_info = BTUSB_REALTEK |
445 BTUSB_WIDEBAND_SPEECH },
446 { USB_DEVICE(0x13d3, 0x3553), .driver_info = BTUSB_REALTEK |
447 BTUSB_WIDEBAND_SPEECH },
448 { USB_DEVICE(0x13d3, 0x3555), .driver_info = BTUSB_REALTEK |
449 BTUSB_WIDEBAND_SPEECH },
450 { USB_DEVICE(0x2ff8, 0x3051), .driver_info = BTUSB_REALTEK |
451 BTUSB_WIDEBAND_SPEECH },
Julian Pidancetf4962972020-11-09 17:47:22 +0100452 { USB_DEVICE(0x1358, 0xc123), .driver_info = BTUSB_REALTEK |
453 BTUSB_WIDEBAND_SPEECH },
Cadel Watson24d6a6d2020-11-19 16:16:25 +1100454 { USB_DEVICE(0x0bda, 0xc123), .driver_info = BTUSB_REALTEK |
455 BTUSB_WIDEBAND_SPEECH },
Jian-Hong Pan6d0762b2019-09-03 17:10:42 +0800456
Peter Poklop4481c072015-08-15 20:47:09 +0200457 /* Silicon Wave based devices */
458 { USB_DEVICE(0x0c10, 0x0000), .driver_info = BTUSB_SWAVE },
459
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200460 { } /* Terminating entry */
461};
462
Hans de Goede1fdb9262018-02-20 09:06:18 +0100463/* The Bluetooth USB module build into some devices needs to be reset on resume,
464 * this is a problem with the platform (likely shutting off all power) not with
465 * the module itself. So we use a DMI list to match known broken platforms.
466 */
467static const struct dmi_system_id btusb_needs_reset_resume_table[] = {
468 {
Kai-Heng Feng0c6e5262018-03-01 13:42:52 +0800469 /* Dell OptiPlex 3060 (QCA ROME device 0cf3:e007) */
Hans de Goede1fdb9262018-02-20 09:06:18 +0100470 .matches = {
Kai-Heng Feng0c6e5262018-03-01 13:42:52 +0800471 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
472 DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 3060"),
Hans de Goede1fdb9262018-02-20 09:06:18 +0100473 },
474 },
Hans de Goede596b07a2018-04-26 20:52:06 +0200475 {
476 /* Dell XPS 9360 (QCA ROME device 0cf3:e300) */
477 .matches = {
478 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
479 DMI_MATCH(DMI_PRODUCT_NAME, "XPS 13 9360"),
480 },
481 },
Hans de Goede939bc6c2018-05-22 09:34:10 +0200482 {
483 /* Dell Inspiron 5565 (QCA ROME device 0cf3:e009) */
484 .matches = {
485 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
486 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5565"),
487 },
488 },
Hans de Goede1fdb9262018-02-20 09:06:18 +0100489 {}
490};
491
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200492#define BTUSB_MAX_ISOC_FRAMES 10
493
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200494#define BTUSB_INTR_RUNNING 0
495#define BTUSB_BULK_RUNNING 1
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200496#define BTUSB_ISOC_RUNNING 2
Oliver Neukum7bee5492009-08-24 23:44:59 +0200497#define BTUSB_SUSPENDING 3
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -0300498#define BTUSB_DID_ISO_RESUME 4
Marcel Holtmanncda0dd72015-01-26 21:33:48 -0800499#define BTUSB_BOOTLOADER 5
500#define BTUSB_DOWNLOADING 6
Marcel Holtmannce6bb922015-01-28 01:58:40 -0800501#define BTUSB_FIRMWARE_LOADED 7
Marcel Holtmanncda0dd72015-01-26 21:33:48 -0800502#define BTUSB_FIRMWARE_FAILED 8
Marcel Holtmannce6bb922015-01-28 01:58:40 -0800503#define BTUSB_BOOTING 9
Hans de Goede61f5ace2018-01-08 10:44:16 +0100504#define BTUSB_DIAG_RUNNING 10
505#define BTUSB_OOB_WAKE_ENABLED 11
Rajat Jaindc786b22019-01-24 15:28:14 -0800506#define BTUSB_HW_RESET_ACTIVE 12
Sean Wanga1c49c432019-06-02 08:02:48 +0800507#define BTUSB_TX_WAIT_VND_EVT 13
Alex Lu9e455242019-08-14 20:02:52 +0800508#define BTUSB_WAKEUP_DISABLE 14
Hilda Wu461f95f2020-06-30 21:09:40 +0800509#define BTUSB_USE_ALT1_FOR_WBS 15
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200510
511struct btusb_data {
512 struct hci_dev *hdev;
513 struct usb_device *udev;
Marcel Holtmann5fbcd262008-09-23 00:16:36 +0200514 struct usb_interface *intf;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200515 struct usb_interface *isoc;
Marcel Holtmann9d08f502015-10-08 20:23:08 +0200516 struct usb_interface *diag;
Marcel Holtmann459232f2017-10-24 19:42:45 +0200517 unsigned isoc_ifnum;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200518
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200519 unsigned long flags;
520
521 struct work_struct work;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200522 struct work_struct waker;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200523
Marcel Holtmann803b5832014-09-16 08:00:29 +0200524 struct usb_anchor deferred;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200525 struct usb_anchor tx_anchor;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200526 int tx_in_flight;
527 spinlock_t txlock;
528
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200529 struct usb_anchor intr_anchor;
530 struct usb_anchor bulk_anchor;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200531 struct usb_anchor isoc_anchor;
Marcel Holtmann9d08f502015-10-08 20:23:08 +0200532 struct usb_anchor diag_anchor;
Sean Wanga1c49c432019-06-02 08:02:48 +0800533 struct usb_anchor ctrl_anchor;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200534 spinlock_t rxlock;
535
536 struct sk_buff *evt_skb;
537 struct sk_buff *acl_skb;
538 struct sk_buff *sco_skb;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200539
540 struct usb_endpoint_descriptor *intr_ep;
541 struct usb_endpoint_descriptor *bulk_tx_ep;
542 struct usb_endpoint_descriptor *bulk_rx_ep;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200543 struct usb_endpoint_descriptor *isoc_tx_ep;
544 struct usb_endpoint_descriptor *isoc_rx_ep;
Marcel Holtmann9d08f502015-10-08 20:23:08 +0200545 struct usb_endpoint_descriptor *diag_tx_ep;
546 struct usb_endpoint_descriptor *diag_rx_ep;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200547
Rajat Jaindc786b22019-01-24 15:28:14 -0800548 struct gpio_desc *reset_gpio;
549
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100550 __u8 cmdreq_type;
Marcel Holtmann893ba542015-01-28 20:27:34 -0800551 __u8 cmdreq;
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100552
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100553 unsigned int sco_num;
Sathish Narasimmanbaac6272020-04-03 21:43:59 +0200554 unsigned int air_mode;
555 bool usb_alt6_packet_flow;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200556 int isoc_altsetting;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +0100557 int suspend_count;
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +0100558
Marcel Holtmann97307f52015-01-12 13:51:10 -0800559 int (*recv_event)(struct hci_dev *hdev, struct sk_buff *skb);
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +0100560 int (*recv_bulk)(struct btusb_data *data, void *buffer, int count);
Kim, Ben Young Taeace31982015-02-15 23:06:14 +0000561
562 int (*setup_on_usb)(struct hci_dev *hdev);
Rajat Jainfd913ef2017-02-01 14:24:09 -0800563
564 int oob_wake_irq; /* irq for out-of-band wake-on-bt */
Rajat Jaindc786b22019-01-24 15:28:14 -0800565 unsigned cmd_timeout_cnt;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200566};
567
Rajat Jaindc786b22019-01-24 15:28:14 -0800568static void btusb_intel_cmd_timeout(struct hci_dev *hdev)
569{
570 struct btusb_data *data = hci_get_drvdata(hdev);
571 struct gpio_desc *reset_gpio = data->reset_gpio;
572
573 if (++data->cmd_timeout_cnt < 5)
574 return;
575
576 if (!reset_gpio) {
577 bt_dev_err(hdev, "No way to reset. Ignoring and continuing");
578 return;
579 }
580
581 /*
582 * Toggle the hard reset line if the platform provides one. The reset
583 * is going to yank the device off the USB and then replug. So doing
584 * once is enough. The cleanup is handled correctly on the way out
585 * (standard USB disconnect), and the new device is detected cleanly
586 * and bound to the driver again like it should be.
587 */
588 if (test_and_set_bit(BTUSB_HW_RESET_ACTIVE, &data->flags)) {
589 bt_dev_err(hdev, "last reset failed? Not resetting again");
590 return;
591 }
592
593 bt_dev_err(hdev, "Initiating HW reset via gpio");
Rajat Jain2de66bb82019-01-28 15:08:09 -0800594 gpiod_set_value_cansleep(reset_gpio, 1);
595 msleep(100);
596 gpiod_set_value_cansleep(reset_gpio, 0);
Rajat Jaindc786b22019-01-24 15:28:14 -0800597}
598
Alex Lud7ef0d12019-09-05 10:36:31 +0800599static void btusb_rtl_cmd_timeout(struct hci_dev *hdev)
600{
601 struct btusb_data *data = hci_get_drvdata(hdev);
602 struct gpio_desc *reset_gpio = data->reset_gpio;
603
604 if (++data->cmd_timeout_cnt < 5)
605 return;
606
607 if (!reset_gpio) {
608 bt_dev_err(hdev, "No gpio to reset Realtek device, ignoring");
609 return;
610 }
611
612 /* Toggle the hard reset line. The Realtek device is going to
613 * yank itself off the USB and then replug. The cleanup is handled
614 * correctly on the way out (standard USB disconnect), and the new
615 * device is detected cleanly and bound to the driver again like
616 * it should be.
617 */
618 if (test_and_set_bit(BTUSB_HW_RESET_ACTIVE, &data->flags)) {
619 bt_dev_err(hdev, "last reset failed? Not resetting again");
620 return;
621 }
622
623 bt_dev_err(hdev, "Reset Realtek device via gpio");
Alex Lud7ef0d12019-09-05 10:36:31 +0800624 gpiod_set_value_cansleep(reset_gpio, 1);
Max Chou34682112019-11-27 11:01:07 +0800625 msleep(200);
626 gpiod_set_value_cansleep(reset_gpio, 0);
Alex Lud7ef0d12019-09-05 10:36:31 +0800627}
628
Abhishek Pandit-Subedi69335682020-06-24 11:11:44 -0700629static void btusb_qca_cmd_timeout(struct hci_dev *hdev)
630{
631 struct btusb_data *data = hci_get_drvdata(hdev);
632 int err;
633
634 if (++data->cmd_timeout_cnt < 5)
635 return;
636
637 bt_dev_err(hdev, "Multiple cmd timeouts seen. Resetting usb device.");
Abhishek Pandit-Subedib980d472020-06-25 16:26:27 -0700638 /* This is not an unbalanced PM reference since the device will reset */
Abhishek Pandit-Subedi69335682020-06-24 11:11:44 -0700639 err = usb_autopm_get_interface(data->intf);
640 if (!err)
641 usb_queue_reset_device(data->intf);
642 else
643 bt_dev_err(hdev, "Failed usb_autopm_get_interface with %d", err);
644}
645
Marcel Holtmann803b5832014-09-16 08:00:29 +0200646static inline void btusb_free_frags(struct btusb_data *data)
647{
648 unsigned long flags;
649
650 spin_lock_irqsave(&data->rxlock, flags);
651
652 kfree_skb(data->evt_skb);
653 data->evt_skb = NULL;
654
655 kfree_skb(data->acl_skb);
656 data->acl_skb = NULL;
657
658 kfree_skb(data->sco_skb);
659 data->sco_skb = NULL;
660
661 spin_unlock_irqrestore(&data->rxlock, flags);
662}
663
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200664static int btusb_recv_intr(struct btusb_data *data, void *buffer, int count)
665{
Marcel Holtmann803b5832014-09-16 08:00:29 +0200666 struct sk_buff *skb;
Sebastian Andrzej Siewiora5e50d52018-06-19 23:56:57 +0200667 unsigned long flags;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200668 int err = 0;
669
Sebastian Andrzej Siewiora5e50d52018-06-19 23:56:57 +0200670 spin_lock_irqsave(&data->rxlock, flags);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200671 skb = data->evt_skb;
672
673 while (count) {
674 int len;
675
676 if (!skb) {
677 skb = bt_skb_alloc(HCI_MAX_EVENT_SIZE, GFP_ATOMIC);
678 if (!skb) {
679 err = -ENOMEM;
680 break;
681 }
682
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100683 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
684 hci_skb_expect(skb) = HCI_EVENT_HDR_SIZE;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200685 }
686
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100687 len = min_t(uint, hci_skb_expect(skb), count);
Johannes Berg59ae1d12017-06-16 14:29:20 +0200688 skb_put_data(skb, buffer, len);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200689
690 count -= len;
691 buffer += len;
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100692 hci_skb_expect(skb) -= len;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200693
694 if (skb->len == HCI_EVENT_HDR_SIZE) {
695 /* Complete event header */
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100696 hci_skb_expect(skb) = hci_event_hdr(skb)->plen;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200697
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100698 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
Marcel Holtmann803b5832014-09-16 08:00:29 +0200699 kfree_skb(skb);
700 skb = NULL;
701
702 err = -EILSEQ;
703 break;
704 }
705 }
706
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100707 if (!hci_skb_expect(skb)) {
Marcel Holtmann803b5832014-09-16 08:00:29 +0200708 /* Complete frame */
Marcel Holtmann97307f52015-01-12 13:51:10 -0800709 data->recv_event(data->hdev, skb);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200710 skb = NULL;
711 }
712 }
713
714 data->evt_skb = skb;
Sebastian Andrzej Siewiora5e50d52018-06-19 23:56:57 +0200715 spin_unlock_irqrestore(&data->rxlock, flags);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200716
717 return err;
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200718}
719
720static int btusb_recv_bulk(struct btusb_data *data, void *buffer, int count)
721{
Marcel Holtmann803b5832014-09-16 08:00:29 +0200722 struct sk_buff *skb;
Sebastian Andrzej Siewiora5e50d52018-06-19 23:56:57 +0200723 unsigned long flags;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200724 int err = 0;
725
Sebastian Andrzej Siewiora5e50d52018-06-19 23:56:57 +0200726 spin_lock_irqsave(&data->rxlock, flags);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200727 skb = data->acl_skb;
728
729 while (count) {
730 int len;
731
732 if (!skb) {
733 skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
734 if (!skb) {
735 err = -ENOMEM;
736 break;
737 }
738
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100739 hci_skb_pkt_type(skb) = HCI_ACLDATA_PKT;
740 hci_skb_expect(skb) = HCI_ACL_HDR_SIZE;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200741 }
742
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100743 len = min_t(uint, hci_skb_expect(skb), count);
Johannes Berg59ae1d12017-06-16 14:29:20 +0200744 skb_put_data(skb, buffer, len);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200745
746 count -= len;
747 buffer += len;
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100748 hci_skb_expect(skb) -= len;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200749
750 if (skb->len == HCI_ACL_HDR_SIZE) {
751 __le16 dlen = hci_acl_hdr(skb)->dlen;
752
753 /* Complete ACL header */
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100754 hci_skb_expect(skb) = __le16_to_cpu(dlen);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200755
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100756 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
Marcel Holtmann803b5832014-09-16 08:00:29 +0200757 kfree_skb(skb);
758 skb = NULL;
759
760 err = -EILSEQ;
761 break;
762 }
763 }
764
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100765 if (!hci_skb_expect(skb)) {
Marcel Holtmann803b5832014-09-16 08:00:29 +0200766 /* Complete frame */
767 hci_recv_frame(data->hdev, skb);
768 skb = NULL;
769 }
770 }
771
772 data->acl_skb = skb;
Sebastian Andrzej Siewiora5e50d52018-06-19 23:56:57 +0200773 spin_unlock_irqrestore(&data->rxlock, flags);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200774
775 return err;
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200776}
777
778static int btusb_recv_isoc(struct btusb_data *data, void *buffer, int count)
779{
Marcel Holtmann803b5832014-09-16 08:00:29 +0200780 struct sk_buff *skb;
Sebastian Andrzej Siewiora5e50d52018-06-19 23:56:57 +0200781 unsigned long flags;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200782 int err = 0;
783
Sebastian Andrzej Siewiora5e50d52018-06-19 23:56:57 +0200784 spin_lock_irqsave(&data->rxlock, flags);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200785 skb = data->sco_skb;
786
787 while (count) {
788 int len;
789
790 if (!skb) {
791 skb = bt_skb_alloc(HCI_MAX_SCO_SIZE, GFP_ATOMIC);
792 if (!skb) {
793 err = -ENOMEM;
794 break;
795 }
796
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100797 hci_skb_pkt_type(skb) = HCI_SCODATA_PKT;
798 hci_skb_expect(skb) = HCI_SCO_HDR_SIZE;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200799 }
800
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100801 len = min_t(uint, hci_skb_expect(skb), count);
Johannes Berg59ae1d12017-06-16 14:29:20 +0200802 skb_put_data(skb, buffer, len);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200803
804 count -= len;
805 buffer += len;
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100806 hci_skb_expect(skb) -= len;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200807
808 if (skb->len == HCI_SCO_HDR_SIZE) {
809 /* Complete SCO header */
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100810 hci_skb_expect(skb) = hci_sco_hdr(skb)->dlen;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200811
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100812 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
Marcel Holtmann803b5832014-09-16 08:00:29 +0200813 kfree_skb(skb);
814 skb = NULL;
815
816 err = -EILSEQ;
817 break;
818 }
819 }
820
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100821 if (!hci_skb_expect(skb)) {
Marcel Holtmann803b5832014-09-16 08:00:29 +0200822 /* Complete frame */
823 hci_recv_frame(data->hdev, skb);
824 skb = NULL;
825 }
826 }
827
828 data->sco_skb = skb;
Sebastian Andrzej Siewiora5e50d52018-06-19 23:56:57 +0200829 spin_unlock_irqrestore(&data->rxlock, flags);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200830
831 return err;
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200832}
833
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200834static void btusb_intr_complete(struct urb *urb)
835{
836 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +0100837 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200838 int err;
839
Marcel Holtmann89e75332014-09-16 04:44:50 +0200840 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
841 urb->actual_length);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200842
843 if (!test_bit(HCI_RUNNING, &hdev->flags))
844 return;
845
846 if (urb->status == 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200847 hdev->stat.byte_rx += urb->actual_length;
848
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200849 if (btusb_recv_intr(data, urb->transfer_buffer,
850 urb->actual_length) < 0) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +0100851 bt_dev_err(hdev, "corrupted event packet");
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200852 hdev->stat.err_rx++;
853 }
Champion Chen85560c42014-09-06 14:06:08 -0500854 } else if (urb->status == -ENOENT) {
855 /* Avoid suspend failed when usb_kill_urb */
856 return;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200857 }
858
859 if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
860 return;
861
Oliver Neukum7bee5492009-08-24 23:44:59 +0200862 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200863 usb_anchor_urb(urb, &data->intr_anchor);
864
865 err = usb_submit_urb(urb, GFP_ATOMIC);
866 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200867 /* -EPERM: urb is being killed;
Derek Robsond98422c2017-07-22 13:47:07 +1200868 * -ENODEV: device got disconnected
869 */
Paul Bolle4935f1c2011-08-09 17:16:28 +0200870 if (err != -EPERM && err != -ENODEV)
Marcel Holtmann2064ee32017-10-30 10:42:59 +0100871 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
872 urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200873 usb_unanchor_urb(urb);
874 }
875}
876
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100877static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200878{
David Herrmann155961e2012-02-09 21:58:32 +0100879 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200880 struct urb *urb;
881 unsigned char *buf;
882 unsigned int pipe;
883 int err, size;
884
885 BT_DBG("%s", hdev->name);
886
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200887 if (!data->intr_ep)
888 return -ENODEV;
889
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100890 urb = usb_alloc_urb(0, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200891 if (!urb)
892 return -ENOMEM;
893
894 size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
895
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100896 buf = kmalloc(size, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200897 if (!buf) {
898 usb_free_urb(urb);
899 return -ENOMEM;
900 }
901
902 pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
903
904 usb_fill_int_urb(urb, data->udev, pipe, buf, size,
Marcel Holtmann89e75332014-09-16 04:44:50 +0200905 btusb_intr_complete, hdev, data->intr_ep->bInterval);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200906
907 urb->transfer_flags |= URB_FREE_BUFFER;
908
909 usb_anchor_urb(urb, &data->intr_anchor);
910
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100911 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200912 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +0200913 if (err != -EPERM && err != -ENODEV)
Marcel Holtmann2064ee32017-10-30 10:42:59 +0100914 bt_dev_err(hdev, "urb %p submission failed (%d)",
915 urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200916 usb_unanchor_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200917 }
918
919 usb_free_urb(urb);
920
921 return err;
922}
923
924static void btusb_bulk_complete(struct urb *urb)
925{
926 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +0100927 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200928 int err;
929
Marcel Holtmann89e75332014-09-16 04:44:50 +0200930 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
931 urb->actual_length);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200932
933 if (!test_bit(HCI_RUNNING, &hdev->flags))
934 return;
935
936 if (urb->status == 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200937 hdev->stat.byte_rx += urb->actual_length;
938
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +0100939 if (data->recv_bulk(data, urb->transfer_buffer,
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200940 urb->actual_length) < 0) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +0100941 bt_dev_err(hdev, "corrupted ACL packet");
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200942 hdev->stat.err_rx++;
943 }
Champion Chen85560c42014-09-06 14:06:08 -0500944 } else if (urb->status == -ENOENT) {
945 /* Avoid suspend failed when usb_kill_urb */
946 return;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200947 }
948
949 if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
950 return;
951
952 usb_anchor_urb(urb, &data->bulk_anchor);
Oliver Neukum652fd782009-12-16 19:23:43 +0100953 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200954
955 err = usb_submit_urb(urb, GFP_ATOMIC);
956 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200957 /* -EPERM: urb is being killed;
Derek Robsond98422c2017-07-22 13:47:07 +1200958 * -ENODEV: device got disconnected
959 */
Paul Bolle4935f1c2011-08-09 17:16:28 +0200960 if (err != -EPERM && err != -ENODEV)
Marcel Holtmann2064ee32017-10-30 10:42:59 +0100961 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
962 urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200963 usb_unanchor_urb(urb);
964 }
965}
966
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100967static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200968{
David Herrmann155961e2012-02-09 21:58:32 +0100969 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200970 struct urb *urb;
971 unsigned char *buf;
972 unsigned int pipe;
Vikram Kandukuri290ba202009-07-02 14:31:59 +0530973 int err, size = HCI_MAX_FRAME_SIZE;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200974
975 BT_DBG("%s", hdev->name);
976
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200977 if (!data->bulk_rx_ep)
978 return -ENODEV;
979
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100980 urb = usb_alloc_urb(0, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200981 if (!urb)
982 return -ENOMEM;
983
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100984 buf = kmalloc(size, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200985 if (!buf) {
986 usb_free_urb(urb);
987 return -ENOMEM;
988 }
989
990 pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
991
Marcel Holtmann89e75332014-09-16 04:44:50 +0200992 usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
993 btusb_bulk_complete, hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200994
995 urb->transfer_flags |= URB_FREE_BUFFER;
996
Oliver Neukum7bee5492009-08-24 23:44:59 +0200997 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200998 usb_anchor_urb(urb, &data->bulk_anchor);
999
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001000 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001001 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +02001002 if (err != -EPERM && err != -ENODEV)
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001003 bt_dev_err(hdev, "urb %p submission failed (%d)",
1004 urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001005 usb_unanchor_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001006 }
1007
1008 usb_free_urb(urb);
1009
1010 return err;
1011}
1012
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001013static void btusb_isoc_complete(struct urb *urb)
1014{
1015 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +01001016 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001017 int i, err;
1018
Marcel Holtmann89e75332014-09-16 04:44:50 +02001019 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1020 urb->actual_length);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001021
1022 if (!test_bit(HCI_RUNNING, &hdev->flags))
1023 return;
1024
1025 if (urb->status == 0) {
1026 for (i = 0; i < urb->number_of_packets; i++) {
1027 unsigned int offset = urb->iso_frame_desc[i].offset;
1028 unsigned int length = urb->iso_frame_desc[i].actual_length;
1029
1030 if (urb->iso_frame_desc[i].status)
1031 continue;
1032
1033 hdev->stat.byte_rx += length;
1034
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +02001035 if (btusb_recv_isoc(data, urb->transfer_buffer + offset,
1036 length) < 0) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001037 bt_dev_err(hdev, "corrupted SCO packet");
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001038 hdev->stat.err_rx++;
1039 }
1040 }
Champion Chen85560c42014-09-06 14:06:08 -05001041 } else if (urb->status == -ENOENT) {
1042 /* Avoid suspend failed when usb_kill_urb */
1043 return;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001044 }
1045
1046 if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags))
1047 return;
1048
1049 usb_anchor_urb(urb, &data->isoc_anchor);
1050
1051 err = usb_submit_urb(urb, GFP_ATOMIC);
1052 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +02001053 /* -EPERM: urb is being killed;
Derek Robsond98422c2017-07-22 13:47:07 +12001054 * -ENODEV: device got disconnected
1055 */
Paul Bolle4935f1c2011-08-09 17:16:28 +02001056 if (err != -EPERM && err != -ENODEV)
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001057 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
1058 urb, -err);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001059 usb_unanchor_urb(urb);
1060 }
1061}
1062
Sathish Narasimmanbaac6272020-04-03 21:43:59 +02001063static inline void __fill_isoc_descriptor_msbc(struct urb *urb, int len,
1064 int mtu, struct btusb_data *data)
1065{
1066 int i, offset = 0;
1067 unsigned int interval;
1068
1069 BT_DBG("len %d mtu %d", len, mtu);
1070
1071 /* For mSBC ALT 6 setting the host will send the packet at continuous
1072 * flow. As per core spec 5, vol 4, part B, table 2.1. For ALT setting
1073 * 6 the HCI PACKET INTERVAL should be 7.5ms for every usb packets.
1074 * To maintain the rate we send 63bytes of usb packets alternatively for
1075 * 7ms and 8ms to maintain the rate as 7.5ms.
1076 */
1077 if (data->usb_alt6_packet_flow) {
1078 interval = 7;
1079 data->usb_alt6_packet_flow = false;
1080 } else {
1081 interval = 6;
1082 data->usb_alt6_packet_flow = true;
1083 }
1084
1085 for (i = 0; i < interval; i++) {
1086 urb->iso_frame_desc[i].offset = offset;
1087 urb->iso_frame_desc[i].length = offset;
1088 }
1089
1090 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
1091 urb->iso_frame_desc[i].offset = offset;
1092 urb->iso_frame_desc[i].length = len;
1093 i++;
1094 }
1095
1096 urb->number_of_packets = i;
1097}
1098
Jesper Juhl42b16b32011-01-17 00:09:38 +01001099static inline void __fill_isoc_descriptor(struct urb *urb, int len, int mtu)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001100{
1101 int i, offset = 0;
1102
1103 BT_DBG("len %d mtu %d", len, mtu);
1104
1105 for (i = 0; i < BTUSB_MAX_ISOC_FRAMES && len >= mtu;
1106 i++, offset += mtu, len -= mtu) {
1107 urb->iso_frame_desc[i].offset = offset;
1108 urb->iso_frame_desc[i].length = mtu;
1109 }
1110
1111 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
1112 urb->iso_frame_desc[i].offset = offset;
1113 urb->iso_frame_desc[i].length = len;
1114 i++;
1115 }
1116
1117 urb->number_of_packets = i;
1118}
1119
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001120static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001121{
David Herrmann155961e2012-02-09 21:58:32 +01001122 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001123 struct urb *urb;
1124 unsigned char *buf;
1125 unsigned int pipe;
1126 int err, size;
1127
1128 BT_DBG("%s", hdev->name);
1129
1130 if (!data->isoc_rx_ep)
1131 return -ENODEV;
1132
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001133 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001134 if (!urb)
1135 return -ENOMEM;
1136
1137 size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) *
1138 BTUSB_MAX_ISOC_FRAMES;
1139
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001140 buf = kmalloc(size, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001141 if (!buf) {
1142 usb_free_urb(urb);
1143 return -ENOMEM;
1144 }
1145
1146 pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress);
1147
Bing Zhaofa0fb932011-12-20 18:19:00 -08001148 usb_fill_int_urb(urb, data->udev, pipe, buf, size, btusb_isoc_complete,
Marcel Holtmann89e75332014-09-16 04:44:50 +02001149 hdev, data->isoc_rx_ep->bInterval);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001150
Marcel Holtmann89e75332014-09-16 04:44:50 +02001151 urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001152
1153 __fill_isoc_descriptor(urb, size,
Marcel Holtmann89e75332014-09-16 04:44:50 +02001154 le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize));
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001155
1156 usb_anchor_urb(urb, &data->isoc_anchor);
1157
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001158 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001159 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +02001160 if (err != -EPERM && err != -ENODEV)
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001161 bt_dev_err(hdev, "urb %p submission failed (%d)",
1162 urb, -err);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001163 usb_unanchor_urb(urb);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001164 }
1165
1166 usb_free_urb(urb);
1167
1168 return err;
1169}
1170
Marcel Holtmann9d08f502015-10-08 20:23:08 +02001171static void btusb_diag_complete(struct urb *urb)
1172{
1173 struct hci_dev *hdev = urb->context;
1174 struct btusb_data *data = hci_get_drvdata(hdev);
1175 int err;
1176
1177 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1178 urb->actual_length);
1179
1180 if (urb->status == 0) {
1181 struct sk_buff *skb;
1182
1183 skb = bt_skb_alloc(urb->actual_length, GFP_ATOMIC);
1184 if (skb) {
Johannes Berg59ae1d12017-06-16 14:29:20 +02001185 skb_put_data(skb, urb->transfer_buffer,
1186 urb->actual_length);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02001187 hci_recv_diag(hdev, skb);
1188 }
1189 } else if (urb->status == -ENOENT) {
1190 /* Avoid suspend failed when usb_kill_urb */
1191 return;
1192 }
1193
1194 if (!test_bit(BTUSB_DIAG_RUNNING, &data->flags))
1195 return;
1196
1197 usb_anchor_urb(urb, &data->diag_anchor);
1198 usb_mark_last_busy(data->udev);
1199
1200 err = usb_submit_urb(urb, GFP_ATOMIC);
1201 if (err < 0) {
1202 /* -EPERM: urb is being killed;
Derek Robsond98422c2017-07-22 13:47:07 +12001203 * -ENODEV: device got disconnected
1204 */
Marcel Holtmann9d08f502015-10-08 20:23:08 +02001205 if (err != -EPERM && err != -ENODEV)
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001206 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
1207 urb, -err);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02001208 usb_unanchor_urb(urb);
1209 }
1210}
1211
1212static int btusb_submit_diag_urb(struct hci_dev *hdev, gfp_t mem_flags)
1213{
1214 struct btusb_data *data = hci_get_drvdata(hdev);
1215 struct urb *urb;
1216 unsigned char *buf;
1217 unsigned int pipe;
1218 int err, size = HCI_MAX_FRAME_SIZE;
1219
1220 BT_DBG("%s", hdev->name);
1221
1222 if (!data->diag_rx_ep)
1223 return -ENODEV;
1224
1225 urb = usb_alloc_urb(0, mem_flags);
1226 if (!urb)
1227 return -ENOMEM;
1228
1229 buf = kmalloc(size, mem_flags);
1230 if (!buf) {
1231 usb_free_urb(urb);
1232 return -ENOMEM;
1233 }
1234
1235 pipe = usb_rcvbulkpipe(data->udev, data->diag_rx_ep->bEndpointAddress);
1236
1237 usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
1238 btusb_diag_complete, hdev);
1239
1240 urb->transfer_flags |= URB_FREE_BUFFER;
1241
1242 usb_mark_last_busy(data->udev);
1243 usb_anchor_urb(urb, &data->diag_anchor);
1244
1245 err = usb_submit_urb(urb, mem_flags);
1246 if (err < 0) {
1247 if (err != -EPERM && err != -ENODEV)
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001248 bt_dev_err(hdev, "urb %p submission failed (%d)",
1249 urb, -err);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02001250 usb_unanchor_urb(urb);
1251 }
1252
1253 usb_free_urb(urb);
1254
1255 return err;
1256}
1257
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001258static void btusb_tx_complete(struct urb *urb)
1259{
1260 struct sk_buff *skb = urb->context;
Marcel Holtmann89e75332014-09-16 04:44:50 +02001261 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
David Herrmann155961e2012-02-09 21:58:32 +01001262 struct btusb_data *data = hci_get_drvdata(hdev);
Sebastian Andrzej Siewiora5e50d52018-06-19 23:56:57 +02001263 unsigned long flags;
Oliver Neukum7bee5492009-08-24 23:44:59 +02001264
Marcel Holtmann89e75332014-09-16 04:44:50 +02001265 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1266 urb->actual_length);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001267
1268 if (!test_bit(HCI_RUNNING, &hdev->flags))
1269 goto done;
1270
1271 if (!urb->status)
1272 hdev->stat.byte_tx += urb->transfer_buffer_length;
1273 else
1274 hdev->stat.err_tx++;
1275
1276done:
Sebastian Andrzej Siewiora5e50d52018-06-19 23:56:57 +02001277 spin_lock_irqsave(&data->txlock, flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001278 data->tx_in_flight--;
Sebastian Andrzej Siewiora5e50d52018-06-19 23:56:57 +02001279 spin_unlock_irqrestore(&data->txlock, flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001280
1281 kfree(urb->setup_packet);
1282
1283 kfree_skb(skb);
1284}
1285
1286static void btusb_isoc_tx_complete(struct urb *urb)
1287{
1288 struct sk_buff *skb = urb->context;
Marcel Holtmann89e75332014-09-16 04:44:50 +02001289 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001290
Marcel Holtmann89e75332014-09-16 04:44:50 +02001291 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1292 urb->actual_length);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001293
1294 if (!test_bit(HCI_RUNNING, &hdev->flags))
1295 goto done;
1296
1297 if (!urb->status)
1298 hdev->stat.byte_tx += urb->transfer_buffer_length;
1299 else
1300 hdev->stat.err_tx++;
1301
1302done:
1303 kfree(urb->setup_packet);
1304
1305 kfree_skb(skb);
1306}
1307
1308static int btusb_open(struct hci_dev *hdev)
1309{
David Herrmann155961e2012-02-09 21:58:32 +01001310 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001311 int err;
1312
1313 BT_DBG("%s", hdev->name);
1314
Ethan Hsiehc7e163f2016-10-07 12:06:42 +08001315 err = usb_autopm_get_interface(data->intf);
1316 if (err < 0)
1317 return err;
1318
Kim, Ben Young Taeace31982015-02-15 23:06:14 +00001319 /* Patching USB firmware files prior to starting any URBs of HCI path
1320 * It is more safe to use USB bulk channel for downloading USB patch
1321 */
1322 if (data->setup_on_usb) {
1323 err = data->setup_on_usb(hdev);
Marcel Holtmanneb500422015-04-16 23:15:50 +02001324 if (err < 0)
Oliver Neukum3d44a6f2019-11-14 16:01:18 +01001325 goto setup_fail;
Kim, Ben Young Taeace31982015-02-15 23:06:14 +00001326 }
1327
Oliver Neukum7bee5492009-08-24 23:44:59 +02001328 data->intf->needs_remote_wakeup = 1;
1329
Alex Lu9e455242019-08-14 20:02:52 +08001330 /* Disable device remote wakeup when host is suspended
1331 * For Realtek chips, global suspend without
1332 * SET_FEATURE (DEVICE_REMOTE_WAKEUP) can save more power in device.
1333 */
1334 if (test_bit(BTUSB_WAKEUP_DISABLE, &data->flags))
1335 device_wakeup_disable(&data->udev->dev);
1336
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001337 if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +02001338 goto done;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001339
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001340 err = btusb_submit_intr_urb(hdev, GFP_KERNEL);
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001341 if (err < 0)
1342 goto failed;
1343
1344 err = btusb_submit_bulk_urb(hdev, GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001345 if (err < 0) {
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001346 usb_kill_anchored_urbs(&data->intr_anchor);
1347 goto failed;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001348 }
1349
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001350 set_bit(BTUSB_BULK_RUNNING, &data->flags);
1351 btusb_submit_bulk_urb(hdev, GFP_KERNEL);
1352
Marcel Holtmann9d08f502015-10-08 20:23:08 +02001353 if (data->diag) {
1354 if (!btusb_submit_diag_urb(hdev, GFP_KERNEL))
1355 set_bit(BTUSB_DIAG_RUNNING, &data->flags);
1356 }
1357
Oliver Neukum7bee5492009-08-24 23:44:59 +02001358done:
1359 usb_autopm_put_interface(data->intf);
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001360 return 0;
1361
1362failed:
1363 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Oliver Neukum3d44a6f2019-11-14 16:01:18 +01001364setup_fail:
Oliver Neukum7bee5492009-08-24 23:44:59 +02001365 usb_autopm_put_interface(data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001366 return err;
1367}
1368
Oliver Neukum7bee5492009-08-24 23:44:59 +02001369static void btusb_stop_traffic(struct btusb_data *data)
1370{
1371 usb_kill_anchored_urbs(&data->intr_anchor);
1372 usb_kill_anchored_urbs(&data->bulk_anchor);
1373 usb_kill_anchored_urbs(&data->isoc_anchor);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02001374 usb_kill_anchored_urbs(&data->diag_anchor);
Sean Wanga1c49c432019-06-02 08:02:48 +08001375 usb_kill_anchored_urbs(&data->ctrl_anchor);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001376}
1377
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001378static int btusb_close(struct hci_dev *hdev)
1379{
David Herrmann155961e2012-02-09 21:58:32 +01001380 struct btusb_data *data = hci_get_drvdata(hdev);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001381 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001382
1383 BT_DBG("%s", hdev->name);
1384
Marcel Holtmanne8c3c3d2008-09-23 00:16:36 +02001385 cancel_work_sync(&data->work);
Linus Torvalds404291a2009-11-11 13:32:29 -08001386 cancel_work_sync(&data->waker);
Marcel Holtmanne8c3c3d2008-09-23 00:16:36 +02001387
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001388 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001389 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001390 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02001391 clear_bit(BTUSB_DIAG_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001392
1393 btusb_stop_traffic(data);
Marcel Holtmann803b5832014-09-16 08:00:29 +02001394 btusb_free_frags(data);
1395
Oliver Neukum7bee5492009-08-24 23:44:59 +02001396 err = usb_autopm_get_interface(data->intf);
1397 if (err < 0)
Oliver Neukum7b8e2c12009-11-13 14:26:23 +01001398 goto failed;
Oliver Neukum7bee5492009-08-24 23:44:59 +02001399
1400 data->intf->needs_remote_wakeup = 0;
Alex Lu9e455242019-08-14 20:02:52 +08001401
1402 /* Enable remote wake up for auto-suspend */
1403 if (test_bit(BTUSB_WAKEUP_DISABLE, &data->flags))
1404 data->intf->needs_remote_wakeup = 1;
1405
Oliver Neukum7bee5492009-08-24 23:44:59 +02001406 usb_autopm_put_interface(data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001407
Oliver Neukum7b8e2c12009-11-13 14:26:23 +01001408failed:
1409 usb_scuttle_anchored_urbs(&data->deferred);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001410 return 0;
1411}
1412
1413static int btusb_flush(struct hci_dev *hdev)
1414{
David Herrmann155961e2012-02-09 21:58:32 +01001415 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001416
1417 BT_DBG("%s", hdev->name);
1418
1419 usb_kill_anchored_urbs(&data->tx_anchor);
Marcel Holtmann803b5832014-09-16 08:00:29 +02001420 btusb_free_frags(data);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001421
1422 return 0;
1423}
1424
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001425static struct urb *alloc_ctrl_urb(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001426{
David Herrmann155961e2012-02-09 21:58:32 +01001427 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001428 struct usb_ctrlrequest *dr;
1429 struct urb *urb;
1430 unsigned int pipe;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001431
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001432 urb = usb_alloc_urb(0, GFP_KERNEL);
1433 if (!urb)
1434 return ERR_PTR(-ENOMEM);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001435
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001436 dr = kmalloc(sizeof(*dr), GFP_KERNEL);
1437 if (!dr) {
1438 usb_free_urb(urb);
1439 return ERR_PTR(-ENOMEM);
1440 }
1441
1442 dr->bRequestType = data->cmdreq_type;
Marcel Holtmann893ba542015-01-28 20:27:34 -08001443 dr->bRequest = data->cmdreq;
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001444 dr->wIndex = 0;
1445 dr->wValue = 0;
1446 dr->wLength = __cpu_to_le16(skb->len);
1447
1448 pipe = usb_sndctrlpipe(data->udev, 0x00);
1449
Marcel Holtmann89e75332014-09-16 04:44:50 +02001450 usb_fill_control_urb(urb, data->udev, pipe, (void *)dr,
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001451 skb->data, skb->len, btusb_tx_complete, skb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001452
Marcel Holtmann89e75332014-09-16 04:44:50 +02001453 skb->dev = (void *)hdev;
Marcel Holtmann7bd8f092013-10-11 06:19:18 -07001454
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001455 return urb;
1456}
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001457
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001458static struct urb *alloc_bulk_urb(struct hci_dev *hdev, struct sk_buff *skb)
1459{
1460 struct btusb_data *data = hci_get_drvdata(hdev);
1461 struct urb *urb;
1462 unsigned int pipe;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001463
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001464 if (!data->bulk_tx_ep)
1465 return ERR_PTR(-ENODEV);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001466
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001467 urb = usb_alloc_urb(0, GFP_KERNEL);
1468 if (!urb)
1469 return ERR_PTR(-ENOMEM);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001470
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001471 pipe = usb_sndbulkpipe(data->udev, data->bulk_tx_ep->bEndpointAddress);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001472
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001473 usb_fill_bulk_urb(urb, data->udev, pipe,
1474 skb->data, skb->len, btusb_tx_complete, skb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001475
Marcel Holtmann89e75332014-09-16 04:44:50 +02001476 skb->dev = (void *)hdev;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001477
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001478 return urb;
1479}
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001480
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001481static struct urb *alloc_isoc_urb(struct hci_dev *hdev, struct sk_buff *skb)
1482{
1483 struct btusb_data *data = hci_get_drvdata(hdev);
1484 struct urb *urb;
1485 unsigned int pipe;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001486
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001487 if (!data->isoc_tx_ep)
1488 return ERR_PTR(-ENODEV);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001489
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001490 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_KERNEL);
1491 if (!urb)
1492 return ERR_PTR(-ENOMEM);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001493
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001494 pipe = usb_sndisocpipe(data->udev, data->isoc_tx_ep->bEndpointAddress);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001495
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001496 usb_fill_int_urb(urb, data->udev, pipe,
1497 skb->data, skb->len, btusb_isoc_tx_complete,
1498 skb, data->isoc_tx_ep->bInterval);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001499
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001500 urb->transfer_flags = URB_ISO_ASAP;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001501
Sathish Narasimmanbaac6272020-04-03 21:43:59 +02001502 if (data->isoc_altsetting == 6)
1503 __fill_isoc_descriptor_msbc(urb, skb->len,
1504 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize),
1505 data);
1506 else
1507 __fill_isoc_descriptor(urb, skb->len,
1508 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
Marcel Holtmann89e75332014-09-16 04:44:50 +02001509 skb->dev = (void *)hdev;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001510
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001511 return urb;
1512}
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001513
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001514static int submit_tx_urb(struct hci_dev *hdev, struct urb *urb)
1515{
1516 struct btusb_data *data = hci_get_drvdata(hdev);
1517 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001518
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001519 usb_anchor_urb(urb, &data->tx_anchor);
1520
Johan Hedberge9753ef2014-09-14 08:49:34 +03001521 err = usb_submit_urb(urb, GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001522 if (err < 0) {
Paul Bolle5a9b80e2011-10-09 12:12:16 +02001523 if (err != -EPERM && err != -ENODEV)
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001524 bt_dev_err(hdev, "urb %p submission failed (%d)",
1525 urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001526 kfree(urb->setup_packet);
1527 usb_unanchor_urb(urb);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001528 } else {
1529 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001530 }
1531
Cong Wang54a8a792011-11-22 09:32:57 +08001532 usb_free_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001533 return err;
1534}
1535
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001536static int submit_or_queue_tx_urb(struct hci_dev *hdev, struct urb *urb)
1537{
1538 struct btusb_data *data = hci_get_drvdata(hdev);
1539 unsigned long flags;
1540 bool suspending;
1541
1542 spin_lock_irqsave(&data->txlock, flags);
1543 suspending = test_bit(BTUSB_SUSPENDING, &data->flags);
1544 if (!suspending)
1545 data->tx_in_flight++;
1546 spin_unlock_irqrestore(&data->txlock, flags);
1547
1548 if (!suspending)
1549 return submit_tx_urb(hdev, urb);
1550
1551 usb_anchor_urb(urb, &data->deferred);
1552 schedule_work(&data->waker);
1553
1554 usb_free_urb(urb);
1555 return 0;
1556}
1557
1558static int btusb_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
1559{
1560 struct urb *urb;
1561
1562 BT_DBG("%s", hdev->name);
1563
Marcel Holtmann618e8bc2015-11-05 07:33:56 +01001564 switch (hci_skb_pkt_type(skb)) {
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001565 case HCI_COMMAND_PKT:
1566 urb = alloc_ctrl_urb(hdev, skb);
1567 if (IS_ERR(urb))
1568 return PTR_ERR(urb);
1569
1570 hdev->stat.cmd_tx++;
1571 return submit_or_queue_tx_urb(hdev, urb);
1572
1573 case HCI_ACLDATA_PKT:
1574 urb = alloc_bulk_urb(hdev, skb);
1575 if (IS_ERR(urb))
1576 return PTR_ERR(urb);
1577
1578 hdev->stat.acl_tx++;
1579 return submit_or_queue_tx_urb(hdev, urb);
1580
1581 case HCI_SCODATA_PKT:
1582 if (hci_conn_num(hdev, SCO_LINK) < 1)
1583 return -ENODEV;
1584
1585 urb = alloc_isoc_urb(hdev, skb);
1586 if (IS_ERR(urb))
1587 return PTR_ERR(urb);
1588
1589 hdev->stat.sco_tx++;
1590 return submit_tx_urb(hdev, urb);
1591 }
1592
1593 return -EILSEQ;
1594}
1595
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001596static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
1597{
David Herrmann155961e2012-02-09 21:58:32 +01001598 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001599
1600 BT_DBG("%s evt %d", hdev->name, evt);
1601
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001602 if (hci_conn_num(hdev, SCO_LINK) != data->sco_num) {
1603 data->sco_num = hci_conn_num(hdev, SCO_LINK);
Sathish Narasimmanbaac6272020-04-03 21:43:59 +02001604 data->air_mode = evt;
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001605 schedule_work(&data->work);
Marcel Holtmanna780efa2008-11-30 12:17:12 +01001606 }
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001607}
1608
Jesper Juhl42b16b32011-01-17 00:09:38 +01001609static inline int __set_isoc_interface(struct hci_dev *hdev, int altsetting)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001610{
David Herrmann155961e2012-02-09 21:58:32 +01001611 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001612 struct usb_interface *intf = data->isoc;
1613 struct usb_endpoint_descriptor *ep_desc;
1614 int i, err;
1615
1616 if (!data->isoc)
1617 return -ENODEV;
1618
Marcel Holtmann459232f2017-10-24 19:42:45 +02001619 err = usb_set_interface(data->udev, data->isoc_ifnum, altsetting);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001620 if (err < 0) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001621 bt_dev_err(hdev, "setting interface failed (%d)", -err);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001622 return err;
1623 }
1624
1625 data->isoc_altsetting = altsetting;
1626
1627 data->isoc_tx_ep = NULL;
1628 data->isoc_rx_ep = NULL;
1629
1630 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
1631 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
1632
1633 if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) {
1634 data->isoc_tx_ep = ep_desc;
1635 continue;
1636 }
1637
1638 if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) {
1639 data->isoc_rx_ep = ep_desc;
1640 continue;
1641 }
1642 }
1643
1644 if (!data->isoc_tx_ep || !data->isoc_rx_ep) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001645 bt_dev_err(hdev, "invalid SCO descriptors");
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001646 return -ENODEV;
1647 }
1648
1649 return 0;
1650}
1651
Sathish Narasimmanbaac6272020-04-03 21:43:59 +02001652static int btusb_switch_alt_setting(struct hci_dev *hdev, int new_alts)
1653{
1654 struct btusb_data *data = hci_get_drvdata(hdev);
1655 int err;
1656
1657 if (data->isoc_altsetting != new_alts) {
1658 unsigned long flags;
1659
1660 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1661 usb_kill_anchored_urbs(&data->isoc_anchor);
1662
1663 /* When isochronous alternate setting needs to be
1664 * changed, because SCO connection has been added
1665 * or removed, a packet fragment may be left in the
1666 * reassembling state. This could lead to wrongly
1667 * assembled fragments.
1668 *
1669 * Clear outstanding fragment when selecting a new
1670 * alternate setting.
1671 */
1672 spin_lock_irqsave(&data->rxlock, flags);
1673 kfree_skb(data->sco_skb);
1674 data->sco_skb = NULL;
1675 spin_unlock_irqrestore(&data->rxlock, flags);
1676
1677 err = __set_isoc_interface(hdev, new_alts);
1678 if (err < 0)
1679 return err;
1680 }
1681
1682 if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
1683 if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0)
1684 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1685 else
1686 btusb_submit_isoc_urb(hdev, GFP_KERNEL);
1687 }
1688
1689 return 0;
1690}
1691
1692static struct usb_host_interface *btusb_find_altsetting(struct btusb_data *data,
1693 int alt)
1694{
1695 struct usb_interface *intf = data->isoc;
1696 int i;
1697
1698 BT_DBG("Looking for Alt no :%d", alt);
1699
Sathish Narasimmanfcd156ee2020-04-08 10:57:03 +05301700 if (!intf)
1701 return NULL;
1702
Sathish Narasimmanbaac6272020-04-03 21:43:59 +02001703 for (i = 0; i < intf->num_altsetting; i++) {
1704 if (intf->altsetting[i].desc.bAlternateSetting == alt)
1705 return &intf->altsetting[i];
1706 }
1707
1708 return NULL;
1709}
1710
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001711static void btusb_work(struct work_struct *work)
1712{
1713 struct btusb_data *data = container_of(work, struct btusb_data, work);
1714 struct hci_dev *hdev = data->hdev;
Sathish Narasimmanbaac6272020-04-03 21:43:59 +02001715 int new_alts = 0;
Oliver Neukum7bee5492009-08-24 23:44:59 +02001716 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001717
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001718 if (data->sco_num > 0) {
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -03001719 if (!test_bit(BTUSB_DID_ISO_RESUME, &data->flags)) {
Oliver Neukum8efdd0c2011-02-11 13:00:06 +01001720 err = usb_autopm_get_interface(data->isoc ? data->isoc : data->intf);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001721 if (err < 0) {
1722 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1723 usb_kill_anchored_urbs(&data->isoc_anchor);
1724 return;
1725 }
1726
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -03001727 set_bit(BTUSB_DID_ISO_RESUME, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001728 }
Mikel Astizf4001d22012-04-11 08:48:51 +02001729
Sathish Narasimmanbaac6272020-04-03 21:43:59 +02001730 if (data->air_mode == HCI_NOTIFY_ENABLE_SCO_CVSD) {
1731 if (hdev->voice_setting & 0x0020) {
1732 static const int alts[3] = { 2, 4, 5 };
Marcel Holtmann89e75332014-09-16 04:44:50 +02001733
Sathish Narasimmanbaac6272020-04-03 21:43:59 +02001734 new_alts = alts[data->sco_num - 1];
1735 } else {
1736 new_alts = data->sco_num;
1737 }
1738 } else if (data->air_mode == HCI_NOTIFY_ENABLE_SCO_TRANSP) {
Sathish Narasimmanbaac6272020-04-03 21:43:59 +02001739 /* Check if Alt 6 is supported for Transparent audio */
Hilda Wu461f95f2020-06-30 21:09:40 +08001740 if (btusb_find_altsetting(data, 6)) {
1741 data->usb_alt6_packet_flow = true;
Sathish Narasimmanbaac6272020-04-03 21:43:59 +02001742 new_alts = 6;
Hilda Wu461f95f2020-06-30 21:09:40 +08001743 } else if (test_bit(BTUSB_USE_ALT1_FOR_WBS, &data->flags)) {
1744 new_alts = 1;
1745 } else {
Sathish Narasimmanbaac6272020-04-03 21:43:59 +02001746 bt_dev_err(hdev, "Device does not support ALT setting 6");
Hilda Wu461f95f2020-06-30 21:09:40 +08001747 }
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001748 }
Sathish Narasimmanbaac6272020-04-03 21:43:59 +02001749
1750 if (btusb_switch_alt_setting(hdev, new_alts) < 0)
1751 bt_dev_err(hdev, "set USB alt:(%d) failed!", new_alts);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001752 } else {
1753 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1754 usb_kill_anchored_urbs(&data->isoc_anchor);
1755
1756 __set_isoc_interface(hdev, 0);
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -03001757 if (test_and_clear_bit(BTUSB_DID_ISO_RESUME, &data->flags))
Oliver Neukum8efdd0c2011-02-11 13:00:06 +01001758 usb_autopm_put_interface(data->isoc ? data->isoc : data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001759 }
1760}
1761
Oliver Neukum7bee5492009-08-24 23:44:59 +02001762static void btusb_waker(struct work_struct *work)
1763{
1764 struct btusb_data *data = container_of(work, struct btusb_data, waker);
1765 int err;
1766
1767 err = usb_autopm_get_interface(data->intf);
1768 if (err < 0)
1769 return;
1770
1771 usb_autopm_put_interface(data->intf);
1772}
1773
Marcel Holtmann9f8f9622013-04-10 08:11:35 -07001774static int btusb_setup_bcm92035(struct hci_dev *hdev)
1775{
1776 struct sk_buff *skb;
1777 u8 val = 0x00;
1778
1779 BT_DBG("%s", hdev->name);
1780
1781 skb = __hci_cmd_sync(hdev, 0xfc3b, 1, &val, HCI_INIT_TIMEOUT);
1782 if (IS_ERR(skb))
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001783 bt_dev_err(hdev, "BCM92035 command failed (%ld)", PTR_ERR(skb));
Marcel Holtmann9f8f9622013-04-10 08:11:35 -07001784 else
1785 kfree_skb(skb);
1786
1787 return 0;
1788}
1789
Marcel Holtmann81cac642014-01-03 03:02:36 -08001790static int btusb_setup_csr(struct hci_dev *hdev)
1791{
1792 struct hci_rp_read_local_version *rp;
1793 struct sk_buff *skb;
Ismael Ferreras Morezuelascde1a8a2020-07-26 23:12:28 +02001794 bool is_fake = false;
Marcel Holtmann81cac642014-01-03 03:02:36 -08001795
1796 BT_DBG("%s", hdev->name);
1797
Marcel Holtmann7cd84d72015-06-07 10:01:02 +02001798 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
1799 HCI_INIT_TIMEOUT);
1800 if (IS_ERR(skb)) {
1801 int err = PTR_ERR(skb);
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001802 bt_dev_err(hdev, "CSR: Local version failed (%d)", err);
Marcel Holtmann7cd84d72015-06-07 10:01:02 +02001803 return err;
1804 }
1805
1806 if (skb->len != sizeof(struct hci_rp_read_local_version)) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001807 bt_dev_err(hdev, "CSR: Local version length mismatch");
Marcel Holtmann7cd84d72015-06-07 10:01:02 +02001808 kfree_skb(skb);
1809 return -EIO;
1810 }
Marcel Holtmann81cac642014-01-03 03:02:36 -08001811
Marcel Holtmann89e75332014-09-16 04:44:50 +02001812 rp = (struct hci_rp_read_local_version *)skb->data;
Marcel Holtmann81cac642014-01-03 03:02:36 -08001813
Ismael Ferreras Morezuelascde1a8a2020-07-26 23:12:28 +02001814 /* Detect a wide host of Chinese controllers that aren't CSR.
1815 *
1816 * Known fake bcdDevices: 0x0100, 0x0134, 0x1915, 0x2520, 0x7558, 0x8891
1817 *
1818 * The main thing they have in common is that these are really popular low-cost
1819 * options that support newer Bluetooth versions but rely on heavy VID/PID
1820 * squatting of this poor old Bluetooth 1.1 device. Even sold as such.
1821 *
1822 * We detect actual CSR devices by checking that the HCI manufacturer code
1823 * is Cambridge Silicon Radio (10) and ensuring that LMP sub-version and
1824 * HCI rev values always match. As they both store the firmware number.
1825 */
Johan Hedberg6cafcd92015-08-30 21:47:21 +03001826 if (le16_to_cpu(rp->manufacturer) != 10 ||
Ismael Ferreras Morezuelascde1a8a2020-07-26 23:12:28 +02001827 le16_to_cpu(rp->hci_rev) != le16_to_cpu(rp->lmp_subver))
1828 is_fake = true;
1829
1830 /* Known legit CSR firmware build numbers and their supported BT versions:
1831 * - 1.1 (0x1) -> 0x0073, 0x020d, 0x033c, 0x034e
1832 * - 1.2 (0x2) -> 0x04d9, 0x0529
1833 * - 2.0 (0x3) -> 0x07a6, 0x07ad, 0x0c5c
1834 * - 2.1 (0x4) -> 0x149c, 0x1735, 0x1899 (0x1899 is a BlueCore4-External)
1835 * - 4.0 (0x6) -> 0x1d86, 0x2031, 0x22bb
1836 *
1837 * e.g. Real CSR dongles with LMP subversion 0x73 are old enough that
1838 * support BT 1.1 only; so it's a dead giveaway when some
1839 * third-party BT 4.0 dongle reuses it.
1840 */
1841 else if (le16_to_cpu(rp->lmp_subver) <= 0x034e &&
1842 le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_1_1)
1843 is_fake = true;
1844
1845 else if (le16_to_cpu(rp->lmp_subver) <= 0x0529 &&
1846 le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_1_2)
1847 is_fake = true;
1848
1849 else if (le16_to_cpu(rp->lmp_subver) <= 0x0c5c &&
1850 le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_2_0)
1851 is_fake = true;
1852
1853 else if (le16_to_cpu(rp->lmp_subver) <= 0x1899 &&
1854 le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_2_1)
1855 is_fake = true;
1856
1857 else if (le16_to_cpu(rp->lmp_subver) <= 0x22bb &&
1858 le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_4_0)
1859 is_fake = true;
1860
1861 if (is_fake) {
1862 bt_dev_warn(hdev, "CSR: Unbranded CSR clone detected; adding workarounds...");
1863
1864 /* Generally these clones have big discrepancies between
1865 * advertised features and what's actually supported.
1866 * Probably will need to be expanded in the future;
1867 * without these the controller will lock up.
1868 */
1869 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
1870 set_bit(HCI_QUIRK_BROKEN_ERR_DATA_REPORTING, &hdev->quirks);
1871
Marcel Holtmann9641d342015-06-07 10:01:01 +02001872 /* Clear the reset quirk since this is not an actual
1873 * early Bluetooth 1.1 device from CSR.
1874 */
1875 clear_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Ismael Ferreras Morezuelascde1a8a2020-07-26 23:12:28 +02001876 clear_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
Marcel Holtmann81cac642014-01-03 03:02:36 -08001877 }
1878
Marcel Holtmann81cac642014-01-03 03:02:36 -08001879 kfree_skb(skb);
1880
Marcel Holtmann9641d342015-06-07 10:01:01 +02001881 return 0;
Marcel Holtmann81cac642014-01-03 03:02:36 -08001882}
1883
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001884static const struct firmware *btusb_setup_intel_get_fw(struct hci_dev *hdev,
Marcel Holtmann89e75332014-09-16 04:44:50 +02001885 struct intel_version *ver)
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001886{
1887 const struct firmware *fw;
1888 char fwname[64];
1889 int ret;
1890
1891 snprintf(fwname, sizeof(fwname),
1892 "intel/ibt-hw-%x.%x.%x-fw-%x.%x.%x.%x.%x.bseq",
1893 ver->hw_platform, ver->hw_variant, ver->hw_revision,
1894 ver->fw_variant, ver->fw_revision, ver->fw_build_num,
1895 ver->fw_build_ww, ver->fw_build_yy);
1896
1897 ret = request_firmware(&fw, fwname, &hdev->dev);
1898 if (ret < 0) {
1899 if (ret == -EINVAL) {
Marcel Holtmann85418fe2018-08-03 11:20:49 +02001900 bt_dev_err(hdev, "Intel firmware file request failed (%d)",
1901 ret);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001902 return NULL;
1903 }
1904
Marcel Holtmann85418fe2018-08-03 11:20:49 +02001905 bt_dev_err(hdev, "failed to open Intel firmware file: %s (%d)",
1906 fwname, ret);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001907
1908 /* If the correct firmware patch file is not found, use the
1909 * default firmware patch file instead
1910 */
1911 snprintf(fwname, sizeof(fwname), "intel/ibt-hw-%x.%x.bseq",
1912 ver->hw_platform, ver->hw_variant);
1913 if (request_firmware(&fw, fwname, &hdev->dev) < 0) {
Marcel Holtmann85418fe2018-08-03 11:20:49 +02001914 bt_dev_err(hdev, "failed to open default fw file: %s",
1915 fwname);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001916 return NULL;
1917 }
1918 }
1919
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001920 bt_dev_info(hdev, "Intel Bluetooth firmware file: %s", fwname);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001921
1922 return fw;
1923}
1924
1925static int btusb_setup_intel_patching(struct hci_dev *hdev,
1926 const struct firmware *fw,
1927 const u8 **fw_ptr, int *disable_patch)
1928{
1929 struct sk_buff *skb;
1930 struct hci_command_hdr *cmd;
1931 const u8 *cmd_param;
1932 struct hci_event_hdr *evt = NULL;
1933 const u8 *evt_param = NULL;
1934 int remain = fw->size - (*fw_ptr - fw->data);
1935
1936 /* The first byte indicates the types of the patch command or event.
1937 * 0x01 means HCI command and 0x02 is HCI event. If the first bytes
1938 * in the current firmware buffer doesn't start with 0x01 or
1939 * the size of remain buffer is smaller than HCI command header,
1940 * the firmware file is corrupted and it should stop the patching
1941 * process.
1942 */
1943 if (remain > HCI_COMMAND_HDR_SIZE && *fw_ptr[0] != 0x01) {
Marcel Holtmann85418fe2018-08-03 11:20:49 +02001944 bt_dev_err(hdev, "Intel fw corrupted: invalid cmd read");
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001945 return -EINVAL;
1946 }
1947 (*fw_ptr)++;
1948 remain--;
1949
1950 cmd = (struct hci_command_hdr *)(*fw_ptr);
1951 *fw_ptr += sizeof(*cmd);
1952 remain -= sizeof(*cmd);
1953
1954 /* Ensure that the remain firmware data is long enough than the length
1955 * of command parameter. If not, the firmware file is corrupted.
1956 */
1957 if (remain < cmd->plen) {
Marcel Holtmann85418fe2018-08-03 11:20:49 +02001958 bt_dev_err(hdev, "Intel fw corrupted: invalid cmd len");
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001959 return -EFAULT;
1960 }
1961
1962 /* If there is a command that loads a patch in the firmware
1963 * file, then enable the patch upon success, otherwise just
1964 * disable the manufacturer mode, for example patch activation
1965 * is not required when the default firmware patch file is used
1966 * because there are no patch data to load.
1967 */
1968 if (*disable_patch && le16_to_cpu(cmd->opcode) == 0xfc8e)
1969 *disable_patch = 0;
1970
1971 cmd_param = *fw_ptr;
1972 *fw_ptr += cmd->plen;
1973 remain -= cmd->plen;
1974
1975 /* This reads the expected events when the above command is sent to the
1976 * device. Some vendor commands expects more than one events, for
1977 * example command status event followed by vendor specific event.
1978 * For this case, it only keeps the last expected event. so the command
1979 * can be sent with __hci_cmd_sync_ev() which returns the sk_buff of
1980 * last expected event.
1981 */
1982 while (remain > HCI_EVENT_HDR_SIZE && *fw_ptr[0] == 0x02) {
1983 (*fw_ptr)++;
1984 remain--;
1985
1986 evt = (struct hci_event_hdr *)(*fw_ptr);
1987 *fw_ptr += sizeof(*evt);
1988 remain -= sizeof(*evt);
1989
1990 if (remain < evt->plen) {
Marcel Holtmann85418fe2018-08-03 11:20:49 +02001991 bt_dev_err(hdev, "Intel fw corrupted: invalid evt len");
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001992 return -EFAULT;
1993 }
1994
1995 evt_param = *fw_ptr;
1996 *fw_ptr += evt->plen;
1997 remain -= evt->plen;
1998 }
1999
2000 /* Every HCI commands in the firmware file has its correspond event.
2001 * If event is not found or remain is smaller than zero, the firmware
2002 * file is corrupted.
2003 */
2004 if (!evt || !evt_param || remain < 0) {
Marcel Holtmann85418fe2018-08-03 11:20:49 +02002005 bt_dev_err(hdev, "Intel fw corrupted: invalid evt read");
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002006 return -EFAULT;
2007 }
2008
2009 skb = __hci_cmd_sync_ev(hdev, le16_to_cpu(cmd->opcode), cmd->plen,
2010 cmd_param, evt->evt, HCI_INIT_TIMEOUT);
2011 if (IS_ERR(skb)) {
Marcel Holtmann85418fe2018-08-03 11:20:49 +02002012 bt_dev_err(hdev, "sending Intel patch command (0x%4.4x) failed (%ld)",
2013 cmd->opcode, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08002014 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002015 }
2016
2017 /* It ensures that the returned event matches the event data read from
2018 * the firmware file. At fist, it checks the length and then
2019 * the contents of the event.
2020 */
2021 if (skb->len != evt->plen) {
Marcel Holtmann85418fe2018-08-03 11:20:49 +02002022 bt_dev_err(hdev, "mismatch event length (opcode 0x%4.4x)",
2023 le16_to_cpu(cmd->opcode));
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002024 kfree_skb(skb);
2025 return -EFAULT;
2026 }
2027
2028 if (memcmp(skb->data, evt_param, evt->plen)) {
Marcel Holtmann85418fe2018-08-03 11:20:49 +02002029 bt_dev_err(hdev, "mismatch event parameter (opcode 0x%4.4x)",
2030 le16_to_cpu(cmd->opcode));
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002031 kfree_skb(skb);
2032 return -EFAULT;
2033 }
2034 kfree_skb(skb);
2035
2036 return 0;
2037}
2038
2039static int btusb_setup_intel(struct hci_dev *hdev)
2040{
2041 struct sk_buff *skb;
2042 const struct firmware *fw;
2043 const u8 *fw_ptr;
Loic Poulain28dc4b92015-12-03 16:10:22 +01002044 int disable_patch, err;
Loic Poulain6c483de2015-12-06 16:18:34 +01002045 struct intel_version ver;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002046
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002047 BT_DBG("%s", hdev->name);
2048
2049 /* The controller has a bug with the first HCI command sent to it
2050 * returning number of completed commands as zero. This would stall the
2051 * command processing in the Bluetooth core.
2052 *
2053 * As a workaround, send HCI Reset command first which will reset the
2054 * number of completed commands and allow normal command processing
2055 * from now on.
2056 */
2057 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
2058 if (IS_ERR(skb)) {
Marcel Holtmann85418fe2018-08-03 11:20:49 +02002059 bt_dev_err(hdev, "sending initial HCI reset command failed (%ld)",
2060 PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08002061 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002062 }
2063 kfree_skb(skb);
2064
2065 /* Read Intel specific controller version first to allow selection of
2066 * which firmware file to load.
2067 *
2068 * The returned information are hardware variant and revision plus
2069 * firmware variant, revision and build number.
2070 */
Loic Poulain6c483de2015-12-06 16:18:34 +01002071 err = btintel_read_version(hdev, &ver);
2072 if (err)
2073 return err;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002074
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002075 bt_dev_info(hdev, "read Intel version: %02x%02x%02x%02x%02x%02x%02x%02x%02x",
2076 ver.hw_platform, ver.hw_variant, ver.hw_revision,
2077 ver.fw_variant, ver.fw_revision, ver.fw_build_num,
2078 ver.fw_build_ww, ver.fw_build_yy, ver.fw_patch_num);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002079
2080 /* fw_patch_num indicates the version of patch the device currently
2081 * have. If there is no patch data in the device, it is always 0x00.
Minjune Kim5075eda2015-08-27 13:21:52 +09002082 * So, if it is other than 0x00, no need to patch the device again.
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002083 */
Loic Poulain6c483de2015-12-06 16:18:34 +01002084 if (ver.fw_patch_num) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002085 bt_dev_info(hdev, "Intel device is already patched. "
2086 "patch num: %02x", ver.fw_patch_num);
Marcel Holtmann213445b2015-10-21 02:45:19 +02002087 goto complete;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002088 }
2089
2090 /* Opens the firmware patch file based on the firmware version read
2091 * from the controller. If it fails to open the matching firmware
2092 * patch file, it tries to open the default firmware patch file.
2093 * If no patch file is found, allow the device to operate without
2094 * a patch.
2095 */
Loic Poulain6c483de2015-12-06 16:18:34 +01002096 fw = btusb_setup_intel_get_fw(hdev, &ver);
2097 if (!fw)
Marcel Holtmann213445b2015-10-21 02:45:19 +02002098 goto complete;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002099 fw_ptr = fw->data;
2100
Loic Poulain28dc4b92015-12-03 16:10:22 +01002101 /* Enable the manufacturer mode of the controller.
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002102 * Only while this mode is enabled, the driver can download the
2103 * firmware patch data and configuration parameters.
2104 */
Loic Poulain28dc4b92015-12-03 16:10:22 +01002105 err = btintel_enter_mfg(hdev);
2106 if (err) {
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002107 release_firmware(fw);
Loic Poulain28dc4b92015-12-03 16:10:22 +01002108 return err;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002109 }
2110
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002111 disable_patch = 1;
2112
2113 /* The firmware data file consists of list of Intel specific HCI
2114 * commands and its expected events. The first byte indicates the
2115 * type of the message, either HCI command or HCI event.
2116 *
2117 * It reads the command and its expected event from the firmware file,
2118 * and send to the controller. Once __hci_cmd_sync_ev() returns,
2119 * the returned event is compared with the event read from the firmware
2120 * file and it will continue until all the messages are downloaded to
2121 * the controller.
2122 *
2123 * Once the firmware patching is completed successfully,
2124 * the manufacturer mode is disabled with reset and activating the
2125 * downloaded patch.
2126 *
2127 * If the firmware patching fails, the manufacturer mode is
2128 * disabled with reset and deactivating the patch.
2129 *
2130 * If the default patch file is used, no reset is done when disabling
2131 * the manufacturer.
2132 */
2133 while (fw->size > fw_ptr - fw->data) {
2134 int ret;
2135
2136 ret = btusb_setup_intel_patching(hdev, fw, &fw_ptr,
2137 &disable_patch);
2138 if (ret < 0)
2139 goto exit_mfg_deactivate;
2140 }
2141
2142 release_firmware(fw);
2143
2144 if (disable_patch)
2145 goto exit_mfg_disable;
2146
2147 /* Patching completed successfully and disable the manufacturer mode
2148 * with reset and activate the downloaded firmware patches.
2149 */
Loic Poulain28dc4b92015-12-03 16:10:22 +01002150 err = btintel_exit_mfg(hdev, true, true);
2151 if (err)
2152 return err;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002153
Sukumar Ghorai905d7b12020-03-16 11:37:18 +05302154 /* Need build number for downloaded fw patches in
2155 * every power-on boot
2156 */
2157 err = btintel_read_version(hdev, &ver);
2158 if (err)
2159 return err;
2160 bt_dev_info(hdev, "Intel BT fw patch 0x%02x completed & activated",
2161 ver.fw_patch_num);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002162
Marcel Holtmann213445b2015-10-21 02:45:19 +02002163 goto complete;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002164
2165exit_mfg_disable:
2166 /* Disable the manufacturer mode without reset */
Loic Poulain28dc4b92015-12-03 16:10:22 +01002167 err = btintel_exit_mfg(hdev, false, false);
2168 if (err)
2169 return err;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002170
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002171 bt_dev_info(hdev, "Intel firmware patch completed");
Marcel Holtmann40cb0982014-07-02 12:06:45 +02002172
Marcel Holtmann213445b2015-10-21 02:45:19 +02002173 goto complete;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002174
2175exit_mfg_deactivate:
2176 release_firmware(fw);
2177
2178 /* Patching failed. Disable the manufacturer mode with reset and
2179 * deactivate the downloaded firmware patches.
2180 */
Loic Poulain28dc4b92015-12-03 16:10:22 +01002181 err = btintel_exit_mfg(hdev, true, false);
2182 if (err)
2183 return err;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002184
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002185 bt_dev_info(hdev, "Intel firmware patch completed and deactivated");
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002186
Marcel Holtmann213445b2015-10-21 02:45:19 +02002187complete:
2188 /* Set the event mask for Intel specific vendor events. This enables
2189 * a few extra events that are useful during general operation.
2190 */
2191 btintel_set_event_mask_mfg(hdev, false);
2192
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07002193 btintel_check_bdaddr(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002194 return 0;
2195}
2196
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002197static int inject_cmd_complete(struct hci_dev *hdev, __u16 opcode)
2198{
2199 struct sk_buff *skb;
2200 struct hci_event_hdr *hdr;
2201 struct hci_ev_cmd_complete *evt;
2202
Jia-Ju Baicf07e342018-07-23 11:38:51 +08002203 skb = bt_skb_alloc(sizeof(*hdr) + sizeof(*evt) + 1, GFP_KERNEL);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002204 if (!skb)
2205 return -ENOMEM;
2206
Johannes Berg4df864c2017-06-16 14:29:21 +02002207 hdr = skb_put(skb, sizeof(*hdr));
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002208 hdr->evt = HCI_EV_CMD_COMPLETE;
2209 hdr->plen = sizeof(*evt) + 1;
2210
Johannes Berg4df864c2017-06-16 14:29:21 +02002211 evt = skb_put(skb, sizeof(*evt));
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002212 evt->ncmd = 0x01;
2213 evt->opcode = cpu_to_le16(opcode);
2214
Johannes Berg634fef62017-06-16 14:29:24 +02002215 skb_put_u8(skb, 0x00);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002216
Marcel Holtmann618e8bc2015-11-05 07:33:56 +01002217 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002218
2219 return hci_recv_frame(hdev, skb);
2220}
2221
2222static int btusb_recv_bulk_intel(struct btusb_data *data, void *buffer,
2223 int count)
2224{
2225 /* When the device is in bootloader mode, then it can send
2226 * events via the bulk endpoint. These events are treated the
2227 * same way as the ones received from the interrupt endpoint.
2228 */
2229 if (test_bit(BTUSB_BOOTLOADER, &data->flags))
2230 return btusb_recv_intr(data, buffer, count);
2231
2232 return btusb_recv_bulk(data, buffer, count);
2233}
2234
Marcel Holtmannccd6da22015-04-09 00:35:19 -07002235static void btusb_intel_bootup(struct btusb_data *data, const void *ptr,
2236 unsigned int len)
2237{
2238 const struct intel_bootup *evt = ptr;
2239
2240 if (len != sizeof(*evt))
2241 return;
2242
Andrea Parridff6d592018-11-27 12:22:25 +01002243 if (test_and_clear_bit(BTUSB_BOOTING, &data->flags))
Marcel Holtmannccd6da22015-04-09 00:35:19 -07002244 wake_up_bit(&data->flags, BTUSB_BOOTING);
Marcel Holtmannccd6da22015-04-09 00:35:19 -07002245}
2246
2247static void btusb_intel_secure_send_result(struct btusb_data *data,
2248 const void *ptr, unsigned int len)
2249{
2250 const struct intel_secure_send_result *evt = ptr;
2251
2252 if (len != sizeof(*evt))
2253 return;
2254
2255 if (evt->result)
2256 set_bit(BTUSB_FIRMWARE_FAILED, &data->flags);
2257
2258 if (test_and_clear_bit(BTUSB_DOWNLOADING, &data->flags) &&
Andrea Parridff6d592018-11-27 12:22:25 +01002259 test_bit(BTUSB_FIRMWARE_LOADED, &data->flags))
Marcel Holtmannccd6da22015-04-09 00:35:19 -07002260 wake_up_bit(&data->flags, BTUSB_DOWNLOADING);
Marcel Holtmannccd6da22015-04-09 00:35:19 -07002261}
2262
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002263static int btusb_recv_event_intel(struct hci_dev *hdev, struct sk_buff *skb)
2264{
2265 struct btusb_data *data = hci_get_drvdata(hdev);
2266
2267 if (test_bit(BTUSB_BOOTLOADER, &data->flags)) {
2268 struct hci_event_hdr *hdr = (void *)skb->data;
2269
Marcel Holtmannccd6da22015-04-09 00:35:19 -07002270 if (skb->len > HCI_EVENT_HDR_SIZE && hdr->evt == 0xff &&
2271 hdr->plen > 0) {
2272 const void *ptr = skb->data + HCI_EVENT_HDR_SIZE + 1;
2273 unsigned int len = skb->len - HCI_EVENT_HDR_SIZE - 1;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002274
Marcel Holtmannccd6da22015-04-09 00:35:19 -07002275 switch (skb->data[2]) {
2276 case 0x02:
2277 /* When switching to the operational firmware
2278 * the device sends a vendor specific event
2279 * indicating that the bootup completed.
2280 */
2281 btusb_intel_bootup(data, ptr, len);
2282 break;
2283 case 0x06:
2284 /* When the firmware loading completes the
2285 * device sends out a vendor specific event
2286 * indicating the result of the firmware
2287 * loading.
2288 */
2289 btusb_intel_secure_send_result(data, ptr, len);
2290 break;
Johan Hedbergfad70972015-01-30 10:58:55 +02002291 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002292 }
2293 }
2294
2295 return hci_recv_frame(hdev, skb);
2296}
2297
2298static int btusb_send_frame_intel(struct hci_dev *hdev, struct sk_buff *skb)
2299{
2300 struct btusb_data *data = hci_get_drvdata(hdev);
2301 struct urb *urb;
2302
2303 BT_DBG("%s", hdev->name);
2304
Marcel Holtmann618e8bc2015-11-05 07:33:56 +01002305 switch (hci_skb_pkt_type(skb)) {
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002306 case HCI_COMMAND_PKT:
2307 if (test_bit(BTUSB_BOOTLOADER, &data->flags)) {
2308 struct hci_command_hdr *cmd = (void *)skb->data;
2309 __u16 opcode = le16_to_cpu(cmd->opcode);
2310
2311 /* When in bootloader mode and the command 0xfc09
2312 * is received, it needs to be send down the
2313 * bulk endpoint. So allocate a bulk URB instead.
2314 */
2315 if (opcode == 0xfc09)
2316 urb = alloc_bulk_urb(hdev, skb);
2317 else
2318 urb = alloc_ctrl_urb(hdev, skb);
2319
2320 /* When the 0xfc01 command is issued to boot into
2321 * the operational firmware, it will actually not
2322 * send a command complete event. To keep the flow
2323 * control working inject that event here.
2324 */
2325 if (opcode == 0xfc01)
2326 inject_cmd_complete(hdev, opcode);
2327 } else {
2328 urb = alloc_ctrl_urb(hdev, skb);
2329 }
2330 if (IS_ERR(urb))
2331 return PTR_ERR(urb);
2332
2333 hdev->stat.cmd_tx++;
2334 return submit_or_queue_tx_urb(hdev, urb);
2335
2336 case HCI_ACLDATA_PKT:
2337 urb = alloc_bulk_urb(hdev, skb);
2338 if (IS_ERR(urb))
2339 return PTR_ERR(urb);
2340
2341 hdev->stat.acl_tx++;
2342 return submit_or_queue_tx_urb(hdev, urb);
2343
2344 case HCI_SCODATA_PKT:
2345 if (hci_conn_num(hdev, SCO_LINK) < 1)
2346 return -ENODEV;
2347
2348 urb = alloc_isoc_urb(hdev, skb);
2349 if (IS_ERR(urb))
2350 return PTR_ERR(urb);
2351
2352 hdev->stat.sco_tx++;
2353 return submit_tx_urb(hdev, urb);
2354 }
2355
2356 return -EILSEQ;
2357}
2358
Raghuram Hegde2da711bc2018-12-19 11:42:18 +05302359static bool btusb_setup_intel_new_get_fw_name(struct intel_version *ver,
2360 struct intel_boot_params *params,
2361 char *fw_name, size_t len,
2362 const char *suffix)
2363{
2364 switch (ver->hw_variant) {
2365 case 0x0b: /* SfP */
2366 case 0x0c: /* WsP */
2367 snprintf(fw_name, len, "intel/ibt-%u-%u.%s",
2368 le16_to_cpu(ver->hw_variant),
2369 le16_to_cpu(params->dev_revid),
2370 suffix);
2371 break;
2372 case 0x11: /* JfP */
2373 case 0x12: /* ThP */
2374 case 0x13: /* HrP */
2375 case 0x14: /* CcP */
2376 snprintf(fw_name, len, "intel/ibt-%u-%u-%u.%s",
2377 le16_to_cpu(ver->hw_variant),
2378 le16_to_cpu(ver->hw_revision),
2379 le16_to_cpu(ver->fw_revision),
2380 suffix);
2381 break;
2382 default:
2383 return false;
2384 }
2385 return true;
2386}
2387
Kiran K9a93b8b2020-11-19 17:09:41 +05302388static void btusb_setup_intel_newgen_get_fw_name(const struct intel_version_tlv *ver_tlv,
2389 char *fw_name, size_t len,
2390 const char *suffix)
2391{
2392 /* The firmware file name for new generation controllers will be
2393 * ibt-<cnvi_top type+cnvi_top step>-<cnvr_top type+cnvr_top step>
2394 */
2395 snprintf(fw_name, len, "intel/ibt-%04x-%04x.%s",
2396 INTEL_CNVX_TOP_PACK_SWAB(INTEL_CNVX_TOP_TYPE(ver_tlv->cnvi_top),
2397 INTEL_CNVX_TOP_STEP(ver_tlv->cnvi_top)),
2398 INTEL_CNVX_TOP_PACK_SWAB(INTEL_CNVX_TOP_TYPE(ver_tlv->cnvr_top),
2399 INTEL_CNVX_TOP_STEP(ver_tlv->cnvr_top)),
2400 suffix);
2401}
2402
Kiran K5ea7c812020-06-25 00:04:32 +05302403static int btusb_intel_download_firmware(struct hci_dev *hdev,
2404 struct intel_version *ver,
Kiran Kdc45d372020-08-26 15:24:32 +05302405 struct intel_boot_params *params,
2406 u32 *boot_param)
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002407{
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002408 const struct firmware *fw;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002409 char fwname[64];
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002410 int err;
Kiran K5ea7c812020-06-25 00:04:32 +05302411 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002412
Kiran K5ea7c812020-06-25 00:04:32 +05302413 if (!ver || !params)
2414 return -EINVAL;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002415
2416 /* The hardware platform number has a fixed value of 0x37 and
2417 * for now only accept this single value.
2418 */
Kiran K5ea7c812020-06-25 00:04:32 +05302419 if (ver->hw_platform != 0x37) {
Marcel Holtmann85418fe2018-08-03 11:20:49 +02002420 bt_dev_err(hdev, "Unsupported Intel hardware platform (%u)",
Kiran K5ea7c812020-06-25 00:04:32 +05302421 ver->hw_platform);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002422 return -EINVAL;
2423 }
2424
Tedd Ho-Jeong An92688342017-03-06 15:38:26 -08002425 /* Check for supported iBT hardware variants of this firmware
2426 * loading method.
Tedd Ho-Jeong Ana0af53b2016-05-06 11:53:46 -07002427 *
2428 * This check has been put in place to ensure correct forward
2429 * compatibility options when newer hardware variants come along.
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002430 */
Kiran K5ea7c812020-06-25 00:04:32 +05302431 switch (ver->hw_variant) {
Tedd Ho-Jeong An92688342017-03-06 15:38:26 -08002432 case 0x0b: /* SfP */
2433 case 0x0c: /* WsP */
Tedd Ho-Jeong An86a61292017-05-01 13:35:12 -07002434 case 0x11: /* JfP */
Marcel Holtmannde766142017-03-06 15:38:28 -08002435 case 0x12: /* ThP */
Tedd Ho-Jeong An1ce0cec2018-02-05 14:20:36 -08002436 case 0x13: /* HrP */
Raghuram Hegde2da711bc2018-12-19 11:42:18 +05302437 case 0x14: /* CcP */
Tedd Ho-Jeong An92688342017-03-06 15:38:26 -08002438 break;
2439 default:
Marcel Holtmann85418fe2018-08-03 11:20:49 +02002440 bt_dev_err(hdev, "Unsupported Intel hardware variant (%u)",
Kiran K5ea7c812020-06-25 00:04:32 +05302441 ver->hw_variant);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002442 return -EINVAL;
2443 }
2444
Kiran K5ea7c812020-06-25 00:04:32 +05302445 btintel_version_info(hdev, ver);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002446
2447 /* The firmware variant determines if the device is in bootloader
2448 * mode or is running operational firmware. The value 0x06 identifies
2449 * the bootloader and the value 0x23 identifies the operational
2450 * firmware.
2451 *
2452 * When the operational firmware is already present, then only
2453 * the check for valid Bluetooth device address is needed. This
2454 * determines if the device will be added as configured or
2455 * unconfigured controller.
2456 *
2457 * It is not possible to use the Secure Boot Parameters in this
2458 * case since that command is only available in bootloader mode.
2459 */
Kiran K5ea7c812020-06-25 00:04:32 +05302460 if (ver->fw_variant == 0x23) {
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002461 clear_bit(BTUSB_BOOTLOADER, &data->flags);
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07002462 btintel_check_bdaddr(hdev);
Kiran K5ea7c812020-06-25 00:04:32 +05302463 return 0;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002464 }
2465
2466 /* If the device is not in bootloader mode, then the only possible
2467 * choice is to return an error and abort the device initialization.
2468 */
Kiran K5ea7c812020-06-25 00:04:32 +05302469 if (ver->fw_variant != 0x06) {
Marcel Holtmann85418fe2018-08-03 11:20:49 +02002470 bt_dev_err(hdev, "Unsupported Intel firmware variant (%u)",
Kiran K5ea7c812020-06-25 00:04:32 +05302471 ver->fw_variant);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002472 return -ENODEV;
2473 }
2474
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002475 /* Read the secure boot parameters to identify the operating
2476 * details of the bootloader.
2477 */
Kiran K5ea7c812020-06-25 00:04:32 +05302478 err = btintel_read_boot_params(hdev, params);
Tedd Ho-Jeong Anfaf174d2018-01-24 09:19:20 -08002479 if (err)
2480 return err;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002481
2482 /* It is required that every single firmware fragment is acknowledged
2483 * with a command complete event. If the boot parameters indicate
2484 * that this bootloader does not send them, then abort the setup.
2485 */
Kiran K5ea7c812020-06-25 00:04:32 +05302486 if (params->limited_cce != 0x00) {
Marcel Holtmann85418fe2018-08-03 11:20:49 +02002487 bt_dev_err(hdev, "Unsupported Intel firmware loading method (%u)",
Kiran K5ea7c812020-06-25 00:04:32 +05302488 params->limited_cce);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002489 return -EINVAL;
2490 }
2491
2492 /* If the OTP has no valid Bluetooth device address, then there will
2493 * also be no valid address for the operational firmware.
2494 */
Kiran K5ea7c812020-06-25 00:04:32 +05302495 if (!bacmp(&params->otp_bdaddr, BDADDR_ANY)) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002496 bt_dev_info(hdev, "No device address configured");
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002497 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
2498 }
2499
2500 /* With this Intel bootloader only the hardware variant and device
Jaya P Gaf3715e2017-10-30 11:01:22 +01002501 * revision information are used to select the right firmware for SfP
2502 * and WsP.
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002503 *
Tedd Ho-Jeong An230b04a2016-06-28 08:56:39 -07002504 * The firmware filename is ibt-<hw_variant>-<dev_revid>.sfi.
2505 *
2506 * Currently the supported hardware variants are:
2507 * 11 (0x0b) for iBT3.0 (LnP/SfP)
2508 * 12 (0x0c) for iBT3.5 (WsP)
Jaya P Gaf3715e2017-10-30 11:01:22 +01002509 *
2510 * For ThP/JfP and for future SKU's, the FW name varies based on HW
2511 * variant, HW revision and FW revision, as these are dependent on CNVi
2512 * and RF Combination.
2513 *
Tedd Ho-Jeong An86a61292017-05-01 13:35:12 -07002514 * 17 (0x11) for iBT3.5 (JfP)
2515 * 18 (0x12) for iBT3.5 (ThP)
Jaya P Gaf3715e2017-10-30 11:01:22 +01002516 *
2517 * The firmware file name for these will be
2518 * ibt-<hw_variant>-<hw_revision>-<fw_revision>.sfi.
2519 *
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002520 */
Kiran K5ea7c812020-06-25 00:04:32 +05302521 err = btusb_setup_intel_new_get_fw_name(ver, params, fwname,
Raghuram Hegde2da711bc2018-12-19 11:42:18 +05302522 sizeof(fwname), "sfi");
2523 if (!err) {
Marcel Holtmann85418fe2018-08-03 11:20:49 +02002524 bt_dev_err(hdev, "Unsupported Intel firmware naming");
Jaya P Gaf3715e2017-10-30 11:01:22 +01002525 return -EINVAL;
2526 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002527
2528 err = request_firmware(&fw, fwname, &hdev->dev);
2529 if (err < 0) {
Marcel Holtmann85418fe2018-08-03 11:20:49 +02002530 bt_dev_err(hdev, "Failed to load Intel firmware file (%d)", err);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002531 return err;
2532 }
2533
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002534 bt_dev_info(hdev, "Found device firmware: %s", fwname);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002535
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002536 if (fw->size < 644) {
Marcel Holtmann85418fe2018-08-03 11:20:49 +02002537 bt_dev_err(hdev, "Invalid size of firmware file (%zu)",
2538 fw->size);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002539 err = -EBADF;
2540 goto done;
2541 }
2542
2543 set_bit(BTUSB_DOWNLOADING, &data->flags);
2544
Tedd Ho-Jeong Anfbbe83c2018-01-24 09:19:21 -08002545 /* Start firmware downloading and get boot parameter */
Kiran Kdc45d372020-08-26 15:24:32 +05302546 err = btintel_download_firmware(hdev, fw, boot_param);
Amit K Bagb9a25622019-10-17 13:52:29 +05302547 if (err < 0) {
2548 /* When FW download fails, send Intel Reset to retry
2549 * FW download.
2550 */
2551 btintel_reset_to_bootloader(hdev);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002552 goto done;
Amit K Bagb9a25622019-10-17 13:52:29 +05302553 }
Marcel Holtmannce6bb922015-01-28 01:58:40 -08002554 set_bit(BTUSB_FIRMWARE_LOADED, &data->flags);
2555
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002556 bt_dev_info(hdev, "Waiting for firmware download to complete");
Johan Hedberga087a982015-01-30 10:58:54 +02002557
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002558 /* Before switching the device into operational mode and with that
2559 * booting the loaded firmware, wait for the bootloader notification
2560 * that all fragments have been successfully received.
2561 *
Johan Hedberga087a982015-01-30 10:58:54 +02002562 * When the event processing receives the notification, then the
2563 * BTUSB_DOWNLOADING flag will be cleared.
2564 *
2565 * The firmware loading should not take longer than 5 seconds
2566 * and thus just timeout if that happens and fail the setup
2567 * of this device.
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002568 */
Johan Hedberg129a7692015-02-14 09:33:35 +02002569 err = wait_on_bit_timeout(&data->flags, BTUSB_DOWNLOADING,
2570 TASK_INTERRUPTIBLE,
2571 msecs_to_jiffies(5000));
Bart Van Asschef0a70a02016-08-11 16:02:44 -07002572 if (err == -EINTR) {
Marcel Holtmann85418fe2018-08-03 11:20:49 +02002573 bt_dev_err(hdev, "Firmware loading interrupted");
Johan Hedberga087a982015-01-30 10:58:54 +02002574 goto done;
2575 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002576
Johan Hedberga087a982015-01-30 10:58:54 +02002577 if (err) {
Marcel Holtmann85418fe2018-08-03 11:20:49 +02002578 bt_dev_err(hdev, "Firmware loading timeout");
Johan Hedberga087a982015-01-30 10:58:54 +02002579 err = -ETIMEDOUT;
Amit K Bagb9a25622019-10-17 13:52:29 +05302580 btintel_reset_to_bootloader(hdev);
Johan Hedberga087a982015-01-30 10:58:54 +02002581 goto done;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002582 }
2583
2584 if (test_bit(BTUSB_FIRMWARE_FAILED, &data->flags)) {
Marcel Holtmann85418fe2018-08-03 11:20:49 +02002585 bt_dev_err(hdev, "Firmware loading failed");
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002586 err = -ENOEXEC;
2587 goto done;
2588 }
2589
Kiran K5ea7c812020-06-25 00:04:32 +05302590done:
2591 release_firmware(fw);
2592 return err;
2593}
2594
2595static int btusb_setup_intel_new(struct hci_dev *hdev)
2596{
2597 struct btusb_data *data = hci_get_drvdata(hdev);
2598 struct intel_version ver;
2599 struct intel_boot_params params;
2600 u32 boot_param;
2601 char ddcname[64];
2602 ktime_t calltime, delta, rettime;
2603 unsigned long long duration;
2604 int err;
2605 struct intel_debug_features features;
2606
2607 BT_DBG("%s", hdev->name);
2608
2609 /* Set the default boot parameter to 0x0 and it is updated to
2610 * SKU specific boot parameter after reading Intel_Write_Boot_Params
2611 * command while downloading the firmware.
2612 */
2613 boot_param = 0x00000000;
2614
2615 calltime = ktime_get();
2616
2617 /* Read the Intel version information to determine if the device
2618 * is in bootloader mode or if it already has operational firmware
2619 * loaded.
2620 */
2621 err = btintel_read_version(hdev, &ver);
2622 if (err) {
2623 bt_dev_err(hdev, "Intel Read version failed (%d)", err);
2624 btintel_reset_to_bootloader(hdev);
2625 return err;
2626 }
2627
Kiran Kdc45d372020-08-26 15:24:32 +05302628 err = btusb_intel_download_firmware(hdev, &ver, &params, &boot_param);
Kiran K5ea7c812020-06-25 00:04:32 +05302629 if (err)
2630 return err;
2631
2632 /* controller is already having an operational firmware */
2633 if (ver.fw_variant == 0x23)
2634 goto finish;
2635
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002636 rettime = ktime_get();
2637 delta = ktime_sub(rettime, calltime);
2638 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
2639
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002640 bt_dev_info(hdev, "Firmware loaded in %llu usecs", duration);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002641
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002642 calltime = ktime_get();
2643
2644 set_bit(BTUSB_BOOTING, &data->flags);
2645
Tedd Ho-Jeong Ane5889af2018-01-24 09:19:18 -08002646 err = btintel_send_intel_reset(hdev, boot_param);
Amit K Bagb9a25622019-10-17 13:52:29 +05302647 if (err) {
2648 bt_dev_err(hdev, "Intel Soft Reset failed (%d)", err);
2649 btintel_reset_to_bootloader(hdev);
Tedd Ho-Jeong Ane5889af2018-01-24 09:19:18 -08002650 return err;
Amit K Bagb9a25622019-10-17 13:52:29 +05302651 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002652
2653 /* The bootloader will not indicate when the device is ready. This
2654 * is done by the operational firmware sending bootup notification.
Johan Hedbergfad70972015-01-30 10:58:55 +02002655 *
2656 * Booting into operational firmware should not take longer than
2657 * 1 second. However if that happens, then just fail the setup
2658 * since something went wrong.
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002659 */
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002660 bt_dev_info(hdev, "Waiting for device to boot");
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002661
Johan Hedberg129a7692015-02-14 09:33:35 +02002662 err = wait_on_bit_timeout(&data->flags, BTUSB_BOOTING,
2663 TASK_INTERRUPTIBLE,
2664 msecs_to_jiffies(1000));
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002665
Bart Van Asschef0a70a02016-08-11 16:02:44 -07002666 if (err == -EINTR) {
Marcel Holtmann85418fe2018-08-03 11:20:49 +02002667 bt_dev_err(hdev, "Device boot interrupted");
Johan Hedbergfad70972015-01-30 10:58:55 +02002668 return -EINTR;
2669 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002670
Johan Hedbergfad70972015-01-30 10:58:55 +02002671 if (err) {
Marcel Holtmann85418fe2018-08-03 11:20:49 +02002672 bt_dev_err(hdev, "Device boot timeout");
Amit K Bagb9a25622019-10-17 13:52:29 +05302673 btintel_reset_to_bootloader(hdev);
Johan Hedbergfad70972015-01-30 10:58:55 +02002674 return -ETIMEDOUT;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002675 }
2676
2677 rettime = ktime_get();
2678 delta = ktime_sub(rettime, calltime);
2679 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
2680
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002681 bt_dev_info(hdev, "Device booted in %llu usecs", duration);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002682
2683 clear_bit(BTUSB_BOOTLOADER, &data->flags);
2684
Kiran K5ea7c812020-06-25 00:04:32 +05302685 err = btusb_setup_intel_new_get_fw_name(&ver, &params, ddcname,
2686 sizeof(ddcname), "ddc");
2687
2688 if (!err) {
2689 bt_dev_err(hdev, "Unsupported Intel firmware naming");
2690 } else {
2691 /* Once the device is running in operational mode, it needs to
2692 * apply the device configuration (DDC) parameters.
2693 *
2694 * The device can work without DDC parameters, so even if it
2695 * fails to load the file, no need to fail the setup.
2696 */
2697 btintel_load_ddc_config(hdev, ddcname);
2698 }
Tedd Ho-Jeong An52cc9162015-06-12 16:20:05 -07002699
Chethan T Nd74abe22020-06-08 17:57:46 +05302700 /* Read the Intel supported features and if new exception formats
2701 * supported, need to load the additional DDC config to enable.
2702 */
2703 btintel_read_debug_features(hdev, &features);
2704
Chethan T Nc453b102020-06-08 17:57:47 +05302705 /* Set DDC mask for available debug features */
2706 btintel_set_debug_features(hdev, &features);
2707
Marcel Holtmann7fd673b2020-04-03 21:44:02 +02002708 /* Read the Intel version information after loading the FW */
2709 err = btintel_read_version(hdev, &ver);
2710 if (err)
2711 return err;
2712
2713 btintel_version_info(hdev, &ver);
2714
2715finish:
Miao-chen Choufc045902020-04-03 21:44:03 +02002716 /* All Intel controllers that support the Microsoft vendor
2717 * extension are using 0xFC1E for VsMsftOpCode.
2718 */
2719 switch (ver.hw_variant) {
2720 case 0x12: /* ThP */
2721 hci_set_msft_opcode(hdev, 0xFC1E);
2722 break;
2723 }
2724
Marcel Holtmann213445b2015-10-21 02:45:19 +02002725 /* Set the event mask for Intel specific vendor events. This enables
2726 * a few extra events that are useful during general operation. It
2727 * does not enable any debugging related events.
2728 *
2729 * The device will function correctly without these events enabled
2730 * and thus no need to fail the setup.
2731 */
2732 btintel_set_event_mask(hdev, false);
2733
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002734 return 0;
2735}
2736
Kiran K0a3c1d42020-11-19 17:09:40 +05302737static int btusb_setup_intel_newgen(struct hci_dev *hdev)
2738{
2739 struct btusb_data *data = hci_get_drvdata(hdev);
2740 u32 boot_param;
2741 char ddcname[64];
2742 ktime_t calltime, delta, rettime;
2743 unsigned long long duration;
2744 int err;
2745 struct intel_debug_features features;
2746 struct intel_version_tlv version;
2747
2748 bt_dev_dbg(hdev, "");
2749
2750 /* Set the default boot parameter to 0x0 and it is updated to
2751 * SKU specific boot parameter after reading Intel_Write_Boot_Params
2752 * command while downloading the firmware.
2753 */
2754 boot_param = 0x00000000;
2755
2756 calltime = ktime_get();
2757
2758 /* Read the Intel version information to determine if the device
2759 * is in bootloader mode or if it already has operational firmware
2760 * loaded.
2761 */
2762 err = btintel_read_version_tlv(hdev, &version);
2763 if (err) {
2764 bt_dev_err(hdev, "Intel Read version failed (%d)", err);
2765 btintel_reset_to_bootloader(hdev);
2766 return err;
2767 }
2768
2769 btintel_version_info_tlv(hdev, &version);
2770
2771 /* check if controller is already having an operational firmware */
2772 if (version.img_type == 0x03)
2773 goto finish;
2774
2775 rettime = ktime_get();
2776 delta = ktime_sub(rettime, calltime);
2777 duration = (unsigned long long)ktime_to_ns(delta) >> 10;
2778
2779 bt_dev_info(hdev, "Firmware loaded in %llu usecs", duration);
2780
2781 calltime = ktime_get();
2782
2783 set_bit(BTUSB_BOOTING, &data->flags);
2784
2785 err = btintel_send_intel_reset(hdev, boot_param);
2786 if (err) {
2787 bt_dev_err(hdev, "Intel Soft Reset failed (%d)", err);
2788 btintel_reset_to_bootloader(hdev);
2789 return err;
2790 }
2791
2792 /* The bootloader will not indicate when the device is ready. This
2793 * is done by the operational firmware sending bootup notification.
2794 *
2795 * Booting into operational firmware should not take longer than
2796 * 1 second. However if that happens, then just fail the setup
2797 * since something went wrong.
2798 */
2799 bt_dev_info(hdev, "Waiting for device to boot");
2800
2801 err = wait_on_bit_timeout(&data->flags, BTUSB_BOOTING,
2802 TASK_INTERRUPTIBLE,
2803 msecs_to_jiffies(1000));
2804
2805 if (err == -EINTR) {
2806 bt_dev_err(hdev, "Device boot interrupted");
2807 return -EINTR;
2808 }
2809
2810 if (err) {
2811 bt_dev_err(hdev, "Device boot timeout");
2812 btintel_reset_to_bootloader(hdev);
2813 return -ETIMEDOUT;
2814 }
2815
2816 rettime = ktime_get();
2817 delta = ktime_sub(rettime, calltime);
2818 duration = (unsigned long long)ktime_to_ns(delta) >> 10;
2819
2820 bt_dev_info(hdev, "Device booted in %llu usecs", duration);
2821
2822 clear_bit(BTUSB_BOOTLOADER, &data->flags);
2823
Kiran K9a93b8b2020-11-19 17:09:41 +05302824 btusb_setup_intel_newgen_get_fw_name(&version, ddcname, sizeof(ddcname),
2825 "ddc");
Kiran K0a3c1d42020-11-19 17:09:40 +05302826 /* Once the device is running in operational mode, it needs to
2827 * apply the device configuration (DDC) parameters.
2828 *
2829 * The device can work without DDC parameters, so even if it
2830 * fails to load the file, no need to fail the setup.
2831 */
2832 btintel_load_ddc_config(hdev, ddcname);
2833
2834 /* Read the Intel supported features and if new exception formats
2835 * supported, need to load the additional DDC config to enable.
2836 */
2837 btintel_read_debug_features(hdev, &features);
2838
2839 /* Set DDC mask for available debug features */
2840 btintel_set_debug_features(hdev, &features);
2841
2842 /* Read the Intel version information after loading the FW */
2843 err = btintel_read_version_tlv(hdev, &version);
2844 if (err)
2845 return err;
2846
2847 btintel_version_info_tlv(hdev, &version);
2848
2849finish:
2850 /* Set the event mask for Intel specific vendor events. This enables
2851 * a few extra events that are useful during general operation. It
2852 * does not enable any debugging related events.
2853 *
2854 * The device will function correctly without these events enabled
2855 * and thus no need to fail the setup.
2856 */
2857 btintel_set_event_mask(hdev, false);
2858
2859 return 0;
2860}
Tedd Ho-Jeong Anbfbd45e2015-02-13 09:20:52 -08002861static int btusb_shutdown_intel(struct hci_dev *hdev)
2862{
2863 struct sk_buff *skb;
2864 long ret;
2865
Amit K Bag1313bcc2018-08-03 12:43:20 +05302866 /* In the shutdown sequence where Bluetooth is turned off followed
2867 * by WiFi being turned off, turning WiFi back on causes issue with
2868 * the RF calibration.
2869 *
2870 * To ensure that any RF activity has been stopped, issue HCI Reset
2871 * command to clear all ongoing activity including advertising,
2872 * scanning etc.
2873 */
2874 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
2875 if (IS_ERR(skb)) {
2876 ret = PTR_ERR(skb);
2877 bt_dev_err(hdev, "HCI reset during shutdown failed");
2878 return ret;
2879 }
2880 kfree_skb(skb);
2881
Tedd Ho-Jeong Anbfbd45e2015-02-13 09:20:52 -08002882 /* Some platforms have an issue with BT LED when the interface is
2883 * down or BT radio is turned off, which takes 5 seconds to BT LED
2884 * goes off. This command turns off the BT LED immediately.
2885 */
2886 skb = __hci_cmd_sync(hdev, 0xfc3f, 0, NULL, HCI_INIT_TIMEOUT);
2887 if (IS_ERR(skb)) {
2888 ret = PTR_ERR(skb);
Marcel Holtmann85418fe2018-08-03 11:20:49 +02002889 bt_dev_err(hdev, "turning off Intel device LED failed");
Tedd Ho-Jeong Anbfbd45e2015-02-13 09:20:52 -08002890 return ret;
2891 }
2892 kfree_skb(skb);
2893
2894 return 0;
2895}
2896
Raghuram Hegde017a01c2019-01-29 17:54:48 +05302897static int btusb_shutdown_intel_new(struct hci_dev *hdev)
2898{
2899 struct sk_buff *skb;
2900
2901 /* Send HCI Reset to the controller to stop any BT activity which
2902 * were triggered. This will help to save power and maintain the
2903 * sync b/w Host and controller
2904 */
2905 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
2906 if (IS_ERR(skb)) {
2907 bt_dev_err(hdev, "HCI reset during shutdown failed");
2908 return PTR_ERR(skb);
2909 }
2910 kfree_skb(skb);
2911
2912 return 0;
2913}
2914
Sean Wang9ce67c32019-06-02 08:02:49 +08002915#define FIRMWARE_MT7663 "mediatek/mt7663pr2h.bin"
Sean Wanga1c49c432019-06-02 08:02:48 +08002916#define FIRMWARE_MT7668 "mediatek/mt7668pr2h.bin"
2917
2918#define HCI_WMT_MAX_EVENT_SIZE 64
2919
2920enum {
2921 BTMTK_WMT_PATCH_DWNLD = 0x1,
2922 BTMTK_WMT_FUNC_CTRL = 0x6,
2923 BTMTK_WMT_RST = 0x7,
2924 BTMTK_WMT_SEMAPHORE = 0x17,
2925};
2926
2927enum {
2928 BTMTK_WMT_INVALID,
2929 BTMTK_WMT_PATCH_UNDONE,
2930 BTMTK_WMT_PATCH_DONE,
2931 BTMTK_WMT_ON_UNDONE,
2932 BTMTK_WMT_ON_DONE,
2933 BTMTK_WMT_ON_PROGRESS,
2934};
2935
2936struct btmtk_wmt_hdr {
2937 u8 dir;
2938 u8 op;
2939 __le16 dlen;
2940 u8 flag;
2941} __packed;
2942
2943struct btmtk_hci_wmt_cmd {
2944 struct btmtk_wmt_hdr hdr;
2945 u8 data[256];
2946} __packed;
2947
2948struct btmtk_hci_wmt_evt {
2949 struct hci_event_hdr hhdr;
2950 struct btmtk_wmt_hdr whdr;
2951} __packed;
2952
2953struct btmtk_hci_wmt_evt_funcc {
2954 struct btmtk_hci_wmt_evt hwhdr;
2955 __be16 status;
2956} __packed;
2957
2958struct btmtk_tci_sleep {
2959 u8 mode;
2960 __le16 duration;
2961 __le16 host_duration;
2962 u8 host_wakeup_pin;
2963 u8 time_compensation;
2964} __packed;
2965
2966struct btmtk_hci_wmt_params {
2967 u8 op;
2968 u8 flag;
2969 u16 dlen;
2970 const void *data;
2971 u32 *status;
2972};
2973
2974static void btusb_mtk_wmt_recv(struct urb *urb)
2975{
2976 struct hci_dev *hdev = urb->context;
2977 struct btusb_data *data = hci_get_drvdata(hdev);
2978 struct hci_event_hdr *hdr;
2979 struct sk_buff *skb;
2980 int err;
2981
2982 if (urb->status == 0 && urb->actual_length > 0) {
2983 hdev->stat.byte_rx += urb->actual_length;
2984
2985 /* WMT event shouldn't be fragmented and the size should be
2986 * less than HCI_WMT_MAX_EVENT_SIZE.
2987 */
2988 skb = bt_skb_alloc(HCI_WMT_MAX_EVENT_SIZE, GFP_ATOMIC);
2989 if (!skb) {
2990 hdev->stat.err_rx++;
2991 goto err_out;
2992 }
2993
2994 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
2995 skb_put_data(skb, urb->transfer_buffer, urb->actual_length);
2996
2997 hdr = (void *)skb->data;
2998 /* Fix up the vendor event id with 0xff for vendor specific
2999 * instead of 0xe4 so that event send via monitoring socket can
3000 * be parsed properly.
3001 */
3002 hdr->evt = 0xff;
3003
3004 /* When someone waits for the WMT event, the skb is being cloned
3005 * and being processed the events from there then.
3006 */
3007 if (test_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags)) {
Johan Hovold22cc6b72019-11-28 19:24:27 +01003008 data->evt_skb = skb_clone(skb, GFP_ATOMIC);
Sean Wanga1c49c432019-06-02 08:02:48 +08003009 if (!data->evt_skb)
3010 goto err_out;
3011 }
3012
3013 err = hci_recv_frame(hdev, skb);
3014 if (err < 0)
3015 goto err_free_skb;
3016
3017 if (test_and_clear_bit(BTUSB_TX_WAIT_VND_EVT,
3018 &data->flags)) {
3019 /* Barrier to sync with other CPUs */
3020 smp_mb__after_atomic();
3021 wake_up_bit(&data->flags,
3022 BTUSB_TX_WAIT_VND_EVT);
3023 }
3024err_out:
3025 return;
3026err_free_skb:
3027 kfree_skb(data->evt_skb);
3028 data->evt_skb = NULL;
3029 return;
3030 } else if (urb->status == -ENOENT) {
3031 /* Avoid suspend failed when usb_kill_urb */
3032 return;
3033 }
3034
3035 usb_mark_last_busy(data->udev);
3036
3037 /* The URB complete handler is still called with urb->actual_length = 0
3038 * when the event is not available, so we should keep re-submitting
3039 * URB until WMT event returns, Also, It's necessary to wait some time
3040 * between the two consecutive control URBs to relax the target device
3041 * to generate the event. Otherwise, the WMT event cannot return from
3042 * the device successfully.
3043 */
3044 udelay(100);
3045
3046 usb_anchor_urb(urb, &data->ctrl_anchor);
3047 err = usb_submit_urb(urb, GFP_ATOMIC);
3048 if (err < 0) {
3049 /* -EPERM: urb is being killed;
3050 * -ENODEV: device got disconnected
3051 */
3052 if (err != -EPERM && err != -ENODEV)
3053 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
3054 urb, -err);
3055 usb_unanchor_urb(urb);
3056 }
3057}
3058
3059static int btusb_mtk_submit_wmt_recv_urb(struct hci_dev *hdev)
3060{
3061 struct btusb_data *data = hci_get_drvdata(hdev);
3062 struct usb_ctrlrequest *dr;
3063 unsigned char *buf;
3064 int err, size = 64;
3065 unsigned int pipe;
3066 struct urb *urb;
3067
3068 urb = usb_alloc_urb(0, GFP_KERNEL);
3069 if (!urb)
3070 return -ENOMEM;
3071
3072 dr = kmalloc(sizeof(*dr), GFP_KERNEL);
3073 if (!dr) {
3074 usb_free_urb(urb);
3075 return -ENOMEM;
3076 }
3077
3078 dr->bRequestType = USB_TYPE_VENDOR | USB_DIR_IN;
3079 dr->bRequest = 1;
3080 dr->wIndex = cpu_to_le16(0);
3081 dr->wValue = cpu_to_le16(48);
3082 dr->wLength = cpu_to_le16(size);
3083
3084 buf = kmalloc(size, GFP_KERNEL);
3085 if (!buf) {
3086 kfree(dr);
Dinghao Liud33fe772020-08-23 15:44:21 +08003087 usb_free_urb(urb);
Sean Wanga1c49c432019-06-02 08:02:48 +08003088 return -ENOMEM;
3089 }
3090
3091 pipe = usb_rcvctrlpipe(data->udev, 0);
3092
3093 usb_fill_control_urb(urb, data->udev, pipe, (void *)dr,
3094 buf, size, btusb_mtk_wmt_recv, hdev);
3095
3096 urb->transfer_flags |= URB_FREE_BUFFER;
3097
3098 usb_anchor_urb(urb, &data->ctrl_anchor);
3099 err = usb_submit_urb(urb, GFP_KERNEL);
3100 if (err < 0) {
3101 if (err != -EPERM && err != -ENODEV)
3102 bt_dev_err(hdev, "urb %p submission failed (%d)",
3103 urb, -err);
3104 usb_unanchor_urb(urb);
3105 }
3106
3107 usb_free_urb(urb);
3108
3109 return err;
3110}
3111
3112static int btusb_mtk_hci_wmt_sync(struct hci_dev *hdev,
3113 struct btmtk_hci_wmt_params *wmt_params)
3114{
3115 struct btusb_data *data = hci_get_drvdata(hdev);
3116 struct btmtk_hci_wmt_evt_funcc *wmt_evt_funcc;
3117 u32 hlen, status = BTMTK_WMT_INVALID;
3118 struct btmtk_hci_wmt_evt *wmt_evt;
3119 struct btmtk_hci_wmt_cmd wc;
3120 struct btmtk_wmt_hdr *hdr;
3121 int err;
3122
3123 /* Submit control IN URB on demand to process the WMT event */
3124 err = btusb_mtk_submit_wmt_recv_urb(hdev);
3125 if (err < 0)
3126 return err;
3127
3128 /* Send the WMT command and wait until the WMT event returns */
3129 hlen = sizeof(*hdr) + wmt_params->dlen;
3130 if (hlen > 255)
3131 return -EINVAL;
3132
3133 hdr = (struct btmtk_wmt_hdr *)&wc;
3134 hdr->dir = 1;
3135 hdr->op = wmt_params->op;
3136 hdr->dlen = cpu_to_le16(wmt_params->dlen + 1);
3137 hdr->flag = wmt_params->flag;
3138 memcpy(wc.data, wmt_params->data, wmt_params->dlen);
3139
3140 set_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
3141
3142 err = __hci_cmd_send(hdev, 0xfc6f, hlen, &wc);
3143
3144 if (err < 0) {
3145 clear_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
3146 return err;
3147 }
3148
3149 /* The vendor specific WMT commands are all answered by a vendor
3150 * specific event and will have the Command Status or Command
3151 * Complete as with usual HCI command flow control.
3152 *
3153 * After sending the command, wait for BTUSB_TX_WAIT_VND_EVT
3154 * state to be cleared. The driver specific event receive routine
3155 * will clear that state and with that indicate completion of the
3156 * WMT command.
3157 */
3158 err = wait_on_bit_timeout(&data->flags, BTUSB_TX_WAIT_VND_EVT,
3159 TASK_INTERRUPTIBLE, HCI_INIT_TIMEOUT);
3160 if (err == -EINTR) {
3161 bt_dev_err(hdev, "Execution of wmt command interrupted");
3162 clear_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
3163 return err;
3164 }
3165
3166 if (err) {
3167 bt_dev_err(hdev, "Execution of wmt command timed out");
3168 clear_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
3169 return -ETIMEDOUT;
3170 }
3171
3172 /* Parse and handle the return WMT event */
3173 wmt_evt = (struct btmtk_hci_wmt_evt *)data->evt_skb->data;
3174 if (wmt_evt->whdr.op != hdr->op) {
3175 bt_dev_err(hdev, "Wrong op received %d expected %d",
3176 wmt_evt->whdr.op, hdr->op);
3177 err = -EIO;
3178 goto err_free_skb;
3179 }
3180
3181 switch (wmt_evt->whdr.op) {
3182 case BTMTK_WMT_SEMAPHORE:
3183 if (wmt_evt->whdr.flag == 2)
3184 status = BTMTK_WMT_PATCH_UNDONE;
3185 else
3186 status = BTMTK_WMT_PATCH_DONE;
3187 break;
3188 case BTMTK_WMT_FUNC_CTRL:
3189 wmt_evt_funcc = (struct btmtk_hci_wmt_evt_funcc *)wmt_evt;
3190 if (be16_to_cpu(wmt_evt_funcc->status) == 0x404)
3191 status = BTMTK_WMT_ON_DONE;
3192 else if (be16_to_cpu(wmt_evt_funcc->status) == 0x420)
3193 status = BTMTK_WMT_ON_PROGRESS;
3194 else
3195 status = BTMTK_WMT_ON_UNDONE;
3196 break;
3197 }
3198
3199 if (wmt_params->status)
3200 *wmt_params->status = status;
3201
3202err_free_skb:
3203 kfree_skb(data->evt_skb);
3204 data->evt_skb = NULL;
3205
3206 return err;
3207}
3208
3209static int btusb_mtk_setup_firmware(struct hci_dev *hdev, const char *fwname)
3210{
3211 struct btmtk_hci_wmt_params wmt_params;
3212 const struct firmware *fw;
3213 const u8 *fw_ptr;
3214 size_t fw_size;
3215 int err, dlen;
Sean Wangf6451252020-06-19 19:52:01 +00003216 u8 flag, param;
Sean Wanga1c49c432019-06-02 08:02:48 +08003217
3218 err = request_firmware(&fw, fwname, &hdev->dev);
3219 if (err < 0) {
3220 bt_dev_err(hdev, "Failed to load firmware file (%d)", err);
3221 return err;
3222 }
3223
Sean Wangf6451252020-06-19 19:52:01 +00003224 /* Power on data RAM the firmware relies on. */
3225 param = 1;
3226 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
3227 wmt_params.flag = 3;
3228 wmt_params.dlen = sizeof(param);
3229 wmt_params.data = &param;
3230 wmt_params.status = NULL;
3231
3232 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3233 if (err < 0) {
3234 bt_dev_err(hdev, "Failed to power on data RAM (%d)", err);
Jing Xiangfengd1e9d232020-11-17 10:59:17 +08003235 goto err_release_fw;
Sean Wangf6451252020-06-19 19:52:01 +00003236 }
3237
Sean Wanga1c49c432019-06-02 08:02:48 +08003238 fw_ptr = fw->data;
3239 fw_size = fw->size;
3240
3241 /* The size of patch header is 30 bytes, should be skip */
Wei Yongjun5ee63102019-07-10 06:12:22 +00003242 if (fw_size < 30) {
3243 err = -EINVAL;
Sean Wanga1c49c432019-06-02 08:02:48 +08003244 goto err_release_fw;
Wei Yongjun5ee63102019-07-10 06:12:22 +00003245 }
Sean Wanga1c49c432019-06-02 08:02:48 +08003246
3247 fw_size -= 30;
3248 fw_ptr += 30;
3249 flag = 1;
3250
3251 wmt_params.op = BTMTK_WMT_PATCH_DWNLD;
3252 wmt_params.status = NULL;
3253
3254 while (fw_size > 0) {
3255 dlen = min_t(int, 250, fw_size);
3256
3257 /* Tell deivice the position in sequence */
3258 if (fw_size - dlen <= 0)
3259 flag = 3;
3260 else if (fw_size < fw->size - 30)
3261 flag = 2;
3262
3263 wmt_params.flag = flag;
3264 wmt_params.dlen = dlen;
3265 wmt_params.data = fw_ptr;
3266
3267 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3268 if (err < 0) {
3269 bt_dev_err(hdev, "Failed to send wmt patch dwnld (%d)",
3270 err);
3271 goto err_release_fw;
3272 }
3273
3274 fw_size -= dlen;
3275 fw_ptr += dlen;
3276 }
3277
3278 wmt_params.op = BTMTK_WMT_RST;
3279 wmt_params.flag = 4;
3280 wmt_params.dlen = 0;
3281 wmt_params.data = NULL;
3282 wmt_params.status = NULL;
3283
3284 /* Activate funciton the firmware providing to */
3285 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3286 if (err < 0) {
3287 bt_dev_err(hdev, "Failed to send wmt rst (%d)", err);
Colin Ian King3168c192019-11-29 17:36:35 +00003288 goto err_release_fw;
Sean Wanga1c49c432019-06-02 08:02:48 +08003289 }
3290
3291 /* Wait a few moments for firmware activation done */
3292 usleep_range(10000, 12000);
3293
3294err_release_fw:
3295 release_firmware(fw);
3296
3297 return err;
3298}
3299
3300static int btusb_mtk_func_query(struct hci_dev *hdev)
3301{
3302 struct btmtk_hci_wmt_params wmt_params;
3303 int status, err;
3304 u8 param = 0;
3305
3306 /* Query whether the function is enabled */
3307 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
3308 wmt_params.flag = 4;
3309 wmt_params.dlen = sizeof(param);
3310 wmt_params.data = &param;
3311 wmt_params.status = &status;
3312
3313 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3314 if (err < 0) {
3315 bt_dev_err(hdev, "Failed to query function status (%d)", err);
3316 return err;
3317 }
3318
3319 return status;
3320}
3321
3322static int btusb_mtk_reg_read(struct btusb_data *data, u32 reg, u32 *val)
3323{
3324 int pipe, err, size = sizeof(u32);
3325 void *buf;
3326
3327 buf = kzalloc(size, GFP_KERNEL);
3328 if (!buf)
3329 return -ENOMEM;
3330
3331 pipe = usb_rcvctrlpipe(data->udev, 0);
3332 err = usb_control_msg(data->udev, pipe, 0x63,
3333 USB_TYPE_VENDOR | USB_DIR_IN,
3334 reg >> 16, reg & 0xffff,
3335 buf, size, USB_CTRL_SET_TIMEOUT);
3336 if (err < 0)
3337 goto err_free_buf;
3338
3339 *val = get_unaligned_le32(buf);
3340
3341err_free_buf:
3342 kfree(buf);
3343
3344 return err;
3345}
3346
3347static int btusb_mtk_id_get(struct btusb_data *data, u32 *id)
3348{
3349 return btusb_mtk_reg_read(data, 0x80000008, id);
3350}
3351
3352static int btusb_mtk_setup(struct hci_dev *hdev)
3353{
3354 struct btusb_data *data = hci_get_drvdata(hdev);
3355 struct btmtk_hci_wmt_params wmt_params;
3356 ktime_t calltime, delta, rettime;
3357 struct btmtk_tci_sleep tci_sleep;
3358 unsigned long long duration;
3359 struct sk_buff *skb;
3360 const char *fwname;
3361 int err, status;
3362 u32 dev_id;
3363 u8 param;
3364
3365 calltime = ktime_get();
3366
3367 err = btusb_mtk_id_get(data, &dev_id);
3368 if (err < 0) {
3369 bt_dev_err(hdev, "Failed to get device id (%d)", err);
3370 return err;
3371 }
3372
3373 switch (dev_id) {
Sean Wang9ce67c32019-06-02 08:02:49 +08003374 case 0x7663:
3375 fwname = FIRMWARE_MT7663;
3376 break;
Sean Wanga1c49c432019-06-02 08:02:48 +08003377 case 0x7668:
3378 fwname = FIRMWARE_MT7668;
3379 break;
3380 default:
3381 bt_dev_err(hdev, "Unsupported support hardware variant (%08x)",
3382 dev_id);
3383 return -ENODEV;
3384 }
3385
3386 /* Query whether the firmware is already download */
3387 wmt_params.op = BTMTK_WMT_SEMAPHORE;
3388 wmt_params.flag = 1;
3389 wmt_params.dlen = 0;
3390 wmt_params.data = NULL;
3391 wmt_params.status = &status;
3392
3393 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3394 if (err < 0) {
3395 bt_dev_err(hdev, "Failed to query firmware status (%d)", err);
3396 return err;
3397 }
3398
3399 if (status == BTMTK_WMT_PATCH_DONE) {
3400 bt_dev_info(hdev, "firmware already downloaded");
3401 goto ignore_setup_fw;
3402 }
3403
3404 /* Setup a firmware which the device definitely requires */
3405 err = btusb_mtk_setup_firmware(hdev, fwname);
3406 if (err < 0)
3407 return err;
3408
3409ignore_setup_fw:
3410 err = readx_poll_timeout(btusb_mtk_func_query, hdev, status,
3411 status < 0 || status != BTMTK_WMT_ON_PROGRESS,
3412 2000, 5000000);
3413 /* -ETIMEDOUT happens */
3414 if (err < 0)
3415 return err;
3416
3417 /* The other errors happen in btusb_mtk_func_query */
3418 if (status < 0)
3419 return status;
3420
3421 if (status == BTMTK_WMT_ON_DONE) {
3422 bt_dev_info(hdev, "function already on");
3423 goto ignore_func_on;
3424 }
3425
3426 /* Enable Bluetooth protocol */
3427 param = 1;
3428 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
3429 wmt_params.flag = 0;
3430 wmt_params.dlen = sizeof(param);
3431 wmt_params.data = &param;
3432 wmt_params.status = NULL;
3433
3434 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3435 if (err < 0) {
3436 bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
3437 return err;
3438 }
3439
3440ignore_func_on:
3441 /* Apply the low power environment setup */
3442 tci_sleep.mode = 0x5;
3443 tci_sleep.duration = cpu_to_le16(0x640);
3444 tci_sleep.host_duration = cpu_to_le16(0x640);
3445 tci_sleep.host_wakeup_pin = 0;
3446 tci_sleep.time_compensation = 0;
3447
3448 skb = __hci_cmd_sync(hdev, 0xfc7a, sizeof(tci_sleep), &tci_sleep,
3449 HCI_INIT_TIMEOUT);
3450 if (IS_ERR(skb)) {
3451 err = PTR_ERR(skb);
3452 bt_dev_err(hdev, "Failed to apply low power setting (%d)", err);
3453 return err;
3454 }
3455 kfree_skb(skb);
3456
3457 rettime = ktime_get();
3458 delta = ktime_sub(rettime, calltime);
3459 duration = (unsigned long long)ktime_to_ns(delta) >> 10;
3460
3461 bt_dev_info(hdev, "Device setup in %llu usecs", duration);
3462
3463 return 0;
3464}
3465
3466static int btusb_mtk_shutdown(struct hci_dev *hdev)
3467{
3468 struct btmtk_hci_wmt_params wmt_params;
3469 u8 param = 0;
3470 int err;
3471
3472 /* Disable the device */
3473 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
3474 wmt_params.flag = 0;
3475 wmt_params.dlen = sizeof(param);
3476 wmt_params.data = &param;
3477 wmt_params.status = NULL;
3478
3479 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3480 if (err < 0) {
3481 bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
3482 return err;
3483 }
3484
3485 return 0;
3486}
3487
Sean Wang9ce67c32019-06-02 08:02:49 +08003488MODULE_FIRMWARE(FIRMWARE_MT7663);
Sean Wanga1c49c432019-06-02 08:02:48 +08003489MODULE_FIRMWARE(FIRMWARE_MT7668);
Sean Wanga1c49c432019-06-02 08:02:48 +08003490
Rajat Jaina4ccc9e2017-02-01 14:24:10 -08003491#ifdef CONFIG_PM
3492/* Configure an out-of-band gpio as wake-up pin, if specified in device tree */
3493static int marvell_config_oob_wake(struct hci_dev *hdev)
3494{
3495 struct sk_buff *skb;
3496 struct btusb_data *data = hci_get_drvdata(hdev);
3497 struct device *dev = &data->udev->dev;
3498 u16 pin, gap, opcode;
3499 int ret;
3500 u8 cmd[5];
3501
3502 /* Move on if no wakeup pin specified */
3503 if (of_property_read_u16(dev->of_node, "marvell,wakeup-pin", &pin) ||
3504 of_property_read_u16(dev->of_node, "marvell,wakeup-gap-ms", &gap))
3505 return 0;
3506
3507 /* Vendor specific command to configure a GPIO as wake-up pin */
3508 opcode = hci_opcode_pack(0x3F, 0x59);
3509 cmd[0] = opcode & 0xFF;
3510 cmd[1] = opcode >> 8;
3511 cmd[2] = 2; /* length of parameters that follow */
3512 cmd[3] = pin;
3513 cmd[4] = gap; /* time in ms, for which wakeup pin should be asserted */
3514
3515 skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL);
3516 if (!skb) {
3517 bt_dev_err(hdev, "%s: No memory\n", __func__);
3518 return -ENOMEM;
3519 }
3520
Johannes Berg59ae1d12017-06-16 14:29:20 +02003521 skb_put_data(skb, cmd, sizeof(cmd));
Rajat Jaina4ccc9e2017-02-01 14:24:10 -08003522 hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
3523
3524 ret = btusb_send_frame(hdev, skb);
3525 if (ret) {
3526 bt_dev_err(hdev, "%s: configuration failed\n", __func__);
3527 kfree_skb(skb);
3528 return ret;
3529 }
3530
3531 return 0;
3532}
3533#endif
3534
Amitkumar Karwarae8df492014-07-18 14:47:06 -07003535static int btusb_set_bdaddr_marvell(struct hci_dev *hdev,
3536 const bdaddr_t *bdaddr)
3537{
3538 struct sk_buff *skb;
3539 u8 buf[8];
3540 long ret;
3541
3542 buf[0] = 0xfe;
3543 buf[1] = sizeof(bdaddr_t);
3544 memcpy(buf + 2, bdaddr, sizeof(bdaddr_t));
3545
3546 skb = __hci_cmd_sync(hdev, 0xfc22, sizeof(buf), buf, HCI_INIT_TIMEOUT);
3547 if (IS_ERR(skb)) {
3548 ret = PTR_ERR(skb);
Marcel Holtmann2064ee32017-10-30 10:42:59 +01003549 bt_dev_err(hdev, "changing Marvell device address failed (%ld)",
3550 ret);
Amitkumar Karwarae8df492014-07-18 14:47:06 -07003551 return ret;
3552 }
3553 kfree_skb(skb);
3554
3555 return 0;
3556}
3557
Toshi Kikuchi58592232014-12-12 10:58:05 -08003558static int btusb_set_bdaddr_ath3012(struct hci_dev *hdev,
3559 const bdaddr_t *bdaddr)
3560{
3561 struct sk_buff *skb;
3562 u8 buf[10];
3563 long ret;
3564
3565 buf[0] = 0x01;
3566 buf[1] = 0x01;
3567 buf[2] = 0x00;
3568 buf[3] = sizeof(bdaddr_t);
3569 memcpy(buf + 4, bdaddr, sizeof(bdaddr_t));
3570
3571 skb = __hci_cmd_sync(hdev, 0xfc0b, sizeof(buf), buf, HCI_INIT_TIMEOUT);
3572 if (IS_ERR(skb)) {
3573 ret = PTR_ERR(skb);
Marcel Holtmann2064ee32017-10-30 10:42:59 +01003574 bt_dev_err(hdev, "Change address command failed (%ld)", ret);
Toshi Kikuchi58592232014-12-12 10:58:05 -08003575 return ret;
3576 }
3577 kfree_skb(skb);
3578
3579 return 0;
3580}
3581
Rocky Liaob40f58b2020-09-29 12:23:51 +08003582static int btusb_set_bdaddr_wcn6855(struct hci_dev *hdev,
3583 const bdaddr_t *bdaddr)
3584{
3585 struct sk_buff *skb;
3586 u8 buf[6];
3587 long ret;
3588
3589 memcpy(buf, bdaddr, sizeof(bdaddr_t));
3590
3591 skb = __hci_cmd_sync_ev(hdev, 0xfc14, sizeof(buf), buf,
3592 HCI_EV_CMD_COMPLETE, HCI_INIT_TIMEOUT);
3593 if (IS_ERR(skb)) {
3594 ret = PTR_ERR(skb);
3595 bt_dev_err(hdev, "Change address command failed (%ld)", ret);
3596 return ret;
3597 }
3598 kfree_skb(skb);
3599
3600 return 0;
3601}
3602
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003603#define QCA_DFU_PACKET_LEN 4096
3604
3605#define QCA_GET_TARGET_VERSION 0x09
3606#define QCA_CHECK_STATUS 0x05
3607#define QCA_DFU_DOWNLOAD 0x01
3608
3609#define QCA_SYSCFG_UPDATED 0x40
3610#define QCA_PATCH_UPDATED 0x80
3611#define QCA_DFU_TIMEOUT 3000
3612
3613struct qca_version {
3614 __le32 rom_version;
3615 __le32 patch_version;
3616 __le32 ram_version;
3617 __le32 ref_clock;
3618 __u8 reserved[4];
3619} __packed;
3620
3621struct qca_rampatch_version {
Rocky Liaob40f58b2020-09-29 12:23:51 +08003622 __le16 rom_version_high;
3623 __le16 rom_version_low;
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003624 __le16 patch_version;
3625} __packed;
3626
3627struct qca_device_info {
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00003628 u32 rom_version;
3629 u8 rampatch_hdr; /* length of header in rampatch */
3630 u8 nvm_hdr; /* length of header in NVM */
3631 u8 ver_offset; /* offset of version structure in rampatch */
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003632};
3633
3634static const struct qca_device_info qca_devices_table[] = {
Rocky Liaob40f58b2020-09-29 12:23:51 +08003635 { 0x00000100, 20, 4, 8 }, /* Rome 1.0 */
3636 { 0x00000101, 20, 4, 8 }, /* Rome 1.1 */
3637 { 0x00000200, 28, 4, 16 }, /* Rome 2.0 */
3638 { 0x00000201, 28, 4, 16 }, /* Rome 2.1 */
3639 { 0x00000300, 28, 4, 16 }, /* Rome 3.0 */
3640 { 0x00000302, 28, 4, 16 }, /* Rome 3.2 */
3641 { 0x00130100, 40, 4, 16 }, /* WCN6855 1.0 */
3642 { 0x00130200, 40, 4, 16 }, /* WCN6855 2.0 */
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003643};
3644
Takashi Iwai803cdb82018-05-21 22:34:52 +02003645static int btusb_qca_send_vendor_req(struct usb_device *udev, u8 request,
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003646 void *data, u16 size)
3647{
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003648 int pipe, err;
3649 u8 *buf;
3650
3651 buf = kmalloc(size, GFP_KERNEL);
3652 if (!buf)
3653 return -ENOMEM;
3654
3655 /* Found some of USB hosts have IOT issues with ours so that we should
3656 * not wait until HCI layer is ready.
3657 */
3658 pipe = usb_rcvctrlpipe(udev, 0);
3659 err = usb_control_msg(udev, pipe, request, USB_TYPE_VENDOR | USB_DIR_IN,
3660 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
3661 if (err < 0) {
Takashi Iwai803cdb82018-05-21 22:34:52 +02003662 dev_err(&udev->dev, "Failed to access otp area (%d)", err);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003663 goto done;
3664 }
3665
3666 memcpy(data, buf, size);
3667
3668done:
3669 kfree(buf);
3670
3671 return err;
3672}
3673
3674static int btusb_setup_qca_download_fw(struct hci_dev *hdev,
3675 const struct firmware *firmware,
3676 size_t hdr_size)
3677{
3678 struct btusb_data *btdata = hci_get_drvdata(hdev);
3679 struct usb_device *udev = btdata->udev;
3680 size_t count, size, sent = 0;
3681 int pipe, len, err;
3682 u8 *buf;
3683
3684 buf = kmalloc(QCA_DFU_PACKET_LEN, GFP_KERNEL);
3685 if (!buf)
3686 return -ENOMEM;
3687
3688 count = firmware->size;
3689
3690 size = min_t(size_t, count, hdr_size);
3691 memcpy(buf, firmware->data, size);
3692
3693 /* USB patches should go down to controller through USB path
3694 * because binary format fits to go down through USB channel.
3695 * USB control path is for patching headers and USB bulk is for
3696 * patch body.
3697 */
3698 pipe = usb_sndctrlpipe(udev, 0);
3699 err = usb_control_msg(udev, pipe, QCA_DFU_DOWNLOAD, USB_TYPE_VENDOR,
3700 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
3701 if (err < 0) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01003702 bt_dev_err(hdev, "Failed to send headers (%d)", err);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003703 goto done;
3704 }
3705
3706 sent += size;
3707 count -= size;
3708
3709 while (count) {
3710 size = min_t(size_t, count, QCA_DFU_PACKET_LEN);
3711
3712 memcpy(buf, firmware->data + sent, size);
3713
3714 pipe = usb_sndbulkpipe(udev, 0x02);
3715 err = usb_bulk_msg(udev, pipe, buf, size, &len,
3716 QCA_DFU_TIMEOUT);
3717 if (err < 0) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01003718 bt_dev_err(hdev, "Failed to send body at %zd of %zd (%d)",
3719 sent, firmware->size, err);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003720 break;
3721 }
3722
3723 if (size != len) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01003724 bt_dev_err(hdev, "Failed to get bulk buffer");
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003725 err = -EILSEQ;
3726 break;
3727 }
3728
3729 sent += size;
3730 count -= size;
3731 }
3732
3733done:
3734 kfree(buf);
3735 return err;
3736}
3737
3738static int btusb_setup_qca_load_rampatch(struct hci_dev *hdev,
3739 struct qca_version *ver,
3740 const struct qca_device_info *info)
3741{
3742 struct qca_rampatch_version *rver;
3743 const struct firmware *fw;
Rocky Liaob40f58b2020-09-29 12:23:51 +08003744 u32 ver_rom, ver_patch, rver_rom;
3745 u16 rver_rom_low, rver_rom_high, rver_patch;
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003746 char fwname[64];
3747 int err;
3748
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00003749 ver_rom = le32_to_cpu(ver->rom_version);
3750 ver_patch = le32_to_cpu(ver->patch_version);
3751
3752 snprintf(fwname, sizeof(fwname), "qca/rampatch_usb_%08x.bin", ver_rom);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003753
3754 err = request_firmware(&fw, fwname, &hdev->dev);
3755 if (err) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01003756 bt_dev_err(hdev, "failed to request rampatch file: %s (%d)",
3757 fwname, err);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003758 return err;
3759 }
3760
Marcel Holtmann2064ee32017-10-30 10:42:59 +01003761 bt_dev_info(hdev, "using rampatch file: %s", fwname);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003762
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00003763 rver = (struct qca_rampatch_version *)(fw->data + info->ver_offset);
Rocky Liaob40f58b2020-09-29 12:23:51 +08003764 rver_rom_low = le16_to_cpu(rver->rom_version_low);
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00003765 rver_patch = le16_to_cpu(rver->patch_version);
3766
Rocky Liaob40f58b2020-09-29 12:23:51 +08003767 if (ver_rom & ~0xffffU) {
3768 rver_rom_high = le16_to_cpu(rver->rom_version_high);
3769 rver_rom = le32_to_cpu(rver_rom_high << 16 | rver_rom_low);
3770 } else {
3771 rver_rom = rver_rom_low;
3772 }
3773
Marcel Holtmann2064ee32017-10-30 10:42:59 +01003774 bt_dev_info(hdev, "QCA: patch rome 0x%x build 0x%x, "
3775 "firmware rome 0x%x build 0x%x",
3776 rver_rom, rver_patch, ver_rom, ver_patch);
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00003777
3778 if (rver_rom != ver_rom || rver_patch <= ver_patch) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01003779 bt_dev_err(hdev, "rampatch file version did not match with firmware");
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003780 err = -EINVAL;
3781 goto done;
3782 }
3783
3784 err = btusb_setup_qca_download_fw(hdev, fw, info->rampatch_hdr);
3785
3786done:
3787 release_firmware(fw);
3788
3789 return err;
3790}
3791
3792static int btusb_setup_qca_load_nvm(struct hci_dev *hdev,
3793 struct qca_version *ver,
3794 const struct qca_device_info *info)
3795{
3796 const struct firmware *fw;
3797 char fwname[64];
3798 int err;
3799
3800 snprintf(fwname, sizeof(fwname), "qca/nvm_usb_%08x.bin",
3801 le32_to_cpu(ver->rom_version));
3802
3803 err = request_firmware(&fw, fwname, &hdev->dev);
3804 if (err) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01003805 bt_dev_err(hdev, "failed to request NVM file: %s (%d)",
3806 fwname, err);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003807 return err;
3808 }
3809
Marcel Holtmann2064ee32017-10-30 10:42:59 +01003810 bt_dev_info(hdev, "using NVM file: %s", fwname);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003811
3812 err = btusb_setup_qca_download_fw(hdev, fw, info->nvm_hdr);
3813
3814 release_firmware(fw);
3815
3816 return err;
3817}
3818
Takashi Iwai803cdb82018-05-21 22:34:52 +02003819/* identify the ROM version and check whether patches are needed */
3820static bool btusb_qca_need_patch(struct usb_device *udev)
3821{
3822 struct qca_version ver;
3823
3824 if (btusb_qca_send_vendor_req(udev, QCA_GET_TARGET_VERSION, &ver,
3825 sizeof(ver)) < 0)
3826 return false;
3827 /* only low ROM versions need patches */
3828 return !(le32_to_cpu(ver.rom_version) & ~0xffffU);
3829}
3830
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003831static int btusb_setup_qca(struct hci_dev *hdev)
3832{
Takashi Iwai803cdb82018-05-21 22:34:52 +02003833 struct btusb_data *btdata = hci_get_drvdata(hdev);
3834 struct usb_device *udev = btdata->udev;
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003835 const struct qca_device_info *info = NULL;
3836 struct qca_version ver;
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00003837 u32 ver_rom;
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003838 u8 status;
3839 int i, err;
3840
Takashi Iwai803cdb82018-05-21 22:34:52 +02003841 err = btusb_qca_send_vendor_req(udev, QCA_GET_TARGET_VERSION, &ver,
Marcel Holtmanneb500422015-04-16 23:15:50 +02003842 sizeof(ver));
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003843 if (err < 0)
3844 return err;
3845
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00003846 ver_rom = le32_to_cpu(ver.rom_version);
Takashi Iwai803cdb82018-05-21 22:34:52 +02003847
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003848 for (i = 0; i < ARRAY_SIZE(qca_devices_table); i++) {
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00003849 if (ver_rom == qca_devices_table[i].rom_version)
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003850 info = &qca_devices_table[i];
3851 }
3852 if (!info) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01003853 bt_dev_err(hdev, "don't support firmware rome 0x%x", ver_rom);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003854 return -ENODEV;
3855 }
3856
Takashi Iwai803cdb82018-05-21 22:34:52 +02003857 err = btusb_qca_send_vendor_req(udev, QCA_CHECK_STATUS, &status,
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003858 sizeof(status));
3859 if (err < 0)
3860 return err;
3861
3862 if (!(status & QCA_PATCH_UPDATED)) {
3863 err = btusb_setup_qca_load_rampatch(hdev, &ver, info);
3864 if (err < 0)
3865 return err;
3866 }
3867
3868 if (!(status & QCA_SYSCFG_UPDATED)) {
3869 err = btusb_setup_qca_load_nvm(hdev, &ver, info);
3870 if (err < 0)
3871 return err;
3872 }
3873
3874 return 0;
3875}
3876
Marcel Holtmann9d08f502015-10-08 20:23:08 +02003877static inline int __set_diag_interface(struct hci_dev *hdev)
3878{
3879 struct btusb_data *data = hci_get_drvdata(hdev);
3880 struct usb_interface *intf = data->diag;
3881 int i;
3882
3883 if (!data->diag)
3884 return -ENODEV;
3885
3886 data->diag_tx_ep = NULL;
3887 data->diag_rx_ep = NULL;
3888
3889 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
3890 struct usb_endpoint_descriptor *ep_desc;
3891
3892 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
3893
3894 if (!data->diag_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
3895 data->diag_tx_ep = ep_desc;
3896 continue;
3897 }
3898
3899 if (!data->diag_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
3900 data->diag_rx_ep = ep_desc;
3901 continue;
3902 }
3903 }
3904
3905 if (!data->diag_tx_ep || !data->diag_rx_ep) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01003906 bt_dev_err(hdev, "invalid diagnostic descriptors");
Marcel Holtmann9d08f502015-10-08 20:23:08 +02003907 return -ENODEV;
3908 }
3909
3910 return 0;
3911}
3912
3913static struct urb *alloc_diag_urb(struct hci_dev *hdev, bool enable)
3914{
3915 struct btusb_data *data = hci_get_drvdata(hdev);
3916 struct sk_buff *skb;
3917 struct urb *urb;
3918 unsigned int pipe;
3919
3920 if (!data->diag_tx_ep)
3921 return ERR_PTR(-ENODEV);
3922
3923 urb = usb_alloc_urb(0, GFP_KERNEL);
3924 if (!urb)
3925 return ERR_PTR(-ENOMEM);
3926
3927 skb = bt_skb_alloc(2, GFP_KERNEL);
3928 if (!skb) {
3929 usb_free_urb(urb);
3930 return ERR_PTR(-ENOMEM);
3931 }
3932
Johannes Berg634fef62017-06-16 14:29:24 +02003933 skb_put_u8(skb, 0xf0);
3934 skb_put_u8(skb, enable);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02003935
3936 pipe = usb_sndbulkpipe(data->udev, data->diag_tx_ep->bEndpointAddress);
3937
3938 usb_fill_bulk_urb(urb, data->udev, pipe,
3939 skb->data, skb->len, btusb_tx_complete, skb);
3940
3941 skb->dev = (void *)hdev;
3942
3943 return urb;
3944}
3945
3946static int btusb_bcm_set_diag(struct hci_dev *hdev, bool enable)
3947{
3948 struct btusb_data *data = hci_get_drvdata(hdev);
3949 struct urb *urb;
3950
3951 if (!data->diag)
3952 return -ENODEV;
3953
3954 if (!test_bit(HCI_RUNNING, &hdev->flags))
3955 return -ENETDOWN;
3956
3957 urb = alloc_diag_urb(hdev, enable);
3958 if (IS_ERR(urb))
3959 return PTR_ERR(urb);
3960
3961 return submit_or_queue_tx_urb(hdev, urb);
3962}
Marcel Holtmann9d08f502015-10-08 20:23:08 +02003963
Rajat Jainfd913ef2017-02-01 14:24:09 -08003964#ifdef CONFIG_PM
3965static irqreturn_t btusb_oob_wake_handler(int irq, void *priv)
3966{
3967 struct btusb_data *data = priv;
3968
3969 pm_wakeup_event(&data->udev->dev, 0);
Jeffy Chen017789f2017-02-24 14:24:29 +08003970 pm_system_wakeup();
Rajat Jainfd913ef2017-02-01 14:24:09 -08003971
3972 /* Disable only if not already disabled (keep it balanced) */
3973 if (test_and_clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags)) {
3974 disable_irq_nosync(irq);
3975 disable_irq_wake(irq);
3976 }
3977 return IRQ_HANDLED;
3978}
3979
3980static const struct of_device_id btusb_match_table[] = {
3981 { .compatible = "usb1286,204e" },
Brian Norris4c409af2019-02-22 14:53:43 -08003982 { .compatible = "usbcf3,e300" }, /* QCA6174A */
3983 { .compatible = "usb4ca,301a" }, /* QCA6174A (Lite-On) */
Rajat Jainfd913ef2017-02-01 14:24:09 -08003984 { }
3985};
3986MODULE_DEVICE_TABLE(of, btusb_match_table);
3987
3988/* Use an oob wakeup pin? */
3989static int btusb_config_oob_wake(struct hci_dev *hdev)
3990{
3991 struct btusb_data *data = hci_get_drvdata(hdev);
3992 struct device *dev = &data->udev->dev;
3993 int irq, ret;
3994
3995 clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags);
3996
3997 if (!of_match_device(btusb_match_table, dev))
3998 return 0;
3999
4000 /* Move on if no IRQ specified */
4001 irq = of_irq_get_byname(dev->of_node, "wakeup");
4002 if (irq <= 0) {
4003 bt_dev_dbg(hdev, "%s: no OOB Wakeup IRQ in DT", __func__);
4004 return 0;
4005 }
4006
Brian Norris771acc72019-04-09 11:49:17 -07004007 irq_set_status_flags(irq, IRQ_NOAUTOEN);
Rajat Jainfd913ef2017-02-01 14:24:09 -08004008 ret = devm_request_irq(&hdev->dev, irq, btusb_oob_wake_handler,
4009 0, "OOB Wake-on-BT", data);
4010 if (ret) {
4011 bt_dev_err(hdev, "%s: IRQ request failed", __func__);
4012 return ret;
4013 }
4014
4015 ret = device_init_wakeup(dev, true);
4016 if (ret) {
4017 bt_dev_err(hdev, "%s: failed to init_wakeup", __func__);
4018 return ret;
4019 }
4020
4021 data->oob_wake_irq = irq;
Rajat Jainfd913ef2017-02-01 14:24:09 -08004022 bt_dev_info(hdev, "OOB Wake-on-BT configured at IRQ %u", irq);
4023 return 0;
4024}
4025#endif
4026
Hans de Goedefc549102018-04-27 11:26:43 +02004027static void btusb_check_needs_reset_resume(struct usb_interface *intf)
4028{
4029 if (dmi_check_system(btusb_needs_reset_resume_table))
4030 interface_to_usbdev(intf)->quirks |= USB_QUIRK_RESET_RESUME;
4031}
4032
Abhishek Pandit-Subedib7d0bf12020-05-12 19:19:27 -07004033static bool btusb_prevent_wake(struct hci_dev *hdev)
4034{
4035 struct btusb_data *data = hci_get_drvdata(hdev);
4036
Abhishek Pandit-Subedi34a68652020-06-24 11:24:30 -07004037 if (test_bit(BTUSB_WAKEUP_DISABLE, &data->flags))
4038 return true;
4039
Abhishek Pandit-Subedib7d0bf12020-05-12 19:19:27 -07004040 return !device_may_wakeup(&data->udev->dev);
4041}
4042
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004043static int btusb_probe(struct usb_interface *intf,
Marcel Holtmann89e75332014-09-16 04:44:50 +02004044 const struct usb_device_id *id)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004045{
4046 struct usb_endpoint_descriptor *ep_desc;
Rajat Jaindc786b22019-01-24 15:28:14 -08004047 struct gpio_desc *reset_gpio;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004048 struct btusb_data *data;
4049 struct hci_dev *hdev;
Marcel Holtmann22f8e9d2015-10-20 00:53:33 +02004050 unsigned ifnum_base;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004051 int i, err;
4052
4053 BT_DBG("intf %p id %p", intf, id);
4054
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02004055 /* interface numbers are hardcoded in the spec */
Marcel Holtmann22f8e9d2015-10-20 00:53:33 +02004056 if (intf->cur_altsetting->desc.bInterfaceNumber != 0) {
4057 if (!(id->driver_info & BTUSB_IFNUM_2))
4058 return -ENODEV;
4059 if (intf->cur_altsetting->desc.bInterfaceNumber != 2)
4060 return -ENODEV;
4061 }
4062
4063 ifnum_base = intf->cur_altsetting->desc.bInterfaceNumber;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004064
4065 if (!id->driver_info) {
4066 const struct usb_device_id *match;
Marcel Holtmann89e75332014-09-16 04:44:50 +02004067
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004068 match = usb_match_id(intf, blacklist_table);
4069 if (match)
4070 id = match;
4071 }
4072
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02004073 if (id->driver_info == BTUSB_IGNORE)
4074 return -ENODEV;
4075
Steven.Li2d25f8b2011-07-01 14:02:36 +08004076 if (id->driver_info & BTUSB_ATH3012) {
4077 struct usb_device *udev = interface_to_usbdev(intf);
4078
4079 /* Old firmware would otherwise let ath3k driver load
Derek Robsond98422c2017-07-22 13:47:07 +12004080 * patch and sysconfig files
4081 */
Takashi Iwai803cdb82018-05-21 22:34:52 +02004082 if (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x0001 &&
4083 !btusb_qca_need_patch(udev))
Steven.Li2d25f8b2011-07-01 14:02:36 +08004084 return -ENODEV;
4085 }
4086
Sachin Kamat98921db2012-07-27 12:38:39 +05304087 data = devm_kzalloc(&intf->dev, sizeof(*data), GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004088 if (!data)
4089 return -ENOMEM;
4090
4091 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
4092 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
4093
4094 if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
4095 data->intr_ep = ep_desc;
4096 continue;
4097 }
4098
4099 if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
4100 data->bulk_tx_ep = ep_desc;
4101 continue;
4102 }
4103
4104 if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
4105 data->bulk_rx_ep = ep_desc;
4106 continue;
4107 }
4108 }
4109
Sachin Kamat98921db2012-07-27 12:38:39 +05304110 if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004111 return -ENODEV;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004112
Marcel Holtmann893ba542015-01-28 20:27:34 -08004113 if (id->driver_info & BTUSB_AMP) {
4114 data->cmdreq_type = USB_TYPE_CLASS | 0x01;
4115 data->cmdreq = 0x2b;
4116 } else {
4117 data->cmdreq_type = USB_TYPE_CLASS;
4118 data->cmdreq = 0x00;
4119 }
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01004120
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004121 data->udev = interface_to_usbdev(intf);
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02004122 data->intf = intf;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004123
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004124 INIT_WORK(&data->work, btusb_work);
Oliver Neukum7bee5492009-08-24 23:44:59 +02004125 INIT_WORK(&data->waker, btusb_waker);
Marcel Holtmann803b5832014-09-16 08:00:29 +02004126 init_usb_anchor(&data->deferred);
4127 init_usb_anchor(&data->tx_anchor);
Oliver Neukum7bee5492009-08-24 23:44:59 +02004128 spin_lock_init(&data->txlock);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004129
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004130 init_usb_anchor(&data->intr_anchor);
4131 init_usb_anchor(&data->bulk_anchor);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02004132 init_usb_anchor(&data->isoc_anchor);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02004133 init_usb_anchor(&data->diag_anchor);
Sean Wanga1c49c432019-06-02 08:02:48 +08004134 init_usb_anchor(&data->ctrl_anchor);
Marcel Holtmann803b5832014-09-16 08:00:29 +02004135 spin_lock_init(&data->rxlock);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004136
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08004137 if (id->driver_info & BTUSB_INTEL_NEW) {
4138 data->recv_event = btusb_recv_event_intel;
4139 data->recv_bulk = btusb_recv_bulk_intel;
4140 set_bit(BTUSB_BOOTLOADER, &data->flags);
4141 } else {
4142 data->recv_event = hci_recv_frame;
4143 data->recv_bulk = btusb_recv_bulk;
4144 }
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +01004145
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004146 hdev = hci_alloc_dev();
Sachin Kamat98921db2012-07-27 12:38:39 +05304147 if (!hdev)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004148 return -ENOMEM;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004149
Marcel Holtmannc13854c2010-02-08 15:27:07 +01004150 hdev->bus = HCI_USB;
David Herrmann155961e2012-02-09 21:58:32 +01004151 hci_set_drvdata(hdev, data);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004152
Marcel Holtmann893ba542015-01-28 20:27:34 -08004153 if (id->driver_info & BTUSB_AMP)
4154 hdev->dev_type = HCI_AMP;
4155 else
Marcel Holtmannca8bee52016-07-05 14:30:14 +02004156 hdev->dev_type = HCI_PRIMARY;
Marcel Holtmann893ba542015-01-28 20:27:34 -08004157
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004158 data->hdev = hdev;
4159
4160 SET_HCIDEV_DEV(hdev, &intf->dev);
4161
Rajat Jaindc786b22019-01-24 15:28:14 -08004162 reset_gpio = gpiod_get_optional(&data->udev->dev, "reset",
4163 GPIOD_OUT_LOW);
4164 if (IS_ERR(reset_gpio)) {
4165 err = PTR_ERR(reset_gpio);
4166 goto out_free_dev;
4167 } else if (reset_gpio) {
4168 data->reset_gpio = reset_gpio;
4169 }
4170
Marcel Holtmann9f8f9622013-04-10 08:11:35 -07004171 hdev->open = btusb_open;
4172 hdev->close = btusb_close;
4173 hdev->flush = btusb_flush;
4174 hdev->send = btusb_send_frame;
4175 hdev->notify = btusb_notify;
Abhishek Pandit-Subedib7d0bf12020-05-12 19:19:27 -07004176 hdev->prevent_wake = btusb_prevent_wake;
Marcel Holtmann9f8f9622013-04-10 08:11:35 -07004177
Rajat Jainfd913ef2017-02-01 14:24:09 -08004178#ifdef CONFIG_PM
4179 err = btusb_config_oob_wake(hdev);
4180 if (err)
4181 goto out_free_dev;
Rajat Jaina4ccc9e2017-02-01 14:24:10 -08004182
4183 /* Marvell devices may need a specific chip configuration */
4184 if (id->driver_info & BTUSB_MARVELL && data->oob_wake_irq) {
4185 err = marvell_config_oob_wake(hdev);
4186 if (err)
4187 goto out_free_dev;
4188 }
Rajat Jainfd913ef2017-02-01 14:24:09 -08004189#endif
Szymon Janc418678b2016-09-01 17:22:37 +02004190 if (id->driver_info & BTUSB_CW6622)
4191 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
4192
Marcel Holtmann6c9d4352015-10-17 14:39:27 +02004193 if (id->driver_info & BTUSB_BCM2045)
4194 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
4195
Marcel Holtmann9f8f9622013-04-10 08:11:35 -07004196 if (id->driver_info & BTUSB_BCM92035)
4197 hdev->setup = btusb_setup_bcm92035;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004198
Marcel Holtmannc0a21a52019-09-27 08:48:58 +02004199 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_BCM) &&
4200 (id->driver_info & BTUSB_BCM_PATCHRAM)) {
Marcel Holtmann49a5f782015-10-20 02:30:48 +02004201 hdev->manufacturer = 15;
Marcel Holtmannc2bfb102015-04-05 22:52:14 -07004202 hdev->setup = btbcm_setup_patchram;
Marcel Holtmann9d08f502015-10-08 20:23:08 +02004203 hdev->set_diag = btusb_bcm_set_diag;
Marcel Holtmann1df1f592015-04-05 22:52:11 -07004204 hdev->set_bdaddr = btbcm_set_bdaddr;
Marcel Holtmann9d08f502015-10-08 20:23:08 +02004205
4206 /* Broadcom LM_DIAG Interface numbers are hardcoded */
Marcel Holtmann22f8e9d2015-10-20 00:53:33 +02004207 data->diag = usb_ifnum_to_if(data->udev, ifnum_base + 2);
Marcel Holtmannabbaf502014-07-02 00:53:48 +02004208 }
Petri Gynther10d4c672014-05-08 15:50:01 -07004209
Marcel Holtmannc0a21a52019-09-27 08:48:58 +02004210 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_BCM) &&
4211 (id->driver_info & BTUSB_BCM_APPLE)) {
Marcel Holtmann49a5f782015-10-20 02:30:48 +02004212 hdev->manufacturer = 15;
Marcel Holtmannc2bfb102015-04-05 22:52:14 -07004213 hdev->setup = btbcm_setup_apple;
Marcel Holtmann9d08f502015-10-08 20:23:08 +02004214 hdev->set_diag = btusb_bcm_set_diag;
4215
4216 /* Broadcom LM_DIAG Interface numbers are hardcoded */
Marcel Holtmann22f8e9d2015-10-20 00:53:33 +02004217 data->diag = usb_ifnum_to_if(data->udev, ifnum_base + 2);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02004218 }
Marcel Holtmann17b27722015-03-22 15:52:38 +01004219
Marcel Holtmanncb8d6592014-07-02 11:25:25 +02004220 if (id->driver_info & BTUSB_INTEL) {
Marcel Holtmann49a5f782015-10-20 02:30:48 +02004221 hdev->manufacturer = 2;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07004222 hdev->setup = btusb_setup_intel;
Tedd Ho-Jeong Anbfbd45e2015-02-13 09:20:52 -08004223 hdev->shutdown = btusb_shutdown_intel;
Marcel Holtmann3e247672015-10-17 16:00:28 +02004224 hdev->set_diag = btintel_set_diag_mfg;
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07004225 hdev->set_bdaddr = btintel_set_bdaddr;
Rajat Jaindc786b22019-01-24 15:28:14 -08004226 hdev->cmd_timeout = btusb_intel_cmd_timeout;
Jakub Pawlowskic33fb9b2015-01-30 18:55:58 -08004227 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
Jakub Pawlowskic1154842015-03-17 09:04:16 -07004228 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
Marcel Holtmann3e247672015-10-17 16:00:28 +02004229 set_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks);
Marcel Holtmanncb8d6592014-07-02 11:25:25 +02004230 }
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07004231
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08004232 if (id->driver_info & BTUSB_INTEL_NEW) {
Marcel Holtmann49a5f782015-10-20 02:30:48 +02004233 hdev->manufacturer = 2;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08004234 hdev->send = btusb_send_frame_intel;
4235 hdev->setup = btusb_setup_intel_new;
Raghuram Hegde017a01c2019-01-29 17:54:48 +05304236 hdev->shutdown = btusb_shutdown_intel_new;
Marcel Holtmanneeb6abe2015-07-05 14:37:39 +02004237 hdev->hw_error = btintel_hw_error;
Marcel Holtmann6d2e50d2015-10-09 14:42:08 +02004238 hdev->set_diag = btintel_set_diag;
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07004239 hdev->set_bdaddr = btintel_set_bdaddr;
Rajat Jaindc786b22019-01-24 15:28:14 -08004240 hdev->cmd_timeout = btusb_intel_cmd_timeout;
Marcel Holtmannb970c5b2015-02-01 23:57:18 -08004241 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
Justin TerAvestf7785022018-09-25 11:04:57 -06004242 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
Marcel Holtmannd8270fb2015-10-17 16:00:27 +02004243 set_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08004244 }
4245
Kiran K0a3c1d42020-11-19 17:09:40 +05304246 if (id->driver_info & BTUSB_INTEL_NEWGEN) {
4247 hdev->manufacturer = 2;
4248 hdev->send = btusb_send_frame_intel;
4249 hdev->setup = btusb_setup_intel_newgen;
4250 hdev->shutdown = btusb_shutdown_intel_new;
4251 hdev->hw_error = btintel_hw_error;
4252 hdev->set_diag = btintel_set_diag;
4253 hdev->set_bdaddr = btintel_set_bdaddr;
4254 hdev->cmd_timeout = btusb_intel_cmd_timeout;
4255 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
4256 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
4257 set_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks);
4258
4259 data->recv_event = btusb_recv_event_intel;
4260 data->recv_bulk = btusb_recv_bulk_intel;
4261 set_bit(BTUSB_BOOTLOADER, &data->flags);
4262 }
4263
Amitkumar Karwarae8df492014-07-18 14:47:06 -07004264 if (id->driver_info & BTUSB_MARVELL)
4265 hdev->set_bdaddr = btusb_set_bdaddr_marvell;
4266
Marcel Holtmannc0a21a52019-09-27 08:48:58 +02004267 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_MTK) &&
4268 (id->driver_info & BTUSB_MEDIATEK)) {
Sean Wanga1c49c432019-06-02 08:02:48 +08004269 hdev->setup = btusb_mtk_setup;
4270 hdev->shutdown = btusb_mtk_shutdown;
4271 hdev->manufacturer = 70;
4272 set_bit(HCI_QUIRK_NON_PERSISTENT_SETUP, &hdev->quirks);
4273 }
Sean Wanga1c49c432019-06-02 08:02:48 +08004274
Marcel Holtmann661cf882015-01-02 23:35:20 -08004275 if (id->driver_info & BTUSB_SWAVE) {
4276 set_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks);
Marcel Holtmannd57dbe72014-12-26 04:42:33 +01004277 set_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS, &hdev->quirks);
Marcel Holtmann661cf882015-01-02 23:35:20 -08004278 }
Marcel Holtmannd57dbe72014-12-26 04:42:33 +01004279
Marcel Holtmanne4c534b2015-10-21 01:31:45 +02004280 if (id->driver_info & BTUSB_INTEL_BOOT) {
4281 hdev->manufacturer = 2;
Marcel Holtmann40df7832014-07-06 13:29:58 +02004282 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
Marcel Holtmanne4c534b2015-10-21 01:31:45 +02004283 }
Marcel Holtmann40df7832014-07-06 13:29:58 +02004284
Jakub Pawlowski79f0c872015-01-29 10:38:34 -08004285 if (id->driver_info & BTUSB_ATH3012) {
Takashi Iwai803cdb82018-05-21 22:34:52 +02004286 data->setup_on_usb = btusb_setup_qca;
Toshi Kikuchi58592232014-12-12 10:58:05 -08004287 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
Jakub Pawlowski3d50d512015-03-17 09:04:15 -07004288 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
Jakub Pawlowski79f0c872015-01-29 10:38:34 -08004289 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
4290 }
Toshi Kikuchi58592232014-12-12 10:58:05 -08004291
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00004292 if (id->driver_info & BTUSB_QCA_ROME) {
4293 data->setup_on_usb = btusb_setup_qca;
4294 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
Abhishek Pandit-Subedi69335682020-06-24 11:11:44 -07004295 hdev->cmd_timeout = btusb_qca_cmd_timeout;
Vic Wei96e58d32018-03-28 08:28:47 -07004296 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
Hans de Goedefc549102018-04-27 11:26:43 +02004297 btusb_check_needs_reset_resume(intf);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00004298 }
4299
Rocky Liaob40f58b2020-09-29 12:23:51 +08004300 if (id->driver_info & BTUSB_QCA_WCN6855) {
4301 data->setup_on_usb = btusb_setup_qca;
4302 hdev->set_bdaddr = btusb_set_bdaddr_wcn6855;
4303 hdev->cmd_timeout = btusb_qca_cmd_timeout;
4304 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
4305 }
4306
Hilda Wu461f95f2020-06-30 21:09:40 +08004307 if (id->driver_info & BTUSB_AMP) {
4308 /* AMP controllers do not support SCO packets */
4309 data->isoc = NULL;
4310 } else {
4311 /* Interface orders are hardcoded in the specification */
4312 data->isoc = usb_ifnum_to_if(data->udev, ifnum_base + 1);
4313 data->isoc_ifnum = ifnum_base + 1;
4314 }
4315
Arnd Bergmann42d22092019-09-18 21:59:02 +02004316 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_RTL) &&
4317 (id->driver_info & BTUSB_REALTEK)) {
Carlo Caionedb33c772015-05-14 10:49:09 +02004318 hdev->setup = btrtl_setup_realtek;
Jian-Hong Pan7af3f5582019-06-25 16:30:51 +08004319 hdev->shutdown = btrtl_shutdown_realtek;
Alex Lud7ef0d12019-09-05 10:36:31 +08004320 hdev->cmd_timeout = btusb_rtl_cmd_timeout;
Daniel Drake04b8c812015-05-21 08:23:50 -06004321
Alex Lu9e455242019-08-14 20:02:52 +08004322 /* Realtek devices lose their updated firmware over global
4323 * suspend that means host doesn't send SET_FEATURE
4324 * (DEVICE_REMOTE_WAKEUP)
Daniel Drake04b8c812015-05-21 08:23:50 -06004325 */
Alex Lu9e455242019-08-14 20:02:52 +08004326 set_bit(BTUSB_WAKEUP_DISABLE, &data->flags);
Hilda Wu461f95f2020-06-30 21:09:40 +08004327 if (btusb_find_altsetting(data, 1))
4328 set_bit(BTUSB_USE_ALT1_FOR_WBS, &data->flags);
4329 else
4330 bt_dev_err(hdev, "Device does not support ALT setting 1");
Daniel Drake04b8c812015-05-21 08:23:50 -06004331 }
Daniel Drakea2698a92015-04-16 14:09:55 -06004332
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01004333 if (!reset)
Szymon Janca6c511c2012-05-23 12:35:46 +02004334 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02004335
4336 if (force_scofix || id->driver_info & BTUSB_WRONG_SCO_MTU) {
4337 if (!disable_scofix)
4338 set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
4339 }
4340
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02004341 if (id->driver_info & BTUSB_BROKEN_ISOC)
4342 data->isoc = NULL;
4343
Alain Michaud4b127bd2020-02-27 18:29:39 +00004344 if (id->driver_info & BTUSB_WIDEBAND_SPEECH)
Alain Michaud00bce3f2020-03-05 16:14:59 +00004345 set_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED, &hdev->quirks);
Alain Michaud4b127bd2020-02-27 18:29:39 +00004346
Alain Michaudaff8c482020-04-23 14:43:31 +00004347 if (id->driver_info & BTUSB_VALID_LE_STATES)
4348 set_bit(HCI_QUIRK_VALID_LE_STATES, &hdev->quirks);
4349
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01004350 if (id->driver_info & BTUSB_DIGIANSWER) {
4351 data->cmdreq_type = USB_TYPE_VENDOR;
Szymon Janca6c511c2012-05-23 12:35:46 +02004352 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01004353 }
4354
4355 if (id->driver_info & BTUSB_CSR) {
4356 struct usb_device *udev = data->udev;
Marcel Holtmann81cac642014-01-03 03:02:36 -08004357 u16 bcdDevice = le16_to_cpu(udev->descriptor.bcdDevice);
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01004358
4359 /* Old firmware would otherwise execute USB reset */
Marcel Holtmann81cac642014-01-03 03:02:36 -08004360 if (bcdDevice < 0x117)
Szymon Janca6c511c2012-05-23 12:35:46 +02004361 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmann81cac642014-01-03 03:02:36 -08004362
Ismael Ferreras Morezuelascde1a8a2020-07-26 23:12:28 +02004363 /* This must be set first in case we disable it for fakes */
Jakub Pawlowski49c989a2015-03-17 09:04:17 -07004364 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
Ismael Ferreras Morezuelascde1a8a2020-07-26 23:12:28 +02004365
4366 /* Fake CSR devices with broken commands */
4367 if (le16_to_cpu(udev->descriptor.idVendor) == 0x0a12 &&
4368 le16_to_cpu(udev->descriptor.idProduct) == 0x0001)
4369 hdev->setup = btusb_setup_csr;
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01004370 }
4371
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02004372 if (id->driver_info & BTUSB_SNIFFER) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02004373 struct usb_device *udev = data->udev;
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02004374
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01004375 /* New sniffer firmware has crippled HCI interface */
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02004376 if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
4377 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
4378 }
4379
Marcel Holtmann3a5ef202014-07-06 14:53:54 +02004380 if (id->driver_info & BTUSB_INTEL_BOOT) {
4381 /* A bug in the bootloader causes that interrupt interface is
4382 * only enabled after receiving SetInterface(0, AltSetting=0).
4383 */
4384 err = usb_set_interface(data->udev, 0, 0);
4385 if (err < 0) {
4386 BT_ERR("failed to set interface 0, alt 0 %d", err);
Rajat Jain10ab1332017-02-01 14:24:08 -08004387 goto out_free_dev;
Marcel Holtmann3a5ef202014-07-06 14:53:54 +02004388 }
4389 }
4390
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02004391 if (data->isoc) {
4392 err = usb_driver_claim_interface(&btusb_driver,
Marcel Holtmann89e75332014-09-16 04:44:50 +02004393 data->isoc, data);
Rajat Jain10ab1332017-02-01 14:24:08 -08004394 if (err < 0)
4395 goto out_free_dev;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02004396 }
4397
Marcel Holtmannc0a21a52019-09-27 08:48:58 +02004398 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_BCM) && data->diag) {
Marcel Holtmann9d08f502015-10-08 20:23:08 +02004399 if (!usb_driver_claim_interface(&btusb_driver,
4400 data->diag, data))
4401 __set_diag_interface(hdev);
4402 else
4403 data->diag = NULL;
4404 }
Marcel Holtmann9d08f502015-10-08 20:23:08 +02004405
Hans de Goedeeff2d682017-11-13 14:44:16 +01004406 if (enable_autosuspend)
4407 usb_enable_autosuspend(data->udev);
4408
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004409 err = hci_register_dev(hdev);
Rajat Jain10ab1332017-02-01 14:24:08 -08004410 if (err < 0)
4411 goto out_free_dev;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004412
4413 usb_set_intfdata(intf, data);
4414
4415 return 0;
Rajat Jain10ab1332017-02-01 14:24:08 -08004416
4417out_free_dev:
Rajat Jaindc786b22019-01-24 15:28:14 -08004418 if (data->reset_gpio)
4419 gpiod_put(data->reset_gpio);
Rajat Jain10ab1332017-02-01 14:24:08 -08004420 hci_free_dev(hdev);
4421 return err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004422}
4423
4424static void btusb_disconnect(struct usb_interface *intf)
4425{
4426 struct btusb_data *data = usb_get_intfdata(intf);
4427 struct hci_dev *hdev;
4428
4429 BT_DBG("intf %p", intf);
4430
4431 if (!data)
4432 return;
4433
4434 hdev = data->hdev;
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02004435 usb_set_intfdata(data->intf, NULL);
4436
4437 if (data->isoc)
4438 usb_set_intfdata(data->isoc, NULL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004439
Marcel Holtmann9d08f502015-10-08 20:23:08 +02004440 if (data->diag)
4441 usb_set_intfdata(data->diag, NULL);
4442
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004443 hci_unregister_dev(hdev);
4444
Marcel Holtmann9d08f502015-10-08 20:23:08 +02004445 if (intf == data->intf) {
4446 if (data->isoc)
4447 usb_driver_release_interface(&btusb_driver, data->isoc);
4448 if (data->diag)
4449 usb_driver_release_interface(&btusb_driver, data->diag);
4450 } else if (intf == data->isoc) {
4451 if (data->diag)
4452 usb_driver_release_interface(&btusb_driver, data->diag);
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02004453 usb_driver_release_interface(&btusb_driver, data->intf);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02004454 } else if (intf == data->diag) {
4455 usb_driver_release_interface(&btusb_driver, data->intf);
4456 if (data->isoc)
4457 usb_driver_release_interface(&btusb_driver, data->isoc);
4458 }
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02004459
Rajat Jainfd913ef2017-02-01 14:24:09 -08004460 if (data->oob_wake_irq)
4461 device_init_wakeup(&data->udev->dev, false);
4462
Rajat Jaindc786b22019-01-24 15:28:14 -08004463 if (data->reset_gpio)
4464 gpiod_put(data->reset_gpio);
4465
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004466 hci_free_dev(hdev);
4467}
4468
Oliver Neukum7bee5492009-08-24 23:44:59 +02004469#ifdef CONFIG_PM
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01004470static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
4471{
4472 struct btusb_data *data = usb_get_intfdata(intf);
4473
4474 BT_DBG("intf %p", intf);
4475
4476 if (data->suspend_count++)
4477 return 0;
4478
Oliver Neukum7bee5492009-08-24 23:44:59 +02004479 spin_lock_irq(&data->txlock);
Alan Stern5b1b0b82011-08-19 23:49:48 +02004480 if (!(PMSG_IS_AUTO(message) && data->tx_in_flight)) {
Oliver Neukum7bee5492009-08-24 23:44:59 +02004481 set_bit(BTUSB_SUSPENDING, &data->flags);
4482 spin_unlock_irq(&data->txlock);
4483 } else {
4484 spin_unlock_irq(&data->txlock);
4485 data->suspend_count--;
4486 return -EBUSY;
4487 }
4488
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01004489 cancel_work_sync(&data->work);
4490
Oliver Neukum7bee5492009-08-24 23:44:59 +02004491 btusb_stop_traffic(data);
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01004492 usb_kill_anchored_urbs(&data->tx_anchor);
4493
Rajat Jainfd913ef2017-02-01 14:24:09 -08004494 if (data->oob_wake_irq && device_may_wakeup(&data->udev->dev)) {
4495 set_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags);
4496 enable_irq_wake(data->oob_wake_irq);
4497 enable_irq(data->oob_wake_irq);
4498 }
4499
Alex Lu9e455242019-08-14 20:02:52 +08004500 /* For global suspend, Realtek devices lose the loaded fw
4501 * in them. But for autosuspend, firmware should remain.
4502 * Actually, it depends on whether the usb host sends
4503 * set feature (enable wakeup) or not.
4504 */
4505 if (test_bit(BTUSB_WAKEUP_DISABLE, &data->flags)) {
4506 if (PMSG_IS_AUTO(message) &&
4507 device_can_wakeup(&data->udev->dev))
4508 data->udev->do_remote_wakeup = 1;
4509 else if (!PMSG_IS_AUTO(message))
4510 data->udev->reset_resume = 1;
4511 }
4512
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01004513 return 0;
4514}
4515
Oliver Neukum7bee5492009-08-24 23:44:59 +02004516static void play_deferred(struct btusb_data *data)
4517{
4518 struct urb *urb;
4519 int err;
4520
4521 while ((urb = usb_get_from_anchor(&data->deferred))) {
Jeffy Chen19cfe912017-07-20 18:53:50 +08004522 usb_anchor_urb(urb, &data->tx_anchor);
4523
Oliver Neukum7bee5492009-08-24 23:44:59 +02004524 err = usb_submit_urb(urb, GFP_ATOMIC);
Jeffy Chen19cfe912017-07-20 18:53:50 +08004525 if (err < 0) {
4526 if (err != -EPERM && err != -ENODEV)
4527 BT_ERR("%s urb %p submission failed (%d)",
4528 data->hdev->name, urb, -err);
4529 kfree(urb->setup_packet);
4530 usb_unanchor_urb(urb);
4531 usb_free_urb(urb);
Oliver Neukum7bee5492009-08-24 23:44:59 +02004532 break;
Jeffy Chen19cfe912017-07-20 18:53:50 +08004533 }
Oliver Neukum7bee5492009-08-24 23:44:59 +02004534
4535 data->tx_in_flight++;
Jeffy Chen19cfe912017-07-20 18:53:50 +08004536 usb_free_urb(urb);
Oliver Neukum7bee5492009-08-24 23:44:59 +02004537 }
Jeffy Chen19cfe912017-07-20 18:53:50 +08004538
4539 /* Cleanup the rest deferred urbs. */
4540 while ((urb = usb_get_from_anchor(&data->deferred))) {
4541 kfree(urb->setup_packet);
4542 usb_free_urb(urb);
4543 }
Oliver Neukum7bee5492009-08-24 23:44:59 +02004544}
4545
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01004546static int btusb_resume(struct usb_interface *intf)
4547{
4548 struct btusb_data *data = usb_get_intfdata(intf);
4549 struct hci_dev *hdev = data->hdev;
Oliver Neukum7bee5492009-08-24 23:44:59 +02004550 int err = 0;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01004551
4552 BT_DBG("intf %p", intf);
4553
4554 if (--data->suspend_count)
4555 return 0;
4556
Rajat Jainfd913ef2017-02-01 14:24:09 -08004557 /* Disable only if not already disabled (keep it balanced) */
4558 if (test_and_clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags)) {
4559 disable_irq(data->oob_wake_irq);
4560 disable_irq_wake(data->oob_wake_irq);
4561 }
4562
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01004563 if (!test_bit(HCI_RUNNING, &hdev->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +02004564 goto done;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01004565
4566 if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
4567 err = btusb_submit_intr_urb(hdev, GFP_NOIO);
4568 if (err < 0) {
4569 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02004570 goto failed;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01004571 }
4572 }
4573
4574 if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) {
Marcel Holtmann43c2e572009-02-04 17:41:38 +01004575 err = btusb_submit_bulk_urb(hdev, GFP_NOIO);
4576 if (err < 0) {
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01004577 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02004578 goto failed;
4579 }
4580
4581 btusb_submit_bulk_urb(hdev, GFP_NOIO);
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01004582 }
4583
4584 if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
4585 if (btusb_submit_isoc_urb(hdev, GFP_NOIO) < 0)
4586 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
4587 else
4588 btusb_submit_isoc_urb(hdev, GFP_NOIO);
4589 }
4590
Oliver Neukum7bee5492009-08-24 23:44:59 +02004591 spin_lock_irq(&data->txlock);
4592 play_deferred(data);
4593 clear_bit(BTUSB_SUSPENDING, &data->flags);
4594 spin_unlock_irq(&data->txlock);
4595 schedule_work(&data->work);
4596
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01004597 return 0;
Oliver Neukum7bee5492009-08-24 23:44:59 +02004598
4599failed:
4600 usb_scuttle_anchored_urbs(&data->deferred);
4601done:
4602 spin_lock_irq(&data->txlock);
4603 clear_bit(BTUSB_SUSPENDING, &data->flags);
4604 spin_unlock_irq(&data->txlock);
4605
4606 return err;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01004607}
Oliver Neukum7bee5492009-08-24 23:44:59 +02004608#endif
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01004609
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004610static struct usb_driver btusb_driver = {
4611 .name = "btusb",
4612 .probe = btusb_probe,
4613 .disconnect = btusb_disconnect,
Oliver Neukum7bee5492009-08-24 23:44:59 +02004614#ifdef CONFIG_PM
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01004615 .suspend = btusb_suspend,
4616 .resume = btusb_resume,
Oliver Neukum7bee5492009-08-24 23:44:59 +02004617#endif
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004618 .id_table = btusb_table,
Oliver Neukum7bee5492009-08-24 23:44:59 +02004619 .supports_autosuspend = 1,
Sarah Sharpe1f12eb2012-04-23 10:08:51 -07004620 .disable_hub_initiated_lpm = 1,
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004621};
4622
Greg Kroah-Hartman93f15082011-11-18 09:47:34 -08004623module_usb_driver(btusb_driver);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004624
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02004625module_param(disable_scofix, bool, 0644);
4626MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
4627
4628module_param(force_scofix, bool, 0644);
4629MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
4630
Hans de Goedeeff2d682017-11-13 14:44:16 +01004631module_param(enable_autosuspend, bool, 0644);
4632MODULE_PARM_DESC(enable_autosuspend, "Enable USB autosuspend by default");
4633
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02004634module_param(reset, bool, 0644);
4635MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
4636
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004637MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
4638MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
4639MODULE_VERSION(VERSION);
4640MODULE_LICENSE("GPL");