blob: de7b236eeae7777f71389ec42c233525d309aef2 [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>
Marcel Holtmann5e23b922007-10-20 14:12:34 +020027
28#include <net/bluetooth/bluetooth.h>
29#include <net/bluetooth/hci_core.h>
30
Marcel Holtmann4185a0f2015-04-06 00:52:12 -070031#include "btintel.h"
Marcel Holtmann1df1f592015-04-05 22:52:11 -070032#include "btbcm.h"
33
Marcel Holtmann34dced9b2015-04-05 22:52:16 -070034#define VERSION "0.8"
Marcel Holtmanncfeb4142008-08-07 22:26:56 +020035
Rusty Russell90ab5ee2012-01-13 09:32:20 +103036static bool disable_scofix;
37static bool force_scofix;
Marcel Holtmann7a9d4022008-11-30 12:17:26 +010038
Rusty Russell90ab5ee2012-01-13 09:32:20 +103039static bool reset = 1;
Marcel Holtmanncfeb4142008-08-07 22:26:56 +020040
41static struct usb_driver btusb_driver;
42
43#define BTUSB_IGNORE 0x01
Marcel Holtmann7a9d4022008-11-30 12:17:26 +010044#define BTUSB_DIGIANSWER 0x02
45#define BTUSB_CSR 0x04
46#define BTUSB_SNIFFER 0x08
47#define BTUSB_BCM92035 0x10
48#define BTUSB_BROKEN_ISOC 0x20
49#define BTUSB_WRONG_SCO_MTU 0x40
Steven.Li2d25f8b2011-07-01 14:02:36 +080050#define BTUSB_ATH3012 0x80
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -070051#define BTUSB_INTEL 0x100
Marcel Holtmann40df7832014-07-06 13:29:58 +020052#define BTUSB_INTEL_BOOT 0x200
53#define BTUSB_BCM_PATCHRAM 0x400
Amitkumar Karwarae8df492014-07-18 14:47:06 -070054#define BTUSB_MARVELL 0x800
Marcel Holtmann4fcef8e2015-01-01 17:34:37 -080055#define BTUSB_SWAVE 0x1000
Marcel Holtmanncda0dd72015-01-26 21:33:48 -080056#define BTUSB_INTEL_NEW 0x2000
Marcel Holtmann893ba542015-01-28 20:27:34 -080057#define BTUSB_AMP 0x4000
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +000058#define BTUSB_QCA_ROME 0x8000
Marcel Holtmann17b27722015-03-22 15:52:38 +010059#define BTUSB_BCM_APPLE 0x10000
Marcel Holtmann5e23b922007-10-20 14:12:34 +020060
Marcel Holtmann54265202013-10-11 07:46:18 -070061static const struct usb_device_id btusb_table[] = {
Marcel Holtmann5e23b922007-10-20 14:12:34 +020062 /* Generic Bluetooth USB device */
63 { USB_DEVICE_INFO(0xe0, 0x01, 0x01) },
64
Marcel Holtmann893ba542015-01-28 20:27:34 -080065 /* Generic Bluetooth AMP device */
66 { USB_DEVICE_INFO(0xe0, 0x01, 0x04), .driver_info = BTUSB_AMP },
67
Henrik Rydberg1fa65352012-08-25 19:28:06 +020068 /* Apple-specific (Broadcom) devices */
Marcel Holtmann17b27722015-03-22 15:52:38 +010069 { USB_VENDOR_AND_INTERFACE_INFO(0x05ac, 0xff, 0x01, 0x01),
70 .driver_info = BTUSB_BCM_APPLE },
Henrik Rydberg1fa65352012-08-25 19:28:06 +020071
Cho, Yu-Chen178c0592013-06-04 21:40:26 +080072 /* MediaTek MT76x0E */
73 { USB_DEVICE(0x0e8d, 0x763f) },
74
Oliver Neukumc510eae2011-09-21 11:41:45 +020075 /* Broadcom SoftSailing reporting vendor specific */
Don Zickus2e8b50632012-03-28 16:41:11 -040076 { USB_DEVICE(0x0a5c, 0x21e1) },
Oliver Neukumc510eae2011-09-21 11:41:45 +020077
Nobuhiro Iwamatsu3cd01972010-08-20 16:24:07 +090078 /* Apple MacBookPro 7,1 */
79 { USB_DEVICE(0x05ac, 0x8213) },
80
Cyril Lacoux0a79f672010-07-14 10:29:27 +040081 /* Apple iMac11,1 */
82 { USB_DEVICE(0x05ac, 0x8215) },
83
Nobuhiro Iwamatsu9c047152010-08-20 16:24:06 +090084 /* Apple MacBookPro6,2 */
85 { USB_DEVICE(0x05ac, 0x8218) },
86
Edgar (gimli) Hucek3e3ede72010-11-04 08:04:33 +010087 /* Apple MacBookAir3,1, MacBookAir3,2 */
88 { USB_DEVICE(0x05ac, 0x821b) },
89
Pieter-Augustijn Van Malleghema63b7232011-09-07 02:28:10 -040090 /* Apple MacBookAir4,1 */
91 { USB_DEVICE(0x05ac, 0x821f) },
92
Marc-Antoine Perennou88d377b2011-03-24 14:51:21 -030093 /* Apple MacBookPro8,2 */
94 { USB_DEVICE(0x05ac, 0x821a) },
95
Jurgen Kramerf78b68262011-09-04 18:01:42 +020096 /* Apple MacMini5,1 */
97 { USB_DEVICE(0x05ac, 0x8281) },
98
Marcel Holtmanncfeb4142008-08-07 22:26:56 +020099 /* AVM BlueFRITZ! USB v2.0 */
Marcel Holtmann4fcef8e2015-01-01 17:34:37 -0800100 { USB_DEVICE(0x057c, 0x3800), .driver_info = BTUSB_SWAVE },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200101
102 /* Bluetooth Ultraport Module from IBM */
103 { USB_DEVICE(0x04bf, 0x030a) },
104
105 /* ALPS Modules with non-standard id */
106 { USB_DEVICE(0x044e, 0x3001) },
107 { USB_DEVICE(0x044e, 0x3002) },
108
109 /* Ericsson with non-standard id */
110 { USB_DEVICE(0x0bdb, 0x1002) },
111
112 /* Canyon CN-BTU1 with HID interfaces */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100113 { USB_DEVICE(0x0c10, 0x0000) },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200114
Wen-chien Jesse Sungd13431c2011-11-08 14:30:22 +0800115 /* Broadcom BCM20702A0 */
116 { USB_DEVICE(0x413c, 0x8197) },
117
Marcel Holtmannd049f4e2015-01-26 20:35:32 -0800118 /* Broadcom BCM20702B0 (Dynex/Insignia) */
119 { USB_DEVICE(0x19ff, 0x0239), .driver_info = BTUSB_BCM_PATCHRAM },
120
Steven Harms98514032012-04-13 14:45:55 -0400121 /* Foxconn - Hon Hai */
Heinrich Siebmanns6029ddc2014-12-03 19:32:22 +0100122 { USB_VENDOR_AND_INTERFACE_INFO(0x0489, 0xff, 0x01, 0x01),
123 .driver_info = BTUSB_BCM_PATCHRAM },
Steven Harms98514032012-04-13 14:45:55 -0400124
Matej Dubovy8f0c3042015-02-02 18:50:14 +0100125 /* Lite-On Technology - Broadcom based */
126 { USB_VENDOR_AND_INTERFACE_INFO(0x04ca, 0xff, 0x01, 0x01),
127 .driver_info = BTUSB_BCM_PATCHRAM },
128
Andy Shevchenko0b880062014-02-18 18:26:19 +0200129 /* Broadcom devices with vendor specific id */
Petri Gynther10d4c672014-05-08 15:50:01 -0700130 { USB_VENDOR_AND_INTERFACE_INFO(0x0a5c, 0xff, 0x01, 0x01),
131 .driver_info = BTUSB_BCM_PATCHRAM },
Gustavo Padovan92c385f2012-08-06 15:36:49 -0300132
Marcel Holtmannc2aef6e2014-07-21 14:02:33 +0200133 /* ASUSTek Computer - Broadcom based */
Rick Dunn9a5abdaa2015-01-17 05:29:12 +0100134 { USB_VENDOR_AND_INTERFACE_INFO(0x0b05, 0xff, 0x01, 0x01),
135 .driver_info = BTUSB_BCM_PATCHRAM },
Marcel Holtmannc2aef6e2014-07-21 14:02:33 +0200136
Ken O'Brien5bcecf32013-09-21 19:14:43 +0100137 /* Belkin F8065bf - Broadcom based */
Marcel Holtmann6331c682015-03-27 15:11:41 -0700138 { USB_VENDOR_AND_INTERFACE_INFO(0x050d, 0xff, 0x01, 0x01),
139 .driver_info = BTUSB_BCM_PATCHRAM },
Ken O'Brien5bcecf32013-09-21 19:14:43 +0100140
Jurgen Kramer9113bfd2014-02-15 12:01:09 +0100141 /* IMC Networks - Broadcom based */
Marcel Holtmann6331c682015-03-27 15:11:41 -0700142 { USB_VENDOR_AND_INTERFACE_INFO(0x13d3, 0xff, 0x01, 0x01),
143 .driver_info = BTUSB_BCM_PATCHRAM },
Jurgen Kramer9113bfd2014-02-15 12:01:09 +0100144
Marcel Holtmann40df7832014-07-06 13:29:58 +0200145 /* Intel Bluetooth USB Bootloader (RAM module) */
Marcel Holtmannd92f2df2014-07-06 14:53:55 +0200146 { USB_DEVICE(0x8087, 0x0a5a),
147 .driver_info = BTUSB_INTEL_BOOT | BTUSB_BROKEN_ISOC },
Marcel Holtmann40df7832014-07-06 13:29:58 +0200148
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200149 { } /* Terminating entry */
150};
151
152MODULE_DEVICE_TABLE(usb, btusb_table);
153
Marcel Holtmann54265202013-10-11 07:46:18 -0700154static const struct usb_device_id blacklist_table[] = {
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200155 /* CSR BlueCore devices */
156 { USB_DEVICE(0x0a12, 0x0001), .driver_info = BTUSB_CSR },
157
158 /* Broadcom BCM2033 without firmware */
159 { USB_DEVICE(0x0a5c, 0x2033), .driver_info = BTUSB_IGNORE },
160
Bala Shanmugambe931122010-11-26 17:35:46 +0530161 /* Atheros 3011 with sflash firmware */
Andy Shevchenko0b880062014-02-18 18:26:19 +0200162 { USB_DEVICE(0x0489, 0xe027), .driver_info = BTUSB_IGNORE },
163 { USB_DEVICE(0x0489, 0xe03d), .driver_info = BTUSB_IGNORE },
Alexander Ploumistos2eeff0b2015-02-13 21:05:11 +0200164 { USB_DEVICE(0x04f2, 0xaff1), .driver_info = BTUSB_IGNORE },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200165 { USB_DEVICE(0x0930, 0x0215), .driver_info = BTUSB_IGNORE },
Bala Shanmugambe931122010-11-26 17:35:46 +0530166 { USB_DEVICE(0x0cf3, 0x3002), .driver_info = BTUSB_IGNORE },
Marek Vasut6eda5412012-06-08 14:32:50 +0200167 { USB_DEVICE(0x0cf3, 0xe019), .driver_info = BTUSB_IGNORE },
Andy Ross2a7bccc2011-05-09 16:11:16 -0700168 { USB_DEVICE(0x13d3, 0x3304), .driver_info = BTUSB_IGNORE },
Bala Shanmugambe931122010-11-26 17:35:46 +0530169
Cho, Yu-Chen509e7862011-01-26 17:10:59 +0800170 /* Atheros AR9285 Malbec with sflash firmware */
171 { USB_DEVICE(0x03f0, 0x311d), .driver_info = BTUSB_IGNORE },
172
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530173 /* Atheros 3012 with sflash firmware */
Andy Shevchenko0b880062014-02-18 18:26:19 +0200174 { USB_DEVICE(0x0489, 0xe04d), .driver_info = BTUSB_ATH3012 },
175 { USB_DEVICE(0x0489, 0xe04e), .driver_info = BTUSB_ATH3012 },
176 { USB_DEVICE(0x0489, 0xe056), .driver_info = BTUSB_ATH3012 },
177 { USB_DEVICE(0x0489, 0xe057), .driver_info = BTUSB_ATH3012 },
178 { USB_DEVICE(0x0489, 0xe05f), .driver_info = BTUSB_ATH3012 },
Anantha Krishnan4b552bc2014-10-06 16:31:49 +0530179 { USB_DEVICE(0x0489, 0xe078), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200180 { USB_DEVICE(0x04c5, 0x1330), .driver_info = BTUSB_ATH3012 },
181 { USB_DEVICE(0x04ca, 0x3004), .driver_info = BTUSB_ATH3012 },
182 { USB_DEVICE(0x04ca, 0x3005), .driver_info = BTUSB_ATH3012 },
183 { USB_DEVICE(0x04ca, 0x3006), .driver_info = BTUSB_ATH3012 },
Mohammed Habibulla1fb4e092014-04-17 11:37:13 -0700184 { USB_DEVICE(0x04ca, 0x3007), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200185 { USB_DEVICE(0x04ca, 0x3008), .driver_info = BTUSB_ATH3012 },
186 { USB_DEVICE(0x04ca, 0x300b), .driver_info = BTUSB_ATH3012 },
Janne Heikkinen134d3b32014-12-09 07:44:51 +0200187 { USB_DEVICE(0x04ca, 0x3010), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200188 { USB_DEVICE(0x0930, 0x0219), .driver_info = BTUSB_ATH3012 },
189 { USB_DEVICE(0x0930, 0x0220), .driver_info = BTUSB_ATH3012 },
Vincent Zwanenburg89d29752014-08-08 12:33:56 +0100190 { USB_DEVICE(0x0930, 0x0227), .driver_info = BTUSB_ATH3012 },
Andy Shevchenkoa735f9e2014-02-18 18:26:20 +0200191 { USB_DEVICE(0x0b05, 0x17d0), .driver_info = BTUSB_ATH3012 },
Ming Leid66629c2013-03-15 11:00:39 +0800192 { USB_DEVICE(0x0cf3, 0x0036), .driver_info = BTUSB_ATH3012 },
Steven.Li2d25f8b2011-07-01 14:02:36 +0800193 { USB_DEVICE(0x0cf3, 0x3004), .driver_info = BTUSB_ATH3012 },
Sunguk Lee94a32d102013-03-12 04:41:58 +0900194 { USB_DEVICE(0x0cf3, 0x3008), .driver_info = BTUSB_ATH3012 },
Cho, Yu-Chen07c0ea82012-03-14 22:01:21 +0200195 { USB_DEVICE(0x0cf3, 0x311d), .driver_info = BTUSB_ATH3012 },
Oliver Neukumb1312372014-01-16 15:37:11 +0100196 { USB_DEVICE(0x0cf3, 0x311e), .driver_info = BTUSB_ATH3012 },
Oliver Neukum1e56f1e2014-01-16 16:02:58 +0100197 { USB_DEVICE(0x0cf3, 0x311f), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200198 { USB_DEVICE(0x0cf3, 0x3121), .driver_info = BTUSB_ATH3012 },
Ming Leiebaf5792013-03-18 23:45:11 +0800199 { USB_DEVICE(0x0cf3, 0x817a), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200200 { USB_DEVICE(0x0cf3, 0xe003), .driver_info = BTUSB_ATH3012 },
AceLan Kaoac713112012-04-19 14:53:45 +0800201 { USB_DEVICE(0x0cf3, 0xe004), .driver_info = BTUSB_ATH3012 },
Peng Chen0a3658c2013-08-30 17:41:40 +0800202 { USB_DEVICE(0x0cf3, 0xe005), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200203 { USB_DEVICE(0x13d3, 0x3362), .driver_info = BTUSB_ATH3012 },
204 { USB_DEVICE(0x13d3, 0x3375), .driver_info = BTUSB_ATH3012 },
AceLan Kaoeed307e2012-12-11 11:41:20 +0800205 { USB_DEVICE(0x13d3, 0x3393), .driver_info = BTUSB_ATH3012 },
Sujith Manoharan5b77a1f2013-07-15 09:29:03 +0530206 { USB_DEVICE(0x13d3, 0x3402), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin3bb30a7c2014-11-25 20:19:52 +0300207 { USB_DEVICE(0x13d3, 0x3408), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin033efa92015-01-18 00:16:51 +0300208 { USB_DEVICE(0x13d3, 0x3423), .driver_info = BTUSB_ATH3012 },
Anantha Krishnanfa2f1392014-07-08 19:25:08 +0530209 { USB_DEVICE(0x13d3, 0x3432), .driver_info = BTUSB_ATH3012 },
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530210
Cho, Yu-Chene9036e32011-02-15 10:20:07 +0800211 /* Atheros AR5BBU12 with sflash firmware */
212 { USB_DEVICE(0x0489, 0xe02c), .driver_info = BTUSB_IGNORE },
213
Michael Gruetzner85d59722012-05-02 22:33:40 +0200214 /* Atheros AR5BBU12 with sflash firmware */
Yevgeniy Melnichukbc21fde2012-08-07 19:48:10 +0530215 { USB_DEVICE(0x0489, 0xe036), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200216 { USB_DEVICE(0x0489, 0xe03c), .driver_info = BTUSB_ATH3012 },
Michael Gruetzner85d59722012-05-02 22:33:40 +0200217
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +0000218 /* QCA ROME chipset */
Marcel Holtmannc9e44472015-03-16 23:56:04 -0700219 { USB_DEVICE(0x0cf3, 0xe300), .driver_info = BTUSB_QCA_ROME },
220 { USB_DEVICE(0x0cf3, 0xe360), .driver_info = BTUSB_QCA_ROME },
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +0000221
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200222 /* Broadcom BCM2035 */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100223 { USB_DEVICE(0x0a5c, 0x2009), .driver_info = BTUSB_BCM92035 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200224 { USB_DEVICE(0x0a5c, 0x200a), .driver_info = BTUSB_WRONG_SCO_MTU },
225 { USB_DEVICE(0x0a5c, 0x2035), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200226
227 /* Broadcom BCM2045 */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100228 { USB_DEVICE(0x0a5c, 0x2039), .driver_info = BTUSB_WRONG_SCO_MTU },
229 { USB_DEVICE(0x0a5c, 0x2101), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmannbdbef3d2008-09-23 00:16:35 +0200230
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200231 /* IBM/Lenovo ThinkPad with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100232 { USB_DEVICE(0x0a5c, 0x201e), .driver_info = BTUSB_WRONG_SCO_MTU },
233 { USB_DEVICE(0x0a5c, 0x2110), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200234
235 /* HP laptop with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100236 { USB_DEVICE(0x03f0, 0x171d), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200237
238 /* Dell laptop with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100239 { USB_DEVICE(0x413c, 0x8126), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200240
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100241 /* Dell Wireless 370 and 410 devices */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100242 { USB_DEVICE(0x413c, 0x8152), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100243 { USB_DEVICE(0x413c, 0x8156), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200244
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100245 /* Belkin F8T012 and F8T013 devices */
246 { USB_DEVICE(0x050d, 0x0012), .driver_info = BTUSB_WRONG_SCO_MTU },
247 { USB_DEVICE(0x050d, 0x0013), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200248
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100249 /* Asus WL-BTD202 device */
250 { USB_DEVICE(0x0b05, 0x1715), .driver_info = BTUSB_WRONG_SCO_MTU },
251
252 /* Kensington Bluetooth USB adapter */
253 { USB_DEVICE(0x047d, 0x105e), .driver_info = BTUSB_WRONG_SCO_MTU },
254
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200255 /* RTX Telecom based adapters with buggy SCO support */
256 { USB_DEVICE(0x0400, 0x0807), .driver_info = BTUSB_BROKEN_ISOC },
257 { USB_DEVICE(0x0400, 0x080a), .driver_info = BTUSB_BROKEN_ISOC },
258
259 /* CONWISE Technology based adapters with buggy SCO support */
260 { USB_DEVICE(0x0e5e, 0x6622), .driver_info = BTUSB_BROKEN_ISOC },
261
Marcel Holtmann4fcef8e2015-01-01 17:34:37 -0800262 /* Roper Class 1 Bluetooth Dongle (Silicon Wave based) */
263 { USB_DEVICE(0x1300, 0x0001), .driver_info = BTUSB_SWAVE },
264
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200265 /* Digianswer devices */
266 { USB_DEVICE(0x08fd, 0x0001), .driver_info = BTUSB_DIGIANSWER },
267 { USB_DEVICE(0x08fd, 0x0002), .driver_info = BTUSB_IGNORE },
268
269 /* CSR BlueCore Bluetooth Sniffer */
Marcel Holtmann4f64fa82014-07-07 00:12:04 +0200270 { USB_DEVICE(0x0a12, 0x0002),
271 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200272
273 /* Frontline ComProbe Bluetooth Sniffer */
Marcel Holtmann4f64fa82014-07-07 00:12:04 +0200274 { USB_DEVICE(0x16d3, 0x0002),
275 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200276
Marcel Holtmanncb1ee892015-01-28 19:41:42 -0800277 /* Marvell Bluetooth devices */
278 { USB_DEVICE(0x1286, 0x2044), .driver_info = BTUSB_MARVELL },
279 { USB_DEVICE(0x1286, 0x2046), .driver_info = BTUSB_MARVELL },
280
Marcel Holtmannd0ac9eb2015-01-28 19:41:43 -0800281 /* Intel Bluetooth devices */
Marcel Holtmann407550f2015-02-22 15:41:18 -0800282 { USB_DEVICE(0x8087, 0x07da), .driver_info = BTUSB_CSR },
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -0700283 { USB_DEVICE(0x8087, 0x07dc), .driver_info = BTUSB_INTEL },
Tedd Ho-Jeong Anef4e5e42013-11-12 13:10:58 -0800284 { USB_DEVICE(0x8087, 0x0a2a), .driver_info = BTUSB_INTEL },
Marcel Holtmanncda0dd72015-01-26 21:33:48 -0800285 { USB_DEVICE(0x8087, 0x0a2b), .driver_info = BTUSB_INTEL_NEW },
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -0700286
Marcel Holtmannd0ac9eb2015-01-28 19:41:43 -0800287 /* Other Intel Bluetooth devices */
288 { USB_VENDOR_AND_INTERFACE_INFO(0x8087, 0xe0, 0x01, 0x01),
289 .driver_info = BTUSB_IGNORE },
Amitkumar Karwarae8df492014-07-18 14:47:06 -0700290
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200291 { } /* Terminating entry */
292};
293
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200294#define BTUSB_MAX_ISOC_FRAMES 10
295
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200296#define BTUSB_INTR_RUNNING 0
297#define BTUSB_BULK_RUNNING 1
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200298#define BTUSB_ISOC_RUNNING 2
Oliver Neukum7bee5492009-08-24 23:44:59 +0200299#define BTUSB_SUSPENDING 3
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -0300300#define BTUSB_DID_ISO_RESUME 4
Marcel Holtmanncda0dd72015-01-26 21:33:48 -0800301#define BTUSB_BOOTLOADER 5
302#define BTUSB_DOWNLOADING 6
Marcel Holtmannce6bb922015-01-28 01:58:40 -0800303#define BTUSB_FIRMWARE_LOADED 7
Marcel Holtmanncda0dd72015-01-26 21:33:48 -0800304#define BTUSB_FIRMWARE_FAILED 8
Marcel Holtmannce6bb922015-01-28 01:58:40 -0800305#define BTUSB_BOOTING 9
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200306
307struct btusb_data {
308 struct hci_dev *hdev;
309 struct usb_device *udev;
Marcel Holtmann5fbcd262008-09-23 00:16:36 +0200310 struct usb_interface *intf;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200311 struct usb_interface *isoc;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200312
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200313 unsigned long flags;
314
315 struct work_struct work;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200316 struct work_struct waker;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200317
Marcel Holtmann803b5832014-09-16 08:00:29 +0200318 struct usb_anchor deferred;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200319 struct usb_anchor tx_anchor;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200320 int tx_in_flight;
321 spinlock_t txlock;
322
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200323 struct usb_anchor intr_anchor;
324 struct usb_anchor bulk_anchor;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200325 struct usb_anchor isoc_anchor;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200326 spinlock_t rxlock;
327
328 struct sk_buff *evt_skb;
329 struct sk_buff *acl_skb;
330 struct sk_buff *sco_skb;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200331
332 struct usb_endpoint_descriptor *intr_ep;
333 struct usb_endpoint_descriptor *bulk_tx_ep;
334 struct usb_endpoint_descriptor *bulk_rx_ep;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200335 struct usb_endpoint_descriptor *isoc_tx_ep;
336 struct usb_endpoint_descriptor *isoc_rx_ep;
337
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100338 __u8 cmdreq_type;
Marcel Holtmann893ba542015-01-28 20:27:34 -0800339 __u8 cmdreq;
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100340
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100341 unsigned int sco_num;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200342 int isoc_altsetting;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +0100343 int suspend_count;
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +0100344
Marcel Holtmann97307f52015-01-12 13:51:10 -0800345 int (*recv_event)(struct hci_dev *hdev, struct sk_buff *skb);
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +0100346 int (*recv_bulk)(struct btusb_data *data, void *buffer, int count);
Kim, Ben Young Taeace31982015-02-15 23:06:14 +0000347
348 int (*setup_on_usb)(struct hci_dev *hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200349};
350
Marcel Holtmann803b5832014-09-16 08:00:29 +0200351static inline void btusb_free_frags(struct btusb_data *data)
352{
353 unsigned long flags;
354
355 spin_lock_irqsave(&data->rxlock, flags);
356
357 kfree_skb(data->evt_skb);
358 data->evt_skb = NULL;
359
360 kfree_skb(data->acl_skb);
361 data->acl_skb = NULL;
362
363 kfree_skb(data->sco_skb);
364 data->sco_skb = NULL;
365
366 spin_unlock_irqrestore(&data->rxlock, flags);
367}
368
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200369static int btusb_recv_intr(struct btusb_data *data, void *buffer, int count)
370{
Marcel Holtmann803b5832014-09-16 08:00:29 +0200371 struct sk_buff *skb;
372 int err = 0;
373
374 spin_lock(&data->rxlock);
375 skb = data->evt_skb;
376
377 while (count) {
378 int len;
379
380 if (!skb) {
381 skb = bt_skb_alloc(HCI_MAX_EVENT_SIZE, GFP_ATOMIC);
382 if (!skb) {
383 err = -ENOMEM;
384 break;
385 }
386
387 bt_cb(skb)->pkt_type = HCI_EVENT_PKT;
388 bt_cb(skb)->expect = HCI_EVENT_HDR_SIZE;
389 }
390
391 len = min_t(uint, bt_cb(skb)->expect, count);
392 memcpy(skb_put(skb, len), buffer, len);
393
394 count -= len;
395 buffer += len;
396 bt_cb(skb)->expect -= len;
397
398 if (skb->len == HCI_EVENT_HDR_SIZE) {
399 /* Complete event header */
400 bt_cb(skb)->expect = hci_event_hdr(skb)->plen;
401
402 if (skb_tailroom(skb) < bt_cb(skb)->expect) {
403 kfree_skb(skb);
404 skb = NULL;
405
406 err = -EILSEQ;
407 break;
408 }
409 }
410
411 if (bt_cb(skb)->expect == 0) {
412 /* Complete frame */
Marcel Holtmann97307f52015-01-12 13:51:10 -0800413 data->recv_event(data->hdev, skb);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200414 skb = NULL;
415 }
416 }
417
418 data->evt_skb = skb;
419 spin_unlock(&data->rxlock);
420
421 return err;
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200422}
423
424static int btusb_recv_bulk(struct btusb_data *data, void *buffer, int count)
425{
Marcel Holtmann803b5832014-09-16 08:00:29 +0200426 struct sk_buff *skb;
427 int err = 0;
428
429 spin_lock(&data->rxlock);
430 skb = data->acl_skb;
431
432 while (count) {
433 int len;
434
435 if (!skb) {
436 skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
437 if (!skb) {
438 err = -ENOMEM;
439 break;
440 }
441
442 bt_cb(skb)->pkt_type = HCI_ACLDATA_PKT;
443 bt_cb(skb)->expect = HCI_ACL_HDR_SIZE;
444 }
445
446 len = min_t(uint, bt_cb(skb)->expect, count);
447 memcpy(skb_put(skb, len), buffer, len);
448
449 count -= len;
450 buffer += len;
451 bt_cb(skb)->expect -= len;
452
453 if (skb->len == HCI_ACL_HDR_SIZE) {
454 __le16 dlen = hci_acl_hdr(skb)->dlen;
455
456 /* Complete ACL header */
457 bt_cb(skb)->expect = __le16_to_cpu(dlen);
458
459 if (skb_tailroom(skb) < bt_cb(skb)->expect) {
460 kfree_skb(skb);
461 skb = NULL;
462
463 err = -EILSEQ;
464 break;
465 }
466 }
467
468 if (bt_cb(skb)->expect == 0) {
469 /* Complete frame */
470 hci_recv_frame(data->hdev, skb);
471 skb = NULL;
472 }
473 }
474
475 data->acl_skb = skb;
476 spin_unlock(&data->rxlock);
477
478 return err;
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200479}
480
481static int btusb_recv_isoc(struct btusb_data *data, void *buffer, int count)
482{
Marcel Holtmann803b5832014-09-16 08:00:29 +0200483 struct sk_buff *skb;
484 int err = 0;
485
486 spin_lock(&data->rxlock);
487 skb = data->sco_skb;
488
489 while (count) {
490 int len;
491
492 if (!skb) {
493 skb = bt_skb_alloc(HCI_MAX_SCO_SIZE, GFP_ATOMIC);
494 if (!skb) {
495 err = -ENOMEM;
496 break;
497 }
498
499 bt_cb(skb)->pkt_type = HCI_SCODATA_PKT;
500 bt_cb(skb)->expect = HCI_SCO_HDR_SIZE;
501 }
502
503 len = min_t(uint, bt_cb(skb)->expect, count);
504 memcpy(skb_put(skb, len), buffer, len);
505
506 count -= len;
507 buffer += len;
508 bt_cb(skb)->expect -= len;
509
510 if (skb->len == HCI_SCO_HDR_SIZE) {
511 /* Complete SCO header */
512 bt_cb(skb)->expect = hci_sco_hdr(skb)->dlen;
513
514 if (skb_tailroom(skb) < bt_cb(skb)->expect) {
515 kfree_skb(skb);
516 skb = NULL;
517
518 err = -EILSEQ;
519 break;
520 }
521 }
522
523 if (bt_cb(skb)->expect == 0) {
524 /* Complete frame */
525 hci_recv_frame(data->hdev, skb);
526 skb = NULL;
527 }
528 }
529
530 data->sco_skb = skb;
531 spin_unlock(&data->rxlock);
532
533 return err;
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200534}
535
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200536static void btusb_intr_complete(struct urb *urb)
537{
538 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +0100539 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200540 int err;
541
Marcel Holtmann89e75332014-09-16 04:44:50 +0200542 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
543 urb->actual_length);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200544
545 if (!test_bit(HCI_RUNNING, &hdev->flags))
546 return;
547
548 if (urb->status == 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200549 hdev->stat.byte_rx += urb->actual_length;
550
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200551 if (btusb_recv_intr(data, urb->transfer_buffer,
552 urb->actual_length) < 0) {
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200553 BT_ERR("%s corrupted event packet", hdev->name);
554 hdev->stat.err_rx++;
555 }
Champion Chen85560c42014-09-06 14:06:08 -0500556 } else if (urb->status == -ENOENT) {
557 /* Avoid suspend failed when usb_kill_urb */
558 return;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200559 }
560
561 if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
562 return;
563
Oliver Neukum7bee5492009-08-24 23:44:59 +0200564 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200565 usb_anchor_urb(urb, &data->intr_anchor);
566
567 err = usb_submit_urb(urb, GFP_ATOMIC);
568 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200569 /* -EPERM: urb is being killed;
570 * -ENODEV: device got disconnected */
571 if (err != -EPERM && err != -ENODEV)
Stefan Seyfried61faddf2010-11-30 21:49:08 +0100572 BT_ERR("%s urb %p failed to resubmit (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200573 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200574 usb_unanchor_urb(urb);
575 }
576}
577
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100578static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200579{
David Herrmann155961e2012-02-09 21:58:32 +0100580 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200581 struct urb *urb;
582 unsigned char *buf;
583 unsigned int pipe;
584 int err, size;
585
586 BT_DBG("%s", hdev->name);
587
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200588 if (!data->intr_ep)
589 return -ENODEV;
590
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100591 urb = usb_alloc_urb(0, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200592 if (!urb)
593 return -ENOMEM;
594
595 size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
596
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100597 buf = kmalloc(size, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200598 if (!buf) {
599 usb_free_urb(urb);
600 return -ENOMEM;
601 }
602
603 pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
604
605 usb_fill_int_urb(urb, data->udev, pipe, buf, size,
Marcel Holtmann89e75332014-09-16 04:44:50 +0200606 btusb_intr_complete, hdev, data->intr_ep->bInterval);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200607
608 urb->transfer_flags |= URB_FREE_BUFFER;
609
610 usb_anchor_urb(urb, &data->intr_anchor);
611
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100612 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200613 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +0200614 if (err != -EPERM && err != -ENODEV)
615 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200616 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200617 usb_unanchor_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200618 }
619
620 usb_free_urb(urb);
621
622 return err;
623}
624
625static void btusb_bulk_complete(struct urb *urb)
626{
627 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +0100628 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200629 int err;
630
Marcel Holtmann89e75332014-09-16 04:44:50 +0200631 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
632 urb->actual_length);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200633
634 if (!test_bit(HCI_RUNNING, &hdev->flags))
635 return;
636
637 if (urb->status == 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200638 hdev->stat.byte_rx += urb->actual_length;
639
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +0100640 if (data->recv_bulk(data, urb->transfer_buffer,
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200641 urb->actual_length) < 0) {
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200642 BT_ERR("%s corrupted ACL packet", hdev->name);
643 hdev->stat.err_rx++;
644 }
Champion Chen85560c42014-09-06 14:06:08 -0500645 } else if (urb->status == -ENOENT) {
646 /* Avoid suspend failed when usb_kill_urb */
647 return;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200648 }
649
650 if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
651 return;
652
653 usb_anchor_urb(urb, &data->bulk_anchor);
Oliver Neukum652fd782009-12-16 19:23:43 +0100654 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200655
656 err = usb_submit_urb(urb, GFP_ATOMIC);
657 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200658 /* -EPERM: urb is being killed;
659 * -ENODEV: device got disconnected */
660 if (err != -EPERM && err != -ENODEV)
Stefan Seyfried61faddf2010-11-30 21:49:08 +0100661 BT_ERR("%s urb %p failed to resubmit (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200662 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200663 usb_unanchor_urb(urb);
664 }
665}
666
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100667static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200668{
David Herrmann155961e2012-02-09 21:58:32 +0100669 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200670 struct urb *urb;
671 unsigned char *buf;
672 unsigned int pipe;
Vikram Kandukuri290ba202009-07-02 14:31:59 +0530673 int err, size = HCI_MAX_FRAME_SIZE;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200674
675 BT_DBG("%s", hdev->name);
676
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200677 if (!data->bulk_rx_ep)
678 return -ENODEV;
679
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100680 urb = usb_alloc_urb(0, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200681 if (!urb)
682 return -ENOMEM;
683
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100684 buf = kmalloc(size, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200685 if (!buf) {
686 usb_free_urb(urb);
687 return -ENOMEM;
688 }
689
690 pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
691
Marcel Holtmann89e75332014-09-16 04:44:50 +0200692 usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
693 btusb_bulk_complete, hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200694
695 urb->transfer_flags |= URB_FREE_BUFFER;
696
Oliver Neukum7bee5492009-08-24 23:44:59 +0200697 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200698 usb_anchor_urb(urb, &data->bulk_anchor);
699
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100700 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200701 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +0200702 if (err != -EPERM && err != -ENODEV)
703 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200704 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200705 usb_unanchor_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200706 }
707
708 usb_free_urb(urb);
709
710 return err;
711}
712
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200713static void btusb_isoc_complete(struct urb *urb)
714{
715 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +0100716 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200717 int i, err;
718
Marcel Holtmann89e75332014-09-16 04:44:50 +0200719 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
720 urb->actual_length);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200721
722 if (!test_bit(HCI_RUNNING, &hdev->flags))
723 return;
724
725 if (urb->status == 0) {
726 for (i = 0; i < urb->number_of_packets; i++) {
727 unsigned int offset = urb->iso_frame_desc[i].offset;
728 unsigned int length = urb->iso_frame_desc[i].actual_length;
729
730 if (urb->iso_frame_desc[i].status)
731 continue;
732
733 hdev->stat.byte_rx += length;
734
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200735 if (btusb_recv_isoc(data, urb->transfer_buffer + offset,
736 length) < 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200737 BT_ERR("%s corrupted SCO packet", hdev->name);
738 hdev->stat.err_rx++;
739 }
740 }
Champion Chen85560c42014-09-06 14:06:08 -0500741 } else if (urb->status == -ENOENT) {
742 /* Avoid suspend failed when usb_kill_urb */
743 return;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200744 }
745
746 if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags))
747 return;
748
749 usb_anchor_urb(urb, &data->isoc_anchor);
750
751 err = usb_submit_urb(urb, GFP_ATOMIC);
752 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200753 /* -EPERM: urb is being killed;
754 * -ENODEV: device got disconnected */
755 if (err != -EPERM && err != -ENODEV)
Stefan Seyfried61faddf2010-11-30 21:49:08 +0100756 BT_ERR("%s urb %p failed to resubmit (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200757 hdev->name, urb, -err);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200758 usb_unanchor_urb(urb);
759 }
760}
761
Jesper Juhl42b16b32011-01-17 00:09:38 +0100762static inline void __fill_isoc_descriptor(struct urb *urb, int len, int mtu)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200763{
764 int i, offset = 0;
765
766 BT_DBG("len %d mtu %d", len, mtu);
767
768 for (i = 0; i < BTUSB_MAX_ISOC_FRAMES && len >= mtu;
769 i++, offset += mtu, len -= mtu) {
770 urb->iso_frame_desc[i].offset = offset;
771 urb->iso_frame_desc[i].length = mtu;
772 }
773
774 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
775 urb->iso_frame_desc[i].offset = offset;
776 urb->iso_frame_desc[i].length = len;
777 i++;
778 }
779
780 urb->number_of_packets = i;
781}
782
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100783static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200784{
David Herrmann155961e2012-02-09 21:58:32 +0100785 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200786 struct urb *urb;
787 unsigned char *buf;
788 unsigned int pipe;
789 int err, size;
790
791 BT_DBG("%s", hdev->name);
792
793 if (!data->isoc_rx_ep)
794 return -ENODEV;
795
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100796 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200797 if (!urb)
798 return -ENOMEM;
799
800 size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) *
801 BTUSB_MAX_ISOC_FRAMES;
802
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100803 buf = kmalloc(size, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200804 if (!buf) {
805 usb_free_urb(urb);
806 return -ENOMEM;
807 }
808
809 pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress);
810
Bing Zhaofa0fb932011-12-20 18:19:00 -0800811 usb_fill_int_urb(urb, data->udev, pipe, buf, size, btusb_isoc_complete,
Marcel Holtmann89e75332014-09-16 04:44:50 +0200812 hdev, data->isoc_rx_ep->bInterval);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200813
Marcel Holtmann89e75332014-09-16 04:44:50 +0200814 urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200815
816 __fill_isoc_descriptor(urb, size,
Marcel Holtmann89e75332014-09-16 04:44:50 +0200817 le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize));
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200818
819 usb_anchor_urb(urb, &data->isoc_anchor);
820
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100821 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200822 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +0200823 if (err != -EPERM && err != -ENODEV)
824 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200825 hdev->name, urb, -err);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200826 usb_unanchor_urb(urb);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200827 }
828
829 usb_free_urb(urb);
830
831 return err;
832}
833
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200834static void btusb_tx_complete(struct urb *urb)
835{
836 struct sk_buff *skb = urb->context;
Marcel Holtmann89e75332014-09-16 04:44:50 +0200837 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
David Herrmann155961e2012-02-09 21:58:32 +0100838 struct btusb_data *data = hci_get_drvdata(hdev);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200839
Marcel Holtmann89e75332014-09-16 04:44:50 +0200840 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
841 urb->actual_length);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200842
843 if (!test_bit(HCI_RUNNING, &hdev->flags))
844 goto done;
845
846 if (!urb->status)
847 hdev->stat.byte_tx += urb->transfer_buffer_length;
848 else
849 hdev->stat.err_tx++;
850
851done:
852 spin_lock(&data->txlock);
853 data->tx_in_flight--;
854 spin_unlock(&data->txlock);
855
856 kfree(urb->setup_packet);
857
858 kfree_skb(skb);
859}
860
861static void btusb_isoc_tx_complete(struct urb *urb)
862{
863 struct sk_buff *skb = urb->context;
Marcel Holtmann89e75332014-09-16 04:44:50 +0200864 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200865
Marcel Holtmann89e75332014-09-16 04:44:50 +0200866 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
867 urb->actual_length);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200868
869 if (!test_bit(HCI_RUNNING, &hdev->flags))
870 goto done;
871
872 if (!urb->status)
873 hdev->stat.byte_tx += urb->transfer_buffer_length;
874 else
875 hdev->stat.err_tx++;
876
877done:
878 kfree(urb->setup_packet);
879
880 kfree_skb(skb);
881}
882
883static int btusb_open(struct hci_dev *hdev)
884{
David Herrmann155961e2012-02-09 21:58:32 +0100885 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200886 int err;
887
888 BT_DBG("%s", hdev->name);
889
Kim, Ben Young Taeace31982015-02-15 23:06:14 +0000890 /* Patching USB firmware files prior to starting any URBs of HCI path
891 * It is more safe to use USB bulk channel for downloading USB patch
892 */
893 if (data->setup_on_usb) {
894 err = data->setup_on_usb(hdev);
895 if (err <0)
896 return err;
897 }
898
Oliver Neukum7bee5492009-08-24 23:44:59 +0200899 err = usb_autopm_get_interface(data->intf);
900 if (err < 0)
901 return err;
902
903 data->intf->needs_remote_wakeup = 1;
904
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200905 if (test_and_set_bit(HCI_RUNNING, &hdev->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +0200906 goto done;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200907
908 if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +0200909 goto done;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200910
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100911 err = btusb_submit_intr_urb(hdev, GFP_KERNEL);
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100912 if (err < 0)
913 goto failed;
914
915 err = btusb_submit_bulk_urb(hdev, GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200916 if (err < 0) {
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100917 usb_kill_anchored_urbs(&data->intr_anchor);
918 goto failed;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200919 }
920
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100921 set_bit(BTUSB_BULK_RUNNING, &data->flags);
922 btusb_submit_bulk_urb(hdev, GFP_KERNEL);
923
Oliver Neukum7bee5492009-08-24 23:44:59 +0200924done:
925 usb_autopm_put_interface(data->intf);
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100926 return 0;
927
928failed:
929 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
930 clear_bit(HCI_RUNNING, &hdev->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200931 usb_autopm_put_interface(data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200932 return err;
933}
934
Oliver Neukum7bee5492009-08-24 23:44:59 +0200935static void btusb_stop_traffic(struct btusb_data *data)
936{
937 usb_kill_anchored_urbs(&data->intr_anchor);
938 usb_kill_anchored_urbs(&data->bulk_anchor);
939 usb_kill_anchored_urbs(&data->isoc_anchor);
940}
941
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200942static int btusb_close(struct hci_dev *hdev)
943{
David Herrmann155961e2012-02-09 21:58:32 +0100944 struct btusb_data *data = hci_get_drvdata(hdev);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200945 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200946
947 BT_DBG("%s", hdev->name);
948
949 if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
950 return 0;
951
Marcel Holtmanne8c3c3d2008-09-23 00:16:36 +0200952 cancel_work_sync(&data->work);
Linus Torvalds404291a2009-11-11 13:32:29 -0800953 cancel_work_sync(&data->waker);
Marcel Holtmanne8c3c3d2008-09-23 00:16:36 +0200954
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200955 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200956 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200957 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200958
959 btusb_stop_traffic(data);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200960 btusb_free_frags(data);
961
Oliver Neukum7bee5492009-08-24 23:44:59 +0200962 err = usb_autopm_get_interface(data->intf);
963 if (err < 0)
Oliver Neukum7b8e2c12009-11-13 14:26:23 +0100964 goto failed;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200965
966 data->intf->needs_remote_wakeup = 0;
967 usb_autopm_put_interface(data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200968
Oliver Neukum7b8e2c12009-11-13 14:26:23 +0100969failed:
970 usb_scuttle_anchored_urbs(&data->deferred);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200971 return 0;
972}
973
974static int btusb_flush(struct hci_dev *hdev)
975{
David Herrmann155961e2012-02-09 21:58:32 +0100976 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200977
978 BT_DBG("%s", hdev->name);
979
980 usb_kill_anchored_urbs(&data->tx_anchor);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200981 btusb_free_frags(data);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200982
983 return 0;
984}
985
Marcel Holtmann047b2ec2014-09-14 09:11:06 +0200986static struct urb *alloc_ctrl_urb(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200987{
David Herrmann155961e2012-02-09 21:58:32 +0100988 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200989 struct usb_ctrlrequest *dr;
990 struct urb *urb;
991 unsigned int pipe;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200992
Marcel Holtmann047b2ec2014-09-14 09:11:06 +0200993 urb = usb_alloc_urb(0, GFP_KERNEL);
994 if (!urb)
995 return ERR_PTR(-ENOMEM);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200996
Marcel Holtmann047b2ec2014-09-14 09:11:06 +0200997 dr = kmalloc(sizeof(*dr), GFP_KERNEL);
998 if (!dr) {
999 usb_free_urb(urb);
1000 return ERR_PTR(-ENOMEM);
1001 }
1002
1003 dr->bRequestType = data->cmdreq_type;
Marcel Holtmann893ba542015-01-28 20:27:34 -08001004 dr->bRequest = data->cmdreq;
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001005 dr->wIndex = 0;
1006 dr->wValue = 0;
1007 dr->wLength = __cpu_to_le16(skb->len);
1008
1009 pipe = usb_sndctrlpipe(data->udev, 0x00);
1010
Marcel Holtmann89e75332014-09-16 04:44:50 +02001011 usb_fill_control_urb(urb, data->udev, pipe, (void *)dr,
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001012 skb->data, skb->len, btusb_tx_complete, skb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001013
Marcel Holtmann89e75332014-09-16 04:44:50 +02001014 skb->dev = (void *)hdev;
Marcel Holtmann7bd8f092013-10-11 06:19:18 -07001015
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001016 return urb;
1017}
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001018
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001019static struct urb *alloc_bulk_urb(struct hci_dev *hdev, struct sk_buff *skb)
1020{
1021 struct btusb_data *data = hci_get_drvdata(hdev);
1022 struct urb *urb;
1023 unsigned int pipe;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001024
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001025 if (!data->bulk_tx_ep)
1026 return ERR_PTR(-ENODEV);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001027
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001028 urb = usb_alloc_urb(0, GFP_KERNEL);
1029 if (!urb)
1030 return ERR_PTR(-ENOMEM);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001031
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001032 pipe = usb_sndbulkpipe(data->udev, data->bulk_tx_ep->bEndpointAddress);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001033
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001034 usb_fill_bulk_urb(urb, data->udev, pipe,
1035 skb->data, skb->len, btusb_tx_complete, skb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001036
Marcel Holtmann89e75332014-09-16 04:44:50 +02001037 skb->dev = (void *)hdev;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001038
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001039 return urb;
1040}
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001041
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001042static struct urb *alloc_isoc_urb(struct hci_dev *hdev, struct sk_buff *skb)
1043{
1044 struct btusb_data *data = hci_get_drvdata(hdev);
1045 struct urb *urb;
1046 unsigned int pipe;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001047
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001048 if (!data->isoc_tx_ep)
1049 return ERR_PTR(-ENODEV);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001050
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001051 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_KERNEL);
1052 if (!urb)
1053 return ERR_PTR(-ENOMEM);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001054
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001055 pipe = usb_sndisocpipe(data->udev, data->isoc_tx_ep->bEndpointAddress);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001056
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001057 usb_fill_int_urb(urb, data->udev, pipe,
1058 skb->data, skb->len, btusb_isoc_tx_complete,
1059 skb, data->isoc_tx_ep->bInterval);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001060
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001061 urb->transfer_flags = URB_ISO_ASAP;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001062
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001063 __fill_isoc_descriptor(urb, skb->len,
1064 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001065
Marcel Holtmann89e75332014-09-16 04:44:50 +02001066 skb->dev = (void *)hdev;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001067
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001068 return urb;
1069}
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001070
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001071static int submit_tx_urb(struct hci_dev *hdev, struct urb *urb)
1072{
1073 struct btusb_data *data = hci_get_drvdata(hdev);
1074 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001075
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001076 usb_anchor_urb(urb, &data->tx_anchor);
1077
Johan Hedberge9753ef2014-09-14 08:49:34 +03001078 err = usb_submit_urb(urb, GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001079 if (err < 0) {
Paul Bolle5a9b80e2011-10-09 12:12:16 +02001080 if (err != -EPERM && err != -ENODEV)
1081 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +02001082 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001083 kfree(urb->setup_packet);
1084 usb_unanchor_urb(urb);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001085 } else {
1086 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001087 }
1088
Cong Wang54a8a792011-11-22 09:32:57 +08001089 usb_free_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001090 return err;
1091}
1092
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001093static int submit_or_queue_tx_urb(struct hci_dev *hdev, struct urb *urb)
1094{
1095 struct btusb_data *data = hci_get_drvdata(hdev);
1096 unsigned long flags;
1097 bool suspending;
1098
1099 spin_lock_irqsave(&data->txlock, flags);
1100 suspending = test_bit(BTUSB_SUSPENDING, &data->flags);
1101 if (!suspending)
1102 data->tx_in_flight++;
1103 spin_unlock_irqrestore(&data->txlock, flags);
1104
1105 if (!suspending)
1106 return submit_tx_urb(hdev, urb);
1107
1108 usb_anchor_urb(urb, &data->deferred);
1109 schedule_work(&data->waker);
1110
1111 usb_free_urb(urb);
1112 return 0;
1113}
1114
1115static int btusb_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
1116{
1117 struct urb *urb;
1118
1119 BT_DBG("%s", hdev->name);
1120
1121 if (!test_bit(HCI_RUNNING, &hdev->flags))
1122 return -EBUSY;
1123
1124 switch (bt_cb(skb)->pkt_type) {
1125 case HCI_COMMAND_PKT:
1126 urb = alloc_ctrl_urb(hdev, skb);
1127 if (IS_ERR(urb))
1128 return PTR_ERR(urb);
1129
1130 hdev->stat.cmd_tx++;
1131 return submit_or_queue_tx_urb(hdev, urb);
1132
1133 case HCI_ACLDATA_PKT:
1134 urb = alloc_bulk_urb(hdev, skb);
1135 if (IS_ERR(urb))
1136 return PTR_ERR(urb);
1137
1138 hdev->stat.acl_tx++;
1139 return submit_or_queue_tx_urb(hdev, urb);
1140
1141 case HCI_SCODATA_PKT:
1142 if (hci_conn_num(hdev, SCO_LINK) < 1)
1143 return -ENODEV;
1144
1145 urb = alloc_isoc_urb(hdev, skb);
1146 if (IS_ERR(urb))
1147 return PTR_ERR(urb);
1148
1149 hdev->stat.sco_tx++;
1150 return submit_tx_urb(hdev, urb);
1151 }
1152
1153 return -EILSEQ;
1154}
1155
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001156static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
1157{
David Herrmann155961e2012-02-09 21:58:32 +01001158 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001159
1160 BT_DBG("%s evt %d", hdev->name, evt);
1161
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001162 if (hci_conn_num(hdev, SCO_LINK) != data->sco_num) {
1163 data->sco_num = hci_conn_num(hdev, SCO_LINK);
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001164 schedule_work(&data->work);
Marcel Holtmanna780efa2008-11-30 12:17:12 +01001165 }
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001166}
1167
Jesper Juhl42b16b32011-01-17 00:09:38 +01001168static inline int __set_isoc_interface(struct hci_dev *hdev, int altsetting)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001169{
David Herrmann155961e2012-02-09 21:58:32 +01001170 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001171 struct usb_interface *intf = data->isoc;
1172 struct usb_endpoint_descriptor *ep_desc;
1173 int i, err;
1174
1175 if (!data->isoc)
1176 return -ENODEV;
1177
1178 err = usb_set_interface(data->udev, 1, altsetting);
1179 if (err < 0) {
1180 BT_ERR("%s setting interface failed (%d)", hdev->name, -err);
1181 return err;
1182 }
1183
1184 data->isoc_altsetting = altsetting;
1185
1186 data->isoc_tx_ep = NULL;
1187 data->isoc_rx_ep = NULL;
1188
1189 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
1190 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
1191
1192 if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) {
1193 data->isoc_tx_ep = ep_desc;
1194 continue;
1195 }
1196
1197 if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) {
1198 data->isoc_rx_ep = ep_desc;
1199 continue;
1200 }
1201 }
1202
1203 if (!data->isoc_tx_ep || !data->isoc_rx_ep) {
1204 BT_ERR("%s invalid SCO descriptors", hdev->name);
1205 return -ENODEV;
1206 }
1207
1208 return 0;
1209}
1210
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001211static void btusb_work(struct work_struct *work)
1212{
1213 struct btusb_data *data = container_of(work, struct btusb_data, work);
1214 struct hci_dev *hdev = data->hdev;
Mikel Astizf4001d22012-04-11 08:48:51 +02001215 int new_alts;
Oliver Neukum7bee5492009-08-24 23:44:59 +02001216 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001217
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001218 if (data->sco_num > 0) {
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -03001219 if (!test_bit(BTUSB_DID_ISO_RESUME, &data->flags)) {
Oliver Neukum8efdd0c2011-02-11 13:00:06 +01001220 err = usb_autopm_get_interface(data->isoc ? data->isoc : data->intf);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001221 if (err < 0) {
1222 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1223 usb_kill_anchored_urbs(&data->isoc_anchor);
1224 return;
1225 }
1226
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -03001227 set_bit(BTUSB_DID_ISO_RESUME, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001228 }
Mikel Astizf4001d22012-04-11 08:48:51 +02001229
1230 if (hdev->voice_setting & 0x0020) {
1231 static const int alts[3] = { 2, 4, 5 };
Marcel Holtmann89e75332014-09-16 04:44:50 +02001232
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001233 new_alts = alts[data->sco_num - 1];
Mikel Astizf4001d22012-04-11 08:48:51 +02001234 } else {
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001235 new_alts = data->sco_num;
Mikel Astizf4001d22012-04-11 08:48:51 +02001236 }
1237
1238 if (data->isoc_altsetting != new_alts) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001239 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1240 usb_kill_anchored_urbs(&data->isoc_anchor);
1241
Mikel Astizf4001d22012-04-11 08:48:51 +02001242 if (__set_isoc_interface(hdev, new_alts) < 0)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001243 return;
1244 }
1245
1246 if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001247 if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001248 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1249 else
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001250 btusb_submit_isoc_urb(hdev, GFP_KERNEL);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001251 }
1252 } else {
1253 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1254 usb_kill_anchored_urbs(&data->isoc_anchor);
1255
1256 __set_isoc_interface(hdev, 0);
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -03001257 if (test_and_clear_bit(BTUSB_DID_ISO_RESUME, &data->flags))
Oliver Neukum8efdd0c2011-02-11 13:00:06 +01001258 usb_autopm_put_interface(data->isoc ? data->isoc : data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001259 }
1260}
1261
Oliver Neukum7bee5492009-08-24 23:44:59 +02001262static void btusb_waker(struct work_struct *work)
1263{
1264 struct btusb_data *data = container_of(work, struct btusb_data, waker);
1265 int err;
1266
1267 err = usb_autopm_get_interface(data->intf);
1268 if (err < 0)
1269 return;
1270
1271 usb_autopm_put_interface(data->intf);
1272}
1273
Daniel Drake6576fe42015-02-23 18:16:47 -06001274static struct sk_buff *btusb_read_local_version(struct hci_dev *hdev)
1275{
1276 struct sk_buff *skb;
1277
1278 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
1279 HCI_INIT_TIMEOUT);
1280 if (IS_ERR(skb)) {
1281 BT_ERR("%s: HCI_OP_READ_LOCAL_VERSION failed (%ld)",
1282 hdev->name, PTR_ERR(skb));
1283 return skb;
1284 }
1285
1286 if (skb->len != sizeof(struct hci_rp_read_local_version)) {
1287 BT_ERR("%s: HCI_OP_READ_LOCAL_VERSION event length mismatch",
1288 hdev->name);
1289 kfree_skb(skb);
1290 return ERR_PTR(-EIO);
1291 }
1292
1293 return skb;
1294}
1295
Marcel Holtmann9f8f9622013-04-10 08:11:35 -07001296static int btusb_setup_bcm92035(struct hci_dev *hdev)
1297{
1298 struct sk_buff *skb;
1299 u8 val = 0x00;
1300
1301 BT_DBG("%s", hdev->name);
1302
1303 skb = __hci_cmd_sync(hdev, 0xfc3b, 1, &val, HCI_INIT_TIMEOUT);
1304 if (IS_ERR(skb))
1305 BT_ERR("BCM92035 command failed (%ld)", -PTR_ERR(skb));
1306 else
1307 kfree_skb(skb);
1308
1309 return 0;
1310}
1311
Marcel Holtmann81cac642014-01-03 03:02:36 -08001312static int btusb_setup_csr(struct hci_dev *hdev)
1313{
1314 struct hci_rp_read_local_version *rp;
1315 struct sk_buff *skb;
1316 int ret;
1317
1318 BT_DBG("%s", hdev->name);
1319
Daniel Drake6576fe42015-02-23 18:16:47 -06001320 skb = btusb_read_local_version(hdev);
1321 if (IS_ERR(skb))
Marcel Holtmann81cac642014-01-03 03:02:36 -08001322 return -PTR_ERR(skb);
Marcel Holtmann81cac642014-01-03 03:02:36 -08001323
Marcel Holtmann89e75332014-09-16 04:44:50 +02001324 rp = (struct hci_rp_read_local_version *)skb->data;
Marcel Holtmann81cac642014-01-03 03:02:36 -08001325
1326 if (!rp->status) {
1327 if (le16_to_cpu(rp->manufacturer) != 10) {
1328 /* Clear the reset quirk since this is not an actual
1329 * early Bluetooth 1.1 device from CSR.
1330 */
1331 clear_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
1332
1333 /* These fake CSR controllers have all a broken
1334 * stored link key handling and so just disable it.
1335 */
1336 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY,
1337 &hdev->quirks);
1338 }
1339 }
1340
1341 ret = -bt_to_errno(rp->status);
1342
1343 kfree_skb(skb);
1344
1345 return ret;
1346}
1347
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001348static const struct firmware *btusb_setup_intel_get_fw(struct hci_dev *hdev,
Marcel Holtmann89e75332014-09-16 04:44:50 +02001349 struct intel_version *ver)
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001350{
1351 const struct firmware *fw;
1352 char fwname[64];
1353 int ret;
1354
1355 snprintf(fwname, sizeof(fwname),
1356 "intel/ibt-hw-%x.%x.%x-fw-%x.%x.%x.%x.%x.bseq",
1357 ver->hw_platform, ver->hw_variant, ver->hw_revision,
1358 ver->fw_variant, ver->fw_revision, ver->fw_build_num,
1359 ver->fw_build_ww, ver->fw_build_yy);
1360
1361 ret = request_firmware(&fw, fwname, &hdev->dev);
1362 if (ret < 0) {
1363 if (ret == -EINVAL) {
1364 BT_ERR("%s Intel firmware file request failed (%d)",
1365 hdev->name, ret);
1366 return NULL;
1367 }
1368
1369 BT_ERR("%s failed to open Intel firmware file: %s(%d)",
1370 hdev->name, fwname, ret);
1371
1372 /* If the correct firmware patch file is not found, use the
1373 * default firmware patch file instead
1374 */
1375 snprintf(fwname, sizeof(fwname), "intel/ibt-hw-%x.%x.bseq",
1376 ver->hw_platform, ver->hw_variant);
1377 if (request_firmware(&fw, fwname, &hdev->dev) < 0) {
1378 BT_ERR("%s failed to open default Intel fw file: %s",
1379 hdev->name, fwname);
1380 return NULL;
1381 }
1382 }
1383
1384 BT_INFO("%s: Intel Bluetooth firmware file: %s", hdev->name, fwname);
1385
1386 return fw;
1387}
1388
1389static int btusb_setup_intel_patching(struct hci_dev *hdev,
1390 const struct firmware *fw,
1391 const u8 **fw_ptr, int *disable_patch)
1392{
1393 struct sk_buff *skb;
1394 struct hci_command_hdr *cmd;
1395 const u8 *cmd_param;
1396 struct hci_event_hdr *evt = NULL;
1397 const u8 *evt_param = NULL;
1398 int remain = fw->size - (*fw_ptr - fw->data);
1399
1400 /* The first byte indicates the types of the patch command or event.
1401 * 0x01 means HCI command and 0x02 is HCI event. If the first bytes
1402 * in the current firmware buffer doesn't start with 0x01 or
1403 * the size of remain buffer is smaller than HCI command header,
1404 * the firmware file is corrupted and it should stop the patching
1405 * process.
1406 */
1407 if (remain > HCI_COMMAND_HDR_SIZE && *fw_ptr[0] != 0x01) {
1408 BT_ERR("%s Intel fw corrupted: invalid cmd read", hdev->name);
1409 return -EINVAL;
1410 }
1411 (*fw_ptr)++;
1412 remain--;
1413
1414 cmd = (struct hci_command_hdr *)(*fw_ptr);
1415 *fw_ptr += sizeof(*cmd);
1416 remain -= sizeof(*cmd);
1417
1418 /* Ensure that the remain firmware data is long enough than the length
1419 * of command parameter. If not, the firmware file is corrupted.
1420 */
1421 if (remain < cmd->plen) {
1422 BT_ERR("%s Intel fw corrupted: invalid cmd len", hdev->name);
1423 return -EFAULT;
1424 }
1425
1426 /* If there is a command that loads a patch in the firmware
1427 * file, then enable the patch upon success, otherwise just
1428 * disable the manufacturer mode, for example patch activation
1429 * is not required when the default firmware patch file is used
1430 * because there are no patch data to load.
1431 */
1432 if (*disable_patch && le16_to_cpu(cmd->opcode) == 0xfc8e)
1433 *disable_patch = 0;
1434
1435 cmd_param = *fw_ptr;
1436 *fw_ptr += cmd->plen;
1437 remain -= cmd->plen;
1438
1439 /* This reads the expected events when the above command is sent to the
1440 * device. Some vendor commands expects more than one events, for
1441 * example command status event followed by vendor specific event.
1442 * For this case, it only keeps the last expected event. so the command
1443 * can be sent with __hci_cmd_sync_ev() which returns the sk_buff of
1444 * last expected event.
1445 */
1446 while (remain > HCI_EVENT_HDR_SIZE && *fw_ptr[0] == 0x02) {
1447 (*fw_ptr)++;
1448 remain--;
1449
1450 evt = (struct hci_event_hdr *)(*fw_ptr);
1451 *fw_ptr += sizeof(*evt);
1452 remain -= sizeof(*evt);
1453
1454 if (remain < evt->plen) {
1455 BT_ERR("%s Intel fw corrupted: invalid evt len",
1456 hdev->name);
1457 return -EFAULT;
1458 }
1459
1460 evt_param = *fw_ptr;
1461 *fw_ptr += evt->plen;
1462 remain -= evt->plen;
1463 }
1464
1465 /* Every HCI commands in the firmware file has its correspond event.
1466 * If event is not found or remain is smaller than zero, the firmware
1467 * file is corrupted.
1468 */
1469 if (!evt || !evt_param || remain < 0) {
1470 BT_ERR("%s Intel fw corrupted: invalid evt read", hdev->name);
1471 return -EFAULT;
1472 }
1473
1474 skb = __hci_cmd_sync_ev(hdev, le16_to_cpu(cmd->opcode), cmd->plen,
1475 cmd_param, evt->evt, HCI_INIT_TIMEOUT);
1476 if (IS_ERR(skb)) {
1477 BT_ERR("%s sending Intel patch command (0x%4.4x) failed (%ld)",
1478 hdev->name, cmd->opcode, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001479 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001480 }
1481
1482 /* It ensures that the returned event matches the event data read from
1483 * the firmware file. At fist, it checks the length and then
1484 * the contents of the event.
1485 */
1486 if (skb->len != evt->plen) {
1487 BT_ERR("%s mismatch event length (opcode 0x%4.4x)", hdev->name,
1488 le16_to_cpu(cmd->opcode));
1489 kfree_skb(skb);
1490 return -EFAULT;
1491 }
1492
1493 if (memcmp(skb->data, evt_param, evt->plen)) {
1494 BT_ERR("%s mismatch event parameter (opcode 0x%4.4x)",
1495 hdev->name, le16_to_cpu(cmd->opcode));
1496 kfree_skb(skb);
1497 return -EFAULT;
1498 }
1499 kfree_skb(skb);
1500
1501 return 0;
1502}
1503
1504static int btusb_setup_intel(struct hci_dev *hdev)
1505{
1506 struct sk_buff *skb;
1507 const struct firmware *fw;
1508 const u8 *fw_ptr;
1509 int disable_patch;
1510 struct intel_version *ver;
1511
1512 const u8 mfg_enable[] = { 0x01, 0x00 };
1513 const u8 mfg_disable[] = { 0x00, 0x00 };
1514 const u8 mfg_reset_deactivate[] = { 0x00, 0x01 };
1515 const u8 mfg_reset_activate[] = { 0x00, 0x02 };
1516
1517 BT_DBG("%s", hdev->name);
1518
1519 /* The controller has a bug with the first HCI command sent to it
1520 * returning number of completed commands as zero. This would stall the
1521 * command processing in the Bluetooth core.
1522 *
1523 * As a workaround, send HCI Reset command first which will reset the
1524 * number of completed commands and allow normal command processing
1525 * from now on.
1526 */
1527 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
1528 if (IS_ERR(skb)) {
1529 BT_ERR("%s sending initial HCI reset command failed (%ld)",
1530 hdev->name, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001531 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001532 }
1533 kfree_skb(skb);
1534
1535 /* Read Intel specific controller version first to allow selection of
1536 * which firmware file to load.
1537 *
1538 * The returned information are hardware variant and revision plus
1539 * firmware variant, revision and build number.
1540 */
1541 skb = __hci_cmd_sync(hdev, 0xfc05, 0, NULL, HCI_INIT_TIMEOUT);
1542 if (IS_ERR(skb)) {
1543 BT_ERR("%s reading Intel fw version command failed (%ld)",
1544 hdev->name, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001545 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001546 }
1547
1548 if (skb->len != sizeof(*ver)) {
1549 BT_ERR("%s Intel version event length mismatch", hdev->name);
1550 kfree_skb(skb);
1551 return -EIO;
1552 }
1553
1554 ver = (struct intel_version *)skb->data;
1555 if (ver->status) {
1556 BT_ERR("%s Intel fw version event failed (%02x)", hdev->name,
1557 ver->status);
1558 kfree_skb(skb);
1559 return -bt_to_errno(ver->status);
1560 }
1561
1562 BT_INFO("%s: read Intel version: %02x%02x%02x%02x%02x%02x%02x%02x%02x",
1563 hdev->name, ver->hw_platform, ver->hw_variant,
1564 ver->hw_revision, ver->fw_variant, ver->fw_revision,
1565 ver->fw_build_num, ver->fw_build_ww, ver->fw_build_yy,
1566 ver->fw_patch_num);
1567
1568 /* fw_patch_num indicates the version of patch the device currently
1569 * have. If there is no patch data in the device, it is always 0x00.
1570 * So, if it is other than 0x00, no need to patch the deivce again.
1571 */
1572 if (ver->fw_patch_num) {
1573 BT_INFO("%s: Intel device is already patched. patch num: %02x",
1574 hdev->name, ver->fw_patch_num);
1575 kfree_skb(skb);
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07001576 btintel_check_bdaddr(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001577 return 0;
1578 }
1579
1580 /* Opens the firmware patch file based on the firmware version read
1581 * from the controller. If it fails to open the matching firmware
1582 * patch file, it tries to open the default firmware patch file.
1583 * If no patch file is found, allow the device to operate without
1584 * a patch.
1585 */
1586 fw = btusb_setup_intel_get_fw(hdev, ver);
1587 if (!fw) {
1588 kfree_skb(skb);
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07001589 btintel_check_bdaddr(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001590 return 0;
1591 }
1592 fw_ptr = fw->data;
1593
1594 /* This Intel specific command enables the manufacturer mode of the
1595 * controller.
1596 *
1597 * Only while this mode is enabled, the driver can download the
1598 * firmware patch data and configuration parameters.
1599 */
1600 skb = __hci_cmd_sync(hdev, 0xfc11, 2, mfg_enable, HCI_INIT_TIMEOUT);
1601 if (IS_ERR(skb)) {
1602 BT_ERR("%s entering Intel manufacturer mode failed (%ld)",
1603 hdev->name, PTR_ERR(skb));
1604 release_firmware(fw);
Adam Leed9c78e92013-07-10 10:02:12 +08001605 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001606 }
1607
1608 if (skb->data[0]) {
1609 u8 evt_status = skb->data[0];
Marcel Holtmann89e75332014-09-16 04:44:50 +02001610
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001611 BT_ERR("%s enable Intel manufacturer mode event failed (%02x)",
1612 hdev->name, evt_status);
1613 kfree_skb(skb);
1614 release_firmware(fw);
1615 return -bt_to_errno(evt_status);
1616 }
1617 kfree_skb(skb);
1618
1619 disable_patch = 1;
1620
1621 /* The firmware data file consists of list of Intel specific HCI
1622 * commands and its expected events. The first byte indicates the
1623 * type of the message, either HCI command or HCI event.
1624 *
1625 * It reads the command and its expected event from the firmware file,
1626 * and send to the controller. Once __hci_cmd_sync_ev() returns,
1627 * the returned event is compared with the event read from the firmware
1628 * file and it will continue until all the messages are downloaded to
1629 * the controller.
1630 *
1631 * Once the firmware patching is completed successfully,
1632 * the manufacturer mode is disabled with reset and activating the
1633 * downloaded patch.
1634 *
1635 * If the firmware patching fails, the manufacturer mode is
1636 * disabled with reset and deactivating the patch.
1637 *
1638 * If the default patch file is used, no reset is done when disabling
1639 * the manufacturer.
1640 */
1641 while (fw->size > fw_ptr - fw->data) {
1642 int ret;
1643
1644 ret = btusb_setup_intel_patching(hdev, fw, &fw_ptr,
1645 &disable_patch);
1646 if (ret < 0)
1647 goto exit_mfg_deactivate;
1648 }
1649
1650 release_firmware(fw);
1651
1652 if (disable_patch)
1653 goto exit_mfg_disable;
1654
1655 /* Patching completed successfully and disable the manufacturer mode
1656 * with reset and activate the downloaded firmware patches.
1657 */
1658 skb = __hci_cmd_sync(hdev, 0xfc11, sizeof(mfg_reset_activate),
1659 mfg_reset_activate, HCI_INIT_TIMEOUT);
1660 if (IS_ERR(skb)) {
1661 BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
1662 hdev->name, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001663 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001664 }
1665 kfree_skb(skb);
1666
1667 BT_INFO("%s: Intel Bluetooth firmware patch completed and activated",
1668 hdev->name);
1669
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07001670 btintel_check_bdaddr(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001671 return 0;
1672
1673exit_mfg_disable:
1674 /* Disable the manufacturer mode without reset */
1675 skb = __hci_cmd_sync(hdev, 0xfc11, sizeof(mfg_disable), mfg_disable,
1676 HCI_INIT_TIMEOUT);
1677 if (IS_ERR(skb)) {
1678 BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
1679 hdev->name, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001680 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001681 }
1682 kfree_skb(skb);
1683
1684 BT_INFO("%s: Intel Bluetooth firmware patch completed", hdev->name);
Marcel Holtmann40cb0982014-07-02 12:06:45 +02001685
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07001686 btintel_check_bdaddr(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001687 return 0;
1688
1689exit_mfg_deactivate:
1690 release_firmware(fw);
1691
1692 /* Patching failed. Disable the manufacturer mode with reset and
1693 * deactivate the downloaded firmware patches.
1694 */
1695 skb = __hci_cmd_sync(hdev, 0xfc11, sizeof(mfg_reset_deactivate),
1696 mfg_reset_deactivate, HCI_INIT_TIMEOUT);
1697 if (IS_ERR(skb)) {
1698 BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
1699 hdev->name, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001700 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001701 }
1702 kfree_skb(skb);
1703
1704 BT_INFO("%s: Intel Bluetooth firmware patch completed and deactivated",
1705 hdev->name);
1706
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07001707 btintel_check_bdaddr(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001708 return 0;
1709}
1710
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001711static int inject_cmd_complete(struct hci_dev *hdev, __u16 opcode)
1712{
1713 struct sk_buff *skb;
1714 struct hci_event_hdr *hdr;
1715 struct hci_ev_cmd_complete *evt;
1716
1717 skb = bt_skb_alloc(sizeof(*hdr) + sizeof(*evt) + 1, GFP_ATOMIC);
1718 if (!skb)
1719 return -ENOMEM;
1720
1721 hdr = (struct hci_event_hdr *)skb_put(skb, sizeof(*hdr));
1722 hdr->evt = HCI_EV_CMD_COMPLETE;
1723 hdr->plen = sizeof(*evt) + 1;
1724
1725 evt = (struct hci_ev_cmd_complete *)skb_put(skb, sizeof(*evt));
1726 evt->ncmd = 0x01;
1727 evt->opcode = cpu_to_le16(opcode);
1728
1729 *skb_put(skb, 1) = 0x00;
1730
1731 bt_cb(skb)->pkt_type = HCI_EVENT_PKT;
1732
1733 return hci_recv_frame(hdev, skb);
1734}
1735
1736static int btusb_recv_bulk_intel(struct btusb_data *data, void *buffer,
1737 int count)
1738{
1739 /* When the device is in bootloader mode, then it can send
1740 * events via the bulk endpoint. These events are treated the
1741 * same way as the ones received from the interrupt endpoint.
1742 */
1743 if (test_bit(BTUSB_BOOTLOADER, &data->flags))
1744 return btusb_recv_intr(data, buffer, count);
1745
1746 return btusb_recv_bulk(data, buffer, count);
1747}
1748
Marcel Holtmannccd6da22015-04-09 00:35:19 -07001749static void btusb_intel_bootup(struct btusb_data *data, const void *ptr,
1750 unsigned int len)
1751{
1752 const struct intel_bootup *evt = ptr;
1753
1754 if (len != sizeof(*evt))
1755 return;
1756
1757 if (test_and_clear_bit(BTUSB_BOOTING, &data->flags)) {
1758 smp_mb__after_atomic();
1759 wake_up_bit(&data->flags, BTUSB_BOOTING);
1760 }
1761}
1762
1763static void btusb_intel_secure_send_result(struct btusb_data *data,
1764 const void *ptr, unsigned int len)
1765{
1766 const struct intel_secure_send_result *evt = ptr;
1767
1768 if (len != sizeof(*evt))
1769 return;
1770
1771 if (evt->result)
1772 set_bit(BTUSB_FIRMWARE_FAILED, &data->flags);
1773
1774 if (test_and_clear_bit(BTUSB_DOWNLOADING, &data->flags) &&
1775 test_bit(BTUSB_FIRMWARE_LOADED, &data->flags)) {
1776 smp_mb__after_atomic();
1777 wake_up_bit(&data->flags, BTUSB_DOWNLOADING);
1778 }
1779}
1780
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001781static int btusb_recv_event_intel(struct hci_dev *hdev, struct sk_buff *skb)
1782{
1783 struct btusb_data *data = hci_get_drvdata(hdev);
1784
1785 if (test_bit(BTUSB_BOOTLOADER, &data->flags)) {
1786 struct hci_event_hdr *hdr = (void *)skb->data;
1787
Marcel Holtmannccd6da22015-04-09 00:35:19 -07001788 if (skb->len > HCI_EVENT_HDR_SIZE && hdr->evt == 0xff &&
1789 hdr->plen > 0) {
1790 const void *ptr = skb->data + HCI_EVENT_HDR_SIZE + 1;
1791 unsigned int len = skb->len - HCI_EVENT_HDR_SIZE - 1;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001792
Marcel Holtmannccd6da22015-04-09 00:35:19 -07001793 switch (skb->data[2]) {
1794 case 0x02:
1795 /* When switching to the operational firmware
1796 * the device sends a vendor specific event
1797 * indicating that the bootup completed.
1798 */
1799 btusb_intel_bootup(data, ptr, len);
1800 break;
1801 case 0x06:
1802 /* When the firmware loading completes the
1803 * device sends out a vendor specific event
1804 * indicating the result of the firmware
1805 * loading.
1806 */
1807 btusb_intel_secure_send_result(data, ptr, len);
1808 break;
Johan Hedbergfad70972015-01-30 10:58:55 +02001809 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001810 }
1811 }
1812
1813 return hci_recv_frame(hdev, skb);
1814}
1815
1816static int btusb_send_frame_intel(struct hci_dev *hdev, struct sk_buff *skb)
1817{
1818 struct btusb_data *data = hci_get_drvdata(hdev);
1819 struct urb *urb;
1820
1821 BT_DBG("%s", hdev->name);
1822
1823 if (!test_bit(HCI_RUNNING, &hdev->flags))
1824 return -EBUSY;
1825
1826 switch (bt_cb(skb)->pkt_type) {
1827 case HCI_COMMAND_PKT:
1828 if (test_bit(BTUSB_BOOTLOADER, &data->flags)) {
1829 struct hci_command_hdr *cmd = (void *)skb->data;
1830 __u16 opcode = le16_to_cpu(cmd->opcode);
1831
1832 /* When in bootloader mode and the command 0xfc09
1833 * is received, it needs to be send down the
1834 * bulk endpoint. So allocate a bulk URB instead.
1835 */
1836 if (opcode == 0xfc09)
1837 urb = alloc_bulk_urb(hdev, skb);
1838 else
1839 urb = alloc_ctrl_urb(hdev, skb);
1840
1841 /* When the 0xfc01 command is issued to boot into
1842 * the operational firmware, it will actually not
1843 * send a command complete event. To keep the flow
1844 * control working inject that event here.
1845 */
1846 if (opcode == 0xfc01)
1847 inject_cmd_complete(hdev, opcode);
1848 } else {
1849 urb = alloc_ctrl_urb(hdev, skb);
1850 }
1851 if (IS_ERR(urb))
1852 return PTR_ERR(urb);
1853
1854 hdev->stat.cmd_tx++;
1855 return submit_or_queue_tx_urb(hdev, urb);
1856
1857 case HCI_ACLDATA_PKT:
1858 urb = alloc_bulk_urb(hdev, skb);
1859 if (IS_ERR(urb))
1860 return PTR_ERR(urb);
1861
1862 hdev->stat.acl_tx++;
1863 return submit_or_queue_tx_urb(hdev, urb);
1864
1865 case HCI_SCODATA_PKT:
1866 if (hci_conn_num(hdev, SCO_LINK) < 1)
1867 return -ENODEV;
1868
1869 urb = alloc_isoc_urb(hdev, skb);
1870 if (IS_ERR(urb))
1871 return PTR_ERR(urb);
1872
1873 hdev->stat.sco_tx++;
1874 return submit_tx_urb(hdev, urb);
1875 }
1876
1877 return -EILSEQ;
1878}
1879
1880static int btusb_intel_secure_send(struct hci_dev *hdev, u8 fragment_type,
1881 u32 plen, const void *param)
1882{
1883 while (plen > 0) {
1884 struct sk_buff *skb;
1885 u8 cmd_param[253], fragment_len = (plen > 252) ? 252 : plen;
1886
1887 cmd_param[0] = fragment_type;
1888 memcpy(cmd_param + 1, param, fragment_len);
1889
1890 skb = __hci_cmd_sync(hdev, 0xfc09, fragment_len + 1,
1891 cmd_param, HCI_INIT_TIMEOUT);
1892 if (IS_ERR(skb))
1893 return PTR_ERR(skb);
1894
1895 kfree_skb(skb);
1896
1897 plen -= fragment_len;
1898 param += fragment_len;
1899 }
1900
1901 return 0;
1902}
1903
1904static void btusb_intel_version_info(struct hci_dev *hdev,
1905 struct intel_version *ver)
1906{
1907 const char *variant;
1908
1909 switch (ver->fw_variant) {
1910 case 0x06:
1911 variant = "Bootloader";
1912 break;
1913 case 0x23:
1914 variant = "Firmware";
1915 break;
1916 default:
1917 return;
1918 }
1919
1920 BT_INFO("%s: %s revision %u.%u build %u week %u %u", hdev->name,
1921 variant, ver->fw_revision >> 4, ver->fw_revision & 0x0f,
1922 ver->fw_build_num, ver->fw_build_ww, 2000 + ver->fw_build_yy);
1923}
1924
1925static int btusb_setup_intel_new(struct hci_dev *hdev)
1926{
1927 static const u8 reset_param[] = { 0x00, 0x01, 0x00, 0x01,
1928 0x00, 0x08, 0x04, 0x00 };
1929 struct btusb_data *data = hci_get_drvdata(hdev);
1930 struct sk_buff *skb;
1931 struct intel_version *ver;
1932 struct intel_boot_params *params;
1933 const struct firmware *fw;
1934 const u8 *fw_ptr;
1935 char fwname[64];
1936 ktime_t calltime, delta, rettime;
1937 unsigned long long duration;
1938 int err;
1939
1940 BT_DBG("%s", hdev->name);
1941
1942 calltime = ktime_get();
1943
1944 /* Read the Intel version information to determine if the device
1945 * is in bootloader mode or if it already has operational firmware
1946 * loaded.
1947 */
1948 skb = __hci_cmd_sync(hdev, 0xfc05, 0, NULL, HCI_INIT_TIMEOUT);
1949 if (IS_ERR(skb)) {
1950 BT_ERR("%s: Reading Intel version information failed (%ld)",
1951 hdev->name, PTR_ERR(skb));
1952 return PTR_ERR(skb);
1953 }
1954
1955 if (skb->len != sizeof(*ver)) {
1956 BT_ERR("%s: Intel version event size mismatch", hdev->name);
1957 kfree_skb(skb);
1958 return -EILSEQ;
1959 }
1960
1961 ver = (struct intel_version *)skb->data;
1962 if (ver->status) {
1963 BT_ERR("%s: Intel version command failure (%02x)",
1964 hdev->name, ver->status);
1965 err = -bt_to_errno(ver->status);
1966 kfree_skb(skb);
1967 return err;
1968 }
1969
1970 /* The hardware platform number has a fixed value of 0x37 and
1971 * for now only accept this single value.
1972 */
1973 if (ver->hw_platform != 0x37) {
1974 BT_ERR("%s: Unsupported Intel hardware platform (%u)",
1975 hdev->name, ver->hw_platform);
1976 kfree_skb(skb);
1977 return -EINVAL;
1978 }
1979
1980 /* At the moment only the hardware variant iBT 3.0 (LnP/SfP) is
1981 * supported by this firmware loading method. This check has been
1982 * put in place to ensure correct forward compatibility options
1983 * when newer hardware variants come along.
1984 */
1985 if (ver->hw_variant != 0x0b) {
1986 BT_ERR("%s: Unsupported Intel hardware variant (%u)",
1987 hdev->name, ver->hw_variant);
1988 kfree_skb(skb);
1989 return -EINVAL;
1990 }
1991
1992 btusb_intel_version_info(hdev, ver);
1993
1994 /* The firmware variant determines if the device is in bootloader
1995 * mode or is running operational firmware. The value 0x06 identifies
1996 * the bootloader and the value 0x23 identifies the operational
1997 * firmware.
1998 *
1999 * When the operational firmware is already present, then only
2000 * the check for valid Bluetooth device address is needed. This
2001 * determines if the device will be added as configured or
2002 * unconfigured controller.
2003 *
2004 * It is not possible to use the Secure Boot Parameters in this
2005 * case since that command is only available in bootloader mode.
2006 */
2007 if (ver->fw_variant == 0x23) {
2008 kfree_skb(skb);
2009 clear_bit(BTUSB_BOOTLOADER, &data->flags);
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07002010 btintel_check_bdaddr(hdev);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002011 return 0;
2012 }
2013
2014 /* If the device is not in bootloader mode, then the only possible
2015 * choice is to return an error and abort the device initialization.
2016 */
2017 if (ver->fw_variant != 0x06) {
2018 BT_ERR("%s: Unsupported Intel firmware variant (%u)",
2019 hdev->name, ver->fw_variant);
2020 kfree_skb(skb);
2021 return -ENODEV;
2022 }
2023
2024 kfree_skb(skb);
2025
2026 /* Read the secure boot parameters to identify the operating
2027 * details of the bootloader.
2028 */
2029 skb = __hci_cmd_sync(hdev, 0xfc0d, 0, NULL, HCI_INIT_TIMEOUT);
2030 if (IS_ERR(skb)) {
2031 BT_ERR("%s: Reading Intel boot parameters failed (%ld)",
2032 hdev->name, PTR_ERR(skb));
2033 return PTR_ERR(skb);
2034 }
2035
2036 if (skb->len != sizeof(*params)) {
2037 BT_ERR("%s: Intel boot parameters size mismatch", hdev->name);
2038 kfree_skb(skb);
2039 return -EILSEQ;
2040 }
2041
2042 params = (struct intel_boot_params *)skb->data;
2043 if (params->status) {
2044 BT_ERR("%s: Intel boot parameters command failure (%02x)",
2045 hdev->name, params->status);
2046 err = -bt_to_errno(params->status);
2047 kfree_skb(skb);
2048 return err;
2049 }
2050
2051 BT_INFO("%s: Device revision is %u", hdev->name,
2052 le16_to_cpu(params->dev_revid));
2053
2054 BT_INFO("%s: Secure boot is %s", hdev->name,
2055 params->secure_boot ? "enabled" : "disabled");
2056
2057 BT_INFO("%s: Minimum firmware build %u week %u %u", hdev->name,
2058 params->min_fw_build_nn, params->min_fw_build_cw,
2059 2000 + params->min_fw_build_yy);
2060
2061 /* It is required that every single firmware fragment is acknowledged
2062 * with a command complete event. If the boot parameters indicate
2063 * that this bootloader does not send them, then abort the setup.
2064 */
2065 if (params->limited_cce != 0x00) {
2066 BT_ERR("%s: Unsupported Intel firmware loading method (%u)",
2067 hdev->name, params->limited_cce);
2068 kfree_skb(skb);
2069 return -EINVAL;
2070 }
2071
2072 /* If the OTP has no valid Bluetooth device address, then there will
2073 * also be no valid address for the operational firmware.
2074 */
2075 if (!bacmp(&params->otp_bdaddr, BDADDR_ANY)) {
2076 BT_INFO("%s: No device address configured", hdev->name);
2077 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
2078 }
2079
2080 /* With this Intel bootloader only the hardware variant and device
2081 * revision information are used to select the right firmware.
2082 *
2083 * Currently this bootloader support is limited to hardware variant
2084 * iBT 3.0 (LnP/SfP) which is identified by the value 11 (0x0b).
2085 */
2086 snprintf(fwname, sizeof(fwname), "intel/ibt-11-%u.sfi",
2087 le16_to_cpu(params->dev_revid));
2088
2089 err = request_firmware(&fw, fwname, &hdev->dev);
2090 if (err < 0) {
2091 BT_ERR("%s: Failed to load Intel firmware file (%d)",
2092 hdev->name, err);
2093 kfree_skb(skb);
2094 return err;
2095 }
2096
2097 BT_INFO("%s: Found device firmware: %s", hdev->name, fwname);
2098
2099 kfree_skb(skb);
2100
2101 if (fw->size < 644) {
2102 BT_ERR("%s: Invalid size of firmware file (%zu)",
2103 hdev->name, fw->size);
2104 err = -EBADF;
2105 goto done;
2106 }
2107
2108 set_bit(BTUSB_DOWNLOADING, &data->flags);
2109
2110 /* Start the firmware download transaction with the Init fragment
2111 * represented by the 128 bytes of CSS header.
2112 */
2113 err = btusb_intel_secure_send(hdev, 0x00, 128, fw->data);
2114 if (err < 0) {
2115 BT_ERR("%s: Failed to send firmware header (%d)",
2116 hdev->name, err);
2117 goto done;
2118 }
2119
2120 /* Send the 256 bytes of public key information from the firmware
2121 * as the PKey fragment.
2122 */
2123 err = btusb_intel_secure_send(hdev, 0x03, 256, fw->data + 128);
2124 if (err < 0) {
2125 BT_ERR("%s: Failed to send firmware public key (%d)",
2126 hdev->name, err);
2127 goto done;
2128 }
2129
2130 /* Send the 256 bytes of signature information from the firmware
2131 * as the Sign fragment.
2132 */
2133 err = btusb_intel_secure_send(hdev, 0x02, 256, fw->data + 388);
2134 if (err < 0) {
2135 BT_ERR("%s: Failed to send firmware signature (%d)",
2136 hdev->name, err);
2137 goto done;
2138 }
2139
2140 fw_ptr = fw->data + 644;
2141
2142 while (fw_ptr - fw->data < fw->size) {
2143 struct hci_command_hdr *cmd = (void *)fw_ptr;
2144 u8 cmd_len;
2145
2146 cmd_len = sizeof(*cmd) + cmd->plen;
2147
2148 /* Send each command from the firmware data buffer as
2149 * a single Data fragment.
2150 */
2151 err = btusb_intel_secure_send(hdev, 0x01, cmd_len, fw_ptr);
2152 if (err < 0) {
2153 BT_ERR("%s: Failed to send firmware data (%d)",
2154 hdev->name, err);
2155 goto done;
2156 }
2157
2158 fw_ptr += cmd_len;
2159 }
2160
Marcel Holtmannce6bb922015-01-28 01:58:40 -08002161 set_bit(BTUSB_FIRMWARE_LOADED, &data->flags);
2162
Johan Hedberga087a982015-01-30 10:58:54 +02002163 BT_INFO("%s: Waiting for firmware download to complete", hdev->name);
2164
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002165 /* Before switching the device into operational mode and with that
2166 * booting the loaded firmware, wait for the bootloader notification
2167 * that all fragments have been successfully received.
2168 *
Johan Hedberga087a982015-01-30 10:58:54 +02002169 * When the event processing receives the notification, then the
2170 * BTUSB_DOWNLOADING flag will be cleared.
2171 *
2172 * The firmware loading should not take longer than 5 seconds
2173 * and thus just timeout if that happens and fail the setup
2174 * of this device.
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002175 */
Johan Hedberg129a7692015-02-14 09:33:35 +02002176 err = wait_on_bit_timeout(&data->flags, BTUSB_DOWNLOADING,
2177 TASK_INTERRUPTIBLE,
2178 msecs_to_jiffies(5000));
Johan Hedberga087a982015-01-30 10:58:54 +02002179 if (err == 1) {
2180 BT_ERR("%s: Firmware loading interrupted", hdev->name);
2181 err = -EINTR;
2182 goto done;
2183 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002184
Johan Hedberga087a982015-01-30 10:58:54 +02002185 if (err) {
2186 BT_ERR("%s: Firmware loading timeout", hdev->name);
2187 err = -ETIMEDOUT;
2188 goto done;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002189 }
2190
2191 if (test_bit(BTUSB_FIRMWARE_FAILED, &data->flags)) {
2192 BT_ERR("%s: Firmware loading failed", hdev->name);
2193 err = -ENOEXEC;
2194 goto done;
2195 }
2196
2197 rettime = ktime_get();
2198 delta = ktime_sub(rettime, calltime);
2199 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
2200
2201 BT_INFO("%s: Firmware loaded in %llu usecs", hdev->name, duration);
2202
2203done:
2204 release_firmware(fw);
2205
2206 if (err < 0)
2207 return err;
2208
2209 calltime = ktime_get();
2210
2211 set_bit(BTUSB_BOOTING, &data->flags);
2212
2213 skb = __hci_cmd_sync(hdev, 0xfc01, sizeof(reset_param), reset_param,
2214 HCI_INIT_TIMEOUT);
2215 if (IS_ERR(skb))
2216 return PTR_ERR(skb);
2217
2218 kfree_skb(skb);
2219
2220 /* The bootloader will not indicate when the device is ready. This
2221 * is done by the operational firmware sending bootup notification.
Johan Hedbergfad70972015-01-30 10:58:55 +02002222 *
2223 * Booting into operational firmware should not take longer than
2224 * 1 second. However if that happens, then just fail the setup
2225 * since something went wrong.
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002226 */
Johan Hedbergfad70972015-01-30 10:58:55 +02002227 BT_INFO("%s: Waiting for device to boot", hdev->name);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002228
Johan Hedberg129a7692015-02-14 09:33:35 +02002229 err = wait_on_bit_timeout(&data->flags, BTUSB_BOOTING,
2230 TASK_INTERRUPTIBLE,
2231 msecs_to_jiffies(1000));
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002232
Johan Hedbergfad70972015-01-30 10:58:55 +02002233 if (err == 1) {
2234 BT_ERR("%s: Device boot interrupted", hdev->name);
2235 return -EINTR;
2236 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002237
Johan Hedbergfad70972015-01-30 10:58:55 +02002238 if (err) {
2239 BT_ERR("%s: Device boot timeout", hdev->name);
2240 return -ETIMEDOUT;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002241 }
2242
2243 rettime = ktime_get();
2244 delta = ktime_sub(rettime, calltime);
2245 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
2246
2247 BT_INFO("%s: Device booted in %llu usecs", hdev->name, duration);
2248
2249 clear_bit(BTUSB_BOOTLOADER, &data->flags);
2250
2251 return 0;
2252}
2253
Marcel Holtmann385a7682015-01-28 11:09:56 -08002254static void btusb_hw_error_intel(struct hci_dev *hdev, u8 code)
2255{
2256 struct sk_buff *skb;
2257 u8 type = 0x00;
2258
2259 BT_ERR("%s: Hardware error 0x%2.2x", hdev->name, code);
2260
2261 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
2262 if (IS_ERR(skb)) {
2263 BT_ERR("%s: Reset after hardware error failed (%ld)",
2264 hdev->name, PTR_ERR(skb));
2265 return;
2266 }
2267 kfree_skb(skb);
2268
2269 skb = __hci_cmd_sync(hdev, 0xfc22, 1, &type, HCI_INIT_TIMEOUT);
2270 if (IS_ERR(skb)) {
2271 BT_ERR("%s: Retrieving Intel exception info failed (%ld)",
2272 hdev->name, PTR_ERR(skb));
2273 return;
2274 }
2275
2276 if (skb->len != 13) {
2277 BT_ERR("%s: Exception info size mismatch", hdev->name);
2278 kfree_skb(skb);
2279 return;
2280 }
2281
2282 if (skb->data[0] != 0x00) {
2283 BT_ERR("%s: Exception info command failure (%02x)",
2284 hdev->name, skb->data[0]);
2285 kfree_skb(skb);
2286 return;
2287 }
2288
2289 BT_ERR("%s: Exception info %s", hdev->name, (char *)(skb->data + 1));
2290
2291 kfree_skb(skb);
2292}
2293
Tedd Ho-Jeong Anbfbd45e2015-02-13 09:20:52 -08002294static int btusb_shutdown_intel(struct hci_dev *hdev)
2295{
2296 struct sk_buff *skb;
2297 long ret;
2298
2299 /* Some platforms have an issue with BT LED when the interface is
2300 * down or BT radio is turned off, which takes 5 seconds to BT LED
2301 * goes off. This command turns off the BT LED immediately.
2302 */
2303 skb = __hci_cmd_sync(hdev, 0xfc3f, 0, NULL, HCI_INIT_TIMEOUT);
2304 if (IS_ERR(skb)) {
2305 ret = PTR_ERR(skb);
2306 BT_ERR("%s: turning off Intel device LED failed (%ld)",
2307 hdev->name, ret);
2308 return ret;
2309 }
2310 kfree_skb(skb);
2311
2312 return 0;
2313}
2314
Amitkumar Karwarae8df492014-07-18 14:47:06 -07002315static int btusb_set_bdaddr_marvell(struct hci_dev *hdev,
2316 const bdaddr_t *bdaddr)
2317{
2318 struct sk_buff *skb;
2319 u8 buf[8];
2320 long ret;
2321
2322 buf[0] = 0xfe;
2323 buf[1] = sizeof(bdaddr_t);
2324 memcpy(buf + 2, bdaddr, sizeof(bdaddr_t));
2325
2326 skb = __hci_cmd_sync(hdev, 0xfc22, sizeof(buf), buf, HCI_INIT_TIMEOUT);
2327 if (IS_ERR(skb)) {
2328 ret = PTR_ERR(skb);
2329 BT_ERR("%s: changing Marvell device address failed (%ld)",
2330 hdev->name, ret);
2331 return ret;
2332 }
2333 kfree_skb(skb);
2334
2335 return 0;
2336}
2337
Toshi Kikuchi58592232014-12-12 10:58:05 -08002338static int btusb_set_bdaddr_ath3012(struct hci_dev *hdev,
2339 const bdaddr_t *bdaddr)
2340{
2341 struct sk_buff *skb;
2342 u8 buf[10];
2343 long ret;
2344
2345 buf[0] = 0x01;
2346 buf[1] = 0x01;
2347 buf[2] = 0x00;
2348 buf[3] = sizeof(bdaddr_t);
2349 memcpy(buf + 4, bdaddr, sizeof(bdaddr_t));
2350
2351 skb = __hci_cmd_sync(hdev, 0xfc0b, sizeof(buf), buf, HCI_INIT_TIMEOUT);
2352 if (IS_ERR(skb)) {
2353 ret = PTR_ERR(skb);
2354 BT_ERR("%s: Change address command failed (%ld)",
2355 hdev->name, ret);
2356 return ret;
2357 }
2358 kfree_skb(skb);
2359
2360 return 0;
2361}
2362
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002363#define QCA_DFU_PACKET_LEN 4096
2364
2365#define QCA_GET_TARGET_VERSION 0x09
2366#define QCA_CHECK_STATUS 0x05
2367#define QCA_DFU_DOWNLOAD 0x01
2368
2369#define QCA_SYSCFG_UPDATED 0x40
2370#define QCA_PATCH_UPDATED 0x80
2371#define QCA_DFU_TIMEOUT 3000
2372
2373struct qca_version {
2374 __le32 rom_version;
2375 __le32 patch_version;
2376 __le32 ram_version;
2377 __le32 ref_clock;
2378 __u8 reserved[4];
2379} __packed;
2380
2381struct qca_rampatch_version {
2382 __le16 rom_version;
2383 __le16 patch_version;
2384} __packed;
2385
2386struct qca_device_info {
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002387 u32 rom_version;
2388 u8 rampatch_hdr; /* length of header in rampatch */
2389 u8 nvm_hdr; /* length of header in NVM */
2390 u8 ver_offset; /* offset of version structure in rampatch */
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002391};
2392
2393static const struct qca_device_info qca_devices_table[] = {
2394 { 0x00000100, 20, 4, 10 }, /* Rome 1.0 */
2395 { 0x00000101, 20, 4, 10 }, /* Rome 1.1 */
2396 { 0x00000201, 28, 4, 18 }, /* Rome 2.1 */
2397 { 0x00000300, 28, 4, 18 }, /* Rome 3.0 */
2398 { 0x00000302, 28, 4, 18 }, /* Rome 3.2 */
2399};
2400
2401static int btusb_qca_send_vendor_req(struct hci_dev *hdev, u8 request,
2402 void *data, u16 size)
2403{
2404 struct btusb_data *btdata = hci_get_drvdata(hdev);
2405 struct usb_device *udev = btdata->udev;
2406 int pipe, err;
2407 u8 *buf;
2408
2409 buf = kmalloc(size, GFP_KERNEL);
2410 if (!buf)
2411 return -ENOMEM;
2412
2413 /* Found some of USB hosts have IOT issues with ours so that we should
2414 * not wait until HCI layer is ready.
2415 */
2416 pipe = usb_rcvctrlpipe(udev, 0);
2417 err = usb_control_msg(udev, pipe, request, USB_TYPE_VENDOR | USB_DIR_IN,
2418 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
2419 if (err < 0) {
2420 BT_ERR("%s: Failed to access otp area (%d)", hdev->name, err);
2421 goto done;
2422 }
2423
2424 memcpy(data, buf, size);
2425
2426done:
2427 kfree(buf);
2428
2429 return err;
2430}
2431
2432static int btusb_setup_qca_download_fw(struct hci_dev *hdev,
2433 const struct firmware *firmware,
2434 size_t hdr_size)
2435{
2436 struct btusb_data *btdata = hci_get_drvdata(hdev);
2437 struct usb_device *udev = btdata->udev;
2438 size_t count, size, sent = 0;
2439 int pipe, len, err;
2440 u8 *buf;
2441
2442 buf = kmalloc(QCA_DFU_PACKET_LEN, GFP_KERNEL);
2443 if (!buf)
2444 return -ENOMEM;
2445
2446 count = firmware->size;
2447
2448 size = min_t(size_t, count, hdr_size);
2449 memcpy(buf, firmware->data, size);
2450
2451 /* USB patches should go down to controller through USB path
2452 * because binary format fits to go down through USB channel.
2453 * USB control path is for patching headers and USB bulk is for
2454 * patch body.
2455 */
2456 pipe = usb_sndctrlpipe(udev, 0);
2457 err = usb_control_msg(udev, pipe, QCA_DFU_DOWNLOAD, USB_TYPE_VENDOR,
2458 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
2459 if (err < 0) {
2460 BT_ERR("%s: Failed to send headers (%d)", hdev->name, err);
2461 goto done;
2462 }
2463
2464 sent += size;
2465 count -= size;
2466
2467 while (count) {
2468 size = min_t(size_t, count, QCA_DFU_PACKET_LEN);
2469
2470 memcpy(buf, firmware->data + sent, size);
2471
2472 pipe = usb_sndbulkpipe(udev, 0x02);
2473 err = usb_bulk_msg(udev, pipe, buf, size, &len,
2474 QCA_DFU_TIMEOUT);
2475 if (err < 0) {
2476 BT_ERR("%s: Failed to send body at %zd of %zd (%d)",
2477 hdev->name, sent, firmware->size, err);
2478 break;
2479 }
2480
2481 if (size != len) {
2482 BT_ERR("%s: Failed to get bulk buffer", hdev->name);
2483 err = -EILSEQ;
2484 break;
2485 }
2486
2487 sent += size;
2488 count -= size;
2489 }
2490
2491done:
2492 kfree(buf);
2493 return err;
2494}
2495
2496static int btusb_setup_qca_load_rampatch(struct hci_dev *hdev,
2497 struct qca_version *ver,
2498 const struct qca_device_info *info)
2499{
2500 struct qca_rampatch_version *rver;
2501 const struct firmware *fw;
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002502 u32 ver_rom, ver_patch;
2503 u16 rver_rom, rver_patch;
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002504 char fwname[64];
2505 int err;
2506
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002507 ver_rom = le32_to_cpu(ver->rom_version);
2508 ver_patch = le32_to_cpu(ver->patch_version);
2509
2510 snprintf(fwname, sizeof(fwname), "qca/rampatch_usb_%08x.bin", ver_rom);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002511
2512 err = request_firmware(&fw, fwname, &hdev->dev);
2513 if (err) {
2514 BT_ERR("%s: failed to request rampatch file: %s (%d)",
2515 hdev->name, fwname, err);
2516 return err;
2517 }
2518
2519 BT_INFO("%s: using rampatch file: %s", hdev->name, fwname);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002520
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002521 rver = (struct qca_rampatch_version *)(fw->data + info->ver_offset);
2522 rver_rom = le16_to_cpu(rver->rom_version);
2523 rver_patch = le16_to_cpu(rver->patch_version);
2524
2525 BT_INFO("%s: QCA: patch rome 0x%x build 0x%x, firmware rome 0x%x "
2526 "build 0x%x", hdev->name, rver_rom, rver_patch, ver_rom,
2527 ver_patch);
2528
2529 if (rver_rom != ver_rom || rver_patch <= ver_patch) {
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002530 BT_ERR("%s: rampatch file version did not match with firmware",
2531 hdev->name);
2532 err = -EINVAL;
2533 goto done;
2534 }
2535
2536 err = btusb_setup_qca_download_fw(hdev, fw, info->rampatch_hdr);
2537
2538done:
2539 release_firmware(fw);
2540
2541 return err;
2542}
2543
2544static int btusb_setup_qca_load_nvm(struct hci_dev *hdev,
2545 struct qca_version *ver,
2546 const struct qca_device_info *info)
2547{
2548 const struct firmware *fw;
2549 char fwname[64];
2550 int err;
2551
2552 snprintf(fwname, sizeof(fwname), "qca/nvm_usb_%08x.bin",
2553 le32_to_cpu(ver->rom_version));
2554
2555 err = request_firmware(&fw, fwname, &hdev->dev);
2556 if (err) {
2557 BT_ERR("%s: failed to request NVM file: %s (%d)",
2558 hdev->name, fwname, err);
2559 return err;
2560 }
2561
2562 BT_INFO("%s: using NVM file: %s", hdev->name, fwname);
2563
2564 err = btusb_setup_qca_download_fw(hdev, fw, info->nvm_hdr);
2565
2566 release_firmware(fw);
2567
2568 return err;
2569}
2570
2571static int btusb_setup_qca(struct hci_dev *hdev)
2572{
2573 const struct qca_device_info *info = NULL;
2574 struct qca_version ver;
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002575 u32 ver_rom;
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002576 u8 status;
2577 int i, err;
2578
2579 err = btusb_qca_send_vendor_req(hdev, QCA_GET_TARGET_VERSION, &ver,
2580 sizeof(ver));
2581 if (err < 0)
2582 return err;
2583
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002584 ver_rom = le32_to_cpu(ver.rom_version);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002585 for (i = 0; i < ARRAY_SIZE(qca_devices_table); i++) {
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002586 if (ver_rom == qca_devices_table[i].rom_version)
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002587 info = &qca_devices_table[i];
2588 }
2589 if (!info) {
2590 BT_ERR("%s: don't support firmware rome 0x%x", hdev->name,
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002591 ver_rom);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002592 return -ENODEV;
2593 }
2594
2595 err = btusb_qca_send_vendor_req(hdev, QCA_CHECK_STATUS, &status,
2596 sizeof(status));
2597 if (err < 0)
2598 return err;
2599
2600 if (!(status & QCA_PATCH_UPDATED)) {
2601 err = btusb_setup_qca_load_rampatch(hdev, &ver, info);
2602 if (err < 0)
2603 return err;
2604 }
2605
2606 if (!(status & QCA_SYSCFG_UPDATED)) {
2607 err = btusb_setup_qca_load_nvm(hdev, &ver, info);
2608 if (err < 0)
2609 return err;
2610 }
2611
2612 return 0;
2613}
2614
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002615static int btusb_probe(struct usb_interface *intf,
Marcel Holtmann89e75332014-09-16 04:44:50 +02002616 const struct usb_device_id *id)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002617{
2618 struct usb_endpoint_descriptor *ep_desc;
2619 struct btusb_data *data;
2620 struct hci_dev *hdev;
2621 int i, err;
2622
2623 BT_DBG("intf %p id %p", intf, id);
2624
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002625 /* interface numbers are hardcoded in the spec */
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002626 if (intf->cur_altsetting->desc.bInterfaceNumber != 0)
2627 return -ENODEV;
2628
2629 if (!id->driver_info) {
2630 const struct usb_device_id *match;
Marcel Holtmann89e75332014-09-16 04:44:50 +02002631
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002632 match = usb_match_id(intf, blacklist_table);
2633 if (match)
2634 id = match;
2635 }
2636
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002637 if (id->driver_info == BTUSB_IGNORE)
2638 return -ENODEV;
2639
Steven.Li2d25f8b2011-07-01 14:02:36 +08002640 if (id->driver_info & BTUSB_ATH3012) {
2641 struct usb_device *udev = interface_to_usbdev(intf);
2642
2643 /* Old firmware would otherwise let ath3k driver load
2644 * patch and sysconfig files */
2645 if (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x0001)
2646 return -ENODEV;
2647 }
2648
Sachin Kamat98921db2012-07-27 12:38:39 +05302649 data = devm_kzalloc(&intf->dev, sizeof(*data), GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002650 if (!data)
2651 return -ENOMEM;
2652
2653 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
2654 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
2655
2656 if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
2657 data->intr_ep = ep_desc;
2658 continue;
2659 }
2660
2661 if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
2662 data->bulk_tx_ep = ep_desc;
2663 continue;
2664 }
2665
2666 if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
2667 data->bulk_rx_ep = ep_desc;
2668 continue;
2669 }
2670 }
2671
Sachin Kamat98921db2012-07-27 12:38:39 +05302672 if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002673 return -ENODEV;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002674
Marcel Holtmann893ba542015-01-28 20:27:34 -08002675 if (id->driver_info & BTUSB_AMP) {
2676 data->cmdreq_type = USB_TYPE_CLASS | 0x01;
2677 data->cmdreq = 0x2b;
2678 } else {
2679 data->cmdreq_type = USB_TYPE_CLASS;
2680 data->cmdreq = 0x00;
2681 }
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002682
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002683 data->udev = interface_to_usbdev(intf);
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02002684 data->intf = intf;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002685
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002686 INIT_WORK(&data->work, btusb_work);
Oliver Neukum7bee5492009-08-24 23:44:59 +02002687 INIT_WORK(&data->waker, btusb_waker);
Marcel Holtmann803b5832014-09-16 08:00:29 +02002688 init_usb_anchor(&data->deferred);
2689 init_usb_anchor(&data->tx_anchor);
Oliver Neukum7bee5492009-08-24 23:44:59 +02002690 spin_lock_init(&data->txlock);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002691
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002692 init_usb_anchor(&data->intr_anchor);
2693 init_usb_anchor(&data->bulk_anchor);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02002694 init_usb_anchor(&data->isoc_anchor);
Marcel Holtmann803b5832014-09-16 08:00:29 +02002695 spin_lock_init(&data->rxlock);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002696
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002697 if (id->driver_info & BTUSB_INTEL_NEW) {
2698 data->recv_event = btusb_recv_event_intel;
2699 data->recv_bulk = btusb_recv_bulk_intel;
2700 set_bit(BTUSB_BOOTLOADER, &data->flags);
2701 } else {
2702 data->recv_event = hci_recv_frame;
2703 data->recv_bulk = btusb_recv_bulk;
2704 }
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +01002705
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002706 hdev = hci_alloc_dev();
Sachin Kamat98921db2012-07-27 12:38:39 +05302707 if (!hdev)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002708 return -ENOMEM;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002709
Marcel Holtmannc13854c2010-02-08 15:27:07 +01002710 hdev->bus = HCI_USB;
David Herrmann155961e2012-02-09 21:58:32 +01002711 hci_set_drvdata(hdev, data);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002712
Marcel Holtmann893ba542015-01-28 20:27:34 -08002713 if (id->driver_info & BTUSB_AMP)
2714 hdev->dev_type = HCI_AMP;
2715 else
2716 hdev->dev_type = HCI_BREDR;
2717
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002718 data->hdev = hdev;
2719
2720 SET_HCIDEV_DEV(hdev, &intf->dev);
2721
Marcel Holtmann9f8f9622013-04-10 08:11:35 -07002722 hdev->open = btusb_open;
2723 hdev->close = btusb_close;
2724 hdev->flush = btusb_flush;
2725 hdev->send = btusb_send_frame;
2726 hdev->notify = btusb_notify;
2727
2728 if (id->driver_info & BTUSB_BCM92035)
2729 hdev->setup = btusb_setup_bcm92035;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002730
Marcel Holtmannc2bfb102015-04-05 22:52:14 -07002731#ifdef CONFIG_BT_HCIBTUSB_BCM
Marcel Holtmannabbaf502014-07-02 00:53:48 +02002732 if (id->driver_info & BTUSB_BCM_PATCHRAM) {
Marcel Holtmannc2bfb102015-04-05 22:52:14 -07002733 hdev->setup = btbcm_setup_patchram;
Marcel Holtmann1df1f592015-04-05 22:52:11 -07002734 hdev->set_bdaddr = btbcm_set_bdaddr;
Marcel Holtmannabbaf502014-07-02 00:53:48 +02002735 }
Petri Gynther10d4c672014-05-08 15:50:01 -07002736
Marcel Holtmann941521e2015-04-05 22:52:15 -07002737 if (id->driver_info & BTUSB_BCM_APPLE)
Marcel Holtmannc2bfb102015-04-05 22:52:14 -07002738 hdev->setup = btbcm_setup_apple;
Marcel Holtmannc2bfb102015-04-05 22:52:14 -07002739#endif
Marcel Holtmann17b27722015-03-22 15:52:38 +01002740
Marcel Holtmanncb8d6592014-07-02 11:25:25 +02002741 if (id->driver_info & BTUSB_INTEL) {
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002742 hdev->setup = btusb_setup_intel;
Tedd Ho-Jeong Anbfbd45e2015-02-13 09:20:52 -08002743 hdev->shutdown = btusb_shutdown_intel;
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07002744 hdev->set_bdaddr = btintel_set_bdaddr;
Jakub Pawlowskic33fb9b2015-01-30 18:55:58 -08002745 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
Jakub Pawlowskic1154842015-03-17 09:04:16 -07002746 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
Marcel Holtmanncb8d6592014-07-02 11:25:25 +02002747 }
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002748
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002749 if (id->driver_info & BTUSB_INTEL_NEW) {
2750 hdev->send = btusb_send_frame_intel;
2751 hdev->setup = btusb_setup_intel_new;
Marcel Holtmann385a7682015-01-28 11:09:56 -08002752 hdev->hw_error = btusb_hw_error_intel;
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07002753 hdev->set_bdaddr = btintel_set_bdaddr;
Marcel Holtmannb970c5b2015-02-01 23:57:18 -08002754 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002755 }
2756
Amitkumar Karwarae8df492014-07-18 14:47:06 -07002757 if (id->driver_info & BTUSB_MARVELL)
2758 hdev->set_bdaddr = btusb_set_bdaddr_marvell;
2759
Marcel Holtmann661cf882015-01-02 23:35:20 -08002760 if (id->driver_info & BTUSB_SWAVE) {
2761 set_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks);
Marcel Holtmannd57dbe72014-12-26 04:42:33 +01002762 set_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS, &hdev->quirks);
Marcel Holtmann661cf882015-01-02 23:35:20 -08002763 }
Marcel Holtmannd57dbe72014-12-26 04:42:33 +01002764
Marcel Holtmann40df7832014-07-06 13:29:58 +02002765 if (id->driver_info & BTUSB_INTEL_BOOT)
2766 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
2767
Jakub Pawlowski79f0c872015-01-29 10:38:34 -08002768 if (id->driver_info & BTUSB_ATH3012) {
Toshi Kikuchi58592232014-12-12 10:58:05 -08002769 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
Jakub Pawlowski3d50d512015-03-17 09:04:15 -07002770 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
Jakub Pawlowski79f0c872015-01-29 10:38:34 -08002771 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
2772 }
Toshi Kikuchi58592232014-12-12 10:58:05 -08002773
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002774 if (id->driver_info & BTUSB_QCA_ROME) {
2775 data->setup_on_usb = btusb_setup_qca;
2776 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
2777 }
2778
Marcel Holtmann893ba542015-01-28 20:27:34 -08002779 if (id->driver_info & BTUSB_AMP) {
2780 /* AMP controllers do not support SCO packets */
2781 data->isoc = NULL;
2782 } else {
2783 /* Interface numbers are hardcoded in the specification */
2784 data->isoc = usb_ifnum_to_if(data->udev, 1);
2785 }
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02002786
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002787 if (!reset)
Szymon Janca6c511c2012-05-23 12:35:46 +02002788 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002789
2790 if (force_scofix || id->driver_info & BTUSB_WRONG_SCO_MTU) {
2791 if (!disable_scofix)
2792 set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
2793 }
2794
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02002795 if (id->driver_info & BTUSB_BROKEN_ISOC)
2796 data->isoc = NULL;
2797
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002798 if (id->driver_info & BTUSB_DIGIANSWER) {
2799 data->cmdreq_type = USB_TYPE_VENDOR;
Szymon Janca6c511c2012-05-23 12:35:46 +02002800 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002801 }
2802
2803 if (id->driver_info & BTUSB_CSR) {
2804 struct usb_device *udev = data->udev;
Marcel Holtmann81cac642014-01-03 03:02:36 -08002805 u16 bcdDevice = le16_to_cpu(udev->descriptor.bcdDevice);
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002806
2807 /* Old firmware would otherwise execute USB reset */
Marcel Holtmann81cac642014-01-03 03:02:36 -08002808 if (bcdDevice < 0x117)
Szymon Janca6c511c2012-05-23 12:35:46 +02002809 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmann81cac642014-01-03 03:02:36 -08002810
2811 /* Fake CSR devices with broken commands */
2812 if (bcdDevice <= 0x100)
2813 hdev->setup = btusb_setup_csr;
Jakub Pawlowski49c989a2015-03-17 09:04:17 -07002814
2815 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002816 }
2817
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002818 if (id->driver_info & BTUSB_SNIFFER) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02002819 struct usb_device *udev = data->udev;
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002820
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002821 /* New sniffer firmware has crippled HCI interface */
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002822 if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
2823 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
2824 }
2825
Marcel Holtmann3a5ef202014-07-06 14:53:54 +02002826 if (id->driver_info & BTUSB_INTEL_BOOT) {
2827 /* A bug in the bootloader causes that interrupt interface is
2828 * only enabled after receiving SetInterface(0, AltSetting=0).
2829 */
2830 err = usb_set_interface(data->udev, 0, 0);
2831 if (err < 0) {
2832 BT_ERR("failed to set interface 0, alt 0 %d", err);
2833 hci_free_dev(hdev);
2834 return err;
2835 }
2836 }
2837
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02002838 if (data->isoc) {
2839 err = usb_driver_claim_interface(&btusb_driver,
Marcel Holtmann89e75332014-09-16 04:44:50 +02002840 data->isoc, data);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02002841 if (err < 0) {
2842 hci_free_dev(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02002843 return err;
2844 }
2845 }
2846
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002847 err = hci_register_dev(hdev);
2848 if (err < 0) {
2849 hci_free_dev(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002850 return err;
2851 }
2852
2853 usb_set_intfdata(intf, data);
2854
2855 return 0;
2856}
2857
2858static void btusb_disconnect(struct usb_interface *intf)
2859{
2860 struct btusb_data *data = usb_get_intfdata(intf);
2861 struct hci_dev *hdev;
2862
2863 BT_DBG("intf %p", intf);
2864
2865 if (!data)
2866 return;
2867
2868 hdev = data->hdev;
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02002869 usb_set_intfdata(data->intf, NULL);
2870
2871 if (data->isoc)
2872 usb_set_intfdata(data->isoc, NULL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002873
2874 hci_unregister_dev(hdev);
2875
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02002876 if (intf == data->isoc)
2877 usb_driver_release_interface(&btusb_driver, data->intf);
2878 else if (data->isoc)
2879 usb_driver_release_interface(&btusb_driver, data->isoc);
2880
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002881 hci_free_dev(hdev);
2882}
2883
Oliver Neukum7bee5492009-08-24 23:44:59 +02002884#ifdef CONFIG_PM
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002885static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
2886{
2887 struct btusb_data *data = usb_get_intfdata(intf);
2888
2889 BT_DBG("intf %p", intf);
2890
2891 if (data->suspend_count++)
2892 return 0;
2893
Oliver Neukum7bee5492009-08-24 23:44:59 +02002894 spin_lock_irq(&data->txlock);
Alan Stern5b1b0b82011-08-19 23:49:48 +02002895 if (!(PMSG_IS_AUTO(message) && data->tx_in_flight)) {
Oliver Neukum7bee5492009-08-24 23:44:59 +02002896 set_bit(BTUSB_SUSPENDING, &data->flags);
2897 spin_unlock_irq(&data->txlock);
2898 } else {
2899 spin_unlock_irq(&data->txlock);
2900 data->suspend_count--;
2901 return -EBUSY;
2902 }
2903
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002904 cancel_work_sync(&data->work);
2905
Oliver Neukum7bee5492009-08-24 23:44:59 +02002906 btusb_stop_traffic(data);
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002907 usb_kill_anchored_urbs(&data->tx_anchor);
2908
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002909 return 0;
2910}
2911
Oliver Neukum7bee5492009-08-24 23:44:59 +02002912static void play_deferred(struct btusb_data *data)
2913{
2914 struct urb *urb;
2915 int err;
2916
2917 while ((urb = usb_get_from_anchor(&data->deferred))) {
2918 err = usb_submit_urb(urb, GFP_ATOMIC);
2919 if (err < 0)
2920 break;
2921
2922 data->tx_in_flight++;
2923 }
2924 usb_scuttle_anchored_urbs(&data->deferred);
2925}
2926
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002927static int btusb_resume(struct usb_interface *intf)
2928{
2929 struct btusb_data *data = usb_get_intfdata(intf);
2930 struct hci_dev *hdev = data->hdev;
Oliver Neukum7bee5492009-08-24 23:44:59 +02002931 int err = 0;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002932
2933 BT_DBG("intf %p", intf);
2934
2935 if (--data->suspend_count)
2936 return 0;
2937
2938 if (!test_bit(HCI_RUNNING, &hdev->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +02002939 goto done;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002940
2941 if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
2942 err = btusb_submit_intr_urb(hdev, GFP_NOIO);
2943 if (err < 0) {
2944 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02002945 goto failed;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002946 }
2947 }
2948
2949 if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) {
Marcel Holtmann43c2e572009-02-04 17:41:38 +01002950 err = btusb_submit_bulk_urb(hdev, GFP_NOIO);
2951 if (err < 0) {
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002952 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02002953 goto failed;
2954 }
2955
2956 btusb_submit_bulk_urb(hdev, GFP_NOIO);
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002957 }
2958
2959 if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
2960 if (btusb_submit_isoc_urb(hdev, GFP_NOIO) < 0)
2961 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
2962 else
2963 btusb_submit_isoc_urb(hdev, GFP_NOIO);
2964 }
2965
Oliver Neukum7bee5492009-08-24 23:44:59 +02002966 spin_lock_irq(&data->txlock);
2967 play_deferred(data);
2968 clear_bit(BTUSB_SUSPENDING, &data->flags);
2969 spin_unlock_irq(&data->txlock);
2970 schedule_work(&data->work);
2971
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002972 return 0;
Oliver Neukum7bee5492009-08-24 23:44:59 +02002973
2974failed:
2975 usb_scuttle_anchored_urbs(&data->deferred);
2976done:
2977 spin_lock_irq(&data->txlock);
2978 clear_bit(BTUSB_SUSPENDING, &data->flags);
2979 spin_unlock_irq(&data->txlock);
2980
2981 return err;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002982}
Oliver Neukum7bee5492009-08-24 23:44:59 +02002983#endif
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002984
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002985static struct usb_driver btusb_driver = {
2986 .name = "btusb",
2987 .probe = btusb_probe,
2988 .disconnect = btusb_disconnect,
Oliver Neukum7bee5492009-08-24 23:44:59 +02002989#ifdef CONFIG_PM
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002990 .suspend = btusb_suspend,
2991 .resume = btusb_resume,
Oliver Neukum7bee5492009-08-24 23:44:59 +02002992#endif
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002993 .id_table = btusb_table,
Oliver Neukum7bee5492009-08-24 23:44:59 +02002994 .supports_autosuspend = 1,
Sarah Sharpe1f12eb2012-04-23 10:08:51 -07002995 .disable_hub_initiated_lpm = 1,
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002996};
2997
Greg Kroah-Hartman93f15082011-11-18 09:47:34 -08002998module_usb_driver(btusb_driver);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002999
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02003000module_param(disable_scofix, bool, 0644);
3001MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
3002
3003module_param(force_scofix, bool, 0644);
3004MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
3005
3006module_param(reset, bool, 0644);
3007MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
3008
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003009MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
3010MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
3011MODULE_VERSION(VERSION);
3012MODULE_LICENSE("GPL");