blob: ce579d811ae41d1a36ecf2b198ad84c222027903 [file] [log] [blame]
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001/*
2 *
3 * Generic Bluetooth USB driver
4 *
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02005 * Copyright (C) 2005-2008 Marcel Holtmann <marcel@holtmann.org>
Marcel Holtmann5e23b922007-10-20 14:12:34 +02006 *
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 */
23
Marcel Holtmann5e23b922007-10-20 14:12:34 +020024#include <linux/module.h>
Marcel Holtmann5e23b922007-10-20 14:12:34 +020025#include <linux/usb.h>
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -070026#include <linux/firmware.h>
Daniel Drakea2698a92015-04-16 14:09:55 -060027#include <asm/unaligned.h>
Marcel Holtmann5e23b922007-10-20 14:12:34 +020028
29#include <net/bluetooth/bluetooth.h>
30#include <net/bluetooth/hci_core.h>
31
Marcel Holtmann4185a0f2015-04-06 00:52:12 -070032#include "btintel.h"
Marcel Holtmann1df1f592015-04-05 22:52:11 -070033#include "btbcm.h"
Carlo Caionedb33c772015-05-14 10:49:09 +020034#include "btrtl.h"
Marcel Holtmann1df1f592015-04-05 22:52:11 -070035
Marcel Holtmann34dced9b2015-04-05 22:52:16 -070036#define VERSION "0.8"
Marcel Holtmanncfeb4142008-08-07 22:26:56 +020037
Rusty Russell90ab5ee2012-01-13 09:32:20 +103038static bool disable_scofix;
39static bool force_scofix;
Marcel Holtmann7a9d4022008-11-30 12:17:26 +010040
Shailendra Verma917a3332015-05-25 23:53:40 +053041static bool reset = true;
Marcel Holtmanncfeb4142008-08-07 22:26:56 +020042
43static struct usb_driver btusb_driver;
44
45#define BTUSB_IGNORE 0x01
Marcel Holtmann7a9d4022008-11-30 12:17:26 +010046#define BTUSB_DIGIANSWER 0x02
47#define BTUSB_CSR 0x04
48#define BTUSB_SNIFFER 0x08
49#define BTUSB_BCM92035 0x10
50#define BTUSB_BROKEN_ISOC 0x20
51#define BTUSB_WRONG_SCO_MTU 0x40
Steven.Li2d25f8b2011-07-01 14:02:36 +080052#define BTUSB_ATH3012 0x80
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -070053#define BTUSB_INTEL 0x100
Marcel Holtmann40df7832014-07-06 13:29:58 +020054#define BTUSB_INTEL_BOOT 0x200
55#define BTUSB_BCM_PATCHRAM 0x400
Amitkumar Karwarae8df492014-07-18 14:47:06 -070056#define BTUSB_MARVELL 0x800
Marcel Holtmann4fcef8e2015-01-01 17:34:37 -080057#define BTUSB_SWAVE 0x1000
Marcel Holtmanncda0dd72015-01-26 21:33:48 -080058#define BTUSB_INTEL_NEW 0x2000
Marcel Holtmann893ba542015-01-28 20:27:34 -080059#define BTUSB_AMP 0x4000
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +000060#define BTUSB_QCA_ROME 0x8000
Marcel Holtmann17b27722015-03-22 15:52:38 +010061#define BTUSB_BCM_APPLE 0x10000
Daniel Drakea2698a92015-04-16 14:09:55 -060062#define BTUSB_REALTEK 0x20000
Marcel Holtmann6c9d4352015-10-17 14:39:27 +020063#define BTUSB_BCM2045 0x40000
Marcel Holtmann5e23b922007-10-20 14:12:34 +020064
Marcel Holtmann54265202013-10-11 07:46:18 -070065static const struct usb_device_id btusb_table[] = {
Marcel Holtmann5e23b922007-10-20 14:12:34 +020066 /* Generic Bluetooth USB device */
67 { USB_DEVICE_INFO(0xe0, 0x01, 0x01) },
68
Marcel Holtmann893ba542015-01-28 20:27:34 -080069 /* Generic Bluetooth AMP device */
70 { USB_DEVICE_INFO(0xe0, 0x01, 0x04), .driver_info = BTUSB_AMP },
71
Daniel Draked63b2822015-07-17 11:12:25 -060072 /* Generic Bluetooth USB interface */
73 { USB_INTERFACE_INFO(0xe0, 0x01, 0x01) },
74
Henrik Rydberg1fa65352012-08-25 19:28:06 +020075 /* Apple-specific (Broadcom) devices */
Marcel Holtmann17b27722015-03-22 15:52:38 +010076 { USB_VENDOR_AND_INTERFACE_INFO(0x05ac, 0xff, 0x01, 0x01),
77 .driver_info = BTUSB_BCM_APPLE },
Henrik Rydberg1fa65352012-08-25 19:28:06 +020078
Cho, Yu-Chen178c0592013-06-04 21:40:26 +080079 /* MediaTek MT76x0E */
80 { USB_DEVICE(0x0e8d, 0x763f) },
81
Oliver Neukumc510eae2011-09-21 11:41:45 +020082 /* Broadcom SoftSailing reporting vendor specific */
Don Zickus2e8b50632012-03-28 16:41:11 -040083 { USB_DEVICE(0x0a5c, 0x21e1) },
Oliver Neukumc510eae2011-09-21 11:41:45 +020084
Nobuhiro Iwamatsu3cd01972010-08-20 16:24:07 +090085 /* Apple MacBookPro 7,1 */
86 { USB_DEVICE(0x05ac, 0x8213) },
87
Cyril Lacoux0a79f672010-07-14 10:29:27 +040088 /* Apple iMac11,1 */
89 { USB_DEVICE(0x05ac, 0x8215) },
90
Nobuhiro Iwamatsu9c047152010-08-20 16:24:06 +090091 /* Apple MacBookPro6,2 */
92 { USB_DEVICE(0x05ac, 0x8218) },
93
Edgar (gimli) Hucek3e3ede72010-11-04 08:04:33 +010094 /* Apple MacBookAir3,1, MacBookAir3,2 */
95 { USB_DEVICE(0x05ac, 0x821b) },
96
Pieter-Augustijn Van Malleghema63b7232011-09-07 02:28:10 -040097 /* Apple MacBookAir4,1 */
98 { USB_DEVICE(0x05ac, 0x821f) },
99
Marc-Antoine Perennou88d377b2011-03-24 14:51:21 -0300100 /* Apple MacBookPro8,2 */
101 { USB_DEVICE(0x05ac, 0x821a) },
102
Jurgen Kramerf78b68262011-09-04 18:01:42 +0200103 /* Apple MacMini5,1 */
104 { USB_DEVICE(0x05ac, 0x8281) },
105
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200106 /* AVM BlueFRITZ! USB v2.0 */
Marcel Holtmann4fcef8e2015-01-01 17:34:37 -0800107 { USB_DEVICE(0x057c, 0x3800), .driver_info = BTUSB_SWAVE },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200108
109 /* Bluetooth Ultraport Module from IBM */
110 { USB_DEVICE(0x04bf, 0x030a) },
111
112 /* ALPS Modules with non-standard id */
113 { USB_DEVICE(0x044e, 0x3001) },
114 { USB_DEVICE(0x044e, 0x3002) },
115
116 /* Ericsson with non-standard id */
117 { USB_DEVICE(0x0bdb, 0x1002) },
118
119 /* Canyon CN-BTU1 with HID interfaces */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100120 { USB_DEVICE(0x0c10, 0x0000) },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200121
Wen-chien Jesse Sungd13431c2011-11-08 14:30:22 +0800122 /* Broadcom BCM20702A0 */
123 { USB_DEVICE(0x413c, 0x8197) },
124
Marcel Holtmannd049f4e2015-01-26 20:35:32 -0800125 /* Broadcom BCM20702B0 (Dynex/Insignia) */
126 { USB_DEVICE(0x19ff, 0x0239), .driver_info = BTUSB_BCM_PATCHRAM },
127
Steven Harms98514032012-04-13 14:45:55 -0400128 /* Foxconn - Hon Hai */
Heinrich Siebmanns6029ddc2014-12-03 19:32:22 +0100129 { USB_VENDOR_AND_INTERFACE_INFO(0x0489, 0xff, 0x01, 0x01),
130 .driver_info = BTUSB_BCM_PATCHRAM },
Steven Harms98514032012-04-13 14:45:55 -0400131
Matej Dubovy8f0c3042015-02-02 18:50:14 +0100132 /* Lite-On Technology - Broadcom based */
133 { USB_VENDOR_AND_INTERFACE_INFO(0x04ca, 0xff, 0x01, 0x01),
134 .driver_info = BTUSB_BCM_PATCHRAM },
135
Andy Shevchenko0b880062014-02-18 18:26:19 +0200136 /* Broadcom devices with vendor specific id */
Petri Gynther10d4c672014-05-08 15:50:01 -0700137 { USB_VENDOR_AND_INTERFACE_INFO(0x0a5c, 0xff, 0x01, 0x01),
138 .driver_info = BTUSB_BCM_PATCHRAM },
Gustavo Padovan92c385f2012-08-06 15:36:49 -0300139
Marcel Holtmannc2aef6e2014-07-21 14:02:33 +0200140 /* ASUSTek Computer - Broadcom based */
Rick Dunn9a5abdaa2015-01-17 05:29:12 +0100141 { USB_VENDOR_AND_INTERFACE_INFO(0x0b05, 0xff, 0x01, 0x01),
142 .driver_info = BTUSB_BCM_PATCHRAM },
Marcel Holtmannc2aef6e2014-07-21 14:02:33 +0200143
Ken O'Brien5bcecf32013-09-21 19:14:43 +0100144 /* Belkin F8065bf - Broadcom based */
Marcel Holtmann6331c682015-03-27 15:11:41 -0700145 { USB_VENDOR_AND_INTERFACE_INFO(0x050d, 0xff, 0x01, 0x01),
146 .driver_info = BTUSB_BCM_PATCHRAM },
Ken O'Brien5bcecf32013-09-21 19:14:43 +0100147
Jurgen Kramer9113bfd2014-02-15 12:01:09 +0100148 /* IMC Networks - Broadcom based */
Marcel Holtmann6331c682015-03-27 15:11:41 -0700149 { USB_VENDOR_AND_INTERFACE_INFO(0x13d3, 0xff, 0x01, 0x01),
150 .driver_info = BTUSB_BCM_PATCHRAM },
Jurgen Kramer9113bfd2014-02-15 12:01:09 +0100151
Marcel Holtmann40df7832014-07-06 13:29:58 +0200152 /* Intel Bluetooth USB Bootloader (RAM module) */
Marcel Holtmannd92f2df2014-07-06 14:53:55 +0200153 { USB_DEVICE(0x8087, 0x0a5a),
154 .driver_info = BTUSB_INTEL_BOOT | BTUSB_BROKEN_ISOC },
Marcel Holtmann40df7832014-07-06 13:29:58 +0200155
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200156 { } /* Terminating entry */
157};
158
159MODULE_DEVICE_TABLE(usb, btusb_table);
160
Marcel Holtmann54265202013-10-11 07:46:18 -0700161static const struct usb_device_id blacklist_table[] = {
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200162 /* CSR BlueCore devices */
163 { USB_DEVICE(0x0a12, 0x0001), .driver_info = BTUSB_CSR },
164
165 /* Broadcom BCM2033 without firmware */
166 { USB_DEVICE(0x0a5c, 0x2033), .driver_info = BTUSB_IGNORE },
167
Marcel Holtmann6c9d4352015-10-17 14:39:27 +0200168 /* Broadcom BCM2045 devices */
169 { USB_DEVICE(0x0a5c, 0x2045), .driver_info = BTUSB_BCM2045 },
170
Bala Shanmugambe931122010-11-26 17:35:46 +0530171 /* Atheros 3011 with sflash firmware */
Andy Shevchenko0b880062014-02-18 18:26:19 +0200172 { USB_DEVICE(0x0489, 0xe027), .driver_info = BTUSB_IGNORE },
173 { USB_DEVICE(0x0489, 0xe03d), .driver_info = BTUSB_IGNORE },
Alexander Ploumistos2eeff0b2015-02-13 21:05:11 +0200174 { USB_DEVICE(0x04f2, 0xaff1), .driver_info = BTUSB_IGNORE },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200175 { USB_DEVICE(0x0930, 0x0215), .driver_info = BTUSB_IGNORE },
Bala Shanmugambe931122010-11-26 17:35:46 +0530176 { USB_DEVICE(0x0cf3, 0x3002), .driver_info = BTUSB_IGNORE },
Marek Vasut6eda5412012-06-08 14:32:50 +0200177 { USB_DEVICE(0x0cf3, 0xe019), .driver_info = BTUSB_IGNORE },
Andy Ross2a7bccc2011-05-09 16:11:16 -0700178 { USB_DEVICE(0x13d3, 0x3304), .driver_info = BTUSB_IGNORE },
Bala Shanmugambe931122010-11-26 17:35:46 +0530179
Cho, Yu-Chen509e7862011-01-26 17:10:59 +0800180 /* Atheros AR9285 Malbec with sflash firmware */
181 { USB_DEVICE(0x03f0, 0x311d), .driver_info = BTUSB_IGNORE },
182
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530183 /* Atheros 3012 with sflash firmware */
Andy Shevchenko0b880062014-02-18 18:26:19 +0200184 { USB_DEVICE(0x0489, 0xe04d), .driver_info = BTUSB_ATH3012 },
185 { USB_DEVICE(0x0489, 0xe04e), .driver_info = BTUSB_ATH3012 },
186 { USB_DEVICE(0x0489, 0xe056), .driver_info = BTUSB_ATH3012 },
187 { USB_DEVICE(0x0489, 0xe057), .driver_info = BTUSB_ATH3012 },
188 { USB_DEVICE(0x0489, 0xe05f), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin692c0622015-06-06 20:25:40 +0300189 { USB_DEVICE(0x0489, 0xe076), .driver_info = BTUSB_ATH3012 },
Anantha Krishnan4b552bc2014-10-06 16:31:49 +0530190 { USB_DEVICE(0x0489, 0xe078), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200191 { USB_DEVICE(0x04c5, 0x1330), .driver_info = BTUSB_ATH3012 },
192 { USB_DEVICE(0x04ca, 0x3004), .driver_info = BTUSB_ATH3012 },
193 { USB_DEVICE(0x04ca, 0x3005), .driver_info = BTUSB_ATH3012 },
194 { USB_DEVICE(0x04ca, 0x3006), .driver_info = BTUSB_ATH3012 },
Mohammed Habibulla1fb4e092014-04-17 11:37:13 -0700195 { USB_DEVICE(0x04ca, 0x3007), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200196 { USB_DEVICE(0x04ca, 0x3008), .driver_info = BTUSB_ATH3012 },
197 { USB_DEVICE(0x04ca, 0x300b), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin7e730c7f2015-06-18 20:41:51 +0300198 { USB_DEVICE(0x04ca, 0x300d), .driver_info = BTUSB_ATH3012 },
Dmitry Tuninec0810d2015-05-02 13:36:58 +0300199 { USB_DEVICE(0x04ca, 0x300f), .driver_info = BTUSB_ATH3012 },
Janne Heikkinen134d3b32014-12-09 07:44:51 +0200200 { USB_DEVICE(0x04ca, 0x3010), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200201 { USB_DEVICE(0x0930, 0x0219), .driver_info = BTUSB_ATH3012 },
202 { USB_DEVICE(0x0930, 0x0220), .driver_info = BTUSB_ATH3012 },
Vincent Zwanenburg89d29752014-08-08 12:33:56 +0100203 { USB_DEVICE(0x0930, 0x0227), .driver_info = BTUSB_ATH3012 },
Andy Shevchenkoa735f9e2014-02-18 18:26:20 +0200204 { USB_DEVICE(0x0b05, 0x17d0), .driver_info = BTUSB_ATH3012 },
Ming Leid66629c2013-03-15 11:00:39 +0800205 { USB_DEVICE(0x0cf3, 0x0036), .driver_info = BTUSB_ATH3012 },
Steven.Li2d25f8b2011-07-01 14:02:36 +0800206 { USB_DEVICE(0x0cf3, 0x3004), .driver_info = BTUSB_ATH3012 },
Sunguk Lee94a32d102013-03-12 04:41:58 +0900207 { USB_DEVICE(0x0cf3, 0x3008), .driver_info = BTUSB_ATH3012 },
Cho, Yu-Chen07c0ea82012-03-14 22:01:21 +0200208 { USB_DEVICE(0x0cf3, 0x311d), .driver_info = BTUSB_ATH3012 },
Oliver Neukumb1312372014-01-16 15:37:11 +0100209 { USB_DEVICE(0x0cf3, 0x311e), .driver_info = BTUSB_ATH3012 },
Oliver Neukum1e56f1e2014-01-16 16:02:58 +0100210 { USB_DEVICE(0x0cf3, 0x311f), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200211 { USB_DEVICE(0x0cf3, 0x3121), .driver_info = BTUSB_ATH3012 },
Ming Leiebaf5792013-03-18 23:45:11 +0800212 { USB_DEVICE(0x0cf3, 0x817a), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200213 { USB_DEVICE(0x0cf3, 0xe003), .driver_info = BTUSB_ATH3012 },
AceLan Kaoac713112012-04-19 14:53:45 +0800214 { USB_DEVICE(0x0cf3, 0xe004), .driver_info = BTUSB_ATH3012 },
Peng Chen0a3658c2013-08-30 17:41:40 +0800215 { USB_DEVICE(0x0cf3, 0xe005), .driver_info = BTUSB_ATH3012 },
Wen-chien Jesse Sungca79f232015-05-13 11:39:24 +0800216 { USB_DEVICE(0x0cf3, 0xe006), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200217 { USB_DEVICE(0x13d3, 0x3362), .driver_info = BTUSB_ATH3012 },
218 { USB_DEVICE(0x13d3, 0x3375), .driver_info = BTUSB_ATH3012 },
AceLan Kaoeed307e2012-12-11 11:41:20 +0800219 { USB_DEVICE(0x13d3, 0x3393), .driver_info = BTUSB_ATH3012 },
Sujith Manoharan5b77a1f2013-07-15 09:29:03 +0530220 { USB_DEVICE(0x13d3, 0x3402), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin3bb30a7c2014-11-25 20:19:52 +0300221 { USB_DEVICE(0x13d3, 0x3408), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin033efa92015-01-18 00:16:51 +0300222 { USB_DEVICE(0x13d3, 0x3423), .driver_info = BTUSB_ATH3012 },
Anantha Krishnanfa2f1392014-07-08 19:25:08 +0530223 { USB_DEVICE(0x13d3, 0x3432), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin0d0cef62015-06-06 20:29:25 +0300224 { USB_DEVICE(0x13d3, 0x3474), .driver_info = BTUSB_ATH3012 },
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530225
Cho, Yu-Chene9036e32011-02-15 10:20:07 +0800226 /* Atheros AR5BBU12 with sflash firmware */
227 { USB_DEVICE(0x0489, 0xe02c), .driver_info = BTUSB_IGNORE },
228
Michael Gruetzner85d59722012-05-02 22:33:40 +0200229 /* Atheros AR5BBU12 with sflash firmware */
Yevgeniy Melnichukbc21fde2012-08-07 19:48:10 +0530230 { USB_DEVICE(0x0489, 0xe036), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200231 { USB_DEVICE(0x0489, 0xe03c), .driver_info = BTUSB_ATH3012 },
Michael Gruetzner85d59722012-05-02 22:33:40 +0200232
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +0000233 /* QCA ROME chipset */
Wen-chien Jesse Sung20541112015-05-13 11:39:25 +0800234 { USB_DEVICE(0x0cf3, 0xe007), .driver_info = BTUSB_QCA_ROME },
Marcel Holtmannc9e44472015-03-16 23:56:04 -0700235 { USB_DEVICE(0x0cf3, 0xe300), .driver_info = BTUSB_QCA_ROME },
236 { USB_DEVICE(0x0cf3, 0xe360), .driver_info = BTUSB_QCA_ROME },
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +0000237
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200238 /* Broadcom BCM2035 */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100239 { USB_DEVICE(0x0a5c, 0x2009), .driver_info = BTUSB_BCM92035 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200240 { USB_DEVICE(0x0a5c, 0x200a), .driver_info = BTUSB_WRONG_SCO_MTU },
241 { USB_DEVICE(0x0a5c, 0x2035), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200242
243 /* Broadcom BCM2045 */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100244 { USB_DEVICE(0x0a5c, 0x2039), .driver_info = BTUSB_WRONG_SCO_MTU },
245 { USB_DEVICE(0x0a5c, 0x2101), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmannbdbef3d2008-09-23 00:16:35 +0200246
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200247 /* IBM/Lenovo ThinkPad with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100248 { USB_DEVICE(0x0a5c, 0x201e), .driver_info = BTUSB_WRONG_SCO_MTU },
249 { USB_DEVICE(0x0a5c, 0x2110), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200250
251 /* HP laptop with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100252 { USB_DEVICE(0x03f0, 0x171d), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200253
254 /* Dell laptop with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100255 { USB_DEVICE(0x413c, 0x8126), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200256
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100257 /* Dell Wireless 370 and 410 devices */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100258 { USB_DEVICE(0x413c, 0x8152), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100259 { USB_DEVICE(0x413c, 0x8156), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200260
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100261 /* Belkin F8T012 and F8T013 devices */
262 { USB_DEVICE(0x050d, 0x0012), .driver_info = BTUSB_WRONG_SCO_MTU },
263 { USB_DEVICE(0x050d, 0x0013), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200264
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100265 /* Asus WL-BTD202 device */
266 { USB_DEVICE(0x0b05, 0x1715), .driver_info = BTUSB_WRONG_SCO_MTU },
267
268 /* Kensington Bluetooth USB adapter */
269 { USB_DEVICE(0x047d, 0x105e), .driver_info = BTUSB_WRONG_SCO_MTU },
270
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200271 /* RTX Telecom based adapters with buggy SCO support */
272 { USB_DEVICE(0x0400, 0x0807), .driver_info = BTUSB_BROKEN_ISOC },
273 { USB_DEVICE(0x0400, 0x080a), .driver_info = BTUSB_BROKEN_ISOC },
274
275 /* CONWISE Technology based adapters with buggy SCO support */
276 { USB_DEVICE(0x0e5e, 0x6622), .driver_info = BTUSB_BROKEN_ISOC },
277
Marcel Holtmann4fcef8e2015-01-01 17:34:37 -0800278 /* Roper Class 1 Bluetooth Dongle (Silicon Wave based) */
Aleksei Volkov2eeac872015-06-08 12:02:10 +0300279 { USB_DEVICE(0x1310, 0x0001), .driver_info = BTUSB_SWAVE },
Marcel Holtmann4fcef8e2015-01-01 17:34:37 -0800280
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200281 /* Digianswer devices */
282 { USB_DEVICE(0x08fd, 0x0001), .driver_info = BTUSB_DIGIANSWER },
283 { USB_DEVICE(0x08fd, 0x0002), .driver_info = BTUSB_IGNORE },
284
285 /* CSR BlueCore Bluetooth Sniffer */
Marcel Holtmann4f64fa82014-07-07 00:12:04 +0200286 { USB_DEVICE(0x0a12, 0x0002),
287 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200288
289 /* Frontline ComProbe Bluetooth Sniffer */
Marcel Holtmann4f64fa82014-07-07 00:12:04 +0200290 { USB_DEVICE(0x16d3, 0x0002),
291 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200292
Marcel Holtmanncb1ee892015-01-28 19:41:42 -0800293 /* Marvell Bluetooth devices */
294 { USB_DEVICE(0x1286, 0x2044), .driver_info = BTUSB_MARVELL },
295 { USB_DEVICE(0x1286, 0x2046), .driver_info = BTUSB_MARVELL },
296
Marcel Holtmannd0ac9eb2015-01-28 19:41:43 -0800297 /* Intel Bluetooth devices */
Marcel Holtmann407550f2015-02-22 15:41:18 -0800298 { USB_DEVICE(0x8087, 0x07da), .driver_info = BTUSB_CSR },
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -0700299 { USB_DEVICE(0x8087, 0x07dc), .driver_info = BTUSB_INTEL },
Tedd Ho-Jeong Anef4e5e42013-11-12 13:10:58 -0800300 { USB_DEVICE(0x8087, 0x0a2a), .driver_info = BTUSB_INTEL },
Marcel Holtmanncda0dd72015-01-26 21:33:48 -0800301 { USB_DEVICE(0x8087, 0x0a2b), .driver_info = BTUSB_INTEL_NEW },
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -0700302
Marcel Holtmannd0ac9eb2015-01-28 19:41:43 -0800303 /* Other Intel Bluetooth devices */
304 { USB_VENDOR_AND_INTERFACE_INFO(0x8087, 0xe0, 0x01, 0x01),
305 .driver_info = BTUSB_IGNORE },
Amitkumar Karwarae8df492014-07-18 14:47:06 -0700306
Daniel Drakea2698a92015-04-16 14:09:55 -0600307 /* Realtek Bluetooth devices */
308 { USB_VENDOR_AND_INTERFACE_INFO(0x0bda, 0xe0, 0x01, 0x01),
309 .driver_info = BTUSB_REALTEK },
310
311 /* Additional Realtek 8723AE Bluetooth devices */
312 { USB_DEVICE(0x0930, 0x021d), .driver_info = BTUSB_REALTEK },
313 { USB_DEVICE(0x13d3, 0x3394), .driver_info = BTUSB_REALTEK },
314
315 /* Additional Realtek 8723BE Bluetooth devices */
316 { USB_DEVICE(0x0489, 0xe085), .driver_info = BTUSB_REALTEK },
317 { USB_DEVICE(0x0489, 0xe08b), .driver_info = BTUSB_REALTEK },
318 { USB_DEVICE(0x13d3, 0x3410), .driver_info = BTUSB_REALTEK },
319 { USB_DEVICE(0x13d3, 0x3416), .driver_info = BTUSB_REALTEK },
320 { USB_DEVICE(0x13d3, 0x3459), .driver_info = BTUSB_REALTEK },
321
322 /* Additional Realtek 8821AE Bluetooth devices */
323 { USB_DEVICE(0x0b05, 0x17dc), .driver_info = BTUSB_REALTEK },
324 { USB_DEVICE(0x13d3, 0x3414), .driver_info = BTUSB_REALTEK },
325 { USB_DEVICE(0x13d3, 0x3458), .driver_info = BTUSB_REALTEK },
326 { USB_DEVICE(0x13d3, 0x3461), .driver_info = BTUSB_REALTEK },
327 { USB_DEVICE(0x13d3, 0x3462), .driver_info = BTUSB_REALTEK },
328
Peter Poklop4481c072015-08-15 20:47:09 +0200329 /* Silicon Wave based devices */
330 { USB_DEVICE(0x0c10, 0x0000), .driver_info = BTUSB_SWAVE },
331
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200332 { } /* Terminating entry */
333};
334
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200335#define BTUSB_MAX_ISOC_FRAMES 10
336
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200337#define BTUSB_INTR_RUNNING 0
338#define BTUSB_BULK_RUNNING 1
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200339#define BTUSB_ISOC_RUNNING 2
Oliver Neukum7bee5492009-08-24 23:44:59 +0200340#define BTUSB_SUSPENDING 3
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -0300341#define BTUSB_DID_ISO_RESUME 4
Marcel Holtmanncda0dd72015-01-26 21:33:48 -0800342#define BTUSB_BOOTLOADER 5
343#define BTUSB_DOWNLOADING 6
Marcel Holtmannce6bb922015-01-28 01:58:40 -0800344#define BTUSB_FIRMWARE_LOADED 7
Marcel Holtmanncda0dd72015-01-26 21:33:48 -0800345#define BTUSB_FIRMWARE_FAILED 8
Marcel Holtmannce6bb922015-01-28 01:58:40 -0800346#define BTUSB_BOOTING 9
Daniel Drake04b8c812015-05-21 08:23:50 -0600347#define BTUSB_RESET_RESUME 10
Marcel Holtmann9d08f502015-10-08 20:23:08 +0200348#define BTUSB_DIAG_RUNNING 11
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200349
350struct btusb_data {
351 struct hci_dev *hdev;
352 struct usb_device *udev;
Marcel Holtmann5fbcd262008-09-23 00:16:36 +0200353 struct usb_interface *intf;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200354 struct usb_interface *isoc;
Marcel Holtmann9d08f502015-10-08 20:23:08 +0200355 struct usb_interface *diag;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200356
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200357 unsigned long flags;
358
359 struct work_struct work;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200360 struct work_struct waker;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200361
Marcel Holtmann803b5832014-09-16 08:00:29 +0200362 struct usb_anchor deferred;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200363 struct usb_anchor tx_anchor;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200364 int tx_in_flight;
365 spinlock_t txlock;
366
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200367 struct usb_anchor intr_anchor;
368 struct usb_anchor bulk_anchor;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200369 struct usb_anchor isoc_anchor;
Marcel Holtmann9d08f502015-10-08 20:23:08 +0200370 struct usb_anchor diag_anchor;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200371 spinlock_t rxlock;
372
373 struct sk_buff *evt_skb;
374 struct sk_buff *acl_skb;
375 struct sk_buff *sco_skb;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200376
377 struct usb_endpoint_descriptor *intr_ep;
378 struct usb_endpoint_descriptor *bulk_tx_ep;
379 struct usb_endpoint_descriptor *bulk_rx_ep;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200380 struct usb_endpoint_descriptor *isoc_tx_ep;
381 struct usb_endpoint_descriptor *isoc_rx_ep;
Marcel Holtmann9d08f502015-10-08 20:23:08 +0200382 struct usb_endpoint_descriptor *diag_tx_ep;
383 struct usb_endpoint_descriptor *diag_rx_ep;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200384
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100385 __u8 cmdreq_type;
Marcel Holtmann893ba542015-01-28 20:27:34 -0800386 __u8 cmdreq;
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100387
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100388 unsigned int sco_num;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200389 int isoc_altsetting;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +0100390 int suspend_count;
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +0100391
Marcel Holtmann97307f52015-01-12 13:51:10 -0800392 int (*recv_event)(struct hci_dev *hdev, struct sk_buff *skb);
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +0100393 int (*recv_bulk)(struct btusb_data *data, void *buffer, int count);
Kim, Ben Young Taeace31982015-02-15 23:06:14 +0000394
395 int (*setup_on_usb)(struct hci_dev *hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200396};
397
Marcel Holtmann803b5832014-09-16 08:00:29 +0200398static inline void btusb_free_frags(struct btusb_data *data)
399{
400 unsigned long flags;
401
402 spin_lock_irqsave(&data->rxlock, flags);
403
404 kfree_skb(data->evt_skb);
405 data->evt_skb = NULL;
406
407 kfree_skb(data->acl_skb);
408 data->acl_skb = NULL;
409
410 kfree_skb(data->sco_skb);
411 data->sco_skb = NULL;
412
413 spin_unlock_irqrestore(&data->rxlock, flags);
414}
415
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200416static int btusb_recv_intr(struct btusb_data *data, void *buffer, int count)
417{
Marcel Holtmann803b5832014-09-16 08:00:29 +0200418 struct sk_buff *skb;
419 int err = 0;
420
421 spin_lock(&data->rxlock);
422 skb = data->evt_skb;
423
424 while (count) {
425 int len;
426
427 if (!skb) {
428 skb = bt_skb_alloc(HCI_MAX_EVENT_SIZE, GFP_ATOMIC);
429 if (!skb) {
430 err = -ENOMEM;
431 break;
432 }
433
434 bt_cb(skb)->pkt_type = HCI_EVENT_PKT;
435 bt_cb(skb)->expect = HCI_EVENT_HDR_SIZE;
436 }
437
438 len = min_t(uint, bt_cb(skb)->expect, count);
439 memcpy(skb_put(skb, len), buffer, len);
440
441 count -= len;
442 buffer += len;
443 bt_cb(skb)->expect -= len;
444
445 if (skb->len == HCI_EVENT_HDR_SIZE) {
446 /* Complete event header */
447 bt_cb(skb)->expect = hci_event_hdr(skb)->plen;
448
449 if (skb_tailroom(skb) < bt_cb(skb)->expect) {
450 kfree_skb(skb);
451 skb = NULL;
452
453 err = -EILSEQ;
454 break;
455 }
456 }
457
458 if (bt_cb(skb)->expect == 0) {
459 /* Complete frame */
Marcel Holtmann97307f52015-01-12 13:51:10 -0800460 data->recv_event(data->hdev, skb);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200461 skb = NULL;
462 }
463 }
464
465 data->evt_skb = skb;
466 spin_unlock(&data->rxlock);
467
468 return err;
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200469}
470
471static int btusb_recv_bulk(struct btusb_data *data, void *buffer, int count)
472{
Marcel Holtmann803b5832014-09-16 08:00:29 +0200473 struct sk_buff *skb;
474 int err = 0;
475
476 spin_lock(&data->rxlock);
477 skb = data->acl_skb;
478
479 while (count) {
480 int len;
481
482 if (!skb) {
483 skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
484 if (!skb) {
485 err = -ENOMEM;
486 break;
487 }
488
489 bt_cb(skb)->pkt_type = HCI_ACLDATA_PKT;
490 bt_cb(skb)->expect = HCI_ACL_HDR_SIZE;
491 }
492
493 len = min_t(uint, bt_cb(skb)->expect, count);
494 memcpy(skb_put(skb, len), buffer, len);
495
496 count -= len;
497 buffer += len;
498 bt_cb(skb)->expect -= len;
499
500 if (skb->len == HCI_ACL_HDR_SIZE) {
501 __le16 dlen = hci_acl_hdr(skb)->dlen;
502
503 /* Complete ACL header */
504 bt_cb(skb)->expect = __le16_to_cpu(dlen);
505
506 if (skb_tailroom(skb) < bt_cb(skb)->expect) {
507 kfree_skb(skb);
508 skb = NULL;
509
510 err = -EILSEQ;
511 break;
512 }
513 }
514
515 if (bt_cb(skb)->expect == 0) {
516 /* Complete frame */
517 hci_recv_frame(data->hdev, skb);
518 skb = NULL;
519 }
520 }
521
522 data->acl_skb = skb;
523 spin_unlock(&data->rxlock);
524
525 return err;
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200526}
527
528static int btusb_recv_isoc(struct btusb_data *data, void *buffer, int count)
529{
Marcel Holtmann803b5832014-09-16 08:00:29 +0200530 struct sk_buff *skb;
531 int err = 0;
532
533 spin_lock(&data->rxlock);
534 skb = data->sco_skb;
535
536 while (count) {
537 int len;
538
539 if (!skb) {
540 skb = bt_skb_alloc(HCI_MAX_SCO_SIZE, GFP_ATOMIC);
541 if (!skb) {
542 err = -ENOMEM;
543 break;
544 }
545
546 bt_cb(skb)->pkt_type = HCI_SCODATA_PKT;
547 bt_cb(skb)->expect = HCI_SCO_HDR_SIZE;
548 }
549
550 len = min_t(uint, bt_cb(skb)->expect, count);
551 memcpy(skb_put(skb, len), buffer, len);
552
553 count -= len;
554 buffer += len;
555 bt_cb(skb)->expect -= len;
556
557 if (skb->len == HCI_SCO_HDR_SIZE) {
558 /* Complete SCO header */
559 bt_cb(skb)->expect = hci_sco_hdr(skb)->dlen;
560
561 if (skb_tailroom(skb) < bt_cb(skb)->expect) {
562 kfree_skb(skb);
563 skb = NULL;
564
565 err = -EILSEQ;
566 break;
567 }
568 }
569
570 if (bt_cb(skb)->expect == 0) {
571 /* Complete frame */
572 hci_recv_frame(data->hdev, skb);
573 skb = NULL;
574 }
575 }
576
577 data->sco_skb = skb;
578 spin_unlock(&data->rxlock);
579
580 return err;
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200581}
582
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200583static void btusb_intr_complete(struct urb *urb)
584{
585 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +0100586 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200587 int err;
588
Marcel Holtmann89e75332014-09-16 04:44:50 +0200589 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
590 urb->actual_length);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200591
592 if (!test_bit(HCI_RUNNING, &hdev->flags))
593 return;
594
595 if (urb->status == 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200596 hdev->stat.byte_rx += urb->actual_length;
597
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200598 if (btusb_recv_intr(data, urb->transfer_buffer,
599 urb->actual_length) < 0) {
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200600 BT_ERR("%s corrupted event packet", hdev->name);
601 hdev->stat.err_rx++;
602 }
Champion Chen85560c42014-09-06 14:06:08 -0500603 } else if (urb->status == -ENOENT) {
604 /* Avoid suspend failed when usb_kill_urb */
605 return;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200606 }
607
608 if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
609 return;
610
Oliver Neukum7bee5492009-08-24 23:44:59 +0200611 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200612 usb_anchor_urb(urb, &data->intr_anchor);
613
614 err = usb_submit_urb(urb, GFP_ATOMIC);
615 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200616 /* -EPERM: urb is being killed;
617 * -ENODEV: device got disconnected */
618 if (err != -EPERM && err != -ENODEV)
Stefan Seyfried61faddf2010-11-30 21:49:08 +0100619 BT_ERR("%s urb %p failed to resubmit (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200620 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200621 usb_unanchor_urb(urb);
622 }
623}
624
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100625static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200626{
David Herrmann155961e2012-02-09 21:58:32 +0100627 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200628 struct urb *urb;
629 unsigned char *buf;
630 unsigned int pipe;
631 int err, size;
632
633 BT_DBG("%s", hdev->name);
634
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200635 if (!data->intr_ep)
636 return -ENODEV;
637
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100638 urb = usb_alloc_urb(0, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200639 if (!urb)
640 return -ENOMEM;
641
642 size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
643
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100644 buf = kmalloc(size, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200645 if (!buf) {
646 usb_free_urb(urb);
647 return -ENOMEM;
648 }
649
650 pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
651
652 usb_fill_int_urb(urb, data->udev, pipe, buf, size,
Marcel Holtmann89e75332014-09-16 04:44:50 +0200653 btusb_intr_complete, hdev, data->intr_ep->bInterval);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200654
655 urb->transfer_flags |= URB_FREE_BUFFER;
656
657 usb_anchor_urb(urb, &data->intr_anchor);
658
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100659 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200660 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +0200661 if (err != -EPERM && err != -ENODEV)
662 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200663 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200664 usb_unanchor_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200665 }
666
667 usb_free_urb(urb);
668
669 return err;
670}
671
672static void btusb_bulk_complete(struct urb *urb)
673{
674 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +0100675 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200676 int err;
677
Marcel Holtmann89e75332014-09-16 04:44:50 +0200678 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
679 urb->actual_length);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200680
681 if (!test_bit(HCI_RUNNING, &hdev->flags))
682 return;
683
684 if (urb->status == 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200685 hdev->stat.byte_rx += urb->actual_length;
686
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +0100687 if (data->recv_bulk(data, urb->transfer_buffer,
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200688 urb->actual_length) < 0) {
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200689 BT_ERR("%s corrupted ACL packet", hdev->name);
690 hdev->stat.err_rx++;
691 }
Champion Chen85560c42014-09-06 14:06:08 -0500692 } else if (urb->status == -ENOENT) {
693 /* Avoid suspend failed when usb_kill_urb */
694 return;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200695 }
696
697 if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
698 return;
699
700 usb_anchor_urb(urb, &data->bulk_anchor);
Oliver Neukum652fd782009-12-16 19:23:43 +0100701 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200702
703 err = usb_submit_urb(urb, GFP_ATOMIC);
704 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200705 /* -EPERM: urb is being killed;
706 * -ENODEV: device got disconnected */
707 if (err != -EPERM && err != -ENODEV)
Stefan Seyfried61faddf2010-11-30 21:49:08 +0100708 BT_ERR("%s urb %p failed to resubmit (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200709 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200710 usb_unanchor_urb(urb);
711 }
712}
713
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100714static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200715{
David Herrmann155961e2012-02-09 21:58:32 +0100716 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200717 struct urb *urb;
718 unsigned char *buf;
719 unsigned int pipe;
Vikram Kandukuri290ba202009-07-02 14:31:59 +0530720 int err, size = HCI_MAX_FRAME_SIZE;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200721
722 BT_DBG("%s", hdev->name);
723
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200724 if (!data->bulk_rx_ep)
725 return -ENODEV;
726
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100727 urb = usb_alloc_urb(0, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200728 if (!urb)
729 return -ENOMEM;
730
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100731 buf = kmalloc(size, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200732 if (!buf) {
733 usb_free_urb(urb);
734 return -ENOMEM;
735 }
736
737 pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
738
Marcel Holtmann89e75332014-09-16 04:44:50 +0200739 usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
740 btusb_bulk_complete, hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200741
742 urb->transfer_flags |= URB_FREE_BUFFER;
743
Oliver Neukum7bee5492009-08-24 23:44:59 +0200744 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200745 usb_anchor_urb(urb, &data->bulk_anchor);
746
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100747 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200748 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +0200749 if (err != -EPERM && err != -ENODEV)
750 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200751 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200752 usb_unanchor_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200753 }
754
755 usb_free_urb(urb);
756
757 return err;
758}
759
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200760static void btusb_isoc_complete(struct urb *urb)
761{
762 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +0100763 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200764 int i, err;
765
Marcel Holtmann89e75332014-09-16 04:44:50 +0200766 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
767 urb->actual_length);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200768
769 if (!test_bit(HCI_RUNNING, &hdev->flags))
770 return;
771
772 if (urb->status == 0) {
773 for (i = 0; i < urb->number_of_packets; i++) {
774 unsigned int offset = urb->iso_frame_desc[i].offset;
775 unsigned int length = urb->iso_frame_desc[i].actual_length;
776
777 if (urb->iso_frame_desc[i].status)
778 continue;
779
780 hdev->stat.byte_rx += length;
781
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200782 if (btusb_recv_isoc(data, urb->transfer_buffer + offset,
783 length) < 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200784 BT_ERR("%s corrupted SCO packet", hdev->name);
785 hdev->stat.err_rx++;
786 }
787 }
Champion Chen85560c42014-09-06 14:06:08 -0500788 } else if (urb->status == -ENOENT) {
789 /* Avoid suspend failed when usb_kill_urb */
790 return;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200791 }
792
793 if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags))
794 return;
795
796 usb_anchor_urb(urb, &data->isoc_anchor);
797
798 err = usb_submit_urb(urb, GFP_ATOMIC);
799 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200800 /* -EPERM: urb is being killed;
801 * -ENODEV: device got disconnected */
802 if (err != -EPERM && err != -ENODEV)
Stefan Seyfried61faddf2010-11-30 21:49:08 +0100803 BT_ERR("%s urb %p failed to resubmit (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200804 hdev->name, urb, -err);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200805 usb_unanchor_urb(urb);
806 }
807}
808
Jesper Juhl42b16b32011-01-17 00:09:38 +0100809static inline void __fill_isoc_descriptor(struct urb *urb, int len, int mtu)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200810{
811 int i, offset = 0;
812
813 BT_DBG("len %d mtu %d", len, mtu);
814
815 for (i = 0; i < BTUSB_MAX_ISOC_FRAMES && len >= mtu;
816 i++, offset += mtu, len -= mtu) {
817 urb->iso_frame_desc[i].offset = offset;
818 urb->iso_frame_desc[i].length = mtu;
819 }
820
821 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
822 urb->iso_frame_desc[i].offset = offset;
823 urb->iso_frame_desc[i].length = len;
824 i++;
825 }
826
827 urb->number_of_packets = i;
828}
829
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100830static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200831{
David Herrmann155961e2012-02-09 21:58:32 +0100832 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200833 struct urb *urb;
834 unsigned char *buf;
835 unsigned int pipe;
836 int err, size;
837
838 BT_DBG("%s", hdev->name);
839
840 if (!data->isoc_rx_ep)
841 return -ENODEV;
842
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100843 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200844 if (!urb)
845 return -ENOMEM;
846
847 size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) *
848 BTUSB_MAX_ISOC_FRAMES;
849
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100850 buf = kmalloc(size, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200851 if (!buf) {
852 usb_free_urb(urb);
853 return -ENOMEM;
854 }
855
856 pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress);
857
Bing Zhaofa0fb932011-12-20 18:19:00 -0800858 usb_fill_int_urb(urb, data->udev, pipe, buf, size, btusb_isoc_complete,
Marcel Holtmann89e75332014-09-16 04:44:50 +0200859 hdev, data->isoc_rx_ep->bInterval);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200860
Marcel Holtmann89e75332014-09-16 04:44:50 +0200861 urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200862
863 __fill_isoc_descriptor(urb, size,
Marcel Holtmann89e75332014-09-16 04:44:50 +0200864 le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize));
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200865
866 usb_anchor_urb(urb, &data->isoc_anchor);
867
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100868 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200869 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +0200870 if (err != -EPERM && err != -ENODEV)
871 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200872 hdev->name, urb, -err);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200873 usb_unanchor_urb(urb);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200874 }
875
876 usb_free_urb(urb);
877
878 return err;
879}
880
Marcel Holtmann9d08f502015-10-08 20:23:08 +0200881static void btusb_diag_complete(struct urb *urb)
882{
883 struct hci_dev *hdev = urb->context;
884 struct btusb_data *data = hci_get_drvdata(hdev);
885 int err;
886
887 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
888 urb->actual_length);
889
890 if (urb->status == 0) {
891 struct sk_buff *skb;
892
893 skb = bt_skb_alloc(urb->actual_length, GFP_ATOMIC);
894 if (skb) {
895 memcpy(skb_put(skb, urb->actual_length),
896 urb->transfer_buffer, urb->actual_length);
897 hci_recv_diag(hdev, skb);
898 }
899 } else if (urb->status == -ENOENT) {
900 /* Avoid suspend failed when usb_kill_urb */
901 return;
902 }
903
904 if (!test_bit(BTUSB_DIAG_RUNNING, &data->flags))
905 return;
906
907 usb_anchor_urb(urb, &data->diag_anchor);
908 usb_mark_last_busy(data->udev);
909
910 err = usb_submit_urb(urb, GFP_ATOMIC);
911 if (err < 0) {
912 /* -EPERM: urb is being killed;
913 * -ENODEV: device got disconnected */
914 if (err != -EPERM && err != -ENODEV)
915 BT_ERR("%s urb %p failed to resubmit (%d)",
916 hdev->name, urb, -err);
917 usb_unanchor_urb(urb);
918 }
919}
920
921static int btusb_submit_diag_urb(struct hci_dev *hdev, gfp_t mem_flags)
922{
923 struct btusb_data *data = hci_get_drvdata(hdev);
924 struct urb *urb;
925 unsigned char *buf;
926 unsigned int pipe;
927 int err, size = HCI_MAX_FRAME_SIZE;
928
929 BT_DBG("%s", hdev->name);
930
931 if (!data->diag_rx_ep)
932 return -ENODEV;
933
934 urb = usb_alloc_urb(0, mem_flags);
935 if (!urb)
936 return -ENOMEM;
937
938 buf = kmalloc(size, mem_flags);
939 if (!buf) {
940 usb_free_urb(urb);
941 return -ENOMEM;
942 }
943
944 pipe = usb_rcvbulkpipe(data->udev, data->diag_rx_ep->bEndpointAddress);
945
946 usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
947 btusb_diag_complete, hdev);
948
949 urb->transfer_flags |= URB_FREE_BUFFER;
950
951 usb_mark_last_busy(data->udev);
952 usb_anchor_urb(urb, &data->diag_anchor);
953
954 err = usb_submit_urb(urb, mem_flags);
955 if (err < 0) {
956 if (err != -EPERM && err != -ENODEV)
957 BT_ERR("%s urb %p submission failed (%d)",
958 hdev->name, urb, -err);
959 usb_unanchor_urb(urb);
960 }
961
962 usb_free_urb(urb);
963
964 return err;
965}
966
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200967static void btusb_tx_complete(struct urb *urb)
968{
969 struct sk_buff *skb = urb->context;
Marcel Holtmann89e75332014-09-16 04:44:50 +0200970 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
David Herrmann155961e2012-02-09 21:58:32 +0100971 struct btusb_data *data = hci_get_drvdata(hdev);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200972
Marcel Holtmann89e75332014-09-16 04:44:50 +0200973 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
974 urb->actual_length);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200975
976 if (!test_bit(HCI_RUNNING, &hdev->flags))
977 goto done;
978
979 if (!urb->status)
980 hdev->stat.byte_tx += urb->transfer_buffer_length;
981 else
982 hdev->stat.err_tx++;
983
984done:
985 spin_lock(&data->txlock);
986 data->tx_in_flight--;
987 spin_unlock(&data->txlock);
988
989 kfree(urb->setup_packet);
990
991 kfree_skb(skb);
992}
993
994static void btusb_isoc_tx_complete(struct urb *urb)
995{
996 struct sk_buff *skb = urb->context;
Marcel Holtmann89e75332014-09-16 04:44:50 +0200997 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200998
Marcel Holtmann89e75332014-09-16 04:44:50 +0200999 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1000 urb->actual_length);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001001
1002 if (!test_bit(HCI_RUNNING, &hdev->flags))
1003 goto done;
1004
1005 if (!urb->status)
1006 hdev->stat.byte_tx += urb->transfer_buffer_length;
1007 else
1008 hdev->stat.err_tx++;
1009
1010done:
1011 kfree(urb->setup_packet);
1012
1013 kfree_skb(skb);
1014}
1015
1016static int btusb_open(struct hci_dev *hdev)
1017{
David Herrmann155961e2012-02-09 21:58:32 +01001018 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001019 int err;
1020
1021 BT_DBG("%s", hdev->name);
1022
Kim, Ben Young Taeace31982015-02-15 23:06:14 +00001023 /* Patching USB firmware files prior to starting any URBs of HCI path
1024 * It is more safe to use USB bulk channel for downloading USB patch
1025 */
1026 if (data->setup_on_usb) {
1027 err = data->setup_on_usb(hdev);
Marcel Holtmanneb500422015-04-16 23:15:50 +02001028 if (err < 0)
Kim, Ben Young Taeace31982015-02-15 23:06:14 +00001029 return err;
1030 }
1031
Oliver Neukum7bee5492009-08-24 23:44:59 +02001032 err = usb_autopm_get_interface(data->intf);
1033 if (err < 0)
1034 return err;
1035
1036 data->intf->needs_remote_wakeup = 1;
1037
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001038 if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +02001039 goto done;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001040
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001041 err = btusb_submit_intr_urb(hdev, GFP_KERNEL);
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001042 if (err < 0)
1043 goto failed;
1044
1045 err = btusb_submit_bulk_urb(hdev, GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001046 if (err < 0) {
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001047 usb_kill_anchored_urbs(&data->intr_anchor);
1048 goto failed;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001049 }
1050
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001051 set_bit(BTUSB_BULK_RUNNING, &data->flags);
1052 btusb_submit_bulk_urb(hdev, GFP_KERNEL);
1053
Marcel Holtmann9d08f502015-10-08 20:23:08 +02001054 if (data->diag) {
1055 if (!btusb_submit_diag_urb(hdev, GFP_KERNEL))
1056 set_bit(BTUSB_DIAG_RUNNING, &data->flags);
1057 }
1058
Oliver Neukum7bee5492009-08-24 23:44:59 +02001059done:
1060 usb_autopm_put_interface(data->intf);
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001061 return 0;
1062
1063failed:
1064 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001065 usb_autopm_put_interface(data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001066 return err;
1067}
1068
Oliver Neukum7bee5492009-08-24 23:44:59 +02001069static void btusb_stop_traffic(struct btusb_data *data)
1070{
1071 usb_kill_anchored_urbs(&data->intr_anchor);
1072 usb_kill_anchored_urbs(&data->bulk_anchor);
1073 usb_kill_anchored_urbs(&data->isoc_anchor);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02001074 usb_kill_anchored_urbs(&data->diag_anchor);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001075}
1076
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001077static int btusb_close(struct hci_dev *hdev)
1078{
David Herrmann155961e2012-02-09 21:58:32 +01001079 struct btusb_data *data = hci_get_drvdata(hdev);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001080 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001081
1082 BT_DBG("%s", hdev->name);
1083
Marcel Holtmanne8c3c3d2008-09-23 00:16:36 +02001084 cancel_work_sync(&data->work);
Linus Torvalds404291a2009-11-11 13:32:29 -08001085 cancel_work_sync(&data->waker);
Marcel Holtmanne8c3c3d2008-09-23 00:16:36 +02001086
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001087 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001088 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001089 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02001090 clear_bit(BTUSB_DIAG_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001091
1092 btusb_stop_traffic(data);
Marcel Holtmann803b5832014-09-16 08:00:29 +02001093 btusb_free_frags(data);
1094
Oliver Neukum7bee5492009-08-24 23:44:59 +02001095 err = usb_autopm_get_interface(data->intf);
1096 if (err < 0)
Oliver Neukum7b8e2c12009-11-13 14:26:23 +01001097 goto failed;
Oliver Neukum7bee5492009-08-24 23:44:59 +02001098
1099 data->intf->needs_remote_wakeup = 0;
1100 usb_autopm_put_interface(data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001101
Oliver Neukum7b8e2c12009-11-13 14:26:23 +01001102failed:
1103 usb_scuttle_anchored_urbs(&data->deferred);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001104 return 0;
1105}
1106
1107static int btusb_flush(struct hci_dev *hdev)
1108{
David Herrmann155961e2012-02-09 21:58:32 +01001109 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001110
1111 BT_DBG("%s", hdev->name);
1112
1113 usb_kill_anchored_urbs(&data->tx_anchor);
Marcel Holtmann803b5832014-09-16 08:00:29 +02001114 btusb_free_frags(data);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001115
1116 return 0;
1117}
1118
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001119static struct urb *alloc_ctrl_urb(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001120{
David Herrmann155961e2012-02-09 21:58:32 +01001121 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001122 struct usb_ctrlrequest *dr;
1123 struct urb *urb;
1124 unsigned int pipe;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001125
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001126 urb = usb_alloc_urb(0, GFP_KERNEL);
1127 if (!urb)
1128 return ERR_PTR(-ENOMEM);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001129
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001130 dr = kmalloc(sizeof(*dr), GFP_KERNEL);
1131 if (!dr) {
1132 usb_free_urb(urb);
1133 return ERR_PTR(-ENOMEM);
1134 }
1135
1136 dr->bRequestType = data->cmdreq_type;
Marcel Holtmann893ba542015-01-28 20:27:34 -08001137 dr->bRequest = data->cmdreq;
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001138 dr->wIndex = 0;
1139 dr->wValue = 0;
1140 dr->wLength = __cpu_to_le16(skb->len);
1141
1142 pipe = usb_sndctrlpipe(data->udev, 0x00);
1143
Marcel Holtmann89e75332014-09-16 04:44:50 +02001144 usb_fill_control_urb(urb, data->udev, pipe, (void *)dr,
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001145 skb->data, skb->len, btusb_tx_complete, skb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001146
Marcel Holtmann89e75332014-09-16 04:44:50 +02001147 skb->dev = (void *)hdev;
Marcel Holtmann7bd8f092013-10-11 06:19:18 -07001148
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001149 return urb;
1150}
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001151
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001152static struct urb *alloc_bulk_urb(struct hci_dev *hdev, struct sk_buff *skb)
1153{
1154 struct btusb_data *data = hci_get_drvdata(hdev);
1155 struct urb *urb;
1156 unsigned int pipe;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001157
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001158 if (!data->bulk_tx_ep)
1159 return ERR_PTR(-ENODEV);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001160
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001161 urb = usb_alloc_urb(0, GFP_KERNEL);
1162 if (!urb)
1163 return ERR_PTR(-ENOMEM);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001164
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001165 pipe = usb_sndbulkpipe(data->udev, data->bulk_tx_ep->bEndpointAddress);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001166
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001167 usb_fill_bulk_urb(urb, data->udev, pipe,
1168 skb->data, skb->len, btusb_tx_complete, skb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001169
Marcel Holtmann89e75332014-09-16 04:44:50 +02001170 skb->dev = (void *)hdev;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001171
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001172 return urb;
1173}
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001174
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001175static struct urb *alloc_isoc_urb(struct hci_dev *hdev, struct sk_buff *skb)
1176{
1177 struct btusb_data *data = hci_get_drvdata(hdev);
1178 struct urb *urb;
1179 unsigned int pipe;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001180
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001181 if (!data->isoc_tx_ep)
1182 return ERR_PTR(-ENODEV);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001183
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001184 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_KERNEL);
1185 if (!urb)
1186 return ERR_PTR(-ENOMEM);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001187
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001188 pipe = usb_sndisocpipe(data->udev, data->isoc_tx_ep->bEndpointAddress);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001189
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001190 usb_fill_int_urb(urb, data->udev, pipe,
1191 skb->data, skb->len, btusb_isoc_tx_complete,
1192 skb, data->isoc_tx_ep->bInterval);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001193
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001194 urb->transfer_flags = URB_ISO_ASAP;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001195
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001196 __fill_isoc_descriptor(urb, skb->len,
1197 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001198
Marcel Holtmann89e75332014-09-16 04:44:50 +02001199 skb->dev = (void *)hdev;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001200
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001201 return urb;
1202}
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001203
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001204static int submit_tx_urb(struct hci_dev *hdev, struct urb *urb)
1205{
1206 struct btusb_data *data = hci_get_drvdata(hdev);
1207 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001208
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001209 usb_anchor_urb(urb, &data->tx_anchor);
1210
Johan Hedberge9753ef2014-09-14 08:49:34 +03001211 err = usb_submit_urb(urb, GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001212 if (err < 0) {
Paul Bolle5a9b80e2011-10-09 12:12:16 +02001213 if (err != -EPERM && err != -ENODEV)
1214 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +02001215 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001216 kfree(urb->setup_packet);
1217 usb_unanchor_urb(urb);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001218 } else {
1219 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001220 }
1221
Cong Wang54a8a792011-11-22 09:32:57 +08001222 usb_free_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001223 return err;
1224}
1225
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001226static int submit_or_queue_tx_urb(struct hci_dev *hdev, struct urb *urb)
1227{
1228 struct btusb_data *data = hci_get_drvdata(hdev);
1229 unsigned long flags;
1230 bool suspending;
1231
1232 spin_lock_irqsave(&data->txlock, flags);
1233 suspending = test_bit(BTUSB_SUSPENDING, &data->flags);
1234 if (!suspending)
1235 data->tx_in_flight++;
1236 spin_unlock_irqrestore(&data->txlock, flags);
1237
1238 if (!suspending)
1239 return submit_tx_urb(hdev, urb);
1240
1241 usb_anchor_urb(urb, &data->deferred);
1242 schedule_work(&data->waker);
1243
1244 usb_free_urb(urb);
1245 return 0;
1246}
1247
1248static int btusb_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
1249{
1250 struct urb *urb;
1251
1252 BT_DBG("%s", hdev->name);
1253
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001254 switch (bt_cb(skb)->pkt_type) {
1255 case HCI_COMMAND_PKT:
1256 urb = alloc_ctrl_urb(hdev, skb);
1257 if (IS_ERR(urb))
1258 return PTR_ERR(urb);
1259
1260 hdev->stat.cmd_tx++;
1261 return submit_or_queue_tx_urb(hdev, urb);
1262
1263 case HCI_ACLDATA_PKT:
1264 urb = alloc_bulk_urb(hdev, skb);
1265 if (IS_ERR(urb))
1266 return PTR_ERR(urb);
1267
1268 hdev->stat.acl_tx++;
1269 return submit_or_queue_tx_urb(hdev, urb);
1270
1271 case HCI_SCODATA_PKT:
1272 if (hci_conn_num(hdev, SCO_LINK) < 1)
1273 return -ENODEV;
1274
1275 urb = alloc_isoc_urb(hdev, skb);
1276 if (IS_ERR(urb))
1277 return PTR_ERR(urb);
1278
1279 hdev->stat.sco_tx++;
1280 return submit_tx_urb(hdev, urb);
1281 }
1282
1283 return -EILSEQ;
1284}
1285
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001286static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
1287{
David Herrmann155961e2012-02-09 21:58:32 +01001288 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001289
1290 BT_DBG("%s evt %d", hdev->name, evt);
1291
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001292 if (hci_conn_num(hdev, SCO_LINK) != data->sco_num) {
1293 data->sco_num = hci_conn_num(hdev, SCO_LINK);
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001294 schedule_work(&data->work);
Marcel Holtmanna780efa2008-11-30 12:17:12 +01001295 }
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001296}
1297
Jesper Juhl42b16b32011-01-17 00:09:38 +01001298static inline int __set_isoc_interface(struct hci_dev *hdev, int altsetting)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001299{
David Herrmann155961e2012-02-09 21:58:32 +01001300 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001301 struct usb_interface *intf = data->isoc;
1302 struct usb_endpoint_descriptor *ep_desc;
1303 int i, err;
1304
1305 if (!data->isoc)
1306 return -ENODEV;
1307
1308 err = usb_set_interface(data->udev, 1, altsetting);
1309 if (err < 0) {
1310 BT_ERR("%s setting interface failed (%d)", hdev->name, -err);
1311 return err;
1312 }
1313
1314 data->isoc_altsetting = altsetting;
1315
1316 data->isoc_tx_ep = NULL;
1317 data->isoc_rx_ep = NULL;
1318
1319 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
1320 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
1321
1322 if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) {
1323 data->isoc_tx_ep = ep_desc;
1324 continue;
1325 }
1326
1327 if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) {
1328 data->isoc_rx_ep = ep_desc;
1329 continue;
1330 }
1331 }
1332
1333 if (!data->isoc_tx_ep || !data->isoc_rx_ep) {
1334 BT_ERR("%s invalid SCO descriptors", hdev->name);
1335 return -ENODEV;
1336 }
1337
1338 return 0;
1339}
1340
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001341static void btusb_work(struct work_struct *work)
1342{
1343 struct btusb_data *data = container_of(work, struct btusb_data, work);
1344 struct hci_dev *hdev = data->hdev;
Mikel Astizf4001d22012-04-11 08:48:51 +02001345 int new_alts;
Oliver Neukum7bee5492009-08-24 23:44:59 +02001346 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001347
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001348 if (data->sco_num > 0) {
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -03001349 if (!test_bit(BTUSB_DID_ISO_RESUME, &data->flags)) {
Oliver Neukum8efdd0c2011-02-11 13:00:06 +01001350 err = usb_autopm_get_interface(data->isoc ? data->isoc : data->intf);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001351 if (err < 0) {
1352 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1353 usb_kill_anchored_urbs(&data->isoc_anchor);
1354 return;
1355 }
1356
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -03001357 set_bit(BTUSB_DID_ISO_RESUME, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001358 }
Mikel Astizf4001d22012-04-11 08:48:51 +02001359
1360 if (hdev->voice_setting & 0x0020) {
1361 static const int alts[3] = { 2, 4, 5 };
Marcel Holtmann89e75332014-09-16 04:44:50 +02001362
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001363 new_alts = alts[data->sco_num - 1];
Mikel Astizf4001d22012-04-11 08:48:51 +02001364 } else {
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001365 new_alts = data->sco_num;
Mikel Astizf4001d22012-04-11 08:48:51 +02001366 }
1367
1368 if (data->isoc_altsetting != new_alts) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001369 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1370 usb_kill_anchored_urbs(&data->isoc_anchor);
1371
Kuba Pawlak8f9d02f2015-09-10 17:07:00 +01001372 /* When isochronous alternate setting needs to be
1373 * changed, because SCO connection has been added
1374 * or removed, a packet fragment may be left in the
1375 * reassembling state. This could lead to wrongly
1376 * assembled fragments.
1377 *
1378 * Clear outstanding fragment when selecting a new
1379 * alternate setting.
1380 */
1381 spin_lock(&data->rxlock);
1382 kfree_skb(data->sco_skb);
1383 data->sco_skb = NULL;
1384 spin_unlock(&data->rxlock);
1385
Mikel Astizf4001d22012-04-11 08:48:51 +02001386 if (__set_isoc_interface(hdev, new_alts) < 0)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001387 return;
1388 }
1389
1390 if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001391 if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001392 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1393 else
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001394 btusb_submit_isoc_urb(hdev, GFP_KERNEL);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001395 }
1396 } else {
1397 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1398 usb_kill_anchored_urbs(&data->isoc_anchor);
1399
1400 __set_isoc_interface(hdev, 0);
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -03001401 if (test_and_clear_bit(BTUSB_DID_ISO_RESUME, &data->flags))
Oliver Neukum8efdd0c2011-02-11 13:00:06 +01001402 usb_autopm_put_interface(data->isoc ? data->isoc : data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001403 }
1404}
1405
Oliver Neukum7bee5492009-08-24 23:44:59 +02001406static void btusb_waker(struct work_struct *work)
1407{
1408 struct btusb_data *data = container_of(work, struct btusb_data, waker);
1409 int err;
1410
1411 err = usb_autopm_get_interface(data->intf);
1412 if (err < 0)
1413 return;
1414
1415 usb_autopm_put_interface(data->intf);
1416}
1417
Marcel Holtmann9f8f9622013-04-10 08:11:35 -07001418static int btusb_setup_bcm92035(struct hci_dev *hdev)
1419{
1420 struct sk_buff *skb;
1421 u8 val = 0x00;
1422
1423 BT_DBG("%s", hdev->name);
1424
1425 skb = __hci_cmd_sync(hdev, 0xfc3b, 1, &val, HCI_INIT_TIMEOUT);
1426 if (IS_ERR(skb))
1427 BT_ERR("BCM92035 command failed (%ld)", -PTR_ERR(skb));
1428 else
1429 kfree_skb(skb);
1430
1431 return 0;
1432}
1433
Marcel Holtmann81cac642014-01-03 03:02:36 -08001434static int btusb_setup_csr(struct hci_dev *hdev)
1435{
1436 struct hci_rp_read_local_version *rp;
1437 struct sk_buff *skb;
Marcel Holtmann81cac642014-01-03 03:02:36 -08001438
1439 BT_DBG("%s", hdev->name);
1440
Marcel Holtmann7cd84d72015-06-07 10:01:02 +02001441 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
1442 HCI_INIT_TIMEOUT);
1443 if (IS_ERR(skb)) {
1444 int err = PTR_ERR(skb);
1445 BT_ERR("%s: CSR: Local version failed (%d)", hdev->name, err);
1446 return err;
1447 }
1448
1449 if (skb->len != sizeof(struct hci_rp_read_local_version)) {
1450 BT_ERR("%s: CSR: Local version length mismatch", hdev->name);
1451 kfree_skb(skb);
1452 return -EIO;
1453 }
Marcel Holtmann81cac642014-01-03 03:02:36 -08001454
Marcel Holtmann89e75332014-09-16 04:44:50 +02001455 rp = (struct hci_rp_read_local_version *)skb->data;
Marcel Holtmann81cac642014-01-03 03:02:36 -08001456
Johan Hedberg6cafcd92015-08-30 21:47:21 +03001457 /* Detect controllers which aren't real CSR ones. */
1458 if (le16_to_cpu(rp->manufacturer) != 10 ||
1459 le16_to_cpu(rp->lmp_subver) == 0x0c5c) {
Marcel Holtmann9641d342015-06-07 10:01:01 +02001460 /* Clear the reset quirk since this is not an actual
1461 * early Bluetooth 1.1 device from CSR.
1462 */
1463 clear_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmann81cac642014-01-03 03:02:36 -08001464
Marcel Holtmann9641d342015-06-07 10:01:01 +02001465 /* These fake CSR controllers have all a broken
1466 * stored link key handling and so just disable it.
1467 */
1468 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
Marcel Holtmann81cac642014-01-03 03:02:36 -08001469 }
1470
Marcel Holtmann81cac642014-01-03 03:02:36 -08001471 kfree_skb(skb);
1472
Marcel Holtmann9641d342015-06-07 10:01:01 +02001473 return 0;
Marcel Holtmann81cac642014-01-03 03:02:36 -08001474}
1475
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001476static const struct firmware *btusb_setup_intel_get_fw(struct hci_dev *hdev,
Marcel Holtmann89e75332014-09-16 04:44:50 +02001477 struct intel_version *ver)
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001478{
1479 const struct firmware *fw;
1480 char fwname[64];
1481 int ret;
1482
1483 snprintf(fwname, sizeof(fwname),
1484 "intel/ibt-hw-%x.%x.%x-fw-%x.%x.%x.%x.%x.bseq",
1485 ver->hw_platform, ver->hw_variant, ver->hw_revision,
1486 ver->fw_variant, ver->fw_revision, ver->fw_build_num,
1487 ver->fw_build_ww, ver->fw_build_yy);
1488
1489 ret = request_firmware(&fw, fwname, &hdev->dev);
1490 if (ret < 0) {
1491 if (ret == -EINVAL) {
1492 BT_ERR("%s Intel firmware file request failed (%d)",
1493 hdev->name, ret);
1494 return NULL;
1495 }
1496
1497 BT_ERR("%s failed to open Intel firmware file: %s(%d)",
1498 hdev->name, fwname, ret);
1499
1500 /* If the correct firmware patch file is not found, use the
1501 * default firmware patch file instead
1502 */
1503 snprintf(fwname, sizeof(fwname), "intel/ibt-hw-%x.%x.bseq",
1504 ver->hw_platform, ver->hw_variant);
1505 if (request_firmware(&fw, fwname, &hdev->dev) < 0) {
1506 BT_ERR("%s failed to open default Intel fw file: %s",
1507 hdev->name, fwname);
1508 return NULL;
1509 }
1510 }
1511
1512 BT_INFO("%s: Intel Bluetooth firmware file: %s", hdev->name, fwname);
1513
1514 return fw;
1515}
1516
1517static int btusb_setup_intel_patching(struct hci_dev *hdev,
1518 const struct firmware *fw,
1519 const u8 **fw_ptr, int *disable_patch)
1520{
1521 struct sk_buff *skb;
1522 struct hci_command_hdr *cmd;
1523 const u8 *cmd_param;
1524 struct hci_event_hdr *evt = NULL;
1525 const u8 *evt_param = NULL;
1526 int remain = fw->size - (*fw_ptr - fw->data);
1527
1528 /* The first byte indicates the types of the patch command or event.
1529 * 0x01 means HCI command and 0x02 is HCI event. If the first bytes
1530 * in the current firmware buffer doesn't start with 0x01 or
1531 * the size of remain buffer is smaller than HCI command header,
1532 * the firmware file is corrupted and it should stop the patching
1533 * process.
1534 */
1535 if (remain > HCI_COMMAND_HDR_SIZE && *fw_ptr[0] != 0x01) {
1536 BT_ERR("%s Intel fw corrupted: invalid cmd read", hdev->name);
1537 return -EINVAL;
1538 }
1539 (*fw_ptr)++;
1540 remain--;
1541
1542 cmd = (struct hci_command_hdr *)(*fw_ptr);
1543 *fw_ptr += sizeof(*cmd);
1544 remain -= sizeof(*cmd);
1545
1546 /* Ensure that the remain firmware data is long enough than the length
1547 * of command parameter. If not, the firmware file is corrupted.
1548 */
1549 if (remain < cmd->plen) {
1550 BT_ERR("%s Intel fw corrupted: invalid cmd len", hdev->name);
1551 return -EFAULT;
1552 }
1553
1554 /* If there is a command that loads a patch in the firmware
1555 * file, then enable the patch upon success, otherwise just
1556 * disable the manufacturer mode, for example patch activation
1557 * is not required when the default firmware patch file is used
1558 * because there are no patch data to load.
1559 */
1560 if (*disable_patch && le16_to_cpu(cmd->opcode) == 0xfc8e)
1561 *disable_patch = 0;
1562
1563 cmd_param = *fw_ptr;
1564 *fw_ptr += cmd->plen;
1565 remain -= cmd->plen;
1566
1567 /* This reads the expected events when the above command is sent to the
1568 * device. Some vendor commands expects more than one events, for
1569 * example command status event followed by vendor specific event.
1570 * For this case, it only keeps the last expected event. so the command
1571 * can be sent with __hci_cmd_sync_ev() which returns the sk_buff of
1572 * last expected event.
1573 */
1574 while (remain > HCI_EVENT_HDR_SIZE && *fw_ptr[0] == 0x02) {
1575 (*fw_ptr)++;
1576 remain--;
1577
1578 evt = (struct hci_event_hdr *)(*fw_ptr);
1579 *fw_ptr += sizeof(*evt);
1580 remain -= sizeof(*evt);
1581
1582 if (remain < evt->plen) {
1583 BT_ERR("%s Intel fw corrupted: invalid evt len",
1584 hdev->name);
1585 return -EFAULT;
1586 }
1587
1588 evt_param = *fw_ptr;
1589 *fw_ptr += evt->plen;
1590 remain -= evt->plen;
1591 }
1592
1593 /* Every HCI commands in the firmware file has its correspond event.
1594 * If event is not found or remain is smaller than zero, the firmware
1595 * file is corrupted.
1596 */
1597 if (!evt || !evt_param || remain < 0) {
1598 BT_ERR("%s Intel fw corrupted: invalid evt read", hdev->name);
1599 return -EFAULT;
1600 }
1601
1602 skb = __hci_cmd_sync_ev(hdev, le16_to_cpu(cmd->opcode), cmd->plen,
1603 cmd_param, evt->evt, HCI_INIT_TIMEOUT);
1604 if (IS_ERR(skb)) {
1605 BT_ERR("%s sending Intel patch command (0x%4.4x) failed (%ld)",
1606 hdev->name, cmd->opcode, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001607 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001608 }
1609
1610 /* It ensures that the returned event matches the event data read from
1611 * the firmware file. At fist, it checks the length and then
1612 * the contents of the event.
1613 */
1614 if (skb->len != evt->plen) {
1615 BT_ERR("%s mismatch event length (opcode 0x%4.4x)", hdev->name,
1616 le16_to_cpu(cmd->opcode));
1617 kfree_skb(skb);
1618 return -EFAULT;
1619 }
1620
1621 if (memcmp(skb->data, evt_param, evt->plen)) {
1622 BT_ERR("%s mismatch event parameter (opcode 0x%4.4x)",
1623 hdev->name, le16_to_cpu(cmd->opcode));
1624 kfree_skb(skb);
1625 return -EFAULT;
1626 }
1627 kfree_skb(skb);
1628
1629 return 0;
1630}
1631
1632static int btusb_setup_intel(struct hci_dev *hdev)
1633{
1634 struct sk_buff *skb;
1635 const struct firmware *fw;
1636 const u8 *fw_ptr;
1637 int disable_patch;
1638 struct intel_version *ver;
1639
1640 const u8 mfg_enable[] = { 0x01, 0x00 };
1641 const u8 mfg_disable[] = { 0x00, 0x00 };
1642 const u8 mfg_reset_deactivate[] = { 0x00, 0x01 };
1643 const u8 mfg_reset_activate[] = { 0x00, 0x02 };
1644
1645 BT_DBG("%s", hdev->name);
1646
1647 /* The controller has a bug with the first HCI command sent to it
1648 * returning number of completed commands as zero. This would stall the
1649 * command processing in the Bluetooth core.
1650 *
1651 * As a workaround, send HCI Reset command first which will reset the
1652 * number of completed commands and allow normal command processing
1653 * from now on.
1654 */
1655 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
1656 if (IS_ERR(skb)) {
1657 BT_ERR("%s sending initial HCI reset command failed (%ld)",
1658 hdev->name, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001659 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001660 }
1661 kfree_skb(skb);
1662
1663 /* Read Intel specific controller version first to allow selection of
1664 * which firmware file to load.
1665 *
1666 * The returned information are hardware variant and revision plus
1667 * firmware variant, revision and build number.
1668 */
1669 skb = __hci_cmd_sync(hdev, 0xfc05, 0, NULL, HCI_INIT_TIMEOUT);
1670 if (IS_ERR(skb)) {
1671 BT_ERR("%s reading Intel fw version command failed (%ld)",
1672 hdev->name, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001673 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001674 }
1675
1676 if (skb->len != sizeof(*ver)) {
1677 BT_ERR("%s Intel version event length mismatch", hdev->name);
1678 kfree_skb(skb);
1679 return -EIO;
1680 }
1681
1682 ver = (struct intel_version *)skb->data;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001683
1684 BT_INFO("%s: read Intel version: %02x%02x%02x%02x%02x%02x%02x%02x%02x",
1685 hdev->name, ver->hw_platform, ver->hw_variant,
1686 ver->hw_revision, ver->fw_variant, ver->fw_revision,
1687 ver->fw_build_num, ver->fw_build_ww, ver->fw_build_yy,
1688 ver->fw_patch_num);
1689
1690 /* fw_patch_num indicates the version of patch the device currently
1691 * have. If there is no patch data in the device, it is always 0x00.
Minjune Kim5075eda2015-08-27 13:21:52 +09001692 * So, if it is other than 0x00, no need to patch the device again.
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001693 */
1694 if (ver->fw_patch_num) {
1695 BT_INFO("%s: Intel device is already patched. patch num: %02x",
1696 hdev->name, ver->fw_patch_num);
1697 kfree_skb(skb);
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07001698 btintel_check_bdaddr(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001699 return 0;
1700 }
1701
1702 /* Opens the firmware patch file based on the firmware version read
1703 * from the controller. If it fails to open the matching firmware
1704 * patch file, it tries to open the default firmware patch file.
1705 * If no patch file is found, allow the device to operate without
1706 * a patch.
1707 */
1708 fw = btusb_setup_intel_get_fw(hdev, ver);
1709 if (!fw) {
1710 kfree_skb(skb);
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07001711 btintel_check_bdaddr(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001712 return 0;
1713 }
1714 fw_ptr = fw->data;
1715
Marcel Holtmannecffc802015-06-07 09:42:19 +02001716 kfree_skb(skb);
1717
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001718 /* This Intel specific command enables the manufacturer mode of the
1719 * controller.
1720 *
1721 * Only while this mode is enabled, the driver can download the
1722 * firmware patch data and configuration parameters.
1723 */
1724 skb = __hci_cmd_sync(hdev, 0xfc11, 2, mfg_enable, HCI_INIT_TIMEOUT);
1725 if (IS_ERR(skb)) {
1726 BT_ERR("%s entering Intel manufacturer mode failed (%ld)",
1727 hdev->name, PTR_ERR(skb));
1728 release_firmware(fw);
Adam Leed9c78e92013-07-10 10:02:12 +08001729 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001730 }
1731
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001732 kfree_skb(skb);
1733
1734 disable_patch = 1;
1735
1736 /* The firmware data file consists of list of Intel specific HCI
1737 * commands and its expected events. The first byte indicates the
1738 * type of the message, either HCI command or HCI event.
1739 *
1740 * It reads the command and its expected event from the firmware file,
1741 * and send to the controller. Once __hci_cmd_sync_ev() returns,
1742 * the returned event is compared with the event read from the firmware
1743 * file and it will continue until all the messages are downloaded to
1744 * the controller.
1745 *
1746 * Once the firmware patching is completed successfully,
1747 * the manufacturer mode is disabled with reset and activating the
1748 * downloaded patch.
1749 *
1750 * If the firmware patching fails, the manufacturer mode is
1751 * disabled with reset and deactivating the patch.
1752 *
1753 * If the default patch file is used, no reset is done when disabling
1754 * the manufacturer.
1755 */
1756 while (fw->size > fw_ptr - fw->data) {
1757 int ret;
1758
1759 ret = btusb_setup_intel_patching(hdev, fw, &fw_ptr,
1760 &disable_patch);
1761 if (ret < 0)
1762 goto exit_mfg_deactivate;
1763 }
1764
1765 release_firmware(fw);
1766
1767 if (disable_patch)
1768 goto exit_mfg_disable;
1769
1770 /* Patching completed successfully and disable the manufacturer mode
1771 * with reset and activate the downloaded firmware patches.
1772 */
1773 skb = __hci_cmd_sync(hdev, 0xfc11, sizeof(mfg_reset_activate),
1774 mfg_reset_activate, HCI_INIT_TIMEOUT);
1775 if (IS_ERR(skb)) {
1776 BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
1777 hdev->name, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001778 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001779 }
1780 kfree_skb(skb);
1781
1782 BT_INFO("%s: Intel Bluetooth firmware patch completed and activated",
1783 hdev->name);
1784
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07001785 btintel_check_bdaddr(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001786 return 0;
1787
1788exit_mfg_disable:
1789 /* Disable the manufacturer mode without reset */
1790 skb = __hci_cmd_sync(hdev, 0xfc11, sizeof(mfg_disable), mfg_disable,
1791 HCI_INIT_TIMEOUT);
1792 if (IS_ERR(skb)) {
1793 BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
1794 hdev->name, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001795 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001796 }
1797 kfree_skb(skb);
1798
1799 BT_INFO("%s: Intel Bluetooth firmware patch completed", hdev->name);
Marcel Holtmann40cb0982014-07-02 12:06:45 +02001800
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07001801 btintel_check_bdaddr(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001802 return 0;
1803
1804exit_mfg_deactivate:
1805 release_firmware(fw);
1806
1807 /* Patching failed. Disable the manufacturer mode with reset and
1808 * deactivate the downloaded firmware patches.
1809 */
1810 skb = __hci_cmd_sync(hdev, 0xfc11, sizeof(mfg_reset_deactivate),
1811 mfg_reset_deactivate, HCI_INIT_TIMEOUT);
1812 if (IS_ERR(skb)) {
1813 BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
1814 hdev->name, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001815 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001816 }
1817 kfree_skb(skb);
1818
1819 BT_INFO("%s: Intel Bluetooth firmware patch completed and deactivated",
1820 hdev->name);
1821
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07001822 btintel_check_bdaddr(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001823 return 0;
1824}
1825
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001826static int inject_cmd_complete(struct hci_dev *hdev, __u16 opcode)
1827{
1828 struct sk_buff *skb;
1829 struct hci_event_hdr *hdr;
1830 struct hci_ev_cmd_complete *evt;
1831
1832 skb = bt_skb_alloc(sizeof(*hdr) + sizeof(*evt) + 1, GFP_ATOMIC);
1833 if (!skb)
1834 return -ENOMEM;
1835
1836 hdr = (struct hci_event_hdr *)skb_put(skb, sizeof(*hdr));
1837 hdr->evt = HCI_EV_CMD_COMPLETE;
1838 hdr->plen = sizeof(*evt) + 1;
1839
1840 evt = (struct hci_ev_cmd_complete *)skb_put(skb, sizeof(*evt));
1841 evt->ncmd = 0x01;
1842 evt->opcode = cpu_to_le16(opcode);
1843
1844 *skb_put(skb, 1) = 0x00;
1845
1846 bt_cb(skb)->pkt_type = HCI_EVENT_PKT;
1847
1848 return hci_recv_frame(hdev, skb);
1849}
1850
1851static int btusb_recv_bulk_intel(struct btusb_data *data, void *buffer,
1852 int count)
1853{
1854 /* When the device is in bootloader mode, then it can send
1855 * events via the bulk endpoint. These events are treated the
1856 * same way as the ones received from the interrupt endpoint.
1857 */
1858 if (test_bit(BTUSB_BOOTLOADER, &data->flags))
1859 return btusb_recv_intr(data, buffer, count);
1860
1861 return btusb_recv_bulk(data, buffer, count);
1862}
1863
Marcel Holtmannccd6da22015-04-09 00:35:19 -07001864static void btusb_intel_bootup(struct btusb_data *data, const void *ptr,
1865 unsigned int len)
1866{
1867 const struct intel_bootup *evt = ptr;
1868
1869 if (len != sizeof(*evt))
1870 return;
1871
1872 if (test_and_clear_bit(BTUSB_BOOTING, &data->flags)) {
1873 smp_mb__after_atomic();
1874 wake_up_bit(&data->flags, BTUSB_BOOTING);
1875 }
1876}
1877
1878static void btusb_intel_secure_send_result(struct btusb_data *data,
1879 const void *ptr, unsigned int len)
1880{
1881 const struct intel_secure_send_result *evt = ptr;
1882
1883 if (len != sizeof(*evt))
1884 return;
1885
1886 if (evt->result)
1887 set_bit(BTUSB_FIRMWARE_FAILED, &data->flags);
1888
1889 if (test_and_clear_bit(BTUSB_DOWNLOADING, &data->flags) &&
1890 test_bit(BTUSB_FIRMWARE_LOADED, &data->flags)) {
1891 smp_mb__after_atomic();
1892 wake_up_bit(&data->flags, BTUSB_DOWNLOADING);
1893 }
1894}
1895
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001896static int btusb_recv_event_intel(struct hci_dev *hdev, struct sk_buff *skb)
1897{
1898 struct btusb_data *data = hci_get_drvdata(hdev);
1899
1900 if (test_bit(BTUSB_BOOTLOADER, &data->flags)) {
1901 struct hci_event_hdr *hdr = (void *)skb->data;
1902
Marcel Holtmannccd6da22015-04-09 00:35:19 -07001903 if (skb->len > HCI_EVENT_HDR_SIZE && hdr->evt == 0xff &&
1904 hdr->plen > 0) {
1905 const void *ptr = skb->data + HCI_EVENT_HDR_SIZE + 1;
1906 unsigned int len = skb->len - HCI_EVENT_HDR_SIZE - 1;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001907
Marcel Holtmannccd6da22015-04-09 00:35:19 -07001908 switch (skb->data[2]) {
1909 case 0x02:
1910 /* When switching to the operational firmware
1911 * the device sends a vendor specific event
1912 * indicating that the bootup completed.
1913 */
1914 btusb_intel_bootup(data, ptr, len);
1915 break;
1916 case 0x06:
1917 /* When the firmware loading completes the
1918 * device sends out a vendor specific event
1919 * indicating the result of the firmware
1920 * loading.
1921 */
1922 btusb_intel_secure_send_result(data, ptr, len);
1923 break;
Johan Hedbergfad70972015-01-30 10:58:55 +02001924 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001925 }
1926 }
1927
1928 return hci_recv_frame(hdev, skb);
1929}
1930
1931static int btusb_send_frame_intel(struct hci_dev *hdev, struct sk_buff *skb)
1932{
1933 struct btusb_data *data = hci_get_drvdata(hdev);
1934 struct urb *urb;
1935
1936 BT_DBG("%s", hdev->name);
1937
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001938 switch (bt_cb(skb)->pkt_type) {
1939 case HCI_COMMAND_PKT:
1940 if (test_bit(BTUSB_BOOTLOADER, &data->flags)) {
1941 struct hci_command_hdr *cmd = (void *)skb->data;
1942 __u16 opcode = le16_to_cpu(cmd->opcode);
1943
1944 /* When in bootloader mode and the command 0xfc09
1945 * is received, it needs to be send down the
1946 * bulk endpoint. So allocate a bulk URB instead.
1947 */
1948 if (opcode == 0xfc09)
1949 urb = alloc_bulk_urb(hdev, skb);
1950 else
1951 urb = alloc_ctrl_urb(hdev, skb);
1952
1953 /* When the 0xfc01 command is issued to boot into
1954 * the operational firmware, it will actually not
1955 * send a command complete event. To keep the flow
1956 * control working inject that event here.
1957 */
1958 if (opcode == 0xfc01)
1959 inject_cmd_complete(hdev, opcode);
1960 } else {
1961 urb = alloc_ctrl_urb(hdev, skb);
1962 }
1963 if (IS_ERR(urb))
1964 return PTR_ERR(urb);
1965
1966 hdev->stat.cmd_tx++;
1967 return submit_or_queue_tx_urb(hdev, urb);
1968
1969 case HCI_ACLDATA_PKT:
1970 urb = alloc_bulk_urb(hdev, skb);
1971 if (IS_ERR(urb))
1972 return PTR_ERR(urb);
1973
1974 hdev->stat.acl_tx++;
1975 return submit_or_queue_tx_urb(hdev, urb);
1976
1977 case HCI_SCODATA_PKT:
1978 if (hci_conn_num(hdev, SCO_LINK) < 1)
1979 return -ENODEV;
1980
1981 urb = alloc_isoc_urb(hdev, skb);
1982 if (IS_ERR(urb))
1983 return PTR_ERR(urb);
1984
1985 hdev->stat.sco_tx++;
1986 return submit_tx_urb(hdev, urb);
1987 }
1988
1989 return -EILSEQ;
1990}
1991
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001992static int btusb_setup_intel_new(struct hci_dev *hdev)
1993{
1994 static const u8 reset_param[] = { 0x00, 0x01, 0x00, 0x01,
1995 0x00, 0x08, 0x04, 0x00 };
1996 struct btusb_data *data = hci_get_drvdata(hdev);
1997 struct sk_buff *skb;
1998 struct intel_version *ver;
1999 struct intel_boot_params *params;
2000 const struct firmware *fw;
2001 const u8 *fw_ptr;
Marcel Holtmanne66890a92015-06-07 09:47:08 +02002002 u32 frag_len;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002003 char fwname[64];
2004 ktime_t calltime, delta, rettime;
2005 unsigned long long duration;
2006 int err;
2007
2008 BT_DBG("%s", hdev->name);
2009
2010 calltime = ktime_get();
2011
2012 /* Read the Intel version information to determine if the device
2013 * is in bootloader mode or if it already has operational firmware
2014 * loaded.
2015 */
2016 skb = __hci_cmd_sync(hdev, 0xfc05, 0, NULL, HCI_INIT_TIMEOUT);
2017 if (IS_ERR(skb)) {
2018 BT_ERR("%s: Reading Intel version information failed (%ld)",
2019 hdev->name, PTR_ERR(skb));
2020 return PTR_ERR(skb);
2021 }
2022
2023 if (skb->len != sizeof(*ver)) {
2024 BT_ERR("%s: Intel version event size mismatch", hdev->name);
2025 kfree_skb(skb);
2026 return -EILSEQ;
2027 }
2028
2029 ver = (struct intel_version *)skb->data;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002030
2031 /* The hardware platform number has a fixed value of 0x37 and
2032 * for now only accept this single value.
2033 */
2034 if (ver->hw_platform != 0x37) {
2035 BT_ERR("%s: Unsupported Intel hardware platform (%u)",
2036 hdev->name, ver->hw_platform);
2037 kfree_skb(skb);
2038 return -EINVAL;
2039 }
2040
2041 /* At the moment only the hardware variant iBT 3.0 (LnP/SfP) is
2042 * supported by this firmware loading method. This check has been
2043 * put in place to ensure correct forward compatibility options
2044 * when newer hardware variants come along.
2045 */
2046 if (ver->hw_variant != 0x0b) {
2047 BT_ERR("%s: Unsupported Intel hardware variant (%u)",
2048 hdev->name, ver->hw_variant);
2049 kfree_skb(skb);
2050 return -EINVAL;
2051 }
2052
Marcel Holtmann7feb99e2015-07-05 15:02:07 +02002053 btintel_version_info(hdev, ver);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002054
2055 /* The firmware variant determines if the device is in bootloader
2056 * mode or is running operational firmware. The value 0x06 identifies
2057 * the bootloader and the value 0x23 identifies the operational
2058 * firmware.
2059 *
2060 * When the operational firmware is already present, then only
2061 * the check for valid Bluetooth device address is needed. This
2062 * determines if the device will be added as configured or
2063 * unconfigured controller.
2064 *
2065 * It is not possible to use the Secure Boot Parameters in this
2066 * case since that command is only available in bootloader mode.
2067 */
2068 if (ver->fw_variant == 0x23) {
2069 kfree_skb(skb);
2070 clear_bit(BTUSB_BOOTLOADER, &data->flags);
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07002071 btintel_check_bdaddr(hdev);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002072 return 0;
2073 }
2074
2075 /* If the device is not in bootloader mode, then the only possible
2076 * choice is to return an error and abort the device initialization.
2077 */
2078 if (ver->fw_variant != 0x06) {
2079 BT_ERR("%s: Unsupported Intel firmware variant (%u)",
2080 hdev->name, ver->fw_variant);
2081 kfree_skb(skb);
2082 return -ENODEV;
2083 }
2084
2085 kfree_skb(skb);
2086
2087 /* Read the secure boot parameters to identify the operating
2088 * details of the bootloader.
2089 */
2090 skb = __hci_cmd_sync(hdev, 0xfc0d, 0, NULL, HCI_INIT_TIMEOUT);
2091 if (IS_ERR(skb)) {
2092 BT_ERR("%s: Reading Intel boot parameters failed (%ld)",
2093 hdev->name, PTR_ERR(skb));
2094 return PTR_ERR(skb);
2095 }
2096
2097 if (skb->len != sizeof(*params)) {
2098 BT_ERR("%s: Intel boot parameters size mismatch", hdev->name);
2099 kfree_skb(skb);
2100 return -EILSEQ;
2101 }
2102
2103 params = (struct intel_boot_params *)skb->data;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002104
2105 BT_INFO("%s: Device revision is %u", hdev->name,
2106 le16_to_cpu(params->dev_revid));
2107
2108 BT_INFO("%s: Secure boot is %s", hdev->name,
2109 params->secure_boot ? "enabled" : "disabled");
2110
Marcel Holtmann2220994e2015-10-13 13:54:55 +02002111 BT_INFO("%s: OTP lock is %s", hdev->name,
2112 params->otp_lock ? "enabled" : "disabled");
2113
2114 BT_INFO("%s: API lock is %s", hdev->name,
2115 params->api_lock ? "enabled" : "disabled");
2116
2117 BT_INFO("%s: Debug lock is %s", hdev->name,
2118 params->debug_lock ? "enabled" : "disabled");
2119
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002120 BT_INFO("%s: Minimum firmware build %u week %u %u", hdev->name,
2121 params->min_fw_build_nn, params->min_fw_build_cw,
2122 2000 + params->min_fw_build_yy);
2123
2124 /* It is required that every single firmware fragment is acknowledged
2125 * with a command complete event. If the boot parameters indicate
2126 * that this bootloader does not send them, then abort the setup.
2127 */
2128 if (params->limited_cce != 0x00) {
2129 BT_ERR("%s: Unsupported Intel firmware loading method (%u)",
2130 hdev->name, params->limited_cce);
2131 kfree_skb(skb);
2132 return -EINVAL;
2133 }
2134
2135 /* If the OTP has no valid Bluetooth device address, then there will
2136 * also be no valid address for the operational firmware.
2137 */
2138 if (!bacmp(&params->otp_bdaddr, BDADDR_ANY)) {
2139 BT_INFO("%s: No device address configured", hdev->name);
2140 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
2141 }
2142
2143 /* With this Intel bootloader only the hardware variant and device
2144 * revision information are used to select the right firmware.
2145 *
2146 * Currently this bootloader support is limited to hardware variant
2147 * iBT 3.0 (LnP/SfP) which is identified by the value 11 (0x0b).
2148 */
2149 snprintf(fwname, sizeof(fwname), "intel/ibt-11-%u.sfi",
2150 le16_to_cpu(params->dev_revid));
2151
2152 err = request_firmware(&fw, fwname, &hdev->dev);
2153 if (err < 0) {
2154 BT_ERR("%s: Failed to load Intel firmware file (%d)",
2155 hdev->name, err);
2156 kfree_skb(skb);
2157 return err;
2158 }
2159
2160 BT_INFO("%s: Found device firmware: %s", hdev->name, fwname);
2161
Tedd Ho-Jeong An52cc9162015-06-12 16:20:05 -07002162 /* Save the DDC file name for later use to apply once the firmware
2163 * downloading is done.
2164 */
2165 snprintf(fwname, sizeof(fwname), "intel/ibt-11-%u.ddc",
2166 le16_to_cpu(params->dev_revid));
2167
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002168 kfree_skb(skb);
2169
2170 if (fw->size < 644) {
2171 BT_ERR("%s: Invalid size of firmware file (%zu)",
2172 hdev->name, fw->size);
2173 err = -EBADF;
2174 goto done;
2175 }
2176
2177 set_bit(BTUSB_DOWNLOADING, &data->flags);
2178
2179 /* Start the firmware download transaction with the Init fragment
2180 * represented by the 128 bytes of CSS header.
2181 */
Marcel Holtmann09df1232015-07-05 14:55:36 +02002182 err = btintel_secure_send(hdev, 0x00, 128, fw->data);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002183 if (err < 0) {
2184 BT_ERR("%s: Failed to send firmware header (%d)",
2185 hdev->name, err);
2186 goto done;
2187 }
2188
2189 /* Send the 256 bytes of public key information from the firmware
2190 * as the PKey fragment.
2191 */
Marcel Holtmann09df1232015-07-05 14:55:36 +02002192 err = btintel_secure_send(hdev, 0x03, 256, fw->data + 128);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002193 if (err < 0) {
2194 BT_ERR("%s: Failed to send firmware public key (%d)",
2195 hdev->name, err);
2196 goto done;
2197 }
2198
2199 /* Send the 256 bytes of signature information from the firmware
2200 * as the Sign fragment.
2201 */
Marcel Holtmann09df1232015-07-05 14:55:36 +02002202 err = btintel_secure_send(hdev, 0x02, 256, fw->data + 388);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002203 if (err < 0) {
2204 BT_ERR("%s: Failed to send firmware signature (%d)",
2205 hdev->name, err);
2206 goto done;
2207 }
2208
2209 fw_ptr = fw->data + 644;
Marcel Holtmanne66890a92015-06-07 09:47:08 +02002210 frag_len = 0;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002211
2212 while (fw_ptr - fw->data < fw->size) {
Marcel Holtmanne66890a92015-06-07 09:47:08 +02002213 struct hci_command_hdr *cmd = (void *)(fw_ptr + frag_len);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002214
Marcel Holtmanne66890a92015-06-07 09:47:08 +02002215 frag_len += sizeof(*cmd) + cmd->plen;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002216
Minjune Kim5075eda2015-08-27 13:21:52 +09002217 /* The parameter length of the secure send command requires
Marcel Holtmanne66890a92015-06-07 09:47:08 +02002218 * a 4 byte alignment. It happens so that the firmware file
2219 * contains proper Intel_NOP commands to align the fragments
2220 * as needed.
2221 *
2222 * Send set of commands with 4 byte alignment from the
2223 * firmware data buffer as a single Data fragement.
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002224 */
Marcel Holtmanne66890a92015-06-07 09:47:08 +02002225 if (!(frag_len % 4)) {
Marcel Holtmann09df1232015-07-05 14:55:36 +02002226 err = btintel_secure_send(hdev, 0x01, frag_len, fw_ptr);
Marcel Holtmanne66890a92015-06-07 09:47:08 +02002227 if (err < 0) {
2228 BT_ERR("%s: Failed to send firmware data (%d)",
2229 hdev->name, err);
2230 goto done;
2231 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002232
Marcel Holtmanne66890a92015-06-07 09:47:08 +02002233 fw_ptr += frag_len;
2234 frag_len = 0;
2235 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002236 }
2237
Marcel Holtmannce6bb922015-01-28 01:58:40 -08002238 set_bit(BTUSB_FIRMWARE_LOADED, &data->flags);
2239
Johan Hedberga087a982015-01-30 10:58:54 +02002240 BT_INFO("%s: Waiting for firmware download to complete", hdev->name);
2241
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002242 /* Before switching the device into operational mode and with that
2243 * booting the loaded firmware, wait for the bootloader notification
2244 * that all fragments have been successfully received.
2245 *
Johan Hedberga087a982015-01-30 10:58:54 +02002246 * When the event processing receives the notification, then the
2247 * BTUSB_DOWNLOADING flag will be cleared.
2248 *
2249 * The firmware loading should not take longer than 5 seconds
2250 * and thus just timeout if that happens and fail the setup
2251 * of this device.
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002252 */
Johan Hedberg129a7692015-02-14 09:33:35 +02002253 err = wait_on_bit_timeout(&data->flags, BTUSB_DOWNLOADING,
2254 TASK_INTERRUPTIBLE,
2255 msecs_to_jiffies(5000));
Johan Hedberga087a982015-01-30 10:58:54 +02002256 if (err == 1) {
2257 BT_ERR("%s: Firmware loading interrupted", hdev->name);
2258 err = -EINTR;
2259 goto done;
2260 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002261
Johan Hedberga087a982015-01-30 10:58:54 +02002262 if (err) {
2263 BT_ERR("%s: Firmware loading timeout", hdev->name);
2264 err = -ETIMEDOUT;
2265 goto done;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002266 }
2267
2268 if (test_bit(BTUSB_FIRMWARE_FAILED, &data->flags)) {
2269 BT_ERR("%s: Firmware loading failed", hdev->name);
2270 err = -ENOEXEC;
2271 goto done;
2272 }
2273
2274 rettime = ktime_get();
2275 delta = ktime_sub(rettime, calltime);
2276 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
2277
2278 BT_INFO("%s: Firmware loaded in %llu usecs", hdev->name, duration);
2279
2280done:
2281 release_firmware(fw);
2282
2283 if (err < 0)
2284 return err;
2285
2286 calltime = ktime_get();
2287
2288 set_bit(BTUSB_BOOTING, &data->flags);
2289
2290 skb = __hci_cmd_sync(hdev, 0xfc01, sizeof(reset_param), reset_param,
2291 HCI_INIT_TIMEOUT);
2292 if (IS_ERR(skb))
2293 return PTR_ERR(skb);
2294
2295 kfree_skb(skb);
2296
2297 /* The bootloader will not indicate when the device is ready. This
2298 * is done by the operational firmware sending bootup notification.
Johan Hedbergfad70972015-01-30 10:58:55 +02002299 *
2300 * Booting into operational firmware should not take longer than
2301 * 1 second. However if that happens, then just fail the setup
2302 * since something went wrong.
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002303 */
Johan Hedbergfad70972015-01-30 10:58:55 +02002304 BT_INFO("%s: Waiting for device to boot", hdev->name);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002305
Johan Hedberg129a7692015-02-14 09:33:35 +02002306 err = wait_on_bit_timeout(&data->flags, BTUSB_BOOTING,
2307 TASK_INTERRUPTIBLE,
2308 msecs_to_jiffies(1000));
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002309
Johan Hedbergfad70972015-01-30 10:58:55 +02002310 if (err == 1) {
2311 BT_ERR("%s: Device boot interrupted", hdev->name);
2312 return -EINTR;
2313 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002314
Johan Hedbergfad70972015-01-30 10:58:55 +02002315 if (err) {
2316 BT_ERR("%s: Device boot timeout", hdev->name);
2317 return -ETIMEDOUT;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002318 }
2319
2320 rettime = ktime_get();
2321 delta = ktime_sub(rettime, calltime);
2322 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
2323
2324 BT_INFO("%s: Device booted in %llu usecs", hdev->name, duration);
2325
2326 clear_bit(BTUSB_BOOTLOADER, &data->flags);
2327
Tedd Ho-Jeong An52cc9162015-06-12 16:20:05 -07002328 /* Once the device is running in operational mode, it needs to apply
2329 * the device configuration (DDC) parameters.
2330 *
2331 * The device can work without DDC parameters, so even if it fails
2332 * to load the file, no need to fail the setup.
2333 */
Loic Poulaine924d3d2015-09-04 17:54:36 +02002334 btintel_load_ddc_config(hdev, fwname);
Tedd Ho-Jeong An52cc9162015-06-12 16:20:05 -07002335
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002336 return 0;
2337}
2338
Tedd Ho-Jeong Anbfbd45e2015-02-13 09:20:52 -08002339static int btusb_shutdown_intel(struct hci_dev *hdev)
2340{
2341 struct sk_buff *skb;
2342 long ret;
2343
2344 /* Some platforms have an issue with BT LED when the interface is
2345 * down or BT radio is turned off, which takes 5 seconds to BT LED
2346 * goes off. This command turns off the BT LED immediately.
2347 */
2348 skb = __hci_cmd_sync(hdev, 0xfc3f, 0, NULL, HCI_INIT_TIMEOUT);
2349 if (IS_ERR(skb)) {
2350 ret = PTR_ERR(skb);
2351 BT_ERR("%s: turning off Intel device LED failed (%ld)",
2352 hdev->name, ret);
2353 return ret;
2354 }
2355 kfree_skb(skb);
2356
2357 return 0;
2358}
2359
Amitkumar Karwarae8df492014-07-18 14:47:06 -07002360static int btusb_set_bdaddr_marvell(struct hci_dev *hdev,
2361 const bdaddr_t *bdaddr)
2362{
2363 struct sk_buff *skb;
2364 u8 buf[8];
2365 long ret;
2366
2367 buf[0] = 0xfe;
2368 buf[1] = sizeof(bdaddr_t);
2369 memcpy(buf + 2, bdaddr, sizeof(bdaddr_t));
2370
2371 skb = __hci_cmd_sync(hdev, 0xfc22, sizeof(buf), buf, HCI_INIT_TIMEOUT);
2372 if (IS_ERR(skb)) {
2373 ret = PTR_ERR(skb);
2374 BT_ERR("%s: changing Marvell device address failed (%ld)",
2375 hdev->name, ret);
2376 return ret;
2377 }
2378 kfree_skb(skb);
2379
2380 return 0;
2381}
2382
Toshi Kikuchi58592232014-12-12 10:58:05 -08002383static int btusb_set_bdaddr_ath3012(struct hci_dev *hdev,
2384 const bdaddr_t *bdaddr)
2385{
2386 struct sk_buff *skb;
2387 u8 buf[10];
2388 long ret;
2389
2390 buf[0] = 0x01;
2391 buf[1] = 0x01;
2392 buf[2] = 0x00;
2393 buf[3] = sizeof(bdaddr_t);
2394 memcpy(buf + 4, bdaddr, sizeof(bdaddr_t));
2395
2396 skb = __hci_cmd_sync(hdev, 0xfc0b, sizeof(buf), buf, HCI_INIT_TIMEOUT);
2397 if (IS_ERR(skb)) {
2398 ret = PTR_ERR(skb);
2399 BT_ERR("%s: Change address command failed (%ld)",
2400 hdev->name, ret);
2401 return ret;
2402 }
2403 kfree_skb(skb);
2404
2405 return 0;
2406}
2407
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002408#define QCA_DFU_PACKET_LEN 4096
2409
2410#define QCA_GET_TARGET_VERSION 0x09
2411#define QCA_CHECK_STATUS 0x05
2412#define QCA_DFU_DOWNLOAD 0x01
2413
2414#define QCA_SYSCFG_UPDATED 0x40
2415#define QCA_PATCH_UPDATED 0x80
2416#define QCA_DFU_TIMEOUT 3000
2417
2418struct qca_version {
2419 __le32 rom_version;
2420 __le32 patch_version;
2421 __le32 ram_version;
2422 __le32 ref_clock;
2423 __u8 reserved[4];
2424} __packed;
2425
2426struct qca_rampatch_version {
2427 __le16 rom_version;
2428 __le16 patch_version;
2429} __packed;
2430
2431struct qca_device_info {
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002432 u32 rom_version;
2433 u8 rampatch_hdr; /* length of header in rampatch */
2434 u8 nvm_hdr; /* length of header in NVM */
2435 u8 ver_offset; /* offset of version structure in rampatch */
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002436};
2437
2438static const struct qca_device_info qca_devices_table[] = {
2439 { 0x00000100, 20, 4, 10 }, /* Rome 1.0 */
2440 { 0x00000101, 20, 4, 10 }, /* Rome 1.1 */
Chan-yeol Park7f6e6362015-05-21 11:24:27 +09002441 { 0x00000200, 28, 4, 18 }, /* Rome 2.0 */
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002442 { 0x00000201, 28, 4, 18 }, /* Rome 2.1 */
2443 { 0x00000300, 28, 4, 18 }, /* Rome 3.0 */
2444 { 0x00000302, 28, 4, 18 }, /* Rome 3.2 */
2445};
2446
2447static int btusb_qca_send_vendor_req(struct hci_dev *hdev, u8 request,
2448 void *data, u16 size)
2449{
2450 struct btusb_data *btdata = hci_get_drvdata(hdev);
2451 struct usb_device *udev = btdata->udev;
2452 int pipe, err;
2453 u8 *buf;
2454
2455 buf = kmalloc(size, GFP_KERNEL);
2456 if (!buf)
2457 return -ENOMEM;
2458
2459 /* Found some of USB hosts have IOT issues with ours so that we should
2460 * not wait until HCI layer is ready.
2461 */
2462 pipe = usb_rcvctrlpipe(udev, 0);
2463 err = usb_control_msg(udev, pipe, request, USB_TYPE_VENDOR | USB_DIR_IN,
2464 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
2465 if (err < 0) {
2466 BT_ERR("%s: Failed to access otp area (%d)", hdev->name, err);
2467 goto done;
2468 }
2469
2470 memcpy(data, buf, size);
2471
2472done:
2473 kfree(buf);
2474
2475 return err;
2476}
2477
2478static int btusb_setup_qca_download_fw(struct hci_dev *hdev,
2479 const struct firmware *firmware,
2480 size_t hdr_size)
2481{
2482 struct btusb_data *btdata = hci_get_drvdata(hdev);
2483 struct usb_device *udev = btdata->udev;
2484 size_t count, size, sent = 0;
2485 int pipe, len, err;
2486 u8 *buf;
2487
2488 buf = kmalloc(QCA_DFU_PACKET_LEN, GFP_KERNEL);
2489 if (!buf)
2490 return -ENOMEM;
2491
2492 count = firmware->size;
2493
2494 size = min_t(size_t, count, hdr_size);
2495 memcpy(buf, firmware->data, size);
2496
2497 /* USB patches should go down to controller through USB path
2498 * because binary format fits to go down through USB channel.
2499 * USB control path is for patching headers and USB bulk is for
2500 * patch body.
2501 */
2502 pipe = usb_sndctrlpipe(udev, 0);
2503 err = usb_control_msg(udev, pipe, QCA_DFU_DOWNLOAD, USB_TYPE_VENDOR,
2504 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
2505 if (err < 0) {
2506 BT_ERR("%s: Failed to send headers (%d)", hdev->name, err);
2507 goto done;
2508 }
2509
2510 sent += size;
2511 count -= size;
2512
2513 while (count) {
2514 size = min_t(size_t, count, QCA_DFU_PACKET_LEN);
2515
2516 memcpy(buf, firmware->data + sent, size);
2517
2518 pipe = usb_sndbulkpipe(udev, 0x02);
2519 err = usb_bulk_msg(udev, pipe, buf, size, &len,
2520 QCA_DFU_TIMEOUT);
2521 if (err < 0) {
2522 BT_ERR("%s: Failed to send body at %zd of %zd (%d)",
2523 hdev->name, sent, firmware->size, err);
2524 break;
2525 }
2526
2527 if (size != len) {
2528 BT_ERR("%s: Failed to get bulk buffer", hdev->name);
2529 err = -EILSEQ;
2530 break;
2531 }
2532
2533 sent += size;
2534 count -= size;
2535 }
2536
2537done:
2538 kfree(buf);
2539 return err;
2540}
2541
2542static int btusb_setup_qca_load_rampatch(struct hci_dev *hdev,
2543 struct qca_version *ver,
2544 const struct qca_device_info *info)
2545{
2546 struct qca_rampatch_version *rver;
2547 const struct firmware *fw;
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002548 u32 ver_rom, ver_patch;
2549 u16 rver_rom, rver_patch;
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002550 char fwname[64];
2551 int err;
2552
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002553 ver_rom = le32_to_cpu(ver->rom_version);
2554 ver_patch = le32_to_cpu(ver->patch_version);
2555
2556 snprintf(fwname, sizeof(fwname), "qca/rampatch_usb_%08x.bin", ver_rom);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002557
2558 err = request_firmware(&fw, fwname, &hdev->dev);
2559 if (err) {
2560 BT_ERR("%s: failed to request rampatch file: %s (%d)",
2561 hdev->name, fwname, err);
2562 return err;
2563 }
2564
2565 BT_INFO("%s: using rampatch file: %s", hdev->name, fwname);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002566
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002567 rver = (struct qca_rampatch_version *)(fw->data + info->ver_offset);
2568 rver_rom = le16_to_cpu(rver->rom_version);
2569 rver_patch = le16_to_cpu(rver->patch_version);
2570
2571 BT_INFO("%s: QCA: patch rome 0x%x build 0x%x, firmware rome 0x%x "
2572 "build 0x%x", hdev->name, rver_rom, rver_patch, ver_rom,
2573 ver_patch);
2574
2575 if (rver_rom != ver_rom || rver_patch <= ver_patch) {
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002576 BT_ERR("%s: rampatch file version did not match with firmware",
2577 hdev->name);
2578 err = -EINVAL;
2579 goto done;
2580 }
2581
2582 err = btusb_setup_qca_download_fw(hdev, fw, info->rampatch_hdr);
2583
2584done:
2585 release_firmware(fw);
2586
2587 return err;
2588}
2589
2590static int btusb_setup_qca_load_nvm(struct hci_dev *hdev,
2591 struct qca_version *ver,
2592 const struct qca_device_info *info)
2593{
2594 const struct firmware *fw;
2595 char fwname[64];
2596 int err;
2597
2598 snprintf(fwname, sizeof(fwname), "qca/nvm_usb_%08x.bin",
2599 le32_to_cpu(ver->rom_version));
2600
2601 err = request_firmware(&fw, fwname, &hdev->dev);
2602 if (err) {
2603 BT_ERR("%s: failed to request NVM file: %s (%d)",
2604 hdev->name, fwname, err);
2605 return err;
2606 }
2607
2608 BT_INFO("%s: using NVM file: %s", hdev->name, fwname);
2609
2610 err = btusb_setup_qca_download_fw(hdev, fw, info->nvm_hdr);
2611
2612 release_firmware(fw);
2613
2614 return err;
2615}
2616
2617static int btusb_setup_qca(struct hci_dev *hdev)
2618{
2619 const struct qca_device_info *info = NULL;
2620 struct qca_version ver;
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002621 u32 ver_rom;
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002622 u8 status;
2623 int i, err;
2624
2625 err = btusb_qca_send_vendor_req(hdev, QCA_GET_TARGET_VERSION, &ver,
Marcel Holtmanneb500422015-04-16 23:15:50 +02002626 sizeof(ver));
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002627 if (err < 0)
2628 return err;
2629
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002630 ver_rom = le32_to_cpu(ver.rom_version);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002631 for (i = 0; i < ARRAY_SIZE(qca_devices_table); i++) {
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002632 if (ver_rom == qca_devices_table[i].rom_version)
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002633 info = &qca_devices_table[i];
2634 }
2635 if (!info) {
2636 BT_ERR("%s: don't support firmware rome 0x%x", hdev->name,
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002637 ver_rom);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002638 return -ENODEV;
2639 }
2640
2641 err = btusb_qca_send_vendor_req(hdev, QCA_CHECK_STATUS, &status,
2642 sizeof(status));
2643 if (err < 0)
2644 return err;
2645
2646 if (!(status & QCA_PATCH_UPDATED)) {
2647 err = btusb_setup_qca_load_rampatch(hdev, &ver, info);
2648 if (err < 0)
2649 return err;
2650 }
2651
2652 if (!(status & QCA_SYSCFG_UPDATED)) {
2653 err = btusb_setup_qca_load_nvm(hdev, &ver, info);
2654 if (err < 0)
2655 return err;
2656 }
2657
2658 return 0;
2659}
2660
Marcel Holtmann9d08f502015-10-08 20:23:08 +02002661#ifdef CONFIG_BT_HCIBTUSB_BCM
2662static inline int __set_diag_interface(struct hci_dev *hdev)
2663{
2664 struct btusb_data *data = hci_get_drvdata(hdev);
2665 struct usb_interface *intf = data->diag;
2666 int i;
2667
2668 if (!data->diag)
2669 return -ENODEV;
2670
2671 data->diag_tx_ep = NULL;
2672 data->diag_rx_ep = NULL;
2673
2674 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
2675 struct usb_endpoint_descriptor *ep_desc;
2676
2677 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
2678
2679 if (!data->diag_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
2680 data->diag_tx_ep = ep_desc;
2681 continue;
2682 }
2683
2684 if (!data->diag_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
2685 data->diag_rx_ep = ep_desc;
2686 continue;
2687 }
2688 }
2689
2690 if (!data->diag_tx_ep || !data->diag_rx_ep) {
2691 BT_ERR("%s invalid diagnostic descriptors", hdev->name);
2692 return -ENODEV;
2693 }
2694
2695 return 0;
2696}
2697
2698static struct urb *alloc_diag_urb(struct hci_dev *hdev, bool enable)
2699{
2700 struct btusb_data *data = hci_get_drvdata(hdev);
2701 struct sk_buff *skb;
2702 struct urb *urb;
2703 unsigned int pipe;
2704
2705 if (!data->diag_tx_ep)
2706 return ERR_PTR(-ENODEV);
2707
2708 urb = usb_alloc_urb(0, GFP_KERNEL);
2709 if (!urb)
2710 return ERR_PTR(-ENOMEM);
2711
2712 skb = bt_skb_alloc(2, GFP_KERNEL);
2713 if (!skb) {
2714 usb_free_urb(urb);
2715 return ERR_PTR(-ENOMEM);
2716 }
2717
2718 *skb_put(skb, 1) = 0xf0;
2719 *skb_put(skb, 1) = enable;
2720
2721 pipe = usb_sndbulkpipe(data->udev, data->diag_tx_ep->bEndpointAddress);
2722
2723 usb_fill_bulk_urb(urb, data->udev, pipe,
2724 skb->data, skb->len, btusb_tx_complete, skb);
2725
2726 skb->dev = (void *)hdev;
2727
2728 return urb;
2729}
2730
2731static int btusb_bcm_set_diag(struct hci_dev *hdev, bool enable)
2732{
2733 struct btusb_data *data = hci_get_drvdata(hdev);
2734 struct urb *urb;
2735
2736 if (!data->diag)
2737 return -ENODEV;
2738
2739 if (!test_bit(HCI_RUNNING, &hdev->flags))
2740 return -ENETDOWN;
2741
2742 urb = alloc_diag_urb(hdev, enable);
2743 if (IS_ERR(urb))
2744 return PTR_ERR(urb);
2745
2746 return submit_or_queue_tx_urb(hdev, urb);
2747}
2748#endif
2749
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002750static int btusb_probe(struct usb_interface *intf,
Marcel Holtmann89e75332014-09-16 04:44:50 +02002751 const struct usb_device_id *id)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002752{
2753 struct usb_endpoint_descriptor *ep_desc;
2754 struct btusb_data *data;
2755 struct hci_dev *hdev;
2756 int i, err;
2757
2758 BT_DBG("intf %p id %p", intf, id);
2759
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002760 /* interface numbers are hardcoded in the spec */
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002761 if (intf->cur_altsetting->desc.bInterfaceNumber != 0)
2762 return -ENODEV;
2763
2764 if (!id->driver_info) {
2765 const struct usb_device_id *match;
Marcel Holtmann89e75332014-09-16 04:44:50 +02002766
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002767 match = usb_match_id(intf, blacklist_table);
2768 if (match)
2769 id = match;
2770 }
2771
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002772 if (id->driver_info == BTUSB_IGNORE)
2773 return -ENODEV;
2774
Steven.Li2d25f8b2011-07-01 14:02:36 +08002775 if (id->driver_info & BTUSB_ATH3012) {
2776 struct usb_device *udev = interface_to_usbdev(intf);
2777
2778 /* Old firmware would otherwise let ath3k driver load
2779 * patch and sysconfig files */
2780 if (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x0001)
2781 return -ENODEV;
2782 }
2783
Sachin Kamat98921db2012-07-27 12:38:39 +05302784 data = devm_kzalloc(&intf->dev, sizeof(*data), GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002785 if (!data)
2786 return -ENOMEM;
2787
2788 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
2789 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
2790
2791 if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
2792 data->intr_ep = ep_desc;
2793 continue;
2794 }
2795
2796 if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
2797 data->bulk_tx_ep = ep_desc;
2798 continue;
2799 }
2800
2801 if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
2802 data->bulk_rx_ep = ep_desc;
2803 continue;
2804 }
2805 }
2806
Sachin Kamat98921db2012-07-27 12:38:39 +05302807 if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002808 return -ENODEV;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002809
Marcel Holtmann893ba542015-01-28 20:27:34 -08002810 if (id->driver_info & BTUSB_AMP) {
2811 data->cmdreq_type = USB_TYPE_CLASS | 0x01;
2812 data->cmdreq = 0x2b;
2813 } else {
2814 data->cmdreq_type = USB_TYPE_CLASS;
2815 data->cmdreq = 0x00;
2816 }
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002817
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002818 data->udev = interface_to_usbdev(intf);
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02002819 data->intf = intf;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002820
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002821 INIT_WORK(&data->work, btusb_work);
Oliver Neukum7bee5492009-08-24 23:44:59 +02002822 INIT_WORK(&data->waker, btusb_waker);
Marcel Holtmann803b5832014-09-16 08:00:29 +02002823 init_usb_anchor(&data->deferred);
2824 init_usb_anchor(&data->tx_anchor);
Oliver Neukum7bee5492009-08-24 23:44:59 +02002825 spin_lock_init(&data->txlock);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002826
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002827 init_usb_anchor(&data->intr_anchor);
2828 init_usb_anchor(&data->bulk_anchor);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02002829 init_usb_anchor(&data->isoc_anchor);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02002830 init_usb_anchor(&data->diag_anchor);
Marcel Holtmann803b5832014-09-16 08:00:29 +02002831 spin_lock_init(&data->rxlock);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002832
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002833 if (id->driver_info & BTUSB_INTEL_NEW) {
2834 data->recv_event = btusb_recv_event_intel;
2835 data->recv_bulk = btusb_recv_bulk_intel;
2836 set_bit(BTUSB_BOOTLOADER, &data->flags);
2837 } else {
2838 data->recv_event = hci_recv_frame;
2839 data->recv_bulk = btusb_recv_bulk;
2840 }
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +01002841
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002842 hdev = hci_alloc_dev();
Sachin Kamat98921db2012-07-27 12:38:39 +05302843 if (!hdev)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002844 return -ENOMEM;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002845
Marcel Holtmannc13854c2010-02-08 15:27:07 +01002846 hdev->bus = HCI_USB;
David Herrmann155961e2012-02-09 21:58:32 +01002847 hci_set_drvdata(hdev, data);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002848
Marcel Holtmann893ba542015-01-28 20:27:34 -08002849 if (id->driver_info & BTUSB_AMP)
2850 hdev->dev_type = HCI_AMP;
2851 else
2852 hdev->dev_type = HCI_BREDR;
2853
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002854 data->hdev = hdev;
2855
2856 SET_HCIDEV_DEV(hdev, &intf->dev);
2857
Marcel Holtmann9f8f9622013-04-10 08:11:35 -07002858 hdev->open = btusb_open;
2859 hdev->close = btusb_close;
2860 hdev->flush = btusb_flush;
2861 hdev->send = btusb_send_frame;
2862 hdev->notify = btusb_notify;
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 Holtmannc2bfb102015-04-05 22:52:14 -07002872 hdev->setup = btbcm_setup_patchram;
Marcel Holtmann9d08f502015-10-08 20:23:08 +02002873 hdev->set_diag = btusb_bcm_set_diag;
Marcel Holtmann1df1f592015-04-05 22:52:11 -07002874 hdev->set_bdaddr = btbcm_set_bdaddr;
Marcel Holtmann9d08f502015-10-08 20:23:08 +02002875
2876 /* Broadcom LM_DIAG Interface numbers are hardcoded */
2877 data->diag = usb_ifnum_to_if(data->udev, 2);
Marcel Holtmannabbaf502014-07-02 00:53:48 +02002878 }
Petri Gynther10d4c672014-05-08 15:50:01 -07002879
Marcel Holtmann9d08f502015-10-08 20:23:08 +02002880 if (id->driver_info & BTUSB_BCM_APPLE) {
Marcel Holtmannc2bfb102015-04-05 22:52:14 -07002881 hdev->setup = btbcm_setup_apple;
Marcel Holtmann9d08f502015-10-08 20:23:08 +02002882 hdev->set_diag = btusb_bcm_set_diag;
2883
2884 /* Broadcom LM_DIAG Interface numbers are hardcoded */
2885 data->diag = usb_ifnum_to_if(data->udev, 2);
2886 }
Marcel Holtmannc2bfb102015-04-05 22:52:14 -07002887#endif
Marcel Holtmann17b27722015-03-22 15:52:38 +01002888
Marcel Holtmanncb8d6592014-07-02 11:25:25 +02002889 if (id->driver_info & BTUSB_INTEL) {
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002890 hdev->setup = btusb_setup_intel;
Tedd Ho-Jeong Anbfbd45e2015-02-13 09:20:52 -08002891 hdev->shutdown = btusb_shutdown_intel;
Marcel Holtmann3e247672015-10-17 16:00:28 +02002892 hdev->set_diag = btintel_set_diag_mfg;
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07002893 hdev->set_bdaddr = btintel_set_bdaddr;
Jakub Pawlowskic33fb9b2015-01-30 18:55:58 -08002894 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
Jakub Pawlowskic1154842015-03-17 09:04:16 -07002895 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
Marcel Holtmann3e247672015-10-17 16:00:28 +02002896 set_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks);
Marcel Holtmanncb8d6592014-07-02 11:25:25 +02002897 }
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002898
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002899 if (id->driver_info & BTUSB_INTEL_NEW) {
2900 hdev->send = btusb_send_frame_intel;
2901 hdev->setup = btusb_setup_intel_new;
Marcel Holtmanneeb6abe2015-07-05 14:37:39 +02002902 hdev->hw_error = btintel_hw_error;
Marcel Holtmann6d2e50d2015-10-09 14:42:08 +02002903 hdev->set_diag = btintel_set_diag;
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07002904 hdev->set_bdaddr = btintel_set_bdaddr;
Marcel Holtmannb970c5b2015-02-01 23:57:18 -08002905 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
Marcel Holtmannd8270fb2015-10-17 16:00:27 +02002906 set_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002907 }
2908
Amitkumar Karwarae8df492014-07-18 14:47:06 -07002909 if (id->driver_info & BTUSB_MARVELL)
2910 hdev->set_bdaddr = btusb_set_bdaddr_marvell;
2911
Marcel Holtmann661cf882015-01-02 23:35:20 -08002912 if (id->driver_info & BTUSB_SWAVE) {
2913 set_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks);
Marcel Holtmannd57dbe72014-12-26 04:42:33 +01002914 set_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS, &hdev->quirks);
Marcel Holtmann661cf882015-01-02 23:35:20 -08002915 }
Marcel Holtmannd57dbe72014-12-26 04:42:33 +01002916
Marcel Holtmann40df7832014-07-06 13:29:58 +02002917 if (id->driver_info & BTUSB_INTEL_BOOT)
2918 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
2919
Jakub Pawlowski79f0c872015-01-29 10:38:34 -08002920 if (id->driver_info & BTUSB_ATH3012) {
Toshi Kikuchi58592232014-12-12 10:58:05 -08002921 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
Jakub Pawlowski3d50d512015-03-17 09:04:15 -07002922 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
Jakub Pawlowski79f0c872015-01-29 10:38:34 -08002923 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
2924 }
Toshi Kikuchi58592232014-12-12 10:58:05 -08002925
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002926 if (id->driver_info & BTUSB_QCA_ROME) {
2927 data->setup_on_usb = btusb_setup_qca;
2928 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
2929 }
2930
Carlo Caionedb33c772015-05-14 10:49:09 +02002931#ifdef CONFIG_BT_HCIBTUSB_RTL
Daniel Drake04b8c812015-05-21 08:23:50 -06002932 if (id->driver_info & BTUSB_REALTEK) {
Carlo Caionedb33c772015-05-14 10:49:09 +02002933 hdev->setup = btrtl_setup_realtek;
Daniel Drake04b8c812015-05-21 08:23:50 -06002934
2935 /* Realtek devices lose their updated firmware over suspend,
2936 * but the USB hub doesn't notice any status change.
2937 * Explicitly request a device reset on resume.
2938 */
2939 set_bit(BTUSB_RESET_RESUME, &data->flags);
2940 }
Carlo Caionedb33c772015-05-14 10:49:09 +02002941#endif
Daniel Drakea2698a92015-04-16 14:09:55 -06002942
Marcel Holtmann893ba542015-01-28 20:27:34 -08002943 if (id->driver_info & BTUSB_AMP) {
2944 /* AMP controllers do not support SCO packets */
2945 data->isoc = NULL;
2946 } else {
2947 /* Interface numbers are hardcoded in the specification */
2948 data->isoc = usb_ifnum_to_if(data->udev, 1);
2949 }
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02002950
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002951 if (!reset)
Szymon Janca6c511c2012-05-23 12:35:46 +02002952 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002953
2954 if (force_scofix || id->driver_info & BTUSB_WRONG_SCO_MTU) {
2955 if (!disable_scofix)
2956 set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
2957 }
2958
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02002959 if (id->driver_info & BTUSB_BROKEN_ISOC)
2960 data->isoc = NULL;
2961
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002962 if (id->driver_info & BTUSB_DIGIANSWER) {
2963 data->cmdreq_type = USB_TYPE_VENDOR;
Szymon Janca6c511c2012-05-23 12:35:46 +02002964 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002965 }
2966
2967 if (id->driver_info & BTUSB_CSR) {
2968 struct usb_device *udev = data->udev;
Marcel Holtmann81cac642014-01-03 03:02:36 -08002969 u16 bcdDevice = le16_to_cpu(udev->descriptor.bcdDevice);
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002970
2971 /* Old firmware would otherwise execute USB reset */
Marcel Holtmann81cac642014-01-03 03:02:36 -08002972 if (bcdDevice < 0x117)
Szymon Janca6c511c2012-05-23 12:35:46 +02002973 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmann81cac642014-01-03 03:02:36 -08002974
2975 /* Fake CSR devices with broken commands */
Johan Hedberg6cafcd92015-08-30 21:47:21 +03002976 if (bcdDevice <= 0x100 || bcdDevice == 0x134)
Marcel Holtmann81cac642014-01-03 03:02:36 -08002977 hdev->setup = btusb_setup_csr;
Jakub Pawlowski49c989a2015-03-17 09:04:17 -07002978
2979 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002980 }
2981
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002982 if (id->driver_info & BTUSB_SNIFFER) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02002983 struct usb_device *udev = data->udev;
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002984
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002985 /* New sniffer firmware has crippled HCI interface */
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002986 if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
2987 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
2988 }
2989
Marcel Holtmann3a5ef202014-07-06 14:53:54 +02002990 if (id->driver_info & BTUSB_INTEL_BOOT) {
2991 /* A bug in the bootloader causes that interrupt interface is
2992 * only enabled after receiving SetInterface(0, AltSetting=0).
2993 */
2994 err = usb_set_interface(data->udev, 0, 0);
2995 if (err < 0) {
2996 BT_ERR("failed to set interface 0, alt 0 %d", err);
2997 hci_free_dev(hdev);
2998 return err;
2999 }
3000 }
3001
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02003002 if (data->isoc) {
3003 err = usb_driver_claim_interface(&btusb_driver,
Marcel Holtmann89e75332014-09-16 04:44:50 +02003004 data->isoc, data);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02003005 if (err < 0) {
3006 hci_free_dev(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02003007 return err;
3008 }
3009 }
3010
Marcel Holtmann9d08f502015-10-08 20:23:08 +02003011#ifdef CONFIG_BT_HCIBTUSB_BCM
3012 if (data->diag) {
3013 if (!usb_driver_claim_interface(&btusb_driver,
3014 data->diag, data))
3015 __set_diag_interface(hdev);
3016 else
3017 data->diag = NULL;
3018 }
3019#endif
3020
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003021 err = hci_register_dev(hdev);
3022 if (err < 0) {
3023 hci_free_dev(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003024 return err;
3025 }
3026
3027 usb_set_intfdata(intf, data);
3028
3029 return 0;
3030}
3031
3032static void btusb_disconnect(struct usb_interface *intf)
3033{
3034 struct btusb_data *data = usb_get_intfdata(intf);
3035 struct hci_dev *hdev;
3036
3037 BT_DBG("intf %p", intf);
3038
3039 if (!data)
3040 return;
3041
3042 hdev = data->hdev;
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02003043 usb_set_intfdata(data->intf, NULL);
3044
3045 if (data->isoc)
3046 usb_set_intfdata(data->isoc, NULL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003047
Marcel Holtmann9d08f502015-10-08 20:23:08 +02003048 if (data->diag)
3049 usb_set_intfdata(data->diag, NULL);
3050
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003051 hci_unregister_dev(hdev);
3052
Marcel Holtmann9d08f502015-10-08 20:23:08 +02003053 if (intf == data->intf) {
3054 if (data->isoc)
3055 usb_driver_release_interface(&btusb_driver, data->isoc);
3056 if (data->diag)
3057 usb_driver_release_interface(&btusb_driver, data->diag);
3058 } else if (intf == data->isoc) {
3059 if (data->diag)
3060 usb_driver_release_interface(&btusb_driver, data->diag);
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02003061 usb_driver_release_interface(&btusb_driver, data->intf);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02003062 } else if (intf == data->diag) {
3063 usb_driver_release_interface(&btusb_driver, data->intf);
3064 if (data->isoc)
3065 usb_driver_release_interface(&btusb_driver, data->isoc);
3066 }
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02003067
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003068 hci_free_dev(hdev);
3069}
3070
Oliver Neukum7bee5492009-08-24 23:44:59 +02003071#ifdef CONFIG_PM
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003072static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
3073{
3074 struct btusb_data *data = usb_get_intfdata(intf);
3075
3076 BT_DBG("intf %p", intf);
3077
3078 if (data->suspend_count++)
3079 return 0;
3080
Oliver Neukum7bee5492009-08-24 23:44:59 +02003081 spin_lock_irq(&data->txlock);
Alan Stern5b1b0b82011-08-19 23:49:48 +02003082 if (!(PMSG_IS_AUTO(message) && data->tx_in_flight)) {
Oliver Neukum7bee5492009-08-24 23:44:59 +02003083 set_bit(BTUSB_SUSPENDING, &data->flags);
3084 spin_unlock_irq(&data->txlock);
3085 } else {
3086 spin_unlock_irq(&data->txlock);
3087 data->suspend_count--;
3088 return -EBUSY;
3089 }
3090
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003091 cancel_work_sync(&data->work);
3092
Oliver Neukum7bee5492009-08-24 23:44:59 +02003093 btusb_stop_traffic(data);
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003094 usb_kill_anchored_urbs(&data->tx_anchor);
3095
Daniel Drake04b8c812015-05-21 08:23:50 -06003096 /* Optionally request a device reset on resume, but only when
3097 * wakeups are disabled. If wakeups are enabled we assume the
3098 * device will stay powered up throughout suspend.
3099 */
3100 if (test_bit(BTUSB_RESET_RESUME, &data->flags) &&
3101 !device_may_wakeup(&data->udev->dev))
3102 data->udev->reset_resume = 1;
3103
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003104 return 0;
3105}
3106
Oliver Neukum7bee5492009-08-24 23:44:59 +02003107static void play_deferred(struct btusb_data *data)
3108{
3109 struct urb *urb;
3110 int err;
3111
3112 while ((urb = usb_get_from_anchor(&data->deferred))) {
3113 err = usb_submit_urb(urb, GFP_ATOMIC);
3114 if (err < 0)
3115 break;
3116
3117 data->tx_in_flight++;
3118 }
3119 usb_scuttle_anchored_urbs(&data->deferred);
3120}
3121
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003122static int btusb_resume(struct usb_interface *intf)
3123{
3124 struct btusb_data *data = usb_get_intfdata(intf);
3125 struct hci_dev *hdev = data->hdev;
Oliver Neukum7bee5492009-08-24 23:44:59 +02003126 int err = 0;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003127
3128 BT_DBG("intf %p", intf);
3129
3130 if (--data->suspend_count)
3131 return 0;
3132
3133 if (!test_bit(HCI_RUNNING, &hdev->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +02003134 goto done;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003135
3136 if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
3137 err = btusb_submit_intr_urb(hdev, GFP_NOIO);
3138 if (err < 0) {
3139 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02003140 goto failed;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003141 }
3142 }
3143
3144 if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) {
Marcel Holtmann43c2e572009-02-04 17:41:38 +01003145 err = btusb_submit_bulk_urb(hdev, GFP_NOIO);
3146 if (err < 0) {
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003147 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02003148 goto failed;
3149 }
3150
3151 btusb_submit_bulk_urb(hdev, GFP_NOIO);
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003152 }
3153
3154 if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
3155 if (btusb_submit_isoc_urb(hdev, GFP_NOIO) < 0)
3156 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
3157 else
3158 btusb_submit_isoc_urb(hdev, GFP_NOIO);
3159 }
3160
Oliver Neukum7bee5492009-08-24 23:44:59 +02003161 spin_lock_irq(&data->txlock);
3162 play_deferred(data);
3163 clear_bit(BTUSB_SUSPENDING, &data->flags);
3164 spin_unlock_irq(&data->txlock);
3165 schedule_work(&data->work);
3166
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003167 return 0;
Oliver Neukum7bee5492009-08-24 23:44:59 +02003168
3169failed:
3170 usb_scuttle_anchored_urbs(&data->deferred);
3171done:
3172 spin_lock_irq(&data->txlock);
3173 clear_bit(BTUSB_SUSPENDING, &data->flags);
3174 spin_unlock_irq(&data->txlock);
3175
3176 return err;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003177}
Oliver Neukum7bee5492009-08-24 23:44:59 +02003178#endif
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003179
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003180static struct usb_driver btusb_driver = {
3181 .name = "btusb",
3182 .probe = btusb_probe,
3183 .disconnect = btusb_disconnect,
Oliver Neukum7bee5492009-08-24 23:44:59 +02003184#ifdef CONFIG_PM
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003185 .suspend = btusb_suspend,
3186 .resume = btusb_resume,
Oliver Neukum7bee5492009-08-24 23:44:59 +02003187#endif
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003188 .id_table = btusb_table,
Oliver Neukum7bee5492009-08-24 23:44:59 +02003189 .supports_autosuspend = 1,
Sarah Sharpe1f12eb2012-04-23 10:08:51 -07003190 .disable_hub_initiated_lpm = 1,
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003191};
3192
Greg Kroah-Hartman93f15082011-11-18 09:47:34 -08003193module_usb_driver(btusb_driver);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003194
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02003195module_param(disable_scofix, bool, 0644);
3196MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
3197
3198module_param(force_scofix, bool, 0644);
3199MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
3200
3201module_param(reset, bool, 0644);
3202MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
3203
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003204MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
3205MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
3206MODULE_VERSION(VERSION);
3207MODULE_LICENSE("GPL");