blob: bff67c5a5fe7f8e55caebd29b3a3964fc9cdd23e [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
Larry Fingerd6f52182018-02-11 12:24:32 -0600358 /* Additional Realtek 8822BE Bluetooth devices */
359 { USB_DEVICE(0x0b05, 0x185c), .driver_info = BTUSB_REALTEK },
360
Peter Poklop4481c072015-08-15 20:47:09 +0200361 /* Silicon Wave based devices */
362 { USB_DEVICE(0x0c10, 0x0000), .driver_info = BTUSB_SWAVE },
363
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200364 { } /* Terminating entry */
365};
366
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200367#define BTUSB_MAX_ISOC_FRAMES 10
368
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200369#define BTUSB_INTR_RUNNING 0
370#define BTUSB_BULK_RUNNING 1
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200371#define BTUSB_ISOC_RUNNING 2
Oliver Neukum7bee5492009-08-24 23:44:59 +0200372#define BTUSB_SUSPENDING 3
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -0300373#define BTUSB_DID_ISO_RESUME 4
Marcel Holtmanncda0dd72015-01-26 21:33:48 -0800374#define BTUSB_BOOTLOADER 5
375#define BTUSB_DOWNLOADING 6
Marcel Holtmannce6bb922015-01-28 01:58:40 -0800376#define BTUSB_FIRMWARE_LOADED 7
Marcel Holtmanncda0dd72015-01-26 21:33:48 -0800377#define BTUSB_FIRMWARE_FAILED 8
Marcel Holtmannce6bb922015-01-28 01:58:40 -0800378#define BTUSB_BOOTING 9
Hans de Goede5795b072018-01-08 10:44:16 +0100379#define BTUSB_DIAG_RUNNING 10
380#define BTUSB_OOB_WAKE_ENABLED 11
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200381
382struct btusb_data {
383 struct hci_dev *hdev;
384 struct usb_device *udev;
Marcel Holtmann5fbcd262008-09-23 00:16:36 +0200385 struct usb_interface *intf;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200386 struct usb_interface *isoc;
Marcel Holtmann9d08f502015-10-08 20:23:08 +0200387 struct usb_interface *diag;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200388
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200389 unsigned long flags;
390
391 struct work_struct work;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200392 struct work_struct waker;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200393
Marcel Holtmann803b5832014-09-16 08:00:29 +0200394 struct usb_anchor deferred;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200395 struct usb_anchor tx_anchor;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200396 int tx_in_flight;
397 spinlock_t txlock;
398
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200399 struct usb_anchor intr_anchor;
400 struct usb_anchor bulk_anchor;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200401 struct usb_anchor isoc_anchor;
Marcel Holtmann9d08f502015-10-08 20:23:08 +0200402 struct usb_anchor diag_anchor;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200403 spinlock_t rxlock;
404
405 struct sk_buff *evt_skb;
406 struct sk_buff *acl_skb;
407 struct sk_buff *sco_skb;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200408
409 struct usb_endpoint_descriptor *intr_ep;
410 struct usb_endpoint_descriptor *bulk_tx_ep;
411 struct usb_endpoint_descriptor *bulk_rx_ep;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200412 struct usb_endpoint_descriptor *isoc_tx_ep;
413 struct usb_endpoint_descriptor *isoc_rx_ep;
Marcel Holtmann9d08f502015-10-08 20:23:08 +0200414 struct usb_endpoint_descriptor *diag_tx_ep;
415 struct usb_endpoint_descriptor *diag_rx_ep;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200416
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100417 __u8 cmdreq_type;
Marcel Holtmann893ba542015-01-28 20:27:34 -0800418 __u8 cmdreq;
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100419
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100420 unsigned int sco_num;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200421 int isoc_altsetting;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +0100422 int suspend_count;
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +0100423
Marcel Holtmann97307f52015-01-12 13:51:10 -0800424 int (*recv_event)(struct hci_dev *hdev, struct sk_buff *skb);
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +0100425 int (*recv_bulk)(struct btusb_data *data, void *buffer, int count);
Kim, Ben Young Taeace31982015-02-15 23:06:14 +0000426
427 int (*setup_on_usb)(struct hci_dev *hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200428};
429
Marcel Holtmann803b5832014-09-16 08:00:29 +0200430static inline void btusb_free_frags(struct btusb_data *data)
431{
432 unsigned long flags;
433
434 spin_lock_irqsave(&data->rxlock, flags);
435
436 kfree_skb(data->evt_skb);
437 data->evt_skb = NULL;
438
439 kfree_skb(data->acl_skb);
440 data->acl_skb = NULL;
441
442 kfree_skb(data->sco_skb);
443 data->sco_skb = NULL;
444
445 spin_unlock_irqrestore(&data->rxlock, flags);
446}
447
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200448static int btusb_recv_intr(struct btusb_data *data, void *buffer, int count)
449{
Marcel Holtmann803b5832014-09-16 08:00:29 +0200450 struct sk_buff *skb;
451 int err = 0;
452
453 spin_lock(&data->rxlock);
454 skb = data->evt_skb;
455
456 while (count) {
457 int len;
458
459 if (!skb) {
460 skb = bt_skb_alloc(HCI_MAX_EVENT_SIZE, GFP_ATOMIC);
461 if (!skb) {
462 err = -ENOMEM;
463 break;
464 }
465
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100466 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
467 hci_skb_expect(skb) = HCI_EVENT_HDR_SIZE;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200468 }
469
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100470 len = min_t(uint, hci_skb_expect(skb), count);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200471 memcpy(skb_put(skb, len), buffer, len);
472
473 count -= len;
474 buffer += len;
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100475 hci_skb_expect(skb) -= len;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200476
477 if (skb->len == HCI_EVENT_HDR_SIZE) {
478 /* Complete event header */
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100479 hci_skb_expect(skb) = hci_event_hdr(skb)->plen;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200480
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100481 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
Marcel Holtmann803b5832014-09-16 08:00:29 +0200482 kfree_skb(skb);
483 skb = NULL;
484
485 err = -EILSEQ;
486 break;
487 }
488 }
489
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100490 if (!hci_skb_expect(skb)) {
Marcel Holtmann803b5832014-09-16 08:00:29 +0200491 /* Complete frame */
Marcel Holtmann97307f52015-01-12 13:51:10 -0800492 data->recv_event(data->hdev, skb);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200493 skb = NULL;
494 }
495 }
496
497 data->evt_skb = skb;
498 spin_unlock(&data->rxlock);
499
500 return err;
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200501}
502
503static int btusb_recv_bulk(struct btusb_data *data, void *buffer, int count)
504{
Marcel Holtmann803b5832014-09-16 08:00:29 +0200505 struct sk_buff *skb;
506 int err = 0;
507
508 spin_lock(&data->rxlock);
509 skb = data->acl_skb;
510
511 while (count) {
512 int len;
513
514 if (!skb) {
515 skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
516 if (!skb) {
517 err = -ENOMEM;
518 break;
519 }
520
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100521 hci_skb_pkt_type(skb) = HCI_ACLDATA_PKT;
522 hci_skb_expect(skb) = HCI_ACL_HDR_SIZE;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200523 }
524
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100525 len = min_t(uint, hci_skb_expect(skb), count);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200526 memcpy(skb_put(skb, len), buffer, len);
527
528 count -= len;
529 buffer += len;
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100530 hci_skb_expect(skb) -= len;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200531
532 if (skb->len == HCI_ACL_HDR_SIZE) {
533 __le16 dlen = hci_acl_hdr(skb)->dlen;
534
535 /* Complete ACL header */
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100536 hci_skb_expect(skb) = __le16_to_cpu(dlen);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200537
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100538 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
Marcel Holtmann803b5832014-09-16 08:00:29 +0200539 kfree_skb(skb);
540 skb = NULL;
541
542 err = -EILSEQ;
543 break;
544 }
545 }
546
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100547 if (!hci_skb_expect(skb)) {
Marcel Holtmann803b5832014-09-16 08:00:29 +0200548 /* Complete frame */
549 hci_recv_frame(data->hdev, skb);
550 skb = NULL;
551 }
552 }
553
554 data->acl_skb = skb;
555 spin_unlock(&data->rxlock);
556
557 return err;
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200558}
559
560static int btusb_recv_isoc(struct btusb_data *data, void *buffer, int count)
561{
Marcel Holtmann803b5832014-09-16 08:00:29 +0200562 struct sk_buff *skb;
563 int err = 0;
564
565 spin_lock(&data->rxlock);
566 skb = data->sco_skb;
567
568 while (count) {
569 int len;
570
571 if (!skb) {
572 skb = bt_skb_alloc(HCI_MAX_SCO_SIZE, GFP_ATOMIC);
573 if (!skb) {
574 err = -ENOMEM;
575 break;
576 }
577
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100578 hci_skb_pkt_type(skb) = HCI_SCODATA_PKT;
579 hci_skb_expect(skb) = HCI_SCO_HDR_SIZE;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200580 }
581
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100582 len = min_t(uint, hci_skb_expect(skb), count);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200583 memcpy(skb_put(skb, len), buffer, len);
584
585 count -= len;
586 buffer += len;
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100587 hci_skb_expect(skb) -= len;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200588
589 if (skb->len == HCI_SCO_HDR_SIZE) {
590 /* Complete SCO header */
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100591 hci_skb_expect(skb) = hci_sco_hdr(skb)->dlen;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200592
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100593 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
Marcel Holtmann803b5832014-09-16 08:00:29 +0200594 kfree_skb(skb);
595 skb = NULL;
596
597 err = -EILSEQ;
598 break;
599 }
600 }
601
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100602 if (!hci_skb_expect(skb)) {
Marcel Holtmann803b5832014-09-16 08:00:29 +0200603 /* Complete frame */
604 hci_recv_frame(data->hdev, skb);
605 skb = NULL;
606 }
607 }
608
609 data->sco_skb = skb;
610 spin_unlock(&data->rxlock);
611
612 return err;
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200613}
614
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200615static void btusb_intr_complete(struct urb *urb)
616{
617 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +0100618 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200619 int err;
620
Marcel Holtmann89e75332014-09-16 04:44:50 +0200621 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
622 urb->actual_length);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200623
624 if (!test_bit(HCI_RUNNING, &hdev->flags))
625 return;
626
627 if (urb->status == 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200628 hdev->stat.byte_rx += urb->actual_length;
629
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200630 if (btusb_recv_intr(data, urb->transfer_buffer,
631 urb->actual_length) < 0) {
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200632 BT_ERR("%s corrupted event packet", hdev->name);
633 hdev->stat.err_rx++;
634 }
Champion Chen85560c42014-09-06 14:06:08 -0500635 } else if (urb->status == -ENOENT) {
636 /* Avoid suspend failed when usb_kill_urb */
637 return;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200638 }
639
640 if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
641 return;
642
Oliver Neukum7bee5492009-08-24 23:44:59 +0200643 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200644 usb_anchor_urb(urb, &data->intr_anchor);
645
646 err = usb_submit_urb(urb, GFP_ATOMIC);
647 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200648 /* -EPERM: urb is being killed;
649 * -ENODEV: device got disconnected */
650 if (err != -EPERM && err != -ENODEV)
Stefan Seyfried61faddf2010-11-30 21:49:08 +0100651 BT_ERR("%s urb %p failed to resubmit (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200652 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200653 usb_unanchor_urb(urb);
654 }
655}
656
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100657static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200658{
David Herrmann155961e2012-02-09 21:58:32 +0100659 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200660 struct urb *urb;
661 unsigned char *buf;
662 unsigned int pipe;
663 int err, size;
664
665 BT_DBG("%s", hdev->name);
666
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200667 if (!data->intr_ep)
668 return -ENODEV;
669
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100670 urb = usb_alloc_urb(0, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200671 if (!urb)
672 return -ENOMEM;
673
674 size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
675
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100676 buf = kmalloc(size, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200677 if (!buf) {
678 usb_free_urb(urb);
679 return -ENOMEM;
680 }
681
682 pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
683
684 usb_fill_int_urb(urb, data->udev, pipe, buf, size,
Marcel Holtmann89e75332014-09-16 04:44:50 +0200685 btusb_intr_complete, hdev, data->intr_ep->bInterval);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200686
687 urb->transfer_flags |= URB_FREE_BUFFER;
688
689 usb_anchor_urb(urb, &data->intr_anchor);
690
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100691 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200692 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +0200693 if (err != -EPERM && err != -ENODEV)
694 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200695 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200696 usb_unanchor_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200697 }
698
699 usb_free_urb(urb);
700
701 return err;
702}
703
704static void btusb_bulk_complete(struct urb *urb)
705{
706 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +0100707 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200708 int err;
709
Marcel Holtmann89e75332014-09-16 04:44:50 +0200710 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
711 urb->actual_length);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200712
713 if (!test_bit(HCI_RUNNING, &hdev->flags))
714 return;
715
716 if (urb->status == 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200717 hdev->stat.byte_rx += urb->actual_length;
718
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +0100719 if (data->recv_bulk(data, urb->transfer_buffer,
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200720 urb->actual_length) < 0) {
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200721 BT_ERR("%s corrupted ACL packet", hdev->name);
722 hdev->stat.err_rx++;
723 }
Champion Chen85560c42014-09-06 14:06:08 -0500724 } else if (urb->status == -ENOENT) {
725 /* Avoid suspend failed when usb_kill_urb */
726 return;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200727 }
728
729 if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
730 return;
731
732 usb_anchor_urb(urb, &data->bulk_anchor);
Oliver Neukum652fd782009-12-16 19:23:43 +0100733 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200734
735 err = usb_submit_urb(urb, GFP_ATOMIC);
736 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200737 /* -EPERM: urb is being killed;
738 * -ENODEV: device got disconnected */
739 if (err != -EPERM && err != -ENODEV)
Stefan Seyfried61faddf2010-11-30 21:49:08 +0100740 BT_ERR("%s urb %p failed to resubmit (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200741 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200742 usb_unanchor_urb(urb);
743 }
744}
745
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100746static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200747{
David Herrmann155961e2012-02-09 21:58:32 +0100748 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200749 struct urb *urb;
750 unsigned char *buf;
751 unsigned int pipe;
Vikram Kandukuri290ba202009-07-02 14:31:59 +0530752 int err, size = HCI_MAX_FRAME_SIZE;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200753
754 BT_DBG("%s", hdev->name);
755
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200756 if (!data->bulk_rx_ep)
757 return -ENODEV;
758
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100759 urb = usb_alloc_urb(0, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200760 if (!urb)
761 return -ENOMEM;
762
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100763 buf = kmalloc(size, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200764 if (!buf) {
765 usb_free_urb(urb);
766 return -ENOMEM;
767 }
768
769 pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
770
Marcel Holtmann89e75332014-09-16 04:44:50 +0200771 usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
772 btusb_bulk_complete, hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200773
774 urb->transfer_flags |= URB_FREE_BUFFER;
775
Oliver Neukum7bee5492009-08-24 23:44:59 +0200776 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200777 usb_anchor_urb(urb, &data->bulk_anchor);
778
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100779 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200780 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +0200781 if (err != -EPERM && err != -ENODEV)
782 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200783 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200784 usb_unanchor_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200785 }
786
787 usb_free_urb(urb);
788
789 return err;
790}
791
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200792static void btusb_isoc_complete(struct urb *urb)
793{
794 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +0100795 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200796 int i, err;
797
Marcel Holtmann89e75332014-09-16 04:44:50 +0200798 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
799 urb->actual_length);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200800
801 if (!test_bit(HCI_RUNNING, &hdev->flags))
802 return;
803
804 if (urb->status == 0) {
805 for (i = 0; i < urb->number_of_packets; i++) {
806 unsigned int offset = urb->iso_frame_desc[i].offset;
807 unsigned int length = urb->iso_frame_desc[i].actual_length;
808
809 if (urb->iso_frame_desc[i].status)
810 continue;
811
812 hdev->stat.byte_rx += length;
813
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200814 if (btusb_recv_isoc(data, urb->transfer_buffer + offset,
815 length) < 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200816 BT_ERR("%s corrupted SCO packet", hdev->name);
817 hdev->stat.err_rx++;
818 }
819 }
Champion Chen85560c42014-09-06 14:06:08 -0500820 } else if (urb->status == -ENOENT) {
821 /* Avoid suspend failed when usb_kill_urb */
822 return;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200823 }
824
825 if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags))
826 return;
827
828 usb_anchor_urb(urb, &data->isoc_anchor);
829
830 err = usb_submit_urb(urb, GFP_ATOMIC);
831 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200832 /* -EPERM: urb is being killed;
833 * -ENODEV: device got disconnected */
834 if (err != -EPERM && err != -ENODEV)
Stefan Seyfried61faddf2010-11-30 21:49:08 +0100835 BT_ERR("%s urb %p failed to resubmit (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200836 hdev->name, urb, -err);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200837 usb_unanchor_urb(urb);
838 }
839}
840
Jesper Juhl42b16b32011-01-17 00:09:38 +0100841static inline void __fill_isoc_descriptor(struct urb *urb, int len, int mtu)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200842{
843 int i, offset = 0;
844
845 BT_DBG("len %d mtu %d", len, mtu);
846
847 for (i = 0; i < BTUSB_MAX_ISOC_FRAMES && len >= mtu;
848 i++, offset += mtu, len -= mtu) {
849 urb->iso_frame_desc[i].offset = offset;
850 urb->iso_frame_desc[i].length = mtu;
851 }
852
853 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
854 urb->iso_frame_desc[i].offset = offset;
855 urb->iso_frame_desc[i].length = len;
856 i++;
857 }
858
859 urb->number_of_packets = i;
860}
861
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100862static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200863{
David Herrmann155961e2012-02-09 21:58:32 +0100864 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200865 struct urb *urb;
866 unsigned char *buf;
867 unsigned int pipe;
868 int err, size;
869
870 BT_DBG("%s", hdev->name);
871
872 if (!data->isoc_rx_ep)
873 return -ENODEV;
874
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100875 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200876 if (!urb)
877 return -ENOMEM;
878
879 size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) *
880 BTUSB_MAX_ISOC_FRAMES;
881
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100882 buf = kmalloc(size, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200883 if (!buf) {
884 usb_free_urb(urb);
885 return -ENOMEM;
886 }
887
888 pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress);
889
Bing Zhaofa0fb932011-12-20 18:19:00 -0800890 usb_fill_int_urb(urb, data->udev, pipe, buf, size, btusb_isoc_complete,
Marcel Holtmann89e75332014-09-16 04:44:50 +0200891 hdev, data->isoc_rx_ep->bInterval);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200892
Marcel Holtmann89e75332014-09-16 04:44:50 +0200893 urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200894
895 __fill_isoc_descriptor(urb, size,
Marcel Holtmann89e75332014-09-16 04:44:50 +0200896 le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize));
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200897
898 usb_anchor_urb(urb, &data->isoc_anchor);
899
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100900 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200901 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +0200902 if (err != -EPERM && err != -ENODEV)
903 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200904 hdev->name, urb, -err);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200905 usb_unanchor_urb(urb);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200906 }
907
908 usb_free_urb(urb);
909
910 return err;
911}
912
Marcel Holtmann9d08f502015-10-08 20:23:08 +0200913static void btusb_diag_complete(struct urb *urb)
914{
915 struct hci_dev *hdev = urb->context;
916 struct btusb_data *data = hci_get_drvdata(hdev);
917 int err;
918
919 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
920 urb->actual_length);
921
922 if (urb->status == 0) {
923 struct sk_buff *skb;
924
925 skb = bt_skb_alloc(urb->actual_length, GFP_ATOMIC);
926 if (skb) {
927 memcpy(skb_put(skb, urb->actual_length),
928 urb->transfer_buffer, urb->actual_length);
929 hci_recv_diag(hdev, skb);
930 }
931 } else if (urb->status == -ENOENT) {
932 /* Avoid suspend failed when usb_kill_urb */
933 return;
934 }
935
936 if (!test_bit(BTUSB_DIAG_RUNNING, &data->flags))
937 return;
938
939 usb_anchor_urb(urb, &data->diag_anchor);
940 usb_mark_last_busy(data->udev);
941
942 err = usb_submit_urb(urb, GFP_ATOMIC);
943 if (err < 0) {
944 /* -EPERM: urb is being killed;
945 * -ENODEV: device got disconnected */
946 if (err != -EPERM && err != -ENODEV)
947 BT_ERR("%s urb %p failed to resubmit (%d)",
948 hdev->name, urb, -err);
949 usb_unanchor_urb(urb);
950 }
951}
952
953static int btusb_submit_diag_urb(struct hci_dev *hdev, gfp_t mem_flags)
954{
955 struct btusb_data *data = hci_get_drvdata(hdev);
956 struct urb *urb;
957 unsigned char *buf;
958 unsigned int pipe;
959 int err, size = HCI_MAX_FRAME_SIZE;
960
961 BT_DBG("%s", hdev->name);
962
963 if (!data->diag_rx_ep)
964 return -ENODEV;
965
966 urb = usb_alloc_urb(0, mem_flags);
967 if (!urb)
968 return -ENOMEM;
969
970 buf = kmalloc(size, mem_flags);
971 if (!buf) {
972 usb_free_urb(urb);
973 return -ENOMEM;
974 }
975
976 pipe = usb_rcvbulkpipe(data->udev, data->diag_rx_ep->bEndpointAddress);
977
978 usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
979 btusb_diag_complete, hdev);
980
981 urb->transfer_flags |= URB_FREE_BUFFER;
982
983 usb_mark_last_busy(data->udev);
984 usb_anchor_urb(urb, &data->diag_anchor);
985
986 err = usb_submit_urb(urb, mem_flags);
987 if (err < 0) {
988 if (err != -EPERM && err != -ENODEV)
989 BT_ERR("%s urb %p submission failed (%d)",
990 hdev->name, urb, -err);
991 usb_unanchor_urb(urb);
992 }
993
994 usb_free_urb(urb);
995
996 return err;
997}
998
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200999static void btusb_tx_complete(struct urb *urb)
1000{
1001 struct sk_buff *skb = urb->context;
Marcel Holtmann89e75332014-09-16 04:44:50 +02001002 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
David Herrmann155961e2012-02-09 21:58:32 +01001003 struct btusb_data *data = hci_get_drvdata(hdev);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001004
Marcel Holtmann89e75332014-09-16 04:44:50 +02001005 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1006 urb->actual_length);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001007
1008 if (!test_bit(HCI_RUNNING, &hdev->flags))
1009 goto done;
1010
1011 if (!urb->status)
1012 hdev->stat.byte_tx += urb->transfer_buffer_length;
1013 else
1014 hdev->stat.err_tx++;
1015
1016done:
1017 spin_lock(&data->txlock);
1018 data->tx_in_flight--;
1019 spin_unlock(&data->txlock);
1020
1021 kfree(urb->setup_packet);
1022
1023 kfree_skb(skb);
1024}
1025
1026static void btusb_isoc_tx_complete(struct urb *urb)
1027{
1028 struct sk_buff *skb = urb->context;
Marcel Holtmann89e75332014-09-16 04:44:50 +02001029 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001030
Marcel Holtmann89e75332014-09-16 04:44:50 +02001031 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1032 urb->actual_length);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001033
1034 if (!test_bit(HCI_RUNNING, &hdev->flags))
1035 goto done;
1036
1037 if (!urb->status)
1038 hdev->stat.byte_tx += urb->transfer_buffer_length;
1039 else
1040 hdev->stat.err_tx++;
1041
1042done:
1043 kfree(urb->setup_packet);
1044
1045 kfree_skb(skb);
1046}
1047
1048static int btusb_open(struct hci_dev *hdev)
1049{
David Herrmann155961e2012-02-09 21:58:32 +01001050 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001051 int err;
1052
1053 BT_DBG("%s", hdev->name);
1054
Ethan Hsiehc7e163f2016-10-07 12:06:42 +08001055 err = usb_autopm_get_interface(data->intf);
1056 if (err < 0)
1057 return err;
1058
Kim, Ben Young Taeace31982015-02-15 23:06:14 +00001059 /* Patching USB firmware files prior to starting any URBs of HCI path
1060 * It is more safe to use USB bulk channel for downloading USB patch
1061 */
1062 if (data->setup_on_usb) {
1063 err = data->setup_on_usb(hdev);
Marcel Holtmanneb500422015-04-16 23:15:50 +02001064 if (err < 0)
Kim, Ben Young Taeace31982015-02-15 23:06:14 +00001065 return err;
1066 }
1067
Oliver Neukum7bee5492009-08-24 23:44:59 +02001068 data->intf->needs_remote_wakeup = 1;
1069
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001070 if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +02001071 goto done;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001072
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001073 err = btusb_submit_intr_urb(hdev, GFP_KERNEL);
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001074 if (err < 0)
1075 goto failed;
1076
1077 err = btusb_submit_bulk_urb(hdev, GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001078 if (err < 0) {
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001079 usb_kill_anchored_urbs(&data->intr_anchor);
1080 goto failed;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001081 }
1082
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001083 set_bit(BTUSB_BULK_RUNNING, &data->flags);
1084 btusb_submit_bulk_urb(hdev, GFP_KERNEL);
1085
Marcel Holtmann9d08f502015-10-08 20:23:08 +02001086 if (data->diag) {
1087 if (!btusb_submit_diag_urb(hdev, GFP_KERNEL))
1088 set_bit(BTUSB_DIAG_RUNNING, &data->flags);
1089 }
1090
Oliver Neukum7bee5492009-08-24 23:44:59 +02001091done:
1092 usb_autopm_put_interface(data->intf);
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001093 return 0;
1094
1095failed:
1096 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001097 usb_autopm_put_interface(data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001098 return err;
1099}
1100
Oliver Neukum7bee5492009-08-24 23:44:59 +02001101static void btusb_stop_traffic(struct btusb_data *data)
1102{
1103 usb_kill_anchored_urbs(&data->intr_anchor);
1104 usb_kill_anchored_urbs(&data->bulk_anchor);
1105 usb_kill_anchored_urbs(&data->isoc_anchor);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02001106 usb_kill_anchored_urbs(&data->diag_anchor);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001107}
1108
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001109static int btusb_close(struct hci_dev *hdev)
1110{
David Herrmann155961e2012-02-09 21:58:32 +01001111 struct btusb_data *data = hci_get_drvdata(hdev);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001112 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001113
1114 BT_DBG("%s", hdev->name);
1115
Marcel Holtmanne8c3c3d2008-09-23 00:16:36 +02001116 cancel_work_sync(&data->work);
Linus Torvalds404291a2009-11-11 13:32:29 -08001117 cancel_work_sync(&data->waker);
Marcel Holtmanne8c3c3d2008-09-23 00:16:36 +02001118
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001119 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001120 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001121 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02001122 clear_bit(BTUSB_DIAG_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001123
1124 btusb_stop_traffic(data);
Marcel Holtmann803b5832014-09-16 08:00:29 +02001125 btusb_free_frags(data);
1126
Oliver Neukum7bee5492009-08-24 23:44:59 +02001127 err = usb_autopm_get_interface(data->intf);
1128 if (err < 0)
Oliver Neukum7b8e2c12009-11-13 14:26:23 +01001129 goto failed;
Oliver Neukum7bee5492009-08-24 23:44:59 +02001130
1131 data->intf->needs_remote_wakeup = 0;
1132 usb_autopm_put_interface(data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001133
Oliver Neukum7b8e2c12009-11-13 14:26:23 +01001134failed:
1135 usb_scuttle_anchored_urbs(&data->deferred);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001136 return 0;
1137}
1138
1139static int btusb_flush(struct hci_dev *hdev)
1140{
David Herrmann155961e2012-02-09 21:58:32 +01001141 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001142
1143 BT_DBG("%s", hdev->name);
1144
1145 usb_kill_anchored_urbs(&data->tx_anchor);
Marcel Holtmann803b5832014-09-16 08:00:29 +02001146 btusb_free_frags(data);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001147
1148 return 0;
1149}
1150
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001151static struct urb *alloc_ctrl_urb(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001152{
David Herrmann155961e2012-02-09 21:58:32 +01001153 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001154 struct usb_ctrlrequest *dr;
1155 struct urb *urb;
1156 unsigned int pipe;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001157
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001158 urb = usb_alloc_urb(0, GFP_KERNEL);
1159 if (!urb)
1160 return ERR_PTR(-ENOMEM);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001161
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001162 dr = kmalloc(sizeof(*dr), GFP_KERNEL);
1163 if (!dr) {
1164 usb_free_urb(urb);
1165 return ERR_PTR(-ENOMEM);
1166 }
1167
1168 dr->bRequestType = data->cmdreq_type;
Marcel Holtmann893ba542015-01-28 20:27:34 -08001169 dr->bRequest = data->cmdreq;
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001170 dr->wIndex = 0;
1171 dr->wValue = 0;
1172 dr->wLength = __cpu_to_le16(skb->len);
1173
1174 pipe = usb_sndctrlpipe(data->udev, 0x00);
1175
Marcel Holtmann89e75332014-09-16 04:44:50 +02001176 usb_fill_control_urb(urb, data->udev, pipe, (void *)dr,
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001177 skb->data, skb->len, btusb_tx_complete, skb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001178
Marcel Holtmann89e75332014-09-16 04:44:50 +02001179 skb->dev = (void *)hdev;
Marcel Holtmann7bd8f092013-10-11 06:19:18 -07001180
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001181 return urb;
1182}
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001183
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001184static struct urb *alloc_bulk_urb(struct hci_dev *hdev, struct sk_buff *skb)
1185{
1186 struct btusb_data *data = hci_get_drvdata(hdev);
1187 struct urb *urb;
1188 unsigned int pipe;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001189
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001190 if (!data->bulk_tx_ep)
1191 return ERR_PTR(-ENODEV);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001192
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001193 urb = usb_alloc_urb(0, GFP_KERNEL);
1194 if (!urb)
1195 return ERR_PTR(-ENOMEM);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001196
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001197 pipe = usb_sndbulkpipe(data->udev, data->bulk_tx_ep->bEndpointAddress);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001198
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001199 usb_fill_bulk_urb(urb, data->udev, pipe,
1200 skb->data, skb->len, btusb_tx_complete, skb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001201
Marcel Holtmann89e75332014-09-16 04:44:50 +02001202 skb->dev = (void *)hdev;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001203
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001204 return urb;
1205}
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001206
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001207static struct urb *alloc_isoc_urb(struct hci_dev *hdev, struct sk_buff *skb)
1208{
1209 struct btusb_data *data = hci_get_drvdata(hdev);
1210 struct urb *urb;
1211 unsigned int pipe;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001212
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001213 if (!data->isoc_tx_ep)
1214 return ERR_PTR(-ENODEV);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001215
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001216 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_KERNEL);
1217 if (!urb)
1218 return ERR_PTR(-ENOMEM);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001219
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001220 pipe = usb_sndisocpipe(data->udev, data->isoc_tx_ep->bEndpointAddress);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001221
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001222 usb_fill_int_urb(urb, data->udev, pipe,
1223 skb->data, skb->len, btusb_isoc_tx_complete,
1224 skb, data->isoc_tx_ep->bInterval);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001225
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001226 urb->transfer_flags = URB_ISO_ASAP;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001227
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001228 __fill_isoc_descriptor(urb, skb->len,
1229 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001230
Marcel Holtmann89e75332014-09-16 04:44:50 +02001231 skb->dev = (void *)hdev;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001232
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001233 return urb;
1234}
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001235
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001236static int submit_tx_urb(struct hci_dev *hdev, struct urb *urb)
1237{
1238 struct btusb_data *data = hci_get_drvdata(hdev);
1239 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001240
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001241 usb_anchor_urb(urb, &data->tx_anchor);
1242
Johan Hedberge9753ef2014-09-14 08:49:34 +03001243 err = usb_submit_urb(urb, GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001244 if (err < 0) {
Paul Bolle5a9b80e2011-10-09 12:12:16 +02001245 if (err != -EPERM && err != -ENODEV)
1246 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +02001247 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001248 kfree(urb->setup_packet);
1249 usb_unanchor_urb(urb);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001250 } else {
1251 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001252 }
1253
Cong Wang54a8a792011-11-22 09:32:57 +08001254 usb_free_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001255 return err;
1256}
1257
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001258static int submit_or_queue_tx_urb(struct hci_dev *hdev, struct urb *urb)
1259{
1260 struct btusb_data *data = hci_get_drvdata(hdev);
1261 unsigned long flags;
1262 bool suspending;
1263
1264 spin_lock_irqsave(&data->txlock, flags);
1265 suspending = test_bit(BTUSB_SUSPENDING, &data->flags);
1266 if (!suspending)
1267 data->tx_in_flight++;
1268 spin_unlock_irqrestore(&data->txlock, flags);
1269
1270 if (!suspending)
1271 return submit_tx_urb(hdev, urb);
1272
1273 usb_anchor_urb(urb, &data->deferred);
1274 schedule_work(&data->waker);
1275
1276 usb_free_urb(urb);
1277 return 0;
1278}
1279
1280static int btusb_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
1281{
1282 struct urb *urb;
1283
1284 BT_DBG("%s", hdev->name);
1285
Marcel Holtmann618e8bc2015-11-05 07:33:56 +01001286 switch (hci_skb_pkt_type(skb)) {
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001287 case HCI_COMMAND_PKT:
1288 urb = alloc_ctrl_urb(hdev, skb);
1289 if (IS_ERR(urb))
1290 return PTR_ERR(urb);
1291
1292 hdev->stat.cmd_tx++;
1293 return submit_or_queue_tx_urb(hdev, urb);
1294
1295 case HCI_ACLDATA_PKT:
1296 urb = alloc_bulk_urb(hdev, skb);
1297 if (IS_ERR(urb))
1298 return PTR_ERR(urb);
1299
1300 hdev->stat.acl_tx++;
1301 return submit_or_queue_tx_urb(hdev, urb);
1302
1303 case HCI_SCODATA_PKT:
1304 if (hci_conn_num(hdev, SCO_LINK) < 1)
1305 return -ENODEV;
1306
1307 urb = alloc_isoc_urb(hdev, skb);
1308 if (IS_ERR(urb))
1309 return PTR_ERR(urb);
1310
1311 hdev->stat.sco_tx++;
1312 return submit_tx_urb(hdev, urb);
1313 }
1314
1315 return -EILSEQ;
1316}
1317
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001318static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
1319{
David Herrmann155961e2012-02-09 21:58:32 +01001320 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001321
1322 BT_DBG("%s evt %d", hdev->name, evt);
1323
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001324 if (hci_conn_num(hdev, SCO_LINK) != data->sco_num) {
1325 data->sco_num = hci_conn_num(hdev, SCO_LINK);
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001326 schedule_work(&data->work);
Marcel Holtmanna780efa2008-11-30 12:17:12 +01001327 }
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001328}
1329
Jesper Juhl42b16b32011-01-17 00:09:38 +01001330static inline int __set_isoc_interface(struct hci_dev *hdev, int altsetting)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001331{
David Herrmann155961e2012-02-09 21:58:32 +01001332 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001333 struct usb_interface *intf = data->isoc;
1334 struct usb_endpoint_descriptor *ep_desc;
1335 int i, err;
1336
1337 if (!data->isoc)
1338 return -ENODEV;
1339
1340 err = usb_set_interface(data->udev, 1, altsetting);
1341 if (err < 0) {
1342 BT_ERR("%s setting interface failed (%d)", hdev->name, -err);
1343 return err;
1344 }
1345
1346 data->isoc_altsetting = altsetting;
1347
1348 data->isoc_tx_ep = NULL;
1349 data->isoc_rx_ep = NULL;
1350
1351 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
1352 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
1353
1354 if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) {
1355 data->isoc_tx_ep = ep_desc;
1356 continue;
1357 }
1358
1359 if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) {
1360 data->isoc_rx_ep = ep_desc;
1361 continue;
1362 }
1363 }
1364
1365 if (!data->isoc_tx_ep || !data->isoc_rx_ep) {
1366 BT_ERR("%s invalid SCO descriptors", hdev->name);
1367 return -ENODEV;
1368 }
1369
1370 return 0;
1371}
1372
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001373static void btusb_work(struct work_struct *work)
1374{
1375 struct btusb_data *data = container_of(work, struct btusb_data, work);
1376 struct hci_dev *hdev = data->hdev;
Mikel Astizf4001d22012-04-11 08:48:51 +02001377 int new_alts;
Oliver Neukum7bee5492009-08-24 23:44:59 +02001378 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001379
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001380 if (data->sco_num > 0) {
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -03001381 if (!test_bit(BTUSB_DID_ISO_RESUME, &data->flags)) {
Oliver Neukum8efdd0c2011-02-11 13:00:06 +01001382 err = usb_autopm_get_interface(data->isoc ? data->isoc : data->intf);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001383 if (err < 0) {
1384 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1385 usb_kill_anchored_urbs(&data->isoc_anchor);
1386 return;
1387 }
1388
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -03001389 set_bit(BTUSB_DID_ISO_RESUME, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001390 }
Mikel Astizf4001d22012-04-11 08:48:51 +02001391
1392 if (hdev->voice_setting & 0x0020) {
1393 static const int alts[3] = { 2, 4, 5 };
Marcel Holtmann89e75332014-09-16 04:44:50 +02001394
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001395 new_alts = alts[data->sco_num - 1];
Mikel Astizf4001d22012-04-11 08:48:51 +02001396 } else {
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001397 new_alts = data->sco_num;
Mikel Astizf4001d22012-04-11 08:48:51 +02001398 }
1399
1400 if (data->isoc_altsetting != new_alts) {
Kuba Pawlakf6fc86f2015-10-28 15:18:05 +01001401 unsigned long flags;
1402
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001403 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1404 usb_kill_anchored_urbs(&data->isoc_anchor);
1405
Kuba Pawlak8f9d02f2015-09-10 17:07:00 +01001406 /* When isochronous alternate setting needs to be
1407 * changed, because SCO connection has been added
1408 * or removed, a packet fragment may be left in the
1409 * reassembling state. This could lead to wrongly
1410 * assembled fragments.
1411 *
1412 * Clear outstanding fragment when selecting a new
1413 * alternate setting.
1414 */
Kuba Pawlakf6fc86f2015-10-28 15:18:05 +01001415 spin_lock_irqsave(&data->rxlock, flags);
Kuba Pawlak8f9d02f2015-09-10 17:07:00 +01001416 kfree_skb(data->sco_skb);
1417 data->sco_skb = NULL;
Kuba Pawlakf6fc86f2015-10-28 15:18:05 +01001418 spin_unlock_irqrestore(&data->rxlock, flags);
Kuba Pawlak8f9d02f2015-09-10 17:07:00 +01001419
Mikel Astizf4001d22012-04-11 08:48:51 +02001420 if (__set_isoc_interface(hdev, new_alts) < 0)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001421 return;
1422 }
1423
1424 if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001425 if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001426 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1427 else
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001428 btusb_submit_isoc_urb(hdev, GFP_KERNEL);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001429 }
1430 } else {
1431 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1432 usb_kill_anchored_urbs(&data->isoc_anchor);
1433
1434 __set_isoc_interface(hdev, 0);
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -03001435 if (test_and_clear_bit(BTUSB_DID_ISO_RESUME, &data->flags))
Oliver Neukum8efdd0c2011-02-11 13:00:06 +01001436 usb_autopm_put_interface(data->isoc ? data->isoc : data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001437 }
1438}
1439
Oliver Neukum7bee5492009-08-24 23:44:59 +02001440static void btusb_waker(struct work_struct *work)
1441{
1442 struct btusb_data *data = container_of(work, struct btusb_data, waker);
1443 int err;
1444
1445 err = usb_autopm_get_interface(data->intf);
1446 if (err < 0)
1447 return;
1448
1449 usb_autopm_put_interface(data->intf);
1450}
1451
Marcel Holtmann9f8f9622013-04-10 08:11:35 -07001452static int btusb_setup_bcm92035(struct hci_dev *hdev)
1453{
1454 struct sk_buff *skb;
1455 u8 val = 0x00;
1456
1457 BT_DBG("%s", hdev->name);
1458
1459 skb = __hci_cmd_sync(hdev, 0xfc3b, 1, &val, HCI_INIT_TIMEOUT);
1460 if (IS_ERR(skb))
1461 BT_ERR("BCM92035 command failed (%ld)", -PTR_ERR(skb));
1462 else
1463 kfree_skb(skb);
1464
1465 return 0;
1466}
1467
Marcel Holtmann81cac642014-01-03 03:02:36 -08001468static int btusb_setup_csr(struct hci_dev *hdev)
1469{
1470 struct hci_rp_read_local_version *rp;
1471 struct sk_buff *skb;
Marcel Holtmann81cac642014-01-03 03:02:36 -08001472
1473 BT_DBG("%s", hdev->name);
1474
Marcel Holtmann7cd84d72015-06-07 10:01:02 +02001475 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
1476 HCI_INIT_TIMEOUT);
1477 if (IS_ERR(skb)) {
1478 int err = PTR_ERR(skb);
1479 BT_ERR("%s: CSR: Local version failed (%d)", hdev->name, err);
1480 return err;
1481 }
1482
1483 if (skb->len != sizeof(struct hci_rp_read_local_version)) {
1484 BT_ERR("%s: CSR: Local version length mismatch", hdev->name);
1485 kfree_skb(skb);
1486 return -EIO;
1487 }
Marcel Holtmann81cac642014-01-03 03:02:36 -08001488
Marcel Holtmann89e75332014-09-16 04:44:50 +02001489 rp = (struct hci_rp_read_local_version *)skb->data;
Marcel Holtmann81cac642014-01-03 03:02:36 -08001490
Johan Hedberg6cafcd92015-08-30 21:47:21 +03001491 /* Detect controllers which aren't real CSR ones. */
1492 if (le16_to_cpu(rp->manufacturer) != 10 ||
1493 le16_to_cpu(rp->lmp_subver) == 0x0c5c) {
Marcel Holtmann9641d342015-06-07 10:01:01 +02001494 /* Clear the reset quirk since this is not an actual
1495 * early Bluetooth 1.1 device from CSR.
1496 */
1497 clear_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmann81cac642014-01-03 03:02:36 -08001498
Marcel Holtmann9641d342015-06-07 10:01:01 +02001499 /* These fake CSR controllers have all a broken
1500 * stored link key handling and so just disable it.
1501 */
1502 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
Marcel Holtmann81cac642014-01-03 03:02:36 -08001503 }
1504
Marcel Holtmann81cac642014-01-03 03:02:36 -08001505 kfree_skb(skb);
1506
Marcel Holtmann9641d342015-06-07 10:01:01 +02001507 return 0;
Marcel Holtmann81cac642014-01-03 03:02:36 -08001508}
1509
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001510static const struct firmware *btusb_setup_intel_get_fw(struct hci_dev *hdev,
Marcel Holtmann89e75332014-09-16 04:44:50 +02001511 struct intel_version *ver)
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001512{
1513 const struct firmware *fw;
1514 char fwname[64];
1515 int ret;
1516
1517 snprintf(fwname, sizeof(fwname),
1518 "intel/ibt-hw-%x.%x.%x-fw-%x.%x.%x.%x.%x.bseq",
1519 ver->hw_platform, ver->hw_variant, ver->hw_revision,
1520 ver->fw_variant, ver->fw_revision, ver->fw_build_num,
1521 ver->fw_build_ww, ver->fw_build_yy);
1522
1523 ret = request_firmware(&fw, fwname, &hdev->dev);
1524 if (ret < 0) {
1525 if (ret == -EINVAL) {
1526 BT_ERR("%s Intel firmware file request failed (%d)",
1527 hdev->name, ret);
1528 return NULL;
1529 }
1530
1531 BT_ERR("%s failed to open Intel firmware file: %s(%d)",
1532 hdev->name, fwname, ret);
1533
1534 /* If the correct firmware patch file is not found, use the
1535 * default firmware patch file instead
1536 */
1537 snprintf(fwname, sizeof(fwname), "intel/ibt-hw-%x.%x.bseq",
1538 ver->hw_platform, ver->hw_variant);
1539 if (request_firmware(&fw, fwname, &hdev->dev) < 0) {
1540 BT_ERR("%s failed to open default Intel fw file: %s",
1541 hdev->name, fwname);
1542 return NULL;
1543 }
1544 }
1545
1546 BT_INFO("%s: Intel Bluetooth firmware file: %s", hdev->name, fwname);
1547
1548 return fw;
1549}
1550
1551static int btusb_setup_intel_patching(struct hci_dev *hdev,
1552 const struct firmware *fw,
1553 const u8 **fw_ptr, int *disable_patch)
1554{
1555 struct sk_buff *skb;
1556 struct hci_command_hdr *cmd;
1557 const u8 *cmd_param;
1558 struct hci_event_hdr *evt = NULL;
1559 const u8 *evt_param = NULL;
1560 int remain = fw->size - (*fw_ptr - fw->data);
1561
1562 /* The first byte indicates the types of the patch command or event.
1563 * 0x01 means HCI command and 0x02 is HCI event. If the first bytes
1564 * in the current firmware buffer doesn't start with 0x01 or
1565 * the size of remain buffer is smaller than HCI command header,
1566 * the firmware file is corrupted and it should stop the patching
1567 * process.
1568 */
1569 if (remain > HCI_COMMAND_HDR_SIZE && *fw_ptr[0] != 0x01) {
1570 BT_ERR("%s Intel fw corrupted: invalid cmd read", hdev->name);
1571 return -EINVAL;
1572 }
1573 (*fw_ptr)++;
1574 remain--;
1575
1576 cmd = (struct hci_command_hdr *)(*fw_ptr);
1577 *fw_ptr += sizeof(*cmd);
1578 remain -= sizeof(*cmd);
1579
1580 /* Ensure that the remain firmware data is long enough than the length
1581 * of command parameter. If not, the firmware file is corrupted.
1582 */
1583 if (remain < cmd->plen) {
1584 BT_ERR("%s Intel fw corrupted: invalid cmd len", hdev->name);
1585 return -EFAULT;
1586 }
1587
1588 /* If there is a command that loads a patch in the firmware
1589 * file, then enable the patch upon success, otherwise just
1590 * disable the manufacturer mode, for example patch activation
1591 * is not required when the default firmware patch file is used
1592 * because there are no patch data to load.
1593 */
1594 if (*disable_patch && le16_to_cpu(cmd->opcode) == 0xfc8e)
1595 *disable_patch = 0;
1596
1597 cmd_param = *fw_ptr;
1598 *fw_ptr += cmd->plen;
1599 remain -= cmd->plen;
1600
1601 /* This reads the expected events when the above command is sent to the
1602 * device. Some vendor commands expects more than one events, for
1603 * example command status event followed by vendor specific event.
1604 * For this case, it only keeps the last expected event. so the command
1605 * can be sent with __hci_cmd_sync_ev() which returns the sk_buff of
1606 * last expected event.
1607 */
1608 while (remain > HCI_EVENT_HDR_SIZE && *fw_ptr[0] == 0x02) {
1609 (*fw_ptr)++;
1610 remain--;
1611
1612 evt = (struct hci_event_hdr *)(*fw_ptr);
1613 *fw_ptr += sizeof(*evt);
1614 remain -= sizeof(*evt);
1615
1616 if (remain < evt->plen) {
1617 BT_ERR("%s Intel fw corrupted: invalid evt len",
1618 hdev->name);
1619 return -EFAULT;
1620 }
1621
1622 evt_param = *fw_ptr;
1623 *fw_ptr += evt->plen;
1624 remain -= evt->plen;
1625 }
1626
1627 /* Every HCI commands in the firmware file has its correspond event.
1628 * If event is not found or remain is smaller than zero, the firmware
1629 * file is corrupted.
1630 */
1631 if (!evt || !evt_param || remain < 0) {
1632 BT_ERR("%s Intel fw corrupted: invalid evt read", hdev->name);
1633 return -EFAULT;
1634 }
1635
1636 skb = __hci_cmd_sync_ev(hdev, le16_to_cpu(cmd->opcode), cmd->plen,
1637 cmd_param, evt->evt, HCI_INIT_TIMEOUT);
1638 if (IS_ERR(skb)) {
1639 BT_ERR("%s sending Intel patch command (0x%4.4x) failed (%ld)",
1640 hdev->name, cmd->opcode, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001641 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001642 }
1643
1644 /* It ensures that the returned event matches the event data read from
1645 * the firmware file. At fist, it checks the length and then
1646 * the contents of the event.
1647 */
1648 if (skb->len != evt->plen) {
1649 BT_ERR("%s mismatch event length (opcode 0x%4.4x)", hdev->name,
1650 le16_to_cpu(cmd->opcode));
1651 kfree_skb(skb);
1652 return -EFAULT;
1653 }
1654
1655 if (memcmp(skb->data, evt_param, evt->plen)) {
1656 BT_ERR("%s mismatch event parameter (opcode 0x%4.4x)",
1657 hdev->name, le16_to_cpu(cmd->opcode));
1658 kfree_skb(skb);
1659 return -EFAULT;
1660 }
1661 kfree_skb(skb);
1662
1663 return 0;
1664}
1665
1666static int btusb_setup_intel(struct hci_dev *hdev)
1667{
1668 struct sk_buff *skb;
1669 const struct firmware *fw;
1670 const u8 *fw_ptr;
Loic Poulain28dc4b92015-12-03 16:10:22 +01001671 int disable_patch, err;
Loic Poulain6c483de2015-12-06 16:18:34 +01001672 struct intel_version ver;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001673
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001674 BT_DBG("%s", hdev->name);
1675
1676 /* The controller has a bug with the first HCI command sent to it
1677 * returning number of completed commands as zero. This would stall the
1678 * command processing in the Bluetooth core.
1679 *
1680 * As a workaround, send HCI Reset command first which will reset the
1681 * number of completed commands and allow normal command processing
1682 * from now on.
1683 */
1684 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
1685 if (IS_ERR(skb)) {
1686 BT_ERR("%s sending initial HCI reset command failed (%ld)",
1687 hdev->name, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001688 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001689 }
1690 kfree_skb(skb);
1691
1692 /* Read Intel specific controller version first to allow selection of
1693 * which firmware file to load.
1694 *
1695 * The returned information are hardware variant and revision plus
1696 * firmware variant, revision and build number.
1697 */
Loic Poulain6c483de2015-12-06 16:18:34 +01001698 err = btintel_read_version(hdev, &ver);
1699 if (err)
1700 return err;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001701
1702 BT_INFO("%s: read Intel version: %02x%02x%02x%02x%02x%02x%02x%02x%02x",
Loic Poulain6c483de2015-12-06 16:18:34 +01001703 hdev->name, ver.hw_platform, ver.hw_variant, ver.hw_revision,
1704 ver.fw_variant, ver.fw_revision, ver.fw_build_num,
1705 ver.fw_build_ww, ver.fw_build_yy, ver.fw_patch_num);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001706
1707 /* fw_patch_num indicates the version of patch the device currently
1708 * have. If there is no patch data in the device, it is always 0x00.
Minjune Kim5075eda2015-08-27 13:21:52 +09001709 * So, if it is other than 0x00, no need to patch the device again.
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001710 */
Loic Poulain6c483de2015-12-06 16:18:34 +01001711 if (ver.fw_patch_num) {
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001712 BT_INFO("%s: Intel device is already patched. patch num: %02x",
Loic Poulain6c483de2015-12-06 16:18:34 +01001713 hdev->name, ver.fw_patch_num);
Marcel Holtmann213445b2015-10-21 02:45:19 +02001714 goto complete;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001715 }
1716
1717 /* Opens the firmware patch file based on the firmware version read
1718 * from the controller. If it fails to open the matching firmware
1719 * patch file, it tries to open the default firmware patch file.
1720 * If no patch file is found, allow the device to operate without
1721 * a patch.
1722 */
Loic Poulain6c483de2015-12-06 16:18:34 +01001723 fw = btusb_setup_intel_get_fw(hdev, &ver);
1724 if (!fw)
Marcel Holtmann213445b2015-10-21 02:45:19 +02001725 goto complete;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001726 fw_ptr = fw->data;
1727
Loic Poulain28dc4b92015-12-03 16:10:22 +01001728 /* Enable the manufacturer mode of the controller.
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001729 * Only while this mode is enabled, the driver can download the
1730 * firmware patch data and configuration parameters.
1731 */
Loic Poulain28dc4b92015-12-03 16:10:22 +01001732 err = btintel_enter_mfg(hdev);
1733 if (err) {
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001734 release_firmware(fw);
Loic Poulain28dc4b92015-12-03 16:10:22 +01001735 return err;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001736 }
1737
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001738 disable_patch = 1;
1739
1740 /* The firmware data file consists of list of Intel specific HCI
1741 * commands and its expected events. The first byte indicates the
1742 * type of the message, either HCI command or HCI event.
1743 *
1744 * It reads the command and its expected event from the firmware file,
1745 * and send to the controller. Once __hci_cmd_sync_ev() returns,
1746 * the returned event is compared with the event read from the firmware
1747 * file and it will continue until all the messages are downloaded to
1748 * the controller.
1749 *
1750 * Once the firmware patching is completed successfully,
1751 * the manufacturer mode is disabled with reset and activating the
1752 * downloaded patch.
1753 *
1754 * If the firmware patching fails, the manufacturer mode is
1755 * disabled with reset and deactivating the patch.
1756 *
1757 * If the default patch file is used, no reset is done when disabling
1758 * the manufacturer.
1759 */
1760 while (fw->size > fw_ptr - fw->data) {
1761 int ret;
1762
1763 ret = btusb_setup_intel_patching(hdev, fw, &fw_ptr,
1764 &disable_patch);
1765 if (ret < 0)
1766 goto exit_mfg_deactivate;
1767 }
1768
1769 release_firmware(fw);
1770
1771 if (disable_patch)
1772 goto exit_mfg_disable;
1773
1774 /* Patching completed successfully and disable the manufacturer mode
1775 * with reset and activate the downloaded firmware patches.
1776 */
Loic Poulain28dc4b92015-12-03 16:10:22 +01001777 err = btintel_exit_mfg(hdev, true, true);
1778 if (err)
1779 return err;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001780
1781 BT_INFO("%s: Intel Bluetooth firmware patch completed and activated",
1782 hdev->name);
1783
Marcel Holtmann213445b2015-10-21 02:45:19 +02001784 goto complete;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001785
1786exit_mfg_disable:
1787 /* Disable the manufacturer mode without reset */
Loic Poulain28dc4b92015-12-03 16:10:22 +01001788 err = btintel_exit_mfg(hdev, false, false);
1789 if (err)
1790 return err;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001791
1792 BT_INFO("%s: Intel Bluetooth firmware patch completed", hdev->name);
Marcel Holtmann40cb0982014-07-02 12:06:45 +02001793
Marcel Holtmann213445b2015-10-21 02:45:19 +02001794 goto complete;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001795
1796exit_mfg_deactivate:
1797 release_firmware(fw);
1798
1799 /* Patching failed. Disable the manufacturer mode with reset and
1800 * deactivate the downloaded firmware patches.
1801 */
Loic Poulain28dc4b92015-12-03 16:10:22 +01001802 err = btintel_exit_mfg(hdev, true, false);
1803 if (err)
1804 return err;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001805
1806 BT_INFO("%s: Intel Bluetooth firmware patch completed and deactivated",
1807 hdev->name);
1808
Marcel Holtmann213445b2015-10-21 02:45:19 +02001809complete:
1810 /* Set the event mask for Intel specific vendor events. This enables
1811 * a few extra events that are useful during general operation.
1812 */
1813 btintel_set_event_mask_mfg(hdev, false);
1814
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07001815 btintel_check_bdaddr(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001816 return 0;
1817}
1818
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001819static int inject_cmd_complete(struct hci_dev *hdev, __u16 opcode)
1820{
1821 struct sk_buff *skb;
1822 struct hci_event_hdr *hdr;
1823 struct hci_ev_cmd_complete *evt;
1824
1825 skb = bt_skb_alloc(sizeof(*hdr) + sizeof(*evt) + 1, GFP_ATOMIC);
1826 if (!skb)
1827 return -ENOMEM;
1828
1829 hdr = (struct hci_event_hdr *)skb_put(skb, sizeof(*hdr));
1830 hdr->evt = HCI_EV_CMD_COMPLETE;
1831 hdr->plen = sizeof(*evt) + 1;
1832
1833 evt = (struct hci_ev_cmd_complete *)skb_put(skb, sizeof(*evt));
1834 evt->ncmd = 0x01;
1835 evt->opcode = cpu_to_le16(opcode);
1836
1837 *skb_put(skb, 1) = 0x00;
1838
Marcel Holtmann618e8bc2015-11-05 07:33:56 +01001839 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001840
1841 return hci_recv_frame(hdev, skb);
1842}
1843
1844static int btusb_recv_bulk_intel(struct btusb_data *data, void *buffer,
1845 int count)
1846{
1847 /* When the device is in bootloader mode, then it can send
1848 * events via the bulk endpoint. These events are treated the
1849 * same way as the ones received from the interrupt endpoint.
1850 */
1851 if (test_bit(BTUSB_BOOTLOADER, &data->flags))
1852 return btusb_recv_intr(data, buffer, count);
1853
1854 return btusb_recv_bulk(data, buffer, count);
1855}
1856
Marcel Holtmannccd6da22015-04-09 00:35:19 -07001857static void btusb_intel_bootup(struct btusb_data *data, const void *ptr,
1858 unsigned int len)
1859{
1860 const struct intel_bootup *evt = ptr;
1861
1862 if (len != sizeof(*evt))
1863 return;
1864
1865 if (test_and_clear_bit(BTUSB_BOOTING, &data->flags)) {
1866 smp_mb__after_atomic();
1867 wake_up_bit(&data->flags, BTUSB_BOOTING);
1868 }
1869}
1870
1871static void btusb_intel_secure_send_result(struct btusb_data *data,
1872 const void *ptr, unsigned int len)
1873{
1874 const struct intel_secure_send_result *evt = ptr;
1875
1876 if (len != sizeof(*evt))
1877 return;
1878
1879 if (evt->result)
1880 set_bit(BTUSB_FIRMWARE_FAILED, &data->flags);
1881
1882 if (test_and_clear_bit(BTUSB_DOWNLOADING, &data->flags) &&
1883 test_bit(BTUSB_FIRMWARE_LOADED, &data->flags)) {
1884 smp_mb__after_atomic();
1885 wake_up_bit(&data->flags, BTUSB_DOWNLOADING);
1886 }
1887}
1888
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001889static int btusb_recv_event_intel(struct hci_dev *hdev, struct sk_buff *skb)
1890{
1891 struct btusb_data *data = hci_get_drvdata(hdev);
1892
1893 if (test_bit(BTUSB_BOOTLOADER, &data->flags)) {
1894 struct hci_event_hdr *hdr = (void *)skb->data;
1895
Marcel Holtmannccd6da22015-04-09 00:35:19 -07001896 if (skb->len > HCI_EVENT_HDR_SIZE && hdr->evt == 0xff &&
1897 hdr->plen > 0) {
1898 const void *ptr = skb->data + HCI_EVENT_HDR_SIZE + 1;
1899 unsigned int len = skb->len - HCI_EVENT_HDR_SIZE - 1;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001900
Marcel Holtmannccd6da22015-04-09 00:35:19 -07001901 switch (skb->data[2]) {
1902 case 0x02:
1903 /* When switching to the operational firmware
1904 * the device sends a vendor specific event
1905 * indicating that the bootup completed.
1906 */
1907 btusb_intel_bootup(data, ptr, len);
1908 break;
1909 case 0x06:
1910 /* When the firmware loading completes the
1911 * device sends out a vendor specific event
1912 * indicating the result of the firmware
1913 * loading.
1914 */
1915 btusb_intel_secure_send_result(data, ptr, len);
1916 break;
Johan Hedbergfad70972015-01-30 10:58:55 +02001917 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001918 }
1919 }
1920
1921 return hci_recv_frame(hdev, skb);
1922}
1923
1924static int btusb_send_frame_intel(struct hci_dev *hdev, struct sk_buff *skb)
1925{
1926 struct btusb_data *data = hci_get_drvdata(hdev);
1927 struct urb *urb;
1928
1929 BT_DBG("%s", hdev->name);
1930
Marcel Holtmann618e8bc2015-11-05 07:33:56 +01001931 switch (hci_skb_pkt_type(skb)) {
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001932 case HCI_COMMAND_PKT:
1933 if (test_bit(BTUSB_BOOTLOADER, &data->flags)) {
1934 struct hci_command_hdr *cmd = (void *)skb->data;
1935 __u16 opcode = le16_to_cpu(cmd->opcode);
1936
1937 /* When in bootloader mode and the command 0xfc09
1938 * is received, it needs to be send down the
1939 * bulk endpoint. So allocate a bulk URB instead.
1940 */
1941 if (opcode == 0xfc09)
1942 urb = alloc_bulk_urb(hdev, skb);
1943 else
1944 urb = alloc_ctrl_urb(hdev, skb);
1945
1946 /* When the 0xfc01 command is issued to boot into
1947 * the operational firmware, it will actually not
1948 * send a command complete event. To keep the flow
1949 * control working inject that event here.
1950 */
1951 if (opcode == 0xfc01)
1952 inject_cmd_complete(hdev, opcode);
1953 } else {
1954 urb = alloc_ctrl_urb(hdev, skb);
1955 }
1956 if (IS_ERR(urb))
1957 return PTR_ERR(urb);
1958
1959 hdev->stat.cmd_tx++;
1960 return submit_or_queue_tx_urb(hdev, urb);
1961
1962 case HCI_ACLDATA_PKT:
1963 urb = alloc_bulk_urb(hdev, skb);
1964 if (IS_ERR(urb))
1965 return PTR_ERR(urb);
1966
1967 hdev->stat.acl_tx++;
1968 return submit_or_queue_tx_urb(hdev, urb);
1969
1970 case HCI_SCODATA_PKT:
1971 if (hci_conn_num(hdev, SCO_LINK) < 1)
1972 return -ENODEV;
1973
1974 urb = alloc_isoc_urb(hdev, skb);
1975 if (IS_ERR(urb))
1976 return PTR_ERR(urb);
1977
1978 hdev->stat.sco_tx++;
1979 return submit_tx_urb(hdev, urb);
1980 }
1981
1982 return -EILSEQ;
1983}
1984
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001985static int btusb_setup_intel_new(struct hci_dev *hdev)
1986{
1987 static const u8 reset_param[] = { 0x00, 0x01, 0x00, 0x01,
1988 0x00, 0x08, 0x04, 0x00 };
1989 struct btusb_data *data = hci_get_drvdata(hdev);
1990 struct sk_buff *skb;
Loic Poulain6c483de2015-12-06 16:18:34 +01001991 struct intel_version ver;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001992 struct intel_boot_params *params;
1993 const struct firmware *fw;
1994 const u8 *fw_ptr;
Marcel Holtmanne66890a92015-06-07 09:47:08 +02001995 u32 frag_len;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001996 char fwname[64];
1997 ktime_t calltime, delta, rettime;
1998 unsigned long long duration;
1999 int err;
2000
2001 BT_DBG("%s", hdev->name);
2002
2003 calltime = ktime_get();
2004
2005 /* Read the Intel version information to determine if the device
2006 * is in bootloader mode or if it already has operational firmware
2007 * loaded.
2008 */
Loic Poulain6c483de2015-12-06 16:18:34 +01002009 err = btintel_read_version(hdev, &ver);
2010 if (err)
2011 return err;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002012
2013 /* The hardware platform number has a fixed value of 0x37 and
2014 * for now only accept this single value.
2015 */
Loic Poulain6c483de2015-12-06 16:18:34 +01002016 if (ver.hw_platform != 0x37) {
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002017 BT_ERR("%s: Unsupported Intel hardware platform (%u)",
Loic Poulain6c483de2015-12-06 16:18:34 +01002018 hdev->name, ver.hw_platform);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002019 return -EINVAL;
2020 }
2021
Tedd Ho-Jeong Ana0af53b2016-05-06 11:53:46 -07002022 /* At the moment the iBT 3.0 hardware variants 0x0b (LnP/SfP)
2023 * and 0x0c (WsP) are supported by this firmware loading method.
2024 *
2025 * This check has been put in place to ensure correct forward
2026 * compatibility options when newer hardware variants come along.
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002027 */
Tedd Ho-Jeong Ana0af53b2016-05-06 11:53:46 -07002028 if (ver.hw_variant != 0x0b && ver.hw_variant != 0x0c) {
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002029 BT_ERR("%s: Unsupported Intel hardware variant (%u)",
Loic Poulain6c483de2015-12-06 16:18:34 +01002030 hdev->name, ver.hw_variant);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002031 return -EINVAL;
2032 }
2033
Loic Poulain6c483de2015-12-06 16:18:34 +01002034 btintel_version_info(hdev, &ver);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002035
2036 /* The firmware variant determines if the device is in bootloader
2037 * mode or is running operational firmware. The value 0x06 identifies
2038 * the bootloader and the value 0x23 identifies the operational
2039 * firmware.
2040 *
2041 * When the operational firmware is already present, then only
2042 * the check for valid Bluetooth device address is needed. This
2043 * determines if the device will be added as configured or
2044 * unconfigured controller.
2045 *
2046 * It is not possible to use the Secure Boot Parameters in this
2047 * case since that command is only available in bootloader mode.
2048 */
Loic Poulain6c483de2015-12-06 16:18:34 +01002049 if (ver.fw_variant == 0x23) {
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002050 clear_bit(BTUSB_BOOTLOADER, &data->flags);
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07002051 btintel_check_bdaddr(hdev);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002052 return 0;
2053 }
2054
2055 /* If the device is not in bootloader mode, then the only possible
2056 * choice is to return an error and abort the device initialization.
2057 */
Loic Poulain6c483de2015-12-06 16:18:34 +01002058 if (ver.fw_variant != 0x06) {
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002059 BT_ERR("%s: Unsupported Intel firmware variant (%u)",
Loic Poulain6c483de2015-12-06 16:18:34 +01002060 hdev->name, ver.fw_variant);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002061 return -ENODEV;
2062 }
2063
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002064 /* Read the secure boot parameters to identify the operating
2065 * details of the bootloader.
2066 */
2067 skb = __hci_cmd_sync(hdev, 0xfc0d, 0, NULL, HCI_INIT_TIMEOUT);
2068 if (IS_ERR(skb)) {
2069 BT_ERR("%s: Reading Intel boot parameters failed (%ld)",
2070 hdev->name, PTR_ERR(skb));
2071 return PTR_ERR(skb);
2072 }
2073
2074 if (skb->len != sizeof(*params)) {
2075 BT_ERR("%s: Intel boot parameters size mismatch", hdev->name);
2076 kfree_skb(skb);
2077 return -EILSEQ;
2078 }
2079
2080 params = (struct intel_boot_params *)skb->data;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002081
2082 BT_INFO("%s: Device revision is %u", hdev->name,
2083 le16_to_cpu(params->dev_revid));
2084
2085 BT_INFO("%s: Secure boot is %s", hdev->name,
2086 params->secure_boot ? "enabled" : "disabled");
2087
Marcel Holtmann2220994e2015-10-13 13:54:55 +02002088 BT_INFO("%s: OTP lock is %s", hdev->name,
2089 params->otp_lock ? "enabled" : "disabled");
2090
2091 BT_INFO("%s: API lock is %s", hdev->name,
2092 params->api_lock ? "enabled" : "disabled");
2093
2094 BT_INFO("%s: Debug lock is %s", hdev->name,
2095 params->debug_lock ? "enabled" : "disabled");
2096
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002097 BT_INFO("%s: Minimum firmware build %u week %u %u", hdev->name,
2098 params->min_fw_build_nn, params->min_fw_build_cw,
2099 2000 + params->min_fw_build_yy);
2100
2101 /* It is required that every single firmware fragment is acknowledged
2102 * with a command complete event. If the boot parameters indicate
2103 * that this bootloader does not send them, then abort the setup.
2104 */
2105 if (params->limited_cce != 0x00) {
2106 BT_ERR("%s: Unsupported Intel firmware loading method (%u)",
2107 hdev->name, params->limited_cce);
2108 kfree_skb(skb);
2109 return -EINVAL;
2110 }
2111
2112 /* If the OTP has no valid Bluetooth device address, then there will
2113 * also be no valid address for the operational firmware.
2114 */
2115 if (!bacmp(&params->otp_bdaddr, BDADDR_ANY)) {
2116 BT_INFO("%s: No device address configured", hdev->name);
2117 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
2118 }
2119
2120 /* With this Intel bootloader only the hardware variant and device
2121 * revision information are used to select the right firmware.
2122 *
Tedd Ho-Jeong An230b04a2016-06-28 08:56:39 -07002123 * The firmware filename is ibt-<hw_variant>-<dev_revid>.sfi.
2124 *
2125 * Currently the supported hardware variants are:
2126 * 11 (0x0b) for iBT3.0 (LnP/SfP)
2127 * 12 (0x0c) for iBT3.5 (WsP)
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002128 */
Tedd Ho-Jeong An230b04a2016-06-28 08:56:39 -07002129 snprintf(fwname, sizeof(fwname), "intel/ibt-%u-%u.sfi",
2130 le16_to_cpu(ver.hw_variant),
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002131 le16_to_cpu(params->dev_revid));
2132
2133 err = request_firmware(&fw, fwname, &hdev->dev);
2134 if (err < 0) {
2135 BT_ERR("%s: Failed to load Intel firmware file (%d)",
2136 hdev->name, err);
2137 kfree_skb(skb);
2138 return err;
2139 }
2140
2141 BT_INFO("%s: Found device firmware: %s", hdev->name, fwname);
2142
Tedd Ho-Jeong An52cc9162015-06-12 16:20:05 -07002143 /* Save the DDC file name for later use to apply once the firmware
2144 * downloading is done.
2145 */
Tedd Ho-Jeong An230b04a2016-06-28 08:56:39 -07002146 snprintf(fwname, sizeof(fwname), "intel/ibt-%u-%u.ddc",
2147 le16_to_cpu(ver.hw_variant),
Tedd Ho-Jeong An52cc9162015-06-12 16:20:05 -07002148 le16_to_cpu(params->dev_revid));
2149
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002150 kfree_skb(skb);
2151
2152 if (fw->size < 644) {
2153 BT_ERR("%s: Invalid size of firmware file (%zu)",
2154 hdev->name, fw->size);
2155 err = -EBADF;
2156 goto done;
2157 }
2158
2159 set_bit(BTUSB_DOWNLOADING, &data->flags);
2160
2161 /* Start the firmware download transaction with the Init fragment
2162 * represented by the 128 bytes of CSS header.
2163 */
Marcel Holtmann09df1232015-07-05 14:55:36 +02002164 err = btintel_secure_send(hdev, 0x00, 128, fw->data);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002165 if (err < 0) {
2166 BT_ERR("%s: Failed to send firmware header (%d)",
2167 hdev->name, err);
2168 goto done;
2169 }
2170
2171 /* Send the 256 bytes of public key information from the firmware
2172 * as the PKey fragment.
2173 */
Marcel Holtmann09df1232015-07-05 14:55:36 +02002174 err = btintel_secure_send(hdev, 0x03, 256, fw->data + 128);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002175 if (err < 0) {
2176 BT_ERR("%s: Failed to send firmware public key (%d)",
2177 hdev->name, err);
2178 goto done;
2179 }
2180
2181 /* Send the 256 bytes of signature information from the firmware
2182 * as the Sign fragment.
2183 */
Marcel Holtmann09df1232015-07-05 14:55:36 +02002184 err = btintel_secure_send(hdev, 0x02, 256, fw->data + 388);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002185 if (err < 0) {
2186 BT_ERR("%s: Failed to send firmware signature (%d)",
2187 hdev->name, err);
2188 goto done;
2189 }
2190
2191 fw_ptr = fw->data + 644;
Marcel Holtmanne66890a92015-06-07 09:47:08 +02002192 frag_len = 0;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002193
2194 while (fw_ptr - fw->data < fw->size) {
Marcel Holtmanne66890a92015-06-07 09:47:08 +02002195 struct hci_command_hdr *cmd = (void *)(fw_ptr + frag_len);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002196
Marcel Holtmanne66890a92015-06-07 09:47:08 +02002197 frag_len += sizeof(*cmd) + cmd->plen;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002198
Minjune Kim5075eda2015-08-27 13:21:52 +09002199 /* The parameter length of the secure send command requires
Marcel Holtmanne66890a92015-06-07 09:47:08 +02002200 * a 4 byte alignment. It happens so that the firmware file
2201 * contains proper Intel_NOP commands to align the fragments
2202 * as needed.
2203 *
2204 * Send set of commands with 4 byte alignment from the
2205 * firmware data buffer as a single Data fragement.
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002206 */
Marcel Holtmanne66890a92015-06-07 09:47:08 +02002207 if (!(frag_len % 4)) {
Marcel Holtmann09df1232015-07-05 14:55:36 +02002208 err = btintel_secure_send(hdev, 0x01, frag_len, fw_ptr);
Marcel Holtmanne66890a92015-06-07 09:47:08 +02002209 if (err < 0) {
2210 BT_ERR("%s: Failed to send firmware data (%d)",
2211 hdev->name, err);
2212 goto done;
2213 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002214
Marcel Holtmanne66890a92015-06-07 09:47:08 +02002215 fw_ptr += frag_len;
2216 frag_len = 0;
2217 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002218 }
2219
Marcel Holtmannce6bb922015-01-28 01:58:40 -08002220 set_bit(BTUSB_FIRMWARE_LOADED, &data->flags);
2221
Johan Hedberga087a982015-01-30 10:58:54 +02002222 BT_INFO("%s: Waiting for firmware download to complete", hdev->name);
2223
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002224 /* Before switching the device into operational mode and with that
2225 * booting the loaded firmware, wait for the bootloader notification
2226 * that all fragments have been successfully received.
2227 *
Johan Hedberga087a982015-01-30 10:58:54 +02002228 * When the event processing receives the notification, then the
2229 * BTUSB_DOWNLOADING flag will be cleared.
2230 *
2231 * The firmware loading should not take longer than 5 seconds
2232 * and thus just timeout if that happens and fail the setup
2233 * of this device.
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002234 */
Johan Hedberg129a7692015-02-14 09:33:35 +02002235 err = wait_on_bit_timeout(&data->flags, BTUSB_DOWNLOADING,
2236 TASK_INTERRUPTIBLE,
2237 msecs_to_jiffies(5000));
Bart Van Asschef0a70a02016-08-11 16:02:44 -07002238 if (err == -EINTR) {
Johan Hedberga087a982015-01-30 10:58:54 +02002239 BT_ERR("%s: Firmware loading interrupted", hdev->name);
Johan Hedberga087a982015-01-30 10:58:54 +02002240 goto done;
2241 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002242
Johan Hedberga087a982015-01-30 10:58:54 +02002243 if (err) {
2244 BT_ERR("%s: Firmware loading timeout", hdev->name);
2245 err = -ETIMEDOUT;
2246 goto done;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002247 }
2248
2249 if (test_bit(BTUSB_FIRMWARE_FAILED, &data->flags)) {
2250 BT_ERR("%s: Firmware loading failed", hdev->name);
2251 err = -ENOEXEC;
2252 goto done;
2253 }
2254
2255 rettime = ktime_get();
2256 delta = ktime_sub(rettime, calltime);
2257 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
2258
2259 BT_INFO("%s: Firmware loaded in %llu usecs", hdev->name, duration);
2260
2261done:
2262 release_firmware(fw);
2263
2264 if (err < 0)
2265 return err;
2266
2267 calltime = ktime_get();
2268
2269 set_bit(BTUSB_BOOTING, &data->flags);
2270
2271 skb = __hci_cmd_sync(hdev, 0xfc01, sizeof(reset_param), reset_param,
2272 HCI_INIT_TIMEOUT);
2273 if (IS_ERR(skb))
2274 return PTR_ERR(skb);
2275
2276 kfree_skb(skb);
2277
2278 /* The bootloader will not indicate when the device is ready. This
2279 * is done by the operational firmware sending bootup notification.
Johan Hedbergfad70972015-01-30 10:58:55 +02002280 *
2281 * Booting into operational firmware should not take longer than
2282 * 1 second. However if that happens, then just fail the setup
2283 * since something went wrong.
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002284 */
Johan Hedbergfad70972015-01-30 10:58:55 +02002285 BT_INFO("%s: Waiting for device to boot", hdev->name);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002286
Johan Hedberg129a7692015-02-14 09:33:35 +02002287 err = wait_on_bit_timeout(&data->flags, BTUSB_BOOTING,
2288 TASK_INTERRUPTIBLE,
2289 msecs_to_jiffies(1000));
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002290
Bart Van Asschef0a70a02016-08-11 16:02:44 -07002291 if (err == -EINTR) {
Johan Hedbergfad70972015-01-30 10:58:55 +02002292 BT_ERR("%s: Device boot interrupted", hdev->name);
2293 return -EINTR;
2294 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002295
Johan Hedbergfad70972015-01-30 10:58:55 +02002296 if (err) {
2297 BT_ERR("%s: Device boot timeout", hdev->name);
2298 return -ETIMEDOUT;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002299 }
2300
2301 rettime = ktime_get();
2302 delta = ktime_sub(rettime, calltime);
2303 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
2304
2305 BT_INFO("%s: Device booted in %llu usecs", hdev->name, duration);
2306
2307 clear_bit(BTUSB_BOOTLOADER, &data->flags);
2308
Tedd Ho-Jeong An52cc9162015-06-12 16:20:05 -07002309 /* Once the device is running in operational mode, it needs to apply
2310 * the device configuration (DDC) parameters.
2311 *
2312 * The device can work without DDC parameters, so even if it fails
2313 * to load the file, no need to fail the setup.
2314 */
Loic Poulaine924d3d2015-09-04 17:54:36 +02002315 btintel_load_ddc_config(hdev, fwname);
Tedd Ho-Jeong An52cc9162015-06-12 16:20:05 -07002316
Marcel Holtmann213445b2015-10-21 02:45:19 +02002317 /* Set the event mask for Intel specific vendor events. This enables
2318 * a few extra events that are useful during general operation. It
2319 * does not enable any debugging related events.
2320 *
2321 * The device will function correctly without these events enabled
2322 * and thus no need to fail the setup.
2323 */
2324 btintel_set_event_mask(hdev, false);
2325
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002326 return 0;
2327}
2328
Tedd Ho-Jeong Anbfbd45e2015-02-13 09:20:52 -08002329static int btusb_shutdown_intel(struct hci_dev *hdev)
2330{
2331 struct sk_buff *skb;
2332 long ret;
2333
2334 /* Some platforms have an issue with BT LED when the interface is
2335 * down or BT radio is turned off, which takes 5 seconds to BT LED
2336 * goes off. This command turns off the BT LED immediately.
2337 */
2338 skb = __hci_cmd_sync(hdev, 0xfc3f, 0, NULL, HCI_INIT_TIMEOUT);
2339 if (IS_ERR(skb)) {
2340 ret = PTR_ERR(skb);
2341 BT_ERR("%s: turning off Intel device LED failed (%ld)",
2342 hdev->name, ret);
2343 return ret;
2344 }
2345 kfree_skb(skb);
2346
2347 return 0;
2348}
2349
Amitkumar Karwarae8df492014-07-18 14:47:06 -07002350static int btusb_set_bdaddr_marvell(struct hci_dev *hdev,
2351 const bdaddr_t *bdaddr)
2352{
2353 struct sk_buff *skb;
2354 u8 buf[8];
2355 long ret;
2356
2357 buf[0] = 0xfe;
2358 buf[1] = sizeof(bdaddr_t);
2359 memcpy(buf + 2, bdaddr, sizeof(bdaddr_t));
2360
2361 skb = __hci_cmd_sync(hdev, 0xfc22, sizeof(buf), buf, HCI_INIT_TIMEOUT);
2362 if (IS_ERR(skb)) {
2363 ret = PTR_ERR(skb);
2364 BT_ERR("%s: changing Marvell device address failed (%ld)",
2365 hdev->name, ret);
2366 return ret;
2367 }
2368 kfree_skb(skb);
2369
2370 return 0;
2371}
2372
Toshi Kikuchi58592232014-12-12 10:58:05 -08002373static int btusb_set_bdaddr_ath3012(struct hci_dev *hdev,
2374 const bdaddr_t *bdaddr)
2375{
2376 struct sk_buff *skb;
2377 u8 buf[10];
2378 long ret;
2379
2380 buf[0] = 0x01;
2381 buf[1] = 0x01;
2382 buf[2] = 0x00;
2383 buf[3] = sizeof(bdaddr_t);
2384 memcpy(buf + 4, bdaddr, sizeof(bdaddr_t));
2385
2386 skb = __hci_cmd_sync(hdev, 0xfc0b, sizeof(buf), buf, HCI_INIT_TIMEOUT);
2387 if (IS_ERR(skb)) {
2388 ret = PTR_ERR(skb);
2389 BT_ERR("%s: Change address command failed (%ld)",
2390 hdev->name, ret);
2391 return ret;
2392 }
2393 kfree_skb(skb);
2394
2395 return 0;
2396}
2397
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002398#define QCA_DFU_PACKET_LEN 4096
2399
2400#define QCA_GET_TARGET_VERSION 0x09
2401#define QCA_CHECK_STATUS 0x05
2402#define QCA_DFU_DOWNLOAD 0x01
2403
2404#define QCA_SYSCFG_UPDATED 0x40
2405#define QCA_PATCH_UPDATED 0x80
2406#define QCA_DFU_TIMEOUT 3000
2407
2408struct qca_version {
2409 __le32 rom_version;
2410 __le32 patch_version;
2411 __le32 ram_version;
2412 __le32 ref_clock;
2413 __u8 reserved[4];
2414} __packed;
2415
2416struct qca_rampatch_version {
2417 __le16 rom_version;
2418 __le16 patch_version;
2419} __packed;
2420
2421struct qca_device_info {
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002422 u32 rom_version;
2423 u8 rampatch_hdr; /* length of header in rampatch */
2424 u8 nvm_hdr; /* length of header in NVM */
2425 u8 ver_offset; /* offset of version structure in rampatch */
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002426};
2427
2428static const struct qca_device_info qca_devices_table[] = {
2429 { 0x00000100, 20, 4, 10 }, /* Rome 1.0 */
2430 { 0x00000101, 20, 4, 10 }, /* Rome 1.1 */
Chan-yeol Park7f6e6362015-05-21 11:24:27 +09002431 { 0x00000200, 28, 4, 18 }, /* Rome 2.0 */
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002432 { 0x00000201, 28, 4, 18 }, /* Rome 2.1 */
2433 { 0x00000300, 28, 4, 18 }, /* Rome 3.0 */
2434 { 0x00000302, 28, 4, 18 }, /* Rome 3.2 */
2435};
2436
2437static int btusb_qca_send_vendor_req(struct hci_dev *hdev, u8 request,
2438 void *data, u16 size)
2439{
2440 struct btusb_data *btdata = hci_get_drvdata(hdev);
2441 struct usb_device *udev = btdata->udev;
2442 int pipe, err;
2443 u8 *buf;
2444
2445 buf = kmalloc(size, GFP_KERNEL);
2446 if (!buf)
2447 return -ENOMEM;
2448
2449 /* Found some of USB hosts have IOT issues with ours so that we should
2450 * not wait until HCI layer is ready.
2451 */
2452 pipe = usb_rcvctrlpipe(udev, 0);
2453 err = usb_control_msg(udev, pipe, request, USB_TYPE_VENDOR | USB_DIR_IN,
2454 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
2455 if (err < 0) {
2456 BT_ERR("%s: Failed to access otp area (%d)", hdev->name, err);
2457 goto done;
2458 }
2459
2460 memcpy(data, buf, size);
2461
2462done:
2463 kfree(buf);
2464
2465 return err;
2466}
2467
2468static int btusb_setup_qca_download_fw(struct hci_dev *hdev,
2469 const struct firmware *firmware,
2470 size_t hdr_size)
2471{
2472 struct btusb_data *btdata = hci_get_drvdata(hdev);
2473 struct usb_device *udev = btdata->udev;
2474 size_t count, size, sent = 0;
2475 int pipe, len, err;
2476 u8 *buf;
2477
2478 buf = kmalloc(QCA_DFU_PACKET_LEN, GFP_KERNEL);
2479 if (!buf)
2480 return -ENOMEM;
2481
2482 count = firmware->size;
2483
2484 size = min_t(size_t, count, hdr_size);
2485 memcpy(buf, firmware->data, size);
2486
2487 /* USB patches should go down to controller through USB path
2488 * because binary format fits to go down through USB channel.
2489 * USB control path is for patching headers and USB bulk is for
2490 * patch body.
2491 */
2492 pipe = usb_sndctrlpipe(udev, 0);
2493 err = usb_control_msg(udev, pipe, QCA_DFU_DOWNLOAD, USB_TYPE_VENDOR,
2494 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
2495 if (err < 0) {
2496 BT_ERR("%s: Failed to send headers (%d)", hdev->name, err);
2497 goto done;
2498 }
2499
2500 sent += size;
2501 count -= size;
2502
2503 while (count) {
2504 size = min_t(size_t, count, QCA_DFU_PACKET_LEN);
2505
2506 memcpy(buf, firmware->data + sent, size);
2507
2508 pipe = usb_sndbulkpipe(udev, 0x02);
2509 err = usb_bulk_msg(udev, pipe, buf, size, &len,
2510 QCA_DFU_TIMEOUT);
2511 if (err < 0) {
2512 BT_ERR("%s: Failed to send body at %zd of %zd (%d)",
2513 hdev->name, sent, firmware->size, err);
2514 break;
2515 }
2516
2517 if (size != len) {
2518 BT_ERR("%s: Failed to get bulk buffer", hdev->name);
2519 err = -EILSEQ;
2520 break;
2521 }
2522
2523 sent += size;
2524 count -= size;
2525 }
2526
2527done:
2528 kfree(buf);
2529 return err;
2530}
2531
2532static int btusb_setup_qca_load_rampatch(struct hci_dev *hdev,
2533 struct qca_version *ver,
2534 const struct qca_device_info *info)
2535{
2536 struct qca_rampatch_version *rver;
2537 const struct firmware *fw;
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002538 u32 ver_rom, ver_patch;
2539 u16 rver_rom, rver_patch;
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002540 char fwname[64];
2541 int err;
2542
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002543 ver_rom = le32_to_cpu(ver->rom_version);
2544 ver_patch = le32_to_cpu(ver->patch_version);
2545
2546 snprintf(fwname, sizeof(fwname), "qca/rampatch_usb_%08x.bin", ver_rom);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002547
2548 err = request_firmware(&fw, fwname, &hdev->dev);
2549 if (err) {
2550 BT_ERR("%s: failed to request rampatch file: %s (%d)",
2551 hdev->name, fwname, err);
2552 return err;
2553 }
2554
2555 BT_INFO("%s: using rampatch file: %s", hdev->name, fwname);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002556
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002557 rver = (struct qca_rampatch_version *)(fw->data + info->ver_offset);
2558 rver_rom = le16_to_cpu(rver->rom_version);
2559 rver_patch = le16_to_cpu(rver->patch_version);
2560
2561 BT_INFO("%s: QCA: patch rome 0x%x build 0x%x, firmware rome 0x%x "
2562 "build 0x%x", hdev->name, rver_rom, rver_patch, ver_rom,
2563 ver_patch);
2564
2565 if (rver_rom != ver_rom || rver_patch <= ver_patch) {
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002566 BT_ERR("%s: rampatch file version did not match with firmware",
2567 hdev->name);
2568 err = -EINVAL;
2569 goto done;
2570 }
2571
2572 err = btusb_setup_qca_download_fw(hdev, fw, info->rampatch_hdr);
2573
2574done:
2575 release_firmware(fw);
2576
2577 return err;
2578}
2579
2580static int btusb_setup_qca_load_nvm(struct hci_dev *hdev,
2581 struct qca_version *ver,
2582 const struct qca_device_info *info)
2583{
2584 const struct firmware *fw;
2585 char fwname[64];
2586 int err;
2587
2588 snprintf(fwname, sizeof(fwname), "qca/nvm_usb_%08x.bin",
2589 le32_to_cpu(ver->rom_version));
2590
2591 err = request_firmware(&fw, fwname, &hdev->dev);
2592 if (err) {
2593 BT_ERR("%s: failed to request NVM file: %s (%d)",
2594 hdev->name, fwname, err);
2595 return err;
2596 }
2597
2598 BT_INFO("%s: using NVM file: %s", hdev->name, fwname);
2599
2600 err = btusb_setup_qca_download_fw(hdev, fw, info->nvm_hdr);
2601
2602 release_firmware(fw);
2603
2604 return err;
2605}
2606
2607static int btusb_setup_qca(struct hci_dev *hdev)
2608{
2609 const struct qca_device_info *info = NULL;
2610 struct qca_version ver;
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002611 u32 ver_rom;
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002612 u8 status;
2613 int i, err;
2614
2615 err = btusb_qca_send_vendor_req(hdev, QCA_GET_TARGET_VERSION, &ver,
Marcel Holtmanneb500422015-04-16 23:15:50 +02002616 sizeof(ver));
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002617 if (err < 0)
2618 return err;
2619
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002620 ver_rom = le32_to_cpu(ver.rom_version);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002621 for (i = 0; i < ARRAY_SIZE(qca_devices_table); i++) {
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002622 if (ver_rom == qca_devices_table[i].rom_version)
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002623 info = &qca_devices_table[i];
2624 }
2625 if (!info) {
2626 BT_ERR("%s: don't support firmware rome 0x%x", hdev->name,
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002627 ver_rom);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002628 return -ENODEV;
2629 }
2630
2631 err = btusb_qca_send_vendor_req(hdev, QCA_CHECK_STATUS, &status,
2632 sizeof(status));
2633 if (err < 0)
2634 return err;
2635
2636 if (!(status & QCA_PATCH_UPDATED)) {
2637 err = btusb_setup_qca_load_rampatch(hdev, &ver, info);
2638 if (err < 0)
2639 return err;
2640 }
2641
2642 if (!(status & QCA_SYSCFG_UPDATED)) {
2643 err = btusb_setup_qca_load_nvm(hdev, &ver, info);
2644 if (err < 0)
2645 return err;
2646 }
2647
2648 return 0;
2649}
2650
Marcel Holtmann9d08f502015-10-08 20:23:08 +02002651#ifdef CONFIG_BT_HCIBTUSB_BCM
2652static inline int __set_diag_interface(struct hci_dev *hdev)
2653{
2654 struct btusb_data *data = hci_get_drvdata(hdev);
2655 struct usb_interface *intf = data->diag;
2656 int i;
2657
2658 if (!data->diag)
2659 return -ENODEV;
2660
2661 data->diag_tx_ep = NULL;
2662 data->diag_rx_ep = NULL;
2663
2664 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
2665 struct usb_endpoint_descriptor *ep_desc;
2666
2667 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
2668
2669 if (!data->diag_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
2670 data->diag_tx_ep = ep_desc;
2671 continue;
2672 }
2673
2674 if (!data->diag_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
2675 data->diag_rx_ep = ep_desc;
2676 continue;
2677 }
2678 }
2679
2680 if (!data->diag_tx_ep || !data->diag_rx_ep) {
2681 BT_ERR("%s invalid diagnostic descriptors", hdev->name);
2682 return -ENODEV;
2683 }
2684
2685 return 0;
2686}
2687
2688static struct urb *alloc_diag_urb(struct hci_dev *hdev, bool enable)
2689{
2690 struct btusb_data *data = hci_get_drvdata(hdev);
2691 struct sk_buff *skb;
2692 struct urb *urb;
2693 unsigned int pipe;
2694
2695 if (!data->diag_tx_ep)
2696 return ERR_PTR(-ENODEV);
2697
2698 urb = usb_alloc_urb(0, GFP_KERNEL);
2699 if (!urb)
2700 return ERR_PTR(-ENOMEM);
2701
2702 skb = bt_skb_alloc(2, GFP_KERNEL);
2703 if (!skb) {
2704 usb_free_urb(urb);
2705 return ERR_PTR(-ENOMEM);
2706 }
2707
2708 *skb_put(skb, 1) = 0xf0;
2709 *skb_put(skb, 1) = enable;
2710
2711 pipe = usb_sndbulkpipe(data->udev, data->diag_tx_ep->bEndpointAddress);
2712
2713 usb_fill_bulk_urb(urb, data->udev, pipe,
2714 skb->data, skb->len, btusb_tx_complete, skb);
2715
2716 skb->dev = (void *)hdev;
2717
2718 return urb;
2719}
2720
2721static int btusb_bcm_set_diag(struct hci_dev *hdev, bool enable)
2722{
2723 struct btusb_data *data = hci_get_drvdata(hdev);
2724 struct urb *urb;
2725
2726 if (!data->diag)
2727 return -ENODEV;
2728
2729 if (!test_bit(HCI_RUNNING, &hdev->flags))
2730 return -ENETDOWN;
2731
2732 urb = alloc_diag_urb(hdev, enable);
2733 if (IS_ERR(urb))
2734 return PTR_ERR(urb);
2735
2736 return submit_or_queue_tx_urb(hdev, urb);
2737}
2738#endif
2739
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002740static int btusb_probe(struct usb_interface *intf,
Marcel Holtmann89e75332014-09-16 04:44:50 +02002741 const struct usb_device_id *id)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002742{
2743 struct usb_endpoint_descriptor *ep_desc;
2744 struct btusb_data *data;
2745 struct hci_dev *hdev;
Marcel Holtmann22f8e9d2015-10-20 00:53:33 +02002746 unsigned ifnum_base;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002747 int i, err;
2748
2749 BT_DBG("intf %p id %p", intf, id);
2750
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002751 /* interface numbers are hardcoded in the spec */
Marcel Holtmann22f8e9d2015-10-20 00:53:33 +02002752 if (intf->cur_altsetting->desc.bInterfaceNumber != 0) {
2753 if (!(id->driver_info & BTUSB_IFNUM_2))
2754 return -ENODEV;
2755 if (intf->cur_altsetting->desc.bInterfaceNumber != 2)
2756 return -ENODEV;
2757 }
2758
2759 ifnum_base = intf->cur_altsetting->desc.bInterfaceNumber;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002760
2761 if (!id->driver_info) {
2762 const struct usb_device_id *match;
Marcel Holtmann89e75332014-09-16 04:44:50 +02002763
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002764 match = usb_match_id(intf, blacklist_table);
2765 if (match)
2766 id = match;
2767 }
2768
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002769 if (id->driver_info == BTUSB_IGNORE)
2770 return -ENODEV;
2771
Steven.Li2d25f8b2011-07-01 14:02:36 +08002772 if (id->driver_info & BTUSB_ATH3012) {
2773 struct usb_device *udev = interface_to_usbdev(intf);
2774
2775 /* Old firmware would otherwise let ath3k driver load
2776 * patch and sysconfig files */
2777 if (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x0001)
2778 return -ENODEV;
2779 }
2780
Sachin Kamat98921db2012-07-27 12:38:39 +05302781 data = devm_kzalloc(&intf->dev, sizeof(*data), GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002782 if (!data)
2783 return -ENOMEM;
2784
2785 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
2786 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
2787
2788 if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
2789 data->intr_ep = ep_desc;
2790 continue;
2791 }
2792
2793 if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
2794 data->bulk_tx_ep = ep_desc;
2795 continue;
2796 }
2797
2798 if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
2799 data->bulk_rx_ep = ep_desc;
2800 continue;
2801 }
2802 }
2803
Sachin Kamat98921db2012-07-27 12:38:39 +05302804 if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002805 return -ENODEV;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002806
Marcel Holtmann893ba542015-01-28 20:27:34 -08002807 if (id->driver_info & BTUSB_AMP) {
2808 data->cmdreq_type = USB_TYPE_CLASS | 0x01;
2809 data->cmdreq = 0x2b;
2810 } else {
2811 data->cmdreq_type = USB_TYPE_CLASS;
2812 data->cmdreq = 0x00;
2813 }
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002814
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002815 data->udev = interface_to_usbdev(intf);
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02002816 data->intf = intf;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002817
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002818 INIT_WORK(&data->work, btusb_work);
Oliver Neukum7bee5492009-08-24 23:44:59 +02002819 INIT_WORK(&data->waker, btusb_waker);
Marcel Holtmann803b5832014-09-16 08:00:29 +02002820 init_usb_anchor(&data->deferred);
2821 init_usb_anchor(&data->tx_anchor);
Oliver Neukum7bee5492009-08-24 23:44:59 +02002822 spin_lock_init(&data->txlock);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002823
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002824 init_usb_anchor(&data->intr_anchor);
2825 init_usb_anchor(&data->bulk_anchor);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02002826 init_usb_anchor(&data->isoc_anchor);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02002827 init_usb_anchor(&data->diag_anchor);
Marcel Holtmann803b5832014-09-16 08:00:29 +02002828 spin_lock_init(&data->rxlock);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002829
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002830 if (id->driver_info & BTUSB_INTEL_NEW) {
2831 data->recv_event = btusb_recv_event_intel;
2832 data->recv_bulk = btusb_recv_bulk_intel;
2833 set_bit(BTUSB_BOOTLOADER, &data->flags);
2834 } else {
2835 data->recv_event = hci_recv_frame;
2836 data->recv_bulk = btusb_recv_bulk;
2837 }
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +01002838
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002839 hdev = hci_alloc_dev();
Sachin Kamat98921db2012-07-27 12:38:39 +05302840 if (!hdev)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002841 return -ENOMEM;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002842
Marcel Holtmannc13854c2010-02-08 15:27:07 +01002843 hdev->bus = HCI_USB;
David Herrmann155961e2012-02-09 21:58:32 +01002844 hci_set_drvdata(hdev, data);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002845
Marcel Holtmann893ba542015-01-28 20:27:34 -08002846 if (id->driver_info & BTUSB_AMP)
2847 hdev->dev_type = HCI_AMP;
2848 else
Marcel Holtmannca8bee52016-07-05 14:30:14 +02002849 hdev->dev_type = HCI_PRIMARY;
Marcel Holtmann893ba542015-01-28 20:27:34 -08002850
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002851 data->hdev = hdev;
2852
2853 SET_HCIDEV_DEV(hdev, &intf->dev);
2854
Marcel Holtmann9f8f9622013-04-10 08:11:35 -07002855 hdev->open = btusb_open;
2856 hdev->close = btusb_close;
2857 hdev->flush = btusb_flush;
2858 hdev->send = btusb_send_frame;
2859 hdev->notify = btusb_notify;
2860
Szymon Janc418678b2016-09-01 17:22:37 +02002861 if (id->driver_info & BTUSB_CW6622)
2862 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
2863
Marcel Holtmann6c9d4352015-10-17 14:39:27 +02002864 if (id->driver_info & BTUSB_BCM2045)
2865 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
2866
Marcel Holtmann9f8f9622013-04-10 08:11:35 -07002867 if (id->driver_info & BTUSB_BCM92035)
2868 hdev->setup = btusb_setup_bcm92035;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002869
Marcel Holtmannc2bfb102015-04-05 22:52:14 -07002870#ifdef CONFIG_BT_HCIBTUSB_BCM
Marcel Holtmannabbaf502014-07-02 00:53:48 +02002871 if (id->driver_info & BTUSB_BCM_PATCHRAM) {
Marcel Holtmann49a5f782015-10-20 02:30:48 +02002872 hdev->manufacturer = 15;
Marcel Holtmannc2bfb102015-04-05 22:52:14 -07002873 hdev->setup = btbcm_setup_patchram;
Marcel Holtmann9d08f502015-10-08 20:23:08 +02002874 hdev->set_diag = btusb_bcm_set_diag;
Marcel Holtmann1df1f592015-04-05 22:52:11 -07002875 hdev->set_bdaddr = btbcm_set_bdaddr;
Marcel Holtmann9d08f502015-10-08 20:23:08 +02002876
2877 /* Broadcom LM_DIAG Interface numbers are hardcoded */
Marcel Holtmann22f8e9d2015-10-20 00:53:33 +02002878 data->diag = usb_ifnum_to_if(data->udev, ifnum_base + 2);
Marcel Holtmannabbaf502014-07-02 00:53:48 +02002879 }
Petri Gynther10d4c672014-05-08 15:50:01 -07002880
Marcel Holtmann9d08f502015-10-08 20:23:08 +02002881 if (id->driver_info & BTUSB_BCM_APPLE) {
Marcel Holtmann49a5f782015-10-20 02:30:48 +02002882 hdev->manufacturer = 15;
Marcel Holtmannc2bfb102015-04-05 22:52:14 -07002883 hdev->setup = btbcm_setup_apple;
Marcel Holtmann9d08f502015-10-08 20:23:08 +02002884 hdev->set_diag = btusb_bcm_set_diag;
2885
2886 /* Broadcom LM_DIAG Interface numbers are hardcoded */
Marcel Holtmann22f8e9d2015-10-20 00:53:33 +02002887 data->diag = usb_ifnum_to_if(data->udev, ifnum_base + 2);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02002888 }
Marcel Holtmannc2bfb102015-04-05 22:52:14 -07002889#endif
Marcel Holtmann17b27722015-03-22 15:52:38 +01002890
Marcel Holtmanncb8d6592014-07-02 11:25:25 +02002891 if (id->driver_info & BTUSB_INTEL) {
Marcel Holtmann49a5f782015-10-20 02:30:48 +02002892 hdev->manufacturer = 2;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002893 hdev->setup = btusb_setup_intel;
Tedd Ho-Jeong Anbfbd45e2015-02-13 09:20:52 -08002894 hdev->shutdown = btusb_shutdown_intel;
Marcel Holtmann3e247672015-10-17 16:00:28 +02002895 hdev->set_diag = btintel_set_diag_mfg;
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07002896 hdev->set_bdaddr = btintel_set_bdaddr;
Jakub Pawlowskic33fb9b2015-01-30 18:55:58 -08002897 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
Jakub Pawlowskic1154842015-03-17 09:04:16 -07002898 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
Marcel Holtmann3e247672015-10-17 16:00:28 +02002899 set_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks);
Marcel Holtmanncb8d6592014-07-02 11:25:25 +02002900 }
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002901
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002902 if (id->driver_info & BTUSB_INTEL_NEW) {
Marcel Holtmann49a5f782015-10-20 02:30:48 +02002903 hdev->manufacturer = 2;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002904 hdev->send = btusb_send_frame_intel;
2905 hdev->setup = btusb_setup_intel_new;
Marcel Holtmanneeb6abe2015-07-05 14:37:39 +02002906 hdev->hw_error = btintel_hw_error;
Marcel Holtmann6d2e50d2015-10-09 14:42:08 +02002907 hdev->set_diag = btintel_set_diag;
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07002908 hdev->set_bdaddr = btintel_set_bdaddr;
Marcel Holtmannb970c5b2015-02-01 23:57:18 -08002909 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
Marcel Holtmannd8270fb2015-10-17 16:00:27 +02002910 set_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002911 }
2912
Amitkumar Karwarae8df492014-07-18 14:47:06 -07002913 if (id->driver_info & BTUSB_MARVELL)
2914 hdev->set_bdaddr = btusb_set_bdaddr_marvell;
2915
Marcel Holtmann661cf882015-01-02 23:35:20 -08002916 if (id->driver_info & BTUSB_SWAVE) {
2917 set_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks);
Marcel Holtmannd57dbe72014-12-26 04:42:33 +01002918 set_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS, &hdev->quirks);
Marcel Holtmann661cf882015-01-02 23:35:20 -08002919 }
Marcel Holtmannd57dbe72014-12-26 04:42:33 +01002920
Marcel Holtmanne4c534b2015-10-21 01:31:45 +02002921 if (id->driver_info & BTUSB_INTEL_BOOT) {
2922 hdev->manufacturer = 2;
Marcel Holtmann40df7832014-07-06 13:29:58 +02002923 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
Marcel Holtmanne4c534b2015-10-21 01:31:45 +02002924 }
Marcel Holtmann40df7832014-07-06 13:29:58 +02002925
Jakub Pawlowski79f0c872015-01-29 10:38:34 -08002926 if (id->driver_info & BTUSB_ATH3012) {
Toshi Kikuchi58592232014-12-12 10:58:05 -08002927 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
Jakub Pawlowski3d50d512015-03-17 09:04:15 -07002928 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
Jakub Pawlowski79f0c872015-01-29 10:38:34 -08002929 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
2930 }
Toshi Kikuchi58592232014-12-12 10:58:05 -08002931
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002932 if (id->driver_info & BTUSB_QCA_ROME) {
2933 data->setup_on_usb = btusb_setup_qca;
2934 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
Hans de Goede5795b072018-01-08 10:44:16 +01002935
2936 /* QCA Rome devices lose their updated firmware over suspend,
2937 * but the USB hub doesn't notice any status change.
2938 * explicitly request a device reset on resume.
2939 */
2940 interface_to_usbdev(intf)->quirks |= USB_QUIRK_RESET_RESUME;
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002941 }
2942
Carlo Caionedb33c772015-05-14 10:49:09 +02002943#ifdef CONFIG_BT_HCIBTUSB_RTL
Daniel Drake04b8c812015-05-21 08:23:50 -06002944 if (id->driver_info & BTUSB_REALTEK) {
Carlo Caionedb33c772015-05-14 10:49:09 +02002945 hdev->setup = btrtl_setup_realtek;
Daniel Drake04b8c812015-05-21 08:23:50 -06002946
2947 /* Realtek devices lose their updated firmware over suspend,
2948 * but the USB hub doesn't notice any status change.
2949 * Explicitly request a device reset on resume.
2950 */
Hans de Goede5795b072018-01-08 10:44:16 +01002951 interface_to_usbdev(intf)->quirks |= USB_QUIRK_RESET_RESUME;
Daniel Drake04b8c812015-05-21 08:23:50 -06002952 }
Carlo Caionedb33c772015-05-14 10:49:09 +02002953#endif
Daniel Drakea2698a92015-04-16 14:09:55 -06002954
Marcel Holtmann893ba542015-01-28 20:27:34 -08002955 if (id->driver_info & BTUSB_AMP) {
2956 /* AMP controllers do not support SCO packets */
2957 data->isoc = NULL;
2958 } else {
Marcel Holtmann22f8e9d2015-10-20 00:53:33 +02002959 /* Interface orders are hardcoded in the specification */
2960 data->isoc = usb_ifnum_to_if(data->udev, ifnum_base + 1);
Marcel Holtmann893ba542015-01-28 20:27:34 -08002961 }
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02002962
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002963 if (!reset)
Szymon Janca6c511c2012-05-23 12:35:46 +02002964 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002965
2966 if (force_scofix || id->driver_info & BTUSB_WRONG_SCO_MTU) {
2967 if (!disable_scofix)
2968 set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
2969 }
2970
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02002971 if (id->driver_info & BTUSB_BROKEN_ISOC)
2972 data->isoc = NULL;
2973
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002974 if (id->driver_info & BTUSB_DIGIANSWER) {
2975 data->cmdreq_type = USB_TYPE_VENDOR;
Szymon Janca6c511c2012-05-23 12:35:46 +02002976 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002977 }
2978
2979 if (id->driver_info & BTUSB_CSR) {
2980 struct usb_device *udev = data->udev;
Marcel Holtmann81cac642014-01-03 03:02:36 -08002981 u16 bcdDevice = le16_to_cpu(udev->descriptor.bcdDevice);
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002982
2983 /* Old firmware would otherwise execute USB reset */
Marcel Holtmann81cac642014-01-03 03:02:36 -08002984 if (bcdDevice < 0x117)
Szymon Janca6c511c2012-05-23 12:35:46 +02002985 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmann81cac642014-01-03 03:02:36 -08002986
2987 /* Fake CSR devices with broken commands */
Johan Hedberg6cafcd92015-08-30 21:47:21 +03002988 if (bcdDevice <= 0x100 || bcdDevice == 0x134)
Marcel Holtmann81cac642014-01-03 03:02:36 -08002989 hdev->setup = btusb_setup_csr;
Jakub Pawlowski49c989a2015-03-17 09:04:17 -07002990
2991 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002992 }
2993
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002994 if (id->driver_info & BTUSB_SNIFFER) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02002995 struct usb_device *udev = data->udev;
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002996
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002997 /* New sniffer firmware has crippled HCI interface */
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002998 if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
2999 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
3000 }
3001
Marcel Holtmann3a5ef202014-07-06 14:53:54 +02003002 if (id->driver_info & BTUSB_INTEL_BOOT) {
3003 /* A bug in the bootloader causes that interrupt interface is
3004 * only enabled after receiving SetInterface(0, AltSetting=0).
3005 */
3006 err = usb_set_interface(data->udev, 0, 0);
3007 if (err < 0) {
3008 BT_ERR("failed to set interface 0, alt 0 %d", err);
3009 hci_free_dev(hdev);
3010 return err;
3011 }
3012 }
3013
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02003014 if (data->isoc) {
3015 err = usb_driver_claim_interface(&btusb_driver,
Marcel Holtmann89e75332014-09-16 04:44:50 +02003016 data->isoc, data);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02003017 if (err < 0) {
3018 hci_free_dev(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02003019 return err;
3020 }
3021 }
3022
Marcel Holtmann9d08f502015-10-08 20:23:08 +02003023#ifdef CONFIG_BT_HCIBTUSB_BCM
3024 if (data->diag) {
3025 if (!usb_driver_claim_interface(&btusb_driver,
3026 data->diag, data))
3027 __set_diag_interface(hdev);
3028 else
3029 data->diag = NULL;
3030 }
3031#endif
3032
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003033 err = hci_register_dev(hdev);
3034 if (err < 0) {
3035 hci_free_dev(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003036 return err;
3037 }
3038
3039 usb_set_intfdata(intf, data);
3040
3041 return 0;
3042}
3043
3044static void btusb_disconnect(struct usb_interface *intf)
3045{
3046 struct btusb_data *data = usb_get_intfdata(intf);
3047 struct hci_dev *hdev;
3048
3049 BT_DBG("intf %p", intf);
3050
3051 if (!data)
3052 return;
3053
3054 hdev = data->hdev;
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02003055 usb_set_intfdata(data->intf, NULL);
3056
3057 if (data->isoc)
3058 usb_set_intfdata(data->isoc, NULL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003059
Marcel Holtmann9d08f502015-10-08 20:23:08 +02003060 if (data->diag)
3061 usb_set_intfdata(data->diag, NULL);
3062
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003063 hci_unregister_dev(hdev);
3064
Marcel Holtmann9d08f502015-10-08 20:23:08 +02003065 if (intf == data->intf) {
3066 if (data->isoc)
3067 usb_driver_release_interface(&btusb_driver, data->isoc);
3068 if (data->diag)
3069 usb_driver_release_interface(&btusb_driver, data->diag);
3070 } else if (intf == data->isoc) {
3071 if (data->diag)
3072 usb_driver_release_interface(&btusb_driver, data->diag);
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02003073 usb_driver_release_interface(&btusb_driver, data->intf);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02003074 } else if (intf == data->diag) {
3075 usb_driver_release_interface(&btusb_driver, data->intf);
3076 if (data->isoc)
3077 usb_driver_release_interface(&btusb_driver, data->isoc);
3078 }
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02003079
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003080 hci_free_dev(hdev);
3081}
3082
Oliver Neukum7bee5492009-08-24 23:44:59 +02003083#ifdef CONFIG_PM
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003084static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
3085{
3086 struct btusb_data *data = usb_get_intfdata(intf);
3087
3088 BT_DBG("intf %p", intf);
3089
3090 if (data->suspend_count++)
3091 return 0;
3092
Oliver Neukum7bee5492009-08-24 23:44:59 +02003093 spin_lock_irq(&data->txlock);
Alan Stern5b1b0b82011-08-19 23:49:48 +02003094 if (!(PMSG_IS_AUTO(message) && data->tx_in_flight)) {
Oliver Neukum7bee5492009-08-24 23:44:59 +02003095 set_bit(BTUSB_SUSPENDING, &data->flags);
3096 spin_unlock_irq(&data->txlock);
3097 } else {
3098 spin_unlock_irq(&data->txlock);
3099 data->suspend_count--;
3100 return -EBUSY;
3101 }
3102
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003103 cancel_work_sync(&data->work);
3104
Oliver Neukum7bee5492009-08-24 23:44:59 +02003105 btusb_stop_traffic(data);
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003106 usb_kill_anchored_urbs(&data->tx_anchor);
3107
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003108 return 0;
3109}
3110
Oliver Neukum7bee5492009-08-24 23:44:59 +02003111static void play_deferred(struct btusb_data *data)
3112{
3113 struct urb *urb;
3114 int err;
3115
3116 while ((urb = usb_get_from_anchor(&data->deferred))) {
3117 err = usb_submit_urb(urb, GFP_ATOMIC);
3118 if (err < 0)
3119 break;
3120
3121 data->tx_in_flight++;
3122 }
3123 usb_scuttle_anchored_urbs(&data->deferred);
3124}
3125
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003126static int btusb_resume(struct usb_interface *intf)
3127{
3128 struct btusb_data *data = usb_get_intfdata(intf);
3129 struct hci_dev *hdev = data->hdev;
Oliver Neukum7bee5492009-08-24 23:44:59 +02003130 int err = 0;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003131
3132 BT_DBG("intf %p", intf);
3133
3134 if (--data->suspend_count)
3135 return 0;
3136
3137 if (!test_bit(HCI_RUNNING, &hdev->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +02003138 goto done;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003139
3140 if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
3141 err = btusb_submit_intr_urb(hdev, GFP_NOIO);
3142 if (err < 0) {
3143 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02003144 goto failed;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003145 }
3146 }
3147
3148 if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) {
Marcel Holtmann43c2e572009-02-04 17:41:38 +01003149 err = btusb_submit_bulk_urb(hdev, GFP_NOIO);
3150 if (err < 0) {
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003151 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02003152 goto failed;
3153 }
3154
3155 btusb_submit_bulk_urb(hdev, GFP_NOIO);
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003156 }
3157
3158 if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
3159 if (btusb_submit_isoc_urb(hdev, GFP_NOIO) < 0)
3160 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
3161 else
3162 btusb_submit_isoc_urb(hdev, GFP_NOIO);
3163 }
3164
Oliver Neukum7bee5492009-08-24 23:44:59 +02003165 spin_lock_irq(&data->txlock);
3166 play_deferred(data);
3167 clear_bit(BTUSB_SUSPENDING, &data->flags);
3168 spin_unlock_irq(&data->txlock);
3169 schedule_work(&data->work);
3170
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003171 return 0;
Oliver Neukum7bee5492009-08-24 23:44:59 +02003172
3173failed:
3174 usb_scuttle_anchored_urbs(&data->deferred);
3175done:
3176 spin_lock_irq(&data->txlock);
3177 clear_bit(BTUSB_SUSPENDING, &data->flags);
3178 spin_unlock_irq(&data->txlock);
3179
3180 return err;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003181}
Oliver Neukum7bee5492009-08-24 23:44:59 +02003182#endif
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003183
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003184static struct usb_driver btusb_driver = {
3185 .name = "btusb",
3186 .probe = btusb_probe,
3187 .disconnect = btusb_disconnect,
Oliver Neukum7bee5492009-08-24 23:44:59 +02003188#ifdef CONFIG_PM
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003189 .suspend = btusb_suspend,
3190 .resume = btusb_resume,
Oliver Neukum7bee5492009-08-24 23:44:59 +02003191#endif
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003192 .id_table = btusb_table,
Oliver Neukum7bee5492009-08-24 23:44:59 +02003193 .supports_autosuspend = 1,
Sarah Sharpe1f12eb2012-04-23 10:08:51 -07003194 .disable_hub_initiated_lpm = 1,
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003195};
3196
Greg Kroah-Hartman93f15082011-11-18 09:47:34 -08003197module_usb_driver(btusb_driver);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003198
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02003199module_param(disable_scofix, bool, 0644);
3200MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
3201
3202module_param(force_scofix, bool, 0644);
3203MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
3204
3205module_param(reset, bool, 0644);
3206MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
3207
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003208MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
3209MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
3210MODULE_VERSION(VERSION);
3211MODULE_LICENSE("GPL");