blob: d084856cc350b018ab009fd4b730a77cabff7c1f [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>
Marcel Holtmann5e23b922007-10-20 14:12:34 +020027
28#include <net/bluetooth/bluetooth.h>
29#include <net/bluetooth/hci_core.h>
30
Marcel Holtmanncda0dd72015-01-26 21:33:48 -080031#define VERSION "0.7"
Marcel Holtmanncfeb4142008-08-07 22:26:56 +020032
Rusty Russell90ab5ee2012-01-13 09:32:20 +103033static bool disable_scofix;
34static bool force_scofix;
Marcel Holtmann7a9d4022008-11-30 12:17:26 +010035
Rusty Russell90ab5ee2012-01-13 09:32:20 +103036static bool reset = 1;
Marcel Holtmanncfeb4142008-08-07 22:26:56 +020037
38static struct usb_driver btusb_driver;
39
40#define BTUSB_IGNORE 0x01
Marcel Holtmann7a9d4022008-11-30 12:17:26 +010041#define BTUSB_DIGIANSWER 0x02
42#define BTUSB_CSR 0x04
43#define BTUSB_SNIFFER 0x08
44#define BTUSB_BCM92035 0x10
45#define BTUSB_BROKEN_ISOC 0x20
46#define BTUSB_WRONG_SCO_MTU 0x40
Steven.Li2d25f8b2011-07-01 14:02:36 +080047#define BTUSB_ATH3012 0x80
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -070048#define BTUSB_INTEL 0x100
Marcel Holtmann40df7832014-07-06 13:29:58 +020049#define BTUSB_INTEL_BOOT 0x200
50#define BTUSB_BCM_PATCHRAM 0x400
Amitkumar Karwarae8df492014-07-18 14:47:06 -070051#define BTUSB_MARVELL 0x800
Marcel Holtmann4fcef8e2015-01-01 17:34:37 -080052#define BTUSB_SWAVE 0x1000
Marcel Holtmanncda0dd72015-01-26 21:33:48 -080053#define BTUSB_INTEL_NEW 0x2000
Marcel Holtmann893ba542015-01-28 20:27:34 -080054#define BTUSB_AMP 0x4000
Marcel Holtmann5e23b922007-10-20 14:12:34 +020055
Marcel Holtmann54265202013-10-11 07:46:18 -070056static const struct usb_device_id btusb_table[] = {
Marcel Holtmann5e23b922007-10-20 14:12:34 +020057 /* Generic Bluetooth USB device */
58 { USB_DEVICE_INFO(0xe0, 0x01, 0x01) },
59
Marcel Holtmann893ba542015-01-28 20:27:34 -080060 /* Generic Bluetooth AMP device */
61 { USB_DEVICE_INFO(0xe0, 0x01, 0x04), .driver_info = BTUSB_AMP },
62
Henrik Rydberg1fa65352012-08-25 19:28:06 +020063 /* Apple-specific (Broadcom) devices */
64 { USB_VENDOR_AND_INTERFACE_INFO(0x05ac, 0xff, 0x01, 0x01) },
65
Cho, Yu-Chen178c0592013-06-04 21:40:26 +080066 /* MediaTek MT76x0E */
67 { USB_DEVICE(0x0e8d, 0x763f) },
68
Oliver Neukumc510eae2011-09-21 11:41:45 +020069 /* Broadcom SoftSailing reporting vendor specific */
Don Zickus2e8b50632012-03-28 16:41:11 -040070 { USB_DEVICE(0x0a5c, 0x21e1) },
Oliver Neukumc510eae2011-09-21 11:41:45 +020071
Nobuhiro Iwamatsu3cd01972010-08-20 16:24:07 +090072 /* Apple MacBookPro 7,1 */
73 { USB_DEVICE(0x05ac, 0x8213) },
74
Cyril Lacoux0a79f672010-07-14 10:29:27 +040075 /* Apple iMac11,1 */
76 { USB_DEVICE(0x05ac, 0x8215) },
77
Nobuhiro Iwamatsu9c047152010-08-20 16:24:06 +090078 /* Apple MacBookPro6,2 */
79 { USB_DEVICE(0x05ac, 0x8218) },
80
Edgar (gimli) Hucek3e3ede72010-11-04 08:04:33 +010081 /* Apple MacBookAir3,1, MacBookAir3,2 */
82 { USB_DEVICE(0x05ac, 0x821b) },
83
Pieter-Augustijn Van Malleghema63b7232011-09-07 02:28:10 -040084 /* Apple MacBookAir4,1 */
85 { USB_DEVICE(0x05ac, 0x821f) },
86
Marc-Antoine Perennou88d377b2011-03-24 14:51:21 -030087 /* Apple MacBookPro8,2 */
88 { USB_DEVICE(0x05ac, 0x821a) },
89
Jurgen Kramerf78b68262011-09-04 18:01:42 +020090 /* Apple MacMini5,1 */
91 { USB_DEVICE(0x05ac, 0x8281) },
92
Marcel Holtmanncfeb4142008-08-07 22:26:56 +020093 /* AVM BlueFRITZ! USB v2.0 */
Marcel Holtmann4fcef8e2015-01-01 17:34:37 -080094 { USB_DEVICE(0x057c, 0x3800), .driver_info = BTUSB_SWAVE },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +020095
96 /* Bluetooth Ultraport Module from IBM */
97 { USB_DEVICE(0x04bf, 0x030a) },
98
99 /* ALPS Modules with non-standard id */
100 { USB_DEVICE(0x044e, 0x3001) },
101 { USB_DEVICE(0x044e, 0x3002) },
102
103 /* Ericsson with non-standard id */
104 { USB_DEVICE(0x0bdb, 0x1002) },
105
106 /* Canyon CN-BTU1 with HID interfaces */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100107 { USB_DEVICE(0x0c10, 0x0000) },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200108
Wen-chien Jesse Sungd13431c2011-11-08 14:30:22 +0800109 /* Broadcom BCM20702A0 */
Andy Shevchenko0b880062014-02-18 18:26:19 +0200110 { USB_DEVICE(0x0489, 0xe042) },
111 { USB_DEVICE(0x04ca, 0x2003) },
Jeff Cook1ee3ff62012-11-09 16:39:48 -0700112 { USB_DEVICE(0x0b05, 0x17b5) },
Raphael Kubo da Costa38a172b2013-09-02 14:57:51 +0300113 { USB_DEVICE(0x0b05, 0x17cb) },
Wen-chien Jesse Sungd13431c2011-11-08 14:30:22 +0800114 { USB_DEVICE(0x413c, 0x8197) },
Fabio Ka86c02e2014-11-18 00:46:28 -0200115 { USB_DEVICE(0x13d3, 0x3404),
116 .driver_info = BTUSB_BCM_PATCHRAM },
Wen-chien Jesse Sungd13431c2011-11-08 14:30:22 +0800117
Marcel Holtmannd049f4e2015-01-26 20:35:32 -0800118 /* Broadcom BCM20702B0 (Dynex/Insignia) */
119 { USB_DEVICE(0x19ff, 0x0239), .driver_info = BTUSB_BCM_PATCHRAM },
120
Steven Harms98514032012-04-13 14:45:55 -0400121 /* Foxconn - Hon Hai */
Heinrich Siebmanns6029ddc2014-12-03 19:32:22 +0100122 { USB_VENDOR_AND_INTERFACE_INFO(0x0489, 0xff, 0x01, 0x01),
123 .driver_info = BTUSB_BCM_PATCHRAM },
Steven Harms98514032012-04-13 14:45:55 -0400124
Andy Shevchenko0b880062014-02-18 18:26:19 +0200125 /* Broadcom devices with vendor specific id */
Petri Gynther10d4c672014-05-08 15:50:01 -0700126 { USB_VENDOR_AND_INTERFACE_INFO(0x0a5c, 0xff, 0x01, 0x01),
127 .driver_info = BTUSB_BCM_PATCHRAM },
Gustavo Padovan92c385f2012-08-06 15:36:49 -0300128
Marcel Holtmannc2aef6e2014-07-21 14:02:33 +0200129 /* ASUSTek Computer - Broadcom based */
Rick Dunn9a5abda2015-01-17 05:29:12 +0100130 { USB_VENDOR_AND_INTERFACE_INFO(0x0b05, 0xff, 0x01, 0x01),
131 .driver_info = BTUSB_BCM_PATCHRAM },
Marcel Holtmannc2aef6e2014-07-21 14:02:33 +0200132
Ken O'Brien5bcecf32013-09-21 19:14:43 +0100133 /* Belkin F8065bf - Broadcom based */
134 { USB_VENDOR_AND_INTERFACE_INFO(0x050d, 0xff, 0x01, 0x01) },
135
Jurgen Kramer9113bfd2014-02-15 12:01:09 +0100136 /* IMC Networks - Broadcom based */
137 { USB_VENDOR_AND_INTERFACE_INFO(0x13d3, 0xff, 0x01, 0x01) },
138
Marcel Holtmann40df7832014-07-06 13:29:58 +0200139 /* Intel Bluetooth USB Bootloader (RAM module) */
Marcel Holtmannd92f2df2014-07-06 14:53:55 +0200140 { USB_DEVICE(0x8087, 0x0a5a),
141 .driver_info = BTUSB_INTEL_BOOT | BTUSB_BROKEN_ISOC },
Marcel Holtmann40df7832014-07-06 13:29:58 +0200142
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200143 { } /* Terminating entry */
144};
145
146MODULE_DEVICE_TABLE(usb, btusb_table);
147
Marcel Holtmann54265202013-10-11 07:46:18 -0700148static const struct usb_device_id blacklist_table[] = {
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200149 /* CSR BlueCore devices */
150 { USB_DEVICE(0x0a12, 0x0001), .driver_info = BTUSB_CSR },
151
152 /* Broadcom BCM2033 without firmware */
153 { USB_DEVICE(0x0a5c, 0x2033), .driver_info = BTUSB_IGNORE },
154
Bala Shanmugambe931122010-11-26 17:35:46 +0530155 /* Atheros 3011 with sflash firmware */
Andy Shevchenko0b880062014-02-18 18:26:19 +0200156 { USB_DEVICE(0x0489, 0xe027), .driver_info = BTUSB_IGNORE },
157 { USB_DEVICE(0x0489, 0xe03d), .driver_info = BTUSB_IGNORE },
158 { USB_DEVICE(0x0930, 0x0215), .driver_info = BTUSB_IGNORE },
Bala Shanmugambe931122010-11-26 17:35:46 +0530159 { USB_DEVICE(0x0cf3, 0x3002), .driver_info = BTUSB_IGNORE },
Marek Vasut6eda5412012-06-08 14:32:50 +0200160 { USB_DEVICE(0x0cf3, 0xe019), .driver_info = BTUSB_IGNORE },
Andy Ross2a7bccc2011-05-09 16:11:16 -0700161 { USB_DEVICE(0x13d3, 0x3304), .driver_info = BTUSB_IGNORE },
Bala Shanmugambe931122010-11-26 17:35:46 +0530162
Cho, Yu-Chen509e7862011-01-26 17:10:59 +0800163 /* Atheros AR9285 Malbec with sflash firmware */
164 { USB_DEVICE(0x03f0, 0x311d), .driver_info = BTUSB_IGNORE },
165
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530166 /* Atheros 3012 with sflash firmware */
Andy Shevchenko0b880062014-02-18 18:26:19 +0200167 { USB_DEVICE(0x0489, 0xe04d), .driver_info = BTUSB_ATH3012 },
168 { USB_DEVICE(0x0489, 0xe04e), .driver_info = BTUSB_ATH3012 },
169 { USB_DEVICE(0x0489, 0xe056), .driver_info = BTUSB_ATH3012 },
170 { USB_DEVICE(0x0489, 0xe057), .driver_info = BTUSB_ATH3012 },
171 { USB_DEVICE(0x0489, 0xe05f), .driver_info = BTUSB_ATH3012 },
Anantha Krishnan4b552bc2014-10-06 16:31:49 +0530172 { USB_DEVICE(0x0489, 0xe078), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200173 { USB_DEVICE(0x04c5, 0x1330), .driver_info = BTUSB_ATH3012 },
174 { USB_DEVICE(0x04ca, 0x3004), .driver_info = BTUSB_ATH3012 },
175 { USB_DEVICE(0x04ca, 0x3005), .driver_info = BTUSB_ATH3012 },
176 { USB_DEVICE(0x04ca, 0x3006), .driver_info = BTUSB_ATH3012 },
Mohammed Habibulla1fb4e092014-04-17 11:37:13 -0700177 { USB_DEVICE(0x04ca, 0x3007), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200178 { USB_DEVICE(0x04ca, 0x3008), .driver_info = BTUSB_ATH3012 },
179 { USB_DEVICE(0x04ca, 0x300b), .driver_info = BTUSB_ATH3012 },
Janne Heikkinen134d3b32014-12-09 07:44:51 +0200180 { USB_DEVICE(0x04ca, 0x3010), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200181 { USB_DEVICE(0x0930, 0x0219), .driver_info = BTUSB_ATH3012 },
182 { USB_DEVICE(0x0930, 0x0220), .driver_info = BTUSB_ATH3012 },
Vincent Zwanenburg89d29752014-08-08 12:33:56 +0100183 { USB_DEVICE(0x0930, 0x0227), .driver_info = BTUSB_ATH3012 },
Andy Shevchenkoa735f9e2014-02-18 18:26:20 +0200184 { USB_DEVICE(0x0b05, 0x17d0), .driver_info = BTUSB_ATH3012 },
Ming Leid66629c2013-03-15 11:00:39 +0800185 { USB_DEVICE(0x0cf3, 0x0036), .driver_info = BTUSB_ATH3012 },
Steven.Li2d25f8b2011-07-01 14:02:36 +0800186 { USB_DEVICE(0x0cf3, 0x3004), .driver_info = BTUSB_ATH3012 },
Sunguk Lee94a32d102013-03-12 04:41:58 +0900187 { USB_DEVICE(0x0cf3, 0x3008), .driver_info = BTUSB_ATH3012 },
Cho, Yu-Chen07c0ea82012-03-14 22:01:21 +0200188 { USB_DEVICE(0x0cf3, 0x311d), .driver_info = BTUSB_ATH3012 },
Oliver Neukumb1312372014-01-16 15:37:11 +0100189 { USB_DEVICE(0x0cf3, 0x311e), .driver_info = BTUSB_ATH3012 },
Oliver Neukum1e56f1e2014-01-16 16:02:58 +0100190 { USB_DEVICE(0x0cf3, 0x311f), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200191 { USB_DEVICE(0x0cf3, 0x3121), .driver_info = BTUSB_ATH3012 },
Ming Leiebaf5792013-03-18 23:45:11 +0800192 { USB_DEVICE(0x0cf3, 0x817a), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200193 { USB_DEVICE(0x0cf3, 0xe003), .driver_info = BTUSB_ATH3012 },
AceLan Kaoac713112012-04-19 14:53:45 +0800194 { USB_DEVICE(0x0cf3, 0xe004), .driver_info = BTUSB_ATH3012 },
Peng Chen0a3658c2013-08-30 17:41:40 +0800195 { USB_DEVICE(0x0cf3, 0xe005), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200196 { USB_DEVICE(0x13d3, 0x3362), .driver_info = BTUSB_ATH3012 },
197 { USB_DEVICE(0x13d3, 0x3375), .driver_info = BTUSB_ATH3012 },
AceLan Kaoeed307e2012-12-11 11:41:20 +0800198 { USB_DEVICE(0x13d3, 0x3393), .driver_info = BTUSB_ATH3012 },
Sujith Manoharan5b77a1f2013-07-15 09:29:03 +0530199 { USB_DEVICE(0x13d3, 0x3402), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin3bb30a7c2014-11-25 20:19:52 +0300200 { USB_DEVICE(0x13d3, 0x3408), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin033efa92015-01-18 00:16:51 +0300201 { USB_DEVICE(0x13d3, 0x3423), .driver_info = BTUSB_ATH3012 },
Anantha Krishnanfa2f1392014-07-08 19:25:08 +0530202 { USB_DEVICE(0x13d3, 0x3432), .driver_info = BTUSB_ATH3012 },
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530203
Cho, Yu-Chene9036e32011-02-15 10:20:07 +0800204 /* Atheros AR5BBU12 with sflash firmware */
205 { USB_DEVICE(0x0489, 0xe02c), .driver_info = BTUSB_IGNORE },
206
Michael Gruetzner85d59722012-05-02 22:33:40 +0200207 /* Atheros AR5BBU12 with sflash firmware */
Yevgeniy Melnichukbc21fde2012-08-07 19:48:10 +0530208 { USB_DEVICE(0x0489, 0xe036), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200209 { USB_DEVICE(0x0489, 0xe03c), .driver_info = BTUSB_ATH3012 },
Michael Gruetzner85d59722012-05-02 22:33:40 +0200210
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200211 /* Broadcom BCM2035 */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100212 { USB_DEVICE(0x0a5c, 0x2009), .driver_info = BTUSB_BCM92035 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200213 { USB_DEVICE(0x0a5c, 0x200a), .driver_info = BTUSB_WRONG_SCO_MTU },
214 { USB_DEVICE(0x0a5c, 0x2035), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200215
216 /* Broadcom BCM2045 */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100217 { USB_DEVICE(0x0a5c, 0x2039), .driver_info = BTUSB_WRONG_SCO_MTU },
218 { USB_DEVICE(0x0a5c, 0x2101), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmannbdbef3d2008-09-23 00:16:35 +0200219
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200220 /* IBM/Lenovo ThinkPad with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100221 { USB_DEVICE(0x0a5c, 0x201e), .driver_info = BTUSB_WRONG_SCO_MTU },
222 { USB_DEVICE(0x0a5c, 0x2110), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200223
224 /* HP laptop with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100225 { USB_DEVICE(0x03f0, 0x171d), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200226
227 /* Dell laptop with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100228 { USB_DEVICE(0x413c, 0x8126), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200229
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100230 /* Dell Wireless 370 and 410 devices */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100231 { USB_DEVICE(0x413c, 0x8152), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100232 { USB_DEVICE(0x413c, 0x8156), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200233
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100234 /* Belkin F8T012 and F8T013 devices */
235 { USB_DEVICE(0x050d, 0x0012), .driver_info = BTUSB_WRONG_SCO_MTU },
236 { USB_DEVICE(0x050d, 0x0013), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200237
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100238 /* Asus WL-BTD202 device */
239 { USB_DEVICE(0x0b05, 0x1715), .driver_info = BTUSB_WRONG_SCO_MTU },
240
241 /* Kensington Bluetooth USB adapter */
242 { USB_DEVICE(0x047d, 0x105e), .driver_info = BTUSB_WRONG_SCO_MTU },
243
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200244 /* RTX Telecom based adapters with buggy SCO support */
245 { USB_DEVICE(0x0400, 0x0807), .driver_info = BTUSB_BROKEN_ISOC },
246 { USB_DEVICE(0x0400, 0x080a), .driver_info = BTUSB_BROKEN_ISOC },
247
248 /* CONWISE Technology based adapters with buggy SCO support */
249 { USB_DEVICE(0x0e5e, 0x6622), .driver_info = BTUSB_BROKEN_ISOC },
250
Marcel Holtmann4fcef8e2015-01-01 17:34:37 -0800251 /* Roper Class 1 Bluetooth Dongle (Silicon Wave based) */
252 { USB_DEVICE(0x1300, 0x0001), .driver_info = BTUSB_SWAVE },
253
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200254 /* Digianswer devices */
255 { USB_DEVICE(0x08fd, 0x0001), .driver_info = BTUSB_DIGIANSWER },
256 { USB_DEVICE(0x08fd, 0x0002), .driver_info = BTUSB_IGNORE },
257
258 /* CSR BlueCore Bluetooth Sniffer */
Marcel Holtmann4f64fa82014-07-07 00:12:04 +0200259 { USB_DEVICE(0x0a12, 0x0002),
260 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200261
262 /* Frontline ComProbe Bluetooth Sniffer */
Marcel Holtmann4f64fa82014-07-07 00:12:04 +0200263 { USB_DEVICE(0x16d3, 0x0002),
264 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200265
Marcel Holtmanncb1ee892015-01-28 19:41:42 -0800266 /* Marvell Bluetooth devices */
267 { USB_DEVICE(0x1286, 0x2044), .driver_info = BTUSB_MARVELL },
268 { USB_DEVICE(0x1286, 0x2046), .driver_info = BTUSB_MARVELL },
269
Marcel Holtmannd0ac9eb2015-01-28 19:41:43 -0800270 /* Intel Bluetooth devices */
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -0700271 { USB_DEVICE(0x8087, 0x07dc), .driver_info = BTUSB_INTEL },
Tedd Ho-Jeong Anef4e5e42013-11-12 13:10:58 -0800272 { USB_DEVICE(0x8087, 0x0a2a), .driver_info = BTUSB_INTEL },
Marcel Holtmanncda0dd72015-01-26 21:33:48 -0800273 { USB_DEVICE(0x8087, 0x0a2b), .driver_info = BTUSB_INTEL_NEW },
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -0700274
Marcel Holtmannd0ac9eb2015-01-28 19:41:43 -0800275 /* Other Intel Bluetooth devices */
276 { USB_VENDOR_AND_INTERFACE_INFO(0x8087, 0xe0, 0x01, 0x01),
277 .driver_info = BTUSB_IGNORE },
Amitkumar Karwarae8df492014-07-18 14:47:06 -0700278
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200279 { } /* Terminating entry */
280};
281
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200282#define BTUSB_MAX_ISOC_FRAMES 10
283
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200284#define BTUSB_INTR_RUNNING 0
285#define BTUSB_BULK_RUNNING 1
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200286#define BTUSB_ISOC_RUNNING 2
Oliver Neukum7bee5492009-08-24 23:44:59 +0200287#define BTUSB_SUSPENDING 3
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -0300288#define BTUSB_DID_ISO_RESUME 4
Marcel Holtmanncda0dd72015-01-26 21:33:48 -0800289#define BTUSB_BOOTLOADER 5
290#define BTUSB_DOWNLOADING 6
Marcel Holtmannce6bb922015-01-28 01:58:40 -0800291#define BTUSB_FIRMWARE_LOADED 7
Marcel Holtmanncda0dd72015-01-26 21:33:48 -0800292#define BTUSB_FIRMWARE_FAILED 8
Marcel Holtmannce6bb922015-01-28 01:58:40 -0800293#define BTUSB_BOOTING 9
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200294
295struct btusb_data {
296 struct hci_dev *hdev;
297 struct usb_device *udev;
Marcel Holtmann5fbcd262008-09-23 00:16:36 +0200298 struct usb_interface *intf;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200299 struct usb_interface *isoc;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200300
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200301 unsigned long flags;
302
303 struct work_struct work;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200304 struct work_struct waker;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200305
Marcel Holtmann803b5832014-09-16 08:00:29 +0200306 struct usb_anchor deferred;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200307 struct usb_anchor tx_anchor;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200308 int tx_in_flight;
309 spinlock_t txlock;
310
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200311 struct usb_anchor intr_anchor;
312 struct usb_anchor bulk_anchor;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200313 struct usb_anchor isoc_anchor;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200314 spinlock_t rxlock;
315
316 struct sk_buff *evt_skb;
317 struct sk_buff *acl_skb;
318 struct sk_buff *sco_skb;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200319
320 struct usb_endpoint_descriptor *intr_ep;
321 struct usb_endpoint_descriptor *bulk_tx_ep;
322 struct usb_endpoint_descriptor *bulk_rx_ep;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200323 struct usb_endpoint_descriptor *isoc_tx_ep;
324 struct usb_endpoint_descriptor *isoc_rx_ep;
325
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100326 __u8 cmdreq_type;
Marcel Holtmann893ba542015-01-28 20:27:34 -0800327 __u8 cmdreq;
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100328
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100329 unsigned int sco_num;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200330 int isoc_altsetting;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +0100331 int suspend_count;
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +0100332
Marcel Holtmann97307f52015-01-12 13:51:10 -0800333 int (*recv_event)(struct hci_dev *hdev, struct sk_buff *skb);
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +0100334 int (*recv_bulk)(struct btusb_data *data, void *buffer, int count);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200335};
336
Johan Hedberga087a982015-01-30 10:58:54 +0200337static int btusb_wait_on_bit_timeout(void *word, int bit, unsigned long timeout,
338 unsigned mode)
339{
340 might_sleep();
341 if (!test_bit(bit, word))
342 return 0;
343 return out_of_line_wait_on_bit_timeout(word, bit, bit_wait_timeout,
344 mode, timeout);
345}
346
Marcel Holtmann803b5832014-09-16 08:00:29 +0200347static inline void btusb_free_frags(struct btusb_data *data)
348{
349 unsigned long flags;
350
351 spin_lock_irqsave(&data->rxlock, flags);
352
353 kfree_skb(data->evt_skb);
354 data->evt_skb = NULL;
355
356 kfree_skb(data->acl_skb);
357 data->acl_skb = NULL;
358
359 kfree_skb(data->sco_skb);
360 data->sco_skb = NULL;
361
362 spin_unlock_irqrestore(&data->rxlock, flags);
363}
364
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200365static int btusb_recv_intr(struct btusb_data *data, void *buffer, int count)
366{
Marcel Holtmann803b5832014-09-16 08:00:29 +0200367 struct sk_buff *skb;
368 int err = 0;
369
370 spin_lock(&data->rxlock);
371 skb = data->evt_skb;
372
373 while (count) {
374 int len;
375
376 if (!skb) {
377 skb = bt_skb_alloc(HCI_MAX_EVENT_SIZE, GFP_ATOMIC);
378 if (!skb) {
379 err = -ENOMEM;
380 break;
381 }
382
383 bt_cb(skb)->pkt_type = HCI_EVENT_PKT;
384 bt_cb(skb)->expect = HCI_EVENT_HDR_SIZE;
385 }
386
387 len = min_t(uint, bt_cb(skb)->expect, count);
388 memcpy(skb_put(skb, len), buffer, len);
389
390 count -= len;
391 buffer += len;
392 bt_cb(skb)->expect -= len;
393
394 if (skb->len == HCI_EVENT_HDR_SIZE) {
395 /* Complete event header */
396 bt_cb(skb)->expect = hci_event_hdr(skb)->plen;
397
398 if (skb_tailroom(skb) < bt_cb(skb)->expect) {
399 kfree_skb(skb);
400 skb = NULL;
401
402 err = -EILSEQ;
403 break;
404 }
405 }
406
407 if (bt_cb(skb)->expect == 0) {
408 /* Complete frame */
Marcel Holtmann97307f52015-01-12 13:51:10 -0800409 data->recv_event(data->hdev, skb);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200410 skb = NULL;
411 }
412 }
413
414 data->evt_skb = skb;
415 spin_unlock(&data->rxlock);
416
417 return err;
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200418}
419
420static int btusb_recv_bulk(struct btusb_data *data, void *buffer, int count)
421{
Marcel Holtmann803b5832014-09-16 08:00:29 +0200422 struct sk_buff *skb;
423 int err = 0;
424
425 spin_lock(&data->rxlock);
426 skb = data->acl_skb;
427
428 while (count) {
429 int len;
430
431 if (!skb) {
432 skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
433 if (!skb) {
434 err = -ENOMEM;
435 break;
436 }
437
438 bt_cb(skb)->pkt_type = HCI_ACLDATA_PKT;
439 bt_cb(skb)->expect = HCI_ACL_HDR_SIZE;
440 }
441
442 len = min_t(uint, bt_cb(skb)->expect, count);
443 memcpy(skb_put(skb, len), buffer, len);
444
445 count -= len;
446 buffer += len;
447 bt_cb(skb)->expect -= len;
448
449 if (skb->len == HCI_ACL_HDR_SIZE) {
450 __le16 dlen = hci_acl_hdr(skb)->dlen;
451
452 /* Complete ACL header */
453 bt_cb(skb)->expect = __le16_to_cpu(dlen);
454
455 if (skb_tailroom(skb) < bt_cb(skb)->expect) {
456 kfree_skb(skb);
457 skb = NULL;
458
459 err = -EILSEQ;
460 break;
461 }
462 }
463
464 if (bt_cb(skb)->expect == 0) {
465 /* Complete frame */
466 hci_recv_frame(data->hdev, skb);
467 skb = NULL;
468 }
469 }
470
471 data->acl_skb = skb;
472 spin_unlock(&data->rxlock);
473
474 return err;
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200475}
476
477static int btusb_recv_isoc(struct btusb_data *data, void *buffer, int count)
478{
Marcel Holtmann803b5832014-09-16 08:00:29 +0200479 struct sk_buff *skb;
480 int err = 0;
481
482 spin_lock(&data->rxlock);
483 skb = data->sco_skb;
484
485 while (count) {
486 int len;
487
488 if (!skb) {
489 skb = bt_skb_alloc(HCI_MAX_SCO_SIZE, GFP_ATOMIC);
490 if (!skb) {
491 err = -ENOMEM;
492 break;
493 }
494
495 bt_cb(skb)->pkt_type = HCI_SCODATA_PKT;
496 bt_cb(skb)->expect = HCI_SCO_HDR_SIZE;
497 }
498
499 len = min_t(uint, bt_cb(skb)->expect, count);
500 memcpy(skb_put(skb, len), buffer, len);
501
502 count -= len;
503 buffer += len;
504 bt_cb(skb)->expect -= len;
505
506 if (skb->len == HCI_SCO_HDR_SIZE) {
507 /* Complete SCO header */
508 bt_cb(skb)->expect = hci_sco_hdr(skb)->dlen;
509
510 if (skb_tailroom(skb) < bt_cb(skb)->expect) {
511 kfree_skb(skb);
512 skb = NULL;
513
514 err = -EILSEQ;
515 break;
516 }
517 }
518
519 if (bt_cb(skb)->expect == 0) {
520 /* Complete frame */
521 hci_recv_frame(data->hdev, skb);
522 skb = NULL;
523 }
524 }
525
526 data->sco_skb = skb;
527 spin_unlock(&data->rxlock);
528
529 return err;
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200530}
531
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200532static void btusb_intr_complete(struct urb *urb)
533{
534 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +0100535 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200536 int err;
537
Marcel Holtmann89e75332014-09-16 04:44:50 +0200538 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
539 urb->actual_length);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200540
541 if (!test_bit(HCI_RUNNING, &hdev->flags))
542 return;
543
544 if (urb->status == 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200545 hdev->stat.byte_rx += urb->actual_length;
546
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200547 if (btusb_recv_intr(data, urb->transfer_buffer,
548 urb->actual_length) < 0) {
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200549 BT_ERR("%s corrupted event packet", hdev->name);
550 hdev->stat.err_rx++;
551 }
Champion Chen85560c42014-09-06 14:06:08 -0500552 } else if (urb->status == -ENOENT) {
553 /* Avoid suspend failed when usb_kill_urb */
554 return;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200555 }
556
557 if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
558 return;
559
Oliver Neukum7bee5492009-08-24 23:44:59 +0200560 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200561 usb_anchor_urb(urb, &data->intr_anchor);
562
563 err = usb_submit_urb(urb, GFP_ATOMIC);
564 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200565 /* -EPERM: urb is being killed;
566 * -ENODEV: device got disconnected */
567 if (err != -EPERM && err != -ENODEV)
Stefan Seyfried61faddf2010-11-30 21:49:08 +0100568 BT_ERR("%s urb %p failed to resubmit (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200569 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200570 usb_unanchor_urb(urb);
571 }
572}
573
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100574static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200575{
David Herrmann155961e2012-02-09 21:58:32 +0100576 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200577 struct urb *urb;
578 unsigned char *buf;
579 unsigned int pipe;
580 int err, size;
581
582 BT_DBG("%s", hdev->name);
583
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200584 if (!data->intr_ep)
585 return -ENODEV;
586
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100587 urb = usb_alloc_urb(0, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200588 if (!urb)
589 return -ENOMEM;
590
591 size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
592
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100593 buf = kmalloc(size, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200594 if (!buf) {
595 usb_free_urb(urb);
596 return -ENOMEM;
597 }
598
599 pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
600
601 usb_fill_int_urb(urb, data->udev, pipe, buf, size,
Marcel Holtmann89e75332014-09-16 04:44:50 +0200602 btusb_intr_complete, hdev, data->intr_ep->bInterval);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200603
604 urb->transfer_flags |= URB_FREE_BUFFER;
605
606 usb_anchor_urb(urb, &data->intr_anchor);
607
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100608 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200609 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +0200610 if (err != -EPERM && err != -ENODEV)
611 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200612 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200613 usb_unanchor_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200614 }
615
616 usb_free_urb(urb);
617
618 return err;
619}
620
621static void btusb_bulk_complete(struct urb *urb)
622{
623 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +0100624 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200625 int err;
626
Marcel Holtmann89e75332014-09-16 04:44:50 +0200627 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
628 urb->actual_length);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200629
630 if (!test_bit(HCI_RUNNING, &hdev->flags))
631 return;
632
633 if (urb->status == 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200634 hdev->stat.byte_rx += urb->actual_length;
635
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +0100636 if (data->recv_bulk(data, urb->transfer_buffer,
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200637 urb->actual_length) < 0) {
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200638 BT_ERR("%s corrupted ACL packet", hdev->name);
639 hdev->stat.err_rx++;
640 }
Champion Chen85560c42014-09-06 14:06:08 -0500641 } else if (urb->status == -ENOENT) {
642 /* Avoid suspend failed when usb_kill_urb */
643 return;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200644 }
645
646 if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
647 return;
648
649 usb_anchor_urb(urb, &data->bulk_anchor);
Oliver Neukum652fd782009-12-16 19:23:43 +0100650 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200651
652 err = usb_submit_urb(urb, GFP_ATOMIC);
653 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200654 /* -EPERM: urb is being killed;
655 * -ENODEV: device got disconnected */
656 if (err != -EPERM && err != -ENODEV)
Stefan Seyfried61faddf2010-11-30 21:49:08 +0100657 BT_ERR("%s urb %p failed to resubmit (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200658 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200659 usb_unanchor_urb(urb);
660 }
661}
662
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100663static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200664{
David Herrmann155961e2012-02-09 21:58:32 +0100665 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200666 struct urb *urb;
667 unsigned char *buf;
668 unsigned int pipe;
Vikram Kandukuri290ba202009-07-02 14:31:59 +0530669 int err, size = HCI_MAX_FRAME_SIZE;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200670
671 BT_DBG("%s", hdev->name);
672
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200673 if (!data->bulk_rx_ep)
674 return -ENODEV;
675
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100676 urb = usb_alloc_urb(0, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200677 if (!urb)
678 return -ENOMEM;
679
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100680 buf = kmalloc(size, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200681 if (!buf) {
682 usb_free_urb(urb);
683 return -ENOMEM;
684 }
685
686 pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
687
Marcel Holtmann89e75332014-09-16 04:44:50 +0200688 usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
689 btusb_bulk_complete, hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200690
691 urb->transfer_flags |= URB_FREE_BUFFER;
692
Oliver Neukum7bee5492009-08-24 23:44:59 +0200693 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200694 usb_anchor_urb(urb, &data->bulk_anchor);
695
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100696 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200697 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +0200698 if (err != -EPERM && err != -ENODEV)
699 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200700 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200701 usb_unanchor_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200702 }
703
704 usb_free_urb(urb);
705
706 return err;
707}
708
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200709static void btusb_isoc_complete(struct urb *urb)
710{
711 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +0100712 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200713 int i, err;
714
Marcel Holtmann89e75332014-09-16 04:44:50 +0200715 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
716 urb->actual_length);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200717
718 if (!test_bit(HCI_RUNNING, &hdev->flags))
719 return;
720
721 if (urb->status == 0) {
722 for (i = 0; i < urb->number_of_packets; i++) {
723 unsigned int offset = urb->iso_frame_desc[i].offset;
724 unsigned int length = urb->iso_frame_desc[i].actual_length;
725
726 if (urb->iso_frame_desc[i].status)
727 continue;
728
729 hdev->stat.byte_rx += length;
730
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200731 if (btusb_recv_isoc(data, urb->transfer_buffer + offset,
732 length) < 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200733 BT_ERR("%s corrupted SCO packet", hdev->name);
734 hdev->stat.err_rx++;
735 }
736 }
Champion Chen85560c42014-09-06 14:06:08 -0500737 } else if (urb->status == -ENOENT) {
738 /* Avoid suspend failed when usb_kill_urb */
739 return;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200740 }
741
742 if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags))
743 return;
744
745 usb_anchor_urb(urb, &data->isoc_anchor);
746
747 err = usb_submit_urb(urb, GFP_ATOMIC);
748 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200749 /* -EPERM: urb is being killed;
750 * -ENODEV: device got disconnected */
751 if (err != -EPERM && err != -ENODEV)
Stefan Seyfried61faddf2010-11-30 21:49:08 +0100752 BT_ERR("%s urb %p failed to resubmit (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200753 hdev->name, urb, -err);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200754 usb_unanchor_urb(urb);
755 }
756}
757
Jesper Juhl42b16b32011-01-17 00:09:38 +0100758static inline void __fill_isoc_descriptor(struct urb *urb, int len, int mtu)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200759{
760 int i, offset = 0;
761
762 BT_DBG("len %d mtu %d", len, mtu);
763
764 for (i = 0; i < BTUSB_MAX_ISOC_FRAMES && len >= mtu;
765 i++, offset += mtu, len -= mtu) {
766 urb->iso_frame_desc[i].offset = offset;
767 urb->iso_frame_desc[i].length = mtu;
768 }
769
770 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
771 urb->iso_frame_desc[i].offset = offset;
772 urb->iso_frame_desc[i].length = len;
773 i++;
774 }
775
776 urb->number_of_packets = i;
777}
778
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100779static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200780{
David Herrmann155961e2012-02-09 21:58:32 +0100781 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200782 struct urb *urb;
783 unsigned char *buf;
784 unsigned int pipe;
785 int err, size;
786
787 BT_DBG("%s", hdev->name);
788
789 if (!data->isoc_rx_ep)
790 return -ENODEV;
791
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100792 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200793 if (!urb)
794 return -ENOMEM;
795
796 size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) *
797 BTUSB_MAX_ISOC_FRAMES;
798
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100799 buf = kmalloc(size, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200800 if (!buf) {
801 usb_free_urb(urb);
802 return -ENOMEM;
803 }
804
805 pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress);
806
Bing Zhaofa0fb932011-12-20 18:19:00 -0800807 usb_fill_int_urb(urb, data->udev, pipe, buf, size, btusb_isoc_complete,
Marcel Holtmann89e75332014-09-16 04:44:50 +0200808 hdev, data->isoc_rx_ep->bInterval);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200809
Marcel Holtmann89e75332014-09-16 04:44:50 +0200810 urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200811
812 __fill_isoc_descriptor(urb, size,
Marcel Holtmann89e75332014-09-16 04:44:50 +0200813 le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize));
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200814
815 usb_anchor_urb(urb, &data->isoc_anchor);
816
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100817 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200818 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +0200819 if (err != -EPERM && err != -ENODEV)
820 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200821 hdev->name, urb, -err);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200822 usb_unanchor_urb(urb);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200823 }
824
825 usb_free_urb(urb);
826
827 return err;
828}
829
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200830static void btusb_tx_complete(struct urb *urb)
831{
832 struct sk_buff *skb = urb->context;
Marcel Holtmann89e75332014-09-16 04:44:50 +0200833 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
David Herrmann155961e2012-02-09 21:58:32 +0100834 struct btusb_data *data = hci_get_drvdata(hdev);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200835
Marcel Holtmann89e75332014-09-16 04:44:50 +0200836 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
837 urb->actual_length);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200838
839 if (!test_bit(HCI_RUNNING, &hdev->flags))
840 goto done;
841
842 if (!urb->status)
843 hdev->stat.byte_tx += urb->transfer_buffer_length;
844 else
845 hdev->stat.err_tx++;
846
847done:
848 spin_lock(&data->txlock);
849 data->tx_in_flight--;
850 spin_unlock(&data->txlock);
851
852 kfree(urb->setup_packet);
853
854 kfree_skb(skb);
855}
856
857static void btusb_isoc_tx_complete(struct urb *urb)
858{
859 struct sk_buff *skb = urb->context;
Marcel Holtmann89e75332014-09-16 04:44:50 +0200860 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200861
Marcel Holtmann89e75332014-09-16 04:44:50 +0200862 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
863 urb->actual_length);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200864
865 if (!test_bit(HCI_RUNNING, &hdev->flags))
866 goto done;
867
868 if (!urb->status)
869 hdev->stat.byte_tx += urb->transfer_buffer_length;
870 else
871 hdev->stat.err_tx++;
872
873done:
874 kfree(urb->setup_packet);
875
876 kfree_skb(skb);
877}
878
879static int btusb_open(struct hci_dev *hdev)
880{
David Herrmann155961e2012-02-09 21:58:32 +0100881 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200882 int err;
883
884 BT_DBG("%s", hdev->name);
885
Oliver Neukum7bee5492009-08-24 23:44:59 +0200886 err = usb_autopm_get_interface(data->intf);
887 if (err < 0)
888 return err;
889
890 data->intf->needs_remote_wakeup = 1;
891
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200892 if (test_and_set_bit(HCI_RUNNING, &hdev->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +0200893 goto done;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200894
895 if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +0200896 goto done;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200897
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100898 err = btusb_submit_intr_urb(hdev, GFP_KERNEL);
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100899 if (err < 0)
900 goto failed;
901
902 err = btusb_submit_bulk_urb(hdev, GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200903 if (err < 0) {
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100904 usb_kill_anchored_urbs(&data->intr_anchor);
905 goto failed;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200906 }
907
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100908 set_bit(BTUSB_BULK_RUNNING, &data->flags);
909 btusb_submit_bulk_urb(hdev, GFP_KERNEL);
910
Oliver Neukum7bee5492009-08-24 23:44:59 +0200911done:
912 usb_autopm_put_interface(data->intf);
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100913 return 0;
914
915failed:
916 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
917 clear_bit(HCI_RUNNING, &hdev->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200918 usb_autopm_put_interface(data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200919 return err;
920}
921
Oliver Neukum7bee5492009-08-24 23:44:59 +0200922static void btusb_stop_traffic(struct btusb_data *data)
923{
924 usb_kill_anchored_urbs(&data->intr_anchor);
925 usb_kill_anchored_urbs(&data->bulk_anchor);
926 usb_kill_anchored_urbs(&data->isoc_anchor);
927}
928
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200929static int btusb_close(struct hci_dev *hdev)
930{
David Herrmann155961e2012-02-09 21:58:32 +0100931 struct btusb_data *data = hci_get_drvdata(hdev);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200932 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200933
934 BT_DBG("%s", hdev->name);
935
936 if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
937 return 0;
938
Marcel Holtmanne8c3c3d2008-09-23 00:16:36 +0200939 cancel_work_sync(&data->work);
Linus Torvalds404291a2009-11-11 13:32:29 -0800940 cancel_work_sync(&data->waker);
Marcel Holtmanne8c3c3d2008-09-23 00:16:36 +0200941
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200942 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200943 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200944 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200945
946 btusb_stop_traffic(data);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200947 btusb_free_frags(data);
948
Oliver Neukum7bee5492009-08-24 23:44:59 +0200949 err = usb_autopm_get_interface(data->intf);
950 if (err < 0)
Oliver Neukum7b8e2c12009-11-13 14:26:23 +0100951 goto failed;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200952
953 data->intf->needs_remote_wakeup = 0;
954 usb_autopm_put_interface(data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200955
Oliver Neukum7b8e2c12009-11-13 14:26:23 +0100956failed:
957 usb_scuttle_anchored_urbs(&data->deferred);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200958 return 0;
959}
960
961static int btusb_flush(struct hci_dev *hdev)
962{
David Herrmann155961e2012-02-09 21:58:32 +0100963 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200964
965 BT_DBG("%s", hdev->name);
966
967 usb_kill_anchored_urbs(&data->tx_anchor);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200968 btusb_free_frags(data);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200969
970 return 0;
971}
972
Marcel Holtmann047b2ec2014-09-14 09:11:06 +0200973static struct urb *alloc_ctrl_urb(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200974{
David Herrmann155961e2012-02-09 21:58:32 +0100975 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200976 struct usb_ctrlrequest *dr;
977 struct urb *urb;
978 unsigned int pipe;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200979
Marcel Holtmann047b2ec2014-09-14 09:11:06 +0200980 urb = usb_alloc_urb(0, GFP_KERNEL);
981 if (!urb)
982 return ERR_PTR(-ENOMEM);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200983
Marcel Holtmann047b2ec2014-09-14 09:11:06 +0200984 dr = kmalloc(sizeof(*dr), GFP_KERNEL);
985 if (!dr) {
986 usb_free_urb(urb);
987 return ERR_PTR(-ENOMEM);
988 }
989
990 dr->bRequestType = data->cmdreq_type;
Marcel Holtmann893ba542015-01-28 20:27:34 -0800991 dr->bRequest = data->cmdreq;
Marcel Holtmann047b2ec2014-09-14 09:11:06 +0200992 dr->wIndex = 0;
993 dr->wValue = 0;
994 dr->wLength = __cpu_to_le16(skb->len);
995
996 pipe = usb_sndctrlpipe(data->udev, 0x00);
997
Marcel Holtmann89e75332014-09-16 04:44:50 +0200998 usb_fill_control_urb(urb, data->udev, pipe, (void *)dr,
Marcel Holtmann047b2ec2014-09-14 09:11:06 +0200999 skb->data, skb->len, btusb_tx_complete, skb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001000
Marcel Holtmann89e75332014-09-16 04:44:50 +02001001 skb->dev = (void *)hdev;
Marcel Holtmann7bd8f092013-10-11 06:19:18 -07001002
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001003 return urb;
1004}
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001005
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001006static struct urb *alloc_bulk_urb(struct hci_dev *hdev, struct sk_buff *skb)
1007{
1008 struct btusb_data *data = hci_get_drvdata(hdev);
1009 struct urb *urb;
1010 unsigned int pipe;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001011
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001012 if (!data->bulk_tx_ep)
1013 return ERR_PTR(-ENODEV);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001014
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001015 urb = usb_alloc_urb(0, GFP_KERNEL);
1016 if (!urb)
1017 return ERR_PTR(-ENOMEM);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001018
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001019 pipe = usb_sndbulkpipe(data->udev, data->bulk_tx_ep->bEndpointAddress);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001020
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001021 usb_fill_bulk_urb(urb, data->udev, pipe,
1022 skb->data, skb->len, btusb_tx_complete, skb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001023
Marcel Holtmann89e75332014-09-16 04:44:50 +02001024 skb->dev = (void *)hdev;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001025
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001026 return urb;
1027}
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001028
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001029static struct urb *alloc_isoc_urb(struct hci_dev *hdev, struct sk_buff *skb)
1030{
1031 struct btusb_data *data = hci_get_drvdata(hdev);
1032 struct urb *urb;
1033 unsigned int pipe;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001034
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001035 if (!data->isoc_tx_ep)
1036 return ERR_PTR(-ENODEV);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001037
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001038 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_KERNEL);
1039 if (!urb)
1040 return ERR_PTR(-ENOMEM);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001041
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001042 pipe = usb_sndisocpipe(data->udev, data->isoc_tx_ep->bEndpointAddress);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001043
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001044 usb_fill_int_urb(urb, data->udev, pipe,
1045 skb->data, skb->len, btusb_isoc_tx_complete,
1046 skb, data->isoc_tx_ep->bInterval);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001047
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001048 urb->transfer_flags = URB_ISO_ASAP;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001049
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001050 __fill_isoc_descriptor(urb, skb->len,
1051 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001052
Marcel Holtmann89e75332014-09-16 04:44:50 +02001053 skb->dev = (void *)hdev;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001054
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001055 return urb;
1056}
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001057
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001058static int submit_tx_urb(struct hci_dev *hdev, struct urb *urb)
1059{
1060 struct btusb_data *data = hci_get_drvdata(hdev);
1061 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001062
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001063 usb_anchor_urb(urb, &data->tx_anchor);
1064
Johan Hedberge9753ef2014-09-14 08:49:34 +03001065 err = usb_submit_urb(urb, GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001066 if (err < 0) {
Paul Bolle5a9b80e2011-10-09 12:12:16 +02001067 if (err != -EPERM && err != -ENODEV)
1068 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +02001069 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001070 kfree(urb->setup_packet);
1071 usb_unanchor_urb(urb);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001072 } else {
1073 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001074 }
1075
Cong Wang54a8a792011-11-22 09:32:57 +08001076 usb_free_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001077 return err;
1078}
1079
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001080static int submit_or_queue_tx_urb(struct hci_dev *hdev, struct urb *urb)
1081{
1082 struct btusb_data *data = hci_get_drvdata(hdev);
1083 unsigned long flags;
1084 bool suspending;
1085
1086 spin_lock_irqsave(&data->txlock, flags);
1087 suspending = test_bit(BTUSB_SUSPENDING, &data->flags);
1088 if (!suspending)
1089 data->tx_in_flight++;
1090 spin_unlock_irqrestore(&data->txlock, flags);
1091
1092 if (!suspending)
1093 return submit_tx_urb(hdev, urb);
1094
1095 usb_anchor_urb(urb, &data->deferred);
1096 schedule_work(&data->waker);
1097
1098 usb_free_urb(urb);
1099 return 0;
1100}
1101
1102static int btusb_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
1103{
1104 struct urb *urb;
1105
1106 BT_DBG("%s", hdev->name);
1107
1108 if (!test_bit(HCI_RUNNING, &hdev->flags))
1109 return -EBUSY;
1110
1111 switch (bt_cb(skb)->pkt_type) {
1112 case HCI_COMMAND_PKT:
1113 urb = alloc_ctrl_urb(hdev, skb);
1114 if (IS_ERR(urb))
1115 return PTR_ERR(urb);
1116
1117 hdev->stat.cmd_tx++;
1118 return submit_or_queue_tx_urb(hdev, urb);
1119
1120 case HCI_ACLDATA_PKT:
1121 urb = alloc_bulk_urb(hdev, skb);
1122 if (IS_ERR(urb))
1123 return PTR_ERR(urb);
1124
1125 hdev->stat.acl_tx++;
1126 return submit_or_queue_tx_urb(hdev, urb);
1127
1128 case HCI_SCODATA_PKT:
1129 if (hci_conn_num(hdev, SCO_LINK) < 1)
1130 return -ENODEV;
1131
1132 urb = alloc_isoc_urb(hdev, skb);
1133 if (IS_ERR(urb))
1134 return PTR_ERR(urb);
1135
1136 hdev->stat.sco_tx++;
1137 return submit_tx_urb(hdev, urb);
1138 }
1139
1140 return -EILSEQ;
1141}
1142
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001143static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
1144{
David Herrmann155961e2012-02-09 21:58:32 +01001145 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001146
1147 BT_DBG("%s evt %d", hdev->name, evt);
1148
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001149 if (hci_conn_num(hdev, SCO_LINK) != data->sco_num) {
1150 data->sco_num = hci_conn_num(hdev, SCO_LINK);
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001151 schedule_work(&data->work);
Marcel Holtmanna780efa2008-11-30 12:17:12 +01001152 }
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001153}
1154
Jesper Juhl42b16b32011-01-17 00:09:38 +01001155static inline int __set_isoc_interface(struct hci_dev *hdev, int altsetting)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001156{
David Herrmann155961e2012-02-09 21:58:32 +01001157 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001158 struct usb_interface *intf = data->isoc;
1159 struct usb_endpoint_descriptor *ep_desc;
1160 int i, err;
1161
1162 if (!data->isoc)
1163 return -ENODEV;
1164
1165 err = usb_set_interface(data->udev, 1, altsetting);
1166 if (err < 0) {
1167 BT_ERR("%s setting interface failed (%d)", hdev->name, -err);
1168 return err;
1169 }
1170
1171 data->isoc_altsetting = altsetting;
1172
1173 data->isoc_tx_ep = NULL;
1174 data->isoc_rx_ep = NULL;
1175
1176 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
1177 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
1178
1179 if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) {
1180 data->isoc_tx_ep = ep_desc;
1181 continue;
1182 }
1183
1184 if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) {
1185 data->isoc_rx_ep = ep_desc;
1186 continue;
1187 }
1188 }
1189
1190 if (!data->isoc_tx_ep || !data->isoc_rx_ep) {
1191 BT_ERR("%s invalid SCO descriptors", hdev->name);
1192 return -ENODEV;
1193 }
1194
1195 return 0;
1196}
1197
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001198static void btusb_work(struct work_struct *work)
1199{
1200 struct btusb_data *data = container_of(work, struct btusb_data, work);
1201 struct hci_dev *hdev = data->hdev;
Mikel Astizf4001d22012-04-11 08:48:51 +02001202 int new_alts;
Oliver Neukum7bee5492009-08-24 23:44:59 +02001203 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001204
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001205 if (data->sco_num > 0) {
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -03001206 if (!test_bit(BTUSB_DID_ISO_RESUME, &data->flags)) {
Oliver Neukum8efdd0c2011-02-11 13:00:06 +01001207 err = usb_autopm_get_interface(data->isoc ? data->isoc : data->intf);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001208 if (err < 0) {
1209 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1210 usb_kill_anchored_urbs(&data->isoc_anchor);
1211 return;
1212 }
1213
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -03001214 set_bit(BTUSB_DID_ISO_RESUME, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001215 }
Mikel Astizf4001d22012-04-11 08:48:51 +02001216
1217 if (hdev->voice_setting & 0x0020) {
1218 static const int alts[3] = { 2, 4, 5 };
Marcel Holtmann89e75332014-09-16 04:44:50 +02001219
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001220 new_alts = alts[data->sco_num - 1];
Mikel Astizf4001d22012-04-11 08:48:51 +02001221 } else {
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001222 new_alts = data->sco_num;
Mikel Astizf4001d22012-04-11 08:48:51 +02001223 }
1224
1225 if (data->isoc_altsetting != new_alts) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001226 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1227 usb_kill_anchored_urbs(&data->isoc_anchor);
1228
Mikel Astizf4001d22012-04-11 08:48:51 +02001229 if (__set_isoc_interface(hdev, new_alts) < 0)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001230 return;
1231 }
1232
1233 if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001234 if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001235 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1236 else
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001237 btusb_submit_isoc_urb(hdev, GFP_KERNEL);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001238 }
1239 } else {
1240 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1241 usb_kill_anchored_urbs(&data->isoc_anchor);
1242
1243 __set_isoc_interface(hdev, 0);
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -03001244 if (test_and_clear_bit(BTUSB_DID_ISO_RESUME, &data->flags))
Oliver Neukum8efdd0c2011-02-11 13:00:06 +01001245 usb_autopm_put_interface(data->isoc ? data->isoc : data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001246 }
1247}
1248
Oliver Neukum7bee5492009-08-24 23:44:59 +02001249static void btusb_waker(struct work_struct *work)
1250{
1251 struct btusb_data *data = container_of(work, struct btusb_data, waker);
1252 int err;
1253
1254 err = usb_autopm_get_interface(data->intf);
1255 if (err < 0)
1256 return;
1257
1258 usb_autopm_put_interface(data->intf);
1259}
1260
Marcel Holtmann9f8f9622013-04-10 08:11:35 -07001261static int btusb_setup_bcm92035(struct hci_dev *hdev)
1262{
1263 struct sk_buff *skb;
1264 u8 val = 0x00;
1265
1266 BT_DBG("%s", hdev->name);
1267
1268 skb = __hci_cmd_sync(hdev, 0xfc3b, 1, &val, HCI_INIT_TIMEOUT);
1269 if (IS_ERR(skb))
1270 BT_ERR("BCM92035 command failed (%ld)", -PTR_ERR(skb));
1271 else
1272 kfree_skb(skb);
1273
1274 return 0;
1275}
1276
Marcel Holtmann81cac642014-01-03 03:02:36 -08001277static int btusb_setup_csr(struct hci_dev *hdev)
1278{
1279 struct hci_rp_read_local_version *rp;
1280 struct sk_buff *skb;
1281 int ret;
1282
1283 BT_DBG("%s", hdev->name);
1284
1285 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
1286 HCI_INIT_TIMEOUT);
1287 if (IS_ERR(skb)) {
1288 BT_ERR("Reading local version failed (%ld)", -PTR_ERR(skb));
1289 return -PTR_ERR(skb);
1290 }
1291
Marcel Holtmann89e75332014-09-16 04:44:50 +02001292 rp = (struct hci_rp_read_local_version *)skb->data;
Marcel Holtmann81cac642014-01-03 03:02:36 -08001293
1294 if (!rp->status) {
1295 if (le16_to_cpu(rp->manufacturer) != 10) {
1296 /* Clear the reset quirk since this is not an actual
1297 * early Bluetooth 1.1 device from CSR.
1298 */
1299 clear_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
1300
1301 /* These fake CSR controllers have all a broken
1302 * stored link key handling and so just disable it.
1303 */
1304 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY,
1305 &hdev->quirks);
1306 }
1307 }
1308
1309 ret = -bt_to_errno(rp->status);
1310
1311 kfree_skb(skb);
1312
1313 return ret;
1314}
1315
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001316struct intel_version {
1317 u8 status;
1318 u8 hw_platform;
1319 u8 hw_variant;
1320 u8 hw_revision;
1321 u8 fw_variant;
1322 u8 fw_revision;
1323 u8 fw_build_num;
1324 u8 fw_build_ww;
1325 u8 fw_build_yy;
1326 u8 fw_patch_num;
1327} __packed;
1328
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001329struct intel_boot_params {
1330 __u8 status;
1331 __u8 otp_format;
1332 __u8 otp_content;
1333 __u8 otp_patch;
1334 __le16 dev_revid;
1335 __u8 secure_boot;
1336 __u8 key_from_hdr;
1337 __u8 key_type;
1338 __u8 otp_lock;
1339 __u8 api_lock;
1340 __u8 debug_lock;
1341 bdaddr_t otp_bdaddr;
1342 __u8 min_fw_build_nn;
1343 __u8 min_fw_build_cw;
1344 __u8 min_fw_build_yy;
1345 __u8 limited_cce;
1346 __u8 unlocked_state;
1347} __packed;
1348
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001349static const struct firmware *btusb_setup_intel_get_fw(struct hci_dev *hdev,
Marcel Holtmann89e75332014-09-16 04:44:50 +02001350 struct intel_version *ver)
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001351{
1352 const struct firmware *fw;
1353 char fwname[64];
1354 int ret;
1355
1356 snprintf(fwname, sizeof(fwname),
1357 "intel/ibt-hw-%x.%x.%x-fw-%x.%x.%x.%x.%x.bseq",
1358 ver->hw_platform, ver->hw_variant, ver->hw_revision,
1359 ver->fw_variant, ver->fw_revision, ver->fw_build_num,
1360 ver->fw_build_ww, ver->fw_build_yy);
1361
1362 ret = request_firmware(&fw, fwname, &hdev->dev);
1363 if (ret < 0) {
1364 if (ret == -EINVAL) {
1365 BT_ERR("%s Intel firmware file request failed (%d)",
1366 hdev->name, ret);
1367 return NULL;
1368 }
1369
1370 BT_ERR("%s failed to open Intel firmware file: %s(%d)",
1371 hdev->name, fwname, ret);
1372
1373 /* If the correct firmware patch file is not found, use the
1374 * default firmware patch file instead
1375 */
1376 snprintf(fwname, sizeof(fwname), "intel/ibt-hw-%x.%x.bseq",
1377 ver->hw_platform, ver->hw_variant);
1378 if (request_firmware(&fw, fwname, &hdev->dev) < 0) {
1379 BT_ERR("%s failed to open default Intel fw file: %s",
1380 hdev->name, fwname);
1381 return NULL;
1382 }
1383 }
1384
1385 BT_INFO("%s: Intel Bluetooth firmware file: %s", hdev->name, fwname);
1386
1387 return fw;
1388}
1389
1390static int btusb_setup_intel_patching(struct hci_dev *hdev,
1391 const struct firmware *fw,
1392 const u8 **fw_ptr, int *disable_patch)
1393{
1394 struct sk_buff *skb;
1395 struct hci_command_hdr *cmd;
1396 const u8 *cmd_param;
1397 struct hci_event_hdr *evt = NULL;
1398 const u8 *evt_param = NULL;
1399 int remain = fw->size - (*fw_ptr - fw->data);
1400
1401 /* The first byte indicates the types of the patch command or event.
1402 * 0x01 means HCI command and 0x02 is HCI event. If the first bytes
1403 * in the current firmware buffer doesn't start with 0x01 or
1404 * the size of remain buffer is smaller than HCI command header,
1405 * the firmware file is corrupted and it should stop the patching
1406 * process.
1407 */
1408 if (remain > HCI_COMMAND_HDR_SIZE && *fw_ptr[0] != 0x01) {
1409 BT_ERR("%s Intel fw corrupted: invalid cmd read", hdev->name);
1410 return -EINVAL;
1411 }
1412 (*fw_ptr)++;
1413 remain--;
1414
1415 cmd = (struct hci_command_hdr *)(*fw_ptr);
1416 *fw_ptr += sizeof(*cmd);
1417 remain -= sizeof(*cmd);
1418
1419 /* Ensure that the remain firmware data is long enough than the length
1420 * of command parameter. If not, the firmware file is corrupted.
1421 */
1422 if (remain < cmd->plen) {
1423 BT_ERR("%s Intel fw corrupted: invalid cmd len", hdev->name);
1424 return -EFAULT;
1425 }
1426
1427 /* If there is a command that loads a patch in the firmware
1428 * file, then enable the patch upon success, otherwise just
1429 * disable the manufacturer mode, for example patch activation
1430 * is not required when the default firmware patch file is used
1431 * because there are no patch data to load.
1432 */
1433 if (*disable_patch && le16_to_cpu(cmd->opcode) == 0xfc8e)
1434 *disable_patch = 0;
1435
1436 cmd_param = *fw_ptr;
1437 *fw_ptr += cmd->plen;
1438 remain -= cmd->plen;
1439
1440 /* This reads the expected events when the above command is sent to the
1441 * device. Some vendor commands expects more than one events, for
1442 * example command status event followed by vendor specific event.
1443 * For this case, it only keeps the last expected event. so the command
1444 * can be sent with __hci_cmd_sync_ev() which returns the sk_buff of
1445 * last expected event.
1446 */
1447 while (remain > HCI_EVENT_HDR_SIZE && *fw_ptr[0] == 0x02) {
1448 (*fw_ptr)++;
1449 remain--;
1450
1451 evt = (struct hci_event_hdr *)(*fw_ptr);
1452 *fw_ptr += sizeof(*evt);
1453 remain -= sizeof(*evt);
1454
1455 if (remain < evt->plen) {
1456 BT_ERR("%s Intel fw corrupted: invalid evt len",
1457 hdev->name);
1458 return -EFAULT;
1459 }
1460
1461 evt_param = *fw_ptr;
1462 *fw_ptr += evt->plen;
1463 remain -= evt->plen;
1464 }
1465
1466 /* Every HCI commands in the firmware file has its correspond event.
1467 * If event is not found or remain is smaller than zero, the firmware
1468 * file is corrupted.
1469 */
1470 if (!evt || !evt_param || remain < 0) {
1471 BT_ERR("%s Intel fw corrupted: invalid evt read", hdev->name);
1472 return -EFAULT;
1473 }
1474
1475 skb = __hci_cmd_sync_ev(hdev, le16_to_cpu(cmd->opcode), cmd->plen,
1476 cmd_param, evt->evt, HCI_INIT_TIMEOUT);
1477 if (IS_ERR(skb)) {
1478 BT_ERR("%s sending Intel patch command (0x%4.4x) failed (%ld)",
1479 hdev->name, cmd->opcode, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001480 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001481 }
1482
1483 /* It ensures that the returned event matches the event data read from
1484 * the firmware file. At fist, it checks the length and then
1485 * the contents of the event.
1486 */
1487 if (skb->len != evt->plen) {
1488 BT_ERR("%s mismatch event length (opcode 0x%4.4x)", hdev->name,
1489 le16_to_cpu(cmd->opcode));
1490 kfree_skb(skb);
1491 return -EFAULT;
1492 }
1493
1494 if (memcmp(skb->data, evt_param, evt->plen)) {
1495 BT_ERR("%s mismatch event parameter (opcode 0x%4.4x)",
1496 hdev->name, le16_to_cpu(cmd->opcode));
1497 kfree_skb(skb);
1498 return -EFAULT;
1499 }
1500 kfree_skb(skb);
1501
1502 return 0;
1503}
1504
Marcel Holtmann40cb0982014-07-02 12:06:45 +02001505#define BDADDR_INTEL (&(bdaddr_t) {{0x00, 0x8b, 0x9e, 0x19, 0x03, 0x00}})
1506
1507static int btusb_check_bdaddr_intel(struct hci_dev *hdev)
1508{
1509 struct sk_buff *skb;
1510 struct hci_rp_read_bd_addr *rp;
1511
1512 skb = __hci_cmd_sync(hdev, HCI_OP_READ_BD_ADDR, 0, NULL,
1513 HCI_INIT_TIMEOUT);
1514 if (IS_ERR(skb)) {
1515 BT_ERR("%s reading Intel device address failed (%ld)",
1516 hdev->name, PTR_ERR(skb));
1517 return PTR_ERR(skb);
1518 }
1519
1520 if (skb->len != sizeof(*rp)) {
1521 BT_ERR("%s Intel device address length mismatch", hdev->name);
1522 kfree_skb(skb);
1523 return -EIO;
1524 }
1525
Marcel Holtmann89e75332014-09-16 04:44:50 +02001526 rp = (struct hci_rp_read_bd_addr *)skb->data;
Marcel Holtmann40cb0982014-07-02 12:06:45 +02001527 if (rp->status) {
1528 BT_ERR("%s Intel device address result failed (%02x)",
1529 hdev->name, rp->status);
1530 kfree_skb(skb);
1531 return -bt_to_errno(rp->status);
1532 }
1533
1534 /* For some Intel based controllers, the default Bluetooth device
1535 * address 00:03:19:9E:8B:00 can be found. These controllers are
1536 * fully operational, but have the danger of duplicate addresses
1537 * and that in turn can cause problems with Bluetooth operation.
1538 */
Marcel Holtmann4739b5b2014-07-04 16:54:38 +02001539 if (!bacmp(&rp->bdaddr, BDADDR_INTEL)) {
Marcel Holtmann40cb0982014-07-02 12:06:45 +02001540 BT_ERR("%s found Intel default device address (%pMR)",
1541 hdev->name, &rp->bdaddr);
Marcel Holtmann4739b5b2014-07-04 16:54:38 +02001542 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
1543 }
Marcel Holtmann40cb0982014-07-02 12:06:45 +02001544
1545 kfree_skb(skb);
1546
1547 return 0;
1548}
1549
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001550static int btusb_setup_intel(struct hci_dev *hdev)
1551{
1552 struct sk_buff *skb;
1553 const struct firmware *fw;
1554 const u8 *fw_ptr;
1555 int disable_patch;
1556 struct intel_version *ver;
1557
1558 const u8 mfg_enable[] = { 0x01, 0x00 };
1559 const u8 mfg_disable[] = { 0x00, 0x00 };
1560 const u8 mfg_reset_deactivate[] = { 0x00, 0x01 };
1561 const u8 mfg_reset_activate[] = { 0x00, 0x02 };
1562
1563 BT_DBG("%s", hdev->name);
1564
1565 /* The controller has a bug with the first HCI command sent to it
1566 * returning number of completed commands as zero. This would stall the
1567 * command processing in the Bluetooth core.
1568 *
1569 * As a workaround, send HCI Reset command first which will reset the
1570 * number of completed commands and allow normal command processing
1571 * from now on.
1572 */
1573 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
1574 if (IS_ERR(skb)) {
1575 BT_ERR("%s sending initial HCI reset command failed (%ld)",
1576 hdev->name, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001577 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001578 }
1579 kfree_skb(skb);
1580
1581 /* Read Intel specific controller version first to allow selection of
1582 * which firmware file to load.
1583 *
1584 * The returned information are hardware variant and revision plus
1585 * firmware variant, revision and build number.
1586 */
1587 skb = __hci_cmd_sync(hdev, 0xfc05, 0, NULL, HCI_INIT_TIMEOUT);
1588 if (IS_ERR(skb)) {
1589 BT_ERR("%s reading Intel fw version command failed (%ld)",
1590 hdev->name, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001591 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001592 }
1593
1594 if (skb->len != sizeof(*ver)) {
1595 BT_ERR("%s Intel version event length mismatch", hdev->name);
1596 kfree_skb(skb);
1597 return -EIO;
1598 }
1599
1600 ver = (struct intel_version *)skb->data;
1601 if (ver->status) {
1602 BT_ERR("%s Intel fw version event failed (%02x)", hdev->name,
1603 ver->status);
1604 kfree_skb(skb);
1605 return -bt_to_errno(ver->status);
1606 }
1607
1608 BT_INFO("%s: read Intel version: %02x%02x%02x%02x%02x%02x%02x%02x%02x",
1609 hdev->name, ver->hw_platform, ver->hw_variant,
1610 ver->hw_revision, ver->fw_variant, ver->fw_revision,
1611 ver->fw_build_num, ver->fw_build_ww, ver->fw_build_yy,
1612 ver->fw_patch_num);
1613
1614 /* fw_patch_num indicates the version of patch the device currently
1615 * have. If there is no patch data in the device, it is always 0x00.
1616 * So, if it is other than 0x00, no need to patch the deivce again.
1617 */
1618 if (ver->fw_patch_num) {
1619 BT_INFO("%s: Intel device is already patched. patch num: %02x",
1620 hdev->name, ver->fw_patch_num);
1621 kfree_skb(skb);
Marcel Holtmann40cb0982014-07-02 12:06:45 +02001622 btusb_check_bdaddr_intel(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001623 return 0;
1624 }
1625
1626 /* Opens the firmware patch file based on the firmware version read
1627 * from the controller. If it fails to open the matching firmware
1628 * patch file, it tries to open the default firmware patch file.
1629 * If no patch file is found, allow the device to operate without
1630 * a patch.
1631 */
1632 fw = btusb_setup_intel_get_fw(hdev, ver);
1633 if (!fw) {
1634 kfree_skb(skb);
Marcel Holtmann40cb0982014-07-02 12:06:45 +02001635 btusb_check_bdaddr_intel(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001636 return 0;
1637 }
1638 fw_ptr = fw->data;
1639
1640 /* This Intel specific command enables the manufacturer mode of the
1641 * controller.
1642 *
1643 * Only while this mode is enabled, the driver can download the
1644 * firmware patch data and configuration parameters.
1645 */
1646 skb = __hci_cmd_sync(hdev, 0xfc11, 2, mfg_enable, HCI_INIT_TIMEOUT);
1647 if (IS_ERR(skb)) {
1648 BT_ERR("%s entering Intel manufacturer mode failed (%ld)",
1649 hdev->name, PTR_ERR(skb));
1650 release_firmware(fw);
Adam Leed9c78e92013-07-10 10:02:12 +08001651 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001652 }
1653
1654 if (skb->data[0]) {
1655 u8 evt_status = skb->data[0];
Marcel Holtmann89e75332014-09-16 04:44:50 +02001656
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001657 BT_ERR("%s enable Intel manufacturer mode event failed (%02x)",
1658 hdev->name, evt_status);
1659 kfree_skb(skb);
1660 release_firmware(fw);
1661 return -bt_to_errno(evt_status);
1662 }
1663 kfree_skb(skb);
1664
1665 disable_patch = 1;
1666
1667 /* The firmware data file consists of list of Intel specific HCI
1668 * commands and its expected events. The first byte indicates the
1669 * type of the message, either HCI command or HCI event.
1670 *
1671 * It reads the command and its expected event from the firmware file,
1672 * and send to the controller. Once __hci_cmd_sync_ev() returns,
1673 * the returned event is compared with the event read from the firmware
1674 * file and it will continue until all the messages are downloaded to
1675 * the controller.
1676 *
1677 * Once the firmware patching is completed successfully,
1678 * the manufacturer mode is disabled with reset and activating the
1679 * downloaded patch.
1680 *
1681 * If the firmware patching fails, the manufacturer mode is
1682 * disabled with reset and deactivating the patch.
1683 *
1684 * If the default patch file is used, no reset is done when disabling
1685 * the manufacturer.
1686 */
1687 while (fw->size > fw_ptr - fw->data) {
1688 int ret;
1689
1690 ret = btusb_setup_intel_patching(hdev, fw, &fw_ptr,
1691 &disable_patch);
1692 if (ret < 0)
1693 goto exit_mfg_deactivate;
1694 }
1695
1696 release_firmware(fw);
1697
1698 if (disable_patch)
1699 goto exit_mfg_disable;
1700
1701 /* Patching completed successfully and disable the manufacturer mode
1702 * with reset and activate the downloaded firmware patches.
1703 */
1704 skb = __hci_cmd_sync(hdev, 0xfc11, sizeof(mfg_reset_activate),
1705 mfg_reset_activate, HCI_INIT_TIMEOUT);
1706 if (IS_ERR(skb)) {
1707 BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
1708 hdev->name, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001709 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001710 }
1711 kfree_skb(skb);
1712
1713 BT_INFO("%s: Intel Bluetooth firmware patch completed and activated",
1714 hdev->name);
1715
Marcel Holtmann40cb0982014-07-02 12:06:45 +02001716 btusb_check_bdaddr_intel(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001717 return 0;
1718
1719exit_mfg_disable:
1720 /* Disable the manufacturer mode without reset */
1721 skb = __hci_cmd_sync(hdev, 0xfc11, sizeof(mfg_disable), mfg_disable,
1722 HCI_INIT_TIMEOUT);
1723 if (IS_ERR(skb)) {
1724 BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
1725 hdev->name, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001726 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001727 }
1728 kfree_skb(skb);
1729
1730 BT_INFO("%s: Intel Bluetooth firmware patch completed", hdev->name);
Marcel Holtmann40cb0982014-07-02 12:06:45 +02001731
1732 btusb_check_bdaddr_intel(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001733 return 0;
1734
1735exit_mfg_deactivate:
1736 release_firmware(fw);
1737
1738 /* Patching failed. Disable the manufacturer mode with reset and
1739 * deactivate the downloaded firmware patches.
1740 */
1741 skb = __hci_cmd_sync(hdev, 0xfc11, sizeof(mfg_reset_deactivate),
1742 mfg_reset_deactivate, HCI_INIT_TIMEOUT);
1743 if (IS_ERR(skb)) {
1744 BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
1745 hdev->name, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001746 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001747 }
1748 kfree_skb(skb);
1749
1750 BT_INFO("%s: Intel Bluetooth firmware patch completed and deactivated",
1751 hdev->name);
1752
Marcel Holtmann40cb0982014-07-02 12:06:45 +02001753 btusb_check_bdaddr_intel(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001754 return 0;
1755}
1756
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001757static int inject_cmd_complete(struct hci_dev *hdev, __u16 opcode)
1758{
1759 struct sk_buff *skb;
1760 struct hci_event_hdr *hdr;
1761 struct hci_ev_cmd_complete *evt;
1762
1763 skb = bt_skb_alloc(sizeof(*hdr) + sizeof(*evt) + 1, GFP_ATOMIC);
1764 if (!skb)
1765 return -ENOMEM;
1766
1767 hdr = (struct hci_event_hdr *)skb_put(skb, sizeof(*hdr));
1768 hdr->evt = HCI_EV_CMD_COMPLETE;
1769 hdr->plen = sizeof(*evt) + 1;
1770
1771 evt = (struct hci_ev_cmd_complete *)skb_put(skb, sizeof(*evt));
1772 evt->ncmd = 0x01;
1773 evt->opcode = cpu_to_le16(opcode);
1774
1775 *skb_put(skb, 1) = 0x00;
1776
1777 bt_cb(skb)->pkt_type = HCI_EVENT_PKT;
1778
1779 return hci_recv_frame(hdev, skb);
1780}
1781
1782static int btusb_recv_bulk_intel(struct btusb_data *data, void *buffer,
1783 int count)
1784{
1785 /* When the device is in bootloader mode, then it can send
1786 * events via the bulk endpoint. These events are treated the
1787 * same way as the ones received from the interrupt endpoint.
1788 */
1789 if (test_bit(BTUSB_BOOTLOADER, &data->flags))
1790 return btusb_recv_intr(data, buffer, count);
1791
1792 return btusb_recv_bulk(data, buffer, count);
1793}
1794
1795static int btusb_recv_event_intel(struct hci_dev *hdev, struct sk_buff *skb)
1796{
1797 struct btusb_data *data = hci_get_drvdata(hdev);
1798
1799 if (test_bit(BTUSB_BOOTLOADER, &data->flags)) {
1800 struct hci_event_hdr *hdr = (void *)skb->data;
1801
1802 /* When the firmware loading completes the device sends
1803 * out a vendor specific event indicating the result of
1804 * the firmware loading.
1805 */
1806 if (skb->len == 7 && hdr->evt == 0xff && hdr->plen == 0x05 &&
1807 skb->data[2] == 0x06) {
1808 if (skb->data[3] != 0x00)
1809 test_bit(BTUSB_FIRMWARE_FAILED, &data->flags);
1810
Marcel Holtmannce6bb922015-01-28 01:58:40 -08001811 if (test_and_clear_bit(BTUSB_DOWNLOADING,
1812 &data->flags) &&
Johan Hedberga087a982015-01-30 10:58:54 +02001813 test_bit(BTUSB_FIRMWARE_LOADED, &data->flags)) {
1814 smp_mb__after_atomic();
1815 wake_up_bit(&data->flags, BTUSB_DOWNLOADING);
1816 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001817 }
1818
1819 /* When switching to the operational firmware the device
1820 * sends a vendor specific event indicating that the bootup
1821 * completed.
1822 */
1823 if (skb->len == 9 && hdr->evt == 0xff && hdr->plen == 0x07 &&
1824 skb->data[2] == 0x02) {
Johan Hedbergfad70972015-01-30 10:58:55 +02001825 if (test_and_clear_bit(BTUSB_BOOTING, &data->flags)) {
1826 smp_mb__after_atomic();
1827 wake_up_bit(&data->flags, BTUSB_BOOTING);
1828 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001829 }
1830 }
1831
1832 return hci_recv_frame(hdev, skb);
1833}
1834
1835static int btusb_send_frame_intel(struct hci_dev *hdev, struct sk_buff *skb)
1836{
1837 struct btusb_data *data = hci_get_drvdata(hdev);
1838 struct urb *urb;
1839
1840 BT_DBG("%s", hdev->name);
1841
1842 if (!test_bit(HCI_RUNNING, &hdev->flags))
1843 return -EBUSY;
1844
1845 switch (bt_cb(skb)->pkt_type) {
1846 case HCI_COMMAND_PKT:
1847 if (test_bit(BTUSB_BOOTLOADER, &data->flags)) {
1848 struct hci_command_hdr *cmd = (void *)skb->data;
1849 __u16 opcode = le16_to_cpu(cmd->opcode);
1850
1851 /* When in bootloader mode and the command 0xfc09
1852 * is received, it needs to be send down the
1853 * bulk endpoint. So allocate a bulk URB instead.
1854 */
1855 if (opcode == 0xfc09)
1856 urb = alloc_bulk_urb(hdev, skb);
1857 else
1858 urb = alloc_ctrl_urb(hdev, skb);
1859
1860 /* When the 0xfc01 command is issued to boot into
1861 * the operational firmware, it will actually not
1862 * send a command complete event. To keep the flow
1863 * control working inject that event here.
1864 */
1865 if (opcode == 0xfc01)
1866 inject_cmd_complete(hdev, opcode);
1867 } else {
1868 urb = alloc_ctrl_urb(hdev, skb);
1869 }
1870 if (IS_ERR(urb))
1871 return PTR_ERR(urb);
1872
1873 hdev->stat.cmd_tx++;
1874 return submit_or_queue_tx_urb(hdev, urb);
1875
1876 case HCI_ACLDATA_PKT:
1877 urb = alloc_bulk_urb(hdev, skb);
1878 if (IS_ERR(urb))
1879 return PTR_ERR(urb);
1880
1881 hdev->stat.acl_tx++;
1882 return submit_or_queue_tx_urb(hdev, urb);
1883
1884 case HCI_SCODATA_PKT:
1885 if (hci_conn_num(hdev, SCO_LINK) < 1)
1886 return -ENODEV;
1887
1888 urb = alloc_isoc_urb(hdev, skb);
1889 if (IS_ERR(urb))
1890 return PTR_ERR(urb);
1891
1892 hdev->stat.sco_tx++;
1893 return submit_tx_urb(hdev, urb);
1894 }
1895
1896 return -EILSEQ;
1897}
1898
1899static int btusb_intel_secure_send(struct hci_dev *hdev, u8 fragment_type,
1900 u32 plen, const void *param)
1901{
1902 while (plen > 0) {
1903 struct sk_buff *skb;
1904 u8 cmd_param[253], fragment_len = (plen > 252) ? 252 : plen;
1905
1906 cmd_param[0] = fragment_type;
1907 memcpy(cmd_param + 1, param, fragment_len);
1908
1909 skb = __hci_cmd_sync(hdev, 0xfc09, fragment_len + 1,
1910 cmd_param, HCI_INIT_TIMEOUT);
1911 if (IS_ERR(skb))
1912 return PTR_ERR(skb);
1913
1914 kfree_skb(skb);
1915
1916 plen -= fragment_len;
1917 param += fragment_len;
1918 }
1919
1920 return 0;
1921}
1922
1923static void btusb_intel_version_info(struct hci_dev *hdev,
1924 struct intel_version *ver)
1925{
1926 const char *variant;
1927
1928 switch (ver->fw_variant) {
1929 case 0x06:
1930 variant = "Bootloader";
1931 break;
1932 case 0x23:
1933 variant = "Firmware";
1934 break;
1935 default:
1936 return;
1937 }
1938
1939 BT_INFO("%s: %s revision %u.%u build %u week %u %u", hdev->name,
1940 variant, ver->fw_revision >> 4, ver->fw_revision & 0x0f,
1941 ver->fw_build_num, ver->fw_build_ww, 2000 + ver->fw_build_yy);
1942}
1943
1944static int btusb_setup_intel_new(struct hci_dev *hdev)
1945{
1946 static const u8 reset_param[] = { 0x00, 0x01, 0x00, 0x01,
1947 0x00, 0x08, 0x04, 0x00 };
1948 struct btusb_data *data = hci_get_drvdata(hdev);
1949 struct sk_buff *skb;
1950 struct intel_version *ver;
1951 struct intel_boot_params *params;
1952 const struct firmware *fw;
1953 const u8 *fw_ptr;
1954 char fwname[64];
1955 ktime_t calltime, delta, rettime;
1956 unsigned long long duration;
1957 int err;
1958
1959 BT_DBG("%s", hdev->name);
1960
1961 calltime = ktime_get();
1962
1963 /* Read the Intel version information to determine if the device
1964 * is in bootloader mode or if it already has operational firmware
1965 * loaded.
1966 */
1967 skb = __hci_cmd_sync(hdev, 0xfc05, 0, NULL, HCI_INIT_TIMEOUT);
1968 if (IS_ERR(skb)) {
1969 BT_ERR("%s: Reading Intel version information failed (%ld)",
1970 hdev->name, PTR_ERR(skb));
1971 return PTR_ERR(skb);
1972 }
1973
1974 if (skb->len != sizeof(*ver)) {
1975 BT_ERR("%s: Intel version event size mismatch", hdev->name);
1976 kfree_skb(skb);
1977 return -EILSEQ;
1978 }
1979
1980 ver = (struct intel_version *)skb->data;
1981 if (ver->status) {
1982 BT_ERR("%s: Intel version command failure (%02x)",
1983 hdev->name, ver->status);
1984 err = -bt_to_errno(ver->status);
1985 kfree_skb(skb);
1986 return err;
1987 }
1988
1989 /* The hardware platform number has a fixed value of 0x37 and
1990 * for now only accept this single value.
1991 */
1992 if (ver->hw_platform != 0x37) {
1993 BT_ERR("%s: Unsupported Intel hardware platform (%u)",
1994 hdev->name, ver->hw_platform);
1995 kfree_skb(skb);
1996 return -EINVAL;
1997 }
1998
1999 /* At the moment only the hardware variant iBT 3.0 (LnP/SfP) is
2000 * supported by this firmware loading method. This check has been
2001 * put in place to ensure correct forward compatibility options
2002 * when newer hardware variants come along.
2003 */
2004 if (ver->hw_variant != 0x0b) {
2005 BT_ERR("%s: Unsupported Intel hardware variant (%u)",
2006 hdev->name, ver->hw_variant);
2007 kfree_skb(skb);
2008 return -EINVAL;
2009 }
2010
2011 btusb_intel_version_info(hdev, ver);
2012
2013 /* The firmware variant determines if the device is in bootloader
2014 * mode or is running operational firmware. The value 0x06 identifies
2015 * the bootloader and the value 0x23 identifies the operational
2016 * firmware.
2017 *
2018 * When the operational firmware is already present, then only
2019 * the check for valid Bluetooth device address is needed. This
2020 * determines if the device will be added as configured or
2021 * unconfigured controller.
2022 *
2023 * It is not possible to use the Secure Boot Parameters in this
2024 * case since that command is only available in bootloader mode.
2025 */
2026 if (ver->fw_variant == 0x23) {
2027 kfree_skb(skb);
2028 clear_bit(BTUSB_BOOTLOADER, &data->flags);
2029 btusb_check_bdaddr_intel(hdev);
2030 return 0;
2031 }
2032
2033 /* If the device is not in bootloader mode, then the only possible
2034 * choice is to return an error and abort the device initialization.
2035 */
2036 if (ver->fw_variant != 0x06) {
2037 BT_ERR("%s: Unsupported Intel firmware variant (%u)",
2038 hdev->name, ver->fw_variant);
2039 kfree_skb(skb);
2040 return -ENODEV;
2041 }
2042
2043 kfree_skb(skb);
2044
2045 /* Read the secure boot parameters to identify the operating
2046 * details of the bootloader.
2047 */
2048 skb = __hci_cmd_sync(hdev, 0xfc0d, 0, NULL, HCI_INIT_TIMEOUT);
2049 if (IS_ERR(skb)) {
2050 BT_ERR("%s: Reading Intel boot parameters failed (%ld)",
2051 hdev->name, PTR_ERR(skb));
2052 return PTR_ERR(skb);
2053 }
2054
2055 if (skb->len != sizeof(*params)) {
2056 BT_ERR("%s: Intel boot parameters size mismatch", hdev->name);
2057 kfree_skb(skb);
2058 return -EILSEQ;
2059 }
2060
2061 params = (struct intel_boot_params *)skb->data;
2062 if (params->status) {
2063 BT_ERR("%s: Intel boot parameters command failure (%02x)",
2064 hdev->name, params->status);
2065 err = -bt_to_errno(params->status);
2066 kfree_skb(skb);
2067 return err;
2068 }
2069
2070 BT_INFO("%s: Device revision is %u", hdev->name,
2071 le16_to_cpu(params->dev_revid));
2072
2073 BT_INFO("%s: Secure boot is %s", hdev->name,
2074 params->secure_boot ? "enabled" : "disabled");
2075
2076 BT_INFO("%s: Minimum firmware build %u week %u %u", hdev->name,
2077 params->min_fw_build_nn, params->min_fw_build_cw,
2078 2000 + params->min_fw_build_yy);
2079
2080 /* It is required that every single firmware fragment is acknowledged
2081 * with a command complete event. If the boot parameters indicate
2082 * that this bootloader does not send them, then abort the setup.
2083 */
2084 if (params->limited_cce != 0x00) {
2085 BT_ERR("%s: Unsupported Intel firmware loading method (%u)",
2086 hdev->name, params->limited_cce);
2087 kfree_skb(skb);
2088 return -EINVAL;
2089 }
2090
2091 /* If the OTP has no valid Bluetooth device address, then there will
2092 * also be no valid address for the operational firmware.
2093 */
2094 if (!bacmp(&params->otp_bdaddr, BDADDR_ANY)) {
2095 BT_INFO("%s: No device address configured", hdev->name);
2096 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
2097 }
2098
2099 /* With this Intel bootloader only the hardware variant and device
2100 * revision information are used to select the right firmware.
2101 *
2102 * Currently this bootloader support is limited to hardware variant
2103 * iBT 3.0 (LnP/SfP) which is identified by the value 11 (0x0b).
2104 */
2105 snprintf(fwname, sizeof(fwname), "intel/ibt-11-%u.sfi",
2106 le16_to_cpu(params->dev_revid));
2107
2108 err = request_firmware(&fw, fwname, &hdev->dev);
2109 if (err < 0) {
2110 BT_ERR("%s: Failed to load Intel firmware file (%d)",
2111 hdev->name, err);
2112 kfree_skb(skb);
2113 return err;
2114 }
2115
2116 BT_INFO("%s: Found device firmware: %s", hdev->name, fwname);
2117
2118 kfree_skb(skb);
2119
2120 if (fw->size < 644) {
2121 BT_ERR("%s: Invalid size of firmware file (%zu)",
2122 hdev->name, fw->size);
2123 err = -EBADF;
2124 goto done;
2125 }
2126
2127 set_bit(BTUSB_DOWNLOADING, &data->flags);
2128
2129 /* Start the firmware download transaction with the Init fragment
2130 * represented by the 128 bytes of CSS header.
2131 */
2132 err = btusb_intel_secure_send(hdev, 0x00, 128, fw->data);
2133 if (err < 0) {
2134 BT_ERR("%s: Failed to send firmware header (%d)",
2135 hdev->name, err);
2136 goto done;
2137 }
2138
2139 /* Send the 256 bytes of public key information from the firmware
2140 * as the PKey fragment.
2141 */
2142 err = btusb_intel_secure_send(hdev, 0x03, 256, fw->data + 128);
2143 if (err < 0) {
2144 BT_ERR("%s: Failed to send firmware public key (%d)",
2145 hdev->name, err);
2146 goto done;
2147 }
2148
2149 /* Send the 256 bytes of signature information from the firmware
2150 * as the Sign fragment.
2151 */
2152 err = btusb_intel_secure_send(hdev, 0x02, 256, fw->data + 388);
2153 if (err < 0) {
2154 BT_ERR("%s: Failed to send firmware signature (%d)",
2155 hdev->name, err);
2156 goto done;
2157 }
2158
2159 fw_ptr = fw->data + 644;
2160
2161 while (fw_ptr - fw->data < fw->size) {
2162 struct hci_command_hdr *cmd = (void *)fw_ptr;
2163 u8 cmd_len;
2164
2165 cmd_len = sizeof(*cmd) + cmd->plen;
2166
2167 /* Send each command from the firmware data buffer as
2168 * a single Data fragment.
2169 */
2170 err = btusb_intel_secure_send(hdev, 0x01, cmd_len, fw_ptr);
2171 if (err < 0) {
2172 BT_ERR("%s: Failed to send firmware data (%d)",
2173 hdev->name, err);
2174 goto done;
2175 }
2176
2177 fw_ptr += cmd_len;
2178 }
2179
Marcel Holtmannce6bb922015-01-28 01:58:40 -08002180 set_bit(BTUSB_FIRMWARE_LOADED, &data->flags);
2181
Johan Hedberga087a982015-01-30 10:58:54 +02002182 BT_INFO("%s: Waiting for firmware download to complete", hdev->name);
2183
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002184 /* Before switching the device into operational mode and with that
2185 * booting the loaded firmware, wait for the bootloader notification
2186 * that all fragments have been successfully received.
2187 *
Johan Hedberga087a982015-01-30 10:58:54 +02002188 * When the event processing receives the notification, then the
2189 * BTUSB_DOWNLOADING flag will be cleared.
2190 *
2191 * The firmware loading should not take longer than 5 seconds
2192 * and thus just timeout if that happens and fail the setup
2193 * of this device.
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002194 */
Johan Hedberga087a982015-01-30 10:58:54 +02002195 err = btusb_wait_on_bit_timeout(&data->flags, BTUSB_DOWNLOADING,
2196 msecs_to_jiffies(5000),
2197 TASK_INTERRUPTIBLE);
2198 if (err == 1) {
2199 BT_ERR("%s: Firmware loading interrupted", hdev->name);
2200 err = -EINTR;
2201 goto done;
2202 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002203
Johan Hedberga087a982015-01-30 10:58:54 +02002204 if (err) {
2205 BT_ERR("%s: Firmware loading timeout", hdev->name);
2206 err = -ETIMEDOUT;
2207 goto done;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002208 }
2209
2210 if (test_bit(BTUSB_FIRMWARE_FAILED, &data->flags)) {
2211 BT_ERR("%s: Firmware loading failed", hdev->name);
2212 err = -ENOEXEC;
2213 goto done;
2214 }
2215
2216 rettime = ktime_get();
2217 delta = ktime_sub(rettime, calltime);
2218 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
2219
2220 BT_INFO("%s: Firmware loaded in %llu usecs", hdev->name, duration);
2221
2222done:
2223 release_firmware(fw);
2224
2225 if (err < 0)
2226 return err;
2227
2228 calltime = ktime_get();
2229
2230 set_bit(BTUSB_BOOTING, &data->flags);
2231
2232 skb = __hci_cmd_sync(hdev, 0xfc01, sizeof(reset_param), reset_param,
2233 HCI_INIT_TIMEOUT);
2234 if (IS_ERR(skb))
2235 return PTR_ERR(skb);
2236
2237 kfree_skb(skb);
2238
2239 /* The bootloader will not indicate when the device is ready. This
2240 * is done by the operational firmware sending bootup notification.
Johan Hedbergfad70972015-01-30 10:58:55 +02002241 *
2242 * Booting into operational firmware should not take longer than
2243 * 1 second. However if that happens, then just fail the setup
2244 * since something went wrong.
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002245 */
Johan Hedbergfad70972015-01-30 10:58:55 +02002246 BT_INFO("%s: Waiting for device to boot", hdev->name);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002247
Johan Hedbergfad70972015-01-30 10:58:55 +02002248 err = btusb_wait_on_bit_timeout(&data->flags, BTUSB_BOOTING,
2249 msecs_to_jiffies(1000),
2250 TASK_INTERRUPTIBLE);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002251
Johan Hedbergfad70972015-01-30 10:58:55 +02002252 if (err == 1) {
2253 BT_ERR("%s: Device boot interrupted", hdev->name);
2254 return -EINTR;
2255 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002256
Johan Hedbergfad70972015-01-30 10:58:55 +02002257 if (err) {
2258 BT_ERR("%s: Device boot timeout", hdev->name);
2259 return -ETIMEDOUT;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002260 }
2261
2262 rettime = ktime_get();
2263 delta = ktime_sub(rettime, calltime);
2264 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
2265
2266 BT_INFO("%s: Device booted in %llu usecs", hdev->name, duration);
2267
2268 clear_bit(BTUSB_BOOTLOADER, &data->flags);
2269
2270 return 0;
2271}
2272
Marcel Holtmann385a7682015-01-28 11:09:56 -08002273static void btusb_hw_error_intel(struct hci_dev *hdev, u8 code)
2274{
2275 struct sk_buff *skb;
2276 u8 type = 0x00;
2277
2278 BT_ERR("%s: Hardware error 0x%2.2x", hdev->name, code);
2279
2280 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
2281 if (IS_ERR(skb)) {
2282 BT_ERR("%s: Reset after hardware error failed (%ld)",
2283 hdev->name, PTR_ERR(skb));
2284 return;
2285 }
2286 kfree_skb(skb);
2287
2288 skb = __hci_cmd_sync(hdev, 0xfc22, 1, &type, HCI_INIT_TIMEOUT);
2289 if (IS_ERR(skb)) {
2290 BT_ERR("%s: Retrieving Intel exception info failed (%ld)",
2291 hdev->name, PTR_ERR(skb));
2292 return;
2293 }
2294
2295 if (skb->len != 13) {
2296 BT_ERR("%s: Exception info size mismatch", hdev->name);
2297 kfree_skb(skb);
2298 return;
2299 }
2300
2301 if (skb->data[0] != 0x00) {
2302 BT_ERR("%s: Exception info command failure (%02x)",
2303 hdev->name, skb->data[0]);
2304 kfree_skb(skb);
2305 return;
2306 }
2307
2308 BT_ERR("%s: Exception info %s", hdev->name, (char *)(skb->data + 1));
2309
2310 kfree_skb(skb);
2311}
2312
Marcel Holtmanncb8d6592014-07-02 11:25:25 +02002313static int btusb_set_bdaddr_intel(struct hci_dev *hdev, const bdaddr_t *bdaddr)
2314{
2315 struct sk_buff *skb;
2316 long ret;
2317
2318 skb = __hci_cmd_sync(hdev, 0xfc31, 6, bdaddr, HCI_INIT_TIMEOUT);
2319 if (IS_ERR(skb)) {
2320 ret = PTR_ERR(skb);
2321 BT_ERR("%s: changing Intel device address failed (%ld)",
Marcel Holtmann89e75332014-09-16 04:44:50 +02002322 hdev->name, ret);
Marcel Holtmanncb8d6592014-07-02 11:25:25 +02002323 return ret;
2324 }
2325 kfree_skb(skb);
2326
2327 return 0;
2328}
2329
Amitkumar Karwarae8df492014-07-18 14:47:06 -07002330static int btusb_set_bdaddr_marvell(struct hci_dev *hdev,
2331 const bdaddr_t *bdaddr)
2332{
2333 struct sk_buff *skb;
2334 u8 buf[8];
2335 long ret;
2336
2337 buf[0] = 0xfe;
2338 buf[1] = sizeof(bdaddr_t);
2339 memcpy(buf + 2, bdaddr, sizeof(bdaddr_t));
2340
2341 skb = __hci_cmd_sync(hdev, 0xfc22, sizeof(buf), buf, HCI_INIT_TIMEOUT);
2342 if (IS_ERR(skb)) {
2343 ret = PTR_ERR(skb);
2344 BT_ERR("%s: changing Marvell device address failed (%ld)",
2345 hdev->name, ret);
2346 return ret;
2347 }
2348 kfree_skb(skb);
2349
2350 return 0;
2351}
2352
Marcel Holtmannc8abb732014-07-02 12:38:22 +02002353#define BDADDR_BCM20702A0 (&(bdaddr_t) {{0x00, 0xa0, 0x02, 0x70, 0x20, 0x00}})
2354
Petri Gynther10d4c672014-05-08 15:50:01 -07002355static int btusb_setup_bcm_patchram(struct hci_dev *hdev)
2356{
2357 struct btusb_data *data = hci_get_drvdata(hdev);
2358 struct usb_device *udev = data->udev;
2359 char fw_name[64];
2360 const struct firmware *fw;
2361 const u8 *fw_ptr;
2362 size_t fw_size;
2363 const struct hci_command_hdr *cmd;
2364 const u8 *cmd_param;
2365 u16 opcode;
2366 struct sk_buff *skb;
2367 struct hci_rp_read_local_version *ver;
Marcel Holtmannc8abb732014-07-02 12:38:22 +02002368 struct hci_rp_read_bd_addr *bda;
Petri Gynther10d4c672014-05-08 15:50:01 -07002369 long ret;
2370
2371 snprintf(fw_name, sizeof(fw_name), "brcm/%s-%04x-%04x.hcd",
2372 udev->product ? udev->product : "BCM",
2373 le16_to_cpu(udev->descriptor.idVendor),
2374 le16_to_cpu(udev->descriptor.idProduct));
2375
2376 ret = request_firmware(&fw, fw_name, &hdev->dev);
2377 if (ret < 0) {
Marcel Holtmannc8abb732014-07-02 12:38:22 +02002378 BT_INFO("%s: BCM: patch %s not found", hdev->name, fw_name);
Petri Gynther10d4c672014-05-08 15:50:01 -07002379 return 0;
2380 }
2381
2382 /* Reset */
2383 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
2384 if (IS_ERR(skb)) {
2385 ret = PTR_ERR(skb);
2386 BT_ERR("%s: HCI_OP_RESET failed (%ld)", hdev->name, ret);
2387 goto done;
2388 }
2389 kfree_skb(skb);
2390
2391 /* Read Local Version Info */
2392 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
2393 HCI_INIT_TIMEOUT);
2394 if (IS_ERR(skb)) {
2395 ret = PTR_ERR(skb);
2396 BT_ERR("%s: HCI_OP_READ_LOCAL_VERSION failed (%ld)",
Marcel Holtmann89e75332014-09-16 04:44:50 +02002397 hdev->name, ret);
Petri Gynther10d4c672014-05-08 15:50:01 -07002398 goto done;
2399 }
2400
2401 if (skb->len != sizeof(*ver)) {
2402 BT_ERR("%s: HCI_OP_READ_LOCAL_VERSION event length mismatch",
Marcel Holtmann89e75332014-09-16 04:44:50 +02002403 hdev->name);
Petri Gynther10d4c672014-05-08 15:50:01 -07002404 kfree_skb(skb);
2405 ret = -EIO;
2406 goto done;
2407 }
2408
Marcel Holtmann89e75332014-09-16 04:44:50 +02002409 ver = (struct hci_rp_read_local_version *)skb->data;
Petri Gynther10d4c672014-05-08 15:50:01 -07002410 BT_INFO("%s: BCM: patching hci_ver=%02x hci_rev=%04x lmp_ver=%02x "
2411 "lmp_subver=%04x", hdev->name, ver->hci_ver, ver->hci_rev,
2412 ver->lmp_ver, ver->lmp_subver);
2413 kfree_skb(skb);
2414
2415 /* Start Download */
2416 skb = __hci_cmd_sync(hdev, 0xfc2e, 0, NULL, HCI_INIT_TIMEOUT);
2417 if (IS_ERR(skb)) {
2418 ret = PTR_ERR(skb);
2419 BT_ERR("%s: BCM: Download Minidrv command failed (%ld)",
Marcel Holtmann89e75332014-09-16 04:44:50 +02002420 hdev->name, ret);
Petri Gynther10d4c672014-05-08 15:50:01 -07002421 goto reset_fw;
2422 }
2423 kfree_skb(skb);
2424
2425 /* 50 msec delay after Download Minidrv completes */
2426 msleep(50);
2427
2428 fw_ptr = fw->data;
2429 fw_size = fw->size;
2430
2431 while (fw_size >= sizeof(*cmd)) {
Marcel Holtmann89e75332014-09-16 04:44:50 +02002432 cmd = (struct hci_command_hdr *)fw_ptr;
Petri Gynther10d4c672014-05-08 15:50:01 -07002433 fw_ptr += sizeof(*cmd);
2434 fw_size -= sizeof(*cmd);
2435
2436 if (fw_size < cmd->plen) {
2437 BT_ERR("%s: BCM: patch %s is corrupted",
Marcel Holtmann89e75332014-09-16 04:44:50 +02002438 hdev->name, fw_name);
Petri Gynther10d4c672014-05-08 15:50:01 -07002439 ret = -EINVAL;
2440 goto reset_fw;
2441 }
2442
2443 cmd_param = fw_ptr;
2444 fw_ptr += cmd->plen;
2445 fw_size -= cmd->plen;
2446
2447 opcode = le16_to_cpu(cmd->opcode);
2448
2449 skb = __hci_cmd_sync(hdev, opcode, cmd->plen, cmd_param,
2450 HCI_INIT_TIMEOUT);
2451 if (IS_ERR(skb)) {
2452 ret = PTR_ERR(skb);
2453 BT_ERR("%s: BCM: patch command %04x failed (%ld)",
Marcel Holtmann89e75332014-09-16 04:44:50 +02002454 hdev->name, opcode, ret);
Petri Gynther10d4c672014-05-08 15:50:01 -07002455 goto reset_fw;
2456 }
2457 kfree_skb(skb);
2458 }
2459
2460 /* 250 msec delay after Launch Ram completes */
2461 msleep(250);
2462
2463reset_fw:
2464 /* Reset */
2465 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
2466 if (IS_ERR(skb)) {
2467 ret = PTR_ERR(skb);
2468 BT_ERR("%s: HCI_OP_RESET failed (%ld)", hdev->name, ret);
2469 goto done;
2470 }
2471 kfree_skb(skb);
2472
2473 /* Read Local Version Info */
2474 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
2475 HCI_INIT_TIMEOUT);
2476 if (IS_ERR(skb)) {
2477 ret = PTR_ERR(skb);
2478 BT_ERR("%s: HCI_OP_READ_LOCAL_VERSION failed (%ld)",
Marcel Holtmann89e75332014-09-16 04:44:50 +02002479 hdev->name, ret);
Petri Gynther10d4c672014-05-08 15:50:01 -07002480 goto done;
2481 }
2482
2483 if (skb->len != sizeof(*ver)) {
2484 BT_ERR("%s: HCI_OP_READ_LOCAL_VERSION event length mismatch",
Marcel Holtmann89e75332014-09-16 04:44:50 +02002485 hdev->name);
Petri Gynther10d4c672014-05-08 15:50:01 -07002486 kfree_skb(skb);
2487 ret = -EIO;
2488 goto done;
2489 }
2490
Marcel Holtmann89e75332014-09-16 04:44:50 +02002491 ver = (struct hci_rp_read_local_version *)skb->data;
Petri Gynther10d4c672014-05-08 15:50:01 -07002492 BT_INFO("%s: BCM: firmware hci_ver=%02x hci_rev=%04x lmp_ver=%02x "
2493 "lmp_subver=%04x", hdev->name, ver->hci_ver, ver->hci_rev,
2494 ver->lmp_ver, ver->lmp_subver);
2495 kfree_skb(skb);
2496
Marcel Holtmannc8abb732014-07-02 12:38:22 +02002497 /* Read BD Address */
2498 skb = __hci_cmd_sync(hdev, HCI_OP_READ_BD_ADDR, 0, NULL,
2499 HCI_INIT_TIMEOUT);
2500 if (IS_ERR(skb)) {
2501 ret = PTR_ERR(skb);
2502 BT_ERR("%s: HCI_OP_READ_BD_ADDR failed (%ld)",
Marcel Holtmann89e75332014-09-16 04:44:50 +02002503 hdev->name, ret);
Marcel Holtmannc8abb732014-07-02 12:38:22 +02002504 goto done;
2505 }
2506
2507 if (skb->len != sizeof(*bda)) {
2508 BT_ERR("%s: HCI_OP_READ_BD_ADDR event length mismatch",
Marcel Holtmann89e75332014-09-16 04:44:50 +02002509 hdev->name);
Marcel Holtmannc8abb732014-07-02 12:38:22 +02002510 kfree_skb(skb);
2511 ret = -EIO;
2512 goto done;
2513 }
2514
Marcel Holtmann89e75332014-09-16 04:44:50 +02002515 bda = (struct hci_rp_read_bd_addr *)skb->data;
Marcel Holtmannc8abb732014-07-02 12:38:22 +02002516 if (bda->status) {
2517 BT_ERR("%s: HCI_OP_READ_BD_ADDR error status (%02x)",
2518 hdev->name, bda->status);
2519 kfree_skb(skb);
2520 ret = -bt_to_errno(bda->status);
2521 goto done;
2522 }
2523
2524 /* The address 00:20:70:02:A0:00 indicates a BCM20702A0 controller
2525 * with no configured address.
2526 */
Marcel Holtmann849e5082014-07-04 16:54:39 +02002527 if (!bacmp(&bda->bdaddr, BDADDR_BCM20702A0)) {
Marcel Holtmannc8abb732014-07-02 12:38:22 +02002528 BT_INFO("%s: BCM: using default device address (%pMR)",
2529 hdev->name, &bda->bdaddr);
Marcel Holtmann849e5082014-07-04 16:54:39 +02002530 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
2531 }
Marcel Holtmannc8abb732014-07-02 12:38:22 +02002532
2533 kfree_skb(skb);
2534
Petri Gynther10d4c672014-05-08 15:50:01 -07002535done:
2536 release_firmware(fw);
2537
2538 return ret;
2539}
2540
Marcel Holtmannabbaf502014-07-02 00:53:48 +02002541static int btusb_set_bdaddr_bcm(struct hci_dev *hdev, const bdaddr_t *bdaddr)
2542{
2543 struct sk_buff *skb;
2544 long ret;
2545
2546 skb = __hci_cmd_sync(hdev, 0xfc01, 6, bdaddr, HCI_INIT_TIMEOUT);
2547 if (IS_ERR(skb)) {
2548 ret = PTR_ERR(skb);
2549 BT_ERR("%s: BCM: Change address command failed (%ld)",
Marcel Holtmann89e75332014-09-16 04:44:50 +02002550 hdev->name, ret);
Marcel Holtmannabbaf502014-07-02 00:53:48 +02002551 return ret;
2552 }
2553 kfree_skb(skb);
2554
2555 return 0;
2556}
2557
Toshi Kikuchi58592232014-12-12 10:58:05 -08002558static int btusb_set_bdaddr_ath3012(struct hci_dev *hdev,
2559 const bdaddr_t *bdaddr)
2560{
2561 struct sk_buff *skb;
2562 u8 buf[10];
2563 long ret;
2564
2565 buf[0] = 0x01;
2566 buf[1] = 0x01;
2567 buf[2] = 0x00;
2568 buf[3] = sizeof(bdaddr_t);
2569 memcpy(buf + 4, bdaddr, sizeof(bdaddr_t));
2570
2571 skb = __hci_cmd_sync(hdev, 0xfc0b, sizeof(buf), buf, HCI_INIT_TIMEOUT);
2572 if (IS_ERR(skb)) {
2573 ret = PTR_ERR(skb);
2574 BT_ERR("%s: Change address command failed (%ld)",
2575 hdev->name, ret);
2576 return ret;
2577 }
2578 kfree_skb(skb);
2579
2580 return 0;
2581}
2582
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002583static int btusb_probe(struct usb_interface *intf,
Marcel Holtmann89e75332014-09-16 04:44:50 +02002584 const struct usb_device_id *id)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002585{
2586 struct usb_endpoint_descriptor *ep_desc;
2587 struct btusb_data *data;
2588 struct hci_dev *hdev;
2589 int i, err;
2590
2591 BT_DBG("intf %p id %p", intf, id);
2592
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002593 /* interface numbers are hardcoded in the spec */
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002594 if (intf->cur_altsetting->desc.bInterfaceNumber != 0)
2595 return -ENODEV;
2596
2597 if (!id->driver_info) {
2598 const struct usb_device_id *match;
Marcel Holtmann89e75332014-09-16 04:44:50 +02002599
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002600 match = usb_match_id(intf, blacklist_table);
2601 if (match)
2602 id = match;
2603 }
2604
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002605 if (id->driver_info == BTUSB_IGNORE)
2606 return -ENODEV;
2607
Steven.Li2d25f8b2011-07-01 14:02:36 +08002608 if (id->driver_info & BTUSB_ATH3012) {
2609 struct usb_device *udev = interface_to_usbdev(intf);
2610
2611 /* Old firmware would otherwise let ath3k driver load
2612 * patch and sysconfig files */
2613 if (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x0001)
2614 return -ENODEV;
2615 }
2616
Sachin Kamat98921db2012-07-27 12:38:39 +05302617 data = devm_kzalloc(&intf->dev, sizeof(*data), GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002618 if (!data)
2619 return -ENOMEM;
2620
2621 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
2622 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
2623
2624 if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
2625 data->intr_ep = ep_desc;
2626 continue;
2627 }
2628
2629 if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
2630 data->bulk_tx_ep = ep_desc;
2631 continue;
2632 }
2633
2634 if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
2635 data->bulk_rx_ep = ep_desc;
2636 continue;
2637 }
2638 }
2639
Sachin Kamat98921db2012-07-27 12:38:39 +05302640 if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002641 return -ENODEV;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002642
Marcel Holtmann893ba542015-01-28 20:27:34 -08002643 if (id->driver_info & BTUSB_AMP) {
2644 data->cmdreq_type = USB_TYPE_CLASS | 0x01;
2645 data->cmdreq = 0x2b;
2646 } else {
2647 data->cmdreq_type = USB_TYPE_CLASS;
2648 data->cmdreq = 0x00;
2649 }
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002650
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002651 data->udev = interface_to_usbdev(intf);
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02002652 data->intf = intf;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002653
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002654 INIT_WORK(&data->work, btusb_work);
Oliver Neukum7bee5492009-08-24 23:44:59 +02002655 INIT_WORK(&data->waker, btusb_waker);
Marcel Holtmann803b5832014-09-16 08:00:29 +02002656 init_usb_anchor(&data->deferred);
2657 init_usb_anchor(&data->tx_anchor);
Oliver Neukum7bee5492009-08-24 23:44:59 +02002658 spin_lock_init(&data->txlock);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002659
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002660 init_usb_anchor(&data->intr_anchor);
2661 init_usb_anchor(&data->bulk_anchor);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02002662 init_usb_anchor(&data->isoc_anchor);
Marcel Holtmann803b5832014-09-16 08:00:29 +02002663 spin_lock_init(&data->rxlock);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002664
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002665 if (id->driver_info & BTUSB_INTEL_NEW) {
2666 data->recv_event = btusb_recv_event_intel;
2667 data->recv_bulk = btusb_recv_bulk_intel;
2668 set_bit(BTUSB_BOOTLOADER, &data->flags);
2669 } else {
2670 data->recv_event = hci_recv_frame;
2671 data->recv_bulk = btusb_recv_bulk;
2672 }
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +01002673
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002674 hdev = hci_alloc_dev();
Sachin Kamat98921db2012-07-27 12:38:39 +05302675 if (!hdev)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002676 return -ENOMEM;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002677
Marcel Holtmannc13854c2010-02-08 15:27:07 +01002678 hdev->bus = HCI_USB;
David Herrmann155961e2012-02-09 21:58:32 +01002679 hci_set_drvdata(hdev, data);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002680
Marcel Holtmann893ba542015-01-28 20:27:34 -08002681 if (id->driver_info & BTUSB_AMP)
2682 hdev->dev_type = HCI_AMP;
2683 else
2684 hdev->dev_type = HCI_BREDR;
2685
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002686 data->hdev = hdev;
2687
2688 SET_HCIDEV_DEV(hdev, &intf->dev);
2689
Marcel Holtmann9f8f9622013-04-10 08:11:35 -07002690 hdev->open = btusb_open;
2691 hdev->close = btusb_close;
2692 hdev->flush = btusb_flush;
2693 hdev->send = btusb_send_frame;
2694 hdev->notify = btusb_notify;
2695
2696 if (id->driver_info & BTUSB_BCM92035)
2697 hdev->setup = btusb_setup_bcm92035;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002698
Marcel Holtmannabbaf502014-07-02 00:53:48 +02002699 if (id->driver_info & BTUSB_BCM_PATCHRAM) {
Petri Gynther10d4c672014-05-08 15:50:01 -07002700 hdev->setup = btusb_setup_bcm_patchram;
Marcel Holtmannabbaf502014-07-02 00:53:48 +02002701 hdev->set_bdaddr = btusb_set_bdaddr_bcm;
Marcel Holtmann27c3fbe2014-11-02 20:52:24 +01002702 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
Marcel Holtmannabbaf502014-07-02 00:53:48 +02002703 }
Petri Gynther10d4c672014-05-08 15:50:01 -07002704
Marcel Holtmanncb8d6592014-07-02 11:25:25 +02002705 if (id->driver_info & BTUSB_INTEL) {
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002706 hdev->setup = btusb_setup_intel;
Marcel Holtmanncb8d6592014-07-02 11:25:25 +02002707 hdev->set_bdaddr = btusb_set_bdaddr_intel;
Jakub Pawlowskic33fb9b2015-01-30 18:55:58 -08002708 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
Marcel Holtmanncb8d6592014-07-02 11:25:25 +02002709 }
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002710
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002711 if (id->driver_info & BTUSB_INTEL_NEW) {
2712 hdev->send = btusb_send_frame_intel;
2713 hdev->setup = btusb_setup_intel_new;
Marcel Holtmann385a7682015-01-28 11:09:56 -08002714 hdev->hw_error = btusb_hw_error_intel;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002715 hdev->set_bdaddr = btusb_set_bdaddr_intel;
2716 }
2717
Amitkumar Karwarae8df492014-07-18 14:47:06 -07002718 if (id->driver_info & BTUSB_MARVELL)
2719 hdev->set_bdaddr = btusb_set_bdaddr_marvell;
2720
Marcel Holtmann661cf882015-01-02 23:35:20 -08002721 if (id->driver_info & BTUSB_SWAVE) {
2722 set_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks);
Marcel Holtmannd57dbe72014-12-26 04:42:33 +01002723 set_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS, &hdev->quirks);
Marcel Holtmann661cf882015-01-02 23:35:20 -08002724 }
Marcel Holtmannd57dbe72014-12-26 04:42:33 +01002725
Marcel Holtmann40df7832014-07-06 13:29:58 +02002726 if (id->driver_info & BTUSB_INTEL_BOOT)
2727 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
2728
Jakub Pawlowski79f0c872015-01-29 10:38:34 -08002729 if (id->driver_info & BTUSB_ATH3012) {
Toshi Kikuchi58592232014-12-12 10:58:05 -08002730 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
Jakub Pawlowski79f0c872015-01-29 10:38:34 -08002731 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
2732 }
Toshi Kikuchi58592232014-12-12 10:58:05 -08002733
Marcel Holtmann893ba542015-01-28 20:27:34 -08002734 if (id->driver_info & BTUSB_AMP) {
2735 /* AMP controllers do not support SCO packets */
2736 data->isoc = NULL;
2737 } else {
2738 /* Interface numbers are hardcoded in the specification */
2739 data->isoc = usb_ifnum_to_if(data->udev, 1);
2740 }
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02002741
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002742 if (!reset)
Szymon Janca6c511c2012-05-23 12:35:46 +02002743 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002744
2745 if (force_scofix || id->driver_info & BTUSB_WRONG_SCO_MTU) {
2746 if (!disable_scofix)
2747 set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
2748 }
2749
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02002750 if (id->driver_info & BTUSB_BROKEN_ISOC)
2751 data->isoc = NULL;
2752
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002753 if (id->driver_info & BTUSB_DIGIANSWER) {
2754 data->cmdreq_type = USB_TYPE_VENDOR;
Szymon Janca6c511c2012-05-23 12:35:46 +02002755 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002756 }
2757
2758 if (id->driver_info & BTUSB_CSR) {
2759 struct usb_device *udev = data->udev;
Marcel Holtmann81cac642014-01-03 03:02:36 -08002760 u16 bcdDevice = le16_to_cpu(udev->descriptor.bcdDevice);
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002761
2762 /* Old firmware would otherwise execute USB reset */
Marcel Holtmann81cac642014-01-03 03:02:36 -08002763 if (bcdDevice < 0x117)
Szymon Janca6c511c2012-05-23 12:35:46 +02002764 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmann81cac642014-01-03 03:02:36 -08002765
2766 /* Fake CSR devices with broken commands */
2767 if (bcdDevice <= 0x100)
2768 hdev->setup = btusb_setup_csr;
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002769 }
2770
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002771 if (id->driver_info & BTUSB_SNIFFER) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02002772 struct usb_device *udev = data->udev;
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002773
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002774 /* New sniffer firmware has crippled HCI interface */
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002775 if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
2776 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
2777 }
2778
Marcel Holtmann3a5ef202014-07-06 14:53:54 +02002779 if (id->driver_info & BTUSB_INTEL_BOOT) {
2780 /* A bug in the bootloader causes that interrupt interface is
2781 * only enabled after receiving SetInterface(0, AltSetting=0).
2782 */
2783 err = usb_set_interface(data->udev, 0, 0);
2784 if (err < 0) {
2785 BT_ERR("failed to set interface 0, alt 0 %d", err);
2786 hci_free_dev(hdev);
2787 return err;
2788 }
2789 }
2790
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02002791 if (data->isoc) {
2792 err = usb_driver_claim_interface(&btusb_driver,
Marcel Holtmann89e75332014-09-16 04:44:50 +02002793 data->isoc, data);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02002794 if (err < 0) {
2795 hci_free_dev(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02002796 return err;
2797 }
2798 }
2799
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002800 err = hci_register_dev(hdev);
2801 if (err < 0) {
2802 hci_free_dev(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002803 return err;
2804 }
2805
2806 usb_set_intfdata(intf, data);
2807
2808 return 0;
2809}
2810
2811static void btusb_disconnect(struct usb_interface *intf)
2812{
2813 struct btusb_data *data = usb_get_intfdata(intf);
2814 struct hci_dev *hdev;
2815
2816 BT_DBG("intf %p", intf);
2817
2818 if (!data)
2819 return;
2820
2821 hdev = data->hdev;
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02002822 usb_set_intfdata(data->intf, NULL);
2823
2824 if (data->isoc)
2825 usb_set_intfdata(data->isoc, NULL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002826
2827 hci_unregister_dev(hdev);
2828
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02002829 if (intf == data->isoc)
2830 usb_driver_release_interface(&btusb_driver, data->intf);
2831 else if (data->isoc)
2832 usb_driver_release_interface(&btusb_driver, data->isoc);
2833
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002834 hci_free_dev(hdev);
2835}
2836
Oliver Neukum7bee5492009-08-24 23:44:59 +02002837#ifdef CONFIG_PM
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002838static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
2839{
2840 struct btusb_data *data = usb_get_intfdata(intf);
2841
2842 BT_DBG("intf %p", intf);
2843
2844 if (data->suspend_count++)
2845 return 0;
2846
Oliver Neukum7bee5492009-08-24 23:44:59 +02002847 spin_lock_irq(&data->txlock);
Alan Stern5b1b0b82011-08-19 23:49:48 +02002848 if (!(PMSG_IS_AUTO(message) && data->tx_in_flight)) {
Oliver Neukum7bee5492009-08-24 23:44:59 +02002849 set_bit(BTUSB_SUSPENDING, &data->flags);
2850 spin_unlock_irq(&data->txlock);
2851 } else {
2852 spin_unlock_irq(&data->txlock);
2853 data->suspend_count--;
2854 return -EBUSY;
2855 }
2856
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002857 cancel_work_sync(&data->work);
2858
Oliver Neukum7bee5492009-08-24 23:44:59 +02002859 btusb_stop_traffic(data);
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002860 usb_kill_anchored_urbs(&data->tx_anchor);
2861
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002862 return 0;
2863}
2864
Oliver Neukum7bee5492009-08-24 23:44:59 +02002865static void play_deferred(struct btusb_data *data)
2866{
2867 struct urb *urb;
2868 int err;
2869
2870 while ((urb = usb_get_from_anchor(&data->deferred))) {
2871 err = usb_submit_urb(urb, GFP_ATOMIC);
2872 if (err < 0)
2873 break;
2874
2875 data->tx_in_flight++;
2876 }
2877 usb_scuttle_anchored_urbs(&data->deferred);
2878}
2879
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002880static int btusb_resume(struct usb_interface *intf)
2881{
2882 struct btusb_data *data = usb_get_intfdata(intf);
2883 struct hci_dev *hdev = data->hdev;
Oliver Neukum7bee5492009-08-24 23:44:59 +02002884 int err = 0;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002885
2886 BT_DBG("intf %p", intf);
2887
2888 if (--data->suspend_count)
2889 return 0;
2890
2891 if (!test_bit(HCI_RUNNING, &hdev->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +02002892 goto done;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002893
2894 if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
2895 err = btusb_submit_intr_urb(hdev, GFP_NOIO);
2896 if (err < 0) {
2897 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02002898 goto failed;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002899 }
2900 }
2901
2902 if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) {
Marcel Holtmann43c2e572009-02-04 17:41:38 +01002903 err = btusb_submit_bulk_urb(hdev, GFP_NOIO);
2904 if (err < 0) {
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002905 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02002906 goto failed;
2907 }
2908
2909 btusb_submit_bulk_urb(hdev, GFP_NOIO);
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002910 }
2911
2912 if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
2913 if (btusb_submit_isoc_urb(hdev, GFP_NOIO) < 0)
2914 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
2915 else
2916 btusb_submit_isoc_urb(hdev, GFP_NOIO);
2917 }
2918
Oliver Neukum7bee5492009-08-24 23:44:59 +02002919 spin_lock_irq(&data->txlock);
2920 play_deferred(data);
2921 clear_bit(BTUSB_SUSPENDING, &data->flags);
2922 spin_unlock_irq(&data->txlock);
2923 schedule_work(&data->work);
2924
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002925 return 0;
Oliver Neukum7bee5492009-08-24 23:44:59 +02002926
2927failed:
2928 usb_scuttle_anchored_urbs(&data->deferred);
2929done:
2930 spin_lock_irq(&data->txlock);
2931 clear_bit(BTUSB_SUSPENDING, &data->flags);
2932 spin_unlock_irq(&data->txlock);
2933
2934 return err;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002935}
Oliver Neukum7bee5492009-08-24 23:44:59 +02002936#endif
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002937
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002938static struct usb_driver btusb_driver = {
2939 .name = "btusb",
2940 .probe = btusb_probe,
2941 .disconnect = btusb_disconnect,
Oliver Neukum7bee5492009-08-24 23:44:59 +02002942#ifdef CONFIG_PM
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002943 .suspend = btusb_suspend,
2944 .resume = btusb_resume,
Oliver Neukum7bee5492009-08-24 23:44:59 +02002945#endif
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002946 .id_table = btusb_table,
Oliver Neukum7bee5492009-08-24 23:44:59 +02002947 .supports_autosuspend = 1,
Sarah Sharpe1f12eb2012-04-23 10:08:51 -07002948 .disable_hub_initiated_lpm = 1,
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002949};
2950
Greg Kroah-Hartman93f15082011-11-18 09:47:34 -08002951module_usb_driver(btusb_driver);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002952
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002953module_param(disable_scofix, bool, 0644);
2954MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
2955
2956module_param(force_scofix, bool, 0644);
2957MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
2958
2959module_param(reset, bool, 0644);
2960MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
2961
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002962MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
2963MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
2964MODULE_VERSION(VERSION);
2965MODULE_LICENSE("GPL");