blob: 24cc8383fdd439d299d59b7848f279772a535446 [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 },
Brian Norrisd829b9e2017-07-31 17:59:39 -0700272 { USB_DEVICE(0x04ca, 0x3016), .driver_info = BTUSB_QCA_ROME },
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +0000273
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200274 /* Broadcom BCM2035 */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100275 { USB_DEVICE(0x0a5c, 0x2009), .driver_info = BTUSB_BCM92035 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200276 { USB_DEVICE(0x0a5c, 0x200a), .driver_info = BTUSB_WRONG_SCO_MTU },
277 { USB_DEVICE(0x0a5c, 0x2035), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200278
279 /* Broadcom BCM2045 */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100280 { USB_DEVICE(0x0a5c, 0x2039), .driver_info = BTUSB_WRONG_SCO_MTU },
281 { USB_DEVICE(0x0a5c, 0x2101), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmannbdbef3d2008-09-23 00:16:35 +0200282
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200283 /* IBM/Lenovo ThinkPad with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100284 { USB_DEVICE(0x0a5c, 0x201e), .driver_info = BTUSB_WRONG_SCO_MTU },
285 { USB_DEVICE(0x0a5c, 0x2110), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200286
287 /* HP laptop with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100288 { USB_DEVICE(0x03f0, 0x171d), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200289
290 /* Dell laptop with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100291 { USB_DEVICE(0x413c, 0x8126), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200292
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100293 /* Dell Wireless 370 and 410 devices */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100294 { USB_DEVICE(0x413c, 0x8152), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100295 { USB_DEVICE(0x413c, 0x8156), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200296
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100297 /* Belkin F8T012 and F8T013 devices */
298 { USB_DEVICE(0x050d, 0x0012), .driver_info = BTUSB_WRONG_SCO_MTU },
299 { USB_DEVICE(0x050d, 0x0013), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200300
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100301 /* Asus WL-BTD202 device */
302 { USB_DEVICE(0x0b05, 0x1715), .driver_info = BTUSB_WRONG_SCO_MTU },
303
304 /* Kensington Bluetooth USB adapter */
305 { USB_DEVICE(0x047d, 0x105e), .driver_info = BTUSB_WRONG_SCO_MTU },
306
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200307 /* RTX Telecom based adapters with buggy SCO support */
308 { USB_DEVICE(0x0400, 0x0807), .driver_info = BTUSB_BROKEN_ISOC },
309 { USB_DEVICE(0x0400, 0x080a), .driver_info = BTUSB_BROKEN_ISOC },
310
311 /* CONWISE Technology based adapters with buggy SCO support */
Szymon Janc418678b2016-09-01 17:22:37 +0200312 { USB_DEVICE(0x0e5e, 0x6622),
313 .driver_info = BTUSB_BROKEN_ISOC | BTUSB_CW6622},
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200314
Marcel Holtmann4fcef8e2015-01-01 17:34:37 -0800315 /* Roper Class 1 Bluetooth Dongle (Silicon Wave based) */
Aleksei Volkov2eeac872015-06-08 12:02:10 +0300316 { USB_DEVICE(0x1310, 0x0001), .driver_info = BTUSB_SWAVE },
Marcel Holtmann4fcef8e2015-01-01 17:34:37 -0800317
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200318 /* Digianswer devices */
319 { USB_DEVICE(0x08fd, 0x0001), .driver_info = BTUSB_DIGIANSWER },
320 { USB_DEVICE(0x08fd, 0x0002), .driver_info = BTUSB_IGNORE },
321
322 /* CSR BlueCore Bluetooth Sniffer */
Marcel Holtmann4f64fa82014-07-07 00:12:04 +0200323 { USB_DEVICE(0x0a12, 0x0002),
324 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200325
326 /* Frontline ComProbe Bluetooth Sniffer */
Marcel Holtmann4f64fa82014-07-07 00:12:04 +0200327 { USB_DEVICE(0x16d3, 0x0002),
328 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200329
Marcel Holtmanncb1ee892015-01-28 19:41:42 -0800330 /* Marvell Bluetooth devices */
331 { USB_DEVICE(0x1286, 0x2044), .driver_info = BTUSB_MARVELL },
332 { USB_DEVICE(0x1286, 0x2046), .driver_info = BTUSB_MARVELL },
Amitkumar Karwar1165df02016-09-28 16:18:35 +0530333 { USB_DEVICE(0x1286, 0x204e), .driver_info = BTUSB_MARVELL },
Marcel Holtmanncb1ee892015-01-28 19:41:42 -0800334
Marcel Holtmannd0ac9eb2015-01-28 19:41:43 -0800335 /* Intel Bluetooth devices */
Marcel Holtmannde766142017-03-06 15:38:28 -0800336 { USB_DEVICE(0x8087, 0x0025), .driver_info = BTUSB_INTEL_NEW },
Marcel Holtmann407550f2015-02-22 15:41:18 -0800337 { USB_DEVICE(0x8087, 0x07da), .driver_info = BTUSB_CSR },
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -0700338 { USB_DEVICE(0x8087, 0x07dc), .driver_info = BTUSB_INTEL },
Tedd Ho-Jeong Anef4e5e42013-11-12 13:10:58 -0800339 { USB_DEVICE(0x8087, 0x0a2a), .driver_info = BTUSB_INTEL },
Marcel Holtmanncda0dd72015-01-26 21:33:48 -0800340 { USB_DEVICE(0x8087, 0x0a2b), .driver_info = BTUSB_INTEL_NEW },
Tedd Ho-Jeong An439e65d2016-06-20 13:43:40 -0700341 { USB_DEVICE(0x8087, 0x0aa7), .driver_info = BTUSB_INTEL },
Tedd Ho-Jeong An86a61292017-05-01 13:35:12 -0700342 { USB_DEVICE(0x8087, 0x0aaa), .driver_info = BTUSB_INTEL_NEW },
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -0700343
Marcel Holtmannd0ac9eb2015-01-28 19:41:43 -0800344 /* Other Intel Bluetooth devices */
345 { USB_VENDOR_AND_INTERFACE_INFO(0x8087, 0xe0, 0x01, 0x01),
346 .driver_info = BTUSB_IGNORE },
Amitkumar Karwarae8df492014-07-18 14:47:06 -0700347
Daniel Drakea2698a92015-04-16 14:09:55 -0600348 /* Realtek Bluetooth devices */
349 { USB_VENDOR_AND_INTERFACE_INFO(0x0bda, 0xe0, 0x01, 0x01),
350 .driver_info = BTUSB_REALTEK },
351
352 /* Additional Realtek 8723AE Bluetooth devices */
353 { USB_DEVICE(0x0930, 0x021d), .driver_info = BTUSB_REALTEK },
354 { USB_DEVICE(0x13d3, 0x3394), .driver_info = BTUSB_REALTEK },
355
356 /* Additional Realtek 8723BE Bluetooth devices */
357 { USB_DEVICE(0x0489, 0xe085), .driver_info = BTUSB_REALTEK },
358 { USB_DEVICE(0x0489, 0xe08b), .driver_info = BTUSB_REALTEK },
359 { USB_DEVICE(0x13d3, 0x3410), .driver_info = BTUSB_REALTEK },
360 { USB_DEVICE(0x13d3, 0x3416), .driver_info = BTUSB_REALTEK },
361 { USB_DEVICE(0x13d3, 0x3459), .driver_info = BTUSB_REALTEK },
362
363 /* Additional Realtek 8821AE Bluetooth devices */
364 { USB_DEVICE(0x0b05, 0x17dc), .driver_info = BTUSB_REALTEK },
365 { USB_DEVICE(0x13d3, 0x3414), .driver_info = BTUSB_REALTEK },
366 { USB_DEVICE(0x13d3, 0x3458), .driver_info = BTUSB_REALTEK },
367 { USB_DEVICE(0x13d3, 0x3461), .driver_info = BTUSB_REALTEK },
368 { USB_DEVICE(0x13d3, 0x3462), .driver_info = BTUSB_REALTEK },
369
Peter Poklop4481c072015-08-15 20:47:09 +0200370 /* Silicon Wave based devices */
371 { USB_DEVICE(0x0c10, 0x0000), .driver_info = BTUSB_SWAVE },
372
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200373 { } /* Terminating entry */
374};
375
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200376#define BTUSB_MAX_ISOC_FRAMES 10
377
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200378#define BTUSB_INTR_RUNNING 0
379#define BTUSB_BULK_RUNNING 1
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200380#define BTUSB_ISOC_RUNNING 2
Oliver Neukum7bee5492009-08-24 23:44:59 +0200381#define BTUSB_SUSPENDING 3
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -0300382#define BTUSB_DID_ISO_RESUME 4
Marcel Holtmanncda0dd72015-01-26 21:33:48 -0800383#define BTUSB_BOOTLOADER 5
384#define BTUSB_DOWNLOADING 6
Marcel Holtmannce6bb922015-01-28 01:58:40 -0800385#define BTUSB_FIRMWARE_LOADED 7
Marcel Holtmanncda0dd72015-01-26 21:33:48 -0800386#define BTUSB_FIRMWARE_FAILED 8
Marcel Holtmannce6bb922015-01-28 01:58:40 -0800387#define BTUSB_BOOTING 9
Daniel Drake04b8c812015-05-21 08:23:50 -0600388#define BTUSB_RESET_RESUME 10
Marcel Holtmann9d08f502015-10-08 20:23:08 +0200389#define BTUSB_DIAG_RUNNING 11
Rajat Jainfd913ef2017-02-01 14:24:09 -0800390#define BTUSB_OOB_WAKE_ENABLED 12
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200391
392struct btusb_data {
393 struct hci_dev *hdev;
394 struct usb_device *udev;
Marcel Holtmann5fbcd262008-09-23 00:16:36 +0200395 struct usb_interface *intf;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200396 struct usb_interface *isoc;
Marcel Holtmann9d08f502015-10-08 20:23:08 +0200397 struct usb_interface *diag;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200398
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200399 unsigned long flags;
400
401 struct work_struct work;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200402 struct work_struct waker;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200403
Marcel Holtmann803b5832014-09-16 08:00:29 +0200404 struct usb_anchor deferred;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200405 struct usb_anchor tx_anchor;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200406 int tx_in_flight;
407 spinlock_t txlock;
408
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200409 struct usb_anchor intr_anchor;
410 struct usb_anchor bulk_anchor;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200411 struct usb_anchor isoc_anchor;
Marcel Holtmann9d08f502015-10-08 20:23:08 +0200412 struct usb_anchor diag_anchor;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200413 spinlock_t rxlock;
414
415 struct sk_buff *evt_skb;
416 struct sk_buff *acl_skb;
417 struct sk_buff *sco_skb;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200418
419 struct usb_endpoint_descriptor *intr_ep;
420 struct usb_endpoint_descriptor *bulk_tx_ep;
421 struct usb_endpoint_descriptor *bulk_rx_ep;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200422 struct usb_endpoint_descriptor *isoc_tx_ep;
423 struct usb_endpoint_descriptor *isoc_rx_ep;
Marcel Holtmann9d08f502015-10-08 20:23:08 +0200424 struct usb_endpoint_descriptor *diag_tx_ep;
425 struct usb_endpoint_descriptor *diag_rx_ep;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200426
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100427 __u8 cmdreq_type;
Marcel Holtmann893ba542015-01-28 20:27:34 -0800428 __u8 cmdreq;
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100429
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100430 unsigned int sco_num;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200431 int isoc_altsetting;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +0100432 int suspend_count;
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +0100433
Marcel Holtmann97307f52015-01-12 13:51:10 -0800434 int (*recv_event)(struct hci_dev *hdev, struct sk_buff *skb);
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +0100435 int (*recv_bulk)(struct btusb_data *data, void *buffer, int count);
Kim, Ben Young Taeace31982015-02-15 23:06:14 +0000436
437 int (*setup_on_usb)(struct hci_dev *hdev);
Rajat Jainfd913ef2017-02-01 14:24:09 -0800438
439 int oob_wake_irq; /* irq for out-of-band wake-on-bt */
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200440};
441
Marcel Holtmann803b5832014-09-16 08:00:29 +0200442static inline void btusb_free_frags(struct btusb_data *data)
443{
444 unsigned long flags;
445
446 spin_lock_irqsave(&data->rxlock, flags);
447
448 kfree_skb(data->evt_skb);
449 data->evt_skb = NULL;
450
451 kfree_skb(data->acl_skb);
452 data->acl_skb = NULL;
453
454 kfree_skb(data->sco_skb);
455 data->sco_skb = NULL;
456
457 spin_unlock_irqrestore(&data->rxlock, flags);
458}
459
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200460static int btusb_recv_intr(struct btusb_data *data, void *buffer, int count)
461{
Marcel Holtmann803b5832014-09-16 08:00:29 +0200462 struct sk_buff *skb;
463 int err = 0;
464
465 spin_lock(&data->rxlock);
466 skb = data->evt_skb;
467
468 while (count) {
469 int len;
470
471 if (!skb) {
472 skb = bt_skb_alloc(HCI_MAX_EVENT_SIZE, GFP_ATOMIC);
473 if (!skb) {
474 err = -ENOMEM;
475 break;
476 }
477
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100478 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
479 hci_skb_expect(skb) = HCI_EVENT_HDR_SIZE;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200480 }
481
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100482 len = min_t(uint, hci_skb_expect(skb), count);
Johannes Berg59ae1d12017-06-16 14:29:20 +0200483 skb_put_data(skb, buffer, len);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200484
485 count -= len;
486 buffer += len;
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100487 hci_skb_expect(skb) -= len;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200488
489 if (skb->len == HCI_EVENT_HDR_SIZE) {
490 /* Complete event header */
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100491 hci_skb_expect(skb) = hci_event_hdr(skb)->plen;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200492
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100493 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
Marcel Holtmann803b5832014-09-16 08:00:29 +0200494 kfree_skb(skb);
495 skb = NULL;
496
497 err = -EILSEQ;
498 break;
499 }
500 }
501
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100502 if (!hci_skb_expect(skb)) {
Marcel Holtmann803b5832014-09-16 08:00:29 +0200503 /* Complete frame */
Marcel Holtmann97307f52015-01-12 13:51:10 -0800504 data->recv_event(data->hdev, skb);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200505 skb = NULL;
506 }
507 }
508
509 data->evt_skb = skb;
510 spin_unlock(&data->rxlock);
511
512 return err;
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200513}
514
515static int btusb_recv_bulk(struct btusb_data *data, void *buffer, int count)
516{
Marcel Holtmann803b5832014-09-16 08:00:29 +0200517 struct sk_buff *skb;
518 int err = 0;
519
520 spin_lock(&data->rxlock);
521 skb = data->acl_skb;
522
523 while (count) {
524 int len;
525
526 if (!skb) {
527 skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
528 if (!skb) {
529 err = -ENOMEM;
530 break;
531 }
532
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100533 hci_skb_pkt_type(skb) = HCI_ACLDATA_PKT;
534 hci_skb_expect(skb) = HCI_ACL_HDR_SIZE;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200535 }
536
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100537 len = min_t(uint, hci_skb_expect(skb), count);
Johannes Berg59ae1d12017-06-16 14:29:20 +0200538 skb_put_data(skb, buffer, len);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200539
540 count -= len;
541 buffer += len;
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100542 hci_skb_expect(skb) -= len;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200543
544 if (skb->len == HCI_ACL_HDR_SIZE) {
545 __le16 dlen = hci_acl_hdr(skb)->dlen;
546
547 /* Complete ACL header */
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100548 hci_skb_expect(skb) = __le16_to_cpu(dlen);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200549
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100550 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
Marcel Holtmann803b5832014-09-16 08:00:29 +0200551 kfree_skb(skb);
552 skb = NULL;
553
554 err = -EILSEQ;
555 break;
556 }
557 }
558
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100559 if (!hci_skb_expect(skb)) {
Marcel Holtmann803b5832014-09-16 08:00:29 +0200560 /* Complete frame */
561 hci_recv_frame(data->hdev, skb);
562 skb = NULL;
563 }
564 }
565
566 data->acl_skb = skb;
567 spin_unlock(&data->rxlock);
568
569 return err;
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200570}
571
572static int btusb_recv_isoc(struct btusb_data *data, void *buffer, int count)
573{
Marcel Holtmann803b5832014-09-16 08:00:29 +0200574 struct sk_buff *skb;
575 int err = 0;
576
577 spin_lock(&data->rxlock);
578 skb = data->sco_skb;
579
580 while (count) {
581 int len;
582
583 if (!skb) {
584 skb = bt_skb_alloc(HCI_MAX_SCO_SIZE, GFP_ATOMIC);
585 if (!skb) {
586 err = -ENOMEM;
587 break;
588 }
589
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100590 hci_skb_pkt_type(skb) = HCI_SCODATA_PKT;
591 hci_skb_expect(skb) = HCI_SCO_HDR_SIZE;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200592 }
593
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100594 len = min_t(uint, hci_skb_expect(skb), count);
Johannes Berg59ae1d12017-06-16 14:29:20 +0200595 skb_put_data(skb, buffer, len);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200596
597 count -= len;
598 buffer += len;
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100599 hci_skb_expect(skb) -= len;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200600
601 if (skb->len == HCI_SCO_HDR_SIZE) {
602 /* Complete SCO header */
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100603 hci_skb_expect(skb) = hci_sco_hdr(skb)->dlen;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200604
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100605 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
Marcel Holtmann803b5832014-09-16 08:00:29 +0200606 kfree_skb(skb);
607 skb = NULL;
608
609 err = -EILSEQ;
610 break;
611 }
612 }
613
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100614 if (!hci_skb_expect(skb)) {
Marcel Holtmann803b5832014-09-16 08:00:29 +0200615 /* Complete frame */
616 hci_recv_frame(data->hdev, skb);
617 skb = NULL;
618 }
619 }
620
621 data->sco_skb = skb;
622 spin_unlock(&data->rxlock);
623
624 return err;
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200625}
626
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200627static void btusb_intr_complete(struct urb *urb)
628{
629 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +0100630 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200631 int err;
632
Marcel Holtmann89e75332014-09-16 04:44:50 +0200633 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
634 urb->actual_length);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200635
636 if (!test_bit(HCI_RUNNING, &hdev->flags))
637 return;
638
639 if (urb->status == 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200640 hdev->stat.byte_rx += urb->actual_length;
641
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200642 if (btusb_recv_intr(data, urb->transfer_buffer,
643 urb->actual_length) < 0) {
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200644 BT_ERR("%s corrupted event packet", hdev->name);
645 hdev->stat.err_rx++;
646 }
Champion Chen85560c42014-09-06 14:06:08 -0500647 } else if (urb->status == -ENOENT) {
648 /* Avoid suspend failed when usb_kill_urb */
649 return;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200650 }
651
652 if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
653 return;
654
Oliver Neukum7bee5492009-08-24 23:44:59 +0200655 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200656 usb_anchor_urb(urb, &data->intr_anchor);
657
658 err = usb_submit_urb(urb, GFP_ATOMIC);
659 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200660 /* -EPERM: urb is being killed;
Derek Robsond98422c2017-07-22 13:47:07 +1200661 * -ENODEV: device got disconnected
662 */
Paul Bolle4935f1c2011-08-09 17:16:28 +0200663 if (err != -EPERM && err != -ENODEV)
Stefan Seyfried61faddf2010-11-30 21:49:08 +0100664 BT_ERR("%s urb %p failed to resubmit (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200665 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200666 usb_unanchor_urb(urb);
667 }
668}
669
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100670static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200671{
David Herrmann155961e2012-02-09 21:58:32 +0100672 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200673 struct urb *urb;
674 unsigned char *buf;
675 unsigned int pipe;
676 int err, size;
677
678 BT_DBG("%s", hdev->name);
679
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200680 if (!data->intr_ep)
681 return -ENODEV;
682
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100683 urb = usb_alloc_urb(0, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200684 if (!urb)
685 return -ENOMEM;
686
687 size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
688
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100689 buf = kmalloc(size, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200690 if (!buf) {
691 usb_free_urb(urb);
692 return -ENOMEM;
693 }
694
695 pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
696
697 usb_fill_int_urb(urb, data->udev, pipe, buf, size,
Marcel Holtmann89e75332014-09-16 04:44:50 +0200698 btusb_intr_complete, hdev, data->intr_ep->bInterval);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200699
700 urb->transfer_flags |= URB_FREE_BUFFER;
701
702 usb_anchor_urb(urb, &data->intr_anchor);
703
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100704 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200705 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +0200706 if (err != -EPERM && err != -ENODEV)
707 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200708 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200709 usb_unanchor_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200710 }
711
712 usb_free_urb(urb);
713
714 return err;
715}
716
717static void btusb_bulk_complete(struct urb *urb)
718{
719 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +0100720 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200721 int err;
722
Marcel Holtmann89e75332014-09-16 04:44:50 +0200723 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
724 urb->actual_length);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200725
726 if (!test_bit(HCI_RUNNING, &hdev->flags))
727 return;
728
729 if (urb->status == 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200730 hdev->stat.byte_rx += urb->actual_length;
731
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +0100732 if (data->recv_bulk(data, urb->transfer_buffer,
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200733 urb->actual_length) < 0) {
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200734 BT_ERR("%s corrupted ACL packet", hdev->name);
735 hdev->stat.err_rx++;
736 }
Champion Chen85560c42014-09-06 14:06:08 -0500737 } else if (urb->status == -ENOENT) {
738 /* Avoid suspend failed when usb_kill_urb */
739 return;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200740 }
741
742 if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
743 return;
744
745 usb_anchor_urb(urb, &data->bulk_anchor);
Oliver Neukum652fd782009-12-16 19:23:43 +0100746 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200747
748 err = usb_submit_urb(urb, GFP_ATOMIC);
749 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200750 /* -EPERM: urb is being killed;
Derek Robsond98422c2017-07-22 13:47:07 +1200751 * -ENODEV: device got disconnected
752 */
Paul Bolle4935f1c2011-08-09 17:16:28 +0200753 if (err != -EPERM && err != -ENODEV)
Stefan Seyfried61faddf2010-11-30 21:49:08 +0100754 BT_ERR("%s urb %p failed to resubmit (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200755 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200756 usb_unanchor_urb(urb);
757 }
758}
759
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100760static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200761{
David Herrmann155961e2012-02-09 21:58:32 +0100762 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200763 struct urb *urb;
764 unsigned char *buf;
765 unsigned int pipe;
Vikram Kandukuri290ba202009-07-02 14:31:59 +0530766 int err, size = HCI_MAX_FRAME_SIZE;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200767
768 BT_DBG("%s", hdev->name);
769
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200770 if (!data->bulk_rx_ep)
771 return -ENODEV;
772
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100773 urb = usb_alloc_urb(0, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200774 if (!urb)
775 return -ENOMEM;
776
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100777 buf = kmalloc(size, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200778 if (!buf) {
779 usb_free_urb(urb);
780 return -ENOMEM;
781 }
782
783 pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
784
Marcel Holtmann89e75332014-09-16 04:44:50 +0200785 usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
786 btusb_bulk_complete, hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200787
788 urb->transfer_flags |= URB_FREE_BUFFER;
789
Oliver Neukum7bee5492009-08-24 23:44:59 +0200790 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200791 usb_anchor_urb(urb, &data->bulk_anchor);
792
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100793 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200794 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +0200795 if (err != -EPERM && err != -ENODEV)
796 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200797 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200798 usb_unanchor_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200799 }
800
801 usb_free_urb(urb);
802
803 return err;
804}
805
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200806static void btusb_isoc_complete(struct urb *urb)
807{
808 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +0100809 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200810 int i, err;
811
Marcel Holtmann89e75332014-09-16 04:44:50 +0200812 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
813 urb->actual_length);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200814
815 if (!test_bit(HCI_RUNNING, &hdev->flags))
816 return;
817
818 if (urb->status == 0) {
819 for (i = 0; i < urb->number_of_packets; i++) {
820 unsigned int offset = urb->iso_frame_desc[i].offset;
821 unsigned int length = urb->iso_frame_desc[i].actual_length;
822
823 if (urb->iso_frame_desc[i].status)
824 continue;
825
826 hdev->stat.byte_rx += length;
827
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200828 if (btusb_recv_isoc(data, urb->transfer_buffer + offset,
829 length) < 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200830 BT_ERR("%s corrupted SCO packet", hdev->name);
831 hdev->stat.err_rx++;
832 }
833 }
Champion Chen85560c42014-09-06 14:06:08 -0500834 } else if (urb->status == -ENOENT) {
835 /* Avoid suspend failed when usb_kill_urb */
836 return;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200837 }
838
839 if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags))
840 return;
841
842 usb_anchor_urb(urb, &data->isoc_anchor);
843
844 err = usb_submit_urb(urb, GFP_ATOMIC);
845 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200846 /* -EPERM: urb is being killed;
Derek Robsond98422c2017-07-22 13:47:07 +1200847 * -ENODEV: device got disconnected
848 */
Paul Bolle4935f1c2011-08-09 17:16:28 +0200849 if (err != -EPERM && err != -ENODEV)
Stefan Seyfried61faddf2010-11-30 21:49:08 +0100850 BT_ERR("%s urb %p failed to resubmit (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200851 hdev->name, urb, -err);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200852 usb_unanchor_urb(urb);
853 }
854}
855
Jesper Juhl42b16b32011-01-17 00:09:38 +0100856static inline void __fill_isoc_descriptor(struct urb *urb, int len, int mtu)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200857{
858 int i, offset = 0;
859
860 BT_DBG("len %d mtu %d", len, mtu);
861
862 for (i = 0; i < BTUSB_MAX_ISOC_FRAMES && len >= mtu;
863 i++, offset += mtu, len -= mtu) {
864 urb->iso_frame_desc[i].offset = offset;
865 urb->iso_frame_desc[i].length = mtu;
866 }
867
868 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
869 urb->iso_frame_desc[i].offset = offset;
870 urb->iso_frame_desc[i].length = len;
871 i++;
872 }
873
874 urb->number_of_packets = i;
875}
876
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100877static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200878{
David Herrmann155961e2012-02-09 21:58:32 +0100879 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200880 struct urb *urb;
881 unsigned char *buf;
882 unsigned int pipe;
883 int err, size;
884
885 BT_DBG("%s", hdev->name);
886
887 if (!data->isoc_rx_ep)
888 return -ENODEV;
889
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100890 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200891 if (!urb)
892 return -ENOMEM;
893
894 size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) *
895 BTUSB_MAX_ISOC_FRAMES;
896
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100897 buf = kmalloc(size, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200898 if (!buf) {
899 usb_free_urb(urb);
900 return -ENOMEM;
901 }
902
903 pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress);
904
Bing Zhaofa0fb932011-12-20 18:19:00 -0800905 usb_fill_int_urb(urb, data->udev, pipe, buf, size, btusb_isoc_complete,
Marcel Holtmann89e75332014-09-16 04:44:50 +0200906 hdev, data->isoc_rx_ep->bInterval);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200907
Marcel Holtmann89e75332014-09-16 04:44:50 +0200908 urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200909
910 __fill_isoc_descriptor(urb, size,
Marcel Holtmann89e75332014-09-16 04:44:50 +0200911 le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize));
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200912
913 usb_anchor_urb(urb, &data->isoc_anchor);
914
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100915 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200916 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +0200917 if (err != -EPERM && err != -ENODEV)
918 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200919 hdev->name, urb, -err);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200920 usb_unanchor_urb(urb);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200921 }
922
923 usb_free_urb(urb);
924
925 return err;
926}
927
Marcel Holtmann9d08f502015-10-08 20:23:08 +0200928static void btusb_diag_complete(struct urb *urb)
929{
930 struct hci_dev *hdev = urb->context;
931 struct btusb_data *data = hci_get_drvdata(hdev);
932 int err;
933
934 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
935 urb->actual_length);
936
937 if (urb->status == 0) {
938 struct sk_buff *skb;
939
940 skb = bt_skb_alloc(urb->actual_length, GFP_ATOMIC);
941 if (skb) {
Johannes Berg59ae1d12017-06-16 14:29:20 +0200942 skb_put_data(skb, urb->transfer_buffer,
943 urb->actual_length);
Marcel Holtmann9d08f502015-10-08 20:23:08 +0200944 hci_recv_diag(hdev, skb);
945 }
946 } else if (urb->status == -ENOENT) {
947 /* Avoid suspend failed when usb_kill_urb */
948 return;
949 }
950
951 if (!test_bit(BTUSB_DIAG_RUNNING, &data->flags))
952 return;
953
954 usb_anchor_urb(urb, &data->diag_anchor);
955 usb_mark_last_busy(data->udev);
956
957 err = usb_submit_urb(urb, GFP_ATOMIC);
958 if (err < 0) {
959 /* -EPERM: urb is being killed;
Derek Robsond98422c2017-07-22 13:47:07 +1200960 * -ENODEV: device got disconnected
961 */
Marcel Holtmann9d08f502015-10-08 20:23:08 +0200962 if (err != -EPERM && err != -ENODEV)
963 BT_ERR("%s urb %p failed to resubmit (%d)",
964 hdev->name, urb, -err);
965 usb_unanchor_urb(urb);
966 }
967}
968
969static int btusb_submit_diag_urb(struct hci_dev *hdev, gfp_t mem_flags)
970{
971 struct btusb_data *data = hci_get_drvdata(hdev);
972 struct urb *urb;
973 unsigned char *buf;
974 unsigned int pipe;
975 int err, size = HCI_MAX_FRAME_SIZE;
976
977 BT_DBG("%s", hdev->name);
978
979 if (!data->diag_rx_ep)
980 return -ENODEV;
981
982 urb = usb_alloc_urb(0, mem_flags);
983 if (!urb)
984 return -ENOMEM;
985
986 buf = kmalloc(size, mem_flags);
987 if (!buf) {
988 usb_free_urb(urb);
989 return -ENOMEM;
990 }
991
992 pipe = usb_rcvbulkpipe(data->udev, data->diag_rx_ep->bEndpointAddress);
993
994 usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
995 btusb_diag_complete, hdev);
996
997 urb->transfer_flags |= URB_FREE_BUFFER;
998
999 usb_mark_last_busy(data->udev);
1000 usb_anchor_urb(urb, &data->diag_anchor);
1001
1002 err = usb_submit_urb(urb, mem_flags);
1003 if (err < 0) {
1004 if (err != -EPERM && err != -ENODEV)
1005 BT_ERR("%s urb %p submission failed (%d)",
1006 hdev->name, urb, -err);
1007 usb_unanchor_urb(urb);
1008 }
1009
1010 usb_free_urb(urb);
1011
1012 return err;
1013}
1014
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001015static void btusb_tx_complete(struct urb *urb)
1016{
1017 struct sk_buff *skb = urb->context;
Marcel Holtmann89e75332014-09-16 04:44:50 +02001018 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
David Herrmann155961e2012-02-09 21:58:32 +01001019 struct btusb_data *data = hci_get_drvdata(hdev);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001020
Marcel Holtmann89e75332014-09-16 04:44:50 +02001021 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1022 urb->actual_length);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001023
1024 if (!test_bit(HCI_RUNNING, &hdev->flags))
1025 goto done;
1026
1027 if (!urb->status)
1028 hdev->stat.byte_tx += urb->transfer_buffer_length;
1029 else
1030 hdev->stat.err_tx++;
1031
1032done:
1033 spin_lock(&data->txlock);
1034 data->tx_in_flight--;
1035 spin_unlock(&data->txlock);
1036
1037 kfree(urb->setup_packet);
1038
1039 kfree_skb(skb);
1040}
1041
1042static void btusb_isoc_tx_complete(struct urb *urb)
1043{
1044 struct sk_buff *skb = urb->context;
Marcel Holtmann89e75332014-09-16 04:44:50 +02001045 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001046
Marcel Holtmann89e75332014-09-16 04:44:50 +02001047 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1048 urb->actual_length);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001049
1050 if (!test_bit(HCI_RUNNING, &hdev->flags))
1051 goto done;
1052
1053 if (!urb->status)
1054 hdev->stat.byte_tx += urb->transfer_buffer_length;
1055 else
1056 hdev->stat.err_tx++;
1057
1058done:
1059 kfree(urb->setup_packet);
1060
1061 kfree_skb(skb);
1062}
1063
1064static int btusb_open(struct hci_dev *hdev)
1065{
David Herrmann155961e2012-02-09 21:58:32 +01001066 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001067 int err;
1068
1069 BT_DBG("%s", hdev->name);
1070
Ethan Hsiehc7e163f2016-10-07 12:06:42 +08001071 err = usb_autopm_get_interface(data->intf);
1072 if (err < 0)
1073 return err;
1074
Kim, Ben Young Taeace31982015-02-15 23:06:14 +00001075 /* Patching USB firmware files prior to starting any URBs of HCI path
1076 * It is more safe to use USB bulk channel for downloading USB patch
1077 */
1078 if (data->setup_on_usb) {
1079 err = data->setup_on_usb(hdev);
Marcel Holtmanneb500422015-04-16 23:15:50 +02001080 if (err < 0)
Kim, Ben Young Taeace31982015-02-15 23:06:14 +00001081 return err;
1082 }
1083
Oliver Neukum7bee5492009-08-24 23:44:59 +02001084 data->intf->needs_remote_wakeup = 1;
1085
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001086 if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +02001087 goto done;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001088
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001089 err = btusb_submit_intr_urb(hdev, GFP_KERNEL);
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001090 if (err < 0)
1091 goto failed;
1092
1093 err = btusb_submit_bulk_urb(hdev, GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001094 if (err < 0) {
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001095 usb_kill_anchored_urbs(&data->intr_anchor);
1096 goto failed;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001097 }
1098
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001099 set_bit(BTUSB_BULK_RUNNING, &data->flags);
1100 btusb_submit_bulk_urb(hdev, GFP_KERNEL);
1101
Marcel Holtmann9d08f502015-10-08 20:23:08 +02001102 if (data->diag) {
1103 if (!btusb_submit_diag_urb(hdev, GFP_KERNEL))
1104 set_bit(BTUSB_DIAG_RUNNING, &data->flags);
1105 }
1106
Oliver Neukum7bee5492009-08-24 23:44:59 +02001107done:
1108 usb_autopm_put_interface(data->intf);
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001109 return 0;
1110
1111failed:
1112 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001113 usb_autopm_put_interface(data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001114 return err;
1115}
1116
Oliver Neukum7bee5492009-08-24 23:44:59 +02001117static void btusb_stop_traffic(struct btusb_data *data)
1118{
1119 usb_kill_anchored_urbs(&data->intr_anchor);
1120 usb_kill_anchored_urbs(&data->bulk_anchor);
1121 usb_kill_anchored_urbs(&data->isoc_anchor);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02001122 usb_kill_anchored_urbs(&data->diag_anchor);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001123}
1124
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001125static int btusb_close(struct hci_dev *hdev)
1126{
David Herrmann155961e2012-02-09 21:58:32 +01001127 struct btusb_data *data = hci_get_drvdata(hdev);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001128 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001129
1130 BT_DBG("%s", hdev->name);
1131
Marcel Holtmanne8c3c3d2008-09-23 00:16:36 +02001132 cancel_work_sync(&data->work);
Linus Torvalds404291a2009-11-11 13:32:29 -08001133 cancel_work_sync(&data->waker);
Marcel Holtmanne8c3c3d2008-09-23 00:16:36 +02001134
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001135 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001136 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001137 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02001138 clear_bit(BTUSB_DIAG_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001139
1140 btusb_stop_traffic(data);
Marcel Holtmann803b5832014-09-16 08:00:29 +02001141 btusb_free_frags(data);
1142
Oliver Neukum7bee5492009-08-24 23:44:59 +02001143 err = usb_autopm_get_interface(data->intf);
1144 if (err < 0)
Oliver Neukum7b8e2c12009-11-13 14:26:23 +01001145 goto failed;
Oliver Neukum7bee5492009-08-24 23:44:59 +02001146
1147 data->intf->needs_remote_wakeup = 0;
1148 usb_autopm_put_interface(data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001149
Oliver Neukum7b8e2c12009-11-13 14:26:23 +01001150failed:
1151 usb_scuttle_anchored_urbs(&data->deferred);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001152 return 0;
1153}
1154
1155static int btusb_flush(struct hci_dev *hdev)
1156{
David Herrmann155961e2012-02-09 21:58:32 +01001157 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001158
1159 BT_DBG("%s", hdev->name);
1160
1161 usb_kill_anchored_urbs(&data->tx_anchor);
Marcel Holtmann803b5832014-09-16 08:00:29 +02001162 btusb_free_frags(data);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001163
1164 return 0;
1165}
1166
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001167static struct urb *alloc_ctrl_urb(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001168{
David Herrmann155961e2012-02-09 21:58:32 +01001169 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001170 struct usb_ctrlrequest *dr;
1171 struct urb *urb;
1172 unsigned int pipe;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001173
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001174 urb = usb_alloc_urb(0, GFP_KERNEL);
1175 if (!urb)
1176 return ERR_PTR(-ENOMEM);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001177
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001178 dr = kmalloc(sizeof(*dr), GFP_KERNEL);
1179 if (!dr) {
1180 usb_free_urb(urb);
1181 return ERR_PTR(-ENOMEM);
1182 }
1183
1184 dr->bRequestType = data->cmdreq_type;
Marcel Holtmann893ba542015-01-28 20:27:34 -08001185 dr->bRequest = data->cmdreq;
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001186 dr->wIndex = 0;
1187 dr->wValue = 0;
1188 dr->wLength = __cpu_to_le16(skb->len);
1189
1190 pipe = usb_sndctrlpipe(data->udev, 0x00);
1191
Marcel Holtmann89e75332014-09-16 04:44:50 +02001192 usb_fill_control_urb(urb, data->udev, pipe, (void *)dr,
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001193 skb->data, skb->len, btusb_tx_complete, skb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001194
Marcel Holtmann89e75332014-09-16 04:44:50 +02001195 skb->dev = (void *)hdev;
Marcel Holtmann7bd8f092013-10-11 06:19:18 -07001196
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001197 return urb;
1198}
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001199
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001200static struct urb *alloc_bulk_urb(struct hci_dev *hdev, struct sk_buff *skb)
1201{
1202 struct btusb_data *data = hci_get_drvdata(hdev);
1203 struct urb *urb;
1204 unsigned int pipe;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001205
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001206 if (!data->bulk_tx_ep)
1207 return ERR_PTR(-ENODEV);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001208
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001209 urb = usb_alloc_urb(0, GFP_KERNEL);
1210 if (!urb)
1211 return ERR_PTR(-ENOMEM);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001212
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001213 pipe = usb_sndbulkpipe(data->udev, data->bulk_tx_ep->bEndpointAddress);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001214
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001215 usb_fill_bulk_urb(urb, data->udev, pipe,
1216 skb->data, skb->len, btusb_tx_complete, skb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001217
Marcel Holtmann89e75332014-09-16 04:44:50 +02001218 skb->dev = (void *)hdev;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001219
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001220 return urb;
1221}
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001222
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001223static struct urb *alloc_isoc_urb(struct hci_dev *hdev, struct sk_buff *skb)
1224{
1225 struct btusb_data *data = hci_get_drvdata(hdev);
1226 struct urb *urb;
1227 unsigned int pipe;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001228
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001229 if (!data->isoc_tx_ep)
1230 return ERR_PTR(-ENODEV);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001231
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001232 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_KERNEL);
1233 if (!urb)
1234 return ERR_PTR(-ENOMEM);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001235
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001236 pipe = usb_sndisocpipe(data->udev, data->isoc_tx_ep->bEndpointAddress);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001237
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001238 usb_fill_int_urb(urb, data->udev, pipe,
1239 skb->data, skb->len, btusb_isoc_tx_complete,
1240 skb, data->isoc_tx_ep->bInterval);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001241
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001242 urb->transfer_flags = URB_ISO_ASAP;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001243
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001244 __fill_isoc_descriptor(urb, skb->len,
1245 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001246
Marcel Holtmann89e75332014-09-16 04:44:50 +02001247 skb->dev = (void *)hdev;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001248
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001249 return urb;
1250}
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001251
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001252static int submit_tx_urb(struct hci_dev *hdev, struct urb *urb)
1253{
1254 struct btusb_data *data = hci_get_drvdata(hdev);
1255 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001256
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001257 usb_anchor_urb(urb, &data->tx_anchor);
1258
Johan Hedberge9753ef2014-09-14 08:49:34 +03001259 err = usb_submit_urb(urb, GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001260 if (err < 0) {
Paul Bolle5a9b80e2011-10-09 12:12:16 +02001261 if (err != -EPERM && err != -ENODEV)
1262 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +02001263 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001264 kfree(urb->setup_packet);
1265 usb_unanchor_urb(urb);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001266 } else {
1267 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001268 }
1269
Cong Wang54a8a792011-11-22 09:32:57 +08001270 usb_free_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001271 return err;
1272}
1273
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001274static int submit_or_queue_tx_urb(struct hci_dev *hdev, struct urb *urb)
1275{
1276 struct btusb_data *data = hci_get_drvdata(hdev);
1277 unsigned long flags;
1278 bool suspending;
1279
1280 spin_lock_irqsave(&data->txlock, flags);
1281 suspending = test_bit(BTUSB_SUSPENDING, &data->flags);
1282 if (!suspending)
1283 data->tx_in_flight++;
1284 spin_unlock_irqrestore(&data->txlock, flags);
1285
1286 if (!suspending)
1287 return submit_tx_urb(hdev, urb);
1288
1289 usb_anchor_urb(urb, &data->deferred);
1290 schedule_work(&data->waker);
1291
1292 usb_free_urb(urb);
1293 return 0;
1294}
1295
1296static int btusb_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
1297{
1298 struct urb *urb;
1299
1300 BT_DBG("%s", hdev->name);
1301
Marcel Holtmann618e8bc2015-11-05 07:33:56 +01001302 switch (hci_skb_pkt_type(skb)) {
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001303 case HCI_COMMAND_PKT:
1304 urb = alloc_ctrl_urb(hdev, skb);
1305 if (IS_ERR(urb))
1306 return PTR_ERR(urb);
1307
1308 hdev->stat.cmd_tx++;
1309 return submit_or_queue_tx_urb(hdev, urb);
1310
1311 case HCI_ACLDATA_PKT:
1312 urb = alloc_bulk_urb(hdev, skb);
1313 if (IS_ERR(urb))
1314 return PTR_ERR(urb);
1315
1316 hdev->stat.acl_tx++;
1317 return submit_or_queue_tx_urb(hdev, urb);
1318
1319 case HCI_SCODATA_PKT:
1320 if (hci_conn_num(hdev, SCO_LINK) < 1)
1321 return -ENODEV;
1322
1323 urb = alloc_isoc_urb(hdev, skb);
1324 if (IS_ERR(urb))
1325 return PTR_ERR(urb);
1326
1327 hdev->stat.sco_tx++;
1328 return submit_tx_urb(hdev, urb);
1329 }
1330
1331 return -EILSEQ;
1332}
1333
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001334static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
1335{
David Herrmann155961e2012-02-09 21:58:32 +01001336 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001337
1338 BT_DBG("%s evt %d", hdev->name, evt);
1339
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001340 if (hci_conn_num(hdev, SCO_LINK) != data->sco_num) {
1341 data->sco_num = hci_conn_num(hdev, SCO_LINK);
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001342 schedule_work(&data->work);
Marcel Holtmanna780efa2008-11-30 12:17:12 +01001343 }
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001344}
1345
Jesper Juhl42b16b32011-01-17 00:09:38 +01001346static inline int __set_isoc_interface(struct hci_dev *hdev, int altsetting)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001347{
David Herrmann155961e2012-02-09 21:58:32 +01001348 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001349 struct usb_interface *intf = data->isoc;
1350 struct usb_endpoint_descriptor *ep_desc;
1351 int i, err;
1352
1353 if (!data->isoc)
1354 return -ENODEV;
1355
1356 err = usb_set_interface(data->udev, 1, altsetting);
1357 if (err < 0) {
1358 BT_ERR("%s setting interface failed (%d)", hdev->name, -err);
1359 return err;
1360 }
1361
1362 data->isoc_altsetting = altsetting;
1363
1364 data->isoc_tx_ep = NULL;
1365 data->isoc_rx_ep = NULL;
1366
1367 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
1368 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
1369
1370 if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) {
1371 data->isoc_tx_ep = ep_desc;
1372 continue;
1373 }
1374
1375 if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) {
1376 data->isoc_rx_ep = ep_desc;
1377 continue;
1378 }
1379 }
1380
1381 if (!data->isoc_tx_ep || !data->isoc_rx_ep) {
1382 BT_ERR("%s invalid SCO descriptors", hdev->name);
1383 return -ENODEV;
1384 }
1385
1386 return 0;
1387}
1388
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001389static void btusb_work(struct work_struct *work)
1390{
1391 struct btusb_data *data = container_of(work, struct btusb_data, work);
1392 struct hci_dev *hdev = data->hdev;
Mikel Astizf4001d22012-04-11 08:48:51 +02001393 int new_alts;
Oliver Neukum7bee5492009-08-24 23:44:59 +02001394 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001395
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001396 if (data->sco_num > 0) {
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -03001397 if (!test_bit(BTUSB_DID_ISO_RESUME, &data->flags)) {
Oliver Neukum8efdd0c2011-02-11 13:00:06 +01001398 err = usb_autopm_get_interface(data->isoc ? data->isoc : data->intf);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001399 if (err < 0) {
1400 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1401 usb_kill_anchored_urbs(&data->isoc_anchor);
1402 return;
1403 }
1404
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -03001405 set_bit(BTUSB_DID_ISO_RESUME, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001406 }
Mikel Astizf4001d22012-04-11 08:48:51 +02001407
1408 if (hdev->voice_setting & 0x0020) {
1409 static const int alts[3] = { 2, 4, 5 };
Marcel Holtmann89e75332014-09-16 04:44:50 +02001410
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001411 new_alts = alts[data->sco_num - 1];
Mikel Astizf4001d22012-04-11 08:48:51 +02001412 } else {
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001413 new_alts = data->sco_num;
Mikel Astizf4001d22012-04-11 08:48:51 +02001414 }
1415
1416 if (data->isoc_altsetting != new_alts) {
Kuba Pawlakf6fc86f2015-10-28 15:18:05 +01001417 unsigned long flags;
1418
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001419 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1420 usb_kill_anchored_urbs(&data->isoc_anchor);
1421
Kuba Pawlak8f9d02f2015-09-10 17:07:00 +01001422 /* When isochronous alternate setting needs to be
1423 * changed, because SCO connection has been added
1424 * or removed, a packet fragment may be left in the
1425 * reassembling state. This could lead to wrongly
1426 * assembled fragments.
1427 *
1428 * Clear outstanding fragment when selecting a new
1429 * alternate setting.
1430 */
Kuba Pawlakf6fc86f2015-10-28 15:18:05 +01001431 spin_lock_irqsave(&data->rxlock, flags);
Kuba Pawlak8f9d02f2015-09-10 17:07:00 +01001432 kfree_skb(data->sco_skb);
1433 data->sco_skb = NULL;
Kuba Pawlakf6fc86f2015-10-28 15:18:05 +01001434 spin_unlock_irqrestore(&data->rxlock, flags);
Kuba Pawlak8f9d02f2015-09-10 17:07:00 +01001435
Mikel Astizf4001d22012-04-11 08:48:51 +02001436 if (__set_isoc_interface(hdev, new_alts) < 0)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001437 return;
1438 }
1439
1440 if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001441 if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001442 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1443 else
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001444 btusb_submit_isoc_urb(hdev, GFP_KERNEL);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001445 }
1446 } else {
1447 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1448 usb_kill_anchored_urbs(&data->isoc_anchor);
1449
1450 __set_isoc_interface(hdev, 0);
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -03001451 if (test_and_clear_bit(BTUSB_DID_ISO_RESUME, &data->flags))
Oliver Neukum8efdd0c2011-02-11 13:00:06 +01001452 usb_autopm_put_interface(data->isoc ? data->isoc : data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001453 }
1454}
1455
Oliver Neukum7bee5492009-08-24 23:44:59 +02001456static void btusb_waker(struct work_struct *work)
1457{
1458 struct btusb_data *data = container_of(work, struct btusb_data, waker);
1459 int err;
1460
1461 err = usb_autopm_get_interface(data->intf);
1462 if (err < 0)
1463 return;
1464
1465 usb_autopm_put_interface(data->intf);
1466}
1467
Marcel Holtmann9f8f9622013-04-10 08:11:35 -07001468static int btusb_setup_bcm92035(struct hci_dev *hdev)
1469{
1470 struct sk_buff *skb;
1471 u8 val = 0x00;
1472
1473 BT_DBG("%s", hdev->name);
1474
1475 skb = __hci_cmd_sync(hdev, 0xfc3b, 1, &val, HCI_INIT_TIMEOUT);
1476 if (IS_ERR(skb))
1477 BT_ERR("BCM92035 command failed (%ld)", -PTR_ERR(skb));
1478 else
1479 kfree_skb(skb);
1480
1481 return 0;
1482}
1483
Marcel Holtmann81cac642014-01-03 03:02:36 -08001484static int btusb_setup_csr(struct hci_dev *hdev)
1485{
1486 struct hci_rp_read_local_version *rp;
1487 struct sk_buff *skb;
Marcel Holtmann81cac642014-01-03 03:02:36 -08001488
1489 BT_DBG("%s", hdev->name);
1490
Marcel Holtmann7cd84d72015-06-07 10:01:02 +02001491 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
1492 HCI_INIT_TIMEOUT);
1493 if (IS_ERR(skb)) {
1494 int err = PTR_ERR(skb);
1495 BT_ERR("%s: CSR: Local version failed (%d)", hdev->name, err);
1496 return err;
1497 }
1498
1499 if (skb->len != sizeof(struct hci_rp_read_local_version)) {
1500 BT_ERR("%s: CSR: Local version length mismatch", hdev->name);
1501 kfree_skb(skb);
1502 return -EIO;
1503 }
Marcel Holtmann81cac642014-01-03 03:02:36 -08001504
Marcel Holtmann89e75332014-09-16 04:44:50 +02001505 rp = (struct hci_rp_read_local_version *)skb->data;
Marcel Holtmann81cac642014-01-03 03:02:36 -08001506
Johan Hedberg6cafcd92015-08-30 21:47:21 +03001507 /* Detect controllers which aren't real CSR ones. */
1508 if (le16_to_cpu(rp->manufacturer) != 10 ||
1509 le16_to_cpu(rp->lmp_subver) == 0x0c5c) {
Marcel Holtmann9641d342015-06-07 10:01:01 +02001510 /* Clear the reset quirk since this is not an actual
1511 * early Bluetooth 1.1 device from CSR.
1512 */
1513 clear_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmann81cac642014-01-03 03:02:36 -08001514
Marcel Holtmann9641d342015-06-07 10:01:01 +02001515 /* These fake CSR controllers have all a broken
1516 * stored link key handling and so just disable it.
1517 */
1518 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
Marcel Holtmann81cac642014-01-03 03:02:36 -08001519 }
1520
Marcel Holtmann81cac642014-01-03 03:02:36 -08001521 kfree_skb(skb);
1522
Marcel Holtmann9641d342015-06-07 10:01:01 +02001523 return 0;
Marcel Holtmann81cac642014-01-03 03:02:36 -08001524}
1525
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001526static const struct firmware *btusb_setup_intel_get_fw(struct hci_dev *hdev,
Marcel Holtmann89e75332014-09-16 04:44:50 +02001527 struct intel_version *ver)
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001528{
1529 const struct firmware *fw;
1530 char fwname[64];
1531 int ret;
1532
1533 snprintf(fwname, sizeof(fwname),
1534 "intel/ibt-hw-%x.%x.%x-fw-%x.%x.%x.%x.%x.bseq",
1535 ver->hw_platform, ver->hw_variant, ver->hw_revision,
1536 ver->fw_variant, ver->fw_revision, ver->fw_build_num,
1537 ver->fw_build_ww, ver->fw_build_yy);
1538
1539 ret = request_firmware(&fw, fwname, &hdev->dev);
1540 if (ret < 0) {
1541 if (ret == -EINVAL) {
1542 BT_ERR("%s Intel firmware file request failed (%d)",
1543 hdev->name, ret);
1544 return NULL;
1545 }
1546
1547 BT_ERR("%s failed to open Intel firmware file: %s(%d)",
1548 hdev->name, fwname, ret);
1549
1550 /* If the correct firmware patch file is not found, use the
1551 * default firmware patch file instead
1552 */
1553 snprintf(fwname, sizeof(fwname), "intel/ibt-hw-%x.%x.bseq",
1554 ver->hw_platform, ver->hw_variant);
1555 if (request_firmware(&fw, fwname, &hdev->dev) < 0) {
1556 BT_ERR("%s failed to open default Intel fw file: %s",
1557 hdev->name, fwname);
1558 return NULL;
1559 }
1560 }
1561
1562 BT_INFO("%s: Intel Bluetooth firmware file: %s", hdev->name, fwname);
1563
1564 return fw;
1565}
1566
1567static int btusb_setup_intel_patching(struct hci_dev *hdev,
1568 const struct firmware *fw,
1569 const u8 **fw_ptr, int *disable_patch)
1570{
1571 struct sk_buff *skb;
1572 struct hci_command_hdr *cmd;
1573 const u8 *cmd_param;
1574 struct hci_event_hdr *evt = NULL;
1575 const u8 *evt_param = NULL;
1576 int remain = fw->size - (*fw_ptr - fw->data);
1577
1578 /* The first byte indicates the types of the patch command or event.
1579 * 0x01 means HCI command and 0x02 is HCI event. If the first bytes
1580 * in the current firmware buffer doesn't start with 0x01 or
1581 * the size of remain buffer is smaller than HCI command header,
1582 * the firmware file is corrupted and it should stop the patching
1583 * process.
1584 */
1585 if (remain > HCI_COMMAND_HDR_SIZE && *fw_ptr[0] != 0x01) {
1586 BT_ERR("%s Intel fw corrupted: invalid cmd read", hdev->name);
1587 return -EINVAL;
1588 }
1589 (*fw_ptr)++;
1590 remain--;
1591
1592 cmd = (struct hci_command_hdr *)(*fw_ptr);
1593 *fw_ptr += sizeof(*cmd);
1594 remain -= sizeof(*cmd);
1595
1596 /* Ensure that the remain firmware data is long enough than the length
1597 * of command parameter. If not, the firmware file is corrupted.
1598 */
1599 if (remain < cmd->plen) {
1600 BT_ERR("%s Intel fw corrupted: invalid cmd len", hdev->name);
1601 return -EFAULT;
1602 }
1603
1604 /* If there is a command that loads a patch in the firmware
1605 * file, then enable the patch upon success, otherwise just
1606 * disable the manufacturer mode, for example patch activation
1607 * is not required when the default firmware patch file is used
1608 * because there are no patch data to load.
1609 */
1610 if (*disable_patch && le16_to_cpu(cmd->opcode) == 0xfc8e)
1611 *disable_patch = 0;
1612
1613 cmd_param = *fw_ptr;
1614 *fw_ptr += cmd->plen;
1615 remain -= cmd->plen;
1616
1617 /* This reads the expected events when the above command is sent to the
1618 * device. Some vendor commands expects more than one events, for
1619 * example command status event followed by vendor specific event.
1620 * For this case, it only keeps the last expected event. so the command
1621 * can be sent with __hci_cmd_sync_ev() which returns the sk_buff of
1622 * last expected event.
1623 */
1624 while (remain > HCI_EVENT_HDR_SIZE && *fw_ptr[0] == 0x02) {
1625 (*fw_ptr)++;
1626 remain--;
1627
1628 evt = (struct hci_event_hdr *)(*fw_ptr);
1629 *fw_ptr += sizeof(*evt);
1630 remain -= sizeof(*evt);
1631
1632 if (remain < evt->plen) {
1633 BT_ERR("%s Intel fw corrupted: invalid evt len",
1634 hdev->name);
1635 return -EFAULT;
1636 }
1637
1638 evt_param = *fw_ptr;
1639 *fw_ptr += evt->plen;
1640 remain -= evt->plen;
1641 }
1642
1643 /* Every HCI commands in the firmware file has its correspond event.
1644 * If event is not found or remain is smaller than zero, the firmware
1645 * file is corrupted.
1646 */
1647 if (!evt || !evt_param || remain < 0) {
1648 BT_ERR("%s Intel fw corrupted: invalid evt read", hdev->name);
1649 return -EFAULT;
1650 }
1651
1652 skb = __hci_cmd_sync_ev(hdev, le16_to_cpu(cmd->opcode), cmd->plen,
1653 cmd_param, evt->evt, HCI_INIT_TIMEOUT);
1654 if (IS_ERR(skb)) {
1655 BT_ERR("%s sending Intel patch command (0x%4.4x) failed (%ld)",
1656 hdev->name, cmd->opcode, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001657 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001658 }
1659
1660 /* It ensures that the returned event matches the event data read from
1661 * the firmware file. At fist, it checks the length and then
1662 * the contents of the event.
1663 */
1664 if (skb->len != evt->plen) {
1665 BT_ERR("%s mismatch event length (opcode 0x%4.4x)", hdev->name,
1666 le16_to_cpu(cmd->opcode));
1667 kfree_skb(skb);
1668 return -EFAULT;
1669 }
1670
1671 if (memcmp(skb->data, evt_param, evt->plen)) {
1672 BT_ERR("%s mismatch event parameter (opcode 0x%4.4x)",
1673 hdev->name, le16_to_cpu(cmd->opcode));
1674 kfree_skb(skb);
1675 return -EFAULT;
1676 }
1677 kfree_skb(skb);
1678
1679 return 0;
1680}
1681
1682static int btusb_setup_intel(struct hci_dev *hdev)
1683{
1684 struct sk_buff *skb;
1685 const struct firmware *fw;
1686 const u8 *fw_ptr;
Loic Poulain28dc4b92015-12-03 16:10:22 +01001687 int disable_patch, err;
Loic Poulain6c483de2015-12-06 16:18:34 +01001688 struct intel_version ver;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001689
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001690 BT_DBG("%s", hdev->name);
1691
1692 /* The controller has a bug with the first HCI command sent to it
1693 * returning number of completed commands as zero. This would stall the
1694 * command processing in the Bluetooth core.
1695 *
1696 * As a workaround, send HCI Reset command first which will reset the
1697 * number of completed commands and allow normal command processing
1698 * from now on.
1699 */
1700 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
1701 if (IS_ERR(skb)) {
1702 BT_ERR("%s sending initial HCI reset command failed (%ld)",
1703 hdev->name, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001704 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001705 }
1706 kfree_skb(skb);
1707
1708 /* Read Intel specific controller version first to allow selection of
1709 * which firmware file to load.
1710 *
1711 * The returned information are hardware variant and revision plus
1712 * firmware variant, revision and build number.
1713 */
Loic Poulain6c483de2015-12-06 16:18:34 +01001714 err = btintel_read_version(hdev, &ver);
1715 if (err)
1716 return err;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001717
1718 BT_INFO("%s: read Intel version: %02x%02x%02x%02x%02x%02x%02x%02x%02x",
Loic Poulain6c483de2015-12-06 16:18:34 +01001719 hdev->name, ver.hw_platform, ver.hw_variant, ver.hw_revision,
1720 ver.fw_variant, ver.fw_revision, ver.fw_build_num,
1721 ver.fw_build_ww, ver.fw_build_yy, ver.fw_patch_num);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001722
1723 /* fw_patch_num indicates the version of patch the device currently
1724 * have. If there is no patch data in the device, it is always 0x00.
Minjune Kim5075eda2015-08-27 13:21:52 +09001725 * So, if it is other than 0x00, no need to patch the device again.
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001726 */
Loic Poulain6c483de2015-12-06 16:18:34 +01001727 if (ver.fw_patch_num) {
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001728 BT_INFO("%s: Intel device is already patched. patch num: %02x",
Loic Poulain6c483de2015-12-06 16:18:34 +01001729 hdev->name, ver.fw_patch_num);
Marcel Holtmann213445b2015-10-21 02:45:19 +02001730 goto complete;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001731 }
1732
1733 /* Opens the firmware patch file based on the firmware version read
1734 * from the controller. If it fails to open the matching firmware
1735 * patch file, it tries to open the default firmware patch file.
1736 * If no patch file is found, allow the device to operate without
1737 * a patch.
1738 */
Loic Poulain6c483de2015-12-06 16:18:34 +01001739 fw = btusb_setup_intel_get_fw(hdev, &ver);
1740 if (!fw)
Marcel Holtmann213445b2015-10-21 02:45:19 +02001741 goto complete;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001742 fw_ptr = fw->data;
1743
Loic Poulain28dc4b92015-12-03 16:10:22 +01001744 /* Enable the manufacturer mode of the controller.
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001745 * Only while this mode is enabled, the driver can download the
1746 * firmware patch data and configuration parameters.
1747 */
Loic Poulain28dc4b92015-12-03 16:10:22 +01001748 err = btintel_enter_mfg(hdev);
1749 if (err) {
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001750 release_firmware(fw);
Loic Poulain28dc4b92015-12-03 16:10:22 +01001751 return err;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001752 }
1753
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001754 disable_patch = 1;
1755
1756 /* The firmware data file consists of list of Intel specific HCI
1757 * commands and its expected events. The first byte indicates the
1758 * type of the message, either HCI command or HCI event.
1759 *
1760 * It reads the command and its expected event from the firmware file,
1761 * and send to the controller. Once __hci_cmd_sync_ev() returns,
1762 * the returned event is compared with the event read from the firmware
1763 * file and it will continue until all the messages are downloaded to
1764 * the controller.
1765 *
1766 * Once the firmware patching is completed successfully,
1767 * the manufacturer mode is disabled with reset and activating the
1768 * downloaded patch.
1769 *
1770 * If the firmware patching fails, the manufacturer mode is
1771 * disabled with reset and deactivating the patch.
1772 *
1773 * If the default patch file is used, no reset is done when disabling
1774 * the manufacturer.
1775 */
1776 while (fw->size > fw_ptr - fw->data) {
1777 int ret;
1778
1779 ret = btusb_setup_intel_patching(hdev, fw, &fw_ptr,
1780 &disable_patch);
1781 if (ret < 0)
1782 goto exit_mfg_deactivate;
1783 }
1784
1785 release_firmware(fw);
1786
1787 if (disable_patch)
1788 goto exit_mfg_disable;
1789
1790 /* Patching completed successfully and disable the manufacturer mode
1791 * with reset and activate the downloaded firmware patches.
1792 */
Loic Poulain28dc4b92015-12-03 16:10:22 +01001793 err = btintel_exit_mfg(hdev, true, true);
1794 if (err)
1795 return err;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001796
1797 BT_INFO("%s: Intel Bluetooth firmware patch completed and activated",
1798 hdev->name);
1799
Marcel Holtmann213445b2015-10-21 02:45:19 +02001800 goto complete;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001801
1802exit_mfg_disable:
1803 /* Disable the manufacturer mode without reset */
Loic Poulain28dc4b92015-12-03 16:10:22 +01001804 err = btintel_exit_mfg(hdev, false, false);
1805 if (err)
1806 return err;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001807
1808 BT_INFO("%s: Intel Bluetooth firmware patch completed", hdev->name);
Marcel Holtmann40cb0982014-07-02 12:06:45 +02001809
Marcel Holtmann213445b2015-10-21 02:45:19 +02001810 goto complete;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001811
1812exit_mfg_deactivate:
1813 release_firmware(fw);
1814
1815 /* Patching failed. Disable the manufacturer mode with reset and
1816 * deactivate the downloaded firmware patches.
1817 */
Loic Poulain28dc4b92015-12-03 16:10:22 +01001818 err = btintel_exit_mfg(hdev, true, false);
1819 if (err)
1820 return err;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001821
1822 BT_INFO("%s: Intel Bluetooth firmware patch completed and deactivated",
1823 hdev->name);
1824
Marcel Holtmann213445b2015-10-21 02:45:19 +02001825complete:
1826 /* Set the event mask for Intel specific vendor events. This enables
1827 * a few extra events that are useful during general operation.
1828 */
1829 btintel_set_event_mask_mfg(hdev, false);
1830
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07001831 btintel_check_bdaddr(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001832 return 0;
1833}
1834
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001835static int inject_cmd_complete(struct hci_dev *hdev, __u16 opcode)
1836{
1837 struct sk_buff *skb;
1838 struct hci_event_hdr *hdr;
1839 struct hci_ev_cmd_complete *evt;
1840
1841 skb = bt_skb_alloc(sizeof(*hdr) + sizeof(*evt) + 1, GFP_ATOMIC);
1842 if (!skb)
1843 return -ENOMEM;
1844
Johannes Berg4df864c2017-06-16 14:29:21 +02001845 hdr = skb_put(skb, sizeof(*hdr));
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001846 hdr->evt = HCI_EV_CMD_COMPLETE;
1847 hdr->plen = sizeof(*evt) + 1;
1848
Johannes Berg4df864c2017-06-16 14:29:21 +02001849 evt = skb_put(skb, sizeof(*evt));
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001850 evt->ncmd = 0x01;
1851 evt->opcode = cpu_to_le16(opcode);
1852
Johannes Berg634fef62017-06-16 14:29:24 +02001853 skb_put_u8(skb, 0x00);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001854
Marcel Holtmann618e8bc2015-11-05 07:33:56 +01001855 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001856
1857 return hci_recv_frame(hdev, skb);
1858}
1859
1860static int btusb_recv_bulk_intel(struct btusb_data *data, void *buffer,
1861 int count)
1862{
1863 /* When the device is in bootloader mode, then it can send
1864 * events via the bulk endpoint. These events are treated the
1865 * same way as the ones received from the interrupt endpoint.
1866 */
1867 if (test_bit(BTUSB_BOOTLOADER, &data->flags))
1868 return btusb_recv_intr(data, buffer, count);
1869
1870 return btusb_recv_bulk(data, buffer, count);
1871}
1872
Marcel Holtmannccd6da22015-04-09 00:35:19 -07001873static void btusb_intel_bootup(struct btusb_data *data, const void *ptr,
1874 unsigned int len)
1875{
1876 const struct intel_bootup *evt = ptr;
1877
1878 if (len != sizeof(*evt))
1879 return;
1880
1881 if (test_and_clear_bit(BTUSB_BOOTING, &data->flags)) {
1882 smp_mb__after_atomic();
1883 wake_up_bit(&data->flags, BTUSB_BOOTING);
1884 }
1885}
1886
1887static void btusb_intel_secure_send_result(struct btusb_data *data,
1888 const void *ptr, unsigned int len)
1889{
1890 const struct intel_secure_send_result *evt = ptr;
1891
1892 if (len != sizeof(*evt))
1893 return;
1894
1895 if (evt->result)
1896 set_bit(BTUSB_FIRMWARE_FAILED, &data->flags);
1897
1898 if (test_and_clear_bit(BTUSB_DOWNLOADING, &data->flags) &&
1899 test_bit(BTUSB_FIRMWARE_LOADED, &data->flags)) {
1900 smp_mb__after_atomic();
1901 wake_up_bit(&data->flags, BTUSB_DOWNLOADING);
1902 }
1903}
1904
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001905static int btusb_recv_event_intel(struct hci_dev *hdev, struct sk_buff *skb)
1906{
1907 struct btusb_data *data = hci_get_drvdata(hdev);
1908
1909 if (test_bit(BTUSB_BOOTLOADER, &data->flags)) {
1910 struct hci_event_hdr *hdr = (void *)skb->data;
1911
Marcel Holtmannccd6da22015-04-09 00:35:19 -07001912 if (skb->len > HCI_EVENT_HDR_SIZE && hdr->evt == 0xff &&
1913 hdr->plen > 0) {
1914 const void *ptr = skb->data + HCI_EVENT_HDR_SIZE + 1;
1915 unsigned int len = skb->len - HCI_EVENT_HDR_SIZE - 1;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001916
Marcel Holtmannccd6da22015-04-09 00:35:19 -07001917 switch (skb->data[2]) {
1918 case 0x02:
1919 /* When switching to the operational firmware
1920 * the device sends a vendor specific event
1921 * indicating that the bootup completed.
1922 */
1923 btusb_intel_bootup(data, ptr, len);
1924 break;
1925 case 0x06:
1926 /* When the firmware loading completes the
1927 * device sends out a vendor specific event
1928 * indicating the result of the firmware
1929 * loading.
1930 */
1931 btusb_intel_secure_send_result(data, ptr, len);
1932 break;
Johan Hedbergfad70972015-01-30 10:58:55 +02001933 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001934 }
1935 }
1936
1937 return hci_recv_frame(hdev, skb);
1938}
1939
1940static int btusb_send_frame_intel(struct hci_dev *hdev, struct sk_buff *skb)
1941{
1942 struct btusb_data *data = hci_get_drvdata(hdev);
1943 struct urb *urb;
1944
1945 BT_DBG("%s", hdev->name);
1946
Marcel Holtmann618e8bc2015-11-05 07:33:56 +01001947 switch (hci_skb_pkt_type(skb)) {
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001948 case HCI_COMMAND_PKT:
1949 if (test_bit(BTUSB_BOOTLOADER, &data->flags)) {
1950 struct hci_command_hdr *cmd = (void *)skb->data;
1951 __u16 opcode = le16_to_cpu(cmd->opcode);
1952
1953 /* When in bootloader mode and the command 0xfc09
1954 * is received, it needs to be send down the
1955 * bulk endpoint. So allocate a bulk URB instead.
1956 */
1957 if (opcode == 0xfc09)
1958 urb = alloc_bulk_urb(hdev, skb);
1959 else
1960 urb = alloc_ctrl_urb(hdev, skb);
1961
1962 /* When the 0xfc01 command is issued to boot into
1963 * the operational firmware, it will actually not
1964 * send a command complete event. To keep the flow
1965 * control working inject that event here.
1966 */
1967 if (opcode == 0xfc01)
1968 inject_cmd_complete(hdev, opcode);
1969 } else {
1970 urb = alloc_ctrl_urb(hdev, skb);
1971 }
1972 if (IS_ERR(urb))
1973 return PTR_ERR(urb);
1974
1975 hdev->stat.cmd_tx++;
1976 return submit_or_queue_tx_urb(hdev, urb);
1977
1978 case HCI_ACLDATA_PKT:
1979 urb = alloc_bulk_urb(hdev, skb);
1980 if (IS_ERR(urb))
1981 return PTR_ERR(urb);
1982
1983 hdev->stat.acl_tx++;
1984 return submit_or_queue_tx_urb(hdev, urb);
1985
1986 case HCI_SCODATA_PKT:
1987 if (hci_conn_num(hdev, SCO_LINK) < 1)
1988 return -ENODEV;
1989
1990 urb = alloc_isoc_urb(hdev, skb);
1991 if (IS_ERR(urb))
1992 return PTR_ERR(urb);
1993
1994 hdev->stat.sco_tx++;
1995 return submit_tx_urb(hdev, urb);
1996 }
1997
1998 return -EILSEQ;
1999}
2000
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002001static int btusb_setup_intel_new(struct hci_dev *hdev)
2002{
2003 static const u8 reset_param[] = { 0x00, 0x01, 0x00, 0x01,
2004 0x00, 0x08, 0x04, 0x00 };
2005 struct btusb_data *data = hci_get_drvdata(hdev);
2006 struct sk_buff *skb;
Loic Poulain6c483de2015-12-06 16:18:34 +01002007 struct intel_version ver;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002008 struct intel_boot_params *params;
2009 const struct firmware *fw;
2010 const u8 *fw_ptr;
Marcel Holtmanne66890a92015-06-07 09:47:08 +02002011 u32 frag_len;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002012 char fwname[64];
2013 ktime_t calltime, delta, rettime;
2014 unsigned long long duration;
2015 int err;
2016
2017 BT_DBG("%s", hdev->name);
2018
2019 calltime = ktime_get();
2020
2021 /* Read the Intel version information to determine if the device
2022 * is in bootloader mode or if it already has operational firmware
2023 * loaded.
2024 */
Loic Poulain6c483de2015-12-06 16:18:34 +01002025 err = btintel_read_version(hdev, &ver);
2026 if (err)
2027 return err;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002028
2029 /* The hardware platform number has a fixed value of 0x37 and
2030 * for now only accept this single value.
2031 */
Loic Poulain6c483de2015-12-06 16:18:34 +01002032 if (ver.hw_platform != 0x37) {
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002033 BT_ERR("%s: Unsupported Intel hardware platform (%u)",
Loic Poulain6c483de2015-12-06 16:18:34 +01002034 hdev->name, ver.hw_platform);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002035 return -EINVAL;
2036 }
2037
Tedd Ho-Jeong An92688342017-03-06 15:38:26 -08002038 /* Check for supported iBT hardware variants of this firmware
2039 * loading method.
Tedd Ho-Jeong Ana0af53b2016-05-06 11:53:46 -07002040 *
2041 * This check has been put in place to ensure correct forward
2042 * compatibility options when newer hardware variants come along.
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002043 */
Tedd Ho-Jeong An92688342017-03-06 15:38:26 -08002044 switch (ver.hw_variant) {
2045 case 0x0b: /* SfP */
2046 case 0x0c: /* WsP */
Tedd Ho-Jeong An86a61292017-05-01 13:35:12 -07002047 case 0x11: /* JfP */
Marcel Holtmannde766142017-03-06 15:38:28 -08002048 case 0x12: /* ThP */
Tedd Ho-Jeong An92688342017-03-06 15:38:26 -08002049 break;
2050 default:
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002051 BT_ERR("%s: Unsupported Intel hardware variant (%u)",
Loic Poulain6c483de2015-12-06 16:18:34 +01002052 hdev->name, ver.hw_variant);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002053 return -EINVAL;
2054 }
2055
Loic Poulain6c483de2015-12-06 16:18:34 +01002056 btintel_version_info(hdev, &ver);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002057
2058 /* The firmware variant determines if the device is in bootloader
2059 * mode or is running operational firmware. The value 0x06 identifies
2060 * the bootloader and the value 0x23 identifies the operational
2061 * firmware.
2062 *
2063 * When the operational firmware is already present, then only
2064 * the check for valid Bluetooth device address is needed. This
2065 * determines if the device will be added as configured or
2066 * unconfigured controller.
2067 *
2068 * It is not possible to use the Secure Boot Parameters in this
2069 * case since that command is only available in bootloader mode.
2070 */
Loic Poulain6c483de2015-12-06 16:18:34 +01002071 if (ver.fw_variant == 0x23) {
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002072 clear_bit(BTUSB_BOOTLOADER, &data->flags);
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07002073 btintel_check_bdaddr(hdev);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002074 return 0;
2075 }
2076
2077 /* If the device is not in bootloader mode, then the only possible
2078 * choice is to return an error and abort the device initialization.
2079 */
Loic Poulain6c483de2015-12-06 16:18:34 +01002080 if (ver.fw_variant != 0x06) {
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002081 BT_ERR("%s: Unsupported Intel firmware variant (%u)",
Loic Poulain6c483de2015-12-06 16:18:34 +01002082 hdev->name, ver.fw_variant);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002083 return -ENODEV;
2084 }
2085
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002086 /* Read the secure boot parameters to identify the operating
2087 * details of the bootloader.
2088 */
2089 skb = __hci_cmd_sync(hdev, 0xfc0d, 0, NULL, HCI_INIT_TIMEOUT);
2090 if (IS_ERR(skb)) {
2091 BT_ERR("%s: Reading Intel boot parameters failed (%ld)",
2092 hdev->name, PTR_ERR(skb));
2093 return PTR_ERR(skb);
2094 }
2095
2096 if (skb->len != sizeof(*params)) {
2097 BT_ERR("%s: Intel boot parameters size mismatch", hdev->name);
2098 kfree_skb(skb);
2099 return -EILSEQ;
2100 }
2101
2102 params = (struct intel_boot_params *)skb->data;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002103
2104 BT_INFO("%s: Device revision is %u", hdev->name,
2105 le16_to_cpu(params->dev_revid));
2106
2107 BT_INFO("%s: Secure boot is %s", hdev->name,
2108 params->secure_boot ? "enabled" : "disabled");
2109
Marcel Holtmann2220994e2015-10-13 13:54:55 +02002110 BT_INFO("%s: OTP lock is %s", hdev->name,
2111 params->otp_lock ? "enabled" : "disabled");
2112
2113 BT_INFO("%s: API lock is %s", hdev->name,
2114 params->api_lock ? "enabled" : "disabled");
2115
2116 BT_INFO("%s: Debug lock is %s", hdev->name,
2117 params->debug_lock ? "enabled" : "disabled");
2118
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002119 BT_INFO("%s: Minimum firmware build %u week %u %u", hdev->name,
2120 params->min_fw_build_nn, params->min_fw_build_cw,
2121 2000 + params->min_fw_build_yy);
2122
2123 /* It is required that every single firmware fragment is acknowledged
2124 * with a command complete event. If the boot parameters indicate
2125 * that this bootloader does not send them, then abort the setup.
2126 */
2127 if (params->limited_cce != 0x00) {
2128 BT_ERR("%s: Unsupported Intel firmware loading method (%u)",
2129 hdev->name, params->limited_cce);
2130 kfree_skb(skb);
2131 return -EINVAL;
2132 }
2133
2134 /* If the OTP has no valid Bluetooth device address, then there will
2135 * also be no valid address for the operational firmware.
2136 */
2137 if (!bacmp(&params->otp_bdaddr, BDADDR_ANY)) {
2138 BT_INFO("%s: No device address configured", hdev->name);
2139 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
2140 }
2141
2142 /* With this Intel bootloader only the hardware variant and device
2143 * revision information are used to select the right firmware.
2144 *
Tedd Ho-Jeong An230b04a2016-06-28 08:56:39 -07002145 * The firmware filename is ibt-<hw_variant>-<dev_revid>.sfi.
2146 *
2147 * Currently the supported hardware variants are:
2148 * 11 (0x0b) for iBT3.0 (LnP/SfP)
2149 * 12 (0x0c) for iBT3.5 (WsP)
Tedd Ho-Jeong An86a61292017-05-01 13:35:12 -07002150 * 17 (0x11) for iBT3.5 (JfP)
2151 * 18 (0x12) for iBT3.5 (ThP)
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002152 */
Tedd Ho-Jeong An230b04a2016-06-28 08:56:39 -07002153 snprintf(fwname, sizeof(fwname), "intel/ibt-%u-%u.sfi",
2154 le16_to_cpu(ver.hw_variant),
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002155 le16_to_cpu(params->dev_revid));
2156
2157 err = request_firmware(&fw, fwname, &hdev->dev);
2158 if (err < 0) {
2159 BT_ERR("%s: Failed to load Intel firmware file (%d)",
2160 hdev->name, err);
2161 kfree_skb(skb);
2162 return err;
2163 }
2164
2165 BT_INFO("%s: Found device firmware: %s", hdev->name, fwname);
2166
Tedd Ho-Jeong An52cc9162015-06-12 16:20:05 -07002167 /* Save the DDC file name for later use to apply once the firmware
2168 * downloading is done.
2169 */
Tedd Ho-Jeong An230b04a2016-06-28 08:56:39 -07002170 snprintf(fwname, sizeof(fwname), "intel/ibt-%u-%u.ddc",
2171 le16_to_cpu(ver.hw_variant),
Tedd Ho-Jeong An52cc9162015-06-12 16:20:05 -07002172 le16_to_cpu(params->dev_revid));
2173
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002174 kfree_skb(skb);
2175
2176 if (fw->size < 644) {
2177 BT_ERR("%s: Invalid size of firmware file (%zu)",
2178 hdev->name, fw->size);
2179 err = -EBADF;
2180 goto done;
2181 }
2182
2183 set_bit(BTUSB_DOWNLOADING, &data->flags);
2184
2185 /* Start the firmware download transaction with the Init fragment
2186 * represented by the 128 bytes of CSS header.
2187 */
Marcel Holtmann09df1232015-07-05 14:55:36 +02002188 err = btintel_secure_send(hdev, 0x00, 128, fw->data);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002189 if (err < 0) {
2190 BT_ERR("%s: Failed to send firmware header (%d)",
2191 hdev->name, err);
2192 goto done;
2193 }
2194
2195 /* Send the 256 bytes of public key information from the firmware
2196 * as the PKey fragment.
2197 */
Marcel Holtmann09df1232015-07-05 14:55:36 +02002198 err = btintel_secure_send(hdev, 0x03, 256, fw->data + 128);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002199 if (err < 0) {
2200 BT_ERR("%s: Failed to send firmware public key (%d)",
2201 hdev->name, err);
2202 goto done;
2203 }
2204
2205 /* Send the 256 bytes of signature information from the firmware
2206 * as the Sign fragment.
2207 */
Marcel Holtmann09df1232015-07-05 14:55:36 +02002208 err = btintel_secure_send(hdev, 0x02, 256, fw->data + 388);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002209 if (err < 0) {
2210 BT_ERR("%s: Failed to send firmware signature (%d)",
2211 hdev->name, err);
2212 goto done;
2213 }
2214
2215 fw_ptr = fw->data + 644;
Marcel Holtmanne66890a92015-06-07 09:47:08 +02002216 frag_len = 0;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002217
2218 while (fw_ptr - fw->data < fw->size) {
Marcel Holtmanne66890a92015-06-07 09:47:08 +02002219 struct hci_command_hdr *cmd = (void *)(fw_ptr + frag_len);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002220
Marcel Holtmanne66890a92015-06-07 09:47:08 +02002221 frag_len += sizeof(*cmd) + cmd->plen;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002222
Minjune Kim5075eda2015-08-27 13:21:52 +09002223 /* The parameter length of the secure send command requires
Marcel Holtmanne66890a92015-06-07 09:47:08 +02002224 * a 4 byte alignment. It happens so that the firmware file
2225 * contains proper Intel_NOP commands to align the fragments
2226 * as needed.
2227 *
2228 * Send set of commands with 4 byte alignment from the
2229 * firmware data buffer as a single Data fragement.
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002230 */
Marcel Holtmanne66890a92015-06-07 09:47:08 +02002231 if (!(frag_len % 4)) {
Marcel Holtmann09df1232015-07-05 14:55:36 +02002232 err = btintel_secure_send(hdev, 0x01, frag_len, fw_ptr);
Marcel Holtmanne66890a92015-06-07 09:47:08 +02002233 if (err < 0) {
2234 BT_ERR("%s: Failed to send firmware data (%d)",
2235 hdev->name, err);
2236 goto done;
2237 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002238
Marcel Holtmanne66890a92015-06-07 09:47:08 +02002239 fw_ptr += frag_len;
2240 frag_len = 0;
2241 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002242 }
2243
Marcel Holtmannce6bb922015-01-28 01:58:40 -08002244 set_bit(BTUSB_FIRMWARE_LOADED, &data->flags);
2245
Johan Hedberga087a982015-01-30 10:58:54 +02002246 BT_INFO("%s: Waiting for firmware download to complete", hdev->name);
2247
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002248 /* Before switching the device into operational mode and with that
2249 * booting the loaded firmware, wait for the bootloader notification
2250 * that all fragments have been successfully received.
2251 *
Johan Hedberga087a982015-01-30 10:58:54 +02002252 * When the event processing receives the notification, then the
2253 * BTUSB_DOWNLOADING flag will be cleared.
2254 *
2255 * The firmware loading should not take longer than 5 seconds
2256 * and thus just timeout if that happens and fail the setup
2257 * of this device.
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002258 */
Johan Hedberg129a7692015-02-14 09:33:35 +02002259 err = wait_on_bit_timeout(&data->flags, BTUSB_DOWNLOADING,
2260 TASK_INTERRUPTIBLE,
2261 msecs_to_jiffies(5000));
Bart Van Asschef0a70a02016-08-11 16:02:44 -07002262 if (err == -EINTR) {
Johan Hedberga087a982015-01-30 10:58:54 +02002263 BT_ERR("%s: Firmware loading interrupted", hdev->name);
Johan Hedberga087a982015-01-30 10:58:54 +02002264 goto done;
2265 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002266
Johan Hedberga087a982015-01-30 10:58:54 +02002267 if (err) {
2268 BT_ERR("%s: Firmware loading timeout", hdev->name);
2269 err = -ETIMEDOUT;
2270 goto done;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002271 }
2272
2273 if (test_bit(BTUSB_FIRMWARE_FAILED, &data->flags)) {
2274 BT_ERR("%s: Firmware loading failed", hdev->name);
2275 err = -ENOEXEC;
2276 goto done;
2277 }
2278
2279 rettime = ktime_get();
2280 delta = ktime_sub(rettime, calltime);
2281 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
2282
2283 BT_INFO("%s: Firmware loaded in %llu usecs", hdev->name, duration);
2284
2285done:
2286 release_firmware(fw);
2287
2288 if (err < 0)
2289 return err;
2290
2291 calltime = ktime_get();
2292
2293 set_bit(BTUSB_BOOTING, &data->flags);
2294
2295 skb = __hci_cmd_sync(hdev, 0xfc01, sizeof(reset_param), reset_param,
2296 HCI_INIT_TIMEOUT);
2297 if (IS_ERR(skb))
2298 return PTR_ERR(skb);
2299
2300 kfree_skb(skb);
2301
2302 /* The bootloader will not indicate when the device is ready. This
2303 * is done by the operational firmware sending bootup notification.
Johan Hedbergfad70972015-01-30 10:58:55 +02002304 *
2305 * Booting into operational firmware should not take longer than
2306 * 1 second. However if that happens, then just fail the setup
2307 * since something went wrong.
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002308 */
Johan Hedbergfad70972015-01-30 10:58:55 +02002309 BT_INFO("%s: Waiting for device to boot", hdev->name);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002310
Johan Hedberg129a7692015-02-14 09:33:35 +02002311 err = wait_on_bit_timeout(&data->flags, BTUSB_BOOTING,
2312 TASK_INTERRUPTIBLE,
2313 msecs_to_jiffies(1000));
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002314
Bart Van Asschef0a70a02016-08-11 16:02:44 -07002315 if (err == -EINTR) {
Johan Hedbergfad70972015-01-30 10:58:55 +02002316 BT_ERR("%s: Device boot interrupted", hdev->name);
2317 return -EINTR;
2318 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002319
Johan Hedbergfad70972015-01-30 10:58:55 +02002320 if (err) {
2321 BT_ERR("%s: Device boot timeout", hdev->name);
2322 return -ETIMEDOUT;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002323 }
2324
2325 rettime = ktime_get();
2326 delta = ktime_sub(rettime, calltime);
2327 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
2328
2329 BT_INFO("%s: Device booted in %llu usecs", hdev->name, duration);
2330
2331 clear_bit(BTUSB_BOOTLOADER, &data->flags);
2332
Tedd Ho-Jeong An52cc9162015-06-12 16:20:05 -07002333 /* Once the device is running in operational mode, it needs to apply
2334 * the device configuration (DDC) parameters.
2335 *
2336 * The device can work without DDC parameters, so even if it fails
2337 * to load the file, no need to fail the setup.
2338 */
Loic Poulaine924d3d2015-09-04 17:54:36 +02002339 btintel_load_ddc_config(hdev, fwname);
Tedd Ho-Jeong An52cc9162015-06-12 16:20:05 -07002340
Marcel Holtmann213445b2015-10-21 02:45:19 +02002341 /* Set the event mask for Intel specific vendor events. This enables
2342 * a few extra events that are useful during general operation. It
2343 * does not enable any debugging related events.
2344 *
2345 * The device will function correctly without these events enabled
2346 * and thus no need to fail the setup.
2347 */
2348 btintel_set_event_mask(hdev, false);
2349
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002350 return 0;
2351}
2352
Tedd Ho-Jeong Anbfbd45e2015-02-13 09:20:52 -08002353static int btusb_shutdown_intel(struct hci_dev *hdev)
2354{
2355 struct sk_buff *skb;
2356 long ret;
2357
2358 /* Some platforms have an issue with BT LED when the interface is
2359 * down or BT radio is turned off, which takes 5 seconds to BT LED
2360 * goes off. This command turns off the BT LED immediately.
2361 */
2362 skb = __hci_cmd_sync(hdev, 0xfc3f, 0, NULL, HCI_INIT_TIMEOUT);
2363 if (IS_ERR(skb)) {
2364 ret = PTR_ERR(skb);
2365 BT_ERR("%s: turning off Intel device LED failed (%ld)",
2366 hdev->name, ret);
2367 return ret;
2368 }
2369 kfree_skb(skb);
2370
2371 return 0;
2372}
2373
Rajat Jaina4ccc9e2017-02-01 14:24:10 -08002374#ifdef CONFIG_PM
2375/* Configure an out-of-band gpio as wake-up pin, if specified in device tree */
2376static int marvell_config_oob_wake(struct hci_dev *hdev)
2377{
2378 struct sk_buff *skb;
2379 struct btusb_data *data = hci_get_drvdata(hdev);
2380 struct device *dev = &data->udev->dev;
2381 u16 pin, gap, opcode;
2382 int ret;
2383 u8 cmd[5];
2384
2385 /* Move on if no wakeup pin specified */
2386 if (of_property_read_u16(dev->of_node, "marvell,wakeup-pin", &pin) ||
2387 of_property_read_u16(dev->of_node, "marvell,wakeup-gap-ms", &gap))
2388 return 0;
2389
2390 /* Vendor specific command to configure a GPIO as wake-up pin */
2391 opcode = hci_opcode_pack(0x3F, 0x59);
2392 cmd[0] = opcode & 0xFF;
2393 cmd[1] = opcode >> 8;
2394 cmd[2] = 2; /* length of parameters that follow */
2395 cmd[3] = pin;
2396 cmd[4] = gap; /* time in ms, for which wakeup pin should be asserted */
2397
2398 skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL);
2399 if (!skb) {
2400 bt_dev_err(hdev, "%s: No memory\n", __func__);
2401 return -ENOMEM;
2402 }
2403
Johannes Berg59ae1d12017-06-16 14:29:20 +02002404 skb_put_data(skb, cmd, sizeof(cmd));
Rajat Jaina4ccc9e2017-02-01 14:24:10 -08002405 hci_skb_pkt_type(skb) = HCI_COMMAND_PKT;
2406
2407 ret = btusb_send_frame(hdev, skb);
2408 if (ret) {
2409 bt_dev_err(hdev, "%s: configuration failed\n", __func__);
2410 kfree_skb(skb);
2411 return ret;
2412 }
2413
2414 return 0;
2415}
2416#endif
2417
Amitkumar Karwarae8df492014-07-18 14:47:06 -07002418static int btusb_set_bdaddr_marvell(struct hci_dev *hdev,
2419 const bdaddr_t *bdaddr)
2420{
2421 struct sk_buff *skb;
2422 u8 buf[8];
2423 long ret;
2424
2425 buf[0] = 0xfe;
2426 buf[1] = sizeof(bdaddr_t);
2427 memcpy(buf + 2, bdaddr, sizeof(bdaddr_t));
2428
2429 skb = __hci_cmd_sync(hdev, 0xfc22, sizeof(buf), buf, HCI_INIT_TIMEOUT);
2430 if (IS_ERR(skb)) {
2431 ret = PTR_ERR(skb);
2432 BT_ERR("%s: changing Marvell device address failed (%ld)",
2433 hdev->name, ret);
2434 return ret;
2435 }
2436 kfree_skb(skb);
2437
2438 return 0;
2439}
2440
Toshi Kikuchi58592232014-12-12 10:58:05 -08002441static int btusb_set_bdaddr_ath3012(struct hci_dev *hdev,
2442 const bdaddr_t *bdaddr)
2443{
2444 struct sk_buff *skb;
2445 u8 buf[10];
2446 long ret;
2447
2448 buf[0] = 0x01;
2449 buf[1] = 0x01;
2450 buf[2] = 0x00;
2451 buf[3] = sizeof(bdaddr_t);
2452 memcpy(buf + 4, bdaddr, sizeof(bdaddr_t));
2453
2454 skb = __hci_cmd_sync(hdev, 0xfc0b, sizeof(buf), buf, HCI_INIT_TIMEOUT);
2455 if (IS_ERR(skb)) {
2456 ret = PTR_ERR(skb);
2457 BT_ERR("%s: Change address command failed (%ld)",
2458 hdev->name, ret);
2459 return ret;
2460 }
2461 kfree_skb(skb);
2462
2463 return 0;
2464}
2465
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002466#define QCA_DFU_PACKET_LEN 4096
2467
2468#define QCA_GET_TARGET_VERSION 0x09
2469#define QCA_CHECK_STATUS 0x05
2470#define QCA_DFU_DOWNLOAD 0x01
2471
2472#define QCA_SYSCFG_UPDATED 0x40
2473#define QCA_PATCH_UPDATED 0x80
2474#define QCA_DFU_TIMEOUT 3000
2475
2476struct qca_version {
2477 __le32 rom_version;
2478 __le32 patch_version;
2479 __le32 ram_version;
2480 __le32 ref_clock;
2481 __u8 reserved[4];
2482} __packed;
2483
2484struct qca_rampatch_version {
2485 __le16 rom_version;
2486 __le16 patch_version;
2487} __packed;
2488
2489struct qca_device_info {
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002490 u32 rom_version;
2491 u8 rampatch_hdr; /* length of header in rampatch */
2492 u8 nvm_hdr; /* length of header in NVM */
2493 u8 ver_offset; /* offset of version structure in rampatch */
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002494};
2495
2496static const struct qca_device_info qca_devices_table[] = {
2497 { 0x00000100, 20, 4, 10 }, /* Rome 1.0 */
2498 { 0x00000101, 20, 4, 10 }, /* Rome 1.1 */
Chan-yeol Park7f6e6362015-05-21 11:24:27 +09002499 { 0x00000200, 28, 4, 18 }, /* Rome 2.0 */
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002500 { 0x00000201, 28, 4, 18 }, /* Rome 2.1 */
2501 { 0x00000300, 28, 4, 18 }, /* Rome 3.0 */
2502 { 0x00000302, 28, 4, 18 }, /* Rome 3.2 */
2503};
2504
2505static int btusb_qca_send_vendor_req(struct hci_dev *hdev, u8 request,
2506 void *data, u16 size)
2507{
2508 struct btusb_data *btdata = hci_get_drvdata(hdev);
2509 struct usb_device *udev = btdata->udev;
2510 int pipe, err;
2511 u8 *buf;
2512
2513 buf = kmalloc(size, GFP_KERNEL);
2514 if (!buf)
2515 return -ENOMEM;
2516
2517 /* Found some of USB hosts have IOT issues with ours so that we should
2518 * not wait until HCI layer is ready.
2519 */
2520 pipe = usb_rcvctrlpipe(udev, 0);
2521 err = usb_control_msg(udev, pipe, request, USB_TYPE_VENDOR | USB_DIR_IN,
2522 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
2523 if (err < 0) {
2524 BT_ERR("%s: Failed to access otp area (%d)", hdev->name, err);
2525 goto done;
2526 }
2527
2528 memcpy(data, buf, size);
2529
2530done:
2531 kfree(buf);
2532
2533 return err;
2534}
2535
2536static int btusb_setup_qca_download_fw(struct hci_dev *hdev,
2537 const struct firmware *firmware,
2538 size_t hdr_size)
2539{
2540 struct btusb_data *btdata = hci_get_drvdata(hdev);
2541 struct usb_device *udev = btdata->udev;
2542 size_t count, size, sent = 0;
2543 int pipe, len, err;
2544 u8 *buf;
2545
2546 buf = kmalloc(QCA_DFU_PACKET_LEN, GFP_KERNEL);
2547 if (!buf)
2548 return -ENOMEM;
2549
2550 count = firmware->size;
2551
2552 size = min_t(size_t, count, hdr_size);
2553 memcpy(buf, firmware->data, size);
2554
2555 /* USB patches should go down to controller through USB path
2556 * because binary format fits to go down through USB channel.
2557 * USB control path is for patching headers and USB bulk is for
2558 * patch body.
2559 */
2560 pipe = usb_sndctrlpipe(udev, 0);
2561 err = usb_control_msg(udev, pipe, QCA_DFU_DOWNLOAD, USB_TYPE_VENDOR,
2562 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
2563 if (err < 0) {
2564 BT_ERR("%s: Failed to send headers (%d)", hdev->name, err);
2565 goto done;
2566 }
2567
2568 sent += size;
2569 count -= size;
2570
2571 while (count) {
2572 size = min_t(size_t, count, QCA_DFU_PACKET_LEN);
2573
2574 memcpy(buf, firmware->data + sent, size);
2575
2576 pipe = usb_sndbulkpipe(udev, 0x02);
2577 err = usb_bulk_msg(udev, pipe, buf, size, &len,
2578 QCA_DFU_TIMEOUT);
2579 if (err < 0) {
2580 BT_ERR("%s: Failed to send body at %zd of %zd (%d)",
2581 hdev->name, sent, firmware->size, err);
2582 break;
2583 }
2584
2585 if (size != len) {
2586 BT_ERR("%s: Failed to get bulk buffer", hdev->name);
2587 err = -EILSEQ;
2588 break;
2589 }
2590
2591 sent += size;
2592 count -= size;
2593 }
2594
2595done:
2596 kfree(buf);
2597 return err;
2598}
2599
2600static int btusb_setup_qca_load_rampatch(struct hci_dev *hdev,
2601 struct qca_version *ver,
2602 const struct qca_device_info *info)
2603{
2604 struct qca_rampatch_version *rver;
2605 const struct firmware *fw;
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002606 u32 ver_rom, ver_patch;
2607 u16 rver_rom, rver_patch;
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002608 char fwname[64];
2609 int err;
2610
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002611 ver_rom = le32_to_cpu(ver->rom_version);
2612 ver_patch = le32_to_cpu(ver->patch_version);
2613
2614 snprintf(fwname, sizeof(fwname), "qca/rampatch_usb_%08x.bin", ver_rom);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002615
2616 err = request_firmware(&fw, fwname, &hdev->dev);
2617 if (err) {
2618 BT_ERR("%s: failed to request rampatch file: %s (%d)",
2619 hdev->name, fwname, err);
2620 return err;
2621 }
2622
2623 BT_INFO("%s: using rampatch file: %s", hdev->name, fwname);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002624
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002625 rver = (struct qca_rampatch_version *)(fw->data + info->ver_offset);
2626 rver_rom = le16_to_cpu(rver->rom_version);
2627 rver_patch = le16_to_cpu(rver->patch_version);
2628
2629 BT_INFO("%s: QCA: patch rome 0x%x build 0x%x, firmware rome 0x%x "
2630 "build 0x%x", hdev->name, rver_rom, rver_patch, ver_rom,
2631 ver_patch);
2632
2633 if (rver_rom != ver_rom || rver_patch <= ver_patch) {
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002634 BT_ERR("%s: rampatch file version did not match with firmware",
2635 hdev->name);
2636 err = -EINVAL;
2637 goto done;
2638 }
2639
2640 err = btusb_setup_qca_download_fw(hdev, fw, info->rampatch_hdr);
2641
2642done:
2643 release_firmware(fw);
2644
2645 return err;
2646}
2647
2648static int btusb_setup_qca_load_nvm(struct hci_dev *hdev,
2649 struct qca_version *ver,
2650 const struct qca_device_info *info)
2651{
2652 const struct firmware *fw;
2653 char fwname[64];
2654 int err;
2655
2656 snprintf(fwname, sizeof(fwname), "qca/nvm_usb_%08x.bin",
2657 le32_to_cpu(ver->rom_version));
2658
2659 err = request_firmware(&fw, fwname, &hdev->dev);
2660 if (err) {
2661 BT_ERR("%s: failed to request NVM file: %s (%d)",
2662 hdev->name, fwname, err);
2663 return err;
2664 }
2665
2666 BT_INFO("%s: using NVM file: %s", hdev->name, fwname);
2667
2668 err = btusb_setup_qca_download_fw(hdev, fw, info->nvm_hdr);
2669
2670 release_firmware(fw);
2671
2672 return err;
2673}
2674
2675static int btusb_setup_qca(struct hci_dev *hdev)
2676{
2677 const struct qca_device_info *info = NULL;
2678 struct qca_version ver;
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002679 u32 ver_rom;
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002680 u8 status;
2681 int i, err;
2682
2683 err = btusb_qca_send_vendor_req(hdev, QCA_GET_TARGET_VERSION, &ver,
Marcel Holtmanneb500422015-04-16 23:15:50 +02002684 sizeof(ver));
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002685 if (err < 0)
2686 return err;
2687
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002688 ver_rom = le32_to_cpu(ver.rom_version);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002689 for (i = 0; i < ARRAY_SIZE(qca_devices_table); i++) {
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002690 if (ver_rom == qca_devices_table[i].rom_version)
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002691 info = &qca_devices_table[i];
2692 }
2693 if (!info) {
2694 BT_ERR("%s: don't support firmware rome 0x%x", hdev->name,
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002695 ver_rom);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002696 return -ENODEV;
2697 }
2698
2699 err = btusb_qca_send_vendor_req(hdev, QCA_CHECK_STATUS, &status,
2700 sizeof(status));
2701 if (err < 0)
2702 return err;
2703
2704 if (!(status & QCA_PATCH_UPDATED)) {
2705 err = btusb_setup_qca_load_rampatch(hdev, &ver, info);
2706 if (err < 0)
2707 return err;
2708 }
2709
2710 if (!(status & QCA_SYSCFG_UPDATED)) {
2711 err = btusb_setup_qca_load_nvm(hdev, &ver, info);
2712 if (err < 0)
2713 return err;
2714 }
2715
2716 return 0;
2717}
2718
Marcel Holtmann9d08f502015-10-08 20:23:08 +02002719#ifdef CONFIG_BT_HCIBTUSB_BCM
2720static inline int __set_diag_interface(struct hci_dev *hdev)
2721{
2722 struct btusb_data *data = hci_get_drvdata(hdev);
2723 struct usb_interface *intf = data->diag;
2724 int i;
2725
2726 if (!data->diag)
2727 return -ENODEV;
2728
2729 data->diag_tx_ep = NULL;
2730 data->diag_rx_ep = NULL;
2731
2732 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
2733 struct usb_endpoint_descriptor *ep_desc;
2734
2735 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
2736
2737 if (!data->diag_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
2738 data->diag_tx_ep = ep_desc;
2739 continue;
2740 }
2741
2742 if (!data->diag_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
2743 data->diag_rx_ep = ep_desc;
2744 continue;
2745 }
2746 }
2747
2748 if (!data->diag_tx_ep || !data->diag_rx_ep) {
2749 BT_ERR("%s invalid diagnostic descriptors", hdev->name);
2750 return -ENODEV;
2751 }
2752
2753 return 0;
2754}
2755
2756static struct urb *alloc_diag_urb(struct hci_dev *hdev, bool enable)
2757{
2758 struct btusb_data *data = hci_get_drvdata(hdev);
2759 struct sk_buff *skb;
2760 struct urb *urb;
2761 unsigned int pipe;
2762
2763 if (!data->diag_tx_ep)
2764 return ERR_PTR(-ENODEV);
2765
2766 urb = usb_alloc_urb(0, GFP_KERNEL);
2767 if (!urb)
2768 return ERR_PTR(-ENOMEM);
2769
2770 skb = bt_skb_alloc(2, GFP_KERNEL);
2771 if (!skb) {
2772 usb_free_urb(urb);
2773 return ERR_PTR(-ENOMEM);
2774 }
2775
Johannes Berg634fef62017-06-16 14:29:24 +02002776 skb_put_u8(skb, 0xf0);
2777 skb_put_u8(skb, enable);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02002778
2779 pipe = usb_sndbulkpipe(data->udev, data->diag_tx_ep->bEndpointAddress);
2780
2781 usb_fill_bulk_urb(urb, data->udev, pipe,
2782 skb->data, skb->len, btusb_tx_complete, skb);
2783
2784 skb->dev = (void *)hdev;
2785
2786 return urb;
2787}
2788
2789static int btusb_bcm_set_diag(struct hci_dev *hdev, bool enable)
2790{
2791 struct btusb_data *data = hci_get_drvdata(hdev);
2792 struct urb *urb;
2793
2794 if (!data->diag)
2795 return -ENODEV;
2796
2797 if (!test_bit(HCI_RUNNING, &hdev->flags))
2798 return -ENETDOWN;
2799
2800 urb = alloc_diag_urb(hdev, enable);
2801 if (IS_ERR(urb))
2802 return PTR_ERR(urb);
2803
2804 return submit_or_queue_tx_urb(hdev, urb);
2805}
2806#endif
2807
Rajat Jainfd913ef2017-02-01 14:24:09 -08002808#ifdef CONFIG_PM
2809static irqreturn_t btusb_oob_wake_handler(int irq, void *priv)
2810{
2811 struct btusb_data *data = priv;
2812
2813 pm_wakeup_event(&data->udev->dev, 0);
Jeffy Chen017789f2017-02-24 14:24:29 +08002814 pm_system_wakeup();
Rajat Jainfd913ef2017-02-01 14:24:09 -08002815
2816 /* Disable only if not already disabled (keep it balanced) */
2817 if (test_and_clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags)) {
2818 disable_irq_nosync(irq);
2819 disable_irq_wake(irq);
2820 }
2821 return IRQ_HANDLED;
2822}
2823
2824static const struct of_device_id btusb_match_table[] = {
2825 { .compatible = "usb1286,204e" },
2826 { }
2827};
2828MODULE_DEVICE_TABLE(of, btusb_match_table);
2829
2830/* Use an oob wakeup pin? */
2831static int btusb_config_oob_wake(struct hci_dev *hdev)
2832{
2833 struct btusb_data *data = hci_get_drvdata(hdev);
2834 struct device *dev = &data->udev->dev;
2835 int irq, ret;
2836
2837 clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags);
2838
2839 if (!of_match_device(btusb_match_table, dev))
2840 return 0;
2841
2842 /* Move on if no IRQ specified */
2843 irq = of_irq_get_byname(dev->of_node, "wakeup");
2844 if (irq <= 0) {
2845 bt_dev_dbg(hdev, "%s: no OOB Wakeup IRQ in DT", __func__);
2846 return 0;
2847 }
2848
2849 ret = devm_request_irq(&hdev->dev, irq, btusb_oob_wake_handler,
2850 0, "OOB Wake-on-BT", data);
2851 if (ret) {
2852 bt_dev_err(hdev, "%s: IRQ request failed", __func__);
2853 return ret;
2854 }
2855
2856 ret = device_init_wakeup(dev, true);
2857 if (ret) {
2858 bt_dev_err(hdev, "%s: failed to init_wakeup", __func__);
2859 return ret;
2860 }
2861
2862 data->oob_wake_irq = irq;
2863 disable_irq(irq);
2864 bt_dev_info(hdev, "OOB Wake-on-BT configured at IRQ %u", irq);
2865 return 0;
2866}
2867#endif
2868
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002869static int btusb_probe(struct usb_interface *intf,
Marcel Holtmann89e75332014-09-16 04:44:50 +02002870 const struct usb_device_id *id)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002871{
2872 struct usb_endpoint_descriptor *ep_desc;
2873 struct btusb_data *data;
2874 struct hci_dev *hdev;
Marcel Holtmann22f8e9d2015-10-20 00:53:33 +02002875 unsigned ifnum_base;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002876 int i, err;
2877
2878 BT_DBG("intf %p id %p", intf, id);
2879
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002880 /* interface numbers are hardcoded in the spec */
Marcel Holtmann22f8e9d2015-10-20 00:53:33 +02002881 if (intf->cur_altsetting->desc.bInterfaceNumber != 0) {
2882 if (!(id->driver_info & BTUSB_IFNUM_2))
2883 return -ENODEV;
2884 if (intf->cur_altsetting->desc.bInterfaceNumber != 2)
2885 return -ENODEV;
2886 }
2887
2888 ifnum_base = intf->cur_altsetting->desc.bInterfaceNumber;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002889
2890 if (!id->driver_info) {
2891 const struct usb_device_id *match;
Marcel Holtmann89e75332014-09-16 04:44:50 +02002892
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002893 match = usb_match_id(intf, blacklist_table);
2894 if (match)
2895 id = match;
2896 }
2897
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002898 if (id->driver_info == BTUSB_IGNORE)
2899 return -ENODEV;
2900
Steven.Li2d25f8b2011-07-01 14:02:36 +08002901 if (id->driver_info & BTUSB_ATH3012) {
2902 struct usb_device *udev = interface_to_usbdev(intf);
2903
2904 /* Old firmware would otherwise let ath3k driver load
Derek Robsond98422c2017-07-22 13:47:07 +12002905 * patch and sysconfig files
2906 */
Steven.Li2d25f8b2011-07-01 14:02:36 +08002907 if (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x0001)
2908 return -ENODEV;
2909 }
2910
Sachin Kamat98921db2012-07-27 12:38:39 +05302911 data = devm_kzalloc(&intf->dev, sizeof(*data), GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002912 if (!data)
2913 return -ENOMEM;
2914
2915 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
2916 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
2917
2918 if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
2919 data->intr_ep = ep_desc;
2920 continue;
2921 }
2922
2923 if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
2924 data->bulk_tx_ep = ep_desc;
2925 continue;
2926 }
2927
2928 if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
2929 data->bulk_rx_ep = ep_desc;
2930 continue;
2931 }
2932 }
2933
Sachin Kamat98921db2012-07-27 12:38:39 +05302934 if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002935 return -ENODEV;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002936
Marcel Holtmann893ba542015-01-28 20:27:34 -08002937 if (id->driver_info & BTUSB_AMP) {
2938 data->cmdreq_type = USB_TYPE_CLASS | 0x01;
2939 data->cmdreq = 0x2b;
2940 } else {
2941 data->cmdreq_type = USB_TYPE_CLASS;
2942 data->cmdreq = 0x00;
2943 }
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002944
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002945 data->udev = interface_to_usbdev(intf);
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02002946 data->intf = intf;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002947
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002948 INIT_WORK(&data->work, btusb_work);
Oliver Neukum7bee5492009-08-24 23:44:59 +02002949 INIT_WORK(&data->waker, btusb_waker);
Marcel Holtmann803b5832014-09-16 08:00:29 +02002950 init_usb_anchor(&data->deferred);
2951 init_usb_anchor(&data->tx_anchor);
Oliver Neukum7bee5492009-08-24 23:44:59 +02002952 spin_lock_init(&data->txlock);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002953
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002954 init_usb_anchor(&data->intr_anchor);
2955 init_usb_anchor(&data->bulk_anchor);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02002956 init_usb_anchor(&data->isoc_anchor);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02002957 init_usb_anchor(&data->diag_anchor);
Marcel Holtmann803b5832014-09-16 08:00:29 +02002958 spin_lock_init(&data->rxlock);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002959
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002960 if (id->driver_info & BTUSB_INTEL_NEW) {
2961 data->recv_event = btusb_recv_event_intel;
2962 data->recv_bulk = btusb_recv_bulk_intel;
2963 set_bit(BTUSB_BOOTLOADER, &data->flags);
2964 } else {
2965 data->recv_event = hci_recv_frame;
2966 data->recv_bulk = btusb_recv_bulk;
2967 }
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +01002968
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002969 hdev = hci_alloc_dev();
Sachin Kamat98921db2012-07-27 12:38:39 +05302970 if (!hdev)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002971 return -ENOMEM;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002972
Marcel Holtmannc13854c2010-02-08 15:27:07 +01002973 hdev->bus = HCI_USB;
David Herrmann155961e2012-02-09 21:58:32 +01002974 hci_set_drvdata(hdev, data);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002975
Marcel Holtmann893ba542015-01-28 20:27:34 -08002976 if (id->driver_info & BTUSB_AMP)
2977 hdev->dev_type = HCI_AMP;
2978 else
Marcel Holtmannca8bee52016-07-05 14:30:14 +02002979 hdev->dev_type = HCI_PRIMARY;
Marcel Holtmann893ba542015-01-28 20:27:34 -08002980
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002981 data->hdev = hdev;
2982
2983 SET_HCIDEV_DEV(hdev, &intf->dev);
2984
Marcel Holtmann9f8f9622013-04-10 08:11:35 -07002985 hdev->open = btusb_open;
2986 hdev->close = btusb_close;
2987 hdev->flush = btusb_flush;
2988 hdev->send = btusb_send_frame;
2989 hdev->notify = btusb_notify;
2990
Rajat Jainfd913ef2017-02-01 14:24:09 -08002991#ifdef CONFIG_PM
2992 err = btusb_config_oob_wake(hdev);
2993 if (err)
2994 goto out_free_dev;
Rajat Jaina4ccc9e2017-02-01 14:24:10 -08002995
2996 /* Marvell devices may need a specific chip configuration */
2997 if (id->driver_info & BTUSB_MARVELL && data->oob_wake_irq) {
2998 err = marvell_config_oob_wake(hdev);
2999 if (err)
3000 goto out_free_dev;
3001 }
Rajat Jainfd913ef2017-02-01 14:24:09 -08003002#endif
Szymon Janc418678b2016-09-01 17:22:37 +02003003 if (id->driver_info & BTUSB_CW6622)
3004 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
3005
Marcel Holtmann6c9d4352015-10-17 14:39:27 +02003006 if (id->driver_info & BTUSB_BCM2045)
3007 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
3008
Marcel Holtmann9f8f9622013-04-10 08:11:35 -07003009 if (id->driver_info & BTUSB_BCM92035)
3010 hdev->setup = btusb_setup_bcm92035;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003011
Marcel Holtmannc2bfb102015-04-05 22:52:14 -07003012#ifdef CONFIG_BT_HCIBTUSB_BCM
Marcel Holtmannabbaf502014-07-02 00:53:48 +02003013 if (id->driver_info & BTUSB_BCM_PATCHRAM) {
Marcel Holtmann49a5f782015-10-20 02:30:48 +02003014 hdev->manufacturer = 15;
Marcel Holtmannc2bfb102015-04-05 22:52:14 -07003015 hdev->setup = btbcm_setup_patchram;
Marcel Holtmann9d08f502015-10-08 20:23:08 +02003016 hdev->set_diag = btusb_bcm_set_diag;
Marcel Holtmann1df1f592015-04-05 22:52:11 -07003017 hdev->set_bdaddr = btbcm_set_bdaddr;
Marcel Holtmann9d08f502015-10-08 20:23:08 +02003018
3019 /* Broadcom LM_DIAG Interface numbers are hardcoded */
Marcel Holtmann22f8e9d2015-10-20 00:53:33 +02003020 data->diag = usb_ifnum_to_if(data->udev, ifnum_base + 2);
Marcel Holtmannabbaf502014-07-02 00:53:48 +02003021 }
Petri Gynther10d4c672014-05-08 15:50:01 -07003022
Marcel Holtmann9d08f502015-10-08 20:23:08 +02003023 if (id->driver_info & BTUSB_BCM_APPLE) {
Marcel Holtmann49a5f782015-10-20 02:30:48 +02003024 hdev->manufacturer = 15;
Marcel Holtmannc2bfb102015-04-05 22:52:14 -07003025 hdev->setup = btbcm_setup_apple;
Marcel Holtmann9d08f502015-10-08 20:23:08 +02003026 hdev->set_diag = btusb_bcm_set_diag;
3027
3028 /* Broadcom LM_DIAG Interface numbers are hardcoded */
Marcel Holtmann22f8e9d2015-10-20 00:53:33 +02003029 data->diag = usb_ifnum_to_if(data->udev, ifnum_base + 2);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02003030 }
Marcel Holtmannc2bfb102015-04-05 22:52:14 -07003031#endif
Marcel Holtmann17b27722015-03-22 15:52:38 +01003032
Marcel Holtmanncb8d6592014-07-02 11:25:25 +02003033 if (id->driver_info & BTUSB_INTEL) {
Marcel Holtmann49a5f782015-10-20 02:30:48 +02003034 hdev->manufacturer = 2;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07003035 hdev->setup = btusb_setup_intel;
Tedd Ho-Jeong Anbfbd45e2015-02-13 09:20:52 -08003036 hdev->shutdown = btusb_shutdown_intel;
Marcel Holtmann3e247672015-10-17 16:00:28 +02003037 hdev->set_diag = btintel_set_diag_mfg;
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07003038 hdev->set_bdaddr = btintel_set_bdaddr;
Jakub Pawlowskic33fb9b2015-01-30 18:55:58 -08003039 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
Jakub Pawlowskic1154842015-03-17 09:04:16 -07003040 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
Marcel Holtmann3e247672015-10-17 16:00:28 +02003041 set_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks);
Marcel Holtmanncb8d6592014-07-02 11:25:25 +02003042 }
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07003043
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08003044 if (id->driver_info & BTUSB_INTEL_NEW) {
Marcel Holtmann49a5f782015-10-20 02:30:48 +02003045 hdev->manufacturer = 2;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08003046 hdev->send = btusb_send_frame_intel;
3047 hdev->setup = btusb_setup_intel_new;
Marcel Holtmanneeb6abe2015-07-05 14:37:39 +02003048 hdev->hw_error = btintel_hw_error;
Marcel Holtmann6d2e50d2015-10-09 14:42:08 +02003049 hdev->set_diag = btintel_set_diag;
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07003050 hdev->set_bdaddr = btintel_set_bdaddr;
Marcel Holtmannb970c5b2015-02-01 23:57:18 -08003051 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
Marcel Holtmannd8270fb2015-10-17 16:00:27 +02003052 set_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08003053 }
3054
Amitkumar Karwarae8df492014-07-18 14:47:06 -07003055 if (id->driver_info & BTUSB_MARVELL)
3056 hdev->set_bdaddr = btusb_set_bdaddr_marvell;
3057
Marcel Holtmann661cf882015-01-02 23:35:20 -08003058 if (id->driver_info & BTUSB_SWAVE) {
3059 set_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks);
Marcel Holtmannd57dbe72014-12-26 04:42:33 +01003060 set_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS, &hdev->quirks);
Marcel Holtmann661cf882015-01-02 23:35:20 -08003061 }
Marcel Holtmannd57dbe72014-12-26 04:42:33 +01003062
Marcel Holtmanne4c534b2015-10-21 01:31:45 +02003063 if (id->driver_info & BTUSB_INTEL_BOOT) {
3064 hdev->manufacturer = 2;
Marcel Holtmann40df7832014-07-06 13:29:58 +02003065 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
Marcel Holtmanne4c534b2015-10-21 01:31:45 +02003066 }
Marcel Holtmann40df7832014-07-06 13:29:58 +02003067
Jakub Pawlowski79f0c872015-01-29 10:38:34 -08003068 if (id->driver_info & BTUSB_ATH3012) {
Toshi Kikuchi58592232014-12-12 10:58:05 -08003069 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
Jakub Pawlowski3d50d512015-03-17 09:04:15 -07003070 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
Jakub Pawlowski79f0c872015-01-29 10:38:34 -08003071 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
3072 }
Toshi Kikuchi58592232014-12-12 10:58:05 -08003073
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003074 if (id->driver_info & BTUSB_QCA_ROME) {
3075 data->setup_on_usb = btusb_setup_qca;
3076 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
Leif Liddyfd865802017-07-08 20:55:32 +02003077
3078 /* QCA Rome devices lose their updated firmware over suspend,
3079 * but the USB hub doesn't notice any status change.
3080 * Explicitly request a device reset on resume.
3081 */
3082 set_bit(BTUSB_RESET_RESUME, &data->flags);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003083 }
3084
Carlo Caionedb33c772015-05-14 10:49:09 +02003085#ifdef CONFIG_BT_HCIBTUSB_RTL
Daniel Drake04b8c812015-05-21 08:23:50 -06003086 if (id->driver_info & BTUSB_REALTEK) {
Carlo Caionedb33c772015-05-14 10:49:09 +02003087 hdev->setup = btrtl_setup_realtek;
Daniel Drake04b8c812015-05-21 08:23:50 -06003088
3089 /* Realtek devices lose their updated firmware over suspend,
3090 * but the USB hub doesn't notice any status change.
3091 * Explicitly request a device reset on resume.
3092 */
3093 set_bit(BTUSB_RESET_RESUME, &data->flags);
3094 }
Carlo Caionedb33c772015-05-14 10:49:09 +02003095#endif
Daniel Drakea2698a92015-04-16 14:09:55 -06003096
Marcel Holtmann893ba542015-01-28 20:27:34 -08003097 if (id->driver_info & BTUSB_AMP) {
3098 /* AMP controllers do not support SCO packets */
3099 data->isoc = NULL;
3100 } else {
Marcel Holtmann22f8e9d2015-10-20 00:53:33 +02003101 /* Interface orders are hardcoded in the specification */
3102 data->isoc = usb_ifnum_to_if(data->udev, ifnum_base + 1);
Marcel Holtmann893ba542015-01-28 20:27:34 -08003103 }
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02003104
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01003105 if (!reset)
Szymon Janca6c511c2012-05-23 12:35:46 +02003106 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02003107
3108 if (force_scofix || id->driver_info & BTUSB_WRONG_SCO_MTU) {
3109 if (!disable_scofix)
3110 set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
3111 }
3112
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02003113 if (id->driver_info & BTUSB_BROKEN_ISOC)
3114 data->isoc = NULL;
3115
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01003116 if (id->driver_info & BTUSB_DIGIANSWER) {
3117 data->cmdreq_type = USB_TYPE_VENDOR;
Szymon Janca6c511c2012-05-23 12:35:46 +02003118 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01003119 }
3120
3121 if (id->driver_info & BTUSB_CSR) {
3122 struct usb_device *udev = data->udev;
Marcel Holtmann81cac642014-01-03 03:02:36 -08003123 u16 bcdDevice = le16_to_cpu(udev->descriptor.bcdDevice);
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01003124
3125 /* Old firmware would otherwise execute USB reset */
Marcel Holtmann81cac642014-01-03 03:02:36 -08003126 if (bcdDevice < 0x117)
Szymon Janca6c511c2012-05-23 12:35:46 +02003127 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmann81cac642014-01-03 03:02:36 -08003128
3129 /* Fake CSR devices with broken commands */
Johan Hedberg6cafcd92015-08-30 21:47:21 +03003130 if (bcdDevice <= 0x100 || bcdDevice == 0x134)
Marcel Holtmann81cac642014-01-03 03:02:36 -08003131 hdev->setup = btusb_setup_csr;
Jakub Pawlowski49c989a2015-03-17 09:04:17 -07003132
3133 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01003134 }
3135
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02003136 if (id->driver_info & BTUSB_SNIFFER) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02003137 struct usb_device *udev = data->udev;
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02003138
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01003139 /* New sniffer firmware has crippled HCI interface */
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02003140 if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
3141 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
3142 }
3143
Marcel Holtmann3a5ef202014-07-06 14:53:54 +02003144 if (id->driver_info & BTUSB_INTEL_BOOT) {
3145 /* A bug in the bootloader causes that interrupt interface is
3146 * only enabled after receiving SetInterface(0, AltSetting=0).
3147 */
3148 err = usb_set_interface(data->udev, 0, 0);
3149 if (err < 0) {
3150 BT_ERR("failed to set interface 0, alt 0 %d", err);
Rajat Jain10ab1332017-02-01 14:24:08 -08003151 goto out_free_dev;
Marcel Holtmann3a5ef202014-07-06 14:53:54 +02003152 }
3153 }
3154
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02003155 if (data->isoc) {
3156 err = usb_driver_claim_interface(&btusb_driver,
Marcel Holtmann89e75332014-09-16 04:44:50 +02003157 data->isoc, data);
Rajat Jain10ab1332017-02-01 14:24:08 -08003158 if (err < 0)
3159 goto out_free_dev;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02003160 }
3161
Marcel Holtmann9d08f502015-10-08 20:23:08 +02003162#ifdef CONFIG_BT_HCIBTUSB_BCM
3163 if (data->diag) {
3164 if (!usb_driver_claim_interface(&btusb_driver,
3165 data->diag, data))
3166 __set_diag_interface(hdev);
3167 else
3168 data->diag = NULL;
3169 }
3170#endif
3171
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003172 err = hci_register_dev(hdev);
Rajat Jain10ab1332017-02-01 14:24:08 -08003173 if (err < 0)
3174 goto out_free_dev;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003175
3176 usb_set_intfdata(intf, data);
3177
3178 return 0;
Rajat Jain10ab1332017-02-01 14:24:08 -08003179
3180out_free_dev:
3181 hci_free_dev(hdev);
3182 return err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003183}
3184
3185static void btusb_disconnect(struct usb_interface *intf)
3186{
3187 struct btusb_data *data = usb_get_intfdata(intf);
3188 struct hci_dev *hdev;
3189
3190 BT_DBG("intf %p", intf);
3191
3192 if (!data)
3193 return;
3194
3195 hdev = data->hdev;
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02003196 usb_set_intfdata(data->intf, NULL);
3197
3198 if (data->isoc)
3199 usb_set_intfdata(data->isoc, NULL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003200
Marcel Holtmann9d08f502015-10-08 20:23:08 +02003201 if (data->diag)
3202 usb_set_intfdata(data->diag, NULL);
3203
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003204 hci_unregister_dev(hdev);
3205
Marcel Holtmann9d08f502015-10-08 20:23:08 +02003206 if (intf == data->intf) {
3207 if (data->isoc)
3208 usb_driver_release_interface(&btusb_driver, data->isoc);
3209 if (data->diag)
3210 usb_driver_release_interface(&btusb_driver, data->diag);
3211 } else if (intf == data->isoc) {
3212 if (data->diag)
3213 usb_driver_release_interface(&btusb_driver, data->diag);
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02003214 usb_driver_release_interface(&btusb_driver, data->intf);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02003215 } else if (intf == data->diag) {
3216 usb_driver_release_interface(&btusb_driver, data->intf);
3217 if (data->isoc)
3218 usb_driver_release_interface(&btusb_driver, data->isoc);
3219 }
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02003220
Rajat Jainfd913ef2017-02-01 14:24:09 -08003221 if (data->oob_wake_irq)
3222 device_init_wakeup(&data->udev->dev, false);
3223
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003224 hci_free_dev(hdev);
3225}
3226
Oliver Neukum7bee5492009-08-24 23:44:59 +02003227#ifdef CONFIG_PM
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003228static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
3229{
3230 struct btusb_data *data = usb_get_intfdata(intf);
3231
3232 BT_DBG("intf %p", intf);
3233
3234 if (data->suspend_count++)
3235 return 0;
3236
Oliver Neukum7bee5492009-08-24 23:44:59 +02003237 spin_lock_irq(&data->txlock);
Alan Stern5b1b0b82011-08-19 23:49:48 +02003238 if (!(PMSG_IS_AUTO(message) && data->tx_in_flight)) {
Oliver Neukum7bee5492009-08-24 23:44:59 +02003239 set_bit(BTUSB_SUSPENDING, &data->flags);
3240 spin_unlock_irq(&data->txlock);
3241 } else {
3242 spin_unlock_irq(&data->txlock);
3243 data->suspend_count--;
3244 return -EBUSY;
3245 }
3246
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003247 cancel_work_sync(&data->work);
3248
Oliver Neukum7bee5492009-08-24 23:44:59 +02003249 btusb_stop_traffic(data);
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003250 usb_kill_anchored_urbs(&data->tx_anchor);
3251
Rajat Jainfd913ef2017-02-01 14:24:09 -08003252 if (data->oob_wake_irq && device_may_wakeup(&data->udev->dev)) {
3253 set_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags);
3254 enable_irq_wake(data->oob_wake_irq);
3255 enable_irq(data->oob_wake_irq);
3256 }
3257
Daniel Drake04b8c812015-05-21 08:23:50 -06003258 /* Optionally request a device reset on resume, but only when
3259 * wakeups are disabled. If wakeups are enabled we assume the
3260 * device will stay powered up throughout suspend.
3261 */
3262 if (test_bit(BTUSB_RESET_RESUME, &data->flags) &&
3263 !device_may_wakeup(&data->udev->dev))
3264 data->udev->reset_resume = 1;
3265
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003266 return 0;
3267}
3268
Oliver Neukum7bee5492009-08-24 23:44:59 +02003269static void play_deferred(struct btusb_data *data)
3270{
3271 struct urb *urb;
3272 int err;
3273
3274 while ((urb = usb_get_from_anchor(&data->deferred))) {
Jeffy Chen19cfe912017-07-20 18:53:50 +08003275 usb_anchor_urb(urb, &data->tx_anchor);
3276
Oliver Neukum7bee5492009-08-24 23:44:59 +02003277 err = usb_submit_urb(urb, GFP_ATOMIC);
Jeffy Chen19cfe912017-07-20 18:53:50 +08003278 if (err < 0) {
3279 if (err != -EPERM && err != -ENODEV)
3280 BT_ERR("%s urb %p submission failed (%d)",
3281 data->hdev->name, urb, -err);
3282 kfree(urb->setup_packet);
3283 usb_unanchor_urb(urb);
3284 usb_free_urb(urb);
Oliver Neukum7bee5492009-08-24 23:44:59 +02003285 break;
Jeffy Chen19cfe912017-07-20 18:53:50 +08003286 }
Oliver Neukum7bee5492009-08-24 23:44:59 +02003287
3288 data->tx_in_flight++;
Jeffy Chen19cfe912017-07-20 18:53:50 +08003289 usb_free_urb(urb);
Oliver Neukum7bee5492009-08-24 23:44:59 +02003290 }
Jeffy Chen19cfe912017-07-20 18:53:50 +08003291
3292 /* Cleanup the rest deferred urbs. */
3293 while ((urb = usb_get_from_anchor(&data->deferred))) {
3294 kfree(urb->setup_packet);
3295 usb_free_urb(urb);
3296 }
Oliver Neukum7bee5492009-08-24 23:44:59 +02003297}
3298
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003299static int btusb_resume(struct usb_interface *intf)
3300{
3301 struct btusb_data *data = usb_get_intfdata(intf);
3302 struct hci_dev *hdev = data->hdev;
Oliver Neukum7bee5492009-08-24 23:44:59 +02003303 int err = 0;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003304
3305 BT_DBG("intf %p", intf);
3306
3307 if (--data->suspend_count)
3308 return 0;
3309
Rajat Jainfd913ef2017-02-01 14:24:09 -08003310 /* Disable only if not already disabled (keep it balanced) */
3311 if (test_and_clear_bit(BTUSB_OOB_WAKE_ENABLED, &data->flags)) {
3312 disable_irq(data->oob_wake_irq);
3313 disable_irq_wake(data->oob_wake_irq);
3314 }
3315
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003316 if (!test_bit(HCI_RUNNING, &hdev->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +02003317 goto done;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003318
3319 if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
3320 err = btusb_submit_intr_urb(hdev, GFP_NOIO);
3321 if (err < 0) {
3322 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02003323 goto failed;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003324 }
3325 }
3326
3327 if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) {
Marcel Holtmann43c2e572009-02-04 17:41:38 +01003328 err = btusb_submit_bulk_urb(hdev, GFP_NOIO);
3329 if (err < 0) {
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003330 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02003331 goto failed;
3332 }
3333
3334 btusb_submit_bulk_urb(hdev, GFP_NOIO);
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003335 }
3336
3337 if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
3338 if (btusb_submit_isoc_urb(hdev, GFP_NOIO) < 0)
3339 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
3340 else
3341 btusb_submit_isoc_urb(hdev, GFP_NOIO);
3342 }
3343
Oliver Neukum7bee5492009-08-24 23:44:59 +02003344 spin_lock_irq(&data->txlock);
3345 play_deferred(data);
3346 clear_bit(BTUSB_SUSPENDING, &data->flags);
3347 spin_unlock_irq(&data->txlock);
3348 schedule_work(&data->work);
3349
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003350 return 0;
Oliver Neukum7bee5492009-08-24 23:44:59 +02003351
3352failed:
3353 usb_scuttle_anchored_urbs(&data->deferred);
3354done:
3355 spin_lock_irq(&data->txlock);
3356 clear_bit(BTUSB_SUSPENDING, &data->flags);
3357 spin_unlock_irq(&data->txlock);
3358
3359 return err;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003360}
Oliver Neukum7bee5492009-08-24 23:44:59 +02003361#endif
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003362
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003363static struct usb_driver btusb_driver = {
3364 .name = "btusb",
3365 .probe = btusb_probe,
3366 .disconnect = btusb_disconnect,
Oliver Neukum7bee5492009-08-24 23:44:59 +02003367#ifdef CONFIG_PM
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003368 .suspend = btusb_suspend,
3369 .resume = btusb_resume,
Oliver Neukum7bee5492009-08-24 23:44:59 +02003370#endif
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003371 .id_table = btusb_table,
Oliver Neukum7bee5492009-08-24 23:44:59 +02003372 .supports_autosuspend = 1,
Sarah Sharpe1f12eb2012-04-23 10:08:51 -07003373 .disable_hub_initiated_lpm = 1,
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003374};
3375
Greg Kroah-Hartman93f15082011-11-18 09:47:34 -08003376module_usb_driver(btusb_driver);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003377
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02003378module_param(disable_scofix, bool, 0644);
3379MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
3380
3381module_param(force_scofix, bool, 0644);
3382MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
3383
3384module_param(reset, bool, 0644);
3385MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
3386
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003387MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
3388MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
3389MODULE_VERSION(VERSION);
3390MODULE_LICENSE("GPL");