blob: 3c10d4dfe9a790e6e34f12022b1bcc2321ac648c [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 Dunn9a5abda2015-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 },
Dmitry Tuninec0810d2015-05-02 13:36:58 +0300189 { USB_DEVICE(0x04ca, 0x300f), .driver_info = BTUSB_ATH3012 },
Janne Heikkinen134d3b32014-12-09 07:44:51 +0200190 { USB_DEVICE(0x04ca, 0x3010), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200191 { USB_DEVICE(0x0930, 0x0219), .driver_info = BTUSB_ATH3012 },
192 { USB_DEVICE(0x0930, 0x0220), .driver_info = BTUSB_ATH3012 },
Vincent Zwanenburg89d29752014-08-08 12:33:56 +0100193 { USB_DEVICE(0x0930, 0x0227), .driver_info = BTUSB_ATH3012 },
Andy Shevchenkoa735f9e2014-02-18 18:26:20 +0200194 { USB_DEVICE(0x0b05, 0x17d0), .driver_info = BTUSB_ATH3012 },
Ming Leid66629c2013-03-15 11:00:39 +0800195 { USB_DEVICE(0x0cf3, 0x0036), .driver_info = BTUSB_ATH3012 },
Steven.Li2d25f8b2011-07-01 14:02:36 +0800196 { USB_DEVICE(0x0cf3, 0x3004), .driver_info = BTUSB_ATH3012 },
Sunguk Lee94a32d102013-03-12 04:41:58 +0900197 { USB_DEVICE(0x0cf3, 0x3008), .driver_info = BTUSB_ATH3012 },
Cho, Yu-Chen07c0ea82012-03-14 22:01:21 +0200198 { USB_DEVICE(0x0cf3, 0x311d), .driver_info = BTUSB_ATH3012 },
Oliver Neukumb1312372014-01-16 15:37:11 +0100199 { USB_DEVICE(0x0cf3, 0x311e), .driver_info = BTUSB_ATH3012 },
Oliver Neukum1e56f1e2014-01-16 16:02:58 +0100200 { USB_DEVICE(0x0cf3, 0x311f), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200201 { USB_DEVICE(0x0cf3, 0x3121), .driver_info = BTUSB_ATH3012 },
Ming Leiebaf5792013-03-18 23:45:11 +0800202 { USB_DEVICE(0x0cf3, 0x817a), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200203 { USB_DEVICE(0x0cf3, 0xe003), .driver_info = BTUSB_ATH3012 },
AceLan Kaoac713112012-04-19 14:53:45 +0800204 { USB_DEVICE(0x0cf3, 0xe004), .driver_info = BTUSB_ATH3012 },
Peng Chen0a3658c2013-08-30 17:41:40 +0800205 { USB_DEVICE(0x0cf3, 0xe005), .driver_info = BTUSB_ATH3012 },
Wen-chien Jesse Sungca79f232015-05-13 11:39:24 +0800206 { USB_DEVICE(0x0cf3, 0xe006), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200207 { USB_DEVICE(0x13d3, 0x3362), .driver_info = BTUSB_ATH3012 },
208 { USB_DEVICE(0x13d3, 0x3375), .driver_info = BTUSB_ATH3012 },
AceLan Kaoeed307e2012-12-11 11:41:20 +0800209 { USB_DEVICE(0x13d3, 0x3393), .driver_info = BTUSB_ATH3012 },
Sujith Manoharan5b77a1f2013-07-15 09:29:03 +0530210 { USB_DEVICE(0x13d3, 0x3402), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin3bb30a7c2014-11-25 20:19:52 +0300211 { USB_DEVICE(0x13d3, 0x3408), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin033efa92015-01-18 00:16:51 +0300212 { USB_DEVICE(0x13d3, 0x3423), .driver_info = BTUSB_ATH3012 },
Anantha Krishnanfa2f1392014-07-08 19:25:08 +0530213 { USB_DEVICE(0x13d3, 0x3432), .driver_info = BTUSB_ATH3012 },
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530214
Cho, Yu-Chene9036e32011-02-15 10:20:07 +0800215 /* Atheros AR5BBU12 with sflash firmware */
216 { USB_DEVICE(0x0489, 0xe02c), .driver_info = BTUSB_IGNORE },
217
Michael Gruetzner85d59722012-05-02 22:33:40 +0200218 /* Atheros AR5BBU12 with sflash firmware */
Yevgeniy Melnichukbc21fde2012-08-07 19:48:10 +0530219 { USB_DEVICE(0x0489, 0xe036), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200220 { USB_DEVICE(0x0489, 0xe03c), .driver_info = BTUSB_ATH3012 },
Michael Gruetzner85d59722012-05-02 22:33:40 +0200221
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +0000222 /* QCA ROME chipset */
Wen-chien Jesse Sung20541112015-05-13 11:39:25 +0800223 { USB_DEVICE(0x0cf3, 0xe007), .driver_info = BTUSB_QCA_ROME },
Marcel Holtmannc9e44472015-03-16 23:56:04 -0700224 { USB_DEVICE(0x0cf3, 0xe300), .driver_info = BTUSB_QCA_ROME },
225 { USB_DEVICE(0x0cf3, 0xe360), .driver_info = BTUSB_QCA_ROME },
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +0000226
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200227 /* Broadcom BCM2035 */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100228 { USB_DEVICE(0x0a5c, 0x2009), .driver_info = BTUSB_BCM92035 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200229 { USB_DEVICE(0x0a5c, 0x200a), .driver_info = BTUSB_WRONG_SCO_MTU },
230 { USB_DEVICE(0x0a5c, 0x2035), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200231
232 /* Broadcom BCM2045 */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100233 { USB_DEVICE(0x0a5c, 0x2039), .driver_info = BTUSB_WRONG_SCO_MTU },
234 { USB_DEVICE(0x0a5c, 0x2101), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmannbdbef3d2008-09-23 00:16:35 +0200235
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200236 /* IBM/Lenovo ThinkPad with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100237 { USB_DEVICE(0x0a5c, 0x201e), .driver_info = BTUSB_WRONG_SCO_MTU },
238 { USB_DEVICE(0x0a5c, 0x2110), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200239
240 /* HP laptop with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100241 { USB_DEVICE(0x03f0, 0x171d), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200242
243 /* Dell laptop with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100244 { USB_DEVICE(0x413c, 0x8126), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200245
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100246 /* Dell Wireless 370 and 410 devices */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100247 { USB_DEVICE(0x413c, 0x8152), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100248 { USB_DEVICE(0x413c, 0x8156), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200249
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100250 /* Belkin F8T012 and F8T013 devices */
251 { USB_DEVICE(0x050d, 0x0012), .driver_info = BTUSB_WRONG_SCO_MTU },
252 { USB_DEVICE(0x050d, 0x0013), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200253
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100254 /* Asus WL-BTD202 device */
255 { USB_DEVICE(0x0b05, 0x1715), .driver_info = BTUSB_WRONG_SCO_MTU },
256
257 /* Kensington Bluetooth USB adapter */
258 { USB_DEVICE(0x047d, 0x105e), .driver_info = BTUSB_WRONG_SCO_MTU },
259
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200260 /* RTX Telecom based adapters with buggy SCO support */
261 { USB_DEVICE(0x0400, 0x0807), .driver_info = BTUSB_BROKEN_ISOC },
262 { USB_DEVICE(0x0400, 0x080a), .driver_info = BTUSB_BROKEN_ISOC },
263
264 /* CONWISE Technology based adapters with buggy SCO support */
265 { USB_DEVICE(0x0e5e, 0x6622), .driver_info = BTUSB_BROKEN_ISOC },
266
Marcel Holtmann4fcef8e2015-01-01 17:34:37 -0800267 /* Roper Class 1 Bluetooth Dongle (Silicon Wave based) */
268 { USB_DEVICE(0x1300, 0x0001), .driver_info = BTUSB_SWAVE },
269
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200270 /* Digianswer devices */
271 { USB_DEVICE(0x08fd, 0x0001), .driver_info = BTUSB_DIGIANSWER },
272 { USB_DEVICE(0x08fd, 0x0002), .driver_info = BTUSB_IGNORE },
273
274 /* CSR BlueCore Bluetooth Sniffer */
Marcel Holtmann4f64fa82014-07-07 00:12:04 +0200275 { USB_DEVICE(0x0a12, 0x0002),
276 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200277
278 /* Frontline ComProbe Bluetooth Sniffer */
Marcel Holtmann4f64fa82014-07-07 00:12:04 +0200279 { USB_DEVICE(0x16d3, 0x0002),
280 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200281
Marcel Holtmanncb1ee892015-01-28 19:41:42 -0800282 /* Marvell Bluetooth devices */
283 { USB_DEVICE(0x1286, 0x2044), .driver_info = BTUSB_MARVELL },
284 { USB_DEVICE(0x1286, 0x2046), .driver_info = BTUSB_MARVELL },
285
Marcel Holtmannd0ac9eb2015-01-28 19:41:43 -0800286 /* Intel Bluetooth devices */
Marcel Holtmann407550f2015-02-22 15:41:18 -0800287 { USB_DEVICE(0x8087, 0x07da), .driver_info = BTUSB_CSR },
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -0700288 { USB_DEVICE(0x8087, 0x07dc), .driver_info = BTUSB_INTEL },
Tedd Ho-Jeong Anef4e5e42013-11-12 13:10:58 -0800289 { USB_DEVICE(0x8087, 0x0a2a), .driver_info = BTUSB_INTEL },
Marcel Holtmanncda0dd72015-01-26 21:33:48 -0800290 { USB_DEVICE(0x8087, 0x0a2b), .driver_info = BTUSB_INTEL_NEW },
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -0700291
Marcel Holtmannd0ac9eb2015-01-28 19:41:43 -0800292 /* Other Intel Bluetooth devices */
293 { USB_VENDOR_AND_INTERFACE_INFO(0x8087, 0xe0, 0x01, 0x01),
294 .driver_info = BTUSB_IGNORE },
Amitkumar Karwarae8df492014-07-18 14:47:06 -0700295
Daniel Drakea2698a92015-04-16 14:09:55 -0600296 /* Realtek Bluetooth devices */
297 { USB_VENDOR_AND_INTERFACE_INFO(0x0bda, 0xe0, 0x01, 0x01),
298 .driver_info = BTUSB_REALTEK },
299
300 /* Additional Realtek 8723AE Bluetooth devices */
301 { USB_DEVICE(0x0930, 0x021d), .driver_info = BTUSB_REALTEK },
302 { USB_DEVICE(0x13d3, 0x3394), .driver_info = BTUSB_REALTEK },
303
304 /* Additional Realtek 8723BE Bluetooth devices */
305 { USB_DEVICE(0x0489, 0xe085), .driver_info = BTUSB_REALTEK },
306 { USB_DEVICE(0x0489, 0xe08b), .driver_info = BTUSB_REALTEK },
307 { USB_DEVICE(0x13d3, 0x3410), .driver_info = BTUSB_REALTEK },
308 { USB_DEVICE(0x13d3, 0x3416), .driver_info = BTUSB_REALTEK },
309 { USB_DEVICE(0x13d3, 0x3459), .driver_info = BTUSB_REALTEK },
310
311 /* Additional Realtek 8821AE Bluetooth devices */
312 { USB_DEVICE(0x0b05, 0x17dc), .driver_info = BTUSB_REALTEK },
313 { USB_DEVICE(0x13d3, 0x3414), .driver_info = BTUSB_REALTEK },
314 { USB_DEVICE(0x13d3, 0x3458), .driver_info = BTUSB_REALTEK },
315 { USB_DEVICE(0x13d3, 0x3461), .driver_info = BTUSB_REALTEK },
316 { USB_DEVICE(0x13d3, 0x3462), .driver_info = BTUSB_REALTEK },
317
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200318 { } /* Terminating entry */
319};
320
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200321#define BTUSB_MAX_ISOC_FRAMES 10
322
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200323#define BTUSB_INTR_RUNNING 0
324#define BTUSB_BULK_RUNNING 1
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200325#define BTUSB_ISOC_RUNNING 2
Oliver Neukum7bee5492009-08-24 23:44:59 +0200326#define BTUSB_SUSPENDING 3
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -0300327#define BTUSB_DID_ISO_RESUME 4
Marcel Holtmanncda0dd72015-01-26 21:33:48 -0800328#define BTUSB_BOOTLOADER 5
329#define BTUSB_DOWNLOADING 6
Marcel Holtmannce6bb922015-01-28 01:58:40 -0800330#define BTUSB_FIRMWARE_LOADED 7
Marcel Holtmanncda0dd72015-01-26 21:33:48 -0800331#define BTUSB_FIRMWARE_FAILED 8
Marcel Holtmannce6bb922015-01-28 01:58:40 -0800332#define BTUSB_BOOTING 9
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200333
334struct btusb_data {
335 struct hci_dev *hdev;
336 struct usb_device *udev;
Marcel Holtmann5fbcd262008-09-23 00:16:36 +0200337 struct usb_interface *intf;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200338 struct usb_interface *isoc;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200339
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200340 unsigned long flags;
341
342 struct work_struct work;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200343 struct work_struct waker;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200344
Marcel Holtmann803b5832014-09-16 08:00:29 +0200345 struct usb_anchor deferred;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200346 struct usb_anchor tx_anchor;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200347 int tx_in_flight;
348 spinlock_t txlock;
349
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200350 struct usb_anchor intr_anchor;
351 struct usb_anchor bulk_anchor;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200352 struct usb_anchor isoc_anchor;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200353 spinlock_t rxlock;
354
355 struct sk_buff *evt_skb;
356 struct sk_buff *acl_skb;
357 struct sk_buff *sco_skb;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200358
359 struct usb_endpoint_descriptor *intr_ep;
360 struct usb_endpoint_descriptor *bulk_tx_ep;
361 struct usb_endpoint_descriptor *bulk_rx_ep;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200362 struct usb_endpoint_descriptor *isoc_tx_ep;
363 struct usb_endpoint_descriptor *isoc_rx_ep;
364
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100365 __u8 cmdreq_type;
Marcel Holtmann893ba542015-01-28 20:27:34 -0800366 __u8 cmdreq;
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100367
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100368 unsigned int sco_num;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200369 int isoc_altsetting;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +0100370 int suspend_count;
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +0100371
Marcel Holtmann97307f52015-01-12 13:51:10 -0800372 int (*recv_event)(struct hci_dev *hdev, struct sk_buff *skb);
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +0100373 int (*recv_bulk)(struct btusb_data *data, void *buffer, int count);
Kim, Ben Young Taeace31982015-02-15 23:06:14 +0000374
375 int (*setup_on_usb)(struct hci_dev *hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200376};
377
Marcel Holtmann803b5832014-09-16 08:00:29 +0200378static inline void btusb_free_frags(struct btusb_data *data)
379{
380 unsigned long flags;
381
382 spin_lock_irqsave(&data->rxlock, flags);
383
384 kfree_skb(data->evt_skb);
385 data->evt_skb = NULL;
386
387 kfree_skb(data->acl_skb);
388 data->acl_skb = NULL;
389
390 kfree_skb(data->sco_skb);
391 data->sco_skb = NULL;
392
393 spin_unlock_irqrestore(&data->rxlock, flags);
394}
395
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200396static int btusb_recv_intr(struct btusb_data *data, void *buffer, int count)
397{
Marcel Holtmann803b5832014-09-16 08:00:29 +0200398 struct sk_buff *skb;
399 int err = 0;
400
401 spin_lock(&data->rxlock);
402 skb = data->evt_skb;
403
404 while (count) {
405 int len;
406
407 if (!skb) {
408 skb = bt_skb_alloc(HCI_MAX_EVENT_SIZE, GFP_ATOMIC);
409 if (!skb) {
410 err = -ENOMEM;
411 break;
412 }
413
414 bt_cb(skb)->pkt_type = HCI_EVENT_PKT;
415 bt_cb(skb)->expect = HCI_EVENT_HDR_SIZE;
416 }
417
418 len = min_t(uint, bt_cb(skb)->expect, count);
419 memcpy(skb_put(skb, len), buffer, len);
420
421 count -= len;
422 buffer += len;
423 bt_cb(skb)->expect -= len;
424
425 if (skb->len == HCI_EVENT_HDR_SIZE) {
426 /* Complete event header */
427 bt_cb(skb)->expect = hci_event_hdr(skb)->plen;
428
429 if (skb_tailroom(skb) < bt_cb(skb)->expect) {
430 kfree_skb(skb);
431 skb = NULL;
432
433 err = -EILSEQ;
434 break;
435 }
436 }
437
438 if (bt_cb(skb)->expect == 0) {
439 /* Complete frame */
Marcel Holtmann97307f52015-01-12 13:51:10 -0800440 data->recv_event(data->hdev, skb);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200441 skb = NULL;
442 }
443 }
444
445 data->evt_skb = skb;
446 spin_unlock(&data->rxlock);
447
448 return err;
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200449}
450
451static int btusb_recv_bulk(struct btusb_data *data, void *buffer, int count)
452{
Marcel Holtmann803b5832014-09-16 08:00:29 +0200453 struct sk_buff *skb;
454 int err = 0;
455
456 spin_lock(&data->rxlock);
457 skb = data->acl_skb;
458
459 while (count) {
460 int len;
461
462 if (!skb) {
463 skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
464 if (!skb) {
465 err = -ENOMEM;
466 break;
467 }
468
469 bt_cb(skb)->pkt_type = HCI_ACLDATA_PKT;
470 bt_cb(skb)->expect = HCI_ACL_HDR_SIZE;
471 }
472
473 len = min_t(uint, bt_cb(skb)->expect, count);
474 memcpy(skb_put(skb, len), buffer, len);
475
476 count -= len;
477 buffer += len;
478 bt_cb(skb)->expect -= len;
479
480 if (skb->len == HCI_ACL_HDR_SIZE) {
481 __le16 dlen = hci_acl_hdr(skb)->dlen;
482
483 /* Complete ACL header */
484 bt_cb(skb)->expect = __le16_to_cpu(dlen);
485
486 if (skb_tailroom(skb) < bt_cb(skb)->expect) {
487 kfree_skb(skb);
488 skb = NULL;
489
490 err = -EILSEQ;
491 break;
492 }
493 }
494
495 if (bt_cb(skb)->expect == 0) {
496 /* Complete frame */
497 hci_recv_frame(data->hdev, skb);
498 skb = NULL;
499 }
500 }
501
502 data->acl_skb = skb;
503 spin_unlock(&data->rxlock);
504
505 return err;
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200506}
507
508static int btusb_recv_isoc(struct btusb_data *data, void *buffer, int count)
509{
Marcel Holtmann803b5832014-09-16 08:00:29 +0200510 struct sk_buff *skb;
511 int err = 0;
512
513 spin_lock(&data->rxlock);
514 skb = data->sco_skb;
515
516 while (count) {
517 int len;
518
519 if (!skb) {
520 skb = bt_skb_alloc(HCI_MAX_SCO_SIZE, GFP_ATOMIC);
521 if (!skb) {
522 err = -ENOMEM;
523 break;
524 }
525
526 bt_cb(skb)->pkt_type = HCI_SCODATA_PKT;
527 bt_cb(skb)->expect = HCI_SCO_HDR_SIZE;
528 }
529
530 len = min_t(uint, bt_cb(skb)->expect, count);
531 memcpy(skb_put(skb, len), buffer, len);
532
533 count -= len;
534 buffer += len;
535 bt_cb(skb)->expect -= len;
536
537 if (skb->len == HCI_SCO_HDR_SIZE) {
538 /* Complete SCO header */
539 bt_cb(skb)->expect = hci_sco_hdr(skb)->dlen;
540
541 if (skb_tailroom(skb) < bt_cb(skb)->expect) {
542 kfree_skb(skb);
543 skb = NULL;
544
545 err = -EILSEQ;
546 break;
547 }
548 }
549
550 if (bt_cb(skb)->expect == 0) {
551 /* Complete frame */
552 hci_recv_frame(data->hdev, skb);
553 skb = NULL;
554 }
555 }
556
557 data->sco_skb = skb;
558 spin_unlock(&data->rxlock);
559
560 return err;
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200561}
562
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200563static void btusb_intr_complete(struct urb *urb)
564{
565 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +0100566 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200567 int err;
568
Marcel Holtmann89e75332014-09-16 04:44:50 +0200569 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
570 urb->actual_length);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200571
572 if (!test_bit(HCI_RUNNING, &hdev->flags))
573 return;
574
575 if (urb->status == 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200576 hdev->stat.byte_rx += urb->actual_length;
577
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200578 if (btusb_recv_intr(data, urb->transfer_buffer,
579 urb->actual_length) < 0) {
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200580 BT_ERR("%s corrupted event packet", hdev->name);
581 hdev->stat.err_rx++;
582 }
Champion Chen85560c42014-09-06 14:06:08 -0500583 } else if (urb->status == -ENOENT) {
584 /* Avoid suspend failed when usb_kill_urb */
585 return;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200586 }
587
588 if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
589 return;
590
Oliver Neukum7bee5492009-08-24 23:44:59 +0200591 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200592 usb_anchor_urb(urb, &data->intr_anchor);
593
594 err = usb_submit_urb(urb, GFP_ATOMIC);
595 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200596 /* -EPERM: urb is being killed;
597 * -ENODEV: device got disconnected */
598 if (err != -EPERM && err != -ENODEV)
Stefan Seyfried61faddf2010-11-30 21:49:08 +0100599 BT_ERR("%s urb %p failed to resubmit (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200600 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200601 usb_unanchor_urb(urb);
602 }
603}
604
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100605static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200606{
David Herrmann155961e2012-02-09 21:58:32 +0100607 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200608 struct urb *urb;
609 unsigned char *buf;
610 unsigned int pipe;
611 int err, size;
612
613 BT_DBG("%s", hdev->name);
614
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200615 if (!data->intr_ep)
616 return -ENODEV;
617
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100618 urb = usb_alloc_urb(0, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200619 if (!urb)
620 return -ENOMEM;
621
622 size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
623
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100624 buf = kmalloc(size, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200625 if (!buf) {
626 usb_free_urb(urb);
627 return -ENOMEM;
628 }
629
630 pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
631
632 usb_fill_int_urb(urb, data->udev, pipe, buf, size,
Marcel Holtmann89e75332014-09-16 04:44:50 +0200633 btusb_intr_complete, hdev, data->intr_ep->bInterval);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200634
635 urb->transfer_flags |= URB_FREE_BUFFER;
636
637 usb_anchor_urb(urb, &data->intr_anchor);
638
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100639 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200640 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +0200641 if (err != -EPERM && err != -ENODEV)
642 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200643 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200644 usb_unanchor_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200645 }
646
647 usb_free_urb(urb);
648
649 return err;
650}
651
652static void btusb_bulk_complete(struct urb *urb)
653{
654 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +0100655 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200656 int err;
657
Marcel Holtmann89e75332014-09-16 04:44:50 +0200658 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
659 urb->actual_length);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200660
661 if (!test_bit(HCI_RUNNING, &hdev->flags))
662 return;
663
664 if (urb->status == 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200665 hdev->stat.byte_rx += urb->actual_length;
666
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +0100667 if (data->recv_bulk(data, urb->transfer_buffer,
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200668 urb->actual_length) < 0) {
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200669 BT_ERR("%s corrupted ACL packet", hdev->name);
670 hdev->stat.err_rx++;
671 }
Champion Chen85560c42014-09-06 14:06:08 -0500672 } else if (urb->status == -ENOENT) {
673 /* Avoid suspend failed when usb_kill_urb */
674 return;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200675 }
676
677 if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
678 return;
679
680 usb_anchor_urb(urb, &data->bulk_anchor);
Oliver Neukum652fd782009-12-16 19:23:43 +0100681 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200682
683 err = usb_submit_urb(urb, GFP_ATOMIC);
684 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200685 /* -EPERM: urb is being killed;
686 * -ENODEV: device got disconnected */
687 if (err != -EPERM && err != -ENODEV)
Stefan Seyfried61faddf2010-11-30 21:49:08 +0100688 BT_ERR("%s urb %p failed to resubmit (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200689 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200690 usb_unanchor_urb(urb);
691 }
692}
693
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100694static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200695{
David Herrmann155961e2012-02-09 21:58:32 +0100696 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200697 struct urb *urb;
698 unsigned char *buf;
699 unsigned int pipe;
Vikram Kandukuri290ba202009-07-02 14:31:59 +0530700 int err, size = HCI_MAX_FRAME_SIZE;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200701
702 BT_DBG("%s", hdev->name);
703
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200704 if (!data->bulk_rx_ep)
705 return -ENODEV;
706
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100707 urb = usb_alloc_urb(0, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200708 if (!urb)
709 return -ENOMEM;
710
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100711 buf = kmalloc(size, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200712 if (!buf) {
713 usb_free_urb(urb);
714 return -ENOMEM;
715 }
716
717 pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
718
Marcel Holtmann89e75332014-09-16 04:44:50 +0200719 usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
720 btusb_bulk_complete, hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200721
722 urb->transfer_flags |= URB_FREE_BUFFER;
723
Oliver Neukum7bee5492009-08-24 23:44:59 +0200724 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200725 usb_anchor_urb(urb, &data->bulk_anchor);
726
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100727 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200728 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +0200729 if (err != -EPERM && err != -ENODEV)
730 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200731 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200732 usb_unanchor_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200733 }
734
735 usb_free_urb(urb);
736
737 return err;
738}
739
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200740static void btusb_isoc_complete(struct urb *urb)
741{
742 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +0100743 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200744 int i, err;
745
Marcel Holtmann89e75332014-09-16 04:44:50 +0200746 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
747 urb->actual_length);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200748
749 if (!test_bit(HCI_RUNNING, &hdev->flags))
750 return;
751
752 if (urb->status == 0) {
753 for (i = 0; i < urb->number_of_packets; i++) {
754 unsigned int offset = urb->iso_frame_desc[i].offset;
755 unsigned int length = urb->iso_frame_desc[i].actual_length;
756
757 if (urb->iso_frame_desc[i].status)
758 continue;
759
760 hdev->stat.byte_rx += length;
761
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200762 if (btusb_recv_isoc(data, urb->transfer_buffer + offset,
763 length) < 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200764 BT_ERR("%s corrupted SCO packet", hdev->name);
765 hdev->stat.err_rx++;
766 }
767 }
Champion Chen85560c42014-09-06 14:06:08 -0500768 } else if (urb->status == -ENOENT) {
769 /* Avoid suspend failed when usb_kill_urb */
770 return;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200771 }
772
773 if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags))
774 return;
775
776 usb_anchor_urb(urb, &data->isoc_anchor);
777
778 err = usb_submit_urb(urb, GFP_ATOMIC);
779 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200780 /* -EPERM: urb is being killed;
781 * -ENODEV: device got disconnected */
782 if (err != -EPERM && err != -ENODEV)
Stefan Seyfried61faddf2010-11-30 21:49:08 +0100783 BT_ERR("%s urb %p failed to resubmit (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200784 hdev->name, urb, -err);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200785 usb_unanchor_urb(urb);
786 }
787}
788
Jesper Juhl42b16b32011-01-17 00:09:38 +0100789static inline void __fill_isoc_descriptor(struct urb *urb, int len, int mtu)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200790{
791 int i, offset = 0;
792
793 BT_DBG("len %d mtu %d", len, mtu);
794
795 for (i = 0; i < BTUSB_MAX_ISOC_FRAMES && len >= mtu;
796 i++, offset += mtu, len -= mtu) {
797 urb->iso_frame_desc[i].offset = offset;
798 urb->iso_frame_desc[i].length = mtu;
799 }
800
801 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
802 urb->iso_frame_desc[i].offset = offset;
803 urb->iso_frame_desc[i].length = len;
804 i++;
805 }
806
807 urb->number_of_packets = i;
808}
809
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100810static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200811{
David Herrmann155961e2012-02-09 21:58:32 +0100812 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200813 struct urb *urb;
814 unsigned char *buf;
815 unsigned int pipe;
816 int err, size;
817
818 BT_DBG("%s", hdev->name);
819
820 if (!data->isoc_rx_ep)
821 return -ENODEV;
822
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100823 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200824 if (!urb)
825 return -ENOMEM;
826
827 size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) *
828 BTUSB_MAX_ISOC_FRAMES;
829
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100830 buf = kmalloc(size, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200831 if (!buf) {
832 usb_free_urb(urb);
833 return -ENOMEM;
834 }
835
836 pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress);
837
Bing Zhaofa0fb932011-12-20 18:19:00 -0800838 usb_fill_int_urb(urb, data->udev, pipe, buf, size, btusb_isoc_complete,
Marcel Holtmann89e75332014-09-16 04:44:50 +0200839 hdev, data->isoc_rx_ep->bInterval);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200840
Marcel Holtmann89e75332014-09-16 04:44:50 +0200841 urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200842
843 __fill_isoc_descriptor(urb, size,
Marcel Holtmann89e75332014-09-16 04:44:50 +0200844 le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize));
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200845
846 usb_anchor_urb(urb, &data->isoc_anchor);
847
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100848 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200849 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +0200850 if (err != -EPERM && err != -ENODEV)
851 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200852 hdev->name, urb, -err);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200853 usb_unanchor_urb(urb);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200854 }
855
856 usb_free_urb(urb);
857
858 return err;
859}
860
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200861static void btusb_tx_complete(struct urb *urb)
862{
863 struct sk_buff *skb = urb->context;
Marcel Holtmann89e75332014-09-16 04:44:50 +0200864 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
David Herrmann155961e2012-02-09 21:58:32 +0100865 struct btusb_data *data = hci_get_drvdata(hdev);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200866
Marcel Holtmann89e75332014-09-16 04:44:50 +0200867 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
868 urb->actual_length);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200869
870 if (!test_bit(HCI_RUNNING, &hdev->flags))
871 goto done;
872
873 if (!urb->status)
874 hdev->stat.byte_tx += urb->transfer_buffer_length;
875 else
876 hdev->stat.err_tx++;
877
878done:
879 spin_lock(&data->txlock);
880 data->tx_in_flight--;
881 spin_unlock(&data->txlock);
882
883 kfree(urb->setup_packet);
884
885 kfree_skb(skb);
886}
887
888static void btusb_isoc_tx_complete(struct urb *urb)
889{
890 struct sk_buff *skb = urb->context;
Marcel Holtmann89e75332014-09-16 04:44:50 +0200891 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200892
Marcel Holtmann89e75332014-09-16 04:44:50 +0200893 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
894 urb->actual_length);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200895
896 if (!test_bit(HCI_RUNNING, &hdev->flags))
897 goto done;
898
899 if (!urb->status)
900 hdev->stat.byte_tx += urb->transfer_buffer_length;
901 else
902 hdev->stat.err_tx++;
903
904done:
905 kfree(urb->setup_packet);
906
907 kfree_skb(skb);
908}
909
910static int btusb_open(struct hci_dev *hdev)
911{
David Herrmann155961e2012-02-09 21:58:32 +0100912 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200913 int err;
914
915 BT_DBG("%s", hdev->name);
916
Kim, Ben Young Taeace31982015-02-15 23:06:14 +0000917 /* Patching USB firmware files prior to starting any URBs of HCI path
918 * It is more safe to use USB bulk channel for downloading USB patch
919 */
920 if (data->setup_on_usb) {
921 err = data->setup_on_usb(hdev);
Marcel Holtmanneb500422015-04-16 23:15:50 +0200922 if (err < 0)
Kim, Ben Young Taeace31982015-02-15 23:06:14 +0000923 return err;
924 }
925
Oliver Neukum7bee5492009-08-24 23:44:59 +0200926 err = usb_autopm_get_interface(data->intf);
927 if (err < 0)
928 return err;
929
930 data->intf->needs_remote_wakeup = 1;
931
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200932 if (test_and_set_bit(HCI_RUNNING, &hdev->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +0200933 goto done;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200934
935 if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +0200936 goto done;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200937
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100938 err = btusb_submit_intr_urb(hdev, GFP_KERNEL);
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100939 if (err < 0)
940 goto failed;
941
942 err = btusb_submit_bulk_urb(hdev, GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200943 if (err < 0) {
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100944 usb_kill_anchored_urbs(&data->intr_anchor);
945 goto failed;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200946 }
947
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100948 set_bit(BTUSB_BULK_RUNNING, &data->flags);
949 btusb_submit_bulk_urb(hdev, GFP_KERNEL);
950
Oliver Neukum7bee5492009-08-24 23:44:59 +0200951done:
952 usb_autopm_put_interface(data->intf);
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100953 return 0;
954
955failed:
956 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
957 clear_bit(HCI_RUNNING, &hdev->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200958 usb_autopm_put_interface(data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200959 return err;
960}
961
Oliver Neukum7bee5492009-08-24 23:44:59 +0200962static void btusb_stop_traffic(struct btusb_data *data)
963{
964 usb_kill_anchored_urbs(&data->intr_anchor);
965 usb_kill_anchored_urbs(&data->bulk_anchor);
966 usb_kill_anchored_urbs(&data->isoc_anchor);
967}
968
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200969static int btusb_close(struct hci_dev *hdev)
970{
David Herrmann155961e2012-02-09 21:58:32 +0100971 struct btusb_data *data = hci_get_drvdata(hdev);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200972 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200973
974 BT_DBG("%s", hdev->name);
975
976 if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
977 return 0;
978
Marcel Holtmanne8c3c3d2008-09-23 00:16:36 +0200979 cancel_work_sync(&data->work);
Linus Torvalds404291a2009-11-11 13:32:29 -0800980 cancel_work_sync(&data->waker);
Marcel Holtmanne8c3c3d2008-09-23 00:16:36 +0200981
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200982 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200983 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200984 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200985
986 btusb_stop_traffic(data);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200987 btusb_free_frags(data);
988
Oliver Neukum7bee5492009-08-24 23:44:59 +0200989 err = usb_autopm_get_interface(data->intf);
990 if (err < 0)
Oliver Neukum7b8e2c12009-11-13 14:26:23 +0100991 goto failed;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200992
993 data->intf->needs_remote_wakeup = 0;
994 usb_autopm_put_interface(data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200995
Oliver Neukum7b8e2c12009-11-13 14:26:23 +0100996failed:
997 usb_scuttle_anchored_urbs(&data->deferred);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200998 return 0;
999}
1000
1001static int btusb_flush(struct hci_dev *hdev)
1002{
David Herrmann155961e2012-02-09 21:58:32 +01001003 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001004
1005 BT_DBG("%s", hdev->name);
1006
1007 usb_kill_anchored_urbs(&data->tx_anchor);
Marcel Holtmann803b5832014-09-16 08:00:29 +02001008 btusb_free_frags(data);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001009
1010 return 0;
1011}
1012
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001013static struct urb *alloc_ctrl_urb(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001014{
David Herrmann155961e2012-02-09 21:58:32 +01001015 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001016 struct usb_ctrlrequest *dr;
1017 struct urb *urb;
1018 unsigned int pipe;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001019
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001020 urb = usb_alloc_urb(0, GFP_KERNEL);
1021 if (!urb)
1022 return ERR_PTR(-ENOMEM);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001023
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001024 dr = kmalloc(sizeof(*dr), GFP_KERNEL);
1025 if (!dr) {
1026 usb_free_urb(urb);
1027 return ERR_PTR(-ENOMEM);
1028 }
1029
1030 dr->bRequestType = data->cmdreq_type;
Marcel Holtmann893ba542015-01-28 20:27:34 -08001031 dr->bRequest = data->cmdreq;
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001032 dr->wIndex = 0;
1033 dr->wValue = 0;
1034 dr->wLength = __cpu_to_le16(skb->len);
1035
1036 pipe = usb_sndctrlpipe(data->udev, 0x00);
1037
Marcel Holtmann89e75332014-09-16 04:44:50 +02001038 usb_fill_control_urb(urb, data->udev, pipe, (void *)dr,
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001039 skb->data, skb->len, btusb_tx_complete, skb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001040
Marcel Holtmann89e75332014-09-16 04:44:50 +02001041 skb->dev = (void *)hdev;
Marcel Holtmann7bd8f092013-10-11 06:19:18 -07001042
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001043 return urb;
1044}
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001045
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001046static struct urb *alloc_bulk_urb(struct hci_dev *hdev, struct sk_buff *skb)
1047{
1048 struct btusb_data *data = hci_get_drvdata(hdev);
1049 struct urb *urb;
1050 unsigned int pipe;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001051
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001052 if (!data->bulk_tx_ep)
1053 return ERR_PTR(-ENODEV);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001054
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001055 urb = usb_alloc_urb(0, GFP_KERNEL);
1056 if (!urb)
1057 return ERR_PTR(-ENOMEM);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001058
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001059 pipe = usb_sndbulkpipe(data->udev, data->bulk_tx_ep->bEndpointAddress);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001060
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001061 usb_fill_bulk_urb(urb, data->udev, pipe,
1062 skb->data, skb->len, btusb_tx_complete, skb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001063
Marcel Holtmann89e75332014-09-16 04:44:50 +02001064 skb->dev = (void *)hdev;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001065
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001066 return urb;
1067}
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001068
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001069static struct urb *alloc_isoc_urb(struct hci_dev *hdev, struct sk_buff *skb)
1070{
1071 struct btusb_data *data = hci_get_drvdata(hdev);
1072 struct urb *urb;
1073 unsigned int pipe;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001074
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001075 if (!data->isoc_tx_ep)
1076 return ERR_PTR(-ENODEV);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001077
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001078 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_KERNEL);
1079 if (!urb)
1080 return ERR_PTR(-ENOMEM);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001081
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001082 pipe = usb_sndisocpipe(data->udev, data->isoc_tx_ep->bEndpointAddress);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001083
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001084 usb_fill_int_urb(urb, data->udev, pipe,
1085 skb->data, skb->len, btusb_isoc_tx_complete,
1086 skb, data->isoc_tx_ep->bInterval);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001087
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001088 urb->transfer_flags = URB_ISO_ASAP;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001089
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001090 __fill_isoc_descriptor(urb, skb->len,
1091 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001092
Marcel Holtmann89e75332014-09-16 04:44:50 +02001093 skb->dev = (void *)hdev;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001094
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001095 return urb;
1096}
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001097
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001098static int submit_tx_urb(struct hci_dev *hdev, struct urb *urb)
1099{
1100 struct btusb_data *data = hci_get_drvdata(hdev);
1101 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001102
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001103 usb_anchor_urb(urb, &data->tx_anchor);
1104
Johan Hedberge9753ef2014-09-14 08:49:34 +03001105 err = usb_submit_urb(urb, GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001106 if (err < 0) {
Paul Bolle5a9b80e2011-10-09 12:12:16 +02001107 if (err != -EPERM && err != -ENODEV)
1108 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +02001109 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001110 kfree(urb->setup_packet);
1111 usb_unanchor_urb(urb);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001112 } else {
1113 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001114 }
1115
Cong Wang54a8a792011-11-22 09:32:57 +08001116 usb_free_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001117 return err;
1118}
1119
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001120static int submit_or_queue_tx_urb(struct hci_dev *hdev, struct urb *urb)
1121{
1122 struct btusb_data *data = hci_get_drvdata(hdev);
1123 unsigned long flags;
1124 bool suspending;
1125
1126 spin_lock_irqsave(&data->txlock, flags);
1127 suspending = test_bit(BTUSB_SUSPENDING, &data->flags);
1128 if (!suspending)
1129 data->tx_in_flight++;
1130 spin_unlock_irqrestore(&data->txlock, flags);
1131
1132 if (!suspending)
1133 return submit_tx_urb(hdev, urb);
1134
1135 usb_anchor_urb(urb, &data->deferred);
1136 schedule_work(&data->waker);
1137
1138 usb_free_urb(urb);
1139 return 0;
1140}
1141
1142static int btusb_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
1143{
1144 struct urb *urb;
1145
1146 BT_DBG("%s", hdev->name);
1147
1148 if (!test_bit(HCI_RUNNING, &hdev->flags))
1149 return -EBUSY;
1150
1151 switch (bt_cb(skb)->pkt_type) {
1152 case HCI_COMMAND_PKT:
1153 urb = alloc_ctrl_urb(hdev, skb);
1154 if (IS_ERR(urb))
1155 return PTR_ERR(urb);
1156
1157 hdev->stat.cmd_tx++;
1158 return submit_or_queue_tx_urb(hdev, urb);
1159
1160 case HCI_ACLDATA_PKT:
1161 urb = alloc_bulk_urb(hdev, skb);
1162 if (IS_ERR(urb))
1163 return PTR_ERR(urb);
1164
1165 hdev->stat.acl_tx++;
1166 return submit_or_queue_tx_urb(hdev, urb);
1167
1168 case HCI_SCODATA_PKT:
1169 if (hci_conn_num(hdev, SCO_LINK) < 1)
1170 return -ENODEV;
1171
1172 urb = alloc_isoc_urb(hdev, skb);
1173 if (IS_ERR(urb))
1174 return PTR_ERR(urb);
1175
1176 hdev->stat.sco_tx++;
1177 return submit_tx_urb(hdev, urb);
1178 }
1179
1180 return -EILSEQ;
1181}
1182
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001183static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
1184{
David Herrmann155961e2012-02-09 21:58:32 +01001185 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001186
1187 BT_DBG("%s evt %d", hdev->name, evt);
1188
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001189 if (hci_conn_num(hdev, SCO_LINK) != data->sco_num) {
1190 data->sco_num = hci_conn_num(hdev, SCO_LINK);
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001191 schedule_work(&data->work);
Marcel Holtmanna780efa2008-11-30 12:17:12 +01001192 }
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001193}
1194
Jesper Juhl42b16b32011-01-17 00:09:38 +01001195static inline int __set_isoc_interface(struct hci_dev *hdev, int altsetting)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001196{
David Herrmann155961e2012-02-09 21:58:32 +01001197 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001198 struct usb_interface *intf = data->isoc;
1199 struct usb_endpoint_descriptor *ep_desc;
1200 int i, err;
1201
1202 if (!data->isoc)
1203 return -ENODEV;
1204
1205 err = usb_set_interface(data->udev, 1, altsetting);
1206 if (err < 0) {
1207 BT_ERR("%s setting interface failed (%d)", hdev->name, -err);
1208 return err;
1209 }
1210
1211 data->isoc_altsetting = altsetting;
1212
1213 data->isoc_tx_ep = NULL;
1214 data->isoc_rx_ep = NULL;
1215
1216 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
1217 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
1218
1219 if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) {
1220 data->isoc_tx_ep = ep_desc;
1221 continue;
1222 }
1223
1224 if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) {
1225 data->isoc_rx_ep = ep_desc;
1226 continue;
1227 }
1228 }
1229
1230 if (!data->isoc_tx_ep || !data->isoc_rx_ep) {
1231 BT_ERR("%s invalid SCO descriptors", hdev->name);
1232 return -ENODEV;
1233 }
1234
1235 return 0;
1236}
1237
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001238static void btusb_work(struct work_struct *work)
1239{
1240 struct btusb_data *data = container_of(work, struct btusb_data, work);
1241 struct hci_dev *hdev = data->hdev;
Mikel Astizf4001d22012-04-11 08:48:51 +02001242 int new_alts;
Oliver Neukum7bee5492009-08-24 23:44:59 +02001243 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001244
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001245 if (data->sco_num > 0) {
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -03001246 if (!test_bit(BTUSB_DID_ISO_RESUME, &data->flags)) {
Oliver Neukum8efdd0c2011-02-11 13:00:06 +01001247 err = usb_autopm_get_interface(data->isoc ? data->isoc : data->intf);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001248 if (err < 0) {
1249 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1250 usb_kill_anchored_urbs(&data->isoc_anchor);
1251 return;
1252 }
1253
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -03001254 set_bit(BTUSB_DID_ISO_RESUME, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001255 }
Mikel Astizf4001d22012-04-11 08:48:51 +02001256
1257 if (hdev->voice_setting & 0x0020) {
1258 static const int alts[3] = { 2, 4, 5 };
Marcel Holtmann89e75332014-09-16 04:44:50 +02001259
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001260 new_alts = alts[data->sco_num - 1];
Mikel Astizf4001d22012-04-11 08:48:51 +02001261 } else {
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001262 new_alts = data->sco_num;
Mikel Astizf4001d22012-04-11 08:48:51 +02001263 }
1264
1265 if (data->isoc_altsetting != new_alts) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001266 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1267 usb_kill_anchored_urbs(&data->isoc_anchor);
1268
Mikel Astizf4001d22012-04-11 08:48:51 +02001269 if (__set_isoc_interface(hdev, new_alts) < 0)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001270 return;
1271 }
1272
1273 if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001274 if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001275 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1276 else
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001277 btusb_submit_isoc_urb(hdev, GFP_KERNEL);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001278 }
1279 } else {
1280 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1281 usb_kill_anchored_urbs(&data->isoc_anchor);
1282
1283 __set_isoc_interface(hdev, 0);
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -03001284 if (test_and_clear_bit(BTUSB_DID_ISO_RESUME, &data->flags))
Oliver Neukum8efdd0c2011-02-11 13:00:06 +01001285 usb_autopm_put_interface(data->isoc ? data->isoc : data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001286 }
1287}
1288
Oliver Neukum7bee5492009-08-24 23:44:59 +02001289static void btusb_waker(struct work_struct *work)
1290{
1291 struct btusb_data *data = container_of(work, struct btusb_data, waker);
1292 int err;
1293
1294 err = usb_autopm_get_interface(data->intf);
1295 if (err < 0)
1296 return;
1297
1298 usb_autopm_put_interface(data->intf);
1299}
1300
Daniel Drake6576fe42015-02-23 18:16:47 -06001301static struct sk_buff *btusb_read_local_version(struct hci_dev *hdev)
1302{
1303 struct sk_buff *skb;
1304
1305 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
1306 HCI_INIT_TIMEOUT);
1307 if (IS_ERR(skb)) {
1308 BT_ERR("%s: HCI_OP_READ_LOCAL_VERSION failed (%ld)",
1309 hdev->name, PTR_ERR(skb));
1310 return skb;
1311 }
1312
1313 if (skb->len != sizeof(struct hci_rp_read_local_version)) {
1314 BT_ERR("%s: HCI_OP_READ_LOCAL_VERSION event length mismatch",
1315 hdev->name);
1316 kfree_skb(skb);
1317 return ERR_PTR(-EIO);
1318 }
1319
1320 return skb;
1321}
1322
Marcel Holtmann9f8f9622013-04-10 08:11:35 -07001323static int btusb_setup_bcm92035(struct hci_dev *hdev)
1324{
1325 struct sk_buff *skb;
1326 u8 val = 0x00;
1327
1328 BT_DBG("%s", hdev->name);
1329
1330 skb = __hci_cmd_sync(hdev, 0xfc3b, 1, &val, HCI_INIT_TIMEOUT);
1331 if (IS_ERR(skb))
1332 BT_ERR("BCM92035 command failed (%ld)", -PTR_ERR(skb));
1333 else
1334 kfree_skb(skb);
1335
1336 return 0;
1337}
1338
Marcel Holtmann81cac642014-01-03 03:02:36 -08001339static int btusb_setup_csr(struct hci_dev *hdev)
1340{
1341 struct hci_rp_read_local_version *rp;
1342 struct sk_buff *skb;
1343 int ret;
1344
1345 BT_DBG("%s", hdev->name);
1346
Daniel Drake6576fe42015-02-23 18:16:47 -06001347 skb = btusb_read_local_version(hdev);
1348 if (IS_ERR(skb))
Marcel Holtmann81cac642014-01-03 03:02:36 -08001349 return -PTR_ERR(skb);
Marcel Holtmann81cac642014-01-03 03:02:36 -08001350
Marcel Holtmann89e75332014-09-16 04:44:50 +02001351 rp = (struct hci_rp_read_local_version *)skb->data;
Marcel Holtmann81cac642014-01-03 03:02:36 -08001352
1353 if (!rp->status) {
1354 if (le16_to_cpu(rp->manufacturer) != 10) {
1355 /* Clear the reset quirk since this is not an actual
1356 * early Bluetooth 1.1 device from CSR.
1357 */
1358 clear_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
1359
1360 /* These fake CSR controllers have all a broken
1361 * stored link key handling and so just disable it.
1362 */
1363 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY,
1364 &hdev->quirks);
1365 }
1366 }
1367
1368 ret = -bt_to_errno(rp->status);
1369
1370 kfree_skb(skb);
1371
1372 return ret;
1373}
1374
Daniel Drakea2698a92015-04-16 14:09:55 -06001375#define RTL_FRAG_LEN 252
1376
1377struct rtl_download_cmd {
1378 __u8 index;
1379 __u8 data[RTL_FRAG_LEN];
1380} __packed;
1381
1382struct rtl_download_response {
1383 __u8 status;
1384 __u8 index;
1385} __packed;
1386
1387struct rtl_rom_version_evt {
1388 __u8 status;
1389 __u8 version;
1390} __packed;
1391
1392struct rtl_epatch_header {
1393 __u8 signature[8];
1394 __le32 fw_version;
1395 __le16 num_patches;
1396} __packed;
1397
1398#define RTL_EPATCH_SIGNATURE "Realtech"
1399#define RTL_ROM_LMP_3499 0x3499
1400#define RTL_ROM_LMP_8723A 0x1200
1401#define RTL_ROM_LMP_8723B 0x8723
1402#define RTL_ROM_LMP_8821A 0x8821
1403#define RTL_ROM_LMP_8761A 0x8761
1404
1405static int rtl_read_rom_version(struct hci_dev *hdev, u8 *version)
1406{
1407 struct rtl_rom_version_evt *rom_version;
1408 struct sk_buff *skb;
1409 int ret;
1410
1411 /* Read RTL ROM version command */
1412 skb = __hci_cmd_sync(hdev, 0xfc6d, 0, NULL, HCI_INIT_TIMEOUT);
1413 if (IS_ERR(skb)) {
1414 BT_ERR("%s: Read ROM version failed (%ld)",
1415 hdev->name, PTR_ERR(skb));
1416 return PTR_ERR(skb);
1417 }
1418
1419 if (skb->len != sizeof(*rom_version)) {
1420 BT_ERR("%s: RTL version event length mismatch", hdev->name);
1421 kfree_skb(skb);
1422 return -EIO;
1423 }
1424
1425 rom_version = (struct rtl_rom_version_evt *)skb->data;
1426 BT_INFO("%s: rom_version status=%x version=%x",
1427 hdev->name, rom_version->status, rom_version->version);
1428
1429 ret = rom_version->status;
1430 if (ret == 0)
1431 *version = rom_version->version;
1432
1433 kfree_skb(skb);
1434 return ret;
1435}
1436
1437static int rtl8723b_parse_firmware(struct hci_dev *hdev, u16 lmp_subver,
1438 const struct firmware *fw,
1439 unsigned char **_buf)
1440{
1441 const u8 extension_sig[] = { 0x51, 0x04, 0xfd, 0x77 };
1442 struct rtl_epatch_header *epatch_info;
1443 unsigned char *buf;
1444 int i, ret, len;
1445 size_t min_size;
1446 u8 opcode, length, data, rom_version = 0;
1447 int project_id = -1;
1448 const unsigned char *fwptr, *chip_id_base;
1449 const unsigned char *patch_length_base, *patch_offset_base;
1450 u32 patch_offset = 0;
1451 u16 patch_length, num_patches;
1452 const u16 project_id_to_lmp_subver[] = {
1453 RTL_ROM_LMP_8723A,
1454 RTL_ROM_LMP_8723B,
1455 RTL_ROM_LMP_8821A,
1456 RTL_ROM_LMP_8761A
1457 };
1458
1459 ret = rtl_read_rom_version(hdev, &rom_version);
1460 if (ret)
1461 return -bt_to_errno(ret);
1462
1463 min_size = sizeof(struct rtl_epatch_header) + sizeof(extension_sig) + 3;
1464 if (fw->size < min_size)
1465 return -EINVAL;
1466
1467 fwptr = fw->data + fw->size - sizeof(extension_sig);
1468 if (memcmp(fwptr, extension_sig, sizeof(extension_sig)) != 0) {
1469 BT_ERR("%s: extension section signature mismatch", hdev->name);
1470 return -EINVAL;
1471 }
1472
1473 /* Loop from the end of the firmware parsing instructions, until
1474 * we find an instruction that identifies the "project ID" for the
1475 * hardware supported by this firwmare file.
1476 * Once we have that, we double-check that that project_id is suitable
1477 * for the hardware we are working with.
1478 */
1479 while (fwptr >= fw->data + (sizeof(struct rtl_epatch_header) + 3)) {
1480 opcode = *--fwptr;
1481 length = *--fwptr;
1482 data = *--fwptr;
1483
1484 BT_DBG("check op=%x len=%x data=%x", opcode, length, data);
1485
1486 if (opcode == 0xff) /* EOF */
1487 break;
1488
1489 if (length == 0) {
1490 BT_ERR("%s: found instruction with length 0",
1491 hdev->name);
1492 return -EINVAL;
1493 }
1494
1495 if (opcode == 0 && length == 1) {
1496 project_id = data;
1497 break;
1498 }
1499
1500 fwptr -= length;
1501 }
1502
1503 if (project_id < 0) {
1504 BT_ERR("%s: failed to find version instruction", hdev->name);
1505 return -EINVAL;
1506 }
1507
Dan Carpenterc57ddfa2015-04-20 18:51:35 +03001508 if (project_id >= ARRAY_SIZE(project_id_to_lmp_subver)) {
Daniel Drakea2698a92015-04-16 14:09:55 -06001509 BT_ERR("%s: unknown project id %d", hdev->name, project_id);
1510 return -EINVAL;
1511 }
1512
1513 if (lmp_subver != project_id_to_lmp_subver[project_id]) {
1514 BT_ERR("%s: firmware is for %x but this is a %x", hdev->name,
1515 project_id_to_lmp_subver[project_id], lmp_subver);
1516 return -EINVAL;
1517 }
1518
1519 epatch_info = (struct rtl_epatch_header *)fw->data;
1520 if (memcmp(epatch_info->signature, RTL_EPATCH_SIGNATURE, 8) != 0) {
1521 BT_ERR("%s: bad EPATCH signature", hdev->name);
1522 return -EINVAL;
1523 }
1524
1525 num_patches = le16_to_cpu(epatch_info->num_patches);
1526 BT_DBG("fw_version=%x, num_patches=%d",
1527 le32_to_cpu(epatch_info->fw_version), num_patches);
1528
1529 /* After the rtl_epatch_header there is a funky patch metadata section.
1530 * Assuming 2 patches, the layout is:
1531 * ChipID1 ChipID2 PatchLength1 PatchLength2 PatchOffset1 PatchOffset2
1532 *
1533 * Find the right patch for this chip.
1534 */
1535 min_size += 8 * num_patches;
1536 if (fw->size < min_size)
1537 return -EINVAL;
1538
1539 chip_id_base = fw->data + sizeof(struct rtl_epatch_header);
1540 patch_length_base = chip_id_base + (sizeof(u16) * num_patches);
1541 patch_offset_base = patch_length_base + (sizeof(u16) * num_patches);
1542 for (i = 0; i < num_patches; i++) {
1543 u16 chip_id = get_unaligned_le16(chip_id_base +
1544 (i * sizeof(u16)));
1545 if (chip_id == rom_version + 1) {
1546 patch_length = get_unaligned_le16(patch_length_base +
1547 (i * sizeof(u16)));
1548 patch_offset = get_unaligned_le32(patch_offset_base +
1549 (i * sizeof(u32)));
1550 break;
1551 }
1552 }
1553
1554 if (!patch_offset) {
1555 BT_ERR("%s: didn't find patch for chip id %d",
1556 hdev->name, rom_version);
1557 return -EINVAL;
1558 }
1559
1560 BT_DBG("length=%x offset=%x index %d", patch_length, patch_offset, i);
1561 min_size = patch_offset + patch_length;
1562 if (fw->size < min_size)
1563 return -EINVAL;
1564
1565 /* Copy the firmware into a new buffer and write the version at
1566 * the end.
1567 */
1568 len = patch_length;
1569 buf = kmemdup(fw->data + patch_offset, patch_length, GFP_KERNEL);
1570 if (!buf)
1571 return -ENOMEM;
1572
1573 memcpy(buf + patch_length - 4, &epatch_info->fw_version, 4);
1574
1575 *_buf = buf;
1576 return len;
1577}
1578
1579static int rtl_download_firmware(struct hci_dev *hdev,
1580 const unsigned char *data, int fw_len)
1581{
1582 struct rtl_download_cmd *dl_cmd;
1583 int frag_num = fw_len / RTL_FRAG_LEN + 1;
1584 int frag_len = RTL_FRAG_LEN;
1585 int ret = 0;
1586 int i;
1587
1588 dl_cmd = kmalloc(sizeof(struct rtl_download_cmd), GFP_KERNEL);
1589 if (!dl_cmd)
1590 return -ENOMEM;
1591
1592 for (i = 0; i < frag_num; i++) {
1593 struct rtl_download_response *dl_resp;
1594 struct sk_buff *skb;
1595
1596 BT_DBG("download fw (%d/%d)", i, frag_num);
1597
1598 dl_cmd->index = i;
1599 if (i == (frag_num - 1)) {
1600 dl_cmd->index |= 0x80; /* data end */
1601 frag_len = fw_len % RTL_FRAG_LEN;
1602 }
1603 memcpy(dl_cmd->data, data, frag_len);
1604
1605 /* Send download command */
1606 skb = __hci_cmd_sync(hdev, 0xfc20, frag_len + 1, dl_cmd,
1607 HCI_INIT_TIMEOUT);
1608 if (IS_ERR(skb)) {
1609 BT_ERR("%s: download fw command failed (%ld)",
1610 hdev->name, PTR_ERR(skb));
1611 ret = -PTR_ERR(skb);
1612 goto out;
1613 }
1614
1615 if (skb->len != sizeof(*dl_resp)) {
1616 BT_ERR("%s: download fw event length mismatch",
1617 hdev->name);
1618 kfree_skb(skb);
1619 ret = -EIO;
1620 goto out;
1621 }
1622
1623 dl_resp = (struct rtl_download_response *)skb->data;
1624 if (dl_resp->status != 0) {
1625 kfree_skb(skb);
1626 ret = bt_to_errno(dl_resp->status);
1627 goto out;
1628 }
1629
1630 kfree_skb(skb);
1631 data += RTL_FRAG_LEN;
1632 }
1633
1634out:
1635 kfree(dl_cmd);
1636 return ret;
1637}
1638
1639static int btusb_setup_rtl8723a(struct hci_dev *hdev)
1640{
1641 struct btusb_data *data = dev_get_drvdata(&hdev->dev);
1642 struct usb_device *udev = interface_to_usbdev(data->intf);
1643 const struct firmware *fw;
1644 int ret;
1645
1646 BT_INFO("%s: rtl: loading rtl_bt/rtl8723a_fw.bin", hdev->name);
1647 ret = request_firmware(&fw, "rtl_bt/rtl8723a_fw.bin", &udev->dev);
1648 if (ret < 0) {
1649 BT_ERR("%s: Failed to load rtl_bt/rtl8723a_fw.bin", hdev->name);
1650 return ret;
1651 }
1652
1653 if (fw->size < 8) {
1654 ret = -EINVAL;
1655 goto out;
1656 }
1657
1658 /* Check that the firmware doesn't have the epatch signature
1659 * (which is only for RTL8723B and newer).
1660 */
1661 if (!memcmp(fw->data, RTL_EPATCH_SIGNATURE, 8)) {
1662 BT_ERR("%s: unexpected EPATCH signature!", hdev->name);
1663 ret = -EINVAL;
1664 goto out;
1665 }
1666
1667 ret = rtl_download_firmware(hdev, fw->data, fw->size);
1668
1669out:
1670 release_firmware(fw);
1671 return ret;
1672}
1673
1674static int btusb_setup_rtl8723b(struct hci_dev *hdev, u16 lmp_subver,
1675 const char *fw_name)
1676{
1677 struct btusb_data *data = dev_get_drvdata(&hdev->dev);
1678 struct usb_device *udev = interface_to_usbdev(data->intf);
1679 unsigned char *fw_data = NULL;
1680 const struct firmware *fw;
1681 int ret;
1682
1683 BT_INFO("%s: rtl: loading %s", hdev->name, fw_name);
1684 ret = request_firmware(&fw, fw_name, &udev->dev);
1685 if (ret < 0) {
1686 BT_ERR("%s: Failed to load %s", hdev->name, fw_name);
1687 return ret;
1688 }
1689
1690 ret = rtl8723b_parse_firmware(hdev, lmp_subver, fw, &fw_data);
1691 if (ret < 0)
1692 goto out;
1693
1694 ret = rtl_download_firmware(hdev, fw_data, ret);
1695 kfree(fw_data);
1696 if (ret < 0)
1697 goto out;
1698
1699out:
1700 release_firmware(fw);
1701 return ret;
1702}
1703
1704static int btusb_setup_realtek(struct hci_dev *hdev)
1705{
1706 struct sk_buff *skb;
1707 struct hci_rp_read_local_version *resp;
1708 u16 lmp_subver;
1709
1710 skb = btusb_read_local_version(hdev);
1711 if (IS_ERR(skb))
1712 return -PTR_ERR(skb);
1713
1714 resp = (struct hci_rp_read_local_version *)skb->data;
1715 BT_INFO("%s: rtl: examining hci_ver=%02x hci_rev=%04x lmp_ver=%02x "
1716 "lmp_subver=%04x", hdev->name, resp->hci_ver, resp->hci_rev,
1717 resp->lmp_ver, resp->lmp_subver);
1718
1719 lmp_subver = le16_to_cpu(resp->lmp_subver);
1720 kfree_skb(skb);
1721
1722 /* Match a set of subver values that correspond to stock firmware,
1723 * which is not compatible with standard btusb.
1724 * If matched, upload an alternative firmware that does conform to
1725 * standard btusb. Once that firmware is uploaded, the subver changes
1726 * to a different value.
1727 */
1728 switch (lmp_subver) {
1729 case RTL_ROM_LMP_8723A:
1730 case RTL_ROM_LMP_3499:
1731 return btusb_setup_rtl8723a(hdev);
1732 case RTL_ROM_LMP_8723B:
1733 return btusb_setup_rtl8723b(hdev, lmp_subver,
1734 "rtl_bt/rtl8723b_fw.bin");
1735 case RTL_ROM_LMP_8821A:
1736 return btusb_setup_rtl8723b(hdev, lmp_subver,
1737 "rtl_bt/rtl8821a_fw.bin");
1738 case RTL_ROM_LMP_8761A:
1739 return btusb_setup_rtl8723b(hdev, lmp_subver,
1740 "rtl_bt/rtl8761a_fw.bin");
1741 default:
1742 BT_INFO("rtl: assuming no firmware upload needed.");
1743 return 0;
1744 }
1745}
1746
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001747static const struct firmware *btusb_setup_intel_get_fw(struct hci_dev *hdev,
Marcel Holtmann89e75332014-09-16 04:44:50 +02001748 struct intel_version *ver)
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001749{
1750 const struct firmware *fw;
1751 char fwname[64];
1752 int ret;
1753
1754 snprintf(fwname, sizeof(fwname),
1755 "intel/ibt-hw-%x.%x.%x-fw-%x.%x.%x.%x.%x.bseq",
1756 ver->hw_platform, ver->hw_variant, ver->hw_revision,
1757 ver->fw_variant, ver->fw_revision, ver->fw_build_num,
1758 ver->fw_build_ww, ver->fw_build_yy);
1759
1760 ret = request_firmware(&fw, fwname, &hdev->dev);
1761 if (ret < 0) {
1762 if (ret == -EINVAL) {
1763 BT_ERR("%s Intel firmware file request failed (%d)",
1764 hdev->name, ret);
1765 return NULL;
1766 }
1767
1768 BT_ERR("%s failed to open Intel firmware file: %s(%d)",
1769 hdev->name, fwname, ret);
1770
1771 /* If the correct firmware patch file is not found, use the
1772 * default firmware patch file instead
1773 */
1774 snprintf(fwname, sizeof(fwname), "intel/ibt-hw-%x.%x.bseq",
1775 ver->hw_platform, ver->hw_variant);
1776 if (request_firmware(&fw, fwname, &hdev->dev) < 0) {
1777 BT_ERR("%s failed to open default Intel fw file: %s",
1778 hdev->name, fwname);
1779 return NULL;
1780 }
1781 }
1782
1783 BT_INFO("%s: Intel Bluetooth firmware file: %s", hdev->name, fwname);
1784
1785 return fw;
1786}
1787
1788static int btusb_setup_intel_patching(struct hci_dev *hdev,
1789 const struct firmware *fw,
1790 const u8 **fw_ptr, int *disable_patch)
1791{
1792 struct sk_buff *skb;
1793 struct hci_command_hdr *cmd;
1794 const u8 *cmd_param;
1795 struct hci_event_hdr *evt = NULL;
1796 const u8 *evt_param = NULL;
1797 int remain = fw->size - (*fw_ptr - fw->data);
1798
1799 /* The first byte indicates the types of the patch command or event.
1800 * 0x01 means HCI command and 0x02 is HCI event. If the first bytes
1801 * in the current firmware buffer doesn't start with 0x01 or
1802 * the size of remain buffer is smaller than HCI command header,
1803 * the firmware file is corrupted and it should stop the patching
1804 * process.
1805 */
1806 if (remain > HCI_COMMAND_HDR_SIZE && *fw_ptr[0] != 0x01) {
1807 BT_ERR("%s Intel fw corrupted: invalid cmd read", hdev->name);
1808 return -EINVAL;
1809 }
1810 (*fw_ptr)++;
1811 remain--;
1812
1813 cmd = (struct hci_command_hdr *)(*fw_ptr);
1814 *fw_ptr += sizeof(*cmd);
1815 remain -= sizeof(*cmd);
1816
1817 /* Ensure that the remain firmware data is long enough than the length
1818 * of command parameter. If not, the firmware file is corrupted.
1819 */
1820 if (remain < cmd->plen) {
1821 BT_ERR("%s Intel fw corrupted: invalid cmd len", hdev->name);
1822 return -EFAULT;
1823 }
1824
1825 /* If there is a command that loads a patch in the firmware
1826 * file, then enable the patch upon success, otherwise just
1827 * disable the manufacturer mode, for example patch activation
1828 * is not required when the default firmware patch file is used
1829 * because there are no patch data to load.
1830 */
1831 if (*disable_patch && le16_to_cpu(cmd->opcode) == 0xfc8e)
1832 *disable_patch = 0;
1833
1834 cmd_param = *fw_ptr;
1835 *fw_ptr += cmd->plen;
1836 remain -= cmd->plen;
1837
1838 /* This reads the expected events when the above command is sent to the
1839 * device. Some vendor commands expects more than one events, for
1840 * example command status event followed by vendor specific event.
1841 * For this case, it only keeps the last expected event. so the command
1842 * can be sent with __hci_cmd_sync_ev() which returns the sk_buff of
1843 * last expected event.
1844 */
1845 while (remain > HCI_EVENT_HDR_SIZE && *fw_ptr[0] == 0x02) {
1846 (*fw_ptr)++;
1847 remain--;
1848
1849 evt = (struct hci_event_hdr *)(*fw_ptr);
1850 *fw_ptr += sizeof(*evt);
1851 remain -= sizeof(*evt);
1852
1853 if (remain < evt->plen) {
1854 BT_ERR("%s Intel fw corrupted: invalid evt len",
1855 hdev->name);
1856 return -EFAULT;
1857 }
1858
1859 evt_param = *fw_ptr;
1860 *fw_ptr += evt->plen;
1861 remain -= evt->plen;
1862 }
1863
1864 /* Every HCI commands in the firmware file has its correspond event.
1865 * If event is not found or remain is smaller than zero, the firmware
1866 * file is corrupted.
1867 */
1868 if (!evt || !evt_param || remain < 0) {
1869 BT_ERR("%s Intel fw corrupted: invalid evt read", hdev->name);
1870 return -EFAULT;
1871 }
1872
1873 skb = __hci_cmd_sync_ev(hdev, le16_to_cpu(cmd->opcode), cmd->plen,
1874 cmd_param, evt->evt, HCI_INIT_TIMEOUT);
1875 if (IS_ERR(skb)) {
1876 BT_ERR("%s sending Intel patch command (0x%4.4x) failed (%ld)",
1877 hdev->name, cmd->opcode, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001878 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001879 }
1880
1881 /* It ensures that the returned event matches the event data read from
1882 * the firmware file. At fist, it checks the length and then
1883 * the contents of the event.
1884 */
1885 if (skb->len != evt->plen) {
1886 BT_ERR("%s mismatch event length (opcode 0x%4.4x)", hdev->name,
1887 le16_to_cpu(cmd->opcode));
1888 kfree_skb(skb);
1889 return -EFAULT;
1890 }
1891
1892 if (memcmp(skb->data, evt_param, evt->plen)) {
1893 BT_ERR("%s mismatch event parameter (opcode 0x%4.4x)",
1894 hdev->name, le16_to_cpu(cmd->opcode));
1895 kfree_skb(skb);
1896 return -EFAULT;
1897 }
1898 kfree_skb(skb);
1899
1900 return 0;
1901}
1902
1903static int btusb_setup_intel(struct hci_dev *hdev)
1904{
1905 struct sk_buff *skb;
1906 const struct firmware *fw;
1907 const u8 *fw_ptr;
1908 int disable_patch;
1909 struct intel_version *ver;
1910
1911 const u8 mfg_enable[] = { 0x01, 0x00 };
1912 const u8 mfg_disable[] = { 0x00, 0x00 };
1913 const u8 mfg_reset_deactivate[] = { 0x00, 0x01 };
1914 const u8 mfg_reset_activate[] = { 0x00, 0x02 };
1915
1916 BT_DBG("%s", hdev->name);
1917
1918 /* The controller has a bug with the first HCI command sent to it
1919 * returning number of completed commands as zero. This would stall the
1920 * command processing in the Bluetooth core.
1921 *
1922 * As a workaround, send HCI Reset command first which will reset the
1923 * number of completed commands and allow normal command processing
1924 * from now on.
1925 */
1926 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
1927 if (IS_ERR(skb)) {
1928 BT_ERR("%s sending initial HCI reset command failed (%ld)",
1929 hdev->name, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001930 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001931 }
1932 kfree_skb(skb);
1933
1934 /* Read Intel specific controller version first to allow selection of
1935 * which firmware file to load.
1936 *
1937 * The returned information are hardware variant and revision plus
1938 * firmware variant, revision and build number.
1939 */
1940 skb = __hci_cmd_sync(hdev, 0xfc05, 0, NULL, HCI_INIT_TIMEOUT);
1941 if (IS_ERR(skb)) {
1942 BT_ERR("%s reading Intel fw version command failed (%ld)",
1943 hdev->name, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001944 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001945 }
1946
1947 if (skb->len != sizeof(*ver)) {
1948 BT_ERR("%s Intel version event length mismatch", hdev->name);
1949 kfree_skb(skb);
1950 return -EIO;
1951 }
1952
1953 ver = (struct intel_version *)skb->data;
1954 if (ver->status) {
1955 BT_ERR("%s Intel fw version event failed (%02x)", hdev->name,
1956 ver->status);
1957 kfree_skb(skb);
1958 return -bt_to_errno(ver->status);
1959 }
1960
1961 BT_INFO("%s: read Intel version: %02x%02x%02x%02x%02x%02x%02x%02x%02x",
1962 hdev->name, ver->hw_platform, ver->hw_variant,
1963 ver->hw_revision, ver->fw_variant, ver->fw_revision,
1964 ver->fw_build_num, ver->fw_build_ww, ver->fw_build_yy,
1965 ver->fw_patch_num);
1966
1967 /* fw_patch_num indicates the version of patch the device currently
1968 * have. If there is no patch data in the device, it is always 0x00.
1969 * So, if it is other than 0x00, no need to patch the deivce again.
1970 */
1971 if (ver->fw_patch_num) {
1972 BT_INFO("%s: Intel device is already patched. patch num: %02x",
1973 hdev->name, ver->fw_patch_num);
1974 kfree_skb(skb);
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07001975 btintel_check_bdaddr(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001976 return 0;
1977 }
1978
1979 /* Opens the firmware patch file based on the firmware version read
1980 * from the controller. If it fails to open the matching firmware
1981 * patch file, it tries to open the default firmware patch file.
1982 * If no patch file is found, allow the device to operate without
1983 * a patch.
1984 */
1985 fw = btusb_setup_intel_get_fw(hdev, ver);
1986 if (!fw) {
1987 kfree_skb(skb);
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07001988 btintel_check_bdaddr(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001989 return 0;
1990 }
1991 fw_ptr = fw->data;
1992
1993 /* This Intel specific command enables the manufacturer mode of the
1994 * controller.
1995 *
1996 * Only while this mode is enabled, the driver can download the
1997 * firmware patch data and configuration parameters.
1998 */
1999 skb = __hci_cmd_sync(hdev, 0xfc11, 2, mfg_enable, HCI_INIT_TIMEOUT);
2000 if (IS_ERR(skb)) {
2001 BT_ERR("%s entering Intel manufacturer mode failed (%ld)",
2002 hdev->name, PTR_ERR(skb));
2003 release_firmware(fw);
Adam Leed9c78e92013-07-10 10:02:12 +08002004 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002005 }
2006
2007 if (skb->data[0]) {
2008 u8 evt_status = skb->data[0];
Marcel Holtmann89e75332014-09-16 04:44:50 +02002009
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002010 BT_ERR("%s enable Intel manufacturer mode event failed (%02x)",
2011 hdev->name, evt_status);
2012 kfree_skb(skb);
2013 release_firmware(fw);
2014 return -bt_to_errno(evt_status);
2015 }
2016 kfree_skb(skb);
2017
2018 disable_patch = 1;
2019
2020 /* The firmware data file consists of list of Intel specific HCI
2021 * commands and its expected events. The first byte indicates the
2022 * type of the message, either HCI command or HCI event.
2023 *
2024 * It reads the command and its expected event from the firmware file,
2025 * and send to the controller. Once __hci_cmd_sync_ev() returns,
2026 * the returned event is compared with the event read from the firmware
2027 * file and it will continue until all the messages are downloaded to
2028 * the controller.
2029 *
2030 * Once the firmware patching is completed successfully,
2031 * the manufacturer mode is disabled with reset and activating the
2032 * downloaded patch.
2033 *
2034 * If the firmware patching fails, the manufacturer mode is
2035 * disabled with reset and deactivating the patch.
2036 *
2037 * If the default patch file is used, no reset is done when disabling
2038 * the manufacturer.
2039 */
2040 while (fw->size > fw_ptr - fw->data) {
2041 int ret;
2042
2043 ret = btusb_setup_intel_patching(hdev, fw, &fw_ptr,
2044 &disable_patch);
2045 if (ret < 0)
2046 goto exit_mfg_deactivate;
2047 }
2048
2049 release_firmware(fw);
2050
2051 if (disable_patch)
2052 goto exit_mfg_disable;
2053
2054 /* Patching completed successfully and disable the manufacturer mode
2055 * with reset and activate the downloaded firmware patches.
2056 */
2057 skb = __hci_cmd_sync(hdev, 0xfc11, sizeof(mfg_reset_activate),
2058 mfg_reset_activate, HCI_INIT_TIMEOUT);
2059 if (IS_ERR(skb)) {
2060 BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
2061 hdev->name, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08002062 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002063 }
2064 kfree_skb(skb);
2065
2066 BT_INFO("%s: Intel Bluetooth firmware patch completed and activated",
2067 hdev->name);
2068
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07002069 btintel_check_bdaddr(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002070 return 0;
2071
2072exit_mfg_disable:
2073 /* Disable the manufacturer mode without reset */
2074 skb = __hci_cmd_sync(hdev, 0xfc11, sizeof(mfg_disable), mfg_disable,
2075 HCI_INIT_TIMEOUT);
2076 if (IS_ERR(skb)) {
2077 BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
2078 hdev->name, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08002079 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002080 }
2081 kfree_skb(skb);
2082
2083 BT_INFO("%s: Intel Bluetooth firmware patch completed", hdev->name);
Marcel Holtmann40cb0982014-07-02 12:06:45 +02002084
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07002085 btintel_check_bdaddr(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002086 return 0;
2087
2088exit_mfg_deactivate:
2089 release_firmware(fw);
2090
2091 /* Patching failed. Disable the manufacturer mode with reset and
2092 * deactivate the downloaded firmware patches.
2093 */
2094 skb = __hci_cmd_sync(hdev, 0xfc11, sizeof(mfg_reset_deactivate),
2095 mfg_reset_deactivate, HCI_INIT_TIMEOUT);
2096 if (IS_ERR(skb)) {
2097 BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
2098 hdev->name, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08002099 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002100 }
2101 kfree_skb(skb);
2102
2103 BT_INFO("%s: Intel Bluetooth firmware patch completed and deactivated",
2104 hdev->name);
2105
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07002106 btintel_check_bdaddr(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002107 return 0;
2108}
2109
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002110static int inject_cmd_complete(struct hci_dev *hdev, __u16 opcode)
2111{
2112 struct sk_buff *skb;
2113 struct hci_event_hdr *hdr;
2114 struct hci_ev_cmd_complete *evt;
2115
2116 skb = bt_skb_alloc(sizeof(*hdr) + sizeof(*evt) + 1, GFP_ATOMIC);
2117 if (!skb)
2118 return -ENOMEM;
2119
2120 hdr = (struct hci_event_hdr *)skb_put(skb, sizeof(*hdr));
2121 hdr->evt = HCI_EV_CMD_COMPLETE;
2122 hdr->plen = sizeof(*evt) + 1;
2123
2124 evt = (struct hci_ev_cmd_complete *)skb_put(skb, sizeof(*evt));
2125 evt->ncmd = 0x01;
2126 evt->opcode = cpu_to_le16(opcode);
2127
2128 *skb_put(skb, 1) = 0x00;
2129
2130 bt_cb(skb)->pkt_type = HCI_EVENT_PKT;
2131
2132 return hci_recv_frame(hdev, skb);
2133}
2134
2135static int btusb_recv_bulk_intel(struct btusb_data *data, void *buffer,
2136 int count)
2137{
2138 /* When the device is in bootloader mode, then it can send
2139 * events via the bulk endpoint. These events are treated the
2140 * same way as the ones received from the interrupt endpoint.
2141 */
2142 if (test_bit(BTUSB_BOOTLOADER, &data->flags))
2143 return btusb_recv_intr(data, buffer, count);
2144
2145 return btusb_recv_bulk(data, buffer, count);
2146}
2147
Marcel Holtmannccd6da22015-04-09 00:35:19 -07002148static void btusb_intel_bootup(struct btusb_data *data, const void *ptr,
2149 unsigned int len)
2150{
2151 const struct intel_bootup *evt = ptr;
2152
2153 if (len != sizeof(*evt))
2154 return;
2155
2156 if (test_and_clear_bit(BTUSB_BOOTING, &data->flags)) {
2157 smp_mb__after_atomic();
2158 wake_up_bit(&data->flags, BTUSB_BOOTING);
2159 }
2160}
2161
2162static void btusb_intel_secure_send_result(struct btusb_data *data,
2163 const void *ptr, unsigned int len)
2164{
2165 const struct intel_secure_send_result *evt = ptr;
2166
2167 if (len != sizeof(*evt))
2168 return;
2169
2170 if (evt->result)
2171 set_bit(BTUSB_FIRMWARE_FAILED, &data->flags);
2172
2173 if (test_and_clear_bit(BTUSB_DOWNLOADING, &data->flags) &&
2174 test_bit(BTUSB_FIRMWARE_LOADED, &data->flags)) {
2175 smp_mb__after_atomic();
2176 wake_up_bit(&data->flags, BTUSB_DOWNLOADING);
2177 }
2178}
2179
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002180static int btusb_recv_event_intel(struct hci_dev *hdev, struct sk_buff *skb)
2181{
2182 struct btusb_data *data = hci_get_drvdata(hdev);
2183
2184 if (test_bit(BTUSB_BOOTLOADER, &data->flags)) {
2185 struct hci_event_hdr *hdr = (void *)skb->data;
2186
Marcel Holtmannccd6da22015-04-09 00:35:19 -07002187 if (skb->len > HCI_EVENT_HDR_SIZE && hdr->evt == 0xff &&
2188 hdr->plen > 0) {
2189 const void *ptr = skb->data + HCI_EVENT_HDR_SIZE + 1;
2190 unsigned int len = skb->len - HCI_EVENT_HDR_SIZE - 1;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002191
Marcel Holtmannccd6da22015-04-09 00:35:19 -07002192 switch (skb->data[2]) {
2193 case 0x02:
2194 /* When switching to the operational firmware
2195 * the device sends a vendor specific event
2196 * indicating that the bootup completed.
2197 */
2198 btusb_intel_bootup(data, ptr, len);
2199 break;
2200 case 0x06:
2201 /* When the firmware loading completes the
2202 * device sends out a vendor specific event
2203 * indicating the result of the firmware
2204 * loading.
2205 */
2206 btusb_intel_secure_send_result(data, ptr, len);
2207 break;
Johan Hedbergfad70972015-01-30 10:58:55 +02002208 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002209 }
2210 }
2211
2212 return hci_recv_frame(hdev, skb);
2213}
2214
2215static int btusb_send_frame_intel(struct hci_dev *hdev, struct sk_buff *skb)
2216{
2217 struct btusb_data *data = hci_get_drvdata(hdev);
2218 struct urb *urb;
2219
2220 BT_DBG("%s", hdev->name);
2221
2222 if (!test_bit(HCI_RUNNING, &hdev->flags))
2223 return -EBUSY;
2224
2225 switch (bt_cb(skb)->pkt_type) {
2226 case HCI_COMMAND_PKT:
2227 if (test_bit(BTUSB_BOOTLOADER, &data->flags)) {
2228 struct hci_command_hdr *cmd = (void *)skb->data;
2229 __u16 opcode = le16_to_cpu(cmd->opcode);
2230
2231 /* When in bootloader mode and the command 0xfc09
2232 * is received, it needs to be send down the
2233 * bulk endpoint. So allocate a bulk URB instead.
2234 */
2235 if (opcode == 0xfc09)
2236 urb = alloc_bulk_urb(hdev, skb);
2237 else
2238 urb = alloc_ctrl_urb(hdev, skb);
2239
2240 /* When the 0xfc01 command is issued to boot into
2241 * the operational firmware, it will actually not
2242 * send a command complete event. To keep the flow
2243 * control working inject that event here.
2244 */
2245 if (opcode == 0xfc01)
2246 inject_cmd_complete(hdev, opcode);
2247 } else {
2248 urb = alloc_ctrl_urb(hdev, skb);
2249 }
2250 if (IS_ERR(urb))
2251 return PTR_ERR(urb);
2252
2253 hdev->stat.cmd_tx++;
2254 return submit_or_queue_tx_urb(hdev, urb);
2255
2256 case HCI_ACLDATA_PKT:
2257 urb = alloc_bulk_urb(hdev, skb);
2258 if (IS_ERR(urb))
2259 return PTR_ERR(urb);
2260
2261 hdev->stat.acl_tx++;
2262 return submit_or_queue_tx_urb(hdev, urb);
2263
2264 case HCI_SCODATA_PKT:
2265 if (hci_conn_num(hdev, SCO_LINK) < 1)
2266 return -ENODEV;
2267
2268 urb = alloc_isoc_urb(hdev, skb);
2269 if (IS_ERR(urb))
2270 return PTR_ERR(urb);
2271
2272 hdev->stat.sco_tx++;
2273 return submit_tx_urb(hdev, urb);
2274 }
2275
2276 return -EILSEQ;
2277}
2278
2279static int btusb_intel_secure_send(struct hci_dev *hdev, u8 fragment_type,
2280 u32 plen, const void *param)
2281{
2282 while (plen > 0) {
2283 struct sk_buff *skb;
2284 u8 cmd_param[253], fragment_len = (plen > 252) ? 252 : plen;
2285
2286 cmd_param[0] = fragment_type;
2287 memcpy(cmd_param + 1, param, fragment_len);
2288
2289 skb = __hci_cmd_sync(hdev, 0xfc09, fragment_len + 1,
2290 cmd_param, HCI_INIT_TIMEOUT);
2291 if (IS_ERR(skb))
2292 return PTR_ERR(skb);
2293
2294 kfree_skb(skb);
2295
2296 plen -= fragment_len;
2297 param += fragment_len;
2298 }
2299
2300 return 0;
2301}
2302
2303static void btusb_intel_version_info(struct hci_dev *hdev,
2304 struct intel_version *ver)
2305{
2306 const char *variant;
2307
2308 switch (ver->fw_variant) {
2309 case 0x06:
2310 variant = "Bootloader";
2311 break;
2312 case 0x23:
2313 variant = "Firmware";
2314 break;
2315 default:
2316 return;
2317 }
2318
2319 BT_INFO("%s: %s revision %u.%u build %u week %u %u", hdev->name,
2320 variant, ver->fw_revision >> 4, ver->fw_revision & 0x0f,
2321 ver->fw_build_num, ver->fw_build_ww, 2000 + ver->fw_build_yy);
2322}
2323
2324static int btusb_setup_intel_new(struct hci_dev *hdev)
2325{
2326 static const u8 reset_param[] = { 0x00, 0x01, 0x00, 0x01,
2327 0x00, 0x08, 0x04, 0x00 };
2328 struct btusb_data *data = hci_get_drvdata(hdev);
2329 struct sk_buff *skb;
2330 struct intel_version *ver;
2331 struct intel_boot_params *params;
2332 const struct firmware *fw;
2333 const u8 *fw_ptr;
2334 char fwname[64];
2335 ktime_t calltime, delta, rettime;
2336 unsigned long long duration;
2337 int err;
2338
2339 BT_DBG("%s", hdev->name);
2340
2341 calltime = ktime_get();
2342
2343 /* Read the Intel version information to determine if the device
2344 * is in bootloader mode or if it already has operational firmware
2345 * loaded.
2346 */
2347 skb = __hci_cmd_sync(hdev, 0xfc05, 0, NULL, HCI_INIT_TIMEOUT);
2348 if (IS_ERR(skb)) {
2349 BT_ERR("%s: Reading Intel version information failed (%ld)",
2350 hdev->name, PTR_ERR(skb));
2351 return PTR_ERR(skb);
2352 }
2353
2354 if (skb->len != sizeof(*ver)) {
2355 BT_ERR("%s: Intel version event size mismatch", hdev->name);
2356 kfree_skb(skb);
2357 return -EILSEQ;
2358 }
2359
2360 ver = (struct intel_version *)skb->data;
2361 if (ver->status) {
2362 BT_ERR("%s: Intel version command failure (%02x)",
2363 hdev->name, ver->status);
2364 err = -bt_to_errno(ver->status);
2365 kfree_skb(skb);
2366 return err;
2367 }
2368
2369 /* The hardware platform number has a fixed value of 0x37 and
2370 * for now only accept this single value.
2371 */
2372 if (ver->hw_platform != 0x37) {
2373 BT_ERR("%s: Unsupported Intel hardware platform (%u)",
2374 hdev->name, ver->hw_platform);
2375 kfree_skb(skb);
2376 return -EINVAL;
2377 }
2378
2379 /* At the moment only the hardware variant iBT 3.0 (LnP/SfP) is
2380 * supported by this firmware loading method. This check has been
2381 * put in place to ensure correct forward compatibility options
2382 * when newer hardware variants come along.
2383 */
2384 if (ver->hw_variant != 0x0b) {
2385 BT_ERR("%s: Unsupported Intel hardware variant (%u)",
2386 hdev->name, ver->hw_variant);
2387 kfree_skb(skb);
2388 return -EINVAL;
2389 }
2390
2391 btusb_intel_version_info(hdev, ver);
2392
2393 /* The firmware variant determines if the device is in bootloader
2394 * mode or is running operational firmware. The value 0x06 identifies
2395 * the bootloader and the value 0x23 identifies the operational
2396 * firmware.
2397 *
2398 * When the operational firmware is already present, then only
2399 * the check for valid Bluetooth device address is needed. This
2400 * determines if the device will be added as configured or
2401 * unconfigured controller.
2402 *
2403 * It is not possible to use the Secure Boot Parameters in this
2404 * case since that command is only available in bootloader mode.
2405 */
2406 if (ver->fw_variant == 0x23) {
2407 kfree_skb(skb);
2408 clear_bit(BTUSB_BOOTLOADER, &data->flags);
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07002409 btintel_check_bdaddr(hdev);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002410 return 0;
2411 }
2412
2413 /* If the device is not in bootloader mode, then the only possible
2414 * choice is to return an error and abort the device initialization.
2415 */
2416 if (ver->fw_variant != 0x06) {
2417 BT_ERR("%s: Unsupported Intel firmware variant (%u)",
2418 hdev->name, ver->fw_variant);
2419 kfree_skb(skb);
2420 return -ENODEV;
2421 }
2422
2423 kfree_skb(skb);
2424
2425 /* Read the secure boot parameters to identify the operating
2426 * details of the bootloader.
2427 */
2428 skb = __hci_cmd_sync(hdev, 0xfc0d, 0, NULL, HCI_INIT_TIMEOUT);
2429 if (IS_ERR(skb)) {
2430 BT_ERR("%s: Reading Intel boot parameters failed (%ld)",
2431 hdev->name, PTR_ERR(skb));
2432 return PTR_ERR(skb);
2433 }
2434
2435 if (skb->len != sizeof(*params)) {
2436 BT_ERR("%s: Intel boot parameters size mismatch", hdev->name);
2437 kfree_skb(skb);
2438 return -EILSEQ;
2439 }
2440
2441 params = (struct intel_boot_params *)skb->data;
2442 if (params->status) {
2443 BT_ERR("%s: Intel boot parameters command failure (%02x)",
2444 hdev->name, params->status);
2445 err = -bt_to_errno(params->status);
2446 kfree_skb(skb);
2447 return err;
2448 }
2449
2450 BT_INFO("%s: Device revision is %u", hdev->name,
2451 le16_to_cpu(params->dev_revid));
2452
2453 BT_INFO("%s: Secure boot is %s", hdev->name,
2454 params->secure_boot ? "enabled" : "disabled");
2455
2456 BT_INFO("%s: Minimum firmware build %u week %u %u", hdev->name,
2457 params->min_fw_build_nn, params->min_fw_build_cw,
2458 2000 + params->min_fw_build_yy);
2459
2460 /* It is required that every single firmware fragment is acknowledged
2461 * with a command complete event. If the boot parameters indicate
2462 * that this bootloader does not send them, then abort the setup.
2463 */
2464 if (params->limited_cce != 0x00) {
2465 BT_ERR("%s: Unsupported Intel firmware loading method (%u)",
2466 hdev->name, params->limited_cce);
2467 kfree_skb(skb);
2468 return -EINVAL;
2469 }
2470
2471 /* If the OTP has no valid Bluetooth device address, then there will
2472 * also be no valid address for the operational firmware.
2473 */
2474 if (!bacmp(&params->otp_bdaddr, BDADDR_ANY)) {
2475 BT_INFO("%s: No device address configured", hdev->name);
2476 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
2477 }
2478
2479 /* With this Intel bootloader only the hardware variant and device
2480 * revision information are used to select the right firmware.
2481 *
2482 * Currently this bootloader support is limited to hardware variant
2483 * iBT 3.0 (LnP/SfP) which is identified by the value 11 (0x0b).
2484 */
2485 snprintf(fwname, sizeof(fwname), "intel/ibt-11-%u.sfi",
2486 le16_to_cpu(params->dev_revid));
2487
2488 err = request_firmware(&fw, fwname, &hdev->dev);
2489 if (err < 0) {
2490 BT_ERR("%s: Failed to load Intel firmware file (%d)",
2491 hdev->name, err);
2492 kfree_skb(skb);
2493 return err;
2494 }
2495
2496 BT_INFO("%s: Found device firmware: %s", hdev->name, fwname);
2497
2498 kfree_skb(skb);
2499
2500 if (fw->size < 644) {
2501 BT_ERR("%s: Invalid size of firmware file (%zu)",
2502 hdev->name, fw->size);
2503 err = -EBADF;
2504 goto done;
2505 }
2506
2507 set_bit(BTUSB_DOWNLOADING, &data->flags);
2508
2509 /* Start the firmware download transaction with the Init fragment
2510 * represented by the 128 bytes of CSS header.
2511 */
2512 err = btusb_intel_secure_send(hdev, 0x00, 128, fw->data);
2513 if (err < 0) {
2514 BT_ERR("%s: Failed to send firmware header (%d)",
2515 hdev->name, err);
2516 goto done;
2517 }
2518
2519 /* Send the 256 bytes of public key information from the firmware
2520 * as the PKey fragment.
2521 */
2522 err = btusb_intel_secure_send(hdev, 0x03, 256, fw->data + 128);
2523 if (err < 0) {
2524 BT_ERR("%s: Failed to send firmware public key (%d)",
2525 hdev->name, err);
2526 goto done;
2527 }
2528
2529 /* Send the 256 bytes of signature information from the firmware
2530 * as the Sign fragment.
2531 */
2532 err = btusb_intel_secure_send(hdev, 0x02, 256, fw->data + 388);
2533 if (err < 0) {
2534 BT_ERR("%s: Failed to send firmware signature (%d)",
2535 hdev->name, err);
2536 goto done;
2537 }
2538
2539 fw_ptr = fw->data + 644;
2540
2541 while (fw_ptr - fw->data < fw->size) {
2542 struct hci_command_hdr *cmd = (void *)fw_ptr;
2543 u8 cmd_len;
2544
2545 cmd_len = sizeof(*cmd) + cmd->plen;
2546
2547 /* Send each command from the firmware data buffer as
2548 * a single Data fragment.
2549 */
2550 err = btusb_intel_secure_send(hdev, 0x01, cmd_len, fw_ptr);
2551 if (err < 0) {
2552 BT_ERR("%s: Failed to send firmware data (%d)",
2553 hdev->name, err);
2554 goto done;
2555 }
2556
2557 fw_ptr += cmd_len;
2558 }
2559
Marcel Holtmannce6bb922015-01-28 01:58:40 -08002560 set_bit(BTUSB_FIRMWARE_LOADED, &data->flags);
2561
Johan Hedberga087a982015-01-30 10:58:54 +02002562 BT_INFO("%s: Waiting for firmware download to complete", hdev->name);
2563
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002564 /* Before switching the device into operational mode and with that
2565 * booting the loaded firmware, wait for the bootloader notification
2566 * that all fragments have been successfully received.
2567 *
Johan Hedberga087a982015-01-30 10:58:54 +02002568 * When the event processing receives the notification, then the
2569 * BTUSB_DOWNLOADING flag will be cleared.
2570 *
2571 * The firmware loading should not take longer than 5 seconds
2572 * and thus just timeout if that happens and fail the setup
2573 * of this device.
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002574 */
Johan Hedberg129a7692015-02-14 09:33:35 +02002575 err = wait_on_bit_timeout(&data->flags, BTUSB_DOWNLOADING,
2576 TASK_INTERRUPTIBLE,
2577 msecs_to_jiffies(5000));
Johan Hedberga087a982015-01-30 10:58:54 +02002578 if (err == 1) {
2579 BT_ERR("%s: Firmware loading interrupted", hdev->name);
2580 err = -EINTR;
2581 goto done;
2582 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002583
Johan Hedberga087a982015-01-30 10:58:54 +02002584 if (err) {
2585 BT_ERR("%s: Firmware loading timeout", hdev->name);
2586 err = -ETIMEDOUT;
2587 goto done;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002588 }
2589
2590 if (test_bit(BTUSB_FIRMWARE_FAILED, &data->flags)) {
2591 BT_ERR("%s: Firmware loading failed", hdev->name);
2592 err = -ENOEXEC;
2593 goto done;
2594 }
2595
2596 rettime = ktime_get();
2597 delta = ktime_sub(rettime, calltime);
2598 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
2599
2600 BT_INFO("%s: Firmware loaded in %llu usecs", hdev->name, duration);
2601
2602done:
2603 release_firmware(fw);
2604
2605 if (err < 0)
2606 return err;
2607
2608 calltime = ktime_get();
2609
2610 set_bit(BTUSB_BOOTING, &data->flags);
2611
2612 skb = __hci_cmd_sync(hdev, 0xfc01, sizeof(reset_param), reset_param,
2613 HCI_INIT_TIMEOUT);
2614 if (IS_ERR(skb))
2615 return PTR_ERR(skb);
2616
2617 kfree_skb(skb);
2618
2619 /* The bootloader will not indicate when the device is ready. This
2620 * is done by the operational firmware sending bootup notification.
Johan Hedbergfad70972015-01-30 10:58:55 +02002621 *
2622 * Booting into operational firmware should not take longer than
2623 * 1 second. However if that happens, then just fail the setup
2624 * since something went wrong.
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002625 */
Johan Hedbergfad70972015-01-30 10:58:55 +02002626 BT_INFO("%s: Waiting for device to boot", hdev->name);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002627
Johan Hedberg129a7692015-02-14 09:33:35 +02002628 err = wait_on_bit_timeout(&data->flags, BTUSB_BOOTING,
2629 TASK_INTERRUPTIBLE,
2630 msecs_to_jiffies(1000));
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002631
Johan Hedbergfad70972015-01-30 10:58:55 +02002632 if (err == 1) {
2633 BT_ERR("%s: Device boot interrupted", hdev->name);
2634 return -EINTR;
2635 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002636
Johan Hedbergfad70972015-01-30 10:58:55 +02002637 if (err) {
2638 BT_ERR("%s: Device boot timeout", hdev->name);
2639 return -ETIMEDOUT;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002640 }
2641
2642 rettime = ktime_get();
2643 delta = ktime_sub(rettime, calltime);
2644 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
2645
2646 BT_INFO("%s: Device booted in %llu usecs", hdev->name, duration);
2647
2648 clear_bit(BTUSB_BOOTLOADER, &data->flags);
2649
2650 return 0;
2651}
2652
Marcel Holtmann385a7682015-01-28 11:09:56 -08002653static void btusb_hw_error_intel(struct hci_dev *hdev, u8 code)
2654{
2655 struct sk_buff *skb;
2656 u8 type = 0x00;
2657
2658 BT_ERR("%s: Hardware error 0x%2.2x", hdev->name, code);
2659
2660 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
2661 if (IS_ERR(skb)) {
2662 BT_ERR("%s: Reset after hardware error failed (%ld)",
2663 hdev->name, PTR_ERR(skb));
2664 return;
2665 }
2666 kfree_skb(skb);
2667
2668 skb = __hci_cmd_sync(hdev, 0xfc22, 1, &type, HCI_INIT_TIMEOUT);
2669 if (IS_ERR(skb)) {
2670 BT_ERR("%s: Retrieving Intel exception info failed (%ld)",
2671 hdev->name, PTR_ERR(skb));
2672 return;
2673 }
2674
2675 if (skb->len != 13) {
2676 BT_ERR("%s: Exception info size mismatch", hdev->name);
2677 kfree_skb(skb);
2678 return;
2679 }
2680
2681 if (skb->data[0] != 0x00) {
2682 BT_ERR("%s: Exception info command failure (%02x)",
2683 hdev->name, skb->data[0]);
2684 kfree_skb(skb);
2685 return;
2686 }
2687
2688 BT_ERR("%s: Exception info %s", hdev->name, (char *)(skb->data + 1));
2689
2690 kfree_skb(skb);
2691}
2692
Tedd Ho-Jeong Anbfbd45e2015-02-13 09:20:52 -08002693static int btusb_shutdown_intel(struct hci_dev *hdev)
2694{
2695 struct sk_buff *skb;
2696 long ret;
2697
2698 /* Some platforms have an issue with BT LED when the interface is
2699 * down or BT radio is turned off, which takes 5 seconds to BT LED
2700 * goes off. This command turns off the BT LED immediately.
2701 */
2702 skb = __hci_cmd_sync(hdev, 0xfc3f, 0, NULL, HCI_INIT_TIMEOUT);
2703 if (IS_ERR(skb)) {
2704 ret = PTR_ERR(skb);
2705 BT_ERR("%s: turning off Intel device LED failed (%ld)",
2706 hdev->name, ret);
2707 return ret;
2708 }
2709 kfree_skb(skb);
2710
2711 return 0;
2712}
2713
Amitkumar Karwarae8df492014-07-18 14:47:06 -07002714static int btusb_set_bdaddr_marvell(struct hci_dev *hdev,
2715 const bdaddr_t *bdaddr)
2716{
2717 struct sk_buff *skb;
2718 u8 buf[8];
2719 long ret;
2720
2721 buf[0] = 0xfe;
2722 buf[1] = sizeof(bdaddr_t);
2723 memcpy(buf + 2, bdaddr, sizeof(bdaddr_t));
2724
2725 skb = __hci_cmd_sync(hdev, 0xfc22, sizeof(buf), buf, HCI_INIT_TIMEOUT);
2726 if (IS_ERR(skb)) {
2727 ret = PTR_ERR(skb);
2728 BT_ERR("%s: changing Marvell device address failed (%ld)",
2729 hdev->name, ret);
2730 return ret;
2731 }
2732 kfree_skb(skb);
2733
2734 return 0;
2735}
2736
Toshi Kikuchi58592232014-12-12 10:58:05 -08002737static int btusb_set_bdaddr_ath3012(struct hci_dev *hdev,
2738 const bdaddr_t *bdaddr)
2739{
2740 struct sk_buff *skb;
2741 u8 buf[10];
2742 long ret;
2743
2744 buf[0] = 0x01;
2745 buf[1] = 0x01;
2746 buf[2] = 0x00;
2747 buf[3] = sizeof(bdaddr_t);
2748 memcpy(buf + 4, bdaddr, sizeof(bdaddr_t));
2749
2750 skb = __hci_cmd_sync(hdev, 0xfc0b, sizeof(buf), buf, HCI_INIT_TIMEOUT);
2751 if (IS_ERR(skb)) {
2752 ret = PTR_ERR(skb);
2753 BT_ERR("%s: Change address command failed (%ld)",
2754 hdev->name, ret);
2755 return ret;
2756 }
2757 kfree_skb(skb);
2758
2759 return 0;
2760}
2761
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002762#define QCA_DFU_PACKET_LEN 4096
2763
2764#define QCA_GET_TARGET_VERSION 0x09
2765#define QCA_CHECK_STATUS 0x05
2766#define QCA_DFU_DOWNLOAD 0x01
2767
2768#define QCA_SYSCFG_UPDATED 0x40
2769#define QCA_PATCH_UPDATED 0x80
2770#define QCA_DFU_TIMEOUT 3000
2771
2772struct qca_version {
2773 __le32 rom_version;
2774 __le32 patch_version;
2775 __le32 ram_version;
2776 __le32 ref_clock;
2777 __u8 reserved[4];
2778} __packed;
2779
2780struct qca_rampatch_version {
2781 __le16 rom_version;
2782 __le16 patch_version;
2783} __packed;
2784
2785struct qca_device_info {
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002786 u32 rom_version;
2787 u8 rampatch_hdr; /* length of header in rampatch */
2788 u8 nvm_hdr; /* length of header in NVM */
2789 u8 ver_offset; /* offset of version structure in rampatch */
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002790};
2791
2792static const struct qca_device_info qca_devices_table[] = {
2793 { 0x00000100, 20, 4, 10 }, /* Rome 1.0 */
2794 { 0x00000101, 20, 4, 10 }, /* Rome 1.1 */
2795 { 0x00000201, 28, 4, 18 }, /* Rome 2.1 */
2796 { 0x00000300, 28, 4, 18 }, /* Rome 3.0 */
2797 { 0x00000302, 28, 4, 18 }, /* Rome 3.2 */
2798};
2799
2800static int btusb_qca_send_vendor_req(struct hci_dev *hdev, u8 request,
2801 void *data, u16 size)
2802{
2803 struct btusb_data *btdata = hci_get_drvdata(hdev);
2804 struct usb_device *udev = btdata->udev;
2805 int pipe, err;
2806 u8 *buf;
2807
2808 buf = kmalloc(size, GFP_KERNEL);
2809 if (!buf)
2810 return -ENOMEM;
2811
2812 /* Found some of USB hosts have IOT issues with ours so that we should
2813 * not wait until HCI layer is ready.
2814 */
2815 pipe = usb_rcvctrlpipe(udev, 0);
2816 err = usb_control_msg(udev, pipe, request, USB_TYPE_VENDOR | USB_DIR_IN,
2817 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
2818 if (err < 0) {
2819 BT_ERR("%s: Failed to access otp area (%d)", hdev->name, err);
2820 goto done;
2821 }
2822
2823 memcpy(data, buf, size);
2824
2825done:
2826 kfree(buf);
2827
2828 return err;
2829}
2830
2831static int btusb_setup_qca_download_fw(struct hci_dev *hdev,
2832 const struct firmware *firmware,
2833 size_t hdr_size)
2834{
2835 struct btusb_data *btdata = hci_get_drvdata(hdev);
2836 struct usb_device *udev = btdata->udev;
2837 size_t count, size, sent = 0;
2838 int pipe, len, err;
2839 u8 *buf;
2840
2841 buf = kmalloc(QCA_DFU_PACKET_LEN, GFP_KERNEL);
2842 if (!buf)
2843 return -ENOMEM;
2844
2845 count = firmware->size;
2846
2847 size = min_t(size_t, count, hdr_size);
2848 memcpy(buf, firmware->data, size);
2849
2850 /* USB patches should go down to controller through USB path
2851 * because binary format fits to go down through USB channel.
2852 * USB control path is for patching headers and USB bulk is for
2853 * patch body.
2854 */
2855 pipe = usb_sndctrlpipe(udev, 0);
2856 err = usb_control_msg(udev, pipe, QCA_DFU_DOWNLOAD, USB_TYPE_VENDOR,
2857 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
2858 if (err < 0) {
2859 BT_ERR("%s: Failed to send headers (%d)", hdev->name, err);
2860 goto done;
2861 }
2862
2863 sent += size;
2864 count -= size;
2865
2866 while (count) {
2867 size = min_t(size_t, count, QCA_DFU_PACKET_LEN);
2868
2869 memcpy(buf, firmware->data + sent, size);
2870
2871 pipe = usb_sndbulkpipe(udev, 0x02);
2872 err = usb_bulk_msg(udev, pipe, buf, size, &len,
2873 QCA_DFU_TIMEOUT);
2874 if (err < 0) {
2875 BT_ERR("%s: Failed to send body at %zd of %zd (%d)",
2876 hdev->name, sent, firmware->size, err);
2877 break;
2878 }
2879
2880 if (size != len) {
2881 BT_ERR("%s: Failed to get bulk buffer", hdev->name);
2882 err = -EILSEQ;
2883 break;
2884 }
2885
2886 sent += size;
2887 count -= size;
2888 }
2889
2890done:
2891 kfree(buf);
2892 return err;
2893}
2894
2895static int btusb_setup_qca_load_rampatch(struct hci_dev *hdev,
2896 struct qca_version *ver,
2897 const struct qca_device_info *info)
2898{
2899 struct qca_rampatch_version *rver;
2900 const struct firmware *fw;
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002901 u32 ver_rom, ver_patch;
2902 u16 rver_rom, rver_patch;
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002903 char fwname[64];
2904 int err;
2905
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002906 ver_rom = le32_to_cpu(ver->rom_version);
2907 ver_patch = le32_to_cpu(ver->patch_version);
2908
2909 snprintf(fwname, sizeof(fwname), "qca/rampatch_usb_%08x.bin", ver_rom);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002910
2911 err = request_firmware(&fw, fwname, &hdev->dev);
2912 if (err) {
2913 BT_ERR("%s: failed to request rampatch file: %s (%d)",
2914 hdev->name, fwname, err);
2915 return err;
2916 }
2917
2918 BT_INFO("%s: using rampatch file: %s", hdev->name, fwname);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002919
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002920 rver = (struct qca_rampatch_version *)(fw->data + info->ver_offset);
2921 rver_rom = le16_to_cpu(rver->rom_version);
2922 rver_patch = le16_to_cpu(rver->patch_version);
2923
2924 BT_INFO("%s: QCA: patch rome 0x%x build 0x%x, firmware rome 0x%x "
2925 "build 0x%x", hdev->name, rver_rom, rver_patch, ver_rom,
2926 ver_patch);
2927
2928 if (rver_rom != ver_rom || rver_patch <= ver_patch) {
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002929 BT_ERR("%s: rampatch file version did not match with firmware",
2930 hdev->name);
2931 err = -EINVAL;
2932 goto done;
2933 }
2934
2935 err = btusb_setup_qca_download_fw(hdev, fw, info->rampatch_hdr);
2936
2937done:
2938 release_firmware(fw);
2939
2940 return err;
2941}
2942
2943static int btusb_setup_qca_load_nvm(struct hci_dev *hdev,
2944 struct qca_version *ver,
2945 const struct qca_device_info *info)
2946{
2947 const struct firmware *fw;
2948 char fwname[64];
2949 int err;
2950
2951 snprintf(fwname, sizeof(fwname), "qca/nvm_usb_%08x.bin",
2952 le32_to_cpu(ver->rom_version));
2953
2954 err = request_firmware(&fw, fwname, &hdev->dev);
2955 if (err) {
2956 BT_ERR("%s: failed to request NVM file: %s (%d)",
2957 hdev->name, fwname, err);
2958 return err;
2959 }
2960
2961 BT_INFO("%s: using NVM file: %s", hdev->name, fwname);
2962
2963 err = btusb_setup_qca_download_fw(hdev, fw, info->nvm_hdr);
2964
2965 release_firmware(fw);
2966
2967 return err;
2968}
2969
2970static int btusb_setup_qca(struct hci_dev *hdev)
2971{
2972 const struct qca_device_info *info = NULL;
2973 struct qca_version ver;
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002974 u32 ver_rom;
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002975 u8 status;
2976 int i, err;
2977
2978 err = btusb_qca_send_vendor_req(hdev, QCA_GET_TARGET_VERSION, &ver,
Marcel Holtmanneb500422015-04-16 23:15:50 +02002979 sizeof(ver));
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002980 if (err < 0)
2981 return err;
2982
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002983 ver_rom = le32_to_cpu(ver.rom_version);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002984 for (i = 0; i < ARRAY_SIZE(qca_devices_table); i++) {
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002985 if (ver_rom == qca_devices_table[i].rom_version)
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002986 info = &qca_devices_table[i];
2987 }
2988 if (!info) {
2989 BT_ERR("%s: don't support firmware rome 0x%x", hdev->name,
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002990 ver_rom);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002991 return -ENODEV;
2992 }
2993
2994 err = btusb_qca_send_vendor_req(hdev, QCA_CHECK_STATUS, &status,
2995 sizeof(status));
2996 if (err < 0)
2997 return err;
2998
2999 if (!(status & QCA_PATCH_UPDATED)) {
3000 err = btusb_setup_qca_load_rampatch(hdev, &ver, info);
3001 if (err < 0)
3002 return err;
3003 }
3004
3005 if (!(status & QCA_SYSCFG_UPDATED)) {
3006 err = btusb_setup_qca_load_nvm(hdev, &ver, info);
3007 if (err < 0)
3008 return err;
3009 }
3010
3011 return 0;
3012}
3013
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003014static int btusb_probe(struct usb_interface *intf,
Marcel Holtmann89e75332014-09-16 04:44:50 +02003015 const struct usb_device_id *id)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003016{
3017 struct usb_endpoint_descriptor *ep_desc;
3018 struct btusb_data *data;
3019 struct hci_dev *hdev;
3020 int i, err;
3021
3022 BT_DBG("intf %p id %p", intf, id);
3023
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02003024 /* interface numbers are hardcoded in the spec */
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003025 if (intf->cur_altsetting->desc.bInterfaceNumber != 0)
3026 return -ENODEV;
3027
3028 if (!id->driver_info) {
3029 const struct usb_device_id *match;
Marcel Holtmann89e75332014-09-16 04:44:50 +02003030
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003031 match = usb_match_id(intf, blacklist_table);
3032 if (match)
3033 id = match;
3034 }
3035
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02003036 if (id->driver_info == BTUSB_IGNORE)
3037 return -ENODEV;
3038
Steven.Li2d25f8b2011-07-01 14:02:36 +08003039 if (id->driver_info & BTUSB_ATH3012) {
3040 struct usb_device *udev = interface_to_usbdev(intf);
3041
3042 /* Old firmware would otherwise let ath3k driver load
3043 * patch and sysconfig files */
3044 if (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x0001)
3045 return -ENODEV;
3046 }
3047
Sachin Kamat98921db2012-07-27 12:38:39 +05303048 data = devm_kzalloc(&intf->dev, sizeof(*data), GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003049 if (!data)
3050 return -ENOMEM;
3051
3052 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
3053 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
3054
3055 if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
3056 data->intr_ep = ep_desc;
3057 continue;
3058 }
3059
3060 if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
3061 data->bulk_tx_ep = ep_desc;
3062 continue;
3063 }
3064
3065 if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
3066 data->bulk_rx_ep = ep_desc;
3067 continue;
3068 }
3069 }
3070
Sachin Kamat98921db2012-07-27 12:38:39 +05303071 if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003072 return -ENODEV;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003073
Marcel Holtmann893ba542015-01-28 20:27:34 -08003074 if (id->driver_info & BTUSB_AMP) {
3075 data->cmdreq_type = USB_TYPE_CLASS | 0x01;
3076 data->cmdreq = 0x2b;
3077 } else {
3078 data->cmdreq_type = USB_TYPE_CLASS;
3079 data->cmdreq = 0x00;
3080 }
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01003081
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003082 data->udev = interface_to_usbdev(intf);
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02003083 data->intf = intf;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003084
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003085 INIT_WORK(&data->work, btusb_work);
Oliver Neukum7bee5492009-08-24 23:44:59 +02003086 INIT_WORK(&data->waker, btusb_waker);
Marcel Holtmann803b5832014-09-16 08:00:29 +02003087 init_usb_anchor(&data->deferred);
3088 init_usb_anchor(&data->tx_anchor);
Oliver Neukum7bee5492009-08-24 23:44:59 +02003089 spin_lock_init(&data->txlock);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003090
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003091 init_usb_anchor(&data->intr_anchor);
3092 init_usb_anchor(&data->bulk_anchor);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02003093 init_usb_anchor(&data->isoc_anchor);
Marcel Holtmann803b5832014-09-16 08:00:29 +02003094 spin_lock_init(&data->rxlock);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003095
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08003096 if (id->driver_info & BTUSB_INTEL_NEW) {
3097 data->recv_event = btusb_recv_event_intel;
3098 data->recv_bulk = btusb_recv_bulk_intel;
3099 set_bit(BTUSB_BOOTLOADER, &data->flags);
3100 } else {
3101 data->recv_event = hci_recv_frame;
3102 data->recv_bulk = btusb_recv_bulk;
3103 }
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +01003104
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003105 hdev = hci_alloc_dev();
Sachin Kamat98921db2012-07-27 12:38:39 +05303106 if (!hdev)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003107 return -ENOMEM;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003108
Marcel Holtmannc13854c2010-02-08 15:27:07 +01003109 hdev->bus = HCI_USB;
David Herrmann155961e2012-02-09 21:58:32 +01003110 hci_set_drvdata(hdev, data);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003111
Marcel Holtmann893ba542015-01-28 20:27:34 -08003112 if (id->driver_info & BTUSB_AMP)
3113 hdev->dev_type = HCI_AMP;
3114 else
3115 hdev->dev_type = HCI_BREDR;
3116
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003117 data->hdev = hdev;
3118
3119 SET_HCIDEV_DEV(hdev, &intf->dev);
3120
Marcel Holtmann9f8f9622013-04-10 08:11:35 -07003121 hdev->open = btusb_open;
3122 hdev->close = btusb_close;
3123 hdev->flush = btusb_flush;
3124 hdev->send = btusb_send_frame;
3125 hdev->notify = btusb_notify;
3126
3127 if (id->driver_info & BTUSB_BCM92035)
3128 hdev->setup = btusb_setup_bcm92035;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003129
Marcel Holtmannc2bfb102015-04-05 22:52:14 -07003130#ifdef CONFIG_BT_HCIBTUSB_BCM
Marcel Holtmannabbaf502014-07-02 00:53:48 +02003131 if (id->driver_info & BTUSB_BCM_PATCHRAM) {
Marcel Holtmannc2bfb102015-04-05 22:52:14 -07003132 hdev->setup = btbcm_setup_patchram;
Marcel Holtmann1df1f592015-04-05 22:52:11 -07003133 hdev->set_bdaddr = btbcm_set_bdaddr;
Marcel Holtmannabbaf502014-07-02 00:53:48 +02003134 }
Petri Gynther10d4c672014-05-08 15:50:01 -07003135
Marcel Holtmann941521e2015-04-05 22:52:15 -07003136 if (id->driver_info & BTUSB_BCM_APPLE)
Marcel Holtmannc2bfb102015-04-05 22:52:14 -07003137 hdev->setup = btbcm_setup_apple;
Marcel Holtmannc2bfb102015-04-05 22:52:14 -07003138#endif
Marcel Holtmann17b27722015-03-22 15:52:38 +01003139
Marcel Holtmanncb8d6592014-07-02 11:25:25 +02003140 if (id->driver_info & BTUSB_INTEL) {
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07003141 hdev->setup = btusb_setup_intel;
Tedd Ho-Jeong Anbfbd45e2015-02-13 09:20:52 -08003142 hdev->shutdown = btusb_shutdown_intel;
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07003143 hdev->set_bdaddr = btintel_set_bdaddr;
Jakub Pawlowskic33fb9b2015-01-30 18:55:58 -08003144 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
Jakub Pawlowskic1154842015-03-17 09:04:16 -07003145 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
Marcel Holtmanncb8d6592014-07-02 11:25:25 +02003146 }
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07003147
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08003148 if (id->driver_info & BTUSB_INTEL_NEW) {
3149 hdev->send = btusb_send_frame_intel;
3150 hdev->setup = btusb_setup_intel_new;
Marcel Holtmann385a7682015-01-28 11:09:56 -08003151 hdev->hw_error = btusb_hw_error_intel;
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07003152 hdev->set_bdaddr = btintel_set_bdaddr;
Marcel Holtmannb970c5b2015-02-01 23:57:18 -08003153 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08003154 }
3155
Amitkumar Karwarae8df492014-07-18 14:47:06 -07003156 if (id->driver_info & BTUSB_MARVELL)
3157 hdev->set_bdaddr = btusb_set_bdaddr_marvell;
3158
Marcel Holtmann661cf882015-01-02 23:35:20 -08003159 if (id->driver_info & BTUSB_SWAVE) {
3160 set_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks);
Marcel Holtmannd57dbe72014-12-26 04:42:33 +01003161 set_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS, &hdev->quirks);
Marcel Holtmann661cf882015-01-02 23:35:20 -08003162 }
Marcel Holtmannd57dbe72014-12-26 04:42:33 +01003163
Marcel Holtmann40df7832014-07-06 13:29:58 +02003164 if (id->driver_info & BTUSB_INTEL_BOOT)
3165 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
3166
Jakub Pawlowski79f0c872015-01-29 10:38:34 -08003167 if (id->driver_info & BTUSB_ATH3012) {
Toshi Kikuchi58592232014-12-12 10:58:05 -08003168 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
Jakub Pawlowski3d50d512015-03-17 09:04:15 -07003169 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
Jakub Pawlowski79f0c872015-01-29 10:38:34 -08003170 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
3171 }
Toshi Kikuchi58592232014-12-12 10:58:05 -08003172
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003173 if (id->driver_info & BTUSB_QCA_ROME) {
3174 data->setup_on_usb = btusb_setup_qca;
3175 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
3176 }
3177
Daniel Drakea2698a92015-04-16 14:09:55 -06003178 if (id->driver_info & BTUSB_REALTEK)
3179 hdev->setup = btusb_setup_realtek;
3180
Marcel Holtmann893ba542015-01-28 20:27:34 -08003181 if (id->driver_info & BTUSB_AMP) {
3182 /* AMP controllers do not support SCO packets */
3183 data->isoc = NULL;
3184 } else {
3185 /* Interface numbers are hardcoded in the specification */
3186 data->isoc = usb_ifnum_to_if(data->udev, 1);
3187 }
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02003188
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01003189 if (!reset)
Szymon Janca6c511c2012-05-23 12:35:46 +02003190 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02003191
3192 if (force_scofix || id->driver_info & BTUSB_WRONG_SCO_MTU) {
3193 if (!disable_scofix)
3194 set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
3195 }
3196
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02003197 if (id->driver_info & BTUSB_BROKEN_ISOC)
3198 data->isoc = NULL;
3199
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01003200 if (id->driver_info & BTUSB_DIGIANSWER) {
3201 data->cmdreq_type = USB_TYPE_VENDOR;
Szymon Janca6c511c2012-05-23 12:35:46 +02003202 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01003203 }
3204
3205 if (id->driver_info & BTUSB_CSR) {
3206 struct usb_device *udev = data->udev;
Marcel Holtmann81cac642014-01-03 03:02:36 -08003207 u16 bcdDevice = le16_to_cpu(udev->descriptor.bcdDevice);
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01003208
3209 /* Old firmware would otherwise execute USB reset */
Marcel Holtmann81cac642014-01-03 03:02:36 -08003210 if (bcdDevice < 0x117)
Szymon Janca6c511c2012-05-23 12:35:46 +02003211 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmann81cac642014-01-03 03:02:36 -08003212
3213 /* Fake CSR devices with broken commands */
3214 if (bcdDevice <= 0x100)
3215 hdev->setup = btusb_setup_csr;
Jakub Pawlowski49c989a2015-03-17 09:04:17 -07003216
3217 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01003218 }
3219
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02003220 if (id->driver_info & BTUSB_SNIFFER) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02003221 struct usb_device *udev = data->udev;
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02003222
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01003223 /* New sniffer firmware has crippled HCI interface */
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02003224 if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
3225 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
3226 }
3227
Marcel Holtmann3a5ef202014-07-06 14:53:54 +02003228 if (id->driver_info & BTUSB_INTEL_BOOT) {
3229 /* A bug in the bootloader causes that interrupt interface is
3230 * only enabled after receiving SetInterface(0, AltSetting=0).
3231 */
3232 err = usb_set_interface(data->udev, 0, 0);
3233 if (err < 0) {
3234 BT_ERR("failed to set interface 0, alt 0 %d", err);
3235 hci_free_dev(hdev);
3236 return err;
3237 }
3238 }
3239
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02003240 if (data->isoc) {
3241 err = usb_driver_claim_interface(&btusb_driver,
Marcel Holtmann89e75332014-09-16 04:44:50 +02003242 data->isoc, data);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02003243 if (err < 0) {
3244 hci_free_dev(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02003245 return err;
3246 }
3247 }
3248
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003249 err = hci_register_dev(hdev);
3250 if (err < 0) {
3251 hci_free_dev(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003252 return err;
3253 }
3254
3255 usb_set_intfdata(intf, data);
3256
3257 return 0;
3258}
3259
3260static void btusb_disconnect(struct usb_interface *intf)
3261{
3262 struct btusb_data *data = usb_get_intfdata(intf);
3263 struct hci_dev *hdev;
3264
3265 BT_DBG("intf %p", intf);
3266
3267 if (!data)
3268 return;
3269
3270 hdev = data->hdev;
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02003271 usb_set_intfdata(data->intf, NULL);
3272
3273 if (data->isoc)
3274 usb_set_intfdata(data->isoc, NULL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003275
3276 hci_unregister_dev(hdev);
3277
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02003278 if (intf == data->isoc)
3279 usb_driver_release_interface(&btusb_driver, data->intf);
3280 else if (data->isoc)
3281 usb_driver_release_interface(&btusb_driver, data->isoc);
3282
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003283 hci_free_dev(hdev);
3284}
3285
Oliver Neukum7bee5492009-08-24 23:44:59 +02003286#ifdef CONFIG_PM
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003287static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
3288{
3289 struct btusb_data *data = usb_get_intfdata(intf);
3290
3291 BT_DBG("intf %p", intf);
3292
3293 if (data->suspend_count++)
3294 return 0;
3295
Oliver Neukum7bee5492009-08-24 23:44:59 +02003296 spin_lock_irq(&data->txlock);
Alan Stern5b1b0b82011-08-19 23:49:48 +02003297 if (!(PMSG_IS_AUTO(message) && data->tx_in_flight)) {
Oliver Neukum7bee5492009-08-24 23:44:59 +02003298 set_bit(BTUSB_SUSPENDING, &data->flags);
3299 spin_unlock_irq(&data->txlock);
3300 } else {
3301 spin_unlock_irq(&data->txlock);
3302 data->suspend_count--;
3303 return -EBUSY;
3304 }
3305
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003306 cancel_work_sync(&data->work);
3307
Oliver Neukum7bee5492009-08-24 23:44:59 +02003308 btusb_stop_traffic(data);
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003309 usb_kill_anchored_urbs(&data->tx_anchor);
3310
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003311 return 0;
3312}
3313
Oliver Neukum7bee5492009-08-24 23:44:59 +02003314static void play_deferred(struct btusb_data *data)
3315{
3316 struct urb *urb;
3317 int err;
3318
3319 while ((urb = usb_get_from_anchor(&data->deferred))) {
3320 err = usb_submit_urb(urb, GFP_ATOMIC);
3321 if (err < 0)
3322 break;
3323
3324 data->tx_in_flight++;
3325 }
3326 usb_scuttle_anchored_urbs(&data->deferred);
3327}
3328
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003329static int btusb_resume(struct usb_interface *intf)
3330{
3331 struct btusb_data *data = usb_get_intfdata(intf);
3332 struct hci_dev *hdev = data->hdev;
Oliver Neukum7bee5492009-08-24 23:44:59 +02003333 int err = 0;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003334
3335 BT_DBG("intf %p", intf);
3336
3337 if (--data->suspend_count)
3338 return 0;
3339
3340 if (!test_bit(HCI_RUNNING, &hdev->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +02003341 goto done;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003342
3343 if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
3344 err = btusb_submit_intr_urb(hdev, GFP_NOIO);
3345 if (err < 0) {
3346 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02003347 goto failed;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003348 }
3349 }
3350
3351 if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) {
Marcel Holtmann43c2e572009-02-04 17:41:38 +01003352 err = btusb_submit_bulk_urb(hdev, GFP_NOIO);
3353 if (err < 0) {
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003354 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02003355 goto failed;
3356 }
3357
3358 btusb_submit_bulk_urb(hdev, GFP_NOIO);
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003359 }
3360
3361 if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
3362 if (btusb_submit_isoc_urb(hdev, GFP_NOIO) < 0)
3363 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
3364 else
3365 btusb_submit_isoc_urb(hdev, GFP_NOIO);
3366 }
3367
Oliver Neukum7bee5492009-08-24 23:44:59 +02003368 spin_lock_irq(&data->txlock);
3369 play_deferred(data);
3370 clear_bit(BTUSB_SUSPENDING, &data->flags);
3371 spin_unlock_irq(&data->txlock);
3372 schedule_work(&data->work);
3373
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003374 return 0;
Oliver Neukum7bee5492009-08-24 23:44:59 +02003375
3376failed:
3377 usb_scuttle_anchored_urbs(&data->deferred);
3378done:
3379 spin_lock_irq(&data->txlock);
3380 clear_bit(BTUSB_SUSPENDING, &data->flags);
3381 spin_unlock_irq(&data->txlock);
3382
3383 return err;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003384}
Oliver Neukum7bee5492009-08-24 23:44:59 +02003385#endif
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003386
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003387static struct usb_driver btusb_driver = {
3388 .name = "btusb",
3389 .probe = btusb_probe,
3390 .disconnect = btusb_disconnect,
Oliver Neukum7bee5492009-08-24 23:44:59 +02003391#ifdef CONFIG_PM
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003392 .suspend = btusb_suspend,
3393 .resume = btusb_resume,
Oliver Neukum7bee5492009-08-24 23:44:59 +02003394#endif
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003395 .id_table = btusb_table,
Oliver Neukum7bee5492009-08-24 23:44:59 +02003396 .supports_autosuspend = 1,
Sarah Sharpe1f12eb2012-04-23 10:08:51 -07003397 .disable_hub_initiated_lpm = 1,
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003398};
3399
Greg Kroah-Hartman93f15082011-11-18 09:47:34 -08003400module_usb_driver(btusb_driver);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003401
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02003402module_param(disable_scofix, bool, 0644);
3403MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
3404
3405module_param(force_scofix, bool, 0644);
3406MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
3407
3408module_param(reset, bool, 0644);
3409MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
3410
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003411MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
3412MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
3413MODULE_VERSION(VERSION);
3414MODULE_LICENSE("GPL");