blob: 0d355bb45e084cdcefc38c9cd84fbf67b763c1b3 [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 },
Kiran K10c24232020-11-19 17:09:43 +0530369 { USB_DEVICE(0x8087, 0x0032), .driver_info = BTUSB_INTEL_NEWGEN |
Raghuram Hegde875e167592020-05-11 16:40:40 +0530370 BTUSB_WIDEBAND_SPEECH},
Kiran Kf01bb2a2021-01-05 08:32:29 +0530371 { USB_DEVICE(0x8087, 0x0033), .driver_info = BTUSB_INTEL_NEWGEN |
372 BTUSB_WIDEBAND_SPEECH},
Marcel Holtmann407550f2015-02-22 15:41:18 -0800373 { USB_DEVICE(0x8087, 0x07da), .driver_info = BTUSB_CSR },
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -0700374 { USB_DEVICE(0x8087, 0x07dc), .driver_info = BTUSB_INTEL },
Tedd Ho-Jeong Anef4e5e42013-11-12 13:10:58 -0800375 { USB_DEVICE(0x8087, 0x0a2a), .driver_info = BTUSB_INTEL },
Alain Michaud3e4e3f72020-02-27 18:29:37 +0000376 { USB_DEVICE(0x8087, 0x0a2b), .driver_info = BTUSB_INTEL_NEW |
377 BTUSB_WIDEBAND_SPEECH },
378 { USB_DEVICE(0x8087, 0x0aa7), .driver_info = BTUSB_INTEL |
379 BTUSB_WIDEBAND_SPEECH },
380 { USB_DEVICE(0x8087, 0x0aaa), .driver_info = BTUSB_INTEL_NEW |
Alain Michaudaff8c482020-04-23 14:43:31 +0000381 BTUSB_WIDEBAND_SPEECH |
382 BTUSB_VALID_LE_STATES },
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -0700383
Marcel Holtmannd0ac9eb2015-01-28 19:41:43 -0800384 /* Other Intel Bluetooth devices */
385 { USB_VENDOR_AND_INTERFACE_INFO(0x8087, 0xe0, 0x01, 0x01),
386 .driver_info = BTUSB_IGNORE },
Amitkumar Karwarae8df492014-07-18 14:47:06 -0700387
Joseph Hwang33bfd942020-07-13 15:45:29 +0800388 /* Realtek 8822CE Bluetooth devices */
389 { USB_DEVICE(0x0bda, 0xb00c), .driver_info = BTUSB_REALTEK |
390 BTUSB_WIDEBAND_SPEECH },
391
Max Chou0d484db2020-11-10 11:38:37 +0800392 /* Realtek 8852AE Bluetooth devices */
393 { USB_DEVICE(0x0bda, 0xc852), .driver_info = BTUSB_REALTEK |
394 BTUSB_WIDEBAND_SPEECH },
395
Daniel Drakea2698a92015-04-16 14:09:55 -0600396 /* Realtek Bluetooth devices */
397 { USB_VENDOR_AND_INTERFACE_INFO(0x0bda, 0xe0, 0x01, 0x01),
398 .driver_info = BTUSB_REALTEK },
399
Sean Wanga1c49c432019-06-02 08:02:48 +0800400 /* MediaTek Bluetooth devices */
401 { USB_VENDOR_AND_INTERFACE_INFO(0x0e8d, 0xe0, 0x01, 0x01),
402 .driver_info = BTUSB_MEDIATEK },
403
Chris Chiu3a567b92020-11-10 11:40:10 +0800404 /* Additional MediaTek MT7615E Bluetooth devices */
405 { USB_DEVICE(0x13d3, 0x3560), .driver_info = BTUSB_MEDIATEK},
406
Daniel Drakea2698a92015-04-16 14:09:55 -0600407 /* Additional Realtek 8723AE Bluetooth devices */
408 { USB_DEVICE(0x0930, 0x021d), .driver_info = BTUSB_REALTEK },
409 { USB_DEVICE(0x13d3, 0x3394), .driver_info = BTUSB_REALTEK },
410
411 /* Additional Realtek 8723BE Bluetooth devices */
412 { USB_DEVICE(0x0489, 0xe085), .driver_info = BTUSB_REALTEK },
413 { USB_DEVICE(0x0489, 0xe08b), .driver_info = BTUSB_REALTEK },
414 { USB_DEVICE(0x13d3, 0x3410), .driver_info = BTUSB_REALTEK },
415 { USB_DEVICE(0x13d3, 0x3416), .driver_info = BTUSB_REALTEK },
416 { USB_DEVICE(0x13d3, 0x3459), .driver_info = BTUSB_REALTEK },
Dmitry Tunina81d72d2017-08-08 14:09:02 +0300417 { USB_DEVICE(0x13d3, 0x3494), .driver_info = BTUSB_REALTEK },
Daniel Drakea2698a92015-04-16 14:09:55 -0600418
Vicente Bergasa41e0792018-03-20 19:41:10 +0100419 /* Additional Realtek 8723BU Bluetooth devices */
420 { USB_DEVICE(0x7392, 0xa611), .driver_info = BTUSB_REALTEK },
421
Jian-Hong Pan66d99752018-05-21 18:09:20 +0800422 /* Additional Realtek 8723DE Bluetooth devices */
Jian-Hong Pan45ae68b2018-05-25 17:54:52 +0800423 { USB_DEVICE(0x0bda, 0xb009), .driver_info = BTUSB_REALTEK },
Jian-Hong Pan66d99752018-05-21 18:09:20 +0800424 { USB_DEVICE(0x2ff8, 0xb011), .driver_info = BTUSB_REALTEK },
425
Daniel Drakea2698a92015-04-16 14:09:55 -0600426 /* Additional Realtek 8821AE Bluetooth devices */
427 { USB_DEVICE(0x0b05, 0x17dc), .driver_info = BTUSB_REALTEK },
428 { USB_DEVICE(0x13d3, 0x3414), .driver_info = BTUSB_REALTEK },
429 { USB_DEVICE(0x13d3, 0x3458), .driver_info = BTUSB_REALTEK },
430 { USB_DEVICE(0x13d3, 0x3461), .driver_info = BTUSB_REALTEK },
431 { USB_DEVICE(0x13d3, 0x3462), .driver_info = BTUSB_REALTEK },
432
Larry Fingerfed03fe2018-02-11 12:24:32 -0600433 /* Additional Realtek 8822BE Bluetooth devices */
Artiom Vaskov1cd2fab2018-05-30 11:23:00 +0300434 { USB_DEVICE(0x13d3, 0x3526), .driver_info = BTUSB_REALTEK },
Larry Fingerfed03fe2018-02-11 12:24:32 -0600435 { USB_DEVICE(0x0b05, 0x185c), .driver_info = BTUSB_REALTEK },
436
Jian-Hong Pan6d0762b2019-09-03 17:10:42 +0800437 /* Additional Realtek 8822CE Bluetooth devices */
Max Chou73280f12020-11-05 18:54:48 +0800438 { USB_DEVICE(0x04ca, 0x4005), .driver_info = BTUSB_REALTEK |
439 BTUSB_WIDEBAND_SPEECH },
440 { USB_DEVICE(0x04c5, 0x161f), .driver_info = BTUSB_REALTEK |
441 BTUSB_WIDEBAND_SPEECH },
442 { USB_DEVICE(0x0b05, 0x18ef), .driver_info = BTUSB_REALTEK |
443 BTUSB_WIDEBAND_SPEECH },
444 { USB_DEVICE(0x13d3, 0x3548), .driver_info = BTUSB_REALTEK |
445 BTUSB_WIDEBAND_SPEECH },
446 { USB_DEVICE(0x13d3, 0x3549), .driver_info = BTUSB_REALTEK |
447 BTUSB_WIDEBAND_SPEECH },
448 { USB_DEVICE(0x13d3, 0x3553), .driver_info = BTUSB_REALTEK |
449 BTUSB_WIDEBAND_SPEECH },
450 { USB_DEVICE(0x13d3, 0x3555), .driver_info = BTUSB_REALTEK |
451 BTUSB_WIDEBAND_SPEECH },
452 { USB_DEVICE(0x2ff8, 0x3051), .driver_info = BTUSB_REALTEK |
453 BTUSB_WIDEBAND_SPEECH },
Julian Pidancetf4962972020-11-09 17:47:22 +0100454 { USB_DEVICE(0x1358, 0xc123), .driver_info = BTUSB_REALTEK |
455 BTUSB_WIDEBAND_SPEECH },
Cadel Watson24d6a6d2020-11-19 16:16:25 +1100456 { USB_DEVICE(0x0bda, 0xc123), .driver_info = BTUSB_REALTEK |
457 BTUSB_WIDEBAND_SPEECH },
Rasmus Moorats3edc5782021-02-18 13:11:24 +0200458 { USB_DEVICE(0x0cb5, 0xc547), .driver_info = BTUSB_REALTEK |
459 BTUSB_WIDEBAND_SPEECH },
Jian-Hong Pan6d0762b2019-09-03 17:10:42 +0800460
Peter Poklop4481c072015-08-15 20:47:09 +0200461 /* Silicon Wave based devices */
462 { USB_DEVICE(0x0c10, 0x0000), .driver_info = BTUSB_SWAVE },
463
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200464 { } /* Terminating entry */
465};
466
Hans de Goede1fdb9262018-02-20 09:06:18 +0100467/* The Bluetooth USB module build into some devices needs to be reset on resume,
468 * this is a problem with the platform (likely shutting off all power) not with
469 * the module itself. So we use a DMI list to match known broken platforms.
470 */
471static const struct dmi_system_id btusb_needs_reset_resume_table[] = {
472 {
Kai-Heng Feng0c6e5262018-03-01 13:42:52 +0800473 /* Dell OptiPlex 3060 (QCA ROME device 0cf3:e007) */
Hans de Goede1fdb9262018-02-20 09:06:18 +0100474 .matches = {
Kai-Heng Feng0c6e5262018-03-01 13:42:52 +0800475 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
476 DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 3060"),
Hans de Goede1fdb9262018-02-20 09:06:18 +0100477 },
478 },
Hans de Goede596b07a2018-04-26 20:52:06 +0200479 {
480 /* Dell XPS 9360 (QCA ROME device 0cf3:e300) */
481 .matches = {
482 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
483 DMI_MATCH(DMI_PRODUCT_NAME, "XPS 13 9360"),
484 },
485 },
Hans de Goede939bc6c2018-05-22 09:34:10 +0200486 {
487 /* Dell Inspiron 5565 (QCA ROME device 0cf3:e009) */
488 .matches = {
489 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
490 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5565"),
491 },
492 },
Hans de Goede1fdb9262018-02-20 09:06:18 +0100493 {}
494};
495
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200496#define BTUSB_MAX_ISOC_FRAMES 10
497
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200498#define BTUSB_INTR_RUNNING 0
499#define BTUSB_BULK_RUNNING 1
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200500#define BTUSB_ISOC_RUNNING 2
Oliver Neukum7bee5492009-08-24 23:44:59 +0200501#define BTUSB_SUSPENDING 3
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -0300502#define BTUSB_DID_ISO_RESUME 4
Marcel Holtmanncda0dd72015-01-26 21:33:48 -0800503#define BTUSB_BOOTLOADER 5
504#define BTUSB_DOWNLOADING 6
Marcel Holtmannce6bb922015-01-28 01:58:40 -0800505#define BTUSB_FIRMWARE_LOADED 7
Marcel Holtmanncda0dd72015-01-26 21:33:48 -0800506#define BTUSB_FIRMWARE_FAILED 8
Marcel Holtmannce6bb922015-01-28 01:58:40 -0800507#define BTUSB_BOOTING 9
Hans de Goede61f5ace2018-01-08 10:44:16 +0100508#define BTUSB_DIAG_RUNNING 10
509#define BTUSB_OOB_WAKE_ENABLED 11
Rajat Jaindc786b22019-01-24 15:28:14 -0800510#define BTUSB_HW_RESET_ACTIVE 12
Sean Wanga1c49c432019-06-02 08:02:48 +0800511#define BTUSB_TX_WAIT_VND_EVT 13
Alex Lu9e455242019-08-14 20:02:52 +0800512#define BTUSB_WAKEUP_DISABLE 14
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200513
514struct btusb_data {
515 struct hci_dev *hdev;
516 struct usb_device *udev;
Marcel Holtmann5fbcd262008-09-23 00:16:36 +0200517 struct usb_interface *intf;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200518 struct usb_interface *isoc;
Marcel Holtmann9d08f502015-10-08 20:23:08 +0200519 struct usb_interface *diag;
Marcel Holtmann459232f2017-10-24 19:42:45 +0200520 unsigned isoc_ifnum;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200521
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200522 unsigned long flags;
523
524 struct work_struct work;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200525 struct work_struct waker;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200526
Marcel Holtmann803b5832014-09-16 08:00:29 +0200527 struct usb_anchor deferred;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200528 struct usb_anchor tx_anchor;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200529 int tx_in_flight;
530 spinlock_t txlock;
531
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200532 struct usb_anchor intr_anchor;
533 struct usb_anchor bulk_anchor;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200534 struct usb_anchor isoc_anchor;
Marcel Holtmann9d08f502015-10-08 20:23:08 +0200535 struct usb_anchor diag_anchor;
Sean Wanga1c49c432019-06-02 08:02:48 +0800536 struct usb_anchor ctrl_anchor;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200537 spinlock_t rxlock;
538
539 struct sk_buff *evt_skb;
540 struct sk_buff *acl_skb;
541 struct sk_buff *sco_skb;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200542
543 struct usb_endpoint_descriptor *intr_ep;
544 struct usb_endpoint_descriptor *bulk_tx_ep;
545 struct usb_endpoint_descriptor *bulk_rx_ep;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200546 struct usb_endpoint_descriptor *isoc_tx_ep;
547 struct usb_endpoint_descriptor *isoc_rx_ep;
Marcel Holtmann9d08f502015-10-08 20:23:08 +0200548 struct usb_endpoint_descriptor *diag_tx_ep;
549 struct usb_endpoint_descriptor *diag_rx_ep;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200550
Rajat Jaindc786b22019-01-24 15:28:14 -0800551 struct gpio_desc *reset_gpio;
552
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100553 __u8 cmdreq_type;
Marcel Holtmann893ba542015-01-28 20:27:34 -0800554 __u8 cmdreq;
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100555
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100556 unsigned int sco_num;
Sathish Narasimmanbaac6272020-04-03 21:43:59 +0200557 unsigned int air_mode;
558 bool usb_alt6_packet_flow;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200559 int isoc_altsetting;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +0100560 int suspend_count;
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +0100561
Marcel Holtmann97307f52015-01-12 13:51:10 -0800562 int (*recv_event)(struct hci_dev *hdev, struct sk_buff *skb);
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +0100563 int (*recv_bulk)(struct btusb_data *data, void *buffer, int count);
Kim, Ben Young Taeace31982015-02-15 23:06:14 +0000564
565 int (*setup_on_usb)(struct hci_dev *hdev);
Rajat Jainfd913ef2017-02-01 14:24:09 -0800566
567 int oob_wake_irq; /* irq for out-of-band wake-on-bt */
Rajat Jaindc786b22019-01-24 15:28:14 -0800568 unsigned cmd_timeout_cnt;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200569};
570
Rajat Jaindc786b22019-01-24 15:28:14 -0800571static void btusb_intel_cmd_timeout(struct hci_dev *hdev)
572{
573 struct btusb_data *data = hci_get_drvdata(hdev);
574 struct gpio_desc *reset_gpio = data->reset_gpio;
575
576 if (++data->cmd_timeout_cnt < 5)
577 return;
578
579 if (!reset_gpio) {
580 bt_dev_err(hdev, "No way to reset. Ignoring and continuing");
581 return;
582 }
583
584 /*
585 * Toggle the hard reset line if the platform provides one. The reset
586 * is going to yank the device off the USB and then replug. So doing
587 * once is enough. The cleanup is handled correctly on the way out
588 * (standard USB disconnect), and the new device is detected cleanly
589 * and bound to the driver again like it should be.
590 */
591 if (test_and_set_bit(BTUSB_HW_RESET_ACTIVE, &data->flags)) {
592 bt_dev_err(hdev, "last reset failed? Not resetting again");
593 return;
594 }
595
596 bt_dev_err(hdev, "Initiating HW reset via gpio");
Rajat Jain2de66bb82019-01-28 15:08:09 -0800597 gpiod_set_value_cansleep(reset_gpio, 1);
598 msleep(100);
599 gpiod_set_value_cansleep(reset_gpio, 0);
Rajat Jaindc786b22019-01-24 15:28:14 -0800600}
601
Alex Lud7ef0d12019-09-05 10:36:31 +0800602static void btusb_rtl_cmd_timeout(struct hci_dev *hdev)
603{
604 struct btusb_data *data = hci_get_drvdata(hdev);
605 struct gpio_desc *reset_gpio = data->reset_gpio;
606
607 if (++data->cmd_timeout_cnt < 5)
608 return;
609
610 if (!reset_gpio) {
611 bt_dev_err(hdev, "No gpio to reset Realtek device, ignoring");
612 return;
613 }
614
615 /* Toggle the hard reset line. The Realtek device is going to
616 * yank itself off the USB and then replug. The cleanup is handled
617 * correctly on the way out (standard USB disconnect), and the new
618 * device is detected cleanly and bound to the driver again like
619 * it should be.
620 */
621 if (test_and_set_bit(BTUSB_HW_RESET_ACTIVE, &data->flags)) {
622 bt_dev_err(hdev, "last reset failed? Not resetting again");
623 return;
624 }
625
626 bt_dev_err(hdev, "Reset Realtek device via gpio");
Alex Lud7ef0d12019-09-05 10:36:31 +0800627 gpiod_set_value_cansleep(reset_gpio, 1);
Max Chou34682112019-11-27 11:01:07 +0800628 msleep(200);
629 gpiod_set_value_cansleep(reset_gpio, 0);
Alex Lud7ef0d12019-09-05 10:36:31 +0800630}
631
Abhishek Pandit-Subedi69335682020-06-24 11:11:44 -0700632static void btusb_qca_cmd_timeout(struct hci_dev *hdev)
633{
634 struct btusb_data *data = hci_get_drvdata(hdev);
635 int err;
636
637 if (++data->cmd_timeout_cnt < 5)
638 return;
639
640 bt_dev_err(hdev, "Multiple cmd timeouts seen. Resetting usb device.");
Abhishek Pandit-Subedib980d472020-06-25 16:26:27 -0700641 /* This is not an unbalanced PM reference since the device will reset */
Abhishek Pandit-Subedi69335682020-06-24 11:11:44 -0700642 err = usb_autopm_get_interface(data->intf);
643 if (!err)
644 usb_queue_reset_device(data->intf);
645 else
646 bt_dev_err(hdev, "Failed usb_autopm_get_interface with %d", err);
647}
648
Marcel Holtmann803b5832014-09-16 08:00:29 +0200649static inline void btusb_free_frags(struct btusb_data *data)
650{
651 unsigned long flags;
652
653 spin_lock_irqsave(&data->rxlock, flags);
654
655 kfree_skb(data->evt_skb);
656 data->evt_skb = NULL;
657
658 kfree_skb(data->acl_skb);
659 data->acl_skb = NULL;
660
661 kfree_skb(data->sco_skb);
662 data->sco_skb = NULL;
663
664 spin_unlock_irqrestore(&data->rxlock, flags);
665}
666
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200667static int btusb_recv_intr(struct btusb_data *data, void *buffer, int count)
668{
Marcel Holtmann803b5832014-09-16 08:00:29 +0200669 struct sk_buff *skb;
Sebastian Andrzej Siewiora5e50d52018-06-19 23:56:57 +0200670 unsigned long flags;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200671 int err = 0;
672
Sebastian Andrzej Siewiora5e50d52018-06-19 23:56:57 +0200673 spin_lock_irqsave(&data->rxlock, flags);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200674 skb = data->evt_skb;
675
676 while (count) {
677 int len;
678
679 if (!skb) {
680 skb = bt_skb_alloc(HCI_MAX_EVENT_SIZE, GFP_ATOMIC);
681 if (!skb) {
682 err = -ENOMEM;
683 break;
684 }
685
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100686 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
687 hci_skb_expect(skb) = HCI_EVENT_HDR_SIZE;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200688 }
689
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100690 len = min_t(uint, hci_skb_expect(skb), count);
Johannes Berg59ae1d12017-06-16 14:29:20 +0200691 skb_put_data(skb, buffer, len);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200692
693 count -= len;
694 buffer += len;
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100695 hci_skb_expect(skb) -= len;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200696
697 if (skb->len == HCI_EVENT_HDR_SIZE) {
698 /* Complete event header */
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100699 hci_skb_expect(skb) = hci_event_hdr(skb)->plen;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200700
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100701 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
Marcel Holtmann803b5832014-09-16 08:00:29 +0200702 kfree_skb(skb);
703 skb = NULL;
704
705 err = -EILSEQ;
706 break;
707 }
708 }
709
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100710 if (!hci_skb_expect(skb)) {
Marcel Holtmann803b5832014-09-16 08:00:29 +0200711 /* Complete frame */
Marcel Holtmann97307f52015-01-12 13:51:10 -0800712 data->recv_event(data->hdev, skb);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200713 skb = NULL;
714 }
715 }
716
717 data->evt_skb = skb;
Sebastian Andrzej Siewiora5e50d52018-06-19 23:56:57 +0200718 spin_unlock_irqrestore(&data->rxlock, flags);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200719
720 return err;
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200721}
722
723static int btusb_recv_bulk(struct btusb_data *data, void *buffer, int count)
724{
Marcel Holtmann803b5832014-09-16 08:00:29 +0200725 struct sk_buff *skb;
Sebastian Andrzej Siewiora5e50d52018-06-19 23:56:57 +0200726 unsigned long flags;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200727 int err = 0;
728
Sebastian Andrzej Siewiora5e50d52018-06-19 23:56:57 +0200729 spin_lock_irqsave(&data->rxlock, flags);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200730 skb = data->acl_skb;
731
732 while (count) {
733 int len;
734
735 if (!skb) {
736 skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
737 if (!skb) {
738 err = -ENOMEM;
739 break;
740 }
741
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100742 hci_skb_pkt_type(skb) = HCI_ACLDATA_PKT;
743 hci_skb_expect(skb) = HCI_ACL_HDR_SIZE;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200744 }
745
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100746 len = min_t(uint, hci_skb_expect(skb), count);
Johannes Berg59ae1d12017-06-16 14:29:20 +0200747 skb_put_data(skb, buffer, len);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200748
749 count -= len;
750 buffer += len;
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100751 hci_skb_expect(skb) -= len;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200752
753 if (skb->len == HCI_ACL_HDR_SIZE) {
754 __le16 dlen = hci_acl_hdr(skb)->dlen;
755
756 /* Complete ACL header */
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100757 hci_skb_expect(skb) = __le16_to_cpu(dlen);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200758
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100759 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
Marcel Holtmann803b5832014-09-16 08:00:29 +0200760 kfree_skb(skb);
761 skb = NULL;
762
763 err = -EILSEQ;
764 break;
765 }
766 }
767
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100768 if (!hci_skb_expect(skb)) {
Marcel Holtmann803b5832014-09-16 08:00:29 +0200769 /* Complete frame */
770 hci_recv_frame(data->hdev, skb);
771 skb = NULL;
772 }
773 }
774
775 data->acl_skb = skb;
Sebastian Andrzej Siewiora5e50d52018-06-19 23:56:57 +0200776 spin_unlock_irqrestore(&data->rxlock, flags);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200777
778 return err;
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200779}
780
781static int btusb_recv_isoc(struct btusb_data *data, void *buffer, int count)
782{
Marcel Holtmann803b5832014-09-16 08:00:29 +0200783 struct sk_buff *skb;
Sebastian Andrzej Siewiora5e50d52018-06-19 23:56:57 +0200784 unsigned long flags;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200785 int err = 0;
786
Sebastian Andrzej Siewiora5e50d52018-06-19 23:56:57 +0200787 spin_lock_irqsave(&data->rxlock, flags);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200788 skb = data->sco_skb;
789
790 while (count) {
791 int len;
792
793 if (!skb) {
794 skb = bt_skb_alloc(HCI_MAX_SCO_SIZE, GFP_ATOMIC);
795 if (!skb) {
796 err = -ENOMEM;
797 break;
798 }
799
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100800 hci_skb_pkt_type(skb) = HCI_SCODATA_PKT;
801 hci_skb_expect(skb) = HCI_SCO_HDR_SIZE;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200802 }
803
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100804 len = min_t(uint, hci_skb_expect(skb), count);
Johannes Berg59ae1d12017-06-16 14:29:20 +0200805 skb_put_data(skb, buffer, len);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200806
807 count -= len;
808 buffer += len;
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100809 hci_skb_expect(skb) -= len;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200810
811 if (skb->len == HCI_SCO_HDR_SIZE) {
812 /* Complete SCO header */
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100813 hci_skb_expect(skb) = hci_sco_hdr(skb)->dlen;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200814
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100815 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
Marcel Holtmann803b5832014-09-16 08:00:29 +0200816 kfree_skb(skb);
817 skb = NULL;
818
819 err = -EILSEQ;
820 break;
821 }
822 }
823
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100824 if (!hci_skb_expect(skb)) {
Marcel Holtmann803b5832014-09-16 08:00:29 +0200825 /* Complete frame */
826 hci_recv_frame(data->hdev, skb);
827 skb = NULL;
828 }
829 }
830
831 data->sco_skb = skb;
Sebastian Andrzej Siewiora5e50d52018-06-19 23:56:57 +0200832 spin_unlock_irqrestore(&data->rxlock, flags);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200833
834 return err;
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200835}
836
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200837static void btusb_intr_complete(struct urb *urb)
838{
839 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +0100840 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200841 int err;
842
Marcel Holtmann89e75332014-09-16 04:44:50 +0200843 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
844 urb->actual_length);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200845
846 if (!test_bit(HCI_RUNNING, &hdev->flags))
847 return;
848
849 if (urb->status == 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200850 hdev->stat.byte_rx += urb->actual_length;
851
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200852 if (btusb_recv_intr(data, urb->transfer_buffer,
853 urb->actual_length) < 0) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +0100854 bt_dev_err(hdev, "corrupted event packet");
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200855 hdev->stat.err_rx++;
856 }
Champion Chen85560c42014-09-06 14:06:08 -0500857 } else if (urb->status == -ENOENT) {
858 /* Avoid suspend failed when usb_kill_urb */
859 return;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200860 }
861
862 if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
863 return;
864
Oliver Neukum7bee5492009-08-24 23:44:59 +0200865 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200866 usb_anchor_urb(urb, &data->intr_anchor);
867
868 err = usb_submit_urb(urb, GFP_ATOMIC);
869 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200870 /* -EPERM: urb is being killed;
Derek Robsond98422c2017-07-22 13:47:07 +1200871 * -ENODEV: device got disconnected
872 */
Paul Bolle4935f1c2011-08-09 17:16:28 +0200873 if (err != -EPERM && err != -ENODEV)
Marcel Holtmann2064ee32017-10-30 10:42:59 +0100874 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
875 urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200876 usb_unanchor_urb(urb);
877 }
878}
879
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100880static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200881{
David Herrmann155961e2012-02-09 21:58:32 +0100882 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200883 struct urb *urb;
884 unsigned char *buf;
885 unsigned int pipe;
886 int err, size;
887
888 BT_DBG("%s", hdev->name);
889
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200890 if (!data->intr_ep)
891 return -ENODEV;
892
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100893 urb = usb_alloc_urb(0, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200894 if (!urb)
895 return -ENOMEM;
896
897 size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
898
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100899 buf = kmalloc(size, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200900 if (!buf) {
901 usb_free_urb(urb);
902 return -ENOMEM;
903 }
904
905 pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
906
907 usb_fill_int_urb(urb, data->udev, pipe, buf, size,
Marcel Holtmann89e75332014-09-16 04:44:50 +0200908 btusb_intr_complete, hdev, data->intr_ep->bInterval);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200909
910 urb->transfer_flags |= URB_FREE_BUFFER;
911
912 usb_anchor_urb(urb, &data->intr_anchor);
913
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100914 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200915 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +0200916 if (err != -EPERM && err != -ENODEV)
Marcel Holtmann2064ee32017-10-30 10:42:59 +0100917 bt_dev_err(hdev, "urb %p submission failed (%d)",
918 urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200919 usb_unanchor_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200920 }
921
922 usb_free_urb(urb);
923
924 return err;
925}
926
927static void btusb_bulk_complete(struct urb *urb)
928{
929 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +0100930 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200931 int err;
932
Marcel Holtmann89e75332014-09-16 04:44:50 +0200933 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
934 urb->actual_length);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200935
936 if (!test_bit(HCI_RUNNING, &hdev->flags))
937 return;
938
939 if (urb->status == 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200940 hdev->stat.byte_rx += urb->actual_length;
941
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +0100942 if (data->recv_bulk(data, urb->transfer_buffer,
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200943 urb->actual_length) < 0) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +0100944 bt_dev_err(hdev, "corrupted ACL packet");
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200945 hdev->stat.err_rx++;
946 }
Champion Chen85560c42014-09-06 14:06:08 -0500947 } else if (urb->status == -ENOENT) {
948 /* Avoid suspend failed when usb_kill_urb */
949 return;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200950 }
951
952 if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
953 return;
954
955 usb_anchor_urb(urb, &data->bulk_anchor);
Oliver Neukum652fd782009-12-16 19:23:43 +0100956 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200957
958 err = usb_submit_urb(urb, GFP_ATOMIC);
959 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200960 /* -EPERM: urb is being killed;
Derek Robsond98422c2017-07-22 13:47:07 +1200961 * -ENODEV: device got disconnected
962 */
Paul Bolle4935f1c2011-08-09 17:16:28 +0200963 if (err != -EPERM && err != -ENODEV)
Marcel Holtmann2064ee32017-10-30 10:42:59 +0100964 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
965 urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200966 usb_unanchor_urb(urb);
967 }
968}
969
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100970static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200971{
David Herrmann155961e2012-02-09 21:58:32 +0100972 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200973 struct urb *urb;
974 unsigned char *buf;
975 unsigned int pipe;
Vikram Kandukuri290ba202009-07-02 14:31:59 +0530976 int err, size = HCI_MAX_FRAME_SIZE;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200977
978 BT_DBG("%s", hdev->name);
979
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200980 if (!data->bulk_rx_ep)
981 return -ENODEV;
982
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100983 urb = usb_alloc_urb(0, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200984 if (!urb)
985 return -ENOMEM;
986
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100987 buf = kmalloc(size, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200988 if (!buf) {
989 usb_free_urb(urb);
990 return -ENOMEM;
991 }
992
993 pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
994
Marcel Holtmann89e75332014-09-16 04:44:50 +0200995 usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
996 btusb_bulk_complete, hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200997
998 urb->transfer_flags |= URB_FREE_BUFFER;
999
Oliver Neukum7bee5492009-08-24 23:44:59 +02001000 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001001 usb_anchor_urb(urb, &data->bulk_anchor);
1002
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001003 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001004 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +02001005 if (err != -EPERM && err != -ENODEV)
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001006 bt_dev_err(hdev, "urb %p submission failed (%d)",
1007 urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001008 usb_unanchor_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001009 }
1010
1011 usb_free_urb(urb);
1012
1013 return err;
1014}
1015
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001016static void btusb_isoc_complete(struct urb *urb)
1017{
1018 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +01001019 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001020 int i, err;
1021
Marcel Holtmann89e75332014-09-16 04:44:50 +02001022 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1023 urb->actual_length);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001024
1025 if (!test_bit(HCI_RUNNING, &hdev->flags))
1026 return;
1027
1028 if (urb->status == 0) {
1029 for (i = 0; i < urb->number_of_packets; i++) {
1030 unsigned int offset = urb->iso_frame_desc[i].offset;
1031 unsigned int length = urb->iso_frame_desc[i].actual_length;
1032
1033 if (urb->iso_frame_desc[i].status)
1034 continue;
1035
1036 hdev->stat.byte_rx += length;
1037
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +02001038 if (btusb_recv_isoc(data, urb->transfer_buffer + offset,
1039 length) < 0) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001040 bt_dev_err(hdev, "corrupted SCO packet");
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001041 hdev->stat.err_rx++;
1042 }
1043 }
Champion Chen85560c42014-09-06 14:06:08 -05001044 } else if (urb->status == -ENOENT) {
1045 /* Avoid suspend failed when usb_kill_urb */
1046 return;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001047 }
1048
1049 if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags))
1050 return;
1051
1052 usb_anchor_urb(urb, &data->isoc_anchor);
1053
1054 err = usb_submit_urb(urb, GFP_ATOMIC);
1055 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +02001056 /* -EPERM: urb is being killed;
Derek Robsond98422c2017-07-22 13:47:07 +12001057 * -ENODEV: device got disconnected
1058 */
Paul Bolle4935f1c2011-08-09 17:16:28 +02001059 if (err != -EPERM && err != -ENODEV)
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001060 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
1061 urb, -err);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001062 usb_unanchor_urb(urb);
1063 }
1064}
1065
Sathish Narasimmanbaac6272020-04-03 21:43:59 +02001066static inline void __fill_isoc_descriptor_msbc(struct urb *urb, int len,
1067 int mtu, struct btusb_data *data)
1068{
1069 int i, offset = 0;
1070 unsigned int interval;
1071
1072 BT_DBG("len %d mtu %d", len, mtu);
1073
1074 /* For mSBC ALT 6 setting the host will send the packet at continuous
1075 * flow. As per core spec 5, vol 4, part B, table 2.1. For ALT setting
1076 * 6 the HCI PACKET INTERVAL should be 7.5ms for every usb packets.
1077 * To maintain the rate we send 63bytes of usb packets alternatively for
1078 * 7ms and 8ms to maintain the rate as 7.5ms.
1079 */
1080 if (data->usb_alt6_packet_flow) {
1081 interval = 7;
1082 data->usb_alt6_packet_flow = false;
1083 } else {
1084 interval = 6;
1085 data->usb_alt6_packet_flow = true;
1086 }
1087
1088 for (i = 0; i < interval; i++) {
1089 urb->iso_frame_desc[i].offset = offset;
1090 urb->iso_frame_desc[i].length = offset;
1091 }
1092
1093 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
1094 urb->iso_frame_desc[i].offset = offset;
1095 urb->iso_frame_desc[i].length = len;
1096 i++;
1097 }
1098
1099 urb->number_of_packets = i;
1100}
1101
Jesper Juhl42b16b32011-01-17 00:09:38 +01001102static inline void __fill_isoc_descriptor(struct urb *urb, int len, int mtu)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001103{
1104 int i, offset = 0;
1105
1106 BT_DBG("len %d mtu %d", len, mtu);
1107
1108 for (i = 0; i < BTUSB_MAX_ISOC_FRAMES && len >= mtu;
1109 i++, offset += mtu, len -= mtu) {
1110 urb->iso_frame_desc[i].offset = offset;
1111 urb->iso_frame_desc[i].length = mtu;
1112 }
1113
1114 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
1115 urb->iso_frame_desc[i].offset = offset;
1116 urb->iso_frame_desc[i].length = len;
1117 i++;
1118 }
1119
1120 urb->number_of_packets = i;
1121}
1122
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001123static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001124{
David Herrmann155961e2012-02-09 21:58:32 +01001125 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001126 struct urb *urb;
1127 unsigned char *buf;
1128 unsigned int pipe;
1129 int err, size;
1130
1131 BT_DBG("%s", hdev->name);
1132
1133 if (!data->isoc_rx_ep)
1134 return -ENODEV;
1135
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001136 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001137 if (!urb)
1138 return -ENOMEM;
1139
1140 size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) *
1141 BTUSB_MAX_ISOC_FRAMES;
1142
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001143 buf = kmalloc(size, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001144 if (!buf) {
1145 usb_free_urb(urb);
1146 return -ENOMEM;
1147 }
1148
1149 pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress);
1150
Bing Zhaofa0fb932011-12-20 18:19:00 -08001151 usb_fill_int_urb(urb, data->udev, pipe, buf, size, btusb_isoc_complete,
Marcel Holtmann89e75332014-09-16 04:44:50 +02001152 hdev, data->isoc_rx_ep->bInterval);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001153
Marcel Holtmann89e75332014-09-16 04:44:50 +02001154 urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001155
1156 __fill_isoc_descriptor(urb, size,
Marcel Holtmann89e75332014-09-16 04:44:50 +02001157 le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize));
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001158
1159 usb_anchor_urb(urb, &data->isoc_anchor);
1160
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001161 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001162 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +02001163 if (err != -EPERM && err != -ENODEV)
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001164 bt_dev_err(hdev, "urb %p submission failed (%d)",
1165 urb, -err);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001166 usb_unanchor_urb(urb);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001167 }
1168
1169 usb_free_urb(urb);
1170
1171 return err;
1172}
1173
Marcel Holtmann9d08f502015-10-08 20:23:08 +02001174static void btusb_diag_complete(struct urb *urb)
1175{
1176 struct hci_dev *hdev = urb->context;
1177 struct btusb_data *data = hci_get_drvdata(hdev);
1178 int err;
1179
1180 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1181 urb->actual_length);
1182
1183 if (urb->status == 0) {
1184 struct sk_buff *skb;
1185
1186 skb = bt_skb_alloc(urb->actual_length, GFP_ATOMIC);
1187 if (skb) {
Johannes Berg59ae1d12017-06-16 14:29:20 +02001188 skb_put_data(skb, urb->transfer_buffer,
1189 urb->actual_length);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02001190 hci_recv_diag(hdev, skb);
1191 }
1192 } else if (urb->status == -ENOENT) {
1193 /* Avoid suspend failed when usb_kill_urb */
1194 return;
1195 }
1196
1197 if (!test_bit(BTUSB_DIAG_RUNNING, &data->flags))
1198 return;
1199
1200 usb_anchor_urb(urb, &data->diag_anchor);
1201 usb_mark_last_busy(data->udev);
1202
1203 err = usb_submit_urb(urb, GFP_ATOMIC);
1204 if (err < 0) {
1205 /* -EPERM: urb is being killed;
Derek Robsond98422c2017-07-22 13:47:07 +12001206 * -ENODEV: device got disconnected
1207 */
Marcel Holtmann9d08f502015-10-08 20:23:08 +02001208 if (err != -EPERM && err != -ENODEV)
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001209 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
1210 urb, -err);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02001211 usb_unanchor_urb(urb);
1212 }
1213}
1214
1215static int btusb_submit_diag_urb(struct hci_dev *hdev, gfp_t mem_flags)
1216{
1217 struct btusb_data *data = hci_get_drvdata(hdev);
1218 struct urb *urb;
1219 unsigned char *buf;
1220 unsigned int pipe;
1221 int err, size = HCI_MAX_FRAME_SIZE;
1222
1223 BT_DBG("%s", hdev->name);
1224
1225 if (!data->diag_rx_ep)
1226 return -ENODEV;
1227
1228 urb = usb_alloc_urb(0, mem_flags);
1229 if (!urb)
1230 return -ENOMEM;
1231
1232 buf = kmalloc(size, mem_flags);
1233 if (!buf) {
1234 usb_free_urb(urb);
1235 return -ENOMEM;
1236 }
1237
1238 pipe = usb_rcvbulkpipe(data->udev, data->diag_rx_ep->bEndpointAddress);
1239
1240 usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
1241 btusb_diag_complete, hdev);
1242
1243 urb->transfer_flags |= URB_FREE_BUFFER;
1244
1245 usb_mark_last_busy(data->udev);
1246 usb_anchor_urb(urb, &data->diag_anchor);
1247
1248 err = usb_submit_urb(urb, mem_flags);
1249 if (err < 0) {
1250 if (err != -EPERM && err != -ENODEV)
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001251 bt_dev_err(hdev, "urb %p submission failed (%d)",
1252 urb, -err);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02001253 usb_unanchor_urb(urb);
1254 }
1255
1256 usb_free_urb(urb);
1257
1258 return err;
1259}
1260
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001261static void btusb_tx_complete(struct urb *urb)
1262{
1263 struct sk_buff *skb = urb->context;
Marcel Holtmann89e75332014-09-16 04:44:50 +02001264 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
David Herrmann155961e2012-02-09 21:58:32 +01001265 struct btusb_data *data = hci_get_drvdata(hdev);
Sebastian Andrzej Siewiora5e50d52018-06-19 23:56:57 +02001266 unsigned long flags;
Oliver Neukum7bee5492009-08-24 23:44:59 +02001267
Marcel Holtmann89e75332014-09-16 04:44:50 +02001268 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1269 urb->actual_length);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001270
1271 if (!test_bit(HCI_RUNNING, &hdev->flags))
1272 goto done;
1273
1274 if (!urb->status)
1275 hdev->stat.byte_tx += urb->transfer_buffer_length;
1276 else
1277 hdev->stat.err_tx++;
1278
1279done:
Sebastian Andrzej Siewiora5e50d52018-06-19 23:56:57 +02001280 spin_lock_irqsave(&data->txlock, flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001281 data->tx_in_flight--;
Sebastian Andrzej Siewiora5e50d52018-06-19 23:56:57 +02001282 spin_unlock_irqrestore(&data->txlock, flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001283
1284 kfree(urb->setup_packet);
1285
1286 kfree_skb(skb);
1287}
1288
1289static void btusb_isoc_tx_complete(struct urb *urb)
1290{
1291 struct sk_buff *skb = urb->context;
Marcel Holtmann89e75332014-09-16 04:44:50 +02001292 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001293
Marcel Holtmann89e75332014-09-16 04:44:50 +02001294 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1295 urb->actual_length);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001296
1297 if (!test_bit(HCI_RUNNING, &hdev->flags))
1298 goto done;
1299
1300 if (!urb->status)
1301 hdev->stat.byte_tx += urb->transfer_buffer_length;
1302 else
1303 hdev->stat.err_tx++;
1304
1305done:
1306 kfree(urb->setup_packet);
1307
1308 kfree_skb(skb);
1309}
1310
1311static int btusb_open(struct hci_dev *hdev)
1312{
David Herrmann155961e2012-02-09 21:58:32 +01001313 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001314 int err;
1315
1316 BT_DBG("%s", hdev->name);
1317
Ethan Hsiehc7e163f2016-10-07 12:06:42 +08001318 err = usb_autopm_get_interface(data->intf);
1319 if (err < 0)
1320 return err;
1321
Kim, Ben Young Taeace31982015-02-15 23:06:14 +00001322 /* Patching USB firmware files prior to starting any URBs of HCI path
1323 * It is more safe to use USB bulk channel for downloading USB patch
1324 */
1325 if (data->setup_on_usb) {
1326 err = data->setup_on_usb(hdev);
Marcel Holtmanneb500422015-04-16 23:15:50 +02001327 if (err < 0)
Oliver Neukum3d44a6f2019-11-14 16:01:18 +01001328 goto setup_fail;
Kim, Ben Young Taeace31982015-02-15 23:06:14 +00001329 }
1330
Oliver Neukum7bee5492009-08-24 23:44:59 +02001331 data->intf->needs_remote_wakeup = 1;
1332
Alex Lu9e455242019-08-14 20:02:52 +08001333 /* Disable device remote wakeup when host is suspended
1334 * For Realtek chips, global suspend without
1335 * SET_FEATURE (DEVICE_REMOTE_WAKEUP) can save more power in device.
1336 */
1337 if (test_bit(BTUSB_WAKEUP_DISABLE, &data->flags))
1338 device_wakeup_disable(&data->udev->dev);
1339
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001340 if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +02001341 goto done;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001342
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001343 err = btusb_submit_intr_urb(hdev, GFP_KERNEL);
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001344 if (err < 0)
1345 goto failed;
1346
1347 err = btusb_submit_bulk_urb(hdev, GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001348 if (err < 0) {
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001349 usb_kill_anchored_urbs(&data->intr_anchor);
1350 goto failed;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001351 }
1352
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001353 set_bit(BTUSB_BULK_RUNNING, &data->flags);
1354 btusb_submit_bulk_urb(hdev, GFP_KERNEL);
1355
Marcel Holtmann9d08f502015-10-08 20:23:08 +02001356 if (data->diag) {
1357 if (!btusb_submit_diag_urb(hdev, GFP_KERNEL))
1358 set_bit(BTUSB_DIAG_RUNNING, &data->flags);
1359 }
1360
Oliver Neukum7bee5492009-08-24 23:44:59 +02001361done:
1362 usb_autopm_put_interface(data->intf);
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001363 return 0;
1364
1365failed:
1366 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Oliver Neukum3d44a6f2019-11-14 16:01:18 +01001367setup_fail:
Oliver Neukum7bee5492009-08-24 23:44:59 +02001368 usb_autopm_put_interface(data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001369 return err;
1370}
1371
Oliver Neukum7bee5492009-08-24 23:44:59 +02001372static void btusb_stop_traffic(struct btusb_data *data)
1373{
1374 usb_kill_anchored_urbs(&data->intr_anchor);
1375 usb_kill_anchored_urbs(&data->bulk_anchor);
1376 usb_kill_anchored_urbs(&data->isoc_anchor);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02001377 usb_kill_anchored_urbs(&data->diag_anchor);
Sean Wanga1c49c432019-06-02 08:02:48 +08001378 usb_kill_anchored_urbs(&data->ctrl_anchor);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001379}
1380
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001381static int btusb_close(struct hci_dev *hdev)
1382{
David Herrmann155961e2012-02-09 21:58:32 +01001383 struct btusb_data *data = hci_get_drvdata(hdev);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001384 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001385
1386 BT_DBG("%s", hdev->name);
1387
Marcel Holtmanne8c3c3d2008-09-23 00:16:36 +02001388 cancel_work_sync(&data->work);
Linus Torvalds404291a2009-11-11 13:32:29 -08001389 cancel_work_sync(&data->waker);
Marcel Holtmanne8c3c3d2008-09-23 00:16:36 +02001390
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001391 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001392 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001393 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02001394 clear_bit(BTUSB_DIAG_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001395
1396 btusb_stop_traffic(data);
Marcel Holtmann803b5832014-09-16 08:00:29 +02001397 btusb_free_frags(data);
1398
Oliver Neukum7bee5492009-08-24 23:44:59 +02001399 err = usb_autopm_get_interface(data->intf);
1400 if (err < 0)
Oliver Neukum7b8e2c12009-11-13 14:26:23 +01001401 goto failed;
Oliver Neukum7bee5492009-08-24 23:44:59 +02001402
1403 data->intf->needs_remote_wakeup = 0;
Alex Lu9e455242019-08-14 20:02:52 +08001404
1405 /* Enable remote wake up for auto-suspend */
1406 if (test_bit(BTUSB_WAKEUP_DISABLE, &data->flags))
1407 data->intf->needs_remote_wakeup = 1;
1408
Oliver Neukum7bee5492009-08-24 23:44:59 +02001409 usb_autopm_put_interface(data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001410
Oliver Neukum7b8e2c12009-11-13 14:26:23 +01001411failed:
1412 usb_scuttle_anchored_urbs(&data->deferred);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001413 return 0;
1414}
1415
1416static int btusb_flush(struct hci_dev *hdev)
1417{
David Herrmann155961e2012-02-09 21:58:32 +01001418 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001419
1420 BT_DBG("%s", hdev->name);
1421
1422 usb_kill_anchored_urbs(&data->tx_anchor);
Marcel Holtmann803b5832014-09-16 08:00:29 +02001423 btusb_free_frags(data);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001424
1425 return 0;
1426}
1427
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001428static struct urb *alloc_ctrl_urb(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001429{
David Herrmann155961e2012-02-09 21:58:32 +01001430 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001431 struct usb_ctrlrequest *dr;
1432 struct urb *urb;
1433 unsigned int pipe;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001434
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001435 urb = usb_alloc_urb(0, GFP_KERNEL);
1436 if (!urb)
1437 return ERR_PTR(-ENOMEM);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001438
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001439 dr = kmalloc(sizeof(*dr), GFP_KERNEL);
1440 if (!dr) {
1441 usb_free_urb(urb);
1442 return ERR_PTR(-ENOMEM);
1443 }
1444
1445 dr->bRequestType = data->cmdreq_type;
Marcel Holtmann893ba542015-01-28 20:27:34 -08001446 dr->bRequest = data->cmdreq;
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001447 dr->wIndex = 0;
1448 dr->wValue = 0;
1449 dr->wLength = __cpu_to_le16(skb->len);
1450
1451 pipe = usb_sndctrlpipe(data->udev, 0x00);
1452
Marcel Holtmann89e75332014-09-16 04:44:50 +02001453 usb_fill_control_urb(urb, data->udev, pipe, (void *)dr,
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001454 skb->data, skb->len, btusb_tx_complete, skb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001455
Marcel Holtmann89e75332014-09-16 04:44:50 +02001456 skb->dev = (void *)hdev;
Marcel Holtmann7bd8f092013-10-11 06:19:18 -07001457
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001458 return urb;
1459}
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001460
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001461static struct urb *alloc_bulk_urb(struct hci_dev *hdev, struct sk_buff *skb)
1462{
1463 struct btusb_data *data = hci_get_drvdata(hdev);
1464 struct urb *urb;
1465 unsigned int pipe;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001466
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001467 if (!data->bulk_tx_ep)
1468 return ERR_PTR(-ENODEV);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001469
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001470 urb = usb_alloc_urb(0, GFP_KERNEL);
1471 if (!urb)
1472 return ERR_PTR(-ENOMEM);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001473
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001474 pipe = usb_sndbulkpipe(data->udev, data->bulk_tx_ep->bEndpointAddress);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001475
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001476 usb_fill_bulk_urb(urb, data->udev, pipe,
1477 skb->data, skb->len, btusb_tx_complete, skb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001478
Marcel Holtmann89e75332014-09-16 04:44:50 +02001479 skb->dev = (void *)hdev;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001480
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001481 return urb;
1482}
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001483
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001484static struct urb *alloc_isoc_urb(struct hci_dev *hdev, struct sk_buff *skb)
1485{
1486 struct btusb_data *data = hci_get_drvdata(hdev);
1487 struct urb *urb;
1488 unsigned int pipe;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001489
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001490 if (!data->isoc_tx_ep)
1491 return ERR_PTR(-ENODEV);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001492
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001493 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_KERNEL);
1494 if (!urb)
1495 return ERR_PTR(-ENOMEM);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001496
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001497 pipe = usb_sndisocpipe(data->udev, data->isoc_tx_ep->bEndpointAddress);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001498
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001499 usb_fill_int_urb(urb, data->udev, pipe,
1500 skb->data, skb->len, btusb_isoc_tx_complete,
1501 skb, data->isoc_tx_ep->bInterval);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001502
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001503 urb->transfer_flags = URB_ISO_ASAP;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001504
Sathish Narasimmanbaac6272020-04-03 21:43:59 +02001505 if (data->isoc_altsetting == 6)
1506 __fill_isoc_descriptor_msbc(urb, skb->len,
1507 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize),
1508 data);
1509 else
1510 __fill_isoc_descriptor(urb, skb->len,
1511 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
Marcel Holtmann89e75332014-09-16 04:44:50 +02001512 skb->dev = (void *)hdev;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001513
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001514 return urb;
1515}
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001516
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001517static int submit_tx_urb(struct hci_dev *hdev, struct urb *urb)
1518{
1519 struct btusb_data *data = hci_get_drvdata(hdev);
1520 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001521
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001522 usb_anchor_urb(urb, &data->tx_anchor);
1523
Johan Hedberge9753ef2014-09-14 08:49:34 +03001524 err = usb_submit_urb(urb, GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001525 if (err < 0) {
Paul Bolle5a9b80e2011-10-09 12:12:16 +02001526 if (err != -EPERM && err != -ENODEV)
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001527 bt_dev_err(hdev, "urb %p submission failed (%d)",
1528 urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001529 kfree(urb->setup_packet);
1530 usb_unanchor_urb(urb);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001531 } else {
1532 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001533 }
1534
Cong Wang54a8a792011-11-22 09:32:57 +08001535 usb_free_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001536 return err;
1537}
1538
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001539static int submit_or_queue_tx_urb(struct hci_dev *hdev, struct urb *urb)
1540{
1541 struct btusb_data *data = hci_get_drvdata(hdev);
1542 unsigned long flags;
1543 bool suspending;
1544
1545 spin_lock_irqsave(&data->txlock, flags);
1546 suspending = test_bit(BTUSB_SUSPENDING, &data->flags);
1547 if (!suspending)
1548 data->tx_in_flight++;
1549 spin_unlock_irqrestore(&data->txlock, flags);
1550
1551 if (!suspending)
1552 return submit_tx_urb(hdev, urb);
1553
1554 usb_anchor_urb(urb, &data->deferred);
1555 schedule_work(&data->waker);
1556
1557 usb_free_urb(urb);
1558 return 0;
1559}
1560
1561static int btusb_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
1562{
1563 struct urb *urb;
1564
1565 BT_DBG("%s", hdev->name);
1566
Marcel Holtmann618e8bc2015-11-05 07:33:56 +01001567 switch (hci_skb_pkt_type(skb)) {
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001568 case HCI_COMMAND_PKT:
1569 urb = alloc_ctrl_urb(hdev, skb);
1570 if (IS_ERR(urb))
1571 return PTR_ERR(urb);
1572
1573 hdev->stat.cmd_tx++;
1574 return submit_or_queue_tx_urb(hdev, urb);
1575
1576 case HCI_ACLDATA_PKT:
1577 urb = alloc_bulk_urb(hdev, skb);
1578 if (IS_ERR(urb))
1579 return PTR_ERR(urb);
1580
1581 hdev->stat.acl_tx++;
1582 return submit_or_queue_tx_urb(hdev, urb);
1583
1584 case HCI_SCODATA_PKT:
1585 if (hci_conn_num(hdev, SCO_LINK) < 1)
1586 return -ENODEV;
1587
1588 urb = alloc_isoc_urb(hdev, skb);
1589 if (IS_ERR(urb))
1590 return PTR_ERR(urb);
1591
1592 hdev->stat.sco_tx++;
1593 return submit_tx_urb(hdev, urb);
1594 }
1595
1596 return -EILSEQ;
1597}
1598
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001599static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
1600{
David Herrmann155961e2012-02-09 21:58:32 +01001601 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001602
1603 BT_DBG("%s evt %d", hdev->name, evt);
1604
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001605 if (hci_conn_num(hdev, SCO_LINK) != data->sco_num) {
1606 data->sco_num = hci_conn_num(hdev, SCO_LINK);
Sathish Narasimmanbaac6272020-04-03 21:43:59 +02001607 data->air_mode = evt;
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001608 schedule_work(&data->work);
Marcel Holtmanna780efa2008-11-30 12:17:12 +01001609 }
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001610}
1611
Jesper Juhl42b16b32011-01-17 00:09:38 +01001612static inline int __set_isoc_interface(struct hci_dev *hdev, int altsetting)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001613{
David Herrmann155961e2012-02-09 21:58:32 +01001614 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001615 struct usb_interface *intf = data->isoc;
1616 struct usb_endpoint_descriptor *ep_desc;
1617 int i, err;
1618
1619 if (!data->isoc)
1620 return -ENODEV;
1621
Marcel Holtmann459232f2017-10-24 19:42:45 +02001622 err = usb_set_interface(data->udev, data->isoc_ifnum, altsetting);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001623 if (err < 0) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001624 bt_dev_err(hdev, "setting interface failed (%d)", -err);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001625 return err;
1626 }
1627
1628 data->isoc_altsetting = altsetting;
1629
1630 data->isoc_tx_ep = NULL;
1631 data->isoc_rx_ep = NULL;
1632
1633 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
1634 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
1635
1636 if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) {
1637 data->isoc_tx_ep = ep_desc;
1638 continue;
1639 }
1640
1641 if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) {
1642 data->isoc_rx_ep = ep_desc;
1643 continue;
1644 }
1645 }
1646
1647 if (!data->isoc_tx_ep || !data->isoc_rx_ep) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001648 bt_dev_err(hdev, "invalid SCO descriptors");
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001649 return -ENODEV;
1650 }
1651
1652 return 0;
1653}
1654
Sathish Narasimmanbaac6272020-04-03 21:43:59 +02001655static int btusb_switch_alt_setting(struct hci_dev *hdev, int new_alts)
1656{
1657 struct btusb_data *data = hci_get_drvdata(hdev);
1658 int err;
1659
1660 if (data->isoc_altsetting != new_alts) {
1661 unsigned long flags;
1662
1663 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1664 usb_kill_anchored_urbs(&data->isoc_anchor);
1665
1666 /* When isochronous alternate setting needs to be
1667 * changed, because SCO connection has been added
1668 * or removed, a packet fragment may be left in the
1669 * reassembling state. This could lead to wrongly
1670 * assembled fragments.
1671 *
1672 * Clear outstanding fragment when selecting a new
1673 * alternate setting.
1674 */
1675 spin_lock_irqsave(&data->rxlock, flags);
1676 kfree_skb(data->sco_skb);
1677 data->sco_skb = NULL;
1678 spin_unlock_irqrestore(&data->rxlock, flags);
1679
1680 err = __set_isoc_interface(hdev, new_alts);
1681 if (err < 0)
1682 return err;
1683 }
1684
1685 if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
1686 if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0)
1687 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1688 else
1689 btusb_submit_isoc_urb(hdev, GFP_KERNEL);
1690 }
1691
1692 return 0;
1693}
1694
1695static struct usb_host_interface *btusb_find_altsetting(struct btusb_data *data,
1696 int alt)
1697{
1698 struct usb_interface *intf = data->isoc;
1699 int i;
1700
1701 BT_DBG("Looking for Alt no :%d", alt);
1702
Sathish Narasimmanfcd156ee2020-04-08 10:57:03 +05301703 if (!intf)
1704 return NULL;
1705
Sathish Narasimmanbaac6272020-04-03 21:43:59 +02001706 for (i = 0; i < intf->num_altsetting; i++) {
1707 if (intf->altsetting[i].desc.bAlternateSetting == alt)
1708 return &intf->altsetting[i];
1709 }
1710
1711 return NULL;
1712}
1713
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001714static void btusb_work(struct work_struct *work)
1715{
1716 struct btusb_data *data = container_of(work, struct btusb_data, work);
1717 struct hci_dev *hdev = data->hdev;
Sathish Narasimmanbaac6272020-04-03 21:43:59 +02001718 int new_alts = 0;
Oliver Neukum7bee5492009-08-24 23:44:59 +02001719 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001720
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001721 if (data->sco_num > 0) {
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -03001722 if (!test_bit(BTUSB_DID_ISO_RESUME, &data->flags)) {
Oliver Neukum8efdd0c2011-02-11 13:00:06 +01001723 err = usb_autopm_get_interface(data->isoc ? data->isoc : data->intf);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001724 if (err < 0) {
1725 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1726 usb_kill_anchored_urbs(&data->isoc_anchor);
1727 return;
1728 }
1729
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -03001730 set_bit(BTUSB_DID_ISO_RESUME, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001731 }
Mikel Astizf4001d22012-04-11 08:48:51 +02001732
Sathish Narasimmanbaac6272020-04-03 21:43:59 +02001733 if (data->air_mode == HCI_NOTIFY_ENABLE_SCO_CVSD) {
1734 if (hdev->voice_setting & 0x0020) {
1735 static const int alts[3] = { 2, 4, 5 };
Marcel Holtmann89e75332014-09-16 04:44:50 +02001736
Sathish Narasimmanbaac6272020-04-03 21:43:59 +02001737 new_alts = alts[data->sco_num - 1];
1738 } else {
1739 new_alts = data->sco_num;
1740 }
1741 } else if (data->air_mode == HCI_NOTIFY_ENABLE_SCO_TRANSP) {
Trent Piepho517b6932020-12-09 17:20:03 -08001742 /* Bluetooth USB spec recommends alt 6 (63 bytes), but
1743 * many adapters do not support it. Alt 1 appears to
1744 * work for all adapters that do not have alt 6, and
1745 * which work with WBS at all.
1746 */
1747 new_alts = btusb_find_altsetting(data, 6) ? 6 : 1;
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{
Hans de Goeded74e0ae2020-12-05 16:02:00 +01001792 struct btusb_data *data = hci_get_drvdata(hdev);
1793 u16 bcdDevice = le16_to_cpu(data->udev->descriptor.bcdDevice);
Marcel Holtmann81cac642014-01-03 03:02:36 -08001794 struct hci_rp_read_local_version *rp;
1795 struct sk_buff *skb;
Ismael Ferreras Morezuelascde1a8a2020-07-26 23:12:28 +02001796 bool is_fake = false;
Hans de Goede0671c0662020-12-05 16:02:01 +01001797 int ret;
Marcel Holtmann81cac642014-01-03 03:02:36 -08001798
1799 BT_DBG("%s", hdev->name);
1800
Marcel Holtmann7cd84d72015-06-07 10:01:02 +02001801 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
1802 HCI_INIT_TIMEOUT);
1803 if (IS_ERR(skb)) {
1804 int err = PTR_ERR(skb);
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001805 bt_dev_err(hdev, "CSR: Local version failed (%d)", err);
Marcel Holtmann7cd84d72015-06-07 10:01:02 +02001806 return err;
1807 }
1808
1809 if (skb->len != sizeof(struct hci_rp_read_local_version)) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001810 bt_dev_err(hdev, "CSR: Local version length mismatch");
Marcel Holtmann7cd84d72015-06-07 10:01:02 +02001811 kfree_skb(skb);
1812 return -EIO;
1813 }
Marcel Holtmann81cac642014-01-03 03:02:36 -08001814
Marcel Holtmann89e75332014-09-16 04:44:50 +02001815 rp = (struct hci_rp_read_local_version *)skb->data;
Marcel Holtmann81cac642014-01-03 03:02:36 -08001816
Ismael Ferreras Morezuelascde1a8a2020-07-26 23:12:28 +02001817 /* Detect a wide host of Chinese controllers that aren't CSR.
1818 *
1819 * Known fake bcdDevices: 0x0100, 0x0134, 0x1915, 0x2520, 0x7558, 0x8891
1820 *
1821 * The main thing they have in common is that these are really popular low-cost
1822 * options that support newer Bluetooth versions but rely on heavy VID/PID
1823 * squatting of this poor old Bluetooth 1.1 device. Even sold as such.
1824 *
1825 * We detect actual CSR devices by checking that the HCI manufacturer code
1826 * is Cambridge Silicon Radio (10) and ensuring that LMP sub-version and
1827 * HCI rev values always match. As they both store the firmware number.
1828 */
Johan Hedberg6cafcd92015-08-30 21:47:21 +03001829 if (le16_to_cpu(rp->manufacturer) != 10 ||
Ismael Ferreras Morezuelascde1a8a2020-07-26 23:12:28 +02001830 le16_to_cpu(rp->hci_rev) != le16_to_cpu(rp->lmp_subver))
1831 is_fake = true;
1832
1833 /* Known legit CSR firmware build numbers and their supported BT versions:
1834 * - 1.1 (0x1) -> 0x0073, 0x020d, 0x033c, 0x034e
1835 * - 1.2 (0x2) -> 0x04d9, 0x0529
1836 * - 2.0 (0x3) -> 0x07a6, 0x07ad, 0x0c5c
1837 * - 2.1 (0x4) -> 0x149c, 0x1735, 0x1899 (0x1899 is a BlueCore4-External)
1838 * - 4.0 (0x6) -> 0x1d86, 0x2031, 0x22bb
1839 *
1840 * e.g. Real CSR dongles with LMP subversion 0x73 are old enough that
1841 * support BT 1.1 only; so it's a dead giveaway when some
1842 * third-party BT 4.0 dongle reuses it.
1843 */
1844 else if (le16_to_cpu(rp->lmp_subver) <= 0x034e &&
1845 le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_1_1)
1846 is_fake = true;
1847
1848 else if (le16_to_cpu(rp->lmp_subver) <= 0x0529 &&
1849 le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_1_2)
1850 is_fake = true;
1851
1852 else if (le16_to_cpu(rp->lmp_subver) <= 0x0c5c &&
1853 le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_2_0)
1854 is_fake = true;
1855
1856 else if (le16_to_cpu(rp->lmp_subver) <= 0x1899 &&
1857 le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_2_1)
1858 is_fake = true;
1859
1860 else if (le16_to_cpu(rp->lmp_subver) <= 0x22bb &&
1861 le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_4_0)
1862 is_fake = true;
1863
Hans de Goeded74e0ae2020-12-05 16:02:00 +01001864 /* Other clones which beat all the above checks */
1865 else if (bcdDevice == 0x0134 &&
1866 le16_to_cpu(rp->lmp_subver) == 0x0c5c &&
1867 le16_to_cpu(rp->hci_ver) == BLUETOOTH_VER_2_0)
1868 is_fake = true;
1869
Ismael Ferreras Morezuelascde1a8a2020-07-26 23:12:28 +02001870 if (is_fake) {
1871 bt_dev_warn(hdev, "CSR: Unbranded CSR clone detected; adding workarounds...");
1872
1873 /* Generally these clones have big discrepancies between
1874 * advertised features and what's actually supported.
1875 * Probably will need to be expanded in the future;
1876 * without these the controller will lock up.
1877 */
1878 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
1879 set_bit(HCI_QUIRK_BROKEN_ERR_DATA_REPORTING, &hdev->quirks);
1880
Marcel Holtmann9641d342015-06-07 10:01:01 +02001881 /* Clear the reset quirk since this is not an actual
1882 * early Bluetooth 1.1 device from CSR.
1883 */
1884 clear_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Ismael Ferreras Morezuelascde1a8a2020-07-26 23:12:28 +02001885 clear_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
Hans de Goede0671c0662020-12-05 16:02:01 +01001886
1887 /*
1888 * Special workaround for clones with a Barrot 8041a02 chip,
1889 * these clones are really messed-up:
1890 * 1. Their bulk rx endpoint will never report any data unless
1891 * the device was suspended at least once (yes really).
1892 * 2. They will not wakeup when autosuspended and receiving data
1893 * on their bulk rx endpoint from e.g. a keyboard or mouse
1894 * (IOW remote-wakeup support is broken for the bulk endpoint).
1895 *
1896 * To fix 1. enable runtime-suspend, force-suspend the
1897 * hci and then wake-it up by disabling runtime-suspend.
1898 *
1899 * To fix 2. clear the hci's can_wake flag, this way the hci
1900 * will still be autosuspended when it is not open.
1901 */
1902 if (bcdDevice == 0x8891 &&
1903 le16_to_cpu(rp->lmp_subver) == 0x1012 &&
1904 le16_to_cpu(rp->hci_rev) == 0x0810 &&
1905 le16_to_cpu(rp->hci_ver) == BLUETOOTH_VER_4_0) {
Joe Perchesd84fc2c2020-12-22 02:31:49 -08001906 bt_dev_warn(hdev, "CSR: detected a fake CSR dongle using a Barrot 8041a02 chip, this chip is very buggy and may have issues");
Hans de Goede0671c0662020-12-05 16:02:01 +01001907
1908 pm_runtime_allow(&data->udev->dev);
1909
1910 ret = pm_runtime_suspend(&data->udev->dev);
1911 if (ret >= 0)
1912 msleep(200);
1913 else
Joe Perchesd84fc2c2020-12-22 02:31:49 -08001914 bt_dev_err(hdev, "Failed to suspend the device for Barrot 8041a02 receive-issue workaround");
Hans de Goede0671c0662020-12-05 16:02:01 +01001915
1916 pm_runtime_forbid(&data->udev->dev);
1917
1918 device_set_wakeup_capable(&data->udev->dev, false);
1919 /* Re-enable autosuspend if this was requested */
1920 if (enable_autosuspend)
1921 usb_enable_autosuspend(data->udev);
1922 }
Marcel Holtmann81cac642014-01-03 03:02:36 -08001923 }
1924
Marcel Holtmann81cac642014-01-03 03:02:36 -08001925 kfree_skb(skb);
1926
Marcel Holtmann9641d342015-06-07 10:01:01 +02001927 return 0;
Marcel Holtmann81cac642014-01-03 03:02:36 -08001928}
1929
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001930static const struct firmware *btusb_setup_intel_get_fw(struct hci_dev *hdev,
Marcel Holtmann89e75332014-09-16 04:44:50 +02001931 struct intel_version *ver)
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001932{
1933 const struct firmware *fw;
1934 char fwname[64];
1935 int ret;
1936
1937 snprintf(fwname, sizeof(fwname),
1938 "intel/ibt-hw-%x.%x.%x-fw-%x.%x.%x.%x.%x.bseq",
1939 ver->hw_platform, ver->hw_variant, ver->hw_revision,
1940 ver->fw_variant, ver->fw_revision, ver->fw_build_num,
1941 ver->fw_build_ww, ver->fw_build_yy);
1942
1943 ret = request_firmware(&fw, fwname, &hdev->dev);
1944 if (ret < 0) {
1945 if (ret == -EINVAL) {
Marcel Holtmann85418fe2018-08-03 11:20:49 +02001946 bt_dev_err(hdev, "Intel firmware file request failed (%d)",
1947 ret);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001948 return NULL;
1949 }
1950
Marcel Holtmann85418fe2018-08-03 11:20:49 +02001951 bt_dev_err(hdev, "failed to open Intel firmware file: %s (%d)",
1952 fwname, ret);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001953
1954 /* If the correct firmware patch file is not found, use the
1955 * default firmware patch file instead
1956 */
1957 snprintf(fwname, sizeof(fwname), "intel/ibt-hw-%x.%x.bseq",
1958 ver->hw_platform, ver->hw_variant);
1959 if (request_firmware(&fw, fwname, &hdev->dev) < 0) {
Marcel Holtmann85418fe2018-08-03 11:20:49 +02001960 bt_dev_err(hdev, "failed to open default fw file: %s",
1961 fwname);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001962 return NULL;
1963 }
1964 }
1965
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001966 bt_dev_info(hdev, "Intel Bluetooth firmware file: %s", fwname);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001967
1968 return fw;
1969}
1970
1971static int btusb_setup_intel_patching(struct hci_dev *hdev,
1972 const struct firmware *fw,
1973 const u8 **fw_ptr, int *disable_patch)
1974{
1975 struct sk_buff *skb;
1976 struct hci_command_hdr *cmd;
1977 const u8 *cmd_param;
1978 struct hci_event_hdr *evt = NULL;
1979 const u8 *evt_param = NULL;
1980 int remain = fw->size - (*fw_ptr - fw->data);
1981
1982 /* The first byte indicates the types of the patch command or event.
1983 * 0x01 means HCI command and 0x02 is HCI event. If the first bytes
1984 * in the current firmware buffer doesn't start with 0x01 or
1985 * the size of remain buffer is smaller than HCI command header,
1986 * the firmware file is corrupted and it should stop the patching
1987 * process.
1988 */
1989 if (remain > HCI_COMMAND_HDR_SIZE && *fw_ptr[0] != 0x01) {
Marcel Holtmann85418fe2018-08-03 11:20:49 +02001990 bt_dev_err(hdev, "Intel fw corrupted: invalid cmd read");
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001991 return -EINVAL;
1992 }
1993 (*fw_ptr)++;
1994 remain--;
1995
1996 cmd = (struct hci_command_hdr *)(*fw_ptr);
1997 *fw_ptr += sizeof(*cmd);
1998 remain -= sizeof(*cmd);
1999
2000 /* Ensure that the remain firmware data is long enough than the length
2001 * of command parameter. If not, the firmware file is corrupted.
2002 */
2003 if (remain < cmd->plen) {
Marcel Holtmann85418fe2018-08-03 11:20:49 +02002004 bt_dev_err(hdev, "Intel fw corrupted: invalid cmd len");
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002005 return -EFAULT;
2006 }
2007
2008 /* If there is a command that loads a patch in the firmware
2009 * file, then enable the patch upon success, otherwise just
2010 * disable the manufacturer mode, for example patch activation
2011 * is not required when the default firmware patch file is used
2012 * because there are no patch data to load.
2013 */
2014 if (*disable_patch && le16_to_cpu(cmd->opcode) == 0xfc8e)
2015 *disable_patch = 0;
2016
2017 cmd_param = *fw_ptr;
2018 *fw_ptr += cmd->plen;
2019 remain -= cmd->plen;
2020
2021 /* This reads the expected events when the above command is sent to the
2022 * device. Some vendor commands expects more than one events, for
2023 * example command status event followed by vendor specific event.
2024 * For this case, it only keeps the last expected event. so the command
2025 * can be sent with __hci_cmd_sync_ev() which returns the sk_buff of
2026 * last expected event.
2027 */
2028 while (remain > HCI_EVENT_HDR_SIZE && *fw_ptr[0] == 0x02) {
2029 (*fw_ptr)++;
2030 remain--;
2031
2032 evt = (struct hci_event_hdr *)(*fw_ptr);
2033 *fw_ptr += sizeof(*evt);
2034 remain -= sizeof(*evt);
2035
2036 if (remain < evt->plen) {
Marcel Holtmann85418fe2018-08-03 11:20:49 +02002037 bt_dev_err(hdev, "Intel fw corrupted: invalid evt len");
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002038 return -EFAULT;
2039 }
2040
2041 evt_param = *fw_ptr;
2042 *fw_ptr += evt->plen;
2043 remain -= evt->plen;
2044 }
2045
2046 /* Every HCI commands in the firmware file has its correspond event.
2047 * If event is not found or remain is smaller than zero, the firmware
2048 * file is corrupted.
2049 */
2050 if (!evt || !evt_param || remain < 0) {
Marcel Holtmann85418fe2018-08-03 11:20:49 +02002051 bt_dev_err(hdev, "Intel fw corrupted: invalid evt read");
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002052 return -EFAULT;
2053 }
2054
2055 skb = __hci_cmd_sync_ev(hdev, le16_to_cpu(cmd->opcode), cmd->plen,
2056 cmd_param, evt->evt, HCI_INIT_TIMEOUT);
2057 if (IS_ERR(skb)) {
Marcel Holtmann85418fe2018-08-03 11:20:49 +02002058 bt_dev_err(hdev, "sending Intel patch command (0x%4.4x) failed (%ld)",
2059 cmd->opcode, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08002060 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002061 }
2062
2063 /* It ensures that the returned event matches the event data read from
2064 * the firmware file. At fist, it checks the length and then
2065 * the contents of the event.
2066 */
2067 if (skb->len != evt->plen) {
Marcel Holtmann85418fe2018-08-03 11:20:49 +02002068 bt_dev_err(hdev, "mismatch event length (opcode 0x%4.4x)",
2069 le16_to_cpu(cmd->opcode));
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002070 kfree_skb(skb);
2071 return -EFAULT;
2072 }
2073
2074 if (memcmp(skb->data, evt_param, evt->plen)) {
Marcel Holtmann85418fe2018-08-03 11:20:49 +02002075 bt_dev_err(hdev, "mismatch event parameter (opcode 0x%4.4x)",
2076 le16_to_cpu(cmd->opcode));
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002077 kfree_skb(skb);
2078 return -EFAULT;
2079 }
2080 kfree_skb(skb);
2081
2082 return 0;
2083}
2084
2085static int btusb_setup_intel(struct hci_dev *hdev)
2086{
2087 struct sk_buff *skb;
2088 const struct firmware *fw;
2089 const u8 *fw_ptr;
Loic Poulain28dc4b92015-12-03 16:10:22 +01002090 int disable_patch, err;
Loic Poulain6c483de2015-12-06 16:18:34 +01002091 struct intel_version ver;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002092
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002093 BT_DBG("%s", hdev->name);
2094
2095 /* The controller has a bug with the first HCI command sent to it
2096 * returning number of completed commands as zero. This would stall the
2097 * command processing in the Bluetooth core.
2098 *
2099 * As a workaround, send HCI Reset command first which will reset the
2100 * number of completed commands and allow normal command processing
2101 * from now on.
2102 */
2103 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
2104 if (IS_ERR(skb)) {
Marcel Holtmann85418fe2018-08-03 11:20:49 +02002105 bt_dev_err(hdev, "sending initial HCI reset command failed (%ld)",
2106 PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08002107 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002108 }
2109 kfree_skb(skb);
2110
2111 /* Read Intel specific controller version first to allow selection of
2112 * which firmware file to load.
2113 *
2114 * The returned information are hardware variant and revision plus
2115 * firmware variant, revision and build number.
2116 */
Loic Poulain6c483de2015-12-06 16:18:34 +01002117 err = btintel_read_version(hdev, &ver);
2118 if (err)
2119 return err;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002120
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002121 bt_dev_info(hdev, "read Intel version: %02x%02x%02x%02x%02x%02x%02x%02x%02x",
2122 ver.hw_platform, ver.hw_variant, ver.hw_revision,
2123 ver.fw_variant, ver.fw_revision, ver.fw_build_num,
2124 ver.fw_build_ww, ver.fw_build_yy, ver.fw_patch_num);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002125
2126 /* fw_patch_num indicates the version of patch the device currently
2127 * have. If there is no patch data in the device, it is always 0x00.
Minjune Kim5075eda2015-08-27 13:21:52 +09002128 * So, if it is other than 0x00, no need to patch the device again.
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002129 */
Loic Poulain6c483de2015-12-06 16:18:34 +01002130 if (ver.fw_patch_num) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002131 bt_dev_info(hdev, "Intel device is already patched. "
2132 "patch num: %02x", ver.fw_patch_num);
Marcel Holtmann213445b2015-10-21 02:45:19 +02002133 goto complete;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002134 }
2135
2136 /* Opens the firmware patch file based on the firmware version read
2137 * from the controller. If it fails to open the matching firmware
2138 * patch file, it tries to open the default firmware patch file.
2139 * If no patch file is found, allow the device to operate without
2140 * a patch.
2141 */
Loic Poulain6c483de2015-12-06 16:18:34 +01002142 fw = btusb_setup_intel_get_fw(hdev, &ver);
2143 if (!fw)
Marcel Holtmann213445b2015-10-21 02:45:19 +02002144 goto complete;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002145 fw_ptr = fw->data;
2146
Loic Poulain28dc4b92015-12-03 16:10:22 +01002147 /* Enable the manufacturer mode of the controller.
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002148 * Only while this mode is enabled, the driver can download the
2149 * firmware patch data and configuration parameters.
2150 */
Loic Poulain28dc4b92015-12-03 16:10:22 +01002151 err = btintel_enter_mfg(hdev);
2152 if (err) {
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002153 release_firmware(fw);
Loic Poulain28dc4b92015-12-03 16:10:22 +01002154 return err;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002155 }
2156
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002157 disable_patch = 1;
2158
2159 /* The firmware data file consists of list of Intel specific HCI
2160 * commands and its expected events. The first byte indicates the
2161 * type of the message, either HCI command or HCI event.
2162 *
2163 * It reads the command and its expected event from the firmware file,
2164 * and send to the controller. Once __hci_cmd_sync_ev() returns,
2165 * the returned event is compared with the event read from the firmware
2166 * file and it will continue until all the messages are downloaded to
2167 * the controller.
2168 *
2169 * Once the firmware patching is completed successfully,
2170 * the manufacturer mode is disabled with reset and activating the
2171 * downloaded patch.
2172 *
2173 * If the firmware patching fails, the manufacturer mode is
2174 * disabled with reset and deactivating the patch.
2175 *
2176 * If the default patch file is used, no reset is done when disabling
2177 * the manufacturer.
2178 */
2179 while (fw->size > fw_ptr - fw->data) {
2180 int ret;
2181
2182 ret = btusb_setup_intel_patching(hdev, fw, &fw_ptr,
2183 &disable_patch);
2184 if (ret < 0)
2185 goto exit_mfg_deactivate;
2186 }
2187
2188 release_firmware(fw);
2189
2190 if (disable_patch)
2191 goto exit_mfg_disable;
2192
2193 /* Patching completed successfully and disable the manufacturer mode
2194 * with reset and activate the downloaded firmware patches.
2195 */
Loic Poulain28dc4b92015-12-03 16:10:22 +01002196 err = btintel_exit_mfg(hdev, true, true);
2197 if (err)
2198 return err;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002199
Sukumar Ghorai905d7b12020-03-16 11:37:18 +05302200 /* Need build number for downloaded fw patches in
2201 * every power-on boot
2202 */
2203 err = btintel_read_version(hdev, &ver);
2204 if (err)
2205 return err;
2206 bt_dev_info(hdev, "Intel BT fw patch 0x%02x completed & activated",
2207 ver.fw_patch_num);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002208
Marcel Holtmann213445b2015-10-21 02:45:19 +02002209 goto complete;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002210
2211exit_mfg_disable:
2212 /* Disable the manufacturer mode without reset */
Loic Poulain28dc4b92015-12-03 16:10:22 +01002213 err = btintel_exit_mfg(hdev, false, false);
2214 if (err)
2215 return err;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002216
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002217 bt_dev_info(hdev, "Intel firmware patch completed");
Marcel Holtmann40cb0982014-07-02 12:06:45 +02002218
Marcel Holtmann213445b2015-10-21 02:45:19 +02002219 goto complete;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002220
2221exit_mfg_deactivate:
2222 release_firmware(fw);
2223
2224 /* Patching failed. Disable the manufacturer mode with reset and
2225 * deactivate the downloaded firmware patches.
2226 */
Loic Poulain28dc4b92015-12-03 16:10:22 +01002227 err = btintel_exit_mfg(hdev, true, false);
2228 if (err)
2229 return err;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002230
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002231 bt_dev_info(hdev, "Intel firmware patch completed and deactivated");
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002232
Marcel Holtmann213445b2015-10-21 02:45:19 +02002233complete:
2234 /* Set the event mask for Intel specific vendor events. This enables
2235 * a few extra events that are useful during general operation.
2236 */
2237 btintel_set_event_mask_mfg(hdev, false);
2238
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07002239 btintel_check_bdaddr(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002240 return 0;
2241}
2242
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002243static int inject_cmd_complete(struct hci_dev *hdev, __u16 opcode)
2244{
2245 struct sk_buff *skb;
2246 struct hci_event_hdr *hdr;
2247 struct hci_ev_cmd_complete *evt;
2248
Jia-Ju Baicf07e342018-07-23 11:38:51 +08002249 skb = bt_skb_alloc(sizeof(*hdr) + sizeof(*evt) + 1, GFP_KERNEL);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002250 if (!skb)
2251 return -ENOMEM;
2252
Johannes Berg4df864c2017-06-16 14:29:21 +02002253 hdr = skb_put(skb, sizeof(*hdr));
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002254 hdr->evt = HCI_EV_CMD_COMPLETE;
2255 hdr->plen = sizeof(*evt) + 1;
2256
Johannes Berg4df864c2017-06-16 14:29:21 +02002257 evt = skb_put(skb, sizeof(*evt));
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002258 evt->ncmd = 0x01;
2259 evt->opcode = cpu_to_le16(opcode);
2260
Johannes Berg634fef62017-06-16 14:29:24 +02002261 skb_put_u8(skb, 0x00);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002262
Marcel Holtmann618e8bc2015-11-05 07:33:56 +01002263 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002264
2265 return hci_recv_frame(hdev, skb);
2266}
2267
2268static int btusb_recv_bulk_intel(struct btusb_data *data, void *buffer,
2269 int count)
2270{
2271 /* When the device is in bootloader mode, then it can send
2272 * events via the bulk endpoint. These events are treated the
2273 * same way as the ones received from the interrupt endpoint.
2274 */
2275 if (test_bit(BTUSB_BOOTLOADER, &data->flags))
2276 return btusb_recv_intr(data, buffer, count);
2277
2278 return btusb_recv_bulk(data, buffer, count);
2279}
2280
Marcel Holtmannccd6da22015-04-09 00:35:19 -07002281static void btusb_intel_bootup(struct btusb_data *data, const void *ptr,
2282 unsigned int len)
2283{
2284 const struct intel_bootup *evt = ptr;
2285
2286 if (len != sizeof(*evt))
2287 return;
2288
Andrea Parridff6d592018-11-27 12:22:25 +01002289 if (test_and_clear_bit(BTUSB_BOOTING, &data->flags))
Marcel Holtmannccd6da22015-04-09 00:35:19 -07002290 wake_up_bit(&data->flags, BTUSB_BOOTING);
Marcel Holtmannccd6da22015-04-09 00:35:19 -07002291}
2292
2293static void btusb_intel_secure_send_result(struct btusb_data *data,
2294 const void *ptr, unsigned int len)
2295{
2296 const struct intel_secure_send_result *evt = ptr;
2297
2298 if (len != sizeof(*evt))
2299 return;
2300
2301 if (evt->result)
2302 set_bit(BTUSB_FIRMWARE_FAILED, &data->flags);
2303
2304 if (test_and_clear_bit(BTUSB_DOWNLOADING, &data->flags) &&
Andrea Parridff6d592018-11-27 12:22:25 +01002305 test_bit(BTUSB_FIRMWARE_LOADED, &data->flags))
Marcel Holtmannccd6da22015-04-09 00:35:19 -07002306 wake_up_bit(&data->flags, BTUSB_DOWNLOADING);
Marcel Holtmannccd6da22015-04-09 00:35:19 -07002307}
2308
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002309static int btusb_recv_event_intel(struct hci_dev *hdev, struct sk_buff *skb)
2310{
2311 struct btusb_data *data = hci_get_drvdata(hdev);
2312
2313 if (test_bit(BTUSB_BOOTLOADER, &data->flags)) {
2314 struct hci_event_hdr *hdr = (void *)skb->data;
2315
Marcel Holtmannccd6da22015-04-09 00:35:19 -07002316 if (skb->len > HCI_EVENT_HDR_SIZE && hdr->evt == 0xff &&
2317 hdr->plen > 0) {
2318 const void *ptr = skb->data + HCI_EVENT_HDR_SIZE + 1;
2319 unsigned int len = skb->len - HCI_EVENT_HDR_SIZE - 1;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002320
Marcel Holtmannccd6da22015-04-09 00:35:19 -07002321 switch (skb->data[2]) {
2322 case 0x02:
2323 /* When switching to the operational firmware
2324 * the device sends a vendor specific event
2325 * indicating that the bootup completed.
2326 */
2327 btusb_intel_bootup(data, ptr, len);
2328 break;
2329 case 0x06:
2330 /* When the firmware loading completes the
2331 * device sends out a vendor specific event
2332 * indicating the result of the firmware
2333 * loading.
2334 */
2335 btusb_intel_secure_send_result(data, ptr, len);
2336 break;
Johan Hedbergfad70972015-01-30 10:58:55 +02002337 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002338 }
2339 }
2340
2341 return hci_recv_frame(hdev, skb);
2342}
2343
2344static int btusb_send_frame_intel(struct hci_dev *hdev, struct sk_buff *skb)
2345{
2346 struct btusb_data *data = hci_get_drvdata(hdev);
2347 struct urb *urb;
2348
2349 BT_DBG("%s", hdev->name);
2350
Marcel Holtmann618e8bc2015-11-05 07:33:56 +01002351 switch (hci_skb_pkt_type(skb)) {
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002352 case HCI_COMMAND_PKT:
2353 if (test_bit(BTUSB_BOOTLOADER, &data->flags)) {
2354 struct hci_command_hdr *cmd = (void *)skb->data;
2355 __u16 opcode = le16_to_cpu(cmd->opcode);
2356
2357 /* When in bootloader mode and the command 0xfc09
2358 * is received, it needs to be send down the
2359 * bulk endpoint. So allocate a bulk URB instead.
2360 */
2361 if (opcode == 0xfc09)
2362 urb = alloc_bulk_urb(hdev, skb);
2363 else
2364 urb = alloc_ctrl_urb(hdev, skb);
2365
2366 /* When the 0xfc01 command is issued to boot into
2367 * the operational firmware, it will actually not
2368 * send a command complete event. To keep the flow
2369 * control working inject that event here.
2370 */
2371 if (opcode == 0xfc01)
2372 inject_cmd_complete(hdev, opcode);
2373 } else {
2374 urb = alloc_ctrl_urb(hdev, skb);
2375 }
2376 if (IS_ERR(urb))
2377 return PTR_ERR(urb);
2378
2379 hdev->stat.cmd_tx++;
2380 return submit_or_queue_tx_urb(hdev, urb);
2381
2382 case HCI_ACLDATA_PKT:
2383 urb = alloc_bulk_urb(hdev, skb);
2384 if (IS_ERR(urb))
2385 return PTR_ERR(urb);
2386
2387 hdev->stat.acl_tx++;
2388 return submit_or_queue_tx_urb(hdev, urb);
2389
2390 case HCI_SCODATA_PKT:
2391 if (hci_conn_num(hdev, SCO_LINK) < 1)
2392 return -ENODEV;
2393
2394 urb = alloc_isoc_urb(hdev, skb);
2395 if (IS_ERR(urb))
2396 return PTR_ERR(urb);
2397
2398 hdev->stat.sco_tx++;
2399 return submit_tx_urb(hdev, urb);
2400 }
2401
2402 return -EILSEQ;
2403}
2404
Raghuram Hegde2da711bc2018-12-19 11:42:18 +05302405static bool btusb_setup_intel_new_get_fw_name(struct intel_version *ver,
2406 struct intel_boot_params *params,
2407 char *fw_name, size_t len,
2408 const char *suffix)
2409{
2410 switch (ver->hw_variant) {
2411 case 0x0b: /* SfP */
2412 case 0x0c: /* WsP */
2413 snprintf(fw_name, len, "intel/ibt-%u-%u.%s",
2414 le16_to_cpu(ver->hw_variant),
2415 le16_to_cpu(params->dev_revid),
2416 suffix);
2417 break;
2418 case 0x11: /* JfP */
2419 case 0x12: /* ThP */
2420 case 0x13: /* HrP */
2421 case 0x14: /* CcP */
2422 snprintf(fw_name, len, "intel/ibt-%u-%u-%u.%s",
2423 le16_to_cpu(ver->hw_variant),
2424 le16_to_cpu(ver->hw_revision),
2425 le16_to_cpu(ver->fw_revision),
2426 suffix);
2427 break;
2428 default:
2429 return false;
2430 }
2431 return true;
2432}
2433
Kiran K9a93b8b2020-11-19 17:09:41 +05302434static void btusb_setup_intel_newgen_get_fw_name(const struct intel_version_tlv *ver_tlv,
2435 char *fw_name, size_t len,
2436 const char *suffix)
2437{
2438 /* The firmware file name for new generation controllers will be
2439 * ibt-<cnvi_top type+cnvi_top step>-<cnvr_top type+cnvr_top step>
2440 */
2441 snprintf(fw_name, len, "intel/ibt-%04x-%04x.%s",
2442 INTEL_CNVX_TOP_PACK_SWAB(INTEL_CNVX_TOP_TYPE(ver_tlv->cnvi_top),
2443 INTEL_CNVX_TOP_STEP(ver_tlv->cnvi_top)),
2444 INTEL_CNVX_TOP_PACK_SWAB(INTEL_CNVX_TOP_TYPE(ver_tlv->cnvr_top),
2445 INTEL_CNVX_TOP_STEP(ver_tlv->cnvr_top)),
2446 suffix);
2447}
2448
Kiran K3f43a372020-11-19 17:09:42 +05302449static int btusb_intel_download_firmware_newgen(struct hci_dev *hdev,
2450 struct intel_version_tlv *ver,
2451 u32 *boot_param)
2452{
2453 const struct firmware *fw;
2454 char fwname[64];
2455 int err;
2456 struct btusb_data *data = hci_get_drvdata(hdev);
2457
2458 if (!ver || !boot_param)
2459 return -EINVAL;
2460
2461 /* The hardware platform number has a fixed value of 0x37 and
2462 * for now only accept this single value.
2463 */
2464 if (INTEL_HW_PLATFORM(ver->cnvi_bt) != 0x37) {
2465 bt_dev_err(hdev, "Unsupported Intel hardware platform (0x%2x)",
2466 INTEL_HW_PLATFORM(ver->cnvi_bt));
2467 return -EINVAL;
2468 }
2469
2470 /* The firmware variant determines if the device is in bootloader
2471 * mode or is running operational firmware. The value 0x03 identifies
2472 * the bootloader and the value 0x23 identifies the operational
2473 * firmware.
2474 *
2475 * When the operational firmware is already present, then only
2476 * the check for valid Bluetooth device address is needed. This
2477 * determines if the device will be added as configured or
2478 * unconfigured controller.
2479 *
2480 * It is not possible to use the Secure Boot Parameters in this
2481 * case since that command is only available in bootloader mode.
2482 */
2483 if (ver->img_type == 0x03) {
2484 clear_bit(BTUSB_BOOTLOADER, &data->flags);
2485 btintel_check_bdaddr(hdev);
2486 return 0;
2487 }
2488
2489 /* Check for supported iBT hardware variants of this firmware
2490 * loading method.
2491 *
2492 * This check has been put in place to ensure correct forward
2493 * compatibility options when newer hardware variants come along.
2494 */
2495 switch (INTEL_HW_VARIANT(ver->cnvi_bt)) {
2496 case 0x17: /* TyP */
2497 case 0x18: /* Slr */
2498 case 0x19: /* Slr-F */
2499 break;
2500 default:
2501 bt_dev_err(hdev, "Unsupported Intel hardware variant (0x%x)",
2502 INTEL_HW_VARIANT(ver->cnvi_bt));
2503 return -EINVAL;
2504 }
2505
2506 /* If the device is not in bootloader mode, then the only possible
2507 * choice is to return an error and abort the device initialization.
2508 */
2509 if (ver->img_type != 0x01) {
2510 bt_dev_err(hdev, "Unsupported Intel firmware variant (0x%x)",
2511 ver->img_type);
2512 return -ENODEV;
2513 }
2514
2515 /* It is required that every single firmware fragment is acknowledged
2516 * with a command complete event. If the boot parameters indicate
2517 * that this bootloader does not send them, then abort the setup.
2518 */
2519 if (ver->limited_cce != 0x00) {
2520 bt_dev_err(hdev, "Unsupported Intel firmware loading method (0x%x)",
2521 ver->limited_cce);
2522 return -EINVAL;
2523 }
2524
2525 /* Secure boot engine type should be either 1 (ECDSA) or 0 (RSA) */
2526 if (ver->sbe_type > 0x01) {
2527 bt_dev_err(hdev, "Unsupported Intel secure boot engine type (0x%x)",
2528 ver->sbe_type);
2529 return -EINVAL;
2530 }
2531
2532 /* If the OTP has no valid Bluetooth device address, then there will
2533 * also be no valid address for the operational firmware.
2534 */
2535 if (!bacmp(&ver->otp_bd_addr, BDADDR_ANY)) {
2536 bt_dev_info(hdev, "No device address configured");
2537 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
2538 }
2539
2540 btusb_setup_intel_newgen_get_fw_name(ver, fwname, sizeof(fwname), "sfi");
2541 err = request_firmware(&fw, fwname, &hdev->dev);
2542 if (err < 0) {
Kiran K62acbbb2021-03-08 16:44:54 +05302543 bt_dev_err(hdev, "Failed to load Intel firmware file %s (%d)",
2544 fwname, err);
Kiran K3f43a372020-11-19 17:09:42 +05302545 return err;
2546 }
2547
2548 bt_dev_info(hdev, "Found device firmware: %s", fwname);
2549
2550 if (fw->size < 644) {
2551 bt_dev_err(hdev, "Invalid size of firmware file (%zu)",
2552 fw->size);
2553 err = -EBADF;
2554 goto done;
2555 }
2556
2557 set_bit(BTUSB_DOWNLOADING, &data->flags);
2558
2559 /* Start firmware downloading and get boot parameter */
2560 err = btintel_download_firmware_newgen(hdev, fw, boot_param,
2561 INTEL_HW_VARIANT(ver->cnvi_bt),
2562 ver->sbe_type);
2563 if (err < 0) {
2564 /* When FW download fails, send Intel Reset to retry
2565 * FW download.
2566 */
2567 btintel_reset_to_bootloader(hdev);
2568 goto done;
2569 }
2570 set_bit(BTUSB_FIRMWARE_LOADED, &data->flags);
2571
2572 bt_dev_info(hdev, "Waiting for firmware download to complete");
2573
2574 /* Before switching the device into operational mode and with that
2575 * booting the loaded firmware, wait for the bootloader notification
2576 * that all fragments have been successfully received.
2577 *
2578 * When the event processing receives the notification, then the
2579 * BTUSB_DOWNLOADING flag will be cleared.
2580 *
2581 * The firmware loading should not take longer than 5 seconds
2582 * and thus just timeout if that happens and fail the setup
2583 * of this device.
2584 */
2585 err = wait_on_bit_timeout(&data->flags, BTUSB_DOWNLOADING,
2586 TASK_INTERRUPTIBLE,
2587 msecs_to_jiffies(5000));
2588 if (err == -EINTR) {
2589 bt_dev_err(hdev, "Firmware loading interrupted");
2590 goto done;
2591 }
2592
2593 if (err) {
2594 bt_dev_err(hdev, "Firmware loading timeout");
2595 err = -ETIMEDOUT;
2596 btintel_reset_to_bootloader(hdev);
2597 goto done;
2598 }
2599
2600 if (test_bit(BTUSB_FIRMWARE_FAILED, &data->flags)) {
2601 bt_dev_err(hdev, "Firmware loading failed");
2602 err = -ENOEXEC;
2603 goto done;
2604 }
2605
2606done:
2607 release_firmware(fw);
2608 return err;
2609}
2610
Kiran K5ea7c812020-06-25 00:04:32 +05302611static int btusb_intel_download_firmware(struct hci_dev *hdev,
2612 struct intel_version *ver,
Kiran Kdc45d372020-08-26 15:24:32 +05302613 struct intel_boot_params *params,
2614 u32 *boot_param)
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002615{
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002616 const struct firmware *fw;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002617 char fwname[64];
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002618 int err;
Kiran K5ea7c812020-06-25 00:04:32 +05302619 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002620
Kiran K5ea7c812020-06-25 00:04:32 +05302621 if (!ver || !params)
2622 return -EINVAL;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002623
2624 /* The hardware platform number has a fixed value of 0x37 and
2625 * for now only accept this single value.
2626 */
Kiran K5ea7c812020-06-25 00:04:32 +05302627 if (ver->hw_platform != 0x37) {
Marcel Holtmann85418fe2018-08-03 11:20:49 +02002628 bt_dev_err(hdev, "Unsupported Intel hardware platform (%u)",
Kiran K5ea7c812020-06-25 00:04:32 +05302629 ver->hw_platform);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002630 return -EINVAL;
2631 }
2632
Tedd Ho-Jeong An92688342017-03-06 15:38:26 -08002633 /* Check for supported iBT hardware variants of this firmware
2634 * loading method.
Tedd Ho-Jeong Ana0af53b2016-05-06 11:53:46 -07002635 *
2636 * This check has been put in place to ensure correct forward
2637 * compatibility options when newer hardware variants come along.
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002638 */
Kiran K5ea7c812020-06-25 00:04:32 +05302639 switch (ver->hw_variant) {
Tedd Ho-Jeong An92688342017-03-06 15:38:26 -08002640 case 0x0b: /* SfP */
2641 case 0x0c: /* WsP */
Tedd Ho-Jeong An86a61292017-05-01 13:35:12 -07002642 case 0x11: /* JfP */
Marcel Holtmannde766142017-03-06 15:38:28 -08002643 case 0x12: /* ThP */
Tedd Ho-Jeong An1ce0cec2018-02-05 14:20:36 -08002644 case 0x13: /* HrP */
Raghuram Hegde2da711bc2018-12-19 11:42:18 +05302645 case 0x14: /* CcP */
Tedd Ho-Jeong An92688342017-03-06 15:38:26 -08002646 break;
2647 default:
Marcel Holtmann85418fe2018-08-03 11:20:49 +02002648 bt_dev_err(hdev, "Unsupported Intel hardware variant (%u)",
Kiran K5ea7c812020-06-25 00:04:32 +05302649 ver->hw_variant);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002650 return -EINVAL;
2651 }
2652
Kiran K5ea7c812020-06-25 00:04:32 +05302653 btintel_version_info(hdev, ver);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002654
2655 /* The firmware variant determines if the device is in bootloader
2656 * mode or is running operational firmware. The value 0x06 identifies
2657 * the bootloader and the value 0x23 identifies the operational
2658 * firmware.
2659 *
2660 * When the operational firmware is already present, then only
2661 * the check for valid Bluetooth device address is needed. This
2662 * determines if the device will be added as configured or
2663 * unconfigured controller.
2664 *
2665 * It is not possible to use the Secure Boot Parameters in this
2666 * case since that command is only available in bootloader mode.
2667 */
Kiran K5ea7c812020-06-25 00:04:32 +05302668 if (ver->fw_variant == 0x23) {
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002669 clear_bit(BTUSB_BOOTLOADER, &data->flags);
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07002670 btintel_check_bdaddr(hdev);
Kiran K5ea7c812020-06-25 00:04:32 +05302671 return 0;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002672 }
2673
2674 /* If the device is not in bootloader mode, then the only possible
2675 * choice is to return an error and abort the device initialization.
2676 */
Kiran K5ea7c812020-06-25 00:04:32 +05302677 if (ver->fw_variant != 0x06) {
Marcel Holtmann85418fe2018-08-03 11:20:49 +02002678 bt_dev_err(hdev, "Unsupported Intel firmware variant (%u)",
Kiran K5ea7c812020-06-25 00:04:32 +05302679 ver->fw_variant);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002680 return -ENODEV;
2681 }
2682
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002683 /* Read the secure boot parameters to identify the operating
2684 * details of the bootloader.
2685 */
Kiran K5ea7c812020-06-25 00:04:32 +05302686 err = btintel_read_boot_params(hdev, params);
Tedd Ho-Jeong Anfaf174d2018-01-24 09:19:20 -08002687 if (err)
2688 return err;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002689
2690 /* It is required that every single firmware fragment is acknowledged
2691 * with a command complete event. If the boot parameters indicate
2692 * that this bootloader does not send them, then abort the setup.
2693 */
Kiran K5ea7c812020-06-25 00:04:32 +05302694 if (params->limited_cce != 0x00) {
Marcel Holtmann85418fe2018-08-03 11:20:49 +02002695 bt_dev_err(hdev, "Unsupported Intel firmware loading method (%u)",
Kiran K5ea7c812020-06-25 00:04:32 +05302696 params->limited_cce);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002697 return -EINVAL;
2698 }
2699
2700 /* If the OTP has no valid Bluetooth device address, then there will
2701 * also be no valid address for the operational firmware.
2702 */
Kiran K5ea7c812020-06-25 00:04:32 +05302703 if (!bacmp(&params->otp_bdaddr, BDADDR_ANY)) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002704 bt_dev_info(hdev, "No device address configured");
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002705 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
2706 }
2707
2708 /* With this Intel bootloader only the hardware variant and device
Jaya P Gaf3715e2017-10-30 11:01:22 +01002709 * revision information are used to select the right firmware for SfP
2710 * and WsP.
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002711 *
Tedd Ho-Jeong An230b04a2016-06-28 08:56:39 -07002712 * The firmware filename is ibt-<hw_variant>-<dev_revid>.sfi.
2713 *
2714 * Currently the supported hardware variants are:
2715 * 11 (0x0b) for iBT3.0 (LnP/SfP)
2716 * 12 (0x0c) for iBT3.5 (WsP)
Jaya P Gaf3715e2017-10-30 11:01:22 +01002717 *
2718 * For ThP/JfP and for future SKU's, the FW name varies based on HW
2719 * variant, HW revision and FW revision, as these are dependent on CNVi
2720 * and RF Combination.
2721 *
Tedd Ho-Jeong An86a61292017-05-01 13:35:12 -07002722 * 17 (0x11) for iBT3.5 (JfP)
2723 * 18 (0x12) for iBT3.5 (ThP)
Jaya P Gaf3715e2017-10-30 11:01:22 +01002724 *
2725 * The firmware file name for these will be
2726 * ibt-<hw_variant>-<hw_revision>-<fw_revision>.sfi.
2727 *
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002728 */
Kiran K5ea7c812020-06-25 00:04:32 +05302729 err = btusb_setup_intel_new_get_fw_name(ver, params, fwname,
Raghuram Hegde2da711bc2018-12-19 11:42:18 +05302730 sizeof(fwname), "sfi");
2731 if (!err) {
Marcel Holtmann85418fe2018-08-03 11:20:49 +02002732 bt_dev_err(hdev, "Unsupported Intel firmware naming");
Jaya P Gaf3715e2017-10-30 11:01:22 +01002733 return -EINVAL;
2734 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002735
2736 err = request_firmware(&fw, fwname, &hdev->dev);
2737 if (err < 0) {
Kiran K62acbbb2021-03-08 16:44:54 +05302738 bt_dev_err(hdev, "Failed to load Intel firmware file %s (%d)",
2739 fwname, err);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002740 return err;
2741 }
2742
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002743 bt_dev_info(hdev, "Found device firmware: %s", fwname);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002744
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002745 if (fw->size < 644) {
Marcel Holtmann85418fe2018-08-03 11:20:49 +02002746 bt_dev_err(hdev, "Invalid size of firmware file (%zu)",
2747 fw->size);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002748 err = -EBADF;
2749 goto done;
2750 }
2751
2752 set_bit(BTUSB_DOWNLOADING, &data->flags);
2753
Tedd Ho-Jeong Anfbbe83c2018-01-24 09:19:21 -08002754 /* Start firmware downloading and get boot parameter */
Kiran Kdc45d372020-08-26 15:24:32 +05302755 err = btintel_download_firmware(hdev, fw, boot_param);
Amit K Bagb9a25622019-10-17 13:52:29 +05302756 if (err < 0) {
2757 /* When FW download fails, send Intel Reset to retry
2758 * FW download.
2759 */
2760 btintel_reset_to_bootloader(hdev);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002761 goto done;
Amit K Bagb9a25622019-10-17 13:52:29 +05302762 }
Marcel Holtmannce6bb922015-01-28 01:58:40 -08002763 set_bit(BTUSB_FIRMWARE_LOADED, &data->flags);
2764
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002765 bt_dev_info(hdev, "Waiting for firmware download to complete");
Johan Hedberga087a982015-01-30 10:58:54 +02002766
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002767 /* Before switching the device into operational mode and with that
2768 * booting the loaded firmware, wait for the bootloader notification
2769 * that all fragments have been successfully received.
2770 *
Johan Hedberga087a982015-01-30 10:58:54 +02002771 * When the event processing receives the notification, then the
2772 * BTUSB_DOWNLOADING flag will be cleared.
2773 *
2774 * The firmware loading should not take longer than 5 seconds
2775 * and thus just timeout if that happens and fail the setup
2776 * of this device.
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002777 */
Johan Hedberg129a7692015-02-14 09:33:35 +02002778 err = wait_on_bit_timeout(&data->flags, BTUSB_DOWNLOADING,
2779 TASK_INTERRUPTIBLE,
2780 msecs_to_jiffies(5000));
Bart Van Asschef0a70a02016-08-11 16:02:44 -07002781 if (err == -EINTR) {
Marcel Holtmann85418fe2018-08-03 11:20:49 +02002782 bt_dev_err(hdev, "Firmware loading interrupted");
Johan Hedberga087a982015-01-30 10:58:54 +02002783 goto done;
2784 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002785
Johan Hedberga087a982015-01-30 10:58:54 +02002786 if (err) {
Marcel Holtmann85418fe2018-08-03 11:20:49 +02002787 bt_dev_err(hdev, "Firmware loading timeout");
Johan Hedberga087a982015-01-30 10:58:54 +02002788 err = -ETIMEDOUT;
Amit K Bagb9a25622019-10-17 13:52:29 +05302789 btintel_reset_to_bootloader(hdev);
Johan Hedberga087a982015-01-30 10:58:54 +02002790 goto done;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002791 }
2792
2793 if (test_bit(BTUSB_FIRMWARE_FAILED, &data->flags)) {
Marcel Holtmann85418fe2018-08-03 11:20:49 +02002794 bt_dev_err(hdev, "Firmware loading failed");
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002795 err = -ENOEXEC;
2796 goto done;
2797 }
2798
Kiran K5ea7c812020-06-25 00:04:32 +05302799done:
2800 release_firmware(fw);
2801 return err;
2802}
2803
2804static int btusb_setup_intel_new(struct hci_dev *hdev)
2805{
2806 struct btusb_data *data = hci_get_drvdata(hdev);
2807 struct intel_version ver;
2808 struct intel_boot_params params;
2809 u32 boot_param;
2810 char ddcname[64];
2811 ktime_t calltime, delta, rettime;
2812 unsigned long long duration;
2813 int err;
2814 struct intel_debug_features features;
2815
2816 BT_DBG("%s", hdev->name);
2817
2818 /* Set the default boot parameter to 0x0 and it is updated to
2819 * SKU specific boot parameter after reading Intel_Write_Boot_Params
2820 * command while downloading the firmware.
2821 */
2822 boot_param = 0x00000000;
2823
2824 calltime = ktime_get();
2825
2826 /* Read the Intel version information to determine if the device
2827 * is in bootloader mode or if it already has operational firmware
2828 * loaded.
2829 */
2830 err = btintel_read_version(hdev, &ver);
2831 if (err) {
2832 bt_dev_err(hdev, "Intel Read version failed (%d)", err);
2833 btintel_reset_to_bootloader(hdev);
2834 return err;
2835 }
2836
Kiran Kdc45d372020-08-26 15:24:32 +05302837 err = btusb_intel_download_firmware(hdev, &ver, &params, &boot_param);
Kiran K5ea7c812020-06-25 00:04:32 +05302838 if (err)
2839 return err;
2840
2841 /* controller is already having an operational firmware */
2842 if (ver.fw_variant == 0x23)
2843 goto finish;
2844
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002845 rettime = ktime_get();
2846 delta = ktime_sub(rettime, calltime);
2847 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
2848
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002849 bt_dev_info(hdev, "Firmware loaded in %llu usecs", duration);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002850
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002851 calltime = ktime_get();
2852
2853 set_bit(BTUSB_BOOTING, &data->flags);
2854
Tedd Ho-Jeong Ane5889af2018-01-24 09:19:18 -08002855 err = btintel_send_intel_reset(hdev, boot_param);
Amit K Bagb9a25622019-10-17 13:52:29 +05302856 if (err) {
2857 bt_dev_err(hdev, "Intel Soft Reset failed (%d)", err);
2858 btintel_reset_to_bootloader(hdev);
Tedd Ho-Jeong Ane5889af2018-01-24 09:19:18 -08002859 return err;
Amit K Bagb9a25622019-10-17 13:52:29 +05302860 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002861
2862 /* The bootloader will not indicate when the device is ready. This
2863 * is done by the operational firmware sending bootup notification.
Johan Hedbergfad70972015-01-30 10:58:55 +02002864 *
2865 * Booting into operational firmware should not take longer than
2866 * 1 second. However if that happens, then just fail the setup
2867 * since something went wrong.
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002868 */
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002869 bt_dev_info(hdev, "Waiting for device to boot");
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002870
Johan Hedberg129a7692015-02-14 09:33:35 +02002871 err = wait_on_bit_timeout(&data->flags, BTUSB_BOOTING,
2872 TASK_INTERRUPTIBLE,
2873 msecs_to_jiffies(1000));
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002874
Bart Van Asschef0a70a02016-08-11 16:02:44 -07002875 if (err == -EINTR) {
Marcel Holtmann85418fe2018-08-03 11:20:49 +02002876 bt_dev_err(hdev, "Device boot interrupted");
Johan Hedbergfad70972015-01-30 10:58:55 +02002877 return -EINTR;
2878 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002879
Johan Hedbergfad70972015-01-30 10:58:55 +02002880 if (err) {
Marcel Holtmann85418fe2018-08-03 11:20:49 +02002881 bt_dev_err(hdev, "Device boot timeout");
Amit K Bagb9a25622019-10-17 13:52:29 +05302882 btintel_reset_to_bootloader(hdev);
Johan Hedbergfad70972015-01-30 10:58:55 +02002883 return -ETIMEDOUT;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002884 }
2885
2886 rettime = ktime_get();
2887 delta = ktime_sub(rettime, calltime);
2888 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
2889
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002890 bt_dev_info(hdev, "Device booted in %llu usecs", duration);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002891
2892 clear_bit(BTUSB_BOOTLOADER, &data->flags);
2893
Kiran K5ea7c812020-06-25 00:04:32 +05302894 err = btusb_setup_intel_new_get_fw_name(&ver, &params, ddcname,
2895 sizeof(ddcname), "ddc");
2896
2897 if (!err) {
2898 bt_dev_err(hdev, "Unsupported Intel firmware naming");
2899 } else {
2900 /* Once the device is running in operational mode, it needs to
2901 * apply the device configuration (DDC) parameters.
2902 *
2903 * The device can work without DDC parameters, so even if it
2904 * fails to load the file, no need to fail the setup.
2905 */
2906 btintel_load_ddc_config(hdev, ddcname);
2907 }
Tedd Ho-Jeong An52cc9162015-06-12 16:20:05 -07002908
Chethan T Nd74abe22020-06-08 17:57:46 +05302909 /* Read the Intel supported features and if new exception formats
2910 * supported, need to load the additional DDC config to enable.
2911 */
2912 btintel_read_debug_features(hdev, &features);
2913
Chethan T Nc453b102020-06-08 17:57:47 +05302914 /* Set DDC mask for available debug features */
2915 btintel_set_debug_features(hdev, &features);
2916
Marcel Holtmann7fd673b2020-04-03 21:44:02 +02002917 /* Read the Intel version information after loading the FW */
2918 err = btintel_read_version(hdev, &ver);
2919 if (err)
2920 return err;
2921
2922 btintel_version_info(hdev, &ver);
2923
2924finish:
Miao-chen Choufc045902020-04-03 21:44:03 +02002925 /* All Intel controllers that support the Microsoft vendor
2926 * extension are using 0xFC1E for VsMsftOpCode.
2927 */
2928 switch (ver.hw_variant) {
Miao-chen Chou7a45bcb2020-12-17 14:53:19 -08002929 case 0x11: /* JfP */
Miao-chen Choufc045902020-04-03 21:44:03 +02002930 case 0x12: /* ThP */
Miao-chen Chou7a45bcb2020-12-17 14:53:19 -08002931 case 0x13: /* HrP */
2932 case 0x14: /* CcP */
Miao-chen Choufc045902020-04-03 21:44:03 +02002933 hci_set_msft_opcode(hdev, 0xFC1E);
2934 break;
2935 }
2936
Marcel Holtmann213445b2015-10-21 02:45:19 +02002937 /* Set the event mask for Intel specific vendor events. This enables
2938 * a few extra events that are useful during general operation. It
2939 * does not enable any debugging related events.
2940 *
2941 * The device will function correctly without these events enabled
2942 * and thus no need to fail the setup.
2943 */
2944 btintel_set_event_mask(hdev, false);
2945
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002946 return 0;
2947}
2948
Kiran K0a3c1d42020-11-19 17:09:40 +05302949static int btusb_setup_intel_newgen(struct hci_dev *hdev)
2950{
2951 struct btusb_data *data = hci_get_drvdata(hdev);
2952 u32 boot_param;
2953 char ddcname[64];
2954 ktime_t calltime, delta, rettime;
2955 unsigned long long duration;
2956 int err;
2957 struct intel_debug_features features;
2958 struct intel_version_tlv version;
2959
2960 bt_dev_dbg(hdev, "");
2961
2962 /* Set the default boot parameter to 0x0 and it is updated to
2963 * SKU specific boot parameter after reading Intel_Write_Boot_Params
2964 * command while downloading the firmware.
2965 */
2966 boot_param = 0x00000000;
2967
2968 calltime = ktime_get();
2969
2970 /* Read the Intel version information to determine if the device
2971 * is in bootloader mode or if it already has operational firmware
2972 * loaded.
2973 */
2974 err = btintel_read_version_tlv(hdev, &version);
2975 if (err) {
2976 bt_dev_err(hdev, "Intel Read version failed (%d)", err);
2977 btintel_reset_to_bootloader(hdev);
2978 return err;
2979 }
2980
2981 btintel_version_info_tlv(hdev, &version);
2982
Kiran K3f43a372020-11-19 17:09:42 +05302983 err = btusb_intel_download_firmware_newgen(hdev, &version, &boot_param);
2984 if (err)
2985 return err;
2986
Kiran K0a3c1d42020-11-19 17:09:40 +05302987 /* check if controller is already having an operational firmware */
2988 if (version.img_type == 0x03)
2989 goto finish;
2990
2991 rettime = ktime_get();
2992 delta = ktime_sub(rettime, calltime);
2993 duration = (unsigned long long)ktime_to_ns(delta) >> 10;
2994
2995 bt_dev_info(hdev, "Firmware loaded in %llu usecs", duration);
2996
2997 calltime = ktime_get();
2998
2999 set_bit(BTUSB_BOOTING, &data->flags);
3000
3001 err = btintel_send_intel_reset(hdev, boot_param);
3002 if (err) {
3003 bt_dev_err(hdev, "Intel Soft Reset failed (%d)", err);
3004 btintel_reset_to_bootloader(hdev);
3005 return err;
3006 }
3007
3008 /* The bootloader will not indicate when the device is ready. This
3009 * is done by the operational firmware sending bootup notification.
3010 *
3011 * Booting into operational firmware should not take longer than
3012 * 1 second. However if that happens, then just fail the setup
3013 * since something went wrong.
3014 */
3015 bt_dev_info(hdev, "Waiting for device to boot");
3016
3017 err = wait_on_bit_timeout(&data->flags, BTUSB_BOOTING,
3018 TASK_INTERRUPTIBLE,
3019 msecs_to_jiffies(1000));
3020
3021 if (err == -EINTR) {
3022 bt_dev_err(hdev, "Device boot interrupted");
3023 return -EINTR;
3024 }
3025
3026 if (err) {
3027 bt_dev_err(hdev, "Device boot timeout");
3028 btintel_reset_to_bootloader(hdev);
3029 return -ETIMEDOUT;
3030 }
3031
3032 rettime = ktime_get();
3033 delta = ktime_sub(rettime, calltime);
3034 duration = (unsigned long long)ktime_to_ns(delta) >> 10;
3035
3036 bt_dev_info(hdev, "Device booted in %llu usecs", duration);
3037
3038 clear_bit(BTUSB_BOOTLOADER, &data->flags);
3039
Kiran K9a93b8b2020-11-19 17:09:41 +05303040 btusb_setup_intel_newgen_get_fw_name(&version, ddcname, sizeof(ddcname),
3041 "ddc");
Kiran K0a3c1d42020-11-19 17:09:40 +05303042 /* Once the device is running in operational mode, it needs to
3043 * apply the device configuration (DDC) parameters.
3044 *
3045 * The device can work without DDC parameters, so even if it
3046 * fails to load the file, no need to fail the setup.
3047 */
3048 btintel_load_ddc_config(hdev, ddcname);
3049
3050 /* Read the Intel supported features and if new exception formats
3051 * supported, need to load the additional DDC config to enable.
3052 */
3053 btintel_read_debug_features(hdev, &features);
3054
3055 /* Set DDC mask for available debug features */
3056 btintel_set_debug_features(hdev, &features);
3057
3058 /* Read the Intel version information after loading the FW */
3059 err = btintel_read_version_tlv(hdev, &version);
3060 if (err)
3061 return err;
3062
3063 btintel_version_info_tlv(hdev, &version);
3064
3065finish:
3066 /* Set the event mask for Intel specific vendor events. This enables
3067 * a few extra events that are useful during general operation. It
3068 * does not enable any debugging related events.
3069 *
3070 * The device will function correctly without these events enabled
3071 * and thus no need to fail the setup.
3072 */
3073 btintel_set_event_mask(hdev, false);
3074
3075 return 0;
3076}
Tedd Ho-Jeong Anbfbd45e2015-02-13 09:20:52 -08003077static int btusb_shutdown_intel(struct hci_dev *hdev)
3078{
3079 struct sk_buff *skb;
3080 long ret;
3081
Amit K Bag1313bcc2018-08-03 12:43:20 +05303082 /* In the shutdown sequence where Bluetooth is turned off followed
3083 * by WiFi being turned off, turning WiFi back on causes issue with
3084 * the RF calibration.
3085 *
3086 * To ensure that any RF activity has been stopped, issue HCI Reset
3087 * command to clear all ongoing activity including advertising,
3088 * scanning etc.
3089 */
3090 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
3091 if (IS_ERR(skb)) {
3092 ret = PTR_ERR(skb);
3093 bt_dev_err(hdev, "HCI reset during shutdown failed");
3094 return ret;
3095 }
3096 kfree_skb(skb);
3097
Tedd Ho-Jeong Anbfbd45e2015-02-13 09:20:52 -08003098 /* Some platforms have an issue with BT LED when the interface is
3099 * down or BT radio is turned off, which takes 5 seconds to BT LED
3100 * goes off. This command turns off the BT LED immediately.
3101 */
3102 skb = __hci_cmd_sync(hdev, 0xfc3f, 0, NULL, HCI_INIT_TIMEOUT);
3103 if (IS_ERR(skb)) {
3104 ret = PTR_ERR(skb);
Marcel Holtmann85418fe2018-08-03 11:20:49 +02003105 bt_dev_err(hdev, "turning off Intel device LED failed");
Tedd Ho-Jeong Anbfbd45e2015-02-13 09:20:52 -08003106 return ret;
3107 }
3108 kfree_skb(skb);
3109
3110 return 0;
3111}
3112
Raghuram Hegde017a01c2019-01-29 17:54:48 +05303113static int btusb_shutdown_intel_new(struct hci_dev *hdev)
3114{
3115 struct sk_buff *skb;
3116
3117 /* Send HCI Reset to the controller to stop any BT activity which
3118 * were triggered. This will help to save power and maintain the
3119 * sync b/w Host and controller
3120 */
3121 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
3122 if (IS_ERR(skb)) {
3123 bt_dev_err(hdev, "HCI reset during shutdown failed");
3124 return PTR_ERR(skb);
3125 }
3126 kfree_skb(skb);
3127
3128 return 0;
3129}
3130
Sean Wang9ce67c32019-06-02 08:02:49 +08003131#define FIRMWARE_MT7663 "mediatek/mt7663pr2h.bin"
Sean Wanga1c49c432019-06-02 08:02:48 +08003132#define FIRMWARE_MT7668 "mediatek/mt7668pr2h.bin"
3133
3134#define HCI_WMT_MAX_EVENT_SIZE 64
Mark Chenfc342c42021-02-02 18:26:18 +08003135/* It is for mt79xx download rom patch*/
3136#define MTK_FW_ROM_PATCH_HEADER_SIZE 32
3137#define MTK_FW_ROM_PATCH_GD_SIZE 64
3138#define MTK_FW_ROM_PATCH_SEC_MAP_SIZE 64
3139#define MTK_SEC_MAP_COMMON_SIZE 12
3140#define MTK_SEC_MAP_NEED_SEND_SIZE 52
Sean Wanga1c49c432019-06-02 08:02:48 +08003141
3142enum {
3143 BTMTK_WMT_PATCH_DWNLD = 0x1,
3144 BTMTK_WMT_FUNC_CTRL = 0x6,
3145 BTMTK_WMT_RST = 0x7,
3146 BTMTK_WMT_SEMAPHORE = 0x17,
3147};
3148
3149enum {
3150 BTMTK_WMT_INVALID,
3151 BTMTK_WMT_PATCH_UNDONE,
Mark Chen48c13302021-02-02 18:26:17 +08003152 BTMTK_WMT_PATCH_PROGRESS,
Sean Wanga1c49c432019-06-02 08:02:48 +08003153 BTMTK_WMT_PATCH_DONE,
3154 BTMTK_WMT_ON_UNDONE,
3155 BTMTK_WMT_ON_DONE,
3156 BTMTK_WMT_ON_PROGRESS,
3157};
3158
3159struct btmtk_wmt_hdr {
3160 u8 dir;
3161 u8 op;
3162 __le16 dlen;
3163 u8 flag;
3164} __packed;
3165
3166struct btmtk_hci_wmt_cmd {
3167 struct btmtk_wmt_hdr hdr;
Arnd Bergmann10888142021-02-04 16:47:07 +01003168 u8 data[];
Sean Wanga1c49c432019-06-02 08:02:48 +08003169} __packed;
3170
3171struct btmtk_hci_wmt_evt {
3172 struct hci_event_hdr hhdr;
3173 struct btmtk_wmt_hdr whdr;
3174} __packed;
3175
3176struct btmtk_hci_wmt_evt_funcc {
3177 struct btmtk_hci_wmt_evt hwhdr;
3178 __be16 status;
3179} __packed;
3180
3181struct btmtk_tci_sleep {
3182 u8 mode;
3183 __le16 duration;
3184 __le16 host_duration;
3185 u8 host_wakeup_pin;
3186 u8 time_compensation;
3187} __packed;
3188
3189struct btmtk_hci_wmt_params {
3190 u8 op;
3191 u8 flag;
3192 u16 dlen;
3193 const void *data;
3194 u32 *status;
3195};
3196
Mark Chenfc342c42021-02-02 18:26:18 +08003197struct btmtk_patch_header {
3198 u8 datetime[16];
3199 u8 platform[4];
3200 __le16 hwver;
3201 __le16 swver;
3202 __le32 magicnum;
3203} __packed;
3204
3205struct btmtk_global_desc {
3206 __le32 patch_ver;
3207 __le32 sub_sys;
3208 __le32 feature_opt;
3209 __le32 section_num;
3210} __packed;
3211
3212struct btmtk_section_map {
3213 __le32 sectype;
3214 __le32 secoffset;
3215 __le32 secsize;
3216 union {
3217 __le32 u4SecSpec[13];
3218 struct {
3219 __le32 dlAddr;
3220 __le32 dlsize;
3221 __le32 seckeyidx;
3222 __le32 alignlen;
3223 __le32 sectype;
3224 __le32 dlmodecrctype;
3225 __le32 crc;
3226 __le32 reserved[6];
3227 } bin_info_spec;
3228 };
3229} __packed;
3230
Sean Wanga1c49c432019-06-02 08:02:48 +08003231static void btusb_mtk_wmt_recv(struct urb *urb)
3232{
3233 struct hci_dev *hdev = urb->context;
3234 struct btusb_data *data = hci_get_drvdata(hdev);
3235 struct hci_event_hdr *hdr;
3236 struct sk_buff *skb;
3237 int err;
3238
3239 if (urb->status == 0 && urb->actual_length > 0) {
3240 hdev->stat.byte_rx += urb->actual_length;
3241
3242 /* WMT event shouldn't be fragmented and the size should be
3243 * less than HCI_WMT_MAX_EVENT_SIZE.
3244 */
3245 skb = bt_skb_alloc(HCI_WMT_MAX_EVENT_SIZE, GFP_ATOMIC);
3246 if (!skb) {
3247 hdev->stat.err_rx++;
Jupeng Zhongde71a6c2021-02-02 09:39:13 +08003248 return;
Sean Wanga1c49c432019-06-02 08:02:48 +08003249 }
3250
3251 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
3252 skb_put_data(skb, urb->transfer_buffer, urb->actual_length);
3253
3254 hdr = (void *)skb->data;
3255 /* Fix up the vendor event id with 0xff for vendor specific
3256 * instead of 0xe4 so that event send via monitoring socket can
3257 * be parsed properly.
3258 */
3259 hdr->evt = 0xff;
3260
3261 /* When someone waits for the WMT event, the skb is being cloned
3262 * and being processed the events from there then.
3263 */
3264 if (test_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags)) {
Johan Hovold22cc6b72019-11-28 19:24:27 +01003265 data->evt_skb = skb_clone(skb, GFP_ATOMIC);
Jupeng Zhongde71a6c2021-02-02 09:39:13 +08003266 if (!data->evt_skb) {
3267 kfree_skb(skb);
3268 return;
3269 }
Sean Wanga1c49c432019-06-02 08:02:48 +08003270 }
3271
3272 err = hci_recv_frame(hdev, skb);
Jupeng Zhongde71a6c2021-02-02 09:39:13 +08003273 if (err < 0) {
3274 kfree_skb(data->evt_skb);
3275 data->evt_skb = NULL;
3276 return;
3277 }
Sean Wanga1c49c432019-06-02 08:02:48 +08003278
3279 if (test_and_clear_bit(BTUSB_TX_WAIT_VND_EVT,
3280 &data->flags)) {
3281 /* Barrier to sync with other CPUs */
3282 smp_mb__after_atomic();
3283 wake_up_bit(&data->flags,
3284 BTUSB_TX_WAIT_VND_EVT);
3285 }
Sean Wanga1c49c432019-06-02 08:02:48 +08003286 return;
3287 } else if (urb->status == -ENOENT) {
3288 /* Avoid suspend failed when usb_kill_urb */
3289 return;
3290 }
3291
3292 usb_mark_last_busy(data->udev);
3293
3294 /* The URB complete handler is still called with urb->actual_length = 0
3295 * when the event is not available, so we should keep re-submitting
3296 * URB until WMT event returns, Also, It's necessary to wait some time
3297 * between the two consecutive control URBs to relax the target device
3298 * to generate the event. Otherwise, the WMT event cannot return from
3299 * the device successfully.
3300 */
Mark Chen48c13302021-02-02 18:26:17 +08003301 udelay(500);
Sean Wanga1c49c432019-06-02 08:02:48 +08003302
3303 usb_anchor_urb(urb, &data->ctrl_anchor);
3304 err = usb_submit_urb(urb, GFP_ATOMIC);
3305 if (err < 0) {
3306 /* -EPERM: urb is being killed;
3307 * -ENODEV: device got disconnected
3308 */
3309 if (err != -EPERM && err != -ENODEV)
3310 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
3311 urb, -err);
3312 usb_unanchor_urb(urb);
3313 }
3314}
3315
3316static int btusb_mtk_submit_wmt_recv_urb(struct hci_dev *hdev)
3317{
3318 struct btusb_data *data = hci_get_drvdata(hdev);
3319 struct usb_ctrlrequest *dr;
3320 unsigned char *buf;
3321 int err, size = 64;
3322 unsigned int pipe;
3323 struct urb *urb;
3324
3325 urb = usb_alloc_urb(0, GFP_KERNEL);
3326 if (!urb)
3327 return -ENOMEM;
3328
3329 dr = kmalloc(sizeof(*dr), GFP_KERNEL);
3330 if (!dr) {
3331 usb_free_urb(urb);
3332 return -ENOMEM;
3333 }
3334
3335 dr->bRequestType = USB_TYPE_VENDOR | USB_DIR_IN;
3336 dr->bRequest = 1;
3337 dr->wIndex = cpu_to_le16(0);
3338 dr->wValue = cpu_to_le16(48);
3339 dr->wLength = cpu_to_le16(size);
3340
3341 buf = kmalloc(size, GFP_KERNEL);
3342 if (!buf) {
3343 kfree(dr);
Dinghao Liud33fe772020-08-23 15:44:21 +08003344 usb_free_urb(urb);
Sean Wanga1c49c432019-06-02 08:02:48 +08003345 return -ENOMEM;
3346 }
3347
3348 pipe = usb_rcvctrlpipe(data->udev, 0);
3349
3350 usb_fill_control_urb(urb, data->udev, pipe, (void *)dr,
3351 buf, size, btusb_mtk_wmt_recv, hdev);
3352
3353 urb->transfer_flags |= URB_FREE_BUFFER;
3354
3355 usb_anchor_urb(urb, &data->ctrl_anchor);
3356 err = usb_submit_urb(urb, GFP_KERNEL);
3357 if (err < 0) {
3358 if (err != -EPERM && err != -ENODEV)
3359 bt_dev_err(hdev, "urb %p submission failed (%d)",
3360 urb, -err);
3361 usb_unanchor_urb(urb);
3362 }
3363
3364 usb_free_urb(urb);
3365
3366 return err;
3367}
3368
3369static int btusb_mtk_hci_wmt_sync(struct hci_dev *hdev,
3370 struct btmtk_hci_wmt_params *wmt_params)
3371{
3372 struct btusb_data *data = hci_get_drvdata(hdev);
3373 struct btmtk_hci_wmt_evt_funcc *wmt_evt_funcc;
3374 u32 hlen, status = BTMTK_WMT_INVALID;
3375 struct btmtk_hci_wmt_evt *wmt_evt;
Arnd Bergmann10888142021-02-04 16:47:07 +01003376 struct btmtk_hci_wmt_cmd *wc;
Sean Wanga1c49c432019-06-02 08:02:48 +08003377 struct btmtk_wmt_hdr *hdr;
3378 int err;
3379
3380 /* Submit control IN URB on demand to process the WMT event */
3381 err = btusb_mtk_submit_wmt_recv_urb(hdev);
3382 if (err < 0)
3383 return err;
3384
3385 /* Send the WMT command and wait until the WMT event returns */
3386 hlen = sizeof(*hdr) + wmt_params->dlen;
3387 if (hlen > 255)
3388 return -EINVAL;
3389
Arnd Bergmann10888142021-02-04 16:47:07 +01003390 wc = kzalloc(hlen, GFP_KERNEL);
3391 if (!wc)
3392 return -ENOMEM;
3393
3394 hdr = &wc->hdr;
Sean Wanga1c49c432019-06-02 08:02:48 +08003395 hdr->dir = 1;
3396 hdr->op = wmt_params->op;
3397 hdr->dlen = cpu_to_le16(wmt_params->dlen + 1);
3398 hdr->flag = wmt_params->flag;
Arnd Bergmann10888142021-02-04 16:47:07 +01003399 memcpy(wc->data, wmt_params->data, wmt_params->dlen);
Sean Wanga1c49c432019-06-02 08:02:48 +08003400
3401 set_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
3402
Arnd Bergmann10888142021-02-04 16:47:07 +01003403 err = __hci_cmd_send(hdev, 0xfc6f, hlen, wc);
Sean Wanga1c49c432019-06-02 08:02:48 +08003404
3405 if (err < 0) {
3406 clear_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
Arnd Bergmann10888142021-02-04 16:47:07 +01003407 goto err_free_wc;
Sean Wanga1c49c432019-06-02 08:02:48 +08003408 }
3409
3410 /* The vendor specific WMT commands are all answered by a vendor
3411 * specific event and will have the Command Status or Command
3412 * Complete as with usual HCI command flow control.
3413 *
3414 * After sending the command, wait for BTUSB_TX_WAIT_VND_EVT
3415 * state to be cleared. The driver specific event receive routine
3416 * will clear that state and with that indicate completion of the
3417 * WMT command.
3418 */
3419 err = wait_on_bit_timeout(&data->flags, BTUSB_TX_WAIT_VND_EVT,
3420 TASK_INTERRUPTIBLE, HCI_INIT_TIMEOUT);
3421 if (err == -EINTR) {
3422 bt_dev_err(hdev, "Execution of wmt command interrupted");
3423 clear_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
Arnd Bergmann10888142021-02-04 16:47:07 +01003424 goto err_free_wc;
Sean Wanga1c49c432019-06-02 08:02:48 +08003425 }
3426
3427 if (err) {
3428 bt_dev_err(hdev, "Execution of wmt command timed out");
3429 clear_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
Arnd Bergmann10888142021-02-04 16:47:07 +01003430 err = -ETIMEDOUT;
3431 goto err_free_wc;
Sean Wanga1c49c432019-06-02 08:02:48 +08003432 }
3433
3434 /* Parse and handle the return WMT event */
3435 wmt_evt = (struct btmtk_hci_wmt_evt *)data->evt_skb->data;
3436 if (wmt_evt->whdr.op != hdr->op) {
3437 bt_dev_err(hdev, "Wrong op received %d expected %d",
3438 wmt_evt->whdr.op, hdr->op);
3439 err = -EIO;
3440 goto err_free_skb;
3441 }
3442
3443 switch (wmt_evt->whdr.op) {
3444 case BTMTK_WMT_SEMAPHORE:
3445 if (wmt_evt->whdr.flag == 2)
3446 status = BTMTK_WMT_PATCH_UNDONE;
3447 else
3448 status = BTMTK_WMT_PATCH_DONE;
3449 break;
3450 case BTMTK_WMT_FUNC_CTRL:
3451 wmt_evt_funcc = (struct btmtk_hci_wmt_evt_funcc *)wmt_evt;
3452 if (be16_to_cpu(wmt_evt_funcc->status) == 0x404)
3453 status = BTMTK_WMT_ON_DONE;
3454 else if (be16_to_cpu(wmt_evt_funcc->status) == 0x420)
3455 status = BTMTK_WMT_ON_PROGRESS;
3456 else
3457 status = BTMTK_WMT_ON_UNDONE;
3458 break;
Mark Chenfc342c42021-02-02 18:26:18 +08003459 case BTMTK_WMT_PATCH_DWNLD:
3460 if (wmt_evt->whdr.flag == 2)
3461 status = BTMTK_WMT_PATCH_DONE;
3462 else if (wmt_evt->whdr.flag == 1)
3463 status = BTMTK_WMT_PATCH_PROGRESS;
3464 else
3465 status = BTMTK_WMT_PATCH_UNDONE;
3466 break;
Sean Wanga1c49c432019-06-02 08:02:48 +08003467 }
3468
3469 if (wmt_params->status)
3470 *wmt_params->status = status;
3471
3472err_free_skb:
3473 kfree_skb(data->evt_skb);
3474 data->evt_skb = NULL;
Arnd Bergmann10888142021-02-04 16:47:07 +01003475err_free_wc:
3476 kfree(wc);
Sean Wanga1c49c432019-06-02 08:02:48 +08003477 return err;
3478}
3479
Mark Chenfc342c42021-02-02 18:26:18 +08003480static int btusb_mtk_setup_firmware_79xx(struct hci_dev *hdev, const char *fwname)
3481{
3482 struct btmtk_hci_wmt_params wmt_params;
Mark Chenfc342c42021-02-02 18:26:18 +08003483 struct btmtk_global_desc *globaldesc = NULL;
3484 struct btmtk_section_map *sectionmap;
3485 const struct firmware *fw;
3486 const u8 *fw_ptr;
3487 const u8 *fw_bin_ptr;
Mark Chenfc342c42021-02-02 18:26:18 +08003488 int err, dlen, i, status;
3489 u8 flag, first_block, retry;
3490 u32 section_num, dl_size, section_offset;
3491 u8 cmd[64];
3492
3493 err = request_firmware(&fw, fwname, &hdev->dev);
3494 if (err < 0) {
3495 bt_dev_err(hdev, "Failed to load firmware file (%d)", err);
3496 return err;
3497 }
3498
3499 fw_ptr = fw->data;
3500 fw_bin_ptr = fw_ptr;
Mark Chenfc342c42021-02-02 18:26:18 +08003501 globaldesc = (struct btmtk_global_desc *)(fw_ptr + MTK_FW_ROM_PATCH_HEADER_SIZE);
mark-yw.chen201cf392021-02-23 14:27:40 +08003502 section_num = le32_to_cpu(globaldesc->section_num);
Mark Chenfc342c42021-02-02 18:26:18 +08003503
3504 for (i = 0; i < section_num; i++) {
3505 first_block = 1;
3506 fw_ptr = fw_bin_ptr;
3507 sectionmap = (struct btmtk_section_map *)(fw_ptr + MTK_FW_ROM_PATCH_HEADER_SIZE +
3508 MTK_FW_ROM_PATCH_GD_SIZE + MTK_FW_ROM_PATCH_SEC_MAP_SIZE * i);
3509
mark-yw.chen201cf392021-02-23 14:27:40 +08003510 section_offset = le32_to_cpu(sectionmap->secoffset);
3511 dl_size = le32_to_cpu(sectionmap->bin_info_spec.dlsize);
Mark Chenfc342c42021-02-02 18:26:18 +08003512
3513 if (dl_size > 0) {
3514 retry = 20;
3515 while (retry > 0) {
3516 cmd[0] = 0; /* 0 means legacy dl mode. */
3517 memcpy(cmd + 1,
3518 fw_ptr + MTK_FW_ROM_PATCH_HEADER_SIZE +
3519 MTK_FW_ROM_PATCH_GD_SIZE + MTK_FW_ROM_PATCH_SEC_MAP_SIZE * i +
3520 MTK_SEC_MAP_COMMON_SIZE,
3521 MTK_SEC_MAP_NEED_SEND_SIZE + 1);
3522
3523 wmt_params.op = BTMTK_WMT_PATCH_DWNLD;
3524 wmt_params.status = &status;
3525 wmt_params.flag = 0;
3526 wmt_params.dlen = MTK_SEC_MAP_NEED_SEND_SIZE + 1;
3527 wmt_params.data = &cmd;
3528
3529 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3530 if (err < 0) {
3531 bt_dev_err(hdev, "Failed to send wmt patch dwnld (%d)",
3532 err);
3533 goto err_release_fw;
3534 }
3535
3536 if (status == BTMTK_WMT_PATCH_UNDONE) {
3537 break;
3538 } else if (status == BTMTK_WMT_PATCH_PROGRESS) {
3539 msleep(100);
3540 retry--;
3541 } else if (status == BTMTK_WMT_PATCH_DONE) {
3542 goto next_section;
3543 } else {
3544 bt_dev_err(hdev, "Failed wmt patch dwnld status (%d)",
3545 status);
3546 goto err_release_fw;
3547 }
3548 }
3549
3550 fw_ptr += section_offset;
3551 wmt_params.op = BTMTK_WMT_PATCH_DWNLD;
3552 wmt_params.status = NULL;
3553
3554 while (dl_size > 0) {
3555 dlen = min_t(int, 250, dl_size);
3556 if (first_block == 1) {
3557 flag = 1;
3558 first_block = 0;
3559 } else if (dl_size - dlen <= 0) {
3560 flag = 3;
3561 } else {
3562 flag = 2;
3563 }
3564
3565 wmt_params.flag = flag;
3566 wmt_params.dlen = dlen;
3567 wmt_params.data = fw_ptr;
3568
3569 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3570 if (err < 0) {
3571 bt_dev_err(hdev, "Failed to send wmt patch dwnld (%d)",
3572 err);
3573 goto err_release_fw;
3574 }
3575
3576 dl_size -= dlen;
3577 fw_ptr += dlen;
3578 }
3579 }
3580next_section:
3581 continue;
3582 }
3583 /* Wait a few moments for firmware activation done */
3584 usleep_range(100000, 120000);
3585
3586err_release_fw:
3587 release_firmware(fw);
3588
3589 return err;
3590}
3591
Sean Wanga1c49c432019-06-02 08:02:48 +08003592static int btusb_mtk_setup_firmware(struct hci_dev *hdev, const char *fwname)
3593{
3594 struct btmtk_hci_wmt_params wmt_params;
3595 const struct firmware *fw;
3596 const u8 *fw_ptr;
3597 size_t fw_size;
3598 int err, dlen;
Sean Wangf6451252020-06-19 19:52:01 +00003599 u8 flag, param;
Sean Wanga1c49c432019-06-02 08:02:48 +08003600
3601 err = request_firmware(&fw, fwname, &hdev->dev);
3602 if (err < 0) {
3603 bt_dev_err(hdev, "Failed to load firmware file (%d)", err);
3604 return err;
3605 }
3606
Sean Wangf6451252020-06-19 19:52:01 +00003607 /* Power on data RAM the firmware relies on. */
3608 param = 1;
3609 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
3610 wmt_params.flag = 3;
3611 wmt_params.dlen = sizeof(param);
3612 wmt_params.data = &param;
3613 wmt_params.status = NULL;
3614
3615 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3616 if (err < 0) {
3617 bt_dev_err(hdev, "Failed to power on data RAM (%d)", err);
Jing Xiangfengd1e9d232020-11-17 10:59:17 +08003618 goto err_release_fw;
Sean Wangf6451252020-06-19 19:52:01 +00003619 }
3620
Sean Wanga1c49c432019-06-02 08:02:48 +08003621 fw_ptr = fw->data;
3622 fw_size = fw->size;
3623
3624 /* The size of patch header is 30 bytes, should be skip */
Wei Yongjun5ee63102019-07-10 06:12:22 +00003625 if (fw_size < 30) {
3626 err = -EINVAL;
Sean Wanga1c49c432019-06-02 08:02:48 +08003627 goto err_release_fw;
Wei Yongjun5ee63102019-07-10 06:12:22 +00003628 }
Sean Wanga1c49c432019-06-02 08:02:48 +08003629
3630 fw_size -= 30;
3631 fw_ptr += 30;
3632 flag = 1;
3633
3634 wmt_params.op = BTMTK_WMT_PATCH_DWNLD;
3635 wmt_params.status = NULL;
3636
3637 while (fw_size > 0) {
3638 dlen = min_t(int, 250, fw_size);
3639
Jupeng Zhonga297f562021-02-03 22:28:46 +08003640 /* Tell device the position in sequence */
Sean Wanga1c49c432019-06-02 08:02:48 +08003641 if (fw_size - dlen <= 0)
3642 flag = 3;
3643 else if (fw_size < fw->size - 30)
3644 flag = 2;
3645
3646 wmt_params.flag = flag;
3647 wmt_params.dlen = dlen;
3648 wmt_params.data = fw_ptr;
3649
3650 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3651 if (err < 0) {
3652 bt_dev_err(hdev, "Failed to send wmt patch dwnld (%d)",
3653 err);
3654 goto err_release_fw;
3655 }
3656
3657 fw_size -= dlen;
3658 fw_ptr += dlen;
3659 }
3660
3661 wmt_params.op = BTMTK_WMT_RST;
3662 wmt_params.flag = 4;
3663 wmt_params.dlen = 0;
3664 wmt_params.data = NULL;
3665 wmt_params.status = NULL;
3666
3667 /* Activate funciton the firmware providing to */
3668 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3669 if (err < 0) {
3670 bt_dev_err(hdev, "Failed to send wmt rst (%d)", err);
Colin Ian King3168c192019-11-29 17:36:35 +00003671 goto err_release_fw;
Sean Wanga1c49c432019-06-02 08:02:48 +08003672 }
3673
3674 /* Wait a few moments for firmware activation done */
3675 usleep_range(10000, 12000);
3676
3677err_release_fw:
3678 release_firmware(fw);
3679
3680 return err;
3681}
3682
3683static int btusb_mtk_func_query(struct hci_dev *hdev)
3684{
3685 struct btmtk_hci_wmt_params wmt_params;
3686 int status, err;
3687 u8 param = 0;
3688
3689 /* Query whether the function is enabled */
3690 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
3691 wmt_params.flag = 4;
3692 wmt_params.dlen = sizeof(param);
3693 wmt_params.data = &param;
3694 wmt_params.status = &status;
3695
3696 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3697 if (err < 0) {
3698 bt_dev_err(hdev, "Failed to query function status (%d)", err);
3699 return err;
3700 }
3701
3702 return status;
3703}
3704
3705static int btusb_mtk_reg_read(struct btusb_data *data, u32 reg, u32 *val)
3706{
3707 int pipe, err, size = sizeof(u32);
3708 void *buf;
3709
3710 buf = kzalloc(size, GFP_KERNEL);
3711 if (!buf)
3712 return -ENOMEM;
3713
3714 pipe = usb_rcvctrlpipe(data->udev, 0);
3715 err = usb_control_msg(data->udev, pipe, 0x63,
3716 USB_TYPE_VENDOR | USB_DIR_IN,
3717 reg >> 16, reg & 0xffff,
3718 buf, size, USB_CTRL_SET_TIMEOUT);
3719 if (err < 0)
3720 goto err_free_buf;
3721
3722 *val = get_unaligned_le32(buf);
3723
3724err_free_buf:
3725 kfree(buf);
3726
3727 return err;
3728}
3729
Mark Chen48c13302021-02-02 18:26:17 +08003730static int btusb_mtk_id_get(struct btusb_data *data, u32 reg, u32 *id)
Sean Wanga1c49c432019-06-02 08:02:48 +08003731{
Mark Chen48c13302021-02-02 18:26:17 +08003732 return btusb_mtk_reg_read(data, reg, id);
Sean Wanga1c49c432019-06-02 08:02:48 +08003733}
3734
3735static int btusb_mtk_setup(struct hci_dev *hdev)
3736{
3737 struct btusb_data *data = hci_get_drvdata(hdev);
3738 struct btmtk_hci_wmt_params wmt_params;
3739 ktime_t calltime, delta, rettime;
3740 struct btmtk_tci_sleep tci_sleep;
3741 unsigned long long duration;
3742 struct sk_buff *skb;
3743 const char *fwname;
3744 int err, status;
3745 u32 dev_id;
Mark Chenfc342c42021-02-02 18:26:18 +08003746 char fw_bin_name[64];
mark-yw.chen201cf392021-02-23 14:27:40 +08003747 u32 fw_version = 0;
Sean Wanga1c49c432019-06-02 08:02:48 +08003748 u8 param;
3749
3750 calltime = ktime_get();
3751
Mark Chen48c13302021-02-02 18:26:17 +08003752 err = btusb_mtk_id_get(data, 0x80000008, &dev_id);
Sean Wanga1c49c432019-06-02 08:02:48 +08003753 if (err < 0) {
3754 bt_dev_err(hdev, "Failed to get device id (%d)", err);
3755 return err;
3756 }
3757
Mark Chenfc342c42021-02-02 18:26:18 +08003758 if (!dev_id) {
3759 err = btusb_mtk_id_get(data, 0x70010200, &dev_id);
3760 if (err < 0) {
3761 bt_dev_err(hdev, "Failed to get device id (%d)", err);
3762 return err;
3763 }
3764 err = btusb_mtk_id_get(data, 0x80021004, &fw_version);
3765 if (err < 0) {
3766 bt_dev_err(hdev, "Failed to get fw version (%d)", err);
3767 return err;
3768 }
3769 }
3770
Sean Wanga1c49c432019-06-02 08:02:48 +08003771 switch (dev_id) {
Sean Wang9ce67c32019-06-02 08:02:49 +08003772 case 0x7663:
3773 fwname = FIRMWARE_MT7663;
3774 break;
Sean Wanga1c49c432019-06-02 08:02:48 +08003775 case 0x7668:
3776 fwname = FIRMWARE_MT7668;
3777 break;
Mark Chenfc342c42021-02-02 18:26:18 +08003778 case 0x7961:
3779 snprintf(fw_bin_name, sizeof(fw_bin_name),
3780 "mediatek/BT_RAM_CODE_MT%04x_1_%x_hdr.bin",
3781 dev_id & 0xffff, (fw_version & 0xff) + 1);
3782 err = btusb_mtk_setup_firmware_79xx(hdev, fw_bin_name);
3783
3784 /* Enable Bluetooth protocol */
3785 param = 1;
3786 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
3787 wmt_params.flag = 0;
3788 wmt_params.dlen = sizeof(param);
3789 wmt_params.data = &param;
3790 wmt_params.status = NULL;
3791
3792 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3793 if (err < 0) {
3794 bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
3795 return err;
3796 }
3797 goto done;
Sean Wanga1c49c432019-06-02 08:02:48 +08003798 default:
Jupeng Zhonga297f562021-02-03 22:28:46 +08003799 bt_dev_err(hdev, "Unsupported hardware variant (%08x)",
Sean Wanga1c49c432019-06-02 08:02:48 +08003800 dev_id);
3801 return -ENODEV;
3802 }
3803
3804 /* Query whether the firmware is already download */
3805 wmt_params.op = BTMTK_WMT_SEMAPHORE;
3806 wmt_params.flag = 1;
3807 wmt_params.dlen = 0;
3808 wmt_params.data = NULL;
3809 wmt_params.status = &status;
3810
3811 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3812 if (err < 0) {
3813 bt_dev_err(hdev, "Failed to query firmware status (%d)", err);
3814 return err;
3815 }
3816
3817 if (status == BTMTK_WMT_PATCH_DONE) {
3818 bt_dev_info(hdev, "firmware already downloaded");
3819 goto ignore_setup_fw;
3820 }
3821
3822 /* Setup a firmware which the device definitely requires */
3823 err = btusb_mtk_setup_firmware(hdev, fwname);
3824 if (err < 0)
3825 return err;
3826
3827ignore_setup_fw:
3828 err = readx_poll_timeout(btusb_mtk_func_query, hdev, status,
3829 status < 0 || status != BTMTK_WMT_ON_PROGRESS,
3830 2000, 5000000);
3831 /* -ETIMEDOUT happens */
3832 if (err < 0)
3833 return err;
3834
3835 /* The other errors happen in btusb_mtk_func_query */
3836 if (status < 0)
3837 return status;
3838
3839 if (status == BTMTK_WMT_ON_DONE) {
3840 bt_dev_info(hdev, "function already on");
3841 goto ignore_func_on;
3842 }
3843
3844 /* Enable Bluetooth protocol */
3845 param = 1;
3846 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
3847 wmt_params.flag = 0;
3848 wmt_params.dlen = sizeof(param);
3849 wmt_params.data = &param;
3850 wmt_params.status = NULL;
3851
3852 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3853 if (err < 0) {
3854 bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
3855 return err;
3856 }
3857
3858ignore_func_on:
3859 /* Apply the low power environment setup */
3860 tci_sleep.mode = 0x5;
3861 tci_sleep.duration = cpu_to_le16(0x640);
3862 tci_sleep.host_duration = cpu_to_le16(0x640);
3863 tci_sleep.host_wakeup_pin = 0;
3864 tci_sleep.time_compensation = 0;
3865
3866 skb = __hci_cmd_sync(hdev, 0xfc7a, sizeof(tci_sleep), &tci_sleep,
3867 HCI_INIT_TIMEOUT);
3868 if (IS_ERR(skb)) {
3869 err = PTR_ERR(skb);
3870 bt_dev_err(hdev, "Failed to apply low power setting (%d)", err);
3871 return err;
3872 }
3873 kfree_skb(skb);
3874
Mark Chenfc342c42021-02-02 18:26:18 +08003875done:
Sean Wanga1c49c432019-06-02 08:02:48 +08003876 rettime = ktime_get();
3877 delta = ktime_sub(rettime, calltime);
3878 duration = (unsigned long long)ktime_to_ns(delta) >> 10;
3879
3880 bt_dev_info(hdev, "Device setup in %llu usecs", duration);
3881
3882 return 0;
3883}
3884
3885static int btusb_mtk_shutdown(struct hci_dev *hdev)
3886{
3887 struct btmtk_hci_wmt_params wmt_params;
3888 u8 param = 0;
3889 int err;
3890
3891 /* Disable the device */
3892 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
3893 wmt_params.flag = 0;
3894 wmt_params.dlen = sizeof(param);
3895 wmt_params.data = &param;
3896 wmt_params.status = NULL;
3897
3898 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3899 if (err < 0) {
3900 bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
3901 return err;
3902 }
3903
3904 return 0;
3905}
3906
Sean Wang9ce67c32019-06-02 08:02:49 +08003907MODULE_FIRMWARE(FIRMWARE_MT7663);
Sean Wanga1c49c432019-06-02 08:02:48 +08003908MODULE_FIRMWARE(FIRMWARE_MT7668);
Sean Wanga1c49c432019-06-02 08:02:48 +08003909
Rajat Jaina4ccc9e2017-02-01 14:24:10 -08003910#ifdef CONFIG_PM
3911/* Configure an out-of-band gpio as wake-up pin, if specified in device tree */
3912static int marvell_config_oob_wake(struct hci_dev *hdev)
3913{
3914 struct sk_buff *skb;
3915 struct btusb_data *data = hci_get_drvdata(hdev);
3916 struct device *dev = &data->udev->dev;
3917 u16 pin, gap, opcode;
3918 int ret;
3919 u8 cmd[5];
3920
3921 /* Move on if no wakeup pin specified */
3922 if (of_property_read_u16(dev->of_node, "marvell,wakeup-pin", &pin) ||
3923 of_property_read_u16(dev->of_node, "marvell,wakeup-gap-ms", &gap))
3924 return 0;
3925
3926 /* Vendor specific command to configure a GPIO as wake-up pin */
3927 opcode = hci_opcode_pack(0x3F, 0x59);
3928 cmd[0] = opcode & 0xFF;
3929 cmd[1] = opcode >> 8;
3930 cmd[2] = 2; /* length of parameters that follow */
3931 cmd[3] = pin;
3932 cmd[4] = gap; /* time in ms, for which wakeup pin should be asserted */
3933
3934 skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL);
3935 if (!skb) {
Joe Perchesd84fc2c2020-12-22 02:31:49 -08003936 bt_dev_err(hdev, "%s: No memory", __func__);
Rajat Jaina4ccc9e2017-02-01 14:24:10 -08003937 return -ENOMEM;
3938 }
3939
Johannes Berg59ae1d12017-06-16 14:29:20 +02003940 skb_put_data(skb, cmd, sizeof(cmd));
Rajat Jaina4ccc9e2017-02-01 14:24:10 -08003941 hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
3942
3943 ret = btusb_send_frame(hdev, skb);
3944 if (ret) {
Joe Perchesd84fc2c2020-12-22 02:31:49 -08003945 bt_dev_err(hdev, "%s: configuration failed", __func__);
Rajat Jaina4ccc9e2017-02-01 14:24:10 -08003946 kfree_skb(skb);
3947 return ret;
3948 }
3949
3950 return 0;
3951}
3952#endif
3953
Amitkumar Karwarae8df492014-07-18 14:47:06 -07003954static int btusb_set_bdaddr_marvell(struct hci_dev *hdev,
3955 const bdaddr_t *bdaddr)
3956{
3957 struct sk_buff *skb;
3958 u8 buf[8];
3959 long ret;
3960
3961 buf[0] = 0xfe;
3962 buf[1] = sizeof(bdaddr_t);
3963 memcpy(buf + 2, bdaddr, sizeof(bdaddr_t));
3964
3965 skb = __hci_cmd_sync(hdev, 0xfc22, sizeof(buf), buf, HCI_INIT_TIMEOUT);
3966 if (IS_ERR(skb)) {
3967 ret = PTR_ERR(skb);
Marcel Holtmann2064ee32017-10-30 10:42:59 +01003968 bt_dev_err(hdev, "changing Marvell device address failed (%ld)",
3969 ret);
Amitkumar Karwarae8df492014-07-18 14:47:06 -07003970 return ret;
3971 }
3972 kfree_skb(skb);
3973
3974 return 0;
3975}
3976
Toshi Kikuchi58592232014-12-12 10:58:05 -08003977static int btusb_set_bdaddr_ath3012(struct hci_dev *hdev,
3978 const bdaddr_t *bdaddr)
3979{
3980 struct sk_buff *skb;
3981 u8 buf[10];
3982 long ret;
3983
3984 buf[0] = 0x01;
3985 buf[1] = 0x01;
3986 buf[2] = 0x00;
3987 buf[3] = sizeof(bdaddr_t);
3988 memcpy(buf + 4, bdaddr, sizeof(bdaddr_t));
3989
3990 skb = __hci_cmd_sync(hdev, 0xfc0b, sizeof(buf), buf, HCI_INIT_TIMEOUT);
3991 if (IS_ERR(skb)) {
3992 ret = PTR_ERR(skb);
Marcel Holtmann2064ee32017-10-30 10:42:59 +01003993 bt_dev_err(hdev, "Change address command failed (%ld)", ret);
Toshi Kikuchi58592232014-12-12 10:58:05 -08003994 return ret;
3995 }
3996 kfree_skb(skb);
3997
3998 return 0;
3999}
4000
Rocky Liaob40f58b2020-09-29 12:23:51 +08004001static int btusb_set_bdaddr_wcn6855(struct hci_dev *hdev,
4002 const bdaddr_t *bdaddr)
4003{
4004 struct sk_buff *skb;
4005 u8 buf[6];
4006 long ret;
4007
4008 memcpy(buf, bdaddr, sizeof(bdaddr_t));
4009
4010 skb = __hci_cmd_sync_ev(hdev, 0xfc14, sizeof(buf), buf,
4011 HCI_EV_CMD_COMPLETE, HCI_INIT_TIMEOUT);
4012 if (IS_ERR(skb)) {
4013 ret = PTR_ERR(skb);
4014 bt_dev_err(hdev, "Change address command failed (%ld)", ret);
4015 return ret;
4016 }
4017 kfree_skb(skb);
4018
4019 return 0;
4020}
4021
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00004022#define QCA_DFU_PACKET_LEN 4096
4023
4024#define QCA_GET_TARGET_VERSION 0x09
4025#define QCA_CHECK_STATUS 0x05
4026#define QCA_DFU_DOWNLOAD 0x01
4027
4028#define QCA_SYSCFG_UPDATED 0x40
4029#define QCA_PATCH_UPDATED 0x80
4030#define QCA_DFU_TIMEOUT 3000
Tim Jiangef2862a2020-12-03 12:57:14 +08004031#define QCA_FLAG_MULTI_NVM 0x80
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00004032
4033struct qca_version {
4034 __le32 rom_version;
4035 __le32 patch_version;
4036 __le32 ram_version;
Tim Jiangef2862a2020-12-03 12:57:14 +08004037 __le16 board_id;
4038 __le16 flag;
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00004039 __u8 reserved[4];
4040} __packed;
4041
4042struct qca_rampatch_version {
Rocky Liaob40f58b2020-09-29 12:23:51 +08004043 __le16 rom_version_high;
4044 __le16 rom_version_low;
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00004045 __le16 patch_version;
4046} __packed;
4047
4048struct qca_device_info {
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00004049 u32 rom_version;
4050 u8 rampatch_hdr; /* length of header in rampatch */
4051 u8 nvm_hdr; /* length of header in NVM */
4052 u8 ver_offset; /* offset of version structure in rampatch */
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00004053};
4054
4055static const struct qca_device_info qca_devices_table[] = {
Rocky Liaob40f58b2020-09-29 12:23:51 +08004056 { 0x00000100, 20, 4, 8 }, /* Rome 1.0 */
4057 { 0x00000101, 20, 4, 8 }, /* Rome 1.1 */
4058 { 0x00000200, 28, 4, 16 }, /* Rome 2.0 */
4059 { 0x00000201, 28, 4, 16 }, /* Rome 2.1 */
4060 { 0x00000300, 28, 4, 16 }, /* Rome 3.0 */
4061 { 0x00000302, 28, 4, 16 }, /* Rome 3.2 */
4062 { 0x00130100, 40, 4, 16 }, /* WCN6855 1.0 */
4063 { 0x00130200, 40, 4, 16 }, /* WCN6855 2.0 */
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00004064};
4065
Takashi Iwai803cdb82018-05-21 22:34:52 +02004066static int btusb_qca_send_vendor_req(struct usb_device *udev, u8 request,
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00004067 void *data, u16 size)
4068{
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00004069 int pipe, err;
4070 u8 *buf;
4071
4072 buf = kmalloc(size, GFP_KERNEL);
4073 if (!buf)
4074 return -ENOMEM;
4075
4076 /* Found some of USB hosts have IOT issues with ours so that we should
4077 * not wait until HCI layer is ready.
4078 */
4079 pipe = usb_rcvctrlpipe(udev, 0);
4080 err = usb_control_msg(udev, pipe, request, USB_TYPE_VENDOR | USB_DIR_IN,
4081 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
4082 if (err < 0) {
Takashi Iwai803cdb82018-05-21 22:34:52 +02004083 dev_err(&udev->dev, "Failed to access otp area (%d)", err);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00004084 goto done;
4085 }
4086
4087 memcpy(data, buf, size);
4088
4089done:
4090 kfree(buf);
4091
4092 return err;
4093}
4094
4095static int btusb_setup_qca_download_fw(struct hci_dev *hdev,
4096 const struct firmware *firmware,
4097 size_t hdr_size)
4098{
4099 struct btusb_data *btdata = hci_get_drvdata(hdev);
4100 struct usb_device *udev = btdata->udev;
4101 size_t count, size, sent = 0;
4102 int pipe, len, err;
4103 u8 *buf;
4104
4105 buf = kmalloc(QCA_DFU_PACKET_LEN, GFP_KERNEL);
4106 if (!buf)
4107 return -ENOMEM;
4108
4109 count = firmware->size;
4110
4111 size = min_t(size_t, count, hdr_size);
4112 memcpy(buf, firmware->data, size);
4113
4114 /* USB patches should go down to controller through USB path
4115 * because binary format fits to go down through USB channel.
4116 * USB control path is for patching headers and USB bulk is for
4117 * patch body.
4118 */
4119 pipe = usb_sndctrlpipe(udev, 0);
4120 err = usb_control_msg(udev, pipe, QCA_DFU_DOWNLOAD, USB_TYPE_VENDOR,
4121 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
4122 if (err < 0) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01004123 bt_dev_err(hdev, "Failed to send headers (%d)", err);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00004124 goto done;
4125 }
4126
4127 sent += size;
4128 count -= size;
4129
4130 while (count) {
4131 size = min_t(size_t, count, QCA_DFU_PACKET_LEN);
4132
4133 memcpy(buf, firmware->data + sent, size);
4134
4135 pipe = usb_sndbulkpipe(udev, 0x02);
4136 err = usb_bulk_msg(udev, pipe, buf, size, &len,
4137 QCA_DFU_TIMEOUT);
4138 if (err < 0) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01004139 bt_dev_err(hdev, "Failed to send body at %zd of %zd (%d)",
4140 sent, firmware->size, err);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00004141 break;
4142 }
4143
4144 if (size != len) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01004145 bt_dev_err(hdev, "Failed to get bulk buffer");
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00004146 err = -EILSEQ;
4147 break;
4148 }
4149
4150 sent += size;
4151 count -= size;
4152 }
4153
4154done:
4155 kfree(buf);
4156 return err;
4157}
4158
4159static int btusb_setup_qca_load_rampatch(struct hci_dev *hdev,
4160 struct qca_version *ver,
4161 const struct qca_device_info *info)
4162{
4163 struct qca_rampatch_version *rver;
4164 const struct firmware *fw;
Rocky Liaob40f58b2020-09-29 12:23:51 +08004165 u32 ver_rom, ver_patch, rver_rom;
4166 u16 rver_rom_low, rver_rom_high, rver_patch;
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00004167 char fwname[64];
4168 int err;
4169
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00004170 ver_rom = le32_to_cpu(ver->rom_version);
4171 ver_patch = le32_to_cpu(ver->patch_version);
4172
4173 snprintf(fwname, sizeof(fwname), "qca/rampatch_usb_%08x.bin", ver_rom);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00004174
4175 err = request_firmware(&fw, fwname, &hdev->dev);
4176 if (err) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01004177 bt_dev_err(hdev, "failed to request rampatch file: %s (%d)",
4178 fwname, err);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00004179 return err;
4180 }
4181
Marcel Holtmann2064ee32017-10-30 10:42:59 +01004182 bt_dev_info(hdev, "using rampatch file: %s", fwname);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00004183
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00004184 rver = (struct qca_rampatch_version *)(fw->data + info->ver_offset);
Rocky Liaob40f58b2020-09-29 12:23:51 +08004185 rver_rom_low = le16_to_cpu(rver->rom_version_low);
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00004186 rver_patch = le16_to_cpu(rver->patch_version);
4187
Rocky Liaob40f58b2020-09-29 12:23:51 +08004188 if (ver_rom & ~0xffffU) {
4189 rver_rom_high = le16_to_cpu(rver->rom_version_high);
4190 rver_rom = le32_to_cpu(rver_rom_high << 16 | rver_rom_low);
4191 } else {
4192 rver_rom = rver_rom_low;
4193 }
4194
Marcel Holtmann2064ee32017-10-30 10:42:59 +01004195 bt_dev_info(hdev, "QCA: patch rome 0x%x build 0x%x, "
4196 "firmware rome 0x%x build 0x%x",
4197 rver_rom, rver_patch, ver_rom, ver_patch);
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00004198
4199 if (rver_rom != ver_rom || rver_patch <= ver_patch) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01004200 bt_dev_err(hdev, "rampatch file version did not match with firmware");
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00004201 err = -EINVAL;
4202 goto done;
4203 }
4204
4205 err = btusb_setup_qca_download_fw(hdev, fw, info->rampatch_hdr);
4206
4207done:
4208 release_firmware(fw);
4209
4210 return err;
4211}
4212
4213static int btusb_setup_qca_load_nvm(struct hci_dev *hdev,
4214 struct qca_version *ver,
4215 const struct qca_device_info *info)
4216{
4217 const struct firmware *fw;
4218 char fwname[64];
4219 int err;
4220
Tim Jiangef2862a2020-12-03 12:57:14 +08004221 if (((ver->flag >> 8) & 0xff) == QCA_FLAG_MULTI_NVM) {
4222 snprintf(fwname, sizeof(fwname), "qca/nvm_usb_%08x_%04x.bin",
4223 le32_to_cpu(ver->rom_version),
4224 le16_to_cpu(ver->board_id));
4225 } else {
4226 snprintf(fwname, sizeof(fwname), "qca/nvm_usb_%08x.bin",
4227 le32_to_cpu(ver->rom_version));
4228 }
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00004229
4230 err = request_firmware(&fw, fwname, &hdev->dev);
4231 if (err) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01004232 bt_dev_err(hdev, "failed to request NVM file: %s (%d)",
4233 fwname, err);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00004234 return err;
4235 }
4236
Marcel Holtmann2064ee32017-10-30 10:42:59 +01004237 bt_dev_info(hdev, "using NVM file: %s", fwname);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00004238
4239 err = btusb_setup_qca_download_fw(hdev, fw, info->nvm_hdr);
4240
4241 release_firmware(fw);
4242
4243 return err;
4244}
4245
Takashi Iwai803cdb82018-05-21 22:34:52 +02004246/* identify the ROM version and check whether patches are needed */
4247static bool btusb_qca_need_patch(struct usb_device *udev)
4248{
4249 struct qca_version ver;
4250
4251 if (btusb_qca_send_vendor_req(udev, QCA_GET_TARGET_VERSION, &ver,
4252 sizeof(ver)) < 0)
4253 return false;
4254 /* only low ROM versions need patches */
4255 return !(le32_to_cpu(ver.rom_version) & ~0xffffU);
4256}
4257
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00004258static int btusb_setup_qca(struct hci_dev *hdev)
4259{
Takashi Iwai803cdb82018-05-21 22:34:52 +02004260 struct btusb_data *btdata = hci_get_drvdata(hdev);
4261 struct usb_device *udev = btdata->udev;
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00004262 const struct qca_device_info *info = NULL;
4263 struct qca_version ver;
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00004264 u32 ver_rom;
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00004265 u8 status;
4266 int i, err;
4267
Takashi Iwai803cdb82018-05-21 22:34:52 +02004268 err = btusb_qca_send_vendor_req(udev, QCA_GET_TARGET_VERSION, &ver,
Marcel Holtmanneb500422015-04-16 23:15:50 +02004269 sizeof(ver));
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00004270 if (err < 0)
4271 return err;
4272
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00004273 ver_rom = le32_to_cpu(ver.rom_version);
Takashi Iwai803cdb82018-05-21 22:34:52 +02004274
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00004275 for (i = 0; i < ARRAY_SIZE(qca_devices_table); i++) {
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00004276 if (ver_rom == qca_devices_table[i].rom_version)
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00004277 info = &qca_devices_table[i];
4278 }
4279 if (!info) {
Hui Wang234f4142021-02-08 13:02:37 +08004280 /* If the rom_version is not matched in the qca_devices_table
4281 * and the high ROM version is not zero, we assume this chip no
4282 * need to load the rampatch and nvm.
4283 */
4284 if (ver_rom & ~0xffffU)
4285 return 0;
4286
Marcel Holtmann2064ee32017-10-30 10:42:59 +01004287 bt_dev_err(hdev, "don't support firmware rome 0x%x", ver_rom);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00004288 return -ENODEV;
4289 }
4290
Takashi Iwai803cdb82018-05-21 22:34:52 +02004291 err = btusb_qca_send_vendor_req(udev, QCA_CHECK_STATUS, &status,
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00004292 sizeof(status));
4293 if (err < 0)
4294 return err;
4295
4296 if (!(status & QCA_PATCH_UPDATED)) {
4297 err = btusb_setup_qca_load_rampatch(hdev, &ver, info);
4298 if (err < 0)
4299 return err;
4300 }
4301
Tim Jiangef2862a2020-12-03 12:57:14 +08004302 err = btusb_qca_send_vendor_req(udev, QCA_GET_TARGET_VERSION, &ver,
4303 sizeof(ver));
4304 if (err < 0)
4305 return err;
4306
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00004307 if (!(status & QCA_SYSCFG_UPDATED)) {
4308 err = btusb_setup_qca_load_nvm(hdev, &ver, info);
4309 if (err < 0)
4310 return err;
4311 }
4312
4313 return 0;
4314}
4315
Marcel Holtmann9d08f502015-10-08 20:23:08 +02004316static inline int __set_diag_interface(struct hci_dev *hdev)
4317{
4318 struct btusb_data *data = hci_get_drvdata(hdev);
4319 struct usb_interface *intf = data->diag;
4320 int i;
4321
4322 if (!data->diag)
4323 return -ENODEV;
4324
4325 data->diag_tx_ep = NULL;
4326 data->diag_rx_ep = NULL;
4327
4328 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
4329 struct usb_endpoint_descriptor *ep_desc;
4330
4331 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
4332
4333 if (!data->diag_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
4334 data->diag_tx_ep = ep_desc;
4335 continue;
4336 }
4337
4338 if (!data->diag_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
4339 data->diag_rx_ep = ep_desc;
4340 continue;
4341 }
4342 }
4343
4344 if (!data->diag_tx_ep || !data->diag_rx_ep) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01004345 bt_dev_err(hdev, "invalid diagnostic descriptors");
Marcel Holtmann9d08f502015-10-08 20:23:08 +02004346 return -ENODEV;
4347 }
4348
4349 return 0;
4350}
4351
4352static struct urb *alloc_diag_urb(struct hci_dev *hdev, bool enable)
4353{
4354 struct btusb_data *data = hci_get_drvdata(hdev);
4355 struct sk_buff *skb;
4356 struct urb *urb;
4357 unsigned int pipe;
4358
4359 if (!data->diag_tx_ep)
4360 return ERR_PTR(-ENODEV);
4361
4362 urb = usb_alloc_urb(0, GFP_KERNEL);
4363 if (!urb)
4364 return ERR_PTR(-ENOMEM);
4365
4366 skb = bt_skb_alloc(2, GFP_KERNEL);
4367 if (!skb) {
4368 usb_free_urb(urb);
4369 return ERR_PTR(-ENOMEM);
4370 }
4371
Johannes Berg634fef62017-06-16 14:29:24 +02004372 skb_put_u8(skb, 0xf0);
4373 skb_put_u8(skb, enable);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02004374
4375 pipe = usb_sndbulkpipe(data->udev, data->diag_tx_ep->bEndpointAddress);
4376
4377 usb_fill_bulk_urb(urb, data->udev, pipe,
4378 skb->data, skb->len, btusb_tx_complete, skb);
4379
4380 skb->dev = (void *)hdev;
4381
4382 return urb;
4383}
4384
4385static int btusb_bcm_set_diag(struct hci_dev *hdev, bool enable)
4386{
4387 struct btusb_data *data = hci_get_drvdata(hdev);
4388 struct urb *urb;
4389
4390 if (!data->diag)
4391 return -ENODEV;
4392
4393 if (!test_bit(HCI_RUNNING, &hdev->flags))
4394 return -ENETDOWN;
4395
4396 urb = alloc_diag_urb(hdev, enable);
4397 if (IS_ERR(urb))
4398 return PTR_ERR(urb);
4399
4400 return submit_or_queue_tx_urb(hdev, urb);
4401}
Marcel Holtmann9d08f502015-10-08 20:23:08 +02004402
Rajat Jainfd913ef2017-02-01 14:24:09 -08004403#ifdef CONFIG_PM
4404static irqreturn_t btusb_oob_wake_handler(int irq, void *priv)
4405{
4406 struct btusb_data *data = priv;
4407
4408 pm_wakeup_event(&data->udev->dev, 0);
Jeffy Chen017789f2017-02-24 14:24:29 +08004409 pm_system_wakeup();
Rajat Jainfd913ef2017-02-01 14:24:09 -08004410
4411 /* Disable only if not already disabled (keep it balanced) */
4412 if (test_and_clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags)) {
4413 disable_irq_nosync(irq);
4414 disable_irq_wake(irq);
4415 }
4416 return IRQ_HANDLED;
4417}
4418
4419static const struct of_device_id btusb_match_table[] = {
4420 { .compatible = "usb1286,204e" },
Brian Norris4c409af2019-02-22 14:53:43 -08004421 { .compatible = "usbcf3,e300" }, /* QCA6174A */
4422 { .compatible = "usb4ca,301a" }, /* QCA6174A (Lite-On) */
Rajat Jainfd913ef2017-02-01 14:24:09 -08004423 { }
4424};
4425MODULE_DEVICE_TABLE(of, btusb_match_table);
4426
4427/* Use an oob wakeup pin? */
4428static int btusb_config_oob_wake(struct hci_dev *hdev)
4429{
4430 struct btusb_data *data = hci_get_drvdata(hdev);
4431 struct device *dev = &data->udev->dev;
4432 int irq, ret;
4433
4434 clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags);
4435
4436 if (!of_match_device(btusb_match_table, dev))
4437 return 0;
4438
4439 /* Move on if no IRQ specified */
4440 irq = of_irq_get_byname(dev->of_node, "wakeup");
4441 if (irq <= 0) {
4442 bt_dev_dbg(hdev, "%s: no OOB Wakeup IRQ in DT", __func__);
4443 return 0;
4444 }
4445
Brian Norris771acc72019-04-09 11:49:17 -07004446 irq_set_status_flags(irq, IRQ_NOAUTOEN);
Rajat Jainfd913ef2017-02-01 14:24:09 -08004447 ret = devm_request_irq(&hdev->dev, irq, btusb_oob_wake_handler,
4448 0, "OOB Wake-on-BT", data);
4449 if (ret) {
4450 bt_dev_err(hdev, "%s: IRQ request failed", __func__);
4451 return ret;
4452 }
4453
4454 ret = device_init_wakeup(dev, true);
4455 if (ret) {
4456 bt_dev_err(hdev, "%s: failed to init_wakeup", __func__);
4457 return ret;
4458 }
4459
4460 data->oob_wake_irq = irq;
Rajat Jainfd913ef2017-02-01 14:24:09 -08004461 bt_dev_info(hdev, "OOB Wake-on-BT configured at IRQ %u", irq);
4462 return 0;
4463}
4464#endif
4465
Hans de Goedefc549102018-04-27 11:26:43 +02004466static void btusb_check_needs_reset_resume(struct usb_interface *intf)
4467{
4468 if (dmi_check_system(btusb_needs_reset_resume_table))
4469 interface_to_usbdev(intf)->quirks |= USB_QUIRK_RESET_RESUME;
4470}
4471
Abhishek Pandit-Subedib7d0bf12020-05-12 19:19:27 -07004472static bool btusb_prevent_wake(struct hci_dev *hdev)
4473{
4474 struct btusb_data *data = hci_get_drvdata(hdev);
4475
Abhishek Pandit-Subedi34a68652020-06-24 11:24:30 -07004476 if (test_bit(BTUSB_WAKEUP_DISABLE, &data->flags))
4477 return true;
4478
Abhishek Pandit-Subedib7d0bf12020-05-12 19:19:27 -07004479 return !device_may_wakeup(&data->udev->dev);
4480}
4481
Tim Jiang3b0d5252020-12-18 18:12:11 +08004482static int btusb_shutdown_qca(struct hci_dev *hdev)
4483{
4484 struct sk_buff *skb;
4485
4486 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
4487 if (IS_ERR(skb)) {
4488 bt_dev_err(hdev, "HCI reset during shutdown failed");
4489 return PTR_ERR(skb);
4490 }
4491 kfree_skb(skb);
4492
4493 return 0;
4494}
4495
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004496static int btusb_probe(struct usb_interface *intf,
Marcel Holtmann89e75332014-09-16 04:44:50 +02004497 const struct usb_device_id *id)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004498{
4499 struct usb_endpoint_descriptor *ep_desc;
Rajat Jaindc786b22019-01-24 15:28:14 -08004500 struct gpio_desc *reset_gpio;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004501 struct btusb_data *data;
4502 struct hci_dev *hdev;
Marcel Holtmann22f8e9d2015-10-20 00:53:33 +02004503 unsigned ifnum_base;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004504 int i, err;
4505
4506 BT_DBG("intf %p id %p", intf, id);
4507
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02004508 /* interface numbers are hardcoded in the spec */
Marcel Holtmann22f8e9d2015-10-20 00:53:33 +02004509 if (intf->cur_altsetting->desc.bInterfaceNumber != 0) {
4510 if (!(id->driver_info & BTUSB_IFNUM_2))
4511 return -ENODEV;
4512 if (intf->cur_altsetting->desc.bInterfaceNumber != 2)
4513 return -ENODEV;
4514 }
4515
4516 ifnum_base = intf->cur_altsetting->desc.bInterfaceNumber;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004517
4518 if (!id->driver_info) {
4519 const struct usb_device_id *match;
Marcel Holtmann89e75332014-09-16 04:44:50 +02004520
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004521 match = usb_match_id(intf, blacklist_table);
4522 if (match)
4523 id = match;
4524 }
4525
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02004526 if (id->driver_info == BTUSB_IGNORE)
4527 return -ENODEV;
4528
Steven.Li2d25f8b2011-07-01 14:02:36 +08004529 if (id->driver_info & BTUSB_ATH3012) {
4530 struct usb_device *udev = interface_to_usbdev(intf);
4531
4532 /* Old firmware would otherwise let ath3k driver load
Derek Robsond98422c2017-07-22 13:47:07 +12004533 * patch and sysconfig files
4534 */
Takashi Iwai803cdb82018-05-21 22:34:52 +02004535 if (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x0001 &&
4536 !btusb_qca_need_patch(udev))
Steven.Li2d25f8b2011-07-01 14:02:36 +08004537 return -ENODEV;
4538 }
4539
Sachin Kamat98921db2012-07-27 12:38:39 +05304540 data = devm_kzalloc(&intf->dev, sizeof(*data), GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004541 if (!data)
4542 return -ENOMEM;
4543
4544 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
4545 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
4546
4547 if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
4548 data->intr_ep = ep_desc;
4549 continue;
4550 }
4551
4552 if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
4553 data->bulk_tx_ep = ep_desc;
4554 continue;
4555 }
4556
4557 if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
4558 data->bulk_rx_ep = ep_desc;
4559 continue;
4560 }
4561 }
4562
Sachin Kamat98921db2012-07-27 12:38:39 +05304563 if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004564 return -ENODEV;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004565
Marcel Holtmann893ba542015-01-28 20:27:34 -08004566 if (id->driver_info & BTUSB_AMP) {
4567 data->cmdreq_type = USB_TYPE_CLASS | 0x01;
4568 data->cmdreq = 0x2b;
4569 } else {
4570 data->cmdreq_type = USB_TYPE_CLASS;
4571 data->cmdreq = 0x00;
4572 }
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01004573
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004574 data->udev = interface_to_usbdev(intf);
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02004575 data->intf = intf;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004576
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004577 INIT_WORK(&data->work, btusb_work);
Oliver Neukum7bee5492009-08-24 23:44:59 +02004578 INIT_WORK(&data->waker, btusb_waker);
Marcel Holtmann803b5832014-09-16 08:00:29 +02004579 init_usb_anchor(&data->deferred);
4580 init_usb_anchor(&data->tx_anchor);
Oliver Neukum7bee5492009-08-24 23:44:59 +02004581 spin_lock_init(&data->txlock);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004582
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004583 init_usb_anchor(&data->intr_anchor);
4584 init_usb_anchor(&data->bulk_anchor);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02004585 init_usb_anchor(&data->isoc_anchor);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02004586 init_usb_anchor(&data->diag_anchor);
Sean Wanga1c49c432019-06-02 08:02:48 +08004587 init_usb_anchor(&data->ctrl_anchor);
Marcel Holtmann803b5832014-09-16 08:00:29 +02004588 spin_lock_init(&data->rxlock);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004589
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08004590 if (id->driver_info & BTUSB_INTEL_NEW) {
4591 data->recv_event = btusb_recv_event_intel;
4592 data->recv_bulk = btusb_recv_bulk_intel;
4593 set_bit(BTUSB_BOOTLOADER, &data->flags);
4594 } else {
4595 data->recv_event = hci_recv_frame;
4596 data->recv_bulk = btusb_recv_bulk;
4597 }
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +01004598
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004599 hdev = hci_alloc_dev();
Sachin Kamat98921db2012-07-27 12:38:39 +05304600 if (!hdev)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004601 return -ENOMEM;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004602
Marcel Holtmannc13854c2010-02-08 15:27:07 +01004603 hdev->bus = HCI_USB;
David Herrmann155961e2012-02-09 21:58:32 +01004604 hci_set_drvdata(hdev, data);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004605
Marcel Holtmann893ba542015-01-28 20:27:34 -08004606 if (id->driver_info & BTUSB_AMP)
4607 hdev->dev_type = HCI_AMP;
4608 else
Marcel Holtmannca8bee52016-07-05 14:30:14 +02004609 hdev->dev_type = HCI_PRIMARY;
Marcel Holtmann893ba542015-01-28 20:27:34 -08004610
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004611 data->hdev = hdev;
4612
4613 SET_HCIDEV_DEV(hdev, &intf->dev);
4614
Rajat Jaindc786b22019-01-24 15:28:14 -08004615 reset_gpio = gpiod_get_optional(&data->udev->dev, "reset",
4616 GPIOD_OUT_LOW);
4617 if (IS_ERR(reset_gpio)) {
4618 err = PTR_ERR(reset_gpio);
4619 goto out_free_dev;
4620 } else if (reset_gpio) {
4621 data->reset_gpio = reset_gpio;
4622 }
4623
Marcel Holtmann9f8f9622013-04-10 08:11:35 -07004624 hdev->open = btusb_open;
4625 hdev->close = btusb_close;
4626 hdev->flush = btusb_flush;
4627 hdev->send = btusb_send_frame;
4628 hdev->notify = btusb_notify;
Abhishek Pandit-Subedib7d0bf12020-05-12 19:19:27 -07004629 hdev->prevent_wake = btusb_prevent_wake;
Marcel Holtmann9f8f9622013-04-10 08:11:35 -07004630
Rajat Jainfd913ef2017-02-01 14:24:09 -08004631#ifdef CONFIG_PM
4632 err = btusb_config_oob_wake(hdev);
4633 if (err)
4634 goto out_free_dev;
Rajat Jaina4ccc9e2017-02-01 14:24:10 -08004635
4636 /* Marvell devices may need a specific chip configuration */
4637 if (id->driver_info & BTUSB_MARVELL && data->oob_wake_irq) {
4638 err = marvell_config_oob_wake(hdev);
4639 if (err)
4640 goto out_free_dev;
4641 }
Rajat Jainfd913ef2017-02-01 14:24:09 -08004642#endif
Szymon Janc418678b2016-09-01 17:22:37 +02004643 if (id->driver_info & BTUSB_CW6622)
4644 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
4645
Marcel Holtmann6c9d4352015-10-17 14:39:27 +02004646 if (id->driver_info & BTUSB_BCM2045)
4647 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
4648
Marcel Holtmann9f8f9622013-04-10 08:11:35 -07004649 if (id->driver_info & BTUSB_BCM92035)
4650 hdev->setup = btusb_setup_bcm92035;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004651
Marcel Holtmannc0a21a52019-09-27 08:48:58 +02004652 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_BCM) &&
4653 (id->driver_info & BTUSB_BCM_PATCHRAM)) {
Marcel Holtmann49a5f782015-10-20 02:30:48 +02004654 hdev->manufacturer = 15;
Marcel Holtmannc2bfb102015-04-05 22:52:14 -07004655 hdev->setup = btbcm_setup_patchram;
Marcel Holtmann9d08f502015-10-08 20:23:08 +02004656 hdev->set_diag = btusb_bcm_set_diag;
Marcel Holtmann1df1f592015-04-05 22:52:11 -07004657 hdev->set_bdaddr = btbcm_set_bdaddr;
Marcel Holtmann9d08f502015-10-08 20:23:08 +02004658
4659 /* Broadcom LM_DIAG Interface numbers are hardcoded */
Marcel Holtmann22f8e9d2015-10-20 00:53:33 +02004660 data->diag = usb_ifnum_to_if(data->udev, ifnum_base + 2);
Marcel Holtmannabbaf502014-07-02 00:53:48 +02004661 }
Petri Gynther10d4c672014-05-08 15:50:01 -07004662
Marcel Holtmannc0a21a52019-09-27 08:48:58 +02004663 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_BCM) &&
4664 (id->driver_info & BTUSB_BCM_APPLE)) {
Marcel Holtmann49a5f782015-10-20 02:30:48 +02004665 hdev->manufacturer = 15;
Marcel Holtmannc2bfb102015-04-05 22:52:14 -07004666 hdev->setup = btbcm_setup_apple;
Marcel Holtmann9d08f502015-10-08 20:23:08 +02004667 hdev->set_diag = btusb_bcm_set_diag;
4668
4669 /* Broadcom LM_DIAG Interface numbers are hardcoded */
Marcel Holtmann22f8e9d2015-10-20 00:53:33 +02004670 data->diag = usb_ifnum_to_if(data->udev, ifnum_base + 2);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02004671 }
Marcel Holtmann17b27722015-03-22 15:52:38 +01004672
Marcel Holtmanncb8d6592014-07-02 11:25:25 +02004673 if (id->driver_info & BTUSB_INTEL) {
Marcel Holtmann49a5f782015-10-20 02:30:48 +02004674 hdev->manufacturer = 2;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07004675 hdev->setup = btusb_setup_intel;
Tedd Ho-Jeong Anbfbd45e2015-02-13 09:20:52 -08004676 hdev->shutdown = btusb_shutdown_intel;
Marcel Holtmann3e247672015-10-17 16:00:28 +02004677 hdev->set_diag = btintel_set_diag_mfg;
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07004678 hdev->set_bdaddr = btintel_set_bdaddr;
Rajat Jaindc786b22019-01-24 15:28:14 -08004679 hdev->cmd_timeout = btusb_intel_cmd_timeout;
Jakub Pawlowskic33fb9b2015-01-30 18:55:58 -08004680 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
Jakub Pawlowskic1154842015-03-17 09:04:16 -07004681 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
Marcel Holtmann3e247672015-10-17 16:00:28 +02004682 set_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks);
Marcel Holtmanncb8d6592014-07-02 11:25:25 +02004683 }
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07004684
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08004685 if (id->driver_info & BTUSB_INTEL_NEW) {
Marcel Holtmann49a5f782015-10-20 02:30:48 +02004686 hdev->manufacturer = 2;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08004687 hdev->send = btusb_send_frame_intel;
4688 hdev->setup = btusb_setup_intel_new;
Raghuram Hegde017a01c2019-01-29 17:54:48 +05304689 hdev->shutdown = btusb_shutdown_intel_new;
Marcel Holtmanneeb6abe2015-07-05 14:37:39 +02004690 hdev->hw_error = btintel_hw_error;
Marcel Holtmann6d2e50d2015-10-09 14:42:08 +02004691 hdev->set_diag = btintel_set_diag;
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07004692 hdev->set_bdaddr = btintel_set_bdaddr;
Rajat Jaindc786b22019-01-24 15:28:14 -08004693 hdev->cmd_timeout = btusb_intel_cmd_timeout;
Marcel Holtmannb970c5b2015-02-01 23:57:18 -08004694 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
Justin TerAvestf7785022018-09-25 11:04:57 -06004695 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
Marcel Holtmannd8270fb2015-10-17 16:00:27 +02004696 set_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08004697 }
4698
Kiran K0a3c1d42020-11-19 17:09:40 +05304699 if (id->driver_info & BTUSB_INTEL_NEWGEN) {
4700 hdev->manufacturer = 2;
4701 hdev->send = btusb_send_frame_intel;
4702 hdev->setup = btusb_setup_intel_newgen;
4703 hdev->shutdown = btusb_shutdown_intel_new;
4704 hdev->hw_error = btintel_hw_error;
4705 hdev->set_diag = btintel_set_diag;
4706 hdev->set_bdaddr = btintel_set_bdaddr;
4707 hdev->cmd_timeout = btusb_intel_cmd_timeout;
4708 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
4709 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
4710 set_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks);
4711
4712 data->recv_event = btusb_recv_event_intel;
4713 data->recv_bulk = btusb_recv_bulk_intel;
4714 set_bit(BTUSB_BOOTLOADER, &data->flags);
4715 }
4716
Amitkumar Karwarae8df492014-07-18 14:47:06 -07004717 if (id->driver_info & BTUSB_MARVELL)
4718 hdev->set_bdaddr = btusb_set_bdaddr_marvell;
4719
Marcel Holtmannc0a21a52019-09-27 08:48:58 +02004720 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_MTK) &&
4721 (id->driver_info & BTUSB_MEDIATEK)) {
Sean Wanga1c49c432019-06-02 08:02:48 +08004722 hdev->setup = btusb_mtk_setup;
4723 hdev->shutdown = btusb_mtk_shutdown;
4724 hdev->manufacturer = 70;
4725 set_bit(HCI_QUIRK_NON_PERSISTENT_SETUP, &hdev->quirks);
4726 }
Sean Wanga1c49c432019-06-02 08:02:48 +08004727
Marcel Holtmann661cf882015-01-02 23:35:20 -08004728 if (id->driver_info & BTUSB_SWAVE) {
4729 set_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks);
Marcel Holtmannd57dbe72014-12-26 04:42:33 +01004730 set_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS, &hdev->quirks);
Marcel Holtmann661cf882015-01-02 23:35:20 -08004731 }
Marcel Holtmannd57dbe72014-12-26 04:42:33 +01004732
Marcel Holtmanne4c534b2015-10-21 01:31:45 +02004733 if (id->driver_info & BTUSB_INTEL_BOOT) {
4734 hdev->manufacturer = 2;
Marcel Holtmann40df7832014-07-06 13:29:58 +02004735 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
Marcel Holtmanne4c534b2015-10-21 01:31:45 +02004736 }
Marcel Holtmann40df7832014-07-06 13:29:58 +02004737
Jakub Pawlowski79f0c872015-01-29 10:38:34 -08004738 if (id->driver_info & BTUSB_ATH3012) {
Takashi Iwai803cdb82018-05-21 22:34:52 +02004739 data->setup_on_usb = btusb_setup_qca;
Toshi Kikuchi58592232014-12-12 10:58:05 -08004740 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
Jakub Pawlowski3d50d512015-03-17 09:04:15 -07004741 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
Jakub Pawlowski79f0c872015-01-29 10:38:34 -08004742 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
4743 }
Toshi Kikuchi58592232014-12-12 10:58:05 -08004744
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00004745 if (id->driver_info & BTUSB_QCA_ROME) {
4746 data->setup_on_usb = btusb_setup_qca;
4747 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
Abhishek Pandit-Subedi69335682020-06-24 11:11:44 -07004748 hdev->cmd_timeout = btusb_qca_cmd_timeout;
Vic Wei96e58d32018-03-28 08:28:47 -07004749 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
Hans de Goedefc549102018-04-27 11:26:43 +02004750 btusb_check_needs_reset_resume(intf);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00004751 }
4752
Rocky Liaob40f58b2020-09-29 12:23:51 +08004753 if (id->driver_info & BTUSB_QCA_WCN6855) {
4754 data->setup_on_usb = btusb_setup_qca;
Tim Jiang3b0d5252020-12-18 18:12:11 +08004755 hdev->shutdown = btusb_shutdown_qca;
Rocky Liaob40f58b2020-09-29 12:23:51 +08004756 hdev->set_bdaddr = btusb_set_bdaddr_wcn6855;
4757 hdev->cmd_timeout = btusb_qca_cmd_timeout;
4758 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
4759 }
4760
Hilda Wu461f95f2020-06-30 21:09:40 +08004761 if (id->driver_info & BTUSB_AMP) {
4762 /* AMP controllers do not support SCO packets */
4763 data->isoc = NULL;
4764 } else {
4765 /* Interface orders are hardcoded in the specification */
4766 data->isoc = usb_ifnum_to_if(data->udev, ifnum_base + 1);
4767 data->isoc_ifnum = ifnum_base + 1;
4768 }
4769
Arnd Bergmann42d22092019-09-18 21:59:02 +02004770 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_RTL) &&
4771 (id->driver_info & BTUSB_REALTEK)) {
Carlo Caionedb33c772015-05-14 10:49:09 +02004772 hdev->setup = btrtl_setup_realtek;
Jian-Hong Pan7af3f5582019-06-25 16:30:51 +08004773 hdev->shutdown = btrtl_shutdown_realtek;
Alex Lud7ef0d12019-09-05 10:36:31 +08004774 hdev->cmd_timeout = btusb_rtl_cmd_timeout;
Daniel Drake04b8c812015-05-21 08:23:50 -06004775
Alex Lu9e455242019-08-14 20:02:52 +08004776 /* Realtek devices lose their updated firmware over global
4777 * suspend that means host doesn't send SET_FEATURE
4778 * (DEVICE_REMOTE_WAKEUP)
Daniel Drake04b8c812015-05-21 08:23:50 -06004779 */
Alex Lu9e455242019-08-14 20:02:52 +08004780 set_bit(BTUSB_WAKEUP_DISABLE, &data->flags);
Daniel Drake04b8c812015-05-21 08:23:50 -06004781 }
Daniel Drakea2698a92015-04-16 14:09:55 -06004782
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01004783 if (!reset)
Szymon Janca6c511c2012-05-23 12:35:46 +02004784 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02004785
4786 if (force_scofix || id->driver_info & BTUSB_WRONG_SCO_MTU) {
4787 if (!disable_scofix)
4788 set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
4789 }
4790
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02004791 if (id->driver_info & BTUSB_BROKEN_ISOC)
4792 data->isoc = NULL;
4793
Alain Michaud4b127bd2020-02-27 18:29:39 +00004794 if (id->driver_info & BTUSB_WIDEBAND_SPEECH)
Alain Michaud00bce3f2020-03-05 16:14:59 +00004795 set_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED, &hdev->quirks);
Alain Michaud4b127bd2020-02-27 18:29:39 +00004796
Alain Michaudaff8c482020-04-23 14:43:31 +00004797 if (id->driver_info & BTUSB_VALID_LE_STATES)
4798 set_bit(HCI_QUIRK_VALID_LE_STATES, &hdev->quirks);
4799
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01004800 if (id->driver_info & BTUSB_DIGIANSWER) {
4801 data->cmdreq_type = USB_TYPE_VENDOR;
Szymon Janca6c511c2012-05-23 12:35:46 +02004802 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01004803 }
4804
4805 if (id->driver_info & BTUSB_CSR) {
4806 struct usb_device *udev = data->udev;
Marcel Holtmann81cac642014-01-03 03:02:36 -08004807 u16 bcdDevice = le16_to_cpu(udev->descriptor.bcdDevice);
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01004808
4809 /* Old firmware would otherwise execute USB reset */
Marcel Holtmann81cac642014-01-03 03:02:36 -08004810 if (bcdDevice < 0x117)
Szymon Janca6c511c2012-05-23 12:35:46 +02004811 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmann81cac642014-01-03 03:02:36 -08004812
Ismael Ferreras Morezuelascde1a8a2020-07-26 23:12:28 +02004813 /* This must be set first in case we disable it for fakes */
Jakub Pawlowski49c989a2015-03-17 09:04:17 -07004814 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
Ismael Ferreras Morezuelascde1a8a2020-07-26 23:12:28 +02004815
4816 /* Fake CSR devices with broken commands */
4817 if (le16_to_cpu(udev->descriptor.idVendor) == 0x0a12 &&
4818 le16_to_cpu(udev->descriptor.idProduct) == 0x0001)
4819 hdev->setup = btusb_setup_csr;
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01004820 }
4821
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02004822 if (id->driver_info & BTUSB_SNIFFER) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02004823 struct usb_device *udev = data->udev;
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02004824
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01004825 /* New sniffer firmware has crippled HCI interface */
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02004826 if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
4827 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
4828 }
4829
Marcel Holtmann3a5ef202014-07-06 14:53:54 +02004830 if (id->driver_info & BTUSB_INTEL_BOOT) {
4831 /* A bug in the bootloader causes that interrupt interface is
4832 * only enabled after receiving SetInterface(0, AltSetting=0).
4833 */
4834 err = usb_set_interface(data->udev, 0, 0);
4835 if (err < 0) {
4836 BT_ERR("failed to set interface 0, alt 0 %d", err);
Rajat Jain10ab1332017-02-01 14:24:08 -08004837 goto out_free_dev;
Marcel Holtmann3a5ef202014-07-06 14:53:54 +02004838 }
4839 }
4840
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02004841 if (data->isoc) {
4842 err = usb_driver_claim_interface(&btusb_driver,
Marcel Holtmann89e75332014-09-16 04:44:50 +02004843 data->isoc, data);
Rajat Jain10ab1332017-02-01 14:24:08 -08004844 if (err < 0)
4845 goto out_free_dev;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02004846 }
4847
Marcel Holtmannc0a21a52019-09-27 08:48:58 +02004848 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_BCM) && data->diag) {
Marcel Holtmann9d08f502015-10-08 20:23:08 +02004849 if (!usb_driver_claim_interface(&btusb_driver,
4850 data->diag, data))
4851 __set_diag_interface(hdev);
4852 else
4853 data->diag = NULL;
4854 }
Marcel Holtmann9d08f502015-10-08 20:23:08 +02004855
Hui Wang7bd9fb02021-02-03 16:02:45 +08004856 if (!enable_autosuspend)
4857 usb_disable_autosuspend(data->udev);
Hans de Goedeeff2d682017-11-13 14:44:16 +01004858
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004859 err = hci_register_dev(hdev);
Rajat Jain10ab1332017-02-01 14:24:08 -08004860 if (err < 0)
4861 goto out_free_dev;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004862
4863 usb_set_intfdata(intf, data);
4864
4865 return 0;
Rajat Jain10ab1332017-02-01 14:24:08 -08004866
4867out_free_dev:
Rajat Jaindc786b22019-01-24 15:28:14 -08004868 if (data->reset_gpio)
4869 gpiod_put(data->reset_gpio);
Rajat Jain10ab1332017-02-01 14:24:08 -08004870 hci_free_dev(hdev);
4871 return err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004872}
4873
4874static void btusb_disconnect(struct usb_interface *intf)
4875{
4876 struct btusb_data *data = usb_get_intfdata(intf);
4877 struct hci_dev *hdev;
4878
4879 BT_DBG("intf %p", intf);
4880
4881 if (!data)
4882 return;
4883
4884 hdev = data->hdev;
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02004885 usb_set_intfdata(data->intf, NULL);
4886
4887 if (data->isoc)
4888 usb_set_intfdata(data->isoc, NULL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004889
Marcel Holtmann9d08f502015-10-08 20:23:08 +02004890 if (data->diag)
4891 usb_set_intfdata(data->diag, NULL);
4892
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004893 hci_unregister_dev(hdev);
4894
Marcel Holtmann9d08f502015-10-08 20:23:08 +02004895 if (intf == data->intf) {
4896 if (data->isoc)
4897 usb_driver_release_interface(&btusb_driver, data->isoc);
4898 if (data->diag)
4899 usb_driver_release_interface(&btusb_driver, data->diag);
4900 } else if (intf == data->isoc) {
4901 if (data->diag)
4902 usb_driver_release_interface(&btusb_driver, data->diag);
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02004903 usb_driver_release_interface(&btusb_driver, data->intf);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02004904 } else if (intf == data->diag) {
4905 usb_driver_release_interface(&btusb_driver, data->intf);
4906 if (data->isoc)
4907 usb_driver_release_interface(&btusb_driver, data->isoc);
4908 }
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02004909
Rajat Jainfd913ef2017-02-01 14:24:09 -08004910 if (data->oob_wake_irq)
4911 device_init_wakeup(&data->udev->dev, false);
4912
Rajat Jaindc786b22019-01-24 15:28:14 -08004913 if (data->reset_gpio)
4914 gpiod_put(data->reset_gpio);
4915
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004916 hci_free_dev(hdev);
Hui Wang7bd9fb02021-02-03 16:02:45 +08004917
4918 if (!enable_autosuspend)
4919 usb_enable_autosuspend(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004920}
4921
Oliver Neukum7bee5492009-08-24 23:44:59 +02004922#ifdef CONFIG_PM
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01004923static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
4924{
4925 struct btusb_data *data = usb_get_intfdata(intf);
4926
4927 BT_DBG("intf %p", intf);
4928
4929 if (data->suspend_count++)
4930 return 0;
4931
Oliver Neukum7bee5492009-08-24 23:44:59 +02004932 spin_lock_irq(&data->txlock);
Alan Stern5b1b0b82011-08-19 23:49:48 +02004933 if (!(PMSG_IS_AUTO(message) && data->tx_in_flight)) {
Oliver Neukum7bee5492009-08-24 23:44:59 +02004934 set_bit(BTUSB_SUSPENDING, &data->flags);
4935 spin_unlock_irq(&data->txlock);
4936 } else {
4937 spin_unlock_irq(&data->txlock);
4938 data->suspend_count--;
4939 return -EBUSY;
4940 }
4941
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01004942 cancel_work_sync(&data->work);
4943
Oliver Neukum7bee5492009-08-24 23:44:59 +02004944 btusb_stop_traffic(data);
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01004945 usb_kill_anchored_urbs(&data->tx_anchor);
4946
Rajat Jainfd913ef2017-02-01 14:24:09 -08004947 if (data->oob_wake_irq && device_may_wakeup(&data->udev->dev)) {
4948 set_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags);
4949 enable_irq_wake(data->oob_wake_irq);
4950 enable_irq(data->oob_wake_irq);
4951 }
4952
Alex Lu9e455242019-08-14 20:02:52 +08004953 /* For global suspend, Realtek devices lose the loaded fw
4954 * in them. But for autosuspend, firmware should remain.
4955 * Actually, it depends on whether the usb host sends
4956 * set feature (enable wakeup) or not.
4957 */
4958 if (test_bit(BTUSB_WAKEUP_DISABLE, &data->flags)) {
4959 if (PMSG_IS_AUTO(message) &&
4960 device_can_wakeup(&data->udev->dev))
4961 data->udev->do_remote_wakeup = 1;
4962 else if (!PMSG_IS_AUTO(message))
4963 data->udev->reset_resume = 1;
4964 }
4965
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01004966 return 0;
4967}
4968
Oliver Neukum7bee5492009-08-24 23:44:59 +02004969static void play_deferred(struct btusb_data *data)
4970{
4971 struct urb *urb;
4972 int err;
4973
4974 while ((urb = usb_get_from_anchor(&data->deferred))) {
Jeffy Chen19cfe912017-07-20 18:53:50 +08004975 usb_anchor_urb(urb, &data->tx_anchor);
4976
Oliver Neukum7bee5492009-08-24 23:44:59 +02004977 err = usb_submit_urb(urb, GFP_ATOMIC);
Jeffy Chen19cfe912017-07-20 18:53:50 +08004978 if (err < 0) {
4979 if (err != -EPERM && err != -ENODEV)
4980 BT_ERR("%s urb %p submission failed (%d)",
4981 data->hdev->name, urb, -err);
4982 kfree(urb->setup_packet);
4983 usb_unanchor_urb(urb);
4984 usb_free_urb(urb);
Oliver Neukum7bee5492009-08-24 23:44:59 +02004985 break;
Jeffy Chen19cfe912017-07-20 18:53:50 +08004986 }
Oliver Neukum7bee5492009-08-24 23:44:59 +02004987
4988 data->tx_in_flight++;
Jeffy Chen19cfe912017-07-20 18:53:50 +08004989 usb_free_urb(urb);
Oliver Neukum7bee5492009-08-24 23:44:59 +02004990 }
Jeffy Chen19cfe912017-07-20 18:53:50 +08004991
4992 /* Cleanup the rest deferred urbs. */
4993 while ((urb = usb_get_from_anchor(&data->deferred))) {
4994 kfree(urb->setup_packet);
4995 usb_free_urb(urb);
4996 }
Oliver Neukum7bee5492009-08-24 23:44:59 +02004997}
4998
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01004999static int btusb_resume(struct usb_interface *intf)
5000{
5001 struct btusb_data *data = usb_get_intfdata(intf);
5002 struct hci_dev *hdev = data->hdev;
Oliver Neukum7bee5492009-08-24 23:44:59 +02005003 int err = 0;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01005004
5005 BT_DBG("intf %p", intf);
5006
5007 if (--data->suspend_count)
5008 return 0;
5009
Rajat Jainfd913ef2017-02-01 14:24:09 -08005010 /* Disable only if not already disabled (keep it balanced) */
5011 if (test_and_clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags)) {
5012 disable_irq(data->oob_wake_irq);
5013 disable_irq_wake(data->oob_wake_irq);
5014 }
5015
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01005016 if (!test_bit(HCI_RUNNING, &hdev->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +02005017 goto done;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01005018
5019 if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
5020 err = btusb_submit_intr_urb(hdev, GFP_NOIO);
5021 if (err < 0) {
5022 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02005023 goto failed;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01005024 }
5025 }
5026
5027 if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) {
Marcel Holtmann43c2e572009-02-04 17:41:38 +01005028 err = btusb_submit_bulk_urb(hdev, GFP_NOIO);
5029 if (err < 0) {
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01005030 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02005031 goto failed;
5032 }
5033
5034 btusb_submit_bulk_urb(hdev, GFP_NOIO);
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01005035 }
5036
5037 if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
5038 if (btusb_submit_isoc_urb(hdev, GFP_NOIO) < 0)
5039 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
5040 else
5041 btusb_submit_isoc_urb(hdev, GFP_NOIO);
5042 }
5043
Oliver Neukum7bee5492009-08-24 23:44:59 +02005044 spin_lock_irq(&data->txlock);
5045 play_deferred(data);
5046 clear_bit(BTUSB_SUSPENDING, &data->flags);
5047 spin_unlock_irq(&data->txlock);
5048 schedule_work(&data->work);
5049
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01005050 return 0;
Oliver Neukum7bee5492009-08-24 23:44:59 +02005051
5052failed:
5053 usb_scuttle_anchored_urbs(&data->deferred);
5054done:
5055 spin_lock_irq(&data->txlock);
5056 clear_bit(BTUSB_SUSPENDING, &data->flags);
5057 spin_unlock_irq(&data->txlock);
5058
5059 return err;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01005060}
Oliver Neukum7bee5492009-08-24 23:44:59 +02005061#endif
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01005062
Marcel Holtmann5e23b922007-10-20 14:12:34 +02005063static struct usb_driver btusb_driver = {
5064 .name = "btusb",
5065 .probe = btusb_probe,
5066 .disconnect = btusb_disconnect,
Oliver Neukum7bee5492009-08-24 23:44:59 +02005067#ifdef CONFIG_PM
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01005068 .suspend = btusb_suspend,
5069 .resume = btusb_resume,
Oliver Neukum7bee5492009-08-24 23:44:59 +02005070#endif
Marcel Holtmann5e23b922007-10-20 14:12:34 +02005071 .id_table = btusb_table,
Oliver Neukum7bee5492009-08-24 23:44:59 +02005072 .supports_autosuspend = 1,
Sarah Sharpe1f12eb2012-04-23 10:08:51 -07005073 .disable_hub_initiated_lpm = 1,
Marcel Holtmann5e23b922007-10-20 14:12:34 +02005074};
5075
Greg Kroah-Hartman93f15082011-11-18 09:47:34 -08005076module_usb_driver(btusb_driver);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02005077
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02005078module_param(disable_scofix, bool, 0644);
5079MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
5080
5081module_param(force_scofix, bool, 0644);
5082MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
5083
Hans de Goedeeff2d682017-11-13 14:44:16 +01005084module_param(enable_autosuspend, bool, 0644);
5085MODULE_PARM_DESC(enable_autosuspend, "Enable USB autosuspend by default");
5086
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02005087module_param(reset, bool, 0644);
5088MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
5089
Marcel Holtmann5e23b922007-10-20 14:12:34 +02005090MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
5091MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
5092MODULE_VERSION(VERSION);
5093MODULE_LICENSE("GPL");