blob: 1cefff772cd03404e8cca4fb3426d23061e4ff81 [file] [log] [blame]
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001/*
2 *
3 * Generic Bluetooth USB driver
4 *
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02005 * Copyright (C) 2005-2008 Marcel Holtmann <marcel@holtmann.org>
Marcel Holtmann5e23b922007-10-20 14:12:34 +02006 *
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 */
23
Marcel Holtmann5e23b922007-10-20 14:12:34 +020024#include <linux/module.h>
Marcel Holtmann5e23b922007-10-20 14:12:34 +020025#include <linux/usb.h>
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -070026#include <linux/firmware.h>
Rajat Jainfd913ef2017-02-01 14:24:09 -080027#include <linux/of_device.h>
28#include <linux/of_irq.h>
Jeffy Chen017789f2017-02-24 14:24:29 +080029#include <linux/suspend.h>
Daniel Drakea2698a92015-04-16 14:09:55 -060030#include <asm/unaligned.h>
Marcel Holtmann5e23b922007-10-20 14:12:34 +020031
32#include <net/bluetooth/bluetooth.h>
33#include <net/bluetooth/hci_core.h>
34
Marcel Holtmann4185a0f2015-04-06 00:52:12 -070035#include "btintel.h"
Marcel Holtmann1df1f592015-04-05 22:52:11 -070036#include "btbcm.h"
Carlo Caionedb33c772015-05-14 10:49:09 +020037#include "btrtl.h"
Marcel Holtmann1df1f592015-04-05 22:52:11 -070038
Marcel Holtmann34dced9b2015-04-05 22:52:16 -070039#define VERSION "0.8"
Marcel Holtmanncfeb4142008-08-07 22:26:56 +020040
Rusty Russell90ab5ee2012-01-13 09:32:20 +103041static bool disable_scofix;
42static bool force_scofix;
Marcel Holtmann7a9d4022008-11-30 12:17:26 +010043
Shailendra Verma917a3332015-05-25 23:53:40 +053044static bool reset = true;
Marcel Holtmanncfeb4142008-08-07 22:26:56 +020045
46static struct usb_driver btusb_driver;
47
48#define BTUSB_IGNORE 0x01
Marcel Holtmann7a9d4022008-11-30 12:17:26 +010049#define BTUSB_DIGIANSWER 0x02
50#define BTUSB_CSR 0x04
51#define BTUSB_SNIFFER 0x08
52#define BTUSB_BCM92035 0x10
53#define BTUSB_BROKEN_ISOC 0x20
54#define BTUSB_WRONG_SCO_MTU 0x40
Steven.Li2d25f8b2011-07-01 14:02:36 +080055#define BTUSB_ATH3012 0x80
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -070056#define BTUSB_INTEL 0x100
Marcel Holtmann40df7832014-07-06 13:29:58 +020057#define BTUSB_INTEL_BOOT 0x200
58#define BTUSB_BCM_PATCHRAM 0x400
Amitkumar Karwarae8df492014-07-18 14:47:06 -070059#define BTUSB_MARVELL 0x800
Marcel Holtmann4fcef8e2015-01-01 17:34:37 -080060#define BTUSB_SWAVE 0x1000
Marcel Holtmanncda0dd72015-01-26 21:33:48 -080061#define BTUSB_INTEL_NEW 0x2000
Marcel Holtmann893ba542015-01-28 20:27:34 -080062#define BTUSB_AMP 0x4000
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +000063#define BTUSB_QCA_ROME 0x8000
Marcel Holtmann17b27722015-03-22 15:52:38 +010064#define BTUSB_BCM_APPLE 0x10000
Daniel Drakea2698a92015-04-16 14:09:55 -060065#define BTUSB_REALTEK 0x20000
Marcel Holtmann6c9d4352015-10-17 14:39:27 +020066#define BTUSB_BCM2045 0x40000
Marcel Holtmann22f8e9d2015-10-20 00:53:33 +020067#define BTUSB_IFNUM_2 0x80000
Szymon Janc418678b2016-09-01 17:22:37 +020068#define BTUSB_CW6622 0x100000
Marcel Holtmann5e23b922007-10-20 14:12:34 +020069
Marcel Holtmann54265202013-10-11 07:46:18 -070070static const struct usb_device_id btusb_table[] = {
Marcel Holtmann5e23b922007-10-20 14:12:34 +020071 /* Generic Bluetooth USB device */
72 { USB_DEVICE_INFO(0xe0, 0x01, 0x01) },
73
Marcel Holtmann893ba542015-01-28 20:27:34 -080074 /* Generic Bluetooth AMP device */
75 { USB_DEVICE_INFO(0xe0, 0x01, 0x04), .driver_info = BTUSB_AMP },
76
Daniel Draked63b2822015-07-17 11:12:25 -060077 /* Generic Bluetooth USB interface */
78 { USB_INTERFACE_INFO(0xe0, 0x01, 0x01) },
79
Henrik Rydberg1fa65352012-08-25 19:28:06 +020080 /* Apple-specific (Broadcom) devices */
Marcel Holtmann17b27722015-03-22 15:52:38 +010081 { USB_VENDOR_AND_INTERFACE_INFO(0x05ac, 0xff, 0x01, 0x01),
Marcel Holtmann22f8e9d2015-10-20 00:53:33 +020082 .driver_info = BTUSB_BCM_APPLE | BTUSB_IFNUM_2 },
Henrik Rydberg1fa65352012-08-25 19:28:06 +020083
Cho, Yu-Chen178c0592013-06-04 21:40:26 +080084 /* MediaTek MT76x0E */
85 { USB_DEVICE(0x0e8d, 0x763f) },
86
Oliver Neukumc510eae2011-09-21 11:41:45 +020087 /* Broadcom SoftSailing reporting vendor specific */
Don Zickus2e8b50632012-03-28 16:41:11 -040088 { USB_DEVICE(0x0a5c, 0x21e1) },
Oliver Neukumc510eae2011-09-21 11:41:45 +020089
Nobuhiro Iwamatsu3cd01972010-08-20 16:24:07 +090090 /* Apple MacBookPro 7,1 */
91 { USB_DEVICE(0x05ac, 0x8213) },
92
Cyril Lacoux0a79f672010-07-14 10:29:27 +040093 /* Apple iMac11,1 */
94 { USB_DEVICE(0x05ac, 0x8215) },
95
Nobuhiro Iwamatsu9c047152010-08-20 16:24:06 +090096 /* Apple MacBookPro6,2 */
97 { USB_DEVICE(0x05ac, 0x8218) },
98
Edgar (gimli) Hucek3e3ede72010-11-04 08:04:33 +010099 /* Apple MacBookAir3,1, MacBookAir3,2 */
100 { USB_DEVICE(0x05ac, 0x821b) },
101
Pieter-Augustijn Van Malleghema63b7232011-09-07 02:28:10 -0400102 /* Apple MacBookAir4,1 */
103 { USB_DEVICE(0x05ac, 0x821f) },
104
Marc-Antoine Perennou88d377b2011-03-24 14:51:21 -0300105 /* Apple MacBookPro8,2 */
106 { USB_DEVICE(0x05ac, 0x821a) },
107
Jurgen Kramerf78b68262011-09-04 18:01:42 +0200108 /* Apple MacMini5,1 */
109 { USB_DEVICE(0x05ac, 0x8281) },
110
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200111 /* AVM BlueFRITZ! USB v2.0 */
Marcel Holtmann4fcef8e2015-01-01 17:34:37 -0800112 { USB_DEVICE(0x057c, 0x3800), .driver_info = BTUSB_SWAVE },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200113
114 /* Bluetooth Ultraport Module from IBM */
115 { USB_DEVICE(0x04bf, 0x030a) },
116
117 /* ALPS Modules with non-standard id */
118 { USB_DEVICE(0x044e, 0x3001) },
119 { USB_DEVICE(0x044e, 0x3002) },
120
121 /* Ericsson with non-standard id */
122 { USB_DEVICE(0x0bdb, 0x1002) },
123
124 /* Canyon CN-BTU1 with HID interfaces */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100125 { USB_DEVICE(0x0c10, 0x0000) },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200126
Wen-chien Jesse Sungd13431c2011-11-08 14:30:22 +0800127 /* Broadcom BCM20702A0 */
128 { USB_DEVICE(0x413c, 0x8197) },
129
Marcel Holtmannd049f4e2015-01-26 20:35:32 -0800130 /* Broadcom BCM20702B0 (Dynex/Insignia) */
131 { USB_DEVICE(0x19ff, 0x0239), .driver_info = BTUSB_BCM_PATCHRAM },
132
Santtu Rekilä2faf71c2015-10-05 15:45:27 +0300133 /* Broadcom BCM43142A0 (Foxconn/Lenovo) */
Dmitry Tunin628c26b2017-07-06 14:41:13 +0300134 { USB_VENDOR_AND_INTERFACE_INFO(0x105b, 0xff, 0x01, 0x01),
135 .driver_info = BTUSB_BCM_PATCHRAM },
Santtu Rekilä2faf71c2015-10-05 15:45:27 +0300136
Christoph Haaga57bac42017-02-10 14:02:45 +0100137 /* Broadcom BCM920703 (HTC Vive) */
138 { USB_VENDOR_AND_INTERFACE_INFO(0x0bb4, 0xff, 0x01, 0x01),
139 .driver_info = BTUSB_BCM_PATCHRAM },
140
Steven Harms98514032012-04-13 14:45:55 -0400141 /* Foxconn - Hon Hai */
Heinrich Siebmanns6029ddc2014-12-03 19:32:22 +0100142 { USB_VENDOR_AND_INTERFACE_INFO(0x0489, 0xff, 0x01, 0x01),
143 .driver_info = BTUSB_BCM_PATCHRAM },
Steven Harms98514032012-04-13 14:45:55 -0400144
Matej Dubovy8f0c3042015-02-02 18:50:14 +0100145 /* Lite-On Technology - Broadcom based */
146 { USB_VENDOR_AND_INTERFACE_INFO(0x04ca, 0xff, 0x01, 0x01),
147 .driver_info = BTUSB_BCM_PATCHRAM },
148
Andy Shevchenko0b880062014-02-18 18:26:19 +0200149 /* Broadcom devices with vendor specific id */
Petri Gynther10d4c672014-05-08 15:50:01 -0700150 { USB_VENDOR_AND_INTERFACE_INFO(0x0a5c, 0xff, 0x01, 0x01),
151 .driver_info = BTUSB_BCM_PATCHRAM },
Gustavo Padovan92c385f2012-08-06 15:36:49 -0300152
Marcel Holtmannc2aef6e2014-07-21 14:02:33 +0200153 /* ASUSTek Computer - Broadcom based */
Rick Dunn9a5abdaa2015-01-17 05:29:12 +0100154 { USB_VENDOR_AND_INTERFACE_INFO(0x0b05, 0xff, 0x01, 0x01),
155 .driver_info = BTUSB_BCM_PATCHRAM },
Marcel Holtmannc2aef6e2014-07-21 14:02:33 +0200156
Ken O'Brien5bcecf32013-09-21 19:14:43 +0100157 /* Belkin F8065bf - Broadcom based */
Marcel Holtmann6331c682015-03-27 15:11:41 -0700158 { USB_VENDOR_AND_INTERFACE_INFO(0x050d, 0xff, 0x01, 0x01),
159 .driver_info = BTUSB_BCM_PATCHRAM },
Ken O'Brien5bcecf32013-09-21 19:14:43 +0100160
Jurgen Kramer9113bfd2014-02-15 12:01:09 +0100161 /* IMC Networks - Broadcom based */
Marcel Holtmann6331c682015-03-27 15:11:41 -0700162 { USB_VENDOR_AND_INTERFACE_INFO(0x13d3, 0xff, 0x01, 0x01),
163 .driver_info = BTUSB_BCM_PATCHRAM },
Jurgen Kramer9113bfd2014-02-15 12:01:09 +0100164
Wen-chien Jesse Sungfdfddc62017-01-10 15:41:13 +0800165 /* Dell Computer - Broadcom based */
166 { USB_VENDOR_AND_INTERFACE_INFO(0x413c, 0xff, 0x01, 0x01),
167 .driver_info = BTUSB_BCM_PATCHRAM },
168
Dmitry Tunin1623d0b2015-12-05 14:09:36 +0300169 /* Toshiba Corp - Broadcom based */
170 { USB_VENDOR_AND_INTERFACE_INFO(0x0930, 0xff, 0x01, 0x01),
171 .driver_info = BTUSB_BCM_PATCHRAM },
172
Marcel Holtmann40df7832014-07-06 13:29:58 +0200173 /* Intel Bluetooth USB Bootloader (RAM module) */
Marcel Holtmannd92f2df2014-07-06 14:53:55 +0200174 { USB_DEVICE(0x8087, 0x0a5a),
175 .driver_info = BTUSB_INTEL_BOOT | BTUSB_BROKEN_ISOC },
Marcel Holtmann40df7832014-07-06 13:29:58 +0200176
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200177 { } /* Terminating entry */
178};
179
180MODULE_DEVICE_TABLE(usb, btusb_table);
181
Marcel Holtmann54265202013-10-11 07:46:18 -0700182static const struct usb_device_id blacklist_table[] = {
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200183 /* CSR BlueCore devices */
184 { USB_DEVICE(0x0a12, 0x0001), .driver_info = BTUSB_CSR },
185
186 /* Broadcom BCM2033 without firmware */
187 { USB_DEVICE(0x0a5c, 0x2033), .driver_info = BTUSB_IGNORE },
188
Marcel Holtmann6c9d4352015-10-17 14:39:27 +0200189 /* Broadcom BCM2045 devices */
190 { USB_DEVICE(0x0a5c, 0x2045), .driver_info = BTUSB_BCM2045 },
191
Bala Shanmugambe931122010-11-26 17:35:46 +0530192 /* Atheros 3011 with sflash firmware */
Andy Shevchenko0b880062014-02-18 18:26:19 +0200193 { USB_DEVICE(0x0489, 0xe027), .driver_info = BTUSB_IGNORE },
194 { USB_DEVICE(0x0489, 0xe03d), .driver_info = BTUSB_IGNORE },
Alexander Ploumistos2eeff0b2015-02-13 21:05:11 +0200195 { USB_DEVICE(0x04f2, 0xaff1), .driver_info = BTUSB_IGNORE },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200196 { USB_DEVICE(0x0930, 0x0215), .driver_info = BTUSB_IGNORE },
Bala Shanmugambe931122010-11-26 17:35:46 +0530197 { USB_DEVICE(0x0cf3, 0x3002), .driver_info = BTUSB_IGNORE },
Marek Vasut6eda5412012-06-08 14:32:50 +0200198 { USB_DEVICE(0x0cf3, 0xe019), .driver_info = BTUSB_IGNORE },
Andy Ross2a7bccc2011-05-09 16:11:16 -0700199 { USB_DEVICE(0x13d3, 0x3304), .driver_info = BTUSB_IGNORE },
Bala Shanmugambe931122010-11-26 17:35:46 +0530200
Cho, Yu-Chen509e7862011-01-26 17:10:59 +0800201 /* Atheros AR9285 Malbec with sflash firmware */
202 { USB_DEVICE(0x03f0, 0x311d), .driver_info = BTUSB_IGNORE },
203
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530204 /* Atheros 3012 with sflash firmware */
Andy Shevchenko0b880062014-02-18 18:26:19 +0200205 { USB_DEVICE(0x0489, 0xe04d), .driver_info = BTUSB_ATH3012 },
206 { USB_DEVICE(0x0489, 0xe04e), .driver_info = BTUSB_ATH3012 },
207 { USB_DEVICE(0x0489, 0xe056), .driver_info = BTUSB_ATH3012 },
208 { USB_DEVICE(0x0489, 0xe057), .driver_info = BTUSB_ATH3012 },
209 { USB_DEVICE(0x0489, 0xe05f), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin692c0622015-06-06 20:25:40 +0300210 { USB_DEVICE(0x0489, 0xe076), .driver_info = BTUSB_ATH3012 },
Anantha Krishnan4b552bc2014-10-06 16:31:49 +0530211 { USB_DEVICE(0x0489, 0xe078), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin28c971d2016-02-10 00:49:11 +0300212 { USB_DEVICE(0x0489, 0xe095), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200213 { USB_DEVICE(0x04c5, 0x1330), .driver_info = BTUSB_ATH3012 },
214 { USB_DEVICE(0x04ca, 0x3004), .driver_info = BTUSB_ATH3012 },
215 { USB_DEVICE(0x04ca, 0x3005), .driver_info = BTUSB_ATH3012 },
216 { USB_DEVICE(0x04ca, 0x3006), .driver_info = BTUSB_ATH3012 },
Mohammed Habibulla1fb4e092014-04-17 11:37:13 -0700217 { USB_DEVICE(0x04ca, 0x3007), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200218 { USB_DEVICE(0x04ca, 0x3008), .driver_info = BTUSB_ATH3012 },
219 { USB_DEVICE(0x04ca, 0x300b), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin7e730c7f2015-06-18 20:41:51 +0300220 { USB_DEVICE(0x04ca, 0x300d), .driver_info = BTUSB_ATH3012 },
Dmitry Tuninec0810d2015-05-02 13:36:58 +0300221 { USB_DEVICE(0x04ca, 0x300f), .driver_info = BTUSB_ATH3012 },
Janne Heikkinen134d3b32014-12-09 07:44:51 +0200222 { USB_DEVICE(0x04ca, 0x3010), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin81d90442016-02-28 11:04:06 +0300223 { USB_DEVICE(0x04ca, 0x3014), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin441ad622017-01-05 13:19:53 +0300224 { USB_DEVICE(0x04ca, 0x3018), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200225 { USB_DEVICE(0x0930, 0x0219), .driver_info = BTUSB_ATH3012 },
Dmitry Tunincd355ff2015-10-05 19:29:33 +0300226 { USB_DEVICE(0x0930, 0x021c), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200227 { USB_DEVICE(0x0930, 0x0220), .driver_info = BTUSB_ATH3012 },
Vincent Zwanenburg89d29752014-08-08 12:33:56 +0100228 { USB_DEVICE(0x0930, 0x0227), .driver_info = BTUSB_ATH3012 },
Andy Shevchenkoa735f9e2014-02-18 18:26:20 +0200229 { USB_DEVICE(0x0b05, 0x17d0), .driver_info = BTUSB_ATH3012 },
Ming Leid66629c2013-03-15 11:00:39 +0800230 { USB_DEVICE(0x0cf3, 0x0036), .driver_info = BTUSB_ATH3012 },
Steven.Li2d25f8b2011-07-01 14:02:36 +0800231 { USB_DEVICE(0x0cf3, 0x3004), .driver_info = BTUSB_ATH3012 },
Sunguk Lee94a32d102013-03-12 04:41:58 +0900232 { USB_DEVICE(0x0cf3, 0x3008), .driver_info = BTUSB_ATH3012 },
Cho, Yu-Chen07c0ea82012-03-14 22:01:21 +0200233 { USB_DEVICE(0x0cf3, 0x311d), .driver_info = BTUSB_ATH3012 },
Oliver Neukumb1312372014-01-16 15:37:11 +0100234 { USB_DEVICE(0x0cf3, 0x311e), .driver_info = BTUSB_ATH3012 },
Oliver Neukum1e56f1e2014-01-16 16:02:58 +0100235 { USB_DEVICE(0x0cf3, 0x311f), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200236 { USB_DEVICE(0x0cf3, 0x3121), .driver_info = BTUSB_ATH3012 },
Ming Leiebaf5792013-03-18 23:45:11 +0800237 { USB_DEVICE(0x0cf3, 0x817a), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin18e0afa2015-10-16 11:45:26 +0300238 { USB_DEVICE(0x0cf3, 0x817b), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200239 { USB_DEVICE(0x0cf3, 0xe003), .driver_info = BTUSB_ATH3012 },
AceLan Kaoac713112012-04-19 14:53:45 +0800240 { USB_DEVICE(0x0cf3, 0xe004), .driver_info = BTUSB_ATH3012 },
Peng Chen0a3658c2013-08-30 17:41:40 +0800241 { USB_DEVICE(0x0cf3, 0xe005), .driver_info = BTUSB_ATH3012 },
Wen-chien Jesse Sungca79f232015-05-13 11:39:24 +0800242 { USB_DEVICE(0x0cf3, 0xe006), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200243 { USB_DEVICE(0x13d3, 0x3362), .driver_info = BTUSB_ATH3012 },
244 { USB_DEVICE(0x13d3, 0x3375), .driver_info = BTUSB_ATH3012 },
AceLan Kaoeed307e2012-12-11 11:41:20 +0800245 { USB_DEVICE(0x13d3, 0x3393), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin609574e2016-02-10 15:33:17 +0300246 { USB_DEVICE(0x13d3, 0x3395), .driver_info = BTUSB_ATH3012 },
Sujith Manoharan5b77a1f2013-07-15 09:29:03 +0530247 { USB_DEVICE(0x13d3, 0x3402), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin3bb30a7c2014-11-25 20:19:52 +0300248 { USB_DEVICE(0x13d3, 0x3408), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin033efa92015-01-18 00:16:51 +0300249 { USB_DEVICE(0x13d3, 0x3423), .driver_info = BTUSB_ATH3012 },
Anantha Krishnanfa2f1392014-07-08 19:25:08 +0530250 { USB_DEVICE(0x13d3, 0x3432), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin75c6aca2016-03-04 01:32:19 +0300251 { USB_DEVICE(0x13d3, 0x3472), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin0d0cef62015-06-06 20:29:25 +0300252 { USB_DEVICE(0x13d3, 0x3474), .driver_info = BTUSB_ATH3012 },
Lauro Costa72f9f8b2016-05-09 17:36:11 -0300253 { USB_DEVICE(0x13d3, 0x3487), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin12d86892016-07-12 01:35:18 +0300254 { USB_DEVICE(0x13d3, 0x3490), .driver_info = BTUSB_ATH3012 },
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530255
Cho, Yu-Chene9036e32011-02-15 10:20:07 +0800256 /* Atheros AR5BBU12 with sflash firmware */
257 { USB_DEVICE(0x0489, 0xe02c), .driver_info = BTUSB_IGNORE },
258
Michael Gruetzner85d59722012-05-02 22:33:40 +0200259 /* Atheros AR5BBU12 with sflash firmware */
Yevgeniy Melnichukbc21fde2012-08-07 19:48:10 +0530260 { USB_DEVICE(0x0489, 0xe036), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200261 { USB_DEVICE(0x0489, 0xe03c), .driver_info = BTUSB_ATH3012 },
Michael Gruetzner85d59722012-05-02 22:33:40 +0200262
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +0000263 /* QCA ROME chipset */
Wen-chien Jesse Sung20541112015-05-13 11:39:25 +0800264 { USB_DEVICE(0x0cf3, 0xe007), .driver_info = BTUSB_QCA_ROME },
Kai-Heng Feng93519932016-08-16 12:50:06 +0800265 { USB_DEVICE(0x0cf3, 0xe009), .driver_info = BTUSB_QCA_ROME },
Marcel Holtmannc9e44472015-03-16 23:56:04 -0700266 { USB_DEVICE(0x0cf3, 0xe300), .driver_info = BTUSB_QCA_ROME },
Gabriel1eef1c32017-02-20 12:32:22 -0500267 { USB_DEVICE(0x0cf3, 0xe301), .driver_info = BTUSB_QCA_ROME },
Marcel Holtmannc9e44472015-03-16 23:56:04 -0700268 { USB_DEVICE(0x0cf3, 0xe360), .driver_info = BTUSB_QCA_ROME },
Yvain THONNART47ca5892016-06-11 15:43:30 +0200269 { USB_DEVICE(0x0489, 0xe092), .driver_info = BTUSB_QCA_ROME },
Shih-Yuan Lee (FourDollars)06e41d82017-06-14 14:42:00 +0800270 { USB_DEVICE(0x0489, 0xe0a2), .driver_info = BTUSB_QCA_ROME },
Dmitry Tunin1144a4e2016-09-21 19:13:08 +0300271 { USB_DEVICE(0x04ca, 0x3011), .driver_info = BTUSB_QCA_ROME },
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +0000272
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200273 /* Broadcom BCM2035 */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100274 { USB_DEVICE(0x0a5c, 0x2009), .driver_info = BTUSB_BCM92035 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200275 { USB_DEVICE(0x0a5c, 0x200a), .driver_info = BTUSB_WRONG_SCO_MTU },
276 { USB_DEVICE(0x0a5c, 0x2035), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200277
278 /* Broadcom BCM2045 */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100279 { USB_DEVICE(0x0a5c, 0x2039), .driver_info = BTUSB_WRONG_SCO_MTU },
280 { USB_DEVICE(0x0a5c, 0x2101), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmannbdbef3d2008-09-23 00:16:35 +0200281
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200282 /* IBM/Lenovo ThinkPad with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100283 { USB_DEVICE(0x0a5c, 0x201e), .driver_info = BTUSB_WRONG_SCO_MTU },
284 { USB_DEVICE(0x0a5c, 0x2110), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200285
286 /* HP laptop with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100287 { USB_DEVICE(0x03f0, 0x171d), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200288
289 /* Dell laptop with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100290 { USB_DEVICE(0x413c, 0x8126), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200291
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100292 /* Dell Wireless 370 and 410 devices */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100293 { USB_DEVICE(0x413c, 0x8152), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100294 { USB_DEVICE(0x413c, 0x8156), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200295
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100296 /* Belkin F8T012 and F8T013 devices */
297 { USB_DEVICE(0x050d, 0x0012), .driver_info = BTUSB_WRONG_SCO_MTU },
298 { USB_DEVICE(0x050d, 0x0013), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200299
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100300 /* Asus WL-BTD202 device */
301 { USB_DEVICE(0x0b05, 0x1715), .driver_info = BTUSB_WRONG_SCO_MTU },
302
303 /* Kensington Bluetooth USB adapter */
304 { USB_DEVICE(0x047d, 0x105e), .driver_info = BTUSB_WRONG_SCO_MTU },
305
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200306 /* RTX Telecom based adapters with buggy SCO support */
307 { USB_DEVICE(0x0400, 0x0807), .driver_info = BTUSB_BROKEN_ISOC },
308 { USB_DEVICE(0x0400, 0x080a), .driver_info = BTUSB_BROKEN_ISOC },
309
310 /* CONWISE Technology based adapters with buggy SCO support */
Szymon Janc418678b2016-09-01 17:22:37 +0200311 { USB_DEVICE(0x0e5e, 0x6622),
312 .driver_info = BTUSB_BROKEN_ISOC | BTUSB_CW6622},
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200313
Marcel Holtmann4fcef8e2015-01-01 17:34:37 -0800314 /* Roper Class 1 Bluetooth Dongle (Silicon Wave based) */
Aleksei Volkov2eeac872015-06-08 12:02:10 +0300315 { USB_DEVICE(0x1310, 0x0001), .driver_info = BTUSB_SWAVE },
Marcel Holtmann4fcef8e2015-01-01 17:34:37 -0800316
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200317 /* Digianswer devices */
318 { USB_DEVICE(0x08fd, 0x0001), .driver_info = BTUSB_DIGIANSWER },
319 { USB_DEVICE(0x08fd, 0x0002), .driver_info = BTUSB_IGNORE },
320
321 /* CSR BlueCore Bluetooth Sniffer */
Marcel Holtmann4f64fa82014-07-07 00:12:04 +0200322 { USB_DEVICE(0x0a12, 0x0002),
323 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200324
325 /* Frontline ComProbe Bluetooth Sniffer */
Marcel Holtmann4f64fa82014-07-07 00:12:04 +0200326 { USB_DEVICE(0x16d3, 0x0002),
327 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200328
Marcel Holtmanncb1ee892015-01-28 19:41:42 -0800329 /* Marvell Bluetooth devices */
330 { USB_DEVICE(0x1286, 0x2044), .driver_info = BTUSB_MARVELL },
331 { USB_DEVICE(0x1286, 0x2046), .driver_info = BTUSB_MARVELL },
Amitkumar Karwar1165df02016-09-28 16:18:35 +0530332 { USB_DEVICE(0x1286, 0x204e), .driver_info = BTUSB_MARVELL },
Marcel Holtmanncb1ee892015-01-28 19:41:42 -0800333
Marcel Holtmannd0ac9eb2015-01-28 19:41:43 -0800334 /* Intel Bluetooth devices */
Marcel Holtmannde766142017-03-06 15:38:28 -0800335 { USB_DEVICE(0x8087, 0x0025), .driver_info = BTUSB_INTEL_NEW },
Marcel Holtmann407550f2015-02-22 15:41:18 -0800336 { USB_DEVICE(0x8087, 0x07da), .driver_info = BTUSB_CSR },
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -0700337 { USB_DEVICE(0x8087, 0x07dc), .driver_info = BTUSB_INTEL },
Tedd Ho-Jeong Anef4e5e42013-11-12 13:10:58 -0800338 { USB_DEVICE(0x8087, 0x0a2a), .driver_info = BTUSB_INTEL },
Marcel Holtmanncda0dd72015-01-26 21:33:48 -0800339 { USB_DEVICE(0x8087, 0x0a2b), .driver_info = BTUSB_INTEL_NEW },
Tedd Ho-Jeong An439e65d2016-06-20 13:43:40 -0700340 { USB_DEVICE(0x8087, 0x0aa7), .driver_info = BTUSB_INTEL },
Tedd Ho-Jeong An86a61292017-05-01 13:35:12 -0700341 { USB_DEVICE(0x8087, 0x0aaa), .driver_info = BTUSB_INTEL_NEW },
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -0700342
Marcel Holtmannd0ac9eb2015-01-28 19:41:43 -0800343 /* Other Intel Bluetooth devices */
344 { USB_VENDOR_AND_INTERFACE_INFO(0x8087, 0xe0, 0x01, 0x01),
345 .driver_info = BTUSB_IGNORE },
Amitkumar Karwarae8df492014-07-18 14:47:06 -0700346
Daniel Drakea2698a92015-04-16 14:09:55 -0600347 /* Realtek Bluetooth devices */
348 { USB_VENDOR_AND_INTERFACE_INFO(0x0bda, 0xe0, 0x01, 0x01),
349 .driver_info = BTUSB_REALTEK },
350
351 /* Additional Realtek 8723AE Bluetooth devices */
352 { USB_DEVICE(0x0930, 0x021d), .driver_info = BTUSB_REALTEK },
353 { USB_DEVICE(0x13d3, 0x3394), .driver_info = BTUSB_REALTEK },
354
355 /* Additional Realtek 8723BE Bluetooth devices */
356 { USB_DEVICE(0x0489, 0xe085), .driver_info = BTUSB_REALTEK },
357 { USB_DEVICE(0x0489, 0xe08b), .driver_info = BTUSB_REALTEK },
358 { USB_DEVICE(0x13d3, 0x3410), .driver_info = BTUSB_REALTEK },
359 { USB_DEVICE(0x13d3, 0x3416), .driver_info = BTUSB_REALTEK },
360 { USB_DEVICE(0x13d3, 0x3459), .driver_info = BTUSB_REALTEK },
361
362 /* Additional Realtek 8821AE Bluetooth devices */
363 { USB_DEVICE(0x0b05, 0x17dc), .driver_info = BTUSB_REALTEK },
364 { USB_DEVICE(0x13d3, 0x3414), .driver_info = BTUSB_REALTEK },
365 { USB_DEVICE(0x13d3, 0x3458), .driver_info = BTUSB_REALTEK },
366 { USB_DEVICE(0x13d3, 0x3461), .driver_info = BTUSB_REALTEK },
367 { USB_DEVICE(0x13d3, 0x3462), .driver_info = BTUSB_REALTEK },
368
Peter Poklop4481c072015-08-15 20:47:09 +0200369 /* Silicon Wave based devices */
370 { USB_DEVICE(0x0c10, 0x0000), .driver_info = BTUSB_SWAVE },
371
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200372 { } /* Terminating entry */
373};
374
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200375#define BTUSB_MAX_ISOC_FRAMES 10
376
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200377#define BTUSB_INTR_RUNNING 0
378#define BTUSB_BULK_RUNNING 1
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200379#define BTUSB_ISOC_RUNNING 2
Oliver Neukum7bee5492009-08-24 23:44:59 +0200380#define BTUSB_SUSPENDING 3
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -0300381#define BTUSB_DID_ISO_RESUME 4
Marcel Holtmanncda0dd72015-01-26 21:33:48 -0800382#define BTUSB_BOOTLOADER 5
383#define BTUSB_DOWNLOADING 6
Marcel Holtmannce6bb922015-01-28 01:58:40 -0800384#define BTUSB_FIRMWARE_LOADED 7
Marcel Holtmanncda0dd72015-01-26 21:33:48 -0800385#define BTUSB_FIRMWARE_FAILED 8
Marcel Holtmannce6bb922015-01-28 01:58:40 -0800386#define BTUSB_BOOTING 9
Daniel Drake04b8c812015-05-21 08:23:50 -0600387#define BTUSB_RESET_RESUME 10
Marcel Holtmann9d08f502015-10-08 20:23:08 +0200388#define BTUSB_DIAG_RUNNING 11
Rajat Jainfd913ef2017-02-01 14:24:09 -0800389#define BTUSB_OOB_WAKE_ENABLED 12
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200390
391struct btusb_data {
392 struct hci_dev *hdev;
393 struct usb_device *udev;
Marcel Holtmann5fbcd262008-09-23 00:16:36 +0200394 struct usb_interface *intf;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200395 struct usb_interface *isoc;
Marcel Holtmann9d08f502015-10-08 20:23:08 +0200396 struct usb_interface *diag;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200397
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200398 unsigned long flags;
399
400 struct work_struct work;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200401 struct work_struct waker;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200402
Marcel Holtmann803b5832014-09-16 08:00:29 +0200403 struct usb_anchor deferred;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200404 struct usb_anchor tx_anchor;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200405 int tx_in_flight;
406 spinlock_t txlock;
407
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200408 struct usb_anchor intr_anchor;
409 struct usb_anchor bulk_anchor;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200410 struct usb_anchor isoc_anchor;
Marcel Holtmann9d08f502015-10-08 20:23:08 +0200411 struct usb_anchor diag_anchor;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200412 spinlock_t rxlock;
413
414 struct sk_buff *evt_skb;
415 struct sk_buff *acl_skb;
416 struct sk_buff *sco_skb;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200417
418 struct usb_endpoint_descriptor *intr_ep;
419 struct usb_endpoint_descriptor *bulk_tx_ep;
420 struct usb_endpoint_descriptor *bulk_rx_ep;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200421 struct usb_endpoint_descriptor *isoc_tx_ep;
422 struct usb_endpoint_descriptor *isoc_rx_ep;
Marcel Holtmann9d08f502015-10-08 20:23:08 +0200423 struct usb_endpoint_descriptor *diag_tx_ep;
424 struct usb_endpoint_descriptor *diag_rx_ep;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200425
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100426 __u8 cmdreq_type;
Marcel Holtmann893ba542015-01-28 20:27:34 -0800427 __u8 cmdreq;
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100428
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100429 unsigned int sco_num;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200430 int isoc_altsetting;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +0100431 int suspend_count;
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +0100432
Marcel Holtmann97307f52015-01-12 13:51:10 -0800433 int (*recv_event)(struct hci_dev *hdev, struct sk_buff *skb);
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +0100434 int (*recv_bulk)(struct btusb_data *data, void *buffer, int count);
Kim, Ben Young Taeace31982015-02-15 23:06:14 +0000435
436 int (*setup_on_usb)(struct hci_dev *hdev);
Rajat Jainfd913ef2017-02-01 14:24:09 -0800437
438 int oob_wake_irq; /* irq for out-of-band wake-on-bt */
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200439};
440
Marcel Holtmann803b5832014-09-16 08:00:29 +0200441static inline void btusb_free_frags(struct btusb_data *data)
442{
443 unsigned long flags;
444
445 spin_lock_irqsave(&data->rxlock, flags);
446
447 kfree_skb(data->evt_skb);
448 data->evt_skb = NULL;
449
450 kfree_skb(data->acl_skb);
451 data->acl_skb = NULL;
452
453 kfree_skb(data->sco_skb);
454 data->sco_skb = NULL;
455
456 spin_unlock_irqrestore(&data->rxlock, flags);
457}
458
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200459static int btusb_recv_intr(struct btusb_data *data, void *buffer, int count)
460{
Marcel Holtmann803b5832014-09-16 08:00:29 +0200461 struct sk_buff *skb;
462 int err = 0;
463
464 spin_lock(&data->rxlock);
465 skb = data->evt_skb;
466
467 while (count) {
468 int len;
469
470 if (!skb) {
471 skb = bt_skb_alloc(HCI_MAX_EVENT_SIZE, GFP_ATOMIC);
472 if (!skb) {
473 err = -ENOMEM;
474 break;
475 }
476
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100477 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
478 hci_skb_expect(skb) = HCI_EVENT_HDR_SIZE;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200479 }
480
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100481 len = min_t(uint, hci_skb_expect(skb), count);
Johannes Berg59ae1d12017-06-16 14:29:20 +0200482 skb_put_data(skb, buffer, len);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200483
484 count -= len;
485 buffer += len;
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100486 hci_skb_expect(skb) -= len;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200487
488 if (skb->len == HCI_EVENT_HDR_SIZE) {
489 /* Complete event header */
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100490 hci_skb_expect(skb) = hci_event_hdr(skb)->plen;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200491
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100492 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
Marcel Holtmann803b5832014-09-16 08:00:29 +0200493 kfree_skb(skb);
494 skb = NULL;
495
496 err = -EILSEQ;
497 break;
498 }
499 }
500
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100501 if (!hci_skb_expect(skb)) {
Marcel Holtmann803b5832014-09-16 08:00:29 +0200502 /* Complete frame */
Marcel Holtmann97307f52015-01-12 13:51:10 -0800503 data->recv_event(data->hdev, skb);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200504 skb = NULL;
505 }
506 }
507
508 data->evt_skb = skb;
509 spin_unlock(&data->rxlock);
510
511 return err;
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200512}
513
514static int btusb_recv_bulk(struct btusb_data *data, void *buffer, int count)
515{
Marcel Holtmann803b5832014-09-16 08:00:29 +0200516 struct sk_buff *skb;
517 int err = 0;
518
519 spin_lock(&data->rxlock);
520 skb = data->acl_skb;
521
522 while (count) {
523 int len;
524
525 if (!skb) {
526 skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
527 if (!skb) {
528 err = -ENOMEM;
529 break;
530 }
531
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100532 hci_skb_pkt_type(skb) = HCI_ACLDATA_PKT;
533 hci_skb_expect(skb) = HCI_ACL_HDR_SIZE;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200534 }
535
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100536 len = min_t(uint, hci_skb_expect(skb), count);
Johannes Berg59ae1d12017-06-16 14:29:20 +0200537 skb_put_data(skb, buffer, len);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200538
539 count -= len;
540 buffer += len;
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100541 hci_skb_expect(skb) -= len;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200542
543 if (skb->len == HCI_ACL_HDR_SIZE) {
544 __le16 dlen = hci_acl_hdr(skb)->dlen;
545
546 /* Complete ACL header */
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100547 hci_skb_expect(skb) = __le16_to_cpu(dlen);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200548
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100549 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
Marcel Holtmann803b5832014-09-16 08:00:29 +0200550 kfree_skb(skb);
551 skb = NULL;
552
553 err = -EILSEQ;
554 break;
555 }
556 }
557
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100558 if (!hci_skb_expect(skb)) {
Marcel Holtmann803b5832014-09-16 08:00:29 +0200559 /* Complete frame */
560 hci_recv_frame(data->hdev, skb);
561 skb = NULL;
562 }
563 }
564
565 data->acl_skb = skb;
566 spin_unlock(&data->rxlock);
567
568 return err;
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200569}
570
571static int btusb_recv_isoc(struct btusb_data *data, void *buffer, int count)
572{
Marcel Holtmann803b5832014-09-16 08:00:29 +0200573 struct sk_buff *skb;
574 int err = 0;
575
576 spin_lock(&data->rxlock);
577 skb = data->sco_skb;
578
579 while (count) {
580 int len;
581
582 if (!skb) {
583 skb = bt_skb_alloc(HCI_MAX_SCO_SIZE, GFP_ATOMIC);
584 if (!skb) {
585 err = -ENOMEM;
586 break;
587 }
588
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100589 hci_skb_pkt_type(skb) = HCI_SCODATA_PKT;
590 hci_skb_expect(skb) = HCI_SCO_HDR_SIZE;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200591 }
592
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100593 len = min_t(uint, hci_skb_expect(skb), count);
Johannes Berg59ae1d12017-06-16 14:29:20 +0200594 skb_put_data(skb, buffer, len);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200595
596 count -= len;
597 buffer += len;
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100598 hci_skb_expect(skb) -= len;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200599
600 if (skb->len == HCI_SCO_HDR_SIZE) {
601 /* Complete SCO header */
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100602 hci_skb_expect(skb) = hci_sco_hdr(skb)->dlen;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200603
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100604 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
Marcel Holtmann803b5832014-09-16 08:00:29 +0200605 kfree_skb(skb);
606 skb = NULL;
607
608 err = -EILSEQ;
609 break;
610 }
611 }
612
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100613 if (!hci_skb_expect(skb)) {
Marcel Holtmann803b5832014-09-16 08:00:29 +0200614 /* Complete frame */
615 hci_recv_frame(data->hdev, skb);
616 skb = NULL;
617 }
618 }
619
620 data->sco_skb = skb;
621 spin_unlock(&data->rxlock);
622
623 return err;
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200624}
625
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200626static void btusb_intr_complete(struct urb *urb)
627{
628 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +0100629 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200630 int err;
631
Marcel Holtmann89e75332014-09-16 04:44:50 +0200632 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
633 urb->actual_length);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200634
635 if (!test_bit(HCI_RUNNING, &hdev->flags))
636 return;
637
638 if (urb->status == 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200639 hdev->stat.byte_rx += urb->actual_length;
640
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200641 if (btusb_recv_intr(data, urb->transfer_buffer,
642 urb->actual_length) < 0) {
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200643 BT_ERR("%s corrupted event packet", hdev->name);
644 hdev->stat.err_rx++;
645 }
Champion Chen85560c42014-09-06 14:06:08 -0500646 } else if (urb->status == -ENOENT) {
647 /* Avoid suspend failed when usb_kill_urb */
648 return;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200649 }
650
651 if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
652 return;
653
Oliver Neukum7bee5492009-08-24 23:44:59 +0200654 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200655 usb_anchor_urb(urb, &data->intr_anchor);
656
657 err = usb_submit_urb(urb, GFP_ATOMIC);
658 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200659 /* -EPERM: urb is being killed;
Derek Robsond98422c2017-07-22 13:47:07 +1200660 * -ENODEV: device got disconnected
661 */
Paul Bolle4935f1c2011-08-09 17:16:28 +0200662 if (err != -EPERM && err != -ENODEV)
Stefan Seyfried61faddf2010-11-30 21:49:08 +0100663 BT_ERR("%s urb %p failed to resubmit (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200664 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200665 usb_unanchor_urb(urb);
666 }
667}
668
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100669static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200670{
David Herrmann155961e2012-02-09 21:58:32 +0100671 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200672 struct urb *urb;
673 unsigned char *buf;
674 unsigned int pipe;
675 int err, size;
676
677 BT_DBG("%s", hdev->name);
678
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200679 if (!data->intr_ep)
680 return -ENODEV;
681
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100682 urb = usb_alloc_urb(0, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200683 if (!urb)
684 return -ENOMEM;
685
686 size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
687
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100688 buf = kmalloc(size, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200689 if (!buf) {
690 usb_free_urb(urb);
691 return -ENOMEM;
692 }
693
694 pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
695
696 usb_fill_int_urb(urb, data->udev, pipe, buf, size,
Marcel Holtmann89e75332014-09-16 04:44:50 +0200697 btusb_intr_complete, hdev, data->intr_ep->bInterval);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200698
699 urb->transfer_flags |= URB_FREE_BUFFER;
700
701 usb_anchor_urb(urb, &data->intr_anchor);
702
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100703 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200704 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +0200705 if (err != -EPERM && err != -ENODEV)
706 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200707 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200708 usb_unanchor_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200709 }
710
711 usb_free_urb(urb);
712
713 return err;
714}
715
716static void btusb_bulk_complete(struct urb *urb)
717{
718 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +0100719 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200720 int err;
721
Marcel Holtmann89e75332014-09-16 04:44:50 +0200722 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
723 urb->actual_length);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200724
725 if (!test_bit(HCI_RUNNING, &hdev->flags))
726 return;
727
728 if (urb->status == 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200729 hdev->stat.byte_rx += urb->actual_length;
730
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +0100731 if (data->recv_bulk(data, urb->transfer_buffer,
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200732 urb->actual_length) < 0) {
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200733 BT_ERR("%s corrupted ACL packet", hdev->name);
734 hdev->stat.err_rx++;
735 }
Champion Chen85560c42014-09-06 14:06:08 -0500736 } else if (urb->status == -ENOENT) {
737 /* Avoid suspend failed when usb_kill_urb */
738 return;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200739 }
740
741 if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
742 return;
743
744 usb_anchor_urb(urb, &data->bulk_anchor);
Oliver Neukum652fd782009-12-16 19:23:43 +0100745 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200746
747 err = usb_submit_urb(urb, GFP_ATOMIC);
748 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200749 /* -EPERM: urb is being killed;
Derek Robsond98422c2017-07-22 13:47:07 +1200750 * -ENODEV: device got disconnected
751 */
Paul Bolle4935f1c2011-08-09 17:16:28 +0200752 if (err != -EPERM && err != -ENODEV)
Stefan Seyfried61faddf2010-11-30 21:49:08 +0100753 BT_ERR("%s urb %p failed to resubmit (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200754 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200755 usb_unanchor_urb(urb);
756 }
757}
758
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100759static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200760{
David Herrmann155961e2012-02-09 21:58:32 +0100761 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200762 struct urb *urb;
763 unsigned char *buf;
764 unsigned int pipe;
Vikram Kandukuri290ba202009-07-02 14:31:59 +0530765 int err, size = HCI_MAX_FRAME_SIZE;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200766
767 BT_DBG("%s", hdev->name);
768
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200769 if (!data->bulk_rx_ep)
770 return -ENODEV;
771
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100772 urb = usb_alloc_urb(0, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200773 if (!urb)
774 return -ENOMEM;
775
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100776 buf = kmalloc(size, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200777 if (!buf) {
778 usb_free_urb(urb);
779 return -ENOMEM;
780 }
781
782 pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
783
Marcel Holtmann89e75332014-09-16 04:44:50 +0200784 usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
785 btusb_bulk_complete, hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200786
787 urb->transfer_flags |= URB_FREE_BUFFER;
788
Oliver Neukum7bee5492009-08-24 23:44:59 +0200789 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200790 usb_anchor_urb(urb, &data->bulk_anchor);
791
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100792 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200793 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +0200794 if (err != -EPERM && err != -ENODEV)
795 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200796 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200797 usb_unanchor_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200798 }
799
800 usb_free_urb(urb);
801
802 return err;
803}
804
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200805static void btusb_isoc_complete(struct urb *urb)
806{
807 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +0100808 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200809 int i, err;
810
Marcel Holtmann89e75332014-09-16 04:44:50 +0200811 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
812 urb->actual_length);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200813
814 if (!test_bit(HCI_RUNNING, &hdev->flags))
815 return;
816
817 if (urb->status == 0) {
818 for (i = 0; i < urb->number_of_packets; i++) {
819 unsigned int offset = urb->iso_frame_desc[i].offset;
820 unsigned int length = urb->iso_frame_desc[i].actual_length;
821
822 if (urb->iso_frame_desc[i].status)
823 continue;
824
825 hdev->stat.byte_rx += length;
826
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200827 if (btusb_recv_isoc(data, urb->transfer_buffer + offset,
828 length) < 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200829 BT_ERR("%s corrupted SCO packet", hdev->name);
830 hdev->stat.err_rx++;
831 }
832 }
Champion Chen85560c42014-09-06 14:06:08 -0500833 } else if (urb->status == -ENOENT) {
834 /* Avoid suspend failed when usb_kill_urb */
835 return;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200836 }
837
838 if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags))
839 return;
840
841 usb_anchor_urb(urb, &data->isoc_anchor);
842
843 err = usb_submit_urb(urb, GFP_ATOMIC);
844 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200845 /* -EPERM: urb is being killed;
Derek Robsond98422c2017-07-22 13:47:07 +1200846 * -ENODEV: device got disconnected
847 */
Paul Bolle4935f1c2011-08-09 17:16:28 +0200848 if (err != -EPERM && err != -ENODEV)
Stefan Seyfried61faddf2010-11-30 21:49:08 +0100849 BT_ERR("%s urb %p failed to resubmit (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200850 hdev->name, urb, -err);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200851 usb_unanchor_urb(urb);
852 }
853}
854
Jesper Juhl42b16b32011-01-17 00:09:38 +0100855static inline void __fill_isoc_descriptor(struct urb *urb, int len, int mtu)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200856{
857 int i, offset = 0;
858
859 BT_DBG("len %d mtu %d", len, mtu);
860
861 for (i = 0; i < BTUSB_MAX_ISOC_FRAMES && len >= mtu;
862 i++, offset += mtu, len -= mtu) {
863 urb->iso_frame_desc[i].offset = offset;
864 urb->iso_frame_desc[i].length = mtu;
865 }
866
867 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
868 urb->iso_frame_desc[i].offset = offset;
869 urb->iso_frame_desc[i].length = len;
870 i++;
871 }
872
873 urb->number_of_packets = i;
874}
875
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100876static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200877{
David Herrmann155961e2012-02-09 21:58:32 +0100878 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200879 struct urb *urb;
880 unsigned char *buf;
881 unsigned int pipe;
882 int err, size;
883
884 BT_DBG("%s", hdev->name);
885
886 if (!data->isoc_rx_ep)
887 return -ENODEV;
888
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100889 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200890 if (!urb)
891 return -ENOMEM;
892
893 size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) *
894 BTUSB_MAX_ISOC_FRAMES;
895
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100896 buf = kmalloc(size, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200897 if (!buf) {
898 usb_free_urb(urb);
899 return -ENOMEM;
900 }
901
902 pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress);
903
Bing Zhaofa0fb932011-12-20 18:19:00 -0800904 usb_fill_int_urb(urb, data->udev, pipe, buf, size, btusb_isoc_complete,
Marcel Holtmann89e75332014-09-16 04:44:50 +0200905 hdev, data->isoc_rx_ep->bInterval);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200906
Marcel Holtmann89e75332014-09-16 04:44:50 +0200907 urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200908
909 __fill_isoc_descriptor(urb, size,
Marcel Holtmann89e75332014-09-16 04:44:50 +0200910 le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize));
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200911
912 usb_anchor_urb(urb, &data->isoc_anchor);
913
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100914 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200915 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +0200916 if (err != -EPERM && err != -ENODEV)
917 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200918 hdev->name, urb, -err);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200919 usb_unanchor_urb(urb);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200920 }
921
922 usb_free_urb(urb);
923
924 return err;
925}
926
Marcel Holtmann9d08f502015-10-08 20:23:08 +0200927static void btusb_diag_complete(struct urb *urb)
928{
929 struct hci_dev *hdev = urb->context;
930 struct btusb_data *data = hci_get_drvdata(hdev);
931 int err;
932
933 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
934 urb->actual_length);
935
936 if (urb->status == 0) {
937 struct sk_buff *skb;
938
939 skb = bt_skb_alloc(urb->actual_length, GFP_ATOMIC);
940 if (skb) {
Johannes Berg59ae1d12017-06-16 14:29:20 +0200941 skb_put_data(skb, urb->transfer_buffer,
942 urb->actual_length);
Marcel Holtmann9d08f502015-10-08 20:23:08 +0200943 hci_recv_diag(hdev, skb);
944 }
945 } else if (urb->status == -ENOENT) {
946 /* Avoid suspend failed when usb_kill_urb */
947 return;
948 }
949
950 if (!test_bit(BTUSB_DIAG_RUNNING, &data->flags))
951 return;
952
953 usb_anchor_urb(urb, &data->diag_anchor);
954 usb_mark_last_busy(data->udev);
955
956 err = usb_submit_urb(urb, GFP_ATOMIC);
957 if (err < 0) {
958 /* -EPERM: urb is being killed;
Derek Robsond98422c2017-07-22 13:47:07 +1200959 * -ENODEV: device got disconnected
960 */
Marcel Holtmann9d08f502015-10-08 20:23:08 +0200961 if (err != -EPERM && err != -ENODEV)
962 BT_ERR("%s urb %p failed to resubmit (%d)",
963 hdev->name, urb, -err);
964 usb_unanchor_urb(urb);
965 }
966}
967
968static int btusb_submit_diag_urb(struct hci_dev *hdev, gfp_t mem_flags)
969{
970 struct btusb_data *data = hci_get_drvdata(hdev);
971 struct urb *urb;
972 unsigned char *buf;
973 unsigned int pipe;
974 int err, size = HCI_MAX_FRAME_SIZE;
975
976 BT_DBG("%s", hdev->name);
977
978 if (!data->diag_rx_ep)
979 return -ENODEV;
980
981 urb = usb_alloc_urb(0, mem_flags);
982 if (!urb)
983 return -ENOMEM;
984
985 buf = kmalloc(size, mem_flags);
986 if (!buf) {
987 usb_free_urb(urb);
988 return -ENOMEM;
989 }
990
991 pipe = usb_rcvbulkpipe(data->udev, data->diag_rx_ep->bEndpointAddress);
992
993 usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
994 btusb_diag_complete, hdev);
995
996 urb->transfer_flags |= URB_FREE_BUFFER;
997
998 usb_mark_last_busy(data->udev);
999 usb_anchor_urb(urb, &data->diag_anchor);
1000
1001 err = usb_submit_urb(urb, mem_flags);
1002 if (err < 0) {
1003 if (err != -EPERM && err != -ENODEV)
1004 BT_ERR("%s urb %p submission failed (%d)",
1005 hdev->name, urb, -err);
1006 usb_unanchor_urb(urb);
1007 }
1008
1009 usb_free_urb(urb);
1010
1011 return err;
1012}
1013
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001014static void btusb_tx_complete(struct urb *urb)
1015{
1016 struct sk_buff *skb = urb->context;
Marcel Holtmann89e75332014-09-16 04:44:50 +02001017 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
David Herrmann155961e2012-02-09 21:58:32 +01001018 struct btusb_data *data = hci_get_drvdata(hdev);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001019
Marcel Holtmann89e75332014-09-16 04:44:50 +02001020 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1021 urb->actual_length);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001022
1023 if (!test_bit(HCI_RUNNING, &hdev->flags))
1024 goto done;
1025
1026 if (!urb->status)
1027 hdev->stat.byte_tx += urb->transfer_buffer_length;
1028 else
1029 hdev->stat.err_tx++;
1030
1031done:
1032 spin_lock(&data->txlock);
1033 data->tx_in_flight--;
1034 spin_unlock(&data->txlock);
1035
1036 kfree(urb->setup_packet);
1037
1038 kfree_skb(skb);
1039}
1040
1041static void btusb_isoc_tx_complete(struct urb *urb)
1042{
1043 struct sk_buff *skb = urb->context;
Marcel Holtmann89e75332014-09-16 04:44:50 +02001044 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001045
Marcel Holtmann89e75332014-09-16 04:44:50 +02001046 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1047 urb->actual_length);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001048
1049 if (!test_bit(HCI_RUNNING, &hdev->flags))
1050 goto done;
1051
1052 if (!urb->status)
1053 hdev->stat.byte_tx += urb->transfer_buffer_length;
1054 else
1055 hdev->stat.err_tx++;
1056
1057done:
1058 kfree(urb->setup_packet);
1059
1060 kfree_skb(skb);
1061}
1062
1063static int btusb_open(struct hci_dev *hdev)
1064{
David Herrmann155961e2012-02-09 21:58:32 +01001065 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001066 int err;
1067
1068 BT_DBG("%s", hdev->name);
1069
Ethan Hsiehc7e163f2016-10-07 12:06:42 +08001070 err = usb_autopm_get_interface(data->intf);
1071 if (err < 0)
1072 return err;
1073
Kim, Ben Young Taeace31982015-02-15 23:06:14 +00001074 /* Patching USB firmware files prior to starting any URBs of HCI path
1075 * It is more safe to use USB bulk channel for downloading USB patch
1076 */
1077 if (data->setup_on_usb) {
1078 err = data->setup_on_usb(hdev);
Marcel Holtmanneb500422015-04-16 23:15:50 +02001079 if (err < 0)
Kim, Ben Young Taeace31982015-02-15 23:06:14 +00001080 return err;
1081 }
1082
Oliver Neukum7bee5492009-08-24 23:44:59 +02001083 data->intf->needs_remote_wakeup = 1;
1084
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001085 if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +02001086 goto done;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001087
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001088 err = btusb_submit_intr_urb(hdev, GFP_KERNEL);
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001089 if (err < 0)
1090 goto failed;
1091
1092 err = btusb_submit_bulk_urb(hdev, GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001093 if (err < 0) {
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001094 usb_kill_anchored_urbs(&data->intr_anchor);
1095 goto failed;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001096 }
1097
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001098 set_bit(BTUSB_BULK_RUNNING, &data->flags);
1099 btusb_submit_bulk_urb(hdev, GFP_KERNEL);
1100
Marcel Holtmann9d08f502015-10-08 20:23:08 +02001101 if (data->diag) {
1102 if (!btusb_submit_diag_urb(hdev, GFP_KERNEL))
1103 set_bit(BTUSB_DIAG_RUNNING, &data->flags);
1104 }
1105
Oliver Neukum7bee5492009-08-24 23:44:59 +02001106done:
1107 usb_autopm_put_interface(data->intf);
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001108 return 0;
1109
1110failed:
1111 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001112 usb_autopm_put_interface(data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001113 return err;
1114}
1115
Oliver Neukum7bee5492009-08-24 23:44:59 +02001116static void btusb_stop_traffic(struct btusb_data *data)
1117{
1118 usb_kill_anchored_urbs(&data->intr_anchor);
1119 usb_kill_anchored_urbs(&data->bulk_anchor);
1120 usb_kill_anchored_urbs(&data->isoc_anchor);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02001121 usb_kill_anchored_urbs(&data->diag_anchor);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001122}
1123
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001124static int btusb_close(struct hci_dev *hdev)
1125{
David Herrmann155961e2012-02-09 21:58:32 +01001126 struct btusb_data *data = hci_get_drvdata(hdev);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001127 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001128
1129 BT_DBG("%s", hdev->name);
1130
Marcel Holtmanne8c3c3d2008-09-23 00:16:36 +02001131 cancel_work_sync(&data->work);
Linus Torvalds404291a2009-11-11 13:32:29 -08001132 cancel_work_sync(&data->waker);
Marcel Holtmanne8c3c3d2008-09-23 00:16:36 +02001133
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001134 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001135 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001136 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02001137 clear_bit(BTUSB_DIAG_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001138
1139 btusb_stop_traffic(data);
Marcel Holtmann803b5832014-09-16 08:00:29 +02001140 btusb_free_frags(data);
1141
Oliver Neukum7bee5492009-08-24 23:44:59 +02001142 err = usb_autopm_get_interface(data->intf);
1143 if (err < 0)
Oliver Neukum7b8e2c12009-11-13 14:26:23 +01001144 goto failed;
Oliver Neukum7bee5492009-08-24 23:44:59 +02001145
1146 data->intf->needs_remote_wakeup = 0;
1147 usb_autopm_put_interface(data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001148
Oliver Neukum7b8e2c12009-11-13 14:26:23 +01001149failed:
1150 usb_scuttle_anchored_urbs(&data->deferred);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001151 return 0;
1152}
1153
1154static int btusb_flush(struct hci_dev *hdev)
1155{
David Herrmann155961e2012-02-09 21:58:32 +01001156 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001157
1158 BT_DBG("%s", hdev->name);
1159
1160 usb_kill_anchored_urbs(&data->tx_anchor);
Marcel Holtmann803b5832014-09-16 08:00:29 +02001161 btusb_free_frags(data);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001162
1163 return 0;
1164}
1165
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001166static struct urb *alloc_ctrl_urb(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001167{
David Herrmann155961e2012-02-09 21:58:32 +01001168 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001169 struct usb_ctrlrequest *dr;
1170 struct urb *urb;
1171 unsigned int pipe;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001172
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001173 urb = usb_alloc_urb(0, GFP_KERNEL);
1174 if (!urb)
1175 return ERR_PTR(-ENOMEM);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001176
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001177 dr = kmalloc(sizeof(*dr), GFP_KERNEL);
1178 if (!dr) {
1179 usb_free_urb(urb);
1180 return ERR_PTR(-ENOMEM);
1181 }
1182
1183 dr->bRequestType = data->cmdreq_type;
Marcel Holtmann893ba542015-01-28 20:27:34 -08001184 dr->bRequest = data->cmdreq;
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001185 dr->wIndex = 0;
1186 dr->wValue = 0;
1187 dr->wLength = __cpu_to_le16(skb->len);
1188
1189 pipe = usb_sndctrlpipe(data->udev, 0x00);
1190
Marcel Holtmann89e75332014-09-16 04:44:50 +02001191 usb_fill_control_urb(urb, data->udev, pipe, (void *)dr,
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001192 skb->data, skb->len, btusb_tx_complete, skb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001193
Marcel Holtmann89e75332014-09-16 04:44:50 +02001194 skb->dev = (void *)hdev;
Marcel Holtmann7bd8f092013-10-11 06:19:18 -07001195
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001196 return urb;
1197}
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001198
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001199static struct urb *alloc_bulk_urb(struct hci_dev *hdev, struct sk_buff *skb)
1200{
1201 struct btusb_data *data = hci_get_drvdata(hdev);
1202 struct urb *urb;
1203 unsigned int pipe;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001204
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001205 if (!data->bulk_tx_ep)
1206 return ERR_PTR(-ENODEV);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001207
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001208 urb = usb_alloc_urb(0, GFP_KERNEL);
1209 if (!urb)
1210 return ERR_PTR(-ENOMEM);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001211
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001212 pipe = usb_sndbulkpipe(data->udev, data->bulk_tx_ep->bEndpointAddress);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001213
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001214 usb_fill_bulk_urb(urb, data->udev, pipe,
1215 skb->data, skb->len, btusb_tx_complete, skb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001216
Marcel Holtmann89e75332014-09-16 04:44:50 +02001217 skb->dev = (void *)hdev;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001218
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001219 return urb;
1220}
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001221
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001222static struct urb *alloc_isoc_urb(struct hci_dev *hdev, struct sk_buff *skb)
1223{
1224 struct btusb_data *data = hci_get_drvdata(hdev);
1225 struct urb *urb;
1226 unsigned int pipe;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001227
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001228 if (!data->isoc_tx_ep)
1229 return ERR_PTR(-ENODEV);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001230
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001231 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_KERNEL);
1232 if (!urb)
1233 return ERR_PTR(-ENOMEM);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001234
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001235 pipe = usb_sndisocpipe(data->udev, data->isoc_tx_ep->bEndpointAddress);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001236
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001237 usb_fill_int_urb(urb, data->udev, pipe,
1238 skb->data, skb->len, btusb_isoc_tx_complete,
1239 skb, data->isoc_tx_ep->bInterval);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001240
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001241 urb->transfer_flags = URB_ISO_ASAP;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001242
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001243 __fill_isoc_descriptor(urb, skb->len,
1244 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001245
Marcel Holtmann89e75332014-09-16 04:44:50 +02001246 skb->dev = (void *)hdev;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001247
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001248 return urb;
1249}
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001250
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001251static int submit_tx_urb(struct hci_dev *hdev, struct urb *urb)
1252{
1253 struct btusb_data *data = hci_get_drvdata(hdev);
1254 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001255
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001256 usb_anchor_urb(urb, &data->tx_anchor);
1257
Johan Hedberge9753ef2014-09-14 08:49:34 +03001258 err = usb_submit_urb(urb, GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001259 if (err < 0) {
Paul Bolle5a9b80e2011-10-09 12:12:16 +02001260 if (err != -EPERM && err != -ENODEV)
1261 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +02001262 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001263 kfree(urb->setup_packet);
1264 usb_unanchor_urb(urb);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001265 } else {
1266 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001267 }
1268
Cong Wang54a8a792011-11-22 09:32:57 +08001269 usb_free_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001270 return err;
1271}
1272
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001273static int submit_or_queue_tx_urb(struct hci_dev *hdev, struct urb *urb)
1274{
1275 struct btusb_data *data = hci_get_drvdata(hdev);
1276 unsigned long flags;
1277 bool suspending;
1278
1279 spin_lock_irqsave(&data->txlock, flags);
1280 suspending = test_bit(BTUSB_SUSPENDING, &data->flags);
1281 if (!suspending)
1282 data->tx_in_flight++;
1283 spin_unlock_irqrestore(&data->txlock, flags);
1284
1285 if (!suspending)
1286 return submit_tx_urb(hdev, urb);
1287
1288 usb_anchor_urb(urb, &data->deferred);
1289 schedule_work(&data->waker);
1290
1291 usb_free_urb(urb);
1292 return 0;
1293}
1294
1295static int btusb_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
1296{
1297 struct urb *urb;
1298
1299 BT_DBG("%s", hdev->name);
1300
Marcel Holtmann618e8bc2015-11-05 07:33:56 +01001301 switch (hci_skb_pkt_type(skb)) {
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001302 case HCI_COMMAND_PKT:
1303 urb = alloc_ctrl_urb(hdev, skb);
1304 if (IS_ERR(urb))
1305 return PTR_ERR(urb);
1306
1307 hdev->stat.cmd_tx++;
1308 return submit_or_queue_tx_urb(hdev, urb);
1309
1310 case HCI_ACLDATA_PKT:
1311 urb = alloc_bulk_urb(hdev, skb);
1312 if (IS_ERR(urb))
1313 return PTR_ERR(urb);
1314
1315 hdev->stat.acl_tx++;
1316 return submit_or_queue_tx_urb(hdev, urb);
1317
1318 case HCI_SCODATA_PKT:
1319 if (hci_conn_num(hdev, SCO_LINK) < 1)
1320 return -ENODEV;
1321
1322 urb = alloc_isoc_urb(hdev, skb);
1323 if (IS_ERR(urb))
1324 return PTR_ERR(urb);
1325
1326 hdev->stat.sco_tx++;
1327 return submit_tx_urb(hdev, urb);
1328 }
1329
1330 return -EILSEQ;
1331}
1332
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001333static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
1334{
David Herrmann155961e2012-02-09 21:58:32 +01001335 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001336
1337 BT_DBG("%s evt %d", hdev->name, evt);
1338
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001339 if (hci_conn_num(hdev, SCO_LINK) != data->sco_num) {
1340 data->sco_num = hci_conn_num(hdev, SCO_LINK);
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001341 schedule_work(&data->work);
Marcel Holtmanna780efa2008-11-30 12:17:12 +01001342 }
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001343}
1344
Jesper Juhl42b16b32011-01-17 00:09:38 +01001345static inline int __set_isoc_interface(struct hci_dev *hdev, int altsetting)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001346{
David Herrmann155961e2012-02-09 21:58:32 +01001347 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001348 struct usb_interface *intf = data->isoc;
1349 struct usb_endpoint_descriptor *ep_desc;
1350 int i, err;
1351
1352 if (!data->isoc)
1353 return -ENODEV;
1354
1355 err = usb_set_interface(data->udev, 1, altsetting);
1356 if (err < 0) {
1357 BT_ERR("%s setting interface failed (%d)", hdev->name, -err);
1358 return err;
1359 }
1360
1361 data->isoc_altsetting = altsetting;
1362
1363 data->isoc_tx_ep = NULL;
1364 data->isoc_rx_ep = NULL;
1365
1366 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
1367 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
1368
1369 if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) {
1370 data->isoc_tx_ep = ep_desc;
1371 continue;
1372 }
1373
1374 if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) {
1375 data->isoc_rx_ep = ep_desc;
1376 continue;
1377 }
1378 }
1379
1380 if (!data->isoc_tx_ep || !data->isoc_rx_ep) {
1381 BT_ERR("%s invalid SCO descriptors", hdev->name);
1382 return -ENODEV;
1383 }
1384
1385 return 0;
1386}
1387
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001388static void btusb_work(struct work_struct *work)
1389{
1390 struct btusb_data *data = container_of(work, struct btusb_data, work);
1391 struct hci_dev *hdev = data->hdev;
Mikel Astizf4001d22012-04-11 08:48:51 +02001392 int new_alts;
Oliver Neukum7bee5492009-08-24 23:44:59 +02001393 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001394
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001395 if (data->sco_num > 0) {
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -03001396 if (!test_bit(BTUSB_DID_ISO_RESUME, &data->flags)) {
Oliver Neukum8efdd0c2011-02-11 13:00:06 +01001397 err = usb_autopm_get_interface(data->isoc ? data->isoc : data->intf);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001398 if (err < 0) {
1399 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1400 usb_kill_anchored_urbs(&data->isoc_anchor);
1401 return;
1402 }
1403
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -03001404 set_bit(BTUSB_DID_ISO_RESUME, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001405 }
Mikel Astizf4001d22012-04-11 08:48:51 +02001406
1407 if (hdev->voice_setting & 0x0020) {
1408 static const int alts[3] = { 2, 4, 5 };
Marcel Holtmann89e75332014-09-16 04:44:50 +02001409
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001410 new_alts = alts[data->sco_num - 1];
Mikel Astizf4001d22012-04-11 08:48:51 +02001411 } else {
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001412 new_alts = data->sco_num;
Mikel Astizf4001d22012-04-11 08:48:51 +02001413 }
1414
1415 if (data->isoc_altsetting != new_alts) {
Kuba Pawlakf6fc86f2015-10-28 15:18:05 +01001416 unsigned long flags;
1417
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001418 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1419 usb_kill_anchored_urbs(&data->isoc_anchor);
1420
Kuba Pawlak8f9d02f2015-09-10 17:07:00 +01001421 /* When isochronous alternate setting needs to be
1422 * changed, because SCO connection has been added
1423 * or removed, a packet fragment may be left in the
1424 * reassembling state. This could lead to wrongly
1425 * assembled fragments.
1426 *
1427 * Clear outstanding fragment when selecting a new
1428 * alternate setting.
1429 */
Kuba Pawlakf6fc86f2015-10-28 15:18:05 +01001430 spin_lock_irqsave(&data->rxlock, flags);
Kuba Pawlak8f9d02f2015-09-10 17:07:00 +01001431 kfree_skb(data->sco_skb);
1432 data->sco_skb = NULL;
Kuba Pawlakf6fc86f2015-10-28 15:18:05 +01001433 spin_unlock_irqrestore(&data->rxlock, flags);
Kuba Pawlak8f9d02f2015-09-10 17:07:00 +01001434
Mikel Astizf4001d22012-04-11 08:48:51 +02001435 if (__set_isoc_interface(hdev, new_alts) < 0)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001436 return;
1437 }
1438
1439 if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001440 if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001441 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1442 else
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001443 btusb_submit_isoc_urb(hdev, GFP_KERNEL);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001444 }
1445 } else {
1446 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1447 usb_kill_anchored_urbs(&data->isoc_anchor);
1448
1449 __set_isoc_interface(hdev, 0);
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -03001450 if (test_and_clear_bit(BTUSB_DID_ISO_RESUME, &data->flags))
Oliver Neukum8efdd0c2011-02-11 13:00:06 +01001451 usb_autopm_put_interface(data->isoc ? data->isoc : data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001452 }
1453}
1454
Oliver Neukum7bee5492009-08-24 23:44:59 +02001455static void btusb_waker(struct work_struct *work)
1456{
1457 struct btusb_data *data = container_of(work, struct btusb_data, waker);
1458 int err;
1459
1460 err = usb_autopm_get_interface(data->intf);
1461 if (err < 0)
1462 return;
1463
1464 usb_autopm_put_interface(data->intf);
1465}
1466
Marcel Holtmann9f8f9622013-04-10 08:11:35 -07001467static int btusb_setup_bcm92035(struct hci_dev *hdev)
1468{
1469 struct sk_buff *skb;
1470 u8 val = 0x00;
1471
1472 BT_DBG("%s", hdev->name);
1473
1474 skb = __hci_cmd_sync(hdev, 0xfc3b, 1, &val, HCI_INIT_TIMEOUT);
1475 if (IS_ERR(skb))
1476 BT_ERR("BCM92035 command failed (%ld)", -PTR_ERR(skb));
1477 else
1478 kfree_skb(skb);
1479
1480 return 0;
1481}
1482
Marcel Holtmann81cac642014-01-03 03:02:36 -08001483static int btusb_setup_csr(struct hci_dev *hdev)
1484{
1485 struct hci_rp_read_local_version *rp;
1486 struct sk_buff *skb;
Marcel Holtmann81cac642014-01-03 03:02:36 -08001487
1488 BT_DBG("%s", hdev->name);
1489
Marcel Holtmann7cd84d72015-06-07 10:01:02 +02001490 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
1491 HCI_INIT_TIMEOUT);
1492 if (IS_ERR(skb)) {
1493 int err = PTR_ERR(skb);
1494 BT_ERR("%s: CSR: Local version failed (%d)", hdev->name, err);
1495 return err;
1496 }
1497
1498 if (skb->len != sizeof(struct hci_rp_read_local_version)) {
1499 BT_ERR("%s: CSR: Local version length mismatch", hdev->name);
1500 kfree_skb(skb);
1501 return -EIO;
1502 }
Marcel Holtmann81cac642014-01-03 03:02:36 -08001503
Marcel Holtmann89e75332014-09-16 04:44:50 +02001504 rp = (struct hci_rp_read_local_version *)skb->data;
Marcel Holtmann81cac642014-01-03 03:02:36 -08001505
Johan Hedberg6cafcd92015-08-30 21:47:21 +03001506 /* Detect controllers which aren't real CSR ones. */
1507 if (le16_to_cpu(rp->manufacturer) != 10 ||
1508 le16_to_cpu(rp->lmp_subver) == 0x0c5c) {
Marcel Holtmann9641d342015-06-07 10:01:01 +02001509 /* Clear the reset quirk since this is not an actual
1510 * early Bluetooth 1.1 device from CSR.
1511 */
1512 clear_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmann81cac642014-01-03 03:02:36 -08001513
Marcel Holtmann9641d342015-06-07 10:01:01 +02001514 /* These fake CSR controllers have all a broken
1515 * stored link key handling and so just disable it.
1516 */
1517 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
Marcel Holtmann81cac642014-01-03 03:02:36 -08001518 }
1519
Marcel Holtmann81cac642014-01-03 03:02:36 -08001520 kfree_skb(skb);
1521
Marcel Holtmann9641d342015-06-07 10:01:01 +02001522 return 0;
Marcel Holtmann81cac642014-01-03 03:02:36 -08001523}
1524
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001525static const struct firmware *btusb_setup_intel_get_fw(struct hci_dev *hdev,
Marcel Holtmann89e75332014-09-16 04:44:50 +02001526 struct intel_version *ver)
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001527{
1528 const struct firmware *fw;
1529 char fwname[64];
1530 int ret;
1531
1532 snprintf(fwname, sizeof(fwname),
1533 "intel/ibt-hw-%x.%x.%x-fw-%x.%x.%x.%x.%x.bseq",
1534 ver->hw_platform, ver->hw_variant, ver->hw_revision,
1535 ver->fw_variant, ver->fw_revision, ver->fw_build_num,
1536 ver->fw_build_ww, ver->fw_build_yy);
1537
1538 ret = request_firmware(&fw, fwname, &hdev->dev);
1539 if (ret < 0) {
1540 if (ret == -EINVAL) {
1541 BT_ERR("%s Intel firmware file request failed (%d)",
1542 hdev->name, ret);
1543 return NULL;
1544 }
1545
1546 BT_ERR("%s failed to open Intel firmware file: %s(%d)",
1547 hdev->name, fwname, ret);
1548
1549 /* If the correct firmware patch file is not found, use the
1550 * default firmware patch file instead
1551 */
1552 snprintf(fwname, sizeof(fwname), "intel/ibt-hw-%x.%x.bseq",
1553 ver->hw_platform, ver->hw_variant);
1554 if (request_firmware(&fw, fwname, &hdev->dev) < 0) {
1555 BT_ERR("%s failed to open default Intel fw file: %s",
1556 hdev->name, fwname);
1557 return NULL;
1558 }
1559 }
1560
1561 BT_INFO("%s: Intel Bluetooth firmware file: %s", hdev->name, fwname);
1562
1563 return fw;
1564}
1565
1566static int btusb_setup_intel_patching(struct hci_dev *hdev,
1567 const struct firmware *fw,
1568 const u8 **fw_ptr, int *disable_patch)
1569{
1570 struct sk_buff *skb;
1571 struct hci_command_hdr *cmd;
1572 const u8 *cmd_param;
1573 struct hci_event_hdr *evt = NULL;
1574 const u8 *evt_param = NULL;
1575 int remain = fw->size - (*fw_ptr - fw->data);
1576
1577 /* The first byte indicates the types of the patch command or event.
1578 * 0x01 means HCI command and 0x02 is HCI event. If the first bytes
1579 * in the current firmware buffer doesn't start with 0x01 or
1580 * the size of remain buffer is smaller than HCI command header,
1581 * the firmware file is corrupted and it should stop the patching
1582 * process.
1583 */
1584 if (remain > HCI_COMMAND_HDR_SIZE && *fw_ptr[0] != 0x01) {
1585 BT_ERR("%s Intel fw corrupted: invalid cmd read", hdev->name);
1586 return -EINVAL;
1587 }
1588 (*fw_ptr)++;
1589 remain--;
1590
1591 cmd = (struct hci_command_hdr *)(*fw_ptr);
1592 *fw_ptr += sizeof(*cmd);
1593 remain -= sizeof(*cmd);
1594
1595 /* Ensure that the remain firmware data is long enough than the length
1596 * of command parameter. If not, the firmware file is corrupted.
1597 */
1598 if (remain < cmd->plen) {
1599 BT_ERR("%s Intel fw corrupted: invalid cmd len", hdev->name);
1600 return -EFAULT;
1601 }
1602
1603 /* If there is a command that loads a patch in the firmware
1604 * file, then enable the patch upon success, otherwise just
1605 * disable the manufacturer mode, for example patch activation
1606 * is not required when the default firmware patch file is used
1607 * because there are no patch data to load.
1608 */
1609 if (*disable_patch && le16_to_cpu(cmd->opcode) == 0xfc8e)
1610 *disable_patch = 0;
1611
1612 cmd_param = *fw_ptr;
1613 *fw_ptr += cmd->plen;
1614 remain -= cmd->plen;
1615
1616 /* This reads the expected events when the above command is sent to the
1617 * device. Some vendor commands expects more than one events, for
1618 * example command status event followed by vendor specific event.
1619 * For this case, it only keeps the last expected event. so the command
1620 * can be sent with __hci_cmd_sync_ev() which returns the sk_buff of
1621 * last expected event.
1622 */
1623 while (remain > HCI_EVENT_HDR_SIZE && *fw_ptr[0] == 0x02) {
1624 (*fw_ptr)++;
1625 remain--;
1626
1627 evt = (struct hci_event_hdr *)(*fw_ptr);
1628 *fw_ptr += sizeof(*evt);
1629 remain -= sizeof(*evt);
1630
1631 if (remain < evt->plen) {
1632 BT_ERR("%s Intel fw corrupted: invalid evt len",
1633 hdev->name);
1634 return -EFAULT;
1635 }
1636
1637 evt_param = *fw_ptr;
1638 *fw_ptr += evt->plen;
1639 remain -= evt->plen;
1640 }
1641
1642 /* Every HCI commands in the firmware file has its correspond event.
1643 * If event is not found or remain is smaller than zero, the firmware
1644 * file is corrupted.
1645 */
1646 if (!evt || !evt_param || remain < 0) {
1647 BT_ERR("%s Intel fw corrupted: invalid evt read", hdev->name);
1648 return -EFAULT;
1649 }
1650
1651 skb = __hci_cmd_sync_ev(hdev, le16_to_cpu(cmd->opcode), cmd->plen,
1652 cmd_param, evt->evt, HCI_INIT_TIMEOUT);
1653 if (IS_ERR(skb)) {
1654 BT_ERR("%s sending Intel patch command (0x%4.4x) failed (%ld)",
1655 hdev->name, cmd->opcode, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001656 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001657 }
1658
1659 /* It ensures that the returned event matches the event data read from
1660 * the firmware file. At fist, it checks the length and then
1661 * the contents of the event.
1662 */
1663 if (skb->len != evt->plen) {
1664 BT_ERR("%s mismatch event length (opcode 0x%4.4x)", hdev->name,
1665 le16_to_cpu(cmd->opcode));
1666 kfree_skb(skb);
1667 return -EFAULT;
1668 }
1669
1670 if (memcmp(skb->data, evt_param, evt->plen)) {
1671 BT_ERR("%s mismatch event parameter (opcode 0x%4.4x)",
1672 hdev->name, le16_to_cpu(cmd->opcode));
1673 kfree_skb(skb);
1674 return -EFAULT;
1675 }
1676 kfree_skb(skb);
1677
1678 return 0;
1679}
1680
1681static int btusb_setup_intel(struct hci_dev *hdev)
1682{
1683 struct sk_buff *skb;
1684 const struct firmware *fw;
1685 const u8 *fw_ptr;
Loic Poulain28dc4b92015-12-03 16:10:22 +01001686 int disable_patch, err;
Loic Poulain6c483de2015-12-06 16:18:34 +01001687 struct intel_version ver;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001688
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001689 BT_DBG("%s", hdev->name);
1690
1691 /* The controller has a bug with the first HCI command sent to it
1692 * returning number of completed commands as zero. This would stall the
1693 * command processing in the Bluetooth core.
1694 *
1695 * As a workaround, send HCI Reset command first which will reset the
1696 * number of completed commands and allow normal command processing
1697 * from now on.
1698 */
1699 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
1700 if (IS_ERR(skb)) {
1701 BT_ERR("%s sending initial HCI reset command failed (%ld)",
1702 hdev->name, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001703 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001704 }
1705 kfree_skb(skb);
1706
1707 /* Read Intel specific controller version first to allow selection of
1708 * which firmware file to load.
1709 *
1710 * The returned information are hardware variant and revision plus
1711 * firmware variant, revision and build number.
1712 */
Loic Poulain6c483de2015-12-06 16:18:34 +01001713 err = btintel_read_version(hdev, &ver);
1714 if (err)
1715 return err;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001716
1717 BT_INFO("%s: read Intel version: %02x%02x%02x%02x%02x%02x%02x%02x%02x",
Loic Poulain6c483de2015-12-06 16:18:34 +01001718 hdev->name, ver.hw_platform, ver.hw_variant, ver.hw_revision,
1719 ver.fw_variant, ver.fw_revision, ver.fw_build_num,
1720 ver.fw_build_ww, ver.fw_build_yy, ver.fw_patch_num);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001721
1722 /* fw_patch_num indicates the version of patch the device currently
1723 * have. If there is no patch data in the device, it is always 0x00.
Minjune Kim5075eda2015-08-27 13:21:52 +09001724 * So, if it is other than 0x00, no need to patch the device again.
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001725 */
Loic Poulain6c483de2015-12-06 16:18:34 +01001726 if (ver.fw_patch_num) {
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001727 BT_INFO("%s: Intel device is already patched. patch num: %02x",
Loic Poulain6c483de2015-12-06 16:18:34 +01001728 hdev->name, ver.fw_patch_num);
Marcel Holtmann213445b2015-10-21 02:45:19 +02001729 goto complete;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001730 }
1731
1732 /* Opens the firmware patch file based on the firmware version read
1733 * from the controller. If it fails to open the matching firmware
1734 * patch file, it tries to open the default firmware patch file.
1735 * If no patch file is found, allow the device to operate without
1736 * a patch.
1737 */
Loic Poulain6c483de2015-12-06 16:18:34 +01001738 fw = btusb_setup_intel_get_fw(hdev, &ver);
1739 if (!fw)
Marcel Holtmann213445b2015-10-21 02:45:19 +02001740 goto complete;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001741 fw_ptr = fw->data;
1742
Loic Poulain28dc4b92015-12-03 16:10:22 +01001743 /* Enable the manufacturer mode of the controller.
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001744 * Only while this mode is enabled, the driver can download the
1745 * firmware patch data and configuration parameters.
1746 */
Loic Poulain28dc4b92015-12-03 16:10:22 +01001747 err = btintel_enter_mfg(hdev);
1748 if (err) {
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001749 release_firmware(fw);
Loic Poulain28dc4b92015-12-03 16:10:22 +01001750 return err;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001751 }
1752
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001753 disable_patch = 1;
1754
1755 /* The firmware data file consists of list of Intel specific HCI
1756 * commands and its expected events. The first byte indicates the
1757 * type of the message, either HCI command or HCI event.
1758 *
1759 * It reads the command and its expected event from the firmware file,
1760 * and send to the controller. Once __hci_cmd_sync_ev() returns,
1761 * the returned event is compared with the event read from the firmware
1762 * file and it will continue until all the messages are downloaded to
1763 * the controller.
1764 *
1765 * Once the firmware patching is completed successfully,
1766 * the manufacturer mode is disabled with reset and activating the
1767 * downloaded patch.
1768 *
1769 * If the firmware patching fails, the manufacturer mode is
1770 * disabled with reset and deactivating the patch.
1771 *
1772 * If the default patch file is used, no reset is done when disabling
1773 * the manufacturer.
1774 */
1775 while (fw->size > fw_ptr - fw->data) {
1776 int ret;
1777
1778 ret = btusb_setup_intel_patching(hdev, fw, &fw_ptr,
1779 &disable_patch);
1780 if (ret < 0)
1781 goto exit_mfg_deactivate;
1782 }
1783
1784 release_firmware(fw);
1785
1786 if (disable_patch)
1787 goto exit_mfg_disable;
1788
1789 /* Patching completed successfully and disable the manufacturer mode
1790 * with reset and activate the downloaded firmware patches.
1791 */
Loic Poulain28dc4b92015-12-03 16:10:22 +01001792 err = btintel_exit_mfg(hdev, true, true);
1793 if (err)
1794 return err;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001795
1796 BT_INFO("%s: Intel Bluetooth firmware patch completed and activated",
1797 hdev->name);
1798
Marcel Holtmann213445b2015-10-21 02:45:19 +02001799 goto complete;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001800
1801exit_mfg_disable:
1802 /* Disable the manufacturer mode without reset */
Loic Poulain28dc4b92015-12-03 16:10:22 +01001803 err = btintel_exit_mfg(hdev, false, false);
1804 if (err)
1805 return err;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001806
1807 BT_INFO("%s: Intel Bluetooth firmware patch completed", hdev->name);
Marcel Holtmann40cb0982014-07-02 12:06:45 +02001808
Marcel Holtmann213445b2015-10-21 02:45:19 +02001809 goto complete;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001810
1811exit_mfg_deactivate:
1812 release_firmware(fw);
1813
1814 /* Patching failed. Disable the manufacturer mode with reset and
1815 * deactivate the downloaded firmware patches.
1816 */
Loic Poulain28dc4b92015-12-03 16:10:22 +01001817 err = btintel_exit_mfg(hdev, true, false);
1818 if (err)
1819 return err;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001820
1821 BT_INFO("%s: Intel Bluetooth firmware patch completed and deactivated",
1822 hdev->name);
1823
Marcel Holtmann213445b2015-10-21 02:45:19 +02001824complete:
1825 /* Set the event mask for Intel specific vendor events. This enables
1826 * a few extra events that are useful during general operation.
1827 */
1828 btintel_set_event_mask_mfg(hdev, false);
1829
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07001830 btintel_check_bdaddr(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001831 return 0;
1832}
1833
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001834static int inject_cmd_complete(struct hci_dev *hdev, __u16 opcode)
1835{
1836 struct sk_buff *skb;
1837 struct hci_event_hdr *hdr;
1838 struct hci_ev_cmd_complete *evt;
1839
1840 skb = bt_skb_alloc(sizeof(*hdr) + sizeof(*evt) + 1, GFP_ATOMIC);
1841 if (!skb)
1842 return -ENOMEM;
1843
Johannes Berg4df864c2017-06-16 14:29:21 +02001844 hdr = skb_put(skb, sizeof(*hdr));
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001845 hdr->evt = HCI_EV_CMD_COMPLETE;
1846 hdr->plen = sizeof(*evt) + 1;
1847
Johannes Berg4df864c2017-06-16 14:29:21 +02001848 evt = skb_put(skb, sizeof(*evt));
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001849 evt->ncmd = 0x01;
1850 evt->opcode = cpu_to_le16(opcode);
1851
Johannes Berg634fef62017-06-16 14:29:24 +02001852 skb_put_u8(skb, 0x00);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001853
Marcel Holtmann618e8bc2015-11-05 07:33:56 +01001854 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001855
1856 return hci_recv_frame(hdev, skb);
1857}
1858
1859static int btusb_recv_bulk_intel(struct btusb_data *data, void *buffer,
1860 int count)
1861{
1862 /* When the device is in bootloader mode, then it can send
1863 * events via the bulk endpoint. These events are treated the
1864 * same way as the ones received from the interrupt endpoint.
1865 */
1866 if (test_bit(BTUSB_BOOTLOADER, &data->flags))
1867 return btusb_recv_intr(data, buffer, count);
1868
1869 return btusb_recv_bulk(data, buffer, count);
1870}
1871
Marcel Holtmannccd6da22015-04-09 00:35:19 -07001872static void btusb_intel_bootup(struct btusb_data *data, const void *ptr,
1873 unsigned int len)
1874{
1875 const struct intel_bootup *evt = ptr;
1876
1877 if (len != sizeof(*evt))
1878 return;
1879
1880 if (test_and_clear_bit(BTUSB_BOOTING, &data->flags)) {
1881 smp_mb__after_atomic();
1882 wake_up_bit(&data->flags, BTUSB_BOOTING);
1883 }
1884}
1885
1886static void btusb_intel_secure_send_result(struct btusb_data *data,
1887 const void *ptr, unsigned int len)
1888{
1889 const struct intel_secure_send_result *evt = ptr;
1890
1891 if (len != sizeof(*evt))
1892 return;
1893
1894 if (evt->result)
1895 set_bit(BTUSB_FIRMWARE_FAILED, &data->flags);
1896
1897 if (test_and_clear_bit(BTUSB_DOWNLOADING, &data->flags) &&
1898 test_bit(BTUSB_FIRMWARE_LOADED, &data->flags)) {
1899 smp_mb__after_atomic();
1900 wake_up_bit(&data->flags, BTUSB_DOWNLOADING);
1901 }
1902}
1903
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001904static int btusb_recv_event_intel(struct hci_dev *hdev, struct sk_buff *skb)
1905{
1906 struct btusb_data *data = hci_get_drvdata(hdev);
1907
1908 if (test_bit(BTUSB_BOOTLOADER, &data->flags)) {
1909 struct hci_event_hdr *hdr = (void *)skb->data;
1910
Marcel Holtmannccd6da22015-04-09 00:35:19 -07001911 if (skb->len > HCI_EVENT_HDR_SIZE && hdr->evt == 0xff &&
1912 hdr->plen > 0) {
1913 const void *ptr = skb->data + HCI_EVENT_HDR_SIZE + 1;
1914 unsigned int len = skb->len - HCI_EVENT_HDR_SIZE - 1;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001915
Marcel Holtmannccd6da22015-04-09 00:35:19 -07001916 switch (skb->data[2]) {
1917 case 0x02:
1918 /* When switching to the operational firmware
1919 * the device sends a vendor specific event
1920 * indicating that the bootup completed.
1921 */
1922 btusb_intel_bootup(data, ptr, len);
1923 break;
1924 case 0x06:
1925 /* When the firmware loading completes the
1926 * device sends out a vendor specific event
1927 * indicating the result of the firmware
1928 * loading.
1929 */
1930 btusb_intel_secure_send_result(data, ptr, len);
1931 break;
Johan Hedbergfad70972015-01-30 10:58:55 +02001932 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001933 }
1934 }
1935
1936 return hci_recv_frame(hdev, skb);
1937}
1938
1939static int btusb_send_frame_intel(struct hci_dev *hdev, struct sk_buff *skb)
1940{
1941 struct btusb_data *data = hci_get_drvdata(hdev);
1942 struct urb *urb;
1943
1944 BT_DBG("%s", hdev->name);
1945
Marcel Holtmann618e8bc2015-11-05 07:33:56 +01001946 switch (hci_skb_pkt_type(skb)) {
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001947 case HCI_COMMAND_PKT:
1948 if (test_bit(BTUSB_BOOTLOADER, &data->flags)) {
1949 struct hci_command_hdr *cmd = (void *)skb->data;
1950 __u16 opcode = le16_to_cpu(cmd->opcode);
1951
1952 /* When in bootloader mode and the command 0xfc09
1953 * is received, it needs to be send down the
1954 * bulk endpoint. So allocate a bulk URB instead.
1955 */
1956 if (opcode == 0xfc09)
1957 urb = alloc_bulk_urb(hdev, skb);
1958 else
1959 urb = alloc_ctrl_urb(hdev, skb);
1960
1961 /* When the 0xfc01 command is issued to boot into
1962 * the operational firmware, it will actually not
1963 * send a command complete event. To keep the flow
1964 * control working inject that event here.
1965 */
1966 if (opcode == 0xfc01)
1967 inject_cmd_complete(hdev, opcode);
1968 } else {
1969 urb = alloc_ctrl_urb(hdev, skb);
1970 }
1971 if (IS_ERR(urb))
1972 return PTR_ERR(urb);
1973
1974 hdev->stat.cmd_tx++;
1975 return submit_or_queue_tx_urb(hdev, urb);
1976
1977 case HCI_ACLDATA_PKT:
1978 urb = alloc_bulk_urb(hdev, skb);
1979 if (IS_ERR(urb))
1980 return PTR_ERR(urb);
1981
1982 hdev->stat.acl_tx++;
1983 return submit_or_queue_tx_urb(hdev, urb);
1984
1985 case HCI_SCODATA_PKT:
1986 if (hci_conn_num(hdev, SCO_LINK) < 1)
1987 return -ENODEV;
1988
1989 urb = alloc_isoc_urb(hdev, skb);
1990 if (IS_ERR(urb))
1991 return PTR_ERR(urb);
1992
1993 hdev->stat.sco_tx++;
1994 return submit_tx_urb(hdev, urb);
1995 }
1996
1997 return -EILSEQ;
1998}
1999
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002000static int btusb_setup_intel_new(struct hci_dev *hdev)
2001{
2002 static const u8 reset_param[] = { 0x00, 0x01, 0x00, 0x01,
2003 0x00, 0x08, 0x04, 0x00 };
2004 struct btusb_data *data = hci_get_drvdata(hdev);
2005 struct sk_buff *skb;
Loic Poulain6c483de2015-12-06 16:18:34 +01002006 struct intel_version ver;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002007 struct intel_boot_params *params;
2008 const struct firmware *fw;
2009 const u8 *fw_ptr;
Marcel Holtmanne66890a92015-06-07 09:47:08 +02002010 u32 frag_len;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002011 char fwname[64];
2012 ktime_t calltime, delta, rettime;
2013 unsigned long long duration;
2014 int err;
2015
2016 BT_DBG("%s", hdev->name);
2017
2018 calltime = ktime_get();
2019
2020 /* Read the Intel version information to determine if the device
2021 * is in bootloader mode or if it already has operational firmware
2022 * loaded.
2023 */
Loic Poulain6c483de2015-12-06 16:18:34 +01002024 err = btintel_read_version(hdev, &ver);
2025 if (err)
2026 return err;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002027
2028 /* The hardware platform number has a fixed value of 0x37 and
2029 * for now only accept this single value.
2030 */
Loic Poulain6c483de2015-12-06 16:18:34 +01002031 if (ver.hw_platform != 0x37) {
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002032 BT_ERR("%s: Unsupported Intel hardware platform (%u)",
Loic Poulain6c483de2015-12-06 16:18:34 +01002033 hdev->name, ver.hw_platform);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002034 return -EINVAL;
2035 }
2036
Tedd Ho-Jeong An92688342017-03-06 15:38:26 -08002037 /* Check for supported iBT hardware variants of this firmware
2038 * loading method.
Tedd Ho-Jeong Ana0af53b2016-05-06 11:53:46 -07002039 *
2040 * This check has been put in place to ensure correct forward
2041 * compatibility options when newer hardware variants come along.
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002042 */
Tedd Ho-Jeong An92688342017-03-06 15:38:26 -08002043 switch (ver.hw_variant) {
2044 case 0x0b: /* SfP */
2045 case 0x0c: /* WsP */
Tedd Ho-Jeong An86a61292017-05-01 13:35:12 -07002046 case 0x11: /* JfP */
Marcel Holtmannde766142017-03-06 15:38:28 -08002047 case 0x12: /* ThP */
Tedd Ho-Jeong An92688342017-03-06 15:38:26 -08002048 break;
2049 default:
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002050 BT_ERR("%s: Unsupported Intel hardware variant (%u)",
Loic Poulain6c483de2015-12-06 16:18:34 +01002051 hdev->name, ver.hw_variant);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002052 return -EINVAL;
2053 }
2054
Loic Poulain6c483de2015-12-06 16:18:34 +01002055 btintel_version_info(hdev, &ver);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002056
2057 /* The firmware variant determines if the device is in bootloader
2058 * mode or is running operational firmware. The value 0x06 identifies
2059 * the bootloader and the value 0x23 identifies the operational
2060 * firmware.
2061 *
2062 * When the operational firmware is already present, then only
2063 * the check for valid Bluetooth device address is needed. This
2064 * determines if the device will be added as configured or
2065 * unconfigured controller.
2066 *
2067 * It is not possible to use the Secure Boot Parameters in this
2068 * case since that command is only available in bootloader mode.
2069 */
Loic Poulain6c483de2015-12-06 16:18:34 +01002070 if (ver.fw_variant == 0x23) {
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002071 clear_bit(BTUSB_BOOTLOADER, &data->flags);
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07002072 btintel_check_bdaddr(hdev);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002073 return 0;
2074 }
2075
2076 /* If the device is not in bootloader mode, then the only possible
2077 * choice is to return an error and abort the device initialization.
2078 */
Loic Poulain6c483de2015-12-06 16:18:34 +01002079 if (ver.fw_variant != 0x06) {
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002080 BT_ERR("%s: Unsupported Intel firmware variant (%u)",
Loic Poulain6c483de2015-12-06 16:18:34 +01002081 hdev->name, ver.fw_variant);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002082 return -ENODEV;
2083 }
2084
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002085 /* Read the secure boot parameters to identify the operating
2086 * details of the bootloader.
2087 */
2088 skb = __hci_cmd_sync(hdev, 0xfc0d, 0, NULL, HCI_INIT_TIMEOUT);
2089 if (IS_ERR(skb)) {
2090 BT_ERR("%s: Reading Intel boot parameters failed (%ld)",
2091 hdev->name, PTR_ERR(skb));
2092 return PTR_ERR(skb);
2093 }
2094
2095 if (skb->len != sizeof(*params)) {
2096 BT_ERR("%s: Intel boot parameters size mismatch", hdev->name);
2097 kfree_skb(skb);
2098 return -EILSEQ;
2099 }
2100
2101 params = (struct intel_boot_params *)skb->data;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002102
2103 BT_INFO("%s: Device revision is %u", hdev->name,
2104 le16_to_cpu(params->dev_revid));
2105
2106 BT_INFO("%s: Secure boot is %s", hdev->name,
2107 params->secure_boot ? "enabled" : "disabled");
2108
Marcel Holtmann2220994e2015-10-13 13:54:55 +02002109 BT_INFO("%s: OTP lock is %s", hdev->name,
2110 params->otp_lock ? "enabled" : "disabled");
2111
2112 BT_INFO("%s: API lock is %s", hdev->name,
2113 params->api_lock ? "enabled" : "disabled");
2114
2115 BT_INFO("%s: Debug lock is %s", hdev->name,
2116 params->debug_lock ? "enabled" : "disabled");
2117
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002118 BT_INFO("%s: Minimum firmware build %u week %u %u", hdev->name,
2119 params->min_fw_build_nn, params->min_fw_build_cw,
2120 2000 + params->min_fw_build_yy);
2121
2122 /* It is required that every single firmware fragment is acknowledged
2123 * with a command complete event. If the boot parameters indicate
2124 * that this bootloader does not send them, then abort the setup.
2125 */
2126 if (params->limited_cce != 0x00) {
2127 BT_ERR("%s: Unsupported Intel firmware loading method (%u)",
2128 hdev->name, params->limited_cce);
2129 kfree_skb(skb);
2130 return -EINVAL;
2131 }
2132
2133 /* If the OTP has no valid Bluetooth device address, then there will
2134 * also be no valid address for the operational firmware.
2135 */
2136 if (!bacmp(&params->otp_bdaddr, BDADDR_ANY)) {
2137 BT_INFO("%s: No device address configured", hdev->name);
2138 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
2139 }
2140
2141 /* With this Intel bootloader only the hardware variant and device
2142 * revision information are used to select the right firmware.
2143 *
Tedd Ho-Jeong An230b04a2016-06-28 08:56:39 -07002144 * The firmware filename is ibt-<hw_variant>-<dev_revid>.sfi.
2145 *
2146 * Currently the supported hardware variants are:
2147 * 11 (0x0b) for iBT3.0 (LnP/SfP)
2148 * 12 (0x0c) for iBT3.5 (WsP)
Tedd Ho-Jeong An86a61292017-05-01 13:35:12 -07002149 * 17 (0x11) for iBT3.5 (JfP)
2150 * 18 (0x12) for iBT3.5 (ThP)
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002151 */
Tedd Ho-Jeong An230b04a2016-06-28 08:56:39 -07002152 snprintf(fwname, sizeof(fwname), "intel/ibt-%u-%u.sfi",
2153 le16_to_cpu(ver.hw_variant),
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002154 le16_to_cpu(params->dev_revid));
2155
2156 err = request_firmware(&fw, fwname, &hdev->dev);
2157 if (err < 0) {
2158 BT_ERR("%s: Failed to load Intel firmware file (%d)",
2159 hdev->name, err);
2160 kfree_skb(skb);
2161 return err;
2162 }
2163
2164 BT_INFO("%s: Found device firmware: %s", hdev->name, fwname);
2165
Tedd Ho-Jeong An52cc9162015-06-12 16:20:05 -07002166 /* Save the DDC file name for later use to apply once the firmware
2167 * downloading is done.
2168 */
Tedd Ho-Jeong An230b04a2016-06-28 08:56:39 -07002169 snprintf(fwname, sizeof(fwname), "intel/ibt-%u-%u.ddc",
2170 le16_to_cpu(ver.hw_variant),
Tedd Ho-Jeong An52cc9162015-06-12 16:20:05 -07002171 le16_to_cpu(params->dev_revid));
2172
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002173 kfree_skb(skb);
2174
2175 if (fw->size < 644) {
2176 BT_ERR("%s: Invalid size of firmware file (%zu)",
2177 hdev->name, fw->size);
2178 err = -EBADF;
2179 goto done;
2180 }
2181
2182 set_bit(BTUSB_DOWNLOADING, &data->flags);
2183
2184 /* Start the firmware download transaction with the Init fragment
2185 * represented by the 128 bytes of CSS header.
2186 */
Marcel Holtmann09df1232015-07-05 14:55:36 +02002187 err = btintel_secure_send(hdev, 0x00, 128, fw->data);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002188 if (err < 0) {
2189 BT_ERR("%s: Failed to send firmware header (%d)",
2190 hdev->name, err);
2191 goto done;
2192 }
2193
2194 /* Send the 256 bytes of public key information from the firmware
2195 * as the PKey fragment.
2196 */
Marcel Holtmann09df1232015-07-05 14:55:36 +02002197 err = btintel_secure_send(hdev, 0x03, 256, fw->data + 128);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002198 if (err < 0) {
2199 BT_ERR("%s: Failed to send firmware public key (%d)",
2200 hdev->name, err);
2201 goto done;
2202 }
2203
2204 /* Send the 256 bytes of signature information from the firmware
2205 * as the Sign fragment.
2206 */
Marcel Holtmann09df1232015-07-05 14:55:36 +02002207 err = btintel_secure_send(hdev, 0x02, 256, fw->data + 388);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002208 if (err < 0) {
2209 BT_ERR("%s: Failed to send firmware signature (%d)",
2210 hdev->name, err);
2211 goto done;
2212 }
2213
2214 fw_ptr = fw->data + 644;
Marcel Holtmanne66890a92015-06-07 09:47:08 +02002215 frag_len = 0;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002216
2217 while (fw_ptr - fw->data < fw->size) {
Marcel Holtmanne66890a92015-06-07 09:47:08 +02002218 struct hci_command_hdr *cmd = (void *)(fw_ptr + frag_len);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002219
Marcel Holtmanne66890a92015-06-07 09:47:08 +02002220 frag_len += sizeof(*cmd) + cmd->plen;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002221
Minjune Kim5075eda2015-08-27 13:21:52 +09002222 /* The parameter length of the secure send command requires
Marcel Holtmanne66890a92015-06-07 09:47:08 +02002223 * a 4 byte alignment. It happens so that the firmware file
2224 * contains proper Intel_NOP commands to align the fragments
2225 * as needed.
2226 *
2227 * Send set of commands with 4 byte alignment from the
2228 * firmware data buffer as a single Data fragement.
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002229 */
Marcel Holtmanne66890a92015-06-07 09:47:08 +02002230 if (!(frag_len % 4)) {
Marcel Holtmann09df1232015-07-05 14:55:36 +02002231 err = btintel_secure_send(hdev, 0x01, frag_len, fw_ptr);
Marcel Holtmanne66890a92015-06-07 09:47:08 +02002232 if (err < 0) {
2233 BT_ERR("%s: Failed to send firmware data (%d)",
2234 hdev->name, err);
2235 goto done;
2236 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002237
Marcel Holtmanne66890a92015-06-07 09:47:08 +02002238 fw_ptr += frag_len;
2239 frag_len = 0;
2240 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002241 }
2242
Marcel Holtmannce6bb922015-01-28 01:58:40 -08002243 set_bit(BTUSB_FIRMWARE_LOADED, &data->flags);
2244
Johan Hedberga087a982015-01-30 10:58:54 +02002245 BT_INFO("%s: Waiting for firmware download to complete", hdev->name);
2246
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002247 /* Before switching the device into operational mode and with that
2248 * booting the loaded firmware, wait for the bootloader notification
2249 * that all fragments have been successfully received.
2250 *
Johan Hedberga087a982015-01-30 10:58:54 +02002251 * When the event processing receives the notification, then the
2252 * BTUSB_DOWNLOADING flag will be cleared.
2253 *
2254 * The firmware loading should not take longer than 5 seconds
2255 * and thus just timeout if that happens and fail the setup
2256 * of this device.
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002257 */
Johan Hedberg129a7692015-02-14 09:33:35 +02002258 err = wait_on_bit_timeout(&data->flags, BTUSB_DOWNLOADING,
2259 TASK_INTERRUPTIBLE,
2260 msecs_to_jiffies(5000));
Bart Van Asschef0a70a02016-08-11 16:02:44 -07002261 if (err == -EINTR) {
Johan Hedberga087a982015-01-30 10:58:54 +02002262 BT_ERR("%s: Firmware loading interrupted", hdev->name);
Johan Hedberga087a982015-01-30 10:58:54 +02002263 goto done;
2264 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002265
Johan Hedberga087a982015-01-30 10:58:54 +02002266 if (err) {
2267 BT_ERR("%s: Firmware loading timeout", hdev->name);
2268 err = -ETIMEDOUT;
2269 goto done;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002270 }
2271
2272 if (test_bit(BTUSB_FIRMWARE_FAILED, &data->flags)) {
2273 BT_ERR("%s: Firmware loading failed", hdev->name);
2274 err = -ENOEXEC;
2275 goto done;
2276 }
2277
2278 rettime = ktime_get();
2279 delta = ktime_sub(rettime, calltime);
2280 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
2281
2282 BT_INFO("%s: Firmware loaded in %llu usecs", hdev->name, duration);
2283
2284done:
2285 release_firmware(fw);
2286
2287 if (err < 0)
2288 return err;
2289
2290 calltime = ktime_get();
2291
2292 set_bit(BTUSB_BOOTING, &data->flags);
2293
2294 skb = __hci_cmd_sync(hdev, 0xfc01, sizeof(reset_param), reset_param,
2295 HCI_INIT_TIMEOUT);
2296 if (IS_ERR(skb))
2297 return PTR_ERR(skb);
2298
2299 kfree_skb(skb);
2300
2301 /* The bootloader will not indicate when the device is ready. This
2302 * is done by the operational firmware sending bootup notification.
Johan Hedbergfad70972015-01-30 10:58:55 +02002303 *
2304 * Booting into operational firmware should not take longer than
2305 * 1 second. However if that happens, then just fail the setup
2306 * since something went wrong.
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002307 */
Johan Hedbergfad70972015-01-30 10:58:55 +02002308 BT_INFO("%s: Waiting for device to boot", hdev->name);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002309
Johan Hedberg129a7692015-02-14 09:33:35 +02002310 err = wait_on_bit_timeout(&data->flags, BTUSB_BOOTING,
2311 TASK_INTERRUPTIBLE,
2312 msecs_to_jiffies(1000));
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002313
Bart Van Asschef0a70a02016-08-11 16:02:44 -07002314 if (err == -EINTR) {
Johan Hedbergfad70972015-01-30 10:58:55 +02002315 BT_ERR("%s: Device boot interrupted", hdev->name);
2316 return -EINTR;
2317 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002318
Johan Hedbergfad70972015-01-30 10:58:55 +02002319 if (err) {
2320 BT_ERR("%s: Device boot timeout", hdev->name);
2321 return -ETIMEDOUT;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002322 }
2323
2324 rettime = ktime_get();
2325 delta = ktime_sub(rettime, calltime);
2326 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
2327
2328 BT_INFO("%s: Device booted in %llu usecs", hdev->name, duration);
2329
2330 clear_bit(BTUSB_BOOTLOADER, &data->flags);
2331
Tedd Ho-Jeong An52cc9162015-06-12 16:20:05 -07002332 /* Once the device is running in operational mode, it needs to apply
2333 * the device configuration (DDC) parameters.
2334 *
2335 * The device can work without DDC parameters, so even if it fails
2336 * to load the file, no need to fail the setup.
2337 */
Loic Poulaine924d3d2015-09-04 17:54:36 +02002338 btintel_load_ddc_config(hdev, fwname);
Tedd Ho-Jeong An52cc9162015-06-12 16:20:05 -07002339
Marcel Holtmann213445b2015-10-21 02:45:19 +02002340 /* Set the event mask for Intel specific vendor events. This enables
2341 * a few extra events that are useful during general operation. It
2342 * does not enable any debugging related events.
2343 *
2344 * The device will function correctly without these events enabled
2345 * and thus no need to fail the setup.
2346 */
2347 btintel_set_event_mask(hdev, false);
2348
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002349 return 0;
2350}
2351
Tedd Ho-Jeong Anbfbd45e2015-02-13 09:20:52 -08002352static int btusb_shutdown_intel(struct hci_dev *hdev)
2353{
2354 struct sk_buff *skb;
2355 long ret;
2356
2357 /* Some platforms have an issue with BT LED when the interface is
2358 * down or BT radio is turned off, which takes 5 seconds to BT LED
2359 * goes off. This command turns off the BT LED immediately.
2360 */
2361 skb = __hci_cmd_sync(hdev, 0xfc3f, 0, NULL, HCI_INIT_TIMEOUT);
2362 if (IS_ERR(skb)) {
2363 ret = PTR_ERR(skb);
2364 BT_ERR("%s: turning off Intel device LED failed (%ld)",
2365 hdev->name, ret);
2366 return ret;
2367 }
2368 kfree_skb(skb);
2369
2370 return 0;
2371}
2372
Rajat Jaina4ccc9e2017-02-01 14:24:10 -08002373#ifdef CONFIG_PM
2374/* Configure an out-of-band gpio as wake-up pin, if specified in device tree */
2375static int marvell_config_oob_wake(struct hci_dev *hdev)
2376{
2377 struct sk_buff *skb;
2378 struct btusb_data *data = hci_get_drvdata(hdev);
2379 struct device *dev = &data->udev->dev;
2380 u16 pin, gap, opcode;
2381 int ret;
2382 u8 cmd[5];
2383
2384 /* Move on if no wakeup pin specified */
2385 if (of_property_read_u16(dev->of_node, "marvell,wakeup-pin", &pin) ||
2386 of_property_read_u16(dev->of_node, "marvell,wakeup-gap-ms", &gap))
2387 return 0;
2388
2389 /* Vendor specific command to configure a GPIO as wake-up pin */
2390 opcode = hci_opcode_pack(0x3F, 0x59);
2391 cmd[0] = opcode & 0xFF;
2392 cmd[1] = opcode >> 8;
2393 cmd[2] = 2; /* length of parameters that follow */
2394 cmd[3] = pin;
2395 cmd[4] = gap; /* time in ms, for which wakeup pin should be asserted */
2396
2397 skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL);
2398 if (!skb) {
2399 bt_dev_err(hdev, "%s: No memory\n", __func__);
2400 return -ENOMEM;
2401 }
2402
Johannes Berg59ae1d12017-06-16 14:29:20 +02002403 skb_put_data(skb, cmd, sizeof(cmd));
Rajat Jaina4ccc9e2017-02-01 14:24:10 -08002404 hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
2405
2406 ret = btusb_send_frame(hdev, skb);
2407 if (ret) {
2408 bt_dev_err(hdev, "%s: configuration failed\n", __func__);
2409 kfree_skb(skb);
2410 return ret;
2411 }
2412
2413 return 0;
2414}
2415#endif
2416
Amitkumar Karwarae8df492014-07-18 14:47:06 -07002417static int btusb_set_bdaddr_marvell(struct hci_dev *hdev,
2418 const bdaddr_t *bdaddr)
2419{
2420 struct sk_buff *skb;
2421 u8 buf[8];
2422 long ret;
2423
2424 buf[0] = 0xfe;
2425 buf[1] = sizeof(bdaddr_t);
2426 memcpy(buf + 2, bdaddr, sizeof(bdaddr_t));
2427
2428 skb = __hci_cmd_sync(hdev, 0xfc22, sizeof(buf), buf, HCI_INIT_TIMEOUT);
2429 if (IS_ERR(skb)) {
2430 ret = PTR_ERR(skb);
2431 BT_ERR("%s: changing Marvell device address failed (%ld)",
2432 hdev->name, ret);
2433 return ret;
2434 }
2435 kfree_skb(skb);
2436
2437 return 0;
2438}
2439
Toshi Kikuchi58592232014-12-12 10:58:05 -08002440static int btusb_set_bdaddr_ath3012(struct hci_dev *hdev,
2441 const bdaddr_t *bdaddr)
2442{
2443 struct sk_buff *skb;
2444 u8 buf[10];
2445 long ret;
2446
2447 buf[0] = 0x01;
2448 buf[1] = 0x01;
2449 buf[2] = 0x00;
2450 buf[3] = sizeof(bdaddr_t);
2451 memcpy(buf + 4, bdaddr, sizeof(bdaddr_t));
2452
2453 skb = __hci_cmd_sync(hdev, 0xfc0b, sizeof(buf), buf, HCI_INIT_TIMEOUT);
2454 if (IS_ERR(skb)) {
2455 ret = PTR_ERR(skb);
2456 BT_ERR("%s: Change address command failed (%ld)",
2457 hdev->name, ret);
2458 return ret;
2459 }
2460 kfree_skb(skb);
2461
2462 return 0;
2463}
2464
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002465#define QCA_DFU_PACKET_LEN 4096
2466
2467#define QCA_GET_TARGET_VERSION 0x09
2468#define QCA_CHECK_STATUS 0x05
2469#define QCA_DFU_DOWNLOAD 0x01
2470
2471#define QCA_SYSCFG_UPDATED 0x40
2472#define QCA_PATCH_UPDATED 0x80
2473#define QCA_DFU_TIMEOUT 3000
2474
2475struct qca_version {
2476 __le32 rom_version;
2477 __le32 patch_version;
2478 __le32 ram_version;
2479 __le32 ref_clock;
2480 __u8 reserved[4];
2481} __packed;
2482
2483struct qca_rampatch_version {
2484 __le16 rom_version;
2485 __le16 patch_version;
2486} __packed;
2487
2488struct qca_device_info {
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002489 u32 rom_version;
2490 u8 rampatch_hdr; /* length of header in rampatch */
2491 u8 nvm_hdr; /* length of header in NVM */
2492 u8 ver_offset; /* offset of version structure in rampatch */
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002493};
2494
2495static const struct qca_device_info qca_devices_table[] = {
2496 { 0x00000100, 20, 4, 10 }, /* Rome 1.0 */
2497 { 0x00000101, 20, 4, 10 }, /* Rome 1.1 */
Chan-yeol Park7f6e6362015-05-21 11:24:27 +09002498 { 0x00000200, 28, 4, 18 }, /* Rome 2.0 */
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002499 { 0x00000201, 28, 4, 18 }, /* Rome 2.1 */
2500 { 0x00000300, 28, 4, 18 }, /* Rome 3.0 */
2501 { 0x00000302, 28, 4, 18 }, /* Rome 3.2 */
2502};
2503
2504static int btusb_qca_send_vendor_req(struct hci_dev *hdev, u8 request,
2505 void *data, u16 size)
2506{
2507 struct btusb_data *btdata = hci_get_drvdata(hdev);
2508 struct usb_device *udev = btdata->udev;
2509 int pipe, err;
2510 u8 *buf;
2511
2512 buf = kmalloc(size, GFP_KERNEL);
2513 if (!buf)
2514 return -ENOMEM;
2515
2516 /* Found some of USB hosts have IOT issues with ours so that we should
2517 * not wait until HCI layer is ready.
2518 */
2519 pipe = usb_rcvctrlpipe(udev, 0);
2520 err = usb_control_msg(udev, pipe, request, USB_TYPE_VENDOR | USB_DIR_IN,
2521 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
2522 if (err < 0) {
2523 BT_ERR("%s: Failed to access otp area (%d)", hdev->name, err);
2524 goto done;
2525 }
2526
2527 memcpy(data, buf, size);
2528
2529done:
2530 kfree(buf);
2531
2532 return err;
2533}
2534
2535static int btusb_setup_qca_download_fw(struct hci_dev *hdev,
2536 const struct firmware *firmware,
2537 size_t hdr_size)
2538{
2539 struct btusb_data *btdata = hci_get_drvdata(hdev);
2540 struct usb_device *udev = btdata->udev;
2541 size_t count, size, sent = 0;
2542 int pipe, len, err;
2543 u8 *buf;
2544
2545 buf = kmalloc(QCA_DFU_PACKET_LEN, GFP_KERNEL);
2546 if (!buf)
2547 return -ENOMEM;
2548
2549 count = firmware->size;
2550
2551 size = min_t(size_t, count, hdr_size);
2552 memcpy(buf, firmware->data, size);
2553
2554 /* USB patches should go down to controller through USB path
2555 * because binary format fits to go down through USB channel.
2556 * USB control path is for patching headers and USB bulk is for
2557 * patch body.
2558 */
2559 pipe = usb_sndctrlpipe(udev, 0);
2560 err = usb_control_msg(udev, pipe, QCA_DFU_DOWNLOAD, USB_TYPE_VENDOR,
2561 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
2562 if (err < 0) {
2563 BT_ERR("%s: Failed to send headers (%d)", hdev->name, err);
2564 goto done;
2565 }
2566
2567 sent += size;
2568 count -= size;
2569
2570 while (count) {
2571 size = min_t(size_t, count, QCA_DFU_PACKET_LEN);
2572
2573 memcpy(buf, firmware->data + sent, size);
2574
2575 pipe = usb_sndbulkpipe(udev, 0x02);
2576 err = usb_bulk_msg(udev, pipe, buf, size, &len,
2577 QCA_DFU_TIMEOUT);
2578 if (err < 0) {
2579 BT_ERR("%s: Failed to send body at %zd of %zd (%d)",
2580 hdev->name, sent, firmware->size, err);
2581 break;
2582 }
2583
2584 if (size != len) {
2585 BT_ERR("%s: Failed to get bulk buffer", hdev->name);
2586 err = -EILSEQ;
2587 break;
2588 }
2589
2590 sent += size;
2591 count -= size;
2592 }
2593
2594done:
2595 kfree(buf);
2596 return err;
2597}
2598
2599static int btusb_setup_qca_load_rampatch(struct hci_dev *hdev,
2600 struct qca_version *ver,
2601 const struct qca_device_info *info)
2602{
2603 struct qca_rampatch_version *rver;
2604 const struct firmware *fw;
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002605 u32 ver_rom, ver_patch;
2606 u16 rver_rom, rver_patch;
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002607 char fwname[64];
2608 int err;
2609
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002610 ver_rom = le32_to_cpu(ver->rom_version);
2611 ver_patch = le32_to_cpu(ver->patch_version);
2612
2613 snprintf(fwname, sizeof(fwname), "qca/rampatch_usb_%08x.bin", ver_rom);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002614
2615 err = request_firmware(&fw, fwname, &hdev->dev);
2616 if (err) {
2617 BT_ERR("%s: failed to request rampatch file: %s (%d)",
2618 hdev->name, fwname, err);
2619 return err;
2620 }
2621
2622 BT_INFO("%s: using rampatch file: %s", hdev->name, fwname);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002623
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002624 rver = (struct qca_rampatch_version *)(fw->data + info->ver_offset);
2625 rver_rom = le16_to_cpu(rver->rom_version);
2626 rver_patch = le16_to_cpu(rver->patch_version);
2627
2628 BT_INFO("%s: QCA: patch rome 0x%x build 0x%x, firmware rome 0x%x "
2629 "build 0x%x", hdev->name, rver_rom, rver_patch, ver_rom,
2630 ver_patch);
2631
2632 if (rver_rom != ver_rom || rver_patch <= ver_patch) {
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002633 BT_ERR("%s: rampatch file version did not match with firmware",
2634 hdev->name);
2635 err = -EINVAL;
2636 goto done;
2637 }
2638
2639 err = btusb_setup_qca_download_fw(hdev, fw, info->rampatch_hdr);
2640
2641done:
2642 release_firmware(fw);
2643
2644 return err;
2645}
2646
2647static int btusb_setup_qca_load_nvm(struct hci_dev *hdev,
2648 struct qca_version *ver,
2649 const struct qca_device_info *info)
2650{
2651 const struct firmware *fw;
2652 char fwname[64];
2653 int err;
2654
2655 snprintf(fwname, sizeof(fwname), "qca/nvm_usb_%08x.bin",
2656 le32_to_cpu(ver->rom_version));
2657
2658 err = request_firmware(&fw, fwname, &hdev->dev);
2659 if (err) {
2660 BT_ERR("%s: failed to request NVM file: %s (%d)",
2661 hdev->name, fwname, err);
2662 return err;
2663 }
2664
2665 BT_INFO("%s: using NVM file: %s", hdev->name, fwname);
2666
2667 err = btusb_setup_qca_download_fw(hdev, fw, info->nvm_hdr);
2668
2669 release_firmware(fw);
2670
2671 return err;
2672}
2673
2674static int btusb_setup_qca(struct hci_dev *hdev)
2675{
2676 const struct qca_device_info *info = NULL;
2677 struct qca_version ver;
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002678 u32 ver_rom;
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002679 u8 status;
2680 int i, err;
2681
2682 err = btusb_qca_send_vendor_req(hdev, QCA_GET_TARGET_VERSION, &ver,
Marcel Holtmanneb500422015-04-16 23:15:50 +02002683 sizeof(ver));
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002684 if (err < 0)
2685 return err;
2686
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002687 ver_rom = le32_to_cpu(ver.rom_version);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002688 for (i = 0; i < ARRAY_SIZE(qca_devices_table); i++) {
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002689 if (ver_rom == qca_devices_table[i].rom_version)
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002690 info = &qca_devices_table[i];
2691 }
2692 if (!info) {
2693 BT_ERR("%s: don't support firmware rome 0x%x", hdev->name,
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002694 ver_rom);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002695 return -ENODEV;
2696 }
2697
2698 err = btusb_qca_send_vendor_req(hdev, QCA_CHECK_STATUS, &status,
2699 sizeof(status));
2700 if (err < 0)
2701 return err;
2702
2703 if (!(status & QCA_PATCH_UPDATED)) {
2704 err = btusb_setup_qca_load_rampatch(hdev, &ver, info);
2705 if (err < 0)
2706 return err;
2707 }
2708
2709 if (!(status & QCA_SYSCFG_UPDATED)) {
2710 err = btusb_setup_qca_load_nvm(hdev, &ver, info);
2711 if (err < 0)
2712 return err;
2713 }
2714
2715 return 0;
2716}
2717
Marcel Holtmann9d08f502015-10-08 20:23:08 +02002718#ifdef CONFIG_BT_HCIBTUSB_BCM
2719static inline int __set_diag_interface(struct hci_dev *hdev)
2720{
2721 struct btusb_data *data = hci_get_drvdata(hdev);
2722 struct usb_interface *intf = data->diag;
2723 int i;
2724
2725 if (!data->diag)
2726 return -ENODEV;
2727
2728 data->diag_tx_ep = NULL;
2729 data->diag_rx_ep = NULL;
2730
2731 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
2732 struct usb_endpoint_descriptor *ep_desc;
2733
2734 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
2735
2736 if (!data->diag_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
2737 data->diag_tx_ep = ep_desc;
2738 continue;
2739 }
2740
2741 if (!data->diag_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
2742 data->diag_rx_ep = ep_desc;
2743 continue;
2744 }
2745 }
2746
2747 if (!data->diag_tx_ep || !data->diag_rx_ep) {
2748 BT_ERR("%s invalid diagnostic descriptors", hdev->name);
2749 return -ENODEV;
2750 }
2751
2752 return 0;
2753}
2754
2755static struct urb *alloc_diag_urb(struct hci_dev *hdev, bool enable)
2756{
2757 struct btusb_data *data = hci_get_drvdata(hdev);
2758 struct sk_buff *skb;
2759 struct urb *urb;
2760 unsigned int pipe;
2761
2762 if (!data->diag_tx_ep)
2763 return ERR_PTR(-ENODEV);
2764
2765 urb = usb_alloc_urb(0, GFP_KERNEL);
2766 if (!urb)
2767 return ERR_PTR(-ENOMEM);
2768
2769 skb = bt_skb_alloc(2, GFP_KERNEL);
2770 if (!skb) {
2771 usb_free_urb(urb);
2772 return ERR_PTR(-ENOMEM);
2773 }
2774
Johannes Berg634fef62017-06-16 14:29:24 +02002775 skb_put_u8(skb, 0xf0);
2776 skb_put_u8(skb, enable);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02002777
2778 pipe = usb_sndbulkpipe(data->udev, data->diag_tx_ep->bEndpointAddress);
2779
2780 usb_fill_bulk_urb(urb, data->udev, pipe,
2781 skb->data, skb->len, btusb_tx_complete, skb);
2782
2783 skb->dev = (void *)hdev;
2784
2785 return urb;
2786}
2787
2788static int btusb_bcm_set_diag(struct hci_dev *hdev, bool enable)
2789{
2790 struct btusb_data *data = hci_get_drvdata(hdev);
2791 struct urb *urb;
2792
2793 if (!data->diag)
2794 return -ENODEV;
2795
2796 if (!test_bit(HCI_RUNNING, &hdev->flags))
2797 return -ENETDOWN;
2798
2799 urb = alloc_diag_urb(hdev, enable);
2800 if (IS_ERR(urb))
2801 return PTR_ERR(urb);
2802
2803 return submit_or_queue_tx_urb(hdev, urb);
2804}
2805#endif
2806
Rajat Jainfd913ef2017-02-01 14:24:09 -08002807#ifdef CONFIG_PM
2808static irqreturn_t btusb_oob_wake_handler(int irq, void *priv)
2809{
2810 struct btusb_data *data = priv;
2811
2812 pm_wakeup_event(&data->udev->dev, 0);
Jeffy Chen017789f2017-02-24 14:24:29 +08002813 pm_system_wakeup();
Rajat Jainfd913ef2017-02-01 14:24:09 -08002814
2815 /* Disable only if not already disabled (keep it balanced) */
2816 if (test_and_clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags)) {
2817 disable_irq_nosync(irq);
2818 disable_irq_wake(irq);
2819 }
2820 return IRQ_HANDLED;
2821}
2822
2823static const struct of_device_id btusb_match_table[] = {
2824 { .compatible = "usb1286,204e" },
2825 { }
2826};
2827MODULE_DEVICE_TABLE(of, btusb_match_table);
2828
2829/* Use an oob wakeup pin? */
2830static int btusb_config_oob_wake(struct hci_dev *hdev)
2831{
2832 struct btusb_data *data = hci_get_drvdata(hdev);
2833 struct device *dev = &data->udev->dev;
2834 int irq, ret;
2835
2836 clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags);
2837
2838 if (!of_match_device(btusb_match_table, dev))
2839 return 0;
2840
2841 /* Move on if no IRQ specified */
2842 irq = of_irq_get_byname(dev->of_node, "wakeup");
2843 if (irq <= 0) {
2844 bt_dev_dbg(hdev, "%s: no OOB Wakeup IRQ in DT", __func__);
2845 return 0;
2846 }
2847
2848 ret = devm_request_irq(&hdev->dev, irq, btusb_oob_wake_handler,
2849 0, "OOB Wake-on-BT", data);
2850 if (ret) {
2851 bt_dev_err(hdev, "%s: IRQ request failed", __func__);
2852 return ret;
2853 }
2854
2855 ret = device_init_wakeup(dev, true);
2856 if (ret) {
2857 bt_dev_err(hdev, "%s: failed to init_wakeup", __func__);
2858 return ret;
2859 }
2860
2861 data->oob_wake_irq = irq;
2862 disable_irq(irq);
2863 bt_dev_info(hdev, "OOB Wake-on-BT configured at IRQ %u", irq);
2864 return 0;
2865}
2866#endif
2867
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002868static int btusb_probe(struct usb_interface *intf,
Marcel Holtmann89e75332014-09-16 04:44:50 +02002869 const struct usb_device_id *id)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002870{
2871 struct usb_endpoint_descriptor *ep_desc;
2872 struct btusb_data *data;
2873 struct hci_dev *hdev;
Marcel Holtmann22f8e9d2015-10-20 00:53:33 +02002874 unsigned ifnum_base;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002875 int i, err;
2876
2877 BT_DBG("intf %p id %p", intf, id);
2878
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002879 /* interface numbers are hardcoded in the spec */
Marcel Holtmann22f8e9d2015-10-20 00:53:33 +02002880 if (intf->cur_altsetting->desc.bInterfaceNumber != 0) {
2881 if (!(id->driver_info & BTUSB_IFNUM_2))
2882 return -ENODEV;
2883 if (intf->cur_altsetting->desc.bInterfaceNumber != 2)
2884 return -ENODEV;
2885 }
2886
2887 ifnum_base = intf->cur_altsetting->desc.bInterfaceNumber;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002888
2889 if (!id->driver_info) {
2890 const struct usb_device_id *match;
Marcel Holtmann89e75332014-09-16 04:44:50 +02002891
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002892 match = usb_match_id(intf, blacklist_table);
2893 if (match)
2894 id = match;
2895 }
2896
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002897 if (id->driver_info == BTUSB_IGNORE)
2898 return -ENODEV;
2899
Steven.Li2d25f8b2011-07-01 14:02:36 +08002900 if (id->driver_info & BTUSB_ATH3012) {
2901 struct usb_device *udev = interface_to_usbdev(intf);
2902
2903 /* Old firmware would otherwise let ath3k driver load
Derek Robsond98422c2017-07-22 13:47:07 +12002904 * patch and sysconfig files
2905 */
Steven.Li2d25f8b2011-07-01 14:02:36 +08002906 if (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x0001)
2907 return -ENODEV;
2908 }
2909
Sachin Kamat98921db2012-07-27 12:38:39 +05302910 data = devm_kzalloc(&intf->dev, sizeof(*data), GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002911 if (!data)
2912 return -ENOMEM;
2913
2914 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
2915 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
2916
2917 if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
2918 data->intr_ep = ep_desc;
2919 continue;
2920 }
2921
2922 if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
2923 data->bulk_tx_ep = ep_desc;
2924 continue;
2925 }
2926
2927 if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
2928 data->bulk_rx_ep = ep_desc;
2929 continue;
2930 }
2931 }
2932
Sachin Kamat98921db2012-07-27 12:38:39 +05302933 if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002934 return -ENODEV;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002935
Marcel Holtmann893ba542015-01-28 20:27:34 -08002936 if (id->driver_info & BTUSB_AMP) {
2937 data->cmdreq_type = USB_TYPE_CLASS | 0x01;
2938 data->cmdreq = 0x2b;
2939 } else {
2940 data->cmdreq_type = USB_TYPE_CLASS;
2941 data->cmdreq = 0x00;
2942 }
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002943
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002944 data->udev = interface_to_usbdev(intf);
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02002945 data->intf = intf;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002946
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002947 INIT_WORK(&data->work, btusb_work);
Oliver Neukum7bee5492009-08-24 23:44:59 +02002948 INIT_WORK(&data->waker, btusb_waker);
Marcel Holtmann803b5832014-09-16 08:00:29 +02002949 init_usb_anchor(&data->deferred);
2950 init_usb_anchor(&data->tx_anchor);
Oliver Neukum7bee5492009-08-24 23:44:59 +02002951 spin_lock_init(&data->txlock);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002952
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002953 init_usb_anchor(&data->intr_anchor);
2954 init_usb_anchor(&data->bulk_anchor);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02002955 init_usb_anchor(&data->isoc_anchor);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02002956 init_usb_anchor(&data->diag_anchor);
Marcel Holtmann803b5832014-09-16 08:00:29 +02002957 spin_lock_init(&data->rxlock);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002958
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002959 if (id->driver_info & BTUSB_INTEL_NEW) {
2960 data->recv_event = btusb_recv_event_intel;
2961 data->recv_bulk = btusb_recv_bulk_intel;
2962 set_bit(BTUSB_BOOTLOADER, &data->flags);
2963 } else {
2964 data->recv_event = hci_recv_frame;
2965 data->recv_bulk = btusb_recv_bulk;
2966 }
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +01002967
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002968 hdev = hci_alloc_dev();
Sachin Kamat98921db2012-07-27 12:38:39 +05302969 if (!hdev)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002970 return -ENOMEM;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002971
Marcel Holtmannc13854c2010-02-08 15:27:07 +01002972 hdev->bus = HCI_USB;
David Herrmann155961e2012-02-09 21:58:32 +01002973 hci_set_drvdata(hdev, data);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002974
Marcel Holtmann893ba542015-01-28 20:27:34 -08002975 if (id->driver_info & BTUSB_AMP)
2976 hdev->dev_type = HCI_AMP;
2977 else
Marcel Holtmannca8bee52016-07-05 14:30:14 +02002978 hdev->dev_type = HCI_PRIMARY;
Marcel Holtmann893ba542015-01-28 20:27:34 -08002979
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002980 data->hdev = hdev;
2981
2982 SET_HCIDEV_DEV(hdev, &intf->dev);
2983
Marcel Holtmann9f8f9622013-04-10 08:11:35 -07002984 hdev->open = btusb_open;
2985 hdev->close = btusb_close;
2986 hdev->flush = btusb_flush;
2987 hdev->send = btusb_send_frame;
2988 hdev->notify = btusb_notify;
2989
Rajat Jainfd913ef2017-02-01 14:24:09 -08002990#ifdef CONFIG_PM
2991 err = btusb_config_oob_wake(hdev);
2992 if (err)
2993 goto out_free_dev;
Rajat Jaina4ccc9e2017-02-01 14:24:10 -08002994
2995 /* Marvell devices may need a specific chip configuration */
2996 if (id->driver_info & BTUSB_MARVELL && data->oob_wake_irq) {
2997 err = marvell_config_oob_wake(hdev);
2998 if (err)
2999 goto out_free_dev;
3000 }
Rajat Jainfd913ef2017-02-01 14:24:09 -08003001#endif
Szymon Janc418678b2016-09-01 17:22:37 +02003002 if (id->driver_info & BTUSB_CW6622)
3003 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
3004
Marcel Holtmann6c9d4352015-10-17 14:39:27 +02003005 if (id->driver_info & BTUSB_BCM2045)
3006 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
3007
Marcel Holtmann9f8f9622013-04-10 08:11:35 -07003008 if (id->driver_info & BTUSB_BCM92035)
3009 hdev->setup = btusb_setup_bcm92035;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003010
Marcel Holtmannc2bfb102015-04-05 22:52:14 -07003011#ifdef CONFIG_BT_HCIBTUSB_BCM
Marcel Holtmannabbaf502014-07-02 00:53:48 +02003012 if (id->driver_info & BTUSB_BCM_PATCHRAM) {
Marcel Holtmann49a5f782015-10-20 02:30:48 +02003013 hdev->manufacturer = 15;
Marcel Holtmannc2bfb102015-04-05 22:52:14 -07003014 hdev->setup = btbcm_setup_patchram;
Marcel Holtmann9d08f502015-10-08 20:23:08 +02003015 hdev->set_diag = btusb_bcm_set_diag;
Marcel Holtmann1df1f592015-04-05 22:52:11 -07003016 hdev->set_bdaddr = btbcm_set_bdaddr;
Marcel Holtmann9d08f502015-10-08 20:23:08 +02003017
3018 /* Broadcom LM_DIAG Interface numbers are hardcoded */
Marcel Holtmann22f8e9d2015-10-20 00:53:33 +02003019 data->diag = usb_ifnum_to_if(data->udev, ifnum_base + 2);
Marcel Holtmannabbaf502014-07-02 00:53:48 +02003020 }
Petri Gynther10d4c672014-05-08 15:50:01 -07003021
Marcel Holtmann9d08f502015-10-08 20:23:08 +02003022 if (id->driver_info & BTUSB_BCM_APPLE) {
Marcel Holtmann49a5f782015-10-20 02:30:48 +02003023 hdev->manufacturer = 15;
Marcel Holtmannc2bfb102015-04-05 22:52:14 -07003024 hdev->setup = btbcm_setup_apple;
Marcel Holtmann9d08f502015-10-08 20:23:08 +02003025 hdev->set_diag = btusb_bcm_set_diag;
3026
3027 /* Broadcom LM_DIAG Interface numbers are hardcoded */
Marcel Holtmann22f8e9d2015-10-20 00:53:33 +02003028 data->diag = usb_ifnum_to_if(data->udev, ifnum_base + 2);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02003029 }
Marcel Holtmannc2bfb102015-04-05 22:52:14 -07003030#endif
Marcel Holtmann17b27722015-03-22 15:52:38 +01003031
Marcel Holtmanncb8d6592014-07-02 11:25:25 +02003032 if (id->driver_info & BTUSB_INTEL) {
Marcel Holtmann49a5f782015-10-20 02:30:48 +02003033 hdev->manufacturer = 2;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07003034 hdev->setup = btusb_setup_intel;
Tedd Ho-Jeong Anbfbd45e2015-02-13 09:20:52 -08003035 hdev->shutdown = btusb_shutdown_intel;
Marcel Holtmann3e247672015-10-17 16:00:28 +02003036 hdev->set_diag = btintel_set_diag_mfg;
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07003037 hdev->set_bdaddr = btintel_set_bdaddr;
Jakub Pawlowskic33fb9b2015-01-30 18:55:58 -08003038 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
Jakub Pawlowskic1154842015-03-17 09:04:16 -07003039 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
Marcel Holtmann3e247672015-10-17 16:00:28 +02003040 set_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks);
Marcel Holtmanncb8d6592014-07-02 11:25:25 +02003041 }
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07003042
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08003043 if (id->driver_info & BTUSB_INTEL_NEW) {
Marcel Holtmann49a5f782015-10-20 02:30:48 +02003044 hdev->manufacturer = 2;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08003045 hdev->send = btusb_send_frame_intel;
3046 hdev->setup = btusb_setup_intel_new;
Marcel Holtmanneeb6abe2015-07-05 14:37:39 +02003047 hdev->hw_error = btintel_hw_error;
Marcel Holtmann6d2e50d2015-10-09 14:42:08 +02003048 hdev->set_diag = btintel_set_diag;
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07003049 hdev->set_bdaddr = btintel_set_bdaddr;
Marcel Holtmannb970c5b2015-02-01 23:57:18 -08003050 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
Marcel Holtmannd8270fb2015-10-17 16:00:27 +02003051 set_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08003052 }
3053
Amitkumar Karwarae8df492014-07-18 14:47:06 -07003054 if (id->driver_info & BTUSB_MARVELL)
3055 hdev->set_bdaddr = btusb_set_bdaddr_marvell;
3056
Marcel Holtmann661cf882015-01-02 23:35:20 -08003057 if (id->driver_info & BTUSB_SWAVE) {
3058 set_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks);
Marcel Holtmannd57dbe72014-12-26 04:42:33 +01003059 set_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS, &hdev->quirks);
Marcel Holtmann661cf882015-01-02 23:35:20 -08003060 }
Marcel Holtmannd57dbe72014-12-26 04:42:33 +01003061
Marcel Holtmanne4c534b2015-10-21 01:31:45 +02003062 if (id->driver_info & BTUSB_INTEL_BOOT) {
3063 hdev->manufacturer = 2;
Marcel Holtmann40df7832014-07-06 13:29:58 +02003064 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
Marcel Holtmanne4c534b2015-10-21 01:31:45 +02003065 }
Marcel Holtmann40df7832014-07-06 13:29:58 +02003066
Jakub Pawlowski79f0c872015-01-29 10:38:34 -08003067 if (id->driver_info & BTUSB_ATH3012) {
Toshi Kikuchi58592232014-12-12 10:58:05 -08003068 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
Jakub Pawlowski3d50d512015-03-17 09:04:15 -07003069 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
Jakub Pawlowski79f0c872015-01-29 10:38:34 -08003070 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
3071 }
Toshi Kikuchi58592232014-12-12 10:58:05 -08003072
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003073 if (id->driver_info & BTUSB_QCA_ROME) {
3074 data->setup_on_usb = btusb_setup_qca;
3075 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
Leif Liddyfd865802017-07-08 20:55:32 +02003076
3077 /* QCA Rome devices lose their updated firmware over suspend,
3078 * but the USB hub doesn't notice any status change.
3079 * Explicitly request a device reset on resume.
3080 */
3081 set_bit(BTUSB_RESET_RESUME, &data->flags);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003082 }
3083
Carlo Caionedb33c772015-05-14 10:49:09 +02003084#ifdef CONFIG_BT_HCIBTUSB_RTL
Daniel Drake04b8c812015-05-21 08:23:50 -06003085 if (id->driver_info & BTUSB_REALTEK) {
Carlo Caionedb33c772015-05-14 10:49:09 +02003086 hdev->setup = btrtl_setup_realtek;
Daniel Drake04b8c812015-05-21 08:23:50 -06003087
3088 /* Realtek devices lose their updated firmware over suspend,
3089 * but the USB hub doesn't notice any status change.
3090 * Explicitly request a device reset on resume.
3091 */
3092 set_bit(BTUSB_RESET_RESUME, &data->flags);
3093 }
Carlo Caionedb33c772015-05-14 10:49:09 +02003094#endif
Daniel Drakea2698a92015-04-16 14:09:55 -06003095
Marcel Holtmann893ba542015-01-28 20:27:34 -08003096 if (id->driver_info & BTUSB_AMP) {
3097 /* AMP controllers do not support SCO packets */
3098 data->isoc = NULL;
3099 } else {
Marcel Holtmann22f8e9d2015-10-20 00:53:33 +02003100 /* Interface orders are hardcoded in the specification */
3101 data->isoc = usb_ifnum_to_if(data->udev, ifnum_base + 1);
Marcel Holtmann893ba542015-01-28 20:27:34 -08003102 }
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02003103
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01003104 if (!reset)
Szymon Janca6c511c2012-05-23 12:35:46 +02003105 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02003106
3107 if (force_scofix || id->driver_info & BTUSB_WRONG_SCO_MTU) {
3108 if (!disable_scofix)
3109 set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
3110 }
3111
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02003112 if (id->driver_info & BTUSB_BROKEN_ISOC)
3113 data->isoc = NULL;
3114
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01003115 if (id->driver_info & BTUSB_DIGIANSWER) {
3116 data->cmdreq_type = USB_TYPE_VENDOR;
Szymon Janca6c511c2012-05-23 12:35:46 +02003117 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01003118 }
3119
3120 if (id->driver_info & BTUSB_CSR) {
3121 struct usb_device *udev = data->udev;
Marcel Holtmann81cac642014-01-03 03:02:36 -08003122 u16 bcdDevice = le16_to_cpu(udev->descriptor.bcdDevice);
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01003123
3124 /* Old firmware would otherwise execute USB reset */
Marcel Holtmann81cac642014-01-03 03:02:36 -08003125 if (bcdDevice < 0x117)
Szymon Janca6c511c2012-05-23 12:35:46 +02003126 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmann81cac642014-01-03 03:02:36 -08003127
3128 /* Fake CSR devices with broken commands */
Johan Hedberg6cafcd92015-08-30 21:47:21 +03003129 if (bcdDevice <= 0x100 || bcdDevice == 0x134)
Marcel Holtmann81cac642014-01-03 03:02:36 -08003130 hdev->setup = btusb_setup_csr;
Jakub Pawlowski49c989a2015-03-17 09:04:17 -07003131
3132 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01003133 }
3134
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02003135 if (id->driver_info & BTUSB_SNIFFER) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02003136 struct usb_device *udev = data->udev;
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02003137
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01003138 /* New sniffer firmware has crippled HCI interface */
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02003139 if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
3140 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
3141 }
3142
Marcel Holtmann3a5ef202014-07-06 14:53:54 +02003143 if (id->driver_info & BTUSB_INTEL_BOOT) {
3144 /* A bug in the bootloader causes that interrupt interface is
3145 * only enabled after receiving SetInterface(0, AltSetting=0).
3146 */
3147 err = usb_set_interface(data->udev, 0, 0);
3148 if (err < 0) {
3149 BT_ERR("failed to set interface 0, alt 0 %d", err);
Rajat Jain10ab1332017-02-01 14:24:08 -08003150 goto out_free_dev;
Marcel Holtmann3a5ef202014-07-06 14:53:54 +02003151 }
3152 }
3153
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02003154 if (data->isoc) {
3155 err = usb_driver_claim_interface(&btusb_driver,
Marcel Holtmann89e75332014-09-16 04:44:50 +02003156 data->isoc, data);
Rajat Jain10ab1332017-02-01 14:24:08 -08003157 if (err < 0)
3158 goto out_free_dev;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02003159 }
3160
Marcel Holtmann9d08f502015-10-08 20:23:08 +02003161#ifdef CONFIG_BT_HCIBTUSB_BCM
3162 if (data->diag) {
3163 if (!usb_driver_claim_interface(&btusb_driver,
3164 data->diag, data))
3165 __set_diag_interface(hdev);
3166 else
3167 data->diag = NULL;
3168 }
3169#endif
3170
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003171 err = hci_register_dev(hdev);
Rajat Jain10ab1332017-02-01 14:24:08 -08003172 if (err < 0)
3173 goto out_free_dev;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003174
3175 usb_set_intfdata(intf, data);
3176
3177 return 0;
Rajat Jain10ab1332017-02-01 14:24:08 -08003178
3179out_free_dev:
3180 hci_free_dev(hdev);
3181 return err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003182}
3183
3184static void btusb_disconnect(struct usb_interface *intf)
3185{
3186 struct btusb_data *data = usb_get_intfdata(intf);
3187 struct hci_dev *hdev;
3188
3189 BT_DBG("intf %p", intf);
3190
3191 if (!data)
3192 return;
3193
3194 hdev = data->hdev;
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02003195 usb_set_intfdata(data->intf, NULL);
3196
3197 if (data->isoc)
3198 usb_set_intfdata(data->isoc, NULL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003199
Marcel Holtmann9d08f502015-10-08 20:23:08 +02003200 if (data->diag)
3201 usb_set_intfdata(data->diag, NULL);
3202
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003203 hci_unregister_dev(hdev);
3204
Marcel Holtmann9d08f502015-10-08 20:23:08 +02003205 if (intf == data->intf) {
3206 if (data->isoc)
3207 usb_driver_release_interface(&btusb_driver, data->isoc);
3208 if (data->diag)
3209 usb_driver_release_interface(&btusb_driver, data->diag);
3210 } else if (intf == data->isoc) {
3211 if (data->diag)
3212 usb_driver_release_interface(&btusb_driver, data->diag);
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02003213 usb_driver_release_interface(&btusb_driver, data->intf);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02003214 } else if (intf == data->diag) {
3215 usb_driver_release_interface(&btusb_driver, data->intf);
3216 if (data->isoc)
3217 usb_driver_release_interface(&btusb_driver, data->isoc);
3218 }
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02003219
Rajat Jainfd913ef2017-02-01 14:24:09 -08003220 if (data->oob_wake_irq)
3221 device_init_wakeup(&data->udev->dev, false);
3222
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003223 hci_free_dev(hdev);
3224}
3225
Oliver Neukum7bee5492009-08-24 23:44:59 +02003226#ifdef CONFIG_PM
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003227static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
3228{
3229 struct btusb_data *data = usb_get_intfdata(intf);
3230
3231 BT_DBG("intf %p", intf);
3232
3233 if (data->suspend_count++)
3234 return 0;
3235
Oliver Neukum7bee5492009-08-24 23:44:59 +02003236 spin_lock_irq(&data->txlock);
Alan Stern5b1b0b82011-08-19 23:49:48 +02003237 if (!(PMSG_IS_AUTO(message) && data->tx_in_flight)) {
Oliver Neukum7bee5492009-08-24 23:44:59 +02003238 set_bit(BTUSB_SUSPENDING, &data->flags);
3239 spin_unlock_irq(&data->txlock);
3240 } else {
3241 spin_unlock_irq(&data->txlock);
3242 data->suspend_count--;
3243 return -EBUSY;
3244 }
3245
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003246 cancel_work_sync(&data->work);
3247
Oliver Neukum7bee5492009-08-24 23:44:59 +02003248 btusb_stop_traffic(data);
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003249 usb_kill_anchored_urbs(&data->tx_anchor);
3250
Rajat Jainfd913ef2017-02-01 14:24:09 -08003251 if (data->oob_wake_irq && device_may_wakeup(&data->udev->dev)) {
3252 set_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags);
3253 enable_irq_wake(data->oob_wake_irq);
3254 enable_irq(data->oob_wake_irq);
3255 }
3256
Daniel Drake04b8c812015-05-21 08:23:50 -06003257 /* Optionally request a device reset on resume, but only when
3258 * wakeups are disabled. If wakeups are enabled we assume the
3259 * device will stay powered up throughout suspend.
3260 */
3261 if (test_bit(BTUSB_RESET_RESUME, &data->flags) &&
3262 !device_may_wakeup(&data->udev->dev))
3263 data->udev->reset_resume = 1;
3264
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003265 return 0;
3266}
3267
Oliver Neukum7bee5492009-08-24 23:44:59 +02003268static void play_deferred(struct btusb_data *data)
3269{
3270 struct urb *urb;
3271 int err;
3272
3273 while ((urb = usb_get_from_anchor(&data->deferred))) {
Jeffy Chen19cfe912017-07-20 18:53:50 +08003274 usb_anchor_urb(urb, &data->tx_anchor);
3275
Oliver Neukum7bee5492009-08-24 23:44:59 +02003276 err = usb_submit_urb(urb, GFP_ATOMIC);
Jeffy Chen19cfe912017-07-20 18:53:50 +08003277 if (err < 0) {
3278 if (err != -EPERM && err != -ENODEV)
3279 BT_ERR("%s urb %p submission failed (%d)",
3280 data->hdev->name, urb, -err);
3281 kfree(urb->setup_packet);
3282 usb_unanchor_urb(urb);
3283 usb_free_urb(urb);
Oliver Neukum7bee5492009-08-24 23:44:59 +02003284 break;
Jeffy Chen19cfe912017-07-20 18:53:50 +08003285 }
Oliver Neukum7bee5492009-08-24 23:44:59 +02003286
3287 data->tx_in_flight++;
Jeffy Chen19cfe912017-07-20 18:53:50 +08003288 usb_free_urb(urb);
Oliver Neukum7bee5492009-08-24 23:44:59 +02003289 }
Jeffy Chen19cfe912017-07-20 18:53:50 +08003290
3291 /* Cleanup the rest deferred urbs. */
3292 while ((urb = usb_get_from_anchor(&data->deferred))) {
3293 kfree(urb->setup_packet);
3294 usb_free_urb(urb);
3295 }
Oliver Neukum7bee5492009-08-24 23:44:59 +02003296}
3297
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003298static int btusb_resume(struct usb_interface *intf)
3299{
3300 struct btusb_data *data = usb_get_intfdata(intf);
3301 struct hci_dev *hdev = data->hdev;
Oliver Neukum7bee5492009-08-24 23:44:59 +02003302 int err = 0;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003303
3304 BT_DBG("intf %p", intf);
3305
3306 if (--data->suspend_count)
3307 return 0;
3308
Rajat Jainfd913ef2017-02-01 14:24:09 -08003309 /* Disable only if not already disabled (keep it balanced) */
3310 if (test_and_clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags)) {
3311 disable_irq(data->oob_wake_irq);
3312 disable_irq_wake(data->oob_wake_irq);
3313 }
3314
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003315 if (!test_bit(HCI_RUNNING, &hdev->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +02003316 goto done;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003317
3318 if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
3319 err = btusb_submit_intr_urb(hdev, GFP_NOIO);
3320 if (err < 0) {
3321 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02003322 goto failed;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003323 }
3324 }
3325
3326 if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) {
Marcel Holtmann43c2e572009-02-04 17:41:38 +01003327 err = btusb_submit_bulk_urb(hdev, GFP_NOIO);
3328 if (err < 0) {
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003329 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02003330 goto failed;
3331 }
3332
3333 btusb_submit_bulk_urb(hdev, GFP_NOIO);
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003334 }
3335
3336 if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
3337 if (btusb_submit_isoc_urb(hdev, GFP_NOIO) < 0)
3338 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
3339 else
3340 btusb_submit_isoc_urb(hdev, GFP_NOIO);
3341 }
3342
Oliver Neukum7bee5492009-08-24 23:44:59 +02003343 spin_lock_irq(&data->txlock);
3344 play_deferred(data);
3345 clear_bit(BTUSB_SUSPENDING, &data->flags);
3346 spin_unlock_irq(&data->txlock);
3347 schedule_work(&data->work);
3348
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003349 return 0;
Oliver Neukum7bee5492009-08-24 23:44:59 +02003350
3351failed:
3352 usb_scuttle_anchored_urbs(&data->deferred);
3353done:
3354 spin_lock_irq(&data->txlock);
3355 clear_bit(BTUSB_SUSPENDING, &data->flags);
3356 spin_unlock_irq(&data->txlock);
3357
3358 return err;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003359}
Oliver Neukum7bee5492009-08-24 23:44:59 +02003360#endif
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003361
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003362static struct usb_driver btusb_driver = {
3363 .name = "btusb",
3364 .probe = btusb_probe,
3365 .disconnect = btusb_disconnect,
Oliver Neukum7bee5492009-08-24 23:44:59 +02003366#ifdef CONFIG_PM
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003367 .suspend = btusb_suspend,
3368 .resume = btusb_resume,
Oliver Neukum7bee5492009-08-24 23:44:59 +02003369#endif
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003370 .id_table = btusb_table,
Oliver Neukum7bee5492009-08-24 23:44:59 +02003371 .supports_autosuspend = 1,
Sarah Sharpe1f12eb2012-04-23 10:08:51 -07003372 .disable_hub_initiated_lpm = 1,
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003373};
3374
Greg Kroah-Hartman93f15082011-11-18 09:47:34 -08003375module_usb_driver(btusb_driver);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003376
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02003377module_param(disable_scofix, bool, 0644);
3378MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
3379
3380module_param(force_scofix, bool, 0644);
3381MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
3382
3383module_param(reset, bool, 0644);
3384MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
3385
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003386MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
3387MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
3388MODULE_VERSION(VERSION);
3389MODULE_LICENSE("GPL");