blob: d21f3b4176d3165f9541275964278db16dd24374 [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 },
Janne Heikkinen134d3b32014-12-09 07:44:51 +0200189 { USB_DEVICE(0x04ca, 0x3010), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200190 { USB_DEVICE(0x0930, 0x0219), .driver_info = BTUSB_ATH3012 },
191 { USB_DEVICE(0x0930, 0x0220), .driver_info = BTUSB_ATH3012 },
Vincent Zwanenburg89d29752014-08-08 12:33:56 +0100192 { USB_DEVICE(0x0930, 0x0227), .driver_info = BTUSB_ATH3012 },
Andy Shevchenkoa735f9e2014-02-18 18:26:20 +0200193 { USB_DEVICE(0x0b05, 0x17d0), .driver_info = BTUSB_ATH3012 },
Ming Leid66629c2013-03-15 11:00:39 +0800194 { USB_DEVICE(0x0cf3, 0x0036), .driver_info = BTUSB_ATH3012 },
Steven.Li2d25f8b2011-07-01 14:02:36 +0800195 { USB_DEVICE(0x0cf3, 0x3004), .driver_info = BTUSB_ATH3012 },
Sunguk Lee94a32d102013-03-12 04:41:58 +0900196 { USB_DEVICE(0x0cf3, 0x3008), .driver_info = BTUSB_ATH3012 },
Cho, Yu-Chen07c0ea82012-03-14 22:01:21 +0200197 { USB_DEVICE(0x0cf3, 0x311d), .driver_info = BTUSB_ATH3012 },
Oliver Neukumb1312372014-01-16 15:37:11 +0100198 { USB_DEVICE(0x0cf3, 0x311e), .driver_info = BTUSB_ATH3012 },
Oliver Neukum1e56f1e2014-01-16 16:02:58 +0100199 { USB_DEVICE(0x0cf3, 0x311f), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200200 { USB_DEVICE(0x0cf3, 0x3121), .driver_info = BTUSB_ATH3012 },
Ming Leiebaf5792013-03-18 23:45:11 +0800201 { USB_DEVICE(0x0cf3, 0x817a), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200202 { USB_DEVICE(0x0cf3, 0xe003), .driver_info = BTUSB_ATH3012 },
AceLan Kaoac713112012-04-19 14:53:45 +0800203 { USB_DEVICE(0x0cf3, 0xe004), .driver_info = BTUSB_ATH3012 },
Peng Chen0a3658c2013-08-30 17:41:40 +0800204 { USB_DEVICE(0x0cf3, 0xe005), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200205 { USB_DEVICE(0x13d3, 0x3362), .driver_info = BTUSB_ATH3012 },
206 { USB_DEVICE(0x13d3, 0x3375), .driver_info = BTUSB_ATH3012 },
AceLan Kaoeed307e2012-12-11 11:41:20 +0800207 { USB_DEVICE(0x13d3, 0x3393), .driver_info = BTUSB_ATH3012 },
Sujith Manoharan5b77a1f2013-07-15 09:29:03 +0530208 { USB_DEVICE(0x13d3, 0x3402), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin3bb30a7c2014-11-25 20:19:52 +0300209 { USB_DEVICE(0x13d3, 0x3408), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin033efa92015-01-18 00:16:51 +0300210 { USB_DEVICE(0x13d3, 0x3423), .driver_info = BTUSB_ATH3012 },
Anantha Krishnanfa2f1392014-07-08 19:25:08 +0530211 { USB_DEVICE(0x13d3, 0x3432), .driver_info = BTUSB_ATH3012 },
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530212
Cho, Yu-Chene9036e32011-02-15 10:20:07 +0800213 /* Atheros AR5BBU12 with sflash firmware */
214 { USB_DEVICE(0x0489, 0xe02c), .driver_info = BTUSB_IGNORE },
215
Michael Gruetzner85d59722012-05-02 22:33:40 +0200216 /* Atheros AR5BBU12 with sflash firmware */
Yevgeniy Melnichukbc21fde2012-08-07 19:48:10 +0530217 { USB_DEVICE(0x0489, 0xe036), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200218 { USB_DEVICE(0x0489, 0xe03c), .driver_info = BTUSB_ATH3012 },
Michael Gruetzner85d59722012-05-02 22:33:40 +0200219
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +0000220 /* QCA ROME chipset */
Marcel Holtmannc9e44472015-03-16 23:56:04 -0700221 { USB_DEVICE(0x0cf3, 0xe300), .driver_info = BTUSB_QCA_ROME },
222 { USB_DEVICE(0x0cf3, 0xe360), .driver_info = BTUSB_QCA_ROME },
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +0000223
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200224 /* Broadcom BCM2035 */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100225 { USB_DEVICE(0x0a5c, 0x2009), .driver_info = BTUSB_BCM92035 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200226 { USB_DEVICE(0x0a5c, 0x200a), .driver_info = BTUSB_WRONG_SCO_MTU },
227 { USB_DEVICE(0x0a5c, 0x2035), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200228
229 /* Broadcom BCM2045 */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100230 { USB_DEVICE(0x0a5c, 0x2039), .driver_info = BTUSB_WRONG_SCO_MTU },
231 { USB_DEVICE(0x0a5c, 0x2101), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmannbdbef3d2008-09-23 00:16:35 +0200232
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200233 /* IBM/Lenovo ThinkPad with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100234 { USB_DEVICE(0x0a5c, 0x201e), .driver_info = BTUSB_WRONG_SCO_MTU },
235 { USB_DEVICE(0x0a5c, 0x2110), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200236
237 /* HP laptop with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100238 { USB_DEVICE(0x03f0, 0x171d), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200239
240 /* Dell laptop with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100241 { USB_DEVICE(0x413c, 0x8126), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200242
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100243 /* Dell Wireless 370 and 410 devices */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100244 { USB_DEVICE(0x413c, 0x8152), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100245 { USB_DEVICE(0x413c, 0x8156), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200246
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100247 /* Belkin F8T012 and F8T013 devices */
248 { USB_DEVICE(0x050d, 0x0012), .driver_info = BTUSB_WRONG_SCO_MTU },
249 { USB_DEVICE(0x050d, 0x0013), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200250
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100251 /* Asus WL-BTD202 device */
252 { USB_DEVICE(0x0b05, 0x1715), .driver_info = BTUSB_WRONG_SCO_MTU },
253
254 /* Kensington Bluetooth USB adapter */
255 { USB_DEVICE(0x047d, 0x105e), .driver_info = BTUSB_WRONG_SCO_MTU },
256
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200257 /* RTX Telecom based adapters with buggy SCO support */
258 { USB_DEVICE(0x0400, 0x0807), .driver_info = BTUSB_BROKEN_ISOC },
259 { USB_DEVICE(0x0400, 0x080a), .driver_info = BTUSB_BROKEN_ISOC },
260
261 /* CONWISE Technology based adapters with buggy SCO support */
262 { USB_DEVICE(0x0e5e, 0x6622), .driver_info = BTUSB_BROKEN_ISOC },
263
Marcel Holtmann4fcef8e2015-01-01 17:34:37 -0800264 /* Roper Class 1 Bluetooth Dongle (Silicon Wave based) */
265 { USB_DEVICE(0x1300, 0x0001), .driver_info = BTUSB_SWAVE },
266
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200267 /* Digianswer devices */
268 { USB_DEVICE(0x08fd, 0x0001), .driver_info = BTUSB_DIGIANSWER },
269 { USB_DEVICE(0x08fd, 0x0002), .driver_info = BTUSB_IGNORE },
270
271 /* CSR BlueCore Bluetooth Sniffer */
Marcel Holtmann4f64fa82014-07-07 00:12:04 +0200272 { USB_DEVICE(0x0a12, 0x0002),
273 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200274
275 /* Frontline ComProbe Bluetooth Sniffer */
Marcel Holtmann4f64fa82014-07-07 00:12:04 +0200276 { USB_DEVICE(0x16d3, 0x0002),
277 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200278
Marcel Holtmanncb1ee892015-01-28 19:41:42 -0800279 /* Marvell Bluetooth devices */
280 { USB_DEVICE(0x1286, 0x2044), .driver_info = BTUSB_MARVELL },
281 { USB_DEVICE(0x1286, 0x2046), .driver_info = BTUSB_MARVELL },
282
Marcel Holtmannd0ac9eb2015-01-28 19:41:43 -0800283 /* Intel Bluetooth devices */
Marcel Holtmann407550f2015-02-22 15:41:18 -0800284 { USB_DEVICE(0x8087, 0x07da), .driver_info = BTUSB_CSR },
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -0700285 { USB_DEVICE(0x8087, 0x07dc), .driver_info = BTUSB_INTEL },
Tedd Ho-Jeong Anef4e5e42013-11-12 13:10:58 -0800286 { USB_DEVICE(0x8087, 0x0a2a), .driver_info = BTUSB_INTEL },
Marcel Holtmanncda0dd72015-01-26 21:33:48 -0800287 { USB_DEVICE(0x8087, 0x0a2b), .driver_info = BTUSB_INTEL_NEW },
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -0700288
Marcel Holtmannd0ac9eb2015-01-28 19:41:43 -0800289 /* Other Intel Bluetooth devices */
290 { USB_VENDOR_AND_INTERFACE_INFO(0x8087, 0xe0, 0x01, 0x01),
291 .driver_info = BTUSB_IGNORE },
Amitkumar Karwarae8df492014-07-18 14:47:06 -0700292
Daniel Drakea2698a92015-04-16 14:09:55 -0600293 /* Realtek Bluetooth devices */
294 { USB_VENDOR_AND_INTERFACE_INFO(0x0bda, 0xe0, 0x01, 0x01),
295 .driver_info = BTUSB_REALTEK },
296
297 /* Additional Realtek 8723AE Bluetooth devices */
298 { USB_DEVICE(0x0930, 0x021d), .driver_info = BTUSB_REALTEK },
299 { USB_DEVICE(0x13d3, 0x3394), .driver_info = BTUSB_REALTEK },
300
301 /* Additional Realtek 8723BE Bluetooth devices */
302 { USB_DEVICE(0x0489, 0xe085), .driver_info = BTUSB_REALTEK },
303 { USB_DEVICE(0x0489, 0xe08b), .driver_info = BTUSB_REALTEK },
304 { USB_DEVICE(0x13d3, 0x3410), .driver_info = BTUSB_REALTEK },
305 { USB_DEVICE(0x13d3, 0x3416), .driver_info = BTUSB_REALTEK },
306 { USB_DEVICE(0x13d3, 0x3459), .driver_info = BTUSB_REALTEK },
307
308 /* Additional Realtek 8821AE Bluetooth devices */
309 { USB_DEVICE(0x0b05, 0x17dc), .driver_info = BTUSB_REALTEK },
310 { USB_DEVICE(0x13d3, 0x3414), .driver_info = BTUSB_REALTEK },
311 { USB_DEVICE(0x13d3, 0x3458), .driver_info = BTUSB_REALTEK },
312 { USB_DEVICE(0x13d3, 0x3461), .driver_info = BTUSB_REALTEK },
313 { USB_DEVICE(0x13d3, 0x3462), .driver_info = BTUSB_REALTEK },
314
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200315 { } /* Terminating entry */
316};
317
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200318#define BTUSB_MAX_ISOC_FRAMES 10
319
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200320#define BTUSB_INTR_RUNNING 0
321#define BTUSB_BULK_RUNNING 1
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200322#define BTUSB_ISOC_RUNNING 2
Oliver Neukum7bee5492009-08-24 23:44:59 +0200323#define BTUSB_SUSPENDING 3
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -0300324#define BTUSB_DID_ISO_RESUME 4
Marcel Holtmanncda0dd72015-01-26 21:33:48 -0800325#define BTUSB_BOOTLOADER 5
326#define BTUSB_DOWNLOADING 6
Marcel Holtmannce6bb922015-01-28 01:58:40 -0800327#define BTUSB_FIRMWARE_LOADED 7
Marcel Holtmanncda0dd72015-01-26 21:33:48 -0800328#define BTUSB_FIRMWARE_FAILED 8
Marcel Holtmannce6bb922015-01-28 01:58:40 -0800329#define BTUSB_BOOTING 9
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200330
331struct btusb_data {
332 struct hci_dev *hdev;
333 struct usb_device *udev;
Marcel Holtmann5fbcd262008-09-23 00:16:36 +0200334 struct usb_interface *intf;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200335 struct usb_interface *isoc;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200336
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200337 unsigned long flags;
338
339 struct work_struct work;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200340 struct work_struct waker;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200341
Marcel Holtmann803b5832014-09-16 08:00:29 +0200342 struct usb_anchor deferred;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200343 struct usb_anchor tx_anchor;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200344 int tx_in_flight;
345 spinlock_t txlock;
346
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200347 struct usb_anchor intr_anchor;
348 struct usb_anchor bulk_anchor;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200349 struct usb_anchor isoc_anchor;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200350 spinlock_t rxlock;
351
352 struct sk_buff *evt_skb;
353 struct sk_buff *acl_skb;
354 struct sk_buff *sco_skb;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200355
356 struct usb_endpoint_descriptor *intr_ep;
357 struct usb_endpoint_descriptor *bulk_tx_ep;
358 struct usb_endpoint_descriptor *bulk_rx_ep;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200359 struct usb_endpoint_descriptor *isoc_tx_ep;
360 struct usb_endpoint_descriptor *isoc_rx_ep;
361
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100362 __u8 cmdreq_type;
Marcel Holtmann893ba542015-01-28 20:27:34 -0800363 __u8 cmdreq;
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100364
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100365 unsigned int sco_num;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200366 int isoc_altsetting;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +0100367 int suspend_count;
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +0100368
Marcel Holtmann97307f52015-01-12 13:51:10 -0800369 int (*recv_event)(struct hci_dev *hdev, struct sk_buff *skb);
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +0100370 int (*recv_bulk)(struct btusb_data *data, void *buffer, int count);
Kim, Ben Young Taeace31982015-02-15 23:06:14 +0000371
372 int (*setup_on_usb)(struct hci_dev *hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200373};
374
Marcel Holtmann803b5832014-09-16 08:00:29 +0200375static inline void btusb_free_frags(struct btusb_data *data)
376{
377 unsigned long flags;
378
379 spin_lock_irqsave(&data->rxlock, flags);
380
381 kfree_skb(data->evt_skb);
382 data->evt_skb = NULL;
383
384 kfree_skb(data->acl_skb);
385 data->acl_skb = NULL;
386
387 kfree_skb(data->sco_skb);
388 data->sco_skb = NULL;
389
390 spin_unlock_irqrestore(&data->rxlock, flags);
391}
392
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200393static int btusb_recv_intr(struct btusb_data *data, void *buffer, int count)
394{
Marcel Holtmann803b5832014-09-16 08:00:29 +0200395 struct sk_buff *skb;
396 int err = 0;
397
398 spin_lock(&data->rxlock);
399 skb = data->evt_skb;
400
401 while (count) {
402 int len;
403
404 if (!skb) {
405 skb = bt_skb_alloc(HCI_MAX_EVENT_SIZE, GFP_ATOMIC);
406 if (!skb) {
407 err = -ENOMEM;
408 break;
409 }
410
411 bt_cb(skb)->pkt_type = HCI_EVENT_PKT;
412 bt_cb(skb)->expect = HCI_EVENT_HDR_SIZE;
413 }
414
415 len = min_t(uint, bt_cb(skb)->expect, count);
416 memcpy(skb_put(skb, len), buffer, len);
417
418 count -= len;
419 buffer += len;
420 bt_cb(skb)->expect -= len;
421
422 if (skb->len == HCI_EVENT_HDR_SIZE) {
423 /* Complete event header */
424 bt_cb(skb)->expect = hci_event_hdr(skb)->plen;
425
426 if (skb_tailroom(skb) < bt_cb(skb)->expect) {
427 kfree_skb(skb);
428 skb = NULL;
429
430 err = -EILSEQ;
431 break;
432 }
433 }
434
435 if (bt_cb(skb)->expect == 0) {
436 /* Complete frame */
Marcel Holtmann97307f52015-01-12 13:51:10 -0800437 data->recv_event(data->hdev, skb);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200438 skb = NULL;
439 }
440 }
441
442 data->evt_skb = skb;
443 spin_unlock(&data->rxlock);
444
445 return err;
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200446}
447
448static int btusb_recv_bulk(struct btusb_data *data, void *buffer, int count)
449{
Marcel Holtmann803b5832014-09-16 08:00:29 +0200450 struct sk_buff *skb;
451 int err = 0;
452
453 spin_lock(&data->rxlock);
454 skb = data->acl_skb;
455
456 while (count) {
457 int len;
458
459 if (!skb) {
460 skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
461 if (!skb) {
462 err = -ENOMEM;
463 break;
464 }
465
466 bt_cb(skb)->pkt_type = HCI_ACLDATA_PKT;
467 bt_cb(skb)->expect = HCI_ACL_HDR_SIZE;
468 }
469
470 len = min_t(uint, bt_cb(skb)->expect, count);
471 memcpy(skb_put(skb, len), buffer, len);
472
473 count -= len;
474 buffer += len;
475 bt_cb(skb)->expect -= len;
476
477 if (skb->len == HCI_ACL_HDR_SIZE) {
478 __le16 dlen = hci_acl_hdr(skb)->dlen;
479
480 /* Complete ACL header */
481 bt_cb(skb)->expect = __le16_to_cpu(dlen);
482
483 if (skb_tailroom(skb) < bt_cb(skb)->expect) {
484 kfree_skb(skb);
485 skb = NULL;
486
487 err = -EILSEQ;
488 break;
489 }
490 }
491
492 if (bt_cb(skb)->expect == 0) {
493 /* Complete frame */
494 hci_recv_frame(data->hdev, skb);
495 skb = NULL;
496 }
497 }
498
499 data->acl_skb = skb;
500 spin_unlock(&data->rxlock);
501
502 return err;
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200503}
504
505static int btusb_recv_isoc(struct btusb_data *data, void *buffer, int count)
506{
Marcel Holtmann803b5832014-09-16 08:00:29 +0200507 struct sk_buff *skb;
508 int err = 0;
509
510 spin_lock(&data->rxlock);
511 skb = data->sco_skb;
512
513 while (count) {
514 int len;
515
516 if (!skb) {
517 skb = bt_skb_alloc(HCI_MAX_SCO_SIZE, GFP_ATOMIC);
518 if (!skb) {
519 err = -ENOMEM;
520 break;
521 }
522
523 bt_cb(skb)->pkt_type = HCI_SCODATA_PKT;
524 bt_cb(skb)->expect = HCI_SCO_HDR_SIZE;
525 }
526
527 len = min_t(uint, bt_cb(skb)->expect, count);
528 memcpy(skb_put(skb, len), buffer, len);
529
530 count -= len;
531 buffer += len;
532 bt_cb(skb)->expect -= len;
533
534 if (skb->len == HCI_SCO_HDR_SIZE) {
535 /* Complete SCO header */
536 bt_cb(skb)->expect = hci_sco_hdr(skb)->dlen;
537
538 if (skb_tailroom(skb) < bt_cb(skb)->expect) {
539 kfree_skb(skb);
540 skb = NULL;
541
542 err = -EILSEQ;
543 break;
544 }
545 }
546
547 if (bt_cb(skb)->expect == 0) {
548 /* Complete frame */
549 hci_recv_frame(data->hdev, skb);
550 skb = NULL;
551 }
552 }
553
554 data->sco_skb = skb;
555 spin_unlock(&data->rxlock);
556
557 return err;
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200558}
559
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200560static void btusb_intr_complete(struct urb *urb)
561{
562 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +0100563 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200564 int err;
565
Marcel Holtmann89e75332014-09-16 04:44:50 +0200566 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
567 urb->actual_length);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200568
569 if (!test_bit(HCI_RUNNING, &hdev->flags))
570 return;
571
572 if (urb->status == 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200573 hdev->stat.byte_rx += urb->actual_length;
574
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200575 if (btusb_recv_intr(data, urb->transfer_buffer,
576 urb->actual_length) < 0) {
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200577 BT_ERR("%s corrupted event packet", hdev->name);
578 hdev->stat.err_rx++;
579 }
Champion Chen85560c42014-09-06 14:06:08 -0500580 } else if (urb->status == -ENOENT) {
581 /* Avoid suspend failed when usb_kill_urb */
582 return;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200583 }
584
585 if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
586 return;
587
Oliver Neukum7bee5492009-08-24 23:44:59 +0200588 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200589 usb_anchor_urb(urb, &data->intr_anchor);
590
591 err = usb_submit_urb(urb, GFP_ATOMIC);
592 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200593 /* -EPERM: urb is being killed;
594 * -ENODEV: device got disconnected */
595 if (err != -EPERM && err != -ENODEV)
Stefan Seyfried61faddf2010-11-30 21:49:08 +0100596 BT_ERR("%s urb %p failed to resubmit (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200597 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200598 usb_unanchor_urb(urb);
599 }
600}
601
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100602static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200603{
David Herrmann155961e2012-02-09 21:58:32 +0100604 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200605 struct urb *urb;
606 unsigned char *buf;
607 unsigned int pipe;
608 int err, size;
609
610 BT_DBG("%s", hdev->name);
611
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200612 if (!data->intr_ep)
613 return -ENODEV;
614
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100615 urb = usb_alloc_urb(0, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200616 if (!urb)
617 return -ENOMEM;
618
619 size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
620
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100621 buf = kmalloc(size, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200622 if (!buf) {
623 usb_free_urb(urb);
624 return -ENOMEM;
625 }
626
627 pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
628
629 usb_fill_int_urb(urb, data->udev, pipe, buf, size,
Marcel Holtmann89e75332014-09-16 04:44:50 +0200630 btusb_intr_complete, hdev, data->intr_ep->bInterval);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200631
632 urb->transfer_flags |= URB_FREE_BUFFER;
633
634 usb_anchor_urb(urb, &data->intr_anchor);
635
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100636 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200637 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +0200638 if (err != -EPERM && err != -ENODEV)
639 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200640 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200641 usb_unanchor_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200642 }
643
644 usb_free_urb(urb);
645
646 return err;
647}
648
649static void btusb_bulk_complete(struct urb *urb)
650{
651 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +0100652 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200653 int err;
654
Marcel Holtmann89e75332014-09-16 04:44:50 +0200655 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
656 urb->actual_length);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200657
658 if (!test_bit(HCI_RUNNING, &hdev->flags))
659 return;
660
661 if (urb->status == 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200662 hdev->stat.byte_rx += urb->actual_length;
663
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +0100664 if (data->recv_bulk(data, urb->transfer_buffer,
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200665 urb->actual_length) < 0) {
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200666 BT_ERR("%s corrupted ACL packet", hdev->name);
667 hdev->stat.err_rx++;
668 }
Champion Chen85560c42014-09-06 14:06:08 -0500669 } else if (urb->status == -ENOENT) {
670 /* Avoid suspend failed when usb_kill_urb */
671 return;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200672 }
673
674 if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
675 return;
676
677 usb_anchor_urb(urb, &data->bulk_anchor);
Oliver Neukum652fd782009-12-16 19:23:43 +0100678 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200679
680 err = usb_submit_urb(urb, GFP_ATOMIC);
681 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200682 /* -EPERM: urb is being killed;
683 * -ENODEV: device got disconnected */
684 if (err != -EPERM && err != -ENODEV)
Stefan Seyfried61faddf2010-11-30 21:49:08 +0100685 BT_ERR("%s urb %p failed to resubmit (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200686 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200687 usb_unanchor_urb(urb);
688 }
689}
690
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100691static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200692{
David Herrmann155961e2012-02-09 21:58:32 +0100693 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200694 struct urb *urb;
695 unsigned char *buf;
696 unsigned int pipe;
Vikram Kandukuri290ba202009-07-02 14:31:59 +0530697 int err, size = HCI_MAX_FRAME_SIZE;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200698
699 BT_DBG("%s", hdev->name);
700
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200701 if (!data->bulk_rx_ep)
702 return -ENODEV;
703
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100704 urb = usb_alloc_urb(0, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200705 if (!urb)
706 return -ENOMEM;
707
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100708 buf = kmalloc(size, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200709 if (!buf) {
710 usb_free_urb(urb);
711 return -ENOMEM;
712 }
713
714 pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
715
Marcel Holtmann89e75332014-09-16 04:44:50 +0200716 usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
717 btusb_bulk_complete, hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200718
719 urb->transfer_flags |= URB_FREE_BUFFER;
720
Oliver Neukum7bee5492009-08-24 23:44:59 +0200721 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200722 usb_anchor_urb(urb, &data->bulk_anchor);
723
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100724 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200725 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +0200726 if (err != -EPERM && err != -ENODEV)
727 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200728 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200729 usb_unanchor_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200730 }
731
732 usb_free_urb(urb);
733
734 return err;
735}
736
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200737static void btusb_isoc_complete(struct urb *urb)
738{
739 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +0100740 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200741 int i, err;
742
Marcel Holtmann89e75332014-09-16 04:44:50 +0200743 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
744 urb->actual_length);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200745
746 if (!test_bit(HCI_RUNNING, &hdev->flags))
747 return;
748
749 if (urb->status == 0) {
750 for (i = 0; i < urb->number_of_packets; i++) {
751 unsigned int offset = urb->iso_frame_desc[i].offset;
752 unsigned int length = urb->iso_frame_desc[i].actual_length;
753
754 if (urb->iso_frame_desc[i].status)
755 continue;
756
757 hdev->stat.byte_rx += length;
758
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200759 if (btusb_recv_isoc(data, urb->transfer_buffer + offset,
760 length) < 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200761 BT_ERR("%s corrupted SCO packet", hdev->name);
762 hdev->stat.err_rx++;
763 }
764 }
Champion Chen85560c42014-09-06 14:06:08 -0500765 } else if (urb->status == -ENOENT) {
766 /* Avoid suspend failed when usb_kill_urb */
767 return;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200768 }
769
770 if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags))
771 return;
772
773 usb_anchor_urb(urb, &data->isoc_anchor);
774
775 err = usb_submit_urb(urb, GFP_ATOMIC);
776 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200777 /* -EPERM: urb is being killed;
778 * -ENODEV: device got disconnected */
779 if (err != -EPERM && err != -ENODEV)
Stefan Seyfried61faddf2010-11-30 21:49:08 +0100780 BT_ERR("%s urb %p failed to resubmit (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200781 hdev->name, urb, -err);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200782 usb_unanchor_urb(urb);
783 }
784}
785
Jesper Juhl42b16b32011-01-17 00:09:38 +0100786static inline void __fill_isoc_descriptor(struct urb *urb, int len, int mtu)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200787{
788 int i, offset = 0;
789
790 BT_DBG("len %d mtu %d", len, mtu);
791
792 for (i = 0; i < BTUSB_MAX_ISOC_FRAMES && len >= mtu;
793 i++, offset += mtu, len -= mtu) {
794 urb->iso_frame_desc[i].offset = offset;
795 urb->iso_frame_desc[i].length = mtu;
796 }
797
798 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
799 urb->iso_frame_desc[i].offset = offset;
800 urb->iso_frame_desc[i].length = len;
801 i++;
802 }
803
804 urb->number_of_packets = i;
805}
806
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100807static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200808{
David Herrmann155961e2012-02-09 21:58:32 +0100809 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200810 struct urb *urb;
811 unsigned char *buf;
812 unsigned int pipe;
813 int err, size;
814
815 BT_DBG("%s", hdev->name);
816
817 if (!data->isoc_rx_ep)
818 return -ENODEV;
819
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100820 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200821 if (!urb)
822 return -ENOMEM;
823
824 size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) *
825 BTUSB_MAX_ISOC_FRAMES;
826
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100827 buf = kmalloc(size, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200828 if (!buf) {
829 usb_free_urb(urb);
830 return -ENOMEM;
831 }
832
833 pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress);
834
Bing Zhaofa0fb932011-12-20 18:19:00 -0800835 usb_fill_int_urb(urb, data->udev, pipe, buf, size, btusb_isoc_complete,
Marcel Holtmann89e75332014-09-16 04:44:50 +0200836 hdev, data->isoc_rx_ep->bInterval);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200837
Marcel Holtmann89e75332014-09-16 04:44:50 +0200838 urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200839
840 __fill_isoc_descriptor(urb, size,
Marcel Holtmann89e75332014-09-16 04:44:50 +0200841 le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize));
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200842
843 usb_anchor_urb(urb, &data->isoc_anchor);
844
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100845 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200846 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +0200847 if (err != -EPERM && err != -ENODEV)
848 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200849 hdev->name, urb, -err);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200850 usb_unanchor_urb(urb);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200851 }
852
853 usb_free_urb(urb);
854
855 return err;
856}
857
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200858static void btusb_tx_complete(struct urb *urb)
859{
860 struct sk_buff *skb = urb->context;
Marcel Holtmann89e75332014-09-16 04:44:50 +0200861 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
David Herrmann155961e2012-02-09 21:58:32 +0100862 struct btusb_data *data = hci_get_drvdata(hdev);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200863
Marcel Holtmann89e75332014-09-16 04:44:50 +0200864 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
865 urb->actual_length);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200866
867 if (!test_bit(HCI_RUNNING, &hdev->flags))
868 goto done;
869
870 if (!urb->status)
871 hdev->stat.byte_tx += urb->transfer_buffer_length;
872 else
873 hdev->stat.err_tx++;
874
875done:
876 spin_lock(&data->txlock);
877 data->tx_in_flight--;
878 spin_unlock(&data->txlock);
879
880 kfree(urb->setup_packet);
881
882 kfree_skb(skb);
883}
884
885static void btusb_isoc_tx_complete(struct urb *urb)
886{
887 struct sk_buff *skb = urb->context;
Marcel Holtmann89e75332014-09-16 04:44:50 +0200888 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200889
Marcel Holtmann89e75332014-09-16 04:44:50 +0200890 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
891 urb->actual_length);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200892
893 if (!test_bit(HCI_RUNNING, &hdev->flags))
894 goto done;
895
896 if (!urb->status)
897 hdev->stat.byte_tx += urb->transfer_buffer_length;
898 else
899 hdev->stat.err_tx++;
900
901done:
902 kfree(urb->setup_packet);
903
904 kfree_skb(skb);
905}
906
907static int btusb_open(struct hci_dev *hdev)
908{
David Herrmann155961e2012-02-09 21:58:32 +0100909 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200910 int err;
911
912 BT_DBG("%s", hdev->name);
913
Kim, Ben Young Taeace31982015-02-15 23:06:14 +0000914 /* Patching USB firmware files prior to starting any URBs of HCI path
915 * It is more safe to use USB bulk channel for downloading USB patch
916 */
917 if (data->setup_on_usb) {
918 err = data->setup_on_usb(hdev);
Marcel Holtmanneb500422015-04-16 23:15:50 +0200919 if (err < 0)
Kim, Ben Young Taeace31982015-02-15 23:06:14 +0000920 return err;
921 }
922
Oliver Neukum7bee5492009-08-24 23:44:59 +0200923 err = usb_autopm_get_interface(data->intf);
924 if (err < 0)
925 return err;
926
927 data->intf->needs_remote_wakeup = 1;
928
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200929 if (test_and_set_bit(HCI_RUNNING, &hdev->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +0200930 goto done;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200931
932 if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +0200933 goto done;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200934
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100935 err = btusb_submit_intr_urb(hdev, GFP_KERNEL);
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100936 if (err < 0)
937 goto failed;
938
939 err = btusb_submit_bulk_urb(hdev, GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200940 if (err < 0) {
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100941 usb_kill_anchored_urbs(&data->intr_anchor);
942 goto failed;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200943 }
944
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100945 set_bit(BTUSB_BULK_RUNNING, &data->flags);
946 btusb_submit_bulk_urb(hdev, GFP_KERNEL);
947
Oliver Neukum7bee5492009-08-24 23:44:59 +0200948done:
949 usb_autopm_put_interface(data->intf);
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100950 return 0;
951
952failed:
953 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
954 clear_bit(HCI_RUNNING, &hdev->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200955 usb_autopm_put_interface(data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200956 return err;
957}
958
Oliver Neukum7bee5492009-08-24 23:44:59 +0200959static void btusb_stop_traffic(struct btusb_data *data)
960{
961 usb_kill_anchored_urbs(&data->intr_anchor);
962 usb_kill_anchored_urbs(&data->bulk_anchor);
963 usb_kill_anchored_urbs(&data->isoc_anchor);
964}
965
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200966static int btusb_close(struct hci_dev *hdev)
967{
David Herrmann155961e2012-02-09 21:58:32 +0100968 struct btusb_data *data = hci_get_drvdata(hdev);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200969 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200970
971 BT_DBG("%s", hdev->name);
972
973 if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
974 return 0;
975
Marcel Holtmanne8c3c3d2008-09-23 00:16:36 +0200976 cancel_work_sync(&data->work);
Linus Torvalds404291a2009-11-11 13:32:29 -0800977 cancel_work_sync(&data->waker);
Marcel Holtmanne8c3c3d2008-09-23 00:16:36 +0200978
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200979 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200980 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200981 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200982
983 btusb_stop_traffic(data);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200984 btusb_free_frags(data);
985
Oliver Neukum7bee5492009-08-24 23:44:59 +0200986 err = usb_autopm_get_interface(data->intf);
987 if (err < 0)
Oliver Neukum7b8e2c12009-11-13 14:26:23 +0100988 goto failed;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200989
990 data->intf->needs_remote_wakeup = 0;
991 usb_autopm_put_interface(data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200992
Oliver Neukum7b8e2c12009-11-13 14:26:23 +0100993failed:
994 usb_scuttle_anchored_urbs(&data->deferred);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200995 return 0;
996}
997
998static int btusb_flush(struct hci_dev *hdev)
999{
David Herrmann155961e2012-02-09 21:58:32 +01001000 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001001
1002 BT_DBG("%s", hdev->name);
1003
1004 usb_kill_anchored_urbs(&data->tx_anchor);
Marcel Holtmann803b5832014-09-16 08:00:29 +02001005 btusb_free_frags(data);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001006
1007 return 0;
1008}
1009
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001010static struct urb *alloc_ctrl_urb(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001011{
David Herrmann155961e2012-02-09 21:58:32 +01001012 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001013 struct usb_ctrlrequest *dr;
1014 struct urb *urb;
1015 unsigned int pipe;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001016
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001017 urb = usb_alloc_urb(0, GFP_KERNEL);
1018 if (!urb)
1019 return ERR_PTR(-ENOMEM);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001020
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001021 dr = kmalloc(sizeof(*dr), GFP_KERNEL);
1022 if (!dr) {
1023 usb_free_urb(urb);
1024 return ERR_PTR(-ENOMEM);
1025 }
1026
1027 dr->bRequestType = data->cmdreq_type;
Marcel Holtmann893ba542015-01-28 20:27:34 -08001028 dr->bRequest = data->cmdreq;
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001029 dr->wIndex = 0;
1030 dr->wValue = 0;
1031 dr->wLength = __cpu_to_le16(skb->len);
1032
1033 pipe = usb_sndctrlpipe(data->udev, 0x00);
1034
Marcel Holtmann89e75332014-09-16 04:44:50 +02001035 usb_fill_control_urb(urb, data->udev, pipe, (void *)dr,
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001036 skb->data, skb->len, btusb_tx_complete, skb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001037
Marcel Holtmann89e75332014-09-16 04:44:50 +02001038 skb->dev = (void *)hdev;
Marcel Holtmann7bd8f092013-10-11 06:19:18 -07001039
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001040 return urb;
1041}
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001042
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001043static struct urb *alloc_bulk_urb(struct hci_dev *hdev, struct sk_buff *skb)
1044{
1045 struct btusb_data *data = hci_get_drvdata(hdev);
1046 struct urb *urb;
1047 unsigned int pipe;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001048
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001049 if (!data->bulk_tx_ep)
1050 return ERR_PTR(-ENODEV);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001051
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001052 urb = usb_alloc_urb(0, GFP_KERNEL);
1053 if (!urb)
1054 return ERR_PTR(-ENOMEM);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001055
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001056 pipe = usb_sndbulkpipe(data->udev, data->bulk_tx_ep->bEndpointAddress);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001057
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001058 usb_fill_bulk_urb(urb, data->udev, pipe,
1059 skb->data, skb->len, btusb_tx_complete, skb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001060
Marcel Holtmann89e75332014-09-16 04:44:50 +02001061 skb->dev = (void *)hdev;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001062
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001063 return urb;
1064}
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001065
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001066static struct urb *alloc_isoc_urb(struct hci_dev *hdev, struct sk_buff *skb)
1067{
1068 struct btusb_data *data = hci_get_drvdata(hdev);
1069 struct urb *urb;
1070 unsigned int pipe;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001071
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001072 if (!data->isoc_tx_ep)
1073 return ERR_PTR(-ENODEV);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001074
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001075 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_KERNEL);
1076 if (!urb)
1077 return ERR_PTR(-ENOMEM);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001078
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001079 pipe = usb_sndisocpipe(data->udev, data->isoc_tx_ep->bEndpointAddress);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001080
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001081 usb_fill_int_urb(urb, data->udev, pipe,
1082 skb->data, skb->len, btusb_isoc_tx_complete,
1083 skb, data->isoc_tx_ep->bInterval);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001084
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001085 urb->transfer_flags = URB_ISO_ASAP;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001086
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001087 __fill_isoc_descriptor(urb, skb->len,
1088 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001089
Marcel Holtmann89e75332014-09-16 04:44:50 +02001090 skb->dev = (void *)hdev;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001091
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001092 return urb;
1093}
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001094
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001095static int submit_tx_urb(struct hci_dev *hdev, struct urb *urb)
1096{
1097 struct btusb_data *data = hci_get_drvdata(hdev);
1098 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001099
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001100 usb_anchor_urb(urb, &data->tx_anchor);
1101
Johan Hedberge9753ef2014-09-14 08:49:34 +03001102 err = usb_submit_urb(urb, GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001103 if (err < 0) {
Paul Bolle5a9b80e2011-10-09 12:12:16 +02001104 if (err != -EPERM && err != -ENODEV)
1105 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +02001106 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001107 kfree(urb->setup_packet);
1108 usb_unanchor_urb(urb);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001109 } else {
1110 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001111 }
1112
Cong Wang54a8a792011-11-22 09:32:57 +08001113 usb_free_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001114 return err;
1115}
1116
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001117static int submit_or_queue_tx_urb(struct hci_dev *hdev, struct urb *urb)
1118{
1119 struct btusb_data *data = hci_get_drvdata(hdev);
1120 unsigned long flags;
1121 bool suspending;
1122
1123 spin_lock_irqsave(&data->txlock, flags);
1124 suspending = test_bit(BTUSB_SUSPENDING, &data->flags);
1125 if (!suspending)
1126 data->tx_in_flight++;
1127 spin_unlock_irqrestore(&data->txlock, flags);
1128
1129 if (!suspending)
1130 return submit_tx_urb(hdev, urb);
1131
1132 usb_anchor_urb(urb, &data->deferred);
1133 schedule_work(&data->waker);
1134
1135 usb_free_urb(urb);
1136 return 0;
1137}
1138
1139static int btusb_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
1140{
1141 struct urb *urb;
1142
1143 BT_DBG("%s", hdev->name);
1144
1145 if (!test_bit(HCI_RUNNING, &hdev->flags))
1146 return -EBUSY;
1147
1148 switch (bt_cb(skb)->pkt_type) {
1149 case HCI_COMMAND_PKT:
1150 urb = alloc_ctrl_urb(hdev, skb);
1151 if (IS_ERR(urb))
1152 return PTR_ERR(urb);
1153
1154 hdev->stat.cmd_tx++;
1155 return submit_or_queue_tx_urb(hdev, urb);
1156
1157 case HCI_ACLDATA_PKT:
1158 urb = alloc_bulk_urb(hdev, skb);
1159 if (IS_ERR(urb))
1160 return PTR_ERR(urb);
1161
1162 hdev->stat.acl_tx++;
1163 return submit_or_queue_tx_urb(hdev, urb);
1164
1165 case HCI_SCODATA_PKT:
1166 if (hci_conn_num(hdev, SCO_LINK) < 1)
1167 return -ENODEV;
1168
1169 urb = alloc_isoc_urb(hdev, skb);
1170 if (IS_ERR(urb))
1171 return PTR_ERR(urb);
1172
1173 hdev->stat.sco_tx++;
1174 return submit_tx_urb(hdev, urb);
1175 }
1176
1177 return -EILSEQ;
1178}
1179
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001180static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
1181{
David Herrmann155961e2012-02-09 21:58:32 +01001182 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001183
1184 BT_DBG("%s evt %d", hdev->name, evt);
1185
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001186 if (hci_conn_num(hdev, SCO_LINK) != data->sco_num) {
1187 data->sco_num = hci_conn_num(hdev, SCO_LINK);
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001188 schedule_work(&data->work);
Marcel Holtmanna780efa2008-11-30 12:17:12 +01001189 }
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001190}
1191
Jesper Juhl42b16b32011-01-17 00:09:38 +01001192static inline int __set_isoc_interface(struct hci_dev *hdev, int altsetting)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001193{
David Herrmann155961e2012-02-09 21:58:32 +01001194 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001195 struct usb_interface *intf = data->isoc;
1196 struct usb_endpoint_descriptor *ep_desc;
1197 int i, err;
1198
1199 if (!data->isoc)
1200 return -ENODEV;
1201
1202 err = usb_set_interface(data->udev, 1, altsetting);
1203 if (err < 0) {
1204 BT_ERR("%s setting interface failed (%d)", hdev->name, -err);
1205 return err;
1206 }
1207
1208 data->isoc_altsetting = altsetting;
1209
1210 data->isoc_tx_ep = NULL;
1211 data->isoc_rx_ep = NULL;
1212
1213 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
1214 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
1215
1216 if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) {
1217 data->isoc_tx_ep = ep_desc;
1218 continue;
1219 }
1220
1221 if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) {
1222 data->isoc_rx_ep = ep_desc;
1223 continue;
1224 }
1225 }
1226
1227 if (!data->isoc_tx_ep || !data->isoc_rx_ep) {
1228 BT_ERR("%s invalid SCO descriptors", hdev->name);
1229 return -ENODEV;
1230 }
1231
1232 return 0;
1233}
1234
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001235static void btusb_work(struct work_struct *work)
1236{
1237 struct btusb_data *data = container_of(work, struct btusb_data, work);
1238 struct hci_dev *hdev = data->hdev;
Mikel Astizf4001d22012-04-11 08:48:51 +02001239 int new_alts;
Oliver Neukum7bee5492009-08-24 23:44:59 +02001240 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001241
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001242 if (data->sco_num > 0) {
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -03001243 if (!test_bit(BTUSB_DID_ISO_RESUME, &data->flags)) {
Oliver Neukum8efdd0c2011-02-11 13:00:06 +01001244 err = usb_autopm_get_interface(data->isoc ? data->isoc : data->intf);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001245 if (err < 0) {
1246 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1247 usb_kill_anchored_urbs(&data->isoc_anchor);
1248 return;
1249 }
1250
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -03001251 set_bit(BTUSB_DID_ISO_RESUME, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001252 }
Mikel Astizf4001d22012-04-11 08:48:51 +02001253
1254 if (hdev->voice_setting & 0x0020) {
1255 static const int alts[3] = { 2, 4, 5 };
Marcel Holtmann89e75332014-09-16 04:44:50 +02001256
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001257 new_alts = alts[data->sco_num - 1];
Mikel Astizf4001d22012-04-11 08:48:51 +02001258 } else {
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001259 new_alts = data->sco_num;
Mikel Astizf4001d22012-04-11 08:48:51 +02001260 }
1261
1262 if (data->isoc_altsetting != new_alts) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001263 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1264 usb_kill_anchored_urbs(&data->isoc_anchor);
1265
Mikel Astizf4001d22012-04-11 08:48:51 +02001266 if (__set_isoc_interface(hdev, new_alts) < 0)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001267 return;
1268 }
1269
1270 if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001271 if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001272 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1273 else
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001274 btusb_submit_isoc_urb(hdev, GFP_KERNEL);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001275 }
1276 } else {
1277 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1278 usb_kill_anchored_urbs(&data->isoc_anchor);
1279
1280 __set_isoc_interface(hdev, 0);
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -03001281 if (test_and_clear_bit(BTUSB_DID_ISO_RESUME, &data->flags))
Oliver Neukum8efdd0c2011-02-11 13:00:06 +01001282 usb_autopm_put_interface(data->isoc ? data->isoc : data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001283 }
1284}
1285
Oliver Neukum7bee5492009-08-24 23:44:59 +02001286static void btusb_waker(struct work_struct *work)
1287{
1288 struct btusb_data *data = container_of(work, struct btusb_data, waker);
1289 int err;
1290
1291 err = usb_autopm_get_interface(data->intf);
1292 if (err < 0)
1293 return;
1294
1295 usb_autopm_put_interface(data->intf);
1296}
1297
Daniel Drake6576fe42015-02-23 18:16:47 -06001298static struct sk_buff *btusb_read_local_version(struct hci_dev *hdev)
1299{
1300 struct sk_buff *skb;
1301
1302 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
1303 HCI_INIT_TIMEOUT);
1304 if (IS_ERR(skb)) {
1305 BT_ERR("%s: HCI_OP_READ_LOCAL_VERSION failed (%ld)",
1306 hdev->name, PTR_ERR(skb));
1307 return skb;
1308 }
1309
1310 if (skb->len != sizeof(struct hci_rp_read_local_version)) {
1311 BT_ERR("%s: HCI_OP_READ_LOCAL_VERSION event length mismatch",
1312 hdev->name);
1313 kfree_skb(skb);
1314 return ERR_PTR(-EIO);
1315 }
1316
1317 return skb;
1318}
1319
Marcel Holtmann9f8f9622013-04-10 08:11:35 -07001320static int btusb_setup_bcm92035(struct hci_dev *hdev)
1321{
1322 struct sk_buff *skb;
1323 u8 val = 0x00;
1324
1325 BT_DBG("%s", hdev->name);
1326
1327 skb = __hci_cmd_sync(hdev, 0xfc3b, 1, &val, HCI_INIT_TIMEOUT);
1328 if (IS_ERR(skb))
1329 BT_ERR("BCM92035 command failed (%ld)", -PTR_ERR(skb));
1330 else
1331 kfree_skb(skb);
1332
1333 return 0;
1334}
1335
Marcel Holtmann81cac642014-01-03 03:02:36 -08001336static int btusb_setup_csr(struct hci_dev *hdev)
1337{
1338 struct hci_rp_read_local_version *rp;
1339 struct sk_buff *skb;
1340 int ret;
1341
1342 BT_DBG("%s", hdev->name);
1343
Daniel Drake6576fe42015-02-23 18:16:47 -06001344 skb = btusb_read_local_version(hdev);
1345 if (IS_ERR(skb))
Marcel Holtmann81cac642014-01-03 03:02:36 -08001346 return -PTR_ERR(skb);
Marcel Holtmann81cac642014-01-03 03:02:36 -08001347
Marcel Holtmann89e75332014-09-16 04:44:50 +02001348 rp = (struct hci_rp_read_local_version *)skb->data;
Marcel Holtmann81cac642014-01-03 03:02:36 -08001349
1350 if (!rp->status) {
1351 if (le16_to_cpu(rp->manufacturer) != 10) {
1352 /* Clear the reset quirk since this is not an actual
1353 * early Bluetooth 1.1 device from CSR.
1354 */
1355 clear_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
1356
1357 /* These fake CSR controllers have all a broken
1358 * stored link key handling and so just disable it.
1359 */
1360 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY,
1361 &hdev->quirks);
1362 }
1363 }
1364
1365 ret = -bt_to_errno(rp->status);
1366
1367 kfree_skb(skb);
1368
1369 return ret;
1370}
1371
Daniel Drakea2698a92015-04-16 14:09:55 -06001372#define RTL_FRAG_LEN 252
1373
1374struct rtl_download_cmd {
1375 __u8 index;
1376 __u8 data[RTL_FRAG_LEN];
1377} __packed;
1378
1379struct rtl_download_response {
1380 __u8 status;
1381 __u8 index;
1382} __packed;
1383
1384struct rtl_rom_version_evt {
1385 __u8 status;
1386 __u8 version;
1387} __packed;
1388
1389struct rtl_epatch_header {
1390 __u8 signature[8];
1391 __le32 fw_version;
1392 __le16 num_patches;
1393} __packed;
1394
1395#define RTL_EPATCH_SIGNATURE "Realtech"
1396#define RTL_ROM_LMP_3499 0x3499
1397#define RTL_ROM_LMP_8723A 0x1200
1398#define RTL_ROM_LMP_8723B 0x8723
1399#define RTL_ROM_LMP_8821A 0x8821
1400#define RTL_ROM_LMP_8761A 0x8761
1401
1402static int rtl_read_rom_version(struct hci_dev *hdev, u8 *version)
1403{
1404 struct rtl_rom_version_evt *rom_version;
1405 struct sk_buff *skb;
1406 int ret;
1407
1408 /* Read RTL ROM version command */
1409 skb = __hci_cmd_sync(hdev, 0xfc6d, 0, NULL, HCI_INIT_TIMEOUT);
1410 if (IS_ERR(skb)) {
1411 BT_ERR("%s: Read ROM version failed (%ld)",
1412 hdev->name, PTR_ERR(skb));
1413 return PTR_ERR(skb);
1414 }
1415
1416 if (skb->len != sizeof(*rom_version)) {
1417 BT_ERR("%s: RTL version event length mismatch", hdev->name);
1418 kfree_skb(skb);
1419 return -EIO;
1420 }
1421
1422 rom_version = (struct rtl_rom_version_evt *)skb->data;
1423 BT_INFO("%s: rom_version status=%x version=%x",
1424 hdev->name, rom_version->status, rom_version->version);
1425
1426 ret = rom_version->status;
1427 if (ret == 0)
1428 *version = rom_version->version;
1429
1430 kfree_skb(skb);
1431 return ret;
1432}
1433
1434static int rtl8723b_parse_firmware(struct hci_dev *hdev, u16 lmp_subver,
1435 const struct firmware *fw,
1436 unsigned char **_buf)
1437{
1438 const u8 extension_sig[] = { 0x51, 0x04, 0xfd, 0x77 };
1439 struct rtl_epatch_header *epatch_info;
1440 unsigned char *buf;
1441 int i, ret, len;
1442 size_t min_size;
1443 u8 opcode, length, data, rom_version = 0;
1444 int project_id = -1;
1445 const unsigned char *fwptr, *chip_id_base;
1446 const unsigned char *patch_length_base, *patch_offset_base;
1447 u32 patch_offset = 0;
1448 u16 patch_length, num_patches;
1449 const u16 project_id_to_lmp_subver[] = {
1450 RTL_ROM_LMP_8723A,
1451 RTL_ROM_LMP_8723B,
1452 RTL_ROM_LMP_8821A,
1453 RTL_ROM_LMP_8761A
1454 };
1455
1456 ret = rtl_read_rom_version(hdev, &rom_version);
1457 if (ret)
1458 return -bt_to_errno(ret);
1459
1460 min_size = sizeof(struct rtl_epatch_header) + sizeof(extension_sig) + 3;
1461 if (fw->size < min_size)
1462 return -EINVAL;
1463
1464 fwptr = fw->data + fw->size - sizeof(extension_sig);
1465 if (memcmp(fwptr, extension_sig, sizeof(extension_sig)) != 0) {
1466 BT_ERR("%s: extension section signature mismatch", hdev->name);
1467 return -EINVAL;
1468 }
1469
1470 /* Loop from the end of the firmware parsing instructions, until
1471 * we find an instruction that identifies the "project ID" for the
1472 * hardware supported by this firwmare file.
1473 * Once we have that, we double-check that that project_id is suitable
1474 * for the hardware we are working with.
1475 */
1476 while (fwptr >= fw->data + (sizeof(struct rtl_epatch_header) + 3)) {
1477 opcode = *--fwptr;
1478 length = *--fwptr;
1479 data = *--fwptr;
1480
1481 BT_DBG("check op=%x len=%x data=%x", opcode, length, data);
1482
1483 if (opcode == 0xff) /* EOF */
1484 break;
1485
1486 if (length == 0) {
1487 BT_ERR("%s: found instruction with length 0",
1488 hdev->name);
1489 return -EINVAL;
1490 }
1491
1492 if (opcode == 0 && length == 1) {
1493 project_id = data;
1494 break;
1495 }
1496
1497 fwptr -= length;
1498 }
1499
1500 if (project_id < 0) {
1501 BT_ERR("%s: failed to find version instruction", hdev->name);
1502 return -EINVAL;
1503 }
1504
Dan Carpenterc57ddfa2015-04-20 18:51:35 +03001505 if (project_id >= ARRAY_SIZE(project_id_to_lmp_subver)) {
Daniel Drakea2698a92015-04-16 14:09:55 -06001506 BT_ERR("%s: unknown project id %d", hdev->name, project_id);
1507 return -EINVAL;
1508 }
1509
1510 if (lmp_subver != project_id_to_lmp_subver[project_id]) {
1511 BT_ERR("%s: firmware is for %x but this is a %x", hdev->name,
1512 project_id_to_lmp_subver[project_id], lmp_subver);
1513 return -EINVAL;
1514 }
1515
1516 epatch_info = (struct rtl_epatch_header *)fw->data;
1517 if (memcmp(epatch_info->signature, RTL_EPATCH_SIGNATURE, 8) != 0) {
1518 BT_ERR("%s: bad EPATCH signature", hdev->name);
1519 return -EINVAL;
1520 }
1521
1522 num_patches = le16_to_cpu(epatch_info->num_patches);
1523 BT_DBG("fw_version=%x, num_patches=%d",
1524 le32_to_cpu(epatch_info->fw_version), num_patches);
1525
1526 /* After the rtl_epatch_header there is a funky patch metadata section.
1527 * Assuming 2 patches, the layout is:
1528 * ChipID1 ChipID2 PatchLength1 PatchLength2 PatchOffset1 PatchOffset2
1529 *
1530 * Find the right patch for this chip.
1531 */
1532 min_size += 8 * num_patches;
1533 if (fw->size < min_size)
1534 return -EINVAL;
1535
1536 chip_id_base = fw->data + sizeof(struct rtl_epatch_header);
1537 patch_length_base = chip_id_base + (sizeof(u16) * num_patches);
1538 patch_offset_base = patch_length_base + (sizeof(u16) * num_patches);
1539 for (i = 0; i < num_patches; i++) {
1540 u16 chip_id = get_unaligned_le16(chip_id_base +
1541 (i * sizeof(u16)));
1542 if (chip_id == rom_version + 1) {
1543 patch_length = get_unaligned_le16(patch_length_base +
1544 (i * sizeof(u16)));
1545 patch_offset = get_unaligned_le32(patch_offset_base +
1546 (i * sizeof(u32)));
1547 break;
1548 }
1549 }
1550
1551 if (!patch_offset) {
1552 BT_ERR("%s: didn't find patch for chip id %d",
1553 hdev->name, rom_version);
1554 return -EINVAL;
1555 }
1556
1557 BT_DBG("length=%x offset=%x index %d", patch_length, patch_offset, i);
1558 min_size = patch_offset + patch_length;
1559 if (fw->size < min_size)
1560 return -EINVAL;
1561
1562 /* Copy the firmware into a new buffer and write the version at
1563 * the end.
1564 */
1565 len = patch_length;
1566 buf = kmemdup(fw->data + patch_offset, patch_length, GFP_KERNEL);
1567 if (!buf)
1568 return -ENOMEM;
1569
1570 memcpy(buf + patch_length - 4, &epatch_info->fw_version, 4);
1571
1572 *_buf = buf;
1573 return len;
1574}
1575
1576static int rtl_download_firmware(struct hci_dev *hdev,
1577 const unsigned char *data, int fw_len)
1578{
1579 struct rtl_download_cmd *dl_cmd;
1580 int frag_num = fw_len / RTL_FRAG_LEN + 1;
1581 int frag_len = RTL_FRAG_LEN;
1582 int ret = 0;
1583 int i;
1584
1585 dl_cmd = kmalloc(sizeof(struct rtl_download_cmd), GFP_KERNEL);
1586 if (!dl_cmd)
1587 return -ENOMEM;
1588
1589 for (i = 0; i < frag_num; i++) {
1590 struct rtl_download_response *dl_resp;
1591 struct sk_buff *skb;
1592
1593 BT_DBG("download fw (%d/%d)", i, frag_num);
1594
1595 dl_cmd->index = i;
1596 if (i == (frag_num - 1)) {
1597 dl_cmd->index |= 0x80; /* data end */
1598 frag_len = fw_len % RTL_FRAG_LEN;
1599 }
1600 memcpy(dl_cmd->data, data, frag_len);
1601
1602 /* Send download command */
1603 skb = __hci_cmd_sync(hdev, 0xfc20, frag_len + 1, dl_cmd,
1604 HCI_INIT_TIMEOUT);
1605 if (IS_ERR(skb)) {
1606 BT_ERR("%s: download fw command failed (%ld)",
1607 hdev->name, PTR_ERR(skb));
1608 ret = -PTR_ERR(skb);
1609 goto out;
1610 }
1611
1612 if (skb->len != sizeof(*dl_resp)) {
1613 BT_ERR("%s: download fw event length mismatch",
1614 hdev->name);
1615 kfree_skb(skb);
1616 ret = -EIO;
1617 goto out;
1618 }
1619
1620 dl_resp = (struct rtl_download_response *)skb->data;
1621 if (dl_resp->status != 0) {
1622 kfree_skb(skb);
1623 ret = bt_to_errno(dl_resp->status);
1624 goto out;
1625 }
1626
1627 kfree_skb(skb);
1628 data += RTL_FRAG_LEN;
1629 }
1630
1631out:
1632 kfree(dl_cmd);
1633 return ret;
1634}
1635
1636static int btusb_setup_rtl8723a(struct hci_dev *hdev)
1637{
1638 struct btusb_data *data = dev_get_drvdata(&hdev->dev);
1639 struct usb_device *udev = interface_to_usbdev(data->intf);
1640 const struct firmware *fw;
1641 int ret;
1642
1643 BT_INFO("%s: rtl: loading rtl_bt/rtl8723a_fw.bin", hdev->name);
1644 ret = request_firmware(&fw, "rtl_bt/rtl8723a_fw.bin", &udev->dev);
1645 if (ret < 0) {
1646 BT_ERR("%s: Failed to load rtl_bt/rtl8723a_fw.bin", hdev->name);
1647 return ret;
1648 }
1649
1650 if (fw->size < 8) {
1651 ret = -EINVAL;
1652 goto out;
1653 }
1654
1655 /* Check that the firmware doesn't have the epatch signature
1656 * (which is only for RTL8723B and newer).
1657 */
1658 if (!memcmp(fw->data, RTL_EPATCH_SIGNATURE, 8)) {
1659 BT_ERR("%s: unexpected EPATCH signature!", hdev->name);
1660 ret = -EINVAL;
1661 goto out;
1662 }
1663
1664 ret = rtl_download_firmware(hdev, fw->data, fw->size);
1665
1666out:
1667 release_firmware(fw);
1668 return ret;
1669}
1670
1671static int btusb_setup_rtl8723b(struct hci_dev *hdev, u16 lmp_subver,
1672 const char *fw_name)
1673{
1674 struct btusb_data *data = dev_get_drvdata(&hdev->dev);
1675 struct usb_device *udev = interface_to_usbdev(data->intf);
1676 unsigned char *fw_data = NULL;
1677 const struct firmware *fw;
1678 int ret;
1679
1680 BT_INFO("%s: rtl: loading %s", hdev->name, fw_name);
1681 ret = request_firmware(&fw, fw_name, &udev->dev);
1682 if (ret < 0) {
1683 BT_ERR("%s: Failed to load %s", hdev->name, fw_name);
1684 return ret;
1685 }
1686
1687 ret = rtl8723b_parse_firmware(hdev, lmp_subver, fw, &fw_data);
1688 if (ret < 0)
1689 goto out;
1690
1691 ret = rtl_download_firmware(hdev, fw_data, ret);
1692 kfree(fw_data);
1693 if (ret < 0)
1694 goto out;
1695
1696out:
1697 release_firmware(fw);
1698 return ret;
1699}
1700
1701static int btusb_setup_realtek(struct hci_dev *hdev)
1702{
1703 struct sk_buff *skb;
1704 struct hci_rp_read_local_version *resp;
1705 u16 lmp_subver;
1706
1707 skb = btusb_read_local_version(hdev);
1708 if (IS_ERR(skb))
1709 return -PTR_ERR(skb);
1710
1711 resp = (struct hci_rp_read_local_version *)skb->data;
1712 BT_INFO("%s: rtl: examining hci_ver=%02x hci_rev=%04x lmp_ver=%02x "
1713 "lmp_subver=%04x", hdev->name, resp->hci_ver, resp->hci_rev,
1714 resp->lmp_ver, resp->lmp_subver);
1715
1716 lmp_subver = le16_to_cpu(resp->lmp_subver);
1717 kfree_skb(skb);
1718
1719 /* Match a set of subver values that correspond to stock firmware,
1720 * which is not compatible with standard btusb.
1721 * If matched, upload an alternative firmware that does conform to
1722 * standard btusb. Once that firmware is uploaded, the subver changes
1723 * to a different value.
1724 */
1725 switch (lmp_subver) {
1726 case RTL_ROM_LMP_8723A:
1727 case RTL_ROM_LMP_3499:
1728 return btusb_setup_rtl8723a(hdev);
1729 case RTL_ROM_LMP_8723B:
1730 return btusb_setup_rtl8723b(hdev, lmp_subver,
1731 "rtl_bt/rtl8723b_fw.bin");
1732 case RTL_ROM_LMP_8821A:
1733 return btusb_setup_rtl8723b(hdev, lmp_subver,
1734 "rtl_bt/rtl8821a_fw.bin");
1735 case RTL_ROM_LMP_8761A:
1736 return btusb_setup_rtl8723b(hdev, lmp_subver,
1737 "rtl_bt/rtl8761a_fw.bin");
1738 default:
1739 BT_INFO("rtl: assuming no firmware upload needed.");
1740 return 0;
1741 }
1742}
1743
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001744static const struct firmware *btusb_setup_intel_get_fw(struct hci_dev *hdev,
Marcel Holtmann89e75332014-09-16 04:44:50 +02001745 struct intel_version *ver)
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001746{
1747 const struct firmware *fw;
1748 char fwname[64];
1749 int ret;
1750
1751 snprintf(fwname, sizeof(fwname),
1752 "intel/ibt-hw-%x.%x.%x-fw-%x.%x.%x.%x.%x.bseq",
1753 ver->hw_platform, ver->hw_variant, ver->hw_revision,
1754 ver->fw_variant, ver->fw_revision, ver->fw_build_num,
1755 ver->fw_build_ww, ver->fw_build_yy);
1756
1757 ret = request_firmware(&fw, fwname, &hdev->dev);
1758 if (ret < 0) {
1759 if (ret == -EINVAL) {
1760 BT_ERR("%s Intel firmware file request failed (%d)",
1761 hdev->name, ret);
1762 return NULL;
1763 }
1764
1765 BT_ERR("%s failed to open Intel firmware file: %s(%d)",
1766 hdev->name, fwname, ret);
1767
1768 /* If the correct firmware patch file is not found, use the
1769 * default firmware patch file instead
1770 */
1771 snprintf(fwname, sizeof(fwname), "intel/ibt-hw-%x.%x.bseq",
1772 ver->hw_platform, ver->hw_variant);
1773 if (request_firmware(&fw, fwname, &hdev->dev) < 0) {
1774 BT_ERR("%s failed to open default Intel fw file: %s",
1775 hdev->name, fwname);
1776 return NULL;
1777 }
1778 }
1779
1780 BT_INFO("%s: Intel Bluetooth firmware file: %s", hdev->name, fwname);
1781
1782 return fw;
1783}
1784
1785static int btusb_setup_intel_patching(struct hci_dev *hdev,
1786 const struct firmware *fw,
1787 const u8 **fw_ptr, int *disable_patch)
1788{
1789 struct sk_buff *skb;
1790 struct hci_command_hdr *cmd;
1791 const u8 *cmd_param;
1792 struct hci_event_hdr *evt = NULL;
1793 const u8 *evt_param = NULL;
1794 int remain = fw->size - (*fw_ptr - fw->data);
1795
1796 /* The first byte indicates the types of the patch command or event.
1797 * 0x01 means HCI command and 0x02 is HCI event. If the first bytes
1798 * in the current firmware buffer doesn't start with 0x01 or
1799 * the size of remain buffer is smaller than HCI command header,
1800 * the firmware file is corrupted and it should stop the patching
1801 * process.
1802 */
1803 if (remain > HCI_COMMAND_HDR_SIZE && *fw_ptr[0] != 0x01) {
1804 BT_ERR("%s Intel fw corrupted: invalid cmd read", hdev->name);
1805 return -EINVAL;
1806 }
1807 (*fw_ptr)++;
1808 remain--;
1809
1810 cmd = (struct hci_command_hdr *)(*fw_ptr);
1811 *fw_ptr += sizeof(*cmd);
1812 remain -= sizeof(*cmd);
1813
1814 /* Ensure that the remain firmware data is long enough than the length
1815 * of command parameter. If not, the firmware file is corrupted.
1816 */
1817 if (remain < cmd->plen) {
1818 BT_ERR("%s Intel fw corrupted: invalid cmd len", hdev->name);
1819 return -EFAULT;
1820 }
1821
1822 /* If there is a command that loads a patch in the firmware
1823 * file, then enable the patch upon success, otherwise just
1824 * disable the manufacturer mode, for example patch activation
1825 * is not required when the default firmware patch file is used
1826 * because there are no patch data to load.
1827 */
1828 if (*disable_patch && le16_to_cpu(cmd->opcode) == 0xfc8e)
1829 *disable_patch = 0;
1830
1831 cmd_param = *fw_ptr;
1832 *fw_ptr += cmd->plen;
1833 remain -= cmd->plen;
1834
1835 /* This reads the expected events when the above command is sent to the
1836 * device. Some vendor commands expects more than one events, for
1837 * example command status event followed by vendor specific event.
1838 * For this case, it only keeps the last expected event. so the command
1839 * can be sent with __hci_cmd_sync_ev() which returns the sk_buff of
1840 * last expected event.
1841 */
1842 while (remain > HCI_EVENT_HDR_SIZE && *fw_ptr[0] == 0x02) {
1843 (*fw_ptr)++;
1844 remain--;
1845
1846 evt = (struct hci_event_hdr *)(*fw_ptr);
1847 *fw_ptr += sizeof(*evt);
1848 remain -= sizeof(*evt);
1849
1850 if (remain < evt->plen) {
1851 BT_ERR("%s Intel fw corrupted: invalid evt len",
1852 hdev->name);
1853 return -EFAULT;
1854 }
1855
1856 evt_param = *fw_ptr;
1857 *fw_ptr += evt->plen;
1858 remain -= evt->plen;
1859 }
1860
1861 /* Every HCI commands in the firmware file has its correspond event.
1862 * If event is not found or remain is smaller than zero, the firmware
1863 * file is corrupted.
1864 */
1865 if (!evt || !evt_param || remain < 0) {
1866 BT_ERR("%s Intel fw corrupted: invalid evt read", hdev->name);
1867 return -EFAULT;
1868 }
1869
1870 skb = __hci_cmd_sync_ev(hdev, le16_to_cpu(cmd->opcode), cmd->plen,
1871 cmd_param, evt->evt, HCI_INIT_TIMEOUT);
1872 if (IS_ERR(skb)) {
1873 BT_ERR("%s sending Intel patch command (0x%4.4x) failed (%ld)",
1874 hdev->name, cmd->opcode, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001875 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001876 }
1877
1878 /* It ensures that the returned event matches the event data read from
1879 * the firmware file. At fist, it checks the length and then
1880 * the contents of the event.
1881 */
1882 if (skb->len != evt->plen) {
1883 BT_ERR("%s mismatch event length (opcode 0x%4.4x)", hdev->name,
1884 le16_to_cpu(cmd->opcode));
1885 kfree_skb(skb);
1886 return -EFAULT;
1887 }
1888
1889 if (memcmp(skb->data, evt_param, evt->plen)) {
1890 BT_ERR("%s mismatch event parameter (opcode 0x%4.4x)",
1891 hdev->name, le16_to_cpu(cmd->opcode));
1892 kfree_skb(skb);
1893 return -EFAULT;
1894 }
1895 kfree_skb(skb);
1896
1897 return 0;
1898}
1899
1900static int btusb_setup_intel(struct hci_dev *hdev)
1901{
1902 struct sk_buff *skb;
1903 const struct firmware *fw;
1904 const u8 *fw_ptr;
1905 int disable_patch;
1906 struct intel_version *ver;
1907
1908 const u8 mfg_enable[] = { 0x01, 0x00 };
1909 const u8 mfg_disable[] = { 0x00, 0x00 };
1910 const u8 mfg_reset_deactivate[] = { 0x00, 0x01 };
1911 const u8 mfg_reset_activate[] = { 0x00, 0x02 };
1912
1913 BT_DBG("%s", hdev->name);
1914
1915 /* The controller has a bug with the first HCI command sent to it
1916 * returning number of completed commands as zero. This would stall the
1917 * command processing in the Bluetooth core.
1918 *
1919 * As a workaround, send HCI Reset command first which will reset the
1920 * number of completed commands and allow normal command processing
1921 * from now on.
1922 */
1923 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
1924 if (IS_ERR(skb)) {
1925 BT_ERR("%s sending initial HCI reset command failed (%ld)",
1926 hdev->name, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001927 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001928 }
1929 kfree_skb(skb);
1930
1931 /* Read Intel specific controller version first to allow selection of
1932 * which firmware file to load.
1933 *
1934 * The returned information are hardware variant and revision plus
1935 * firmware variant, revision and build number.
1936 */
1937 skb = __hci_cmd_sync(hdev, 0xfc05, 0, NULL, HCI_INIT_TIMEOUT);
1938 if (IS_ERR(skb)) {
1939 BT_ERR("%s reading Intel fw version command failed (%ld)",
1940 hdev->name, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001941 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001942 }
1943
1944 if (skb->len != sizeof(*ver)) {
1945 BT_ERR("%s Intel version event length mismatch", hdev->name);
1946 kfree_skb(skb);
1947 return -EIO;
1948 }
1949
1950 ver = (struct intel_version *)skb->data;
1951 if (ver->status) {
1952 BT_ERR("%s Intel fw version event failed (%02x)", hdev->name,
1953 ver->status);
1954 kfree_skb(skb);
1955 return -bt_to_errno(ver->status);
1956 }
1957
1958 BT_INFO("%s: read Intel version: %02x%02x%02x%02x%02x%02x%02x%02x%02x",
1959 hdev->name, ver->hw_platform, ver->hw_variant,
1960 ver->hw_revision, ver->fw_variant, ver->fw_revision,
1961 ver->fw_build_num, ver->fw_build_ww, ver->fw_build_yy,
1962 ver->fw_patch_num);
1963
1964 /* fw_patch_num indicates the version of patch the device currently
1965 * have. If there is no patch data in the device, it is always 0x00.
1966 * So, if it is other than 0x00, no need to patch the deivce again.
1967 */
1968 if (ver->fw_patch_num) {
1969 BT_INFO("%s: Intel device is already patched. patch num: %02x",
1970 hdev->name, ver->fw_patch_num);
1971 kfree_skb(skb);
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07001972 btintel_check_bdaddr(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001973 return 0;
1974 }
1975
1976 /* Opens the firmware patch file based on the firmware version read
1977 * from the controller. If it fails to open the matching firmware
1978 * patch file, it tries to open the default firmware patch file.
1979 * If no patch file is found, allow the device to operate without
1980 * a patch.
1981 */
1982 fw = btusb_setup_intel_get_fw(hdev, ver);
1983 if (!fw) {
1984 kfree_skb(skb);
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07001985 btintel_check_bdaddr(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001986 return 0;
1987 }
1988 fw_ptr = fw->data;
1989
1990 /* This Intel specific command enables the manufacturer mode of the
1991 * controller.
1992 *
1993 * Only while this mode is enabled, the driver can download the
1994 * firmware patch data and configuration parameters.
1995 */
1996 skb = __hci_cmd_sync(hdev, 0xfc11, 2, mfg_enable, HCI_INIT_TIMEOUT);
1997 if (IS_ERR(skb)) {
1998 BT_ERR("%s entering Intel manufacturer mode failed (%ld)",
1999 hdev->name, PTR_ERR(skb));
2000 release_firmware(fw);
Adam Leed9c78e92013-07-10 10:02:12 +08002001 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002002 }
2003
2004 if (skb->data[0]) {
2005 u8 evt_status = skb->data[0];
Marcel Holtmann89e75332014-09-16 04:44:50 +02002006
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002007 BT_ERR("%s enable Intel manufacturer mode event failed (%02x)",
2008 hdev->name, evt_status);
2009 kfree_skb(skb);
2010 release_firmware(fw);
2011 return -bt_to_errno(evt_status);
2012 }
2013 kfree_skb(skb);
2014
2015 disable_patch = 1;
2016
2017 /* The firmware data file consists of list of Intel specific HCI
2018 * commands and its expected events. The first byte indicates the
2019 * type of the message, either HCI command or HCI event.
2020 *
2021 * It reads the command and its expected event from the firmware file,
2022 * and send to the controller. Once __hci_cmd_sync_ev() returns,
2023 * the returned event is compared with the event read from the firmware
2024 * file and it will continue until all the messages are downloaded to
2025 * the controller.
2026 *
2027 * Once the firmware patching is completed successfully,
2028 * the manufacturer mode is disabled with reset and activating the
2029 * downloaded patch.
2030 *
2031 * If the firmware patching fails, the manufacturer mode is
2032 * disabled with reset and deactivating the patch.
2033 *
2034 * If the default patch file is used, no reset is done when disabling
2035 * the manufacturer.
2036 */
2037 while (fw->size > fw_ptr - fw->data) {
2038 int ret;
2039
2040 ret = btusb_setup_intel_patching(hdev, fw, &fw_ptr,
2041 &disable_patch);
2042 if (ret < 0)
2043 goto exit_mfg_deactivate;
2044 }
2045
2046 release_firmware(fw);
2047
2048 if (disable_patch)
2049 goto exit_mfg_disable;
2050
2051 /* Patching completed successfully and disable the manufacturer mode
2052 * with reset and activate the downloaded firmware patches.
2053 */
2054 skb = __hci_cmd_sync(hdev, 0xfc11, sizeof(mfg_reset_activate),
2055 mfg_reset_activate, HCI_INIT_TIMEOUT);
2056 if (IS_ERR(skb)) {
2057 BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
2058 hdev->name, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08002059 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002060 }
2061 kfree_skb(skb);
2062
2063 BT_INFO("%s: Intel Bluetooth firmware patch completed and activated",
2064 hdev->name);
2065
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07002066 btintel_check_bdaddr(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002067 return 0;
2068
2069exit_mfg_disable:
2070 /* Disable the manufacturer mode without reset */
2071 skb = __hci_cmd_sync(hdev, 0xfc11, sizeof(mfg_disable), mfg_disable,
2072 HCI_INIT_TIMEOUT);
2073 if (IS_ERR(skb)) {
2074 BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
2075 hdev->name, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08002076 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002077 }
2078 kfree_skb(skb);
2079
2080 BT_INFO("%s: Intel Bluetooth firmware patch completed", hdev->name);
Marcel Holtmann40cb0982014-07-02 12:06:45 +02002081
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07002082 btintel_check_bdaddr(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002083 return 0;
2084
2085exit_mfg_deactivate:
2086 release_firmware(fw);
2087
2088 /* Patching failed. Disable the manufacturer mode with reset and
2089 * deactivate the downloaded firmware patches.
2090 */
2091 skb = __hci_cmd_sync(hdev, 0xfc11, sizeof(mfg_reset_deactivate),
2092 mfg_reset_deactivate, HCI_INIT_TIMEOUT);
2093 if (IS_ERR(skb)) {
2094 BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
2095 hdev->name, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08002096 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002097 }
2098 kfree_skb(skb);
2099
2100 BT_INFO("%s: Intel Bluetooth firmware patch completed and deactivated",
2101 hdev->name);
2102
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07002103 btintel_check_bdaddr(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002104 return 0;
2105}
2106
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002107static int inject_cmd_complete(struct hci_dev *hdev, __u16 opcode)
2108{
2109 struct sk_buff *skb;
2110 struct hci_event_hdr *hdr;
2111 struct hci_ev_cmd_complete *evt;
2112
2113 skb = bt_skb_alloc(sizeof(*hdr) + sizeof(*evt) + 1, GFP_ATOMIC);
2114 if (!skb)
2115 return -ENOMEM;
2116
2117 hdr = (struct hci_event_hdr *)skb_put(skb, sizeof(*hdr));
2118 hdr->evt = HCI_EV_CMD_COMPLETE;
2119 hdr->plen = sizeof(*evt) + 1;
2120
2121 evt = (struct hci_ev_cmd_complete *)skb_put(skb, sizeof(*evt));
2122 evt->ncmd = 0x01;
2123 evt->opcode = cpu_to_le16(opcode);
2124
2125 *skb_put(skb, 1) = 0x00;
2126
2127 bt_cb(skb)->pkt_type = HCI_EVENT_PKT;
2128
2129 return hci_recv_frame(hdev, skb);
2130}
2131
2132static int btusb_recv_bulk_intel(struct btusb_data *data, void *buffer,
2133 int count)
2134{
2135 /* When the device is in bootloader mode, then it can send
2136 * events via the bulk endpoint. These events are treated the
2137 * same way as the ones received from the interrupt endpoint.
2138 */
2139 if (test_bit(BTUSB_BOOTLOADER, &data->flags))
2140 return btusb_recv_intr(data, buffer, count);
2141
2142 return btusb_recv_bulk(data, buffer, count);
2143}
2144
Marcel Holtmannccd6da22015-04-09 00:35:19 -07002145static void btusb_intel_bootup(struct btusb_data *data, const void *ptr,
2146 unsigned int len)
2147{
2148 const struct intel_bootup *evt = ptr;
2149
2150 if (len != sizeof(*evt))
2151 return;
2152
2153 if (test_and_clear_bit(BTUSB_BOOTING, &data->flags)) {
2154 smp_mb__after_atomic();
2155 wake_up_bit(&data->flags, BTUSB_BOOTING);
2156 }
2157}
2158
2159static void btusb_intel_secure_send_result(struct btusb_data *data,
2160 const void *ptr, unsigned int len)
2161{
2162 const struct intel_secure_send_result *evt = ptr;
2163
2164 if (len != sizeof(*evt))
2165 return;
2166
2167 if (evt->result)
2168 set_bit(BTUSB_FIRMWARE_FAILED, &data->flags);
2169
2170 if (test_and_clear_bit(BTUSB_DOWNLOADING, &data->flags) &&
2171 test_bit(BTUSB_FIRMWARE_LOADED, &data->flags)) {
2172 smp_mb__after_atomic();
2173 wake_up_bit(&data->flags, BTUSB_DOWNLOADING);
2174 }
2175}
2176
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002177static int btusb_recv_event_intel(struct hci_dev *hdev, struct sk_buff *skb)
2178{
2179 struct btusb_data *data = hci_get_drvdata(hdev);
2180
2181 if (test_bit(BTUSB_BOOTLOADER, &data->flags)) {
2182 struct hci_event_hdr *hdr = (void *)skb->data;
2183
Marcel Holtmannccd6da22015-04-09 00:35:19 -07002184 if (skb->len > HCI_EVENT_HDR_SIZE && hdr->evt == 0xff &&
2185 hdr->plen > 0) {
2186 const void *ptr = skb->data + HCI_EVENT_HDR_SIZE + 1;
2187 unsigned int len = skb->len - HCI_EVENT_HDR_SIZE - 1;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002188
Marcel Holtmannccd6da22015-04-09 00:35:19 -07002189 switch (skb->data[2]) {
2190 case 0x02:
2191 /* When switching to the operational firmware
2192 * the device sends a vendor specific event
2193 * indicating that the bootup completed.
2194 */
2195 btusb_intel_bootup(data, ptr, len);
2196 break;
2197 case 0x06:
2198 /* When the firmware loading completes the
2199 * device sends out a vendor specific event
2200 * indicating the result of the firmware
2201 * loading.
2202 */
2203 btusb_intel_secure_send_result(data, ptr, len);
2204 break;
Johan Hedbergfad70972015-01-30 10:58:55 +02002205 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002206 }
2207 }
2208
2209 return hci_recv_frame(hdev, skb);
2210}
2211
2212static int btusb_send_frame_intel(struct hci_dev *hdev, struct sk_buff *skb)
2213{
2214 struct btusb_data *data = hci_get_drvdata(hdev);
2215 struct urb *urb;
2216
2217 BT_DBG("%s", hdev->name);
2218
2219 if (!test_bit(HCI_RUNNING, &hdev->flags))
2220 return -EBUSY;
2221
2222 switch (bt_cb(skb)->pkt_type) {
2223 case HCI_COMMAND_PKT:
2224 if (test_bit(BTUSB_BOOTLOADER, &data->flags)) {
2225 struct hci_command_hdr *cmd = (void *)skb->data;
2226 __u16 opcode = le16_to_cpu(cmd->opcode);
2227
2228 /* When in bootloader mode and the command 0xfc09
2229 * is received, it needs to be send down the
2230 * bulk endpoint. So allocate a bulk URB instead.
2231 */
2232 if (opcode == 0xfc09)
2233 urb = alloc_bulk_urb(hdev, skb);
2234 else
2235 urb = alloc_ctrl_urb(hdev, skb);
2236
2237 /* When the 0xfc01 command is issued to boot into
2238 * the operational firmware, it will actually not
2239 * send a command complete event. To keep the flow
2240 * control working inject that event here.
2241 */
2242 if (opcode == 0xfc01)
2243 inject_cmd_complete(hdev, opcode);
2244 } else {
2245 urb = alloc_ctrl_urb(hdev, skb);
2246 }
2247 if (IS_ERR(urb))
2248 return PTR_ERR(urb);
2249
2250 hdev->stat.cmd_tx++;
2251 return submit_or_queue_tx_urb(hdev, urb);
2252
2253 case HCI_ACLDATA_PKT:
2254 urb = alloc_bulk_urb(hdev, skb);
2255 if (IS_ERR(urb))
2256 return PTR_ERR(urb);
2257
2258 hdev->stat.acl_tx++;
2259 return submit_or_queue_tx_urb(hdev, urb);
2260
2261 case HCI_SCODATA_PKT:
2262 if (hci_conn_num(hdev, SCO_LINK) < 1)
2263 return -ENODEV;
2264
2265 urb = alloc_isoc_urb(hdev, skb);
2266 if (IS_ERR(urb))
2267 return PTR_ERR(urb);
2268
2269 hdev->stat.sco_tx++;
2270 return submit_tx_urb(hdev, urb);
2271 }
2272
2273 return -EILSEQ;
2274}
2275
2276static int btusb_intel_secure_send(struct hci_dev *hdev, u8 fragment_type,
2277 u32 plen, const void *param)
2278{
2279 while (plen > 0) {
2280 struct sk_buff *skb;
2281 u8 cmd_param[253], fragment_len = (plen > 252) ? 252 : plen;
2282
2283 cmd_param[0] = fragment_type;
2284 memcpy(cmd_param + 1, param, fragment_len);
2285
2286 skb = __hci_cmd_sync(hdev, 0xfc09, fragment_len + 1,
2287 cmd_param, HCI_INIT_TIMEOUT);
2288 if (IS_ERR(skb))
2289 return PTR_ERR(skb);
2290
2291 kfree_skb(skb);
2292
2293 plen -= fragment_len;
2294 param += fragment_len;
2295 }
2296
2297 return 0;
2298}
2299
2300static void btusb_intel_version_info(struct hci_dev *hdev,
2301 struct intel_version *ver)
2302{
2303 const char *variant;
2304
2305 switch (ver->fw_variant) {
2306 case 0x06:
2307 variant = "Bootloader";
2308 break;
2309 case 0x23:
2310 variant = "Firmware";
2311 break;
2312 default:
2313 return;
2314 }
2315
2316 BT_INFO("%s: %s revision %u.%u build %u week %u %u", hdev->name,
2317 variant, ver->fw_revision >> 4, ver->fw_revision & 0x0f,
2318 ver->fw_build_num, ver->fw_build_ww, 2000 + ver->fw_build_yy);
2319}
2320
2321static int btusb_setup_intel_new(struct hci_dev *hdev)
2322{
2323 static const u8 reset_param[] = { 0x00, 0x01, 0x00, 0x01,
2324 0x00, 0x08, 0x04, 0x00 };
2325 struct btusb_data *data = hci_get_drvdata(hdev);
2326 struct sk_buff *skb;
2327 struct intel_version *ver;
2328 struct intel_boot_params *params;
2329 const struct firmware *fw;
2330 const u8 *fw_ptr;
2331 char fwname[64];
2332 ktime_t calltime, delta, rettime;
2333 unsigned long long duration;
2334 int err;
2335
2336 BT_DBG("%s", hdev->name);
2337
2338 calltime = ktime_get();
2339
2340 /* Read the Intel version information to determine if the device
2341 * is in bootloader mode or if it already has operational firmware
2342 * loaded.
2343 */
2344 skb = __hci_cmd_sync(hdev, 0xfc05, 0, NULL, HCI_INIT_TIMEOUT);
2345 if (IS_ERR(skb)) {
2346 BT_ERR("%s: Reading Intel version information failed (%ld)",
2347 hdev->name, PTR_ERR(skb));
2348 return PTR_ERR(skb);
2349 }
2350
2351 if (skb->len != sizeof(*ver)) {
2352 BT_ERR("%s: Intel version event size mismatch", hdev->name);
2353 kfree_skb(skb);
2354 return -EILSEQ;
2355 }
2356
2357 ver = (struct intel_version *)skb->data;
2358 if (ver->status) {
2359 BT_ERR("%s: Intel version command failure (%02x)",
2360 hdev->name, ver->status);
2361 err = -bt_to_errno(ver->status);
2362 kfree_skb(skb);
2363 return err;
2364 }
2365
2366 /* The hardware platform number has a fixed value of 0x37 and
2367 * for now only accept this single value.
2368 */
2369 if (ver->hw_platform != 0x37) {
2370 BT_ERR("%s: Unsupported Intel hardware platform (%u)",
2371 hdev->name, ver->hw_platform);
2372 kfree_skb(skb);
2373 return -EINVAL;
2374 }
2375
2376 /* At the moment only the hardware variant iBT 3.0 (LnP/SfP) is
2377 * supported by this firmware loading method. This check has been
2378 * put in place to ensure correct forward compatibility options
2379 * when newer hardware variants come along.
2380 */
2381 if (ver->hw_variant != 0x0b) {
2382 BT_ERR("%s: Unsupported Intel hardware variant (%u)",
2383 hdev->name, ver->hw_variant);
2384 kfree_skb(skb);
2385 return -EINVAL;
2386 }
2387
2388 btusb_intel_version_info(hdev, ver);
2389
2390 /* The firmware variant determines if the device is in bootloader
2391 * mode or is running operational firmware. The value 0x06 identifies
2392 * the bootloader and the value 0x23 identifies the operational
2393 * firmware.
2394 *
2395 * When the operational firmware is already present, then only
2396 * the check for valid Bluetooth device address is needed. This
2397 * determines if the device will be added as configured or
2398 * unconfigured controller.
2399 *
2400 * It is not possible to use the Secure Boot Parameters in this
2401 * case since that command is only available in bootloader mode.
2402 */
2403 if (ver->fw_variant == 0x23) {
2404 kfree_skb(skb);
2405 clear_bit(BTUSB_BOOTLOADER, &data->flags);
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07002406 btintel_check_bdaddr(hdev);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002407 return 0;
2408 }
2409
2410 /* If the device is not in bootloader mode, then the only possible
2411 * choice is to return an error and abort the device initialization.
2412 */
2413 if (ver->fw_variant != 0x06) {
2414 BT_ERR("%s: Unsupported Intel firmware variant (%u)",
2415 hdev->name, ver->fw_variant);
2416 kfree_skb(skb);
2417 return -ENODEV;
2418 }
2419
2420 kfree_skb(skb);
2421
2422 /* Read the secure boot parameters to identify the operating
2423 * details of the bootloader.
2424 */
2425 skb = __hci_cmd_sync(hdev, 0xfc0d, 0, NULL, HCI_INIT_TIMEOUT);
2426 if (IS_ERR(skb)) {
2427 BT_ERR("%s: Reading Intel boot parameters failed (%ld)",
2428 hdev->name, PTR_ERR(skb));
2429 return PTR_ERR(skb);
2430 }
2431
2432 if (skb->len != sizeof(*params)) {
2433 BT_ERR("%s: Intel boot parameters size mismatch", hdev->name);
2434 kfree_skb(skb);
2435 return -EILSEQ;
2436 }
2437
2438 params = (struct intel_boot_params *)skb->data;
2439 if (params->status) {
2440 BT_ERR("%s: Intel boot parameters command failure (%02x)",
2441 hdev->name, params->status);
2442 err = -bt_to_errno(params->status);
2443 kfree_skb(skb);
2444 return err;
2445 }
2446
2447 BT_INFO("%s: Device revision is %u", hdev->name,
2448 le16_to_cpu(params->dev_revid));
2449
2450 BT_INFO("%s: Secure boot is %s", hdev->name,
2451 params->secure_boot ? "enabled" : "disabled");
2452
2453 BT_INFO("%s: Minimum firmware build %u week %u %u", hdev->name,
2454 params->min_fw_build_nn, params->min_fw_build_cw,
2455 2000 + params->min_fw_build_yy);
2456
2457 /* It is required that every single firmware fragment is acknowledged
2458 * with a command complete event. If the boot parameters indicate
2459 * that this bootloader does not send them, then abort the setup.
2460 */
2461 if (params->limited_cce != 0x00) {
2462 BT_ERR("%s: Unsupported Intel firmware loading method (%u)",
2463 hdev->name, params->limited_cce);
2464 kfree_skb(skb);
2465 return -EINVAL;
2466 }
2467
2468 /* If the OTP has no valid Bluetooth device address, then there will
2469 * also be no valid address for the operational firmware.
2470 */
2471 if (!bacmp(&params->otp_bdaddr, BDADDR_ANY)) {
2472 BT_INFO("%s: No device address configured", hdev->name);
2473 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
2474 }
2475
2476 /* With this Intel bootloader only the hardware variant and device
2477 * revision information are used to select the right firmware.
2478 *
2479 * Currently this bootloader support is limited to hardware variant
2480 * iBT 3.0 (LnP/SfP) which is identified by the value 11 (0x0b).
2481 */
2482 snprintf(fwname, sizeof(fwname), "intel/ibt-11-%u.sfi",
2483 le16_to_cpu(params->dev_revid));
2484
2485 err = request_firmware(&fw, fwname, &hdev->dev);
2486 if (err < 0) {
2487 BT_ERR("%s: Failed to load Intel firmware file (%d)",
2488 hdev->name, err);
2489 kfree_skb(skb);
2490 return err;
2491 }
2492
2493 BT_INFO("%s: Found device firmware: %s", hdev->name, fwname);
2494
2495 kfree_skb(skb);
2496
2497 if (fw->size < 644) {
2498 BT_ERR("%s: Invalid size of firmware file (%zu)",
2499 hdev->name, fw->size);
2500 err = -EBADF;
2501 goto done;
2502 }
2503
2504 set_bit(BTUSB_DOWNLOADING, &data->flags);
2505
2506 /* Start the firmware download transaction with the Init fragment
2507 * represented by the 128 bytes of CSS header.
2508 */
2509 err = btusb_intel_secure_send(hdev, 0x00, 128, fw->data);
2510 if (err < 0) {
2511 BT_ERR("%s: Failed to send firmware header (%d)",
2512 hdev->name, err);
2513 goto done;
2514 }
2515
2516 /* Send the 256 bytes of public key information from the firmware
2517 * as the PKey fragment.
2518 */
2519 err = btusb_intel_secure_send(hdev, 0x03, 256, fw->data + 128);
2520 if (err < 0) {
2521 BT_ERR("%s: Failed to send firmware public key (%d)",
2522 hdev->name, err);
2523 goto done;
2524 }
2525
2526 /* Send the 256 bytes of signature information from the firmware
2527 * as the Sign fragment.
2528 */
2529 err = btusb_intel_secure_send(hdev, 0x02, 256, fw->data + 388);
2530 if (err < 0) {
2531 BT_ERR("%s: Failed to send firmware signature (%d)",
2532 hdev->name, err);
2533 goto done;
2534 }
2535
2536 fw_ptr = fw->data + 644;
2537
2538 while (fw_ptr - fw->data < fw->size) {
2539 struct hci_command_hdr *cmd = (void *)fw_ptr;
2540 u8 cmd_len;
2541
2542 cmd_len = sizeof(*cmd) + cmd->plen;
2543
2544 /* Send each command from the firmware data buffer as
2545 * a single Data fragment.
2546 */
2547 err = btusb_intel_secure_send(hdev, 0x01, cmd_len, fw_ptr);
2548 if (err < 0) {
2549 BT_ERR("%s: Failed to send firmware data (%d)",
2550 hdev->name, err);
2551 goto done;
2552 }
2553
2554 fw_ptr += cmd_len;
2555 }
2556
Marcel Holtmannce6bb922015-01-28 01:58:40 -08002557 set_bit(BTUSB_FIRMWARE_LOADED, &data->flags);
2558
Johan Hedberga087a982015-01-30 10:58:54 +02002559 BT_INFO("%s: Waiting for firmware download to complete", hdev->name);
2560
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002561 /* Before switching the device into operational mode and with that
2562 * booting the loaded firmware, wait for the bootloader notification
2563 * that all fragments have been successfully received.
2564 *
Johan Hedberga087a982015-01-30 10:58:54 +02002565 * When the event processing receives the notification, then the
2566 * BTUSB_DOWNLOADING flag will be cleared.
2567 *
2568 * The firmware loading should not take longer than 5 seconds
2569 * and thus just timeout if that happens and fail the setup
2570 * of this device.
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002571 */
Johan Hedberg129a7692015-02-14 09:33:35 +02002572 err = wait_on_bit_timeout(&data->flags, BTUSB_DOWNLOADING,
2573 TASK_INTERRUPTIBLE,
2574 msecs_to_jiffies(5000));
Johan Hedberga087a982015-01-30 10:58:54 +02002575 if (err == 1) {
2576 BT_ERR("%s: Firmware loading interrupted", hdev->name);
2577 err = -EINTR;
2578 goto done;
2579 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002580
Johan Hedberga087a982015-01-30 10:58:54 +02002581 if (err) {
2582 BT_ERR("%s: Firmware loading timeout", hdev->name);
2583 err = -ETIMEDOUT;
2584 goto done;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002585 }
2586
2587 if (test_bit(BTUSB_FIRMWARE_FAILED, &data->flags)) {
2588 BT_ERR("%s: Firmware loading failed", hdev->name);
2589 err = -ENOEXEC;
2590 goto done;
2591 }
2592
2593 rettime = ktime_get();
2594 delta = ktime_sub(rettime, calltime);
2595 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
2596
2597 BT_INFO("%s: Firmware loaded in %llu usecs", hdev->name, duration);
2598
2599done:
2600 release_firmware(fw);
2601
2602 if (err < 0)
2603 return err;
2604
2605 calltime = ktime_get();
2606
2607 set_bit(BTUSB_BOOTING, &data->flags);
2608
2609 skb = __hci_cmd_sync(hdev, 0xfc01, sizeof(reset_param), reset_param,
2610 HCI_INIT_TIMEOUT);
2611 if (IS_ERR(skb))
2612 return PTR_ERR(skb);
2613
2614 kfree_skb(skb);
2615
2616 /* The bootloader will not indicate when the device is ready. This
2617 * is done by the operational firmware sending bootup notification.
Johan Hedbergfad70972015-01-30 10:58:55 +02002618 *
2619 * Booting into operational firmware should not take longer than
2620 * 1 second. However if that happens, then just fail the setup
2621 * since something went wrong.
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002622 */
Johan Hedbergfad70972015-01-30 10:58:55 +02002623 BT_INFO("%s: Waiting for device to boot", hdev->name);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002624
Johan Hedberg129a7692015-02-14 09:33:35 +02002625 err = wait_on_bit_timeout(&data->flags, BTUSB_BOOTING,
2626 TASK_INTERRUPTIBLE,
2627 msecs_to_jiffies(1000));
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002628
Johan Hedbergfad70972015-01-30 10:58:55 +02002629 if (err == 1) {
2630 BT_ERR("%s: Device boot interrupted", hdev->name);
2631 return -EINTR;
2632 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002633
Johan Hedbergfad70972015-01-30 10:58:55 +02002634 if (err) {
2635 BT_ERR("%s: Device boot timeout", hdev->name);
2636 return -ETIMEDOUT;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002637 }
2638
2639 rettime = ktime_get();
2640 delta = ktime_sub(rettime, calltime);
2641 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
2642
2643 BT_INFO("%s: Device booted in %llu usecs", hdev->name, duration);
2644
2645 clear_bit(BTUSB_BOOTLOADER, &data->flags);
2646
2647 return 0;
2648}
2649
Marcel Holtmann385a7682015-01-28 11:09:56 -08002650static void btusb_hw_error_intel(struct hci_dev *hdev, u8 code)
2651{
2652 struct sk_buff *skb;
2653 u8 type = 0x00;
2654
2655 BT_ERR("%s: Hardware error 0x%2.2x", hdev->name, code);
2656
2657 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
2658 if (IS_ERR(skb)) {
2659 BT_ERR("%s: Reset after hardware error failed (%ld)",
2660 hdev->name, PTR_ERR(skb));
2661 return;
2662 }
2663 kfree_skb(skb);
2664
2665 skb = __hci_cmd_sync(hdev, 0xfc22, 1, &type, HCI_INIT_TIMEOUT);
2666 if (IS_ERR(skb)) {
2667 BT_ERR("%s: Retrieving Intel exception info failed (%ld)",
2668 hdev->name, PTR_ERR(skb));
2669 return;
2670 }
2671
2672 if (skb->len != 13) {
2673 BT_ERR("%s: Exception info size mismatch", hdev->name);
2674 kfree_skb(skb);
2675 return;
2676 }
2677
2678 if (skb->data[0] != 0x00) {
2679 BT_ERR("%s: Exception info command failure (%02x)",
2680 hdev->name, skb->data[0]);
2681 kfree_skb(skb);
2682 return;
2683 }
2684
2685 BT_ERR("%s: Exception info %s", hdev->name, (char *)(skb->data + 1));
2686
2687 kfree_skb(skb);
2688}
2689
Tedd Ho-Jeong Anbfbd45e2015-02-13 09:20:52 -08002690static int btusb_shutdown_intel(struct hci_dev *hdev)
2691{
2692 struct sk_buff *skb;
2693 long ret;
2694
2695 /* Some platforms have an issue with BT LED when the interface is
2696 * down or BT radio is turned off, which takes 5 seconds to BT LED
2697 * goes off. This command turns off the BT LED immediately.
2698 */
2699 skb = __hci_cmd_sync(hdev, 0xfc3f, 0, NULL, HCI_INIT_TIMEOUT);
2700 if (IS_ERR(skb)) {
2701 ret = PTR_ERR(skb);
2702 BT_ERR("%s: turning off Intel device LED failed (%ld)",
2703 hdev->name, ret);
2704 return ret;
2705 }
2706 kfree_skb(skb);
2707
2708 return 0;
2709}
2710
Amitkumar Karwarae8df492014-07-18 14:47:06 -07002711static int btusb_set_bdaddr_marvell(struct hci_dev *hdev,
2712 const bdaddr_t *bdaddr)
2713{
2714 struct sk_buff *skb;
2715 u8 buf[8];
2716 long ret;
2717
2718 buf[0] = 0xfe;
2719 buf[1] = sizeof(bdaddr_t);
2720 memcpy(buf + 2, bdaddr, sizeof(bdaddr_t));
2721
2722 skb = __hci_cmd_sync(hdev, 0xfc22, sizeof(buf), buf, HCI_INIT_TIMEOUT);
2723 if (IS_ERR(skb)) {
2724 ret = PTR_ERR(skb);
2725 BT_ERR("%s: changing Marvell device address failed (%ld)",
2726 hdev->name, ret);
2727 return ret;
2728 }
2729 kfree_skb(skb);
2730
2731 return 0;
2732}
2733
Toshi Kikuchi58592232014-12-12 10:58:05 -08002734static int btusb_set_bdaddr_ath3012(struct hci_dev *hdev,
2735 const bdaddr_t *bdaddr)
2736{
2737 struct sk_buff *skb;
2738 u8 buf[10];
2739 long ret;
2740
2741 buf[0] = 0x01;
2742 buf[1] = 0x01;
2743 buf[2] = 0x00;
2744 buf[3] = sizeof(bdaddr_t);
2745 memcpy(buf + 4, bdaddr, sizeof(bdaddr_t));
2746
2747 skb = __hci_cmd_sync(hdev, 0xfc0b, sizeof(buf), buf, HCI_INIT_TIMEOUT);
2748 if (IS_ERR(skb)) {
2749 ret = PTR_ERR(skb);
2750 BT_ERR("%s: Change address command failed (%ld)",
2751 hdev->name, ret);
2752 return ret;
2753 }
2754 kfree_skb(skb);
2755
2756 return 0;
2757}
2758
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002759#define QCA_DFU_PACKET_LEN 4096
2760
2761#define QCA_GET_TARGET_VERSION 0x09
2762#define QCA_CHECK_STATUS 0x05
2763#define QCA_DFU_DOWNLOAD 0x01
2764
2765#define QCA_SYSCFG_UPDATED 0x40
2766#define QCA_PATCH_UPDATED 0x80
2767#define QCA_DFU_TIMEOUT 3000
2768
2769struct qca_version {
2770 __le32 rom_version;
2771 __le32 patch_version;
2772 __le32 ram_version;
2773 __le32 ref_clock;
2774 __u8 reserved[4];
2775} __packed;
2776
2777struct qca_rampatch_version {
2778 __le16 rom_version;
2779 __le16 patch_version;
2780} __packed;
2781
2782struct qca_device_info {
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002783 u32 rom_version;
2784 u8 rampatch_hdr; /* length of header in rampatch */
2785 u8 nvm_hdr; /* length of header in NVM */
2786 u8 ver_offset; /* offset of version structure in rampatch */
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002787};
2788
2789static const struct qca_device_info qca_devices_table[] = {
2790 { 0x00000100, 20, 4, 10 }, /* Rome 1.0 */
2791 { 0x00000101, 20, 4, 10 }, /* Rome 1.1 */
2792 { 0x00000201, 28, 4, 18 }, /* Rome 2.1 */
2793 { 0x00000300, 28, 4, 18 }, /* Rome 3.0 */
2794 { 0x00000302, 28, 4, 18 }, /* Rome 3.2 */
2795};
2796
2797static int btusb_qca_send_vendor_req(struct hci_dev *hdev, u8 request,
2798 void *data, u16 size)
2799{
2800 struct btusb_data *btdata = hci_get_drvdata(hdev);
2801 struct usb_device *udev = btdata->udev;
2802 int pipe, err;
2803 u8 *buf;
2804
2805 buf = kmalloc(size, GFP_KERNEL);
2806 if (!buf)
2807 return -ENOMEM;
2808
2809 /* Found some of USB hosts have IOT issues with ours so that we should
2810 * not wait until HCI layer is ready.
2811 */
2812 pipe = usb_rcvctrlpipe(udev, 0);
2813 err = usb_control_msg(udev, pipe, request, USB_TYPE_VENDOR | USB_DIR_IN,
2814 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
2815 if (err < 0) {
2816 BT_ERR("%s: Failed to access otp area (%d)", hdev->name, err);
2817 goto done;
2818 }
2819
2820 memcpy(data, buf, size);
2821
2822done:
2823 kfree(buf);
2824
2825 return err;
2826}
2827
2828static int btusb_setup_qca_download_fw(struct hci_dev *hdev,
2829 const struct firmware *firmware,
2830 size_t hdr_size)
2831{
2832 struct btusb_data *btdata = hci_get_drvdata(hdev);
2833 struct usb_device *udev = btdata->udev;
2834 size_t count, size, sent = 0;
2835 int pipe, len, err;
2836 u8 *buf;
2837
2838 buf = kmalloc(QCA_DFU_PACKET_LEN, GFP_KERNEL);
2839 if (!buf)
2840 return -ENOMEM;
2841
2842 count = firmware->size;
2843
2844 size = min_t(size_t, count, hdr_size);
2845 memcpy(buf, firmware->data, size);
2846
2847 /* USB patches should go down to controller through USB path
2848 * because binary format fits to go down through USB channel.
2849 * USB control path is for patching headers and USB bulk is for
2850 * patch body.
2851 */
2852 pipe = usb_sndctrlpipe(udev, 0);
2853 err = usb_control_msg(udev, pipe, QCA_DFU_DOWNLOAD, USB_TYPE_VENDOR,
2854 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
2855 if (err < 0) {
2856 BT_ERR("%s: Failed to send headers (%d)", hdev->name, err);
2857 goto done;
2858 }
2859
2860 sent += size;
2861 count -= size;
2862
2863 while (count) {
2864 size = min_t(size_t, count, QCA_DFU_PACKET_LEN);
2865
2866 memcpy(buf, firmware->data + sent, size);
2867
2868 pipe = usb_sndbulkpipe(udev, 0x02);
2869 err = usb_bulk_msg(udev, pipe, buf, size, &len,
2870 QCA_DFU_TIMEOUT);
2871 if (err < 0) {
2872 BT_ERR("%s: Failed to send body at %zd of %zd (%d)",
2873 hdev->name, sent, firmware->size, err);
2874 break;
2875 }
2876
2877 if (size != len) {
2878 BT_ERR("%s: Failed to get bulk buffer", hdev->name);
2879 err = -EILSEQ;
2880 break;
2881 }
2882
2883 sent += size;
2884 count -= size;
2885 }
2886
2887done:
2888 kfree(buf);
2889 return err;
2890}
2891
2892static int btusb_setup_qca_load_rampatch(struct hci_dev *hdev,
2893 struct qca_version *ver,
2894 const struct qca_device_info *info)
2895{
2896 struct qca_rampatch_version *rver;
2897 const struct firmware *fw;
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002898 u32 ver_rom, ver_patch;
2899 u16 rver_rom, rver_patch;
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002900 char fwname[64];
2901 int err;
2902
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002903 ver_rom = le32_to_cpu(ver->rom_version);
2904 ver_patch = le32_to_cpu(ver->patch_version);
2905
2906 snprintf(fwname, sizeof(fwname), "qca/rampatch_usb_%08x.bin", ver_rom);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002907
2908 err = request_firmware(&fw, fwname, &hdev->dev);
2909 if (err) {
2910 BT_ERR("%s: failed to request rampatch file: %s (%d)",
2911 hdev->name, fwname, err);
2912 return err;
2913 }
2914
2915 BT_INFO("%s: using rampatch file: %s", hdev->name, fwname);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002916
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002917 rver = (struct qca_rampatch_version *)(fw->data + info->ver_offset);
2918 rver_rom = le16_to_cpu(rver->rom_version);
2919 rver_patch = le16_to_cpu(rver->patch_version);
2920
2921 BT_INFO("%s: QCA: patch rome 0x%x build 0x%x, firmware rome 0x%x "
2922 "build 0x%x", hdev->name, rver_rom, rver_patch, ver_rom,
2923 ver_patch);
2924
2925 if (rver_rom != ver_rom || rver_patch <= ver_patch) {
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002926 BT_ERR("%s: rampatch file version did not match with firmware",
2927 hdev->name);
2928 err = -EINVAL;
2929 goto done;
2930 }
2931
2932 err = btusb_setup_qca_download_fw(hdev, fw, info->rampatch_hdr);
2933
2934done:
2935 release_firmware(fw);
2936
2937 return err;
2938}
2939
2940static int btusb_setup_qca_load_nvm(struct hci_dev *hdev,
2941 struct qca_version *ver,
2942 const struct qca_device_info *info)
2943{
2944 const struct firmware *fw;
2945 char fwname[64];
2946 int err;
2947
2948 snprintf(fwname, sizeof(fwname), "qca/nvm_usb_%08x.bin",
2949 le32_to_cpu(ver->rom_version));
2950
2951 err = request_firmware(&fw, fwname, &hdev->dev);
2952 if (err) {
2953 BT_ERR("%s: failed to request NVM file: %s (%d)",
2954 hdev->name, fwname, err);
2955 return err;
2956 }
2957
2958 BT_INFO("%s: using NVM file: %s", hdev->name, fwname);
2959
2960 err = btusb_setup_qca_download_fw(hdev, fw, info->nvm_hdr);
2961
2962 release_firmware(fw);
2963
2964 return err;
2965}
2966
2967static int btusb_setup_qca(struct hci_dev *hdev)
2968{
2969 const struct qca_device_info *info = NULL;
2970 struct qca_version ver;
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002971 u32 ver_rom;
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002972 u8 status;
2973 int i, err;
2974
2975 err = btusb_qca_send_vendor_req(hdev, QCA_GET_TARGET_VERSION, &ver,
Marcel Holtmanneb500422015-04-16 23:15:50 +02002976 sizeof(ver));
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002977 if (err < 0)
2978 return err;
2979
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002980 ver_rom = le32_to_cpu(ver.rom_version);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002981 for (i = 0; i < ARRAY_SIZE(qca_devices_table); i++) {
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002982 if (ver_rom == qca_devices_table[i].rom_version)
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002983 info = &qca_devices_table[i];
2984 }
2985 if (!info) {
2986 BT_ERR("%s: don't support firmware rome 0x%x", hdev->name,
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002987 ver_rom);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002988 return -ENODEV;
2989 }
2990
2991 err = btusb_qca_send_vendor_req(hdev, QCA_CHECK_STATUS, &status,
2992 sizeof(status));
2993 if (err < 0)
2994 return err;
2995
2996 if (!(status & QCA_PATCH_UPDATED)) {
2997 err = btusb_setup_qca_load_rampatch(hdev, &ver, info);
2998 if (err < 0)
2999 return err;
3000 }
3001
3002 if (!(status & QCA_SYSCFG_UPDATED)) {
3003 err = btusb_setup_qca_load_nvm(hdev, &ver, info);
3004 if (err < 0)
3005 return err;
3006 }
3007
3008 return 0;
3009}
3010
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003011static int btusb_probe(struct usb_interface *intf,
Marcel Holtmann89e75332014-09-16 04:44:50 +02003012 const struct usb_device_id *id)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003013{
3014 struct usb_endpoint_descriptor *ep_desc;
3015 struct btusb_data *data;
3016 struct hci_dev *hdev;
3017 int i, err;
3018
3019 BT_DBG("intf %p id %p", intf, id);
3020
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02003021 /* interface numbers are hardcoded in the spec */
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003022 if (intf->cur_altsetting->desc.bInterfaceNumber != 0)
3023 return -ENODEV;
3024
3025 if (!id->driver_info) {
3026 const struct usb_device_id *match;
Marcel Holtmann89e75332014-09-16 04:44:50 +02003027
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003028 match = usb_match_id(intf, blacklist_table);
3029 if (match)
3030 id = match;
3031 }
3032
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02003033 if (id->driver_info == BTUSB_IGNORE)
3034 return -ENODEV;
3035
Steven.Li2d25f8b2011-07-01 14:02:36 +08003036 if (id->driver_info & BTUSB_ATH3012) {
3037 struct usb_device *udev = interface_to_usbdev(intf);
3038
3039 /* Old firmware would otherwise let ath3k driver load
3040 * patch and sysconfig files */
3041 if (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x0001)
3042 return -ENODEV;
3043 }
3044
Sachin Kamat98921db2012-07-27 12:38:39 +05303045 data = devm_kzalloc(&intf->dev, sizeof(*data), GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003046 if (!data)
3047 return -ENOMEM;
3048
3049 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
3050 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
3051
3052 if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
3053 data->intr_ep = ep_desc;
3054 continue;
3055 }
3056
3057 if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
3058 data->bulk_tx_ep = ep_desc;
3059 continue;
3060 }
3061
3062 if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
3063 data->bulk_rx_ep = ep_desc;
3064 continue;
3065 }
3066 }
3067
Sachin Kamat98921db2012-07-27 12:38:39 +05303068 if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003069 return -ENODEV;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003070
Marcel Holtmann893ba542015-01-28 20:27:34 -08003071 if (id->driver_info & BTUSB_AMP) {
3072 data->cmdreq_type = USB_TYPE_CLASS | 0x01;
3073 data->cmdreq = 0x2b;
3074 } else {
3075 data->cmdreq_type = USB_TYPE_CLASS;
3076 data->cmdreq = 0x00;
3077 }
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01003078
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003079 data->udev = interface_to_usbdev(intf);
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02003080 data->intf = intf;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003081
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003082 INIT_WORK(&data->work, btusb_work);
Oliver Neukum7bee5492009-08-24 23:44:59 +02003083 INIT_WORK(&data->waker, btusb_waker);
Marcel Holtmann803b5832014-09-16 08:00:29 +02003084 init_usb_anchor(&data->deferred);
3085 init_usb_anchor(&data->tx_anchor);
Oliver Neukum7bee5492009-08-24 23:44:59 +02003086 spin_lock_init(&data->txlock);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003087
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003088 init_usb_anchor(&data->intr_anchor);
3089 init_usb_anchor(&data->bulk_anchor);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02003090 init_usb_anchor(&data->isoc_anchor);
Marcel Holtmann803b5832014-09-16 08:00:29 +02003091 spin_lock_init(&data->rxlock);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003092
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08003093 if (id->driver_info & BTUSB_INTEL_NEW) {
3094 data->recv_event = btusb_recv_event_intel;
3095 data->recv_bulk = btusb_recv_bulk_intel;
3096 set_bit(BTUSB_BOOTLOADER, &data->flags);
3097 } else {
3098 data->recv_event = hci_recv_frame;
3099 data->recv_bulk = btusb_recv_bulk;
3100 }
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +01003101
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003102 hdev = hci_alloc_dev();
Sachin Kamat98921db2012-07-27 12:38:39 +05303103 if (!hdev)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003104 return -ENOMEM;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003105
Marcel Holtmannc13854c2010-02-08 15:27:07 +01003106 hdev->bus = HCI_USB;
David Herrmann155961e2012-02-09 21:58:32 +01003107 hci_set_drvdata(hdev, data);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003108
Marcel Holtmann893ba542015-01-28 20:27:34 -08003109 if (id->driver_info & BTUSB_AMP)
3110 hdev->dev_type = HCI_AMP;
3111 else
3112 hdev->dev_type = HCI_BREDR;
3113
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003114 data->hdev = hdev;
3115
3116 SET_HCIDEV_DEV(hdev, &intf->dev);
3117
Marcel Holtmann9f8f9622013-04-10 08:11:35 -07003118 hdev->open = btusb_open;
3119 hdev->close = btusb_close;
3120 hdev->flush = btusb_flush;
3121 hdev->send = btusb_send_frame;
3122 hdev->notify = btusb_notify;
3123
3124 if (id->driver_info & BTUSB_BCM92035)
3125 hdev->setup = btusb_setup_bcm92035;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003126
Marcel Holtmannc2bfb102015-04-05 22:52:14 -07003127#ifdef CONFIG_BT_HCIBTUSB_BCM
Marcel Holtmannabbaf502014-07-02 00:53:48 +02003128 if (id->driver_info & BTUSB_BCM_PATCHRAM) {
Marcel Holtmannc2bfb102015-04-05 22:52:14 -07003129 hdev->setup = btbcm_setup_patchram;
Marcel Holtmann1df1f592015-04-05 22:52:11 -07003130 hdev->set_bdaddr = btbcm_set_bdaddr;
Marcel Holtmannabbaf502014-07-02 00:53:48 +02003131 }
Petri Gynther10d4c672014-05-08 15:50:01 -07003132
Marcel Holtmann941521e2015-04-05 22:52:15 -07003133 if (id->driver_info & BTUSB_BCM_APPLE)
Marcel Holtmannc2bfb102015-04-05 22:52:14 -07003134 hdev->setup = btbcm_setup_apple;
Marcel Holtmannc2bfb102015-04-05 22:52:14 -07003135#endif
Marcel Holtmann17b27722015-03-22 15:52:38 +01003136
Marcel Holtmanncb8d6592014-07-02 11:25:25 +02003137 if (id->driver_info & BTUSB_INTEL) {
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07003138 hdev->setup = btusb_setup_intel;
Tedd Ho-Jeong Anbfbd45e2015-02-13 09:20:52 -08003139 hdev->shutdown = btusb_shutdown_intel;
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07003140 hdev->set_bdaddr = btintel_set_bdaddr;
Jakub Pawlowskic33fb9b2015-01-30 18:55:58 -08003141 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
Jakub Pawlowskic1154842015-03-17 09:04:16 -07003142 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
Marcel Holtmanncb8d6592014-07-02 11:25:25 +02003143 }
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07003144
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08003145 if (id->driver_info & BTUSB_INTEL_NEW) {
3146 hdev->send = btusb_send_frame_intel;
3147 hdev->setup = btusb_setup_intel_new;
Marcel Holtmann385a7682015-01-28 11:09:56 -08003148 hdev->hw_error = btusb_hw_error_intel;
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07003149 hdev->set_bdaddr = btintel_set_bdaddr;
Marcel Holtmannb970c5b2015-02-01 23:57:18 -08003150 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08003151 }
3152
Amitkumar Karwarae8df492014-07-18 14:47:06 -07003153 if (id->driver_info & BTUSB_MARVELL)
3154 hdev->set_bdaddr = btusb_set_bdaddr_marvell;
3155
Marcel Holtmann661cf882015-01-02 23:35:20 -08003156 if (id->driver_info & BTUSB_SWAVE) {
3157 set_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks);
Marcel Holtmannd57dbe72014-12-26 04:42:33 +01003158 set_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS, &hdev->quirks);
Marcel Holtmann661cf882015-01-02 23:35:20 -08003159 }
Marcel Holtmannd57dbe72014-12-26 04:42:33 +01003160
Marcel Holtmann40df7832014-07-06 13:29:58 +02003161 if (id->driver_info & BTUSB_INTEL_BOOT)
3162 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
3163
Jakub Pawlowski79f0c872015-01-29 10:38:34 -08003164 if (id->driver_info & BTUSB_ATH3012) {
Toshi Kikuchi58592232014-12-12 10:58:05 -08003165 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
Jakub Pawlowski3d50d512015-03-17 09:04:15 -07003166 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
Jakub Pawlowski79f0c872015-01-29 10:38:34 -08003167 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
3168 }
Toshi Kikuchi58592232014-12-12 10:58:05 -08003169
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003170 if (id->driver_info & BTUSB_QCA_ROME) {
3171 data->setup_on_usb = btusb_setup_qca;
3172 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
3173 }
3174
Daniel Drakea2698a92015-04-16 14:09:55 -06003175 if (id->driver_info & BTUSB_REALTEK)
3176 hdev->setup = btusb_setup_realtek;
3177
Marcel Holtmann893ba542015-01-28 20:27:34 -08003178 if (id->driver_info & BTUSB_AMP) {
3179 /* AMP controllers do not support SCO packets */
3180 data->isoc = NULL;
3181 } else {
3182 /* Interface numbers are hardcoded in the specification */
3183 data->isoc = usb_ifnum_to_if(data->udev, 1);
3184 }
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02003185
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01003186 if (!reset)
Szymon Janca6c511c2012-05-23 12:35:46 +02003187 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02003188
3189 if (force_scofix || id->driver_info & BTUSB_WRONG_SCO_MTU) {
3190 if (!disable_scofix)
3191 set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
3192 }
3193
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02003194 if (id->driver_info & BTUSB_BROKEN_ISOC)
3195 data->isoc = NULL;
3196
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01003197 if (id->driver_info & BTUSB_DIGIANSWER) {
3198 data->cmdreq_type = USB_TYPE_VENDOR;
Szymon Janca6c511c2012-05-23 12:35:46 +02003199 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01003200 }
3201
3202 if (id->driver_info & BTUSB_CSR) {
3203 struct usb_device *udev = data->udev;
Marcel Holtmann81cac642014-01-03 03:02:36 -08003204 u16 bcdDevice = le16_to_cpu(udev->descriptor.bcdDevice);
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01003205
3206 /* Old firmware would otherwise execute USB reset */
Marcel Holtmann81cac642014-01-03 03:02:36 -08003207 if (bcdDevice < 0x117)
Szymon Janca6c511c2012-05-23 12:35:46 +02003208 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmann81cac642014-01-03 03:02:36 -08003209
3210 /* Fake CSR devices with broken commands */
3211 if (bcdDevice <= 0x100)
3212 hdev->setup = btusb_setup_csr;
Jakub Pawlowski49c989a2015-03-17 09:04:17 -07003213
3214 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01003215 }
3216
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02003217 if (id->driver_info & BTUSB_SNIFFER) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02003218 struct usb_device *udev = data->udev;
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02003219
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01003220 /* New sniffer firmware has crippled HCI interface */
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02003221 if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
3222 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
3223 }
3224
Marcel Holtmann3a5ef202014-07-06 14:53:54 +02003225 if (id->driver_info & BTUSB_INTEL_BOOT) {
3226 /* A bug in the bootloader causes that interrupt interface is
3227 * only enabled after receiving SetInterface(0, AltSetting=0).
3228 */
3229 err = usb_set_interface(data->udev, 0, 0);
3230 if (err < 0) {
3231 BT_ERR("failed to set interface 0, alt 0 %d", err);
3232 hci_free_dev(hdev);
3233 return err;
3234 }
3235 }
3236
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02003237 if (data->isoc) {
3238 err = usb_driver_claim_interface(&btusb_driver,
Marcel Holtmann89e75332014-09-16 04:44:50 +02003239 data->isoc, data);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02003240 if (err < 0) {
3241 hci_free_dev(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02003242 return err;
3243 }
3244 }
3245
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003246 err = hci_register_dev(hdev);
3247 if (err < 0) {
3248 hci_free_dev(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003249 return err;
3250 }
3251
3252 usb_set_intfdata(intf, data);
3253
3254 return 0;
3255}
3256
3257static void btusb_disconnect(struct usb_interface *intf)
3258{
3259 struct btusb_data *data = usb_get_intfdata(intf);
3260 struct hci_dev *hdev;
3261
3262 BT_DBG("intf %p", intf);
3263
3264 if (!data)
3265 return;
3266
3267 hdev = data->hdev;
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02003268 usb_set_intfdata(data->intf, NULL);
3269
3270 if (data->isoc)
3271 usb_set_intfdata(data->isoc, NULL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003272
3273 hci_unregister_dev(hdev);
3274
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02003275 if (intf == data->isoc)
3276 usb_driver_release_interface(&btusb_driver, data->intf);
3277 else if (data->isoc)
3278 usb_driver_release_interface(&btusb_driver, data->isoc);
3279
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003280 hci_free_dev(hdev);
3281}
3282
Oliver Neukum7bee5492009-08-24 23:44:59 +02003283#ifdef CONFIG_PM
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003284static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
3285{
3286 struct btusb_data *data = usb_get_intfdata(intf);
3287
3288 BT_DBG("intf %p", intf);
3289
3290 if (data->suspend_count++)
3291 return 0;
3292
Oliver Neukum7bee5492009-08-24 23:44:59 +02003293 spin_lock_irq(&data->txlock);
Alan Stern5b1b0b82011-08-19 23:49:48 +02003294 if (!(PMSG_IS_AUTO(message) && data->tx_in_flight)) {
Oliver Neukum7bee5492009-08-24 23:44:59 +02003295 set_bit(BTUSB_SUSPENDING, &data->flags);
3296 spin_unlock_irq(&data->txlock);
3297 } else {
3298 spin_unlock_irq(&data->txlock);
3299 data->suspend_count--;
3300 return -EBUSY;
3301 }
3302
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003303 cancel_work_sync(&data->work);
3304
Oliver Neukum7bee5492009-08-24 23:44:59 +02003305 btusb_stop_traffic(data);
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003306 usb_kill_anchored_urbs(&data->tx_anchor);
3307
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003308 return 0;
3309}
3310
Oliver Neukum7bee5492009-08-24 23:44:59 +02003311static void play_deferred(struct btusb_data *data)
3312{
3313 struct urb *urb;
3314 int err;
3315
3316 while ((urb = usb_get_from_anchor(&data->deferred))) {
3317 err = usb_submit_urb(urb, GFP_ATOMIC);
3318 if (err < 0)
3319 break;
3320
3321 data->tx_in_flight++;
3322 }
3323 usb_scuttle_anchored_urbs(&data->deferred);
3324}
3325
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003326static int btusb_resume(struct usb_interface *intf)
3327{
3328 struct btusb_data *data = usb_get_intfdata(intf);
3329 struct hci_dev *hdev = data->hdev;
Oliver Neukum7bee5492009-08-24 23:44:59 +02003330 int err = 0;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003331
3332 BT_DBG("intf %p", intf);
3333
3334 if (--data->suspend_count)
3335 return 0;
3336
3337 if (!test_bit(HCI_RUNNING, &hdev->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +02003338 goto done;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003339
3340 if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
3341 err = btusb_submit_intr_urb(hdev, GFP_NOIO);
3342 if (err < 0) {
3343 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02003344 goto failed;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003345 }
3346 }
3347
3348 if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) {
Marcel Holtmann43c2e572009-02-04 17:41:38 +01003349 err = btusb_submit_bulk_urb(hdev, GFP_NOIO);
3350 if (err < 0) {
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003351 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02003352 goto failed;
3353 }
3354
3355 btusb_submit_bulk_urb(hdev, GFP_NOIO);
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003356 }
3357
3358 if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
3359 if (btusb_submit_isoc_urb(hdev, GFP_NOIO) < 0)
3360 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
3361 else
3362 btusb_submit_isoc_urb(hdev, GFP_NOIO);
3363 }
3364
Oliver Neukum7bee5492009-08-24 23:44:59 +02003365 spin_lock_irq(&data->txlock);
3366 play_deferred(data);
3367 clear_bit(BTUSB_SUSPENDING, &data->flags);
3368 spin_unlock_irq(&data->txlock);
3369 schedule_work(&data->work);
3370
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003371 return 0;
Oliver Neukum7bee5492009-08-24 23:44:59 +02003372
3373failed:
3374 usb_scuttle_anchored_urbs(&data->deferred);
3375done:
3376 spin_lock_irq(&data->txlock);
3377 clear_bit(BTUSB_SUSPENDING, &data->flags);
3378 spin_unlock_irq(&data->txlock);
3379
3380 return err;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003381}
Oliver Neukum7bee5492009-08-24 23:44:59 +02003382#endif
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003383
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003384static struct usb_driver btusb_driver = {
3385 .name = "btusb",
3386 .probe = btusb_probe,
3387 .disconnect = btusb_disconnect,
Oliver Neukum7bee5492009-08-24 23:44:59 +02003388#ifdef CONFIG_PM
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003389 .suspend = btusb_suspend,
3390 .resume = btusb_resume,
Oliver Neukum7bee5492009-08-24 23:44:59 +02003391#endif
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003392 .id_table = btusb_table,
Oliver Neukum7bee5492009-08-24 23:44:59 +02003393 .supports_autosuspend = 1,
Sarah Sharpe1f12eb2012-04-23 10:08:51 -07003394 .disable_hub_initiated_lpm = 1,
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003395};
3396
Greg Kroah-Hartman93f15082011-11-18 09:47:34 -08003397module_usb_driver(btusb_driver);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003398
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02003399module_param(disable_scofix, bool, 0644);
3400MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
3401
3402module_param(force_scofix, bool, 0644);
3403MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
3404
3405module_param(reset, bool, 0644);
3406MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
3407
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003408MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
3409MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
3410MODULE_VERSION(VERSION);
3411MODULE_LICENSE("GPL");