blob: 94c6c048130fe61be8173248bdb769eb7d50f84b [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>
Daniel Drakea2698a92015-04-16 14:09:55 -060027#include <asm/unaligned.h>
Marcel Holtmann5e23b922007-10-20 14:12:34 +020028
29#include <net/bluetooth/bluetooth.h>
30#include <net/bluetooth/hci_core.h>
31
Marcel Holtmann4185a0f2015-04-06 00:52:12 -070032#include "btintel.h"
Marcel Holtmann1df1f592015-04-05 22:52:11 -070033#include "btbcm.h"
Carlo Caionedb33c772015-05-14 10:49:09 +020034#include "btrtl.h"
Marcel Holtmann1df1f592015-04-05 22:52:11 -070035
Marcel Holtmann34dced9b2015-04-05 22:52:16 -070036#define VERSION "0.8"
Marcel Holtmanncfeb4142008-08-07 22:26:56 +020037
Rusty Russell90ab5ee2012-01-13 09:32:20 +103038static bool disable_scofix;
39static bool force_scofix;
Marcel Holtmann7a9d4022008-11-30 12:17:26 +010040
Shailendra Verma917a3332015-05-25 23:53:40 +053041static bool reset = true;
Marcel Holtmanncfeb4142008-08-07 22:26:56 +020042
43static struct usb_driver btusb_driver;
44
45#define BTUSB_IGNORE 0x01
Marcel Holtmann7a9d4022008-11-30 12:17:26 +010046#define BTUSB_DIGIANSWER 0x02
47#define BTUSB_CSR 0x04
48#define BTUSB_SNIFFER 0x08
49#define BTUSB_BCM92035 0x10
50#define BTUSB_BROKEN_ISOC 0x20
51#define BTUSB_WRONG_SCO_MTU 0x40
Steven.Li2d25f8b2011-07-01 14:02:36 +080052#define BTUSB_ATH3012 0x80
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -070053#define BTUSB_INTEL 0x100
Marcel Holtmann40df7832014-07-06 13:29:58 +020054#define BTUSB_INTEL_BOOT 0x200
55#define BTUSB_BCM_PATCHRAM 0x400
Amitkumar Karwarae8df492014-07-18 14:47:06 -070056#define BTUSB_MARVELL 0x800
Marcel Holtmann4fcef8e2015-01-01 17:34:37 -080057#define BTUSB_SWAVE 0x1000
Marcel Holtmanncda0dd72015-01-26 21:33:48 -080058#define BTUSB_INTEL_NEW 0x2000
Marcel Holtmann893ba542015-01-28 20:27:34 -080059#define BTUSB_AMP 0x4000
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +000060#define BTUSB_QCA_ROME 0x8000
Marcel Holtmann17b27722015-03-22 15:52:38 +010061#define BTUSB_BCM_APPLE 0x10000
Daniel Drakea2698a92015-04-16 14:09:55 -060062#define BTUSB_REALTEK 0x20000
Marcel Holtmann5e23b922007-10-20 14:12:34 +020063
Marcel Holtmann54265202013-10-11 07:46:18 -070064static const struct usb_device_id btusb_table[] = {
Marcel Holtmann5e23b922007-10-20 14:12:34 +020065 /* Generic Bluetooth USB device */
66 { USB_DEVICE_INFO(0xe0, 0x01, 0x01) },
67
Marcel Holtmann893ba542015-01-28 20:27:34 -080068 /* Generic Bluetooth AMP device */
69 { USB_DEVICE_INFO(0xe0, 0x01, 0x04), .driver_info = BTUSB_AMP },
70
Henrik Rydberg1fa65352012-08-25 19:28:06 +020071 /* Apple-specific (Broadcom) devices */
Marcel Holtmann17b27722015-03-22 15:52:38 +010072 { USB_VENDOR_AND_INTERFACE_INFO(0x05ac, 0xff, 0x01, 0x01),
73 .driver_info = BTUSB_BCM_APPLE },
Henrik Rydberg1fa65352012-08-25 19:28:06 +020074
Cho, Yu-Chen178c0592013-06-04 21:40:26 +080075 /* MediaTek MT76x0E */
76 { USB_DEVICE(0x0e8d, 0x763f) },
77
Oliver Neukumc510eae2011-09-21 11:41:45 +020078 /* Broadcom SoftSailing reporting vendor specific */
Don Zickus2e8b50632012-03-28 16:41:11 -040079 { USB_DEVICE(0x0a5c, 0x21e1) },
Oliver Neukumc510eae2011-09-21 11:41:45 +020080
Nobuhiro Iwamatsu3cd01972010-08-20 16:24:07 +090081 /* Apple MacBookPro 7,1 */
82 { USB_DEVICE(0x05ac, 0x8213) },
83
Cyril Lacoux0a79f672010-07-14 10:29:27 +040084 /* Apple iMac11,1 */
85 { USB_DEVICE(0x05ac, 0x8215) },
86
Nobuhiro Iwamatsu9c047152010-08-20 16:24:06 +090087 /* Apple MacBookPro6,2 */
88 { USB_DEVICE(0x05ac, 0x8218) },
89
Edgar (gimli) Hucek3e3ede72010-11-04 08:04:33 +010090 /* Apple MacBookAir3,1, MacBookAir3,2 */
91 { USB_DEVICE(0x05ac, 0x821b) },
92
Pieter-Augustijn Van Malleghema63b7232011-09-07 02:28:10 -040093 /* Apple MacBookAir4,1 */
94 { USB_DEVICE(0x05ac, 0x821f) },
95
Marc-Antoine Perennou88d377b2011-03-24 14:51:21 -030096 /* Apple MacBookPro8,2 */
97 { USB_DEVICE(0x05ac, 0x821a) },
98
Jurgen Kramerf78b68262011-09-04 18:01:42 +020099 /* Apple MacMini5,1 */
100 { USB_DEVICE(0x05ac, 0x8281) },
101
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200102 /* AVM BlueFRITZ! USB v2.0 */
Marcel Holtmann4fcef8e2015-01-01 17:34:37 -0800103 { USB_DEVICE(0x057c, 0x3800), .driver_info = BTUSB_SWAVE },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200104
105 /* Bluetooth Ultraport Module from IBM */
106 { USB_DEVICE(0x04bf, 0x030a) },
107
108 /* ALPS Modules with non-standard id */
109 { USB_DEVICE(0x044e, 0x3001) },
110 { USB_DEVICE(0x044e, 0x3002) },
111
112 /* Ericsson with non-standard id */
113 { USB_DEVICE(0x0bdb, 0x1002) },
114
115 /* Canyon CN-BTU1 with HID interfaces */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100116 { USB_DEVICE(0x0c10, 0x0000) },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200117
Wen-chien Jesse Sungd13431c2011-11-08 14:30:22 +0800118 /* Broadcom BCM20702A0 */
119 { USB_DEVICE(0x413c, 0x8197) },
120
Marcel Holtmannd049f4e2015-01-26 20:35:32 -0800121 /* Broadcom BCM20702B0 (Dynex/Insignia) */
122 { USB_DEVICE(0x19ff, 0x0239), .driver_info = BTUSB_BCM_PATCHRAM },
123
Steven Harms98514032012-04-13 14:45:55 -0400124 /* Foxconn - Hon Hai */
Heinrich Siebmanns6029ddc2014-12-03 19:32:22 +0100125 { USB_VENDOR_AND_INTERFACE_INFO(0x0489, 0xff, 0x01, 0x01),
126 .driver_info = BTUSB_BCM_PATCHRAM },
Steven Harms98514032012-04-13 14:45:55 -0400127
Matej Dubovy8f0c3042015-02-02 18:50:14 +0100128 /* Lite-On Technology - Broadcom based */
129 { USB_VENDOR_AND_INTERFACE_INFO(0x04ca, 0xff, 0x01, 0x01),
130 .driver_info = BTUSB_BCM_PATCHRAM },
131
Andy Shevchenko0b880062014-02-18 18:26:19 +0200132 /* Broadcom devices with vendor specific id */
Petri Gynther10d4c672014-05-08 15:50:01 -0700133 { USB_VENDOR_AND_INTERFACE_INFO(0x0a5c, 0xff, 0x01, 0x01),
134 .driver_info = BTUSB_BCM_PATCHRAM },
Gustavo Padovan92c385f2012-08-06 15:36:49 -0300135
Marcel Holtmannc2aef6e2014-07-21 14:02:33 +0200136 /* ASUSTek Computer - Broadcom based */
Rick Dunn9a5abdaa2015-01-17 05:29:12 +0100137 { USB_VENDOR_AND_INTERFACE_INFO(0x0b05, 0xff, 0x01, 0x01),
138 .driver_info = BTUSB_BCM_PATCHRAM },
Marcel Holtmannc2aef6e2014-07-21 14:02:33 +0200139
Ken O'Brien5bcecf32013-09-21 19:14:43 +0100140 /* Belkin F8065bf - Broadcom based */
Marcel Holtmann6331c682015-03-27 15:11:41 -0700141 { USB_VENDOR_AND_INTERFACE_INFO(0x050d, 0xff, 0x01, 0x01),
142 .driver_info = BTUSB_BCM_PATCHRAM },
Ken O'Brien5bcecf32013-09-21 19:14:43 +0100143
Jurgen Kramer9113bfd2014-02-15 12:01:09 +0100144 /* IMC Networks - Broadcom based */
Marcel Holtmann6331c682015-03-27 15:11:41 -0700145 { USB_VENDOR_AND_INTERFACE_INFO(0x13d3, 0xff, 0x01, 0x01),
146 .driver_info = BTUSB_BCM_PATCHRAM },
Jurgen Kramer9113bfd2014-02-15 12:01:09 +0100147
Marcel Holtmann40df7832014-07-06 13:29:58 +0200148 /* Intel Bluetooth USB Bootloader (RAM module) */
Marcel Holtmannd92f2df2014-07-06 14:53:55 +0200149 { USB_DEVICE(0x8087, 0x0a5a),
150 .driver_info = BTUSB_INTEL_BOOT | BTUSB_BROKEN_ISOC },
Marcel Holtmann40df7832014-07-06 13:29:58 +0200151
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200152 { } /* Terminating entry */
153};
154
155MODULE_DEVICE_TABLE(usb, btusb_table);
156
Marcel Holtmann54265202013-10-11 07:46:18 -0700157static const struct usb_device_id blacklist_table[] = {
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200158 /* CSR BlueCore devices */
159 { USB_DEVICE(0x0a12, 0x0001), .driver_info = BTUSB_CSR },
160
161 /* Broadcom BCM2033 without firmware */
162 { USB_DEVICE(0x0a5c, 0x2033), .driver_info = BTUSB_IGNORE },
163
Bala Shanmugambe931122010-11-26 17:35:46 +0530164 /* Atheros 3011 with sflash firmware */
Andy Shevchenko0b880062014-02-18 18:26:19 +0200165 { USB_DEVICE(0x0489, 0xe027), .driver_info = BTUSB_IGNORE },
166 { USB_DEVICE(0x0489, 0xe03d), .driver_info = BTUSB_IGNORE },
Alexander Ploumistos2eeff0b2015-02-13 21:05:11 +0200167 { USB_DEVICE(0x04f2, 0xaff1), .driver_info = BTUSB_IGNORE },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200168 { USB_DEVICE(0x0930, 0x0215), .driver_info = BTUSB_IGNORE },
Bala Shanmugambe931122010-11-26 17:35:46 +0530169 { USB_DEVICE(0x0cf3, 0x3002), .driver_info = BTUSB_IGNORE },
Marek Vasut6eda5412012-06-08 14:32:50 +0200170 { USB_DEVICE(0x0cf3, 0xe019), .driver_info = BTUSB_IGNORE },
Andy Ross2a7bccc2011-05-09 16:11:16 -0700171 { USB_DEVICE(0x13d3, 0x3304), .driver_info = BTUSB_IGNORE },
Bala Shanmugambe931122010-11-26 17:35:46 +0530172
Cho, Yu-Chen509e7862011-01-26 17:10:59 +0800173 /* Atheros AR9285 Malbec with sflash firmware */
174 { USB_DEVICE(0x03f0, 0x311d), .driver_info = BTUSB_IGNORE },
175
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530176 /* Atheros 3012 with sflash firmware */
Andy Shevchenko0b880062014-02-18 18:26:19 +0200177 { USB_DEVICE(0x0489, 0xe04d), .driver_info = BTUSB_ATH3012 },
178 { USB_DEVICE(0x0489, 0xe04e), .driver_info = BTUSB_ATH3012 },
179 { USB_DEVICE(0x0489, 0xe056), .driver_info = BTUSB_ATH3012 },
180 { USB_DEVICE(0x0489, 0xe057), .driver_info = BTUSB_ATH3012 },
181 { USB_DEVICE(0x0489, 0xe05f), .driver_info = BTUSB_ATH3012 },
Anantha Krishnan4b552bc2014-10-06 16:31:49 +0530182 { USB_DEVICE(0x0489, 0xe078), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200183 { USB_DEVICE(0x04c5, 0x1330), .driver_info = BTUSB_ATH3012 },
184 { USB_DEVICE(0x04ca, 0x3004), .driver_info = BTUSB_ATH3012 },
185 { USB_DEVICE(0x04ca, 0x3005), .driver_info = BTUSB_ATH3012 },
186 { USB_DEVICE(0x04ca, 0x3006), .driver_info = BTUSB_ATH3012 },
Mohammed Habibulla1fb4e092014-04-17 11:37:13 -0700187 { USB_DEVICE(0x04ca, 0x3007), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200188 { USB_DEVICE(0x04ca, 0x3008), .driver_info = BTUSB_ATH3012 },
189 { USB_DEVICE(0x04ca, 0x300b), .driver_info = BTUSB_ATH3012 },
Dmitry Tuninec0810d2015-05-02 13:36:58 +0300190 { USB_DEVICE(0x04ca, 0x300f), .driver_info = BTUSB_ATH3012 },
Janne Heikkinen134d3b32014-12-09 07:44:51 +0200191 { USB_DEVICE(0x04ca, 0x3010), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200192 { USB_DEVICE(0x0930, 0x0219), .driver_info = BTUSB_ATH3012 },
193 { USB_DEVICE(0x0930, 0x0220), .driver_info = BTUSB_ATH3012 },
Vincent Zwanenburg89d29752014-08-08 12:33:56 +0100194 { USB_DEVICE(0x0930, 0x0227), .driver_info = BTUSB_ATH3012 },
Andy Shevchenkoa735f9e2014-02-18 18:26:20 +0200195 { USB_DEVICE(0x0b05, 0x17d0), .driver_info = BTUSB_ATH3012 },
Ming Leid66629c2013-03-15 11:00:39 +0800196 { USB_DEVICE(0x0cf3, 0x0036), .driver_info = BTUSB_ATH3012 },
Steven.Li2d25f8b2011-07-01 14:02:36 +0800197 { USB_DEVICE(0x0cf3, 0x3004), .driver_info = BTUSB_ATH3012 },
Sunguk Lee94a32d102013-03-12 04:41:58 +0900198 { USB_DEVICE(0x0cf3, 0x3008), .driver_info = BTUSB_ATH3012 },
Cho, Yu-Chen07c0ea82012-03-14 22:01:21 +0200199 { USB_DEVICE(0x0cf3, 0x311d), .driver_info = BTUSB_ATH3012 },
Oliver Neukumb1312372014-01-16 15:37:11 +0100200 { USB_DEVICE(0x0cf3, 0x311e), .driver_info = BTUSB_ATH3012 },
Oliver Neukum1e56f1e2014-01-16 16:02:58 +0100201 { USB_DEVICE(0x0cf3, 0x311f), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200202 { USB_DEVICE(0x0cf3, 0x3121), .driver_info = BTUSB_ATH3012 },
Ming Leiebaf5792013-03-18 23:45:11 +0800203 { USB_DEVICE(0x0cf3, 0x817a), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200204 { USB_DEVICE(0x0cf3, 0xe003), .driver_info = BTUSB_ATH3012 },
AceLan Kaoac713112012-04-19 14:53:45 +0800205 { USB_DEVICE(0x0cf3, 0xe004), .driver_info = BTUSB_ATH3012 },
Peng Chen0a3658c2013-08-30 17:41:40 +0800206 { USB_DEVICE(0x0cf3, 0xe005), .driver_info = BTUSB_ATH3012 },
Wen-chien Jesse Sungca79f232015-05-13 11:39:24 +0800207 { USB_DEVICE(0x0cf3, 0xe006), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200208 { USB_DEVICE(0x13d3, 0x3362), .driver_info = BTUSB_ATH3012 },
209 { USB_DEVICE(0x13d3, 0x3375), .driver_info = BTUSB_ATH3012 },
AceLan Kaoeed307e2012-12-11 11:41:20 +0800210 { USB_DEVICE(0x13d3, 0x3393), .driver_info = BTUSB_ATH3012 },
Sujith Manoharan5b77a1f2013-07-15 09:29:03 +0530211 { USB_DEVICE(0x13d3, 0x3402), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin3bb30a7c2014-11-25 20:19:52 +0300212 { USB_DEVICE(0x13d3, 0x3408), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin033efa92015-01-18 00:16:51 +0300213 { USB_DEVICE(0x13d3, 0x3423), .driver_info = BTUSB_ATH3012 },
Anantha Krishnanfa2f1392014-07-08 19:25:08 +0530214 { USB_DEVICE(0x13d3, 0x3432), .driver_info = BTUSB_ATH3012 },
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530215
Cho, Yu-Chene9036e32011-02-15 10:20:07 +0800216 /* Atheros AR5BBU12 with sflash firmware */
217 { USB_DEVICE(0x0489, 0xe02c), .driver_info = BTUSB_IGNORE },
218
Michael Gruetzner85d59722012-05-02 22:33:40 +0200219 /* Atheros AR5BBU12 with sflash firmware */
Yevgeniy Melnichukbc21fde2012-08-07 19:48:10 +0530220 { USB_DEVICE(0x0489, 0xe036), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200221 { USB_DEVICE(0x0489, 0xe03c), .driver_info = BTUSB_ATH3012 },
Michael Gruetzner85d59722012-05-02 22:33:40 +0200222
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +0000223 /* QCA ROME chipset */
Wen-chien Jesse Sung20541112015-05-13 11:39:25 +0800224 { USB_DEVICE(0x0cf3, 0xe007), .driver_info = BTUSB_QCA_ROME },
Marcel Holtmannc9e44472015-03-16 23:56:04 -0700225 { USB_DEVICE(0x0cf3, 0xe300), .driver_info = BTUSB_QCA_ROME },
226 { USB_DEVICE(0x0cf3, 0xe360), .driver_info = BTUSB_QCA_ROME },
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +0000227
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200228 /* Broadcom BCM2035 */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100229 { USB_DEVICE(0x0a5c, 0x2009), .driver_info = BTUSB_BCM92035 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200230 { USB_DEVICE(0x0a5c, 0x200a), .driver_info = BTUSB_WRONG_SCO_MTU },
231 { USB_DEVICE(0x0a5c, 0x2035), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200232
233 /* Broadcom BCM2045 */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100234 { USB_DEVICE(0x0a5c, 0x2039), .driver_info = BTUSB_WRONG_SCO_MTU },
235 { USB_DEVICE(0x0a5c, 0x2101), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmannbdbef3d2008-09-23 00:16:35 +0200236
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200237 /* IBM/Lenovo ThinkPad with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100238 { USB_DEVICE(0x0a5c, 0x201e), .driver_info = BTUSB_WRONG_SCO_MTU },
239 { USB_DEVICE(0x0a5c, 0x2110), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200240
241 /* HP laptop with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100242 { USB_DEVICE(0x03f0, 0x171d), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200243
244 /* Dell laptop with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100245 { USB_DEVICE(0x413c, 0x8126), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200246
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100247 /* Dell Wireless 370 and 410 devices */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100248 { USB_DEVICE(0x413c, 0x8152), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100249 { USB_DEVICE(0x413c, 0x8156), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200250
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100251 /* Belkin F8T012 and F8T013 devices */
252 { USB_DEVICE(0x050d, 0x0012), .driver_info = BTUSB_WRONG_SCO_MTU },
253 { USB_DEVICE(0x050d, 0x0013), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200254
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100255 /* Asus WL-BTD202 device */
256 { USB_DEVICE(0x0b05, 0x1715), .driver_info = BTUSB_WRONG_SCO_MTU },
257
258 /* Kensington Bluetooth USB adapter */
259 { USB_DEVICE(0x047d, 0x105e), .driver_info = BTUSB_WRONG_SCO_MTU },
260
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200261 /* RTX Telecom based adapters with buggy SCO support */
262 { USB_DEVICE(0x0400, 0x0807), .driver_info = BTUSB_BROKEN_ISOC },
263 { USB_DEVICE(0x0400, 0x080a), .driver_info = BTUSB_BROKEN_ISOC },
264
265 /* CONWISE Technology based adapters with buggy SCO support */
266 { USB_DEVICE(0x0e5e, 0x6622), .driver_info = BTUSB_BROKEN_ISOC },
267
Marcel Holtmann4fcef8e2015-01-01 17:34:37 -0800268 /* Roper Class 1 Bluetooth Dongle (Silicon Wave based) */
269 { USB_DEVICE(0x1300, 0x0001), .driver_info = BTUSB_SWAVE },
270
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200271 /* Digianswer devices */
272 { USB_DEVICE(0x08fd, 0x0001), .driver_info = BTUSB_DIGIANSWER },
273 { USB_DEVICE(0x08fd, 0x0002), .driver_info = BTUSB_IGNORE },
274
275 /* CSR BlueCore Bluetooth Sniffer */
Marcel Holtmann4f64fa82014-07-07 00:12:04 +0200276 { USB_DEVICE(0x0a12, 0x0002),
277 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200278
279 /* Frontline ComProbe Bluetooth Sniffer */
Marcel Holtmann4f64fa82014-07-07 00:12:04 +0200280 { USB_DEVICE(0x16d3, 0x0002),
281 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200282
Marcel Holtmanncb1ee892015-01-28 19:41:42 -0800283 /* Marvell Bluetooth devices */
284 { USB_DEVICE(0x1286, 0x2044), .driver_info = BTUSB_MARVELL },
285 { USB_DEVICE(0x1286, 0x2046), .driver_info = BTUSB_MARVELL },
286
Marcel Holtmannd0ac9eb2015-01-28 19:41:43 -0800287 /* Intel Bluetooth devices */
Marcel Holtmann407550f2015-02-22 15:41:18 -0800288 { USB_DEVICE(0x8087, 0x07da), .driver_info = BTUSB_CSR },
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -0700289 { USB_DEVICE(0x8087, 0x07dc), .driver_info = BTUSB_INTEL },
Tedd Ho-Jeong Anef4e5e42013-11-12 13:10:58 -0800290 { USB_DEVICE(0x8087, 0x0a2a), .driver_info = BTUSB_INTEL },
Marcel Holtmanncda0dd72015-01-26 21:33:48 -0800291 { USB_DEVICE(0x8087, 0x0a2b), .driver_info = BTUSB_INTEL_NEW },
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -0700292
Marcel Holtmannd0ac9eb2015-01-28 19:41:43 -0800293 /* Other Intel Bluetooth devices */
294 { USB_VENDOR_AND_INTERFACE_INFO(0x8087, 0xe0, 0x01, 0x01),
295 .driver_info = BTUSB_IGNORE },
Amitkumar Karwarae8df492014-07-18 14:47:06 -0700296
Daniel Drakea2698a92015-04-16 14:09:55 -0600297 /* Realtek Bluetooth devices */
298 { USB_VENDOR_AND_INTERFACE_INFO(0x0bda, 0xe0, 0x01, 0x01),
299 .driver_info = BTUSB_REALTEK },
300
301 /* Additional Realtek 8723AE Bluetooth devices */
302 { USB_DEVICE(0x0930, 0x021d), .driver_info = BTUSB_REALTEK },
303 { USB_DEVICE(0x13d3, 0x3394), .driver_info = BTUSB_REALTEK },
304
305 /* Additional Realtek 8723BE Bluetooth devices */
306 { USB_DEVICE(0x0489, 0xe085), .driver_info = BTUSB_REALTEK },
307 { USB_DEVICE(0x0489, 0xe08b), .driver_info = BTUSB_REALTEK },
308 { USB_DEVICE(0x13d3, 0x3410), .driver_info = BTUSB_REALTEK },
309 { USB_DEVICE(0x13d3, 0x3416), .driver_info = BTUSB_REALTEK },
310 { USB_DEVICE(0x13d3, 0x3459), .driver_info = BTUSB_REALTEK },
311
312 /* Additional Realtek 8821AE Bluetooth devices */
313 { USB_DEVICE(0x0b05, 0x17dc), .driver_info = BTUSB_REALTEK },
314 { USB_DEVICE(0x13d3, 0x3414), .driver_info = BTUSB_REALTEK },
315 { USB_DEVICE(0x13d3, 0x3458), .driver_info = BTUSB_REALTEK },
316 { USB_DEVICE(0x13d3, 0x3461), .driver_info = BTUSB_REALTEK },
317 { USB_DEVICE(0x13d3, 0x3462), .driver_info = BTUSB_REALTEK },
318
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200319 { } /* Terminating entry */
320};
321
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200322#define BTUSB_MAX_ISOC_FRAMES 10
323
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200324#define BTUSB_INTR_RUNNING 0
325#define BTUSB_BULK_RUNNING 1
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200326#define BTUSB_ISOC_RUNNING 2
Oliver Neukum7bee5492009-08-24 23:44:59 +0200327#define BTUSB_SUSPENDING 3
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -0300328#define BTUSB_DID_ISO_RESUME 4
Marcel Holtmanncda0dd72015-01-26 21:33:48 -0800329#define BTUSB_BOOTLOADER 5
330#define BTUSB_DOWNLOADING 6
Marcel Holtmannce6bb922015-01-28 01:58:40 -0800331#define BTUSB_FIRMWARE_LOADED 7
Marcel Holtmanncda0dd72015-01-26 21:33:48 -0800332#define BTUSB_FIRMWARE_FAILED 8
Marcel Holtmannce6bb922015-01-28 01:58:40 -0800333#define BTUSB_BOOTING 9
Daniel Drake04b8c812015-05-21 08:23:50 -0600334#define BTUSB_RESET_RESUME 10
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200335
336struct btusb_data {
337 struct hci_dev *hdev;
338 struct usb_device *udev;
Marcel Holtmann5fbcd262008-09-23 00:16:36 +0200339 struct usb_interface *intf;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200340 struct usb_interface *isoc;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200341
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200342 unsigned long flags;
343
344 struct work_struct work;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200345 struct work_struct waker;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200346
Marcel Holtmann803b5832014-09-16 08:00:29 +0200347 struct usb_anchor deferred;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200348 struct usb_anchor tx_anchor;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200349 int tx_in_flight;
350 spinlock_t txlock;
351
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200352 struct usb_anchor intr_anchor;
353 struct usb_anchor bulk_anchor;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200354 struct usb_anchor isoc_anchor;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200355 spinlock_t rxlock;
356
357 struct sk_buff *evt_skb;
358 struct sk_buff *acl_skb;
359 struct sk_buff *sco_skb;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200360
361 struct usb_endpoint_descriptor *intr_ep;
362 struct usb_endpoint_descriptor *bulk_tx_ep;
363 struct usb_endpoint_descriptor *bulk_rx_ep;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200364 struct usb_endpoint_descriptor *isoc_tx_ep;
365 struct usb_endpoint_descriptor *isoc_rx_ep;
366
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100367 __u8 cmdreq_type;
Marcel Holtmann893ba542015-01-28 20:27:34 -0800368 __u8 cmdreq;
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100369
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100370 unsigned int sco_num;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200371 int isoc_altsetting;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +0100372 int suspend_count;
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +0100373
Marcel Holtmann97307f52015-01-12 13:51:10 -0800374 int (*recv_event)(struct hci_dev *hdev, struct sk_buff *skb);
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +0100375 int (*recv_bulk)(struct btusb_data *data, void *buffer, int count);
Kim, Ben Young Taeace31982015-02-15 23:06:14 +0000376
377 int (*setup_on_usb)(struct hci_dev *hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200378};
379
Marcel Holtmann803b5832014-09-16 08:00:29 +0200380static inline void btusb_free_frags(struct btusb_data *data)
381{
382 unsigned long flags;
383
384 spin_lock_irqsave(&data->rxlock, flags);
385
386 kfree_skb(data->evt_skb);
387 data->evt_skb = NULL;
388
389 kfree_skb(data->acl_skb);
390 data->acl_skb = NULL;
391
392 kfree_skb(data->sco_skb);
393 data->sco_skb = NULL;
394
395 spin_unlock_irqrestore(&data->rxlock, flags);
396}
397
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200398static int btusb_recv_intr(struct btusb_data *data, void *buffer, int count)
399{
Marcel Holtmann803b5832014-09-16 08:00:29 +0200400 struct sk_buff *skb;
401 int err = 0;
402
403 spin_lock(&data->rxlock);
404 skb = data->evt_skb;
405
406 while (count) {
407 int len;
408
409 if (!skb) {
410 skb = bt_skb_alloc(HCI_MAX_EVENT_SIZE, GFP_ATOMIC);
411 if (!skb) {
412 err = -ENOMEM;
413 break;
414 }
415
416 bt_cb(skb)->pkt_type = HCI_EVENT_PKT;
417 bt_cb(skb)->expect = HCI_EVENT_HDR_SIZE;
418 }
419
420 len = min_t(uint, bt_cb(skb)->expect, count);
421 memcpy(skb_put(skb, len), buffer, len);
422
423 count -= len;
424 buffer += len;
425 bt_cb(skb)->expect -= len;
426
427 if (skb->len == HCI_EVENT_HDR_SIZE) {
428 /* Complete event header */
429 bt_cb(skb)->expect = hci_event_hdr(skb)->plen;
430
431 if (skb_tailroom(skb) < bt_cb(skb)->expect) {
432 kfree_skb(skb);
433 skb = NULL;
434
435 err = -EILSEQ;
436 break;
437 }
438 }
439
440 if (bt_cb(skb)->expect == 0) {
441 /* Complete frame */
Marcel Holtmann97307f52015-01-12 13:51:10 -0800442 data->recv_event(data->hdev, skb);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200443 skb = NULL;
444 }
445 }
446
447 data->evt_skb = skb;
448 spin_unlock(&data->rxlock);
449
450 return err;
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200451}
452
453static int btusb_recv_bulk(struct btusb_data *data, void *buffer, int count)
454{
Marcel Holtmann803b5832014-09-16 08:00:29 +0200455 struct sk_buff *skb;
456 int err = 0;
457
458 spin_lock(&data->rxlock);
459 skb = data->acl_skb;
460
461 while (count) {
462 int len;
463
464 if (!skb) {
465 skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
466 if (!skb) {
467 err = -ENOMEM;
468 break;
469 }
470
471 bt_cb(skb)->pkt_type = HCI_ACLDATA_PKT;
472 bt_cb(skb)->expect = HCI_ACL_HDR_SIZE;
473 }
474
475 len = min_t(uint, bt_cb(skb)->expect, count);
476 memcpy(skb_put(skb, len), buffer, len);
477
478 count -= len;
479 buffer += len;
480 bt_cb(skb)->expect -= len;
481
482 if (skb->len == HCI_ACL_HDR_SIZE) {
483 __le16 dlen = hci_acl_hdr(skb)->dlen;
484
485 /* Complete ACL header */
486 bt_cb(skb)->expect = __le16_to_cpu(dlen);
487
488 if (skb_tailroom(skb) < bt_cb(skb)->expect) {
489 kfree_skb(skb);
490 skb = NULL;
491
492 err = -EILSEQ;
493 break;
494 }
495 }
496
497 if (bt_cb(skb)->expect == 0) {
498 /* Complete frame */
499 hci_recv_frame(data->hdev, skb);
500 skb = NULL;
501 }
502 }
503
504 data->acl_skb = skb;
505 spin_unlock(&data->rxlock);
506
507 return err;
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200508}
509
510static int btusb_recv_isoc(struct btusb_data *data, void *buffer, int count)
511{
Marcel Holtmann803b5832014-09-16 08:00:29 +0200512 struct sk_buff *skb;
513 int err = 0;
514
515 spin_lock(&data->rxlock);
516 skb = data->sco_skb;
517
518 while (count) {
519 int len;
520
521 if (!skb) {
522 skb = bt_skb_alloc(HCI_MAX_SCO_SIZE, GFP_ATOMIC);
523 if (!skb) {
524 err = -ENOMEM;
525 break;
526 }
527
528 bt_cb(skb)->pkt_type = HCI_SCODATA_PKT;
529 bt_cb(skb)->expect = HCI_SCO_HDR_SIZE;
530 }
531
532 len = min_t(uint, bt_cb(skb)->expect, count);
533 memcpy(skb_put(skb, len), buffer, len);
534
535 count -= len;
536 buffer += len;
537 bt_cb(skb)->expect -= len;
538
539 if (skb->len == HCI_SCO_HDR_SIZE) {
540 /* Complete SCO header */
541 bt_cb(skb)->expect = hci_sco_hdr(skb)->dlen;
542
543 if (skb_tailroom(skb) < bt_cb(skb)->expect) {
544 kfree_skb(skb);
545 skb = NULL;
546
547 err = -EILSEQ;
548 break;
549 }
550 }
551
552 if (bt_cb(skb)->expect == 0) {
553 /* Complete frame */
554 hci_recv_frame(data->hdev, skb);
555 skb = NULL;
556 }
557 }
558
559 data->sco_skb = skb;
560 spin_unlock(&data->rxlock);
561
562 return err;
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200563}
564
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200565static void btusb_intr_complete(struct urb *urb)
566{
567 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +0100568 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200569 int err;
570
Marcel Holtmann89e75332014-09-16 04:44:50 +0200571 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
572 urb->actual_length);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200573
574 if (!test_bit(HCI_RUNNING, &hdev->flags))
575 return;
576
577 if (urb->status == 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200578 hdev->stat.byte_rx += urb->actual_length;
579
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200580 if (btusb_recv_intr(data, urb->transfer_buffer,
581 urb->actual_length) < 0) {
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200582 BT_ERR("%s corrupted event packet", hdev->name);
583 hdev->stat.err_rx++;
584 }
Champion Chen85560c42014-09-06 14:06:08 -0500585 } else if (urb->status == -ENOENT) {
586 /* Avoid suspend failed when usb_kill_urb */
587 return;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200588 }
589
590 if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
591 return;
592
Oliver Neukum7bee5492009-08-24 23:44:59 +0200593 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200594 usb_anchor_urb(urb, &data->intr_anchor);
595
596 err = usb_submit_urb(urb, GFP_ATOMIC);
597 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200598 /* -EPERM: urb is being killed;
599 * -ENODEV: device got disconnected */
600 if (err != -EPERM && err != -ENODEV)
Stefan Seyfried61faddf2010-11-30 21:49:08 +0100601 BT_ERR("%s urb %p failed to resubmit (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200602 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200603 usb_unanchor_urb(urb);
604 }
605}
606
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100607static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200608{
David Herrmann155961e2012-02-09 21:58:32 +0100609 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200610 struct urb *urb;
611 unsigned char *buf;
612 unsigned int pipe;
613 int err, size;
614
615 BT_DBG("%s", hdev->name);
616
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200617 if (!data->intr_ep)
618 return -ENODEV;
619
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100620 urb = usb_alloc_urb(0, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200621 if (!urb)
622 return -ENOMEM;
623
624 size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
625
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100626 buf = kmalloc(size, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200627 if (!buf) {
628 usb_free_urb(urb);
629 return -ENOMEM;
630 }
631
632 pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
633
634 usb_fill_int_urb(urb, data->udev, pipe, buf, size,
Marcel Holtmann89e75332014-09-16 04:44:50 +0200635 btusb_intr_complete, hdev, data->intr_ep->bInterval);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200636
637 urb->transfer_flags |= URB_FREE_BUFFER;
638
639 usb_anchor_urb(urb, &data->intr_anchor);
640
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100641 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200642 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +0200643 if (err != -EPERM && err != -ENODEV)
644 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200645 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200646 usb_unanchor_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200647 }
648
649 usb_free_urb(urb);
650
651 return err;
652}
653
654static void btusb_bulk_complete(struct urb *urb)
655{
656 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +0100657 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200658 int err;
659
Marcel Holtmann89e75332014-09-16 04:44:50 +0200660 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
661 urb->actual_length);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200662
663 if (!test_bit(HCI_RUNNING, &hdev->flags))
664 return;
665
666 if (urb->status == 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200667 hdev->stat.byte_rx += urb->actual_length;
668
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +0100669 if (data->recv_bulk(data, urb->transfer_buffer,
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200670 urb->actual_length) < 0) {
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200671 BT_ERR("%s corrupted ACL packet", hdev->name);
672 hdev->stat.err_rx++;
673 }
Champion Chen85560c42014-09-06 14:06:08 -0500674 } else if (urb->status == -ENOENT) {
675 /* Avoid suspend failed when usb_kill_urb */
676 return;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200677 }
678
679 if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
680 return;
681
682 usb_anchor_urb(urb, &data->bulk_anchor);
Oliver Neukum652fd782009-12-16 19:23:43 +0100683 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200684
685 err = usb_submit_urb(urb, GFP_ATOMIC);
686 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200687 /* -EPERM: urb is being killed;
688 * -ENODEV: device got disconnected */
689 if (err != -EPERM && err != -ENODEV)
Stefan Seyfried61faddf2010-11-30 21:49:08 +0100690 BT_ERR("%s urb %p failed to resubmit (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200691 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200692 usb_unanchor_urb(urb);
693 }
694}
695
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100696static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200697{
David Herrmann155961e2012-02-09 21:58:32 +0100698 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200699 struct urb *urb;
700 unsigned char *buf;
701 unsigned int pipe;
Vikram Kandukuri290ba202009-07-02 14:31:59 +0530702 int err, size = HCI_MAX_FRAME_SIZE;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200703
704 BT_DBG("%s", hdev->name);
705
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200706 if (!data->bulk_rx_ep)
707 return -ENODEV;
708
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100709 urb = usb_alloc_urb(0, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200710 if (!urb)
711 return -ENOMEM;
712
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100713 buf = kmalloc(size, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200714 if (!buf) {
715 usb_free_urb(urb);
716 return -ENOMEM;
717 }
718
719 pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
720
Marcel Holtmann89e75332014-09-16 04:44:50 +0200721 usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
722 btusb_bulk_complete, hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200723
724 urb->transfer_flags |= URB_FREE_BUFFER;
725
Oliver Neukum7bee5492009-08-24 23:44:59 +0200726 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200727 usb_anchor_urb(urb, &data->bulk_anchor);
728
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100729 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200730 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +0200731 if (err != -EPERM && err != -ENODEV)
732 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200733 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200734 usb_unanchor_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200735 }
736
737 usb_free_urb(urb);
738
739 return err;
740}
741
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200742static void btusb_isoc_complete(struct urb *urb)
743{
744 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +0100745 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200746 int i, err;
747
Marcel Holtmann89e75332014-09-16 04:44:50 +0200748 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
749 urb->actual_length);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200750
751 if (!test_bit(HCI_RUNNING, &hdev->flags))
752 return;
753
754 if (urb->status == 0) {
755 for (i = 0; i < urb->number_of_packets; i++) {
756 unsigned int offset = urb->iso_frame_desc[i].offset;
757 unsigned int length = urb->iso_frame_desc[i].actual_length;
758
759 if (urb->iso_frame_desc[i].status)
760 continue;
761
762 hdev->stat.byte_rx += length;
763
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200764 if (btusb_recv_isoc(data, urb->transfer_buffer + offset,
765 length) < 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200766 BT_ERR("%s corrupted SCO packet", hdev->name);
767 hdev->stat.err_rx++;
768 }
769 }
Champion Chen85560c42014-09-06 14:06:08 -0500770 } else if (urb->status == -ENOENT) {
771 /* Avoid suspend failed when usb_kill_urb */
772 return;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200773 }
774
775 if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags))
776 return;
777
778 usb_anchor_urb(urb, &data->isoc_anchor);
779
780 err = usb_submit_urb(urb, GFP_ATOMIC);
781 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200782 /* -EPERM: urb is being killed;
783 * -ENODEV: device got disconnected */
784 if (err != -EPERM && err != -ENODEV)
Stefan Seyfried61faddf2010-11-30 21:49:08 +0100785 BT_ERR("%s urb %p failed to resubmit (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200786 hdev->name, urb, -err);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200787 usb_unanchor_urb(urb);
788 }
789}
790
Jesper Juhl42b16b32011-01-17 00:09:38 +0100791static inline void __fill_isoc_descriptor(struct urb *urb, int len, int mtu)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200792{
793 int i, offset = 0;
794
795 BT_DBG("len %d mtu %d", len, mtu);
796
797 for (i = 0; i < BTUSB_MAX_ISOC_FRAMES && len >= mtu;
798 i++, offset += mtu, len -= mtu) {
799 urb->iso_frame_desc[i].offset = offset;
800 urb->iso_frame_desc[i].length = mtu;
801 }
802
803 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
804 urb->iso_frame_desc[i].offset = offset;
805 urb->iso_frame_desc[i].length = len;
806 i++;
807 }
808
809 urb->number_of_packets = i;
810}
811
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100812static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200813{
David Herrmann155961e2012-02-09 21:58:32 +0100814 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200815 struct urb *urb;
816 unsigned char *buf;
817 unsigned int pipe;
818 int err, size;
819
820 BT_DBG("%s", hdev->name);
821
822 if (!data->isoc_rx_ep)
823 return -ENODEV;
824
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100825 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200826 if (!urb)
827 return -ENOMEM;
828
829 size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) *
830 BTUSB_MAX_ISOC_FRAMES;
831
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100832 buf = kmalloc(size, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200833 if (!buf) {
834 usb_free_urb(urb);
835 return -ENOMEM;
836 }
837
838 pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress);
839
Bing Zhaofa0fb932011-12-20 18:19:00 -0800840 usb_fill_int_urb(urb, data->udev, pipe, buf, size, btusb_isoc_complete,
Marcel Holtmann89e75332014-09-16 04:44:50 +0200841 hdev, data->isoc_rx_ep->bInterval);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200842
Marcel Holtmann89e75332014-09-16 04:44:50 +0200843 urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200844
845 __fill_isoc_descriptor(urb, size,
Marcel Holtmann89e75332014-09-16 04:44:50 +0200846 le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize));
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200847
848 usb_anchor_urb(urb, &data->isoc_anchor);
849
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100850 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200851 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +0200852 if (err != -EPERM && err != -ENODEV)
853 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200854 hdev->name, urb, -err);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200855 usb_unanchor_urb(urb);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200856 }
857
858 usb_free_urb(urb);
859
860 return err;
861}
862
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200863static void btusb_tx_complete(struct urb *urb)
864{
865 struct sk_buff *skb = urb->context;
Marcel Holtmann89e75332014-09-16 04:44:50 +0200866 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
David Herrmann155961e2012-02-09 21:58:32 +0100867 struct btusb_data *data = hci_get_drvdata(hdev);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200868
Marcel Holtmann89e75332014-09-16 04:44:50 +0200869 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
870 urb->actual_length);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200871
872 if (!test_bit(HCI_RUNNING, &hdev->flags))
873 goto done;
874
875 if (!urb->status)
876 hdev->stat.byte_tx += urb->transfer_buffer_length;
877 else
878 hdev->stat.err_tx++;
879
880done:
881 spin_lock(&data->txlock);
882 data->tx_in_flight--;
883 spin_unlock(&data->txlock);
884
885 kfree(urb->setup_packet);
886
887 kfree_skb(skb);
888}
889
890static void btusb_isoc_tx_complete(struct urb *urb)
891{
892 struct sk_buff *skb = urb->context;
Marcel Holtmann89e75332014-09-16 04:44:50 +0200893 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200894
Marcel Holtmann89e75332014-09-16 04:44:50 +0200895 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
896 urb->actual_length);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200897
898 if (!test_bit(HCI_RUNNING, &hdev->flags))
899 goto done;
900
901 if (!urb->status)
902 hdev->stat.byte_tx += urb->transfer_buffer_length;
903 else
904 hdev->stat.err_tx++;
905
906done:
907 kfree(urb->setup_packet);
908
909 kfree_skb(skb);
910}
911
912static int btusb_open(struct hci_dev *hdev)
913{
David Herrmann155961e2012-02-09 21:58:32 +0100914 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200915 int err;
916
917 BT_DBG("%s", hdev->name);
918
Kim, Ben Young Taeace31982015-02-15 23:06:14 +0000919 /* Patching USB firmware files prior to starting any URBs of HCI path
920 * It is more safe to use USB bulk channel for downloading USB patch
921 */
922 if (data->setup_on_usb) {
923 err = data->setup_on_usb(hdev);
Marcel Holtmanneb500422015-04-16 23:15:50 +0200924 if (err < 0)
Kim, Ben Young Taeace31982015-02-15 23:06:14 +0000925 return err;
926 }
927
Oliver Neukum7bee5492009-08-24 23:44:59 +0200928 err = usb_autopm_get_interface(data->intf);
929 if (err < 0)
930 return err;
931
932 data->intf->needs_remote_wakeup = 1;
933
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200934 if (test_and_set_bit(HCI_RUNNING, &hdev->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +0200935 goto done;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200936
937 if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +0200938 goto done;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200939
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100940 err = btusb_submit_intr_urb(hdev, GFP_KERNEL);
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100941 if (err < 0)
942 goto failed;
943
944 err = btusb_submit_bulk_urb(hdev, GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200945 if (err < 0) {
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100946 usb_kill_anchored_urbs(&data->intr_anchor);
947 goto failed;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200948 }
949
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100950 set_bit(BTUSB_BULK_RUNNING, &data->flags);
951 btusb_submit_bulk_urb(hdev, GFP_KERNEL);
952
Oliver Neukum7bee5492009-08-24 23:44:59 +0200953done:
954 usb_autopm_put_interface(data->intf);
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100955 return 0;
956
957failed:
958 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
959 clear_bit(HCI_RUNNING, &hdev->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200960 usb_autopm_put_interface(data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200961 return err;
962}
963
Oliver Neukum7bee5492009-08-24 23:44:59 +0200964static void btusb_stop_traffic(struct btusb_data *data)
965{
966 usb_kill_anchored_urbs(&data->intr_anchor);
967 usb_kill_anchored_urbs(&data->bulk_anchor);
968 usb_kill_anchored_urbs(&data->isoc_anchor);
969}
970
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200971static int btusb_close(struct hci_dev *hdev)
972{
David Herrmann155961e2012-02-09 21:58:32 +0100973 struct btusb_data *data = hci_get_drvdata(hdev);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200974 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200975
976 BT_DBG("%s", hdev->name);
977
978 if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
979 return 0;
980
Marcel Holtmanne8c3c3d2008-09-23 00:16:36 +0200981 cancel_work_sync(&data->work);
Linus Torvalds404291a2009-11-11 13:32:29 -0800982 cancel_work_sync(&data->waker);
Marcel Holtmanne8c3c3d2008-09-23 00:16:36 +0200983
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200984 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200985 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200986 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200987
988 btusb_stop_traffic(data);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200989 btusb_free_frags(data);
990
Oliver Neukum7bee5492009-08-24 23:44:59 +0200991 err = usb_autopm_get_interface(data->intf);
992 if (err < 0)
Oliver Neukum7b8e2c12009-11-13 14:26:23 +0100993 goto failed;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200994
995 data->intf->needs_remote_wakeup = 0;
996 usb_autopm_put_interface(data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200997
Oliver Neukum7b8e2c12009-11-13 14:26:23 +0100998failed:
999 usb_scuttle_anchored_urbs(&data->deferred);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001000 return 0;
1001}
1002
1003static int btusb_flush(struct hci_dev *hdev)
1004{
David Herrmann155961e2012-02-09 21:58:32 +01001005 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001006
1007 BT_DBG("%s", hdev->name);
1008
1009 usb_kill_anchored_urbs(&data->tx_anchor);
Marcel Holtmann803b5832014-09-16 08:00:29 +02001010 btusb_free_frags(data);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001011
1012 return 0;
1013}
1014
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001015static struct urb *alloc_ctrl_urb(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001016{
David Herrmann155961e2012-02-09 21:58:32 +01001017 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001018 struct usb_ctrlrequest *dr;
1019 struct urb *urb;
1020 unsigned int pipe;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001021
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001022 urb = usb_alloc_urb(0, GFP_KERNEL);
1023 if (!urb)
1024 return ERR_PTR(-ENOMEM);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001025
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001026 dr = kmalloc(sizeof(*dr), GFP_KERNEL);
1027 if (!dr) {
1028 usb_free_urb(urb);
1029 return ERR_PTR(-ENOMEM);
1030 }
1031
1032 dr->bRequestType = data->cmdreq_type;
Marcel Holtmann893ba542015-01-28 20:27:34 -08001033 dr->bRequest = data->cmdreq;
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001034 dr->wIndex = 0;
1035 dr->wValue = 0;
1036 dr->wLength = __cpu_to_le16(skb->len);
1037
1038 pipe = usb_sndctrlpipe(data->udev, 0x00);
1039
Marcel Holtmann89e75332014-09-16 04:44:50 +02001040 usb_fill_control_urb(urb, data->udev, pipe, (void *)dr,
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001041 skb->data, skb->len, btusb_tx_complete, skb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001042
Marcel Holtmann89e75332014-09-16 04:44:50 +02001043 skb->dev = (void *)hdev;
Marcel Holtmann7bd8f092013-10-11 06:19:18 -07001044
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001045 return urb;
1046}
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001047
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001048static struct urb *alloc_bulk_urb(struct hci_dev *hdev, struct sk_buff *skb)
1049{
1050 struct btusb_data *data = hci_get_drvdata(hdev);
1051 struct urb *urb;
1052 unsigned int pipe;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001053
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001054 if (!data->bulk_tx_ep)
1055 return ERR_PTR(-ENODEV);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001056
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001057 urb = usb_alloc_urb(0, GFP_KERNEL);
1058 if (!urb)
1059 return ERR_PTR(-ENOMEM);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001060
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001061 pipe = usb_sndbulkpipe(data->udev, data->bulk_tx_ep->bEndpointAddress);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001062
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001063 usb_fill_bulk_urb(urb, data->udev, pipe,
1064 skb->data, skb->len, btusb_tx_complete, skb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +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 Holtmann5e23b922007-10-20 14:12:34 +02001070
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001071static struct urb *alloc_isoc_urb(struct hci_dev *hdev, struct sk_buff *skb)
1072{
1073 struct btusb_data *data = hci_get_drvdata(hdev);
1074 struct urb *urb;
1075 unsigned int pipe;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001076
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001077 if (!data->isoc_tx_ep)
1078 return ERR_PTR(-ENODEV);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001079
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001080 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_KERNEL);
1081 if (!urb)
1082 return ERR_PTR(-ENOMEM);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001083
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001084 pipe = usb_sndisocpipe(data->udev, data->isoc_tx_ep->bEndpointAddress);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001085
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001086 usb_fill_int_urb(urb, data->udev, pipe,
1087 skb->data, skb->len, btusb_isoc_tx_complete,
1088 skb, data->isoc_tx_ep->bInterval);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001089
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001090 urb->transfer_flags = URB_ISO_ASAP;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001091
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001092 __fill_isoc_descriptor(urb, skb->len,
1093 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001094
Marcel Holtmann89e75332014-09-16 04:44:50 +02001095 skb->dev = (void *)hdev;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001096
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001097 return urb;
1098}
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001099
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001100static int submit_tx_urb(struct hci_dev *hdev, struct urb *urb)
1101{
1102 struct btusb_data *data = hci_get_drvdata(hdev);
1103 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001104
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001105 usb_anchor_urb(urb, &data->tx_anchor);
1106
Johan Hedberge9753ef2014-09-14 08:49:34 +03001107 err = usb_submit_urb(urb, GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001108 if (err < 0) {
Paul Bolle5a9b80e2011-10-09 12:12:16 +02001109 if (err != -EPERM && err != -ENODEV)
1110 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +02001111 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001112 kfree(urb->setup_packet);
1113 usb_unanchor_urb(urb);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001114 } else {
1115 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001116 }
1117
Cong Wang54a8a792011-11-22 09:32:57 +08001118 usb_free_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001119 return err;
1120}
1121
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001122static int submit_or_queue_tx_urb(struct hci_dev *hdev, struct urb *urb)
1123{
1124 struct btusb_data *data = hci_get_drvdata(hdev);
1125 unsigned long flags;
1126 bool suspending;
1127
1128 spin_lock_irqsave(&data->txlock, flags);
1129 suspending = test_bit(BTUSB_SUSPENDING, &data->flags);
1130 if (!suspending)
1131 data->tx_in_flight++;
1132 spin_unlock_irqrestore(&data->txlock, flags);
1133
1134 if (!suspending)
1135 return submit_tx_urb(hdev, urb);
1136
1137 usb_anchor_urb(urb, &data->deferred);
1138 schedule_work(&data->waker);
1139
1140 usb_free_urb(urb);
1141 return 0;
1142}
1143
1144static int btusb_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
1145{
1146 struct urb *urb;
1147
1148 BT_DBG("%s", hdev->name);
1149
1150 if (!test_bit(HCI_RUNNING, &hdev->flags))
1151 return -EBUSY;
1152
1153 switch (bt_cb(skb)->pkt_type) {
1154 case HCI_COMMAND_PKT:
1155 urb = alloc_ctrl_urb(hdev, skb);
1156 if (IS_ERR(urb))
1157 return PTR_ERR(urb);
1158
1159 hdev->stat.cmd_tx++;
1160 return submit_or_queue_tx_urb(hdev, urb);
1161
1162 case HCI_ACLDATA_PKT:
1163 urb = alloc_bulk_urb(hdev, skb);
1164 if (IS_ERR(urb))
1165 return PTR_ERR(urb);
1166
1167 hdev->stat.acl_tx++;
1168 return submit_or_queue_tx_urb(hdev, urb);
1169
1170 case HCI_SCODATA_PKT:
1171 if (hci_conn_num(hdev, SCO_LINK) < 1)
1172 return -ENODEV;
1173
1174 urb = alloc_isoc_urb(hdev, skb);
1175 if (IS_ERR(urb))
1176 return PTR_ERR(urb);
1177
1178 hdev->stat.sco_tx++;
1179 return submit_tx_urb(hdev, urb);
1180 }
1181
1182 return -EILSEQ;
1183}
1184
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001185static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
1186{
David Herrmann155961e2012-02-09 21:58:32 +01001187 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001188
1189 BT_DBG("%s evt %d", hdev->name, evt);
1190
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001191 if (hci_conn_num(hdev, SCO_LINK) != data->sco_num) {
1192 data->sco_num = hci_conn_num(hdev, SCO_LINK);
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001193 schedule_work(&data->work);
Marcel Holtmanna780efa2008-11-30 12:17:12 +01001194 }
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001195}
1196
Jesper Juhl42b16b32011-01-17 00:09:38 +01001197static inline int __set_isoc_interface(struct hci_dev *hdev, int altsetting)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001198{
David Herrmann155961e2012-02-09 21:58:32 +01001199 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001200 struct usb_interface *intf = data->isoc;
1201 struct usb_endpoint_descriptor *ep_desc;
1202 int i, err;
1203
1204 if (!data->isoc)
1205 return -ENODEV;
1206
1207 err = usb_set_interface(data->udev, 1, altsetting);
1208 if (err < 0) {
1209 BT_ERR("%s setting interface failed (%d)", hdev->name, -err);
1210 return err;
1211 }
1212
1213 data->isoc_altsetting = altsetting;
1214
1215 data->isoc_tx_ep = NULL;
1216 data->isoc_rx_ep = NULL;
1217
1218 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
1219 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
1220
1221 if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) {
1222 data->isoc_tx_ep = ep_desc;
1223 continue;
1224 }
1225
1226 if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) {
1227 data->isoc_rx_ep = ep_desc;
1228 continue;
1229 }
1230 }
1231
1232 if (!data->isoc_tx_ep || !data->isoc_rx_ep) {
1233 BT_ERR("%s invalid SCO descriptors", hdev->name);
1234 return -ENODEV;
1235 }
1236
1237 return 0;
1238}
1239
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001240static void btusb_work(struct work_struct *work)
1241{
1242 struct btusb_data *data = container_of(work, struct btusb_data, work);
1243 struct hci_dev *hdev = data->hdev;
Mikel Astizf4001d22012-04-11 08:48:51 +02001244 int new_alts;
Oliver Neukum7bee5492009-08-24 23:44:59 +02001245 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001246
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001247 if (data->sco_num > 0) {
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -03001248 if (!test_bit(BTUSB_DID_ISO_RESUME, &data->flags)) {
Oliver Neukum8efdd0c2011-02-11 13:00:06 +01001249 err = usb_autopm_get_interface(data->isoc ? data->isoc : data->intf);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001250 if (err < 0) {
1251 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1252 usb_kill_anchored_urbs(&data->isoc_anchor);
1253 return;
1254 }
1255
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -03001256 set_bit(BTUSB_DID_ISO_RESUME, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001257 }
Mikel Astizf4001d22012-04-11 08:48:51 +02001258
1259 if (hdev->voice_setting & 0x0020) {
1260 static const int alts[3] = { 2, 4, 5 };
Marcel Holtmann89e75332014-09-16 04:44:50 +02001261
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001262 new_alts = alts[data->sco_num - 1];
Mikel Astizf4001d22012-04-11 08:48:51 +02001263 } else {
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001264 new_alts = data->sco_num;
Mikel Astizf4001d22012-04-11 08:48:51 +02001265 }
1266
1267 if (data->isoc_altsetting != new_alts) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001268 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1269 usb_kill_anchored_urbs(&data->isoc_anchor);
1270
Mikel Astizf4001d22012-04-11 08:48:51 +02001271 if (__set_isoc_interface(hdev, new_alts) < 0)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001272 return;
1273 }
1274
1275 if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001276 if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001277 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1278 else
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001279 btusb_submit_isoc_urb(hdev, GFP_KERNEL);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001280 }
1281 } else {
1282 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1283 usb_kill_anchored_urbs(&data->isoc_anchor);
1284
1285 __set_isoc_interface(hdev, 0);
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -03001286 if (test_and_clear_bit(BTUSB_DID_ISO_RESUME, &data->flags))
Oliver Neukum8efdd0c2011-02-11 13:00:06 +01001287 usb_autopm_put_interface(data->isoc ? data->isoc : data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001288 }
1289}
1290
Oliver Neukum7bee5492009-08-24 23:44:59 +02001291static void btusb_waker(struct work_struct *work)
1292{
1293 struct btusb_data *data = container_of(work, struct btusb_data, waker);
1294 int err;
1295
1296 err = usb_autopm_get_interface(data->intf);
1297 if (err < 0)
1298 return;
1299
1300 usb_autopm_put_interface(data->intf);
1301}
1302
Daniel Drake6576fe42015-02-23 18:16:47 -06001303static struct sk_buff *btusb_read_local_version(struct hci_dev *hdev)
1304{
1305 struct sk_buff *skb;
1306
1307 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
1308 HCI_INIT_TIMEOUT);
1309 if (IS_ERR(skb)) {
1310 BT_ERR("%s: HCI_OP_READ_LOCAL_VERSION failed (%ld)",
1311 hdev->name, PTR_ERR(skb));
1312 return skb;
1313 }
1314
1315 if (skb->len != sizeof(struct hci_rp_read_local_version)) {
1316 BT_ERR("%s: HCI_OP_READ_LOCAL_VERSION event length mismatch",
1317 hdev->name);
1318 kfree_skb(skb);
1319 return ERR_PTR(-EIO);
1320 }
1321
1322 return skb;
1323}
1324
Marcel Holtmann9f8f9622013-04-10 08:11:35 -07001325static int btusb_setup_bcm92035(struct hci_dev *hdev)
1326{
1327 struct sk_buff *skb;
1328 u8 val = 0x00;
1329
1330 BT_DBG("%s", hdev->name);
1331
1332 skb = __hci_cmd_sync(hdev, 0xfc3b, 1, &val, HCI_INIT_TIMEOUT);
1333 if (IS_ERR(skb))
1334 BT_ERR("BCM92035 command failed (%ld)", -PTR_ERR(skb));
1335 else
1336 kfree_skb(skb);
1337
1338 return 0;
1339}
1340
Marcel Holtmann81cac642014-01-03 03:02:36 -08001341static int btusb_setup_csr(struct hci_dev *hdev)
1342{
1343 struct hci_rp_read_local_version *rp;
1344 struct sk_buff *skb;
1345 int ret;
1346
1347 BT_DBG("%s", hdev->name);
1348
Daniel Drake6576fe42015-02-23 18:16:47 -06001349 skb = btusb_read_local_version(hdev);
1350 if (IS_ERR(skb))
Marcel Holtmann81cac642014-01-03 03:02:36 -08001351 return -PTR_ERR(skb);
Marcel Holtmann81cac642014-01-03 03:02:36 -08001352
Marcel Holtmann89e75332014-09-16 04:44:50 +02001353 rp = (struct hci_rp_read_local_version *)skb->data;
Marcel Holtmann81cac642014-01-03 03:02:36 -08001354
1355 if (!rp->status) {
1356 if (le16_to_cpu(rp->manufacturer) != 10) {
1357 /* Clear the reset quirk since this is not an actual
1358 * early Bluetooth 1.1 device from CSR.
1359 */
1360 clear_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
1361
1362 /* These fake CSR controllers have all a broken
1363 * stored link key handling and so just disable it.
1364 */
1365 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY,
1366 &hdev->quirks);
1367 }
1368 }
1369
1370 ret = -bt_to_errno(rp->status);
1371
1372 kfree_skb(skb);
1373
1374 return ret;
1375}
1376
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001377static const struct firmware *btusb_setup_intel_get_fw(struct hci_dev *hdev,
Marcel Holtmann89e75332014-09-16 04:44:50 +02001378 struct intel_version *ver)
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001379{
1380 const struct firmware *fw;
1381 char fwname[64];
1382 int ret;
1383
1384 snprintf(fwname, sizeof(fwname),
1385 "intel/ibt-hw-%x.%x.%x-fw-%x.%x.%x.%x.%x.bseq",
1386 ver->hw_platform, ver->hw_variant, ver->hw_revision,
1387 ver->fw_variant, ver->fw_revision, ver->fw_build_num,
1388 ver->fw_build_ww, ver->fw_build_yy);
1389
1390 ret = request_firmware(&fw, fwname, &hdev->dev);
1391 if (ret < 0) {
1392 if (ret == -EINVAL) {
1393 BT_ERR("%s Intel firmware file request failed (%d)",
1394 hdev->name, ret);
1395 return NULL;
1396 }
1397
1398 BT_ERR("%s failed to open Intel firmware file: %s(%d)",
1399 hdev->name, fwname, ret);
1400
1401 /* If the correct firmware patch file is not found, use the
1402 * default firmware patch file instead
1403 */
1404 snprintf(fwname, sizeof(fwname), "intel/ibt-hw-%x.%x.bseq",
1405 ver->hw_platform, ver->hw_variant);
1406 if (request_firmware(&fw, fwname, &hdev->dev) < 0) {
1407 BT_ERR("%s failed to open default Intel fw file: %s",
1408 hdev->name, fwname);
1409 return NULL;
1410 }
1411 }
1412
1413 BT_INFO("%s: Intel Bluetooth firmware file: %s", hdev->name, fwname);
1414
1415 return fw;
1416}
1417
1418static int btusb_setup_intel_patching(struct hci_dev *hdev,
1419 const struct firmware *fw,
1420 const u8 **fw_ptr, int *disable_patch)
1421{
1422 struct sk_buff *skb;
1423 struct hci_command_hdr *cmd;
1424 const u8 *cmd_param;
1425 struct hci_event_hdr *evt = NULL;
1426 const u8 *evt_param = NULL;
1427 int remain = fw->size - (*fw_ptr - fw->data);
1428
1429 /* The first byte indicates the types of the patch command or event.
1430 * 0x01 means HCI command and 0x02 is HCI event. If the first bytes
1431 * in the current firmware buffer doesn't start with 0x01 or
1432 * the size of remain buffer is smaller than HCI command header,
1433 * the firmware file is corrupted and it should stop the patching
1434 * process.
1435 */
1436 if (remain > HCI_COMMAND_HDR_SIZE && *fw_ptr[0] != 0x01) {
1437 BT_ERR("%s Intel fw corrupted: invalid cmd read", hdev->name);
1438 return -EINVAL;
1439 }
1440 (*fw_ptr)++;
1441 remain--;
1442
1443 cmd = (struct hci_command_hdr *)(*fw_ptr);
1444 *fw_ptr += sizeof(*cmd);
1445 remain -= sizeof(*cmd);
1446
1447 /* Ensure that the remain firmware data is long enough than the length
1448 * of command parameter. If not, the firmware file is corrupted.
1449 */
1450 if (remain < cmd->plen) {
1451 BT_ERR("%s Intel fw corrupted: invalid cmd len", hdev->name);
1452 return -EFAULT;
1453 }
1454
1455 /* If there is a command that loads a patch in the firmware
1456 * file, then enable the patch upon success, otherwise just
1457 * disable the manufacturer mode, for example patch activation
1458 * is not required when the default firmware patch file is used
1459 * because there are no patch data to load.
1460 */
1461 if (*disable_patch && le16_to_cpu(cmd->opcode) == 0xfc8e)
1462 *disable_patch = 0;
1463
1464 cmd_param = *fw_ptr;
1465 *fw_ptr += cmd->plen;
1466 remain -= cmd->plen;
1467
1468 /* This reads the expected events when the above command is sent to the
1469 * device. Some vendor commands expects more than one events, for
1470 * example command status event followed by vendor specific event.
1471 * For this case, it only keeps the last expected event. so the command
1472 * can be sent with __hci_cmd_sync_ev() which returns the sk_buff of
1473 * last expected event.
1474 */
1475 while (remain > HCI_EVENT_HDR_SIZE && *fw_ptr[0] == 0x02) {
1476 (*fw_ptr)++;
1477 remain--;
1478
1479 evt = (struct hci_event_hdr *)(*fw_ptr);
1480 *fw_ptr += sizeof(*evt);
1481 remain -= sizeof(*evt);
1482
1483 if (remain < evt->plen) {
1484 BT_ERR("%s Intel fw corrupted: invalid evt len",
1485 hdev->name);
1486 return -EFAULT;
1487 }
1488
1489 evt_param = *fw_ptr;
1490 *fw_ptr += evt->plen;
1491 remain -= evt->plen;
1492 }
1493
1494 /* Every HCI commands in the firmware file has its correspond event.
1495 * If event is not found or remain is smaller than zero, the firmware
1496 * file is corrupted.
1497 */
1498 if (!evt || !evt_param || remain < 0) {
1499 BT_ERR("%s Intel fw corrupted: invalid evt read", hdev->name);
1500 return -EFAULT;
1501 }
1502
1503 skb = __hci_cmd_sync_ev(hdev, le16_to_cpu(cmd->opcode), cmd->plen,
1504 cmd_param, evt->evt, HCI_INIT_TIMEOUT);
1505 if (IS_ERR(skb)) {
1506 BT_ERR("%s sending Intel patch command (0x%4.4x) failed (%ld)",
1507 hdev->name, cmd->opcode, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001508 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001509 }
1510
1511 /* It ensures that the returned event matches the event data read from
1512 * the firmware file. At fist, it checks the length and then
1513 * the contents of the event.
1514 */
1515 if (skb->len != evt->plen) {
1516 BT_ERR("%s mismatch event length (opcode 0x%4.4x)", hdev->name,
1517 le16_to_cpu(cmd->opcode));
1518 kfree_skb(skb);
1519 return -EFAULT;
1520 }
1521
1522 if (memcmp(skb->data, evt_param, evt->plen)) {
1523 BT_ERR("%s mismatch event parameter (opcode 0x%4.4x)",
1524 hdev->name, le16_to_cpu(cmd->opcode));
1525 kfree_skb(skb);
1526 return -EFAULT;
1527 }
1528 kfree_skb(skb);
1529
1530 return 0;
1531}
1532
1533static int btusb_setup_intel(struct hci_dev *hdev)
1534{
1535 struct sk_buff *skb;
1536 const struct firmware *fw;
1537 const u8 *fw_ptr;
1538 int disable_patch;
1539 struct intel_version *ver;
1540
1541 const u8 mfg_enable[] = { 0x01, 0x00 };
1542 const u8 mfg_disable[] = { 0x00, 0x00 };
1543 const u8 mfg_reset_deactivate[] = { 0x00, 0x01 };
1544 const u8 mfg_reset_activate[] = { 0x00, 0x02 };
1545
1546 BT_DBG("%s", hdev->name);
1547
1548 /* The controller has a bug with the first HCI command sent to it
1549 * returning number of completed commands as zero. This would stall the
1550 * command processing in the Bluetooth core.
1551 *
1552 * As a workaround, send HCI Reset command first which will reset the
1553 * number of completed commands and allow normal command processing
1554 * from now on.
1555 */
1556 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
1557 if (IS_ERR(skb)) {
1558 BT_ERR("%s sending initial HCI reset command failed (%ld)",
1559 hdev->name, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001560 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001561 }
1562 kfree_skb(skb);
1563
1564 /* Read Intel specific controller version first to allow selection of
1565 * which firmware file to load.
1566 *
1567 * The returned information are hardware variant and revision plus
1568 * firmware variant, revision and build number.
1569 */
1570 skb = __hci_cmd_sync(hdev, 0xfc05, 0, NULL, HCI_INIT_TIMEOUT);
1571 if (IS_ERR(skb)) {
1572 BT_ERR("%s reading Intel fw version command failed (%ld)",
1573 hdev->name, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001574 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001575 }
1576
1577 if (skb->len != sizeof(*ver)) {
1578 BT_ERR("%s Intel version event length mismatch", hdev->name);
1579 kfree_skb(skb);
1580 return -EIO;
1581 }
1582
1583 ver = (struct intel_version *)skb->data;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001584
1585 BT_INFO("%s: read Intel version: %02x%02x%02x%02x%02x%02x%02x%02x%02x",
1586 hdev->name, ver->hw_platform, ver->hw_variant,
1587 ver->hw_revision, ver->fw_variant, ver->fw_revision,
1588 ver->fw_build_num, ver->fw_build_ww, ver->fw_build_yy,
1589 ver->fw_patch_num);
1590
1591 /* fw_patch_num indicates the version of patch the device currently
1592 * have. If there is no patch data in the device, it is always 0x00.
1593 * So, if it is other than 0x00, no need to patch the deivce again.
1594 */
1595 if (ver->fw_patch_num) {
1596 BT_INFO("%s: Intel device is already patched. patch num: %02x",
1597 hdev->name, ver->fw_patch_num);
1598 kfree_skb(skb);
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07001599 btintel_check_bdaddr(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001600 return 0;
1601 }
1602
1603 /* Opens the firmware patch file based on the firmware version read
1604 * from the controller. If it fails to open the matching firmware
1605 * patch file, it tries to open the default firmware patch file.
1606 * If no patch file is found, allow the device to operate without
1607 * a patch.
1608 */
1609 fw = btusb_setup_intel_get_fw(hdev, ver);
1610 if (!fw) {
1611 kfree_skb(skb);
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07001612 btintel_check_bdaddr(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001613 return 0;
1614 }
1615 fw_ptr = fw->data;
1616
1617 /* This Intel specific command enables the manufacturer mode of the
1618 * controller.
1619 *
1620 * Only while this mode is enabled, the driver can download the
1621 * firmware patch data and configuration parameters.
1622 */
1623 skb = __hci_cmd_sync(hdev, 0xfc11, 2, mfg_enable, HCI_INIT_TIMEOUT);
1624 if (IS_ERR(skb)) {
1625 BT_ERR("%s entering Intel manufacturer mode failed (%ld)",
1626 hdev->name, PTR_ERR(skb));
1627 release_firmware(fw);
Adam Leed9c78e92013-07-10 10:02:12 +08001628 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001629 }
1630
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001631 kfree_skb(skb);
1632
1633 disable_patch = 1;
1634
1635 /* The firmware data file consists of list of Intel specific HCI
1636 * commands and its expected events. The first byte indicates the
1637 * type of the message, either HCI command or HCI event.
1638 *
1639 * It reads the command and its expected event from the firmware file,
1640 * and send to the controller. Once __hci_cmd_sync_ev() returns,
1641 * the returned event is compared with the event read from the firmware
1642 * file and it will continue until all the messages are downloaded to
1643 * the controller.
1644 *
1645 * Once the firmware patching is completed successfully,
1646 * the manufacturer mode is disabled with reset and activating the
1647 * downloaded patch.
1648 *
1649 * If the firmware patching fails, the manufacturer mode is
1650 * disabled with reset and deactivating the patch.
1651 *
1652 * If the default patch file is used, no reset is done when disabling
1653 * the manufacturer.
1654 */
1655 while (fw->size > fw_ptr - fw->data) {
1656 int ret;
1657
1658 ret = btusb_setup_intel_patching(hdev, fw, &fw_ptr,
1659 &disable_patch);
1660 if (ret < 0)
1661 goto exit_mfg_deactivate;
1662 }
1663
1664 release_firmware(fw);
1665
1666 if (disable_patch)
1667 goto exit_mfg_disable;
1668
1669 /* Patching completed successfully and disable the manufacturer mode
1670 * with reset and activate the downloaded firmware patches.
1671 */
1672 skb = __hci_cmd_sync(hdev, 0xfc11, sizeof(mfg_reset_activate),
1673 mfg_reset_activate, HCI_INIT_TIMEOUT);
1674 if (IS_ERR(skb)) {
1675 BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
1676 hdev->name, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001677 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001678 }
1679 kfree_skb(skb);
1680
1681 BT_INFO("%s: Intel Bluetooth firmware patch completed and activated",
1682 hdev->name);
1683
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07001684 btintel_check_bdaddr(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001685 return 0;
1686
1687exit_mfg_disable:
1688 /* Disable the manufacturer mode without reset */
1689 skb = __hci_cmd_sync(hdev, 0xfc11, sizeof(mfg_disable), mfg_disable,
1690 HCI_INIT_TIMEOUT);
1691 if (IS_ERR(skb)) {
1692 BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
1693 hdev->name, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001694 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001695 }
1696 kfree_skb(skb);
1697
1698 BT_INFO("%s: Intel Bluetooth firmware patch completed", hdev->name);
Marcel Holtmann40cb0982014-07-02 12:06:45 +02001699
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07001700 btintel_check_bdaddr(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001701 return 0;
1702
1703exit_mfg_deactivate:
1704 release_firmware(fw);
1705
1706 /* Patching failed. Disable the manufacturer mode with reset and
1707 * deactivate the downloaded firmware patches.
1708 */
1709 skb = __hci_cmd_sync(hdev, 0xfc11, sizeof(mfg_reset_deactivate),
1710 mfg_reset_deactivate, HCI_INIT_TIMEOUT);
1711 if (IS_ERR(skb)) {
1712 BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
1713 hdev->name, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001714 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001715 }
1716 kfree_skb(skb);
1717
1718 BT_INFO("%s: Intel Bluetooth firmware patch completed and deactivated",
1719 hdev->name);
1720
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07001721 btintel_check_bdaddr(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001722 return 0;
1723}
1724
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001725static int inject_cmd_complete(struct hci_dev *hdev, __u16 opcode)
1726{
1727 struct sk_buff *skb;
1728 struct hci_event_hdr *hdr;
1729 struct hci_ev_cmd_complete *evt;
1730
1731 skb = bt_skb_alloc(sizeof(*hdr) + sizeof(*evt) + 1, GFP_ATOMIC);
1732 if (!skb)
1733 return -ENOMEM;
1734
1735 hdr = (struct hci_event_hdr *)skb_put(skb, sizeof(*hdr));
1736 hdr->evt = HCI_EV_CMD_COMPLETE;
1737 hdr->plen = sizeof(*evt) + 1;
1738
1739 evt = (struct hci_ev_cmd_complete *)skb_put(skb, sizeof(*evt));
1740 evt->ncmd = 0x01;
1741 evt->opcode = cpu_to_le16(opcode);
1742
1743 *skb_put(skb, 1) = 0x00;
1744
1745 bt_cb(skb)->pkt_type = HCI_EVENT_PKT;
1746
1747 return hci_recv_frame(hdev, skb);
1748}
1749
1750static int btusb_recv_bulk_intel(struct btusb_data *data, void *buffer,
1751 int count)
1752{
1753 /* When the device is in bootloader mode, then it can send
1754 * events via the bulk endpoint. These events are treated the
1755 * same way as the ones received from the interrupt endpoint.
1756 */
1757 if (test_bit(BTUSB_BOOTLOADER, &data->flags))
1758 return btusb_recv_intr(data, buffer, count);
1759
1760 return btusb_recv_bulk(data, buffer, count);
1761}
1762
Marcel Holtmannccd6da22015-04-09 00:35:19 -07001763static void btusb_intel_bootup(struct btusb_data *data, const void *ptr,
1764 unsigned int len)
1765{
1766 const struct intel_bootup *evt = ptr;
1767
1768 if (len != sizeof(*evt))
1769 return;
1770
1771 if (test_and_clear_bit(BTUSB_BOOTING, &data->flags)) {
1772 smp_mb__after_atomic();
1773 wake_up_bit(&data->flags, BTUSB_BOOTING);
1774 }
1775}
1776
1777static void btusb_intel_secure_send_result(struct btusb_data *data,
1778 const void *ptr, unsigned int len)
1779{
1780 const struct intel_secure_send_result *evt = ptr;
1781
1782 if (len != sizeof(*evt))
1783 return;
1784
1785 if (evt->result)
1786 set_bit(BTUSB_FIRMWARE_FAILED, &data->flags);
1787
1788 if (test_and_clear_bit(BTUSB_DOWNLOADING, &data->flags) &&
1789 test_bit(BTUSB_FIRMWARE_LOADED, &data->flags)) {
1790 smp_mb__after_atomic();
1791 wake_up_bit(&data->flags, BTUSB_DOWNLOADING);
1792 }
1793}
1794
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001795static int btusb_recv_event_intel(struct hci_dev *hdev, struct sk_buff *skb)
1796{
1797 struct btusb_data *data = hci_get_drvdata(hdev);
1798
1799 if (test_bit(BTUSB_BOOTLOADER, &data->flags)) {
1800 struct hci_event_hdr *hdr = (void *)skb->data;
1801
Marcel Holtmannccd6da22015-04-09 00:35:19 -07001802 if (skb->len > HCI_EVENT_HDR_SIZE && hdr->evt == 0xff &&
1803 hdr->plen > 0) {
1804 const void *ptr = skb->data + HCI_EVENT_HDR_SIZE + 1;
1805 unsigned int len = skb->len - HCI_EVENT_HDR_SIZE - 1;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001806
Marcel Holtmannccd6da22015-04-09 00:35:19 -07001807 switch (skb->data[2]) {
1808 case 0x02:
1809 /* When switching to the operational firmware
1810 * the device sends a vendor specific event
1811 * indicating that the bootup completed.
1812 */
1813 btusb_intel_bootup(data, ptr, len);
1814 break;
1815 case 0x06:
1816 /* When the firmware loading completes the
1817 * device sends out a vendor specific event
1818 * indicating the result of the firmware
1819 * loading.
1820 */
1821 btusb_intel_secure_send_result(data, ptr, len);
1822 break;
Johan Hedbergfad70972015-01-30 10:58:55 +02001823 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001824 }
1825 }
1826
1827 return hci_recv_frame(hdev, skb);
1828}
1829
1830static int btusb_send_frame_intel(struct hci_dev *hdev, struct sk_buff *skb)
1831{
1832 struct btusb_data *data = hci_get_drvdata(hdev);
1833 struct urb *urb;
1834
1835 BT_DBG("%s", hdev->name);
1836
1837 if (!test_bit(HCI_RUNNING, &hdev->flags))
1838 return -EBUSY;
1839
1840 switch (bt_cb(skb)->pkt_type) {
1841 case HCI_COMMAND_PKT:
1842 if (test_bit(BTUSB_BOOTLOADER, &data->flags)) {
1843 struct hci_command_hdr *cmd = (void *)skb->data;
1844 __u16 opcode = le16_to_cpu(cmd->opcode);
1845
1846 /* When in bootloader mode and the command 0xfc09
1847 * is received, it needs to be send down the
1848 * bulk endpoint. So allocate a bulk URB instead.
1849 */
1850 if (opcode == 0xfc09)
1851 urb = alloc_bulk_urb(hdev, skb);
1852 else
1853 urb = alloc_ctrl_urb(hdev, skb);
1854
1855 /* When the 0xfc01 command is issued to boot into
1856 * the operational firmware, it will actually not
1857 * send a command complete event. To keep the flow
1858 * control working inject that event here.
1859 */
1860 if (opcode == 0xfc01)
1861 inject_cmd_complete(hdev, opcode);
1862 } else {
1863 urb = alloc_ctrl_urb(hdev, skb);
1864 }
1865 if (IS_ERR(urb))
1866 return PTR_ERR(urb);
1867
1868 hdev->stat.cmd_tx++;
1869 return submit_or_queue_tx_urb(hdev, urb);
1870
1871 case HCI_ACLDATA_PKT:
1872 urb = alloc_bulk_urb(hdev, skb);
1873 if (IS_ERR(urb))
1874 return PTR_ERR(urb);
1875
1876 hdev->stat.acl_tx++;
1877 return submit_or_queue_tx_urb(hdev, urb);
1878
1879 case HCI_SCODATA_PKT:
1880 if (hci_conn_num(hdev, SCO_LINK) < 1)
1881 return -ENODEV;
1882
1883 urb = alloc_isoc_urb(hdev, skb);
1884 if (IS_ERR(urb))
1885 return PTR_ERR(urb);
1886
1887 hdev->stat.sco_tx++;
1888 return submit_tx_urb(hdev, urb);
1889 }
1890
1891 return -EILSEQ;
1892}
1893
1894static int btusb_intel_secure_send(struct hci_dev *hdev, u8 fragment_type,
1895 u32 plen, const void *param)
1896{
1897 while (plen > 0) {
1898 struct sk_buff *skb;
1899 u8 cmd_param[253], fragment_len = (plen > 252) ? 252 : plen;
1900
1901 cmd_param[0] = fragment_type;
1902 memcpy(cmd_param + 1, param, fragment_len);
1903
1904 skb = __hci_cmd_sync(hdev, 0xfc09, fragment_len + 1,
1905 cmd_param, HCI_INIT_TIMEOUT);
1906 if (IS_ERR(skb))
1907 return PTR_ERR(skb);
1908
1909 kfree_skb(skb);
1910
1911 plen -= fragment_len;
1912 param += fragment_len;
1913 }
1914
1915 return 0;
1916}
1917
1918static void btusb_intel_version_info(struct hci_dev *hdev,
1919 struct intel_version *ver)
1920{
1921 const char *variant;
1922
1923 switch (ver->fw_variant) {
1924 case 0x06:
1925 variant = "Bootloader";
1926 break;
1927 case 0x23:
1928 variant = "Firmware";
1929 break;
1930 default:
1931 return;
1932 }
1933
1934 BT_INFO("%s: %s revision %u.%u build %u week %u %u", hdev->name,
1935 variant, ver->fw_revision >> 4, ver->fw_revision & 0x0f,
1936 ver->fw_build_num, ver->fw_build_ww, 2000 + ver->fw_build_yy);
1937}
1938
1939static int btusb_setup_intel_new(struct hci_dev *hdev)
1940{
1941 static const u8 reset_param[] = { 0x00, 0x01, 0x00, 0x01,
1942 0x00, 0x08, 0x04, 0x00 };
1943 struct btusb_data *data = hci_get_drvdata(hdev);
1944 struct sk_buff *skb;
1945 struct intel_version *ver;
1946 struct intel_boot_params *params;
1947 const struct firmware *fw;
1948 const u8 *fw_ptr;
1949 char fwname[64];
1950 ktime_t calltime, delta, rettime;
1951 unsigned long long duration;
1952 int err;
1953
1954 BT_DBG("%s", hdev->name);
1955
1956 calltime = ktime_get();
1957
1958 /* Read the Intel version information to determine if the device
1959 * is in bootloader mode or if it already has operational firmware
1960 * loaded.
1961 */
1962 skb = __hci_cmd_sync(hdev, 0xfc05, 0, NULL, HCI_INIT_TIMEOUT);
1963 if (IS_ERR(skb)) {
1964 BT_ERR("%s: Reading Intel version information failed (%ld)",
1965 hdev->name, PTR_ERR(skb));
1966 return PTR_ERR(skb);
1967 }
1968
1969 if (skb->len != sizeof(*ver)) {
1970 BT_ERR("%s: Intel version event size mismatch", hdev->name);
1971 kfree_skb(skb);
1972 return -EILSEQ;
1973 }
1974
1975 ver = (struct intel_version *)skb->data;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001976
1977 /* The hardware platform number has a fixed value of 0x37 and
1978 * for now only accept this single value.
1979 */
1980 if (ver->hw_platform != 0x37) {
1981 BT_ERR("%s: Unsupported Intel hardware platform (%u)",
1982 hdev->name, ver->hw_platform);
1983 kfree_skb(skb);
1984 return -EINVAL;
1985 }
1986
1987 /* At the moment only the hardware variant iBT 3.0 (LnP/SfP) is
1988 * supported by this firmware loading method. This check has been
1989 * put in place to ensure correct forward compatibility options
1990 * when newer hardware variants come along.
1991 */
1992 if (ver->hw_variant != 0x0b) {
1993 BT_ERR("%s: Unsupported Intel hardware variant (%u)",
1994 hdev->name, ver->hw_variant);
1995 kfree_skb(skb);
1996 return -EINVAL;
1997 }
1998
1999 btusb_intel_version_info(hdev, ver);
2000
2001 /* The firmware variant determines if the device is in bootloader
2002 * mode or is running operational firmware. The value 0x06 identifies
2003 * the bootloader and the value 0x23 identifies the operational
2004 * firmware.
2005 *
2006 * When the operational firmware is already present, then only
2007 * the check for valid Bluetooth device address is needed. This
2008 * determines if the device will be added as configured or
2009 * unconfigured controller.
2010 *
2011 * It is not possible to use the Secure Boot Parameters in this
2012 * case since that command is only available in bootloader mode.
2013 */
2014 if (ver->fw_variant == 0x23) {
2015 kfree_skb(skb);
2016 clear_bit(BTUSB_BOOTLOADER, &data->flags);
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07002017 btintel_check_bdaddr(hdev);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002018 return 0;
2019 }
2020
2021 /* If the device is not in bootloader mode, then the only possible
2022 * choice is to return an error and abort the device initialization.
2023 */
2024 if (ver->fw_variant != 0x06) {
2025 BT_ERR("%s: Unsupported Intel firmware variant (%u)",
2026 hdev->name, ver->fw_variant);
2027 kfree_skb(skb);
2028 return -ENODEV;
2029 }
2030
2031 kfree_skb(skb);
2032
2033 /* Read the secure boot parameters to identify the operating
2034 * details of the bootloader.
2035 */
2036 skb = __hci_cmd_sync(hdev, 0xfc0d, 0, NULL, HCI_INIT_TIMEOUT);
2037 if (IS_ERR(skb)) {
2038 BT_ERR("%s: Reading Intel boot parameters failed (%ld)",
2039 hdev->name, PTR_ERR(skb));
2040 return PTR_ERR(skb);
2041 }
2042
2043 if (skb->len != sizeof(*params)) {
2044 BT_ERR("%s: Intel boot parameters size mismatch", hdev->name);
2045 kfree_skb(skb);
2046 return -EILSEQ;
2047 }
2048
2049 params = (struct intel_boot_params *)skb->data;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002050
2051 BT_INFO("%s: Device revision is %u", hdev->name,
2052 le16_to_cpu(params->dev_revid));
2053
2054 BT_INFO("%s: Secure boot is %s", hdev->name,
2055 params->secure_boot ? "enabled" : "disabled");
2056
2057 BT_INFO("%s: Minimum firmware build %u week %u %u", hdev->name,
2058 params->min_fw_build_nn, params->min_fw_build_cw,
2059 2000 + params->min_fw_build_yy);
2060
2061 /* It is required that every single firmware fragment is acknowledged
2062 * with a command complete event. If the boot parameters indicate
2063 * that this bootloader does not send them, then abort the setup.
2064 */
2065 if (params->limited_cce != 0x00) {
2066 BT_ERR("%s: Unsupported Intel firmware loading method (%u)",
2067 hdev->name, params->limited_cce);
2068 kfree_skb(skb);
2069 return -EINVAL;
2070 }
2071
2072 /* If the OTP has no valid Bluetooth device address, then there will
2073 * also be no valid address for the operational firmware.
2074 */
2075 if (!bacmp(&params->otp_bdaddr, BDADDR_ANY)) {
2076 BT_INFO("%s: No device address configured", hdev->name);
2077 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
2078 }
2079
2080 /* With this Intel bootloader only the hardware variant and device
2081 * revision information are used to select the right firmware.
2082 *
2083 * Currently this bootloader support is limited to hardware variant
2084 * iBT 3.0 (LnP/SfP) which is identified by the value 11 (0x0b).
2085 */
2086 snprintf(fwname, sizeof(fwname), "intel/ibt-11-%u.sfi",
2087 le16_to_cpu(params->dev_revid));
2088
2089 err = request_firmware(&fw, fwname, &hdev->dev);
2090 if (err < 0) {
2091 BT_ERR("%s: Failed to load Intel firmware file (%d)",
2092 hdev->name, err);
2093 kfree_skb(skb);
2094 return err;
2095 }
2096
2097 BT_INFO("%s: Found device firmware: %s", hdev->name, fwname);
2098
2099 kfree_skb(skb);
2100
2101 if (fw->size < 644) {
2102 BT_ERR("%s: Invalid size of firmware file (%zu)",
2103 hdev->name, fw->size);
2104 err = -EBADF;
2105 goto done;
2106 }
2107
2108 set_bit(BTUSB_DOWNLOADING, &data->flags);
2109
2110 /* Start the firmware download transaction with the Init fragment
2111 * represented by the 128 bytes of CSS header.
2112 */
2113 err = btusb_intel_secure_send(hdev, 0x00, 128, fw->data);
2114 if (err < 0) {
2115 BT_ERR("%s: Failed to send firmware header (%d)",
2116 hdev->name, err);
2117 goto done;
2118 }
2119
2120 /* Send the 256 bytes of public key information from the firmware
2121 * as the PKey fragment.
2122 */
2123 err = btusb_intel_secure_send(hdev, 0x03, 256, fw->data + 128);
2124 if (err < 0) {
2125 BT_ERR("%s: Failed to send firmware public key (%d)",
2126 hdev->name, err);
2127 goto done;
2128 }
2129
2130 /* Send the 256 bytes of signature information from the firmware
2131 * as the Sign fragment.
2132 */
2133 err = btusb_intel_secure_send(hdev, 0x02, 256, fw->data + 388);
2134 if (err < 0) {
2135 BT_ERR("%s: Failed to send firmware signature (%d)",
2136 hdev->name, err);
2137 goto done;
2138 }
2139
2140 fw_ptr = fw->data + 644;
2141
2142 while (fw_ptr - fw->data < fw->size) {
2143 struct hci_command_hdr *cmd = (void *)fw_ptr;
2144 u8 cmd_len;
2145
2146 cmd_len = sizeof(*cmd) + cmd->plen;
2147
2148 /* Send each command from the firmware data buffer as
2149 * a single Data fragment.
2150 */
2151 err = btusb_intel_secure_send(hdev, 0x01, cmd_len, fw_ptr);
2152 if (err < 0) {
2153 BT_ERR("%s: Failed to send firmware data (%d)",
2154 hdev->name, err);
2155 goto done;
2156 }
2157
2158 fw_ptr += cmd_len;
2159 }
2160
Marcel Holtmannce6bb922015-01-28 01:58:40 -08002161 set_bit(BTUSB_FIRMWARE_LOADED, &data->flags);
2162
Johan Hedberga087a982015-01-30 10:58:54 +02002163 BT_INFO("%s: Waiting for firmware download to complete", hdev->name);
2164
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002165 /* Before switching the device into operational mode and with that
2166 * booting the loaded firmware, wait for the bootloader notification
2167 * that all fragments have been successfully received.
2168 *
Johan Hedberga087a982015-01-30 10:58:54 +02002169 * When the event processing receives the notification, then the
2170 * BTUSB_DOWNLOADING flag will be cleared.
2171 *
2172 * The firmware loading should not take longer than 5 seconds
2173 * and thus just timeout if that happens and fail the setup
2174 * of this device.
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002175 */
Johan Hedberg129a7692015-02-14 09:33:35 +02002176 err = wait_on_bit_timeout(&data->flags, BTUSB_DOWNLOADING,
2177 TASK_INTERRUPTIBLE,
2178 msecs_to_jiffies(5000));
Johan Hedberga087a982015-01-30 10:58:54 +02002179 if (err == 1) {
2180 BT_ERR("%s: Firmware loading interrupted", hdev->name);
2181 err = -EINTR;
2182 goto done;
2183 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002184
Johan Hedberga087a982015-01-30 10:58:54 +02002185 if (err) {
2186 BT_ERR("%s: Firmware loading timeout", hdev->name);
2187 err = -ETIMEDOUT;
2188 goto done;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002189 }
2190
2191 if (test_bit(BTUSB_FIRMWARE_FAILED, &data->flags)) {
2192 BT_ERR("%s: Firmware loading failed", hdev->name);
2193 err = -ENOEXEC;
2194 goto done;
2195 }
2196
2197 rettime = ktime_get();
2198 delta = ktime_sub(rettime, calltime);
2199 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
2200
2201 BT_INFO("%s: Firmware loaded in %llu usecs", hdev->name, duration);
2202
2203done:
2204 release_firmware(fw);
2205
2206 if (err < 0)
2207 return err;
2208
2209 calltime = ktime_get();
2210
2211 set_bit(BTUSB_BOOTING, &data->flags);
2212
2213 skb = __hci_cmd_sync(hdev, 0xfc01, sizeof(reset_param), reset_param,
2214 HCI_INIT_TIMEOUT);
2215 if (IS_ERR(skb))
2216 return PTR_ERR(skb);
2217
2218 kfree_skb(skb);
2219
2220 /* The bootloader will not indicate when the device is ready. This
2221 * is done by the operational firmware sending bootup notification.
Johan Hedbergfad70972015-01-30 10:58:55 +02002222 *
2223 * Booting into operational firmware should not take longer than
2224 * 1 second. However if that happens, then just fail the setup
2225 * since something went wrong.
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002226 */
Johan Hedbergfad70972015-01-30 10:58:55 +02002227 BT_INFO("%s: Waiting for device to boot", hdev->name);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002228
Johan Hedberg129a7692015-02-14 09:33:35 +02002229 err = wait_on_bit_timeout(&data->flags, BTUSB_BOOTING,
2230 TASK_INTERRUPTIBLE,
2231 msecs_to_jiffies(1000));
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002232
Johan Hedbergfad70972015-01-30 10:58:55 +02002233 if (err == 1) {
2234 BT_ERR("%s: Device boot interrupted", hdev->name);
2235 return -EINTR;
2236 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002237
Johan Hedbergfad70972015-01-30 10:58:55 +02002238 if (err) {
2239 BT_ERR("%s: Device boot timeout", hdev->name);
2240 return -ETIMEDOUT;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002241 }
2242
2243 rettime = ktime_get();
2244 delta = ktime_sub(rettime, calltime);
2245 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
2246
2247 BT_INFO("%s: Device booted in %llu usecs", hdev->name, duration);
2248
2249 clear_bit(BTUSB_BOOTLOADER, &data->flags);
2250
2251 return 0;
2252}
2253
Marcel Holtmann385a7682015-01-28 11:09:56 -08002254static void btusb_hw_error_intel(struct hci_dev *hdev, u8 code)
2255{
2256 struct sk_buff *skb;
2257 u8 type = 0x00;
2258
2259 BT_ERR("%s: Hardware error 0x%2.2x", hdev->name, code);
2260
2261 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
2262 if (IS_ERR(skb)) {
2263 BT_ERR("%s: Reset after hardware error failed (%ld)",
2264 hdev->name, PTR_ERR(skb));
2265 return;
2266 }
2267 kfree_skb(skb);
2268
2269 skb = __hci_cmd_sync(hdev, 0xfc22, 1, &type, HCI_INIT_TIMEOUT);
2270 if (IS_ERR(skb)) {
2271 BT_ERR("%s: Retrieving Intel exception info failed (%ld)",
2272 hdev->name, PTR_ERR(skb));
2273 return;
2274 }
2275
2276 if (skb->len != 13) {
2277 BT_ERR("%s: Exception info size mismatch", hdev->name);
2278 kfree_skb(skb);
2279 return;
2280 }
2281
Marcel Holtmann385a7682015-01-28 11:09:56 -08002282 BT_ERR("%s: Exception info %s", hdev->name, (char *)(skb->data + 1));
2283
2284 kfree_skb(skb);
2285}
2286
Tedd Ho-Jeong Anbfbd45e2015-02-13 09:20:52 -08002287static int btusb_shutdown_intel(struct hci_dev *hdev)
2288{
2289 struct sk_buff *skb;
2290 long ret;
2291
2292 /* Some platforms have an issue with BT LED when the interface is
2293 * down or BT radio is turned off, which takes 5 seconds to BT LED
2294 * goes off. This command turns off the BT LED immediately.
2295 */
2296 skb = __hci_cmd_sync(hdev, 0xfc3f, 0, NULL, HCI_INIT_TIMEOUT);
2297 if (IS_ERR(skb)) {
2298 ret = PTR_ERR(skb);
2299 BT_ERR("%s: turning off Intel device LED failed (%ld)",
2300 hdev->name, ret);
2301 return ret;
2302 }
2303 kfree_skb(skb);
2304
2305 return 0;
2306}
2307
Amitkumar Karwarae8df492014-07-18 14:47:06 -07002308static int btusb_set_bdaddr_marvell(struct hci_dev *hdev,
2309 const bdaddr_t *bdaddr)
2310{
2311 struct sk_buff *skb;
2312 u8 buf[8];
2313 long ret;
2314
2315 buf[0] = 0xfe;
2316 buf[1] = sizeof(bdaddr_t);
2317 memcpy(buf + 2, bdaddr, sizeof(bdaddr_t));
2318
2319 skb = __hci_cmd_sync(hdev, 0xfc22, sizeof(buf), buf, HCI_INIT_TIMEOUT);
2320 if (IS_ERR(skb)) {
2321 ret = PTR_ERR(skb);
2322 BT_ERR("%s: changing Marvell device address failed (%ld)",
2323 hdev->name, ret);
2324 return ret;
2325 }
2326 kfree_skb(skb);
2327
2328 return 0;
2329}
2330
Toshi Kikuchi58592232014-12-12 10:58:05 -08002331static int btusb_set_bdaddr_ath3012(struct hci_dev *hdev,
2332 const bdaddr_t *bdaddr)
2333{
2334 struct sk_buff *skb;
2335 u8 buf[10];
2336 long ret;
2337
2338 buf[0] = 0x01;
2339 buf[1] = 0x01;
2340 buf[2] = 0x00;
2341 buf[3] = sizeof(bdaddr_t);
2342 memcpy(buf + 4, bdaddr, sizeof(bdaddr_t));
2343
2344 skb = __hci_cmd_sync(hdev, 0xfc0b, sizeof(buf), buf, HCI_INIT_TIMEOUT);
2345 if (IS_ERR(skb)) {
2346 ret = PTR_ERR(skb);
2347 BT_ERR("%s: Change address command failed (%ld)",
2348 hdev->name, ret);
2349 return ret;
2350 }
2351 kfree_skb(skb);
2352
2353 return 0;
2354}
2355
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002356#define QCA_DFU_PACKET_LEN 4096
2357
2358#define QCA_GET_TARGET_VERSION 0x09
2359#define QCA_CHECK_STATUS 0x05
2360#define QCA_DFU_DOWNLOAD 0x01
2361
2362#define QCA_SYSCFG_UPDATED 0x40
2363#define QCA_PATCH_UPDATED 0x80
2364#define QCA_DFU_TIMEOUT 3000
2365
2366struct qca_version {
2367 __le32 rom_version;
2368 __le32 patch_version;
2369 __le32 ram_version;
2370 __le32 ref_clock;
2371 __u8 reserved[4];
2372} __packed;
2373
2374struct qca_rampatch_version {
2375 __le16 rom_version;
2376 __le16 patch_version;
2377} __packed;
2378
2379struct qca_device_info {
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002380 u32 rom_version;
2381 u8 rampatch_hdr; /* length of header in rampatch */
2382 u8 nvm_hdr; /* length of header in NVM */
2383 u8 ver_offset; /* offset of version structure in rampatch */
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002384};
2385
2386static const struct qca_device_info qca_devices_table[] = {
2387 { 0x00000100, 20, 4, 10 }, /* Rome 1.0 */
2388 { 0x00000101, 20, 4, 10 }, /* Rome 1.1 */
Chan-yeol Park7f6e6362015-05-21 11:24:27 +09002389 { 0x00000200, 28, 4, 18 }, /* Rome 2.0 */
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002390 { 0x00000201, 28, 4, 18 }, /* Rome 2.1 */
2391 { 0x00000300, 28, 4, 18 }, /* Rome 3.0 */
2392 { 0x00000302, 28, 4, 18 }, /* Rome 3.2 */
2393};
2394
2395static int btusb_qca_send_vendor_req(struct hci_dev *hdev, u8 request,
2396 void *data, u16 size)
2397{
2398 struct btusb_data *btdata = hci_get_drvdata(hdev);
2399 struct usb_device *udev = btdata->udev;
2400 int pipe, err;
2401 u8 *buf;
2402
2403 buf = kmalloc(size, GFP_KERNEL);
2404 if (!buf)
2405 return -ENOMEM;
2406
2407 /* Found some of USB hosts have IOT issues with ours so that we should
2408 * not wait until HCI layer is ready.
2409 */
2410 pipe = usb_rcvctrlpipe(udev, 0);
2411 err = usb_control_msg(udev, pipe, request, USB_TYPE_VENDOR | USB_DIR_IN,
2412 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
2413 if (err < 0) {
2414 BT_ERR("%s: Failed to access otp area (%d)", hdev->name, err);
2415 goto done;
2416 }
2417
2418 memcpy(data, buf, size);
2419
2420done:
2421 kfree(buf);
2422
2423 return err;
2424}
2425
2426static int btusb_setup_qca_download_fw(struct hci_dev *hdev,
2427 const struct firmware *firmware,
2428 size_t hdr_size)
2429{
2430 struct btusb_data *btdata = hci_get_drvdata(hdev);
2431 struct usb_device *udev = btdata->udev;
2432 size_t count, size, sent = 0;
2433 int pipe, len, err;
2434 u8 *buf;
2435
2436 buf = kmalloc(QCA_DFU_PACKET_LEN, GFP_KERNEL);
2437 if (!buf)
2438 return -ENOMEM;
2439
2440 count = firmware->size;
2441
2442 size = min_t(size_t, count, hdr_size);
2443 memcpy(buf, firmware->data, size);
2444
2445 /* USB patches should go down to controller through USB path
2446 * because binary format fits to go down through USB channel.
2447 * USB control path is for patching headers and USB bulk is for
2448 * patch body.
2449 */
2450 pipe = usb_sndctrlpipe(udev, 0);
2451 err = usb_control_msg(udev, pipe, QCA_DFU_DOWNLOAD, USB_TYPE_VENDOR,
2452 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
2453 if (err < 0) {
2454 BT_ERR("%s: Failed to send headers (%d)", hdev->name, err);
2455 goto done;
2456 }
2457
2458 sent += size;
2459 count -= size;
2460
2461 while (count) {
2462 size = min_t(size_t, count, QCA_DFU_PACKET_LEN);
2463
2464 memcpy(buf, firmware->data + sent, size);
2465
2466 pipe = usb_sndbulkpipe(udev, 0x02);
2467 err = usb_bulk_msg(udev, pipe, buf, size, &len,
2468 QCA_DFU_TIMEOUT);
2469 if (err < 0) {
2470 BT_ERR("%s: Failed to send body at %zd of %zd (%d)",
2471 hdev->name, sent, firmware->size, err);
2472 break;
2473 }
2474
2475 if (size != len) {
2476 BT_ERR("%s: Failed to get bulk buffer", hdev->name);
2477 err = -EILSEQ;
2478 break;
2479 }
2480
2481 sent += size;
2482 count -= size;
2483 }
2484
2485done:
2486 kfree(buf);
2487 return err;
2488}
2489
2490static int btusb_setup_qca_load_rampatch(struct hci_dev *hdev,
2491 struct qca_version *ver,
2492 const struct qca_device_info *info)
2493{
2494 struct qca_rampatch_version *rver;
2495 const struct firmware *fw;
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002496 u32 ver_rom, ver_patch;
2497 u16 rver_rom, rver_patch;
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002498 char fwname[64];
2499 int err;
2500
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002501 ver_rom = le32_to_cpu(ver->rom_version);
2502 ver_patch = le32_to_cpu(ver->patch_version);
2503
2504 snprintf(fwname, sizeof(fwname), "qca/rampatch_usb_%08x.bin", ver_rom);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002505
2506 err = request_firmware(&fw, fwname, &hdev->dev);
2507 if (err) {
2508 BT_ERR("%s: failed to request rampatch file: %s (%d)",
2509 hdev->name, fwname, err);
2510 return err;
2511 }
2512
2513 BT_INFO("%s: using rampatch file: %s", hdev->name, fwname);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002514
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002515 rver = (struct qca_rampatch_version *)(fw->data + info->ver_offset);
2516 rver_rom = le16_to_cpu(rver->rom_version);
2517 rver_patch = le16_to_cpu(rver->patch_version);
2518
2519 BT_INFO("%s: QCA: patch rome 0x%x build 0x%x, firmware rome 0x%x "
2520 "build 0x%x", hdev->name, rver_rom, rver_patch, ver_rom,
2521 ver_patch);
2522
2523 if (rver_rom != ver_rom || rver_patch <= ver_patch) {
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002524 BT_ERR("%s: rampatch file version did not match with firmware",
2525 hdev->name);
2526 err = -EINVAL;
2527 goto done;
2528 }
2529
2530 err = btusb_setup_qca_download_fw(hdev, fw, info->rampatch_hdr);
2531
2532done:
2533 release_firmware(fw);
2534
2535 return err;
2536}
2537
2538static int btusb_setup_qca_load_nvm(struct hci_dev *hdev,
2539 struct qca_version *ver,
2540 const struct qca_device_info *info)
2541{
2542 const struct firmware *fw;
2543 char fwname[64];
2544 int err;
2545
2546 snprintf(fwname, sizeof(fwname), "qca/nvm_usb_%08x.bin",
2547 le32_to_cpu(ver->rom_version));
2548
2549 err = request_firmware(&fw, fwname, &hdev->dev);
2550 if (err) {
2551 BT_ERR("%s: failed to request NVM file: %s (%d)",
2552 hdev->name, fwname, err);
2553 return err;
2554 }
2555
2556 BT_INFO("%s: using NVM file: %s", hdev->name, fwname);
2557
2558 err = btusb_setup_qca_download_fw(hdev, fw, info->nvm_hdr);
2559
2560 release_firmware(fw);
2561
2562 return err;
2563}
2564
2565static int btusb_setup_qca(struct hci_dev *hdev)
2566{
2567 const struct qca_device_info *info = NULL;
2568 struct qca_version ver;
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002569 u32 ver_rom;
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002570 u8 status;
2571 int i, err;
2572
2573 err = btusb_qca_send_vendor_req(hdev, QCA_GET_TARGET_VERSION, &ver,
Marcel Holtmanneb500422015-04-16 23:15:50 +02002574 sizeof(ver));
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002575 if (err < 0)
2576 return err;
2577
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002578 ver_rom = le32_to_cpu(ver.rom_version);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002579 for (i = 0; i < ARRAY_SIZE(qca_devices_table); i++) {
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002580 if (ver_rom == qca_devices_table[i].rom_version)
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002581 info = &qca_devices_table[i];
2582 }
2583 if (!info) {
2584 BT_ERR("%s: don't support firmware rome 0x%x", hdev->name,
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002585 ver_rom);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002586 return -ENODEV;
2587 }
2588
2589 err = btusb_qca_send_vendor_req(hdev, QCA_CHECK_STATUS, &status,
2590 sizeof(status));
2591 if (err < 0)
2592 return err;
2593
2594 if (!(status & QCA_PATCH_UPDATED)) {
2595 err = btusb_setup_qca_load_rampatch(hdev, &ver, info);
2596 if (err < 0)
2597 return err;
2598 }
2599
2600 if (!(status & QCA_SYSCFG_UPDATED)) {
2601 err = btusb_setup_qca_load_nvm(hdev, &ver, info);
2602 if (err < 0)
2603 return err;
2604 }
2605
2606 return 0;
2607}
2608
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002609static int btusb_probe(struct usb_interface *intf,
Marcel Holtmann89e75332014-09-16 04:44:50 +02002610 const struct usb_device_id *id)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002611{
2612 struct usb_endpoint_descriptor *ep_desc;
2613 struct btusb_data *data;
2614 struct hci_dev *hdev;
2615 int i, err;
2616
2617 BT_DBG("intf %p id %p", intf, id);
2618
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002619 /* interface numbers are hardcoded in the spec */
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002620 if (intf->cur_altsetting->desc.bInterfaceNumber != 0)
2621 return -ENODEV;
2622
2623 if (!id->driver_info) {
2624 const struct usb_device_id *match;
Marcel Holtmann89e75332014-09-16 04:44:50 +02002625
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002626 match = usb_match_id(intf, blacklist_table);
2627 if (match)
2628 id = match;
2629 }
2630
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002631 if (id->driver_info == BTUSB_IGNORE)
2632 return -ENODEV;
2633
Steven.Li2d25f8b2011-07-01 14:02:36 +08002634 if (id->driver_info & BTUSB_ATH3012) {
2635 struct usb_device *udev = interface_to_usbdev(intf);
2636
2637 /* Old firmware would otherwise let ath3k driver load
2638 * patch and sysconfig files */
2639 if (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x0001)
2640 return -ENODEV;
2641 }
2642
Sachin Kamat98921db2012-07-27 12:38:39 +05302643 data = devm_kzalloc(&intf->dev, sizeof(*data), GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002644 if (!data)
2645 return -ENOMEM;
2646
2647 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
2648 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
2649
2650 if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
2651 data->intr_ep = ep_desc;
2652 continue;
2653 }
2654
2655 if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
2656 data->bulk_tx_ep = ep_desc;
2657 continue;
2658 }
2659
2660 if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
2661 data->bulk_rx_ep = ep_desc;
2662 continue;
2663 }
2664 }
2665
Sachin Kamat98921db2012-07-27 12:38:39 +05302666 if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002667 return -ENODEV;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002668
Marcel Holtmann893ba542015-01-28 20:27:34 -08002669 if (id->driver_info & BTUSB_AMP) {
2670 data->cmdreq_type = USB_TYPE_CLASS | 0x01;
2671 data->cmdreq = 0x2b;
2672 } else {
2673 data->cmdreq_type = USB_TYPE_CLASS;
2674 data->cmdreq = 0x00;
2675 }
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002676
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002677 data->udev = interface_to_usbdev(intf);
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02002678 data->intf = intf;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002679
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002680 INIT_WORK(&data->work, btusb_work);
Oliver Neukum7bee5492009-08-24 23:44:59 +02002681 INIT_WORK(&data->waker, btusb_waker);
Marcel Holtmann803b5832014-09-16 08:00:29 +02002682 init_usb_anchor(&data->deferred);
2683 init_usb_anchor(&data->tx_anchor);
Oliver Neukum7bee5492009-08-24 23:44:59 +02002684 spin_lock_init(&data->txlock);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002685
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002686 init_usb_anchor(&data->intr_anchor);
2687 init_usb_anchor(&data->bulk_anchor);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02002688 init_usb_anchor(&data->isoc_anchor);
Marcel Holtmann803b5832014-09-16 08:00:29 +02002689 spin_lock_init(&data->rxlock);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002690
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002691 if (id->driver_info & BTUSB_INTEL_NEW) {
2692 data->recv_event = btusb_recv_event_intel;
2693 data->recv_bulk = btusb_recv_bulk_intel;
2694 set_bit(BTUSB_BOOTLOADER, &data->flags);
2695 } else {
2696 data->recv_event = hci_recv_frame;
2697 data->recv_bulk = btusb_recv_bulk;
2698 }
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +01002699
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002700 hdev = hci_alloc_dev();
Sachin Kamat98921db2012-07-27 12:38:39 +05302701 if (!hdev)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002702 return -ENOMEM;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002703
Marcel Holtmannc13854c2010-02-08 15:27:07 +01002704 hdev->bus = HCI_USB;
David Herrmann155961e2012-02-09 21:58:32 +01002705 hci_set_drvdata(hdev, data);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002706
Marcel Holtmann893ba542015-01-28 20:27:34 -08002707 if (id->driver_info & BTUSB_AMP)
2708 hdev->dev_type = HCI_AMP;
2709 else
2710 hdev->dev_type = HCI_BREDR;
2711
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002712 data->hdev = hdev;
2713
2714 SET_HCIDEV_DEV(hdev, &intf->dev);
2715
Marcel Holtmann9f8f9622013-04-10 08:11:35 -07002716 hdev->open = btusb_open;
2717 hdev->close = btusb_close;
2718 hdev->flush = btusb_flush;
2719 hdev->send = btusb_send_frame;
2720 hdev->notify = btusb_notify;
2721
2722 if (id->driver_info & BTUSB_BCM92035)
2723 hdev->setup = btusb_setup_bcm92035;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002724
Marcel Holtmannc2bfb102015-04-05 22:52:14 -07002725#ifdef CONFIG_BT_HCIBTUSB_BCM
Marcel Holtmannabbaf502014-07-02 00:53:48 +02002726 if (id->driver_info & BTUSB_BCM_PATCHRAM) {
Marcel Holtmannc2bfb102015-04-05 22:52:14 -07002727 hdev->setup = btbcm_setup_patchram;
Marcel Holtmann1df1f592015-04-05 22:52:11 -07002728 hdev->set_bdaddr = btbcm_set_bdaddr;
Marcel Holtmannabbaf502014-07-02 00:53:48 +02002729 }
Petri Gynther10d4c672014-05-08 15:50:01 -07002730
Marcel Holtmann941521e2015-04-05 22:52:15 -07002731 if (id->driver_info & BTUSB_BCM_APPLE)
Marcel Holtmannc2bfb102015-04-05 22:52:14 -07002732 hdev->setup = btbcm_setup_apple;
Marcel Holtmannc2bfb102015-04-05 22:52:14 -07002733#endif
Marcel Holtmann17b27722015-03-22 15:52:38 +01002734
Marcel Holtmanncb8d6592014-07-02 11:25:25 +02002735 if (id->driver_info & BTUSB_INTEL) {
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002736 hdev->setup = btusb_setup_intel;
Tedd Ho-Jeong Anbfbd45e2015-02-13 09:20:52 -08002737 hdev->shutdown = btusb_shutdown_intel;
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07002738 hdev->set_bdaddr = btintel_set_bdaddr;
Jakub Pawlowskic33fb9b2015-01-30 18:55:58 -08002739 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
Jakub Pawlowskic1154842015-03-17 09:04:16 -07002740 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
Marcel Holtmanncb8d6592014-07-02 11:25:25 +02002741 }
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002742
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002743 if (id->driver_info & BTUSB_INTEL_NEW) {
2744 hdev->send = btusb_send_frame_intel;
2745 hdev->setup = btusb_setup_intel_new;
Marcel Holtmann385a7682015-01-28 11:09:56 -08002746 hdev->hw_error = btusb_hw_error_intel;
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07002747 hdev->set_bdaddr = btintel_set_bdaddr;
Marcel Holtmannb970c5b2015-02-01 23:57:18 -08002748 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002749 }
2750
Amitkumar Karwarae8df492014-07-18 14:47:06 -07002751 if (id->driver_info & BTUSB_MARVELL)
2752 hdev->set_bdaddr = btusb_set_bdaddr_marvell;
2753
Marcel Holtmann661cf882015-01-02 23:35:20 -08002754 if (id->driver_info & BTUSB_SWAVE) {
2755 set_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks);
Marcel Holtmannd57dbe72014-12-26 04:42:33 +01002756 set_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS, &hdev->quirks);
Marcel Holtmann661cf882015-01-02 23:35:20 -08002757 }
Marcel Holtmannd57dbe72014-12-26 04:42:33 +01002758
Marcel Holtmann40df7832014-07-06 13:29:58 +02002759 if (id->driver_info & BTUSB_INTEL_BOOT)
2760 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
2761
Jakub Pawlowski79f0c872015-01-29 10:38:34 -08002762 if (id->driver_info & BTUSB_ATH3012) {
Toshi Kikuchi58592232014-12-12 10:58:05 -08002763 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
Jakub Pawlowski3d50d512015-03-17 09:04:15 -07002764 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
Jakub Pawlowski79f0c872015-01-29 10:38:34 -08002765 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
2766 }
Toshi Kikuchi58592232014-12-12 10:58:05 -08002767
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002768 if (id->driver_info & BTUSB_QCA_ROME) {
2769 data->setup_on_usb = btusb_setup_qca;
2770 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
2771 }
2772
Carlo Caionedb33c772015-05-14 10:49:09 +02002773#ifdef CONFIG_BT_HCIBTUSB_RTL
Daniel Drake04b8c812015-05-21 08:23:50 -06002774 if (id->driver_info & BTUSB_REALTEK) {
Carlo Caionedb33c772015-05-14 10:49:09 +02002775 hdev->setup = btrtl_setup_realtek;
Daniel Drake04b8c812015-05-21 08:23:50 -06002776
2777 /* Realtek devices lose their updated firmware over suspend,
2778 * but the USB hub doesn't notice any status change.
2779 * Explicitly request a device reset on resume.
2780 */
2781 set_bit(BTUSB_RESET_RESUME, &data->flags);
2782 }
Carlo Caionedb33c772015-05-14 10:49:09 +02002783#endif
Daniel Drakea2698a92015-04-16 14:09:55 -06002784
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
Daniel Drake04b8c812015-05-21 08:23:50 -06002915 /* Optionally request a device reset on resume, but only when
2916 * wakeups are disabled. If wakeups are enabled we assume the
2917 * device will stay powered up throughout suspend.
2918 */
2919 if (test_bit(BTUSB_RESET_RESUME, &data->flags) &&
2920 !device_may_wakeup(&data->udev->dev))
2921 data->udev->reset_resume = 1;
2922
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002923 return 0;
2924}
2925
Oliver Neukum7bee5492009-08-24 23:44:59 +02002926static void play_deferred(struct btusb_data *data)
2927{
2928 struct urb *urb;
2929 int err;
2930
2931 while ((urb = usb_get_from_anchor(&data->deferred))) {
2932 err = usb_submit_urb(urb, GFP_ATOMIC);
2933 if (err < 0)
2934 break;
2935
2936 data->tx_in_flight++;
2937 }
2938 usb_scuttle_anchored_urbs(&data->deferred);
2939}
2940
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002941static int btusb_resume(struct usb_interface *intf)
2942{
2943 struct btusb_data *data = usb_get_intfdata(intf);
2944 struct hci_dev *hdev = data->hdev;
Oliver Neukum7bee5492009-08-24 23:44:59 +02002945 int err = 0;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002946
2947 BT_DBG("intf %p", intf);
2948
2949 if (--data->suspend_count)
2950 return 0;
2951
2952 if (!test_bit(HCI_RUNNING, &hdev->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +02002953 goto done;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002954
2955 if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
2956 err = btusb_submit_intr_urb(hdev, GFP_NOIO);
2957 if (err < 0) {
2958 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02002959 goto failed;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002960 }
2961 }
2962
2963 if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) {
Marcel Holtmann43c2e572009-02-04 17:41:38 +01002964 err = btusb_submit_bulk_urb(hdev, GFP_NOIO);
2965 if (err < 0) {
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002966 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02002967 goto failed;
2968 }
2969
2970 btusb_submit_bulk_urb(hdev, GFP_NOIO);
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002971 }
2972
2973 if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
2974 if (btusb_submit_isoc_urb(hdev, GFP_NOIO) < 0)
2975 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
2976 else
2977 btusb_submit_isoc_urb(hdev, GFP_NOIO);
2978 }
2979
Oliver Neukum7bee5492009-08-24 23:44:59 +02002980 spin_lock_irq(&data->txlock);
2981 play_deferred(data);
2982 clear_bit(BTUSB_SUSPENDING, &data->flags);
2983 spin_unlock_irq(&data->txlock);
2984 schedule_work(&data->work);
2985
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002986 return 0;
Oliver Neukum7bee5492009-08-24 23:44:59 +02002987
2988failed:
2989 usb_scuttle_anchored_urbs(&data->deferred);
2990done:
2991 spin_lock_irq(&data->txlock);
2992 clear_bit(BTUSB_SUSPENDING, &data->flags);
2993 spin_unlock_irq(&data->txlock);
2994
2995 return err;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002996}
Oliver Neukum7bee5492009-08-24 23:44:59 +02002997#endif
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002998
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002999static struct usb_driver btusb_driver = {
3000 .name = "btusb",
3001 .probe = btusb_probe,
3002 .disconnect = btusb_disconnect,
Oliver Neukum7bee5492009-08-24 23:44:59 +02003003#ifdef CONFIG_PM
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003004 .suspend = btusb_suspend,
3005 .resume = btusb_resume,
Oliver Neukum7bee5492009-08-24 23:44:59 +02003006#endif
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003007 .id_table = btusb_table,
Oliver Neukum7bee5492009-08-24 23:44:59 +02003008 .supports_autosuspend = 1,
Sarah Sharpe1f12eb2012-04-23 10:08:51 -07003009 .disable_hub_initiated_lpm = 1,
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003010};
3011
Greg Kroah-Hartman93f15082011-11-18 09:47:34 -08003012module_usb_driver(btusb_driver);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003013
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02003014module_param(disable_scofix, bool, 0644);
3015MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
3016
3017module_param(force_scofix, bool, 0644);
3018MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
3019
3020module_param(reset, bool, 0644);
3021MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
3022
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003023MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
3024MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
3025MODULE_VERSION(VERSION);
3026MODULE_LICENSE("GPL");