blob: 80ae854a0bee124bed21cffbb9681abc86449a14 [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 Holtmann6c9d4352015-10-17 14:39:27 +020063#define BTUSB_BCM2045 0x40000
Marcel Holtmann22f8e9d2015-10-20 00:53:33 +020064#define BTUSB_IFNUM_2 0x80000
Marcel Holtmann5e23b922007-10-20 14:12:34 +020065
Marcel Holtmann54265202013-10-11 07:46:18 -070066static const struct usb_device_id btusb_table[] = {
Marcel Holtmann5e23b922007-10-20 14:12:34 +020067 /* Generic Bluetooth USB device */
68 { USB_DEVICE_INFO(0xe0, 0x01, 0x01) },
69
Marcel Holtmann893ba542015-01-28 20:27:34 -080070 /* Generic Bluetooth AMP device */
71 { USB_DEVICE_INFO(0xe0, 0x01, 0x04), .driver_info = BTUSB_AMP },
72
Daniel Draked63b2822015-07-17 11:12:25 -060073 /* Generic Bluetooth USB interface */
74 { USB_INTERFACE_INFO(0xe0, 0x01, 0x01) },
75
Henrik Rydberg1fa65352012-08-25 19:28:06 +020076 /* Apple-specific (Broadcom) devices */
Marcel Holtmann17b27722015-03-22 15:52:38 +010077 { USB_VENDOR_AND_INTERFACE_INFO(0x05ac, 0xff, 0x01, 0x01),
Marcel Holtmann22f8e9d2015-10-20 00:53:33 +020078 .driver_info = BTUSB_BCM_APPLE | BTUSB_IFNUM_2 },
Henrik Rydberg1fa65352012-08-25 19:28:06 +020079
Cho, Yu-Chen178c0592013-06-04 21:40:26 +080080 /* MediaTek MT76x0E */
81 { USB_DEVICE(0x0e8d, 0x763f) },
82
Oliver Neukumc510eae2011-09-21 11:41:45 +020083 /* Broadcom SoftSailing reporting vendor specific */
Don Zickus2e8b50632012-03-28 16:41:11 -040084 { USB_DEVICE(0x0a5c, 0x21e1) },
Oliver Neukumc510eae2011-09-21 11:41:45 +020085
Nobuhiro Iwamatsu3cd01972010-08-20 16:24:07 +090086 /* Apple MacBookPro 7,1 */
87 { USB_DEVICE(0x05ac, 0x8213) },
88
Cyril Lacoux0a79f672010-07-14 10:29:27 +040089 /* Apple iMac11,1 */
90 { USB_DEVICE(0x05ac, 0x8215) },
91
Nobuhiro Iwamatsu9c047152010-08-20 16:24:06 +090092 /* Apple MacBookPro6,2 */
93 { USB_DEVICE(0x05ac, 0x8218) },
94
Edgar (gimli) Hucek3e3ede72010-11-04 08:04:33 +010095 /* Apple MacBookAir3,1, MacBookAir3,2 */
96 { USB_DEVICE(0x05ac, 0x821b) },
97
Pieter-Augustijn Van Malleghema63b7232011-09-07 02:28:10 -040098 /* Apple MacBookAir4,1 */
99 { USB_DEVICE(0x05ac, 0x821f) },
100
Marc-Antoine Perennou88d377b2011-03-24 14:51:21 -0300101 /* Apple MacBookPro8,2 */
102 { USB_DEVICE(0x05ac, 0x821a) },
103
Jurgen Kramerf78b68262011-09-04 18:01:42 +0200104 /* Apple MacMini5,1 */
105 { USB_DEVICE(0x05ac, 0x8281) },
106
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200107 /* AVM BlueFRITZ! USB v2.0 */
Marcel Holtmann4fcef8e2015-01-01 17:34:37 -0800108 { USB_DEVICE(0x057c, 0x3800), .driver_info = BTUSB_SWAVE },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200109
110 /* Bluetooth Ultraport Module from IBM */
111 { USB_DEVICE(0x04bf, 0x030a) },
112
113 /* ALPS Modules with non-standard id */
114 { USB_DEVICE(0x044e, 0x3001) },
115 { USB_DEVICE(0x044e, 0x3002) },
116
117 /* Ericsson with non-standard id */
118 { USB_DEVICE(0x0bdb, 0x1002) },
119
120 /* Canyon CN-BTU1 with HID interfaces */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100121 { USB_DEVICE(0x0c10, 0x0000) },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200122
Wen-chien Jesse Sungd13431c2011-11-08 14:30:22 +0800123 /* Broadcom BCM20702A0 */
124 { USB_DEVICE(0x413c, 0x8197) },
125
Marcel Holtmannd049f4e2015-01-26 20:35:32 -0800126 /* Broadcom BCM20702B0 (Dynex/Insignia) */
127 { USB_DEVICE(0x19ff, 0x0239), .driver_info = BTUSB_BCM_PATCHRAM },
128
Santtu Rekilä2faf71c2015-10-05 15:45:27 +0300129 /* Broadcom BCM43142A0 (Foxconn/Lenovo) */
130 { USB_DEVICE(0x105b, 0xe065), .driver_info = BTUSB_BCM_PATCHRAM },
131
Steven Harms98514032012-04-13 14:45:55 -0400132 /* Foxconn - Hon Hai */
Heinrich Siebmanns6029ddc2014-12-03 19:32:22 +0100133 { USB_VENDOR_AND_INTERFACE_INFO(0x0489, 0xff, 0x01, 0x01),
134 .driver_info = BTUSB_BCM_PATCHRAM },
Steven Harms98514032012-04-13 14:45:55 -0400135
Matej Dubovy8f0c3042015-02-02 18:50:14 +0100136 /* Lite-On Technology - Broadcom based */
137 { USB_VENDOR_AND_INTERFACE_INFO(0x04ca, 0xff, 0x01, 0x01),
138 .driver_info = BTUSB_BCM_PATCHRAM },
139
Andy Shevchenko0b880062014-02-18 18:26:19 +0200140 /* Broadcom devices with vendor specific id */
Petri Gynther10d4c672014-05-08 15:50:01 -0700141 { USB_VENDOR_AND_INTERFACE_INFO(0x0a5c, 0xff, 0x01, 0x01),
142 .driver_info = BTUSB_BCM_PATCHRAM },
Gustavo Padovan92c385f2012-08-06 15:36:49 -0300143
Marcel Holtmannc2aef6e2014-07-21 14:02:33 +0200144 /* ASUSTek Computer - Broadcom based */
Rick Dunn9a5abda2015-01-17 05:29:12 +0100145 { USB_VENDOR_AND_INTERFACE_INFO(0x0b05, 0xff, 0x01, 0x01),
146 .driver_info = BTUSB_BCM_PATCHRAM },
Marcel Holtmannc2aef6e2014-07-21 14:02:33 +0200147
Ken O'Brien5bcecf32013-09-21 19:14:43 +0100148 /* Belkin F8065bf - Broadcom based */
Marcel Holtmann6331c682015-03-27 15:11:41 -0700149 { USB_VENDOR_AND_INTERFACE_INFO(0x050d, 0xff, 0x01, 0x01),
150 .driver_info = BTUSB_BCM_PATCHRAM },
Ken O'Brien5bcecf32013-09-21 19:14:43 +0100151
Jurgen Kramer9113bfd2014-02-15 12:01:09 +0100152 /* IMC Networks - Broadcom based */
Marcel Holtmann6331c682015-03-27 15:11:41 -0700153 { USB_VENDOR_AND_INTERFACE_INFO(0x13d3, 0xff, 0x01, 0x01),
154 .driver_info = BTUSB_BCM_PATCHRAM },
Jurgen Kramer9113bfd2014-02-15 12:01:09 +0100155
Dmitry Tunin1623d0b2015-12-05 14:09:36 +0300156 /* Toshiba Corp - Broadcom based */
157 { USB_VENDOR_AND_INTERFACE_INFO(0x0930, 0xff, 0x01, 0x01),
158 .driver_info = BTUSB_BCM_PATCHRAM },
159
Marcel Holtmann40df7832014-07-06 13:29:58 +0200160 /* Intel Bluetooth USB Bootloader (RAM module) */
Marcel Holtmannd92f2df2014-07-06 14:53:55 +0200161 { USB_DEVICE(0x8087, 0x0a5a),
162 .driver_info = BTUSB_INTEL_BOOT | BTUSB_BROKEN_ISOC },
Marcel Holtmann40df7832014-07-06 13:29:58 +0200163
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200164 { } /* Terminating entry */
165};
166
167MODULE_DEVICE_TABLE(usb, btusb_table);
168
Marcel Holtmann54265202013-10-11 07:46:18 -0700169static const struct usb_device_id blacklist_table[] = {
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200170 /* CSR BlueCore devices */
171 { USB_DEVICE(0x0a12, 0x0001), .driver_info = BTUSB_CSR },
172
173 /* Broadcom BCM2033 without firmware */
174 { USB_DEVICE(0x0a5c, 0x2033), .driver_info = BTUSB_IGNORE },
175
Marcel Holtmann6c9d4352015-10-17 14:39:27 +0200176 /* Broadcom BCM2045 devices */
177 { USB_DEVICE(0x0a5c, 0x2045), .driver_info = BTUSB_BCM2045 },
178
Bala Shanmugambe931122010-11-26 17:35:46 +0530179 /* Atheros 3011 with sflash firmware */
Andy Shevchenko0b880062014-02-18 18:26:19 +0200180 { USB_DEVICE(0x0489, 0xe027), .driver_info = BTUSB_IGNORE },
181 { USB_DEVICE(0x0489, 0xe03d), .driver_info = BTUSB_IGNORE },
Alexander Ploumistos2eeff0b2015-02-13 21:05:11 +0200182 { USB_DEVICE(0x04f2, 0xaff1), .driver_info = BTUSB_IGNORE },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200183 { USB_DEVICE(0x0930, 0x0215), .driver_info = BTUSB_IGNORE },
Bala Shanmugambe931122010-11-26 17:35:46 +0530184 { USB_DEVICE(0x0cf3, 0x3002), .driver_info = BTUSB_IGNORE },
Marek Vasut6eda5412012-06-08 14:32:50 +0200185 { USB_DEVICE(0x0cf3, 0xe019), .driver_info = BTUSB_IGNORE },
Andy Ross2a7bccc2011-05-09 16:11:16 -0700186 { USB_DEVICE(0x13d3, 0x3304), .driver_info = BTUSB_IGNORE },
Bala Shanmugambe931122010-11-26 17:35:46 +0530187
Cho, Yu-Chen509e7862011-01-26 17:10:59 +0800188 /* Atheros AR9285 Malbec with sflash firmware */
189 { USB_DEVICE(0x03f0, 0x311d), .driver_info = BTUSB_IGNORE },
190
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530191 /* Atheros 3012 with sflash firmware */
Andy Shevchenko0b880062014-02-18 18:26:19 +0200192 { USB_DEVICE(0x0489, 0xe04d), .driver_info = BTUSB_ATH3012 },
193 { USB_DEVICE(0x0489, 0xe04e), .driver_info = BTUSB_ATH3012 },
194 { USB_DEVICE(0x0489, 0xe056), .driver_info = BTUSB_ATH3012 },
195 { USB_DEVICE(0x0489, 0xe057), .driver_info = BTUSB_ATH3012 },
196 { USB_DEVICE(0x0489, 0xe05f), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin692c0622015-06-06 20:25:40 +0300197 { USB_DEVICE(0x0489, 0xe076), .driver_info = BTUSB_ATH3012 },
Anantha Krishnan4b552bc2014-10-06 16:31:49 +0530198 { USB_DEVICE(0x0489, 0xe078), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin28c971d2016-02-10 00:49:11 +0300199 { USB_DEVICE(0x0489, 0xe095), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200200 { USB_DEVICE(0x04c5, 0x1330), .driver_info = BTUSB_ATH3012 },
201 { USB_DEVICE(0x04ca, 0x3004), .driver_info = BTUSB_ATH3012 },
202 { USB_DEVICE(0x04ca, 0x3005), .driver_info = BTUSB_ATH3012 },
203 { USB_DEVICE(0x04ca, 0x3006), .driver_info = BTUSB_ATH3012 },
Mohammed Habibulla1fb4e092014-04-17 11:37:13 -0700204 { USB_DEVICE(0x04ca, 0x3007), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200205 { USB_DEVICE(0x04ca, 0x3008), .driver_info = BTUSB_ATH3012 },
206 { USB_DEVICE(0x04ca, 0x300b), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin7e730c72015-06-18 20:41:51 +0300207 { USB_DEVICE(0x04ca, 0x300d), .driver_info = BTUSB_ATH3012 },
Dmitry Tuninec0810d2015-05-02 13:36:58 +0300208 { USB_DEVICE(0x04ca, 0x300f), .driver_info = BTUSB_ATH3012 },
Janne Heikkinen134d3b32014-12-09 07:44:51 +0200209 { USB_DEVICE(0x04ca, 0x3010), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin81d90442016-02-28 11:04:06 +0300210 { USB_DEVICE(0x04ca, 0x3014), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200211 { USB_DEVICE(0x0930, 0x0219), .driver_info = BTUSB_ATH3012 },
Dmitry Tunincd355ff2015-10-05 19:29:33 +0300212 { USB_DEVICE(0x0930, 0x021c), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200213 { USB_DEVICE(0x0930, 0x0220), .driver_info = BTUSB_ATH3012 },
Vincent Zwanenburg89d29752014-08-08 12:33:56 +0100214 { USB_DEVICE(0x0930, 0x0227), .driver_info = BTUSB_ATH3012 },
Andy Shevchenkoa735f9e2014-02-18 18:26:20 +0200215 { USB_DEVICE(0x0b05, 0x17d0), .driver_info = BTUSB_ATH3012 },
Ming Leid66629c2013-03-15 11:00:39 +0800216 { USB_DEVICE(0x0cf3, 0x0036), .driver_info = BTUSB_ATH3012 },
Steven.Li2d25f8b2011-07-01 14:02:36 +0800217 { USB_DEVICE(0x0cf3, 0x3004), .driver_info = BTUSB_ATH3012 },
Sunguk Lee94a32d102013-03-12 04:41:58 +0900218 { USB_DEVICE(0x0cf3, 0x3008), .driver_info = BTUSB_ATH3012 },
Cho, Yu-Chen07c0ea82012-03-14 22:01:21 +0200219 { USB_DEVICE(0x0cf3, 0x311d), .driver_info = BTUSB_ATH3012 },
Oliver Neukumb1312372014-01-16 15:37:11 +0100220 { USB_DEVICE(0x0cf3, 0x311e), .driver_info = BTUSB_ATH3012 },
Oliver Neukum1e56f1e2014-01-16 16:02:58 +0100221 { USB_DEVICE(0x0cf3, 0x311f), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200222 { USB_DEVICE(0x0cf3, 0x3121), .driver_info = BTUSB_ATH3012 },
Ming Leiebaf5792013-03-18 23:45:11 +0800223 { USB_DEVICE(0x0cf3, 0x817a), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin18e0afa2015-10-16 11:45:26 +0300224 { USB_DEVICE(0x0cf3, 0x817b), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200225 { USB_DEVICE(0x0cf3, 0xe003), .driver_info = BTUSB_ATH3012 },
AceLan Kaoac713112012-04-19 14:53:45 +0800226 { USB_DEVICE(0x0cf3, 0xe004), .driver_info = BTUSB_ATH3012 },
Peng Chen0a3658c2013-08-30 17:41:40 +0800227 { USB_DEVICE(0x0cf3, 0xe005), .driver_info = BTUSB_ATH3012 },
Wen-chien Jesse Sungca79f232015-05-13 11:39:24 +0800228 { USB_DEVICE(0x0cf3, 0xe006), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200229 { USB_DEVICE(0x13d3, 0x3362), .driver_info = BTUSB_ATH3012 },
230 { USB_DEVICE(0x13d3, 0x3375), .driver_info = BTUSB_ATH3012 },
AceLan Kaoeed307e2012-12-11 11:41:20 +0800231 { USB_DEVICE(0x13d3, 0x3393), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin609574e2016-02-10 15:33:17 +0300232 { USB_DEVICE(0x13d3, 0x3395), .driver_info = BTUSB_ATH3012 },
Sujith Manoharan5b77a1f2013-07-15 09:29:03 +0530233 { USB_DEVICE(0x13d3, 0x3402), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin3bb30a7c2014-11-25 20:19:52 +0300234 { USB_DEVICE(0x13d3, 0x3408), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin033efa92015-01-18 00:16:51 +0300235 { USB_DEVICE(0x13d3, 0x3423), .driver_info = BTUSB_ATH3012 },
Anantha Krishnanfa2f1392014-07-08 19:25:08 +0530236 { USB_DEVICE(0x13d3, 0x3432), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin75c6aca2016-03-04 01:32:19 +0300237 { USB_DEVICE(0x13d3, 0x3472), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin0d0cef62015-06-06 20:29:25 +0300238 { USB_DEVICE(0x13d3, 0x3474), .driver_info = BTUSB_ATH3012 },
Lauro Costa72f9f8b2016-05-09 17:36:11 -0300239 { USB_DEVICE(0x13d3, 0x3487), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin12d86892016-07-12 01:35:18 +0300240 { USB_DEVICE(0x13d3, 0x3490), .driver_info = BTUSB_ATH3012 },
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530241
Cho, Yu-Chene9036e32011-02-15 10:20:07 +0800242 /* Atheros AR5BBU12 with sflash firmware */
243 { USB_DEVICE(0x0489, 0xe02c), .driver_info = BTUSB_IGNORE },
244
Michael Gruetzner85d59722012-05-02 22:33:40 +0200245 /* Atheros AR5BBU12 with sflash firmware */
Yevgeniy Melnichukbc21fde2012-08-07 19:48:10 +0530246 { USB_DEVICE(0x0489, 0xe036), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200247 { USB_DEVICE(0x0489, 0xe03c), .driver_info = BTUSB_ATH3012 },
Michael Gruetzner85d59722012-05-02 22:33:40 +0200248
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +0000249 /* QCA ROME chipset */
Wen-chien Jesse Sung20541112015-05-13 11:39:25 +0800250 { USB_DEVICE(0x0cf3, 0xe007), .driver_info = BTUSB_QCA_ROME },
Kai-Heng Feng93519932016-08-16 12:50:06 +0800251 { USB_DEVICE(0x0cf3, 0xe009), .driver_info = BTUSB_QCA_ROME },
Marcel Holtmannc9e44472015-03-16 23:56:04 -0700252 { USB_DEVICE(0x0cf3, 0xe300), .driver_info = BTUSB_QCA_ROME },
253 { USB_DEVICE(0x0cf3, 0xe360), .driver_info = BTUSB_QCA_ROME },
Yvain THONNART47ca5892016-06-11 15:43:30 +0200254 { USB_DEVICE(0x0489, 0xe092), .driver_info = BTUSB_QCA_ROME },
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +0000255
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200256 /* Broadcom BCM2035 */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100257 { USB_DEVICE(0x0a5c, 0x2009), .driver_info = BTUSB_BCM92035 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200258 { USB_DEVICE(0x0a5c, 0x200a), .driver_info = BTUSB_WRONG_SCO_MTU },
259 { USB_DEVICE(0x0a5c, 0x2035), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200260
261 /* Broadcom BCM2045 */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100262 { USB_DEVICE(0x0a5c, 0x2039), .driver_info = BTUSB_WRONG_SCO_MTU },
263 { USB_DEVICE(0x0a5c, 0x2101), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmannbdbef3d2008-09-23 00:16:35 +0200264
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200265 /* IBM/Lenovo ThinkPad with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100266 { USB_DEVICE(0x0a5c, 0x201e), .driver_info = BTUSB_WRONG_SCO_MTU },
267 { USB_DEVICE(0x0a5c, 0x2110), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200268
269 /* HP laptop with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100270 { USB_DEVICE(0x03f0, 0x171d), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200271
272 /* Dell laptop with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100273 { USB_DEVICE(0x413c, 0x8126), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200274
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100275 /* Dell Wireless 370 and 410 devices */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100276 { USB_DEVICE(0x413c, 0x8152), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100277 { USB_DEVICE(0x413c, 0x8156), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200278
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100279 /* Belkin F8T012 and F8T013 devices */
280 { USB_DEVICE(0x050d, 0x0012), .driver_info = BTUSB_WRONG_SCO_MTU },
281 { USB_DEVICE(0x050d, 0x0013), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200282
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100283 /* Asus WL-BTD202 device */
284 { USB_DEVICE(0x0b05, 0x1715), .driver_info = BTUSB_WRONG_SCO_MTU },
285
286 /* Kensington Bluetooth USB adapter */
287 { USB_DEVICE(0x047d, 0x105e), .driver_info = BTUSB_WRONG_SCO_MTU },
288
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200289 /* RTX Telecom based adapters with buggy SCO support */
290 { USB_DEVICE(0x0400, 0x0807), .driver_info = BTUSB_BROKEN_ISOC },
291 { USB_DEVICE(0x0400, 0x080a), .driver_info = BTUSB_BROKEN_ISOC },
292
293 /* CONWISE Technology based adapters with buggy SCO support */
294 { USB_DEVICE(0x0e5e, 0x6622), .driver_info = BTUSB_BROKEN_ISOC },
295
Marcel Holtmann4fcef8e2015-01-01 17:34:37 -0800296 /* Roper Class 1 Bluetooth Dongle (Silicon Wave based) */
Aleksei Volkov2eeac872015-06-08 12:02:10 +0300297 { USB_DEVICE(0x1310, 0x0001), .driver_info = BTUSB_SWAVE },
Marcel Holtmann4fcef8e2015-01-01 17:34:37 -0800298
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200299 /* Digianswer devices */
300 { USB_DEVICE(0x08fd, 0x0001), .driver_info = BTUSB_DIGIANSWER },
301 { USB_DEVICE(0x08fd, 0x0002), .driver_info = BTUSB_IGNORE },
302
303 /* CSR BlueCore Bluetooth Sniffer */
Marcel Holtmann4f64fa82014-07-07 00:12:04 +0200304 { USB_DEVICE(0x0a12, 0x0002),
305 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200306
307 /* Frontline ComProbe Bluetooth Sniffer */
Marcel Holtmann4f64fa82014-07-07 00:12:04 +0200308 { USB_DEVICE(0x16d3, 0x0002),
309 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200310
Marcel Holtmanncb1ee892015-01-28 19:41:42 -0800311 /* Marvell Bluetooth devices */
312 { USB_DEVICE(0x1286, 0x2044), .driver_info = BTUSB_MARVELL },
313 { USB_DEVICE(0x1286, 0x2046), .driver_info = BTUSB_MARVELL },
314
Marcel Holtmannd0ac9eb2015-01-28 19:41:43 -0800315 /* Intel Bluetooth devices */
Marcel Holtmann407550f2015-02-22 15:41:18 -0800316 { USB_DEVICE(0x8087, 0x07da), .driver_info = BTUSB_CSR },
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -0700317 { USB_DEVICE(0x8087, 0x07dc), .driver_info = BTUSB_INTEL },
Tedd Ho-Jeong Anef4e5e42013-11-12 13:10:58 -0800318 { USB_DEVICE(0x8087, 0x0a2a), .driver_info = BTUSB_INTEL },
Marcel Holtmanncda0dd72015-01-26 21:33:48 -0800319 { USB_DEVICE(0x8087, 0x0a2b), .driver_info = BTUSB_INTEL_NEW },
Tedd Ho-Jeong An439e65d2016-06-20 13:43:40 -0700320 { USB_DEVICE(0x8087, 0x0aa7), .driver_info = BTUSB_INTEL },
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -0700321
Marcel Holtmannd0ac9eb2015-01-28 19:41:43 -0800322 /* Other Intel Bluetooth devices */
323 { USB_VENDOR_AND_INTERFACE_INFO(0x8087, 0xe0, 0x01, 0x01),
324 .driver_info = BTUSB_IGNORE },
Amitkumar Karwarae8df492014-07-18 14:47:06 -0700325
Daniel Drakea2698a92015-04-16 14:09:55 -0600326 /* Realtek Bluetooth devices */
327 { USB_VENDOR_AND_INTERFACE_INFO(0x0bda, 0xe0, 0x01, 0x01),
328 .driver_info = BTUSB_REALTEK },
329
330 /* Additional Realtek 8723AE Bluetooth devices */
331 { USB_DEVICE(0x0930, 0x021d), .driver_info = BTUSB_REALTEK },
332 { USB_DEVICE(0x13d3, 0x3394), .driver_info = BTUSB_REALTEK },
333
334 /* Additional Realtek 8723BE Bluetooth devices */
335 { USB_DEVICE(0x0489, 0xe085), .driver_info = BTUSB_REALTEK },
336 { USB_DEVICE(0x0489, 0xe08b), .driver_info = BTUSB_REALTEK },
337 { USB_DEVICE(0x13d3, 0x3410), .driver_info = BTUSB_REALTEK },
338 { USB_DEVICE(0x13d3, 0x3416), .driver_info = BTUSB_REALTEK },
339 { USB_DEVICE(0x13d3, 0x3459), .driver_info = BTUSB_REALTEK },
340
341 /* Additional Realtek 8821AE Bluetooth devices */
342 { USB_DEVICE(0x0b05, 0x17dc), .driver_info = BTUSB_REALTEK },
343 { USB_DEVICE(0x13d3, 0x3414), .driver_info = BTUSB_REALTEK },
344 { USB_DEVICE(0x13d3, 0x3458), .driver_info = BTUSB_REALTEK },
345 { USB_DEVICE(0x13d3, 0x3461), .driver_info = BTUSB_REALTEK },
346 { USB_DEVICE(0x13d3, 0x3462), .driver_info = BTUSB_REALTEK },
347
Peter Poklop4481c072015-08-15 20:47:09 +0200348 /* Silicon Wave based devices */
349 { USB_DEVICE(0x0c10, 0x0000), .driver_info = BTUSB_SWAVE },
350
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200351 { } /* Terminating entry */
352};
353
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200354#define BTUSB_MAX_ISOC_FRAMES 10
355
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200356#define BTUSB_INTR_RUNNING 0
357#define BTUSB_BULK_RUNNING 1
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200358#define BTUSB_ISOC_RUNNING 2
Oliver Neukum7bee5492009-08-24 23:44:59 +0200359#define BTUSB_SUSPENDING 3
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -0300360#define BTUSB_DID_ISO_RESUME 4
Marcel Holtmanncda0dd72015-01-26 21:33:48 -0800361#define BTUSB_BOOTLOADER 5
362#define BTUSB_DOWNLOADING 6
Marcel Holtmannce6bb922015-01-28 01:58:40 -0800363#define BTUSB_FIRMWARE_LOADED 7
Marcel Holtmanncda0dd72015-01-26 21:33:48 -0800364#define BTUSB_FIRMWARE_FAILED 8
Marcel Holtmannce6bb922015-01-28 01:58:40 -0800365#define BTUSB_BOOTING 9
Daniel Drake04b8c812015-05-21 08:23:50 -0600366#define BTUSB_RESET_RESUME 10
Marcel Holtmann9d08f502015-10-08 20:23:08 +0200367#define BTUSB_DIAG_RUNNING 11
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200368
369struct btusb_data {
370 struct hci_dev *hdev;
371 struct usb_device *udev;
Marcel Holtmann5fbcd262008-09-23 00:16:36 +0200372 struct usb_interface *intf;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200373 struct usb_interface *isoc;
Marcel Holtmann9d08f502015-10-08 20:23:08 +0200374 struct usb_interface *diag;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200375
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200376 unsigned long flags;
377
378 struct work_struct work;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200379 struct work_struct waker;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200380
Marcel Holtmann803b5832014-09-16 08:00:29 +0200381 struct usb_anchor deferred;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200382 struct usb_anchor tx_anchor;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200383 int tx_in_flight;
384 spinlock_t txlock;
385
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200386 struct usb_anchor intr_anchor;
387 struct usb_anchor bulk_anchor;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200388 struct usb_anchor isoc_anchor;
Marcel Holtmann9d08f502015-10-08 20:23:08 +0200389 struct usb_anchor diag_anchor;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200390 spinlock_t rxlock;
391
392 struct sk_buff *evt_skb;
393 struct sk_buff *acl_skb;
394 struct sk_buff *sco_skb;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200395
396 struct usb_endpoint_descriptor *intr_ep;
397 struct usb_endpoint_descriptor *bulk_tx_ep;
398 struct usb_endpoint_descriptor *bulk_rx_ep;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200399 struct usb_endpoint_descriptor *isoc_tx_ep;
400 struct usb_endpoint_descriptor *isoc_rx_ep;
Marcel Holtmann9d08f502015-10-08 20:23:08 +0200401 struct usb_endpoint_descriptor *diag_tx_ep;
402 struct usb_endpoint_descriptor *diag_rx_ep;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200403
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100404 __u8 cmdreq_type;
Marcel Holtmann893ba542015-01-28 20:27:34 -0800405 __u8 cmdreq;
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100406
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100407 unsigned int sco_num;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200408 int isoc_altsetting;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +0100409 int suspend_count;
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +0100410
Marcel Holtmann97307f52015-01-12 13:51:10 -0800411 int (*recv_event)(struct hci_dev *hdev, struct sk_buff *skb);
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +0100412 int (*recv_bulk)(struct btusb_data *data, void *buffer, int count);
Kim, Ben Young Taeace31982015-02-15 23:06:14 +0000413
414 int (*setup_on_usb)(struct hci_dev *hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200415};
416
Marcel Holtmann803b5832014-09-16 08:00:29 +0200417static inline void btusb_free_frags(struct btusb_data *data)
418{
419 unsigned long flags;
420
421 spin_lock_irqsave(&data->rxlock, flags);
422
423 kfree_skb(data->evt_skb);
424 data->evt_skb = NULL;
425
426 kfree_skb(data->acl_skb);
427 data->acl_skb = NULL;
428
429 kfree_skb(data->sco_skb);
430 data->sco_skb = NULL;
431
432 spin_unlock_irqrestore(&data->rxlock, flags);
433}
434
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200435static int btusb_recv_intr(struct btusb_data *data, void *buffer, int count)
436{
Marcel Holtmann803b5832014-09-16 08:00:29 +0200437 struct sk_buff *skb;
438 int err = 0;
439
440 spin_lock(&data->rxlock);
441 skb = data->evt_skb;
442
443 while (count) {
444 int len;
445
446 if (!skb) {
447 skb = bt_skb_alloc(HCI_MAX_EVENT_SIZE, GFP_ATOMIC);
448 if (!skb) {
449 err = -ENOMEM;
450 break;
451 }
452
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100453 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
454 hci_skb_expect(skb) = HCI_EVENT_HDR_SIZE;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200455 }
456
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100457 len = min_t(uint, hci_skb_expect(skb), count);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200458 memcpy(skb_put(skb, len), buffer, len);
459
460 count -= len;
461 buffer += len;
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100462 hci_skb_expect(skb) -= len;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200463
464 if (skb->len == HCI_EVENT_HDR_SIZE) {
465 /* Complete event header */
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100466 hci_skb_expect(skb) = hci_event_hdr(skb)->plen;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200467
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100468 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
Marcel Holtmann803b5832014-09-16 08:00:29 +0200469 kfree_skb(skb);
470 skb = NULL;
471
472 err = -EILSEQ;
473 break;
474 }
475 }
476
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100477 if (!hci_skb_expect(skb)) {
Marcel Holtmann803b5832014-09-16 08:00:29 +0200478 /* Complete frame */
Marcel Holtmann97307f52015-01-12 13:51:10 -0800479 data->recv_event(data->hdev, skb);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200480 skb = NULL;
481 }
482 }
483
484 data->evt_skb = skb;
485 spin_unlock(&data->rxlock);
486
487 return err;
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200488}
489
490static int btusb_recv_bulk(struct btusb_data *data, void *buffer, int count)
491{
Marcel Holtmann803b5832014-09-16 08:00:29 +0200492 struct sk_buff *skb;
493 int err = 0;
494
495 spin_lock(&data->rxlock);
496 skb = data->acl_skb;
497
498 while (count) {
499 int len;
500
501 if (!skb) {
502 skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
503 if (!skb) {
504 err = -ENOMEM;
505 break;
506 }
507
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100508 hci_skb_pkt_type(skb) = HCI_ACLDATA_PKT;
509 hci_skb_expect(skb) = HCI_ACL_HDR_SIZE;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200510 }
511
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100512 len = min_t(uint, hci_skb_expect(skb), count);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200513 memcpy(skb_put(skb, len), buffer, len);
514
515 count -= len;
516 buffer += len;
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100517 hci_skb_expect(skb) -= len;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200518
519 if (skb->len == HCI_ACL_HDR_SIZE) {
520 __le16 dlen = hci_acl_hdr(skb)->dlen;
521
522 /* Complete ACL header */
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100523 hci_skb_expect(skb) = __le16_to_cpu(dlen);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200524
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100525 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
Marcel Holtmann803b5832014-09-16 08:00:29 +0200526 kfree_skb(skb);
527 skb = NULL;
528
529 err = -EILSEQ;
530 break;
531 }
532 }
533
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100534 if (!hci_skb_expect(skb)) {
Marcel Holtmann803b5832014-09-16 08:00:29 +0200535 /* Complete frame */
536 hci_recv_frame(data->hdev, skb);
537 skb = NULL;
538 }
539 }
540
541 data->acl_skb = skb;
542 spin_unlock(&data->rxlock);
543
544 return err;
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200545}
546
547static int btusb_recv_isoc(struct btusb_data *data, void *buffer, int count)
548{
Marcel Holtmann803b5832014-09-16 08:00:29 +0200549 struct sk_buff *skb;
550 int err = 0;
551
552 spin_lock(&data->rxlock);
553 skb = data->sco_skb;
554
555 while (count) {
556 int len;
557
558 if (!skb) {
559 skb = bt_skb_alloc(HCI_MAX_SCO_SIZE, GFP_ATOMIC);
560 if (!skb) {
561 err = -ENOMEM;
562 break;
563 }
564
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100565 hci_skb_pkt_type(skb) = HCI_SCODATA_PKT;
566 hci_skb_expect(skb) = HCI_SCO_HDR_SIZE;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200567 }
568
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100569 len = min_t(uint, hci_skb_expect(skb), count);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200570 memcpy(skb_put(skb, len), buffer, len);
571
572 count -= len;
573 buffer += len;
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100574 hci_skb_expect(skb) -= len;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200575
576 if (skb->len == HCI_SCO_HDR_SIZE) {
577 /* Complete SCO header */
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100578 hci_skb_expect(skb) = hci_sco_hdr(skb)->dlen;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200579
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100580 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
Marcel Holtmann803b5832014-09-16 08:00:29 +0200581 kfree_skb(skb);
582 skb = NULL;
583
584 err = -EILSEQ;
585 break;
586 }
587 }
588
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100589 if (!hci_skb_expect(skb)) {
Marcel Holtmann803b5832014-09-16 08:00:29 +0200590 /* Complete frame */
591 hci_recv_frame(data->hdev, skb);
592 skb = NULL;
593 }
594 }
595
596 data->sco_skb = skb;
597 spin_unlock(&data->rxlock);
598
599 return err;
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200600}
601
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200602static void btusb_intr_complete(struct urb *urb)
603{
604 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +0100605 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200606 int err;
607
Marcel Holtmann89e75332014-09-16 04:44:50 +0200608 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
609 urb->actual_length);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200610
611 if (!test_bit(HCI_RUNNING, &hdev->flags))
612 return;
613
614 if (urb->status == 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200615 hdev->stat.byte_rx += urb->actual_length;
616
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200617 if (btusb_recv_intr(data, urb->transfer_buffer,
618 urb->actual_length) < 0) {
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200619 BT_ERR("%s corrupted event packet", hdev->name);
620 hdev->stat.err_rx++;
621 }
Champion Chen85560c42014-09-06 14:06:08 -0500622 } else if (urb->status == -ENOENT) {
623 /* Avoid suspend failed when usb_kill_urb */
624 return;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200625 }
626
627 if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
628 return;
629
Oliver Neukum7bee5492009-08-24 23:44:59 +0200630 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200631 usb_anchor_urb(urb, &data->intr_anchor);
632
633 err = usb_submit_urb(urb, GFP_ATOMIC);
634 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200635 /* -EPERM: urb is being killed;
636 * -ENODEV: device got disconnected */
637 if (err != -EPERM && err != -ENODEV)
Stefan Seyfried61faddf2010-11-30 21:49:08 +0100638 BT_ERR("%s urb %p failed to resubmit (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200639 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200640 usb_unanchor_urb(urb);
641 }
642}
643
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100644static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200645{
David Herrmann155961e2012-02-09 21:58:32 +0100646 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200647 struct urb *urb;
648 unsigned char *buf;
649 unsigned int pipe;
650 int err, size;
651
652 BT_DBG("%s", hdev->name);
653
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200654 if (!data->intr_ep)
655 return -ENODEV;
656
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100657 urb = usb_alloc_urb(0, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200658 if (!urb)
659 return -ENOMEM;
660
661 size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
662
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100663 buf = kmalloc(size, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200664 if (!buf) {
665 usb_free_urb(urb);
666 return -ENOMEM;
667 }
668
669 pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
670
671 usb_fill_int_urb(urb, data->udev, pipe, buf, size,
Marcel Holtmann89e75332014-09-16 04:44:50 +0200672 btusb_intr_complete, hdev, data->intr_ep->bInterval);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200673
674 urb->transfer_flags |= URB_FREE_BUFFER;
675
676 usb_anchor_urb(urb, &data->intr_anchor);
677
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100678 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200679 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +0200680 if (err != -EPERM && err != -ENODEV)
681 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200682 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200683 usb_unanchor_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200684 }
685
686 usb_free_urb(urb);
687
688 return err;
689}
690
691static void btusb_bulk_complete(struct urb *urb)
692{
693 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +0100694 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200695 int err;
696
Marcel Holtmann89e75332014-09-16 04:44:50 +0200697 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
698 urb->actual_length);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200699
700 if (!test_bit(HCI_RUNNING, &hdev->flags))
701 return;
702
703 if (urb->status == 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200704 hdev->stat.byte_rx += urb->actual_length;
705
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +0100706 if (data->recv_bulk(data, urb->transfer_buffer,
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200707 urb->actual_length) < 0) {
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200708 BT_ERR("%s corrupted ACL packet", hdev->name);
709 hdev->stat.err_rx++;
710 }
Champion Chen85560c42014-09-06 14:06:08 -0500711 } else if (urb->status == -ENOENT) {
712 /* Avoid suspend failed when usb_kill_urb */
713 return;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200714 }
715
716 if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
717 return;
718
719 usb_anchor_urb(urb, &data->bulk_anchor);
Oliver Neukum652fd782009-12-16 19:23:43 +0100720 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200721
722 err = usb_submit_urb(urb, GFP_ATOMIC);
723 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200724 /* -EPERM: urb is being killed;
725 * -ENODEV: device got disconnected */
726 if (err != -EPERM && err != -ENODEV)
Stefan Seyfried61faddf2010-11-30 21:49:08 +0100727 BT_ERR("%s urb %p failed to resubmit (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200728 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200729 usb_unanchor_urb(urb);
730 }
731}
732
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100733static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200734{
David Herrmann155961e2012-02-09 21:58:32 +0100735 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200736 struct urb *urb;
737 unsigned char *buf;
738 unsigned int pipe;
Vikram Kandukuri290ba202009-07-02 14:31:59 +0530739 int err, size = HCI_MAX_FRAME_SIZE;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200740
741 BT_DBG("%s", hdev->name);
742
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200743 if (!data->bulk_rx_ep)
744 return -ENODEV;
745
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100746 urb = usb_alloc_urb(0, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200747 if (!urb)
748 return -ENOMEM;
749
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100750 buf = kmalloc(size, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200751 if (!buf) {
752 usb_free_urb(urb);
753 return -ENOMEM;
754 }
755
756 pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
757
Marcel Holtmann89e75332014-09-16 04:44:50 +0200758 usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
759 btusb_bulk_complete, hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200760
761 urb->transfer_flags |= URB_FREE_BUFFER;
762
Oliver Neukum7bee5492009-08-24 23:44:59 +0200763 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200764 usb_anchor_urb(urb, &data->bulk_anchor);
765
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100766 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200767 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +0200768 if (err != -EPERM && err != -ENODEV)
769 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200770 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200771 usb_unanchor_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200772 }
773
774 usb_free_urb(urb);
775
776 return err;
777}
778
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200779static void btusb_isoc_complete(struct urb *urb)
780{
781 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +0100782 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200783 int i, err;
784
Marcel Holtmann89e75332014-09-16 04:44:50 +0200785 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
786 urb->actual_length);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200787
788 if (!test_bit(HCI_RUNNING, &hdev->flags))
789 return;
790
791 if (urb->status == 0) {
792 for (i = 0; i < urb->number_of_packets; i++) {
793 unsigned int offset = urb->iso_frame_desc[i].offset;
794 unsigned int length = urb->iso_frame_desc[i].actual_length;
795
796 if (urb->iso_frame_desc[i].status)
797 continue;
798
799 hdev->stat.byte_rx += length;
800
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200801 if (btusb_recv_isoc(data, urb->transfer_buffer + offset,
802 length) < 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200803 BT_ERR("%s corrupted SCO packet", hdev->name);
804 hdev->stat.err_rx++;
805 }
806 }
Champion Chen85560c42014-09-06 14:06:08 -0500807 } else if (urb->status == -ENOENT) {
808 /* Avoid suspend failed when usb_kill_urb */
809 return;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200810 }
811
812 if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags))
813 return;
814
815 usb_anchor_urb(urb, &data->isoc_anchor);
816
817 err = usb_submit_urb(urb, GFP_ATOMIC);
818 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200819 /* -EPERM: urb is being killed;
820 * -ENODEV: device got disconnected */
821 if (err != -EPERM && err != -ENODEV)
Stefan Seyfried61faddf2010-11-30 21:49:08 +0100822 BT_ERR("%s urb %p failed to resubmit (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200823 hdev->name, urb, -err);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200824 usb_unanchor_urb(urb);
825 }
826}
827
Jesper Juhl42b16b32011-01-17 00:09:38 +0100828static inline void __fill_isoc_descriptor(struct urb *urb, int len, int mtu)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200829{
830 int i, offset = 0;
831
832 BT_DBG("len %d mtu %d", len, mtu);
833
834 for (i = 0; i < BTUSB_MAX_ISOC_FRAMES && len >= mtu;
835 i++, offset += mtu, len -= mtu) {
836 urb->iso_frame_desc[i].offset = offset;
837 urb->iso_frame_desc[i].length = mtu;
838 }
839
840 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
841 urb->iso_frame_desc[i].offset = offset;
842 urb->iso_frame_desc[i].length = len;
843 i++;
844 }
845
846 urb->number_of_packets = i;
847}
848
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100849static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200850{
David Herrmann155961e2012-02-09 21:58:32 +0100851 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200852 struct urb *urb;
853 unsigned char *buf;
854 unsigned int pipe;
855 int err, size;
856
857 BT_DBG("%s", hdev->name);
858
859 if (!data->isoc_rx_ep)
860 return -ENODEV;
861
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100862 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200863 if (!urb)
864 return -ENOMEM;
865
866 size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) *
867 BTUSB_MAX_ISOC_FRAMES;
868
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100869 buf = kmalloc(size, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200870 if (!buf) {
871 usb_free_urb(urb);
872 return -ENOMEM;
873 }
874
875 pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress);
876
Bing Zhaofa0fb932011-12-20 18:19:00 -0800877 usb_fill_int_urb(urb, data->udev, pipe, buf, size, btusb_isoc_complete,
Marcel Holtmann89e75332014-09-16 04:44:50 +0200878 hdev, data->isoc_rx_ep->bInterval);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200879
Marcel Holtmann89e75332014-09-16 04:44:50 +0200880 urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200881
882 __fill_isoc_descriptor(urb, size,
Marcel Holtmann89e75332014-09-16 04:44:50 +0200883 le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize));
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200884
885 usb_anchor_urb(urb, &data->isoc_anchor);
886
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100887 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200888 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +0200889 if (err != -EPERM && err != -ENODEV)
890 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200891 hdev->name, urb, -err);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200892 usb_unanchor_urb(urb);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200893 }
894
895 usb_free_urb(urb);
896
897 return err;
898}
899
Marcel Holtmann9d08f502015-10-08 20:23:08 +0200900static void btusb_diag_complete(struct urb *urb)
901{
902 struct hci_dev *hdev = urb->context;
903 struct btusb_data *data = hci_get_drvdata(hdev);
904 int err;
905
906 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
907 urb->actual_length);
908
909 if (urb->status == 0) {
910 struct sk_buff *skb;
911
912 skb = bt_skb_alloc(urb->actual_length, GFP_ATOMIC);
913 if (skb) {
914 memcpy(skb_put(skb, urb->actual_length),
915 urb->transfer_buffer, urb->actual_length);
916 hci_recv_diag(hdev, skb);
917 }
918 } else if (urb->status == -ENOENT) {
919 /* Avoid suspend failed when usb_kill_urb */
920 return;
921 }
922
923 if (!test_bit(BTUSB_DIAG_RUNNING, &data->flags))
924 return;
925
926 usb_anchor_urb(urb, &data->diag_anchor);
927 usb_mark_last_busy(data->udev);
928
929 err = usb_submit_urb(urb, GFP_ATOMIC);
930 if (err < 0) {
931 /* -EPERM: urb is being killed;
932 * -ENODEV: device got disconnected */
933 if (err != -EPERM && err != -ENODEV)
934 BT_ERR("%s urb %p failed to resubmit (%d)",
935 hdev->name, urb, -err);
936 usb_unanchor_urb(urb);
937 }
938}
939
940static int btusb_submit_diag_urb(struct hci_dev *hdev, gfp_t mem_flags)
941{
942 struct btusb_data *data = hci_get_drvdata(hdev);
943 struct urb *urb;
944 unsigned char *buf;
945 unsigned int pipe;
946 int err, size = HCI_MAX_FRAME_SIZE;
947
948 BT_DBG("%s", hdev->name);
949
950 if (!data->diag_rx_ep)
951 return -ENODEV;
952
953 urb = usb_alloc_urb(0, mem_flags);
954 if (!urb)
955 return -ENOMEM;
956
957 buf = kmalloc(size, mem_flags);
958 if (!buf) {
959 usb_free_urb(urb);
960 return -ENOMEM;
961 }
962
963 pipe = usb_rcvbulkpipe(data->udev, data->diag_rx_ep->bEndpointAddress);
964
965 usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
966 btusb_diag_complete, hdev);
967
968 urb->transfer_flags |= URB_FREE_BUFFER;
969
970 usb_mark_last_busy(data->udev);
971 usb_anchor_urb(urb, &data->diag_anchor);
972
973 err = usb_submit_urb(urb, mem_flags);
974 if (err < 0) {
975 if (err != -EPERM && err != -ENODEV)
976 BT_ERR("%s urb %p submission failed (%d)",
977 hdev->name, urb, -err);
978 usb_unanchor_urb(urb);
979 }
980
981 usb_free_urb(urb);
982
983 return err;
984}
985
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200986static void btusb_tx_complete(struct urb *urb)
987{
988 struct sk_buff *skb = urb->context;
Marcel Holtmann89e75332014-09-16 04:44:50 +0200989 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
David Herrmann155961e2012-02-09 21:58:32 +0100990 struct btusb_data *data = hci_get_drvdata(hdev);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200991
Marcel Holtmann89e75332014-09-16 04:44:50 +0200992 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
993 urb->actual_length);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200994
995 if (!test_bit(HCI_RUNNING, &hdev->flags))
996 goto done;
997
998 if (!urb->status)
999 hdev->stat.byte_tx += urb->transfer_buffer_length;
1000 else
1001 hdev->stat.err_tx++;
1002
1003done:
1004 spin_lock(&data->txlock);
1005 data->tx_in_flight--;
1006 spin_unlock(&data->txlock);
1007
1008 kfree(urb->setup_packet);
1009
1010 kfree_skb(skb);
1011}
1012
1013static void btusb_isoc_tx_complete(struct urb *urb)
1014{
1015 struct sk_buff *skb = urb->context;
Marcel Holtmann89e75332014-09-16 04:44:50 +02001016 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001017
Marcel Holtmann89e75332014-09-16 04:44:50 +02001018 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1019 urb->actual_length);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001020
1021 if (!test_bit(HCI_RUNNING, &hdev->flags))
1022 goto done;
1023
1024 if (!urb->status)
1025 hdev->stat.byte_tx += urb->transfer_buffer_length;
1026 else
1027 hdev->stat.err_tx++;
1028
1029done:
1030 kfree(urb->setup_packet);
1031
1032 kfree_skb(skb);
1033}
1034
1035static int btusb_open(struct hci_dev *hdev)
1036{
David Herrmann155961e2012-02-09 21:58:32 +01001037 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001038 int err;
1039
1040 BT_DBG("%s", hdev->name);
1041
Kim, Ben Young Taeace31982015-02-15 23:06:14 +00001042 /* Patching USB firmware files prior to starting any URBs of HCI path
1043 * It is more safe to use USB bulk channel for downloading USB patch
1044 */
1045 if (data->setup_on_usb) {
1046 err = data->setup_on_usb(hdev);
Marcel Holtmanneb500422015-04-16 23:15:50 +02001047 if (err < 0)
Kim, Ben Young Taeace31982015-02-15 23:06:14 +00001048 return err;
1049 }
1050
Oliver Neukum7bee5492009-08-24 23:44:59 +02001051 err = usb_autopm_get_interface(data->intf);
1052 if (err < 0)
1053 return err;
1054
1055 data->intf->needs_remote_wakeup = 1;
1056
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001057 if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +02001058 goto done;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001059
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001060 err = btusb_submit_intr_urb(hdev, GFP_KERNEL);
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001061 if (err < 0)
1062 goto failed;
1063
1064 err = btusb_submit_bulk_urb(hdev, GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001065 if (err < 0) {
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001066 usb_kill_anchored_urbs(&data->intr_anchor);
1067 goto failed;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001068 }
1069
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001070 set_bit(BTUSB_BULK_RUNNING, &data->flags);
1071 btusb_submit_bulk_urb(hdev, GFP_KERNEL);
1072
Marcel Holtmann9d08f502015-10-08 20:23:08 +02001073 if (data->diag) {
1074 if (!btusb_submit_diag_urb(hdev, GFP_KERNEL))
1075 set_bit(BTUSB_DIAG_RUNNING, &data->flags);
1076 }
1077
Oliver Neukum7bee5492009-08-24 23:44:59 +02001078done:
1079 usb_autopm_put_interface(data->intf);
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001080 return 0;
1081
1082failed:
1083 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001084 usb_autopm_put_interface(data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001085 return err;
1086}
1087
Oliver Neukum7bee5492009-08-24 23:44:59 +02001088static void btusb_stop_traffic(struct btusb_data *data)
1089{
1090 usb_kill_anchored_urbs(&data->intr_anchor);
1091 usb_kill_anchored_urbs(&data->bulk_anchor);
1092 usb_kill_anchored_urbs(&data->isoc_anchor);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02001093 usb_kill_anchored_urbs(&data->diag_anchor);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001094}
1095
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001096static int btusb_close(struct hci_dev *hdev)
1097{
David Herrmann155961e2012-02-09 21:58:32 +01001098 struct btusb_data *data = hci_get_drvdata(hdev);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001099 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001100
1101 BT_DBG("%s", hdev->name);
1102
Marcel Holtmanne8c3c3d2008-09-23 00:16:36 +02001103 cancel_work_sync(&data->work);
Linus Torvalds404291a2009-11-11 13:32:29 -08001104 cancel_work_sync(&data->waker);
Marcel Holtmanne8c3c3d2008-09-23 00:16:36 +02001105
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001106 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001107 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001108 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02001109 clear_bit(BTUSB_DIAG_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001110
1111 btusb_stop_traffic(data);
Marcel Holtmann803b5832014-09-16 08:00:29 +02001112 btusb_free_frags(data);
1113
Oliver Neukum7bee5492009-08-24 23:44:59 +02001114 err = usb_autopm_get_interface(data->intf);
1115 if (err < 0)
Oliver Neukum7b8e2c12009-11-13 14:26:23 +01001116 goto failed;
Oliver Neukum7bee5492009-08-24 23:44:59 +02001117
1118 data->intf->needs_remote_wakeup = 0;
1119 usb_autopm_put_interface(data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001120
Oliver Neukum7b8e2c12009-11-13 14:26:23 +01001121failed:
1122 usb_scuttle_anchored_urbs(&data->deferred);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001123 return 0;
1124}
1125
1126static int btusb_flush(struct hci_dev *hdev)
1127{
David Herrmann155961e2012-02-09 21:58:32 +01001128 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001129
1130 BT_DBG("%s", hdev->name);
1131
1132 usb_kill_anchored_urbs(&data->tx_anchor);
Marcel Holtmann803b5832014-09-16 08:00:29 +02001133 btusb_free_frags(data);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001134
1135 return 0;
1136}
1137
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001138static struct urb *alloc_ctrl_urb(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001139{
David Herrmann155961e2012-02-09 21:58:32 +01001140 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001141 struct usb_ctrlrequest *dr;
1142 struct urb *urb;
1143 unsigned int pipe;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001144
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001145 urb = usb_alloc_urb(0, GFP_KERNEL);
1146 if (!urb)
1147 return ERR_PTR(-ENOMEM);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001148
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001149 dr = kmalloc(sizeof(*dr), GFP_KERNEL);
1150 if (!dr) {
1151 usb_free_urb(urb);
1152 return ERR_PTR(-ENOMEM);
1153 }
1154
1155 dr->bRequestType = data->cmdreq_type;
Marcel Holtmann893ba542015-01-28 20:27:34 -08001156 dr->bRequest = data->cmdreq;
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001157 dr->wIndex = 0;
1158 dr->wValue = 0;
1159 dr->wLength = __cpu_to_le16(skb->len);
1160
1161 pipe = usb_sndctrlpipe(data->udev, 0x00);
1162
Marcel Holtmann89e75332014-09-16 04:44:50 +02001163 usb_fill_control_urb(urb, data->udev, pipe, (void *)dr,
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001164 skb->data, skb->len, btusb_tx_complete, skb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001165
Marcel Holtmann89e75332014-09-16 04:44:50 +02001166 skb->dev = (void *)hdev;
Marcel Holtmann7bd8f092013-10-11 06:19:18 -07001167
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001168 return urb;
1169}
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001170
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001171static struct urb *alloc_bulk_urb(struct hci_dev *hdev, struct sk_buff *skb)
1172{
1173 struct btusb_data *data = hci_get_drvdata(hdev);
1174 struct urb *urb;
1175 unsigned int pipe;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001176
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001177 if (!data->bulk_tx_ep)
1178 return ERR_PTR(-ENODEV);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001179
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001180 urb = usb_alloc_urb(0, GFP_KERNEL);
1181 if (!urb)
1182 return ERR_PTR(-ENOMEM);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001183
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001184 pipe = usb_sndbulkpipe(data->udev, data->bulk_tx_ep->bEndpointAddress);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001185
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001186 usb_fill_bulk_urb(urb, data->udev, pipe,
1187 skb->data, skb->len, btusb_tx_complete, skb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001188
Marcel Holtmann89e75332014-09-16 04:44:50 +02001189 skb->dev = (void *)hdev;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001190
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001191 return urb;
1192}
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001193
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001194static struct urb *alloc_isoc_urb(struct hci_dev *hdev, struct sk_buff *skb)
1195{
1196 struct btusb_data *data = hci_get_drvdata(hdev);
1197 struct urb *urb;
1198 unsigned int pipe;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001199
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001200 if (!data->isoc_tx_ep)
1201 return ERR_PTR(-ENODEV);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001202
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001203 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_KERNEL);
1204 if (!urb)
1205 return ERR_PTR(-ENOMEM);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001206
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001207 pipe = usb_sndisocpipe(data->udev, data->isoc_tx_ep->bEndpointAddress);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001208
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001209 usb_fill_int_urb(urb, data->udev, pipe,
1210 skb->data, skb->len, btusb_isoc_tx_complete,
1211 skb, data->isoc_tx_ep->bInterval);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001212
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001213 urb->transfer_flags = URB_ISO_ASAP;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001214
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001215 __fill_isoc_descriptor(urb, skb->len,
1216 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001217
Marcel Holtmann89e75332014-09-16 04:44:50 +02001218 skb->dev = (void *)hdev;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001219
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001220 return urb;
1221}
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001222
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001223static int submit_tx_urb(struct hci_dev *hdev, struct urb *urb)
1224{
1225 struct btusb_data *data = hci_get_drvdata(hdev);
1226 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001227
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001228 usb_anchor_urb(urb, &data->tx_anchor);
1229
Johan Hedberge9753ef2014-09-14 08:49:34 +03001230 err = usb_submit_urb(urb, GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001231 if (err < 0) {
Paul Bolle5a9b80e2011-10-09 12:12:16 +02001232 if (err != -EPERM && err != -ENODEV)
1233 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +02001234 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001235 kfree(urb->setup_packet);
1236 usb_unanchor_urb(urb);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001237 } else {
1238 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001239 }
1240
Cong Wang54a8a792011-11-22 09:32:57 +08001241 usb_free_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001242 return err;
1243}
1244
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001245static int submit_or_queue_tx_urb(struct hci_dev *hdev, struct urb *urb)
1246{
1247 struct btusb_data *data = hci_get_drvdata(hdev);
1248 unsigned long flags;
1249 bool suspending;
1250
1251 spin_lock_irqsave(&data->txlock, flags);
1252 suspending = test_bit(BTUSB_SUSPENDING, &data->flags);
1253 if (!suspending)
1254 data->tx_in_flight++;
1255 spin_unlock_irqrestore(&data->txlock, flags);
1256
1257 if (!suspending)
1258 return submit_tx_urb(hdev, urb);
1259
1260 usb_anchor_urb(urb, &data->deferred);
1261 schedule_work(&data->waker);
1262
1263 usb_free_urb(urb);
1264 return 0;
1265}
1266
1267static int btusb_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
1268{
1269 struct urb *urb;
1270
1271 BT_DBG("%s", hdev->name);
1272
Marcel Holtmann618e8bc2015-11-05 07:33:56 +01001273 switch (hci_skb_pkt_type(skb)) {
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001274 case HCI_COMMAND_PKT:
1275 urb = alloc_ctrl_urb(hdev, skb);
1276 if (IS_ERR(urb))
1277 return PTR_ERR(urb);
1278
1279 hdev->stat.cmd_tx++;
1280 return submit_or_queue_tx_urb(hdev, urb);
1281
1282 case HCI_ACLDATA_PKT:
1283 urb = alloc_bulk_urb(hdev, skb);
1284 if (IS_ERR(urb))
1285 return PTR_ERR(urb);
1286
1287 hdev->stat.acl_tx++;
1288 return submit_or_queue_tx_urb(hdev, urb);
1289
1290 case HCI_SCODATA_PKT:
1291 if (hci_conn_num(hdev, SCO_LINK) < 1)
1292 return -ENODEV;
1293
1294 urb = alloc_isoc_urb(hdev, skb);
1295 if (IS_ERR(urb))
1296 return PTR_ERR(urb);
1297
1298 hdev->stat.sco_tx++;
1299 return submit_tx_urb(hdev, urb);
1300 }
1301
1302 return -EILSEQ;
1303}
1304
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001305static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
1306{
David Herrmann155961e2012-02-09 21:58:32 +01001307 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001308
1309 BT_DBG("%s evt %d", hdev->name, evt);
1310
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001311 if (hci_conn_num(hdev, SCO_LINK) != data->sco_num) {
1312 data->sco_num = hci_conn_num(hdev, SCO_LINK);
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001313 schedule_work(&data->work);
Marcel Holtmanna780efa2008-11-30 12:17:12 +01001314 }
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001315}
1316
Jesper Juhl42b16b32011-01-17 00:09:38 +01001317static inline int __set_isoc_interface(struct hci_dev *hdev, int altsetting)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001318{
David Herrmann155961e2012-02-09 21:58:32 +01001319 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001320 struct usb_interface *intf = data->isoc;
1321 struct usb_endpoint_descriptor *ep_desc;
1322 int i, err;
1323
1324 if (!data->isoc)
1325 return -ENODEV;
1326
1327 err = usb_set_interface(data->udev, 1, altsetting);
1328 if (err < 0) {
1329 BT_ERR("%s setting interface failed (%d)", hdev->name, -err);
1330 return err;
1331 }
1332
1333 data->isoc_altsetting = altsetting;
1334
1335 data->isoc_tx_ep = NULL;
1336 data->isoc_rx_ep = NULL;
1337
1338 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
1339 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
1340
1341 if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) {
1342 data->isoc_tx_ep = ep_desc;
1343 continue;
1344 }
1345
1346 if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) {
1347 data->isoc_rx_ep = ep_desc;
1348 continue;
1349 }
1350 }
1351
1352 if (!data->isoc_tx_ep || !data->isoc_rx_ep) {
1353 BT_ERR("%s invalid SCO descriptors", hdev->name);
1354 return -ENODEV;
1355 }
1356
1357 return 0;
1358}
1359
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001360static void btusb_work(struct work_struct *work)
1361{
1362 struct btusb_data *data = container_of(work, struct btusb_data, work);
1363 struct hci_dev *hdev = data->hdev;
Mikel Astizf4001d22012-04-11 08:48:51 +02001364 int new_alts;
Oliver Neukum7bee5492009-08-24 23:44:59 +02001365 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001366
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001367 if (data->sco_num > 0) {
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -03001368 if (!test_bit(BTUSB_DID_ISO_RESUME, &data->flags)) {
Oliver Neukum8efdd0c2011-02-11 13:00:06 +01001369 err = usb_autopm_get_interface(data->isoc ? data->isoc : data->intf);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001370 if (err < 0) {
1371 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1372 usb_kill_anchored_urbs(&data->isoc_anchor);
1373 return;
1374 }
1375
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -03001376 set_bit(BTUSB_DID_ISO_RESUME, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001377 }
Mikel Astizf4001d22012-04-11 08:48:51 +02001378
1379 if (hdev->voice_setting & 0x0020) {
1380 static const int alts[3] = { 2, 4, 5 };
Marcel Holtmann89e75332014-09-16 04:44:50 +02001381
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001382 new_alts = alts[data->sco_num - 1];
Mikel Astizf4001d22012-04-11 08:48:51 +02001383 } else {
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001384 new_alts = data->sco_num;
Mikel Astizf4001d22012-04-11 08:48:51 +02001385 }
1386
1387 if (data->isoc_altsetting != new_alts) {
Kuba Pawlakf6fc86f2015-10-28 15:18:05 +01001388 unsigned long flags;
1389
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001390 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1391 usb_kill_anchored_urbs(&data->isoc_anchor);
1392
Kuba Pawlak8f9d02f2015-09-10 17:07:00 +01001393 /* When isochronous alternate setting needs to be
1394 * changed, because SCO connection has been added
1395 * or removed, a packet fragment may be left in the
1396 * reassembling state. This could lead to wrongly
1397 * assembled fragments.
1398 *
1399 * Clear outstanding fragment when selecting a new
1400 * alternate setting.
1401 */
Kuba Pawlakf6fc86f2015-10-28 15:18:05 +01001402 spin_lock_irqsave(&data->rxlock, flags);
Kuba Pawlak8f9d02f2015-09-10 17:07:00 +01001403 kfree_skb(data->sco_skb);
1404 data->sco_skb = NULL;
Kuba Pawlakf6fc86f2015-10-28 15:18:05 +01001405 spin_unlock_irqrestore(&data->rxlock, flags);
Kuba Pawlak8f9d02f2015-09-10 17:07:00 +01001406
Mikel Astizf4001d22012-04-11 08:48:51 +02001407 if (__set_isoc_interface(hdev, new_alts) < 0)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001408 return;
1409 }
1410
1411 if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001412 if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001413 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1414 else
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001415 btusb_submit_isoc_urb(hdev, GFP_KERNEL);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001416 }
1417 } else {
1418 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1419 usb_kill_anchored_urbs(&data->isoc_anchor);
1420
1421 __set_isoc_interface(hdev, 0);
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -03001422 if (test_and_clear_bit(BTUSB_DID_ISO_RESUME, &data->flags))
Oliver Neukum8efdd0c2011-02-11 13:00:06 +01001423 usb_autopm_put_interface(data->isoc ? data->isoc : data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001424 }
1425}
1426
Oliver Neukum7bee5492009-08-24 23:44:59 +02001427static void btusb_waker(struct work_struct *work)
1428{
1429 struct btusb_data *data = container_of(work, struct btusb_data, waker);
1430 int err;
1431
1432 err = usb_autopm_get_interface(data->intf);
1433 if (err < 0)
1434 return;
1435
1436 usb_autopm_put_interface(data->intf);
1437}
1438
Marcel Holtmann9f8f9622013-04-10 08:11:35 -07001439static int btusb_setup_bcm92035(struct hci_dev *hdev)
1440{
1441 struct sk_buff *skb;
1442 u8 val = 0x00;
1443
1444 BT_DBG("%s", hdev->name);
1445
1446 skb = __hci_cmd_sync(hdev, 0xfc3b, 1, &val, HCI_INIT_TIMEOUT);
1447 if (IS_ERR(skb))
1448 BT_ERR("BCM92035 command failed (%ld)", -PTR_ERR(skb));
1449 else
1450 kfree_skb(skb);
1451
1452 return 0;
1453}
1454
Marcel Holtmann81cac642014-01-03 03:02:36 -08001455static int btusb_setup_csr(struct hci_dev *hdev)
1456{
1457 struct hci_rp_read_local_version *rp;
1458 struct sk_buff *skb;
Marcel Holtmann81cac642014-01-03 03:02:36 -08001459
1460 BT_DBG("%s", hdev->name);
1461
Marcel Holtmann7cd84d72015-06-07 10:01:02 +02001462 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
1463 HCI_INIT_TIMEOUT);
1464 if (IS_ERR(skb)) {
1465 int err = PTR_ERR(skb);
1466 BT_ERR("%s: CSR: Local version failed (%d)", hdev->name, err);
1467 return err;
1468 }
1469
1470 if (skb->len != sizeof(struct hci_rp_read_local_version)) {
1471 BT_ERR("%s: CSR: Local version length mismatch", hdev->name);
1472 kfree_skb(skb);
1473 return -EIO;
1474 }
Marcel Holtmann81cac642014-01-03 03:02:36 -08001475
Marcel Holtmann89e75332014-09-16 04:44:50 +02001476 rp = (struct hci_rp_read_local_version *)skb->data;
Marcel Holtmann81cac642014-01-03 03:02:36 -08001477
Johan Hedberg6cafcd92015-08-30 21:47:21 +03001478 /* Detect controllers which aren't real CSR ones. */
1479 if (le16_to_cpu(rp->manufacturer) != 10 ||
1480 le16_to_cpu(rp->lmp_subver) == 0x0c5c) {
Marcel Holtmann9641d342015-06-07 10:01:01 +02001481 /* Clear the reset quirk since this is not an actual
1482 * early Bluetooth 1.1 device from CSR.
1483 */
1484 clear_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmann81cac642014-01-03 03:02:36 -08001485
Marcel Holtmann9641d342015-06-07 10:01:01 +02001486 /* These fake CSR controllers have all a broken
1487 * stored link key handling and so just disable it.
1488 */
1489 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
Marcel Holtmann81cac642014-01-03 03:02:36 -08001490 }
1491
Marcel Holtmann81cac642014-01-03 03:02:36 -08001492 kfree_skb(skb);
1493
Marcel Holtmann9641d342015-06-07 10:01:01 +02001494 return 0;
Marcel Holtmann81cac642014-01-03 03:02:36 -08001495}
1496
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001497static const struct firmware *btusb_setup_intel_get_fw(struct hci_dev *hdev,
Marcel Holtmann89e75332014-09-16 04:44:50 +02001498 struct intel_version *ver)
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001499{
1500 const struct firmware *fw;
1501 char fwname[64];
1502 int ret;
1503
1504 snprintf(fwname, sizeof(fwname),
1505 "intel/ibt-hw-%x.%x.%x-fw-%x.%x.%x.%x.%x.bseq",
1506 ver->hw_platform, ver->hw_variant, ver->hw_revision,
1507 ver->fw_variant, ver->fw_revision, ver->fw_build_num,
1508 ver->fw_build_ww, ver->fw_build_yy);
1509
1510 ret = request_firmware(&fw, fwname, &hdev->dev);
1511 if (ret < 0) {
1512 if (ret == -EINVAL) {
1513 BT_ERR("%s Intel firmware file request failed (%d)",
1514 hdev->name, ret);
1515 return NULL;
1516 }
1517
1518 BT_ERR("%s failed to open Intel firmware file: %s(%d)",
1519 hdev->name, fwname, ret);
1520
1521 /* If the correct firmware patch file is not found, use the
1522 * default firmware patch file instead
1523 */
1524 snprintf(fwname, sizeof(fwname), "intel/ibt-hw-%x.%x.bseq",
1525 ver->hw_platform, ver->hw_variant);
1526 if (request_firmware(&fw, fwname, &hdev->dev) < 0) {
1527 BT_ERR("%s failed to open default Intel fw file: %s",
1528 hdev->name, fwname);
1529 return NULL;
1530 }
1531 }
1532
1533 BT_INFO("%s: Intel Bluetooth firmware file: %s", hdev->name, fwname);
1534
1535 return fw;
1536}
1537
1538static int btusb_setup_intel_patching(struct hci_dev *hdev,
1539 const struct firmware *fw,
1540 const u8 **fw_ptr, int *disable_patch)
1541{
1542 struct sk_buff *skb;
1543 struct hci_command_hdr *cmd;
1544 const u8 *cmd_param;
1545 struct hci_event_hdr *evt = NULL;
1546 const u8 *evt_param = NULL;
1547 int remain = fw->size - (*fw_ptr - fw->data);
1548
1549 /* The first byte indicates the types of the patch command or event.
1550 * 0x01 means HCI command and 0x02 is HCI event. If the first bytes
1551 * in the current firmware buffer doesn't start with 0x01 or
1552 * the size of remain buffer is smaller than HCI command header,
1553 * the firmware file is corrupted and it should stop the patching
1554 * process.
1555 */
1556 if (remain > HCI_COMMAND_HDR_SIZE && *fw_ptr[0] != 0x01) {
1557 BT_ERR("%s Intel fw corrupted: invalid cmd read", hdev->name);
1558 return -EINVAL;
1559 }
1560 (*fw_ptr)++;
1561 remain--;
1562
1563 cmd = (struct hci_command_hdr *)(*fw_ptr);
1564 *fw_ptr += sizeof(*cmd);
1565 remain -= sizeof(*cmd);
1566
1567 /* Ensure that the remain firmware data is long enough than the length
1568 * of command parameter. If not, the firmware file is corrupted.
1569 */
1570 if (remain < cmd->plen) {
1571 BT_ERR("%s Intel fw corrupted: invalid cmd len", hdev->name);
1572 return -EFAULT;
1573 }
1574
1575 /* If there is a command that loads a patch in the firmware
1576 * file, then enable the patch upon success, otherwise just
1577 * disable the manufacturer mode, for example patch activation
1578 * is not required when the default firmware patch file is used
1579 * because there are no patch data to load.
1580 */
1581 if (*disable_patch && le16_to_cpu(cmd->opcode) == 0xfc8e)
1582 *disable_patch = 0;
1583
1584 cmd_param = *fw_ptr;
1585 *fw_ptr += cmd->plen;
1586 remain -= cmd->plen;
1587
1588 /* This reads the expected events when the above command is sent to the
1589 * device. Some vendor commands expects more than one events, for
1590 * example command status event followed by vendor specific event.
1591 * For this case, it only keeps the last expected event. so the command
1592 * can be sent with __hci_cmd_sync_ev() which returns the sk_buff of
1593 * last expected event.
1594 */
1595 while (remain > HCI_EVENT_HDR_SIZE && *fw_ptr[0] == 0x02) {
1596 (*fw_ptr)++;
1597 remain--;
1598
1599 evt = (struct hci_event_hdr *)(*fw_ptr);
1600 *fw_ptr += sizeof(*evt);
1601 remain -= sizeof(*evt);
1602
1603 if (remain < evt->plen) {
1604 BT_ERR("%s Intel fw corrupted: invalid evt len",
1605 hdev->name);
1606 return -EFAULT;
1607 }
1608
1609 evt_param = *fw_ptr;
1610 *fw_ptr += evt->plen;
1611 remain -= evt->plen;
1612 }
1613
1614 /* Every HCI commands in the firmware file has its correspond event.
1615 * If event is not found or remain is smaller than zero, the firmware
1616 * file is corrupted.
1617 */
1618 if (!evt || !evt_param || remain < 0) {
1619 BT_ERR("%s Intel fw corrupted: invalid evt read", hdev->name);
1620 return -EFAULT;
1621 }
1622
1623 skb = __hci_cmd_sync_ev(hdev, le16_to_cpu(cmd->opcode), cmd->plen,
1624 cmd_param, evt->evt, HCI_INIT_TIMEOUT);
1625 if (IS_ERR(skb)) {
1626 BT_ERR("%s sending Intel patch command (0x%4.4x) failed (%ld)",
1627 hdev->name, cmd->opcode, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001628 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001629 }
1630
1631 /* It ensures that the returned event matches the event data read from
1632 * the firmware file. At fist, it checks the length and then
1633 * the contents of the event.
1634 */
1635 if (skb->len != evt->plen) {
1636 BT_ERR("%s mismatch event length (opcode 0x%4.4x)", hdev->name,
1637 le16_to_cpu(cmd->opcode));
1638 kfree_skb(skb);
1639 return -EFAULT;
1640 }
1641
1642 if (memcmp(skb->data, evt_param, evt->plen)) {
1643 BT_ERR("%s mismatch event parameter (opcode 0x%4.4x)",
1644 hdev->name, le16_to_cpu(cmd->opcode));
1645 kfree_skb(skb);
1646 return -EFAULT;
1647 }
1648 kfree_skb(skb);
1649
1650 return 0;
1651}
1652
1653static int btusb_setup_intel(struct hci_dev *hdev)
1654{
1655 struct sk_buff *skb;
1656 const struct firmware *fw;
1657 const u8 *fw_ptr;
Loic Poulain28dc4b92015-12-03 16:10:22 +01001658 int disable_patch, err;
Loic Poulain6c483de2015-12-06 16:18:34 +01001659 struct intel_version ver;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001660
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001661 BT_DBG("%s", hdev->name);
1662
1663 /* The controller has a bug with the first HCI command sent to it
1664 * returning number of completed commands as zero. This would stall the
1665 * command processing in the Bluetooth core.
1666 *
1667 * As a workaround, send HCI Reset command first which will reset the
1668 * number of completed commands and allow normal command processing
1669 * from now on.
1670 */
1671 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
1672 if (IS_ERR(skb)) {
1673 BT_ERR("%s sending initial HCI reset command failed (%ld)",
1674 hdev->name, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001675 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001676 }
1677 kfree_skb(skb);
1678
1679 /* Read Intel specific controller version first to allow selection of
1680 * which firmware file to load.
1681 *
1682 * The returned information are hardware variant and revision plus
1683 * firmware variant, revision and build number.
1684 */
Loic Poulain6c483de2015-12-06 16:18:34 +01001685 err = btintel_read_version(hdev, &ver);
1686 if (err)
1687 return err;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001688
1689 BT_INFO("%s: read Intel version: %02x%02x%02x%02x%02x%02x%02x%02x%02x",
Loic Poulain6c483de2015-12-06 16:18:34 +01001690 hdev->name, ver.hw_platform, ver.hw_variant, ver.hw_revision,
1691 ver.fw_variant, ver.fw_revision, ver.fw_build_num,
1692 ver.fw_build_ww, ver.fw_build_yy, ver.fw_patch_num);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001693
1694 /* fw_patch_num indicates the version of patch the device currently
1695 * have. If there is no patch data in the device, it is always 0x00.
Minjune Kim5075eda2015-08-27 13:21:52 +09001696 * So, if it is other than 0x00, no need to patch the device again.
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001697 */
Loic Poulain6c483de2015-12-06 16:18:34 +01001698 if (ver.fw_patch_num) {
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001699 BT_INFO("%s: Intel device is already patched. patch num: %02x",
Loic Poulain6c483de2015-12-06 16:18:34 +01001700 hdev->name, ver.fw_patch_num);
Marcel Holtmann213445b2015-10-21 02:45:19 +02001701 goto complete;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001702 }
1703
1704 /* Opens the firmware patch file based on the firmware version read
1705 * from the controller. If it fails to open the matching firmware
1706 * patch file, it tries to open the default firmware patch file.
1707 * If no patch file is found, allow the device to operate without
1708 * a patch.
1709 */
Loic Poulain6c483de2015-12-06 16:18:34 +01001710 fw = btusb_setup_intel_get_fw(hdev, &ver);
1711 if (!fw)
Marcel Holtmann213445b2015-10-21 02:45:19 +02001712 goto complete;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001713 fw_ptr = fw->data;
1714
Loic Poulain28dc4b92015-12-03 16:10:22 +01001715 /* Enable the manufacturer mode of the controller.
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001716 * Only while this mode is enabled, the driver can download the
1717 * firmware patch data and configuration parameters.
1718 */
Loic Poulain28dc4b92015-12-03 16:10:22 +01001719 err = btintel_enter_mfg(hdev);
1720 if (err) {
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001721 release_firmware(fw);
Loic Poulain28dc4b92015-12-03 16:10:22 +01001722 return err;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001723 }
1724
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001725 disable_patch = 1;
1726
1727 /* The firmware data file consists of list of Intel specific HCI
1728 * commands and its expected events. The first byte indicates the
1729 * type of the message, either HCI command or HCI event.
1730 *
1731 * It reads the command and its expected event from the firmware file,
1732 * and send to the controller. Once __hci_cmd_sync_ev() returns,
1733 * the returned event is compared with the event read from the firmware
1734 * file and it will continue until all the messages are downloaded to
1735 * the controller.
1736 *
1737 * Once the firmware patching is completed successfully,
1738 * the manufacturer mode is disabled with reset and activating the
1739 * downloaded patch.
1740 *
1741 * If the firmware patching fails, the manufacturer mode is
1742 * disabled with reset and deactivating the patch.
1743 *
1744 * If the default patch file is used, no reset is done when disabling
1745 * the manufacturer.
1746 */
1747 while (fw->size > fw_ptr - fw->data) {
1748 int ret;
1749
1750 ret = btusb_setup_intel_patching(hdev, fw, &fw_ptr,
1751 &disable_patch);
1752 if (ret < 0)
1753 goto exit_mfg_deactivate;
1754 }
1755
1756 release_firmware(fw);
1757
1758 if (disable_patch)
1759 goto exit_mfg_disable;
1760
1761 /* Patching completed successfully and disable the manufacturer mode
1762 * with reset and activate the downloaded firmware patches.
1763 */
Loic Poulain28dc4b92015-12-03 16:10:22 +01001764 err = btintel_exit_mfg(hdev, true, true);
1765 if (err)
1766 return err;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001767
1768 BT_INFO("%s: Intel Bluetooth firmware patch completed and activated",
1769 hdev->name);
1770
Marcel Holtmann213445b2015-10-21 02:45:19 +02001771 goto complete;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001772
1773exit_mfg_disable:
1774 /* Disable the manufacturer mode without reset */
Loic Poulain28dc4b92015-12-03 16:10:22 +01001775 err = btintel_exit_mfg(hdev, false, false);
1776 if (err)
1777 return err;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001778
1779 BT_INFO("%s: Intel Bluetooth firmware patch completed", hdev->name);
Marcel Holtmann40cb0982014-07-02 12:06:45 +02001780
Marcel Holtmann213445b2015-10-21 02:45:19 +02001781 goto complete;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001782
1783exit_mfg_deactivate:
1784 release_firmware(fw);
1785
1786 /* Patching failed. Disable the manufacturer mode with reset and
1787 * deactivate the downloaded firmware patches.
1788 */
Loic Poulain28dc4b92015-12-03 16:10:22 +01001789 err = btintel_exit_mfg(hdev, true, false);
1790 if (err)
1791 return err;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001792
1793 BT_INFO("%s: Intel Bluetooth firmware patch completed and deactivated",
1794 hdev->name);
1795
Marcel Holtmann213445b2015-10-21 02:45:19 +02001796complete:
1797 /* Set the event mask for Intel specific vendor events. This enables
1798 * a few extra events that are useful during general operation.
1799 */
1800 btintel_set_event_mask_mfg(hdev, false);
1801
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07001802 btintel_check_bdaddr(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001803 return 0;
1804}
1805
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001806static int inject_cmd_complete(struct hci_dev *hdev, __u16 opcode)
1807{
1808 struct sk_buff *skb;
1809 struct hci_event_hdr *hdr;
1810 struct hci_ev_cmd_complete *evt;
1811
1812 skb = bt_skb_alloc(sizeof(*hdr) + sizeof(*evt) + 1, GFP_ATOMIC);
1813 if (!skb)
1814 return -ENOMEM;
1815
1816 hdr = (struct hci_event_hdr *)skb_put(skb, sizeof(*hdr));
1817 hdr->evt = HCI_EV_CMD_COMPLETE;
1818 hdr->plen = sizeof(*evt) + 1;
1819
1820 evt = (struct hci_ev_cmd_complete *)skb_put(skb, sizeof(*evt));
1821 evt->ncmd = 0x01;
1822 evt->opcode = cpu_to_le16(opcode);
1823
1824 *skb_put(skb, 1) = 0x00;
1825
Marcel Holtmann618e8bc2015-11-05 07:33:56 +01001826 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001827
1828 return hci_recv_frame(hdev, skb);
1829}
1830
1831static int btusb_recv_bulk_intel(struct btusb_data *data, void *buffer,
1832 int count)
1833{
1834 /* When the device is in bootloader mode, then it can send
1835 * events via the bulk endpoint. These events are treated the
1836 * same way as the ones received from the interrupt endpoint.
1837 */
1838 if (test_bit(BTUSB_BOOTLOADER, &data->flags))
1839 return btusb_recv_intr(data, buffer, count);
1840
1841 return btusb_recv_bulk(data, buffer, count);
1842}
1843
Marcel Holtmannccd6da22015-04-09 00:35:19 -07001844static void btusb_intel_bootup(struct btusb_data *data, const void *ptr,
1845 unsigned int len)
1846{
1847 const struct intel_bootup *evt = ptr;
1848
1849 if (len != sizeof(*evt))
1850 return;
1851
1852 if (test_and_clear_bit(BTUSB_BOOTING, &data->flags)) {
1853 smp_mb__after_atomic();
1854 wake_up_bit(&data->flags, BTUSB_BOOTING);
1855 }
1856}
1857
1858static void btusb_intel_secure_send_result(struct btusb_data *data,
1859 const void *ptr, unsigned int len)
1860{
1861 const struct intel_secure_send_result *evt = ptr;
1862
1863 if (len != sizeof(*evt))
1864 return;
1865
1866 if (evt->result)
1867 set_bit(BTUSB_FIRMWARE_FAILED, &data->flags);
1868
1869 if (test_and_clear_bit(BTUSB_DOWNLOADING, &data->flags) &&
1870 test_bit(BTUSB_FIRMWARE_LOADED, &data->flags)) {
1871 smp_mb__after_atomic();
1872 wake_up_bit(&data->flags, BTUSB_DOWNLOADING);
1873 }
1874}
1875
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001876static int btusb_recv_event_intel(struct hci_dev *hdev, struct sk_buff *skb)
1877{
1878 struct btusb_data *data = hci_get_drvdata(hdev);
1879
1880 if (test_bit(BTUSB_BOOTLOADER, &data->flags)) {
1881 struct hci_event_hdr *hdr = (void *)skb->data;
1882
Marcel Holtmannccd6da22015-04-09 00:35:19 -07001883 if (skb->len > HCI_EVENT_HDR_SIZE && hdr->evt == 0xff &&
1884 hdr->plen > 0) {
1885 const void *ptr = skb->data + HCI_EVENT_HDR_SIZE + 1;
1886 unsigned int len = skb->len - HCI_EVENT_HDR_SIZE - 1;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001887
Marcel Holtmannccd6da22015-04-09 00:35:19 -07001888 switch (skb->data[2]) {
1889 case 0x02:
1890 /* When switching to the operational firmware
1891 * the device sends a vendor specific event
1892 * indicating that the bootup completed.
1893 */
1894 btusb_intel_bootup(data, ptr, len);
1895 break;
1896 case 0x06:
1897 /* When the firmware loading completes the
1898 * device sends out a vendor specific event
1899 * indicating the result of the firmware
1900 * loading.
1901 */
1902 btusb_intel_secure_send_result(data, ptr, len);
1903 break;
Johan Hedbergfad70972015-01-30 10:58:55 +02001904 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001905 }
1906 }
1907
1908 return hci_recv_frame(hdev, skb);
1909}
1910
1911static int btusb_send_frame_intel(struct hci_dev *hdev, struct sk_buff *skb)
1912{
1913 struct btusb_data *data = hci_get_drvdata(hdev);
1914 struct urb *urb;
1915
1916 BT_DBG("%s", hdev->name);
1917
Marcel Holtmann618e8bc2015-11-05 07:33:56 +01001918 switch (hci_skb_pkt_type(skb)) {
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001919 case HCI_COMMAND_PKT:
1920 if (test_bit(BTUSB_BOOTLOADER, &data->flags)) {
1921 struct hci_command_hdr *cmd = (void *)skb->data;
1922 __u16 opcode = le16_to_cpu(cmd->opcode);
1923
1924 /* When in bootloader mode and the command 0xfc09
1925 * is received, it needs to be send down the
1926 * bulk endpoint. So allocate a bulk URB instead.
1927 */
1928 if (opcode == 0xfc09)
1929 urb = alloc_bulk_urb(hdev, skb);
1930 else
1931 urb = alloc_ctrl_urb(hdev, skb);
1932
1933 /* When the 0xfc01 command is issued to boot into
1934 * the operational firmware, it will actually not
1935 * send a command complete event. To keep the flow
1936 * control working inject that event here.
1937 */
1938 if (opcode == 0xfc01)
1939 inject_cmd_complete(hdev, opcode);
1940 } else {
1941 urb = alloc_ctrl_urb(hdev, skb);
1942 }
1943 if (IS_ERR(urb))
1944 return PTR_ERR(urb);
1945
1946 hdev->stat.cmd_tx++;
1947 return submit_or_queue_tx_urb(hdev, urb);
1948
1949 case HCI_ACLDATA_PKT:
1950 urb = alloc_bulk_urb(hdev, skb);
1951 if (IS_ERR(urb))
1952 return PTR_ERR(urb);
1953
1954 hdev->stat.acl_tx++;
1955 return submit_or_queue_tx_urb(hdev, urb);
1956
1957 case HCI_SCODATA_PKT:
1958 if (hci_conn_num(hdev, SCO_LINK) < 1)
1959 return -ENODEV;
1960
1961 urb = alloc_isoc_urb(hdev, skb);
1962 if (IS_ERR(urb))
1963 return PTR_ERR(urb);
1964
1965 hdev->stat.sco_tx++;
1966 return submit_tx_urb(hdev, urb);
1967 }
1968
1969 return -EILSEQ;
1970}
1971
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001972static int btusb_setup_intel_new(struct hci_dev *hdev)
1973{
1974 static const u8 reset_param[] = { 0x00, 0x01, 0x00, 0x01,
1975 0x00, 0x08, 0x04, 0x00 };
1976 struct btusb_data *data = hci_get_drvdata(hdev);
1977 struct sk_buff *skb;
Loic Poulain6c483de2015-12-06 16:18:34 +01001978 struct intel_version ver;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001979 struct intel_boot_params *params;
1980 const struct firmware *fw;
1981 const u8 *fw_ptr;
Marcel Holtmanne66890a92015-06-07 09:47:08 +02001982 u32 frag_len;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001983 char fwname[64];
1984 ktime_t calltime, delta, rettime;
1985 unsigned long long duration;
1986 int err;
1987
1988 BT_DBG("%s", hdev->name);
1989
1990 calltime = ktime_get();
1991
1992 /* Read the Intel version information to determine if the device
1993 * is in bootloader mode or if it already has operational firmware
1994 * loaded.
1995 */
Loic Poulain6c483de2015-12-06 16:18:34 +01001996 err = btintel_read_version(hdev, &ver);
1997 if (err)
1998 return err;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001999
2000 /* The hardware platform number has a fixed value of 0x37 and
2001 * for now only accept this single value.
2002 */
Loic Poulain6c483de2015-12-06 16:18:34 +01002003 if (ver.hw_platform != 0x37) {
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002004 BT_ERR("%s: Unsupported Intel hardware platform (%u)",
Loic Poulain6c483de2015-12-06 16:18:34 +01002005 hdev->name, ver.hw_platform);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002006 return -EINVAL;
2007 }
2008
Tedd Ho-Jeong Ana0af53b2016-05-06 11:53:46 -07002009 /* At the moment the iBT 3.0 hardware variants 0x0b (LnP/SfP)
2010 * and 0x0c (WsP) are supported by this firmware loading method.
2011 *
2012 * This check has been put in place to ensure correct forward
2013 * compatibility options when newer hardware variants come along.
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002014 */
Tedd Ho-Jeong Ana0af53b2016-05-06 11:53:46 -07002015 if (ver.hw_variant != 0x0b && ver.hw_variant != 0x0c) {
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002016 BT_ERR("%s: Unsupported Intel hardware variant (%u)",
Loic Poulain6c483de2015-12-06 16:18:34 +01002017 hdev->name, ver.hw_variant);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002018 return -EINVAL;
2019 }
2020
Loic Poulain6c483de2015-12-06 16:18:34 +01002021 btintel_version_info(hdev, &ver);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002022
2023 /* The firmware variant determines if the device is in bootloader
2024 * mode or is running operational firmware. The value 0x06 identifies
2025 * the bootloader and the value 0x23 identifies the operational
2026 * firmware.
2027 *
2028 * When the operational firmware is already present, then only
2029 * the check for valid Bluetooth device address is needed. This
2030 * determines if the device will be added as configured or
2031 * unconfigured controller.
2032 *
2033 * It is not possible to use the Secure Boot Parameters in this
2034 * case since that command is only available in bootloader mode.
2035 */
Loic Poulain6c483de2015-12-06 16:18:34 +01002036 if (ver.fw_variant == 0x23) {
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002037 clear_bit(BTUSB_BOOTLOADER, &data->flags);
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07002038 btintel_check_bdaddr(hdev);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002039 return 0;
2040 }
2041
2042 /* If the device is not in bootloader mode, then the only possible
2043 * choice is to return an error and abort the device initialization.
2044 */
Loic Poulain6c483de2015-12-06 16:18:34 +01002045 if (ver.fw_variant != 0x06) {
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002046 BT_ERR("%s: Unsupported Intel firmware variant (%u)",
Loic Poulain6c483de2015-12-06 16:18:34 +01002047 hdev->name, ver.fw_variant);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002048 return -ENODEV;
2049 }
2050
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002051 /* Read the secure boot parameters to identify the operating
2052 * details of the bootloader.
2053 */
2054 skb = __hci_cmd_sync(hdev, 0xfc0d, 0, NULL, HCI_INIT_TIMEOUT);
2055 if (IS_ERR(skb)) {
2056 BT_ERR("%s: Reading Intel boot parameters failed (%ld)",
2057 hdev->name, PTR_ERR(skb));
2058 return PTR_ERR(skb);
2059 }
2060
2061 if (skb->len != sizeof(*params)) {
2062 BT_ERR("%s: Intel boot parameters size mismatch", hdev->name);
2063 kfree_skb(skb);
2064 return -EILSEQ;
2065 }
2066
2067 params = (struct intel_boot_params *)skb->data;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002068
2069 BT_INFO("%s: Device revision is %u", hdev->name,
2070 le16_to_cpu(params->dev_revid));
2071
2072 BT_INFO("%s: Secure boot is %s", hdev->name,
2073 params->secure_boot ? "enabled" : "disabled");
2074
Marcel Holtmann2220994e2015-10-13 13:54:55 +02002075 BT_INFO("%s: OTP lock is %s", hdev->name,
2076 params->otp_lock ? "enabled" : "disabled");
2077
2078 BT_INFO("%s: API lock is %s", hdev->name,
2079 params->api_lock ? "enabled" : "disabled");
2080
2081 BT_INFO("%s: Debug lock is %s", hdev->name,
2082 params->debug_lock ? "enabled" : "disabled");
2083
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002084 BT_INFO("%s: Minimum firmware build %u week %u %u", hdev->name,
2085 params->min_fw_build_nn, params->min_fw_build_cw,
2086 2000 + params->min_fw_build_yy);
2087
2088 /* It is required that every single firmware fragment is acknowledged
2089 * with a command complete event. If the boot parameters indicate
2090 * that this bootloader does not send them, then abort the setup.
2091 */
2092 if (params->limited_cce != 0x00) {
2093 BT_ERR("%s: Unsupported Intel firmware loading method (%u)",
2094 hdev->name, params->limited_cce);
2095 kfree_skb(skb);
2096 return -EINVAL;
2097 }
2098
2099 /* If the OTP has no valid Bluetooth device address, then there will
2100 * also be no valid address for the operational firmware.
2101 */
2102 if (!bacmp(&params->otp_bdaddr, BDADDR_ANY)) {
2103 BT_INFO("%s: No device address configured", hdev->name);
2104 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
2105 }
2106
2107 /* With this Intel bootloader only the hardware variant and device
2108 * revision information are used to select the right firmware.
2109 *
Tedd Ho-Jeong An230b04a2016-06-28 08:56:39 -07002110 * The firmware filename is ibt-<hw_variant>-<dev_revid>.sfi.
2111 *
2112 * Currently the supported hardware variants are:
2113 * 11 (0x0b) for iBT3.0 (LnP/SfP)
2114 * 12 (0x0c) for iBT3.5 (WsP)
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002115 */
Tedd Ho-Jeong An230b04a2016-06-28 08:56:39 -07002116 snprintf(fwname, sizeof(fwname), "intel/ibt-%u-%u.sfi",
2117 le16_to_cpu(ver.hw_variant),
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002118 le16_to_cpu(params->dev_revid));
2119
2120 err = request_firmware(&fw, fwname, &hdev->dev);
2121 if (err < 0) {
2122 BT_ERR("%s: Failed to load Intel firmware file (%d)",
2123 hdev->name, err);
2124 kfree_skb(skb);
2125 return err;
2126 }
2127
2128 BT_INFO("%s: Found device firmware: %s", hdev->name, fwname);
2129
Tedd Ho-Jeong An52cc9162015-06-12 16:20:05 -07002130 /* Save the DDC file name for later use to apply once the firmware
2131 * downloading is done.
2132 */
Tedd Ho-Jeong An230b04a2016-06-28 08:56:39 -07002133 snprintf(fwname, sizeof(fwname), "intel/ibt-%u-%u.ddc",
2134 le16_to_cpu(ver.hw_variant),
Tedd Ho-Jeong An52cc9162015-06-12 16:20:05 -07002135 le16_to_cpu(params->dev_revid));
2136
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002137 kfree_skb(skb);
2138
2139 if (fw->size < 644) {
2140 BT_ERR("%s: Invalid size of firmware file (%zu)",
2141 hdev->name, fw->size);
2142 err = -EBADF;
2143 goto done;
2144 }
2145
2146 set_bit(BTUSB_DOWNLOADING, &data->flags);
2147
2148 /* Start the firmware download transaction with the Init fragment
2149 * represented by the 128 bytes of CSS header.
2150 */
Marcel Holtmann09df1232015-07-05 14:55:36 +02002151 err = btintel_secure_send(hdev, 0x00, 128, fw->data);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002152 if (err < 0) {
2153 BT_ERR("%s: Failed to send firmware header (%d)",
2154 hdev->name, err);
2155 goto done;
2156 }
2157
2158 /* Send the 256 bytes of public key information from the firmware
2159 * as the PKey fragment.
2160 */
Marcel Holtmann09df1232015-07-05 14:55:36 +02002161 err = btintel_secure_send(hdev, 0x03, 256, fw->data + 128);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002162 if (err < 0) {
2163 BT_ERR("%s: Failed to send firmware public key (%d)",
2164 hdev->name, err);
2165 goto done;
2166 }
2167
2168 /* Send the 256 bytes of signature information from the firmware
2169 * as the Sign fragment.
2170 */
Marcel Holtmann09df1232015-07-05 14:55:36 +02002171 err = btintel_secure_send(hdev, 0x02, 256, fw->data + 388);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002172 if (err < 0) {
2173 BT_ERR("%s: Failed to send firmware signature (%d)",
2174 hdev->name, err);
2175 goto done;
2176 }
2177
2178 fw_ptr = fw->data + 644;
Marcel Holtmanne66890a92015-06-07 09:47:08 +02002179 frag_len = 0;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002180
2181 while (fw_ptr - fw->data < fw->size) {
Marcel Holtmanne66890a92015-06-07 09:47:08 +02002182 struct hci_command_hdr *cmd = (void *)(fw_ptr + frag_len);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002183
Marcel Holtmanne66890a92015-06-07 09:47:08 +02002184 frag_len += sizeof(*cmd) + cmd->plen;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002185
Minjune Kim5075eda2015-08-27 13:21:52 +09002186 /* The parameter length of the secure send command requires
Marcel Holtmanne66890a92015-06-07 09:47:08 +02002187 * a 4 byte alignment. It happens so that the firmware file
2188 * contains proper Intel_NOP commands to align the fragments
2189 * as needed.
2190 *
2191 * Send set of commands with 4 byte alignment from the
2192 * firmware data buffer as a single Data fragement.
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002193 */
Marcel Holtmanne66890a92015-06-07 09:47:08 +02002194 if (!(frag_len % 4)) {
Marcel Holtmann09df1232015-07-05 14:55:36 +02002195 err = btintel_secure_send(hdev, 0x01, frag_len, fw_ptr);
Marcel Holtmanne66890a92015-06-07 09:47:08 +02002196 if (err < 0) {
2197 BT_ERR("%s: Failed to send firmware data (%d)",
2198 hdev->name, err);
2199 goto done;
2200 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002201
Marcel Holtmanne66890a92015-06-07 09:47:08 +02002202 fw_ptr += frag_len;
2203 frag_len = 0;
2204 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002205 }
2206
Marcel Holtmannce6bb922015-01-28 01:58:40 -08002207 set_bit(BTUSB_FIRMWARE_LOADED, &data->flags);
2208
Johan Hedberga087a982015-01-30 10:58:54 +02002209 BT_INFO("%s: Waiting for firmware download to complete", hdev->name);
2210
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002211 /* Before switching the device into operational mode and with that
2212 * booting the loaded firmware, wait for the bootloader notification
2213 * that all fragments have been successfully received.
2214 *
Johan Hedberga087a982015-01-30 10:58:54 +02002215 * When the event processing receives the notification, then the
2216 * BTUSB_DOWNLOADING flag will be cleared.
2217 *
2218 * The firmware loading should not take longer than 5 seconds
2219 * and thus just timeout if that happens and fail the setup
2220 * of this device.
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002221 */
Johan Hedberg129a7692015-02-14 09:33:35 +02002222 err = wait_on_bit_timeout(&data->flags, BTUSB_DOWNLOADING,
2223 TASK_INTERRUPTIBLE,
2224 msecs_to_jiffies(5000));
Bart Van Asschef0a70a02016-08-11 16:02:44 -07002225 if (err == -EINTR) {
Johan Hedberga087a982015-01-30 10:58:54 +02002226 BT_ERR("%s: Firmware loading interrupted", hdev->name);
Johan Hedberga087a982015-01-30 10:58:54 +02002227 goto done;
2228 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002229
Johan Hedberga087a982015-01-30 10:58:54 +02002230 if (err) {
2231 BT_ERR("%s: Firmware loading timeout", hdev->name);
2232 err = -ETIMEDOUT;
2233 goto done;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002234 }
2235
2236 if (test_bit(BTUSB_FIRMWARE_FAILED, &data->flags)) {
2237 BT_ERR("%s: Firmware loading failed", hdev->name);
2238 err = -ENOEXEC;
2239 goto done;
2240 }
2241
2242 rettime = ktime_get();
2243 delta = ktime_sub(rettime, calltime);
2244 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
2245
2246 BT_INFO("%s: Firmware loaded in %llu usecs", hdev->name, duration);
2247
2248done:
2249 release_firmware(fw);
2250
2251 if (err < 0)
2252 return err;
2253
2254 calltime = ktime_get();
2255
2256 set_bit(BTUSB_BOOTING, &data->flags);
2257
2258 skb = __hci_cmd_sync(hdev, 0xfc01, sizeof(reset_param), reset_param,
2259 HCI_INIT_TIMEOUT);
2260 if (IS_ERR(skb))
2261 return PTR_ERR(skb);
2262
2263 kfree_skb(skb);
2264
2265 /* The bootloader will not indicate when the device is ready. This
2266 * is done by the operational firmware sending bootup notification.
Johan Hedbergfad70972015-01-30 10:58:55 +02002267 *
2268 * Booting into operational firmware should not take longer than
2269 * 1 second. However if that happens, then just fail the setup
2270 * since something went wrong.
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002271 */
Johan Hedbergfad70972015-01-30 10:58:55 +02002272 BT_INFO("%s: Waiting for device to boot", hdev->name);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002273
Johan Hedberg129a7692015-02-14 09:33:35 +02002274 err = wait_on_bit_timeout(&data->flags, BTUSB_BOOTING,
2275 TASK_INTERRUPTIBLE,
2276 msecs_to_jiffies(1000));
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002277
Bart Van Asschef0a70a02016-08-11 16:02:44 -07002278 if (err == -EINTR) {
Johan Hedbergfad70972015-01-30 10:58:55 +02002279 BT_ERR("%s: Device boot interrupted", hdev->name);
2280 return -EINTR;
2281 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002282
Johan Hedbergfad70972015-01-30 10:58:55 +02002283 if (err) {
2284 BT_ERR("%s: Device boot timeout", hdev->name);
2285 return -ETIMEDOUT;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002286 }
2287
2288 rettime = ktime_get();
2289 delta = ktime_sub(rettime, calltime);
2290 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
2291
2292 BT_INFO("%s: Device booted in %llu usecs", hdev->name, duration);
2293
2294 clear_bit(BTUSB_BOOTLOADER, &data->flags);
2295
Tedd Ho-Jeong An52cc9162015-06-12 16:20:05 -07002296 /* Once the device is running in operational mode, it needs to apply
2297 * the device configuration (DDC) parameters.
2298 *
2299 * The device can work without DDC parameters, so even if it fails
2300 * to load the file, no need to fail the setup.
2301 */
Loic Poulaine924d3d2015-09-04 17:54:36 +02002302 btintel_load_ddc_config(hdev, fwname);
Tedd Ho-Jeong An52cc9162015-06-12 16:20:05 -07002303
Marcel Holtmann213445b2015-10-21 02:45:19 +02002304 /* Set the event mask for Intel specific vendor events. This enables
2305 * a few extra events that are useful during general operation. It
2306 * does not enable any debugging related events.
2307 *
2308 * The device will function correctly without these events enabled
2309 * and thus no need to fail the setup.
2310 */
2311 btintel_set_event_mask(hdev, false);
2312
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002313 return 0;
2314}
2315
Tedd Ho-Jeong Anbfbd45e2015-02-13 09:20:52 -08002316static int btusb_shutdown_intel(struct hci_dev *hdev)
2317{
2318 struct sk_buff *skb;
2319 long ret;
2320
2321 /* Some platforms have an issue with BT LED when the interface is
2322 * down or BT radio is turned off, which takes 5 seconds to BT LED
2323 * goes off. This command turns off the BT LED immediately.
2324 */
2325 skb = __hci_cmd_sync(hdev, 0xfc3f, 0, NULL, HCI_INIT_TIMEOUT);
2326 if (IS_ERR(skb)) {
2327 ret = PTR_ERR(skb);
2328 BT_ERR("%s: turning off Intel device LED failed (%ld)",
2329 hdev->name, ret);
2330 return ret;
2331 }
2332 kfree_skb(skb);
2333
2334 return 0;
2335}
2336
Amitkumar Karwarae8df492014-07-18 14:47:06 -07002337static int btusb_set_bdaddr_marvell(struct hci_dev *hdev,
2338 const bdaddr_t *bdaddr)
2339{
2340 struct sk_buff *skb;
2341 u8 buf[8];
2342 long ret;
2343
2344 buf[0] = 0xfe;
2345 buf[1] = sizeof(bdaddr_t);
2346 memcpy(buf + 2, bdaddr, sizeof(bdaddr_t));
2347
2348 skb = __hci_cmd_sync(hdev, 0xfc22, sizeof(buf), buf, HCI_INIT_TIMEOUT);
2349 if (IS_ERR(skb)) {
2350 ret = PTR_ERR(skb);
2351 BT_ERR("%s: changing Marvell device address failed (%ld)",
2352 hdev->name, ret);
2353 return ret;
2354 }
2355 kfree_skb(skb);
2356
2357 return 0;
2358}
2359
Toshi Kikuchi58592232014-12-12 10:58:05 -08002360static int btusb_set_bdaddr_ath3012(struct hci_dev *hdev,
2361 const bdaddr_t *bdaddr)
2362{
2363 struct sk_buff *skb;
2364 u8 buf[10];
2365 long ret;
2366
2367 buf[0] = 0x01;
2368 buf[1] = 0x01;
2369 buf[2] = 0x00;
2370 buf[3] = sizeof(bdaddr_t);
2371 memcpy(buf + 4, bdaddr, sizeof(bdaddr_t));
2372
2373 skb = __hci_cmd_sync(hdev, 0xfc0b, sizeof(buf), buf, HCI_INIT_TIMEOUT);
2374 if (IS_ERR(skb)) {
2375 ret = PTR_ERR(skb);
2376 BT_ERR("%s: Change address command failed (%ld)",
2377 hdev->name, ret);
2378 return ret;
2379 }
2380 kfree_skb(skb);
2381
2382 return 0;
2383}
2384
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002385#define QCA_DFU_PACKET_LEN 4096
2386
2387#define QCA_GET_TARGET_VERSION 0x09
2388#define QCA_CHECK_STATUS 0x05
2389#define QCA_DFU_DOWNLOAD 0x01
2390
2391#define QCA_SYSCFG_UPDATED 0x40
2392#define QCA_PATCH_UPDATED 0x80
2393#define QCA_DFU_TIMEOUT 3000
2394
2395struct qca_version {
2396 __le32 rom_version;
2397 __le32 patch_version;
2398 __le32 ram_version;
2399 __le32 ref_clock;
2400 __u8 reserved[4];
2401} __packed;
2402
2403struct qca_rampatch_version {
2404 __le16 rom_version;
2405 __le16 patch_version;
2406} __packed;
2407
2408struct qca_device_info {
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002409 u32 rom_version;
2410 u8 rampatch_hdr; /* length of header in rampatch */
2411 u8 nvm_hdr; /* length of header in NVM */
2412 u8 ver_offset; /* offset of version structure in rampatch */
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002413};
2414
2415static const struct qca_device_info qca_devices_table[] = {
2416 { 0x00000100, 20, 4, 10 }, /* Rome 1.0 */
2417 { 0x00000101, 20, 4, 10 }, /* Rome 1.1 */
Chan-yeol Park7f6e6362015-05-21 11:24:27 +09002418 { 0x00000200, 28, 4, 18 }, /* Rome 2.0 */
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002419 { 0x00000201, 28, 4, 18 }, /* Rome 2.1 */
2420 { 0x00000300, 28, 4, 18 }, /* Rome 3.0 */
2421 { 0x00000302, 28, 4, 18 }, /* Rome 3.2 */
2422};
2423
2424static int btusb_qca_send_vendor_req(struct hci_dev *hdev, u8 request,
2425 void *data, u16 size)
2426{
2427 struct btusb_data *btdata = hci_get_drvdata(hdev);
2428 struct usb_device *udev = btdata->udev;
2429 int pipe, err;
2430 u8 *buf;
2431
2432 buf = kmalloc(size, GFP_KERNEL);
2433 if (!buf)
2434 return -ENOMEM;
2435
2436 /* Found some of USB hosts have IOT issues with ours so that we should
2437 * not wait until HCI layer is ready.
2438 */
2439 pipe = usb_rcvctrlpipe(udev, 0);
2440 err = usb_control_msg(udev, pipe, request, USB_TYPE_VENDOR | USB_DIR_IN,
2441 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
2442 if (err < 0) {
2443 BT_ERR("%s: Failed to access otp area (%d)", hdev->name, err);
2444 goto done;
2445 }
2446
2447 memcpy(data, buf, size);
2448
2449done:
2450 kfree(buf);
2451
2452 return err;
2453}
2454
2455static int btusb_setup_qca_download_fw(struct hci_dev *hdev,
2456 const struct firmware *firmware,
2457 size_t hdr_size)
2458{
2459 struct btusb_data *btdata = hci_get_drvdata(hdev);
2460 struct usb_device *udev = btdata->udev;
2461 size_t count, size, sent = 0;
2462 int pipe, len, err;
2463 u8 *buf;
2464
2465 buf = kmalloc(QCA_DFU_PACKET_LEN, GFP_KERNEL);
2466 if (!buf)
2467 return -ENOMEM;
2468
2469 count = firmware->size;
2470
2471 size = min_t(size_t, count, hdr_size);
2472 memcpy(buf, firmware->data, size);
2473
2474 /* USB patches should go down to controller through USB path
2475 * because binary format fits to go down through USB channel.
2476 * USB control path is for patching headers and USB bulk is for
2477 * patch body.
2478 */
2479 pipe = usb_sndctrlpipe(udev, 0);
2480 err = usb_control_msg(udev, pipe, QCA_DFU_DOWNLOAD, USB_TYPE_VENDOR,
2481 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
2482 if (err < 0) {
2483 BT_ERR("%s: Failed to send headers (%d)", hdev->name, err);
2484 goto done;
2485 }
2486
2487 sent += size;
2488 count -= size;
2489
2490 while (count) {
2491 size = min_t(size_t, count, QCA_DFU_PACKET_LEN);
2492
2493 memcpy(buf, firmware->data + sent, size);
2494
2495 pipe = usb_sndbulkpipe(udev, 0x02);
2496 err = usb_bulk_msg(udev, pipe, buf, size, &len,
2497 QCA_DFU_TIMEOUT);
2498 if (err < 0) {
2499 BT_ERR("%s: Failed to send body at %zd of %zd (%d)",
2500 hdev->name, sent, firmware->size, err);
2501 break;
2502 }
2503
2504 if (size != len) {
2505 BT_ERR("%s: Failed to get bulk buffer", hdev->name);
2506 err = -EILSEQ;
2507 break;
2508 }
2509
2510 sent += size;
2511 count -= size;
2512 }
2513
2514done:
2515 kfree(buf);
2516 return err;
2517}
2518
2519static int btusb_setup_qca_load_rampatch(struct hci_dev *hdev,
2520 struct qca_version *ver,
2521 const struct qca_device_info *info)
2522{
2523 struct qca_rampatch_version *rver;
2524 const struct firmware *fw;
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002525 u32 ver_rom, ver_patch;
2526 u16 rver_rom, rver_patch;
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002527 char fwname[64];
2528 int err;
2529
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002530 ver_rom = le32_to_cpu(ver->rom_version);
2531 ver_patch = le32_to_cpu(ver->patch_version);
2532
2533 snprintf(fwname, sizeof(fwname), "qca/rampatch_usb_%08x.bin", ver_rom);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002534
2535 err = request_firmware(&fw, fwname, &hdev->dev);
2536 if (err) {
2537 BT_ERR("%s: failed to request rampatch file: %s (%d)",
2538 hdev->name, fwname, err);
2539 return err;
2540 }
2541
2542 BT_INFO("%s: using rampatch file: %s", hdev->name, fwname);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002543
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002544 rver = (struct qca_rampatch_version *)(fw->data + info->ver_offset);
2545 rver_rom = le16_to_cpu(rver->rom_version);
2546 rver_patch = le16_to_cpu(rver->patch_version);
2547
2548 BT_INFO("%s: QCA: patch rome 0x%x build 0x%x, firmware rome 0x%x "
2549 "build 0x%x", hdev->name, rver_rom, rver_patch, ver_rom,
2550 ver_patch);
2551
2552 if (rver_rom != ver_rom || rver_patch <= ver_patch) {
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002553 BT_ERR("%s: rampatch file version did not match with firmware",
2554 hdev->name);
2555 err = -EINVAL;
2556 goto done;
2557 }
2558
2559 err = btusb_setup_qca_download_fw(hdev, fw, info->rampatch_hdr);
2560
2561done:
2562 release_firmware(fw);
2563
2564 return err;
2565}
2566
2567static int btusb_setup_qca_load_nvm(struct hci_dev *hdev,
2568 struct qca_version *ver,
2569 const struct qca_device_info *info)
2570{
2571 const struct firmware *fw;
2572 char fwname[64];
2573 int err;
2574
2575 snprintf(fwname, sizeof(fwname), "qca/nvm_usb_%08x.bin",
2576 le32_to_cpu(ver->rom_version));
2577
2578 err = request_firmware(&fw, fwname, &hdev->dev);
2579 if (err) {
2580 BT_ERR("%s: failed to request NVM file: %s (%d)",
2581 hdev->name, fwname, err);
2582 return err;
2583 }
2584
2585 BT_INFO("%s: using NVM file: %s", hdev->name, fwname);
2586
2587 err = btusb_setup_qca_download_fw(hdev, fw, info->nvm_hdr);
2588
2589 release_firmware(fw);
2590
2591 return err;
2592}
2593
2594static int btusb_setup_qca(struct hci_dev *hdev)
2595{
2596 const struct qca_device_info *info = NULL;
2597 struct qca_version ver;
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002598 u32 ver_rom;
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002599 u8 status;
2600 int i, err;
2601
2602 err = btusb_qca_send_vendor_req(hdev, QCA_GET_TARGET_VERSION, &ver,
Marcel Holtmanneb500422015-04-16 23:15:50 +02002603 sizeof(ver));
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002604 if (err < 0)
2605 return err;
2606
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002607 ver_rom = le32_to_cpu(ver.rom_version);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002608 for (i = 0; i < ARRAY_SIZE(qca_devices_table); i++) {
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002609 if (ver_rom == qca_devices_table[i].rom_version)
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002610 info = &qca_devices_table[i];
2611 }
2612 if (!info) {
2613 BT_ERR("%s: don't support firmware rome 0x%x", hdev->name,
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002614 ver_rom);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002615 return -ENODEV;
2616 }
2617
2618 err = btusb_qca_send_vendor_req(hdev, QCA_CHECK_STATUS, &status,
2619 sizeof(status));
2620 if (err < 0)
2621 return err;
2622
2623 if (!(status & QCA_PATCH_UPDATED)) {
2624 err = btusb_setup_qca_load_rampatch(hdev, &ver, info);
2625 if (err < 0)
2626 return err;
2627 }
2628
2629 if (!(status & QCA_SYSCFG_UPDATED)) {
2630 err = btusb_setup_qca_load_nvm(hdev, &ver, info);
2631 if (err < 0)
2632 return err;
2633 }
2634
2635 return 0;
2636}
2637
Marcel Holtmann9d08f502015-10-08 20:23:08 +02002638#ifdef CONFIG_BT_HCIBTUSB_BCM
2639static inline int __set_diag_interface(struct hci_dev *hdev)
2640{
2641 struct btusb_data *data = hci_get_drvdata(hdev);
2642 struct usb_interface *intf = data->diag;
2643 int i;
2644
2645 if (!data->diag)
2646 return -ENODEV;
2647
2648 data->diag_tx_ep = NULL;
2649 data->diag_rx_ep = NULL;
2650
2651 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
2652 struct usb_endpoint_descriptor *ep_desc;
2653
2654 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
2655
2656 if (!data->diag_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
2657 data->diag_tx_ep = ep_desc;
2658 continue;
2659 }
2660
2661 if (!data->diag_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
2662 data->diag_rx_ep = ep_desc;
2663 continue;
2664 }
2665 }
2666
2667 if (!data->diag_tx_ep || !data->diag_rx_ep) {
2668 BT_ERR("%s invalid diagnostic descriptors", hdev->name);
2669 return -ENODEV;
2670 }
2671
2672 return 0;
2673}
2674
2675static struct urb *alloc_diag_urb(struct hci_dev *hdev, bool enable)
2676{
2677 struct btusb_data *data = hci_get_drvdata(hdev);
2678 struct sk_buff *skb;
2679 struct urb *urb;
2680 unsigned int pipe;
2681
2682 if (!data->diag_tx_ep)
2683 return ERR_PTR(-ENODEV);
2684
2685 urb = usb_alloc_urb(0, GFP_KERNEL);
2686 if (!urb)
2687 return ERR_PTR(-ENOMEM);
2688
2689 skb = bt_skb_alloc(2, GFP_KERNEL);
2690 if (!skb) {
2691 usb_free_urb(urb);
2692 return ERR_PTR(-ENOMEM);
2693 }
2694
2695 *skb_put(skb, 1) = 0xf0;
2696 *skb_put(skb, 1) = enable;
2697
2698 pipe = usb_sndbulkpipe(data->udev, data->diag_tx_ep->bEndpointAddress);
2699
2700 usb_fill_bulk_urb(urb, data->udev, pipe,
2701 skb->data, skb->len, btusb_tx_complete, skb);
2702
2703 skb->dev = (void *)hdev;
2704
2705 return urb;
2706}
2707
2708static int btusb_bcm_set_diag(struct hci_dev *hdev, bool enable)
2709{
2710 struct btusb_data *data = hci_get_drvdata(hdev);
2711 struct urb *urb;
2712
2713 if (!data->diag)
2714 return -ENODEV;
2715
2716 if (!test_bit(HCI_RUNNING, &hdev->flags))
2717 return -ENETDOWN;
2718
2719 urb = alloc_diag_urb(hdev, enable);
2720 if (IS_ERR(urb))
2721 return PTR_ERR(urb);
2722
2723 return submit_or_queue_tx_urb(hdev, urb);
2724}
2725#endif
2726
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002727static int btusb_probe(struct usb_interface *intf,
Marcel Holtmann89e75332014-09-16 04:44:50 +02002728 const struct usb_device_id *id)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002729{
2730 struct usb_endpoint_descriptor *ep_desc;
2731 struct btusb_data *data;
2732 struct hci_dev *hdev;
Marcel Holtmann22f8e9d2015-10-20 00:53:33 +02002733 unsigned ifnum_base;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002734 int i, err;
2735
2736 BT_DBG("intf %p id %p", intf, id);
2737
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002738 /* interface numbers are hardcoded in the spec */
Marcel Holtmann22f8e9d2015-10-20 00:53:33 +02002739 if (intf->cur_altsetting->desc.bInterfaceNumber != 0) {
2740 if (!(id->driver_info & BTUSB_IFNUM_2))
2741 return -ENODEV;
2742 if (intf->cur_altsetting->desc.bInterfaceNumber != 2)
2743 return -ENODEV;
2744 }
2745
2746 ifnum_base = intf->cur_altsetting->desc.bInterfaceNumber;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002747
2748 if (!id->driver_info) {
2749 const struct usb_device_id *match;
Marcel Holtmann89e75332014-09-16 04:44:50 +02002750
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002751 match = usb_match_id(intf, blacklist_table);
2752 if (match)
2753 id = match;
2754 }
2755
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002756 if (id->driver_info == BTUSB_IGNORE)
2757 return -ENODEV;
2758
Steven.Li2d25f8b2011-07-01 14:02:36 +08002759 if (id->driver_info & BTUSB_ATH3012) {
2760 struct usb_device *udev = interface_to_usbdev(intf);
2761
2762 /* Old firmware would otherwise let ath3k driver load
2763 * patch and sysconfig files */
2764 if (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x0001)
2765 return -ENODEV;
2766 }
2767
Sachin Kamat98921db2012-07-27 12:38:39 +05302768 data = devm_kzalloc(&intf->dev, sizeof(*data), GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002769 if (!data)
2770 return -ENOMEM;
2771
2772 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
2773 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
2774
2775 if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
2776 data->intr_ep = ep_desc;
2777 continue;
2778 }
2779
2780 if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
2781 data->bulk_tx_ep = ep_desc;
2782 continue;
2783 }
2784
2785 if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
2786 data->bulk_rx_ep = ep_desc;
2787 continue;
2788 }
2789 }
2790
Sachin Kamat98921db2012-07-27 12:38:39 +05302791 if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002792 return -ENODEV;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002793
Marcel Holtmann893ba542015-01-28 20:27:34 -08002794 if (id->driver_info & BTUSB_AMP) {
2795 data->cmdreq_type = USB_TYPE_CLASS | 0x01;
2796 data->cmdreq = 0x2b;
2797 } else {
2798 data->cmdreq_type = USB_TYPE_CLASS;
2799 data->cmdreq = 0x00;
2800 }
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002801
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002802 data->udev = interface_to_usbdev(intf);
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02002803 data->intf = intf;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002804
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002805 INIT_WORK(&data->work, btusb_work);
Oliver Neukum7bee5492009-08-24 23:44:59 +02002806 INIT_WORK(&data->waker, btusb_waker);
Marcel Holtmann803b5832014-09-16 08:00:29 +02002807 init_usb_anchor(&data->deferred);
2808 init_usb_anchor(&data->tx_anchor);
Oliver Neukum7bee5492009-08-24 23:44:59 +02002809 spin_lock_init(&data->txlock);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002810
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002811 init_usb_anchor(&data->intr_anchor);
2812 init_usb_anchor(&data->bulk_anchor);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02002813 init_usb_anchor(&data->isoc_anchor);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02002814 init_usb_anchor(&data->diag_anchor);
Marcel Holtmann803b5832014-09-16 08:00:29 +02002815 spin_lock_init(&data->rxlock);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002816
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002817 if (id->driver_info & BTUSB_INTEL_NEW) {
2818 data->recv_event = btusb_recv_event_intel;
2819 data->recv_bulk = btusb_recv_bulk_intel;
2820 set_bit(BTUSB_BOOTLOADER, &data->flags);
2821 } else {
2822 data->recv_event = hci_recv_frame;
2823 data->recv_bulk = btusb_recv_bulk;
2824 }
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +01002825
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002826 hdev = hci_alloc_dev();
Sachin Kamat98921db2012-07-27 12:38:39 +05302827 if (!hdev)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002828 return -ENOMEM;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002829
Marcel Holtmannc13854c2010-02-08 15:27:07 +01002830 hdev->bus = HCI_USB;
David Herrmann155961e2012-02-09 21:58:32 +01002831 hci_set_drvdata(hdev, data);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002832
Marcel Holtmann893ba542015-01-28 20:27:34 -08002833 if (id->driver_info & BTUSB_AMP)
2834 hdev->dev_type = HCI_AMP;
2835 else
Marcel Holtmannca8bee52016-07-05 14:30:14 +02002836 hdev->dev_type = HCI_PRIMARY;
Marcel Holtmann893ba542015-01-28 20:27:34 -08002837
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002838 data->hdev = hdev;
2839
2840 SET_HCIDEV_DEV(hdev, &intf->dev);
2841
Marcel Holtmann9f8f9622013-04-10 08:11:35 -07002842 hdev->open = btusb_open;
2843 hdev->close = btusb_close;
2844 hdev->flush = btusb_flush;
2845 hdev->send = btusb_send_frame;
2846 hdev->notify = btusb_notify;
2847
Marcel Holtmann6c9d4352015-10-17 14:39:27 +02002848 if (id->driver_info & BTUSB_BCM2045)
2849 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
2850
Marcel Holtmann9f8f9622013-04-10 08:11:35 -07002851 if (id->driver_info & BTUSB_BCM92035)
2852 hdev->setup = btusb_setup_bcm92035;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002853
Marcel Holtmannc2bfb102015-04-05 22:52:14 -07002854#ifdef CONFIG_BT_HCIBTUSB_BCM
Marcel Holtmannabbaf502014-07-02 00:53:48 +02002855 if (id->driver_info & BTUSB_BCM_PATCHRAM) {
Marcel Holtmann49a5f782015-10-20 02:30:48 +02002856 hdev->manufacturer = 15;
Marcel Holtmannc2bfb102015-04-05 22:52:14 -07002857 hdev->setup = btbcm_setup_patchram;
Marcel Holtmann9d08f502015-10-08 20:23:08 +02002858 hdev->set_diag = btusb_bcm_set_diag;
Marcel Holtmann1df1f592015-04-05 22:52:11 -07002859 hdev->set_bdaddr = btbcm_set_bdaddr;
Marcel Holtmann9d08f502015-10-08 20:23:08 +02002860
2861 /* Broadcom LM_DIAG Interface numbers are hardcoded */
Marcel Holtmann22f8e9d2015-10-20 00:53:33 +02002862 data->diag = usb_ifnum_to_if(data->udev, ifnum_base + 2);
Marcel Holtmannabbaf502014-07-02 00:53:48 +02002863 }
Petri Gynther10d4c672014-05-08 15:50:01 -07002864
Marcel Holtmann9d08f502015-10-08 20:23:08 +02002865 if (id->driver_info & BTUSB_BCM_APPLE) {
Marcel Holtmann49a5f782015-10-20 02:30:48 +02002866 hdev->manufacturer = 15;
Marcel Holtmannc2bfb102015-04-05 22:52:14 -07002867 hdev->setup = btbcm_setup_apple;
Marcel Holtmann9d08f502015-10-08 20:23:08 +02002868 hdev->set_diag = btusb_bcm_set_diag;
2869
2870 /* Broadcom LM_DIAG Interface numbers are hardcoded */
Marcel Holtmann22f8e9d2015-10-20 00:53:33 +02002871 data->diag = usb_ifnum_to_if(data->udev, ifnum_base + 2);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02002872 }
Marcel Holtmannc2bfb102015-04-05 22:52:14 -07002873#endif
Marcel Holtmann17b27722015-03-22 15:52:38 +01002874
Marcel Holtmanncb8d6592014-07-02 11:25:25 +02002875 if (id->driver_info & BTUSB_INTEL) {
Marcel Holtmann49a5f782015-10-20 02:30:48 +02002876 hdev->manufacturer = 2;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002877 hdev->setup = btusb_setup_intel;
Tedd Ho-Jeong Anbfbd45e2015-02-13 09:20:52 -08002878 hdev->shutdown = btusb_shutdown_intel;
Marcel Holtmann3e247672015-10-17 16:00:28 +02002879 hdev->set_diag = btintel_set_diag_mfg;
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07002880 hdev->set_bdaddr = btintel_set_bdaddr;
Jakub Pawlowskic33fb9b2015-01-30 18:55:58 -08002881 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
Jakub Pawlowskic1154842015-03-17 09:04:16 -07002882 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
Marcel Holtmann3e247672015-10-17 16:00:28 +02002883 set_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks);
Marcel Holtmanncb8d6592014-07-02 11:25:25 +02002884 }
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002885
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002886 if (id->driver_info & BTUSB_INTEL_NEW) {
Marcel Holtmann49a5f782015-10-20 02:30:48 +02002887 hdev->manufacturer = 2;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002888 hdev->send = btusb_send_frame_intel;
2889 hdev->setup = btusb_setup_intel_new;
Marcel Holtmanneeb6abe2015-07-05 14:37:39 +02002890 hdev->hw_error = btintel_hw_error;
Marcel Holtmann6d2e50d2015-10-09 14:42:08 +02002891 hdev->set_diag = btintel_set_diag;
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07002892 hdev->set_bdaddr = btintel_set_bdaddr;
Marcel Holtmannb970c5b2015-02-01 23:57:18 -08002893 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
Marcel Holtmannd8270fb2015-10-17 16:00:27 +02002894 set_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002895 }
2896
Amitkumar Karwarae8df492014-07-18 14:47:06 -07002897 if (id->driver_info & BTUSB_MARVELL)
2898 hdev->set_bdaddr = btusb_set_bdaddr_marvell;
2899
Marcel Holtmann661cf882015-01-02 23:35:20 -08002900 if (id->driver_info & BTUSB_SWAVE) {
2901 set_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks);
Marcel Holtmannd57dbe72014-12-26 04:42:33 +01002902 set_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS, &hdev->quirks);
Marcel Holtmann661cf882015-01-02 23:35:20 -08002903 }
Marcel Holtmannd57dbe72014-12-26 04:42:33 +01002904
Marcel Holtmanne4c534b2015-10-21 01:31:45 +02002905 if (id->driver_info & BTUSB_INTEL_BOOT) {
2906 hdev->manufacturer = 2;
Marcel Holtmann40df7832014-07-06 13:29:58 +02002907 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
Marcel Holtmanne4c534b2015-10-21 01:31:45 +02002908 }
Marcel Holtmann40df7832014-07-06 13:29:58 +02002909
Jakub Pawlowski79f0c872015-01-29 10:38:34 -08002910 if (id->driver_info & BTUSB_ATH3012) {
Toshi Kikuchi58592232014-12-12 10:58:05 -08002911 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
Jakub Pawlowski3d50d512015-03-17 09:04:15 -07002912 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
Jakub Pawlowski79f0c872015-01-29 10:38:34 -08002913 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
2914 }
Toshi Kikuchi58592232014-12-12 10:58:05 -08002915
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002916 if (id->driver_info & BTUSB_QCA_ROME) {
2917 data->setup_on_usb = btusb_setup_qca;
2918 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
2919 }
2920
Carlo Caionedb33c772015-05-14 10:49:09 +02002921#ifdef CONFIG_BT_HCIBTUSB_RTL
Daniel Drake04b8c812015-05-21 08:23:50 -06002922 if (id->driver_info & BTUSB_REALTEK) {
Carlo Caionedb33c772015-05-14 10:49:09 +02002923 hdev->setup = btrtl_setup_realtek;
Daniel Drake04b8c812015-05-21 08:23:50 -06002924
2925 /* Realtek devices lose their updated firmware over suspend,
2926 * but the USB hub doesn't notice any status change.
2927 * Explicitly request a device reset on resume.
2928 */
2929 set_bit(BTUSB_RESET_RESUME, &data->flags);
2930 }
Carlo Caionedb33c772015-05-14 10:49:09 +02002931#endif
Daniel Drakea2698a92015-04-16 14:09:55 -06002932
Marcel Holtmann893ba542015-01-28 20:27:34 -08002933 if (id->driver_info & BTUSB_AMP) {
2934 /* AMP controllers do not support SCO packets */
2935 data->isoc = NULL;
2936 } else {
Marcel Holtmann22f8e9d2015-10-20 00:53:33 +02002937 /* Interface orders are hardcoded in the specification */
2938 data->isoc = usb_ifnum_to_if(data->udev, ifnum_base + 1);
Marcel Holtmann893ba542015-01-28 20:27:34 -08002939 }
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02002940
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002941 if (!reset)
Szymon Janca6c511c2012-05-23 12:35:46 +02002942 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002943
2944 if (force_scofix || id->driver_info & BTUSB_WRONG_SCO_MTU) {
2945 if (!disable_scofix)
2946 set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
2947 }
2948
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02002949 if (id->driver_info & BTUSB_BROKEN_ISOC)
2950 data->isoc = NULL;
2951
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002952 if (id->driver_info & BTUSB_DIGIANSWER) {
2953 data->cmdreq_type = USB_TYPE_VENDOR;
Szymon Janca6c511c2012-05-23 12:35:46 +02002954 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002955 }
2956
2957 if (id->driver_info & BTUSB_CSR) {
2958 struct usb_device *udev = data->udev;
Marcel Holtmann81cac642014-01-03 03:02:36 -08002959 u16 bcdDevice = le16_to_cpu(udev->descriptor.bcdDevice);
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002960
2961 /* Old firmware would otherwise execute USB reset */
Marcel Holtmann81cac642014-01-03 03:02:36 -08002962 if (bcdDevice < 0x117)
Szymon Janca6c511c2012-05-23 12:35:46 +02002963 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmann81cac642014-01-03 03:02:36 -08002964
2965 /* Fake CSR devices with broken commands */
Johan Hedberg6cafcd92015-08-30 21:47:21 +03002966 if (bcdDevice <= 0x100 || bcdDevice == 0x134)
Marcel Holtmann81cac642014-01-03 03:02:36 -08002967 hdev->setup = btusb_setup_csr;
Jakub Pawlowski49c989a2015-03-17 09:04:17 -07002968
2969 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002970 }
2971
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002972 if (id->driver_info & BTUSB_SNIFFER) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02002973 struct usb_device *udev = data->udev;
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002974
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002975 /* New sniffer firmware has crippled HCI interface */
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002976 if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
2977 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
2978 }
2979
Marcel Holtmann3a5ef202014-07-06 14:53:54 +02002980 if (id->driver_info & BTUSB_INTEL_BOOT) {
2981 /* A bug in the bootloader causes that interrupt interface is
2982 * only enabled after receiving SetInterface(0, AltSetting=0).
2983 */
2984 err = usb_set_interface(data->udev, 0, 0);
2985 if (err < 0) {
2986 BT_ERR("failed to set interface 0, alt 0 %d", err);
2987 hci_free_dev(hdev);
2988 return err;
2989 }
2990 }
2991
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02002992 if (data->isoc) {
2993 err = usb_driver_claim_interface(&btusb_driver,
Marcel Holtmann89e75332014-09-16 04:44:50 +02002994 data->isoc, data);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02002995 if (err < 0) {
2996 hci_free_dev(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02002997 return err;
2998 }
2999 }
3000
Marcel Holtmann9d08f502015-10-08 20:23:08 +02003001#ifdef CONFIG_BT_HCIBTUSB_BCM
3002 if (data->diag) {
3003 if (!usb_driver_claim_interface(&btusb_driver,
3004 data->diag, data))
3005 __set_diag_interface(hdev);
3006 else
3007 data->diag = NULL;
3008 }
3009#endif
3010
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003011 err = hci_register_dev(hdev);
3012 if (err < 0) {
3013 hci_free_dev(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003014 return err;
3015 }
3016
3017 usb_set_intfdata(intf, data);
3018
3019 return 0;
3020}
3021
3022static void btusb_disconnect(struct usb_interface *intf)
3023{
3024 struct btusb_data *data = usb_get_intfdata(intf);
3025 struct hci_dev *hdev;
3026
3027 BT_DBG("intf %p", intf);
3028
3029 if (!data)
3030 return;
3031
3032 hdev = data->hdev;
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02003033 usb_set_intfdata(data->intf, NULL);
3034
3035 if (data->isoc)
3036 usb_set_intfdata(data->isoc, NULL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003037
Marcel Holtmann9d08f502015-10-08 20:23:08 +02003038 if (data->diag)
3039 usb_set_intfdata(data->diag, NULL);
3040
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003041 hci_unregister_dev(hdev);
3042
Marcel Holtmann9d08f502015-10-08 20:23:08 +02003043 if (intf == data->intf) {
3044 if (data->isoc)
3045 usb_driver_release_interface(&btusb_driver, data->isoc);
3046 if (data->diag)
3047 usb_driver_release_interface(&btusb_driver, data->diag);
3048 } else if (intf == data->isoc) {
3049 if (data->diag)
3050 usb_driver_release_interface(&btusb_driver, data->diag);
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02003051 usb_driver_release_interface(&btusb_driver, data->intf);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02003052 } else if (intf == data->diag) {
3053 usb_driver_release_interface(&btusb_driver, data->intf);
3054 if (data->isoc)
3055 usb_driver_release_interface(&btusb_driver, data->isoc);
3056 }
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02003057
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003058 hci_free_dev(hdev);
3059}
3060
Oliver Neukum7bee5492009-08-24 23:44:59 +02003061#ifdef CONFIG_PM
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003062static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
3063{
3064 struct btusb_data *data = usb_get_intfdata(intf);
3065
3066 BT_DBG("intf %p", intf);
3067
3068 if (data->suspend_count++)
3069 return 0;
3070
Oliver Neukum7bee5492009-08-24 23:44:59 +02003071 spin_lock_irq(&data->txlock);
Alan Stern5b1b0b82011-08-19 23:49:48 +02003072 if (!(PMSG_IS_AUTO(message) && data->tx_in_flight)) {
Oliver Neukum7bee5492009-08-24 23:44:59 +02003073 set_bit(BTUSB_SUSPENDING, &data->flags);
3074 spin_unlock_irq(&data->txlock);
3075 } else {
3076 spin_unlock_irq(&data->txlock);
3077 data->suspend_count--;
3078 return -EBUSY;
3079 }
3080
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003081 cancel_work_sync(&data->work);
3082
Oliver Neukum7bee5492009-08-24 23:44:59 +02003083 btusb_stop_traffic(data);
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003084 usb_kill_anchored_urbs(&data->tx_anchor);
3085
Daniel Drake04b8c812015-05-21 08:23:50 -06003086 /* Optionally request a device reset on resume, but only when
3087 * wakeups are disabled. If wakeups are enabled we assume the
3088 * device will stay powered up throughout suspend.
3089 */
3090 if (test_bit(BTUSB_RESET_RESUME, &data->flags) &&
3091 !device_may_wakeup(&data->udev->dev))
3092 data->udev->reset_resume = 1;
3093
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003094 return 0;
3095}
3096
Oliver Neukum7bee5492009-08-24 23:44:59 +02003097static void play_deferred(struct btusb_data *data)
3098{
3099 struct urb *urb;
3100 int err;
3101
3102 while ((urb = usb_get_from_anchor(&data->deferred))) {
3103 err = usb_submit_urb(urb, GFP_ATOMIC);
3104 if (err < 0)
3105 break;
3106
3107 data->tx_in_flight++;
3108 }
3109 usb_scuttle_anchored_urbs(&data->deferred);
3110}
3111
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003112static int btusb_resume(struct usb_interface *intf)
3113{
3114 struct btusb_data *data = usb_get_intfdata(intf);
3115 struct hci_dev *hdev = data->hdev;
Oliver Neukum7bee5492009-08-24 23:44:59 +02003116 int err = 0;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003117
3118 BT_DBG("intf %p", intf);
3119
3120 if (--data->suspend_count)
3121 return 0;
3122
3123 if (!test_bit(HCI_RUNNING, &hdev->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +02003124 goto done;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003125
3126 if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
3127 err = btusb_submit_intr_urb(hdev, GFP_NOIO);
3128 if (err < 0) {
3129 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02003130 goto failed;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003131 }
3132 }
3133
3134 if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) {
Marcel Holtmann43c2e572009-02-04 17:41:38 +01003135 err = btusb_submit_bulk_urb(hdev, GFP_NOIO);
3136 if (err < 0) {
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003137 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02003138 goto failed;
3139 }
3140
3141 btusb_submit_bulk_urb(hdev, GFP_NOIO);
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003142 }
3143
3144 if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
3145 if (btusb_submit_isoc_urb(hdev, GFP_NOIO) < 0)
3146 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
3147 else
3148 btusb_submit_isoc_urb(hdev, GFP_NOIO);
3149 }
3150
Oliver Neukum7bee5492009-08-24 23:44:59 +02003151 spin_lock_irq(&data->txlock);
3152 play_deferred(data);
3153 clear_bit(BTUSB_SUSPENDING, &data->flags);
3154 spin_unlock_irq(&data->txlock);
3155 schedule_work(&data->work);
3156
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003157 return 0;
Oliver Neukum7bee5492009-08-24 23:44:59 +02003158
3159failed:
3160 usb_scuttle_anchored_urbs(&data->deferred);
3161done:
3162 spin_lock_irq(&data->txlock);
3163 clear_bit(BTUSB_SUSPENDING, &data->flags);
3164 spin_unlock_irq(&data->txlock);
3165
3166 return err;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003167}
Oliver Neukum7bee5492009-08-24 23:44:59 +02003168#endif
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003169
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003170static struct usb_driver btusb_driver = {
3171 .name = "btusb",
3172 .probe = btusb_probe,
3173 .disconnect = btusb_disconnect,
Oliver Neukum7bee5492009-08-24 23:44:59 +02003174#ifdef CONFIG_PM
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003175 .suspend = btusb_suspend,
3176 .resume = btusb_resume,
Oliver Neukum7bee5492009-08-24 23:44:59 +02003177#endif
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003178 .id_table = btusb_table,
Oliver Neukum7bee5492009-08-24 23:44:59 +02003179 .supports_autosuspend = 1,
Sarah Sharpe1f12eb2012-04-23 10:08:51 -07003180 .disable_hub_initiated_lpm = 1,
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003181};
3182
Greg Kroah-Hartman93f15082011-11-18 09:47:34 -08003183module_usb_driver(btusb_driver);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003184
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02003185module_param(disable_scofix, bool, 0644);
3186MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
3187
3188module_param(force_scofix, bool, 0644);
3189MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
3190
3191module_param(reset, bool, 0644);
3192MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
3193
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003194MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
3195MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
3196MODULE_VERSION(VERSION);
3197MODULE_LICENSE("GPL");