blob: b1a05bb9f4bf8d6c3cad2661e1679cd29fa67cf6 [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
Marcel Holtmannd049f4e2015-01-26 20:35:32 -0800122 /* Broadcom BCM20702B0 (Dynex/Insignia) */
123 { USB_DEVICE(0x19ff, 0x0239), .driver_info = BTUSB_BCM_PATCHRAM },
124
Santtu Rekilä2faf71c2015-10-05 15:45:27 +0300125 /* Broadcom BCM43142A0 (Foxconn/Lenovo) */
Dmitry Tunin628c26b2017-07-06 14:41:13 +0300126 { USB_VENDOR_AND_INTERFACE_INFO(0x105b, 0xff, 0x01, 0x01),
127 .driver_info = BTUSB_BCM_PATCHRAM },
Santtu Rekilä2faf71c2015-10-05 15:45:27 +0300128
Christoph Haaga57bac42017-02-10 14:02:45 +0100129 /* Broadcom BCM920703 (HTC Vive) */
130 { USB_VENDOR_AND_INTERFACE_INFO(0x0bb4, 0xff, 0x01, 0x01),
131 .driver_info = BTUSB_BCM_PATCHRAM },
132
Steven Harms98514032012-04-13 14:45:55 -0400133 /* Foxconn - Hon Hai */
Heinrich Siebmanns6029ddc2014-12-03 19:32:22 +0100134 { USB_VENDOR_AND_INTERFACE_INFO(0x0489, 0xff, 0x01, 0x01),
135 .driver_info = BTUSB_BCM_PATCHRAM },
Steven Harms98514032012-04-13 14:45:55 -0400136
Matej Dubovy8f0c3042015-02-02 18:50:14 +0100137 /* Lite-On Technology - Broadcom based */
138 { USB_VENDOR_AND_INTERFACE_INFO(0x04ca, 0xff, 0x01, 0x01),
139 .driver_info = BTUSB_BCM_PATCHRAM },
140
Andy Shevchenko0b880062014-02-18 18:26:19 +0200141 /* Broadcom devices with vendor specific id */
Petri Gynther10d4c672014-05-08 15:50:01 -0700142 { USB_VENDOR_AND_INTERFACE_INFO(0x0a5c, 0xff, 0x01, 0x01),
143 .driver_info = BTUSB_BCM_PATCHRAM },
Gustavo Padovan92c385f2012-08-06 15:36:49 -0300144
Marcel Holtmannc2aef6e2014-07-21 14:02:33 +0200145 /* ASUSTek Computer - Broadcom based */
Rick Dunn9a5abdaa2015-01-17 05:29:12 +0100146 { USB_VENDOR_AND_INTERFACE_INFO(0x0b05, 0xff, 0x01, 0x01),
147 .driver_info = BTUSB_BCM_PATCHRAM },
Marcel Holtmannc2aef6e2014-07-21 14:02:33 +0200148
Ken O'Brien5bcecf32013-09-21 19:14:43 +0100149 /* Belkin F8065bf - Broadcom based */
Marcel Holtmann6331c682015-03-27 15:11:41 -0700150 { USB_VENDOR_AND_INTERFACE_INFO(0x050d, 0xff, 0x01, 0x01),
151 .driver_info = BTUSB_BCM_PATCHRAM },
Ken O'Brien5bcecf32013-09-21 19:14:43 +0100152
Jurgen Kramer9113bfd2014-02-15 12:01:09 +0100153 /* IMC Networks - Broadcom based */
Marcel Holtmann6331c682015-03-27 15:11:41 -0700154 { USB_VENDOR_AND_INTERFACE_INFO(0x13d3, 0xff, 0x01, 0x01),
155 .driver_info = BTUSB_BCM_PATCHRAM },
Jurgen Kramer9113bfd2014-02-15 12:01:09 +0100156
Wen-chien Jesse Sungfdfddc62017-01-10 15:41:13 +0800157 /* Dell Computer - Broadcom based */
158 { USB_VENDOR_AND_INTERFACE_INFO(0x413c, 0xff, 0x01, 0x01),
159 .driver_info = BTUSB_BCM_PATCHRAM },
160
Dmitry Tunin1623d0b2015-12-05 14:09:36 +0300161 /* Toshiba Corp - Broadcom based */
162 { USB_VENDOR_AND_INTERFACE_INFO(0x0930, 0xff, 0x01, 0x01),
163 .driver_info = BTUSB_BCM_PATCHRAM },
164
Marcel Holtmann40df7832014-07-06 13:29:58 +0200165 /* Intel Bluetooth USB Bootloader (RAM module) */
Marcel Holtmannd92f2df2014-07-06 14:53:55 +0200166 { USB_DEVICE(0x8087, 0x0a5a),
167 .driver_info = BTUSB_INTEL_BOOT | BTUSB_BROKEN_ISOC },
Marcel Holtmann40df7832014-07-06 13:29:58 +0200168
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200169 { } /* Terminating entry */
170};
171
172MODULE_DEVICE_TABLE(usb, btusb_table);
173
Marcel Holtmann54265202013-10-11 07:46:18 -0700174static const struct usb_device_id blacklist_table[] = {
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200175 /* CSR BlueCore devices */
176 { USB_DEVICE(0x0a12, 0x0001), .driver_info = BTUSB_CSR },
177
178 /* Broadcom BCM2033 without firmware */
179 { USB_DEVICE(0x0a5c, 0x2033), .driver_info = BTUSB_IGNORE },
180
Marcel Holtmann6c9d4352015-10-17 14:39:27 +0200181 /* Broadcom BCM2045 devices */
182 { USB_DEVICE(0x0a5c, 0x2045), .driver_info = BTUSB_BCM2045 },
183
Bala Shanmugambe931122010-11-26 17:35:46 +0530184 /* Atheros 3011 with sflash firmware */
Andy Shevchenko0b880062014-02-18 18:26:19 +0200185 { USB_DEVICE(0x0489, 0xe027), .driver_info = BTUSB_IGNORE },
186 { USB_DEVICE(0x0489, 0xe03d), .driver_info = BTUSB_IGNORE },
Alexander Ploumistos2eeff0b2015-02-13 21:05:11 +0200187 { USB_DEVICE(0x04f2, 0xaff1), .driver_info = BTUSB_IGNORE },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200188 { USB_DEVICE(0x0930, 0x0215), .driver_info = BTUSB_IGNORE },
Bala Shanmugambe931122010-11-26 17:35:46 +0530189 { USB_DEVICE(0x0cf3, 0x3002), .driver_info = BTUSB_IGNORE },
Marek Vasut6eda5412012-06-08 14:32:50 +0200190 { USB_DEVICE(0x0cf3, 0xe019), .driver_info = BTUSB_IGNORE },
Andy Ross2a7bccc2011-05-09 16:11:16 -0700191 { USB_DEVICE(0x13d3, 0x3304), .driver_info = BTUSB_IGNORE },
Bala Shanmugambe931122010-11-26 17:35:46 +0530192
Cho, Yu-Chen509e7862011-01-26 17:10:59 +0800193 /* Atheros AR9285 Malbec with sflash firmware */
194 { USB_DEVICE(0x03f0, 0x311d), .driver_info = BTUSB_IGNORE },
195
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530196 /* Atheros 3012 with sflash firmware */
Andy Shevchenko0b880062014-02-18 18:26:19 +0200197 { USB_DEVICE(0x0489, 0xe04d), .driver_info = BTUSB_ATH3012 },
198 { USB_DEVICE(0x0489, 0xe04e), .driver_info = BTUSB_ATH3012 },
199 { USB_DEVICE(0x0489, 0xe056), .driver_info = BTUSB_ATH3012 },
200 { USB_DEVICE(0x0489, 0xe057), .driver_info = BTUSB_ATH3012 },
201 { USB_DEVICE(0x0489, 0xe05f), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin692c0622015-06-06 20:25:40 +0300202 { USB_DEVICE(0x0489, 0xe076), .driver_info = BTUSB_ATH3012 },
Anantha Krishnan4b552bc2014-10-06 16:31:49 +0530203 { USB_DEVICE(0x0489, 0xe078), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin28c971d2016-02-10 00:49:11 +0300204 { USB_DEVICE(0x0489, 0xe095), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200205 { USB_DEVICE(0x04c5, 0x1330), .driver_info = BTUSB_ATH3012 },
206 { USB_DEVICE(0x04ca, 0x3004), .driver_info = BTUSB_ATH3012 },
207 { USB_DEVICE(0x04ca, 0x3005), .driver_info = BTUSB_ATH3012 },
208 { USB_DEVICE(0x04ca, 0x3006), .driver_info = BTUSB_ATH3012 },
Mohammed Habibulla1fb4e092014-04-17 11:37:13 -0700209 { USB_DEVICE(0x04ca, 0x3007), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200210 { USB_DEVICE(0x04ca, 0x3008), .driver_info = BTUSB_ATH3012 },
211 { USB_DEVICE(0x04ca, 0x300b), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin7e730c7f2015-06-18 20:41:51 +0300212 { USB_DEVICE(0x04ca, 0x300d), .driver_info = BTUSB_ATH3012 },
Dmitry Tuninec0810d2015-05-02 13:36:58 +0300213 { USB_DEVICE(0x04ca, 0x300f), .driver_info = BTUSB_ATH3012 },
Janne Heikkinen134d3b32014-12-09 07:44:51 +0200214 { USB_DEVICE(0x04ca, 0x3010), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin81d90442016-02-28 11:04:06 +0300215 { USB_DEVICE(0x04ca, 0x3014), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin441ad622017-01-05 13:19:53 +0300216 { USB_DEVICE(0x04ca, 0x3018), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200217 { USB_DEVICE(0x0930, 0x0219), .driver_info = BTUSB_ATH3012 },
Dmitry Tunincd355ff2015-10-05 19:29:33 +0300218 { USB_DEVICE(0x0930, 0x021c), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200219 { USB_DEVICE(0x0930, 0x0220), .driver_info = BTUSB_ATH3012 },
Vincent Zwanenburg89d29752014-08-08 12:33:56 +0100220 { USB_DEVICE(0x0930, 0x0227), .driver_info = BTUSB_ATH3012 },
Andy Shevchenkoa735f9e2014-02-18 18:26:20 +0200221 { USB_DEVICE(0x0b05, 0x17d0), .driver_info = BTUSB_ATH3012 },
Ming Leid66629c2013-03-15 11:00:39 +0800222 { USB_DEVICE(0x0cf3, 0x0036), .driver_info = BTUSB_ATH3012 },
Hans de Goede544a5912018-04-26 14:18:19 +0200223 { USB_DEVICE(0x0cf3, 0x3004), .driver_info = BTUSB_ATH3012 },
Sunguk Lee94a32d102013-03-12 04:41:58 +0900224 { USB_DEVICE(0x0cf3, 0x3008), .driver_info = BTUSB_ATH3012 },
Cho, Yu-Chen07c0ea82012-03-14 22:01:21 +0200225 { USB_DEVICE(0x0cf3, 0x311d), .driver_info = BTUSB_ATH3012 },
Oliver Neukumb1312372014-01-16 15:37:11 +0100226 { USB_DEVICE(0x0cf3, 0x311e), .driver_info = BTUSB_ATH3012 },
Oliver Neukum1e56f1e2014-01-16 16:02:58 +0100227 { USB_DEVICE(0x0cf3, 0x311f), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200228 { USB_DEVICE(0x0cf3, 0x3121), .driver_info = BTUSB_ATH3012 },
Ming Leiebaf5792013-03-18 23:45:11 +0800229 { USB_DEVICE(0x0cf3, 0x817a), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin18e0afa2015-10-16 11:45:26 +0300230 { USB_DEVICE(0x0cf3, 0x817b), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200231 { USB_DEVICE(0x0cf3, 0xe003), .driver_info = BTUSB_ATH3012 },
AceLan Kaoac713112012-04-19 14:53:45 +0800232 { USB_DEVICE(0x0cf3, 0xe004), .driver_info = BTUSB_ATH3012 },
Peng Chen0a3658c2013-08-30 17:41:40 +0800233 { USB_DEVICE(0x0cf3, 0xe005), .driver_info = BTUSB_ATH3012 },
Wen-chien Jesse Sungca79f232015-05-13 11:39:24 +0800234 { USB_DEVICE(0x0cf3, 0xe006), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200235 { USB_DEVICE(0x13d3, 0x3362), .driver_info = BTUSB_ATH3012 },
236 { USB_DEVICE(0x13d3, 0x3375), .driver_info = BTUSB_ATH3012 },
AceLan Kaoeed307e2012-12-11 11:41:20 +0800237 { USB_DEVICE(0x13d3, 0x3393), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin609574e2016-02-10 15:33:17 +0300238 { USB_DEVICE(0x13d3, 0x3395), .driver_info = BTUSB_ATH3012 },
Sujith Manoharan5b77a1f2013-07-15 09:29:03 +0530239 { USB_DEVICE(0x13d3, 0x3402), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin3bb30a7c2014-11-25 20:19:52 +0300240 { USB_DEVICE(0x13d3, 0x3408), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin033efa92015-01-18 00:16:51 +0300241 { USB_DEVICE(0x13d3, 0x3423), .driver_info = BTUSB_ATH3012 },
Anantha Krishnanfa2f1392014-07-08 19:25:08 +0530242 { USB_DEVICE(0x13d3, 0x3432), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin75c6aca2016-03-04 01:32:19 +0300243 { USB_DEVICE(0x13d3, 0x3472), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin0d0cef62015-06-06 20:29:25 +0300244 { USB_DEVICE(0x13d3, 0x3474), .driver_info = BTUSB_ATH3012 },
Lauro Costa72f9f8b2016-05-09 17:36:11 -0300245 { USB_DEVICE(0x13d3, 0x3487), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin12d86892016-07-12 01:35:18 +0300246 { USB_DEVICE(0x13d3, 0x3490), .driver_info = BTUSB_ATH3012 },
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530247
Cho, Yu-Chene9036e32011-02-15 10:20:07 +0800248 /* Atheros AR5BBU12 with sflash firmware */
249 { USB_DEVICE(0x0489, 0xe02c), .driver_info = BTUSB_IGNORE },
250
Michael Gruetzner85d59722012-05-02 22:33:40 +0200251 /* Atheros AR5BBU12 with sflash firmware */
Yevgeniy Melnichukbc21fde2012-08-07 19:48:10 +0530252 { USB_DEVICE(0x0489, 0xe036), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200253 { USB_DEVICE(0x0489, 0xe03c), .driver_info = BTUSB_ATH3012 },
Michael Gruetzner85d59722012-05-02 22:33:40 +0200254
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +0000255 /* QCA ROME chipset */
Rocky Liaoe99f38b2020-09-17 21:09:23 +0800256 { USB_DEVICE(0x0cf3, 0x535b), .driver_info = BTUSB_QCA_ROME |
257 BTUSB_WIDEBAND_SPEECH },
258 { USB_DEVICE(0x0cf3, 0xe007), .driver_info = BTUSB_QCA_ROME |
259 BTUSB_WIDEBAND_SPEECH },
260 { USB_DEVICE(0x0cf3, 0xe009), .driver_info = BTUSB_QCA_ROME |
261 BTUSB_WIDEBAND_SPEECH },
262 { USB_DEVICE(0x0cf3, 0xe010), .driver_info = BTUSB_QCA_ROME |
263 BTUSB_WIDEBAND_SPEECH },
264 { USB_DEVICE(0x0cf3, 0xe300), .driver_info = BTUSB_QCA_ROME |
265 BTUSB_WIDEBAND_SPEECH },
266 { USB_DEVICE(0x0cf3, 0xe301), .driver_info = BTUSB_QCA_ROME |
267 BTUSB_WIDEBAND_SPEECH },
268 { USB_DEVICE(0x0cf3, 0xe360), .driver_info = BTUSB_QCA_ROME |
269 BTUSB_WIDEBAND_SPEECH },
Daniel Lenski0324d192021-05-18 10:25:46 -0700270 { USB_DEVICE(0x0cf3, 0xe500), .driver_info = BTUSB_QCA_ROME |
271 BTUSB_WIDEBAND_SPEECH },
Rocky Liaoe99f38b2020-09-17 21:09:23 +0800272 { USB_DEVICE(0x0489, 0xe092), .driver_info = BTUSB_QCA_ROME |
273 BTUSB_WIDEBAND_SPEECH },
274 { USB_DEVICE(0x0489, 0xe09f), .driver_info = BTUSB_QCA_ROME |
275 BTUSB_WIDEBAND_SPEECH },
276 { USB_DEVICE(0x0489, 0xe0a2), .driver_info = BTUSB_QCA_ROME |
277 BTUSB_WIDEBAND_SPEECH },
278 { USB_DEVICE(0x04ca, 0x3011), .driver_info = BTUSB_QCA_ROME |
279 BTUSB_WIDEBAND_SPEECH },
280 { USB_DEVICE(0x04ca, 0x3015), .driver_info = BTUSB_QCA_ROME |
281 BTUSB_WIDEBAND_SPEECH },
282 { USB_DEVICE(0x04ca, 0x3016), .driver_info = BTUSB_QCA_ROME |
283 BTUSB_WIDEBAND_SPEECH },
284 { USB_DEVICE(0x04ca, 0x301a), .driver_info = BTUSB_QCA_ROME |
285 BTUSB_WIDEBAND_SPEECH },
286 { USB_DEVICE(0x04ca, 0x3021), .driver_info = BTUSB_QCA_ROME |
287 BTUSB_WIDEBAND_SPEECH },
288 { USB_DEVICE(0x13d3, 0x3491), .driver_info = BTUSB_QCA_ROME |
289 BTUSB_WIDEBAND_SPEECH },
290 { USB_DEVICE(0x13d3, 0x3496), .driver_info = BTUSB_QCA_ROME |
291 BTUSB_WIDEBAND_SPEECH },
292 { USB_DEVICE(0x13d3, 0x3501), .driver_info = BTUSB_QCA_ROME |
293 BTUSB_WIDEBAND_SPEECH },
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +0000294
Rocky Liaob40f58b2020-09-29 12:23:51 +0800295 /* QCA WCN6855 chipset */
296 { USB_DEVICE(0x0cf3, 0xe600), .driver_info = BTUSB_QCA_WCN6855 |
Michael Sunacd5aea2021-07-23 17:17:31 -0700297 BTUSB_WIDEBAND_SPEECH |
298 BTUSB_VALID_LE_STATES },
Rocky Liaob40f58b2020-09-29 12:23:51 +0800299
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200300 /* Broadcom BCM2035 */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100301 { USB_DEVICE(0x0a5c, 0x2009), .driver_info = BTUSB_BCM92035 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200302 { USB_DEVICE(0x0a5c, 0x200a), .driver_info = BTUSB_WRONG_SCO_MTU },
303 { USB_DEVICE(0x0a5c, 0x2035), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200304
305 /* Broadcom BCM2045 */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100306 { USB_DEVICE(0x0a5c, 0x2039), .driver_info = BTUSB_WRONG_SCO_MTU },
307 { USB_DEVICE(0x0a5c, 0x2101), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmannbdbef3d2008-09-23 00:16:35 +0200308
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200309 /* IBM/Lenovo ThinkPad with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100310 { USB_DEVICE(0x0a5c, 0x201e), .driver_info = BTUSB_WRONG_SCO_MTU },
311 { USB_DEVICE(0x0a5c, 0x2110), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200312
313 /* HP laptop with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100314 { USB_DEVICE(0x03f0, 0x171d), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200315
316 /* Dell laptop with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100317 { USB_DEVICE(0x413c, 0x8126), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200318
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100319 /* Dell Wireless 370 and 410 devices */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100320 { USB_DEVICE(0x413c, 0x8152), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100321 { USB_DEVICE(0x413c, 0x8156), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200322
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100323 /* Belkin F8T012 and F8T013 devices */
324 { USB_DEVICE(0x050d, 0x0012), .driver_info = BTUSB_WRONG_SCO_MTU },
325 { USB_DEVICE(0x050d, 0x0013), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200326
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100327 /* Asus WL-BTD202 device */
328 { USB_DEVICE(0x0b05, 0x1715), .driver_info = BTUSB_WRONG_SCO_MTU },
329
330 /* Kensington Bluetooth USB adapter */
331 { USB_DEVICE(0x047d, 0x105e), .driver_info = BTUSB_WRONG_SCO_MTU },
332
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200333 /* RTX Telecom based adapters with buggy SCO support */
334 { USB_DEVICE(0x0400, 0x0807), .driver_info = BTUSB_BROKEN_ISOC },
335 { USB_DEVICE(0x0400, 0x080a), .driver_info = BTUSB_BROKEN_ISOC },
336
337 /* CONWISE Technology based adapters with buggy SCO support */
Szymon Janc418678b2016-09-01 17:22:37 +0200338 { USB_DEVICE(0x0e5e, 0x6622),
339 .driver_info = BTUSB_BROKEN_ISOC | BTUSB_CW6622},
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200340
Marcel Holtmann4fcef8e2015-01-01 17:34:37 -0800341 /* Roper Class 1 Bluetooth Dongle (Silicon Wave based) */
Aleksei Volkov2eeac872015-06-08 12:02:10 +0300342 { USB_DEVICE(0x1310, 0x0001), .driver_info = BTUSB_SWAVE },
Marcel Holtmann4fcef8e2015-01-01 17:34:37 -0800343
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200344 /* Digianswer devices */
345 { USB_DEVICE(0x08fd, 0x0001), .driver_info = BTUSB_DIGIANSWER },
346 { USB_DEVICE(0x08fd, 0x0002), .driver_info = BTUSB_IGNORE },
347
348 /* CSR BlueCore Bluetooth Sniffer */
Marcel Holtmann4f64fa82014-07-07 00:12:04 +0200349 { USB_DEVICE(0x0a12, 0x0002),
350 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200351
352 /* Frontline ComProbe Bluetooth Sniffer */
Marcel Holtmann4f64fa82014-07-07 00:12:04 +0200353 { USB_DEVICE(0x16d3, 0x0002),
354 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200355
Marcel Holtmanncb1ee892015-01-28 19:41:42 -0800356 /* Marvell Bluetooth devices */
357 { USB_DEVICE(0x1286, 0x2044), .driver_info = BTUSB_MARVELL },
358 { USB_DEVICE(0x1286, 0x2046), .driver_info = BTUSB_MARVELL },
Amitkumar Karwar1165df02016-09-28 16:18:35 +0530359 { USB_DEVICE(0x1286, 0x204e), .driver_info = BTUSB_MARVELL },
Marcel Holtmanncb1ee892015-01-28 19:41:42 -0800360
Marcel Holtmannd0ac9eb2015-01-28 19:41:43 -0800361 /* Intel Bluetooth devices */
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 |
Michael Sunacd5aea2021-07-23 17:17:31 -0700372 BTUSB_WIDEBAND_SPEECH |
373 BTUSB_VALID_LE_STATES },
Marcel Holtmann407550f2015-02-22 15:41:18 -0800374 { USB_DEVICE(0x8087, 0x07da), .driver_info = BTUSB_CSR },
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -0700375 { USB_DEVICE(0x8087, 0x07dc), .driver_info = BTUSB_INTEL },
Tedd Ho-Jeong Anef4e5e42013-11-12 13:10:58 -0800376 { USB_DEVICE(0x8087, 0x0a2a), .driver_info = BTUSB_INTEL },
Alain Michaud3e4e3f72020-02-27 18:29:37 +0000377 { USB_DEVICE(0x8087, 0x0a2b), .driver_info = BTUSB_INTEL_NEW |
378 BTUSB_WIDEBAND_SPEECH },
379 { USB_DEVICE(0x8087, 0x0aa7), .driver_info = BTUSB_INTEL |
380 BTUSB_WIDEBAND_SPEECH },
381 { USB_DEVICE(0x8087, 0x0aaa), .driver_info = BTUSB_INTEL_NEW |
Alain Michaudaff8c482020-04-23 14:43:31 +0000382 BTUSB_WIDEBAND_SPEECH |
383 BTUSB_VALID_LE_STATES },
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -0700384
Marcel Holtmannd0ac9eb2015-01-28 19:41:43 -0800385 /* Other Intel Bluetooth devices */
386 { USB_VENDOR_AND_INTERFACE_INFO(0x8087, 0xe0, 0x01, 0x01),
387 .driver_info = BTUSB_IGNORE },
Amitkumar Karwarae8df492014-07-18 14:47:06 -0700388
Joseph Hwang33bfd942020-07-13 15:45:29 +0800389 /* Realtek 8822CE Bluetooth devices */
390 { USB_DEVICE(0x0bda, 0xb00c), .driver_info = BTUSB_REALTEK |
391 BTUSB_WIDEBAND_SPEECH },
Larry Finger4d96d3b2021-06-01 14:04:18 -0500392 { USB_DEVICE(0x0bda, 0xc822), .driver_info = BTUSB_REALTEK |
393 BTUSB_WIDEBAND_SPEECH },
Joseph Hwang33bfd942020-07-13 15:45:29 +0800394
Max Chou0d484db2020-11-10 11:38:37 +0800395 /* Realtek 8852AE Bluetooth devices */
396 { USB_DEVICE(0x0bda, 0xc852), .driver_info = BTUSB_REALTEK |
397 BTUSB_WIDEBAND_SPEECH },
398
Daniel Drakea2698a92015-04-16 14:09:55 -0600399 /* Realtek Bluetooth devices */
400 { USB_VENDOR_AND_INTERFACE_INFO(0x0bda, 0xe0, 0x01, 0x01),
401 .driver_info = BTUSB_REALTEK },
402
Sean Wanga1c49c432019-06-02 08:02:48 +0800403 /* MediaTek Bluetooth devices */
404 { USB_VENDOR_AND_INTERFACE_INFO(0x0e8d, 0xe0, 0x01, 0x01),
mark-yw.chen27e554a2021-03-29 01:18:33 +0800405 .driver_info = BTUSB_MEDIATEK |
406 BTUSB_WIDEBAND_SPEECH |
407 BTUSB_VALID_LE_STATES },
Sean Wanga1c49c432019-06-02 08:02:48 +0800408
Chris Chiu3a567b92020-11-10 11:40:10 +0800409 /* Additional MediaTek MT7615E Bluetooth devices */
410 { USB_DEVICE(0x13d3, 0x3560), .driver_info = BTUSB_MEDIATEK},
411
Forest Crossman37356822021-07-12 02:32:20 -0500412 /* Additional MediaTek MT7668 Bluetooth devices */
413 { USB_DEVICE(0x043e, 0x3109), .driver_info = BTUSB_MEDIATEK |
414 BTUSB_WIDEBAND_SPEECH |
415 BTUSB_VALID_LE_STATES },
416
mark-yw.chen393dc5d2021-04-12 23:06:27 +0800417 /* Additional MediaTek MT7921 Bluetooth devices */
418 { USB_DEVICE(0x04ca, 0x3802), .driver_info = BTUSB_MEDIATEK |
419 BTUSB_WIDEBAND_SPEECH |
420 BTUSB_VALID_LE_STATES },
Wai Paulo Valerio Wang2cdff8c2021-07-08 04:00:59 +0800421 { USB_DEVICE(0x13d3, 0x3563), .driver_info = BTUSB_MEDIATEK |
422 BTUSB_WIDEBAND_SPEECH |
423 BTUSB_VALID_LE_STATES },
Aaron Ma64832df2021-07-23 01:17:18 +0800424 { USB_DEVICE(0x0489, 0xe0cd), .driver_info = BTUSB_MEDIATEK |
425 BTUSB_WIDEBAND_SPEECH |
426 BTUSB_VALID_LE_STATES },
mark-yw.chen393dc5d2021-04-12 23:06:27 +0800427
Daniel Drakea2698a92015-04-16 14:09:55 -0600428 /* Additional Realtek 8723AE Bluetooth devices */
429 { USB_DEVICE(0x0930, 0x021d), .driver_info = BTUSB_REALTEK },
430 { USB_DEVICE(0x13d3, 0x3394), .driver_info = BTUSB_REALTEK },
431
432 /* Additional Realtek 8723BE Bluetooth devices */
433 { USB_DEVICE(0x0489, 0xe085), .driver_info = BTUSB_REALTEK },
434 { USB_DEVICE(0x0489, 0xe08b), .driver_info = BTUSB_REALTEK },
435 { USB_DEVICE(0x13d3, 0x3410), .driver_info = BTUSB_REALTEK },
436 { USB_DEVICE(0x13d3, 0x3416), .driver_info = BTUSB_REALTEK },
437 { USB_DEVICE(0x13d3, 0x3459), .driver_info = BTUSB_REALTEK },
Dmitry Tunina81d72d2017-08-08 14:09:02 +0300438 { USB_DEVICE(0x13d3, 0x3494), .driver_info = BTUSB_REALTEK },
Daniel Drakea2698a92015-04-16 14:09:55 -0600439
Vicente Bergasa41e0792018-03-20 19:41:10 +0100440 /* Additional Realtek 8723BU Bluetooth devices */
441 { USB_DEVICE(0x7392, 0xa611), .driver_info = BTUSB_REALTEK },
442
Jian-Hong Pan66d99752018-05-21 18:09:20 +0800443 /* Additional Realtek 8723DE Bluetooth devices */
Jian-Hong Pan45ae68b2018-05-25 17:54:52 +0800444 { USB_DEVICE(0x0bda, 0xb009), .driver_info = BTUSB_REALTEK },
Jian-Hong Pan66d99752018-05-21 18:09:20 +0800445 { USB_DEVICE(0x2ff8, 0xb011), .driver_info = BTUSB_REALTEK },
446
Joakim Tjernlund33404382021-05-28 17:26:45 +0200447 /* Additional Realtek 8761BU Bluetooth devices */
448 { USB_DEVICE(0x0b05, 0x190e), .driver_info = BTUSB_REALTEK |
449 BTUSB_WIDEBAND_SPEECH },
450
Daniel Drakea2698a92015-04-16 14:09:55 -0600451 /* Additional Realtek 8821AE Bluetooth devices */
452 { USB_DEVICE(0x0b05, 0x17dc), .driver_info = BTUSB_REALTEK },
453 { USB_DEVICE(0x13d3, 0x3414), .driver_info = BTUSB_REALTEK },
454 { USB_DEVICE(0x13d3, 0x3458), .driver_info = BTUSB_REALTEK },
455 { USB_DEVICE(0x13d3, 0x3461), .driver_info = BTUSB_REALTEK },
456 { USB_DEVICE(0x13d3, 0x3462), .driver_info = BTUSB_REALTEK },
457
Larry Fingerfed03fe2018-02-11 12:24:32 -0600458 /* Additional Realtek 8822BE Bluetooth devices */
Artiom Vaskov1cd2fab2018-05-30 11:23:00 +0300459 { USB_DEVICE(0x13d3, 0x3526), .driver_info = BTUSB_REALTEK },
Larry Fingerfed03fe2018-02-11 12:24:32 -0600460 { USB_DEVICE(0x0b05, 0x185c), .driver_info = BTUSB_REALTEK },
461
Jian-Hong Pan6d0762b2019-09-03 17:10:42 +0800462 /* Additional Realtek 8822CE Bluetooth devices */
Max Chou73280f12020-11-05 18:54:48 +0800463 { USB_DEVICE(0x04ca, 0x4005), .driver_info = BTUSB_REALTEK |
464 BTUSB_WIDEBAND_SPEECH },
465 { USB_DEVICE(0x04c5, 0x161f), .driver_info = BTUSB_REALTEK |
466 BTUSB_WIDEBAND_SPEECH },
467 { USB_DEVICE(0x0b05, 0x18ef), .driver_info = BTUSB_REALTEK |
468 BTUSB_WIDEBAND_SPEECH },
469 { USB_DEVICE(0x13d3, 0x3548), .driver_info = BTUSB_REALTEK |
470 BTUSB_WIDEBAND_SPEECH },
471 { USB_DEVICE(0x13d3, 0x3549), .driver_info = BTUSB_REALTEK |
472 BTUSB_WIDEBAND_SPEECH },
473 { USB_DEVICE(0x13d3, 0x3553), .driver_info = BTUSB_REALTEK |
474 BTUSB_WIDEBAND_SPEECH },
475 { USB_DEVICE(0x13d3, 0x3555), .driver_info = BTUSB_REALTEK |
476 BTUSB_WIDEBAND_SPEECH },
477 { USB_DEVICE(0x2ff8, 0x3051), .driver_info = BTUSB_REALTEK |
478 BTUSB_WIDEBAND_SPEECH },
Julian Pidancetf4962972020-11-09 17:47:22 +0100479 { USB_DEVICE(0x1358, 0xc123), .driver_info = BTUSB_REALTEK |
480 BTUSB_WIDEBAND_SPEECH },
Cadel Watson24d6a6d2020-11-19 16:16:25 +1100481 { USB_DEVICE(0x0bda, 0xc123), .driver_info = BTUSB_REALTEK |
482 BTUSB_WIDEBAND_SPEECH },
Rasmus Moorats3edc5782021-02-18 13:11:24 +0200483 { USB_DEVICE(0x0cb5, 0xc547), .driver_info = BTUSB_REALTEK |
484 BTUSB_WIDEBAND_SPEECH },
Jian-Hong Pan6d0762b2019-09-03 17:10:42 +0800485
Peter Poklop4481c072015-08-15 20:47:09 +0200486 /* Silicon Wave based devices */
487 { USB_DEVICE(0x0c10, 0x0000), .driver_info = BTUSB_SWAVE },
488
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200489 { } /* Terminating entry */
490};
491
Hans de Goede1fdb9262018-02-20 09:06:18 +0100492/* The Bluetooth USB module build into some devices needs to be reset on resume,
493 * this is a problem with the platform (likely shutting off all power) not with
494 * the module itself. So we use a DMI list to match known broken platforms.
495 */
496static const struct dmi_system_id btusb_needs_reset_resume_table[] = {
497 {
Kai-Heng Feng0c6e5262018-03-01 13:42:52 +0800498 /* Dell OptiPlex 3060 (QCA ROME device 0cf3:e007) */
Hans de Goede1fdb9262018-02-20 09:06:18 +0100499 .matches = {
Kai-Heng Feng0c6e5262018-03-01 13:42:52 +0800500 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
501 DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 3060"),
Hans de Goede1fdb9262018-02-20 09:06:18 +0100502 },
503 },
Hans de Goede596b07a2018-04-26 20:52:06 +0200504 {
505 /* Dell XPS 9360 (QCA ROME device 0cf3:e300) */
506 .matches = {
507 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
508 DMI_MATCH(DMI_PRODUCT_NAME, "XPS 13 9360"),
509 },
510 },
Hans de Goede939bc6c2018-05-22 09:34:10 +0200511 {
512 /* Dell Inspiron 5565 (QCA ROME device 0cf3:e009) */
513 .matches = {
514 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
515 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5565"),
516 },
517 },
Hans de Goede1fdb9262018-02-20 09:06:18 +0100518 {}
519};
520
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200521#define BTUSB_MAX_ISOC_FRAMES 10
522
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200523#define BTUSB_INTR_RUNNING 0
524#define BTUSB_BULK_RUNNING 1
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200525#define BTUSB_ISOC_RUNNING 2
Oliver Neukum7bee5492009-08-24 23:44:59 +0200526#define BTUSB_SUSPENDING 3
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -0300527#define BTUSB_DID_ISO_RESUME 4
Marcel Holtmanncda0dd72015-01-26 21:33:48 -0800528#define BTUSB_BOOTLOADER 5
529#define BTUSB_DOWNLOADING 6
Marcel Holtmannce6bb922015-01-28 01:58:40 -0800530#define BTUSB_FIRMWARE_LOADED 7
Marcel Holtmanncda0dd72015-01-26 21:33:48 -0800531#define BTUSB_FIRMWARE_FAILED 8
Marcel Holtmannce6bb922015-01-28 01:58:40 -0800532#define BTUSB_BOOTING 9
Hans de Goede61f5ace2018-01-08 10:44:16 +0100533#define BTUSB_DIAG_RUNNING 10
534#define BTUSB_OOB_WAKE_ENABLED 11
Rajat Jaindc786b22019-01-24 15:28:14 -0800535#define BTUSB_HW_RESET_ACTIVE 12
Sean Wanga1c49c432019-06-02 08:02:48 +0800536#define BTUSB_TX_WAIT_VND_EVT 13
Alex Lu9e455242019-08-14 20:02:52 +0800537#define BTUSB_WAKEUP_DISABLE 14
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200538
539struct btusb_data {
540 struct hci_dev *hdev;
541 struct usb_device *udev;
Marcel Holtmann5fbcd262008-09-23 00:16:36 +0200542 struct usb_interface *intf;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200543 struct usb_interface *isoc;
Marcel Holtmann9d08f502015-10-08 20:23:08 +0200544 struct usb_interface *diag;
Marcel Holtmann459232f2017-10-24 19:42:45 +0200545 unsigned isoc_ifnum;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200546
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200547 unsigned long flags;
548
549 struct work_struct work;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200550 struct work_struct waker;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200551
Marcel Holtmann803b5832014-09-16 08:00:29 +0200552 struct usb_anchor deferred;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200553 struct usb_anchor tx_anchor;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200554 int tx_in_flight;
555 spinlock_t txlock;
556
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200557 struct usb_anchor intr_anchor;
558 struct usb_anchor bulk_anchor;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200559 struct usb_anchor isoc_anchor;
Marcel Holtmann9d08f502015-10-08 20:23:08 +0200560 struct usb_anchor diag_anchor;
Sean Wanga1c49c432019-06-02 08:02:48 +0800561 struct usb_anchor ctrl_anchor;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200562 spinlock_t rxlock;
563
564 struct sk_buff *evt_skb;
565 struct sk_buff *acl_skb;
566 struct sk_buff *sco_skb;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200567
568 struct usb_endpoint_descriptor *intr_ep;
569 struct usb_endpoint_descriptor *bulk_tx_ep;
570 struct usb_endpoint_descriptor *bulk_rx_ep;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200571 struct usb_endpoint_descriptor *isoc_tx_ep;
572 struct usb_endpoint_descriptor *isoc_rx_ep;
Marcel Holtmann9d08f502015-10-08 20:23:08 +0200573 struct usb_endpoint_descriptor *diag_tx_ep;
574 struct usb_endpoint_descriptor *diag_rx_ep;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200575
Rajat Jaindc786b22019-01-24 15:28:14 -0800576 struct gpio_desc *reset_gpio;
577
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100578 __u8 cmdreq_type;
Marcel Holtmann893ba542015-01-28 20:27:34 -0800579 __u8 cmdreq;
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100580
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100581 unsigned int sco_num;
Sathish Narasimmanbaac6272020-04-03 21:43:59 +0200582 unsigned int air_mode;
583 bool usb_alt6_packet_flow;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200584 int isoc_altsetting;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +0100585 int suspend_count;
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +0100586
Marcel Holtmann97307f52015-01-12 13:51:10 -0800587 int (*recv_event)(struct hci_dev *hdev, struct sk_buff *skb);
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +0100588 int (*recv_bulk)(struct btusb_data *data, void *buffer, int count);
Kim, Ben Young Taeace31982015-02-15 23:06:14 +0000589
590 int (*setup_on_usb)(struct hci_dev *hdev);
Rajat Jainfd913ef2017-02-01 14:24:09 -0800591
592 int oob_wake_irq; /* irq for out-of-band wake-on-bt */
Rajat Jaindc786b22019-01-24 15:28:14 -0800593 unsigned cmd_timeout_cnt;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200594};
595
Rajat Jaindc786b22019-01-24 15:28:14 -0800596static void btusb_intel_cmd_timeout(struct hci_dev *hdev)
597{
598 struct btusb_data *data = hci_get_drvdata(hdev);
599 struct gpio_desc *reset_gpio = data->reset_gpio;
600
601 if (++data->cmd_timeout_cnt < 5)
602 return;
603
604 if (!reset_gpio) {
605 bt_dev_err(hdev, "No way to reset. Ignoring and continuing");
606 return;
607 }
608
609 /*
610 * Toggle the hard reset line if the platform provides one. The reset
611 * is going to yank the device off the USB and then replug. So doing
612 * once is enough. The cleanup is handled correctly on the way out
613 * (standard USB disconnect), and the new device is detected cleanly
614 * and bound to the driver again like it should be.
615 */
616 if (test_and_set_bit(BTUSB_HW_RESET_ACTIVE, &data->flags)) {
617 bt_dev_err(hdev, "last reset failed? Not resetting again");
618 return;
619 }
620
621 bt_dev_err(hdev, "Initiating HW reset via gpio");
Rajat Jain2de66bb82019-01-28 15:08:09 -0800622 gpiod_set_value_cansleep(reset_gpio, 1);
623 msleep(100);
624 gpiod_set_value_cansleep(reset_gpio, 0);
Rajat Jaindc786b22019-01-24 15:28:14 -0800625}
626
Alex Lud7ef0d12019-09-05 10:36:31 +0800627static void btusb_rtl_cmd_timeout(struct hci_dev *hdev)
628{
629 struct btusb_data *data = hci_get_drvdata(hdev);
630 struct gpio_desc *reset_gpio = data->reset_gpio;
631
632 if (++data->cmd_timeout_cnt < 5)
633 return;
634
635 if (!reset_gpio) {
636 bt_dev_err(hdev, "No gpio to reset Realtek device, ignoring");
637 return;
638 }
639
640 /* Toggle the hard reset line. The Realtek device is going to
641 * yank itself off the USB and then replug. The cleanup is handled
642 * correctly on the way out (standard USB disconnect), and the new
643 * device is detected cleanly and bound to the driver again like
644 * it should be.
645 */
646 if (test_and_set_bit(BTUSB_HW_RESET_ACTIVE, &data->flags)) {
647 bt_dev_err(hdev, "last reset failed? Not resetting again");
648 return;
649 }
650
651 bt_dev_err(hdev, "Reset Realtek device via gpio");
Alex Lud7ef0d12019-09-05 10:36:31 +0800652 gpiod_set_value_cansleep(reset_gpio, 1);
Max Chou34682112019-11-27 11:01:07 +0800653 msleep(200);
654 gpiod_set_value_cansleep(reset_gpio, 0);
Alex Lud7ef0d12019-09-05 10:36:31 +0800655}
656
Abhishek Pandit-Subedi69335682020-06-24 11:11:44 -0700657static void btusb_qca_cmd_timeout(struct hci_dev *hdev)
658{
659 struct btusb_data *data = hci_get_drvdata(hdev);
660 int err;
661
662 if (++data->cmd_timeout_cnt < 5)
663 return;
664
665 bt_dev_err(hdev, "Multiple cmd timeouts seen. Resetting usb device.");
Abhishek Pandit-Subedib980d472020-06-25 16:26:27 -0700666 /* This is not an unbalanced PM reference since the device will reset */
Abhishek Pandit-Subedi69335682020-06-24 11:11:44 -0700667 err = usb_autopm_get_interface(data->intf);
668 if (!err)
669 usb_queue_reset_device(data->intf);
670 else
671 bt_dev_err(hdev, "Failed usb_autopm_get_interface with %d", err);
672}
673
Marcel Holtmann803b5832014-09-16 08:00:29 +0200674static inline void btusb_free_frags(struct btusb_data *data)
675{
676 unsigned long flags;
677
678 spin_lock_irqsave(&data->rxlock, flags);
679
680 kfree_skb(data->evt_skb);
681 data->evt_skb = NULL;
682
683 kfree_skb(data->acl_skb);
684 data->acl_skb = NULL;
685
686 kfree_skb(data->sco_skb);
687 data->sco_skb = NULL;
688
689 spin_unlock_irqrestore(&data->rxlock, flags);
690}
691
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200692static int btusb_recv_intr(struct btusb_data *data, void *buffer, int count)
693{
Marcel Holtmann803b5832014-09-16 08:00:29 +0200694 struct sk_buff *skb;
Sebastian Andrzej Siewiora5e50d52018-06-19 23:56:57 +0200695 unsigned long flags;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200696 int err = 0;
697
Sebastian Andrzej Siewiora5e50d52018-06-19 23:56:57 +0200698 spin_lock_irqsave(&data->rxlock, flags);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200699 skb = data->evt_skb;
700
701 while (count) {
702 int len;
703
704 if (!skb) {
705 skb = bt_skb_alloc(HCI_MAX_EVENT_SIZE, GFP_ATOMIC);
706 if (!skb) {
707 err = -ENOMEM;
708 break;
709 }
710
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100711 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
712 hci_skb_expect(skb) = HCI_EVENT_HDR_SIZE;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200713 }
714
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100715 len = min_t(uint, hci_skb_expect(skb), count);
Johannes Berg59ae1d12017-06-16 14:29:20 +0200716 skb_put_data(skb, buffer, len);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200717
718 count -= len;
719 buffer += len;
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100720 hci_skb_expect(skb) -= len;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200721
722 if (skb->len == HCI_EVENT_HDR_SIZE) {
723 /* Complete event header */
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100724 hci_skb_expect(skb) = hci_event_hdr(skb)->plen;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200725
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100726 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
Marcel Holtmann803b5832014-09-16 08:00:29 +0200727 kfree_skb(skb);
728 skb = NULL;
729
730 err = -EILSEQ;
731 break;
732 }
733 }
734
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100735 if (!hci_skb_expect(skb)) {
Marcel Holtmann803b5832014-09-16 08:00:29 +0200736 /* Complete frame */
Marcel Holtmann97307f52015-01-12 13:51:10 -0800737 data->recv_event(data->hdev, skb);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200738 skb = NULL;
739 }
740 }
741
742 data->evt_skb = skb;
Sebastian Andrzej Siewiora5e50d52018-06-19 23:56:57 +0200743 spin_unlock_irqrestore(&data->rxlock, flags);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200744
745 return err;
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200746}
747
748static int btusb_recv_bulk(struct btusb_data *data, void *buffer, int count)
749{
Marcel Holtmann803b5832014-09-16 08:00:29 +0200750 struct sk_buff *skb;
Sebastian Andrzej Siewiora5e50d52018-06-19 23:56:57 +0200751 unsigned long flags;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200752 int err = 0;
753
Sebastian Andrzej Siewiora5e50d52018-06-19 23:56:57 +0200754 spin_lock_irqsave(&data->rxlock, flags);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200755 skb = data->acl_skb;
756
757 while (count) {
758 int len;
759
760 if (!skb) {
761 skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
762 if (!skb) {
763 err = -ENOMEM;
764 break;
765 }
766
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100767 hci_skb_pkt_type(skb) = HCI_ACLDATA_PKT;
768 hci_skb_expect(skb) = HCI_ACL_HDR_SIZE;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200769 }
770
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100771 len = min_t(uint, hci_skb_expect(skb), count);
Johannes Berg59ae1d12017-06-16 14:29:20 +0200772 skb_put_data(skb, buffer, len);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200773
774 count -= len;
775 buffer += len;
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100776 hci_skb_expect(skb) -= len;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200777
778 if (skb->len == HCI_ACL_HDR_SIZE) {
779 __le16 dlen = hci_acl_hdr(skb)->dlen;
780
781 /* Complete ACL header */
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100782 hci_skb_expect(skb) = __le16_to_cpu(dlen);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200783
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100784 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
Marcel Holtmann803b5832014-09-16 08:00:29 +0200785 kfree_skb(skb);
786 skb = NULL;
787
788 err = -EILSEQ;
789 break;
790 }
791 }
792
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100793 if (!hci_skb_expect(skb)) {
Marcel Holtmann803b5832014-09-16 08:00:29 +0200794 /* Complete frame */
795 hci_recv_frame(data->hdev, skb);
796 skb = NULL;
797 }
798 }
799
800 data->acl_skb = skb;
Sebastian Andrzej Siewiora5e50d52018-06-19 23:56:57 +0200801 spin_unlock_irqrestore(&data->rxlock, flags);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200802
803 return err;
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200804}
805
806static int btusb_recv_isoc(struct btusb_data *data, void *buffer, int count)
807{
Marcel Holtmann803b5832014-09-16 08:00:29 +0200808 struct sk_buff *skb;
Sebastian Andrzej Siewiora5e50d52018-06-19 23:56:57 +0200809 unsigned long flags;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200810 int err = 0;
811
Sebastian Andrzej Siewiora5e50d52018-06-19 23:56:57 +0200812 spin_lock_irqsave(&data->rxlock, flags);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200813 skb = data->sco_skb;
814
815 while (count) {
816 int len;
817
818 if (!skb) {
819 skb = bt_skb_alloc(HCI_MAX_SCO_SIZE, GFP_ATOMIC);
820 if (!skb) {
821 err = -ENOMEM;
822 break;
823 }
824
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100825 hci_skb_pkt_type(skb) = HCI_SCODATA_PKT;
826 hci_skb_expect(skb) = HCI_SCO_HDR_SIZE;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200827 }
828
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100829 len = min_t(uint, hci_skb_expect(skb), count);
Johannes Berg59ae1d12017-06-16 14:29:20 +0200830 skb_put_data(skb, buffer, len);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200831
832 count -= len;
833 buffer += len;
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100834 hci_skb_expect(skb) -= len;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200835
836 if (skb->len == HCI_SCO_HDR_SIZE) {
837 /* Complete SCO header */
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100838 hci_skb_expect(skb) = hci_sco_hdr(skb)->dlen;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200839
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100840 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
Marcel Holtmann803b5832014-09-16 08:00:29 +0200841 kfree_skb(skb);
842 skb = NULL;
843
844 err = -EILSEQ;
845 break;
846 }
847 }
848
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100849 if (!hci_skb_expect(skb)) {
Marcel Holtmann803b5832014-09-16 08:00:29 +0200850 /* Complete frame */
851 hci_recv_frame(data->hdev, skb);
852 skb = NULL;
853 }
854 }
855
856 data->sco_skb = skb;
Sebastian Andrzej Siewiora5e50d52018-06-19 23:56:57 +0200857 spin_unlock_irqrestore(&data->rxlock, flags);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200858
859 return err;
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200860}
861
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200862static void btusb_intr_complete(struct urb *urb)
863{
864 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +0100865 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200866 int err;
867
Marcel Holtmann89e75332014-09-16 04:44:50 +0200868 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
869 urb->actual_length);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200870
871 if (!test_bit(HCI_RUNNING, &hdev->flags))
872 return;
873
874 if (urb->status == 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200875 hdev->stat.byte_rx += urb->actual_length;
876
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200877 if (btusb_recv_intr(data, urb->transfer_buffer,
878 urb->actual_length) < 0) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +0100879 bt_dev_err(hdev, "corrupted event packet");
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200880 hdev->stat.err_rx++;
881 }
Champion Chen85560c42014-09-06 14:06:08 -0500882 } else if (urb->status == -ENOENT) {
883 /* Avoid suspend failed when usb_kill_urb */
884 return;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200885 }
886
887 if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
888 return;
889
Oliver Neukum7bee5492009-08-24 23:44:59 +0200890 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200891 usb_anchor_urb(urb, &data->intr_anchor);
892
893 err = usb_submit_urb(urb, GFP_ATOMIC);
894 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200895 /* -EPERM: urb is being killed;
Derek Robsond98422c2017-07-22 13:47:07 +1200896 * -ENODEV: device got disconnected
897 */
Paul Bolle4935f1c2011-08-09 17:16:28 +0200898 if (err != -EPERM && err != -ENODEV)
Marcel Holtmann2064ee32017-10-30 10:42:59 +0100899 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
900 urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200901 usb_unanchor_urb(urb);
902 }
903}
904
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100905static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200906{
David Herrmann155961e2012-02-09 21:58:32 +0100907 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200908 struct urb *urb;
909 unsigned char *buf;
910 unsigned int pipe;
911 int err, size;
912
913 BT_DBG("%s", hdev->name);
914
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200915 if (!data->intr_ep)
916 return -ENODEV;
917
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100918 urb = usb_alloc_urb(0, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200919 if (!urb)
920 return -ENOMEM;
921
922 size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
923
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100924 buf = kmalloc(size, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200925 if (!buf) {
926 usb_free_urb(urb);
927 return -ENOMEM;
928 }
929
930 pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
931
932 usb_fill_int_urb(urb, data->udev, pipe, buf, size,
Marcel Holtmann89e75332014-09-16 04:44:50 +0200933 btusb_intr_complete, hdev, data->intr_ep->bInterval);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200934
935 urb->transfer_flags |= URB_FREE_BUFFER;
936
937 usb_anchor_urb(urb, &data->intr_anchor);
938
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100939 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200940 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +0200941 if (err != -EPERM && err != -ENODEV)
Marcel Holtmann2064ee32017-10-30 10:42:59 +0100942 bt_dev_err(hdev, "urb %p submission failed (%d)",
943 urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200944 usb_unanchor_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200945 }
946
947 usb_free_urb(urb);
948
949 return err;
950}
951
952static void btusb_bulk_complete(struct urb *urb)
953{
954 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +0100955 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200956 int err;
957
Marcel Holtmann89e75332014-09-16 04:44:50 +0200958 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
959 urb->actual_length);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200960
961 if (!test_bit(HCI_RUNNING, &hdev->flags))
962 return;
963
964 if (urb->status == 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200965 hdev->stat.byte_rx += urb->actual_length;
966
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +0100967 if (data->recv_bulk(data, urb->transfer_buffer,
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200968 urb->actual_length) < 0) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +0100969 bt_dev_err(hdev, "corrupted ACL packet");
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200970 hdev->stat.err_rx++;
971 }
Champion Chen85560c42014-09-06 14:06:08 -0500972 } else if (urb->status == -ENOENT) {
973 /* Avoid suspend failed when usb_kill_urb */
974 return;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200975 }
976
977 if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
978 return;
979
980 usb_anchor_urb(urb, &data->bulk_anchor);
Oliver Neukum652fd782009-12-16 19:23:43 +0100981 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200982
983 err = usb_submit_urb(urb, GFP_ATOMIC);
984 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200985 /* -EPERM: urb is being killed;
Derek Robsond98422c2017-07-22 13:47:07 +1200986 * -ENODEV: device got disconnected
987 */
Paul Bolle4935f1c2011-08-09 17:16:28 +0200988 if (err != -EPERM && err != -ENODEV)
Marcel Holtmann2064ee32017-10-30 10:42:59 +0100989 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
990 urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200991 usb_unanchor_urb(urb);
992 }
993}
994
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100995static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200996{
David Herrmann155961e2012-02-09 21:58:32 +0100997 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200998 struct urb *urb;
999 unsigned char *buf;
1000 unsigned int pipe;
Vikram Kandukuri290ba202009-07-02 14:31:59 +05301001 int err, size = HCI_MAX_FRAME_SIZE;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001002
1003 BT_DBG("%s", hdev->name);
1004
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001005 if (!data->bulk_rx_ep)
1006 return -ENODEV;
1007
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001008 urb = usb_alloc_urb(0, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001009 if (!urb)
1010 return -ENOMEM;
1011
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001012 buf = kmalloc(size, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001013 if (!buf) {
1014 usb_free_urb(urb);
1015 return -ENOMEM;
1016 }
1017
1018 pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
1019
Marcel Holtmann89e75332014-09-16 04:44:50 +02001020 usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
1021 btusb_bulk_complete, hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001022
1023 urb->transfer_flags |= URB_FREE_BUFFER;
1024
Oliver Neukum7bee5492009-08-24 23:44:59 +02001025 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001026 usb_anchor_urb(urb, &data->bulk_anchor);
1027
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001028 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001029 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +02001030 if (err != -EPERM && err != -ENODEV)
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001031 bt_dev_err(hdev, "urb %p submission failed (%d)",
1032 urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001033 usb_unanchor_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001034 }
1035
1036 usb_free_urb(urb);
1037
1038 return err;
1039}
1040
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001041static void btusb_isoc_complete(struct urb *urb)
1042{
1043 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +01001044 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001045 int i, err;
1046
Marcel Holtmann89e75332014-09-16 04:44:50 +02001047 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1048 urb->actual_length);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001049
1050 if (!test_bit(HCI_RUNNING, &hdev->flags))
1051 return;
1052
1053 if (urb->status == 0) {
1054 for (i = 0; i < urb->number_of_packets; i++) {
1055 unsigned int offset = urb->iso_frame_desc[i].offset;
1056 unsigned int length = urb->iso_frame_desc[i].actual_length;
1057
1058 if (urb->iso_frame_desc[i].status)
1059 continue;
1060
1061 hdev->stat.byte_rx += length;
1062
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +02001063 if (btusb_recv_isoc(data, urb->transfer_buffer + offset,
1064 length) < 0) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001065 bt_dev_err(hdev, "corrupted SCO packet");
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001066 hdev->stat.err_rx++;
1067 }
1068 }
Champion Chen85560c42014-09-06 14:06:08 -05001069 } else if (urb->status == -ENOENT) {
1070 /* Avoid suspend failed when usb_kill_urb */
1071 return;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001072 }
1073
1074 if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags))
1075 return;
1076
1077 usb_anchor_urb(urb, &data->isoc_anchor);
1078
1079 err = usb_submit_urb(urb, GFP_ATOMIC);
1080 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +02001081 /* -EPERM: urb is being killed;
Derek Robsond98422c2017-07-22 13:47:07 +12001082 * -ENODEV: device got disconnected
1083 */
Paul Bolle4935f1c2011-08-09 17:16:28 +02001084 if (err != -EPERM && err != -ENODEV)
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001085 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
1086 urb, -err);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001087 usb_unanchor_urb(urb);
1088 }
1089}
1090
Sathish Narasimmanbaac6272020-04-03 21:43:59 +02001091static inline void __fill_isoc_descriptor_msbc(struct urb *urb, int len,
1092 int mtu, struct btusb_data *data)
1093{
1094 int i, offset = 0;
1095 unsigned int interval;
1096
1097 BT_DBG("len %d mtu %d", len, mtu);
1098
1099 /* For mSBC ALT 6 setting the host will send the packet at continuous
1100 * flow. As per core spec 5, vol 4, part B, table 2.1. For ALT setting
1101 * 6 the HCI PACKET INTERVAL should be 7.5ms for every usb packets.
1102 * To maintain the rate we send 63bytes of usb packets alternatively for
1103 * 7ms and 8ms to maintain the rate as 7.5ms.
1104 */
1105 if (data->usb_alt6_packet_flow) {
1106 interval = 7;
1107 data->usb_alt6_packet_flow = false;
1108 } else {
1109 interval = 6;
1110 data->usb_alt6_packet_flow = true;
1111 }
1112
1113 for (i = 0; i < interval; i++) {
1114 urb->iso_frame_desc[i].offset = offset;
1115 urb->iso_frame_desc[i].length = offset;
1116 }
1117
1118 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
1119 urb->iso_frame_desc[i].offset = offset;
1120 urb->iso_frame_desc[i].length = len;
1121 i++;
1122 }
1123
1124 urb->number_of_packets = i;
1125}
1126
Jesper Juhl42b16b32011-01-17 00:09:38 +01001127static inline void __fill_isoc_descriptor(struct urb *urb, int len, int mtu)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001128{
1129 int i, offset = 0;
1130
1131 BT_DBG("len %d mtu %d", len, mtu);
1132
1133 for (i = 0; i < BTUSB_MAX_ISOC_FRAMES && len >= mtu;
1134 i++, offset += mtu, len -= mtu) {
1135 urb->iso_frame_desc[i].offset = offset;
1136 urb->iso_frame_desc[i].length = mtu;
1137 }
1138
1139 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
1140 urb->iso_frame_desc[i].offset = offset;
1141 urb->iso_frame_desc[i].length = len;
1142 i++;
1143 }
1144
1145 urb->number_of_packets = i;
1146}
1147
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001148static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001149{
David Herrmann155961e2012-02-09 21:58:32 +01001150 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001151 struct urb *urb;
1152 unsigned char *buf;
1153 unsigned int pipe;
1154 int err, size;
1155
1156 BT_DBG("%s", hdev->name);
1157
1158 if (!data->isoc_rx_ep)
1159 return -ENODEV;
1160
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001161 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001162 if (!urb)
1163 return -ENOMEM;
1164
1165 size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) *
1166 BTUSB_MAX_ISOC_FRAMES;
1167
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001168 buf = kmalloc(size, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001169 if (!buf) {
1170 usb_free_urb(urb);
1171 return -ENOMEM;
1172 }
1173
1174 pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress);
1175
Bing Zhaofa0fb932011-12-20 18:19:00 -08001176 usb_fill_int_urb(urb, data->udev, pipe, buf, size, btusb_isoc_complete,
Marcel Holtmann89e75332014-09-16 04:44:50 +02001177 hdev, data->isoc_rx_ep->bInterval);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001178
Marcel Holtmann89e75332014-09-16 04:44:50 +02001179 urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001180
1181 __fill_isoc_descriptor(urb, size,
Marcel Holtmann89e75332014-09-16 04:44:50 +02001182 le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize));
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001183
1184 usb_anchor_urb(urb, &data->isoc_anchor);
1185
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001186 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001187 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +02001188 if (err != -EPERM && err != -ENODEV)
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001189 bt_dev_err(hdev, "urb %p submission failed (%d)",
1190 urb, -err);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001191 usb_unanchor_urb(urb);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001192 }
1193
1194 usb_free_urb(urb);
1195
1196 return err;
1197}
1198
Marcel Holtmann9d08f502015-10-08 20:23:08 +02001199static void btusb_diag_complete(struct urb *urb)
1200{
1201 struct hci_dev *hdev = urb->context;
1202 struct btusb_data *data = hci_get_drvdata(hdev);
1203 int err;
1204
1205 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1206 urb->actual_length);
1207
1208 if (urb->status == 0) {
1209 struct sk_buff *skb;
1210
1211 skb = bt_skb_alloc(urb->actual_length, GFP_ATOMIC);
1212 if (skb) {
Johannes Berg59ae1d12017-06-16 14:29:20 +02001213 skb_put_data(skb, urb->transfer_buffer,
1214 urb->actual_length);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02001215 hci_recv_diag(hdev, skb);
1216 }
1217 } else if (urb->status == -ENOENT) {
1218 /* Avoid suspend failed when usb_kill_urb */
1219 return;
1220 }
1221
1222 if (!test_bit(BTUSB_DIAG_RUNNING, &data->flags))
1223 return;
1224
1225 usb_anchor_urb(urb, &data->diag_anchor);
1226 usb_mark_last_busy(data->udev);
1227
1228 err = usb_submit_urb(urb, GFP_ATOMIC);
1229 if (err < 0) {
1230 /* -EPERM: urb is being killed;
Derek Robsond98422c2017-07-22 13:47:07 +12001231 * -ENODEV: device got disconnected
1232 */
Marcel Holtmann9d08f502015-10-08 20:23:08 +02001233 if (err != -EPERM && err != -ENODEV)
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001234 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
1235 urb, -err);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02001236 usb_unanchor_urb(urb);
1237 }
1238}
1239
1240static int btusb_submit_diag_urb(struct hci_dev *hdev, gfp_t mem_flags)
1241{
1242 struct btusb_data *data = hci_get_drvdata(hdev);
1243 struct urb *urb;
1244 unsigned char *buf;
1245 unsigned int pipe;
1246 int err, size = HCI_MAX_FRAME_SIZE;
1247
1248 BT_DBG("%s", hdev->name);
1249
1250 if (!data->diag_rx_ep)
1251 return -ENODEV;
1252
1253 urb = usb_alloc_urb(0, mem_flags);
1254 if (!urb)
1255 return -ENOMEM;
1256
1257 buf = kmalloc(size, mem_flags);
1258 if (!buf) {
1259 usb_free_urb(urb);
1260 return -ENOMEM;
1261 }
1262
1263 pipe = usb_rcvbulkpipe(data->udev, data->diag_rx_ep->bEndpointAddress);
1264
1265 usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
1266 btusb_diag_complete, hdev);
1267
1268 urb->transfer_flags |= URB_FREE_BUFFER;
1269
1270 usb_mark_last_busy(data->udev);
1271 usb_anchor_urb(urb, &data->diag_anchor);
1272
1273 err = usb_submit_urb(urb, mem_flags);
1274 if (err < 0) {
1275 if (err != -EPERM && err != -ENODEV)
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001276 bt_dev_err(hdev, "urb %p submission failed (%d)",
1277 urb, -err);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02001278 usb_unanchor_urb(urb);
1279 }
1280
1281 usb_free_urb(urb);
1282
1283 return err;
1284}
1285
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001286static void btusb_tx_complete(struct urb *urb)
1287{
1288 struct sk_buff *skb = urb->context;
Marcel Holtmann89e75332014-09-16 04:44:50 +02001289 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
David Herrmann155961e2012-02-09 21:58:32 +01001290 struct btusb_data *data = hci_get_drvdata(hdev);
Sebastian Andrzej Siewiora5e50d52018-06-19 23:56:57 +02001291 unsigned long flags;
Oliver Neukum7bee5492009-08-24 23:44:59 +02001292
Marcel Holtmann89e75332014-09-16 04:44:50 +02001293 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1294 urb->actual_length);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001295
1296 if (!test_bit(HCI_RUNNING, &hdev->flags))
1297 goto done;
1298
1299 if (!urb->status)
1300 hdev->stat.byte_tx += urb->transfer_buffer_length;
1301 else
1302 hdev->stat.err_tx++;
1303
1304done:
Sebastian Andrzej Siewiora5e50d52018-06-19 23:56:57 +02001305 spin_lock_irqsave(&data->txlock, flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001306 data->tx_in_flight--;
Sebastian Andrzej Siewiora5e50d52018-06-19 23:56:57 +02001307 spin_unlock_irqrestore(&data->txlock, flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001308
1309 kfree(urb->setup_packet);
1310
1311 kfree_skb(skb);
1312}
1313
1314static void btusb_isoc_tx_complete(struct urb *urb)
1315{
1316 struct sk_buff *skb = urb->context;
Marcel Holtmann89e75332014-09-16 04:44:50 +02001317 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001318
Marcel Holtmann89e75332014-09-16 04:44:50 +02001319 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1320 urb->actual_length);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001321
1322 if (!test_bit(HCI_RUNNING, &hdev->flags))
1323 goto done;
1324
1325 if (!urb->status)
1326 hdev->stat.byte_tx += urb->transfer_buffer_length;
1327 else
1328 hdev->stat.err_tx++;
1329
1330done:
1331 kfree(urb->setup_packet);
1332
1333 kfree_skb(skb);
1334}
1335
1336static int btusb_open(struct hci_dev *hdev)
1337{
David Herrmann155961e2012-02-09 21:58:32 +01001338 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001339 int err;
1340
1341 BT_DBG("%s", hdev->name);
1342
Ethan Hsiehc7e163f2016-10-07 12:06:42 +08001343 err = usb_autopm_get_interface(data->intf);
1344 if (err < 0)
1345 return err;
1346
Kim, Ben Young Taeace31982015-02-15 23:06:14 +00001347 /* Patching USB firmware files prior to starting any URBs of HCI path
1348 * It is more safe to use USB bulk channel for downloading USB patch
1349 */
1350 if (data->setup_on_usb) {
1351 err = data->setup_on_usb(hdev);
Marcel Holtmanneb500422015-04-16 23:15:50 +02001352 if (err < 0)
Oliver Neukum3d44a6f2019-11-14 16:01:18 +01001353 goto setup_fail;
Kim, Ben Young Taeace31982015-02-15 23:06:14 +00001354 }
1355
Oliver Neukum7bee5492009-08-24 23:44:59 +02001356 data->intf->needs_remote_wakeup = 1;
1357
Alex Lu9e455242019-08-14 20:02:52 +08001358 /* Disable device remote wakeup when host is suspended
1359 * For Realtek chips, global suspend without
1360 * SET_FEATURE (DEVICE_REMOTE_WAKEUP) can save more power in device.
1361 */
1362 if (test_bit(BTUSB_WAKEUP_DISABLE, &data->flags))
1363 device_wakeup_disable(&data->udev->dev);
1364
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001365 if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +02001366 goto done;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001367
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001368 err = btusb_submit_intr_urb(hdev, GFP_KERNEL);
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001369 if (err < 0)
1370 goto failed;
1371
1372 err = btusb_submit_bulk_urb(hdev, GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001373 if (err < 0) {
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001374 usb_kill_anchored_urbs(&data->intr_anchor);
1375 goto failed;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001376 }
1377
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001378 set_bit(BTUSB_BULK_RUNNING, &data->flags);
1379 btusb_submit_bulk_urb(hdev, GFP_KERNEL);
1380
Marcel Holtmann9d08f502015-10-08 20:23:08 +02001381 if (data->diag) {
1382 if (!btusb_submit_diag_urb(hdev, GFP_KERNEL))
1383 set_bit(BTUSB_DIAG_RUNNING, &data->flags);
1384 }
1385
Oliver Neukum7bee5492009-08-24 23:44:59 +02001386done:
1387 usb_autopm_put_interface(data->intf);
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001388 return 0;
1389
1390failed:
1391 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Oliver Neukum3d44a6f2019-11-14 16:01:18 +01001392setup_fail:
Oliver Neukum7bee5492009-08-24 23:44:59 +02001393 usb_autopm_put_interface(data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001394 return err;
1395}
1396
Oliver Neukum7bee5492009-08-24 23:44:59 +02001397static void btusb_stop_traffic(struct btusb_data *data)
1398{
1399 usb_kill_anchored_urbs(&data->intr_anchor);
1400 usb_kill_anchored_urbs(&data->bulk_anchor);
1401 usb_kill_anchored_urbs(&data->isoc_anchor);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02001402 usb_kill_anchored_urbs(&data->diag_anchor);
Sean Wanga1c49c432019-06-02 08:02:48 +08001403 usb_kill_anchored_urbs(&data->ctrl_anchor);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001404}
1405
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001406static int btusb_close(struct hci_dev *hdev)
1407{
David Herrmann155961e2012-02-09 21:58:32 +01001408 struct btusb_data *data = hci_get_drvdata(hdev);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001409 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001410
1411 BT_DBG("%s", hdev->name);
1412
Marcel Holtmanne8c3c3d2008-09-23 00:16:36 +02001413 cancel_work_sync(&data->work);
Linus Torvalds404291a2009-11-11 13:32:29 -08001414 cancel_work_sync(&data->waker);
Marcel Holtmanne8c3c3d2008-09-23 00:16:36 +02001415
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001416 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001417 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001418 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02001419 clear_bit(BTUSB_DIAG_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001420
1421 btusb_stop_traffic(data);
Marcel Holtmann803b5832014-09-16 08:00:29 +02001422 btusb_free_frags(data);
1423
Oliver Neukum7bee5492009-08-24 23:44:59 +02001424 err = usb_autopm_get_interface(data->intf);
1425 if (err < 0)
Oliver Neukum7b8e2c12009-11-13 14:26:23 +01001426 goto failed;
Oliver Neukum7bee5492009-08-24 23:44:59 +02001427
1428 data->intf->needs_remote_wakeup = 0;
Alex Lu9e455242019-08-14 20:02:52 +08001429
1430 /* Enable remote wake up for auto-suspend */
1431 if (test_bit(BTUSB_WAKEUP_DISABLE, &data->flags))
1432 data->intf->needs_remote_wakeup = 1;
1433
Oliver Neukum7bee5492009-08-24 23:44:59 +02001434 usb_autopm_put_interface(data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001435
Oliver Neukum7b8e2c12009-11-13 14:26:23 +01001436failed:
1437 usb_scuttle_anchored_urbs(&data->deferred);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001438 return 0;
1439}
1440
1441static int btusb_flush(struct hci_dev *hdev)
1442{
David Herrmann155961e2012-02-09 21:58:32 +01001443 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001444
1445 BT_DBG("%s", hdev->name);
1446
1447 usb_kill_anchored_urbs(&data->tx_anchor);
Marcel Holtmann803b5832014-09-16 08:00:29 +02001448 btusb_free_frags(data);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001449
1450 return 0;
1451}
1452
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001453static struct urb *alloc_ctrl_urb(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001454{
David Herrmann155961e2012-02-09 21:58:32 +01001455 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001456 struct usb_ctrlrequest *dr;
1457 struct urb *urb;
1458 unsigned int pipe;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001459
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001460 urb = usb_alloc_urb(0, GFP_KERNEL);
1461 if (!urb)
1462 return ERR_PTR(-ENOMEM);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001463
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001464 dr = kmalloc(sizeof(*dr), GFP_KERNEL);
1465 if (!dr) {
1466 usb_free_urb(urb);
1467 return ERR_PTR(-ENOMEM);
1468 }
1469
1470 dr->bRequestType = data->cmdreq_type;
Marcel Holtmann893ba542015-01-28 20:27:34 -08001471 dr->bRequest = data->cmdreq;
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001472 dr->wIndex = 0;
1473 dr->wValue = 0;
1474 dr->wLength = __cpu_to_le16(skb->len);
1475
1476 pipe = usb_sndctrlpipe(data->udev, 0x00);
1477
Marcel Holtmann89e75332014-09-16 04:44:50 +02001478 usb_fill_control_urb(urb, data->udev, pipe, (void *)dr,
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001479 skb->data, skb->len, btusb_tx_complete, skb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001480
Marcel Holtmann89e75332014-09-16 04:44:50 +02001481 skb->dev = (void *)hdev;
Marcel Holtmann7bd8f092013-10-11 06:19:18 -07001482
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001483 return urb;
1484}
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001485
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001486static struct urb *alloc_bulk_urb(struct hci_dev *hdev, struct sk_buff *skb)
1487{
1488 struct btusb_data *data = hci_get_drvdata(hdev);
1489 struct urb *urb;
1490 unsigned int pipe;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001491
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001492 if (!data->bulk_tx_ep)
1493 return ERR_PTR(-ENODEV);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001494
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001495 urb = usb_alloc_urb(0, GFP_KERNEL);
1496 if (!urb)
1497 return ERR_PTR(-ENOMEM);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001498
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001499 pipe = usb_sndbulkpipe(data->udev, data->bulk_tx_ep->bEndpointAddress);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001500
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001501 usb_fill_bulk_urb(urb, data->udev, pipe,
1502 skb->data, skb->len, btusb_tx_complete, skb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001503
Marcel Holtmann89e75332014-09-16 04:44:50 +02001504 skb->dev = (void *)hdev;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001505
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001506 return urb;
1507}
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001508
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001509static struct urb *alloc_isoc_urb(struct hci_dev *hdev, struct sk_buff *skb)
1510{
1511 struct btusb_data *data = hci_get_drvdata(hdev);
1512 struct urb *urb;
1513 unsigned int pipe;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001514
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001515 if (!data->isoc_tx_ep)
1516 return ERR_PTR(-ENODEV);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001517
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001518 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_KERNEL);
1519 if (!urb)
1520 return ERR_PTR(-ENOMEM);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001521
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001522 pipe = usb_sndisocpipe(data->udev, data->isoc_tx_ep->bEndpointAddress);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001523
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001524 usb_fill_int_urb(urb, data->udev, pipe,
1525 skb->data, skb->len, btusb_isoc_tx_complete,
1526 skb, data->isoc_tx_ep->bInterval);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001527
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001528 urb->transfer_flags = URB_ISO_ASAP;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001529
Sathish Narasimmanbaac6272020-04-03 21:43:59 +02001530 if (data->isoc_altsetting == 6)
1531 __fill_isoc_descriptor_msbc(urb, skb->len,
1532 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize),
1533 data);
1534 else
1535 __fill_isoc_descriptor(urb, skb->len,
1536 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
Marcel Holtmann89e75332014-09-16 04:44:50 +02001537 skb->dev = (void *)hdev;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001538
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001539 return urb;
1540}
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001541
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001542static int submit_tx_urb(struct hci_dev *hdev, struct urb *urb)
1543{
1544 struct btusb_data *data = hci_get_drvdata(hdev);
1545 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001546
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001547 usb_anchor_urb(urb, &data->tx_anchor);
1548
Johan Hedberge9753ef2014-09-14 08:49:34 +03001549 err = usb_submit_urb(urb, GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001550 if (err < 0) {
Paul Bolle5a9b80e2011-10-09 12:12:16 +02001551 if (err != -EPERM && err != -ENODEV)
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001552 bt_dev_err(hdev, "urb %p submission failed (%d)",
1553 urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001554 kfree(urb->setup_packet);
1555 usb_unanchor_urb(urb);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001556 } else {
1557 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001558 }
1559
Cong Wang54a8a792011-11-22 09:32:57 +08001560 usb_free_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001561 return err;
1562}
1563
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001564static int submit_or_queue_tx_urb(struct hci_dev *hdev, struct urb *urb)
1565{
1566 struct btusb_data *data = hci_get_drvdata(hdev);
1567 unsigned long flags;
1568 bool suspending;
1569
1570 spin_lock_irqsave(&data->txlock, flags);
1571 suspending = test_bit(BTUSB_SUSPENDING, &data->flags);
1572 if (!suspending)
1573 data->tx_in_flight++;
1574 spin_unlock_irqrestore(&data->txlock, flags);
1575
1576 if (!suspending)
1577 return submit_tx_urb(hdev, urb);
1578
1579 usb_anchor_urb(urb, &data->deferred);
1580 schedule_work(&data->waker);
1581
1582 usb_free_urb(urb);
1583 return 0;
1584}
1585
1586static int btusb_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
1587{
1588 struct urb *urb;
1589
1590 BT_DBG("%s", hdev->name);
1591
Marcel Holtmann618e8bc2015-11-05 07:33:56 +01001592 switch (hci_skb_pkt_type(skb)) {
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001593 case HCI_COMMAND_PKT:
1594 urb = alloc_ctrl_urb(hdev, skb);
1595 if (IS_ERR(urb))
1596 return PTR_ERR(urb);
1597
1598 hdev->stat.cmd_tx++;
1599 return submit_or_queue_tx_urb(hdev, urb);
1600
1601 case HCI_ACLDATA_PKT:
1602 urb = alloc_bulk_urb(hdev, skb);
1603 if (IS_ERR(urb))
1604 return PTR_ERR(urb);
1605
1606 hdev->stat.acl_tx++;
1607 return submit_or_queue_tx_urb(hdev, urb);
1608
1609 case HCI_SCODATA_PKT:
1610 if (hci_conn_num(hdev, SCO_LINK) < 1)
1611 return -ENODEV;
1612
1613 urb = alloc_isoc_urb(hdev, skb);
1614 if (IS_ERR(urb))
1615 return PTR_ERR(urb);
1616
1617 hdev->stat.sco_tx++;
1618 return submit_tx_urb(hdev, urb);
1619 }
1620
1621 return -EILSEQ;
1622}
1623
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001624static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
1625{
David Herrmann155961e2012-02-09 21:58:32 +01001626 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001627
1628 BT_DBG("%s evt %d", hdev->name, evt);
1629
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001630 if (hci_conn_num(hdev, SCO_LINK) != data->sco_num) {
1631 data->sco_num = hci_conn_num(hdev, SCO_LINK);
Sathish Narasimmanbaac6272020-04-03 21:43:59 +02001632 data->air_mode = evt;
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001633 schedule_work(&data->work);
Marcel Holtmanna780efa2008-11-30 12:17:12 +01001634 }
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001635}
1636
Jesper Juhl42b16b32011-01-17 00:09:38 +01001637static inline int __set_isoc_interface(struct hci_dev *hdev, int altsetting)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001638{
David Herrmann155961e2012-02-09 21:58:32 +01001639 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001640 struct usb_interface *intf = data->isoc;
1641 struct usb_endpoint_descriptor *ep_desc;
1642 int i, err;
1643
1644 if (!data->isoc)
1645 return -ENODEV;
1646
Marcel Holtmann459232f2017-10-24 19:42:45 +02001647 err = usb_set_interface(data->udev, data->isoc_ifnum, altsetting);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001648 if (err < 0) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001649 bt_dev_err(hdev, "setting interface failed (%d)", -err);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001650 return err;
1651 }
1652
1653 data->isoc_altsetting = altsetting;
1654
1655 data->isoc_tx_ep = NULL;
1656 data->isoc_rx_ep = NULL;
1657
1658 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
1659 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
1660
1661 if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) {
1662 data->isoc_tx_ep = ep_desc;
1663 continue;
1664 }
1665
1666 if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) {
1667 data->isoc_rx_ep = ep_desc;
1668 continue;
1669 }
1670 }
1671
1672 if (!data->isoc_tx_ep || !data->isoc_rx_ep) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001673 bt_dev_err(hdev, "invalid SCO descriptors");
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001674 return -ENODEV;
1675 }
1676
1677 return 0;
1678}
1679
Sathish Narasimmanbaac6272020-04-03 21:43:59 +02001680static int btusb_switch_alt_setting(struct hci_dev *hdev, int new_alts)
1681{
1682 struct btusb_data *data = hci_get_drvdata(hdev);
1683 int err;
1684
1685 if (data->isoc_altsetting != new_alts) {
1686 unsigned long flags;
1687
1688 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1689 usb_kill_anchored_urbs(&data->isoc_anchor);
1690
1691 /* When isochronous alternate setting needs to be
1692 * changed, because SCO connection has been added
1693 * or removed, a packet fragment may be left in the
1694 * reassembling state. This could lead to wrongly
1695 * assembled fragments.
1696 *
1697 * Clear outstanding fragment when selecting a new
1698 * alternate setting.
1699 */
1700 spin_lock_irqsave(&data->rxlock, flags);
1701 kfree_skb(data->sco_skb);
1702 data->sco_skb = NULL;
1703 spin_unlock_irqrestore(&data->rxlock, flags);
1704
1705 err = __set_isoc_interface(hdev, new_alts);
1706 if (err < 0)
1707 return err;
1708 }
1709
1710 if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
1711 if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0)
1712 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1713 else
1714 btusb_submit_isoc_urb(hdev, GFP_KERNEL);
1715 }
1716
1717 return 0;
1718}
1719
1720static struct usb_host_interface *btusb_find_altsetting(struct btusb_data *data,
1721 int alt)
1722{
1723 struct usb_interface *intf = data->isoc;
1724 int i;
1725
1726 BT_DBG("Looking for Alt no :%d", alt);
1727
Sathish Narasimmanfcd156ee2020-04-08 10:57:03 +05301728 if (!intf)
1729 return NULL;
1730
Sathish Narasimmanbaac6272020-04-03 21:43:59 +02001731 for (i = 0; i < intf->num_altsetting; i++) {
1732 if (intf->altsetting[i].desc.bAlternateSetting == alt)
1733 return &intf->altsetting[i];
1734 }
1735
1736 return NULL;
1737}
1738
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001739static void btusb_work(struct work_struct *work)
1740{
1741 struct btusb_data *data = container_of(work, struct btusb_data, work);
1742 struct hci_dev *hdev = data->hdev;
Sathish Narasimmanbaac6272020-04-03 21:43:59 +02001743 int new_alts = 0;
Oliver Neukum7bee5492009-08-24 23:44:59 +02001744 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001745
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001746 if (data->sco_num > 0) {
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -03001747 if (!test_bit(BTUSB_DID_ISO_RESUME, &data->flags)) {
Oliver Neukum8efdd0c2011-02-11 13:00:06 +01001748 err = usb_autopm_get_interface(data->isoc ? data->isoc : data->intf);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001749 if (err < 0) {
1750 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1751 usb_kill_anchored_urbs(&data->isoc_anchor);
1752 return;
1753 }
1754
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -03001755 set_bit(BTUSB_DID_ISO_RESUME, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001756 }
Mikel Astizf4001d22012-04-11 08:48:51 +02001757
Sathish Narasimmanbaac6272020-04-03 21:43:59 +02001758 if (data->air_mode == HCI_NOTIFY_ENABLE_SCO_CVSD) {
1759 if (hdev->voice_setting & 0x0020) {
1760 static const int alts[3] = { 2, 4, 5 };
Marcel Holtmann89e75332014-09-16 04:44:50 +02001761
Sathish Narasimmanbaac6272020-04-03 21:43:59 +02001762 new_alts = alts[data->sco_num - 1];
1763 } else {
1764 new_alts = data->sco_num;
1765 }
1766 } else if (data->air_mode == HCI_NOTIFY_ENABLE_SCO_TRANSP) {
Trent Piepho517b6932020-12-09 17:20:03 -08001767 /* Bluetooth USB spec recommends alt 6 (63 bytes), but
1768 * many adapters do not support it. Alt 1 appears to
1769 * work for all adapters that do not have alt 6, and
1770 * which work with WBS at all.
1771 */
1772 new_alts = btusb_find_altsetting(data, 6) ? 6 : 1;
Hilda Wue848dbd2021-05-14 11:19:01 +08001773 /* Because mSBC frames do not need to be aligned to the
1774 * SCO packet boundary. If support the Alt 3, use the
1775 * Alt 3 for HCI payload >= 60 Bytes let air packet
1776 * data satisfy 60 bytes.
1777 */
1778 if (new_alts == 1 && btusb_find_altsetting(data, 3))
1779 new_alts = 3;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001780 }
Sathish Narasimmanbaac6272020-04-03 21:43:59 +02001781
1782 if (btusb_switch_alt_setting(hdev, new_alts) < 0)
1783 bt_dev_err(hdev, "set USB alt:(%d) failed!", new_alts);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001784 } else {
1785 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1786 usb_kill_anchored_urbs(&data->isoc_anchor);
1787
1788 __set_isoc_interface(hdev, 0);
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -03001789 if (test_and_clear_bit(BTUSB_DID_ISO_RESUME, &data->flags))
Oliver Neukum8efdd0c2011-02-11 13:00:06 +01001790 usb_autopm_put_interface(data->isoc ? data->isoc : data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001791 }
1792}
1793
Oliver Neukum7bee5492009-08-24 23:44:59 +02001794static void btusb_waker(struct work_struct *work)
1795{
1796 struct btusb_data *data = container_of(work, struct btusb_data, waker);
1797 int err;
1798
1799 err = usb_autopm_get_interface(data->intf);
1800 if (err < 0)
1801 return;
1802
1803 usb_autopm_put_interface(data->intf);
1804}
1805
Marcel Holtmann9f8f9622013-04-10 08:11:35 -07001806static int btusb_setup_bcm92035(struct hci_dev *hdev)
1807{
1808 struct sk_buff *skb;
1809 u8 val = 0x00;
1810
1811 BT_DBG("%s", hdev->name);
1812
1813 skb = __hci_cmd_sync(hdev, 0xfc3b, 1, &val, HCI_INIT_TIMEOUT);
1814 if (IS_ERR(skb))
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001815 bt_dev_err(hdev, "BCM92035 command failed (%ld)", PTR_ERR(skb));
Marcel Holtmann9f8f9622013-04-10 08:11:35 -07001816 else
1817 kfree_skb(skb);
1818
1819 return 0;
1820}
1821
Marcel Holtmann81cac642014-01-03 03:02:36 -08001822static int btusb_setup_csr(struct hci_dev *hdev)
1823{
Hans de Goeded74e0ae2020-12-05 16:02:00 +01001824 struct btusb_data *data = hci_get_drvdata(hdev);
1825 u16 bcdDevice = le16_to_cpu(data->udev->descriptor.bcdDevice);
Marcel Holtmann81cac642014-01-03 03:02:36 -08001826 struct hci_rp_read_local_version *rp;
1827 struct sk_buff *skb;
Ismael Ferreras Morezuelascde1a8a2020-07-26 23:12:28 +02001828 bool is_fake = false;
Hans de Goede0671c0662020-12-05 16:02:01 +01001829 int ret;
Marcel Holtmann81cac642014-01-03 03:02:36 -08001830
1831 BT_DBG("%s", hdev->name);
1832
Marcel Holtmann7cd84d72015-06-07 10:01:02 +02001833 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
1834 HCI_INIT_TIMEOUT);
1835 if (IS_ERR(skb)) {
1836 int err = PTR_ERR(skb);
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001837 bt_dev_err(hdev, "CSR: Local version failed (%d)", err);
Marcel Holtmann7cd84d72015-06-07 10:01:02 +02001838 return err;
1839 }
1840
1841 if (skb->len != sizeof(struct hci_rp_read_local_version)) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001842 bt_dev_err(hdev, "CSR: Local version length mismatch");
Marcel Holtmann7cd84d72015-06-07 10:01:02 +02001843 kfree_skb(skb);
1844 return -EIO;
1845 }
Marcel Holtmann81cac642014-01-03 03:02:36 -08001846
Marcel Holtmann89e75332014-09-16 04:44:50 +02001847 rp = (struct hci_rp_read_local_version *)skb->data;
Marcel Holtmann81cac642014-01-03 03:02:36 -08001848
Ismael Ferreras Morezuelascde1a8a2020-07-26 23:12:28 +02001849 /* Detect a wide host of Chinese controllers that aren't CSR.
1850 *
1851 * Known fake bcdDevices: 0x0100, 0x0134, 0x1915, 0x2520, 0x7558, 0x8891
1852 *
1853 * The main thing they have in common is that these are really popular low-cost
1854 * options that support newer Bluetooth versions but rely on heavy VID/PID
1855 * squatting of this poor old Bluetooth 1.1 device. Even sold as such.
1856 *
1857 * We detect actual CSR devices by checking that the HCI manufacturer code
1858 * is Cambridge Silicon Radio (10) and ensuring that LMP sub-version and
1859 * HCI rev values always match. As they both store the firmware number.
1860 */
Johan Hedberg6cafcd92015-08-30 21:47:21 +03001861 if (le16_to_cpu(rp->manufacturer) != 10 ||
Ismael Ferreras Morezuelascde1a8a2020-07-26 23:12:28 +02001862 le16_to_cpu(rp->hci_rev) != le16_to_cpu(rp->lmp_subver))
1863 is_fake = true;
1864
1865 /* Known legit CSR firmware build numbers and their supported BT versions:
1866 * - 1.1 (0x1) -> 0x0073, 0x020d, 0x033c, 0x034e
1867 * - 1.2 (0x2) -> 0x04d9, 0x0529
1868 * - 2.0 (0x3) -> 0x07a6, 0x07ad, 0x0c5c
1869 * - 2.1 (0x4) -> 0x149c, 0x1735, 0x1899 (0x1899 is a BlueCore4-External)
1870 * - 4.0 (0x6) -> 0x1d86, 0x2031, 0x22bb
1871 *
1872 * e.g. Real CSR dongles with LMP subversion 0x73 are old enough that
1873 * support BT 1.1 only; so it's a dead giveaway when some
1874 * third-party BT 4.0 dongle reuses it.
1875 */
1876 else if (le16_to_cpu(rp->lmp_subver) <= 0x034e &&
1877 le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_1_1)
1878 is_fake = true;
1879
1880 else if (le16_to_cpu(rp->lmp_subver) <= 0x0529 &&
1881 le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_1_2)
1882 is_fake = true;
1883
1884 else if (le16_to_cpu(rp->lmp_subver) <= 0x0c5c &&
1885 le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_2_0)
1886 is_fake = true;
1887
1888 else if (le16_to_cpu(rp->lmp_subver) <= 0x1899 &&
1889 le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_2_1)
1890 is_fake = true;
1891
1892 else if (le16_to_cpu(rp->lmp_subver) <= 0x22bb &&
1893 le16_to_cpu(rp->hci_ver) > BLUETOOTH_VER_4_0)
1894 is_fake = true;
1895
Hans de Goeded74e0ae2020-12-05 16:02:00 +01001896 /* Other clones which beat all the above checks */
1897 else if (bcdDevice == 0x0134 &&
1898 le16_to_cpu(rp->lmp_subver) == 0x0c5c &&
1899 le16_to_cpu(rp->hci_ver) == BLUETOOTH_VER_2_0)
1900 is_fake = true;
1901
Ismael Ferreras Morezuelascde1a8a2020-07-26 23:12:28 +02001902 if (is_fake) {
Ismael Ferreras Morezuelasf4292e22021-07-17 01:21:43 +02001903 bt_dev_warn(hdev, "CSR: Unbranded CSR clone detected; adding workarounds and force-suspending once...");
Ismael Ferreras Morezuelascde1a8a2020-07-26 23:12:28 +02001904
1905 /* Generally these clones have big discrepancies between
1906 * advertised features and what's actually supported.
1907 * Probably will need to be expanded in the future;
1908 * without these the controller will lock up.
1909 */
1910 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
1911 set_bit(HCI_QUIRK_BROKEN_ERR_DATA_REPORTING, &hdev->quirks);
1912
Marcel Holtmann9641d342015-06-07 10:01:01 +02001913 /* Clear the reset quirk since this is not an actual
1914 * early Bluetooth 1.1 device from CSR.
1915 */
1916 clear_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Ismael Ferreras Morezuelascde1a8a2020-07-26 23:12:28 +02001917 clear_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
Hans de Goede0671c0662020-12-05 16:02:01 +01001918
1919 /*
Ismael Ferreras Morezuelasf4292e22021-07-17 01:21:43 +02001920 * Special workaround for these BT 4.0 chip clones, and potentially more:
1921 *
1922 * - 0x0134: a Barrot 8041a02 (HCI rev: 0x1012 sub: 0x0810)
1923 * - 0x7558: IC markings FR3191AHAL 749H15143 (HCI rev/sub-version: 0x0709)
1924 *
1925 * These controllers are really messed-up.
1926 *
1927 * 1. Their bulk RX endpoint will never report any data unless
1928 * the device was suspended at least once (yes, really).
Hans de Goede0671c0662020-12-05 16:02:01 +01001929 * 2. They will not wakeup when autosuspended and receiving data
Ismael Ferreras Morezuelasf4292e22021-07-17 01:21:43 +02001930 * on their bulk RX endpoint from e.g. a keyboard or mouse
Hans de Goede0671c0662020-12-05 16:02:01 +01001931 * (IOW remote-wakeup support is broken for the bulk endpoint).
1932 *
1933 * To fix 1. enable runtime-suspend, force-suspend the
Ismael Ferreras Morezuelasf4292e22021-07-17 01:21:43 +02001934 * HCI and then wake-it up by disabling runtime-suspend.
Hans de Goede0671c0662020-12-05 16:02:01 +01001935 *
Ismael Ferreras Morezuelasf4292e22021-07-17 01:21:43 +02001936 * To fix 2. clear the HCI's can_wake flag, this way the HCI
Hans de Goede0671c0662020-12-05 16:02:01 +01001937 * will still be autosuspended when it is not open.
Ismael Ferreras Morezuelasf4292e22021-07-17 01:21:43 +02001938 *
1939 * --
1940 *
1941 * Because these are widespread problems we prefer generic solutions; so
1942 * apply this initialization quirk to every controller that gets here,
1943 * it should be harmless. The alternative is to not work at all.
Hans de Goede0671c0662020-12-05 16:02:01 +01001944 */
Ismael Ferreras Morezuelasf4292e22021-07-17 01:21:43 +02001945 pm_runtime_allow(&data->udev->dev);
Hans de Goede0671c0662020-12-05 16:02:01 +01001946
Ismael Ferreras Morezuelasf4292e22021-07-17 01:21:43 +02001947 ret = pm_runtime_suspend(&data->udev->dev);
1948 if (ret >= 0)
1949 msleep(200);
1950 else
1951 bt_dev_err(hdev, "CSR: Failed to suspend the device for our Barrot 8041a02 receive-issue workaround");
Hans de Goede0671c0662020-12-05 16:02:01 +01001952
Ismael Ferreras Morezuelasf4292e22021-07-17 01:21:43 +02001953 pm_runtime_forbid(&data->udev->dev);
Hans de Goede0671c0662020-12-05 16:02:01 +01001954
Ismael Ferreras Morezuelasf4292e22021-07-17 01:21:43 +02001955 device_set_wakeup_capable(&data->udev->dev, false);
Hans de Goede0671c0662020-12-05 16:02:01 +01001956
Ismael Ferreras Morezuelasf4292e22021-07-17 01:21:43 +02001957 /* Re-enable autosuspend if this was requested */
1958 if (enable_autosuspend)
1959 usb_enable_autosuspend(data->udev);
Marcel Holtmann81cac642014-01-03 03:02:36 -08001960 }
1961
Marcel Holtmann81cac642014-01-03 03:02:36 -08001962 kfree_skb(skb);
1963
Marcel Holtmann9641d342015-06-07 10:01:01 +02001964 return 0;
Marcel Holtmann81cac642014-01-03 03:02:36 -08001965}
1966
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001967static const struct firmware *btusb_setup_intel_get_fw(struct hci_dev *hdev,
Marcel Holtmann89e75332014-09-16 04:44:50 +02001968 struct intel_version *ver)
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001969{
1970 const struct firmware *fw;
1971 char fwname[64];
1972 int ret;
1973
1974 snprintf(fwname, sizeof(fwname),
1975 "intel/ibt-hw-%x.%x.%x-fw-%x.%x.%x.%x.%x.bseq",
1976 ver->hw_platform, ver->hw_variant, ver->hw_revision,
1977 ver->fw_variant, ver->fw_revision, ver->fw_build_num,
1978 ver->fw_build_ww, ver->fw_build_yy);
1979
1980 ret = request_firmware(&fw, fwname, &hdev->dev);
1981 if (ret < 0) {
1982 if (ret == -EINVAL) {
Marcel Holtmann85418fe2018-08-03 11:20:49 +02001983 bt_dev_err(hdev, "Intel firmware file request failed (%d)",
1984 ret);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001985 return NULL;
1986 }
1987
Marcel Holtmann85418fe2018-08-03 11:20:49 +02001988 bt_dev_err(hdev, "failed to open Intel firmware file: %s (%d)",
1989 fwname, ret);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001990
1991 /* If the correct firmware patch file is not found, use the
1992 * default firmware patch file instead
1993 */
1994 snprintf(fwname, sizeof(fwname), "intel/ibt-hw-%x.%x.bseq",
1995 ver->hw_platform, ver->hw_variant);
1996 if (request_firmware(&fw, fwname, &hdev->dev) < 0) {
Marcel Holtmann85418fe2018-08-03 11:20:49 +02001997 bt_dev_err(hdev, "failed to open default fw file: %s",
1998 fwname);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001999 return NULL;
2000 }
2001 }
2002
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002003 bt_dev_info(hdev, "Intel Bluetooth firmware file: %s", fwname);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002004
2005 return fw;
2006}
2007
2008static int btusb_setup_intel_patching(struct hci_dev *hdev,
2009 const struct firmware *fw,
2010 const u8 **fw_ptr, int *disable_patch)
2011{
2012 struct sk_buff *skb;
2013 struct hci_command_hdr *cmd;
2014 const u8 *cmd_param;
2015 struct hci_event_hdr *evt = NULL;
2016 const u8 *evt_param = NULL;
2017 int remain = fw->size - (*fw_ptr - fw->data);
2018
2019 /* The first byte indicates the types of the patch command or event.
2020 * 0x01 means HCI command and 0x02 is HCI event. If the first bytes
2021 * in the current firmware buffer doesn't start with 0x01 or
2022 * the size of remain buffer is smaller than HCI command header,
2023 * the firmware file is corrupted and it should stop the patching
2024 * process.
2025 */
2026 if (remain > HCI_COMMAND_HDR_SIZE && *fw_ptr[0] != 0x01) {
Marcel Holtmann85418fe2018-08-03 11:20:49 +02002027 bt_dev_err(hdev, "Intel fw corrupted: invalid cmd read");
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002028 return -EINVAL;
2029 }
2030 (*fw_ptr)++;
2031 remain--;
2032
2033 cmd = (struct hci_command_hdr *)(*fw_ptr);
2034 *fw_ptr += sizeof(*cmd);
2035 remain -= sizeof(*cmd);
2036
2037 /* Ensure that the remain firmware data is long enough than the length
2038 * of command parameter. If not, the firmware file is corrupted.
2039 */
2040 if (remain < cmd->plen) {
Marcel Holtmann85418fe2018-08-03 11:20:49 +02002041 bt_dev_err(hdev, "Intel fw corrupted: invalid cmd len");
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002042 return -EFAULT;
2043 }
2044
2045 /* If there is a command that loads a patch in the firmware
2046 * file, then enable the patch upon success, otherwise just
2047 * disable the manufacturer mode, for example patch activation
2048 * is not required when the default firmware patch file is used
2049 * because there are no patch data to load.
2050 */
2051 if (*disable_patch && le16_to_cpu(cmd->opcode) == 0xfc8e)
2052 *disable_patch = 0;
2053
2054 cmd_param = *fw_ptr;
2055 *fw_ptr += cmd->plen;
2056 remain -= cmd->plen;
2057
2058 /* This reads the expected events when the above command is sent to the
2059 * device. Some vendor commands expects more than one events, for
2060 * example command status event followed by vendor specific event.
2061 * For this case, it only keeps the last expected event. so the command
2062 * can be sent with __hci_cmd_sync_ev() which returns the sk_buff of
2063 * last expected event.
2064 */
2065 while (remain > HCI_EVENT_HDR_SIZE && *fw_ptr[0] == 0x02) {
2066 (*fw_ptr)++;
2067 remain--;
2068
2069 evt = (struct hci_event_hdr *)(*fw_ptr);
2070 *fw_ptr += sizeof(*evt);
2071 remain -= sizeof(*evt);
2072
2073 if (remain < evt->plen) {
Marcel Holtmann85418fe2018-08-03 11:20:49 +02002074 bt_dev_err(hdev, "Intel fw corrupted: invalid evt len");
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002075 return -EFAULT;
2076 }
2077
2078 evt_param = *fw_ptr;
2079 *fw_ptr += evt->plen;
2080 remain -= evt->plen;
2081 }
2082
2083 /* Every HCI commands in the firmware file has its correspond event.
2084 * If event is not found or remain is smaller than zero, the firmware
2085 * file is corrupted.
2086 */
2087 if (!evt || !evt_param || remain < 0) {
Marcel Holtmann85418fe2018-08-03 11:20:49 +02002088 bt_dev_err(hdev, "Intel fw corrupted: invalid evt read");
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002089 return -EFAULT;
2090 }
2091
2092 skb = __hci_cmd_sync_ev(hdev, le16_to_cpu(cmd->opcode), cmd->plen,
2093 cmd_param, evt->evt, HCI_INIT_TIMEOUT);
2094 if (IS_ERR(skb)) {
Marcel Holtmann85418fe2018-08-03 11:20:49 +02002095 bt_dev_err(hdev, "sending Intel patch command (0x%4.4x) failed (%ld)",
2096 cmd->opcode, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08002097 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002098 }
2099
2100 /* It ensures that the returned event matches the event data read from
2101 * the firmware file. At fist, it checks the length and then
2102 * the contents of the event.
2103 */
2104 if (skb->len != evt->plen) {
Marcel Holtmann85418fe2018-08-03 11:20:49 +02002105 bt_dev_err(hdev, "mismatch event length (opcode 0x%4.4x)",
2106 le16_to_cpu(cmd->opcode));
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002107 kfree_skb(skb);
2108 return -EFAULT;
2109 }
2110
2111 if (memcmp(skb->data, evt_param, evt->plen)) {
Marcel Holtmann85418fe2018-08-03 11:20:49 +02002112 bt_dev_err(hdev, "mismatch event parameter (opcode 0x%4.4x)",
2113 le16_to_cpu(cmd->opcode));
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002114 kfree_skb(skb);
2115 return -EFAULT;
2116 }
2117 kfree_skb(skb);
2118
2119 return 0;
2120}
2121
2122static int btusb_setup_intel(struct hci_dev *hdev)
2123{
2124 struct sk_buff *skb;
2125 const struct firmware *fw;
2126 const u8 *fw_ptr;
Loic Poulain28dc4b92015-12-03 16:10:22 +01002127 int disable_patch, err;
Loic Poulain6c483de2015-12-06 16:18:34 +01002128 struct intel_version ver;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002129
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002130 BT_DBG("%s", hdev->name);
2131
2132 /* The controller has a bug with the first HCI command sent to it
2133 * returning number of completed commands as zero. This would stall the
2134 * command processing in the Bluetooth core.
2135 *
2136 * As a workaround, send HCI Reset command first which will reset the
2137 * number of completed commands and allow normal command processing
2138 * from now on.
2139 */
2140 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
2141 if (IS_ERR(skb)) {
Marcel Holtmann85418fe2018-08-03 11:20:49 +02002142 bt_dev_err(hdev, "sending initial HCI reset command failed (%ld)",
2143 PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08002144 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002145 }
2146 kfree_skb(skb);
2147
2148 /* Read Intel specific controller version first to allow selection of
2149 * which firmware file to load.
2150 *
2151 * The returned information are hardware variant and revision plus
2152 * firmware variant, revision and build number.
2153 */
Loic Poulain6c483de2015-12-06 16:18:34 +01002154 err = btintel_read_version(hdev, &ver);
2155 if (err)
2156 return err;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002157
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002158 bt_dev_info(hdev, "read Intel version: %02x%02x%02x%02x%02x%02x%02x%02x%02x",
2159 ver.hw_platform, ver.hw_variant, ver.hw_revision,
2160 ver.fw_variant, ver.fw_revision, ver.fw_build_num,
2161 ver.fw_build_ww, ver.fw_build_yy, ver.fw_patch_num);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002162
2163 /* fw_patch_num indicates the version of patch the device currently
2164 * have. If there is no patch data in the device, it is always 0x00.
Minjune Kim5075eda2015-08-27 13:21:52 +09002165 * So, if it is other than 0x00, no need to patch the device again.
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002166 */
Loic Poulain6c483de2015-12-06 16:18:34 +01002167 if (ver.fw_patch_num) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002168 bt_dev_info(hdev, "Intel device is already patched. "
2169 "patch num: %02x", ver.fw_patch_num);
Marcel Holtmann213445b2015-10-21 02:45:19 +02002170 goto complete;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002171 }
2172
2173 /* Opens the firmware patch file based on the firmware version read
2174 * from the controller. If it fails to open the matching firmware
2175 * patch file, it tries to open the default firmware patch file.
2176 * If no patch file is found, allow the device to operate without
2177 * a patch.
2178 */
Loic Poulain6c483de2015-12-06 16:18:34 +01002179 fw = btusb_setup_intel_get_fw(hdev, &ver);
2180 if (!fw)
Marcel Holtmann213445b2015-10-21 02:45:19 +02002181 goto complete;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002182 fw_ptr = fw->data;
2183
Loic Poulain28dc4b92015-12-03 16:10:22 +01002184 /* Enable the manufacturer mode of the controller.
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002185 * Only while this mode is enabled, the driver can download the
2186 * firmware patch data and configuration parameters.
2187 */
Loic Poulain28dc4b92015-12-03 16:10:22 +01002188 err = btintel_enter_mfg(hdev);
2189 if (err) {
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002190 release_firmware(fw);
Loic Poulain28dc4b92015-12-03 16:10:22 +01002191 return err;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002192 }
2193
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002194 disable_patch = 1;
2195
2196 /* The firmware data file consists of list of Intel specific HCI
2197 * commands and its expected events. The first byte indicates the
2198 * type of the message, either HCI command or HCI event.
2199 *
2200 * It reads the command and its expected event from the firmware file,
2201 * and send to the controller. Once __hci_cmd_sync_ev() returns,
2202 * the returned event is compared with the event read from the firmware
2203 * file and it will continue until all the messages are downloaded to
2204 * the controller.
2205 *
2206 * Once the firmware patching is completed successfully,
2207 * the manufacturer mode is disabled with reset and activating the
2208 * downloaded patch.
2209 *
2210 * If the firmware patching fails, the manufacturer mode is
2211 * disabled with reset and deactivating the patch.
2212 *
2213 * If the default patch file is used, no reset is done when disabling
2214 * the manufacturer.
2215 */
2216 while (fw->size > fw_ptr - fw->data) {
2217 int ret;
2218
2219 ret = btusb_setup_intel_patching(hdev, fw, &fw_ptr,
2220 &disable_patch);
2221 if (ret < 0)
2222 goto exit_mfg_deactivate;
2223 }
2224
2225 release_firmware(fw);
2226
2227 if (disable_patch)
2228 goto exit_mfg_disable;
2229
2230 /* Patching completed successfully and disable the manufacturer mode
2231 * with reset and activate the downloaded firmware patches.
2232 */
Loic Poulain28dc4b92015-12-03 16:10:22 +01002233 err = btintel_exit_mfg(hdev, true, true);
2234 if (err)
2235 return err;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002236
Sukumar Ghorai905d7b12020-03-16 11:37:18 +05302237 /* Need build number for downloaded fw patches in
2238 * every power-on boot
2239 */
2240 err = btintel_read_version(hdev, &ver);
2241 if (err)
2242 return err;
2243 bt_dev_info(hdev, "Intel BT fw patch 0x%02x completed & activated",
2244 ver.fw_patch_num);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002245
Marcel Holtmann213445b2015-10-21 02:45:19 +02002246 goto complete;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002247
2248exit_mfg_disable:
2249 /* Disable the manufacturer mode without reset */
Loic Poulain28dc4b92015-12-03 16:10:22 +01002250 err = btintel_exit_mfg(hdev, false, false);
2251 if (err)
2252 return err;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002253
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002254 bt_dev_info(hdev, "Intel firmware patch completed");
Marcel Holtmann40cb0982014-07-02 12:06:45 +02002255
Marcel Holtmann213445b2015-10-21 02:45:19 +02002256 goto complete;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002257
2258exit_mfg_deactivate:
2259 release_firmware(fw);
2260
2261 /* Patching failed. Disable the manufacturer mode with reset and
2262 * deactivate the downloaded firmware patches.
2263 */
Loic Poulain28dc4b92015-12-03 16:10:22 +01002264 err = btintel_exit_mfg(hdev, true, false);
2265 if (err)
2266 return err;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002267
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002268 bt_dev_info(hdev, "Intel firmware patch completed and deactivated");
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002269
Marcel Holtmann213445b2015-10-21 02:45:19 +02002270complete:
2271 /* Set the event mask for Intel specific vendor events. This enables
2272 * a few extra events that are useful during general operation.
2273 */
2274 btintel_set_event_mask_mfg(hdev, false);
2275
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07002276 btintel_check_bdaddr(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002277 return 0;
2278}
2279
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002280static int inject_cmd_complete(struct hci_dev *hdev, __u16 opcode)
2281{
2282 struct sk_buff *skb;
2283 struct hci_event_hdr *hdr;
2284 struct hci_ev_cmd_complete *evt;
2285
Jia-Ju Baicf07e342018-07-23 11:38:51 +08002286 skb = bt_skb_alloc(sizeof(*hdr) + sizeof(*evt) + 1, GFP_KERNEL);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002287 if (!skb)
2288 return -ENOMEM;
2289
Johannes Berg4df864c2017-06-16 14:29:21 +02002290 hdr = skb_put(skb, sizeof(*hdr));
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002291 hdr->evt = HCI_EV_CMD_COMPLETE;
2292 hdr->plen = sizeof(*evt) + 1;
2293
Johannes Berg4df864c2017-06-16 14:29:21 +02002294 evt = skb_put(skb, sizeof(*evt));
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002295 evt->ncmd = 0x01;
2296 evt->opcode = cpu_to_le16(opcode);
2297
Johannes Berg634fef62017-06-16 14:29:24 +02002298 skb_put_u8(skb, 0x00);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002299
Marcel Holtmann618e8bc2015-11-05 07:33:56 +01002300 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002301
2302 return hci_recv_frame(hdev, skb);
2303}
2304
2305static int btusb_recv_bulk_intel(struct btusb_data *data, void *buffer,
2306 int count)
2307{
2308 /* When the device is in bootloader mode, then it can send
2309 * events via the bulk endpoint. These events are treated the
2310 * same way as the ones received from the interrupt endpoint.
2311 */
2312 if (test_bit(BTUSB_BOOTLOADER, &data->flags))
2313 return btusb_recv_intr(data, buffer, count);
2314
2315 return btusb_recv_bulk(data, buffer, count);
2316}
2317
Marcel Holtmannccd6da22015-04-09 00:35:19 -07002318static void btusb_intel_bootup(struct btusb_data *data, const void *ptr,
2319 unsigned int len)
2320{
2321 const struct intel_bootup *evt = ptr;
2322
2323 if (len != sizeof(*evt))
2324 return;
2325
Andrea Parridff6d592018-11-27 12:22:25 +01002326 if (test_and_clear_bit(BTUSB_BOOTING, &data->flags))
Marcel Holtmannccd6da22015-04-09 00:35:19 -07002327 wake_up_bit(&data->flags, BTUSB_BOOTING);
Marcel Holtmannccd6da22015-04-09 00:35:19 -07002328}
2329
2330static void btusb_intel_secure_send_result(struct btusb_data *data,
2331 const void *ptr, unsigned int len)
2332{
2333 const struct intel_secure_send_result *evt = ptr;
2334
2335 if (len != sizeof(*evt))
2336 return;
2337
2338 if (evt->result)
2339 set_bit(BTUSB_FIRMWARE_FAILED, &data->flags);
2340
2341 if (test_and_clear_bit(BTUSB_DOWNLOADING, &data->flags) &&
Andrea Parridff6d592018-11-27 12:22:25 +01002342 test_bit(BTUSB_FIRMWARE_LOADED, &data->flags))
Marcel Holtmannccd6da22015-04-09 00:35:19 -07002343 wake_up_bit(&data->flags, BTUSB_DOWNLOADING);
Marcel Holtmannccd6da22015-04-09 00:35:19 -07002344}
2345
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002346static int btusb_recv_event_intel(struct hci_dev *hdev, struct sk_buff *skb)
2347{
2348 struct btusb_data *data = hci_get_drvdata(hdev);
2349
2350 if (test_bit(BTUSB_BOOTLOADER, &data->flags)) {
2351 struct hci_event_hdr *hdr = (void *)skb->data;
2352
Marcel Holtmannccd6da22015-04-09 00:35:19 -07002353 if (skb->len > HCI_EVENT_HDR_SIZE && hdr->evt == 0xff &&
2354 hdr->plen > 0) {
2355 const void *ptr = skb->data + HCI_EVENT_HDR_SIZE + 1;
2356 unsigned int len = skb->len - HCI_EVENT_HDR_SIZE - 1;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002357
Marcel Holtmannccd6da22015-04-09 00:35:19 -07002358 switch (skb->data[2]) {
2359 case 0x02:
2360 /* When switching to the operational firmware
2361 * the device sends a vendor specific event
2362 * indicating that the bootup completed.
2363 */
2364 btusb_intel_bootup(data, ptr, len);
2365 break;
2366 case 0x06:
2367 /* When the firmware loading completes the
2368 * device sends out a vendor specific event
2369 * indicating the result of the firmware
2370 * loading.
2371 */
2372 btusb_intel_secure_send_result(data, ptr, len);
2373 break;
Johan Hedbergfad70972015-01-30 10:58:55 +02002374 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002375 }
2376 }
2377
2378 return hci_recv_frame(hdev, skb);
2379}
2380
2381static int btusb_send_frame_intel(struct hci_dev *hdev, struct sk_buff *skb)
2382{
2383 struct btusb_data *data = hci_get_drvdata(hdev);
2384 struct urb *urb;
2385
2386 BT_DBG("%s", hdev->name);
2387
Marcel Holtmann618e8bc2015-11-05 07:33:56 +01002388 switch (hci_skb_pkt_type(skb)) {
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002389 case HCI_COMMAND_PKT:
2390 if (test_bit(BTUSB_BOOTLOADER, &data->flags)) {
2391 struct hci_command_hdr *cmd = (void *)skb->data;
2392 __u16 opcode = le16_to_cpu(cmd->opcode);
2393
2394 /* When in bootloader mode and the command 0xfc09
2395 * is received, it needs to be send down the
2396 * bulk endpoint. So allocate a bulk URB instead.
2397 */
2398 if (opcode == 0xfc09)
2399 urb = alloc_bulk_urb(hdev, skb);
2400 else
2401 urb = alloc_ctrl_urb(hdev, skb);
2402
2403 /* When the 0xfc01 command is issued to boot into
2404 * the operational firmware, it will actually not
2405 * send a command complete event. To keep the flow
2406 * control working inject that event here.
2407 */
2408 if (opcode == 0xfc01)
2409 inject_cmd_complete(hdev, opcode);
2410 } else {
2411 urb = alloc_ctrl_urb(hdev, skb);
2412 }
2413 if (IS_ERR(urb))
2414 return PTR_ERR(urb);
2415
2416 hdev->stat.cmd_tx++;
2417 return submit_or_queue_tx_urb(hdev, urb);
2418
2419 case HCI_ACLDATA_PKT:
2420 urb = alloc_bulk_urb(hdev, skb);
2421 if (IS_ERR(urb))
2422 return PTR_ERR(urb);
2423
2424 hdev->stat.acl_tx++;
2425 return submit_or_queue_tx_urb(hdev, urb);
2426
2427 case HCI_SCODATA_PKT:
2428 if (hci_conn_num(hdev, SCO_LINK) < 1)
2429 return -ENODEV;
2430
2431 urb = alloc_isoc_urb(hdev, skb);
2432 if (IS_ERR(urb))
2433 return PTR_ERR(urb);
2434
2435 hdev->stat.sco_tx++;
2436 return submit_tx_urb(hdev, urb);
2437 }
2438
2439 return -EILSEQ;
2440}
2441
Luiz Augusto von Dentz9b16bfb2021-03-23 11:58:57 -07002442static int btusb_setup_intel_new_get_fw_name(struct intel_version *ver,
Raghuram Hegde2da711bc2018-12-19 11:42:18 +05302443 struct intel_boot_params *params,
2444 char *fw_name, size_t len,
2445 const char *suffix)
2446{
2447 switch (ver->hw_variant) {
2448 case 0x0b: /* SfP */
2449 case 0x0c: /* WsP */
2450 snprintf(fw_name, len, "intel/ibt-%u-%u.%s",
2451 le16_to_cpu(ver->hw_variant),
2452 le16_to_cpu(params->dev_revid),
2453 suffix);
2454 break;
2455 case 0x11: /* JfP */
2456 case 0x12: /* ThP */
2457 case 0x13: /* HrP */
2458 case 0x14: /* CcP */
2459 snprintf(fw_name, len, "intel/ibt-%u-%u-%u.%s",
2460 le16_to_cpu(ver->hw_variant),
2461 le16_to_cpu(ver->hw_revision),
2462 le16_to_cpu(ver->fw_revision),
2463 suffix);
2464 break;
2465 default:
Luiz Augusto von Dentz9b16bfb2021-03-23 11:58:57 -07002466 return -EINVAL;
Raghuram Hegde2da711bc2018-12-19 11:42:18 +05302467 }
Luiz Augusto von Dentz9b16bfb2021-03-23 11:58:57 -07002468
2469 return 0;
Raghuram Hegde2da711bc2018-12-19 11:42:18 +05302470}
2471
Kiran K9a93b8b2020-11-19 17:09:41 +05302472static void btusb_setup_intel_newgen_get_fw_name(const struct intel_version_tlv *ver_tlv,
2473 char *fw_name, size_t len,
2474 const char *suffix)
2475{
2476 /* The firmware file name for new generation controllers will be
2477 * ibt-<cnvi_top type+cnvi_top step>-<cnvr_top type+cnvr_top step>
2478 */
2479 snprintf(fw_name, len, "intel/ibt-%04x-%04x.%s",
2480 INTEL_CNVX_TOP_PACK_SWAB(INTEL_CNVX_TOP_TYPE(ver_tlv->cnvi_top),
2481 INTEL_CNVX_TOP_STEP(ver_tlv->cnvi_top)),
2482 INTEL_CNVX_TOP_PACK_SWAB(INTEL_CNVX_TOP_TYPE(ver_tlv->cnvr_top),
2483 INTEL_CNVX_TOP_STEP(ver_tlv->cnvr_top)),
2484 suffix);
2485}
2486
Luiz Augusto von Dentz21e31c62021-03-23 11:59:00 -07002487static int btusb_download_wait(struct hci_dev *hdev, ktime_t calltime, int msec)
2488{
2489 struct btusb_data *data = hci_get_drvdata(hdev);
2490 ktime_t delta, rettime;
2491 unsigned long long duration;
2492 int err;
2493
2494 set_bit(BTUSB_FIRMWARE_LOADED, &data->flags);
2495
2496 bt_dev_info(hdev, "Waiting for firmware download to complete");
2497
2498 err = wait_on_bit_timeout(&data->flags, BTUSB_DOWNLOADING,
2499 TASK_INTERRUPTIBLE,
2500 msecs_to_jiffies(msec));
2501 if (err == -EINTR) {
2502 bt_dev_err(hdev, "Firmware loading interrupted");
2503 return err;
2504 }
2505
2506 if (err) {
2507 bt_dev_err(hdev, "Firmware loading timeout");
2508 return -ETIMEDOUT;
2509 }
2510
2511 if (test_bit(BTUSB_FIRMWARE_FAILED, &data->flags)) {
2512 bt_dev_err(hdev, "Firmware loading failed");
2513 return -ENOEXEC;
2514 }
2515
2516 rettime = ktime_get();
2517 delta = ktime_sub(rettime, calltime);
2518 duration = (unsigned long long)ktime_to_ns(delta) >> 10;
2519
2520 bt_dev_info(hdev, "Firmware loaded in %llu usecs", duration);
2521
2522 return 0;
2523}
2524
Kiran K3f43a372020-11-19 17:09:42 +05302525static int btusb_intel_download_firmware_newgen(struct hci_dev *hdev,
2526 struct intel_version_tlv *ver,
2527 u32 *boot_param)
2528{
2529 const struct firmware *fw;
2530 char fwname[64];
2531 int err;
2532 struct btusb_data *data = hci_get_drvdata(hdev);
Luiz Augusto von Dentz21e31c62021-03-23 11:59:00 -07002533 ktime_t calltime;
Kiran K3f43a372020-11-19 17:09:42 +05302534
2535 if (!ver || !boot_param)
2536 return -EINVAL;
2537
Kiran K3f43a372020-11-19 17:09:42 +05302538 /* The firmware variant determines if the device is in bootloader
2539 * mode or is running operational firmware. The value 0x03 identifies
2540 * the bootloader and the value 0x23 identifies the operational
2541 * firmware.
2542 *
2543 * When the operational firmware is already present, then only
2544 * the check for valid Bluetooth device address is needed. This
2545 * determines if the device will be added as configured or
2546 * unconfigured controller.
2547 *
2548 * It is not possible to use the Secure Boot Parameters in this
2549 * case since that command is only available in bootloader mode.
2550 */
2551 if (ver->img_type == 0x03) {
2552 clear_bit(BTUSB_BOOTLOADER, &data->flags);
2553 btintel_check_bdaddr(hdev);
Kiran K3f43a372020-11-19 17:09:42 +05302554 }
2555
Kiran K3f43a372020-11-19 17:09:42 +05302556 /* If the OTP has no valid Bluetooth device address, then there will
2557 * also be no valid address for the operational firmware.
2558 */
2559 if (!bacmp(&ver->otp_bd_addr, BDADDR_ANY)) {
2560 bt_dev_info(hdev, "No device address configured");
2561 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
2562 }
2563
2564 btusb_setup_intel_newgen_get_fw_name(ver, fwname, sizeof(fwname), "sfi");
Luiz Augusto von Dentz1f14a622021-04-30 16:05:01 -07002565 err = firmware_request_nowarn(&fw, fwname, &hdev->dev);
Kiran K3f43a372020-11-19 17:09:42 +05302566 if (err < 0) {
Luiz Augusto von Dentz1f14a622021-04-30 16:05:01 -07002567 if (!test_bit(BTUSB_BOOTLOADER, &data->flags)) {
2568 /* Firmware has already been loaded */
2569 set_bit(BTUSB_FIRMWARE_LOADED, &data->flags);
2570 return 0;
2571 }
2572
Kiran K62acbbb2021-03-08 16:44:54 +05302573 bt_dev_err(hdev, "Failed to load Intel firmware file %s (%d)",
2574 fwname, err);
Luiz Augusto von Dentz1f14a622021-04-30 16:05:01 -07002575
Kiran K3f43a372020-11-19 17:09:42 +05302576 return err;
2577 }
2578
2579 bt_dev_info(hdev, "Found device firmware: %s", fwname);
2580
2581 if (fw->size < 644) {
2582 bt_dev_err(hdev, "Invalid size of firmware file (%zu)",
2583 fw->size);
2584 err = -EBADF;
2585 goto done;
2586 }
2587
Luiz Augusto von Dentz21e31c62021-03-23 11:59:00 -07002588 calltime = ktime_get();
2589
Kiran K3f43a372020-11-19 17:09:42 +05302590 set_bit(BTUSB_DOWNLOADING, &data->flags);
2591
2592 /* Start firmware downloading and get boot parameter */
Luiz Augusto von Dentzac056542021-03-23 11:58:56 -07002593 err = btintel_download_firmware_newgen(hdev, ver, fw, boot_param,
Kiran K3f43a372020-11-19 17:09:42 +05302594 INTEL_HW_VARIANT(ver->cnvi_bt),
2595 ver->sbe_type);
2596 if (err < 0) {
Luiz Augusto von Dentzac056542021-03-23 11:58:56 -07002597 if (err == -EALREADY) {
2598 /* Firmware has already been loaded */
2599 set_bit(BTUSB_FIRMWARE_LOADED, &data->flags);
2600 err = 0;
2601 goto done;
2602 }
2603
Kiran K3f43a372020-11-19 17:09:42 +05302604 /* When FW download fails, send Intel Reset to retry
2605 * FW download.
2606 */
2607 btintel_reset_to_bootloader(hdev);
2608 goto done;
2609 }
Kiran K3f43a372020-11-19 17:09:42 +05302610
2611 /* Before switching the device into operational mode and with that
2612 * booting the loaded firmware, wait for the bootloader notification
2613 * that all fragments have been successfully received.
2614 *
2615 * When the event processing receives the notification, then the
2616 * BTUSB_DOWNLOADING flag will be cleared.
2617 *
2618 * The firmware loading should not take longer than 5 seconds
2619 * and thus just timeout if that happens and fail the setup
2620 * of this device.
2621 */
Luiz Augusto von Dentz21e31c62021-03-23 11:59:00 -07002622 err = btusb_download_wait(hdev, calltime, 5000);
2623 if (err == -ETIMEDOUT)
Kiran K3f43a372020-11-19 17:09:42 +05302624 btintel_reset_to_bootloader(hdev);
Kiran K3f43a372020-11-19 17:09:42 +05302625
2626done:
2627 release_firmware(fw);
2628 return err;
2629}
2630
Kiran K5ea7c812020-06-25 00:04:32 +05302631static int btusb_intel_download_firmware(struct hci_dev *hdev,
2632 struct intel_version *ver,
Kiran Kdc45d372020-08-26 15:24:32 +05302633 struct intel_boot_params *params,
2634 u32 *boot_param)
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002635{
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002636 const struct firmware *fw;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002637 char fwname[64];
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002638 int err;
Kiran K5ea7c812020-06-25 00:04:32 +05302639 struct btusb_data *data = hci_get_drvdata(hdev);
Luiz Augusto von Dentz21e31c62021-03-23 11:59:00 -07002640 ktime_t calltime;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002641
Kiran K5ea7c812020-06-25 00:04:32 +05302642 if (!ver || !params)
2643 return -EINVAL;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002644
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002645 /* The firmware variant determines if the device is in bootloader
2646 * mode or is running operational firmware. The value 0x06 identifies
2647 * the bootloader and the value 0x23 identifies the operational
2648 * firmware.
2649 *
2650 * When the operational firmware is already present, then only
2651 * the check for valid Bluetooth device address is needed. This
2652 * determines if the device will be added as configured or
2653 * unconfigured controller.
2654 *
2655 * It is not possible to use the Secure Boot Parameters in this
2656 * case since that command is only available in bootloader mode.
2657 */
Kiran K5ea7c812020-06-25 00:04:32 +05302658 if (ver->fw_variant == 0x23) {
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002659 clear_bit(BTUSB_BOOTLOADER, &data->flags);
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07002660 btintel_check_bdaddr(hdev);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002661
Luiz Augusto von Dentz9b16bfb2021-03-23 11:58:57 -07002662 /* SfP and WsP don't seem to update the firmware version on file
2663 * so version checking is currently possible.
2664 */
2665 switch (ver->hw_variant) {
2666 case 0x0b: /* SfP */
2667 case 0x0c: /* WsP */
2668 return 0;
2669 }
2670
2671 /* Proceed to download to check if the version matches */
2672 goto download;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002673 }
2674
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002675 /* Read the secure boot parameters to identify the operating
2676 * details of the bootloader.
2677 */
Kiran K5ea7c812020-06-25 00:04:32 +05302678 err = btintel_read_boot_params(hdev, params);
Tedd Ho-Jeong Anfaf174d2018-01-24 09:19:20 -08002679 if (err)
2680 return err;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002681
2682 /* It is required that every single firmware fragment is acknowledged
2683 * with a command complete event. If the boot parameters indicate
2684 * that this bootloader does not send them, then abort the setup.
2685 */
Kiran K5ea7c812020-06-25 00:04:32 +05302686 if (params->limited_cce != 0x00) {
Marcel Holtmann85418fe2018-08-03 11:20:49 +02002687 bt_dev_err(hdev, "Unsupported Intel firmware loading method (%u)",
Kiran K5ea7c812020-06-25 00:04:32 +05302688 params->limited_cce);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002689 return -EINVAL;
2690 }
2691
2692 /* If the OTP has no valid Bluetooth device address, then there will
2693 * also be no valid address for the operational firmware.
2694 */
Kiran K5ea7c812020-06-25 00:04:32 +05302695 if (!bacmp(&params->otp_bdaddr, BDADDR_ANY)) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002696 bt_dev_info(hdev, "No device address configured");
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002697 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
2698 }
2699
Luiz Augusto von Dentz9b16bfb2021-03-23 11:58:57 -07002700download:
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002701 /* With this Intel bootloader only the hardware variant and device
Jaya P Gaf3715e2017-10-30 11:01:22 +01002702 * revision information are used to select the right firmware for SfP
2703 * and WsP.
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002704 *
Tedd Ho-Jeong An230b04a2016-06-28 08:56:39 -07002705 * The firmware filename is ibt-<hw_variant>-<dev_revid>.sfi.
2706 *
2707 * Currently the supported hardware variants are:
2708 * 11 (0x0b) for iBT3.0 (LnP/SfP)
2709 * 12 (0x0c) for iBT3.5 (WsP)
Jaya P Gaf3715e2017-10-30 11:01:22 +01002710 *
2711 * For ThP/JfP and for future SKU's, the FW name varies based on HW
2712 * variant, HW revision and FW revision, as these are dependent on CNVi
2713 * and RF Combination.
2714 *
Tedd Ho-Jeong An86a61292017-05-01 13:35:12 -07002715 * 17 (0x11) for iBT3.5 (JfP)
2716 * 18 (0x12) for iBT3.5 (ThP)
Jaya P Gaf3715e2017-10-30 11:01:22 +01002717 *
2718 * The firmware file name for these will be
2719 * ibt-<hw_variant>-<hw_revision>-<fw_revision>.sfi.
2720 *
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002721 */
Kiran K5ea7c812020-06-25 00:04:32 +05302722 err = btusb_setup_intel_new_get_fw_name(ver, params, fwname,
Raghuram Hegde2da711bc2018-12-19 11:42:18 +05302723 sizeof(fwname), "sfi");
Luiz Augusto von Dentz9b16bfb2021-03-23 11:58:57 -07002724 if (err < 0) {
Luiz Augusto von Dentz1f14a622021-04-30 16:05:01 -07002725 if (!test_bit(BTUSB_BOOTLOADER, &data->flags)) {
2726 /* Firmware has already been loaded */
2727 set_bit(BTUSB_FIRMWARE_LOADED, &data->flags);
2728 return 0;
2729 }
2730
Marcel Holtmann85418fe2018-08-03 11:20:49 +02002731 bt_dev_err(hdev, "Unsupported Intel firmware naming");
Jaya P Gaf3715e2017-10-30 11:01:22 +01002732 return -EINVAL;
2733 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002734
Luiz Augusto von Dentz1f14a622021-04-30 16:05:01 -07002735 err = firmware_request_nowarn(&fw, fwname, &hdev->dev);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002736 if (err < 0) {
Luiz Augusto von Dentz1f14a622021-04-30 16:05:01 -07002737 if (!test_bit(BTUSB_BOOTLOADER, &data->flags)) {
2738 /* Firmware has already been loaded */
2739 set_bit(BTUSB_FIRMWARE_LOADED, &data->flags);
2740 return 0;
2741 }
2742
Kiran K62acbbb2021-03-08 16:44:54 +05302743 bt_dev_err(hdev, "Failed to load Intel firmware file %s (%d)",
2744 fwname, err);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002745 return err;
2746 }
2747
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002748 bt_dev_info(hdev, "Found device firmware: %s", fwname);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002749
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002750 if (fw->size < 644) {
Marcel Holtmann85418fe2018-08-03 11:20:49 +02002751 bt_dev_err(hdev, "Invalid size of firmware file (%zu)",
2752 fw->size);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002753 err = -EBADF;
2754 goto done;
2755 }
2756
Luiz Augusto von Dentz21e31c62021-03-23 11:59:00 -07002757 calltime = ktime_get();
2758
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002759 set_bit(BTUSB_DOWNLOADING, &data->flags);
2760
Tedd Ho-Jeong Anfbbe83c2018-01-24 09:19:21 -08002761 /* Start firmware downloading and get boot parameter */
Luiz Augusto von Dentzac056542021-03-23 11:58:56 -07002762 err = btintel_download_firmware(hdev, ver, fw, boot_param);
Amit K Bagb9a25622019-10-17 13:52:29 +05302763 if (err < 0) {
Luiz Augusto von Dentzac056542021-03-23 11:58:56 -07002764 if (err == -EALREADY) {
2765 /* Firmware has already been loaded */
2766 set_bit(BTUSB_FIRMWARE_LOADED, &data->flags);
2767 err = 0;
2768 goto done;
2769 }
2770
Amit K Bagb9a25622019-10-17 13:52:29 +05302771 /* When FW download fails, send Intel Reset to retry
2772 * FW download.
2773 */
2774 btintel_reset_to_bootloader(hdev);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002775 goto done;
Amit K Bagb9a25622019-10-17 13:52:29 +05302776 }
Johan Hedberga087a982015-01-30 10:58:54 +02002777
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002778 /* Before switching the device into operational mode and with that
2779 * booting the loaded firmware, wait for the bootloader notification
2780 * that all fragments have been successfully received.
2781 *
Johan Hedberga087a982015-01-30 10:58:54 +02002782 * When the event processing receives the notification, then the
2783 * BTUSB_DOWNLOADING flag will be cleared.
2784 *
2785 * The firmware loading should not take longer than 5 seconds
2786 * and thus just timeout if that happens and fail the setup
2787 * of this device.
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002788 */
Luiz Augusto von Dentz21e31c62021-03-23 11:59:00 -07002789 err = btusb_download_wait(hdev, calltime, 5000);
2790 if (err == -ETIMEDOUT)
Amit K Bagb9a25622019-10-17 13:52:29 +05302791 btintel_reset_to_bootloader(hdev);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002792
Kiran K5ea7c812020-06-25 00:04:32 +05302793done:
2794 release_firmware(fw);
2795 return err;
2796}
2797
Luiz Augusto von Dentz604b3cf2021-03-23 11:59:01 -07002798static int btusb_boot_wait(struct hci_dev *hdev, ktime_t calltime, int msec)
2799{
2800 struct btusb_data *data = hci_get_drvdata(hdev);
2801 ktime_t delta, rettime;
2802 unsigned long long duration;
2803 int err;
2804
2805 bt_dev_info(hdev, "Waiting for device to boot");
2806
2807 err = wait_on_bit_timeout(&data->flags, BTUSB_BOOTING,
2808 TASK_INTERRUPTIBLE,
2809 msecs_to_jiffies(msec));
2810 if (err == -EINTR) {
2811 bt_dev_err(hdev, "Device boot interrupted");
2812 return -EINTR;
2813 }
2814
2815 if (err) {
2816 bt_dev_err(hdev, "Device boot timeout");
2817 return -ETIMEDOUT;
2818 }
2819
2820 rettime = ktime_get();
2821 delta = ktime_sub(rettime, calltime);
2822 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
2823
2824 bt_dev_info(hdev, "Device booted in %llu usecs", duration);
2825
2826 return 0;
2827}
2828
2829static int btusb_intel_boot(struct hci_dev *hdev, u32 boot_addr)
2830{
2831 struct btusb_data *data = hci_get_drvdata(hdev);
2832 ktime_t calltime;
2833 int err;
2834
2835 calltime = ktime_get();
2836
2837 set_bit(BTUSB_BOOTING, &data->flags);
2838
2839 err = btintel_send_intel_reset(hdev, boot_addr);
2840 if (err) {
2841 bt_dev_err(hdev, "Intel Soft Reset failed (%d)", err);
2842 btintel_reset_to_bootloader(hdev);
2843 return err;
2844 }
2845
2846 /* The bootloader will not indicate when the device is ready. This
2847 * is done by the operational firmware sending bootup notification.
2848 *
2849 * Booting into operational firmware should not take longer than
2850 * 1 second. However if that happens, then just fail the setup
2851 * since something went wrong.
2852 */
2853 err = btusb_boot_wait(hdev, calltime, 1000);
2854 if (err == -ETIMEDOUT)
2855 btintel_reset_to_bootloader(hdev);
2856
2857 return err;
2858}
2859
Kiran K5ea7c812020-06-25 00:04:32 +05302860static int btusb_setup_intel_new(struct hci_dev *hdev)
2861{
2862 struct btusb_data *data = hci_get_drvdata(hdev);
2863 struct intel_version ver;
2864 struct intel_boot_params params;
2865 u32 boot_param;
2866 char ddcname[64];
Kiran K5ea7c812020-06-25 00:04:32 +05302867 int err;
2868 struct intel_debug_features features;
2869
2870 BT_DBG("%s", hdev->name);
2871
2872 /* Set the default boot parameter to 0x0 and it is updated to
2873 * SKU specific boot parameter after reading Intel_Write_Boot_Params
2874 * command while downloading the firmware.
2875 */
2876 boot_param = 0x00000000;
2877
Kiran K5ea7c812020-06-25 00:04:32 +05302878 /* Read the Intel version information to determine if the device
2879 * is in bootloader mode or if it already has operational firmware
2880 * loaded.
2881 */
2882 err = btintel_read_version(hdev, &ver);
2883 if (err) {
2884 bt_dev_err(hdev, "Intel Read version failed (%d)", err);
2885 btintel_reset_to_bootloader(hdev);
2886 return err;
2887 }
2888
Luiz Augusto von Dentzd68903d2021-03-23 11:58:59 -07002889 err = btintel_version_info(hdev, &ver);
2890 if (err)
2891 return err;
2892
Kiran Kdc45d372020-08-26 15:24:32 +05302893 err = btusb_intel_download_firmware(hdev, &ver, &params, &boot_param);
Kiran K5ea7c812020-06-25 00:04:32 +05302894 if (err)
2895 return err;
2896
2897 /* controller is already having an operational firmware */
2898 if (ver.fw_variant == 0x23)
2899 goto finish;
2900
Luiz Augusto von Dentz604b3cf2021-03-23 11:59:01 -07002901 err = btusb_intel_boot(hdev, boot_param);
2902 if (err)
Tedd Ho-Jeong Ane5889af2018-01-24 09:19:18 -08002903 return err;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002904
2905 clear_bit(BTUSB_BOOTLOADER, &data->flags);
2906
Kiran K5ea7c812020-06-25 00:04:32 +05302907 err = btusb_setup_intel_new_get_fw_name(&ver, &params, ddcname,
2908 sizeof(ddcname), "ddc");
2909
Luiz Augusto von Dentz9b16bfb2021-03-23 11:58:57 -07002910 if (err < 0) {
Kiran K5ea7c812020-06-25 00:04:32 +05302911 bt_dev_err(hdev, "Unsupported Intel firmware naming");
2912 } else {
2913 /* Once the device is running in operational mode, it needs to
2914 * apply the device configuration (DDC) parameters.
2915 *
2916 * The device can work without DDC parameters, so even if it
2917 * fails to load the file, no need to fail the setup.
2918 */
2919 btintel_load_ddc_config(hdev, ddcname);
2920 }
Tedd Ho-Jeong An52cc9162015-06-12 16:20:05 -07002921
Chethan T Nd74abe22020-06-08 17:57:46 +05302922 /* Read the Intel supported features and if new exception formats
2923 * supported, need to load the additional DDC config to enable.
2924 */
Jun Miao20a831f2021-07-09 21:46:25 +08002925 err = btintel_read_debug_features(hdev, &features);
2926 if (!err) {
2927 /* Set DDC mask for available debug features */
2928 btintel_set_debug_features(hdev, &features);
2929 }
Chethan T Nc453b102020-06-08 17:57:47 +05302930
Marcel Holtmann7fd673b2020-04-03 21:44:02 +02002931 /* Read the Intel version information after loading the FW */
2932 err = btintel_read_version(hdev, &ver);
2933 if (err)
2934 return err;
2935
2936 btintel_version_info(hdev, &ver);
2937
2938finish:
Miao-chen Choufc045902020-04-03 21:44:03 +02002939 /* All Intel controllers that support the Microsoft vendor
2940 * extension are using 0xFC1E for VsMsftOpCode.
2941 */
2942 switch (ver.hw_variant) {
Miao-chen Chou7a45bcb2020-12-17 14:53:19 -08002943 case 0x11: /* JfP */
Miao-chen Choufc045902020-04-03 21:44:03 +02002944 case 0x12: /* ThP */
Miao-chen Chou7a45bcb2020-12-17 14:53:19 -08002945 case 0x13: /* HrP */
2946 case 0x14: /* CcP */
Miao-chen Choufc045902020-04-03 21:44:03 +02002947 hci_set_msft_opcode(hdev, 0xFC1E);
2948 break;
2949 }
2950
Marcel Holtmann213445b2015-10-21 02:45:19 +02002951 /* Set the event mask for Intel specific vendor events. This enables
2952 * a few extra events that are useful during general operation. It
2953 * does not enable any debugging related events.
2954 *
2955 * The device will function correctly without these events enabled
2956 * and thus no need to fail the setup.
2957 */
2958 btintel_set_event_mask(hdev, false);
2959
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002960 return 0;
2961}
2962
Kiran K0a3c1d42020-11-19 17:09:40 +05302963static int btusb_setup_intel_newgen(struct hci_dev *hdev)
2964{
2965 struct btusb_data *data = hci_get_drvdata(hdev);
2966 u32 boot_param;
2967 char ddcname[64];
Kiran K0a3c1d42020-11-19 17:09:40 +05302968 int err;
2969 struct intel_debug_features features;
2970 struct intel_version_tlv version;
2971
2972 bt_dev_dbg(hdev, "");
2973
2974 /* Set the default boot parameter to 0x0 and it is updated to
2975 * SKU specific boot parameter after reading Intel_Write_Boot_Params
2976 * command while downloading the firmware.
2977 */
2978 boot_param = 0x00000000;
2979
Kiran K0a3c1d42020-11-19 17:09:40 +05302980 /* Read the Intel version information to determine if the device
2981 * is in bootloader mode or if it already has operational firmware
2982 * loaded.
2983 */
2984 err = btintel_read_version_tlv(hdev, &version);
2985 if (err) {
2986 bt_dev_err(hdev, "Intel Read version failed (%d)", err);
2987 btintel_reset_to_bootloader(hdev);
2988 return err;
2989 }
2990
Luiz Augusto von Dentz0a460d82021-03-23 11:58:58 -07002991 err = btintel_version_info_tlv(hdev, &version);
2992 if (err)
2993 return err;
Kiran K0a3c1d42020-11-19 17:09:40 +05302994
Kiran K3f43a372020-11-19 17:09:42 +05302995 err = btusb_intel_download_firmware_newgen(hdev, &version, &boot_param);
2996 if (err)
2997 return err;
2998
Kiran K0a3c1d42020-11-19 17:09:40 +05302999 /* check if controller is already having an operational firmware */
3000 if (version.img_type == 0x03)
3001 goto finish;
3002
Luiz Augusto von Dentz604b3cf2021-03-23 11:59:01 -07003003 err = btusb_intel_boot(hdev, boot_param);
3004 if (err)
Kiran K0a3c1d42020-11-19 17:09:40 +05303005 return err;
Kiran K0a3c1d42020-11-19 17:09:40 +05303006
3007 clear_bit(BTUSB_BOOTLOADER, &data->flags);
3008
Kiran K9a93b8b2020-11-19 17:09:41 +05303009 btusb_setup_intel_newgen_get_fw_name(&version, ddcname, sizeof(ddcname),
3010 "ddc");
Kiran K0a3c1d42020-11-19 17:09:40 +05303011 /* Once the device is running in operational mode, it needs to
3012 * apply the device configuration (DDC) parameters.
3013 *
3014 * The device can work without DDC parameters, so even if it
3015 * fails to load the file, no need to fail the setup.
3016 */
3017 btintel_load_ddc_config(hdev, ddcname);
3018
3019 /* Read the Intel supported features and if new exception formats
3020 * supported, need to load the additional DDC config to enable.
3021 */
Jun Miao20a831f2021-07-09 21:46:25 +08003022 err = btintel_read_debug_features(hdev, &features);
3023 if (!err) {
3024 /* Set DDC mask for available debug features */
3025 btintel_set_debug_features(hdev, &features);
3026 }
Kiran K0a3c1d42020-11-19 17:09:40 +05303027
3028 /* Read the Intel version information after loading the FW */
3029 err = btintel_read_version_tlv(hdev, &version);
3030 if (err)
3031 return err;
3032
3033 btintel_version_info_tlv(hdev, &version);
3034
3035finish:
Chethan T Nf283f472021-07-29 09:43:21 -07003036 /* All Intel new genration controllers support the Microsoft vendor
3037 * extension are using 0xFC1E for VsMsftOpCode.
3038 */
3039 switch (INTEL_HW_VARIANT(version.cnvi_bt)) {
3040 case 0x17:
3041 case 0x18:
3042 case 0x19:
3043 hci_set_msft_opcode(hdev, 0xFC1E);
3044 break;
3045 }
3046
Kiran K0a3c1d42020-11-19 17:09:40 +05303047 /* Set the event mask for Intel specific vendor events. This enables
3048 * a few extra events that are useful during general operation. It
3049 * does not enable any debugging related events.
3050 *
3051 * The device will function correctly without these events enabled
3052 * and thus no need to fail the setup.
3053 */
3054 btintel_set_event_mask(hdev, false);
3055
3056 return 0;
3057}
Tedd Ho-Jeong Anbfbd45e2015-02-13 09:20:52 -08003058static int btusb_shutdown_intel(struct hci_dev *hdev)
3059{
3060 struct sk_buff *skb;
3061 long ret;
3062
Amit K Bag1313bcc2018-08-03 12:43:20 +05303063 /* In the shutdown sequence where Bluetooth is turned off followed
3064 * by WiFi being turned off, turning WiFi back on causes issue with
3065 * the RF calibration.
3066 *
3067 * To ensure that any RF activity has been stopped, issue HCI Reset
3068 * command to clear all ongoing activity including advertising,
3069 * scanning etc.
3070 */
3071 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
3072 if (IS_ERR(skb)) {
3073 ret = PTR_ERR(skb);
3074 bt_dev_err(hdev, "HCI reset during shutdown failed");
3075 return ret;
3076 }
3077 kfree_skb(skb);
3078
Tedd Ho-Jeong Anbfbd45e2015-02-13 09:20:52 -08003079 /* Some platforms have an issue with BT LED when the interface is
3080 * down or BT radio is turned off, which takes 5 seconds to BT LED
3081 * goes off. This command turns off the BT LED immediately.
3082 */
3083 skb = __hci_cmd_sync(hdev, 0xfc3f, 0, NULL, HCI_INIT_TIMEOUT);
3084 if (IS_ERR(skb)) {
3085 ret = PTR_ERR(skb);
Marcel Holtmann85418fe2018-08-03 11:20:49 +02003086 bt_dev_err(hdev, "turning off Intel device LED failed");
Tedd Ho-Jeong Anbfbd45e2015-02-13 09:20:52 -08003087 return ret;
3088 }
3089 kfree_skb(skb);
3090
3091 return 0;
3092}
3093
Raghuram Hegde017a01c2019-01-29 17:54:48 +05303094static int btusb_shutdown_intel_new(struct hci_dev *hdev)
3095{
3096 struct sk_buff *skb;
3097
3098 /* Send HCI Reset to the controller to stop any BT activity which
3099 * were triggered. This will help to save power and maintain the
3100 * sync b/w Host and controller
3101 */
3102 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
3103 if (IS_ERR(skb)) {
3104 bt_dev_err(hdev, "HCI reset during shutdown failed");
3105 return PTR_ERR(skb);
3106 }
3107 kfree_skb(skb);
3108
3109 return 0;
3110}
3111
Sean Wang9ce67c32019-06-02 08:02:49 +08003112#define FIRMWARE_MT7663 "mediatek/mt7663pr2h.bin"
Sean Wanga1c49c432019-06-02 08:02:48 +08003113#define FIRMWARE_MT7668 "mediatek/mt7668pr2h.bin"
3114
3115#define HCI_WMT_MAX_EVENT_SIZE 64
Mark Chenfc342c42021-02-02 18:26:18 +08003116/* It is for mt79xx download rom patch*/
3117#define MTK_FW_ROM_PATCH_HEADER_SIZE 32
3118#define MTK_FW_ROM_PATCH_GD_SIZE 64
3119#define MTK_FW_ROM_PATCH_SEC_MAP_SIZE 64
3120#define MTK_SEC_MAP_COMMON_SIZE 12
3121#define MTK_SEC_MAP_NEED_SEND_SIZE 52
Sean Wanga1c49c432019-06-02 08:02:48 +08003122
3123enum {
3124 BTMTK_WMT_PATCH_DWNLD = 0x1,
3125 BTMTK_WMT_FUNC_CTRL = 0x6,
3126 BTMTK_WMT_RST = 0x7,
3127 BTMTK_WMT_SEMAPHORE = 0x17,
3128};
3129
3130enum {
3131 BTMTK_WMT_INVALID,
3132 BTMTK_WMT_PATCH_UNDONE,
Mark Chen48c13302021-02-02 18:26:17 +08003133 BTMTK_WMT_PATCH_PROGRESS,
Sean Wanga1c49c432019-06-02 08:02:48 +08003134 BTMTK_WMT_PATCH_DONE,
3135 BTMTK_WMT_ON_UNDONE,
3136 BTMTK_WMT_ON_DONE,
3137 BTMTK_WMT_ON_PROGRESS,
3138};
3139
3140struct btmtk_wmt_hdr {
3141 u8 dir;
3142 u8 op;
3143 __le16 dlen;
3144 u8 flag;
3145} __packed;
3146
3147struct btmtk_hci_wmt_cmd {
3148 struct btmtk_wmt_hdr hdr;
Arnd Bergmann10888142021-02-04 16:47:07 +01003149 u8 data[];
Sean Wanga1c49c432019-06-02 08:02:48 +08003150} __packed;
3151
3152struct btmtk_hci_wmt_evt {
3153 struct hci_event_hdr hhdr;
3154 struct btmtk_wmt_hdr whdr;
3155} __packed;
3156
3157struct btmtk_hci_wmt_evt_funcc {
3158 struct btmtk_hci_wmt_evt hwhdr;
3159 __be16 status;
3160} __packed;
3161
3162struct btmtk_tci_sleep {
3163 u8 mode;
3164 __le16 duration;
3165 __le16 host_duration;
3166 u8 host_wakeup_pin;
3167 u8 time_compensation;
3168} __packed;
3169
3170struct btmtk_hci_wmt_params {
3171 u8 op;
3172 u8 flag;
3173 u16 dlen;
3174 const void *data;
3175 u32 *status;
3176};
3177
Mark Chenfc342c42021-02-02 18:26:18 +08003178struct btmtk_patch_header {
3179 u8 datetime[16];
3180 u8 platform[4];
3181 __le16 hwver;
3182 __le16 swver;
3183 __le32 magicnum;
3184} __packed;
3185
3186struct btmtk_global_desc {
3187 __le32 patch_ver;
3188 __le32 sub_sys;
3189 __le32 feature_opt;
3190 __le32 section_num;
3191} __packed;
3192
3193struct btmtk_section_map {
3194 __le32 sectype;
3195 __le32 secoffset;
3196 __le32 secsize;
3197 union {
3198 __le32 u4SecSpec[13];
3199 struct {
3200 __le32 dlAddr;
3201 __le32 dlsize;
3202 __le32 seckeyidx;
3203 __le32 alignlen;
3204 __le32 sectype;
3205 __le32 dlmodecrctype;
3206 __le32 crc;
3207 __le32 reserved[6];
3208 } bin_info_spec;
3209 };
3210} __packed;
3211
Sean Wanga1c49c432019-06-02 08:02:48 +08003212static void btusb_mtk_wmt_recv(struct urb *urb)
3213{
3214 struct hci_dev *hdev = urb->context;
3215 struct btusb_data *data = hci_get_drvdata(hdev);
3216 struct hci_event_hdr *hdr;
3217 struct sk_buff *skb;
3218 int err;
3219
3220 if (urb->status == 0 && urb->actual_length > 0) {
3221 hdev->stat.byte_rx += urb->actual_length;
3222
3223 /* WMT event shouldn't be fragmented and the size should be
3224 * less than HCI_WMT_MAX_EVENT_SIZE.
3225 */
3226 skb = bt_skb_alloc(HCI_WMT_MAX_EVENT_SIZE, GFP_ATOMIC);
3227 if (!skb) {
3228 hdev->stat.err_rx++;
Jupeng Zhongde71a6c2021-02-02 09:39:13 +08003229 return;
Sean Wanga1c49c432019-06-02 08:02:48 +08003230 }
3231
3232 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
3233 skb_put_data(skb, urb->transfer_buffer, urb->actual_length);
3234
3235 hdr = (void *)skb->data;
3236 /* Fix up the vendor event id with 0xff for vendor specific
3237 * instead of 0xe4 so that event send via monitoring socket can
3238 * be parsed properly.
3239 */
3240 hdr->evt = 0xff;
3241
3242 /* When someone waits for the WMT event, the skb is being cloned
3243 * and being processed the events from there then.
3244 */
3245 if (test_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags)) {
Johan Hovold22cc6b72019-11-28 19:24:27 +01003246 data->evt_skb = skb_clone(skb, GFP_ATOMIC);
Jupeng Zhongde71a6c2021-02-02 09:39:13 +08003247 if (!data->evt_skb) {
3248 kfree_skb(skb);
3249 return;
3250 }
Sean Wanga1c49c432019-06-02 08:02:48 +08003251 }
3252
3253 err = hci_recv_frame(hdev, skb);
Jupeng Zhongde71a6c2021-02-02 09:39:13 +08003254 if (err < 0) {
3255 kfree_skb(data->evt_skb);
3256 data->evt_skb = NULL;
3257 return;
3258 }
Sean Wanga1c49c432019-06-02 08:02:48 +08003259
3260 if (test_and_clear_bit(BTUSB_TX_WAIT_VND_EVT,
3261 &data->flags)) {
3262 /* Barrier to sync with other CPUs */
3263 smp_mb__after_atomic();
3264 wake_up_bit(&data->flags,
3265 BTUSB_TX_WAIT_VND_EVT);
3266 }
Sean Wanga1c49c432019-06-02 08:02:48 +08003267 return;
3268 } else if (urb->status == -ENOENT) {
3269 /* Avoid suspend failed when usb_kill_urb */
3270 return;
3271 }
3272
3273 usb_mark_last_busy(data->udev);
3274
3275 /* The URB complete handler is still called with urb->actual_length = 0
3276 * when the event is not available, so we should keep re-submitting
3277 * URB until WMT event returns, Also, It's necessary to wait some time
3278 * between the two consecutive control URBs to relax the target device
3279 * to generate the event. Otherwise, the WMT event cannot return from
3280 * the device successfully.
3281 */
Mark Chen48c13302021-02-02 18:26:17 +08003282 udelay(500);
Sean Wanga1c49c432019-06-02 08:02:48 +08003283
3284 usb_anchor_urb(urb, &data->ctrl_anchor);
3285 err = usb_submit_urb(urb, GFP_ATOMIC);
3286 if (err < 0) {
3287 /* -EPERM: urb is being killed;
3288 * -ENODEV: device got disconnected
3289 */
3290 if (err != -EPERM && err != -ENODEV)
3291 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
3292 urb, -err);
3293 usb_unanchor_urb(urb);
3294 }
3295}
3296
3297static int btusb_mtk_submit_wmt_recv_urb(struct hci_dev *hdev)
3298{
3299 struct btusb_data *data = hci_get_drvdata(hdev);
3300 struct usb_ctrlrequest *dr;
3301 unsigned char *buf;
3302 int err, size = 64;
3303 unsigned int pipe;
3304 struct urb *urb;
3305
3306 urb = usb_alloc_urb(0, GFP_KERNEL);
3307 if (!urb)
3308 return -ENOMEM;
3309
3310 dr = kmalloc(sizeof(*dr), GFP_KERNEL);
3311 if (!dr) {
3312 usb_free_urb(urb);
3313 return -ENOMEM;
3314 }
3315
3316 dr->bRequestType = USB_TYPE_VENDOR | USB_DIR_IN;
3317 dr->bRequest = 1;
3318 dr->wIndex = cpu_to_le16(0);
3319 dr->wValue = cpu_to_le16(48);
3320 dr->wLength = cpu_to_le16(size);
3321
3322 buf = kmalloc(size, GFP_KERNEL);
3323 if (!buf) {
3324 kfree(dr);
Dinghao Liud33fe772020-08-23 15:44:21 +08003325 usb_free_urb(urb);
Sean Wanga1c49c432019-06-02 08:02:48 +08003326 return -ENOMEM;
3327 }
3328
3329 pipe = usb_rcvctrlpipe(data->udev, 0);
3330
3331 usb_fill_control_urb(urb, data->udev, pipe, (void *)dr,
3332 buf, size, btusb_mtk_wmt_recv, hdev);
3333
3334 urb->transfer_flags |= URB_FREE_BUFFER;
3335
3336 usb_anchor_urb(urb, &data->ctrl_anchor);
3337 err = usb_submit_urb(urb, GFP_KERNEL);
3338 if (err < 0) {
3339 if (err != -EPERM && err != -ENODEV)
3340 bt_dev_err(hdev, "urb %p submission failed (%d)",
3341 urb, -err);
3342 usb_unanchor_urb(urb);
3343 }
3344
3345 usb_free_urb(urb);
3346
3347 return err;
3348}
3349
3350static int btusb_mtk_hci_wmt_sync(struct hci_dev *hdev,
3351 struct btmtk_hci_wmt_params *wmt_params)
3352{
3353 struct btusb_data *data = hci_get_drvdata(hdev);
3354 struct btmtk_hci_wmt_evt_funcc *wmt_evt_funcc;
3355 u32 hlen, status = BTMTK_WMT_INVALID;
3356 struct btmtk_hci_wmt_evt *wmt_evt;
Arnd Bergmann10888142021-02-04 16:47:07 +01003357 struct btmtk_hci_wmt_cmd *wc;
Sean Wanga1c49c432019-06-02 08:02:48 +08003358 struct btmtk_wmt_hdr *hdr;
3359 int err;
3360
Sean Wanga1c49c432019-06-02 08:02:48 +08003361 /* Send the WMT command and wait until the WMT event returns */
3362 hlen = sizeof(*hdr) + wmt_params->dlen;
3363 if (hlen > 255)
3364 return -EINVAL;
3365
Arnd Bergmann10888142021-02-04 16:47:07 +01003366 wc = kzalloc(hlen, GFP_KERNEL);
3367 if (!wc)
3368 return -ENOMEM;
3369
3370 hdr = &wc->hdr;
Sean Wanga1c49c432019-06-02 08:02:48 +08003371 hdr->dir = 1;
3372 hdr->op = wmt_params->op;
3373 hdr->dlen = cpu_to_le16(wmt_params->dlen + 1);
3374 hdr->flag = wmt_params->flag;
Arnd Bergmann10888142021-02-04 16:47:07 +01003375 memcpy(wc->data, wmt_params->data, wmt_params->dlen);
Sean Wanga1c49c432019-06-02 08:02:48 +08003376
3377 set_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
3378
Arnd Bergmann10888142021-02-04 16:47:07 +01003379 err = __hci_cmd_send(hdev, 0xfc6f, hlen, wc);
Sean Wanga1c49c432019-06-02 08:02:48 +08003380
3381 if (err < 0) {
3382 clear_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
Arnd Bergmann10888142021-02-04 16:47:07 +01003383 goto err_free_wc;
Sean Wanga1c49c432019-06-02 08:02:48 +08003384 }
3385
mark-yw.chen8454ed92021-04-12 23:06:26 +08003386 /* Submit control IN URB on demand to process the WMT event */
3387 err = btusb_mtk_submit_wmt_recv_urb(hdev);
3388 if (err < 0)
Muhammad Usama Anjum44e936d2021-04-13 22:52:08 +05003389 goto err_free_wc;
mark-yw.chen8454ed92021-04-12 23:06:26 +08003390
Sean Wanga1c49c432019-06-02 08:02:48 +08003391 /* The vendor specific WMT commands are all answered by a vendor
3392 * specific event and will have the Command Status or Command
3393 * Complete as with usual HCI command flow control.
3394 *
3395 * After sending the command, wait for BTUSB_TX_WAIT_VND_EVT
3396 * state to be cleared. The driver specific event receive routine
3397 * will clear that state and with that indicate completion of the
3398 * WMT command.
3399 */
3400 err = wait_on_bit_timeout(&data->flags, BTUSB_TX_WAIT_VND_EVT,
3401 TASK_INTERRUPTIBLE, HCI_INIT_TIMEOUT);
3402 if (err == -EINTR) {
3403 bt_dev_err(hdev, "Execution of wmt command interrupted");
3404 clear_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
Arnd Bergmann10888142021-02-04 16:47:07 +01003405 goto err_free_wc;
Sean Wanga1c49c432019-06-02 08:02:48 +08003406 }
3407
3408 if (err) {
3409 bt_dev_err(hdev, "Execution of wmt command timed out");
3410 clear_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
Arnd Bergmann10888142021-02-04 16:47:07 +01003411 err = -ETIMEDOUT;
3412 goto err_free_wc;
Sean Wanga1c49c432019-06-02 08:02:48 +08003413 }
3414
3415 /* Parse and handle the return WMT event */
3416 wmt_evt = (struct btmtk_hci_wmt_evt *)data->evt_skb->data;
3417 if (wmt_evt->whdr.op != hdr->op) {
3418 bt_dev_err(hdev, "Wrong op received %d expected %d",
3419 wmt_evt->whdr.op, hdr->op);
3420 err = -EIO;
3421 goto err_free_skb;
3422 }
3423
3424 switch (wmt_evt->whdr.op) {
3425 case BTMTK_WMT_SEMAPHORE:
3426 if (wmt_evt->whdr.flag == 2)
3427 status = BTMTK_WMT_PATCH_UNDONE;
3428 else
3429 status = BTMTK_WMT_PATCH_DONE;
3430 break;
3431 case BTMTK_WMT_FUNC_CTRL:
3432 wmt_evt_funcc = (struct btmtk_hci_wmt_evt_funcc *)wmt_evt;
3433 if (be16_to_cpu(wmt_evt_funcc->status) == 0x404)
3434 status = BTMTK_WMT_ON_DONE;
3435 else if (be16_to_cpu(wmt_evt_funcc->status) == 0x420)
3436 status = BTMTK_WMT_ON_PROGRESS;
3437 else
3438 status = BTMTK_WMT_ON_UNDONE;
3439 break;
Mark Chenfc342c42021-02-02 18:26:18 +08003440 case BTMTK_WMT_PATCH_DWNLD:
3441 if (wmt_evt->whdr.flag == 2)
3442 status = BTMTK_WMT_PATCH_DONE;
3443 else if (wmt_evt->whdr.flag == 1)
3444 status = BTMTK_WMT_PATCH_PROGRESS;
3445 else
3446 status = BTMTK_WMT_PATCH_UNDONE;
3447 break;
Sean Wanga1c49c432019-06-02 08:02:48 +08003448 }
3449
3450 if (wmt_params->status)
3451 *wmt_params->status = status;
3452
3453err_free_skb:
3454 kfree_skb(data->evt_skb);
3455 data->evt_skb = NULL;
Arnd Bergmann10888142021-02-04 16:47:07 +01003456err_free_wc:
3457 kfree(wc);
Sean Wanga1c49c432019-06-02 08:02:48 +08003458 return err;
3459}
3460
Mark Chenfc342c42021-02-02 18:26:18 +08003461static int btusb_mtk_setup_firmware_79xx(struct hci_dev *hdev, const char *fwname)
3462{
3463 struct btmtk_hci_wmt_params wmt_params;
Mark Chenfc342c42021-02-02 18:26:18 +08003464 struct btmtk_global_desc *globaldesc = NULL;
3465 struct btmtk_section_map *sectionmap;
3466 const struct firmware *fw;
3467 const u8 *fw_ptr;
3468 const u8 *fw_bin_ptr;
Mark Chenfc342c42021-02-02 18:26:18 +08003469 int err, dlen, i, status;
3470 u8 flag, first_block, retry;
3471 u32 section_num, dl_size, section_offset;
3472 u8 cmd[64];
3473
3474 err = request_firmware(&fw, fwname, &hdev->dev);
3475 if (err < 0) {
3476 bt_dev_err(hdev, "Failed to load firmware file (%d)", err);
3477 return err;
3478 }
3479
3480 fw_ptr = fw->data;
3481 fw_bin_ptr = fw_ptr;
Mark Chenfc342c42021-02-02 18:26:18 +08003482 globaldesc = (struct btmtk_global_desc *)(fw_ptr + MTK_FW_ROM_PATCH_HEADER_SIZE);
mark-yw.chen201cf392021-02-23 14:27:40 +08003483 section_num = le32_to_cpu(globaldesc->section_num);
Mark Chenfc342c42021-02-02 18:26:18 +08003484
3485 for (i = 0; i < section_num; i++) {
3486 first_block = 1;
3487 fw_ptr = fw_bin_ptr;
3488 sectionmap = (struct btmtk_section_map *)(fw_ptr + MTK_FW_ROM_PATCH_HEADER_SIZE +
3489 MTK_FW_ROM_PATCH_GD_SIZE + MTK_FW_ROM_PATCH_SEC_MAP_SIZE * i);
3490
mark-yw.chen201cf392021-02-23 14:27:40 +08003491 section_offset = le32_to_cpu(sectionmap->secoffset);
3492 dl_size = le32_to_cpu(sectionmap->bin_info_spec.dlsize);
Mark Chenfc342c42021-02-02 18:26:18 +08003493
3494 if (dl_size > 0) {
3495 retry = 20;
3496 while (retry > 0) {
3497 cmd[0] = 0; /* 0 means legacy dl mode. */
3498 memcpy(cmd + 1,
3499 fw_ptr + MTK_FW_ROM_PATCH_HEADER_SIZE +
3500 MTK_FW_ROM_PATCH_GD_SIZE + MTK_FW_ROM_PATCH_SEC_MAP_SIZE * i +
3501 MTK_SEC_MAP_COMMON_SIZE,
3502 MTK_SEC_MAP_NEED_SEND_SIZE + 1);
3503
3504 wmt_params.op = BTMTK_WMT_PATCH_DWNLD;
3505 wmt_params.status = &status;
3506 wmt_params.flag = 0;
3507 wmt_params.dlen = MTK_SEC_MAP_NEED_SEND_SIZE + 1;
3508 wmt_params.data = &cmd;
3509
3510 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3511 if (err < 0) {
3512 bt_dev_err(hdev, "Failed to send wmt patch dwnld (%d)",
3513 err);
3514 goto err_release_fw;
3515 }
3516
3517 if (status == BTMTK_WMT_PATCH_UNDONE) {
3518 break;
3519 } else if (status == BTMTK_WMT_PATCH_PROGRESS) {
3520 msleep(100);
3521 retry--;
3522 } else if (status == BTMTK_WMT_PATCH_DONE) {
3523 goto next_section;
3524 } else {
3525 bt_dev_err(hdev, "Failed wmt patch dwnld status (%d)",
3526 status);
3527 goto err_release_fw;
3528 }
3529 }
3530
3531 fw_ptr += section_offset;
3532 wmt_params.op = BTMTK_WMT_PATCH_DWNLD;
3533 wmt_params.status = NULL;
3534
3535 while (dl_size > 0) {
3536 dlen = min_t(int, 250, dl_size);
3537 if (first_block == 1) {
3538 flag = 1;
3539 first_block = 0;
3540 } else if (dl_size - dlen <= 0) {
3541 flag = 3;
3542 } else {
3543 flag = 2;
3544 }
3545
3546 wmt_params.flag = flag;
3547 wmt_params.dlen = dlen;
3548 wmt_params.data = fw_ptr;
3549
3550 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3551 if (err < 0) {
3552 bt_dev_err(hdev, "Failed to send wmt patch dwnld (%d)",
3553 err);
3554 goto err_release_fw;
3555 }
3556
3557 dl_size -= dlen;
3558 fw_ptr += dlen;
3559 }
3560 }
3561next_section:
3562 continue;
3563 }
3564 /* Wait a few moments for firmware activation done */
3565 usleep_range(100000, 120000);
3566
3567err_release_fw:
3568 release_firmware(fw);
3569
3570 return err;
3571}
3572
Sean Wanga1c49c432019-06-02 08:02:48 +08003573static int btusb_mtk_setup_firmware(struct hci_dev *hdev, const char *fwname)
3574{
3575 struct btmtk_hci_wmt_params wmt_params;
3576 const struct firmware *fw;
3577 const u8 *fw_ptr;
3578 size_t fw_size;
3579 int err, dlen;
Sean Wangf6451252020-06-19 19:52:01 +00003580 u8 flag, param;
Sean Wanga1c49c432019-06-02 08:02:48 +08003581
3582 err = request_firmware(&fw, fwname, &hdev->dev);
3583 if (err < 0) {
3584 bt_dev_err(hdev, "Failed to load firmware file (%d)", err);
3585 return err;
3586 }
3587
Sean Wangf6451252020-06-19 19:52:01 +00003588 /* Power on data RAM the firmware relies on. */
3589 param = 1;
3590 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
3591 wmt_params.flag = 3;
3592 wmt_params.dlen = sizeof(param);
3593 wmt_params.data = &param;
3594 wmt_params.status = NULL;
3595
3596 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3597 if (err < 0) {
3598 bt_dev_err(hdev, "Failed to power on data RAM (%d)", err);
Jing Xiangfengd1e9d232020-11-17 10:59:17 +08003599 goto err_release_fw;
Sean Wangf6451252020-06-19 19:52:01 +00003600 }
3601
Sean Wanga1c49c432019-06-02 08:02:48 +08003602 fw_ptr = fw->data;
3603 fw_size = fw->size;
3604
3605 /* The size of patch header is 30 bytes, should be skip */
Wei Yongjun5ee63102019-07-10 06:12:22 +00003606 if (fw_size < 30) {
3607 err = -EINVAL;
Sean Wanga1c49c432019-06-02 08:02:48 +08003608 goto err_release_fw;
Wei Yongjun5ee63102019-07-10 06:12:22 +00003609 }
Sean Wanga1c49c432019-06-02 08:02:48 +08003610
3611 fw_size -= 30;
3612 fw_ptr += 30;
3613 flag = 1;
3614
3615 wmt_params.op = BTMTK_WMT_PATCH_DWNLD;
3616 wmt_params.status = NULL;
3617
3618 while (fw_size > 0) {
3619 dlen = min_t(int, 250, fw_size);
3620
Jupeng Zhonga297f562021-02-03 22:28:46 +08003621 /* Tell device the position in sequence */
Sean Wanga1c49c432019-06-02 08:02:48 +08003622 if (fw_size - dlen <= 0)
3623 flag = 3;
3624 else if (fw_size < fw->size - 30)
3625 flag = 2;
3626
3627 wmt_params.flag = flag;
3628 wmt_params.dlen = dlen;
3629 wmt_params.data = fw_ptr;
3630
3631 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3632 if (err < 0) {
3633 bt_dev_err(hdev, "Failed to send wmt patch dwnld (%d)",
3634 err);
3635 goto err_release_fw;
3636 }
3637
3638 fw_size -= dlen;
3639 fw_ptr += dlen;
3640 }
3641
3642 wmt_params.op = BTMTK_WMT_RST;
3643 wmt_params.flag = 4;
3644 wmt_params.dlen = 0;
3645 wmt_params.data = NULL;
3646 wmt_params.status = NULL;
3647
3648 /* Activate funciton the firmware providing to */
3649 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3650 if (err < 0) {
3651 bt_dev_err(hdev, "Failed to send wmt rst (%d)", err);
Colin Ian King3168c192019-11-29 17:36:35 +00003652 goto err_release_fw;
Sean Wanga1c49c432019-06-02 08:02:48 +08003653 }
3654
3655 /* Wait a few moments for firmware activation done */
3656 usleep_range(10000, 12000);
3657
3658err_release_fw:
3659 release_firmware(fw);
3660
3661 return err;
3662}
3663
3664static int btusb_mtk_func_query(struct hci_dev *hdev)
3665{
3666 struct btmtk_hci_wmt_params wmt_params;
3667 int status, err;
3668 u8 param = 0;
3669
3670 /* Query whether the function is enabled */
3671 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
3672 wmt_params.flag = 4;
3673 wmt_params.dlen = sizeof(param);
3674 wmt_params.data = &param;
3675 wmt_params.status = &status;
3676
3677 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3678 if (err < 0) {
3679 bt_dev_err(hdev, "Failed to query function status (%d)", err);
3680 return err;
3681 }
3682
3683 return status;
3684}
3685
3686static int btusb_mtk_reg_read(struct btusb_data *data, u32 reg, u32 *val)
3687{
3688 int pipe, err, size = sizeof(u32);
3689 void *buf;
3690
3691 buf = kzalloc(size, GFP_KERNEL);
3692 if (!buf)
3693 return -ENOMEM;
3694
3695 pipe = usb_rcvctrlpipe(data->udev, 0);
3696 err = usb_control_msg(data->udev, pipe, 0x63,
3697 USB_TYPE_VENDOR | USB_DIR_IN,
3698 reg >> 16, reg & 0xffff,
3699 buf, size, USB_CTRL_SET_TIMEOUT);
3700 if (err < 0)
3701 goto err_free_buf;
3702
3703 *val = get_unaligned_le32(buf);
3704
3705err_free_buf:
3706 kfree(buf);
3707
3708 return err;
3709}
3710
Mark Chen48c13302021-02-02 18:26:17 +08003711static int btusb_mtk_id_get(struct btusb_data *data, u32 reg, u32 *id)
Sean Wanga1c49c432019-06-02 08:02:48 +08003712{
Mark Chen48c13302021-02-02 18:26:17 +08003713 return btusb_mtk_reg_read(data, reg, id);
Sean Wanga1c49c432019-06-02 08:02:48 +08003714}
3715
3716static int btusb_mtk_setup(struct hci_dev *hdev)
3717{
3718 struct btusb_data *data = hci_get_drvdata(hdev);
3719 struct btmtk_hci_wmt_params wmt_params;
3720 ktime_t calltime, delta, rettime;
3721 struct btmtk_tci_sleep tci_sleep;
3722 unsigned long long duration;
3723 struct sk_buff *skb;
3724 const char *fwname;
3725 int err, status;
3726 u32 dev_id;
Mark Chenfc342c42021-02-02 18:26:18 +08003727 char fw_bin_name[64];
mark-yw.chen201cf392021-02-23 14:27:40 +08003728 u32 fw_version = 0;
Sean Wanga1c49c432019-06-02 08:02:48 +08003729 u8 param;
3730
3731 calltime = ktime_get();
3732
Mark Chen48c13302021-02-02 18:26:17 +08003733 err = btusb_mtk_id_get(data, 0x80000008, &dev_id);
Sean Wanga1c49c432019-06-02 08:02:48 +08003734 if (err < 0) {
3735 bt_dev_err(hdev, "Failed to get device id (%d)", err);
3736 return err;
3737 }
3738
Mark Chenfc342c42021-02-02 18:26:18 +08003739 if (!dev_id) {
3740 err = btusb_mtk_id_get(data, 0x70010200, &dev_id);
3741 if (err < 0) {
3742 bt_dev_err(hdev, "Failed to get device id (%d)", err);
3743 return err;
3744 }
3745 err = btusb_mtk_id_get(data, 0x80021004, &fw_version);
3746 if (err < 0) {
3747 bt_dev_err(hdev, "Failed to get fw version (%d)", err);
3748 return err;
3749 }
3750 }
3751
Sean Wanga1c49c432019-06-02 08:02:48 +08003752 switch (dev_id) {
Sean Wang9ce67c32019-06-02 08:02:49 +08003753 case 0x7663:
3754 fwname = FIRMWARE_MT7663;
3755 break;
Sean Wanga1c49c432019-06-02 08:02:48 +08003756 case 0x7668:
3757 fwname = FIRMWARE_MT7668;
3758 break;
Mark Chenfc342c42021-02-02 18:26:18 +08003759 case 0x7961:
3760 snprintf(fw_bin_name, sizeof(fw_bin_name),
3761 "mediatek/BT_RAM_CODE_MT%04x_1_%x_hdr.bin",
3762 dev_id & 0xffff, (fw_version & 0xff) + 1);
3763 err = btusb_mtk_setup_firmware_79xx(hdev, fw_bin_name);
3764
3765 /* Enable Bluetooth protocol */
3766 param = 1;
3767 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
3768 wmt_params.flag = 0;
3769 wmt_params.dlen = sizeof(param);
3770 wmt_params.data = &param;
3771 wmt_params.status = NULL;
3772
3773 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3774 if (err < 0) {
3775 bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
3776 return err;
3777 }
mark-yw.chen654e6f72021-08-02 20:59:41 +08003778
3779 hci_set_msft_opcode(hdev, 0xFD30);
Mark Chenfc342c42021-02-02 18:26:18 +08003780 goto done;
Sean Wanga1c49c432019-06-02 08:02:48 +08003781 default:
Jupeng Zhonga297f562021-02-03 22:28:46 +08003782 bt_dev_err(hdev, "Unsupported hardware variant (%08x)",
Sean Wanga1c49c432019-06-02 08:02:48 +08003783 dev_id);
3784 return -ENODEV;
3785 }
3786
3787 /* Query whether the firmware is already download */
3788 wmt_params.op = BTMTK_WMT_SEMAPHORE;
3789 wmt_params.flag = 1;
3790 wmt_params.dlen = 0;
3791 wmt_params.data = NULL;
3792 wmt_params.status = &status;
3793
3794 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3795 if (err < 0) {
3796 bt_dev_err(hdev, "Failed to query firmware status (%d)", err);
3797 return err;
3798 }
3799
3800 if (status == BTMTK_WMT_PATCH_DONE) {
3801 bt_dev_info(hdev, "firmware already downloaded");
3802 goto ignore_setup_fw;
3803 }
3804
3805 /* Setup a firmware which the device definitely requires */
3806 err = btusb_mtk_setup_firmware(hdev, fwname);
3807 if (err < 0)
3808 return err;
3809
3810ignore_setup_fw:
3811 err = readx_poll_timeout(btusb_mtk_func_query, hdev, status,
3812 status < 0 || status != BTMTK_WMT_ON_PROGRESS,
3813 2000, 5000000);
3814 /* -ETIMEDOUT happens */
3815 if (err < 0)
3816 return err;
3817
3818 /* The other errors happen in btusb_mtk_func_query */
3819 if (status < 0)
3820 return status;
3821
3822 if (status == BTMTK_WMT_ON_DONE) {
3823 bt_dev_info(hdev, "function already on");
3824 goto ignore_func_on;
3825 }
3826
3827 /* Enable Bluetooth protocol */
3828 param = 1;
3829 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
3830 wmt_params.flag = 0;
3831 wmt_params.dlen = sizeof(param);
3832 wmt_params.data = &param;
3833 wmt_params.status = NULL;
3834
3835 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3836 if (err < 0) {
3837 bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
3838 return err;
3839 }
3840
3841ignore_func_on:
3842 /* Apply the low power environment setup */
3843 tci_sleep.mode = 0x5;
3844 tci_sleep.duration = cpu_to_le16(0x640);
3845 tci_sleep.host_duration = cpu_to_le16(0x640);
3846 tci_sleep.host_wakeup_pin = 0;
3847 tci_sleep.time_compensation = 0;
3848
3849 skb = __hci_cmd_sync(hdev, 0xfc7a, sizeof(tci_sleep), &tci_sleep,
3850 HCI_INIT_TIMEOUT);
3851 if (IS_ERR(skb)) {
3852 err = PTR_ERR(skb);
3853 bt_dev_err(hdev, "Failed to apply low power setting (%d)", err);
3854 return err;
3855 }
3856 kfree_skb(skb);
3857
Mark Chenfc342c42021-02-02 18:26:18 +08003858done:
Sean Wanga1c49c432019-06-02 08:02:48 +08003859 rettime = ktime_get();
3860 delta = ktime_sub(rettime, calltime);
3861 duration = (unsigned long long)ktime_to_ns(delta) >> 10;
3862
3863 bt_dev_info(hdev, "Device setup in %llu usecs", duration);
3864
3865 return 0;
3866}
3867
3868static int btusb_mtk_shutdown(struct hci_dev *hdev)
3869{
3870 struct btmtk_hci_wmt_params wmt_params;
3871 u8 param = 0;
3872 int err;
3873
3874 /* Disable the device */
3875 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
3876 wmt_params.flag = 0;
3877 wmt_params.dlen = sizeof(param);
3878 wmt_params.data = &param;
3879 wmt_params.status = NULL;
3880
3881 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3882 if (err < 0) {
3883 bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
3884 return err;
3885 }
3886
3887 return 0;
3888}
3889
Sean Wang9ce67c32019-06-02 08:02:49 +08003890MODULE_FIRMWARE(FIRMWARE_MT7663);
Sean Wanga1c49c432019-06-02 08:02:48 +08003891MODULE_FIRMWARE(FIRMWARE_MT7668);
Sean Wanga1c49c432019-06-02 08:02:48 +08003892
Rajat Jaina4ccc9e2017-02-01 14:24:10 -08003893#ifdef CONFIG_PM
3894/* Configure an out-of-band gpio as wake-up pin, if specified in device tree */
3895static int marvell_config_oob_wake(struct hci_dev *hdev)
3896{
3897 struct sk_buff *skb;
3898 struct btusb_data *data = hci_get_drvdata(hdev);
3899 struct device *dev = &data->udev->dev;
3900 u16 pin, gap, opcode;
3901 int ret;
3902 u8 cmd[5];
3903
3904 /* Move on if no wakeup pin specified */
3905 if (of_property_read_u16(dev->of_node, "marvell,wakeup-pin", &pin) ||
3906 of_property_read_u16(dev->of_node, "marvell,wakeup-gap-ms", &gap))
3907 return 0;
3908
3909 /* Vendor specific command to configure a GPIO as wake-up pin */
3910 opcode = hci_opcode_pack(0x3F, 0x59);
3911 cmd[0] = opcode & 0xFF;
3912 cmd[1] = opcode >> 8;
3913 cmd[2] = 2; /* length of parameters that follow */
3914 cmd[3] = pin;
3915 cmd[4] = gap; /* time in ms, for which wakeup pin should be asserted */
3916
3917 skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL);
3918 if (!skb) {
Joe Perchesd84fc2c2020-12-22 02:31:49 -08003919 bt_dev_err(hdev, "%s: No memory", __func__);
Rajat Jaina4ccc9e2017-02-01 14:24:10 -08003920 return -ENOMEM;
3921 }
3922
Johannes Berg59ae1d12017-06-16 14:29:20 +02003923 skb_put_data(skb, cmd, sizeof(cmd));
Rajat Jaina4ccc9e2017-02-01 14:24:10 -08003924 hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
3925
3926 ret = btusb_send_frame(hdev, skb);
3927 if (ret) {
Joe Perchesd84fc2c2020-12-22 02:31:49 -08003928 bt_dev_err(hdev, "%s: configuration failed", __func__);
Rajat Jaina4ccc9e2017-02-01 14:24:10 -08003929 kfree_skb(skb);
3930 return ret;
3931 }
3932
3933 return 0;
3934}
3935#endif
3936
Amitkumar Karwarae8df492014-07-18 14:47:06 -07003937static int btusb_set_bdaddr_marvell(struct hci_dev *hdev,
3938 const bdaddr_t *bdaddr)
3939{
3940 struct sk_buff *skb;
3941 u8 buf[8];
3942 long ret;
3943
3944 buf[0] = 0xfe;
3945 buf[1] = sizeof(bdaddr_t);
3946 memcpy(buf + 2, bdaddr, sizeof(bdaddr_t));
3947
3948 skb = __hci_cmd_sync(hdev, 0xfc22, sizeof(buf), buf, HCI_INIT_TIMEOUT);
3949 if (IS_ERR(skb)) {
3950 ret = PTR_ERR(skb);
Marcel Holtmann2064ee32017-10-30 10:42:59 +01003951 bt_dev_err(hdev, "changing Marvell device address failed (%ld)",
3952 ret);
Amitkumar Karwarae8df492014-07-18 14:47:06 -07003953 return ret;
3954 }
3955 kfree_skb(skb);
3956
3957 return 0;
3958}
3959
Toshi Kikuchi58592232014-12-12 10:58:05 -08003960static int btusb_set_bdaddr_ath3012(struct hci_dev *hdev,
3961 const bdaddr_t *bdaddr)
3962{
3963 struct sk_buff *skb;
3964 u8 buf[10];
3965 long ret;
3966
3967 buf[0] = 0x01;
3968 buf[1] = 0x01;
3969 buf[2] = 0x00;
3970 buf[3] = sizeof(bdaddr_t);
3971 memcpy(buf + 4, bdaddr, sizeof(bdaddr_t));
3972
3973 skb = __hci_cmd_sync(hdev, 0xfc0b, sizeof(buf), buf, HCI_INIT_TIMEOUT);
3974 if (IS_ERR(skb)) {
3975 ret = PTR_ERR(skb);
Marcel Holtmann2064ee32017-10-30 10:42:59 +01003976 bt_dev_err(hdev, "Change address command failed (%ld)", ret);
Toshi Kikuchi58592232014-12-12 10:58:05 -08003977 return ret;
3978 }
3979 kfree_skb(skb);
3980
3981 return 0;
3982}
3983
Rocky Liaob40f58b2020-09-29 12:23:51 +08003984static int btusb_set_bdaddr_wcn6855(struct hci_dev *hdev,
3985 const bdaddr_t *bdaddr)
3986{
3987 struct sk_buff *skb;
3988 u8 buf[6];
3989 long ret;
3990
3991 memcpy(buf, bdaddr, sizeof(bdaddr_t));
3992
3993 skb = __hci_cmd_sync_ev(hdev, 0xfc14, sizeof(buf), buf,
3994 HCI_EV_CMD_COMPLETE, HCI_INIT_TIMEOUT);
3995 if (IS_ERR(skb)) {
3996 ret = PTR_ERR(skb);
3997 bt_dev_err(hdev, "Change address command failed (%ld)", ret);
3998 return ret;
3999 }
4000 kfree_skb(skb);
4001
4002 return 0;
4003}
4004
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00004005#define QCA_DFU_PACKET_LEN 4096
4006
4007#define QCA_GET_TARGET_VERSION 0x09
4008#define QCA_CHECK_STATUS 0x05
4009#define QCA_DFU_DOWNLOAD 0x01
4010
4011#define QCA_SYSCFG_UPDATED 0x40
4012#define QCA_PATCH_UPDATED 0x80
4013#define QCA_DFU_TIMEOUT 3000
Tim Jiangef2862a2020-12-03 12:57:14 +08004014#define QCA_FLAG_MULTI_NVM 0x80
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00004015
4016struct qca_version {
4017 __le32 rom_version;
4018 __le32 patch_version;
4019 __le32 ram_version;
Tim Jiangef2862a2020-12-03 12:57:14 +08004020 __le16 board_id;
4021 __le16 flag;
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00004022 __u8 reserved[4];
4023} __packed;
4024
4025struct qca_rampatch_version {
Rocky Liaob40f58b2020-09-29 12:23:51 +08004026 __le16 rom_version_high;
4027 __le16 rom_version_low;
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00004028 __le16 patch_version;
4029} __packed;
4030
4031struct qca_device_info {
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00004032 u32 rom_version;
4033 u8 rampatch_hdr; /* length of header in rampatch */
4034 u8 nvm_hdr; /* length of header in NVM */
4035 u8 ver_offset; /* offset of version structure in rampatch */
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00004036};
4037
4038static const struct qca_device_info qca_devices_table[] = {
Rocky Liaob40f58b2020-09-29 12:23:51 +08004039 { 0x00000100, 20, 4, 8 }, /* Rome 1.0 */
4040 { 0x00000101, 20, 4, 8 }, /* Rome 1.1 */
4041 { 0x00000200, 28, 4, 16 }, /* Rome 2.0 */
4042 { 0x00000201, 28, 4, 16 }, /* Rome 2.1 */
4043 { 0x00000300, 28, 4, 16 }, /* Rome 3.0 */
4044 { 0x00000302, 28, 4, 16 }, /* Rome 3.2 */
4045 { 0x00130100, 40, 4, 16 }, /* WCN6855 1.0 */
4046 { 0x00130200, 40, 4, 16 }, /* WCN6855 2.0 */
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00004047};
4048
Takashi Iwai803cdb82018-05-21 22:34:52 +02004049static int btusb_qca_send_vendor_req(struct usb_device *udev, u8 request,
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00004050 void *data, u16 size)
4051{
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00004052 int pipe, err;
4053 u8 *buf;
4054
4055 buf = kmalloc(size, GFP_KERNEL);
4056 if (!buf)
4057 return -ENOMEM;
4058
4059 /* Found some of USB hosts have IOT issues with ours so that we should
4060 * not wait until HCI layer is ready.
4061 */
4062 pipe = usb_rcvctrlpipe(udev, 0);
4063 err = usb_control_msg(udev, pipe, request, USB_TYPE_VENDOR | USB_DIR_IN,
4064 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
4065 if (err < 0) {
Takashi Iwai803cdb82018-05-21 22:34:52 +02004066 dev_err(&udev->dev, "Failed to access otp area (%d)", err);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00004067 goto done;
4068 }
4069
4070 memcpy(data, buf, size);
4071
4072done:
4073 kfree(buf);
4074
4075 return err;
4076}
4077
4078static int btusb_setup_qca_download_fw(struct hci_dev *hdev,
4079 const struct firmware *firmware,
4080 size_t hdr_size)
4081{
4082 struct btusb_data *btdata = hci_get_drvdata(hdev);
4083 struct usb_device *udev = btdata->udev;
4084 size_t count, size, sent = 0;
4085 int pipe, len, err;
4086 u8 *buf;
4087
4088 buf = kmalloc(QCA_DFU_PACKET_LEN, GFP_KERNEL);
4089 if (!buf)
4090 return -ENOMEM;
4091
4092 count = firmware->size;
4093
4094 size = min_t(size_t, count, hdr_size);
4095 memcpy(buf, firmware->data, size);
4096
4097 /* USB patches should go down to controller through USB path
4098 * because binary format fits to go down through USB channel.
4099 * USB control path is for patching headers and USB bulk is for
4100 * patch body.
4101 */
4102 pipe = usb_sndctrlpipe(udev, 0);
4103 err = usb_control_msg(udev, pipe, QCA_DFU_DOWNLOAD, USB_TYPE_VENDOR,
4104 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
4105 if (err < 0) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01004106 bt_dev_err(hdev, "Failed to send headers (%d)", err);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00004107 goto done;
4108 }
4109
4110 sent += size;
4111 count -= size;
4112
Tim Jiang4f00bfb2021-06-01 17:57:10 +08004113 /* ep2 need time to switch from function acl to function dfu,
4114 * so we add 20ms delay here.
4115 */
4116 msleep(20);
4117
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00004118 while (count) {
4119 size = min_t(size_t, count, QCA_DFU_PACKET_LEN);
4120
4121 memcpy(buf, firmware->data + sent, size);
4122
4123 pipe = usb_sndbulkpipe(udev, 0x02);
4124 err = usb_bulk_msg(udev, pipe, buf, size, &len,
4125 QCA_DFU_TIMEOUT);
4126 if (err < 0) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01004127 bt_dev_err(hdev, "Failed to send body at %zd of %zd (%d)",
4128 sent, firmware->size, err);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00004129 break;
4130 }
4131
4132 if (size != len) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01004133 bt_dev_err(hdev, "Failed to get bulk buffer");
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00004134 err = -EILSEQ;
4135 break;
4136 }
4137
4138 sent += size;
4139 count -= size;
4140 }
4141
4142done:
4143 kfree(buf);
4144 return err;
4145}
4146
4147static int btusb_setup_qca_load_rampatch(struct hci_dev *hdev,
4148 struct qca_version *ver,
4149 const struct qca_device_info *info)
4150{
4151 struct qca_rampatch_version *rver;
4152 const struct firmware *fw;
Rocky Liaob40f58b2020-09-29 12:23:51 +08004153 u32 ver_rom, ver_patch, rver_rom;
4154 u16 rver_rom_low, rver_rom_high, rver_patch;
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00004155 char fwname[64];
4156 int err;
4157
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00004158 ver_rom = le32_to_cpu(ver->rom_version);
4159 ver_patch = le32_to_cpu(ver->patch_version);
4160
4161 snprintf(fwname, sizeof(fwname), "qca/rampatch_usb_%08x.bin", ver_rom);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00004162
4163 err = request_firmware(&fw, fwname, &hdev->dev);
4164 if (err) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01004165 bt_dev_err(hdev, "failed to request rampatch file: %s (%d)",
4166 fwname, err);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00004167 return err;
4168 }
4169
Marcel Holtmann2064ee32017-10-30 10:42:59 +01004170 bt_dev_info(hdev, "using rampatch file: %s", fwname);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00004171
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00004172 rver = (struct qca_rampatch_version *)(fw->data + info->ver_offset);
Rocky Liaob40f58b2020-09-29 12:23:51 +08004173 rver_rom_low = le16_to_cpu(rver->rom_version_low);
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00004174 rver_patch = le16_to_cpu(rver->patch_version);
4175
Rocky Liaob40f58b2020-09-29 12:23:51 +08004176 if (ver_rom & ~0xffffU) {
4177 rver_rom_high = le16_to_cpu(rver->rom_version_high);
4178 rver_rom = le32_to_cpu(rver_rom_high << 16 | rver_rom_low);
4179 } else {
4180 rver_rom = rver_rom_low;
4181 }
4182
Marcel Holtmann2064ee32017-10-30 10:42:59 +01004183 bt_dev_info(hdev, "QCA: patch rome 0x%x build 0x%x, "
4184 "firmware rome 0x%x build 0x%x",
4185 rver_rom, rver_patch, ver_rom, ver_patch);
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00004186
4187 if (rver_rom != ver_rom || rver_patch <= ver_patch) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01004188 bt_dev_err(hdev, "rampatch file version did not match with firmware");
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00004189 err = -EINVAL;
4190 goto done;
4191 }
4192
4193 err = btusb_setup_qca_download_fw(hdev, fw, info->rampatch_hdr);
4194
4195done:
4196 release_firmware(fw);
4197
4198 return err;
4199}
4200
4201static int btusb_setup_qca_load_nvm(struct hci_dev *hdev,
4202 struct qca_version *ver,
4203 const struct qca_device_info *info)
4204{
4205 const struct firmware *fw;
4206 char fwname[64];
4207 int err;
4208
Tim Jiangef2862a2020-12-03 12:57:14 +08004209 if (((ver->flag >> 8) & 0xff) == QCA_FLAG_MULTI_NVM) {
Tim Jiangca17a5c2021-06-01 17:57:25 +08004210 /* if boardid equal 0, use default nvm without surfix */
4211 if (le16_to_cpu(ver->board_id) == 0x0) {
4212 snprintf(fwname, sizeof(fwname), "qca/nvm_usb_%08x.bin",
4213 le32_to_cpu(ver->rom_version));
4214 } else {
4215 snprintf(fwname, sizeof(fwname), "qca/nvm_usb_%08x_%04x.bin",
4216 le32_to_cpu(ver->rom_version),
4217 le16_to_cpu(ver->board_id));
4218 }
Tim Jiangef2862a2020-12-03 12:57:14 +08004219 } else {
4220 snprintf(fwname, sizeof(fwname), "qca/nvm_usb_%08x.bin",
4221 le32_to_cpu(ver->rom_version));
4222 }
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00004223
4224 err = request_firmware(&fw, fwname, &hdev->dev);
4225 if (err) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01004226 bt_dev_err(hdev, "failed to request NVM file: %s (%d)",
4227 fwname, err);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00004228 return err;
4229 }
4230
Marcel Holtmann2064ee32017-10-30 10:42:59 +01004231 bt_dev_info(hdev, "using NVM file: %s", fwname);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00004232
4233 err = btusb_setup_qca_download_fw(hdev, fw, info->nvm_hdr);
4234
4235 release_firmware(fw);
4236
4237 return err;
4238}
4239
Takashi Iwai803cdb82018-05-21 22:34:52 +02004240/* identify the ROM version and check whether patches are needed */
4241static bool btusb_qca_need_patch(struct usb_device *udev)
4242{
4243 struct qca_version ver;
4244
4245 if (btusb_qca_send_vendor_req(udev, QCA_GET_TARGET_VERSION, &ver,
4246 sizeof(ver)) < 0)
4247 return false;
4248 /* only low ROM versions need patches */
4249 return !(le32_to_cpu(ver.rom_version) & ~0xffffU);
4250}
4251
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00004252static int btusb_setup_qca(struct hci_dev *hdev)
4253{
Takashi Iwai803cdb82018-05-21 22:34:52 +02004254 struct btusb_data *btdata = hci_get_drvdata(hdev);
4255 struct usb_device *udev = btdata->udev;
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00004256 const struct qca_device_info *info = NULL;
4257 struct qca_version ver;
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00004258 u32 ver_rom;
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00004259 u8 status;
4260 int i, err;
4261
Takashi Iwai803cdb82018-05-21 22:34:52 +02004262 err = btusb_qca_send_vendor_req(udev, QCA_GET_TARGET_VERSION, &ver,
Marcel Holtmanneb500422015-04-16 23:15:50 +02004263 sizeof(ver));
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00004264 if (err < 0)
4265 return err;
4266
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00004267 ver_rom = le32_to_cpu(ver.rom_version);
Takashi Iwai803cdb82018-05-21 22:34:52 +02004268
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00004269 for (i = 0; i < ARRAY_SIZE(qca_devices_table); i++) {
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00004270 if (ver_rom == qca_devices_table[i].rom_version)
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00004271 info = &qca_devices_table[i];
4272 }
4273 if (!info) {
Hui Wang234f4142021-02-08 13:02:37 +08004274 /* If the rom_version is not matched in the qca_devices_table
4275 * and the high ROM version is not zero, we assume this chip no
4276 * need to load the rampatch and nvm.
4277 */
4278 if (ver_rom & ~0xffffU)
4279 return 0;
4280
Marcel Holtmann2064ee32017-10-30 10:42:59 +01004281 bt_dev_err(hdev, "don't support firmware rome 0x%x", ver_rom);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00004282 return -ENODEV;
4283 }
4284
Takashi Iwai803cdb82018-05-21 22:34:52 +02004285 err = btusb_qca_send_vendor_req(udev, QCA_CHECK_STATUS, &status,
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00004286 sizeof(status));
4287 if (err < 0)
4288 return err;
4289
4290 if (!(status & QCA_PATCH_UPDATED)) {
4291 err = btusb_setup_qca_load_rampatch(hdev, &ver, info);
4292 if (err < 0)
4293 return err;
4294 }
4295
Tim Jiangef2862a2020-12-03 12:57:14 +08004296 err = btusb_qca_send_vendor_req(udev, QCA_GET_TARGET_VERSION, &ver,
4297 sizeof(ver));
4298 if (err < 0)
4299 return err;
4300
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00004301 if (!(status & QCA_SYSCFG_UPDATED)) {
4302 err = btusb_setup_qca_load_nvm(hdev, &ver, info);
4303 if (err < 0)
4304 return err;
4305 }
4306
4307 return 0;
4308}
4309
Marcel Holtmann9d08f502015-10-08 20:23:08 +02004310static inline int __set_diag_interface(struct hci_dev *hdev)
4311{
4312 struct btusb_data *data = hci_get_drvdata(hdev);
4313 struct usb_interface *intf = data->diag;
4314 int i;
4315
4316 if (!data->diag)
4317 return -ENODEV;
4318
4319 data->diag_tx_ep = NULL;
4320 data->diag_rx_ep = NULL;
4321
4322 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
4323 struct usb_endpoint_descriptor *ep_desc;
4324
4325 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
4326
4327 if (!data->diag_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
4328 data->diag_tx_ep = ep_desc;
4329 continue;
4330 }
4331
4332 if (!data->diag_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
4333 data->diag_rx_ep = ep_desc;
4334 continue;
4335 }
4336 }
4337
4338 if (!data->diag_tx_ep || !data->diag_rx_ep) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01004339 bt_dev_err(hdev, "invalid diagnostic descriptors");
Marcel Holtmann9d08f502015-10-08 20:23:08 +02004340 return -ENODEV;
4341 }
4342
4343 return 0;
4344}
4345
4346static struct urb *alloc_diag_urb(struct hci_dev *hdev, bool enable)
4347{
4348 struct btusb_data *data = hci_get_drvdata(hdev);
4349 struct sk_buff *skb;
4350 struct urb *urb;
4351 unsigned int pipe;
4352
4353 if (!data->diag_tx_ep)
4354 return ERR_PTR(-ENODEV);
4355
4356 urb = usb_alloc_urb(0, GFP_KERNEL);
4357 if (!urb)
4358 return ERR_PTR(-ENOMEM);
4359
4360 skb = bt_skb_alloc(2, GFP_KERNEL);
4361 if (!skb) {
4362 usb_free_urb(urb);
4363 return ERR_PTR(-ENOMEM);
4364 }
4365
Johannes Berg634fef62017-06-16 14:29:24 +02004366 skb_put_u8(skb, 0xf0);
4367 skb_put_u8(skb, enable);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02004368
4369 pipe = usb_sndbulkpipe(data->udev, data->diag_tx_ep->bEndpointAddress);
4370
4371 usb_fill_bulk_urb(urb, data->udev, pipe,
4372 skb->data, skb->len, btusb_tx_complete, skb);
4373
4374 skb->dev = (void *)hdev;
4375
4376 return urb;
4377}
4378
4379static int btusb_bcm_set_diag(struct hci_dev *hdev, bool enable)
4380{
4381 struct btusb_data *data = hci_get_drvdata(hdev);
4382 struct urb *urb;
4383
4384 if (!data->diag)
4385 return -ENODEV;
4386
4387 if (!test_bit(HCI_RUNNING, &hdev->flags))
4388 return -ENETDOWN;
4389
4390 urb = alloc_diag_urb(hdev, enable);
4391 if (IS_ERR(urb))
4392 return PTR_ERR(urb);
4393
4394 return submit_or_queue_tx_urb(hdev, urb);
4395}
Marcel Holtmann9d08f502015-10-08 20:23:08 +02004396
Rajat Jainfd913ef2017-02-01 14:24:09 -08004397#ifdef CONFIG_PM
4398static irqreturn_t btusb_oob_wake_handler(int irq, void *priv)
4399{
4400 struct btusb_data *data = priv;
4401
4402 pm_wakeup_event(&data->udev->dev, 0);
Jeffy Chen017789f2017-02-24 14:24:29 +08004403 pm_system_wakeup();
Rajat Jainfd913ef2017-02-01 14:24:09 -08004404
4405 /* Disable only if not already disabled (keep it balanced) */
4406 if (test_and_clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags)) {
4407 disable_irq_nosync(irq);
4408 disable_irq_wake(irq);
4409 }
4410 return IRQ_HANDLED;
4411}
4412
4413static const struct of_device_id btusb_match_table[] = {
4414 { .compatible = "usb1286,204e" },
Brian Norris4c409af2019-02-22 14:53:43 -08004415 { .compatible = "usbcf3,e300" }, /* QCA6174A */
4416 { .compatible = "usb4ca,301a" }, /* QCA6174A (Lite-On) */
Rajat Jainfd913ef2017-02-01 14:24:09 -08004417 { }
4418};
4419MODULE_DEVICE_TABLE(of, btusb_match_table);
4420
4421/* Use an oob wakeup pin? */
4422static int btusb_config_oob_wake(struct hci_dev *hdev)
4423{
4424 struct btusb_data *data = hci_get_drvdata(hdev);
4425 struct device *dev = &data->udev->dev;
4426 int irq, ret;
4427
4428 clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags);
4429
4430 if (!of_match_device(btusb_match_table, dev))
4431 return 0;
4432
4433 /* Move on if no IRQ specified */
4434 irq = of_irq_get_byname(dev->of_node, "wakeup");
4435 if (irq <= 0) {
4436 bt_dev_dbg(hdev, "%s: no OOB Wakeup IRQ in DT", __func__);
4437 return 0;
4438 }
4439
Brian Norris771acc72019-04-09 11:49:17 -07004440 irq_set_status_flags(irq, IRQ_NOAUTOEN);
Rajat Jainfd913ef2017-02-01 14:24:09 -08004441 ret = devm_request_irq(&hdev->dev, irq, btusb_oob_wake_handler,
4442 0, "OOB Wake-on-BT", data);
4443 if (ret) {
4444 bt_dev_err(hdev, "%s: IRQ request failed", __func__);
4445 return ret;
4446 }
4447
4448 ret = device_init_wakeup(dev, true);
4449 if (ret) {
4450 bt_dev_err(hdev, "%s: failed to init_wakeup", __func__);
4451 return ret;
4452 }
4453
4454 data->oob_wake_irq = irq;
Rajat Jainfd913ef2017-02-01 14:24:09 -08004455 bt_dev_info(hdev, "OOB Wake-on-BT configured at IRQ %u", irq);
4456 return 0;
4457}
4458#endif
4459
Hans de Goedefc549102018-04-27 11:26:43 +02004460static void btusb_check_needs_reset_resume(struct usb_interface *intf)
4461{
4462 if (dmi_check_system(btusb_needs_reset_resume_table))
4463 interface_to_usbdev(intf)->quirks |= USB_QUIRK_RESET_RESUME;
4464}
4465
Abhishek Pandit-Subedib7d0bf12020-05-12 19:19:27 -07004466static bool btusb_prevent_wake(struct hci_dev *hdev)
4467{
4468 struct btusb_data *data = hci_get_drvdata(hdev);
4469
Abhishek Pandit-Subedi34a68652020-06-24 11:24:30 -07004470 if (test_bit(BTUSB_WAKEUP_DISABLE, &data->flags))
4471 return true;
4472
Abhishek Pandit-Subedib7d0bf12020-05-12 19:19:27 -07004473 return !device_may_wakeup(&data->udev->dev);
4474}
4475
Tim Jiang3b0d5252020-12-18 18:12:11 +08004476static int btusb_shutdown_qca(struct hci_dev *hdev)
4477{
4478 struct sk_buff *skb;
4479
4480 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
4481 if (IS_ERR(skb)) {
4482 bt_dev_err(hdev, "HCI reset during shutdown failed");
4483 return PTR_ERR(skb);
4484 }
4485 kfree_skb(skb);
4486
4487 return 0;
4488}
4489
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004490static int btusb_probe(struct usb_interface *intf,
Marcel Holtmann89e75332014-09-16 04:44:50 +02004491 const struct usb_device_id *id)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004492{
4493 struct usb_endpoint_descriptor *ep_desc;
Rajat Jaindc786b22019-01-24 15:28:14 -08004494 struct gpio_desc *reset_gpio;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004495 struct btusb_data *data;
4496 struct hci_dev *hdev;
Marcel Holtmann22f8e9d2015-10-20 00:53:33 +02004497 unsigned ifnum_base;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004498 int i, err;
4499
4500 BT_DBG("intf %p id %p", intf, id);
4501
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02004502 /* interface numbers are hardcoded in the spec */
Marcel Holtmann22f8e9d2015-10-20 00:53:33 +02004503 if (intf->cur_altsetting->desc.bInterfaceNumber != 0) {
4504 if (!(id->driver_info & BTUSB_IFNUM_2))
4505 return -ENODEV;
4506 if (intf->cur_altsetting->desc.bInterfaceNumber != 2)
4507 return -ENODEV;
4508 }
4509
4510 ifnum_base = intf->cur_altsetting->desc.bInterfaceNumber;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004511
4512 if (!id->driver_info) {
4513 const struct usb_device_id *match;
Marcel Holtmann89e75332014-09-16 04:44:50 +02004514
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004515 match = usb_match_id(intf, blacklist_table);
4516 if (match)
4517 id = match;
4518 }
4519
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02004520 if (id->driver_info == BTUSB_IGNORE)
4521 return -ENODEV;
4522
Steven.Li2d25f8b2011-07-01 14:02:36 +08004523 if (id->driver_info & BTUSB_ATH3012) {
4524 struct usb_device *udev = interface_to_usbdev(intf);
4525
4526 /* Old firmware would otherwise let ath3k driver load
Derek Robsond98422c2017-07-22 13:47:07 +12004527 * patch and sysconfig files
4528 */
Takashi Iwai803cdb82018-05-21 22:34:52 +02004529 if (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x0001 &&
4530 !btusb_qca_need_patch(udev))
Steven.Li2d25f8b2011-07-01 14:02:36 +08004531 return -ENODEV;
4532 }
4533
Sachin Kamat98921db2012-07-27 12:38:39 +05304534 data = devm_kzalloc(&intf->dev, sizeof(*data), GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004535 if (!data)
4536 return -ENOMEM;
4537
4538 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
4539 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
4540
4541 if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
4542 data->intr_ep = ep_desc;
4543 continue;
4544 }
4545
4546 if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
4547 data->bulk_tx_ep = ep_desc;
4548 continue;
4549 }
4550
4551 if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
4552 data->bulk_rx_ep = ep_desc;
4553 continue;
4554 }
4555 }
4556
Sachin Kamat98921db2012-07-27 12:38:39 +05304557 if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004558 return -ENODEV;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004559
Marcel Holtmann893ba542015-01-28 20:27:34 -08004560 if (id->driver_info & BTUSB_AMP) {
4561 data->cmdreq_type = USB_TYPE_CLASS | 0x01;
4562 data->cmdreq = 0x2b;
4563 } else {
4564 data->cmdreq_type = USB_TYPE_CLASS;
4565 data->cmdreq = 0x00;
4566 }
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01004567
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004568 data->udev = interface_to_usbdev(intf);
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02004569 data->intf = intf;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004570
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004571 INIT_WORK(&data->work, btusb_work);
Oliver Neukum7bee5492009-08-24 23:44:59 +02004572 INIT_WORK(&data->waker, btusb_waker);
Marcel Holtmann803b5832014-09-16 08:00:29 +02004573 init_usb_anchor(&data->deferred);
4574 init_usb_anchor(&data->tx_anchor);
Oliver Neukum7bee5492009-08-24 23:44:59 +02004575 spin_lock_init(&data->txlock);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004576
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004577 init_usb_anchor(&data->intr_anchor);
4578 init_usb_anchor(&data->bulk_anchor);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02004579 init_usb_anchor(&data->isoc_anchor);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02004580 init_usb_anchor(&data->diag_anchor);
Sean Wanga1c49c432019-06-02 08:02:48 +08004581 init_usb_anchor(&data->ctrl_anchor);
Marcel Holtmann803b5832014-09-16 08:00:29 +02004582 spin_lock_init(&data->rxlock);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004583
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08004584 if (id->driver_info & BTUSB_INTEL_NEW) {
4585 data->recv_event = btusb_recv_event_intel;
4586 data->recv_bulk = btusb_recv_bulk_intel;
4587 set_bit(BTUSB_BOOTLOADER, &data->flags);
4588 } else {
4589 data->recv_event = hci_recv_frame;
4590 data->recv_bulk = btusb_recv_bulk;
4591 }
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +01004592
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004593 hdev = hci_alloc_dev();
Sachin Kamat98921db2012-07-27 12:38:39 +05304594 if (!hdev)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004595 return -ENOMEM;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004596
Marcel Holtmannc13854c2010-02-08 15:27:07 +01004597 hdev->bus = HCI_USB;
David Herrmann155961e2012-02-09 21:58:32 +01004598 hci_set_drvdata(hdev, data);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004599
Marcel Holtmann893ba542015-01-28 20:27:34 -08004600 if (id->driver_info & BTUSB_AMP)
4601 hdev->dev_type = HCI_AMP;
4602 else
Marcel Holtmannca8bee52016-07-05 14:30:14 +02004603 hdev->dev_type = HCI_PRIMARY;
Marcel Holtmann893ba542015-01-28 20:27:34 -08004604
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004605 data->hdev = hdev;
4606
4607 SET_HCIDEV_DEV(hdev, &intf->dev);
4608
Rajat Jaindc786b22019-01-24 15:28:14 -08004609 reset_gpio = gpiod_get_optional(&data->udev->dev, "reset",
4610 GPIOD_OUT_LOW);
4611 if (IS_ERR(reset_gpio)) {
4612 err = PTR_ERR(reset_gpio);
4613 goto out_free_dev;
4614 } else if (reset_gpio) {
4615 data->reset_gpio = reset_gpio;
4616 }
4617
Marcel Holtmann9f8f9622013-04-10 08:11:35 -07004618 hdev->open = btusb_open;
4619 hdev->close = btusb_close;
4620 hdev->flush = btusb_flush;
4621 hdev->send = btusb_send_frame;
4622 hdev->notify = btusb_notify;
Abhishek Pandit-Subedib7d0bf12020-05-12 19:19:27 -07004623 hdev->prevent_wake = btusb_prevent_wake;
Marcel Holtmann9f8f9622013-04-10 08:11:35 -07004624
Rajat Jainfd913ef2017-02-01 14:24:09 -08004625#ifdef CONFIG_PM
4626 err = btusb_config_oob_wake(hdev);
4627 if (err)
4628 goto out_free_dev;
Rajat Jaina4ccc9e2017-02-01 14:24:10 -08004629
4630 /* Marvell devices may need a specific chip configuration */
4631 if (id->driver_info & BTUSB_MARVELL && data->oob_wake_irq) {
4632 err = marvell_config_oob_wake(hdev);
4633 if (err)
4634 goto out_free_dev;
4635 }
Rajat Jainfd913ef2017-02-01 14:24:09 -08004636#endif
Szymon Janc418678b2016-09-01 17:22:37 +02004637 if (id->driver_info & BTUSB_CW6622)
4638 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
4639
Marcel Holtmann6c9d4352015-10-17 14:39:27 +02004640 if (id->driver_info & BTUSB_BCM2045)
4641 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
4642
Marcel Holtmann9f8f9622013-04-10 08:11:35 -07004643 if (id->driver_info & BTUSB_BCM92035)
4644 hdev->setup = btusb_setup_bcm92035;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004645
Marcel Holtmannc0a21a52019-09-27 08:48:58 +02004646 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_BCM) &&
4647 (id->driver_info & BTUSB_BCM_PATCHRAM)) {
Marcel Holtmann49a5f782015-10-20 02:30:48 +02004648 hdev->manufacturer = 15;
Marcel Holtmannc2bfb102015-04-05 22:52:14 -07004649 hdev->setup = btbcm_setup_patchram;
Marcel Holtmann9d08f502015-10-08 20:23:08 +02004650 hdev->set_diag = btusb_bcm_set_diag;
Marcel Holtmann1df1f592015-04-05 22:52:11 -07004651 hdev->set_bdaddr = btbcm_set_bdaddr;
Marcel Holtmann9d08f502015-10-08 20:23:08 +02004652
4653 /* Broadcom LM_DIAG Interface numbers are hardcoded */
Marcel Holtmann22f8e9d2015-10-20 00:53:33 +02004654 data->diag = usb_ifnum_to_if(data->udev, ifnum_base + 2);
Marcel Holtmannabbaf502014-07-02 00:53:48 +02004655 }
Petri Gynther10d4c672014-05-08 15:50:01 -07004656
Marcel Holtmannc0a21a52019-09-27 08:48:58 +02004657 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_BCM) &&
4658 (id->driver_info & BTUSB_BCM_APPLE)) {
Marcel Holtmann49a5f782015-10-20 02:30:48 +02004659 hdev->manufacturer = 15;
Marcel Holtmannc2bfb102015-04-05 22:52:14 -07004660 hdev->setup = btbcm_setup_apple;
Marcel Holtmann9d08f502015-10-08 20:23:08 +02004661 hdev->set_diag = btusb_bcm_set_diag;
4662
4663 /* Broadcom LM_DIAG Interface numbers are hardcoded */
Marcel Holtmann22f8e9d2015-10-20 00:53:33 +02004664 data->diag = usb_ifnum_to_if(data->udev, ifnum_base + 2);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02004665 }
Marcel Holtmann17b27722015-03-22 15:52:38 +01004666
Marcel Holtmanncb8d6592014-07-02 11:25:25 +02004667 if (id->driver_info & BTUSB_INTEL) {
Marcel Holtmann49a5f782015-10-20 02:30:48 +02004668 hdev->manufacturer = 2;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07004669 hdev->setup = btusb_setup_intel;
Tedd Ho-Jeong Anbfbd45e2015-02-13 09:20:52 -08004670 hdev->shutdown = btusb_shutdown_intel;
Marcel Holtmann3e247672015-10-17 16:00:28 +02004671 hdev->set_diag = btintel_set_diag_mfg;
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07004672 hdev->set_bdaddr = btintel_set_bdaddr;
Rajat Jaindc786b22019-01-24 15:28:14 -08004673 hdev->cmd_timeout = btusb_intel_cmd_timeout;
Jakub Pawlowskic33fb9b2015-01-30 18:55:58 -08004674 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
Jakub Pawlowskic1154842015-03-17 09:04:16 -07004675 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
Marcel Holtmann3e247672015-10-17 16:00:28 +02004676 set_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks);
Marcel Holtmanncb8d6592014-07-02 11:25:25 +02004677 }
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07004678
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08004679 if (id->driver_info & BTUSB_INTEL_NEW) {
Marcel Holtmann49a5f782015-10-20 02:30:48 +02004680 hdev->manufacturer = 2;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08004681 hdev->send = btusb_send_frame_intel;
4682 hdev->setup = btusb_setup_intel_new;
Raghuram Hegde017a01c2019-01-29 17:54:48 +05304683 hdev->shutdown = btusb_shutdown_intel_new;
Marcel Holtmanneeb6abe2015-07-05 14:37:39 +02004684 hdev->hw_error = btintel_hw_error;
Marcel Holtmann6d2e50d2015-10-09 14:42:08 +02004685 hdev->set_diag = btintel_set_diag;
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07004686 hdev->set_bdaddr = btintel_set_bdaddr;
Rajat Jaindc786b22019-01-24 15:28:14 -08004687 hdev->cmd_timeout = btusb_intel_cmd_timeout;
Marcel Holtmannb970c5b2015-02-01 23:57:18 -08004688 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
Justin TerAvestf7785022018-09-25 11:04:57 -06004689 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
Marcel Holtmannd8270fb2015-10-17 16:00:27 +02004690 set_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08004691 }
4692
Kiran K0a3c1d42020-11-19 17:09:40 +05304693 if (id->driver_info & BTUSB_INTEL_NEWGEN) {
4694 hdev->manufacturer = 2;
4695 hdev->send = btusb_send_frame_intel;
4696 hdev->setup = btusb_setup_intel_newgen;
4697 hdev->shutdown = btusb_shutdown_intel_new;
4698 hdev->hw_error = btintel_hw_error;
4699 hdev->set_diag = btintel_set_diag;
4700 hdev->set_bdaddr = btintel_set_bdaddr;
4701 hdev->cmd_timeout = btusb_intel_cmd_timeout;
4702 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
4703 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
4704 set_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks);
4705
4706 data->recv_event = btusb_recv_event_intel;
4707 data->recv_bulk = btusb_recv_bulk_intel;
4708 set_bit(BTUSB_BOOTLOADER, &data->flags);
4709 }
4710
Amitkumar Karwarae8df492014-07-18 14:47:06 -07004711 if (id->driver_info & BTUSB_MARVELL)
4712 hdev->set_bdaddr = btusb_set_bdaddr_marvell;
4713
Marcel Holtmannc0a21a52019-09-27 08:48:58 +02004714 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_MTK) &&
4715 (id->driver_info & BTUSB_MEDIATEK)) {
Sean Wanga1c49c432019-06-02 08:02:48 +08004716 hdev->setup = btusb_mtk_setup;
4717 hdev->shutdown = btusb_mtk_shutdown;
4718 hdev->manufacturer = 70;
4719 set_bit(HCI_QUIRK_NON_PERSISTENT_SETUP, &hdev->quirks);
4720 }
Sean Wanga1c49c432019-06-02 08:02:48 +08004721
Marcel Holtmann661cf882015-01-02 23:35:20 -08004722 if (id->driver_info & BTUSB_SWAVE) {
4723 set_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks);
Marcel Holtmannd57dbe72014-12-26 04:42:33 +01004724 set_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS, &hdev->quirks);
Marcel Holtmann661cf882015-01-02 23:35:20 -08004725 }
Marcel Holtmannd57dbe72014-12-26 04:42:33 +01004726
Marcel Holtmanne4c534b2015-10-21 01:31:45 +02004727 if (id->driver_info & BTUSB_INTEL_BOOT) {
4728 hdev->manufacturer = 2;
Marcel Holtmann40df7832014-07-06 13:29:58 +02004729 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
Marcel Holtmanne4c534b2015-10-21 01:31:45 +02004730 }
Marcel Holtmann40df7832014-07-06 13:29:58 +02004731
Jakub Pawlowski79f0c872015-01-29 10:38:34 -08004732 if (id->driver_info & BTUSB_ATH3012) {
Takashi Iwai803cdb82018-05-21 22:34:52 +02004733 data->setup_on_usb = btusb_setup_qca;
Toshi Kikuchi58592232014-12-12 10:58:05 -08004734 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
Jakub Pawlowski3d50d512015-03-17 09:04:15 -07004735 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
Jakub Pawlowski79f0c872015-01-29 10:38:34 -08004736 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
4737 }
Toshi Kikuchi58592232014-12-12 10:58:05 -08004738
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00004739 if (id->driver_info & BTUSB_QCA_ROME) {
4740 data->setup_on_usb = btusb_setup_qca;
4741 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
Abhishek Pandit-Subedi69335682020-06-24 11:11:44 -07004742 hdev->cmd_timeout = btusb_qca_cmd_timeout;
Vic Wei96e58d32018-03-28 08:28:47 -07004743 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
Hans de Goedefc549102018-04-27 11:26:43 +02004744 btusb_check_needs_reset_resume(intf);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00004745 }
4746
Rocky Liaob40f58b2020-09-29 12:23:51 +08004747 if (id->driver_info & BTUSB_QCA_WCN6855) {
4748 data->setup_on_usb = btusb_setup_qca;
Tim Jiang3b0d5252020-12-18 18:12:11 +08004749 hdev->shutdown = btusb_shutdown_qca;
Rocky Liaob40f58b2020-09-29 12:23:51 +08004750 hdev->set_bdaddr = btusb_set_bdaddr_wcn6855;
4751 hdev->cmd_timeout = btusb_qca_cmd_timeout;
4752 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
Michael Suncbe6a042021-07-29 10:10:59 -07004753 hci_set_msft_opcode(hdev, 0xFD70);
Rocky Liaob40f58b2020-09-29 12:23:51 +08004754 }
4755
Hilda Wu461f95f2020-06-30 21:09:40 +08004756 if (id->driver_info & BTUSB_AMP) {
4757 /* AMP controllers do not support SCO packets */
4758 data->isoc = NULL;
4759 } else {
4760 /* Interface orders are hardcoded in the specification */
4761 data->isoc = usb_ifnum_to_if(data->udev, ifnum_base + 1);
4762 data->isoc_ifnum = ifnum_base + 1;
4763 }
4764
Arnd Bergmann42d22092019-09-18 21:59:02 +02004765 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_RTL) &&
4766 (id->driver_info & BTUSB_REALTEK)) {
Carlo Caionedb33c772015-05-14 10:49:09 +02004767 hdev->setup = btrtl_setup_realtek;
Jian-Hong Pan7af3f5582019-06-25 16:30:51 +08004768 hdev->shutdown = btrtl_shutdown_realtek;
Alex Lud7ef0d12019-09-05 10:36:31 +08004769 hdev->cmd_timeout = btusb_rtl_cmd_timeout;
Daniel Drake04b8c812015-05-21 08:23:50 -06004770
Alex Lu9e455242019-08-14 20:02:52 +08004771 /* Realtek devices lose their updated firmware over global
4772 * suspend that means host doesn't send SET_FEATURE
4773 * (DEVICE_REMOTE_WAKEUP)
Daniel Drake04b8c812015-05-21 08:23:50 -06004774 */
Alex Lu9e455242019-08-14 20:02:52 +08004775 set_bit(BTUSB_WAKEUP_DISABLE, &data->flags);
Daniel Drake04b8c812015-05-21 08:23:50 -06004776 }
Daniel Drakea2698a92015-04-16 14:09:55 -06004777
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01004778 if (!reset)
Szymon Janca6c511c2012-05-23 12:35:46 +02004779 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02004780
4781 if (force_scofix || id->driver_info & BTUSB_WRONG_SCO_MTU) {
4782 if (!disable_scofix)
4783 set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
4784 }
4785
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02004786 if (id->driver_info & BTUSB_BROKEN_ISOC)
4787 data->isoc = NULL;
4788
Alain Michaud4b127bd2020-02-27 18:29:39 +00004789 if (id->driver_info & BTUSB_WIDEBAND_SPEECH)
Alain Michaud00bce3f2020-03-05 16:14:59 +00004790 set_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED, &hdev->quirks);
Alain Michaud4b127bd2020-02-27 18:29:39 +00004791
Alain Michaudaff8c482020-04-23 14:43:31 +00004792 if (id->driver_info & BTUSB_VALID_LE_STATES)
4793 set_bit(HCI_QUIRK_VALID_LE_STATES, &hdev->quirks);
4794
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01004795 if (id->driver_info & BTUSB_DIGIANSWER) {
4796 data->cmdreq_type = USB_TYPE_VENDOR;
Szymon Janca6c511c2012-05-23 12:35:46 +02004797 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01004798 }
4799
4800 if (id->driver_info & BTUSB_CSR) {
4801 struct usb_device *udev = data->udev;
Marcel Holtmann81cac642014-01-03 03:02:36 -08004802 u16 bcdDevice = le16_to_cpu(udev->descriptor.bcdDevice);
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01004803
4804 /* Old firmware would otherwise execute USB reset */
Marcel Holtmann81cac642014-01-03 03:02:36 -08004805 if (bcdDevice < 0x117)
Szymon Janca6c511c2012-05-23 12:35:46 +02004806 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmann81cac642014-01-03 03:02:36 -08004807
Ismael Ferreras Morezuelascde1a8a2020-07-26 23:12:28 +02004808 /* This must be set first in case we disable it for fakes */
Jakub Pawlowski49c989a2015-03-17 09:04:17 -07004809 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
Ismael Ferreras Morezuelascde1a8a2020-07-26 23:12:28 +02004810
4811 /* Fake CSR devices with broken commands */
4812 if (le16_to_cpu(udev->descriptor.idVendor) == 0x0a12 &&
4813 le16_to_cpu(udev->descriptor.idProduct) == 0x0001)
4814 hdev->setup = btusb_setup_csr;
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01004815 }
4816
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02004817 if (id->driver_info & BTUSB_SNIFFER) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02004818 struct usb_device *udev = data->udev;
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02004819
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01004820 /* New sniffer firmware has crippled HCI interface */
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02004821 if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
4822 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
4823 }
4824
Marcel Holtmann3a5ef202014-07-06 14:53:54 +02004825 if (id->driver_info & BTUSB_INTEL_BOOT) {
4826 /* A bug in the bootloader causes that interrupt interface is
4827 * only enabled after receiving SetInterface(0, AltSetting=0).
4828 */
4829 err = usb_set_interface(data->udev, 0, 0);
4830 if (err < 0) {
4831 BT_ERR("failed to set interface 0, alt 0 %d", err);
Rajat Jain10ab1332017-02-01 14:24:08 -08004832 goto out_free_dev;
Marcel Holtmann3a5ef202014-07-06 14:53:54 +02004833 }
4834 }
4835
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02004836 if (data->isoc) {
4837 err = usb_driver_claim_interface(&btusb_driver,
Marcel Holtmann89e75332014-09-16 04:44:50 +02004838 data->isoc, data);
Rajat Jain10ab1332017-02-01 14:24:08 -08004839 if (err < 0)
4840 goto out_free_dev;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02004841 }
4842
Marcel Holtmannc0a21a52019-09-27 08:48:58 +02004843 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_BCM) && data->diag) {
Marcel Holtmann9d08f502015-10-08 20:23:08 +02004844 if (!usb_driver_claim_interface(&btusb_driver,
4845 data->diag, data))
4846 __set_diag_interface(hdev);
4847 else
4848 data->diag = NULL;
4849 }
Marcel Holtmann9d08f502015-10-08 20:23:08 +02004850
Hans de Goede89591522021-04-09 15:58:50 +02004851 if (enable_autosuspend)
4852 usb_enable_autosuspend(data->udev);
Hans de Goedeeff2d682017-11-13 14:44:16 +01004853
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004854 err = hci_register_dev(hdev);
Rajat Jain10ab1332017-02-01 14:24:08 -08004855 if (err < 0)
4856 goto out_free_dev;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004857
4858 usb_set_intfdata(intf, data);
4859
4860 return 0;
Rajat Jain10ab1332017-02-01 14:24:08 -08004861
4862out_free_dev:
Rajat Jaindc786b22019-01-24 15:28:14 -08004863 if (data->reset_gpio)
4864 gpiod_put(data->reset_gpio);
Rajat Jain10ab1332017-02-01 14:24:08 -08004865 hci_free_dev(hdev);
4866 return err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004867}
4868
4869static void btusb_disconnect(struct usb_interface *intf)
4870{
4871 struct btusb_data *data = usb_get_intfdata(intf);
4872 struct hci_dev *hdev;
4873
4874 BT_DBG("intf %p", intf);
4875
4876 if (!data)
4877 return;
4878
4879 hdev = data->hdev;
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02004880 usb_set_intfdata(data->intf, NULL);
4881
4882 if (data->isoc)
4883 usb_set_intfdata(data->isoc, NULL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004884
Marcel Holtmann9d08f502015-10-08 20:23:08 +02004885 if (data->diag)
4886 usb_set_intfdata(data->diag, NULL);
4887
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004888 hci_unregister_dev(hdev);
4889
Marcel Holtmann9d08f502015-10-08 20:23:08 +02004890 if (intf == data->intf) {
4891 if (data->isoc)
4892 usb_driver_release_interface(&btusb_driver, data->isoc);
4893 if (data->diag)
4894 usb_driver_release_interface(&btusb_driver, data->diag);
4895 } else if (intf == data->isoc) {
4896 if (data->diag)
4897 usb_driver_release_interface(&btusb_driver, data->diag);
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02004898 usb_driver_release_interface(&btusb_driver, data->intf);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02004899 } else if (intf == data->diag) {
4900 usb_driver_release_interface(&btusb_driver, data->intf);
4901 if (data->isoc)
4902 usb_driver_release_interface(&btusb_driver, data->isoc);
4903 }
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02004904
Rajat Jainfd913ef2017-02-01 14:24:09 -08004905 if (data->oob_wake_irq)
4906 device_init_wakeup(&data->udev->dev, false);
4907
Rajat Jaindc786b22019-01-24 15:28:14 -08004908 if (data->reset_gpio)
4909 gpiod_put(data->reset_gpio);
4910
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004911 hci_free_dev(hdev);
4912}
4913
Oliver Neukum7bee5492009-08-24 23:44:59 +02004914#ifdef CONFIG_PM
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01004915static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
4916{
4917 struct btusb_data *data = usb_get_intfdata(intf);
4918
4919 BT_DBG("intf %p", intf);
4920
4921 if (data->suspend_count++)
4922 return 0;
4923
Oliver Neukum7bee5492009-08-24 23:44:59 +02004924 spin_lock_irq(&data->txlock);
Alan Stern5b1b0b82011-08-19 23:49:48 +02004925 if (!(PMSG_IS_AUTO(message) && data->tx_in_flight)) {
Oliver Neukum7bee5492009-08-24 23:44:59 +02004926 set_bit(BTUSB_SUSPENDING, &data->flags);
4927 spin_unlock_irq(&data->txlock);
4928 } else {
4929 spin_unlock_irq(&data->txlock);
4930 data->suspend_count--;
4931 return -EBUSY;
4932 }
4933
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01004934 cancel_work_sync(&data->work);
4935
Oliver Neukum7bee5492009-08-24 23:44:59 +02004936 btusb_stop_traffic(data);
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01004937 usb_kill_anchored_urbs(&data->tx_anchor);
4938
Rajat Jainfd913ef2017-02-01 14:24:09 -08004939 if (data->oob_wake_irq && device_may_wakeup(&data->udev->dev)) {
4940 set_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags);
4941 enable_irq_wake(data->oob_wake_irq);
4942 enable_irq(data->oob_wake_irq);
4943 }
4944
Alex Lu9e455242019-08-14 20:02:52 +08004945 /* For global suspend, Realtek devices lose the loaded fw
4946 * in them. But for autosuspend, firmware should remain.
4947 * Actually, it depends on whether the usb host sends
4948 * set feature (enable wakeup) or not.
4949 */
4950 if (test_bit(BTUSB_WAKEUP_DISABLE, &data->flags)) {
4951 if (PMSG_IS_AUTO(message) &&
4952 device_can_wakeup(&data->udev->dev))
4953 data->udev->do_remote_wakeup = 1;
4954 else if (!PMSG_IS_AUTO(message))
4955 data->udev->reset_resume = 1;
4956 }
4957
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01004958 return 0;
4959}
4960
Oliver Neukum7bee5492009-08-24 23:44:59 +02004961static void play_deferred(struct btusb_data *data)
4962{
4963 struct urb *urb;
4964 int err;
4965
4966 while ((urb = usb_get_from_anchor(&data->deferred))) {
Jeffy Chen19cfe912017-07-20 18:53:50 +08004967 usb_anchor_urb(urb, &data->tx_anchor);
4968
Oliver Neukum7bee5492009-08-24 23:44:59 +02004969 err = usb_submit_urb(urb, GFP_ATOMIC);
Jeffy Chen19cfe912017-07-20 18:53:50 +08004970 if (err < 0) {
4971 if (err != -EPERM && err != -ENODEV)
4972 BT_ERR("%s urb %p submission failed (%d)",
4973 data->hdev->name, urb, -err);
4974 kfree(urb->setup_packet);
4975 usb_unanchor_urb(urb);
4976 usb_free_urb(urb);
Oliver Neukum7bee5492009-08-24 23:44:59 +02004977 break;
Jeffy Chen19cfe912017-07-20 18:53:50 +08004978 }
Oliver Neukum7bee5492009-08-24 23:44:59 +02004979
4980 data->tx_in_flight++;
Jeffy Chen19cfe912017-07-20 18:53:50 +08004981 usb_free_urb(urb);
Oliver Neukum7bee5492009-08-24 23:44:59 +02004982 }
Jeffy Chen19cfe912017-07-20 18:53:50 +08004983
4984 /* Cleanup the rest deferred urbs. */
4985 while ((urb = usb_get_from_anchor(&data->deferred))) {
4986 kfree(urb->setup_packet);
4987 usb_free_urb(urb);
4988 }
Oliver Neukum7bee5492009-08-24 23:44:59 +02004989}
4990
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01004991static int btusb_resume(struct usb_interface *intf)
4992{
4993 struct btusb_data *data = usb_get_intfdata(intf);
4994 struct hci_dev *hdev = data->hdev;
Oliver Neukum7bee5492009-08-24 23:44:59 +02004995 int err = 0;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01004996
4997 BT_DBG("intf %p", intf);
4998
4999 if (--data->suspend_count)
5000 return 0;
5001
Rajat Jainfd913ef2017-02-01 14:24:09 -08005002 /* Disable only if not already disabled (keep it balanced) */
5003 if (test_and_clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags)) {
5004 disable_irq(data->oob_wake_irq);
5005 disable_irq_wake(data->oob_wake_irq);
5006 }
5007
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01005008 if (!test_bit(HCI_RUNNING, &hdev->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +02005009 goto done;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01005010
5011 if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
5012 err = btusb_submit_intr_urb(hdev, GFP_NOIO);
5013 if (err < 0) {
5014 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02005015 goto failed;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01005016 }
5017 }
5018
5019 if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) {
Marcel Holtmann43c2e572009-02-04 17:41:38 +01005020 err = btusb_submit_bulk_urb(hdev, GFP_NOIO);
5021 if (err < 0) {
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01005022 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02005023 goto failed;
5024 }
5025
5026 btusb_submit_bulk_urb(hdev, GFP_NOIO);
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01005027 }
5028
5029 if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
5030 if (btusb_submit_isoc_urb(hdev, GFP_NOIO) < 0)
5031 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
5032 else
5033 btusb_submit_isoc_urb(hdev, GFP_NOIO);
5034 }
5035
Oliver Neukum7bee5492009-08-24 23:44:59 +02005036 spin_lock_irq(&data->txlock);
5037 play_deferred(data);
5038 clear_bit(BTUSB_SUSPENDING, &data->flags);
5039 spin_unlock_irq(&data->txlock);
5040 schedule_work(&data->work);
5041
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01005042 return 0;
Oliver Neukum7bee5492009-08-24 23:44:59 +02005043
5044failed:
5045 usb_scuttle_anchored_urbs(&data->deferred);
5046done:
5047 spin_lock_irq(&data->txlock);
5048 clear_bit(BTUSB_SUSPENDING, &data->flags);
5049 spin_unlock_irq(&data->txlock);
5050
5051 return err;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01005052}
Oliver Neukum7bee5492009-08-24 23:44:59 +02005053#endif
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01005054
Marcel Holtmann5e23b922007-10-20 14:12:34 +02005055static struct usb_driver btusb_driver = {
5056 .name = "btusb",
5057 .probe = btusb_probe,
5058 .disconnect = btusb_disconnect,
Oliver Neukum7bee5492009-08-24 23:44:59 +02005059#ifdef CONFIG_PM
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01005060 .suspend = btusb_suspend,
5061 .resume = btusb_resume,
Oliver Neukum7bee5492009-08-24 23:44:59 +02005062#endif
Marcel Holtmann5e23b922007-10-20 14:12:34 +02005063 .id_table = btusb_table,
Oliver Neukum7bee5492009-08-24 23:44:59 +02005064 .supports_autosuspend = 1,
Sarah Sharpe1f12eb2012-04-23 10:08:51 -07005065 .disable_hub_initiated_lpm = 1,
Marcel Holtmann5e23b922007-10-20 14:12:34 +02005066};
5067
Greg Kroah-Hartman93f15082011-11-18 09:47:34 -08005068module_usb_driver(btusb_driver);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02005069
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02005070module_param(disable_scofix, bool, 0644);
5071MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
5072
5073module_param(force_scofix, bool, 0644);
5074MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
5075
Hans de Goedeeff2d682017-11-13 14:44:16 +01005076module_param(enable_autosuspend, bool, 0644);
5077MODULE_PARM_DESC(enable_autosuspend, "Enable USB autosuspend by default");
5078
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02005079module_param(reset, bool, 0644);
5080MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
5081
Marcel Holtmann5e23b922007-10-20 14:12:34 +02005082MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
5083MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
5084MODULE_VERSION(VERSION);
5085MODULE_LICENSE("GPL");