blob: 7da7efc2cd677a5db2475bf990278369e1e79fe2 [file] [log] [blame]
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001/*
2 *
3 * Generic Bluetooth USB driver
4 *
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02005 * Copyright (C) 2005-2008 Marcel Holtmann <marcel@holtmann.org>
Marcel Holtmann5e23b922007-10-20 14:12:34 +02006 *
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 */
23
Marcel Holtmann5e23b922007-10-20 14:12:34 +020024#include <linux/module.h>
Marcel Holtmann5e23b922007-10-20 14:12:34 +020025#include <linux/usb.h>
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -070026#include <linux/firmware.h>
Daniel Drakea2698a92015-04-16 14:09:55 -060027#include <asm/unaligned.h>
Marcel Holtmann5e23b922007-10-20 14:12:34 +020028
29#include <net/bluetooth/bluetooth.h>
30#include <net/bluetooth/hci_core.h>
31
Marcel Holtmann4185a0f2015-04-06 00:52:12 -070032#include "btintel.h"
Marcel Holtmann1df1f592015-04-05 22:52:11 -070033#include "btbcm.h"
34
Marcel Holtmann34dced9b2015-04-05 22:52:16 -070035#define VERSION "0.8"
Marcel Holtmanncfeb4142008-08-07 22:26:56 +020036
Rusty Russell90ab5ee2012-01-13 09:32:20 +103037static bool disable_scofix;
38static bool force_scofix;
Marcel Holtmann7a9d4022008-11-30 12:17:26 +010039
Rusty Russell90ab5ee2012-01-13 09:32:20 +103040static bool reset = 1;
Marcel Holtmanncfeb4142008-08-07 22:26:56 +020041
42static struct usb_driver btusb_driver;
43
44#define BTUSB_IGNORE 0x01
Marcel Holtmann7a9d4022008-11-30 12:17:26 +010045#define BTUSB_DIGIANSWER 0x02
46#define BTUSB_CSR 0x04
47#define BTUSB_SNIFFER 0x08
48#define BTUSB_BCM92035 0x10
49#define BTUSB_BROKEN_ISOC 0x20
50#define BTUSB_WRONG_SCO_MTU 0x40
Steven.Li2d25f8b2011-07-01 14:02:36 +080051#define BTUSB_ATH3012 0x80
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -070052#define BTUSB_INTEL 0x100
Marcel Holtmann40df7832014-07-06 13:29:58 +020053#define BTUSB_INTEL_BOOT 0x200
54#define BTUSB_BCM_PATCHRAM 0x400
Amitkumar Karwarae8df492014-07-18 14:47:06 -070055#define BTUSB_MARVELL 0x800
Marcel Holtmann4fcef8e2015-01-01 17:34:37 -080056#define BTUSB_SWAVE 0x1000
Marcel Holtmanncda0dd72015-01-26 21:33:48 -080057#define BTUSB_INTEL_NEW 0x2000
Marcel Holtmann893ba542015-01-28 20:27:34 -080058#define BTUSB_AMP 0x4000
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +000059#define BTUSB_QCA_ROME 0x8000
Marcel Holtmann17b27722015-03-22 15:52:38 +010060#define BTUSB_BCM_APPLE 0x10000
Daniel Drakea2698a92015-04-16 14:09:55 -060061#define BTUSB_REALTEK 0x20000
Marcel Holtmann5e23b922007-10-20 14:12:34 +020062
Marcel Holtmann54265202013-10-11 07:46:18 -070063static const struct usb_device_id btusb_table[] = {
Marcel Holtmann5e23b922007-10-20 14:12:34 +020064 /* Generic Bluetooth USB device */
65 { USB_DEVICE_INFO(0xe0, 0x01, 0x01) },
66
Marcel Holtmann893ba542015-01-28 20:27:34 -080067 /* Generic Bluetooth AMP device */
68 { USB_DEVICE_INFO(0xe0, 0x01, 0x04), .driver_info = BTUSB_AMP },
69
Henrik Rydberg1fa65352012-08-25 19:28:06 +020070 /* Apple-specific (Broadcom) devices */
Marcel Holtmann17b27722015-03-22 15:52:38 +010071 { USB_VENDOR_AND_INTERFACE_INFO(0x05ac, 0xff, 0x01, 0x01),
72 .driver_info = BTUSB_BCM_APPLE },
Henrik Rydberg1fa65352012-08-25 19:28:06 +020073
Cho, Yu-Chen178c0592013-06-04 21:40:26 +080074 /* MediaTek MT76x0E */
75 { USB_DEVICE(0x0e8d, 0x763f) },
76
Oliver Neukumc510eae2011-09-21 11:41:45 +020077 /* Broadcom SoftSailing reporting vendor specific */
Don Zickus2e8b50632012-03-28 16:41:11 -040078 { USB_DEVICE(0x0a5c, 0x21e1) },
Oliver Neukumc510eae2011-09-21 11:41:45 +020079
Nobuhiro Iwamatsu3cd01972010-08-20 16:24:07 +090080 /* Apple MacBookPro 7,1 */
81 { USB_DEVICE(0x05ac, 0x8213) },
82
Cyril Lacoux0a79f672010-07-14 10:29:27 +040083 /* Apple iMac11,1 */
84 { USB_DEVICE(0x05ac, 0x8215) },
85
Nobuhiro Iwamatsu9c047152010-08-20 16:24:06 +090086 /* Apple MacBookPro6,2 */
87 { USB_DEVICE(0x05ac, 0x8218) },
88
Edgar (gimli) Hucek3e3ede72010-11-04 08:04:33 +010089 /* Apple MacBookAir3,1, MacBookAir3,2 */
90 { USB_DEVICE(0x05ac, 0x821b) },
91
Pieter-Augustijn Van Malleghema63b7232011-09-07 02:28:10 -040092 /* Apple MacBookAir4,1 */
93 { USB_DEVICE(0x05ac, 0x821f) },
94
Marc-Antoine Perennou88d377b2011-03-24 14:51:21 -030095 /* Apple MacBookPro8,2 */
96 { USB_DEVICE(0x05ac, 0x821a) },
97
Jurgen Kramerf78b68262011-09-04 18:01:42 +020098 /* Apple MacMini5,1 */
99 { USB_DEVICE(0x05ac, 0x8281) },
100
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200101 /* AVM BlueFRITZ! USB v2.0 */
Marcel Holtmann4fcef8e2015-01-01 17:34:37 -0800102 { USB_DEVICE(0x057c, 0x3800), .driver_info = BTUSB_SWAVE },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200103
104 /* Bluetooth Ultraport Module from IBM */
105 { USB_DEVICE(0x04bf, 0x030a) },
106
107 /* ALPS Modules with non-standard id */
108 { USB_DEVICE(0x044e, 0x3001) },
109 { USB_DEVICE(0x044e, 0x3002) },
110
111 /* Ericsson with non-standard id */
112 { USB_DEVICE(0x0bdb, 0x1002) },
113
114 /* Canyon CN-BTU1 with HID interfaces */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100115 { USB_DEVICE(0x0c10, 0x0000) },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200116
Wen-chien Jesse Sungd13431c2011-11-08 14:30:22 +0800117 /* Broadcom BCM20702A0 */
118 { USB_DEVICE(0x413c, 0x8197) },
119
Marcel Holtmannd049f4e2015-01-26 20:35:32 -0800120 /* Broadcom BCM20702B0 (Dynex/Insignia) */
121 { USB_DEVICE(0x19ff, 0x0239), .driver_info = BTUSB_BCM_PATCHRAM },
122
Steven Harms98514032012-04-13 14:45:55 -0400123 /* Foxconn - Hon Hai */
Heinrich Siebmanns6029ddc2014-12-03 19:32:22 +0100124 { USB_VENDOR_AND_INTERFACE_INFO(0x0489, 0xff, 0x01, 0x01),
125 .driver_info = BTUSB_BCM_PATCHRAM },
Steven Harms98514032012-04-13 14:45:55 -0400126
Matej Dubovy8f0c3042015-02-02 18:50:14 +0100127 /* Lite-On Technology - Broadcom based */
128 { USB_VENDOR_AND_INTERFACE_INFO(0x04ca, 0xff, 0x01, 0x01),
129 .driver_info = BTUSB_BCM_PATCHRAM },
130
Andy Shevchenko0b880062014-02-18 18:26:19 +0200131 /* Broadcom devices with vendor specific id */
Petri Gynther10d4c672014-05-08 15:50:01 -0700132 { USB_VENDOR_AND_INTERFACE_INFO(0x0a5c, 0xff, 0x01, 0x01),
133 .driver_info = BTUSB_BCM_PATCHRAM },
Gustavo Padovan92c385f2012-08-06 15:36:49 -0300134
Marcel Holtmannc2aef6e2014-07-21 14:02:33 +0200135 /* ASUSTek Computer - Broadcom based */
Rick Dunn9a5abdaa2015-01-17 05:29:12 +0100136 { USB_VENDOR_AND_INTERFACE_INFO(0x0b05, 0xff, 0x01, 0x01),
137 .driver_info = BTUSB_BCM_PATCHRAM },
Marcel Holtmannc2aef6e2014-07-21 14:02:33 +0200138
Ken O'Brien5bcecf32013-09-21 19:14:43 +0100139 /* Belkin F8065bf - Broadcom based */
Marcel Holtmann6331c682015-03-27 15:11:41 -0700140 { USB_VENDOR_AND_INTERFACE_INFO(0x050d, 0xff, 0x01, 0x01),
141 .driver_info = BTUSB_BCM_PATCHRAM },
Ken O'Brien5bcecf32013-09-21 19:14:43 +0100142
Jurgen Kramer9113bfd2014-02-15 12:01:09 +0100143 /* IMC Networks - Broadcom based */
Marcel Holtmann6331c682015-03-27 15:11:41 -0700144 { USB_VENDOR_AND_INTERFACE_INFO(0x13d3, 0xff, 0x01, 0x01),
145 .driver_info = BTUSB_BCM_PATCHRAM },
Jurgen Kramer9113bfd2014-02-15 12:01:09 +0100146
Marcel Holtmann40df7832014-07-06 13:29:58 +0200147 /* Intel Bluetooth USB Bootloader (RAM module) */
Marcel Holtmannd92f2df2014-07-06 14:53:55 +0200148 { USB_DEVICE(0x8087, 0x0a5a),
149 .driver_info = BTUSB_INTEL_BOOT | BTUSB_BROKEN_ISOC },
Marcel Holtmann40df7832014-07-06 13:29:58 +0200150
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200151 { } /* Terminating entry */
152};
153
154MODULE_DEVICE_TABLE(usb, btusb_table);
155
Marcel Holtmann54265202013-10-11 07:46:18 -0700156static const struct usb_device_id blacklist_table[] = {
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200157 /* CSR BlueCore devices */
158 { USB_DEVICE(0x0a12, 0x0001), .driver_info = BTUSB_CSR },
159
160 /* Broadcom BCM2033 without firmware */
161 { USB_DEVICE(0x0a5c, 0x2033), .driver_info = BTUSB_IGNORE },
162
Bala Shanmugambe931122010-11-26 17:35:46 +0530163 /* Atheros 3011 with sflash firmware */
Andy Shevchenko0b880062014-02-18 18:26:19 +0200164 { USB_DEVICE(0x0489, 0xe027), .driver_info = BTUSB_IGNORE },
165 { USB_DEVICE(0x0489, 0xe03d), .driver_info = BTUSB_IGNORE },
Alexander Ploumistos2eeff0b2015-02-13 21:05:11 +0200166 { USB_DEVICE(0x04f2, 0xaff1), .driver_info = BTUSB_IGNORE },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200167 { USB_DEVICE(0x0930, 0x0215), .driver_info = BTUSB_IGNORE },
Bala Shanmugambe931122010-11-26 17:35:46 +0530168 { USB_DEVICE(0x0cf3, 0x3002), .driver_info = BTUSB_IGNORE },
Marek Vasut6eda5412012-06-08 14:32:50 +0200169 { USB_DEVICE(0x0cf3, 0xe019), .driver_info = BTUSB_IGNORE },
Andy Ross2a7bccc2011-05-09 16:11:16 -0700170 { USB_DEVICE(0x13d3, 0x3304), .driver_info = BTUSB_IGNORE },
Bala Shanmugambe931122010-11-26 17:35:46 +0530171
Cho, Yu-Chen509e7862011-01-26 17:10:59 +0800172 /* Atheros AR9285 Malbec with sflash firmware */
173 { USB_DEVICE(0x03f0, 0x311d), .driver_info = BTUSB_IGNORE },
174
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530175 /* Atheros 3012 with sflash firmware */
Andy Shevchenko0b880062014-02-18 18:26:19 +0200176 { USB_DEVICE(0x0489, 0xe04d), .driver_info = BTUSB_ATH3012 },
177 { USB_DEVICE(0x0489, 0xe04e), .driver_info = BTUSB_ATH3012 },
178 { USB_DEVICE(0x0489, 0xe056), .driver_info = BTUSB_ATH3012 },
179 { USB_DEVICE(0x0489, 0xe057), .driver_info = BTUSB_ATH3012 },
180 { USB_DEVICE(0x0489, 0xe05f), .driver_info = BTUSB_ATH3012 },
Anantha Krishnan4b552bc2014-10-06 16:31:49 +0530181 { USB_DEVICE(0x0489, 0xe078), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200182 { USB_DEVICE(0x04c5, 0x1330), .driver_info = BTUSB_ATH3012 },
183 { USB_DEVICE(0x04ca, 0x3004), .driver_info = BTUSB_ATH3012 },
184 { USB_DEVICE(0x04ca, 0x3005), .driver_info = BTUSB_ATH3012 },
185 { USB_DEVICE(0x04ca, 0x3006), .driver_info = BTUSB_ATH3012 },
Mohammed Habibulla1fb4e092014-04-17 11:37:13 -0700186 { USB_DEVICE(0x04ca, 0x3007), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200187 { USB_DEVICE(0x04ca, 0x3008), .driver_info = BTUSB_ATH3012 },
188 { USB_DEVICE(0x04ca, 0x300b), .driver_info = BTUSB_ATH3012 },
Janne Heikkinen134d3b32014-12-09 07:44:51 +0200189 { USB_DEVICE(0x04ca, 0x3010), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200190 { USB_DEVICE(0x0930, 0x0219), .driver_info = BTUSB_ATH3012 },
191 { USB_DEVICE(0x0930, 0x0220), .driver_info = BTUSB_ATH3012 },
Vincent Zwanenburg89d29752014-08-08 12:33:56 +0100192 { USB_DEVICE(0x0930, 0x0227), .driver_info = BTUSB_ATH3012 },
Andy Shevchenkoa735f9e2014-02-18 18:26:20 +0200193 { USB_DEVICE(0x0b05, 0x17d0), .driver_info = BTUSB_ATH3012 },
Ming Leid66629c2013-03-15 11:00:39 +0800194 { USB_DEVICE(0x0cf3, 0x0036), .driver_info = BTUSB_ATH3012 },
Steven.Li2d25f8b2011-07-01 14:02:36 +0800195 { USB_DEVICE(0x0cf3, 0x3004), .driver_info = BTUSB_ATH3012 },
Sunguk Lee94a32d102013-03-12 04:41:58 +0900196 { USB_DEVICE(0x0cf3, 0x3008), .driver_info = BTUSB_ATH3012 },
Cho, Yu-Chen07c0ea82012-03-14 22:01:21 +0200197 { USB_DEVICE(0x0cf3, 0x311d), .driver_info = BTUSB_ATH3012 },
Oliver Neukumb1312372014-01-16 15:37:11 +0100198 { USB_DEVICE(0x0cf3, 0x311e), .driver_info = BTUSB_ATH3012 },
Oliver Neukum1e56f1e2014-01-16 16:02:58 +0100199 { USB_DEVICE(0x0cf3, 0x311f), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200200 { USB_DEVICE(0x0cf3, 0x3121), .driver_info = BTUSB_ATH3012 },
Ming Leiebaf5792013-03-18 23:45:11 +0800201 { USB_DEVICE(0x0cf3, 0x817a), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200202 { USB_DEVICE(0x0cf3, 0xe003), .driver_info = BTUSB_ATH3012 },
AceLan Kaoac713112012-04-19 14:53:45 +0800203 { USB_DEVICE(0x0cf3, 0xe004), .driver_info = BTUSB_ATH3012 },
Peng Chen0a3658c2013-08-30 17:41:40 +0800204 { USB_DEVICE(0x0cf3, 0xe005), .driver_info = BTUSB_ATH3012 },
Wen-chien Jesse Sungca79f232015-05-13 11:39:24 +0800205 { USB_DEVICE(0x0cf3, 0xe006), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200206 { USB_DEVICE(0x13d3, 0x3362), .driver_info = BTUSB_ATH3012 },
207 { USB_DEVICE(0x13d3, 0x3375), .driver_info = BTUSB_ATH3012 },
AceLan Kaoeed307e2012-12-11 11:41:20 +0800208 { USB_DEVICE(0x13d3, 0x3393), .driver_info = BTUSB_ATH3012 },
Sujith Manoharan5b77a1f2013-07-15 09:29:03 +0530209 { USB_DEVICE(0x13d3, 0x3402), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin3bb30a7c2014-11-25 20:19:52 +0300210 { USB_DEVICE(0x13d3, 0x3408), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin033efa92015-01-18 00:16:51 +0300211 { USB_DEVICE(0x13d3, 0x3423), .driver_info = BTUSB_ATH3012 },
Anantha Krishnanfa2f1392014-07-08 19:25:08 +0530212 { USB_DEVICE(0x13d3, 0x3432), .driver_info = BTUSB_ATH3012 },
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530213
Cho, Yu-Chene9036e32011-02-15 10:20:07 +0800214 /* Atheros AR5BBU12 with sflash firmware */
215 { USB_DEVICE(0x0489, 0xe02c), .driver_info = BTUSB_IGNORE },
216
Michael Gruetzner85d59722012-05-02 22:33:40 +0200217 /* Atheros AR5BBU12 with sflash firmware */
Yevgeniy Melnichukbc21fde2012-08-07 19:48:10 +0530218 { USB_DEVICE(0x0489, 0xe036), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200219 { USB_DEVICE(0x0489, 0xe03c), .driver_info = BTUSB_ATH3012 },
Michael Gruetzner85d59722012-05-02 22:33:40 +0200220
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +0000221 /* QCA ROME chipset */
Wen-chien Jesse Sung20541112015-05-13 11:39:25 +0800222 { USB_DEVICE(0x0cf3, 0xe007), .driver_info = BTUSB_QCA_ROME },
Marcel Holtmannc9e44472015-03-16 23:56:04 -0700223 { USB_DEVICE(0x0cf3, 0xe300), .driver_info = BTUSB_QCA_ROME },
224 { USB_DEVICE(0x0cf3, 0xe360), .driver_info = BTUSB_QCA_ROME },
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +0000225
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200226 /* Broadcom BCM2035 */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100227 { USB_DEVICE(0x0a5c, 0x2009), .driver_info = BTUSB_BCM92035 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200228 { USB_DEVICE(0x0a5c, 0x200a), .driver_info = BTUSB_WRONG_SCO_MTU },
229 { USB_DEVICE(0x0a5c, 0x2035), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200230
231 /* Broadcom BCM2045 */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100232 { USB_DEVICE(0x0a5c, 0x2039), .driver_info = BTUSB_WRONG_SCO_MTU },
233 { USB_DEVICE(0x0a5c, 0x2101), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmannbdbef3d2008-09-23 00:16:35 +0200234
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200235 /* IBM/Lenovo ThinkPad with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100236 { USB_DEVICE(0x0a5c, 0x201e), .driver_info = BTUSB_WRONG_SCO_MTU },
237 { USB_DEVICE(0x0a5c, 0x2110), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200238
239 /* HP laptop with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100240 { USB_DEVICE(0x03f0, 0x171d), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200241
242 /* Dell laptop with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100243 { USB_DEVICE(0x413c, 0x8126), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200244
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100245 /* Dell Wireless 370 and 410 devices */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100246 { USB_DEVICE(0x413c, 0x8152), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100247 { USB_DEVICE(0x413c, 0x8156), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200248
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100249 /* Belkin F8T012 and F8T013 devices */
250 { USB_DEVICE(0x050d, 0x0012), .driver_info = BTUSB_WRONG_SCO_MTU },
251 { USB_DEVICE(0x050d, 0x0013), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200252
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100253 /* Asus WL-BTD202 device */
254 { USB_DEVICE(0x0b05, 0x1715), .driver_info = BTUSB_WRONG_SCO_MTU },
255
256 /* Kensington Bluetooth USB adapter */
257 { USB_DEVICE(0x047d, 0x105e), .driver_info = BTUSB_WRONG_SCO_MTU },
258
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200259 /* RTX Telecom based adapters with buggy SCO support */
260 { USB_DEVICE(0x0400, 0x0807), .driver_info = BTUSB_BROKEN_ISOC },
261 { USB_DEVICE(0x0400, 0x080a), .driver_info = BTUSB_BROKEN_ISOC },
262
263 /* CONWISE Technology based adapters with buggy SCO support */
264 { USB_DEVICE(0x0e5e, 0x6622), .driver_info = BTUSB_BROKEN_ISOC },
265
Marcel Holtmann4fcef8e2015-01-01 17:34:37 -0800266 /* Roper Class 1 Bluetooth Dongle (Silicon Wave based) */
267 { USB_DEVICE(0x1300, 0x0001), .driver_info = BTUSB_SWAVE },
268
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200269 /* Digianswer devices */
270 { USB_DEVICE(0x08fd, 0x0001), .driver_info = BTUSB_DIGIANSWER },
271 { USB_DEVICE(0x08fd, 0x0002), .driver_info = BTUSB_IGNORE },
272
273 /* CSR BlueCore Bluetooth Sniffer */
Marcel Holtmann4f64fa82014-07-07 00:12:04 +0200274 { USB_DEVICE(0x0a12, 0x0002),
275 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200276
277 /* Frontline ComProbe Bluetooth Sniffer */
Marcel Holtmann4f64fa82014-07-07 00:12:04 +0200278 { USB_DEVICE(0x16d3, 0x0002),
279 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200280
Marcel Holtmanncb1ee892015-01-28 19:41:42 -0800281 /* Marvell Bluetooth devices */
282 { USB_DEVICE(0x1286, 0x2044), .driver_info = BTUSB_MARVELL },
283 { USB_DEVICE(0x1286, 0x2046), .driver_info = BTUSB_MARVELL },
284
Marcel Holtmannd0ac9eb2015-01-28 19:41:43 -0800285 /* Intel Bluetooth devices */
Marcel Holtmann407550f2015-02-22 15:41:18 -0800286 { USB_DEVICE(0x8087, 0x07da), .driver_info = BTUSB_CSR },
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -0700287 { USB_DEVICE(0x8087, 0x07dc), .driver_info = BTUSB_INTEL },
Tedd Ho-Jeong Anef4e5e42013-11-12 13:10:58 -0800288 { USB_DEVICE(0x8087, 0x0a2a), .driver_info = BTUSB_INTEL },
Marcel Holtmanncda0dd72015-01-26 21:33:48 -0800289 { USB_DEVICE(0x8087, 0x0a2b), .driver_info = BTUSB_INTEL_NEW },
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -0700290
Marcel Holtmannd0ac9eb2015-01-28 19:41:43 -0800291 /* Other Intel Bluetooth devices */
292 { USB_VENDOR_AND_INTERFACE_INFO(0x8087, 0xe0, 0x01, 0x01),
293 .driver_info = BTUSB_IGNORE },
Amitkumar Karwarae8df492014-07-18 14:47:06 -0700294
Daniel Drakea2698a92015-04-16 14:09:55 -0600295 /* Realtek Bluetooth devices */
296 { USB_VENDOR_AND_INTERFACE_INFO(0x0bda, 0xe0, 0x01, 0x01),
297 .driver_info = BTUSB_REALTEK },
298
299 /* Additional Realtek 8723AE Bluetooth devices */
300 { USB_DEVICE(0x0930, 0x021d), .driver_info = BTUSB_REALTEK },
301 { USB_DEVICE(0x13d3, 0x3394), .driver_info = BTUSB_REALTEK },
302
303 /* Additional Realtek 8723BE Bluetooth devices */
304 { USB_DEVICE(0x0489, 0xe085), .driver_info = BTUSB_REALTEK },
305 { USB_DEVICE(0x0489, 0xe08b), .driver_info = BTUSB_REALTEK },
306 { USB_DEVICE(0x13d3, 0x3410), .driver_info = BTUSB_REALTEK },
307 { USB_DEVICE(0x13d3, 0x3416), .driver_info = BTUSB_REALTEK },
308 { USB_DEVICE(0x13d3, 0x3459), .driver_info = BTUSB_REALTEK },
309
310 /* Additional Realtek 8821AE Bluetooth devices */
311 { USB_DEVICE(0x0b05, 0x17dc), .driver_info = BTUSB_REALTEK },
312 { USB_DEVICE(0x13d3, 0x3414), .driver_info = BTUSB_REALTEK },
313 { USB_DEVICE(0x13d3, 0x3458), .driver_info = BTUSB_REALTEK },
314 { USB_DEVICE(0x13d3, 0x3461), .driver_info = BTUSB_REALTEK },
315 { USB_DEVICE(0x13d3, 0x3462), .driver_info = BTUSB_REALTEK },
316
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200317 { } /* Terminating entry */
318};
319
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200320#define BTUSB_MAX_ISOC_FRAMES 10
321
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200322#define BTUSB_INTR_RUNNING 0
323#define BTUSB_BULK_RUNNING 1
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200324#define BTUSB_ISOC_RUNNING 2
Oliver Neukum7bee5492009-08-24 23:44:59 +0200325#define BTUSB_SUSPENDING 3
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -0300326#define BTUSB_DID_ISO_RESUME 4
Marcel Holtmanncda0dd72015-01-26 21:33:48 -0800327#define BTUSB_BOOTLOADER 5
328#define BTUSB_DOWNLOADING 6
Marcel Holtmannce6bb922015-01-28 01:58:40 -0800329#define BTUSB_FIRMWARE_LOADED 7
Marcel Holtmanncda0dd72015-01-26 21:33:48 -0800330#define BTUSB_FIRMWARE_FAILED 8
Marcel Holtmannce6bb922015-01-28 01:58:40 -0800331#define BTUSB_BOOTING 9
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200332
333struct btusb_data {
334 struct hci_dev *hdev;
335 struct usb_device *udev;
Marcel Holtmann5fbcd262008-09-23 00:16:36 +0200336 struct usb_interface *intf;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200337 struct usb_interface *isoc;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200338
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200339 unsigned long flags;
340
341 struct work_struct work;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200342 struct work_struct waker;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200343
Marcel Holtmann803b5832014-09-16 08:00:29 +0200344 struct usb_anchor deferred;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200345 struct usb_anchor tx_anchor;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200346 int tx_in_flight;
347 spinlock_t txlock;
348
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200349 struct usb_anchor intr_anchor;
350 struct usb_anchor bulk_anchor;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200351 struct usb_anchor isoc_anchor;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200352 spinlock_t rxlock;
353
354 struct sk_buff *evt_skb;
355 struct sk_buff *acl_skb;
356 struct sk_buff *sco_skb;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200357
358 struct usb_endpoint_descriptor *intr_ep;
359 struct usb_endpoint_descriptor *bulk_tx_ep;
360 struct usb_endpoint_descriptor *bulk_rx_ep;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200361 struct usb_endpoint_descriptor *isoc_tx_ep;
362 struct usb_endpoint_descriptor *isoc_rx_ep;
363
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100364 __u8 cmdreq_type;
Marcel Holtmann893ba542015-01-28 20:27:34 -0800365 __u8 cmdreq;
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100366
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100367 unsigned int sco_num;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200368 int isoc_altsetting;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +0100369 int suspend_count;
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +0100370
Marcel Holtmann97307f52015-01-12 13:51:10 -0800371 int (*recv_event)(struct hci_dev *hdev, struct sk_buff *skb);
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +0100372 int (*recv_bulk)(struct btusb_data *data, void *buffer, int count);
Kim, Ben Young Taeace31982015-02-15 23:06:14 +0000373
374 int (*setup_on_usb)(struct hci_dev *hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200375};
376
Marcel Holtmann803b5832014-09-16 08:00:29 +0200377static inline void btusb_free_frags(struct btusb_data *data)
378{
379 unsigned long flags;
380
381 spin_lock_irqsave(&data->rxlock, flags);
382
383 kfree_skb(data->evt_skb);
384 data->evt_skb = NULL;
385
386 kfree_skb(data->acl_skb);
387 data->acl_skb = NULL;
388
389 kfree_skb(data->sco_skb);
390 data->sco_skb = NULL;
391
392 spin_unlock_irqrestore(&data->rxlock, flags);
393}
394
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200395static int btusb_recv_intr(struct btusb_data *data, void *buffer, int count)
396{
Marcel Holtmann803b5832014-09-16 08:00:29 +0200397 struct sk_buff *skb;
398 int err = 0;
399
400 spin_lock(&data->rxlock);
401 skb = data->evt_skb;
402
403 while (count) {
404 int len;
405
406 if (!skb) {
407 skb = bt_skb_alloc(HCI_MAX_EVENT_SIZE, GFP_ATOMIC);
408 if (!skb) {
409 err = -ENOMEM;
410 break;
411 }
412
413 bt_cb(skb)->pkt_type = HCI_EVENT_PKT;
414 bt_cb(skb)->expect = HCI_EVENT_HDR_SIZE;
415 }
416
417 len = min_t(uint, bt_cb(skb)->expect, count);
418 memcpy(skb_put(skb, len), buffer, len);
419
420 count -= len;
421 buffer += len;
422 bt_cb(skb)->expect -= len;
423
424 if (skb->len == HCI_EVENT_HDR_SIZE) {
425 /* Complete event header */
426 bt_cb(skb)->expect = hci_event_hdr(skb)->plen;
427
428 if (skb_tailroom(skb) < bt_cb(skb)->expect) {
429 kfree_skb(skb);
430 skb = NULL;
431
432 err = -EILSEQ;
433 break;
434 }
435 }
436
437 if (bt_cb(skb)->expect == 0) {
438 /* Complete frame */
Marcel Holtmann97307f52015-01-12 13:51:10 -0800439 data->recv_event(data->hdev, skb);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200440 skb = NULL;
441 }
442 }
443
444 data->evt_skb = skb;
445 spin_unlock(&data->rxlock);
446
447 return err;
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200448}
449
450static int btusb_recv_bulk(struct btusb_data *data, void *buffer, int count)
451{
Marcel Holtmann803b5832014-09-16 08:00:29 +0200452 struct sk_buff *skb;
453 int err = 0;
454
455 spin_lock(&data->rxlock);
456 skb = data->acl_skb;
457
458 while (count) {
459 int len;
460
461 if (!skb) {
462 skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
463 if (!skb) {
464 err = -ENOMEM;
465 break;
466 }
467
468 bt_cb(skb)->pkt_type = HCI_ACLDATA_PKT;
469 bt_cb(skb)->expect = HCI_ACL_HDR_SIZE;
470 }
471
472 len = min_t(uint, bt_cb(skb)->expect, count);
473 memcpy(skb_put(skb, len), buffer, len);
474
475 count -= len;
476 buffer += len;
477 bt_cb(skb)->expect -= len;
478
479 if (skb->len == HCI_ACL_HDR_SIZE) {
480 __le16 dlen = hci_acl_hdr(skb)->dlen;
481
482 /* Complete ACL header */
483 bt_cb(skb)->expect = __le16_to_cpu(dlen);
484
485 if (skb_tailroom(skb) < bt_cb(skb)->expect) {
486 kfree_skb(skb);
487 skb = NULL;
488
489 err = -EILSEQ;
490 break;
491 }
492 }
493
494 if (bt_cb(skb)->expect == 0) {
495 /* Complete frame */
496 hci_recv_frame(data->hdev, skb);
497 skb = NULL;
498 }
499 }
500
501 data->acl_skb = skb;
502 spin_unlock(&data->rxlock);
503
504 return err;
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200505}
506
507static int btusb_recv_isoc(struct btusb_data *data, void *buffer, int count)
508{
Marcel Holtmann803b5832014-09-16 08:00:29 +0200509 struct sk_buff *skb;
510 int err = 0;
511
512 spin_lock(&data->rxlock);
513 skb = data->sco_skb;
514
515 while (count) {
516 int len;
517
518 if (!skb) {
519 skb = bt_skb_alloc(HCI_MAX_SCO_SIZE, GFP_ATOMIC);
520 if (!skb) {
521 err = -ENOMEM;
522 break;
523 }
524
525 bt_cb(skb)->pkt_type = HCI_SCODATA_PKT;
526 bt_cb(skb)->expect = HCI_SCO_HDR_SIZE;
527 }
528
529 len = min_t(uint, bt_cb(skb)->expect, count);
530 memcpy(skb_put(skb, len), buffer, len);
531
532 count -= len;
533 buffer += len;
534 bt_cb(skb)->expect -= len;
535
536 if (skb->len == HCI_SCO_HDR_SIZE) {
537 /* Complete SCO header */
538 bt_cb(skb)->expect = hci_sco_hdr(skb)->dlen;
539
540 if (skb_tailroom(skb) < bt_cb(skb)->expect) {
541 kfree_skb(skb);
542 skb = NULL;
543
544 err = -EILSEQ;
545 break;
546 }
547 }
548
549 if (bt_cb(skb)->expect == 0) {
550 /* Complete frame */
551 hci_recv_frame(data->hdev, skb);
552 skb = NULL;
553 }
554 }
555
556 data->sco_skb = skb;
557 spin_unlock(&data->rxlock);
558
559 return err;
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200560}
561
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200562static void btusb_intr_complete(struct urb *urb)
563{
564 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +0100565 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200566 int err;
567
Marcel Holtmann89e75332014-09-16 04:44:50 +0200568 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
569 urb->actual_length);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200570
571 if (!test_bit(HCI_RUNNING, &hdev->flags))
572 return;
573
574 if (urb->status == 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200575 hdev->stat.byte_rx += urb->actual_length;
576
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200577 if (btusb_recv_intr(data, urb->transfer_buffer,
578 urb->actual_length) < 0) {
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200579 BT_ERR("%s corrupted event packet", hdev->name);
580 hdev->stat.err_rx++;
581 }
Champion Chen85560c42014-09-06 14:06:08 -0500582 } else if (urb->status == -ENOENT) {
583 /* Avoid suspend failed when usb_kill_urb */
584 return;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200585 }
586
587 if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
588 return;
589
Oliver Neukum7bee5492009-08-24 23:44:59 +0200590 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200591 usb_anchor_urb(urb, &data->intr_anchor);
592
593 err = usb_submit_urb(urb, GFP_ATOMIC);
594 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200595 /* -EPERM: urb is being killed;
596 * -ENODEV: device got disconnected */
597 if (err != -EPERM && err != -ENODEV)
Stefan Seyfried61faddf2010-11-30 21:49:08 +0100598 BT_ERR("%s urb %p failed to resubmit (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200599 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200600 usb_unanchor_urb(urb);
601 }
602}
603
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100604static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200605{
David Herrmann155961e2012-02-09 21:58:32 +0100606 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200607 struct urb *urb;
608 unsigned char *buf;
609 unsigned int pipe;
610 int err, size;
611
612 BT_DBG("%s", hdev->name);
613
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200614 if (!data->intr_ep)
615 return -ENODEV;
616
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100617 urb = usb_alloc_urb(0, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200618 if (!urb)
619 return -ENOMEM;
620
621 size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
622
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100623 buf = kmalloc(size, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200624 if (!buf) {
625 usb_free_urb(urb);
626 return -ENOMEM;
627 }
628
629 pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
630
631 usb_fill_int_urb(urb, data->udev, pipe, buf, size,
Marcel Holtmann89e75332014-09-16 04:44:50 +0200632 btusb_intr_complete, hdev, data->intr_ep->bInterval);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200633
634 urb->transfer_flags |= URB_FREE_BUFFER;
635
636 usb_anchor_urb(urb, &data->intr_anchor);
637
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100638 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200639 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +0200640 if (err != -EPERM && err != -ENODEV)
641 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200642 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200643 usb_unanchor_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200644 }
645
646 usb_free_urb(urb);
647
648 return err;
649}
650
651static void btusb_bulk_complete(struct urb *urb)
652{
653 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +0100654 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200655 int err;
656
Marcel Holtmann89e75332014-09-16 04:44:50 +0200657 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
658 urb->actual_length);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200659
660 if (!test_bit(HCI_RUNNING, &hdev->flags))
661 return;
662
663 if (urb->status == 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200664 hdev->stat.byte_rx += urb->actual_length;
665
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +0100666 if (data->recv_bulk(data, urb->transfer_buffer,
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200667 urb->actual_length) < 0) {
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200668 BT_ERR("%s corrupted ACL packet", hdev->name);
669 hdev->stat.err_rx++;
670 }
Champion Chen85560c42014-09-06 14:06:08 -0500671 } else if (urb->status == -ENOENT) {
672 /* Avoid suspend failed when usb_kill_urb */
673 return;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200674 }
675
676 if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
677 return;
678
679 usb_anchor_urb(urb, &data->bulk_anchor);
Oliver Neukum652fd782009-12-16 19:23:43 +0100680 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200681
682 err = usb_submit_urb(urb, GFP_ATOMIC);
683 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200684 /* -EPERM: urb is being killed;
685 * -ENODEV: device got disconnected */
686 if (err != -EPERM && err != -ENODEV)
Stefan Seyfried61faddf2010-11-30 21:49:08 +0100687 BT_ERR("%s urb %p failed to resubmit (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200688 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200689 usb_unanchor_urb(urb);
690 }
691}
692
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100693static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200694{
David Herrmann155961e2012-02-09 21:58:32 +0100695 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200696 struct urb *urb;
697 unsigned char *buf;
698 unsigned int pipe;
Vikram Kandukuri290ba202009-07-02 14:31:59 +0530699 int err, size = HCI_MAX_FRAME_SIZE;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200700
701 BT_DBG("%s", hdev->name);
702
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200703 if (!data->bulk_rx_ep)
704 return -ENODEV;
705
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100706 urb = usb_alloc_urb(0, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200707 if (!urb)
708 return -ENOMEM;
709
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100710 buf = kmalloc(size, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200711 if (!buf) {
712 usb_free_urb(urb);
713 return -ENOMEM;
714 }
715
716 pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
717
Marcel Holtmann89e75332014-09-16 04:44:50 +0200718 usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
719 btusb_bulk_complete, hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200720
721 urb->transfer_flags |= URB_FREE_BUFFER;
722
Oliver Neukum7bee5492009-08-24 23:44:59 +0200723 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200724 usb_anchor_urb(urb, &data->bulk_anchor);
725
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100726 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200727 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +0200728 if (err != -EPERM && err != -ENODEV)
729 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200730 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200731 usb_unanchor_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200732 }
733
734 usb_free_urb(urb);
735
736 return err;
737}
738
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200739static void btusb_isoc_complete(struct urb *urb)
740{
741 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +0100742 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200743 int i, err;
744
Marcel Holtmann89e75332014-09-16 04:44:50 +0200745 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
746 urb->actual_length);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200747
748 if (!test_bit(HCI_RUNNING, &hdev->flags))
749 return;
750
751 if (urb->status == 0) {
752 for (i = 0; i < urb->number_of_packets; i++) {
753 unsigned int offset = urb->iso_frame_desc[i].offset;
754 unsigned int length = urb->iso_frame_desc[i].actual_length;
755
756 if (urb->iso_frame_desc[i].status)
757 continue;
758
759 hdev->stat.byte_rx += length;
760
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200761 if (btusb_recv_isoc(data, urb->transfer_buffer + offset,
762 length) < 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200763 BT_ERR("%s corrupted SCO packet", hdev->name);
764 hdev->stat.err_rx++;
765 }
766 }
Champion Chen85560c42014-09-06 14:06:08 -0500767 } else if (urb->status == -ENOENT) {
768 /* Avoid suspend failed when usb_kill_urb */
769 return;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200770 }
771
772 if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags))
773 return;
774
775 usb_anchor_urb(urb, &data->isoc_anchor);
776
777 err = usb_submit_urb(urb, GFP_ATOMIC);
778 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200779 /* -EPERM: urb is being killed;
780 * -ENODEV: device got disconnected */
781 if (err != -EPERM && err != -ENODEV)
Stefan Seyfried61faddf2010-11-30 21:49:08 +0100782 BT_ERR("%s urb %p failed to resubmit (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200783 hdev->name, urb, -err);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200784 usb_unanchor_urb(urb);
785 }
786}
787
Jesper Juhl42b16b32011-01-17 00:09:38 +0100788static inline void __fill_isoc_descriptor(struct urb *urb, int len, int mtu)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200789{
790 int i, offset = 0;
791
792 BT_DBG("len %d mtu %d", len, mtu);
793
794 for (i = 0; i < BTUSB_MAX_ISOC_FRAMES && len >= mtu;
795 i++, offset += mtu, len -= mtu) {
796 urb->iso_frame_desc[i].offset = offset;
797 urb->iso_frame_desc[i].length = mtu;
798 }
799
800 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
801 urb->iso_frame_desc[i].offset = offset;
802 urb->iso_frame_desc[i].length = len;
803 i++;
804 }
805
806 urb->number_of_packets = i;
807}
808
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100809static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200810{
David Herrmann155961e2012-02-09 21:58:32 +0100811 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200812 struct urb *urb;
813 unsigned char *buf;
814 unsigned int pipe;
815 int err, size;
816
817 BT_DBG("%s", hdev->name);
818
819 if (!data->isoc_rx_ep)
820 return -ENODEV;
821
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100822 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200823 if (!urb)
824 return -ENOMEM;
825
826 size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) *
827 BTUSB_MAX_ISOC_FRAMES;
828
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100829 buf = kmalloc(size, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200830 if (!buf) {
831 usb_free_urb(urb);
832 return -ENOMEM;
833 }
834
835 pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress);
836
Bing Zhaofa0fb932011-12-20 18:19:00 -0800837 usb_fill_int_urb(urb, data->udev, pipe, buf, size, btusb_isoc_complete,
Marcel Holtmann89e75332014-09-16 04:44:50 +0200838 hdev, data->isoc_rx_ep->bInterval);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200839
Marcel Holtmann89e75332014-09-16 04:44:50 +0200840 urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200841
842 __fill_isoc_descriptor(urb, size,
Marcel Holtmann89e75332014-09-16 04:44:50 +0200843 le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize));
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200844
845 usb_anchor_urb(urb, &data->isoc_anchor);
846
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100847 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200848 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +0200849 if (err != -EPERM && err != -ENODEV)
850 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200851 hdev->name, urb, -err);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200852 usb_unanchor_urb(urb);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200853 }
854
855 usb_free_urb(urb);
856
857 return err;
858}
859
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200860static void btusb_tx_complete(struct urb *urb)
861{
862 struct sk_buff *skb = urb->context;
Marcel Holtmann89e75332014-09-16 04:44:50 +0200863 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
David Herrmann155961e2012-02-09 21:58:32 +0100864 struct btusb_data *data = hci_get_drvdata(hdev);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200865
Marcel Holtmann89e75332014-09-16 04:44:50 +0200866 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
867 urb->actual_length);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200868
869 if (!test_bit(HCI_RUNNING, &hdev->flags))
870 goto done;
871
872 if (!urb->status)
873 hdev->stat.byte_tx += urb->transfer_buffer_length;
874 else
875 hdev->stat.err_tx++;
876
877done:
878 spin_lock(&data->txlock);
879 data->tx_in_flight--;
880 spin_unlock(&data->txlock);
881
882 kfree(urb->setup_packet);
883
884 kfree_skb(skb);
885}
886
887static void btusb_isoc_tx_complete(struct urb *urb)
888{
889 struct sk_buff *skb = urb->context;
Marcel Holtmann89e75332014-09-16 04:44:50 +0200890 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200891
Marcel Holtmann89e75332014-09-16 04:44:50 +0200892 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
893 urb->actual_length);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200894
895 if (!test_bit(HCI_RUNNING, &hdev->flags))
896 goto done;
897
898 if (!urb->status)
899 hdev->stat.byte_tx += urb->transfer_buffer_length;
900 else
901 hdev->stat.err_tx++;
902
903done:
904 kfree(urb->setup_packet);
905
906 kfree_skb(skb);
907}
908
909static int btusb_open(struct hci_dev *hdev)
910{
David Herrmann155961e2012-02-09 21:58:32 +0100911 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200912 int err;
913
914 BT_DBG("%s", hdev->name);
915
Kim, Ben Young Taeace31982015-02-15 23:06:14 +0000916 /* Patching USB firmware files prior to starting any URBs of HCI path
917 * It is more safe to use USB bulk channel for downloading USB patch
918 */
919 if (data->setup_on_usb) {
920 err = data->setup_on_usb(hdev);
Marcel Holtmanneb500422015-04-16 23:15:50 +0200921 if (err < 0)
Kim, Ben Young Taeace31982015-02-15 23:06:14 +0000922 return err;
923 }
924
Oliver Neukum7bee5492009-08-24 23:44:59 +0200925 err = usb_autopm_get_interface(data->intf);
926 if (err < 0)
927 return err;
928
929 data->intf->needs_remote_wakeup = 1;
930
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200931 if (test_and_set_bit(HCI_RUNNING, &hdev->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +0200932 goto done;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200933
934 if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +0200935 goto done;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200936
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100937 err = btusb_submit_intr_urb(hdev, GFP_KERNEL);
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100938 if (err < 0)
939 goto failed;
940
941 err = btusb_submit_bulk_urb(hdev, GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200942 if (err < 0) {
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100943 usb_kill_anchored_urbs(&data->intr_anchor);
944 goto failed;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200945 }
946
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100947 set_bit(BTUSB_BULK_RUNNING, &data->flags);
948 btusb_submit_bulk_urb(hdev, GFP_KERNEL);
949
Oliver Neukum7bee5492009-08-24 23:44:59 +0200950done:
951 usb_autopm_put_interface(data->intf);
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100952 return 0;
953
954failed:
955 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
956 clear_bit(HCI_RUNNING, &hdev->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200957 usb_autopm_put_interface(data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200958 return err;
959}
960
Oliver Neukum7bee5492009-08-24 23:44:59 +0200961static void btusb_stop_traffic(struct btusb_data *data)
962{
963 usb_kill_anchored_urbs(&data->intr_anchor);
964 usb_kill_anchored_urbs(&data->bulk_anchor);
965 usb_kill_anchored_urbs(&data->isoc_anchor);
966}
967
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200968static int btusb_close(struct hci_dev *hdev)
969{
David Herrmann155961e2012-02-09 21:58:32 +0100970 struct btusb_data *data = hci_get_drvdata(hdev);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200971 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200972
973 BT_DBG("%s", hdev->name);
974
975 if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
976 return 0;
977
Marcel Holtmanne8c3c3d2008-09-23 00:16:36 +0200978 cancel_work_sync(&data->work);
Linus Torvalds404291a2009-11-11 13:32:29 -0800979 cancel_work_sync(&data->waker);
Marcel Holtmanne8c3c3d2008-09-23 00:16:36 +0200980
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200981 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200982 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200983 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200984
985 btusb_stop_traffic(data);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200986 btusb_free_frags(data);
987
Oliver Neukum7bee5492009-08-24 23:44:59 +0200988 err = usb_autopm_get_interface(data->intf);
989 if (err < 0)
Oliver Neukum7b8e2c12009-11-13 14:26:23 +0100990 goto failed;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200991
992 data->intf->needs_remote_wakeup = 0;
993 usb_autopm_put_interface(data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200994
Oliver Neukum7b8e2c12009-11-13 14:26:23 +0100995failed:
996 usb_scuttle_anchored_urbs(&data->deferred);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200997 return 0;
998}
999
1000static int btusb_flush(struct hci_dev *hdev)
1001{
David Herrmann155961e2012-02-09 21:58:32 +01001002 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001003
1004 BT_DBG("%s", hdev->name);
1005
1006 usb_kill_anchored_urbs(&data->tx_anchor);
Marcel Holtmann803b5832014-09-16 08:00:29 +02001007 btusb_free_frags(data);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001008
1009 return 0;
1010}
1011
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001012static struct urb *alloc_ctrl_urb(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001013{
David Herrmann155961e2012-02-09 21:58:32 +01001014 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001015 struct usb_ctrlrequest *dr;
1016 struct urb *urb;
1017 unsigned int pipe;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001018
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001019 urb = usb_alloc_urb(0, GFP_KERNEL);
1020 if (!urb)
1021 return ERR_PTR(-ENOMEM);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001022
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001023 dr = kmalloc(sizeof(*dr), GFP_KERNEL);
1024 if (!dr) {
1025 usb_free_urb(urb);
1026 return ERR_PTR(-ENOMEM);
1027 }
1028
1029 dr->bRequestType = data->cmdreq_type;
Marcel Holtmann893ba542015-01-28 20:27:34 -08001030 dr->bRequest = data->cmdreq;
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001031 dr->wIndex = 0;
1032 dr->wValue = 0;
1033 dr->wLength = __cpu_to_le16(skb->len);
1034
1035 pipe = usb_sndctrlpipe(data->udev, 0x00);
1036
Marcel Holtmann89e75332014-09-16 04:44:50 +02001037 usb_fill_control_urb(urb, data->udev, pipe, (void *)dr,
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001038 skb->data, skb->len, btusb_tx_complete, skb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001039
Marcel Holtmann89e75332014-09-16 04:44:50 +02001040 skb->dev = (void *)hdev;
Marcel Holtmann7bd8f092013-10-11 06:19:18 -07001041
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001042 return urb;
1043}
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001044
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001045static struct urb *alloc_bulk_urb(struct hci_dev *hdev, struct sk_buff *skb)
1046{
1047 struct btusb_data *data = hci_get_drvdata(hdev);
1048 struct urb *urb;
1049 unsigned int pipe;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001050
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001051 if (!data->bulk_tx_ep)
1052 return ERR_PTR(-ENODEV);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001053
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001054 urb = usb_alloc_urb(0, GFP_KERNEL);
1055 if (!urb)
1056 return ERR_PTR(-ENOMEM);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001057
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001058 pipe = usb_sndbulkpipe(data->udev, data->bulk_tx_ep->bEndpointAddress);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001059
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001060 usb_fill_bulk_urb(urb, data->udev, pipe,
1061 skb->data, skb->len, btusb_tx_complete, skb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001062
Marcel Holtmann89e75332014-09-16 04:44:50 +02001063 skb->dev = (void *)hdev;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001064
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001065 return urb;
1066}
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001067
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001068static struct urb *alloc_isoc_urb(struct hci_dev *hdev, struct sk_buff *skb)
1069{
1070 struct btusb_data *data = hci_get_drvdata(hdev);
1071 struct urb *urb;
1072 unsigned int pipe;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001073
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001074 if (!data->isoc_tx_ep)
1075 return ERR_PTR(-ENODEV);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001076
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001077 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_KERNEL);
1078 if (!urb)
1079 return ERR_PTR(-ENOMEM);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001080
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001081 pipe = usb_sndisocpipe(data->udev, data->isoc_tx_ep->bEndpointAddress);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001082
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001083 usb_fill_int_urb(urb, data->udev, pipe,
1084 skb->data, skb->len, btusb_isoc_tx_complete,
1085 skb, data->isoc_tx_ep->bInterval);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001086
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001087 urb->transfer_flags = URB_ISO_ASAP;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001088
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001089 __fill_isoc_descriptor(urb, skb->len,
1090 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001091
Marcel Holtmann89e75332014-09-16 04:44:50 +02001092 skb->dev = (void *)hdev;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001093
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001094 return urb;
1095}
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001096
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001097static int submit_tx_urb(struct hci_dev *hdev, struct urb *urb)
1098{
1099 struct btusb_data *data = hci_get_drvdata(hdev);
1100 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001101
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001102 usb_anchor_urb(urb, &data->tx_anchor);
1103
Johan Hedberge9753ef2014-09-14 08:49:34 +03001104 err = usb_submit_urb(urb, GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001105 if (err < 0) {
Paul Bolle5a9b80e2011-10-09 12:12:16 +02001106 if (err != -EPERM && err != -ENODEV)
1107 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +02001108 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001109 kfree(urb->setup_packet);
1110 usb_unanchor_urb(urb);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001111 } else {
1112 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001113 }
1114
Cong Wang54a8a792011-11-22 09:32:57 +08001115 usb_free_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001116 return err;
1117}
1118
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001119static int submit_or_queue_tx_urb(struct hci_dev *hdev, struct urb *urb)
1120{
1121 struct btusb_data *data = hci_get_drvdata(hdev);
1122 unsigned long flags;
1123 bool suspending;
1124
1125 spin_lock_irqsave(&data->txlock, flags);
1126 suspending = test_bit(BTUSB_SUSPENDING, &data->flags);
1127 if (!suspending)
1128 data->tx_in_flight++;
1129 spin_unlock_irqrestore(&data->txlock, flags);
1130
1131 if (!suspending)
1132 return submit_tx_urb(hdev, urb);
1133
1134 usb_anchor_urb(urb, &data->deferred);
1135 schedule_work(&data->waker);
1136
1137 usb_free_urb(urb);
1138 return 0;
1139}
1140
1141static int btusb_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
1142{
1143 struct urb *urb;
1144
1145 BT_DBG("%s", hdev->name);
1146
1147 if (!test_bit(HCI_RUNNING, &hdev->flags))
1148 return -EBUSY;
1149
1150 switch (bt_cb(skb)->pkt_type) {
1151 case HCI_COMMAND_PKT:
1152 urb = alloc_ctrl_urb(hdev, skb);
1153 if (IS_ERR(urb))
1154 return PTR_ERR(urb);
1155
1156 hdev->stat.cmd_tx++;
1157 return submit_or_queue_tx_urb(hdev, urb);
1158
1159 case HCI_ACLDATA_PKT:
1160 urb = alloc_bulk_urb(hdev, skb);
1161 if (IS_ERR(urb))
1162 return PTR_ERR(urb);
1163
1164 hdev->stat.acl_tx++;
1165 return submit_or_queue_tx_urb(hdev, urb);
1166
1167 case HCI_SCODATA_PKT:
1168 if (hci_conn_num(hdev, SCO_LINK) < 1)
1169 return -ENODEV;
1170
1171 urb = alloc_isoc_urb(hdev, skb);
1172 if (IS_ERR(urb))
1173 return PTR_ERR(urb);
1174
1175 hdev->stat.sco_tx++;
1176 return submit_tx_urb(hdev, urb);
1177 }
1178
1179 return -EILSEQ;
1180}
1181
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001182static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
1183{
David Herrmann155961e2012-02-09 21:58:32 +01001184 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001185
1186 BT_DBG("%s evt %d", hdev->name, evt);
1187
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001188 if (hci_conn_num(hdev, SCO_LINK) != data->sco_num) {
1189 data->sco_num = hci_conn_num(hdev, SCO_LINK);
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001190 schedule_work(&data->work);
Marcel Holtmanna780efa2008-11-30 12:17:12 +01001191 }
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001192}
1193
Jesper Juhl42b16b32011-01-17 00:09:38 +01001194static inline int __set_isoc_interface(struct hci_dev *hdev, int altsetting)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001195{
David Herrmann155961e2012-02-09 21:58:32 +01001196 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001197 struct usb_interface *intf = data->isoc;
1198 struct usb_endpoint_descriptor *ep_desc;
1199 int i, err;
1200
1201 if (!data->isoc)
1202 return -ENODEV;
1203
1204 err = usb_set_interface(data->udev, 1, altsetting);
1205 if (err < 0) {
1206 BT_ERR("%s setting interface failed (%d)", hdev->name, -err);
1207 return err;
1208 }
1209
1210 data->isoc_altsetting = altsetting;
1211
1212 data->isoc_tx_ep = NULL;
1213 data->isoc_rx_ep = NULL;
1214
1215 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
1216 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
1217
1218 if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) {
1219 data->isoc_tx_ep = ep_desc;
1220 continue;
1221 }
1222
1223 if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) {
1224 data->isoc_rx_ep = ep_desc;
1225 continue;
1226 }
1227 }
1228
1229 if (!data->isoc_tx_ep || !data->isoc_rx_ep) {
1230 BT_ERR("%s invalid SCO descriptors", hdev->name);
1231 return -ENODEV;
1232 }
1233
1234 return 0;
1235}
1236
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001237static void btusb_work(struct work_struct *work)
1238{
1239 struct btusb_data *data = container_of(work, struct btusb_data, work);
1240 struct hci_dev *hdev = data->hdev;
Mikel Astizf4001d22012-04-11 08:48:51 +02001241 int new_alts;
Oliver Neukum7bee5492009-08-24 23:44:59 +02001242 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001243
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001244 if (data->sco_num > 0) {
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -03001245 if (!test_bit(BTUSB_DID_ISO_RESUME, &data->flags)) {
Oliver Neukum8efdd0c2011-02-11 13:00:06 +01001246 err = usb_autopm_get_interface(data->isoc ? data->isoc : data->intf);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001247 if (err < 0) {
1248 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1249 usb_kill_anchored_urbs(&data->isoc_anchor);
1250 return;
1251 }
1252
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -03001253 set_bit(BTUSB_DID_ISO_RESUME, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001254 }
Mikel Astizf4001d22012-04-11 08:48:51 +02001255
1256 if (hdev->voice_setting & 0x0020) {
1257 static const int alts[3] = { 2, 4, 5 };
Marcel Holtmann89e75332014-09-16 04:44:50 +02001258
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001259 new_alts = alts[data->sco_num - 1];
Mikel Astizf4001d22012-04-11 08:48:51 +02001260 } else {
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001261 new_alts = data->sco_num;
Mikel Astizf4001d22012-04-11 08:48:51 +02001262 }
1263
1264 if (data->isoc_altsetting != new_alts) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001265 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1266 usb_kill_anchored_urbs(&data->isoc_anchor);
1267
Mikel Astizf4001d22012-04-11 08:48:51 +02001268 if (__set_isoc_interface(hdev, new_alts) < 0)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001269 return;
1270 }
1271
1272 if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001273 if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001274 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1275 else
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001276 btusb_submit_isoc_urb(hdev, GFP_KERNEL);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001277 }
1278 } else {
1279 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1280 usb_kill_anchored_urbs(&data->isoc_anchor);
1281
1282 __set_isoc_interface(hdev, 0);
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -03001283 if (test_and_clear_bit(BTUSB_DID_ISO_RESUME, &data->flags))
Oliver Neukum8efdd0c2011-02-11 13:00:06 +01001284 usb_autopm_put_interface(data->isoc ? data->isoc : data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001285 }
1286}
1287
Oliver Neukum7bee5492009-08-24 23:44:59 +02001288static void btusb_waker(struct work_struct *work)
1289{
1290 struct btusb_data *data = container_of(work, struct btusb_data, waker);
1291 int err;
1292
1293 err = usb_autopm_get_interface(data->intf);
1294 if (err < 0)
1295 return;
1296
1297 usb_autopm_put_interface(data->intf);
1298}
1299
Daniel Drake6576fe42015-02-23 18:16:47 -06001300static struct sk_buff *btusb_read_local_version(struct hci_dev *hdev)
1301{
1302 struct sk_buff *skb;
1303
1304 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
1305 HCI_INIT_TIMEOUT);
1306 if (IS_ERR(skb)) {
1307 BT_ERR("%s: HCI_OP_READ_LOCAL_VERSION failed (%ld)",
1308 hdev->name, PTR_ERR(skb));
1309 return skb;
1310 }
1311
1312 if (skb->len != sizeof(struct hci_rp_read_local_version)) {
1313 BT_ERR("%s: HCI_OP_READ_LOCAL_VERSION event length mismatch",
1314 hdev->name);
1315 kfree_skb(skb);
1316 return ERR_PTR(-EIO);
1317 }
1318
1319 return skb;
1320}
1321
Marcel Holtmann9f8f9622013-04-10 08:11:35 -07001322static int btusb_setup_bcm92035(struct hci_dev *hdev)
1323{
1324 struct sk_buff *skb;
1325 u8 val = 0x00;
1326
1327 BT_DBG("%s", hdev->name);
1328
1329 skb = __hci_cmd_sync(hdev, 0xfc3b, 1, &val, HCI_INIT_TIMEOUT);
1330 if (IS_ERR(skb))
1331 BT_ERR("BCM92035 command failed (%ld)", -PTR_ERR(skb));
1332 else
1333 kfree_skb(skb);
1334
1335 return 0;
1336}
1337
Marcel Holtmann81cac642014-01-03 03:02:36 -08001338static int btusb_setup_csr(struct hci_dev *hdev)
1339{
1340 struct hci_rp_read_local_version *rp;
1341 struct sk_buff *skb;
1342 int ret;
1343
1344 BT_DBG("%s", hdev->name);
1345
Daniel Drake6576fe42015-02-23 18:16:47 -06001346 skb = btusb_read_local_version(hdev);
1347 if (IS_ERR(skb))
Marcel Holtmann81cac642014-01-03 03:02:36 -08001348 return -PTR_ERR(skb);
Marcel Holtmann81cac642014-01-03 03:02:36 -08001349
Marcel Holtmann89e75332014-09-16 04:44:50 +02001350 rp = (struct hci_rp_read_local_version *)skb->data;
Marcel Holtmann81cac642014-01-03 03:02:36 -08001351
1352 if (!rp->status) {
1353 if (le16_to_cpu(rp->manufacturer) != 10) {
1354 /* Clear the reset quirk since this is not an actual
1355 * early Bluetooth 1.1 device from CSR.
1356 */
1357 clear_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
1358
1359 /* These fake CSR controllers have all a broken
1360 * stored link key handling and so just disable it.
1361 */
1362 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY,
1363 &hdev->quirks);
1364 }
1365 }
1366
1367 ret = -bt_to_errno(rp->status);
1368
1369 kfree_skb(skb);
1370
1371 return ret;
1372}
1373
Daniel Drakea2698a92015-04-16 14:09:55 -06001374#define RTL_FRAG_LEN 252
1375
1376struct rtl_download_cmd {
1377 __u8 index;
1378 __u8 data[RTL_FRAG_LEN];
1379} __packed;
1380
1381struct rtl_download_response {
1382 __u8 status;
1383 __u8 index;
1384} __packed;
1385
1386struct rtl_rom_version_evt {
1387 __u8 status;
1388 __u8 version;
1389} __packed;
1390
1391struct rtl_epatch_header {
1392 __u8 signature[8];
1393 __le32 fw_version;
1394 __le16 num_patches;
1395} __packed;
1396
1397#define RTL_EPATCH_SIGNATURE "Realtech"
1398#define RTL_ROM_LMP_3499 0x3499
1399#define RTL_ROM_LMP_8723A 0x1200
1400#define RTL_ROM_LMP_8723B 0x8723
1401#define RTL_ROM_LMP_8821A 0x8821
1402#define RTL_ROM_LMP_8761A 0x8761
1403
1404static int rtl_read_rom_version(struct hci_dev *hdev, u8 *version)
1405{
1406 struct rtl_rom_version_evt *rom_version;
1407 struct sk_buff *skb;
1408 int ret;
1409
1410 /* Read RTL ROM version command */
1411 skb = __hci_cmd_sync(hdev, 0xfc6d, 0, NULL, HCI_INIT_TIMEOUT);
1412 if (IS_ERR(skb)) {
1413 BT_ERR("%s: Read ROM version failed (%ld)",
1414 hdev->name, PTR_ERR(skb));
1415 return PTR_ERR(skb);
1416 }
1417
1418 if (skb->len != sizeof(*rom_version)) {
1419 BT_ERR("%s: RTL version event length mismatch", hdev->name);
1420 kfree_skb(skb);
1421 return -EIO;
1422 }
1423
1424 rom_version = (struct rtl_rom_version_evt *)skb->data;
1425 BT_INFO("%s: rom_version status=%x version=%x",
1426 hdev->name, rom_version->status, rom_version->version);
1427
1428 ret = rom_version->status;
1429 if (ret == 0)
1430 *version = rom_version->version;
1431
1432 kfree_skb(skb);
1433 return ret;
1434}
1435
1436static int rtl8723b_parse_firmware(struct hci_dev *hdev, u16 lmp_subver,
1437 const struct firmware *fw,
1438 unsigned char **_buf)
1439{
1440 const u8 extension_sig[] = { 0x51, 0x04, 0xfd, 0x77 };
1441 struct rtl_epatch_header *epatch_info;
1442 unsigned char *buf;
1443 int i, ret, len;
1444 size_t min_size;
1445 u8 opcode, length, data, rom_version = 0;
1446 int project_id = -1;
1447 const unsigned char *fwptr, *chip_id_base;
1448 const unsigned char *patch_length_base, *patch_offset_base;
1449 u32 patch_offset = 0;
1450 u16 patch_length, num_patches;
1451 const u16 project_id_to_lmp_subver[] = {
1452 RTL_ROM_LMP_8723A,
1453 RTL_ROM_LMP_8723B,
1454 RTL_ROM_LMP_8821A,
1455 RTL_ROM_LMP_8761A
1456 };
1457
1458 ret = rtl_read_rom_version(hdev, &rom_version);
1459 if (ret)
1460 return -bt_to_errno(ret);
1461
1462 min_size = sizeof(struct rtl_epatch_header) + sizeof(extension_sig) + 3;
1463 if (fw->size < min_size)
1464 return -EINVAL;
1465
1466 fwptr = fw->data + fw->size - sizeof(extension_sig);
1467 if (memcmp(fwptr, extension_sig, sizeof(extension_sig)) != 0) {
1468 BT_ERR("%s: extension section signature mismatch", hdev->name);
1469 return -EINVAL;
1470 }
1471
1472 /* Loop from the end of the firmware parsing instructions, until
1473 * we find an instruction that identifies the "project ID" for the
1474 * hardware supported by this firwmare file.
1475 * Once we have that, we double-check that that project_id is suitable
1476 * for the hardware we are working with.
1477 */
1478 while (fwptr >= fw->data + (sizeof(struct rtl_epatch_header) + 3)) {
1479 opcode = *--fwptr;
1480 length = *--fwptr;
1481 data = *--fwptr;
1482
1483 BT_DBG("check op=%x len=%x data=%x", opcode, length, data);
1484
1485 if (opcode == 0xff) /* EOF */
1486 break;
1487
1488 if (length == 0) {
1489 BT_ERR("%s: found instruction with length 0",
1490 hdev->name);
1491 return -EINVAL;
1492 }
1493
1494 if (opcode == 0 && length == 1) {
1495 project_id = data;
1496 break;
1497 }
1498
1499 fwptr -= length;
1500 }
1501
1502 if (project_id < 0) {
1503 BT_ERR("%s: failed to find version instruction", hdev->name);
1504 return -EINVAL;
1505 }
1506
Dan Carpenterc57ddfa2015-04-20 18:51:35 +03001507 if (project_id >= ARRAY_SIZE(project_id_to_lmp_subver)) {
Daniel Drakea2698a92015-04-16 14:09:55 -06001508 BT_ERR("%s: unknown project id %d", hdev->name, project_id);
1509 return -EINVAL;
1510 }
1511
1512 if (lmp_subver != project_id_to_lmp_subver[project_id]) {
1513 BT_ERR("%s: firmware is for %x but this is a %x", hdev->name,
1514 project_id_to_lmp_subver[project_id], lmp_subver);
1515 return -EINVAL;
1516 }
1517
1518 epatch_info = (struct rtl_epatch_header *)fw->data;
1519 if (memcmp(epatch_info->signature, RTL_EPATCH_SIGNATURE, 8) != 0) {
1520 BT_ERR("%s: bad EPATCH signature", hdev->name);
1521 return -EINVAL;
1522 }
1523
1524 num_patches = le16_to_cpu(epatch_info->num_patches);
1525 BT_DBG("fw_version=%x, num_patches=%d",
1526 le32_to_cpu(epatch_info->fw_version), num_patches);
1527
1528 /* After the rtl_epatch_header there is a funky patch metadata section.
1529 * Assuming 2 patches, the layout is:
1530 * ChipID1 ChipID2 PatchLength1 PatchLength2 PatchOffset1 PatchOffset2
1531 *
1532 * Find the right patch for this chip.
1533 */
1534 min_size += 8 * num_patches;
1535 if (fw->size < min_size)
1536 return -EINVAL;
1537
1538 chip_id_base = fw->data + sizeof(struct rtl_epatch_header);
1539 patch_length_base = chip_id_base + (sizeof(u16) * num_patches);
1540 patch_offset_base = patch_length_base + (sizeof(u16) * num_patches);
1541 for (i = 0; i < num_patches; i++) {
1542 u16 chip_id = get_unaligned_le16(chip_id_base +
1543 (i * sizeof(u16)));
1544 if (chip_id == rom_version + 1) {
1545 patch_length = get_unaligned_le16(patch_length_base +
1546 (i * sizeof(u16)));
1547 patch_offset = get_unaligned_le32(patch_offset_base +
1548 (i * sizeof(u32)));
1549 break;
1550 }
1551 }
1552
1553 if (!patch_offset) {
1554 BT_ERR("%s: didn't find patch for chip id %d",
1555 hdev->name, rom_version);
1556 return -EINVAL;
1557 }
1558
1559 BT_DBG("length=%x offset=%x index %d", patch_length, patch_offset, i);
1560 min_size = patch_offset + patch_length;
1561 if (fw->size < min_size)
1562 return -EINVAL;
1563
1564 /* Copy the firmware into a new buffer and write the version at
1565 * the end.
1566 */
1567 len = patch_length;
1568 buf = kmemdup(fw->data + patch_offset, patch_length, GFP_KERNEL);
1569 if (!buf)
1570 return -ENOMEM;
1571
1572 memcpy(buf + patch_length - 4, &epatch_info->fw_version, 4);
1573
1574 *_buf = buf;
1575 return len;
1576}
1577
1578static int rtl_download_firmware(struct hci_dev *hdev,
1579 const unsigned char *data, int fw_len)
1580{
1581 struct rtl_download_cmd *dl_cmd;
1582 int frag_num = fw_len / RTL_FRAG_LEN + 1;
1583 int frag_len = RTL_FRAG_LEN;
1584 int ret = 0;
1585 int i;
1586
1587 dl_cmd = kmalloc(sizeof(struct rtl_download_cmd), GFP_KERNEL);
1588 if (!dl_cmd)
1589 return -ENOMEM;
1590
1591 for (i = 0; i < frag_num; i++) {
1592 struct rtl_download_response *dl_resp;
1593 struct sk_buff *skb;
1594
1595 BT_DBG("download fw (%d/%d)", i, frag_num);
1596
1597 dl_cmd->index = i;
1598 if (i == (frag_num - 1)) {
1599 dl_cmd->index |= 0x80; /* data end */
1600 frag_len = fw_len % RTL_FRAG_LEN;
1601 }
1602 memcpy(dl_cmd->data, data, frag_len);
1603
1604 /* Send download command */
1605 skb = __hci_cmd_sync(hdev, 0xfc20, frag_len + 1, dl_cmd,
1606 HCI_INIT_TIMEOUT);
1607 if (IS_ERR(skb)) {
1608 BT_ERR("%s: download fw command failed (%ld)",
1609 hdev->name, PTR_ERR(skb));
1610 ret = -PTR_ERR(skb);
1611 goto out;
1612 }
1613
1614 if (skb->len != sizeof(*dl_resp)) {
1615 BT_ERR("%s: download fw event length mismatch",
1616 hdev->name);
1617 kfree_skb(skb);
1618 ret = -EIO;
1619 goto out;
1620 }
1621
1622 dl_resp = (struct rtl_download_response *)skb->data;
1623 if (dl_resp->status != 0) {
1624 kfree_skb(skb);
1625 ret = bt_to_errno(dl_resp->status);
1626 goto out;
1627 }
1628
1629 kfree_skb(skb);
1630 data += RTL_FRAG_LEN;
1631 }
1632
1633out:
1634 kfree(dl_cmd);
1635 return ret;
1636}
1637
1638static int btusb_setup_rtl8723a(struct hci_dev *hdev)
1639{
1640 struct btusb_data *data = dev_get_drvdata(&hdev->dev);
1641 struct usb_device *udev = interface_to_usbdev(data->intf);
1642 const struct firmware *fw;
1643 int ret;
1644
1645 BT_INFO("%s: rtl: loading rtl_bt/rtl8723a_fw.bin", hdev->name);
1646 ret = request_firmware(&fw, "rtl_bt/rtl8723a_fw.bin", &udev->dev);
1647 if (ret < 0) {
1648 BT_ERR("%s: Failed to load rtl_bt/rtl8723a_fw.bin", hdev->name);
1649 return ret;
1650 }
1651
1652 if (fw->size < 8) {
1653 ret = -EINVAL;
1654 goto out;
1655 }
1656
1657 /* Check that the firmware doesn't have the epatch signature
1658 * (which is only for RTL8723B and newer).
1659 */
1660 if (!memcmp(fw->data, RTL_EPATCH_SIGNATURE, 8)) {
1661 BT_ERR("%s: unexpected EPATCH signature!", hdev->name);
1662 ret = -EINVAL;
1663 goto out;
1664 }
1665
1666 ret = rtl_download_firmware(hdev, fw->data, fw->size);
1667
1668out:
1669 release_firmware(fw);
1670 return ret;
1671}
1672
1673static int btusb_setup_rtl8723b(struct hci_dev *hdev, u16 lmp_subver,
1674 const char *fw_name)
1675{
1676 struct btusb_data *data = dev_get_drvdata(&hdev->dev);
1677 struct usb_device *udev = interface_to_usbdev(data->intf);
1678 unsigned char *fw_data = NULL;
1679 const struct firmware *fw;
1680 int ret;
1681
1682 BT_INFO("%s: rtl: loading %s", hdev->name, fw_name);
1683 ret = request_firmware(&fw, fw_name, &udev->dev);
1684 if (ret < 0) {
1685 BT_ERR("%s: Failed to load %s", hdev->name, fw_name);
1686 return ret;
1687 }
1688
1689 ret = rtl8723b_parse_firmware(hdev, lmp_subver, fw, &fw_data);
1690 if (ret < 0)
1691 goto out;
1692
1693 ret = rtl_download_firmware(hdev, fw_data, ret);
1694 kfree(fw_data);
1695 if (ret < 0)
1696 goto out;
1697
1698out:
1699 release_firmware(fw);
1700 return ret;
1701}
1702
1703static int btusb_setup_realtek(struct hci_dev *hdev)
1704{
1705 struct sk_buff *skb;
1706 struct hci_rp_read_local_version *resp;
1707 u16 lmp_subver;
1708
1709 skb = btusb_read_local_version(hdev);
1710 if (IS_ERR(skb))
1711 return -PTR_ERR(skb);
1712
1713 resp = (struct hci_rp_read_local_version *)skb->data;
1714 BT_INFO("%s: rtl: examining hci_ver=%02x hci_rev=%04x lmp_ver=%02x "
1715 "lmp_subver=%04x", hdev->name, resp->hci_ver, resp->hci_rev,
1716 resp->lmp_ver, resp->lmp_subver);
1717
1718 lmp_subver = le16_to_cpu(resp->lmp_subver);
1719 kfree_skb(skb);
1720
1721 /* Match a set of subver values that correspond to stock firmware,
1722 * which is not compatible with standard btusb.
1723 * If matched, upload an alternative firmware that does conform to
1724 * standard btusb. Once that firmware is uploaded, the subver changes
1725 * to a different value.
1726 */
1727 switch (lmp_subver) {
1728 case RTL_ROM_LMP_8723A:
1729 case RTL_ROM_LMP_3499:
1730 return btusb_setup_rtl8723a(hdev);
1731 case RTL_ROM_LMP_8723B:
1732 return btusb_setup_rtl8723b(hdev, lmp_subver,
1733 "rtl_bt/rtl8723b_fw.bin");
1734 case RTL_ROM_LMP_8821A:
1735 return btusb_setup_rtl8723b(hdev, lmp_subver,
1736 "rtl_bt/rtl8821a_fw.bin");
1737 case RTL_ROM_LMP_8761A:
1738 return btusb_setup_rtl8723b(hdev, lmp_subver,
1739 "rtl_bt/rtl8761a_fw.bin");
1740 default:
1741 BT_INFO("rtl: assuming no firmware upload needed.");
1742 return 0;
1743 }
1744}
1745
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001746static const struct firmware *btusb_setup_intel_get_fw(struct hci_dev *hdev,
Marcel Holtmann89e75332014-09-16 04:44:50 +02001747 struct intel_version *ver)
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001748{
1749 const struct firmware *fw;
1750 char fwname[64];
1751 int ret;
1752
1753 snprintf(fwname, sizeof(fwname),
1754 "intel/ibt-hw-%x.%x.%x-fw-%x.%x.%x.%x.%x.bseq",
1755 ver->hw_platform, ver->hw_variant, ver->hw_revision,
1756 ver->fw_variant, ver->fw_revision, ver->fw_build_num,
1757 ver->fw_build_ww, ver->fw_build_yy);
1758
1759 ret = request_firmware(&fw, fwname, &hdev->dev);
1760 if (ret < 0) {
1761 if (ret == -EINVAL) {
1762 BT_ERR("%s Intel firmware file request failed (%d)",
1763 hdev->name, ret);
1764 return NULL;
1765 }
1766
1767 BT_ERR("%s failed to open Intel firmware file: %s(%d)",
1768 hdev->name, fwname, ret);
1769
1770 /* If the correct firmware patch file is not found, use the
1771 * default firmware patch file instead
1772 */
1773 snprintf(fwname, sizeof(fwname), "intel/ibt-hw-%x.%x.bseq",
1774 ver->hw_platform, ver->hw_variant);
1775 if (request_firmware(&fw, fwname, &hdev->dev) < 0) {
1776 BT_ERR("%s failed to open default Intel fw file: %s",
1777 hdev->name, fwname);
1778 return NULL;
1779 }
1780 }
1781
1782 BT_INFO("%s: Intel Bluetooth firmware file: %s", hdev->name, fwname);
1783
1784 return fw;
1785}
1786
1787static int btusb_setup_intel_patching(struct hci_dev *hdev,
1788 const struct firmware *fw,
1789 const u8 **fw_ptr, int *disable_patch)
1790{
1791 struct sk_buff *skb;
1792 struct hci_command_hdr *cmd;
1793 const u8 *cmd_param;
1794 struct hci_event_hdr *evt = NULL;
1795 const u8 *evt_param = NULL;
1796 int remain = fw->size - (*fw_ptr - fw->data);
1797
1798 /* The first byte indicates the types of the patch command or event.
1799 * 0x01 means HCI command and 0x02 is HCI event. If the first bytes
1800 * in the current firmware buffer doesn't start with 0x01 or
1801 * the size of remain buffer is smaller than HCI command header,
1802 * the firmware file is corrupted and it should stop the patching
1803 * process.
1804 */
1805 if (remain > HCI_COMMAND_HDR_SIZE && *fw_ptr[0] != 0x01) {
1806 BT_ERR("%s Intel fw corrupted: invalid cmd read", hdev->name);
1807 return -EINVAL;
1808 }
1809 (*fw_ptr)++;
1810 remain--;
1811
1812 cmd = (struct hci_command_hdr *)(*fw_ptr);
1813 *fw_ptr += sizeof(*cmd);
1814 remain -= sizeof(*cmd);
1815
1816 /* Ensure that the remain firmware data is long enough than the length
1817 * of command parameter. If not, the firmware file is corrupted.
1818 */
1819 if (remain < cmd->plen) {
1820 BT_ERR("%s Intel fw corrupted: invalid cmd len", hdev->name);
1821 return -EFAULT;
1822 }
1823
1824 /* If there is a command that loads a patch in the firmware
1825 * file, then enable the patch upon success, otherwise just
1826 * disable the manufacturer mode, for example patch activation
1827 * is not required when the default firmware patch file is used
1828 * because there are no patch data to load.
1829 */
1830 if (*disable_patch && le16_to_cpu(cmd->opcode) == 0xfc8e)
1831 *disable_patch = 0;
1832
1833 cmd_param = *fw_ptr;
1834 *fw_ptr += cmd->plen;
1835 remain -= cmd->plen;
1836
1837 /* This reads the expected events when the above command is sent to the
1838 * device. Some vendor commands expects more than one events, for
1839 * example command status event followed by vendor specific event.
1840 * For this case, it only keeps the last expected event. so the command
1841 * can be sent with __hci_cmd_sync_ev() which returns the sk_buff of
1842 * last expected event.
1843 */
1844 while (remain > HCI_EVENT_HDR_SIZE && *fw_ptr[0] == 0x02) {
1845 (*fw_ptr)++;
1846 remain--;
1847
1848 evt = (struct hci_event_hdr *)(*fw_ptr);
1849 *fw_ptr += sizeof(*evt);
1850 remain -= sizeof(*evt);
1851
1852 if (remain < evt->plen) {
1853 BT_ERR("%s Intel fw corrupted: invalid evt len",
1854 hdev->name);
1855 return -EFAULT;
1856 }
1857
1858 evt_param = *fw_ptr;
1859 *fw_ptr += evt->plen;
1860 remain -= evt->plen;
1861 }
1862
1863 /* Every HCI commands in the firmware file has its correspond event.
1864 * If event is not found or remain is smaller than zero, the firmware
1865 * file is corrupted.
1866 */
1867 if (!evt || !evt_param || remain < 0) {
1868 BT_ERR("%s Intel fw corrupted: invalid evt read", hdev->name);
1869 return -EFAULT;
1870 }
1871
1872 skb = __hci_cmd_sync_ev(hdev, le16_to_cpu(cmd->opcode), cmd->plen,
1873 cmd_param, evt->evt, HCI_INIT_TIMEOUT);
1874 if (IS_ERR(skb)) {
1875 BT_ERR("%s sending Intel patch command (0x%4.4x) failed (%ld)",
1876 hdev->name, cmd->opcode, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001877 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001878 }
1879
1880 /* It ensures that the returned event matches the event data read from
1881 * the firmware file. At fist, it checks the length and then
1882 * the contents of the event.
1883 */
1884 if (skb->len != evt->plen) {
1885 BT_ERR("%s mismatch event length (opcode 0x%4.4x)", hdev->name,
1886 le16_to_cpu(cmd->opcode));
1887 kfree_skb(skb);
1888 return -EFAULT;
1889 }
1890
1891 if (memcmp(skb->data, evt_param, evt->plen)) {
1892 BT_ERR("%s mismatch event parameter (opcode 0x%4.4x)",
1893 hdev->name, le16_to_cpu(cmd->opcode));
1894 kfree_skb(skb);
1895 return -EFAULT;
1896 }
1897 kfree_skb(skb);
1898
1899 return 0;
1900}
1901
1902static int btusb_setup_intel(struct hci_dev *hdev)
1903{
1904 struct sk_buff *skb;
1905 const struct firmware *fw;
1906 const u8 *fw_ptr;
1907 int disable_patch;
1908 struct intel_version *ver;
1909
1910 const u8 mfg_enable[] = { 0x01, 0x00 };
1911 const u8 mfg_disable[] = { 0x00, 0x00 };
1912 const u8 mfg_reset_deactivate[] = { 0x00, 0x01 };
1913 const u8 mfg_reset_activate[] = { 0x00, 0x02 };
1914
1915 BT_DBG("%s", hdev->name);
1916
1917 /* The controller has a bug with the first HCI command sent to it
1918 * returning number of completed commands as zero. This would stall the
1919 * command processing in the Bluetooth core.
1920 *
1921 * As a workaround, send HCI Reset command first which will reset the
1922 * number of completed commands and allow normal command processing
1923 * from now on.
1924 */
1925 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
1926 if (IS_ERR(skb)) {
1927 BT_ERR("%s sending initial HCI reset command failed (%ld)",
1928 hdev->name, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001929 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001930 }
1931 kfree_skb(skb);
1932
1933 /* Read Intel specific controller version first to allow selection of
1934 * which firmware file to load.
1935 *
1936 * The returned information are hardware variant and revision plus
1937 * firmware variant, revision and build number.
1938 */
1939 skb = __hci_cmd_sync(hdev, 0xfc05, 0, NULL, HCI_INIT_TIMEOUT);
1940 if (IS_ERR(skb)) {
1941 BT_ERR("%s reading Intel fw version command failed (%ld)",
1942 hdev->name, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001943 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001944 }
1945
1946 if (skb->len != sizeof(*ver)) {
1947 BT_ERR("%s Intel version event length mismatch", hdev->name);
1948 kfree_skb(skb);
1949 return -EIO;
1950 }
1951
1952 ver = (struct intel_version *)skb->data;
1953 if (ver->status) {
1954 BT_ERR("%s Intel fw version event failed (%02x)", hdev->name,
1955 ver->status);
1956 kfree_skb(skb);
1957 return -bt_to_errno(ver->status);
1958 }
1959
1960 BT_INFO("%s: read Intel version: %02x%02x%02x%02x%02x%02x%02x%02x%02x",
1961 hdev->name, ver->hw_platform, ver->hw_variant,
1962 ver->hw_revision, ver->fw_variant, ver->fw_revision,
1963 ver->fw_build_num, ver->fw_build_ww, ver->fw_build_yy,
1964 ver->fw_patch_num);
1965
1966 /* fw_patch_num indicates the version of patch the device currently
1967 * have. If there is no patch data in the device, it is always 0x00.
1968 * So, if it is other than 0x00, no need to patch the deivce again.
1969 */
1970 if (ver->fw_patch_num) {
1971 BT_INFO("%s: Intel device is already patched. patch num: %02x",
1972 hdev->name, ver->fw_patch_num);
1973 kfree_skb(skb);
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07001974 btintel_check_bdaddr(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001975 return 0;
1976 }
1977
1978 /* Opens the firmware patch file based on the firmware version read
1979 * from the controller. If it fails to open the matching firmware
1980 * patch file, it tries to open the default firmware patch file.
1981 * If no patch file is found, allow the device to operate without
1982 * a patch.
1983 */
1984 fw = btusb_setup_intel_get_fw(hdev, ver);
1985 if (!fw) {
1986 kfree_skb(skb);
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07001987 btintel_check_bdaddr(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001988 return 0;
1989 }
1990 fw_ptr = fw->data;
1991
1992 /* This Intel specific command enables the manufacturer mode of the
1993 * controller.
1994 *
1995 * Only while this mode is enabled, the driver can download the
1996 * firmware patch data and configuration parameters.
1997 */
1998 skb = __hci_cmd_sync(hdev, 0xfc11, 2, mfg_enable, HCI_INIT_TIMEOUT);
1999 if (IS_ERR(skb)) {
2000 BT_ERR("%s entering Intel manufacturer mode failed (%ld)",
2001 hdev->name, PTR_ERR(skb));
2002 release_firmware(fw);
Adam Leed9c78e92013-07-10 10:02:12 +08002003 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002004 }
2005
2006 if (skb->data[0]) {
2007 u8 evt_status = skb->data[0];
Marcel Holtmann89e75332014-09-16 04:44:50 +02002008
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002009 BT_ERR("%s enable Intel manufacturer mode event failed (%02x)",
2010 hdev->name, evt_status);
2011 kfree_skb(skb);
2012 release_firmware(fw);
2013 return -bt_to_errno(evt_status);
2014 }
2015 kfree_skb(skb);
2016
2017 disable_patch = 1;
2018
2019 /* The firmware data file consists of list of Intel specific HCI
2020 * commands and its expected events. The first byte indicates the
2021 * type of the message, either HCI command or HCI event.
2022 *
2023 * It reads the command and its expected event from the firmware file,
2024 * and send to the controller. Once __hci_cmd_sync_ev() returns,
2025 * the returned event is compared with the event read from the firmware
2026 * file and it will continue until all the messages are downloaded to
2027 * the controller.
2028 *
2029 * Once the firmware patching is completed successfully,
2030 * the manufacturer mode is disabled with reset and activating the
2031 * downloaded patch.
2032 *
2033 * If the firmware patching fails, the manufacturer mode is
2034 * disabled with reset and deactivating the patch.
2035 *
2036 * If the default patch file is used, no reset is done when disabling
2037 * the manufacturer.
2038 */
2039 while (fw->size > fw_ptr - fw->data) {
2040 int ret;
2041
2042 ret = btusb_setup_intel_patching(hdev, fw, &fw_ptr,
2043 &disable_patch);
2044 if (ret < 0)
2045 goto exit_mfg_deactivate;
2046 }
2047
2048 release_firmware(fw);
2049
2050 if (disable_patch)
2051 goto exit_mfg_disable;
2052
2053 /* Patching completed successfully and disable the manufacturer mode
2054 * with reset and activate the downloaded firmware patches.
2055 */
2056 skb = __hci_cmd_sync(hdev, 0xfc11, sizeof(mfg_reset_activate),
2057 mfg_reset_activate, HCI_INIT_TIMEOUT);
2058 if (IS_ERR(skb)) {
2059 BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
2060 hdev->name, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08002061 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002062 }
2063 kfree_skb(skb);
2064
2065 BT_INFO("%s: Intel Bluetooth firmware patch completed and activated",
2066 hdev->name);
2067
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07002068 btintel_check_bdaddr(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002069 return 0;
2070
2071exit_mfg_disable:
2072 /* Disable the manufacturer mode without reset */
2073 skb = __hci_cmd_sync(hdev, 0xfc11, sizeof(mfg_disable), mfg_disable,
2074 HCI_INIT_TIMEOUT);
2075 if (IS_ERR(skb)) {
2076 BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
2077 hdev->name, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08002078 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002079 }
2080 kfree_skb(skb);
2081
2082 BT_INFO("%s: Intel Bluetooth firmware patch completed", hdev->name);
Marcel Holtmann40cb0982014-07-02 12:06:45 +02002083
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07002084 btintel_check_bdaddr(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002085 return 0;
2086
2087exit_mfg_deactivate:
2088 release_firmware(fw);
2089
2090 /* Patching failed. Disable the manufacturer mode with reset and
2091 * deactivate the downloaded firmware patches.
2092 */
2093 skb = __hci_cmd_sync(hdev, 0xfc11, sizeof(mfg_reset_deactivate),
2094 mfg_reset_deactivate, HCI_INIT_TIMEOUT);
2095 if (IS_ERR(skb)) {
2096 BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
2097 hdev->name, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08002098 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002099 }
2100 kfree_skb(skb);
2101
2102 BT_INFO("%s: Intel Bluetooth firmware patch completed and deactivated",
2103 hdev->name);
2104
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07002105 btintel_check_bdaddr(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002106 return 0;
2107}
2108
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002109static int inject_cmd_complete(struct hci_dev *hdev, __u16 opcode)
2110{
2111 struct sk_buff *skb;
2112 struct hci_event_hdr *hdr;
2113 struct hci_ev_cmd_complete *evt;
2114
2115 skb = bt_skb_alloc(sizeof(*hdr) + sizeof(*evt) + 1, GFP_ATOMIC);
2116 if (!skb)
2117 return -ENOMEM;
2118
2119 hdr = (struct hci_event_hdr *)skb_put(skb, sizeof(*hdr));
2120 hdr->evt = HCI_EV_CMD_COMPLETE;
2121 hdr->plen = sizeof(*evt) + 1;
2122
2123 evt = (struct hci_ev_cmd_complete *)skb_put(skb, sizeof(*evt));
2124 evt->ncmd = 0x01;
2125 evt->opcode = cpu_to_le16(opcode);
2126
2127 *skb_put(skb, 1) = 0x00;
2128
2129 bt_cb(skb)->pkt_type = HCI_EVENT_PKT;
2130
2131 return hci_recv_frame(hdev, skb);
2132}
2133
2134static int btusb_recv_bulk_intel(struct btusb_data *data, void *buffer,
2135 int count)
2136{
2137 /* When the device is in bootloader mode, then it can send
2138 * events via the bulk endpoint. These events are treated the
2139 * same way as the ones received from the interrupt endpoint.
2140 */
2141 if (test_bit(BTUSB_BOOTLOADER, &data->flags))
2142 return btusb_recv_intr(data, buffer, count);
2143
2144 return btusb_recv_bulk(data, buffer, count);
2145}
2146
Marcel Holtmannccd6da22015-04-09 00:35:19 -07002147static void btusb_intel_bootup(struct btusb_data *data, const void *ptr,
2148 unsigned int len)
2149{
2150 const struct intel_bootup *evt = ptr;
2151
2152 if (len != sizeof(*evt))
2153 return;
2154
2155 if (test_and_clear_bit(BTUSB_BOOTING, &data->flags)) {
2156 smp_mb__after_atomic();
2157 wake_up_bit(&data->flags, BTUSB_BOOTING);
2158 }
2159}
2160
2161static void btusb_intel_secure_send_result(struct btusb_data *data,
2162 const void *ptr, unsigned int len)
2163{
2164 const struct intel_secure_send_result *evt = ptr;
2165
2166 if (len != sizeof(*evt))
2167 return;
2168
2169 if (evt->result)
2170 set_bit(BTUSB_FIRMWARE_FAILED, &data->flags);
2171
2172 if (test_and_clear_bit(BTUSB_DOWNLOADING, &data->flags) &&
2173 test_bit(BTUSB_FIRMWARE_LOADED, &data->flags)) {
2174 smp_mb__after_atomic();
2175 wake_up_bit(&data->flags, BTUSB_DOWNLOADING);
2176 }
2177}
2178
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002179static int btusb_recv_event_intel(struct hci_dev *hdev, struct sk_buff *skb)
2180{
2181 struct btusb_data *data = hci_get_drvdata(hdev);
2182
2183 if (test_bit(BTUSB_BOOTLOADER, &data->flags)) {
2184 struct hci_event_hdr *hdr = (void *)skb->data;
2185
Marcel Holtmannccd6da22015-04-09 00:35:19 -07002186 if (skb->len > HCI_EVENT_HDR_SIZE && hdr->evt == 0xff &&
2187 hdr->plen > 0) {
2188 const void *ptr = skb->data + HCI_EVENT_HDR_SIZE + 1;
2189 unsigned int len = skb->len - HCI_EVENT_HDR_SIZE - 1;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002190
Marcel Holtmannccd6da22015-04-09 00:35:19 -07002191 switch (skb->data[2]) {
2192 case 0x02:
2193 /* When switching to the operational firmware
2194 * the device sends a vendor specific event
2195 * indicating that the bootup completed.
2196 */
2197 btusb_intel_bootup(data, ptr, len);
2198 break;
2199 case 0x06:
2200 /* When the firmware loading completes the
2201 * device sends out a vendor specific event
2202 * indicating the result of the firmware
2203 * loading.
2204 */
2205 btusb_intel_secure_send_result(data, ptr, len);
2206 break;
Johan Hedbergfad70972015-01-30 10:58:55 +02002207 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002208 }
2209 }
2210
2211 return hci_recv_frame(hdev, skb);
2212}
2213
2214static int btusb_send_frame_intel(struct hci_dev *hdev, struct sk_buff *skb)
2215{
2216 struct btusb_data *data = hci_get_drvdata(hdev);
2217 struct urb *urb;
2218
2219 BT_DBG("%s", hdev->name);
2220
2221 if (!test_bit(HCI_RUNNING, &hdev->flags))
2222 return -EBUSY;
2223
2224 switch (bt_cb(skb)->pkt_type) {
2225 case HCI_COMMAND_PKT:
2226 if (test_bit(BTUSB_BOOTLOADER, &data->flags)) {
2227 struct hci_command_hdr *cmd = (void *)skb->data;
2228 __u16 opcode = le16_to_cpu(cmd->opcode);
2229
2230 /* When in bootloader mode and the command 0xfc09
2231 * is received, it needs to be send down the
2232 * bulk endpoint. So allocate a bulk URB instead.
2233 */
2234 if (opcode == 0xfc09)
2235 urb = alloc_bulk_urb(hdev, skb);
2236 else
2237 urb = alloc_ctrl_urb(hdev, skb);
2238
2239 /* When the 0xfc01 command is issued to boot into
2240 * the operational firmware, it will actually not
2241 * send a command complete event. To keep the flow
2242 * control working inject that event here.
2243 */
2244 if (opcode == 0xfc01)
2245 inject_cmd_complete(hdev, opcode);
2246 } else {
2247 urb = alloc_ctrl_urb(hdev, skb);
2248 }
2249 if (IS_ERR(urb))
2250 return PTR_ERR(urb);
2251
2252 hdev->stat.cmd_tx++;
2253 return submit_or_queue_tx_urb(hdev, urb);
2254
2255 case HCI_ACLDATA_PKT:
2256 urb = alloc_bulk_urb(hdev, skb);
2257 if (IS_ERR(urb))
2258 return PTR_ERR(urb);
2259
2260 hdev->stat.acl_tx++;
2261 return submit_or_queue_tx_urb(hdev, urb);
2262
2263 case HCI_SCODATA_PKT:
2264 if (hci_conn_num(hdev, SCO_LINK) < 1)
2265 return -ENODEV;
2266
2267 urb = alloc_isoc_urb(hdev, skb);
2268 if (IS_ERR(urb))
2269 return PTR_ERR(urb);
2270
2271 hdev->stat.sco_tx++;
2272 return submit_tx_urb(hdev, urb);
2273 }
2274
2275 return -EILSEQ;
2276}
2277
2278static int btusb_intel_secure_send(struct hci_dev *hdev, u8 fragment_type,
2279 u32 plen, const void *param)
2280{
2281 while (plen > 0) {
2282 struct sk_buff *skb;
2283 u8 cmd_param[253], fragment_len = (plen > 252) ? 252 : plen;
2284
2285 cmd_param[0] = fragment_type;
2286 memcpy(cmd_param + 1, param, fragment_len);
2287
2288 skb = __hci_cmd_sync(hdev, 0xfc09, fragment_len + 1,
2289 cmd_param, HCI_INIT_TIMEOUT);
2290 if (IS_ERR(skb))
2291 return PTR_ERR(skb);
2292
2293 kfree_skb(skb);
2294
2295 plen -= fragment_len;
2296 param += fragment_len;
2297 }
2298
2299 return 0;
2300}
2301
2302static void btusb_intel_version_info(struct hci_dev *hdev,
2303 struct intel_version *ver)
2304{
2305 const char *variant;
2306
2307 switch (ver->fw_variant) {
2308 case 0x06:
2309 variant = "Bootloader";
2310 break;
2311 case 0x23:
2312 variant = "Firmware";
2313 break;
2314 default:
2315 return;
2316 }
2317
2318 BT_INFO("%s: %s revision %u.%u build %u week %u %u", hdev->name,
2319 variant, ver->fw_revision >> 4, ver->fw_revision & 0x0f,
2320 ver->fw_build_num, ver->fw_build_ww, 2000 + ver->fw_build_yy);
2321}
2322
2323static int btusb_setup_intel_new(struct hci_dev *hdev)
2324{
2325 static const u8 reset_param[] = { 0x00, 0x01, 0x00, 0x01,
2326 0x00, 0x08, 0x04, 0x00 };
2327 struct btusb_data *data = hci_get_drvdata(hdev);
2328 struct sk_buff *skb;
2329 struct intel_version *ver;
2330 struct intel_boot_params *params;
2331 const struct firmware *fw;
2332 const u8 *fw_ptr;
2333 char fwname[64];
2334 ktime_t calltime, delta, rettime;
2335 unsigned long long duration;
2336 int err;
2337
2338 BT_DBG("%s", hdev->name);
2339
2340 calltime = ktime_get();
2341
2342 /* Read the Intel version information to determine if the device
2343 * is in bootloader mode or if it already has operational firmware
2344 * loaded.
2345 */
2346 skb = __hci_cmd_sync(hdev, 0xfc05, 0, NULL, HCI_INIT_TIMEOUT);
2347 if (IS_ERR(skb)) {
2348 BT_ERR("%s: Reading Intel version information failed (%ld)",
2349 hdev->name, PTR_ERR(skb));
2350 return PTR_ERR(skb);
2351 }
2352
2353 if (skb->len != sizeof(*ver)) {
2354 BT_ERR("%s: Intel version event size mismatch", hdev->name);
2355 kfree_skb(skb);
2356 return -EILSEQ;
2357 }
2358
2359 ver = (struct intel_version *)skb->data;
2360 if (ver->status) {
2361 BT_ERR("%s: Intel version command failure (%02x)",
2362 hdev->name, ver->status);
2363 err = -bt_to_errno(ver->status);
2364 kfree_skb(skb);
2365 return err;
2366 }
2367
2368 /* The hardware platform number has a fixed value of 0x37 and
2369 * for now only accept this single value.
2370 */
2371 if (ver->hw_platform != 0x37) {
2372 BT_ERR("%s: Unsupported Intel hardware platform (%u)",
2373 hdev->name, ver->hw_platform);
2374 kfree_skb(skb);
2375 return -EINVAL;
2376 }
2377
2378 /* At the moment only the hardware variant iBT 3.0 (LnP/SfP) is
2379 * supported by this firmware loading method. This check has been
2380 * put in place to ensure correct forward compatibility options
2381 * when newer hardware variants come along.
2382 */
2383 if (ver->hw_variant != 0x0b) {
2384 BT_ERR("%s: Unsupported Intel hardware variant (%u)",
2385 hdev->name, ver->hw_variant);
2386 kfree_skb(skb);
2387 return -EINVAL;
2388 }
2389
2390 btusb_intel_version_info(hdev, ver);
2391
2392 /* The firmware variant determines if the device is in bootloader
2393 * mode or is running operational firmware. The value 0x06 identifies
2394 * the bootloader and the value 0x23 identifies the operational
2395 * firmware.
2396 *
2397 * When the operational firmware is already present, then only
2398 * the check for valid Bluetooth device address is needed. This
2399 * determines if the device will be added as configured or
2400 * unconfigured controller.
2401 *
2402 * It is not possible to use the Secure Boot Parameters in this
2403 * case since that command is only available in bootloader mode.
2404 */
2405 if (ver->fw_variant == 0x23) {
2406 kfree_skb(skb);
2407 clear_bit(BTUSB_BOOTLOADER, &data->flags);
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07002408 btintel_check_bdaddr(hdev);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002409 return 0;
2410 }
2411
2412 /* If the device is not in bootloader mode, then the only possible
2413 * choice is to return an error and abort the device initialization.
2414 */
2415 if (ver->fw_variant != 0x06) {
2416 BT_ERR("%s: Unsupported Intel firmware variant (%u)",
2417 hdev->name, ver->fw_variant);
2418 kfree_skb(skb);
2419 return -ENODEV;
2420 }
2421
2422 kfree_skb(skb);
2423
2424 /* Read the secure boot parameters to identify the operating
2425 * details of the bootloader.
2426 */
2427 skb = __hci_cmd_sync(hdev, 0xfc0d, 0, NULL, HCI_INIT_TIMEOUT);
2428 if (IS_ERR(skb)) {
2429 BT_ERR("%s: Reading Intel boot parameters failed (%ld)",
2430 hdev->name, PTR_ERR(skb));
2431 return PTR_ERR(skb);
2432 }
2433
2434 if (skb->len != sizeof(*params)) {
2435 BT_ERR("%s: Intel boot parameters size mismatch", hdev->name);
2436 kfree_skb(skb);
2437 return -EILSEQ;
2438 }
2439
2440 params = (struct intel_boot_params *)skb->data;
2441 if (params->status) {
2442 BT_ERR("%s: Intel boot parameters command failure (%02x)",
2443 hdev->name, params->status);
2444 err = -bt_to_errno(params->status);
2445 kfree_skb(skb);
2446 return err;
2447 }
2448
2449 BT_INFO("%s: Device revision is %u", hdev->name,
2450 le16_to_cpu(params->dev_revid));
2451
2452 BT_INFO("%s: Secure boot is %s", hdev->name,
2453 params->secure_boot ? "enabled" : "disabled");
2454
2455 BT_INFO("%s: Minimum firmware build %u week %u %u", hdev->name,
2456 params->min_fw_build_nn, params->min_fw_build_cw,
2457 2000 + params->min_fw_build_yy);
2458
2459 /* It is required that every single firmware fragment is acknowledged
2460 * with a command complete event. If the boot parameters indicate
2461 * that this bootloader does not send them, then abort the setup.
2462 */
2463 if (params->limited_cce != 0x00) {
2464 BT_ERR("%s: Unsupported Intel firmware loading method (%u)",
2465 hdev->name, params->limited_cce);
2466 kfree_skb(skb);
2467 return -EINVAL;
2468 }
2469
2470 /* If the OTP has no valid Bluetooth device address, then there will
2471 * also be no valid address for the operational firmware.
2472 */
2473 if (!bacmp(&params->otp_bdaddr, BDADDR_ANY)) {
2474 BT_INFO("%s: No device address configured", hdev->name);
2475 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
2476 }
2477
2478 /* With this Intel bootloader only the hardware variant and device
2479 * revision information are used to select the right firmware.
2480 *
2481 * Currently this bootloader support is limited to hardware variant
2482 * iBT 3.0 (LnP/SfP) which is identified by the value 11 (0x0b).
2483 */
2484 snprintf(fwname, sizeof(fwname), "intel/ibt-11-%u.sfi",
2485 le16_to_cpu(params->dev_revid));
2486
2487 err = request_firmware(&fw, fwname, &hdev->dev);
2488 if (err < 0) {
2489 BT_ERR("%s: Failed to load Intel firmware file (%d)",
2490 hdev->name, err);
2491 kfree_skb(skb);
2492 return err;
2493 }
2494
2495 BT_INFO("%s: Found device firmware: %s", hdev->name, fwname);
2496
2497 kfree_skb(skb);
2498
2499 if (fw->size < 644) {
2500 BT_ERR("%s: Invalid size of firmware file (%zu)",
2501 hdev->name, fw->size);
2502 err = -EBADF;
2503 goto done;
2504 }
2505
2506 set_bit(BTUSB_DOWNLOADING, &data->flags);
2507
2508 /* Start the firmware download transaction with the Init fragment
2509 * represented by the 128 bytes of CSS header.
2510 */
2511 err = btusb_intel_secure_send(hdev, 0x00, 128, fw->data);
2512 if (err < 0) {
2513 BT_ERR("%s: Failed to send firmware header (%d)",
2514 hdev->name, err);
2515 goto done;
2516 }
2517
2518 /* Send the 256 bytes of public key information from the firmware
2519 * as the PKey fragment.
2520 */
2521 err = btusb_intel_secure_send(hdev, 0x03, 256, fw->data + 128);
2522 if (err < 0) {
2523 BT_ERR("%s: Failed to send firmware public key (%d)",
2524 hdev->name, err);
2525 goto done;
2526 }
2527
2528 /* Send the 256 bytes of signature information from the firmware
2529 * as the Sign fragment.
2530 */
2531 err = btusb_intel_secure_send(hdev, 0x02, 256, fw->data + 388);
2532 if (err < 0) {
2533 BT_ERR("%s: Failed to send firmware signature (%d)",
2534 hdev->name, err);
2535 goto done;
2536 }
2537
2538 fw_ptr = fw->data + 644;
2539
2540 while (fw_ptr - fw->data < fw->size) {
2541 struct hci_command_hdr *cmd = (void *)fw_ptr;
2542 u8 cmd_len;
2543
2544 cmd_len = sizeof(*cmd) + cmd->plen;
2545
2546 /* Send each command from the firmware data buffer as
2547 * a single Data fragment.
2548 */
2549 err = btusb_intel_secure_send(hdev, 0x01, cmd_len, fw_ptr);
2550 if (err < 0) {
2551 BT_ERR("%s: Failed to send firmware data (%d)",
2552 hdev->name, err);
2553 goto done;
2554 }
2555
2556 fw_ptr += cmd_len;
2557 }
2558
Marcel Holtmannce6bb922015-01-28 01:58:40 -08002559 set_bit(BTUSB_FIRMWARE_LOADED, &data->flags);
2560
Johan Hedberga087a982015-01-30 10:58:54 +02002561 BT_INFO("%s: Waiting for firmware download to complete", hdev->name);
2562
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002563 /* Before switching the device into operational mode and with that
2564 * booting the loaded firmware, wait for the bootloader notification
2565 * that all fragments have been successfully received.
2566 *
Johan Hedberga087a982015-01-30 10:58:54 +02002567 * When the event processing receives the notification, then the
2568 * BTUSB_DOWNLOADING flag will be cleared.
2569 *
2570 * The firmware loading should not take longer than 5 seconds
2571 * and thus just timeout if that happens and fail the setup
2572 * of this device.
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002573 */
Johan Hedberg129a7692015-02-14 09:33:35 +02002574 err = wait_on_bit_timeout(&data->flags, BTUSB_DOWNLOADING,
2575 TASK_INTERRUPTIBLE,
2576 msecs_to_jiffies(5000));
Johan Hedberga087a982015-01-30 10:58:54 +02002577 if (err == 1) {
2578 BT_ERR("%s: Firmware loading interrupted", hdev->name);
2579 err = -EINTR;
2580 goto done;
2581 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002582
Johan Hedberga087a982015-01-30 10:58:54 +02002583 if (err) {
2584 BT_ERR("%s: Firmware loading timeout", hdev->name);
2585 err = -ETIMEDOUT;
2586 goto done;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002587 }
2588
2589 if (test_bit(BTUSB_FIRMWARE_FAILED, &data->flags)) {
2590 BT_ERR("%s: Firmware loading failed", hdev->name);
2591 err = -ENOEXEC;
2592 goto done;
2593 }
2594
2595 rettime = ktime_get();
2596 delta = ktime_sub(rettime, calltime);
2597 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
2598
2599 BT_INFO("%s: Firmware loaded in %llu usecs", hdev->name, duration);
2600
2601done:
2602 release_firmware(fw);
2603
2604 if (err < 0)
2605 return err;
2606
2607 calltime = ktime_get();
2608
2609 set_bit(BTUSB_BOOTING, &data->flags);
2610
2611 skb = __hci_cmd_sync(hdev, 0xfc01, sizeof(reset_param), reset_param,
2612 HCI_INIT_TIMEOUT);
2613 if (IS_ERR(skb))
2614 return PTR_ERR(skb);
2615
2616 kfree_skb(skb);
2617
2618 /* The bootloader will not indicate when the device is ready. This
2619 * is done by the operational firmware sending bootup notification.
Johan Hedbergfad70972015-01-30 10:58:55 +02002620 *
2621 * Booting into operational firmware should not take longer than
2622 * 1 second. However if that happens, then just fail the setup
2623 * since something went wrong.
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002624 */
Johan Hedbergfad70972015-01-30 10:58:55 +02002625 BT_INFO("%s: Waiting for device to boot", hdev->name);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002626
Johan Hedberg129a7692015-02-14 09:33:35 +02002627 err = wait_on_bit_timeout(&data->flags, BTUSB_BOOTING,
2628 TASK_INTERRUPTIBLE,
2629 msecs_to_jiffies(1000));
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002630
Johan Hedbergfad70972015-01-30 10:58:55 +02002631 if (err == 1) {
2632 BT_ERR("%s: Device boot interrupted", hdev->name);
2633 return -EINTR;
2634 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002635
Johan Hedbergfad70972015-01-30 10:58:55 +02002636 if (err) {
2637 BT_ERR("%s: Device boot timeout", hdev->name);
2638 return -ETIMEDOUT;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002639 }
2640
2641 rettime = ktime_get();
2642 delta = ktime_sub(rettime, calltime);
2643 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
2644
2645 BT_INFO("%s: Device booted in %llu usecs", hdev->name, duration);
2646
2647 clear_bit(BTUSB_BOOTLOADER, &data->flags);
2648
2649 return 0;
2650}
2651
Marcel Holtmann385a7682015-01-28 11:09:56 -08002652static void btusb_hw_error_intel(struct hci_dev *hdev, u8 code)
2653{
2654 struct sk_buff *skb;
2655 u8 type = 0x00;
2656
2657 BT_ERR("%s: Hardware error 0x%2.2x", hdev->name, code);
2658
2659 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
2660 if (IS_ERR(skb)) {
2661 BT_ERR("%s: Reset after hardware error failed (%ld)",
2662 hdev->name, PTR_ERR(skb));
2663 return;
2664 }
2665 kfree_skb(skb);
2666
2667 skb = __hci_cmd_sync(hdev, 0xfc22, 1, &type, HCI_INIT_TIMEOUT);
2668 if (IS_ERR(skb)) {
2669 BT_ERR("%s: Retrieving Intel exception info failed (%ld)",
2670 hdev->name, PTR_ERR(skb));
2671 return;
2672 }
2673
2674 if (skb->len != 13) {
2675 BT_ERR("%s: Exception info size mismatch", hdev->name);
2676 kfree_skb(skb);
2677 return;
2678 }
2679
2680 if (skb->data[0] != 0x00) {
2681 BT_ERR("%s: Exception info command failure (%02x)",
2682 hdev->name, skb->data[0]);
2683 kfree_skb(skb);
2684 return;
2685 }
2686
2687 BT_ERR("%s: Exception info %s", hdev->name, (char *)(skb->data + 1));
2688
2689 kfree_skb(skb);
2690}
2691
Tedd Ho-Jeong Anbfbd45e2015-02-13 09:20:52 -08002692static int btusb_shutdown_intel(struct hci_dev *hdev)
2693{
2694 struct sk_buff *skb;
2695 long ret;
2696
2697 /* Some platforms have an issue with BT LED when the interface is
2698 * down or BT radio is turned off, which takes 5 seconds to BT LED
2699 * goes off. This command turns off the BT LED immediately.
2700 */
2701 skb = __hci_cmd_sync(hdev, 0xfc3f, 0, NULL, HCI_INIT_TIMEOUT);
2702 if (IS_ERR(skb)) {
2703 ret = PTR_ERR(skb);
2704 BT_ERR("%s: turning off Intel device LED failed (%ld)",
2705 hdev->name, ret);
2706 return ret;
2707 }
2708 kfree_skb(skb);
2709
2710 return 0;
2711}
2712
Amitkumar Karwarae8df492014-07-18 14:47:06 -07002713static int btusb_set_bdaddr_marvell(struct hci_dev *hdev,
2714 const bdaddr_t *bdaddr)
2715{
2716 struct sk_buff *skb;
2717 u8 buf[8];
2718 long ret;
2719
2720 buf[0] = 0xfe;
2721 buf[1] = sizeof(bdaddr_t);
2722 memcpy(buf + 2, bdaddr, sizeof(bdaddr_t));
2723
2724 skb = __hci_cmd_sync(hdev, 0xfc22, sizeof(buf), buf, HCI_INIT_TIMEOUT);
2725 if (IS_ERR(skb)) {
2726 ret = PTR_ERR(skb);
2727 BT_ERR("%s: changing Marvell device address failed (%ld)",
2728 hdev->name, ret);
2729 return ret;
2730 }
2731 kfree_skb(skb);
2732
2733 return 0;
2734}
2735
Toshi Kikuchi58592232014-12-12 10:58:05 -08002736static int btusb_set_bdaddr_ath3012(struct hci_dev *hdev,
2737 const bdaddr_t *bdaddr)
2738{
2739 struct sk_buff *skb;
2740 u8 buf[10];
2741 long ret;
2742
2743 buf[0] = 0x01;
2744 buf[1] = 0x01;
2745 buf[2] = 0x00;
2746 buf[3] = sizeof(bdaddr_t);
2747 memcpy(buf + 4, bdaddr, sizeof(bdaddr_t));
2748
2749 skb = __hci_cmd_sync(hdev, 0xfc0b, sizeof(buf), buf, HCI_INIT_TIMEOUT);
2750 if (IS_ERR(skb)) {
2751 ret = PTR_ERR(skb);
2752 BT_ERR("%s: Change address command failed (%ld)",
2753 hdev->name, ret);
2754 return ret;
2755 }
2756 kfree_skb(skb);
2757
2758 return 0;
2759}
2760
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002761#define QCA_DFU_PACKET_LEN 4096
2762
2763#define QCA_GET_TARGET_VERSION 0x09
2764#define QCA_CHECK_STATUS 0x05
2765#define QCA_DFU_DOWNLOAD 0x01
2766
2767#define QCA_SYSCFG_UPDATED 0x40
2768#define QCA_PATCH_UPDATED 0x80
2769#define QCA_DFU_TIMEOUT 3000
2770
2771struct qca_version {
2772 __le32 rom_version;
2773 __le32 patch_version;
2774 __le32 ram_version;
2775 __le32 ref_clock;
2776 __u8 reserved[4];
2777} __packed;
2778
2779struct qca_rampatch_version {
2780 __le16 rom_version;
2781 __le16 patch_version;
2782} __packed;
2783
2784struct qca_device_info {
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002785 u32 rom_version;
2786 u8 rampatch_hdr; /* length of header in rampatch */
2787 u8 nvm_hdr; /* length of header in NVM */
2788 u8 ver_offset; /* offset of version structure in rampatch */
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002789};
2790
2791static const struct qca_device_info qca_devices_table[] = {
2792 { 0x00000100, 20, 4, 10 }, /* Rome 1.0 */
2793 { 0x00000101, 20, 4, 10 }, /* Rome 1.1 */
2794 { 0x00000201, 28, 4, 18 }, /* Rome 2.1 */
2795 { 0x00000300, 28, 4, 18 }, /* Rome 3.0 */
2796 { 0x00000302, 28, 4, 18 }, /* Rome 3.2 */
2797};
2798
2799static int btusb_qca_send_vendor_req(struct hci_dev *hdev, u8 request,
2800 void *data, u16 size)
2801{
2802 struct btusb_data *btdata = hci_get_drvdata(hdev);
2803 struct usb_device *udev = btdata->udev;
2804 int pipe, err;
2805 u8 *buf;
2806
2807 buf = kmalloc(size, GFP_KERNEL);
2808 if (!buf)
2809 return -ENOMEM;
2810
2811 /* Found some of USB hosts have IOT issues with ours so that we should
2812 * not wait until HCI layer is ready.
2813 */
2814 pipe = usb_rcvctrlpipe(udev, 0);
2815 err = usb_control_msg(udev, pipe, request, USB_TYPE_VENDOR | USB_DIR_IN,
2816 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
2817 if (err < 0) {
2818 BT_ERR("%s: Failed to access otp area (%d)", hdev->name, err);
2819 goto done;
2820 }
2821
2822 memcpy(data, buf, size);
2823
2824done:
2825 kfree(buf);
2826
2827 return err;
2828}
2829
2830static int btusb_setup_qca_download_fw(struct hci_dev *hdev,
2831 const struct firmware *firmware,
2832 size_t hdr_size)
2833{
2834 struct btusb_data *btdata = hci_get_drvdata(hdev);
2835 struct usb_device *udev = btdata->udev;
2836 size_t count, size, sent = 0;
2837 int pipe, len, err;
2838 u8 *buf;
2839
2840 buf = kmalloc(QCA_DFU_PACKET_LEN, GFP_KERNEL);
2841 if (!buf)
2842 return -ENOMEM;
2843
2844 count = firmware->size;
2845
2846 size = min_t(size_t, count, hdr_size);
2847 memcpy(buf, firmware->data, size);
2848
2849 /* USB patches should go down to controller through USB path
2850 * because binary format fits to go down through USB channel.
2851 * USB control path is for patching headers and USB bulk is for
2852 * patch body.
2853 */
2854 pipe = usb_sndctrlpipe(udev, 0);
2855 err = usb_control_msg(udev, pipe, QCA_DFU_DOWNLOAD, USB_TYPE_VENDOR,
2856 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
2857 if (err < 0) {
2858 BT_ERR("%s: Failed to send headers (%d)", hdev->name, err);
2859 goto done;
2860 }
2861
2862 sent += size;
2863 count -= size;
2864
2865 while (count) {
2866 size = min_t(size_t, count, QCA_DFU_PACKET_LEN);
2867
2868 memcpy(buf, firmware->data + sent, size);
2869
2870 pipe = usb_sndbulkpipe(udev, 0x02);
2871 err = usb_bulk_msg(udev, pipe, buf, size, &len,
2872 QCA_DFU_TIMEOUT);
2873 if (err < 0) {
2874 BT_ERR("%s: Failed to send body at %zd of %zd (%d)",
2875 hdev->name, sent, firmware->size, err);
2876 break;
2877 }
2878
2879 if (size != len) {
2880 BT_ERR("%s: Failed to get bulk buffer", hdev->name);
2881 err = -EILSEQ;
2882 break;
2883 }
2884
2885 sent += size;
2886 count -= size;
2887 }
2888
2889done:
2890 kfree(buf);
2891 return err;
2892}
2893
2894static int btusb_setup_qca_load_rampatch(struct hci_dev *hdev,
2895 struct qca_version *ver,
2896 const struct qca_device_info *info)
2897{
2898 struct qca_rampatch_version *rver;
2899 const struct firmware *fw;
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002900 u32 ver_rom, ver_patch;
2901 u16 rver_rom, rver_patch;
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002902 char fwname[64];
2903 int err;
2904
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002905 ver_rom = le32_to_cpu(ver->rom_version);
2906 ver_patch = le32_to_cpu(ver->patch_version);
2907
2908 snprintf(fwname, sizeof(fwname), "qca/rampatch_usb_%08x.bin", ver_rom);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002909
2910 err = request_firmware(&fw, fwname, &hdev->dev);
2911 if (err) {
2912 BT_ERR("%s: failed to request rampatch file: %s (%d)",
2913 hdev->name, fwname, err);
2914 return err;
2915 }
2916
2917 BT_INFO("%s: using rampatch file: %s", hdev->name, fwname);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002918
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002919 rver = (struct qca_rampatch_version *)(fw->data + info->ver_offset);
2920 rver_rom = le16_to_cpu(rver->rom_version);
2921 rver_patch = le16_to_cpu(rver->patch_version);
2922
2923 BT_INFO("%s: QCA: patch rome 0x%x build 0x%x, firmware rome 0x%x "
2924 "build 0x%x", hdev->name, rver_rom, rver_patch, ver_rom,
2925 ver_patch);
2926
2927 if (rver_rom != ver_rom || rver_patch <= ver_patch) {
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002928 BT_ERR("%s: rampatch file version did not match with firmware",
2929 hdev->name);
2930 err = -EINVAL;
2931 goto done;
2932 }
2933
2934 err = btusb_setup_qca_download_fw(hdev, fw, info->rampatch_hdr);
2935
2936done:
2937 release_firmware(fw);
2938
2939 return err;
2940}
2941
2942static int btusb_setup_qca_load_nvm(struct hci_dev *hdev,
2943 struct qca_version *ver,
2944 const struct qca_device_info *info)
2945{
2946 const struct firmware *fw;
2947 char fwname[64];
2948 int err;
2949
2950 snprintf(fwname, sizeof(fwname), "qca/nvm_usb_%08x.bin",
2951 le32_to_cpu(ver->rom_version));
2952
2953 err = request_firmware(&fw, fwname, &hdev->dev);
2954 if (err) {
2955 BT_ERR("%s: failed to request NVM file: %s (%d)",
2956 hdev->name, fwname, err);
2957 return err;
2958 }
2959
2960 BT_INFO("%s: using NVM file: %s", hdev->name, fwname);
2961
2962 err = btusb_setup_qca_download_fw(hdev, fw, info->nvm_hdr);
2963
2964 release_firmware(fw);
2965
2966 return err;
2967}
2968
2969static int btusb_setup_qca(struct hci_dev *hdev)
2970{
2971 const struct qca_device_info *info = NULL;
2972 struct qca_version ver;
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002973 u32 ver_rom;
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002974 u8 status;
2975 int i, err;
2976
2977 err = btusb_qca_send_vendor_req(hdev, QCA_GET_TARGET_VERSION, &ver,
Marcel Holtmanneb500422015-04-16 23:15:50 +02002978 sizeof(ver));
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002979 if (err < 0)
2980 return err;
2981
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002982 ver_rom = le32_to_cpu(ver.rom_version);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002983 for (i = 0; i < ARRAY_SIZE(qca_devices_table); i++) {
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002984 if (ver_rom == qca_devices_table[i].rom_version)
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002985 info = &qca_devices_table[i];
2986 }
2987 if (!info) {
2988 BT_ERR("%s: don't support firmware rome 0x%x", hdev->name,
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002989 ver_rom);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002990 return -ENODEV;
2991 }
2992
2993 err = btusb_qca_send_vendor_req(hdev, QCA_CHECK_STATUS, &status,
2994 sizeof(status));
2995 if (err < 0)
2996 return err;
2997
2998 if (!(status & QCA_PATCH_UPDATED)) {
2999 err = btusb_setup_qca_load_rampatch(hdev, &ver, info);
3000 if (err < 0)
3001 return err;
3002 }
3003
3004 if (!(status & QCA_SYSCFG_UPDATED)) {
3005 err = btusb_setup_qca_load_nvm(hdev, &ver, info);
3006 if (err < 0)
3007 return err;
3008 }
3009
3010 return 0;
3011}
3012
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003013static int btusb_probe(struct usb_interface *intf,
Marcel Holtmann89e75332014-09-16 04:44:50 +02003014 const struct usb_device_id *id)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003015{
3016 struct usb_endpoint_descriptor *ep_desc;
3017 struct btusb_data *data;
3018 struct hci_dev *hdev;
3019 int i, err;
3020
3021 BT_DBG("intf %p id %p", intf, id);
3022
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02003023 /* interface numbers are hardcoded in the spec */
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003024 if (intf->cur_altsetting->desc.bInterfaceNumber != 0)
3025 return -ENODEV;
3026
3027 if (!id->driver_info) {
3028 const struct usb_device_id *match;
Marcel Holtmann89e75332014-09-16 04:44:50 +02003029
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003030 match = usb_match_id(intf, blacklist_table);
3031 if (match)
3032 id = match;
3033 }
3034
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02003035 if (id->driver_info == BTUSB_IGNORE)
3036 return -ENODEV;
3037
Steven.Li2d25f8b2011-07-01 14:02:36 +08003038 if (id->driver_info & BTUSB_ATH3012) {
3039 struct usb_device *udev = interface_to_usbdev(intf);
3040
3041 /* Old firmware would otherwise let ath3k driver load
3042 * patch and sysconfig files */
3043 if (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x0001)
3044 return -ENODEV;
3045 }
3046
Sachin Kamat98921db2012-07-27 12:38:39 +05303047 data = devm_kzalloc(&intf->dev, sizeof(*data), GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003048 if (!data)
3049 return -ENOMEM;
3050
3051 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
3052 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
3053
3054 if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
3055 data->intr_ep = ep_desc;
3056 continue;
3057 }
3058
3059 if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
3060 data->bulk_tx_ep = ep_desc;
3061 continue;
3062 }
3063
3064 if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
3065 data->bulk_rx_ep = ep_desc;
3066 continue;
3067 }
3068 }
3069
Sachin Kamat98921db2012-07-27 12:38:39 +05303070 if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003071 return -ENODEV;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003072
Marcel Holtmann893ba542015-01-28 20:27:34 -08003073 if (id->driver_info & BTUSB_AMP) {
3074 data->cmdreq_type = USB_TYPE_CLASS | 0x01;
3075 data->cmdreq = 0x2b;
3076 } else {
3077 data->cmdreq_type = USB_TYPE_CLASS;
3078 data->cmdreq = 0x00;
3079 }
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01003080
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003081 data->udev = interface_to_usbdev(intf);
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02003082 data->intf = intf;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003083
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003084 INIT_WORK(&data->work, btusb_work);
Oliver Neukum7bee5492009-08-24 23:44:59 +02003085 INIT_WORK(&data->waker, btusb_waker);
Marcel Holtmann803b5832014-09-16 08:00:29 +02003086 init_usb_anchor(&data->deferred);
3087 init_usb_anchor(&data->tx_anchor);
Oliver Neukum7bee5492009-08-24 23:44:59 +02003088 spin_lock_init(&data->txlock);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003089
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003090 init_usb_anchor(&data->intr_anchor);
3091 init_usb_anchor(&data->bulk_anchor);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02003092 init_usb_anchor(&data->isoc_anchor);
Marcel Holtmann803b5832014-09-16 08:00:29 +02003093 spin_lock_init(&data->rxlock);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003094
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08003095 if (id->driver_info & BTUSB_INTEL_NEW) {
3096 data->recv_event = btusb_recv_event_intel;
3097 data->recv_bulk = btusb_recv_bulk_intel;
3098 set_bit(BTUSB_BOOTLOADER, &data->flags);
3099 } else {
3100 data->recv_event = hci_recv_frame;
3101 data->recv_bulk = btusb_recv_bulk;
3102 }
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +01003103
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003104 hdev = hci_alloc_dev();
Sachin Kamat98921db2012-07-27 12:38:39 +05303105 if (!hdev)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003106 return -ENOMEM;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003107
Marcel Holtmannc13854c2010-02-08 15:27:07 +01003108 hdev->bus = HCI_USB;
David Herrmann155961e2012-02-09 21:58:32 +01003109 hci_set_drvdata(hdev, data);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003110
Marcel Holtmann893ba542015-01-28 20:27:34 -08003111 if (id->driver_info & BTUSB_AMP)
3112 hdev->dev_type = HCI_AMP;
3113 else
3114 hdev->dev_type = HCI_BREDR;
3115
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003116 data->hdev = hdev;
3117
3118 SET_HCIDEV_DEV(hdev, &intf->dev);
3119
Marcel Holtmann9f8f9622013-04-10 08:11:35 -07003120 hdev->open = btusb_open;
3121 hdev->close = btusb_close;
3122 hdev->flush = btusb_flush;
3123 hdev->send = btusb_send_frame;
3124 hdev->notify = btusb_notify;
3125
3126 if (id->driver_info & BTUSB_BCM92035)
3127 hdev->setup = btusb_setup_bcm92035;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003128
Marcel Holtmannc2bfb102015-04-05 22:52:14 -07003129#ifdef CONFIG_BT_HCIBTUSB_BCM
Marcel Holtmannabbaf502014-07-02 00:53:48 +02003130 if (id->driver_info & BTUSB_BCM_PATCHRAM) {
Marcel Holtmannc2bfb102015-04-05 22:52:14 -07003131 hdev->setup = btbcm_setup_patchram;
Marcel Holtmann1df1f592015-04-05 22:52:11 -07003132 hdev->set_bdaddr = btbcm_set_bdaddr;
Marcel Holtmannabbaf502014-07-02 00:53:48 +02003133 }
Petri Gynther10d4c672014-05-08 15:50:01 -07003134
Marcel Holtmann941521e2015-04-05 22:52:15 -07003135 if (id->driver_info & BTUSB_BCM_APPLE)
Marcel Holtmannc2bfb102015-04-05 22:52:14 -07003136 hdev->setup = btbcm_setup_apple;
Marcel Holtmannc2bfb102015-04-05 22:52:14 -07003137#endif
Marcel Holtmann17b27722015-03-22 15:52:38 +01003138
Marcel Holtmanncb8d6592014-07-02 11:25:25 +02003139 if (id->driver_info & BTUSB_INTEL) {
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07003140 hdev->setup = btusb_setup_intel;
Tedd Ho-Jeong Anbfbd45e2015-02-13 09:20:52 -08003141 hdev->shutdown = btusb_shutdown_intel;
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07003142 hdev->set_bdaddr = btintel_set_bdaddr;
Jakub Pawlowskic33fb9b2015-01-30 18:55:58 -08003143 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
Jakub Pawlowskic1154842015-03-17 09:04:16 -07003144 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
Marcel Holtmanncb8d6592014-07-02 11:25:25 +02003145 }
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07003146
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08003147 if (id->driver_info & BTUSB_INTEL_NEW) {
3148 hdev->send = btusb_send_frame_intel;
3149 hdev->setup = btusb_setup_intel_new;
Marcel Holtmann385a7682015-01-28 11:09:56 -08003150 hdev->hw_error = btusb_hw_error_intel;
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07003151 hdev->set_bdaddr = btintel_set_bdaddr;
Marcel Holtmannb970c5b2015-02-01 23:57:18 -08003152 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08003153 }
3154
Amitkumar Karwarae8df492014-07-18 14:47:06 -07003155 if (id->driver_info & BTUSB_MARVELL)
3156 hdev->set_bdaddr = btusb_set_bdaddr_marvell;
3157
Marcel Holtmann661cf882015-01-02 23:35:20 -08003158 if (id->driver_info & BTUSB_SWAVE) {
3159 set_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks);
Marcel Holtmannd57dbe72014-12-26 04:42:33 +01003160 set_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS, &hdev->quirks);
Marcel Holtmann661cf882015-01-02 23:35:20 -08003161 }
Marcel Holtmannd57dbe72014-12-26 04:42:33 +01003162
Marcel Holtmann40df7832014-07-06 13:29:58 +02003163 if (id->driver_info & BTUSB_INTEL_BOOT)
3164 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
3165
Jakub Pawlowski79f0c872015-01-29 10:38:34 -08003166 if (id->driver_info & BTUSB_ATH3012) {
Toshi Kikuchi58592232014-12-12 10:58:05 -08003167 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
Jakub Pawlowski3d50d512015-03-17 09:04:15 -07003168 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
Jakub Pawlowski79f0c872015-01-29 10:38:34 -08003169 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
3170 }
Toshi Kikuchi58592232014-12-12 10:58:05 -08003171
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003172 if (id->driver_info & BTUSB_QCA_ROME) {
3173 data->setup_on_usb = btusb_setup_qca;
3174 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
3175 }
3176
Daniel Drakea2698a92015-04-16 14:09:55 -06003177 if (id->driver_info & BTUSB_REALTEK)
3178 hdev->setup = btusb_setup_realtek;
3179
Marcel Holtmann893ba542015-01-28 20:27:34 -08003180 if (id->driver_info & BTUSB_AMP) {
3181 /* AMP controllers do not support SCO packets */
3182 data->isoc = NULL;
3183 } else {
3184 /* Interface numbers are hardcoded in the specification */
3185 data->isoc = usb_ifnum_to_if(data->udev, 1);
3186 }
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02003187
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01003188 if (!reset)
Szymon Janca6c511c2012-05-23 12:35:46 +02003189 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02003190
3191 if (force_scofix || id->driver_info & BTUSB_WRONG_SCO_MTU) {
3192 if (!disable_scofix)
3193 set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
3194 }
3195
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02003196 if (id->driver_info & BTUSB_BROKEN_ISOC)
3197 data->isoc = NULL;
3198
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01003199 if (id->driver_info & BTUSB_DIGIANSWER) {
3200 data->cmdreq_type = USB_TYPE_VENDOR;
Szymon Janca6c511c2012-05-23 12:35:46 +02003201 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01003202 }
3203
3204 if (id->driver_info & BTUSB_CSR) {
3205 struct usb_device *udev = data->udev;
Marcel Holtmann81cac642014-01-03 03:02:36 -08003206 u16 bcdDevice = le16_to_cpu(udev->descriptor.bcdDevice);
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01003207
3208 /* Old firmware would otherwise execute USB reset */
Marcel Holtmann81cac642014-01-03 03:02:36 -08003209 if (bcdDevice < 0x117)
Szymon Janca6c511c2012-05-23 12:35:46 +02003210 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmann81cac642014-01-03 03:02:36 -08003211
3212 /* Fake CSR devices with broken commands */
3213 if (bcdDevice <= 0x100)
3214 hdev->setup = btusb_setup_csr;
Jakub Pawlowski49c989a2015-03-17 09:04:17 -07003215
3216 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01003217 }
3218
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02003219 if (id->driver_info & BTUSB_SNIFFER) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02003220 struct usb_device *udev = data->udev;
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02003221
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01003222 /* New sniffer firmware has crippled HCI interface */
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02003223 if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
3224 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
3225 }
3226
Marcel Holtmann3a5ef202014-07-06 14:53:54 +02003227 if (id->driver_info & BTUSB_INTEL_BOOT) {
3228 /* A bug in the bootloader causes that interrupt interface is
3229 * only enabled after receiving SetInterface(0, AltSetting=0).
3230 */
3231 err = usb_set_interface(data->udev, 0, 0);
3232 if (err < 0) {
3233 BT_ERR("failed to set interface 0, alt 0 %d", err);
3234 hci_free_dev(hdev);
3235 return err;
3236 }
3237 }
3238
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02003239 if (data->isoc) {
3240 err = usb_driver_claim_interface(&btusb_driver,
Marcel Holtmann89e75332014-09-16 04:44:50 +02003241 data->isoc, data);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02003242 if (err < 0) {
3243 hci_free_dev(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02003244 return err;
3245 }
3246 }
3247
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003248 err = hci_register_dev(hdev);
3249 if (err < 0) {
3250 hci_free_dev(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003251 return err;
3252 }
3253
3254 usb_set_intfdata(intf, data);
3255
3256 return 0;
3257}
3258
3259static void btusb_disconnect(struct usb_interface *intf)
3260{
3261 struct btusb_data *data = usb_get_intfdata(intf);
3262 struct hci_dev *hdev;
3263
3264 BT_DBG("intf %p", intf);
3265
3266 if (!data)
3267 return;
3268
3269 hdev = data->hdev;
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02003270 usb_set_intfdata(data->intf, NULL);
3271
3272 if (data->isoc)
3273 usb_set_intfdata(data->isoc, NULL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003274
3275 hci_unregister_dev(hdev);
3276
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02003277 if (intf == data->isoc)
3278 usb_driver_release_interface(&btusb_driver, data->intf);
3279 else if (data->isoc)
3280 usb_driver_release_interface(&btusb_driver, data->isoc);
3281
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003282 hci_free_dev(hdev);
3283}
3284
Oliver Neukum7bee5492009-08-24 23:44:59 +02003285#ifdef CONFIG_PM
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003286static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
3287{
3288 struct btusb_data *data = usb_get_intfdata(intf);
3289
3290 BT_DBG("intf %p", intf);
3291
3292 if (data->suspend_count++)
3293 return 0;
3294
Oliver Neukum7bee5492009-08-24 23:44:59 +02003295 spin_lock_irq(&data->txlock);
Alan Stern5b1b0b82011-08-19 23:49:48 +02003296 if (!(PMSG_IS_AUTO(message) && data->tx_in_flight)) {
Oliver Neukum7bee5492009-08-24 23:44:59 +02003297 set_bit(BTUSB_SUSPENDING, &data->flags);
3298 spin_unlock_irq(&data->txlock);
3299 } else {
3300 spin_unlock_irq(&data->txlock);
3301 data->suspend_count--;
3302 return -EBUSY;
3303 }
3304
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003305 cancel_work_sync(&data->work);
3306
Oliver Neukum7bee5492009-08-24 23:44:59 +02003307 btusb_stop_traffic(data);
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003308 usb_kill_anchored_urbs(&data->tx_anchor);
3309
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003310 return 0;
3311}
3312
Oliver Neukum7bee5492009-08-24 23:44:59 +02003313static void play_deferred(struct btusb_data *data)
3314{
3315 struct urb *urb;
3316 int err;
3317
3318 while ((urb = usb_get_from_anchor(&data->deferred))) {
3319 err = usb_submit_urb(urb, GFP_ATOMIC);
3320 if (err < 0)
3321 break;
3322
3323 data->tx_in_flight++;
3324 }
3325 usb_scuttle_anchored_urbs(&data->deferred);
3326}
3327
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003328static int btusb_resume(struct usb_interface *intf)
3329{
3330 struct btusb_data *data = usb_get_intfdata(intf);
3331 struct hci_dev *hdev = data->hdev;
Oliver Neukum7bee5492009-08-24 23:44:59 +02003332 int err = 0;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003333
3334 BT_DBG("intf %p", intf);
3335
3336 if (--data->suspend_count)
3337 return 0;
3338
3339 if (!test_bit(HCI_RUNNING, &hdev->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +02003340 goto done;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003341
3342 if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
3343 err = btusb_submit_intr_urb(hdev, GFP_NOIO);
3344 if (err < 0) {
3345 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02003346 goto failed;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003347 }
3348 }
3349
3350 if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) {
Marcel Holtmann43c2e572009-02-04 17:41:38 +01003351 err = btusb_submit_bulk_urb(hdev, GFP_NOIO);
3352 if (err < 0) {
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003353 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02003354 goto failed;
3355 }
3356
3357 btusb_submit_bulk_urb(hdev, GFP_NOIO);
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003358 }
3359
3360 if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
3361 if (btusb_submit_isoc_urb(hdev, GFP_NOIO) < 0)
3362 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
3363 else
3364 btusb_submit_isoc_urb(hdev, GFP_NOIO);
3365 }
3366
Oliver Neukum7bee5492009-08-24 23:44:59 +02003367 spin_lock_irq(&data->txlock);
3368 play_deferred(data);
3369 clear_bit(BTUSB_SUSPENDING, &data->flags);
3370 spin_unlock_irq(&data->txlock);
3371 schedule_work(&data->work);
3372
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003373 return 0;
Oliver Neukum7bee5492009-08-24 23:44:59 +02003374
3375failed:
3376 usb_scuttle_anchored_urbs(&data->deferred);
3377done:
3378 spin_lock_irq(&data->txlock);
3379 clear_bit(BTUSB_SUSPENDING, &data->flags);
3380 spin_unlock_irq(&data->txlock);
3381
3382 return err;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003383}
Oliver Neukum7bee5492009-08-24 23:44:59 +02003384#endif
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003385
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003386static struct usb_driver btusb_driver = {
3387 .name = "btusb",
3388 .probe = btusb_probe,
3389 .disconnect = btusb_disconnect,
Oliver Neukum7bee5492009-08-24 23:44:59 +02003390#ifdef CONFIG_PM
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003391 .suspend = btusb_suspend,
3392 .resume = btusb_resume,
Oliver Neukum7bee5492009-08-24 23:44:59 +02003393#endif
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003394 .id_table = btusb_table,
Oliver Neukum7bee5492009-08-24 23:44:59 +02003395 .supports_autosuspend = 1,
Sarah Sharpe1f12eb2012-04-23 10:08:51 -07003396 .disable_hub_initiated_lpm = 1,
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003397};
3398
Greg Kroah-Hartman93f15082011-11-18 09:47:34 -08003399module_usb_driver(btusb_driver);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003400
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02003401module_param(disable_scofix, bool, 0644);
3402MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
3403
3404module_param(force_scofix, bool, 0644);
3405MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
3406
3407module_param(reset, bool, 0644);
3408MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
3409
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003410MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
3411MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
3412MODULE_VERSION(VERSION);
3413MODULE_LICENSE("GPL");