blob: 8ae3ad7a60131b53368e4cfb9cd63cc05418b68c [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
Marcel Holtmann5e23b922007-10-20 14:12:34 +020062
Marcel Holtmann54265202013-10-11 07:46:18 -070063static const struct usb_device_id btusb_table[] = {
Marcel Holtmann5e23b922007-10-20 14:12:34 +020064 /* Generic Bluetooth USB device */
65 { USB_DEVICE_INFO(0xe0, 0x01, 0x01) },
66
Marcel Holtmann893ba542015-01-28 20:27:34 -080067 /* Generic Bluetooth AMP device */
68 { USB_DEVICE_INFO(0xe0, 0x01, 0x04), .driver_info = BTUSB_AMP },
69
Daniel Draked63b2822015-07-17 11:12:25 -060070 /* Generic Bluetooth USB interface */
71 { USB_INTERFACE_INFO(0xe0, 0x01, 0x01) },
72
Henrik Rydberg1fa65352012-08-25 19:28:06 +020073 /* Apple-specific (Broadcom) devices */
Marcel Holtmann17b27722015-03-22 15:52:38 +010074 { USB_VENDOR_AND_INTERFACE_INFO(0x05ac, 0xff, 0x01, 0x01),
Marcel Holtmann22f8e9d2015-10-20 00:53:33 +020075 .driver_info = BTUSB_BCM_APPLE | BTUSB_IFNUM_2 },
Henrik Rydberg1fa65352012-08-25 19:28:06 +020076
Cho, Yu-Chen178c0592013-06-04 21:40:26 +080077 /* MediaTek MT76x0E */
78 { USB_DEVICE(0x0e8d, 0x763f) },
79
Oliver Neukumc510eae2011-09-21 11:41:45 +020080 /* Broadcom SoftSailing reporting vendor specific */
Don Zickus2e8b50632012-03-28 16:41:11 -040081 { USB_DEVICE(0x0a5c, 0x21e1) },
Oliver Neukumc510eae2011-09-21 11:41:45 +020082
Nobuhiro Iwamatsu3cd01972010-08-20 16:24:07 +090083 /* Apple MacBookPro 7,1 */
84 { USB_DEVICE(0x05ac, 0x8213) },
85
Cyril Lacoux0a79f672010-07-14 10:29:27 +040086 /* Apple iMac11,1 */
87 { USB_DEVICE(0x05ac, 0x8215) },
88
Nobuhiro Iwamatsu9c047152010-08-20 16:24:06 +090089 /* Apple MacBookPro6,2 */
90 { USB_DEVICE(0x05ac, 0x8218) },
91
Edgar (gimli) Hucek3e3ede72010-11-04 08:04:33 +010092 /* Apple MacBookAir3,1, MacBookAir3,2 */
93 { USB_DEVICE(0x05ac, 0x821b) },
94
Pieter-Augustijn Van Malleghema63b7232011-09-07 02:28:10 -040095 /* Apple MacBookAir4,1 */
96 { USB_DEVICE(0x05ac, 0x821f) },
97
Marc-Antoine Perennou88d377b2011-03-24 14:51:21 -030098 /* Apple MacBookPro8,2 */
99 { USB_DEVICE(0x05ac, 0x821a) },
100
Jurgen Kramerf78b68262011-09-04 18:01:42 +0200101 /* Apple MacMini5,1 */
102 { USB_DEVICE(0x05ac, 0x8281) },
103
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200104 /* AVM BlueFRITZ! USB v2.0 */
Marcel Holtmann4fcef8e2015-01-01 17:34:37 -0800105 { USB_DEVICE(0x057c, 0x3800), .driver_info = BTUSB_SWAVE },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200106
107 /* Bluetooth Ultraport Module from IBM */
108 { USB_DEVICE(0x04bf, 0x030a) },
109
110 /* ALPS Modules with non-standard id */
111 { USB_DEVICE(0x044e, 0x3001) },
112 { USB_DEVICE(0x044e, 0x3002) },
113
114 /* Ericsson with non-standard id */
115 { USB_DEVICE(0x0bdb, 0x1002) },
116
117 /* Canyon CN-BTU1 with HID interfaces */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100118 { USB_DEVICE(0x0c10, 0x0000) },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200119
Wen-chien Jesse Sungd13431c2011-11-08 14:30:22 +0800120 /* Broadcom BCM20702A0 */
121 { USB_DEVICE(0x413c, 0x8197) },
122
Marcel Holtmannd049f4e2015-01-26 20:35:32 -0800123 /* Broadcom BCM20702B0 (Dynex/Insignia) */
124 { USB_DEVICE(0x19ff, 0x0239), .driver_info = BTUSB_BCM_PATCHRAM },
125
Santtu Rekilä2faf71c2015-10-05 15:45:27 +0300126 /* Broadcom BCM43142A0 (Foxconn/Lenovo) */
Dmitry Tunin628c26b2017-07-06 14:41:13 +0300127 { USB_VENDOR_AND_INTERFACE_INFO(0x105b, 0xff, 0x01, 0x01),
128 .driver_info = BTUSB_BCM_PATCHRAM },
Santtu Rekilä2faf71c2015-10-05 15:45:27 +0300129
Christoph Haaga57bac42017-02-10 14:02:45 +0100130 /* Broadcom BCM920703 (HTC Vive) */
131 { USB_VENDOR_AND_INTERFACE_INFO(0x0bb4, 0xff, 0x01, 0x01),
132 .driver_info = BTUSB_BCM_PATCHRAM },
133
Steven Harms98514032012-04-13 14:45:55 -0400134 /* Foxconn - Hon Hai */
Heinrich Siebmanns6029ddc2014-12-03 19:32:22 +0100135 { USB_VENDOR_AND_INTERFACE_INFO(0x0489, 0xff, 0x01, 0x01),
136 .driver_info = BTUSB_BCM_PATCHRAM },
Steven Harms98514032012-04-13 14:45:55 -0400137
Matej Dubovy8f0c3042015-02-02 18:50:14 +0100138 /* Lite-On Technology - Broadcom based */
139 { USB_VENDOR_AND_INTERFACE_INFO(0x04ca, 0xff, 0x01, 0x01),
140 .driver_info = BTUSB_BCM_PATCHRAM },
141
Andy Shevchenko0b880062014-02-18 18:26:19 +0200142 /* Broadcom devices with vendor specific id */
Petri Gynther10d4c672014-05-08 15:50:01 -0700143 { USB_VENDOR_AND_INTERFACE_INFO(0x0a5c, 0xff, 0x01, 0x01),
144 .driver_info = BTUSB_BCM_PATCHRAM },
Gustavo Padovan92c385f2012-08-06 15:36:49 -0300145
Marcel Holtmannc2aef6e2014-07-21 14:02:33 +0200146 /* ASUSTek Computer - Broadcom based */
Rick Dunn9a5abdaa2015-01-17 05:29:12 +0100147 { USB_VENDOR_AND_INTERFACE_INFO(0x0b05, 0xff, 0x01, 0x01),
148 .driver_info = BTUSB_BCM_PATCHRAM },
Marcel Holtmannc2aef6e2014-07-21 14:02:33 +0200149
Ken O'Brien5bcecf32013-09-21 19:14:43 +0100150 /* Belkin F8065bf - Broadcom based */
Marcel Holtmann6331c682015-03-27 15:11:41 -0700151 { USB_VENDOR_AND_INTERFACE_INFO(0x050d, 0xff, 0x01, 0x01),
152 .driver_info = BTUSB_BCM_PATCHRAM },
Ken O'Brien5bcecf32013-09-21 19:14:43 +0100153
Jurgen Kramer9113bfd2014-02-15 12:01:09 +0100154 /* IMC Networks - Broadcom based */
Marcel Holtmann6331c682015-03-27 15:11:41 -0700155 { USB_VENDOR_AND_INTERFACE_INFO(0x13d3, 0xff, 0x01, 0x01),
156 .driver_info = BTUSB_BCM_PATCHRAM },
Jurgen Kramer9113bfd2014-02-15 12:01:09 +0100157
Wen-chien Jesse Sungfdfddc62017-01-10 15:41:13 +0800158 /* Dell Computer - Broadcom based */
159 { USB_VENDOR_AND_INTERFACE_INFO(0x413c, 0xff, 0x01, 0x01),
160 .driver_info = BTUSB_BCM_PATCHRAM },
161
Dmitry Tunin1623d0b2015-12-05 14:09:36 +0300162 /* Toshiba Corp - Broadcom based */
163 { USB_VENDOR_AND_INTERFACE_INFO(0x0930, 0xff, 0x01, 0x01),
164 .driver_info = BTUSB_BCM_PATCHRAM },
165
Marcel Holtmann40df7832014-07-06 13:29:58 +0200166 /* Intel Bluetooth USB Bootloader (RAM module) */
Marcel Holtmannd92f2df2014-07-06 14:53:55 +0200167 { USB_DEVICE(0x8087, 0x0a5a),
168 .driver_info = BTUSB_INTEL_BOOT | BTUSB_BROKEN_ISOC },
Marcel Holtmann40df7832014-07-06 13:29:58 +0200169
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200170 { } /* Terminating entry */
171};
172
173MODULE_DEVICE_TABLE(usb, btusb_table);
174
Marcel Holtmann54265202013-10-11 07:46:18 -0700175static const struct usb_device_id blacklist_table[] = {
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200176 /* CSR BlueCore devices */
177 { USB_DEVICE(0x0a12, 0x0001), .driver_info = BTUSB_CSR },
178
179 /* Broadcom BCM2033 without firmware */
180 { USB_DEVICE(0x0a5c, 0x2033), .driver_info = BTUSB_IGNORE },
181
Marcel Holtmann6c9d4352015-10-17 14:39:27 +0200182 /* Broadcom BCM2045 devices */
183 { USB_DEVICE(0x0a5c, 0x2045), .driver_info = BTUSB_BCM2045 },
184
Bala Shanmugambe931122010-11-26 17:35:46 +0530185 /* Atheros 3011 with sflash firmware */
Andy Shevchenko0b880062014-02-18 18:26:19 +0200186 { USB_DEVICE(0x0489, 0xe027), .driver_info = BTUSB_IGNORE },
187 { USB_DEVICE(0x0489, 0xe03d), .driver_info = BTUSB_IGNORE },
Alexander Ploumistos2eeff0b2015-02-13 21:05:11 +0200188 { USB_DEVICE(0x04f2, 0xaff1), .driver_info = BTUSB_IGNORE },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200189 { USB_DEVICE(0x0930, 0x0215), .driver_info = BTUSB_IGNORE },
Bala Shanmugambe931122010-11-26 17:35:46 +0530190 { USB_DEVICE(0x0cf3, 0x3002), .driver_info = BTUSB_IGNORE },
Marek Vasut6eda5412012-06-08 14:32:50 +0200191 { USB_DEVICE(0x0cf3, 0xe019), .driver_info = BTUSB_IGNORE },
Andy Ross2a7bccc2011-05-09 16:11:16 -0700192 { USB_DEVICE(0x13d3, 0x3304), .driver_info = BTUSB_IGNORE },
Bala Shanmugambe931122010-11-26 17:35:46 +0530193
Cho, Yu-Chen509e7862011-01-26 17:10:59 +0800194 /* Atheros AR9285 Malbec with sflash firmware */
195 { USB_DEVICE(0x03f0, 0x311d), .driver_info = BTUSB_IGNORE },
196
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530197 /* Atheros 3012 with sflash firmware */
Andy Shevchenko0b880062014-02-18 18:26:19 +0200198 { USB_DEVICE(0x0489, 0xe04d), .driver_info = BTUSB_ATH3012 },
199 { USB_DEVICE(0x0489, 0xe04e), .driver_info = BTUSB_ATH3012 },
200 { USB_DEVICE(0x0489, 0xe056), .driver_info = BTUSB_ATH3012 },
201 { USB_DEVICE(0x0489, 0xe057), .driver_info = BTUSB_ATH3012 },
202 { USB_DEVICE(0x0489, 0xe05f), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin692c0622015-06-06 20:25:40 +0300203 { USB_DEVICE(0x0489, 0xe076), .driver_info = BTUSB_ATH3012 },
Anantha Krishnan4b552bc2014-10-06 16:31:49 +0530204 { USB_DEVICE(0x0489, 0xe078), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin28c971d2016-02-10 00:49:11 +0300205 { USB_DEVICE(0x0489, 0xe095), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200206 { USB_DEVICE(0x04c5, 0x1330), .driver_info = BTUSB_ATH3012 },
207 { USB_DEVICE(0x04ca, 0x3004), .driver_info = BTUSB_ATH3012 },
208 { USB_DEVICE(0x04ca, 0x3005), .driver_info = BTUSB_ATH3012 },
209 { USB_DEVICE(0x04ca, 0x3006), .driver_info = BTUSB_ATH3012 },
Mohammed Habibulla1fb4e092014-04-17 11:37:13 -0700210 { USB_DEVICE(0x04ca, 0x3007), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200211 { USB_DEVICE(0x04ca, 0x3008), .driver_info = BTUSB_ATH3012 },
212 { USB_DEVICE(0x04ca, 0x300b), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin7e730c7f2015-06-18 20:41:51 +0300213 { USB_DEVICE(0x04ca, 0x300d), .driver_info = BTUSB_ATH3012 },
Dmitry Tuninec0810d2015-05-02 13:36:58 +0300214 { USB_DEVICE(0x04ca, 0x300f), .driver_info = BTUSB_ATH3012 },
Janne Heikkinen134d3b32014-12-09 07:44:51 +0200215 { USB_DEVICE(0x04ca, 0x3010), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin81d90442016-02-28 11:04:06 +0300216 { USB_DEVICE(0x04ca, 0x3014), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin441ad622017-01-05 13:19:53 +0300217 { USB_DEVICE(0x04ca, 0x3018), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200218 { USB_DEVICE(0x0930, 0x0219), .driver_info = BTUSB_ATH3012 },
Dmitry Tunincd355ff2015-10-05 19:29:33 +0300219 { USB_DEVICE(0x0930, 0x021c), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200220 { USB_DEVICE(0x0930, 0x0220), .driver_info = BTUSB_ATH3012 },
Vincent Zwanenburg89d29752014-08-08 12:33:56 +0100221 { USB_DEVICE(0x0930, 0x0227), .driver_info = BTUSB_ATH3012 },
Andy Shevchenkoa735f9e2014-02-18 18:26:20 +0200222 { USB_DEVICE(0x0b05, 0x17d0), .driver_info = BTUSB_ATH3012 },
Ming Leid66629c2013-03-15 11:00:39 +0800223 { USB_DEVICE(0x0cf3, 0x0036), .driver_info = BTUSB_ATH3012 },
Hans de Goede544a5912018-04-26 14:18:19 +0200224 { USB_DEVICE(0x0cf3, 0x3004), .driver_info = BTUSB_ATH3012 },
Sunguk Lee94a32d102013-03-12 04:41:58 +0900225 { USB_DEVICE(0x0cf3, 0x3008), .driver_info = BTUSB_ATH3012 },
Cho, Yu-Chen07c0ea82012-03-14 22:01:21 +0200226 { USB_DEVICE(0x0cf3, 0x311d), .driver_info = BTUSB_ATH3012 },
Oliver Neukumb1312372014-01-16 15:37:11 +0100227 { USB_DEVICE(0x0cf3, 0x311e), .driver_info = BTUSB_ATH3012 },
Oliver Neukum1e56f1e2014-01-16 16:02:58 +0100228 { USB_DEVICE(0x0cf3, 0x311f), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200229 { USB_DEVICE(0x0cf3, 0x3121), .driver_info = BTUSB_ATH3012 },
Ming Leiebaf5792013-03-18 23:45:11 +0800230 { USB_DEVICE(0x0cf3, 0x817a), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin18e0afa2015-10-16 11:45:26 +0300231 { USB_DEVICE(0x0cf3, 0x817b), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200232 { USB_DEVICE(0x0cf3, 0xe003), .driver_info = BTUSB_ATH3012 },
AceLan Kaoac713112012-04-19 14:53:45 +0800233 { USB_DEVICE(0x0cf3, 0xe004), .driver_info = BTUSB_ATH3012 },
Peng Chen0a3658c2013-08-30 17:41:40 +0800234 { USB_DEVICE(0x0cf3, 0xe005), .driver_info = BTUSB_ATH3012 },
Wen-chien Jesse Sungca79f232015-05-13 11:39:24 +0800235 { USB_DEVICE(0x0cf3, 0xe006), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200236 { USB_DEVICE(0x13d3, 0x3362), .driver_info = BTUSB_ATH3012 },
237 { USB_DEVICE(0x13d3, 0x3375), .driver_info = BTUSB_ATH3012 },
AceLan Kaoeed307e2012-12-11 11:41:20 +0800238 { USB_DEVICE(0x13d3, 0x3393), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin609574e2016-02-10 15:33:17 +0300239 { USB_DEVICE(0x13d3, 0x3395), .driver_info = BTUSB_ATH3012 },
Sujith Manoharan5b77a1f2013-07-15 09:29:03 +0530240 { USB_DEVICE(0x13d3, 0x3402), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin3bb30a7c2014-11-25 20:19:52 +0300241 { USB_DEVICE(0x13d3, 0x3408), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin033efa92015-01-18 00:16:51 +0300242 { USB_DEVICE(0x13d3, 0x3423), .driver_info = BTUSB_ATH3012 },
Anantha Krishnanfa2f1392014-07-08 19:25:08 +0530243 { USB_DEVICE(0x13d3, 0x3432), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin75c6aca2016-03-04 01:32:19 +0300244 { USB_DEVICE(0x13d3, 0x3472), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin0d0cef62015-06-06 20:29:25 +0300245 { USB_DEVICE(0x13d3, 0x3474), .driver_info = BTUSB_ATH3012 },
Lauro Costa72f9f8b2016-05-09 17:36:11 -0300246 { USB_DEVICE(0x13d3, 0x3487), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin12d86892016-07-12 01:35:18 +0300247 { USB_DEVICE(0x13d3, 0x3490), .driver_info = BTUSB_ATH3012 },
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530248
Cho, Yu-Chene9036e32011-02-15 10:20:07 +0800249 /* Atheros AR5BBU12 with sflash firmware */
250 { USB_DEVICE(0x0489, 0xe02c), .driver_info = BTUSB_IGNORE },
251
Michael Gruetzner85d59722012-05-02 22:33:40 +0200252 /* Atheros AR5BBU12 with sflash firmware */
Yevgeniy Melnichukbc21fde2012-08-07 19:48:10 +0530253 { USB_DEVICE(0x0489, 0xe036), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200254 { USB_DEVICE(0x0489, 0xe03c), .driver_info = BTUSB_ATH3012 },
Michael Gruetzner85d59722012-05-02 22:33:40 +0200255
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +0000256 /* QCA ROME chipset */
Owen Lin1411a262018-10-18 04:37:57 +0000257 { USB_DEVICE(0x0cf3, 0x535b), .driver_info = BTUSB_QCA_ROME },
Wen-chien Jesse Sung20541112015-05-13 11:39:25 +0800258 { USB_DEVICE(0x0cf3, 0xe007), .driver_info = BTUSB_QCA_ROME },
Kai-Heng Feng93519932016-08-16 12:50:06 +0800259 { USB_DEVICE(0x0cf3, 0xe009), .driver_info = BTUSB_QCA_ROME },
AceLan Kaoe5a49ee2018-01-02 10:41:11 +0800260 { USB_DEVICE(0x0cf3, 0xe010), .driver_info = BTUSB_QCA_ROME },
Marcel Holtmannc9e44472015-03-16 23:56:04 -0700261 { USB_DEVICE(0x0cf3, 0xe300), .driver_info = BTUSB_QCA_ROME },
Gabriel1eef1c32017-02-20 12:32:22 -0500262 { USB_DEVICE(0x0cf3, 0xe301), .driver_info = BTUSB_QCA_ROME },
Marcel Holtmannc9e44472015-03-16 23:56:04 -0700263 { USB_DEVICE(0x0cf3, 0xe360), .driver_info = BTUSB_QCA_ROME },
Yvain THONNART47ca5892016-06-11 15:43:30 +0200264 { USB_DEVICE(0x0489, 0xe092), .driver_info = BTUSB_QCA_ROME },
Bartosz Chronowski858ff382017-10-26 10:22:43 +0200265 { USB_DEVICE(0x0489, 0xe09f), .driver_info = BTUSB_QCA_ROME },
Shih-Yuan Lee (FourDollars)06e41d82017-06-14 14:42:00 +0800266 { USB_DEVICE(0x0489, 0xe0a2), .driver_info = BTUSB_QCA_ROME },
Dmitry Tunin1144a4e2016-09-21 19:13:08 +0300267 { USB_DEVICE(0x04ca, 0x3011), .driver_info = BTUSB_QCA_ROME },
Ioan Moldovan0a03f982017-08-28 18:09:39 +0300268 { USB_DEVICE(0x04ca, 0x3015), .driver_info = BTUSB_QCA_ROME },
Brian Norrisd829b9e2017-07-31 17:59:39 -0700269 { USB_DEVICE(0x04ca, 0x3016), .driver_info = BTUSB_QCA_ROME },
Vic Weid666fc52018-04-23 15:17:07 -0700270 { USB_DEVICE(0x04ca, 0x301a), .driver_info = BTUSB_QCA_ROME },
Rocky Liao19220f32020-01-06 14:07:44 +0800271 { USB_DEVICE(0x04ca, 0x3021), .driver_info = BTUSB_QCA_ROME },
João Paulo Rechi Vita44d34af2019-05-23 13:32:01 -0700272 { USB_DEVICE(0x13d3, 0x3491), .driver_info = BTUSB_QCA_ROME },
João Paulo Rechi Vitadf2445b2018-04-27 10:09:58 -0700273 { USB_DEVICE(0x13d3, 0x3496), .driver_info = BTUSB_QCA_ROME },
João Paulo Rechi Vita881cec42019-05-23 13:32:02 -0700274 { USB_DEVICE(0x13d3, 0x3501), .driver_info = BTUSB_QCA_ROME },
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +0000275
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200276 /* Broadcom BCM2035 */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100277 { USB_DEVICE(0x0a5c, 0x2009), .driver_info = BTUSB_BCM92035 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200278 { USB_DEVICE(0x0a5c, 0x200a), .driver_info = BTUSB_WRONG_SCO_MTU },
279 { USB_DEVICE(0x0a5c, 0x2035), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200280
281 /* Broadcom BCM2045 */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100282 { USB_DEVICE(0x0a5c, 0x2039), .driver_info = BTUSB_WRONG_SCO_MTU },
283 { USB_DEVICE(0x0a5c, 0x2101), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmannbdbef3d2008-09-23 00:16:35 +0200284
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200285 /* IBM/Lenovo ThinkPad with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100286 { USB_DEVICE(0x0a5c, 0x201e), .driver_info = BTUSB_WRONG_SCO_MTU },
287 { USB_DEVICE(0x0a5c, 0x2110), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200288
289 /* HP laptop with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100290 { USB_DEVICE(0x03f0, 0x171d), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200291
292 /* Dell laptop with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100293 { USB_DEVICE(0x413c, 0x8126), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200294
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100295 /* Dell Wireless 370 and 410 devices */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100296 { USB_DEVICE(0x413c, 0x8152), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100297 { USB_DEVICE(0x413c, 0x8156), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200298
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100299 /* Belkin F8T012 and F8T013 devices */
300 { USB_DEVICE(0x050d, 0x0012), .driver_info = BTUSB_WRONG_SCO_MTU },
301 { USB_DEVICE(0x050d, 0x0013), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200302
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100303 /* Asus WL-BTD202 device */
304 { USB_DEVICE(0x0b05, 0x1715), .driver_info = BTUSB_WRONG_SCO_MTU },
305
306 /* Kensington Bluetooth USB adapter */
307 { USB_DEVICE(0x047d, 0x105e), .driver_info = BTUSB_WRONG_SCO_MTU },
308
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200309 /* RTX Telecom based adapters with buggy SCO support */
310 { USB_DEVICE(0x0400, 0x0807), .driver_info = BTUSB_BROKEN_ISOC },
311 { USB_DEVICE(0x0400, 0x080a), .driver_info = BTUSB_BROKEN_ISOC },
312
313 /* CONWISE Technology based adapters with buggy SCO support */
Szymon Janc418678b2016-09-01 17:22:37 +0200314 { USB_DEVICE(0x0e5e, 0x6622),
315 .driver_info = BTUSB_BROKEN_ISOC | BTUSB_CW6622},
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200316
Marcel Holtmann4fcef8e2015-01-01 17:34:37 -0800317 /* Roper Class 1 Bluetooth Dongle (Silicon Wave based) */
Aleksei Volkov2eeac872015-06-08 12:02:10 +0300318 { USB_DEVICE(0x1310, 0x0001), .driver_info = BTUSB_SWAVE },
Marcel Holtmann4fcef8e2015-01-01 17:34:37 -0800319
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200320 /* Digianswer devices */
321 { USB_DEVICE(0x08fd, 0x0001), .driver_info = BTUSB_DIGIANSWER },
322 { USB_DEVICE(0x08fd, 0x0002), .driver_info = BTUSB_IGNORE },
323
324 /* CSR BlueCore Bluetooth Sniffer */
Marcel Holtmann4f64fa82014-07-07 00:12:04 +0200325 { USB_DEVICE(0x0a12, 0x0002),
326 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200327
328 /* Frontline ComProbe Bluetooth Sniffer */
Marcel Holtmann4f64fa82014-07-07 00:12:04 +0200329 { USB_DEVICE(0x16d3, 0x0002),
330 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200331
Marcel Holtmanncb1ee892015-01-28 19:41:42 -0800332 /* Marvell Bluetooth devices */
333 { USB_DEVICE(0x1286, 0x2044), .driver_info = BTUSB_MARVELL },
334 { USB_DEVICE(0x1286, 0x2046), .driver_info = BTUSB_MARVELL },
Amitkumar Karwar1165df02016-09-28 16:18:35 +0530335 { USB_DEVICE(0x1286, 0x204e), .driver_info = BTUSB_MARVELL },
Marcel Holtmanncb1ee892015-01-28 19:41:42 -0800336
Marcel Holtmannd0ac9eb2015-01-28 19:41:43 -0800337 /* Intel Bluetooth devices */
Alain Michaud3e4e3f72020-02-27 18:29:37 +0000338 { USB_DEVICE(0x8087, 0x0025), .driver_info = BTUSB_INTEL_NEW |
Alain Michaudaff8c482020-04-23 14:43:31 +0000339 BTUSB_WIDEBAND_SPEECH |
340 BTUSB_VALID_LE_STATES },
Alain Michaud3e4e3f72020-02-27 18:29:37 +0000341 { USB_DEVICE(0x8087, 0x0026), .driver_info = BTUSB_INTEL_NEW |
342 BTUSB_WIDEBAND_SPEECH },
343 { USB_DEVICE(0x8087, 0x0029), .driver_info = BTUSB_INTEL_NEW |
344 BTUSB_WIDEBAND_SPEECH },
Marcel Holtmann407550f2015-02-22 15:41:18 -0800345 { USB_DEVICE(0x8087, 0x07da), .driver_info = BTUSB_CSR },
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -0700346 { USB_DEVICE(0x8087, 0x07dc), .driver_info = BTUSB_INTEL },
Tedd Ho-Jeong Anef4e5e42013-11-12 13:10:58 -0800347 { USB_DEVICE(0x8087, 0x0a2a), .driver_info = BTUSB_INTEL },
Alain Michaud3e4e3f72020-02-27 18:29:37 +0000348 { USB_DEVICE(0x8087, 0x0a2b), .driver_info = BTUSB_INTEL_NEW |
349 BTUSB_WIDEBAND_SPEECH },
350 { USB_DEVICE(0x8087, 0x0aa7), .driver_info = BTUSB_INTEL |
351 BTUSB_WIDEBAND_SPEECH },
352 { USB_DEVICE(0x8087, 0x0aaa), .driver_info = BTUSB_INTEL_NEW |
Alain Michaudaff8c482020-04-23 14:43:31 +0000353 BTUSB_WIDEBAND_SPEECH |
354 BTUSB_VALID_LE_STATES },
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -0700355
Marcel Holtmannd0ac9eb2015-01-28 19:41:43 -0800356 /* Other Intel Bluetooth devices */
357 { USB_VENDOR_AND_INTERFACE_INFO(0x8087, 0xe0, 0x01, 0x01),
358 .driver_info = BTUSB_IGNORE },
Amitkumar Karwarae8df492014-07-18 14:47:06 -0700359
Daniel Drakea2698a92015-04-16 14:09:55 -0600360 /* Realtek Bluetooth devices */
361 { USB_VENDOR_AND_INTERFACE_INFO(0x0bda, 0xe0, 0x01, 0x01),
362 .driver_info = BTUSB_REALTEK },
363
Sean Wanga1c49c432019-06-02 08:02:48 +0800364 /* MediaTek Bluetooth devices */
365 { USB_VENDOR_AND_INTERFACE_INFO(0x0e8d, 0xe0, 0x01, 0x01),
366 .driver_info = BTUSB_MEDIATEK },
367
Daniel Drakea2698a92015-04-16 14:09:55 -0600368 /* Additional Realtek 8723AE Bluetooth devices */
369 { USB_DEVICE(0x0930, 0x021d), .driver_info = BTUSB_REALTEK },
370 { USB_DEVICE(0x13d3, 0x3394), .driver_info = BTUSB_REALTEK },
371
372 /* Additional Realtek 8723BE Bluetooth devices */
373 { USB_DEVICE(0x0489, 0xe085), .driver_info = BTUSB_REALTEK },
374 { USB_DEVICE(0x0489, 0xe08b), .driver_info = BTUSB_REALTEK },
375 { USB_DEVICE(0x13d3, 0x3410), .driver_info = BTUSB_REALTEK },
376 { USB_DEVICE(0x13d3, 0x3416), .driver_info = BTUSB_REALTEK },
377 { USB_DEVICE(0x13d3, 0x3459), .driver_info = BTUSB_REALTEK },
Dmitry Tunina81d72d2017-08-08 14:09:02 +0300378 { USB_DEVICE(0x13d3, 0x3494), .driver_info = BTUSB_REALTEK },
Daniel Drakea2698a92015-04-16 14:09:55 -0600379
Vicente Bergasa41e0792018-03-20 19:41:10 +0100380 /* Additional Realtek 8723BU Bluetooth devices */
381 { USB_DEVICE(0x7392, 0xa611), .driver_info = BTUSB_REALTEK },
382
Jian-Hong Pan66d99752018-05-21 18:09:20 +0800383 /* Additional Realtek 8723DE Bluetooth devices */
Jian-Hong Pan45ae68b2018-05-25 17:54:52 +0800384 { USB_DEVICE(0x0bda, 0xb009), .driver_info = BTUSB_REALTEK },
Jian-Hong Pan66d99752018-05-21 18:09:20 +0800385 { USB_DEVICE(0x2ff8, 0xb011), .driver_info = BTUSB_REALTEK },
386
Daniel Drakea2698a92015-04-16 14:09:55 -0600387 /* Additional Realtek 8821AE Bluetooth devices */
388 { USB_DEVICE(0x0b05, 0x17dc), .driver_info = BTUSB_REALTEK },
389 { USB_DEVICE(0x13d3, 0x3414), .driver_info = BTUSB_REALTEK },
390 { USB_DEVICE(0x13d3, 0x3458), .driver_info = BTUSB_REALTEK },
391 { USB_DEVICE(0x13d3, 0x3461), .driver_info = BTUSB_REALTEK },
392 { USB_DEVICE(0x13d3, 0x3462), .driver_info = BTUSB_REALTEK },
393
Larry Fingerfed03fe2018-02-11 12:24:32 -0600394 /* Additional Realtek 8822BE Bluetooth devices */
Artiom Vaskov1cd2fab2018-05-30 11:23:00 +0300395 { USB_DEVICE(0x13d3, 0x3526), .driver_info = BTUSB_REALTEK },
Larry Fingerfed03fe2018-02-11 12:24:32 -0600396 { USB_DEVICE(0x0b05, 0x185c), .driver_info = BTUSB_REALTEK },
397
Jian-Hong Pan6d0762b2019-09-03 17:10:42 +0800398 /* Additional Realtek 8822CE Bluetooth devices */
399 { USB_DEVICE(0x04ca, 0x4005), .driver_info = BTUSB_REALTEK },
Sergey Shatunoveb3939e32020-02-08 23:53:15 +0800400 { USB_DEVICE(0x13d3, 0x3548), .driver_info = BTUSB_REALTEK },
Jian-Hong Pan6d0762b2019-09-03 17:10:42 +0800401
Peter Poklop4481c072015-08-15 20:47:09 +0200402 /* Silicon Wave based devices */
403 { USB_DEVICE(0x0c10, 0x0000), .driver_info = BTUSB_SWAVE },
404
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200405 { } /* Terminating entry */
406};
407
Hans de Goede1fdb9262018-02-20 09:06:18 +0100408/* The Bluetooth USB module build into some devices needs to be reset on resume,
409 * this is a problem with the platform (likely shutting off all power) not with
410 * the module itself. So we use a DMI list to match known broken platforms.
411 */
412static const struct dmi_system_id btusb_needs_reset_resume_table[] = {
413 {
Kai-Heng Feng0c6e5262018-03-01 13:42:52 +0800414 /* Dell OptiPlex 3060 (QCA ROME device 0cf3:e007) */
Hans de Goede1fdb9262018-02-20 09:06:18 +0100415 .matches = {
Kai-Heng Feng0c6e5262018-03-01 13:42:52 +0800416 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
417 DMI_MATCH(DMI_PRODUCT_NAME, "OptiPlex 3060"),
Hans de Goede1fdb9262018-02-20 09:06:18 +0100418 },
419 },
Hans de Goede596b07a2018-04-26 20:52:06 +0200420 {
421 /* Dell XPS 9360 (QCA ROME device 0cf3:e300) */
422 .matches = {
423 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
424 DMI_MATCH(DMI_PRODUCT_NAME, "XPS 13 9360"),
425 },
426 },
Hans de Goede939bc6c2018-05-22 09:34:10 +0200427 {
428 /* Dell Inspiron 5565 (QCA ROME device 0cf3:e009) */
429 .matches = {
430 DMI_MATCH(DMI_SYS_VENDOR, "Dell Inc."),
431 DMI_MATCH(DMI_PRODUCT_NAME, "Inspiron 5565"),
432 },
433 },
Hans de Goede1fdb9262018-02-20 09:06:18 +0100434 {}
435};
436
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200437#define BTUSB_MAX_ISOC_FRAMES 10
438
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200439#define BTUSB_INTR_RUNNING 0
440#define BTUSB_BULK_RUNNING 1
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200441#define BTUSB_ISOC_RUNNING 2
Oliver Neukum7bee5492009-08-24 23:44:59 +0200442#define BTUSB_SUSPENDING 3
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -0300443#define BTUSB_DID_ISO_RESUME 4
Marcel Holtmanncda0dd72015-01-26 21:33:48 -0800444#define BTUSB_BOOTLOADER 5
445#define BTUSB_DOWNLOADING 6
Marcel Holtmannce6bb922015-01-28 01:58:40 -0800446#define BTUSB_FIRMWARE_LOADED 7
Marcel Holtmanncda0dd72015-01-26 21:33:48 -0800447#define BTUSB_FIRMWARE_FAILED 8
Marcel Holtmannce6bb922015-01-28 01:58:40 -0800448#define BTUSB_BOOTING 9
Hans de Goede61f5ace2018-01-08 10:44:16 +0100449#define BTUSB_DIAG_RUNNING 10
450#define BTUSB_OOB_WAKE_ENABLED 11
Rajat Jaindc786b22019-01-24 15:28:14 -0800451#define BTUSB_HW_RESET_ACTIVE 12
Sean Wanga1c49c432019-06-02 08:02:48 +0800452#define BTUSB_TX_WAIT_VND_EVT 13
Alex Lu9e455242019-08-14 20:02:52 +0800453#define BTUSB_WAKEUP_DISABLE 14
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200454
455struct btusb_data {
456 struct hci_dev *hdev;
457 struct usb_device *udev;
Marcel Holtmann5fbcd262008-09-23 00:16:36 +0200458 struct usb_interface *intf;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200459 struct usb_interface *isoc;
Marcel Holtmann9d08f502015-10-08 20:23:08 +0200460 struct usb_interface *diag;
Marcel Holtmann459232f2017-10-24 19:42:45 +0200461 unsigned isoc_ifnum;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200462
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200463 unsigned long flags;
464
465 struct work_struct work;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200466 struct work_struct waker;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200467
Marcel Holtmann803b5832014-09-16 08:00:29 +0200468 struct usb_anchor deferred;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200469 struct usb_anchor tx_anchor;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200470 int tx_in_flight;
471 spinlock_t txlock;
472
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200473 struct usb_anchor intr_anchor;
474 struct usb_anchor bulk_anchor;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200475 struct usb_anchor isoc_anchor;
Marcel Holtmann9d08f502015-10-08 20:23:08 +0200476 struct usb_anchor diag_anchor;
Sean Wanga1c49c432019-06-02 08:02:48 +0800477 struct usb_anchor ctrl_anchor;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200478 spinlock_t rxlock;
479
480 struct sk_buff *evt_skb;
481 struct sk_buff *acl_skb;
482 struct sk_buff *sco_skb;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200483
484 struct usb_endpoint_descriptor *intr_ep;
485 struct usb_endpoint_descriptor *bulk_tx_ep;
486 struct usb_endpoint_descriptor *bulk_rx_ep;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200487 struct usb_endpoint_descriptor *isoc_tx_ep;
488 struct usb_endpoint_descriptor *isoc_rx_ep;
Marcel Holtmann9d08f502015-10-08 20:23:08 +0200489 struct usb_endpoint_descriptor *diag_tx_ep;
490 struct usb_endpoint_descriptor *diag_rx_ep;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200491
Rajat Jaindc786b22019-01-24 15:28:14 -0800492 struct gpio_desc *reset_gpio;
493
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100494 __u8 cmdreq_type;
Marcel Holtmann893ba542015-01-28 20:27:34 -0800495 __u8 cmdreq;
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100496
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100497 unsigned int sco_num;
Sathish Narasimmanbaac6272020-04-03 21:43:59 +0200498 unsigned int air_mode;
499 bool usb_alt6_packet_flow;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200500 int isoc_altsetting;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +0100501 int suspend_count;
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +0100502
Marcel Holtmann97307f52015-01-12 13:51:10 -0800503 int (*recv_event)(struct hci_dev *hdev, struct sk_buff *skb);
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +0100504 int (*recv_bulk)(struct btusb_data *data, void *buffer, int count);
Kim, Ben Young Taeace31982015-02-15 23:06:14 +0000505
506 int (*setup_on_usb)(struct hci_dev *hdev);
Rajat Jainfd913ef2017-02-01 14:24:09 -0800507
508 int oob_wake_irq; /* irq for out-of-band wake-on-bt */
Rajat Jaindc786b22019-01-24 15:28:14 -0800509 unsigned cmd_timeout_cnt;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200510};
511
Rajat Jaindc786b22019-01-24 15:28:14 -0800512
513static void btusb_intel_cmd_timeout(struct hci_dev *hdev)
514{
515 struct btusb_data *data = hci_get_drvdata(hdev);
516 struct gpio_desc *reset_gpio = data->reset_gpio;
517
518 if (++data->cmd_timeout_cnt < 5)
519 return;
520
521 if (!reset_gpio) {
522 bt_dev_err(hdev, "No way to reset. Ignoring and continuing");
523 return;
524 }
525
526 /*
527 * Toggle the hard reset line if the platform provides one. The reset
528 * is going to yank the device off the USB and then replug. So doing
529 * once is enough. The cleanup is handled correctly on the way out
530 * (standard USB disconnect), and the new device is detected cleanly
531 * and bound to the driver again like it should be.
532 */
533 if (test_and_set_bit(BTUSB_HW_RESET_ACTIVE, &data->flags)) {
534 bt_dev_err(hdev, "last reset failed? Not resetting again");
535 return;
536 }
537
538 bt_dev_err(hdev, "Initiating HW reset via gpio");
Rajat Jain2de66bb82019-01-28 15:08:09 -0800539 gpiod_set_value_cansleep(reset_gpio, 1);
540 msleep(100);
541 gpiod_set_value_cansleep(reset_gpio, 0);
Rajat Jaindc786b22019-01-24 15:28:14 -0800542}
543
Alex Lud7ef0d12019-09-05 10:36:31 +0800544static void btusb_rtl_cmd_timeout(struct hci_dev *hdev)
545{
546 struct btusb_data *data = hci_get_drvdata(hdev);
547 struct gpio_desc *reset_gpio = data->reset_gpio;
548
549 if (++data->cmd_timeout_cnt < 5)
550 return;
551
552 if (!reset_gpio) {
553 bt_dev_err(hdev, "No gpio to reset Realtek device, ignoring");
554 return;
555 }
556
557 /* Toggle the hard reset line. The Realtek device is going to
558 * yank itself off the USB and then replug. The cleanup is handled
559 * correctly on the way out (standard USB disconnect), and the new
560 * device is detected cleanly and bound to the driver again like
561 * it should be.
562 */
563 if (test_and_set_bit(BTUSB_HW_RESET_ACTIVE, &data->flags)) {
564 bt_dev_err(hdev, "last reset failed? Not resetting again");
565 return;
566 }
567
568 bt_dev_err(hdev, "Reset Realtek device via gpio");
Alex Lud7ef0d12019-09-05 10:36:31 +0800569 gpiod_set_value_cansleep(reset_gpio, 1);
Max Chou34682112019-11-27 11:01:07 +0800570 msleep(200);
571 gpiod_set_value_cansleep(reset_gpio, 0);
Alex Lud7ef0d12019-09-05 10:36:31 +0800572}
573
Marcel Holtmann803b5832014-09-16 08:00:29 +0200574static inline void btusb_free_frags(struct btusb_data *data)
575{
576 unsigned long flags;
577
578 spin_lock_irqsave(&data->rxlock, flags);
579
580 kfree_skb(data->evt_skb);
581 data->evt_skb = NULL;
582
583 kfree_skb(data->acl_skb);
584 data->acl_skb = NULL;
585
586 kfree_skb(data->sco_skb);
587 data->sco_skb = NULL;
588
589 spin_unlock_irqrestore(&data->rxlock, flags);
590}
591
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200592static int btusb_recv_intr(struct btusb_data *data, void *buffer, int count)
593{
Marcel Holtmann803b5832014-09-16 08:00:29 +0200594 struct sk_buff *skb;
Sebastian Andrzej Siewiora5e50d52018-06-19 23:56:57 +0200595 unsigned long flags;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200596 int err = 0;
597
Sebastian Andrzej Siewiora5e50d52018-06-19 23:56:57 +0200598 spin_lock_irqsave(&data->rxlock, flags);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200599 skb = data->evt_skb;
600
601 while (count) {
602 int len;
603
604 if (!skb) {
605 skb = bt_skb_alloc(HCI_MAX_EVENT_SIZE, GFP_ATOMIC);
606 if (!skb) {
607 err = -ENOMEM;
608 break;
609 }
610
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100611 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
612 hci_skb_expect(skb) = HCI_EVENT_HDR_SIZE;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200613 }
614
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100615 len = min_t(uint, hci_skb_expect(skb), count);
Johannes Berg59ae1d12017-06-16 14:29:20 +0200616 skb_put_data(skb, buffer, len);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200617
618 count -= len;
619 buffer += len;
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100620 hci_skb_expect(skb) -= len;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200621
622 if (skb->len == HCI_EVENT_HDR_SIZE) {
623 /* Complete event header */
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100624 hci_skb_expect(skb) = hci_event_hdr(skb)->plen;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200625
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100626 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
Marcel Holtmann803b5832014-09-16 08:00:29 +0200627 kfree_skb(skb);
628 skb = NULL;
629
630 err = -EILSEQ;
631 break;
632 }
633 }
634
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100635 if (!hci_skb_expect(skb)) {
Marcel Holtmann803b5832014-09-16 08:00:29 +0200636 /* Complete frame */
Marcel Holtmann97307f52015-01-12 13:51:10 -0800637 data->recv_event(data->hdev, skb);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200638 skb = NULL;
639 }
640 }
641
642 data->evt_skb = skb;
Sebastian Andrzej Siewiora5e50d52018-06-19 23:56:57 +0200643 spin_unlock_irqrestore(&data->rxlock, flags);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200644
645 return err;
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200646}
647
648static int btusb_recv_bulk(struct btusb_data *data, void *buffer, int count)
649{
Marcel Holtmann803b5832014-09-16 08:00:29 +0200650 struct sk_buff *skb;
Sebastian Andrzej Siewiora5e50d52018-06-19 23:56:57 +0200651 unsigned long flags;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200652 int err = 0;
653
Sebastian Andrzej Siewiora5e50d52018-06-19 23:56:57 +0200654 spin_lock_irqsave(&data->rxlock, flags);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200655 skb = data->acl_skb;
656
657 while (count) {
658 int len;
659
660 if (!skb) {
661 skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
662 if (!skb) {
663 err = -ENOMEM;
664 break;
665 }
666
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100667 hci_skb_pkt_type(skb) = HCI_ACLDATA_PKT;
668 hci_skb_expect(skb) = HCI_ACL_HDR_SIZE;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200669 }
670
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100671 len = min_t(uint, hci_skb_expect(skb), count);
Johannes Berg59ae1d12017-06-16 14:29:20 +0200672 skb_put_data(skb, buffer, len);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200673
674 count -= len;
675 buffer += len;
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100676 hci_skb_expect(skb) -= len;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200677
678 if (skb->len == HCI_ACL_HDR_SIZE) {
679 __le16 dlen = hci_acl_hdr(skb)->dlen;
680
681 /* Complete ACL header */
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100682 hci_skb_expect(skb) = __le16_to_cpu(dlen);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200683
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100684 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
Marcel Holtmann803b5832014-09-16 08:00:29 +0200685 kfree_skb(skb);
686 skb = NULL;
687
688 err = -EILSEQ;
689 break;
690 }
691 }
692
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100693 if (!hci_skb_expect(skb)) {
Marcel Holtmann803b5832014-09-16 08:00:29 +0200694 /* Complete frame */
695 hci_recv_frame(data->hdev, skb);
696 skb = NULL;
697 }
698 }
699
700 data->acl_skb = skb;
Sebastian Andrzej Siewiora5e50d52018-06-19 23:56:57 +0200701 spin_unlock_irqrestore(&data->rxlock, flags);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200702
703 return err;
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200704}
705
706static int btusb_recv_isoc(struct btusb_data *data, void *buffer, int count)
707{
Marcel Holtmann803b5832014-09-16 08:00:29 +0200708 struct sk_buff *skb;
Sebastian Andrzej Siewiora5e50d52018-06-19 23:56:57 +0200709 unsigned long flags;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200710 int err = 0;
711
Sebastian Andrzej Siewiora5e50d52018-06-19 23:56:57 +0200712 spin_lock_irqsave(&data->rxlock, flags);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200713 skb = data->sco_skb;
714
715 while (count) {
716 int len;
717
718 if (!skb) {
719 skb = bt_skb_alloc(HCI_MAX_SCO_SIZE, GFP_ATOMIC);
720 if (!skb) {
721 err = -ENOMEM;
722 break;
723 }
724
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100725 hci_skb_pkt_type(skb) = HCI_SCODATA_PKT;
726 hci_skb_expect(skb) = HCI_SCO_HDR_SIZE;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200727 }
728
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100729 len = min_t(uint, hci_skb_expect(skb), count);
Johannes Berg59ae1d12017-06-16 14:29:20 +0200730 skb_put_data(skb, buffer, len);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200731
732 count -= len;
733 buffer += len;
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100734 hci_skb_expect(skb) -= len;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200735
736 if (skb->len == HCI_SCO_HDR_SIZE) {
737 /* Complete SCO header */
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100738 hci_skb_expect(skb) = hci_sco_hdr(skb)->dlen;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200739
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100740 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
Marcel Holtmann803b5832014-09-16 08:00:29 +0200741 kfree_skb(skb);
742 skb = NULL;
743
744 err = -EILSEQ;
745 break;
746 }
747 }
748
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100749 if (!hci_skb_expect(skb)) {
Marcel Holtmann803b5832014-09-16 08:00:29 +0200750 /* Complete frame */
751 hci_recv_frame(data->hdev, skb);
752 skb = NULL;
753 }
754 }
755
756 data->sco_skb = skb;
Sebastian Andrzej Siewiora5e50d52018-06-19 23:56:57 +0200757 spin_unlock_irqrestore(&data->rxlock, flags);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200758
759 return err;
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200760}
761
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200762static void btusb_intr_complete(struct urb *urb)
763{
764 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +0100765 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200766 int err;
767
Marcel Holtmann89e75332014-09-16 04:44:50 +0200768 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
769 urb->actual_length);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200770
771 if (!test_bit(HCI_RUNNING, &hdev->flags))
772 return;
773
774 if (urb->status == 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200775 hdev->stat.byte_rx += urb->actual_length;
776
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200777 if (btusb_recv_intr(data, urb->transfer_buffer,
778 urb->actual_length) < 0) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +0100779 bt_dev_err(hdev, "corrupted event packet");
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200780 hdev->stat.err_rx++;
781 }
Champion Chen85560c42014-09-06 14:06:08 -0500782 } else if (urb->status == -ENOENT) {
783 /* Avoid suspend failed when usb_kill_urb */
784 return;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200785 }
786
787 if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
788 return;
789
Oliver Neukum7bee5492009-08-24 23:44:59 +0200790 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200791 usb_anchor_urb(urb, &data->intr_anchor);
792
793 err = usb_submit_urb(urb, GFP_ATOMIC);
794 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200795 /* -EPERM: urb is being killed;
Derek Robsond98422c2017-07-22 13:47:07 +1200796 * -ENODEV: device got disconnected
797 */
Paul Bolle4935f1c2011-08-09 17:16:28 +0200798 if (err != -EPERM && err != -ENODEV)
Marcel Holtmann2064ee32017-10-30 10:42:59 +0100799 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
800 urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200801 usb_unanchor_urb(urb);
802 }
803}
804
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100805static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200806{
David Herrmann155961e2012-02-09 21:58:32 +0100807 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200808 struct urb *urb;
809 unsigned char *buf;
810 unsigned int pipe;
811 int err, size;
812
813 BT_DBG("%s", hdev->name);
814
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200815 if (!data->intr_ep)
816 return -ENODEV;
817
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100818 urb = usb_alloc_urb(0, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200819 if (!urb)
820 return -ENOMEM;
821
822 size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
823
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100824 buf = kmalloc(size, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200825 if (!buf) {
826 usb_free_urb(urb);
827 return -ENOMEM;
828 }
829
830 pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
831
832 usb_fill_int_urb(urb, data->udev, pipe, buf, size,
Marcel Holtmann89e75332014-09-16 04:44:50 +0200833 btusb_intr_complete, hdev, data->intr_ep->bInterval);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200834
835 urb->transfer_flags |= URB_FREE_BUFFER;
836
837 usb_anchor_urb(urb, &data->intr_anchor);
838
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100839 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200840 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +0200841 if (err != -EPERM && err != -ENODEV)
Marcel Holtmann2064ee32017-10-30 10:42:59 +0100842 bt_dev_err(hdev, "urb %p submission failed (%d)",
843 urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200844 usb_unanchor_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200845 }
846
847 usb_free_urb(urb);
848
849 return err;
850}
851
852static void btusb_bulk_complete(struct urb *urb)
853{
854 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +0100855 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200856 int err;
857
Marcel Holtmann89e75332014-09-16 04:44:50 +0200858 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
859 urb->actual_length);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200860
861 if (!test_bit(HCI_RUNNING, &hdev->flags))
862 return;
863
864 if (urb->status == 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200865 hdev->stat.byte_rx += urb->actual_length;
866
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +0100867 if (data->recv_bulk(data, urb->transfer_buffer,
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200868 urb->actual_length) < 0) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +0100869 bt_dev_err(hdev, "corrupted ACL packet");
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200870 hdev->stat.err_rx++;
871 }
Champion Chen85560c42014-09-06 14:06:08 -0500872 } else if (urb->status == -ENOENT) {
873 /* Avoid suspend failed when usb_kill_urb */
874 return;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200875 }
876
877 if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
878 return;
879
880 usb_anchor_urb(urb, &data->bulk_anchor);
Oliver Neukum652fd782009-12-16 19:23:43 +0100881 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200882
883 err = usb_submit_urb(urb, GFP_ATOMIC);
884 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200885 /* -EPERM: urb is being killed;
Derek Robsond98422c2017-07-22 13:47:07 +1200886 * -ENODEV: device got disconnected
887 */
Paul Bolle4935f1c2011-08-09 17:16:28 +0200888 if (err != -EPERM && err != -ENODEV)
Marcel Holtmann2064ee32017-10-30 10:42:59 +0100889 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
890 urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200891 usb_unanchor_urb(urb);
892 }
893}
894
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100895static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200896{
David Herrmann155961e2012-02-09 21:58:32 +0100897 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200898 struct urb *urb;
899 unsigned char *buf;
900 unsigned int pipe;
Vikram Kandukuri290ba202009-07-02 14:31:59 +0530901 int err, size = HCI_MAX_FRAME_SIZE;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200902
903 BT_DBG("%s", hdev->name);
904
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200905 if (!data->bulk_rx_ep)
906 return -ENODEV;
907
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100908 urb = usb_alloc_urb(0, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200909 if (!urb)
910 return -ENOMEM;
911
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100912 buf = kmalloc(size, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200913 if (!buf) {
914 usb_free_urb(urb);
915 return -ENOMEM;
916 }
917
918 pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
919
Marcel Holtmann89e75332014-09-16 04:44:50 +0200920 usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
921 btusb_bulk_complete, hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200922
923 urb->transfer_flags |= URB_FREE_BUFFER;
924
Oliver Neukum7bee5492009-08-24 23:44:59 +0200925 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200926 usb_anchor_urb(urb, &data->bulk_anchor);
927
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100928 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200929 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +0200930 if (err != -EPERM && err != -ENODEV)
Marcel Holtmann2064ee32017-10-30 10:42:59 +0100931 bt_dev_err(hdev, "urb %p submission failed (%d)",
932 urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200933 usb_unanchor_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200934 }
935
936 usb_free_urb(urb);
937
938 return err;
939}
940
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200941static void btusb_isoc_complete(struct urb *urb)
942{
943 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +0100944 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200945 int i, err;
946
Marcel Holtmann89e75332014-09-16 04:44:50 +0200947 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
948 urb->actual_length);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200949
950 if (!test_bit(HCI_RUNNING, &hdev->flags))
951 return;
952
953 if (urb->status == 0) {
954 for (i = 0; i < urb->number_of_packets; i++) {
955 unsigned int offset = urb->iso_frame_desc[i].offset;
956 unsigned int length = urb->iso_frame_desc[i].actual_length;
957
958 if (urb->iso_frame_desc[i].status)
959 continue;
960
961 hdev->stat.byte_rx += length;
962
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200963 if (btusb_recv_isoc(data, urb->transfer_buffer + offset,
964 length) < 0) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +0100965 bt_dev_err(hdev, "corrupted SCO packet");
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200966 hdev->stat.err_rx++;
967 }
968 }
Champion Chen85560c42014-09-06 14:06:08 -0500969 } else if (urb->status == -ENOENT) {
970 /* Avoid suspend failed when usb_kill_urb */
971 return;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200972 }
973
974 if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags))
975 return;
976
977 usb_anchor_urb(urb, &data->isoc_anchor);
978
979 err = usb_submit_urb(urb, GFP_ATOMIC);
980 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200981 /* -EPERM: urb is being killed;
Derek Robsond98422c2017-07-22 13:47:07 +1200982 * -ENODEV: device got disconnected
983 */
Paul Bolle4935f1c2011-08-09 17:16:28 +0200984 if (err != -EPERM && err != -ENODEV)
Marcel Holtmann2064ee32017-10-30 10:42:59 +0100985 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
986 urb, -err);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200987 usb_unanchor_urb(urb);
988 }
989}
990
Sathish Narasimmanbaac6272020-04-03 21:43:59 +0200991static inline void __fill_isoc_descriptor_msbc(struct urb *urb, int len,
992 int mtu, struct btusb_data *data)
993{
994 int i, offset = 0;
995 unsigned int interval;
996
997 BT_DBG("len %d mtu %d", len, mtu);
998
999 /* For mSBC ALT 6 setting the host will send the packet at continuous
1000 * flow. As per core spec 5, vol 4, part B, table 2.1. For ALT setting
1001 * 6 the HCI PACKET INTERVAL should be 7.5ms for every usb packets.
1002 * To maintain the rate we send 63bytes of usb packets alternatively for
1003 * 7ms and 8ms to maintain the rate as 7.5ms.
1004 */
1005 if (data->usb_alt6_packet_flow) {
1006 interval = 7;
1007 data->usb_alt6_packet_flow = false;
1008 } else {
1009 interval = 6;
1010 data->usb_alt6_packet_flow = true;
1011 }
1012
1013 for (i = 0; i < interval; i++) {
1014 urb->iso_frame_desc[i].offset = offset;
1015 urb->iso_frame_desc[i].length = offset;
1016 }
1017
1018 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
1019 urb->iso_frame_desc[i].offset = offset;
1020 urb->iso_frame_desc[i].length = len;
1021 i++;
1022 }
1023
1024 urb->number_of_packets = i;
1025}
1026
Jesper Juhl42b16b32011-01-17 00:09:38 +01001027static inline void __fill_isoc_descriptor(struct urb *urb, int len, int mtu)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001028{
1029 int i, offset = 0;
1030
1031 BT_DBG("len %d mtu %d", len, mtu);
1032
1033 for (i = 0; i < BTUSB_MAX_ISOC_FRAMES && len >= mtu;
1034 i++, offset += mtu, len -= mtu) {
1035 urb->iso_frame_desc[i].offset = offset;
1036 urb->iso_frame_desc[i].length = mtu;
1037 }
1038
1039 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
1040 urb->iso_frame_desc[i].offset = offset;
1041 urb->iso_frame_desc[i].length = len;
1042 i++;
1043 }
1044
1045 urb->number_of_packets = i;
1046}
1047
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001048static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001049{
David Herrmann155961e2012-02-09 21:58:32 +01001050 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001051 struct urb *urb;
1052 unsigned char *buf;
1053 unsigned int pipe;
1054 int err, size;
1055
1056 BT_DBG("%s", hdev->name);
1057
1058 if (!data->isoc_rx_ep)
1059 return -ENODEV;
1060
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001061 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001062 if (!urb)
1063 return -ENOMEM;
1064
1065 size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) *
1066 BTUSB_MAX_ISOC_FRAMES;
1067
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001068 buf = kmalloc(size, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001069 if (!buf) {
1070 usb_free_urb(urb);
1071 return -ENOMEM;
1072 }
1073
1074 pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress);
1075
Bing Zhaofa0fb932011-12-20 18:19:00 -08001076 usb_fill_int_urb(urb, data->udev, pipe, buf, size, btusb_isoc_complete,
Marcel Holtmann89e75332014-09-16 04:44:50 +02001077 hdev, data->isoc_rx_ep->bInterval);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001078
Marcel Holtmann89e75332014-09-16 04:44:50 +02001079 urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001080
1081 __fill_isoc_descriptor(urb, size,
Marcel Holtmann89e75332014-09-16 04:44:50 +02001082 le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize));
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001083
1084 usb_anchor_urb(urb, &data->isoc_anchor);
1085
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001086 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001087 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +02001088 if (err != -EPERM && err != -ENODEV)
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001089 bt_dev_err(hdev, "urb %p submission failed (%d)",
1090 urb, -err);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001091 usb_unanchor_urb(urb);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001092 }
1093
1094 usb_free_urb(urb);
1095
1096 return err;
1097}
1098
Marcel Holtmann9d08f502015-10-08 20:23:08 +02001099static void btusb_diag_complete(struct urb *urb)
1100{
1101 struct hci_dev *hdev = urb->context;
1102 struct btusb_data *data = hci_get_drvdata(hdev);
1103 int err;
1104
1105 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1106 urb->actual_length);
1107
1108 if (urb->status == 0) {
1109 struct sk_buff *skb;
1110
1111 skb = bt_skb_alloc(urb->actual_length, GFP_ATOMIC);
1112 if (skb) {
Johannes Berg59ae1d12017-06-16 14:29:20 +02001113 skb_put_data(skb, urb->transfer_buffer,
1114 urb->actual_length);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02001115 hci_recv_diag(hdev, skb);
1116 }
1117 } else if (urb->status == -ENOENT) {
1118 /* Avoid suspend failed when usb_kill_urb */
1119 return;
1120 }
1121
1122 if (!test_bit(BTUSB_DIAG_RUNNING, &data->flags))
1123 return;
1124
1125 usb_anchor_urb(urb, &data->diag_anchor);
1126 usb_mark_last_busy(data->udev);
1127
1128 err = usb_submit_urb(urb, GFP_ATOMIC);
1129 if (err < 0) {
1130 /* -EPERM: urb is being killed;
Derek Robsond98422c2017-07-22 13:47:07 +12001131 * -ENODEV: device got disconnected
1132 */
Marcel Holtmann9d08f502015-10-08 20:23:08 +02001133 if (err != -EPERM && err != -ENODEV)
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001134 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
1135 urb, -err);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02001136 usb_unanchor_urb(urb);
1137 }
1138}
1139
1140static int btusb_submit_diag_urb(struct hci_dev *hdev, gfp_t mem_flags)
1141{
1142 struct btusb_data *data = hci_get_drvdata(hdev);
1143 struct urb *urb;
1144 unsigned char *buf;
1145 unsigned int pipe;
1146 int err, size = HCI_MAX_FRAME_SIZE;
1147
1148 BT_DBG("%s", hdev->name);
1149
1150 if (!data->diag_rx_ep)
1151 return -ENODEV;
1152
1153 urb = usb_alloc_urb(0, mem_flags);
1154 if (!urb)
1155 return -ENOMEM;
1156
1157 buf = kmalloc(size, mem_flags);
1158 if (!buf) {
1159 usb_free_urb(urb);
1160 return -ENOMEM;
1161 }
1162
1163 pipe = usb_rcvbulkpipe(data->udev, data->diag_rx_ep->bEndpointAddress);
1164
1165 usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
1166 btusb_diag_complete, hdev);
1167
1168 urb->transfer_flags |= URB_FREE_BUFFER;
1169
1170 usb_mark_last_busy(data->udev);
1171 usb_anchor_urb(urb, &data->diag_anchor);
1172
1173 err = usb_submit_urb(urb, mem_flags);
1174 if (err < 0) {
1175 if (err != -EPERM && err != -ENODEV)
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001176 bt_dev_err(hdev, "urb %p submission failed (%d)",
1177 urb, -err);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02001178 usb_unanchor_urb(urb);
1179 }
1180
1181 usb_free_urb(urb);
1182
1183 return err;
1184}
1185
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001186static void btusb_tx_complete(struct urb *urb)
1187{
1188 struct sk_buff *skb = urb->context;
Marcel Holtmann89e75332014-09-16 04:44:50 +02001189 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
David Herrmann155961e2012-02-09 21:58:32 +01001190 struct btusb_data *data = hci_get_drvdata(hdev);
Sebastian Andrzej Siewiora5e50d52018-06-19 23:56:57 +02001191 unsigned long flags;
Oliver Neukum7bee5492009-08-24 23:44:59 +02001192
Marcel Holtmann89e75332014-09-16 04:44:50 +02001193 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1194 urb->actual_length);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001195
1196 if (!test_bit(HCI_RUNNING, &hdev->flags))
1197 goto done;
1198
1199 if (!urb->status)
1200 hdev->stat.byte_tx += urb->transfer_buffer_length;
1201 else
1202 hdev->stat.err_tx++;
1203
1204done:
Sebastian Andrzej Siewiora5e50d52018-06-19 23:56:57 +02001205 spin_lock_irqsave(&data->txlock, flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001206 data->tx_in_flight--;
Sebastian Andrzej Siewiora5e50d52018-06-19 23:56:57 +02001207 spin_unlock_irqrestore(&data->txlock, flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001208
1209 kfree(urb->setup_packet);
1210
1211 kfree_skb(skb);
1212}
1213
1214static void btusb_isoc_tx_complete(struct urb *urb)
1215{
1216 struct sk_buff *skb = urb->context;
Marcel Holtmann89e75332014-09-16 04:44:50 +02001217 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001218
Marcel Holtmann89e75332014-09-16 04:44:50 +02001219 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1220 urb->actual_length);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001221
1222 if (!test_bit(HCI_RUNNING, &hdev->flags))
1223 goto done;
1224
1225 if (!urb->status)
1226 hdev->stat.byte_tx += urb->transfer_buffer_length;
1227 else
1228 hdev->stat.err_tx++;
1229
1230done:
1231 kfree(urb->setup_packet);
1232
1233 kfree_skb(skb);
1234}
1235
1236static int btusb_open(struct hci_dev *hdev)
1237{
David Herrmann155961e2012-02-09 21:58:32 +01001238 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001239 int err;
1240
1241 BT_DBG("%s", hdev->name);
1242
Ethan Hsiehc7e163f2016-10-07 12:06:42 +08001243 err = usb_autopm_get_interface(data->intf);
1244 if (err < 0)
1245 return err;
1246
Kim, Ben Young Taeace31982015-02-15 23:06:14 +00001247 /* Patching USB firmware files prior to starting any URBs of HCI path
1248 * It is more safe to use USB bulk channel for downloading USB patch
1249 */
1250 if (data->setup_on_usb) {
1251 err = data->setup_on_usb(hdev);
Marcel Holtmanneb500422015-04-16 23:15:50 +02001252 if (err < 0)
Oliver Neukum3d44a6f2019-11-14 16:01:18 +01001253 goto setup_fail;
Kim, Ben Young Taeace31982015-02-15 23:06:14 +00001254 }
1255
Oliver Neukum7bee5492009-08-24 23:44:59 +02001256 data->intf->needs_remote_wakeup = 1;
1257
Alex Lu9e455242019-08-14 20:02:52 +08001258 /* Disable device remote wakeup when host is suspended
1259 * For Realtek chips, global suspend without
1260 * SET_FEATURE (DEVICE_REMOTE_WAKEUP) can save more power in device.
1261 */
1262 if (test_bit(BTUSB_WAKEUP_DISABLE, &data->flags))
1263 device_wakeup_disable(&data->udev->dev);
1264
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001265 if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +02001266 goto done;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001267
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001268 err = btusb_submit_intr_urb(hdev, GFP_KERNEL);
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001269 if (err < 0)
1270 goto failed;
1271
1272 err = btusb_submit_bulk_urb(hdev, GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001273 if (err < 0) {
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001274 usb_kill_anchored_urbs(&data->intr_anchor);
1275 goto failed;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001276 }
1277
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001278 set_bit(BTUSB_BULK_RUNNING, &data->flags);
1279 btusb_submit_bulk_urb(hdev, GFP_KERNEL);
1280
Marcel Holtmann9d08f502015-10-08 20:23:08 +02001281 if (data->diag) {
1282 if (!btusb_submit_diag_urb(hdev, GFP_KERNEL))
1283 set_bit(BTUSB_DIAG_RUNNING, &data->flags);
1284 }
1285
Oliver Neukum7bee5492009-08-24 23:44:59 +02001286done:
1287 usb_autopm_put_interface(data->intf);
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001288 return 0;
1289
1290failed:
1291 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Oliver Neukum3d44a6f2019-11-14 16:01:18 +01001292setup_fail:
Oliver Neukum7bee5492009-08-24 23:44:59 +02001293 usb_autopm_put_interface(data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001294 return err;
1295}
1296
Oliver Neukum7bee5492009-08-24 23:44:59 +02001297static void btusb_stop_traffic(struct btusb_data *data)
1298{
1299 usb_kill_anchored_urbs(&data->intr_anchor);
1300 usb_kill_anchored_urbs(&data->bulk_anchor);
1301 usb_kill_anchored_urbs(&data->isoc_anchor);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02001302 usb_kill_anchored_urbs(&data->diag_anchor);
Sean Wanga1c49c432019-06-02 08:02:48 +08001303 usb_kill_anchored_urbs(&data->ctrl_anchor);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001304}
1305
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001306static int btusb_close(struct hci_dev *hdev)
1307{
David Herrmann155961e2012-02-09 21:58:32 +01001308 struct btusb_data *data = hci_get_drvdata(hdev);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001309 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001310
1311 BT_DBG("%s", hdev->name);
1312
Marcel Holtmanne8c3c3d2008-09-23 00:16:36 +02001313 cancel_work_sync(&data->work);
Linus Torvalds404291a2009-11-11 13:32:29 -08001314 cancel_work_sync(&data->waker);
Marcel Holtmanne8c3c3d2008-09-23 00:16:36 +02001315
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001316 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001317 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001318 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02001319 clear_bit(BTUSB_DIAG_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001320
1321 btusb_stop_traffic(data);
Marcel Holtmann803b5832014-09-16 08:00:29 +02001322 btusb_free_frags(data);
1323
Oliver Neukum7bee5492009-08-24 23:44:59 +02001324 err = usb_autopm_get_interface(data->intf);
1325 if (err < 0)
Oliver Neukum7b8e2c12009-11-13 14:26:23 +01001326 goto failed;
Oliver Neukum7bee5492009-08-24 23:44:59 +02001327
1328 data->intf->needs_remote_wakeup = 0;
Alex Lu9e455242019-08-14 20:02:52 +08001329
1330 /* Enable remote wake up for auto-suspend */
1331 if (test_bit(BTUSB_WAKEUP_DISABLE, &data->flags))
1332 data->intf->needs_remote_wakeup = 1;
1333
Oliver Neukum7bee5492009-08-24 23:44:59 +02001334 usb_autopm_put_interface(data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001335
Oliver Neukum7b8e2c12009-11-13 14:26:23 +01001336failed:
1337 usb_scuttle_anchored_urbs(&data->deferred);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001338 return 0;
1339}
1340
1341static int btusb_flush(struct hci_dev *hdev)
1342{
David Herrmann155961e2012-02-09 21:58:32 +01001343 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001344
1345 BT_DBG("%s", hdev->name);
1346
1347 usb_kill_anchored_urbs(&data->tx_anchor);
Marcel Holtmann803b5832014-09-16 08:00:29 +02001348 btusb_free_frags(data);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001349
1350 return 0;
1351}
1352
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001353static struct urb *alloc_ctrl_urb(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001354{
David Herrmann155961e2012-02-09 21:58:32 +01001355 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001356 struct usb_ctrlrequest *dr;
1357 struct urb *urb;
1358 unsigned int pipe;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001359
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001360 urb = usb_alloc_urb(0, GFP_KERNEL);
1361 if (!urb)
1362 return ERR_PTR(-ENOMEM);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001363
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001364 dr = kmalloc(sizeof(*dr), GFP_KERNEL);
1365 if (!dr) {
1366 usb_free_urb(urb);
1367 return ERR_PTR(-ENOMEM);
1368 }
1369
1370 dr->bRequestType = data->cmdreq_type;
Marcel Holtmann893ba542015-01-28 20:27:34 -08001371 dr->bRequest = data->cmdreq;
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001372 dr->wIndex = 0;
1373 dr->wValue = 0;
1374 dr->wLength = __cpu_to_le16(skb->len);
1375
1376 pipe = usb_sndctrlpipe(data->udev, 0x00);
1377
Marcel Holtmann89e75332014-09-16 04:44:50 +02001378 usb_fill_control_urb(urb, data->udev, pipe, (void *)dr,
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001379 skb->data, skb->len, btusb_tx_complete, skb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001380
Marcel Holtmann89e75332014-09-16 04:44:50 +02001381 skb->dev = (void *)hdev;
Marcel Holtmann7bd8f092013-10-11 06:19:18 -07001382
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001383 return urb;
1384}
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001385
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001386static struct urb *alloc_bulk_urb(struct hci_dev *hdev, struct sk_buff *skb)
1387{
1388 struct btusb_data *data = hci_get_drvdata(hdev);
1389 struct urb *urb;
1390 unsigned int pipe;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001391
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001392 if (!data->bulk_tx_ep)
1393 return ERR_PTR(-ENODEV);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001394
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001395 urb = usb_alloc_urb(0, GFP_KERNEL);
1396 if (!urb)
1397 return ERR_PTR(-ENOMEM);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001398
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001399 pipe = usb_sndbulkpipe(data->udev, data->bulk_tx_ep->bEndpointAddress);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001400
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001401 usb_fill_bulk_urb(urb, data->udev, pipe,
1402 skb->data, skb->len, btusb_tx_complete, skb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001403
Marcel Holtmann89e75332014-09-16 04:44:50 +02001404 skb->dev = (void *)hdev;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001405
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001406 return urb;
1407}
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001408
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001409static struct urb *alloc_isoc_urb(struct hci_dev *hdev, struct sk_buff *skb)
1410{
1411 struct btusb_data *data = hci_get_drvdata(hdev);
1412 struct urb *urb;
1413 unsigned int pipe;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001414
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001415 if (!data->isoc_tx_ep)
1416 return ERR_PTR(-ENODEV);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001417
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001418 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_KERNEL);
1419 if (!urb)
1420 return ERR_PTR(-ENOMEM);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001421
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001422 pipe = usb_sndisocpipe(data->udev, data->isoc_tx_ep->bEndpointAddress);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001423
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001424 usb_fill_int_urb(urb, data->udev, pipe,
1425 skb->data, skb->len, btusb_isoc_tx_complete,
1426 skb, data->isoc_tx_ep->bInterval);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001427
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001428 urb->transfer_flags = URB_ISO_ASAP;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001429
Sathish Narasimmanbaac6272020-04-03 21:43:59 +02001430 if (data->isoc_altsetting == 6)
1431 __fill_isoc_descriptor_msbc(urb, skb->len,
1432 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize),
1433 data);
1434 else
1435 __fill_isoc_descriptor(urb, skb->len,
1436 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
Marcel Holtmann89e75332014-09-16 04:44:50 +02001437 skb->dev = (void *)hdev;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001438
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001439 return urb;
1440}
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001441
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001442static int submit_tx_urb(struct hci_dev *hdev, struct urb *urb)
1443{
1444 struct btusb_data *data = hci_get_drvdata(hdev);
1445 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001446
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001447 usb_anchor_urb(urb, &data->tx_anchor);
1448
Johan Hedberge9753ef2014-09-14 08:49:34 +03001449 err = usb_submit_urb(urb, GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001450 if (err < 0) {
Paul Bolle5a9b80e2011-10-09 12:12:16 +02001451 if (err != -EPERM && err != -ENODEV)
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001452 bt_dev_err(hdev, "urb %p submission failed (%d)",
1453 urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001454 kfree(urb->setup_packet);
1455 usb_unanchor_urb(urb);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001456 } else {
1457 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001458 }
1459
Cong Wang54a8a792011-11-22 09:32:57 +08001460 usb_free_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001461 return err;
1462}
1463
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001464static int submit_or_queue_tx_urb(struct hci_dev *hdev, struct urb *urb)
1465{
1466 struct btusb_data *data = hci_get_drvdata(hdev);
1467 unsigned long flags;
1468 bool suspending;
1469
1470 spin_lock_irqsave(&data->txlock, flags);
1471 suspending = test_bit(BTUSB_SUSPENDING, &data->flags);
1472 if (!suspending)
1473 data->tx_in_flight++;
1474 spin_unlock_irqrestore(&data->txlock, flags);
1475
1476 if (!suspending)
1477 return submit_tx_urb(hdev, urb);
1478
1479 usb_anchor_urb(urb, &data->deferred);
1480 schedule_work(&data->waker);
1481
1482 usb_free_urb(urb);
1483 return 0;
1484}
1485
1486static int btusb_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
1487{
1488 struct urb *urb;
1489
1490 BT_DBG("%s", hdev->name);
1491
Marcel Holtmann618e8bc2015-11-05 07:33:56 +01001492 switch (hci_skb_pkt_type(skb)) {
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001493 case HCI_COMMAND_PKT:
1494 urb = alloc_ctrl_urb(hdev, skb);
1495 if (IS_ERR(urb))
1496 return PTR_ERR(urb);
1497
1498 hdev->stat.cmd_tx++;
1499 return submit_or_queue_tx_urb(hdev, urb);
1500
1501 case HCI_ACLDATA_PKT:
1502 urb = alloc_bulk_urb(hdev, skb);
1503 if (IS_ERR(urb))
1504 return PTR_ERR(urb);
1505
1506 hdev->stat.acl_tx++;
1507 return submit_or_queue_tx_urb(hdev, urb);
1508
1509 case HCI_SCODATA_PKT:
1510 if (hci_conn_num(hdev, SCO_LINK) < 1)
1511 return -ENODEV;
1512
1513 urb = alloc_isoc_urb(hdev, skb);
1514 if (IS_ERR(urb))
1515 return PTR_ERR(urb);
1516
1517 hdev->stat.sco_tx++;
1518 return submit_tx_urb(hdev, urb);
1519 }
1520
1521 return -EILSEQ;
1522}
1523
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001524static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
1525{
David Herrmann155961e2012-02-09 21:58:32 +01001526 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001527
1528 BT_DBG("%s evt %d", hdev->name, evt);
1529
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001530 if (hci_conn_num(hdev, SCO_LINK) != data->sco_num) {
1531 data->sco_num = hci_conn_num(hdev, SCO_LINK);
Sathish Narasimmanbaac6272020-04-03 21:43:59 +02001532 data->air_mode = evt;
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001533 schedule_work(&data->work);
Marcel Holtmanna780efa2008-11-30 12:17:12 +01001534 }
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001535}
1536
Jesper Juhl42b16b32011-01-17 00:09:38 +01001537static inline int __set_isoc_interface(struct hci_dev *hdev, int altsetting)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001538{
David Herrmann155961e2012-02-09 21:58:32 +01001539 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001540 struct usb_interface *intf = data->isoc;
1541 struct usb_endpoint_descriptor *ep_desc;
1542 int i, err;
1543
1544 if (!data->isoc)
1545 return -ENODEV;
1546
Marcel Holtmann459232f2017-10-24 19:42:45 +02001547 err = usb_set_interface(data->udev, data->isoc_ifnum, altsetting);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001548 if (err < 0) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001549 bt_dev_err(hdev, "setting interface failed (%d)", -err);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001550 return err;
1551 }
1552
1553 data->isoc_altsetting = altsetting;
1554
1555 data->isoc_tx_ep = NULL;
1556 data->isoc_rx_ep = NULL;
1557
1558 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
1559 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
1560
1561 if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) {
1562 data->isoc_tx_ep = ep_desc;
1563 continue;
1564 }
1565
1566 if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) {
1567 data->isoc_rx_ep = ep_desc;
1568 continue;
1569 }
1570 }
1571
1572 if (!data->isoc_tx_ep || !data->isoc_rx_ep) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001573 bt_dev_err(hdev, "invalid SCO descriptors");
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001574 return -ENODEV;
1575 }
1576
1577 return 0;
1578}
1579
Sathish Narasimmanbaac6272020-04-03 21:43:59 +02001580static int btusb_switch_alt_setting(struct hci_dev *hdev, int new_alts)
1581{
1582 struct btusb_data *data = hci_get_drvdata(hdev);
1583 int err;
1584
1585 if (data->isoc_altsetting != new_alts) {
1586 unsigned long flags;
1587
1588 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1589 usb_kill_anchored_urbs(&data->isoc_anchor);
1590
1591 /* When isochronous alternate setting needs to be
1592 * changed, because SCO connection has been added
1593 * or removed, a packet fragment may be left in the
1594 * reassembling state. This could lead to wrongly
1595 * assembled fragments.
1596 *
1597 * Clear outstanding fragment when selecting a new
1598 * alternate setting.
1599 */
1600 spin_lock_irqsave(&data->rxlock, flags);
1601 kfree_skb(data->sco_skb);
1602 data->sco_skb = NULL;
1603 spin_unlock_irqrestore(&data->rxlock, flags);
1604
1605 err = __set_isoc_interface(hdev, new_alts);
1606 if (err < 0)
1607 return err;
1608 }
1609
1610 if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
1611 if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0)
1612 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1613 else
1614 btusb_submit_isoc_urb(hdev, GFP_KERNEL);
1615 }
1616
1617 return 0;
1618}
1619
1620static struct usb_host_interface *btusb_find_altsetting(struct btusb_data *data,
1621 int alt)
1622{
1623 struct usb_interface *intf = data->isoc;
1624 int i;
1625
1626 BT_DBG("Looking for Alt no :%d", alt);
1627
Sathish Narasimmanfcd156ee2020-04-08 10:57:03 +05301628 if (!intf)
1629 return NULL;
1630
Sathish Narasimmanbaac6272020-04-03 21:43:59 +02001631 for (i = 0; i < intf->num_altsetting; i++) {
1632 if (intf->altsetting[i].desc.bAlternateSetting == alt)
1633 return &intf->altsetting[i];
1634 }
1635
1636 return NULL;
1637}
1638
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001639static void btusb_work(struct work_struct *work)
1640{
1641 struct btusb_data *data = container_of(work, struct btusb_data, work);
1642 struct hci_dev *hdev = data->hdev;
Sathish Narasimmanbaac6272020-04-03 21:43:59 +02001643 int new_alts = 0;
Oliver Neukum7bee5492009-08-24 23:44:59 +02001644 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001645
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001646 if (data->sco_num > 0) {
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -03001647 if (!test_bit(BTUSB_DID_ISO_RESUME, &data->flags)) {
Oliver Neukum8efdd0c2011-02-11 13:00:06 +01001648 err = usb_autopm_get_interface(data->isoc ? data->isoc : data->intf);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001649 if (err < 0) {
1650 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1651 usb_kill_anchored_urbs(&data->isoc_anchor);
1652 return;
1653 }
1654
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -03001655 set_bit(BTUSB_DID_ISO_RESUME, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001656 }
Mikel Astizf4001d22012-04-11 08:48:51 +02001657
Sathish Narasimmanbaac6272020-04-03 21:43:59 +02001658 if (data->air_mode == HCI_NOTIFY_ENABLE_SCO_CVSD) {
1659 if (hdev->voice_setting & 0x0020) {
1660 static const int alts[3] = { 2, 4, 5 };
Marcel Holtmann89e75332014-09-16 04:44:50 +02001661
Sathish Narasimmanbaac6272020-04-03 21:43:59 +02001662 new_alts = alts[data->sco_num - 1];
1663 } else {
1664 new_alts = data->sco_num;
1665 }
1666 } else if (data->air_mode == HCI_NOTIFY_ENABLE_SCO_TRANSP) {
Mikel Astizf4001d22012-04-11 08:48:51 +02001667
Sathish Narasimmanbaac6272020-04-03 21:43:59 +02001668 data->usb_alt6_packet_flow = true;
Kuba Pawlakf6fc86f2015-10-28 15:18:05 +01001669
Sathish Narasimmanbaac6272020-04-03 21:43:59 +02001670 /* Check if Alt 6 is supported for Transparent audio */
1671 if (btusb_find_altsetting(data, 6))
1672 new_alts = 6;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001673 else
Sathish Narasimmanbaac6272020-04-03 21:43:59 +02001674 bt_dev_err(hdev, "Device does not support ALT setting 6");
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001675 }
Sathish Narasimmanbaac6272020-04-03 21:43:59 +02001676
1677 if (btusb_switch_alt_setting(hdev, new_alts) < 0)
1678 bt_dev_err(hdev, "set USB alt:(%d) failed!", new_alts);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001679 } else {
1680 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1681 usb_kill_anchored_urbs(&data->isoc_anchor);
1682
1683 __set_isoc_interface(hdev, 0);
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -03001684 if (test_and_clear_bit(BTUSB_DID_ISO_RESUME, &data->flags))
Oliver Neukum8efdd0c2011-02-11 13:00:06 +01001685 usb_autopm_put_interface(data->isoc ? data->isoc : data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001686 }
1687}
1688
Oliver Neukum7bee5492009-08-24 23:44:59 +02001689static void btusb_waker(struct work_struct *work)
1690{
1691 struct btusb_data *data = container_of(work, struct btusb_data, waker);
1692 int err;
1693
1694 err = usb_autopm_get_interface(data->intf);
1695 if (err < 0)
1696 return;
1697
1698 usb_autopm_put_interface(data->intf);
1699}
1700
Marcel Holtmann9f8f9622013-04-10 08:11:35 -07001701static int btusb_setup_bcm92035(struct hci_dev *hdev)
1702{
1703 struct sk_buff *skb;
1704 u8 val = 0x00;
1705
1706 BT_DBG("%s", hdev->name);
1707
1708 skb = __hci_cmd_sync(hdev, 0xfc3b, 1, &val, HCI_INIT_TIMEOUT);
1709 if (IS_ERR(skb))
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001710 bt_dev_err(hdev, "BCM92035 command failed (%ld)", PTR_ERR(skb));
Marcel Holtmann9f8f9622013-04-10 08:11:35 -07001711 else
1712 kfree_skb(skb);
1713
1714 return 0;
1715}
1716
Marcel Holtmann81cac642014-01-03 03:02:36 -08001717static int btusb_setup_csr(struct hci_dev *hdev)
1718{
1719 struct hci_rp_read_local_version *rp;
1720 struct sk_buff *skb;
Marcel Holtmann81cac642014-01-03 03:02:36 -08001721
1722 BT_DBG("%s", hdev->name);
1723
Marcel Holtmann7cd84d72015-06-07 10:01:02 +02001724 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
1725 HCI_INIT_TIMEOUT);
1726 if (IS_ERR(skb)) {
1727 int err = PTR_ERR(skb);
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001728 bt_dev_err(hdev, "CSR: Local version failed (%d)", err);
Marcel Holtmann7cd84d72015-06-07 10:01:02 +02001729 return err;
1730 }
1731
1732 if (skb->len != sizeof(struct hci_rp_read_local_version)) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001733 bt_dev_err(hdev, "CSR: Local version length mismatch");
Marcel Holtmann7cd84d72015-06-07 10:01:02 +02001734 kfree_skb(skb);
1735 return -EIO;
1736 }
Marcel Holtmann81cac642014-01-03 03:02:36 -08001737
Marcel Holtmann89e75332014-09-16 04:44:50 +02001738 rp = (struct hci_rp_read_local_version *)skb->data;
Marcel Holtmann81cac642014-01-03 03:02:36 -08001739
Johan Hedberg6cafcd92015-08-30 21:47:21 +03001740 /* Detect controllers which aren't real CSR ones. */
1741 if (le16_to_cpu(rp->manufacturer) != 10 ||
1742 le16_to_cpu(rp->lmp_subver) == 0x0c5c) {
Marcel Holtmann9641d342015-06-07 10:01:01 +02001743 /* Clear the reset quirk since this is not an actual
1744 * early Bluetooth 1.1 device from CSR.
1745 */
1746 clear_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmann81cac642014-01-03 03:02:36 -08001747
Marcel Holtmann9641d342015-06-07 10:01:01 +02001748 /* These fake CSR controllers have all a broken
1749 * stored link key handling and so just disable it.
1750 */
1751 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
Marcel Holtmann81cac642014-01-03 03:02:36 -08001752 }
1753
Marcel Holtmann81cac642014-01-03 03:02:36 -08001754 kfree_skb(skb);
1755
Marcel Holtmann9641d342015-06-07 10:01:01 +02001756 return 0;
Marcel Holtmann81cac642014-01-03 03:02:36 -08001757}
1758
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001759static const struct firmware *btusb_setup_intel_get_fw(struct hci_dev *hdev,
Marcel Holtmann89e75332014-09-16 04:44:50 +02001760 struct intel_version *ver)
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001761{
1762 const struct firmware *fw;
1763 char fwname[64];
1764 int ret;
1765
1766 snprintf(fwname, sizeof(fwname),
1767 "intel/ibt-hw-%x.%x.%x-fw-%x.%x.%x.%x.%x.bseq",
1768 ver->hw_platform, ver->hw_variant, ver->hw_revision,
1769 ver->fw_variant, ver->fw_revision, ver->fw_build_num,
1770 ver->fw_build_ww, ver->fw_build_yy);
1771
1772 ret = request_firmware(&fw, fwname, &hdev->dev);
1773 if (ret < 0) {
1774 if (ret == -EINVAL) {
Marcel Holtmann85418fe2018-08-03 11:20:49 +02001775 bt_dev_err(hdev, "Intel firmware file request failed (%d)",
1776 ret);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001777 return NULL;
1778 }
1779
Marcel Holtmann85418fe2018-08-03 11:20:49 +02001780 bt_dev_err(hdev, "failed to open Intel firmware file: %s (%d)",
1781 fwname, ret);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001782
1783 /* If the correct firmware patch file is not found, use the
1784 * default firmware patch file instead
1785 */
1786 snprintf(fwname, sizeof(fwname), "intel/ibt-hw-%x.%x.bseq",
1787 ver->hw_platform, ver->hw_variant);
1788 if (request_firmware(&fw, fwname, &hdev->dev) < 0) {
Marcel Holtmann85418fe2018-08-03 11:20:49 +02001789 bt_dev_err(hdev, "failed to open default fw file: %s",
1790 fwname);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001791 return NULL;
1792 }
1793 }
1794
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001795 bt_dev_info(hdev, "Intel Bluetooth firmware file: %s", fwname);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001796
1797 return fw;
1798}
1799
1800static int btusb_setup_intel_patching(struct hci_dev *hdev,
1801 const struct firmware *fw,
1802 const u8 **fw_ptr, int *disable_patch)
1803{
1804 struct sk_buff *skb;
1805 struct hci_command_hdr *cmd;
1806 const u8 *cmd_param;
1807 struct hci_event_hdr *evt = NULL;
1808 const u8 *evt_param = NULL;
1809 int remain = fw->size - (*fw_ptr - fw->data);
1810
1811 /* The first byte indicates the types of the patch command or event.
1812 * 0x01 means HCI command and 0x02 is HCI event. If the first bytes
1813 * in the current firmware buffer doesn't start with 0x01 or
1814 * the size of remain buffer is smaller than HCI command header,
1815 * the firmware file is corrupted and it should stop the patching
1816 * process.
1817 */
1818 if (remain > HCI_COMMAND_HDR_SIZE && *fw_ptr[0] != 0x01) {
Marcel Holtmann85418fe2018-08-03 11:20:49 +02001819 bt_dev_err(hdev, "Intel fw corrupted: invalid cmd read");
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001820 return -EINVAL;
1821 }
1822 (*fw_ptr)++;
1823 remain--;
1824
1825 cmd = (struct hci_command_hdr *)(*fw_ptr);
1826 *fw_ptr += sizeof(*cmd);
1827 remain -= sizeof(*cmd);
1828
1829 /* Ensure that the remain firmware data is long enough than the length
1830 * of command parameter. If not, the firmware file is corrupted.
1831 */
1832 if (remain < cmd->plen) {
Marcel Holtmann85418fe2018-08-03 11:20:49 +02001833 bt_dev_err(hdev, "Intel fw corrupted: invalid cmd len");
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001834 return -EFAULT;
1835 }
1836
1837 /* If there is a command that loads a patch in the firmware
1838 * file, then enable the patch upon success, otherwise just
1839 * disable the manufacturer mode, for example patch activation
1840 * is not required when the default firmware patch file is used
1841 * because there are no patch data to load.
1842 */
1843 if (*disable_patch && le16_to_cpu(cmd->opcode) == 0xfc8e)
1844 *disable_patch = 0;
1845
1846 cmd_param = *fw_ptr;
1847 *fw_ptr += cmd->plen;
1848 remain -= cmd->plen;
1849
1850 /* This reads the expected events when the above command is sent to the
1851 * device. Some vendor commands expects more than one events, for
1852 * example command status event followed by vendor specific event.
1853 * For this case, it only keeps the last expected event. so the command
1854 * can be sent with __hci_cmd_sync_ev() which returns the sk_buff of
1855 * last expected event.
1856 */
1857 while (remain > HCI_EVENT_HDR_SIZE && *fw_ptr[0] == 0x02) {
1858 (*fw_ptr)++;
1859 remain--;
1860
1861 evt = (struct hci_event_hdr *)(*fw_ptr);
1862 *fw_ptr += sizeof(*evt);
1863 remain -= sizeof(*evt);
1864
1865 if (remain < evt->plen) {
Marcel Holtmann85418fe2018-08-03 11:20:49 +02001866 bt_dev_err(hdev, "Intel fw corrupted: invalid evt len");
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001867 return -EFAULT;
1868 }
1869
1870 evt_param = *fw_ptr;
1871 *fw_ptr += evt->plen;
1872 remain -= evt->plen;
1873 }
1874
1875 /* Every HCI commands in the firmware file has its correspond event.
1876 * If event is not found or remain is smaller than zero, the firmware
1877 * file is corrupted.
1878 */
1879 if (!evt || !evt_param || remain < 0) {
Marcel Holtmann85418fe2018-08-03 11:20:49 +02001880 bt_dev_err(hdev, "Intel fw corrupted: invalid evt read");
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001881 return -EFAULT;
1882 }
1883
1884 skb = __hci_cmd_sync_ev(hdev, le16_to_cpu(cmd->opcode), cmd->plen,
1885 cmd_param, evt->evt, HCI_INIT_TIMEOUT);
1886 if (IS_ERR(skb)) {
Marcel Holtmann85418fe2018-08-03 11:20:49 +02001887 bt_dev_err(hdev, "sending Intel patch command (0x%4.4x) failed (%ld)",
1888 cmd->opcode, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001889 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001890 }
1891
1892 /* It ensures that the returned event matches the event data read from
1893 * the firmware file. At fist, it checks the length and then
1894 * the contents of the event.
1895 */
1896 if (skb->len != evt->plen) {
Marcel Holtmann85418fe2018-08-03 11:20:49 +02001897 bt_dev_err(hdev, "mismatch event length (opcode 0x%4.4x)",
1898 le16_to_cpu(cmd->opcode));
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001899 kfree_skb(skb);
1900 return -EFAULT;
1901 }
1902
1903 if (memcmp(skb->data, evt_param, evt->plen)) {
Marcel Holtmann85418fe2018-08-03 11:20:49 +02001904 bt_dev_err(hdev, "mismatch event parameter (opcode 0x%4.4x)",
1905 le16_to_cpu(cmd->opcode));
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001906 kfree_skb(skb);
1907 return -EFAULT;
1908 }
1909 kfree_skb(skb);
1910
1911 return 0;
1912}
1913
1914static int btusb_setup_intel(struct hci_dev *hdev)
1915{
1916 struct sk_buff *skb;
1917 const struct firmware *fw;
1918 const u8 *fw_ptr;
Loic Poulain28dc4b92015-12-03 16:10:22 +01001919 int disable_patch, err;
Loic Poulain6c483de2015-12-06 16:18:34 +01001920 struct intel_version ver;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001921
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001922 BT_DBG("%s", hdev->name);
1923
1924 /* The controller has a bug with the first HCI command sent to it
1925 * returning number of completed commands as zero. This would stall the
1926 * command processing in the Bluetooth core.
1927 *
1928 * As a workaround, send HCI Reset command first which will reset the
1929 * number of completed commands and allow normal command processing
1930 * from now on.
1931 */
1932 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
1933 if (IS_ERR(skb)) {
Marcel Holtmann85418fe2018-08-03 11:20:49 +02001934 bt_dev_err(hdev, "sending initial HCI reset command failed (%ld)",
1935 PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001936 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001937 }
1938 kfree_skb(skb);
1939
1940 /* Read Intel specific controller version first to allow selection of
1941 * which firmware file to load.
1942 *
1943 * The returned information are hardware variant and revision plus
1944 * firmware variant, revision and build number.
1945 */
Loic Poulain6c483de2015-12-06 16:18:34 +01001946 err = btintel_read_version(hdev, &ver);
1947 if (err)
1948 return err;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001949
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001950 bt_dev_info(hdev, "read Intel version: %02x%02x%02x%02x%02x%02x%02x%02x%02x",
1951 ver.hw_platform, ver.hw_variant, ver.hw_revision,
1952 ver.fw_variant, ver.fw_revision, ver.fw_build_num,
1953 ver.fw_build_ww, ver.fw_build_yy, ver.fw_patch_num);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001954
1955 /* fw_patch_num indicates the version of patch the device currently
1956 * have. If there is no patch data in the device, it is always 0x00.
Minjune Kim5075eda2015-08-27 13:21:52 +09001957 * So, if it is other than 0x00, no need to patch the device again.
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001958 */
Loic Poulain6c483de2015-12-06 16:18:34 +01001959 if (ver.fw_patch_num) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001960 bt_dev_info(hdev, "Intel device is already patched. "
1961 "patch num: %02x", ver.fw_patch_num);
Marcel Holtmann213445b2015-10-21 02:45:19 +02001962 goto complete;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001963 }
1964
1965 /* Opens the firmware patch file based on the firmware version read
1966 * from the controller. If it fails to open the matching firmware
1967 * patch file, it tries to open the default firmware patch file.
1968 * If no patch file is found, allow the device to operate without
1969 * a patch.
1970 */
Loic Poulain6c483de2015-12-06 16:18:34 +01001971 fw = btusb_setup_intel_get_fw(hdev, &ver);
1972 if (!fw)
Marcel Holtmann213445b2015-10-21 02:45:19 +02001973 goto complete;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001974 fw_ptr = fw->data;
1975
Loic Poulain28dc4b92015-12-03 16:10:22 +01001976 /* Enable the manufacturer mode of the controller.
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001977 * Only while this mode is enabled, the driver can download the
1978 * firmware patch data and configuration parameters.
1979 */
Loic Poulain28dc4b92015-12-03 16:10:22 +01001980 err = btintel_enter_mfg(hdev);
1981 if (err) {
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001982 release_firmware(fw);
Loic Poulain28dc4b92015-12-03 16:10:22 +01001983 return err;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001984 }
1985
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001986 disable_patch = 1;
1987
1988 /* The firmware data file consists of list of Intel specific HCI
1989 * commands and its expected events. The first byte indicates the
1990 * type of the message, either HCI command or HCI event.
1991 *
1992 * It reads the command and its expected event from the firmware file,
1993 * and send to the controller. Once __hci_cmd_sync_ev() returns,
1994 * the returned event is compared with the event read from the firmware
1995 * file and it will continue until all the messages are downloaded to
1996 * the controller.
1997 *
1998 * Once the firmware patching is completed successfully,
1999 * the manufacturer mode is disabled with reset and activating the
2000 * downloaded patch.
2001 *
2002 * If the firmware patching fails, the manufacturer mode is
2003 * disabled with reset and deactivating the patch.
2004 *
2005 * If the default patch file is used, no reset is done when disabling
2006 * the manufacturer.
2007 */
2008 while (fw->size > fw_ptr - fw->data) {
2009 int ret;
2010
2011 ret = btusb_setup_intel_patching(hdev, fw, &fw_ptr,
2012 &disable_patch);
2013 if (ret < 0)
2014 goto exit_mfg_deactivate;
2015 }
2016
2017 release_firmware(fw);
2018
2019 if (disable_patch)
2020 goto exit_mfg_disable;
2021
2022 /* Patching completed successfully and disable the manufacturer mode
2023 * with reset and activate the downloaded firmware patches.
2024 */
Loic Poulain28dc4b92015-12-03 16:10:22 +01002025 err = btintel_exit_mfg(hdev, true, true);
2026 if (err)
2027 return err;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002028
Sukumar Ghorai905d7b12020-03-16 11:37:18 +05302029 /* Need build number for downloaded fw patches in
2030 * every power-on boot
2031 */
2032 err = btintel_read_version(hdev, &ver);
2033 if (err)
2034 return err;
2035 bt_dev_info(hdev, "Intel BT fw patch 0x%02x completed & activated",
2036 ver.fw_patch_num);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002037
Marcel Holtmann213445b2015-10-21 02:45:19 +02002038 goto complete;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002039
2040exit_mfg_disable:
2041 /* Disable the manufacturer mode without reset */
Loic Poulain28dc4b92015-12-03 16:10:22 +01002042 err = btintel_exit_mfg(hdev, false, false);
2043 if (err)
2044 return err;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002045
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002046 bt_dev_info(hdev, "Intel firmware patch completed");
Marcel Holtmann40cb0982014-07-02 12:06:45 +02002047
Marcel Holtmann213445b2015-10-21 02:45:19 +02002048 goto complete;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002049
2050exit_mfg_deactivate:
2051 release_firmware(fw);
2052
2053 /* Patching failed. Disable the manufacturer mode with reset and
2054 * deactivate the downloaded firmware patches.
2055 */
Loic Poulain28dc4b92015-12-03 16:10:22 +01002056 err = btintel_exit_mfg(hdev, true, false);
2057 if (err)
2058 return err;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002059
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002060 bt_dev_info(hdev, "Intel firmware patch completed and deactivated");
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002061
Marcel Holtmann213445b2015-10-21 02:45:19 +02002062complete:
2063 /* Set the event mask for Intel specific vendor events. This enables
2064 * a few extra events that are useful during general operation.
2065 */
2066 btintel_set_event_mask_mfg(hdev, false);
2067
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07002068 btintel_check_bdaddr(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002069 return 0;
2070}
2071
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002072static int inject_cmd_complete(struct hci_dev *hdev, __u16 opcode)
2073{
2074 struct sk_buff *skb;
2075 struct hci_event_hdr *hdr;
2076 struct hci_ev_cmd_complete *evt;
2077
Jia-Ju Baicf07e342018-07-23 11:38:51 +08002078 skb = bt_skb_alloc(sizeof(*hdr) + sizeof(*evt) + 1, GFP_KERNEL);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002079 if (!skb)
2080 return -ENOMEM;
2081
Johannes Berg4df864c2017-06-16 14:29:21 +02002082 hdr = skb_put(skb, sizeof(*hdr));
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002083 hdr->evt = HCI_EV_CMD_COMPLETE;
2084 hdr->plen = sizeof(*evt) + 1;
2085
Johannes Berg4df864c2017-06-16 14:29:21 +02002086 evt = skb_put(skb, sizeof(*evt));
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002087 evt->ncmd = 0x01;
2088 evt->opcode = cpu_to_le16(opcode);
2089
Johannes Berg634fef62017-06-16 14:29:24 +02002090 skb_put_u8(skb, 0x00);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002091
Marcel Holtmann618e8bc2015-11-05 07:33:56 +01002092 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002093
2094 return hci_recv_frame(hdev, skb);
2095}
2096
2097static int btusb_recv_bulk_intel(struct btusb_data *data, void *buffer,
2098 int count)
2099{
2100 /* When the device is in bootloader mode, then it can send
2101 * events via the bulk endpoint. These events are treated the
2102 * same way as the ones received from the interrupt endpoint.
2103 */
2104 if (test_bit(BTUSB_BOOTLOADER, &data->flags))
2105 return btusb_recv_intr(data, buffer, count);
2106
2107 return btusb_recv_bulk(data, buffer, count);
2108}
2109
Marcel Holtmannccd6da22015-04-09 00:35:19 -07002110static void btusb_intel_bootup(struct btusb_data *data, const void *ptr,
2111 unsigned int len)
2112{
2113 const struct intel_bootup *evt = ptr;
2114
2115 if (len != sizeof(*evt))
2116 return;
2117
Andrea Parridff6d592018-11-27 12:22:25 +01002118 if (test_and_clear_bit(BTUSB_BOOTING, &data->flags))
Marcel Holtmannccd6da22015-04-09 00:35:19 -07002119 wake_up_bit(&data->flags, BTUSB_BOOTING);
Marcel Holtmannccd6da22015-04-09 00:35:19 -07002120}
2121
2122static void btusb_intel_secure_send_result(struct btusb_data *data,
2123 const void *ptr, unsigned int len)
2124{
2125 const struct intel_secure_send_result *evt = ptr;
2126
2127 if (len != sizeof(*evt))
2128 return;
2129
2130 if (evt->result)
2131 set_bit(BTUSB_FIRMWARE_FAILED, &data->flags);
2132
2133 if (test_and_clear_bit(BTUSB_DOWNLOADING, &data->flags) &&
Andrea Parridff6d592018-11-27 12:22:25 +01002134 test_bit(BTUSB_FIRMWARE_LOADED, &data->flags))
Marcel Holtmannccd6da22015-04-09 00:35:19 -07002135 wake_up_bit(&data->flags, BTUSB_DOWNLOADING);
Marcel Holtmannccd6da22015-04-09 00:35:19 -07002136}
2137
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002138static int btusb_recv_event_intel(struct hci_dev *hdev, struct sk_buff *skb)
2139{
2140 struct btusb_data *data = hci_get_drvdata(hdev);
2141
2142 if (test_bit(BTUSB_BOOTLOADER, &data->flags)) {
2143 struct hci_event_hdr *hdr = (void *)skb->data;
2144
Marcel Holtmannccd6da22015-04-09 00:35:19 -07002145 if (skb->len > HCI_EVENT_HDR_SIZE && hdr->evt == 0xff &&
2146 hdr->plen > 0) {
2147 const void *ptr = skb->data + HCI_EVENT_HDR_SIZE + 1;
2148 unsigned int len = skb->len - HCI_EVENT_HDR_SIZE - 1;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002149
Marcel Holtmannccd6da22015-04-09 00:35:19 -07002150 switch (skb->data[2]) {
2151 case 0x02:
2152 /* When switching to the operational firmware
2153 * the device sends a vendor specific event
2154 * indicating that the bootup completed.
2155 */
2156 btusb_intel_bootup(data, ptr, len);
2157 break;
2158 case 0x06:
2159 /* When the firmware loading completes the
2160 * device sends out a vendor specific event
2161 * indicating the result of the firmware
2162 * loading.
2163 */
2164 btusb_intel_secure_send_result(data, ptr, len);
2165 break;
Johan Hedbergfad70972015-01-30 10:58:55 +02002166 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002167 }
2168 }
2169
2170 return hci_recv_frame(hdev, skb);
2171}
2172
2173static int btusb_send_frame_intel(struct hci_dev *hdev, struct sk_buff *skb)
2174{
2175 struct btusb_data *data = hci_get_drvdata(hdev);
2176 struct urb *urb;
2177
2178 BT_DBG("%s", hdev->name);
2179
Marcel Holtmann618e8bc2015-11-05 07:33:56 +01002180 switch (hci_skb_pkt_type(skb)) {
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002181 case HCI_COMMAND_PKT:
2182 if (test_bit(BTUSB_BOOTLOADER, &data->flags)) {
2183 struct hci_command_hdr *cmd = (void *)skb->data;
2184 __u16 opcode = le16_to_cpu(cmd->opcode);
2185
2186 /* When in bootloader mode and the command 0xfc09
2187 * is received, it needs to be send down the
2188 * bulk endpoint. So allocate a bulk URB instead.
2189 */
2190 if (opcode == 0xfc09)
2191 urb = alloc_bulk_urb(hdev, skb);
2192 else
2193 urb = alloc_ctrl_urb(hdev, skb);
2194
2195 /* When the 0xfc01 command is issued to boot into
2196 * the operational firmware, it will actually not
2197 * send a command complete event. To keep the flow
2198 * control working inject that event here.
2199 */
2200 if (opcode == 0xfc01)
2201 inject_cmd_complete(hdev, opcode);
2202 } else {
2203 urb = alloc_ctrl_urb(hdev, skb);
2204 }
2205 if (IS_ERR(urb))
2206 return PTR_ERR(urb);
2207
2208 hdev->stat.cmd_tx++;
2209 return submit_or_queue_tx_urb(hdev, urb);
2210
2211 case HCI_ACLDATA_PKT:
2212 urb = alloc_bulk_urb(hdev, skb);
2213 if (IS_ERR(urb))
2214 return PTR_ERR(urb);
2215
2216 hdev->stat.acl_tx++;
2217 return submit_or_queue_tx_urb(hdev, urb);
2218
2219 case HCI_SCODATA_PKT:
2220 if (hci_conn_num(hdev, SCO_LINK) < 1)
2221 return -ENODEV;
2222
2223 urb = alloc_isoc_urb(hdev, skb);
2224 if (IS_ERR(urb))
2225 return PTR_ERR(urb);
2226
2227 hdev->stat.sco_tx++;
2228 return submit_tx_urb(hdev, urb);
2229 }
2230
2231 return -EILSEQ;
2232}
2233
Raghuram Hegde2da711bc2018-12-19 11:42:18 +05302234static bool btusb_setup_intel_new_get_fw_name(struct intel_version *ver,
2235 struct intel_boot_params *params,
2236 char *fw_name, size_t len,
2237 const char *suffix)
2238{
2239 switch (ver->hw_variant) {
2240 case 0x0b: /* SfP */
2241 case 0x0c: /* WsP */
2242 snprintf(fw_name, len, "intel/ibt-%u-%u.%s",
2243 le16_to_cpu(ver->hw_variant),
2244 le16_to_cpu(params->dev_revid),
2245 suffix);
2246 break;
2247 case 0x11: /* JfP */
2248 case 0x12: /* ThP */
2249 case 0x13: /* HrP */
2250 case 0x14: /* CcP */
2251 snprintf(fw_name, len, "intel/ibt-%u-%u-%u.%s",
2252 le16_to_cpu(ver->hw_variant),
2253 le16_to_cpu(ver->hw_revision),
2254 le16_to_cpu(ver->fw_revision),
2255 suffix);
2256 break;
2257 default:
2258 return false;
2259 }
2260 return true;
2261}
2262
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002263static int btusb_setup_intel_new(struct hci_dev *hdev)
2264{
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002265 struct btusb_data *data = hci_get_drvdata(hdev);
Loic Poulain6c483de2015-12-06 16:18:34 +01002266 struct intel_version ver;
Tedd Ho-Jeong Anfaf174d2018-01-24 09:19:20 -08002267 struct intel_boot_params params;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002268 const struct firmware *fw;
Tedd Ho-Jeong Ane5889af2018-01-24 09:19:18 -08002269 u32 boot_param;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002270 char fwname[64];
2271 ktime_t calltime, delta, rettime;
2272 unsigned long long duration;
2273 int err;
2274
2275 BT_DBG("%s", hdev->name);
2276
Tedd Ho-Jeong An04d729b2018-01-24 09:19:19 -08002277 /* Set the default boot parameter to 0x0 and it is updated to
2278 * SKU specific boot parameter after reading Intel_Write_Boot_Params
2279 * command while downloading the firmware.
2280 */
2281 boot_param = 0x00000000;
Tedd Ho-Jeong Ane5889af2018-01-24 09:19:18 -08002282
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002283 calltime = ktime_get();
2284
2285 /* Read the Intel version information to determine if the device
2286 * is in bootloader mode or if it already has operational firmware
2287 * loaded.
2288 */
Loic Poulain6c483de2015-12-06 16:18:34 +01002289 err = btintel_read_version(hdev, &ver);
Amit K Bagb9a25622019-10-17 13:52:29 +05302290 if (err) {
2291 bt_dev_err(hdev, "Intel Read version failed (%d)", err);
2292 btintel_reset_to_bootloader(hdev);
Loic Poulain6c483de2015-12-06 16:18:34 +01002293 return err;
Amit K Bagb9a25622019-10-17 13:52:29 +05302294 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002295
2296 /* The hardware platform number has a fixed value of 0x37 and
2297 * for now only accept this single value.
2298 */
Loic Poulain6c483de2015-12-06 16:18:34 +01002299 if (ver.hw_platform != 0x37) {
Marcel Holtmann85418fe2018-08-03 11:20:49 +02002300 bt_dev_err(hdev, "Unsupported Intel hardware platform (%u)",
2301 ver.hw_platform);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002302 return -EINVAL;
2303 }
2304
Tedd Ho-Jeong An92688342017-03-06 15:38:26 -08002305 /* Check for supported iBT hardware variants of this firmware
2306 * loading method.
Tedd Ho-Jeong Ana0af53b2016-05-06 11:53:46 -07002307 *
2308 * This check has been put in place to ensure correct forward
2309 * compatibility options when newer hardware variants come along.
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002310 */
Tedd Ho-Jeong An92688342017-03-06 15:38:26 -08002311 switch (ver.hw_variant) {
2312 case 0x0b: /* SfP */
2313 case 0x0c: /* WsP */
Tedd Ho-Jeong An86a61292017-05-01 13:35:12 -07002314 case 0x11: /* JfP */
Marcel Holtmannde766142017-03-06 15:38:28 -08002315 case 0x12: /* ThP */
Tedd Ho-Jeong An1ce0cec2018-02-05 14:20:36 -08002316 case 0x13: /* HrP */
Raghuram Hegde2da711bc2018-12-19 11:42:18 +05302317 case 0x14: /* CcP */
Tedd Ho-Jeong An92688342017-03-06 15:38:26 -08002318 break;
2319 default:
Marcel Holtmann85418fe2018-08-03 11:20:49 +02002320 bt_dev_err(hdev, "Unsupported Intel hardware variant (%u)",
2321 ver.hw_variant);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002322 return -EINVAL;
2323 }
2324
Loic Poulain6c483de2015-12-06 16:18:34 +01002325 btintel_version_info(hdev, &ver);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002326
2327 /* The firmware variant determines if the device is in bootloader
2328 * mode or is running operational firmware. The value 0x06 identifies
2329 * the bootloader and the value 0x23 identifies the operational
2330 * firmware.
2331 *
2332 * When the operational firmware is already present, then only
2333 * the check for valid Bluetooth device address is needed. This
2334 * determines if the device will be added as configured or
2335 * unconfigured controller.
2336 *
2337 * It is not possible to use the Secure Boot Parameters in this
2338 * case since that command is only available in bootloader mode.
2339 */
Loic Poulain6c483de2015-12-06 16:18:34 +01002340 if (ver.fw_variant == 0x23) {
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002341 clear_bit(BTUSB_BOOTLOADER, &data->flags);
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07002342 btintel_check_bdaddr(hdev);
Marcel Holtmann7fd673b2020-04-03 21:44:02 +02002343 goto finish;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002344 }
2345
2346 /* If the device is not in bootloader mode, then the only possible
2347 * choice is to return an error and abort the device initialization.
2348 */
Loic Poulain6c483de2015-12-06 16:18:34 +01002349 if (ver.fw_variant != 0x06) {
Marcel Holtmann85418fe2018-08-03 11:20:49 +02002350 bt_dev_err(hdev, "Unsupported Intel firmware variant (%u)",
2351 ver.fw_variant);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002352 return -ENODEV;
2353 }
2354
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002355 /* Read the secure boot parameters to identify the operating
2356 * details of the bootloader.
2357 */
Tedd Ho-Jeong Anfaf174d2018-01-24 09:19:20 -08002358 err = btintel_read_boot_params(hdev, &params);
2359 if (err)
2360 return err;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002361
2362 /* It is required that every single firmware fragment is acknowledged
2363 * with a command complete event. If the boot parameters indicate
2364 * that this bootloader does not send them, then abort the setup.
2365 */
Tedd Ho-Jeong Anfaf174d2018-01-24 09:19:20 -08002366 if (params.limited_cce != 0x00) {
Marcel Holtmann85418fe2018-08-03 11:20:49 +02002367 bt_dev_err(hdev, "Unsupported Intel firmware loading method (%u)",
2368 params.limited_cce);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002369 return -EINVAL;
2370 }
2371
2372 /* If the OTP has no valid Bluetooth device address, then there will
2373 * also be no valid address for the operational firmware.
2374 */
Tedd Ho-Jeong Anfaf174d2018-01-24 09:19:20 -08002375 if (!bacmp(&params.otp_bdaddr, BDADDR_ANY)) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002376 bt_dev_info(hdev, "No device address configured");
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002377 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
2378 }
2379
2380 /* With this Intel bootloader only the hardware variant and device
Jaya P Gaf3715e2017-10-30 11:01:22 +01002381 * revision information are used to select the right firmware for SfP
2382 * and WsP.
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002383 *
Tedd Ho-Jeong An230b04a2016-06-28 08:56:39 -07002384 * The firmware filename is ibt-<hw_variant>-<dev_revid>.sfi.
2385 *
2386 * Currently the supported hardware variants are:
2387 * 11 (0x0b) for iBT3.0 (LnP/SfP)
2388 * 12 (0x0c) for iBT3.5 (WsP)
Jaya P Gaf3715e2017-10-30 11:01:22 +01002389 *
2390 * For ThP/JfP and for future SKU's, the FW name varies based on HW
2391 * variant, HW revision and FW revision, as these are dependent on CNVi
2392 * and RF Combination.
2393 *
Tedd Ho-Jeong An86a61292017-05-01 13:35:12 -07002394 * 17 (0x11) for iBT3.5 (JfP)
2395 * 18 (0x12) for iBT3.5 (ThP)
Jaya P Gaf3715e2017-10-30 11:01:22 +01002396 *
2397 * The firmware file name for these will be
2398 * ibt-<hw_variant>-<hw_revision>-<fw_revision>.sfi.
2399 *
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002400 */
Raghuram Hegde2da711bc2018-12-19 11:42:18 +05302401 err = btusb_setup_intel_new_get_fw_name(&ver, &params, fwname,
2402 sizeof(fwname), "sfi");
2403 if (!err) {
Marcel Holtmann85418fe2018-08-03 11:20:49 +02002404 bt_dev_err(hdev, "Unsupported Intel firmware naming");
Jaya P Gaf3715e2017-10-30 11:01:22 +01002405 return -EINVAL;
2406 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002407
2408 err = request_firmware(&fw, fwname, &hdev->dev);
2409 if (err < 0) {
Marcel Holtmann85418fe2018-08-03 11:20:49 +02002410 bt_dev_err(hdev, "Failed to load Intel firmware file (%d)", err);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002411 return err;
2412 }
2413
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002414 bt_dev_info(hdev, "Found device firmware: %s", fwname);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002415
Tedd Ho-Jeong An52cc9162015-06-12 16:20:05 -07002416 /* Save the DDC file name for later use to apply once the firmware
2417 * downloading is done.
2418 */
Raghuram Hegde2da711bc2018-12-19 11:42:18 +05302419 err = btusb_setup_intel_new_get_fw_name(&ver, &params, fwname,
2420 sizeof(fwname), "ddc");
2421 if (!err) {
Marcel Holtmann85418fe2018-08-03 11:20:49 +02002422 bt_dev_err(hdev, "Unsupported Intel firmware naming");
Jaya P Gaf3715e2017-10-30 11:01:22 +01002423 return -EINVAL;
2424 }
Tedd Ho-Jeong An52cc9162015-06-12 16:20:05 -07002425
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002426 if (fw->size < 644) {
Marcel Holtmann85418fe2018-08-03 11:20:49 +02002427 bt_dev_err(hdev, "Invalid size of firmware file (%zu)",
2428 fw->size);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002429 err = -EBADF;
2430 goto done;
2431 }
2432
2433 set_bit(BTUSB_DOWNLOADING, &data->flags);
2434
Tedd Ho-Jeong Anfbbe83c2018-01-24 09:19:21 -08002435 /* Start firmware downloading and get boot parameter */
2436 err = btintel_download_firmware(hdev, fw, &boot_param);
Amit K Bagb9a25622019-10-17 13:52:29 +05302437 if (err < 0) {
2438 /* When FW download fails, send Intel Reset to retry
2439 * FW download.
2440 */
2441 btintel_reset_to_bootloader(hdev);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002442 goto done;
Amit K Bagb9a25622019-10-17 13:52:29 +05302443 }
Marcel Holtmannce6bb922015-01-28 01:58:40 -08002444 set_bit(BTUSB_FIRMWARE_LOADED, &data->flags);
2445
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002446 bt_dev_info(hdev, "Waiting for firmware download to complete");
Johan Hedberga087a982015-01-30 10:58:54 +02002447
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002448 /* Before switching the device into operational mode and with that
2449 * booting the loaded firmware, wait for the bootloader notification
2450 * that all fragments have been successfully received.
2451 *
Johan Hedberga087a982015-01-30 10:58:54 +02002452 * When the event processing receives the notification, then the
2453 * BTUSB_DOWNLOADING flag will be cleared.
2454 *
2455 * The firmware loading should not take longer than 5 seconds
2456 * and thus just timeout if that happens and fail the setup
2457 * of this device.
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002458 */
Johan Hedberg129a7692015-02-14 09:33:35 +02002459 err = wait_on_bit_timeout(&data->flags, BTUSB_DOWNLOADING,
2460 TASK_INTERRUPTIBLE,
2461 msecs_to_jiffies(5000));
Bart Van Asschef0a70a02016-08-11 16:02:44 -07002462 if (err == -EINTR) {
Marcel Holtmann85418fe2018-08-03 11:20:49 +02002463 bt_dev_err(hdev, "Firmware loading interrupted");
Johan Hedberga087a982015-01-30 10:58:54 +02002464 goto done;
2465 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002466
Johan Hedberga087a982015-01-30 10:58:54 +02002467 if (err) {
Marcel Holtmann85418fe2018-08-03 11:20:49 +02002468 bt_dev_err(hdev, "Firmware loading timeout");
Johan Hedberga087a982015-01-30 10:58:54 +02002469 err = -ETIMEDOUT;
Amit K Bagb9a25622019-10-17 13:52:29 +05302470 btintel_reset_to_bootloader(hdev);
Johan Hedberga087a982015-01-30 10:58:54 +02002471 goto done;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002472 }
2473
2474 if (test_bit(BTUSB_FIRMWARE_FAILED, &data->flags)) {
Marcel Holtmann85418fe2018-08-03 11:20:49 +02002475 bt_dev_err(hdev, "Firmware loading failed");
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002476 err = -ENOEXEC;
2477 goto done;
2478 }
2479
2480 rettime = ktime_get();
2481 delta = ktime_sub(rettime, calltime);
2482 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
2483
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002484 bt_dev_info(hdev, "Firmware loaded in %llu usecs", duration);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002485
2486done:
2487 release_firmware(fw);
2488
2489 if (err < 0)
2490 return err;
2491
2492 calltime = ktime_get();
2493
2494 set_bit(BTUSB_BOOTING, &data->flags);
2495
Tedd Ho-Jeong Ane5889af2018-01-24 09:19:18 -08002496 err = btintel_send_intel_reset(hdev, boot_param);
Amit K Bagb9a25622019-10-17 13:52:29 +05302497 if (err) {
2498 bt_dev_err(hdev, "Intel Soft Reset failed (%d)", err);
2499 btintel_reset_to_bootloader(hdev);
Tedd Ho-Jeong Ane5889af2018-01-24 09:19:18 -08002500 return err;
Amit K Bagb9a25622019-10-17 13:52:29 +05302501 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002502
2503 /* The bootloader will not indicate when the device is ready. This
2504 * is done by the operational firmware sending bootup notification.
Johan Hedbergfad70972015-01-30 10:58:55 +02002505 *
2506 * Booting into operational firmware should not take longer than
2507 * 1 second. However if that happens, then just fail the setup
2508 * since something went wrong.
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002509 */
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002510 bt_dev_info(hdev, "Waiting for device to boot");
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002511
Johan Hedberg129a7692015-02-14 09:33:35 +02002512 err = wait_on_bit_timeout(&data->flags, BTUSB_BOOTING,
2513 TASK_INTERRUPTIBLE,
2514 msecs_to_jiffies(1000));
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002515
Bart Van Asschef0a70a02016-08-11 16:02:44 -07002516 if (err == -EINTR) {
Marcel Holtmann85418fe2018-08-03 11:20:49 +02002517 bt_dev_err(hdev, "Device boot interrupted");
Johan Hedbergfad70972015-01-30 10:58:55 +02002518 return -EINTR;
2519 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002520
Johan Hedbergfad70972015-01-30 10:58:55 +02002521 if (err) {
Marcel Holtmann85418fe2018-08-03 11:20:49 +02002522 bt_dev_err(hdev, "Device boot timeout");
Amit K Bagb9a25622019-10-17 13:52:29 +05302523 btintel_reset_to_bootloader(hdev);
Johan Hedbergfad70972015-01-30 10:58:55 +02002524 return -ETIMEDOUT;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002525 }
2526
2527 rettime = ktime_get();
2528 delta = ktime_sub(rettime, calltime);
2529 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
2530
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002531 bt_dev_info(hdev, "Device booted in %llu usecs", duration);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002532
2533 clear_bit(BTUSB_BOOTLOADER, &data->flags);
2534
Tedd Ho-Jeong An52cc9162015-06-12 16:20:05 -07002535 /* Once the device is running in operational mode, it needs to apply
2536 * the device configuration (DDC) parameters.
2537 *
2538 * The device can work without DDC parameters, so even if it fails
2539 * to load the file, no need to fail the setup.
2540 */
Loic Poulaine924d3d2015-09-04 17:54:36 +02002541 btintel_load_ddc_config(hdev, fwname);
Tedd Ho-Jeong An52cc9162015-06-12 16:20:05 -07002542
Marcel Holtmann7fd673b2020-04-03 21:44:02 +02002543 /* Read the Intel version information after loading the FW */
2544 err = btintel_read_version(hdev, &ver);
2545 if (err)
2546 return err;
2547
2548 btintel_version_info(hdev, &ver);
2549
2550finish:
Miao-chen Choufc045902020-04-03 21:44:03 +02002551 /* All Intel controllers that support the Microsoft vendor
2552 * extension are using 0xFC1E for VsMsftOpCode.
2553 */
2554 switch (ver.hw_variant) {
2555 case 0x12: /* ThP */
2556 hci_set_msft_opcode(hdev, 0xFC1E);
2557 break;
2558 }
2559
Marcel Holtmann213445b2015-10-21 02:45:19 +02002560 /* Set the event mask for Intel specific vendor events. This enables
2561 * a few extra events that are useful during general operation. It
2562 * does not enable any debugging related events.
2563 *
2564 * The device will function correctly without these events enabled
2565 * and thus no need to fail the setup.
2566 */
2567 btintel_set_event_mask(hdev, false);
2568
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002569 return 0;
2570}
2571
Tedd Ho-Jeong Anbfbd45e2015-02-13 09:20:52 -08002572static int btusb_shutdown_intel(struct hci_dev *hdev)
2573{
2574 struct sk_buff *skb;
2575 long ret;
2576
Amit K Bag1313bcc2018-08-03 12:43:20 +05302577 /* In the shutdown sequence where Bluetooth is turned off followed
2578 * by WiFi being turned off, turning WiFi back on causes issue with
2579 * the RF calibration.
2580 *
2581 * To ensure that any RF activity has been stopped, issue HCI Reset
2582 * command to clear all ongoing activity including advertising,
2583 * scanning etc.
2584 */
2585 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
2586 if (IS_ERR(skb)) {
2587 ret = PTR_ERR(skb);
2588 bt_dev_err(hdev, "HCI reset during shutdown failed");
2589 return ret;
2590 }
2591 kfree_skb(skb);
2592
Tedd Ho-Jeong Anbfbd45e2015-02-13 09:20:52 -08002593 /* Some platforms have an issue with BT LED when the interface is
2594 * down or BT radio is turned off, which takes 5 seconds to BT LED
2595 * goes off. This command turns off the BT LED immediately.
2596 */
2597 skb = __hci_cmd_sync(hdev, 0xfc3f, 0, NULL, HCI_INIT_TIMEOUT);
2598 if (IS_ERR(skb)) {
2599 ret = PTR_ERR(skb);
Marcel Holtmann85418fe2018-08-03 11:20:49 +02002600 bt_dev_err(hdev, "turning off Intel device LED failed");
Tedd Ho-Jeong Anbfbd45e2015-02-13 09:20:52 -08002601 return ret;
2602 }
2603 kfree_skb(skb);
2604
2605 return 0;
2606}
2607
Raghuram Hegde017a01c2019-01-29 17:54:48 +05302608static int btusb_shutdown_intel_new(struct hci_dev *hdev)
2609{
2610 struct sk_buff *skb;
2611
2612 /* Send HCI Reset to the controller to stop any BT activity which
2613 * were triggered. This will help to save power and maintain the
2614 * sync b/w Host and controller
2615 */
2616 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
2617 if (IS_ERR(skb)) {
2618 bt_dev_err(hdev, "HCI reset during shutdown failed");
2619 return PTR_ERR(skb);
2620 }
2621 kfree_skb(skb);
2622
2623 return 0;
2624}
2625
Sean Wang9ce67c32019-06-02 08:02:49 +08002626#define FIRMWARE_MT7663 "mediatek/mt7663pr2h.bin"
Sean Wanga1c49c432019-06-02 08:02:48 +08002627#define FIRMWARE_MT7668 "mediatek/mt7668pr2h.bin"
2628
2629#define HCI_WMT_MAX_EVENT_SIZE 64
2630
2631enum {
2632 BTMTK_WMT_PATCH_DWNLD = 0x1,
2633 BTMTK_WMT_FUNC_CTRL = 0x6,
2634 BTMTK_WMT_RST = 0x7,
2635 BTMTK_WMT_SEMAPHORE = 0x17,
2636};
2637
2638enum {
2639 BTMTK_WMT_INVALID,
2640 BTMTK_WMT_PATCH_UNDONE,
2641 BTMTK_WMT_PATCH_DONE,
2642 BTMTK_WMT_ON_UNDONE,
2643 BTMTK_WMT_ON_DONE,
2644 BTMTK_WMT_ON_PROGRESS,
2645};
2646
2647struct btmtk_wmt_hdr {
2648 u8 dir;
2649 u8 op;
2650 __le16 dlen;
2651 u8 flag;
2652} __packed;
2653
2654struct btmtk_hci_wmt_cmd {
2655 struct btmtk_wmt_hdr hdr;
2656 u8 data[256];
2657} __packed;
2658
2659struct btmtk_hci_wmt_evt {
2660 struct hci_event_hdr hhdr;
2661 struct btmtk_wmt_hdr whdr;
2662} __packed;
2663
2664struct btmtk_hci_wmt_evt_funcc {
2665 struct btmtk_hci_wmt_evt hwhdr;
2666 __be16 status;
2667} __packed;
2668
2669struct btmtk_tci_sleep {
2670 u8 mode;
2671 __le16 duration;
2672 __le16 host_duration;
2673 u8 host_wakeup_pin;
2674 u8 time_compensation;
2675} __packed;
2676
2677struct btmtk_hci_wmt_params {
2678 u8 op;
2679 u8 flag;
2680 u16 dlen;
2681 const void *data;
2682 u32 *status;
2683};
2684
2685static void btusb_mtk_wmt_recv(struct urb *urb)
2686{
2687 struct hci_dev *hdev = urb->context;
2688 struct btusb_data *data = hci_get_drvdata(hdev);
2689 struct hci_event_hdr *hdr;
2690 struct sk_buff *skb;
2691 int err;
2692
2693 if (urb->status == 0 && urb->actual_length > 0) {
2694 hdev->stat.byte_rx += urb->actual_length;
2695
2696 /* WMT event shouldn't be fragmented and the size should be
2697 * less than HCI_WMT_MAX_EVENT_SIZE.
2698 */
2699 skb = bt_skb_alloc(HCI_WMT_MAX_EVENT_SIZE, GFP_ATOMIC);
2700 if (!skb) {
2701 hdev->stat.err_rx++;
2702 goto err_out;
2703 }
2704
2705 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
2706 skb_put_data(skb, urb->transfer_buffer, urb->actual_length);
2707
2708 hdr = (void *)skb->data;
2709 /* Fix up the vendor event id with 0xff for vendor specific
2710 * instead of 0xe4 so that event send via monitoring socket can
2711 * be parsed properly.
2712 */
2713 hdr->evt = 0xff;
2714
2715 /* When someone waits for the WMT event, the skb is being cloned
2716 * and being processed the events from there then.
2717 */
2718 if (test_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags)) {
Johan Hovold22cc6b72019-11-28 19:24:27 +01002719 data->evt_skb = skb_clone(skb, GFP_ATOMIC);
Sean Wanga1c49c432019-06-02 08:02:48 +08002720 if (!data->evt_skb)
2721 goto err_out;
2722 }
2723
2724 err = hci_recv_frame(hdev, skb);
2725 if (err < 0)
2726 goto err_free_skb;
2727
2728 if (test_and_clear_bit(BTUSB_TX_WAIT_VND_EVT,
2729 &data->flags)) {
2730 /* Barrier to sync with other CPUs */
2731 smp_mb__after_atomic();
2732 wake_up_bit(&data->flags,
2733 BTUSB_TX_WAIT_VND_EVT);
2734 }
2735err_out:
2736 return;
2737err_free_skb:
2738 kfree_skb(data->evt_skb);
2739 data->evt_skb = NULL;
2740 return;
2741 } else if (urb->status == -ENOENT) {
2742 /* Avoid suspend failed when usb_kill_urb */
2743 return;
2744 }
2745
2746 usb_mark_last_busy(data->udev);
2747
2748 /* The URB complete handler is still called with urb->actual_length = 0
2749 * when the event is not available, so we should keep re-submitting
2750 * URB until WMT event returns, Also, It's necessary to wait some time
2751 * between the two consecutive control URBs to relax the target device
2752 * to generate the event. Otherwise, the WMT event cannot return from
2753 * the device successfully.
2754 */
2755 udelay(100);
2756
2757 usb_anchor_urb(urb, &data->ctrl_anchor);
2758 err = usb_submit_urb(urb, GFP_ATOMIC);
2759 if (err < 0) {
2760 /* -EPERM: urb is being killed;
2761 * -ENODEV: device got disconnected
2762 */
2763 if (err != -EPERM && err != -ENODEV)
2764 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
2765 urb, -err);
2766 usb_unanchor_urb(urb);
2767 }
2768}
2769
2770static int btusb_mtk_submit_wmt_recv_urb(struct hci_dev *hdev)
2771{
2772 struct btusb_data *data = hci_get_drvdata(hdev);
2773 struct usb_ctrlrequest *dr;
2774 unsigned char *buf;
2775 int err, size = 64;
2776 unsigned int pipe;
2777 struct urb *urb;
2778
2779 urb = usb_alloc_urb(0, GFP_KERNEL);
2780 if (!urb)
2781 return -ENOMEM;
2782
2783 dr = kmalloc(sizeof(*dr), GFP_KERNEL);
2784 if (!dr) {
2785 usb_free_urb(urb);
2786 return -ENOMEM;
2787 }
2788
2789 dr->bRequestType = USB_TYPE_VENDOR | USB_DIR_IN;
2790 dr->bRequest = 1;
2791 dr->wIndex = cpu_to_le16(0);
2792 dr->wValue = cpu_to_le16(48);
2793 dr->wLength = cpu_to_le16(size);
2794
2795 buf = kmalloc(size, GFP_KERNEL);
2796 if (!buf) {
2797 kfree(dr);
2798 return -ENOMEM;
2799 }
2800
2801 pipe = usb_rcvctrlpipe(data->udev, 0);
2802
2803 usb_fill_control_urb(urb, data->udev, pipe, (void *)dr,
2804 buf, size, btusb_mtk_wmt_recv, hdev);
2805
2806 urb->transfer_flags |= URB_FREE_BUFFER;
2807
2808 usb_anchor_urb(urb, &data->ctrl_anchor);
2809 err = usb_submit_urb(urb, GFP_KERNEL);
2810 if (err < 0) {
2811 if (err != -EPERM && err != -ENODEV)
2812 bt_dev_err(hdev, "urb %p submission failed (%d)",
2813 urb, -err);
2814 usb_unanchor_urb(urb);
2815 }
2816
2817 usb_free_urb(urb);
2818
2819 return err;
2820}
2821
2822static int btusb_mtk_hci_wmt_sync(struct hci_dev *hdev,
2823 struct btmtk_hci_wmt_params *wmt_params)
2824{
2825 struct btusb_data *data = hci_get_drvdata(hdev);
2826 struct btmtk_hci_wmt_evt_funcc *wmt_evt_funcc;
2827 u32 hlen, status = BTMTK_WMT_INVALID;
2828 struct btmtk_hci_wmt_evt *wmt_evt;
2829 struct btmtk_hci_wmt_cmd wc;
2830 struct btmtk_wmt_hdr *hdr;
2831 int err;
2832
2833 /* Submit control IN URB on demand to process the WMT event */
2834 err = btusb_mtk_submit_wmt_recv_urb(hdev);
2835 if (err < 0)
2836 return err;
2837
2838 /* Send the WMT command and wait until the WMT event returns */
2839 hlen = sizeof(*hdr) + wmt_params->dlen;
2840 if (hlen > 255)
2841 return -EINVAL;
2842
2843 hdr = (struct btmtk_wmt_hdr *)&wc;
2844 hdr->dir = 1;
2845 hdr->op = wmt_params->op;
2846 hdr->dlen = cpu_to_le16(wmt_params->dlen + 1);
2847 hdr->flag = wmt_params->flag;
2848 memcpy(wc.data, wmt_params->data, wmt_params->dlen);
2849
2850 set_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
2851
2852 err = __hci_cmd_send(hdev, 0xfc6f, hlen, &wc);
2853
2854 if (err < 0) {
2855 clear_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
2856 return err;
2857 }
2858
2859 /* The vendor specific WMT commands are all answered by a vendor
2860 * specific event and will have the Command Status or Command
2861 * Complete as with usual HCI command flow control.
2862 *
2863 * After sending the command, wait for BTUSB_TX_WAIT_VND_EVT
2864 * state to be cleared. The driver specific event receive routine
2865 * will clear that state and with that indicate completion of the
2866 * WMT command.
2867 */
2868 err = wait_on_bit_timeout(&data->flags, BTUSB_TX_WAIT_VND_EVT,
2869 TASK_INTERRUPTIBLE, HCI_INIT_TIMEOUT);
2870 if (err == -EINTR) {
2871 bt_dev_err(hdev, "Execution of wmt command interrupted");
2872 clear_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
2873 return err;
2874 }
2875
2876 if (err) {
2877 bt_dev_err(hdev, "Execution of wmt command timed out");
2878 clear_bit(BTUSB_TX_WAIT_VND_EVT, &data->flags);
2879 return -ETIMEDOUT;
2880 }
2881
2882 /* Parse and handle the return WMT event */
2883 wmt_evt = (struct btmtk_hci_wmt_evt *)data->evt_skb->data;
2884 if (wmt_evt->whdr.op != hdr->op) {
2885 bt_dev_err(hdev, "Wrong op received %d expected %d",
2886 wmt_evt->whdr.op, hdr->op);
2887 err = -EIO;
2888 goto err_free_skb;
2889 }
2890
2891 switch (wmt_evt->whdr.op) {
2892 case BTMTK_WMT_SEMAPHORE:
2893 if (wmt_evt->whdr.flag == 2)
2894 status = BTMTK_WMT_PATCH_UNDONE;
2895 else
2896 status = BTMTK_WMT_PATCH_DONE;
2897 break;
2898 case BTMTK_WMT_FUNC_CTRL:
2899 wmt_evt_funcc = (struct btmtk_hci_wmt_evt_funcc *)wmt_evt;
2900 if (be16_to_cpu(wmt_evt_funcc->status) == 0x404)
2901 status = BTMTK_WMT_ON_DONE;
2902 else if (be16_to_cpu(wmt_evt_funcc->status) == 0x420)
2903 status = BTMTK_WMT_ON_PROGRESS;
2904 else
2905 status = BTMTK_WMT_ON_UNDONE;
2906 break;
2907 }
2908
2909 if (wmt_params->status)
2910 *wmt_params->status = status;
2911
2912err_free_skb:
2913 kfree_skb(data->evt_skb);
2914 data->evt_skb = NULL;
2915
2916 return err;
2917}
2918
2919static int btusb_mtk_setup_firmware(struct hci_dev *hdev, const char *fwname)
2920{
2921 struct btmtk_hci_wmt_params wmt_params;
2922 const struct firmware *fw;
2923 const u8 *fw_ptr;
2924 size_t fw_size;
2925 int err, dlen;
2926 u8 flag;
2927
2928 err = request_firmware(&fw, fwname, &hdev->dev);
2929 if (err < 0) {
2930 bt_dev_err(hdev, "Failed to load firmware file (%d)", err);
2931 return err;
2932 }
2933
2934 fw_ptr = fw->data;
2935 fw_size = fw->size;
2936
2937 /* The size of patch header is 30 bytes, should be skip */
Wei Yongjun5ee63102019-07-10 06:12:22 +00002938 if (fw_size < 30) {
2939 err = -EINVAL;
Sean Wanga1c49c432019-06-02 08:02:48 +08002940 goto err_release_fw;
Wei Yongjun5ee63102019-07-10 06:12:22 +00002941 }
Sean Wanga1c49c432019-06-02 08:02:48 +08002942
2943 fw_size -= 30;
2944 fw_ptr += 30;
2945 flag = 1;
2946
2947 wmt_params.op = BTMTK_WMT_PATCH_DWNLD;
2948 wmt_params.status = NULL;
2949
2950 while (fw_size > 0) {
2951 dlen = min_t(int, 250, fw_size);
2952
2953 /* Tell deivice the position in sequence */
2954 if (fw_size - dlen <= 0)
2955 flag = 3;
2956 else if (fw_size < fw->size - 30)
2957 flag = 2;
2958
2959 wmt_params.flag = flag;
2960 wmt_params.dlen = dlen;
2961 wmt_params.data = fw_ptr;
2962
2963 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
2964 if (err < 0) {
2965 bt_dev_err(hdev, "Failed to send wmt patch dwnld (%d)",
2966 err);
2967 goto err_release_fw;
2968 }
2969
2970 fw_size -= dlen;
2971 fw_ptr += dlen;
2972 }
2973
2974 wmt_params.op = BTMTK_WMT_RST;
2975 wmt_params.flag = 4;
2976 wmt_params.dlen = 0;
2977 wmt_params.data = NULL;
2978 wmt_params.status = NULL;
2979
2980 /* Activate funciton the firmware providing to */
2981 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
2982 if (err < 0) {
2983 bt_dev_err(hdev, "Failed to send wmt rst (%d)", err);
Colin Ian King3168c192019-11-29 17:36:35 +00002984 goto err_release_fw;
Sean Wanga1c49c432019-06-02 08:02:48 +08002985 }
2986
2987 /* Wait a few moments for firmware activation done */
2988 usleep_range(10000, 12000);
2989
2990err_release_fw:
2991 release_firmware(fw);
2992
2993 return err;
2994}
2995
2996static int btusb_mtk_func_query(struct hci_dev *hdev)
2997{
2998 struct btmtk_hci_wmt_params wmt_params;
2999 int status, err;
3000 u8 param = 0;
3001
3002 /* Query whether the function is enabled */
3003 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
3004 wmt_params.flag = 4;
3005 wmt_params.dlen = sizeof(param);
3006 wmt_params.data = &param;
3007 wmt_params.status = &status;
3008
3009 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3010 if (err < 0) {
3011 bt_dev_err(hdev, "Failed to query function status (%d)", err);
3012 return err;
3013 }
3014
3015 return status;
3016}
3017
3018static int btusb_mtk_reg_read(struct btusb_data *data, u32 reg, u32 *val)
3019{
3020 int pipe, err, size = sizeof(u32);
3021 void *buf;
3022
3023 buf = kzalloc(size, GFP_KERNEL);
3024 if (!buf)
3025 return -ENOMEM;
3026
3027 pipe = usb_rcvctrlpipe(data->udev, 0);
3028 err = usb_control_msg(data->udev, pipe, 0x63,
3029 USB_TYPE_VENDOR | USB_DIR_IN,
3030 reg >> 16, reg & 0xffff,
3031 buf, size, USB_CTRL_SET_TIMEOUT);
3032 if (err < 0)
3033 goto err_free_buf;
3034
3035 *val = get_unaligned_le32(buf);
3036
3037err_free_buf:
3038 kfree(buf);
3039
3040 return err;
3041}
3042
3043static int btusb_mtk_id_get(struct btusb_data *data, u32 *id)
3044{
3045 return btusb_mtk_reg_read(data, 0x80000008, id);
3046}
3047
3048static int btusb_mtk_setup(struct hci_dev *hdev)
3049{
3050 struct btusb_data *data = hci_get_drvdata(hdev);
3051 struct btmtk_hci_wmt_params wmt_params;
3052 ktime_t calltime, delta, rettime;
3053 struct btmtk_tci_sleep tci_sleep;
3054 unsigned long long duration;
3055 struct sk_buff *skb;
3056 const char *fwname;
3057 int err, status;
3058 u32 dev_id;
3059 u8 param;
3060
3061 calltime = ktime_get();
3062
3063 err = btusb_mtk_id_get(data, &dev_id);
3064 if (err < 0) {
3065 bt_dev_err(hdev, "Failed to get device id (%d)", err);
3066 return err;
3067 }
3068
3069 switch (dev_id) {
Sean Wang9ce67c32019-06-02 08:02:49 +08003070 case 0x7663:
3071 fwname = FIRMWARE_MT7663;
3072 break;
Sean Wanga1c49c432019-06-02 08:02:48 +08003073 case 0x7668:
3074 fwname = FIRMWARE_MT7668;
3075 break;
3076 default:
3077 bt_dev_err(hdev, "Unsupported support hardware variant (%08x)",
3078 dev_id);
3079 return -ENODEV;
3080 }
3081
3082 /* Query whether the firmware is already download */
3083 wmt_params.op = BTMTK_WMT_SEMAPHORE;
3084 wmt_params.flag = 1;
3085 wmt_params.dlen = 0;
3086 wmt_params.data = NULL;
3087 wmt_params.status = &status;
3088
3089 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3090 if (err < 0) {
3091 bt_dev_err(hdev, "Failed to query firmware status (%d)", err);
3092 return err;
3093 }
3094
3095 if (status == BTMTK_WMT_PATCH_DONE) {
3096 bt_dev_info(hdev, "firmware already downloaded");
3097 goto ignore_setup_fw;
3098 }
3099
3100 /* Setup a firmware which the device definitely requires */
3101 err = btusb_mtk_setup_firmware(hdev, fwname);
3102 if (err < 0)
3103 return err;
3104
3105ignore_setup_fw:
3106 err = readx_poll_timeout(btusb_mtk_func_query, hdev, status,
3107 status < 0 || status != BTMTK_WMT_ON_PROGRESS,
3108 2000, 5000000);
3109 /* -ETIMEDOUT happens */
3110 if (err < 0)
3111 return err;
3112
3113 /* The other errors happen in btusb_mtk_func_query */
3114 if (status < 0)
3115 return status;
3116
3117 if (status == BTMTK_WMT_ON_DONE) {
3118 bt_dev_info(hdev, "function already on");
3119 goto ignore_func_on;
3120 }
3121
3122 /* Enable Bluetooth protocol */
3123 param = 1;
3124 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
3125 wmt_params.flag = 0;
3126 wmt_params.dlen = sizeof(param);
3127 wmt_params.data = &param;
3128 wmt_params.status = NULL;
3129
3130 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3131 if (err < 0) {
3132 bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
3133 return err;
3134 }
3135
3136ignore_func_on:
3137 /* Apply the low power environment setup */
3138 tci_sleep.mode = 0x5;
3139 tci_sleep.duration = cpu_to_le16(0x640);
3140 tci_sleep.host_duration = cpu_to_le16(0x640);
3141 tci_sleep.host_wakeup_pin = 0;
3142 tci_sleep.time_compensation = 0;
3143
3144 skb = __hci_cmd_sync(hdev, 0xfc7a, sizeof(tci_sleep), &tci_sleep,
3145 HCI_INIT_TIMEOUT);
3146 if (IS_ERR(skb)) {
3147 err = PTR_ERR(skb);
3148 bt_dev_err(hdev, "Failed to apply low power setting (%d)", err);
3149 return err;
3150 }
3151 kfree_skb(skb);
3152
3153 rettime = ktime_get();
3154 delta = ktime_sub(rettime, calltime);
3155 duration = (unsigned long long)ktime_to_ns(delta) >> 10;
3156
3157 bt_dev_info(hdev, "Device setup in %llu usecs", duration);
3158
3159 return 0;
3160}
3161
3162static int btusb_mtk_shutdown(struct hci_dev *hdev)
3163{
3164 struct btmtk_hci_wmt_params wmt_params;
3165 u8 param = 0;
3166 int err;
3167
3168 /* Disable the device */
3169 wmt_params.op = BTMTK_WMT_FUNC_CTRL;
3170 wmt_params.flag = 0;
3171 wmt_params.dlen = sizeof(param);
3172 wmt_params.data = &param;
3173 wmt_params.status = NULL;
3174
3175 err = btusb_mtk_hci_wmt_sync(hdev, &wmt_params);
3176 if (err < 0) {
3177 bt_dev_err(hdev, "Failed to send wmt func ctrl (%d)", err);
3178 return err;
3179 }
3180
3181 return 0;
3182}
3183
Sean Wang9ce67c32019-06-02 08:02:49 +08003184MODULE_FIRMWARE(FIRMWARE_MT7663);
Sean Wanga1c49c432019-06-02 08:02:48 +08003185MODULE_FIRMWARE(FIRMWARE_MT7668);
Sean Wanga1c49c432019-06-02 08:02:48 +08003186
Rajat Jaina4ccc9e2017-02-01 14:24:10 -08003187#ifdef CONFIG_PM
3188/* Configure an out-of-band gpio as wake-up pin, if specified in device tree */
3189static int marvell_config_oob_wake(struct hci_dev *hdev)
3190{
3191 struct sk_buff *skb;
3192 struct btusb_data *data = hci_get_drvdata(hdev);
3193 struct device *dev = &data->udev->dev;
3194 u16 pin, gap, opcode;
3195 int ret;
3196 u8 cmd[5];
3197
3198 /* Move on if no wakeup pin specified */
3199 if (of_property_read_u16(dev->of_node, "marvell,wakeup-pin", &pin) ||
3200 of_property_read_u16(dev->of_node, "marvell,wakeup-gap-ms", &gap))
3201 return 0;
3202
3203 /* Vendor specific command to configure a GPIO as wake-up pin */
3204 opcode = hci_opcode_pack(0x3F, 0x59);
3205 cmd[0] = opcode & 0xFF;
3206 cmd[1] = opcode >> 8;
3207 cmd[2] = 2; /* length of parameters that follow */
3208 cmd[3] = pin;
3209 cmd[4] = gap; /* time in ms, for which wakeup pin should be asserted */
3210
3211 skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL);
3212 if (!skb) {
3213 bt_dev_err(hdev, "%s: No memory\n", __func__);
3214 return -ENOMEM;
3215 }
3216
Johannes Berg59ae1d12017-06-16 14:29:20 +02003217 skb_put_data(skb, cmd, sizeof(cmd));
Rajat Jaina4ccc9e2017-02-01 14:24:10 -08003218 hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
3219
3220 ret = btusb_send_frame(hdev, skb);
3221 if (ret) {
3222 bt_dev_err(hdev, "%s: configuration failed\n", __func__);
3223 kfree_skb(skb);
3224 return ret;
3225 }
3226
3227 return 0;
3228}
3229#endif
3230
Amitkumar Karwarae8df492014-07-18 14:47:06 -07003231static int btusb_set_bdaddr_marvell(struct hci_dev *hdev,
3232 const bdaddr_t *bdaddr)
3233{
3234 struct sk_buff *skb;
3235 u8 buf[8];
3236 long ret;
3237
3238 buf[0] = 0xfe;
3239 buf[1] = sizeof(bdaddr_t);
3240 memcpy(buf + 2, bdaddr, sizeof(bdaddr_t));
3241
3242 skb = __hci_cmd_sync(hdev, 0xfc22, sizeof(buf), buf, HCI_INIT_TIMEOUT);
3243 if (IS_ERR(skb)) {
3244 ret = PTR_ERR(skb);
Marcel Holtmann2064ee32017-10-30 10:42:59 +01003245 bt_dev_err(hdev, "changing Marvell device address failed (%ld)",
3246 ret);
Amitkumar Karwarae8df492014-07-18 14:47:06 -07003247 return ret;
3248 }
3249 kfree_skb(skb);
3250
3251 return 0;
3252}
3253
Toshi Kikuchi58592232014-12-12 10:58:05 -08003254static int btusb_set_bdaddr_ath3012(struct hci_dev *hdev,
3255 const bdaddr_t *bdaddr)
3256{
3257 struct sk_buff *skb;
3258 u8 buf[10];
3259 long ret;
3260
3261 buf[0] = 0x01;
3262 buf[1] = 0x01;
3263 buf[2] = 0x00;
3264 buf[3] = sizeof(bdaddr_t);
3265 memcpy(buf + 4, bdaddr, sizeof(bdaddr_t));
3266
3267 skb = __hci_cmd_sync(hdev, 0xfc0b, sizeof(buf), buf, HCI_INIT_TIMEOUT);
3268 if (IS_ERR(skb)) {
3269 ret = PTR_ERR(skb);
Marcel Holtmann2064ee32017-10-30 10:42:59 +01003270 bt_dev_err(hdev, "Change address command failed (%ld)", ret);
Toshi Kikuchi58592232014-12-12 10:58:05 -08003271 return ret;
3272 }
3273 kfree_skb(skb);
3274
3275 return 0;
3276}
3277
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003278#define QCA_DFU_PACKET_LEN 4096
3279
3280#define QCA_GET_TARGET_VERSION 0x09
3281#define QCA_CHECK_STATUS 0x05
3282#define QCA_DFU_DOWNLOAD 0x01
3283
3284#define QCA_SYSCFG_UPDATED 0x40
3285#define QCA_PATCH_UPDATED 0x80
3286#define QCA_DFU_TIMEOUT 3000
3287
3288struct qca_version {
3289 __le32 rom_version;
3290 __le32 patch_version;
3291 __le32 ram_version;
3292 __le32 ref_clock;
3293 __u8 reserved[4];
3294} __packed;
3295
3296struct qca_rampatch_version {
3297 __le16 rom_version;
3298 __le16 patch_version;
3299} __packed;
3300
3301struct qca_device_info {
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00003302 u32 rom_version;
3303 u8 rampatch_hdr; /* length of header in rampatch */
3304 u8 nvm_hdr; /* length of header in NVM */
3305 u8 ver_offset; /* offset of version structure in rampatch */
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003306};
3307
3308static const struct qca_device_info qca_devices_table[] = {
3309 { 0x00000100, 20, 4, 10 }, /* Rome 1.0 */
3310 { 0x00000101, 20, 4, 10 }, /* Rome 1.1 */
Chan-yeol Park7f6e6362015-05-21 11:24:27 +09003311 { 0x00000200, 28, 4, 18 }, /* Rome 2.0 */
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003312 { 0x00000201, 28, 4, 18 }, /* Rome 2.1 */
3313 { 0x00000300, 28, 4, 18 }, /* Rome 3.0 */
3314 { 0x00000302, 28, 4, 18 }, /* Rome 3.2 */
3315};
3316
Takashi Iwai803cdb82018-05-21 22:34:52 +02003317static int btusb_qca_send_vendor_req(struct usb_device *udev, u8 request,
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003318 void *data, u16 size)
3319{
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003320 int pipe, err;
3321 u8 *buf;
3322
3323 buf = kmalloc(size, GFP_KERNEL);
3324 if (!buf)
3325 return -ENOMEM;
3326
3327 /* Found some of USB hosts have IOT issues with ours so that we should
3328 * not wait until HCI layer is ready.
3329 */
3330 pipe = usb_rcvctrlpipe(udev, 0);
3331 err = usb_control_msg(udev, pipe, request, USB_TYPE_VENDOR | USB_DIR_IN,
3332 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
3333 if (err < 0) {
Takashi Iwai803cdb82018-05-21 22:34:52 +02003334 dev_err(&udev->dev, "Failed to access otp area (%d)", err);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003335 goto done;
3336 }
3337
3338 memcpy(data, buf, size);
3339
3340done:
3341 kfree(buf);
3342
3343 return err;
3344}
3345
3346static int btusb_setup_qca_download_fw(struct hci_dev *hdev,
3347 const struct firmware *firmware,
3348 size_t hdr_size)
3349{
3350 struct btusb_data *btdata = hci_get_drvdata(hdev);
3351 struct usb_device *udev = btdata->udev;
3352 size_t count, size, sent = 0;
3353 int pipe, len, err;
3354 u8 *buf;
3355
3356 buf = kmalloc(QCA_DFU_PACKET_LEN, GFP_KERNEL);
3357 if (!buf)
3358 return -ENOMEM;
3359
3360 count = firmware->size;
3361
3362 size = min_t(size_t, count, hdr_size);
3363 memcpy(buf, firmware->data, size);
3364
3365 /* USB patches should go down to controller through USB path
3366 * because binary format fits to go down through USB channel.
3367 * USB control path is for patching headers and USB bulk is for
3368 * patch body.
3369 */
3370 pipe = usb_sndctrlpipe(udev, 0);
3371 err = usb_control_msg(udev, pipe, QCA_DFU_DOWNLOAD, USB_TYPE_VENDOR,
3372 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
3373 if (err < 0) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01003374 bt_dev_err(hdev, "Failed to send headers (%d)", err);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003375 goto done;
3376 }
3377
3378 sent += size;
3379 count -= size;
3380
3381 while (count) {
3382 size = min_t(size_t, count, QCA_DFU_PACKET_LEN);
3383
3384 memcpy(buf, firmware->data + sent, size);
3385
3386 pipe = usb_sndbulkpipe(udev, 0x02);
3387 err = usb_bulk_msg(udev, pipe, buf, size, &len,
3388 QCA_DFU_TIMEOUT);
3389 if (err < 0) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01003390 bt_dev_err(hdev, "Failed to send body at %zd of %zd (%d)",
3391 sent, firmware->size, err);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003392 break;
3393 }
3394
3395 if (size != len) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01003396 bt_dev_err(hdev, "Failed to get bulk buffer");
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003397 err = -EILSEQ;
3398 break;
3399 }
3400
3401 sent += size;
3402 count -= size;
3403 }
3404
3405done:
3406 kfree(buf);
3407 return err;
3408}
3409
3410static int btusb_setup_qca_load_rampatch(struct hci_dev *hdev,
3411 struct qca_version *ver,
3412 const struct qca_device_info *info)
3413{
3414 struct qca_rampatch_version *rver;
3415 const struct firmware *fw;
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00003416 u32 ver_rom, ver_patch;
3417 u16 rver_rom, rver_patch;
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003418 char fwname[64];
3419 int err;
3420
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00003421 ver_rom = le32_to_cpu(ver->rom_version);
3422 ver_patch = le32_to_cpu(ver->patch_version);
3423
3424 snprintf(fwname, sizeof(fwname), "qca/rampatch_usb_%08x.bin", ver_rom);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003425
3426 err = request_firmware(&fw, fwname, &hdev->dev);
3427 if (err) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01003428 bt_dev_err(hdev, "failed to request rampatch file: %s (%d)",
3429 fwname, err);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003430 return err;
3431 }
3432
Marcel Holtmann2064ee32017-10-30 10:42:59 +01003433 bt_dev_info(hdev, "using rampatch file: %s", fwname);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003434
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00003435 rver = (struct qca_rampatch_version *)(fw->data + info->ver_offset);
3436 rver_rom = le16_to_cpu(rver->rom_version);
3437 rver_patch = le16_to_cpu(rver->patch_version);
3438
Marcel Holtmann2064ee32017-10-30 10:42:59 +01003439 bt_dev_info(hdev, "QCA: patch rome 0x%x build 0x%x, "
3440 "firmware rome 0x%x build 0x%x",
3441 rver_rom, rver_patch, ver_rom, ver_patch);
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00003442
3443 if (rver_rom != ver_rom || rver_patch <= ver_patch) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01003444 bt_dev_err(hdev, "rampatch file version did not match with firmware");
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003445 err = -EINVAL;
3446 goto done;
3447 }
3448
3449 err = btusb_setup_qca_download_fw(hdev, fw, info->rampatch_hdr);
3450
3451done:
3452 release_firmware(fw);
3453
3454 return err;
3455}
3456
3457static int btusb_setup_qca_load_nvm(struct hci_dev *hdev,
3458 struct qca_version *ver,
3459 const struct qca_device_info *info)
3460{
3461 const struct firmware *fw;
3462 char fwname[64];
3463 int err;
3464
3465 snprintf(fwname, sizeof(fwname), "qca/nvm_usb_%08x.bin",
3466 le32_to_cpu(ver->rom_version));
3467
3468 err = request_firmware(&fw, fwname, &hdev->dev);
3469 if (err) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01003470 bt_dev_err(hdev, "failed to request NVM file: %s (%d)",
3471 fwname, err);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003472 return err;
3473 }
3474
Marcel Holtmann2064ee32017-10-30 10:42:59 +01003475 bt_dev_info(hdev, "using NVM file: %s", fwname);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003476
3477 err = btusb_setup_qca_download_fw(hdev, fw, info->nvm_hdr);
3478
3479 release_firmware(fw);
3480
3481 return err;
3482}
3483
Takashi Iwai803cdb82018-05-21 22:34:52 +02003484/* identify the ROM version and check whether patches are needed */
3485static bool btusb_qca_need_patch(struct usb_device *udev)
3486{
3487 struct qca_version ver;
3488
3489 if (btusb_qca_send_vendor_req(udev, QCA_GET_TARGET_VERSION, &ver,
3490 sizeof(ver)) < 0)
3491 return false;
3492 /* only low ROM versions need patches */
3493 return !(le32_to_cpu(ver.rom_version) & ~0xffffU);
3494}
3495
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003496static int btusb_setup_qca(struct hci_dev *hdev)
3497{
Takashi Iwai803cdb82018-05-21 22:34:52 +02003498 struct btusb_data *btdata = hci_get_drvdata(hdev);
3499 struct usb_device *udev = btdata->udev;
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003500 const struct qca_device_info *info = NULL;
3501 struct qca_version ver;
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00003502 u32 ver_rom;
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003503 u8 status;
3504 int i, err;
3505
Takashi Iwai803cdb82018-05-21 22:34:52 +02003506 err = btusb_qca_send_vendor_req(udev, QCA_GET_TARGET_VERSION, &ver,
Marcel Holtmanneb500422015-04-16 23:15:50 +02003507 sizeof(ver));
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003508 if (err < 0)
3509 return err;
3510
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00003511 ver_rom = le32_to_cpu(ver.rom_version);
Takashi Iwai803cdb82018-05-21 22:34:52 +02003512 /* Don't care about high ROM versions */
3513 if (ver_rom & ~0xffffU)
3514 return 0;
3515
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003516 for (i = 0; i < ARRAY_SIZE(qca_devices_table); i++) {
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00003517 if (ver_rom == qca_devices_table[i].rom_version)
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003518 info = &qca_devices_table[i];
3519 }
3520 if (!info) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01003521 bt_dev_err(hdev, "don't support firmware rome 0x%x", ver_rom);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003522 return -ENODEV;
3523 }
3524
Takashi Iwai803cdb82018-05-21 22:34:52 +02003525 err = btusb_qca_send_vendor_req(udev, QCA_CHECK_STATUS, &status,
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003526 sizeof(status));
3527 if (err < 0)
3528 return err;
3529
3530 if (!(status & QCA_PATCH_UPDATED)) {
3531 err = btusb_setup_qca_load_rampatch(hdev, &ver, info);
3532 if (err < 0)
3533 return err;
3534 }
3535
3536 if (!(status & QCA_SYSCFG_UPDATED)) {
3537 err = btusb_setup_qca_load_nvm(hdev, &ver, info);
3538 if (err < 0)
3539 return err;
3540 }
3541
3542 return 0;
3543}
3544
Marcel Holtmann9d08f502015-10-08 20:23:08 +02003545static inline int __set_diag_interface(struct hci_dev *hdev)
3546{
3547 struct btusb_data *data = hci_get_drvdata(hdev);
3548 struct usb_interface *intf = data->diag;
3549 int i;
3550
3551 if (!data->diag)
3552 return -ENODEV;
3553
3554 data->diag_tx_ep = NULL;
3555 data->diag_rx_ep = NULL;
3556
3557 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
3558 struct usb_endpoint_descriptor *ep_desc;
3559
3560 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
3561
3562 if (!data->diag_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
3563 data->diag_tx_ep = ep_desc;
3564 continue;
3565 }
3566
3567 if (!data->diag_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
3568 data->diag_rx_ep = ep_desc;
3569 continue;
3570 }
3571 }
3572
3573 if (!data->diag_tx_ep || !data->diag_rx_ep) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01003574 bt_dev_err(hdev, "invalid diagnostic descriptors");
Marcel Holtmann9d08f502015-10-08 20:23:08 +02003575 return -ENODEV;
3576 }
3577
3578 return 0;
3579}
3580
3581static struct urb *alloc_diag_urb(struct hci_dev *hdev, bool enable)
3582{
3583 struct btusb_data *data = hci_get_drvdata(hdev);
3584 struct sk_buff *skb;
3585 struct urb *urb;
3586 unsigned int pipe;
3587
3588 if (!data->diag_tx_ep)
3589 return ERR_PTR(-ENODEV);
3590
3591 urb = usb_alloc_urb(0, GFP_KERNEL);
3592 if (!urb)
3593 return ERR_PTR(-ENOMEM);
3594
3595 skb = bt_skb_alloc(2, GFP_KERNEL);
3596 if (!skb) {
3597 usb_free_urb(urb);
3598 return ERR_PTR(-ENOMEM);
3599 }
3600
Johannes Berg634fef62017-06-16 14:29:24 +02003601 skb_put_u8(skb, 0xf0);
3602 skb_put_u8(skb, enable);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02003603
3604 pipe = usb_sndbulkpipe(data->udev, data->diag_tx_ep->bEndpointAddress);
3605
3606 usb_fill_bulk_urb(urb, data->udev, pipe,
3607 skb->data, skb->len, btusb_tx_complete, skb);
3608
3609 skb->dev = (void *)hdev;
3610
3611 return urb;
3612}
3613
3614static int btusb_bcm_set_diag(struct hci_dev *hdev, bool enable)
3615{
3616 struct btusb_data *data = hci_get_drvdata(hdev);
3617 struct urb *urb;
3618
3619 if (!data->diag)
3620 return -ENODEV;
3621
3622 if (!test_bit(HCI_RUNNING, &hdev->flags))
3623 return -ENETDOWN;
3624
3625 urb = alloc_diag_urb(hdev, enable);
3626 if (IS_ERR(urb))
3627 return PTR_ERR(urb);
3628
3629 return submit_or_queue_tx_urb(hdev, urb);
3630}
Marcel Holtmann9d08f502015-10-08 20:23:08 +02003631
Rajat Jainfd913ef2017-02-01 14:24:09 -08003632#ifdef CONFIG_PM
3633static irqreturn_t btusb_oob_wake_handler(int irq, void *priv)
3634{
3635 struct btusb_data *data = priv;
3636
3637 pm_wakeup_event(&data->udev->dev, 0);
Jeffy Chen017789f2017-02-24 14:24:29 +08003638 pm_system_wakeup();
Rajat Jainfd913ef2017-02-01 14:24:09 -08003639
3640 /* Disable only if not already disabled (keep it balanced) */
3641 if (test_and_clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags)) {
3642 disable_irq_nosync(irq);
3643 disable_irq_wake(irq);
3644 }
3645 return IRQ_HANDLED;
3646}
3647
3648static const struct of_device_id btusb_match_table[] = {
3649 { .compatible = "usb1286,204e" },
Brian Norris4c409af2019-02-22 14:53:43 -08003650 { .compatible = "usbcf3,e300" }, /* QCA6174A */
3651 { .compatible = "usb4ca,301a" }, /* QCA6174A (Lite-On) */
Rajat Jainfd913ef2017-02-01 14:24:09 -08003652 { }
3653};
3654MODULE_DEVICE_TABLE(of, btusb_match_table);
3655
3656/* Use an oob wakeup pin? */
3657static int btusb_config_oob_wake(struct hci_dev *hdev)
3658{
3659 struct btusb_data *data = hci_get_drvdata(hdev);
3660 struct device *dev = &data->udev->dev;
3661 int irq, ret;
3662
3663 clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags);
3664
3665 if (!of_match_device(btusb_match_table, dev))
3666 return 0;
3667
3668 /* Move on if no IRQ specified */
3669 irq = of_irq_get_byname(dev->of_node, "wakeup");
3670 if (irq <= 0) {
3671 bt_dev_dbg(hdev, "%s: no OOB Wakeup IRQ in DT", __func__);
3672 return 0;
3673 }
3674
Brian Norris771acc72019-04-09 11:49:17 -07003675 irq_set_status_flags(irq, IRQ_NOAUTOEN);
Rajat Jainfd913ef2017-02-01 14:24:09 -08003676 ret = devm_request_irq(&hdev->dev, irq, btusb_oob_wake_handler,
3677 0, "OOB Wake-on-BT", data);
3678 if (ret) {
3679 bt_dev_err(hdev, "%s: IRQ request failed", __func__);
3680 return ret;
3681 }
3682
3683 ret = device_init_wakeup(dev, true);
3684 if (ret) {
3685 bt_dev_err(hdev, "%s: failed to init_wakeup", __func__);
3686 return ret;
3687 }
3688
3689 data->oob_wake_irq = irq;
Rajat Jainfd913ef2017-02-01 14:24:09 -08003690 bt_dev_info(hdev, "OOB Wake-on-BT configured at IRQ %u", irq);
3691 return 0;
3692}
3693#endif
3694
Hans de Goedefc549102018-04-27 11:26:43 +02003695static void btusb_check_needs_reset_resume(struct usb_interface *intf)
3696{
3697 if (dmi_check_system(btusb_needs_reset_resume_table))
3698 interface_to_usbdev(intf)->quirks |= USB_QUIRK_RESET_RESUME;
3699}
3700
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003701static int btusb_probe(struct usb_interface *intf,
Marcel Holtmann89e75332014-09-16 04:44:50 +02003702 const struct usb_device_id *id)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003703{
3704 struct usb_endpoint_descriptor *ep_desc;
Rajat Jaindc786b22019-01-24 15:28:14 -08003705 struct gpio_desc *reset_gpio;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003706 struct btusb_data *data;
3707 struct hci_dev *hdev;
Marcel Holtmann22f8e9d2015-10-20 00:53:33 +02003708 unsigned ifnum_base;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003709 int i, err;
3710
3711 BT_DBG("intf %p id %p", intf, id);
3712
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02003713 /* interface numbers are hardcoded in the spec */
Marcel Holtmann22f8e9d2015-10-20 00:53:33 +02003714 if (intf->cur_altsetting->desc.bInterfaceNumber != 0) {
3715 if (!(id->driver_info & BTUSB_IFNUM_2))
3716 return -ENODEV;
3717 if (intf->cur_altsetting->desc.bInterfaceNumber != 2)
3718 return -ENODEV;
3719 }
3720
3721 ifnum_base = intf->cur_altsetting->desc.bInterfaceNumber;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003722
3723 if (!id->driver_info) {
3724 const struct usb_device_id *match;
Marcel Holtmann89e75332014-09-16 04:44:50 +02003725
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003726 match = usb_match_id(intf, blacklist_table);
3727 if (match)
3728 id = match;
3729 }
3730
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02003731 if (id->driver_info == BTUSB_IGNORE)
3732 return -ENODEV;
3733
Steven.Li2d25f8b2011-07-01 14:02:36 +08003734 if (id->driver_info & BTUSB_ATH3012) {
3735 struct usb_device *udev = interface_to_usbdev(intf);
3736
3737 /* Old firmware would otherwise let ath3k driver load
Derek Robsond98422c2017-07-22 13:47:07 +12003738 * patch and sysconfig files
3739 */
Takashi Iwai803cdb82018-05-21 22:34:52 +02003740 if (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x0001 &&
3741 !btusb_qca_need_patch(udev))
Steven.Li2d25f8b2011-07-01 14:02:36 +08003742 return -ENODEV;
3743 }
3744
Sachin Kamat98921db2012-07-27 12:38:39 +05303745 data = devm_kzalloc(&intf->dev, sizeof(*data), GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003746 if (!data)
3747 return -ENOMEM;
3748
3749 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
3750 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
3751
3752 if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
3753 data->intr_ep = ep_desc;
3754 continue;
3755 }
3756
3757 if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
3758 data->bulk_tx_ep = ep_desc;
3759 continue;
3760 }
3761
3762 if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
3763 data->bulk_rx_ep = ep_desc;
3764 continue;
3765 }
3766 }
3767
Sachin Kamat98921db2012-07-27 12:38:39 +05303768 if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003769 return -ENODEV;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003770
Marcel Holtmann893ba542015-01-28 20:27:34 -08003771 if (id->driver_info & BTUSB_AMP) {
3772 data->cmdreq_type = USB_TYPE_CLASS | 0x01;
3773 data->cmdreq = 0x2b;
3774 } else {
3775 data->cmdreq_type = USB_TYPE_CLASS;
3776 data->cmdreq = 0x00;
3777 }
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01003778
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003779 data->udev = interface_to_usbdev(intf);
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02003780 data->intf = intf;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003781
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003782 INIT_WORK(&data->work, btusb_work);
Oliver Neukum7bee5492009-08-24 23:44:59 +02003783 INIT_WORK(&data->waker, btusb_waker);
Marcel Holtmann803b5832014-09-16 08:00:29 +02003784 init_usb_anchor(&data->deferred);
3785 init_usb_anchor(&data->tx_anchor);
Oliver Neukum7bee5492009-08-24 23:44:59 +02003786 spin_lock_init(&data->txlock);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003787
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003788 init_usb_anchor(&data->intr_anchor);
3789 init_usb_anchor(&data->bulk_anchor);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02003790 init_usb_anchor(&data->isoc_anchor);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02003791 init_usb_anchor(&data->diag_anchor);
Sean Wanga1c49c432019-06-02 08:02:48 +08003792 init_usb_anchor(&data->ctrl_anchor);
Marcel Holtmann803b5832014-09-16 08:00:29 +02003793 spin_lock_init(&data->rxlock);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003794
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08003795 if (id->driver_info & BTUSB_INTEL_NEW) {
3796 data->recv_event = btusb_recv_event_intel;
3797 data->recv_bulk = btusb_recv_bulk_intel;
3798 set_bit(BTUSB_BOOTLOADER, &data->flags);
3799 } else {
3800 data->recv_event = hci_recv_frame;
3801 data->recv_bulk = btusb_recv_bulk;
3802 }
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +01003803
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003804 hdev = hci_alloc_dev();
Sachin Kamat98921db2012-07-27 12:38:39 +05303805 if (!hdev)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003806 return -ENOMEM;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003807
Marcel Holtmannc13854c2010-02-08 15:27:07 +01003808 hdev->bus = HCI_USB;
David Herrmann155961e2012-02-09 21:58:32 +01003809 hci_set_drvdata(hdev, data);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003810
Marcel Holtmann893ba542015-01-28 20:27:34 -08003811 if (id->driver_info & BTUSB_AMP)
3812 hdev->dev_type = HCI_AMP;
3813 else
Marcel Holtmannca8bee52016-07-05 14:30:14 +02003814 hdev->dev_type = HCI_PRIMARY;
Marcel Holtmann893ba542015-01-28 20:27:34 -08003815
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003816 data->hdev = hdev;
3817
3818 SET_HCIDEV_DEV(hdev, &intf->dev);
3819
Rajat Jaindc786b22019-01-24 15:28:14 -08003820 reset_gpio = gpiod_get_optional(&data->udev->dev, "reset",
3821 GPIOD_OUT_LOW);
3822 if (IS_ERR(reset_gpio)) {
3823 err = PTR_ERR(reset_gpio);
3824 goto out_free_dev;
3825 } else if (reset_gpio) {
3826 data->reset_gpio = reset_gpio;
3827 }
3828
Marcel Holtmann9f8f9622013-04-10 08:11:35 -07003829 hdev->open = btusb_open;
3830 hdev->close = btusb_close;
3831 hdev->flush = btusb_flush;
3832 hdev->send = btusb_send_frame;
3833 hdev->notify = btusb_notify;
3834
Rajat Jainfd913ef2017-02-01 14:24:09 -08003835#ifdef CONFIG_PM
3836 err = btusb_config_oob_wake(hdev);
3837 if (err)
3838 goto out_free_dev;
Rajat Jaina4ccc9e2017-02-01 14:24:10 -08003839
3840 /* Marvell devices may need a specific chip configuration */
3841 if (id->driver_info & BTUSB_MARVELL && data->oob_wake_irq) {
3842 err = marvell_config_oob_wake(hdev);
3843 if (err)
3844 goto out_free_dev;
3845 }
Rajat Jainfd913ef2017-02-01 14:24:09 -08003846#endif
Szymon Janc418678b2016-09-01 17:22:37 +02003847 if (id->driver_info & BTUSB_CW6622)
3848 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
3849
Marcel Holtmann6c9d4352015-10-17 14:39:27 +02003850 if (id->driver_info & BTUSB_BCM2045)
3851 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
3852
Marcel Holtmann9f8f9622013-04-10 08:11:35 -07003853 if (id->driver_info & BTUSB_BCM92035)
3854 hdev->setup = btusb_setup_bcm92035;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003855
Marcel Holtmannc0a21a52019-09-27 08:48:58 +02003856 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_BCM) &&
3857 (id->driver_info & BTUSB_BCM_PATCHRAM)) {
Marcel Holtmann49a5f782015-10-20 02:30:48 +02003858 hdev->manufacturer = 15;
Marcel Holtmannc2bfb102015-04-05 22:52:14 -07003859 hdev->setup = btbcm_setup_patchram;
Marcel Holtmann9d08f502015-10-08 20:23:08 +02003860 hdev->set_diag = btusb_bcm_set_diag;
Marcel Holtmann1df1f592015-04-05 22:52:11 -07003861 hdev->set_bdaddr = btbcm_set_bdaddr;
Marcel Holtmann9d08f502015-10-08 20:23:08 +02003862
3863 /* Broadcom LM_DIAG Interface numbers are hardcoded */
Marcel Holtmann22f8e9d2015-10-20 00:53:33 +02003864 data->diag = usb_ifnum_to_if(data->udev, ifnum_base + 2);
Marcel Holtmannabbaf502014-07-02 00:53:48 +02003865 }
Petri Gynther10d4c672014-05-08 15:50:01 -07003866
Marcel Holtmannc0a21a52019-09-27 08:48:58 +02003867 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_BCM) &&
3868 (id->driver_info & BTUSB_BCM_APPLE)) {
Marcel Holtmann49a5f782015-10-20 02:30:48 +02003869 hdev->manufacturer = 15;
Marcel Holtmannc2bfb102015-04-05 22:52:14 -07003870 hdev->setup = btbcm_setup_apple;
Marcel Holtmann9d08f502015-10-08 20:23:08 +02003871 hdev->set_diag = btusb_bcm_set_diag;
3872
3873 /* Broadcom LM_DIAG Interface numbers are hardcoded */
Marcel Holtmann22f8e9d2015-10-20 00:53:33 +02003874 data->diag = usb_ifnum_to_if(data->udev, ifnum_base + 2);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02003875 }
Marcel Holtmann17b27722015-03-22 15:52:38 +01003876
Marcel Holtmanncb8d6592014-07-02 11:25:25 +02003877 if (id->driver_info & BTUSB_INTEL) {
Marcel Holtmann49a5f782015-10-20 02:30:48 +02003878 hdev->manufacturer = 2;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07003879 hdev->setup = btusb_setup_intel;
Tedd Ho-Jeong Anbfbd45e2015-02-13 09:20:52 -08003880 hdev->shutdown = btusb_shutdown_intel;
Marcel Holtmann3e247672015-10-17 16:00:28 +02003881 hdev->set_diag = btintel_set_diag_mfg;
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07003882 hdev->set_bdaddr = btintel_set_bdaddr;
Rajat Jaindc786b22019-01-24 15:28:14 -08003883 hdev->cmd_timeout = btusb_intel_cmd_timeout;
Jakub Pawlowskic33fb9b2015-01-30 18:55:58 -08003884 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
Jakub Pawlowskic1154842015-03-17 09:04:16 -07003885 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
Marcel Holtmann3e247672015-10-17 16:00:28 +02003886 set_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks);
Marcel Holtmanncb8d6592014-07-02 11:25:25 +02003887 }
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07003888
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08003889 if (id->driver_info & BTUSB_INTEL_NEW) {
Marcel Holtmann49a5f782015-10-20 02:30:48 +02003890 hdev->manufacturer = 2;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08003891 hdev->send = btusb_send_frame_intel;
3892 hdev->setup = btusb_setup_intel_new;
Raghuram Hegde017a01c2019-01-29 17:54:48 +05303893 hdev->shutdown = btusb_shutdown_intel_new;
Marcel Holtmanneeb6abe2015-07-05 14:37:39 +02003894 hdev->hw_error = btintel_hw_error;
Marcel Holtmann6d2e50d2015-10-09 14:42:08 +02003895 hdev->set_diag = btintel_set_diag;
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07003896 hdev->set_bdaddr = btintel_set_bdaddr;
Rajat Jaindc786b22019-01-24 15:28:14 -08003897 hdev->cmd_timeout = btusb_intel_cmd_timeout;
Marcel Holtmannb970c5b2015-02-01 23:57:18 -08003898 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
Justin TerAvestf7785022018-09-25 11:04:57 -06003899 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
Marcel Holtmannd8270fb2015-10-17 16:00:27 +02003900 set_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08003901 }
3902
Amitkumar Karwarae8df492014-07-18 14:47:06 -07003903 if (id->driver_info & BTUSB_MARVELL)
3904 hdev->set_bdaddr = btusb_set_bdaddr_marvell;
3905
Marcel Holtmannc0a21a52019-09-27 08:48:58 +02003906 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_MTK) &&
3907 (id->driver_info & BTUSB_MEDIATEK)) {
Sean Wanga1c49c432019-06-02 08:02:48 +08003908 hdev->setup = btusb_mtk_setup;
3909 hdev->shutdown = btusb_mtk_shutdown;
3910 hdev->manufacturer = 70;
3911 set_bit(HCI_QUIRK_NON_PERSISTENT_SETUP, &hdev->quirks);
3912 }
Sean Wanga1c49c432019-06-02 08:02:48 +08003913
Marcel Holtmann661cf882015-01-02 23:35:20 -08003914 if (id->driver_info & BTUSB_SWAVE) {
3915 set_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks);
Marcel Holtmannd57dbe72014-12-26 04:42:33 +01003916 set_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS, &hdev->quirks);
Marcel Holtmann661cf882015-01-02 23:35:20 -08003917 }
Marcel Holtmannd57dbe72014-12-26 04:42:33 +01003918
Marcel Holtmanne4c534b2015-10-21 01:31:45 +02003919 if (id->driver_info & BTUSB_INTEL_BOOT) {
3920 hdev->manufacturer = 2;
Marcel Holtmann40df7832014-07-06 13:29:58 +02003921 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
Marcel Holtmanne4c534b2015-10-21 01:31:45 +02003922 }
Marcel Holtmann40df7832014-07-06 13:29:58 +02003923
Jakub Pawlowski79f0c872015-01-29 10:38:34 -08003924 if (id->driver_info & BTUSB_ATH3012) {
Takashi Iwai803cdb82018-05-21 22:34:52 +02003925 data->setup_on_usb = btusb_setup_qca;
Toshi Kikuchi58592232014-12-12 10:58:05 -08003926 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
Jakub Pawlowski3d50d512015-03-17 09:04:15 -07003927 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
Jakub Pawlowski79f0c872015-01-29 10:38:34 -08003928 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
3929 }
Toshi Kikuchi58592232014-12-12 10:58:05 -08003930
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003931 if (id->driver_info & BTUSB_QCA_ROME) {
3932 data->setup_on_usb = btusb_setup_qca;
3933 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
Vic Wei96e58d32018-03-28 08:28:47 -07003934 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
Hans de Goedefc549102018-04-27 11:26:43 +02003935 btusb_check_needs_reset_resume(intf);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003936 }
3937
Arnd Bergmann42d22092019-09-18 21:59:02 +02003938 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_RTL) &&
3939 (id->driver_info & BTUSB_REALTEK)) {
Carlo Caionedb33c772015-05-14 10:49:09 +02003940 hdev->setup = btrtl_setup_realtek;
Jian-Hong Pan7af3f5582019-06-25 16:30:51 +08003941 hdev->shutdown = btrtl_shutdown_realtek;
Alex Lud7ef0d12019-09-05 10:36:31 +08003942 hdev->cmd_timeout = btusb_rtl_cmd_timeout;
Daniel Drake04b8c812015-05-21 08:23:50 -06003943
Alex Lu9e455242019-08-14 20:02:52 +08003944 /* Realtek devices lose their updated firmware over global
3945 * suspend that means host doesn't send SET_FEATURE
3946 * (DEVICE_REMOTE_WAKEUP)
Daniel Drake04b8c812015-05-21 08:23:50 -06003947 */
Alex Lu9e455242019-08-14 20:02:52 +08003948 set_bit(BTUSB_WAKEUP_DISABLE, &data->flags);
Kai-Heng Feng7ecacaf2019-12-05 17:07:01 +08003949
3950 err = usb_autopm_get_interface(intf);
3951 if (err < 0)
3952 goto out_free_dev;
Daniel Drake04b8c812015-05-21 08:23:50 -06003953 }
Daniel Drakea2698a92015-04-16 14:09:55 -06003954
Marcel Holtmann893ba542015-01-28 20:27:34 -08003955 if (id->driver_info & BTUSB_AMP) {
3956 /* AMP controllers do not support SCO packets */
3957 data->isoc = NULL;
3958 } else {
Marcel Holtmann22f8e9d2015-10-20 00:53:33 +02003959 /* Interface orders are hardcoded in the specification */
3960 data->isoc = usb_ifnum_to_if(data->udev, ifnum_base + 1);
Marcel Holtmann459232f2017-10-24 19:42:45 +02003961 data->isoc_ifnum = ifnum_base + 1;
Marcel Holtmann893ba542015-01-28 20:27:34 -08003962 }
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02003963
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01003964 if (!reset)
Szymon Janca6c511c2012-05-23 12:35:46 +02003965 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02003966
3967 if (force_scofix || id->driver_info & BTUSB_WRONG_SCO_MTU) {
3968 if (!disable_scofix)
3969 set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
3970 }
3971
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02003972 if (id->driver_info & BTUSB_BROKEN_ISOC)
3973 data->isoc = NULL;
3974
Alain Michaud4b127bd2020-02-27 18:29:39 +00003975 if (id->driver_info & BTUSB_WIDEBAND_SPEECH)
Alain Michaud00bce3f2020-03-05 16:14:59 +00003976 set_bit(HCI_QUIRK_WIDEBAND_SPEECH_SUPPORTED, &hdev->quirks);
Alain Michaud4b127bd2020-02-27 18:29:39 +00003977
Alain Michaudaff8c482020-04-23 14:43:31 +00003978 if (id->driver_info & BTUSB_VALID_LE_STATES)
3979 set_bit(HCI_QUIRK_VALID_LE_STATES, &hdev->quirks);
3980
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01003981 if (id->driver_info & BTUSB_DIGIANSWER) {
3982 data->cmdreq_type = USB_TYPE_VENDOR;
Szymon Janca6c511c2012-05-23 12:35:46 +02003983 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01003984 }
3985
3986 if (id->driver_info & BTUSB_CSR) {
3987 struct usb_device *udev = data->udev;
Marcel Holtmann81cac642014-01-03 03:02:36 -08003988 u16 bcdDevice = le16_to_cpu(udev->descriptor.bcdDevice);
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01003989
3990 /* Old firmware would otherwise execute USB reset */
Marcel Holtmann81cac642014-01-03 03:02:36 -08003991 if (bcdDevice < 0x117)
Szymon Janca6c511c2012-05-23 12:35:46 +02003992 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmann81cac642014-01-03 03:02:36 -08003993
3994 /* Fake CSR devices with broken commands */
Johan Hedberg6cafcd92015-08-30 21:47:21 +03003995 if (bcdDevice <= 0x100 || bcdDevice == 0x134)
Marcel Holtmann81cac642014-01-03 03:02:36 -08003996 hdev->setup = btusb_setup_csr;
Jakub Pawlowski49c989a2015-03-17 09:04:17 -07003997
3998 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01003999 }
4000
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02004001 if (id->driver_info & BTUSB_SNIFFER) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02004002 struct usb_device *udev = data->udev;
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02004003
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01004004 /* New sniffer firmware has crippled HCI interface */
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02004005 if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
4006 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
4007 }
4008
Marcel Holtmann3a5ef202014-07-06 14:53:54 +02004009 if (id->driver_info & BTUSB_INTEL_BOOT) {
4010 /* A bug in the bootloader causes that interrupt interface is
4011 * only enabled after receiving SetInterface(0, AltSetting=0).
4012 */
4013 err = usb_set_interface(data->udev, 0, 0);
4014 if (err < 0) {
4015 BT_ERR("failed to set interface 0, alt 0 %d", err);
Rajat Jain10ab1332017-02-01 14:24:08 -08004016 goto out_free_dev;
Marcel Holtmann3a5ef202014-07-06 14:53:54 +02004017 }
4018 }
4019
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02004020 if (data->isoc) {
4021 err = usb_driver_claim_interface(&btusb_driver,
Marcel Holtmann89e75332014-09-16 04:44:50 +02004022 data->isoc, data);
Rajat Jain10ab1332017-02-01 14:24:08 -08004023 if (err < 0)
4024 goto out_free_dev;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02004025 }
4026
Marcel Holtmannc0a21a52019-09-27 08:48:58 +02004027 if (IS_ENABLED(CONFIG_BT_HCIBTUSB_BCM) && data->diag) {
Marcel Holtmann9d08f502015-10-08 20:23:08 +02004028 if (!usb_driver_claim_interface(&btusb_driver,
4029 data->diag, data))
4030 __set_diag_interface(hdev);
4031 else
4032 data->diag = NULL;
4033 }
Marcel Holtmann9d08f502015-10-08 20:23:08 +02004034
Hans de Goedeeff2d682017-11-13 14:44:16 +01004035 if (enable_autosuspend)
4036 usb_enable_autosuspend(data->udev);
4037
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004038 err = hci_register_dev(hdev);
Rajat Jain10ab1332017-02-01 14:24:08 -08004039 if (err < 0)
4040 goto out_free_dev;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004041
4042 usb_set_intfdata(intf, data);
4043
4044 return 0;
Rajat Jain10ab1332017-02-01 14:24:08 -08004045
4046out_free_dev:
Rajat Jaindc786b22019-01-24 15:28:14 -08004047 if (data->reset_gpio)
4048 gpiod_put(data->reset_gpio);
Rajat Jain10ab1332017-02-01 14:24:08 -08004049 hci_free_dev(hdev);
4050 return err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004051}
4052
4053static void btusb_disconnect(struct usb_interface *intf)
4054{
4055 struct btusb_data *data = usb_get_intfdata(intf);
4056 struct hci_dev *hdev;
4057
4058 BT_DBG("intf %p", intf);
4059
4060 if (!data)
4061 return;
4062
4063 hdev = data->hdev;
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02004064 usb_set_intfdata(data->intf, NULL);
4065
4066 if (data->isoc)
4067 usb_set_intfdata(data->isoc, NULL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004068
Marcel Holtmann9d08f502015-10-08 20:23:08 +02004069 if (data->diag)
4070 usb_set_intfdata(data->diag, NULL);
4071
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004072 hci_unregister_dev(hdev);
4073
Marcel Holtmann9d08f502015-10-08 20:23:08 +02004074 if (intf == data->intf) {
4075 if (data->isoc)
4076 usb_driver_release_interface(&btusb_driver, data->isoc);
4077 if (data->diag)
4078 usb_driver_release_interface(&btusb_driver, data->diag);
4079 } else if (intf == data->isoc) {
4080 if (data->diag)
4081 usb_driver_release_interface(&btusb_driver, data->diag);
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02004082 usb_driver_release_interface(&btusb_driver, data->intf);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02004083 } else if (intf == data->diag) {
4084 usb_driver_release_interface(&btusb_driver, data->intf);
4085 if (data->isoc)
4086 usb_driver_release_interface(&btusb_driver, data->isoc);
4087 }
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02004088
Rajat Jainfd913ef2017-02-01 14:24:09 -08004089 if (data->oob_wake_irq)
4090 device_init_wakeup(&data->udev->dev, false);
4091
Rajat Jaindc786b22019-01-24 15:28:14 -08004092 if (data->reset_gpio)
4093 gpiod_put(data->reset_gpio);
4094
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004095 hci_free_dev(hdev);
4096}
4097
Oliver Neukum7bee5492009-08-24 23:44:59 +02004098#ifdef CONFIG_PM
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01004099static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
4100{
4101 struct btusb_data *data = usb_get_intfdata(intf);
4102
4103 BT_DBG("intf %p", intf);
4104
4105 if (data->suspend_count++)
4106 return 0;
4107
Oliver Neukum7bee5492009-08-24 23:44:59 +02004108 spin_lock_irq(&data->txlock);
Alan Stern5b1b0b82011-08-19 23:49:48 +02004109 if (!(PMSG_IS_AUTO(message) && data->tx_in_flight)) {
Oliver Neukum7bee5492009-08-24 23:44:59 +02004110 set_bit(BTUSB_SUSPENDING, &data->flags);
4111 spin_unlock_irq(&data->txlock);
4112 } else {
4113 spin_unlock_irq(&data->txlock);
4114 data->suspend_count--;
4115 return -EBUSY;
4116 }
4117
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01004118 cancel_work_sync(&data->work);
4119
Oliver Neukum7bee5492009-08-24 23:44:59 +02004120 btusb_stop_traffic(data);
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01004121 usb_kill_anchored_urbs(&data->tx_anchor);
4122
Rajat Jainfd913ef2017-02-01 14:24:09 -08004123 if (data->oob_wake_irq && device_may_wakeup(&data->udev->dev)) {
4124 set_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags);
4125 enable_irq_wake(data->oob_wake_irq);
4126 enable_irq(data->oob_wake_irq);
4127 }
4128
Alex Lu9e455242019-08-14 20:02:52 +08004129 /* For global suspend, Realtek devices lose the loaded fw
4130 * in them. But for autosuspend, firmware should remain.
4131 * Actually, it depends on whether the usb host sends
4132 * set feature (enable wakeup) or not.
4133 */
4134 if (test_bit(BTUSB_WAKEUP_DISABLE, &data->flags)) {
4135 if (PMSG_IS_AUTO(message) &&
4136 device_can_wakeup(&data->udev->dev))
4137 data->udev->do_remote_wakeup = 1;
4138 else if (!PMSG_IS_AUTO(message))
4139 data->udev->reset_resume = 1;
4140 }
4141
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01004142 return 0;
4143}
4144
Oliver Neukum7bee5492009-08-24 23:44:59 +02004145static void play_deferred(struct btusb_data *data)
4146{
4147 struct urb *urb;
4148 int err;
4149
4150 while ((urb = usb_get_from_anchor(&data->deferred))) {
Jeffy Chen19cfe912017-07-20 18:53:50 +08004151 usb_anchor_urb(urb, &data->tx_anchor);
4152
Oliver Neukum7bee5492009-08-24 23:44:59 +02004153 err = usb_submit_urb(urb, GFP_ATOMIC);
Jeffy Chen19cfe912017-07-20 18:53:50 +08004154 if (err < 0) {
4155 if (err != -EPERM && err != -ENODEV)
4156 BT_ERR("%s urb %p submission failed (%d)",
4157 data->hdev->name, urb, -err);
4158 kfree(urb->setup_packet);
4159 usb_unanchor_urb(urb);
4160 usb_free_urb(urb);
Oliver Neukum7bee5492009-08-24 23:44:59 +02004161 break;
Jeffy Chen19cfe912017-07-20 18:53:50 +08004162 }
Oliver Neukum7bee5492009-08-24 23:44:59 +02004163
4164 data->tx_in_flight++;
Jeffy Chen19cfe912017-07-20 18:53:50 +08004165 usb_free_urb(urb);
Oliver Neukum7bee5492009-08-24 23:44:59 +02004166 }
Jeffy Chen19cfe912017-07-20 18:53:50 +08004167
4168 /* Cleanup the rest deferred urbs. */
4169 while ((urb = usb_get_from_anchor(&data->deferred))) {
4170 kfree(urb->setup_packet);
4171 usb_free_urb(urb);
4172 }
Oliver Neukum7bee5492009-08-24 23:44:59 +02004173}
4174
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01004175static int btusb_resume(struct usb_interface *intf)
4176{
4177 struct btusb_data *data = usb_get_intfdata(intf);
4178 struct hci_dev *hdev = data->hdev;
Oliver Neukum7bee5492009-08-24 23:44:59 +02004179 int err = 0;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01004180
4181 BT_DBG("intf %p", intf);
4182
4183 if (--data->suspend_count)
4184 return 0;
4185
Rajat Jainfd913ef2017-02-01 14:24:09 -08004186 /* Disable only if not already disabled (keep it balanced) */
4187 if (test_and_clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags)) {
4188 disable_irq(data->oob_wake_irq);
4189 disable_irq_wake(data->oob_wake_irq);
4190 }
4191
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01004192 if (!test_bit(HCI_RUNNING, &hdev->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +02004193 goto done;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01004194
4195 if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
4196 err = btusb_submit_intr_urb(hdev, GFP_NOIO);
4197 if (err < 0) {
4198 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02004199 goto failed;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01004200 }
4201 }
4202
4203 if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) {
Marcel Holtmann43c2e572009-02-04 17:41:38 +01004204 err = btusb_submit_bulk_urb(hdev, GFP_NOIO);
4205 if (err < 0) {
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01004206 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02004207 goto failed;
4208 }
4209
4210 btusb_submit_bulk_urb(hdev, GFP_NOIO);
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01004211 }
4212
4213 if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
4214 if (btusb_submit_isoc_urb(hdev, GFP_NOIO) < 0)
4215 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
4216 else
4217 btusb_submit_isoc_urb(hdev, GFP_NOIO);
4218 }
4219
Oliver Neukum7bee5492009-08-24 23:44:59 +02004220 spin_lock_irq(&data->txlock);
4221 play_deferred(data);
4222 clear_bit(BTUSB_SUSPENDING, &data->flags);
4223 spin_unlock_irq(&data->txlock);
4224 schedule_work(&data->work);
4225
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01004226 return 0;
Oliver Neukum7bee5492009-08-24 23:44:59 +02004227
4228failed:
4229 usb_scuttle_anchored_urbs(&data->deferred);
4230done:
4231 spin_lock_irq(&data->txlock);
4232 clear_bit(BTUSB_SUSPENDING, &data->flags);
4233 spin_unlock_irq(&data->txlock);
4234
4235 return err;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01004236}
Oliver Neukum7bee5492009-08-24 23:44:59 +02004237#endif
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01004238
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004239static struct usb_driver btusb_driver = {
4240 .name = "btusb",
4241 .probe = btusb_probe,
4242 .disconnect = btusb_disconnect,
Oliver Neukum7bee5492009-08-24 23:44:59 +02004243#ifdef CONFIG_PM
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01004244 .suspend = btusb_suspend,
4245 .resume = btusb_resume,
Oliver Neukum7bee5492009-08-24 23:44:59 +02004246#endif
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004247 .id_table = btusb_table,
Oliver Neukum7bee5492009-08-24 23:44:59 +02004248 .supports_autosuspend = 1,
Sarah Sharpe1f12eb2012-04-23 10:08:51 -07004249 .disable_hub_initiated_lpm = 1,
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004250};
4251
Greg Kroah-Hartman93f15082011-11-18 09:47:34 -08004252module_usb_driver(btusb_driver);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004253
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02004254module_param(disable_scofix, bool, 0644);
4255MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
4256
4257module_param(force_scofix, bool, 0644);
4258MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
4259
Hans de Goedeeff2d682017-11-13 14:44:16 +01004260module_param(enable_autosuspend, bool, 0644);
4261MODULE_PARM_DESC(enable_autosuspend, "Enable USB autosuspend by default");
4262
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02004263module_param(reset, bool, 0644);
4264MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
4265
Marcel Holtmann5e23b922007-10-20 14:12:34 +02004266MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
4267MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
4268MODULE_VERSION(VERSION);
4269MODULE_LICENSE("GPL");