blob: 6f799c42cf2c72180b75369399a1f0aca274828b [file] [log] [blame]
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001/*
2 *
3 * Generic Bluetooth USB driver
4 *
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02005 * Copyright (C) 2005-2008 Marcel Holtmann <marcel@holtmann.org>
Marcel Holtmann5e23b922007-10-20 14:12:34 +02006 *
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 */
23
Marcel Holtmann5e23b922007-10-20 14:12:34 +020024#include <linux/module.h>
Marcel Holtmann5e23b922007-10-20 14:12:34 +020025#include <linux/usb.h>
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -070026#include <linux/firmware.h>
Daniel Drakea2698a92015-04-16 14:09:55 -060027#include <asm/unaligned.h>
Marcel Holtmann5e23b922007-10-20 14:12:34 +020028
29#include <net/bluetooth/bluetooth.h>
30#include <net/bluetooth/hci_core.h>
31
Marcel Holtmann4185a0f2015-04-06 00:52:12 -070032#include "btintel.h"
Marcel Holtmann1df1f592015-04-05 22:52:11 -070033#include "btbcm.h"
Carlo Caionedb33c772015-05-14 10:49:09 +020034#include "btrtl.h"
Marcel Holtmann1df1f592015-04-05 22:52:11 -070035
Marcel Holtmann34dced9b2015-04-05 22:52:16 -070036#define VERSION "0.8"
Marcel Holtmanncfeb4142008-08-07 22:26:56 +020037
Rusty Russell90ab5ee2012-01-13 09:32:20 +103038static bool disable_scofix;
39static bool force_scofix;
Marcel Holtmann7a9d4022008-11-30 12:17:26 +010040
Shailendra Verma917a3332015-05-25 23:53:40 +053041static bool reset = true;
Marcel Holtmanncfeb4142008-08-07 22:26:56 +020042
43static struct usb_driver btusb_driver;
44
45#define BTUSB_IGNORE 0x01
Marcel Holtmann7a9d4022008-11-30 12:17:26 +010046#define BTUSB_DIGIANSWER 0x02
47#define BTUSB_CSR 0x04
48#define BTUSB_SNIFFER 0x08
49#define BTUSB_BCM92035 0x10
50#define BTUSB_BROKEN_ISOC 0x20
51#define BTUSB_WRONG_SCO_MTU 0x40
Steven.Li2d25f8b2011-07-01 14:02:36 +080052#define BTUSB_ATH3012 0x80
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -070053#define BTUSB_INTEL 0x100
Marcel Holtmann40df7832014-07-06 13:29:58 +020054#define BTUSB_INTEL_BOOT 0x200
55#define BTUSB_BCM_PATCHRAM 0x400
Amitkumar Karwarae8df492014-07-18 14:47:06 -070056#define BTUSB_MARVELL 0x800
Marcel Holtmann4fcef8e2015-01-01 17:34:37 -080057#define BTUSB_SWAVE 0x1000
Marcel Holtmanncda0dd72015-01-26 21:33:48 -080058#define BTUSB_INTEL_NEW 0x2000
Marcel Holtmann893ba542015-01-28 20:27:34 -080059#define BTUSB_AMP 0x4000
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +000060#define BTUSB_QCA_ROME 0x8000
Marcel Holtmann17b27722015-03-22 15:52:38 +010061#define BTUSB_BCM_APPLE 0x10000
Daniel Drakea2698a92015-04-16 14:09:55 -060062#define BTUSB_REALTEK 0x20000
Marcel Holtmann5e23b922007-10-20 14:12:34 +020063
Marcel Holtmann54265202013-10-11 07:46:18 -070064static const struct usb_device_id btusb_table[] = {
Marcel Holtmann5e23b922007-10-20 14:12:34 +020065 /* Generic Bluetooth USB device */
66 { USB_DEVICE_INFO(0xe0, 0x01, 0x01) },
67
Marcel Holtmann893ba542015-01-28 20:27:34 -080068 /* Generic Bluetooth AMP device */
69 { USB_DEVICE_INFO(0xe0, 0x01, 0x04), .driver_info = BTUSB_AMP },
70
Daniel Draked63b2822015-07-17 11:12:25 -060071 /* Generic Bluetooth USB interface */
72 { USB_INTERFACE_INFO(0xe0, 0x01, 0x01) },
73
Henrik Rydberg1fa65352012-08-25 19:28:06 +020074 /* Apple-specific (Broadcom) devices */
Marcel Holtmann17b27722015-03-22 15:52:38 +010075 { USB_VENDOR_AND_INTERFACE_INFO(0x05ac, 0xff, 0x01, 0x01),
76 .driver_info = BTUSB_BCM_APPLE },
Henrik Rydberg1fa65352012-08-25 19:28:06 +020077
Cho, Yu-Chen178c0592013-06-04 21:40:26 +080078 /* MediaTek MT76x0E */
79 { USB_DEVICE(0x0e8d, 0x763f) },
80
Oliver Neukumc510eae2011-09-21 11:41:45 +020081 /* Broadcom SoftSailing reporting vendor specific */
Don Zickus2e8b50632012-03-28 16:41:11 -040082 { USB_DEVICE(0x0a5c, 0x21e1) },
Oliver Neukumc510eae2011-09-21 11:41:45 +020083
Nobuhiro Iwamatsu3cd01972010-08-20 16:24:07 +090084 /* Apple MacBookPro 7,1 */
85 { USB_DEVICE(0x05ac, 0x8213) },
86
Cyril Lacoux0a79f672010-07-14 10:29:27 +040087 /* Apple iMac11,1 */
88 { USB_DEVICE(0x05ac, 0x8215) },
89
Nobuhiro Iwamatsu9c047152010-08-20 16:24:06 +090090 /* Apple MacBookPro6,2 */
91 { USB_DEVICE(0x05ac, 0x8218) },
92
Edgar (gimli) Hucek3e3ede72010-11-04 08:04:33 +010093 /* Apple MacBookAir3,1, MacBookAir3,2 */
94 { USB_DEVICE(0x05ac, 0x821b) },
95
Pieter-Augustijn Van Malleghema63b7232011-09-07 02:28:10 -040096 /* Apple MacBookAir4,1 */
97 { USB_DEVICE(0x05ac, 0x821f) },
98
Marc-Antoine Perennou88d377b2011-03-24 14:51:21 -030099 /* Apple MacBookPro8,2 */
100 { USB_DEVICE(0x05ac, 0x821a) },
101
Jurgen Kramerf78b68262011-09-04 18:01:42 +0200102 /* Apple MacMini5,1 */
103 { USB_DEVICE(0x05ac, 0x8281) },
104
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200105 /* AVM BlueFRITZ! USB v2.0 */
Marcel Holtmann4fcef8e2015-01-01 17:34:37 -0800106 { USB_DEVICE(0x057c, 0x3800), .driver_info = BTUSB_SWAVE },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200107
108 /* Bluetooth Ultraport Module from IBM */
109 { USB_DEVICE(0x04bf, 0x030a) },
110
111 /* ALPS Modules with non-standard id */
112 { USB_DEVICE(0x044e, 0x3001) },
113 { USB_DEVICE(0x044e, 0x3002) },
114
115 /* Ericsson with non-standard id */
116 { USB_DEVICE(0x0bdb, 0x1002) },
117
118 /* Canyon CN-BTU1 with HID interfaces */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100119 { USB_DEVICE(0x0c10, 0x0000) },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200120
Wen-chien Jesse Sungd13431c2011-11-08 14:30:22 +0800121 /* Broadcom BCM20702A0 */
122 { USB_DEVICE(0x413c, 0x8197) },
123
Marcel Holtmannd049f4e2015-01-26 20:35:32 -0800124 /* Broadcom BCM20702B0 (Dynex/Insignia) */
125 { USB_DEVICE(0x19ff, 0x0239), .driver_info = BTUSB_BCM_PATCHRAM },
126
Steven Harms98514032012-04-13 14:45:55 -0400127 /* Foxconn - Hon Hai */
Heinrich Siebmanns6029ddc2014-12-03 19:32:22 +0100128 { USB_VENDOR_AND_INTERFACE_INFO(0x0489, 0xff, 0x01, 0x01),
129 .driver_info = BTUSB_BCM_PATCHRAM },
Steven Harms98514032012-04-13 14:45:55 -0400130
Matej Dubovy8f0c3042015-02-02 18:50:14 +0100131 /* Lite-On Technology - Broadcom based */
132 { USB_VENDOR_AND_INTERFACE_INFO(0x04ca, 0xff, 0x01, 0x01),
133 .driver_info = BTUSB_BCM_PATCHRAM },
134
Andy Shevchenko0b880062014-02-18 18:26:19 +0200135 /* Broadcom devices with vendor specific id */
Petri Gynther10d4c672014-05-08 15:50:01 -0700136 { USB_VENDOR_AND_INTERFACE_INFO(0x0a5c, 0xff, 0x01, 0x01),
137 .driver_info = BTUSB_BCM_PATCHRAM },
Gustavo Padovan92c385f2012-08-06 15:36:49 -0300138
Marcel Holtmannc2aef6e2014-07-21 14:02:33 +0200139 /* ASUSTek Computer - Broadcom based */
Rick Dunn9a5abda2015-01-17 05:29:12 +0100140 { USB_VENDOR_AND_INTERFACE_INFO(0x0b05, 0xff, 0x01, 0x01),
141 .driver_info = BTUSB_BCM_PATCHRAM },
Marcel Holtmannc2aef6e2014-07-21 14:02:33 +0200142
Ken O'Brien5bcecf32013-09-21 19:14:43 +0100143 /* Belkin F8065bf - Broadcom based */
Marcel Holtmann6331c682015-03-27 15:11:41 -0700144 { USB_VENDOR_AND_INTERFACE_INFO(0x050d, 0xff, 0x01, 0x01),
145 .driver_info = BTUSB_BCM_PATCHRAM },
Ken O'Brien5bcecf32013-09-21 19:14:43 +0100146
Jurgen Kramer9113bfd2014-02-15 12:01:09 +0100147 /* IMC Networks - Broadcom based */
Marcel Holtmann6331c682015-03-27 15:11:41 -0700148 { USB_VENDOR_AND_INTERFACE_INFO(0x13d3, 0xff, 0x01, 0x01),
149 .driver_info = BTUSB_BCM_PATCHRAM },
Jurgen Kramer9113bfd2014-02-15 12:01:09 +0100150
Marcel Holtmann40df7832014-07-06 13:29:58 +0200151 /* Intel Bluetooth USB Bootloader (RAM module) */
Marcel Holtmannd92f2df2014-07-06 14:53:55 +0200152 { USB_DEVICE(0x8087, 0x0a5a),
153 .driver_info = BTUSB_INTEL_BOOT | BTUSB_BROKEN_ISOC },
Marcel Holtmann40df7832014-07-06 13:29:58 +0200154
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200155 { } /* Terminating entry */
156};
157
158MODULE_DEVICE_TABLE(usb, btusb_table);
159
Marcel Holtmann54265202013-10-11 07:46:18 -0700160static const struct usb_device_id blacklist_table[] = {
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200161 /* CSR BlueCore devices */
162 { USB_DEVICE(0x0a12, 0x0001), .driver_info = BTUSB_CSR },
163
164 /* Broadcom BCM2033 without firmware */
165 { USB_DEVICE(0x0a5c, 0x2033), .driver_info = BTUSB_IGNORE },
166
Bala Shanmugambe931122010-11-26 17:35:46 +0530167 /* Atheros 3011 with sflash firmware */
Andy Shevchenko0b880062014-02-18 18:26:19 +0200168 { USB_DEVICE(0x0489, 0xe027), .driver_info = BTUSB_IGNORE },
169 { USB_DEVICE(0x0489, 0xe03d), .driver_info = BTUSB_IGNORE },
Alexander Ploumistos2eeff0b2015-02-13 21:05:11 +0200170 { USB_DEVICE(0x04f2, 0xaff1), .driver_info = BTUSB_IGNORE },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200171 { USB_DEVICE(0x0930, 0x0215), .driver_info = BTUSB_IGNORE },
Bala Shanmugambe931122010-11-26 17:35:46 +0530172 { USB_DEVICE(0x0cf3, 0x3002), .driver_info = BTUSB_IGNORE },
Marek Vasut6eda5412012-06-08 14:32:50 +0200173 { USB_DEVICE(0x0cf3, 0xe019), .driver_info = BTUSB_IGNORE },
Andy Ross2a7bccc2011-05-09 16:11:16 -0700174 { USB_DEVICE(0x13d3, 0x3304), .driver_info = BTUSB_IGNORE },
Bala Shanmugambe931122010-11-26 17:35:46 +0530175
Cho, Yu-Chen509e7862011-01-26 17:10:59 +0800176 /* Atheros AR9285 Malbec with sflash firmware */
177 { USB_DEVICE(0x03f0, 0x311d), .driver_info = BTUSB_IGNORE },
178
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530179 /* Atheros 3012 with sflash firmware */
Andy Shevchenko0b880062014-02-18 18:26:19 +0200180 { USB_DEVICE(0x0489, 0xe04d), .driver_info = BTUSB_ATH3012 },
181 { USB_DEVICE(0x0489, 0xe04e), .driver_info = BTUSB_ATH3012 },
182 { USB_DEVICE(0x0489, 0xe056), .driver_info = BTUSB_ATH3012 },
183 { USB_DEVICE(0x0489, 0xe057), .driver_info = BTUSB_ATH3012 },
184 { USB_DEVICE(0x0489, 0xe05f), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin692c0622015-06-06 20:25:40 +0300185 { USB_DEVICE(0x0489, 0xe076), .driver_info = BTUSB_ATH3012 },
Anantha Krishnan4b552bc2014-10-06 16:31:49 +0530186 { USB_DEVICE(0x0489, 0xe078), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200187 { USB_DEVICE(0x04c5, 0x1330), .driver_info = BTUSB_ATH3012 },
188 { USB_DEVICE(0x04ca, 0x3004), .driver_info = BTUSB_ATH3012 },
189 { USB_DEVICE(0x04ca, 0x3005), .driver_info = BTUSB_ATH3012 },
190 { USB_DEVICE(0x04ca, 0x3006), .driver_info = BTUSB_ATH3012 },
Mohammed Habibulla1fb4e092014-04-17 11:37:13 -0700191 { USB_DEVICE(0x04ca, 0x3007), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200192 { USB_DEVICE(0x04ca, 0x3008), .driver_info = BTUSB_ATH3012 },
193 { USB_DEVICE(0x04ca, 0x300b), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin7e730c72015-06-18 20:41:51 +0300194 { USB_DEVICE(0x04ca, 0x300d), .driver_info = BTUSB_ATH3012 },
Dmitry Tuninec0810d2015-05-02 13:36:58 +0300195 { USB_DEVICE(0x04ca, 0x300f), .driver_info = BTUSB_ATH3012 },
Janne Heikkinen134d3b32014-12-09 07:44:51 +0200196 { USB_DEVICE(0x04ca, 0x3010), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200197 { USB_DEVICE(0x0930, 0x0219), .driver_info = BTUSB_ATH3012 },
198 { USB_DEVICE(0x0930, 0x0220), .driver_info = BTUSB_ATH3012 },
Vincent Zwanenburg89d29752014-08-08 12:33:56 +0100199 { USB_DEVICE(0x0930, 0x0227), .driver_info = BTUSB_ATH3012 },
Andy Shevchenkoa735f9e2014-02-18 18:26:20 +0200200 { USB_DEVICE(0x0b05, 0x17d0), .driver_info = BTUSB_ATH3012 },
Ming Leid66629c2013-03-15 11:00:39 +0800201 { USB_DEVICE(0x0cf3, 0x0036), .driver_info = BTUSB_ATH3012 },
Steven.Li2d25f8b2011-07-01 14:02:36 +0800202 { USB_DEVICE(0x0cf3, 0x3004), .driver_info = BTUSB_ATH3012 },
Sunguk Lee94a32d102013-03-12 04:41:58 +0900203 { USB_DEVICE(0x0cf3, 0x3008), .driver_info = BTUSB_ATH3012 },
Cho, Yu-Chen07c0ea82012-03-14 22:01:21 +0200204 { USB_DEVICE(0x0cf3, 0x311d), .driver_info = BTUSB_ATH3012 },
Oliver Neukumb1312372014-01-16 15:37:11 +0100205 { USB_DEVICE(0x0cf3, 0x311e), .driver_info = BTUSB_ATH3012 },
Oliver Neukum1e56f1e2014-01-16 16:02:58 +0100206 { USB_DEVICE(0x0cf3, 0x311f), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200207 { USB_DEVICE(0x0cf3, 0x3121), .driver_info = BTUSB_ATH3012 },
Ming Leiebaf5792013-03-18 23:45:11 +0800208 { USB_DEVICE(0x0cf3, 0x817a), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200209 { USB_DEVICE(0x0cf3, 0xe003), .driver_info = BTUSB_ATH3012 },
AceLan Kaoac713112012-04-19 14:53:45 +0800210 { USB_DEVICE(0x0cf3, 0xe004), .driver_info = BTUSB_ATH3012 },
Peng Chen0a3658c2013-08-30 17:41:40 +0800211 { USB_DEVICE(0x0cf3, 0xe005), .driver_info = BTUSB_ATH3012 },
Wen-chien Jesse Sungca79f232015-05-13 11:39:24 +0800212 { USB_DEVICE(0x0cf3, 0xe006), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200213 { USB_DEVICE(0x13d3, 0x3362), .driver_info = BTUSB_ATH3012 },
214 { USB_DEVICE(0x13d3, 0x3375), .driver_info = BTUSB_ATH3012 },
AceLan Kaoeed307e2012-12-11 11:41:20 +0800215 { USB_DEVICE(0x13d3, 0x3393), .driver_info = BTUSB_ATH3012 },
Sujith Manoharan5b77a1f2013-07-15 09:29:03 +0530216 { USB_DEVICE(0x13d3, 0x3402), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin3bb30a7c2014-11-25 20:19:52 +0300217 { USB_DEVICE(0x13d3, 0x3408), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin033efa92015-01-18 00:16:51 +0300218 { USB_DEVICE(0x13d3, 0x3423), .driver_info = BTUSB_ATH3012 },
Anantha Krishnanfa2f1392014-07-08 19:25:08 +0530219 { USB_DEVICE(0x13d3, 0x3432), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin0d0cef62015-06-06 20:29:25 +0300220 { USB_DEVICE(0x13d3, 0x3474), .driver_info = BTUSB_ATH3012 },
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530221
Cho, Yu-Chene9036e32011-02-15 10:20:07 +0800222 /* Atheros AR5BBU12 with sflash firmware */
223 { USB_DEVICE(0x0489, 0xe02c), .driver_info = BTUSB_IGNORE },
224
Michael Gruetzner85d59722012-05-02 22:33:40 +0200225 /* Atheros AR5BBU12 with sflash firmware */
Yevgeniy Melnichukbc21fde2012-08-07 19:48:10 +0530226 { USB_DEVICE(0x0489, 0xe036), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200227 { USB_DEVICE(0x0489, 0xe03c), .driver_info = BTUSB_ATH3012 },
Michael Gruetzner85d59722012-05-02 22:33:40 +0200228
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +0000229 /* QCA ROME chipset */
Wen-chien Jesse Sung20541112015-05-13 11:39:25 +0800230 { USB_DEVICE(0x0cf3, 0xe007), .driver_info = BTUSB_QCA_ROME },
Marcel Holtmannc9e44472015-03-16 23:56:04 -0700231 { USB_DEVICE(0x0cf3, 0xe300), .driver_info = BTUSB_QCA_ROME },
232 { USB_DEVICE(0x0cf3, 0xe360), .driver_info = BTUSB_QCA_ROME },
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +0000233
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200234 /* Broadcom BCM2035 */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100235 { USB_DEVICE(0x0a5c, 0x2009), .driver_info = BTUSB_BCM92035 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200236 { USB_DEVICE(0x0a5c, 0x200a), .driver_info = BTUSB_WRONG_SCO_MTU },
237 { USB_DEVICE(0x0a5c, 0x2035), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200238
239 /* Broadcom BCM2045 */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100240 { USB_DEVICE(0x0a5c, 0x2039), .driver_info = BTUSB_WRONG_SCO_MTU },
241 { USB_DEVICE(0x0a5c, 0x2101), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmannbdbef3d2008-09-23 00:16:35 +0200242
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200243 /* IBM/Lenovo ThinkPad with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100244 { USB_DEVICE(0x0a5c, 0x201e), .driver_info = BTUSB_WRONG_SCO_MTU },
245 { USB_DEVICE(0x0a5c, 0x2110), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200246
247 /* HP laptop with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100248 { USB_DEVICE(0x03f0, 0x171d), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200249
250 /* Dell laptop with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100251 { USB_DEVICE(0x413c, 0x8126), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200252
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100253 /* Dell Wireless 370 and 410 devices */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100254 { USB_DEVICE(0x413c, 0x8152), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100255 { USB_DEVICE(0x413c, 0x8156), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200256
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100257 /* Belkin F8T012 and F8T013 devices */
258 { USB_DEVICE(0x050d, 0x0012), .driver_info = BTUSB_WRONG_SCO_MTU },
259 { USB_DEVICE(0x050d, 0x0013), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200260
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100261 /* Asus WL-BTD202 device */
262 { USB_DEVICE(0x0b05, 0x1715), .driver_info = BTUSB_WRONG_SCO_MTU },
263
264 /* Kensington Bluetooth USB adapter */
265 { USB_DEVICE(0x047d, 0x105e), .driver_info = BTUSB_WRONG_SCO_MTU },
266
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200267 /* RTX Telecom based adapters with buggy SCO support */
268 { USB_DEVICE(0x0400, 0x0807), .driver_info = BTUSB_BROKEN_ISOC },
269 { USB_DEVICE(0x0400, 0x080a), .driver_info = BTUSB_BROKEN_ISOC },
270
271 /* CONWISE Technology based adapters with buggy SCO support */
272 { USB_DEVICE(0x0e5e, 0x6622), .driver_info = BTUSB_BROKEN_ISOC },
273
Marcel Holtmann4fcef8e2015-01-01 17:34:37 -0800274 /* Roper Class 1 Bluetooth Dongle (Silicon Wave based) */
Aleksei Volkov2eeac872015-06-08 12:02:10 +0300275 { USB_DEVICE(0x1310, 0x0001), .driver_info = BTUSB_SWAVE },
Marcel Holtmann4fcef8e2015-01-01 17:34:37 -0800276
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200277 /* Digianswer devices */
278 { USB_DEVICE(0x08fd, 0x0001), .driver_info = BTUSB_DIGIANSWER },
279 { USB_DEVICE(0x08fd, 0x0002), .driver_info = BTUSB_IGNORE },
280
281 /* CSR BlueCore Bluetooth Sniffer */
Marcel Holtmann4f64fa82014-07-07 00:12:04 +0200282 { USB_DEVICE(0x0a12, 0x0002),
283 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200284
285 /* Frontline ComProbe Bluetooth Sniffer */
Marcel Holtmann4f64fa82014-07-07 00:12:04 +0200286 { USB_DEVICE(0x16d3, 0x0002),
287 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200288
Marcel Holtmanncb1ee892015-01-28 19:41:42 -0800289 /* Marvell Bluetooth devices */
290 { USB_DEVICE(0x1286, 0x2044), .driver_info = BTUSB_MARVELL },
291 { USB_DEVICE(0x1286, 0x2046), .driver_info = BTUSB_MARVELL },
292
Marcel Holtmannd0ac9eb2015-01-28 19:41:43 -0800293 /* Intel Bluetooth devices */
Marcel Holtmann407550f2015-02-22 15:41:18 -0800294 { USB_DEVICE(0x8087, 0x07da), .driver_info = BTUSB_CSR },
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -0700295 { USB_DEVICE(0x8087, 0x07dc), .driver_info = BTUSB_INTEL },
Tedd Ho-Jeong Anef4e5e42013-11-12 13:10:58 -0800296 { USB_DEVICE(0x8087, 0x0a2a), .driver_info = BTUSB_INTEL },
Marcel Holtmanncda0dd72015-01-26 21:33:48 -0800297 { USB_DEVICE(0x8087, 0x0a2b), .driver_info = BTUSB_INTEL_NEW },
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -0700298
Marcel Holtmannd0ac9eb2015-01-28 19:41:43 -0800299 /* Other Intel Bluetooth devices */
300 { USB_VENDOR_AND_INTERFACE_INFO(0x8087, 0xe0, 0x01, 0x01),
301 .driver_info = BTUSB_IGNORE },
Amitkumar Karwarae8df492014-07-18 14:47:06 -0700302
Daniel Drakea2698a92015-04-16 14:09:55 -0600303 /* Realtek Bluetooth devices */
304 { USB_VENDOR_AND_INTERFACE_INFO(0x0bda, 0xe0, 0x01, 0x01),
305 .driver_info = BTUSB_REALTEK },
306
307 /* Additional Realtek 8723AE Bluetooth devices */
308 { USB_DEVICE(0x0930, 0x021d), .driver_info = BTUSB_REALTEK },
309 { USB_DEVICE(0x13d3, 0x3394), .driver_info = BTUSB_REALTEK },
310
311 /* Additional Realtek 8723BE Bluetooth devices */
312 { USB_DEVICE(0x0489, 0xe085), .driver_info = BTUSB_REALTEK },
313 { USB_DEVICE(0x0489, 0xe08b), .driver_info = BTUSB_REALTEK },
314 { USB_DEVICE(0x13d3, 0x3410), .driver_info = BTUSB_REALTEK },
315 { USB_DEVICE(0x13d3, 0x3416), .driver_info = BTUSB_REALTEK },
316 { USB_DEVICE(0x13d3, 0x3459), .driver_info = BTUSB_REALTEK },
317
318 /* Additional Realtek 8821AE Bluetooth devices */
319 { USB_DEVICE(0x0b05, 0x17dc), .driver_info = BTUSB_REALTEK },
320 { USB_DEVICE(0x13d3, 0x3414), .driver_info = BTUSB_REALTEK },
321 { USB_DEVICE(0x13d3, 0x3458), .driver_info = BTUSB_REALTEK },
322 { USB_DEVICE(0x13d3, 0x3461), .driver_info = BTUSB_REALTEK },
323 { USB_DEVICE(0x13d3, 0x3462), .driver_info = BTUSB_REALTEK },
324
Peter Poklop4481c072015-08-15 20:47:09 +0200325 /* Silicon Wave based devices */
326 { USB_DEVICE(0x0c10, 0x0000), .driver_info = BTUSB_SWAVE },
327
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200328 { } /* Terminating entry */
329};
330
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200331#define BTUSB_MAX_ISOC_FRAMES 10
332
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200333#define BTUSB_INTR_RUNNING 0
334#define BTUSB_BULK_RUNNING 1
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200335#define BTUSB_ISOC_RUNNING 2
Oliver Neukum7bee5492009-08-24 23:44:59 +0200336#define BTUSB_SUSPENDING 3
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -0300337#define BTUSB_DID_ISO_RESUME 4
Marcel Holtmanncda0dd72015-01-26 21:33:48 -0800338#define BTUSB_BOOTLOADER 5
339#define BTUSB_DOWNLOADING 6
Marcel Holtmannce6bb922015-01-28 01:58:40 -0800340#define BTUSB_FIRMWARE_LOADED 7
Marcel Holtmanncda0dd72015-01-26 21:33:48 -0800341#define BTUSB_FIRMWARE_FAILED 8
Marcel Holtmannce6bb922015-01-28 01:58:40 -0800342#define BTUSB_BOOTING 9
Daniel Drake04b8c812015-05-21 08:23:50 -0600343#define BTUSB_RESET_RESUME 10
Marcel Holtmann9d08f502015-10-08 20:23:08 +0200344#define BTUSB_DIAG_RUNNING 11
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200345
346struct btusb_data {
347 struct hci_dev *hdev;
348 struct usb_device *udev;
Marcel Holtmann5fbcd262008-09-23 00:16:36 +0200349 struct usb_interface *intf;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200350 struct usb_interface *isoc;
Marcel Holtmann9d08f502015-10-08 20:23:08 +0200351 struct usb_interface *diag;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200352
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200353 unsigned long flags;
354
355 struct work_struct work;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200356 struct work_struct waker;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200357
Marcel Holtmann803b5832014-09-16 08:00:29 +0200358 struct usb_anchor deferred;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200359 struct usb_anchor tx_anchor;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200360 int tx_in_flight;
361 spinlock_t txlock;
362
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200363 struct usb_anchor intr_anchor;
364 struct usb_anchor bulk_anchor;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200365 struct usb_anchor isoc_anchor;
Marcel Holtmann9d08f502015-10-08 20:23:08 +0200366 struct usb_anchor diag_anchor;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200367 spinlock_t rxlock;
368
369 struct sk_buff *evt_skb;
370 struct sk_buff *acl_skb;
371 struct sk_buff *sco_skb;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200372
373 struct usb_endpoint_descriptor *intr_ep;
374 struct usb_endpoint_descriptor *bulk_tx_ep;
375 struct usb_endpoint_descriptor *bulk_rx_ep;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200376 struct usb_endpoint_descriptor *isoc_tx_ep;
377 struct usb_endpoint_descriptor *isoc_rx_ep;
Marcel Holtmann9d08f502015-10-08 20:23:08 +0200378 struct usb_endpoint_descriptor *diag_tx_ep;
379 struct usb_endpoint_descriptor *diag_rx_ep;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200380
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100381 __u8 cmdreq_type;
Marcel Holtmann893ba542015-01-28 20:27:34 -0800382 __u8 cmdreq;
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100383
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100384 unsigned int sco_num;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200385 int isoc_altsetting;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +0100386 int suspend_count;
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +0100387
Marcel Holtmann97307f52015-01-12 13:51:10 -0800388 int (*recv_event)(struct hci_dev *hdev, struct sk_buff *skb);
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +0100389 int (*recv_bulk)(struct btusb_data *data, void *buffer, int count);
Kim, Ben Young Taeace31982015-02-15 23:06:14 +0000390
391 int (*setup_on_usb)(struct hci_dev *hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200392};
393
Marcel Holtmann803b5832014-09-16 08:00:29 +0200394static inline void btusb_free_frags(struct btusb_data *data)
395{
396 unsigned long flags;
397
398 spin_lock_irqsave(&data->rxlock, flags);
399
400 kfree_skb(data->evt_skb);
401 data->evt_skb = NULL;
402
403 kfree_skb(data->acl_skb);
404 data->acl_skb = NULL;
405
406 kfree_skb(data->sco_skb);
407 data->sco_skb = NULL;
408
409 spin_unlock_irqrestore(&data->rxlock, flags);
410}
411
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200412static int btusb_recv_intr(struct btusb_data *data, void *buffer, int count)
413{
Marcel Holtmann803b5832014-09-16 08:00:29 +0200414 struct sk_buff *skb;
415 int err = 0;
416
417 spin_lock(&data->rxlock);
418 skb = data->evt_skb;
419
420 while (count) {
421 int len;
422
423 if (!skb) {
424 skb = bt_skb_alloc(HCI_MAX_EVENT_SIZE, GFP_ATOMIC);
425 if (!skb) {
426 err = -ENOMEM;
427 break;
428 }
429
430 bt_cb(skb)->pkt_type = HCI_EVENT_PKT;
431 bt_cb(skb)->expect = HCI_EVENT_HDR_SIZE;
432 }
433
434 len = min_t(uint, bt_cb(skb)->expect, count);
435 memcpy(skb_put(skb, len), buffer, len);
436
437 count -= len;
438 buffer += len;
439 bt_cb(skb)->expect -= len;
440
441 if (skb->len == HCI_EVENT_HDR_SIZE) {
442 /* Complete event header */
443 bt_cb(skb)->expect = hci_event_hdr(skb)->plen;
444
445 if (skb_tailroom(skb) < bt_cb(skb)->expect) {
446 kfree_skb(skb);
447 skb = NULL;
448
449 err = -EILSEQ;
450 break;
451 }
452 }
453
454 if (bt_cb(skb)->expect == 0) {
455 /* Complete frame */
Marcel Holtmann97307f52015-01-12 13:51:10 -0800456 data->recv_event(data->hdev, skb);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200457 skb = NULL;
458 }
459 }
460
461 data->evt_skb = skb;
462 spin_unlock(&data->rxlock);
463
464 return err;
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200465}
466
467static int btusb_recv_bulk(struct btusb_data *data, void *buffer, int count)
468{
Marcel Holtmann803b5832014-09-16 08:00:29 +0200469 struct sk_buff *skb;
470 int err = 0;
471
472 spin_lock(&data->rxlock);
473 skb = data->acl_skb;
474
475 while (count) {
476 int len;
477
478 if (!skb) {
479 skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
480 if (!skb) {
481 err = -ENOMEM;
482 break;
483 }
484
485 bt_cb(skb)->pkt_type = HCI_ACLDATA_PKT;
486 bt_cb(skb)->expect = HCI_ACL_HDR_SIZE;
487 }
488
489 len = min_t(uint, bt_cb(skb)->expect, count);
490 memcpy(skb_put(skb, len), buffer, len);
491
492 count -= len;
493 buffer += len;
494 bt_cb(skb)->expect -= len;
495
496 if (skb->len == HCI_ACL_HDR_SIZE) {
497 __le16 dlen = hci_acl_hdr(skb)->dlen;
498
499 /* Complete ACL header */
500 bt_cb(skb)->expect = __le16_to_cpu(dlen);
501
502 if (skb_tailroom(skb) < bt_cb(skb)->expect) {
503 kfree_skb(skb);
504 skb = NULL;
505
506 err = -EILSEQ;
507 break;
508 }
509 }
510
511 if (bt_cb(skb)->expect == 0) {
512 /* Complete frame */
513 hci_recv_frame(data->hdev, skb);
514 skb = NULL;
515 }
516 }
517
518 data->acl_skb = skb;
519 spin_unlock(&data->rxlock);
520
521 return err;
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200522}
523
524static int btusb_recv_isoc(struct btusb_data *data, void *buffer, int count)
525{
Marcel Holtmann803b5832014-09-16 08:00:29 +0200526 struct sk_buff *skb;
527 int err = 0;
528
529 spin_lock(&data->rxlock);
530 skb = data->sco_skb;
531
532 while (count) {
533 int len;
534
535 if (!skb) {
536 skb = bt_skb_alloc(HCI_MAX_SCO_SIZE, GFP_ATOMIC);
537 if (!skb) {
538 err = -ENOMEM;
539 break;
540 }
541
542 bt_cb(skb)->pkt_type = HCI_SCODATA_PKT;
543 bt_cb(skb)->expect = HCI_SCO_HDR_SIZE;
544 }
545
546 len = min_t(uint, bt_cb(skb)->expect, count);
547 memcpy(skb_put(skb, len), buffer, len);
548
549 count -= len;
550 buffer += len;
551 bt_cb(skb)->expect -= len;
552
553 if (skb->len == HCI_SCO_HDR_SIZE) {
554 /* Complete SCO header */
555 bt_cb(skb)->expect = hci_sco_hdr(skb)->dlen;
556
557 if (skb_tailroom(skb) < bt_cb(skb)->expect) {
558 kfree_skb(skb);
559 skb = NULL;
560
561 err = -EILSEQ;
562 break;
563 }
564 }
565
566 if (bt_cb(skb)->expect == 0) {
567 /* Complete frame */
568 hci_recv_frame(data->hdev, skb);
569 skb = NULL;
570 }
571 }
572
573 data->sco_skb = skb;
574 spin_unlock(&data->rxlock);
575
576 return err;
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200577}
578
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200579static void btusb_intr_complete(struct urb *urb)
580{
581 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +0100582 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200583 int err;
584
Marcel Holtmann89e75332014-09-16 04:44:50 +0200585 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
586 urb->actual_length);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200587
588 if (!test_bit(HCI_RUNNING, &hdev->flags))
589 return;
590
591 if (urb->status == 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200592 hdev->stat.byte_rx += urb->actual_length;
593
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200594 if (btusb_recv_intr(data, urb->transfer_buffer,
595 urb->actual_length) < 0) {
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200596 BT_ERR("%s corrupted event packet", hdev->name);
597 hdev->stat.err_rx++;
598 }
Champion Chen85560c42014-09-06 14:06:08 -0500599 } else if (urb->status == -ENOENT) {
600 /* Avoid suspend failed when usb_kill_urb */
601 return;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200602 }
603
604 if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
605 return;
606
Oliver Neukum7bee5492009-08-24 23:44:59 +0200607 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200608 usb_anchor_urb(urb, &data->intr_anchor);
609
610 err = usb_submit_urb(urb, GFP_ATOMIC);
611 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200612 /* -EPERM: urb is being killed;
613 * -ENODEV: device got disconnected */
614 if (err != -EPERM && err != -ENODEV)
Stefan Seyfried61faddf2010-11-30 21:49:08 +0100615 BT_ERR("%s urb %p failed to resubmit (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200616 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200617 usb_unanchor_urb(urb);
618 }
619}
620
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100621static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200622{
David Herrmann155961e2012-02-09 21:58:32 +0100623 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200624 struct urb *urb;
625 unsigned char *buf;
626 unsigned int pipe;
627 int err, size;
628
629 BT_DBG("%s", hdev->name);
630
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200631 if (!data->intr_ep)
632 return -ENODEV;
633
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100634 urb = usb_alloc_urb(0, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200635 if (!urb)
636 return -ENOMEM;
637
638 size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
639
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100640 buf = kmalloc(size, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200641 if (!buf) {
642 usb_free_urb(urb);
643 return -ENOMEM;
644 }
645
646 pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
647
648 usb_fill_int_urb(urb, data->udev, pipe, buf, size,
Marcel Holtmann89e75332014-09-16 04:44:50 +0200649 btusb_intr_complete, hdev, data->intr_ep->bInterval);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200650
651 urb->transfer_flags |= URB_FREE_BUFFER;
652
653 usb_anchor_urb(urb, &data->intr_anchor);
654
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100655 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200656 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +0200657 if (err != -EPERM && err != -ENODEV)
658 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200659 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200660 usb_unanchor_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200661 }
662
663 usb_free_urb(urb);
664
665 return err;
666}
667
668static void btusb_bulk_complete(struct urb *urb)
669{
670 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +0100671 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200672 int err;
673
Marcel Holtmann89e75332014-09-16 04:44:50 +0200674 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
675 urb->actual_length);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200676
677 if (!test_bit(HCI_RUNNING, &hdev->flags))
678 return;
679
680 if (urb->status == 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200681 hdev->stat.byte_rx += urb->actual_length;
682
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +0100683 if (data->recv_bulk(data, urb->transfer_buffer,
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200684 urb->actual_length) < 0) {
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200685 BT_ERR("%s corrupted ACL packet", hdev->name);
686 hdev->stat.err_rx++;
687 }
Champion Chen85560c42014-09-06 14:06:08 -0500688 } else if (urb->status == -ENOENT) {
689 /* Avoid suspend failed when usb_kill_urb */
690 return;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200691 }
692
693 if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
694 return;
695
696 usb_anchor_urb(urb, &data->bulk_anchor);
Oliver Neukum652fd782009-12-16 19:23:43 +0100697 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200698
699 err = usb_submit_urb(urb, GFP_ATOMIC);
700 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200701 /* -EPERM: urb is being killed;
702 * -ENODEV: device got disconnected */
703 if (err != -EPERM && err != -ENODEV)
Stefan Seyfried61faddf2010-11-30 21:49:08 +0100704 BT_ERR("%s urb %p failed to resubmit (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200705 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200706 usb_unanchor_urb(urb);
707 }
708}
709
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100710static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200711{
David Herrmann155961e2012-02-09 21:58:32 +0100712 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200713 struct urb *urb;
714 unsigned char *buf;
715 unsigned int pipe;
Vikram Kandukuri290ba202009-07-02 14:31:59 +0530716 int err, size = HCI_MAX_FRAME_SIZE;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200717
718 BT_DBG("%s", hdev->name);
719
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200720 if (!data->bulk_rx_ep)
721 return -ENODEV;
722
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100723 urb = usb_alloc_urb(0, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200724 if (!urb)
725 return -ENOMEM;
726
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100727 buf = kmalloc(size, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200728 if (!buf) {
729 usb_free_urb(urb);
730 return -ENOMEM;
731 }
732
733 pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
734
Marcel Holtmann89e75332014-09-16 04:44:50 +0200735 usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
736 btusb_bulk_complete, hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200737
738 urb->transfer_flags |= URB_FREE_BUFFER;
739
Oliver Neukum7bee5492009-08-24 23:44:59 +0200740 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200741 usb_anchor_urb(urb, &data->bulk_anchor);
742
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100743 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200744 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +0200745 if (err != -EPERM && err != -ENODEV)
746 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200747 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200748 usb_unanchor_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200749 }
750
751 usb_free_urb(urb);
752
753 return err;
754}
755
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200756static void btusb_isoc_complete(struct urb *urb)
757{
758 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +0100759 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200760 int i, err;
761
Marcel Holtmann89e75332014-09-16 04:44:50 +0200762 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
763 urb->actual_length);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200764
765 if (!test_bit(HCI_RUNNING, &hdev->flags))
766 return;
767
768 if (urb->status == 0) {
769 for (i = 0; i < urb->number_of_packets; i++) {
770 unsigned int offset = urb->iso_frame_desc[i].offset;
771 unsigned int length = urb->iso_frame_desc[i].actual_length;
772
773 if (urb->iso_frame_desc[i].status)
774 continue;
775
776 hdev->stat.byte_rx += length;
777
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200778 if (btusb_recv_isoc(data, urb->transfer_buffer + offset,
779 length) < 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200780 BT_ERR("%s corrupted SCO packet", hdev->name);
781 hdev->stat.err_rx++;
782 }
783 }
Champion Chen85560c42014-09-06 14:06:08 -0500784 } else if (urb->status == -ENOENT) {
785 /* Avoid suspend failed when usb_kill_urb */
786 return;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200787 }
788
789 if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags))
790 return;
791
792 usb_anchor_urb(urb, &data->isoc_anchor);
793
794 err = usb_submit_urb(urb, GFP_ATOMIC);
795 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200796 /* -EPERM: urb is being killed;
797 * -ENODEV: device got disconnected */
798 if (err != -EPERM && err != -ENODEV)
Stefan Seyfried61faddf2010-11-30 21:49:08 +0100799 BT_ERR("%s urb %p failed to resubmit (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200800 hdev->name, urb, -err);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200801 usb_unanchor_urb(urb);
802 }
803}
804
Jesper Juhl42b16b32011-01-17 00:09:38 +0100805static inline void __fill_isoc_descriptor(struct urb *urb, int len, int mtu)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200806{
807 int i, offset = 0;
808
809 BT_DBG("len %d mtu %d", len, mtu);
810
811 for (i = 0; i < BTUSB_MAX_ISOC_FRAMES && len >= mtu;
812 i++, offset += mtu, len -= mtu) {
813 urb->iso_frame_desc[i].offset = offset;
814 urb->iso_frame_desc[i].length = mtu;
815 }
816
817 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
818 urb->iso_frame_desc[i].offset = offset;
819 urb->iso_frame_desc[i].length = len;
820 i++;
821 }
822
823 urb->number_of_packets = i;
824}
825
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100826static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200827{
David Herrmann155961e2012-02-09 21:58:32 +0100828 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200829 struct urb *urb;
830 unsigned char *buf;
831 unsigned int pipe;
832 int err, size;
833
834 BT_DBG("%s", hdev->name);
835
836 if (!data->isoc_rx_ep)
837 return -ENODEV;
838
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100839 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200840 if (!urb)
841 return -ENOMEM;
842
843 size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) *
844 BTUSB_MAX_ISOC_FRAMES;
845
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100846 buf = kmalloc(size, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200847 if (!buf) {
848 usb_free_urb(urb);
849 return -ENOMEM;
850 }
851
852 pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress);
853
Bing Zhaofa0fb932011-12-20 18:19:00 -0800854 usb_fill_int_urb(urb, data->udev, pipe, buf, size, btusb_isoc_complete,
Marcel Holtmann89e75332014-09-16 04:44:50 +0200855 hdev, data->isoc_rx_ep->bInterval);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200856
Marcel Holtmann89e75332014-09-16 04:44:50 +0200857 urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200858
859 __fill_isoc_descriptor(urb, size,
Marcel Holtmann89e75332014-09-16 04:44:50 +0200860 le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize));
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200861
862 usb_anchor_urb(urb, &data->isoc_anchor);
863
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100864 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200865 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +0200866 if (err != -EPERM && err != -ENODEV)
867 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200868 hdev->name, urb, -err);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200869 usb_unanchor_urb(urb);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200870 }
871
872 usb_free_urb(urb);
873
874 return err;
875}
876
Marcel Holtmann9d08f502015-10-08 20:23:08 +0200877static void btusb_diag_complete(struct urb *urb)
878{
879 struct hci_dev *hdev = urb->context;
880 struct btusb_data *data = hci_get_drvdata(hdev);
881 int err;
882
883 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
884 urb->actual_length);
885
886 if (urb->status == 0) {
887 struct sk_buff *skb;
888
889 skb = bt_skb_alloc(urb->actual_length, GFP_ATOMIC);
890 if (skb) {
891 memcpy(skb_put(skb, urb->actual_length),
892 urb->transfer_buffer, urb->actual_length);
893 hci_recv_diag(hdev, skb);
894 }
895 } else if (urb->status == -ENOENT) {
896 /* Avoid suspend failed when usb_kill_urb */
897 return;
898 }
899
900 if (!test_bit(BTUSB_DIAG_RUNNING, &data->flags))
901 return;
902
903 usb_anchor_urb(urb, &data->diag_anchor);
904 usb_mark_last_busy(data->udev);
905
906 err = usb_submit_urb(urb, GFP_ATOMIC);
907 if (err < 0) {
908 /* -EPERM: urb is being killed;
909 * -ENODEV: device got disconnected */
910 if (err != -EPERM && err != -ENODEV)
911 BT_ERR("%s urb %p failed to resubmit (%d)",
912 hdev->name, urb, -err);
913 usb_unanchor_urb(urb);
914 }
915}
916
917static int btusb_submit_diag_urb(struct hci_dev *hdev, gfp_t mem_flags)
918{
919 struct btusb_data *data = hci_get_drvdata(hdev);
920 struct urb *urb;
921 unsigned char *buf;
922 unsigned int pipe;
923 int err, size = HCI_MAX_FRAME_SIZE;
924
925 BT_DBG("%s", hdev->name);
926
927 if (!data->diag_rx_ep)
928 return -ENODEV;
929
930 urb = usb_alloc_urb(0, mem_flags);
931 if (!urb)
932 return -ENOMEM;
933
934 buf = kmalloc(size, mem_flags);
935 if (!buf) {
936 usb_free_urb(urb);
937 return -ENOMEM;
938 }
939
940 pipe = usb_rcvbulkpipe(data->udev, data->diag_rx_ep->bEndpointAddress);
941
942 usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
943 btusb_diag_complete, hdev);
944
945 urb->transfer_flags |= URB_FREE_BUFFER;
946
947 usb_mark_last_busy(data->udev);
948 usb_anchor_urb(urb, &data->diag_anchor);
949
950 err = usb_submit_urb(urb, mem_flags);
951 if (err < 0) {
952 if (err != -EPERM && err != -ENODEV)
953 BT_ERR("%s urb %p submission failed (%d)",
954 hdev->name, urb, -err);
955 usb_unanchor_urb(urb);
956 }
957
958 usb_free_urb(urb);
959
960 return err;
961}
962
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200963static void btusb_tx_complete(struct urb *urb)
964{
965 struct sk_buff *skb = urb->context;
Marcel Holtmann89e75332014-09-16 04:44:50 +0200966 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
David Herrmann155961e2012-02-09 21:58:32 +0100967 struct btusb_data *data = hci_get_drvdata(hdev);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200968
Marcel Holtmann89e75332014-09-16 04:44:50 +0200969 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
970 urb->actual_length);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200971
972 if (!test_bit(HCI_RUNNING, &hdev->flags))
973 goto done;
974
975 if (!urb->status)
976 hdev->stat.byte_tx += urb->transfer_buffer_length;
977 else
978 hdev->stat.err_tx++;
979
980done:
981 spin_lock(&data->txlock);
982 data->tx_in_flight--;
983 spin_unlock(&data->txlock);
984
985 kfree(urb->setup_packet);
986
987 kfree_skb(skb);
988}
989
990static void btusb_isoc_tx_complete(struct urb *urb)
991{
992 struct sk_buff *skb = urb->context;
Marcel Holtmann89e75332014-09-16 04:44:50 +0200993 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200994
Marcel Holtmann89e75332014-09-16 04:44:50 +0200995 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
996 urb->actual_length);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200997
998 if (!test_bit(HCI_RUNNING, &hdev->flags))
999 goto done;
1000
1001 if (!urb->status)
1002 hdev->stat.byte_tx += urb->transfer_buffer_length;
1003 else
1004 hdev->stat.err_tx++;
1005
1006done:
1007 kfree(urb->setup_packet);
1008
1009 kfree_skb(skb);
1010}
1011
1012static int btusb_open(struct hci_dev *hdev)
1013{
David Herrmann155961e2012-02-09 21:58:32 +01001014 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001015 int err;
1016
1017 BT_DBG("%s", hdev->name);
1018
Kim, Ben Young Taeace31982015-02-15 23:06:14 +00001019 /* Patching USB firmware files prior to starting any URBs of HCI path
1020 * It is more safe to use USB bulk channel for downloading USB patch
1021 */
1022 if (data->setup_on_usb) {
1023 err = data->setup_on_usb(hdev);
Marcel Holtmanneb500422015-04-16 23:15:50 +02001024 if (err < 0)
Kim, Ben Young Taeace31982015-02-15 23:06:14 +00001025 return err;
1026 }
1027
Oliver Neukum7bee5492009-08-24 23:44:59 +02001028 err = usb_autopm_get_interface(data->intf);
1029 if (err < 0)
1030 return err;
1031
1032 data->intf->needs_remote_wakeup = 1;
1033
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001034 if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +02001035 goto done;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001036
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001037 err = btusb_submit_intr_urb(hdev, GFP_KERNEL);
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001038 if (err < 0)
1039 goto failed;
1040
1041 err = btusb_submit_bulk_urb(hdev, GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001042 if (err < 0) {
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001043 usb_kill_anchored_urbs(&data->intr_anchor);
1044 goto failed;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001045 }
1046
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001047 set_bit(BTUSB_BULK_RUNNING, &data->flags);
1048 btusb_submit_bulk_urb(hdev, GFP_KERNEL);
1049
Marcel Holtmann9d08f502015-10-08 20:23:08 +02001050 if (data->diag) {
1051 if (!btusb_submit_diag_urb(hdev, GFP_KERNEL))
1052 set_bit(BTUSB_DIAG_RUNNING, &data->flags);
1053 }
1054
Oliver Neukum7bee5492009-08-24 23:44:59 +02001055done:
1056 usb_autopm_put_interface(data->intf);
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001057 return 0;
1058
1059failed:
1060 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001061 usb_autopm_put_interface(data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001062 return err;
1063}
1064
Oliver Neukum7bee5492009-08-24 23:44:59 +02001065static void btusb_stop_traffic(struct btusb_data *data)
1066{
1067 usb_kill_anchored_urbs(&data->intr_anchor);
1068 usb_kill_anchored_urbs(&data->bulk_anchor);
1069 usb_kill_anchored_urbs(&data->isoc_anchor);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02001070 usb_kill_anchored_urbs(&data->diag_anchor);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001071}
1072
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001073static int btusb_close(struct hci_dev *hdev)
1074{
David Herrmann155961e2012-02-09 21:58:32 +01001075 struct btusb_data *data = hci_get_drvdata(hdev);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001076 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001077
1078 BT_DBG("%s", hdev->name);
1079
Marcel Holtmanne8c3c3d2008-09-23 00:16:36 +02001080 cancel_work_sync(&data->work);
Linus Torvalds404291a2009-11-11 13:32:29 -08001081 cancel_work_sync(&data->waker);
Marcel Holtmanne8c3c3d2008-09-23 00:16:36 +02001082
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001083 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001084 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001085 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02001086 clear_bit(BTUSB_DIAG_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001087
1088 btusb_stop_traffic(data);
Marcel Holtmann803b5832014-09-16 08:00:29 +02001089 btusb_free_frags(data);
1090
Oliver Neukum7bee5492009-08-24 23:44:59 +02001091 err = usb_autopm_get_interface(data->intf);
1092 if (err < 0)
Oliver Neukum7b8e2c12009-11-13 14:26:23 +01001093 goto failed;
Oliver Neukum7bee5492009-08-24 23:44:59 +02001094
1095 data->intf->needs_remote_wakeup = 0;
1096 usb_autopm_put_interface(data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001097
Oliver Neukum7b8e2c12009-11-13 14:26:23 +01001098failed:
1099 usb_scuttle_anchored_urbs(&data->deferred);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001100 return 0;
1101}
1102
1103static int btusb_flush(struct hci_dev *hdev)
1104{
David Herrmann155961e2012-02-09 21:58:32 +01001105 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001106
1107 BT_DBG("%s", hdev->name);
1108
1109 usb_kill_anchored_urbs(&data->tx_anchor);
Marcel Holtmann803b5832014-09-16 08:00:29 +02001110 btusb_free_frags(data);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001111
1112 return 0;
1113}
1114
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001115static struct urb *alloc_ctrl_urb(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001116{
David Herrmann155961e2012-02-09 21:58:32 +01001117 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001118 struct usb_ctrlrequest *dr;
1119 struct urb *urb;
1120 unsigned int pipe;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001121
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001122 urb = usb_alloc_urb(0, GFP_KERNEL);
1123 if (!urb)
1124 return ERR_PTR(-ENOMEM);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001125
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001126 dr = kmalloc(sizeof(*dr), GFP_KERNEL);
1127 if (!dr) {
1128 usb_free_urb(urb);
1129 return ERR_PTR(-ENOMEM);
1130 }
1131
1132 dr->bRequestType = data->cmdreq_type;
Marcel Holtmann893ba542015-01-28 20:27:34 -08001133 dr->bRequest = data->cmdreq;
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001134 dr->wIndex = 0;
1135 dr->wValue = 0;
1136 dr->wLength = __cpu_to_le16(skb->len);
1137
1138 pipe = usb_sndctrlpipe(data->udev, 0x00);
1139
Marcel Holtmann89e75332014-09-16 04:44:50 +02001140 usb_fill_control_urb(urb, data->udev, pipe, (void *)dr,
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001141 skb->data, skb->len, btusb_tx_complete, skb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001142
Marcel Holtmann89e75332014-09-16 04:44:50 +02001143 skb->dev = (void *)hdev;
Marcel Holtmann7bd8f092013-10-11 06:19:18 -07001144
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001145 return urb;
1146}
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001147
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001148static struct urb *alloc_bulk_urb(struct hci_dev *hdev, struct sk_buff *skb)
1149{
1150 struct btusb_data *data = hci_get_drvdata(hdev);
1151 struct urb *urb;
1152 unsigned int pipe;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001153
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001154 if (!data->bulk_tx_ep)
1155 return ERR_PTR(-ENODEV);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001156
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001157 urb = usb_alloc_urb(0, GFP_KERNEL);
1158 if (!urb)
1159 return ERR_PTR(-ENOMEM);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001160
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001161 pipe = usb_sndbulkpipe(data->udev, data->bulk_tx_ep->bEndpointAddress);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001162
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001163 usb_fill_bulk_urb(urb, data->udev, pipe,
1164 skb->data, skb->len, btusb_tx_complete, skb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001165
Marcel Holtmann89e75332014-09-16 04:44:50 +02001166 skb->dev = (void *)hdev;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001167
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001168 return urb;
1169}
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001170
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001171static struct urb *alloc_isoc_urb(struct hci_dev *hdev, struct sk_buff *skb)
1172{
1173 struct btusb_data *data = hci_get_drvdata(hdev);
1174 struct urb *urb;
1175 unsigned int pipe;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001176
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001177 if (!data->isoc_tx_ep)
1178 return ERR_PTR(-ENODEV);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001179
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001180 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_KERNEL);
1181 if (!urb)
1182 return ERR_PTR(-ENOMEM);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001183
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001184 pipe = usb_sndisocpipe(data->udev, data->isoc_tx_ep->bEndpointAddress);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001185
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001186 usb_fill_int_urb(urb, data->udev, pipe,
1187 skb->data, skb->len, btusb_isoc_tx_complete,
1188 skb, data->isoc_tx_ep->bInterval);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001189
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001190 urb->transfer_flags = URB_ISO_ASAP;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001191
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001192 __fill_isoc_descriptor(urb, skb->len,
1193 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001194
Marcel Holtmann89e75332014-09-16 04:44:50 +02001195 skb->dev = (void *)hdev;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001196
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001197 return urb;
1198}
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001199
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001200static int submit_tx_urb(struct hci_dev *hdev, struct urb *urb)
1201{
1202 struct btusb_data *data = hci_get_drvdata(hdev);
1203 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001204
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001205 usb_anchor_urb(urb, &data->tx_anchor);
1206
Johan Hedberge9753ef2014-09-14 08:49:34 +03001207 err = usb_submit_urb(urb, GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001208 if (err < 0) {
Paul Bolle5a9b80e2011-10-09 12:12:16 +02001209 if (err != -EPERM && err != -ENODEV)
1210 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +02001211 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001212 kfree(urb->setup_packet);
1213 usb_unanchor_urb(urb);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001214 } else {
1215 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001216 }
1217
Cong Wang54a8a792011-11-22 09:32:57 +08001218 usb_free_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001219 return err;
1220}
1221
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001222static int submit_or_queue_tx_urb(struct hci_dev *hdev, struct urb *urb)
1223{
1224 struct btusb_data *data = hci_get_drvdata(hdev);
1225 unsigned long flags;
1226 bool suspending;
1227
1228 spin_lock_irqsave(&data->txlock, flags);
1229 suspending = test_bit(BTUSB_SUSPENDING, &data->flags);
1230 if (!suspending)
1231 data->tx_in_flight++;
1232 spin_unlock_irqrestore(&data->txlock, flags);
1233
1234 if (!suspending)
1235 return submit_tx_urb(hdev, urb);
1236
1237 usb_anchor_urb(urb, &data->deferred);
1238 schedule_work(&data->waker);
1239
1240 usb_free_urb(urb);
1241 return 0;
1242}
1243
1244static int btusb_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
1245{
1246 struct urb *urb;
1247
1248 BT_DBG("%s", hdev->name);
1249
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001250 switch (bt_cb(skb)->pkt_type) {
1251 case HCI_COMMAND_PKT:
1252 urb = alloc_ctrl_urb(hdev, skb);
1253 if (IS_ERR(urb))
1254 return PTR_ERR(urb);
1255
1256 hdev->stat.cmd_tx++;
1257 return submit_or_queue_tx_urb(hdev, urb);
1258
1259 case HCI_ACLDATA_PKT:
1260 urb = alloc_bulk_urb(hdev, skb);
1261 if (IS_ERR(urb))
1262 return PTR_ERR(urb);
1263
1264 hdev->stat.acl_tx++;
1265 return submit_or_queue_tx_urb(hdev, urb);
1266
1267 case HCI_SCODATA_PKT:
1268 if (hci_conn_num(hdev, SCO_LINK) < 1)
1269 return -ENODEV;
1270
1271 urb = alloc_isoc_urb(hdev, skb);
1272 if (IS_ERR(urb))
1273 return PTR_ERR(urb);
1274
1275 hdev->stat.sco_tx++;
1276 return submit_tx_urb(hdev, urb);
1277 }
1278
1279 return -EILSEQ;
1280}
1281
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001282static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
1283{
David Herrmann155961e2012-02-09 21:58:32 +01001284 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001285
1286 BT_DBG("%s evt %d", hdev->name, evt);
1287
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001288 if (hci_conn_num(hdev, SCO_LINK) != data->sco_num) {
1289 data->sco_num = hci_conn_num(hdev, SCO_LINK);
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001290 schedule_work(&data->work);
Marcel Holtmanna780efa2008-11-30 12:17:12 +01001291 }
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001292}
1293
Jesper Juhl42b16b32011-01-17 00:09:38 +01001294static inline int __set_isoc_interface(struct hci_dev *hdev, int altsetting)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001295{
David Herrmann155961e2012-02-09 21:58:32 +01001296 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001297 struct usb_interface *intf = data->isoc;
1298 struct usb_endpoint_descriptor *ep_desc;
1299 int i, err;
1300
1301 if (!data->isoc)
1302 return -ENODEV;
1303
1304 err = usb_set_interface(data->udev, 1, altsetting);
1305 if (err < 0) {
1306 BT_ERR("%s setting interface failed (%d)", hdev->name, -err);
1307 return err;
1308 }
1309
1310 data->isoc_altsetting = altsetting;
1311
1312 data->isoc_tx_ep = NULL;
1313 data->isoc_rx_ep = NULL;
1314
1315 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
1316 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
1317
1318 if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) {
1319 data->isoc_tx_ep = ep_desc;
1320 continue;
1321 }
1322
1323 if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) {
1324 data->isoc_rx_ep = ep_desc;
1325 continue;
1326 }
1327 }
1328
1329 if (!data->isoc_tx_ep || !data->isoc_rx_ep) {
1330 BT_ERR("%s invalid SCO descriptors", hdev->name);
1331 return -ENODEV;
1332 }
1333
1334 return 0;
1335}
1336
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001337static void btusb_work(struct work_struct *work)
1338{
1339 struct btusb_data *data = container_of(work, struct btusb_data, work);
1340 struct hci_dev *hdev = data->hdev;
Mikel Astizf4001d22012-04-11 08:48:51 +02001341 int new_alts;
Oliver Neukum7bee5492009-08-24 23:44:59 +02001342 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001343
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001344 if (data->sco_num > 0) {
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -03001345 if (!test_bit(BTUSB_DID_ISO_RESUME, &data->flags)) {
Oliver Neukum8efdd0c2011-02-11 13:00:06 +01001346 err = usb_autopm_get_interface(data->isoc ? data->isoc : data->intf);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001347 if (err < 0) {
1348 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1349 usb_kill_anchored_urbs(&data->isoc_anchor);
1350 return;
1351 }
1352
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -03001353 set_bit(BTUSB_DID_ISO_RESUME, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001354 }
Mikel Astizf4001d22012-04-11 08:48:51 +02001355
1356 if (hdev->voice_setting & 0x0020) {
1357 static const int alts[3] = { 2, 4, 5 };
Marcel Holtmann89e75332014-09-16 04:44:50 +02001358
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001359 new_alts = alts[data->sco_num - 1];
Mikel Astizf4001d22012-04-11 08:48:51 +02001360 } else {
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001361 new_alts = data->sco_num;
Mikel Astizf4001d22012-04-11 08:48:51 +02001362 }
1363
1364 if (data->isoc_altsetting != new_alts) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001365 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1366 usb_kill_anchored_urbs(&data->isoc_anchor);
1367
Kuba Pawlak8f9d02f2015-09-10 17:07:00 +01001368 /* When isochronous alternate setting needs to be
1369 * changed, because SCO connection has been added
1370 * or removed, a packet fragment may be left in the
1371 * reassembling state. This could lead to wrongly
1372 * assembled fragments.
1373 *
1374 * Clear outstanding fragment when selecting a new
1375 * alternate setting.
1376 */
1377 spin_lock(&data->rxlock);
1378 kfree_skb(data->sco_skb);
1379 data->sco_skb = NULL;
1380 spin_unlock(&data->rxlock);
1381
Mikel Astizf4001d22012-04-11 08:48:51 +02001382 if (__set_isoc_interface(hdev, new_alts) < 0)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001383 return;
1384 }
1385
1386 if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001387 if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001388 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1389 else
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001390 btusb_submit_isoc_urb(hdev, GFP_KERNEL);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001391 }
1392 } else {
1393 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1394 usb_kill_anchored_urbs(&data->isoc_anchor);
1395
1396 __set_isoc_interface(hdev, 0);
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -03001397 if (test_and_clear_bit(BTUSB_DID_ISO_RESUME, &data->flags))
Oliver Neukum8efdd0c2011-02-11 13:00:06 +01001398 usb_autopm_put_interface(data->isoc ? data->isoc : data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001399 }
1400}
1401
Oliver Neukum7bee5492009-08-24 23:44:59 +02001402static void btusb_waker(struct work_struct *work)
1403{
1404 struct btusb_data *data = container_of(work, struct btusb_data, waker);
1405 int err;
1406
1407 err = usb_autopm_get_interface(data->intf);
1408 if (err < 0)
1409 return;
1410
1411 usb_autopm_put_interface(data->intf);
1412}
1413
Marcel Holtmann9f8f9622013-04-10 08:11:35 -07001414static int btusb_setup_bcm92035(struct hci_dev *hdev)
1415{
1416 struct sk_buff *skb;
1417 u8 val = 0x00;
1418
1419 BT_DBG("%s", hdev->name);
1420
1421 skb = __hci_cmd_sync(hdev, 0xfc3b, 1, &val, HCI_INIT_TIMEOUT);
1422 if (IS_ERR(skb))
1423 BT_ERR("BCM92035 command failed (%ld)", -PTR_ERR(skb));
1424 else
1425 kfree_skb(skb);
1426
1427 return 0;
1428}
1429
Marcel Holtmann81cac642014-01-03 03:02:36 -08001430static int btusb_setup_csr(struct hci_dev *hdev)
1431{
1432 struct hci_rp_read_local_version *rp;
1433 struct sk_buff *skb;
Marcel Holtmann81cac642014-01-03 03:02:36 -08001434
1435 BT_DBG("%s", hdev->name);
1436
Marcel Holtmann7cd84d72015-06-07 10:01:02 +02001437 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
1438 HCI_INIT_TIMEOUT);
1439 if (IS_ERR(skb)) {
1440 int err = PTR_ERR(skb);
1441 BT_ERR("%s: CSR: Local version failed (%d)", hdev->name, err);
1442 return err;
1443 }
1444
1445 if (skb->len != sizeof(struct hci_rp_read_local_version)) {
1446 BT_ERR("%s: CSR: Local version length mismatch", hdev->name);
1447 kfree_skb(skb);
1448 return -EIO;
1449 }
Marcel Holtmann81cac642014-01-03 03:02:36 -08001450
Marcel Holtmann89e75332014-09-16 04:44:50 +02001451 rp = (struct hci_rp_read_local_version *)skb->data;
Marcel Holtmann81cac642014-01-03 03:02:36 -08001452
Johan Hedberg6cafcd92015-08-30 21:47:21 +03001453 /* Detect controllers which aren't real CSR ones. */
1454 if (le16_to_cpu(rp->manufacturer) != 10 ||
1455 le16_to_cpu(rp->lmp_subver) == 0x0c5c) {
Marcel Holtmann9641d342015-06-07 10:01:01 +02001456 /* Clear the reset quirk since this is not an actual
1457 * early Bluetooth 1.1 device from CSR.
1458 */
1459 clear_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmann81cac642014-01-03 03:02:36 -08001460
Marcel Holtmann9641d342015-06-07 10:01:01 +02001461 /* These fake CSR controllers have all a broken
1462 * stored link key handling and so just disable it.
1463 */
1464 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
Marcel Holtmann81cac642014-01-03 03:02:36 -08001465 }
1466
Marcel Holtmann81cac642014-01-03 03:02:36 -08001467 kfree_skb(skb);
1468
Marcel Holtmann9641d342015-06-07 10:01:01 +02001469 return 0;
Marcel Holtmann81cac642014-01-03 03:02:36 -08001470}
1471
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001472static const struct firmware *btusb_setup_intel_get_fw(struct hci_dev *hdev,
Marcel Holtmann89e75332014-09-16 04:44:50 +02001473 struct intel_version *ver)
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001474{
1475 const struct firmware *fw;
1476 char fwname[64];
1477 int ret;
1478
1479 snprintf(fwname, sizeof(fwname),
1480 "intel/ibt-hw-%x.%x.%x-fw-%x.%x.%x.%x.%x.bseq",
1481 ver->hw_platform, ver->hw_variant, ver->hw_revision,
1482 ver->fw_variant, ver->fw_revision, ver->fw_build_num,
1483 ver->fw_build_ww, ver->fw_build_yy);
1484
1485 ret = request_firmware(&fw, fwname, &hdev->dev);
1486 if (ret < 0) {
1487 if (ret == -EINVAL) {
1488 BT_ERR("%s Intel firmware file request failed (%d)",
1489 hdev->name, ret);
1490 return NULL;
1491 }
1492
1493 BT_ERR("%s failed to open Intel firmware file: %s(%d)",
1494 hdev->name, fwname, ret);
1495
1496 /* If the correct firmware patch file is not found, use the
1497 * default firmware patch file instead
1498 */
1499 snprintf(fwname, sizeof(fwname), "intel/ibt-hw-%x.%x.bseq",
1500 ver->hw_platform, ver->hw_variant);
1501 if (request_firmware(&fw, fwname, &hdev->dev) < 0) {
1502 BT_ERR("%s failed to open default Intel fw file: %s",
1503 hdev->name, fwname);
1504 return NULL;
1505 }
1506 }
1507
1508 BT_INFO("%s: Intel Bluetooth firmware file: %s", hdev->name, fwname);
1509
1510 return fw;
1511}
1512
1513static int btusb_setup_intel_patching(struct hci_dev *hdev,
1514 const struct firmware *fw,
1515 const u8 **fw_ptr, int *disable_patch)
1516{
1517 struct sk_buff *skb;
1518 struct hci_command_hdr *cmd;
1519 const u8 *cmd_param;
1520 struct hci_event_hdr *evt = NULL;
1521 const u8 *evt_param = NULL;
1522 int remain = fw->size - (*fw_ptr - fw->data);
1523
1524 /* The first byte indicates the types of the patch command or event.
1525 * 0x01 means HCI command and 0x02 is HCI event. If the first bytes
1526 * in the current firmware buffer doesn't start with 0x01 or
1527 * the size of remain buffer is smaller than HCI command header,
1528 * the firmware file is corrupted and it should stop the patching
1529 * process.
1530 */
1531 if (remain > HCI_COMMAND_HDR_SIZE && *fw_ptr[0] != 0x01) {
1532 BT_ERR("%s Intel fw corrupted: invalid cmd read", hdev->name);
1533 return -EINVAL;
1534 }
1535 (*fw_ptr)++;
1536 remain--;
1537
1538 cmd = (struct hci_command_hdr *)(*fw_ptr);
1539 *fw_ptr += sizeof(*cmd);
1540 remain -= sizeof(*cmd);
1541
1542 /* Ensure that the remain firmware data is long enough than the length
1543 * of command parameter. If not, the firmware file is corrupted.
1544 */
1545 if (remain < cmd->plen) {
1546 BT_ERR("%s Intel fw corrupted: invalid cmd len", hdev->name);
1547 return -EFAULT;
1548 }
1549
1550 /* If there is a command that loads a patch in the firmware
1551 * file, then enable the patch upon success, otherwise just
1552 * disable the manufacturer mode, for example patch activation
1553 * is not required when the default firmware patch file is used
1554 * because there are no patch data to load.
1555 */
1556 if (*disable_patch && le16_to_cpu(cmd->opcode) == 0xfc8e)
1557 *disable_patch = 0;
1558
1559 cmd_param = *fw_ptr;
1560 *fw_ptr += cmd->plen;
1561 remain -= cmd->plen;
1562
1563 /* This reads the expected events when the above command is sent to the
1564 * device. Some vendor commands expects more than one events, for
1565 * example command status event followed by vendor specific event.
1566 * For this case, it only keeps the last expected event. so the command
1567 * can be sent with __hci_cmd_sync_ev() which returns the sk_buff of
1568 * last expected event.
1569 */
1570 while (remain > HCI_EVENT_HDR_SIZE && *fw_ptr[0] == 0x02) {
1571 (*fw_ptr)++;
1572 remain--;
1573
1574 evt = (struct hci_event_hdr *)(*fw_ptr);
1575 *fw_ptr += sizeof(*evt);
1576 remain -= sizeof(*evt);
1577
1578 if (remain < evt->plen) {
1579 BT_ERR("%s Intel fw corrupted: invalid evt len",
1580 hdev->name);
1581 return -EFAULT;
1582 }
1583
1584 evt_param = *fw_ptr;
1585 *fw_ptr += evt->plen;
1586 remain -= evt->plen;
1587 }
1588
1589 /* Every HCI commands in the firmware file has its correspond event.
1590 * If event is not found or remain is smaller than zero, the firmware
1591 * file is corrupted.
1592 */
1593 if (!evt || !evt_param || remain < 0) {
1594 BT_ERR("%s Intel fw corrupted: invalid evt read", hdev->name);
1595 return -EFAULT;
1596 }
1597
1598 skb = __hci_cmd_sync_ev(hdev, le16_to_cpu(cmd->opcode), cmd->plen,
1599 cmd_param, evt->evt, HCI_INIT_TIMEOUT);
1600 if (IS_ERR(skb)) {
1601 BT_ERR("%s sending Intel patch command (0x%4.4x) failed (%ld)",
1602 hdev->name, cmd->opcode, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001603 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001604 }
1605
1606 /* It ensures that the returned event matches the event data read from
1607 * the firmware file. At fist, it checks the length and then
1608 * the contents of the event.
1609 */
1610 if (skb->len != evt->plen) {
1611 BT_ERR("%s mismatch event length (opcode 0x%4.4x)", hdev->name,
1612 le16_to_cpu(cmd->opcode));
1613 kfree_skb(skb);
1614 return -EFAULT;
1615 }
1616
1617 if (memcmp(skb->data, evt_param, evt->plen)) {
1618 BT_ERR("%s mismatch event parameter (opcode 0x%4.4x)",
1619 hdev->name, le16_to_cpu(cmd->opcode));
1620 kfree_skb(skb);
1621 return -EFAULT;
1622 }
1623 kfree_skb(skb);
1624
1625 return 0;
1626}
1627
1628static int btusb_setup_intel(struct hci_dev *hdev)
1629{
1630 struct sk_buff *skb;
1631 const struct firmware *fw;
1632 const u8 *fw_ptr;
1633 int disable_patch;
1634 struct intel_version *ver;
1635
1636 const u8 mfg_enable[] = { 0x01, 0x00 };
1637 const u8 mfg_disable[] = { 0x00, 0x00 };
1638 const u8 mfg_reset_deactivate[] = { 0x00, 0x01 };
1639 const u8 mfg_reset_activate[] = { 0x00, 0x02 };
1640
1641 BT_DBG("%s", hdev->name);
1642
1643 /* The controller has a bug with the first HCI command sent to it
1644 * returning number of completed commands as zero. This would stall the
1645 * command processing in the Bluetooth core.
1646 *
1647 * As a workaround, send HCI Reset command first which will reset the
1648 * number of completed commands and allow normal command processing
1649 * from now on.
1650 */
1651 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
1652 if (IS_ERR(skb)) {
1653 BT_ERR("%s sending initial HCI reset command failed (%ld)",
1654 hdev->name, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001655 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001656 }
1657 kfree_skb(skb);
1658
1659 /* Read Intel specific controller version first to allow selection of
1660 * which firmware file to load.
1661 *
1662 * The returned information are hardware variant and revision plus
1663 * firmware variant, revision and build number.
1664 */
1665 skb = __hci_cmd_sync(hdev, 0xfc05, 0, NULL, HCI_INIT_TIMEOUT);
1666 if (IS_ERR(skb)) {
1667 BT_ERR("%s reading Intel fw version command failed (%ld)",
1668 hdev->name, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001669 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001670 }
1671
1672 if (skb->len != sizeof(*ver)) {
1673 BT_ERR("%s Intel version event length mismatch", hdev->name);
1674 kfree_skb(skb);
1675 return -EIO;
1676 }
1677
1678 ver = (struct intel_version *)skb->data;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001679
1680 BT_INFO("%s: read Intel version: %02x%02x%02x%02x%02x%02x%02x%02x%02x",
1681 hdev->name, ver->hw_platform, ver->hw_variant,
1682 ver->hw_revision, ver->fw_variant, ver->fw_revision,
1683 ver->fw_build_num, ver->fw_build_ww, ver->fw_build_yy,
1684 ver->fw_patch_num);
1685
1686 /* fw_patch_num indicates the version of patch the device currently
1687 * have. If there is no patch data in the device, it is always 0x00.
Minjune Kim5075eda2015-08-27 13:21:52 +09001688 * So, if it is other than 0x00, no need to patch the device again.
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001689 */
1690 if (ver->fw_patch_num) {
1691 BT_INFO("%s: Intel device is already patched. patch num: %02x",
1692 hdev->name, ver->fw_patch_num);
1693 kfree_skb(skb);
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07001694 btintel_check_bdaddr(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001695 return 0;
1696 }
1697
1698 /* Opens the firmware patch file based on the firmware version read
1699 * from the controller. If it fails to open the matching firmware
1700 * patch file, it tries to open the default firmware patch file.
1701 * If no patch file is found, allow the device to operate without
1702 * a patch.
1703 */
1704 fw = btusb_setup_intel_get_fw(hdev, ver);
1705 if (!fw) {
1706 kfree_skb(skb);
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07001707 btintel_check_bdaddr(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001708 return 0;
1709 }
1710 fw_ptr = fw->data;
1711
Marcel Holtmannecffc802015-06-07 09:42:19 +02001712 kfree_skb(skb);
1713
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001714 /* This Intel specific command enables the manufacturer mode of the
1715 * controller.
1716 *
1717 * Only while this mode is enabled, the driver can download the
1718 * firmware patch data and configuration parameters.
1719 */
1720 skb = __hci_cmd_sync(hdev, 0xfc11, 2, mfg_enable, HCI_INIT_TIMEOUT);
1721 if (IS_ERR(skb)) {
1722 BT_ERR("%s entering Intel manufacturer mode failed (%ld)",
1723 hdev->name, PTR_ERR(skb));
1724 release_firmware(fw);
Adam Leed9c78e92013-07-10 10:02:12 +08001725 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001726 }
1727
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001728 kfree_skb(skb);
1729
1730 disable_patch = 1;
1731
1732 /* The firmware data file consists of list of Intel specific HCI
1733 * commands and its expected events. The first byte indicates the
1734 * type of the message, either HCI command or HCI event.
1735 *
1736 * It reads the command and its expected event from the firmware file,
1737 * and send to the controller. Once __hci_cmd_sync_ev() returns,
1738 * the returned event is compared with the event read from the firmware
1739 * file and it will continue until all the messages are downloaded to
1740 * the controller.
1741 *
1742 * Once the firmware patching is completed successfully,
1743 * the manufacturer mode is disabled with reset and activating the
1744 * downloaded patch.
1745 *
1746 * If the firmware patching fails, the manufacturer mode is
1747 * disabled with reset and deactivating the patch.
1748 *
1749 * If the default patch file is used, no reset is done when disabling
1750 * the manufacturer.
1751 */
1752 while (fw->size > fw_ptr - fw->data) {
1753 int ret;
1754
1755 ret = btusb_setup_intel_patching(hdev, fw, &fw_ptr,
1756 &disable_patch);
1757 if (ret < 0)
1758 goto exit_mfg_deactivate;
1759 }
1760
1761 release_firmware(fw);
1762
1763 if (disable_patch)
1764 goto exit_mfg_disable;
1765
1766 /* Patching completed successfully and disable the manufacturer mode
1767 * with reset and activate the downloaded firmware patches.
1768 */
1769 skb = __hci_cmd_sync(hdev, 0xfc11, sizeof(mfg_reset_activate),
1770 mfg_reset_activate, HCI_INIT_TIMEOUT);
1771 if (IS_ERR(skb)) {
1772 BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
1773 hdev->name, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001774 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001775 }
1776 kfree_skb(skb);
1777
1778 BT_INFO("%s: Intel Bluetooth firmware patch completed and activated",
1779 hdev->name);
1780
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07001781 btintel_check_bdaddr(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001782 return 0;
1783
1784exit_mfg_disable:
1785 /* Disable the manufacturer mode without reset */
1786 skb = __hci_cmd_sync(hdev, 0xfc11, sizeof(mfg_disable), mfg_disable,
1787 HCI_INIT_TIMEOUT);
1788 if (IS_ERR(skb)) {
1789 BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
1790 hdev->name, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001791 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001792 }
1793 kfree_skb(skb);
1794
1795 BT_INFO("%s: Intel Bluetooth firmware patch completed", hdev->name);
Marcel Holtmann40cb0982014-07-02 12:06:45 +02001796
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07001797 btintel_check_bdaddr(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001798 return 0;
1799
1800exit_mfg_deactivate:
1801 release_firmware(fw);
1802
1803 /* Patching failed. Disable the manufacturer mode with reset and
1804 * deactivate the downloaded firmware patches.
1805 */
1806 skb = __hci_cmd_sync(hdev, 0xfc11, sizeof(mfg_reset_deactivate),
1807 mfg_reset_deactivate, HCI_INIT_TIMEOUT);
1808 if (IS_ERR(skb)) {
1809 BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
1810 hdev->name, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001811 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001812 }
1813 kfree_skb(skb);
1814
1815 BT_INFO("%s: Intel Bluetooth firmware patch completed and deactivated",
1816 hdev->name);
1817
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07001818 btintel_check_bdaddr(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001819 return 0;
1820}
1821
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001822static int inject_cmd_complete(struct hci_dev *hdev, __u16 opcode)
1823{
1824 struct sk_buff *skb;
1825 struct hci_event_hdr *hdr;
1826 struct hci_ev_cmd_complete *evt;
1827
1828 skb = bt_skb_alloc(sizeof(*hdr) + sizeof(*evt) + 1, GFP_ATOMIC);
1829 if (!skb)
1830 return -ENOMEM;
1831
1832 hdr = (struct hci_event_hdr *)skb_put(skb, sizeof(*hdr));
1833 hdr->evt = HCI_EV_CMD_COMPLETE;
1834 hdr->plen = sizeof(*evt) + 1;
1835
1836 evt = (struct hci_ev_cmd_complete *)skb_put(skb, sizeof(*evt));
1837 evt->ncmd = 0x01;
1838 evt->opcode = cpu_to_le16(opcode);
1839
1840 *skb_put(skb, 1) = 0x00;
1841
1842 bt_cb(skb)->pkt_type = HCI_EVENT_PKT;
1843
1844 return hci_recv_frame(hdev, skb);
1845}
1846
1847static int btusb_recv_bulk_intel(struct btusb_data *data, void *buffer,
1848 int count)
1849{
1850 /* When the device is in bootloader mode, then it can send
1851 * events via the bulk endpoint. These events are treated the
1852 * same way as the ones received from the interrupt endpoint.
1853 */
1854 if (test_bit(BTUSB_BOOTLOADER, &data->flags))
1855 return btusb_recv_intr(data, buffer, count);
1856
1857 return btusb_recv_bulk(data, buffer, count);
1858}
1859
Marcel Holtmannccd6da22015-04-09 00:35:19 -07001860static void btusb_intel_bootup(struct btusb_data *data, const void *ptr,
1861 unsigned int len)
1862{
1863 const struct intel_bootup *evt = ptr;
1864
1865 if (len != sizeof(*evt))
1866 return;
1867
1868 if (test_and_clear_bit(BTUSB_BOOTING, &data->flags)) {
1869 smp_mb__after_atomic();
1870 wake_up_bit(&data->flags, BTUSB_BOOTING);
1871 }
1872}
1873
1874static void btusb_intel_secure_send_result(struct btusb_data *data,
1875 const void *ptr, unsigned int len)
1876{
1877 const struct intel_secure_send_result *evt = ptr;
1878
1879 if (len != sizeof(*evt))
1880 return;
1881
1882 if (evt->result)
1883 set_bit(BTUSB_FIRMWARE_FAILED, &data->flags);
1884
1885 if (test_and_clear_bit(BTUSB_DOWNLOADING, &data->flags) &&
1886 test_bit(BTUSB_FIRMWARE_LOADED, &data->flags)) {
1887 smp_mb__after_atomic();
1888 wake_up_bit(&data->flags, BTUSB_DOWNLOADING);
1889 }
1890}
1891
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001892static int btusb_recv_event_intel(struct hci_dev *hdev, struct sk_buff *skb)
1893{
1894 struct btusb_data *data = hci_get_drvdata(hdev);
1895
1896 if (test_bit(BTUSB_BOOTLOADER, &data->flags)) {
1897 struct hci_event_hdr *hdr = (void *)skb->data;
1898
Marcel Holtmannccd6da22015-04-09 00:35:19 -07001899 if (skb->len > HCI_EVENT_HDR_SIZE && hdr->evt == 0xff &&
1900 hdr->plen > 0) {
1901 const void *ptr = skb->data + HCI_EVENT_HDR_SIZE + 1;
1902 unsigned int len = skb->len - HCI_EVENT_HDR_SIZE - 1;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001903
Marcel Holtmannccd6da22015-04-09 00:35:19 -07001904 switch (skb->data[2]) {
1905 case 0x02:
1906 /* When switching to the operational firmware
1907 * the device sends a vendor specific event
1908 * indicating that the bootup completed.
1909 */
1910 btusb_intel_bootup(data, ptr, len);
1911 break;
1912 case 0x06:
1913 /* When the firmware loading completes the
1914 * device sends out a vendor specific event
1915 * indicating the result of the firmware
1916 * loading.
1917 */
1918 btusb_intel_secure_send_result(data, ptr, len);
1919 break;
Johan Hedbergfad70972015-01-30 10:58:55 +02001920 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001921 }
1922 }
1923
1924 return hci_recv_frame(hdev, skb);
1925}
1926
1927static int btusb_send_frame_intel(struct hci_dev *hdev, struct sk_buff *skb)
1928{
1929 struct btusb_data *data = hci_get_drvdata(hdev);
1930 struct urb *urb;
1931
1932 BT_DBG("%s", hdev->name);
1933
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001934 switch (bt_cb(skb)->pkt_type) {
1935 case HCI_COMMAND_PKT:
1936 if (test_bit(BTUSB_BOOTLOADER, &data->flags)) {
1937 struct hci_command_hdr *cmd = (void *)skb->data;
1938 __u16 opcode = le16_to_cpu(cmd->opcode);
1939
1940 /* When in bootloader mode and the command 0xfc09
1941 * is received, it needs to be send down the
1942 * bulk endpoint. So allocate a bulk URB instead.
1943 */
1944 if (opcode == 0xfc09)
1945 urb = alloc_bulk_urb(hdev, skb);
1946 else
1947 urb = alloc_ctrl_urb(hdev, skb);
1948
1949 /* When the 0xfc01 command is issued to boot into
1950 * the operational firmware, it will actually not
1951 * send a command complete event. To keep the flow
1952 * control working inject that event here.
1953 */
1954 if (opcode == 0xfc01)
1955 inject_cmd_complete(hdev, opcode);
1956 } else {
1957 urb = alloc_ctrl_urb(hdev, skb);
1958 }
1959 if (IS_ERR(urb))
1960 return PTR_ERR(urb);
1961
1962 hdev->stat.cmd_tx++;
1963 return submit_or_queue_tx_urb(hdev, urb);
1964
1965 case HCI_ACLDATA_PKT:
1966 urb = alloc_bulk_urb(hdev, skb);
1967 if (IS_ERR(urb))
1968 return PTR_ERR(urb);
1969
1970 hdev->stat.acl_tx++;
1971 return submit_or_queue_tx_urb(hdev, urb);
1972
1973 case HCI_SCODATA_PKT:
1974 if (hci_conn_num(hdev, SCO_LINK) < 1)
1975 return -ENODEV;
1976
1977 urb = alloc_isoc_urb(hdev, skb);
1978 if (IS_ERR(urb))
1979 return PTR_ERR(urb);
1980
1981 hdev->stat.sco_tx++;
1982 return submit_tx_urb(hdev, urb);
1983 }
1984
1985 return -EILSEQ;
1986}
1987
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001988static int btusb_setup_intel_new(struct hci_dev *hdev)
1989{
1990 static const u8 reset_param[] = { 0x00, 0x01, 0x00, 0x01,
1991 0x00, 0x08, 0x04, 0x00 };
1992 struct btusb_data *data = hci_get_drvdata(hdev);
1993 struct sk_buff *skb;
1994 struct intel_version *ver;
1995 struct intel_boot_params *params;
1996 const struct firmware *fw;
1997 const u8 *fw_ptr;
Marcel Holtmanne66890a92015-06-07 09:47:08 +02001998 u32 frag_len;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001999 char fwname[64];
2000 ktime_t calltime, delta, rettime;
2001 unsigned long long duration;
2002 int err;
2003
2004 BT_DBG("%s", hdev->name);
2005
2006 calltime = ktime_get();
2007
2008 /* Read the Intel version information to determine if the device
2009 * is in bootloader mode or if it already has operational firmware
2010 * loaded.
2011 */
2012 skb = __hci_cmd_sync(hdev, 0xfc05, 0, NULL, HCI_INIT_TIMEOUT);
2013 if (IS_ERR(skb)) {
2014 BT_ERR("%s: Reading Intel version information failed (%ld)",
2015 hdev->name, PTR_ERR(skb));
2016 return PTR_ERR(skb);
2017 }
2018
2019 if (skb->len != sizeof(*ver)) {
2020 BT_ERR("%s: Intel version event size mismatch", hdev->name);
2021 kfree_skb(skb);
2022 return -EILSEQ;
2023 }
2024
2025 ver = (struct intel_version *)skb->data;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002026
2027 /* The hardware platform number has a fixed value of 0x37 and
2028 * for now only accept this single value.
2029 */
2030 if (ver->hw_platform != 0x37) {
2031 BT_ERR("%s: Unsupported Intel hardware platform (%u)",
2032 hdev->name, ver->hw_platform);
2033 kfree_skb(skb);
2034 return -EINVAL;
2035 }
2036
2037 /* At the moment only the hardware variant iBT 3.0 (LnP/SfP) is
2038 * supported by this firmware loading method. This check has been
2039 * put in place to ensure correct forward compatibility options
2040 * when newer hardware variants come along.
2041 */
2042 if (ver->hw_variant != 0x0b) {
2043 BT_ERR("%s: Unsupported Intel hardware variant (%u)",
2044 hdev->name, ver->hw_variant);
2045 kfree_skb(skb);
2046 return -EINVAL;
2047 }
2048
Marcel Holtmann7feb99e2015-07-05 15:02:07 +02002049 btintel_version_info(hdev, ver);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002050
2051 /* The firmware variant determines if the device is in bootloader
2052 * mode or is running operational firmware. The value 0x06 identifies
2053 * the bootloader and the value 0x23 identifies the operational
2054 * firmware.
2055 *
2056 * When the operational firmware is already present, then only
2057 * the check for valid Bluetooth device address is needed. This
2058 * determines if the device will be added as configured or
2059 * unconfigured controller.
2060 *
2061 * It is not possible to use the Secure Boot Parameters in this
2062 * case since that command is only available in bootloader mode.
2063 */
2064 if (ver->fw_variant == 0x23) {
2065 kfree_skb(skb);
2066 clear_bit(BTUSB_BOOTLOADER, &data->flags);
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07002067 btintel_check_bdaddr(hdev);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002068 return 0;
2069 }
2070
2071 /* If the device is not in bootloader mode, then the only possible
2072 * choice is to return an error and abort the device initialization.
2073 */
2074 if (ver->fw_variant != 0x06) {
2075 BT_ERR("%s: Unsupported Intel firmware variant (%u)",
2076 hdev->name, ver->fw_variant);
2077 kfree_skb(skb);
2078 return -ENODEV;
2079 }
2080
2081 kfree_skb(skb);
2082
2083 /* Read the secure boot parameters to identify the operating
2084 * details of the bootloader.
2085 */
2086 skb = __hci_cmd_sync(hdev, 0xfc0d, 0, NULL, HCI_INIT_TIMEOUT);
2087 if (IS_ERR(skb)) {
2088 BT_ERR("%s: Reading Intel boot parameters failed (%ld)",
2089 hdev->name, PTR_ERR(skb));
2090 return PTR_ERR(skb);
2091 }
2092
2093 if (skb->len != sizeof(*params)) {
2094 BT_ERR("%s: Intel boot parameters size mismatch", hdev->name);
2095 kfree_skb(skb);
2096 return -EILSEQ;
2097 }
2098
2099 params = (struct intel_boot_params *)skb->data;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002100
2101 BT_INFO("%s: Device revision is %u", hdev->name,
2102 le16_to_cpu(params->dev_revid));
2103
2104 BT_INFO("%s: Secure boot is %s", hdev->name,
2105 params->secure_boot ? "enabled" : "disabled");
2106
Marcel Holtmann2220994e2015-10-13 13:54:55 +02002107 BT_INFO("%s: OTP lock is %s", hdev->name,
2108 params->otp_lock ? "enabled" : "disabled");
2109
2110 BT_INFO("%s: API lock is %s", hdev->name,
2111 params->api_lock ? "enabled" : "disabled");
2112
2113 BT_INFO("%s: Debug lock is %s", hdev->name,
2114 params->debug_lock ? "enabled" : "disabled");
2115
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002116 BT_INFO("%s: Minimum firmware build %u week %u %u", hdev->name,
2117 params->min_fw_build_nn, params->min_fw_build_cw,
2118 2000 + params->min_fw_build_yy);
2119
2120 /* It is required that every single firmware fragment is acknowledged
2121 * with a command complete event. If the boot parameters indicate
2122 * that this bootloader does not send them, then abort the setup.
2123 */
2124 if (params->limited_cce != 0x00) {
2125 BT_ERR("%s: Unsupported Intel firmware loading method (%u)",
2126 hdev->name, params->limited_cce);
2127 kfree_skb(skb);
2128 return -EINVAL;
2129 }
2130
2131 /* If the OTP has no valid Bluetooth device address, then there will
2132 * also be no valid address for the operational firmware.
2133 */
2134 if (!bacmp(&params->otp_bdaddr, BDADDR_ANY)) {
2135 BT_INFO("%s: No device address configured", hdev->name);
2136 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
2137 }
2138
2139 /* With this Intel bootloader only the hardware variant and device
2140 * revision information are used to select the right firmware.
2141 *
2142 * Currently this bootloader support is limited to hardware variant
2143 * iBT 3.0 (LnP/SfP) which is identified by the value 11 (0x0b).
2144 */
2145 snprintf(fwname, sizeof(fwname), "intel/ibt-11-%u.sfi",
2146 le16_to_cpu(params->dev_revid));
2147
2148 err = request_firmware(&fw, fwname, &hdev->dev);
2149 if (err < 0) {
2150 BT_ERR("%s: Failed to load Intel firmware file (%d)",
2151 hdev->name, err);
2152 kfree_skb(skb);
2153 return err;
2154 }
2155
2156 BT_INFO("%s: Found device firmware: %s", hdev->name, fwname);
2157
Tedd Ho-Jeong An52cc9162015-06-12 16:20:05 -07002158 /* Save the DDC file name for later use to apply once the firmware
2159 * downloading is done.
2160 */
2161 snprintf(fwname, sizeof(fwname), "intel/ibt-11-%u.ddc",
2162 le16_to_cpu(params->dev_revid));
2163
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002164 kfree_skb(skb);
2165
2166 if (fw->size < 644) {
2167 BT_ERR("%s: Invalid size of firmware file (%zu)",
2168 hdev->name, fw->size);
2169 err = -EBADF;
2170 goto done;
2171 }
2172
2173 set_bit(BTUSB_DOWNLOADING, &data->flags);
2174
2175 /* Start the firmware download transaction with the Init fragment
2176 * represented by the 128 bytes of CSS header.
2177 */
Marcel Holtmann09df1232015-07-05 14:55:36 +02002178 err = btintel_secure_send(hdev, 0x00, 128, fw->data);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002179 if (err < 0) {
2180 BT_ERR("%s: Failed to send firmware header (%d)",
2181 hdev->name, err);
2182 goto done;
2183 }
2184
2185 /* Send the 256 bytes of public key information from the firmware
2186 * as the PKey fragment.
2187 */
Marcel Holtmann09df1232015-07-05 14:55:36 +02002188 err = btintel_secure_send(hdev, 0x03, 256, fw->data + 128);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002189 if (err < 0) {
2190 BT_ERR("%s: Failed to send firmware public key (%d)",
2191 hdev->name, err);
2192 goto done;
2193 }
2194
2195 /* Send the 256 bytes of signature information from the firmware
2196 * as the Sign fragment.
2197 */
Marcel Holtmann09df1232015-07-05 14:55:36 +02002198 err = btintel_secure_send(hdev, 0x02, 256, fw->data + 388);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002199 if (err < 0) {
2200 BT_ERR("%s: Failed to send firmware signature (%d)",
2201 hdev->name, err);
2202 goto done;
2203 }
2204
2205 fw_ptr = fw->data + 644;
Marcel Holtmanne66890a92015-06-07 09:47:08 +02002206 frag_len = 0;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002207
2208 while (fw_ptr - fw->data < fw->size) {
Marcel Holtmanne66890a92015-06-07 09:47:08 +02002209 struct hci_command_hdr *cmd = (void *)(fw_ptr + frag_len);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002210
Marcel Holtmanne66890a92015-06-07 09:47:08 +02002211 frag_len += sizeof(*cmd) + cmd->plen;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002212
Minjune Kim5075eda2015-08-27 13:21:52 +09002213 /* The parameter length of the secure send command requires
Marcel Holtmanne66890a92015-06-07 09:47:08 +02002214 * a 4 byte alignment. It happens so that the firmware file
2215 * contains proper Intel_NOP commands to align the fragments
2216 * as needed.
2217 *
2218 * Send set of commands with 4 byte alignment from the
2219 * firmware data buffer as a single Data fragement.
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002220 */
Marcel Holtmanne66890a92015-06-07 09:47:08 +02002221 if (!(frag_len % 4)) {
Marcel Holtmann09df1232015-07-05 14:55:36 +02002222 err = btintel_secure_send(hdev, 0x01, frag_len, fw_ptr);
Marcel Holtmanne66890a92015-06-07 09:47:08 +02002223 if (err < 0) {
2224 BT_ERR("%s: Failed to send firmware data (%d)",
2225 hdev->name, err);
2226 goto done;
2227 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002228
Marcel Holtmanne66890a92015-06-07 09:47:08 +02002229 fw_ptr += frag_len;
2230 frag_len = 0;
2231 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002232 }
2233
Marcel Holtmannce6bb922015-01-28 01:58:40 -08002234 set_bit(BTUSB_FIRMWARE_LOADED, &data->flags);
2235
Johan Hedberga087a982015-01-30 10:58:54 +02002236 BT_INFO("%s: Waiting for firmware download to complete", hdev->name);
2237
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002238 /* Before switching the device into operational mode and with that
2239 * booting the loaded firmware, wait for the bootloader notification
2240 * that all fragments have been successfully received.
2241 *
Johan Hedberga087a982015-01-30 10:58:54 +02002242 * When the event processing receives the notification, then the
2243 * BTUSB_DOWNLOADING flag will be cleared.
2244 *
2245 * The firmware loading should not take longer than 5 seconds
2246 * and thus just timeout if that happens and fail the setup
2247 * of this device.
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002248 */
Johan Hedberg129a7692015-02-14 09:33:35 +02002249 err = wait_on_bit_timeout(&data->flags, BTUSB_DOWNLOADING,
2250 TASK_INTERRUPTIBLE,
2251 msecs_to_jiffies(5000));
Johan Hedberga087a982015-01-30 10:58:54 +02002252 if (err == 1) {
2253 BT_ERR("%s: Firmware loading interrupted", hdev->name);
2254 err = -EINTR;
2255 goto done;
2256 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002257
Johan Hedberga087a982015-01-30 10:58:54 +02002258 if (err) {
2259 BT_ERR("%s: Firmware loading timeout", hdev->name);
2260 err = -ETIMEDOUT;
2261 goto done;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002262 }
2263
2264 if (test_bit(BTUSB_FIRMWARE_FAILED, &data->flags)) {
2265 BT_ERR("%s: Firmware loading failed", hdev->name);
2266 err = -ENOEXEC;
2267 goto done;
2268 }
2269
2270 rettime = ktime_get();
2271 delta = ktime_sub(rettime, calltime);
2272 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
2273
2274 BT_INFO("%s: Firmware loaded in %llu usecs", hdev->name, duration);
2275
2276done:
2277 release_firmware(fw);
2278
2279 if (err < 0)
2280 return err;
2281
2282 calltime = ktime_get();
2283
2284 set_bit(BTUSB_BOOTING, &data->flags);
2285
2286 skb = __hci_cmd_sync(hdev, 0xfc01, sizeof(reset_param), reset_param,
2287 HCI_INIT_TIMEOUT);
2288 if (IS_ERR(skb))
2289 return PTR_ERR(skb);
2290
2291 kfree_skb(skb);
2292
2293 /* The bootloader will not indicate when the device is ready. This
2294 * is done by the operational firmware sending bootup notification.
Johan Hedbergfad70972015-01-30 10:58:55 +02002295 *
2296 * Booting into operational firmware should not take longer than
2297 * 1 second. However if that happens, then just fail the setup
2298 * since something went wrong.
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002299 */
Johan Hedbergfad70972015-01-30 10:58:55 +02002300 BT_INFO("%s: Waiting for device to boot", hdev->name);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002301
Johan Hedberg129a7692015-02-14 09:33:35 +02002302 err = wait_on_bit_timeout(&data->flags, BTUSB_BOOTING,
2303 TASK_INTERRUPTIBLE,
2304 msecs_to_jiffies(1000));
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002305
Johan Hedbergfad70972015-01-30 10:58:55 +02002306 if (err == 1) {
2307 BT_ERR("%s: Device boot interrupted", hdev->name);
2308 return -EINTR;
2309 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002310
Johan Hedbergfad70972015-01-30 10:58:55 +02002311 if (err) {
2312 BT_ERR("%s: Device boot timeout", hdev->name);
2313 return -ETIMEDOUT;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002314 }
2315
2316 rettime = ktime_get();
2317 delta = ktime_sub(rettime, calltime);
2318 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
2319
2320 BT_INFO("%s: Device booted in %llu usecs", hdev->name, duration);
2321
2322 clear_bit(BTUSB_BOOTLOADER, &data->flags);
2323
Tedd Ho-Jeong An52cc9162015-06-12 16:20:05 -07002324 /* Once the device is running in operational mode, it needs to apply
2325 * the device configuration (DDC) parameters.
2326 *
2327 * The device can work without DDC parameters, so even if it fails
2328 * to load the file, no need to fail the setup.
2329 */
Loic Poulaine924d3d2015-09-04 17:54:36 +02002330 btintel_load_ddc_config(hdev, fwname);
Tedd Ho-Jeong An52cc9162015-06-12 16:20:05 -07002331
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002332 return 0;
2333}
2334
Tedd Ho-Jeong Anbfbd45e2015-02-13 09:20:52 -08002335static int btusb_shutdown_intel(struct hci_dev *hdev)
2336{
2337 struct sk_buff *skb;
2338 long ret;
2339
2340 /* Some platforms have an issue with BT LED when the interface is
2341 * down or BT radio is turned off, which takes 5 seconds to BT LED
2342 * goes off. This command turns off the BT LED immediately.
2343 */
2344 skb = __hci_cmd_sync(hdev, 0xfc3f, 0, NULL, HCI_INIT_TIMEOUT);
2345 if (IS_ERR(skb)) {
2346 ret = PTR_ERR(skb);
2347 BT_ERR("%s: turning off Intel device LED failed (%ld)",
2348 hdev->name, ret);
2349 return ret;
2350 }
2351 kfree_skb(skb);
2352
2353 return 0;
2354}
2355
Amitkumar Karwarae8df492014-07-18 14:47:06 -07002356static int btusb_set_bdaddr_marvell(struct hci_dev *hdev,
2357 const bdaddr_t *bdaddr)
2358{
2359 struct sk_buff *skb;
2360 u8 buf[8];
2361 long ret;
2362
2363 buf[0] = 0xfe;
2364 buf[1] = sizeof(bdaddr_t);
2365 memcpy(buf + 2, bdaddr, sizeof(bdaddr_t));
2366
2367 skb = __hci_cmd_sync(hdev, 0xfc22, sizeof(buf), buf, HCI_INIT_TIMEOUT);
2368 if (IS_ERR(skb)) {
2369 ret = PTR_ERR(skb);
2370 BT_ERR("%s: changing Marvell device address failed (%ld)",
2371 hdev->name, ret);
2372 return ret;
2373 }
2374 kfree_skb(skb);
2375
2376 return 0;
2377}
2378
Toshi Kikuchi58592232014-12-12 10:58:05 -08002379static int btusb_set_bdaddr_ath3012(struct hci_dev *hdev,
2380 const bdaddr_t *bdaddr)
2381{
2382 struct sk_buff *skb;
2383 u8 buf[10];
2384 long ret;
2385
2386 buf[0] = 0x01;
2387 buf[1] = 0x01;
2388 buf[2] = 0x00;
2389 buf[3] = sizeof(bdaddr_t);
2390 memcpy(buf + 4, bdaddr, sizeof(bdaddr_t));
2391
2392 skb = __hci_cmd_sync(hdev, 0xfc0b, sizeof(buf), buf, HCI_INIT_TIMEOUT);
2393 if (IS_ERR(skb)) {
2394 ret = PTR_ERR(skb);
2395 BT_ERR("%s: Change address command failed (%ld)",
2396 hdev->name, ret);
2397 return ret;
2398 }
2399 kfree_skb(skb);
2400
2401 return 0;
2402}
2403
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002404#define QCA_DFU_PACKET_LEN 4096
2405
2406#define QCA_GET_TARGET_VERSION 0x09
2407#define QCA_CHECK_STATUS 0x05
2408#define QCA_DFU_DOWNLOAD 0x01
2409
2410#define QCA_SYSCFG_UPDATED 0x40
2411#define QCA_PATCH_UPDATED 0x80
2412#define QCA_DFU_TIMEOUT 3000
2413
2414struct qca_version {
2415 __le32 rom_version;
2416 __le32 patch_version;
2417 __le32 ram_version;
2418 __le32 ref_clock;
2419 __u8 reserved[4];
2420} __packed;
2421
2422struct qca_rampatch_version {
2423 __le16 rom_version;
2424 __le16 patch_version;
2425} __packed;
2426
2427struct qca_device_info {
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002428 u32 rom_version;
2429 u8 rampatch_hdr; /* length of header in rampatch */
2430 u8 nvm_hdr; /* length of header in NVM */
2431 u8 ver_offset; /* offset of version structure in rampatch */
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002432};
2433
2434static const struct qca_device_info qca_devices_table[] = {
2435 { 0x00000100, 20, 4, 10 }, /* Rome 1.0 */
2436 { 0x00000101, 20, 4, 10 }, /* Rome 1.1 */
Chan-yeol Park7f6e6362015-05-21 11:24:27 +09002437 { 0x00000200, 28, 4, 18 }, /* Rome 2.0 */
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002438 { 0x00000201, 28, 4, 18 }, /* Rome 2.1 */
2439 { 0x00000300, 28, 4, 18 }, /* Rome 3.0 */
2440 { 0x00000302, 28, 4, 18 }, /* Rome 3.2 */
2441};
2442
2443static int btusb_qca_send_vendor_req(struct hci_dev *hdev, u8 request,
2444 void *data, u16 size)
2445{
2446 struct btusb_data *btdata = hci_get_drvdata(hdev);
2447 struct usb_device *udev = btdata->udev;
2448 int pipe, err;
2449 u8 *buf;
2450
2451 buf = kmalloc(size, GFP_KERNEL);
2452 if (!buf)
2453 return -ENOMEM;
2454
2455 /* Found some of USB hosts have IOT issues with ours so that we should
2456 * not wait until HCI layer is ready.
2457 */
2458 pipe = usb_rcvctrlpipe(udev, 0);
2459 err = usb_control_msg(udev, pipe, request, USB_TYPE_VENDOR | USB_DIR_IN,
2460 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
2461 if (err < 0) {
2462 BT_ERR("%s: Failed to access otp area (%d)", hdev->name, err);
2463 goto done;
2464 }
2465
2466 memcpy(data, buf, size);
2467
2468done:
2469 kfree(buf);
2470
2471 return err;
2472}
2473
2474static int btusb_setup_qca_download_fw(struct hci_dev *hdev,
2475 const struct firmware *firmware,
2476 size_t hdr_size)
2477{
2478 struct btusb_data *btdata = hci_get_drvdata(hdev);
2479 struct usb_device *udev = btdata->udev;
2480 size_t count, size, sent = 0;
2481 int pipe, len, err;
2482 u8 *buf;
2483
2484 buf = kmalloc(QCA_DFU_PACKET_LEN, GFP_KERNEL);
2485 if (!buf)
2486 return -ENOMEM;
2487
2488 count = firmware->size;
2489
2490 size = min_t(size_t, count, hdr_size);
2491 memcpy(buf, firmware->data, size);
2492
2493 /* USB patches should go down to controller through USB path
2494 * because binary format fits to go down through USB channel.
2495 * USB control path is for patching headers and USB bulk is for
2496 * patch body.
2497 */
2498 pipe = usb_sndctrlpipe(udev, 0);
2499 err = usb_control_msg(udev, pipe, QCA_DFU_DOWNLOAD, USB_TYPE_VENDOR,
2500 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
2501 if (err < 0) {
2502 BT_ERR("%s: Failed to send headers (%d)", hdev->name, err);
2503 goto done;
2504 }
2505
2506 sent += size;
2507 count -= size;
2508
2509 while (count) {
2510 size = min_t(size_t, count, QCA_DFU_PACKET_LEN);
2511
2512 memcpy(buf, firmware->data + sent, size);
2513
2514 pipe = usb_sndbulkpipe(udev, 0x02);
2515 err = usb_bulk_msg(udev, pipe, buf, size, &len,
2516 QCA_DFU_TIMEOUT);
2517 if (err < 0) {
2518 BT_ERR("%s: Failed to send body at %zd of %zd (%d)",
2519 hdev->name, sent, firmware->size, err);
2520 break;
2521 }
2522
2523 if (size != len) {
2524 BT_ERR("%s: Failed to get bulk buffer", hdev->name);
2525 err = -EILSEQ;
2526 break;
2527 }
2528
2529 sent += size;
2530 count -= size;
2531 }
2532
2533done:
2534 kfree(buf);
2535 return err;
2536}
2537
2538static int btusb_setup_qca_load_rampatch(struct hci_dev *hdev,
2539 struct qca_version *ver,
2540 const struct qca_device_info *info)
2541{
2542 struct qca_rampatch_version *rver;
2543 const struct firmware *fw;
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002544 u32 ver_rom, ver_patch;
2545 u16 rver_rom, rver_patch;
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002546 char fwname[64];
2547 int err;
2548
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002549 ver_rom = le32_to_cpu(ver->rom_version);
2550 ver_patch = le32_to_cpu(ver->patch_version);
2551
2552 snprintf(fwname, sizeof(fwname), "qca/rampatch_usb_%08x.bin", ver_rom);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002553
2554 err = request_firmware(&fw, fwname, &hdev->dev);
2555 if (err) {
2556 BT_ERR("%s: failed to request rampatch file: %s (%d)",
2557 hdev->name, fwname, err);
2558 return err;
2559 }
2560
2561 BT_INFO("%s: using rampatch file: %s", hdev->name, fwname);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002562
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002563 rver = (struct qca_rampatch_version *)(fw->data + info->ver_offset);
2564 rver_rom = le16_to_cpu(rver->rom_version);
2565 rver_patch = le16_to_cpu(rver->patch_version);
2566
2567 BT_INFO("%s: QCA: patch rome 0x%x build 0x%x, firmware rome 0x%x "
2568 "build 0x%x", hdev->name, rver_rom, rver_patch, ver_rom,
2569 ver_patch);
2570
2571 if (rver_rom != ver_rom || rver_patch <= ver_patch) {
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002572 BT_ERR("%s: rampatch file version did not match with firmware",
2573 hdev->name);
2574 err = -EINVAL;
2575 goto done;
2576 }
2577
2578 err = btusb_setup_qca_download_fw(hdev, fw, info->rampatch_hdr);
2579
2580done:
2581 release_firmware(fw);
2582
2583 return err;
2584}
2585
2586static int btusb_setup_qca_load_nvm(struct hci_dev *hdev,
2587 struct qca_version *ver,
2588 const struct qca_device_info *info)
2589{
2590 const struct firmware *fw;
2591 char fwname[64];
2592 int err;
2593
2594 snprintf(fwname, sizeof(fwname), "qca/nvm_usb_%08x.bin",
2595 le32_to_cpu(ver->rom_version));
2596
2597 err = request_firmware(&fw, fwname, &hdev->dev);
2598 if (err) {
2599 BT_ERR("%s: failed to request NVM file: %s (%d)",
2600 hdev->name, fwname, err);
2601 return err;
2602 }
2603
2604 BT_INFO("%s: using NVM file: %s", hdev->name, fwname);
2605
2606 err = btusb_setup_qca_download_fw(hdev, fw, info->nvm_hdr);
2607
2608 release_firmware(fw);
2609
2610 return err;
2611}
2612
2613static int btusb_setup_qca(struct hci_dev *hdev)
2614{
2615 const struct qca_device_info *info = NULL;
2616 struct qca_version ver;
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002617 u32 ver_rom;
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002618 u8 status;
2619 int i, err;
2620
2621 err = btusb_qca_send_vendor_req(hdev, QCA_GET_TARGET_VERSION, &ver,
Marcel Holtmanneb500422015-04-16 23:15:50 +02002622 sizeof(ver));
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002623 if (err < 0)
2624 return err;
2625
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002626 ver_rom = le32_to_cpu(ver.rom_version);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002627 for (i = 0; i < ARRAY_SIZE(qca_devices_table); i++) {
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002628 if (ver_rom == qca_devices_table[i].rom_version)
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002629 info = &qca_devices_table[i];
2630 }
2631 if (!info) {
2632 BT_ERR("%s: don't support firmware rome 0x%x", hdev->name,
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002633 ver_rom);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002634 return -ENODEV;
2635 }
2636
2637 err = btusb_qca_send_vendor_req(hdev, QCA_CHECK_STATUS, &status,
2638 sizeof(status));
2639 if (err < 0)
2640 return err;
2641
2642 if (!(status & QCA_PATCH_UPDATED)) {
2643 err = btusb_setup_qca_load_rampatch(hdev, &ver, info);
2644 if (err < 0)
2645 return err;
2646 }
2647
2648 if (!(status & QCA_SYSCFG_UPDATED)) {
2649 err = btusb_setup_qca_load_nvm(hdev, &ver, info);
2650 if (err < 0)
2651 return err;
2652 }
2653
2654 return 0;
2655}
2656
Marcel Holtmann9d08f502015-10-08 20:23:08 +02002657#ifdef CONFIG_BT_HCIBTUSB_BCM
2658static inline int __set_diag_interface(struct hci_dev *hdev)
2659{
2660 struct btusb_data *data = hci_get_drvdata(hdev);
2661 struct usb_interface *intf = data->diag;
2662 int i;
2663
2664 if (!data->diag)
2665 return -ENODEV;
2666
2667 data->diag_tx_ep = NULL;
2668 data->diag_rx_ep = NULL;
2669
2670 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
2671 struct usb_endpoint_descriptor *ep_desc;
2672
2673 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
2674
2675 if (!data->diag_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
2676 data->diag_tx_ep = ep_desc;
2677 continue;
2678 }
2679
2680 if (!data->diag_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
2681 data->diag_rx_ep = ep_desc;
2682 continue;
2683 }
2684 }
2685
2686 if (!data->diag_tx_ep || !data->diag_rx_ep) {
2687 BT_ERR("%s invalid diagnostic descriptors", hdev->name);
2688 return -ENODEV;
2689 }
2690
2691 return 0;
2692}
2693
2694static struct urb *alloc_diag_urb(struct hci_dev *hdev, bool enable)
2695{
2696 struct btusb_data *data = hci_get_drvdata(hdev);
2697 struct sk_buff *skb;
2698 struct urb *urb;
2699 unsigned int pipe;
2700
2701 if (!data->diag_tx_ep)
2702 return ERR_PTR(-ENODEV);
2703
2704 urb = usb_alloc_urb(0, GFP_KERNEL);
2705 if (!urb)
2706 return ERR_PTR(-ENOMEM);
2707
2708 skb = bt_skb_alloc(2, GFP_KERNEL);
2709 if (!skb) {
2710 usb_free_urb(urb);
2711 return ERR_PTR(-ENOMEM);
2712 }
2713
2714 *skb_put(skb, 1) = 0xf0;
2715 *skb_put(skb, 1) = enable;
2716
2717 pipe = usb_sndbulkpipe(data->udev, data->diag_tx_ep->bEndpointAddress);
2718
2719 usb_fill_bulk_urb(urb, data->udev, pipe,
2720 skb->data, skb->len, btusb_tx_complete, skb);
2721
2722 skb->dev = (void *)hdev;
2723
2724 return urb;
2725}
2726
2727static int btusb_bcm_set_diag(struct hci_dev *hdev, bool enable)
2728{
2729 struct btusb_data *data = hci_get_drvdata(hdev);
2730 struct urb *urb;
2731
2732 if (!data->diag)
2733 return -ENODEV;
2734
2735 if (!test_bit(HCI_RUNNING, &hdev->flags))
2736 return -ENETDOWN;
2737
2738 urb = alloc_diag_urb(hdev, enable);
2739 if (IS_ERR(urb))
2740 return PTR_ERR(urb);
2741
2742 return submit_or_queue_tx_urb(hdev, urb);
2743}
2744#endif
2745
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002746static int btusb_probe(struct usb_interface *intf,
Marcel Holtmann89e75332014-09-16 04:44:50 +02002747 const struct usb_device_id *id)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002748{
2749 struct usb_endpoint_descriptor *ep_desc;
2750 struct btusb_data *data;
2751 struct hci_dev *hdev;
2752 int i, err;
2753
2754 BT_DBG("intf %p id %p", intf, id);
2755
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002756 /* interface numbers are hardcoded in the spec */
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002757 if (intf->cur_altsetting->desc.bInterfaceNumber != 0)
2758 return -ENODEV;
2759
2760 if (!id->driver_info) {
2761 const struct usb_device_id *match;
Marcel Holtmann89e75332014-09-16 04:44:50 +02002762
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002763 match = usb_match_id(intf, blacklist_table);
2764 if (match)
2765 id = match;
2766 }
2767
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002768 if (id->driver_info == BTUSB_IGNORE)
2769 return -ENODEV;
2770
Steven.Li2d25f8b2011-07-01 14:02:36 +08002771 if (id->driver_info & BTUSB_ATH3012) {
2772 struct usb_device *udev = interface_to_usbdev(intf);
2773
2774 /* Old firmware would otherwise let ath3k driver load
2775 * patch and sysconfig files */
2776 if (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x0001)
2777 return -ENODEV;
2778 }
2779
Sachin Kamat98921db2012-07-27 12:38:39 +05302780 data = devm_kzalloc(&intf->dev, sizeof(*data), GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002781 if (!data)
2782 return -ENOMEM;
2783
2784 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
2785 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
2786
2787 if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
2788 data->intr_ep = ep_desc;
2789 continue;
2790 }
2791
2792 if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
2793 data->bulk_tx_ep = ep_desc;
2794 continue;
2795 }
2796
2797 if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
2798 data->bulk_rx_ep = ep_desc;
2799 continue;
2800 }
2801 }
2802
Sachin Kamat98921db2012-07-27 12:38:39 +05302803 if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002804 return -ENODEV;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002805
Marcel Holtmann893ba542015-01-28 20:27:34 -08002806 if (id->driver_info & BTUSB_AMP) {
2807 data->cmdreq_type = USB_TYPE_CLASS | 0x01;
2808 data->cmdreq = 0x2b;
2809 } else {
2810 data->cmdreq_type = USB_TYPE_CLASS;
2811 data->cmdreq = 0x00;
2812 }
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002813
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002814 data->udev = interface_to_usbdev(intf);
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02002815 data->intf = intf;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002816
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002817 INIT_WORK(&data->work, btusb_work);
Oliver Neukum7bee5492009-08-24 23:44:59 +02002818 INIT_WORK(&data->waker, btusb_waker);
Marcel Holtmann803b5832014-09-16 08:00:29 +02002819 init_usb_anchor(&data->deferred);
2820 init_usb_anchor(&data->tx_anchor);
Oliver Neukum7bee5492009-08-24 23:44:59 +02002821 spin_lock_init(&data->txlock);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002822
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002823 init_usb_anchor(&data->intr_anchor);
2824 init_usb_anchor(&data->bulk_anchor);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02002825 init_usb_anchor(&data->isoc_anchor);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02002826 init_usb_anchor(&data->diag_anchor);
Marcel Holtmann803b5832014-09-16 08:00:29 +02002827 spin_lock_init(&data->rxlock);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002828
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002829 if (id->driver_info & BTUSB_INTEL_NEW) {
2830 data->recv_event = btusb_recv_event_intel;
2831 data->recv_bulk = btusb_recv_bulk_intel;
2832 set_bit(BTUSB_BOOTLOADER, &data->flags);
2833 } else {
2834 data->recv_event = hci_recv_frame;
2835 data->recv_bulk = btusb_recv_bulk;
2836 }
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +01002837
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002838 hdev = hci_alloc_dev();
Sachin Kamat98921db2012-07-27 12:38:39 +05302839 if (!hdev)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002840 return -ENOMEM;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002841
Marcel Holtmannc13854c2010-02-08 15:27:07 +01002842 hdev->bus = HCI_USB;
David Herrmann155961e2012-02-09 21:58:32 +01002843 hci_set_drvdata(hdev, data);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002844
Marcel Holtmann893ba542015-01-28 20:27:34 -08002845 if (id->driver_info & BTUSB_AMP)
2846 hdev->dev_type = HCI_AMP;
2847 else
2848 hdev->dev_type = HCI_BREDR;
2849
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002850 data->hdev = hdev;
2851
2852 SET_HCIDEV_DEV(hdev, &intf->dev);
2853
Marcel Holtmann9f8f9622013-04-10 08:11:35 -07002854 hdev->open = btusb_open;
2855 hdev->close = btusb_close;
2856 hdev->flush = btusb_flush;
2857 hdev->send = btusb_send_frame;
2858 hdev->notify = btusb_notify;
2859
2860 if (id->driver_info & BTUSB_BCM92035)
2861 hdev->setup = btusb_setup_bcm92035;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002862
Marcel Holtmannc2bfb102015-04-05 22:52:14 -07002863#ifdef CONFIG_BT_HCIBTUSB_BCM
Marcel Holtmannabbaf502014-07-02 00:53:48 +02002864 if (id->driver_info & BTUSB_BCM_PATCHRAM) {
Marcel Holtmannc2bfb102015-04-05 22:52:14 -07002865 hdev->setup = btbcm_setup_patchram;
Marcel Holtmann9d08f502015-10-08 20:23:08 +02002866 hdev->set_diag = btusb_bcm_set_diag;
Marcel Holtmann1df1f592015-04-05 22:52:11 -07002867 hdev->set_bdaddr = btbcm_set_bdaddr;
Marcel Holtmann9d08f502015-10-08 20:23:08 +02002868
2869 /* Broadcom LM_DIAG Interface numbers are hardcoded */
2870 data->diag = usb_ifnum_to_if(data->udev, 2);
Marcel Holtmannabbaf502014-07-02 00:53:48 +02002871 }
Petri Gynther10d4c672014-05-08 15:50:01 -07002872
Marcel Holtmann9d08f502015-10-08 20:23:08 +02002873 if (id->driver_info & BTUSB_BCM_APPLE) {
Marcel Holtmannc2bfb102015-04-05 22:52:14 -07002874 hdev->setup = btbcm_setup_apple;
Marcel Holtmann9d08f502015-10-08 20:23:08 +02002875 hdev->set_diag = btusb_bcm_set_diag;
2876
2877 /* Broadcom LM_DIAG Interface numbers are hardcoded */
2878 data->diag = usb_ifnum_to_if(data->udev, 2);
2879 }
Marcel Holtmannc2bfb102015-04-05 22:52:14 -07002880#endif
Marcel Holtmann17b27722015-03-22 15:52:38 +01002881
Marcel Holtmanncb8d6592014-07-02 11:25:25 +02002882 if (id->driver_info & BTUSB_INTEL) {
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002883 hdev->setup = btusb_setup_intel;
Tedd Ho-Jeong Anbfbd45e2015-02-13 09:20:52 -08002884 hdev->shutdown = btusb_shutdown_intel;
Marcel Holtmann3e247672015-10-17 16:00:28 +02002885 hdev->set_diag = btintel_set_diag_mfg;
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07002886 hdev->set_bdaddr = btintel_set_bdaddr;
Jakub Pawlowskic33fb9b2015-01-30 18:55:58 -08002887 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
Jakub Pawlowskic1154842015-03-17 09:04:16 -07002888 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
Marcel Holtmann3e247672015-10-17 16:00:28 +02002889 set_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks);
Marcel Holtmanncb8d6592014-07-02 11:25:25 +02002890 }
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002891
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002892 if (id->driver_info & BTUSB_INTEL_NEW) {
2893 hdev->send = btusb_send_frame_intel;
2894 hdev->setup = btusb_setup_intel_new;
Marcel Holtmanneeb6abe2015-07-05 14:37:39 +02002895 hdev->hw_error = btintel_hw_error;
Marcel Holtmann6d2e50d2015-10-09 14:42:08 +02002896 hdev->set_diag = btintel_set_diag;
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07002897 hdev->set_bdaddr = btintel_set_bdaddr;
Marcel Holtmannb970c5b2015-02-01 23:57:18 -08002898 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
Marcel Holtmannd8270fb2015-10-17 16:00:27 +02002899 set_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002900 }
2901
Amitkumar Karwarae8df492014-07-18 14:47:06 -07002902 if (id->driver_info & BTUSB_MARVELL)
2903 hdev->set_bdaddr = btusb_set_bdaddr_marvell;
2904
Marcel Holtmann661cf882015-01-02 23:35:20 -08002905 if (id->driver_info & BTUSB_SWAVE) {
2906 set_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks);
Marcel Holtmannd57dbe72014-12-26 04:42:33 +01002907 set_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS, &hdev->quirks);
Marcel Holtmann661cf882015-01-02 23:35:20 -08002908 }
Marcel Holtmannd57dbe72014-12-26 04:42:33 +01002909
Marcel Holtmann40df7832014-07-06 13:29:58 +02002910 if (id->driver_info & BTUSB_INTEL_BOOT)
2911 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
2912
Jakub Pawlowski79f0c872015-01-29 10:38:34 -08002913 if (id->driver_info & BTUSB_ATH3012) {
Toshi Kikuchi58592232014-12-12 10:58:05 -08002914 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
Jakub Pawlowski3d50d512015-03-17 09:04:15 -07002915 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
Jakub Pawlowski79f0c872015-01-29 10:38:34 -08002916 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
2917 }
Toshi Kikuchi58592232014-12-12 10:58:05 -08002918
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002919 if (id->driver_info & BTUSB_QCA_ROME) {
2920 data->setup_on_usb = btusb_setup_qca;
2921 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
2922 }
2923
Carlo Caionedb33c772015-05-14 10:49:09 +02002924#ifdef CONFIG_BT_HCIBTUSB_RTL
Daniel Drake04b8c812015-05-21 08:23:50 -06002925 if (id->driver_info & BTUSB_REALTEK) {
Carlo Caionedb33c772015-05-14 10:49:09 +02002926 hdev->setup = btrtl_setup_realtek;
Daniel Drake04b8c812015-05-21 08:23:50 -06002927
2928 /* Realtek devices lose their updated firmware over suspend,
2929 * but the USB hub doesn't notice any status change.
2930 * Explicitly request a device reset on resume.
2931 */
2932 set_bit(BTUSB_RESET_RESUME, &data->flags);
2933 }
Carlo Caionedb33c772015-05-14 10:49:09 +02002934#endif
Daniel Drakea2698a92015-04-16 14:09:55 -06002935
Marcel Holtmann893ba542015-01-28 20:27:34 -08002936 if (id->driver_info & BTUSB_AMP) {
2937 /* AMP controllers do not support SCO packets */
2938 data->isoc = NULL;
2939 } else {
2940 /* Interface numbers are hardcoded in the specification */
2941 data->isoc = usb_ifnum_to_if(data->udev, 1);
2942 }
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02002943
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002944 if (!reset)
Szymon Janca6c511c2012-05-23 12:35:46 +02002945 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002946
2947 if (force_scofix || id->driver_info & BTUSB_WRONG_SCO_MTU) {
2948 if (!disable_scofix)
2949 set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
2950 }
2951
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02002952 if (id->driver_info & BTUSB_BROKEN_ISOC)
2953 data->isoc = NULL;
2954
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002955 if (id->driver_info & BTUSB_DIGIANSWER) {
2956 data->cmdreq_type = USB_TYPE_VENDOR;
Szymon Janca6c511c2012-05-23 12:35:46 +02002957 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002958 }
2959
2960 if (id->driver_info & BTUSB_CSR) {
2961 struct usb_device *udev = data->udev;
Marcel Holtmann81cac642014-01-03 03:02:36 -08002962 u16 bcdDevice = le16_to_cpu(udev->descriptor.bcdDevice);
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002963
2964 /* Old firmware would otherwise execute USB reset */
Marcel Holtmann81cac642014-01-03 03:02:36 -08002965 if (bcdDevice < 0x117)
Szymon Janca6c511c2012-05-23 12:35:46 +02002966 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmann81cac642014-01-03 03:02:36 -08002967
2968 /* Fake CSR devices with broken commands */
Johan Hedberg6cafcd92015-08-30 21:47:21 +03002969 if (bcdDevice <= 0x100 || bcdDevice == 0x134)
Marcel Holtmann81cac642014-01-03 03:02:36 -08002970 hdev->setup = btusb_setup_csr;
Jakub Pawlowski49c989a2015-03-17 09:04:17 -07002971
2972 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002973 }
2974
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002975 if (id->driver_info & BTUSB_SNIFFER) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02002976 struct usb_device *udev = data->udev;
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002977
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002978 /* New sniffer firmware has crippled HCI interface */
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002979 if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
2980 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
2981 }
2982
Marcel Holtmann3a5ef202014-07-06 14:53:54 +02002983 if (id->driver_info & BTUSB_INTEL_BOOT) {
2984 /* A bug in the bootloader causes that interrupt interface is
2985 * only enabled after receiving SetInterface(0, AltSetting=0).
2986 */
2987 err = usb_set_interface(data->udev, 0, 0);
2988 if (err < 0) {
2989 BT_ERR("failed to set interface 0, alt 0 %d", err);
2990 hci_free_dev(hdev);
2991 return err;
2992 }
2993 }
2994
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02002995 if (data->isoc) {
2996 err = usb_driver_claim_interface(&btusb_driver,
Marcel Holtmann89e75332014-09-16 04:44:50 +02002997 data->isoc, data);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02002998 if (err < 0) {
2999 hci_free_dev(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02003000 return err;
3001 }
3002 }
3003
Marcel Holtmann9d08f502015-10-08 20:23:08 +02003004#ifdef CONFIG_BT_HCIBTUSB_BCM
3005 if (data->diag) {
3006 if (!usb_driver_claim_interface(&btusb_driver,
3007 data->diag, data))
3008 __set_diag_interface(hdev);
3009 else
3010 data->diag = NULL;
3011 }
3012#endif
3013
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003014 err = hci_register_dev(hdev);
3015 if (err < 0) {
3016 hci_free_dev(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003017 return err;
3018 }
3019
3020 usb_set_intfdata(intf, data);
3021
3022 return 0;
3023}
3024
3025static void btusb_disconnect(struct usb_interface *intf)
3026{
3027 struct btusb_data *data = usb_get_intfdata(intf);
3028 struct hci_dev *hdev;
3029
3030 BT_DBG("intf %p", intf);
3031
3032 if (!data)
3033 return;
3034
3035 hdev = data->hdev;
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02003036 usb_set_intfdata(data->intf, NULL);
3037
3038 if (data->isoc)
3039 usb_set_intfdata(data->isoc, NULL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003040
Marcel Holtmann9d08f502015-10-08 20:23:08 +02003041 if (data->diag)
3042 usb_set_intfdata(data->diag, NULL);
3043
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003044 hci_unregister_dev(hdev);
3045
Marcel Holtmann9d08f502015-10-08 20:23:08 +02003046 if (intf == data->intf) {
3047 if (data->isoc)
3048 usb_driver_release_interface(&btusb_driver, data->isoc);
3049 if (data->diag)
3050 usb_driver_release_interface(&btusb_driver, data->diag);
3051 } else if (intf == data->isoc) {
3052 if (data->diag)
3053 usb_driver_release_interface(&btusb_driver, data->diag);
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02003054 usb_driver_release_interface(&btusb_driver, data->intf);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02003055 } else if (intf == data->diag) {
3056 usb_driver_release_interface(&btusb_driver, data->intf);
3057 if (data->isoc)
3058 usb_driver_release_interface(&btusb_driver, data->isoc);
3059 }
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02003060
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003061 hci_free_dev(hdev);
3062}
3063
Oliver Neukum7bee5492009-08-24 23:44:59 +02003064#ifdef CONFIG_PM
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003065static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
3066{
3067 struct btusb_data *data = usb_get_intfdata(intf);
3068
3069 BT_DBG("intf %p", intf);
3070
3071 if (data->suspend_count++)
3072 return 0;
3073
Oliver Neukum7bee5492009-08-24 23:44:59 +02003074 spin_lock_irq(&data->txlock);
Alan Stern5b1b0b82011-08-19 23:49:48 +02003075 if (!(PMSG_IS_AUTO(message) && data->tx_in_flight)) {
Oliver Neukum7bee5492009-08-24 23:44:59 +02003076 set_bit(BTUSB_SUSPENDING, &data->flags);
3077 spin_unlock_irq(&data->txlock);
3078 } else {
3079 spin_unlock_irq(&data->txlock);
3080 data->suspend_count--;
3081 return -EBUSY;
3082 }
3083
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003084 cancel_work_sync(&data->work);
3085
Oliver Neukum7bee5492009-08-24 23:44:59 +02003086 btusb_stop_traffic(data);
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003087 usb_kill_anchored_urbs(&data->tx_anchor);
3088
Daniel Drake04b8c812015-05-21 08:23:50 -06003089 /* Optionally request a device reset on resume, but only when
3090 * wakeups are disabled. If wakeups are enabled we assume the
3091 * device will stay powered up throughout suspend.
3092 */
3093 if (test_bit(BTUSB_RESET_RESUME, &data->flags) &&
3094 !device_may_wakeup(&data->udev->dev))
3095 data->udev->reset_resume = 1;
3096
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003097 return 0;
3098}
3099
Oliver Neukum7bee5492009-08-24 23:44:59 +02003100static void play_deferred(struct btusb_data *data)
3101{
3102 struct urb *urb;
3103 int err;
3104
3105 while ((urb = usb_get_from_anchor(&data->deferred))) {
3106 err = usb_submit_urb(urb, GFP_ATOMIC);
3107 if (err < 0)
3108 break;
3109
3110 data->tx_in_flight++;
3111 }
3112 usb_scuttle_anchored_urbs(&data->deferred);
3113}
3114
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003115static int btusb_resume(struct usb_interface *intf)
3116{
3117 struct btusb_data *data = usb_get_intfdata(intf);
3118 struct hci_dev *hdev = data->hdev;
Oliver Neukum7bee5492009-08-24 23:44:59 +02003119 int err = 0;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003120
3121 BT_DBG("intf %p", intf);
3122
3123 if (--data->suspend_count)
3124 return 0;
3125
3126 if (!test_bit(HCI_RUNNING, &hdev->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +02003127 goto done;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003128
3129 if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
3130 err = btusb_submit_intr_urb(hdev, GFP_NOIO);
3131 if (err < 0) {
3132 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02003133 goto failed;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003134 }
3135 }
3136
3137 if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) {
Marcel Holtmann43c2e572009-02-04 17:41:38 +01003138 err = btusb_submit_bulk_urb(hdev, GFP_NOIO);
3139 if (err < 0) {
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003140 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02003141 goto failed;
3142 }
3143
3144 btusb_submit_bulk_urb(hdev, GFP_NOIO);
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003145 }
3146
3147 if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
3148 if (btusb_submit_isoc_urb(hdev, GFP_NOIO) < 0)
3149 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
3150 else
3151 btusb_submit_isoc_urb(hdev, GFP_NOIO);
3152 }
3153
Oliver Neukum7bee5492009-08-24 23:44:59 +02003154 spin_lock_irq(&data->txlock);
3155 play_deferred(data);
3156 clear_bit(BTUSB_SUSPENDING, &data->flags);
3157 spin_unlock_irq(&data->txlock);
3158 schedule_work(&data->work);
3159
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003160 return 0;
Oliver Neukum7bee5492009-08-24 23:44:59 +02003161
3162failed:
3163 usb_scuttle_anchored_urbs(&data->deferred);
3164done:
3165 spin_lock_irq(&data->txlock);
3166 clear_bit(BTUSB_SUSPENDING, &data->flags);
3167 spin_unlock_irq(&data->txlock);
3168
3169 return err;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003170}
Oliver Neukum7bee5492009-08-24 23:44:59 +02003171#endif
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003172
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003173static struct usb_driver btusb_driver = {
3174 .name = "btusb",
3175 .probe = btusb_probe,
3176 .disconnect = btusb_disconnect,
Oliver Neukum7bee5492009-08-24 23:44:59 +02003177#ifdef CONFIG_PM
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003178 .suspend = btusb_suspend,
3179 .resume = btusb_resume,
Oliver Neukum7bee5492009-08-24 23:44:59 +02003180#endif
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003181 .id_table = btusb_table,
Oliver Neukum7bee5492009-08-24 23:44:59 +02003182 .supports_autosuspend = 1,
Sarah Sharpe1f12eb2012-04-23 10:08:51 -07003183 .disable_hub_initiated_lpm = 1,
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003184};
3185
Greg Kroah-Hartman93f15082011-11-18 09:47:34 -08003186module_usb_driver(btusb_driver);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003187
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02003188module_param(disable_scofix, bool, 0644);
3189MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
3190
3191module_param(force_scofix, bool, 0644);
3192MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
3193
3194module_param(reset, bool, 0644);
3195MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
3196
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003197MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
3198MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
3199MODULE_VERSION(VERSION);
3200MODULE_LICENSE("GPL");