blob: 08330548f7fe08a090df7f50e750b26432c2154d [file] [log] [blame]
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001/*
2 *
3 * Generic Bluetooth USB driver
4 *
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02005 * Copyright (C) 2005-2008 Marcel Holtmann <marcel@holtmann.org>
Marcel Holtmann5e23b922007-10-20 14:12:34 +02006 *
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 */
23
Marcel Holtmann5e23b922007-10-20 14:12:34 +020024#include <linux/module.h>
Marcel Holtmann5e23b922007-10-20 14:12:34 +020025#include <linux/usb.h>
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -070026#include <linux/firmware.h>
Marcel Holtmann5e23b922007-10-20 14:12:34 +020027
28#include <net/bluetooth/bluetooth.h>
29#include <net/bluetooth/hci_core.h>
30
Marcel Holtmanncda0dd72015-01-26 21:33:48 -080031#define VERSION "0.7"
Marcel Holtmanncfeb4142008-08-07 22:26:56 +020032
Rusty Russell90ab5ee2012-01-13 09:32:20 +103033static bool disable_scofix;
34static bool force_scofix;
Marcel Holtmann7a9d4022008-11-30 12:17:26 +010035
Rusty Russell90ab5ee2012-01-13 09:32:20 +103036static bool reset = 1;
Marcel Holtmanncfeb4142008-08-07 22:26:56 +020037
38static struct usb_driver btusb_driver;
39
40#define BTUSB_IGNORE 0x01
Marcel Holtmann7a9d4022008-11-30 12:17:26 +010041#define BTUSB_DIGIANSWER 0x02
42#define BTUSB_CSR 0x04
43#define BTUSB_SNIFFER 0x08
44#define BTUSB_BCM92035 0x10
45#define BTUSB_BROKEN_ISOC 0x20
46#define BTUSB_WRONG_SCO_MTU 0x40
Steven.Li2d25f8b2011-07-01 14:02:36 +080047#define BTUSB_ATH3012 0x80
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -070048#define BTUSB_INTEL 0x100
Marcel Holtmann40df7832014-07-06 13:29:58 +020049#define BTUSB_INTEL_BOOT 0x200
50#define BTUSB_BCM_PATCHRAM 0x400
Amitkumar Karwarae8df492014-07-18 14:47:06 -070051#define BTUSB_MARVELL 0x800
Marcel Holtmann4fcef8e2015-01-01 17:34:37 -080052#define BTUSB_SWAVE 0x1000
Marcel Holtmanncda0dd72015-01-26 21:33:48 -080053#define BTUSB_INTEL_NEW 0x2000
Marcel Holtmann893ba542015-01-28 20:27:34 -080054#define BTUSB_AMP 0x4000
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +000055#define BTUSB_QCA_ROME 0x8000
Marcel Holtmann5e23b922007-10-20 14:12:34 +020056
Marcel Holtmann54265202013-10-11 07:46:18 -070057static const struct usb_device_id btusb_table[] = {
Marcel Holtmann5e23b922007-10-20 14:12:34 +020058 /* Generic Bluetooth USB device */
59 { USB_DEVICE_INFO(0xe0, 0x01, 0x01) },
60
Marcel Holtmann893ba542015-01-28 20:27:34 -080061 /* Generic Bluetooth AMP device */
62 { USB_DEVICE_INFO(0xe0, 0x01, 0x04), .driver_info = BTUSB_AMP },
63
Henrik Rydberg1fa65352012-08-25 19:28:06 +020064 /* Apple-specific (Broadcom) devices */
65 { USB_VENDOR_AND_INTERFACE_INFO(0x05ac, 0xff, 0x01, 0x01) },
66
Cho, Yu-Chen178c0592013-06-04 21:40:26 +080067 /* MediaTek MT76x0E */
68 { USB_DEVICE(0x0e8d, 0x763f) },
69
Oliver Neukumc510eae2011-09-21 11:41:45 +020070 /* Broadcom SoftSailing reporting vendor specific */
Don Zickus2e8b50632012-03-28 16:41:11 -040071 { USB_DEVICE(0x0a5c, 0x21e1) },
Oliver Neukumc510eae2011-09-21 11:41:45 +020072
Nobuhiro Iwamatsu3cd01972010-08-20 16:24:07 +090073 /* Apple MacBookPro 7,1 */
74 { USB_DEVICE(0x05ac, 0x8213) },
75
Cyril Lacoux0a79f672010-07-14 10:29:27 +040076 /* Apple iMac11,1 */
77 { USB_DEVICE(0x05ac, 0x8215) },
78
Nobuhiro Iwamatsu9c047152010-08-20 16:24:06 +090079 /* Apple MacBookPro6,2 */
80 { USB_DEVICE(0x05ac, 0x8218) },
81
Edgar (gimli) Hucek3e3ede72010-11-04 08:04:33 +010082 /* Apple MacBookAir3,1, MacBookAir3,2 */
83 { USB_DEVICE(0x05ac, 0x821b) },
84
Pieter-Augustijn Van Malleghema63b7232011-09-07 02:28:10 -040085 /* Apple MacBookAir4,1 */
86 { USB_DEVICE(0x05ac, 0x821f) },
87
Marc-Antoine Perennou88d377b2011-03-24 14:51:21 -030088 /* Apple MacBookPro8,2 */
89 { USB_DEVICE(0x05ac, 0x821a) },
90
Jurgen Kramerf78b68262011-09-04 18:01:42 +020091 /* Apple MacMini5,1 */
92 { USB_DEVICE(0x05ac, 0x8281) },
93
Marcel Holtmanncfeb4142008-08-07 22:26:56 +020094 /* AVM BlueFRITZ! USB v2.0 */
Marcel Holtmann4fcef8e2015-01-01 17:34:37 -080095 { USB_DEVICE(0x057c, 0x3800), .driver_info = BTUSB_SWAVE },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +020096
97 /* Bluetooth Ultraport Module from IBM */
98 { USB_DEVICE(0x04bf, 0x030a) },
99
100 /* ALPS Modules with non-standard id */
101 { USB_DEVICE(0x044e, 0x3001) },
102 { USB_DEVICE(0x044e, 0x3002) },
103
104 /* Ericsson with non-standard id */
105 { USB_DEVICE(0x0bdb, 0x1002) },
106
107 /* Canyon CN-BTU1 with HID interfaces */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100108 { USB_DEVICE(0x0c10, 0x0000) },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200109
Wen-chien Jesse Sungd13431c2011-11-08 14:30:22 +0800110 /* Broadcom BCM20702A0 */
Andy Shevchenko0b880062014-02-18 18:26:19 +0200111 { USB_DEVICE(0x0489, 0xe042) },
112 { USB_DEVICE(0x04ca, 0x2003) },
Jeff Cook1ee3ff62012-11-09 16:39:48 -0700113 { USB_DEVICE(0x0b05, 0x17b5) },
Raphael Kubo da Costa38a172b2013-09-02 14:57:51 +0300114 { USB_DEVICE(0x0b05, 0x17cb) },
Wen-chien Jesse Sungd13431c2011-11-08 14:30:22 +0800115 { USB_DEVICE(0x413c, 0x8197) },
Fabio Ka86c02e2014-11-18 00:46:28 -0200116 { USB_DEVICE(0x13d3, 0x3404),
117 .driver_info = BTUSB_BCM_PATCHRAM },
Wen-chien Jesse Sungd13431c2011-11-08 14:30:22 +0800118
Marcel Holtmannd049f4e2015-01-26 20:35:32 -0800119 /* Broadcom BCM20702B0 (Dynex/Insignia) */
120 { USB_DEVICE(0x19ff, 0x0239), .driver_info = BTUSB_BCM_PATCHRAM },
121
Steven Harms98514032012-04-13 14:45:55 -0400122 /* Foxconn - Hon Hai */
Heinrich Siebmanns6029ddc2014-12-03 19:32:22 +0100123 { USB_VENDOR_AND_INTERFACE_INFO(0x0489, 0xff, 0x01, 0x01),
124 .driver_info = BTUSB_BCM_PATCHRAM },
Steven Harms98514032012-04-13 14:45:55 -0400125
Matej Dubovy8f0c3042015-02-02 18:50:14 +0100126 /* Lite-On Technology - Broadcom based */
127 { USB_VENDOR_AND_INTERFACE_INFO(0x04ca, 0xff, 0x01, 0x01),
128 .driver_info = BTUSB_BCM_PATCHRAM },
129
Andy Shevchenko0b880062014-02-18 18:26:19 +0200130 /* Broadcom devices with vendor specific id */
Petri Gynther10d4c672014-05-08 15:50:01 -0700131 { USB_VENDOR_AND_INTERFACE_INFO(0x0a5c, 0xff, 0x01, 0x01),
132 .driver_info = BTUSB_BCM_PATCHRAM },
Gustavo Padovan92c385f2012-08-06 15:36:49 -0300133
Marcel Holtmannc2aef6e2014-07-21 14:02:33 +0200134 /* ASUSTek Computer - Broadcom based */
Rick Dunn9a5abda2015-01-17 05:29:12 +0100135 { USB_VENDOR_AND_INTERFACE_INFO(0x0b05, 0xff, 0x01, 0x01),
136 .driver_info = BTUSB_BCM_PATCHRAM },
Marcel Holtmannc2aef6e2014-07-21 14:02:33 +0200137
Ken O'Brien5bcecf32013-09-21 19:14:43 +0100138 /* Belkin F8065bf - Broadcom based */
139 { USB_VENDOR_AND_INTERFACE_INFO(0x050d, 0xff, 0x01, 0x01) },
140
Jurgen Kramer9113bfd2014-02-15 12:01:09 +0100141 /* IMC Networks - Broadcom based */
142 { USB_VENDOR_AND_INTERFACE_INFO(0x13d3, 0xff, 0x01, 0x01) },
143
Marcel Holtmann40df7832014-07-06 13:29:58 +0200144 /* Intel Bluetooth USB Bootloader (RAM module) */
Marcel Holtmannd92f2df2014-07-06 14:53:55 +0200145 { USB_DEVICE(0x8087, 0x0a5a),
146 .driver_info = BTUSB_INTEL_BOOT | BTUSB_BROKEN_ISOC },
Marcel Holtmann40df7832014-07-06 13:29:58 +0200147
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200148 { } /* Terminating entry */
149};
150
151MODULE_DEVICE_TABLE(usb, btusb_table);
152
Marcel Holtmann54265202013-10-11 07:46:18 -0700153static const struct usb_device_id blacklist_table[] = {
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200154 /* CSR BlueCore devices */
155 { USB_DEVICE(0x0a12, 0x0001), .driver_info = BTUSB_CSR },
156
157 /* Broadcom BCM2033 without firmware */
158 { USB_DEVICE(0x0a5c, 0x2033), .driver_info = BTUSB_IGNORE },
159
Bala Shanmugambe931122010-11-26 17:35:46 +0530160 /* Atheros 3011 with sflash firmware */
Andy Shevchenko0b880062014-02-18 18:26:19 +0200161 { USB_DEVICE(0x0489, 0xe027), .driver_info = BTUSB_IGNORE },
162 { USB_DEVICE(0x0489, 0xe03d), .driver_info = BTUSB_IGNORE },
Alexander Ploumistos2eeff0b2015-02-13 21:05:11 +0200163 { USB_DEVICE(0x04f2, 0xaff1), .driver_info = BTUSB_IGNORE },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200164 { USB_DEVICE(0x0930, 0x0215), .driver_info = BTUSB_IGNORE },
Bala Shanmugambe931122010-11-26 17:35:46 +0530165 { USB_DEVICE(0x0cf3, 0x3002), .driver_info = BTUSB_IGNORE },
Marek Vasut6eda5412012-06-08 14:32:50 +0200166 { USB_DEVICE(0x0cf3, 0xe019), .driver_info = BTUSB_IGNORE },
Andy Ross2a7bccc2011-05-09 16:11:16 -0700167 { USB_DEVICE(0x13d3, 0x3304), .driver_info = BTUSB_IGNORE },
Bala Shanmugambe931122010-11-26 17:35:46 +0530168
Cho, Yu-Chen509e7862011-01-26 17:10:59 +0800169 /* Atheros AR9285 Malbec with sflash firmware */
170 { USB_DEVICE(0x03f0, 0x311d), .driver_info = BTUSB_IGNORE },
171
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530172 /* Atheros 3012 with sflash firmware */
Andy Shevchenko0b880062014-02-18 18:26:19 +0200173 { USB_DEVICE(0x0489, 0xe04d), .driver_info = BTUSB_ATH3012 },
174 { USB_DEVICE(0x0489, 0xe04e), .driver_info = BTUSB_ATH3012 },
175 { USB_DEVICE(0x0489, 0xe056), .driver_info = BTUSB_ATH3012 },
176 { USB_DEVICE(0x0489, 0xe057), .driver_info = BTUSB_ATH3012 },
177 { USB_DEVICE(0x0489, 0xe05f), .driver_info = BTUSB_ATH3012 },
Anantha Krishnan4b552bc2014-10-06 16:31:49 +0530178 { USB_DEVICE(0x0489, 0xe078), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200179 { USB_DEVICE(0x04c5, 0x1330), .driver_info = BTUSB_ATH3012 },
180 { USB_DEVICE(0x04ca, 0x3004), .driver_info = BTUSB_ATH3012 },
181 { USB_DEVICE(0x04ca, 0x3005), .driver_info = BTUSB_ATH3012 },
182 { USB_DEVICE(0x04ca, 0x3006), .driver_info = BTUSB_ATH3012 },
Mohammed Habibulla1fb4e092014-04-17 11:37:13 -0700183 { USB_DEVICE(0x04ca, 0x3007), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200184 { USB_DEVICE(0x04ca, 0x3008), .driver_info = BTUSB_ATH3012 },
185 { USB_DEVICE(0x04ca, 0x300b), .driver_info = BTUSB_ATH3012 },
Janne Heikkinen134d3b32014-12-09 07:44:51 +0200186 { USB_DEVICE(0x04ca, 0x3010), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200187 { USB_DEVICE(0x0930, 0x0219), .driver_info = BTUSB_ATH3012 },
188 { USB_DEVICE(0x0930, 0x0220), .driver_info = BTUSB_ATH3012 },
Vincent Zwanenburg89d29752014-08-08 12:33:56 +0100189 { USB_DEVICE(0x0930, 0x0227), .driver_info = BTUSB_ATH3012 },
Andy Shevchenkoa735f9e2014-02-18 18:26:20 +0200190 { USB_DEVICE(0x0b05, 0x17d0), .driver_info = BTUSB_ATH3012 },
Ming Leid66629c2013-03-15 11:00:39 +0800191 { USB_DEVICE(0x0cf3, 0x0036), .driver_info = BTUSB_ATH3012 },
Steven.Li2d25f8b2011-07-01 14:02:36 +0800192 { USB_DEVICE(0x0cf3, 0x3004), .driver_info = BTUSB_ATH3012 },
Sunguk Lee94a32d102013-03-12 04:41:58 +0900193 { USB_DEVICE(0x0cf3, 0x3008), .driver_info = BTUSB_ATH3012 },
Cho, Yu-Chen07c0ea82012-03-14 22:01:21 +0200194 { USB_DEVICE(0x0cf3, 0x311d), .driver_info = BTUSB_ATH3012 },
Oliver Neukumb1312372014-01-16 15:37:11 +0100195 { USB_DEVICE(0x0cf3, 0x311e), .driver_info = BTUSB_ATH3012 },
Oliver Neukum1e56f1e2014-01-16 16:02:58 +0100196 { USB_DEVICE(0x0cf3, 0x311f), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200197 { USB_DEVICE(0x0cf3, 0x3121), .driver_info = BTUSB_ATH3012 },
Ming Leiebaf5792013-03-18 23:45:11 +0800198 { USB_DEVICE(0x0cf3, 0x817a), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200199 { USB_DEVICE(0x0cf3, 0xe003), .driver_info = BTUSB_ATH3012 },
AceLan Kaoac713112012-04-19 14:53:45 +0800200 { USB_DEVICE(0x0cf3, 0xe004), .driver_info = BTUSB_ATH3012 },
Peng Chen0a3658c2013-08-30 17:41:40 +0800201 { USB_DEVICE(0x0cf3, 0xe005), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200202 { USB_DEVICE(0x13d3, 0x3362), .driver_info = BTUSB_ATH3012 },
203 { USB_DEVICE(0x13d3, 0x3375), .driver_info = BTUSB_ATH3012 },
AceLan Kaoeed307e2012-12-11 11:41:20 +0800204 { USB_DEVICE(0x13d3, 0x3393), .driver_info = BTUSB_ATH3012 },
Sujith Manoharan5b77a1f2013-07-15 09:29:03 +0530205 { USB_DEVICE(0x13d3, 0x3402), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin3bb30a7c2014-11-25 20:19:52 +0300206 { USB_DEVICE(0x13d3, 0x3408), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin033efa92015-01-18 00:16:51 +0300207 { USB_DEVICE(0x13d3, 0x3423), .driver_info = BTUSB_ATH3012 },
Anantha Krishnanfa2f1392014-07-08 19:25:08 +0530208 { USB_DEVICE(0x13d3, 0x3432), .driver_info = BTUSB_ATH3012 },
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530209
Cho, Yu-Chene9036e32011-02-15 10:20:07 +0800210 /* Atheros AR5BBU12 with sflash firmware */
211 { USB_DEVICE(0x0489, 0xe02c), .driver_info = BTUSB_IGNORE },
212
Michael Gruetzner85d59722012-05-02 22:33:40 +0200213 /* Atheros AR5BBU12 with sflash firmware */
Yevgeniy Melnichukbc21fde2012-08-07 19:48:10 +0530214 { USB_DEVICE(0x0489, 0xe036), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200215 { USB_DEVICE(0x0489, 0xe03c), .driver_info = BTUSB_ATH3012 },
Michael Gruetzner85d59722012-05-02 22:33:40 +0200216
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +0000217 /* QCA ROME chipset */
218 { USB_DEVICE(0x0cf3, 0xe300), .driver_info = BTUSB_QCA_ROME},
219 { USB_DEVICE(0x0cf3, 0xe360), .driver_info = BTUSB_QCA_ROME},
220
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200221 /* Broadcom BCM2035 */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100222 { USB_DEVICE(0x0a5c, 0x2009), .driver_info = BTUSB_BCM92035 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200223 { USB_DEVICE(0x0a5c, 0x200a), .driver_info = BTUSB_WRONG_SCO_MTU },
224 { USB_DEVICE(0x0a5c, 0x2035), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200225
226 /* Broadcom BCM2045 */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100227 { USB_DEVICE(0x0a5c, 0x2039), .driver_info = BTUSB_WRONG_SCO_MTU },
228 { USB_DEVICE(0x0a5c, 0x2101), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmannbdbef3d2008-09-23 00:16:35 +0200229
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200230 /* IBM/Lenovo ThinkPad with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100231 { USB_DEVICE(0x0a5c, 0x201e), .driver_info = BTUSB_WRONG_SCO_MTU },
232 { USB_DEVICE(0x0a5c, 0x2110), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200233
234 /* HP laptop with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100235 { USB_DEVICE(0x03f0, 0x171d), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200236
237 /* Dell laptop with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100238 { USB_DEVICE(0x413c, 0x8126), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200239
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100240 /* Dell Wireless 370 and 410 devices */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100241 { USB_DEVICE(0x413c, 0x8152), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100242 { USB_DEVICE(0x413c, 0x8156), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200243
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100244 /* Belkin F8T012 and F8T013 devices */
245 { USB_DEVICE(0x050d, 0x0012), .driver_info = BTUSB_WRONG_SCO_MTU },
246 { USB_DEVICE(0x050d, 0x0013), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200247
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100248 /* Asus WL-BTD202 device */
249 { USB_DEVICE(0x0b05, 0x1715), .driver_info = BTUSB_WRONG_SCO_MTU },
250
251 /* Kensington Bluetooth USB adapter */
252 { USB_DEVICE(0x047d, 0x105e), .driver_info = BTUSB_WRONG_SCO_MTU },
253
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200254 /* RTX Telecom based adapters with buggy SCO support */
255 { USB_DEVICE(0x0400, 0x0807), .driver_info = BTUSB_BROKEN_ISOC },
256 { USB_DEVICE(0x0400, 0x080a), .driver_info = BTUSB_BROKEN_ISOC },
257
258 /* CONWISE Technology based adapters with buggy SCO support */
259 { USB_DEVICE(0x0e5e, 0x6622), .driver_info = BTUSB_BROKEN_ISOC },
260
Marcel Holtmann4fcef8e2015-01-01 17:34:37 -0800261 /* Roper Class 1 Bluetooth Dongle (Silicon Wave based) */
262 { USB_DEVICE(0x1300, 0x0001), .driver_info = BTUSB_SWAVE },
263
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200264 /* Digianswer devices */
265 { USB_DEVICE(0x08fd, 0x0001), .driver_info = BTUSB_DIGIANSWER },
266 { USB_DEVICE(0x08fd, 0x0002), .driver_info = BTUSB_IGNORE },
267
268 /* CSR BlueCore Bluetooth Sniffer */
Marcel Holtmann4f64fa82014-07-07 00:12:04 +0200269 { USB_DEVICE(0x0a12, 0x0002),
270 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200271
272 /* Frontline ComProbe Bluetooth Sniffer */
Marcel Holtmann4f64fa82014-07-07 00:12:04 +0200273 { USB_DEVICE(0x16d3, 0x0002),
274 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200275
Marcel Holtmanncb1ee892015-01-28 19:41:42 -0800276 /* Marvell Bluetooth devices */
277 { USB_DEVICE(0x1286, 0x2044), .driver_info = BTUSB_MARVELL },
278 { USB_DEVICE(0x1286, 0x2046), .driver_info = BTUSB_MARVELL },
279
Marcel Holtmannd0ac9eb2015-01-28 19:41:43 -0800280 /* Intel Bluetooth devices */
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -0700281 { USB_DEVICE(0x8087, 0x07dc), .driver_info = BTUSB_INTEL },
Tedd Ho-Jeong Anef4e5e42013-11-12 13:10:58 -0800282 { USB_DEVICE(0x8087, 0x0a2a), .driver_info = BTUSB_INTEL },
Marcel Holtmanncda0dd72015-01-26 21:33:48 -0800283 { USB_DEVICE(0x8087, 0x0a2b), .driver_info = BTUSB_INTEL_NEW },
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -0700284
Marcel Holtmannd0ac9eb2015-01-28 19:41:43 -0800285 /* Other Intel Bluetooth devices */
286 { USB_VENDOR_AND_INTERFACE_INFO(0x8087, 0xe0, 0x01, 0x01),
287 .driver_info = BTUSB_IGNORE },
Amitkumar Karwarae8df492014-07-18 14:47:06 -0700288
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200289 { } /* Terminating entry */
290};
291
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200292#define BTUSB_MAX_ISOC_FRAMES 10
293
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200294#define BTUSB_INTR_RUNNING 0
295#define BTUSB_BULK_RUNNING 1
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200296#define BTUSB_ISOC_RUNNING 2
Oliver Neukum7bee5492009-08-24 23:44:59 +0200297#define BTUSB_SUSPENDING 3
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -0300298#define BTUSB_DID_ISO_RESUME 4
Marcel Holtmanncda0dd72015-01-26 21:33:48 -0800299#define BTUSB_BOOTLOADER 5
300#define BTUSB_DOWNLOADING 6
Marcel Holtmannce6bb922015-01-28 01:58:40 -0800301#define BTUSB_FIRMWARE_LOADED 7
Marcel Holtmanncda0dd72015-01-26 21:33:48 -0800302#define BTUSB_FIRMWARE_FAILED 8
Marcel Holtmannce6bb922015-01-28 01:58:40 -0800303#define BTUSB_BOOTING 9
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200304
305struct btusb_data {
306 struct hci_dev *hdev;
307 struct usb_device *udev;
Marcel Holtmann5fbcd262008-09-23 00:16:36 +0200308 struct usb_interface *intf;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200309 struct usb_interface *isoc;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200310
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200311 unsigned long flags;
312
313 struct work_struct work;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200314 struct work_struct waker;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200315
Marcel Holtmann803b5832014-09-16 08:00:29 +0200316 struct usb_anchor deferred;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200317 struct usb_anchor tx_anchor;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200318 int tx_in_flight;
319 spinlock_t txlock;
320
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200321 struct usb_anchor intr_anchor;
322 struct usb_anchor bulk_anchor;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200323 struct usb_anchor isoc_anchor;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200324 spinlock_t rxlock;
325
326 struct sk_buff *evt_skb;
327 struct sk_buff *acl_skb;
328 struct sk_buff *sco_skb;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200329
330 struct usb_endpoint_descriptor *intr_ep;
331 struct usb_endpoint_descriptor *bulk_tx_ep;
332 struct usb_endpoint_descriptor *bulk_rx_ep;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200333 struct usb_endpoint_descriptor *isoc_tx_ep;
334 struct usb_endpoint_descriptor *isoc_rx_ep;
335
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100336 __u8 cmdreq_type;
Marcel Holtmann893ba542015-01-28 20:27:34 -0800337 __u8 cmdreq;
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100338
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100339 unsigned int sco_num;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200340 int isoc_altsetting;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +0100341 int suspend_count;
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +0100342
Marcel Holtmann97307f52015-01-12 13:51:10 -0800343 int (*recv_event)(struct hci_dev *hdev, struct sk_buff *skb);
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +0100344 int (*recv_bulk)(struct btusb_data *data, void *buffer, int count);
Kim, Ben Young Taeace31982015-02-15 23:06:14 +0000345
346 int (*setup_on_usb)(struct hci_dev *hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200347};
348
Marcel Holtmann803b5832014-09-16 08:00:29 +0200349static inline void btusb_free_frags(struct btusb_data *data)
350{
351 unsigned long flags;
352
353 spin_lock_irqsave(&data->rxlock, flags);
354
355 kfree_skb(data->evt_skb);
356 data->evt_skb = NULL;
357
358 kfree_skb(data->acl_skb);
359 data->acl_skb = NULL;
360
361 kfree_skb(data->sco_skb);
362 data->sco_skb = NULL;
363
364 spin_unlock_irqrestore(&data->rxlock, flags);
365}
366
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200367static int btusb_recv_intr(struct btusb_data *data, void *buffer, int count)
368{
Marcel Holtmann803b5832014-09-16 08:00:29 +0200369 struct sk_buff *skb;
370 int err = 0;
371
372 spin_lock(&data->rxlock);
373 skb = data->evt_skb;
374
375 while (count) {
376 int len;
377
378 if (!skb) {
379 skb = bt_skb_alloc(HCI_MAX_EVENT_SIZE, GFP_ATOMIC);
380 if (!skb) {
381 err = -ENOMEM;
382 break;
383 }
384
385 bt_cb(skb)->pkt_type = HCI_EVENT_PKT;
386 bt_cb(skb)->expect = HCI_EVENT_HDR_SIZE;
387 }
388
389 len = min_t(uint, bt_cb(skb)->expect, count);
390 memcpy(skb_put(skb, len), buffer, len);
391
392 count -= len;
393 buffer += len;
394 bt_cb(skb)->expect -= len;
395
396 if (skb->len == HCI_EVENT_HDR_SIZE) {
397 /* Complete event header */
398 bt_cb(skb)->expect = hci_event_hdr(skb)->plen;
399
400 if (skb_tailroom(skb) < bt_cb(skb)->expect) {
401 kfree_skb(skb);
402 skb = NULL;
403
404 err = -EILSEQ;
405 break;
406 }
407 }
408
409 if (bt_cb(skb)->expect == 0) {
410 /* Complete frame */
Marcel Holtmann97307f52015-01-12 13:51:10 -0800411 data->recv_event(data->hdev, skb);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200412 skb = NULL;
413 }
414 }
415
416 data->evt_skb = skb;
417 spin_unlock(&data->rxlock);
418
419 return err;
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200420}
421
422static int btusb_recv_bulk(struct btusb_data *data, void *buffer, int count)
423{
Marcel Holtmann803b5832014-09-16 08:00:29 +0200424 struct sk_buff *skb;
425 int err = 0;
426
427 spin_lock(&data->rxlock);
428 skb = data->acl_skb;
429
430 while (count) {
431 int len;
432
433 if (!skb) {
434 skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
435 if (!skb) {
436 err = -ENOMEM;
437 break;
438 }
439
440 bt_cb(skb)->pkt_type = HCI_ACLDATA_PKT;
441 bt_cb(skb)->expect = HCI_ACL_HDR_SIZE;
442 }
443
444 len = min_t(uint, bt_cb(skb)->expect, count);
445 memcpy(skb_put(skb, len), buffer, len);
446
447 count -= len;
448 buffer += len;
449 bt_cb(skb)->expect -= len;
450
451 if (skb->len == HCI_ACL_HDR_SIZE) {
452 __le16 dlen = hci_acl_hdr(skb)->dlen;
453
454 /* Complete ACL header */
455 bt_cb(skb)->expect = __le16_to_cpu(dlen);
456
457 if (skb_tailroom(skb) < bt_cb(skb)->expect) {
458 kfree_skb(skb);
459 skb = NULL;
460
461 err = -EILSEQ;
462 break;
463 }
464 }
465
466 if (bt_cb(skb)->expect == 0) {
467 /* Complete frame */
468 hci_recv_frame(data->hdev, skb);
469 skb = NULL;
470 }
471 }
472
473 data->acl_skb = skb;
474 spin_unlock(&data->rxlock);
475
476 return err;
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200477}
478
479static int btusb_recv_isoc(struct btusb_data *data, void *buffer, int count)
480{
Marcel Holtmann803b5832014-09-16 08:00:29 +0200481 struct sk_buff *skb;
482 int err = 0;
483
484 spin_lock(&data->rxlock);
485 skb = data->sco_skb;
486
487 while (count) {
488 int len;
489
490 if (!skb) {
491 skb = bt_skb_alloc(HCI_MAX_SCO_SIZE, GFP_ATOMIC);
492 if (!skb) {
493 err = -ENOMEM;
494 break;
495 }
496
497 bt_cb(skb)->pkt_type = HCI_SCODATA_PKT;
498 bt_cb(skb)->expect = HCI_SCO_HDR_SIZE;
499 }
500
501 len = min_t(uint, bt_cb(skb)->expect, count);
502 memcpy(skb_put(skb, len), buffer, len);
503
504 count -= len;
505 buffer += len;
506 bt_cb(skb)->expect -= len;
507
508 if (skb->len == HCI_SCO_HDR_SIZE) {
509 /* Complete SCO header */
510 bt_cb(skb)->expect = hci_sco_hdr(skb)->dlen;
511
512 if (skb_tailroom(skb) < bt_cb(skb)->expect) {
513 kfree_skb(skb);
514 skb = NULL;
515
516 err = -EILSEQ;
517 break;
518 }
519 }
520
521 if (bt_cb(skb)->expect == 0) {
522 /* Complete frame */
523 hci_recv_frame(data->hdev, skb);
524 skb = NULL;
525 }
526 }
527
528 data->sco_skb = skb;
529 spin_unlock(&data->rxlock);
530
531 return err;
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200532}
533
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200534static void btusb_intr_complete(struct urb *urb)
535{
536 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +0100537 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200538 int err;
539
Marcel Holtmann89e75332014-09-16 04:44:50 +0200540 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
541 urb->actual_length);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200542
543 if (!test_bit(HCI_RUNNING, &hdev->flags))
544 return;
545
546 if (urb->status == 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200547 hdev->stat.byte_rx += urb->actual_length;
548
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200549 if (btusb_recv_intr(data, urb->transfer_buffer,
550 urb->actual_length) < 0) {
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200551 BT_ERR("%s corrupted event packet", hdev->name);
552 hdev->stat.err_rx++;
553 }
Champion Chen85560c42014-09-06 14:06:08 -0500554 } else if (urb->status == -ENOENT) {
555 /* Avoid suspend failed when usb_kill_urb */
556 return;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200557 }
558
559 if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
560 return;
561
Oliver Neukum7bee5492009-08-24 23:44:59 +0200562 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200563 usb_anchor_urb(urb, &data->intr_anchor);
564
565 err = usb_submit_urb(urb, GFP_ATOMIC);
566 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200567 /* -EPERM: urb is being killed;
568 * -ENODEV: device got disconnected */
569 if (err != -EPERM && err != -ENODEV)
Stefan Seyfried61faddf2010-11-30 21:49:08 +0100570 BT_ERR("%s urb %p failed to resubmit (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200571 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200572 usb_unanchor_urb(urb);
573 }
574}
575
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100576static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200577{
David Herrmann155961e2012-02-09 21:58:32 +0100578 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200579 struct urb *urb;
580 unsigned char *buf;
581 unsigned int pipe;
582 int err, size;
583
584 BT_DBG("%s", hdev->name);
585
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200586 if (!data->intr_ep)
587 return -ENODEV;
588
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100589 urb = usb_alloc_urb(0, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200590 if (!urb)
591 return -ENOMEM;
592
593 size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
594
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100595 buf = kmalloc(size, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200596 if (!buf) {
597 usb_free_urb(urb);
598 return -ENOMEM;
599 }
600
601 pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
602
603 usb_fill_int_urb(urb, data->udev, pipe, buf, size,
Marcel Holtmann89e75332014-09-16 04:44:50 +0200604 btusb_intr_complete, hdev, data->intr_ep->bInterval);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200605
606 urb->transfer_flags |= URB_FREE_BUFFER;
607
608 usb_anchor_urb(urb, &data->intr_anchor);
609
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100610 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200611 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +0200612 if (err != -EPERM && err != -ENODEV)
613 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200614 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200615 usb_unanchor_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200616 }
617
618 usb_free_urb(urb);
619
620 return err;
621}
622
623static void btusb_bulk_complete(struct urb *urb)
624{
625 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +0100626 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200627 int err;
628
Marcel Holtmann89e75332014-09-16 04:44:50 +0200629 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
630 urb->actual_length);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200631
632 if (!test_bit(HCI_RUNNING, &hdev->flags))
633 return;
634
635 if (urb->status == 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200636 hdev->stat.byte_rx += urb->actual_length;
637
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +0100638 if (data->recv_bulk(data, urb->transfer_buffer,
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200639 urb->actual_length) < 0) {
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200640 BT_ERR("%s corrupted ACL packet", hdev->name);
641 hdev->stat.err_rx++;
642 }
Champion Chen85560c42014-09-06 14:06:08 -0500643 } else if (urb->status == -ENOENT) {
644 /* Avoid suspend failed when usb_kill_urb */
645 return;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200646 }
647
648 if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
649 return;
650
651 usb_anchor_urb(urb, &data->bulk_anchor);
Oliver Neukum652fd782009-12-16 19:23:43 +0100652 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200653
654 err = usb_submit_urb(urb, GFP_ATOMIC);
655 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200656 /* -EPERM: urb is being killed;
657 * -ENODEV: device got disconnected */
658 if (err != -EPERM && err != -ENODEV)
Stefan Seyfried61faddf2010-11-30 21:49:08 +0100659 BT_ERR("%s urb %p failed to resubmit (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200660 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200661 usb_unanchor_urb(urb);
662 }
663}
664
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100665static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200666{
David Herrmann155961e2012-02-09 21:58:32 +0100667 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200668 struct urb *urb;
669 unsigned char *buf;
670 unsigned int pipe;
Vikram Kandukuri290ba202009-07-02 14:31:59 +0530671 int err, size = HCI_MAX_FRAME_SIZE;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200672
673 BT_DBG("%s", hdev->name);
674
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200675 if (!data->bulk_rx_ep)
676 return -ENODEV;
677
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100678 urb = usb_alloc_urb(0, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200679 if (!urb)
680 return -ENOMEM;
681
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100682 buf = kmalloc(size, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200683 if (!buf) {
684 usb_free_urb(urb);
685 return -ENOMEM;
686 }
687
688 pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
689
Marcel Holtmann89e75332014-09-16 04:44:50 +0200690 usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
691 btusb_bulk_complete, hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200692
693 urb->transfer_flags |= URB_FREE_BUFFER;
694
Oliver Neukum7bee5492009-08-24 23:44:59 +0200695 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200696 usb_anchor_urb(urb, &data->bulk_anchor);
697
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100698 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200699 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +0200700 if (err != -EPERM && err != -ENODEV)
701 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200702 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200703 usb_unanchor_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200704 }
705
706 usb_free_urb(urb);
707
708 return err;
709}
710
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200711static void btusb_isoc_complete(struct urb *urb)
712{
713 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +0100714 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200715 int i, err;
716
Marcel Holtmann89e75332014-09-16 04:44:50 +0200717 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
718 urb->actual_length);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200719
720 if (!test_bit(HCI_RUNNING, &hdev->flags))
721 return;
722
723 if (urb->status == 0) {
724 for (i = 0; i < urb->number_of_packets; i++) {
725 unsigned int offset = urb->iso_frame_desc[i].offset;
726 unsigned int length = urb->iso_frame_desc[i].actual_length;
727
728 if (urb->iso_frame_desc[i].status)
729 continue;
730
731 hdev->stat.byte_rx += length;
732
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200733 if (btusb_recv_isoc(data, urb->transfer_buffer + offset,
734 length) < 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200735 BT_ERR("%s corrupted SCO packet", hdev->name);
736 hdev->stat.err_rx++;
737 }
738 }
Champion Chen85560c42014-09-06 14:06:08 -0500739 } else if (urb->status == -ENOENT) {
740 /* Avoid suspend failed when usb_kill_urb */
741 return;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200742 }
743
744 if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags))
745 return;
746
747 usb_anchor_urb(urb, &data->isoc_anchor);
748
749 err = usb_submit_urb(urb, GFP_ATOMIC);
750 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200751 /* -EPERM: urb is being killed;
752 * -ENODEV: device got disconnected */
753 if (err != -EPERM && err != -ENODEV)
Stefan Seyfried61faddf2010-11-30 21:49:08 +0100754 BT_ERR("%s urb %p failed to resubmit (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200755 hdev->name, urb, -err);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200756 usb_unanchor_urb(urb);
757 }
758}
759
Jesper Juhl42b16b32011-01-17 00:09:38 +0100760static inline void __fill_isoc_descriptor(struct urb *urb, int len, int mtu)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200761{
762 int i, offset = 0;
763
764 BT_DBG("len %d mtu %d", len, mtu);
765
766 for (i = 0; i < BTUSB_MAX_ISOC_FRAMES && len >= mtu;
767 i++, offset += mtu, len -= mtu) {
768 urb->iso_frame_desc[i].offset = offset;
769 urb->iso_frame_desc[i].length = mtu;
770 }
771
772 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
773 urb->iso_frame_desc[i].offset = offset;
774 urb->iso_frame_desc[i].length = len;
775 i++;
776 }
777
778 urb->number_of_packets = i;
779}
780
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100781static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200782{
David Herrmann155961e2012-02-09 21:58:32 +0100783 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200784 struct urb *urb;
785 unsigned char *buf;
786 unsigned int pipe;
787 int err, size;
788
789 BT_DBG("%s", hdev->name);
790
791 if (!data->isoc_rx_ep)
792 return -ENODEV;
793
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100794 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200795 if (!urb)
796 return -ENOMEM;
797
798 size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) *
799 BTUSB_MAX_ISOC_FRAMES;
800
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100801 buf = kmalloc(size, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200802 if (!buf) {
803 usb_free_urb(urb);
804 return -ENOMEM;
805 }
806
807 pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress);
808
Bing Zhaofa0fb932011-12-20 18:19:00 -0800809 usb_fill_int_urb(urb, data->udev, pipe, buf, size, btusb_isoc_complete,
Marcel Holtmann89e75332014-09-16 04:44:50 +0200810 hdev, data->isoc_rx_ep->bInterval);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200811
Marcel Holtmann89e75332014-09-16 04:44:50 +0200812 urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200813
814 __fill_isoc_descriptor(urb, size,
Marcel Holtmann89e75332014-09-16 04:44:50 +0200815 le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize));
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200816
817 usb_anchor_urb(urb, &data->isoc_anchor);
818
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100819 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200820 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +0200821 if (err != -EPERM && err != -ENODEV)
822 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200823 hdev->name, urb, -err);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200824 usb_unanchor_urb(urb);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200825 }
826
827 usb_free_urb(urb);
828
829 return err;
830}
831
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200832static void btusb_tx_complete(struct urb *urb)
833{
834 struct sk_buff *skb = urb->context;
Marcel Holtmann89e75332014-09-16 04:44:50 +0200835 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
David Herrmann155961e2012-02-09 21:58:32 +0100836 struct btusb_data *data = hci_get_drvdata(hdev);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200837
Marcel Holtmann89e75332014-09-16 04:44:50 +0200838 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
839 urb->actual_length);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200840
841 if (!test_bit(HCI_RUNNING, &hdev->flags))
842 goto done;
843
844 if (!urb->status)
845 hdev->stat.byte_tx += urb->transfer_buffer_length;
846 else
847 hdev->stat.err_tx++;
848
849done:
850 spin_lock(&data->txlock);
851 data->tx_in_flight--;
852 spin_unlock(&data->txlock);
853
854 kfree(urb->setup_packet);
855
856 kfree_skb(skb);
857}
858
859static void btusb_isoc_tx_complete(struct urb *urb)
860{
861 struct sk_buff *skb = urb->context;
Marcel Holtmann89e75332014-09-16 04:44:50 +0200862 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200863
Marcel Holtmann89e75332014-09-16 04:44:50 +0200864 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
865 urb->actual_length);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200866
867 if (!test_bit(HCI_RUNNING, &hdev->flags))
868 goto done;
869
870 if (!urb->status)
871 hdev->stat.byte_tx += urb->transfer_buffer_length;
872 else
873 hdev->stat.err_tx++;
874
875done:
876 kfree(urb->setup_packet);
877
878 kfree_skb(skb);
879}
880
881static int btusb_open(struct hci_dev *hdev)
882{
David Herrmann155961e2012-02-09 21:58:32 +0100883 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200884 int err;
885
886 BT_DBG("%s", hdev->name);
887
Kim, Ben Young Taeace31982015-02-15 23:06:14 +0000888 /* Patching USB firmware files prior to starting any URBs of HCI path
889 * It is more safe to use USB bulk channel for downloading USB patch
890 */
891 if (data->setup_on_usb) {
892 err = data->setup_on_usb(hdev);
893 if (err <0)
894 return err;
895 }
896
Oliver Neukum7bee5492009-08-24 23:44:59 +0200897 err = usb_autopm_get_interface(data->intf);
898 if (err < 0)
899 return err;
900
901 data->intf->needs_remote_wakeup = 1;
902
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200903 if (test_and_set_bit(HCI_RUNNING, &hdev->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +0200904 goto done;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200905
906 if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +0200907 goto done;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200908
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100909 err = btusb_submit_intr_urb(hdev, GFP_KERNEL);
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100910 if (err < 0)
911 goto failed;
912
913 err = btusb_submit_bulk_urb(hdev, GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200914 if (err < 0) {
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100915 usb_kill_anchored_urbs(&data->intr_anchor);
916 goto failed;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200917 }
918
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100919 set_bit(BTUSB_BULK_RUNNING, &data->flags);
920 btusb_submit_bulk_urb(hdev, GFP_KERNEL);
921
Oliver Neukum7bee5492009-08-24 23:44:59 +0200922done:
923 usb_autopm_put_interface(data->intf);
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100924 return 0;
925
926failed:
927 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
928 clear_bit(HCI_RUNNING, &hdev->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200929 usb_autopm_put_interface(data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200930 return err;
931}
932
Oliver Neukum7bee5492009-08-24 23:44:59 +0200933static void btusb_stop_traffic(struct btusb_data *data)
934{
935 usb_kill_anchored_urbs(&data->intr_anchor);
936 usb_kill_anchored_urbs(&data->bulk_anchor);
937 usb_kill_anchored_urbs(&data->isoc_anchor);
938}
939
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200940static int btusb_close(struct hci_dev *hdev)
941{
David Herrmann155961e2012-02-09 21:58:32 +0100942 struct btusb_data *data = hci_get_drvdata(hdev);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200943 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200944
945 BT_DBG("%s", hdev->name);
946
947 if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
948 return 0;
949
Marcel Holtmanne8c3c3d2008-09-23 00:16:36 +0200950 cancel_work_sync(&data->work);
Linus Torvalds404291a2009-11-11 13:32:29 -0800951 cancel_work_sync(&data->waker);
Marcel Holtmanne8c3c3d2008-09-23 00:16:36 +0200952
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200953 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200954 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200955 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200956
957 btusb_stop_traffic(data);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200958 btusb_free_frags(data);
959
Oliver Neukum7bee5492009-08-24 23:44:59 +0200960 err = usb_autopm_get_interface(data->intf);
961 if (err < 0)
Oliver Neukum7b8e2c12009-11-13 14:26:23 +0100962 goto failed;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200963
964 data->intf->needs_remote_wakeup = 0;
965 usb_autopm_put_interface(data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200966
Oliver Neukum7b8e2c12009-11-13 14:26:23 +0100967failed:
968 usb_scuttle_anchored_urbs(&data->deferred);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200969 return 0;
970}
971
972static int btusb_flush(struct hci_dev *hdev)
973{
David Herrmann155961e2012-02-09 21:58:32 +0100974 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200975
976 BT_DBG("%s", hdev->name);
977
978 usb_kill_anchored_urbs(&data->tx_anchor);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200979 btusb_free_frags(data);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200980
981 return 0;
982}
983
Marcel Holtmann047b2ec2014-09-14 09:11:06 +0200984static struct urb *alloc_ctrl_urb(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200985{
David Herrmann155961e2012-02-09 21:58:32 +0100986 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200987 struct usb_ctrlrequest *dr;
988 struct urb *urb;
989 unsigned int pipe;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200990
Marcel Holtmann047b2ec2014-09-14 09:11:06 +0200991 urb = usb_alloc_urb(0, GFP_KERNEL);
992 if (!urb)
993 return ERR_PTR(-ENOMEM);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200994
Marcel Holtmann047b2ec2014-09-14 09:11:06 +0200995 dr = kmalloc(sizeof(*dr), GFP_KERNEL);
996 if (!dr) {
997 usb_free_urb(urb);
998 return ERR_PTR(-ENOMEM);
999 }
1000
1001 dr->bRequestType = data->cmdreq_type;
Marcel Holtmann893ba542015-01-28 20:27:34 -08001002 dr->bRequest = data->cmdreq;
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001003 dr->wIndex = 0;
1004 dr->wValue = 0;
1005 dr->wLength = __cpu_to_le16(skb->len);
1006
1007 pipe = usb_sndctrlpipe(data->udev, 0x00);
1008
Marcel Holtmann89e75332014-09-16 04:44:50 +02001009 usb_fill_control_urb(urb, data->udev, pipe, (void *)dr,
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001010 skb->data, skb->len, btusb_tx_complete, skb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001011
Marcel Holtmann89e75332014-09-16 04:44:50 +02001012 skb->dev = (void *)hdev;
Marcel Holtmann7bd8f092013-10-11 06:19:18 -07001013
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001014 return urb;
1015}
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001016
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001017static struct urb *alloc_bulk_urb(struct hci_dev *hdev, struct sk_buff *skb)
1018{
1019 struct btusb_data *data = hci_get_drvdata(hdev);
1020 struct urb *urb;
1021 unsigned int pipe;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001022
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001023 if (!data->bulk_tx_ep)
1024 return ERR_PTR(-ENODEV);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001025
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001026 urb = usb_alloc_urb(0, GFP_KERNEL);
1027 if (!urb)
1028 return ERR_PTR(-ENOMEM);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001029
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001030 pipe = usb_sndbulkpipe(data->udev, data->bulk_tx_ep->bEndpointAddress);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001031
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001032 usb_fill_bulk_urb(urb, data->udev, pipe,
1033 skb->data, skb->len, btusb_tx_complete, skb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001034
Marcel Holtmann89e75332014-09-16 04:44:50 +02001035 skb->dev = (void *)hdev;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001036
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001037 return urb;
1038}
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001039
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001040static struct urb *alloc_isoc_urb(struct hci_dev *hdev, struct sk_buff *skb)
1041{
1042 struct btusb_data *data = hci_get_drvdata(hdev);
1043 struct urb *urb;
1044 unsigned int pipe;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001045
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001046 if (!data->isoc_tx_ep)
1047 return ERR_PTR(-ENODEV);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001048
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001049 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_KERNEL);
1050 if (!urb)
1051 return ERR_PTR(-ENOMEM);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001052
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001053 pipe = usb_sndisocpipe(data->udev, data->isoc_tx_ep->bEndpointAddress);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001054
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001055 usb_fill_int_urb(urb, data->udev, pipe,
1056 skb->data, skb->len, btusb_isoc_tx_complete,
1057 skb, data->isoc_tx_ep->bInterval);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001058
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001059 urb->transfer_flags = URB_ISO_ASAP;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001060
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001061 __fill_isoc_descriptor(urb, skb->len,
1062 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001063
Marcel Holtmann89e75332014-09-16 04:44:50 +02001064 skb->dev = (void *)hdev;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001065
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001066 return urb;
1067}
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001068
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001069static int submit_tx_urb(struct hci_dev *hdev, struct urb *urb)
1070{
1071 struct btusb_data *data = hci_get_drvdata(hdev);
1072 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001073
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001074 usb_anchor_urb(urb, &data->tx_anchor);
1075
Johan Hedberge9753ef2014-09-14 08:49:34 +03001076 err = usb_submit_urb(urb, GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001077 if (err < 0) {
Paul Bolle5a9b80e2011-10-09 12:12:16 +02001078 if (err != -EPERM && err != -ENODEV)
1079 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +02001080 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001081 kfree(urb->setup_packet);
1082 usb_unanchor_urb(urb);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001083 } else {
1084 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001085 }
1086
Cong Wang54a8a792011-11-22 09:32:57 +08001087 usb_free_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001088 return err;
1089}
1090
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001091static int submit_or_queue_tx_urb(struct hci_dev *hdev, struct urb *urb)
1092{
1093 struct btusb_data *data = hci_get_drvdata(hdev);
1094 unsigned long flags;
1095 bool suspending;
1096
1097 spin_lock_irqsave(&data->txlock, flags);
1098 suspending = test_bit(BTUSB_SUSPENDING, &data->flags);
1099 if (!suspending)
1100 data->tx_in_flight++;
1101 spin_unlock_irqrestore(&data->txlock, flags);
1102
1103 if (!suspending)
1104 return submit_tx_urb(hdev, urb);
1105
1106 usb_anchor_urb(urb, &data->deferred);
1107 schedule_work(&data->waker);
1108
1109 usb_free_urb(urb);
1110 return 0;
1111}
1112
1113static int btusb_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
1114{
1115 struct urb *urb;
1116
1117 BT_DBG("%s", hdev->name);
1118
1119 if (!test_bit(HCI_RUNNING, &hdev->flags))
1120 return -EBUSY;
1121
1122 switch (bt_cb(skb)->pkt_type) {
1123 case HCI_COMMAND_PKT:
1124 urb = alloc_ctrl_urb(hdev, skb);
1125 if (IS_ERR(urb))
1126 return PTR_ERR(urb);
1127
1128 hdev->stat.cmd_tx++;
1129 return submit_or_queue_tx_urb(hdev, urb);
1130
1131 case HCI_ACLDATA_PKT:
1132 urb = alloc_bulk_urb(hdev, skb);
1133 if (IS_ERR(urb))
1134 return PTR_ERR(urb);
1135
1136 hdev->stat.acl_tx++;
1137 return submit_or_queue_tx_urb(hdev, urb);
1138
1139 case HCI_SCODATA_PKT:
1140 if (hci_conn_num(hdev, SCO_LINK) < 1)
1141 return -ENODEV;
1142
1143 urb = alloc_isoc_urb(hdev, skb);
1144 if (IS_ERR(urb))
1145 return PTR_ERR(urb);
1146
1147 hdev->stat.sco_tx++;
1148 return submit_tx_urb(hdev, urb);
1149 }
1150
1151 return -EILSEQ;
1152}
1153
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001154static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
1155{
David Herrmann155961e2012-02-09 21:58:32 +01001156 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001157
1158 BT_DBG("%s evt %d", hdev->name, evt);
1159
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001160 if (hci_conn_num(hdev, SCO_LINK) != data->sco_num) {
1161 data->sco_num = hci_conn_num(hdev, SCO_LINK);
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001162 schedule_work(&data->work);
Marcel Holtmanna780efa2008-11-30 12:17:12 +01001163 }
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001164}
1165
Jesper Juhl42b16b32011-01-17 00:09:38 +01001166static inline int __set_isoc_interface(struct hci_dev *hdev, int altsetting)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001167{
David Herrmann155961e2012-02-09 21:58:32 +01001168 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001169 struct usb_interface *intf = data->isoc;
1170 struct usb_endpoint_descriptor *ep_desc;
1171 int i, err;
1172
1173 if (!data->isoc)
1174 return -ENODEV;
1175
1176 err = usb_set_interface(data->udev, 1, altsetting);
1177 if (err < 0) {
1178 BT_ERR("%s setting interface failed (%d)", hdev->name, -err);
1179 return err;
1180 }
1181
1182 data->isoc_altsetting = altsetting;
1183
1184 data->isoc_tx_ep = NULL;
1185 data->isoc_rx_ep = NULL;
1186
1187 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
1188 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
1189
1190 if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) {
1191 data->isoc_tx_ep = ep_desc;
1192 continue;
1193 }
1194
1195 if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) {
1196 data->isoc_rx_ep = ep_desc;
1197 continue;
1198 }
1199 }
1200
1201 if (!data->isoc_tx_ep || !data->isoc_rx_ep) {
1202 BT_ERR("%s invalid SCO descriptors", hdev->name);
1203 return -ENODEV;
1204 }
1205
1206 return 0;
1207}
1208
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001209static void btusb_work(struct work_struct *work)
1210{
1211 struct btusb_data *data = container_of(work, struct btusb_data, work);
1212 struct hci_dev *hdev = data->hdev;
Mikel Astizf4001d22012-04-11 08:48:51 +02001213 int new_alts;
Oliver Neukum7bee5492009-08-24 23:44:59 +02001214 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001215
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001216 if (data->sco_num > 0) {
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -03001217 if (!test_bit(BTUSB_DID_ISO_RESUME, &data->flags)) {
Oliver Neukum8efdd0c2011-02-11 13:00:06 +01001218 err = usb_autopm_get_interface(data->isoc ? data->isoc : data->intf);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001219 if (err < 0) {
1220 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1221 usb_kill_anchored_urbs(&data->isoc_anchor);
1222 return;
1223 }
1224
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -03001225 set_bit(BTUSB_DID_ISO_RESUME, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001226 }
Mikel Astizf4001d22012-04-11 08:48:51 +02001227
1228 if (hdev->voice_setting & 0x0020) {
1229 static const int alts[3] = { 2, 4, 5 };
Marcel Holtmann89e75332014-09-16 04:44:50 +02001230
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001231 new_alts = alts[data->sco_num - 1];
Mikel Astizf4001d22012-04-11 08:48:51 +02001232 } else {
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001233 new_alts = data->sco_num;
Mikel Astizf4001d22012-04-11 08:48:51 +02001234 }
1235
1236 if (data->isoc_altsetting != new_alts) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001237 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1238 usb_kill_anchored_urbs(&data->isoc_anchor);
1239
Mikel Astizf4001d22012-04-11 08:48:51 +02001240 if (__set_isoc_interface(hdev, new_alts) < 0)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001241 return;
1242 }
1243
1244 if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001245 if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001246 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1247 else
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001248 btusb_submit_isoc_urb(hdev, GFP_KERNEL);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001249 }
1250 } else {
1251 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1252 usb_kill_anchored_urbs(&data->isoc_anchor);
1253
1254 __set_isoc_interface(hdev, 0);
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -03001255 if (test_and_clear_bit(BTUSB_DID_ISO_RESUME, &data->flags))
Oliver Neukum8efdd0c2011-02-11 13:00:06 +01001256 usb_autopm_put_interface(data->isoc ? data->isoc : data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001257 }
1258}
1259
Oliver Neukum7bee5492009-08-24 23:44:59 +02001260static void btusb_waker(struct work_struct *work)
1261{
1262 struct btusb_data *data = container_of(work, struct btusb_data, waker);
1263 int err;
1264
1265 err = usb_autopm_get_interface(data->intf);
1266 if (err < 0)
1267 return;
1268
1269 usb_autopm_put_interface(data->intf);
1270}
1271
Marcel Holtmann9f8f9622013-04-10 08:11:35 -07001272static int btusb_setup_bcm92035(struct hci_dev *hdev)
1273{
1274 struct sk_buff *skb;
1275 u8 val = 0x00;
1276
1277 BT_DBG("%s", hdev->name);
1278
1279 skb = __hci_cmd_sync(hdev, 0xfc3b, 1, &val, HCI_INIT_TIMEOUT);
1280 if (IS_ERR(skb))
1281 BT_ERR("BCM92035 command failed (%ld)", -PTR_ERR(skb));
1282 else
1283 kfree_skb(skb);
1284
1285 return 0;
1286}
1287
Marcel Holtmann81cac642014-01-03 03:02:36 -08001288static int btusb_setup_csr(struct hci_dev *hdev)
1289{
1290 struct hci_rp_read_local_version *rp;
1291 struct sk_buff *skb;
1292 int ret;
1293
1294 BT_DBG("%s", hdev->name);
1295
1296 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
1297 HCI_INIT_TIMEOUT);
1298 if (IS_ERR(skb)) {
1299 BT_ERR("Reading local version failed (%ld)", -PTR_ERR(skb));
1300 return -PTR_ERR(skb);
1301 }
1302
Marcel Holtmann89e75332014-09-16 04:44:50 +02001303 rp = (struct hci_rp_read_local_version *)skb->data;
Marcel Holtmann81cac642014-01-03 03:02:36 -08001304
1305 if (!rp->status) {
1306 if (le16_to_cpu(rp->manufacturer) != 10) {
1307 /* Clear the reset quirk since this is not an actual
1308 * early Bluetooth 1.1 device from CSR.
1309 */
1310 clear_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
1311
1312 /* These fake CSR controllers have all a broken
1313 * stored link key handling and so just disable it.
1314 */
1315 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY,
1316 &hdev->quirks);
1317 }
1318 }
1319
1320 ret = -bt_to_errno(rp->status);
1321
1322 kfree_skb(skb);
1323
1324 return ret;
1325}
1326
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001327struct intel_version {
1328 u8 status;
1329 u8 hw_platform;
1330 u8 hw_variant;
1331 u8 hw_revision;
1332 u8 fw_variant;
1333 u8 fw_revision;
1334 u8 fw_build_num;
1335 u8 fw_build_ww;
1336 u8 fw_build_yy;
1337 u8 fw_patch_num;
1338} __packed;
1339
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001340struct intel_boot_params {
1341 __u8 status;
1342 __u8 otp_format;
1343 __u8 otp_content;
1344 __u8 otp_patch;
1345 __le16 dev_revid;
1346 __u8 secure_boot;
1347 __u8 key_from_hdr;
1348 __u8 key_type;
1349 __u8 otp_lock;
1350 __u8 api_lock;
1351 __u8 debug_lock;
1352 bdaddr_t otp_bdaddr;
1353 __u8 min_fw_build_nn;
1354 __u8 min_fw_build_cw;
1355 __u8 min_fw_build_yy;
1356 __u8 limited_cce;
1357 __u8 unlocked_state;
1358} __packed;
1359
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001360static const struct firmware *btusb_setup_intel_get_fw(struct hci_dev *hdev,
Marcel Holtmann89e75332014-09-16 04:44:50 +02001361 struct intel_version *ver)
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001362{
1363 const struct firmware *fw;
1364 char fwname[64];
1365 int ret;
1366
1367 snprintf(fwname, sizeof(fwname),
1368 "intel/ibt-hw-%x.%x.%x-fw-%x.%x.%x.%x.%x.bseq",
1369 ver->hw_platform, ver->hw_variant, ver->hw_revision,
1370 ver->fw_variant, ver->fw_revision, ver->fw_build_num,
1371 ver->fw_build_ww, ver->fw_build_yy);
1372
1373 ret = request_firmware(&fw, fwname, &hdev->dev);
1374 if (ret < 0) {
1375 if (ret == -EINVAL) {
1376 BT_ERR("%s Intel firmware file request failed (%d)",
1377 hdev->name, ret);
1378 return NULL;
1379 }
1380
1381 BT_ERR("%s failed to open Intel firmware file: %s(%d)",
1382 hdev->name, fwname, ret);
1383
1384 /* If the correct firmware patch file is not found, use the
1385 * default firmware patch file instead
1386 */
1387 snprintf(fwname, sizeof(fwname), "intel/ibt-hw-%x.%x.bseq",
1388 ver->hw_platform, ver->hw_variant);
1389 if (request_firmware(&fw, fwname, &hdev->dev) < 0) {
1390 BT_ERR("%s failed to open default Intel fw file: %s",
1391 hdev->name, fwname);
1392 return NULL;
1393 }
1394 }
1395
1396 BT_INFO("%s: Intel Bluetooth firmware file: %s", hdev->name, fwname);
1397
1398 return fw;
1399}
1400
1401static int btusb_setup_intel_patching(struct hci_dev *hdev,
1402 const struct firmware *fw,
1403 const u8 **fw_ptr, int *disable_patch)
1404{
1405 struct sk_buff *skb;
1406 struct hci_command_hdr *cmd;
1407 const u8 *cmd_param;
1408 struct hci_event_hdr *evt = NULL;
1409 const u8 *evt_param = NULL;
1410 int remain = fw->size - (*fw_ptr - fw->data);
1411
1412 /* The first byte indicates the types of the patch command or event.
1413 * 0x01 means HCI command and 0x02 is HCI event. If the first bytes
1414 * in the current firmware buffer doesn't start with 0x01 or
1415 * the size of remain buffer is smaller than HCI command header,
1416 * the firmware file is corrupted and it should stop the patching
1417 * process.
1418 */
1419 if (remain > HCI_COMMAND_HDR_SIZE && *fw_ptr[0] != 0x01) {
1420 BT_ERR("%s Intel fw corrupted: invalid cmd read", hdev->name);
1421 return -EINVAL;
1422 }
1423 (*fw_ptr)++;
1424 remain--;
1425
1426 cmd = (struct hci_command_hdr *)(*fw_ptr);
1427 *fw_ptr += sizeof(*cmd);
1428 remain -= sizeof(*cmd);
1429
1430 /* Ensure that the remain firmware data is long enough than the length
1431 * of command parameter. If not, the firmware file is corrupted.
1432 */
1433 if (remain < cmd->plen) {
1434 BT_ERR("%s Intel fw corrupted: invalid cmd len", hdev->name);
1435 return -EFAULT;
1436 }
1437
1438 /* If there is a command that loads a patch in the firmware
1439 * file, then enable the patch upon success, otherwise just
1440 * disable the manufacturer mode, for example patch activation
1441 * is not required when the default firmware patch file is used
1442 * because there are no patch data to load.
1443 */
1444 if (*disable_patch && le16_to_cpu(cmd->opcode) == 0xfc8e)
1445 *disable_patch = 0;
1446
1447 cmd_param = *fw_ptr;
1448 *fw_ptr += cmd->plen;
1449 remain -= cmd->plen;
1450
1451 /* This reads the expected events when the above command is sent to the
1452 * device. Some vendor commands expects more than one events, for
1453 * example command status event followed by vendor specific event.
1454 * For this case, it only keeps the last expected event. so the command
1455 * can be sent with __hci_cmd_sync_ev() which returns the sk_buff of
1456 * last expected event.
1457 */
1458 while (remain > HCI_EVENT_HDR_SIZE && *fw_ptr[0] == 0x02) {
1459 (*fw_ptr)++;
1460 remain--;
1461
1462 evt = (struct hci_event_hdr *)(*fw_ptr);
1463 *fw_ptr += sizeof(*evt);
1464 remain -= sizeof(*evt);
1465
1466 if (remain < evt->plen) {
1467 BT_ERR("%s Intel fw corrupted: invalid evt len",
1468 hdev->name);
1469 return -EFAULT;
1470 }
1471
1472 evt_param = *fw_ptr;
1473 *fw_ptr += evt->plen;
1474 remain -= evt->plen;
1475 }
1476
1477 /* Every HCI commands in the firmware file has its correspond event.
1478 * If event is not found or remain is smaller than zero, the firmware
1479 * file is corrupted.
1480 */
1481 if (!evt || !evt_param || remain < 0) {
1482 BT_ERR("%s Intel fw corrupted: invalid evt read", hdev->name);
1483 return -EFAULT;
1484 }
1485
1486 skb = __hci_cmd_sync_ev(hdev, le16_to_cpu(cmd->opcode), cmd->plen,
1487 cmd_param, evt->evt, HCI_INIT_TIMEOUT);
1488 if (IS_ERR(skb)) {
1489 BT_ERR("%s sending Intel patch command (0x%4.4x) failed (%ld)",
1490 hdev->name, cmd->opcode, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001491 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001492 }
1493
1494 /* It ensures that the returned event matches the event data read from
1495 * the firmware file. At fist, it checks the length and then
1496 * the contents of the event.
1497 */
1498 if (skb->len != evt->plen) {
1499 BT_ERR("%s mismatch event length (opcode 0x%4.4x)", hdev->name,
1500 le16_to_cpu(cmd->opcode));
1501 kfree_skb(skb);
1502 return -EFAULT;
1503 }
1504
1505 if (memcmp(skb->data, evt_param, evt->plen)) {
1506 BT_ERR("%s mismatch event parameter (opcode 0x%4.4x)",
1507 hdev->name, le16_to_cpu(cmd->opcode));
1508 kfree_skb(skb);
1509 return -EFAULT;
1510 }
1511 kfree_skb(skb);
1512
1513 return 0;
1514}
1515
Marcel Holtmann40cb0982014-07-02 12:06:45 +02001516#define BDADDR_INTEL (&(bdaddr_t) {{0x00, 0x8b, 0x9e, 0x19, 0x03, 0x00}})
1517
1518static int btusb_check_bdaddr_intel(struct hci_dev *hdev)
1519{
1520 struct sk_buff *skb;
1521 struct hci_rp_read_bd_addr *rp;
1522
1523 skb = __hci_cmd_sync(hdev, HCI_OP_READ_BD_ADDR, 0, NULL,
1524 HCI_INIT_TIMEOUT);
1525 if (IS_ERR(skb)) {
1526 BT_ERR("%s reading Intel device address failed (%ld)",
1527 hdev->name, PTR_ERR(skb));
1528 return PTR_ERR(skb);
1529 }
1530
1531 if (skb->len != sizeof(*rp)) {
1532 BT_ERR("%s Intel device address length mismatch", hdev->name);
1533 kfree_skb(skb);
1534 return -EIO;
1535 }
1536
Marcel Holtmann89e75332014-09-16 04:44:50 +02001537 rp = (struct hci_rp_read_bd_addr *)skb->data;
Marcel Holtmann40cb0982014-07-02 12:06:45 +02001538 if (rp->status) {
1539 BT_ERR("%s Intel device address result failed (%02x)",
1540 hdev->name, rp->status);
1541 kfree_skb(skb);
1542 return -bt_to_errno(rp->status);
1543 }
1544
1545 /* For some Intel based controllers, the default Bluetooth device
1546 * address 00:03:19:9E:8B:00 can be found. These controllers are
1547 * fully operational, but have the danger of duplicate addresses
1548 * and that in turn can cause problems with Bluetooth operation.
1549 */
Marcel Holtmann4739b5b2014-07-04 16:54:38 +02001550 if (!bacmp(&rp->bdaddr, BDADDR_INTEL)) {
Marcel Holtmann40cb0982014-07-02 12:06:45 +02001551 BT_ERR("%s found Intel default device address (%pMR)",
1552 hdev->name, &rp->bdaddr);
Marcel Holtmann4739b5b2014-07-04 16:54:38 +02001553 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
1554 }
Marcel Holtmann40cb0982014-07-02 12:06:45 +02001555
1556 kfree_skb(skb);
1557
1558 return 0;
1559}
1560
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001561static int btusb_setup_intel(struct hci_dev *hdev)
1562{
1563 struct sk_buff *skb;
1564 const struct firmware *fw;
1565 const u8 *fw_ptr;
1566 int disable_patch;
1567 struct intel_version *ver;
1568
1569 const u8 mfg_enable[] = { 0x01, 0x00 };
1570 const u8 mfg_disable[] = { 0x00, 0x00 };
1571 const u8 mfg_reset_deactivate[] = { 0x00, 0x01 };
1572 const u8 mfg_reset_activate[] = { 0x00, 0x02 };
1573
1574 BT_DBG("%s", hdev->name);
1575
1576 /* The controller has a bug with the first HCI command sent to it
1577 * returning number of completed commands as zero. This would stall the
1578 * command processing in the Bluetooth core.
1579 *
1580 * As a workaround, send HCI Reset command first which will reset the
1581 * number of completed commands and allow normal command processing
1582 * from now on.
1583 */
1584 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
1585 if (IS_ERR(skb)) {
1586 BT_ERR("%s sending initial HCI reset command failed (%ld)",
1587 hdev->name, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001588 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001589 }
1590 kfree_skb(skb);
1591
1592 /* Read Intel specific controller version first to allow selection of
1593 * which firmware file to load.
1594 *
1595 * The returned information are hardware variant and revision plus
1596 * firmware variant, revision and build number.
1597 */
1598 skb = __hci_cmd_sync(hdev, 0xfc05, 0, NULL, HCI_INIT_TIMEOUT);
1599 if (IS_ERR(skb)) {
1600 BT_ERR("%s reading Intel fw version command failed (%ld)",
1601 hdev->name, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001602 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001603 }
1604
1605 if (skb->len != sizeof(*ver)) {
1606 BT_ERR("%s Intel version event length mismatch", hdev->name);
1607 kfree_skb(skb);
1608 return -EIO;
1609 }
1610
1611 ver = (struct intel_version *)skb->data;
1612 if (ver->status) {
1613 BT_ERR("%s Intel fw version event failed (%02x)", hdev->name,
1614 ver->status);
1615 kfree_skb(skb);
1616 return -bt_to_errno(ver->status);
1617 }
1618
1619 BT_INFO("%s: read Intel version: %02x%02x%02x%02x%02x%02x%02x%02x%02x",
1620 hdev->name, ver->hw_platform, ver->hw_variant,
1621 ver->hw_revision, ver->fw_variant, ver->fw_revision,
1622 ver->fw_build_num, ver->fw_build_ww, ver->fw_build_yy,
1623 ver->fw_patch_num);
1624
1625 /* fw_patch_num indicates the version of patch the device currently
1626 * have. If there is no patch data in the device, it is always 0x00.
1627 * So, if it is other than 0x00, no need to patch the deivce again.
1628 */
1629 if (ver->fw_patch_num) {
1630 BT_INFO("%s: Intel device is already patched. patch num: %02x",
1631 hdev->name, ver->fw_patch_num);
1632 kfree_skb(skb);
Marcel Holtmann40cb0982014-07-02 12:06:45 +02001633 btusb_check_bdaddr_intel(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001634 return 0;
1635 }
1636
1637 /* Opens the firmware patch file based on the firmware version read
1638 * from the controller. If it fails to open the matching firmware
1639 * patch file, it tries to open the default firmware patch file.
1640 * If no patch file is found, allow the device to operate without
1641 * a patch.
1642 */
1643 fw = btusb_setup_intel_get_fw(hdev, ver);
1644 if (!fw) {
1645 kfree_skb(skb);
Marcel Holtmann40cb0982014-07-02 12:06:45 +02001646 btusb_check_bdaddr_intel(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001647 return 0;
1648 }
1649 fw_ptr = fw->data;
1650
1651 /* This Intel specific command enables the manufacturer mode of the
1652 * controller.
1653 *
1654 * Only while this mode is enabled, the driver can download the
1655 * firmware patch data and configuration parameters.
1656 */
1657 skb = __hci_cmd_sync(hdev, 0xfc11, 2, mfg_enable, HCI_INIT_TIMEOUT);
1658 if (IS_ERR(skb)) {
1659 BT_ERR("%s entering Intel manufacturer mode failed (%ld)",
1660 hdev->name, PTR_ERR(skb));
1661 release_firmware(fw);
Adam Leed9c78e92013-07-10 10:02:12 +08001662 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001663 }
1664
1665 if (skb->data[0]) {
1666 u8 evt_status = skb->data[0];
Marcel Holtmann89e75332014-09-16 04:44:50 +02001667
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001668 BT_ERR("%s enable Intel manufacturer mode event failed (%02x)",
1669 hdev->name, evt_status);
1670 kfree_skb(skb);
1671 release_firmware(fw);
1672 return -bt_to_errno(evt_status);
1673 }
1674 kfree_skb(skb);
1675
1676 disable_patch = 1;
1677
1678 /* The firmware data file consists of list of Intel specific HCI
1679 * commands and its expected events. The first byte indicates the
1680 * type of the message, either HCI command or HCI event.
1681 *
1682 * It reads the command and its expected event from the firmware file,
1683 * and send to the controller. Once __hci_cmd_sync_ev() returns,
1684 * the returned event is compared with the event read from the firmware
1685 * file and it will continue until all the messages are downloaded to
1686 * the controller.
1687 *
1688 * Once the firmware patching is completed successfully,
1689 * the manufacturer mode is disabled with reset and activating the
1690 * downloaded patch.
1691 *
1692 * If the firmware patching fails, the manufacturer mode is
1693 * disabled with reset and deactivating the patch.
1694 *
1695 * If the default patch file is used, no reset is done when disabling
1696 * the manufacturer.
1697 */
1698 while (fw->size > fw_ptr - fw->data) {
1699 int ret;
1700
1701 ret = btusb_setup_intel_patching(hdev, fw, &fw_ptr,
1702 &disable_patch);
1703 if (ret < 0)
1704 goto exit_mfg_deactivate;
1705 }
1706
1707 release_firmware(fw);
1708
1709 if (disable_patch)
1710 goto exit_mfg_disable;
1711
1712 /* Patching completed successfully and disable the manufacturer mode
1713 * with reset and activate the downloaded firmware patches.
1714 */
1715 skb = __hci_cmd_sync(hdev, 0xfc11, sizeof(mfg_reset_activate),
1716 mfg_reset_activate, HCI_INIT_TIMEOUT);
1717 if (IS_ERR(skb)) {
1718 BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
1719 hdev->name, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001720 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001721 }
1722 kfree_skb(skb);
1723
1724 BT_INFO("%s: Intel Bluetooth firmware patch completed and activated",
1725 hdev->name);
1726
Marcel Holtmann40cb0982014-07-02 12:06:45 +02001727 btusb_check_bdaddr_intel(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001728 return 0;
1729
1730exit_mfg_disable:
1731 /* Disable the manufacturer mode without reset */
1732 skb = __hci_cmd_sync(hdev, 0xfc11, sizeof(mfg_disable), mfg_disable,
1733 HCI_INIT_TIMEOUT);
1734 if (IS_ERR(skb)) {
1735 BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
1736 hdev->name, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001737 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001738 }
1739 kfree_skb(skb);
1740
1741 BT_INFO("%s: Intel Bluetooth firmware patch completed", hdev->name);
Marcel Holtmann40cb0982014-07-02 12:06:45 +02001742
1743 btusb_check_bdaddr_intel(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001744 return 0;
1745
1746exit_mfg_deactivate:
1747 release_firmware(fw);
1748
1749 /* Patching failed. Disable the manufacturer mode with reset and
1750 * deactivate the downloaded firmware patches.
1751 */
1752 skb = __hci_cmd_sync(hdev, 0xfc11, sizeof(mfg_reset_deactivate),
1753 mfg_reset_deactivate, HCI_INIT_TIMEOUT);
1754 if (IS_ERR(skb)) {
1755 BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
1756 hdev->name, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001757 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001758 }
1759 kfree_skb(skb);
1760
1761 BT_INFO("%s: Intel Bluetooth firmware patch completed and deactivated",
1762 hdev->name);
1763
Marcel Holtmann40cb0982014-07-02 12:06:45 +02001764 btusb_check_bdaddr_intel(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001765 return 0;
1766}
1767
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001768static int inject_cmd_complete(struct hci_dev *hdev, __u16 opcode)
1769{
1770 struct sk_buff *skb;
1771 struct hci_event_hdr *hdr;
1772 struct hci_ev_cmd_complete *evt;
1773
1774 skb = bt_skb_alloc(sizeof(*hdr) + sizeof(*evt) + 1, GFP_ATOMIC);
1775 if (!skb)
1776 return -ENOMEM;
1777
1778 hdr = (struct hci_event_hdr *)skb_put(skb, sizeof(*hdr));
1779 hdr->evt = HCI_EV_CMD_COMPLETE;
1780 hdr->plen = sizeof(*evt) + 1;
1781
1782 evt = (struct hci_ev_cmd_complete *)skb_put(skb, sizeof(*evt));
1783 evt->ncmd = 0x01;
1784 evt->opcode = cpu_to_le16(opcode);
1785
1786 *skb_put(skb, 1) = 0x00;
1787
1788 bt_cb(skb)->pkt_type = HCI_EVENT_PKT;
1789
1790 return hci_recv_frame(hdev, skb);
1791}
1792
1793static int btusb_recv_bulk_intel(struct btusb_data *data, void *buffer,
1794 int count)
1795{
1796 /* When the device is in bootloader mode, then it can send
1797 * events via the bulk endpoint. These events are treated the
1798 * same way as the ones received from the interrupt endpoint.
1799 */
1800 if (test_bit(BTUSB_BOOTLOADER, &data->flags))
1801 return btusb_recv_intr(data, buffer, count);
1802
1803 return btusb_recv_bulk(data, buffer, count);
1804}
1805
1806static int btusb_recv_event_intel(struct hci_dev *hdev, struct sk_buff *skb)
1807{
1808 struct btusb_data *data = hci_get_drvdata(hdev);
1809
1810 if (test_bit(BTUSB_BOOTLOADER, &data->flags)) {
1811 struct hci_event_hdr *hdr = (void *)skb->data;
1812
1813 /* When the firmware loading completes the device sends
1814 * out a vendor specific event indicating the result of
1815 * the firmware loading.
1816 */
1817 if (skb->len == 7 && hdr->evt == 0xff && hdr->plen == 0x05 &&
1818 skb->data[2] == 0x06) {
1819 if (skb->data[3] != 0x00)
1820 test_bit(BTUSB_FIRMWARE_FAILED, &data->flags);
1821
Marcel Holtmannce6bb922015-01-28 01:58:40 -08001822 if (test_and_clear_bit(BTUSB_DOWNLOADING,
1823 &data->flags) &&
Johan Hedberga087a982015-01-30 10:58:54 +02001824 test_bit(BTUSB_FIRMWARE_LOADED, &data->flags)) {
1825 smp_mb__after_atomic();
1826 wake_up_bit(&data->flags, BTUSB_DOWNLOADING);
1827 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001828 }
1829
1830 /* When switching to the operational firmware the device
1831 * sends a vendor specific event indicating that the bootup
1832 * completed.
1833 */
1834 if (skb->len == 9 && hdr->evt == 0xff && hdr->plen == 0x07 &&
1835 skb->data[2] == 0x02) {
Johan Hedbergfad70972015-01-30 10:58:55 +02001836 if (test_and_clear_bit(BTUSB_BOOTING, &data->flags)) {
1837 smp_mb__after_atomic();
1838 wake_up_bit(&data->flags, BTUSB_BOOTING);
1839 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001840 }
1841 }
1842
1843 return hci_recv_frame(hdev, skb);
1844}
1845
1846static int btusb_send_frame_intel(struct hci_dev *hdev, struct sk_buff *skb)
1847{
1848 struct btusb_data *data = hci_get_drvdata(hdev);
1849 struct urb *urb;
1850
1851 BT_DBG("%s", hdev->name);
1852
1853 if (!test_bit(HCI_RUNNING, &hdev->flags))
1854 return -EBUSY;
1855
1856 switch (bt_cb(skb)->pkt_type) {
1857 case HCI_COMMAND_PKT:
1858 if (test_bit(BTUSB_BOOTLOADER, &data->flags)) {
1859 struct hci_command_hdr *cmd = (void *)skb->data;
1860 __u16 opcode = le16_to_cpu(cmd->opcode);
1861
1862 /* When in bootloader mode and the command 0xfc09
1863 * is received, it needs to be send down the
1864 * bulk endpoint. So allocate a bulk URB instead.
1865 */
1866 if (opcode == 0xfc09)
1867 urb = alloc_bulk_urb(hdev, skb);
1868 else
1869 urb = alloc_ctrl_urb(hdev, skb);
1870
1871 /* When the 0xfc01 command is issued to boot into
1872 * the operational firmware, it will actually not
1873 * send a command complete event. To keep the flow
1874 * control working inject that event here.
1875 */
1876 if (opcode == 0xfc01)
1877 inject_cmd_complete(hdev, opcode);
1878 } else {
1879 urb = alloc_ctrl_urb(hdev, skb);
1880 }
1881 if (IS_ERR(urb))
1882 return PTR_ERR(urb);
1883
1884 hdev->stat.cmd_tx++;
1885 return submit_or_queue_tx_urb(hdev, urb);
1886
1887 case HCI_ACLDATA_PKT:
1888 urb = alloc_bulk_urb(hdev, skb);
1889 if (IS_ERR(urb))
1890 return PTR_ERR(urb);
1891
1892 hdev->stat.acl_tx++;
1893 return submit_or_queue_tx_urb(hdev, urb);
1894
1895 case HCI_SCODATA_PKT:
1896 if (hci_conn_num(hdev, SCO_LINK) < 1)
1897 return -ENODEV;
1898
1899 urb = alloc_isoc_urb(hdev, skb);
1900 if (IS_ERR(urb))
1901 return PTR_ERR(urb);
1902
1903 hdev->stat.sco_tx++;
1904 return submit_tx_urb(hdev, urb);
1905 }
1906
1907 return -EILSEQ;
1908}
1909
1910static int btusb_intel_secure_send(struct hci_dev *hdev, u8 fragment_type,
1911 u32 plen, const void *param)
1912{
1913 while (plen > 0) {
1914 struct sk_buff *skb;
1915 u8 cmd_param[253], fragment_len = (plen > 252) ? 252 : plen;
1916
1917 cmd_param[0] = fragment_type;
1918 memcpy(cmd_param + 1, param, fragment_len);
1919
1920 skb = __hci_cmd_sync(hdev, 0xfc09, fragment_len + 1,
1921 cmd_param, HCI_INIT_TIMEOUT);
1922 if (IS_ERR(skb))
1923 return PTR_ERR(skb);
1924
1925 kfree_skb(skb);
1926
1927 plen -= fragment_len;
1928 param += fragment_len;
1929 }
1930
1931 return 0;
1932}
1933
1934static void btusb_intel_version_info(struct hci_dev *hdev,
1935 struct intel_version *ver)
1936{
1937 const char *variant;
1938
1939 switch (ver->fw_variant) {
1940 case 0x06:
1941 variant = "Bootloader";
1942 break;
1943 case 0x23:
1944 variant = "Firmware";
1945 break;
1946 default:
1947 return;
1948 }
1949
1950 BT_INFO("%s: %s revision %u.%u build %u week %u %u", hdev->name,
1951 variant, ver->fw_revision >> 4, ver->fw_revision & 0x0f,
1952 ver->fw_build_num, ver->fw_build_ww, 2000 + ver->fw_build_yy);
1953}
1954
1955static int btusb_setup_intel_new(struct hci_dev *hdev)
1956{
1957 static const u8 reset_param[] = { 0x00, 0x01, 0x00, 0x01,
1958 0x00, 0x08, 0x04, 0x00 };
1959 struct btusb_data *data = hci_get_drvdata(hdev);
1960 struct sk_buff *skb;
1961 struct intel_version *ver;
1962 struct intel_boot_params *params;
1963 const struct firmware *fw;
1964 const u8 *fw_ptr;
1965 char fwname[64];
1966 ktime_t calltime, delta, rettime;
1967 unsigned long long duration;
1968 int err;
1969
1970 BT_DBG("%s", hdev->name);
1971
1972 calltime = ktime_get();
1973
1974 /* Read the Intel version information to determine if the device
1975 * is in bootloader mode or if it already has operational firmware
1976 * loaded.
1977 */
1978 skb = __hci_cmd_sync(hdev, 0xfc05, 0, NULL, HCI_INIT_TIMEOUT);
1979 if (IS_ERR(skb)) {
1980 BT_ERR("%s: Reading Intel version information failed (%ld)",
1981 hdev->name, PTR_ERR(skb));
1982 return PTR_ERR(skb);
1983 }
1984
1985 if (skb->len != sizeof(*ver)) {
1986 BT_ERR("%s: Intel version event size mismatch", hdev->name);
1987 kfree_skb(skb);
1988 return -EILSEQ;
1989 }
1990
1991 ver = (struct intel_version *)skb->data;
1992 if (ver->status) {
1993 BT_ERR("%s: Intel version command failure (%02x)",
1994 hdev->name, ver->status);
1995 err = -bt_to_errno(ver->status);
1996 kfree_skb(skb);
1997 return err;
1998 }
1999
2000 /* The hardware platform number has a fixed value of 0x37 and
2001 * for now only accept this single value.
2002 */
2003 if (ver->hw_platform != 0x37) {
2004 BT_ERR("%s: Unsupported Intel hardware platform (%u)",
2005 hdev->name, ver->hw_platform);
2006 kfree_skb(skb);
2007 return -EINVAL;
2008 }
2009
2010 /* At the moment only the hardware variant iBT 3.0 (LnP/SfP) is
2011 * supported by this firmware loading method. This check has been
2012 * put in place to ensure correct forward compatibility options
2013 * when newer hardware variants come along.
2014 */
2015 if (ver->hw_variant != 0x0b) {
2016 BT_ERR("%s: Unsupported Intel hardware variant (%u)",
2017 hdev->name, ver->hw_variant);
2018 kfree_skb(skb);
2019 return -EINVAL;
2020 }
2021
2022 btusb_intel_version_info(hdev, ver);
2023
2024 /* The firmware variant determines if the device is in bootloader
2025 * mode or is running operational firmware. The value 0x06 identifies
2026 * the bootloader and the value 0x23 identifies the operational
2027 * firmware.
2028 *
2029 * When the operational firmware is already present, then only
2030 * the check for valid Bluetooth device address is needed. This
2031 * determines if the device will be added as configured or
2032 * unconfigured controller.
2033 *
2034 * It is not possible to use the Secure Boot Parameters in this
2035 * case since that command is only available in bootloader mode.
2036 */
2037 if (ver->fw_variant == 0x23) {
2038 kfree_skb(skb);
2039 clear_bit(BTUSB_BOOTLOADER, &data->flags);
2040 btusb_check_bdaddr_intel(hdev);
2041 return 0;
2042 }
2043
2044 /* If the device is not in bootloader mode, then the only possible
2045 * choice is to return an error and abort the device initialization.
2046 */
2047 if (ver->fw_variant != 0x06) {
2048 BT_ERR("%s: Unsupported Intel firmware variant (%u)",
2049 hdev->name, ver->fw_variant);
2050 kfree_skb(skb);
2051 return -ENODEV;
2052 }
2053
2054 kfree_skb(skb);
2055
2056 /* Read the secure boot parameters to identify the operating
2057 * details of the bootloader.
2058 */
2059 skb = __hci_cmd_sync(hdev, 0xfc0d, 0, NULL, HCI_INIT_TIMEOUT);
2060 if (IS_ERR(skb)) {
2061 BT_ERR("%s: Reading Intel boot parameters failed (%ld)",
2062 hdev->name, PTR_ERR(skb));
2063 return PTR_ERR(skb);
2064 }
2065
2066 if (skb->len != sizeof(*params)) {
2067 BT_ERR("%s: Intel boot parameters size mismatch", hdev->name);
2068 kfree_skb(skb);
2069 return -EILSEQ;
2070 }
2071
2072 params = (struct intel_boot_params *)skb->data;
2073 if (params->status) {
2074 BT_ERR("%s: Intel boot parameters command failure (%02x)",
2075 hdev->name, params->status);
2076 err = -bt_to_errno(params->status);
2077 kfree_skb(skb);
2078 return err;
2079 }
2080
2081 BT_INFO("%s: Device revision is %u", hdev->name,
2082 le16_to_cpu(params->dev_revid));
2083
2084 BT_INFO("%s: Secure boot is %s", hdev->name,
2085 params->secure_boot ? "enabled" : "disabled");
2086
2087 BT_INFO("%s: Minimum firmware build %u week %u %u", hdev->name,
2088 params->min_fw_build_nn, params->min_fw_build_cw,
2089 2000 + params->min_fw_build_yy);
2090
2091 /* It is required that every single firmware fragment is acknowledged
2092 * with a command complete event. If the boot parameters indicate
2093 * that this bootloader does not send them, then abort the setup.
2094 */
2095 if (params->limited_cce != 0x00) {
2096 BT_ERR("%s: Unsupported Intel firmware loading method (%u)",
2097 hdev->name, params->limited_cce);
2098 kfree_skb(skb);
2099 return -EINVAL;
2100 }
2101
2102 /* If the OTP has no valid Bluetooth device address, then there will
2103 * also be no valid address for the operational firmware.
2104 */
2105 if (!bacmp(&params->otp_bdaddr, BDADDR_ANY)) {
2106 BT_INFO("%s: No device address configured", hdev->name);
2107 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
2108 }
2109
2110 /* With this Intel bootloader only the hardware variant and device
2111 * revision information are used to select the right firmware.
2112 *
2113 * Currently this bootloader support is limited to hardware variant
2114 * iBT 3.0 (LnP/SfP) which is identified by the value 11 (0x0b).
2115 */
2116 snprintf(fwname, sizeof(fwname), "intel/ibt-11-%u.sfi",
2117 le16_to_cpu(params->dev_revid));
2118
2119 err = request_firmware(&fw, fwname, &hdev->dev);
2120 if (err < 0) {
2121 BT_ERR("%s: Failed to load Intel firmware file (%d)",
2122 hdev->name, err);
2123 kfree_skb(skb);
2124 return err;
2125 }
2126
2127 BT_INFO("%s: Found device firmware: %s", hdev->name, fwname);
2128
2129 kfree_skb(skb);
2130
2131 if (fw->size < 644) {
2132 BT_ERR("%s: Invalid size of firmware file (%zu)",
2133 hdev->name, fw->size);
2134 err = -EBADF;
2135 goto done;
2136 }
2137
2138 set_bit(BTUSB_DOWNLOADING, &data->flags);
2139
2140 /* Start the firmware download transaction with the Init fragment
2141 * represented by the 128 bytes of CSS header.
2142 */
2143 err = btusb_intel_secure_send(hdev, 0x00, 128, fw->data);
2144 if (err < 0) {
2145 BT_ERR("%s: Failed to send firmware header (%d)",
2146 hdev->name, err);
2147 goto done;
2148 }
2149
2150 /* Send the 256 bytes of public key information from the firmware
2151 * as the PKey fragment.
2152 */
2153 err = btusb_intel_secure_send(hdev, 0x03, 256, fw->data + 128);
2154 if (err < 0) {
2155 BT_ERR("%s: Failed to send firmware public key (%d)",
2156 hdev->name, err);
2157 goto done;
2158 }
2159
2160 /* Send the 256 bytes of signature information from the firmware
2161 * as the Sign fragment.
2162 */
2163 err = btusb_intel_secure_send(hdev, 0x02, 256, fw->data + 388);
2164 if (err < 0) {
2165 BT_ERR("%s: Failed to send firmware signature (%d)",
2166 hdev->name, err);
2167 goto done;
2168 }
2169
2170 fw_ptr = fw->data + 644;
2171
2172 while (fw_ptr - fw->data < fw->size) {
2173 struct hci_command_hdr *cmd = (void *)fw_ptr;
2174 u8 cmd_len;
2175
2176 cmd_len = sizeof(*cmd) + cmd->plen;
2177
2178 /* Send each command from the firmware data buffer as
2179 * a single Data fragment.
2180 */
2181 err = btusb_intel_secure_send(hdev, 0x01, cmd_len, fw_ptr);
2182 if (err < 0) {
2183 BT_ERR("%s: Failed to send firmware data (%d)",
2184 hdev->name, err);
2185 goto done;
2186 }
2187
2188 fw_ptr += cmd_len;
2189 }
2190
Marcel Holtmannce6bb922015-01-28 01:58:40 -08002191 set_bit(BTUSB_FIRMWARE_LOADED, &data->flags);
2192
Johan Hedberga087a982015-01-30 10:58:54 +02002193 BT_INFO("%s: Waiting for firmware download to complete", hdev->name);
2194
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002195 /* Before switching the device into operational mode and with that
2196 * booting the loaded firmware, wait for the bootloader notification
2197 * that all fragments have been successfully received.
2198 *
Johan Hedberga087a982015-01-30 10:58:54 +02002199 * When the event processing receives the notification, then the
2200 * BTUSB_DOWNLOADING flag will be cleared.
2201 *
2202 * The firmware loading should not take longer than 5 seconds
2203 * and thus just timeout if that happens and fail the setup
2204 * of this device.
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002205 */
Johan Hedberg129a7692015-02-14 09:33:35 +02002206 err = wait_on_bit_timeout(&data->flags, BTUSB_DOWNLOADING,
2207 TASK_INTERRUPTIBLE,
2208 msecs_to_jiffies(5000));
Johan Hedberga087a982015-01-30 10:58:54 +02002209 if (err == 1) {
2210 BT_ERR("%s: Firmware loading interrupted", hdev->name);
2211 err = -EINTR;
2212 goto done;
2213 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002214
Johan Hedberga087a982015-01-30 10:58:54 +02002215 if (err) {
2216 BT_ERR("%s: Firmware loading timeout", hdev->name);
2217 err = -ETIMEDOUT;
2218 goto done;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002219 }
2220
2221 if (test_bit(BTUSB_FIRMWARE_FAILED, &data->flags)) {
2222 BT_ERR("%s: Firmware loading failed", hdev->name);
2223 err = -ENOEXEC;
2224 goto done;
2225 }
2226
2227 rettime = ktime_get();
2228 delta = ktime_sub(rettime, calltime);
2229 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
2230
2231 BT_INFO("%s: Firmware loaded in %llu usecs", hdev->name, duration);
2232
2233done:
2234 release_firmware(fw);
2235
2236 if (err < 0)
2237 return err;
2238
2239 calltime = ktime_get();
2240
2241 set_bit(BTUSB_BOOTING, &data->flags);
2242
2243 skb = __hci_cmd_sync(hdev, 0xfc01, sizeof(reset_param), reset_param,
2244 HCI_INIT_TIMEOUT);
2245 if (IS_ERR(skb))
2246 return PTR_ERR(skb);
2247
2248 kfree_skb(skb);
2249
2250 /* The bootloader will not indicate when the device is ready. This
2251 * is done by the operational firmware sending bootup notification.
Johan Hedbergfad70972015-01-30 10:58:55 +02002252 *
2253 * Booting into operational firmware should not take longer than
2254 * 1 second. However if that happens, then just fail the setup
2255 * since something went wrong.
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002256 */
Johan Hedbergfad70972015-01-30 10:58:55 +02002257 BT_INFO("%s: Waiting for device to boot", hdev->name);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002258
Johan Hedberg129a7692015-02-14 09:33:35 +02002259 err = wait_on_bit_timeout(&data->flags, BTUSB_BOOTING,
2260 TASK_INTERRUPTIBLE,
2261 msecs_to_jiffies(1000));
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002262
Johan Hedbergfad70972015-01-30 10:58:55 +02002263 if (err == 1) {
2264 BT_ERR("%s: Device boot interrupted", hdev->name);
2265 return -EINTR;
2266 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002267
Johan Hedbergfad70972015-01-30 10:58:55 +02002268 if (err) {
2269 BT_ERR("%s: Device boot timeout", hdev->name);
2270 return -ETIMEDOUT;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002271 }
2272
2273 rettime = ktime_get();
2274 delta = ktime_sub(rettime, calltime);
2275 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
2276
2277 BT_INFO("%s: Device booted in %llu usecs", hdev->name, duration);
2278
2279 clear_bit(BTUSB_BOOTLOADER, &data->flags);
2280
2281 return 0;
2282}
2283
Marcel Holtmann385a7682015-01-28 11:09:56 -08002284static void btusb_hw_error_intel(struct hci_dev *hdev, u8 code)
2285{
2286 struct sk_buff *skb;
2287 u8 type = 0x00;
2288
2289 BT_ERR("%s: Hardware error 0x%2.2x", hdev->name, code);
2290
2291 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
2292 if (IS_ERR(skb)) {
2293 BT_ERR("%s: Reset after hardware error failed (%ld)",
2294 hdev->name, PTR_ERR(skb));
2295 return;
2296 }
2297 kfree_skb(skb);
2298
2299 skb = __hci_cmd_sync(hdev, 0xfc22, 1, &type, HCI_INIT_TIMEOUT);
2300 if (IS_ERR(skb)) {
2301 BT_ERR("%s: Retrieving Intel exception info failed (%ld)",
2302 hdev->name, PTR_ERR(skb));
2303 return;
2304 }
2305
2306 if (skb->len != 13) {
2307 BT_ERR("%s: Exception info size mismatch", hdev->name);
2308 kfree_skb(skb);
2309 return;
2310 }
2311
2312 if (skb->data[0] != 0x00) {
2313 BT_ERR("%s: Exception info command failure (%02x)",
2314 hdev->name, skb->data[0]);
2315 kfree_skb(skb);
2316 return;
2317 }
2318
2319 BT_ERR("%s: Exception info %s", hdev->name, (char *)(skb->data + 1));
2320
2321 kfree_skb(skb);
2322}
2323
Marcel Holtmanncb8d6592014-07-02 11:25:25 +02002324static int btusb_set_bdaddr_intel(struct hci_dev *hdev, const bdaddr_t *bdaddr)
2325{
2326 struct sk_buff *skb;
2327 long ret;
2328
2329 skb = __hci_cmd_sync(hdev, 0xfc31, 6, bdaddr, HCI_INIT_TIMEOUT);
2330 if (IS_ERR(skb)) {
2331 ret = PTR_ERR(skb);
2332 BT_ERR("%s: changing Intel device address failed (%ld)",
Marcel Holtmann89e75332014-09-16 04:44:50 +02002333 hdev->name, ret);
Marcel Holtmanncb8d6592014-07-02 11:25:25 +02002334 return ret;
2335 }
2336 kfree_skb(skb);
2337
2338 return 0;
2339}
2340
Tedd Ho-Jeong Anbfbd45e2015-02-13 09:20:52 -08002341static int btusb_shutdown_intel(struct hci_dev *hdev)
2342{
2343 struct sk_buff *skb;
2344 long ret;
2345
2346 /* Some platforms have an issue with BT LED when the interface is
2347 * down or BT radio is turned off, which takes 5 seconds to BT LED
2348 * goes off. This command turns off the BT LED immediately.
2349 */
2350 skb = __hci_cmd_sync(hdev, 0xfc3f, 0, NULL, HCI_INIT_TIMEOUT);
2351 if (IS_ERR(skb)) {
2352 ret = PTR_ERR(skb);
2353 BT_ERR("%s: turning off Intel device LED failed (%ld)",
2354 hdev->name, ret);
2355 return ret;
2356 }
2357 kfree_skb(skb);
2358
2359 return 0;
2360}
2361
Amitkumar Karwarae8df492014-07-18 14:47:06 -07002362static int btusb_set_bdaddr_marvell(struct hci_dev *hdev,
2363 const bdaddr_t *bdaddr)
2364{
2365 struct sk_buff *skb;
2366 u8 buf[8];
2367 long ret;
2368
2369 buf[0] = 0xfe;
2370 buf[1] = sizeof(bdaddr_t);
2371 memcpy(buf + 2, bdaddr, sizeof(bdaddr_t));
2372
2373 skb = __hci_cmd_sync(hdev, 0xfc22, sizeof(buf), buf, HCI_INIT_TIMEOUT);
2374 if (IS_ERR(skb)) {
2375 ret = PTR_ERR(skb);
2376 BT_ERR("%s: changing Marvell device address failed (%ld)",
2377 hdev->name, ret);
2378 return ret;
2379 }
2380 kfree_skb(skb);
2381
2382 return 0;
2383}
2384
Marcel Holtmann18835df2015-02-14 23:08:47 -08002385static const struct {
2386 u16 subver;
2387 const char *name;
2388} bcm_subver_table[] = {
2389 { 0x210b, "BCM43142A0" }, /* 001.001.011 */
2390 { 0x2112, "BCM4314A0" }, /* 001.001.018 */
2391 { 0x2118, "BCM20702A0" }, /* 001.001.024 */
2392 { 0x2126, "BCM4335A0" }, /* 001.001.038 */
2393 { 0x220e, "BCM20702A1" }, /* 001.002.014 */
2394 { 0x230f, "BCM4354A2" }, /* 001.003.015 */
2395 { 0x4106, "BCM4335B0" }, /* 002.001.006 */
2396 { 0x410e, "BCM20702B0" }, /* 002.001.014 */
2397 { 0x6109, "BCM4335C0" }, /* 003.001.009 */
2398 { 0x610c, "BCM4354" }, /* 003.001.012 */
2399 { }
2400};
2401
Marcel Holtmannc8abb732014-07-02 12:38:22 +02002402#define BDADDR_BCM20702A0 (&(bdaddr_t) {{0x00, 0xa0, 0x02, 0x70, 0x20, 0x00}})
2403
Petri Gynther10d4c672014-05-08 15:50:01 -07002404static int btusb_setup_bcm_patchram(struct hci_dev *hdev)
2405{
2406 struct btusb_data *data = hci_get_drvdata(hdev);
2407 struct usb_device *udev = data->udev;
2408 char fw_name[64];
2409 const struct firmware *fw;
2410 const u8 *fw_ptr;
2411 size_t fw_size;
2412 const struct hci_command_hdr *cmd;
2413 const u8 *cmd_param;
Marcel Holtmann18835df2015-02-14 23:08:47 -08002414 u16 opcode, subver, rev;
2415 const char *hw_name = NULL;
Petri Gynther10d4c672014-05-08 15:50:01 -07002416 struct sk_buff *skb;
2417 struct hci_rp_read_local_version *ver;
Marcel Holtmannc8abb732014-07-02 12:38:22 +02002418 struct hci_rp_read_bd_addr *bda;
Petri Gynther10d4c672014-05-08 15:50:01 -07002419 long ret;
Marcel Holtmann18835df2015-02-14 23:08:47 -08002420 int i;
Petri Gynther10d4c672014-05-08 15:50:01 -07002421
2422 /* Reset */
2423 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
2424 if (IS_ERR(skb)) {
2425 ret = PTR_ERR(skb);
2426 BT_ERR("%s: HCI_OP_RESET failed (%ld)", hdev->name, ret);
Marcel Holtmann18835df2015-02-14 23:08:47 -08002427 return ret;
Petri Gynther10d4c672014-05-08 15:50:01 -07002428 }
2429 kfree_skb(skb);
2430
2431 /* Read Local Version Info */
2432 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
2433 HCI_INIT_TIMEOUT);
2434 if (IS_ERR(skb)) {
2435 ret = PTR_ERR(skb);
2436 BT_ERR("%s: HCI_OP_READ_LOCAL_VERSION failed (%ld)",
Marcel Holtmann89e75332014-09-16 04:44:50 +02002437 hdev->name, ret);
Marcel Holtmann18835df2015-02-14 23:08:47 -08002438 return ret;
Petri Gynther10d4c672014-05-08 15:50:01 -07002439 }
2440
2441 if (skb->len != sizeof(*ver)) {
2442 BT_ERR("%s: HCI_OP_READ_LOCAL_VERSION event length mismatch",
Marcel Holtmann89e75332014-09-16 04:44:50 +02002443 hdev->name);
Petri Gynther10d4c672014-05-08 15:50:01 -07002444 kfree_skb(skb);
Marcel Holtmann18835df2015-02-14 23:08:47 -08002445 return -EIO;
Petri Gynther10d4c672014-05-08 15:50:01 -07002446 }
2447
Marcel Holtmann89e75332014-09-16 04:44:50 +02002448 ver = (struct hci_rp_read_local_version *)skb->data;
Marcel Holtmann18835df2015-02-14 23:08:47 -08002449 rev = le16_to_cpu(ver->hci_rev);
2450 subver = le16_to_cpu(ver->lmp_subver);
Petri Gynther10d4c672014-05-08 15:50:01 -07002451 kfree_skb(skb);
2452
Marcel Holtmann18835df2015-02-14 23:08:47 -08002453 for (i = 0; bcm_subver_table[i].name; i++) {
2454 if (subver == bcm_subver_table[i].subver) {
2455 hw_name = bcm_subver_table[i].name;
2456 break;
2457 }
2458 }
2459
2460 BT_INFO("%s: %s (%3.3u.%3.3u.%3.3u) build %4.4u", hdev->name,
2461 hw_name ? : "BCM", (subver & 0x7000) >> 13,
2462 (subver & 0x1f00) >> 8, (subver & 0x00ff), rev & 0x0fff);
2463
2464 snprintf(fw_name, sizeof(fw_name), "brcm/%s-%4.4x-%4.4x.hcd",
2465 hw_name ? : "BCM",
2466 le16_to_cpu(udev->descriptor.idVendor),
2467 le16_to_cpu(udev->descriptor.idProduct));
2468
2469 ret = request_firmware(&fw, fw_name, &hdev->dev);
2470 if (ret < 0) {
2471 BT_INFO("%s: BCM: patch %s not found", hdev->name, fw_name);
2472 return 0;
2473 }
2474
Petri Gynther10d4c672014-05-08 15:50:01 -07002475 /* Start Download */
2476 skb = __hci_cmd_sync(hdev, 0xfc2e, 0, NULL, HCI_INIT_TIMEOUT);
2477 if (IS_ERR(skb)) {
2478 ret = PTR_ERR(skb);
2479 BT_ERR("%s: BCM: Download Minidrv command failed (%ld)",
Marcel Holtmann89e75332014-09-16 04:44:50 +02002480 hdev->name, ret);
Petri Gynther10d4c672014-05-08 15:50:01 -07002481 goto reset_fw;
2482 }
2483 kfree_skb(skb);
2484
2485 /* 50 msec delay after Download Minidrv completes */
2486 msleep(50);
2487
2488 fw_ptr = fw->data;
2489 fw_size = fw->size;
2490
2491 while (fw_size >= sizeof(*cmd)) {
Marcel Holtmann89e75332014-09-16 04:44:50 +02002492 cmd = (struct hci_command_hdr *)fw_ptr;
Petri Gynther10d4c672014-05-08 15:50:01 -07002493 fw_ptr += sizeof(*cmd);
2494 fw_size -= sizeof(*cmd);
2495
2496 if (fw_size < cmd->plen) {
2497 BT_ERR("%s: BCM: patch %s is corrupted",
Marcel Holtmann89e75332014-09-16 04:44:50 +02002498 hdev->name, fw_name);
Petri Gynther10d4c672014-05-08 15:50:01 -07002499 ret = -EINVAL;
2500 goto reset_fw;
2501 }
2502
2503 cmd_param = fw_ptr;
2504 fw_ptr += cmd->plen;
2505 fw_size -= cmd->plen;
2506
2507 opcode = le16_to_cpu(cmd->opcode);
2508
2509 skb = __hci_cmd_sync(hdev, opcode, cmd->plen, cmd_param,
2510 HCI_INIT_TIMEOUT);
2511 if (IS_ERR(skb)) {
2512 ret = PTR_ERR(skb);
2513 BT_ERR("%s: BCM: patch command %04x failed (%ld)",
Marcel Holtmann89e75332014-09-16 04:44:50 +02002514 hdev->name, opcode, ret);
Petri Gynther10d4c672014-05-08 15:50:01 -07002515 goto reset_fw;
2516 }
2517 kfree_skb(skb);
2518 }
2519
2520 /* 250 msec delay after Launch Ram completes */
2521 msleep(250);
2522
2523reset_fw:
2524 /* Reset */
2525 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
2526 if (IS_ERR(skb)) {
2527 ret = PTR_ERR(skb);
2528 BT_ERR("%s: HCI_OP_RESET failed (%ld)", hdev->name, ret);
2529 goto done;
2530 }
2531 kfree_skb(skb);
2532
2533 /* Read Local Version Info */
2534 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
2535 HCI_INIT_TIMEOUT);
2536 if (IS_ERR(skb)) {
2537 ret = PTR_ERR(skb);
2538 BT_ERR("%s: HCI_OP_READ_LOCAL_VERSION failed (%ld)",
Marcel Holtmann89e75332014-09-16 04:44:50 +02002539 hdev->name, ret);
Petri Gynther10d4c672014-05-08 15:50:01 -07002540 goto done;
2541 }
2542
2543 if (skb->len != sizeof(*ver)) {
2544 BT_ERR("%s: HCI_OP_READ_LOCAL_VERSION event length mismatch",
Marcel Holtmann89e75332014-09-16 04:44:50 +02002545 hdev->name);
Petri Gynther10d4c672014-05-08 15:50:01 -07002546 kfree_skb(skb);
2547 ret = -EIO;
2548 goto done;
2549 }
2550
Marcel Holtmann89e75332014-09-16 04:44:50 +02002551 ver = (struct hci_rp_read_local_version *)skb->data;
Marcel Holtmann18835df2015-02-14 23:08:47 -08002552 rev = le16_to_cpu(ver->hci_rev);
2553 subver = le16_to_cpu(ver->lmp_subver);
Petri Gynther10d4c672014-05-08 15:50:01 -07002554 kfree_skb(skb);
2555
Marcel Holtmann18835df2015-02-14 23:08:47 -08002556 BT_INFO("%s: %s (%3.3u.%3.3u.%3.3u) build %4.4u", hdev->name,
2557 hw_name ? : "BCM", (subver & 0x7000) >> 13,
2558 (subver & 0x1f00) >> 8, (subver & 0x00ff), rev & 0x0fff);
2559
Marcel Holtmannc8abb732014-07-02 12:38:22 +02002560 /* Read BD Address */
2561 skb = __hci_cmd_sync(hdev, HCI_OP_READ_BD_ADDR, 0, NULL,
2562 HCI_INIT_TIMEOUT);
2563 if (IS_ERR(skb)) {
2564 ret = PTR_ERR(skb);
2565 BT_ERR("%s: HCI_OP_READ_BD_ADDR failed (%ld)",
Marcel Holtmann89e75332014-09-16 04:44:50 +02002566 hdev->name, ret);
Marcel Holtmannc8abb732014-07-02 12:38:22 +02002567 goto done;
2568 }
2569
2570 if (skb->len != sizeof(*bda)) {
2571 BT_ERR("%s: HCI_OP_READ_BD_ADDR event length mismatch",
Marcel Holtmann89e75332014-09-16 04:44:50 +02002572 hdev->name);
Marcel Holtmannc8abb732014-07-02 12:38:22 +02002573 kfree_skb(skb);
2574 ret = -EIO;
2575 goto done;
2576 }
2577
Marcel Holtmann89e75332014-09-16 04:44:50 +02002578 bda = (struct hci_rp_read_bd_addr *)skb->data;
Marcel Holtmannc8abb732014-07-02 12:38:22 +02002579 if (bda->status) {
2580 BT_ERR("%s: HCI_OP_READ_BD_ADDR error status (%02x)",
2581 hdev->name, bda->status);
2582 kfree_skb(skb);
2583 ret = -bt_to_errno(bda->status);
2584 goto done;
2585 }
2586
2587 /* The address 00:20:70:02:A0:00 indicates a BCM20702A0 controller
2588 * with no configured address.
2589 */
Marcel Holtmann849e5082014-07-04 16:54:39 +02002590 if (!bacmp(&bda->bdaddr, BDADDR_BCM20702A0)) {
Marcel Holtmannc8abb732014-07-02 12:38:22 +02002591 BT_INFO("%s: BCM: using default device address (%pMR)",
2592 hdev->name, &bda->bdaddr);
Marcel Holtmann849e5082014-07-04 16:54:39 +02002593 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
2594 }
Marcel Holtmannc8abb732014-07-02 12:38:22 +02002595
2596 kfree_skb(skb);
2597
Petri Gynther10d4c672014-05-08 15:50:01 -07002598done:
2599 release_firmware(fw);
2600
2601 return ret;
2602}
2603
Marcel Holtmannabbaf502014-07-02 00:53:48 +02002604static int btusb_set_bdaddr_bcm(struct hci_dev *hdev, const bdaddr_t *bdaddr)
2605{
2606 struct sk_buff *skb;
2607 long ret;
2608
2609 skb = __hci_cmd_sync(hdev, 0xfc01, 6, bdaddr, HCI_INIT_TIMEOUT);
2610 if (IS_ERR(skb)) {
2611 ret = PTR_ERR(skb);
2612 BT_ERR("%s: BCM: Change address command failed (%ld)",
Marcel Holtmann89e75332014-09-16 04:44:50 +02002613 hdev->name, ret);
Marcel Holtmannabbaf502014-07-02 00:53:48 +02002614 return ret;
2615 }
2616 kfree_skb(skb);
2617
2618 return 0;
2619}
2620
Toshi Kikuchi58592232014-12-12 10:58:05 -08002621static int btusb_set_bdaddr_ath3012(struct hci_dev *hdev,
2622 const bdaddr_t *bdaddr)
2623{
2624 struct sk_buff *skb;
2625 u8 buf[10];
2626 long ret;
2627
2628 buf[0] = 0x01;
2629 buf[1] = 0x01;
2630 buf[2] = 0x00;
2631 buf[3] = sizeof(bdaddr_t);
2632 memcpy(buf + 4, bdaddr, sizeof(bdaddr_t));
2633
2634 skb = __hci_cmd_sync(hdev, 0xfc0b, sizeof(buf), buf, HCI_INIT_TIMEOUT);
2635 if (IS_ERR(skb)) {
2636 ret = PTR_ERR(skb);
2637 BT_ERR("%s: Change address command failed (%ld)",
2638 hdev->name, ret);
2639 return ret;
2640 }
2641 kfree_skb(skb);
2642
2643 return 0;
2644}
2645
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002646#define QCA_DFU_PACKET_LEN 4096
2647
2648#define QCA_GET_TARGET_VERSION 0x09
2649#define QCA_CHECK_STATUS 0x05
2650#define QCA_DFU_DOWNLOAD 0x01
2651
2652#define QCA_SYSCFG_UPDATED 0x40
2653#define QCA_PATCH_UPDATED 0x80
2654#define QCA_DFU_TIMEOUT 3000
2655
2656struct qca_version {
2657 __le32 rom_version;
2658 __le32 patch_version;
2659 __le32 ram_version;
2660 __le32 ref_clock;
2661 __u8 reserved[4];
2662} __packed;
2663
2664struct qca_rampatch_version {
2665 __le16 rom_version;
2666 __le16 patch_version;
2667} __packed;
2668
2669struct qca_device_info {
2670 __le32 rom_version;
2671 __u8 rampatch_hdr; /* length of header in rampatch */
2672 __u8 nvm_hdr; /* length of header in NVM */
2673 __u8 ver_offset; /* offset of version structure in rampatch */
2674};
2675
2676static const struct qca_device_info qca_devices_table[] = {
2677 { 0x00000100, 20, 4, 10 }, /* Rome 1.0 */
2678 { 0x00000101, 20, 4, 10 }, /* Rome 1.1 */
2679 { 0x00000201, 28, 4, 18 }, /* Rome 2.1 */
2680 { 0x00000300, 28, 4, 18 }, /* Rome 3.0 */
2681 { 0x00000302, 28, 4, 18 }, /* Rome 3.2 */
2682};
2683
2684static int btusb_qca_send_vendor_req(struct hci_dev *hdev, u8 request,
2685 void *data, u16 size)
2686{
2687 struct btusb_data *btdata = hci_get_drvdata(hdev);
2688 struct usb_device *udev = btdata->udev;
2689 int pipe, err;
2690 u8 *buf;
2691
2692 buf = kmalloc(size, GFP_KERNEL);
2693 if (!buf)
2694 return -ENOMEM;
2695
2696 /* Found some of USB hosts have IOT issues with ours so that we should
2697 * not wait until HCI layer is ready.
2698 */
2699 pipe = usb_rcvctrlpipe(udev, 0);
2700 err = usb_control_msg(udev, pipe, request, USB_TYPE_VENDOR | USB_DIR_IN,
2701 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
2702 if (err < 0) {
2703 BT_ERR("%s: Failed to access otp area (%d)", hdev->name, err);
2704 goto done;
2705 }
2706
2707 memcpy(data, buf, size);
2708
2709done:
2710 kfree(buf);
2711
2712 return err;
2713}
2714
2715static int btusb_setup_qca_download_fw(struct hci_dev *hdev,
2716 const struct firmware *firmware,
2717 size_t hdr_size)
2718{
2719 struct btusb_data *btdata = hci_get_drvdata(hdev);
2720 struct usb_device *udev = btdata->udev;
2721 size_t count, size, sent = 0;
2722 int pipe, len, err;
2723 u8 *buf;
2724
2725 buf = kmalloc(QCA_DFU_PACKET_LEN, GFP_KERNEL);
2726 if (!buf)
2727 return -ENOMEM;
2728
2729 count = firmware->size;
2730
2731 size = min_t(size_t, count, hdr_size);
2732 memcpy(buf, firmware->data, size);
2733
2734 /* USB patches should go down to controller through USB path
2735 * because binary format fits to go down through USB channel.
2736 * USB control path is for patching headers and USB bulk is for
2737 * patch body.
2738 */
2739 pipe = usb_sndctrlpipe(udev, 0);
2740 err = usb_control_msg(udev, pipe, QCA_DFU_DOWNLOAD, USB_TYPE_VENDOR,
2741 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
2742 if (err < 0) {
2743 BT_ERR("%s: Failed to send headers (%d)", hdev->name, err);
2744 goto done;
2745 }
2746
2747 sent += size;
2748 count -= size;
2749
2750 while (count) {
2751 size = min_t(size_t, count, QCA_DFU_PACKET_LEN);
2752
2753 memcpy(buf, firmware->data + sent, size);
2754
2755 pipe = usb_sndbulkpipe(udev, 0x02);
2756 err = usb_bulk_msg(udev, pipe, buf, size, &len,
2757 QCA_DFU_TIMEOUT);
2758 if (err < 0) {
2759 BT_ERR("%s: Failed to send body at %zd of %zd (%d)",
2760 hdev->name, sent, firmware->size, err);
2761 break;
2762 }
2763
2764 if (size != len) {
2765 BT_ERR("%s: Failed to get bulk buffer", hdev->name);
2766 err = -EILSEQ;
2767 break;
2768 }
2769
2770 sent += size;
2771 count -= size;
2772 }
2773
2774done:
2775 kfree(buf);
2776 return err;
2777}
2778
2779static int btusb_setup_qca_load_rampatch(struct hci_dev *hdev,
2780 struct qca_version *ver,
2781 const struct qca_device_info *info)
2782{
2783 struct qca_rampatch_version *rver;
2784 const struct firmware *fw;
2785 char fwname[64];
2786 int err;
2787
2788 snprintf(fwname, sizeof(fwname), "qca/rampatch_usb_%08x.bin",
2789 le32_to_cpu(ver->rom_version));
2790
2791 err = request_firmware(&fw, fwname, &hdev->dev);
2792 if (err) {
2793 BT_ERR("%s: failed to request rampatch file: %s (%d)",
2794 hdev->name, fwname, err);
2795 return err;
2796 }
2797
2798 BT_INFO("%s: using rampatch file: %s", hdev->name, fwname);
2799 rver = (struct qca_rampatch_version *)(fw->data + info->ver_offset);
2800 BT_INFO("%s: QCA: patch rome 0x%x build 0x%x, firmware rome 0x%x "
2801 "build 0x%x", hdev->name, le16_to_cpu(rver->rom_version),
2802 le16_to_cpu(rver->patch_version), le32_to_cpu(ver->rom_version),
2803 le32_to_cpu(ver->patch_version));
2804
2805 if (rver->rom_version != ver->rom_version ||
2806 rver->patch_version <= ver->patch_version) {
2807 BT_ERR("%s: rampatch file version did not match with firmware",
2808 hdev->name);
2809 err = -EINVAL;
2810 goto done;
2811 }
2812
2813 err = btusb_setup_qca_download_fw(hdev, fw, info->rampatch_hdr);
2814
2815done:
2816 release_firmware(fw);
2817
2818 return err;
2819}
2820
2821static int btusb_setup_qca_load_nvm(struct hci_dev *hdev,
2822 struct qca_version *ver,
2823 const struct qca_device_info *info)
2824{
2825 const struct firmware *fw;
2826 char fwname[64];
2827 int err;
2828
2829 snprintf(fwname, sizeof(fwname), "qca/nvm_usb_%08x.bin",
2830 le32_to_cpu(ver->rom_version));
2831
2832 err = request_firmware(&fw, fwname, &hdev->dev);
2833 if (err) {
2834 BT_ERR("%s: failed to request NVM file: %s (%d)",
2835 hdev->name, fwname, err);
2836 return err;
2837 }
2838
2839 BT_INFO("%s: using NVM file: %s", hdev->name, fwname);
2840
2841 err = btusb_setup_qca_download_fw(hdev, fw, info->nvm_hdr);
2842
2843 release_firmware(fw);
2844
2845 return err;
2846}
2847
2848static int btusb_setup_qca(struct hci_dev *hdev)
2849{
2850 const struct qca_device_info *info = NULL;
2851 struct qca_version ver;
2852 u8 status;
2853 int i, err;
2854
2855 err = btusb_qca_send_vendor_req(hdev, QCA_GET_TARGET_VERSION, &ver,
2856 sizeof(ver));
2857 if (err < 0)
2858 return err;
2859
2860 for (i = 0; i < ARRAY_SIZE(qca_devices_table); i++) {
2861 if (ver.rom_version == qca_devices_table[i].rom_version)
2862 info = &qca_devices_table[i];
2863 }
2864 if (!info) {
2865 BT_ERR("%s: don't support firmware rome 0x%x", hdev->name,
2866 le32_to_cpu(ver.rom_version));
2867 return -ENODEV;
2868 }
2869
2870 err = btusb_qca_send_vendor_req(hdev, QCA_CHECK_STATUS, &status,
2871 sizeof(status));
2872 if (err < 0)
2873 return err;
2874
2875 if (!(status & QCA_PATCH_UPDATED)) {
2876 err = btusb_setup_qca_load_rampatch(hdev, &ver, info);
2877 if (err < 0)
2878 return err;
2879 }
2880
2881 if (!(status & QCA_SYSCFG_UPDATED)) {
2882 err = btusb_setup_qca_load_nvm(hdev, &ver, info);
2883 if (err < 0)
2884 return err;
2885 }
2886
2887 return 0;
2888}
2889
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002890static int btusb_probe(struct usb_interface *intf,
Marcel Holtmann89e75332014-09-16 04:44:50 +02002891 const struct usb_device_id *id)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002892{
2893 struct usb_endpoint_descriptor *ep_desc;
2894 struct btusb_data *data;
2895 struct hci_dev *hdev;
2896 int i, err;
2897
2898 BT_DBG("intf %p id %p", intf, id);
2899
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002900 /* interface numbers are hardcoded in the spec */
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002901 if (intf->cur_altsetting->desc.bInterfaceNumber != 0)
2902 return -ENODEV;
2903
2904 if (!id->driver_info) {
2905 const struct usb_device_id *match;
Marcel Holtmann89e75332014-09-16 04:44:50 +02002906
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002907 match = usb_match_id(intf, blacklist_table);
2908 if (match)
2909 id = match;
2910 }
2911
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002912 if (id->driver_info == BTUSB_IGNORE)
2913 return -ENODEV;
2914
Steven.Li2d25f8b2011-07-01 14:02:36 +08002915 if (id->driver_info & BTUSB_ATH3012) {
2916 struct usb_device *udev = interface_to_usbdev(intf);
2917
2918 /* Old firmware would otherwise let ath3k driver load
2919 * patch and sysconfig files */
2920 if (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x0001)
2921 return -ENODEV;
2922 }
2923
Sachin Kamat98921db2012-07-27 12:38:39 +05302924 data = devm_kzalloc(&intf->dev, sizeof(*data), GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002925 if (!data)
2926 return -ENOMEM;
2927
2928 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
2929 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
2930
2931 if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
2932 data->intr_ep = ep_desc;
2933 continue;
2934 }
2935
2936 if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
2937 data->bulk_tx_ep = ep_desc;
2938 continue;
2939 }
2940
2941 if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
2942 data->bulk_rx_ep = ep_desc;
2943 continue;
2944 }
2945 }
2946
Sachin Kamat98921db2012-07-27 12:38:39 +05302947 if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002948 return -ENODEV;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002949
Marcel Holtmann893ba542015-01-28 20:27:34 -08002950 if (id->driver_info & BTUSB_AMP) {
2951 data->cmdreq_type = USB_TYPE_CLASS | 0x01;
2952 data->cmdreq = 0x2b;
2953 } else {
2954 data->cmdreq_type = USB_TYPE_CLASS;
2955 data->cmdreq = 0x00;
2956 }
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002957
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002958 data->udev = interface_to_usbdev(intf);
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02002959 data->intf = intf;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002960
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002961 INIT_WORK(&data->work, btusb_work);
Oliver Neukum7bee5492009-08-24 23:44:59 +02002962 INIT_WORK(&data->waker, btusb_waker);
Marcel Holtmann803b5832014-09-16 08:00:29 +02002963 init_usb_anchor(&data->deferred);
2964 init_usb_anchor(&data->tx_anchor);
Oliver Neukum7bee5492009-08-24 23:44:59 +02002965 spin_lock_init(&data->txlock);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002966
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002967 init_usb_anchor(&data->intr_anchor);
2968 init_usb_anchor(&data->bulk_anchor);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02002969 init_usb_anchor(&data->isoc_anchor);
Marcel Holtmann803b5832014-09-16 08:00:29 +02002970 spin_lock_init(&data->rxlock);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002971
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002972 if (id->driver_info & BTUSB_INTEL_NEW) {
2973 data->recv_event = btusb_recv_event_intel;
2974 data->recv_bulk = btusb_recv_bulk_intel;
2975 set_bit(BTUSB_BOOTLOADER, &data->flags);
2976 } else {
2977 data->recv_event = hci_recv_frame;
2978 data->recv_bulk = btusb_recv_bulk;
2979 }
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +01002980
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002981 hdev = hci_alloc_dev();
Sachin Kamat98921db2012-07-27 12:38:39 +05302982 if (!hdev)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002983 return -ENOMEM;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002984
Marcel Holtmannc13854c2010-02-08 15:27:07 +01002985 hdev->bus = HCI_USB;
David Herrmann155961e2012-02-09 21:58:32 +01002986 hci_set_drvdata(hdev, data);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002987
Marcel Holtmann893ba542015-01-28 20:27:34 -08002988 if (id->driver_info & BTUSB_AMP)
2989 hdev->dev_type = HCI_AMP;
2990 else
2991 hdev->dev_type = HCI_BREDR;
2992
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002993 data->hdev = hdev;
2994
2995 SET_HCIDEV_DEV(hdev, &intf->dev);
2996
Marcel Holtmann9f8f9622013-04-10 08:11:35 -07002997 hdev->open = btusb_open;
2998 hdev->close = btusb_close;
2999 hdev->flush = btusb_flush;
3000 hdev->send = btusb_send_frame;
3001 hdev->notify = btusb_notify;
3002
3003 if (id->driver_info & BTUSB_BCM92035)
3004 hdev->setup = btusb_setup_bcm92035;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003005
Marcel Holtmannabbaf502014-07-02 00:53:48 +02003006 if (id->driver_info & BTUSB_BCM_PATCHRAM) {
Petri Gynther10d4c672014-05-08 15:50:01 -07003007 hdev->setup = btusb_setup_bcm_patchram;
Marcel Holtmannabbaf502014-07-02 00:53:48 +02003008 hdev->set_bdaddr = btusb_set_bdaddr_bcm;
Marcel Holtmann27c3fbe2014-11-02 20:52:24 +01003009 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
Marcel Holtmannabbaf502014-07-02 00:53:48 +02003010 }
Petri Gynther10d4c672014-05-08 15:50:01 -07003011
Marcel Holtmanncb8d6592014-07-02 11:25:25 +02003012 if (id->driver_info & BTUSB_INTEL) {
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07003013 hdev->setup = btusb_setup_intel;
Tedd Ho-Jeong Anbfbd45e2015-02-13 09:20:52 -08003014 hdev->shutdown = btusb_shutdown_intel;
Marcel Holtmanncb8d6592014-07-02 11:25:25 +02003015 hdev->set_bdaddr = btusb_set_bdaddr_intel;
Jakub Pawlowskic33fb9b2015-01-30 18:55:58 -08003016 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
Marcel Holtmanncb8d6592014-07-02 11:25:25 +02003017 }
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07003018
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08003019 if (id->driver_info & BTUSB_INTEL_NEW) {
3020 hdev->send = btusb_send_frame_intel;
3021 hdev->setup = btusb_setup_intel_new;
Marcel Holtmann385a7682015-01-28 11:09:56 -08003022 hdev->hw_error = btusb_hw_error_intel;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08003023 hdev->set_bdaddr = btusb_set_bdaddr_intel;
Marcel Holtmannb970c5b2015-02-01 23:57:18 -08003024 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08003025 }
3026
Amitkumar Karwarae8df492014-07-18 14:47:06 -07003027 if (id->driver_info & BTUSB_MARVELL)
3028 hdev->set_bdaddr = btusb_set_bdaddr_marvell;
3029
Marcel Holtmann661cf882015-01-02 23:35:20 -08003030 if (id->driver_info & BTUSB_SWAVE) {
3031 set_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks);
Marcel Holtmannd57dbe72014-12-26 04:42:33 +01003032 set_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS, &hdev->quirks);
Marcel Holtmann661cf882015-01-02 23:35:20 -08003033 }
Marcel Holtmannd57dbe72014-12-26 04:42:33 +01003034
Marcel Holtmann40df7832014-07-06 13:29:58 +02003035 if (id->driver_info & BTUSB_INTEL_BOOT)
3036 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
3037
Jakub Pawlowski79f0c872015-01-29 10:38:34 -08003038 if (id->driver_info & BTUSB_ATH3012) {
Toshi Kikuchi58592232014-12-12 10:58:05 -08003039 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
Jakub Pawlowski79f0c872015-01-29 10:38:34 -08003040 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
3041 }
Toshi Kikuchi58592232014-12-12 10:58:05 -08003042
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003043 if (id->driver_info & BTUSB_QCA_ROME) {
3044 data->setup_on_usb = btusb_setup_qca;
3045 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
3046 }
3047
Marcel Holtmann893ba542015-01-28 20:27:34 -08003048 if (id->driver_info & BTUSB_AMP) {
3049 /* AMP controllers do not support SCO packets */
3050 data->isoc = NULL;
3051 } else {
3052 /* Interface numbers are hardcoded in the specification */
3053 data->isoc = usb_ifnum_to_if(data->udev, 1);
3054 }
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02003055
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01003056 if (!reset)
Szymon Janca6c511c2012-05-23 12:35:46 +02003057 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02003058
3059 if (force_scofix || id->driver_info & BTUSB_WRONG_SCO_MTU) {
3060 if (!disable_scofix)
3061 set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
3062 }
3063
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02003064 if (id->driver_info & BTUSB_BROKEN_ISOC)
3065 data->isoc = NULL;
3066
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01003067 if (id->driver_info & BTUSB_DIGIANSWER) {
3068 data->cmdreq_type = USB_TYPE_VENDOR;
Szymon Janca6c511c2012-05-23 12:35:46 +02003069 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01003070 }
3071
3072 if (id->driver_info & BTUSB_CSR) {
3073 struct usb_device *udev = data->udev;
Marcel Holtmann81cac642014-01-03 03:02:36 -08003074 u16 bcdDevice = le16_to_cpu(udev->descriptor.bcdDevice);
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01003075
3076 /* Old firmware would otherwise execute USB reset */
Marcel Holtmann81cac642014-01-03 03:02:36 -08003077 if (bcdDevice < 0x117)
Szymon Janca6c511c2012-05-23 12:35:46 +02003078 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmann81cac642014-01-03 03:02:36 -08003079
3080 /* Fake CSR devices with broken commands */
3081 if (bcdDevice <= 0x100)
3082 hdev->setup = btusb_setup_csr;
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01003083 }
3084
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02003085 if (id->driver_info & BTUSB_SNIFFER) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02003086 struct usb_device *udev = data->udev;
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02003087
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01003088 /* New sniffer firmware has crippled HCI interface */
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02003089 if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
3090 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
3091 }
3092
Marcel Holtmann3a5ef202014-07-06 14:53:54 +02003093 if (id->driver_info & BTUSB_INTEL_BOOT) {
3094 /* A bug in the bootloader causes that interrupt interface is
3095 * only enabled after receiving SetInterface(0, AltSetting=0).
3096 */
3097 err = usb_set_interface(data->udev, 0, 0);
3098 if (err < 0) {
3099 BT_ERR("failed to set interface 0, alt 0 %d", err);
3100 hci_free_dev(hdev);
3101 return err;
3102 }
3103 }
3104
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02003105 if (data->isoc) {
3106 err = usb_driver_claim_interface(&btusb_driver,
Marcel Holtmann89e75332014-09-16 04:44:50 +02003107 data->isoc, data);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02003108 if (err < 0) {
3109 hci_free_dev(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02003110 return err;
3111 }
3112 }
3113
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003114 err = hci_register_dev(hdev);
3115 if (err < 0) {
3116 hci_free_dev(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003117 return err;
3118 }
3119
3120 usb_set_intfdata(intf, data);
3121
3122 return 0;
3123}
3124
3125static void btusb_disconnect(struct usb_interface *intf)
3126{
3127 struct btusb_data *data = usb_get_intfdata(intf);
3128 struct hci_dev *hdev;
3129
3130 BT_DBG("intf %p", intf);
3131
3132 if (!data)
3133 return;
3134
3135 hdev = data->hdev;
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02003136 usb_set_intfdata(data->intf, NULL);
3137
3138 if (data->isoc)
3139 usb_set_intfdata(data->isoc, NULL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003140
3141 hci_unregister_dev(hdev);
3142
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02003143 if (intf == data->isoc)
3144 usb_driver_release_interface(&btusb_driver, data->intf);
3145 else if (data->isoc)
3146 usb_driver_release_interface(&btusb_driver, data->isoc);
3147
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003148 hci_free_dev(hdev);
3149}
3150
Oliver Neukum7bee5492009-08-24 23:44:59 +02003151#ifdef CONFIG_PM
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003152static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
3153{
3154 struct btusb_data *data = usb_get_intfdata(intf);
3155
3156 BT_DBG("intf %p", intf);
3157
3158 if (data->suspend_count++)
3159 return 0;
3160
Oliver Neukum7bee5492009-08-24 23:44:59 +02003161 spin_lock_irq(&data->txlock);
Alan Stern5b1b0b82011-08-19 23:49:48 +02003162 if (!(PMSG_IS_AUTO(message) && data->tx_in_flight)) {
Oliver Neukum7bee5492009-08-24 23:44:59 +02003163 set_bit(BTUSB_SUSPENDING, &data->flags);
3164 spin_unlock_irq(&data->txlock);
3165 } else {
3166 spin_unlock_irq(&data->txlock);
3167 data->suspend_count--;
3168 return -EBUSY;
3169 }
3170
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003171 cancel_work_sync(&data->work);
3172
Oliver Neukum7bee5492009-08-24 23:44:59 +02003173 btusb_stop_traffic(data);
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003174 usb_kill_anchored_urbs(&data->tx_anchor);
3175
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003176 return 0;
3177}
3178
Oliver Neukum7bee5492009-08-24 23:44:59 +02003179static void play_deferred(struct btusb_data *data)
3180{
3181 struct urb *urb;
3182 int err;
3183
3184 while ((urb = usb_get_from_anchor(&data->deferred))) {
3185 err = usb_submit_urb(urb, GFP_ATOMIC);
3186 if (err < 0)
3187 break;
3188
3189 data->tx_in_flight++;
3190 }
3191 usb_scuttle_anchored_urbs(&data->deferred);
3192}
3193
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003194static int btusb_resume(struct usb_interface *intf)
3195{
3196 struct btusb_data *data = usb_get_intfdata(intf);
3197 struct hci_dev *hdev = data->hdev;
Oliver Neukum7bee5492009-08-24 23:44:59 +02003198 int err = 0;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003199
3200 BT_DBG("intf %p", intf);
3201
3202 if (--data->suspend_count)
3203 return 0;
3204
3205 if (!test_bit(HCI_RUNNING, &hdev->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +02003206 goto done;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003207
3208 if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
3209 err = btusb_submit_intr_urb(hdev, GFP_NOIO);
3210 if (err < 0) {
3211 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02003212 goto failed;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003213 }
3214 }
3215
3216 if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) {
Marcel Holtmann43c2e572009-02-04 17:41:38 +01003217 err = btusb_submit_bulk_urb(hdev, GFP_NOIO);
3218 if (err < 0) {
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003219 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02003220 goto failed;
3221 }
3222
3223 btusb_submit_bulk_urb(hdev, GFP_NOIO);
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003224 }
3225
3226 if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
3227 if (btusb_submit_isoc_urb(hdev, GFP_NOIO) < 0)
3228 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
3229 else
3230 btusb_submit_isoc_urb(hdev, GFP_NOIO);
3231 }
3232
Oliver Neukum7bee5492009-08-24 23:44:59 +02003233 spin_lock_irq(&data->txlock);
3234 play_deferred(data);
3235 clear_bit(BTUSB_SUSPENDING, &data->flags);
3236 spin_unlock_irq(&data->txlock);
3237 schedule_work(&data->work);
3238
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003239 return 0;
Oliver Neukum7bee5492009-08-24 23:44:59 +02003240
3241failed:
3242 usb_scuttle_anchored_urbs(&data->deferred);
3243done:
3244 spin_lock_irq(&data->txlock);
3245 clear_bit(BTUSB_SUSPENDING, &data->flags);
3246 spin_unlock_irq(&data->txlock);
3247
3248 return err;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003249}
Oliver Neukum7bee5492009-08-24 23:44:59 +02003250#endif
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003251
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003252static struct usb_driver btusb_driver = {
3253 .name = "btusb",
3254 .probe = btusb_probe,
3255 .disconnect = btusb_disconnect,
Oliver Neukum7bee5492009-08-24 23:44:59 +02003256#ifdef CONFIG_PM
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003257 .suspend = btusb_suspend,
3258 .resume = btusb_resume,
Oliver Neukum7bee5492009-08-24 23:44:59 +02003259#endif
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003260 .id_table = btusb_table,
Oliver Neukum7bee5492009-08-24 23:44:59 +02003261 .supports_autosuspend = 1,
Sarah Sharpe1f12eb2012-04-23 10:08:51 -07003262 .disable_hub_initiated_lpm = 1,
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003263};
3264
Greg Kroah-Hartman93f15082011-11-18 09:47:34 -08003265module_usb_driver(btusb_driver);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003266
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02003267module_param(disable_scofix, bool, 0644);
3268MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
3269
3270module_param(force_scofix, bool, 0644);
3271MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
3272
3273module_param(reset, bool, 0644);
3274MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
3275
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003276MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
3277MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
3278MODULE_VERSION(VERSION);
3279MODULE_LICENSE("GPL");