blob: c8e9ae6b99e1753b95f436e89b67e962091f9c81 [file] [log] [blame]
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001/*
2 *
3 * Generic Bluetooth USB driver
4 *
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02005 * Copyright (C) 2005-2008 Marcel Holtmann <marcel@holtmann.org>
Marcel Holtmann5e23b922007-10-20 14:12:34 +02006 *
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 */
23
Marcel Holtmann5e23b922007-10-20 14:12:34 +020024#include <linux/module.h>
Marcel Holtmann5e23b922007-10-20 14:12:34 +020025#include <linux/usb.h>
Hans de Goede61f5ace2018-01-08 10:44:16 +010026#include <linux/usb/quirks.h>
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -070027#include <linux/firmware.h>
Rajat Jainfd913ef2017-02-01 14:24:09 -080028#include <linux/of_device.h>
29#include <linux/of_irq.h>
Jeffy Chen017789f2017-02-24 14:24:29 +080030#include <linux/suspend.h>
Daniel Drakea2698a92015-04-16 14:09:55 -060031#include <asm/unaligned.h>
Marcel Holtmann5e23b922007-10-20 14:12:34 +020032
33#include <net/bluetooth/bluetooth.h>
34#include <net/bluetooth/hci_core.h>
35
Marcel Holtmann4185a0f2015-04-06 00:52:12 -070036#include "btintel.h"
Marcel Holtmann1df1f592015-04-05 22:52:11 -070037#include "btbcm.h"
Carlo Caionedb33c772015-05-14 10:49:09 +020038#include "btrtl.h"
Marcel Holtmann1df1f592015-04-05 22:52:11 -070039
Marcel Holtmann34dced9b2015-04-05 22:52:16 -070040#define VERSION "0.8"
Marcel Holtmanncfeb4142008-08-07 22:26:56 +020041
Rusty Russell90ab5ee2012-01-13 09:32:20 +103042static bool disable_scofix;
43static bool force_scofix;
Hans de Goedeeff2d682017-11-13 14:44:16 +010044static bool enable_autosuspend = IS_ENABLED(CONFIG_BT_HCIBTUSB_AUTOSUSPEND);
Marcel Holtmann7a9d4022008-11-30 12:17:26 +010045
Shailendra Verma917a3332015-05-25 23:53:40 +053046static bool reset = true;
Marcel Holtmanncfeb4142008-08-07 22:26:56 +020047
48static struct usb_driver btusb_driver;
49
50#define BTUSB_IGNORE 0x01
Marcel Holtmann7a9d4022008-11-30 12:17:26 +010051#define BTUSB_DIGIANSWER 0x02
52#define BTUSB_CSR 0x04
53#define BTUSB_SNIFFER 0x08
54#define BTUSB_BCM92035 0x10
55#define BTUSB_BROKEN_ISOC 0x20
56#define BTUSB_WRONG_SCO_MTU 0x40
Steven.Li2d25f8b2011-07-01 14:02:36 +080057#define BTUSB_ATH3012 0x80
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -070058#define BTUSB_INTEL 0x100
Marcel Holtmann40df7832014-07-06 13:29:58 +020059#define BTUSB_INTEL_BOOT 0x200
60#define BTUSB_BCM_PATCHRAM 0x400
Amitkumar Karwarae8df492014-07-18 14:47:06 -070061#define BTUSB_MARVELL 0x800
Marcel Holtmann4fcef8e2015-01-01 17:34:37 -080062#define BTUSB_SWAVE 0x1000
Marcel Holtmanncda0dd72015-01-26 21:33:48 -080063#define BTUSB_INTEL_NEW 0x2000
Marcel Holtmann893ba542015-01-28 20:27:34 -080064#define BTUSB_AMP 0x4000
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +000065#define BTUSB_QCA_ROME 0x8000
Marcel Holtmann17b27722015-03-22 15:52:38 +010066#define BTUSB_BCM_APPLE 0x10000
Daniel Drakea2698a92015-04-16 14:09:55 -060067#define BTUSB_REALTEK 0x20000
Marcel Holtmann6c9d4352015-10-17 14:39:27 +020068#define BTUSB_BCM2045 0x40000
Marcel Holtmann22f8e9d2015-10-20 00:53:33 +020069#define BTUSB_IFNUM_2 0x80000
Szymon Janc418678b2016-09-01 17:22:37 +020070#define BTUSB_CW6622 0x100000
Marcel Holtmann5e23b922007-10-20 14:12:34 +020071
Marcel Holtmann54265202013-10-11 07:46:18 -070072static const struct usb_device_id btusb_table[] = {
Marcel Holtmann5e23b922007-10-20 14:12:34 +020073 /* Generic Bluetooth USB device */
74 { USB_DEVICE_INFO(0xe0, 0x01, 0x01) },
75
Marcel Holtmann893ba542015-01-28 20:27:34 -080076 /* Generic Bluetooth AMP device */
77 { USB_DEVICE_INFO(0xe0, 0x01, 0x04), .driver_info = BTUSB_AMP },
78
Daniel Draked63b2822015-07-17 11:12:25 -060079 /* Generic Bluetooth USB interface */
80 { USB_INTERFACE_INFO(0xe0, 0x01, 0x01) },
81
Henrik Rydberg1fa65352012-08-25 19:28:06 +020082 /* Apple-specific (Broadcom) devices */
Marcel Holtmann17b27722015-03-22 15:52:38 +010083 { USB_VENDOR_AND_INTERFACE_INFO(0x05ac, 0xff, 0x01, 0x01),
Marcel Holtmann22f8e9d2015-10-20 00:53:33 +020084 .driver_info = BTUSB_BCM_APPLE | BTUSB_IFNUM_2 },
Henrik Rydberg1fa65352012-08-25 19:28:06 +020085
Cho, Yu-Chen178c0592013-06-04 21:40:26 +080086 /* MediaTek MT76x0E */
87 { USB_DEVICE(0x0e8d, 0x763f) },
88
Oliver Neukumc510eae2011-09-21 11:41:45 +020089 /* Broadcom SoftSailing reporting vendor specific */
Don Zickus2e8b50632012-03-28 16:41:11 -040090 { USB_DEVICE(0x0a5c, 0x21e1) },
Oliver Neukumc510eae2011-09-21 11:41:45 +020091
Nobuhiro Iwamatsu3cd01972010-08-20 16:24:07 +090092 /* Apple MacBookPro 7,1 */
93 { USB_DEVICE(0x05ac, 0x8213) },
94
Cyril Lacoux0a79f672010-07-14 10:29:27 +040095 /* Apple iMac11,1 */
96 { USB_DEVICE(0x05ac, 0x8215) },
97
Nobuhiro Iwamatsu9c047152010-08-20 16:24:06 +090098 /* Apple MacBookPro6,2 */
99 { USB_DEVICE(0x05ac, 0x8218) },
100
Edgar (gimli) Hucek3e3ede72010-11-04 08:04:33 +0100101 /* Apple MacBookAir3,1, MacBookAir3,2 */
102 { USB_DEVICE(0x05ac, 0x821b) },
103
Pieter-Augustijn Van Malleghema63b7232011-09-07 02:28:10 -0400104 /* Apple MacBookAir4,1 */
105 { USB_DEVICE(0x05ac, 0x821f) },
106
Marc-Antoine Perennou88d377b2011-03-24 14:51:21 -0300107 /* Apple MacBookPro8,2 */
108 { USB_DEVICE(0x05ac, 0x821a) },
109
Jurgen Kramerf78b68262011-09-04 18:01:42 +0200110 /* Apple MacMini5,1 */
111 { USB_DEVICE(0x05ac, 0x8281) },
112
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200113 /* AVM BlueFRITZ! USB v2.0 */
Marcel Holtmann4fcef8e2015-01-01 17:34:37 -0800114 { USB_DEVICE(0x057c, 0x3800), .driver_info = BTUSB_SWAVE },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200115
116 /* Bluetooth Ultraport Module from IBM */
117 { USB_DEVICE(0x04bf, 0x030a) },
118
119 /* ALPS Modules with non-standard id */
120 { USB_DEVICE(0x044e, 0x3001) },
121 { USB_DEVICE(0x044e, 0x3002) },
122
123 /* Ericsson with non-standard id */
124 { USB_DEVICE(0x0bdb, 0x1002) },
125
126 /* Canyon CN-BTU1 with HID interfaces */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100127 { USB_DEVICE(0x0c10, 0x0000) },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200128
Wen-chien Jesse Sungd13431c2011-11-08 14:30:22 +0800129 /* Broadcom BCM20702A0 */
130 { USB_DEVICE(0x413c, 0x8197) },
131
Marcel Holtmannd049f4e2015-01-26 20:35:32 -0800132 /* Broadcom BCM20702B0 (Dynex/Insignia) */
133 { USB_DEVICE(0x19ff, 0x0239), .driver_info = BTUSB_BCM_PATCHRAM },
134
Santtu Rekilä2faf71c2015-10-05 15:45:27 +0300135 /* Broadcom BCM43142A0 (Foxconn/Lenovo) */
Dmitry Tunin628c26b2017-07-06 14:41:13 +0300136 { USB_VENDOR_AND_INTERFACE_INFO(0x105b, 0xff, 0x01, 0x01),
137 .driver_info = BTUSB_BCM_PATCHRAM },
Santtu Rekilä2faf71c2015-10-05 15:45:27 +0300138
Christoph Haaga57bac42017-02-10 14:02:45 +0100139 /* Broadcom BCM920703 (HTC Vive) */
140 { USB_VENDOR_AND_INTERFACE_INFO(0x0bb4, 0xff, 0x01, 0x01),
141 .driver_info = BTUSB_BCM_PATCHRAM },
142
Steven Harms98514032012-04-13 14:45:55 -0400143 /* Foxconn - Hon Hai */
Heinrich Siebmanns6029ddc2014-12-03 19:32:22 +0100144 { USB_VENDOR_AND_INTERFACE_INFO(0x0489, 0xff, 0x01, 0x01),
145 .driver_info = BTUSB_BCM_PATCHRAM },
Steven Harms98514032012-04-13 14:45:55 -0400146
Matej Dubovy8f0c3042015-02-02 18:50:14 +0100147 /* Lite-On Technology - Broadcom based */
148 { USB_VENDOR_AND_INTERFACE_INFO(0x04ca, 0xff, 0x01, 0x01),
149 .driver_info = BTUSB_BCM_PATCHRAM },
150
Andy Shevchenko0b880062014-02-18 18:26:19 +0200151 /* Broadcom devices with vendor specific id */
Petri Gynther10d4c672014-05-08 15:50:01 -0700152 { USB_VENDOR_AND_INTERFACE_INFO(0x0a5c, 0xff, 0x01, 0x01),
153 .driver_info = BTUSB_BCM_PATCHRAM },
Gustavo Padovan92c385f2012-08-06 15:36:49 -0300154
Marcel Holtmannc2aef6e2014-07-21 14:02:33 +0200155 /* ASUSTek Computer - Broadcom based */
Rick Dunn9a5abdaa2015-01-17 05:29:12 +0100156 { USB_VENDOR_AND_INTERFACE_INFO(0x0b05, 0xff, 0x01, 0x01),
157 .driver_info = BTUSB_BCM_PATCHRAM },
Marcel Holtmannc2aef6e2014-07-21 14:02:33 +0200158
Ken O'Brien5bcecf32013-09-21 19:14:43 +0100159 /* Belkin F8065bf - Broadcom based */
Marcel Holtmann6331c682015-03-27 15:11:41 -0700160 { USB_VENDOR_AND_INTERFACE_INFO(0x050d, 0xff, 0x01, 0x01),
161 .driver_info = BTUSB_BCM_PATCHRAM },
Ken O'Brien5bcecf32013-09-21 19:14:43 +0100162
Jurgen Kramer9113bfd2014-02-15 12:01:09 +0100163 /* IMC Networks - Broadcom based */
Marcel Holtmann6331c682015-03-27 15:11:41 -0700164 { USB_VENDOR_AND_INTERFACE_INFO(0x13d3, 0xff, 0x01, 0x01),
165 .driver_info = BTUSB_BCM_PATCHRAM },
Jurgen Kramer9113bfd2014-02-15 12:01:09 +0100166
Wen-chien Jesse Sungfdfddc62017-01-10 15:41:13 +0800167 /* Dell Computer - Broadcom based */
168 { USB_VENDOR_AND_INTERFACE_INFO(0x413c, 0xff, 0x01, 0x01),
169 .driver_info = BTUSB_BCM_PATCHRAM },
170
Dmitry Tunin1623d0b2015-12-05 14:09:36 +0300171 /* Toshiba Corp - Broadcom based */
172 { USB_VENDOR_AND_INTERFACE_INFO(0x0930, 0xff, 0x01, 0x01),
173 .driver_info = BTUSB_BCM_PATCHRAM },
174
Marcel Holtmann40df7832014-07-06 13:29:58 +0200175 /* Intel Bluetooth USB Bootloader (RAM module) */
Marcel Holtmannd92f2df2014-07-06 14:53:55 +0200176 { USB_DEVICE(0x8087, 0x0a5a),
177 .driver_info = BTUSB_INTEL_BOOT | BTUSB_BROKEN_ISOC },
Marcel Holtmann40df7832014-07-06 13:29:58 +0200178
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200179 { } /* Terminating entry */
180};
181
182MODULE_DEVICE_TABLE(usb, btusb_table);
183
Marcel Holtmann54265202013-10-11 07:46:18 -0700184static const struct usb_device_id blacklist_table[] = {
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200185 /* CSR BlueCore devices */
186 { USB_DEVICE(0x0a12, 0x0001), .driver_info = BTUSB_CSR },
187
188 /* Broadcom BCM2033 without firmware */
189 { USB_DEVICE(0x0a5c, 0x2033), .driver_info = BTUSB_IGNORE },
190
Marcel Holtmann6c9d4352015-10-17 14:39:27 +0200191 /* Broadcom BCM2045 devices */
192 { USB_DEVICE(0x0a5c, 0x2045), .driver_info = BTUSB_BCM2045 },
193
Bala Shanmugambe931122010-11-26 17:35:46 +0530194 /* Atheros 3011 with sflash firmware */
Andy Shevchenko0b880062014-02-18 18:26:19 +0200195 { USB_DEVICE(0x0489, 0xe027), .driver_info = BTUSB_IGNORE },
196 { USB_DEVICE(0x0489, 0xe03d), .driver_info = BTUSB_IGNORE },
Alexander Ploumistos2eeff0b2015-02-13 21:05:11 +0200197 { USB_DEVICE(0x04f2, 0xaff1), .driver_info = BTUSB_IGNORE },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200198 { USB_DEVICE(0x0930, 0x0215), .driver_info = BTUSB_IGNORE },
Bala Shanmugambe931122010-11-26 17:35:46 +0530199 { USB_DEVICE(0x0cf3, 0x3002), .driver_info = BTUSB_IGNORE },
Marek Vasut6eda5412012-06-08 14:32:50 +0200200 { USB_DEVICE(0x0cf3, 0xe019), .driver_info = BTUSB_IGNORE },
Andy Ross2a7bccc2011-05-09 16:11:16 -0700201 { USB_DEVICE(0x13d3, 0x3304), .driver_info = BTUSB_IGNORE },
Bala Shanmugambe931122010-11-26 17:35:46 +0530202
Cho, Yu-Chen509e7862011-01-26 17:10:59 +0800203 /* Atheros AR9285 Malbec with sflash firmware */
204 { USB_DEVICE(0x03f0, 0x311d), .driver_info = BTUSB_IGNORE },
205
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530206 /* Atheros 3012 with sflash firmware */
Andy Shevchenko0b880062014-02-18 18:26:19 +0200207 { USB_DEVICE(0x0489, 0xe04d), .driver_info = BTUSB_ATH3012 },
208 { USB_DEVICE(0x0489, 0xe04e), .driver_info = BTUSB_ATH3012 },
209 { USB_DEVICE(0x0489, 0xe056), .driver_info = BTUSB_ATH3012 },
210 { USB_DEVICE(0x0489, 0xe057), .driver_info = BTUSB_ATH3012 },
211 { USB_DEVICE(0x0489, 0xe05f), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin692c0622015-06-06 20:25:40 +0300212 { USB_DEVICE(0x0489, 0xe076), .driver_info = BTUSB_ATH3012 },
Anantha Krishnan4b552bc2014-10-06 16:31:49 +0530213 { USB_DEVICE(0x0489, 0xe078), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin28c971d2016-02-10 00:49:11 +0300214 { USB_DEVICE(0x0489, 0xe095), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200215 { USB_DEVICE(0x04c5, 0x1330), .driver_info = BTUSB_ATH3012 },
216 { USB_DEVICE(0x04ca, 0x3004), .driver_info = BTUSB_ATH3012 },
217 { USB_DEVICE(0x04ca, 0x3005), .driver_info = BTUSB_ATH3012 },
218 { USB_DEVICE(0x04ca, 0x3006), .driver_info = BTUSB_ATH3012 },
Mohammed Habibulla1fb4e092014-04-17 11:37:13 -0700219 { USB_DEVICE(0x04ca, 0x3007), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200220 { USB_DEVICE(0x04ca, 0x3008), .driver_info = BTUSB_ATH3012 },
221 { USB_DEVICE(0x04ca, 0x300b), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin7e730c7f2015-06-18 20:41:51 +0300222 { USB_DEVICE(0x04ca, 0x300d), .driver_info = BTUSB_ATH3012 },
Dmitry Tuninec0810d2015-05-02 13:36:58 +0300223 { USB_DEVICE(0x04ca, 0x300f), .driver_info = BTUSB_ATH3012 },
Janne Heikkinen134d3b32014-12-09 07:44:51 +0200224 { USB_DEVICE(0x04ca, 0x3010), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin81d90442016-02-28 11:04:06 +0300225 { USB_DEVICE(0x04ca, 0x3014), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin441ad622017-01-05 13:19:53 +0300226 { USB_DEVICE(0x04ca, 0x3018), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200227 { USB_DEVICE(0x0930, 0x0219), .driver_info = BTUSB_ATH3012 },
Dmitry Tunincd355ff2015-10-05 19:29:33 +0300228 { USB_DEVICE(0x0930, 0x021c), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200229 { USB_DEVICE(0x0930, 0x0220), .driver_info = BTUSB_ATH3012 },
Vincent Zwanenburg89d29752014-08-08 12:33:56 +0100230 { USB_DEVICE(0x0930, 0x0227), .driver_info = BTUSB_ATH3012 },
Andy Shevchenkoa735f9e2014-02-18 18:26:20 +0200231 { USB_DEVICE(0x0b05, 0x17d0), .driver_info = BTUSB_ATH3012 },
Ming Leid66629c2013-03-15 11:00:39 +0800232 { USB_DEVICE(0x0cf3, 0x0036), .driver_info = BTUSB_ATH3012 },
Steven.Li2d25f8b2011-07-01 14:02:36 +0800233 { USB_DEVICE(0x0cf3, 0x3004), .driver_info = BTUSB_ATH3012 },
Sunguk Lee94a32d102013-03-12 04:41:58 +0900234 { USB_DEVICE(0x0cf3, 0x3008), .driver_info = BTUSB_ATH3012 },
Cho, Yu-Chen07c0ea82012-03-14 22:01:21 +0200235 { USB_DEVICE(0x0cf3, 0x311d), .driver_info = BTUSB_ATH3012 },
Oliver Neukumb1312372014-01-16 15:37:11 +0100236 { USB_DEVICE(0x0cf3, 0x311e), .driver_info = BTUSB_ATH3012 },
Oliver Neukum1e56f1e2014-01-16 16:02:58 +0100237 { USB_DEVICE(0x0cf3, 0x311f), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200238 { USB_DEVICE(0x0cf3, 0x3121), .driver_info = BTUSB_ATH3012 },
Ming Leiebaf5792013-03-18 23:45:11 +0800239 { USB_DEVICE(0x0cf3, 0x817a), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin18e0afa2015-10-16 11:45:26 +0300240 { USB_DEVICE(0x0cf3, 0x817b), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200241 { USB_DEVICE(0x0cf3, 0xe003), .driver_info = BTUSB_ATH3012 },
AceLan Kaoac713112012-04-19 14:53:45 +0800242 { USB_DEVICE(0x0cf3, 0xe004), .driver_info = BTUSB_ATH3012 },
Peng Chen0a3658c2013-08-30 17:41:40 +0800243 { USB_DEVICE(0x0cf3, 0xe005), .driver_info = BTUSB_ATH3012 },
Wen-chien Jesse Sungca79f232015-05-13 11:39:24 +0800244 { USB_DEVICE(0x0cf3, 0xe006), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200245 { USB_DEVICE(0x13d3, 0x3362), .driver_info = BTUSB_ATH3012 },
246 { USB_DEVICE(0x13d3, 0x3375), .driver_info = BTUSB_ATH3012 },
AceLan Kaoeed307e2012-12-11 11:41:20 +0800247 { USB_DEVICE(0x13d3, 0x3393), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin609574e2016-02-10 15:33:17 +0300248 { USB_DEVICE(0x13d3, 0x3395), .driver_info = BTUSB_ATH3012 },
Sujith Manoharan5b77a1f2013-07-15 09:29:03 +0530249 { USB_DEVICE(0x13d3, 0x3402), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin3bb30a7c2014-11-25 20:19:52 +0300250 { USB_DEVICE(0x13d3, 0x3408), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin033efa92015-01-18 00:16:51 +0300251 { USB_DEVICE(0x13d3, 0x3423), .driver_info = BTUSB_ATH3012 },
Anantha Krishnanfa2f1392014-07-08 19:25:08 +0530252 { USB_DEVICE(0x13d3, 0x3432), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin75c6aca2016-03-04 01:32:19 +0300253 { USB_DEVICE(0x13d3, 0x3472), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin0d0cef62015-06-06 20:29:25 +0300254 { USB_DEVICE(0x13d3, 0x3474), .driver_info = BTUSB_ATH3012 },
Lauro Costa72f9f8b2016-05-09 17:36:11 -0300255 { USB_DEVICE(0x13d3, 0x3487), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin12d86892016-07-12 01:35:18 +0300256 { USB_DEVICE(0x13d3, 0x3490), .driver_info = BTUSB_ATH3012 },
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530257
Cho, Yu-Chene9036e32011-02-15 10:20:07 +0800258 /* Atheros AR5BBU12 with sflash firmware */
259 { USB_DEVICE(0x0489, 0xe02c), .driver_info = BTUSB_IGNORE },
260
Michael Gruetzner85d59722012-05-02 22:33:40 +0200261 /* Atheros AR5BBU12 with sflash firmware */
Yevgeniy Melnichukbc21fde2012-08-07 19:48:10 +0530262 { USB_DEVICE(0x0489, 0xe036), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200263 { USB_DEVICE(0x0489, 0xe03c), .driver_info = BTUSB_ATH3012 },
Michael Gruetzner85d59722012-05-02 22:33:40 +0200264
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +0000265 /* QCA ROME chipset */
Wen-chien Jesse Sung20541112015-05-13 11:39:25 +0800266 { USB_DEVICE(0x0cf3, 0xe007), .driver_info = BTUSB_QCA_ROME },
Kai-Heng Feng93519932016-08-16 12:50:06 +0800267 { USB_DEVICE(0x0cf3, 0xe009), .driver_info = BTUSB_QCA_ROME },
AceLan Kaoe5a49ee2018-01-02 10:41:11 +0800268 { USB_DEVICE(0x0cf3, 0xe010), .driver_info = BTUSB_QCA_ROME },
Marcel Holtmannc9e44472015-03-16 23:56:04 -0700269 { USB_DEVICE(0x0cf3, 0xe300), .driver_info = BTUSB_QCA_ROME },
Gabriel1eef1c32017-02-20 12:32:22 -0500270 { USB_DEVICE(0x0cf3, 0xe301), .driver_info = BTUSB_QCA_ROME },
Marcel Holtmannc9e44472015-03-16 23:56:04 -0700271 { USB_DEVICE(0x0cf3, 0xe360), .driver_info = BTUSB_QCA_ROME },
Yvain THONNART47ca5892016-06-11 15:43:30 +0200272 { USB_DEVICE(0x0489, 0xe092), .driver_info = BTUSB_QCA_ROME },
Bartosz Chronowski858ff382017-10-26 10:22:43 +0200273 { USB_DEVICE(0x0489, 0xe09f), .driver_info = BTUSB_QCA_ROME },
Shih-Yuan Lee (FourDollars)06e41d82017-06-14 14:42:00 +0800274 { USB_DEVICE(0x0489, 0xe0a2), .driver_info = BTUSB_QCA_ROME },
Dmitry Tunin1144a4e2016-09-21 19:13:08 +0300275 { USB_DEVICE(0x04ca, 0x3011), .driver_info = BTUSB_QCA_ROME },
Ioan Moldovan0a03f982017-08-28 18:09:39 +0300276 { USB_DEVICE(0x04ca, 0x3015), .driver_info = BTUSB_QCA_ROME },
Brian Norrisd829b9e2017-07-31 17:59:39 -0700277 { USB_DEVICE(0x04ca, 0x3016), .driver_info = BTUSB_QCA_ROME },
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +0000278
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200279 /* Broadcom BCM2035 */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100280 { USB_DEVICE(0x0a5c, 0x2009), .driver_info = BTUSB_BCM92035 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200281 { USB_DEVICE(0x0a5c, 0x200a), .driver_info = BTUSB_WRONG_SCO_MTU },
282 { USB_DEVICE(0x0a5c, 0x2035), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200283
284 /* Broadcom BCM2045 */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100285 { USB_DEVICE(0x0a5c, 0x2039), .driver_info = BTUSB_WRONG_SCO_MTU },
286 { USB_DEVICE(0x0a5c, 0x2101), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmannbdbef3d2008-09-23 00:16:35 +0200287
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200288 /* IBM/Lenovo ThinkPad with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100289 { USB_DEVICE(0x0a5c, 0x201e), .driver_info = BTUSB_WRONG_SCO_MTU },
290 { USB_DEVICE(0x0a5c, 0x2110), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200291
292 /* HP laptop with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100293 { USB_DEVICE(0x03f0, 0x171d), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200294
295 /* Dell laptop with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100296 { USB_DEVICE(0x413c, 0x8126), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200297
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100298 /* Dell Wireless 370 and 410 devices */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100299 { USB_DEVICE(0x413c, 0x8152), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100300 { USB_DEVICE(0x413c, 0x8156), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200301
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100302 /* Belkin F8T012 and F8T013 devices */
303 { USB_DEVICE(0x050d, 0x0012), .driver_info = BTUSB_WRONG_SCO_MTU },
304 { USB_DEVICE(0x050d, 0x0013), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200305
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100306 /* Asus WL-BTD202 device */
307 { USB_DEVICE(0x0b05, 0x1715), .driver_info = BTUSB_WRONG_SCO_MTU },
308
309 /* Kensington Bluetooth USB adapter */
310 { USB_DEVICE(0x047d, 0x105e), .driver_info = BTUSB_WRONG_SCO_MTU },
311
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200312 /* RTX Telecom based adapters with buggy SCO support */
313 { USB_DEVICE(0x0400, 0x0807), .driver_info = BTUSB_BROKEN_ISOC },
314 { USB_DEVICE(0x0400, 0x080a), .driver_info = BTUSB_BROKEN_ISOC },
315
316 /* CONWISE Technology based adapters with buggy SCO support */
Szymon Janc418678b2016-09-01 17:22:37 +0200317 { USB_DEVICE(0x0e5e, 0x6622),
318 .driver_info = BTUSB_BROKEN_ISOC | BTUSB_CW6622},
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200319
Marcel Holtmann4fcef8e2015-01-01 17:34:37 -0800320 /* Roper Class 1 Bluetooth Dongle (Silicon Wave based) */
Aleksei Volkov2eeac872015-06-08 12:02:10 +0300321 { USB_DEVICE(0x1310, 0x0001), .driver_info = BTUSB_SWAVE },
Marcel Holtmann4fcef8e2015-01-01 17:34:37 -0800322
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200323 /* Digianswer devices */
324 { USB_DEVICE(0x08fd, 0x0001), .driver_info = BTUSB_DIGIANSWER },
325 { USB_DEVICE(0x08fd, 0x0002), .driver_info = BTUSB_IGNORE },
326
327 /* CSR BlueCore Bluetooth Sniffer */
Marcel Holtmann4f64fa82014-07-07 00:12:04 +0200328 { USB_DEVICE(0x0a12, 0x0002),
329 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200330
331 /* Frontline ComProbe Bluetooth Sniffer */
Marcel Holtmann4f64fa82014-07-07 00:12:04 +0200332 { USB_DEVICE(0x16d3, 0x0002),
333 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200334
Marcel Holtmanncb1ee892015-01-28 19:41:42 -0800335 /* Marvell Bluetooth devices */
336 { USB_DEVICE(0x1286, 0x2044), .driver_info = BTUSB_MARVELL },
337 { USB_DEVICE(0x1286, 0x2046), .driver_info = BTUSB_MARVELL },
Amitkumar Karwar1165df02016-09-28 16:18:35 +0530338 { USB_DEVICE(0x1286, 0x204e), .driver_info = BTUSB_MARVELL },
Marcel Holtmanncb1ee892015-01-28 19:41:42 -0800339
Marcel Holtmannd0ac9eb2015-01-28 19:41:43 -0800340 /* Intel Bluetooth devices */
Marcel Holtmannde766142017-03-06 15:38:28 -0800341 { USB_DEVICE(0x8087, 0x0025), .driver_info = BTUSB_INTEL_NEW },
Tedd Ho-Jeong An1ce0cec2018-02-05 14:20:36 -0800342 { USB_DEVICE(0x8087, 0x0026), .driver_info = BTUSB_INTEL_NEW },
Marcel Holtmann407550f2015-02-22 15:41:18 -0800343 { USB_DEVICE(0x8087, 0x07da), .driver_info = BTUSB_CSR },
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -0700344 { USB_DEVICE(0x8087, 0x07dc), .driver_info = BTUSB_INTEL },
Tedd Ho-Jeong Anef4e5e42013-11-12 13:10:58 -0800345 { USB_DEVICE(0x8087, 0x0a2a), .driver_info = BTUSB_INTEL },
Marcel Holtmanncda0dd72015-01-26 21:33:48 -0800346 { USB_DEVICE(0x8087, 0x0a2b), .driver_info = BTUSB_INTEL_NEW },
Tedd Ho-Jeong An439e65d2016-06-20 13:43:40 -0700347 { USB_DEVICE(0x8087, 0x0aa7), .driver_info = BTUSB_INTEL },
Tedd Ho-Jeong An86a61292017-05-01 13:35:12 -0700348 { USB_DEVICE(0x8087, 0x0aaa), .driver_info = BTUSB_INTEL_NEW },
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -0700349
Marcel Holtmannd0ac9eb2015-01-28 19:41:43 -0800350 /* Other Intel Bluetooth devices */
351 { USB_VENDOR_AND_INTERFACE_INFO(0x8087, 0xe0, 0x01, 0x01),
352 .driver_info = BTUSB_IGNORE },
Amitkumar Karwarae8df492014-07-18 14:47:06 -0700353
Daniel Drakea2698a92015-04-16 14:09:55 -0600354 /* Realtek Bluetooth devices */
355 { USB_VENDOR_AND_INTERFACE_INFO(0x0bda, 0xe0, 0x01, 0x01),
356 .driver_info = BTUSB_REALTEK },
357
358 /* Additional Realtek 8723AE Bluetooth devices */
359 { USB_DEVICE(0x0930, 0x021d), .driver_info = BTUSB_REALTEK },
360 { USB_DEVICE(0x13d3, 0x3394), .driver_info = BTUSB_REALTEK },
361
362 /* Additional Realtek 8723BE Bluetooth devices */
363 { USB_DEVICE(0x0489, 0xe085), .driver_info = BTUSB_REALTEK },
364 { USB_DEVICE(0x0489, 0xe08b), .driver_info = BTUSB_REALTEK },
365 { USB_DEVICE(0x13d3, 0x3410), .driver_info = BTUSB_REALTEK },
366 { USB_DEVICE(0x13d3, 0x3416), .driver_info = BTUSB_REALTEK },
367 { USB_DEVICE(0x13d3, 0x3459), .driver_info = BTUSB_REALTEK },
Dmitry Tunina81d72d2017-08-08 14:09:02 +0300368 { USB_DEVICE(0x13d3, 0x3494), .driver_info = BTUSB_REALTEK },
Daniel Drakea2698a92015-04-16 14:09:55 -0600369
370 /* Additional Realtek 8821AE Bluetooth devices */
371 { USB_DEVICE(0x0b05, 0x17dc), .driver_info = BTUSB_REALTEK },
372 { USB_DEVICE(0x13d3, 0x3414), .driver_info = BTUSB_REALTEK },
373 { USB_DEVICE(0x13d3, 0x3458), .driver_info = BTUSB_REALTEK },
374 { USB_DEVICE(0x13d3, 0x3461), .driver_info = BTUSB_REALTEK },
375 { USB_DEVICE(0x13d3, 0x3462), .driver_info = BTUSB_REALTEK },
376
Larry Fingerfed03fe2018-02-11 12:24:32 -0600377 /* Additional Realtek 8822BE Bluetooth devices */
378 { USB_DEVICE(0x0b05, 0x185c), .driver_info = BTUSB_REALTEK },
379
Peter Poklop4481c072015-08-15 20:47:09 +0200380 /* Silicon Wave based devices */
381 { USB_DEVICE(0x0c10, 0x0000), .driver_info = BTUSB_SWAVE },
382
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200383 { } /* Terminating entry */
384};
385
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200386#define BTUSB_MAX_ISOC_FRAMES 10
387
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200388#define BTUSB_INTR_RUNNING 0
389#define BTUSB_BULK_RUNNING 1
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200390#define BTUSB_ISOC_RUNNING 2
Oliver Neukum7bee5492009-08-24 23:44:59 +0200391#define BTUSB_SUSPENDING 3
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -0300392#define BTUSB_DID_ISO_RESUME 4
Marcel Holtmanncda0dd72015-01-26 21:33:48 -0800393#define BTUSB_BOOTLOADER 5
394#define BTUSB_DOWNLOADING 6
Marcel Holtmannce6bb922015-01-28 01:58:40 -0800395#define BTUSB_FIRMWARE_LOADED 7
Marcel Holtmanncda0dd72015-01-26 21:33:48 -0800396#define BTUSB_FIRMWARE_FAILED 8
Marcel Holtmannce6bb922015-01-28 01:58:40 -0800397#define BTUSB_BOOTING 9
Hans de Goede61f5ace2018-01-08 10:44:16 +0100398#define BTUSB_DIAG_RUNNING 10
399#define BTUSB_OOB_WAKE_ENABLED 11
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200400
401struct btusb_data {
402 struct hci_dev *hdev;
403 struct usb_device *udev;
Marcel Holtmann5fbcd262008-09-23 00:16:36 +0200404 struct usb_interface *intf;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200405 struct usb_interface *isoc;
Marcel Holtmann9d08f502015-10-08 20:23:08 +0200406 struct usb_interface *diag;
Marcel Holtmann459232f2017-10-24 19:42:45 +0200407 unsigned isoc_ifnum;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200408
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200409 unsigned long flags;
410
411 struct work_struct work;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200412 struct work_struct waker;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200413
Marcel Holtmann803b5832014-09-16 08:00:29 +0200414 struct usb_anchor deferred;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200415 struct usb_anchor tx_anchor;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200416 int tx_in_flight;
417 spinlock_t txlock;
418
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200419 struct usb_anchor intr_anchor;
420 struct usb_anchor bulk_anchor;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200421 struct usb_anchor isoc_anchor;
Marcel Holtmann9d08f502015-10-08 20:23:08 +0200422 struct usb_anchor diag_anchor;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200423 spinlock_t rxlock;
424
425 struct sk_buff *evt_skb;
426 struct sk_buff *acl_skb;
427 struct sk_buff *sco_skb;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200428
429 struct usb_endpoint_descriptor *intr_ep;
430 struct usb_endpoint_descriptor *bulk_tx_ep;
431 struct usb_endpoint_descriptor *bulk_rx_ep;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200432 struct usb_endpoint_descriptor *isoc_tx_ep;
433 struct usb_endpoint_descriptor *isoc_rx_ep;
Marcel Holtmann9d08f502015-10-08 20:23:08 +0200434 struct usb_endpoint_descriptor *diag_tx_ep;
435 struct usb_endpoint_descriptor *diag_rx_ep;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200436
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100437 __u8 cmdreq_type;
Marcel Holtmann893ba542015-01-28 20:27:34 -0800438 __u8 cmdreq;
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100439
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100440 unsigned int sco_num;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200441 int isoc_altsetting;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +0100442 int suspend_count;
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +0100443
Marcel Holtmann97307f52015-01-12 13:51:10 -0800444 int (*recv_event)(struct hci_dev *hdev, struct sk_buff *skb);
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +0100445 int (*recv_bulk)(struct btusb_data *data, void *buffer, int count);
Kim, Ben Young Taeace31982015-02-15 23:06:14 +0000446
447 int (*setup_on_usb)(struct hci_dev *hdev);
Rajat Jainfd913ef2017-02-01 14:24:09 -0800448
449 int oob_wake_irq; /* irq for out-of-band wake-on-bt */
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200450};
451
Marcel Holtmann803b5832014-09-16 08:00:29 +0200452static inline void btusb_free_frags(struct btusb_data *data)
453{
454 unsigned long flags;
455
456 spin_lock_irqsave(&data->rxlock, flags);
457
458 kfree_skb(data->evt_skb);
459 data->evt_skb = NULL;
460
461 kfree_skb(data->acl_skb);
462 data->acl_skb = NULL;
463
464 kfree_skb(data->sco_skb);
465 data->sco_skb = NULL;
466
467 spin_unlock_irqrestore(&data->rxlock, flags);
468}
469
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200470static int btusb_recv_intr(struct btusb_data *data, void *buffer, int count)
471{
Marcel Holtmann803b5832014-09-16 08:00:29 +0200472 struct sk_buff *skb;
473 int err = 0;
474
475 spin_lock(&data->rxlock);
476 skb = data->evt_skb;
477
478 while (count) {
479 int len;
480
481 if (!skb) {
482 skb = bt_skb_alloc(HCI_MAX_EVENT_SIZE, GFP_ATOMIC);
483 if (!skb) {
484 err = -ENOMEM;
485 break;
486 }
487
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100488 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
489 hci_skb_expect(skb) = HCI_EVENT_HDR_SIZE;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200490 }
491
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100492 len = min_t(uint, hci_skb_expect(skb), count);
Johannes Berg59ae1d12017-06-16 14:29:20 +0200493 skb_put_data(skb, buffer, len);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200494
495 count -= len;
496 buffer += len;
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100497 hci_skb_expect(skb) -= len;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200498
499 if (skb->len == HCI_EVENT_HDR_SIZE) {
500 /* Complete event header */
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100501 hci_skb_expect(skb) = hci_event_hdr(skb)->plen;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200502
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100503 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
Marcel Holtmann803b5832014-09-16 08:00:29 +0200504 kfree_skb(skb);
505 skb = NULL;
506
507 err = -EILSEQ;
508 break;
509 }
510 }
511
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100512 if (!hci_skb_expect(skb)) {
Marcel Holtmann803b5832014-09-16 08:00:29 +0200513 /* Complete frame */
Marcel Holtmann97307f52015-01-12 13:51:10 -0800514 data->recv_event(data->hdev, skb);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200515 skb = NULL;
516 }
517 }
518
519 data->evt_skb = skb;
520 spin_unlock(&data->rxlock);
521
522 return err;
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200523}
524
525static int btusb_recv_bulk(struct btusb_data *data, void *buffer, int count)
526{
Marcel Holtmann803b5832014-09-16 08:00:29 +0200527 struct sk_buff *skb;
528 int err = 0;
529
530 spin_lock(&data->rxlock);
531 skb = data->acl_skb;
532
533 while (count) {
534 int len;
535
536 if (!skb) {
537 skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
538 if (!skb) {
539 err = -ENOMEM;
540 break;
541 }
542
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100543 hci_skb_pkt_type(skb) = HCI_ACLDATA_PKT;
544 hci_skb_expect(skb) = HCI_ACL_HDR_SIZE;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200545 }
546
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100547 len = min_t(uint, hci_skb_expect(skb), count);
Johannes Berg59ae1d12017-06-16 14:29:20 +0200548 skb_put_data(skb, buffer, len);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200549
550 count -= len;
551 buffer += len;
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100552 hci_skb_expect(skb) -= len;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200553
554 if (skb->len == HCI_ACL_HDR_SIZE) {
555 __le16 dlen = hci_acl_hdr(skb)->dlen;
556
557 /* Complete ACL header */
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100558 hci_skb_expect(skb) = __le16_to_cpu(dlen);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200559
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100560 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
Marcel Holtmann803b5832014-09-16 08:00:29 +0200561 kfree_skb(skb);
562 skb = NULL;
563
564 err = -EILSEQ;
565 break;
566 }
567 }
568
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100569 if (!hci_skb_expect(skb)) {
Marcel Holtmann803b5832014-09-16 08:00:29 +0200570 /* Complete frame */
571 hci_recv_frame(data->hdev, skb);
572 skb = NULL;
573 }
574 }
575
576 data->acl_skb = skb;
577 spin_unlock(&data->rxlock);
578
579 return err;
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200580}
581
582static int btusb_recv_isoc(struct btusb_data *data, void *buffer, int count)
583{
Marcel Holtmann803b5832014-09-16 08:00:29 +0200584 struct sk_buff *skb;
585 int err = 0;
586
587 spin_lock(&data->rxlock);
588 skb = data->sco_skb;
589
590 while (count) {
591 int len;
592
593 if (!skb) {
594 skb = bt_skb_alloc(HCI_MAX_SCO_SIZE, GFP_ATOMIC);
595 if (!skb) {
596 err = -ENOMEM;
597 break;
598 }
599
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100600 hci_skb_pkt_type(skb) = HCI_SCODATA_PKT;
601 hci_skb_expect(skb) = HCI_SCO_HDR_SIZE;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200602 }
603
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100604 len = min_t(uint, hci_skb_expect(skb), count);
Johannes Berg59ae1d12017-06-16 14:29:20 +0200605 skb_put_data(skb, buffer, len);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200606
607 count -= len;
608 buffer += len;
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100609 hci_skb_expect(skb) -= len;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200610
611 if (skb->len == HCI_SCO_HDR_SIZE) {
612 /* Complete SCO header */
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100613 hci_skb_expect(skb) = hci_sco_hdr(skb)->dlen;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200614
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100615 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
Marcel Holtmann803b5832014-09-16 08:00:29 +0200616 kfree_skb(skb);
617 skb = NULL;
618
619 err = -EILSEQ;
620 break;
621 }
622 }
623
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100624 if (!hci_skb_expect(skb)) {
Marcel Holtmann803b5832014-09-16 08:00:29 +0200625 /* Complete frame */
626 hci_recv_frame(data->hdev, skb);
627 skb = NULL;
628 }
629 }
630
631 data->sco_skb = skb;
632 spin_unlock(&data->rxlock);
633
634 return err;
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200635}
636
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200637static void btusb_intr_complete(struct urb *urb)
638{
639 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +0100640 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200641 int err;
642
Marcel Holtmann89e75332014-09-16 04:44:50 +0200643 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
644 urb->actual_length);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200645
646 if (!test_bit(HCI_RUNNING, &hdev->flags))
647 return;
648
649 if (urb->status == 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200650 hdev->stat.byte_rx += urb->actual_length;
651
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200652 if (btusb_recv_intr(data, urb->transfer_buffer,
653 urb->actual_length) < 0) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +0100654 bt_dev_err(hdev, "corrupted event packet");
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200655 hdev->stat.err_rx++;
656 }
Champion Chen85560c42014-09-06 14:06:08 -0500657 } else if (urb->status == -ENOENT) {
658 /* Avoid suspend failed when usb_kill_urb */
659 return;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200660 }
661
662 if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
663 return;
664
Oliver Neukum7bee5492009-08-24 23:44:59 +0200665 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200666 usb_anchor_urb(urb, &data->intr_anchor);
667
668 err = usb_submit_urb(urb, GFP_ATOMIC);
669 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200670 /* -EPERM: urb is being killed;
Derek Robsond98422c2017-07-22 13:47:07 +1200671 * -ENODEV: device got disconnected
672 */
Paul Bolle4935f1c2011-08-09 17:16:28 +0200673 if (err != -EPERM && err != -ENODEV)
Marcel Holtmann2064ee32017-10-30 10:42:59 +0100674 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
675 urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200676 usb_unanchor_urb(urb);
677 }
678}
679
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100680static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200681{
David Herrmann155961e2012-02-09 21:58:32 +0100682 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200683 struct urb *urb;
684 unsigned char *buf;
685 unsigned int pipe;
686 int err, size;
687
688 BT_DBG("%s", hdev->name);
689
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200690 if (!data->intr_ep)
691 return -ENODEV;
692
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100693 urb = usb_alloc_urb(0, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200694 if (!urb)
695 return -ENOMEM;
696
697 size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
698
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100699 buf = kmalloc(size, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200700 if (!buf) {
701 usb_free_urb(urb);
702 return -ENOMEM;
703 }
704
705 pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
706
707 usb_fill_int_urb(urb, data->udev, pipe, buf, size,
Marcel Holtmann89e75332014-09-16 04:44:50 +0200708 btusb_intr_complete, hdev, data->intr_ep->bInterval);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200709
710 urb->transfer_flags |= URB_FREE_BUFFER;
711
712 usb_anchor_urb(urb, &data->intr_anchor);
713
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100714 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200715 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +0200716 if (err != -EPERM && err != -ENODEV)
Marcel Holtmann2064ee32017-10-30 10:42:59 +0100717 bt_dev_err(hdev, "urb %p submission failed (%d)",
718 urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200719 usb_unanchor_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200720 }
721
722 usb_free_urb(urb);
723
724 return err;
725}
726
727static void btusb_bulk_complete(struct urb *urb)
728{
729 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +0100730 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200731 int err;
732
Marcel Holtmann89e75332014-09-16 04:44:50 +0200733 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
734 urb->actual_length);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200735
736 if (!test_bit(HCI_RUNNING, &hdev->flags))
737 return;
738
739 if (urb->status == 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200740 hdev->stat.byte_rx += urb->actual_length;
741
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +0100742 if (data->recv_bulk(data, urb->transfer_buffer,
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200743 urb->actual_length) < 0) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +0100744 bt_dev_err(hdev, "corrupted ACL packet");
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200745 hdev->stat.err_rx++;
746 }
Champion Chen85560c42014-09-06 14:06:08 -0500747 } else if (urb->status == -ENOENT) {
748 /* Avoid suspend failed when usb_kill_urb */
749 return;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200750 }
751
752 if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
753 return;
754
755 usb_anchor_urb(urb, &data->bulk_anchor);
Oliver Neukum652fd782009-12-16 19:23:43 +0100756 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200757
758 err = usb_submit_urb(urb, GFP_ATOMIC);
759 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200760 /* -EPERM: urb is being killed;
Derek Robsond98422c2017-07-22 13:47:07 +1200761 * -ENODEV: device got disconnected
762 */
Paul Bolle4935f1c2011-08-09 17:16:28 +0200763 if (err != -EPERM && err != -ENODEV)
Marcel Holtmann2064ee32017-10-30 10:42:59 +0100764 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
765 urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200766 usb_unanchor_urb(urb);
767 }
768}
769
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100770static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200771{
David Herrmann155961e2012-02-09 21:58:32 +0100772 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200773 struct urb *urb;
774 unsigned char *buf;
775 unsigned int pipe;
Vikram Kandukuri290ba202009-07-02 14:31:59 +0530776 int err, size = HCI_MAX_FRAME_SIZE;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200777
778 BT_DBG("%s", hdev->name);
779
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200780 if (!data->bulk_rx_ep)
781 return -ENODEV;
782
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100783 urb = usb_alloc_urb(0, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200784 if (!urb)
785 return -ENOMEM;
786
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100787 buf = kmalloc(size, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200788 if (!buf) {
789 usb_free_urb(urb);
790 return -ENOMEM;
791 }
792
793 pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
794
Marcel Holtmann89e75332014-09-16 04:44:50 +0200795 usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
796 btusb_bulk_complete, hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200797
798 urb->transfer_flags |= URB_FREE_BUFFER;
799
Oliver Neukum7bee5492009-08-24 23:44:59 +0200800 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200801 usb_anchor_urb(urb, &data->bulk_anchor);
802
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100803 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200804 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +0200805 if (err != -EPERM && err != -ENODEV)
Marcel Holtmann2064ee32017-10-30 10:42:59 +0100806 bt_dev_err(hdev, "urb %p submission failed (%d)",
807 urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200808 usb_unanchor_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200809 }
810
811 usb_free_urb(urb);
812
813 return err;
814}
815
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200816static void btusb_isoc_complete(struct urb *urb)
817{
818 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +0100819 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200820 int i, err;
821
Marcel Holtmann89e75332014-09-16 04:44:50 +0200822 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
823 urb->actual_length);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200824
825 if (!test_bit(HCI_RUNNING, &hdev->flags))
826 return;
827
828 if (urb->status == 0) {
829 for (i = 0; i < urb->number_of_packets; i++) {
830 unsigned int offset = urb->iso_frame_desc[i].offset;
831 unsigned int length = urb->iso_frame_desc[i].actual_length;
832
833 if (urb->iso_frame_desc[i].status)
834 continue;
835
836 hdev->stat.byte_rx += length;
837
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200838 if (btusb_recv_isoc(data, urb->transfer_buffer + offset,
839 length) < 0) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +0100840 bt_dev_err(hdev, "corrupted SCO packet");
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200841 hdev->stat.err_rx++;
842 }
843 }
Champion Chen85560c42014-09-06 14:06:08 -0500844 } else if (urb->status == -ENOENT) {
845 /* Avoid suspend failed when usb_kill_urb */
846 return;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200847 }
848
849 if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags))
850 return;
851
852 usb_anchor_urb(urb, &data->isoc_anchor);
853
854 err = usb_submit_urb(urb, GFP_ATOMIC);
855 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200856 /* -EPERM: urb is being killed;
Derek Robsond98422c2017-07-22 13:47:07 +1200857 * -ENODEV: device got disconnected
858 */
Paul Bolle4935f1c2011-08-09 17:16:28 +0200859 if (err != -EPERM && err != -ENODEV)
Marcel Holtmann2064ee32017-10-30 10:42:59 +0100860 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
861 urb, -err);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200862 usb_unanchor_urb(urb);
863 }
864}
865
Jesper Juhl42b16b32011-01-17 00:09:38 +0100866static inline void __fill_isoc_descriptor(struct urb *urb, int len, int mtu)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200867{
868 int i, offset = 0;
869
870 BT_DBG("len %d mtu %d", len, mtu);
871
872 for (i = 0; i < BTUSB_MAX_ISOC_FRAMES && len >= mtu;
873 i++, offset += mtu, len -= mtu) {
874 urb->iso_frame_desc[i].offset = offset;
875 urb->iso_frame_desc[i].length = mtu;
876 }
877
878 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
879 urb->iso_frame_desc[i].offset = offset;
880 urb->iso_frame_desc[i].length = len;
881 i++;
882 }
883
884 urb->number_of_packets = i;
885}
886
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100887static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200888{
David Herrmann155961e2012-02-09 21:58:32 +0100889 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200890 struct urb *urb;
891 unsigned char *buf;
892 unsigned int pipe;
893 int err, size;
894
895 BT_DBG("%s", hdev->name);
896
897 if (!data->isoc_rx_ep)
898 return -ENODEV;
899
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100900 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200901 if (!urb)
902 return -ENOMEM;
903
904 size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) *
905 BTUSB_MAX_ISOC_FRAMES;
906
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100907 buf = kmalloc(size, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200908 if (!buf) {
909 usb_free_urb(urb);
910 return -ENOMEM;
911 }
912
913 pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress);
914
Bing Zhaofa0fb932011-12-20 18:19:00 -0800915 usb_fill_int_urb(urb, data->udev, pipe, buf, size, btusb_isoc_complete,
Marcel Holtmann89e75332014-09-16 04:44:50 +0200916 hdev, data->isoc_rx_ep->bInterval);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200917
Marcel Holtmann89e75332014-09-16 04:44:50 +0200918 urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200919
920 __fill_isoc_descriptor(urb, size,
Marcel Holtmann89e75332014-09-16 04:44:50 +0200921 le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize));
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200922
923 usb_anchor_urb(urb, &data->isoc_anchor);
924
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100925 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200926 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +0200927 if (err != -EPERM && err != -ENODEV)
Marcel Holtmann2064ee32017-10-30 10:42:59 +0100928 bt_dev_err(hdev, "urb %p submission failed (%d)",
929 urb, -err);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200930 usb_unanchor_urb(urb);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200931 }
932
933 usb_free_urb(urb);
934
935 return err;
936}
937
Marcel Holtmann9d08f502015-10-08 20:23:08 +0200938static void btusb_diag_complete(struct urb *urb)
939{
940 struct hci_dev *hdev = urb->context;
941 struct btusb_data *data = hci_get_drvdata(hdev);
942 int err;
943
944 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
945 urb->actual_length);
946
947 if (urb->status == 0) {
948 struct sk_buff *skb;
949
950 skb = bt_skb_alloc(urb->actual_length, GFP_ATOMIC);
951 if (skb) {
Johannes Berg59ae1d12017-06-16 14:29:20 +0200952 skb_put_data(skb, urb->transfer_buffer,
953 urb->actual_length);
Marcel Holtmann9d08f502015-10-08 20:23:08 +0200954 hci_recv_diag(hdev, skb);
955 }
956 } else if (urb->status == -ENOENT) {
957 /* Avoid suspend failed when usb_kill_urb */
958 return;
959 }
960
961 if (!test_bit(BTUSB_DIAG_RUNNING, &data->flags))
962 return;
963
964 usb_anchor_urb(urb, &data->diag_anchor);
965 usb_mark_last_busy(data->udev);
966
967 err = usb_submit_urb(urb, GFP_ATOMIC);
968 if (err < 0) {
969 /* -EPERM: urb is being killed;
Derek Robsond98422c2017-07-22 13:47:07 +1200970 * -ENODEV: device got disconnected
971 */
Marcel Holtmann9d08f502015-10-08 20:23:08 +0200972 if (err != -EPERM && err != -ENODEV)
Marcel Holtmann2064ee32017-10-30 10:42:59 +0100973 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
974 urb, -err);
Marcel Holtmann9d08f502015-10-08 20:23:08 +0200975 usb_unanchor_urb(urb);
976 }
977}
978
979static int btusb_submit_diag_urb(struct hci_dev *hdev, gfp_t mem_flags)
980{
981 struct btusb_data *data = hci_get_drvdata(hdev);
982 struct urb *urb;
983 unsigned char *buf;
984 unsigned int pipe;
985 int err, size = HCI_MAX_FRAME_SIZE;
986
987 BT_DBG("%s", hdev->name);
988
989 if (!data->diag_rx_ep)
990 return -ENODEV;
991
992 urb = usb_alloc_urb(0, mem_flags);
993 if (!urb)
994 return -ENOMEM;
995
996 buf = kmalloc(size, mem_flags);
997 if (!buf) {
998 usb_free_urb(urb);
999 return -ENOMEM;
1000 }
1001
1002 pipe = usb_rcvbulkpipe(data->udev, data->diag_rx_ep->bEndpointAddress);
1003
1004 usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
1005 btusb_diag_complete, hdev);
1006
1007 urb->transfer_flags |= URB_FREE_BUFFER;
1008
1009 usb_mark_last_busy(data->udev);
1010 usb_anchor_urb(urb, &data->diag_anchor);
1011
1012 err = usb_submit_urb(urb, mem_flags);
1013 if (err < 0) {
1014 if (err != -EPERM && err != -ENODEV)
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001015 bt_dev_err(hdev, "urb %p submission failed (%d)",
1016 urb, -err);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02001017 usb_unanchor_urb(urb);
1018 }
1019
1020 usb_free_urb(urb);
1021
1022 return err;
1023}
1024
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001025static void btusb_tx_complete(struct urb *urb)
1026{
1027 struct sk_buff *skb = urb->context;
Marcel Holtmann89e75332014-09-16 04:44:50 +02001028 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
David Herrmann155961e2012-02-09 21:58:32 +01001029 struct btusb_data *data = hci_get_drvdata(hdev);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001030
Marcel Holtmann89e75332014-09-16 04:44:50 +02001031 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1032 urb->actual_length);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001033
1034 if (!test_bit(HCI_RUNNING, &hdev->flags))
1035 goto done;
1036
1037 if (!urb->status)
1038 hdev->stat.byte_tx += urb->transfer_buffer_length;
1039 else
1040 hdev->stat.err_tx++;
1041
1042done:
1043 spin_lock(&data->txlock);
1044 data->tx_in_flight--;
1045 spin_unlock(&data->txlock);
1046
1047 kfree(urb->setup_packet);
1048
1049 kfree_skb(skb);
1050}
1051
1052static void btusb_isoc_tx_complete(struct urb *urb)
1053{
1054 struct sk_buff *skb = urb->context;
Marcel Holtmann89e75332014-09-16 04:44:50 +02001055 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001056
Marcel Holtmann89e75332014-09-16 04:44:50 +02001057 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1058 urb->actual_length);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001059
1060 if (!test_bit(HCI_RUNNING, &hdev->flags))
1061 goto done;
1062
1063 if (!urb->status)
1064 hdev->stat.byte_tx += urb->transfer_buffer_length;
1065 else
1066 hdev->stat.err_tx++;
1067
1068done:
1069 kfree(urb->setup_packet);
1070
1071 kfree_skb(skb);
1072}
1073
1074static int btusb_open(struct hci_dev *hdev)
1075{
David Herrmann155961e2012-02-09 21:58:32 +01001076 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001077 int err;
1078
1079 BT_DBG("%s", hdev->name);
1080
Ethan Hsiehc7e163f2016-10-07 12:06:42 +08001081 err = usb_autopm_get_interface(data->intf);
1082 if (err < 0)
1083 return err;
1084
Kim, Ben Young Taeace31982015-02-15 23:06:14 +00001085 /* Patching USB firmware files prior to starting any URBs of HCI path
1086 * It is more safe to use USB bulk channel for downloading USB patch
1087 */
1088 if (data->setup_on_usb) {
1089 err = data->setup_on_usb(hdev);
Marcel Holtmanneb500422015-04-16 23:15:50 +02001090 if (err < 0)
Kim, Ben Young Taeace31982015-02-15 23:06:14 +00001091 return err;
1092 }
1093
Oliver Neukum7bee5492009-08-24 23:44:59 +02001094 data->intf->needs_remote_wakeup = 1;
Sukumar Ghoraia0085f22017-08-16 14:46:55 -07001095 /* device specific wakeup source enabled and required for USB
1096 * remote wakeup while host is suspended
1097 */
1098 device_wakeup_enable(&data->udev->dev);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001099
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001100 if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +02001101 goto done;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001102
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001103 err = btusb_submit_intr_urb(hdev, GFP_KERNEL);
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001104 if (err < 0)
1105 goto failed;
1106
1107 err = btusb_submit_bulk_urb(hdev, GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001108 if (err < 0) {
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001109 usb_kill_anchored_urbs(&data->intr_anchor);
1110 goto failed;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001111 }
1112
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001113 set_bit(BTUSB_BULK_RUNNING, &data->flags);
1114 btusb_submit_bulk_urb(hdev, GFP_KERNEL);
1115
Marcel Holtmann9d08f502015-10-08 20:23:08 +02001116 if (data->diag) {
1117 if (!btusb_submit_diag_urb(hdev, GFP_KERNEL))
1118 set_bit(BTUSB_DIAG_RUNNING, &data->flags);
1119 }
1120
Oliver Neukum7bee5492009-08-24 23:44:59 +02001121done:
1122 usb_autopm_put_interface(data->intf);
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001123 return 0;
1124
1125failed:
1126 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001127 usb_autopm_put_interface(data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001128 return err;
1129}
1130
Oliver Neukum7bee5492009-08-24 23:44:59 +02001131static void btusb_stop_traffic(struct btusb_data *data)
1132{
1133 usb_kill_anchored_urbs(&data->intr_anchor);
1134 usb_kill_anchored_urbs(&data->bulk_anchor);
1135 usb_kill_anchored_urbs(&data->isoc_anchor);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02001136 usb_kill_anchored_urbs(&data->diag_anchor);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001137}
1138
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001139static int btusb_close(struct hci_dev *hdev)
1140{
David Herrmann155961e2012-02-09 21:58:32 +01001141 struct btusb_data *data = hci_get_drvdata(hdev);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001142 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001143
1144 BT_DBG("%s", hdev->name);
1145
Marcel Holtmanne8c3c3d2008-09-23 00:16:36 +02001146 cancel_work_sync(&data->work);
Linus Torvalds404291a2009-11-11 13:32:29 -08001147 cancel_work_sync(&data->waker);
Marcel Holtmanne8c3c3d2008-09-23 00:16:36 +02001148
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001149 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001150 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001151 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02001152 clear_bit(BTUSB_DIAG_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001153
1154 btusb_stop_traffic(data);
Marcel Holtmann803b5832014-09-16 08:00:29 +02001155 btusb_free_frags(data);
1156
Oliver Neukum7bee5492009-08-24 23:44:59 +02001157 err = usb_autopm_get_interface(data->intf);
1158 if (err < 0)
Oliver Neukum7b8e2c12009-11-13 14:26:23 +01001159 goto failed;
Oliver Neukum7bee5492009-08-24 23:44:59 +02001160
1161 data->intf->needs_remote_wakeup = 0;
Sukumar Ghoraia0085f22017-08-16 14:46:55 -07001162 device_wakeup_disable(&data->udev->dev);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001163 usb_autopm_put_interface(data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001164
Oliver Neukum7b8e2c12009-11-13 14:26:23 +01001165failed:
1166 usb_scuttle_anchored_urbs(&data->deferred);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001167 return 0;
1168}
1169
1170static int btusb_flush(struct hci_dev *hdev)
1171{
David Herrmann155961e2012-02-09 21:58:32 +01001172 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001173
1174 BT_DBG("%s", hdev->name);
1175
1176 usb_kill_anchored_urbs(&data->tx_anchor);
Marcel Holtmann803b5832014-09-16 08:00:29 +02001177 btusb_free_frags(data);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001178
1179 return 0;
1180}
1181
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001182static struct urb *alloc_ctrl_urb(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001183{
David Herrmann155961e2012-02-09 21:58:32 +01001184 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001185 struct usb_ctrlrequest *dr;
1186 struct urb *urb;
1187 unsigned int pipe;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001188
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001189 urb = usb_alloc_urb(0, GFP_KERNEL);
1190 if (!urb)
1191 return ERR_PTR(-ENOMEM);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001192
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001193 dr = kmalloc(sizeof(*dr), GFP_KERNEL);
1194 if (!dr) {
1195 usb_free_urb(urb);
1196 return ERR_PTR(-ENOMEM);
1197 }
1198
1199 dr->bRequestType = data->cmdreq_type;
Marcel Holtmann893ba542015-01-28 20:27:34 -08001200 dr->bRequest = data->cmdreq;
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001201 dr->wIndex = 0;
1202 dr->wValue = 0;
1203 dr->wLength = __cpu_to_le16(skb->len);
1204
1205 pipe = usb_sndctrlpipe(data->udev, 0x00);
1206
Marcel Holtmann89e75332014-09-16 04:44:50 +02001207 usb_fill_control_urb(urb, data->udev, pipe, (void *)dr,
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001208 skb->data, skb->len, btusb_tx_complete, skb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001209
Marcel Holtmann89e75332014-09-16 04:44:50 +02001210 skb->dev = (void *)hdev;
Marcel Holtmann7bd8f092013-10-11 06:19:18 -07001211
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001212 return urb;
1213}
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001214
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001215static struct urb *alloc_bulk_urb(struct hci_dev *hdev, struct sk_buff *skb)
1216{
1217 struct btusb_data *data = hci_get_drvdata(hdev);
1218 struct urb *urb;
1219 unsigned int pipe;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001220
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001221 if (!data->bulk_tx_ep)
1222 return ERR_PTR(-ENODEV);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001223
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001224 urb = usb_alloc_urb(0, GFP_KERNEL);
1225 if (!urb)
1226 return ERR_PTR(-ENOMEM);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001227
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001228 pipe = usb_sndbulkpipe(data->udev, data->bulk_tx_ep->bEndpointAddress);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001229
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001230 usb_fill_bulk_urb(urb, data->udev, pipe,
1231 skb->data, skb->len, btusb_tx_complete, skb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001232
Marcel Holtmann89e75332014-09-16 04:44:50 +02001233 skb->dev = (void *)hdev;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001234
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001235 return urb;
1236}
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001237
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001238static struct urb *alloc_isoc_urb(struct hci_dev *hdev, struct sk_buff *skb)
1239{
1240 struct btusb_data *data = hci_get_drvdata(hdev);
1241 struct urb *urb;
1242 unsigned int pipe;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001243
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001244 if (!data->isoc_tx_ep)
1245 return ERR_PTR(-ENODEV);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001246
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001247 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_KERNEL);
1248 if (!urb)
1249 return ERR_PTR(-ENOMEM);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001250
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001251 pipe = usb_sndisocpipe(data->udev, data->isoc_tx_ep->bEndpointAddress);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001252
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001253 usb_fill_int_urb(urb, data->udev, pipe,
1254 skb->data, skb->len, btusb_isoc_tx_complete,
1255 skb, data->isoc_tx_ep->bInterval);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001256
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001257 urb->transfer_flags = URB_ISO_ASAP;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001258
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001259 __fill_isoc_descriptor(urb, skb->len,
1260 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001261
Marcel Holtmann89e75332014-09-16 04:44:50 +02001262 skb->dev = (void *)hdev;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001263
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001264 return urb;
1265}
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001266
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001267static int submit_tx_urb(struct hci_dev *hdev, struct urb *urb)
1268{
1269 struct btusb_data *data = hci_get_drvdata(hdev);
1270 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001271
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001272 usb_anchor_urb(urb, &data->tx_anchor);
1273
Johan Hedberge9753ef2014-09-14 08:49:34 +03001274 err = usb_submit_urb(urb, GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001275 if (err < 0) {
Paul Bolle5a9b80e2011-10-09 12:12:16 +02001276 if (err != -EPERM && err != -ENODEV)
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001277 bt_dev_err(hdev, "urb %p submission failed (%d)",
1278 urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001279 kfree(urb->setup_packet);
1280 usb_unanchor_urb(urb);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001281 } else {
1282 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001283 }
1284
Cong Wang54a8a792011-11-22 09:32:57 +08001285 usb_free_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001286 return err;
1287}
1288
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001289static int submit_or_queue_tx_urb(struct hci_dev *hdev, struct urb *urb)
1290{
1291 struct btusb_data *data = hci_get_drvdata(hdev);
1292 unsigned long flags;
1293 bool suspending;
1294
1295 spin_lock_irqsave(&data->txlock, flags);
1296 suspending = test_bit(BTUSB_SUSPENDING, &data->flags);
1297 if (!suspending)
1298 data->tx_in_flight++;
1299 spin_unlock_irqrestore(&data->txlock, flags);
1300
1301 if (!suspending)
1302 return submit_tx_urb(hdev, urb);
1303
1304 usb_anchor_urb(urb, &data->deferred);
1305 schedule_work(&data->waker);
1306
1307 usb_free_urb(urb);
1308 return 0;
1309}
1310
1311static int btusb_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
1312{
1313 struct urb *urb;
1314
1315 BT_DBG("%s", hdev->name);
1316
Marcel Holtmann618e8bc2015-11-05 07:33:56 +01001317 switch (hci_skb_pkt_type(skb)) {
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001318 case HCI_COMMAND_PKT:
1319 urb = alloc_ctrl_urb(hdev, skb);
1320 if (IS_ERR(urb))
1321 return PTR_ERR(urb);
1322
1323 hdev->stat.cmd_tx++;
1324 return submit_or_queue_tx_urb(hdev, urb);
1325
1326 case HCI_ACLDATA_PKT:
1327 urb = alloc_bulk_urb(hdev, skb);
1328 if (IS_ERR(urb))
1329 return PTR_ERR(urb);
1330
1331 hdev->stat.acl_tx++;
1332 return submit_or_queue_tx_urb(hdev, urb);
1333
1334 case HCI_SCODATA_PKT:
1335 if (hci_conn_num(hdev, SCO_LINK) < 1)
1336 return -ENODEV;
1337
1338 urb = alloc_isoc_urb(hdev, skb);
1339 if (IS_ERR(urb))
1340 return PTR_ERR(urb);
1341
1342 hdev->stat.sco_tx++;
1343 return submit_tx_urb(hdev, urb);
1344 }
1345
1346 return -EILSEQ;
1347}
1348
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001349static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
1350{
David Herrmann155961e2012-02-09 21:58:32 +01001351 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001352
1353 BT_DBG("%s evt %d", hdev->name, evt);
1354
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001355 if (hci_conn_num(hdev, SCO_LINK) != data->sco_num) {
1356 data->sco_num = hci_conn_num(hdev, SCO_LINK);
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001357 schedule_work(&data->work);
Marcel Holtmanna780efa2008-11-30 12:17:12 +01001358 }
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001359}
1360
Jesper Juhl42b16b32011-01-17 00:09:38 +01001361static inline int __set_isoc_interface(struct hci_dev *hdev, int altsetting)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001362{
David Herrmann155961e2012-02-09 21:58:32 +01001363 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001364 struct usb_interface *intf = data->isoc;
1365 struct usb_endpoint_descriptor *ep_desc;
1366 int i, err;
1367
1368 if (!data->isoc)
1369 return -ENODEV;
1370
Marcel Holtmann459232f2017-10-24 19:42:45 +02001371 err = usb_set_interface(data->udev, data->isoc_ifnum, altsetting);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001372 if (err < 0) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001373 bt_dev_err(hdev, "setting interface failed (%d)", -err);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001374 return err;
1375 }
1376
1377 data->isoc_altsetting = altsetting;
1378
1379 data->isoc_tx_ep = NULL;
1380 data->isoc_rx_ep = NULL;
1381
1382 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
1383 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
1384
1385 if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) {
1386 data->isoc_tx_ep = ep_desc;
1387 continue;
1388 }
1389
1390 if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) {
1391 data->isoc_rx_ep = ep_desc;
1392 continue;
1393 }
1394 }
1395
1396 if (!data->isoc_tx_ep || !data->isoc_rx_ep) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001397 bt_dev_err(hdev, "invalid SCO descriptors");
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001398 return -ENODEV;
1399 }
1400
1401 return 0;
1402}
1403
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001404static void btusb_work(struct work_struct *work)
1405{
1406 struct btusb_data *data = container_of(work, struct btusb_data, work);
1407 struct hci_dev *hdev = data->hdev;
Mikel Astizf4001d22012-04-11 08:48:51 +02001408 int new_alts;
Oliver Neukum7bee5492009-08-24 23:44:59 +02001409 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001410
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001411 if (data->sco_num > 0) {
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -03001412 if (!test_bit(BTUSB_DID_ISO_RESUME, &data->flags)) {
Oliver Neukum8efdd0c2011-02-11 13:00:06 +01001413 err = usb_autopm_get_interface(data->isoc ? data->isoc : data->intf);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001414 if (err < 0) {
1415 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1416 usb_kill_anchored_urbs(&data->isoc_anchor);
1417 return;
1418 }
1419
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -03001420 set_bit(BTUSB_DID_ISO_RESUME, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001421 }
Mikel Astizf4001d22012-04-11 08:48:51 +02001422
1423 if (hdev->voice_setting & 0x0020) {
1424 static const int alts[3] = { 2, 4, 5 };
Marcel Holtmann89e75332014-09-16 04:44:50 +02001425
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001426 new_alts = alts[data->sco_num - 1];
Mikel Astizf4001d22012-04-11 08:48:51 +02001427 } else {
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001428 new_alts = data->sco_num;
Mikel Astizf4001d22012-04-11 08:48:51 +02001429 }
1430
1431 if (data->isoc_altsetting != new_alts) {
Kuba Pawlakf6fc86f2015-10-28 15:18:05 +01001432 unsigned long flags;
1433
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001434 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1435 usb_kill_anchored_urbs(&data->isoc_anchor);
1436
Kuba Pawlak8f9d02f2015-09-10 17:07:00 +01001437 /* When isochronous alternate setting needs to be
1438 * changed, because SCO connection has been added
1439 * or removed, a packet fragment may be left in the
1440 * reassembling state. This could lead to wrongly
1441 * assembled fragments.
1442 *
1443 * Clear outstanding fragment when selecting a new
1444 * alternate setting.
1445 */
Kuba Pawlakf6fc86f2015-10-28 15:18:05 +01001446 spin_lock_irqsave(&data->rxlock, flags);
Kuba Pawlak8f9d02f2015-09-10 17:07:00 +01001447 kfree_skb(data->sco_skb);
1448 data->sco_skb = NULL;
Kuba Pawlakf6fc86f2015-10-28 15:18:05 +01001449 spin_unlock_irqrestore(&data->rxlock, flags);
Kuba Pawlak8f9d02f2015-09-10 17:07:00 +01001450
Mikel Astizf4001d22012-04-11 08:48:51 +02001451 if (__set_isoc_interface(hdev, new_alts) < 0)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001452 return;
1453 }
1454
1455 if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001456 if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001457 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1458 else
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001459 btusb_submit_isoc_urb(hdev, GFP_KERNEL);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001460 }
1461 } else {
1462 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1463 usb_kill_anchored_urbs(&data->isoc_anchor);
1464
1465 __set_isoc_interface(hdev, 0);
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -03001466 if (test_and_clear_bit(BTUSB_DID_ISO_RESUME, &data->flags))
Oliver Neukum8efdd0c2011-02-11 13:00:06 +01001467 usb_autopm_put_interface(data->isoc ? data->isoc : data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001468 }
1469}
1470
Oliver Neukum7bee5492009-08-24 23:44:59 +02001471static void btusb_waker(struct work_struct *work)
1472{
1473 struct btusb_data *data = container_of(work, struct btusb_data, waker);
1474 int err;
1475
1476 err = usb_autopm_get_interface(data->intf);
1477 if (err < 0)
1478 return;
1479
1480 usb_autopm_put_interface(data->intf);
1481}
1482
Marcel Holtmann9f8f9622013-04-10 08:11:35 -07001483static int btusb_setup_bcm92035(struct hci_dev *hdev)
1484{
1485 struct sk_buff *skb;
1486 u8 val = 0x00;
1487
1488 BT_DBG("%s", hdev->name);
1489
1490 skb = __hci_cmd_sync(hdev, 0xfc3b, 1, &val, HCI_INIT_TIMEOUT);
1491 if (IS_ERR(skb))
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001492 bt_dev_err(hdev, "BCM92035 command failed (%ld)", PTR_ERR(skb));
Marcel Holtmann9f8f9622013-04-10 08:11:35 -07001493 else
1494 kfree_skb(skb);
1495
1496 return 0;
1497}
1498
Marcel Holtmann81cac642014-01-03 03:02:36 -08001499static int btusb_setup_csr(struct hci_dev *hdev)
1500{
1501 struct hci_rp_read_local_version *rp;
1502 struct sk_buff *skb;
Marcel Holtmann81cac642014-01-03 03:02:36 -08001503
1504 BT_DBG("%s", hdev->name);
1505
Marcel Holtmann7cd84d72015-06-07 10:01:02 +02001506 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
1507 HCI_INIT_TIMEOUT);
1508 if (IS_ERR(skb)) {
1509 int err = PTR_ERR(skb);
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001510 bt_dev_err(hdev, "CSR: Local version failed (%d)", err);
Marcel Holtmann7cd84d72015-06-07 10:01:02 +02001511 return err;
1512 }
1513
1514 if (skb->len != sizeof(struct hci_rp_read_local_version)) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001515 bt_dev_err(hdev, "CSR: Local version length mismatch");
Marcel Holtmann7cd84d72015-06-07 10:01:02 +02001516 kfree_skb(skb);
1517 return -EIO;
1518 }
Marcel Holtmann81cac642014-01-03 03:02:36 -08001519
Marcel Holtmann89e75332014-09-16 04:44:50 +02001520 rp = (struct hci_rp_read_local_version *)skb->data;
Marcel Holtmann81cac642014-01-03 03:02:36 -08001521
Johan Hedberg6cafcd92015-08-30 21:47:21 +03001522 /* Detect controllers which aren't real CSR ones. */
1523 if (le16_to_cpu(rp->manufacturer) != 10 ||
1524 le16_to_cpu(rp->lmp_subver) == 0x0c5c) {
Marcel Holtmann9641d342015-06-07 10:01:01 +02001525 /* Clear the reset quirk since this is not an actual
1526 * early Bluetooth 1.1 device from CSR.
1527 */
1528 clear_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmann81cac642014-01-03 03:02:36 -08001529
Marcel Holtmann9641d342015-06-07 10:01:01 +02001530 /* These fake CSR controllers have all a broken
1531 * stored link key handling and so just disable it.
1532 */
1533 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
Marcel Holtmann81cac642014-01-03 03:02:36 -08001534 }
1535
Marcel Holtmann81cac642014-01-03 03:02:36 -08001536 kfree_skb(skb);
1537
Marcel Holtmann9641d342015-06-07 10:01:01 +02001538 return 0;
Marcel Holtmann81cac642014-01-03 03:02:36 -08001539}
1540
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001541static const struct firmware *btusb_setup_intel_get_fw(struct hci_dev *hdev,
Marcel Holtmann89e75332014-09-16 04:44:50 +02001542 struct intel_version *ver)
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001543{
1544 const struct firmware *fw;
1545 char fwname[64];
1546 int ret;
1547
1548 snprintf(fwname, sizeof(fwname),
1549 "intel/ibt-hw-%x.%x.%x-fw-%x.%x.%x.%x.%x.bseq",
1550 ver->hw_platform, ver->hw_variant, ver->hw_revision,
1551 ver->fw_variant, ver->fw_revision, ver->fw_build_num,
1552 ver->fw_build_ww, ver->fw_build_yy);
1553
1554 ret = request_firmware(&fw, fwname, &hdev->dev);
1555 if (ret < 0) {
1556 if (ret == -EINVAL) {
1557 BT_ERR("%s Intel firmware file request failed (%d)",
1558 hdev->name, ret);
1559 return NULL;
1560 }
1561
1562 BT_ERR("%s failed to open Intel firmware file: %s(%d)",
1563 hdev->name, fwname, ret);
1564
1565 /* If the correct firmware patch file is not found, use the
1566 * default firmware patch file instead
1567 */
1568 snprintf(fwname, sizeof(fwname), "intel/ibt-hw-%x.%x.bseq",
1569 ver->hw_platform, ver->hw_variant);
1570 if (request_firmware(&fw, fwname, &hdev->dev) < 0) {
1571 BT_ERR("%s failed to open default Intel fw file: %s",
1572 hdev->name, fwname);
1573 return NULL;
1574 }
1575 }
1576
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001577 bt_dev_info(hdev, "Intel Bluetooth firmware file: %s", fwname);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001578
1579 return fw;
1580}
1581
1582static int btusb_setup_intel_patching(struct hci_dev *hdev,
1583 const struct firmware *fw,
1584 const u8 **fw_ptr, int *disable_patch)
1585{
1586 struct sk_buff *skb;
1587 struct hci_command_hdr *cmd;
1588 const u8 *cmd_param;
1589 struct hci_event_hdr *evt = NULL;
1590 const u8 *evt_param = NULL;
1591 int remain = fw->size - (*fw_ptr - fw->data);
1592
1593 /* The first byte indicates the types of the patch command or event.
1594 * 0x01 means HCI command and 0x02 is HCI event. If the first bytes
1595 * in the current firmware buffer doesn't start with 0x01 or
1596 * the size of remain buffer is smaller than HCI command header,
1597 * the firmware file is corrupted and it should stop the patching
1598 * process.
1599 */
1600 if (remain > HCI_COMMAND_HDR_SIZE && *fw_ptr[0] != 0x01) {
1601 BT_ERR("%s Intel fw corrupted: invalid cmd read", hdev->name);
1602 return -EINVAL;
1603 }
1604 (*fw_ptr)++;
1605 remain--;
1606
1607 cmd = (struct hci_command_hdr *)(*fw_ptr);
1608 *fw_ptr += sizeof(*cmd);
1609 remain -= sizeof(*cmd);
1610
1611 /* Ensure that the remain firmware data is long enough than the length
1612 * of command parameter. If not, the firmware file is corrupted.
1613 */
1614 if (remain < cmd->plen) {
1615 BT_ERR("%s Intel fw corrupted: invalid cmd len", hdev->name);
1616 return -EFAULT;
1617 }
1618
1619 /* If there is a command that loads a patch in the firmware
1620 * file, then enable the patch upon success, otherwise just
1621 * disable the manufacturer mode, for example patch activation
1622 * is not required when the default firmware patch file is used
1623 * because there are no patch data to load.
1624 */
1625 if (*disable_patch && le16_to_cpu(cmd->opcode) == 0xfc8e)
1626 *disable_patch = 0;
1627
1628 cmd_param = *fw_ptr;
1629 *fw_ptr += cmd->plen;
1630 remain -= cmd->plen;
1631
1632 /* This reads the expected events when the above command is sent to the
1633 * device. Some vendor commands expects more than one events, for
1634 * example command status event followed by vendor specific event.
1635 * For this case, it only keeps the last expected event. so the command
1636 * can be sent with __hci_cmd_sync_ev() which returns the sk_buff of
1637 * last expected event.
1638 */
1639 while (remain > HCI_EVENT_HDR_SIZE && *fw_ptr[0] == 0x02) {
1640 (*fw_ptr)++;
1641 remain--;
1642
1643 evt = (struct hci_event_hdr *)(*fw_ptr);
1644 *fw_ptr += sizeof(*evt);
1645 remain -= sizeof(*evt);
1646
1647 if (remain < evt->plen) {
1648 BT_ERR("%s Intel fw corrupted: invalid evt len",
1649 hdev->name);
1650 return -EFAULT;
1651 }
1652
1653 evt_param = *fw_ptr;
1654 *fw_ptr += evt->plen;
1655 remain -= evt->plen;
1656 }
1657
1658 /* Every HCI commands in the firmware file has its correspond event.
1659 * If event is not found or remain is smaller than zero, the firmware
1660 * file is corrupted.
1661 */
1662 if (!evt || !evt_param || remain < 0) {
1663 BT_ERR("%s Intel fw corrupted: invalid evt read", hdev->name);
1664 return -EFAULT;
1665 }
1666
1667 skb = __hci_cmd_sync_ev(hdev, le16_to_cpu(cmd->opcode), cmd->plen,
1668 cmd_param, evt->evt, HCI_INIT_TIMEOUT);
1669 if (IS_ERR(skb)) {
1670 BT_ERR("%s sending Intel patch command (0x%4.4x) failed (%ld)",
1671 hdev->name, cmd->opcode, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001672 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001673 }
1674
1675 /* It ensures that the returned event matches the event data read from
1676 * the firmware file. At fist, it checks the length and then
1677 * the contents of the event.
1678 */
1679 if (skb->len != evt->plen) {
1680 BT_ERR("%s mismatch event length (opcode 0x%4.4x)", hdev->name,
1681 le16_to_cpu(cmd->opcode));
1682 kfree_skb(skb);
1683 return -EFAULT;
1684 }
1685
1686 if (memcmp(skb->data, evt_param, evt->plen)) {
1687 BT_ERR("%s mismatch event parameter (opcode 0x%4.4x)",
1688 hdev->name, le16_to_cpu(cmd->opcode));
1689 kfree_skb(skb);
1690 return -EFAULT;
1691 }
1692 kfree_skb(skb);
1693
1694 return 0;
1695}
1696
1697static int btusb_setup_intel(struct hci_dev *hdev)
1698{
1699 struct sk_buff *skb;
1700 const struct firmware *fw;
1701 const u8 *fw_ptr;
Loic Poulain28dc4b92015-12-03 16:10:22 +01001702 int disable_patch, err;
Loic Poulain6c483de2015-12-06 16:18:34 +01001703 struct intel_version ver;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001704
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001705 BT_DBG("%s", hdev->name);
1706
1707 /* The controller has a bug with the first HCI command sent to it
1708 * returning number of completed commands as zero. This would stall the
1709 * command processing in the Bluetooth core.
1710 *
1711 * As a workaround, send HCI Reset command first which will reset the
1712 * number of completed commands and allow normal command processing
1713 * from now on.
1714 */
1715 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
1716 if (IS_ERR(skb)) {
1717 BT_ERR("%s sending initial HCI reset command failed (%ld)",
1718 hdev->name, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001719 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001720 }
1721 kfree_skb(skb);
1722
1723 /* Read Intel specific controller version first to allow selection of
1724 * which firmware file to load.
1725 *
1726 * The returned information are hardware variant and revision plus
1727 * firmware variant, revision and build number.
1728 */
Loic Poulain6c483de2015-12-06 16:18:34 +01001729 err = btintel_read_version(hdev, &ver);
1730 if (err)
1731 return err;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001732
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001733 bt_dev_info(hdev, "read Intel version: %02x%02x%02x%02x%02x%02x%02x%02x%02x",
1734 ver.hw_platform, ver.hw_variant, ver.hw_revision,
1735 ver.fw_variant, ver.fw_revision, ver.fw_build_num,
1736 ver.fw_build_ww, ver.fw_build_yy, ver.fw_patch_num);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001737
1738 /* fw_patch_num indicates the version of patch the device currently
1739 * have. If there is no patch data in the device, it is always 0x00.
Minjune Kim5075eda2015-08-27 13:21:52 +09001740 * So, if it is other than 0x00, no need to patch the device again.
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001741 */
Loic Poulain6c483de2015-12-06 16:18:34 +01001742 if (ver.fw_patch_num) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001743 bt_dev_info(hdev, "Intel device is already patched. "
1744 "patch num: %02x", ver.fw_patch_num);
Marcel Holtmann213445b2015-10-21 02:45:19 +02001745 goto complete;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001746 }
1747
1748 /* Opens the firmware patch file based on the firmware version read
1749 * from the controller. If it fails to open the matching firmware
1750 * patch file, it tries to open the default firmware patch file.
1751 * If no patch file is found, allow the device to operate without
1752 * a patch.
1753 */
Loic Poulain6c483de2015-12-06 16:18:34 +01001754 fw = btusb_setup_intel_get_fw(hdev, &ver);
1755 if (!fw)
Marcel Holtmann213445b2015-10-21 02:45:19 +02001756 goto complete;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001757 fw_ptr = fw->data;
1758
Loic Poulain28dc4b92015-12-03 16:10:22 +01001759 /* Enable the manufacturer mode of the controller.
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001760 * Only while this mode is enabled, the driver can download the
1761 * firmware patch data and configuration parameters.
1762 */
Loic Poulain28dc4b92015-12-03 16:10:22 +01001763 err = btintel_enter_mfg(hdev);
1764 if (err) {
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001765 release_firmware(fw);
Loic Poulain28dc4b92015-12-03 16:10:22 +01001766 return err;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001767 }
1768
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001769 disable_patch = 1;
1770
1771 /* The firmware data file consists of list of Intel specific HCI
1772 * commands and its expected events. The first byte indicates the
1773 * type of the message, either HCI command or HCI event.
1774 *
1775 * It reads the command and its expected event from the firmware file,
1776 * and send to the controller. Once __hci_cmd_sync_ev() returns,
1777 * the returned event is compared with the event read from the firmware
1778 * file and it will continue until all the messages are downloaded to
1779 * the controller.
1780 *
1781 * Once the firmware patching is completed successfully,
1782 * the manufacturer mode is disabled with reset and activating the
1783 * downloaded patch.
1784 *
1785 * If the firmware patching fails, the manufacturer mode is
1786 * disabled with reset and deactivating the patch.
1787 *
1788 * If the default patch file is used, no reset is done when disabling
1789 * the manufacturer.
1790 */
1791 while (fw->size > fw_ptr - fw->data) {
1792 int ret;
1793
1794 ret = btusb_setup_intel_patching(hdev, fw, &fw_ptr,
1795 &disable_patch);
1796 if (ret < 0)
1797 goto exit_mfg_deactivate;
1798 }
1799
1800 release_firmware(fw);
1801
1802 if (disable_patch)
1803 goto exit_mfg_disable;
1804
1805 /* Patching completed successfully and disable the manufacturer mode
1806 * with reset and activate the downloaded firmware patches.
1807 */
Loic Poulain28dc4b92015-12-03 16:10:22 +01001808 err = btintel_exit_mfg(hdev, true, true);
1809 if (err)
1810 return err;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001811
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001812 bt_dev_info(hdev, "Intel firmware patch completed and activated");
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001813
Marcel Holtmann213445b2015-10-21 02:45:19 +02001814 goto complete;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001815
1816exit_mfg_disable:
1817 /* Disable the manufacturer mode without reset */
Loic Poulain28dc4b92015-12-03 16:10:22 +01001818 err = btintel_exit_mfg(hdev, false, false);
1819 if (err)
1820 return err;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001821
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001822 bt_dev_info(hdev, "Intel firmware patch completed");
Marcel Holtmann40cb0982014-07-02 12:06:45 +02001823
Marcel Holtmann213445b2015-10-21 02:45:19 +02001824 goto complete;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001825
1826exit_mfg_deactivate:
1827 release_firmware(fw);
1828
1829 /* Patching failed. Disable the manufacturer mode with reset and
1830 * deactivate the downloaded firmware patches.
1831 */
Loic Poulain28dc4b92015-12-03 16:10:22 +01001832 err = btintel_exit_mfg(hdev, true, false);
1833 if (err)
1834 return err;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001835
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001836 bt_dev_info(hdev, "Intel firmware patch completed and deactivated");
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001837
Marcel Holtmann213445b2015-10-21 02:45:19 +02001838complete:
1839 /* Set the event mask for Intel specific vendor events. This enables
1840 * a few extra events that are useful during general operation.
1841 */
1842 btintel_set_event_mask_mfg(hdev, false);
1843
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07001844 btintel_check_bdaddr(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001845 return 0;
1846}
1847
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001848static int inject_cmd_complete(struct hci_dev *hdev, __u16 opcode)
1849{
1850 struct sk_buff *skb;
1851 struct hci_event_hdr *hdr;
1852 struct hci_ev_cmd_complete *evt;
1853
1854 skb = bt_skb_alloc(sizeof(*hdr) + sizeof(*evt) + 1, GFP_ATOMIC);
1855 if (!skb)
1856 return -ENOMEM;
1857
Johannes Berg4df864c2017-06-16 14:29:21 +02001858 hdr = skb_put(skb, sizeof(*hdr));
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001859 hdr->evt = HCI_EV_CMD_COMPLETE;
1860 hdr->plen = sizeof(*evt) + 1;
1861
Johannes Berg4df864c2017-06-16 14:29:21 +02001862 evt = skb_put(skb, sizeof(*evt));
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001863 evt->ncmd = 0x01;
1864 evt->opcode = cpu_to_le16(opcode);
1865
Johannes Berg634fef62017-06-16 14:29:24 +02001866 skb_put_u8(skb, 0x00);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001867
Marcel Holtmann618e8bc2015-11-05 07:33:56 +01001868 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001869
1870 return hci_recv_frame(hdev, skb);
1871}
1872
1873static int btusb_recv_bulk_intel(struct btusb_data *data, void *buffer,
1874 int count)
1875{
1876 /* When the device is in bootloader mode, then it can send
1877 * events via the bulk endpoint. These events are treated the
1878 * same way as the ones received from the interrupt endpoint.
1879 */
1880 if (test_bit(BTUSB_BOOTLOADER, &data->flags))
1881 return btusb_recv_intr(data, buffer, count);
1882
1883 return btusb_recv_bulk(data, buffer, count);
1884}
1885
Marcel Holtmannccd6da22015-04-09 00:35:19 -07001886static void btusb_intel_bootup(struct btusb_data *data, const void *ptr,
1887 unsigned int len)
1888{
1889 const struct intel_bootup *evt = ptr;
1890
1891 if (len != sizeof(*evt))
1892 return;
1893
1894 if (test_and_clear_bit(BTUSB_BOOTING, &data->flags)) {
1895 smp_mb__after_atomic();
1896 wake_up_bit(&data->flags, BTUSB_BOOTING);
1897 }
1898}
1899
1900static void btusb_intel_secure_send_result(struct btusb_data *data,
1901 const void *ptr, unsigned int len)
1902{
1903 const struct intel_secure_send_result *evt = ptr;
1904
1905 if (len != sizeof(*evt))
1906 return;
1907
1908 if (evt->result)
1909 set_bit(BTUSB_FIRMWARE_FAILED, &data->flags);
1910
1911 if (test_and_clear_bit(BTUSB_DOWNLOADING, &data->flags) &&
1912 test_bit(BTUSB_FIRMWARE_LOADED, &data->flags)) {
1913 smp_mb__after_atomic();
1914 wake_up_bit(&data->flags, BTUSB_DOWNLOADING);
1915 }
1916}
1917
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001918static int btusb_recv_event_intel(struct hci_dev *hdev, struct sk_buff *skb)
1919{
1920 struct btusb_data *data = hci_get_drvdata(hdev);
1921
1922 if (test_bit(BTUSB_BOOTLOADER, &data->flags)) {
1923 struct hci_event_hdr *hdr = (void *)skb->data;
1924
Marcel Holtmannccd6da22015-04-09 00:35:19 -07001925 if (skb->len > HCI_EVENT_HDR_SIZE && hdr->evt == 0xff &&
1926 hdr->plen > 0) {
1927 const void *ptr = skb->data + HCI_EVENT_HDR_SIZE + 1;
1928 unsigned int len = skb->len - HCI_EVENT_HDR_SIZE - 1;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001929
Marcel Holtmannccd6da22015-04-09 00:35:19 -07001930 switch (skb->data[2]) {
1931 case 0x02:
1932 /* When switching to the operational firmware
1933 * the device sends a vendor specific event
1934 * indicating that the bootup completed.
1935 */
1936 btusb_intel_bootup(data, ptr, len);
1937 break;
1938 case 0x06:
1939 /* When the firmware loading completes the
1940 * device sends out a vendor specific event
1941 * indicating the result of the firmware
1942 * loading.
1943 */
1944 btusb_intel_secure_send_result(data, ptr, len);
1945 break;
Johan Hedbergfad70972015-01-30 10:58:55 +02001946 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001947 }
1948 }
1949
1950 return hci_recv_frame(hdev, skb);
1951}
1952
1953static int btusb_send_frame_intel(struct hci_dev *hdev, struct sk_buff *skb)
1954{
1955 struct btusb_data *data = hci_get_drvdata(hdev);
1956 struct urb *urb;
1957
1958 BT_DBG("%s", hdev->name);
1959
Marcel Holtmann618e8bc2015-11-05 07:33:56 +01001960 switch (hci_skb_pkt_type(skb)) {
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001961 case HCI_COMMAND_PKT:
1962 if (test_bit(BTUSB_BOOTLOADER, &data->flags)) {
1963 struct hci_command_hdr *cmd = (void *)skb->data;
1964 __u16 opcode = le16_to_cpu(cmd->opcode);
1965
1966 /* When in bootloader mode and the command 0xfc09
1967 * is received, it needs to be send down the
1968 * bulk endpoint. So allocate a bulk URB instead.
1969 */
1970 if (opcode == 0xfc09)
1971 urb = alloc_bulk_urb(hdev, skb);
1972 else
1973 urb = alloc_ctrl_urb(hdev, skb);
1974
1975 /* When the 0xfc01 command is issued to boot into
1976 * the operational firmware, it will actually not
1977 * send a command complete event. To keep the flow
1978 * control working inject that event here.
1979 */
1980 if (opcode == 0xfc01)
1981 inject_cmd_complete(hdev, opcode);
1982 } else {
1983 urb = alloc_ctrl_urb(hdev, skb);
1984 }
1985 if (IS_ERR(urb))
1986 return PTR_ERR(urb);
1987
1988 hdev->stat.cmd_tx++;
1989 return submit_or_queue_tx_urb(hdev, urb);
1990
1991 case HCI_ACLDATA_PKT:
1992 urb = alloc_bulk_urb(hdev, skb);
1993 if (IS_ERR(urb))
1994 return PTR_ERR(urb);
1995
1996 hdev->stat.acl_tx++;
1997 return submit_or_queue_tx_urb(hdev, urb);
1998
1999 case HCI_SCODATA_PKT:
2000 if (hci_conn_num(hdev, SCO_LINK) < 1)
2001 return -ENODEV;
2002
2003 urb = alloc_isoc_urb(hdev, skb);
2004 if (IS_ERR(urb))
2005 return PTR_ERR(urb);
2006
2007 hdev->stat.sco_tx++;
2008 return submit_tx_urb(hdev, urb);
2009 }
2010
2011 return -EILSEQ;
2012}
2013
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002014static int btusb_setup_intel_new(struct hci_dev *hdev)
2015{
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002016 struct btusb_data *data = hci_get_drvdata(hdev);
Loic Poulain6c483de2015-12-06 16:18:34 +01002017 struct intel_version ver;
Tedd Ho-Jeong Anfaf174d2018-01-24 09:19:20 -08002018 struct intel_boot_params params;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002019 const struct firmware *fw;
Tedd Ho-Jeong Ane5889af2018-01-24 09:19:18 -08002020 u32 boot_param;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002021 char fwname[64];
2022 ktime_t calltime, delta, rettime;
2023 unsigned long long duration;
2024 int err;
2025
2026 BT_DBG("%s", hdev->name);
2027
Tedd Ho-Jeong An04d729b2018-01-24 09:19:19 -08002028 /* Set the default boot parameter to 0x0 and it is updated to
2029 * SKU specific boot parameter after reading Intel_Write_Boot_Params
2030 * command while downloading the firmware.
2031 */
2032 boot_param = 0x00000000;
Tedd Ho-Jeong Ane5889af2018-01-24 09:19:18 -08002033
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002034 calltime = ktime_get();
2035
2036 /* Read the Intel version information to determine if the device
2037 * is in bootloader mode or if it already has operational firmware
2038 * loaded.
2039 */
Loic Poulain6c483de2015-12-06 16:18:34 +01002040 err = btintel_read_version(hdev, &ver);
2041 if (err)
2042 return err;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002043
2044 /* The hardware platform number has a fixed value of 0x37 and
2045 * for now only accept this single value.
2046 */
Loic Poulain6c483de2015-12-06 16:18:34 +01002047 if (ver.hw_platform != 0x37) {
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002048 BT_ERR("%s: Unsupported Intel hardware platform (%u)",
Loic Poulain6c483de2015-12-06 16:18:34 +01002049 hdev->name, ver.hw_platform);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002050 return -EINVAL;
2051 }
2052
Tedd Ho-Jeong An92688342017-03-06 15:38:26 -08002053 /* Check for supported iBT hardware variants of this firmware
2054 * loading method.
Tedd Ho-Jeong Ana0af53b2016-05-06 11:53:46 -07002055 *
2056 * This check has been put in place to ensure correct forward
2057 * compatibility options when newer hardware variants come along.
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002058 */
Tedd Ho-Jeong An92688342017-03-06 15:38:26 -08002059 switch (ver.hw_variant) {
2060 case 0x0b: /* SfP */
2061 case 0x0c: /* WsP */
Tedd Ho-Jeong An86a61292017-05-01 13:35:12 -07002062 case 0x11: /* JfP */
Marcel Holtmannde766142017-03-06 15:38:28 -08002063 case 0x12: /* ThP */
Tedd Ho-Jeong An1ce0cec2018-02-05 14:20:36 -08002064 case 0x13: /* HrP */
2065 case 0x14: /* QnJ, IcP */
Tedd Ho-Jeong An92688342017-03-06 15:38:26 -08002066 break;
2067 default:
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002068 BT_ERR("%s: Unsupported Intel hardware variant (%u)",
Loic Poulain6c483de2015-12-06 16:18:34 +01002069 hdev->name, ver.hw_variant);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002070 return -EINVAL;
2071 }
2072
Loic Poulain6c483de2015-12-06 16:18:34 +01002073 btintel_version_info(hdev, &ver);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002074
2075 /* The firmware variant determines if the device is in bootloader
2076 * mode or is running operational firmware. The value 0x06 identifies
2077 * the bootloader and the value 0x23 identifies the operational
2078 * firmware.
2079 *
2080 * When the operational firmware is already present, then only
2081 * the check for valid Bluetooth device address is needed. This
2082 * determines if the device will be added as configured or
2083 * unconfigured controller.
2084 *
2085 * It is not possible to use the Secure Boot Parameters in this
2086 * case since that command is only available in bootloader mode.
2087 */
Loic Poulain6c483de2015-12-06 16:18:34 +01002088 if (ver.fw_variant == 0x23) {
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002089 clear_bit(BTUSB_BOOTLOADER, &data->flags);
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07002090 btintel_check_bdaddr(hdev);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002091 return 0;
2092 }
2093
2094 /* If the device is not in bootloader mode, then the only possible
2095 * choice is to return an error and abort the device initialization.
2096 */
Loic Poulain6c483de2015-12-06 16:18:34 +01002097 if (ver.fw_variant != 0x06) {
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002098 BT_ERR("%s: Unsupported Intel firmware variant (%u)",
Loic Poulain6c483de2015-12-06 16:18:34 +01002099 hdev->name, ver.fw_variant);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002100 return -ENODEV;
2101 }
2102
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002103 /* Read the secure boot parameters to identify the operating
2104 * details of the bootloader.
2105 */
Tedd Ho-Jeong Anfaf174d2018-01-24 09:19:20 -08002106 err = btintel_read_boot_params(hdev, &params);
2107 if (err)
2108 return err;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002109
2110 /* It is required that every single firmware fragment is acknowledged
2111 * with a command complete event. If the boot parameters indicate
2112 * that this bootloader does not send them, then abort the setup.
2113 */
Tedd Ho-Jeong Anfaf174d2018-01-24 09:19:20 -08002114 if (params.limited_cce != 0x00) {
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002115 BT_ERR("%s: Unsupported Intel firmware loading method (%u)",
Tedd Ho-Jeong Anfaf174d2018-01-24 09:19:20 -08002116 hdev->name, params.limited_cce);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002117 return -EINVAL;
2118 }
2119
2120 /* If the OTP has no valid Bluetooth device address, then there will
2121 * also be no valid address for the operational firmware.
2122 */
Tedd Ho-Jeong Anfaf174d2018-01-24 09:19:20 -08002123 if (!bacmp(&params.otp_bdaddr, BDADDR_ANY)) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002124 bt_dev_info(hdev, "No device address configured");
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002125 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
2126 }
2127
2128 /* With this Intel bootloader only the hardware variant and device
Jaya P Gaf3715e2017-10-30 11:01:22 +01002129 * revision information are used to select the right firmware for SfP
2130 * and WsP.
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002131 *
Tedd Ho-Jeong An230b04a2016-06-28 08:56:39 -07002132 * The firmware filename is ibt-<hw_variant>-<dev_revid>.sfi.
2133 *
2134 * Currently the supported hardware variants are:
2135 * 11 (0x0b) for iBT3.0 (LnP/SfP)
2136 * 12 (0x0c) for iBT3.5 (WsP)
Jaya P Gaf3715e2017-10-30 11:01:22 +01002137 *
2138 * For ThP/JfP and for future SKU's, the FW name varies based on HW
2139 * variant, HW revision and FW revision, as these are dependent on CNVi
2140 * and RF Combination.
2141 *
Tedd Ho-Jeong An86a61292017-05-01 13:35:12 -07002142 * 17 (0x11) for iBT3.5 (JfP)
2143 * 18 (0x12) for iBT3.5 (ThP)
Jaya P Gaf3715e2017-10-30 11:01:22 +01002144 *
2145 * The firmware file name for these will be
2146 * ibt-<hw_variant>-<hw_revision>-<fw_revision>.sfi.
2147 *
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002148 */
Jaya P Gaf3715e2017-10-30 11:01:22 +01002149 switch (ver.hw_variant) {
2150 case 0x0b: /* SfP */
2151 case 0x0c: /* WsP */
2152 snprintf(fwname, sizeof(fwname), "intel/ibt-%u-%u.sfi",
2153 le16_to_cpu(ver.hw_variant),
Tedd Ho-Jeong Anfaf174d2018-01-24 09:19:20 -08002154 le16_to_cpu(params.dev_revid));
Jaya P Gaf3715e2017-10-30 11:01:22 +01002155 break;
2156 case 0x11: /* JfP */
2157 case 0x12: /* ThP */
Tedd Ho-Jeong An1ce0cec2018-02-05 14:20:36 -08002158 case 0x13: /* HrP */
2159 case 0x14: /* QnJ, IcP */
Jaya P Gaf3715e2017-10-30 11:01:22 +01002160 snprintf(fwname, sizeof(fwname), "intel/ibt-%u-%u-%u.sfi",
2161 le16_to_cpu(ver.hw_variant),
2162 le16_to_cpu(ver.hw_revision),
2163 le16_to_cpu(ver.fw_revision));
2164 break;
2165 default:
2166 BT_ERR("%s: Unsupported Intel firmware naming", hdev->name);
2167 return -EINVAL;
2168 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002169
2170 err = request_firmware(&fw, fwname, &hdev->dev);
2171 if (err < 0) {
2172 BT_ERR("%s: Failed to load Intel firmware file (%d)",
2173 hdev->name, err);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002174 return err;
2175 }
2176
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002177 bt_dev_info(hdev, "Found device firmware: %s", fwname);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002178
Tedd Ho-Jeong An52cc9162015-06-12 16:20:05 -07002179 /* Save the DDC file name for later use to apply once the firmware
2180 * downloading is done.
2181 */
Jaya P Gaf3715e2017-10-30 11:01:22 +01002182 switch (ver.hw_variant) {
2183 case 0x0b: /* SfP */
2184 case 0x0c: /* WsP */
2185 snprintf(fwname, sizeof(fwname), "intel/ibt-%u-%u.ddc",
2186 le16_to_cpu(ver.hw_variant),
Tedd Ho-Jeong Anfaf174d2018-01-24 09:19:20 -08002187 le16_to_cpu(params.dev_revid));
Jaya P Gaf3715e2017-10-30 11:01:22 +01002188 break;
2189 case 0x11: /* JfP */
2190 case 0x12: /* ThP */
Tedd Ho-Jeong An1ce0cec2018-02-05 14:20:36 -08002191 case 0x13: /* HrP */
2192 case 0x14: /* QnJ, IcP */
Jaya P Gaf3715e2017-10-30 11:01:22 +01002193 snprintf(fwname, sizeof(fwname), "intel/ibt-%u-%u-%u.ddc",
2194 le16_to_cpu(ver.hw_variant),
2195 le16_to_cpu(ver.hw_revision),
2196 le16_to_cpu(ver.fw_revision));
2197 break;
2198 default:
2199 BT_ERR("%s: Unsupported Intel firmware naming", hdev->name);
2200 return -EINVAL;
2201 }
Tedd Ho-Jeong An52cc9162015-06-12 16:20:05 -07002202
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002203 if (fw->size < 644) {
2204 BT_ERR("%s: Invalid size of firmware file (%zu)",
2205 hdev->name, fw->size);
2206 err = -EBADF;
2207 goto done;
2208 }
2209
2210 set_bit(BTUSB_DOWNLOADING, &data->flags);
2211
Tedd Ho-Jeong Anfbbe83c2018-01-24 09:19:21 -08002212 /* Start firmware downloading and get boot parameter */
2213 err = btintel_download_firmware(hdev, fw, &boot_param);
2214 if (err < 0)
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002215 goto done;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002216
Marcel Holtmannce6bb922015-01-28 01:58:40 -08002217 set_bit(BTUSB_FIRMWARE_LOADED, &data->flags);
2218
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002219 bt_dev_info(hdev, "Waiting for firmware download to complete");
Johan Hedberga087a982015-01-30 10:58:54 +02002220
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002221 /* Before switching the device into operational mode and with that
2222 * booting the loaded firmware, wait for the bootloader notification
2223 * that all fragments have been successfully received.
2224 *
Johan Hedberga087a982015-01-30 10:58:54 +02002225 * When the event processing receives the notification, then the
2226 * BTUSB_DOWNLOADING flag will be cleared.
2227 *
2228 * The firmware loading should not take longer than 5 seconds
2229 * and thus just timeout if that happens and fail the setup
2230 * of this device.
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002231 */
Johan Hedberg129a7692015-02-14 09:33:35 +02002232 err = wait_on_bit_timeout(&data->flags, BTUSB_DOWNLOADING,
2233 TASK_INTERRUPTIBLE,
2234 msecs_to_jiffies(5000));
Bart Van Asschef0a70a02016-08-11 16:02:44 -07002235 if (err == -EINTR) {
Johan Hedberga087a982015-01-30 10:58:54 +02002236 BT_ERR("%s: Firmware loading interrupted", hdev->name);
Johan Hedberga087a982015-01-30 10:58:54 +02002237 goto done;
2238 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002239
Johan Hedberga087a982015-01-30 10:58:54 +02002240 if (err) {
2241 BT_ERR("%s: Firmware loading timeout", hdev->name);
2242 err = -ETIMEDOUT;
2243 goto done;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002244 }
2245
2246 if (test_bit(BTUSB_FIRMWARE_FAILED, &data->flags)) {
2247 BT_ERR("%s: Firmware loading failed", hdev->name);
2248 err = -ENOEXEC;
2249 goto done;
2250 }
2251
2252 rettime = ktime_get();
2253 delta = ktime_sub(rettime, calltime);
2254 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
2255
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002256 bt_dev_info(hdev, "Firmware loaded in %llu usecs", duration);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002257
2258done:
2259 release_firmware(fw);
2260
2261 if (err < 0)
2262 return err;
2263
2264 calltime = ktime_get();
2265
2266 set_bit(BTUSB_BOOTING, &data->flags);
2267
Tedd Ho-Jeong Ane5889af2018-01-24 09:19:18 -08002268 err = btintel_send_intel_reset(hdev, boot_param);
2269 if (err)
2270 return err;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002271
2272 /* The bootloader will not indicate when the device is ready. This
2273 * is done by the operational firmware sending bootup notification.
Johan Hedbergfad70972015-01-30 10:58:55 +02002274 *
2275 * Booting into operational firmware should not take longer than
2276 * 1 second. However if that happens, then just fail the setup
2277 * since something went wrong.
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002278 */
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002279 bt_dev_info(hdev, "Waiting for device to boot");
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002280
Johan Hedberg129a7692015-02-14 09:33:35 +02002281 err = wait_on_bit_timeout(&data->flags, BTUSB_BOOTING,
2282 TASK_INTERRUPTIBLE,
2283 msecs_to_jiffies(1000));
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002284
Bart Van Asschef0a70a02016-08-11 16:02:44 -07002285 if (err == -EINTR) {
Johan Hedbergfad70972015-01-30 10:58:55 +02002286 BT_ERR("%s: Device boot interrupted", hdev->name);
2287 return -EINTR;
2288 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002289
Johan Hedbergfad70972015-01-30 10:58:55 +02002290 if (err) {
2291 BT_ERR("%s: Device boot timeout", hdev->name);
2292 return -ETIMEDOUT;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002293 }
2294
2295 rettime = ktime_get();
2296 delta = ktime_sub(rettime, calltime);
2297 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
2298
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002299 bt_dev_info(hdev, "Device booted in %llu usecs", duration);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002300
2301 clear_bit(BTUSB_BOOTLOADER, &data->flags);
2302
Tedd Ho-Jeong An52cc9162015-06-12 16:20:05 -07002303 /* Once the device is running in operational mode, it needs to apply
2304 * the device configuration (DDC) parameters.
2305 *
2306 * The device can work without DDC parameters, so even if it fails
2307 * to load the file, no need to fail the setup.
2308 */
Loic Poulaine924d3d2015-09-04 17:54:36 +02002309 btintel_load_ddc_config(hdev, fwname);
Tedd Ho-Jeong An52cc9162015-06-12 16:20:05 -07002310
Marcel Holtmann213445b2015-10-21 02:45:19 +02002311 /* Set the event mask for Intel specific vendor events. This enables
2312 * a few extra events that are useful during general operation. It
2313 * does not enable any debugging related events.
2314 *
2315 * The device will function correctly without these events enabled
2316 * and thus no need to fail the setup.
2317 */
2318 btintel_set_event_mask(hdev, false);
2319
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002320 return 0;
2321}
2322
Tedd Ho-Jeong Anbfbd45e2015-02-13 09:20:52 -08002323static int btusb_shutdown_intel(struct hci_dev *hdev)
2324{
2325 struct sk_buff *skb;
2326 long ret;
2327
2328 /* Some platforms have an issue with BT LED when the interface is
2329 * down or BT radio is turned off, which takes 5 seconds to BT LED
2330 * goes off. This command turns off the BT LED immediately.
2331 */
2332 skb = __hci_cmd_sync(hdev, 0xfc3f, 0, NULL, HCI_INIT_TIMEOUT);
2333 if (IS_ERR(skb)) {
2334 ret = PTR_ERR(skb);
2335 BT_ERR("%s: turning off Intel device LED failed (%ld)",
2336 hdev->name, ret);
2337 return ret;
2338 }
2339 kfree_skb(skb);
2340
2341 return 0;
2342}
2343
Rajat Jaina4ccc9e2017-02-01 14:24:10 -08002344#ifdef CONFIG_PM
2345/* Configure an out-of-band gpio as wake-up pin, if specified in device tree */
2346static int marvell_config_oob_wake(struct hci_dev *hdev)
2347{
2348 struct sk_buff *skb;
2349 struct btusb_data *data = hci_get_drvdata(hdev);
2350 struct device *dev = &data->udev->dev;
2351 u16 pin, gap, opcode;
2352 int ret;
2353 u8 cmd[5];
2354
2355 /* Move on if no wakeup pin specified */
2356 if (of_property_read_u16(dev->of_node, "marvell,wakeup-pin", &pin) ||
2357 of_property_read_u16(dev->of_node, "marvell,wakeup-gap-ms", &gap))
2358 return 0;
2359
2360 /* Vendor specific command to configure a GPIO as wake-up pin */
2361 opcode = hci_opcode_pack(0x3F, 0x59);
2362 cmd[0] = opcode & 0xFF;
2363 cmd[1] = opcode >> 8;
2364 cmd[2] = 2; /* length of parameters that follow */
2365 cmd[3] = pin;
2366 cmd[4] = gap; /* time in ms, for which wakeup pin should be asserted */
2367
2368 skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL);
2369 if (!skb) {
2370 bt_dev_err(hdev, "%s: No memory\n", __func__);
2371 return -ENOMEM;
2372 }
2373
Johannes Berg59ae1d12017-06-16 14:29:20 +02002374 skb_put_data(skb, cmd, sizeof(cmd));
Rajat Jaina4ccc9e2017-02-01 14:24:10 -08002375 hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
2376
2377 ret = btusb_send_frame(hdev, skb);
2378 if (ret) {
2379 bt_dev_err(hdev, "%s: configuration failed\n", __func__);
2380 kfree_skb(skb);
2381 return ret;
2382 }
2383
2384 return 0;
2385}
2386#endif
2387
Amitkumar Karwarae8df492014-07-18 14:47:06 -07002388static int btusb_set_bdaddr_marvell(struct hci_dev *hdev,
2389 const bdaddr_t *bdaddr)
2390{
2391 struct sk_buff *skb;
2392 u8 buf[8];
2393 long ret;
2394
2395 buf[0] = 0xfe;
2396 buf[1] = sizeof(bdaddr_t);
2397 memcpy(buf + 2, bdaddr, sizeof(bdaddr_t));
2398
2399 skb = __hci_cmd_sync(hdev, 0xfc22, sizeof(buf), buf, HCI_INIT_TIMEOUT);
2400 if (IS_ERR(skb)) {
2401 ret = PTR_ERR(skb);
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002402 bt_dev_err(hdev, "changing Marvell device address failed (%ld)",
2403 ret);
Amitkumar Karwarae8df492014-07-18 14:47:06 -07002404 return ret;
2405 }
2406 kfree_skb(skb);
2407
2408 return 0;
2409}
2410
Toshi Kikuchi58592232014-12-12 10:58:05 -08002411static int btusb_set_bdaddr_ath3012(struct hci_dev *hdev,
2412 const bdaddr_t *bdaddr)
2413{
2414 struct sk_buff *skb;
2415 u8 buf[10];
2416 long ret;
2417
2418 buf[0] = 0x01;
2419 buf[1] = 0x01;
2420 buf[2] = 0x00;
2421 buf[3] = sizeof(bdaddr_t);
2422 memcpy(buf + 4, bdaddr, sizeof(bdaddr_t));
2423
2424 skb = __hci_cmd_sync(hdev, 0xfc0b, sizeof(buf), buf, HCI_INIT_TIMEOUT);
2425 if (IS_ERR(skb)) {
2426 ret = PTR_ERR(skb);
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002427 bt_dev_err(hdev, "Change address command failed (%ld)", ret);
Toshi Kikuchi58592232014-12-12 10:58:05 -08002428 return ret;
2429 }
2430 kfree_skb(skb);
2431
2432 return 0;
2433}
2434
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002435#define QCA_DFU_PACKET_LEN 4096
2436
2437#define QCA_GET_TARGET_VERSION 0x09
2438#define QCA_CHECK_STATUS 0x05
2439#define QCA_DFU_DOWNLOAD 0x01
2440
2441#define QCA_SYSCFG_UPDATED 0x40
2442#define QCA_PATCH_UPDATED 0x80
2443#define QCA_DFU_TIMEOUT 3000
2444
2445struct qca_version {
2446 __le32 rom_version;
2447 __le32 patch_version;
2448 __le32 ram_version;
2449 __le32 ref_clock;
2450 __u8 reserved[4];
2451} __packed;
2452
2453struct qca_rampatch_version {
2454 __le16 rom_version;
2455 __le16 patch_version;
2456} __packed;
2457
2458struct qca_device_info {
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002459 u32 rom_version;
2460 u8 rampatch_hdr; /* length of header in rampatch */
2461 u8 nvm_hdr; /* length of header in NVM */
2462 u8 ver_offset; /* offset of version structure in rampatch */
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002463};
2464
2465static const struct qca_device_info qca_devices_table[] = {
2466 { 0x00000100, 20, 4, 10 }, /* Rome 1.0 */
2467 { 0x00000101, 20, 4, 10 }, /* Rome 1.1 */
Chan-yeol Park7f6e6362015-05-21 11:24:27 +09002468 { 0x00000200, 28, 4, 18 }, /* Rome 2.0 */
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002469 { 0x00000201, 28, 4, 18 }, /* Rome 2.1 */
2470 { 0x00000300, 28, 4, 18 }, /* Rome 3.0 */
2471 { 0x00000302, 28, 4, 18 }, /* Rome 3.2 */
2472};
2473
2474static int btusb_qca_send_vendor_req(struct hci_dev *hdev, u8 request,
2475 void *data, u16 size)
2476{
2477 struct btusb_data *btdata = hci_get_drvdata(hdev);
2478 struct usb_device *udev = btdata->udev;
2479 int pipe, err;
2480 u8 *buf;
2481
2482 buf = kmalloc(size, GFP_KERNEL);
2483 if (!buf)
2484 return -ENOMEM;
2485
2486 /* Found some of USB hosts have IOT issues with ours so that we should
2487 * not wait until HCI layer is ready.
2488 */
2489 pipe = usb_rcvctrlpipe(udev, 0);
2490 err = usb_control_msg(udev, pipe, request, USB_TYPE_VENDOR | USB_DIR_IN,
2491 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
2492 if (err < 0) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002493 bt_dev_err(hdev, "Failed to access otp area (%d)", err);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002494 goto done;
2495 }
2496
2497 memcpy(data, buf, size);
2498
2499done:
2500 kfree(buf);
2501
2502 return err;
2503}
2504
2505static int btusb_setup_qca_download_fw(struct hci_dev *hdev,
2506 const struct firmware *firmware,
2507 size_t hdr_size)
2508{
2509 struct btusb_data *btdata = hci_get_drvdata(hdev);
2510 struct usb_device *udev = btdata->udev;
2511 size_t count, size, sent = 0;
2512 int pipe, len, err;
2513 u8 *buf;
2514
2515 buf = kmalloc(QCA_DFU_PACKET_LEN, GFP_KERNEL);
2516 if (!buf)
2517 return -ENOMEM;
2518
2519 count = firmware->size;
2520
2521 size = min_t(size_t, count, hdr_size);
2522 memcpy(buf, firmware->data, size);
2523
2524 /* USB patches should go down to controller through USB path
2525 * because binary format fits to go down through USB channel.
2526 * USB control path is for patching headers and USB bulk is for
2527 * patch body.
2528 */
2529 pipe = usb_sndctrlpipe(udev, 0);
2530 err = usb_control_msg(udev, pipe, QCA_DFU_DOWNLOAD, USB_TYPE_VENDOR,
2531 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
2532 if (err < 0) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002533 bt_dev_err(hdev, "Failed to send headers (%d)", err);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002534 goto done;
2535 }
2536
2537 sent += size;
2538 count -= size;
2539
2540 while (count) {
2541 size = min_t(size_t, count, QCA_DFU_PACKET_LEN);
2542
2543 memcpy(buf, firmware->data + sent, size);
2544
2545 pipe = usb_sndbulkpipe(udev, 0x02);
2546 err = usb_bulk_msg(udev, pipe, buf, size, &len,
2547 QCA_DFU_TIMEOUT);
2548 if (err < 0) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002549 bt_dev_err(hdev, "Failed to send body at %zd of %zd (%d)",
2550 sent, firmware->size, err);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002551 break;
2552 }
2553
2554 if (size != len) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002555 bt_dev_err(hdev, "Failed to get bulk buffer");
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002556 err = -EILSEQ;
2557 break;
2558 }
2559
2560 sent += size;
2561 count -= size;
2562 }
2563
2564done:
2565 kfree(buf);
2566 return err;
2567}
2568
2569static int btusb_setup_qca_load_rampatch(struct hci_dev *hdev,
2570 struct qca_version *ver,
2571 const struct qca_device_info *info)
2572{
2573 struct qca_rampatch_version *rver;
2574 const struct firmware *fw;
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002575 u32 ver_rom, ver_patch;
2576 u16 rver_rom, rver_patch;
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002577 char fwname[64];
2578 int err;
2579
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002580 ver_rom = le32_to_cpu(ver->rom_version);
2581 ver_patch = le32_to_cpu(ver->patch_version);
2582
2583 snprintf(fwname, sizeof(fwname), "qca/rampatch_usb_%08x.bin", ver_rom);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002584
2585 err = request_firmware(&fw, fwname, &hdev->dev);
2586 if (err) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002587 bt_dev_err(hdev, "failed to request rampatch file: %s (%d)",
2588 fwname, err);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002589 return err;
2590 }
2591
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002592 bt_dev_info(hdev, "using rampatch file: %s", fwname);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002593
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002594 rver = (struct qca_rampatch_version *)(fw->data + info->ver_offset);
2595 rver_rom = le16_to_cpu(rver->rom_version);
2596 rver_patch = le16_to_cpu(rver->patch_version);
2597
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002598 bt_dev_info(hdev, "QCA: patch rome 0x%x build 0x%x, "
2599 "firmware rome 0x%x build 0x%x",
2600 rver_rom, rver_patch, ver_rom, ver_patch);
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002601
2602 if (rver_rom != ver_rom || rver_patch <= ver_patch) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002603 bt_dev_err(hdev, "rampatch file version did not match with firmware");
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002604 err = -EINVAL;
2605 goto done;
2606 }
2607
2608 err = btusb_setup_qca_download_fw(hdev, fw, info->rampatch_hdr);
2609
2610done:
2611 release_firmware(fw);
2612
2613 return err;
2614}
2615
2616static int btusb_setup_qca_load_nvm(struct hci_dev *hdev,
2617 struct qca_version *ver,
2618 const struct qca_device_info *info)
2619{
2620 const struct firmware *fw;
2621 char fwname[64];
2622 int err;
2623
2624 snprintf(fwname, sizeof(fwname), "qca/nvm_usb_%08x.bin",
2625 le32_to_cpu(ver->rom_version));
2626
2627 err = request_firmware(&fw, fwname, &hdev->dev);
2628 if (err) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002629 bt_dev_err(hdev, "failed to request NVM file: %s (%d)",
2630 fwname, err);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002631 return err;
2632 }
2633
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002634 bt_dev_info(hdev, "using NVM file: %s", fwname);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002635
2636 err = btusb_setup_qca_download_fw(hdev, fw, info->nvm_hdr);
2637
2638 release_firmware(fw);
2639
2640 return err;
2641}
2642
2643static int btusb_setup_qca(struct hci_dev *hdev)
2644{
2645 const struct qca_device_info *info = NULL;
2646 struct qca_version ver;
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002647 u32 ver_rom;
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002648 u8 status;
2649 int i, err;
2650
2651 err = btusb_qca_send_vendor_req(hdev, QCA_GET_TARGET_VERSION, &ver,
Marcel Holtmanneb500422015-04-16 23:15:50 +02002652 sizeof(ver));
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002653 if (err < 0)
2654 return err;
2655
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002656 ver_rom = le32_to_cpu(ver.rom_version);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002657 for (i = 0; i < ARRAY_SIZE(qca_devices_table); i++) {
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002658 if (ver_rom == qca_devices_table[i].rom_version)
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002659 info = &qca_devices_table[i];
2660 }
2661 if (!info) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002662 bt_dev_err(hdev, "don't support firmware rome 0x%x", ver_rom);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002663 return -ENODEV;
2664 }
2665
2666 err = btusb_qca_send_vendor_req(hdev, QCA_CHECK_STATUS, &status,
2667 sizeof(status));
2668 if (err < 0)
2669 return err;
2670
2671 if (!(status & QCA_PATCH_UPDATED)) {
2672 err = btusb_setup_qca_load_rampatch(hdev, &ver, info);
2673 if (err < 0)
2674 return err;
2675 }
2676
2677 if (!(status & QCA_SYSCFG_UPDATED)) {
2678 err = btusb_setup_qca_load_nvm(hdev, &ver, info);
2679 if (err < 0)
2680 return err;
2681 }
2682
2683 return 0;
2684}
2685
Marcel Holtmann9d08f502015-10-08 20:23:08 +02002686#ifdef CONFIG_BT_HCIBTUSB_BCM
2687static inline int __set_diag_interface(struct hci_dev *hdev)
2688{
2689 struct btusb_data *data = hci_get_drvdata(hdev);
2690 struct usb_interface *intf = data->diag;
2691 int i;
2692
2693 if (!data->diag)
2694 return -ENODEV;
2695
2696 data->diag_tx_ep = NULL;
2697 data->diag_rx_ep = NULL;
2698
2699 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
2700 struct usb_endpoint_descriptor *ep_desc;
2701
2702 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
2703
2704 if (!data->diag_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
2705 data->diag_tx_ep = ep_desc;
2706 continue;
2707 }
2708
2709 if (!data->diag_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
2710 data->diag_rx_ep = ep_desc;
2711 continue;
2712 }
2713 }
2714
2715 if (!data->diag_tx_ep || !data->diag_rx_ep) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002716 bt_dev_err(hdev, "invalid diagnostic descriptors");
Marcel Holtmann9d08f502015-10-08 20:23:08 +02002717 return -ENODEV;
2718 }
2719
2720 return 0;
2721}
2722
2723static struct urb *alloc_diag_urb(struct hci_dev *hdev, bool enable)
2724{
2725 struct btusb_data *data = hci_get_drvdata(hdev);
2726 struct sk_buff *skb;
2727 struct urb *urb;
2728 unsigned int pipe;
2729
2730 if (!data->diag_tx_ep)
2731 return ERR_PTR(-ENODEV);
2732
2733 urb = usb_alloc_urb(0, GFP_KERNEL);
2734 if (!urb)
2735 return ERR_PTR(-ENOMEM);
2736
2737 skb = bt_skb_alloc(2, GFP_KERNEL);
2738 if (!skb) {
2739 usb_free_urb(urb);
2740 return ERR_PTR(-ENOMEM);
2741 }
2742
Johannes Berg634fef62017-06-16 14:29:24 +02002743 skb_put_u8(skb, 0xf0);
2744 skb_put_u8(skb, enable);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02002745
2746 pipe = usb_sndbulkpipe(data->udev, data->diag_tx_ep->bEndpointAddress);
2747
2748 usb_fill_bulk_urb(urb, data->udev, pipe,
2749 skb->data, skb->len, btusb_tx_complete, skb);
2750
2751 skb->dev = (void *)hdev;
2752
2753 return urb;
2754}
2755
2756static int btusb_bcm_set_diag(struct hci_dev *hdev, bool enable)
2757{
2758 struct btusb_data *data = hci_get_drvdata(hdev);
2759 struct urb *urb;
2760
2761 if (!data->diag)
2762 return -ENODEV;
2763
2764 if (!test_bit(HCI_RUNNING, &hdev->flags))
2765 return -ENETDOWN;
2766
2767 urb = alloc_diag_urb(hdev, enable);
2768 if (IS_ERR(urb))
2769 return PTR_ERR(urb);
2770
2771 return submit_or_queue_tx_urb(hdev, urb);
2772}
2773#endif
2774
Rajat Jainfd913ef2017-02-01 14:24:09 -08002775#ifdef CONFIG_PM
2776static irqreturn_t btusb_oob_wake_handler(int irq, void *priv)
2777{
2778 struct btusb_data *data = priv;
2779
2780 pm_wakeup_event(&data->udev->dev, 0);
Jeffy Chen017789f2017-02-24 14:24:29 +08002781 pm_system_wakeup();
Rajat Jainfd913ef2017-02-01 14:24:09 -08002782
2783 /* Disable only if not already disabled (keep it balanced) */
2784 if (test_and_clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags)) {
2785 disable_irq_nosync(irq);
2786 disable_irq_wake(irq);
2787 }
2788 return IRQ_HANDLED;
2789}
2790
2791static const struct of_device_id btusb_match_table[] = {
2792 { .compatible = "usb1286,204e" },
2793 { }
2794};
2795MODULE_DEVICE_TABLE(of, btusb_match_table);
2796
2797/* Use an oob wakeup pin? */
2798static int btusb_config_oob_wake(struct hci_dev *hdev)
2799{
2800 struct btusb_data *data = hci_get_drvdata(hdev);
2801 struct device *dev = &data->udev->dev;
2802 int irq, ret;
2803
2804 clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags);
2805
2806 if (!of_match_device(btusb_match_table, dev))
2807 return 0;
2808
2809 /* Move on if no IRQ specified */
2810 irq = of_irq_get_byname(dev->of_node, "wakeup");
2811 if (irq <= 0) {
2812 bt_dev_dbg(hdev, "%s: no OOB Wakeup IRQ in DT", __func__);
2813 return 0;
2814 }
2815
2816 ret = devm_request_irq(&hdev->dev, irq, btusb_oob_wake_handler,
2817 0, "OOB Wake-on-BT", data);
2818 if (ret) {
2819 bt_dev_err(hdev, "%s: IRQ request failed", __func__);
2820 return ret;
2821 }
2822
2823 ret = device_init_wakeup(dev, true);
2824 if (ret) {
2825 bt_dev_err(hdev, "%s: failed to init_wakeup", __func__);
2826 return ret;
2827 }
2828
2829 data->oob_wake_irq = irq;
2830 disable_irq(irq);
2831 bt_dev_info(hdev, "OOB Wake-on-BT configured at IRQ %u", irq);
2832 return 0;
2833}
2834#endif
2835
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002836static int btusb_probe(struct usb_interface *intf,
Marcel Holtmann89e75332014-09-16 04:44:50 +02002837 const struct usb_device_id *id)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002838{
2839 struct usb_endpoint_descriptor *ep_desc;
2840 struct btusb_data *data;
2841 struct hci_dev *hdev;
Marcel Holtmann22f8e9d2015-10-20 00:53:33 +02002842 unsigned ifnum_base;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002843 int i, err;
2844
2845 BT_DBG("intf %p id %p", intf, id);
2846
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002847 /* interface numbers are hardcoded in the spec */
Marcel Holtmann22f8e9d2015-10-20 00:53:33 +02002848 if (intf->cur_altsetting->desc.bInterfaceNumber != 0) {
2849 if (!(id->driver_info & BTUSB_IFNUM_2))
2850 return -ENODEV;
2851 if (intf->cur_altsetting->desc.bInterfaceNumber != 2)
2852 return -ENODEV;
2853 }
2854
2855 ifnum_base = intf->cur_altsetting->desc.bInterfaceNumber;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002856
2857 if (!id->driver_info) {
2858 const struct usb_device_id *match;
Marcel Holtmann89e75332014-09-16 04:44:50 +02002859
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002860 match = usb_match_id(intf, blacklist_table);
2861 if (match)
2862 id = match;
2863 }
2864
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002865 if (id->driver_info == BTUSB_IGNORE)
2866 return -ENODEV;
2867
Steven.Li2d25f8b2011-07-01 14:02:36 +08002868 if (id->driver_info & BTUSB_ATH3012) {
2869 struct usb_device *udev = interface_to_usbdev(intf);
2870
2871 /* Old firmware would otherwise let ath3k driver load
Derek Robsond98422c2017-07-22 13:47:07 +12002872 * patch and sysconfig files
2873 */
Steven.Li2d25f8b2011-07-01 14:02:36 +08002874 if (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x0001)
2875 return -ENODEV;
2876 }
2877
Sachin Kamat98921db2012-07-27 12:38:39 +05302878 data = devm_kzalloc(&intf->dev, sizeof(*data), GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002879 if (!data)
2880 return -ENOMEM;
2881
2882 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
2883 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
2884
2885 if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
2886 data->intr_ep = ep_desc;
2887 continue;
2888 }
2889
2890 if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
2891 data->bulk_tx_ep = ep_desc;
2892 continue;
2893 }
2894
2895 if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
2896 data->bulk_rx_ep = ep_desc;
2897 continue;
2898 }
2899 }
2900
Sachin Kamat98921db2012-07-27 12:38:39 +05302901 if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002902 return -ENODEV;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002903
Marcel Holtmann893ba542015-01-28 20:27:34 -08002904 if (id->driver_info & BTUSB_AMP) {
2905 data->cmdreq_type = USB_TYPE_CLASS | 0x01;
2906 data->cmdreq = 0x2b;
2907 } else {
2908 data->cmdreq_type = USB_TYPE_CLASS;
2909 data->cmdreq = 0x00;
2910 }
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002911
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002912 data->udev = interface_to_usbdev(intf);
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02002913 data->intf = intf;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002914
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002915 INIT_WORK(&data->work, btusb_work);
Oliver Neukum7bee5492009-08-24 23:44:59 +02002916 INIT_WORK(&data->waker, btusb_waker);
Marcel Holtmann803b5832014-09-16 08:00:29 +02002917 init_usb_anchor(&data->deferred);
2918 init_usb_anchor(&data->tx_anchor);
Oliver Neukum7bee5492009-08-24 23:44:59 +02002919 spin_lock_init(&data->txlock);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002920
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002921 init_usb_anchor(&data->intr_anchor);
2922 init_usb_anchor(&data->bulk_anchor);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02002923 init_usb_anchor(&data->isoc_anchor);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02002924 init_usb_anchor(&data->diag_anchor);
Marcel Holtmann803b5832014-09-16 08:00:29 +02002925 spin_lock_init(&data->rxlock);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002926
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002927 if (id->driver_info & BTUSB_INTEL_NEW) {
2928 data->recv_event = btusb_recv_event_intel;
2929 data->recv_bulk = btusb_recv_bulk_intel;
2930 set_bit(BTUSB_BOOTLOADER, &data->flags);
2931 } else {
2932 data->recv_event = hci_recv_frame;
2933 data->recv_bulk = btusb_recv_bulk;
2934 }
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +01002935
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002936 hdev = hci_alloc_dev();
Sachin Kamat98921db2012-07-27 12:38:39 +05302937 if (!hdev)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002938 return -ENOMEM;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002939
Marcel Holtmannc13854c2010-02-08 15:27:07 +01002940 hdev->bus = HCI_USB;
David Herrmann155961e2012-02-09 21:58:32 +01002941 hci_set_drvdata(hdev, data);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002942
Marcel Holtmann893ba542015-01-28 20:27:34 -08002943 if (id->driver_info & BTUSB_AMP)
2944 hdev->dev_type = HCI_AMP;
2945 else
Marcel Holtmannca8bee52016-07-05 14:30:14 +02002946 hdev->dev_type = HCI_PRIMARY;
Marcel Holtmann893ba542015-01-28 20:27:34 -08002947
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002948 data->hdev = hdev;
2949
2950 SET_HCIDEV_DEV(hdev, &intf->dev);
2951
Marcel Holtmann9f8f9622013-04-10 08:11:35 -07002952 hdev->open = btusb_open;
2953 hdev->close = btusb_close;
2954 hdev->flush = btusb_flush;
2955 hdev->send = btusb_send_frame;
2956 hdev->notify = btusb_notify;
2957
Rajat Jainfd913ef2017-02-01 14:24:09 -08002958#ifdef CONFIG_PM
2959 err = btusb_config_oob_wake(hdev);
2960 if (err)
2961 goto out_free_dev;
Rajat Jaina4ccc9e2017-02-01 14:24:10 -08002962
2963 /* Marvell devices may need a specific chip configuration */
2964 if (id->driver_info & BTUSB_MARVELL && data->oob_wake_irq) {
2965 err = marvell_config_oob_wake(hdev);
2966 if (err)
2967 goto out_free_dev;
2968 }
Rajat Jainfd913ef2017-02-01 14:24:09 -08002969#endif
Szymon Janc418678b2016-09-01 17:22:37 +02002970 if (id->driver_info & BTUSB_CW6622)
2971 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
2972
Marcel Holtmann6c9d4352015-10-17 14:39:27 +02002973 if (id->driver_info & BTUSB_BCM2045)
2974 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
2975
Marcel Holtmann9f8f9622013-04-10 08:11:35 -07002976 if (id->driver_info & BTUSB_BCM92035)
2977 hdev->setup = btusb_setup_bcm92035;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002978
Marcel Holtmannc2bfb102015-04-05 22:52:14 -07002979#ifdef CONFIG_BT_HCIBTUSB_BCM
Marcel Holtmannabbaf502014-07-02 00:53:48 +02002980 if (id->driver_info & BTUSB_BCM_PATCHRAM) {
Marcel Holtmann49a5f782015-10-20 02:30:48 +02002981 hdev->manufacturer = 15;
Marcel Holtmannc2bfb102015-04-05 22:52:14 -07002982 hdev->setup = btbcm_setup_patchram;
Marcel Holtmann9d08f502015-10-08 20:23:08 +02002983 hdev->set_diag = btusb_bcm_set_diag;
Marcel Holtmann1df1f592015-04-05 22:52:11 -07002984 hdev->set_bdaddr = btbcm_set_bdaddr;
Marcel Holtmann9d08f502015-10-08 20:23:08 +02002985
2986 /* Broadcom LM_DIAG Interface numbers are hardcoded */
Marcel Holtmann22f8e9d2015-10-20 00:53:33 +02002987 data->diag = usb_ifnum_to_if(data->udev, ifnum_base + 2);
Marcel Holtmannabbaf502014-07-02 00:53:48 +02002988 }
Petri Gynther10d4c672014-05-08 15:50:01 -07002989
Marcel Holtmann9d08f502015-10-08 20:23:08 +02002990 if (id->driver_info & BTUSB_BCM_APPLE) {
Marcel Holtmann49a5f782015-10-20 02:30:48 +02002991 hdev->manufacturer = 15;
Marcel Holtmannc2bfb102015-04-05 22:52:14 -07002992 hdev->setup = btbcm_setup_apple;
Marcel Holtmann9d08f502015-10-08 20:23:08 +02002993 hdev->set_diag = btusb_bcm_set_diag;
2994
2995 /* Broadcom LM_DIAG Interface numbers are hardcoded */
Marcel Holtmann22f8e9d2015-10-20 00:53:33 +02002996 data->diag = usb_ifnum_to_if(data->udev, ifnum_base + 2);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02002997 }
Marcel Holtmannc2bfb102015-04-05 22:52:14 -07002998#endif
Marcel Holtmann17b27722015-03-22 15:52:38 +01002999
Marcel Holtmanncb8d6592014-07-02 11:25:25 +02003000 if (id->driver_info & BTUSB_INTEL) {
Marcel Holtmann49a5f782015-10-20 02:30:48 +02003001 hdev->manufacturer = 2;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07003002 hdev->setup = btusb_setup_intel;
Tedd Ho-Jeong Anbfbd45e2015-02-13 09:20:52 -08003003 hdev->shutdown = btusb_shutdown_intel;
Marcel Holtmann3e247672015-10-17 16:00:28 +02003004 hdev->set_diag = btintel_set_diag_mfg;
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07003005 hdev->set_bdaddr = btintel_set_bdaddr;
Jakub Pawlowskic33fb9b2015-01-30 18:55:58 -08003006 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
Jakub Pawlowskic1154842015-03-17 09:04:16 -07003007 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
Marcel Holtmann3e247672015-10-17 16:00:28 +02003008 set_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks);
Marcel Holtmanncb8d6592014-07-02 11:25:25 +02003009 }
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07003010
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08003011 if (id->driver_info & BTUSB_INTEL_NEW) {
Marcel Holtmann49a5f782015-10-20 02:30:48 +02003012 hdev->manufacturer = 2;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08003013 hdev->send = btusb_send_frame_intel;
3014 hdev->setup = btusb_setup_intel_new;
Marcel Holtmanneeb6abe2015-07-05 14:37:39 +02003015 hdev->hw_error = btintel_hw_error;
Marcel Holtmann6d2e50d2015-10-09 14:42:08 +02003016 hdev->set_diag = btintel_set_diag;
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07003017 hdev->set_bdaddr = btintel_set_bdaddr;
Marcel Holtmannb970c5b2015-02-01 23:57:18 -08003018 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
Marcel Holtmannd8270fb2015-10-17 16:00:27 +02003019 set_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08003020 }
3021
Amitkumar Karwarae8df492014-07-18 14:47:06 -07003022 if (id->driver_info & BTUSB_MARVELL)
3023 hdev->set_bdaddr = btusb_set_bdaddr_marvell;
3024
Marcel Holtmann661cf882015-01-02 23:35:20 -08003025 if (id->driver_info & BTUSB_SWAVE) {
3026 set_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks);
Marcel Holtmannd57dbe72014-12-26 04:42:33 +01003027 set_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS, &hdev->quirks);
Marcel Holtmann661cf882015-01-02 23:35:20 -08003028 }
Marcel Holtmannd57dbe72014-12-26 04:42:33 +01003029
Marcel Holtmanne4c534b2015-10-21 01:31:45 +02003030 if (id->driver_info & BTUSB_INTEL_BOOT) {
3031 hdev->manufacturer = 2;
Marcel Holtmann40df7832014-07-06 13:29:58 +02003032 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
Marcel Holtmanne4c534b2015-10-21 01:31:45 +02003033 }
Marcel Holtmann40df7832014-07-06 13:29:58 +02003034
Jakub Pawlowski79f0c872015-01-29 10:38:34 -08003035 if (id->driver_info & BTUSB_ATH3012) {
Toshi Kikuchi58592232014-12-12 10:58:05 -08003036 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
Jakub Pawlowski3d50d512015-03-17 09:04:15 -07003037 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
Jakub Pawlowski79f0c872015-01-29 10:38:34 -08003038 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
3039 }
Toshi Kikuchi58592232014-12-12 10:58:05 -08003040
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003041 if (id->driver_info & BTUSB_QCA_ROME) {
3042 data->setup_on_usb = btusb_setup_qca;
3043 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
Hans de Goede61f5ace2018-01-08 10:44:16 +01003044
3045 /* QCA Rome devices lose their updated firmware over suspend,
3046 * but the USB hub doesn't notice any status change.
3047 * explicitly request a device reset on resume.
3048 */
3049 interface_to_usbdev(intf)->quirks |= USB_QUIRK_RESET_RESUME;
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003050 }
3051
Carlo Caionedb33c772015-05-14 10:49:09 +02003052#ifdef CONFIG_BT_HCIBTUSB_RTL
Daniel Drake04b8c812015-05-21 08:23:50 -06003053 if (id->driver_info & BTUSB_REALTEK) {
Carlo Caionedb33c772015-05-14 10:49:09 +02003054 hdev->setup = btrtl_setup_realtek;
Daniel Drake04b8c812015-05-21 08:23:50 -06003055
3056 /* Realtek devices lose their updated firmware over suspend,
3057 * but the USB hub doesn't notice any status change.
3058 * Explicitly request a device reset on resume.
3059 */
Hans de Goede61f5ace2018-01-08 10:44:16 +01003060 interface_to_usbdev(intf)->quirks |= USB_QUIRK_RESET_RESUME;
Daniel Drake04b8c812015-05-21 08:23:50 -06003061 }
Carlo Caionedb33c772015-05-14 10:49:09 +02003062#endif
Daniel Drakea2698a92015-04-16 14:09:55 -06003063
Marcel Holtmann893ba542015-01-28 20:27:34 -08003064 if (id->driver_info & BTUSB_AMP) {
3065 /* AMP controllers do not support SCO packets */
3066 data->isoc = NULL;
3067 } else {
Marcel Holtmann22f8e9d2015-10-20 00:53:33 +02003068 /* Interface orders are hardcoded in the specification */
3069 data->isoc = usb_ifnum_to_if(data->udev, ifnum_base + 1);
Marcel Holtmann459232f2017-10-24 19:42:45 +02003070 data->isoc_ifnum = ifnum_base + 1;
Marcel Holtmann893ba542015-01-28 20:27:34 -08003071 }
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02003072
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01003073 if (!reset)
Szymon Janca6c511c2012-05-23 12:35:46 +02003074 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02003075
3076 if (force_scofix || id->driver_info & BTUSB_WRONG_SCO_MTU) {
3077 if (!disable_scofix)
3078 set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
3079 }
3080
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02003081 if (id->driver_info & BTUSB_BROKEN_ISOC)
3082 data->isoc = NULL;
3083
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01003084 if (id->driver_info & BTUSB_DIGIANSWER) {
3085 data->cmdreq_type = USB_TYPE_VENDOR;
Szymon Janca6c511c2012-05-23 12:35:46 +02003086 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01003087 }
3088
3089 if (id->driver_info & BTUSB_CSR) {
3090 struct usb_device *udev = data->udev;
Marcel Holtmann81cac642014-01-03 03:02:36 -08003091 u16 bcdDevice = le16_to_cpu(udev->descriptor.bcdDevice);
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01003092
3093 /* Old firmware would otherwise execute USB reset */
Marcel Holtmann81cac642014-01-03 03:02:36 -08003094 if (bcdDevice < 0x117)
Szymon Janca6c511c2012-05-23 12:35:46 +02003095 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmann81cac642014-01-03 03:02:36 -08003096
3097 /* Fake CSR devices with broken commands */
Johan Hedberg6cafcd92015-08-30 21:47:21 +03003098 if (bcdDevice <= 0x100 || bcdDevice == 0x134)
Marcel Holtmann81cac642014-01-03 03:02:36 -08003099 hdev->setup = btusb_setup_csr;
Jakub Pawlowski49c989a2015-03-17 09:04:17 -07003100
3101 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01003102 }
3103
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02003104 if (id->driver_info & BTUSB_SNIFFER) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02003105 struct usb_device *udev = data->udev;
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02003106
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01003107 /* New sniffer firmware has crippled HCI interface */
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02003108 if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
3109 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
3110 }
3111
Marcel Holtmann3a5ef202014-07-06 14:53:54 +02003112 if (id->driver_info & BTUSB_INTEL_BOOT) {
3113 /* A bug in the bootloader causes that interrupt interface is
3114 * only enabled after receiving SetInterface(0, AltSetting=0).
3115 */
3116 err = usb_set_interface(data->udev, 0, 0);
3117 if (err < 0) {
3118 BT_ERR("failed to set interface 0, alt 0 %d", err);
Rajat Jain10ab1332017-02-01 14:24:08 -08003119 goto out_free_dev;
Marcel Holtmann3a5ef202014-07-06 14:53:54 +02003120 }
3121 }
3122
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02003123 if (data->isoc) {
3124 err = usb_driver_claim_interface(&btusb_driver,
Marcel Holtmann89e75332014-09-16 04:44:50 +02003125 data->isoc, data);
Rajat Jain10ab1332017-02-01 14:24:08 -08003126 if (err < 0)
3127 goto out_free_dev;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02003128 }
3129
Marcel Holtmann9d08f502015-10-08 20:23:08 +02003130#ifdef CONFIG_BT_HCIBTUSB_BCM
3131 if (data->diag) {
3132 if (!usb_driver_claim_interface(&btusb_driver,
3133 data->diag, data))
3134 __set_diag_interface(hdev);
3135 else
3136 data->diag = NULL;
3137 }
3138#endif
3139
Hans de Goedeeff2d682017-11-13 14:44:16 +01003140 if (enable_autosuspend)
3141 usb_enable_autosuspend(data->udev);
3142
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003143 err = hci_register_dev(hdev);
Rajat Jain10ab1332017-02-01 14:24:08 -08003144 if (err < 0)
3145 goto out_free_dev;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003146
3147 usb_set_intfdata(intf, data);
3148
3149 return 0;
Rajat Jain10ab1332017-02-01 14:24:08 -08003150
3151out_free_dev:
3152 hci_free_dev(hdev);
3153 return err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003154}
3155
3156static void btusb_disconnect(struct usb_interface *intf)
3157{
3158 struct btusb_data *data = usb_get_intfdata(intf);
3159 struct hci_dev *hdev;
3160
3161 BT_DBG("intf %p", intf);
3162
3163 if (!data)
3164 return;
3165
3166 hdev = data->hdev;
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02003167 usb_set_intfdata(data->intf, NULL);
3168
3169 if (data->isoc)
3170 usb_set_intfdata(data->isoc, NULL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003171
Marcel Holtmann9d08f502015-10-08 20:23:08 +02003172 if (data->diag)
3173 usb_set_intfdata(data->diag, NULL);
3174
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003175 hci_unregister_dev(hdev);
3176
Marcel Holtmann9d08f502015-10-08 20:23:08 +02003177 if (intf == data->intf) {
3178 if (data->isoc)
3179 usb_driver_release_interface(&btusb_driver, data->isoc);
3180 if (data->diag)
3181 usb_driver_release_interface(&btusb_driver, data->diag);
3182 } else if (intf == data->isoc) {
3183 if (data->diag)
3184 usb_driver_release_interface(&btusb_driver, data->diag);
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02003185 usb_driver_release_interface(&btusb_driver, data->intf);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02003186 } else if (intf == data->diag) {
3187 usb_driver_release_interface(&btusb_driver, data->intf);
3188 if (data->isoc)
3189 usb_driver_release_interface(&btusb_driver, data->isoc);
3190 }
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02003191
Rajat Jainfd913ef2017-02-01 14:24:09 -08003192 if (data->oob_wake_irq)
3193 device_init_wakeup(&data->udev->dev, false);
3194
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003195 hci_free_dev(hdev);
3196}
3197
Oliver Neukum7bee5492009-08-24 23:44:59 +02003198#ifdef CONFIG_PM
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003199static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
3200{
3201 struct btusb_data *data = usb_get_intfdata(intf);
3202
3203 BT_DBG("intf %p", intf);
3204
3205 if (data->suspend_count++)
3206 return 0;
3207
Oliver Neukum7bee5492009-08-24 23:44:59 +02003208 spin_lock_irq(&data->txlock);
Alan Stern5b1b0b82011-08-19 23:49:48 +02003209 if (!(PMSG_IS_AUTO(message) && data->tx_in_flight)) {
Oliver Neukum7bee5492009-08-24 23:44:59 +02003210 set_bit(BTUSB_SUSPENDING, &data->flags);
3211 spin_unlock_irq(&data->txlock);
3212 } else {
3213 spin_unlock_irq(&data->txlock);
3214 data->suspend_count--;
3215 return -EBUSY;
3216 }
3217
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003218 cancel_work_sync(&data->work);
3219
Oliver Neukum7bee5492009-08-24 23:44:59 +02003220 btusb_stop_traffic(data);
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003221 usb_kill_anchored_urbs(&data->tx_anchor);
3222
Rajat Jainfd913ef2017-02-01 14:24:09 -08003223 if (data->oob_wake_irq && device_may_wakeup(&data->udev->dev)) {
3224 set_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags);
3225 enable_irq_wake(data->oob_wake_irq);
3226 enable_irq(data->oob_wake_irq);
3227 }
3228
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003229 return 0;
3230}
3231
Oliver Neukum7bee5492009-08-24 23:44:59 +02003232static void play_deferred(struct btusb_data *data)
3233{
3234 struct urb *urb;
3235 int err;
3236
3237 while ((urb = usb_get_from_anchor(&data->deferred))) {
Jeffy Chen19cfe912017-07-20 18:53:50 +08003238 usb_anchor_urb(urb, &data->tx_anchor);
3239
Oliver Neukum7bee5492009-08-24 23:44:59 +02003240 err = usb_submit_urb(urb, GFP_ATOMIC);
Jeffy Chen19cfe912017-07-20 18:53:50 +08003241 if (err < 0) {
3242 if (err != -EPERM && err != -ENODEV)
3243 BT_ERR("%s urb %p submission failed (%d)",
3244 data->hdev->name, urb, -err);
3245 kfree(urb->setup_packet);
3246 usb_unanchor_urb(urb);
3247 usb_free_urb(urb);
Oliver Neukum7bee5492009-08-24 23:44:59 +02003248 break;
Jeffy Chen19cfe912017-07-20 18:53:50 +08003249 }
Oliver Neukum7bee5492009-08-24 23:44:59 +02003250
3251 data->tx_in_flight++;
Jeffy Chen19cfe912017-07-20 18:53:50 +08003252 usb_free_urb(urb);
Oliver Neukum7bee5492009-08-24 23:44:59 +02003253 }
Jeffy Chen19cfe912017-07-20 18:53:50 +08003254
3255 /* Cleanup the rest deferred urbs. */
3256 while ((urb = usb_get_from_anchor(&data->deferred))) {
3257 kfree(urb->setup_packet);
3258 usb_free_urb(urb);
3259 }
Oliver Neukum7bee5492009-08-24 23:44:59 +02003260}
3261
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003262static int btusb_resume(struct usb_interface *intf)
3263{
3264 struct btusb_data *data = usb_get_intfdata(intf);
3265 struct hci_dev *hdev = data->hdev;
Oliver Neukum7bee5492009-08-24 23:44:59 +02003266 int err = 0;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003267
3268 BT_DBG("intf %p", intf);
3269
3270 if (--data->suspend_count)
3271 return 0;
3272
Rajat Jainfd913ef2017-02-01 14:24:09 -08003273 /* Disable only if not already disabled (keep it balanced) */
3274 if (test_and_clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags)) {
3275 disable_irq(data->oob_wake_irq);
3276 disable_irq_wake(data->oob_wake_irq);
3277 }
3278
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003279 if (!test_bit(HCI_RUNNING, &hdev->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +02003280 goto done;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003281
3282 if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
3283 err = btusb_submit_intr_urb(hdev, GFP_NOIO);
3284 if (err < 0) {
3285 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02003286 goto failed;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003287 }
3288 }
3289
3290 if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) {
Marcel Holtmann43c2e572009-02-04 17:41:38 +01003291 err = btusb_submit_bulk_urb(hdev, GFP_NOIO);
3292 if (err < 0) {
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003293 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02003294 goto failed;
3295 }
3296
3297 btusb_submit_bulk_urb(hdev, GFP_NOIO);
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003298 }
3299
3300 if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
3301 if (btusb_submit_isoc_urb(hdev, GFP_NOIO) < 0)
3302 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
3303 else
3304 btusb_submit_isoc_urb(hdev, GFP_NOIO);
3305 }
3306
Oliver Neukum7bee5492009-08-24 23:44:59 +02003307 spin_lock_irq(&data->txlock);
3308 play_deferred(data);
3309 clear_bit(BTUSB_SUSPENDING, &data->flags);
3310 spin_unlock_irq(&data->txlock);
3311 schedule_work(&data->work);
3312
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003313 return 0;
Oliver Neukum7bee5492009-08-24 23:44:59 +02003314
3315failed:
3316 usb_scuttle_anchored_urbs(&data->deferred);
3317done:
3318 spin_lock_irq(&data->txlock);
3319 clear_bit(BTUSB_SUSPENDING, &data->flags);
3320 spin_unlock_irq(&data->txlock);
3321
3322 return err;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003323}
Oliver Neukum7bee5492009-08-24 23:44:59 +02003324#endif
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003325
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003326static struct usb_driver btusb_driver = {
3327 .name = "btusb",
3328 .probe = btusb_probe,
3329 .disconnect = btusb_disconnect,
Oliver Neukum7bee5492009-08-24 23:44:59 +02003330#ifdef CONFIG_PM
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003331 .suspend = btusb_suspend,
3332 .resume = btusb_resume,
Oliver Neukum7bee5492009-08-24 23:44:59 +02003333#endif
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003334 .id_table = btusb_table,
Oliver Neukum7bee5492009-08-24 23:44:59 +02003335 .supports_autosuspend = 1,
Sarah Sharpe1f12eb2012-04-23 10:08:51 -07003336 .disable_hub_initiated_lpm = 1,
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003337};
3338
Greg Kroah-Hartman93f15082011-11-18 09:47:34 -08003339module_usb_driver(btusb_driver);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003340
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02003341module_param(disable_scofix, bool, 0644);
3342MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
3343
3344module_param(force_scofix, bool, 0644);
3345MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
3346
Hans de Goedeeff2d682017-11-13 14:44:16 +01003347module_param(enable_autosuspend, bool, 0644);
3348MODULE_PARM_DESC(enable_autosuspend, "Enable USB autosuspend by default");
3349
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02003350module_param(reset, bool, 0644);
3351MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
3352
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003353MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
3354MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
3355MODULE_VERSION(VERSION);
3356MODULE_LICENSE("GPL");