blob: fdba79c3877cde3b9a13ba0196b1eed96799912d [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
Dmitry Tunin13879292015-12-05 14:09:36 +0300147 /* Toshiba Corp - Broadcom based */
148 { USB_VENDOR_AND_INTERFACE_INFO(0x0930, 0xff, 0x01, 0x01),
149 .driver_info = BTUSB_BCM_PATCHRAM },
150
Marcel Holtmann40df7832014-07-06 13:29:58 +0200151 /* Intel Bluetooth USB Bootloader (RAM module) */
Marcel Holtmannd92f2df2014-07-06 14:53:55 +0200152 { USB_DEVICE(0x8087, 0x0a5a),
153 .driver_info = BTUSB_INTEL_BOOT | BTUSB_BROKEN_ISOC },
Marcel Holtmann40df7832014-07-06 13:29:58 +0200154
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200155 { } /* Terminating entry */
156};
157
158MODULE_DEVICE_TABLE(usb, btusb_table);
159
Marcel Holtmann54265202013-10-11 07:46:18 -0700160static const struct usb_device_id blacklist_table[] = {
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200161 /* CSR BlueCore devices */
162 { USB_DEVICE(0x0a12, 0x0001), .driver_info = BTUSB_CSR },
163
164 /* Broadcom BCM2033 without firmware */
165 { USB_DEVICE(0x0a5c, 0x2033), .driver_info = BTUSB_IGNORE },
166
Bala Shanmugambe931122010-11-26 17:35:46 +0530167 /* Atheros 3011 with sflash firmware */
Andy Shevchenko0b880062014-02-18 18:26:19 +0200168 { USB_DEVICE(0x0489, 0xe027), .driver_info = BTUSB_IGNORE },
169 { USB_DEVICE(0x0489, 0xe03d), .driver_info = BTUSB_IGNORE },
Alexander Ploumistos2eeff0b2015-02-13 21:05:11 +0200170 { USB_DEVICE(0x04f2, 0xaff1), .driver_info = BTUSB_IGNORE },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200171 { USB_DEVICE(0x0930, 0x0215), .driver_info = BTUSB_IGNORE },
Bala Shanmugambe931122010-11-26 17:35:46 +0530172 { USB_DEVICE(0x0cf3, 0x3002), .driver_info = BTUSB_IGNORE },
Marek Vasut6eda5412012-06-08 14:32:50 +0200173 { USB_DEVICE(0x0cf3, 0xe019), .driver_info = BTUSB_IGNORE },
Andy Ross2a7bccc2011-05-09 16:11:16 -0700174 { USB_DEVICE(0x13d3, 0x3304), .driver_info = BTUSB_IGNORE },
Bala Shanmugambe931122010-11-26 17:35:46 +0530175
Cho, Yu-Chen509e7862011-01-26 17:10:59 +0800176 /* Atheros AR9285 Malbec with sflash firmware */
177 { USB_DEVICE(0x03f0, 0x311d), .driver_info = BTUSB_IGNORE },
178
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530179 /* Atheros 3012 with sflash firmware */
Andy Shevchenko0b880062014-02-18 18:26:19 +0200180 { USB_DEVICE(0x0489, 0xe04d), .driver_info = BTUSB_ATH3012 },
181 { USB_DEVICE(0x0489, 0xe04e), .driver_info = BTUSB_ATH3012 },
182 { USB_DEVICE(0x0489, 0xe056), .driver_info = BTUSB_ATH3012 },
183 { USB_DEVICE(0x0489, 0xe057), .driver_info = BTUSB_ATH3012 },
184 { USB_DEVICE(0x0489, 0xe05f), .driver_info = BTUSB_ATH3012 },
Dmitry Tunina621d2c2015-06-06 20:25:40 +0300185 { USB_DEVICE(0x0489, 0xe076), .driver_info = BTUSB_ATH3012 },
Anantha Krishnan4b552bc2014-10-06 16:31:49 +0530186 { USB_DEVICE(0x0489, 0xe078), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200187 { USB_DEVICE(0x04c5, 0x1330), .driver_info = BTUSB_ATH3012 },
188 { USB_DEVICE(0x04ca, 0x3004), .driver_info = BTUSB_ATH3012 },
189 { USB_DEVICE(0x04ca, 0x3005), .driver_info = BTUSB_ATH3012 },
190 { USB_DEVICE(0x04ca, 0x3006), .driver_info = BTUSB_ATH3012 },
Mohammed Habibulla1fb4e092014-04-17 11:37:13 -0700191 { USB_DEVICE(0x04ca, 0x3007), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200192 { USB_DEVICE(0x04ca, 0x3008), .driver_info = BTUSB_ATH3012 },
193 { USB_DEVICE(0x04ca, 0x300b), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin754bad22015-06-18 20:41:51 +0300194 { USB_DEVICE(0x04ca, 0x300d), .driver_info = BTUSB_ATH3012 },
Dmitry Tuninec0810d2015-05-02 13:36:58 +0300195 { USB_DEVICE(0x04ca, 0x300f), .driver_info = BTUSB_ATH3012 },
Janne Heikkinen134d3b32014-12-09 07:44:51 +0200196 { USB_DEVICE(0x04ca, 0x3010), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200197 { USB_DEVICE(0x0930, 0x0219), .driver_info = BTUSB_ATH3012 },
Dmitry Tunind53162c2015-10-05 19:29:33 +0300198 { USB_DEVICE(0x0930, 0x021c), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200199 { USB_DEVICE(0x0930, 0x0220), .driver_info = BTUSB_ATH3012 },
Vincent Zwanenburg89d29752014-08-08 12:33:56 +0100200 { USB_DEVICE(0x0930, 0x0227), .driver_info = BTUSB_ATH3012 },
Andy Shevchenkoa735f9e2014-02-18 18:26:20 +0200201 { USB_DEVICE(0x0b05, 0x17d0), .driver_info = BTUSB_ATH3012 },
Ming Leid66629c2013-03-15 11:00:39 +0800202 { USB_DEVICE(0x0cf3, 0x0036), .driver_info = BTUSB_ATH3012 },
Steven.Li2d25f8b2011-07-01 14:02:36 +0800203 { USB_DEVICE(0x0cf3, 0x3004), .driver_info = BTUSB_ATH3012 },
Sunguk Lee94a32d102013-03-12 04:41:58 +0900204 { USB_DEVICE(0x0cf3, 0x3008), .driver_info = BTUSB_ATH3012 },
Cho, Yu-Chen07c0ea82012-03-14 22:01:21 +0200205 { USB_DEVICE(0x0cf3, 0x311d), .driver_info = BTUSB_ATH3012 },
Oliver Neukumb1312372014-01-16 15:37:11 +0100206 { USB_DEVICE(0x0cf3, 0x311e), .driver_info = BTUSB_ATH3012 },
Oliver Neukum1e56f1e2014-01-16 16:02:58 +0100207 { USB_DEVICE(0x0cf3, 0x311f), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200208 { USB_DEVICE(0x0cf3, 0x3121), .driver_info = BTUSB_ATH3012 },
Ming Leiebaf5792013-03-18 23:45:11 +0800209 { USB_DEVICE(0x0cf3, 0x817a), .driver_info = BTUSB_ATH3012 },
Dmitry Tunina1475c62015-10-16 11:45:26 +0300210 { USB_DEVICE(0x0cf3, 0x817b), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200211 { USB_DEVICE(0x0cf3, 0xe003), .driver_info = BTUSB_ATH3012 },
AceLan Kaoac713112012-04-19 14:53:45 +0800212 { USB_DEVICE(0x0cf3, 0xe004), .driver_info = BTUSB_ATH3012 },
Peng Chen0a3658c2013-08-30 17:41:40 +0800213 { USB_DEVICE(0x0cf3, 0xe005), .driver_info = BTUSB_ATH3012 },
Wen-chien Jesse Sungca79f232015-05-13 11:39:24 +0800214 { USB_DEVICE(0x0cf3, 0xe006), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200215 { USB_DEVICE(0x13d3, 0x3362), .driver_info = BTUSB_ATH3012 },
216 { USB_DEVICE(0x13d3, 0x3375), .driver_info = BTUSB_ATH3012 },
AceLan Kaoeed307e2012-12-11 11:41:20 +0800217 { USB_DEVICE(0x13d3, 0x3393), .driver_info = BTUSB_ATH3012 },
Sujith Manoharan5b77a1f2013-07-15 09:29:03 +0530218 { USB_DEVICE(0x13d3, 0x3402), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin3bb30a7c2014-11-25 20:19:52 +0300219 { USB_DEVICE(0x13d3, 0x3408), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin033efa92015-01-18 00:16:51 +0300220 { USB_DEVICE(0x13d3, 0x3423), .driver_info = BTUSB_ATH3012 },
Anantha Krishnanfa2f1392014-07-08 19:25:08 +0530221 { USB_DEVICE(0x13d3, 0x3432), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin46c266f2015-06-06 20:29:25 +0300222 { USB_DEVICE(0x13d3, 0x3474), .driver_info = BTUSB_ATH3012 },
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530223
Cho, Yu-Chene9036e32011-02-15 10:20:07 +0800224 /* Atheros AR5BBU12 with sflash firmware */
225 { USB_DEVICE(0x0489, 0xe02c), .driver_info = BTUSB_IGNORE },
226
Michael Gruetzner85d59722012-05-02 22:33:40 +0200227 /* Atheros AR5BBU12 with sflash firmware */
Yevgeniy Melnichukbc21fde2012-08-07 19:48:10 +0530228 { USB_DEVICE(0x0489, 0xe036), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200229 { USB_DEVICE(0x0489, 0xe03c), .driver_info = BTUSB_ATH3012 },
Michael Gruetzner85d59722012-05-02 22:33:40 +0200230
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +0000231 /* QCA ROME chipset */
Wen-chien Jesse Sung20541112015-05-13 11:39:25 +0800232 { USB_DEVICE(0x0cf3, 0xe007), .driver_info = BTUSB_QCA_ROME },
Marcel Holtmannc9e44472015-03-16 23:56:04 -0700233 { USB_DEVICE(0x0cf3, 0xe300), .driver_info = BTUSB_QCA_ROME },
234 { USB_DEVICE(0x0cf3, 0xe360), .driver_info = BTUSB_QCA_ROME },
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +0000235
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200236 /* Broadcom BCM2035 */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100237 { USB_DEVICE(0x0a5c, 0x2009), .driver_info = BTUSB_BCM92035 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200238 { USB_DEVICE(0x0a5c, 0x200a), .driver_info = BTUSB_WRONG_SCO_MTU },
239 { USB_DEVICE(0x0a5c, 0x2035), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200240
241 /* Broadcom BCM2045 */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100242 { USB_DEVICE(0x0a5c, 0x2039), .driver_info = BTUSB_WRONG_SCO_MTU },
243 { USB_DEVICE(0x0a5c, 0x2101), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmannbdbef3d2008-09-23 00:16:35 +0200244
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200245 /* IBM/Lenovo ThinkPad with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100246 { USB_DEVICE(0x0a5c, 0x201e), .driver_info = BTUSB_WRONG_SCO_MTU },
247 { USB_DEVICE(0x0a5c, 0x2110), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200248
249 /* HP laptop with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100250 { USB_DEVICE(0x03f0, 0x171d), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200251
252 /* Dell laptop with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100253 { USB_DEVICE(0x413c, 0x8126), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200254
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100255 /* Dell Wireless 370 and 410 devices */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100256 { USB_DEVICE(0x413c, 0x8152), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100257 { USB_DEVICE(0x413c, 0x8156), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200258
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100259 /* Belkin F8T012 and F8T013 devices */
260 { USB_DEVICE(0x050d, 0x0012), .driver_info = BTUSB_WRONG_SCO_MTU },
261 { USB_DEVICE(0x050d, 0x0013), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200262
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100263 /* Asus WL-BTD202 device */
264 { USB_DEVICE(0x0b05, 0x1715), .driver_info = BTUSB_WRONG_SCO_MTU },
265
266 /* Kensington Bluetooth USB adapter */
267 { USB_DEVICE(0x047d, 0x105e), .driver_info = BTUSB_WRONG_SCO_MTU },
268
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200269 /* RTX Telecom based adapters with buggy SCO support */
270 { USB_DEVICE(0x0400, 0x0807), .driver_info = BTUSB_BROKEN_ISOC },
271 { USB_DEVICE(0x0400, 0x080a), .driver_info = BTUSB_BROKEN_ISOC },
272
273 /* CONWISE Technology based adapters with buggy SCO support */
274 { USB_DEVICE(0x0e5e, 0x6622), .driver_info = BTUSB_BROKEN_ISOC },
275
Marcel Holtmann4fcef8e2015-01-01 17:34:37 -0800276 /* Roper Class 1 Bluetooth Dongle (Silicon Wave based) */
Aleksei Volkov6665e532015-06-08 12:02:10 +0300277 { USB_DEVICE(0x1310, 0x0001), .driver_info = BTUSB_SWAVE },
Marcel Holtmann4fcef8e2015-01-01 17:34:37 -0800278
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200279 /* Digianswer devices */
280 { USB_DEVICE(0x08fd, 0x0001), .driver_info = BTUSB_DIGIANSWER },
281 { USB_DEVICE(0x08fd, 0x0002), .driver_info = BTUSB_IGNORE },
282
283 /* CSR BlueCore Bluetooth Sniffer */
Marcel Holtmann4f64fa82014-07-07 00:12:04 +0200284 { USB_DEVICE(0x0a12, 0x0002),
285 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200286
287 /* Frontline ComProbe Bluetooth Sniffer */
Marcel Holtmann4f64fa82014-07-07 00:12:04 +0200288 { USB_DEVICE(0x16d3, 0x0002),
289 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200290
Marcel Holtmanncb1ee892015-01-28 19:41:42 -0800291 /* Marvell Bluetooth devices */
292 { USB_DEVICE(0x1286, 0x2044), .driver_info = BTUSB_MARVELL },
293 { USB_DEVICE(0x1286, 0x2046), .driver_info = BTUSB_MARVELL },
294
Marcel Holtmannd0ac9eb2015-01-28 19:41:43 -0800295 /* Intel Bluetooth devices */
Marcel Holtmann407550f2015-02-22 15:41:18 -0800296 { USB_DEVICE(0x8087, 0x07da), .driver_info = BTUSB_CSR },
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -0700297 { USB_DEVICE(0x8087, 0x07dc), .driver_info = BTUSB_INTEL },
Tedd Ho-Jeong Anef4e5e42013-11-12 13:10:58 -0800298 { USB_DEVICE(0x8087, 0x0a2a), .driver_info = BTUSB_INTEL },
Marcel Holtmanncda0dd72015-01-26 21:33:48 -0800299 { USB_DEVICE(0x8087, 0x0a2b), .driver_info = BTUSB_INTEL_NEW },
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -0700300
Marcel Holtmannd0ac9eb2015-01-28 19:41:43 -0800301 /* Other Intel Bluetooth devices */
302 { USB_VENDOR_AND_INTERFACE_INFO(0x8087, 0xe0, 0x01, 0x01),
303 .driver_info = BTUSB_IGNORE },
Amitkumar Karwarae8df492014-07-18 14:47:06 -0700304
Daniel Drakea2698a92015-04-16 14:09:55 -0600305 /* Realtek Bluetooth devices */
306 { USB_VENDOR_AND_INTERFACE_INFO(0x0bda, 0xe0, 0x01, 0x01),
307 .driver_info = BTUSB_REALTEK },
308
309 /* Additional Realtek 8723AE Bluetooth devices */
310 { USB_DEVICE(0x0930, 0x021d), .driver_info = BTUSB_REALTEK },
311 { USB_DEVICE(0x13d3, 0x3394), .driver_info = BTUSB_REALTEK },
312
313 /* Additional Realtek 8723BE Bluetooth devices */
314 { USB_DEVICE(0x0489, 0xe085), .driver_info = BTUSB_REALTEK },
315 { USB_DEVICE(0x0489, 0xe08b), .driver_info = BTUSB_REALTEK },
316 { USB_DEVICE(0x13d3, 0x3410), .driver_info = BTUSB_REALTEK },
317 { USB_DEVICE(0x13d3, 0x3416), .driver_info = BTUSB_REALTEK },
318 { USB_DEVICE(0x13d3, 0x3459), .driver_info = BTUSB_REALTEK },
319
320 /* Additional Realtek 8821AE Bluetooth devices */
321 { USB_DEVICE(0x0b05, 0x17dc), .driver_info = BTUSB_REALTEK },
322 { USB_DEVICE(0x13d3, 0x3414), .driver_info = BTUSB_REALTEK },
323 { USB_DEVICE(0x13d3, 0x3458), .driver_info = BTUSB_REALTEK },
324 { USB_DEVICE(0x13d3, 0x3461), .driver_info = BTUSB_REALTEK },
325 { USB_DEVICE(0x13d3, 0x3462), .driver_info = BTUSB_REALTEK },
326
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200327 { } /* Terminating entry */
328};
329
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200330#define BTUSB_MAX_ISOC_FRAMES 10
331
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200332#define BTUSB_INTR_RUNNING 0
333#define BTUSB_BULK_RUNNING 1
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200334#define BTUSB_ISOC_RUNNING 2
Oliver Neukum7bee5492009-08-24 23:44:59 +0200335#define BTUSB_SUSPENDING 3
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -0300336#define BTUSB_DID_ISO_RESUME 4
Marcel Holtmanncda0dd72015-01-26 21:33:48 -0800337#define BTUSB_BOOTLOADER 5
338#define BTUSB_DOWNLOADING 6
Marcel Holtmannce6bb922015-01-28 01:58:40 -0800339#define BTUSB_FIRMWARE_LOADED 7
Marcel Holtmanncda0dd72015-01-26 21:33:48 -0800340#define BTUSB_FIRMWARE_FAILED 8
Marcel Holtmannce6bb922015-01-28 01:58:40 -0800341#define BTUSB_BOOTING 9
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200342
343struct btusb_data {
344 struct hci_dev *hdev;
345 struct usb_device *udev;
Marcel Holtmann5fbcd262008-09-23 00:16:36 +0200346 struct usb_interface *intf;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200347 struct usb_interface *isoc;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200348
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200349 unsigned long flags;
350
351 struct work_struct work;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200352 struct work_struct waker;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200353
Marcel Holtmann803b5832014-09-16 08:00:29 +0200354 struct usb_anchor deferred;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200355 struct usb_anchor tx_anchor;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200356 int tx_in_flight;
357 spinlock_t txlock;
358
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200359 struct usb_anchor intr_anchor;
360 struct usb_anchor bulk_anchor;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200361 struct usb_anchor isoc_anchor;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200362 spinlock_t rxlock;
363
364 struct sk_buff *evt_skb;
365 struct sk_buff *acl_skb;
366 struct sk_buff *sco_skb;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200367
368 struct usb_endpoint_descriptor *intr_ep;
369 struct usb_endpoint_descriptor *bulk_tx_ep;
370 struct usb_endpoint_descriptor *bulk_rx_ep;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200371 struct usb_endpoint_descriptor *isoc_tx_ep;
372 struct usb_endpoint_descriptor *isoc_rx_ep;
373
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100374 __u8 cmdreq_type;
Marcel Holtmann893ba542015-01-28 20:27:34 -0800375 __u8 cmdreq;
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100376
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100377 unsigned int sco_num;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200378 int isoc_altsetting;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +0100379 int suspend_count;
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +0100380
Marcel Holtmann97307f52015-01-12 13:51:10 -0800381 int (*recv_event)(struct hci_dev *hdev, struct sk_buff *skb);
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +0100382 int (*recv_bulk)(struct btusb_data *data, void *buffer, int count);
Kim, Ben Young Taeace31982015-02-15 23:06:14 +0000383
384 int (*setup_on_usb)(struct hci_dev *hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200385};
386
Marcel Holtmann803b5832014-09-16 08:00:29 +0200387static inline void btusb_free_frags(struct btusb_data *data)
388{
389 unsigned long flags;
390
391 spin_lock_irqsave(&data->rxlock, flags);
392
393 kfree_skb(data->evt_skb);
394 data->evt_skb = NULL;
395
396 kfree_skb(data->acl_skb);
397 data->acl_skb = NULL;
398
399 kfree_skb(data->sco_skb);
400 data->sco_skb = NULL;
401
402 spin_unlock_irqrestore(&data->rxlock, flags);
403}
404
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200405static int btusb_recv_intr(struct btusb_data *data, void *buffer, int count)
406{
Marcel Holtmann803b5832014-09-16 08:00:29 +0200407 struct sk_buff *skb;
408 int err = 0;
409
410 spin_lock(&data->rxlock);
411 skb = data->evt_skb;
412
413 while (count) {
414 int len;
415
416 if (!skb) {
417 skb = bt_skb_alloc(HCI_MAX_EVENT_SIZE, GFP_ATOMIC);
418 if (!skb) {
419 err = -ENOMEM;
420 break;
421 }
422
423 bt_cb(skb)->pkt_type = HCI_EVENT_PKT;
424 bt_cb(skb)->expect = HCI_EVENT_HDR_SIZE;
425 }
426
427 len = min_t(uint, bt_cb(skb)->expect, count);
428 memcpy(skb_put(skb, len), buffer, len);
429
430 count -= len;
431 buffer += len;
432 bt_cb(skb)->expect -= len;
433
434 if (skb->len == HCI_EVENT_HDR_SIZE) {
435 /* Complete event header */
436 bt_cb(skb)->expect = hci_event_hdr(skb)->plen;
437
438 if (skb_tailroom(skb) < bt_cb(skb)->expect) {
439 kfree_skb(skb);
440 skb = NULL;
441
442 err = -EILSEQ;
443 break;
444 }
445 }
446
447 if (bt_cb(skb)->expect == 0) {
448 /* Complete frame */
Marcel Holtmann97307f52015-01-12 13:51:10 -0800449 data->recv_event(data->hdev, skb);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200450 skb = NULL;
451 }
452 }
453
454 data->evt_skb = skb;
455 spin_unlock(&data->rxlock);
456
457 return err;
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200458}
459
460static int btusb_recv_bulk(struct btusb_data *data, void *buffer, int count)
461{
Marcel Holtmann803b5832014-09-16 08:00:29 +0200462 struct sk_buff *skb;
463 int err = 0;
464
465 spin_lock(&data->rxlock);
466 skb = data->acl_skb;
467
468 while (count) {
469 int len;
470
471 if (!skb) {
472 skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
473 if (!skb) {
474 err = -ENOMEM;
475 break;
476 }
477
478 bt_cb(skb)->pkt_type = HCI_ACLDATA_PKT;
479 bt_cb(skb)->expect = HCI_ACL_HDR_SIZE;
480 }
481
482 len = min_t(uint, bt_cb(skb)->expect, count);
483 memcpy(skb_put(skb, len), buffer, len);
484
485 count -= len;
486 buffer += len;
487 bt_cb(skb)->expect -= len;
488
489 if (skb->len == HCI_ACL_HDR_SIZE) {
490 __le16 dlen = hci_acl_hdr(skb)->dlen;
491
492 /* Complete ACL header */
493 bt_cb(skb)->expect = __le16_to_cpu(dlen);
494
495 if (skb_tailroom(skb) < bt_cb(skb)->expect) {
496 kfree_skb(skb);
497 skb = NULL;
498
499 err = -EILSEQ;
500 break;
501 }
502 }
503
504 if (bt_cb(skb)->expect == 0) {
505 /* Complete frame */
506 hci_recv_frame(data->hdev, skb);
507 skb = NULL;
508 }
509 }
510
511 data->acl_skb = skb;
512 spin_unlock(&data->rxlock);
513
514 return err;
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200515}
516
517static int btusb_recv_isoc(struct btusb_data *data, void *buffer, int count)
518{
Marcel Holtmann803b5832014-09-16 08:00:29 +0200519 struct sk_buff *skb;
520 int err = 0;
521
522 spin_lock(&data->rxlock);
523 skb = data->sco_skb;
524
525 while (count) {
526 int len;
527
528 if (!skb) {
529 skb = bt_skb_alloc(HCI_MAX_SCO_SIZE, GFP_ATOMIC);
530 if (!skb) {
531 err = -ENOMEM;
532 break;
533 }
534
535 bt_cb(skb)->pkt_type = HCI_SCODATA_PKT;
536 bt_cb(skb)->expect = HCI_SCO_HDR_SIZE;
537 }
538
539 len = min_t(uint, bt_cb(skb)->expect, count);
540 memcpy(skb_put(skb, len), buffer, len);
541
542 count -= len;
543 buffer += len;
544 bt_cb(skb)->expect -= len;
545
546 if (skb->len == HCI_SCO_HDR_SIZE) {
547 /* Complete SCO header */
548 bt_cb(skb)->expect = hci_sco_hdr(skb)->dlen;
549
550 if (skb_tailroom(skb) < bt_cb(skb)->expect) {
551 kfree_skb(skb);
552 skb = NULL;
553
554 err = -EILSEQ;
555 break;
556 }
557 }
558
559 if (bt_cb(skb)->expect == 0) {
560 /* Complete frame */
561 hci_recv_frame(data->hdev, skb);
562 skb = NULL;
563 }
564 }
565
566 data->sco_skb = skb;
567 spin_unlock(&data->rxlock);
568
569 return err;
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200570}
571
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200572static void btusb_intr_complete(struct urb *urb)
573{
574 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +0100575 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200576 int err;
577
Marcel Holtmann89e75332014-09-16 04:44:50 +0200578 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
579 urb->actual_length);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200580
581 if (!test_bit(HCI_RUNNING, &hdev->flags))
582 return;
583
584 if (urb->status == 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200585 hdev->stat.byte_rx += urb->actual_length;
586
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200587 if (btusb_recv_intr(data, urb->transfer_buffer,
588 urb->actual_length) < 0) {
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200589 BT_ERR("%s corrupted event packet", hdev->name);
590 hdev->stat.err_rx++;
591 }
Champion Chen85560c42014-09-06 14:06:08 -0500592 } else if (urb->status == -ENOENT) {
593 /* Avoid suspend failed when usb_kill_urb */
594 return;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200595 }
596
597 if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
598 return;
599
Oliver Neukum7bee5492009-08-24 23:44:59 +0200600 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200601 usb_anchor_urb(urb, &data->intr_anchor);
602
603 err = usb_submit_urb(urb, GFP_ATOMIC);
604 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200605 /* -EPERM: urb is being killed;
606 * -ENODEV: device got disconnected */
607 if (err != -EPERM && err != -ENODEV)
Stefan Seyfried61faddf2010-11-30 21:49:08 +0100608 BT_ERR("%s urb %p failed to resubmit (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200609 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200610 usb_unanchor_urb(urb);
611 }
612}
613
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100614static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200615{
David Herrmann155961e2012-02-09 21:58:32 +0100616 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200617 struct urb *urb;
618 unsigned char *buf;
619 unsigned int pipe;
620 int err, size;
621
622 BT_DBG("%s", hdev->name);
623
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200624 if (!data->intr_ep)
625 return -ENODEV;
626
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100627 urb = usb_alloc_urb(0, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200628 if (!urb)
629 return -ENOMEM;
630
631 size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
632
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100633 buf = kmalloc(size, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200634 if (!buf) {
635 usb_free_urb(urb);
636 return -ENOMEM;
637 }
638
639 pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
640
641 usb_fill_int_urb(urb, data->udev, pipe, buf, size,
Marcel Holtmann89e75332014-09-16 04:44:50 +0200642 btusb_intr_complete, hdev, data->intr_ep->bInterval);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200643
644 urb->transfer_flags |= URB_FREE_BUFFER;
645
646 usb_anchor_urb(urb, &data->intr_anchor);
647
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100648 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200649 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +0200650 if (err != -EPERM && err != -ENODEV)
651 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200652 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200653 usb_unanchor_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200654 }
655
656 usb_free_urb(urb);
657
658 return err;
659}
660
661static void btusb_bulk_complete(struct urb *urb)
662{
663 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +0100664 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200665 int err;
666
Marcel Holtmann89e75332014-09-16 04:44:50 +0200667 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
668 urb->actual_length);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200669
670 if (!test_bit(HCI_RUNNING, &hdev->flags))
671 return;
672
673 if (urb->status == 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200674 hdev->stat.byte_rx += urb->actual_length;
675
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +0100676 if (data->recv_bulk(data, urb->transfer_buffer,
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200677 urb->actual_length) < 0) {
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200678 BT_ERR("%s corrupted ACL packet", hdev->name);
679 hdev->stat.err_rx++;
680 }
Champion Chen85560c42014-09-06 14:06:08 -0500681 } else if (urb->status == -ENOENT) {
682 /* Avoid suspend failed when usb_kill_urb */
683 return;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200684 }
685
686 if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
687 return;
688
689 usb_anchor_urb(urb, &data->bulk_anchor);
Oliver Neukum652fd782009-12-16 19:23:43 +0100690 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200691
692 err = usb_submit_urb(urb, GFP_ATOMIC);
693 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200694 /* -EPERM: urb is being killed;
695 * -ENODEV: device got disconnected */
696 if (err != -EPERM && err != -ENODEV)
Stefan Seyfried61faddf2010-11-30 21:49:08 +0100697 BT_ERR("%s urb %p failed to resubmit (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200698 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200699 usb_unanchor_urb(urb);
700 }
701}
702
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100703static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200704{
David Herrmann155961e2012-02-09 21:58:32 +0100705 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200706 struct urb *urb;
707 unsigned char *buf;
708 unsigned int pipe;
Vikram Kandukuri290ba202009-07-02 14:31:59 +0530709 int err, size = HCI_MAX_FRAME_SIZE;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200710
711 BT_DBG("%s", hdev->name);
712
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200713 if (!data->bulk_rx_ep)
714 return -ENODEV;
715
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100716 urb = usb_alloc_urb(0, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200717 if (!urb)
718 return -ENOMEM;
719
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100720 buf = kmalloc(size, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200721 if (!buf) {
722 usb_free_urb(urb);
723 return -ENOMEM;
724 }
725
726 pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
727
Marcel Holtmann89e75332014-09-16 04:44:50 +0200728 usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
729 btusb_bulk_complete, hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200730
731 urb->transfer_flags |= URB_FREE_BUFFER;
732
Oliver Neukum7bee5492009-08-24 23:44:59 +0200733 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200734 usb_anchor_urb(urb, &data->bulk_anchor);
735
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100736 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200737 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +0200738 if (err != -EPERM && err != -ENODEV)
739 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200740 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200741 usb_unanchor_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200742 }
743
744 usb_free_urb(urb);
745
746 return err;
747}
748
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200749static void btusb_isoc_complete(struct urb *urb)
750{
751 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +0100752 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200753 int i, err;
754
Marcel Holtmann89e75332014-09-16 04:44:50 +0200755 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
756 urb->actual_length);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200757
758 if (!test_bit(HCI_RUNNING, &hdev->flags))
759 return;
760
761 if (urb->status == 0) {
762 for (i = 0; i < urb->number_of_packets; i++) {
763 unsigned int offset = urb->iso_frame_desc[i].offset;
764 unsigned int length = urb->iso_frame_desc[i].actual_length;
765
766 if (urb->iso_frame_desc[i].status)
767 continue;
768
769 hdev->stat.byte_rx += length;
770
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200771 if (btusb_recv_isoc(data, urb->transfer_buffer + offset,
772 length) < 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200773 BT_ERR("%s corrupted SCO packet", hdev->name);
774 hdev->stat.err_rx++;
775 }
776 }
Champion Chen85560c42014-09-06 14:06:08 -0500777 } else if (urb->status == -ENOENT) {
778 /* Avoid suspend failed when usb_kill_urb */
779 return;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200780 }
781
782 if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags))
783 return;
784
785 usb_anchor_urb(urb, &data->isoc_anchor);
786
787 err = usb_submit_urb(urb, GFP_ATOMIC);
788 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200789 /* -EPERM: urb is being killed;
790 * -ENODEV: device got disconnected */
791 if (err != -EPERM && err != -ENODEV)
Stefan Seyfried61faddf2010-11-30 21:49:08 +0100792 BT_ERR("%s urb %p failed to resubmit (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200793 hdev->name, urb, -err);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200794 usb_unanchor_urb(urb);
795 }
796}
797
Jesper Juhl42b16b32011-01-17 00:09:38 +0100798static inline void __fill_isoc_descriptor(struct urb *urb, int len, int mtu)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200799{
800 int i, offset = 0;
801
802 BT_DBG("len %d mtu %d", len, mtu);
803
804 for (i = 0; i < BTUSB_MAX_ISOC_FRAMES && len >= mtu;
805 i++, offset += mtu, len -= mtu) {
806 urb->iso_frame_desc[i].offset = offset;
807 urb->iso_frame_desc[i].length = mtu;
808 }
809
810 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
811 urb->iso_frame_desc[i].offset = offset;
812 urb->iso_frame_desc[i].length = len;
813 i++;
814 }
815
816 urb->number_of_packets = i;
817}
818
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100819static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200820{
David Herrmann155961e2012-02-09 21:58:32 +0100821 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200822 struct urb *urb;
823 unsigned char *buf;
824 unsigned int pipe;
825 int err, size;
826
827 BT_DBG("%s", hdev->name);
828
829 if (!data->isoc_rx_ep)
830 return -ENODEV;
831
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100832 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200833 if (!urb)
834 return -ENOMEM;
835
836 size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) *
837 BTUSB_MAX_ISOC_FRAMES;
838
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100839 buf = kmalloc(size, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200840 if (!buf) {
841 usb_free_urb(urb);
842 return -ENOMEM;
843 }
844
845 pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress);
846
Bing Zhaofa0fb932011-12-20 18:19:00 -0800847 usb_fill_int_urb(urb, data->udev, pipe, buf, size, btusb_isoc_complete,
Marcel Holtmann89e75332014-09-16 04:44:50 +0200848 hdev, data->isoc_rx_ep->bInterval);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200849
Marcel Holtmann89e75332014-09-16 04:44:50 +0200850 urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200851
852 __fill_isoc_descriptor(urb, size,
Marcel Holtmann89e75332014-09-16 04:44:50 +0200853 le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize));
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200854
855 usb_anchor_urb(urb, &data->isoc_anchor);
856
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100857 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200858 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +0200859 if (err != -EPERM && err != -ENODEV)
860 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200861 hdev->name, urb, -err);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200862 usb_unanchor_urb(urb);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200863 }
864
865 usb_free_urb(urb);
866
867 return err;
868}
869
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200870static void btusb_tx_complete(struct urb *urb)
871{
872 struct sk_buff *skb = urb->context;
Marcel Holtmann89e75332014-09-16 04:44:50 +0200873 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
David Herrmann155961e2012-02-09 21:58:32 +0100874 struct btusb_data *data = hci_get_drvdata(hdev);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200875
Marcel Holtmann89e75332014-09-16 04:44:50 +0200876 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
877 urb->actual_length);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200878
879 if (!test_bit(HCI_RUNNING, &hdev->flags))
880 goto done;
881
882 if (!urb->status)
883 hdev->stat.byte_tx += urb->transfer_buffer_length;
884 else
885 hdev->stat.err_tx++;
886
887done:
888 spin_lock(&data->txlock);
889 data->tx_in_flight--;
890 spin_unlock(&data->txlock);
891
892 kfree(urb->setup_packet);
893
894 kfree_skb(skb);
895}
896
897static void btusb_isoc_tx_complete(struct urb *urb)
898{
899 struct sk_buff *skb = urb->context;
Marcel Holtmann89e75332014-09-16 04:44:50 +0200900 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200901
Marcel Holtmann89e75332014-09-16 04:44:50 +0200902 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
903 urb->actual_length);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200904
905 if (!test_bit(HCI_RUNNING, &hdev->flags))
906 goto done;
907
908 if (!urb->status)
909 hdev->stat.byte_tx += urb->transfer_buffer_length;
910 else
911 hdev->stat.err_tx++;
912
913done:
914 kfree(urb->setup_packet);
915
916 kfree_skb(skb);
917}
918
919static int btusb_open(struct hci_dev *hdev)
920{
David Herrmann155961e2012-02-09 21:58:32 +0100921 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200922 int err;
923
924 BT_DBG("%s", hdev->name);
925
Kim, Ben Young Taeace31982015-02-15 23:06:14 +0000926 /* Patching USB firmware files prior to starting any URBs of HCI path
927 * It is more safe to use USB bulk channel for downloading USB patch
928 */
929 if (data->setup_on_usb) {
930 err = data->setup_on_usb(hdev);
Marcel Holtmanneb500422015-04-16 23:15:50 +0200931 if (err < 0)
Kim, Ben Young Taeace31982015-02-15 23:06:14 +0000932 return err;
933 }
934
Oliver Neukum7bee5492009-08-24 23:44:59 +0200935 err = usb_autopm_get_interface(data->intf);
936 if (err < 0)
937 return err;
938
939 data->intf->needs_remote_wakeup = 1;
940
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200941 if (test_and_set_bit(HCI_RUNNING, &hdev->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +0200942 goto done;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200943
944 if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +0200945 goto done;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200946
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100947 err = btusb_submit_intr_urb(hdev, GFP_KERNEL);
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100948 if (err < 0)
949 goto failed;
950
951 err = btusb_submit_bulk_urb(hdev, GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200952 if (err < 0) {
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100953 usb_kill_anchored_urbs(&data->intr_anchor);
954 goto failed;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200955 }
956
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100957 set_bit(BTUSB_BULK_RUNNING, &data->flags);
958 btusb_submit_bulk_urb(hdev, GFP_KERNEL);
959
Oliver Neukum7bee5492009-08-24 23:44:59 +0200960done:
961 usb_autopm_put_interface(data->intf);
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100962 return 0;
963
964failed:
965 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
966 clear_bit(HCI_RUNNING, &hdev->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200967 usb_autopm_put_interface(data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200968 return err;
969}
970
Oliver Neukum7bee5492009-08-24 23:44:59 +0200971static void btusb_stop_traffic(struct btusb_data *data)
972{
973 usb_kill_anchored_urbs(&data->intr_anchor);
974 usb_kill_anchored_urbs(&data->bulk_anchor);
975 usb_kill_anchored_urbs(&data->isoc_anchor);
976}
977
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200978static int btusb_close(struct hci_dev *hdev)
979{
David Herrmann155961e2012-02-09 21:58:32 +0100980 struct btusb_data *data = hci_get_drvdata(hdev);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200981 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200982
983 BT_DBG("%s", hdev->name);
984
985 if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
986 return 0;
987
Marcel Holtmanne8c3c3d2008-09-23 00:16:36 +0200988 cancel_work_sync(&data->work);
Linus Torvalds404291a2009-11-11 13:32:29 -0800989 cancel_work_sync(&data->waker);
Marcel Holtmanne8c3c3d2008-09-23 00:16:36 +0200990
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200991 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200992 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200993 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200994
995 btusb_stop_traffic(data);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200996 btusb_free_frags(data);
997
Oliver Neukum7bee5492009-08-24 23:44:59 +0200998 err = usb_autopm_get_interface(data->intf);
999 if (err < 0)
Oliver Neukum7b8e2c12009-11-13 14:26:23 +01001000 goto failed;
Oliver Neukum7bee5492009-08-24 23:44:59 +02001001
1002 data->intf->needs_remote_wakeup = 0;
1003 usb_autopm_put_interface(data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001004
Oliver Neukum7b8e2c12009-11-13 14:26:23 +01001005failed:
1006 usb_scuttle_anchored_urbs(&data->deferred);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001007 return 0;
1008}
1009
1010static int btusb_flush(struct hci_dev *hdev)
1011{
David Herrmann155961e2012-02-09 21:58:32 +01001012 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001013
1014 BT_DBG("%s", hdev->name);
1015
1016 usb_kill_anchored_urbs(&data->tx_anchor);
Marcel Holtmann803b5832014-09-16 08:00:29 +02001017 btusb_free_frags(data);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001018
1019 return 0;
1020}
1021
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001022static struct urb *alloc_ctrl_urb(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001023{
David Herrmann155961e2012-02-09 21:58:32 +01001024 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001025 struct usb_ctrlrequest *dr;
1026 struct urb *urb;
1027 unsigned int pipe;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001028
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001029 urb = usb_alloc_urb(0, GFP_KERNEL);
1030 if (!urb)
1031 return ERR_PTR(-ENOMEM);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001032
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001033 dr = kmalloc(sizeof(*dr), GFP_KERNEL);
1034 if (!dr) {
1035 usb_free_urb(urb);
1036 return ERR_PTR(-ENOMEM);
1037 }
1038
1039 dr->bRequestType = data->cmdreq_type;
Marcel Holtmann893ba542015-01-28 20:27:34 -08001040 dr->bRequest = data->cmdreq;
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001041 dr->wIndex = 0;
1042 dr->wValue = 0;
1043 dr->wLength = __cpu_to_le16(skb->len);
1044
1045 pipe = usb_sndctrlpipe(data->udev, 0x00);
1046
Marcel Holtmann89e75332014-09-16 04:44:50 +02001047 usb_fill_control_urb(urb, data->udev, pipe, (void *)dr,
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001048 skb->data, skb->len, btusb_tx_complete, skb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001049
Marcel Holtmann89e75332014-09-16 04:44:50 +02001050 skb->dev = (void *)hdev;
Marcel Holtmann7bd8f092013-10-11 06:19:18 -07001051
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001052 return urb;
1053}
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001054
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001055static struct urb *alloc_bulk_urb(struct hci_dev *hdev, struct sk_buff *skb)
1056{
1057 struct btusb_data *data = hci_get_drvdata(hdev);
1058 struct urb *urb;
1059 unsigned int pipe;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001060
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001061 if (!data->bulk_tx_ep)
1062 return ERR_PTR(-ENODEV);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001063
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001064 urb = usb_alloc_urb(0, GFP_KERNEL);
1065 if (!urb)
1066 return ERR_PTR(-ENOMEM);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001067
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001068 pipe = usb_sndbulkpipe(data->udev, data->bulk_tx_ep->bEndpointAddress);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001069
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001070 usb_fill_bulk_urb(urb, data->udev, pipe,
1071 skb->data, skb->len, btusb_tx_complete, skb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001072
Marcel Holtmann89e75332014-09-16 04:44:50 +02001073 skb->dev = (void *)hdev;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001074
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001075 return urb;
1076}
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001077
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001078static struct urb *alloc_isoc_urb(struct hci_dev *hdev, struct sk_buff *skb)
1079{
1080 struct btusb_data *data = hci_get_drvdata(hdev);
1081 struct urb *urb;
1082 unsigned int pipe;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001083
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001084 if (!data->isoc_tx_ep)
1085 return ERR_PTR(-ENODEV);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001086
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001087 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_KERNEL);
1088 if (!urb)
1089 return ERR_PTR(-ENOMEM);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001090
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001091 pipe = usb_sndisocpipe(data->udev, data->isoc_tx_ep->bEndpointAddress);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001092
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001093 usb_fill_int_urb(urb, data->udev, pipe,
1094 skb->data, skb->len, btusb_isoc_tx_complete,
1095 skb, data->isoc_tx_ep->bInterval);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001096
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001097 urb->transfer_flags = URB_ISO_ASAP;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001098
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001099 __fill_isoc_descriptor(urb, skb->len,
1100 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001101
Marcel Holtmann89e75332014-09-16 04:44:50 +02001102 skb->dev = (void *)hdev;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001103
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001104 return urb;
1105}
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001106
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001107static int submit_tx_urb(struct hci_dev *hdev, struct urb *urb)
1108{
1109 struct btusb_data *data = hci_get_drvdata(hdev);
1110 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001111
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001112 usb_anchor_urb(urb, &data->tx_anchor);
1113
Johan Hedberge9753ef2014-09-14 08:49:34 +03001114 err = usb_submit_urb(urb, GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001115 if (err < 0) {
Paul Bolle5a9b80e2011-10-09 12:12:16 +02001116 if (err != -EPERM && err != -ENODEV)
1117 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +02001118 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001119 kfree(urb->setup_packet);
1120 usb_unanchor_urb(urb);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001121 } else {
1122 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001123 }
1124
Cong Wang54a8a792011-11-22 09:32:57 +08001125 usb_free_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001126 return err;
1127}
1128
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001129static int submit_or_queue_tx_urb(struct hci_dev *hdev, struct urb *urb)
1130{
1131 struct btusb_data *data = hci_get_drvdata(hdev);
1132 unsigned long flags;
1133 bool suspending;
1134
1135 spin_lock_irqsave(&data->txlock, flags);
1136 suspending = test_bit(BTUSB_SUSPENDING, &data->flags);
1137 if (!suspending)
1138 data->tx_in_flight++;
1139 spin_unlock_irqrestore(&data->txlock, flags);
1140
1141 if (!suspending)
1142 return submit_tx_urb(hdev, urb);
1143
1144 usb_anchor_urb(urb, &data->deferred);
1145 schedule_work(&data->waker);
1146
1147 usb_free_urb(urb);
1148 return 0;
1149}
1150
1151static int btusb_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
1152{
1153 struct urb *urb;
1154
1155 BT_DBG("%s", hdev->name);
1156
1157 if (!test_bit(HCI_RUNNING, &hdev->flags))
1158 return -EBUSY;
1159
1160 switch (bt_cb(skb)->pkt_type) {
1161 case HCI_COMMAND_PKT:
1162 urb = alloc_ctrl_urb(hdev, skb);
1163 if (IS_ERR(urb))
1164 return PTR_ERR(urb);
1165
1166 hdev->stat.cmd_tx++;
1167 return submit_or_queue_tx_urb(hdev, urb);
1168
1169 case HCI_ACLDATA_PKT:
1170 urb = alloc_bulk_urb(hdev, skb);
1171 if (IS_ERR(urb))
1172 return PTR_ERR(urb);
1173
1174 hdev->stat.acl_tx++;
1175 return submit_or_queue_tx_urb(hdev, urb);
1176
1177 case HCI_SCODATA_PKT:
1178 if (hci_conn_num(hdev, SCO_LINK) < 1)
1179 return -ENODEV;
1180
1181 urb = alloc_isoc_urb(hdev, skb);
1182 if (IS_ERR(urb))
1183 return PTR_ERR(urb);
1184
1185 hdev->stat.sco_tx++;
1186 return submit_tx_urb(hdev, urb);
1187 }
1188
1189 return -EILSEQ;
1190}
1191
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001192static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
1193{
David Herrmann155961e2012-02-09 21:58:32 +01001194 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001195
1196 BT_DBG("%s evt %d", hdev->name, evt);
1197
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001198 if (hci_conn_num(hdev, SCO_LINK) != data->sco_num) {
1199 data->sco_num = hci_conn_num(hdev, SCO_LINK);
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001200 schedule_work(&data->work);
Marcel Holtmanna780efa2008-11-30 12:17:12 +01001201 }
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001202}
1203
Jesper Juhl42b16b32011-01-17 00:09:38 +01001204static inline int __set_isoc_interface(struct hci_dev *hdev, int altsetting)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001205{
David Herrmann155961e2012-02-09 21:58:32 +01001206 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001207 struct usb_interface *intf = data->isoc;
1208 struct usb_endpoint_descriptor *ep_desc;
1209 int i, err;
1210
1211 if (!data->isoc)
1212 return -ENODEV;
1213
1214 err = usb_set_interface(data->udev, 1, altsetting);
1215 if (err < 0) {
1216 BT_ERR("%s setting interface failed (%d)", hdev->name, -err);
1217 return err;
1218 }
1219
1220 data->isoc_altsetting = altsetting;
1221
1222 data->isoc_tx_ep = NULL;
1223 data->isoc_rx_ep = NULL;
1224
1225 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
1226 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
1227
1228 if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) {
1229 data->isoc_tx_ep = ep_desc;
1230 continue;
1231 }
1232
1233 if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) {
1234 data->isoc_rx_ep = ep_desc;
1235 continue;
1236 }
1237 }
1238
1239 if (!data->isoc_tx_ep || !data->isoc_rx_ep) {
1240 BT_ERR("%s invalid SCO descriptors", hdev->name);
1241 return -ENODEV;
1242 }
1243
1244 return 0;
1245}
1246
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001247static void btusb_work(struct work_struct *work)
1248{
1249 struct btusb_data *data = container_of(work, struct btusb_data, work);
1250 struct hci_dev *hdev = data->hdev;
Mikel Astizf4001d22012-04-11 08:48:51 +02001251 int new_alts;
Oliver Neukum7bee5492009-08-24 23:44:59 +02001252 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001253
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001254 if (data->sco_num > 0) {
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -03001255 if (!test_bit(BTUSB_DID_ISO_RESUME, &data->flags)) {
Oliver Neukum8efdd0c2011-02-11 13:00:06 +01001256 err = usb_autopm_get_interface(data->isoc ? data->isoc : data->intf);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001257 if (err < 0) {
1258 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1259 usb_kill_anchored_urbs(&data->isoc_anchor);
1260 return;
1261 }
1262
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -03001263 set_bit(BTUSB_DID_ISO_RESUME, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001264 }
Mikel Astizf4001d22012-04-11 08:48:51 +02001265
1266 if (hdev->voice_setting & 0x0020) {
1267 static const int alts[3] = { 2, 4, 5 };
Marcel Holtmann89e75332014-09-16 04:44:50 +02001268
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001269 new_alts = alts[data->sco_num - 1];
Mikel Astizf4001d22012-04-11 08:48:51 +02001270 } else {
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001271 new_alts = data->sco_num;
Mikel Astizf4001d22012-04-11 08:48:51 +02001272 }
1273
1274 if (data->isoc_altsetting != new_alts) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001275 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1276 usb_kill_anchored_urbs(&data->isoc_anchor);
1277
Mikel Astizf4001d22012-04-11 08:48:51 +02001278 if (__set_isoc_interface(hdev, new_alts) < 0)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001279 return;
1280 }
1281
1282 if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001283 if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001284 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1285 else
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001286 btusb_submit_isoc_urb(hdev, GFP_KERNEL);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001287 }
1288 } else {
1289 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1290 usb_kill_anchored_urbs(&data->isoc_anchor);
1291
1292 __set_isoc_interface(hdev, 0);
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -03001293 if (test_and_clear_bit(BTUSB_DID_ISO_RESUME, &data->flags))
Oliver Neukum8efdd0c2011-02-11 13:00:06 +01001294 usb_autopm_put_interface(data->isoc ? data->isoc : data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001295 }
1296}
1297
Oliver Neukum7bee5492009-08-24 23:44:59 +02001298static void btusb_waker(struct work_struct *work)
1299{
1300 struct btusb_data *data = container_of(work, struct btusb_data, waker);
1301 int err;
1302
1303 err = usb_autopm_get_interface(data->intf);
1304 if (err < 0)
1305 return;
1306
1307 usb_autopm_put_interface(data->intf);
1308}
1309
Daniel Drake6576fe42015-02-23 18:16:47 -06001310static struct sk_buff *btusb_read_local_version(struct hci_dev *hdev)
1311{
1312 struct sk_buff *skb;
1313
1314 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
1315 HCI_INIT_TIMEOUT);
1316 if (IS_ERR(skb)) {
1317 BT_ERR("%s: HCI_OP_READ_LOCAL_VERSION failed (%ld)",
1318 hdev->name, PTR_ERR(skb));
1319 return skb;
1320 }
1321
1322 if (skb->len != sizeof(struct hci_rp_read_local_version)) {
1323 BT_ERR("%s: HCI_OP_READ_LOCAL_VERSION event length mismatch",
1324 hdev->name);
1325 kfree_skb(skb);
1326 return ERR_PTR(-EIO);
1327 }
1328
1329 return skb;
1330}
1331
Marcel Holtmann9f8f9622013-04-10 08:11:35 -07001332static int btusb_setup_bcm92035(struct hci_dev *hdev)
1333{
1334 struct sk_buff *skb;
1335 u8 val = 0x00;
1336
1337 BT_DBG("%s", hdev->name);
1338
1339 skb = __hci_cmd_sync(hdev, 0xfc3b, 1, &val, HCI_INIT_TIMEOUT);
1340 if (IS_ERR(skb))
1341 BT_ERR("BCM92035 command failed (%ld)", -PTR_ERR(skb));
1342 else
1343 kfree_skb(skb);
1344
1345 return 0;
1346}
1347
Marcel Holtmann81cac642014-01-03 03:02:36 -08001348static int btusb_setup_csr(struct hci_dev *hdev)
1349{
1350 struct hci_rp_read_local_version *rp;
1351 struct sk_buff *skb;
1352 int ret;
1353
1354 BT_DBG("%s", hdev->name);
1355
Daniel Drake6576fe42015-02-23 18:16:47 -06001356 skb = btusb_read_local_version(hdev);
1357 if (IS_ERR(skb))
Marcel Holtmann81cac642014-01-03 03:02:36 -08001358 return -PTR_ERR(skb);
Marcel Holtmann81cac642014-01-03 03:02:36 -08001359
Marcel Holtmann89e75332014-09-16 04:44:50 +02001360 rp = (struct hci_rp_read_local_version *)skb->data;
Marcel Holtmann81cac642014-01-03 03:02:36 -08001361
1362 if (!rp->status) {
1363 if (le16_to_cpu(rp->manufacturer) != 10) {
1364 /* Clear the reset quirk since this is not an actual
1365 * early Bluetooth 1.1 device from CSR.
1366 */
1367 clear_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
1368
1369 /* These fake CSR controllers have all a broken
1370 * stored link key handling and so just disable it.
1371 */
1372 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY,
1373 &hdev->quirks);
1374 }
1375 }
1376
1377 ret = -bt_to_errno(rp->status);
1378
1379 kfree_skb(skb);
1380
1381 return ret;
1382}
1383
Daniel Drakea2698a92015-04-16 14:09:55 -06001384#define RTL_FRAG_LEN 252
1385
1386struct rtl_download_cmd {
1387 __u8 index;
1388 __u8 data[RTL_FRAG_LEN];
1389} __packed;
1390
1391struct rtl_download_response {
1392 __u8 status;
1393 __u8 index;
1394} __packed;
1395
1396struct rtl_rom_version_evt {
1397 __u8 status;
1398 __u8 version;
1399} __packed;
1400
1401struct rtl_epatch_header {
1402 __u8 signature[8];
1403 __le32 fw_version;
1404 __le16 num_patches;
1405} __packed;
1406
1407#define RTL_EPATCH_SIGNATURE "Realtech"
1408#define RTL_ROM_LMP_3499 0x3499
1409#define RTL_ROM_LMP_8723A 0x1200
1410#define RTL_ROM_LMP_8723B 0x8723
1411#define RTL_ROM_LMP_8821A 0x8821
1412#define RTL_ROM_LMP_8761A 0x8761
1413
1414static int rtl_read_rom_version(struct hci_dev *hdev, u8 *version)
1415{
1416 struct rtl_rom_version_evt *rom_version;
1417 struct sk_buff *skb;
1418 int ret;
1419
1420 /* Read RTL ROM version command */
1421 skb = __hci_cmd_sync(hdev, 0xfc6d, 0, NULL, HCI_INIT_TIMEOUT);
1422 if (IS_ERR(skb)) {
1423 BT_ERR("%s: Read ROM version failed (%ld)",
1424 hdev->name, PTR_ERR(skb));
1425 return PTR_ERR(skb);
1426 }
1427
1428 if (skb->len != sizeof(*rom_version)) {
1429 BT_ERR("%s: RTL version event length mismatch", hdev->name);
1430 kfree_skb(skb);
1431 return -EIO;
1432 }
1433
1434 rom_version = (struct rtl_rom_version_evt *)skb->data;
1435 BT_INFO("%s: rom_version status=%x version=%x",
1436 hdev->name, rom_version->status, rom_version->version);
1437
1438 ret = rom_version->status;
1439 if (ret == 0)
1440 *version = rom_version->version;
1441
1442 kfree_skb(skb);
1443 return ret;
1444}
1445
1446static int rtl8723b_parse_firmware(struct hci_dev *hdev, u16 lmp_subver,
1447 const struct firmware *fw,
1448 unsigned char **_buf)
1449{
1450 const u8 extension_sig[] = { 0x51, 0x04, 0xfd, 0x77 };
1451 struct rtl_epatch_header *epatch_info;
1452 unsigned char *buf;
1453 int i, ret, len;
1454 size_t min_size;
1455 u8 opcode, length, data, rom_version = 0;
1456 int project_id = -1;
1457 const unsigned char *fwptr, *chip_id_base;
1458 const unsigned char *patch_length_base, *patch_offset_base;
1459 u32 patch_offset = 0;
1460 u16 patch_length, num_patches;
1461 const u16 project_id_to_lmp_subver[] = {
1462 RTL_ROM_LMP_8723A,
1463 RTL_ROM_LMP_8723B,
1464 RTL_ROM_LMP_8821A,
1465 RTL_ROM_LMP_8761A
1466 };
1467
1468 ret = rtl_read_rom_version(hdev, &rom_version);
1469 if (ret)
1470 return -bt_to_errno(ret);
1471
1472 min_size = sizeof(struct rtl_epatch_header) + sizeof(extension_sig) + 3;
1473 if (fw->size < min_size)
1474 return -EINVAL;
1475
1476 fwptr = fw->data + fw->size - sizeof(extension_sig);
1477 if (memcmp(fwptr, extension_sig, sizeof(extension_sig)) != 0) {
1478 BT_ERR("%s: extension section signature mismatch", hdev->name);
1479 return -EINVAL;
1480 }
1481
1482 /* Loop from the end of the firmware parsing instructions, until
1483 * we find an instruction that identifies the "project ID" for the
1484 * hardware supported by this firwmare file.
1485 * Once we have that, we double-check that that project_id is suitable
1486 * for the hardware we are working with.
1487 */
1488 while (fwptr >= fw->data + (sizeof(struct rtl_epatch_header) + 3)) {
1489 opcode = *--fwptr;
1490 length = *--fwptr;
1491 data = *--fwptr;
1492
1493 BT_DBG("check op=%x len=%x data=%x", opcode, length, data);
1494
1495 if (opcode == 0xff) /* EOF */
1496 break;
1497
1498 if (length == 0) {
1499 BT_ERR("%s: found instruction with length 0",
1500 hdev->name);
1501 return -EINVAL;
1502 }
1503
1504 if (opcode == 0 && length == 1) {
1505 project_id = data;
1506 break;
1507 }
1508
1509 fwptr -= length;
1510 }
1511
1512 if (project_id < 0) {
1513 BT_ERR("%s: failed to find version instruction", hdev->name);
1514 return -EINVAL;
1515 }
1516
Dan Carpenterc57ddfa2015-04-20 18:51:35 +03001517 if (project_id >= ARRAY_SIZE(project_id_to_lmp_subver)) {
Daniel Drakea2698a92015-04-16 14:09:55 -06001518 BT_ERR("%s: unknown project id %d", hdev->name, project_id);
1519 return -EINVAL;
1520 }
1521
1522 if (lmp_subver != project_id_to_lmp_subver[project_id]) {
1523 BT_ERR("%s: firmware is for %x but this is a %x", hdev->name,
1524 project_id_to_lmp_subver[project_id], lmp_subver);
1525 return -EINVAL;
1526 }
1527
1528 epatch_info = (struct rtl_epatch_header *)fw->data;
1529 if (memcmp(epatch_info->signature, RTL_EPATCH_SIGNATURE, 8) != 0) {
1530 BT_ERR("%s: bad EPATCH signature", hdev->name);
1531 return -EINVAL;
1532 }
1533
1534 num_patches = le16_to_cpu(epatch_info->num_patches);
1535 BT_DBG("fw_version=%x, num_patches=%d",
1536 le32_to_cpu(epatch_info->fw_version), num_patches);
1537
1538 /* After the rtl_epatch_header there is a funky patch metadata section.
1539 * Assuming 2 patches, the layout is:
1540 * ChipID1 ChipID2 PatchLength1 PatchLength2 PatchOffset1 PatchOffset2
1541 *
1542 * Find the right patch for this chip.
1543 */
1544 min_size += 8 * num_patches;
1545 if (fw->size < min_size)
1546 return -EINVAL;
1547
1548 chip_id_base = fw->data + sizeof(struct rtl_epatch_header);
1549 patch_length_base = chip_id_base + (sizeof(u16) * num_patches);
1550 patch_offset_base = patch_length_base + (sizeof(u16) * num_patches);
1551 for (i = 0; i < num_patches; i++) {
1552 u16 chip_id = get_unaligned_le16(chip_id_base +
1553 (i * sizeof(u16)));
1554 if (chip_id == rom_version + 1) {
1555 patch_length = get_unaligned_le16(patch_length_base +
1556 (i * sizeof(u16)));
1557 patch_offset = get_unaligned_le32(patch_offset_base +
1558 (i * sizeof(u32)));
1559 break;
1560 }
1561 }
1562
1563 if (!patch_offset) {
1564 BT_ERR("%s: didn't find patch for chip id %d",
1565 hdev->name, rom_version);
1566 return -EINVAL;
1567 }
1568
1569 BT_DBG("length=%x offset=%x index %d", patch_length, patch_offset, i);
1570 min_size = patch_offset + patch_length;
1571 if (fw->size < min_size)
1572 return -EINVAL;
1573
1574 /* Copy the firmware into a new buffer and write the version at
1575 * the end.
1576 */
1577 len = patch_length;
1578 buf = kmemdup(fw->data + patch_offset, patch_length, GFP_KERNEL);
1579 if (!buf)
1580 return -ENOMEM;
1581
1582 memcpy(buf + patch_length - 4, &epatch_info->fw_version, 4);
1583
1584 *_buf = buf;
1585 return len;
1586}
1587
1588static int rtl_download_firmware(struct hci_dev *hdev,
1589 const unsigned char *data, int fw_len)
1590{
1591 struct rtl_download_cmd *dl_cmd;
1592 int frag_num = fw_len / RTL_FRAG_LEN + 1;
1593 int frag_len = RTL_FRAG_LEN;
1594 int ret = 0;
1595 int i;
1596
1597 dl_cmd = kmalloc(sizeof(struct rtl_download_cmd), GFP_KERNEL);
1598 if (!dl_cmd)
1599 return -ENOMEM;
1600
1601 for (i = 0; i < frag_num; i++) {
1602 struct rtl_download_response *dl_resp;
1603 struct sk_buff *skb;
1604
1605 BT_DBG("download fw (%d/%d)", i, frag_num);
1606
1607 dl_cmd->index = i;
1608 if (i == (frag_num - 1)) {
1609 dl_cmd->index |= 0x80; /* data end */
1610 frag_len = fw_len % RTL_FRAG_LEN;
1611 }
1612 memcpy(dl_cmd->data, data, frag_len);
1613
1614 /* Send download command */
1615 skb = __hci_cmd_sync(hdev, 0xfc20, frag_len + 1, dl_cmd,
1616 HCI_INIT_TIMEOUT);
1617 if (IS_ERR(skb)) {
1618 BT_ERR("%s: download fw command failed (%ld)",
1619 hdev->name, PTR_ERR(skb));
1620 ret = -PTR_ERR(skb);
1621 goto out;
1622 }
1623
1624 if (skb->len != sizeof(*dl_resp)) {
1625 BT_ERR("%s: download fw event length mismatch",
1626 hdev->name);
1627 kfree_skb(skb);
1628 ret = -EIO;
1629 goto out;
1630 }
1631
1632 dl_resp = (struct rtl_download_response *)skb->data;
1633 if (dl_resp->status != 0) {
1634 kfree_skb(skb);
1635 ret = bt_to_errno(dl_resp->status);
1636 goto out;
1637 }
1638
1639 kfree_skb(skb);
1640 data += RTL_FRAG_LEN;
1641 }
1642
1643out:
1644 kfree(dl_cmd);
1645 return ret;
1646}
1647
1648static int btusb_setup_rtl8723a(struct hci_dev *hdev)
1649{
1650 struct btusb_data *data = dev_get_drvdata(&hdev->dev);
1651 struct usb_device *udev = interface_to_usbdev(data->intf);
1652 const struct firmware *fw;
1653 int ret;
1654
1655 BT_INFO("%s: rtl: loading rtl_bt/rtl8723a_fw.bin", hdev->name);
1656 ret = request_firmware(&fw, "rtl_bt/rtl8723a_fw.bin", &udev->dev);
1657 if (ret < 0) {
1658 BT_ERR("%s: Failed to load rtl_bt/rtl8723a_fw.bin", hdev->name);
1659 return ret;
1660 }
1661
1662 if (fw->size < 8) {
1663 ret = -EINVAL;
1664 goto out;
1665 }
1666
1667 /* Check that the firmware doesn't have the epatch signature
1668 * (which is only for RTL8723B and newer).
1669 */
1670 if (!memcmp(fw->data, RTL_EPATCH_SIGNATURE, 8)) {
1671 BT_ERR("%s: unexpected EPATCH signature!", hdev->name);
1672 ret = -EINVAL;
1673 goto out;
1674 }
1675
1676 ret = rtl_download_firmware(hdev, fw->data, fw->size);
1677
1678out:
1679 release_firmware(fw);
1680 return ret;
1681}
1682
1683static int btusb_setup_rtl8723b(struct hci_dev *hdev, u16 lmp_subver,
1684 const char *fw_name)
1685{
1686 struct btusb_data *data = dev_get_drvdata(&hdev->dev);
1687 struct usb_device *udev = interface_to_usbdev(data->intf);
1688 unsigned char *fw_data = NULL;
1689 const struct firmware *fw;
1690 int ret;
1691
1692 BT_INFO("%s: rtl: loading %s", hdev->name, fw_name);
1693 ret = request_firmware(&fw, fw_name, &udev->dev);
1694 if (ret < 0) {
1695 BT_ERR("%s: Failed to load %s", hdev->name, fw_name);
1696 return ret;
1697 }
1698
1699 ret = rtl8723b_parse_firmware(hdev, lmp_subver, fw, &fw_data);
1700 if (ret < 0)
1701 goto out;
1702
1703 ret = rtl_download_firmware(hdev, fw_data, ret);
1704 kfree(fw_data);
1705 if (ret < 0)
1706 goto out;
1707
1708out:
1709 release_firmware(fw);
1710 return ret;
1711}
1712
1713static int btusb_setup_realtek(struct hci_dev *hdev)
1714{
1715 struct sk_buff *skb;
1716 struct hci_rp_read_local_version *resp;
1717 u16 lmp_subver;
1718
1719 skb = btusb_read_local_version(hdev);
1720 if (IS_ERR(skb))
1721 return -PTR_ERR(skb);
1722
1723 resp = (struct hci_rp_read_local_version *)skb->data;
1724 BT_INFO("%s: rtl: examining hci_ver=%02x hci_rev=%04x lmp_ver=%02x "
1725 "lmp_subver=%04x", hdev->name, resp->hci_ver, resp->hci_rev,
1726 resp->lmp_ver, resp->lmp_subver);
1727
1728 lmp_subver = le16_to_cpu(resp->lmp_subver);
1729 kfree_skb(skb);
1730
1731 /* Match a set of subver values that correspond to stock firmware,
1732 * which is not compatible with standard btusb.
1733 * If matched, upload an alternative firmware that does conform to
1734 * standard btusb. Once that firmware is uploaded, the subver changes
1735 * to a different value.
1736 */
1737 switch (lmp_subver) {
1738 case RTL_ROM_LMP_8723A:
1739 case RTL_ROM_LMP_3499:
1740 return btusb_setup_rtl8723a(hdev);
1741 case RTL_ROM_LMP_8723B:
1742 return btusb_setup_rtl8723b(hdev, lmp_subver,
1743 "rtl_bt/rtl8723b_fw.bin");
1744 case RTL_ROM_LMP_8821A:
1745 return btusb_setup_rtl8723b(hdev, lmp_subver,
1746 "rtl_bt/rtl8821a_fw.bin");
1747 case RTL_ROM_LMP_8761A:
1748 return btusb_setup_rtl8723b(hdev, lmp_subver,
1749 "rtl_bt/rtl8761a_fw.bin");
1750 default:
1751 BT_INFO("rtl: assuming no firmware upload needed.");
1752 return 0;
1753 }
1754}
1755
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001756static const struct firmware *btusb_setup_intel_get_fw(struct hci_dev *hdev,
Marcel Holtmann89e75332014-09-16 04:44:50 +02001757 struct intel_version *ver)
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001758{
1759 const struct firmware *fw;
1760 char fwname[64];
1761 int ret;
1762
1763 snprintf(fwname, sizeof(fwname),
1764 "intel/ibt-hw-%x.%x.%x-fw-%x.%x.%x.%x.%x.bseq",
1765 ver->hw_platform, ver->hw_variant, ver->hw_revision,
1766 ver->fw_variant, ver->fw_revision, ver->fw_build_num,
1767 ver->fw_build_ww, ver->fw_build_yy);
1768
1769 ret = request_firmware(&fw, fwname, &hdev->dev);
1770 if (ret < 0) {
1771 if (ret == -EINVAL) {
1772 BT_ERR("%s Intel firmware file request failed (%d)",
1773 hdev->name, ret);
1774 return NULL;
1775 }
1776
1777 BT_ERR("%s failed to open Intel firmware file: %s(%d)",
1778 hdev->name, fwname, ret);
1779
1780 /* If the correct firmware patch file is not found, use the
1781 * default firmware patch file instead
1782 */
1783 snprintf(fwname, sizeof(fwname), "intel/ibt-hw-%x.%x.bseq",
1784 ver->hw_platform, ver->hw_variant);
1785 if (request_firmware(&fw, fwname, &hdev->dev) < 0) {
1786 BT_ERR("%s failed to open default Intel fw file: %s",
1787 hdev->name, fwname);
1788 return NULL;
1789 }
1790 }
1791
1792 BT_INFO("%s: Intel Bluetooth firmware file: %s", hdev->name, fwname);
1793
1794 return fw;
1795}
1796
1797static int btusb_setup_intel_patching(struct hci_dev *hdev,
1798 const struct firmware *fw,
1799 const u8 **fw_ptr, int *disable_patch)
1800{
1801 struct sk_buff *skb;
1802 struct hci_command_hdr *cmd;
1803 const u8 *cmd_param;
1804 struct hci_event_hdr *evt = NULL;
1805 const u8 *evt_param = NULL;
1806 int remain = fw->size - (*fw_ptr - fw->data);
1807
1808 /* The first byte indicates the types of the patch command or event.
1809 * 0x01 means HCI command and 0x02 is HCI event. If the first bytes
1810 * in the current firmware buffer doesn't start with 0x01 or
1811 * the size of remain buffer is smaller than HCI command header,
1812 * the firmware file is corrupted and it should stop the patching
1813 * process.
1814 */
1815 if (remain > HCI_COMMAND_HDR_SIZE && *fw_ptr[0] != 0x01) {
1816 BT_ERR("%s Intel fw corrupted: invalid cmd read", hdev->name);
1817 return -EINVAL;
1818 }
1819 (*fw_ptr)++;
1820 remain--;
1821
1822 cmd = (struct hci_command_hdr *)(*fw_ptr);
1823 *fw_ptr += sizeof(*cmd);
1824 remain -= sizeof(*cmd);
1825
1826 /* Ensure that the remain firmware data is long enough than the length
1827 * of command parameter. If not, the firmware file is corrupted.
1828 */
1829 if (remain < cmd->plen) {
1830 BT_ERR("%s Intel fw corrupted: invalid cmd len", hdev->name);
1831 return -EFAULT;
1832 }
1833
1834 /* If there is a command that loads a patch in the firmware
1835 * file, then enable the patch upon success, otherwise just
1836 * disable the manufacturer mode, for example patch activation
1837 * is not required when the default firmware patch file is used
1838 * because there are no patch data to load.
1839 */
1840 if (*disable_patch && le16_to_cpu(cmd->opcode) == 0xfc8e)
1841 *disable_patch = 0;
1842
1843 cmd_param = *fw_ptr;
1844 *fw_ptr += cmd->plen;
1845 remain -= cmd->plen;
1846
1847 /* This reads the expected events when the above command is sent to the
1848 * device. Some vendor commands expects more than one events, for
1849 * example command status event followed by vendor specific event.
1850 * For this case, it only keeps the last expected event. so the command
1851 * can be sent with __hci_cmd_sync_ev() which returns the sk_buff of
1852 * last expected event.
1853 */
1854 while (remain > HCI_EVENT_HDR_SIZE && *fw_ptr[0] == 0x02) {
1855 (*fw_ptr)++;
1856 remain--;
1857
1858 evt = (struct hci_event_hdr *)(*fw_ptr);
1859 *fw_ptr += sizeof(*evt);
1860 remain -= sizeof(*evt);
1861
1862 if (remain < evt->plen) {
1863 BT_ERR("%s Intel fw corrupted: invalid evt len",
1864 hdev->name);
1865 return -EFAULT;
1866 }
1867
1868 evt_param = *fw_ptr;
1869 *fw_ptr += evt->plen;
1870 remain -= evt->plen;
1871 }
1872
1873 /* Every HCI commands in the firmware file has its correspond event.
1874 * If event is not found or remain is smaller than zero, the firmware
1875 * file is corrupted.
1876 */
1877 if (!evt || !evt_param || remain < 0) {
1878 BT_ERR("%s Intel fw corrupted: invalid evt read", hdev->name);
1879 return -EFAULT;
1880 }
1881
1882 skb = __hci_cmd_sync_ev(hdev, le16_to_cpu(cmd->opcode), cmd->plen,
1883 cmd_param, evt->evt, HCI_INIT_TIMEOUT);
1884 if (IS_ERR(skb)) {
1885 BT_ERR("%s sending Intel patch command (0x%4.4x) failed (%ld)",
1886 hdev->name, cmd->opcode, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001887 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001888 }
1889
1890 /* It ensures that the returned event matches the event data read from
1891 * the firmware file. At fist, it checks the length and then
1892 * the contents of the event.
1893 */
1894 if (skb->len != evt->plen) {
1895 BT_ERR("%s mismatch event length (opcode 0x%4.4x)", hdev->name,
1896 le16_to_cpu(cmd->opcode));
1897 kfree_skb(skb);
1898 return -EFAULT;
1899 }
1900
1901 if (memcmp(skb->data, evt_param, evt->plen)) {
1902 BT_ERR("%s mismatch event parameter (opcode 0x%4.4x)",
1903 hdev->name, le16_to_cpu(cmd->opcode));
1904 kfree_skb(skb);
1905 return -EFAULT;
1906 }
1907 kfree_skb(skb);
1908
1909 return 0;
1910}
1911
1912static int btusb_setup_intel(struct hci_dev *hdev)
1913{
1914 struct sk_buff *skb;
1915 const struct firmware *fw;
1916 const u8 *fw_ptr;
1917 int disable_patch;
1918 struct intel_version *ver;
1919
1920 const u8 mfg_enable[] = { 0x01, 0x00 };
1921 const u8 mfg_disable[] = { 0x00, 0x00 };
1922 const u8 mfg_reset_deactivate[] = { 0x00, 0x01 };
1923 const u8 mfg_reset_activate[] = { 0x00, 0x02 };
1924
1925 BT_DBG("%s", hdev->name);
1926
1927 /* The controller has a bug with the first HCI command sent to it
1928 * returning number of completed commands as zero. This would stall the
1929 * command processing in the Bluetooth core.
1930 *
1931 * As a workaround, send HCI Reset command first which will reset the
1932 * number of completed commands and allow normal command processing
1933 * from now on.
1934 */
1935 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
1936 if (IS_ERR(skb)) {
1937 BT_ERR("%s sending initial HCI reset command failed (%ld)",
1938 hdev->name, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001939 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001940 }
1941 kfree_skb(skb);
1942
1943 /* Read Intel specific controller version first to allow selection of
1944 * which firmware file to load.
1945 *
1946 * The returned information are hardware variant and revision plus
1947 * firmware variant, revision and build number.
1948 */
1949 skb = __hci_cmd_sync(hdev, 0xfc05, 0, NULL, HCI_INIT_TIMEOUT);
1950 if (IS_ERR(skb)) {
1951 BT_ERR("%s reading Intel fw version command failed (%ld)",
1952 hdev->name, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001953 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001954 }
1955
1956 if (skb->len != sizeof(*ver)) {
1957 BT_ERR("%s Intel version event length mismatch", hdev->name);
1958 kfree_skb(skb);
1959 return -EIO;
1960 }
1961
1962 ver = (struct intel_version *)skb->data;
1963 if (ver->status) {
1964 BT_ERR("%s Intel fw version event failed (%02x)", hdev->name,
1965 ver->status);
1966 kfree_skb(skb);
1967 return -bt_to_errno(ver->status);
1968 }
1969
1970 BT_INFO("%s: read Intel version: %02x%02x%02x%02x%02x%02x%02x%02x%02x",
1971 hdev->name, ver->hw_platform, ver->hw_variant,
1972 ver->hw_revision, ver->fw_variant, ver->fw_revision,
1973 ver->fw_build_num, ver->fw_build_ww, ver->fw_build_yy,
1974 ver->fw_patch_num);
1975
1976 /* fw_patch_num indicates the version of patch the device currently
1977 * have. If there is no patch data in the device, it is always 0x00.
1978 * So, if it is other than 0x00, no need to patch the deivce again.
1979 */
1980 if (ver->fw_patch_num) {
1981 BT_INFO("%s: Intel device is already patched. patch num: %02x",
1982 hdev->name, ver->fw_patch_num);
1983 kfree_skb(skb);
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07001984 btintel_check_bdaddr(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001985 return 0;
1986 }
1987
1988 /* Opens the firmware patch file based on the firmware version read
1989 * from the controller. If it fails to open the matching firmware
1990 * patch file, it tries to open the default firmware patch file.
1991 * If no patch file is found, allow the device to operate without
1992 * a patch.
1993 */
1994 fw = btusb_setup_intel_get_fw(hdev, ver);
1995 if (!fw) {
1996 kfree_skb(skb);
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07001997 btintel_check_bdaddr(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001998 return 0;
1999 }
2000 fw_ptr = fw->data;
2001
Marcel Holtmanna5b637f2015-06-07 09:42:19 +02002002 kfree_skb(skb);
2003
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002004 /* This Intel specific command enables the manufacturer mode of the
2005 * controller.
2006 *
2007 * Only while this mode is enabled, the driver can download the
2008 * firmware patch data and configuration parameters.
2009 */
2010 skb = __hci_cmd_sync(hdev, 0xfc11, 2, mfg_enable, HCI_INIT_TIMEOUT);
2011 if (IS_ERR(skb)) {
2012 BT_ERR("%s entering Intel manufacturer mode failed (%ld)",
2013 hdev->name, PTR_ERR(skb));
2014 release_firmware(fw);
Adam Leed9c78e92013-07-10 10:02:12 +08002015 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002016 }
2017
2018 if (skb->data[0]) {
2019 u8 evt_status = skb->data[0];
Marcel Holtmann89e75332014-09-16 04:44:50 +02002020
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002021 BT_ERR("%s enable Intel manufacturer mode event failed (%02x)",
2022 hdev->name, evt_status);
2023 kfree_skb(skb);
2024 release_firmware(fw);
2025 return -bt_to_errno(evt_status);
2026 }
2027 kfree_skb(skb);
2028
2029 disable_patch = 1;
2030
2031 /* The firmware data file consists of list of Intel specific HCI
2032 * commands and its expected events. The first byte indicates the
2033 * type of the message, either HCI command or HCI event.
2034 *
2035 * It reads the command and its expected event from the firmware file,
2036 * and send to the controller. Once __hci_cmd_sync_ev() returns,
2037 * the returned event is compared with the event read from the firmware
2038 * file and it will continue until all the messages are downloaded to
2039 * the controller.
2040 *
2041 * Once the firmware patching is completed successfully,
2042 * the manufacturer mode is disabled with reset and activating the
2043 * downloaded patch.
2044 *
2045 * If the firmware patching fails, the manufacturer mode is
2046 * disabled with reset and deactivating the patch.
2047 *
2048 * If the default patch file is used, no reset is done when disabling
2049 * the manufacturer.
2050 */
2051 while (fw->size > fw_ptr - fw->data) {
2052 int ret;
2053
2054 ret = btusb_setup_intel_patching(hdev, fw, &fw_ptr,
2055 &disable_patch);
2056 if (ret < 0)
2057 goto exit_mfg_deactivate;
2058 }
2059
2060 release_firmware(fw);
2061
2062 if (disable_patch)
2063 goto exit_mfg_disable;
2064
2065 /* Patching completed successfully and disable the manufacturer mode
2066 * with reset and activate the downloaded firmware patches.
2067 */
2068 skb = __hci_cmd_sync(hdev, 0xfc11, sizeof(mfg_reset_activate),
2069 mfg_reset_activate, HCI_INIT_TIMEOUT);
2070 if (IS_ERR(skb)) {
2071 BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
2072 hdev->name, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08002073 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002074 }
2075 kfree_skb(skb);
2076
2077 BT_INFO("%s: Intel Bluetooth firmware patch completed and activated",
2078 hdev->name);
2079
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07002080 btintel_check_bdaddr(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002081 return 0;
2082
2083exit_mfg_disable:
2084 /* Disable the manufacturer mode without reset */
2085 skb = __hci_cmd_sync(hdev, 0xfc11, sizeof(mfg_disable), mfg_disable,
2086 HCI_INIT_TIMEOUT);
2087 if (IS_ERR(skb)) {
2088 BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
2089 hdev->name, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08002090 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002091 }
2092 kfree_skb(skb);
2093
2094 BT_INFO("%s: Intel Bluetooth firmware patch completed", hdev->name);
Marcel Holtmann40cb0982014-07-02 12:06:45 +02002095
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07002096 btintel_check_bdaddr(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002097 return 0;
2098
2099exit_mfg_deactivate:
2100 release_firmware(fw);
2101
2102 /* Patching failed. Disable the manufacturer mode with reset and
2103 * deactivate the downloaded firmware patches.
2104 */
2105 skb = __hci_cmd_sync(hdev, 0xfc11, sizeof(mfg_reset_deactivate),
2106 mfg_reset_deactivate, HCI_INIT_TIMEOUT);
2107 if (IS_ERR(skb)) {
2108 BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
2109 hdev->name, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08002110 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002111 }
2112 kfree_skb(skb);
2113
2114 BT_INFO("%s: Intel Bluetooth firmware patch completed and deactivated",
2115 hdev->name);
2116
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07002117 btintel_check_bdaddr(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002118 return 0;
2119}
2120
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002121static int inject_cmd_complete(struct hci_dev *hdev, __u16 opcode)
2122{
2123 struct sk_buff *skb;
2124 struct hci_event_hdr *hdr;
2125 struct hci_ev_cmd_complete *evt;
2126
2127 skb = bt_skb_alloc(sizeof(*hdr) + sizeof(*evt) + 1, GFP_ATOMIC);
2128 if (!skb)
2129 return -ENOMEM;
2130
2131 hdr = (struct hci_event_hdr *)skb_put(skb, sizeof(*hdr));
2132 hdr->evt = HCI_EV_CMD_COMPLETE;
2133 hdr->plen = sizeof(*evt) + 1;
2134
2135 evt = (struct hci_ev_cmd_complete *)skb_put(skb, sizeof(*evt));
2136 evt->ncmd = 0x01;
2137 evt->opcode = cpu_to_le16(opcode);
2138
2139 *skb_put(skb, 1) = 0x00;
2140
2141 bt_cb(skb)->pkt_type = HCI_EVENT_PKT;
2142
2143 return hci_recv_frame(hdev, skb);
2144}
2145
2146static int btusb_recv_bulk_intel(struct btusb_data *data, void *buffer,
2147 int count)
2148{
2149 /* When the device is in bootloader mode, then it can send
2150 * events via the bulk endpoint. These events are treated the
2151 * same way as the ones received from the interrupt endpoint.
2152 */
2153 if (test_bit(BTUSB_BOOTLOADER, &data->flags))
2154 return btusb_recv_intr(data, buffer, count);
2155
2156 return btusb_recv_bulk(data, buffer, count);
2157}
2158
Marcel Holtmannccd6da22015-04-09 00:35:19 -07002159static void btusb_intel_bootup(struct btusb_data *data, const void *ptr,
2160 unsigned int len)
2161{
2162 const struct intel_bootup *evt = ptr;
2163
2164 if (len != sizeof(*evt))
2165 return;
2166
2167 if (test_and_clear_bit(BTUSB_BOOTING, &data->flags)) {
2168 smp_mb__after_atomic();
2169 wake_up_bit(&data->flags, BTUSB_BOOTING);
2170 }
2171}
2172
2173static void btusb_intel_secure_send_result(struct btusb_data *data,
2174 const void *ptr, unsigned int len)
2175{
2176 const struct intel_secure_send_result *evt = ptr;
2177
2178 if (len != sizeof(*evt))
2179 return;
2180
2181 if (evt->result)
2182 set_bit(BTUSB_FIRMWARE_FAILED, &data->flags);
2183
2184 if (test_and_clear_bit(BTUSB_DOWNLOADING, &data->flags) &&
2185 test_bit(BTUSB_FIRMWARE_LOADED, &data->flags)) {
2186 smp_mb__after_atomic();
2187 wake_up_bit(&data->flags, BTUSB_DOWNLOADING);
2188 }
2189}
2190
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002191static int btusb_recv_event_intel(struct hci_dev *hdev, struct sk_buff *skb)
2192{
2193 struct btusb_data *data = hci_get_drvdata(hdev);
2194
2195 if (test_bit(BTUSB_BOOTLOADER, &data->flags)) {
2196 struct hci_event_hdr *hdr = (void *)skb->data;
2197
Marcel Holtmannccd6da22015-04-09 00:35:19 -07002198 if (skb->len > HCI_EVENT_HDR_SIZE && hdr->evt == 0xff &&
2199 hdr->plen > 0) {
2200 const void *ptr = skb->data + HCI_EVENT_HDR_SIZE + 1;
2201 unsigned int len = skb->len - HCI_EVENT_HDR_SIZE - 1;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002202
Marcel Holtmannccd6da22015-04-09 00:35:19 -07002203 switch (skb->data[2]) {
2204 case 0x02:
2205 /* When switching to the operational firmware
2206 * the device sends a vendor specific event
2207 * indicating that the bootup completed.
2208 */
2209 btusb_intel_bootup(data, ptr, len);
2210 break;
2211 case 0x06:
2212 /* When the firmware loading completes the
2213 * device sends out a vendor specific event
2214 * indicating the result of the firmware
2215 * loading.
2216 */
2217 btusb_intel_secure_send_result(data, ptr, len);
2218 break;
Johan Hedbergfad70972015-01-30 10:58:55 +02002219 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002220 }
2221 }
2222
2223 return hci_recv_frame(hdev, skb);
2224}
2225
2226static int btusb_send_frame_intel(struct hci_dev *hdev, struct sk_buff *skb)
2227{
2228 struct btusb_data *data = hci_get_drvdata(hdev);
2229 struct urb *urb;
2230
2231 BT_DBG("%s", hdev->name);
2232
2233 if (!test_bit(HCI_RUNNING, &hdev->flags))
2234 return -EBUSY;
2235
2236 switch (bt_cb(skb)->pkt_type) {
2237 case HCI_COMMAND_PKT:
2238 if (test_bit(BTUSB_BOOTLOADER, &data->flags)) {
2239 struct hci_command_hdr *cmd = (void *)skb->data;
2240 __u16 opcode = le16_to_cpu(cmd->opcode);
2241
2242 /* When in bootloader mode and the command 0xfc09
2243 * is received, it needs to be send down the
2244 * bulk endpoint. So allocate a bulk URB instead.
2245 */
2246 if (opcode == 0xfc09)
2247 urb = alloc_bulk_urb(hdev, skb);
2248 else
2249 urb = alloc_ctrl_urb(hdev, skb);
2250
2251 /* When the 0xfc01 command is issued to boot into
2252 * the operational firmware, it will actually not
2253 * send a command complete event. To keep the flow
2254 * control working inject that event here.
2255 */
2256 if (opcode == 0xfc01)
2257 inject_cmd_complete(hdev, opcode);
2258 } else {
2259 urb = alloc_ctrl_urb(hdev, skb);
2260 }
2261 if (IS_ERR(urb))
2262 return PTR_ERR(urb);
2263
2264 hdev->stat.cmd_tx++;
2265 return submit_or_queue_tx_urb(hdev, urb);
2266
2267 case HCI_ACLDATA_PKT:
2268 urb = alloc_bulk_urb(hdev, skb);
2269 if (IS_ERR(urb))
2270 return PTR_ERR(urb);
2271
2272 hdev->stat.acl_tx++;
2273 return submit_or_queue_tx_urb(hdev, urb);
2274
2275 case HCI_SCODATA_PKT:
2276 if (hci_conn_num(hdev, SCO_LINK) < 1)
2277 return -ENODEV;
2278
2279 urb = alloc_isoc_urb(hdev, skb);
2280 if (IS_ERR(urb))
2281 return PTR_ERR(urb);
2282
2283 hdev->stat.sco_tx++;
2284 return submit_tx_urb(hdev, urb);
2285 }
2286
2287 return -EILSEQ;
2288}
2289
2290static int btusb_intel_secure_send(struct hci_dev *hdev, u8 fragment_type,
2291 u32 plen, const void *param)
2292{
2293 while (plen > 0) {
2294 struct sk_buff *skb;
2295 u8 cmd_param[253], fragment_len = (plen > 252) ? 252 : plen;
2296
2297 cmd_param[0] = fragment_type;
2298 memcpy(cmd_param + 1, param, fragment_len);
2299
2300 skb = __hci_cmd_sync(hdev, 0xfc09, fragment_len + 1,
2301 cmd_param, HCI_INIT_TIMEOUT);
2302 if (IS_ERR(skb))
2303 return PTR_ERR(skb);
2304
2305 kfree_skb(skb);
2306
2307 plen -= fragment_len;
2308 param += fragment_len;
2309 }
2310
2311 return 0;
2312}
2313
2314static void btusb_intel_version_info(struct hci_dev *hdev,
2315 struct intel_version *ver)
2316{
2317 const char *variant;
2318
2319 switch (ver->fw_variant) {
2320 case 0x06:
2321 variant = "Bootloader";
2322 break;
2323 case 0x23:
2324 variant = "Firmware";
2325 break;
2326 default:
2327 return;
2328 }
2329
2330 BT_INFO("%s: %s revision %u.%u build %u week %u %u", hdev->name,
2331 variant, ver->fw_revision >> 4, ver->fw_revision & 0x0f,
2332 ver->fw_build_num, ver->fw_build_ww, 2000 + ver->fw_build_yy);
2333}
2334
2335static int btusb_setup_intel_new(struct hci_dev *hdev)
2336{
2337 static const u8 reset_param[] = { 0x00, 0x01, 0x00, 0x01,
2338 0x00, 0x08, 0x04, 0x00 };
2339 struct btusb_data *data = hci_get_drvdata(hdev);
2340 struct sk_buff *skb;
2341 struct intel_version *ver;
2342 struct intel_boot_params *params;
2343 const struct firmware *fw;
2344 const u8 *fw_ptr;
Marcel Holtmann2f5ce3a2015-06-07 09:47:08 +02002345 u32 frag_len;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002346 char fwname[64];
2347 ktime_t calltime, delta, rettime;
2348 unsigned long long duration;
2349 int err;
2350
2351 BT_DBG("%s", hdev->name);
2352
2353 calltime = ktime_get();
2354
2355 /* Read the Intel version information to determine if the device
2356 * is in bootloader mode or if it already has operational firmware
2357 * loaded.
2358 */
2359 skb = __hci_cmd_sync(hdev, 0xfc05, 0, NULL, HCI_INIT_TIMEOUT);
2360 if (IS_ERR(skb)) {
2361 BT_ERR("%s: Reading Intel version information failed (%ld)",
2362 hdev->name, PTR_ERR(skb));
2363 return PTR_ERR(skb);
2364 }
2365
2366 if (skb->len != sizeof(*ver)) {
2367 BT_ERR("%s: Intel version event size mismatch", hdev->name);
2368 kfree_skb(skb);
2369 return -EILSEQ;
2370 }
2371
2372 ver = (struct intel_version *)skb->data;
2373 if (ver->status) {
2374 BT_ERR("%s: Intel version command failure (%02x)",
2375 hdev->name, ver->status);
2376 err = -bt_to_errno(ver->status);
2377 kfree_skb(skb);
2378 return err;
2379 }
2380
2381 /* The hardware platform number has a fixed value of 0x37 and
2382 * for now only accept this single value.
2383 */
2384 if (ver->hw_platform != 0x37) {
2385 BT_ERR("%s: Unsupported Intel hardware platform (%u)",
2386 hdev->name, ver->hw_platform);
2387 kfree_skb(skb);
2388 return -EINVAL;
2389 }
2390
2391 /* At the moment only the hardware variant iBT 3.0 (LnP/SfP) is
2392 * supported by this firmware loading method. This check has been
2393 * put in place to ensure correct forward compatibility options
2394 * when newer hardware variants come along.
2395 */
2396 if (ver->hw_variant != 0x0b) {
2397 BT_ERR("%s: Unsupported Intel hardware variant (%u)",
2398 hdev->name, ver->hw_variant);
2399 kfree_skb(skb);
2400 return -EINVAL;
2401 }
2402
2403 btusb_intel_version_info(hdev, ver);
2404
2405 /* The firmware variant determines if the device is in bootloader
2406 * mode or is running operational firmware. The value 0x06 identifies
2407 * the bootloader and the value 0x23 identifies the operational
2408 * firmware.
2409 *
2410 * When the operational firmware is already present, then only
2411 * the check for valid Bluetooth device address is needed. This
2412 * determines if the device will be added as configured or
2413 * unconfigured controller.
2414 *
2415 * It is not possible to use the Secure Boot Parameters in this
2416 * case since that command is only available in bootloader mode.
2417 */
2418 if (ver->fw_variant == 0x23) {
2419 kfree_skb(skb);
2420 clear_bit(BTUSB_BOOTLOADER, &data->flags);
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07002421 btintel_check_bdaddr(hdev);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002422 return 0;
2423 }
2424
2425 /* If the device is not in bootloader mode, then the only possible
2426 * choice is to return an error and abort the device initialization.
2427 */
2428 if (ver->fw_variant != 0x06) {
2429 BT_ERR("%s: Unsupported Intel firmware variant (%u)",
2430 hdev->name, ver->fw_variant);
2431 kfree_skb(skb);
2432 return -ENODEV;
2433 }
2434
2435 kfree_skb(skb);
2436
2437 /* Read the secure boot parameters to identify the operating
2438 * details of the bootloader.
2439 */
2440 skb = __hci_cmd_sync(hdev, 0xfc0d, 0, NULL, HCI_INIT_TIMEOUT);
2441 if (IS_ERR(skb)) {
2442 BT_ERR("%s: Reading Intel boot parameters failed (%ld)",
2443 hdev->name, PTR_ERR(skb));
2444 return PTR_ERR(skb);
2445 }
2446
2447 if (skb->len != sizeof(*params)) {
2448 BT_ERR("%s: Intel boot parameters size mismatch", hdev->name);
2449 kfree_skb(skb);
2450 return -EILSEQ;
2451 }
2452
2453 params = (struct intel_boot_params *)skb->data;
2454 if (params->status) {
2455 BT_ERR("%s: Intel boot parameters command failure (%02x)",
2456 hdev->name, params->status);
2457 err = -bt_to_errno(params->status);
2458 kfree_skb(skb);
2459 return err;
2460 }
2461
2462 BT_INFO("%s: Device revision is %u", hdev->name,
2463 le16_to_cpu(params->dev_revid));
2464
2465 BT_INFO("%s: Secure boot is %s", hdev->name,
2466 params->secure_boot ? "enabled" : "disabled");
2467
2468 BT_INFO("%s: Minimum firmware build %u week %u %u", hdev->name,
2469 params->min_fw_build_nn, params->min_fw_build_cw,
2470 2000 + params->min_fw_build_yy);
2471
2472 /* It is required that every single firmware fragment is acknowledged
2473 * with a command complete event. If the boot parameters indicate
2474 * that this bootloader does not send them, then abort the setup.
2475 */
2476 if (params->limited_cce != 0x00) {
2477 BT_ERR("%s: Unsupported Intel firmware loading method (%u)",
2478 hdev->name, params->limited_cce);
2479 kfree_skb(skb);
2480 return -EINVAL;
2481 }
2482
2483 /* If the OTP has no valid Bluetooth device address, then there will
2484 * also be no valid address for the operational firmware.
2485 */
2486 if (!bacmp(&params->otp_bdaddr, BDADDR_ANY)) {
2487 BT_INFO("%s: No device address configured", hdev->name);
2488 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
2489 }
2490
2491 /* With this Intel bootloader only the hardware variant and device
2492 * revision information are used to select the right firmware.
2493 *
2494 * Currently this bootloader support is limited to hardware variant
2495 * iBT 3.0 (LnP/SfP) which is identified by the value 11 (0x0b).
2496 */
2497 snprintf(fwname, sizeof(fwname), "intel/ibt-11-%u.sfi",
2498 le16_to_cpu(params->dev_revid));
2499
2500 err = request_firmware(&fw, fwname, &hdev->dev);
2501 if (err < 0) {
2502 BT_ERR("%s: Failed to load Intel firmware file (%d)",
2503 hdev->name, err);
2504 kfree_skb(skb);
2505 return err;
2506 }
2507
2508 BT_INFO("%s: Found device firmware: %s", hdev->name, fwname);
2509
2510 kfree_skb(skb);
2511
2512 if (fw->size < 644) {
2513 BT_ERR("%s: Invalid size of firmware file (%zu)",
2514 hdev->name, fw->size);
2515 err = -EBADF;
2516 goto done;
2517 }
2518
2519 set_bit(BTUSB_DOWNLOADING, &data->flags);
2520
2521 /* Start the firmware download transaction with the Init fragment
2522 * represented by the 128 bytes of CSS header.
2523 */
2524 err = btusb_intel_secure_send(hdev, 0x00, 128, fw->data);
2525 if (err < 0) {
2526 BT_ERR("%s: Failed to send firmware header (%d)",
2527 hdev->name, err);
2528 goto done;
2529 }
2530
2531 /* Send the 256 bytes of public key information from the firmware
2532 * as the PKey fragment.
2533 */
2534 err = btusb_intel_secure_send(hdev, 0x03, 256, fw->data + 128);
2535 if (err < 0) {
2536 BT_ERR("%s: Failed to send firmware public key (%d)",
2537 hdev->name, err);
2538 goto done;
2539 }
2540
2541 /* Send the 256 bytes of signature information from the firmware
2542 * as the Sign fragment.
2543 */
2544 err = btusb_intel_secure_send(hdev, 0x02, 256, fw->data + 388);
2545 if (err < 0) {
2546 BT_ERR("%s: Failed to send firmware signature (%d)",
2547 hdev->name, err);
2548 goto done;
2549 }
2550
2551 fw_ptr = fw->data + 644;
Marcel Holtmann2f5ce3a2015-06-07 09:47:08 +02002552 frag_len = 0;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002553
2554 while (fw_ptr - fw->data < fw->size) {
Marcel Holtmann2f5ce3a2015-06-07 09:47:08 +02002555 struct hci_command_hdr *cmd = (void *)(fw_ptr + frag_len);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002556
Marcel Holtmann2f5ce3a2015-06-07 09:47:08 +02002557 frag_len += sizeof(*cmd) + cmd->plen;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002558
Marcel Holtmann2f5ce3a2015-06-07 09:47:08 +02002559 /* The paramter length of the secure send command requires
2560 * a 4 byte alignment. It happens so that the firmware file
2561 * contains proper Intel_NOP commands to align the fragments
2562 * as needed.
2563 *
2564 * Send set of commands with 4 byte alignment from the
2565 * firmware data buffer as a single Data fragement.
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002566 */
Marcel Holtmann2f5ce3a2015-06-07 09:47:08 +02002567 if (!(frag_len % 4)) {
2568 err = btusb_intel_secure_send(hdev, 0x01, frag_len,
2569 fw_ptr);
2570 if (err < 0) {
2571 BT_ERR("%s: Failed to send firmware data (%d)",
2572 hdev->name, err);
2573 goto done;
2574 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002575
Marcel Holtmann2f5ce3a2015-06-07 09:47:08 +02002576 fw_ptr += frag_len;
2577 frag_len = 0;
2578 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002579 }
2580
Marcel Holtmannce6bb922015-01-28 01:58:40 -08002581 set_bit(BTUSB_FIRMWARE_LOADED, &data->flags);
2582
Johan Hedberga087a982015-01-30 10:58:54 +02002583 BT_INFO("%s: Waiting for firmware download to complete", hdev->name);
2584
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002585 /* Before switching the device into operational mode and with that
2586 * booting the loaded firmware, wait for the bootloader notification
2587 * that all fragments have been successfully received.
2588 *
Johan Hedberga087a982015-01-30 10:58:54 +02002589 * When the event processing receives the notification, then the
2590 * BTUSB_DOWNLOADING flag will be cleared.
2591 *
2592 * The firmware loading should not take longer than 5 seconds
2593 * and thus just timeout if that happens and fail the setup
2594 * of this device.
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002595 */
Johan Hedberg129a7692015-02-14 09:33:35 +02002596 err = wait_on_bit_timeout(&data->flags, BTUSB_DOWNLOADING,
2597 TASK_INTERRUPTIBLE,
2598 msecs_to_jiffies(5000));
Johan Hedberga087a982015-01-30 10:58:54 +02002599 if (err == 1) {
2600 BT_ERR("%s: Firmware loading interrupted", hdev->name);
2601 err = -EINTR;
2602 goto done;
2603 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002604
Johan Hedberga087a982015-01-30 10:58:54 +02002605 if (err) {
2606 BT_ERR("%s: Firmware loading timeout", hdev->name);
2607 err = -ETIMEDOUT;
2608 goto done;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002609 }
2610
2611 if (test_bit(BTUSB_FIRMWARE_FAILED, &data->flags)) {
2612 BT_ERR("%s: Firmware loading failed", hdev->name);
2613 err = -ENOEXEC;
2614 goto done;
2615 }
2616
2617 rettime = ktime_get();
2618 delta = ktime_sub(rettime, calltime);
2619 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
2620
2621 BT_INFO("%s: Firmware loaded in %llu usecs", hdev->name, duration);
2622
2623done:
2624 release_firmware(fw);
2625
2626 if (err < 0)
2627 return err;
2628
2629 calltime = ktime_get();
2630
2631 set_bit(BTUSB_BOOTING, &data->flags);
2632
2633 skb = __hci_cmd_sync(hdev, 0xfc01, sizeof(reset_param), reset_param,
2634 HCI_INIT_TIMEOUT);
2635 if (IS_ERR(skb))
2636 return PTR_ERR(skb);
2637
2638 kfree_skb(skb);
2639
2640 /* The bootloader will not indicate when the device is ready. This
2641 * is done by the operational firmware sending bootup notification.
Johan Hedbergfad70972015-01-30 10:58:55 +02002642 *
2643 * Booting into operational firmware should not take longer than
2644 * 1 second. However if that happens, then just fail the setup
2645 * since something went wrong.
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002646 */
Johan Hedbergfad70972015-01-30 10:58:55 +02002647 BT_INFO("%s: Waiting for device to boot", hdev->name);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002648
Johan Hedberg129a7692015-02-14 09:33:35 +02002649 err = wait_on_bit_timeout(&data->flags, BTUSB_BOOTING,
2650 TASK_INTERRUPTIBLE,
2651 msecs_to_jiffies(1000));
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002652
Johan Hedbergfad70972015-01-30 10:58:55 +02002653 if (err == 1) {
2654 BT_ERR("%s: Device boot interrupted", hdev->name);
2655 return -EINTR;
2656 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002657
Johan Hedbergfad70972015-01-30 10:58:55 +02002658 if (err) {
2659 BT_ERR("%s: Device boot timeout", hdev->name);
2660 return -ETIMEDOUT;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002661 }
2662
2663 rettime = ktime_get();
2664 delta = ktime_sub(rettime, calltime);
2665 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
2666
2667 BT_INFO("%s: Device booted in %llu usecs", hdev->name, duration);
2668
2669 clear_bit(BTUSB_BOOTLOADER, &data->flags);
2670
2671 return 0;
2672}
2673
Marcel Holtmann385a7682015-01-28 11:09:56 -08002674static void btusb_hw_error_intel(struct hci_dev *hdev, u8 code)
2675{
2676 struct sk_buff *skb;
2677 u8 type = 0x00;
2678
2679 BT_ERR("%s: Hardware error 0x%2.2x", hdev->name, code);
2680
2681 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
2682 if (IS_ERR(skb)) {
2683 BT_ERR("%s: Reset after hardware error failed (%ld)",
2684 hdev->name, PTR_ERR(skb));
2685 return;
2686 }
2687 kfree_skb(skb);
2688
2689 skb = __hci_cmd_sync(hdev, 0xfc22, 1, &type, HCI_INIT_TIMEOUT);
2690 if (IS_ERR(skb)) {
2691 BT_ERR("%s: Retrieving Intel exception info failed (%ld)",
2692 hdev->name, PTR_ERR(skb));
2693 return;
2694 }
2695
2696 if (skb->len != 13) {
2697 BT_ERR("%s: Exception info size mismatch", hdev->name);
2698 kfree_skb(skb);
2699 return;
2700 }
2701
2702 if (skb->data[0] != 0x00) {
2703 BT_ERR("%s: Exception info command failure (%02x)",
2704 hdev->name, skb->data[0]);
2705 kfree_skb(skb);
2706 return;
2707 }
2708
2709 BT_ERR("%s: Exception info %s", hdev->name, (char *)(skb->data + 1));
2710
2711 kfree_skb(skb);
2712}
2713
Tedd Ho-Jeong Anbfbd45e2015-02-13 09:20:52 -08002714static int btusb_shutdown_intel(struct hci_dev *hdev)
2715{
2716 struct sk_buff *skb;
2717 long ret;
2718
2719 /* Some platforms have an issue with BT LED when the interface is
2720 * down or BT radio is turned off, which takes 5 seconds to BT LED
2721 * goes off. This command turns off the BT LED immediately.
2722 */
2723 skb = __hci_cmd_sync(hdev, 0xfc3f, 0, NULL, HCI_INIT_TIMEOUT);
2724 if (IS_ERR(skb)) {
2725 ret = PTR_ERR(skb);
2726 BT_ERR("%s: turning off Intel device LED failed (%ld)",
2727 hdev->name, ret);
2728 return ret;
2729 }
2730 kfree_skb(skb);
2731
2732 return 0;
2733}
2734
Amitkumar Karwarae8df492014-07-18 14:47:06 -07002735static int btusb_set_bdaddr_marvell(struct hci_dev *hdev,
2736 const bdaddr_t *bdaddr)
2737{
2738 struct sk_buff *skb;
2739 u8 buf[8];
2740 long ret;
2741
2742 buf[0] = 0xfe;
2743 buf[1] = sizeof(bdaddr_t);
2744 memcpy(buf + 2, bdaddr, sizeof(bdaddr_t));
2745
2746 skb = __hci_cmd_sync(hdev, 0xfc22, sizeof(buf), buf, HCI_INIT_TIMEOUT);
2747 if (IS_ERR(skb)) {
2748 ret = PTR_ERR(skb);
2749 BT_ERR("%s: changing Marvell device address failed (%ld)",
2750 hdev->name, ret);
2751 return ret;
2752 }
2753 kfree_skb(skb);
2754
2755 return 0;
2756}
2757
Toshi Kikuchi58592232014-12-12 10:58:05 -08002758static int btusb_set_bdaddr_ath3012(struct hci_dev *hdev,
2759 const bdaddr_t *bdaddr)
2760{
2761 struct sk_buff *skb;
2762 u8 buf[10];
2763 long ret;
2764
2765 buf[0] = 0x01;
2766 buf[1] = 0x01;
2767 buf[2] = 0x00;
2768 buf[3] = sizeof(bdaddr_t);
2769 memcpy(buf + 4, bdaddr, sizeof(bdaddr_t));
2770
2771 skb = __hci_cmd_sync(hdev, 0xfc0b, sizeof(buf), buf, HCI_INIT_TIMEOUT);
2772 if (IS_ERR(skb)) {
2773 ret = PTR_ERR(skb);
2774 BT_ERR("%s: Change address command failed (%ld)",
2775 hdev->name, ret);
2776 return ret;
2777 }
2778 kfree_skb(skb);
2779
2780 return 0;
2781}
2782
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002783#define QCA_DFU_PACKET_LEN 4096
2784
2785#define QCA_GET_TARGET_VERSION 0x09
2786#define QCA_CHECK_STATUS 0x05
2787#define QCA_DFU_DOWNLOAD 0x01
2788
2789#define QCA_SYSCFG_UPDATED 0x40
2790#define QCA_PATCH_UPDATED 0x80
2791#define QCA_DFU_TIMEOUT 3000
2792
2793struct qca_version {
2794 __le32 rom_version;
2795 __le32 patch_version;
2796 __le32 ram_version;
2797 __le32 ref_clock;
2798 __u8 reserved[4];
2799} __packed;
2800
2801struct qca_rampatch_version {
2802 __le16 rom_version;
2803 __le16 patch_version;
2804} __packed;
2805
2806struct qca_device_info {
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002807 u32 rom_version;
2808 u8 rampatch_hdr; /* length of header in rampatch */
2809 u8 nvm_hdr; /* length of header in NVM */
2810 u8 ver_offset; /* offset of version structure in rampatch */
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002811};
2812
2813static const struct qca_device_info qca_devices_table[] = {
2814 { 0x00000100, 20, 4, 10 }, /* Rome 1.0 */
2815 { 0x00000101, 20, 4, 10 }, /* Rome 1.1 */
2816 { 0x00000201, 28, 4, 18 }, /* Rome 2.1 */
2817 { 0x00000300, 28, 4, 18 }, /* Rome 3.0 */
2818 { 0x00000302, 28, 4, 18 }, /* Rome 3.2 */
2819};
2820
2821static int btusb_qca_send_vendor_req(struct hci_dev *hdev, u8 request,
2822 void *data, u16 size)
2823{
2824 struct btusb_data *btdata = hci_get_drvdata(hdev);
2825 struct usb_device *udev = btdata->udev;
2826 int pipe, err;
2827 u8 *buf;
2828
2829 buf = kmalloc(size, GFP_KERNEL);
2830 if (!buf)
2831 return -ENOMEM;
2832
2833 /* Found some of USB hosts have IOT issues with ours so that we should
2834 * not wait until HCI layer is ready.
2835 */
2836 pipe = usb_rcvctrlpipe(udev, 0);
2837 err = usb_control_msg(udev, pipe, request, USB_TYPE_VENDOR | USB_DIR_IN,
2838 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
2839 if (err < 0) {
2840 BT_ERR("%s: Failed to access otp area (%d)", hdev->name, err);
2841 goto done;
2842 }
2843
2844 memcpy(data, buf, size);
2845
2846done:
2847 kfree(buf);
2848
2849 return err;
2850}
2851
2852static int btusb_setup_qca_download_fw(struct hci_dev *hdev,
2853 const struct firmware *firmware,
2854 size_t hdr_size)
2855{
2856 struct btusb_data *btdata = hci_get_drvdata(hdev);
2857 struct usb_device *udev = btdata->udev;
2858 size_t count, size, sent = 0;
2859 int pipe, len, err;
2860 u8 *buf;
2861
2862 buf = kmalloc(QCA_DFU_PACKET_LEN, GFP_KERNEL);
2863 if (!buf)
2864 return -ENOMEM;
2865
2866 count = firmware->size;
2867
2868 size = min_t(size_t, count, hdr_size);
2869 memcpy(buf, firmware->data, size);
2870
2871 /* USB patches should go down to controller through USB path
2872 * because binary format fits to go down through USB channel.
2873 * USB control path is for patching headers and USB bulk is for
2874 * patch body.
2875 */
2876 pipe = usb_sndctrlpipe(udev, 0);
2877 err = usb_control_msg(udev, pipe, QCA_DFU_DOWNLOAD, USB_TYPE_VENDOR,
2878 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
2879 if (err < 0) {
2880 BT_ERR("%s: Failed to send headers (%d)", hdev->name, err);
2881 goto done;
2882 }
2883
2884 sent += size;
2885 count -= size;
2886
2887 while (count) {
2888 size = min_t(size_t, count, QCA_DFU_PACKET_LEN);
2889
2890 memcpy(buf, firmware->data + sent, size);
2891
2892 pipe = usb_sndbulkpipe(udev, 0x02);
2893 err = usb_bulk_msg(udev, pipe, buf, size, &len,
2894 QCA_DFU_TIMEOUT);
2895 if (err < 0) {
2896 BT_ERR("%s: Failed to send body at %zd of %zd (%d)",
2897 hdev->name, sent, firmware->size, err);
2898 break;
2899 }
2900
2901 if (size != len) {
2902 BT_ERR("%s: Failed to get bulk buffer", hdev->name);
2903 err = -EILSEQ;
2904 break;
2905 }
2906
2907 sent += size;
2908 count -= size;
2909 }
2910
2911done:
2912 kfree(buf);
2913 return err;
2914}
2915
2916static int btusb_setup_qca_load_rampatch(struct hci_dev *hdev,
2917 struct qca_version *ver,
2918 const struct qca_device_info *info)
2919{
2920 struct qca_rampatch_version *rver;
2921 const struct firmware *fw;
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002922 u32 ver_rom, ver_patch;
2923 u16 rver_rom, rver_patch;
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002924 char fwname[64];
2925 int err;
2926
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002927 ver_rom = le32_to_cpu(ver->rom_version);
2928 ver_patch = le32_to_cpu(ver->patch_version);
2929
2930 snprintf(fwname, sizeof(fwname), "qca/rampatch_usb_%08x.bin", ver_rom);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002931
2932 err = request_firmware(&fw, fwname, &hdev->dev);
2933 if (err) {
2934 BT_ERR("%s: failed to request rampatch file: %s (%d)",
2935 hdev->name, fwname, err);
2936 return err;
2937 }
2938
2939 BT_INFO("%s: using rampatch file: %s", hdev->name, fwname);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002940
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002941 rver = (struct qca_rampatch_version *)(fw->data + info->ver_offset);
2942 rver_rom = le16_to_cpu(rver->rom_version);
2943 rver_patch = le16_to_cpu(rver->patch_version);
2944
2945 BT_INFO("%s: QCA: patch rome 0x%x build 0x%x, firmware rome 0x%x "
2946 "build 0x%x", hdev->name, rver_rom, rver_patch, ver_rom,
2947 ver_patch);
2948
2949 if (rver_rom != ver_rom || rver_patch <= ver_patch) {
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002950 BT_ERR("%s: rampatch file version did not match with firmware",
2951 hdev->name);
2952 err = -EINVAL;
2953 goto done;
2954 }
2955
2956 err = btusb_setup_qca_download_fw(hdev, fw, info->rampatch_hdr);
2957
2958done:
2959 release_firmware(fw);
2960
2961 return err;
2962}
2963
2964static int btusb_setup_qca_load_nvm(struct hci_dev *hdev,
2965 struct qca_version *ver,
2966 const struct qca_device_info *info)
2967{
2968 const struct firmware *fw;
2969 char fwname[64];
2970 int err;
2971
2972 snprintf(fwname, sizeof(fwname), "qca/nvm_usb_%08x.bin",
2973 le32_to_cpu(ver->rom_version));
2974
2975 err = request_firmware(&fw, fwname, &hdev->dev);
2976 if (err) {
2977 BT_ERR("%s: failed to request NVM file: %s (%d)",
2978 hdev->name, fwname, err);
2979 return err;
2980 }
2981
2982 BT_INFO("%s: using NVM file: %s", hdev->name, fwname);
2983
2984 err = btusb_setup_qca_download_fw(hdev, fw, info->nvm_hdr);
2985
2986 release_firmware(fw);
2987
2988 return err;
2989}
2990
2991static int btusb_setup_qca(struct hci_dev *hdev)
2992{
2993 const struct qca_device_info *info = NULL;
2994 struct qca_version ver;
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002995 u32 ver_rom;
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002996 u8 status;
2997 int i, err;
2998
2999 err = btusb_qca_send_vendor_req(hdev, QCA_GET_TARGET_VERSION, &ver,
Marcel Holtmanneb500422015-04-16 23:15:50 +02003000 sizeof(ver));
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003001 if (err < 0)
3002 return err;
3003
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00003004 ver_rom = le32_to_cpu(ver.rom_version);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003005 for (i = 0; i < ARRAY_SIZE(qca_devices_table); i++) {
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00003006 if (ver_rom == qca_devices_table[i].rom_version)
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003007 info = &qca_devices_table[i];
3008 }
3009 if (!info) {
3010 BT_ERR("%s: don't support firmware rome 0x%x", hdev->name,
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00003011 ver_rom);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003012 return -ENODEV;
3013 }
3014
3015 err = btusb_qca_send_vendor_req(hdev, QCA_CHECK_STATUS, &status,
3016 sizeof(status));
3017 if (err < 0)
3018 return err;
3019
3020 if (!(status & QCA_PATCH_UPDATED)) {
3021 err = btusb_setup_qca_load_rampatch(hdev, &ver, info);
3022 if (err < 0)
3023 return err;
3024 }
3025
3026 if (!(status & QCA_SYSCFG_UPDATED)) {
3027 err = btusb_setup_qca_load_nvm(hdev, &ver, info);
3028 if (err < 0)
3029 return err;
3030 }
3031
3032 return 0;
3033}
3034
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003035static int btusb_probe(struct usb_interface *intf,
Marcel Holtmann89e75332014-09-16 04:44:50 +02003036 const struct usb_device_id *id)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003037{
3038 struct usb_endpoint_descriptor *ep_desc;
3039 struct btusb_data *data;
3040 struct hci_dev *hdev;
3041 int i, err;
3042
3043 BT_DBG("intf %p id %p", intf, id);
3044
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02003045 /* interface numbers are hardcoded in the spec */
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003046 if (intf->cur_altsetting->desc.bInterfaceNumber != 0)
3047 return -ENODEV;
3048
3049 if (!id->driver_info) {
3050 const struct usb_device_id *match;
Marcel Holtmann89e75332014-09-16 04:44:50 +02003051
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003052 match = usb_match_id(intf, blacklist_table);
3053 if (match)
3054 id = match;
3055 }
3056
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02003057 if (id->driver_info == BTUSB_IGNORE)
3058 return -ENODEV;
3059
Steven.Li2d25f8b2011-07-01 14:02:36 +08003060 if (id->driver_info & BTUSB_ATH3012) {
3061 struct usb_device *udev = interface_to_usbdev(intf);
3062
3063 /* Old firmware would otherwise let ath3k driver load
3064 * patch and sysconfig files */
3065 if (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x0001)
3066 return -ENODEV;
3067 }
3068
Sachin Kamat98921db2012-07-27 12:38:39 +05303069 data = devm_kzalloc(&intf->dev, sizeof(*data), GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003070 if (!data)
3071 return -ENOMEM;
3072
3073 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
3074 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
3075
3076 if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
3077 data->intr_ep = ep_desc;
3078 continue;
3079 }
3080
3081 if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
3082 data->bulk_tx_ep = ep_desc;
3083 continue;
3084 }
3085
3086 if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
3087 data->bulk_rx_ep = ep_desc;
3088 continue;
3089 }
3090 }
3091
Sachin Kamat98921db2012-07-27 12:38:39 +05303092 if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003093 return -ENODEV;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003094
Marcel Holtmann893ba542015-01-28 20:27:34 -08003095 if (id->driver_info & BTUSB_AMP) {
3096 data->cmdreq_type = USB_TYPE_CLASS | 0x01;
3097 data->cmdreq = 0x2b;
3098 } else {
3099 data->cmdreq_type = USB_TYPE_CLASS;
3100 data->cmdreq = 0x00;
3101 }
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01003102
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003103 data->udev = interface_to_usbdev(intf);
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02003104 data->intf = intf;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003105
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003106 INIT_WORK(&data->work, btusb_work);
Oliver Neukum7bee5492009-08-24 23:44:59 +02003107 INIT_WORK(&data->waker, btusb_waker);
Marcel Holtmann803b5832014-09-16 08:00:29 +02003108 init_usb_anchor(&data->deferred);
3109 init_usb_anchor(&data->tx_anchor);
Oliver Neukum7bee5492009-08-24 23:44:59 +02003110 spin_lock_init(&data->txlock);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003111
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003112 init_usb_anchor(&data->intr_anchor);
3113 init_usb_anchor(&data->bulk_anchor);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02003114 init_usb_anchor(&data->isoc_anchor);
Marcel Holtmann803b5832014-09-16 08:00:29 +02003115 spin_lock_init(&data->rxlock);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003116
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08003117 if (id->driver_info & BTUSB_INTEL_NEW) {
3118 data->recv_event = btusb_recv_event_intel;
3119 data->recv_bulk = btusb_recv_bulk_intel;
3120 set_bit(BTUSB_BOOTLOADER, &data->flags);
3121 } else {
3122 data->recv_event = hci_recv_frame;
3123 data->recv_bulk = btusb_recv_bulk;
3124 }
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +01003125
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003126 hdev = hci_alloc_dev();
Sachin Kamat98921db2012-07-27 12:38:39 +05303127 if (!hdev)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003128 return -ENOMEM;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003129
Marcel Holtmannc13854c2010-02-08 15:27:07 +01003130 hdev->bus = HCI_USB;
David Herrmann155961e2012-02-09 21:58:32 +01003131 hci_set_drvdata(hdev, data);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003132
Marcel Holtmann893ba542015-01-28 20:27:34 -08003133 if (id->driver_info & BTUSB_AMP)
3134 hdev->dev_type = HCI_AMP;
3135 else
3136 hdev->dev_type = HCI_BREDR;
3137
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003138 data->hdev = hdev;
3139
3140 SET_HCIDEV_DEV(hdev, &intf->dev);
3141
Marcel Holtmann9f8f9622013-04-10 08:11:35 -07003142 hdev->open = btusb_open;
3143 hdev->close = btusb_close;
3144 hdev->flush = btusb_flush;
3145 hdev->send = btusb_send_frame;
3146 hdev->notify = btusb_notify;
3147
3148 if (id->driver_info & BTUSB_BCM92035)
3149 hdev->setup = btusb_setup_bcm92035;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003150
Marcel Holtmannc2bfb102015-04-05 22:52:14 -07003151#ifdef CONFIG_BT_HCIBTUSB_BCM
Marcel Holtmannabbaf502014-07-02 00:53:48 +02003152 if (id->driver_info & BTUSB_BCM_PATCHRAM) {
Marcel Holtmannc2bfb102015-04-05 22:52:14 -07003153 hdev->setup = btbcm_setup_patchram;
Marcel Holtmann1df1f592015-04-05 22:52:11 -07003154 hdev->set_bdaddr = btbcm_set_bdaddr;
Marcel Holtmannabbaf502014-07-02 00:53:48 +02003155 }
Petri Gynther10d4c672014-05-08 15:50:01 -07003156
Marcel Holtmann941521e2015-04-05 22:52:15 -07003157 if (id->driver_info & BTUSB_BCM_APPLE)
Marcel Holtmannc2bfb102015-04-05 22:52:14 -07003158 hdev->setup = btbcm_setup_apple;
Marcel Holtmannc2bfb102015-04-05 22:52:14 -07003159#endif
Marcel Holtmann17b27722015-03-22 15:52:38 +01003160
Marcel Holtmanncb8d6592014-07-02 11:25:25 +02003161 if (id->driver_info & BTUSB_INTEL) {
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07003162 hdev->setup = btusb_setup_intel;
Tedd Ho-Jeong Anbfbd45e2015-02-13 09:20:52 -08003163 hdev->shutdown = btusb_shutdown_intel;
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07003164 hdev->set_bdaddr = btintel_set_bdaddr;
Jakub Pawlowskic33fb9b2015-01-30 18:55:58 -08003165 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
Jakub Pawlowskic1154842015-03-17 09:04:16 -07003166 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
Marcel Holtmanncb8d6592014-07-02 11:25:25 +02003167 }
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07003168
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08003169 if (id->driver_info & BTUSB_INTEL_NEW) {
3170 hdev->send = btusb_send_frame_intel;
3171 hdev->setup = btusb_setup_intel_new;
Marcel Holtmann385a7682015-01-28 11:09:56 -08003172 hdev->hw_error = btusb_hw_error_intel;
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07003173 hdev->set_bdaddr = btintel_set_bdaddr;
Marcel Holtmannb970c5b2015-02-01 23:57:18 -08003174 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08003175 }
3176
Amitkumar Karwarae8df492014-07-18 14:47:06 -07003177 if (id->driver_info & BTUSB_MARVELL)
3178 hdev->set_bdaddr = btusb_set_bdaddr_marvell;
3179
Marcel Holtmann661cf882015-01-02 23:35:20 -08003180 if (id->driver_info & BTUSB_SWAVE) {
3181 set_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks);
Marcel Holtmannd57dbe72014-12-26 04:42:33 +01003182 set_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS, &hdev->quirks);
Marcel Holtmann661cf882015-01-02 23:35:20 -08003183 }
Marcel Holtmannd57dbe72014-12-26 04:42:33 +01003184
Marcel Holtmann40df7832014-07-06 13:29:58 +02003185 if (id->driver_info & BTUSB_INTEL_BOOT)
3186 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
3187
Jakub Pawlowski79f0c872015-01-29 10:38:34 -08003188 if (id->driver_info & BTUSB_ATH3012) {
Toshi Kikuchi58592232014-12-12 10:58:05 -08003189 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
Jakub Pawlowski3d50d512015-03-17 09:04:15 -07003190 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
Jakub Pawlowski79f0c872015-01-29 10:38:34 -08003191 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
3192 }
Toshi Kikuchi58592232014-12-12 10:58:05 -08003193
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003194 if (id->driver_info & BTUSB_QCA_ROME) {
3195 data->setup_on_usb = btusb_setup_qca;
3196 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
3197 }
3198
Daniel Drakea2698a92015-04-16 14:09:55 -06003199 if (id->driver_info & BTUSB_REALTEK)
3200 hdev->setup = btusb_setup_realtek;
3201
Marcel Holtmann893ba542015-01-28 20:27:34 -08003202 if (id->driver_info & BTUSB_AMP) {
3203 /* AMP controllers do not support SCO packets */
3204 data->isoc = NULL;
3205 } else {
3206 /* Interface numbers are hardcoded in the specification */
3207 data->isoc = usb_ifnum_to_if(data->udev, 1);
3208 }
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02003209
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01003210 if (!reset)
Szymon Janca6c511c2012-05-23 12:35:46 +02003211 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02003212
3213 if (force_scofix || id->driver_info & BTUSB_WRONG_SCO_MTU) {
3214 if (!disable_scofix)
3215 set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
3216 }
3217
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02003218 if (id->driver_info & BTUSB_BROKEN_ISOC)
3219 data->isoc = NULL;
3220
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01003221 if (id->driver_info & BTUSB_DIGIANSWER) {
3222 data->cmdreq_type = USB_TYPE_VENDOR;
Szymon Janca6c511c2012-05-23 12:35:46 +02003223 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01003224 }
3225
3226 if (id->driver_info & BTUSB_CSR) {
3227 struct usb_device *udev = data->udev;
Marcel Holtmann81cac642014-01-03 03:02:36 -08003228 u16 bcdDevice = le16_to_cpu(udev->descriptor.bcdDevice);
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01003229
3230 /* Old firmware would otherwise execute USB reset */
Marcel Holtmann81cac642014-01-03 03:02:36 -08003231 if (bcdDevice < 0x117)
Szymon Janca6c511c2012-05-23 12:35:46 +02003232 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmann81cac642014-01-03 03:02:36 -08003233
3234 /* Fake CSR devices with broken commands */
3235 if (bcdDevice <= 0x100)
3236 hdev->setup = btusb_setup_csr;
Jakub Pawlowski49c989a2015-03-17 09:04:17 -07003237
3238 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01003239 }
3240
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02003241 if (id->driver_info & BTUSB_SNIFFER) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02003242 struct usb_device *udev = data->udev;
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02003243
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01003244 /* New sniffer firmware has crippled HCI interface */
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02003245 if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
3246 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
3247 }
3248
Marcel Holtmann3a5ef202014-07-06 14:53:54 +02003249 if (id->driver_info & BTUSB_INTEL_BOOT) {
3250 /* A bug in the bootloader causes that interrupt interface is
3251 * only enabled after receiving SetInterface(0, AltSetting=0).
3252 */
3253 err = usb_set_interface(data->udev, 0, 0);
3254 if (err < 0) {
3255 BT_ERR("failed to set interface 0, alt 0 %d", err);
3256 hci_free_dev(hdev);
3257 return err;
3258 }
3259 }
3260
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02003261 if (data->isoc) {
3262 err = usb_driver_claim_interface(&btusb_driver,
Marcel Holtmann89e75332014-09-16 04:44:50 +02003263 data->isoc, data);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02003264 if (err < 0) {
3265 hci_free_dev(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02003266 return err;
3267 }
3268 }
3269
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003270 err = hci_register_dev(hdev);
3271 if (err < 0) {
3272 hci_free_dev(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003273 return err;
3274 }
3275
3276 usb_set_intfdata(intf, data);
3277
3278 return 0;
3279}
3280
3281static void btusb_disconnect(struct usb_interface *intf)
3282{
3283 struct btusb_data *data = usb_get_intfdata(intf);
3284 struct hci_dev *hdev;
3285
3286 BT_DBG("intf %p", intf);
3287
3288 if (!data)
3289 return;
3290
3291 hdev = data->hdev;
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02003292 usb_set_intfdata(data->intf, NULL);
3293
3294 if (data->isoc)
3295 usb_set_intfdata(data->isoc, NULL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003296
3297 hci_unregister_dev(hdev);
3298
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02003299 if (intf == data->isoc)
3300 usb_driver_release_interface(&btusb_driver, data->intf);
3301 else if (data->isoc)
3302 usb_driver_release_interface(&btusb_driver, data->isoc);
3303
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003304 hci_free_dev(hdev);
3305}
3306
Oliver Neukum7bee5492009-08-24 23:44:59 +02003307#ifdef CONFIG_PM
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003308static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
3309{
3310 struct btusb_data *data = usb_get_intfdata(intf);
3311
3312 BT_DBG("intf %p", intf);
3313
3314 if (data->suspend_count++)
3315 return 0;
3316
Oliver Neukum7bee5492009-08-24 23:44:59 +02003317 spin_lock_irq(&data->txlock);
Alan Stern5b1b0b82011-08-19 23:49:48 +02003318 if (!(PMSG_IS_AUTO(message) && data->tx_in_flight)) {
Oliver Neukum7bee5492009-08-24 23:44:59 +02003319 set_bit(BTUSB_SUSPENDING, &data->flags);
3320 spin_unlock_irq(&data->txlock);
3321 } else {
3322 spin_unlock_irq(&data->txlock);
3323 data->suspend_count--;
3324 return -EBUSY;
3325 }
3326
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003327 cancel_work_sync(&data->work);
3328
Oliver Neukum7bee5492009-08-24 23:44:59 +02003329 btusb_stop_traffic(data);
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003330 usb_kill_anchored_urbs(&data->tx_anchor);
3331
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003332 return 0;
3333}
3334
Oliver Neukum7bee5492009-08-24 23:44:59 +02003335static void play_deferred(struct btusb_data *data)
3336{
3337 struct urb *urb;
3338 int err;
3339
3340 while ((urb = usb_get_from_anchor(&data->deferred))) {
3341 err = usb_submit_urb(urb, GFP_ATOMIC);
3342 if (err < 0)
3343 break;
3344
3345 data->tx_in_flight++;
3346 }
3347 usb_scuttle_anchored_urbs(&data->deferred);
3348}
3349
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003350static int btusb_resume(struct usb_interface *intf)
3351{
3352 struct btusb_data *data = usb_get_intfdata(intf);
3353 struct hci_dev *hdev = data->hdev;
Oliver Neukum7bee5492009-08-24 23:44:59 +02003354 int err = 0;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003355
3356 BT_DBG("intf %p", intf);
3357
3358 if (--data->suspend_count)
3359 return 0;
3360
3361 if (!test_bit(HCI_RUNNING, &hdev->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +02003362 goto done;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003363
3364 if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
3365 err = btusb_submit_intr_urb(hdev, GFP_NOIO);
3366 if (err < 0) {
3367 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02003368 goto failed;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003369 }
3370 }
3371
3372 if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) {
Marcel Holtmann43c2e572009-02-04 17:41:38 +01003373 err = btusb_submit_bulk_urb(hdev, GFP_NOIO);
3374 if (err < 0) {
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003375 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02003376 goto failed;
3377 }
3378
3379 btusb_submit_bulk_urb(hdev, GFP_NOIO);
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003380 }
3381
3382 if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
3383 if (btusb_submit_isoc_urb(hdev, GFP_NOIO) < 0)
3384 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
3385 else
3386 btusb_submit_isoc_urb(hdev, GFP_NOIO);
3387 }
3388
Oliver Neukum7bee5492009-08-24 23:44:59 +02003389 spin_lock_irq(&data->txlock);
3390 play_deferred(data);
3391 clear_bit(BTUSB_SUSPENDING, &data->flags);
3392 spin_unlock_irq(&data->txlock);
3393 schedule_work(&data->work);
3394
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003395 return 0;
Oliver Neukum7bee5492009-08-24 23:44:59 +02003396
3397failed:
3398 usb_scuttle_anchored_urbs(&data->deferred);
3399done:
3400 spin_lock_irq(&data->txlock);
3401 clear_bit(BTUSB_SUSPENDING, &data->flags);
3402 spin_unlock_irq(&data->txlock);
3403
3404 return err;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003405}
Oliver Neukum7bee5492009-08-24 23:44:59 +02003406#endif
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003407
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003408static struct usb_driver btusb_driver = {
3409 .name = "btusb",
3410 .probe = btusb_probe,
3411 .disconnect = btusb_disconnect,
Oliver Neukum7bee5492009-08-24 23:44:59 +02003412#ifdef CONFIG_PM
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003413 .suspend = btusb_suspend,
3414 .resume = btusb_resume,
Oliver Neukum7bee5492009-08-24 23:44:59 +02003415#endif
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003416 .id_table = btusb_table,
Oliver Neukum7bee5492009-08-24 23:44:59 +02003417 .supports_autosuspend = 1,
Sarah Sharpe1f12eb2012-04-23 10:08:51 -07003418 .disable_hub_initiated_lpm = 1,
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003419};
3420
Greg Kroah-Hartman93f15082011-11-18 09:47:34 -08003421module_usb_driver(btusb_driver);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003422
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02003423module_param(disable_scofix, bool, 0644);
3424MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
3425
3426module_param(force_scofix, bool, 0644);
3427MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
3428
3429module_param(reset, bool, 0644);
3430MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
3431
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003432MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
3433MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
3434MODULE_VERSION(VERSION);
3435MODULE_LICENSE("GPL");