blob: 041c17e0c668cf7e88c47395f9b093c08b2ec737 [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
Hans de Goede1fdb9262018-02-20 09:06:18 +010024#include <linux/dmi.h>
Marcel Holtmann5e23b922007-10-20 14:12:34 +020025#include <linux/module.h>
Marcel Holtmann5e23b922007-10-20 14:12:34 +020026#include <linux/usb.h>
Hans de Goede61f5ace2018-01-08 10:44:16 +010027#include <linux/usb/quirks.h>
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -070028#include <linux/firmware.h>
Rajat Jainfd913ef2017-02-01 14:24:09 -080029#include <linux/of_device.h>
30#include <linux/of_irq.h>
Jeffy Chen017789f2017-02-24 14:24:29 +080031#include <linux/suspend.h>
Daniel Drakea2698a92015-04-16 14:09:55 -060032#include <asm/unaligned.h>
Marcel Holtmann5e23b922007-10-20 14:12:34 +020033
34#include <net/bluetooth/bluetooth.h>
35#include <net/bluetooth/hci_core.h>
36
Marcel Holtmann4185a0f2015-04-06 00:52:12 -070037#include "btintel.h"
Marcel Holtmann1df1f592015-04-05 22:52:11 -070038#include "btbcm.h"
Carlo Caionedb33c772015-05-14 10:49:09 +020039#include "btrtl.h"
Marcel Holtmann1df1f592015-04-05 22:52:11 -070040
Marcel Holtmann34dced9b2015-04-05 22:52:16 -070041#define VERSION "0.8"
Marcel Holtmanncfeb4142008-08-07 22:26:56 +020042
Rusty Russell90ab5ee2012-01-13 09:32:20 +103043static bool disable_scofix;
44static bool force_scofix;
Hans de Goedeeff2d682017-11-13 14:44:16 +010045static bool enable_autosuspend = IS_ENABLED(CONFIG_BT_HCIBTUSB_AUTOSUSPEND);
Marcel Holtmann7a9d4022008-11-30 12:17:26 +010046
Shailendra Verma917a3332015-05-25 23:53:40 +053047static bool reset = true;
Marcel Holtmanncfeb4142008-08-07 22:26:56 +020048
49static struct usb_driver btusb_driver;
50
51#define BTUSB_IGNORE 0x01
Marcel Holtmann7a9d4022008-11-30 12:17:26 +010052#define BTUSB_DIGIANSWER 0x02
53#define BTUSB_CSR 0x04
54#define BTUSB_SNIFFER 0x08
55#define BTUSB_BCM92035 0x10
56#define BTUSB_BROKEN_ISOC 0x20
57#define BTUSB_WRONG_SCO_MTU 0x40
Steven.Li2d25f8b2011-07-01 14:02:36 +080058#define BTUSB_ATH3012 0x80
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -070059#define BTUSB_INTEL 0x100
Marcel Holtmann40df7832014-07-06 13:29:58 +020060#define BTUSB_INTEL_BOOT 0x200
61#define BTUSB_BCM_PATCHRAM 0x400
Amitkumar Karwarae8df492014-07-18 14:47:06 -070062#define BTUSB_MARVELL 0x800
Marcel Holtmann4fcef8e2015-01-01 17:34:37 -080063#define BTUSB_SWAVE 0x1000
Marcel Holtmanncda0dd72015-01-26 21:33:48 -080064#define BTUSB_INTEL_NEW 0x2000
Marcel Holtmann893ba542015-01-28 20:27:34 -080065#define BTUSB_AMP 0x4000
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +000066#define BTUSB_QCA_ROME 0x8000
Marcel Holtmann17b27722015-03-22 15:52:38 +010067#define BTUSB_BCM_APPLE 0x10000
Daniel Drakea2698a92015-04-16 14:09:55 -060068#define BTUSB_REALTEK 0x20000
Marcel Holtmann6c9d4352015-10-17 14:39:27 +020069#define BTUSB_BCM2045 0x40000
Marcel Holtmann22f8e9d2015-10-20 00:53:33 +020070#define BTUSB_IFNUM_2 0x80000
Szymon Janc418678b2016-09-01 17:22:37 +020071#define BTUSB_CW6622 0x100000
Marcel Holtmann5e23b922007-10-20 14:12:34 +020072
Marcel Holtmann54265202013-10-11 07:46:18 -070073static const struct usb_device_id btusb_table[] = {
Marcel Holtmann5e23b922007-10-20 14:12:34 +020074 /* Generic Bluetooth USB device */
75 { USB_DEVICE_INFO(0xe0, 0x01, 0x01) },
76
Marcel Holtmann893ba542015-01-28 20:27:34 -080077 /* Generic Bluetooth AMP device */
78 { USB_DEVICE_INFO(0xe0, 0x01, 0x04), .driver_info = BTUSB_AMP },
79
Daniel Draked63b2822015-07-17 11:12:25 -060080 /* Generic Bluetooth USB interface */
81 { USB_INTERFACE_INFO(0xe0, 0x01, 0x01) },
82
Henrik Rydberg1fa65352012-08-25 19:28:06 +020083 /* Apple-specific (Broadcom) devices */
Marcel Holtmann17b27722015-03-22 15:52:38 +010084 { USB_VENDOR_AND_INTERFACE_INFO(0x05ac, 0xff, 0x01, 0x01),
Marcel Holtmann22f8e9d2015-10-20 00:53:33 +020085 .driver_info = BTUSB_BCM_APPLE | BTUSB_IFNUM_2 },
Henrik Rydberg1fa65352012-08-25 19:28:06 +020086
Cho, Yu-Chen178c0592013-06-04 21:40:26 +080087 /* MediaTek MT76x0E */
88 { USB_DEVICE(0x0e8d, 0x763f) },
89
Oliver Neukumc510eae2011-09-21 11:41:45 +020090 /* Broadcom SoftSailing reporting vendor specific */
Don Zickus2e8b50632012-03-28 16:41:11 -040091 { USB_DEVICE(0x0a5c, 0x21e1) },
Oliver Neukumc510eae2011-09-21 11:41:45 +020092
Nobuhiro Iwamatsu3cd01972010-08-20 16:24:07 +090093 /* Apple MacBookPro 7,1 */
94 { USB_DEVICE(0x05ac, 0x8213) },
95
Cyril Lacoux0a79f672010-07-14 10:29:27 +040096 /* Apple iMac11,1 */
97 { USB_DEVICE(0x05ac, 0x8215) },
98
Nobuhiro Iwamatsu9c047152010-08-20 16:24:06 +090099 /* Apple MacBookPro6,2 */
100 { USB_DEVICE(0x05ac, 0x8218) },
101
Edgar (gimli) Hucek3e3ede72010-11-04 08:04:33 +0100102 /* Apple MacBookAir3,1, MacBookAir3,2 */
103 { USB_DEVICE(0x05ac, 0x821b) },
104
Pieter-Augustijn Van Malleghema63b7232011-09-07 02:28:10 -0400105 /* Apple MacBookAir4,1 */
106 { USB_DEVICE(0x05ac, 0x821f) },
107
Marc-Antoine Perennou88d377b2011-03-24 14:51:21 -0300108 /* Apple MacBookPro8,2 */
109 { USB_DEVICE(0x05ac, 0x821a) },
110
Jurgen Kramerf78b68262011-09-04 18:01:42 +0200111 /* Apple MacMini5,1 */
112 { USB_DEVICE(0x05ac, 0x8281) },
113
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200114 /* AVM BlueFRITZ! USB v2.0 */
Marcel Holtmann4fcef8e2015-01-01 17:34:37 -0800115 { USB_DEVICE(0x057c, 0x3800), .driver_info = BTUSB_SWAVE },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200116
117 /* Bluetooth Ultraport Module from IBM */
118 { USB_DEVICE(0x04bf, 0x030a) },
119
120 /* ALPS Modules with non-standard id */
121 { USB_DEVICE(0x044e, 0x3001) },
122 { USB_DEVICE(0x044e, 0x3002) },
123
124 /* Ericsson with non-standard id */
125 { USB_DEVICE(0x0bdb, 0x1002) },
126
127 /* Canyon CN-BTU1 with HID interfaces */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100128 { USB_DEVICE(0x0c10, 0x0000) },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200129
Wen-chien Jesse Sungd13431c2011-11-08 14:30:22 +0800130 /* Broadcom BCM20702A0 */
131 { USB_DEVICE(0x413c, 0x8197) },
132
Marcel Holtmannd049f4e2015-01-26 20:35:32 -0800133 /* Broadcom BCM20702B0 (Dynex/Insignia) */
134 { USB_DEVICE(0x19ff, 0x0239), .driver_info = BTUSB_BCM_PATCHRAM },
135
Santtu Rekilä2faf71c2015-10-05 15:45:27 +0300136 /* Broadcom BCM43142A0 (Foxconn/Lenovo) */
Dmitry Tunin628c26b2017-07-06 14:41:13 +0300137 { USB_VENDOR_AND_INTERFACE_INFO(0x105b, 0xff, 0x01, 0x01),
138 .driver_info = BTUSB_BCM_PATCHRAM },
Santtu Rekilä2faf71c2015-10-05 15:45:27 +0300139
Christoph Haaga57bac42017-02-10 14:02:45 +0100140 /* Broadcom BCM920703 (HTC Vive) */
141 { USB_VENDOR_AND_INTERFACE_INFO(0x0bb4, 0xff, 0x01, 0x01),
142 .driver_info = BTUSB_BCM_PATCHRAM },
143
Steven Harms98514032012-04-13 14:45:55 -0400144 /* Foxconn - Hon Hai */
Heinrich Siebmanns6029ddc2014-12-03 19:32:22 +0100145 { USB_VENDOR_AND_INTERFACE_INFO(0x0489, 0xff, 0x01, 0x01),
146 .driver_info = BTUSB_BCM_PATCHRAM },
Steven Harms98514032012-04-13 14:45:55 -0400147
Matej Dubovy8f0c3042015-02-02 18:50:14 +0100148 /* Lite-On Technology - Broadcom based */
149 { USB_VENDOR_AND_INTERFACE_INFO(0x04ca, 0xff, 0x01, 0x01),
150 .driver_info = BTUSB_BCM_PATCHRAM },
151
Andy Shevchenko0b880062014-02-18 18:26:19 +0200152 /* Broadcom devices with vendor specific id */
Petri Gynther10d4c672014-05-08 15:50:01 -0700153 { USB_VENDOR_AND_INTERFACE_INFO(0x0a5c, 0xff, 0x01, 0x01),
154 .driver_info = BTUSB_BCM_PATCHRAM },
Gustavo Padovan92c385f2012-08-06 15:36:49 -0300155
Marcel Holtmannc2aef6e2014-07-21 14:02:33 +0200156 /* ASUSTek Computer - Broadcom based */
Rick Dunn9a5abdaa2015-01-17 05:29:12 +0100157 { USB_VENDOR_AND_INTERFACE_INFO(0x0b05, 0xff, 0x01, 0x01),
158 .driver_info = BTUSB_BCM_PATCHRAM },
Marcel Holtmannc2aef6e2014-07-21 14:02:33 +0200159
Ken O'Brien5bcecf32013-09-21 19:14:43 +0100160 /* Belkin F8065bf - Broadcom based */
Marcel Holtmann6331c682015-03-27 15:11:41 -0700161 { USB_VENDOR_AND_INTERFACE_INFO(0x050d, 0xff, 0x01, 0x01),
162 .driver_info = BTUSB_BCM_PATCHRAM },
Ken O'Brien5bcecf32013-09-21 19:14:43 +0100163
Jurgen Kramer9113bfd2014-02-15 12:01:09 +0100164 /* IMC Networks - Broadcom based */
Marcel Holtmann6331c682015-03-27 15:11:41 -0700165 { USB_VENDOR_AND_INTERFACE_INFO(0x13d3, 0xff, 0x01, 0x01),
166 .driver_info = BTUSB_BCM_PATCHRAM },
Jurgen Kramer9113bfd2014-02-15 12:01:09 +0100167
Wen-chien Jesse Sungfdfddc62017-01-10 15:41:13 +0800168 /* Dell Computer - Broadcom based */
169 { USB_VENDOR_AND_INTERFACE_INFO(0x413c, 0xff, 0x01, 0x01),
170 .driver_info = BTUSB_BCM_PATCHRAM },
171
Dmitry Tunin1623d0b2015-12-05 14:09:36 +0300172 /* Toshiba Corp - Broadcom based */
173 { USB_VENDOR_AND_INTERFACE_INFO(0x0930, 0xff, 0x01, 0x01),
174 .driver_info = BTUSB_BCM_PATCHRAM },
175
Marcel Holtmann40df7832014-07-06 13:29:58 +0200176 /* Intel Bluetooth USB Bootloader (RAM module) */
Marcel Holtmannd92f2df2014-07-06 14:53:55 +0200177 { USB_DEVICE(0x8087, 0x0a5a),
178 .driver_info = BTUSB_INTEL_BOOT | BTUSB_BROKEN_ISOC },
Marcel Holtmann40df7832014-07-06 13:29:58 +0200179
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200180 { } /* Terminating entry */
181};
182
183MODULE_DEVICE_TABLE(usb, btusb_table);
184
Marcel Holtmann54265202013-10-11 07:46:18 -0700185static const struct usb_device_id blacklist_table[] = {
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200186 /* CSR BlueCore devices */
187 { USB_DEVICE(0x0a12, 0x0001), .driver_info = BTUSB_CSR },
188
189 /* Broadcom BCM2033 without firmware */
190 { USB_DEVICE(0x0a5c, 0x2033), .driver_info = BTUSB_IGNORE },
191
Marcel Holtmann6c9d4352015-10-17 14:39:27 +0200192 /* Broadcom BCM2045 devices */
193 { USB_DEVICE(0x0a5c, 0x2045), .driver_info = BTUSB_BCM2045 },
194
Bala Shanmugambe931122010-11-26 17:35:46 +0530195 /* Atheros 3011 with sflash firmware */
Andy Shevchenko0b880062014-02-18 18:26:19 +0200196 { USB_DEVICE(0x0489, 0xe027), .driver_info = BTUSB_IGNORE },
197 { USB_DEVICE(0x0489, 0xe03d), .driver_info = BTUSB_IGNORE },
Alexander Ploumistos2eeff0b2015-02-13 21:05:11 +0200198 { USB_DEVICE(0x04f2, 0xaff1), .driver_info = BTUSB_IGNORE },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200199 { USB_DEVICE(0x0930, 0x0215), .driver_info = BTUSB_IGNORE },
Bala Shanmugambe931122010-11-26 17:35:46 +0530200 { USB_DEVICE(0x0cf3, 0x3002), .driver_info = BTUSB_IGNORE },
Marek Vasut6eda5412012-06-08 14:32:50 +0200201 { USB_DEVICE(0x0cf3, 0xe019), .driver_info = BTUSB_IGNORE },
Andy Ross2a7bccc2011-05-09 16:11:16 -0700202 { USB_DEVICE(0x13d3, 0x3304), .driver_info = BTUSB_IGNORE },
Bala Shanmugambe931122010-11-26 17:35:46 +0530203
Cho, Yu-Chen509e7862011-01-26 17:10:59 +0800204 /* Atheros AR9285 Malbec with sflash firmware */
205 { USB_DEVICE(0x03f0, 0x311d), .driver_info = BTUSB_IGNORE },
206
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530207 /* Atheros 3012 with sflash firmware */
Andy Shevchenko0b880062014-02-18 18:26:19 +0200208 { USB_DEVICE(0x0489, 0xe04d), .driver_info = BTUSB_ATH3012 },
209 { USB_DEVICE(0x0489, 0xe04e), .driver_info = BTUSB_ATH3012 },
210 { USB_DEVICE(0x0489, 0xe056), .driver_info = BTUSB_ATH3012 },
211 { USB_DEVICE(0x0489, 0xe057), .driver_info = BTUSB_ATH3012 },
212 { USB_DEVICE(0x0489, 0xe05f), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin692c0622015-06-06 20:25:40 +0300213 { USB_DEVICE(0x0489, 0xe076), .driver_info = BTUSB_ATH3012 },
Anantha Krishnan4b552bc2014-10-06 16:31:49 +0530214 { USB_DEVICE(0x0489, 0xe078), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin28c971d2016-02-10 00:49:11 +0300215 { USB_DEVICE(0x0489, 0xe095), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200216 { USB_DEVICE(0x04c5, 0x1330), .driver_info = BTUSB_ATH3012 },
217 { USB_DEVICE(0x04ca, 0x3004), .driver_info = BTUSB_ATH3012 },
218 { USB_DEVICE(0x04ca, 0x3005), .driver_info = BTUSB_ATH3012 },
219 { USB_DEVICE(0x04ca, 0x3006), .driver_info = BTUSB_ATH3012 },
Mohammed Habibulla1fb4e092014-04-17 11:37:13 -0700220 { USB_DEVICE(0x04ca, 0x3007), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200221 { USB_DEVICE(0x04ca, 0x3008), .driver_info = BTUSB_ATH3012 },
222 { USB_DEVICE(0x04ca, 0x300b), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin7e730c7f2015-06-18 20:41:51 +0300223 { USB_DEVICE(0x04ca, 0x300d), .driver_info = BTUSB_ATH3012 },
Dmitry Tuninec0810d2015-05-02 13:36:58 +0300224 { USB_DEVICE(0x04ca, 0x300f), .driver_info = BTUSB_ATH3012 },
Janne Heikkinen134d3b32014-12-09 07:44:51 +0200225 { USB_DEVICE(0x04ca, 0x3010), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin81d90442016-02-28 11:04:06 +0300226 { USB_DEVICE(0x04ca, 0x3014), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin441ad622017-01-05 13:19:53 +0300227 { USB_DEVICE(0x04ca, 0x3018), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200228 { USB_DEVICE(0x0930, 0x0219), .driver_info = BTUSB_ATH3012 },
Dmitry Tunincd355ff2015-10-05 19:29:33 +0300229 { USB_DEVICE(0x0930, 0x021c), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200230 { USB_DEVICE(0x0930, 0x0220), .driver_info = BTUSB_ATH3012 },
Vincent Zwanenburg89d29752014-08-08 12:33:56 +0100231 { USB_DEVICE(0x0930, 0x0227), .driver_info = BTUSB_ATH3012 },
Andy Shevchenkoa735f9e2014-02-18 18:26:20 +0200232 { USB_DEVICE(0x0b05, 0x17d0), .driver_info = BTUSB_ATH3012 },
Ming Leid66629c2013-03-15 11:00:39 +0800233 { USB_DEVICE(0x0cf3, 0x0036), .driver_info = BTUSB_ATH3012 },
Steven.Li2d25f8b2011-07-01 14:02:36 +0800234 { USB_DEVICE(0x0cf3, 0x3004), .driver_info = BTUSB_ATH3012 },
Sunguk Lee94a32d102013-03-12 04:41:58 +0900235 { USB_DEVICE(0x0cf3, 0x3008), .driver_info = BTUSB_ATH3012 },
Cho, Yu-Chen07c0ea82012-03-14 22:01:21 +0200236 { USB_DEVICE(0x0cf3, 0x311d), .driver_info = BTUSB_ATH3012 },
Oliver Neukumb1312372014-01-16 15:37:11 +0100237 { USB_DEVICE(0x0cf3, 0x311e), .driver_info = BTUSB_ATH3012 },
Oliver Neukum1e56f1e2014-01-16 16:02:58 +0100238 { USB_DEVICE(0x0cf3, 0x311f), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200239 { USB_DEVICE(0x0cf3, 0x3121), .driver_info = BTUSB_ATH3012 },
Ming Leiebaf5792013-03-18 23:45:11 +0800240 { USB_DEVICE(0x0cf3, 0x817a), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin18e0afa2015-10-16 11:45:26 +0300241 { USB_DEVICE(0x0cf3, 0x817b), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200242 { USB_DEVICE(0x0cf3, 0xe003), .driver_info = BTUSB_ATH3012 },
AceLan Kaoac713112012-04-19 14:53:45 +0800243 { USB_DEVICE(0x0cf3, 0xe004), .driver_info = BTUSB_ATH3012 },
Peng Chen0a3658c2013-08-30 17:41:40 +0800244 { USB_DEVICE(0x0cf3, 0xe005), .driver_info = BTUSB_ATH3012 },
Wen-chien Jesse Sungca79f232015-05-13 11:39:24 +0800245 { USB_DEVICE(0x0cf3, 0xe006), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200246 { USB_DEVICE(0x13d3, 0x3362), .driver_info = BTUSB_ATH3012 },
247 { USB_DEVICE(0x13d3, 0x3375), .driver_info = BTUSB_ATH3012 },
AceLan Kaoeed307e2012-12-11 11:41:20 +0800248 { USB_DEVICE(0x13d3, 0x3393), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin609574e2016-02-10 15:33:17 +0300249 { USB_DEVICE(0x13d3, 0x3395), .driver_info = BTUSB_ATH3012 },
Sujith Manoharan5b77a1f2013-07-15 09:29:03 +0530250 { USB_DEVICE(0x13d3, 0x3402), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin3bb30a7c2014-11-25 20:19:52 +0300251 { USB_DEVICE(0x13d3, 0x3408), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin033efa92015-01-18 00:16:51 +0300252 { USB_DEVICE(0x13d3, 0x3423), .driver_info = BTUSB_ATH3012 },
Anantha Krishnanfa2f1392014-07-08 19:25:08 +0530253 { USB_DEVICE(0x13d3, 0x3432), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin75c6aca2016-03-04 01:32:19 +0300254 { USB_DEVICE(0x13d3, 0x3472), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin0d0cef62015-06-06 20:29:25 +0300255 { USB_DEVICE(0x13d3, 0x3474), .driver_info = BTUSB_ATH3012 },
Lauro Costa72f9f8b2016-05-09 17:36:11 -0300256 { USB_DEVICE(0x13d3, 0x3487), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin12d86892016-07-12 01:35:18 +0300257 { USB_DEVICE(0x13d3, 0x3490), .driver_info = BTUSB_ATH3012 },
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530258
Cho, Yu-Chene9036e32011-02-15 10:20:07 +0800259 /* Atheros AR5BBU12 with sflash firmware */
260 { USB_DEVICE(0x0489, 0xe02c), .driver_info = BTUSB_IGNORE },
261
Michael Gruetzner85d59722012-05-02 22:33:40 +0200262 /* Atheros AR5BBU12 with sflash firmware */
Yevgeniy Melnichukbc21fde2012-08-07 19:48:10 +0530263 { USB_DEVICE(0x0489, 0xe036), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200264 { USB_DEVICE(0x0489, 0xe03c), .driver_info = BTUSB_ATH3012 },
Michael Gruetzner85d59722012-05-02 22:33:40 +0200265
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +0000266 /* QCA ROME chipset */
Wen-chien Jesse Sung20541112015-05-13 11:39:25 +0800267 { USB_DEVICE(0x0cf3, 0xe007), .driver_info = BTUSB_QCA_ROME },
Kai-Heng Feng93519932016-08-16 12:50:06 +0800268 { USB_DEVICE(0x0cf3, 0xe009), .driver_info = BTUSB_QCA_ROME },
AceLan Kaoe5a49ee2018-01-02 10:41:11 +0800269 { USB_DEVICE(0x0cf3, 0xe010), .driver_info = BTUSB_QCA_ROME },
Marcel Holtmannc9e44472015-03-16 23:56:04 -0700270 { USB_DEVICE(0x0cf3, 0xe300), .driver_info = BTUSB_QCA_ROME },
Gabriel1eef1c32017-02-20 12:32:22 -0500271 { USB_DEVICE(0x0cf3, 0xe301), .driver_info = BTUSB_QCA_ROME },
Marcel Holtmannc9e44472015-03-16 23:56:04 -0700272 { USB_DEVICE(0x0cf3, 0xe360), .driver_info = BTUSB_QCA_ROME },
Yvain THONNART47ca5892016-06-11 15:43:30 +0200273 { USB_DEVICE(0x0489, 0xe092), .driver_info = BTUSB_QCA_ROME },
Bartosz Chronowski858ff382017-10-26 10:22:43 +0200274 { USB_DEVICE(0x0489, 0xe09f), .driver_info = BTUSB_QCA_ROME },
Shih-Yuan Lee (FourDollars)06e41d82017-06-14 14:42:00 +0800275 { USB_DEVICE(0x0489, 0xe0a2), .driver_info = BTUSB_QCA_ROME },
Dmitry Tunin1144a4e2016-09-21 19:13:08 +0300276 { USB_DEVICE(0x04ca, 0x3011), .driver_info = BTUSB_QCA_ROME },
Ioan Moldovan0a03f982017-08-28 18:09:39 +0300277 { USB_DEVICE(0x04ca, 0x3015), .driver_info = BTUSB_QCA_ROME },
Brian Norrisd829b9e2017-07-31 17:59:39 -0700278 { USB_DEVICE(0x04ca, 0x3016), .driver_info = BTUSB_QCA_ROME },
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +0000279
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200280 /* Broadcom BCM2035 */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100281 { USB_DEVICE(0x0a5c, 0x2009), .driver_info = BTUSB_BCM92035 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200282 { USB_DEVICE(0x0a5c, 0x200a), .driver_info = BTUSB_WRONG_SCO_MTU },
283 { USB_DEVICE(0x0a5c, 0x2035), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200284
285 /* Broadcom BCM2045 */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100286 { USB_DEVICE(0x0a5c, 0x2039), .driver_info = BTUSB_WRONG_SCO_MTU },
287 { USB_DEVICE(0x0a5c, 0x2101), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmannbdbef3d2008-09-23 00:16:35 +0200288
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200289 /* IBM/Lenovo ThinkPad with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100290 { USB_DEVICE(0x0a5c, 0x201e), .driver_info = BTUSB_WRONG_SCO_MTU },
291 { USB_DEVICE(0x0a5c, 0x2110), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200292
293 /* HP laptop with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100294 { USB_DEVICE(0x03f0, 0x171d), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200295
296 /* Dell laptop with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100297 { USB_DEVICE(0x413c, 0x8126), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200298
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100299 /* Dell Wireless 370 and 410 devices */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100300 { USB_DEVICE(0x413c, 0x8152), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100301 { USB_DEVICE(0x413c, 0x8156), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200302
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100303 /* Belkin F8T012 and F8T013 devices */
304 { USB_DEVICE(0x050d, 0x0012), .driver_info = BTUSB_WRONG_SCO_MTU },
305 { USB_DEVICE(0x050d, 0x0013), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200306
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100307 /* Asus WL-BTD202 device */
308 { USB_DEVICE(0x0b05, 0x1715), .driver_info = BTUSB_WRONG_SCO_MTU },
309
310 /* Kensington Bluetooth USB adapter */
311 { USB_DEVICE(0x047d, 0x105e), .driver_info = BTUSB_WRONG_SCO_MTU },
312
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200313 /* RTX Telecom based adapters with buggy SCO support */
314 { USB_DEVICE(0x0400, 0x0807), .driver_info = BTUSB_BROKEN_ISOC },
315 { USB_DEVICE(0x0400, 0x080a), .driver_info = BTUSB_BROKEN_ISOC },
316
317 /* CONWISE Technology based adapters with buggy SCO support */
Szymon Janc418678b2016-09-01 17:22:37 +0200318 { USB_DEVICE(0x0e5e, 0x6622),
319 .driver_info = BTUSB_BROKEN_ISOC | BTUSB_CW6622},
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200320
Marcel Holtmann4fcef8e2015-01-01 17:34:37 -0800321 /* Roper Class 1 Bluetooth Dongle (Silicon Wave based) */
Aleksei Volkov2eeac872015-06-08 12:02:10 +0300322 { USB_DEVICE(0x1310, 0x0001), .driver_info = BTUSB_SWAVE },
Marcel Holtmann4fcef8e2015-01-01 17:34:37 -0800323
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200324 /* Digianswer devices */
325 { USB_DEVICE(0x08fd, 0x0001), .driver_info = BTUSB_DIGIANSWER },
326 { USB_DEVICE(0x08fd, 0x0002), .driver_info = BTUSB_IGNORE },
327
328 /* CSR BlueCore Bluetooth Sniffer */
Marcel Holtmann4f64fa82014-07-07 00:12:04 +0200329 { USB_DEVICE(0x0a12, 0x0002),
330 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200331
332 /* Frontline ComProbe Bluetooth Sniffer */
Marcel Holtmann4f64fa82014-07-07 00:12:04 +0200333 { USB_DEVICE(0x16d3, 0x0002),
334 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200335
Marcel Holtmanncb1ee892015-01-28 19:41:42 -0800336 /* Marvell Bluetooth devices */
337 { USB_DEVICE(0x1286, 0x2044), .driver_info = BTUSB_MARVELL },
338 { USB_DEVICE(0x1286, 0x2046), .driver_info = BTUSB_MARVELL },
Amitkumar Karwar1165df02016-09-28 16:18:35 +0530339 { USB_DEVICE(0x1286, 0x204e), .driver_info = BTUSB_MARVELL },
Marcel Holtmanncb1ee892015-01-28 19:41:42 -0800340
Marcel Holtmannd0ac9eb2015-01-28 19:41:43 -0800341 /* Intel Bluetooth devices */
Marcel Holtmannde766142017-03-06 15:38:28 -0800342 { USB_DEVICE(0x8087, 0x0025), .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
Peter Poklop4481c072015-08-15 20:47:09 +0200377 /* Silicon Wave based devices */
378 { USB_DEVICE(0x0c10, 0x0000), .driver_info = BTUSB_SWAVE },
379
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200380 { } /* Terminating entry */
381};
382
Hans de Goede1fdb9262018-02-20 09:06:18 +0100383/* The Bluetooth USB module build into some devices needs to be reset on resume,
384 * this is a problem with the platform (likely shutting off all power) not with
385 * the module itself. So we use a DMI list to match known broken platforms.
386 */
387static const struct dmi_system_id btusb_needs_reset_resume_table[] = {
Hans de Goede1fdb9262018-02-20 09:06:18 +0100388 {}
389};
390
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200391#define BTUSB_MAX_ISOC_FRAMES 10
392
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200393#define BTUSB_INTR_RUNNING 0
394#define BTUSB_BULK_RUNNING 1
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200395#define BTUSB_ISOC_RUNNING 2
Oliver Neukum7bee5492009-08-24 23:44:59 +0200396#define BTUSB_SUSPENDING 3
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -0300397#define BTUSB_DID_ISO_RESUME 4
Marcel Holtmanncda0dd72015-01-26 21:33:48 -0800398#define BTUSB_BOOTLOADER 5
399#define BTUSB_DOWNLOADING 6
Marcel Holtmannce6bb922015-01-28 01:58:40 -0800400#define BTUSB_FIRMWARE_LOADED 7
Marcel Holtmanncda0dd72015-01-26 21:33:48 -0800401#define BTUSB_FIRMWARE_FAILED 8
Marcel Holtmannce6bb922015-01-28 01:58:40 -0800402#define BTUSB_BOOTING 9
Hans de Goede61f5ace2018-01-08 10:44:16 +0100403#define BTUSB_DIAG_RUNNING 10
404#define BTUSB_OOB_WAKE_ENABLED 11
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200405
406struct btusb_data {
407 struct hci_dev *hdev;
408 struct usb_device *udev;
Marcel Holtmann5fbcd262008-09-23 00:16:36 +0200409 struct usb_interface *intf;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200410 struct usb_interface *isoc;
Marcel Holtmann9d08f502015-10-08 20:23:08 +0200411 struct usb_interface *diag;
Marcel Holtmann459232f2017-10-24 19:42:45 +0200412 unsigned isoc_ifnum;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200413
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200414 unsigned long flags;
415
416 struct work_struct work;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200417 struct work_struct waker;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200418
Marcel Holtmann803b5832014-09-16 08:00:29 +0200419 struct usb_anchor deferred;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200420 struct usb_anchor tx_anchor;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200421 int tx_in_flight;
422 spinlock_t txlock;
423
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200424 struct usb_anchor intr_anchor;
425 struct usb_anchor bulk_anchor;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200426 struct usb_anchor isoc_anchor;
Marcel Holtmann9d08f502015-10-08 20:23:08 +0200427 struct usb_anchor diag_anchor;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200428 spinlock_t rxlock;
429
430 struct sk_buff *evt_skb;
431 struct sk_buff *acl_skb;
432 struct sk_buff *sco_skb;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200433
434 struct usb_endpoint_descriptor *intr_ep;
435 struct usb_endpoint_descriptor *bulk_tx_ep;
436 struct usb_endpoint_descriptor *bulk_rx_ep;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200437 struct usb_endpoint_descriptor *isoc_tx_ep;
438 struct usb_endpoint_descriptor *isoc_rx_ep;
Marcel Holtmann9d08f502015-10-08 20:23:08 +0200439 struct usb_endpoint_descriptor *diag_tx_ep;
440 struct usb_endpoint_descriptor *diag_rx_ep;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200441
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100442 __u8 cmdreq_type;
Marcel Holtmann893ba542015-01-28 20:27:34 -0800443 __u8 cmdreq;
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100444
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100445 unsigned int sco_num;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200446 int isoc_altsetting;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +0100447 int suspend_count;
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +0100448
Marcel Holtmann97307f52015-01-12 13:51:10 -0800449 int (*recv_event)(struct hci_dev *hdev, struct sk_buff *skb);
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +0100450 int (*recv_bulk)(struct btusb_data *data, void *buffer, int count);
Kim, Ben Young Taeace31982015-02-15 23:06:14 +0000451
452 int (*setup_on_usb)(struct hci_dev *hdev);
Rajat Jainfd913ef2017-02-01 14:24:09 -0800453
454 int oob_wake_irq; /* irq for out-of-band wake-on-bt */
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200455};
456
Marcel Holtmann803b5832014-09-16 08:00:29 +0200457static inline void btusb_free_frags(struct btusb_data *data)
458{
459 unsigned long flags;
460
461 spin_lock_irqsave(&data->rxlock, flags);
462
463 kfree_skb(data->evt_skb);
464 data->evt_skb = NULL;
465
466 kfree_skb(data->acl_skb);
467 data->acl_skb = NULL;
468
469 kfree_skb(data->sco_skb);
470 data->sco_skb = NULL;
471
472 spin_unlock_irqrestore(&data->rxlock, flags);
473}
474
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200475static int btusb_recv_intr(struct btusb_data *data, void *buffer, int count)
476{
Marcel Holtmann803b5832014-09-16 08:00:29 +0200477 struct sk_buff *skb;
478 int err = 0;
479
480 spin_lock(&data->rxlock);
481 skb = data->evt_skb;
482
483 while (count) {
484 int len;
485
486 if (!skb) {
487 skb = bt_skb_alloc(HCI_MAX_EVENT_SIZE, GFP_ATOMIC);
488 if (!skb) {
489 err = -ENOMEM;
490 break;
491 }
492
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100493 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
494 hci_skb_expect(skb) = HCI_EVENT_HDR_SIZE;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200495 }
496
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100497 len = min_t(uint, hci_skb_expect(skb), count);
Johannes Berg59ae1d12017-06-16 14:29:20 +0200498 skb_put_data(skb, buffer, len);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200499
500 count -= len;
501 buffer += len;
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100502 hci_skb_expect(skb) -= len;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200503
504 if (skb->len == HCI_EVENT_HDR_SIZE) {
505 /* Complete event header */
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100506 hci_skb_expect(skb) = hci_event_hdr(skb)->plen;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200507
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100508 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
Marcel Holtmann803b5832014-09-16 08:00:29 +0200509 kfree_skb(skb);
510 skb = NULL;
511
512 err = -EILSEQ;
513 break;
514 }
515 }
516
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100517 if (!hci_skb_expect(skb)) {
Marcel Holtmann803b5832014-09-16 08:00:29 +0200518 /* Complete frame */
Marcel Holtmann97307f52015-01-12 13:51:10 -0800519 data->recv_event(data->hdev, skb);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200520 skb = NULL;
521 }
522 }
523
524 data->evt_skb = skb;
525 spin_unlock(&data->rxlock);
526
527 return err;
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200528}
529
530static int btusb_recv_bulk(struct btusb_data *data, void *buffer, int count)
531{
Marcel Holtmann803b5832014-09-16 08:00:29 +0200532 struct sk_buff *skb;
533 int err = 0;
534
535 spin_lock(&data->rxlock);
536 skb = data->acl_skb;
537
538 while (count) {
539 int len;
540
541 if (!skb) {
542 skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
543 if (!skb) {
544 err = -ENOMEM;
545 break;
546 }
547
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100548 hci_skb_pkt_type(skb) = HCI_ACLDATA_PKT;
549 hci_skb_expect(skb) = HCI_ACL_HDR_SIZE;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200550 }
551
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100552 len = min_t(uint, hci_skb_expect(skb), count);
Johannes Berg59ae1d12017-06-16 14:29:20 +0200553 skb_put_data(skb, buffer, len);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200554
555 count -= len;
556 buffer += len;
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100557 hci_skb_expect(skb) -= len;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200558
559 if (skb->len == HCI_ACL_HDR_SIZE) {
560 __le16 dlen = hci_acl_hdr(skb)->dlen;
561
562 /* Complete ACL header */
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100563 hci_skb_expect(skb) = __le16_to_cpu(dlen);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200564
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100565 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
Marcel Holtmann803b5832014-09-16 08:00:29 +0200566 kfree_skb(skb);
567 skb = NULL;
568
569 err = -EILSEQ;
570 break;
571 }
572 }
573
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100574 if (!hci_skb_expect(skb)) {
Marcel Holtmann803b5832014-09-16 08:00:29 +0200575 /* Complete frame */
576 hci_recv_frame(data->hdev, skb);
577 skb = NULL;
578 }
579 }
580
581 data->acl_skb = skb;
582 spin_unlock(&data->rxlock);
583
584 return err;
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200585}
586
587static int btusb_recv_isoc(struct btusb_data *data, void *buffer, int count)
588{
Marcel Holtmann803b5832014-09-16 08:00:29 +0200589 struct sk_buff *skb;
590 int err = 0;
591
592 spin_lock(&data->rxlock);
593 skb = data->sco_skb;
594
595 while (count) {
596 int len;
597
598 if (!skb) {
599 skb = bt_skb_alloc(HCI_MAX_SCO_SIZE, GFP_ATOMIC);
600 if (!skb) {
601 err = -ENOMEM;
602 break;
603 }
604
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100605 hci_skb_pkt_type(skb) = HCI_SCODATA_PKT;
606 hci_skb_expect(skb) = HCI_SCO_HDR_SIZE;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200607 }
608
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100609 len = min_t(uint, hci_skb_expect(skb), count);
Johannes Berg59ae1d12017-06-16 14:29:20 +0200610 skb_put_data(skb, buffer, len);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200611
612 count -= len;
613 buffer += len;
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100614 hci_skb_expect(skb) -= len;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200615
616 if (skb->len == HCI_SCO_HDR_SIZE) {
617 /* Complete SCO header */
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100618 hci_skb_expect(skb) = hci_sco_hdr(skb)->dlen;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200619
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100620 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
Marcel Holtmann803b5832014-09-16 08:00:29 +0200621 kfree_skb(skb);
622 skb = NULL;
623
624 err = -EILSEQ;
625 break;
626 }
627 }
628
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100629 if (!hci_skb_expect(skb)) {
Marcel Holtmann803b5832014-09-16 08:00:29 +0200630 /* Complete frame */
631 hci_recv_frame(data->hdev, skb);
632 skb = NULL;
633 }
634 }
635
636 data->sco_skb = skb;
637 spin_unlock(&data->rxlock);
638
639 return err;
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200640}
641
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200642static void btusb_intr_complete(struct urb *urb)
643{
644 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +0100645 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200646 int err;
647
Marcel Holtmann89e75332014-09-16 04:44:50 +0200648 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
649 urb->actual_length);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200650
651 if (!test_bit(HCI_RUNNING, &hdev->flags))
652 return;
653
654 if (urb->status == 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200655 hdev->stat.byte_rx += urb->actual_length;
656
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200657 if (btusb_recv_intr(data, urb->transfer_buffer,
658 urb->actual_length) < 0) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +0100659 bt_dev_err(hdev, "corrupted event packet");
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200660 hdev->stat.err_rx++;
661 }
Champion Chen85560c42014-09-06 14:06:08 -0500662 } else if (urb->status == -ENOENT) {
663 /* Avoid suspend failed when usb_kill_urb */
664 return;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200665 }
666
667 if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
668 return;
669
Oliver Neukum7bee5492009-08-24 23:44:59 +0200670 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200671 usb_anchor_urb(urb, &data->intr_anchor);
672
673 err = usb_submit_urb(urb, GFP_ATOMIC);
674 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200675 /* -EPERM: urb is being killed;
Derek Robsond98422c2017-07-22 13:47:07 +1200676 * -ENODEV: device got disconnected
677 */
Paul Bolle4935f1c2011-08-09 17:16:28 +0200678 if (err != -EPERM && err != -ENODEV)
Marcel Holtmann2064ee32017-10-30 10:42:59 +0100679 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
680 urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200681 usb_unanchor_urb(urb);
682 }
683}
684
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100685static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200686{
David Herrmann155961e2012-02-09 21:58:32 +0100687 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200688 struct urb *urb;
689 unsigned char *buf;
690 unsigned int pipe;
691 int err, size;
692
693 BT_DBG("%s", hdev->name);
694
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200695 if (!data->intr_ep)
696 return -ENODEV;
697
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100698 urb = usb_alloc_urb(0, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200699 if (!urb)
700 return -ENOMEM;
701
702 size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
703
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100704 buf = kmalloc(size, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200705 if (!buf) {
706 usb_free_urb(urb);
707 return -ENOMEM;
708 }
709
710 pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
711
712 usb_fill_int_urb(urb, data->udev, pipe, buf, size,
Marcel Holtmann89e75332014-09-16 04:44:50 +0200713 btusb_intr_complete, hdev, data->intr_ep->bInterval);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200714
715 urb->transfer_flags |= URB_FREE_BUFFER;
716
717 usb_anchor_urb(urb, &data->intr_anchor);
718
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100719 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200720 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +0200721 if (err != -EPERM && err != -ENODEV)
Marcel Holtmann2064ee32017-10-30 10:42:59 +0100722 bt_dev_err(hdev, "urb %p submission failed (%d)",
723 urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200724 usb_unanchor_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200725 }
726
727 usb_free_urb(urb);
728
729 return err;
730}
731
732static void btusb_bulk_complete(struct urb *urb)
733{
734 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +0100735 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200736 int err;
737
Marcel Holtmann89e75332014-09-16 04:44:50 +0200738 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
739 urb->actual_length);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200740
741 if (!test_bit(HCI_RUNNING, &hdev->flags))
742 return;
743
744 if (urb->status == 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200745 hdev->stat.byte_rx += urb->actual_length;
746
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +0100747 if (data->recv_bulk(data, urb->transfer_buffer,
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200748 urb->actual_length) < 0) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +0100749 bt_dev_err(hdev, "corrupted ACL packet");
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200750 hdev->stat.err_rx++;
751 }
Champion Chen85560c42014-09-06 14:06:08 -0500752 } else if (urb->status == -ENOENT) {
753 /* Avoid suspend failed when usb_kill_urb */
754 return;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200755 }
756
757 if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
758 return;
759
760 usb_anchor_urb(urb, &data->bulk_anchor);
Oliver Neukum652fd782009-12-16 19:23:43 +0100761 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200762
763 err = usb_submit_urb(urb, GFP_ATOMIC);
764 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200765 /* -EPERM: urb is being killed;
Derek Robsond98422c2017-07-22 13:47:07 +1200766 * -ENODEV: device got disconnected
767 */
Paul Bolle4935f1c2011-08-09 17:16:28 +0200768 if (err != -EPERM && err != -ENODEV)
Marcel Holtmann2064ee32017-10-30 10:42:59 +0100769 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
770 urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200771 usb_unanchor_urb(urb);
772 }
773}
774
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100775static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200776{
David Herrmann155961e2012-02-09 21:58:32 +0100777 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200778 struct urb *urb;
779 unsigned char *buf;
780 unsigned int pipe;
Vikram Kandukuri290ba202009-07-02 14:31:59 +0530781 int err, size = HCI_MAX_FRAME_SIZE;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200782
783 BT_DBG("%s", hdev->name);
784
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200785 if (!data->bulk_rx_ep)
786 return -ENODEV;
787
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100788 urb = usb_alloc_urb(0, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200789 if (!urb)
790 return -ENOMEM;
791
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100792 buf = kmalloc(size, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200793 if (!buf) {
794 usb_free_urb(urb);
795 return -ENOMEM;
796 }
797
798 pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
799
Marcel Holtmann89e75332014-09-16 04:44:50 +0200800 usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
801 btusb_bulk_complete, hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200802
803 urb->transfer_flags |= URB_FREE_BUFFER;
804
Oliver Neukum7bee5492009-08-24 23:44:59 +0200805 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200806 usb_anchor_urb(urb, &data->bulk_anchor);
807
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100808 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200809 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +0200810 if (err != -EPERM && err != -ENODEV)
Marcel Holtmann2064ee32017-10-30 10:42:59 +0100811 bt_dev_err(hdev, "urb %p submission failed (%d)",
812 urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200813 usb_unanchor_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200814 }
815
816 usb_free_urb(urb);
817
818 return err;
819}
820
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200821static void btusb_isoc_complete(struct urb *urb)
822{
823 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +0100824 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200825 int i, err;
826
Marcel Holtmann89e75332014-09-16 04:44:50 +0200827 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
828 urb->actual_length);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200829
830 if (!test_bit(HCI_RUNNING, &hdev->flags))
831 return;
832
833 if (urb->status == 0) {
834 for (i = 0; i < urb->number_of_packets; i++) {
835 unsigned int offset = urb->iso_frame_desc[i].offset;
836 unsigned int length = urb->iso_frame_desc[i].actual_length;
837
838 if (urb->iso_frame_desc[i].status)
839 continue;
840
841 hdev->stat.byte_rx += length;
842
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200843 if (btusb_recv_isoc(data, urb->transfer_buffer + offset,
844 length) < 0) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +0100845 bt_dev_err(hdev, "corrupted SCO packet");
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200846 hdev->stat.err_rx++;
847 }
848 }
Champion Chen85560c42014-09-06 14:06:08 -0500849 } else if (urb->status == -ENOENT) {
850 /* Avoid suspend failed when usb_kill_urb */
851 return;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200852 }
853
854 if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags))
855 return;
856
857 usb_anchor_urb(urb, &data->isoc_anchor);
858
859 err = usb_submit_urb(urb, GFP_ATOMIC);
860 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200861 /* -EPERM: urb is being killed;
Derek Robsond98422c2017-07-22 13:47:07 +1200862 * -ENODEV: device got disconnected
863 */
Paul Bolle4935f1c2011-08-09 17:16:28 +0200864 if (err != -EPERM && err != -ENODEV)
Marcel Holtmann2064ee32017-10-30 10:42:59 +0100865 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
866 urb, -err);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200867 usb_unanchor_urb(urb);
868 }
869}
870
Jesper Juhl42b16b32011-01-17 00:09:38 +0100871static inline void __fill_isoc_descriptor(struct urb *urb, int len, int mtu)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200872{
873 int i, offset = 0;
874
875 BT_DBG("len %d mtu %d", len, mtu);
876
877 for (i = 0; i < BTUSB_MAX_ISOC_FRAMES && len >= mtu;
878 i++, offset += mtu, len -= mtu) {
879 urb->iso_frame_desc[i].offset = offset;
880 urb->iso_frame_desc[i].length = mtu;
881 }
882
883 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
884 urb->iso_frame_desc[i].offset = offset;
885 urb->iso_frame_desc[i].length = len;
886 i++;
887 }
888
889 urb->number_of_packets = i;
890}
891
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100892static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200893{
David Herrmann155961e2012-02-09 21:58:32 +0100894 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200895 struct urb *urb;
896 unsigned char *buf;
897 unsigned int pipe;
898 int err, size;
899
900 BT_DBG("%s", hdev->name);
901
902 if (!data->isoc_rx_ep)
903 return -ENODEV;
904
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100905 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200906 if (!urb)
907 return -ENOMEM;
908
909 size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) *
910 BTUSB_MAX_ISOC_FRAMES;
911
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100912 buf = kmalloc(size, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200913 if (!buf) {
914 usb_free_urb(urb);
915 return -ENOMEM;
916 }
917
918 pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress);
919
Bing Zhaofa0fb932011-12-20 18:19:00 -0800920 usb_fill_int_urb(urb, data->udev, pipe, buf, size, btusb_isoc_complete,
Marcel Holtmann89e75332014-09-16 04:44:50 +0200921 hdev, data->isoc_rx_ep->bInterval);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200922
Marcel Holtmann89e75332014-09-16 04:44:50 +0200923 urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200924
925 __fill_isoc_descriptor(urb, size,
Marcel Holtmann89e75332014-09-16 04:44:50 +0200926 le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize));
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200927
928 usb_anchor_urb(urb, &data->isoc_anchor);
929
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100930 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200931 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +0200932 if (err != -EPERM && err != -ENODEV)
Marcel Holtmann2064ee32017-10-30 10:42:59 +0100933 bt_dev_err(hdev, "urb %p submission failed (%d)",
934 urb, -err);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200935 usb_unanchor_urb(urb);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200936 }
937
938 usb_free_urb(urb);
939
940 return err;
941}
942
Marcel Holtmann9d08f502015-10-08 20:23:08 +0200943static void btusb_diag_complete(struct urb *urb)
944{
945 struct hci_dev *hdev = urb->context;
946 struct btusb_data *data = hci_get_drvdata(hdev);
947 int err;
948
949 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
950 urb->actual_length);
951
952 if (urb->status == 0) {
953 struct sk_buff *skb;
954
955 skb = bt_skb_alloc(urb->actual_length, GFP_ATOMIC);
956 if (skb) {
Johannes Berg59ae1d12017-06-16 14:29:20 +0200957 skb_put_data(skb, urb->transfer_buffer,
958 urb->actual_length);
Marcel Holtmann9d08f502015-10-08 20:23:08 +0200959 hci_recv_diag(hdev, skb);
960 }
961 } else if (urb->status == -ENOENT) {
962 /* Avoid suspend failed when usb_kill_urb */
963 return;
964 }
965
966 if (!test_bit(BTUSB_DIAG_RUNNING, &data->flags))
967 return;
968
969 usb_anchor_urb(urb, &data->diag_anchor);
970 usb_mark_last_busy(data->udev);
971
972 err = usb_submit_urb(urb, GFP_ATOMIC);
973 if (err < 0) {
974 /* -EPERM: urb is being killed;
Derek Robsond98422c2017-07-22 13:47:07 +1200975 * -ENODEV: device got disconnected
976 */
Marcel Holtmann9d08f502015-10-08 20:23:08 +0200977 if (err != -EPERM && err != -ENODEV)
Marcel Holtmann2064ee32017-10-30 10:42:59 +0100978 bt_dev_err(hdev, "urb %p failed to resubmit (%d)",
979 urb, -err);
Marcel Holtmann9d08f502015-10-08 20:23:08 +0200980 usb_unanchor_urb(urb);
981 }
982}
983
984static int btusb_submit_diag_urb(struct hci_dev *hdev, gfp_t mem_flags)
985{
986 struct btusb_data *data = hci_get_drvdata(hdev);
987 struct urb *urb;
988 unsigned char *buf;
989 unsigned int pipe;
990 int err, size = HCI_MAX_FRAME_SIZE;
991
992 BT_DBG("%s", hdev->name);
993
994 if (!data->diag_rx_ep)
995 return -ENODEV;
996
997 urb = usb_alloc_urb(0, mem_flags);
998 if (!urb)
999 return -ENOMEM;
1000
1001 buf = kmalloc(size, mem_flags);
1002 if (!buf) {
1003 usb_free_urb(urb);
1004 return -ENOMEM;
1005 }
1006
1007 pipe = usb_rcvbulkpipe(data->udev, data->diag_rx_ep->bEndpointAddress);
1008
1009 usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
1010 btusb_diag_complete, hdev);
1011
1012 urb->transfer_flags |= URB_FREE_BUFFER;
1013
1014 usb_mark_last_busy(data->udev);
1015 usb_anchor_urb(urb, &data->diag_anchor);
1016
1017 err = usb_submit_urb(urb, mem_flags);
1018 if (err < 0) {
1019 if (err != -EPERM && err != -ENODEV)
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001020 bt_dev_err(hdev, "urb %p submission failed (%d)",
1021 urb, -err);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02001022 usb_unanchor_urb(urb);
1023 }
1024
1025 usb_free_urb(urb);
1026
1027 return err;
1028}
1029
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001030static void btusb_tx_complete(struct urb *urb)
1031{
1032 struct sk_buff *skb = urb->context;
Marcel Holtmann89e75332014-09-16 04:44:50 +02001033 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
David Herrmann155961e2012-02-09 21:58:32 +01001034 struct btusb_data *data = hci_get_drvdata(hdev);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001035
Marcel Holtmann89e75332014-09-16 04:44:50 +02001036 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1037 urb->actual_length);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001038
1039 if (!test_bit(HCI_RUNNING, &hdev->flags))
1040 goto done;
1041
1042 if (!urb->status)
1043 hdev->stat.byte_tx += urb->transfer_buffer_length;
1044 else
1045 hdev->stat.err_tx++;
1046
1047done:
1048 spin_lock(&data->txlock);
1049 data->tx_in_flight--;
1050 spin_unlock(&data->txlock);
1051
1052 kfree(urb->setup_packet);
1053
1054 kfree_skb(skb);
1055}
1056
1057static void btusb_isoc_tx_complete(struct urb *urb)
1058{
1059 struct sk_buff *skb = urb->context;
Marcel Holtmann89e75332014-09-16 04:44:50 +02001060 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001061
Marcel Holtmann89e75332014-09-16 04:44:50 +02001062 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1063 urb->actual_length);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001064
1065 if (!test_bit(HCI_RUNNING, &hdev->flags))
1066 goto done;
1067
1068 if (!urb->status)
1069 hdev->stat.byte_tx += urb->transfer_buffer_length;
1070 else
1071 hdev->stat.err_tx++;
1072
1073done:
1074 kfree(urb->setup_packet);
1075
1076 kfree_skb(skb);
1077}
1078
1079static int btusb_open(struct hci_dev *hdev)
1080{
David Herrmann155961e2012-02-09 21:58:32 +01001081 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001082 int err;
1083
1084 BT_DBG("%s", hdev->name);
1085
Ethan Hsiehc7e163f2016-10-07 12:06:42 +08001086 err = usb_autopm_get_interface(data->intf);
1087 if (err < 0)
1088 return err;
1089
Kim, Ben Young Taeace31982015-02-15 23:06:14 +00001090 /* Patching USB firmware files prior to starting any URBs of HCI path
1091 * It is more safe to use USB bulk channel for downloading USB patch
1092 */
1093 if (data->setup_on_usb) {
1094 err = data->setup_on_usb(hdev);
Marcel Holtmanneb500422015-04-16 23:15:50 +02001095 if (err < 0)
Kim, Ben Young Taeace31982015-02-15 23:06:14 +00001096 return err;
1097 }
1098
Oliver Neukum7bee5492009-08-24 23:44:59 +02001099 data->intf->needs_remote_wakeup = 1;
Sukumar Ghoraia0085f22017-08-16 14:46:55 -07001100 /* device specific wakeup source enabled and required for USB
1101 * remote wakeup while host is suspended
1102 */
1103 device_wakeup_enable(&data->udev->dev);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001104
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001105 if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +02001106 goto done;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001107
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001108 err = btusb_submit_intr_urb(hdev, GFP_KERNEL);
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001109 if (err < 0)
1110 goto failed;
1111
1112 err = btusb_submit_bulk_urb(hdev, GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001113 if (err < 0) {
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001114 usb_kill_anchored_urbs(&data->intr_anchor);
1115 goto failed;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001116 }
1117
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001118 set_bit(BTUSB_BULK_RUNNING, &data->flags);
1119 btusb_submit_bulk_urb(hdev, GFP_KERNEL);
1120
Marcel Holtmann9d08f502015-10-08 20:23:08 +02001121 if (data->diag) {
1122 if (!btusb_submit_diag_urb(hdev, GFP_KERNEL))
1123 set_bit(BTUSB_DIAG_RUNNING, &data->flags);
1124 }
1125
Oliver Neukum7bee5492009-08-24 23:44:59 +02001126done:
1127 usb_autopm_put_interface(data->intf);
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001128 return 0;
1129
1130failed:
1131 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001132 usb_autopm_put_interface(data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001133 return err;
1134}
1135
Oliver Neukum7bee5492009-08-24 23:44:59 +02001136static void btusb_stop_traffic(struct btusb_data *data)
1137{
1138 usb_kill_anchored_urbs(&data->intr_anchor);
1139 usb_kill_anchored_urbs(&data->bulk_anchor);
1140 usb_kill_anchored_urbs(&data->isoc_anchor);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02001141 usb_kill_anchored_urbs(&data->diag_anchor);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001142}
1143
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001144static int btusb_close(struct hci_dev *hdev)
1145{
David Herrmann155961e2012-02-09 21:58:32 +01001146 struct btusb_data *data = hci_get_drvdata(hdev);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001147 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001148
1149 BT_DBG("%s", hdev->name);
1150
Marcel Holtmanne8c3c3d2008-09-23 00:16:36 +02001151 cancel_work_sync(&data->work);
Linus Torvalds404291a2009-11-11 13:32:29 -08001152 cancel_work_sync(&data->waker);
Marcel Holtmanne8c3c3d2008-09-23 00:16:36 +02001153
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001154 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001155 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001156 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02001157 clear_bit(BTUSB_DIAG_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001158
1159 btusb_stop_traffic(data);
Marcel Holtmann803b5832014-09-16 08:00:29 +02001160 btusb_free_frags(data);
1161
Oliver Neukum7bee5492009-08-24 23:44:59 +02001162 err = usb_autopm_get_interface(data->intf);
1163 if (err < 0)
Oliver Neukum7b8e2c12009-11-13 14:26:23 +01001164 goto failed;
Oliver Neukum7bee5492009-08-24 23:44:59 +02001165
1166 data->intf->needs_remote_wakeup = 0;
Sukumar Ghoraia0085f22017-08-16 14:46:55 -07001167 device_wakeup_disable(&data->udev->dev);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001168 usb_autopm_put_interface(data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001169
Oliver Neukum7b8e2c12009-11-13 14:26:23 +01001170failed:
1171 usb_scuttle_anchored_urbs(&data->deferred);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001172 return 0;
1173}
1174
1175static int btusb_flush(struct hci_dev *hdev)
1176{
David Herrmann155961e2012-02-09 21:58:32 +01001177 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001178
1179 BT_DBG("%s", hdev->name);
1180
1181 usb_kill_anchored_urbs(&data->tx_anchor);
Marcel Holtmann803b5832014-09-16 08:00:29 +02001182 btusb_free_frags(data);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001183
1184 return 0;
1185}
1186
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001187static struct urb *alloc_ctrl_urb(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001188{
David Herrmann155961e2012-02-09 21:58:32 +01001189 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001190 struct usb_ctrlrequest *dr;
1191 struct urb *urb;
1192 unsigned int pipe;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001193
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001194 urb = usb_alloc_urb(0, GFP_KERNEL);
1195 if (!urb)
1196 return ERR_PTR(-ENOMEM);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001197
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001198 dr = kmalloc(sizeof(*dr), GFP_KERNEL);
1199 if (!dr) {
1200 usb_free_urb(urb);
1201 return ERR_PTR(-ENOMEM);
1202 }
1203
1204 dr->bRequestType = data->cmdreq_type;
Marcel Holtmann893ba542015-01-28 20:27:34 -08001205 dr->bRequest = data->cmdreq;
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001206 dr->wIndex = 0;
1207 dr->wValue = 0;
1208 dr->wLength = __cpu_to_le16(skb->len);
1209
1210 pipe = usb_sndctrlpipe(data->udev, 0x00);
1211
Marcel Holtmann89e75332014-09-16 04:44:50 +02001212 usb_fill_control_urb(urb, data->udev, pipe, (void *)dr,
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001213 skb->data, skb->len, btusb_tx_complete, skb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001214
Marcel Holtmann89e75332014-09-16 04:44:50 +02001215 skb->dev = (void *)hdev;
Marcel Holtmann7bd8f092013-10-11 06:19:18 -07001216
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001217 return urb;
1218}
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001219
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001220static struct urb *alloc_bulk_urb(struct hci_dev *hdev, struct sk_buff *skb)
1221{
1222 struct btusb_data *data = hci_get_drvdata(hdev);
1223 struct urb *urb;
1224 unsigned int pipe;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001225
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001226 if (!data->bulk_tx_ep)
1227 return ERR_PTR(-ENODEV);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001228
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001229 urb = usb_alloc_urb(0, GFP_KERNEL);
1230 if (!urb)
1231 return ERR_PTR(-ENOMEM);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001232
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001233 pipe = usb_sndbulkpipe(data->udev, data->bulk_tx_ep->bEndpointAddress);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001234
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001235 usb_fill_bulk_urb(urb, data->udev, pipe,
1236 skb->data, skb->len, btusb_tx_complete, skb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001237
Marcel Holtmann89e75332014-09-16 04:44:50 +02001238 skb->dev = (void *)hdev;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001239
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001240 return urb;
1241}
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001242
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001243static struct urb *alloc_isoc_urb(struct hci_dev *hdev, struct sk_buff *skb)
1244{
1245 struct btusb_data *data = hci_get_drvdata(hdev);
1246 struct urb *urb;
1247 unsigned int pipe;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001248
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001249 if (!data->isoc_tx_ep)
1250 return ERR_PTR(-ENODEV);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001251
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001252 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_KERNEL);
1253 if (!urb)
1254 return ERR_PTR(-ENOMEM);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001255
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001256 pipe = usb_sndisocpipe(data->udev, data->isoc_tx_ep->bEndpointAddress);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001257
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001258 usb_fill_int_urb(urb, data->udev, pipe,
1259 skb->data, skb->len, btusb_isoc_tx_complete,
1260 skb, data->isoc_tx_ep->bInterval);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001261
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001262 urb->transfer_flags = URB_ISO_ASAP;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001263
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001264 __fill_isoc_descriptor(urb, skb->len,
1265 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001266
Marcel Holtmann89e75332014-09-16 04:44:50 +02001267 skb->dev = (void *)hdev;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001268
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001269 return urb;
1270}
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001271
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001272static int submit_tx_urb(struct hci_dev *hdev, struct urb *urb)
1273{
1274 struct btusb_data *data = hci_get_drvdata(hdev);
1275 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001276
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001277 usb_anchor_urb(urb, &data->tx_anchor);
1278
Johan Hedberge9753ef2014-09-14 08:49:34 +03001279 err = usb_submit_urb(urb, GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001280 if (err < 0) {
Paul Bolle5a9b80e2011-10-09 12:12:16 +02001281 if (err != -EPERM && err != -ENODEV)
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001282 bt_dev_err(hdev, "urb %p submission failed (%d)",
1283 urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001284 kfree(urb->setup_packet);
1285 usb_unanchor_urb(urb);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001286 } else {
1287 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001288 }
1289
Cong Wang54a8a792011-11-22 09:32:57 +08001290 usb_free_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001291 return err;
1292}
1293
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001294static int submit_or_queue_tx_urb(struct hci_dev *hdev, struct urb *urb)
1295{
1296 struct btusb_data *data = hci_get_drvdata(hdev);
1297 unsigned long flags;
1298 bool suspending;
1299
1300 spin_lock_irqsave(&data->txlock, flags);
1301 suspending = test_bit(BTUSB_SUSPENDING, &data->flags);
1302 if (!suspending)
1303 data->tx_in_flight++;
1304 spin_unlock_irqrestore(&data->txlock, flags);
1305
1306 if (!suspending)
1307 return submit_tx_urb(hdev, urb);
1308
1309 usb_anchor_urb(urb, &data->deferred);
1310 schedule_work(&data->waker);
1311
1312 usb_free_urb(urb);
1313 return 0;
1314}
1315
1316static int btusb_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
1317{
1318 struct urb *urb;
1319
1320 BT_DBG("%s", hdev->name);
1321
Marcel Holtmann618e8bc2015-11-05 07:33:56 +01001322 switch (hci_skb_pkt_type(skb)) {
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001323 case HCI_COMMAND_PKT:
1324 urb = alloc_ctrl_urb(hdev, skb);
1325 if (IS_ERR(urb))
1326 return PTR_ERR(urb);
1327
1328 hdev->stat.cmd_tx++;
1329 return submit_or_queue_tx_urb(hdev, urb);
1330
1331 case HCI_ACLDATA_PKT:
1332 urb = alloc_bulk_urb(hdev, skb);
1333 if (IS_ERR(urb))
1334 return PTR_ERR(urb);
1335
1336 hdev->stat.acl_tx++;
1337 return submit_or_queue_tx_urb(hdev, urb);
1338
1339 case HCI_SCODATA_PKT:
1340 if (hci_conn_num(hdev, SCO_LINK) < 1)
1341 return -ENODEV;
1342
1343 urb = alloc_isoc_urb(hdev, skb);
1344 if (IS_ERR(urb))
1345 return PTR_ERR(urb);
1346
1347 hdev->stat.sco_tx++;
1348 return submit_tx_urb(hdev, urb);
1349 }
1350
1351 return -EILSEQ;
1352}
1353
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001354static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
1355{
David Herrmann155961e2012-02-09 21:58:32 +01001356 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001357
1358 BT_DBG("%s evt %d", hdev->name, evt);
1359
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001360 if (hci_conn_num(hdev, SCO_LINK) != data->sco_num) {
1361 data->sco_num = hci_conn_num(hdev, SCO_LINK);
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001362 schedule_work(&data->work);
Marcel Holtmanna780efa2008-11-30 12:17:12 +01001363 }
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001364}
1365
Jesper Juhl42b16b32011-01-17 00:09:38 +01001366static inline int __set_isoc_interface(struct hci_dev *hdev, int altsetting)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001367{
David Herrmann155961e2012-02-09 21:58:32 +01001368 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001369 struct usb_interface *intf = data->isoc;
1370 struct usb_endpoint_descriptor *ep_desc;
1371 int i, err;
1372
1373 if (!data->isoc)
1374 return -ENODEV;
1375
Marcel Holtmann459232f2017-10-24 19:42:45 +02001376 err = usb_set_interface(data->udev, data->isoc_ifnum, altsetting);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001377 if (err < 0) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001378 bt_dev_err(hdev, "setting interface failed (%d)", -err);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001379 return err;
1380 }
1381
1382 data->isoc_altsetting = altsetting;
1383
1384 data->isoc_tx_ep = NULL;
1385 data->isoc_rx_ep = NULL;
1386
1387 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
1388 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
1389
1390 if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) {
1391 data->isoc_tx_ep = ep_desc;
1392 continue;
1393 }
1394
1395 if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) {
1396 data->isoc_rx_ep = ep_desc;
1397 continue;
1398 }
1399 }
1400
1401 if (!data->isoc_tx_ep || !data->isoc_rx_ep) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001402 bt_dev_err(hdev, "invalid SCO descriptors");
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001403 return -ENODEV;
1404 }
1405
1406 return 0;
1407}
1408
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001409static void btusb_work(struct work_struct *work)
1410{
1411 struct btusb_data *data = container_of(work, struct btusb_data, work);
1412 struct hci_dev *hdev = data->hdev;
Mikel Astizf4001d22012-04-11 08:48:51 +02001413 int new_alts;
Oliver Neukum7bee5492009-08-24 23:44:59 +02001414 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001415
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001416 if (data->sco_num > 0) {
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -03001417 if (!test_bit(BTUSB_DID_ISO_RESUME, &data->flags)) {
Oliver Neukum8efdd0c2011-02-11 13:00:06 +01001418 err = usb_autopm_get_interface(data->isoc ? data->isoc : data->intf);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001419 if (err < 0) {
1420 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1421 usb_kill_anchored_urbs(&data->isoc_anchor);
1422 return;
1423 }
1424
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -03001425 set_bit(BTUSB_DID_ISO_RESUME, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001426 }
Mikel Astizf4001d22012-04-11 08:48:51 +02001427
1428 if (hdev->voice_setting & 0x0020) {
1429 static const int alts[3] = { 2, 4, 5 };
Marcel Holtmann89e75332014-09-16 04:44:50 +02001430
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001431 new_alts = alts[data->sco_num - 1];
Mikel Astizf4001d22012-04-11 08:48:51 +02001432 } else {
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001433 new_alts = data->sco_num;
Mikel Astizf4001d22012-04-11 08:48:51 +02001434 }
1435
1436 if (data->isoc_altsetting != new_alts) {
Kuba Pawlakf6fc86f2015-10-28 15:18:05 +01001437 unsigned long flags;
1438
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001439 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1440 usb_kill_anchored_urbs(&data->isoc_anchor);
1441
Kuba Pawlak8f9d02f2015-09-10 17:07:00 +01001442 /* When isochronous alternate setting needs to be
1443 * changed, because SCO connection has been added
1444 * or removed, a packet fragment may be left in the
1445 * reassembling state. This could lead to wrongly
1446 * assembled fragments.
1447 *
1448 * Clear outstanding fragment when selecting a new
1449 * alternate setting.
1450 */
Kuba Pawlakf6fc86f2015-10-28 15:18:05 +01001451 spin_lock_irqsave(&data->rxlock, flags);
Kuba Pawlak8f9d02f2015-09-10 17:07:00 +01001452 kfree_skb(data->sco_skb);
1453 data->sco_skb = NULL;
Kuba Pawlakf6fc86f2015-10-28 15:18:05 +01001454 spin_unlock_irqrestore(&data->rxlock, flags);
Kuba Pawlak8f9d02f2015-09-10 17:07:00 +01001455
Mikel Astizf4001d22012-04-11 08:48:51 +02001456 if (__set_isoc_interface(hdev, new_alts) < 0)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001457 return;
1458 }
1459
1460 if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001461 if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001462 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1463 else
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001464 btusb_submit_isoc_urb(hdev, GFP_KERNEL);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001465 }
1466 } else {
1467 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1468 usb_kill_anchored_urbs(&data->isoc_anchor);
1469
1470 __set_isoc_interface(hdev, 0);
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -03001471 if (test_and_clear_bit(BTUSB_DID_ISO_RESUME, &data->flags))
Oliver Neukum8efdd0c2011-02-11 13:00:06 +01001472 usb_autopm_put_interface(data->isoc ? data->isoc : data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001473 }
1474}
1475
Oliver Neukum7bee5492009-08-24 23:44:59 +02001476static void btusb_waker(struct work_struct *work)
1477{
1478 struct btusb_data *data = container_of(work, struct btusb_data, waker);
1479 int err;
1480
1481 err = usb_autopm_get_interface(data->intf);
1482 if (err < 0)
1483 return;
1484
1485 usb_autopm_put_interface(data->intf);
1486}
1487
Marcel Holtmann9f8f9622013-04-10 08:11:35 -07001488static int btusb_setup_bcm92035(struct hci_dev *hdev)
1489{
1490 struct sk_buff *skb;
1491 u8 val = 0x00;
1492
1493 BT_DBG("%s", hdev->name);
1494
1495 skb = __hci_cmd_sync(hdev, 0xfc3b, 1, &val, HCI_INIT_TIMEOUT);
1496 if (IS_ERR(skb))
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001497 bt_dev_err(hdev, "BCM92035 command failed (%ld)", PTR_ERR(skb));
Marcel Holtmann9f8f9622013-04-10 08:11:35 -07001498 else
1499 kfree_skb(skb);
1500
1501 return 0;
1502}
1503
Marcel Holtmann81cac642014-01-03 03:02:36 -08001504static int btusb_setup_csr(struct hci_dev *hdev)
1505{
1506 struct hci_rp_read_local_version *rp;
1507 struct sk_buff *skb;
Marcel Holtmann81cac642014-01-03 03:02:36 -08001508
1509 BT_DBG("%s", hdev->name);
1510
Marcel Holtmann7cd84d72015-06-07 10:01:02 +02001511 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
1512 HCI_INIT_TIMEOUT);
1513 if (IS_ERR(skb)) {
1514 int err = PTR_ERR(skb);
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001515 bt_dev_err(hdev, "CSR: Local version failed (%d)", err);
Marcel Holtmann7cd84d72015-06-07 10:01:02 +02001516 return err;
1517 }
1518
1519 if (skb->len != sizeof(struct hci_rp_read_local_version)) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001520 bt_dev_err(hdev, "CSR: Local version length mismatch");
Marcel Holtmann7cd84d72015-06-07 10:01:02 +02001521 kfree_skb(skb);
1522 return -EIO;
1523 }
Marcel Holtmann81cac642014-01-03 03:02:36 -08001524
Marcel Holtmann89e75332014-09-16 04:44:50 +02001525 rp = (struct hci_rp_read_local_version *)skb->data;
Marcel Holtmann81cac642014-01-03 03:02:36 -08001526
Johan Hedberg6cafcd92015-08-30 21:47:21 +03001527 /* Detect controllers which aren't real CSR ones. */
1528 if (le16_to_cpu(rp->manufacturer) != 10 ||
1529 le16_to_cpu(rp->lmp_subver) == 0x0c5c) {
Marcel Holtmann9641d342015-06-07 10:01:01 +02001530 /* Clear the reset quirk since this is not an actual
1531 * early Bluetooth 1.1 device from CSR.
1532 */
1533 clear_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmann81cac642014-01-03 03:02:36 -08001534
Marcel Holtmann9641d342015-06-07 10:01:01 +02001535 /* These fake CSR controllers have all a broken
1536 * stored link key handling and so just disable it.
1537 */
1538 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
Marcel Holtmann81cac642014-01-03 03:02:36 -08001539 }
1540
Marcel Holtmann81cac642014-01-03 03:02:36 -08001541 kfree_skb(skb);
1542
Marcel Holtmann9641d342015-06-07 10:01:01 +02001543 return 0;
Marcel Holtmann81cac642014-01-03 03:02:36 -08001544}
1545
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001546static const struct firmware *btusb_setup_intel_get_fw(struct hci_dev *hdev,
Marcel Holtmann89e75332014-09-16 04:44:50 +02001547 struct intel_version *ver)
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001548{
1549 const struct firmware *fw;
1550 char fwname[64];
1551 int ret;
1552
1553 snprintf(fwname, sizeof(fwname),
1554 "intel/ibt-hw-%x.%x.%x-fw-%x.%x.%x.%x.%x.bseq",
1555 ver->hw_platform, ver->hw_variant, ver->hw_revision,
1556 ver->fw_variant, ver->fw_revision, ver->fw_build_num,
1557 ver->fw_build_ww, ver->fw_build_yy);
1558
1559 ret = request_firmware(&fw, fwname, &hdev->dev);
1560 if (ret < 0) {
1561 if (ret == -EINVAL) {
1562 BT_ERR("%s Intel firmware file request failed (%d)",
1563 hdev->name, ret);
1564 return NULL;
1565 }
1566
1567 BT_ERR("%s failed to open Intel firmware file: %s(%d)",
1568 hdev->name, fwname, ret);
1569
1570 /* If the correct firmware patch file is not found, use the
1571 * default firmware patch file instead
1572 */
1573 snprintf(fwname, sizeof(fwname), "intel/ibt-hw-%x.%x.bseq",
1574 ver->hw_platform, ver->hw_variant);
1575 if (request_firmware(&fw, fwname, &hdev->dev) < 0) {
1576 BT_ERR("%s failed to open default Intel fw file: %s",
1577 hdev->name, fwname);
1578 return NULL;
1579 }
1580 }
1581
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001582 bt_dev_info(hdev, "Intel Bluetooth firmware file: %s", fwname);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001583
1584 return fw;
1585}
1586
1587static int btusb_setup_intel_patching(struct hci_dev *hdev,
1588 const struct firmware *fw,
1589 const u8 **fw_ptr, int *disable_patch)
1590{
1591 struct sk_buff *skb;
1592 struct hci_command_hdr *cmd;
1593 const u8 *cmd_param;
1594 struct hci_event_hdr *evt = NULL;
1595 const u8 *evt_param = NULL;
1596 int remain = fw->size - (*fw_ptr - fw->data);
1597
1598 /* The first byte indicates the types of the patch command or event.
1599 * 0x01 means HCI command and 0x02 is HCI event. If the first bytes
1600 * in the current firmware buffer doesn't start with 0x01 or
1601 * the size of remain buffer is smaller than HCI command header,
1602 * the firmware file is corrupted and it should stop the patching
1603 * process.
1604 */
1605 if (remain > HCI_COMMAND_HDR_SIZE && *fw_ptr[0] != 0x01) {
1606 BT_ERR("%s Intel fw corrupted: invalid cmd read", hdev->name);
1607 return -EINVAL;
1608 }
1609 (*fw_ptr)++;
1610 remain--;
1611
1612 cmd = (struct hci_command_hdr *)(*fw_ptr);
1613 *fw_ptr += sizeof(*cmd);
1614 remain -= sizeof(*cmd);
1615
1616 /* Ensure that the remain firmware data is long enough than the length
1617 * of command parameter. If not, the firmware file is corrupted.
1618 */
1619 if (remain < cmd->plen) {
1620 BT_ERR("%s Intel fw corrupted: invalid cmd len", hdev->name);
1621 return -EFAULT;
1622 }
1623
1624 /* If there is a command that loads a patch in the firmware
1625 * file, then enable the patch upon success, otherwise just
1626 * disable the manufacturer mode, for example patch activation
1627 * is not required when the default firmware patch file is used
1628 * because there are no patch data to load.
1629 */
1630 if (*disable_patch && le16_to_cpu(cmd->opcode) == 0xfc8e)
1631 *disable_patch = 0;
1632
1633 cmd_param = *fw_ptr;
1634 *fw_ptr += cmd->plen;
1635 remain -= cmd->plen;
1636
1637 /* This reads the expected events when the above command is sent to the
1638 * device. Some vendor commands expects more than one events, for
1639 * example command status event followed by vendor specific event.
1640 * For this case, it only keeps the last expected event. so the command
1641 * can be sent with __hci_cmd_sync_ev() which returns the sk_buff of
1642 * last expected event.
1643 */
1644 while (remain > HCI_EVENT_HDR_SIZE && *fw_ptr[0] == 0x02) {
1645 (*fw_ptr)++;
1646 remain--;
1647
1648 evt = (struct hci_event_hdr *)(*fw_ptr);
1649 *fw_ptr += sizeof(*evt);
1650 remain -= sizeof(*evt);
1651
1652 if (remain < evt->plen) {
1653 BT_ERR("%s Intel fw corrupted: invalid evt len",
1654 hdev->name);
1655 return -EFAULT;
1656 }
1657
1658 evt_param = *fw_ptr;
1659 *fw_ptr += evt->plen;
1660 remain -= evt->plen;
1661 }
1662
1663 /* Every HCI commands in the firmware file has its correspond event.
1664 * If event is not found or remain is smaller than zero, the firmware
1665 * file is corrupted.
1666 */
1667 if (!evt || !evt_param || remain < 0) {
1668 BT_ERR("%s Intel fw corrupted: invalid evt read", hdev->name);
1669 return -EFAULT;
1670 }
1671
1672 skb = __hci_cmd_sync_ev(hdev, le16_to_cpu(cmd->opcode), cmd->plen,
1673 cmd_param, evt->evt, HCI_INIT_TIMEOUT);
1674 if (IS_ERR(skb)) {
1675 BT_ERR("%s sending Intel patch command (0x%4.4x) failed (%ld)",
1676 hdev->name, cmd->opcode, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001677 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001678 }
1679
1680 /* It ensures that the returned event matches the event data read from
1681 * the firmware file. At fist, it checks the length and then
1682 * the contents of the event.
1683 */
1684 if (skb->len != evt->plen) {
1685 BT_ERR("%s mismatch event length (opcode 0x%4.4x)", hdev->name,
1686 le16_to_cpu(cmd->opcode));
1687 kfree_skb(skb);
1688 return -EFAULT;
1689 }
1690
1691 if (memcmp(skb->data, evt_param, evt->plen)) {
1692 BT_ERR("%s mismatch event parameter (opcode 0x%4.4x)",
1693 hdev->name, le16_to_cpu(cmd->opcode));
1694 kfree_skb(skb);
1695 return -EFAULT;
1696 }
1697 kfree_skb(skb);
1698
1699 return 0;
1700}
1701
1702static int btusb_setup_intel(struct hci_dev *hdev)
1703{
1704 struct sk_buff *skb;
1705 const struct firmware *fw;
1706 const u8 *fw_ptr;
Loic Poulain28dc4b92015-12-03 16:10:22 +01001707 int disable_patch, err;
Loic Poulain6c483de2015-12-06 16:18:34 +01001708 struct intel_version ver;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001709
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001710 BT_DBG("%s", hdev->name);
1711
1712 /* The controller has a bug with the first HCI command sent to it
1713 * returning number of completed commands as zero. This would stall the
1714 * command processing in the Bluetooth core.
1715 *
1716 * As a workaround, send HCI Reset command first which will reset the
1717 * number of completed commands and allow normal command processing
1718 * from now on.
1719 */
1720 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
1721 if (IS_ERR(skb)) {
1722 BT_ERR("%s sending initial HCI reset command failed (%ld)",
1723 hdev->name, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001724 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001725 }
1726 kfree_skb(skb);
1727
1728 /* Read Intel specific controller version first to allow selection of
1729 * which firmware file to load.
1730 *
1731 * The returned information are hardware variant and revision plus
1732 * firmware variant, revision and build number.
1733 */
Loic Poulain6c483de2015-12-06 16:18:34 +01001734 err = btintel_read_version(hdev, &ver);
1735 if (err)
1736 return err;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001737
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001738 bt_dev_info(hdev, "read Intel version: %02x%02x%02x%02x%02x%02x%02x%02x%02x",
1739 ver.hw_platform, ver.hw_variant, ver.hw_revision,
1740 ver.fw_variant, ver.fw_revision, ver.fw_build_num,
1741 ver.fw_build_ww, ver.fw_build_yy, ver.fw_patch_num);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001742
1743 /* fw_patch_num indicates the version of patch the device currently
1744 * have. If there is no patch data in the device, it is always 0x00.
Minjune Kim5075eda2015-08-27 13:21:52 +09001745 * So, if it is other than 0x00, no need to patch the device again.
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001746 */
Loic Poulain6c483de2015-12-06 16:18:34 +01001747 if (ver.fw_patch_num) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001748 bt_dev_info(hdev, "Intel device is already patched. "
1749 "patch num: %02x", ver.fw_patch_num);
Marcel Holtmann213445b2015-10-21 02:45:19 +02001750 goto complete;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001751 }
1752
1753 /* Opens the firmware patch file based on the firmware version read
1754 * from the controller. If it fails to open the matching firmware
1755 * patch file, it tries to open the default firmware patch file.
1756 * If no patch file is found, allow the device to operate without
1757 * a patch.
1758 */
Loic Poulain6c483de2015-12-06 16:18:34 +01001759 fw = btusb_setup_intel_get_fw(hdev, &ver);
1760 if (!fw)
Marcel Holtmann213445b2015-10-21 02:45:19 +02001761 goto complete;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001762 fw_ptr = fw->data;
1763
Loic Poulain28dc4b92015-12-03 16:10:22 +01001764 /* Enable the manufacturer mode of the controller.
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001765 * Only while this mode is enabled, the driver can download the
1766 * firmware patch data and configuration parameters.
1767 */
Loic Poulain28dc4b92015-12-03 16:10:22 +01001768 err = btintel_enter_mfg(hdev);
1769 if (err) {
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001770 release_firmware(fw);
Loic Poulain28dc4b92015-12-03 16:10:22 +01001771 return err;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001772 }
1773
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001774 disable_patch = 1;
1775
1776 /* The firmware data file consists of list of Intel specific HCI
1777 * commands and its expected events. The first byte indicates the
1778 * type of the message, either HCI command or HCI event.
1779 *
1780 * It reads the command and its expected event from the firmware file,
1781 * and send to the controller. Once __hci_cmd_sync_ev() returns,
1782 * the returned event is compared with the event read from the firmware
1783 * file and it will continue until all the messages are downloaded to
1784 * the controller.
1785 *
1786 * Once the firmware patching is completed successfully,
1787 * the manufacturer mode is disabled with reset and activating the
1788 * downloaded patch.
1789 *
1790 * If the firmware patching fails, the manufacturer mode is
1791 * disabled with reset and deactivating the patch.
1792 *
1793 * If the default patch file is used, no reset is done when disabling
1794 * the manufacturer.
1795 */
1796 while (fw->size > fw_ptr - fw->data) {
1797 int ret;
1798
1799 ret = btusb_setup_intel_patching(hdev, fw, &fw_ptr,
1800 &disable_patch);
1801 if (ret < 0)
1802 goto exit_mfg_deactivate;
1803 }
1804
1805 release_firmware(fw);
1806
1807 if (disable_patch)
1808 goto exit_mfg_disable;
1809
1810 /* Patching completed successfully and disable the manufacturer mode
1811 * with reset and activate the downloaded firmware patches.
1812 */
Loic Poulain28dc4b92015-12-03 16:10:22 +01001813 err = btintel_exit_mfg(hdev, true, true);
1814 if (err)
1815 return err;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001816
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001817 bt_dev_info(hdev, "Intel firmware patch completed and activated");
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001818
Marcel Holtmann213445b2015-10-21 02:45:19 +02001819 goto complete;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001820
1821exit_mfg_disable:
1822 /* Disable the manufacturer mode without reset */
Loic Poulain28dc4b92015-12-03 16:10:22 +01001823 err = btintel_exit_mfg(hdev, false, false);
1824 if (err)
1825 return err;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001826
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001827 bt_dev_info(hdev, "Intel firmware patch completed");
Marcel Holtmann40cb0982014-07-02 12:06:45 +02001828
Marcel Holtmann213445b2015-10-21 02:45:19 +02001829 goto complete;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001830
1831exit_mfg_deactivate:
1832 release_firmware(fw);
1833
1834 /* Patching failed. Disable the manufacturer mode with reset and
1835 * deactivate the downloaded firmware patches.
1836 */
Loic Poulain28dc4b92015-12-03 16:10:22 +01001837 err = btintel_exit_mfg(hdev, true, false);
1838 if (err)
1839 return err;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001840
Marcel Holtmann2064ee32017-10-30 10:42:59 +01001841 bt_dev_info(hdev, "Intel firmware patch completed and deactivated");
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001842
Marcel Holtmann213445b2015-10-21 02:45:19 +02001843complete:
1844 /* Set the event mask for Intel specific vendor events. This enables
1845 * a few extra events that are useful during general operation.
1846 */
1847 btintel_set_event_mask_mfg(hdev, false);
1848
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07001849 btintel_check_bdaddr(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001850 return 0;
1851}
1852
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001853static int inject_cmd_complete(struct hci_dev *hdev, __u16 opcode)
1854{
1855 struct sk_buff *skb;
1856 struct hci_event_hdr *hdr;
1857 struct hci_ev_cmd_complete *evt;
1858
1859 skb = bt_skb_alloc(sizeof(*hdr) + sizeof(*evt) + 1, GFP_ATOMIC);
1860 if (!skb)
1861 return -ENOMEM;
1862
Johannes Berg4df864c2017-06-16 14:29:21 +02001863 hdr = skb_put(skb, sizeof(*hdr));
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001864 hdr->evt = HCI_EV_CMD_COMPLETE;
1865 hdr->plen = sizeof(*evt) + 1;
1866
Johannes Berg4df864c2017-06-16 14:29:21 +02001867 evt = skb_put(skb, sizeof(*evt));
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001868 evt->ncmd = 0x01;
1869 evt->opcode = cpu_to_le16(opcode);
1870
Johannes Berg634fef62017-06-16 14:29:24 +02001871 skb_put_u8(skb, 0x00);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001872
Marcel Holtmann618e8bc2015-11-05 07:33:56 +01001873 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001874
1875 return hci_recv_frame(hdev, skb);
1876}
1877
1878static int btusb_recv_bulk_intel(struct btusb_data *data, void *buffer,
1879 int count)
1880{
1881 /* When the device is in bootloader mode, then it can send
1882 * events via the bulk endpoint. These events are treated the
1883 * same way as the ones received from the interrupt endpoint.
1884 */
1885 if (test_bit(BTUSB_BOOTLOADER, &data->flags))
1886 return btusb_recv_intr(data, buffer, count);
1887
1888 return btusb_recv_bulk(data, buffer, count);
1889}
1890
Marcel Holtmannccd6da22015-04-09 00:35:19 -07001891static void btusb_intel_bootup(struct btusb_data *data, const void *ptr,
1892 unsigned int len)
1893{
1894 const struct intel_bootup *evt = ptr;
1895
1896 if (len != sizeof(*evt))
1897 return;
1898
1899 if (test_and_clear_bit(BTUSB_BOOTING, &data->flags)) {
1900 smp_mb__after_atomic();
1901 wake_up_bit(&data->flags, BTUSB_BOOTING);
1902 }
1903}
1904
1905static void btusb_intel_secure_send_result(struct btusb_data *data,
1906 const void *ptr, unsigned int len)
1907{
1908 const struct intel_secure_send_result *evt = ptr;
1909
1910 if (len != sizeof(*evt))
1911 return;
1912
1913 if (evt->result)
1914 set_bit(BTUSB_FIRMWARE_FAILED, &data->flags);
1915
1916 if (test_and_clear_bit(BTUSB_DOWNLOADING, &data->flags) &&
1917 test_bit(BTUSB_FIRMWARE_LOADED, &data->flags)) {
1918 smp_mb__after_atomic();
1919 wake_up_bit(&data->flags, BTUSB_DOWNLOADING);
1920 }
1921}
1922
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001923static int btusb_recv_event_intel(struct hci_dev *hdev, struct sk_buff *skb)
1924{
1925 struct btusb_data *data = hci_get_drvdata(hdev);
1926
1927 if (test_bit(BTUSB_BOOTLOADER, &data->flags)) {
1928 struct hci_event_hdr *hdr = (void *)skb->data;
1929
Marcel Holtmannccd6da22015-04-09 00:35:19 -07001930 if (skb->len > HCI_EVENT_HDR_SIZE && hdr->evt == 0xff &&
1931 hdr->plen > 0) {
1932 const void *ptr = skb->data + HCI_EVENT_HDR_SIZE + 1;
1933 unsigned int len = skb->len - HCI_EVENT_HDR_SIZE - 1;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001934
Marcel Holtmannccd6da22015-04-09 00:35:19 -07001935 switch (skb->data[2]) {
1936 case 0x02:
1937 /* When switching to the operational firmware
1938 * the device sends a vendor specific event
1939 * indicating that the bootup completed.
1940 */
1941 btusb_intel_bootup(data, ptr, len);
1942 break;
1943 case 0x06:
1944 /* When the firmware loading completes the
1945 * device sends out a vendor specific event
1946 * indicating the result of the firmware
1947 * loading.
1948 */
1949 btusb_intel_secure_send_result(data, ptr, len);
1950 break;
Johan Hedbergfad70972015-01-30 10:58:55 +02001951 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001952 }
1953 }
1954
1955 return hci_recv_frame(hdev, skb);
1956}
1957
1958static int btusb_send_frame_intel(struct hci_dev *hdev, struct sk_buff *skb)
1959{
1960 struct btusb_data *data = hci_get_drvdata(hdev);
1961 struct urb *urb;
1962
1963 BT_DBG("%s", hdev->name);
1964
Marcel Holtmann618e8bc2015-11-05 07:33:56 +01001965 switch (hci_skb_pkt_type(skb)) {
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001966 case HCI_COMMAND_PKT:
1967 if (test_bit(BTUSB_BOOTLOADER, &data->flags)) {
1968 struct hci_command_hdr *cmd = (void *)skb->data;
1969 __u16 opcode = le16_to_cpu(cmd->opcode);
1970
1971 /* When in bootloader mode and the command 0xfc09
1972 * is received, it needs to be send down the
1973 * bulk endpoint. So allocate a bulk URB instead.
1974 */
1975 if (opcode == 0xfc09)
1976 urb = alloc_bulk_urb(hdev, skb);
1977 else
1978 urb = alloc_ctrl_urb(hdev, skb);
1979
1980 /* When the 0xfc01 command is issued to boot into
1981 * the operational firmware, it will actually not
1982 * send a command complete event. To keep the flow
1983 * control working inject that event here.
1984 */
1985 if (opcode == 0xfc01)
1986 inject_cmd_complete(hdev, opcode);
1987 } else {
1988 urb = alloc_ctrl_urb(hdev, skb);
1989 }
1990 if (IS_ERR(urb))
1991 return PTR_ERR(urb);
1992
1993 hdev->stat.cmd_tx++;
1994 return submit_or_queue_tx_urb(hdev, urb);
1995
1996 case HCI_ACLDATA_PKT:
1997 urb = alloc_bulk_urb(hdev, skb);
1998 if (IS_ERR(urb))
1999 return PTR_ERR(urb);
2000
2001 hdev->stat.acl_tx++;
2002 return submit_or_queue_tx_urb(hdev, urb);
2003
2004 case HCI_SCODATA_PKT:
2005 if (hci_conn_num(hdev, SCO_LINK) < 1)
2006 return -ENODEV;
2007
2008 urb = alloc_isoc_urb(hdev, skb);
2009 if (IS_ERR(urb))
2010 return PTR_ERR(urb);
2011
2012 hdev->stat.sco_tx++;
2013 return submit_tx_urb(hdev, urb);
2014 }
2015
2016 return -EILSEQ;
2017}
2018
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002019static int btusb_setup_intel_new(struct hci_dev *hdev)
2020{
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002021 struct btusb_data *data = hci_get_drvdata(hdev);
Loic Poulain6c483de2015-12-06 16:18:34 +01002022 struct intel_version ver;
Tedd Ho-Jeong Anfaf174d2018-01-24 09:19:20 -08002023 struct intel_boot_params params;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002024 const struct firmware *fw;
Tedd Ho-Jeong Ane5889af2018-01-24 09:19:18 -08002025 u32 boot_param;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002026 char fwname[64];
2027 ktime_t calltime, delta, rettime;
2028 unsigned long long duration;
2029 int err;
2030
2031 BT_DBG("%s", hdev->name);
2032
Tedd Ho-Jeong An04d729b2018-01-24 09:19:19 -08002033 /* Set the default boot parameter to 0x0 and it is updated to
2034 * SKU specific boot parameter after reading Intel_Write_Boot_Params
2035 * command while downloading the firmware.
2036 */
2037 boot_param = 0x00000000;
Tedd Ho-Jeong Ane5889af2018-01-24 09:19:18 -08002038
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002039 calltime = ktime_get();
2040
2041 /* Read the Intel version information to determine if the device
2042 * is in bootloader mode or if it already has operational firmware
2043 * loaded.
2044 */
Loic Poulain6c483de2015-12-06 16:18:34 +01002045 err = btintel_read_version(hdev, &ver);
2046 if (err)
2047 return err;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002048
2049 /* The hardware platform number has a fixed value of 0x37 and
2050 * for now only accept this single value.
2051 */
Loic Poulain6c483de2015-12-06 16:18:34 +01002052 if (ver.hw_platform != 0x37) {
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002053 BT_ERR("%s: Unsupported Intel hardware platform (%u)",
Loic Poulain6c483de2015-12-06 16:18:34 +01002054 hdev->name, ver.hw_platform);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002055 return -EINVAL;
2056 }
2057
Tedd Ho-Jeong An92688342017-03-06 15:38:26 -08002058 /* Check for supported iBT hardware variants of this firmware
2059 * loading method.
Tedd Ho-Jeong Ana0af53b2016-05-06 11:53:46 -07002060 *
2061 * This check has been put in place to ensure correct forward
2062 * compatibility options when newer hardware variants come along.
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002063 */
Tedd Ho-Jeong An92688342017-03-06 15:38:26 -08002064 switch (ver.hw_variant) {
2065 case 0x0b: /* SfP */
2066 case 0x0c: /* WsP */
Tedd Ho-Jeong An86a61292017-05-01 13:35:12 -07002067 case 0x11: /* JfP */
Marcel Holtmannde766142017-03-06 15:38:28 -08002068 case 0x12: /* ThP */
Tedd Ho-Jeong An92688342017-03-06 15:38:26 -08002069 break;
2070 default:
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002071 BT_ERR("%s: Unsupported Intel hardware variant (%u)",
Loic Poulain6c483de2015-12-06 16:18:34 +01002072 hdev->name, ver.hw_variant);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002073 return -EINVAL;
2074 }
2075
Loic Poulain6c483de2015-12-06 16:18:34 +01002076 btintel_version_info(hdev, &ver);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002077
2078 /* The firmware variant determines if the device is in bootloader
2079 * mode or is running operational firmware. The value 0x06 identifies
2080 * the bootloader and the value 0x23 identifies the operational
2081 * firmware.
2082 *
2083 * When the operational firmware is already present, then only
2084 * the check for valid Bluetooth device address is needed. This
2085 * determines if the device will be added as configured or
2086 * unconfigured controller.
2087 *
2088 * It is not possible to use the Secure Boot Parameters in this
2089 * case since that command is only available in bootloader mode.
2090 */
Loic Poulain6c483de2015-12-06 16:18:34 +01002091 if (ver.fw_variant == 0x23) {
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002092 clear_bit(BTUSB_BOOTLOADER, &data->flags);
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07002093 btintel_check_bdaddr(hdev);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002094 return 0;
2095 }
2096
2097 /* If the device is not in bootloader mode, then the only possible
2098 * choice is to return an error and abort the device initialization.
2099 */
Loic Poulain6c483de2015-12-06 16:18:34 +01002100 if (ver.fw_variant != 0x06) {
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002101 BT_ERR("%s: Unsupported Intel firmware variant (%u)",
Loic Poulain6c483de2015-12-06 16:18:34 +01002102 hdev->name, ver.fw_variant);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002103 return -ENODEV;
2104 }
2105
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002106 /* Read the secure boot parameters to identify the operating
2107 * details of the bootloader.
2108 */
Tedd Ho-Jeong Anfaf174d2018-01-24 09:19:20 -08002109 err = btintel_read_boot_params(hdev, &params);
2110 if (err)
2111 return err;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002112
2113 /* It is required that every single firmware fragment is acknowledged
2114 * with a command complete event. If the boot parameters indicate
2115 * that this bootloader does not send them, then abort the setup.
2116 */
Tedd Ho-Jeong Anfaf174d2018-01-24 09:19:20 -08002117 if (params.limited_cce != 0x00) {
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002118 BT_ERR("%s: Unsupported Intel firmware loading method (%u)",
Tedd Ho-Jeong Anfaf174d2018-01-24 09:19:20 -08002119 hdev->name, params.limited_cce);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002120 return -EINVAL;
2121 }
2122
2123 /* If the OTP has no valid Bluetooth device address, then there will
2124 * also be no valid address for the operational firmware.
2125 */
Tedd Ho-Jeong Anfaf174d2018-01-24 09:19:20 -08002126 if (!bacmp(&params.otp_bdaddr, BDADDR_ANY)) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002127 bt_dev_info(hdev, "No device address configured");
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002128 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
2129 }
2130
2131 /* With this Intel bootloader only the hardware variant and device
Jaya P Gaf3715e2017-10-30 11:01:22 +01002132 * revision information are used to select the right firmware for SfP
2133 * and WsP.
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002134 *
Tedd Ho-Jeong An230b04a2016-06-28 08:56:39 -07002135 * The firmware filename is ibt-<hw_variant>-<dev_revid>.sfi.
2136 *
2137 * Currently the supported hardware variants are:
2138 * 11 (0x0b) for iBT3.0 (LnP/SfP)
2139 * 12 (0x0c) for iBT3.5 (WsP)
Jaya P Gaf3715e2017-10-30 11:01:22 +01002140 *
2141 * For ThP/JfP and for future SKU's, the FW name varies based on HW
2142 * variant, HW revision and FW revision, as these are dependent on CNVi
2143 * and RF Combination.
2144 *
Tedd Ho-Jeong An86a61292017-05-01 13:35:12 -07002145 * 17 (0x11) for iBT3.5 (JfP)
2146 * 18 (0x12) for iBT3.5 (ThP)
Jaya P Gaf3715e2017-10-30 11:01:22 +01002147 *
2148 * The firmware file name for these will be
2149 * ibt-<hw_variant>-<hw_revision>-<fw_revision>.sfi.
2150 *
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002151 */
Jaya P Gaf3715e2017-10-30 11:01:22 +01002152 switch (ver.hw_variant) {
2153 case 0x0b: /* SfP */
2154 case 0x0c: /* WsP */
2155 snprintf(fwname, sizeof(fwname), "intel/ibt-%u-%u.sfi",
2156 le16_to_cpu(ver.hw_variant),
Tedd Ho-Jeong Anfaf174d2018-01-24 09:19:20 -08002157 le16_to_cpu(params.dev_revid));
Jaya P Gaf3715e2017-10-30 11:01:22 +01002158 break;
2159 case 0x11: /* JfP */
2160 case 0x12: /* ThP */
2161 snprintf(fwname, sizeof(fwname), "intel/ibt-%u-%u-%u.sfi",
2162 le16_to_cpu(ver.hw_variant),
2163 le16_to_cpu(ver.hw_revision),
2164 le16_to_cpu(ver.fw_revision));
2165 break;
2166 default:
2167 BT_ERR("%s: Unsupported Intel firmware naming", hdev->name);
2168 return -EINVAL;
2169 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002170
2171 err = request_firmware(&fw, fwname, &hdev->dev);
2172 if (err < 0) {
2173 BT_ERR("%s: Failed to load Intel firmware file (%d)",
2174 hdev->name, err);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002175 return err;
2176 }
2177
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002178 bt_dev_info(hdev, "Found device firmware: %s", fwname);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002179
Tedd Ho-Jeong An52cc9162015-06-12 16:20:05 -07002180 /* Save the DDC file name for later use to apply once the firmware
2181 * downloading is done.
2182 */
Jaya P Gaf3715e2017-10-30 11:01:22 +01002183 switch (ver.hw_variant) {
2184 case 0x0b: /* SfP */
2185 case 0x0c: /* WsP */
2186 snprintf(fwname, sizeof(fwname), "intel/ibt-%u-%u.ddc",
2187 le16_to_cpu(ver.hw_variant),
Tedd Ho-Jeong Anfaf174d2018-01-24 09:19:20 -08002188 le16_to_cpu(params.dev_revid));
Jaya P Gaf3715e2017-10-30 11:01:22 +01002189 break;
2190 case 0x11: /* JfP */
2191 case 0x12: /* ThP */
2192 snprintf(fwname, sizeof(fwname), "intel/ibt-%u-%u-%u.ddc",
2193 le16_to_cpu(ver.hw_variant),
2194 le16_to_cpu(ver.hw_revision),
2195 le16_to_cpu(ver.fw_revision));
2196 break;
2197 default:
2198 BT_ERR("%s: Unsupported Intel firmware naming", hdev->name);
2199 return -EINVAL;
2200 }
Tedd Ho-Jeong An52cc9162015-06-12 16:20:05 -07002201
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002202 if (fw->size < 644) {
2203 BT_ERR("%s: Invalid size of firmware file (%zu)",
2204 hdev->name, fw->size);
2205 err = -EBADF;
2206 goto done;
2207 }
2208
2209 set_bit(BTUSB_DOWNLOADING, &data->flags);
2210
Tedd Ho-Jeong Anfbbe83c2018-01-24 09:19:21 -08002211 /* Start firmware downloading and get boot parameter */
2212 err = btintel_download_firmware(hdev, fw, &boot_param);
2213 if (err < 0)
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002214 goto done;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002215
Marcel Holtmannce6bb922015-01-28 01:58:40 -08002216 set_bit(BTUSB_FIRMWARE_LOADED, &data->flags);
2217
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002218 bt_dev_info(hdev, "Waiting for firmware download to complete");
Johan Hedberga087a982015-01-30 10:58:54 +02002219
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002220 /* Before switching the device into operational mode and with that
2221 * booting the loaded firmware, wait for the bootloader notification
2222 * that all fragments have been successfully received.
2223 *
Johan Hedberga087a982015-01-30 10:58:54 +02002224 * When the event processing receives the notification, then the
2225 * BTUSB_DOWNLOADING flag will be cleared.
2226 *
2227 * The firmware loading should not take longer than 5 seconds
2228 * and thus just timeout if that happens and fail the setup
2229 * of this device.
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002230 */
Johan Hedberg129a7692015-02-14 09:33:35 +02002231 err = wait_on_bit_timeout(&data->flags, BTUSB_DOWNLOADING,
2232 TASK_INTERRUPTIBLE,
2233 msecs_to_jiffies(5000));
Bart Van Asschef0a70a02016-08-11 16:02:44 -07002234 if (err == -EINTR) {
Johan Hedberga087a982015-01-30 10:58:54 +02002235 BT_ERR("%s: Firmware loading interrupted", hdev->name);
Johan Hedberga087a982015-01-30 10:58:54 +02002236 goto done;
2237 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002238
Johan Hedberga087a982015-01-30 10:58:54 +02002239 if (err) {
2240 BT_ERR("%s: Firmware loading timeout", hdev->name);
2241 err = -ETIMEDOUT;
2242 goto done;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002243 }
2244
2245 if (test_bit(BTUSB_FIRMWARE_FAILED, &data->flags)) {
2246 BT_ERR("%s: Firmware loading failed", hdev->name);
2247 err = -ENOEXEC;
2248 goto done;
2249 }
2250
2251 rettime = ktime_get();
2252 delta = ktime_sub(rettime, calltime);
2253 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
2254
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002255 bt_dev_info(hdev, "Firmware loaded in %llu usecs", duration);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002256
2257done:
2258 release_firmware(fw);
2259
2260 if (err < 0)
2261 return err;
2262
2263 calltime = ktime_get();
2264
2265 set_bit(BTUSB_BOOTING, &data->flags);
2266
Tedd Ho-Jeong Ane5889af2018-01-24 09:19:18 -08002267 err = btintel_send_intel_reset(hdev, boot_param);
2268 if (err)
2269 return err;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002270
2271 /* The bootloader will not indicate when the device is ready. This
2272 * is done by the operational firmware sending bootup notification.
Johan Hedbergfad70972015-01-30 10:58:55 +02002273 *
2274 * Booting into operational firmware should not take longer than
2275 * 1 second. However if that happens, then just fail the setup
2276 * since something went wrong.
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002277 */
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002278 bt_dev_info(hdev, "Waiting for device to boot");
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002279
Johan Hedberg129a7692015-02-14 09:33:35 +02002280 err = wait_on_bit_timeout(&data->flags, BTUSB_BOOTING,
2281 TASK_INTERRUPTIBLE,
2282 msecs_to_jiffies(1000));
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002283
Bart Van Asschef0a70a02016-08-11 16:02:44 -07002284 if (err == -EINTR) {
Johan Hedbergfad70972015-01-30 10:58:55 +02002285 BT_ERR("%s: Device boot interrupted", hdev->name);
2286 return -EINTR;
2287 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002288
Johan Hedbergfad70972015-01-30 10:58:55 +02002289 if (err) {
2290 BT_ERR("%s: Device boot timeout", hdev->name);
2291 return -ETIMEDOUT;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002292 }
2293
2294 rettime = ktime_get();
2295 delta = ktime_sub(rettime, calltime);
2296 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
2297
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002298 bt_dev_info(hdev, "Device booted in %llu usecs", duration);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002299
2300 clear_bit(BTUSB_BOOTLOADER, &data->flags);
2301
Tedd Ho-Jeong An52cc9162015-06-12 16:20:05 -07002302 /* Once the device is running in operational mode, it needs to apply
2303 * the device configuration (DDC) parameters.
2304 *
2305 * The device can work without DDC parameters, so even if it fails
2306 * to load the file, no need to fail the setup.
2307 */
Loic Poulaine924d3d2015-09-04 17:54:36 +02002308 btintel_load_ddc_config(hdev, fwname);
Tedd Ho-Jeong An52cc9162015-06-12 16:20:05 -07002309
Marcel Holtmann213445b2015-10-21 02:45:19 +02002310 /* Set the event mask for Intel specific vendor events. This enables
2311 * a few extra events that are useful during general operation. It
2312 * does not enable any debugging related events.
2313 *
2314 * The device will function correctly without these events enabled
2315 * and thus no need to fail the setup.
2316 */
2317 btintel_set_event_mask(hdev, false);
2318
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002319 return 0;
2320}
2321
Tedd Ho-Jeong Anbfbd45e2015-02-13 09:20:52 -08002322static int btusb_shutdown_intel(struct hci_dev *hdev)
2323{
2324 struct sk_buff *skb;
2325 long ret;
2326
2327 /* Some platforms have an issue with BT LED when the interface is
2328 * down or BT radio is turned off, which takes 5 seconds to BT LED
2329 * goes off. This command turns off the BT LED immediately.
2330 */
2331 skb = __hci_cmd_sync(hdev, 0xfc3f, 0, NULL, HCI_INIT_TIMEOUT);
2332 if (IS_ERR(skb)) {
2333 ret = PTR_ERR(skb);
2334 BT_ERR("%s: turning off Intel device LED failed (%ld)",
2335 hdev->name, ret);
2336 return ret;
2337 }
2338 kfree_skb(skb);
2339
2340 return 0;
2341}
2342
Rajat Jaina4ccc9e2017-02-01 14:24:10 -08002343#ifdef CONFIG_PM
2344/* Configure an out-of-band gpio as wake-up pin, if specified in device tree */
2345static int marvell_config_oob_wake(struct hci_dev *hdev)
2346{
2347 struct sk_buff *skb;
2348 struct btusb_data *data = hci_get_drvdata(hdev);
2349 struct device *dev = &data->udev->dev;
2350 u16 pin, gap, opcode;
2351 int ret;
2352 u8 cmd[5];
2353
2354 /* Move on if no wakeup pin specified */
2355 if (of_property_read_u16(dev->of_node, "marvell,wakeup-pin", &pin) ||
2356 of_property_read_u16(dev->of_node, "marvell,wakeup-gap-ms", &gap))
2357 return 0;
2358
2359 /* Vendor specific command to configure a GPIO as wake-up pin */
2360 opcode = hci_opcode_pack(0x3F, 0x59);
2361 cmd[0] = opcode & 0xFF;
2362 cmd[1] = opcode >> 8;
2363 cmd[2] = 2; /* length of parameters that follow */
2364 cmd[3] = pin;
2365 cmd[4] = gap; /* time in ms, for which wakeup pin should be asserted */
2366
2367 skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL);
2368 if (!skb) {
2369 bt_dev_err(hdev, "%s: No memory\n", __func__);
2370 return -ENOMEM;
2371 }
2372
Johannes Berg59ae1d12017-06-16 14:29:20 +02002373 skb_put_data(skb, cmd, sizeof(cmd));
Rajat Jaina4ccc9e2017-02-01 14:24:10 -08002374 hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
2375
2376 ret = btusb_send_frame(hdev, skb);
2377 if (ret) {
2378 bt_dev_err(hdev, "%s: configuration failed\n", __func__);
2379 kfree_skb(skb);
2380 return ret;
2381 }
2382
2383 return 0;
2384}
2385#endif
2386
Amitkumar Karwarae8df492014-07-18 14:47:06 -07002387static int btusb_set_bdaddr_marvell(struct hci_dev *hdev,
2388 const bdaddr_t *bdaddr)
2389{
2390 struct sk_buff *skb;
2391 u8 buf[8];
2392 long ret;
2393
2394 buf[0] = 0xfe;
2395 buf[1] = sizeof(bdaddr_t);
2396 memcpy(buf + 2, bdaddr, sizeof(bdaddr_t));
2397
2398 skb = __hci_cmd_sync(hdev, 0xfc22, sizeof(buf), buf, HCI_INIT_TIMEOUT);
2399 if (IS_ERR(skb)) {
2400 ret = PTR_ERR(skb);
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002401 bt_dev_err(hdev, "changing Marvell device address failed (%ld)",
2402 ret);
Amitkumar Karwarae8df492014-07-18 14:47:06 -07002403 return ret;
2404 }
2405 kfree_skb(skb);
2406
2407 return 0;
2408}
2409
Toshi Kikuchi58592232014-12-12 10:58:05 -08002410static int btusb_set_bdaddr_ath3012(struct hci_dev *hdev,
2411 const bdaddr_t *bdaddr)
2412{
2413 struct sk_buff *skb;
2414 u8 buf[10];
2415 long ret;
2416
2417 buf[0] = 0x01;
2418 buf[1] = 0x01;
2419 buf[2] = 0x00;
2420 buf[3] = sizeof(bdaddr_t);
2421 memcpy(buf + 4, bdaddr, sizeof(bdaddr_t));
2422
2423 skb = __hci_cmd_sync(hdev, 0xfc0b, sizeof(buf), buf, HCI_INIT_TIMEOUT);
2424 if (IS_ERR(skb)) {
2425 ret = PTR_ERR(skb);
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002426 bt_dev_err(hdev, "Change address command failed (%ld)", ret);
Toshi Kikuchi58592232014-12-12 10:58:05 -08002427 return ret;
2428 }
2429 kfree_skb(skb);
2430
2431 return 0;
2432}
2433
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002434#define QCA_DFU_PACKET_LEN 4096
2435
2436#define QCA_GET_TARGET_VERSION 0x09
2437#define QCA_CHECK_STATUS 0x05
2438#define QCA_DFU_DOWNLOAD 0x01
2439
2440#define QCA_SYSCFG_UPDATED 0x40
2441#define QCA_PATCH_UPDATED 0x80
2442#define QCA_DFU_TIMEOUT 3000
2443
2444struct qca_version {
2445 __le32 rom_version;
2446 __le32 patch_version;
2447 __le32 ram_version;
2448 __le32 ref_clock;
2449 __u8 reserved[4];
2450} __packed;
2451
2452struct qca_rampatch_version {
2453 __le16 rom_version;
2454 __le16 patch_version;
2455} __packed;
2456
2457struct qca_device_info {
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002458 u32 rom_version;
2459 u8 rampatch_hdr; /* length of header in rampatch */
2460 u8 nvm_hdr; /* length of header in NVM */
2461 u8 ver_offset; /* offset of version structure in rampatch */
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002462};
2463
2464static const struct qca_device_info qca_devices_table[] = {
2465 { 0x00000100, 20, 4, 10 }, /* Rome 1.0 */
2466 { 0x00000101, 20, 4, 10 }, /* Rome 1.1 */
Chan-yeol Park7f6e6362015-05-21 11:24:27 +09002467 { 0x00000200, 28, 4, 18 }, /* Rome 2.0 */
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002468 { 0x00000201, 28, 4, 18 }, /* Rome 2.1 */
2469 { 0x00000300, 28, 4, 18 }, /* Rome 3.0 */
2470 { 0x00000302, 28, 4, 18 }, /* Rome 3.2 */
2471};
2472
2473static int btusb_qca_send_vendor_req(struct hci_dev *hdev, u8 request,
2474 void *data, u16 size)
2475{
2476 struct btusb_data *btdata = hci_get_drvdata(hdev);
2477 struct usb_device *udev = btdata->udev;
2478 int pipe, err;
2479 u8 *buf;
2480
2481 buf = kmalloc(size, GFP_KERNEL);
2482 if (!buf)
2483 return -ENOMEM;
2484
2485 /* Found some of USB hosts have IOT issues with ours so that we should
2486 * not wait until HCI layer is ready.
2487 */
2488 pipe = usb_rcvctrlpipe(udev, 0);
2489 err = usb_control_msg(udev, pipe, request, USB_TYPE_VENDOR | USB_DIR_IN,
2490 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
2491 if (err < 0) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002492 bt_dev_err(hdev, "Failed to access otp area (%d)", err);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002493 goto done;
2494 }
2495
2496 memcpy(data, buf, size);
2497
2498done:
2499 kfree(buf);
2500
2501 return err;
2502}
2503
2504static int btusb_setup_qca_download_fw(struct hci_dev *hdev,
2505 const struct firmware *firmware,
2506 size_t hdr_size)
2507{
2508 struct btusb_data *btdata = hci_get_drvdata(hdev);
2509 struct usb_device *udev = btdata->udev;
2510 size_t count, size, sent = 0;
2511 int pipe, len, err;
2512 u8 *buf;
2513
2514 buf = kmalloc(QCA_DFU_PACKET_LEN, GFP_KERNEL);
2515 if (!buf)
2516 return -ENOMEM;
2517
2518 count = firmware->size;
2519
2520 size = min_t(size_t, count, hdr_size);
2521 memcpy(buf, firmware->data, size);
2522
2523 /* USB patches should go down to controller through USB path
2524 * because binary format fits to go down through USB channel.
2525 * USB control path is for patching headers and USB bulk is for
2526 * patch body.
2527 */
2528 pipe = usb_sndctrlpipe(udev, 0);
2529 err = usb_control_msg(udev, pipe, QCA_DFU_DOWNLOAD, USB_TYPE_VENDOR,
2530 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
2531 if (err < 0) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002532 bt_dev_err(hdev, "Failed to send headers (%d)", err);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002533 goto done;
2534 }
2535
2536 sent += size;
2537 count -= size;
2538
2539 while (count) {
2540 size = min_t(size_t, count, QCA_DFU_PACKET_LEN);
2541
2542 memcpy(buf, firmware->data + sent, size);
2543
2544 pipe = usb_sndbulkpipe(udev, 0x02);
2545 err = usb_bulk_msg(udev, pipe, buf, size, &len,
2546 QCA_DFU_TIMEOUT);
2547 if (err < 0) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002548 bt_dev_err(hdev, "Failed to send body at %zd of %zd (%d)",
2549 sent, firmware->size, err);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002550 break;
2551 }
2552
2553 if (size != len) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002554 bt_dev_err(hdev, "Failed to get bulk buffer");
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002555 err = -EILSEQ;
2556 break;
2557 }
2558
2559 sent += size;
2560 count -= size;
2561 }
2562
2563done:
2564 kfree(buf);
2565 return err;
2566}
2567
2568static int btusb_setup_qca_load_rampatch(struct hci_dev *hdev,
2569 struct qca_version *ver,
2570 const struct qca_device_info *info)
2571{
2572 struct qca_rampatch_version *rver;
2573 const struct firmware *fw;
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002574 u32 ver_rom, ver_patch;
2575 u16 rver_rom, rver_patch;
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002576 char fwname[64];
2577 int err;
2578
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002579 ver_rom = le32_to_cpu(ver->rom_version);
2580 ver_patch = le32_to_cpu(ver->patch_version);
2581
2582 snprintf(fwname, sizeof(fwname), "qca/rampatch_usb_%08x.bin", ver_rom);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002583
2584 err = request_firmware(&fw, fwname, &hdev->dev);
2585 if (err) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002586 bt_dev_err(hdev, "failed to request rampatch file: %s (%d)",
2587 fwname, err);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002588 return err;
2589 }
2590
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002591 bt_dev_info(hdev, "using rampatch file: %s", fwname);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002592
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002593 rver = (struct qca_rampatch_version *)(fw->data + info->ver_offset);
2594 rver_rom = le16_to_cpu(rver->rom_version);
2595 rver_patch = le16_to_cpu(rver->patch_version);
2596
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002597 bt_dev_info(hdev, "QCA: patch rome 0x%x build 0x%x, "
2598 "firmware rome 0x%x build 0x%x",
2599 rver_rom, rver_patch, ver_rom, ver_patch);
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002600
2601 if (rver_rom != ver_rom || rver_patch <= ver_patch) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002602 bt_dev_err(hdev, "rampatch file version did not match with firmware");
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002603 err = -EINVAL;
2604 goto done;
2605 }
2606
2607 err = btusb_setup_qca_download_fw(hdev, fw, info->rampatch_hdr);
2608
2609done:
2610 release_firmware(fw);
2611
2612 return err;
2613}
2614
2615static int btusb_setup_qca_load_nvm(struct hci_dev *hdev,
2616 struct qca_version *ver,
2617 const struct qca_device_info *info)
2618{
2619 const struct firmware *fw;
2620 char fwname[64];
2621 int err;
2622
2623 snprintf(fwname, sizeof(fwname), "qca/nvm_usb_%08x.bin",
2624 le32_to_cpu(ver->rom_version));
2625
2626 err = request_firmware(&fw, fwname, &hdev->dev);
2627 if (err) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002628 bt_dev_err(hdev, "failed to request NVM file: %s (%d)",
2629 fwname, err);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002630 return err;
2631 }
2632
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002633 bt_dev_info(hdev, "using NVM file: %s", fwname);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002634
2635 err = btusb_setup_qca_download_fw(hdev, fw, info->nvm_hdr);
2636
2637 release_firmware(fw);
2638
2639 return err;
2640}
2641
2642static int btusb_setup_qca(struct hci_dev *hdev)
2643{
2644 const struct qca_device_info *info = NULL;
2645 struct qca_version ver;
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002646 u32 ver_rom;
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002647 u8 status;
2648 int i, err;
2649
2650 err = btusb_qca_send_vendor_req(hdev, QCA_GET_TARGET_VERSION, &ver,
Marcel Holtmanneb500422015-04-16 23:15:50 +02002651 sizeof(ver));
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002652 if (err < 0)
2653 return err;
2654
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002655 ver_rom = le32_to_cpu(ver.rom_version);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002656 for (i = 0; i < ARRAY_SIZE(qca_devices_table); i++) {
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002657 if (ver_rom == qca_devices_table[i].rom_version)
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002658 info = &qca_devices_table[i];
2659 }
2660 if (!info) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002661 bt_dev_err(hdev, "don't support firmware rome 0x%x", ver_rom);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002662 return -ENODEV;
2663 }
2664
2665 err = btusb_qca_send_vendor_req(hdev, QCA_CHECK_STATUS, &status,
2666 sizeof(status));
2667 if (err < 0)
2668 return err;
2669
2670 if (!(status & QCA_PATCH_UPDATED)) {
2671 err = btusb_setup_qca_load_rampatch(hdev, &ver, info);
2672 if (err < 0)
2673 return err;
2674 }
2675
2676 if (!(status & QCA_SYSCFG_UPDATED)) {
2677 err = btusb_setup_qca_load_nvm(hdev, &ver, info);
2678 if (err < 0)
2679 return err;
2680 }
2681
2682 return 0;
2683}
2684
Marcel Holtmann9d08f502015-10-08 20:23:08 +02002685#ifdef CONFIG_BT_HCIBTUSB_BCM
2686static inline int __set_diag_interface(struct hci_dev *hdev)
2687{
2688 struct btusb_data *data = hci_get_drvdata(hdev);
2689 struct usb_interface *intf = data->diag;
2690 int i;
2691
2692 if (!data->diag)
2693 return -ENODEV;
2694
2695 data->diag_tx_ep = NULL;
2696 data->diag_rx_ep = NULL;
2697
2698 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
2699 struct usb_endpoint_descriptor *ep_desc;
2700
2701 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
2702
2703 if (!data->diag_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
2704 data->diag_tx_ep = ep_desc;
2705 continue;
2706 }
2707
2708 if (!data->diag_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
2709 data->diag_rx_ep = ep_desc;
2710 continue;
2711 }
2712 }
2713
2714 if (!data->diag_tx_ep || !data->diag_rx_ep) {
Marcel Holtmann2064ee32017-10-30 10:42:59 +01002715 bt_dev_err(hdev, "invalid diagnostic descriptors");
Marcel Holtmann9d08f502015-10-08 20:23:08 +02002716 return -ENODEV;
2717 }
2718
2719 return 0;
2720}
2721
2722static struct urb *alloc_diag_urb(struct hci_dev *hdev, bool enable)
2723{
2724 struct btusb_data *data = hci_get_drvdata(hdev);
2725 struct sk_buff *skb;
2726 struct urb *urb;
2727 unsigned int pipe;
2728
2729 if (!data->diag_tx_ep)
2730 return ERR_PTR(-ENODEV);
2731
2732 urb = usb_alloc_urb(0, GFP_KERNEL);
2733 if (!urb)
2734 return ERR_PTR(-ENOMEM);
2735
2736 skb = bt_skb_alloc(2, GFP_KERNEL);
2737 if (!skb) {
2738 usb_free_urb(urb);
2739 return ERR_PTR(-ENOMEM);
2740 }
2741
Johannes Berg634fef62017-06-16 14:29:24 +02002742 skb_put_u8(skb, 0xf0);
2743 skb_put_u8(skb, enable);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02002744
2745 pipe = usb_sndbulkpipe(data->udev, data->diag_tx_ep->bEndpointAddress);
2746
2747 usb_fill_bulk_urb(urb, data->udev, pipe,
2748 skb->data, skb->len, btusb_tx_complete, skb);
2749
2750 skb->dev = (void *)hdev;
2751
2752 return urb;
2753}
2754
2755static int btusb_bcm_set_diag(struct hci_dev *hdev, bool enable)
2756{
2757 struct btusb_data *data = hci_get_drvdata(hdev);
2758 struct urb *urb;
2759
2760 if (!data->diag)
2761 return -ENODEV;
2762
2763 if (!test_bit(HCI_RUNNING, &hdev->flags))
2764 return -ENETDOWN;
2765
2766 urb = alloc_diag_urb(hdev, enable);
2767 if (IS_ERR(urb))
2768 return PTR_ERR(urb);
2769
2770 return submit_or_queue_tx_urb(hdev, urb);
2771}
2772#endif
2773
Rajat Jainfd913ef2017-02-01 14:24:09 -08002774#ifdef CONFIG_PM
2775static irqreturn_t btusb_oob_wake_handler(int irq, void *priv)
2776{
2777 struct btusb_data *data = priv;
2778
2779 pm_wakeup_event(&data->udev->dev, 0);
Jeffy Chen017789f2017-02-24 14:24:29 +08002780 pm_system_wakeup();
Rajat Jainfd913ef2017-02-01 14:24:09 -08002781
2782 /* Disable only if not already disabled (keep it balanced) */
2783 if (test_and_clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags)) {
2784 disable_irq_nosync(irq);
2785 disable_irq_wake(irq);
2786 }
2787 return IRQ_HANDLED;
2788}
2789
2790static const struct of_device_id btusb_match_table[] = {
2791 { .compatible = "usb1286,204e" },
2792 { }
2793};
2794MODULE_DEVICE_TABLE(of, btusb_match_table);
2795
2796/* Use an oob wakeup pin? */
2797static int btusb_config_oob_wake(struct hci_dev *hdev)
2798{
2799 struct btusb_data *data = hci_get_drvdata(hdev);
2800 struct device *dev = &data->udev->dev;
2801 int irq, ret;
2802
2803 clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags);
2804
2805 if (!of_match_device(btusb_match_table, dev))
2806 return 0;
2807
2808 /* Move on if no IRQ specified */
2809 irq = of_irq_get_byname(dev->of_node, "wakeup");
2810 if (irq <= 0) {
2811 bt_dev_dbg(hdev, "%s: no OOB Wakeup IRQ in DT", __func__);
2812 return 0;
2813 }
2814
2815 ret = devm_request_irq(&hdev->dev, irq, btusb_oob_wake_handler,
2816 0, "OOB Wake-on-BT", data);
2817 if (ret) {
2818 bt_dev_err(hdev, "%s: IRQ request failed", __func__);
2819 return ret;
2820 }
2821
2822 ret = device_init_wakeup(dev, true);
2823 if (ret) {
2824 bt_dev_err(hdev, "%s: failed to init_wakeup", __func__);
2825 return ret;
2826 }
2827
2828 data->oob_wake_irq = irq;
2829 disable_irq(irq);
2830 bt_dev_info(hdev, "OOB Wake-on-BT configured at IRQ %u", irq);
2831 return 0;
2832}
2833#endif
2834
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002835static int btusb_probe(struct usb_interface *intf,
Marcel Holtmann89e75332014-09-16 04:44:50 +02002836 const struct usb_device_id *id)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002837{
2838 struct usb_endpoint_descriptor *ep_desc;
2839 struct btusb_data *data;
2840 struct hci_dev *hdev;
Marcel Holtmann22f8e9d2015-10-20 00:53:33 +02002841 unsigned ifnum_base;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002842 int i, err;
2843
2844 BT_DBG("intf %p id %p", intf, id);
2845
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002846 /* interface numbers are hardcoded in the spec */
Marcel Holtmann22f8e9d2015-10-20 00:53:33 +02002847 if (intf->cur_altsetting->desc.bInterfaceNumber != 0) {
2848 if (!(id->driver_info & BTUSB_IFNUM_2))
2849 return -ENODEV;
2850 if (intf->cur_altsetting->desc.bInterfaceNumber != 2)
2851 return -ENODEV;
2852 }
2853
2854 ifnum_base = intf->cur_altsetting->desc.bInterfaceNumber;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002855
2856 if (!id->driver_info) {
2857 const struct usb_device_id *match;
Marcel Holtmann89e75332014-09-16 04:44:50 +02002858
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002859 match = usb_match_id(intf, blacklist_table);
2860 if (match)
2861 id = match;
2862 }
2863
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002864 if (id->driver_info == BTUSB_IGNORE)
2865 return -ENODEV;
2866
Steven.Li2d25f8b2011-07-01 14:02:36 +08002867 if (id->driver_info & BTUSB_ATH3012) {
2868 struct usb_device *udev = interface_to_usbdev(intf);
2869
2870 /* Old firmware would otherwise let ath3k driver load
Derek Robsond98422c2017-07-22 13:47:07 +12002871 * patch and sysconfig files
2872 */
Steven.Li2d25f8b2011-07-01 14:02:36 +08002873 if (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x0001)
2874 return -ENODEV;
2875 }
2876
Sachin Kamat98921db2012-07-27 12:38:39 +05302877 data = devm_kzalloc(&intf->dev, sizeof(*data), GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002878 if (!data)
2879 return -ENOMEM;
2880
2881 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
2882 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
2883
2884 if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
2885 data->intr_ep = ep_desc;
2886 continue;
2887 }
2888
2889 if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
2890 data->bulk_tx_ep = ep_desc;
2891 continue;
2892 }
2893
2894 if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
2895 data->bulk_rx_ep = ep_desc;
2896 continue;
2897 }
2898 }
2899
Sachin Kamat98921db2012-07-27 12:38:39 +05302900 if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002901 return -ENODEV;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002902
Marcel Holtmann893ba542015-01-28 20:27:34 -08002903 if (id->driver_info & BTUSB_AMP) {
2904 data->cmdreq_type = USB_TYPE_CLASS | 0x01;
2905 data->cmdreq = 0x2b;
2906 } else {
2907 data->cmdreq_type = USB_TYPE_CLASS;
2908 data->cmdreq = 0x00;
2909 }
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002910
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002911 data->udev = interface_to_usbdev(intf);
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02002912 data->intf = intf;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002913
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002914 INIT_WORK(&data->work, btusb_work);
Oliver Neukum7bee5492009-08-24 23:44:59 +02002915 INIT_WORK(&data->waker, btusb_waker);
Marcel Holtmann803b5832014-09-16 08:00:29 +02002916 init_usb_anchor(&data->deferred);
2917 init_usb_anchor(&data->tx_anchor);
Oliver Neukum7bee5492009-08-24 23:44:59 +02002918 spin_lock_init(&data->txlock);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002919
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002920 init_usb_anchor(&data->intr_anchor);
2921 init_usb_anchor(&data->bulk_anchor);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02002922 init_usb_anchor(&data->isoc_anchor);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02002923 init_usb_anchor(&data->diag_anchor);
Marcel Holtmann803b5832014-09-16 08:00:29 +02002924 spin_lock_init(&data->rxlock);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002925
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002926 if (id->driver_info & BTUSB_INTEL_NEW) {
2927 data->recv_event = btusb_recv_event_intel;
2928 data->recv_bulk = btusb_recv_bulk_intel;
2929 set_bit(BTUSB_BOOTLOADER, &data->flags);
2930 } else {
2931 data->recv_event = hci_recv_frame;
2932 data->recv_bulk = btusb_recv_bulk;
2933 }
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +01002934
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002935 hdev = hci_alloc_dev();
Sachin Kamat98921db2012-07-27 12:38:39 +05302936 if (!hdev)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002937 return -ENOMEM;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002938
Marcel Holtmannc13854c2010-02-08 15:27:07 +01002939 hdev->bus = HCI_USB;
David Herrmann155961e2012-02-09 21:58:32 +01002940 hci_set_drvdata(hdev, data);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002941
Marcel Holtmann893ba542015-01-28 20:27:34 -08002942 if (id->driver_info & BTUSB_AMP)
2943 hdev->dev_type = HCI_AMP;
2944 else
Marcel Holtmannca8bee52016-07-05 14:30:14 +02002945 hdev->dev_type = HCI_PRIMARY;
Marcel Holtmann893ba542015-01-28 20:27:34 -08002946
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002947 data->hdev = hdev;
2948
2949 SET_HCIDEV_DEV(hdev, &intf->dev);
2950
Marcel Holtmann9f8f9622013-04-10 08:11:35 -07002951 hdev->open = btusb_open;
2952 hdev->close = btusb_close;
2953 hdev->flush = btusb_flush;
2954 hdev->send = btusb_send_frame;
2955 hdev->notify = btusb_notify;
2956
Hans de Goede1fdb9262018-02-20 09:06:18 +01002957 if (dmi_check_system(btusb_needs_reset_resume_table))
2958 interface_to_usbdev(intf)->quirks |= USB_QUIRK_RESET_RESUME;
2959
Rajat Jainfd913ef2017-02-01 14:24:09 -08002960#ifdef CONFIG_PM
2961 err = btusb_config_oob_wake(hdev);
2962 if (err)
2963 goto out_free_dev;
Rajat Jaina4ccc9e2017-02-01 14:24:10 -08002964
2965 /* Marvell devices may need a specific chip configuration */
2966 if (id->driver_info & BTUSB_MARVELL && data->oob_wake_irq) {
2967 err = marvell_config_oob_wake(hdev);
2968 if (err)
2969 goto out_free_dev;
2970 }
Rajat Jainfd913ef2017-02-01 14:24:09 -08002971#endif
Szymon Janc418678b2016-09-01 17:22:37 +02002972 if (id->driver_info & BTUSB_CW6622)
2973 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
2974
Marcel Holtmann6c9d4352015-10-17 14:39:27 +02002975 if (id->driver_info & BTUSB_BCM2045)
2976 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
2977
Marcel Holtmann9f8f9622013-04-10 08:11:35 -07002978 if (id->driver_info & BTUSB_BCM92035)
2979 hdev->setup = btusb_setup_bcm92035;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002980
Marcel Holtmannc2bfb102015-04-05 22:52:14 -07002981#ifdef CONFIG_BT_HCIBTUSB_BCM
Marcel Holtmannabbaf502014-07-02 00:53:48 +02002982 if (id->driver_info & BTUSB_BCM_PATCHRAM) {
Marcel Holtmann49a5f782015-10-20 02:30:48 +02002983 hdev->manufacturer = 15;
Marcel Holtmannc2bfb102015-04-05 22:52:14 -07002984 hdev->setup = btbcm_setup_patchram;
Marcel Holtmann9d08f502015-10-08 20:23:08 +02002985 hdev->set_diag = btusb_bcm_set_diag;
Marcel Holtmann1df1f592015-04-05 22:52:11 -07002986 hdev->set_bdaddr = btbcm_set_bdaddr;
Marcel Holtmann9d08f502015-10-08 20:23:08 +02002987
2988 /* Broadcom LM_DIAG Interface numbers are hardcoded */
Marcel Holtmann22f8e9d2015-10-20 00:53:33 +02002989 data->diag = usb_ifnum_to_if(data->udev, ifnum_base + 2);
Marcel Holtmannabbaf502014-07-02 00:53:48 +02002990 }
Petri Gynther10d4c672014-05-08 15:50:01 -07002991
Marcel Holtmann9d08f502015-10-08 20:23:08 +02002992 if (id->driver_info & BTUSB_BCM_APPLE) {
Marcel Holtmann49a5f782015-10-20 02:30:48 +02002993 hdev->manufacturer = 15;
Marcel Holtmannc2bfb102015-04-05 22:52:14 -07002994 hdev->setup = btbcm_setup_apple;
Marcel Holtmann9d08f502015-10-08 20:23:08 +02002995 hdev->set_diag = btusb_bcm_set_diag;
2996
2997 /* Broadcom LM_DIAG Interface numbers are hardcoded */
Marcel Holtmann22f8e9d2015-10-20 00:53:33 +02002998 data->diag = usb_ifnum_to_if(data->udev, ifnum_base + 2);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02002999 }
Marcel Holtmannc2bfb102015-04-05 22:52:14 -07003000#endif
Marcel Holtmann17b27722015-03-22 15:52:38 +01003001
Marcel Holtmanncb8d6592014-07-02 11:25:25 +02003002 if (id->driver_info & BTUSB_INTEL) {
Marcel Holtmann49a5f782015-10-20 02:30:48 +02003003 hdev->manufacturer = 2;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07003004 hdev->setup = btusb_setup_intel;
Tedd Ho-Jeong Anbfbd45e2015-02-13 09:20:52 -08003005 hdev->shutdown = btusb_shutdown_intel;
Marcel Holtmann3e247672015-10-17 16:00:28 +02003006 hdev->set_diag = btintel_set_diag_mfg;
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07003007 hdev->set_bdaddr = btintel_set_bdaddr;
Jakub Pawlowskic33fb9b2015-01-30 18:55:58 -08003008 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
Jakub Pawlowskic1154842015-03-17 09:04:16 -07003009 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
Marcel Holtmann3e247672015-10-17 16:00:28 +02003010 set_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks);
Marcel Holtmanncb8d6592014-07-02 11:25:25 +02003011 }
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07003012
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08003013 if (id->driver_info & BTUSB_INTEL_NEW) {
Marcel Holtmann49a5f782015-10-20 02:30:48 +02003014 hdev->manufacturer = 2;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08003015 hdev->send = btusb_send_frame_intel;
3016 hdev->setup = btusb_setup_intel_new;
Marcel Holtmanneeb6abe2015-07-05 14:37:39 +02003017 hdev->hw_error = btintel_hw_error;
Marcel Holtmann6d2e50d2015-10-09 14:42:08 +02003018 hdev->set_diag = btintel_set_diag;
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07003019 hdev->set_bdaddr = btintel_set_bdaddr;
Marcel Holtmannb970c5b2015-02-01 23:57:18 -08003020 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
Marcel Holtmannd8270fb2015-10-17 16:00:27 +02003021 set_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08003022 }
3023
Amitkumar Karwarae8df492014-07-18 14:47:06 -07003024 if (id->driver_info & BTUSB_MARVELL)
3025 hdev->set_bdaddr = btusb_set_bdaddr_marvell;
3026
Marcel Holtmann661cf882015-01-02 23:35:20 -08003027 if (id->driver_info & BTUSB_SWAVE) {
3028 set_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks);
Marcel Holtmannd57dbe72014-12-26 04:42:33 +01003029 set_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS, &hdev->quirks);
Marcel Holtmann661cf882015-01-02 23:35:20 -08003030 }
Marcel Holtmannd57dbe72014-12-26 04:42:33 +01003031
Marcel Holtmanne4c534b2015-10-21 01:31:45 +02003032 if (id->driver_info & BTUSB_INTEL_BOOT) {
3033 hdev->manufacturer = 2;
Marcel Holtmann40df7832014-07-06 13:29:58 +02003034 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
Marcel Holtmanne4c534b2015-10-21 01:31:45 +02003035 }
Marcel Holtmann40df7832014-07-06 13:29:58 +02003036
Jakub Pawlowski79f0c872015-01-29 10:38:34 -08003037 if (id->driver_info & BTUSB_ATH3012) {
Toshi Kikuchi58592232014-12-12 10:58:05 -08003038 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
Jakub Pawlowski3d50d512015-03-17 09:04:15 -07003039 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
Jakub Pawlowski79f0c872015-01-29 10:38:34 -08003040 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
3041 }
Toshi Kikuchi58592232014-12-12 10:58:05 -08003042
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003043 if (id->driver_info & BTUSB_QCA_ROME) {
3044 data->setup_on_usb = btusb_setup_qca;
3045 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
3046 }
3047
Carlo Caionedb33c772015-05-14 10:49:09 +02003048#ifdef CONFIG_BT_HCIBTUSB_RTL
Daniel Drake04b8c812015-05-21 08:23:50 -06003049 if (id->driver_info & BTUSB_REALTEK) {
Carlo Caionedb33c772015-05-14 10:49:09 +02003050 hdev->setup = btrtl_setup_realtek;
Daniel Drake04b8c812015-05-21 08:23:50 -06003051
3052 /* Realtek devices lose their updated firmware over suspend,
3053 * but the USB hub doesn't notice any status change.
3054 * Explicitly request a device reset on resume.
3055 */
Hans de Goede61f5ace2018-01-08 10:44:16 +01003056 interface_to_usbdev(intf)->quirks |= USB_QUIRK_RESET_RESUME;
Daniel Drake04b8c812015-05-21 08:23:50 -06003057 }
Carlo Caionedb33c772015-05-14 10:49:09 +02003058#endif
Daniel Drakea2698a92015-04-16 14:09:55 -06003059
Marcel Holtmann893ba542015-01-28 20:27:34 -08003060 if (id->driver_info & BTUSB_AMP) {
3061 /* AMP controllers do not support SCO packets */
3062 data->isoc = NULL;
3063 } else {
Marcel Holtmann22f8e9d2015-10-20 00:53:33 +02003064 /* Interface orders are hardcoded in the specification */
3065 data->isoc = usb_ifnum_to_if(data->udev, ifnum_base + 1);
Marcel Holtmann459232f2017-10-24 19:42:45 +02003066 data->isoc_ifnum = ifnum_base + 1;
Marcel Holtmann893ba542015-01-28 20:27:34 -08003067 }
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02003068
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01003069 if (!reset)
Szymon Janca6c511c2012-05-23 12:35:46 +02003070 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02003071
3072 if (force_scofix || id->driver_info & BTUSB_WRONG_SCO_MTU) {
3073 if (!disable_scofix)
3074 set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
3075 }
3076
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02003077 if (id->driver_info & BTUSB_BROKEN_ISOC)
3078 data->isoc = NULL;
3079
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01003080 if (id->driver_info & BTUSB_DIGIANSWER) {
3081 data->cmdreq_type = USB_TYPE_VENDOR;
Szymon Janca6c511c2012-05-23 12:35:46 +02003082 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01003083 }
3084
3085 if (id->driver_info & BTUSB_CSR) {
3086 struct usb_device *udev = data->udev;
Marcel Holtmann81cac642014-01-03 03:02:36 -08003087 u16 bcdDevice = le16_to_cpu(udev->descriptor.bcdDevice);
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01003088
3089 /* Old firmware would otherwise execute USB reset */
Marcel Holtmann81cac642014-01-03 03:02:36 -08003090 if (bcdDevice < 0x117)
Szymon Janca6c511c2012-05-23 12:35:46 +02003091 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmann81cac642014-01-03 03:02:36 -08003092
3093 /* Fake CSR devices with broken commands */
Johan Hedberg6cafcd92015-08-30 21:47:21 +03003094 if (bcdDevice <= 0x100 || bcdDevice == 0x134)
Marcel Holtmann81cac642014-01-03 03:02:36 -08003095 hdev->setup = btusb_setup_csr;
Jakub Pawlowski49c989a2015-03-17 09:04:17 -07003096
3097 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01003098 }
3099
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02003100 if (id->driver_info & BTUSB_SNIFFER) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02003101 struct usb_device *udev = data->udev;
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02003102
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01003103 /* New sniffer firmware has crippled HCI interface */
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02003104 if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
3105 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
3106 }
3107
Marcel Holtmann3a5ef202014-07-06 14:53:54 +02003108 if (id->driver_info & BTUSB_INTEL_BOOT) {
3109 /* A bug in the bootloader causes that interrupt interface is
3110 * only enabled after receiving SetInterface(0, AltSetting=0).
3111 */
3112 err = usb_set_interface(data->udev, 0, 0);
3113 if (err < 0) {
3114 BT_ERR("failed to set interface 0, alt 0 %d", err);
Rajat Jain10ab1332017-02-01 14:24:08 -08003115 goto out_free_dev;
Marcel Holtmann3a5ef202014-07-06 14:53:54 +02003116 }
3117 }
3118
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02003119 if (data->isoc) {
3120 err = usb_driver_claim_interface(&btusb_driver,
Marcel Holtmann89e75332014-09-16 04:44:50 +02003121 data->isoc, data);
Rajat Jain10ab1332017-02-01 14:24:08 -08003122 if (err < 0)
3123 goto out_free_dev;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02003124 }
3125
Marcel Holtmann9d08f502015-10-08 20:23:08 +02003126#ifdef CONFIG_BT_HCIBTUSB_BCM
3127 if (data->diag) {
3128 if (!usb_driver_claim_interface(&btusb_driver,
3129 data->diag, data))
3130 __set_diag_interface(hdev);
3131 else
3132 data->diag = NULL;
3133 }
3134#endif
3135
Hans de Goedeeff2d682017-11-13 14:44:16 +01003136 if (enable_autosuspend)
3137 usb_enable_autosuspend(data->udev);
3138
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003139 err = hci_register_dev(hdev);
Rajat Jain10ab1332017-02-01 14:24:08 -08003140 if (err < 0)
3141 goto out_free_dev;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003142
3143 usb_set_intfdata(intf, data);
3144
3145 return 0;
Rajat Jain10ab1332017-02-01 14:24:08 -08003146
3147out_free_dev:
3148 hci_free_dev(hdev);
3149 return err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003150}
3151
3152static void btusb_disconnect(struct usb_interface *intf)
3153{
3154 struct btusb_data *data = usb_get_intfdata(intf);
3155 struct hci_dev *hdev;
3156
3157 BT_DBG("intf %p", intf);
3158
3159 if (!data)
3160 return;
3161
3162 hdev = data->hdev;
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02003163 usb_set_intfdata(data->intf, NULL);
3164
3165 if (data->isoc)
3166 usb_set_intfdata(data->isoc, NULL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003167
Marcel Holtmann9d08f502015-10-08 20:23:08 +02003168 if (data->diag)
3169 usb_set_intfdata(data->diag, NULL);
3170
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003171 hci_unregister_dev(hdev);
3172
Marcel Holtmann9d08f502015-10-08 20:23:08 +02003173 if (intf == data->intf) {
3174 if (data->isoc)
3175 usb_driver_release_interface(&btusb_driver, data->isoc);
3176 if (data->diag)
3177 usb_driver_release_interface(&btusb_driver, data->diag);
3178 } else if (intf == data->isoc) {
3179 if (data->diag)
3180 usb_driver_release_interface(&btusb_driver, data->diag);
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02003181 usb_driver_release_interface(&btusb_driver, data->intf);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02003182 } else if (intf == data->diag) {
3183 usb_driver_release_interface(&btusb_driver, data->intf);
3184 if (data->isoc)
3185 usb_driver_release_interface(&btusb_driver, data->isoc);
3186 }
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02003187
Rajat Jainfd913ef2017-02-01 14:24:09 -08003188 if (data->oob_wake_irq)
3189 device_init_wakeup(&data->udev->dev, false);
3190
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003191 hci_free_dev(hdev);
3192}
3193
Oliver Neukum7bee5492009-08-24 23:44:59 +02003194#ifdef CONFIG_PM
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003195static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
3196{
3197 struct btusb_data *data = usb_get_intfdata(intf);
3198
3199 BT_DBG("intf %p", intf);
3200
3201 if (data->suspend_count++)
3202 return 0;
3203
Oliver Neukum7bee5492009-08-24 23:44:59 +02003204 spin_lock_irq(&data->txlock);
Alan Stern5b1b0b82011-08-19 23:49:48 +02003205 if (!(PMSG_IS_AUTO(message) && data->tx_in_flight)) {
Oliver Neukum7bee5492009-08-24 23:44:59 +02003206 set_bit(BTUSB_SUSPENDING, &data->flags);
3207 spin_unlock_irq(&data->txlock);
3208 } else {
3209 spin_unlock_irq(&data->txlock);
3210 data->suspend_count--;
3211 return -EBUSY;
3212 }
3213
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003214 cancel_work_sync(&data->work);
3215
Oliver Neukum7bee5492009-08-24 23:44:59 +02003216 btusb_stop_traffic(data);
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003217 usb_kill_anchored_urbs(&data->tx_anchor);
3218
Rajat Jainfd913ef2017-02-01 14:24:09 -08003219 if (data->oob_wake_irq && device_may_wakeup(&data->udev->dev)) {
3220 set_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags);
3221 enable_irq_wake(data->oob_wake_irq);
3222 enable_irq(data->oob_wake_irq);
3223 }
3224
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003225 return 0;
3226}
3227
Oliver Neukum7bee5492009-08-24 23:44:59 +02003228static void play_deferred(struct btusb_data *data)
3229{
3230 struct urb *urb;
3231 int err;
3232
3233 while ((urb = usb_get_from_anchor(&data->deferred))) {
Jeffy Chen19cfe912017-07-20 18:53:50 +08003234 usb_anchor_urb(urb, &data->tx_anchor);
3235
Oliver Neukum7bee5492009-08-24 23:44:59 +02003236 err = usb_submit_urb(urb, GFP_ATOMIC);
Jeffy Chen19cfe912017-07-20 18:53:50 +08003237 if (err < 0) {
3238 if (err != -EPERM && err != -ENODEV)
3239 BT_ERR("%s urb %p submission failed (%d)",
3240 data->hdev->name, urb, -err);
3241 kfree(urb->setup_packet);
3242 usb_unanchor_urb(urb);
3243 usb_free_urb(urb);
Oliver Neukum7bee5492009-08-24 23:44:59 +02003244 break;
Jeffy Chen19cfe912017-07-20 18:53:50 +08003245 }
Oliver Neukum7bee5492009-08-24 23:44:59 +02003246
3247 data->tx_in_flight++;
Jeffy Chen19cfe912017-07-20 18:53:50 +08003248 usb_free_urb(urb);
Oliver Neukum7bee5492009-08-24 23:44:59 +02003249 }
Jeffy Chen19cfe912017-07-20 18:53:50 +08003250
3251 /* Cleanup the rest deferred urbs. */
3252 while ((urb = usb_get_from_anchor(&data->deferred))) {
3253 kfree(urb->setup_packet);
3254 usb_free_urb(urb);
3255 }
Oliver Neukum7bee5492009-08-24 23:44:59 +02003256}
3257
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003258static int btusb_resume(struct usb_interface *intf)
3259{
3260 struct btusb_data *data = usb_get_intfdata(intf);
3261 struct hci_dev *hdev = data->hdev;
Oliver Neukum7bee5492009-08-24 23:44:59 +02003262 int err = 0;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003263
3264 BT_DBG("intf %p", intf);
3265
3266 if (--data->suspend_count)
3267 return 0;
3268
Rajat Jainfd913ef2017-02-01 14:24:09 -08003269 /* Disable only if not already disabled (keep it balanced) */
3270 if (test_and_clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags)) {
3271 disable_irq(data->oob_wake_irq);
3272 disable_irq_wake(data->oob_wake_irq);
3273 }
3274
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003275 if (!test_bit(HCI_RUNNING, &hdev->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +02003276 goto done;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003277
3278 if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
3279 err = btusb_submit_intr_urb(hdev, GFP_NOIO);
3280 if (err < 0) {
3281 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02003282 goto failed;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003283 }
3284 }
3285
3286 if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) {
Marcel Holtmann43c2e572009-02-04 17:41:38 +01003287 err = btusb_submit_bulk_urb(hdev, GFP_NOIO);
3288 if (err < 0) {
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003289 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02003290 goto failed;
3291 }
3292
3293 btusb_submit_bulk_urb(hdev, GFP_NOIO);
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003294 }
3295
3296 if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
3297 if (btusb_submit_isoc_urb(hdev, GFP_NOIO) < 0)
3298 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
3299 else
3300 btusb_submit_isoc_urb(hdev, GFP_NOIO);
3301 }
3302
Oliver Neukum7bee5492009-08-24 23:44:59 +02003303 spin_lock_irq(&data->txlock);
3304 play_deferred(data);
3305 clear_bit(BTUSB_SUSPENDING, &data->flags);
3306 spin_unlock_irq(&data->txlock);
3307 schedule_work(&data->work);
3308
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003309 return 0;
Oliver Neukum7bee5492009-08-24 23:44:59 +02003310
3311failed:
3312 usb_scuttle_anchored_urbs(&data->deferred);
3313done:
3314 spin_lock_irq(&data->txlock);
3315 clear_bit(BTUSB_SUSPENDING, &data->flags);
3316 spin_unlock_irq(&data->txlock);
3317
3318 return err;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003319}
Oliver Neukum7bee5492009-08-24 23:44:59 +02003320#endif
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003321
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003322static struct usb_driver btusb_driver = {
3323 .name = "btusb",
3324 .probe = btusb_probe,
3325 .disconnect = btusb_disconnect,
Oliver Neukum7bee5492009-08-24 23:44:59 +02003326#ifdef CONFIG_PM
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003327 .suspend = btusb_suspend,
3328 .resume = btusb_resume,
Oliver Neukum7bee5492009-08-24 23:44:59 +02003329#endif
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003330 .id_table = btusb_table,
Oliver Neukum7bee5492009-08-24 23:44:59 +02003331 .supports_autosuspend = 1,
Sarah Sharpe1f12eb2012-04-23 10:08:51 -07003332 .disable_hub_initiated_lpm = 1,
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003333};
3334
Greg Kroah-Hartman93f15082011-11-18 09:47:34 -08003335module_usb_driver(btusb_driver);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003336
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02003337module_param(disable_scofix, bool, 0644);
3338MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
3339
3340module_param(force_scofix, bool, 0644);
3341MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
3342
Hans de Goedeeff2d682017-11-13 14:44:16 +01003343module_param(enable_autosuspend, bool, 0644);
3344MODULE_PARM_DESC(enable_autosuspend, "Enable USB autosuspend by default");
3345
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02003346module_param(reset, bool, 0644);
3347MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
3348
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003349MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
3350MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
3351MODULE_VERSION(VERSION);
3352MODULE_LICENSE("GPL");