blob: 9bf4d6ae6c6be17f28396dc2eae3d311a105b34b [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 Holtmann17b27722015-03-22 15:52:38 +010027#include <asm/unaligned.h>
Marcel Holtmann5e23b922007-10-20 14:12:34 +020028
29#include <net/bluetooth/bluetooth.h>
30#include <net/bluetooth/hci_core.h>
31
Marcel Holtmanncda0dd72015-01-26 21:33:48 -080032#define VERSION "0.7"
Marcel Holtmanncfeb4142008-08-07 22:26:56 +020033
Rusty Russell90ab5ee2012-01-13 09:32:20 +103034static bool disable_scofix;
35static bool force_scofix;
Marcel Holtmann7a9d4022008-11-30 12:17:26 +010036
Rusty Russell90ab5ee2012-01-13 09:32:20 +103037static bool reset = 1;
Marcel Holtmanncfeb4142008-08-07 22:26:56 +020038
39static struct usb_driver btusb_driver;
40
41#define BTUSB_IGNORE 0x01
Marcel Holtmann7a9d4022008-11-30 12:17:26 +010042#define BTUSB_DIGIANSWER 0x02
43#define BTUSB_CSR 0x04
44#define BTUSB_SNIFFER 0x08
45#define BTUSB_BCM92035 0x10
46#define BTUSB_BROKEN_ISOC 0x20
47#define BTUSB_WRONG_SCO_MTU 0x40
Steven.Li2d25f8b2011-07-01 14:02:36 +080048#define BTUSB_ATH3012 0x80
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -070049#define BTUSB_INTEL 0x100
Marcel Holtmann40df7832014-07-06 13:29:58 +020050#define BTUSB_INTEL_BOOT 0x200
51#define BTUSB_BCM_PATCHRAM 0x400
Amitkumar Karwarae8df492014-07-18 14:47:06 -070052#define BTUSB_MARVELL 0x800
Marcel Holtmann4fcef8e2015-01-01 17:34:37 -080053#define BTUSB_SWAVE 0x1000
Marcel Holtmanncda0dd72015-01-26 21:33:48 -080054#define BTUSB_INTEL_NEW 0x2000
Marcel Holtmann893ba542015-01-28 20:27:34 -080055#define BTUSB_AMP 0x4000
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +000056#define BTUSB_QCA_ROME 0x8000
Marcel Holtmann17b27722015-03-22 15:52:38 +010057#define BTUSB_BCM_APPLE 0x10000
Marcel Holtmann5e23b922007-10-20 14:12:34 +020058
Marcel Holtmann54265202013-10-11 07:46:18 -070059static const struct usb_device_id btusb_table[] = {
Marcel Holtmann5e23b922007-10-20 14:12:34 +020060 /* Generic Bluetooth USB device */
61 { USB_DEVICE_INFO(0xe0, 0x01, 0x01) },
62
Marcel Holtmann893ba542015-01-28 20:27:34 -080063 /* Generic Bluetooth AMP device */
64 { USB_DEVICE_INFO(0xe0, 0x01, 0x04), .driver_info = BTUSB_AMP },
65
Henrik Rydberg1fa65352012-08-25 19:28:06 +020066 /* Apple-specific (Broadcom) devices */
Marcel Holtmann17b27722015-03-22 15:52:38 +010067 { USB_VENDOR_AND_INTERFACE_INFO(0x05ac, 0xff, 0x01, 0x01),
68 .driver_info = BTUSB_BCM_APPLE },
Henrik Rydberg1fa65352012-08-25 19:28:06 +020069
Cho, Yu-Chen178c0592013-06-04 21:40:26 +080070 /* MediaTek MT76x0E */
71 { USB_DEVICE(0x0e8d, 0x763f) },
72
Oliver Neukumc510eae2011-09-21 11:41:45 +020073 /* Broadcom SoftSailing reporting vendor specific */
Don Zickus2e8b50632012-03-28 16:41:11 -040074 { USB_DEVICE(0x0a5c, 0x21e1) },
Oliver Neukumc510eae2011-09-21 11:41:45 +020075
Nobuhiro Iwamatsu3cd01972010-08-20 16:24:07 +090076 /* Apple MacBookPro 7,1 */
77 { USB_DEVICE(0x05ac, 0x8213) },
78
Cyril Lacoux0a79f672010-07-14 10:29:27 +040079 /* Apple iMac11,1 */
80 { USB_DEVICE(0x05ac, 0x8215) },
81
Nobuhiro Iwamatsu9c047152010-08-20 16:24:06 +090082 /* Apple MacBookPro6,2 */
83 { USB_DEVICE(0x05ac, 0x8218) },
84
Edgar (gimli) Hucek3e3ede72010-11-04 08:04:33 +010085 /* Apple MacBookAir3,1, MacBookAir3,2 */
86 { USB_DEVICE(0x05ac, 0x821b) },
87
Pieter-Augustijn Van Malleghema63b7232011-09-07 02:28:10 -040088 /* Apple MacBookAir4,1 */
89 { USB_DEVICE(0x05ac, 0x821f) },
90
Marc-Antoine Perennou88d377b2011-03-24 14:51:21 -030091 /* Apple MacBookPro8,2 */
92 { USB_DEVICE(0x05ac, 0x821a) },
93
Jurgen Kramerf78b68262011-09-04 18:01:42 +020094 /* Apple MacMini5,1 */
95 { USB_DEVICE(0x05ac, 0x8281) },
96
Marcel Holtmanncfeb4142008-08-07 22:26:56 +020097 /* AVM BlueFRITZ! USB v2.0 */
Marcel Holtmann4fcef8e2015-01-01 17:34:37 -080098 { USB_DEVICE(0x057c, 0x3800), .driver_info = BTUSB_SWAVE },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +020099
100 /* Bluetooth Ultraport Module from IBM */
101 { USB_DEVICE(0x04bf, 0x030a) },
102
103 /* ALPS Modules with non-standard id */
104 { USB_DEVICE(0x044e, 0x3001) },
105 { USB_DEVICE(0x044e, 0x3002) },
106
107 /* Ericsson with non-standard id */
108 { USB_DEVICE(0x0bdb, 0x1002) },
109
110 /* Canyon CN-BTU1 with HID interfaces */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100111 { USB_DEVICE(0x0c10, 0x0000) },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200112
Wen-chien Jesse Sungd13431c2011-11-08 14:30:22 +0800113 /* Broadcom BCM20702A0 */
Andy Shevchenko0b880062014-02-18 18:26:19 +0200114 { USB_DEVICE(0x0489, 0xe042) },
115 { USB_DEVICE(0x04ca, 0x2003) },
Jeff Cook1ee3ff62012-11-09 16:39:48 -0700116 { USB_DEVICE(0x0b05, 0x17b5) },
Raphael Kubo da Costa38a172b2013-09-02 14:57:51 +0300117 { USB_DEVICE(0x0b05, 0x17cb) },
Wen-chien Jesse Sungd13431c2011-11-08 14:30:22 +0800118 { USB_DEVICE(0x413c, 0x8197) },
Fabio Ka86c02e2014-11-18 00:46:28 -0200119 { USB_DEVICE(0x13d3, 0x3404),
120 .driver_info = BTUSB_BCM_PATCHRAM },
Wen-chien Jesse Sungd13431c2011-11-08 14:30:22 +0800121
Marcel Holtmannd049f4e2015-01-26 20:35:32 -0800122 /* Broadcom BCM20702B0 (Dynex/Insignia) */
123 { USB_DEVICE(0x19ff, 0x0239), .driver_info = BTUSB_BCM_PATCHRAM },
124
Steven Harms98514032012-04-13 14:45:55 -0400125 /* Foxconn - Hon Hai */
Heinrich Siebmanns6029ddc2014-12-03 19:32:22 +0100126 { USB_VENDOR_AND_INTERFACE_INFO(0x0489, 0xff, 0x01, 0x01),
127 .driver_info = BTUSB_BCM_PATCHRAM },
Steven Harms98514032012-04-13 14:45:55 -0400128
Matej Dubovy8f0c3042015-02-02 18:50:14 +0100129 /* Lite-On Technology - Broadcom based */
130 { USB_VENDOR_AND_INTERFACE_INFO(0x04ca, 0xff, 0x01, 0x01),
131 .driver_info = BTUSB_BCM_PATCHRAM },
132
Andy Shevchenko0b880062014-02-18 18:26:19 +0200133 /* Broadcom devices with vendor specific id */
Petri Gynther10d4c672014-05-08 15:50:01 -0700134 { USB_VENDOR_AND_INTERFACE_INFO(0x0a5c, 0xff, 0x01, 0x01),
135 .driver_info = BTUSB_BCM_PATCHRAM },
Gustavo Padovan92c385f2012-08-06 15:36:49 -0300136
Marcel Holtmannc2aef6e2014-07-21 14:02:33 +0200137 /* ASUSTek Computer - Broadcom based */
Rick Dunn9a5abda2015-01-17 05:29:12 +0100138 { USB_VENDOR_AND_INTERFACE_INFO(0x0b05, 0xff, 0x01, 0x01),
139 .driver_info = BTUSB_BCM_PATCHRAM },
Marcel Holtmannc2aef6e2014-07-21 14:02:33 +0200140
Ken O'Brien5bcecf32013-09-21 19:14:43 +0100141 /* Belkin F8065bf - Broadcom based */
142 { USB_VENDOR_AND_INTERFACE_INFO(0x050d, 0xff, 0x01, 0x01) },
143
Jurgen Kramer9113bfd2014-02-15 12:01:09 +0100144 /* IMC Networks - Broadcom based */
145 { USB_VENDOR_AND_INTERFACE_INFO(0x13d3, 0xff, 0x01, 0x01) },
146
Marcel Holtmann40df7832014-07-06 13:29:58 +0200147 /* Intel Bluetooth USB Bootloader (RAM module) */
Marcel Holtmannd92f2df2014-07-06 14:53:55 +0200148 { USB_DEVICE(0x8087, 0x0a5a),
149 .driver_info = BTUSB_INTEL_BOOT | BTUSB_BROKEN_ISOC },
Marcel Holtmann40df7832014-07-06 13:29:58 +0200150
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200151 { } /* Terminating entry */
152};
153
154MODULE_DEVICE_TABLE(usb, btusb_table);
155
Marcel Holtmann54265202013-10-11 07:46:18 -0700156static const struct usb_device_id blacklist_table[] = {
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200157 /* CSR BlueCore devices */
158 { USB_DEVICE(0x0a12, 0x0001), .driver_info = BTUSB_CSR },
159
160 /* Broadcom BCM2033 without firmware */
161 { USB_DEVICE(0x0a5c, 0x2033), .driver_info = BTUSB_IGNORE },
162
Bala Shanmugambe931122010-11-26 17:35:46 +0530163 /* Atheros 3011 with sflash firmware */
Andy Shevchenko0b880062014-02-18 18:26:19 +0200164 { USB_DEVICE(0x0489, 0xe027), .driver_info = BTUSB_IGNORE },
165 { USB_DEVICE(0x0489, 0xe03d), .driver_info = BTUSB_IGNORE },
Alexander Ploumistos2eeff0b2015-02-13 21:05:11 +0200166 { USB_DEVICE(0x04f2, 0xaff1), .driver_info = BTUSB_IGNORE },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200167 { USB_DEVICE(0x0930, 0x0215), .driver_info = BTUSB_IGNORE },
Bala Shanmugambe931122010-11-26 17:35:46 +0530168 { USB_DEVICE(0x0cf3, 0x3002), .driver_info = BTUSB_IGNORE },
Marek Vasut6eda5412012-06-08 14:32:50 +0200169 { USB_DEVICE(0x0cf3, 0xe019), .driver_info = BTUSB_IGNORE },
Andy Ross2a7bccc2011-05-09 16:11:16 -0700170 { USB_DEVICE(0x13d3, 0x3304), .driver_info = BTUSB_IGNORE },
Bala Shanmugambe931122010-11-26 17:35:46 +0530171
Cho, Yu-Chen509e7862011-01-26 17:10:59 +0800172 /* Atheros AR9285 Malbec with sflash firmware */
173 { USB_DEVICE(0x03f0, 0x311d), .driver_info = BTUSB_IGNORE },
174
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530175 /* Atheros 3012 with sflash firmware */
Andy Shevchenko0b880062014-02-18 18:26:19 +0200176 { USB_DEVICE(0x0489, 0xe04d), .driver_info = BTUSB_ATH3012 },
177 { USB_DEVICE(0x0489, 0xe04e), .driver_info = BTUSB_ATH3012 },
178 { USB_DEVICE(0x0489, 0xe056), .driver_info = BTUSB_ATH3012 },
179 { USB_DEVICE(0x0489, 0xe057), .driver_info = BTUSB_ATH3012 },
180 { USB_DEVICE(0x0489, 0xe05f), .driver_info = BTUSB_ATH3012 },
Anantha Krishnan4b552bc2014-10-06 16:31:49 +0530181 { USB_DEVICE(0x0489, 0xe078), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200182 { USB_DEVICE(0x04c5, 0x1330), .driver_info = BTUSB_ATH3012 },
183 { USB_DEVICE(0x04ca, 0x3004), .driver_info = BTUSB_ATH3012 },
184 { USB_DEVICE(0x04ca, 0x3005), .driver_info = BTUSB_ATH3012 },
185 { USB_DEVICE(0x04ca, 0x3006), .driver_info = BTUSB_ATH3012 },
Mohammed Habibulla1fb4e092014-04-17 11:37:13 -0700186 { USB_DEVICE(0x04ca, 0x3007), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200187 { USB_DEVICE(0x04ca, 0x3008), .driver_info = BTUSB_ATH3012 },
188 { USB_DEVICE(0x04ca, 0x300b), .driver_info = BTUSB_ATH3012 },
Janne Heikkinen134d3b32014-12-09 07:44:51 +0200189 { USB_DEVICE(0x04ca, 0x3010), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200190 { USB_DEVICE(0x0930, 0x0219), .driver_info = BTUSB_ATH3012 },
191 { USB_DEVICE(0x0930, 0x0220), .driver_info = BTUSB_ATH3012 },
Vincent Zwanenburg89d29752014-08-08 12:33:56 +0100192 { USB_DEVICE(0x0930, 0x0227), .driver_info = BTUSB_ATH3012 },
Andy Shevchenkoa735f9e2014-02-18 18:26:20 +0200193 { USB_DEVICE(0x0b05, 0x17d0), .driver_info = BTUSB_ATH3012 },
Ming Leid66629c2013-03-15 11:00:39 +0800194 { USB_DEVICE(0x0cf3, 0x0036), .driver_info = BTUSB_ATH3012 },
Steven.Li2d25f8b2011-07-01 14:02:36 +0800195 { USB_DEVICE(0x0cf3, 0x3004), .driver_info = BTUSB_ATH3012 },
Sunguk Lee94a32d102013-03-12 04:41:58 +0900196 { USB_DEVICE(0x0cf3, 0x3008), .driver_info = BTUSB_ATH3012 },
Cho, Yu-Chen07c0ea82012-03-14 22:01:21 +0200197 { USB_DEVICE(0x0cf3, 0x311d), .driver_info = BTUSB_ATH3012 },
Oliver Neukumb1312372014-01-16 15:37:11 +0100198 { USB_DEVICE(0x0cf3, 0x311e), .driver_info = BTUSB_ATH3012 },
Oliver Neukum1e56f1e2014-01-16 16:02:58 +0100199 { USB_DEVICE(0x0cf3, 0x311f), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200200 { USB_DEVICE(0x0cf3, 0x3121), .driver_info = BTUSB_ATH3012 },
Ming Leiebaf5792013-03-18 23:45:11 +0800201 { USB_DEVICE(0x0cf3, 0x817a), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200202 { USB_DEVICE(0x0cf3, 0xe003), .driver_info = BTUSB_ATH3012 },
AceLan Kaoac713112012-04-19 14:53:45 +0800203 { USB_DEVICE(0x0cf3, 0xe004), .driver_info = BTUSB_ATH3012 },
Peng Chen0a3658c2013-08-30 17:41:40 +0800204 { USB_DEVICE(0x0cf3, 0xe005), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200205 { USB_DEVICE(0x13d3, 0x3362), .driver_info = BTUSB_ATH3012 },
206 { USB_DEVICE(0x13d3, 0x3375), .driver_info = BTUSB_ATH3012 },
AceLan Kaoeed307e2012-12-11 11:41:20 +0800207 { USB_DEVICE(0x13d3, 0x3393), .driver_info = BTUSB_ATH3012 },
Sujith Manoharan5b77a1f2013-07-15 09:29:03 +0530208 { USB_DEVICE(0x13d3, 0x3402), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin3bb30a7c2014-11-25 20:19:52 +0300209 { USB_DEVICE(0x13d3, 0x3408), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin033efa92015-01-18 00:16:51 +0300210 { USB_DEVICE(0x13d3, 0x3423), .driver_info = BTUSB_ATH3012 },
Anantha Krishnanfa2f1392014-07-08 19:25:08 +0530211 { USB_DEVICE(0x13d3, 0x3432), .driver_info = BTUSB_ATH3012 },
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530212
Cho, Yu-Chene9036e32011-02-15 10:20:07 +0800213 /* Atheros AR5BBU12 with sflash firmware */
214 { USB_DEVICE(0x0489, 0xe02c), .driver_info = BTUSB_IGNORE },
215
Michael Gruetzner85d59722012-05-02 22:33:40 +0200216 /* Atheros AR5BBU12 with sflash firmware */
Yevgeniy Melnichukbc21fde2012-08-07 19:48:10 +0530217 { USB_DEVICE(0x0489, 0xe036), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200218 { USB_DEVICE(0x0489, 0xe03c), .driver_info = BTUSB_ATH3012 },
Michael Gruetzner85d59722012-05-02 22:33:40 +0200219
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +0000220 /* QCA ROME chipset */
Marcel Holtmannc9e44472015-03-16 23:56:04 -0700221 { USB_DEVICE(0x0cf3, 0xe300), .driver_info = BTUSB_QCA_ROME },
222 { USB_DEVICE(0x0cf3, 0xe360), .driver_info = BTUSB_QCA_ROME },
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +0000223
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200224 /* Broadcom BCM2035 */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100225 { USB_DEVICE(0x0a5c, 0x2009), .driver_info = BTUSB_BCM92035 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200226 { USB_DEVICE(0x0a5c, 0x200a), .driver_info = BTUSB_WRONG_SCO_MTU },
227 { USB_DEVICE(0x0a5c, 0x2035), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200228
229 /* Broadcom BCM2045 */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100230 { USB_DEVICE(0x0a5c, 0x2039), .driver_info = BTUSB_WRONG_SCO_MTU },
231 { USB_DEVICE(0x0a5c, 0x2101), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmannbdbef3d2008-09-23 00:16:35 +0200232
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200233 /* IBM/Lenovo ThinkPad with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100234 { USB_DEVICE(0x0a5c, 0x201e), .driver_info = BTUSB_WRONG_SCO_MTU },
235 { USB_DEVICE(0x0a5c, 0x2110), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200236
237 /* HP laptop with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100238 { USB_DEVICE(0x03f0, 0x171d), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200239
240 /* Dell laptop with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100241 { USB_DEVICE(0x413c, 0x8126), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200242
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100243 /* Dell Wireless 370 and 410 devices */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100244 { USB_DEVICE(0x413c, 0x8152), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100245 { USB_DEVICE(0x413c, 0x8156), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200246
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100247 /* Belkin F8T012 and F8T013 devices */
248 { USB_DEVICE(0x050d, 0x0012), .driver_info = BTUSB_WRONG_SCO_MTU },
249 { USB_DEVICE(0x050d, 0x0013), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200250
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100251 /* Asus WL-BTD202 device */
252 { USB_DEVICE(0x0b05, 0x1715), .driver_info = BTUSB_WRONG_SCO_MTU },
253
254 /* Kensington Bluetooth USB adapter */
255 { USB_DEVICE(0x047d, 0x105e), .driver_info = BTUSB_WRONG_SCO_MTU },
256
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200257 /* RTX Telecom based adapters with buggy SCO support */
258 { USB_DEVICE(0x0400, 0x0807), .driver_info = BTUSB_BROKEN_ISOC },
259 { USB_DEVICE(0x0400, 0x080a), .driver_info = BTUSB_BROKEN_ISOC },
260
261 /* CONWISE Technology based adapters with buggy SCO support */
262 { USB_DEVICE(0x0e5e, 0x6622), .driver_info = BTUSB_BROKEN_ISOC },
263
Marcel Holtmann4fcef8e2015-01-01 17:34:37 -0800264 /* Roper Class 1 Bluetooth Dongle (Silicon Wave based) */
265 { USB_DEVICE(0x1300, 0x0001), .driver_info = BTUSB_SWAVE },
266
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200267 /* Digianswer devices */
268 { USB_DEVICE(0x08fd, 0x0001), .driver_info = BTUSB_DIGIANSWER },
269 { USB_DEVICE(0x08fd, 0x0002), .driver_info = BTUSB_IGNORE },
270
271 /* CSR BlueCore Bluetooth Sniffer */
Marcel Holtmann4f64fa82014-07-07 00:12:04 +0200272 { USB_DEVICE(0x0a12, 0x0002),
273 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200274
275 /* Frontline ComProbe Bluetooth Sniffer */
Marcel Holtmann4f64fa82014-07-07 00:12:04 +0200276 { USB_DEVICE(0x16d3, 0x0002),
277 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200278
Marcel Holtmanncb1ee892015-01-28 19:41:42 -0800279 /* Marvell Bluetooth devices */
280 { USB_DEVICE(0x1286, 0x2044), .driver_info = BTUSB_MARVELL },
281 { USB_DEVICE(0x1286, 0x2046), .driver_info = BTUSB_MARVELL },
282
Marcel Holtmannd0ac9eb2015-01-28 19:41:43 -0800283 /* Intel Bluetooth devices */
Marcel Holtmann407550f2015-02-22 15:41:18 -0800284 { USB_DEVICE(0x8087, 0x07da), .driver_info = BTUSB_CSR },
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -0700285 { USB_DEVICE(0x8087, 0x07dc), .driver_info = BTUSB_INTEL },
Tedd Ho-Jeong Anef4e5e42013-11-12 13:10:58 -0800286 { USB_DEVICE(0x8087, 0x0a2a), .driver_info = BTUSB_INTEL },
Marcel Holtmanncda0dd72015-01-26 21:33:48 -0800287 { USB_DEVICE(0x8087, 0x0a2b), .driver_info = BTUSB_INTEL_NEW },
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -0700288
Marcel Holtmannd0ac9eb2015-01-28 19:41:43 -0800289 /* Other Intel Bluetooth devices */
290 { USB_VENDOR_AND_INTERFACE_INFO(0x8087, 0xe0, 0x01, 0x01),
291 .driver_info = BTUSB_IGNORE },
Amitkumar Karwarae8df492014-07-18 14:47:06 -0700292
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200293 { } /* Terminating entry */
294};
295
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200296#define BTUSB_MAX_ISOC_FRAMES 10
297
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200298#define BTUSB_INTR_RUNNING 0
299#define BTUSB_BULK_RUNNING 1
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200300#define BTUSB_ISOC_RUNNING 2
Oliver Neukum7bee5492009-08-24 23:44:59 +0200301#define BTUSB_SUSPENDING 3
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -0300302#define BTUSB_DID_ISO_RESUME 4
Marcel Holtmanncda0dd72015-01-26 21:33:48 -0800303#define BTUSB_BOOTLOADER 5
304#define BTUSB_DOWNLOADING 6
Marcel Holtmannce6bb922015-01-28 01:58:40 -0800305#define BTUSB_FIRMWARE_LOADED 7
Marcel Holtmanncda0dd72015-01-26 21:33:48 -0800306#define BTUSB_FIRMWARE_FAILED 8
Marcel Holtmannce6bb922015-01-28 01:58:40 -0800307#define BTUSB_BOOTING 9
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200308
309struct btusb_data {
310 struct hci_dev *hdev;
311 struct usb_device *udev;
Marcel Holtmann5fbcd262008-09-23 00:16:36 +0200312 struct usb_interface *intf;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200313 struct usb_interface *isoc;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200314
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200315 unsigned long flags;
316
317 struct work_struct work;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200318 struct work_struct waker;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200319
Marcel Holtmann803b5832014-09-16 08:00:29 +0200320 struct usb_anchor deferred;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200321 struct usb_anchor tx_anchor;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200322 int tx_in_flight;
323 spinlock_t txlock;
324
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200325 struct usb_anchor intr_anchor;
326 struct usb_anchor bulk_anchor;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200327 struct usb_anchor isoc_anchor;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200328 spinlock_t rxlock;
329
330 struct sk_buff *evt_skb;
331 struct sk_buff *acl_skb;
332 struct sk_buff *sco_skb;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200333
334 struct usb_endpoint_descriptor *intr_ep;
335 struct usb_endpoint_descriptor *bulk_tx_ep;
336 struct usb_endpoint_descriptor *bulk_rx_ep;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200337 struct usb_endpoint_descriptor *isoc_tx_ep;
338 struct usb_endpoint_descriptor *isoc_rx_ep;
339
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100340 __u8 cmdreq_type;
Marcel Holtmann893ba542015-01-28 20:27:34 -0800341 __u8 cmdreq;
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100342
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100343 unsigned int sco_num;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200344 int isoc_altsetting;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +0100345 int suspend_count;
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +0100346
Marcel Holtmann97307f52015-01-12 13:51:10 -0800347 int (*recv_event)(struct hci_dev *hdev, struct sk_buff *skb);
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +0100348 int (*recv_bulk)(struct btusb_data *data, void *buffer, int count);
Kim, Ben Young Taeace31982015-02-15 23:06:14 +0000349
350 int (*setup_on_usb)(struct hci_dev *hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200351};
352
Marcel Holtmann803b5832014-09-16 08:00:29 +0200353static inline void btusb_free_frags(struct btusb_data *data)
354{
355 unsigned long flags;
356
357 spin_lock_irqsave(&data->rxlock, flags);
358
359 kfree_skb(data->evt_skb);
360 data->evt_skb = NULL;
361
362 kfree_skb(data->acl_skb);
363 data->acl_skb = NULL;
364
365 kfree_skb(data->sco_skb);
366 data->sco_skb = NULL;
367
368 spin_unlock_irqrestore(&data->rxlock, flags);
369}
370
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200371static int btusb_recv_intr(struct btusb_data *data, void *buffer, int count)
372{
Marcel Holtmann803b5832014-09-16 08:00:29 +0200373 struct sk_buff *skb;
374 int err = 0;
375
376 spin_lock(&data->rxlock);
377 skb = data->evt_skb;
378
379 while (count) {
380 int len;
381
382 if (!skb) {
383 skb = bt_skb_alloc(HCI_MAX_EVENT_SIZE, GFP_ATOMIC);
384 if (!skb) {
385 err = -ENOMEM;
386 break;
387 }
388
389 bt_cb(skb)->pkt_type = HCI_EVENT_PKT;
390 bt_cb(skb)->expect = HCI_EVENT_HDR_SIZE;
391 }
392
393 len = min_t(uint, bt_cb(skb)->expect, count);
394 memcpy(skb_put(skb, len), buffer, len);
395
396 count -= len;
397 buffer += len;
398 bt_cb(skb)->expect -= len;
399
400 if (skb->len == HCI_EVENT_HDR_SIZE) {
401 /* Complete event header */
402 bt_cb(skb)->expect = hci_event_hdr(skb)->plen;
403
404 if (skb_tailroom(skb) < bt_cb(skb)->expect) {
405 kfree_skb(skb);
406 skb = NULL;
407
408 err = -EILSEQ;
409 break;
410 }
411 }
412
413 if (bt_cb(skb)->expect == 0) {
414 /* Complete frame */
Marcel Holtmann97307f52015-01-12 13:51:10 -0800415 data->recv_event(data->hdev, skb);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200416 skb = NULL;
417 }
418 }
419
420 data->evt_skb = skb;
421 spin_unlock(&data->rxlock);
422
423 return err;
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200424}
425
426static int btusb_recv_bulk(struct btusb_data *data, void *buffer, int count)
427{
Marcel Holtmann803b5832014-09-16 08:00:29 +0200428 struct sk_buff *skb;
429 int err = 0;
430
431 spin_lock(&data->rxlock);
432 skb = data->acl_skb;
433
434 while (count) {
435 int len;
436
437 if (!skb) {
438 skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
439 if (!skb) {
440 err = -ENOMEM;
441 break;
442 }
443
444 bt_cb(skb)->pkt_type = HCI_ACLDATA_PKT;
445 bt_cb(skb)->expect = HCI_ACL_HDR_SIZE;
446 }
447
448 len = min_t(uint, bt_cb(skb)->expect, count);
449 memcpy(skb_put(skb, len), buffer, len);
450
451 count -= len;
452 buffer += len;
453 bt_cb(skb)->expect -= len;
454
455 if (skb->len == HCI_ACL_HDR_SIZE) {
456 __le16 dlen = hci_acl_hdr(skb)->dlen;
457
458 /* Complete ACL header */
459 bt_cb(skb)->expect = __le16_to_cpu(dlen);
460
461 if (skb_tailroom(skb) < bt_cb(skb)->expect) {
462 kfree_skb(skb);
463 skb = NULL;
464
465 err = -EILSEQ;
466 break;
467 }
468 }
469
470 if (bt_cb(skb)->expect == 0) {
471 /* Complete frame */
472 hci_recv_frame(data->hdev, skb);
473 skb = NULL;
474 }
475 }
476
477 data->acl_skb = skb;
478 spin_unlock(&data->rxlock);
479
480 return err;
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200481}
482
483static int btusb_recv_isoc(struct btusb_data *data, void *buffer, int count)
484{
Marcel Holtmann803b5832014-09-16 08:00:29 +0200485 struct sk_buff *skb;
486 int err = 0;
487
488 spin_lock(&data->rxlock);
489 skb = data->sco_skb;
490
491 while (count) {
492 int len;
493
494 if (!skb) {
495 skb = bt_skb_alloc(HCI_MAX_SCO_SIZE, GFP_ATOMIC);
496 if (!skb) {
497 err = -ENOMEM;
498 break;
499 }
500
501 bt_cb(skb)->pkt_type = HCI_SCODATA_PKT;
502 bt_cb(skb)->expect = HCI_SCO_HDR_SIZE;
503 }
504
505 len = min_t(uint, bt_cb(skb)->expect, count);
506 memcpy(skb_put(skb, len), buffer, len);
507
508 count -= len;
509 buffer += len;
510 bt_cb(skb)->expect -= len;
511
512 if (skb->len == HCI_SCO_HDR_SIZE) {
513 /* Complete SCO header */
514 bt_cb(skb)->expect = hci_sco_hdr(skb)->dlen;
515
516 if (skb_tailroom(skb) < bt_cb(skb)->expect) {
517 kfree_skb(skb);
518 skb = NULL;
519
520 err = -EILSEQ;
521 break;
522 }
523 }
524
525 if (bt_cb(skb)->expect == 0) {
526 /* Complete frame */
527 hci_recv_frame(data->hdev, skb);
528 skb = NULL;
529 }
530 }
531
532 data->sco_skb = skb;
533 spin_unlock(&data->rxlock);
534
535 return err;
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200536}
537
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200538static void btusb_intr_complete(struct urb *urb)
539{
540 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +0100541 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200542 int err;
543
Marcel Holtmann89e75332014-09-16 04:44:50 +0200544 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
545 urb->actual_length);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200546
547 if (!test_bit(HCI_RUNNING, &hdev->flags))
548 return;
549
550 if (urb->status == 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200551 hdev->stat.byte_rx += urb->actual_length;
552
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200553 if (btusb_recv_intr(data, urb->transfer_buffer,
554 urb->actual_length) < 0) {
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200555 BT_ERR("%s corrupted event packet", hdev->name);
556 hdev->stat.err_rx++;
557 }
Champion Chen85560c42014-09-06 14:06:08 -0500558 } else if (urb->status == -ENOENT) {
559 /* Avoid suspend failed when usb_kill_urb */
560 return;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200561 }
562
563 if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
564 return;
565
Oliver Neukum7bee5492009-08-24 23:44:59 +0200566 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200567 usb_anchor_urb(urb, &data->intr_anchor);
568
569 err = usb_submit_urb(urb, GFP_ATOMIC);
570 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200571 /* -EPERM: urb is being killed;
572 * -ENODEV: device got disconnected */
573 if (err != -EPERM && err != -ENODEV)
Stefan Seyfried61faddf2010-11-30 21:49:08 +0100574 BT_ERR("%s urb %p failed to resubmit (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200575 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200576 usb_unanchor_urb(urb);
577 }
578}
579
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100580static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200581{
David Herrmann155961e2012-02-09 21:58:32 +0100582 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200583 struct urb *urb;
584 unsigned char *buf;
585 unsigned int pipe;
586 int err, size;
587
588 BT_DBG("%s", hdev->name);
589
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200590 if (!data->intr_ep)
591 return -ENODEV;
592
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100593 urb = usb_alloc_urb(0, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200594 if (!urb)
595 return -ENOMEM;
596
597 size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
598
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100599 buf = kmalloc(size, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200600 if (!buf) {
601 usb_free_urb(urb);
602 return -ENOMEM;
603 }
604
605 pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
606
607 usb_fill_int_urb(urb, data->udev, pipe, buf, size,
Marcel Holtmann89e75332014-09-16 04:44:50 +0200608 btusb_intr_complete, hdev, data->intr_ep->bInterval);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200609
610 urb->transfer_flags |= URB_FREE_BUFFER;
611
612 usb_anchor_urb(urb, &data->intr_anchor);
613
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100614 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200615 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +0200616 if (err != -EPERM && err != -ENODEV)
617 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200618 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200619 usb_unanchor_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200620 }
621
622 usb_free_urb(urb);
623
624 return err;
625}
626
627static void btusb_bulk_complete(struct urb *urb)
628{
629 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +0100630 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200631 int err;
632
Marcel Holtmann89e75332014-09-16 04:44:50 +0200633 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
634 urb->actual_length);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200635
636 if (!test_bit(HCI_RUNNING, &hdev->flags))
637 return;
638
639 if (urb->status == 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200640 hdev->stat.byte_rx += urb->actual_length;
641
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +0100642 if (data->recv_bulk(data, urb->transfer_buffer,
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200643 urb->actual_length) < 0) {
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200644 BT_ERR("%s corrupted ACL packet", hdev->name);
645 hdev->stat.err_rx++;
646 }
Champion Chen85560c42014-09-06 14:06:08 -0500647 } else if (urb->status == -ENOENT) {
648 /* Avoid suspend failed when usb_kill_urb */
649 return;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200650 }
651
652 if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
653 return;
654
655 usb_anchor_urb(urb, &data->bulk_anchor);
Oliver Neukum652fd782009-12-16 19:23:43 +0100656 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200657
658 err = usb_submit_urb(urb, GFP_ATOMIC);
659 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200660 /* -EPERM: urb is being killed;
661 * -ENODEV: device got disconnected */
662 if (err != -EPERM && err != -ENODEV)
Stefan Seyfried61faddf2010-11-30 21:49:08 +0100663 BT_ERR("%s urb %p failed to resubmit (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200664 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200665 usb_unanchor_urb(urb);
666 }
667}
668
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100669static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200670{
David Herrmann155961e2012-02-09 21:58:32 +0100671 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200672 struct urb *urb;
673 unsigned char *buf;
674 unsigned int pipe;
Vikram Kandukuri290ba202009-07-02 14:31:59 +0530675 int err, size = HCI_MAX_FRAME_SIZE;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200676
677 BT_DBG("%s", hdev->name);
678
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200679 if (!data->bulk_rx_ep)
680 return -ENODEV;
681
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100682 urb = usb_alloc_urb(0, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200683 if (!urb)
684 return -ENOMEM;
685
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100686 buf = kmalloc(size, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200687 if (!buf) {
688 usb_free_urb(urb);
689 return -ENOMEM;
690 }
691
692 pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
693
Marcel Holtmann89e75332014-09-16 04:44:50 +0200694 usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
695 btusb_bulk_complete, hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200696
697 urb->transfer_flags |= URB_FREE_BUFFER;
698
Oliver Neukum7bee5492009-08-24 23:44:59 +0200699 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200700 usb_anchor_urb(urb, &data->bulk_anchor);
701
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100702 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200703 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +0200704 if (err != -EPERM && err != -ENODEV)
705 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200706 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200707 usb_unanchor_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200708 }
709
710 usb_free_urb(urb);
711
712 return err;
713}
714
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200715static void btusb_isoc_complete(struct urb *urb)
716{
717 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +0100718 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200719 int i, err;
720
Marcel Holtmann89e75332014-09-16 04:44:50 +0200721 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
722 urb->actual_length);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200723
724 if (!test_bit(HCI_RUNNING, &hdev->flags))
725 return;
726
727 if (urb->status == 0) {
728 for (i = 0; i < urb->number_of_packets; i++) {
729 unsigned int offset = urb->iso_frame_desc[i].offset;
730 unsigned int length = urb->iso_frame_desc[i].actual_length;
731
732 if (urb->iso_frame_desc[i].status)
733 continue;
734
735 hdev->stat.byte_rx += length;
736
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200737 if (btusb_recv_isoc(data, urb->transfer_buffer + offset,
738 length) < 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200739 BT_ERR("%s corrupted SCO packet", hdev->name);
740 hdev->stat.err_rx++;
741 }
742 }
Champion Chen85560c42014-09-06 14:06:08 -0500743 } else if (urb->status == -ENOENT) {
744 /* Avoid suspend failed when usb_kill_urb */
745 return;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200746 }
747
748 if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags))
749 return;
750
751 usb_anchor_urb(urb, &data->isoc_anchor);
752
753 err = usb_submit_urb(urb, GFP_ATOMIC);
754 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200755 /* -EPERM: urb is being killed;
756 * -ENODEV: device got disconnected */
757 if (err != -EPERM && err != -ENODEV)
Stefan Seyfried61faddf2010-11-30 21:49:08 +0100758 BT_ERR("%s urb %p failed to resubmit (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200759 hdev->name, urb, -err);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200760 usb_unanchor_urb(urb);
761 }
762}
763
Jesper Juhl42b16b32011-01-17 00:09:38 +0100764static inline void __fill_isoc_descriptor(struct urb *urb, int len, int mtu)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200765{
766 int i, offset = 0;
767
768 BT_DBG("len %d mtu %d", len, mtu);
769
770 for (i = 0; i < BTUSB_MAX_ISOC_FRAMES && len >= mtu;
771 i++, offset += mtu, len -= mtu) {
772 urb->iso_frame_desc[i].offset = offset;
773 urb->iso_frame_desc[i].length = mtu;
774 }
775
776 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
777 urb->iso_frame_desc[i].offset = offset;
778 urb->iso_frame_desc[i].length = len;
779 i++;
780 }
781
782 urb->number_of_packets = i;
783}
784
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100785static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200786{
David Herrmann155961e2012-02-09 21:58:32 +0100787 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200788 struct urb *urb;
789 unsigned char *buf;
790 unsigned int pipe;
791 int err, size;
792
793 BT_DBG("%s", hdev->name);
794
795 if (!data->isoc_rx_ep)
796 return -ENODEV;
797
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100798 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200799 if (!urb)
800 return -ENOMEM;
801
802 size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) *
803 BTUSB_MAX_ISOC_FRAMES;
804
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100805 buf = kmalloc(size, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200806 if (!buf) {
807 usb_free_urb(urb);
808 return -ENOMEM;
809 }
810
811 pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress);
812
Bing Zhaofa0fb932011-12-20 18:19:00 -0800813 usb_fill_int_urb(urb, data->udev, pipe, buf, size, btusb_isoc_complete,
Marcel Holtmann89e75332014-09-16 04:44:50 +0200814 hdev, data->isoc_rx_ep->bInterval);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200815
Marcel Holtmann89e75332014-09-16 04:44:50 +0200816 urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200817
818 __fill_isoc_descriptor(urb, size,
Marcel Holtmann89e75332014-09-16 04:44:50 +0200819 le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize));
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200820
821 usb_anchor_urb(urb, &data->isoc_anchor);
822
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100823 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200824 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +0200825 if (err != -EPERM && err != -ENODEV)
826 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200827 hdev->name, urb, -err);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200828 usb_unanchor_urb(urb);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200829 }
830
831 usb_free_urb(urb);
832
833 return err;
834}
835
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200836static void btusb_tx_complete(struct urb *urb)
837{
838 struct sk_buff *skb = urb->context;
Marcel Holtmann89e75332014-09-16 04:44:50 +0200839 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
David Herrmann155961e2012-02-09 21:58:32 +0100840 struct btusb_data *data = hci_get_drvdata(hdev);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200841
Marcel Holtmann89e75332014-09-16 04:44:50 +0200842 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
843 urb->actual_length);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200844
845 if (!test_bit(HCI_RUNNING, &hdev->flags))
846 goto done;
847
848 if (!urb->status)
849 hdev->stat.byte_tx += urb->transfer_buffer_length;
850 else
851 hdev->stat.err_tx++;
852
853done:
854 spin_lock(&data->txlock);
855 data->tx_in_flight--;
856 spin_unlock(&data->txlock);
857
858 kfree(urb->setup_packet);
859
860 kfree_skb(skb);
861}
862
863static void btusb_isoc_tx_complete(struct urb *urb)
864{
865 struct sk_buff *skb = urb->context;
Marcel Holtmann89e75332014-09-16 04:44:50 +0200866 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200867
Marcel Holtmann89e75332014-09-16 04:44:50 +0200868 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
869 urb->actual_length);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200870
871 if (!test_bit(HCI_RUNNING, &hdev->flags))
872 goto done;
873
874 if (!urb->status)
875 hdev->stat.byte_tx += urb->transfer_buffer_length;
876 else
877 hdev->stat.err_tx++;
878
879done:
880 kfree(urb->setup_packet);
881
882 kfree_skb(skb);
883}
884
885static int btusb_open(struct hci_dev *hdev)
886{
David Herrmann155961e2012-02-09 21:58:32 +0100887 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200888 int err;
889
890 BT_DBG("%s", hdev->name);
891
Kim, Ben Young Taeace31982015-02-15 23:06:14 +0000892 /* Patching USB firmware files prior to starting any URBs of HCI path
893 * It is more safe to use USB bulk channel for downloading USB patch
894 */
895 if (data->setup_on_usb) {
896 err = data->setup_on_usb(hdev);
897 if (err <0)
898 return err;
899 }
900
Oliver Neukum7bee5492009-08-24 23:44:59 +0200901 err = usb_autopm_get_interface(data->intf);
902 if (err < 0)
903 return err;
904
905 data->intf->needs_remote_wakeup = 1;
906
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200907 if (test_and_set_bit(HCI_RUNNING, &hdev->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +0200908 goto done;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200909
910 if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +0200911 goto done;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200912
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100913 err = btusb_submit_intr_urb(hdev, GFP_KERNEL);
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100914 if (err < 0)
915 goto failed;
916
917 err = btusb_submit_bulk_urb(hdev, GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200918 if (err < 0) {
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100919 usb_kill_anchored_urbs(&data->intr_anchor);
920 goto failed;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200921 }
922
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100923 set_bit(BTUSB_BULK_RUNNING, &data->flags);
924 btusb_submit_bulk_urb(hdev, GFP_KERNEL);
925
Oliver Neukum7bee5492009-08-24 23:44:59 +0200926done:
927 usb_autopm_put_interface(data->intf);
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100928 return 0;
929
930failed:
931 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
932 clear_bit(HCI_RUNNING, &hdev->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200933 usb_autopm_put_interface(data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200934 return err;
935}
936
Oliver Neukum7bee5492009-08-24 23:44:59 +0200937static void btusb_stop_traffic(struct btusb_data *data)
938{
939 usb_kill_anchored_urbs(&data->intr_anchor);
940 usb_kill_anchored_urbs(&data->bulk_anchor);
941 usb_kill_anchored_urbs(&data->isoc_anchor);
942}
943
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200944static int btusb_close(struct hci_dev *hdev)
945{
David Herrmann155961e2012-02-09 21:58:32 +0100946 struct btusb_data *data = hci_get_drvdata(hdev);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200947 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200948
949 BT_DBG("%s", hdev->name);
950
951 if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
952 return 0;
953
Marcel Holtmanne8c3c3d2008-09-23 00:16:36 +0200954 cancel_work_sync(&data->work);
Linus Torvalds404291a2009-11-11 13:32:29 -0800955 cancel_work_sync(&data->waker);
Marcel Holtmanne8c3c3d2008-09-23 00:16:36 +0200956
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200957 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200958 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200959 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200960
961 btusb_stop_traffic(data);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200962 btusb_free_frags(data);
963
Oliver Neukum7bee5492009-08-24 23:44:59 +0200964 err = usb_autopm_get_interface(data->intf);
965 if (err < 0)
Oliver Neukum7b8e2c12009-11-13 14:26:23 +0100966 goto failed;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200967
968 data->intf->needs_remote_wakeup = 0;
969 usb_autopm_put_interface(data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200970
Oliver Neukum7b8e2c12009-11-13 14:26:23 +0100971failed:
972 usb_scuttle_anchored_urbs(&data->deferred);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200973 return 0;
974}
975
976static int btusb_flush(struct hci_dev *hdev)
977{
David Herrmann155961e2012-02-09 21:58:32 +0100978 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200979
980 BT_DBG("%s", hdev->name);
981
982 usb_kill_anchored_urbs(&data->tx_anchor);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200983 btusb_free_frags(data);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200984
985 return 0;
986}
987
Marcel Holtmann047b2ec2014-09-14 09:11:06 +0200988static struct urb *alloc_ctrl_urb(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200989{
David Herrmann155961e2012-02-09 21:58:32 +0100990 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200991 struct usb_ctrlrequest *dr;
992 struct urb *urb;
993 unsigned int pipe;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200994
Marcel Holtmann047b2ec2014-09-14 09:11:06 +0200995 urb = usb_alloc_urb(0, GFP_KERNEL);
996 if (!urb)
997 return ERR_PTR(-ENOMEM);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200998
Marcel Holtmann047b2ec2014-09-14 09:11:06 +0200999 dr = kmalloc(sizeof(*dr), GFP_KERNEL);
1000 if (!dr) {
1001 usb_free_urb(urb);
1002 return ERR_PTR(-ENOMEM);
1003 }
1004
1005 dr->bRequestType = data->cmdreq_type;
Marcel Holtmann893ba542015-01-28 20:27:34 -08001006 dr->bRequest = data->cmdreq;
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001007 dr->wIndex = 0;
1008 dr->wValue = 0;
1009 dr->wLength = __cpu_to_le16(skb->len);
1010
1011 pipe = usb_sndctrlpipe(data->udev, 0x00);
1012
Marcel Holtmann89e75332014-09-16 04:44:50 +02001013 usb_fill_control_urb(urb, data->udev, pipe, (void *)dr,
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001014 skb->data, skb->len, btusb_tx_complete, skb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001015
Marcel Holtmann89e75332014-09-16 04:44:50 +02001016 skb->dev = (void *)hdev;
Marcel Holtmann7bd8f092013-10-11 06:19:18 -07001017
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001018 return urb;
1019}
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001020
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001021static struct urb *alloc_bulk_urb(struct hci_dev *hdev, struct sk_buff *skb)
1022{
1023 struct btusb_data *data = hci_get_drvdata(hdev);
1024 struct urb *urb;
1025 unsigned int pipe;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001026
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001027 if (!data->bulk_tx_ep)
1028 return ERR_PTR(-ENODEV);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001029
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001030 urb = usb_alloc_urb(0, GFP_KERNEL);
1031 if (!urb)
1032 return ERR_PTR(-ENOMEM);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001033
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001034 pipe = usb_sndbulkpipe(data->udev, data->bulk_tx_ep->bEndpointAddress);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001035
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001036 usb_fill_bulk_urb(urb, data->udev, pipe,
1037 skb->data, skb->len, btusb_tx_complete, skb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001038
Marcel Holtmann89e75332014-09-16 04:44:50 +02001039 skb->dev = (void *)hdev;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001040
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001041 return urb;
1042}
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001043
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001044static struct urb *alloc_isoc_urb(struct hci_dev *hdev, struct sk_buff *skb)
1045{
1046 struct btusb_data *data = hci_get_drvdata(hdev);
1047 struct urb *urb;
1048 unsigned int pipe;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001049
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001050 if (!data->isoc_tx_ep)
1051 return ERR_PTR(-ENODEV);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001052
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001053 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_KERNEL);
1054 if (!urb)
1055 return ERR_PTR(-ENOMEM);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001056
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001057 pipe = usb_sndisocpipe(data->udev, data->isoc_tx_ep->bEndpointAddress);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001058
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001059 usb_fill_int_urb(urb, data->udev, pipe,
1060 skb->data, skb->len, btusb_isoc_tx_complete,
1061 skb, data->isoc_tx_ep->bInterval);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001062
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001063 urb->transfer_flags = URB_ISO_ASAP;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001064
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001065 __fill_isoc_descriptor(urb, skb->len,
1066 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001067
Marcel Holtmann89e75332014-09-16 04:44:50 +02001068 skb->dev = (void *)hdev;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001069
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001070 return urb;
1071}
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001072
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001073static int submit_tx_urb(struct hci_dev *hdev, struct urb *urb)
1074{
1075 struct btusb_data *data = hci_get_drvdata(hdev);
1076 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001077
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001078 usb_anchor_urb(urb, &data->tx_anchor);
1079
Johan Hedberge9753ef2014-09-14 08:49:34 +03001080 err = usb_submit_urb(urb, GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001081 if (err < 0) {
Paul Bolle5a9b80e2011-10-09 12:12:16 +02001082 if (err != -EPERM && err != -ENODEV)
1083 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +02001084 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001085 kfree(urb->setup_packet);
1086 usb_unanchor_urb(urb);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001087 } else {
1088 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001089 }
1090
Cong Wang54a8a792011-11-22 09:32:57 +08001091 usb_free_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001092 return err;
1093}
1094
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001095static int submit_or_queue_tx_urb(struct hci_dev *hdev, struct urb *urb)
1096{
1097 struct btusb_data *data = hci_get_drvdata(hdev);
1098 unsigned long flags;
1099 bool suspending;
1100
1101 spin_lock_irqsave(&data->txlock, flags);
1102 suspending = test_bit(BTUSB_SUSPENDING, &data->flags);
1103 if (!suspending)
1104 data->tx_in_flight++;
1105 spin_unlock_irqrestore(&data->txlock, flags);
1106
1107 if (!suspending)
1108 return submit_tx_urb(hdev, urb);
1109
1110 usb_anchor_urb(urb, &data->deferred);
1111 schedule_work(&data->waker);
1112
1113 usb_free_urb(urb);
1114 return 0;
1115}
1116
1117static int btusb_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
1118{
1119 struct urb *urb;
1120
1121 BT_DBG("%s", hdev->name);
1122
1123 if (!test_bit(HCI_RUNNING, &hdev->flags))
1124 return -EBUSY;
1125
1126 switch (bt_cb(skb)->pkt_type) {
1127 case HCI_COMMAND_PKT:
1128 urb = alloc_ctrl_urb(hdev, skb);
1129 if (IS_ERR(urb))
1130 return PTR_ERR(urb);
1131
1132 hdev->stat.cmd_tx++;
1133 return submit_or_queue_tx_urb(hdev, urb);
1134
1135 case HCI_ACLDATA_PKT:
1136 urb = alloc_bulk_urb(hdev, skb);
1137 if (IS_ERR(urb))
1138 return PTR_ERR(urb);
1139
1140 hdev->stat.acl_tx++;
1141 return submit_or_queue_tx_urb(hdev, urb);
1142
1143 case HCI_SCODATA_PKT:
1144 if (hci_conn_num(hdev, SCO_LINK) < 1)
1145 return -ENODEV;
1146
1147 urb = alloc_isoc_urb(hdev, skb);
1148 if (IS_ERR(urb))
1149 return PTR_ERR(urb);
1150
1151 hdev->stat.sco_tx++;
1152 return submit_tx_urb(hdev, urb);
1153 }
1154
1155 return -EILSEQ;
1156}
1157
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001158static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
1159{
David Herrmann155961e2012-02-09 21:58:32 +01001160 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001161
1162 BT_DBG("%s evt %d", hdev->name, evt);
1163
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001164 if (hci_conn_num(hdev, SCO_LINK) != data->sco_num) {
1165 data->sco_num = hci_conn_num(hdev, SCO_LINK);
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001166 schedule_work(&data->work);
Marcel Holtmanna780efa2008-11-30 12:17:12 +01001167 }
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001168}
1169
Jesper Juhl42b16b32011-01-17 00:09:38 +01001170static inline int __set_isoc_interface(struct hci_dev *hdev, int altsetting)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001171{
David Herrmann155961e2012-02-09 21:58:32 +01001172 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001173 struct usb_interface *intf = data->isoc;
1174 struct usb_endpoint_descriptor *ep_desc;
1175 int i, err;
1176
1177 if (!data->isoc)
1178 return -ENODEV;
1179
1180 err = usb_set_interface(data->udev, 1, altsetting);
1181 if (err < 0) {
1182 BT_ERR("%s setting interface failed (%d)", hdev->name, -err);
1183 return err;
1184 }
1185
1186 data->isoc_altsetting = altsetting;
1187
1188 data->isoc_tx_ep = NULL;
1189 data->isoc_rx_ep = NULL;
1190
1191 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
1192 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
1193
1194 if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) {
1195 data->isoc_tx_ep = ep_desc;
1196 continue;
1197 }
1198
1199 if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) {
1200 data->isoc_rx_ep = ep_desc;
1201 continue;
1202 }
1203 }
1204
1205 if (!data->isoc_tx_ep || !data->isoc_rx_ep) {
1206 BT_ERR("%s invalid SCO descriptors", hdev->name);
1207 return -ENODEV;
1208 }
1209
1210 return 0;
1211}
1212
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001213static void btusb_work(struct work_struct *work)
1214{
1215 struct btusb_data *data = container_of(work, struct btusb_data, work);
1216 struct hci_dev *hdev = data->hdev;
Mikel Astizf4001d22012-04-11 08:48:51 +02001217 int new_alts;
Oliver Neukum7bee5492009-08-24 23:44:59 +02001218 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001219
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001220 if (data->sco_num > 0) {
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -03001221 if (!test_bit(BTUSB_DID_ISO_RESUME, &data->flags)) {
Oliver Neukum8efdd0c2011-02-11 13:00:06 +01001222 err = usb_autopm_get_interface(data->isoc ? data->isoc : data->intf);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001223 if (err < 0) {
1224 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1225 usb_kill_anchored_urbs(&data->isoc_anchor);
1226 return;
1227 }
1228
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -03001229 set_bit(BTUSB_DID_ISO_RESUME, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001230 }
Mikel Astizf4001d22012-04-11 08:48:51 +02001231
1232 if (hdev->voice_setting & 0x0020) {
1233 static const int alts[3] = { 2, 4, 5 };
Marcel Holtmann89e75332014-09-16 04:44:50 +02001234
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001235 new_alts = alts[data->sco_num - 1];
Mikel Astizf4001d22012-04-11 08:48:51 +02001236 } else {
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001237 new_alts = data->sco_num;
Mikel Astizf4001d22012-04-11 08:48:51 +02001238 }
1239
1240 if (data->isoc_altsetting != new_alts) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001241 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1242 usb_kill_anchored_urbs(&data->isoc_anchor);
1243
Mikel Astizf4001d22012-04-11 08:48:51 +02001244 if (__set_isoc_interface(hdev, new_alts) < 0)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001245 return;
1246 }
1247
1248 if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001249 if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001250 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1251 else
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001252 btusb_submit_isoc_urb(hdev, GFP_KERNEL);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001253 }
1254 } else {
1255 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1256 usb_kill_anchored_urbs(&data->isoc_anchor);
1257
1258 __set_isoc_interface(hdev, 0);
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -03001259 if (test_and_clear_bit(BTUSB_DID_ISO_RESUME, &data->flags))
Oliver Neukum8efdd0c2011-02-11 13:00:06 +01001260 usb_autopm_put_interface(data->isoc ? data->isoc : data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001261 }
1262}
1263
Oliver Neukum7bee5492009-08-24 23:44:59 +02001264static void btusb_waker(struct work_struct *work)
1265{
1266 struct btusb_data *data = container_of(work, struct btusb_data, waker);
1267 int err;
1268
1269 err = usb_autopm_get_interface(data->intf);
1270 if (err < 0)
1271 return;
1272
1273 usb_autopm_put_interface(data->intf);
1274}
1275
Daniel Drake6576fe42015-02-23 18:16:47 -06001276static struct sk_buff *btusb_read_local_version(struct hci_dev *hdev)
1277{
1278 struct sk_buff *skb;
1279
1280 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
1281 HCI_INIT_TIMEOUT);
1282 if (IS_ERR(skb)) {
1283 BT_ERR("%s: HCI_OP_READ_LOCAL_VERSION failed (%ld)",
1284 hdev->name, PTR_ERR(skb));
1285 return skb;
1286 }
1287
1288 if (skb->len != sizeof(struct hci_rp_read_local_version)) {
1289 BT_ERR("%s: HCI_OP_READ_LOCAL_VERSION event length mismatch",
1290 hdev->name);
1291 kfree_skb(skb);
1292 return ERR_PTR(-EIO);
1293 }
1294
1295 return skb;
1296}
1297
Marcel Holtmann9f8f9622013-04-10 08:11:35 -07001298static int btusb_setup_bcm92035(struct hci_dev *hdev)
1299{
1300 struct sk_buff *skb;
1301 u8 val = 0x00;
1302
1303 BT_DBG("%s", hdev->name);
1304
1305 skb = __hci_cmd_sync(hdev, 0xfc3b, 1, &val, HCI_INIT_TIMEOUT);
1306 if (IS_ERR(skb))
1307 BT_ERR("BCM92035 command failed (%ld)", -PTR_ERR(skb));
1308 else
1309 kfree_skb(skb);
1310
1311 return 0;
1312}
1313
Marcel Holtmann81cac642014-01-03 03:02:36 -08001314static int btusb_setup_csr(struct hci_dev *hdev)
1315{
1316 struct hci_rp_read_local_version *rp;
1317 struct sk_buff *skb;
1318 int ret;
1319
1320 BT_DBG("%s", hdev->name);
1321
Daniel Drake6576fe42015-02-23 18:16:47 -06001322 skb = btusb_read_local_version(hdev);
1323 if (IS_ERR(skb))
Marcel Holtmann81cac642014-01-03 03:02:36 -08001324 return -PTR_ERR(skb);
Marcel Holtmann81cac642014-01-03 03:02:36 -08001325
Marcel Holtmann89e75332014-09-16 04:44:50 +02001326 rp = (struct hci_rp_read_local_version *)skb->data;
Marcel Holtmann81cac642014-01-03 03:02:36 -08001327
1328 if (!rp->status) {
1329 if (le16_to_cpu(rp->manufacturer) != 10) {
1330 /* Clear the reset quirk since this is not an actual
1331 * early Bluetooth 1.1 device from CSR.
1332 */
1333 clear_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
1334
1335 /* These fake CSR controllers have all a broken
1336 * stored link key handling and so just disable it.
1337 */
1338 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY,
1339 &hdev->quirks);
1340 }
1341 }
1342
1343 ret = -bt_to_errno(rp->status);
1344
1345 kfree_skb(skb);
1346
1347 return ret;
1348}
1349
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001350struct intel_version {
1351 u8 status;
1352 u8 hw_platform;
1353 u8 hw_variant;
1354 u8 hw_revision;
1355 u8 fw_variant;
1356 u8 fw_revision;
1357 u8 fw_build_num;
1358 u8 fw_build_ww;
1359 u8 fw_build_yy;
1360 u8 fw_patch_num;
1361} __packed;
1362
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001363struct intel_boot_params {
1364 __u8 status;
1365 __u8 otp_format;
1366 __u8 otp_content;
1367 __u8 otp_patch;
1368 __le16 dev_revid;
1369 __u8 secure_boot;
1370 __u8 key_from_hdr;
1371 __u8 key_type;
1372 __u8 otp_lock;
1373 __u8 api_lock;
1374 __u8 debug_lock;
1375 bdaddr_t otp_bdaddr;
1376 __u8 min_fw_build_nn;
1377 __u8 min_fw_build_cw;
1378 __u8 min_fw_build_yy;
1379 __u8 limited_cce;
1380 __u8 unlocked_state;
1381} __packed;
1382
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001383static const struct firmware *btusb_setup_intel_get_fw(struct hci_dev *hdev,
Marcel Holtmann89e75332014-09-16 04:44:50 +02001384 struct intel_version *ver)
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001385{
1386 const struct firmware *fw;
1387 char fwname[64];
1388 int ret;
1389
1390 snprintf(fwname, sizeof(fwname),
1391 "intel/ibt-hw-%x.%x.%x-fw-%x.%x.%x.%x.%x.bseq",
1392 ver->hw_platform, ver->hw_variant, ver->hw_revision,
1393 ver->fw_variant, ver->fw_revision, ver->fw_build_num,
1394 ver->fw_build_ww, ver->fw_build_yy);
1395
1396 ret = request_firmware(&fw, fwname, &hdev->dev);
1397 if (ret < 0) {
1398 if (ret == -EINVAL) {
1399 BT_ERR("%s Intel firmware file request failed (%d)",
1400 hdev->name, ret);
1401 return NULL;
1402 }
1403
1404 BT_ERR("%s failed to open Intel firmware file: %s(%d)",
1405 hdev->name, fwname, ret);
1406
1407 /* If the correct firmware patch file is not found, use the
1408 * default firmware patch file instead
1409 */
1410 snprintf(fwname, sizeof(fwname), "intel/ibt-hw-%x.%x.bseq",
1411 ver->hw_platform, ver->hw_variant);
1412 if (request_firmware(&fw, fwname, &hdev->dev) < 0) {
1413 BT_ERR("%s failed to open default Intel fw file: %s",
1414 hdev->name, fwname);
1415 return NULL;
1416 }
1417 }
1418
1419 BT_INFO("%s: Intel Bluetooth firmware file: %s", hdev->name, fwname);
1420
1421 return fw;
1422}
1423
1424static int btusb_setup_intel_patching(struct hci_dev *hdev,
1425 const struct firmware *fw,
1426 const u8 **fw_ptr, int *disable_patch)
1427{
1428 struct sk_buff *skb;
1429 struct hci_command_hdr *cmd;
1430 const u8 *cmd_param;
1431 struct hci_event_hdr *evt = NULL;
1432 const u8 *evt_param = NULL;
1433 int remain = fw->size - (*fw_ptr - fw->data);
1434
1435 /* The first byte indicates the types of the patch command or event.
1436 * 0x01 means HCI command and 0x02 is HCI event. If the first bytes
1437 * in the current firmware buffer doesn't start with 0x01 or
1438 * the size of remain buffer is smaller than HCI command header,
1439 * the firmware file is corrupted and it should stop the patching
1440 * process.
1441 */
1442 if (remain > HCI_COMMAND_HDR_SIZE && *fw_ptr[0] != 0x01) {
1443 BT_ERR("%s Intel fw corrupted: invalid cmd read", hdev->name);
1444 return -EINVAL;
1445 }
1446 (*fw_ptr)++;
1447 remain--;
1448
1449 cmd = (struct hci_command_hdr *)(*fw_ptr);
1450 *fw_ptr += sizeof(*cmd);
1451 remain -= sizeof(*cmd);
1452
1453 /* Ensure that the remain firmware data is long enough than the length
1454 * of command parameter. If not, the firmware file is corrupted.
1455 */
1456 if (remain < cmd->plen) {
1457 BT_ERR("%s Intel fw corrupted: invalid cmd len", hdev->name);
1458 return -EFAULT;
1459 }
1460
1461 /* If there is a command that loads a patch in the firmware
1462 * file, then enable the patch upon success, otherwise just
1463 * disable the manufacturer mode, for example patch activation
1464 * is not required when the default firmware patch file is used
1465 * because there are no patch data to load.
1466 */
1467 if (*disable_patch && le16_to_cpu(cmd->opcode) == 0xfc8e)
1468 *disable_patch = 0;
1469
1470 cmd_param = *fw_ptr;
1471 *fw_ptr += cmd->plen;
1472 remain -= cmd->plen;
1473
1474 /* This reads the expected events when the above command is sent to the
1475 * device. Some vendor commands expects more than one events, for
1476 * example command status event followed by vendor specific event.
1477 * For this case, it only keeps the last expected event. so the command
1478 * can be sent with __hci_cmd_sync_ev() which returns the sk_buff of
1479 * last expected event.
1480 */
1481 while (remain > HCI_EVENT_HDR_SIZE && *fw_ptr[0] == 0x02) {
1482 (*fw_ptr)++;
1483 remain--;
1484
1485 evt = (struct hci_event_hdr *)(*fw_ptr);
1486 *fw_ptr += sizeof(*evt);
1487 remain -= sizeof(*evt);
1488
1489 if (remain < evt->plen) {
1490 BT_ERR("%s Intel fw corrupted: invalid evt len",
1491 hdev->name);
1492 return -EFAULT;
1493 }
1494
1495 evt_param = *fw_ptr;
1496 *fw_ptr += evt->plen;
1497 remain -= evt->plen;
1498 }
1499
1500 /* Every HCI commands in the firmware file has its correspond event.
1501 * If event is not found or remain is smaller than zero, the firmware
1502 * file is corrupted.
1503 */
1504 if (!evt || !evt_param || remain < 0) {
1505 BT_ERR("%s Intel fw corrupted: invalid evt read", hdev->name);
1506 return -EFAULT;
1507 }
1508
1509 skb = __hci_cmd_sync_ev(hdev, le16_to_cpu(cmd->opcode), cmd->plen,
1510 cmd_param, evt->evt, HCI_INIT_TIMEOUT);
1511 if (IS_ERR(skb)) {
1512 BT_ERR("%s sending Intel patch command (0x%4.4x) failed (%ld)",
1513 hdev->name, cmd->opcode, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001514 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001515 }
1516
1517 /* It ensures that the returned event matches the event data read from
1518 * the firmware file. At fist, it checks the length and then
1519 * the contents of the event.
1520 */
1521 if (skb->len != evt->plen) {
1522 BT_ERR("%s mismatch event length (opcode 0x%4.4x)", hdev->name,
1523 le16_to_cpu(cmd->opcode));
1524 kfree_skb(skb);
1525 return -EFAULT;
1526 }
1527
1528 if (memcmp(skb->data, evt_param, evt->plen)) {
1529 BT_ERR("%s mismatch event parameter (opcode 0x%4.4x)",
1530 hdev->name, le16_to_cpu(cmd->opcode));
1531 kfree_skb(skb);
1532 return -EFAULT;
1533 }
1534 kfree_skb(skb);
1535
1536 return 0;
1537}
1538
Marcel Holtmann40cb0982014-07-02 12:06:45 +02001539#define BDADDR_INTEL (&(bdaddr_t) {{0x00, 0x8b, 0x9e, 0x19, 0x03, 0x00}})
1540
1541static int btusb_check_bdaddr_intel(struct hci_dev *hdev)
1542{
1543 struct sk_buff *skb;
1544 struct hci_rp_read_bd_addr *rp;
1545
1546 skb = __hci_cmd_sync(hdev, HCI_OP_READ_BD_ADDR, 0, NULL,
1547 HCI_INIT_TIMEOUT);
1548 if (IS_ERR(skb)) {
1549 BT_ERR("%s reading Intel device address failed (%ld)",
1550 hdev->name, PTR_ERR(skb));
1551 return PTR_ERR(skb);
1552 }
1553
1554 if (skb->len != sizeof(*rp)) {
1555 BT_ERR("%s Intel device address length mismatch", hdev->name);
1556 kfree_skb(skb);
1557 return -EIO;
1558 }
1559
Marcel Holtmann89e75332014-09-16 04:44:50 +02001560 rp = (struct hci_rp_read_bd_addr *)skb->data;
Marcel Holtmann40cb0982014-07-02 12:06:45 +02001561 if (rp->status) {
1562 BT_ERR("%s Intel device address result failed (%02x)",
1563 hdev->name, rp->status);
1564 kfree_skb(skb);
1565 return -bt_to_errno(rp->status);
1566 }
1567
1568 /* For some Intel based controllers, the default Bluetooth device
1569 * address 00:03:19:9E:8B:00 can be found. These controllers are
1570 * fully operational, but have the danger of duplicate addresses
1571 * and that in turn can cause problems with Bluetooth operation.
1572 */
Marcel Holtmann4739b5b2014-07-04 16:54:38 +02001573 if (!bacmp(&rp->bdaddr, BDADDR_INTEL)) {
Marcel Holtmann40cb0982014-07-02 12:06:45 +02001574 BT_ERR("%s found Intel default device address (%pMR)",
1575 hdev->name, &rp->bdaddr);
Marcel Holtmann4739b5b2014-07-04 16:54:38 +02001576 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
1577 }
Marcel Holtmann40cb0982014-07-02 12:06:45 +02001578
1579 kfree_skb(skb);
1580
1581 return 0;
1582}
1583
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001584static int btusb_setup_intel(struct hci_dev *hdev)
1585{
1586 struct sk_buff *skb;
1587 const struct firmware *fw;
1588 const u8 *fw_ptr;
1589 int disable_patch;
1590 struct intel_version *ver;
1591
1592 const u8 mfg_enable[] = { 0x01, 0x00 };
1593 const u8 mfg_disable[] = { 0x00, 0x00 };
1594 const u8 mfg_reset_deactivate[] = { 0x00, 0x01 };
1595 const u8 mfg_reset_activate[] = { 0x00, 0x02 };
1596
1597 BT_DBG("%s", hdev->name);
1598
1599 /* The controller has a bug with the first HCI command sent to it
1600 * returning number of completed commands as zero. This would stall the
1601 * command processing in the Bluetooth core.
1602 *
1603 * As a workaround, send HCI Reset command first which will reset the
1604 * number of completed commands and allow normal command processing
1605 * from now on.
1606 */
1607 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
1608 if (IS_ERR(skb)) {
1609 BT_ERR("%s sending initial HCI reset command failed (%ld)",
1610 hdev->name, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001611 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001612 }
1613 kfree_skb(skb);
1614
1615 /* Read Intel specific controller version first to allow selection of
1616 * which firmware file to load.
1617 *
1618 * The returned information are hardware variant and revision plus
1619 * firmware variant, revision and build number.
1620 */
1621 skb = __hci_cmd_sync(hdev, 0xfc05, 0, NULL, HCI_INIT_TIMEOUT);
1622 if (IS_ERR(skb)) {
1623 BT_ERR("%s reading Intel fw version command failed (%ld)",
1624 hdev->name, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001625 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001626 }
1627
1628 if (skb->len != sizeof(*ver)) {
1629 BT_ERR("%s Intel version event length mismatch", hdev->name);
1630 kfree_skb(skb);
1631 return -EIO;
1632 }
1633
1634 ver = (struct intel_version *)skb->data;
1635 if (ver->status) {
1636 BT_ERR("%s Intel fw version event failed (%02x)", hdev->name,
1637 ver->status);
1638 kfree_skb(skb);
1639 return -bt_to_errno(ver->status);
1640 }
1641
1642 BT_INFO("%s: read Intel version: %02x%02x%02x%02x%02x%02x%02x%02x%02x",
1643 hdev->name, ver->hw_platform, ver->hw_variant,
1644 ver->hw_revision, ver->fw_variant, ver->fw_revision,
1645 ver->fw_build_num, ver->fw_build_ww, ver->fw_build_yy,
1646 ver->fw_patch_num);
1647
1648 /* fw_patch_num indicates the version of patch the device currently
1649 * have. If there is no patch data in the device, it is always 0x00.
1650 * So, if it is other than 0x00, no need to patch the deivce again.
1651 */
1652 if (ver->fw_patch_num) {
1653 BT_INFO("%s: Intel device is already patched. patch num: %02x",
1654 hdev->name, ver->fw_patch_num);
1655 kfree_skb(skb);
Marcel Holtmann40cb0982014-07-02 12:06:45 +02001656 btusb_check_bdaddr_intel(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001657 return 0;
1658 }
1659
1660 /* Opens the firmware patch file based on the firmware version read
1661 * from the controller. If it fails to open the matching firmware
1662 * patch file, it tries to open the default firmware patch file.
1663 * If no patch file is found, allow the device to operate without
1664 * a patch.
1665 */
1666 fw = btusb_setup_intel_get_fw(hdev, ver);
1667 if (!fw) {
1668 kfree_skb(skb);
Marcel Holtmann40cb0982014-07-02 12:06:45 +02001669 btusb_check_bdaddr_intel(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001670 return 0;
1671 }
1672 fw_ptr = fw->data;
1673
1674 /* This Intel specific command enables the manufacturer mode of the
1675 * controller.
1676 *
1677 * Only while this mode is enabled, the driver can download the
1678 * firmware patch data and configuration parameters.
1679 */
1680 skb = __hci_cmd_sync(hdev, 0xfc11, 2, mfg_enable, HCI_INIT_TIMEOUT);
1681 if (IS_ERR(skb)) {
1682 BT_ERR("%s entering Intel manufacturer mode failed (%ld)",
1683 hdev->name, PTR_ERR(skb));
1684 release_firmware(fw);
Adam Leed9c78e92013-07-10 10:02:12 +08001685 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001686 }
1687
1688 if (skb->data[0]) {
1689 u8 evt_status = skb->data[0];
Marcel Holtmann89e75332014-09-16 04:44:50 +02001690
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001691 BT_ERR("%s enable Intel manufacturer mode event failed (%02x)",
1692 hdev->name, evt_status);
1693 kfree_skb(skb);
1694 release_firmware(fw);
1695 return -bt_to_errno(evt_status);
1696 }
1697 kfree_skb(skb);
1698
1699 disable_patch = 1;
1700
1701 /* The firmware data file consists of list of Intel specific HCI
1702 * commands and its expected events. The first byte indicates the
1703 * type of the message, either HCI command or HCI event.
1704 *
1705 * It reads the command and its expected event from the firmware file,
1706 * and send to the controller. Once __hci_cmd_sync_ev() returns,
1707 * the returned event is compared with the event read from the firmware
1708 * file and it will continue until all the messages are downloaded to
1709 * the controller.
1710 *
1711 * Once the firmware patching is completed successfully,
1712 * the manufacturer mode is disabled with reset and activating the
1713 * downloaded patch.
1714 *
1715 * If the firmware patching fails, the manufacturer mode is
1716 * disabled with reset and deactivating the patch.
1717 *
1718 * If the default patch file is used, no reset is done when disabling
1719 * the manufacturer.
1720 */
1721 while (fw->size > fw_ptr - fw->data) {
1722 int ret;
1723
1724 ret = btusb_setup_intel_patching(hdev, fw, &fw_ptr,
1725 &disable_patch);
1726 if (ret < 0)
1727 goto exit_mfg_deactivate;
1728 }
1729
1730 release_firmware(fw);
1731
1732 if (disable_patch)
1733 goto exit_mfg_disable;
1734
1735 /* Patching completed successfully and disable the manufacturer mode
1736 * with reset and activate the downloaded firmware patches.
1737 */
1738 skb = __hci_cmd_sync(hdev, 0xfc11, sizeof(mfg_reset_activate),
1739 mfg_reset_activate, HCI_INIT_TIMEOUT);
1740 if (IS_ERR(skb)) {
1741 BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
1742 hdev->name, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001743 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001744 }
1745 kfree_skb(skb);
1746
1747 BT_INFO("%s: Intel Bluetooth firmware patch completed and activated",
1748 hdev->name);
1749
Marcel Holtmann40cb0982014-07-02 12:06:45 +02001750 btusb_check_bdaddr_intel(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001751 return 0;
1752
1753exit_mfg_disable:
1754 /* Disable the manufacturer mode without reset */
1755 skb = __hci_cmd_sync(hdev, 0xfc11, sizeof(mfg_disable), mfg_disable,
1756 HCI_INIT_TIMEOUT);
1757 if (IS_ERR(skb)) {
1758 BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
1759 hdev->name, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001760 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001761 }
1762 kfree_skb(skb);
1763
1764 BT_INFO("%s: Intel Bluetooth firmware patch completed", hdev->name);
Marcel Holtmann40cb0982014-07-02 12:06:45 +02001765
1766 btusb_check_bdaddr_intel(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001767 return 0;
1768
1769exit_mfg_deactivate:
1770 release_firmware(fw);
1771
1772 /* Patching failed. Disable the manufacturer mode with reset and
1773 * deactivate the downloaded firmware patches.
1774 */
1775 skb = __hci_cmd_sync(hdev, 0xfc11, sizeof(mfg_reset_deactivate),
1776 mfg_reset_deactivate, HCI_INIT_TIMEOUT);
1777 if (IS_ERR(skb)) {
1778 BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
1779 hdev->name, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001780 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001781 }
1782 kfree_skb(skb);
1783
1784 BT_INFO("%s: Intel Bluetooth firmware patch completed and deactivated",
1785 hdev->name);
1786
Marcel Holtmann40cb0982014-07-02 12:06:45 +02001787 btusb_check_bdaddr_intel(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001788 return 0;
1789}
1790
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001791static int inject_cmd_complete(struct hci_dev *hdev, __u16 opcode)
1792{
1793 struct sk_buff *skb;
1794 struct hci_event_hdr *hdr;
1795 struct hci_ev_cmd_complete *evt;
1796
1797 skb = bt_skb_alloc(sizeof(*hdr) + sizeof(*evt) + 1, GFP_ATOMIC);
1798 if (!skb)
1799 return -ENOMEM;
1800
1801 hdr = (struct hci_event_hdr *)skb_put(skb, sizeof(*hdr));
1802 hdr->evt = HCI_EV_CMD_COMPLETE;
1803 hdr->plen = sizeof(*evt) + 1;
1804
1805 evt = (struct hci_ev_cmd_complete *)skb_put(skb, sizeof(*evt));
1806 evt->ncmd = 0x01;
1807 evt->opcode = cpu_to_le16(opcode);
1808
1809 *skb_put(skb, 1) = 0x00;
1810
1811 bt_cb(skb)->pkt_type = HCI_EVENT_PKT;
1812
1813 return hci_recv_frame(hdev, skb);
1814}
1815
1816static int btusb_recv_bulk_intel(struct btusb_data *data, void *buffer,
1817 int count)
1818{
1819 /* When the device is in bootloader mode, then it can send
1820 * events via the bulk endpoint. These events are treated the
1821 * same way as the ones received from the interrupt endpoint.
1822 */
1823 if (test_bit(BTUSB_BOOTLOADER, &data->flags))
1824 return btusb_recv_intr(data, buffer, count);
1825
1826 return btusb_recv_bulk(data, buffer, count);
1827}
1828
1829static int btusb_recv_event_intel(struct hci_dev *hdev, struct sk_buff *skb)
1830{
1831 struct btusb_data *data = hci_get_drvdata(hdev);
1832
1833 if (test_bit(BTUSB_BOOTLOADER, &data->flags)) {
1834 struct hci_event_hdr *hdr = (void *)skb->data;
1835
1836 /* When the firmware loading completes the device sends
1837 * out a vendor specific event indicating the result of
1838 * the firmware loading.
1839 */
1840 if (skb->len == 7 && hdr->evt == 0xff && hdr->plen == 0x05 &&
1841 skb->data[2] == 0x06) {
1842 if (skb->data[3] != 0x00)
1843 test_bit(BTUSB_FIRMWARE_FAILED, &data->flags);
1844
Marcel Holtmannce6bb922015-01-28 01:58:40 -08001845 if (test_and_clear_bit(BTUSB_DOWNLOADING,
1846 &data->flags) &&
Johan Hedberga087a982015-01-30 10:58:54 +02001847 test_bit(BTUSB_FIRMWARE_LOADED, &data->flags)) {
1848 smp_mb__after_atomic();
1849 wake_up_bit(&data->flags, BTUSB_DOWNLOADING);
1850 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001851 }
1852
1853 /* When switching to the operational firmware the device
1854 * sends a vendor specific event indicating that the bootup
1855 * completed.
1856 */
1857 if (skb->len == 9 && hdr->evt == 0xff && hdr->plen == 0x07 &&
1858 skb->data[2] == 0x02) {
Johan Hedbergfad70972015-01-30 10:58:55 +02001859 if (test_and_clear_bit(BTUSB_BOOTING, &data->flags)) {
1860 smp_mb__after_atomic();
1861 wake_up_bit(&data->flags, BTUSB_BOOTING);
1862 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001863 }
1864 }
1865
1866 return hci_recv_frame(hdev, skb);
1867}
1868
1869static int btusb_send_frame_intel(struct hci_dev *hdev, struct sk_buff *skb)
1870{
1871 struct btusb_data *data = hci_get_drvdata(hdev);
1872 struct urb *urb;
1873
1874 BT_DBG("%s", hdev->name);
1875
1876 if (!test_bit(HCI_RUNNING, &hdev->flags))
1877 return -EBUSY;
1878
1879 switch (bt_cb(skb)->pkt_type) {
1880 case HCI_COMMAND_PKT:
1881 if (test_bit(BTUSB_BOOTLOADER, &data->flags)) {
1882 struct hci_command_hdr *cmd = (void *)skb->data;
1883 __u16 opcode = le16_to_cpu(cmd->opcode);
1884
1885 /* When in bootloader mode and the command 0xfc09
1886 * is received, it needs to be send down the
1887 * bulk endpoint. So allocate a bulk URB instead.
1888 */
1889 if (opcode == 0xfc09)
1890 urb = alloc_bulk_urb(hdev, skb);
1891 else
1892 urb = alloc_ctrl_urb(hdev, skb);
1893
1894 /* When the 0xfc01 command is issued to boot into
1895 * the operational firmware, it will actually not
1896 * send a command complete event. To keep the flow
1897 * control working inject that event here.
1898 */
1899 if (opcode == 0xfc01)
1900 inject_cmd_complete(hdev, opcode);
1901 } else {
1902 urb = alloc_ctrl_urb(hdev, skb);
1903 }
1904 if (IS_ERR(urb))
1905 return PTR_ERR(urb);
1906
1907 hdev->stat.cmd_tx++;
1908 return submit_or_queue_tx_urb(hdev, urb);
1909
1910 case HCI_ACLDATA_PKT:
1911 urb = alloc_bulk_urb(hdev, skb);
1912 if (IS_ERR(urb))
1913 return PTR_ERR(urb);
1914
1915 hdev->stat.acl_tx++;
1916 return submit_or_queue_tx_urb(hdev, urb);
1917
1918 case HCI_SCODATA_PKT:
1919 if (hci_conn_num(hdev, SCO_LINK) < 1)
1920 return -ENODEV;
1921
1922 urb = alloc_isoc_urb(hdev, skb);
1923 if (IS_ERR(urb))
1924 return PTR_ERR(urb);
1925
1926 hdev->stat.sco_tx++;
1927 return submit_tx_urb(hdev, urb);
1928 }
1929
1930 return -EILSEQ;
1931}
1932
1933static int btusb_intel_secure_send(struct hci_dev *hdev, u8 fragment_type,
1934 u32 plen, const void *param)
1935{
1936 while (plen > 0) {
1937 struct sk_buff *skb;
1938 u8 cmd_param[253], fragment_len = (plen > 252) ? 252 : plen;
1939
1940 cmd_param[0] = fragment_type;
1941 memcpy(cmd_param + 1, param, fragment_len);
1942
1943 skb = __hci_cmd_sync(hdev, 0xfc09, fragment_len + 1,
1944 cmd_param, HCI_INIT_TIMEOUT);
1945 if (IS_ERR(skb))
1946 return PTR_ERR(skb);
1947
1948 kfree_skb(skb);
1949
1950 plen -= fragment_len;
1951 param += fragment_len;
1952 }
1953
1954 return 0;
1955}
1956
1957static void btusb_intel_version_info(struct hci_dev *hdev,
1958 struct intel_version *ver)
1959{
1960 const char *variant;
1961
1962 switch (ver->fw_variant) {
1963 case 0x06:
1964 variant = "Bootloader";
1965 break;
1966 case 0x23:
1967 variant = "Firmware";
1968 break;
1969 default:
1970 return;
1971 }
1972
1973 BT_INFO("%s: %s revision %u.%u build %u week %u %u", hdev->name,
1974 variant, ver->fw_revision >> 4, ver->fw_revision & 0x0f,
1975 ver->fw_build_num, ver->fw_build_ww, 2000 + ver->fw_build_yy);
1976}
1977
1978static int btusb_setup_intel_new(struct hci_dev *hdev)
1979{
1980 static const u8 reset_param[] = { 0x00, 0x01, 0x00, 0x01,
1981 0x00, 0x08, 0x04, 0x00 };
1982 struct btusb_data *data = hci_get_drvdata(hdev);
1983 struct sk_buff *skb;
1984 struct intel_version *ver;
1985 struct intel_boot_params *params;
1986 const struct firmware *fw;
1987 const u8 *fw_ptr;
1988 char fwname[64];
1989 ktime_t calltime, delta, rettime;
1990 unsigned long long duration;
1991 int err;
1992
1993 BT_DBG("%s", hdev->name);
1994
1995 calltime = ktime_get();
1996
1997 /* Read the Intel version information to determine if the device
1998 * is in bootloader mode or if it already has operational firmware
1999 * loaded.
2000 */
2001 skb = __hci_cmd_sync(hdev, 0xfc05, 0, NULL, HCI_INIT_TIMEOUT);
2002 if (IS_ERR(skb)) {
2003 BT_ERR("%s: Reading Intel version information failed (%ld)",
2004 hdev->name, PTR_ERR(skb));
2005 return PTR_ERR(skb);
2006 }
2007
2008 if (skb->len != sizeof(*ver)) {
2009 BT_ERR("%s: Intel version event size mismatch", hdev->name);
2010 kfree_skb(skb);
2011 return -EILSEQ;
2012 }
2013
2014 ver = (struct intel_version *)skb->data;
2015 if (ver->status) {
2016 BT_ERR("%s: Intel version command failure (%02x)",
2017 hdev->name, ver->status);
2018 err = -bt_to_errno(ver->status);
2019 kfree_skb(skb);
2020 return err;
2021 }
2022
2023 /* The hardware platform number has a fixed value of 0x37 and
2024 * for now only accept this single value.
2025 */
2026 if (ver->hw_platform != 0x37) {
2027 BT_ERR("%s: Unsupported Intel hardware platform (%u)",
2028 hdev->name, ver->hw_platform);
2029 kfree_skb(skb);
2030 return -EINVAL;
2031 }
2032
2033 /* At the moment only the hardware variant iBT 3.0 (LnP/SfP) is
2034 * supported by this firmware loading method. This check has been
2035 * put in place to ensure correct forward compatibility options
2036 * when newer hardware variants come along.
2037 */
2038 if (ver->hw_variant != 0x0b) {
2039 BT_ERR("%s: Unsupported Intel hardware variant (%u)",
2040 hdev->name, ver->hw_variant);
2041 kfree_skb(skb);
2042 return -EINVAL;
2043 }
2044
2045 btusb_intel_version_info(hdev, ver);
2046
2047 /* The firmware variant determines if the device is in bootloader
2048 * mode or is running operational firmware. The value 0x06 identifies
2049 * the bootloader and the value 0x23 identifies the operational
2050 * firmware.
2051 *
2052 * When the operational firmware is already present, then only
2053 * the check for valid Bluetooth device address is needed. This
2054 * determines if the device will be added as configured or
2055 * unconfigured controller.
2056 *
2057 * It is not possible to use the Secure Boot Parameters in this
2058 * case since that command is only available in bootloader mode.
2059 */
2060 if (ver->fw_variant == 0x23) {
2061 kfree_skb(skb);
2062 clear_bit(BTUSB_BOOTLOADER, &data->flags);
2063 btusb_check_bdaddr_intel(hdev);
2064 return 0;
2065 }
2066
2067 /* If the device is not in bootloader mode, then the only possible
2068 * choice is to return an error and abort the device initialization.
2069 */
2070 if (ver->fw_variant != 0x06) {
2071 BT_ERR("%s: Unsupported Intel firmware variant (%u)",
2072 hdev->name, ver->fw_variant);
2073 kfree_skb(skb);
2074 return -ENODEV;
2075 }
2076
2077 kfree_skb(skb);
2078
2079 /* Read the secure boot parameters to identify the operating
2080 * details of the bootloader.
2081 */
2082 skb = __hci_cmd_sync(hdev, 0xfc0d, 0, NULL, HCI_INIT_TIMEOUT);
2083 if (IS_ERR(skb)) {
2084 BT_ERR("%s: Reading Intel boot parameters failed (%ld)",
2085 hdev->name, PTR_ERR(skb));
2086 return PTR_ERR(skb);
2087 }
2088
2089 if (skb->len != sizeof(*params)) {
2090 BT_ERR("%s: Intel boot parameters size mismatch", hdev->name);
2091 kfree_skb(skb);
2092 return -EILSEQ;
2093 }
2094
2095 params = (struct intel_boot_params *)skb->data;
2096 if (params->status) {
2097 BT_ERR("%s: Intel boot parameters command failure (%02x)",
2098 hdev->name, params->status);
2099 err = -bt_to_errno(params->status);
2100 kfree_skb(skb);
2101 return err;
2102 }
2103
2104 BT_INFO("%s: Device revision is %u", hdev->name,
2105 le16_to_cpu(params->dev_revid));
2106
2107 BT_INFO("%s: Secure boot is %s", hdev->name,
2108 params->secure_boot ? "enabled" : "disabled");
2109
2110 BT_INFO("%s: Minimum firmware build %u week %u %u", hdev->name,
2111 params->min_fw_build_nn, params->min_fw_build_cw,
2112 2000 + params->min_fw_build_yy);
2113
2114 /* It is required that every single firmware fragment is acknowledged
2115 * with a command complete event. If the boot parameters indicate
2116 * that this bootloader does not send them, then abort the setup.
2117 */
2118 if (params->limited_cce != 0x00) {
2119 BT_ERR("%s: Unsupported Intel firmware loading method (%u)",
2120 hdev->name, params->limited_cce);
2121 kfree_skb(skb);
2122 return -EINVAL;
2123 }
2124
2125 /* If the OTP has no valid Bluetooth device address, then there will
2126 * also be no valid address for the operational firmware.
2127 */
2128 if (!bacmp(&params->otp_bdaddr, BDADDR_ANY)) {
2129 BT_INFO("%s: No device address configured", hdev->name);
2130 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
2131 }
2132
2133 /* With this Intel bootloader only the hardware variant and device
2134 * revision information are used to select the right firmware.
2135 *
2136 * Currently this bootloader support is limited to hardware variant
2137 * iBT 3.0 (LnP/SfP) which is identified by the value 11 (0x0b).
2138 */
2139 snprintf(fwname, sizeof(fwname), "intel/ibt-11-%u.sfi",
2140 le16_to_cpu(params->dev_revid));
2141
2142 err = request_firmware(&fw, fwname, &hdev->dev);
2143 if (err < 0) {
2144 BT_ERR("%s: Failed to load Intel firmware file (%d)",
2145 hdev->name, err);
2146 kfree_skb(skb);
2147 return err;
2148 }
2149
2150 BT_INFO("%s: Found device firmware: %s", hdev->name, fwname);
2151
2152 kfree_skb(skb);
2153
2154 if (fw->size < 644) {
2155 BT_ERR("%s: Invalid size of firmware file (%zu)",
2156 hdev->name, fw->size);
2157 err = -EBADF;
2158 goto done;
2159 }
2160
2161 set_bit(BTUSB_DOWNLOADING, &data->flags);
2162
2163 /* Start the firmware download transaction with the Init fragment
2164 * represented by the 128 bytes of CSS header.
2165 */
2166 err = btusb_intel_secure_send(hdev, 0x00, 128, fw->data);
2167 if (err < 0) {
2168 BT_ERR("%s: Failed to send firmware header (%d)",
2169 hdev->name, err);
2170 goto done;
2171 }
2172
2173 /* Send the 256 bytes of public key information from the firmware
2174 * as the PKey fragment.
2175 */
2176 err = btusb_intel_secure_send(hdev, 0x03, 256, fw->data + 128);
2177 if (err < 0) {
2178 BT_ERR("%s: Failed to send firmware public key (%d)",
2179 hdev->name, err);
2180 goto done;
2181 }
2182
2183 /* Send the 256 bytes of signature information from the firmware
2184 * as the Sign fragment.
2185 */
2186 err = btusb_intel_secure_send(hdev, 0x02, 256, fw->data + 388);
2187 if (err < 0) {
2188 BT_ERR("%s: Failed to send firmware signature (%d)",
2189 hdev->name, err);
2190 goto done;
2191 }
2192
2193 fw_ptr = fw->data + 644;
2194
2195 while (fw_ptr - fw->data < fw->size) {
2196 struct hci_command_hdr *cmd = (void *)fw_ptr;
2197 u8 cmd_len;
2198
2199 cmd_len = sizeof(*cmd) + cmd->plen;
2200
2201 /* Send each command from the firmware data buffer as
2202 * a single Data fragment.
2203 */
2204 err = btusb_intel_secure_send(hdev, 0x01, cmd_len, fw_ptr);
2205 if (err < 0) {
2206 BT_ERR("%s: Failed to send firmware data (%d)",
2207 hdev->name, err);
2208 goto done;
2209 }
2210
2211 fw_ptr += cmd_len;
2212 }
2213
Marcel Holtmannce6bb922015-01-28 01:58:40 -08002214 set_bit(BTUSB_FIRMWARE_LOADED, &data->flags);
2215
Johan Hedberga087a982015-01-30 10:58:54 +02002216 BT_INFO("%s: Waiting for firmware download to complete", hdev->name);
2217
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002218 /* Before switching the device into operational mode and with that
2219 * booting the loaded firmware, wait for the bootloader notification
2220 * that all fragments have been successfully received.
2221 *
Johan Hedberga087a982015-01-30 10:58:54 +02002222 * When the event processing receives the notification, then the
2223 * BTUSB_DOWNLOADING flag will be cleared.
2224 *
2225 * The firmware loading should not take longer than 5 seconds
2226 * and thus just timeout if that happens and fail the setup
2227 * of this device.
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002228 */
Johan Hedberg129a7692015-02-14 09:33:35 +02002229 err = wait_on_bit_timeout(&data->flags, BTUSB_DOWNLOADING,
2230 TASK_INTERRUPTIBLE,
2231 msecs_to_jiffies(5000));
Johan Hedberga087a982015-01-30 10:58:54 +02002232 if (err == 1) {
2233 BT_ERR("%s: Firmware loading interrupted", hdev->name);
2234 err = -EINTR;
2235 goto done;
2236 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002237
Johan Hedberga087a982015-01-30 10:58:54 +02002238 if (err) {
2239 BT_ERR("%s: Firmware loading timeout", hdev->name);
2240 err = -ETIMEDOUT;
2241 goto done;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002242 }
2243
2244 if (test_bit(BTUSB_FIRMWARE_FAILED, &data->flags)) {
2245 BT_ERR("%s: Firmware loading failed", hdev->name);
2246 err = -ENOEXEC;
2247 goto done;
2248 }
2249
2250 rettime = ktime_get();
2251 delta = ktime_sub(rettime, calltime);
2252 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
2253
2254 BT_INFO("%s: Firmware loaded in %llu usecs", hdev->name, duration);
2255
2256done:
2257 release_firmware(fw);
2258
2259 if (err < 0)
2260 return err;
2261
2262 calltime = ktime_get();
2263
2264 set_bit(BTUSB_BOOTING, &data->flags);
2265
2266 skb = __hci_cmd_sync(hdev, 0xfc01, sizeof(reset_param), reset_param,
2267 HCI_INIT_TIMEOUT);
2268 if (IS_ERR(skb))
2269 return PTR_ERR(skb);
2270
2271 kfree_skb(skb);
2272
2273 /* The bootloader will not indicate when the device is ready. This
2274 * is done by the operational firmware sending bootup notification.
Johan Hedbergfad70972015-01-30 10:58:55 +02002275 *
2276 * Booting into operational firmware should not take longer than
2277 * 1 second. However if that happens, then just fail the setup
2278 * since something went wrong.
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002279 */
Johan Hedbergfad70972015-01-30 10:58:55 +02002280 BT_INFO("%s: Waiting for device to boot", hdev->name);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002281
Johan Hedberg129a7692015-02-14 09:33:35 +02002282 err = wait_on_bit_timeout(&data->flags, BTUSB_BOOTING,
2283 TASK_INTERRUPTIBLE,
2284 msecs_to_jiffies(1000));
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002285
Johan Hedbergfad70972015-01-30 10:58:55 +02002286 if (err == 1) {
2287 BT_ERR("%s: Device boot interrupted", hdev->name);
2288 return -EINTR;
2289 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002290
Johan Hedbergfad70972015-01-30 10:58:55 +02002291 if (err) {
2292 BT_ERR("%s: Device boot timeout", hdev->name);
2293 return -ETIMEDOUT;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002294 }
2295
2296 rettime = ktime_get();
2297 delta = ktime_sub(rettime, calltime);
2298 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
2299
2300 BT_INFO("%s: Device booted in %llu usecs", hdev->name, duration);
2301
2302 clear_bit(BTUSB_BOOTLOADER, &data->flags);
2303
2304 return 0;
2305}
2306
Marcel Holtmann385a7682015-01-28 11:09:56 -08002307static void btusb_hw_error_intel(struct hci_dev *hdev, u8 code)
2308{
2309 struct sk_buff *skb;
2310 u8 type = 0x00;
2311
2312 BT_ERR("%s: Hardware error 0x%2.2x", hdev->name, code);
2313
2314 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
2315 if (IS_ERR(skb)) {
2316 BT_ERR("%s: Reset after hardware error failed (%ld)",
2317 hdev->name, PTR_ERR(skb));
2318 return;
2319 }
2320 kfree_skb(skb);
2321
2322 skb = __hci_cmd_sync(hdev, 0xfc22, 1, &type, HCI_INIT_TIMEOUT);
2323 if (IS_ERR(skb)) {
2324 BT_ERR("%s: Retrieving Intel exception info failed (%ld)",
2325 hdev->name, PTR_ERR(skb));
2326 return;
2327 }
2328
2329 if (skb->len != 13) {
2330 BT_ERR("%s: Exception info size mismatch", hdev->name);
2331 kfree_skb(skb);
2332 return;
2333 }
2334
2335 if (skb->data[0] != 0x00) {
2336 BT_ERR("%s: Exception info command failure (%02x)",
2337 hdev->name, skb->data[0]);
2338 kfree_skb(skb);
2339 return;
2340 }
2341
2342 BT_ERR("%s: Exception info %s", hdev->name, (char *)(skb->data + 1));
2343
2344 kfree_skb(skb);
2345}
2346
Marcel Holtmanncb8d6592014-07-02 11:25:25 +02002347static int btusb_set_bdaddr_intel(struct hci_dev *hdev, const bdaddr_t *bdaddr)
2348{
2349 struct sk_buff *skb;
2350 long ret;
2351
2352 skb = __hci_cmd_sync(hdev, 0xfc31, 6, bdaddr, HCI_INIT_TIMEOUT);
2353 if (IS_ERR(skb)) {
2354 ret = PTR_ERR(skb);
2355 BT_ERR("%s: changing Intel device address failed (%ld)",
Marcel Holtmann89e75332014-09-16 04:44:50 +02002356 hdev->name, ret);
Marcel Holtmanncb8d6592014-07-02 11:25:25 +02002357 return ret;
2358 }
2359 kfree_skb(skb);
2360
2361 return 0;
2362}
2363
Tedd Ho-Jeong Anbfbd45e2015-02-13 09:20:52 -08002364static int btusb_shutdown_intel(struct hci_dev *hdev)
2365{
2366 struct sk_buff *skb;
2367 long ret;
2368
2369 /* Some platforms have an issue with BT LED when the interface is
2370 * down or BT radio is turned off, which takes 5 seconds to BT LED
2371 * goes off. This command turns off the BT LED immediately.
2372 */
2373 skb = __hci_cmd_sync(hdev, 0xfc3f, 0, NULL, HCI_INIT_TIMEOUT);
2374 if (IS_ERR(skb)) {
2375 ret = PTR_ERR(skb);
2376 BT_ERR("%s: turning off Intel device LED failed (%ld)",
2377 hdev->name, ret);
2378 return ret;
2379 }
2380 kfree_skb(skb);
2381
2382 return 0;
2383}
2384
Amitkumar Karwarae8df492014-07-18 14:47:06 -07002385static int btusb_set_bdaddr_marvell(struct hci_dev *hdev,
2386 const bdaddr_t *bdaddr)
2387{
2388 struct sk_buff *skb;
2389 u8 buf[8];
2390 long ret;
2391
2392 buf[0] = 0xfe;
2393 buf[1] = sizeof(bdaddr_t);
2394 memcpy(buf + 2, bdaddr, sizeof(bdaddr_t));
2395
2396 skb = __hci_cmd_sync(hdev, 0xfc22, sizeof(buf), buf, HCI_INIT_TIMEOUT);
2397 if (IS_ERR(skb)) {
2398 ret = PTR_ERR(skb);
2399 BT_ERR("%s: changing Marvell device address failed (%ld)",
2400 hdev->name, ret);
2401 return ret;
2402 }
2403 kfree_skb(skb);
2404
2405 return 0;
2406}
2407
Marcel Holtmann18835df2015-02-14 23:08:47 -08002408static const struct {
2409 u16 subver;
2410 const char *name;
2411} bcm_subver_table[] = {
2412 { 0x210b, "BCM43142A0" }, /* 001.001.011 */
2413 { 0x2112, "BCM4314A0" }, /* 001.001.018 */
2414 { 0x2118, "BCM20702A0" }, /* 001.001.024 */
2415 { 0x2126, "BCM4335A0" }, /* 001.001.038 */
2416 { 0x220e, "BCM20702A1" }, /* 001.002.014 */
2417 { 0x230f, "BCM4354A2" }, /* 001.003.015 */
2418 { 0x4106, "BCM4335B0" }, /* 002.001.006 */
2419 { 0x410e, "BCM20702B0" }, /* 002.001.014 */
2420 { 0x6109, "BCM4335C0" }, /* 003.001.009 */
2421 { 0x610c, "BCM4354" }, /* 003.001.012 */
2422 { }
2423};
2424
Marcel Holtmannc8abb732014-07-02 12:38:22 +02002425#define BDADDR_BCM20702A0 (&(bdaddr_t) {{0x00, 0xa0, 0x02, 0x70, 0x20, 0x00}})
2426
Petri Gynther10d4c672014-05-08 15:50:01 -07002427static int btusb_setup_bcm_patchram(struct hci_dev *hdev)
2428{
2429 struct btusb_data *data = hci_get_drvdata(hdev);
2430 struct usb_device *udev = data->udev;
2431 char fw_name[64];
2432 const struct firmware *fw;
2433 const u8 *fw_ptr;
2434 size_t fw_size;
2435 const struct hci_command_hdr *cmd;
2436 const u8 *cmd_param;
Marcel Holtmann18835df2015-02-14 23:08:47 -08002437 u16 opcode, subver, rev;
2438 const char *hw_name = NULL;
Petri Gynther10d4c672014-05-08 15:50:01 -07002439 struct sk_buff *skb;
2440 struct hci_rp_read_local_version *ver;
Marcel Holtmannc8abb732014-07-02 12:38:22 +02002441 struct hci_rp_read_bd_addr *bda;
Petri Gynther10d4c672014-05-08 15:50:01 -07002442 long ret;
Marcel Holtmann18835df2015-02-14 23:08:47 -08002443 int i;
Petri Gynther10d4c672014-05-08 15:50:01 -07002444
2445 /* Reset */
2446 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
2447 if (IS_ERR(skb)) {
2448 ret = PTR_ERR(skb);
2449 BT_ERR("%s: HCI_OP_RESET failed (%ld)", hdev->name, ret);
Marcel Holtmann18835df2015-02-14 23:08:47 -08002450 return ret;
Petri Gynther10d4c672014-05-08 15:50:01 -07002451 }
2452 kfree_skb(skb);
2453
2454 /* Read Local Version Info */
Daniel Drake6576fe42015-02-23 18:16:47 -06002455 skb = btusb_read_local_version(hdev);
2456 if (IS_ERR(skb))
2457 return PTR_ERR(skb);
Petri Gynther10d4c672014-05-08 15:50:01 -07002458
Marcel Holtmann89e75332014-09-16 04:44:50 +02002459 ver = (struct hci_rp_read_local_version *)skb->data;
Marcel Holtmann18835df2015-02-14 23:08:47 -08002460 rev = le16_to_cpu(ver->hci_rev);
2461 subver = le16_to_cpu(ver->lmp_subver);
Petri Gynther10d4c672014-05-08 15:50:01 -07002462 kfree_skb(skb);
2463
Marcel Holtmann15c32c52015-03-20 17:16:36 -07002464 /* Read Verbose Config Version Info */
2465 skb = __hci_cmd_sync(hdev, 0xfc79, 0, NULL, HCI_INIT_TIMEOUT);
2466 if (IS_ERR(skb)) {
2467 ret = PTR_ERR(skb);
2468 BT_ERR("%s: BCM: Read Verbose Version failed (%ld)",
2469 hdev->name, ret);
2470 return ret;
2471 }
2472
2473 if (skb->len != 7) {
2474 BT_ERR("%s: BCM: Read Verbose Version event length mismatch",
2475 hdev->name);
2476 kfree_skb(skb);
2477 return -EIO;
2478 }
2479
2480 BT_INFO("%s: BCM: chip id %u", hdev->name, skb->data[1]);
2481 kfree_skb(skb);
2482
Marcel Holtmann18835df2015-02-14 23:08:47 -08002483 for (i = 0; bcm_subver_table[i].name; i++) {
2484 if (subver == bcm_subver_table[i].subver) {
2485 hw_name = bcm_subver_table[i].name;
2486 break;
2487 }
2488 }
2489
2490 BT_INFO("%s: %s (%3.3u.%3.3u.%3.3u) build %4.4u", hdev->name,
2491 hw_name ? : "BCM", (subver & 0x7000) >> 13,
2492 (subver & 0x1f00) >> 8, (subver & 0x00ff), rev & 0x0fff);
2493
2494 snprintf(fw_name, sizeof(fw_name), "brcm/%s-%4.4x-%4.4x.hcd",
2495 hw_name ? : "BCM",
2496 le16_to_cpu(udev->descriptor.idVendor),
2497 le16_to_cpu(udev->descriptor.idProduct));
2498
2499 ret = request_firmware(&fw, fw_name, &hdev->dev);
2500 if (ret < 0) {
2501 BT_INFO("%s: BCM: patch %s not found", hdev->name, fw_name);
2502 return 0;
2503 }
2504
Petri Gynther10d4c672014-05-08 15:50:01 -07002505 /* Start Download */
2506 skb = __hci_cmd_sync(hdev, 0xfc2e, 0, NULL, HCI_INIT_TIMEOUT);
2507 if (IS_ERR(skb)) {
2508 ret = PTR_ERR(skb);
2509 BT_ERR("%s: BCM: Download Minidrv command failed (%ld)",
Marcel Holtmann89e75332014-09-16 04:44:50 +02002510 hdev->name, ret);
Petri Gynther10d4c672014-05-08 15:50:01 -07002511 goto reset_fw;
2512 }
2513 kfree_skb(skb);
2514
2515 /* 50 msec delay after Download Minidrv completes */
2516 msleep(50);
2517
2518 fw_ptr = fw->data;
2519 fw_size = fw->size;
2520
2521 while (fw_size >= sizeof(*cmd)) {
Marcel Holtmann89e75332014-09-16 04:44:50 +02002522 cmd = (struct hci_command_hdr *)fw_ptr;
Petri Gynther10d4c672014-05-08 15:50:01 -07002523 fw_ptr += sizeof(*cmd);
2524 fw_size -= sizeof(*cmd);
2525
2526 if (fw_size < cmd->plen) {
2527 BT_ERR("%s: BCM: patch %s is corrupted",
Marcel Holtmann89e75332014-09-16 04:44:50 +02002528 hdev->name, fw_name);
Petri Gynther10d4c672014-05-08 15:50:01 -07002529 ret = -EINVAL;
2530 goto reset_fw;
2531 }
2532
2533 cmd_param = fw_ptr;
2534 fw_ptr += cmd->plen;
2535 fw_size -= cmd->plen;
2536
2537 opcode = le16_to_cpu(cmd->opcode);
2538
2539 skb = __hci_cmd_sync(hdev, opcode, cmd->plen, cmd_param,
2540 HCI_INIT_TIMEOUT);
2541 if (IS_ERR(skb)) {
2542 ret = PTR_ERR(skb);
2543 BT_ERR("%s: BCM: patch command %04x failed (%ld)",
Marcel Holtmann89e75332014-09-16 04:44:50 +02002544 hdev->name, opcode, ret);
Petri Gynther10d4c672014-05-08 15:50:01 -07002545 goto reset_fw;
2546 }
2547 kfree_skb(skb);
2548 }
2549
2550 /* 250 msec delay after Launch Ram completes */
2551 msleep(250);
2552
2553reset_fw:
2554 /* Reset */
2555 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
2556 if (IS_ERR(skb)) {
2557 ret = PTR_ERR(skb);
2558 BT_ERR("%s: HCI_OP_RESET failed (%ld)", hdev->name, ret);
2559 goto done;
2560 }
2561 kfree_skb(skb);
2562
2563 /* Read Local Version Info */
Daniel Drake6576fe42015-02-23 18:16:47 -06002564 skb = btusb_read_local_version(hdev);
Petri Gynther10d4c672014-05-08 15:50:01 -07002565 if (IS_ERR(skb)) {
2566 ret = PTR_ERR(skb);
Petri Gynther10d4c672014-05-08 15:50:01 -07002567 goto done;
2568 }
2569
Marcel Holtmann89e75332014-09-16 04:44:50 +02002570 ver = (struct hci_rp_read_local_version *)skb->data;
Marcel Holtmann18835df2015-02-14 23:08:47 -08002571 rev = le16_to_cpu(ver->hci_rev);
2572 subver = le16_to_cpu(ver->lmp_subver);
Petri Gynther10d4c672014-05-08 15:50:01 -07002573 kfree_skb(skb);
2574
Marcel Holtmann18835df2015-02-14 23:08:47 -08002575 BT_INFO("%s: %s (%3.3u.%3.3u.%3.3u) build %4.4u", hdev->name,
2576 hw_name ? : "BCM", (subver & 0x7000) >> 13,
2577 (subver & 0x1f00) >> 8, (subver & 0x00ff), rev & 0x0fff);
2578
Marcel Holtmannc8abb732014-07-02 12:38:22 +02002579 /* Read BD Address */
2580 skb = __hci_cmd_sync(hdev, HCI_OP_READ_BD_ADDR, 0, NULL,
2581 HCI_INIT_TIMEOUT);
2582 if (IS_ERR(skb)) {
2583 ret = PTR_ERR(skb);
2584 BT_ERR("%s: HCI_OP_READ_BD_ADDR failed (%ld)",
Marcel Holtmann89e75332014-09-16 04:44:50 +02002585 hdev->name, ret);
Marcel Holtmannc8abb732014-07-02 12:38:22 +02002586 goto done;
2587 }
2588
2589 if (skb->len != sizeof(*bda)) {
2590 BT_ERR("%s: HCI_OP_READ_BD_ADDR event length mismatch",
Marcel Holtmann89e75332014-09-16 04:44:50 +02002591 hdev->name);
Marcel Holtmannc8abb732014-07-02 12:38:22 +02002592 kfree_skb(skb);
2593 ret = -EIO;
2594 goto done;
2595 }
2596
Marcel Holtmann89e75332014-09-16 04:44:50 +02002597 bda = (struct hci_rp_read_bd_addr *)skb->data;
Marcel Holtmannc8abb732014-07-02 12:38:22 +02002598 if (bda->status) {
2599 BT_ERR("%s: HCI_OP_READ_BD_ADDR error status (%02x)",
2600 hdev->name, bda->status);
2601 kfree_skb(skb);
2602 ret = -bt_to_errno(bda->status);
2603 goto done;
2604 }
2605
2606 /* The address 00:20:70:02:A0:00 indicates a BCM20702A0 controller
2607 * with no configured address.
2608 */
Marcel Holtmann849e5082014-07-04 16:54:39 +02002609 if (!bacmp(&bda->bdaddr, BDADDR_BCM20702A0)) {
Marcel Holtmannc8abb732014-07-02 12:38:22 +02002610 BT_INFO("%s: BCM: using default device address (%pMR)",
2611 hdev->name, &bda->bdaddr);
Marcel Holtmann849e5082014-07-04 16:54:39 +02002612 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
2613 }
Marcel Holtmannc8abb732014-07-02 12:38:22 +02002614
2615 kfree_skb(skb);
2616
Petri Gynther10d4c672014-05-08 15:50:01 -07002617done:
2618 release_firmware(fw);
2619
2620 return ret;
2621}
2622
Marcel Holtmannabbaf502014-07-02 00:53:48 +02002623static int btusb_set_bdaddr_bcm(struct hci_dev *hdev, const bdaddr_t *bdaddr)
2624{
2625 struct sk_buff *skb;
2626 long ret;
2627
2628 skb = __hci_cmd_sync(hdev, 0xfc01, 6, bdaddr, HCI_INIT_TIMEOUT);
2629 if (IS_ERR(skb)) {
2630 ret = PTR_ERR(skb);
2631 BT_ERR("%s: BCM: Change address command failed (%ld)",
Marcel Holtmann89e75332014-09-16 04:44:50 +02002632 hdev->name, ret);
Marcel Holtmannabbaf502014-07-02 00:53:48 +02002633 return ret;
2634 }
2635 kfree_skb(skb);
2636
2637 return 0;
2638}
2639
Marcel Holtmann17b27722015-03-22 15:52:38 +01002640static int btusb_setup_bcm_apple(struct hci_dev *hdev)
2641{
2642 struct sk_buff *skb;
2643 int err;
2644
2645 /* Read Verbose Config Version Info */
2646 skb = __hci_cmd_sync(hdev, 0xfc79, 0, NULL, HCI_INIT_TIMEOUT);
2647 if (IS_ERR(skb)) {
2648 err = PTR_ERR(skb);
2649 BT_ERR("%s: BCM: Read Verbose Version failed (%d)",
2650 hdev->name, err);
2651 return err;
2652 }
2653
2654 if (skb->len != 7) {
2655 BT_ERR("%s: BCM: Read Verbose Version event length mismatch",
2656 hdev->name);
2657 kfree_skb(skb);
2658 return -EIO;
2659 }
2660
2661 BT_INFO("%s: BCM: chip id %u build %4.4u", hdev->name, skb->data[1],
2662 get_unaligned_le16(skb->data + 5));
2663 kfree_skb(skb);
2664
2665 return 0;
2666}
2667
Toshi Kikuchi58592232014-12-12 10:58:05 -08002668static int btusb_set_bdaddr_ath3012(struct hci_dev *hdev,
2669 const bdaddr_t *bdaddr)
2670{
2671 struct sk_buff *skb;
2672 u8 buf[10];
2673 long ret;
2674
2675 buf[0] = 0x01;
2676 buf[1] = 0x01;
2677 buf[2] = 0x00;
2678 buf[3] = sizeof(bdaddr_t);
2679 memcpy(buf + 4, bdaddr, sizeof(bdaddr_t));
2680
2681 skb = __hci_cmd_sync(hdev, 0xfc0b, sizeof(buf), buf, HCI_INIT_TIMEOUT);
2682 if (IS_ERR(skb)) {
2683 ret = PTR_ERR(skb);
2684 BT_ERR("%s: Change address command failed (%ld)",
2685 hdev->name, ret);
2686 return ret;
2687 }
2688 kfree_skb(skb);
2689
2690 return 0;
2691}
2692
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002693#define QCA_DFU_PACKET_LEN 4096
2694
2695#define QCA_GET_TARGET_VERSION 0x09
2696#define QCA_CHECK_STATUS 0x05
2697#define QCA_DFU_DOWNLOAD 0x01
2698
2699#define QCA_SYSCFG_UPDATED 0x40
2700#define QCA_PATCH_UPDATED 0x80
2701#define QCA_DFU_TIMEOUT 3000
2702
2703struct qca_version {
2704 __le32 rom_version;
2705 __le32 patch_version;
2706 __le32 ram_version;
2707 __le32 ref_clock;
2708 __u8 reserved[4];
2709} __packed;
2710
2711struct qca_rampatch_version {
2712 __le16 rom_version;
2713 __le16 patch_version;
2714} __packed;
2715
2716struct qca_device_info {
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002717 u32 rom_version;
2718 u8 rampatch_hdr; /* length of header in rampatch */
2719 u8 nvm_hdr; /* length of header in NVM */
2720 u8 ver_offset; /* offset of version structure in rampatch */
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002721};
2722
2723static const struct qca_device_info qca_devices_table[] = {
2724 { 0x00000100, 20, 4, 10 }, /* Rome 1.0 */
2725 { 0x00000101, 20, 4, 10 }, /* Rome 1.1 */
2726 { 0x00000201, 28, 4, 18 }, /* Rome 2.1 */
2727 { 0x00000300, 28, 4, 18 }, /* Rome 3.0 */
2728 { 0x00000302, 28, 4, 18 }, /* Rome 3.2 */
2729};
2730
2731static int btusb_qca_send_vendor_req(struct hci_dev *hdev, u8 request,
2732 void *data, u16 size)
2733{
2734 struct btusb_data *btdata = hci_get_drvdata(hdev);
2735 struct usb_device *udev = btdata->udev;
2736 int pipe, err;
2737 u8 *buf;
2738
2739 buf = kmalloc(size, GFP_KERNEL);
2740 if (!buf)
2741 return -ENOMEM;
2742
2743 /* Found some of USB hosts have IOT issues with ours so that we should
2744 * not wait until HCI layer is ready.
2745 */
2746 pipe = usb_rcvctrlpipe(udev, 0);
2747 err = usb_control_msg(udev, pipe, request, USB_TYPE_VENDOR | USB_DIR_IN,
2748 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
2749 if (err < 0) {
2750 BT_ERR("%s: Failed to access otp area (%d)", hdev->name, err);
2751 goto done;
2752 }
2753
2754 memcpy(data, buf, size);
2755
2756done:
2757 kfree(buf);
2758
2759 return err;
2760}
2761
2762static int btusb_setup_qca_download_fw(struct hci_dev *hdev,
2763 const struct firmware *firmware,
2764 size_t hdr_size)
2765{
2766 struct btusb_data *btdata = hci_get_drvdata(hdev);
2767 struct usb_device *udev = btdata->udev;
2768 size_t count, size, sent = 0;
2769 int pipe, len, err;
2770 u8 *buf;
2771
2772 buf = kmalloc(QCA_DFU_PACKET_LEN, GFP_KERNEL);
2773 if (!buf)
2774 return -ENOMEM;
2775
2776 count = firmware->size;
2777
2778 size = min_t(size_t, count, hdr_size);
2779 memcpy(buf, firmware->data, size);
2780
2781 /* USB patches should go down to controller through USB path
2782 * because binary format fits to go down through USB channel.
2783 * USB control path is for patching headers and USB bulk is for
2784 * patch body.
2785 */
2786 pipe = usb_sndctrlpipe(udev, 0);
2787 err = usb_control_msg(udev, pipe, QCA_DFU_DOWNLOAD, USB_TYPE_VENDOR,
2788 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
2789 if (err < 0) {
2790 BT_ERR("%s: Failed to send headers (%d)", hdev->name, err);
2791 goto done;
2792 }
2793
2794 sent += size;
2795 count -= size;
2796
2797 while (count) {
2798 size = min_t(size_t, count, QCA_DFU_PACKET_LEN);
2799
2800 memcpy(buf, firmware->data + sent, size);
2801
2802 pipe = usb_sndbulkpipe(udev, 0x02);
2803 err = usb_bulk_msg(udev, pipe, buf, size, &len,
2804 QCA_DFU_TIMEOUT);
2805 if (err < 0) {
2806 BT_ERR("%s: Failed to send body at %zd of %zd (%d)",
2807 hdev->name, sent, firmware->size, err);
2808 break;
2809 }
2810
2811 if (size != len) {
2812 BT_ERR("%s: Failed to get bulk buffer", hdev->name);
2813 err = -EILSEQ;
2814 break;
2815 }
2816
2817 sent += size;
2818 count -= size;
2819 }
2820
2821done:
2822 kfree(buf);
2823 return err;
2824}
2825
2826static int btusb_setup_qca_load_rampatch(struct hci_dev *hdev,
2827 struct qca_version *ver,
2828 const struct qca_device_info *info)
2829{
2830 struct qca_rampatch_version *rver;
2831 const struct firmware *fw;
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002832 u32 ver_rom, ver_patch;
2833 u16 rver_rom, rver_patch;
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002834 char fwname[64];
2835 int err;
2836
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002837 ver_rom = le32_to_cpu(ver->rom_version);
2838 ver_patch = le32_to_cpu(ver->patch_version);
2839
2840 snprintf(fwname, sizeof(fwname), "qca/rampatch_usb_%08x.bin", ver_rom);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002841
2842 err = request_firmware(&fw, fwname, &hdev->dev);
2843 if (err) {
2844 BT_ERR("%s: failed to request rampatch file: %s (%d)",
2845 hdev->name, fwname, err);
2846 return err;
2847 }
2848
2849 BT_INFO("%s: using rampatch file: %s", hdev->name, fwname);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002850
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002851 rver = (struct qca_rampatch_version *)(fw->data + info->ver_offset);
2852 rver_rom = le16_to_cpu(rver->rom_version);
2853 rver_patch = le16_to_cpu(rver->patch_version);
2854
2855 BT_INFO("%s: QCA: patch rome 0x%x build 0x%x, firmware rome 0x%x "
2856 "build 0x%x", hdev->name, rver_rom, rver_patch, ver_rom,
2857 ver_patch);
2858
2859 if (rver_rom != ver_rom || rver_patch <= ver_patch) {
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002860 BT_ERR("%s: rampatch file version did not match with firmware",
2861 hdev->name);
2862 err = -EINVAL;
2863 goto done;
2864 }
2865
2866 err = btusb_setup_qca_download_fw(hdev, fw, info->rampatch_hdr);
2867
2868done:
2869 release_firmware(fw);
2870
2871 return err;
2872}
2873
2874static int btusb_setup_qca_load_nvm(struct hci_dev *hdev,
2875 struct qca_version *ver,
2876 const struct qca_device_info *info)
2877{
2878 const struct firmware *fw;
2879 char fwname[64];
2880 int err;
2881
2882 snprintf(fwname, sizeof(fwname), "qca/nvm_usb_%08x.bin",
2883 le32_to_cpu(ver->rom_version));
2884
2885 err = request_firmware(&fw, fwname, &hdev->dev);
2886 if (err) {
2887 BT_ERR("%s: failed to request NVM file: %s (%d)",
2888 hdev->name, fwname, err);
2889 return err;
2890 }
2891
2892 BT_INFO("%s: using NVM file: %s", hdev->name, fwname);
2893
2894 err = btusb_setup_qca_download_fw(hdev, fw, info->nvm_hdr);
2895
2896 release_firmware(fw);
2897
2898 return err;
2899}
2900
2901static int btusb_setup_qca(struct hci_dev *hdev)
2902{
2903 const struct qca_device_info *info = NULL;
2904 struct qca_version ver;
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002905 u32 ver_rom;
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002906 u8 status;
2907 int i, err;
2908
2909 err = btusb_qca_send_vendor_req(hdev, QCA_GET_TARGET_VERSION, &ver,
2910 sizeof(ver));
2911 if (err < 0)
2912 return err;
2913
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002914 ver_rom = le32_to_cpu(ver.rom_version);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002915 for (i = 0; i < ARRAY_SIZE(qca_devices_table); i++) {
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002916 if (ver_rom == qca_devices_table[i].rom_version)
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002917 info = &qca_devices_table[i];
2918 }
2919 if (!info) {
2920 BT_ERR("%s: don't support firmware rome 0x%x", hdev->name,
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002921 ver_rom);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002922 return -ENODEV;
2923 }
2924
2925 err = btusb_qca_send_vendor_req(hdev, QCA_CHECK_STATUS, &status,
2926 sizeof(status));
2927 if (err < 0)
2928 return err;
2929
2930 if (!(status & QCA_PATCH_UPDATED)) {
2931 err = btusb_setup_qca_load_rampatch(hdev, &ver, info);
2932 if (err < 0)
2933 return err;
2934 }
2935
2936 if (!(status & QCA_SYSCFG_UPDATED)) {
2937 err = btusb_setup_qca_load_nvm(hdev, &ver, info);
2938 if (err < 0)
2939 return err;
2940 }
2941
2942 return 0;
2943}
2944
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002945static int btusb_probe(struct usb_interface *intf,
Marcel Holtmann89e75332014-09-16 04:44:50 +02002946 const struct usb_device_id *id)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002947{
2948 struct usb_endpoint_descriptor *ep_desc;
2949 struct btusb_data *data;
2950 struct hci_dev *hdev;
2951 int i, err;
2952
2953 BT_DBG("intf %p id %p", intf, id);
2954
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002955 /* interface numbers are hardcoded in the spec */
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002956 if (intf->cur_altsetting->desc.bInterfaceNumber != 0)
2957 return -ENODEV;
2958
2959 if (!id->driver_info) {
2960 const struct usb_device_id *match;
Marcel Holtmann89e75332014-09-16 04:44:50 +02002961
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002962 match = usb_match_id(intf, blacklist_table);
2963 if (match)
2964 id = match;
2965 }
2966
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002967 if (id->driver_info == BTUSB_IGNORE)
2968 return -ENODEV;
2969
Steven.Li2d25f8b2011-07-01 14:02:36 +08002970 if (id->driver_info & BTUSB_ATH3012) {
2971 struct usb_device *udev = interface_to_usbdev(intf);
2972
2973 /* Old firmware would otherwise let ath3k driver load
2974 * patch and sysconfig files */
2975 if (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x0001)
2976 return -ENODEV;
2977 }
2978
Sachin Kamat98921db2012-07-27 12:38:39 +05302979 data = devm_kzalloc(&intf->dev, sizeof(*data), GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002980 if (!data)
2981 return -ENOMEM;
2982
2983 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
2984 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
2985
2986 if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
2987 data->intr_ep = ep_desc;
2988 continue;
2989 }
2990
2991 if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
2992 data->bulk_tx_ep = ep_desc;
2993 continue;
2994 }
2995
2996 if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
2997 data->bulk_rx_ep = ep_desc;
2998 continue;
2999 }
3000 }
3001
Sachin Kamat98921db2012-07-27 12:38:39 +05303002 if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003003 return -ENODEV;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003004
Marcel Holtmann893ba542015-01-28 20:27:34 -08003005 if (id->driver_info & BTUSB_AMP) {
3006 data->cmdreq_type = USB_TYPE_CLASS | 0x01;
3007 data->cmdreq = 0x2b;
3008 } else {
3009 data->cmdreq_type = USB_TYPE_CLASS;
3010 data->cmdreq = 0x00;
3011 }
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01003012
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003013 data->udev = interface_to_usbdev(intf);
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02003014 data->intf = intf;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003015
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003016 INIT_WORK(&data->work, btusb_work);
Oliver Neukum7bee5492009-08-24 23:44:59 +02003017 INIT_WORK(&data->waker, btusb_waker);
Marcel Holtmann803b5832014-09-16 08:00:29 +02003018 init_usb_anchor(&data->deferred);
3019 init_usb_anchor(&data->tx_anchor);
Oliver Neukum7bee5492009-08-24 23:44:59 +02003020 spin_lock_init(&data->txlock);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003021
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003022 init_usb_anchor(&data->intr_anchor);
3023 init_usb_anchor(&data->bulk_anchor);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02003024 init_usb_anchor(&data->isoc_anchor);
Marcel Holtmann803b5832014-09-16 08:00:29 +02003025 spin_lock_init(&data->rxlock);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003026
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08003027 if (id->driver_info & BTUSB_INTEL_NEW) {
3028 data->recv_event = btusb_recv_event_intel;
3029 data->recv_bulk = btusb_recv_bulk_intel;
3030 set_bit(BTUSB_BOOTLOADER, &data->flags);
3031 } else {
3032 data->recv_event = hci_recv_frame;
3033 data->recv_bulk = btusb_recv_bulk;
3034 }
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +01003035
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003036 hdev = hci_alloc_dev();
Sachin Kamat98921db2012-07-27 12:38:39 +05303037 if (!hdev)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003038 return -ENOMEM;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003039
Marcel Holtmannc13854c2010-02-08 15:27:07 +01003040 hdev->bus = HCI_USB;
David Herrmann155961e2012-02-09 21:58:32 +01003041 hci_set_drvdata(hdev, data);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003042
Marcel Holtmann893ba542015-01-28 20:27:34 -08003043 if (id->driver_info & BTUSB_AMP)
3044 hdev->dev_type = HCI_AMP;
3045 else
3046 hdev->dev_type = HCI_BREDR;
3047
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003048 data->hdev = hdev;
3049
3050 SET_HCIDEV_DEV(hdev, &intf->dev);
3051
Marcel Holtmann9f8f9622013-04-10 08:11:35 -07003052 hdev->open = btusb_open;
3053 hdev->close = btusb_close;
3054 hdev->flush = btusb_flush;
3055 hdev->send = btusb_send_frame;
3056 hdev->notify = btusb_notify;
3057
3058 if (id->driver_info & BTUSB_BCM92035)
3059 hdev->setup = btusb_setup_bcm92035;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003060
Marcel Holtmannabbaf502014-07-02 00:53:48 +02003061 if (id->driver_info & BTUSB_BCM_PATCHRAM) {
Petri Gynther10d4c672014-05-08 15:50:01 -07003062 hdev->setup = btusb_setup_bcm_patchram;
Marcel Holtmannabbaf502014-07-02 00:53:48 +02003063 hdev->set_bdaddr = btusb_set_bdaddr_bcm;
Marcel Holtmann27c3fbe2014-11-02 20:52:24 +01003064 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
Marcel Holtmannabbaf502014-07-02 00:53:48 +02003065 }
Petri Gynther10d4c672014-05-08 15:50:01 -07003066
Marcel Holtmann17b27722015-03-22 15:52:38 +01003067 if (id->driver_info & BTUSB_BCM_APPLE) {
3068 hdev->setup = btusb_setup_bcm_apple;
3069 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
3070 }
3071
Marcel Holtmanncb8d6592014-07-02 11:25:25 +02003072 if (id->driver_info & BTUSB_INTEL) {
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07003073 hdev->setup = btusb_setup_intel;
Tedd Ho-Jeong Anbfbd45e2015-02-13 09:20:52 -08003074 hdev->shutdown = btusb_shutdown_intel;
Marcel Holtmanncb8d6592014-07-02 11:25:25 +02003075 hdev->set_bdaddr = btusb_set_bdaddr_intel;
Jakub Pawlowskic33fb9b2015-01-30 18:55:58 -08003076 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
Jakub Pawlowskic1154842015-03-17 09:04:16 -07003077 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
Marcel Holtmanncb8d6592014-07-02 11:25:25 +02003078 }
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07003079
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08003080 if (id->driver_info & BTUSB_INTEL_NEW) {
3081 hdev->send = btusb_send_frame_intel;
3082 hdev->setup = btusb_setup_intel_new;
Marcel Holtmann385a7682015-01-28 11:09:56 -08003083 hdev->hw_error = btusb_hw_error_intel;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08003084 hdev->set_bdaddr = btusb_set_bdaddr_intel;
Marcel Holtmannb970c5b2015-02-01 23:57:18 -08003085 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08003086 }
3087
Amitkumar Karwarae8df492014-07-18 14:47:06 -07003088 if (id->driver_info & BTUSB_MARVELL)
3089 hdev->set_bdaddr = btusb_set_bdaddr_marvell;
3090
Marcel Holtmann661cf882015-01-02 23:35:20 -08003091 if (id->driver_info & BTUSB_SWAVE) {
3092 set_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks);
Marcel Holtmannd57dbe72014-12-26 04:42:33 +01003093 set_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS, &hdev->quirks);
Marcel Holtmann661cf882015-01-02 23:35:20 -08003094 }
Marcel Holtmannd57dbe72014-12-26 04:42:33 +01003095
Marcel Holtmann40df7832014-07-06 13:29:58 +02003096 if (id->driver_info & BTUSB_INTEL_BOOT)
3097 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
3098
Jakub Pawlowski79f0c872015-01-29 10:38:34 -08003099 if (id->driver_info & BTUSB_ATH3012) {
Toshi Kikuchi58592232014-12-12 10:58:05 -08003100 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
Jakub Pawlowski3d50d512015-03-17 09:04:15 -07003101 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
Jakub Pawlowski79f0c872015-01-29 10:38:34 -08003102 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
3103 }
Toshi Kikuchi58592232014-12-12 10:58:05 -08003104
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003105 if (id->driver_info & BTUSB_QCA_ROME) {
3106 data->setup_on_usb = btusb_setup_qca;
3107 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
3108 }
3109
Marcel Holtmann893ba542015-01-28 20:27:34 -08003110 if (id->driver_info & BTUSB_AMP) {
3111 /* AMP controllers do not support SCO packets */
3112 data->isoc = NULL;
3113 } else {
3114 /* Interface numbers are hardcoded in the specification */
3115 data->isoc = usb_ifnum_to_if(data->udev, 1);
3116 }
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02003117
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01003118 if (!reset)
Szymon Janca6c511c2012-05-23 12:35:46 +02003119 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02003120
3121 if (force_scofix || id->driver_info & BTUSB_WRONG_SCO_MTU) {
3122 if (!disable_scofix)
3123 set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
3124 }
3125
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02003126 if (id->driver_info & BTUSB_BROKEN_ISOC)
3127 data->isoc = NULL;
3128
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01003129 if (id->driver_info & BTUSB_DIGIANSWER) {
3130 data->cmdreq_type = USB_TYPE_VENDOR;
Szymon Janca6c511c2012-05-23 12:35:46 +02003131 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01003132 }
3133
3134 if (id->driver_info & BTUSB_CSR) {
3135 struct usb_device *udev = data->udev;
Marcel Holtmann81cac642014-01-03 03:02:36 -08003136 u16 bcdDevice = le16_to_cpu(udev->descriptor.bcdDevice);
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01003137
3138 /* Old firmware would otherwise execute USB reset */
Marcel Holtmann81cac642014-01-03 03:02:36 -08003139 if (bcdDevice < 0x117)
Szymon Janca6c511c2012-05-23 12:35:46 +02003140 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmann81cac642014-01-03 03:02:36 -08003141
3142 /* Fake CSR devices with broken commands */
3143 if (bcdDevice <= 0x100)
3144 hdev->setup = btusb_setup_csr;
Jakub Pawlowski49c989a2015-03-17 09:04:17 -07003145
3146 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01003147 }
3148
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02003149 if (id->driver_info & BTUSB_SNIFFER) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02003150 struct usb_device *udev = data->udev;
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02003151
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01003152 /* New sniffer firmware has crippled HCI interface */
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02003153 if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
3154 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
3155 }
3156
Marcel Holtmann3a5ef202014-07-06 14:53:54 +02003157 if (id->driver_info & BTUSB_INTEL_BOOT) {
3158 /* A bug in the bootloader causes that interrupt interface is
3159 * only enabled after receiving SetInterface(0, AltSetting=0).
3160 */
3161 err = usb_set_interface(data->udev, 0, 0);
3162 if (err < 0) {
3163 BT_ERR("failed to set interface 0, alt 0 %d", err);
3164 hci_free_dev(hdev);
3165 return err;
3166 }
3167 }
3168
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02003169 if (data->isoc) {
3170 err = usb_driver_claim_interface(&btusb_driver,
Marcel Holtmann89e75332014-09-16 04:44:50 +02003171 data->isoc, data);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02003172 if (err < 0) {
3173 hci_free_dev(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02003174 return err;
3175 }
3176 }
3177
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003178 err = hci_register_dev(hdev);
3179 if (err < 0) {
3180 hci_free_dev(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003181 return err;
3182 }
3183
3184 usb_set_intfdata(intf, data);
3185
3186 return 0;
3187}
3188
3189static void btusb_disconnect(struct usb_interface *intf)
3190{
3191 struct btusb_data *data = usb_get_intfdata(intf);
3192 struct hci_dev *hdev;
3193
3194 BT_DBG("intf %p", intf);
3195
3196 if (!data)
3197 return;
3198
3199 hdev = data->hdev;
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02003200 usb_set_intfdata(data->intf, NULL);
3201
3202 if (data->isoc)
3203 usb_set_intfdata(data->isoc, NULL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003204
3205 hci_unregister_dev(hdev);
3206
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02003207 if (intf == data->isoc)
3208 usb_driver_release_interface(&btusb_driver, data->intf);
3209 else if (data->isoc)
3210 usb_driver_release_interface(&btusb_driver, data->isoc);
3211
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003212 hci_free_dev(hdev);
3213}
3214
Oliver Neukum7bee5492009-08-24 23:44:59 +02003215#ifdef CONFIG_PM
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003216static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
3217{
3218 struct btusb_data *data = usb_get_intfdata(intf);
3219
3220 BT_DBG("intf %p", intf);
3221
3222 if (data->suspend_count++)
3223 return 0;
3224
Oliver Neukum7bee5492009-08-24 23:44:59 +02003225 spin_lock_irq(&data->txlock);
Alan Stern5b1b0b82011-08-19 23:49:48 +02003226 if (!(PMSG_IS_AUTO(message) && data->tx_in_flight)) {
Oliver Neukum7bee5492009-08-24 23:44:59 +02003227 set_bit(BTUSB_SUSPENDING, &data->flags);
3228 spin_unlock_irq(&data->txlock);
3229 } else {
3230 spin_unlock_irq(&data->txlock);
3231 data->suspend_count--;
3232 return -EBUSY;
3233 }
3234
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003235 cancel_work_sync(&data->work);
3236
Oliver Neukum7bee5492009-08-24 23:44:59 +02003237 btusb_stop_traffic(data);
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003238 usb_kill_anchored_urbs(&data->tx_anchor);
3239
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003240 return 0;
3241}
3242
Oliver Neukum7bee5492009-08-24 23:44:59 +02003243static void play_deferred(struct btusb_data *data)
3244{
3245 struct urb *urb;
3246 int err;
3247
3248 while ((urb = usb_get_from_anchor(&data->deferred))) {
3249 err = usb_submit_urb(urb, GFP_ATOMIC);
3250 if (err < 0)
3251 break;
3252
3253 data->tx_in_flight++;
3254 }
3255 usb_scuttle_anchored_urbs(&data->deferred);
3256}
3257
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003258static int btusb_resume(struct usb_interface *intf)
3259{
3260 struct btusb_data *data = usb_get_intfdata(intf);
3261 struct hci_dev *hdev = data->hdev;
Oliver Neukum7bee5492009-08-24 23:44:59 +02003262 int err = 0;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003263
3264 BT_DBG("intf %p", intf);
3265
3266 if (--data->suspend_count)
3267 return 0;
3268
3269 if (!test_bit(HCI_RUNNING, &hdev->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +02003270 goto done;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003271
3272 if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
3273 err = btusb_submit_intr_urb(hdev, GFP_NOIO);
3274 if (err < 0) {
3275 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02003276 goto failed;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003277 }
3278 }
3279
3280 if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) {
Marcel Holtmann43c2e572009-02-04 17:41:38 +01003281 err = btusb_submit_bulk_urb(hdev, GFP_NOIO);
3282 if (err < 0) {
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003283 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02003284 goto failed;
3285 }
3286
3287 btusb_submit_bulk_urb(hdev, GFP_NOIO);
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003288 }
3289
3290 if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
3291 if (btusb_submit_isoc_urb(hdev, GFP_NOIO) < 0)
3292 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
3293 else
3294 btusb_submit_isoc_urb(hdev, GFP_NOIO);
3295 }
3296
Oliver Neukum7bee5492009-08-24 23:44:59 +02003297 spin_lock_irq(&data->txlock);
3298 play_deferred(data);
3299 clear_bit(BTUSB_SUSPENDING, &data->flags);
3300 spin_unlock_irq(&data->txlock);
3301 schedule_work(&data->work);
3302
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003303 return 0;
Oliver Neukum7bee5492009-08-24 23:44:59 +02003304
3305failed:
3306 usb_scuttle_anchored_urbs(&data->deferred);
3307done:
3308 spin_lock_irq(&data->txlock);
3309 clear_bit(BTUSB_SUSPENDING, &data->flags);
3310 spin_unlock_irq(&data->txlock);
3311
3312 return err;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003313}
Oliver Neukum7bee5492009-08-24 23:44:59 +02003314#endif
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003315
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003316static struct usb_driver btusb_driver = {
3317 .name = "btusb",
3318 .probe = btusb_probe,
3319 .disconnect = btusb_disconnect,
Oliver Neukum7bee5492009-08-24 23:44:59 +02003320#ifdef CONFIG_PM
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003321 .suspend = btusb_suspend,
3322 .resume = btusb_resume,
Oliver Neukum7bee5492009-08-24 23:44:59 +02003323#endif
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003324 .id_table = btusb_table,
Oliver Neukum7bee5492009-08-24 23:44:59 +02003325 .supports_autosuspend = 1,
Sarah Sharpe1f12eb2012-04-23 10:08:51 -07003326 .disable_hub_initiated_lpm = 1,
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003327};
3328
Greg Kroah-Hartman93f15082011-11-18 09:47:34 -08003329module_usb_driver(btusb_driver);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003330
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02003331module_param(disable_scofix, bool, 0644);
3332MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
3333
3334module_param(force_scofix, bool, 0644);
3335MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
3336
3337module_param(reset, bool, 0644);
3338MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
3339
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003340MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
3341MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
3342MODULE_VERSION(VERSION);
3343MODULE_LICENSE("GPL");