blob: 586c1885732c288460d18a9ff06ef03e01145c4d [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>
Hans de Goede5795b072018-01-08 10:44:16 +010026#include <linux/usb/quirks.h>
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -070027#include <linux/firmware.h>
Daniel Drakea2698a92015-04-16 14:09:55 -060028#include <asm/unaligned.h>
Marcel Holtmann5e23b922007-10-20 14:12:34 +020029
30#include <net/bluetooth/bluetooth.h>
31#include <net/bluetooth/hci_core.h>
32
Marcel Holtmann4185a0f2015-04-06 00:52:12 -070033#include "btintel.h"
Marcel Holtmann1df1f592015-04-05 22:52:11 -070034#include "btbcm.h"
Carlo Caionedb33c772015-05-14 10:49:09 +020035#include "btrtl.h"
Marcel Holtmann1df1f592015-04-05 22:52:11 -070036
Marcel Holtmann34dced9b2015-04-05 22:52:16 -070037#define VERSION "0.8"
Marcel Holtmanncfeb4142008-08-07 22:26:56 +020038
Rusty Russell90ab5ee2012-01-13 09:32:20 +103039static bool disable_scofix;
40static bool force_scofix;
Marcel Holtmann7a9d4022008-11-30 12:17:26 +010041
Shailendra Verma917a3332015-05-25 23:53:40 +053042static bool reset = true;
Marcel Holtmanncfeb4142008-08-07 22:26:56 +020043
44static struct usb_driver btusb_driver;
45
46#define BTUSB_IGNORE 0x01
Marcel Holtmann7a9d4022008-11-30 12:17:26 +010047#define BTUSB_DIGIANSWER 0x02
48#define BTUSB_CSR 0x04
49#define BTUSB_SNIFFER 0x08
50#define BTUSB_BCM92035 0x10
51#define BTUSB_BROKEN_ISOC 0x20
52#define BTUSB_WRONG_SCO_MTU 0x40
Steven.Li2d25f8b2011-07-01 14:02:36 +080053#define BTUSB_ATH3012 0x80
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -070054#define BTUSB_INTEL 0x100
Marcel Holtmann40df7832014-07-06 13:29:58 +020055#define BTUSB_INTEL_BOOT 0x200
56#define BTUSB_BCM_PATCHRAM 0x400
Amitkumar Karwarae8df492014-07-18 14:47:06 -070057#define BTUSB_MARVELL 0x800
Marcel Holtmann4fcef8e2015-01-01 17:34:37 -080058#define BTUSB_SWAVE 0x1000
Marcel Holtmanncda0dd72015-01-26 21:33:48 -080059#define BTUSB_INTEL_NEW 0x2000
Marcel Holtmann893ba542015-01-28 20:27:34 -080060#define BTUSB_AMP 0x4000
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +000061#define BTUSB_QCA_ROME 0x8000
Marcel Holtmann17b27722015-03-22 15:52:38 +010062#define BTUSB_BCM_APPLE 0x10000
Daniel Drakea2698a92015-04-16 14:09:55 -060063#define BTUSB_REALTEK 0x20000
Marcel Holtmann6c9d4352015-10-17 14:39:27 +020064#define BTUSB_BCM2045 0x40000
Marcel Holtmann22f8e9d2015-10-20 00:53:33 +020065#define BTUSB_IFNUM_2 0x80000
Szymon Janc418678b2016-09-01 17:22:37 +020066#define BTUSB_CW6622 0x100000
Marcel Holtmann5e23b922007-10-20 14:12:34 +020067
Marcel Holtmann54265202013-10-11 07:46:18 -070068static const struct usb_device_id btusb_table[] = {
Marcel Holtmann5e23b922007-10-20 14:12:34 +020069 /* Generic Bluetooth USB device */
70 { USB_DEVICE_INFO(0xe0, 0x01, 0x01) },
71
Marcel Holtmann893ba542015-01-28 20:27:34 -080072 /* Generic Bluetooth AMP device */
73 { USB_DEVICE_INFO(0xe0, 0x01, 0x04), .driver_info = BTUSB_AMP },
74
Daniel Draked63b2822015-07-17 11:12:25 -060075 /* Generic Bluetooth USB interface */
76 { USB_INTERFACE_INFO(0xe0, 0x01, 0x01) },
77
Henrik Rydberg1fa65352012-08-25 19:28:06 +020078 /* Apple-specific (Broadcom) devices */
Marcel Holtmann17b27722015-03-22 15:52:38 +010079 { USB_VENDOR_AND_INTERFACE_INFO(0x05ac, 0xff, 0x01, 0x01),
Marcel Holtmann22f8e9d2015-10-20 00:53:33 +020080 .driver_info = BTUSB_BCM_APPLE | BTUSB_IFNUM_2 },
Henrik Rydberg1fa65352012-08-25 19:28:06 +020081
Cho, Yu-Chen178c0592013-06-04 21:40:26 +080082 /* MediaTek MT76x0E */
83 { USB_DEVICE(0x0e8d, 0x763f) },
84
Oliver Neukumc510eae2011-09-21 11:41:45 +020085 /* Broadcom SoftSailing reporting vendor specific */
Don Zickus2e8b50632012-03-28 16:41:11 -040086 { USB_DEVICE(0x0a5c, 0x21e1) },
Oliver Neukumc510eae2011-09-21 11:41:45 +020087
Nobuhiro Iwamatsu3cd01972010-08-20 16:24:07 +090088 /* Apple MacBookPro 7,1 */
89 { USB_DEVICE(0x05ac, 0x8213) },
90
Cyril Lacoux0a79f672010-07-14 10:29:27 +040091 /* Apple iMac11,1 */
92 { USB_DEVICE(0x05ac, 0x8215) },
93
Nobuhiro Iwamatsu9c047152010-08-20 16:24:06 +090094 /* Apple MacBookPro6,2 */
95 { USB_DEVICE(0x05ac, 0x8218) },
96
Edgar (gimli) Hucek3e3ede72010-11-04 08:04:33 +010097 /* Apple MacBookAir3,1, MacBookAir3,2 */
98 { USB_DEVICE(0x05ac, 0x821b) },
99
Pieter-Augustijn Van Malleghema63b7232011-09-07 02:28:10 -0400100 /* Apple MacBookAir4,1 */
101 { USB_DEVICE(0x05ac, 0x821f) },
102
Marc-Antoine Perennou88d377b2011-03-24 14:51:21 -0300103 /* Apple MacBookPro8,2 */
104 { USB_DEVICE(0x05ac, 0x821a) },
105
Jurgen Kramerf78b68262011-09-04 18:01:42 +0200106 /* Apple MacMini5,1 */
107 { USB_DEVICE(0x05ac, 0x8281) },
108
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200109 /* AVM BlueFRITZ! USB v2.0 */
Marcel Holtmann4fcef8e2015-01-01 17:34:37 -0800110 { USB_DEVICE(0x057c, 0x3800), .driver_info = BTUSB_SWAVE },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200111
112 /* Bluetooth Ultraport Module from IBM */
113 { USB_DEVICE(0x04bf, 0x030a) },
114
115 /* ALPS Modules with non-standard id */
116 { USB_DEVICE(0x044e, 0x3001) },
117 { USB_DEVICE(0x044e, 0x3002) },
118
119 /* Ericsson with non-standard id */
120 { USB_DEVICE(0x0bdb, 0x1002) },
121
122 /* Canyon CN-BTU1 with HID interfaces */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100123 { USB_DEVICE(0x0c10, 0x0000) },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200124
Wen-chien Jesse Sungd13431c2011-11-08 14:30:22 +0800125 /* Broadcom BCM20702A0 */
126 { USB_DEVICE(0x413c, 0x8197) },
127
Marcel Holtmannd049f4e2015-01-26 20:35:32 -0800128 /* Broadcom BCM20702B0 (Dynex/Insignia) */
129 { USB_DEVICE(0x19ff, 0x0239), .driver_info = BTUSB_BCM_PATCHRAM },
130
Santtu Rekilä2faf71c2015-10-05 15:45:27 +0300131 /* Broadcom BCM43142A0 (Foxconn/Lenovo) */
132 { USB_DEVICE(0x105b, 0xe065), .driver_info = BTUSB_BCM_PATCHRAM },
133
Steven Harms98514032012-04-13 14:45:55 -0400134 /* Foxconn - Hon Hai */
Heinrich Siebmanns6029ddc2014-12-03 19:32:22 +0100135 { USB_VENDOR_AND_INTERFACE_INFO(0x0489, 0xff, 0x01, 0x01),
136 .driver_info = BTUSB_BCM_PATCHRAM },
Steven Harms98514032012-04-13 14:45:55 -0400137
Matej Dubovy8f0c3042015-02-02 18:50:14 +0100138 /* Lite-On Technology - Broadcom based */
139 { USB_VENDOR_AND_INTERFACE_INFO(0x04ca, 0xff, 0x01, 0x01),
140 .driver_info = BTUSB_BCM_PATCHRAM },
141
Andy Shevchenko0b880062014-02-18 18:26:19 +0200142 /* Broadcom devices with vendor specific id */
Petri Gynther10d4c672014-05-08 15:50:01 -0700143 { USB_VENDOR_AND_INTERFACE_INFO(0x0a5c, 0xff, 0x01, 0x01),
144 .driver_info = BTUSB_BCM_PATCHRAM },
Gustavo Padovan92c385f2012-08-06 15:36:49 -0300145
Marcel Holtmannc2aef6e2014-07-21 14:02:33 +0200146 /* ASUSTek Computer - Broadcom based */
Rick Dunn9a5abda2015-01-17 05:29:12 +0100147 { USB_VENDOR_AND_INTERFACE_INFO(0x0b05, 0xff, 0x01, 0x01),
148 .driver_info = BTUSB_BCM_PATCHRAM },
Marcel Holtmannc2aef6e2014-07-21 14:02:33 +0200149
Ken O'Brien5bcecf32013-09-21 19:14:43 +0100150 /* Belkin F8065bf - Broadcom based */
Marcel Holtmann6331c682015-03-27 15:11:41 -0700151 { USB_VENDOR_AND_INTERFACE_INFO(0x050d, 0xff, 0x01, 0x01),
152 .driver_info = BTUSB_BCM_PATCHRAM },
Ken O'Brien5bcecf32013-09-21 19:14:43 +0100153
Jurgen Kramer9113bfd2014-02-15 12:01:09 +0100154 /* IMC Networks - Broadcom based */
Marcel Holtmann6331c682015-03-27 15:11:41 -0700155 { USB_VENDOR_AND_INTERFACE_INFO(0x13d3, 0xff, 0x01, 0x01),
156 .driver_info = BTUSB_BCM_PATCHRAM },
Jurgen Kramer9113bfd2014-02-15 12:01:09 +0100157
Dmitry Tunin1623d0b2015-12-05 14:09:36 +0300158 /* Toshiba Corp - Broadcom based */
159 { USB_VENDOR_AND_INTERFACE_INFO(0x0930, 0xff, 0x01, 0x01),
160 .driver_info = BTUSB_BCM_PATCHRAM },
161
Marcel Holtmann40df7832014-07-06 13:29:58 +0200162 /* Intel Bluetooth USB Bootloader (RAM module) */
Marcel Holtmannd92f2df2014-07-06 14:53:55 +0200163 { USB_DEVICE(0x8087, 0x0a5a),
164 .driver_info = BTUSB_INTEL_BOOT | BTUSB_BROKEN_ISOC },
Marcel Holtmann40df7832014-07-06 13:29:58 +0200165
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200166 { } /* Terminating entry */
167};
168
169MODULE_DEVICE_TABLE(usb, btusb_table);
170
Marcel Holtmann54265202013-10-11 07:46:18 -0700171static const struct usb_device_id blacklist_table[] = {
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200172 /* CSR BlueCore devices */
173 { USB_DEVICE(0x0a12, 0x0001), .driver_info = BTUSB_CSR },
174
175 /* Broadcom BCM2033 without firmware */
176 { USB_DEVICE(0x0a5c, 0x2033), .driver_info = BTUSB_IGNORE },
177
Marcel Holtmann6c9d4352015-10-17 14:39:27 +0200178 /* Broadcom BCM2045 devices */
179 { USB_DEVICE(0x0a5c, 0x2045), .driver_info = BTUSB_BCM2045 },
180
Bala Shanmugambe931122010-11-26 17:35:46 +0530181 /* Atheros 3011 with sflash firmware */
Andy Shevchenko0b880062014-02-18 18:26:19 +0200182 { USB_DEVICE(0x0489, 0xe027), .driver_info = BTUSB_IGNORE },
183 { USB_DEVICE(0x0489, 0xe03d), .driver_info = BTUSB_IGNORE },
Alexander Ploumistos2eeff0b2015-02-13 21:05:11 +0200184 { USB_DEVICE(0x04f2, 0xaff1), .driver_info = BTUSB_IGNORE },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200185 { USB_DEVICE(0x0930, 0x0215), .driver_info = BTUSB_IGNORE },
Bala Shanmugambe931122010-11-26 17:35:46 +0530186 { USB_DEVICE(0x0cf3, 0x3002), .driver_info = BTUSB_IGNORE },
Marek Vasut6eda5412012-06-08 14:32:50 +0200187 { USB_DEVICE(0x0cf3, 0xe019), .driver_info = BTUSB_IGNORE },
Andy Ross2a7bccc2011-05-09 16:11:16 -0700188 { USB_DEVICE(0x13d3, 0x3304), .driver_info = BTUSB_IGNORE },
Bala Shanmugambe931122010-11-26 17:35:46 +0530189
Cho, Yu-Chen509e7862011-01-26 17:10:59 +0800190 /* Atheros AR9285 Malbec with sflash firmware */
191 { USB_DEVICE(0x03f0, 0x311d), .driver_info = BTUSB_IGNORE },
192
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530193 /* Atheros 3012 with sflash firmware */
Andy Shevchenko0b880062014-02-18 18:26:19 +0200194 { USB_DEVICE(0x0489, 0xe04d), .driver_info = BTUSB_ATH3012 },
195 { USB_DEVICE(0x0489, 0xe04e), .driver_info = BTUSB_ATH3012 },
196 { USB_DEVICE(0x0489, 0xe056), .driver_info = BTUSB_ATH3012 },
197 { USB_DEVICE(0x0489, 0xe057), .driver_info = BTUSB_ATH3012 },
198 { USB_DEVICE(0x0489, 0xe05f), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin692c0622015-06-06 20:25:40 +0300199 { USB_DEVICE(0x0489, 0xe076), .driver_info = BTUSB_ATH3012 },
Anantha Krishnan4b552bc2014-10-06 16:31:49 +0530200 { USB_DEVICE(0x0489, 0xe078), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin28c971d2016-02-10 00:49:11 +0300201 { USB_DEVICE(0x0489, 0xe095), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200202 { USB_DEVICE(0x04c5, 0x1330), .driver_info = BTUSB_ATH3012 },
203 { USB_DEVICE(0x04ca, 0x3004), .driver_info = BTUSB_ATH3012 },
204 { USB_DEVICE(0x04ca, 0x3005), .driver_info = BTUSB_ATH3012 },
205 { USB_DEVICE(0x04ca, 0x3006), .driver_info = BTUSB_ATH3012 },
Mohammed Habibulla1fb4e092014-04-17 11:37:13 -0700206 { USB_DEVICE(0x04ca, 0x3007), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200207 { USB_DEVICE(0x04ca, 0x3008), .driver_info = BTUSB_ATH3012 },
208 { USB_DEVICE(0x04ca, 0x300b), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin7e730c72015-06-18 20:41:51 +0300209 { USB_DEVICE(0x04ca, 0x300d), .driver_info = BTUSB_ATH3012 },
Dmitry Tuninec0810d2015-05-02 13:36:58 +0300210 { USB_DEVICE(0x04ca, 0x300f), .driver_info = BTUSB_ATH3012 },
Janne Heikkinen134d3b32014-12-09 07:44:51 +0200211 { USB_DEVICE(0x04ca, 0x3010), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin81d90442016-02-28 11:04:06 +0300212 { USB_DEVICE(0x04ca, 0x3014), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin519b6ce2017-01-05 13:19:53 +0300213 { USB_DEVICE(0x04ca, 0x3018), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200214 { USB_DEVICE(0x0930, 0x0219), .driver_info = BTUSB_ATH3012 },
Dmitry Tunincd355ff2015-10-05 19:29:33 +0300215 { USB_DEVICE(0x0930, 0x021c), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200216 { USB_DEVICE(0x0930, 0x0220), .driver_info = BTUSB_ATH3012 },
Vincent Zwanenburg89d29752014-08-08 12:33:56 +0100217 { USB_DEVICE(0x0930, 0x0227), .driver_info = BTUSB_ATH3012 },
Andy Shevchenkoa735f9e2014-02-18 18:26:20 +0200218 { USB_DEVICE(0x0b05, 0x17d0), .driver_info = BTUSB_ATH3012 },
Ming Leid66629c2013-03-15 11:00:39 +0800219 { USB_DEVICE(0x0cf3, 0x0036), .driver_info = BTUSB_ATH3012 },
Hans de Goedec7a2c152018-04-26 14:18:19 +0200220 { USB_DEVICE(0x0cf3, 0x3004), .driver_info = BTUSB_ATH3012 },
Sunguk Lee94a32d102013-03-12 04:41:58 +0900221 { USB_DEVICE(0x0cf3, 0x3008), .driver_info = BTUSB_ATH3012 },
Cho, Yu-Chen07c0ea82012-03-14 22:01:21 +0200222 { USB_DEVICE(0x0cf3, 0x311d), .driver_info = BTUSB_ATH3012 },
Oliver Neukumb1312372014-01-16 15:37:11 +0100223 { USB_DEVICE(0x0cf3, 0x311e), .driver_info = BTUSB_ATH3012 },
Oliver Neukum1e56f1e2014-01-16 16:02:58 +0100224 { USB_DEVICE(0x0cf3, 0x311f), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200225 { USB_DEVICE(0x0cf3, 0x3121), .driver_info = BTUSB_ATH3012 },
Ming Leiebaf5792013-03-18 23:45:11 +0800226 { USB_DEVICE(0x0cf3, 0x817a), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin18e0afa2015-10-16 11:45:26 +0300227 { USB_DEVICE(0x0cf3, 0x817b), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200228 { USB_DEVICE(0x0cf3, 0xe003), .driver_info = BTUSB_ATH3012 },
AceLan Kaoac713112012-04-19 14:53:45 +0800229 { USB_DEVICE(0x0cf3, 0xe004), .driver_info = BTUSB_ATH3012 },
Peng Chen0a3658c2013-08-30 17:41:40 +0800230 { USB_DEVICE(0x0cf3, 0xe005), .driver_info = BTUSB_ATH3012 },
Wen-chien Jesse Sungca79f232015-05-13 11:39:24 +0800231 { USB_DEVICE(0x0cf3, 0xe006), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200232 { USB_DEVICE(0x13d3, 0x3362), .driver_info = BTUSB_ATH3012 },
233 { USB_DEVICE(0x13d3, 0x3375), .driver_info = BTUSB_ATH3012 },
AceLan Kaoeed307e2012-12-11 11:41:20 +0800234 { USB_DEVICE(0x13d3, 0x3393), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin609574e2016-02-10 15:33:17 +0300235 { USB_DEVICE(0x13d3, 0x3395), .driver_info = BTUSB_ATH3012 },
Sujith Manoharan5b77a1f2013-07-15 09:29:03 +0530236 { USB_DEVICE(0x13d3, 0x3402), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin3bb30a7c2014-11-25 20:19:52 +0300237 { USB_DEVICE(0x13d3, 0x3408), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin033efa92015-01-18 00:16:51 +0300238 { USB_DEVICE(0x13d3, 0x3423), .driver_info = BTUSB_ATH3012 },
Anantha Krishnanfa2f1392014-07-08 19:25:08 +0530239 { USB_DEVICE(0x13d3, 0x3432), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin75c6aca2016-03-04 01:32:19 +0300240 { USB_DEVICE(0x13d3, 0x3472), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin0d0cef62015-06-06 20:29:25 +0300241 { USB_DEVICE(0x13d3, 0x3474), .driver_info = BTUSB_ATH3012 },
Lauro Costa72f9f8b2016-05-09 17:36:11 -0300242 { USB_DEVICE(0x13d3, 0x3487), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin12d86892016-07-12 01:35:18 +0300243 { USB_DEVICE(0x13d3, 0x3490), .driver_info = BTUSB_ATH3012 },
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530244
Cho, Yu-Chene9036e32011-02-15 10:20:07 +0800245 /* Atheros AR5BBU12 with sflash firmware */
246 { USB_DEVICE(0x0489, 0xe02c), .driver_info = BTUSB_IGNORE },
247
Michael Gruetzner85d59722012-05-02 22:33:40 +0200248 /* Atheros AR5BBU12 with sflash firmware */
Yevgeniy Melnichukbc21fde2012-08-07 19:48:10 +0530249 { USB_DEVICE(0x0489, 0xe036), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200250 { USB_DEVICE(0x0489, 0xe03c), .driver_info = BTUSB_ATH3012 },
Michael Gruetzner85d59722012-05-02 22:33:40 +0200251
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +0000252 /* QCA ROME chipset */
Wen-chien Jesse Sung20541112015-05-13 11:39:25 +0800253 { USB_DEVICE(0x0cf3, 0xe007), .driver_info = BTUSB_QCA_ROME },
Kai-Heng Feng93519932016-08-16 12:50:06 +0800254 { USB_DEVICE(0x0cf3, 0xe009), .driver_info = BTUSB_QCA_ROME },
Marcel Holtmannc9e44472015-03-16 23:56:04 -0700255 { USB_DEVICE(0x0cf3, 0xe300), .driver_info = BTUSB_QCA_ROME },
256 { USB_DEVICE(0x0cf3, 0xe360), .driver_info = BTUSB_QCA_ROME },
Yvain THONNART47ca5892016-06-11 15:43:30 +0200257 { USB_DEVICE(0x0489, 0xe092), .driver_info = BTUSB_QCA_ROME },
Dmitry Tunin1144a4e2016-09-21 19:13:08 +0300258 { USB_DEVICE(0x04ca, 0x3011), .driver_info = BTUSB_QCA_ROME },
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +0000259
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200260 /* Broadcom BCM2035 */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100261 { USB_DEVICE(0x0a5c, 0x2009), .driver_info = BTUSB_BCM92035 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200262 { USB_DEVICE(0x0a5c, 0x200a), .driver_info = BTUSB_WRONG_SCO_MTU },
263 { USB_DEVICE(0x0a5c, 0x2035), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200264
265 /* Broadcom BCM2045 */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100266 { USB_DEVICE(0x0a5c, 0x2039), .driver_info = BTUSB_WRONG_SCO_MTU },
267 { USB_DEVICE(0x0a5c, 0x2101), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmannbdbef3d2008-09-23 00:16:35 +0200268
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200269 /* IBM/Lenovo ThinkPad with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100270 { USB_DEVICE(0x0a5c, 0x201e), .driver_info = BTUSB_WRONG_SCO_MTU },
271 { USB_DEVICE(0x0a5c, 0x2110), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200272
273 /* HP laptop with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100274 { USB_DEVICE(0x03f0, 0x171d), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200275
276 /* Dell laptop with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100277 { USB_DEVICE(0x413c, 0x8126), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200278
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100279 /* Dell Wireless 370 and 410 devices */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100280 { USB_DEVICE(0x413c, 0x8152), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100281 { USB_DEVICE(0x413c, 0x8156), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200282
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100283 /* Belkin F8T012 and F8T013 devices */
284 { USB_DEVICE(0x050d, 0x0012), .driver_info = BTUSB_WRONG_SCO_MTU },
285 { USB_DEVICE(0x050d, 0x0013), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200286
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100287 /* Asus WL-BTD202 device */
288 { USB_DEVICE(0x0b05, 0x1715), .driver_info = BTUSB_WRONG_SCO_MTU },
289
290 /* Kensington Bluetooth USB adapter */
291 { USB_DEVICE(0x047d, 0x105e), .driver_info = BTUSB_WRONG_SCO_MTU },
292
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200293 /* RTX Telecom based adapters with buggy SCO support */
294 { USB_DEVICE(0x0400, 0x0807), .driver_info = BTUSB_BROKEN_ISOC },
295 { USB_DEVICE(0x0400, 0x080a), .driver_info = BTUSB_BROKEN_ISOC },
296
297 /* CONWISE Technology based adapters with buggy SCO support */
Szymon Janc418678b2016-09-01 17:22:37 +0200298 { USB_DEVICE(0x0e5e, 0x6622),
299 .driver_info = BTUSB_BROKEN_ISOC | BTUSB_CW6622},
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200300
Marcel Holtmann4fcef8e2015-01-01 17:34:37 -0800301 /* Roper Class 1 Bluetooth Dongle (Silicon Wave based) */
Aleksei Volkov2eeac872015-06-08 12:02:10 +0300302 { USB_DEVICE(0x1310, 0x0001), .driver_info = BTUSB_SWAVE },
Marcel Holtmann4fcef8e2015-01-01 17:34:37 -0800303
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200304 /* Digianswer devices */
305 { USB_DEVICE(0x08fd, 0x0001), .driver_info = BTUSB_DIGIANSWER },
306 { USB_DEVICE(0x08fd, 0x0002), .driver_info = BTUSB_IGNORE },
307
308 /* CSR BlueCore Bluetooth Sniffer */
Marcel Holtmann4f64fa82014-07-07 00:12:04 +0200309 { USB_DEVICE(0x0a12, 0x0002),
310 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200311
312 /* Frontline ComProbe Bluetooth Sniffer */
Marcel Holtmann4f64fa82014-07-07 00:12:04 +0200313 { USB_DEVICE(0x16d3, 0x0002),
314 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200315
Marcel Holtmanncb1ee892015-01-28 19:41:42 -0800316 /* Marvell Bluetooth devices */
317 { USB_DEVICE(0x1286, 0x2044), .driver_info = BTUSB_MARVELL },
318 { USB_DEVICE(0x1286, 0x2046), .driver_info = BTUSB_MARVELL },
Amitkumar Karwar1165df02016-09-28 16:18:35 +0530319 { USB_DEVICE(0x1286, 0x204e), .driver_info = BTUSB_MARVELL },
Marcel Holtmanncb1ee892015-01-28 19:41:42 -0800320
Marcel Holtmannd0ac9eb2015-01-28 19:41:43 -0800321 /* Intel Bluetooth devices */
Marcel Holtmann407550f2015-02-22 15:41:18 -0800322 { USB_DEVICE(0x8087, 0x07da), .driver_info = BTUSB_CSR },
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -0700323 { USB_DEVICE(0x8087, 0x07dc), .driver_info = BTUSB_INTEL },
Tedd Ho-Jeong Anef4e5e42013-11-12 13:10:58 -0800324 { USB_DEVICE(0x8087, 0x0a2a), .driver_info = BTUSB_INTEL },
Marcel Holtmanncda0dd72015-01-26 21:33:48 -0800325 { USB_DEVICE(0x8087, 0x0a2b), .driver_info = BTUSB_INTEL_NEW },
Tedd Ho-Jeong An439e65d2016-06-20 13:43:40 -0700326 { USB_DEVICE(0x8087, 0x0aa7), .driver_info = BTUSB_INTEL },
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -0700327
Marcel Holtmannd0ac9eb2015-01-28 19:41:43 -0800328 /* Other Intel Bluetooth devices */
329 { USB_VENDOR_AND_INTERFACE_INFO(0x8087, 0xe0, 0x01, 0x01),
330 .driver_info = BTUSB_IGNORE },
Amitkumar Karwarae8df492014-07-18 14:47:06 -0700331
Daniel Drakea2698a92015-04-16 14:09:55 -0600332 /* Realtek Bluetooth devices */
333 { USB_VENDOR_AND_INTERFACE_INFO(0x0bda, 0xe0, 0x01, 0x01),
334 .driver_info = BTUSB_REALTEK },
335
336 /* Additional Realtek 8723AE Bluetooth devices */
337 { USB_DEVICE(0x0930, 0x021d), .driver_info = BTUSB_REALTEK },
338 { USB_DEVICE(0x13d3, 0x3394), .driver_info = BTUSB_REALTEK },
339
340 /* Additional Realtek 8723BE Bluetooth devices */
341 { USB_DEVICE(0x0489, 0xe085), .driver_info = BTUSB_REALTEK },
342 { USB_DEVICE(0x0489, 0xe08b), .driver_info = BTUSB_REALTEK },
343 { USB_DEVICE(0x13d3, 0x3410), .driver_info = BTUSB_REALTEK },
344 { USB_DEVICE(0x13d3, 0x3416), .driver_info = BTUSB_REALTEK },
345 { USB_DEVICE(0x13d3, 0x3459), .driver_info = BTUSB_REALTEK },
Dmitry Tunin74756262017-08-08 14:09:02 +0300346 { USB_DEVICE(0x13d3, 0x3494), .driver_info = BTUSB_REALTEK },
Daniel Drakea2698a92015-04-16 14:09:55 -0600347
Vicente Bergase6e57a82018-03-20 19:41:10 +0100348 /* Additional Realtek 8723BU Bluetooth devices */
349 { USB_DEVICE(0x7392, 0xa611), .driver_info = BTUSB_REALTEK },
350
Daniel Drakea2698a92015-04-16 14:09:55 -0600351 /* Additional Realtek 8821AE Bluetooth devices */
352 { USB_DEVICE(0x0b05, 0x17dc), .driver_info = BTUSB_REALTEK },
353 { USB_DEVICE(0x13d3, 0x3414), .driver_info = BTUSB_REALTEK },
354 { USB_DEVICE(0x13d3, 0x3458), .driver_info = BTUSB_REALTEK },
355 { USB_DEVICE(0x13d3, 0x3461), .driver_info = BTUSB_REALTEK },
356 { USB_DEVICE(0x13d3, 0x3462), .driver_info = BTUSB_REALTEK },
357
Peter Poklop4481c072015-08-15 20:47:09 +0200358 /* Silicon Wave based devices */
359 { USB_DEVICE(0x0c10, 0x0000), .driver_info = BTUSB_SWAVE },
360
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200361 { } /* Terminating entry */
362};
363
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200364#define BTUSB_MAX_ISOC_FRAMES 10
365
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200366#define BTUSB_INTR_RUNNING 0
367#define BTUSB_BULK_RUNNING 1
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200368#define BTUSB_ISOC_RUNNING 2
Oliver Neukum7bee5492009-08-24 23:44:59 +0200369#define BTUSB_SUSPENDING 3
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -0300370#define BTUSB_DID_ISO_RESUME 4
Marcel Holtmanncda0dd72015-01-26 21:33:48 -0800371#define BTUSB_BOOTLOADER 5
372#define BTUSB_DOWNLOADING 6
Marcel Holtmannce6bb922015-01-28 01:58:40 -0800373#define BTUSB_FIRMWARE_LOADED 7
Marcel Holtmanncda0dd72015-01-26 21:33:48 -0800374#define BTUSB_FIRMWARE_FAILED 8
Marcel Holtmannce6bb922015-01-28 01:58:40 -0800375#define BTUSB_BOOTING 9
Hans de Goede5795b072018-01-08 10:44:16 +0100376#define BTUSB_DIAG_RUNNING 10
377#define BTUSB_OOB_WAKE_ENABLED 11
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200378
379struct btusb_data {
380 struct hci_dev *hdev;
381 struct usb_device *udev;
Marcel Holtmann5fbcd262008-09-23 00:16:36 +0200382 struct usb_interface *intf;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200383 struct usb_interface *isoc;
Marcel Holtmann9d08f502015-10-08 20:23:08 +0200384 struct usb_interface *diag;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200385
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200386 unsigned long flags;
387
388 struct work_struct work;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200389 struct work_struct waker;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200390
Marcel Holtmann803b5832014-09-16 08:00:29 +0200391 struct usb_anchor deferred;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200392 struct usb_anchor tx_anchor;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200393 int tx_in_flight;
394 spinlock_t txlock;
395
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200396 struct usb_anchor intr_anchor;
397 struct usb_anchor bulk_anchor;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200398 struct usb_anchor isoc_anchor;
Marcel Holtmann9d08f502015-10-08 20:23:08 +0200399 struct usb_anchor diag_anchor;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200400 spinlock_t rxlock;
401
402 struct sk_buff *evt_skb;
403 struct sk_buff *acl_skb;
404 struct sk_buff *sco_skb;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200405
406 struct usb_endpoint_descriptor *intr_ep;
407 struct usb_endpoint_descriptor *bulk_tx_ep;
408 struct usb_endpoint_descriptor *bulk_rx_ep;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200409 struct usb_endpoint_descriptor *isoc_tx_ep;
410 struct usb_endpoint_descriptor *isoc_rx_ep;
Marcel Holtmann9d08f502015-10-08 20:23:08 +0200411 struct usb_endpoint_descriptor *diag_tx_ep;
412 struct usb_endpoint_descriptor *diag_rx_ep;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200413
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100414 __u8 cmdreq_type;
Marcel Holtmann893ba542015-01-28 20:27:34 -0800415 __u8 cmdreq;
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100416
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100417 unsigned int sco_num;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200418 int isoc_altsetting;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +0100419 int suspend_count;
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +0100420
Marcel Holtmann97307f52015-01-12 13:51:10 -0800421 int (*recv_event)(struct hci_dev *hdev, struct sk_buff *skb);
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +0100422 int (*recv_bulk)(struct btusb_data *data, void *buffer, int count);
Kim, Ben Young Taeace31982015-02-15 23:06:14 +0000423
424 int (*setup_on_usb)(struct hci_dev *hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200425};
426
Marcel Holtmann803b5832014-09-16 08:00:29 +0200427static inline void btusb_free_frags(struct btusb_data *data)
428{
429 unsigned long flags;
430
431 spin_lock_irqsave(&data->rxlock, flags);
432
433 kfree_skb(data->evt_skb);
434 data->evt_skb = NULL;
435
436 kfree_skb(data->acl_skb);
437 data->acl_skb = NULL;
438
439 kfree_skb(data->sco_skb);
440 data->sco_skb = NULL;
441
442 spin_unlock_irqrestore(&data->rxlock, flags);
443}
444
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200445static int btusb_recv_intr(struct btusb_data *data, void *buffer, int count)
446{
Marcel Holtmann803b5832014-09-16 08:00:29 +0200447 struct sk_buff *skb;
448 int err = 0;
449
450 spin_lock(&data->rxlock);
451 skb = data->evt_skb;
452
453 while (count) {
454 int len;
455
456 if (!skb) {
457 skb = bt_skb_alloc(HCI_MAX_EVENT_SIZE, GFP_ATOMIC);
458 if (!skb) {
459 err = -ENOMEM;
460 break;
461 }
462
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100463 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
464 hci_skb_expect(skb) = HCI_EVENT_HDR_SIZE;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200465 }
466
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100467 len = min_t(uint, hci_skb_expect(skb), count);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200468 memcpy(skb_put(skb, len), buffer, len);
469
470 count -= len;
471 buffer += len;
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100472 hci_skb_expect(skb) -= len;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200473
474 if (skb->len == HCI_EVENT_HDR_SIZE) {
475 /* Complete event header */
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100476 hci_skb_expect(skb) = hci_event_hdr(skb)->plen;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200477
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100478 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
Marcel Holtmann803b5832014-09-16 08:00:29 +0200479 kfree_skb(skb);
480 skb = NULL;
481
482 err = -EILSEQ;
483 break;
484 }
485 }
486
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100487 if (!hci_skb_expect(skb)) {
Marcel Holtmann803b5832014-09-16 08:00:29 +0200488 /* Complete frame */
Marcel Holtmann97307f52015-01-12 13:51:10 -0800489 data->recv_event(data->hdev, skb);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200490 skb = NULL;
491 }
492 }
493
494 data->evt_skb = skb;
495 spin_unlock(&data->rxlock);
496
497 return err;
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200498}
499
500static int btusb_recv_bulk(struct btusb_data *data, void *buffer, int count)
501{
Marcel Holtmann803b5832014-09-16 08:00:29 +0200502 struct sk_buff *skb;
503 int err = 0;
504
505 spin_lock(&data->rxlock);
506 skb = data->acl_skb;
507
508 while (count) {
509 int len;
510
511 if (!skb) {
512 skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
513 if (!skb) {
514 err = -ENOMEM;
515 break;
516 }
517
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100518 hci_skb_pkt_type(skb) = HCI_ACLDATA_PKT;
519 hci_skb_expect(skb) = HCI_ACL_HDR_SIZE;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200520 }
521
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100522 len = min_t(uint, hci_skb_expect(skb), count);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200523 memcpy(skb_put(skb, len), buffer, len);
524
525 count -= len;
526 buffer += len;
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100527 hci_skb_expect(skb) -= len;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200528
529 if (skb->len == HCI_ACL_HDR_SIZE) {
530 __le16 dlen = hci_acl_hdr(skb)->dlen;
531
532 /* Complete ACL header */
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100533 hci_skb_expect(skb) = __le16_to_cpu(dlen);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200534
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100535 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
Marcel Holtmann803b5832014-09-16 08:00:29 +0200536 kfree_skb(skb);
537 skb = NULL;
538
539 err = -EILSEQ;
540 break;
541 }
542 }
543
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100544 if (!hci_skb_expect(skb)) {
Marcel Holtmann803b5832014-09-16 08:00:29 +0200545 /* Complete frame */
546 hci_recv_frame(data->hdev, skb);
547 skb = NULL;
548 }
549 }
550
551 data->acl_skb = skb;
552 spin_unlock(&data->rxlock);
553
554 return err;
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200555}
556
557static int btusb_recv_isoc(struct btusb_data *data, void *buffer, int count)
558{
Marcel Holtmann803b5832014-09-16 08:00:29 +0200559 struct sk_buff *skb;
560 int err = 0;
561
562 spin_lock(&data->rxlock);
563 skb = data->sco_skb;
564
565 while (count) {
566 int len;
567
568 if (!skb) {
569 skb = bt_skb_alloc(HCI_MAX_SCO_SIZE, GFP_ATOMIC);
570 if (!skb) {
571 err = -ENOMEM;
572 break;
573 }
574
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100575 hci_skb_pkt_type(skb) = HCI_SCODATA_PKT;
576 hci_skb_expect(skb) = HCI_SCO_HDR_SIZE;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200577 }
578
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100579 len = min_t(uint, hci_skb_expect(skb), count);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200580 memcpy(skb_put(skb, len), buffer, len);
581
582 count -= len;
583 buffer += len;
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100584 hci_skb_expect(skb) -= len;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200585
586 if (skb->len == HCI_SCO_HDR_SIZE) {
587 /* Complete SCO header */
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100588 hci_skb_expect(skb) = hci_sco_hdr(skb)->dlen;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200589
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100590 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
Marcel Holtmann803b5832014-09-16 08:00:29 +0200591 kfree_skb(skb);
592 skb = NULL;
593
594 err = -EILSEQ;
595 break;
596 }
597 }
598
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100599 if (!hci_skb_expect(skb)) {
Marcel Holtmann803b5832014-09-16 08:00:29 +0200600 /* Complete frame */
601 hci_recv_frame(data->hdev, skb);
602 skb = NULL;
603 }
604 }
605
606 data->sco_skb = skb;
607 spin_unlock(&data->rxlock);
608
609 return err;
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200610}
611
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200612static void btusb_intr_complete(struct urb *urb)
613{
614 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +0100615 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200616 int err;
617
Marcel Holtmann89e75332014-09-16 04:44:50 +0200618 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
619 urb->actual_length);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200620
621 if (!test_bit(HCI_RUNNING, &hdev->flags))
622 return;
623
624 if (urb->status == 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200625 hdev->stat.byte_rx += urb->actual_length;
626
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200627 if (btusb_recv_intr(data, urb->transfer_buffer,
628 urb->actual_length) < 0) {
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200629 BT_ERR("%s corrupted event packet", hdev->name);
630 hdev->stat.err_rx++;
631 }
Champion Chen85560c42014-09-06 14:06:08 -0500632 } else if (urb->status == -ENOENT) {
633 /* Avoid suspend failed when usb_kill_urb */
634 return;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200635 }
636
637 if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
638 return;
639
Oliver Neukum7bee5492009-08-24 23:44:59 +0200640 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200641 usb_anchor_urb(urb, &data->intr_anchor);
642
643 err = usb_submit_urb(urb, GFP_ATOMIC);
644 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200645 /* -EPERM: urb is being killed;
646 * -ENODEV: device got disconnected */
647 if (err != -EPERM && err != -ENODEV)
Stefan Seyfried61faddf2010-11-30 21:49:08 +0100648 BT_ERR("%s urb %p failed to resubmit (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200649 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200650 usb_unanchor_urb(urb);
651 }
652}
653
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100654static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200655{
David Herrmann155961e2012-02-09 21:58:32 +0100656 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200657 struct urb *urb;
658 unsigned char *buf;
659 unsigned int pipe;
660 int err, size;
661
662 BT_DBG("%s", hdev->name);
663
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200664 if (!data->intr_ep)
665 return -ENODEV;
666
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100667 urb = usb_alloc_urb(0, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200668 if (!urb)
669 return -ENOMEM;
670
671 size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
672
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100673 buf = kmalloc(size, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200674 if (!buf) {
675 usb_free_urb(urb);
676 return -ENOMEM;
677 }
678
679 pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
680
681 usb_fill_int_urb(urb, data->udev, pipe, buf, size,
Marcel Holtmann89e75332014-09-16 04:44:50 +0200682 btusb_intr_complete, hdev, data->intr_ep->bInterval);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200683
684 urb->transfer_flags |= URB_FREE_BUFFER;
685
686 usb_anchor_urb(urb, &data->intr_anchor);
687
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100688 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200689 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +0200690 if (err != -EPERM && err != -ENODEV)
691 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200692 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200693 usb_unanchor_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200694 }
695
696 usb_free_urb(urb);
697
698 return err;
699}
700
701static void btusb_bulk_complete(struct urb *urb)
702{
703 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +0100704 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200705 int err;
706
Marcel Holtmann89e75332014-09-16 04:44:50 +0200707 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
708 urb->actual_length);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200709
710 if (!test_bit(HCI_RUNNING, &hdev->flags))
711 return;
712
713 if (urb->status == 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200714 hdev->stat.byte_rx += urb->actual_length;
715
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +0100716 if (data->recv_bulk(data, urb->transfer_buffer,
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200717 urb->actual_length) < 0) {
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200718 BT_ERR("%s corrupted ACL packet", hdev->name);
719 hdev->stat.err_rx++;
720 }
Champion Chen85560c42014-09-06 14:06:08 -0500721 } else if (urb->status == -ENOENT) {
722 /* Avoid suspend failed when usb_kill_urb */
723 return;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200724 }
725
726 if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
727 return;
728
729 usb_anchor_urb(urb, &data->bulk_anchor);
Oliver Neukum652fd782009-12-16 19:23:43 +0100730 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200731
732 err = usb_submit_urb(urb, GFP_ATOMIC);
733 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200734 /* -EPERM: urb is being killed;
735 * -ENODEV: device got disconnected */
736 if (err != -EPERM && err != -ENODEV)
Stefan Seyfried61faddf2010-11-30 21:49:08 +0100737 BT_ERR("%s urb %p failed to resubmit (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200738 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200739 usb_unanchor_urb(urb);
740 }
741}
742
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100743static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200744{
David Herrmann155961e2012-02-09 21:58:32 +0100745 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200746 struct urb *urb;
747 unsigned char *buf;
748 unsigned int pipe;
Vikram Kandukuri290ba202009-07-02 14:31:59 +0530749 int err, size = HCI_MAX_FRAME_SIZE;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200750
751 BT_DBG("%s", hdev->name);
752
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200753 if (!data->bulk_rx_ep)
754 return -ENODEV;
755
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100756 urb = usb_alloc_urb(0, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200757 if (!urb)
758 return -ENOMEM;
759
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100760 buf = kmalloc(size, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200761 if (!buf) {
762 usb_free_urb(urb);
763 return -ENOMEM;
764 }
765
766 pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
767
Marcel Holtmann89e75332014-09-16 04:44:50 +0200768 usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
769 btusb_bulk_complete, hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200770
771 urb->transfer_flags |= URB_FREE_BUFFER;
772
Oliver Neukum7bee5492009-08-24 23:44:59 +0200773 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200774 usb_anchor_urb(urb, &data->bulk_anchor);
775
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100776 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200777 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +0200778 if (err != -EPERM && err != -ENODEV)
779 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200780 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200781 usb_unanchor_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200782 }
783
784 usb_free_urb(urb);
785
786 return err;
787}
788
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200789static void btusb_isoc_complete(struct urb *urb)
790{
791 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +0100792 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200793 int i, err;
794
Marcel Holtmann89e75332014-09-16 04:44:50 +0200795 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
796 urb->actual_length);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200797
798 if (!test_bit(HCI_RUNNING, &hdev->flags))
799 return;
800
801 if (urb->status == 0) {
802 for (i = 0; i < urb->number_of_packets; i++) {
803 unsigned int offset = urb->iso_frame_desc[i].offset;
804 unsigned int length = urb->iso_frame_desc[i].actual_length;
805
806 if (urb->iso_frame_desc[i].status)
807 continue;
808
809 hdev->stat.byte_rx += length;
810
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200811 if (btusb_recv_isoc(data, urb->transfer_buffer + offset,
812 length) < 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200813 BT_ERR("%s corrupted SCO packet", hdev->name);
814 hdev->stat.err_rx++;
815 }
816 }
Champion Chen85560c42014-09-06 14:06:08 -0500817 } else if (urb->status == -ENOENT) {
818 /* Avoid suspend failed when usb_kill_urb */
819 return;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200820 }
821
822 if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags))
823 return;
824
825 usb_anchor_urb(urb, &data->isoc_anchor);
826
827 err = usb_submit_urb(urb, GFP_ATOMIC);
828 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200829 /* -EPERM: urb is being killed;
830 * -ENODEV: device got disconnected */
831 if (err != -EPERM && err != -ENODEV)
Stefan Seyfried61faddf2010-11-30 21:49:08 +0100832 BT_ERR("%s urb %p failed to resubmit (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200833 hdev->name, urb, -err);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200834 usb_unanchor_urb(urb);
835 }
836}
837
Jesper Juhl42b16b32011-01-17 00:09:38 +0100838static inline void __fill_isoc_descriptor(struct urb *urb, int len, int mtu)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200839{
840 int i, offset = 0;
841
842 BT_DBG("len %d mtu %d", len, mtu);
843
844 for (i = 0; i < BTUSB_MAX_ISOC_FRAMES && len >= mtu;
845 i++, offset += mtu, len -= mtu) {
846 urb->iso_frame_desc[i].offset = offset;
847 urb->iso_frame_desc[i].length = mtu;
848 }
849
850 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
851 urb->iso_frame_desc[i].offset = offset;
852 urb->iso_frame_desc[i].length = len;
853 i++;
854 }
855
856 urb->number_of_packets = i;
857}
858
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100859static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200860{
David Herrmann155961e2012-02-09 21:58:32 +0100861 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200862 struct urb *urb;
863 unsigned char *buf;
864 unsigned int pipe;
865 int err, size;
866
867 BT_DBG("%s", hdev->name);
868
869 if (!data->isoc_rx_ep)
870 return -ENODEV;
871
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100872 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200873 if (!urb)
874 return -ENOMEM;
875
876 size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) *
877 BTUSB_MAX_ISOC_FRAMES;
878
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100879 buf = kmalloc(size, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200880 if (!buf) {
881 usb_free_urb(urb);
882 return -ENOMEM;
883 }
884
885 pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress);
886
Bing Zhaofa0fb932011-12-20 18:19:00 -0800887 usb_fill_int_urb(urb, data->udev, pipe, buf, size, btusb_isoc_complete,
Marcel Holtmann89e75332014-09-16 04:44:50 +0200888 hdev, data->isoc_rx_ep->bInterval);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200889
Marcel Holtmann89e75332014-09-16 04:44:50 +0200890 urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200891
892 __fill_isoc_descriptor(urb, size,
Marcel Holtmann89e75332014-09-16 04:44:50 +0200893 le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize));
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200894
895 usb_anchor_urb(urb, &data->isoc_anchor);
896
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100897 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200898 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +0200899 if (err != -EPERM && err != -ENODEV)
900 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200901 hdev->name, urb, -err);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200902 usb_unanchor_urb(urb);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200903 }
904
905 usb_free_urb(urb);
906
907 return err;
908}
909
Marcel Holtmann9d08f502015-10-08 20:23:08 +0200910static void btusb_diag_complete(struct urb *urb)
911{
912 struct hci_dev *hdev = urb->context;
913 struct btusb_data *data = hci_get_drvdata(hdev);
914 int err;
915
916 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
917 urb->actual_length);
918
919 if (urb->status == 0) {
920 struct sk_buff *skb;
921
922 skb = bt_skb_alloc(urb->actual_length, GFP_ATOMIC);
923 if (skb) {
924 memcpy(skb_put(skb, urb->actual_length),
925 urb->transfer_buffer, urb->actual_length);
926 hci_recv_diag(hdev, skb);
927 }
928 } else if (urb->status == -ENOENT) {
929 /* Avoid suspend failed when usb_kill_urb */
930 return;
931 }
932
933 if (!test_bit(BTUSB_DIAG_RUNNING, &data->flags))
934 return;
935
936 usb_anchor_urb(urb, &data->diag_anchor);
937 usb_mark_last_busy(data->udev);
938
939 err = usb_submit_urb(urb, GFP_ATOMIC);
940 if (err < 0) {
941 /* -EPERM: urb is being killed;
942 * -ENODEV: device got disconnected */
943 if (err != -EPERM && err != -ENODEV)
944 BT_ERR("%s urb %p failed to resubmit (%d)",
945 hdev->name, urb, -err);
946 usb_unanchor_urb(urb);
947 }
948}
949
950static int btusb_submit_diag_urb(struct hci_dev *hdev, gfp_t mem_flags)
951{
952 struct btusb_data *data = hci_get_drvdata(hdev);
953 struct urb *urb;
954 unsigned char *buf;
955 unsigned int pipe;
956 int err, size = HCI_MAX_FRAME_SIZE;
957
958 BT_DBG("%s", hdev->name);
959
960 if (!data->diag_rx_ep)
961 return -ENODEV;
962
963 urb = usb_alloc_urb(0, mem_flags);
964 if (!urb)
965 return -ENOMEM;
966
967 buf = kmalloc(size, mem_flags);
968 if (!buf) {
969 usb_free_urb(urb);
970 return -ENOMEM;
971 }
972
973 pipe = usb_rcvbulkpipe(data->udev, data->diag_rx_ep->bEndpointAddress);
974
975 usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
976 btusb_diag_complete, hdev);
977
978 urb->transfer_flags |= URB_FREE_BUFFER;
979
980 usb_mark_last_busy(data->udev);
981 usb_anchor_urb(urb, &data->diag_anchor);
982
983 err = usb_submit_urb(urb, mem_flags);
984 if (err < 0) {
985 if (err != -EPERM && err != -ENODEV)
986 BT_ERR("%s urb %p submission failed (%d)",
987 hdev->name, urb, -err);
988 usb_unanchor_urb(urb);
989 }
990
991 usb_free_urb(urb);
992
993 return err;
994}
995
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200996static void btusb_tx_complete(struct urb *urb)
997{
998 struct sk_buff *skb = urb->context;
Marcel Holtmann89e75332014-09-16 04:44:50 +0200999 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
David Herrmann155961e2012-02-09 21:58:32 +01001000 struct btusb_data *data = hci_get_drvdata(hdev);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001001
Marcel Holtmann89e75332014-09-16 04:44:50 +02001002 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1003 urb->actual_length);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001004
1005 if (!test_bit(HCI_RUNNING, &hdev->flags))
1006 goto done;
1007
1008 if (!urb->status)
1009 hdev->stat.byte_tx += urb->transfer_buffer_length;
1010 else
1011 hdev->stat.err_tx++;
1012
1013done:
1014 spin_lock(&data->txlock);
1015 data->tx_in_flight--;
1016 spin_unlock(&data->txlock);
1017
1018 kfree(urb->setup_packet);
1019
1020 kfree_skb(skb);
1021}
1022
1023static void btusb_isoc_tx_complete(struct urb *urb)
1024{
1025 struct sk_buff *skb = urb->context;
Marcel Holtmann89e75332014-09-16 04:44:50 +02001026 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001027
Marcel Holtmann89e75332014-09-16 04:44:50 +02001028 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1029 urb->actual_length);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001030
1031 if (!test_bit(HCI_RUNNING, &hdev->flags))
1032 goto done;
1033
1034 if (!urb->status)
1035 hdev->stat.byte_tx += urb->transfer_buffer_length;
1036 else
1037 hdev->stat.err_tx++;
1038
1039done:
1040 kfree(urb->setup_packet);
1041
1042 kfree_skb(skb);
1043}
1044
1045static int btusb_open(struct hci_dev *hdev)
1046{
David Herrmann155961e2012-02-09 21:58:32 +01001047 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001048 int err;
1049
1050 BT_DBG("%s", hdev->name);
1051
Ethan Hsiehc7e163f2016-10-07 12:06:42 +08001052 err = usb_autopm_get_interface(data->intf);
1053 if (err < 0)
1054 return err;
1055
Kim, Ben Young Taeace31982015-02-15 23:06:14 +00001056 /* Patching USB firmware files prior to starting any URBs of HCI path
1057 * It is more safe to use USB bulk channel for downloading USB patch
1058 */
1059 if (data->setup_on_usb) {
1060 err = data->setup_on_usb(hdev);
Marcel Holtmanneb500422015-04-16 23:15:50 +02001061 if (err < 0)
Kim, Ben Young Taeace31982015-02-15 23:06:14 +00001062 return err;
1063 }
1064
Oliver Neukum7bee5492009-08-24 23:44:59 +02001065 data->intf->needs_remote_wakeup = 1;
1066
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001067 if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +02001068 goto done;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001069
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001070 err = btusb_submit_intr_urb(hdev, GFP_KERNEL);
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001071 if (err < 0)
1072 goto failed;
1073
1074 err = btusb_submit_bulk_urb(hdev, GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001075 if (err < 0) {
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001076 usb_kill_anchored_urbs(&data->intr_anchor);
1077 goto failed;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001078 }
1079
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001080 set_bit(BTUSB_BULK_RUNNING, &data->flags);
1081 btusb_submit_bulk_urb(hdev, GFP_KERNEL);
1082
Marcel Holtmann9d08f502015-10-08 20:23:08 +02001083 if (data->diag) {
1084 if (!btusb_submit_diag_urb(hdev, GFP_KERNEL))
1085 set_bit(BTUSB_DIAG_RUNNING, &data->flags);
1086 }
1087
Oliver Neukum7bee5492009-08-24 23:44:59 +02001088done:
1089 usb_autopm_put_interface(data->intf);
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001090 return 0;
1091
1092failed:
1093 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001094 usb_autopm_put_interface(data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001095 return err;
1096}
1097
Oliver Neukum7bee5492009-08-24 23:44:59 +02001098static void btusb_stop_traffic(struct btusb_data *data)
1099{
1100 usb_kill_anchored_urbs(&data->intr_anchor);
1101 usb_kill_anchored_urbs(&data->bulk_anchor);
1102 usb_kill_anchored_urbs(&data->isoc_anchor);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02001103 usb_kill_anchored_urbs(&data->diag_anchor);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001104}
1105
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001106static int btusb_close(struct hci_dev *hdev)
1107{
David Herrmann155961e2012-02-09 21:58:32 +01001108 struct btusb_data *data = hci_get_drvdata(hdev);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001109 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001110
1111 BT_DBG("%s", hdev->name);
1112
Marcel Holtmanne8c3c3d2008-09-23 00:16:36 +02001113 cancel_work_sync(&data->work);
Linus Torvalds404291a2009-11-11 13:32:29 -08001114 cancel_work_sync(&data->waker);
Marcel Holtmanne8c3c3d2008-09-23 00:16:36 +02001115
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001116 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001117 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001118 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02001119 clear_bit(BTUSB_DIAG_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001120
1121 btusb_stop_traffic(data);
Marcel Holtmann803b5832014-09-16 08:00:29 +02001122 btusb_free_frags(data);
1123
Oliver Neukum7bee5492009-08-24 23:44:59 +02001124 err = usb_autopm_get_interface(data->intf);
1125 if (err < 0)
Oliver Neukum7b8e2c12009-11-13 14:26:23 +01001126 goto failed;
Oliver Neukum7bee5492009-08-24 23:44:59 +02001127
1128 data->intf->needs_remote_wakeup = 0;
1129 usb_autopm_put_interface(data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001130
Oliver Neukum7b8e2c12009-11-13 14:26:23 +01001131failed:
1132 usb_scuttle_anchored_urbs(&data->deferred);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001133 return 0;
1134}
1135
1136static int btusb_flush(struct hci_dev *hdev)
1137{
David Herrmann155961e2012-02-09 21:58:32 +01001138 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001139
1140 BT_DBG("%s", hdev->name);
1141
1142 usb_kill_anchored_urbs(&data->tx_anchor);
Marcel Holtmann803b5832014-09-16 08:00:29 +02001143 btusb_free_frags(data);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001144
1145 return 0;
1146}
1147
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001148static struct urb *alloc_ctrl_urb(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001149{
David Herrmann155961e2012-02-09 21:58:32 +01001150 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001151 struct usb_ctrlrequest *dr;
1152 struct urb *urb;
1153 unsigned int pipe;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001154
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001155 urb = usb_alloc_urb(0, GFP_KERNEL);
1156 if (!urb)
1157 return ERR_PTR(-ENOMEM);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001158
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001159 dr = kmalloc(sizeof(*dr), GFP_KERNEL);
1160 if (!dr) {
1161 usb_free_urb(urb);
1162 return ERR_PTR(-ENOMEM);
1163 }
1164
1165 dr->bRequestType = data->cmdreq_type;
Marcel Holtmann893ba542015-01-28 20:27:34 -08001166 dr->bRequest = data->cmdreq;
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001167 dr->wIndex = 0;
1168 dr->wValue = 0;
1169 dr->wLength = __cpu_to_le16(skb->len);
1170
1171 pipe = usb_sndctrlpipe(data->udev, 0x00);
1172
Marcel Holtmann89e75332014-09-16 04:44:50 +02001173 usb_fill_control_urb(urb, data->udev, pipe, (void *)dr,
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001174 skb->data, skb->len, btusb_tx_complete, skb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001175
Marcel Holtmann89e75332014-09-16 04:44:50 +02001176 skb->dev = (void *)hdev;
Marcel Holtmann7bd8f092013-10-11 06:19:18 -07001177
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001178 return urb;
1179}
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001180
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001181static struct urb *alloc_bulk_urb(struct hci_dev *hdev, struct sk_buff *skb)
1182{
1183 struct btusb_data *data = hci_get_drvdata(hdev);
1184 struct urb *urb;
1185 unsigned int pipe;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001186
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001187 if (!data->bulk_tx_ep)
1188 return ERR_PTR(-ENODEV);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001189
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001190 urb = usb_alloc_urb(0, GFP_KERNEL);
1191 if (!urb)
1192 return ERR_PTR(-ENOMEM);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001193
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001194 pipe = usb_sndbulkpipe(data->udev, data->bulk_tx_ep->bEndpointAddress);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001195
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001196 usb_fill_bulk_urb(urb, data->udev, pipe,
1197 skb->data, skb->len, btusb_tx_complete, skb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001198
Marcel Holtmann89e75332014-09-16 04:44:50 +02001199 skb->dev = (void *)hdev;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001200
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001201 return urb;
1202}
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001203
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001204static struct urb *alloc_isoc_urb(struct hci_dev *hdev, struct sk_buff *skb)
1205{
1206 struct btusb_data *data = hci_get_drvdata(hdev);
1207 struct urb *urb;
1208 unsigned int pipe;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001209
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001210 if (!data->isoc_tx_ep)
1211 return ERR_PTR(-ENODEV);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001212
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001213 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_KERNEL);
1214 if (!urb)
1215 return ERR_PTR(-ENOMEM);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001216
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001217 pipe = usb_sndisocpipe(data->udev, data->isoc_tx_ep->bEndpointAddress);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001218
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001219 usb_fill_int_urb(urb, data->udev, pipe,
1220 skb->data, skb->len, btusb_isoc_tx_complete,
1221 skb, data->isoc_tx_ep->bInterval);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001222
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001223 urb->transfer_flags = URB_ISO_ASAP;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001224
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001225 __fill_isoc_descriptor(urb, skb->len,
1226 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001227
Marcel Holtmann89e75332014-09-16 04:44:50 +02001228 skb->dev = (void *)hdev;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001229
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001230 return urb;
1231}
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001232
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001233static int submit_tx_urb(struct hci_dev *hdev, struct urb *urb)
1234{
1235 struct btusb_data *data = hci_get_drvdata(hdev);
1236 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001237
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001238 usb_anchor_urb(urb, &data->tx_anchor);
1239
Johan Hedberge9753ef2014-09-14 08:49:34 +03001240 err = usb_submit_urb(urb, GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001241 if (err < 0) {
Paul Bolle5a9b80e2011-10-09 12:12:16 +02001242 if (err != -EPERM && err != -ENODEV)
1243 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +02001244 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001245 kfree(urb->setup_packet);
1246 usb_unanchor_urb(urb);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001247 } else {
1248 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001249 }
1250
Cong Wang54a8a792011-11-22 09:32:57 +08001251 usb_free_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001252 return err;
1253}
1254
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001255static int submit_or_queue_tx_urb(struct hci_dev *hdev, struct urb *urb)
1256{
1257 struct btusb_data *data = hci_get_drvdata(hdev);
1258 unsigned long flags;
1259 bool suspending;
1260
1261 spin_lock_irqsave(&data->txlock, flags);
1262 suspending = test_bit(BTUSB_SUSPENDING, &data->flags);
1263 if (!suspending)
1264 data->tx_in_flight++;
1265 spin_unlock_irqrestore(&data->txlock, flags);
1266
1267 if (!suspending)
1268 return submit_tx_urb(hdev, urb);
1269
1270 usb_anchor_urb(urb, &data->deferred);
1271 schedule_work(&data->waker);
1272
1273 usb_free_urb(urb);
1274 return 0;
1275}
1276
1277static int btusb_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
1278{
1279 struct urb *urb;
1280
1281 BT_DBG("%s", hdev->name);
1282
Marcel Holtmann618e8bc2015-11-05 07:33:56 +01001283 switch (hci_skb_pkt_type(skb)) {
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001284 case HCI_COMMAND_PKT:
1285 urb = alloc_ctrl_urb(hdev, skb);
1286 if (IS_ERR(urb))
1287 return PTR_ERR(urb);
1288
1289 hdev->stat.cmd_tx++;
1290 return submit_or_queue_tx_urb(hdev, urb);
1291
1292 case HCI_ACLDATA_PKT:
1293 urb = alloc_bulk_urb(hdev, skb);
1294 if (IS_ERR(urb))
1295 return PTR_ERR(urb);
1296
1297 hdev->stat.acl_tx++;
1298 return submit_or_queue_tx_urb(hdev, urb);
1299
1300 case HCI_SCODATA_PKT:
1301 if (hci_conn_num(hdev, SCO_LINK) < 1)
1302 return -ENODEV;
1303
1304 urb = alloc_isoc_urb(hdev, skb);
1305 if (IS_ERR(urb))
1306 return PTR_ERR(urb);
1307
1308 hdev->stat.sco_tx++;
1309 return submit_tx_urb(hdev, urb);
1310 }
1311
1312 return -EILSEQ;
1313}
1314
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001315static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
1316{
David Herrmann155961e2012-02-09 21:58:32 +01001317 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001318
1319 BT_DBG("%s evt %d", hdev->name, evt);
1320
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001321 if (hci_conn_num(hdev, SCO_LINK) != data->sco_num) {
1322 data->sco_num = hci_conn_num(hdev, SCO_LINK);
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001323 schedule_work(&data->work);
Marcel Holtmanna780efa2008-11-30 12:17:12 +01001324 }
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001325}
1326
Jesper Juhl42b16b32011-01-17 00:09:38 +01001327static inline int __set_isoc_interface(struct hci_dev *hdev, int altsetting)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001328{
David Herrmann155961e2012-02-09 21:58:32 +01001329 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001330 struct usb_interface *intf = data->isoc;
1331 struct usb_endpoint_descriptor *ep_desc;
1332 int i, err;
1333
1334 if (!data->isoc)
1335 return -ENODEV;
1336
1337 err = usb_set_interface(data->udev, 1, altsetting);
1338 if (err < 0) {
1339 BT_ERR("%s setting interface failed (%d)", hdev->name, -err);
1340 return err;
1341 }
1342
1343 data->isoc_altsetting = altsetting;
1344
1345 data->isoc_tx_ep = NULL;
1346 data->isoc_rx_ep = NULL;
1347
1348 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
1349 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
1350
1351 if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) {
1352 data->isoc_tx_ep = ep_desc;
1353 continue;
1354 }
1355
1356 if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) {
1357 data->isoc_rx_ep = ep_desc;
1358 continue;
1359 }
1360 }
1361
1362 if (!data->isoc_tx_ep || !data->isoc_rx_ep) {
1363 BT_ERR("%s invalid SCO descriptors", hdev->name);
1364 return -ENODEV;
1365 }
1366
1367 return 0;
1368}
1369
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001370static void btusb_work(struct work_struct *work)
1371{
1372 struct btusb_data *data = container_of(work, struct btusb_data, work);
1373 struct hci_dev *hdev = data->hdev;
Mikel Astizf4001d22012-04-11 08:48:51 +02001374 int new_alts;
Oliver Neukum7bee5492009-08-24 23:44:59 +02001375 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001376
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001377 if (data->sco_num > 0) {
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -03001378 if (!test_bit(BTUSB_DID_ISO_RESUME, &data->flags)) {
Oliver Neukum8efdd0c2011-02-11 13:00:06 +01001379 err = usb_autopm_get_interface(data->isoc ? data->isoc : data->intf);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001380 if (err < 0) {
1381 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1382 usb_kill_anchored_urbs(&data->isoc_anchor);
1383 return;
1384 }
1385
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -03001386 set_bit(BTUSB_DID_ISO_RESUME, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001387 }
Mikel Astizf4001d22012-04-11 08:48:51 +02001388
1389 if (hdev->voice_setting & 0x0020) {
1390 static const int alts[3] = { 2, 4, 5 };
Marcel Holtmann89e75332014-09-16 04:44:50 +02001391
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001392 new_alts = alts[data->sco_num - 1];
Mikel Astizf4001d22012-04-11 08:48:51 +02001393 } else {
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001394 new_alts = data->sco_num;
Mikel Astizf4001d22012-04-11 08:48:51 +02001395 }
1396
1397 if (data->isoc_altsetting != new_alts) {
Kuba Pawlakf6fc86f2015-10-28 15:18:05 +01001398 unsigned long flags;
1399
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001400 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1401 usb_kill_anchored_urbs(&data->isoc_anchor);
1402
Kuba Pawlak8f9d02f2015-09-10 17:07:00 +01001403 /* When isochronous alternate setting needs to be
1404 * changed, because SCO connection has been added
1405 * or removed, a packet fragment may be left in the
1406 * reassembling state. This could lead to wrongly
1407 * assembled fragments.
1408 *
1409 * Clear outstanding fragment when selecting a new
1410 * alternate setting.
1411 */
Kuba Pawlakf6fc86f2015-10-28 15:18:05 +01001412 spin_lock_irqsave(&data->rxlock, flags);
Kuba Pawlak8f9d02f2015-09-10 17:07:00 +01001413 kfree_skb(data->sco_skb);
1414 data->sco_skb = NULL;
Kuba Pawlakf6fc86f2015-10-28 15:18:05 +01001415 spin_unlock_irqrestore(&data->rxlock, flags);
Kuba Pawlak8f9d02f2015-09-10 17:07:00 +01001416
Mikel Astizf4001d22012-04-11 08:48:51 +02001417 if (__set_isoc_interface(hdev, new_alts) < 0)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001418 return;
1419 }
1420
1421 if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001422 if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001423 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1424 else
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001425 btusb_submit_isoc_urb(hdev, GFP_KERNEL);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001426 }
1427 } else {
1428 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1429 usb_kill_anchored_urbs(&data->isoc_anchor);
1430
1431 __set_isoc_interface(hdev, 0);
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -03001432 if (test_and_clear_bit(BTUSB_DID_ISO_RESUME, &data->flags))
Oliver Neukum8efdd0c2011-02-11 13:00:06 +01001433 usb_autopm_put_interface(data->isoc ? data->isoc : data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001434 }
1435}
1436
Oliver Neukum7bee5492009-08-24 23:44:59 +02001437static void btusb_waker(struct work_struct *work)
1438{
1439 struct btusb_data *data = container_of(work, struct btusb_data, waker);
1440 int err;
1441
1442 err = usb_autopm_get_interface(data->intf);
1443 if (err < 0)
1444 return;
1445
1446 usb_autopm_put_interface(data->intf);
1447}
1448
Marcel Holtmann9f8f9622013-04-10 08:11:35 -07001449static int btusb_setup_bcm92035(struct hci_dev *hdev)
1450{
1451 struct sk_buff *skb;
1452 u8 val = 0x00;
1453
1454 BT_DBG("%s", hdev->name);
1455
1456 skb = __hci_cmd_sync(hdev, 0xfc3b, 1, &val, HCI_INIT_TIMEOUT);
1457 if (IS_ERR(skb))
1458 BT_ERR("BCM92035 command failed (%ld)", -PTR_ERR(skb));
1459 else
1460 kfree_skb(skb);
1461
1462 return 0;
1463}
1464
Marcel Holtmann81cac642014-01-03 03:02:36 -08001465static int btusb_setup_csr(struct hci_dev *hdev)
1466{
1467 struct hci_rp_read_local_version *rp;
1468 struct sk_buff *skb;
Marcel Holtmann81cac642014-01-03 03:02:36 -08001469
1470 BT_DBG("%s", hdev->name);
1471
Marcel Holtmann7cd84d72015-06-07 10:01:02 +02001472 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
1473 HCI_INIT_TIMEOUT);
1474 if (IS_ERR(skb)) {
1475 int err = PTR_ERR(skb);
1476 BT_ERR("%s: CSR: Local version failed (%d)", hdev->name, err);
1477 return err;
1478 }
1479
1480 if (skb->len != sizeof(struct hci_rp_read_local_version)) {
1481 BT_ERR("%s: CSR: Local version length mismatch", hdev->name);
1482 kfree_skb(skb);
1483 return -EIO;
1484 }
Marcel Holtmann81cac642014-01-03 03:02:36 -08001485
Marcel Holtmann89e75332014-09-16 04:44:50 +02001486 rp = (struct hci_rp_read_local_version *)skb->data;
Marcel Holtmann81cac642014-01-03 03:02:36 -08001487
Johan Hedberg6cafcd92015-08-30 21:47:21 +03001488 /* Detect controllers which aren't real CSR ones. */
1489 if (le16_to_cpu(rp->manufacturer) != 10 ||
1490 le16_to_cpu(rp->lmp_subver) == 0x0c5c) {
Marcel Holtmann9641d342015-06-07 10:01:01 +02001491 /* Clear the reset quirk since this is not an actual
1492 * early Bluetooth 1.1 device from CSR.
1493 */
1494 clear_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmann81cac642014-01-03 03:02:36 -08001495
Marcel Holtmann9641d342015-06-07 10:01:01 +02001496 /* These fake CSR controllers have all a broken
1497 * stored link key handling and so just disable it.
1498 */
1499 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
Marcel Holtmann81cac642014-01-03 03:02:36 -08001500 }
1501
Marcel Holtmann81cac642014-01-03 03:02:36 -08001502 kfree_skb(skb);
1503
Marcel Holtmann9641d342015-06-07 10:01:01 +02001504 return 0;
Marcel Holtmann81cac642014-01-03 03:02:36 -08001505}
1506
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001507static const struct firmware *btusb_setup_intel_get_fw(struct hci_dev *hdev,
Marcel Holtmann89e75332014-09-16 04:44:50 +02001508 struct intel_version *ver)
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001509{
1510 const struct firmware *fw;
1511 char fwname[64];
1512 int ret;
1513
1514 snprintf(fwname, sizeof(fwname),
1515 "intel/ibt-hw-%x.%x.%x-fw-%x.%x.%x.%x.%x.bseq",
1516 ver->hw_platform, ver->hw_variant, ver->hw_revision,
1517 ver->fw_variant, ver->fw_revision, ver->fw_build_num,
1518 ver->fw_build_ww, ver->fw_build_yy);
1519
1520 ret = request_firmware(&fw, fwname, &hdev->dev);
1521 if (ret < 0) {
1522 if (ret == -EINVAL) {
1523 BT_ERR("%s Intel firmware file request failed (%d)",
1524 hdev->name, ret);
1525 return NULL;
1526 }
1527
1528 BT_ERR("%s failed to open Intel firmware file: %s(%d)",
1529 hdev->name, fwname, ret);
1530
1531 /* If the correct firmware patch file is not found, use the
1532 * default firmware patch file instead
1533 */
1534 snprintf(fwname, sizeof(fwname), "intel/ibt-hw-%x.%x.bseq",
1535 ver->hw_platform, ver->hw_variant);
1536 if (request_firmware(&fw, fwname, &hdev->dev) < 0) {
1537 BT_ERR("%s failed to open default Intel fw file: %s",
1538 hdev->name, fwname);
1539 return NULL;
1540 }
1541 }
1542
1543 BT_INFO("%s: Intel Bluetooth firmware file: %s", hdev->name, fwname);
1544
1545 return fw;
1546}
1547
1548static int btusb_setup_intel_patching(struct hci_dev *hdev,
1549 const struct firmware *fw,
1550 const u8 **fw_ptr, int *disable_patch)
1551{
1552 struct sk_buff *skb;
1553 struct hci_command_hdr *cmd;
1554 const u8 *cmd_param;
1555 struct hci_event_hdr *evt = NULL;
1556 const u8 *evt_param = NULL;
1557 int remain = fw->size - (*fw_ptr - fw->data);
1558
1559 /* The first byte indicates the types of the patch command or event.
1560 * 0x01 means HCI command and 0x02 is HCI event. If the first bytes
1561 * in the current firmware buffer doesn't start with 0x01 or
1562 * the size of remain buffer is smaller than HCI command header,
1563 * the firmware file is corrupted and it should stop the patching
1564 * process.
1565 */
1566 if (remain > HCI_COMMAND_HDR_SIZE && *fw_ptr[0] != 0x01) {
1567 BT_ERR("%s Intel fw corrupted: invalid cmd read", hdev->name);
1568 return -EINVAL;
1569 }
1570 (*fw_ptr)++;
1571 remain--;
1572
1573 cmd = (struct hci_command_hdr *)(*fw_ptr);
1574 *fw_ptr += sizeof(*cmd);
1575 remain -= sizeof(*cmd);
1576
1577 /* Ensure that the remain firmware data is long enough than the length
1578 * of command parameter. If not, the firmware file is corrupted.
1579 */
1580 if (remain < cmd->plen) {
1581 BT_ERR("%s Intel fw corrupted: invalid cmd len", hdev->name);
1582 return -EFAULT;
1583 }
1584
1585 /* If there is a command that loads a patch in the firmware
1586 * file, then enable the patch upon success, otherwise just
1587 * disable the manufacturer mode, for example patch activation
1588 * is not required when the default firmware patch file is used
1589 * because there are no patch data to load.
1590 */
1591 if (*disable_patch && le16_to_cpu(cmd->opcode) == 0xfc8e)
1592 *disable_patch = 0;
1593
1594 cmd_param = *fw_ptr;
1595 *fw_ptr += cmd->plen;
1596 remain -= cmd->plen;
1597
1598 /* This reads the expected events when the above command is sent to the
1599 * device. Some vendor commands expects more than one events, for
1600 * example command status event followed by vendor specific event.
1601 * For this case, it only keeps the last expected event. so the command
1602 * can be sent with __hci_cmd_sync_ev() which returns the sk_buff of
1603 * last expected event.
1604 */
1605 while (remain > HCI_EVENT_HDR_SIZE && *fw_ptr[0] == 0x02) {
1606 (*fw_ptr)++;
1607 remain--;
1608
1609 evt = (struct hci_event_hdr *)(*fw_ptr);
1610 *fw_ptr += sizeof(*evt);
1611 remain -= sizeof(*evt);
1612
1613 if (remain < evt->plen) {
1614 BT_ERR("%s Intel fw corrupted: invalid evt len",
1615 hdev->name);
1616 return -EFAULT;
1617 }
1618
1619 evt_param = *fw_ptr;
1620 *fw_ptr += evt->plen;
1621 remain -= evt->plen;
1622 }
1623
1624 /* Every HCI commands in the firmware file has its correspond event.
1625 * If event is not found or remain is smaller than zero, the firmware
1626 * file is corrupted.
1627 */
1628 if (!evt || !evt_param || remain < 0) {
1629 BT_ERR("%s Intel fw corrupted: invalid evt read", hdev->name);
1630 return -EFAULT;
1631 }
1632
1633 skb = __hci_cmd_sync_ev(hdev, le16_to_cpu(cmd->opcode), cmd->plen,
1634 cmd_param, evt->evt, HCI_INIT_TIMEOUT);
1635 if (IS_ERR(skb)) {
1636 BT_ERR("%s sending Intel patch command (0x%4.4x) failed (%ld)",
1637 hdev->name, cmd->opcode, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001638 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001639 }
1640
1641 /* It ensures that the returned event matches the event data read from
1642 * the firmware file. At fist, it checks the length and then
1643 * the contents of the event.
1644 */
1645 if (skb->len != evt->plen) {
1646 BT_ERR("%s mismatch event length (opcode 0x%4.4x)", hdev->name,
1647 le16_to_cpu(cmd->opcode));
1648 kfree_skb(skb);
1649 return -EFAULT;
1650 }
1651
1652 if (memcmp(skb->data, evt_param, evt->plen)) {
1653 BT_ERR("%s mismatch event parameter (opcode 0x%4.4x)",
1654 hdev->name, le16_to_cpu(cmd->opcode));
1655 kfree_skb(skb);
1656 return -EFAULT;
1657 }
1658 kfree_skb(skb);
1659
1660 return 0;
1661}
1662
1663static int btusb_setup_intel(struct hci_dev *hdev)
1664{
1665 struct sk_buff *skb;
1666 const struct firmware *fw;
1667 const u8 *fw_ptr;
Loic Poulain28dc4b92015-12-03 16:10:22 +01001668 int disable_patch, err;
Loic Poulain6c483de2015-12-06 16:18:34 +01001669 struct intel_version ver;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001670
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001671 BT_DBG("%s", hdev->name);
1672
1673 /* The controller has a bug with the first HCI command sent to it
1674 * returning number of completed commands as zero. This would stall the
1675 * command processing in the Bluetooth core.
1676 *
1677 * As a workaround, send HCI Reset command first which will reset the
1678 * number of completed commands and allow normal command processing
1679 * from now on.
1680 */
1681 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
1682 if (IS_ERR(skb)) {
1683 BT_ERR("%s sending initial HCI reset command failed (%ld)",
1684 hdev->name, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001685 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001686 }
1687 kfree_skb(skb);
1688
1689 /* Read Intel specific controller version first to allow selection of
1690 * which firmware file to load.
1691 *
1692 * The returned information are hardware variant and revision plus
1693 * firmware variant, revision and build number.
1694 */
Loic Poulain6c483de2015-12-06 16:18:34 +01001695 err = btintel_read_version(hdev, &ver);
1696 if (err)
1697 return err;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001698
1699 BT_INFO("%s: read Intel version: %02x%02x%02x%02x%02x%02x%02x%02x%02x",
Loic Poulain6c483de2015-12-06 16:18:34 +01001700 hdev->name, ver.hw_platform, ver.hw_variant, ver.hw_revision,
1701 ver.fw_variant, ver.fw_revision, ver.fw_build_num,
1702 ver.fw_build_ww, ver.fw_build_yy, ver.fw_patch_num);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001703
1704 /* fw_patch_num indicates the version of patch the device currently
1705 * have. If there is no patch data in the device, it is always 0x00.
Minjune Kim5075eda2015-08-27 13:21:52 +09001706 * So, if it is other than 0x00, no need to patch the device again.
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001707 */
Loic Poulain6c483de2015-12-06 16:18:34 +01001708 if (ver.fw_patch_num) {
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001709 BT_INFO("%s: Intel device is already patched. patch num: %02x",
Loic Poulain6c483de2015-12-06 16:18:34 +01001710 hdev->name, ver.fw_patch_num);
Marcel Holtmann213445b2015-10-21 02:45:19 +02001711 goto complete;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001712 }
1713
1714 /* Opens the firmware patch file based on the firmware version read
1715 * from the controller. If it fails to open the matching firmware
1716 * patch file, it tries to open the default firmware patch file.
1717 * If no patch file is found, allow the device to operate without
1718 * a patch.
1719 */
Loic Poulain6c483de2015-12-06 16:18:34 +01001720 fw = btusb_setup_intel_get_fw(hdev, &ver);
1721 if (!fw)
Marcel Holtmann213445b2015-10-21 02:45:19 +02001722 goto complete;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001723 fw_ptr = fw->data;
1724
Loic Poulain28dc4b92015-12-03 16:10:22 +01001725 /* Enable the manufacturer mode of the controller.
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001726 * Only while this mode is enabled, the driver can download the
1727 * firmware patch data and configuration parameters.
1728 */
Loic Poulain28dc4b92015-12-03 16:10:22 +01001729 err = btintel_enter_mfg(hdev);
1730 if (err) {
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001731 release_firmware(fw);
Loic Poulain28dc4b92015-12-03 16:10:22 +01001732 return err;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001733 }
1734
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001735 disable_patch = 1;
1736
1737 /* The firmware data file consists of list of Intel specific HCI
1738 * commands and its expected events. The first byte indicates the
1739 * type of the message, either HCI command or HCI event.
1740 *
1741 * It reads the command and its expected event from the firmware file,
1742 * and send to the controller. Once __hci_cmd_sync_ev() returns,
1743 * the returned event is compared with the event read from the firmware
1744 * file and it will continue until all the messages are downloaded to
1745 * the controller.
1746 *
1747 * Once the firmware patching is completed successfully,
1748 * the manufacturer mode is disabled with reset and activating the
1749 * downloaded patch.
1750 *
1751 * If the firmware patching fails, the manufacturer mode is
1752 * disabled with reset and deactivating the patch.
1753 *
1754 * If the default patch file is used, no reset is done when disabling
1755 * the manufacturer.
1756 */
1757 while (fw->size > fw_ptr - fw->data) {
1758 int ret;
1759
1760 ret = btusb_setup_intel_patching(hdev, fw, &fw_ptr,
1761 &disable_patch);
1762 if (ret < 0)
1763 goto exit_mfg_deactivate;
1764 }
1765
1766 release_firmware(fw);
1767
1768 if (disable_patch)
1769 goto exit_mfg_disable;
1770
1771 /* Patching completed successfully and disable the manufacturer mode
1772 * with reset and activate the downloaded firmware patches.
1773 */
Loic Poulain28dc4b92015-12-03 16:10:22 +01001774 err = btintel_exit_mfg(hdev, true, true);
1775 if (err)
1776 return err;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001777
1778 BT_INFO("%s: Intel Bluetooth firmware patch completed and activated",
1779 hdev->name);
1780
Marcel Holtmann213445b2015-10-21 02:45:19 +02001781 goto complete;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001782
1783exit_mfg_disable:
1784 /* Disable the manufacturer mode without reset */
Loic Poulain28dc4b92015-12-03 16:10:22 +01001785 err = btintel_exit_mfg(hdev, false, false);
1786 if (err)
1787 return err;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001788
1789 BT_INFO("%s: Intel Bluetooth firmware patch completed", hdev->name);
Marcel Holtmann40cb0982014-07-02 12:06:45 +02001790
Marcel Holtmann213445b2015-10-21 02:45:19 +02001791 goto complete;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001792
1793exit_mfg_deactivate:
1794 release_firmware(fw);
1795
1796 /* Patching failed. Disable the manufacturer mode with reset and
1797 * deactivate the downloaded firmware patches.
1798 */
Loic Poulain28dc4b92015-12-03 16:10:22 +01001799 err = btintel_exit_mfg(hdev, true, false);
1800 if (err)
1801 return err;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001802
1803 BT_INFO("%s: Intel Bluetooth firmware patch completed and deactivated",
1804 hdev->name);
1805
Marcel Holtmann213445b2015-10-21 02:45:19 +02001806complete:
1807 /* Set the event mask for Intel specific vendor events. This enables
1808 * a few extra events that are useful during general operation.
1809 */
1810 btintel_set_event_mask_mfg(hdev, false);
1811
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07001812 btintel_check_bdaddr(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001813 return 0;
1814}
1815
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001816static int inject_cmd_complete(struct hci_dev *hdev, __u16 opcode)
1817{
1818 struct sk_buff *skb;
1819 struct hci_event_hdr *hdr;
1820 struct hci_ev_cmd_complete *evt;
1821
1822 skb = bt_skb_alloc(sizeof(*hdr) + sizeof(*evt) + 1, GFP_ATOMIC);
1823 if (!skb)
1824 return -ENOMEM;
1825
1826 hdr = (struct hci_event_hdr *)skb_put(skb, sizeof(*hdr));
1827 hdr->evt = HCI_EV_CMD_COMPLETE;
1828 hdr->plen = sizeof(*evt) + 1;
1829
1830 evt = (struct hci_ev_cmd_complete *)skb_put(skb, sizeof(*evt));
1831 evt->ncmd = 0x01;
1832 evt->opcode = cpu_to_le16(opcode);
1833
1834 *skb_put(skb, 1) = 0x00;
1835
Marcel Holtmann618e8bc2015-11-05 07:33:56 +01001836 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001837
1838 return hci_recv_frame(hdev, skb);
1839}
1840
1841static int btusb_recv_bulk_intel(struct btusb_data *data, void *buffer,
1842 int count)
1843{
1844 /* When the device is in bootloader mode, then it can send
1845 * events via the bulk endpoint. These events are treated the
1846 * same way as the ones received from the interrupt endpoint.
1847 */
1848 if (test_bit(BTUSB_BOOTLOADER, &data->flags))
1849 return btusb_recv_intr(data, buffer, count);
1850
1851 return btusb_recv_bulk(data, buffer, count);
1852}
1853
Marcel Holtmannccd6da22015-04-09 00:35:19 -07001854static void btusb_intel_bootup(struct btusb_data *data, const void *ptr,
1855 unsigned int len)
1856{
1857 const struct intel_bootup *evt = ptr;
1858
1859 if (len != sizeof(*evt))
1860 return;
1861
1862 if (test_and_clear_bit(BTUSB_BOOTING, &data->flags)) {
1863 smp_mb__after_atomic();
1864 wake_up_bit(&data->flags, BTUSB_BOOTING);
1865 }
1866}
1867
1868static void btusb_intel_secure_send_result(struct btusb_data *data,
1869 const void *ptr, unsigned int len)
1870{
1871 const struct intel_secure_send_result *evt = ptr;
1872
1873 if (len != sizeof(*evt))
1874 return;
1875
1876 if (evt->result)
1877 set_bit(BTUSB_FIRMWARE_FAILED, &data->flags);
1878
1879 if (test_and_clear_bit(BTUSB_DOWNLOADING, &data->flags) &&
1880 test_bit(BTUSB_FIRMWARE_LOADED, &data->flags)) {
1881 smp_mb__after_atomic();
1882 wake_up_bit(&data->flags, BTUSB_DOWNLOADING);
1883 }
1884}
1885
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001886static int btusb_recv_event_intel(struct hci_dev *hdev, struct sk_buff *skb)
1887{
1888 struct btusb_data *data = hci_get_drvdata(hdev);
1889
1890 if (test_bit(BTUSB_BOOTLOADER, &data->flags)) {
1891 struct hci_event_hdr *hdr = (void *)skb->data;
1892
Marcel Holtmannccd6da22015-04-09 00:35:19 -07001893 if (skb->len > HCI_EVENT_HDR_SIZE && hdr->evt == 0xff &&
1894 hdr->plen > 0) {
1895 const void *ptr = skb->data + HCI_EVENT_HDR_SIZE + 1;
1896 unsigned int len = skb->len - HCI_EVENT_HDR_SIZE - 1;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001897
Marcel Holtmannccd6da22015-04-09 00:35:19 -07001898 switch (skb->data[2]) {
1899 case 0x02:
1900 /* When switching to the operational firmware
1901 * the device sends a vendor specific event
1902 * indicating that the bootup completed.
1903 */
1904 btusb_intel_bootup(data, ptr, len);
1905 break;
1906 case 0x06:
1907 /* When the firmware loading completes the
1908 * device sends out a vendor specific event
1909 * indicating the result of the firmware
1910 * loading.
1911 */
1912 btusb_intel_secure_send_result(data, ptr, len);
1913 break;
Johan Hedbergfad70972015-01-30 10:58:55 +02001914 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001915 }
1916 }
1917
1918 return hci_recv_frame(hdev, skb);
1919}
1920
1921static int btusb_send_frame_intel(struct hci_dev *hdev, struct sk_buff *skb)
1922{
1923 struct btusb_data *data = hci_get_drvdata(hdev);
1924 struct urb *urb;
1925
1926 BT_DBG("%s", hdev->name);
1927
Marcel Holtmann618e8bc2015-11-05 07:33:56 +01001928 switch (hci_skb_pkt_type(skb)) {
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001929 case HCI_COMMAND_PKT:
1930 if (test_bit(BTUSB_BOOTLOADER, &data->flags)) {
1931 struct hci_command_hdr *cmd = (void *)skb->data;
1932 __u16 opcode = le16_to_cpu(cmd->opcode);
1933
1934 /* When in bootloader mode and the command 0xfc09
1935 * is received, it needs to be send down the
1936 * bulk endpoint. So allocate a bulk URB instead.
1937 */
1938 if (opcode == 0xfc09)
1939 urb = alloc_bulk_urb(hdev, skb);
1940 else
1941 urb = alloc_ctrl_urb(hdev, skb);
1942
1943 /* When the 0xfc01 command is issued to boot into
1944 * the operational firmware, it will actually not
1945 * send a command complete event. To keep the flow
1946 * control working inject that event here.
1947 */
1948 if (opcode == 0xfc01)
1949 inject_cmd_complete(hdev, opcode);
1950 } else {
1951 urb = alloc_ctrl_urb(hdev, skb);
1952 }
1953 if (IS_ERR(urb))
1954 return PTR_ERR(urb);
1955
1956 hdev->stat.cmd_tx++;
1957 return submit_or_queue_tx_urb(hdev, urb);
1958
1959 case HCI_ACLDATA_PKT:
1960 urb = alloc_bulk_urb(hdev, skb);
1961 if (IS_ERR(urb))
1962 return PTR_ERR(urb);
1963
1964 hdev->stat.acl_tx++;
1965 return submit_or_queue_tx_urb(hdev, urb);
1966
1967 case HCI_SCODATA_PKT:
1968 if (hci_conn_num(hdev, SCO_LINK) < 1)
1969 return -ENODEV;
1970
1971 urb = alloc_isoc_urb(hdev, skb);
1972 if (IS_ERR(urb))
1973 return PTR_ERR(urb);
1974
1975 hdev->stat.sco_tx++;
1976 return submit_tx_urb(hdev, urb);
1977 }
1978
1979 return -EILSEQ;
1980}
1981
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001982static int btusb_setup_intel_new(struct hci_dev *hdev)
1983{
1984 static const u8 reset_param[] = { 0x00, 0x01, 0x00, 0x01,
1985 0x00, 0x08, 0x04, 0x00 };
1986 struct btusb_data *data = hci_get_drvdata(hdev);
1987 struct sk_buff *skb;
Loic Poulain6c483de2015-12-06 16:18:34 +01001988 struct intel_version ver;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001989 struct intel_boot_params *params;
1990 const struct firmware *fw;
1991 const u8 *fw_ptr;
Marcel Holtmanne66890a92015-06-07 09:47:08 +02001992 u32 frag_len;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001993 char fwname[64];
1994 ktime_t calltime, delta, rettime;
1995 unsigned long long duration;
1996 int err;
1997
1998 BT_DBG("%s", hdev->name);
1999
2000 calltime = ktime_get();
2001
2002 /* Read the Intel version information to determine if the device
2003 * is in bootloader mode or if it already has operational firmware
2004 * loaded.
2005 */
Loic Poulain6c483de2015-12-06 16:18:34 +01002006 err = btintel_read_version(hdev, &ver);
2007 if (err)
2008 return err;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002009
2010 /* The hardware platform number has a fixed value of 0x37 and
2011 * for now only accept this single value.
2012 */
Loic Poulain6c483de2015-12-06 16:18:34 +01002013 if (ver.hw_platform != 0x37) {
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002014 BT_ERR("%s: Unsupported Intel hardware platform (%u)",
Loic Poulain6c483de2015-12-06 16:18:34 +01002015 hdev->name, ver.hw_platform);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002016 return -EINVAL;
2017 }
2018
Tedd Ho-Jeong Ana0af53b2016-05-06 11:53:46 -07002019 /* At the moment the iBT 3.0 hardware variants 0x0b (LnP/SfP)
2020 * and 0x0c (WsP) are supported by this firmware loading method.
2021 *
2022 * This check has been put in place to ensure correct forward
2023 * compatibility options when newer hardware variants come along.
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002024 */
Tedd Ho-Jeong Ana0af53b2016-05-06 11:53:46 -07002025 if (ver.hw_variant != 0x0b && ver.hw_variant != 0x0c) {
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002026 BT_ERR("%s: Unsupported Intel hardware variant (%u)",
Loic Poulain6c483de2015-12-06 16:18:34 +01002027 hdev->name, ver.hw_variant);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002028 return -EINVAL;
2029 }
2030
Loic Poulain6c483de2015-12-06 16:18:34 +01002031 btintel_version_info(hdev, &ver);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002032
2033 /* The firmware variant determines if the device is in bootloader
2034 * mode or is running operational firmware. The value 0x06 identifies
2035 * the bootloader and the value 0x23 identifies the operational
2036 * firmware.
2037 *
2038 * When the operational firmware is already present, then only
2039 * the check for valid Bluetooth device address is needed. This
2040 * determines if the device will be added as configured or
2041 * unconfigured controller.
2042 *
2043 * It is not possible to use the Secure Boot Parameters in this
2044 * case since that command is only available in bootloader mode.
2045 */
Loic Poulain6c483de2015-12-06 16:18:34 +01002046 if (ver.fw_variant == 0x23) {
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002047 clear_bit(BTUSB_BOOTLOADER, &data->flags);
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07002048 btintel_check_bdaddr(hdev);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002049 return 0;
2050 }
2051
2052 /* If the device is not in bootloader mode, then the only possible
2053 * choice is to return an error and abort the device initialization.
2054 */
Loic Poulain6c483de2015-12-06 16:18:34 +01002055 if (ver.fw_variant != 0x06) {
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002056 BT_ERR("%s: Unsupported Intel firmware variant (%u)",
Loic Poulain6c483de2015-12-06 16:18:34 +01002057 hdev->name, ver.fw_variant);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002058 return -ENODEV;
2059 }
2060
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002061 /* Read the secure boot parameters to identify the operating
2062 * details of the bootloader.
2063 */
2064 skb = __hci_cmd_sync(hdev, 0xfc0d, 0, NULL, HCI_INIT_TIMEOUT);
2065 if (IS_ERR(skb)) {
2066 BT_ERR("%s: Reading Intel boot parameters failed (%ld)",
2067 hdev->name, PTR_ERR(skb));
2068 return PTR_ERR(skb);
2069 }
2070
2071 if (skb->len != sizeof(*params)) {
2072 BT_ERR("%s: Intel boot parameters size mismatch", hdev->name);
2073 kfree_skb(skb);
2074 return -EILSEQ;
2075 }
2076
2077 params = (struct intel_boot_params *)skb->data;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002078
2079 BT_INFO("%s: Device revision is %u", hdev->name,
2080 le16_to_cpu(params->dev_revid));
2081
2082 BT_INFO("%s: Secure boot is %s", hdev->name,
2083 params->secure_boot ? "enabled" : "disabled");
2084
Marcel Holtmann2220994e2015-10-13 13:54:55 +02002085 BT_INFO("%s: OTP lock is %s", hdev->name,
2086 params->otp_lock ? "enabled" : "disabled");
2087
2088 BT_INFO("%s: API lock is %s", hdev->name,
2089 params->api_lock ? "enabled" : "disabled");
2090
2091 BT_INFO("%s: Debug lock is %s", hdev->name,
2092 params->debug_lock ? "enabled" : "disabled");
2093
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002094 BT_INFO("%s: Minimum firmware build %u week %u %u", hdev->name,
2095 params->min_fw_build_nn, params->min_fw_build_cw,
2096 2000 + params->min_fw_build_yy);
2097
2098 /* It is required that every single firmware fragment is acknowledged
2099 * with a command complete event. If the boot parameters indicate
2100 * that this bootloader does not send them, then abort the setup.
2101 */
2102 if (params->limited_cce != 0x00) {
2103 BT_ERR("%s: Unsupported Intel firmware loading method (%u)",
2104 hdev->name, params->limited_cce);
2105 kfree_skb(skb);
2106 return -EINVAL;
2107 }
2108
2109 /* If the OTP has no valid Bluetooth device address, then there will
2110 * also be no valid address for the operational firmware.
2111 */
2112 if (!bacmp(&params->otp_bdaddr, BDADDR_ANY)) {
2113 BT_INFO("%s: No device address configured", hdev->name);
2114 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
2115 }
2116
2117 /* With this Intel bootloader only the hardware variant and device
2118 * revision information are used to select the right firmware.
2119 *
Tedd Ho-Jeong An230b04a2016-06-28 08:56:39 -07002120 * The firmware filename is ibt-<hw_variant>-<dev_revid>.sfi.
2121 *
2122 * Currently the supported hardware variants are:
2123 * 11 (0x0b) for iBT3.0 (LnP/SfP)
2124 * 12 (0x0c) for iBT3.5 (WsP)
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002125 */
Tedd Ho-Jeong An230b04a2016-06-28 08:56:39 -07002126 snprintf(fwname, sizeof(fwname), "intel/ibt-%u-%u.sfi",
2127 le16_to_cpu(ver.hw_variant),
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002128 le16_to_cpu(params->dev_revid));
2129
2130 err = request_firmware(&fw, fwname, &hdev->dev);
2131 if (err < 0) {
2132 BT_ERR("%s: Failed to load Intel firmware file (%d)",
2133 hdev->name, err);
2134 kfree_skb(skb);
2135 return err;
2136 }
2137
2138 BT_INFO("%s: Found device firmware: %s", hdev->name, fwname);
2139
Tedd Ho-Jeong An52cc9162015-06-12 16:20:05 -07002140 /* Save the DDC file name for later use to apply once the firmware
2141 * downloading is done.
2142 */
Tedd Ho-Jeong An230b04a2016-06-28 08:56:39 -07002143 snprintf(fwname, sizeof(fwname), "intel/ibt-%u-%u.ddc",
2144 le16_to_cpu(ver.hw_variant),
Tedd Ho-Jeong An52cc9162015-06-12 16:20:05 -07002145 le16_to_cpu(params->dev_revid));
2146
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002147 kfree_skb(skb);
2148
2149 if (fw->size < 644) {
2150 BT_ERR("%s: Invalid size of firmware file (%zu)",
2151 hdev->name, fw->size);
2152 err = -EBADF;
2153 goto done;
2154 }
2155
2156 set_bit(BTUSB_DOWNLOADING, &data->flags);
2157
2158 /* Start the firmware download transaction with the Init fragment
2159 * represented by the 128 bytes of CSS header.
2160 */
Marcel Holtmann09df1232015-07-05 14:55:36 +02002161 err = btintel_secure_send(hdev, 0x00, 128, fw->data);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002162 if (err < 0) {
2163 BT_ERR("%s: Failed to send firmware header (%d)",
2164 hdev->name, err);
2165 goto done;
2166 }
2167
2168 /* Send the 256 bytes of public key information from the firmware
2169 * as the PKey fragment.
2170 */
Marcel Holtmann09df1232015-07-05 14:55:36 +02002171 err = btintel_secure_send(hdev, 0x03, 256, fw->data + 128);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002172 if (err < 0) {
2173 BT_ERR("%s: Failed to send firmware public key (%d)",
2174 hdev->name, err);
2175 goto done;
2176 }
2177
2178 /* Send the 256 bytes of signature information from the firmware
2179 * as the Sign fragment.
2180 */
Marcel Holtmann09df1232015-07-05 14:55:36 +02002181 err = btintel_secure_send(hdev, 0x02, 256, fw->data + 388);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002182 if (err < 0) {
2183 BT_ERR("%s: Failed to send firmware signature (%d)",
2184 hdev->name, err);
2185 goto done;
2186 }
2187
2188 fw_ptr = fw->data + 644;
Marcel Holtmanne66890a92015-06-07 09:47:08 +02002189 frag_len = 0;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002190
2191 while (fw_ptr - fw->data < fw->size) {
Marcel Holtmanne66890a92015-06-07 09:47:08 +02002192 struct hci_command_hdr *cmd = (void *)(fw_ptr + frag_len);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002193
Marcel Holtmanne66890a92015-06-07 09:47:08 +02002194 frag_len += sizeof(*cmd) + cmd->plen;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002195
Minjune Kim5075eda2015-08-27 13:21:52 +09002196 /* The parameter length of the secure send command requires
Marcel Holtmanne66890a92015-06-07 09:47:08 +02002197 * a 4 byte alignment. It happens so that the firmware file
2198 * contains proper Intel_NOP commands to align the fragments
2199 * as needed.
2200 *
2201 * Send set of commands with 4 byte alignment from the
2202 * firmware data buffer as a single Data fragement.
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002203 */
Marcel Holtmanne66890a92015-06-07 09:47:08 +02002204 if (!(frag_len % 4)) {
Marcel Holtmann09df1232015-07-05 14:55:36 +02002205 err = btintel_secure_send(hdev, 0x01, frag_len, fw_ptr);
Marcel Holtmanne66890a92015-06-07 09:47:08 +02002206 if (err < 0) {
2207 BT_ERR("%s: Failed to send firmware data (%d)",
2208 hdev->name, err);
2209 goto done;
2210 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002211
Marcel Holtmanne66890a92015-06-07 09:47:08 +02002212 fw_ptr += frag_len;
2213 frag_len = 0;
2214 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002215 }
2216
Marcel Holtmannce6bb922015-01-28 01:58:40 -08002217 set_bit(BTUSB_FIRMWARE_LOADED, &data->flags);
2218
Johan Hedberga087a982015-01-30 10:58:54 +02002219 BT_INFO("%s: Waiting for firmware download to complete", hdev->name);
2220
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002221 /* Before switching the device into operational mode and with that
2222 * booting the loaded firmware, wait for the bootloader notification
2223 * that all fragments have been successfully received.
2224 *
Johan Hedberga087a982015-01-30 10:58:54 +02002225 * When the event processing receives the notification, then the
2226 * BTUSB_DOWNLOADING flag will be cleared.
2227 *
2228 * The firmware loading should not take longer than 5 seconds
2229 * and thus just timeout if that happens and fail the setup
2230 * of this device.
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002231 */
Johan Hedberg129a7692015-02-14 09:33:35 +02002232 err = wait_on_bit_timeout(&data->flags, BTUSB_DOWNLOADING,
2233 TASK_INTERRUPTIBLE,
2234 msecs_to_jiffies(5000));
Bart Van Asschef0a70a02016-08-11 16:02:44 -07002235 if (err == -EINTR) {
Johan Hedberga087a982015-01-30 10:58:54 +02002236 BT_ERR("%s: Firmware loading interrupted", hdev->name);
Johan Hedberga087a982015-01-30 10:58:54 +02002237 goto done;
2238 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002239
Johan Hedberga087a982015-01-30 10:58:54 +02002240 if (err) {
2241 BT_ERR("%s: Firmware loading timeout", hdev->name);
2242 err = -ETIMEDOUT;
2243 goto done;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002244 }
2245
2246 if (test_bit(BTUSB_FIRMWARE_FAILED, &data->flags)) {
2247 BT_ERR("%s: Firmware loading failed", hdev->name);
2248 err = -ENOEXEC;
2249 goto done;
2250 }
2251
2252 rettime = ktime_get();
2253 delta = ktime_sub(rettime, calltime);
2254 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
2255
2256 BT_INFO("%s: Firmware loaded in %llu usecs", hdev->name, duration);
2257
2258done:
2259 release_firmware(fw);
2260
2261 if (err < 0)
2262 return err;
2263
2264 calltime = ktime_get();
2265
2266 set_bit(BTUSB_BOOTING, &data->flags);
2267
2268 skb = __hci_cmd_sync(hdev, 0xfc01, sizeof(reset_param), reset_param,
2269 HCI_INIT_TIMEOUT);
2270 if (IS_ERR(skb))
2271 return PTR_ERR(skb);
2272
2273 kfree_skb(skb);
2274
2275 /* The bootloader will not indicate when the device is ready. This
2276 * is done by the operational firmware sending bootup notification.
Johan Hedbergfad70972015-01-30 10:58:55 +02002277 *
2278 * Booting into operational firmware should not take longer than
2279 * 1 second. However if that happens, then just fail the setup
2280 * since something went wrong.
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002281 */
Johan Hedbergfad70972015-01-30 10:58:55 +02002282 BT_INFO("%s: Waiting for device to boot", hdev->name);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002283
Johan Hedberg129a7692015-02-14 09:33:35 +02002284 err = wait_on_bit_timeout(&data->flags, BTUSB_BOOTING,
2285 TASK_INTERRUPTIBLE,
2286 msecs_to_jiffies(1000));
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002287
Bart Van Asschef0a70a02016-08-11 16:02:44 -07002288 if (err == -EINTR) {
Johan Hedbergfad70972015-01-30 10:58:55 +02002289 BT_ERR("%s: Device boot interrupted", hdev->name);
2290 return -EINTR;
2291 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002292
Johan Hedbergfad70972015-01-30 10:58:55 +02002293 if (err) {
2294 BT_ERR("%s: Device boot timeout", hdev->name);
2295 return -ETIMEDOUT;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002296 }
2297
2298 rettime = ktime_get();
2299 delta = ktime_sub(rettime, calltime);
2300 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
2301
2302 BT_INFO("%s: Device booted in %llu usecs", hdev->name, duration);
2303
2304 clear_bit(BTUSB_BOOTLOADER, &data->flags);
2305
Tedd Ho-Jeong An52cc9162015-06-12 16:20:05 -07002306 /* Once the device is running in operational mode, it needs to apply
2307 * the device configuration (DDC) parameters.
2308 *
2309 * The device can work without DDC parameters, so even if it fails
2310 * to load the file, no need to fail the setup.
2311 */
Loic Poulaine924d3d2015-09-04 17:54:36 +02002312 btintel_load_ddc_config(hdev, fwname);
Tedd Ho-Jeong An52cc9162015-06-12 16:20:05 -07002313
Marcel Holtmann213445b2015-10-21 02:45:19 +02002314 /* Set the event mask for Intel specific vendor events. This enables
2315 * a few extra events that are useful during general operation. It
2316 * does not enable any debugging related events.
2317 *
2318 * The device will function correctly without these events enabled
2319 * and thus no need to fail the setup.
2320 */
2321 btintel_set_event_mask(hdev, false);
2322
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002323 return 0;
2324}
2325
Tedd Ho-Jeong Anbfbd45e2015-02-13 09:20:52 -08002326static int btusb_shutdown_intel(struct hci_dev *hdev)
2327{
2328 struct sk_buff *skb;
2329 long ret;
2330
2331 /* Some platforms have an issue with BT LED when the interface is
2332 * down or BT radio is turned off, which takes 5 seconds to BT LED
2333 * goes off. This command turns off the BT LED immediately.
2334 */
2335 skb = __hci_cmd_sync(hdev, 0xfc3f, 0, NULL, HCI_INIT_TIMEOUT);
2336 if (IS_ERR(skb)) {
2337 ret = PTR_ERR(skb);
2338 BT_ERR("%s: turning off Intel device LED failed (%ld)",
2339 hdev->name, ret);
2340 return ret;
2341 }
2342 kfree_skb(skb);
2343
2344 return 0;
2345}
2346
Amitkumar Karwarae8df492014-07-18 14:47:06 -07002347static int btusb_set_bdaddr_marvell(struct hci_dev *hdev,
2348 const bdaddr_t *bdaddr)
2349{
2350 struct sk_buff *skb;
2351 u8 buf[8];
2352 long ret;
2353
2354 buf[0] = 0xfe;
2355 buf[1] = sizeof(bdaddr_t);
2356 memcpy(buf + 2, bdaddr, sizeof(bdaddr_t));
2357
2358 skb = __hci_cmd_sync(hdev, 0xfc22, sizeof(buf), buf, HCI_INIT_TIMEOUT);
2359 if (IS_ERR(skb)) {
2360 ret = PTR_ERR(skb);
2361 BT_ERR("%s: changing Marvell device address failed (%ld)",
2362 hdev->name, ret);
2363 return ret;
2364 }
2365 kfree_skb(skb);
2366
2367 return 0;
2368}
2369
Toshi Kikuchi58592232014-12-12 10:58:05 -08002370static int btusb_set_bdaddr_ath3012(struct hci_dev *hdev,
2371 const bdaddr_t *bdaddr)
2372{
2373 struct sk_buff *skb;
2374 u8 buf[10];
2375 long ret;
2376
2377 buf[0] = 0x01;
2378 buf[1] = 0x01;
2379 buf[2] = 0x00;
2380 buf[3] = sizeof(bdaddr_t);
2381 memcpy(buf + 4, bdaddr, sizeof(bdaddr_t));
2382
2383 skb = __hci_cmd_sync(hdev, 0xfc0b, sizeof(buf), buf, HCI_INIT_TIMEOUT);
2384 if (IS_ERR(skb)) {
2385 ret = PTR_ERR(skb);
2386 BT_ERR("%s: Change address command failed (%ld)",
2387 hdev->name, ret);
2388 return ret;
2389 }
2390 kfree_skb(skb);
2391
2392 return 0;
2393}
2394
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002395#define QCA_DFU_PACKET_LEN 4096
2396
2397#define QCA_GET_TARGET_VERSION 0x09
2398#define QCA_CHECK_STATUS 0x05
2399#define QCA_DFU_DOWNLOAD 0x01
2400
2401#define QCA_SYSCFG_UPDATED 0x40
2402#define QCA_PATCH_UPDATED 0x80
2403#define QCA_DFU_TIMEOUT 3000
2404
2405struct qca_version {
2406 __le32 rom_version;
2407 __le32 patch_version;
2408 __le32 ram_version;
2409 __le32 ref_clock;
2410 __u8 reserved[4];
2411} __packed;
2412
2413struct qca_rampatch_version {
2414 __le16 rom_version;
2415 __le16 patch_version;
2416} __packed;
2417
2418struct qca_device_info {
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002419 u32 rom_version;
2420 u8 rampatch_hdr; /* length of header in rampatch */
2421 u8 nvm_hdr; /* length of header in NVM */
2422 u8 ver_offset; /* offset of version structure in rampatch */
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002423};
2424
2425static const struct qca_device_info qca_devices_table[] = {
2426 { 0x00000100, 20, 4, 10 }, /* Rome 1.0 */
2427 { 0x00000101, 20, 4, 10 }, /* Rome 1.1 */
Chan-yeol Park7f6e6362015-05-21 11:24:27 +09002428 { 0x00000200, 28, 4, 18 }, /* Rome 2.0 */
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002429 { 0x00000201, 28, 4, 18 }, /* Rome 2.1 */
2430 { 0x00000300, 28, 4, 18 }, /* Rome 3.0 */
2431 { 0x00000302, 28, 4, 18 }, /* Rome 3.2 */
2432};
2433
2434static int btusb_qca_send_vendor_req(struct hci_dev *hdev, u8 request,
2435 void *data, u16 size)
2436{
2437 struct btusb_data *btdata = hci_get_drvdata(hdev);
2438 struct usb_device *udev = btdata->udev;
2439 int pipe, err;
2440 u8 *buf;
2441
2442 buf = kmalloc(size, GFP_KERNEL);
2443 if (!buf)
2444 return -ENOMEM;
2445
2446 /* Found some of USB hosts have IOT issues with ours so that we should
2447 * not wait until HCI layer is ready.
2448 */
2449 pipe = usb_rcvctrlpipe(udev, 0);
2450 err = usb_control_msg(udev, pipe, request, USB_TYPE_VENDOR | USB_DIR_IN,
2451 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
2452 if (err < 0) {
2453 BT_ERR("%s: Failed to access otp area (%d)", hdev->name, err);
2454 goto done;
2455 }
2456
2457 memcpy(data, buf, size);
2458
2459done:
2460 kfree(buf);
2461
2462 return err;
2463}
2464
2465static int btusb_setup_qca_download_fw(struct hci_dev *hdev,
2466 const struct firmware *firmware,
2467 size_t hdr_size)
2468{
2469 struct btusb_data *btdata = hci_get_drvdata(hdev);
2470 struct usb_device *udev = btdata->udev;
2471 size_t count, size, sent = 0;
2472 int pipe, len, err;
2473 u8 *buf;
2474
2475 buf = kmalloc(QCA_DFU_PACKET_LEN, GFP_KERNEL);
2476 if (!buf)
2477 return -ENOMEM;
2478
2479 count = firmware->size;
2480
2481 size = min_t(size_t, count, hdr_size);
2482 memcpy(buf, firmware->data, size);
2483
2484 /* USB patches should go down to controller through USB path
2485 * because binary format fits to go down through USB channel.
2486 * USB control path is for patching headers and USB bulk is for
2487 * patch body.
2488 */
2489 pipe = usb_sndctrlpipe(udev, 0);
2490 err = usb_control_msg(udev, pipe, QCA_DFU_DOWNLOAD, USB_TYPE_VENDOR,
2491 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
2492 if (err < 0) {
2493 BT_ERR("%s: Failed to send headers (%d)", hdev->name, err);
2494 goto done;
2495 }
2496
2497 sent += size;
2498 count -= size;
2499
2500 while (count) {
2501 size = min_t(size_t, count, QCA_DFU_PACKET_LEN);
2502
2503 memcpy(buf, firmware->data + sent, size);
2504
2505 pipe = usb_sndbulkpipe(udev, 0x02);
2506 err = usb_bulk_msg(udev, pipe, buf, size, &len,
2507 QCA_DFU_TIMEOUT);
2508 if (err < 0) {
2509 BT_ERR("%s: Failed to send body at %zd of %zd (%d)",
2510 hdev->name, sent, firmware->size, err);
2511 break;
2512 }
2513
2514 if (size != len) {
2515 BT_ERR("%s: Failed to get bulk buffer", hdev->name);
2516 err = -EILSEQ;
2517 break;
2518 }
2519
2520 sent += size;
2521 count -= size;
2522 }
2523
2524done:
2525 kfree(buf);
2526 return err;
2527}
2528
2529static int btusb_setup_qca_load_rampatch(struct hci_dev *hdev,
2530 struct qca_version *ver,
2531 const struct qca_device_info *info)
2532{
2533 struct qca_rampatch_version *rver;
2534 const struct firmware *fw;
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002535 u32 ver_rom, ver_patch;
2536 u16 rver_rom, rver_patch;
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002537 char fwname[64];
2538 int err;
2539
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002540 ver_rom = le32_to_cpu(ver->rom_version);
2541 ver_patch = le32_to_cpu(ver->patch_version);
2542
2543 snprintf(fwname, sizeof(fwname), "qca/rampatch_usb_%08x.bin", ver_rom);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002544
2545 err = request_firmware(&fw, fwname, &hdev->dev);
2546 if (err) {
2547 BT_ERR("%s: failed to request rampatch file: %s (%d)",
2548 hdev->name, fwname, err);
2549 return err;
2550 }
2551
2552 BT_INFO("%s: using rampatch file: %s", hdev->name, fwname);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002553
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002554 rver = (struct qca_rampatch_version *)(fw->data + info->ver_offset);
2555 rver_rom = le16_to_cpu(rver->rom_version);
2556 rver_patch = le16_to_cpu(rver->patch_version);
2557
2558 BT_INFO("%s: QCA: patch rome 0x%x build 0x%x, firmware rome 0x%x "
2559 "build 0x%x", hdev->name, rver_rom, rver_patch, ver_rom,
2560 ver_patch);
2561
2562 if (rver_rom != ver_rom || rver_patch <= ver_patch) {
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002563 BT_ERR("%s: rampatch file version did not match with firmware",
2564 hdev->name);
2565 err = -EINVAL;
2566 goto done;
2567 }
2568
2569 err = btusb_setup_qca_download_fw(hdev, fw, info->rampatch_hdr);
2570
2571done:
2572 release_firmware(fw);
2573
2574 return err;
2575}
2576
2577static int btusb_setup_qca_load_nvm(struct hci_dev *hdev,
2578 struct qca_version *ver,
2579 const struct qca_device_info *info)
2580{
2581 const struct firmware *fw;
2582 char fwname[64];
2583 int err;
2584
2585 snprintf(fwname, sizeof(fwname), "qca/nvm_usb_%08x.bin",
2586 le32_to_cpu(ver->rom_version));
2587
2588 err = request_firmware(&fw, fwname, &hdev->dev);
2589 if (err) {
2590 BT_ERR("%s: failed to request NVM file: %s (%d)",
2591 hdev->name, fwname, err);
2592 return err;
2593 }
2594
2595 BT_INFO("%s: using NVM file: %s", hdev->name, fwname);
2596
2597 err = btusb_setup_qca_download_fw(hdev, fw, info->nvm_hdr);
2598
2599 release_firmware(fw);
2600
2601 return err;
2602}
2603
2604static int btusb_setup_qca(struct hci_dev *hdev)
2605{
2606 const struct qca_device_info *info = NULL;
2607 struct qca_version ver;
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002608 u32 ver_rom;
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002609 u8 status;
2610 int i, err;
2611
2612 err = btusb_qca_send_vendor_req(hdev, QCA_GET_TARGET_VERSION, &ver,
Marcel Holtmanneb500422015-04-16 23:15:50 +02002613 sizeof(ver));
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002614 if (err < 0)
2615 return err;
2616
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002617 ver_rom = le32_to_cpu(ver.rom_version);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002618 for (i = 0; i < ARRAY_SIZE(qca_devices_table); i++) {
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002619 if (ver_rom == qca_devices_table[i].rom_version)
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002620 info = &qca_devices_table[i];
2621 }
2622 if (!info) {
2623 BT_ERR("%s: don't support firmware rome 0x%x", hdev->name,
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002624 ver_rom);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002625 return -ENODEV;
2626 }
2627
2628 err = btusb_qca_send_vendor_req(hdev, QCA_CHECK_STATUS, &status,
2629 sizeof(status));
2630 if (err < 0)
2631 return err;
2632
2633 if (!(status & QCA_PATCH_UPDATED)) {
2634 err = btusb_setup_qca_load_rampatch(hdev, &ver, info);
2635 if (err < 0)
2636 return err;
2637 }
2638
2639 if (!(status & QCA_SYSCFG_UPDATED)) {
2640 err = btusb_setup_qca_load_nvm(hdev, &ver, info);
2641 if (err < 0)
2642 return err;
2643 }
2644
2645 return 0;
2646}
2647
Marcel Holtmann9d08f502015-10-08 20:23:08 +02002648#ifdef CONFIG_BT_HCIBTUSB_BCM
2649static inline int __set_diag_interface(struct hci_dev *hdev)
2650{
2651 struct btusb_data *data = hci_get_drvdata(hdev);
2652 struct usb_interface *intf = data->diag;
2653 int i;
2654
2655 if (!data->diag)
2656 return -ENODEV;
2657
2658 data->diag_tx_ep = NULL;
2659 data->diag_rx_ep = NULL;
2660
2661 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
2662 struct usb_endpoint_descriptor *ep_desc;
2663
2664 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
2665
2666 if (!data->diag_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
2667 data->diag_tx_ep = ep_desc;
2668 continue;
2669 }
2670
2671 if (!data->diag_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
2672 data->diag_rx_ep = ep_desc;
2673 continue;
2674 }
2675 }
2676
2677 if (!data->diag_tx_ep || !data->diag_rx_ep) {
2678 BT_ERR("%s invalid diagnostic descriptors", hdev->name);
2679 return -ENODEV;
2680 }
2681
2682 return 0;
2683}
2684
2685static struct urb *alloc_diag_urb(struct hci_dev *hdev, bool enable)
2686{
2687 struct btusb_data *data = hci_get_drvdata(hdev);
2688 struct sk_buff *skb;
2689 struct urb *urb;
2690 unsigned int pipe;
2691
2692 if (!data->diag_tx_ep)
2693 return ERR_PTR(-ENODEV);
2694
2695 urb = usb_alloc_urb(0, GFP_KERNEL);
2696 if (!urb)
2697 return ERR_PTR(-ENOMEM);
2698
2699 skb = bt_skb_alloc(2, GFP_KERNEL);
2700 if (!skb) {
2701 usb_free_urb(urb);
2702 return ERR_PTR(-ENOMEM);
2703 }
2704
2705 *skb_put(skb, 1) = 0xf0;
2706 *skb_put(skb, 1) = enable;
2707
2708 pipe = usb_sndbulkpipe(data->udev, data->diag_tx_ep->bEndpointAddress);
2709
2710 usb_fill_bulk_urb(urb, data->udev, pipe,
2711 skb->data, skb->len, btusb_tx_complete, skb);
2712
2713 skb->dev = (void *)hdev;
2714
2715 return urb;
2716}
2717
2718static int btusb_bcm_set_diag(struct hci_dev *hdev, bool enable)
2719{
2720 struct btusb_data *data = hci_get_drvdata(hdev);
2721 struct urb *urb;
2722
2723 if (!data->diag)
2724 return -ENODEV;
2725
2726 if (!test_bit(HCI_RUNNING, &hdev->flags))
2727 return -ENETDOWN;
2728
2729 urb = alloc_diag_urb(hdev, enable);
2730 if (IS_ERR(urb))
2731 return PTR_ERR(urb);
2732
2733 return submit_or_queue_tx_urb(hdev, urb);
2734}
2735#endif
2736
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002737static int btusb_probe(struct usb_interface *intf,
Marcel Holtmann89e75332014-09-16 04:44:50 +02002738 const struct usb_device_id *id)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002739{
2740 struct usb_endpoint_descriptor *ep_desc;
2741 struct btusb_data *data;
2742 struct hci_dev *hdev;
Marcel Holtmann22f8e9d2015-10-20 00:53:33 +02002743 unsigned ifnum_base;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002744 int i, err;
2745
2746 BT_DBG("intf %p id %p", intf, id);
2747
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002748 /* interface numbers are hardcoded in the spec */
Marcel Holtmann22f8e9d2015-10-20 00:53:33 +02002749 if (intf->cur_altsetting->desc.bInterfaceNumber != 0) {
2750 if (!(id->driver_info & BTUSB_IFNUM_2))
2751 return -ENODEV;
2752 if (intf->cur_altsetting->desc.bInterfaceNumber != 2)
2753 return -ENODEV;
2754 }
2755
2756 ifnum_base = intf->cur_altsetting->desc.bInterfaceNumber;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002757
2758 if (!id->driver_info) {
2759 const struct usb_device_id *match;
Marcel Holtmann89e75332014-09-16 04:44:50 +02002760
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002761 match = usb_match_id(intf, blacklist_table);
2762 if (match)
2763 id = match;
2764 }
2765
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002766 if (id->driver_info == BTUSB_IGNORE)
2767 return -ENODEV;
2768
Steven.Li2d25f8b2011-07-01 14:02:36 +08002769 if (id->driver_info & BTUSB_ATH3012) {
2770 struct usb_device *udev = interface_to_usbdev(intf);
2771
2772 /* Old firmware would otherwise let ath3k driver load
2773 * patch and sysconfig files */
2774 if (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x0001)
2775 return -ENODEV;
2776 }
2777
Sachin Kamat98921db2012-07-27 12:38:39 +05302778 data = devm_kzalloc(&intf->dev, sizeof(*data), GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002779 if (!data)
2780 return -ENOMEM;
2781
2782 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
2783 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
2784
2785 if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
2786 data->intr_ep = ep_desc;
2787 continue;
2788 }
2789
2790 if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
2791 data->bulk_tx_ep = ep_desc;
2792 continue;
2793 }
2794
2795 if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
2796 data->bulk_rx_ep = ep_desc;
2797 continue;
2798 }
2799 }
2800
Sachin Kamat98921db2012-07-27 12:38:39 +05302801 if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002802 return -ENODEV;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002803
Marcel Holtmann893ba542015-01-28 20:27:34 -08002804 if (id->driver_info & BTUSB_AMP) {
2805 data->cmdreq_type = USB_TYPE_CLASS | 0x01;
2806 data->cmdreq = 0x2b;
2807 } else {
2808 data->cmdreq_type = USB_TYPE_CLASS;
2809 data->cmdreq = 0x00;
2810 }
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002811
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002812 data->udev = interface_to_usbdev(intf);
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02002813 data->intf = intf;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002814
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002815 INIT_WORK(&data->work, btusb_work);
Oliver Neukum7bee5492009-08-24 23:44:59 +02002816 INIT_WORK(&data->waker, btusb_waker);
Marcel Holtmann803b5832014-09-16 08:00:29 +02002817 init_usb_anchor(&data->deferred);
2818 init_usb_anchor(&data->tx_anchor);
Oliver Neukum7bee5492009-08-24 23:44:59 +02002819 spin_lock_init(&data->txlock);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002820
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002821 init_usb_anchor(&data->intr_anchor);
2822 init_usb_anchor(&data->bulk_anchor);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02002823 init_usb_anchor(&data->isoc_anchor);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02002824 init_usb_anchor(&data->diag_anchor);
Marcel Holtmann803b5832014-09-16 08:00:29 +02002825 spin_lock_init(&data->rxlock);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002826
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002827 if (id->driver_info & BTUSB_INTEL_NEW) {
2828 data->recv_event = btusb_recv_event_intel;
2829 data->recv_bulk = btusb_recv_bulk_intel;
2830 set_bit(BTUSB_BOOTLOADER, &data->flags);
2831 } else {
2832 data->recv_event = hci_recv_frame;
2833 data->recv_bulk = btusb_recv_bulk;
2834 }
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +01002835
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002836 hdev = hci_alloc_dev();
Sachin Kamat98921db2012-07-27 12:38:39 +05302837 if (!hdev)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002838 return -ENOMEM;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002839
Marcel Holtmannc13854c2010-02-08 15:27:07 +01002840 hdev->bus = HCI_USB;
David Herrmann155961e2012-02-09 21:58:32 +01002841 hci_set_drvdata(hdev, data);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002842
Marcel Holtmann893ba542015-01-28 20:27:34 -08002843 if (id->driver_info & BTUSB_AMP)
2844 hdev->dev_type = HCI_AMP;
2845 else
Marcel Holtmannca8bee52016-07-05 14:30:14 +02002846 hdev->dev_type = HCI_PRIMARY;
Marcel Holtmann893ba542015-01-28 20:27:34 -08002847
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002848 data->hdev = hdev;
2849
2850 SET_HCIDEV_DEV(hdev, &intf->dev);
2851
Marcel Holtmann9f8f9622013-04-10 08:11:35 -07002852 hdev->open = btusb_open;
2853 hdev->close = btusb_close;
2854 hdev->flush = btusb_flush;
2855 hdev->send = btusb_send_frame;
2856 hdev->notify = btusb_notify;
2857
Szymon Janc418678b2016-09-01 17:22:37 +02002858 if (id->driver_info & BTUSB_CW6622)
2859 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
2860
Marcel Holtmann6c9d4352015-10-17 14:39:27 +02002861 if (id->driver_info & BTUSB_BCM2045)
2862 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
2863
Marcel Holtmann9f8f9622013-04-10 08:11:35 -07002864 if (id->driver_info & BTUSB_BCM92035)
2865 hdev->setup = btusb_setup_bcm92035;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002866
Marcel Holtmannc2bfb102015-04-05 22:52:14 -07002867#ifdef CONFIG_BT_HCIBTUSB_BCM
Marcel Holtmannabbaf502014-07-02 00:53:48 +02002868 if (id->driver_info & BTUSB_BCM_PATCHRAM) {
Marcel Holtmann49a5f782015-10-20 02:30:48 +02002869 hdev->manufacturer = 15;
Marcel Holtmannc2bfb102015-04-05 22:52:14 -07002870 hdev->setup = btbcm_setup_patchram;
Marcel Holtmann9d08f502015-10-08 20:23:08 +02002871 hdev->set_diag = btusb_bcm_set_diag;
Marcel Holtmann1df1f592015-04-05 22:52:11 -07002872 hdev->set_bdaddr = btbcm_set_bdaddr;
Marcel Holtmann9d08f502015-10-08 20:23:08 +02002873
2874 /* Broadcom LM_DIAG Interface numbers are hardcoded */
Marcel Holtmann22f8e9d2015-10-20 00:53:33 +02002875 data->diag = usb_ifnum_to_if(data->udev, ifnum_base + 2);
Marcel Holtmannabbaf502014-07-02 00:53:48 +02002876 }
Petri Gynther10d4c672014-05-08 15:50:01 -07002877
Marcel Holtmann9d08f502015-10-08 20:23:08 +02002878 if (id->driver_info & BTUSB_BCM_APPLE) {
Marcel Holtmann49a5f782015-10-20 02:30:48 +02002879 hdev->manufacturer = 15;
Marcel Holtmannc2bfb102015-04-05 22:52:14 -07002880 hdev->setup = btbcm_setup_apple;
Marcel Holtmann9d08f502015-10-08 20:23:08 +02002881 hdev->set_diag = btusb_bcm_set_diag;
2882
2883 /* Broadcom LM_DIAG Interface numbers are hardcoded */
Marcel Holtmann22f8e9d2015-10-20 00:53:33 +02002884 data->diag = usb_ifnum_to_if(data->udev, ifnum_base + 2);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02002885 }
Marcel Holtmannc2bfb102015-04-05 22:52:14 -07002886#endif
Marcel Holtmann17b27722015-03-22 15:52:38 +01002887
Marcel Holtmanncb8d6592014-07-02 11:25:25 +02002888 if (id->driver_info & BTUSB_INTEL) {
Marcel Holtmann49a5f782015-10-20 02:30:48 +02002889 hdev->manufacturer = 2;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002890 hdev->setup = btusb_setup_intel;
Tedd Ho-Jeong Anbfbd45e2015-02-13 09:20:52 -08002891 hdev->shutdown = btusb_shutdown_intel;
Marcel Holtmann3e247672015-10-17 16:00:28 +02002892 hdev->set_diag = btintel_set_diag_mfg;
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07002893 hdev->set_bdaddr = btintel_set_bdaddr;
Jakub Pawlowskic33fb9b2015-01-30 18:55:58 -08002894 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
Jakub Pawlowskic1154842015-03-17 09:04:16 -07002895 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
Marcel Holtmann3e247672015-10-17 16:00:28 +02002896 set_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks);
Marcel Holtmanncb8d6592014-07-02 11:25:25 +02002897 }
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002898
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002899 if (id->driver_info & BTUSB_INTEL_NEW) {
Marcel Holtmann49a5f782015-10-20 02:30:48 +02002900 hdev->manufacturer = 2;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002901 hdev->send = btusb_send_frame_intel;
2902 hdev->setup = btusb_setup_intel_new;
Marcel Holtmanneeb6abe2015-07-05 14:37:39 +02002903 hdev->hw_error = btintel_hw_error;
Marcel Holtmann6d2e50d2015-10-09 14:42:08 +02002904 hdev->set_diag = btintel_set_diag;
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07002905 hdev->set_bdaddr = btintel_set_bdaddr;
Marcel Holtmannb970c5b2015-02-01 23:57:18 -08002906 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
Marcel Holtmannd8270fb2015-10-17 16:00:27 +02002907 set_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002908 }
2909
Amitkumar Karwarae8df492014-07-18 14:47:06 -07002910 if (id->driver_info & BTUSB_MARVELL)
2911 hdev->set_bdaddr = btusb_set_bdaddr_marvell;
2912
Marcel Holtmann661cf882015-01-02 23:35:20 -08002913 if (id->driver_info & BTUSB_SWAVE) {
2914 set_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks);
Marcel Holtmannd57dbe72014-12-26 04:42:33 +01002915 set_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS, &hdev->quirks);
Marcel Holtmann661cf882015-01-02 23:35:20 -08002916 }
Marcel Holtmannd57dbe72014-12-26 04:42:33 +01002917
Marcel Holtmanne4c534b2015-10-21 01:31:45 +02002918 if (id->driver_info & BTUSB_INTEL_BOOT) {
2919 hdev->manufacturer = 2;
Marcel Holtmann40df7832014-07-06 13:29:58 +02002920 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
Marcel Holtmanne4c534b2015-10-21 01:31:45 +02002921 }
Marcel Holtmann40df7832014-07-06 13:29:58 +02002922
Jakub Pawlowski79f0c872015-01-29 10:38:34 -08002923 if (id->driver_info & BTUSB_ATH3012) {
Toshi Kikuchi58592232014-12-12 10:58:05 -08002924 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
Jakub Pawlowski3d50d512015-03-17 09:04:15 -07002925 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
Jakub Pawlowski79f0c872015-01-29 10:38:34 -08002926 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
2927 }
Toshi Kikuchi58592232014-12-12 10:58:05 -08002928
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002929 if (id->driver_info & BTUSB_QCA_ROME) {
2930 data->setup_on_usb = btusb_setup_qca;
2931 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
Hans de Goede5795b072018-01-08 10:44:16 +01002932
2933 /* QCA Rome devices lose their updated firmware over suspend,
2934 * but the USB hub doesn't notice any status change.
2935 * explicitly request a device reset on resume.
2936 */
2937 interface_to_usbdev(intf)->quirks |= USB_QUIRK_RESET_RESUME;
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002938 }
2939
Carlo Caionedb33c772015-05-14 10:49:09 +02002940#ifdef CONFIG_BT_HCIBTUSB_RTL
Daniel Drake04b8c812015-05-21 08:23:50 -06002941 if (id->driver_info & BTUSB_REALTEK) {
Carlo Caionedb33c772015-05-14 10:49:09 +02002942 hdev->setup = btrtl_setup_realtek;
Daniel Drake04b8c812015-05-21 08:23:50 -06002943
2944 /* Realtek devices lose their updated firmware over suspend,
2945 * but the USB hub doesn't notice any status change.
2946 * Explicitly request a device reset on resume.
2947 */
Hans de Goede5795b072018-01-08 10:44:16 +01002948 interface_to_usbdev(intf)->quirks |= USB_QUIRK_RESET_RESUME;
Daniel Drake04b8c812015-05-21 08:23:50 -06002949 }
Carlo Caionedb33c772015-05-14 10:49:09 +02002950#endif
Daniel Drakea2698a92015-04-16 14:09:55 -06002951
Marcel Holtmann893ba542015-01-28 20:27:34 -08002952 if (id->driver_info & BTUSB_AMP) {
2953 /* AMP controllers do not support SCO packets */
2954 data->isoc = NULL;
2955 } else {
Marcel Holtmann22f8e9d2015-10-20 00:53:33 +02002956 /* Interface orders are hardcoded in the specification */
2957 data->isoc = usb_ifnum_to_if(data->udev, ifnum_base + 1);
Marcel Holtmann893ba542015-01-28 20:27:34 -08002958 }
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02002959
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002960 if (!reset)
Szymon Janca6c511c2012-05-23 12:35:46 +02002961 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002962
2963 if (force_scofix || id->driver_info & BTUSB_WRONG_SCO_MTU) {
2964 if (!disable_scofix)
2965 set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
2966 }
2967
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02002968 if (id->driver_info & BTUSB_BROKEN_ISOC)
2969 data->isoc = NULL;
2970
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002971 if (id->driver_info & BTUSB_DIGIANSWER) {
2972 data->cmdreq_type = USB_TYPE_VENDOR;
Szymon Janca6c511c2012-05-23 12:35:46 +02002973 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002974 }
2975
2976 if (id->driver_info & BTUSB_CSR) {
2977 struct usb_device *udev = data->udev;
Marcel Holtmann81cac642014-01-03 03:02:36 -08002978 u16 bcdDevice = le16_to_cpu(udev->descriptor.bcdDevice);
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002979
2980 /* Old firmware would otherwise execute USB reset */
Marcel Holtmann81cac642014-01-03 03:02:36 -08002981 if (bcdDevice < 0x117)
Szymon Janca6c511c2012-05-23 12:35:46 +02002982 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmann81cac642014-01-03 03:02:36 -08002983
2984 /* Fake CSR devices with broken commands */
Johan Hedberg6cafcd92015-08-30 21:47:21 +03002985 if (bcdDevice <= 0x100 || bcdDevice == 0x134)
Marcel Holtmann81cac642014-01-03 03:02:36 -08002986 hdev->setup = btusb_setup_csr;
Jakub Pawlowski49c989a2015-03-17 09:04:17 -07002987
2988 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002989 }
2990
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002991 if (id->driver_info & BTUSB_SNIFFER) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02002992 struct usb_device *udev = data->udev;
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002993
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002994 /* New sniffer firmware has crippled HCI interface */
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002995 if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
2996 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
2997 }
2998
Marcel Holtmann3a5ef202014-07-06 14:53:54 +02002999 if (id->driver_info & BTUSB_INTEL_BOOT) {
3000 /* A bug in the bootloader causes that interrupt interface is
3001 * only enabled after receiving SetInterface(0, AltSetting=0).
3002 */
3003 err = usb_set_interface(data->udev, 0, 0);
3004 if (err < 0) {
3005 BT_ERR("failed to set interface 0, alt 0 %d", err);
3006 hci_free_dev(hdev);
3007 return err;
3008 }
3009 }
3010
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02003011 if (data->isoc) {
3012 err = usb_driver_claim_interface(&btusb_driver,
Marcel Holtmann89e75332014-09-16 04:44:50 +02003013 data->isoc, data);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02003014 if (err < 0) {
3015 hci_free_dev(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02003016 return err;
3017 }
3018 }
3019
Marcel Holtmann9d08f502015-10-08 20:23:08 +02003020#ifdef CONFIG_BT_HCIBTUSB_BCM
3021 if (data->diag) {
3022 if (!usb_driver_claim_interface(&btusb_driver,
3023 data->diag, data))
3024 __set_diag_interface(hdev);
3025 else
3026 data->diag = NULL;
3027 }
3028#endif
3029
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003030 err = hci_register_dev(hdev);
3031 if (err < 0) {
3032 hci_free_dev(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003033 return err;
3034 }
3035
3036 usb_set_intfdata(intf, data);
3037
3038 return 0;
3039}
3040
3041static void btusb_disconnect(struct usb_interface *intf)
3042{
3043 struct btusb_data *data = usb_get_intfdata(intf);
3044 struct hci_dev *hdev;
3045
3046 BT_DBG("intf %p", intf);
3047
3048 if (!data)
3049 return;
3050
3051 hdev = data->hdev;
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02003052 usb_set_intfdata(data->intf, NULL);
3053
3054 if (data->isoc)
3055 usb_set_intfdata(data->isoc, NULL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003056
Marcel Holtmann9d08f502015-10-08 20:23:08 +02003057 if (data->diag)
3058 usb_set_intfdata(data->diag, NULL);
3059
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003060 hci_unregister_dev(hdev);
3061
Marcel Holtmann9d08f502015-10-08 20:23:08 +02003062 if (intf == data->intf) {
3063 if (data->isoc)
3064 usb_driver_release_interface(&btusb_driver, data->isoc);
3065 if (data->diag)
3066 usb_driver_release_interface(&btusb_driver, data->diag);
3067 } else if (intf == data->isoc) {
3068 if (data->diag)
3069 usb_driver_release_interface(&btusb_driver, data->diag);
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02003070 usb_driver_release_interface(&btusb_driver, data->intf);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02003071 } else if (intf == data->diag) {
3072 usb_driver_release_interface(&btusb_driver, data->intf);
3073 if (data->isoc)
3074 usb_driver_release_interface(&btusb_driver, data->isoc);
3075 }
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02003076
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003077 hci_free_dev(hdev);
3078}
3079
Oliver Neukum7bee5492009-08-24 23:44:59 +02003080#ifdef CONFIG_PM
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003081static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
3082{
3083 struct btusb_data *data = usb_get_intfdata(intf);
3084
3085 BT_DBG("intf %p", intf);
3086
3087 if (data->suspend_count++)
3088 return 0;
3089
Oliver Neukum7bee5492009-08-24 23:44:59 +02003090 spin_lock_irq(&data->txlock);
Alan Stern5b1b0b82011-08-19 23:49:48 +02003091 if (!(PMSG_IS_AUTO(message) && data->tx_in_flight)) {
Oliver Neukum7bee5492009-08-24 23:44:59 +02003092 set_bit(BTUSB_SUSPENDING, &data->flags);
3093 spin_unlock_irq(&data->txlock);
3094 } else {
3095 spin_unlock_irq(&data->txlock);
3096 data->suspend_count--;
3097 return -EBUSY;
3098 }
3099
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003100 cancel_work_sync(&data->work);
3101
Oliver Neukum7bee5492009-08-24 23:44:59 +02003102 btusb_stop_traffic(data);
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003103 usb_kill_anchored_urbs(&data->tx_anchor);
3104
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003105 return 0;
3106}
3107
Oliver Neukum7bee5492009-08-24 23:44:59 +02003108static void play_deferred(struct btusb_data *data)
3109{
3110 struct urb *urb;
3111 int err;
3112
3113 while ((urb = usb_get_from_anchor(&data->deferred))) {
3114 err = usb_submit_urb(urb, GFP_ATOMIC);
3115 if (err < 0)
3116 break;
3117
3118 data->tx_in_flight++;
3119 }
3120 usb_scuttle_anchored_urbs(&data->deferred);
3121}
3122
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003123static int btusb_resume(struct usb_interface *intf)
3124{
3125 struct btusb_data *data = usb_get_intfdata(intf);
3126 struct hci_dev *hdev = data->hdev;
Oliver Neukum7bee5492009-08-24 23:44:59 +02003127 int err = 0;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003128
3129 BT_DBG("intf %p", intf);
3130
3131 if (--data->suspend_count)
3132 return 0;
3133
3134 if (!test_bit(HCI_RUNNING, &hdev->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +02003135 goto done;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003136
3137 if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
3138 err = btusb_submit_intr_urb(hdev, GFP_NOIO);
3139 if (err < 0) {
3140 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02003141 goto failed;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003142 }
3143 }
3144
3145 if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) {
Marcel Holtmann43c2e572009-02-04 17:41:38 +01003146 err = btusb_submit_bulk_urb(hdev, GFP_NOIO);
3147 if (err < 0) {
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003148 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02003149 goto failed;
3150 }
3151
3152 btusb_submit_bulk_urb(hdev, GFP_NOIO);
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003153 }
3154
3155 if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
3156 if (btusb_submit_isoc_urb(hdev, GFP_NOIO) < 0)
3157 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
3158 else
3159 btusb_submit_isoc_urb(hdev, GFP_NOIO);
3160 }
3161
Oliver Neukum7bee5492009-08-24 23:44:59 +02003162 spin_lock_irq(&data->txlock);
3163 play_deferred(data);
3164 clear_bit(BTUSB_SUSPENDING, &data->flags);
3165 spin_unlock_irq(&data->txlock);
3166 schedule_work(&data->work);
3167
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003168 return 0;
Oliver Neukum7bee5492009-08-24 23:44:59 +02003169
3170failed:
3171 usb_scuttle_anchored_urbs(&data->deferred);
3172done:
3173 spin_lock_irq(&data->txlock);
3174 clear_bit(BTUSB_SUSPENDING, &data->flags);
3175 spin_unlock_irq(&data->txlock);
3176
3177 return err;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003178}
Oliver Neukum7bee5492009-08-24 23:44:59 +02003179#endif
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003180
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003181static struct usb_driver btusb_driver = {
3182 .name = "btusb",
3183 .probe = btusb_probe,
3184 .disconnect = btusb_disconnect,
Oliver Neukum7bee5492009-08-24 23:44:59 +02003185#ifdef CONFIG_PM
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003186 .suspend = btusb_suspend,
3187 .resume = btusb_resume,
Oliver Neukum7bee5492009-08-24 23:44:59 +02003188#endif
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003189 .id_table = btusb_table,
Oliver Neukum7bee5492009-08-24 23:44:59 +02003190 .supports_autosuspend = 1,
Sarah Sharpe1f12eb2012-04-23 10:08:51 -07003191 .disable_hub_initiated_lpm = 1,
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003192};
3193
Greg Kroah-Hartman93f15082011-11-18 09:47:34 -08003194module_usb_driver(btusb_driver);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003195
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02003196module_param(disable_scofix, bool, 0644);
3197MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
3198
3199module_param(force_scofix, bool, 0644);
3200MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
3201
3202module_param(reset, bool, 0644);
3203MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
3204
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003205MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
3206MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
3207MODULE_VERSION(VERSION);
3208MODULE_LICENSE("GPL");