blob: 5798aec44824144817a8956e10c4b0aed3b7ab9d [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 -07001348struct intel_version {
1349 u8 status;
1350 u8 hw_platform;
1351 u8 hw_variant;
1352 u8 hw_revision;
1353 u8 fw_variant;
1354 u8 fw_revision;
1355 u8 fw_build_num;
1356 u8 fw_build_ww;
1357 u8 fw_build_yy;
1358 u8 fw_patch_num;
1359} __packed;
1360
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001361struct intel_boot_params {
1362 __u8 status;
1363 __u8 otp_format;
1364 __u8 otp_content;
1365 __u8 otp_patch;
1366 __le16 dev_revid;
1367 __u8 secure_boot;
1368 __u8 key_from_hdr;
1369 __u8 key_type;
1370 __u8 otp_lock;
1371 __u8 api_lock;
1372 __u8 debug_lock;
1373 bdaddr_t otp_bdaddr;
1374 __u8 min_fw_build_nn;
1375 __u8 min_fw_build_cw;
1376 __u8 min_fw_build_yy;
1377 __u8 limited_cce;
1378 __u8 unlocked_state;
1379} __packed;
1380
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001381static const struct firmware *btusb_setup_intel_get_fw(struct hci_dev *hdev,
Marcel Holtmann89e75332014-09-16 04:44:50 +02001382 struct intel_version *ver)
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001383{
1384 const struct firmware *fw;
1385 char fwname[64];
1386 int ret;
1387
1388 snprintf(fwname, sizeof(fwname),
1389 "intel/ibt-hw-%x.%x.%x-fw-%x.%x.%x.%x.%x.bseq",
1390 ver->hw_platform, ver->hw_variant, ver->hw_revision,
1391 ver->fw_variant, ver->fw_revision, ver->fw_build_num,
1392 ver->fw_build_ww, ver->fw_build_yy);
1393
1394 ret = request_firmware(&fw, fwname, &hdev->dev);
1395 if (ret < 0) {
1396 if (ret == -EINVAL) {
1397 BT_ERR("%s Intel firmware file request failed (%d)",
1398 hdev->name, ret);
1399 return NULL;
1400 }
1401
1402 BT_ERR("%s failed to open Intel firmware file: %s(%d)",
1403 hdev->name, fwname, ret);
1404
1405 /* If the correct firmware patch file is not found, use the
1406 * default firmware patch file instead
1407 */
1408 snprintf(fwname, sizeof(fwname), "intel/ibt-hw-%x.%x.bseq",
1409 ver->hw_platform, ver->hw_variant);
1410 if (request_firmware(&fw, fwname, &hdev->dev) < 0) {
1411 BT_ERR("%s failed to open default Intel fw file: %s",
1412 hdev->name, fwname);
1413 return NULL;
1414 }
1415 }
1416
1417 BT_INFO("%s: Intel Bluetooth firmware file: %s", hdev->name, fwname);
1418
1419 return fw;
1420}
1421
1422static int btusb_setup_intel_patching(struct hci_dev *hdev,
1423 const struct firmware *fw,
1424 const u8 **fw_ptr, int *disable_patch)
1425{
1426 struct sk_buff *skb;
1427 struct hci_command_hdr *cmd;
1428 const u8 *cmd_param;
1429 struct hci_event_hdr *evt = NULL;
1430 const u8 *evt_param = NULL;
1431 int remain = fw->size - (*fw_ptr - fw->data);
1432
1433 /* The first byte indicates the types of the patch command or event.
1434 * 0x01 means HCI command and 0x02 is HCI event. If the first bytes
1435 * in the current firmware buffer doesn't start with 0x01 or
1436 * the size of remain buffer is smaller than HCI command header,
1437 * the firmware file is corrupted and it should stop the patching
1438 * process.
1439 */
1440 if (remain > HCI_COMMAND_HDR_SIZE && *fw_ptr[0] != 0x01) {
1441 BT_ERR("%s Intel fw corrupted: invalid cmd read", hdev->name);
1442 return -EINVAL;
1443 }
1444 (*fw_ptr)++;
1445 remain--;
1446
1447 cmd = (struct hci_command_hdr *)(*fw_ptr);
1448 *fw_ptr += sizeof(*cmd);
1449 remain -= sizeof(*cmd);
1450
1451 /* Ensure that the remain firmware data is long enough than the length
1452 * of command parameter. If not, the firmware file is corrupted.
1453 */
1454 if (remain < cmd->plen) {
1455 BT_ERR("%s Intel fw corrupted: invalid cmd len", hdev->name);
1456 return -EFAULT;
1457 }
1458
1459 /* If there is a command that loads a patch in the firmware
1460 * file, then enable the patch upon success, otherwise just
1461 * disable the manufacturer mode, for example patch activation
1462 * is not required when the default firmware patch file is used
1463 * because there are no patch data to load.
1464 */
1465 if (*disable_patch && le16_to_cpu(cmd->opcode) == 0xfc8e)
1466 *disable_patch = 0;
1467
1468 cmd_param = *fw_ptr;
1469 *fw_ptr += cmd->plen;
1470 remain -= cmd->plen;
1471
1472 /* This reads the expected events when the above command is sent to the
1473 * device. Some vendor commands expects more than one events, for
1474 * example command status event followed by vendor specific event.
1475 * For this case, it only keeps the last expected event. so the command
1476 * can be sent with __hci_cmd_sync_ev() which returns the sk_buff of
1477 * last expected event.
1478 */
1479 while (remain > HCI_EVENT_HDR_SIZE && *fw_ptr[0] == 0x02) {
1480 (*fw_ptr)++;
1481 remain--;
1482
1483 evt = (struct hci_event_hdr *)(*fw_ptr);
1484 *fw_ptr += sizeof(*evt);
1485 remain -= sizeof(*evt);
1486
1487 if (remain < evt->plen) {
1488 BT_ERR("%s Intel fw corrupted: invalid evt len",
1489 hdev->name);
1490 return -EFAULT;
1491 }
1492
1493 evt_param = *fw_ptr;
1494 *fw_ptr += evt->plen;
1495 remain -= evt->plen;
1496 }
1497
1498 /* Every HCI commands in the firmware file has its correspond event.
1499 * If event is not found or remain is smaller than zero, the firmware
1500 * file is corrupted.
1501 */
1502 if (!evt || !evt_param || remain < 0) {
1503 BT_ERR("%s Intel fw corrupted: invalid evt read", hdev->name);
1504 return -EFAULT;
1505 }
1506
1507 skb = __hci_cmd_sync_ev(hdev, le16_to_cpu(cmd->opcode), cmd->plen,
1508 cmd_param, evt->evt, HCI_INIT_TIMEOUT);
1509 if (IS_ERR(skb)) {
1510 BT_ERR("%s sending Intel patch command (0x%4.4x) failed (%ld)",
1511 hdev->name, cmd->opcode, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001512 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001513 }
1514
1515 /* It ensures that the returned event matches the event data read from
1516 * the firmware file. At fist, it checks the length and then
1517 * the contents of the event.
1518 */
1519 if (skb->len != evt->plen) {
1520 BT_ERR("%s mismatch event length (opcode 0x%4.4x)", hdev->name,
1521 le16_to_cpu(cmd->opcode));
1522 kfree_skb(skb);
1523 return -EFAULT;
1524 }
1525
1526 if (memcmp(skb->data, evt_param, evt->plen)) {
1527 BT_ERR("%s mismatch event parameter (opcode 0x%4.4x)",
1528 hdev->name, le16_to_cpu(cmd->opcode));
1529 kfree_skb(skb);
1530 return -EFAULT;
1531 }
1532 kfree_skb(skb);
1533
1534 return 0;
1535}
1536
1537static int btusb_setup_intel(struct hci_dev *hdev)
1538{
1539 struct sk_buff *skb;
1540 const struct firmware *fw;
1541 const u8 *fw_ptr;
1542 int disable_patch;
1543 struct intel_version *ver;
1544
1545 const u8 mfg_enable[] = { 0x01, 0x00 };
1546 const u8 mfg_disable[] = { 0x00, 0x00 };
1547 const u8 mfg_reset_deactivate[] = { 0x00, 0x01 };
1548 const u8 mfg_reset_activate[] = { 0x00, 0x02 };
1549
1550 BT_DBG("%s", hdev->name);
1551
1552 /* The controller has a bug with the first HCI command sent to it
1553 * returning number of completed commands as zero. This would stall the
1554 * command processing in the Bluetooth core.
1555 *
1556 * As a workaround, send HCI Reset command first which will reset the
1557 * number of completed commands and allow normal command processing
1558 * from now on.
1559 */
1560 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
1561 if (IS_ERR(skb)) {
1562 BT_ERR("%s sending initial HCI reset command failed (%ld)",
1563 hdev->name, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001564 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001565 }
1566 kfree_skb(skb);
1567
1568 /* Read Intel specific controller version first to allow selection of
1569 * which firmware file to load.
1570 *
1571 * The returned information are hardware variant and revision plus
1572 * firmware variant, revision and build number.
1573 */
1574 skb = __hci_cmd_sync(hdev, 0xfc05, 0, NULL, HCI_INIT_TIMEOUT);
1575 if (IS_ERR(skb)) {
1576 BT_ERR("%s reading Intel fw version command failed (%ld)",
1577 hdev->name, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001578 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001579 }
1580
1581 if (skb->len != sizeof(*ver)) {
1582 BT_ERR("%s Intel version event length mismatch", hdev->name);
1583 kfree_skb(skb);
1584 return -EIO;
1585 }
1586
1587 ver = (struct intel_version *)skb->data;
1588 if (ver->status) {
1589 BT_ERR("%s Intel fw version event failed (%02x)", hdev->name,
1590 ver->status);
1591 kfree_skb(skb);
1592 return -bt_to_errno(ver->status);
1593 }
1594
1595 BT_INFO("%s: read Intel version: %02x%02x%02x%02x%02x%02x%02x%02x%02x",
1596 hdev->name, ver->hw_platform, ver->hw_variant,
1597 ver->hw_revision, ver->fw_variant, ver->fw_revision,
1598 ver->fw_build_num, ver->fw_build_ww, ver->fw_build_yy,
1599 ver->fw_patch_num);
1600
1601 /* fw_patch_num indicates the version of patch the device currently
1602 * have. If there is no patch data in the device, it is always 0x00.
1603 * So, if it is other than 0x00, no need to patch the deivce again.
1604 */
1605 if (ver->fw_patch_num) {
1606 BT_INFO("%s: Intel device is already patched. patch num: %02x",
1607 hdev->name, ver->fw_patch_num);
1608 kfree_skb(skb);
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07001609 btintel_check_bdaddr(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001610 return 0;
1611 }
1612
1613 /* Opens the firmware patch file based on the firmware version read
1614 * from the controller. If it fails to open the matching firmware
1615 * patch file, it tries to open the default firmware patch file.
1616 * If no patch file is found, allow the device to operate without
1617 * a patch.
1618 */
1619 fw = btusb_setup_intel_get_fw(hdev, ver);
1620 if (!fw) {
1621 kfree_skb(skb);
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07001622 btintel_check_bdaddr(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001623 return 0;
1624 }
1625 fw_ptr = fw->data;
1626
1627 /* This Intel specific command enables the manufacturer mode of the
1628 * controller.
1629 *
1630 * Only while this mode is enabled, the driver can download the
1631 * firmware patch data and configuration parameters.
1632 */
1633 skb = __hci_cmd_sync(hdev, 0xfc11, 2, mfg_enable, HCI_INIT_TIMEOUT);
1634 if (IS_ERR(skb)) {
1635 BT_ERR("%s entering Intel manufacturer mode failed (%ld)",
1636 hdev->name, PTR_ERR(skb));
1637 release_firmware(fw);
Adam Leed9c78e92013-07-10 10:02:12 +08001638 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001639 }
1640
1641 if (skb->data[0]) {
1642 u8 evt_status = skb->data[0];
Marcel Holtmann89e75332014-09-16 04:44:50 +02001643
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001644 BT_ERR("%s enable Intel manufacturer mode event failed (%02x)",
1645 hdev->name, evt_status);
1646 kfree_skb(skb);
1647 release_firmware(fw);
1648 return -bt_to_errno(evt_status);
1649 }
1650 kfree_skb(skb);
1651
1652 disable_patch = 1;
1653
1654 /* The firmware data file consists of list of Intel specific HCI
1655 * commands and its expected events. The first byte indicates the
1656 * type of the message, either HCI command or HCI event.
1657 *
1658 * It reads the command and its expected event from the firmware file,
1659 * and send to the controller. Once __hci_cmd_sync_ev() returns,
1660 * the returned event is compared with the event read from the firmware
1661 * file and it will continue until all the messages are downloaded to
1662 * the controller.
1663 *
1664 * Once the firmware patching is completed successfully,
1665 * the manufacturer mode is disabled with reset and activating the
1666 * downloaded patch.
1667 *
1668 * If the firmware patching fails, the manufacturer mode is
1669 * disabled with reset and deactivating the patch.
1670 *
1671 * If the default patch file is used, no reset is done when disabling
1672 * the manufacturer.
1673 */
1674 while (fw->size > fw_ptr - fw->data) {
1675 int ret;
1676
1677 ret = btusb_setup_intel_patching(hdev, fw, &fw_ptr,
1678 &disable_patch);
1679 if (ret < 0)
1680 goto exit_mfg_deactivate;
1681 }
1682
1683 release_firmware(fw);
1684
1685 if (disable_patch)
1686 goto exit_mfg_disable;
1687
1688 /* Patching completed successfully and disable the manufacturer mode
1689 * with reset and activate the downloaded firmware patches.
1690 */
1691 skb = __hci_cmd_sync(hdev, 0xfc11, sizeof(mfg_reset_activate),
1692 mfg_reset_activate, HCI_INIT_TIMEOUT);
1693 if (IS_ERR(skb)) {
1694 BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
1695 hdev->name, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001696 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001697 }
1698 kfree_skb(skb);
1699
1700 BT_INFO("%s: Intel Bluetooth firmware patch completed and activated",
1701 hdev->name);
1702
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07001703 btintel_check_bdaddr(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001704 return 0;
1705
1706exit_mfg_disable:
1707 /* Disable the manufacturer mode without reset */
1708 skb = __hci_cmd_sync(hdev, 0xfc11, sizeof(mfg_disable), mfg_disable,
1709 HCI_INIT_TIMEOUT);
1710 if (IS_ERR(skb)) {
1711 BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
1712 hdev->name, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001713 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001714 }
1715 kfree_skb(skb);
1716
1717 BT_INFO("%s: Intel Bluetooth firmware patch completed", hdev->name);
Marcel Holtmann40cb0982014-07-02 12:06:45 +02001718
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07001719 btintel_check_bdaddr(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001720 return 0;
1721
1722exit_mfg_deactivate:
1723 release_firmware(fw);
1724
1725 /* Patching failed. Disable the manufacturer mode with reset and
1726 * deactivate the downloaded firmware patches.
1727 */
1728 skb = __hci_cmd_sync(hdev, 0xfc11, sizeof(mfg_reset_deactivate),
1729 mfg_reset_deactivate, HCI_INIT_TIMEOUT);
1730 if (IS_ERR(skb)) {
1731 BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
1732 hdev->name, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001733 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001734 }
1735 kfree_skb(skb);
1736
1737 BT_INFO("%s: Intel Bluetooth firmware patch completed and deactivated",
1738 hdev->name);
1739
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07001740 btintel_check_bdaddr(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001741 return 0;
1742}
1743
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001744static int inject_cmd_complete(struct hci_dev *hdev, __u16 opcode)
1745{
1746 struct sk_buff *skb;
1747 struct hci_event_hdr *hdr;
1748 struct hci_ev_cmd_complete *evt;
1749
1750 skb = bt_skb_alloc(sizeof(*hdr) + sizeof(*evt) + 1, GFP_ATOMIC);
1751 if (!skb)
1752 return -ENOMEM;
1753
1754 hdr = (struct hci_event_hdr *)skb_put(skb, sizeof(*hdr));
1755 hdr->evt = HCI_EV_CMD_COMPLETE;
1756 hdr->plen = sizeof(*evt) + 1;
1757
1758 evt = (struct hci_ev_cmd_complete *)skb_put(skb, sizeof(*evt));
1759 evt->ncmd = 0x01;
1760 evt->opcode = cpu_to_le16(opcode);
1761
1762 *skb_put(skb, 1) = 0x00;
1763
1764 bt_cb(skb)->pkt_type = HCI_EVENT_PKT;
1765
1766 return hci_recv_frame(hdev, skb);
1767}
1768
1769static int btusb_recv_bulk_intel(struct btusb_data *data, void *buffer,
1770 int count)
1771{
1772 /* When the device is in bootloader mode, then it can send
1773 * events via the bulk endpoint. These events are treated the
1774 * same way as the ones received from the interrupt endpoint.
1775 */
1776 if (test_bit(BTUSB_BOOTLOADER, &data->flags))
1777 return btusb_recv_intr(data, buffer, count);
1778
1779 return btusb_recv_bulk(data, buffer, count);
1780}
1781
1782static int btusb_recv_event_intel(struct hci_dev *hdev, struct sk_buff *skb)
1783{
1784 struct btusb_data *data = hci_get_drvdata(hdev);
1785
1786 if (test_bit(BTUSB_BOOTLOADER, &data->flags)) {
1787 struct hci_event_hdr *hdr = (void *)skb->data;
1788
1789 /* When the firmware loading completes the device sends
1790 * out a vendor specific event indicating the result of
1791 * the firmware loading.
1792 */
1793 if (skb->len == 7 && hdr->evt == 0xff && hdr->plen == 0x05 &&
1794 skb->data[2] == 0x06) {
1795 if (skb->data[3] != 0x00)
1796 test_bit(BTUSB_FIRMWARE_FAILED, &data->flags);
1797
Marcel Holtmannce6bb922015-01-28 01:58:40 -08001798 if (test_and_clear_bit(BTUSB_DOWNLOADING,
1799 &data->flags) &&
Johan Hedberga087a982015-01-30 10:58:54 +02001800 test_bit(BTUSB_FIRMWARE_LOADED, &data->flags)) {
1801 smp_mb__after_atomic();
1802 wake_up_bit(&data->flags, BTUSB_DOWNLOADING);
1803 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001804 }
1805
1806 /* When switching to the operational firmware the device
1807 * sends a vendor specific event indicating that the bootup
1808 * completed.
1809 */
1810 if (skb->len == 9 && hdr->evt == 0xff && hdr->plen == 0x07 &&
1811 skb->data[2] == 0x02) {
Johan Hedbergfad70972015-01-30 10:58:55 +02001812 if (test_and_clear_bit(BTUSB_BOOTING, &data->flags)) {
1813 smp_mb__after_atomic();
1814 wake_up_bit(&data->flags, BTUSB_BOOTING);
1815 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001816 }
1817 }
1818
1819 return hci_recv_frame(hdev, skb);
1820}
1821
1822static int btusb_send_frame_intel(struct hci_dev *hdev, struct sk_buff *skb)
1823{
1824 struct btusb_data *data = hci_get_drvdata(hdev);
1825 struct urb *urb;
1826
1827 BT_DBG("%s", hdev->name);
1828
1829 if (!test_bit(HCI_RUNNING, &hdev->flags))
1830 return -EBUSY;
1831
1832 switch (bt_cb(skb)->pkt_type) {
1833 case HCI_COMMAND_PKT:
1834 if (test_bit(BTUSB_BOOTLOADER, &data->flags)) {
1835 struct hci_command_hdr *cmd = (void *)skb->data;
1836 __u16 opcode = le16_to_cpu(cmd->opcode);
1837
1838 /* When in bootloader mode and the command 0xfc09
1839 * is received, it needs to be send down the
1840 * bulk endpoint. So allocate a bulk URB instead.
1841 */
1842 if (opcode == 0xfc09)
1843 urb = alloc_bulk_urb(hdev, skb);
1844 else
1845 urb = alloc_ctrl_urb(hdev, skb);
1846
1847 /* When the 0xfc01 command is issued to boot into
1848 * the operational firmware, it will actually not
1849 * send a command complete event. To keep the flow
1850 * control working inject that event here.
1851 */
1852 if (opcode == 0xfc01)
1853 inject_cmd_complete(hdev, opcode);
1854 } else {
1855 urb = alloc_ctrl_urb(hdev, skb);
1856 }
1857 if (IS_ERR(urb))
1858 return PTR_ERR(urb);
1859
1860 hdev->stat.cmd_tx++;
1861 return submit_or_queue_tx_urb(hdev, urb);
1862
1863 case HCI_ACLDATA_PKT:
1864 urb = alloc_bulk_urb(hdev, skb);
1865 if (IS_ERR(urb))
1866 return PTR_ERR(urb);
1867
1868 hdev->stat.acl_tx++;
1869 return submit_or_queue_tx_urb(hdev, urb);
1870
1871 case HCI_SCODATA_PKT:
1872 if (hci_conn_num(hdev, SCO_LINK) < 1)
1873 return -ENODEV;
1874
1875 urb = alloc_isoc_urb(hdev, skb);
1876 if (IS_ERR(urb))
1877 return PTR_ERR(urb);
1878
1879 hdev->stat.sco_tx++;
1880 return submit_tx_urb(hdev, urb);
1881 }
1882
1883 return -EILSEQ;
1884}
1885
1886static int btusb_intel_secure_send(struct hci_dev *hdev, u8 fragment_type,
1887 u32 plen, const void *param)
1888{
1889 while (plen > 0) {
1890 struct sk_buff *skb;
1891 u8 cmd_param[253], fragment_len = (plen > 252) ? 252 : plen;
1892
1893 cmd_param[0] = fragment_type;
1894 memcpy(cmd_param + 1, param, fragment_len);
1895
1896 skb = __hci_cmd_sync(hdev, 0xfc09, fragment_len + 1,
1897 cmd_param, HCI_INIT_TIMEOUT);
1898 if (IS_ERR(skb))
1899 return PTR_ERR(skb);
1900
1901 kfree_skb(skb);
1902
1903 plen -= fragment_len;
1904 param += fragment_len;
1905 }
1906
1907 return 0;
1908}
1909
1910static void btusb_intel_version_info(struct hci_dev *hdev,
1911 struct intel_version *ver)
1912{
1913 const char *variant;
1914
1915 switch (ver->fw_variant) {
1916 case 0x06:
1917 variant = "Bootloader";
1918 break;
1919 case 0x23:
1920 variant = "Firmware";
1921 break;
1922 default:
1923 return;
1924 }
1925
1926 BT_INFO("%s: %s revision %u.%u build %u week %u %u", hdev->name,
1927 variant, ver->fw_revision >> 4, ver->fw_revision & 0x0f,
1928 ver->fw_build_num, ver->fw_build_ww, 2000 + ver->fw_build_yy);
1929}
1930
1931static int btusb_setup_intel_new(struct hci_dev *hdev)
1932{
1933 static const u8 reset_param[] = { 0x00, 0x01, 0x00, 0x01,
1934 0x00, 0x08, 0x04, 0x00 };
1935 struct btusb_data *data = hci_get_drvdata(hdev);
1936 struct sk_buff *skb;
1937 struct intel_version *ver;
1938 struct intel_boot_params *params;
1939 const struct firmware *fw;
1940 const u8 *fw_ptr;
1941 char fwname[64];
1942 ktime_t calltime, delta, rettime;
1943 unsigned long long duration;
1944 int err;
1945
1946 BT_DBG("%s", hdev->name);
1947
1948 calltime = ktime_get();
1949
1950 /* Read the Intel version information to determine if the device
1951 * is in bootloader mode or if it already has operational firmware
1952 * loaded.
1953 */
1954 skb = __hci_cmd_sync(hdev, 0xfc05, 0, NULL, HCI_INIT_TIMEOUT);
1955 if (IS_ERR(skb)) {
1956 BT_ERR("%s: Reading Intel version information failed (%ld)",
1957 hdev->name, PTR_ERR(skb));
1958 return PTR_ERR(skb);
1959 }
1960
1961 if (skb->len != sizeof(*ver)) {
1962 BT_ERR("%s: Intel version event size mismatch", hdev->name);
1963 kfree_skb(skb);
1964 return -EILSEQ;
1965 }
1966
1967 ver = (struct intel_version *)skb->data;
1968 if (ver->status) {
1969 BT_ERR("%s: Intel version command failure (%02x)",
1970 hdev->name, ver->status);
1971 err = -bt_to_errno(ver->status);
1972 kfree_skb(skb);
1973 return err;
1974 }
1975
1976 /* The hardware platform number has a fixed value of 0x37 and
1977 * for now only accept this single value.
1978 */
1979 if (ver->hw_platform != 0x37) {
1980 BT_ERR("%s: Unsupported Intel hardware platform (%u)",
1981 hdev->name, ver->hw_platform);
1982 kfree_skb(skb);
1983 return -EINVAL;
1984 }
1985
1986 /* At the moment only the hardware variant iBT 3.0 (LnP/SfP) is
1987 * supported by this firmware loading method. This check has been
1988 * put in place to ensure correct forward compatibility options
1989 * when newer hardware variants come along.
1990 */
1991 if (ver->hw_variant != 0x0b) {
1992 BT_ERR("%s: Unsupported Intel hardware variant (%u)",
1993 hdev->name, ver->hw_variant);
1994 kfree_skb(skb);
1995 return -EINVAL;
1996 }
1997
1998 btusb_intel_version_info(hdev, ver);
1999
2000 /* The firmware variant determines if the device is in bootloader
2001 * mode or is running operational firmware. The value 0x06 identifies
2002 * the bootloader and the value 0x23 identifies the operational
2003 * firmware.
2004 *
2005 * When the operational firmware is already present, then only
2006 * the check for valid Bluetooth device address is needed. This
2007 * determines if the device will be added as configured or
2008 * unconfigured controller.
2009 *
2010 * It is not possible to use the Secure Boot Parameters in this
2011 * case since that command is only available in bootloader mode.
2012 */
2013 if (ver->fw_variant == 0x23) {
2014 kfree_skb(skb);
2015 clear_bit(BTUSB_BOOTLOADER, &data->flags);
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07002016 btintel_check_bdaddr(hdev);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002017 return 0;
2018 }
2019
2020 /* If the device is not in bootloader mode, then the only possible
2021 * choice is to return an error and abort the device initialization.
2022 */
2023 if (ver->fw_variant != 0x06) {
2024 BT_ERR("%s: Unsupported Intel firmware variant (%u)",
2025 hdev->name, ver->fw_variant);
2026 kfree_skb(skb);
2027 return -ENODEV;
2028 }
2029
2030 kfree_skb(skb);
2031
2032 /* Read the secure boot parameters to identify the operating
2033 * details of the bootloader.
2034 */
2035 skb = __hci_cmd_sync(hdev, 0xfc0d, 0, NULL, HCI_INIT_TIMEOUT);
2036 if (IS_ERR(skb)) {
2037 BT_ERR("%s: Reading Intel boot parameters failed (%ld)",
2038 hdev->name, PTR_ERR(skb));
2039 return PTR_ERR(skb);
2040 }
2041
2042 if (skb->len != sizeof(*params)) {
2043 BT_ERR("%s: Intel boot parameters size mismatch", hdev->name);
2044 kfree_skb(skb);
2045 return -EILSEQ;
2046 }
2047
2048 params = (struct intel_boot_params *)skb->data;
2049 if (params->status) {
2050 BT_ERR("%s: Intel boot parameters command failure (%02x)",
2051 hdev->name, params->status);
2052 err = -bt_to_errno(params->status);
2053 kfree_skb(skb);
2054 return err;
2055 }
2056
2057 BT_INFO("%s: Device revision is %u", hdev->name,
2058 le16_to_cpu(params->dev_revid));
2059
2060 BT_INFO("%s: Secure boot is %s", hdev->name,
2061 params->secure_boot ? "enabled" : "disabled");
2062
2063 BT_INFO("%s: Minimum firmware build %u week %u %u", hdev->name,
2064 params->min_fw_build_nn, params->min_fw_build_cw,
2065 2000 + params->min_fw_build_yy);
2066
2067 /* It is required that every single firmware fragment is acknowledged
2068 * with a command complete event. If the boot parameters indicate
2069 * that this bootloader does not send them, then abort the setup.
2070 */
2071 if (params->limited_cce != 0x00) {
2072 BT_ERR("%s: Unsupported Intel firmware loading method (%u)",
2073 hdev->name, params->limited_cce);
2074 kfree_skb(skb);
2075 return -EINVAL;
2076 }
2077
2078 /* If the OTP has no valid Bluetooth device address, then there will
2079 * also be no valid address for the operational firmware.
2080 */
2081 if (!bacmp(&params->otp_bdaddr, BDADDR_ANY)) {
2082 BT_INFO("%s: No device address configured", hdev->name);
2083 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
2084 }
2085
2086 /* With this Intel bootloader only the hardware variant and device
2087 * revision information are used to select the right firmware.
2088 *
2089 * Currently this bootloader support is limited to hardware variant
2090 * iBT 3.0 (LnP/SfP) which is identified by the value 11 (0x0b).
2091 */
2092 snprintf(fwname, sizeof(fwname), "intel/ibt-11-%u.sfi",
2093 le16_to_cpu(params->dev_revid));
2094
2095 err = request_firmware(&fw, fwname, &hdev->dev);
2096 if (err < 0) {
2097 BT_ERR("%s: Failed to load Intel firmware file (%d)",
2098 hdev->name, err);
2099 kfree_skb(skb);
2100 return err;
2101 }
2102
2103 BT_INFO("%s: Found device firmware: %s", hdev->name, fwname);
2104
2105 kfree_skb(skb);
2106
2107 if (fw->size < 644) {
2108 BT_ERR("%s: Invalid size of firmware file (%zu)",
2109 hdev->name, fw->size);
2110 err = -EBADF;
2111 goto done;
2112 }
2113
2114 set_bit(BTUSB_DOWNLOADING, &data->flags);
2115
2116 /* Start the firmware download transaction with the Init fragment
2117 * represented by the 128 bytes of CSS header.
2118 */
2119 err = btusb_intel_secure_send(hdev, 0x00, 128, fw->data);
2120 if (err < 0) {
2121 BT_ERR("%s: Failed to send firmware header (%d)",
2122 hdev->name, err);
2123 goto done;
2124 }
2125
2126 /* Send the 256 bytes of public key information from the firmware
2127 * as the PKey fragment.
2128 */
2129 err = btusb_intel_secure_send(hdev, 0x03, 256, fw->data + 128);
2130 if (err < 0) {
2131 BT_ERR("%s: Failed to send firmware public key (%d)",
2132 hdev->name, err);
2133 goto done;
2134 }
2135
2136 /* Send the 256 bytes of signature information from the firmware
2137 * as the Sign fragment.
2138 */
2139 err = btusb_intel_secure_send(hdev, 0x02, 256, fw->data + 388);
2140 if (err < 0) {
2141 BT_ERR("%s: Failed to send firmware signature (%d)",
2142 hdev->name, err);
2143 goto done;
2144 }
2145
2146 fw_ptr = fw->data + 644;
2147
2148 while (fw_ptr - fw->data < fw->size) {
2149 struct hci_command_hdr *cmd = (void *)fw_ptr;
2150 u8 cmd_len;
2151
2152 cmd_len = sizeof(*cmd) + cmd->plen;
2153
2154 /* Send each command from the firmware data buffer as
2155 * a single Data fragment.
2156 */
2157 err = btusb_intel_secure_send(hdev, 0x01, cmd_len, fw_ptr);
2158 if (err < 0) {
2159 BT_ERR("%s: Failed to send firmware data (%d)",
2160 hdev->name, err);
2161 goto done;
2162 }
2163
2164 fw_ptr += cmd_len;
2165 }
2166
Marcel Holtmannce6bb922015-01-28 01:58:40 -08002167 set_bit(BTUSB_FIRMWARE_LOADED, &data->flags);
2168
Johan Hedberga087a982015-01-30 10:58:54 +02002169 BT_INFO("%s: Waiting for firmware download to complete", hdev->name);
2170
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002171 /* Before switching the device into operational mode and with that
2172 * booting the loaded firmware, wait for the bootloader notification
2173 * that all fragments have been successfully received.
2174 *
Johan Hedberga087a982015-01-30 10:58:54 +02002175 * When the event processing receives the notification, then the
2176 * BTUSB_DOWNLOADING flag will be cleared.
2177 *
2178 * The firmware loading should not take longer than 5 seconds
2179 * and thus just timeout if that happens and fail the setup
2180 * of this device.
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002181 */
Johan Hedberg129a7692015-02-14 09:33:35 +02002182 err = wait_on_bit_timeout(&data->flags, BTUSB_DOWNLOADING,
2183 TASK_INTERRUPTIBLE,
2184 msecs_to_jiffies(5000));
Johan Hedberga087a982015-01-30 10:58:54 +02002185 if (err == 1) {
2186 BT_ERR("%s: Firmware loading interrupted", hdev->name);
2187 err = -EINTR;
2188 goto done;
2189 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002190
Johan Hedberga087a982015-01-30 10:58:54 +02002191 if (err) {
2192 BT_ERR("%s: Firmware loading timeout", hdev->name);
2193 err = -ETIMEDOUT;
2194 goto done;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002195 }
2196
2197 if (test_bit(BTUSB_FIRMWARE_FAILED, &data->flags)) {
2198 BT_ERR("%s: Firmware loading failed", hdev->name);
2199 err = -ENOEXEC;
2200 goto done;
2201 }
2202
2203 rettime = ktime_get();
2204 delta = ktime_sub(rettime, calltime);
2205 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
2206
2207 BT_INFO("%s: Firmware loaded in %llu usecs", hdev->name, duration);
2208
2209done:
2210 release_firmware(fw);
2211
2212 if (err < 0)
2213 return err;
2214
2215 calltime = ktime_get();
2216
2217 set_bit(BTUSB_BOOTING, &data->flags);
2218
2219 skb = __hci_cmd_sync(hdev, 0xfc01, sizeof(reset_param), reset_param,
2220 HCI_INIT_TIMEOUT);
2221 if (IS_ERR(skb))
2222 return PTR_ERR(skb);
2223
2224 kfree_skb(skb);
2225
2226 /* The bootloader will not indicate when the device is ready. This
2227 * is done by the operational firmware sending bootup notification.
Johan Hedbergfad70972015-01-30 10:58:55 +02002228 *
2229 * Booting into operational firmware should not take longer than
2230 * 1 second. However if that happens, then just fail the setup
2231 * since something went wrong.
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002232 */
Johan Hedbergfad70972015-01-30 10:58:55 +02002233 BT_INFO("%s: Waiting for device to boot", hdev->name);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002234
Johan Hedberg129a7692015-02-14 09:33:35 +02002235 err = wait_on_bit_timeout(&data->flags, BTUSB_BOOTING,
2236 TASK_INTERRUPTIBLE,
2237 msecs_to_jiffies(1000));
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002238
Johan Hedbergfad70972015-01-30 10:58:55 +02002239 if (err == 1) {
2240 BT_ERR("%s: Device boot interrupted", hdev->name);
2241 return -EINTR;
2242 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002243
Johan Hedbergfad70972015-01-30 10:58:55 +02002244 if (err) {
2245 BT_ERR("%s: Device boot timeout", hdev->name);
2246 return -ETIMEDOUT;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002247 }
2248
2249 rettime = ktime_get();
2250 delta = ktime_sub(rettime, calltime);
2251 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
2252
2253 BT_INFO("%s: Device booted in %llu usecs", hdev->name, duration);
2254
2255 clear_bit(BTUSB_BOOTLOADER, &data->flags);
2256
2257 return 0;
2258}
2259
Marcel Holtmann385a7682015-01-28 11:09:56 -08002260static void btusb_hw_error_intel(struct hci_dev *hdev, u8 code)
2261{
2262 struct sk_buff *skb;
2263 u8 type = 0x00;
2264
2265 BT_ERR("%s: Hardware error 0x%2.2x", hdev->name, code);
2266
2267 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
2268 if (IS_ERR(skb)) {
2269 BT_ERR("%s: Reset after hardware error failed (%ld)",
2270 hdev->name, PTR_ERR(skb));
2271 return;
2272 }
2273 kfree_skb(skb);
2274
2275 skb = __hci_cmd_sync(hdev, 0xfc22, 1, &type, HCI_INIT_TIMEOUT);
2276 if (IS_ERR(skb)) {
2277 BT_ERR("%s: Retrieving Intel exception info failed (%ld)",
2278 hdev->name, PTR_ERR(skb));
2279 return;
2280 }
2281
2282 if (skb->len != 13) {
2283 BT_ERR("%s: Exception info size mismatch", hdev->name);
2284 kfree_skb(skb);
2285 return;
2286 }
2287
2288 if (skb->data[0] != 0x00) {
2289 BT_ERR("%s: Exception info command failure (%02x)",
2290 hdev->name, skb->data[0]);
2291 kfree_skb(skb);
2292 return;
2293 }
2294
2295 BT_ERR("%s: Exception info %s", hdev->name, (char *)(skb->data + 1));
2296
2297 kfree_skb(skb);
2298}
2299
Tedd Ho-Jeong Anbfbd45e2015-02-13 09:20:52 -08002300static int btusb_shutdown_intel(struct hci_dev *hdev)
2301{
2302 struct sk_buff *skb;
2303 long ret;
2304
2305 /* Some platforms have an issue with BT LED when the interface is
2306 * down or BT radio is turned off, which takes 5 seconds to BT LED
2307 * goes off. This command turns off the BT LED immediately.
2308 */
2309 skb = __hci_cmd_sync(hdev, 0xfc3f, 0, NULL, HCI_INIT_TIMEOUT);
2310 if (IS_ERR(skb)) {
2311 ret = PTR_ERR(skb);
2312 BT_ERR("%s: turning off Intel device LED failed (%ld)",
2313 hdev->name, ret);
2314 return ret;
2315 }
2316 kfree_skb(skb);
2317
2318 return 0;
2319}
2320
Amitkumar Karwarae8df492014-07-18 14:47:06 -07002321static int btusb_set_bdaddr_marvell(struct hci_dev *hdev,
2322 const bdaddr_t *bdaddr)
2323{
2324 struct sk_buff *skb;
2325 u8 buf[8];
2326 long ret;
2327
2328 buf[0] = 0xfe;
2329 buf[1] = sizeof(bdaddr_t);
2330 memcpy(buf + 2, bdaddr, sizeof(bdaddr_t));
2331
2332 skb = __hci_cmd_sync(hdev, 0xfc22, sizeof(buf), buf, HCI_INIT_TIMEOUT);
2333 if (IS_ERR(skb)) {
2334 ret = PTR_ERR(skb);
2335 BT_ERR("%s: changing Marvell device address failed (%ld)",
2336 hdev->name, ret);
2337 return ret;
2338 }
2339 kfree_skb(skb);
2340
2341 return 0;
2342}
2343
Toshi Kikuchi58592232014-12-12 10:58:05 -08002344static int btusb_set_bdaddr_ath3012(struct hci_dev *hdev,
2345 const bdaddr_t *bdaddr)
2346{
2347 struct sk_buff *skb;
2348 u8 buf[10];
2349 long ret;
2350
2351 buf[0] = 0x01;
2352 buf[1] = 0x01;
2353 buf[2] = 0x00;
2354 buf[3] = sizeof(bdaddr_t);
2355 memcpy(buf + 4, bdaddr, sizeof(bdaddr_t));
2356
2357 skb = __hci_cmd_sync(hdev, 0xfc0b, sizeof(buf), buf, HCI_INIT_TIMEOUT);
2358 if (IS_ERR(skb)) {
2359 ret = PTR_ERR(skb);
2360 BT_ERR("%s: Change address command failed (%ld)",
2361 hdev->name, ret);
2362 return ret;
2363 }
2364 kfree_skb(skb);
2365
2366 return 0;
2367}
2368
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002369#define QCA_DFU_PACKET_LEN 4096
2370
2371#define QCA_GET_TARGET_VERSION 0x09
2372#define QCA_CHECK_STATUS 0x05
2373#define QCA_DFU_DOWNLOAD 0x01
2374
2375#define QCA_SYSCFG_UPDATED 0x40
2376#define QCA_PATCH_UPDATED 0x80
2377#define QCA_DFU_TIMEOUT 3000
2378
2379struct qca_version {
2380 __le32 rom_version;
2381 __le32 patch_version;
2382 __le32 ram_version;
2383 __le32 ref_clock;
2384 __u8 reserved[4];
2385} __packed;
2386
2387struct qca_rampatch_version {
2388 __le16 rom_version;
2389 __le16 patch_version;
2390} __packed;
2391
2392struct qca_device_info {
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002393 u32 rom_version;
2394 u8 rampatch_hdr; /* length of header in rampatch */
2395 u8 nvm_hdr; /* length of header in NVM */
2396 u8 ver_offset; /* offset of version structure in rampatch */
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002397};
2398
2399static const struct qca_device_info qca_devices_table[] = {
2400 { 0x00000100, 20, 4, 10 }, /* Rome 1.0 */
2401 { 0x00000101, 20, 4, 10 }, /* Rome 1.1 */
2402 { 0x00000201, 28, 4, 18 }, /* Rome 2.1 */
2403 { 0x00000300, 28, 4, 18 }, /* Rome 3.0 */
2404 { 0x00000302, 28, 4, 18 }, /* Rome 3.2 */
2405};
2406
2407static int btusb_qca_send_vendor_req(struct hci_dev *hdev, u8 request,
2408 void *data, u16 size)
2409{
2410 struct btusb_data *btdata = hci_get_drvdata(hdev);
2411 struct usb_device *udev = btdata->udev;
2412 int pipe, err;
2413 u8 *buf;
2414
2415 buf = kmalloc(size, GFP_KERNEL);
2416 if (!buf)
2417 return -ENOMEM;
2418
2419 /* Found some of USB hosts have IOT issues with ours so that we should
2420 * not wait until HCI layer is ready.
2421 */
2422 pipe = usb_rcvctrlpipe(udev, 0);
2423 err = usb_control_msg(udev, pipe, request, USB_TYPE_VENDOR | USB_DIR_IN,
2424 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
2425 if (err < 0) {
2426 BT_ERR("%s: Failed to access otp area (%d)", hdev->name, err);
2427 goto done;
2428 }
2429
2430 memcpy(data, buf, size);
2431
2432done:
2433 kfree(buf);
2434
2435 return err;
2436}
2437
2438static int btusb_setup_qca_download_fw(struct hci_dev *hdev,
2439 const struct firmware *firmware,
2440 size_t hdr_size)
2441{
2442 struct btusb_data *btdata = hci_get_drvdata(hdev);
2443 struct usb_device *udev = btdata->udev;
2444 size_t count, size, sent = 0;
2445 int pipe, len, err;
2446 u8 *buf;
2447
2448 buf = kmalloc(QCA_DFU_PACKET_LEN, GFP_KERNEL);
2449 if (!buf)
2450 return -ENOMEM;
2451
2452 count = firmware->size;
2453
2454 size = min_t(size_t, count, hdr_size);
2455 memcpy(buf, firmware->data, size);
2456
2457 /* USB patches should go down to controller through USB path
2458 * because binary format fits to go down through USB channel.
2459 * USB control path is for patching headers and USB bulk is for
2460 * patch body.
2461 */
2462 pipe = usb_sndctrlpipe(udev, 0);
2463 err = usb_control_msg(udev, pipe, QCA_DFU_DOWNLOAD, USB_TYPE_VENDOR,
2464 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
2465 if (err < 0) {
2466 BT_ERR("%s: Failed to send headers (%d)", hdev->name, err);
2467 goto done;
2468 }
2469
2470 sent += size;
2471 count -= size;
2472
2473 while (count) {
2474 size = min_t(size_t, count, QCA_DFU_PACKET_LEN);
2475
2476 memcpy(buf, firmware->data + sent, size);
2477
2478 pipe = usb_sndbulkpipe(udev, 0x02);
2479 err = usb_bulk_msg(udev, pipe, buf, size, &len,
2480 QCA_DFU_TIMEOUT);
2481 if (err < 0) {
2482 BT_ERR("%s: Failed to send body at %zd of %zd (%d)",
2483 hdev->name, sent, firmware->size, err);
2484 break;
2485 }
2486
2487 if (size != len) {
2488 BT_ERR("%s: Failed to get bulk buffer", hdev->name);
2489 err = -EILSEQ;
2490 break;
2491 }
2492
2493 sent += size;
2494 count -= size;
2495 }
2496
2497done:
2498 kfree(buf);
2499 return err;
2500}
2501
2502static int btusb_setup_qca_load_rampatch(struct hci_dev *hdev,
2503 struct qca_version *ver,
2504 const struct qca_device_info *info)
2505{
2506 struct qca_rampatch_version *rver;
2507 const struct firmware *fw;
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002508 u32 ver_rom, ver_patch;
2509 u16 rver_rom, rver_patch;
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002510 char fwname[64];
2511 int err;
2512
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002513 ver_rom = le32_to_cpu(ver->rom_version);
2514 ver_patch = le32_to_cpu(ver->patch_version);
2515
2516 snprintf(fwname, sizeof(fwname), "qca/rampatch_usb_%08x.bin", ver_rom);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002517
2518 err = request_firmware(&fw, fwname, &hdev->dev);
2519 if (err) {
2520 BT_ERR("%s: failed to request rampatch file: %s (%d)",
2521 hdev->name, fwname, err);
2522 return err;
2523 }
2524
2525 BT_INFO("%s: using rampatch file: %s", hdev->name, fwname);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002526
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002527 rver = (struct qca_rampatch_version *)(fw->data + info->ver_offset);
2528 rver_rom = le16_to_cpu(rver->rom_version);
2529 rver_patch = le16_to_cpu(rver->patch_version);
2530
2531 BT_INFO("%s: QCA: patch rome 0x%x build 0x%x, firmware rome 0x%x "
2532 "build 0x%x", hdev->name, rver_rom, rver_patch, ver_rom,
2533 ver_patch);
2534
2535 if (rver_rom != ver_rom || rver_patch <= ver_patch) {
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002536 BT_ERR("%s: rampatch file version did not match with firmware",
2537 hdev->name);
2538 err = -EINVAL;
2539 goto done;
2540 }
2541
2542 err = btusb_setup_qca_download_fw(hdev, fw, info->rampatch_hdr);
2543
2544done:
2545 release_firmware(fw);
2546
2547 return err;
2548}
2549
2550static int btusb_setup_qca_load_nvm(struct hci_dev *hdev,
2551 struct qca_version *ver,
2552 const struct qca_device_info *info)
2553{
2554 const struct firmware *fw;
2555 char fwname[64];
2556 int err;
2557
2558 snprintf(fwname, sizeof(fwname), "qca/nvm_usb_%08x.bin",
2559 le32_to_cpu(ver->rom_version));
2560
2561 err = request_firmware(&fw, fwname, &hdev->dev);
2562 if (err) {
2563 BT_ERR("%s: failed to request NVM file: %s (%d)",
2564 hdev->name, fwname, err);
2565 return err;
2566 }
2567
2568 BT_INFO("%s: using NVM file: %s", hdev->name, fwname);
2569
2570 err = btusb_setup_qca_download_fw(hdev, fw, info->nvm_hdr);
2571
2572 release_firmware(fw);
2573
2574 return err;
2575}
2576
2577static int btusb_setup_qca(struct hci_dev *hdev)
2578{
2579 const struct qca_device_info *info = NULL;
2580 struct qca_version ver;
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002581 u32 ver_rom;
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002582 u8 status;
2583 int i, err;
2584
2585 err = btusb_qca_send_vendor_req(hdev, QCA_GET_TARGET_VERSION, &ver,
2586 sizeof(ver));
2587 if (err < 0)
2588 return err;
2589
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002590 ver_rom = le32_to_cpu(ver.rom_version);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002591 for (i = 0; i < ARRAY_SIZE(qca_devices_table); i++) {
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002592 if (ver_rom == qca_devices_table[i].rom_version)
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002593 info = &qca_devices_table[i];
2594 }
2595 if (!info) {
2596 BT_ERR("%s: don't support firmware rome 0x%x", hdev->name,
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002597 ver_rom);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002598 return -ENODEV;
2599 }
2600
2601 err = btusb_qca_send_vendor_req(hdev, QCA_CHECK_STATUS, &status,
2602 sizeof(status));
2603 if (err < 0)
2604 return err;
2605
2606 if (!(status & QCA_PATCH_UPDATED)) {
2607 err = btusb_setup_qca_load_rampatch(hdev, &ver, info);
2608 if (err < 0)
2609 return err;
2610 }
2611
2612 if (!(status & QCA_SYSCFG_UPDATED)) {
2613 err = btusb_setup_qca_load_nvm(hdev, &ver, info);
2614 if (err < 0)
2615 return err;
2616 }
2617
2618 return 0;
2619}
2620
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002621static int btusb_probe(struct usb_interface *intf,
Marcel Holtmann89e75332014-09-16 04:44:50 +02002622 const struct usb_device_id *id)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002623{
2624 struct usb_endpoint_descriptor *ep_desc;
2625 struct btusb_data *data;
2626 struct hci_dev *hdev;
2627 int i, err;
2628
2629 BT_DBG("intf %p id %p", intf, id);
2630
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002631 /* interface numbers are hardcoded in the spec */
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002632 if (intf->cur_altsetting->desc.bInterfaceNumber != 0)
2633 return -ENODEV;
2634
2635 if (!id->driver_info) {
2636 const struct usb_device_id *match;
Marcel Holtmann89e75332014-09-16 04:44:50 +02002637
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002638 match = usb_match_id(intf, blacklist_table);
2639 if (match)
2640 id = match;
2641 }
2642
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002643 if (id->driver_info == BTUSB_IGNORE)
2644 return -ENODEV;
2645
Steven.Li2d25f8b2011-07-01 14:02:36 +08002646 if (id->driver_info & BTUSB_ATH3012) {
2647 struct usb_device *udev = interface_to_usbdev(intf);
2648
2649 /* Old firmware would otherwise let ath3k driver load
2650 * patch and sysconfig files */
2651 if (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x0001)
2652 return -ENODEV;
2653 }
2654
Sachin Kamat98921db2012-07-27 12:38:39 +05302655 data = devm_kzalloc(&intf->dev, sizeof(*data), GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002656 if (!data)
2657 return -ENOMEM;
2658
2659 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
2660 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
2661
2662 if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
2663 data->intr_ep = ep_desc;
2664 continue;
2665 }
2666
2667 if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
2668 data->bulk_tx_ep = ep_desc;
2669 continue;
2670 }
2671
2672 if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
2673 data->bulk_rx_ep = ep_desc;
2674 continue;
2675 }
2676 }
2677
Sachin Kamat98921db2012-07-27 12:38:39 +05302678 if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002679 return -ENODEV;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002680
Marcel Holtmann893ba542015-01-28 20:27:34 -08002681 if (id->driver_info & BTUSB_AMP) {
2682 data->cmdreq_type = USB_TYPE_CLASS | 0x01;
2683 data->cmdreq = 0x2b;
2684 } else {
2685 data->cmdreq_type = USB_TYPE_CLASS;
2686 data->cmdreq = 0x00;
2687 }
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002688
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002689 data->udev = interface_to_usbdev(intf);
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02002690 data->intf = intf;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002691
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002692 INIT_WORK(&data->work, btusb_work);
Oliver Neukum7bee5492009-08-24 23:44:59 +02002693 INIT_WORK(&data->waker, btusb_waker);
Marcel Holtmann803b5832014-09-16 08:00:29 +02002694 init_usb_anchor(&data->deferred);
2695 init_usb_anchor(&data->tx_anchor);
Oliver Neukum7bee5492009-08-24 23:44:59 +02002696 spin_lock_init(&data->txlock);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002697
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002698 init_usb_anchor(&data->intr_anchor);
2699 init_usb_anchor(&data->bulk_anchor);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02002700 init_usb_anchor(&data->isoc_anchor);
Marcel Holtmann803b5832014-09-16 08:00:29 +02002701 spin_lock_init(&data->rxlock);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002702
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002703 if (id->driver_info & BTUSB_INTEL_NEW) {
2704 data->recv_event = btusb_recv_event_intel;
2705 data->recv_bulk = btusb_recv_bulk_intel;
2706 set_bit(BTUSB_BOOTLOADER, &data->flags);
2707 } else {
2708 data->recv_event = hci_recv_frame;
2709 data->recv_bulk = btusb_recv_bulk;
2710 }
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +01002711
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002712 hdev = hci_alloc_dev();
Sachin Kamat98921db2012-07-27 12:38:39 +05302713 if (!hdev)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002714 return -ENOMEM;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002715
Marcel Holtmannc13854c2010-02-08 15:27:07 +01002716 hdev->bus = HCI_USB;
David Herrmann155961e2012-02-09 21:58:32 +01002717 hci_set_drvdata(hdev, data);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002718
Marcel Holtmann893ba542015-01-28 20:27:34 -08002719 if (id->driver_info & BTUSB_AMP)
2720 hdev->dev_type = HCI_AMP;
2721 else
2722 hdev->dev_type = HCI_BREDR;
2723
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002724 data->hdev = hdev;
2725
2726 SET_HCIDEV_DEV(hdev, &intf->dev);
2727
Marcel Holtmann9f8f9622013-04-10 08:11:35 -07002728 hdev->open = btusb_open;
2729 hdev->close = btusb_close;
2730 hdev->flush = btusb_flush;
2731 hdev->send = btusb_send_frame;
2732 hdev->notify = btusb_notify;
2733
2734 if (id->driver_info & BTUSB_BCM92035)
2735 hdev->setup = btusb_setup_bcm92035;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002736
Marcel Holtmannc2bfb102015-04-05 22:52:14 -07002737#ifdef CONFIG_BT_HCIBTUSB_BCM
Marcel Holtmannabbaf502014-07-02 00:53:48 +02002738 if (id->driver_info & BTUSB_BCM_PATCHRAM) {
Marcel Holtmannc2bfb102015-04-05 22:52:14 -07002739 hdev->setup = btbcm_setup_patchram;
Marcel Holtmann1df1f592015-04-05 22:52:11 -07002740 hdev->set_bdaddr = btbcm_set_bdaddr;
Marcel Holtmannabbaf502014-07-02 00:53:48 +02002741 }
Petri Gynther10d4c672014-05-08 15:50:01 -07002742
Marcel Holtmann941521e2015-04-05 22:52:15 -07002743 if (id->driver_info & BTUSB_BCM_APPLE)
Marcel Holtmannc2bfb102015-04-05 22:52:14 -07002744 hdev->setup = btbcm_setup_apple;
Marcel Holtmannc2bfb102015-04-05 22:52:14 -07002745#endif
Marcel Holtmann17b27722015-03-22 15:52:38 +01002746
Marcel Holtmanncb8d6592014-07-02 11:25:25 +02002747 if (id->driver_info & BTUSB_INTEL) {
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002748 hdev->setup = btusb_setup_intel;
Tedd Ho-Jeong Anbfbd45e2015-02-13 09:20:52 -08002749 hdev->shutdown = btusb_shutdown_intel;
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07002750 hdev->set_bdaddr = btintel_set_bdaddr;
Jakub Pawlowskic33fb9b2015-01-30 18:55:58 -08002751 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
Jakub Pawlowskic1154842015-03-17 09:04:16 -07002752 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
Marcel Holtmanncb8d6592014-07-02 11:25:25 +02002753 }
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002754
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002755 if (id->driver_info & BTUSB_INTEL_NEW) {
2756 hdev->send = btusb_send_frame_intel;
2757 hdev->setup = btusb_setup_intel_new;
Marcel Holtmann385a7682015-01-28 11:09:56 -08002758 hdev->hw_error = btusb_hw_error_intel;
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07002759 hdev->set_bdaddr = btintel_set_bdaddr;
Marcel Holtmannb970c5b2015-02-01 23:57:18 -08002760 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002761 }
2762
Amitkumar Karwarae8df492014-07-18 14:47:06 -07002763 if (id->driver_info & BTUSB_MARVELL)
2764 hdev->set_bdaddr = btusb_set_bdaddr_marvell;
2765
Marcel Holtmann661cf882015-01-02 23:35:20 -08002766 if (id->driver_info & BTUSB_SWAVE) {
2767 set_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks);
Marcel Holtmannd57dbe72014-12-26 04:42:33 +01002768 set_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS, &hdev->quirks);
Marcel Holtmann661cf882015-01-02 23:35:20 -08002769 }
Marcel Holtmannd57dbe72014-12-26 04:42:33 +01002770
Marcel Holtmann40df7832014-07-06 13:29:58 +02002771 if (id->driver_info & BTUSB_INTEL_BOOT)
2772 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
2773
Jakub Pawlowski79f0c872015-01-29 10:38:34 -08002774 if (id->driver_info & BTUSB_ATH3012) {
Toshi Kikuchi58592232014-12-12 10:58:05 -08002775 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
Jakub Pawlowski3d50d512015-03-17 09:04:15 -07002776 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
Jakub Pawlowski79f0c872015-01-29 10:38:34 -08002777 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
2778 }
Toshi Kikuchi58592232014-12-12 10:58:05 -08002779
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002780 if (id->driver_info & BTUSB_QCA_ROME) {
2781 data->setup_on_usb = btusb_setup_qca;
2782 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
2783 }
2784
Marcel Holtmann893ba542015-01-28 20:27:34 -08002785 if (id->driver_info & BTUSB_AMP) {
2786 /* AMP controllers do not support SCO packets */
2787 data->isoc = NULL;
2788 } else {
2789 /* Interface numbers are hardcoded in the specification */
2790 data->isoc = usb_ifnum_to_if(data->udev, 1);
2791 }
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02002792
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002793 if (!reset)
Szymon Janca6c511c2012-05-23 12:35:46 +02002794 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002795
2796 if (force_scofix || id->driver_info & BTUSB_WRONG_SCO_MTU) {
2797 if (!disable_scofix)
2798 set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
2799 }
2800
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02002801 if (id->driver_info & BTUSB_BROKEN_ISOC)
2802 data->isoc = NULL;
2803
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002804 if (id->driver_info & BTUSB_DIGIANSWER) {
2805 data->cmdreq_type = USB_TYPE_VENDOR;
Szymon Janca6c511c2012-05-23 12:35:46 +02002806 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002807 }
2808
2809 if (id->driver_info & BTUSB_CSR) {
2810 struct usb_device *udev = data->udev;
Marcel Holtmann81cac642014-01-03 03:02:36 -08002811 u16 bcdDevice = le16_to_cpu(udev->descriptor.bcdDevice);
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002812
2813 /* Old firmware would otherwise execute USB reset */
Marcel Holtmann81cac642014-01-03 03:02:36 -08002814 if (bcdDevice < 0x117)
Szymon Janca6c511c2012-05-23 12:35:46 +02002815 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmann81cac642014-01-03 03:02:36 -08002816
2817 /* Fake CSR devices with broken commands */
2818 if (bcdDevice <= 0x100)
2819 hdev->setup = btusb_setup_csr;
Jakub Pawlowski49c989a2015-03-17 09:04:17 -07002820
2821 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002822 }
2823
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002824 if (id->driver_info & BTUSB_SNIFFER) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02002825 struct usb_device *udev = data->udev;
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002826
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002827 /* New sniffer firmware has crippled HCI interface */
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002828 if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
2829 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
2830 }
2831
Marcel Holtmann3a5ef202014-07-06 14:53:54 +02002832 if (id->driver_info & BTUSB_INTEL_BOOT) {
2833 /* A bug in the bootloader causes that interrupt interface is
2834 * only enabled after receiving SetInterface(0, AltSetting=0).
2835 */
2836 err = usb_set_interface(data->udev, 0, 0);
2837 if (err < 0) {
2838 BT_ERR("failed to set interface 0, alt 0 %d", err);
2839 hci_free_dev(hdev);
2840 return err;
2841 }
2842 }
2843
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02002844 if (data->isoc) {
2845 err = usb_driver_claim_interface(&btusb_driver,
Marcel Holtmann89e75332014-09-16 04:44:50 +02002846 data->isoc, data);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02002847 if (err < 0) {
2848 hci_free_dev(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02002849 return err;
2850 }
2851 }
2852
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002853 err = hci_register_dev(hdev);
2854 if (err < 0) {
2855 hci_free_dev(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002856 return err;
2857 }
2858
2859 usb_set_intfdata(intf, data);
2860
2861 return 0;
2862}
2863
2864static void btusb_disconnect(struct usb_interface *intf)
2865{
2866 struct btusb_data *data = usb_get_intfdata(intf);
2867 struct hci_dev *hdev;
2868
2869 BT_DBG("intf %p", intf);
2870
2871 if (!data)
2872 return;
2873
2874 hdev = data->hdev;
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02002875 usb_set_intfdata(data->intf, NULL);
2876
2877 if (data->isoc)
2878 usb_set_intfdata(data->isoc, NULL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002879
2880 hci_unregister_dev(hdev);
2881
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02002882 if (intf == data->isoc)
2883 usb_driver_release_interface(&btusb_driver, data->intf);
2884 else if (data->isoc)
2885 usb_driver_release_interface(&btusb_driver, data->isoc);
2886
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002887 hci_free_dev(hdev);
2888}
2889
Oliver Neukum7bee5492009-08-24 23:44:59 +02002890#ifdef CONFIG_PM
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002891static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
2892{
2893 struct btusb_data *data = usb_get_intfdata(intf);
2894
2895 BT_DBG("intf %p", intf);
2896
2897 if (data->suspend_count++)
2898 return 0;
2899
Oliver Neukum7bee5492009-08-24 23:44:59 +02002900 spin_lock_irq(&data->txlock);
Alan Stern5b1b0b82011-08-19 23:49:48 +02002901 if (!(PMSG_IS_AUTO(message) && data->tx_in_flight)) {
Oliver Neukum7bee5492009-08-24 23:44:59 +02002902 set_bit(BTUSB_SUSPENDING, &data->flags);
2903 spin_unlock_irq(&data->txlock);
2904 } else {
2905 spin_unlock_irq(&data->txlock);
2906 data->suspend_count--;
2907 return -EBUSY;
2908 }
2909
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002910 cancel_work_sync(&data->work);
2911
Oliver Neukum7bee5492009-08-24 23:44:59 +02002912 btusb_stop_traffic(data);
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002913 usb_kill_anchored_urbs(&data->tx_anchor);
2914
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002915 return 0;
2916}
2917
Oliver Neukum7bee5492009-08-24 23:44:59 +02002918static void play_deferred(struct btusb_data *data)
2919{
2920 struct urb *urb;
2921 int err;
2922
2923 while ((urb = usb_get_from_anchor(&data->deferred))) {
2924 err = usb_submit_urb(urb, GFP_ATOMIC);
2925 if (err < 0)
2926 break;
2927
2928 data->tx_in_flight++;
2929 }
2930 usb_scuttle_anchored_urbs(&data->deferred);
2931}
2932
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002933static int btusb_resume(struct usb_interface *intf)
2934{
2935 struct btusb_data *data = usb_get_intfdata(intf);
2936 struct hci_dev *hdev = data->hdev;
Oliver Neukum7bee5492009-08-24 23:44:59 +02002937 int err = 0;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002938
2939 BT_DBG("intf %p", intf);
2940
2941 if (--data->suspend_count)
2942 return 0;
2943
2944 if (!test_bit(HCI_RUNNING, &hdev->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +02002945 goto done;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002946
2947 if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
2948 err = btusb_submit_intr_urb(hdev, GFP_NOIO);
2949 if (err < 0) {
2950 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02002951 goto failed;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002952 }
2953 }
2954
2955 if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) {
Marcel Holtmann43c2e572009-02-04 17:41:38 +01002956 err = btusb_submit_bulk_urb(hdev, GFP_NOIO);
2957 if (err < 0) {
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002958 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02002959 goto failed;
2960 }
2961
2962 btusb_submit_bulk_urb(hdev, GFP_NOIO);
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002963 }
2964
2965 if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
2966 if (btusb_submit_isoc_urb(hdev, GFP_NOIO) < 0)
2967 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
2968 else
2969 btusb_submit_isoc_urb(hdev, GFP_NOIO);
2970 }
2971
Oliver Neukum7bee5492009-08-24 23:44:59 +02002972 spin_lock_irq(&data->txlock);
2973 play_deferred(data);
2974 clear_bit(BTUSB_SUSPENDING, &data->flags);
2975 spin_unlock_irq(&data->txlock);
2976 schedule_work(&data->work);
2977
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002978 return 0;
Oliver Neukum7bee5492009-08-24 23:44:59 +02002979
2980failed:
2981 usb_scuttle_anchored_urbs(&data->deferred);
2982done:
2983 spin_lock_irq(&data->txlock);
2984 clear_bit(BTUSB_SUSPENDING, &data->flags);
2985 spin_unlock_irq(&data->txlock);
2986
2987 return err;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002988}
Oliver Neukum7bee5492009-08-24 23:44:59 +02002989#endif
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002990
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002991static struct usb_driver btusb_driver = {
2992 .name = "btusb",
2993 .probe = btusb_probe,
2994 .disconnect = btusb_disconnect,
Oliver Neukum7bee5492009-08-24 23:44:59 +02002995#ifdef CONFIG_PM
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002996 .suspend = btusb_suspend,
2997 .resume = btusb_resume,
Oliver Neukum7bee5492009-08-24 23:44:59 +02002998#endif
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002999 .id_table = btusb_table,
Oliver Neukum7bee5492009-08-24 23:44:59 +02003000 .supports_autosuspend = 1,
Sarah Sharpe1f12eb2012-04-23 10:08:51 -07003001 .disable_hub_initiated_lpm = 1,
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003002};
3003
Greg Kroah-Hartman93f15082011-11-18 09:47:34 -08003004module_usb_driver(btusb_driver);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003005
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02003006module_param(disable_scofix, bool, 0644);
3007MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
3008
3009module_param(force_scofix, bool, 0644);
3010MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
3011
3012module_param(reset, bool, 0644);
3013MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
3014
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003015MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
3016MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
3017MODULE_VERSION(VERSION);
3018MODULE_LICENSE("GPL");