blob: 2f633df9f4e6d7bf681a9281c3911669f49abbc0 [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"
Carlo Caionedb33c772015-05-14 10:49:09 +020034#include "btrtl.h"
Marcel Holtmann1df1f592015-04-05 22:52:11 -070035
Marcel Holtmann34dced9b2015-04-05 22:52:16 -070036#define VERSION "0.8"
Marcel Holtmanncfeb4142008-08-07 22:26:56 +020037
Rusty Russell90ab5ee2012-01-13 09:32:20 +103038static bool disable_scofix;
39static bool force_scofix;
Marcel Holtmann7a9d4022008-11-30 12:17:26 +010040
Shailendra Verma917a3332015-05-25 23:53:40 +053041static bool reset = true;
Marcel Holtmanncfeb4142008-08-07 22:26:56 +020042
43static struct usb_driver btusb_driver;
44
45#define BTUSB_IGNORE 0x01
Marcel Holtmann7a9d4022008-11-30 12:17:26 +010046#define BTUSB_DIGIANSWER 0x02
47#define BTUSB_CSR 0x04
48#define BTUSB_SNIFFER 0x08
49#define BTUSB_BCM92035 0x10
50#define BTUSB_BROKEN_ISOC 0x20
51#define BTUSB_WRONG_SCO_MTU 0x40
Steven.Li2d25f8b2011-07-01 14:02:36 +080052#define BTUSB_ATH3012 0x80
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -070053#define BTUSB_INTEL 0x100
Marcel Holtmann40df7832014-07-06 13:29:58 +020054#define BTUSB_INTEL_BOOT 0x200
55#define BTUSB_BCM_PATCHRAM 0x400
Amitkumar Karwarae8df492014-07-18 14:47:06 -070056#define BTUSB_MARVELL 0x800
Marcel Holtmann4fcef8e2015-01-01 17:34:37 -080057#define BTUSB_SWAVE 0x1000
Marcel Holtmanncda0dd72015-01-26 21:33:48 -080058#define BTUSB_INTEL_NEW 0x2000
Marcel Holtmann893ba542015-01-28 20:27:34 -080059#define BTUSB_AMP 0x4000
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +000060#define BTUSB_QCA_ROME 0x8000
Marcel Holtmann17b27722015-03-22 15:52:38 +010061#define BTUSB_BCM_APPLE 0x10000
Daniel Drakea2698a92015-04-16 14:09:55 -060062#define BTUSB_REALTEK 0x20000
Marcel Holtmann6c9d4352015-10-17 14:39:27 +020063#define BTUSB_BCM2045 0x40000
Marcel Holtmann22f8e9d2015-10-20 00:53:33 +020064#define BTUSB_IFNUM_2 0x80000
Szymon Janc418678b2016-09-01 17:22:37 +020065#define BTUSB_CW6622 0x100000
Marcel Holtmann5e23b922007-10-20 14:12:34 +020066
Marcel Holtmann54265202013-10-11 07:46:18 -070067static const struct usb_device_id btusb_table[] = {
Marcel Holtmann5e23b922007-10-20 14:12:34 +020068 /* Generic Bluetooth USB device */
69 { USB_DEVICE_INFO(0xe0, 0x01, 0x01) },
70
Marcel Holtmann893ba542015-01-28 20:27:34 -080071 /* Generic Bluetooth AMP device */
72 { USB_DEVICE_INFO(0xe0, 0x01, 0x04), .driver_info = BTUSB_AMP },
73
Daniel Draked63b2822015-07-17 11:12:25 -060074 /* Generic Bluetooth USB interface */
75 { USB_INTERFACE_INFO(0xe0, 0x01, 0x01) },
76
Henrik Rydberg1fa65352012-08-25 19:28:06 +020077 /* Apple-specific (Broadcom) devices */
Marcel Holtmann17b27722015-03-22 15:52:38 +010078 { USB_VENDOR_AND_INTERFACE_INFO(0x05ac, 0xff, 0x01, 0x01),
Marcel Holtmann22f8e9d2015-10-20 00:53:33 +020079 .driver_info = BTUSB_BCM_APPLE | BTUSB_IFNUM_2 },
Henrik Rydberg1fa65352012-08-25 19:28:06 +020080
Cho, Yu-Chen178c0592013-06-04 21:40:26 +080081 /* MediaTek MT76x0E */
82 { USB_DEVICE(0x0e8d, 0x763f) },
83
Oliver Neukumc510eae2011-09-21 11:41:45 +020084 /* Broadcom SoftSailing reporting vendor specific */
Don Zickus2e8b50632012-03-28 16:41:11 -040085 { USB_DEVICE(0x0a5c, 0x21e1) },
Oliver Neukumc510eae2011-09-21 11:41:45 +020086
Nobuhiro Iwamatsu3cd01972010-08-20 16:24:07 +090087 /* Apple MacBookPro 7,1 */
88 { USB_DEVICE(0x05ac, 0x8213) },
89
Cyril Lacoux0a79f672010-07-14 10:29:27 +040090 /* Apple iMac11,1 */
91 { USB_DEVICE(0x05ac, 0x8215) },
92
Nobuhiro Iwamatsu9c047152010-08-20 16:24:06 +090093 /* Apple MacBookPro6,2 */
94 { USB_DEVICE(0x05ac, 0x8218) },
95
Edgar (gimli) Hucek3e3ede72010-11-04 08:04:33 +010096 /* Apple MacBookAir3,1, MacBookAir3,2 */
97 { USB_DEVICE(0x05ac, 0x821b) },
98
Pieter-Augustijn Van Malleghema63b7232011-09-07 02:28:10 -040099 /* Apple MacBookAir4,1 */
100 { USB_DEVICE(0x05ac, 0x821f) },
101
Marc-Antoine Perennou88d377b2011-03-24 14:51:21 -0300102 /* Apple MacBookPro8,2 */
103 { USB_DEVICE(0x05ac, 0x821a) },
104
Jurgen Kramerf78b68262011-09-04 18:01:42 +0200105 /* Apple MacMini5,1 */
106 { USB_DEVICE(0x05ac, 0x8281) },
107
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200108 /* AVM BlueFRITZ! USB v2.0 */
Marcel Holtmann4fcef8e2015-01-01 17:34:37 -0800109 { USB_DEVICE(0x057c, 0x3800), .driver_info = BTUSB_SWAVE },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200110
111 /* Bluetooth Ultraport Module from IBM */
112 { USB_DEVICE(0x04bf, 0x030a) },
113
114 /* ALPS Modules with non-standard id */
115 { USB_DEVICE(0x044e, 0x3001) },
116 { USB_DEVICE(0x044e, 0x3002) },
117
118 /* Ericsson with non-standard id */
119 { USB_DEVICE(0x0bdb, 0x1002) },
120
121 /* Canyon CN-BTU1 with HID interfaces */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100122 { USB_DEVICE(0x0c10, 0x0000) },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200123
Wen-chien Jesse Sungd13431c2011-11-08 14:30:22 +0800124 /* Broadcom BCM20702A0 */
125 { USB_DEVICE(0x413c, 0x8197) },
126
Marcel Holtmannd049f4e2015-01-26 20:35:32 -0800127 /* Broadcom BCM20702B0 (Dynex/Insignia) */
128 { USB_DEVICE(0x19ff, 0x0239), .driver_info = BTUSB_BCM_PATCHRAM },
129
Santtu Rekilä2faf71c2015-10-05 15:45:27 +0300130 /* Broadcom BCM43142A0 (Foxconn/Lenovo) */
131 { USB_DEVICE(0x105b, 0xe065), .driver_info = BTUSB_BCM_PATCHRAM },
132
Steven Harms98514032012-04-13 14:45:55 -0400133 /* Foxconn - Hon Hai */
Heinrich Siebmanns6029ddc2014-12-03 19:32:22 +0100134 { USB_VENDOR_AND_INTERFACE_INFO(0x0489, 0xff, 0x01, 0x01),
135 .driver_info = BTUSB_BCM_PATCHRAM },
Steven Harms98514032012-04-13 14:45:55 -0400136
Matej Dubovy8f0c3042015-02-02 18:50:14 +0100137 /* Lite-On Technology - Broadcom based */
138 { USB_VENDOR_AND_INTERFACE_INFO(0x04ca, 0xff, 0x01, 0x01),
139 .driver_info = BTUSB_BCM_PATCHRAM },
140
Andy Shevchenko0b880062014-02-18 18:26:19 +0200141 /* Broadcom devices with vendor specific id */
Petri Gynther10d4c672014-05-08 15:50:01 -0700142 { USB_VENDOR_AND_INTERFACE_INFO(0x0a5c, 0xff, 0x01, 0x01),
143 .driver_info = BTUSB_BCM_PATCHRAM },
Gustavo Padovan92c385f2012-08-06 15:36:49 -0300144
Marcel Holtmannc2aef6e2014-07-21 14:02:33 +0200145 /* ASUSTek Computer - Broadcom based */
Rick Dunn9a5abda2015-01-17 05:29:12 +0100146 { USB_VENDOR_AND_INTERFACE_INFO(0x0b05, 0xff, 0x01, 0x01),
147 .driver_info = BTUSB_BCM_PATCHRAM },
Marcel Holtmannc2aef6e2014-07-21 14:02:33 +0200148
Ken O'Brien5bcecf32013-09-21 19:14:43 +0100149 /* Belkin F8065bf - Broadcom based */
Marcel Holtmann6331c682015-03-27 15:11:41 -0700150 { USB_VENDOR_AND_INTERFACE_INFO(0x050d, 0xff, 0x01, 0x01),
151 .driver_info = BTUSB_BCM_PATCHRAM },
Ken O'Brien5bcecf32013-09-21 19:14:43 +0100152
Jurgen Kramer9113bfd2014-02-15 12:01:09 +0100153 /* IMC Networks - Broadcom based */
Marcel Holtmann6331c682015-03-27 15:11:41 -0700154 { USB_VENDOR_AND_INTERFACE_INFO(0x13d3, 0xff, 0x01, 0x01),
155 .driver_info = BTUSB_BCM_PATCHRAM },
Jurgen Kramer9113bfd2014-02-15 12:01:09 +0100156
Dmitry Tunin1623d0b2015-12-05 14:09:36 +0300157 /* Toshiba Corp - Broadcom based */
158 { USB_VENDOR_AND_INTERFACE_INFO(0x0930, 0xff, 0x01, 0x01),
159 .driver_info = BTUSB_BCM_PATCHRAM },
160
Marcel Holtmann40df7832014-07-06 13:29:58 +0200161 /* Intel Bluetooth USB Bootloader (RAM module) */
Marcel Holtmannd92f2df2014-07-06 14:53:55 +0200162 { USB_DEVICE(0x8087, 0x0a5a),
163 .driver_info = BTUSB_INTEL_BOOT | BTUSB_BROKEN_ISOC },
Marcel Holtmann40df7832014-07-06 13:29:58 +0200164
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200165 { } /* Terminating entry */
166};
167
168MODULE_DEVICE_TABLE(usb, btusb_table);
169
Marcel Holtmann54265202013-10-11 07:46:18 -0700170static const struct usb_device_id blacklist_table[] = {
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200171 /* CSR BlueCore devices */
172 { USB_DEVICE(0x0a12, 0x0001), .driver_info = BTUSB_CSR },
173
174 /* Broadcom BCM2033 without firmware */
175 { USB_DEVICE(0x0a5c, 0x2033), .driver_info = BTUSB_IGNORE },
176
Marcel Holtmann6c9d4352015-10-17 14:39:27 +0200177 /* Broadcom BCM2045 devices */
178 { USB_DEVICE(0x0a5c, 0x2045), .driver_info = BTUSB_BCM2045 },
179
Bala Shanmugambe931122010-11-26 17:35:46 +0530180 /* Atheros 3011 with sflash firmware */
Andy Shevchenko0b880062014-02-18 18:26:19 +0200181 { USB_DEVICE(0x0489, 0xe027), .driver_info = BTUSB_IGNORE },
182 { USB_DEVICE(0x0489, 0xe03d), .driver_info = BTUSB_IGNORE },
Alexander Ploumistos2eeff0b2015-02-13 21:05:11 +0200183 { USB_DEVICE(0x04f2, 0xaff1), .driver_info = BTUSB_IGNORE },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200184 { USB_DEVICE(0x0930, 0x0215), .driver_info = BTUSB_IGNORE },
Bala Shanmugambe931122010-11-26 17:35:46 +0530185 { USB_DEVICE(0x0cf3, 0x3002), .driver_info = BTUSB_IGNORE },
Marek Vasut6eda5412012-06-08 14:32:50 +0200186 { USB_DEVICE(0x0cf3, 0xe019), .driver_info = BTUSB_IGNORE },
Andy Ross2a7bccc2011-05-09 16:11:16 -0700187 { USB_DEVICE(0x13d3, 0x3304), .driver_info = BTUSB_IGNORE },
Bala Shanmugambe931122010-11-26 17:35:46 +0530188
Cho, Yu-Chen509e7862011-01-26 17:10:59 +0800189 /* Atheros AR9285 Malbec with sflash firmware */
190 { USB_DEVICE(0x03f0, 0x311d), .driver_info = BTUSB_IGNORE },
191
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530192 /* Atheros 3012 with sflash firmware */
Andy Shevchenko0b880062014-02-18 18:26:19 +0200193 { USB_DEVICE(0x0489, 0xe04d), .driver_info = BTUSB_ATH3012 },
194 { USB_DEVICE(0x0489, 0xe04e), .driver_info = BTUSB_ATH3012 },
195 { USB_DEVICE(0x0489, 0xe056), .driver_info = BTUSB_ATH3012 },
196 { USB_DEVICE(0x0489, 0xe057), .driver_info = BTUSB_ATH3012 },
197 { USB_DEVICE(0x0489, 0xe05f), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin692c0622015-06-06 20:25:40 +0300198 { USB_DEVICE(0x0489, 0xe076), .driver_info = BTUSB_ATH3012 },
Anantha Krishnan4b552bc2014-10-06 16:31:49 +0530199 { USB_DEVICE(0x0489, 0xe078), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin28c971d2016-02-10 00:49:11 +0300200 { USB_DEVICE(0x0489, 0xe095), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200201 { USB_DEVICE(0x04c5, 0x1330), .driver_info = BTUSB_ATH3012 },
202 { USB_DEVICE(0x04ca, 0x3004), .driver_info = BTUSB_ATH3012 },
203 { USB_DEVICE(0x04ca, 0x3005), .driver_info = BTUSB_ATH3012 },
204 { USB_DEVICE(0x04ca, 0x3006), .driver_info = BTUSB_ATH3012 },
Mohammed Habibulla1fb4e092014-04-17 11:37:13 -0700205 { USB_DEVICE(0x04ca, 0x3007), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200206 { USB_DEVICE(0x04ca, 0x3008), .driver_info = BTUSB_ATH3012 },
207 { USB_DEVICE(0x04ca, 0x300b), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin7e730c72015-06-18 20:41:51 +0300208 { USB_DEVICE(0x04ca, 0x300d), .driver_info = BTUSB_ATH3012 },
Dmitry Tuninec0810d2015-05-02 13:36:58 +0300209 { USB_DEVICE(0x04ca, 0x300f), .driver_info = BTUSB_ATH3012 },
Janne Heikkinen134d3b32014-12-09 07:44:51 +0200210 { USB_DEVICE(0x04ca, 0x3010), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin81d90442016-02-28 11:04:06 +0300211 { USB_DEVICE(0x04ca, 0x3014), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200212 { USB_DEVICE(0x0930, 0x0219), .driver_info = BTUSB_ATH3012 },
Dmitry Tunincd355ff2015-10-05 19:29:33 +0300213 { USB_DEVICE(0x0930, 0x021c), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200214 { USB_DEVICE(0x0930, 0x0220), .driver_info = BTUSB_ATH3012 },
Vincent Zwanenburg89d29752014-08-08 12:33:56 +0100215 { USB_DEVICE(0x0930, 0x0227), .driver_info = BTUSB_ATH3012 },
Andy Shevchenkoa735f9e2014-02-18 18:26:20 +0200216 { USB_DEVICE(0x0b05, 0x17d0), .driver_info = BTUSB_ATH3012 },
Ming Leid66629c2013-03-15 11:00:39 +0800217 { USB_DEVICE(0x0cf3, 0x0036), .driver_info = BTUSB_ATH3012 },
Steven.Li2d25f8b2011-07-01 14:02:36 +0800218 { USB_DEVICE(0x0cf3, 0x3004), .driver_info = BTUSB_ATH3012 },
Sunguk Lee94a32d102013-03-12 04:41:58 +0900219 { USB_DEVICE(0x0cf3, 0x3008), .driver_info = BTUSB_ATH3012 },
Cho, Yu-Chen07c0ea82012-03-14 22:01:21 +0200220 { USB_DEVICE(0x0cf3, 0x311d), .driver_info = BTUSB_ATH3012 },
Oliver Neukumb1312372014-01-16 15:37:11 +0100221 { USB_DEVICE(0x0cf3, 0x311e), .driver_info = BTUSB_ATH3012 },
Oliver Neukum1e56f1e2014-01-16 16:02:58 +0100222 { USB_DEVICE(0x0cf3, 0x311f), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200223 { USB_DEVICE(0x0cf3, 0x3121), .driver_info = BTUSB_ATH3012 },
Ming Leiebaf5792013-03-18 23:45:11 +0800224 { USB_DEVICE(0x0cf3, 0x817a), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin18e0afa2015-10-16 11:45:26 +0300225 { USB_DEVICE(0x0cf3, 0x817b), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200226 { USB_DEVICE(0x0cf3, 0xe003), .driver_info = BTUSB_ATH3012 },
AceLan Kaoac713112012-04-19 14:53:45 +0800227 { USB_DEVICE(0x0cf3, 0xe004), .driver_info = BTUSB_ATH3012 },
Peng Chen0a3658c2013-08-30 17:41:40 +0800228 { USB_DEVICE(0x0cf3, 0xe005), .driver_info = BTUSB_ATH3012 },
Wen-chien Jesse Sungca79f232015-05-13 11:39:24 +0800229 { USB_DEVICE(0x0cf3, 0xe006), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200230 { USB_DEVICE(0x13d3, 0x3362), .driver_info = BTUSB_ATH3012 },
231 { USB_DEVICE(0x13d3, 0x3375), .driver_info = BTUSB_ATH3012 },
AceLan Kaoeed307e2012-12-11 11:41:20 +0800232 { USB_DEVICE(0x13d3, 0x3393), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin609574e2016-02-10 15:33:17 +0300233 { USB_DEVICE(0x13d3, 0x3395), .driver_info = BTUSB_ATH3012 },
Sujith Manoharan5b77a1f2013-07-15 09:29:03 +0530234 { USB_DEVICE(0x13d3, 0x3402), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin3bb30a7c2014-11-25 20:19:52 +0300235 { USB_DEVICE(0x13d3, 0x3408), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin033efa92015-01-18 00:16:51 +0300236 { USB_DEVICE(0x13d3, 0x3423), .driver_info = BTUSB_ATH3012 },
Anantha Krishnanfa2f1392014-07-08 19:25:08 +0530237 { USB_DEVICE(0x13d3, 0x3432), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin75c6aca2016-03-04 01:32:19 +0300238 { USB_DEVICE(0x13d3, 0x3472), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin0d0cef62015-06-06 20:29:25 +0300239 { USB_DEVICE(0x13d3, 0x3474), .driver_info = BTUSB_ATH3012 },
Lauro Costa72f9f8b2016-05-09 17:36:11 -0300240 { USB_DEVICE(0x13d3, 0x3487), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin12d86892016-07-12 01:35:18 +0300241 { USB_DEVICE(0x13d3, 0x3490), .driver_info = BTUSB_ATH3012 },
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530242
Cho, Yu-Chene9036e32011-02-15 10:20:07 +0800243 /* Atheros AR5BBU12 with sflash firmware */
244 { USB_DEVICE(0x0489, 0xe02c), .driver_info = BTUSB_IGNORE },
245
Michael Gruetzner85d59722012-05-02 22:33:40 +0200246 /* Atheros AR5BBU12 with sflash firmware */
Yevgeniy Melnichukbc21fde2012-08-07 19:48:10 +0530247 { USB_DEVICE(0x0489, 0xe036), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200248 { USB_DEVICE(0x0489, 0xe03c), .driver_info = BTUSB_ATH3012 },
Michael Gruetzner85d59722012-05-02 22:33:40 +0200249
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +0000250 /* QCA ROME chipset */
Wen-chien Jesse Sung20541112015-05-13 11:39:25 +0800251 { USB_DEVICE(0x0cf3, 0xe007), .driver_info = BTUSB_QCA_ROME },
Kai-Heng Feng93519932016-08-16 12:50:06 +0800252 { USB_DEVICE(0x0cf3, 0xe009), .driver_info = BTUSB_QCA_ROME },
Marcel Holtmannc9e44472015-03-16 23:56:04 -0700253 { USB_DEVICE(0x0cf3, 0xe300), .driver_info = BTUSB_QCA_ROME },
254 { USB_DEVICE(0x0cf3, 0xe360), .driver_info = BTUSB_QCA_ROME },
Yvain THONNART47ca5892016-06-11 15:43:30 +0200255 { USB_DEVICE(0x0489, 0xe092), .driver_info = BTUSB_QCA_ROME },
Dmitry Tunin1144a4e2016-09-21 19:13:08 +0300256 { USB_DEVICE(0x04ca, 0x3011), .driver_info = BTUSB_QCA_ROME },
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +0000257
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200258 /* Broadcom BCM2035 */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100259 { USB_DEVICE(0x0a5c, 0x2009), .driver_info = BTUSB_BCM92035 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200260 { USB_DEVICE(0x0a5c, 0x200a), .driver_info = BTUSB_WRONG_SCO_MTU },
261 { USB_DEVICE(0x0a5c, 0x2035), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200262
263 /* Broadcom BCM2045 */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100264 { USB_DEVICE(0x0a5c, 0x2039), .driver_info = BTUSB_WRONG_SCO_MTU },
265 { USB_DEVICE(0x0a5c, 0x2101), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmannbdbef3d2008-09-23 00:16:35 +0200266
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200267 /* IBM/Lenovo ThinkPad with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100268 { USB_DEVICE(0x0a5c, 0x201e), .driver_info = BTUSB_WRONG_SCO_MTU },
269 { USB_DEVICE(0x0a5c, 0x2110), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200270
271 /* HP laptop with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100272 { USB_DEVICE(0x03f0, 0x171d), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200273
274 /* Dell laptop with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100275 { USB_DEVICE(0x413c, 0x8126), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200276
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100277 /* Dell Wireless 370 and 410 devices */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100278 { USB_DEVICE(0x413c, 0x8152), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100279 { USB_DEVICE(0x413c, 0x8156), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200280
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100281 /* Belkin F8T012 and F8T013 devices */
282 { USB_DEVICE(0x050d, 0x0012), .driver_info = BTUSB_WRONG_SCO_MTU },
283 { USB_DEVICE(0x050d, 0x0013), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200284
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100285 /* Asus WL-BTD202 device */
286 { USB_DEVICE(0x0b05, 0x1715), .driver_info = BTUSB_WRONG_SCO_MTU },
287
288 /* Kensington Bluetooth USB adapter */
289 { USB_DEVICE(0x047d, 0x105e), .driver_info = BTUSB_WRONG_SCO_MTU },
290
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200291 /* RTX Telecom based adapters with buggy SCO support */
292 { USB_DEVICE(0x0400, 0x0807), .driver_info = BTUSB_BROKEN_ISOC },
293 { USB_DEVICE(0x0400, 0x080a), .driver_info = BTUSB_BROKEN_ISOC },
294
295 /* CONWISE Technology based adapters with buggy SCO support */
Szymon Janc418678b2016-09-01 17:22:37 +0200296 { USB_DEVICE(0x0e5e, 0x6622),
297 .driver_info = BTUSB_BROKEN_ISOC | BTUSB_CW6622},
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200298
Marcel Holtmann4fcef8e2015-01-01 17:34:37 -0800299 /* Roper Class 1 Bluetooth Dongle (Silicon Wave based) */
Aleksei Volkov2eeac872015-06-08 12:02:10 +0300300 { USB_DEVICE(0x1310, 0x0001), .driver_info = BTUSB_SWAVE },
Marcel Holtmann4fcef8e2015-01-01 17:34:37 -0800301
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200302 /* Digianswer devices */
303 { USB_DEVICE(0x08fd, 0x0001), .driver_info = BTUSB_DIGIANSWER },
304 { USB_DEVICE(0x08fd, 0x0002), .driver_info = BTUSB_IGNORE },
305
306 /* CSR BlueCore Bluetooth Sniffer */
Marcel Holtmann4f64fa82014-07-07 00:12:04 +0200307 { USB_DEVICE(0x0a12, 0x0002),
308 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200309
310 /* Frontline ComProbe Bluetooth Sniffer */
Marcel Holtmann4f64fa82014-07-07 00:12:04 +0200311 { USB_DEVICE(0x16d3, 0x0002),
312 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200313
Marcel Holtmanncb1ee892015-01-28 19:41:42 -0800314 /* Marvell Bluetooth devices */
315 { USB_DEVICE(0x1286, 0x2044), .driver_info = BTUSB_MARVELL },
316 { USB_DEVICE(0x1286, 0x2046), .driver_info = BTUSB_MARVELL },
Amitkumar Karwar1165df02016-09-28 16:18:35 +0530317 { USB_DEVICE(0x1286, 0x204e), .driver_info = BTUSB_MARVELL },
Marcel Holtmanncb1ee892015-01-28 19:41:42 -0800318
Marcel Holtmannd0ac9eb2015-01-28 19:41:43 -0800319 /* Intel Bluetooth devices */
Marcel Holtmann407550f2015-02-22 15:41:18 -0800320 { USB_DEVICE(0x8087, 0x07da), .driver_info = BTUSB_CSR },
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -0700321 { USB_DEVICE(0x8087, 0x07dc), .driver_info = BTUSB_INTEL },
Tedd Ho-Jeong Anef4e5e42013-11-12 13:10:58 -0800322 { USB_DEVICE(0x8087, 0x0a2a), .driver_info = BTUSB_INTEL },
Marcel Holtmanncda0dd72015-01-26 21:33:48 -0800323 { USB_DEVICE(0x8087, 0x0a2b), .driver_info = BTUSB_INTEL_NEW },
Tedd Ho-Jeong An439e65d2016-06-20 13:43:40 -0700324 { USB_DEVICE(0x8087, 0x0aa7), .driver_info = BTUSB_INTEL },
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -0700325
Marcel Holtmannd0ac9eb2015-01-28 19:41:43 -0800326 /* Other Intel Bluetooth devices */
327 { USB_VENDOR_AND_INTERFACE_INFO(0x8087, 0xe0, 0x01, 0x01),
328 .driver_info = BTUSB_IGNORE },
Amitkumar Karwarae8df492014-07-18 14:47:06 -0700329
Daniel Drakea2698a92015-04-16 14:09:55 -0600330 /* Realtek Bluetooth devices */
331 { USB_VENDOR_AND_INTERFACE_INFO(0x0bda, 0xe0, 0x01, 0x01),
332 .driver_info = BTUSB_REALTEK },
333
334 /* Additional Realtek 8723AE Bluetooth devices */
335 { USB_DEVICE(0x0930, 0x021d), .driver_info = BTUSB_REALTEK },
336 { USB_DEVICE(0x13d3, 0x3394), .driver_info = BTUSB_REALTEK },
337
338 /* Additional Realtek 8723BE Bluetooth devices */
339 { USB_DEVICE(0x0489, 0xe085), .driver_info = BTUSB_REALTEK },
340 { USB_DEVICE(0x0489, 0xe08b), .driver_info = BTUSB_REALTEK },
341 { USB_DEVICE(0x13d3, 0x3410), .driver_info = BTUSB_REALTEK },
342 { USB_DEVICE(0x13d3, 0x3416), .driver_info = BTUSB_REALTEK },
343 { USB_DEVICE(0x13d3, 0x3459), .driver_info = BTUSB_REALTEK },
344
345 /* Additional Realtek 8821AE Bluetooth devices */
346 { USB_DEVICE(0x0b05, 0x17dc), .driver_info = BTUSB_REALTEK },
347 { USB_DEVICE(0x13d3, 0x3414), .driver_info = BTUSB_REALTEK },
348 { USB_DEVICE(0x13d3, 0x3458), .driver_info = BTUSB_REALTEK },
349 { USB_DEVICE(0x13d3, 0x3461), .driver_info = BTUSB_REALTEK },
350 { USB_DEVICE(0x13d3, 0x3462), .driver_info = BTUSB_REALTEK },
351
Peter Poklop4481c072015-08-15 20:47:09 +0200352 /* Silicon Wave based devices */
353 { USB_DEVICE(0x0c10, 0x0000), .driver_info = BTUSB_SWAVE },
354
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200355 { } /* Terminating entry */
356};
357
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200358#define BTUSB_MAX_ISOC_FRAMES 10
359
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200360#define BTUSB_INTR_RUNNING 0
361#define BTUSB_BULK_RUNNING 1
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200362#define BTUSB_ISOC_RUNNING 2
Oliver Neukum7bee5492009-08-24 23:44:59 +0200363#define BTUSB_SUSPENDING 3
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -0300364#define BTUSB_DID_ISO_RESUME 4
Marcel Holtmanncda0dd72015-01-26 21:33:48 -0800365#define BTUSB_BOOTLOADER 5
366#define BTUSB_DOWNLOADING 6
Marcel Holtmannce6bb922015-01-28 01:58:40 -0800367#define BTUSB_FIRMWARE_LOADED 7
Marcel Holtmanncda0dd72015-01-26 21:33:48 -0800368#define BTUSB_FIRMWARE_FAILED 8
Marcel Holtmannce6bb922015-01-28 01:58:40 -0800369#define BTUSB_BOOTING 9
Daniel Drake04b8c812015-05-21 08:23:50 -0600370#define BTUSB_RESET_RESUME 10
Marcel Holtmann9d08f502015-10-08 20:23:08 +0200371#define BTUSB_DIAG_RUNNING 11
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200372
373struct btusb_data {
374 struct hci_dev *hdev;
375 struct usb_device *udev;
Marcel Holtmann5fbcd262008-09-23 00:16:36 +0200376 struct usb_interface *intf;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200377 struct usb_interface *isoc;
Marcel Holtmann9d08f502015-10-08 20:23:08 +0200378 struct usb_interface *diag;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200379
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200380 unsigned long flags;
381
382 struct work_struct work;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200383 struct work_struct waker;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200384
Marcel Holtmann803b5832014-09-16 08:00:29 +0200385 struct usb_anchor deferred;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200386 struct usb_anchor tx_anchor;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200387 int tx_in_flight;
388 spinlock_t txlock;
389
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200390 struct usb_anchor intr_anchor;
391 struct usb_anchor bulk_anchor;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200392 struct usb_anchor isoc_anchor;
Marcel Holtmann9d08f502015-10-08 20:23:08 +0200393 struct usb_anchor diag_anchor;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200394 spinlock_t rxlock;
395
396 struct sk_buff *evt_skb;
397 struct sk_buff *acl_skb;
398 struct sk_buff *sco_skb;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200399
400 struct usb_endpoint_descriptor *intr_ep;
401 struct usb_endpoint_descriptor *bulk_tx_ep;
402 struct usb_endpoint_descriptor *bulk_rx_ep;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200403 struct usb_endpoint_descriptor *isoc_tx_ep;
404 struct usb_endpoint_descriptor *isoc_rx_ep;
Marcel Holtmann9d08f502015-10-08 20:23:08 +0200405 struct usb_endpoint_descriptor *diag_tx_ep;
406 struct usb_endpoint_descriptor *diag_rx_ep;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200407
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100408 __u8 cmdreq_type;
Marcel Holtmann893ba542015-01-28 20:27:34 -0800409 __u8 cmdreq;
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100410
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100411 unsigned int sco_num;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200412 int isoc_altsetting;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +0100413 int suspend_count;
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +0100414
Marcel Holtmann97307f52015-01-12 13:51:10 -0800415 int (*recv_event)(struct hci_dev *hdev, struct sk_buff *skb);
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +0100416 int (*recv_bulk)(struct btusb_data *data, void *buffer, int count);
Kim, Ben Young Taeace31982015-02-15 23:06:14 +0000417
418 int (*setup_on_usb)(struct hci_dev *hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200419};
420
Marcel Holtmann803b5832014-09-16 08:00:29 +0200421static inline void btusb_free_frags(struct btusb_data *data)
422{
423 unsigned long flags;
424
425 spin_lock_irqsave(&data->rxlock, flags);
426
427 kfree_skb(data->evt_skb);
428 data->evt_skb = NULL;
429
430 kfree_skb(data->acl_skb);
431 data->acl_skb = NULL;
432
433 kfree_skb(data->sco_skb);
434 data->sco_skb = NULL;
435
436 spin_unlock_irqrestore(&data->rxlock, flags);
437}
438
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200439static int btusb_recv_intr(struct btusb_data *data, void *buffer, int count)
440{
Marcel Holtmann803b5832014-09-16 08:00:29 +0200441 struct sk_buff *skb;
442 int err = 0;
443
444 spin_lock(&data->rxlock);
445 skb = data->evt_skb;
446
447 while (count) {
448 int len;
449
450 if (!skb) {
451 skb = bt_skb_alloc(HCI_MAX_EVENT_SIZE, GFP_ATOMIC);
452 if (!skb) {
453 err = -ENOMEM;
454 break;
455 }
456
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100457 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
458 hci_skb_expect(skb) = HCI_EVENT_HDR_SIZE;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200459 }
460
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100461 len = min_t(uint, hci_skb_expect(skb), count);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200462 memcpy(skb_put(skb, len), buffer, len);
463
464 count -= len;
465 buffer += len;
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100466 hci_skb_expect(skb) -= len;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200467
468 if (skb->len == HCI_EVENT_HDR_SIZE) {
469 /* Complete event header */
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100470 hci_skb_expect(skb) = hci_event_hdr(skb)->plen;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200471
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100472 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
Marcel Holtmann803b5832014-09-16 08:00:29 +0200473 kfree_skb(skb);
474 skb = NULL;
475
476 err = -EILSEQ;
477 break;
478 }
479 }
480
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100481 if (!hci_skb_expect(skb)) {
Marcel Holtmann803b5832014-09-16 08:00:29 +0200482 /* Complete frame */
Marcel Holtmann97307f52015-01-12 13:51:10 -0800483 data->recv_event(data->hdev, skb);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200484 skb = NULL;
485 }
486 }
487
488 data->evt_skb = skb;
489 spin_unlock(&data->rxlock);
490
491 return err;
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200492}
493
494static int btusb_recv_bulk(struct btusb_data *data, void *buffer, int count)
495{
Marcel Holtmann803b5832014-09-16 08:00:29 +0200496 struct sk_buff *skb;
497 int err = 0;
498
499 spin_lock(&data->rxlock);
500 skb = data->acl_skb;
501
502 while (count) {
503 int len;
504
505 if (!skb) {
506 skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
507 if (!skb) {
508 err = -ENOMEM;
509 break;
510 }
511
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100512 hci_skb_pkt_type(skb) = HCI_ACLDATA_PKT;
513 hci_skb_expect(skb) = HCI_ACL_HDR_SIZE;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200514 }
515
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100516 len = min_t(uint, hci_skb_expect(skb), count);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200517 memcpy(skb_put(skb, len), buffer, len);
518
519 count -= len;
520 buffer += len;
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100521 hci_skb_expect(skb) -= len;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200522
523 if (skb->len == HCI_ACL_HDR_SIZE) {
524 __le16 dlen = hci_acl_hdr(skb)->dlen;
525
526 /* Complete ACL header */
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100527 hci_skb_expect(skb) = __le16_to_cpu(dlen);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200528
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100529 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
Marcel Holtmann803b5832014-09-16 08:00:29 +0200530 kfree_skb(skb);
531 skb = NULL;
532
533 err = -EILSEQ;
534 break;
535 }
536 }
537
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100538 if (!hci_skb_expect(skb)) {
Marcel Holtmann803b5832014-09-16 08:00:29 +0200539 /* Complete frame */
540 hci_recv_frame(data->hdev, skb);
541 skb = NULL;
542 }
543 }
544
545 data->acl_skb = skb;
546 spin_unlock(&data->rxlock);
547
548 return err;
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200549}
550
551static int btusb_recv_isoc(struct btusb_data *data, void *buffer, int count)
552{
Marcel Holtmann803b5832014-09-16 08:00:29 +0200553 struct sk_buff *skb;
554 int err = 0;
555
556 spin_lock(&data->rxlock);
557 skb = data->sco_skb;
558
559 while (count) {
560 int len;
561
562 if (!skb) {
563 skb = bt_skb_alloc(HCI_MAX_SCO_SIZE, GFP_ATOMIC);
564 if (!skb) {
565 err = -ENOMEM;
566 break;
567 }
568
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100569 hci_skb_pkt_type(skb) = HCI_SCODATA_PKT;
570 hci_skb_expect(skb) = HCI_SCO_HDR_SIZE;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200571 }
572
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100573 len = min_t(uint, hci_skb_expect(skb), count);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200574 memcpy(skb_put(skb, len), buffer, len);
575
576 count -= len;
577 buffer += len;
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100578 hci_skb_expect(skb) -= len;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200579
580 if (skb->len == HCI_SCO_HDR_SIZE) {
581 /* Complete SCO header */
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100582 hci_skb_expect(skb) = hci_sco_hdr(skb)->dlen;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200583
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100584 if (skb_tailroom(skb) < hci_skb_expect(skb)) {
Marcel Holtmann803b5832014-09-16 08:00:29 +0200585 kfree_skb(skb);
586 skb = NULL;
587
588 err = -EILSEQ;
589 break;
590 }
591 }
592
Marcel Holtmann618e8bc2015-11-05 07:33:56 +0100593 if (!hci_skb_expect(skb)) {
Marcel Holtmann803b5832014-09-16 08:00:29 +0200594 /* Complete frame */
595 hci_recv_frame(data->hdev, skb);
596 skb = NULL;
597 }
598 }
599
600 data->sco_skb = skb;
601 spin_unlock(&data->rxlock);
602
603 return err;
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200604}
605
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200606static void btusb_intr_complete(struct urb *urb)
607{
608 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +0100609 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200610 int err;
611
Marcel Holtmann89e75332014-09-16 04:44:50 +0200612 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
613 urb->actual_length);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200614
615 if (!test_bit(HCI_RUNNING, &hdev->flags))
616 return;
617
618 if (urb->status == 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200619 hdev->stat.byte_rx += urb->actual_length;
620
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200621 if (btusb_recv_intr(data, urb->transfer_buffer,
622 urb->actual_length) < 0) {
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200623 BT_ERR("%s corrupted event packet", hdev->name);
624 hdev->stat.err_rx++;
625 }
Champion Chen85560c42014-09-06 14:06:08 -0500626 } else if (urb->status == -ENOENT) {
627 /* Avoid suspend failed when usb_kill_urb */
628 return;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200629 }
630
631 if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
632 return;
633
Oliver Neukum7bee5492009-08-24 23:44:59 +0200634 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200635 usb_anchor_urb(urb, &data->intr_anchor);
636
637 err = usb_submit_urb(urb, GFP_ATOMIC);
638 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200639 /* -EPERM: urb is being killed;
640 * -ENODEV: device got disconnected */
641 if (err != -EPERM && err != -ENODEV)
Stefan Seyfried61faddf2010-11-30 21:49:08 +0100642 BT_ERR("%s urb %p failed to resubmit (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200643 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200644 usb_unanchor_urb(urb);
645 }
646}
647
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100648static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200649{
David Herrmann155961e2012-02-09 21:58:32 +0100650 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200651 struct urb *urb;
652 unsigned char *buf;
653 unsigned int pipe;
654 int err, size;
655
656 BT_DBG("%s", hdev->name);
657
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200658 if (!data->intr_ep)
659 return -ENODEV;
660
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100661 urb = usb_alloc_urb(0, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200662 if (!urb)
663 return -ENOMEM;
664
665 size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
666
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100667 buf = kmalloc(size, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200668 if (!buf) {
669 usb_free_urb(urb);
670 return -ENOMEM;
671 }
672
673 pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
674
675 usb_fill_int_urb(urb, data->udev, pipe, buf, size,
Marcel Holtmann89e75332014-09-16 04:44:50 +0200676 btusb_intr_complete, hdev, data->intr_ep->bInterval);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200677
678 urb->transfer_flags |= URB_FREE_BUFFER;
679
680 usb_anchor_urb(urb, &data->intr_anchor);
681
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100682 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200683 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +0200684 if (err != -EPERM && err != -ENODEV)
685 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200686 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200687 usb_unanchor_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200688 }
689
690 usb_free_urb(urb);
691
692 return err;
693}
694
695static void btusb_bulk_complete(struct urb *urb)
696{
697 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +0100698 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200699 int err;
700
Marcel Holtmann89e75332014-09-16 04:44:50 +0200701 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
702 urb->actual_length);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200703
704 if (!test_bit(HCI_RUNNING, &hdev->flags))
705 return;
706
707 if (urb->status == 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200708 hdev->stat.byte_rx += urb->actual_length;
709
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +0100710 if (data->recv_bulk(data, urb->transfer_buffer,
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200711 urb->actual_length) < 0) {
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200712 BT_ERR("%s corrupted ACL packet", hdev->name);
713 hdev->stat.err_rx++;
714 }
Champion Chen85560c42014-09-06 14:06:08 -0500715 } else if (urb->status == -ENOENT) {
716 /* Avoid suspend failed when usb_kill_urb */
717 return;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200718 }
719
720 if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
721 return;
722
723 usb_anchor_urb(urb, &data->bulk_anchor);
Oliver Neukum652fd782009-12-16 19:23:43 +0100724 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200725
726 err = usb_submit_urb(urb, GFP_ATOMIC);
727 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200728 /* -EPERM: urb is being killed;
729 * -ENODEV: device got disconnected */
730 if (err != -EPERM && err != -ENODEV)
Stefan Seyfried61faddf2010-11-30 21:49:08 +0100731 BT_ERR("%s urb %p failed to resubmit (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200732 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200733 usb_unanchor_urb(urb);
734 }
735}
736
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100737static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200738{
David Herrmann155961e2012-02-09 21:58:32 +0100739 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200740 struct urb *urb;
741 unsigned char *buf;
742 unsigned int pipe;
Vikram Kandukuri290ba202009-07-02 14:31:59 +0530743 int err, size = HCI_MAX_FRAME_SIZE;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200744
745 BT_DBG("%s", hdev->name);
746
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200747 if (!data->bulk_rx_ep)
748 return -ENODEV;
749
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100750 urb = usb_alloc_urb(0, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200751 if (!urb)
752 return -ENOMEM;
753
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100754 buf = kmalloc(size, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200755 if (!buf) {
756 usb_free_urb(urb);
757 return -ENOMEM;
758 }
759
760 pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
761
Marcel Holtmann89e75332014-09-16 04:44:50 +0200762 usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
763 btusb_bulk_complete, hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200764
765 urb->transfer_flags |= URB_FREE_BUFFER;
766
Oliver Neukum7bee5492009-08-24 23:44:59 +0200767 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200768 usb_anchor_urb(urb, &data->bulk_anchor);
769
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100770 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200771 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +0200772 if (err != -EPERM && err != -ENODEV)
773 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200774 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200775 usb_unanchor_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200776 }
777
778 usb_free_urb(urb);
779
780 return err;
781}
782
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200783static void btusb_isoc_complete(struct urb *urb)
784{
785 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +0100786 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200787 int i, err;
788
Marcel Holtmann89e75332014-09-16 04:44:50 +0200789 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
790 urb->actual_length);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200791
792 if (!test_bit(HCI_RUNNING, &hdev->flags))
793 return;
794
795 if (urb->status == 0) {
796 for (i = 0; i < urb->number_of_packets; i++) {
797 unsigned int offset = urb->iso_frame_desc[i].offset;
798 unsigned int length = urb->iso_frame_desc[i].actual_length;
799
800 if (urb->iso_frame_desc[i].status)
801 continue;
802
803 hdev->stat.byte_rx += length;
804
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200805 if (btusb_recv_isoc(data, urb->transfer_buffer + offset,
806 length) < 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200807 BT_ERR("%s corrupted SCO packet", hdev->name);
808 hdev->stat.err_rx++;
809 }
810 }
Champion Chen85560c42014-09-06 14:06:08 -0500811 } else if (urb->status == -ENOENT) {
812 /* Avoid suspend failed when usb_kill_urb */
813 return;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200814 }
815
816 if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags))
817 return;
818
819 usb_anchor_urb(urb, &data->isoc_anchor);
820
821 err = usb_submit_urb(urb, GFP_ATOMIC);
822 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200823 /* -EPERM: urb is being killed;
824 * -ENODEV: device got disconnected */
825 if (err != -EPERM && err != -ENODEV)
Stefan Seyfried61faddf2010-11-30 21:49:08 +0100826 BT_ERR("%s urb %p failed to resubmit (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200827 hdev->name, urb, -err);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200828 usb_unanchor_urb(urb);
829 }
830}
831
Jesper Juhl42b16b32011-01-17 00:09:38 +0100832static inline void __fill_isoc_descriptor(struct urb *urb, int len, int mtu)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200833{
834 int i, offset = 0;
835
836 BT_DBG("len %d mtu %d", len, mtu);
837
838 for (i = 0; i < BTUSB_MAX_ISOC_FRAMES && len >= mtu;
839 i++, offset += mtu, len -= mtu) {
840 urb->iso_frame_desc[i].offset = offset;
841 urb->iso_frame_desc[i].length = mtu;
842 }
843
844 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
845 urb->iso_frame_desc[i].offset = offset;
846 urb->iso_frame_desc[i].length = len;
847 i++;
848 }
849
850 urb->number_of_packets = i;
851}
852
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100853static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200854{
David Herrmann155961e2012-02-09 21:58:32 +0100855 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200856 struct urb *urb;
857 unsigned char *buf;
858 unsigned int pipe;
859 int err, size;
860
861 BT_DBG("%s", hdev->name);
862
863 if (!data->isoc_rx_ep)
864 return -ENODEV;
865
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100866 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200867 if (!urb)
868 return -ENOMEM;
869
870 size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) *
871 BTUSB_MAX_ISOC_FRAMES;
872
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100873 buf = kmalloc(size, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200874 if (!buf) {
875 usb_free_urb(urb);
876 return -ENOMEM;
877 }
878
879 pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress);
880
Bing Zhaofa0fb932011-12-20 18:19:00 -0800881 usb_fill_int_urb(urb, data->udev, pipe, buf, size, btusb_isoc_complete,
Marcel Holtmann89e75332014-09-16 04:44:50 +0200882 hdev, data->isoc_rx_ep->bInterval);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200883
Marcel Holtmann89e75332014-09-16 04:44:50 +0200884 urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200885
886 __fill_isoc_descriptor(urb, size,
Marcel Holtmann89e75332014-09-16 04:44:50 +0200887 le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize));
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200888
889 usb_anchor_urb(urb, &data->isoc_anchor);
890
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100891 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200892 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +0200893 if (err != -EPERM && err != -ENODEV)
894 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200895 hdev->name, urb, -err);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200896 usb_unanchor_urb(urb);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200897 }
898
899 usb_free_urb(urb);
900
901 return err;
902}
903
Marcel Holtmann9d08f502015-10-08 20:23:08 +0200904static void btusb_diag_complete(struct urb *urb)
905{
906 struct hci_dev *hdev = urb->context;
907 struct btusb_data *data = hci_get_drvdata(hdev);
908 int err;
909
910 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
911 urb->actual_length);
912
913 if (urb->status == 0) {
914 struct sk_buff *skb;
915
916 skb = bt_skb_alloc(urb->actual_length, GFP_ATOMIC);
917 if (skb) {
918 memcpy(skb_put(skb, urb->actual_length),
919 urb->transfer_buffer, urb->actual_length);
920 hci_recv_diag(hdev, skb);
921 }
922 } else if (urb->status == -ENOENT) {
923 /* Avoid suspend failed when usb_kill_urb */
924 return;
925 }
926
927 if (!test_bit(BTUSB_DIAG_RUNNING, &data->flags))
928 return;
929
930 usb_anchor_urb(urb, &data->diag_anchor);
931 usb_mark_last_busy(data->udev);
932
933 err = usb_submit_urb(urb, GFP_ATOMIC);
934 if (err < 0) {
935 /* -EPERM: urb is being killed;
936 * -ENODEV: device got disconnected */
937 if (err != -EPERM && err != -ENODEV)
938 BT_ERR("%s urb %p failed to resubmit (%d)",
939 hdev->name, urb, -err);
940 usb_unanchor_urb(urb);
941 }
942}
943
944static int btusb_submit_diag_urb(struct hci_dev *hdev, gfp_t mem_flags)
945{
946 struct btusb_data *data = hci_get_drvdata(hdev);
947 struct urb *urb;
948 unsigned char *buf;
949 unsigned int pipe;
950 int err, size = HCI_MAX_FRAME_SIZE;
951
952 BT_DBG("%s", hdev->name);
953
954 if (!data->diag_rx_ep)
955 return -ENODEV;
956
957 urb = usb_alloc_urb(0, mem_flags);
958 if (!urb)
959 return -ENOMEM;
960
961 buf = kmalloc(size, mem_flags);
962 if (!buf) {
963 usb_free_urb(urb);
964 return -ENOMEM;
965 }
966
967 pipe = usb_rcvbulkpipe(data->udev, data->diag_rx_ep->bEndpointAddress);
968
969 usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
970 btusb_diag_complete, hdev);
971
972 urb->transfer_flags |= URB_FREE_BUFFER;
973
974 usb_mark_last_busy(data->udev);
975 usb_anchor_urb(urb, &data->diag_anchor);
976
977 err = usb_submit_urb(urb, mem_flags);
978 if (err < 0) {
979 if (err != -EPERM && err != -ENODEV)
980 BT_ERR("%s urb %p submission failed (%d)",
981 hdev->name, urb, -err);
982 usb_unanchor_urb(urb);
983 }
984
985 usb_free_urb(urb);
986
987 return err;
988}
989
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200990static void btusb_tx_complete(struct urb *urb)
991{
992 struct sk_buff *skb = urb->context;
Marcel Holtmann89e75332014-09-16 04:44:50 +0200993 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
David Herrmann155961e2012-02-09 21:58:32 +0100994 struct btusb_data *data = hci_get_drvdata(hdev);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200995
Marcel Holtmann89e75332014-09-16 04:44:50 +0200996 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
997 urb->actual_length);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200998
999 if (!test_bit(HCI_RUNNING, &hdev->flags))
1000 goto done;
1001
1002 if (!urb->status)
1003 hdev->stat.byte_tx += urb->transfer_buffer_length;
1004 else
1005 hdev->stat.err_tx++;
1006
1007done:
1008 spin_lock(&data->txlock);
1009 data->tx_in_flight--;
1010 spin_unlock(&data->txlock);
1011
1012 kfree(urb->setup_packet);
1013
1014 kfree_skb(skb);
1015}
1016
1017static void btusb_isoc_tx_complete(struct urb *urb)
1018{
1019 struct sk_buff *skb = urb->context;
Marcel Holtmann89e75332014-09-16 04:44:50 +02001020 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001021
Marcel Holtmann89e75332014-09-16 04:44:50 +02001022 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
1023 urb->actual_length);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001024
1025 if (!test_bit(HCI_RUNNING, &hdev->flags))
1026 goto done;
1027
1028 if (!urb->status)
1029 hdev->stat.byte_tx += urb->transfer_buffer_length;
1030 else
1031 hdev->stat.err_tx++;
1032
1033done:
1034 kfree(urb->setup_packet);
1035
1036 kfree_skb(skb);
1037}
1038
1039static int btusb_open(struct hci_dev *hdev)
1040{
David Herrmann155961e2012-02-09 21:58:32 +01001041 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001042 int err;
1043
1044 BT_DBG("%s", hdev->name);
1045
Ethan Hsiehc7e163f2016-10-07 12:06:42 +08001046 err = usb_autopm_get_interface(data->intf);
1047 if (err < 0)
1048 return err;
1049
Kim, Ben Young Taeace31982015-02-15 23:06:14 +00001050 /* Patching USB firmware files prior to starting any URBs of HCI path
1051 * It is more safe to use USB bulk channel for downloading USB patch
1052 */
1053 if (data->setup_on_usb) {
1054 err = data->setup_on_usb(hdev);
Marcel Holtmanneb500422015-04-16 23:15:50 +02001055 if (err < 0)
Kim, Ben Young Taeace31982015-02-15 23:06:14 +00001056 return err;
1057 }
1058
Oliver Neukum7bee5492009-08-24 23:44:59 +02001059 data->intf->needs_remote_wakeup = 1;
1060
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001061 if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +02001062 goto done;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001063
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001064 err = btusb_submit_intr_urb(hdev, GFP_KERNEL);
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001065 if (err < 0)
1066 goto failed;
1067
1068 err = btusb_submit_bulk_urb(hdev, GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001069 if (err < 0) {
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001070 usb_kill_anchored_urbs(&data->intr_anchor);
1071 goto failed;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001072 }
1073
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001074 set_bit(BTUSB_BULK_RUNNING, &data->flags);
1075 btusb_submit_bulk_urb(hdev, GFP_KERNEL);
1076
Marcel Holtmann9d08f502015-10-08 20:23:08 +02001077 if (data->diag) {
1078 if (!btusb_submit_diag_urb(hdev, GFP_KERNEL))
1079 set_bit(BTUSB_DIAG_RUNNING, &data->flags);
1080 }
1081
Oliver Neukum7bee5492009-08-24 23:44:59 +02001082done:
1083 usb_autopm_put_interface(data->intf);
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001084 return 0;
1085
1086failed:
1087 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001088 usb_autopm_put_interface(data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001089 return err;
1090}
1091
Oliver Neukum7bee5492009-08-24 23:44:59 +02001092static void btusb_stop_traffic(struct btusb_data *data)
1093{
1094 usb_kill_anchored_urbs(&data->intr_anchor);
1095 usb_kill_anchored_urbs(&data->bulk_anchor);
1096 usb_kill_anchored_urbs(&data->isoc_anchor);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02001097 usb_kill_anchored_urbs(&data->diag_anchor);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001098}
1099
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001100static int btusb_close(struct hci_dev *hdev)
1101{
David Herrmann155961e2012-02-09 21:58:32 +01001102 struct btusb_data *data = hci_get_drvdata(hdev);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001103 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001104
1105 BT_DBG("%s", hdev->name);
1106
Marcel Holtmanne8c3c3d2008-09-23 00:16:36 +02001107 cancel_work_sync(&data->work);
Linus Torvalds404291a2009-11-11 13:32:29 -08001108 cancel_work_sync(&data->waker);
Marcel Holtmanne8c3c3d2008-09-23 00:16:36 +02001109
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001110 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001111 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001112 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02001113 clear_bit(BTUSB_DIAG_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001114
1115 btusb_stop_traffic(data);
Marcel Holtmann803b5832014-09-16 08:00:29 +02001116 btusb_free_frags(data);
1117
Oliver Neukum7bee5492009-08-24 23:44:59 +02001118 err = usb_autopm_get_interface(data->intf);
1119 if (err < 0)
Oliver Neukum7b8e2c12009-11-13 14:26:23 +01001120 goto failed;
Oliver Neukum7bee5492009-08-24 23:44:59 +02001121
1122 data->intf->needs_remote_wakeup = 0;
1123 usb_autopm_put_interface(data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001124
Oliver Neukum7b8e2c12009-11-13 14:26:23 +01001125failed:
1126 usb_scuttle_anchored_urbs(&data->deferred);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001127 return 0;
1128}
1129
1130static int btusb_flush(struct hci_dev *hdev)
1131{
David Herrmann155961e2012-02-09 21:58:32 +01001132 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001133
1134 BT_DBG("%s", hdev->name);
1135
1136 usb_kill_anchored_urbs(&data->tx_anchor);
Marcel Holtmann803b5832014-09-16 08:00:29 +02001137 btusb_free_frags(data);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001138
1139 return 0;
1140}
1141
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001142static struct urb *alloc_ctrl_urb(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001143{
David Herrmann155961e2012-02-09 21:58:32 +01001144 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001145 struct usb_ctrlrequest *dr;
1146 struct urb *urb;
1147 unsigned int pipe;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001148
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001149 urb = usb_alloc_urb(0, GFP_KERNEL);
1150 if (!urb)
1151 return ERR_PTR(-ENOMEM);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001152
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001153 dr = kmalloc(sizeof(*dr), GFP_KERNEL);
1154 if (!dr) {
1155 usb_free_urb(urb);
1156 return ERR_PTR(-ENOMEM);
1157 }
1158
1159 dr->bRequestType = data->cmdreq_type;
Marcel Holtmann893ba542015-01-28 20:27:34 -08001160 dr->bRequest = data->cmdreq;
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001161 dr->wIndex = 0;
1162 dr->wValue = 0;
1163 dr->wLength = __cpu_to_le16(skb->len);
1164
1165 pipe = usb_sndctrlpipe(data->udev, 0x00);
1166
Marcel Holtmann89e75332014-09-16 04:44:50 +02001167 usb_fill_control_urb(urb, data->udev, pipe, (void *)dr,
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001168 skb->data, skb->len, btusb_tx_complete, skb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001169
Marcel Holtmann89e75332014-09-16 04:44:50 +02001170 skb->dev = (void *)hdev;
Marcel Holtmann7bd8f092013-10-11 06:19:18 -07001171
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001172 return urb;
1173}
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001174
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001175static struct urb *alloc_bulk_urb(struct hci_dev *hdev, struct sk_buff *skb)
1176{
1177 struct btusb_data *data = hci_get_drvdata(hdev);
1178 struct urb *urb;
1179 unsigned int pipe;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001180
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001181 if (!data->bulk_tx_ep)
1182 return ERR_PTR(-ENODEV);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001183
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001184 urb = usb_alloc_urb(0, GFP_KERNEL);
1185 if (!urb)
1186 return ERR_PTR(-ENOMEM);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001187
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001188 pipe = usb_sndbulkpipe(data->udev, data->bulk_tx_ep->bEndpointAddress);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001189
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001190 usb_fill_bulk_urb(urb, data->udev, pipe,
1191 skb->data, skb->len, btusb_tx_complete, skb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001192
Marcel Holtmann89e75332014-09-16 04:44:50 +02001193 skb->dev = (void *)hdev;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001194
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001195 return urb;
1196}
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001197
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001198static struct urb *alloc_isoc_urb(struct hci_dev *hdev, struct sk_buff *skb)
1199{
1200 struct btusb_data *data = hci_get_drvdata(hdev);
1201 struct urb *urb;
1202 unsigned int pipe;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001203
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001204 if (!data->isoc_tx_ep)
1205 return ERR_PTR(-ENODEV);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001206
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001207 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_KERNEL);
1208 if (!urb)
1209 return ERR_PTR(-ENOMEM);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001210
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001211 pipe = usb_sndisocpipe(data->udev, data->isoc_tx_ep->bEndpointAddress);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001212
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001213 usb_fill_int_urb(urb, data->udev, pipe,
1214 skb->data, skb->len, btusb_isoc_tx_complete,
1215 skb, data->isoc_tx_ep->bInterval);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001216
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001217 urb->transfer_flags = URB_ISO_ASAP;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001218
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001219 __fill_isoc_descriptor(urb, skb->len,
1220 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001221
Marcel Holtmann89e75332014-09-16 04:44:50 +02001222 skb->dev = (void *)hdev;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001223
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001224 return urb;
1225}
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001226
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001227static int submit_tx_urb(struct hci_dev *hdev, struct urb *urb)
1228{
1229 struct btusb_data *data = hci_get_drvdata(hdev);
1230 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001231
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001232 usb_anchor_urb(urb, &data->tx_anchor);
1233
Johan Hedberge9753ef2014-09-14 08:49:34 +03001234 err = usb_submit_urb(urb, GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001235 if (err < 0) {
Paul Bolle5a9b80e2011-10-09 12:12:16 +02001236 if (err != -EPERM && err != -ENODEV)
1237 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +02001238 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001239 kfree(urb->setup_packet);
1240 usb_unanchor_urb(urb);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001241 } else {
1242 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001243 }
1244
Cong Wang54a8a792011-11-22 09:32:57 +08001245 usb_free_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001246 return err;
1247}
1248
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001249static int submit_or_queue_tx_urb(struct hci_dev *hdev, struct urb *urb)
1250{
1251 struct btusb_data *data = hci_get_drvdata(hdev);
1252 unsigned long flags;
1253 bool suspending;
1254
1255 spin_lock_irqsave(&data->txlock, flags);
1256 suspending = test_bit(BTUSB_SUSPENDING, &data->flags);
1257 if (!suspending)
1258 data->tx_in_flight++;
1259 spin_unlock_irqrestore(&data->txlock, flags);
1260
1261 if (!suspending)
1262 return submit_tx_urb(hdev, urb);
1263
1264 usb_anchor_urb(urb, &data->deferred);
1265 schedule_work(&data->waker);
1266
1267 usb_free_urb(urb);
1268 return 0;
1269}
1270
1271static int btusb_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
1272{
1273 struct urb *urb;
1274
1275 BT_DBG("%s", hdev->name);
1276
Marcel Holtmann618e8bc2015-11-05 07:33:56 +01001277 switch (hci_skb_pkt_type(skb)) {
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001278 case HCI_COMMAND_PKT:
1279 urb = alloc_ctrl_urb(hdev, skb);
1280 if (IS_ERR(urb))
1281 return PTR_ERR(urb);
1282
1283 hdev->stat.cmd_tx++;
1284 return submit_or_queue_tx_urb(hdev, urb);
1285
1286 case HCI_ACLDATA_PKT:
1287 urb = alloc_bulk_urb(hdev, skb);
1288 if (IS_ERR(urb))
1289 return PTR_ERR(urb);
1290
1291 hdev->stat.acl_tx++;
1292 return submit_or_queue_tx_urb(hdev, urb);
1293
1294 case HCI_SCODATA_PKT:
1295 if (hci_conn_num(hdev, SCO_LINK) < 1)
1296 return -ENODEV;
1297
1298 urb = alloc_isoc_urb(hdev, skb);
1299 if (IS_ERR(urb))
1300 return PTR_ERR(urb);
1301
1302 hdev->stat.sco_tx++;
1303 return submit_tx_urb(hdev, urb);
1304 }
1305
1306 return -EILSEQ;
1307}
1308
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001309static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
1310{
David Herrmann155961e2012-02-09 21:58:32 +01001311 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001312
1313 BT_DBG("%s evt %d", hdev->name, evt);
1314
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001315 if (hci_conn_num(hdev, SCO_LINK) != data->sco_num) {
1316 data->sco_num = hci_conn_num(hdev, SCO_LINK);
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001317 schedule_work(&data->work);
Marcel Holtmanna780efa2008-11-30 12:17:12 +01001318 }
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001319}
1320
Jesper Juhl42b16b32011-01-17 00:09:38 +01001321static inline int __set_isoc_interface(struct hci_dev *hdev, int altsetting)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001322{
David Herrmann155961e2012-02-09 21:58:32 +01001323 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001324 struct usb_interface *intf = data->isoc;
1325 struct usb_endpoint_descriptor *ep_desc;
1326 int i, err;
1327
1328 if (!data->isoc)
1329 return -ENODEV;
1330
1331 err = usb_set_interface(data->udev, 1, altsetting);
1332 if (err < 0) {
1333 BT_ERR("%s setting interface failed (%d)", hdev->name, -err);
1334 return err;
1335 }
1336
1337 data->isoc_altsetting = altsetting;
1338
1339 data->isoc_tx_ep = NULL;
1340 data->isoc_rx_ep = NULL;
1341
1342 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
1343 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
1344
1345 if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) {
1346 data->isoc_tx_ep = ep_desc;
1347 continue;
1348 }
1349
1350 if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) {
1351 data->isoc_rx_ep = ep_desc;
1352 continue;
1353 }
1354 }
1355
1356 if (!data->isoc_tx_ep || !data->isoc_rx_ep) {
1357 BT_ERR("%s invalid SCO descriptors", hdev->name);
1358 return -ENODEV;
1359 }
1360
1361 return 0;
1362}
1363
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001364static void btusb_work(struct work_struct *work)
1365{
1366 struct btusb_data *data = container_of(work, struct btusb_data, work);
1367 struct hci_dev *hdev = data->hdev;
Mikel Astizf4001d22012-04-11 08:48:51 +02001368 int new_alts;
Oliver Neukum7bee5492009-08-24 23:44:59 +02001369 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001370
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001371 if (data->sco_num > 0) {
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -03001372 if (!test_bit(BTUSB_DID_ISO_RESUME, &data->flags)) {
Oliver Neukum8efdd0c2011-02-11 13:00:06 +01001373 err = usb_autopm_get_interface(data->isoc ? data->isoc : data->intf);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001374 if (err < 0) {
1375 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1376 usb_kill_anchored_urbs(&data->isoc_anchor);
1377 return;
1378 }
1379
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -03001380 set_bit(BTUSB_DID_ISO_RESUME, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001381 }
Mikel Astizf4001d22012-04-11 08:48:51 +02001382
1383 if (hdev->voice_setting & 0x0020) {
1384 static const int alts[3] = { 2, 4, 5 };
Marcel Holtmann89e75332014-09-16 04:44:50 +02001385
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001386 new_alts = alts[data->sco_num - 1];
Mikel Astizf4001d22012-04-11 08:48:51 +02001387 } else {
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001388 new_alts = data->sco_num;
Mikel Astizf4001d22012-04-11 08:48:51 +02001389 }
1390
1391 if (data->isoc_altsetting != new_alts) {
Kuba Pawlakf6fc86f2015-10-28 15:18:05 +01001392 unsigned long flags;
1393
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001394 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1395 usb_kill_anchored_urbs(&data->isoc_anchor);
1396
Kuba Pawlak8f9d02f2015-09-10 17:07:00 +01001397 /* When isochronous alternate setting needs to be
1398 * changed, because SCO connection has been added
1399 * or removed, a packet fragment may be left in the
1400 * reassembling state. This could lead to wrongly
1401 * assembled fragments.
1402 *
1403 * Clear outstanding fragment when selecting a new
1404 * alternate setting.
1405 */
Kuba Pawlakf6fc86f2015-10-28 15:18:05 +01001406 spin_lock_irqsave(&data->rxlock, flags);
Kuba Pawlak8f9d02f2015-09-10 17:07:00 +01001407 kfree_skb(data->sco_skb);
1408 data->sco_skb = NULL;
Kuba Pawlakf6fc86f2015-10-28 15:18:05 +01001409 spin_unlock_irqrestore(&data->rxlock, flags);
Kuba Pawlak8f9d02f2015-09-10 17:07:00 +01001410
Mikel Astizf4001d22012-04-11 08:48:51 +02001411 if (__set_isoc_interface(hdev, new_alts) < 0)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001412 return;
1413 }
1414
1415 if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001416 if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001417 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1418 else
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001419 btusb_submit_isoc_urb(hdev, GFP_KERNEL);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001420 }
1421 } else {
1422 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1423 usb_kill_anchored_urbs(&data->isoc_anchor);
1424
1425 __set_isoc_interface(hdev, 0);
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -03001426 if (test_and_clear_bit(BTUSB_DID_ISO_RESUME, &data->flags))
Oliver Neukum8efdd0c2011-02-11 13:00:06 +01001427 usb_autopm_put_interface(data->isoc ? data->isoc : data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001428 }
1429}
1430
Oliver Neukum7bee5492009-08-24 23:44:59 +02001431static void btusb_waker(struct work_struct *work)
1432{
1433 struct btusb_data *data = container_of(work, struct btusb_data, waker);
1434 int err;
1435
1436 err = usb_autopm_get_interface(data->intf);
1437 if (err < 0)
1438 return;
1439
1440 usb_autopm_put_interface(data->intf);
1441}
1442
Marcel Holtmann9f8f9622013-04-10 08:11:35 -07001443static int btusb_setup_bcm92035(struct hci_dev *hdev)
1444{
1445 struct sk_buff *skb;
1446 u8 val = 0x00;
1447
1448 BT_DBG("%s", hdev->name);
1449
1450 skb = __hci_cmd_sync(hdev, 0xfc3b, 1, &val, HCI_INIT_TIMEOUT);
1451 if (IS_ERR(skb))
1452 BT_ERR("BCM92035 command failed (%ld)", -PTR_ERR(skb));
1453 else
1454 kfree_skb(skb);
1455
1456 return 0;
1457}
1458
Marcel Holtmann81cac642014-01-03 03:02:36 -08001459static int btusb_setup_csr(struct hci_dev *hdev)
1460{
1461 struct hci_rp_read_local_version *rp;
1462 struct sk_buff *skb;
Marcel Holtmann81cac642014-01-03 03:02:36 -08001463
1464 BT_DBG("%s", hdev->name);
1465
Marcel Holtmann7cd84d72015-06-07 10:01:02 +02001466 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
1467 HCI_INIT_TIMEOUT);
1468 if (IS_ERR(skb)) {
1469 int err = PTR_ERR(skb);
1470 BT_ERR("%s: CSR: Local version failed (%d)", hdev->name, err);
1471 return err;
1472 }
1473
1474 if (skb->len != sizeof(struct hci_rp_read_local_version)) {
1475 BT_ERR("%s: CSR: Local version length mismatch", hdev->name);
1476 kfree_skb(skb);
1477 return -EIO;
1478 }
Marcel Holtmann81cac642014-01-03 03:02:36 -08001479
Marcel Holtmann89e75332014-09-16 04:44:50 +02001480 rp = (struct hci_rp_read_local_version *)skb->data;
Marcel Holtmann81cac642014-01-03 03:02:36 -08001481
Johan Hedberg6cafcd92015-08-30 21:47:21 +03001482 /* Detect controllers which aren't real CSR ones. */
1483 if (le16_to_cpu(rp->manufacturer) != 10 ||
1484 le16_to_cpu(rp->lmp_subver) == 0x0c5c) {
Marcel Holtmann9641d342015-06-07 10:01:01 +02001485 /* Clear the reset quirk since this is not an actual
1486 * early Bluetooth 1.1 device from CSR.
1487 */
1488 clear_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmann81cac642014-01-03 03:02:36 -08001489
Marcel Holtmann9641d342015-06-07 10:01:01 +02001490 /* These fake CSR controllers have all a broken
1491 * stored link key handling and so just disable it.
1492 */
1493 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
Marcel Holtmann81cac642014-01-03 03:02:36 -08001494 }
1495
Marcel Holtmann81cac642014-01-03 03:02:36 -08001496 kfree_skb(skb);
1497
Marcel Holtmann9641d342015-06-07 10:01:01 +02001498 return 0;
Marcel Holtmann81cac642014-01-03 03:02:36 -08001499}
1500
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001501static const struct firmware *btusb_setup_intel_get_fw(struct hci_dev *hdev,
Marcel Holtmann89e75332014-09-16 04:44:50 +02001502 struct intel_version *ver)
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001503{
1504 const struct firmware *fw;
1505 char fwname[64];
1506 int ret;
1507
1508 snprintf(fwname, sizeof(fwname),
1509 "intel/ibt-hw-%x.%x.%x-fw-%x.%x.%x.%x.%x.bseq",
1510 ver->hw_platform, ver->hw_variant, ver->hw_revision,
1511 ver->fw_variant, ver->fw_revision, ver->fw_build_num,
1512 ver->fw_build_ww, ver->fw_build_yy);
1513
1514 ret = request_firmware(&fw, fwname, &hdev->dev);
1515 if (ret < 0) {
1516 if (ret == -EINVAL) {
1517 BT_ERR("%s Intel firmware file request failed (%d)",
1518 hdev->name, ret);
1519 return NULL;
1520 }
1521
1522 BT_ERR("%s failed to open Intel firmware file: %s(%d)",
1523 hdev->name, fwname, ret);
1524
1525 /* If the correct firmware patch file is not found, use the
1526 * default firmware patch file instead
1527 */
1528 snprintf(fwname, sizeof(fwname), "intel/ibt-hw-%x.%x.bseq",
1529 ver->hw_platform, ver->hw_variant);
1530 if (request_firmware(&fw, fwname, &hdev->dev) < 0) {
1531 BT_ERR("%s failed to open default Intel fw file: %s",
1532 hdev->name, fwname);
1533 return NULL;
1534 }
1535 }
1536
1537 BT_INFO("%s: Intel Bluetooth firmware file: %s", hdev->name, fwname);
1538
1539 return fw;
1540}
1541
1542static int btusb_setup_intel_patching(struct hci_dev *hdev,
1543 const struct firmware *fw,
1544 const u8 **fw_ptr, int *disable_patch)
1545{
1546 struct sk_buff *skb;
1547 struct hci_command_hdr *cmd;
1548 const u8 *cmd_param;
1549 struct hci_event_hdr *evt = NULL;
1550 const u8 *evt_param = NULL;
1551 int remain = fw->size - (*fw_ptr - fw->data);
1552
1553 /* The first byte indicates the types of the patch command or event.
1554 * 0x01 means HCI command and 0x02 is HCI event. If the first bytes
1555 * in the current firmware buffer doesn't start with 0x01 or
1556 * the size of remain buffer is smaller than HCI command header,
1557 * the firmware file is corrupted and it should stop the patching
1558 * process.
1559 */
1560 if (remain > HCI_COMMAND_HDR_SIZE && *fw_ptr[0] != 0x01) {
1561 BT_ERR("%s Intel fw corrupted: invalid cmd read", hdev->name);
1562 return -EINVAL;
1563 }
1564 (*fw_ptr)++;
1565 remain--;
1566
1567 cmd = (struct hci_command_hdr *)(*fw_ptr);
1568 *fw_ptr += sizeof(*cmd);
1569 remain -= sizeof(*cmd);
1570
1571 /* Ensure that the remain firmware data is long enough than the length
1572 * of command parameter. If not, the firmware file is corrupted.
1573 */
1574 if (remain < cmd->plen) {
1575 BT_ERR("%s Intel fw corrupted: invalid cmd len", hdev->name);
1576 return -EFAULT;
1577 }
1578
1579 /* If there is a command that loads a patch in the firmware
1580 * file, then enable the patch upon success, otherwise just
1581 * disable the manufacturer mode, for example patch activation
1582 * is not required when the default firmware patch file is used
1583 * because there are no patch data to load.
1584 */
1585 if (*disable_patch && le16_to_cpu(cmd->opcode) == 0xfc8e)
1586 *disable_patch = 0;
1587
1588 cmd_param = *fw_ptr;
1589 *fw_ptr += cmd->plen;
1590 remain -= cmd->plen;
1591
1592 /* This reads the expected events when the above command is sent to the
1593 * device. Some vendor commands expects more than one events, for
1594 * example command status event followed by vendor specific event.
1595 * For this case, it only keeps the last expected event. so the command
1596 * can be sent with __hci_cmd_sync_ev() which returns the sk_buff of
1597 * last expected event.
1598 */
1599 while (remain > HCI_EVENT_HDR_SIZE && *fw_ptr[0] == 0x02) {
1600 (*fw_ptr)++;
1601 remain--;
1602
1603 evt = (struct hci_event_hdr *)(*fw_ptr);
1604 *fw_ptr += sizeof(*evt);
1605 remain -= sizeof(*evt);
1606
1607 if (remain < evt->plen) {
1608 BT_ERR("%s Intel fw corrupted: invalid evt len",
1609 hdev->name);
1610 return -EFAULT;
1611 }
1612
1613 evt_param = *fw_ptr;
1614 *fw_ptr += evt->plen;
1615 remain -= evt->plen;
1616 }
1617
1618 /* Every HCI commands in the firmware file has its correspond event.
1619 * If event is not found or remain is smaller than zero, the firmware
1620 * file is corrupted.
1621 */
1622 if (!evt || !evt_param || remain < 0) {
1623 BT_ERR("%s Intel fw corrupted: invalid evt read", hdev->name);
1624 return -EFAULT;
1625 }
1626
1627 skb = __hci_cmd_sync_ev(hdev, le16_to_cpu(cmd->opcode), cmd->plen,
1628 cmd_param, evt->evt, HCI_INIT_TIMEOUT);
1629 if (IS_ERR(skb)) {
1630 BT_ERR("%s sending Intel patch command (0x%4.4x) failed (%ld)",
1631 hdev->name, cmd->opcode, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001632 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001633 }
1634
1635 /* It ensures that the returned event matches the event data read from
1636 * the firmware file. At fist, it checks the length and then
1637 * the contents of the event.
1638 */
1639 if (skb->len != evt->plen) {
1640 BT_ERR("%s mismatch event length (opcode 0x%4.4x)", hdev->name,
1641 le16_to_cpu(cmd->opcode));
1642 kfree_skb(skb);
1643 return -EFAULT;
1644 }
1645
1646 if (memcmp(skb->data, evt_param, evt->plen)) {
1647 BT_ERR("%s mismatch event parameter (opcode 0x%4.4x)",
1648 hdev->name, le16_to_cpu(cmd->opcode));
1649 kfree_skb(skb);
1650 return -EFAULT;
1651 }
1652 kfree_skb(skb);
1653
1654 return 0;
1655}
1656
1657static int btusb_setup_intel(struct hci_dev *hdev)
1658{
1659 struct sk_buff *skb;
1660 const struct firmware *fw;
1661 const u8 *fw_ptr;
Loic Poulain28dc4b92015-12-03 16:10:22 +01001662 int disable_patch, err;
Loic Poulain6c483de2015-12-06 16:18:34 +01001663 struct intel_version ver;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001664
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001665 BT_DBG("%s", hdev->name);
1666
1667 /* The controller has a bug with the first HCI command sent to it
1668 * returning number of completed commands as zero. This would stall the
1669 * command processing in the Bluetooth core.
1670 *
1671 * As a workaround, send HCI Reset command first which will reset the
1672 * number of completed commands and allow normal command processing
1673 * from now on.
1674 */
1675 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
1676 if (IS_ERR(skb)) {
1677 BT_ERR("%s sending initial HCI reset command failed (%ld)",
1678 hdev->name, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001679 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001680 }
1681 kfree_skb(skb);
1682
1683 /* Read Intel specific controller version first to allow selection of
1684 * which firmware file to load.
1685 *
1686 * The returned information are hardware variant and revision plus
1687 * firmware variant, revision and build number.
1688 */
Loic Poulain6c483de2015-12-06 16:18:34 +01001689 err = btintel_read_version(hdev, &ver);
1690 if (err)
1691 return err;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001692
1693 BT_INFO("%s: read Intel version: %02x%02x%02x%02x%02x%02x%02x%02x%02x",
Loic Poulain6c483de2015-12-06 16:18:34 +01001694 hdev->name, ver.hw_platform, ver.hw_variant, ver.hw_revision,
1695 ver.fw_variant, ver.fw_revision, ver.fw_build_num,
1696 ver.fw_build_ww, ver.fw_build_yy, ver.fw_patch_num);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001697
1698 /* fw_patch_num indicates the version of patch the device currently
1699 * have. If there is no patch data in the device, it is always 0x00.
Minjune Kim5075eda2015-08-27 13:21:52 +09001700 * So, if it is other than 0x00, no need to patch the device again.
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001701 */
Loic Poulain6c483de2015-12-06 16:18:34 +01001702 if (ver.fw_patch_num) {
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001703 BT_INFO("%s: Intel device is already patched. patch num: %02x",
Loic Poulain6c483de2015-12-06 16:18:34 +01001704 hdev->name, ver.fw_patch_num);
Marcel Holtmann213445b2015-10-21 02:45:19 +02001705 goto complete;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001706 }
1707
1708 /* Opens the firmware patch file based on the firmware version read
1709 * from the controller. If it fails to open the matching firmware
1710 * patch file, it tries to open the default firmware patch file.
1711 * If no patch file is found, allow the device to operate without
1712 * a patch.
1713 */
Loic Poulain6c483de2015-12-06 16:18:34 +01001714 fw = btusb_setup_intel_get_fw(hdev, &ver);
1715 if (!fw)
Marcel Holtmann213445b2015-10-21 02:45:19 +02001716 goto complete;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001717 fw_ptr = fw->data;
1718
Loic Poulain28dc4b92015-12-03 16:10:22 +01001719 /* Enable the manufacturer mode of the controller.
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001720 * Only while this mode is enabled, the driver can download the
1721 * firmware patch data and configuration parameters.
1722 */
Loic Poulain28dc4b92015-12-03 16:10:22 +01001723 err = btintel_enter_mfg(hdev);
1724 if (err) {
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001725 release_firmware(fw);
Loic Poulain28dc4b92015-12-03 16:10:22 +01001726 return err;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001727 }
1728
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001729 disable_patch = 1;
1730
1731 /* The firmware data file consists of list of Intel specific HCI
1732 * commands and its expected events. The first byte indicates the
1733 * type of the message, either HCI command or HCI event.
1734 *
1735 * It reads the command and its expected event from the firmware file,
1736 * and send to the controller. Once __hci_cmd_sync_ev() returns,
1737 * the returned event is compared with the event read from the firmware
1738 * file and it will continue until all the messages are downloaded to
1739 * the controller.
1740 *
1741 * Once the firmware patching is completed successfully,
1742 * the manufacturer mode is disabled with reset and activating the
1743 * downloaded patch.
1744 *
1745 * If the firmware patching fails, the manufacturer mode is
1746 * disabled with reset and deactivating the patch.
1747 *
1748 * If the default patch file is used, no reset is done when disabling
1749 * the manufacturer.
1750 */
1751 while (fw->size > fw_ptr - fw->data) {
1752 int ret;
1753
1754 ret = btusb_setup_intel_patching(hdev, fw, &fw_ptr,
1755 &disable_patch);
1756 if (ret < 0)
1757 goto exit_mfg_deactivate;
1758 }
1759
1760 release_firmware(fw);
1761
1762 if (disable_patch)
1763 goto exit_mfg_disable;
1764
1765 /* Patching completed successfully and disable the manufacturer mode
1766 * with reset and activate the downloaded firmware patches.
1767 */
Loic Poulain28dc4b92015-12-03 16:10:22 +01001768 err = btintel_exit_mfg(hdev, true, true);
1769 if (err)
1770 return err;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001771
1772 BT_INFO("%s: Intel Bluetooth firmware patch completed and activated",
1773 hdev->name);
1774
Marcel Holtmann213445b2015-10-21 02:45:19 +02001775 goto complete;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001776
1777exit_mfg_disable:
1778 /* Disable the manufacturer mode without reset */
Loic Poulain28dc4b92015-12-03 16:10:22 +01001779 err = btintel_exit_mfg(hdev, false, false);
1780 if (err)
1781 return err;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001782
1783 BT_INFO("%s: Intel Bluetooth firmware patch completed", hdev->name);
Marcel Holtmann40cb0982014-07-02 12:06:45 +02001784
Marcel Holtmann213445b2015-10-21 02:45:19 +02001785 goto complete;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001786
1787exit_mfg_deactivate:
1788 release_firmware(fw);
1789
1790 /* Patching failed. Disable the manufacturer mode with reset and
1791 * deactivate the downloaded firmware patches.
1792 */
Loic Poulain28dc4b92015-12-03 16:10:22 +01001793 err = btintel_exit_mfg(hdev, true, false);
1794 if (err)
1795 return err;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001796
1797 BT_INFO("%s: Intel Bluetooth firmware patch completed and deactivated",
1798 hdev->name);
1799
Marcel Holtmann213445b2015-10-21 02:45:19 +02001800complete:
1801 /* Set the event mask for Intel specific vendor events. This enables
1802 * a few extra events that are useful during general operation.
1803 */
1804 btintel_set_event_mask_mfg(hdev, false);
1805
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07001806 btintel_check_bdaddr(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001807 return 0;
1808}
1809
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001810static int inject_cmd_complete(struct hci_dev *hdev, __u16 opcode)
1811{
1812 struct sk_buff *skb;
1813 struct hci_event_hdr *hdr;
1814 struct hci_ev_cmd_complete *evt;
1815
1816 skb = bt_skb_alloc(sizeof(*hdr) + sizeof(*evt) + 1, GFP_ATOMIC);
1817 if (!skb)
1818 return -ENOMEM;
1819
1820 hdr = (struct hci_event_hdr *)skb_put(skb, sizeof(*hdr));
1821 hdr->evt = HCI_EV_CMD_COMPLETE;
1822 hdr->plen = sizeof(*evt) + 1;
1823
1824 evt = (struct hci_ev_cmd_complete *)skb_put(skb, sizeof(*evt));
1825 evt->ncmd = 0x01;
1826 evt->opcode = cpu_to_le16(opcode);
1827
1828 *skb_put(skb, 1) = 0x00;
1829
Marcel Holtmann618e8bc2015-11-05 07:33:56 +01001830 hci_skb_pkt_type(skb) = HCI_EVENT_PKT;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001831
1832 return hci_recv_frame(hdev, skb);
1833}
1834
1835static int btusb_recv_bulk_intel(struct btusb_data *data, void *buffer,
1836 int count)
1837{
1838 /* When the device is in bootloader mode, then it can send
1839 * events via the bulk endpoint. These events are treated the
1840 * same way as the ones received from the interrupt endpoint.
1841 */
1842 if (test_bit(BTUSB_BOOTLOADER, &data->flags))
1843 return btusb_recv_intr(data, buffer, count);
1844
1845 return btusb_recv_bulk(data, buffer, count);
1846}
1847
Marcel Holtmannccd6da22015-04-09 00:35:19 -07001848static void btusb_intel_bootup(struct btusb_data *data, const void *ptr,
1849 unsigned int len)
1850{
1851 const struct intel_bootup *evt = ptr;
1852
1853 if (len != sizeof(*evt))
1854 return;
1855
1856 if (test_and_clear_bit(BTUSB_BOOTING, &data->flags)) {
1857 smp_mb__after_atomic();
1858 wake_up_bit(&data->flags, BTUSB_BOOTING);
1859 }
1860}
1861
1862static void btusb_intel_secure_send_result(struct btusb_data *data,
1863 const void *ptr, unsigned int len)
1864{
1865 const struct intel_secure_send_result *evt = ptr;
1866
1867 if (len != sizeof(*evt))
1868 return;
1869
1870 if (evt->result)
1871 set_bit(BTUSB_FIRMWARE_FAILED, &data->flags);
1872
1873 if (test_and_clear_bit(BTUSB_DOWNLOADING, &data->flags) &&
1874 test_bit(BTUSB_FIRMWARE_LOADED, &data->flags)) {
1875 smp_mb__after_atomic();
1876 wake_up_bit(&data->flags, BTUSB_DOWNLOADING);
1877 }
1878}
1879
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001880static int btusb_recv_event_intel(struct hci_dev *hdev, struct sk_buff *skb)
1881{
1882 struct btusb_data *data = hci_get_drvdata(hdev);
1883
1884 if (test_bit(BTUSB_BOOTLOADER, &data->flags)) {
1885 struct hci_event_hdr *hdr = (void *)skb->data;
1886
Marcel Holtmannccd6da22015-04-09 00:35:19 -07001887 if (skb->len > HCI_EVENT_HDR_SIZE && hdr->evt == 0xff &&
1888 hdr->plen > 0) {
1889 const void *ptr = skb->data + HCI_EVENT_HDR_SIZE + 1;
1890 unsigned int len = skb->len - HCI_EVENT_HDR_SIZE - 1;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001891
Marcel Holtmannccd6da22015-04-09 00:35:19 -07001892 switch (skb->data[2]) {
1893 case 0x02:
1894 /* When switching to the operational firmware
1895 * the device sends a vendor specific event
1896 * indicating that the bootup completed.
1897 */
1898 btusb_intel_bootup(data, ptr, len);
1899 break;
1900 case 0x06:
1901 /* When the firmware loading completes the
1902 * device sends out a vendor specific event
1903 * indicating the result of the firmware
1904 * loading.
1905 */
1906 btusb_intel_secure_send_result(data, ptr, len);
1907 break;
Johan Hedbergfad70972015-01-30 10:58:55 +02001908 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001909 }
1910 }
1911
1912 return hci_recv_frame(hdev, skb);
1913}
1914
1915static int btusb_send_frame_intel(struct hci_dev *hdev, struct sk_buff *skb)
1916{
1917 struct btusb_data *data = hci_get_drvdata(hdev);
1918 struct urb *urb;
1919
1920 BT_DBG("%s", hdev->name);
1921
Marcel Holtmann618e8bc2015-11-05 07:33:56 +01001922 switch (hci_skb_pkt_type(skb)) {
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001923 case HCI_COMMAND_PKT:
1924 if (test_bit(BTUSB_BOOTLOADER, &data->flags)) {
1925 struct hci_command_hdr *cmd = (void *)skb->data;
1926 __u16 opcode = le16_to_cpu(cmd->opcode);
1927
1928 /* When in bootloader mode and the command 0xfc09
1929 * is received, it needs to be send down the
1930 * bulk endpoint. So allocate a bulk URB instead.
1931 */
1932 if (opcode == 0xfc09)
1933 urb = alloc_bulk_urb(hdev, skb);
1934 else
1935 urb = alloc_ctrl_urb(hdev, skb);
1936
1937 /* When the 0xfc01 command is issued to boot into
1938 * the operational firmware, it will actually not
1939 * send a command complete event. To keep the flow
1940 * control working inject that event here.
1941 */
1942 if (opcode == 0xfc01)
1943 inject_cmd_complete(hdev, opcode);
1944 } else {
1945 urb = alloc_ctrl_urb(hdev, skb);
1946 }
1947 if (IS_ERR(urb))
1948 return PTR_ERR(urb);
1949
1950 hdev->stat.cmd_tx++;
1951 return submit_or_queue_tx_urb(hdev, urb);
1952
1953 case HCI_ACLDATA_PKT:
1954 urb = alloc_bulk_urb(hdev, skb);
1955 if (IS_ERR(urb))
1956 return PTR_ERR(urb);
1957
1958 hdev->stat.acl_tx++;
1959 return submit_or_queue_tx_urb(hdev, urb);
1960
1961 case HCI_SCODATA_PKT:
1962 if (hci_conn_num(hdev, SCO_LINK) < 1)
1963 return -ENODEV;
1964
1965 urb = alloc_isoc_urb(hdev, skb);
1966 if (IS_ERR(urb))
1967 return PTR_ERR(urb);
1968
1969 hdev->stat.sco_tx++;
1970 return submit_tx_urb(hdev, urb);
1971 }
1972
1973 return -EILSEQ;
1974}
1975
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001976static int btusb_setup_intel_new(struct hci_dev *hdev)
1977{
1978 static const u8 reset_param[] = { 0x00, 0x01, 0x00, 0x01,
1979 0x00, 0x08, 0x04, 0x00 };
1980 struct btusb_data *data = hci_get_drvdata(hdev);
1981 struct sk_buff *skb;
Loic Poulain6c483de2015-12-06 16:18:34 +01001982 struct intel_version ver;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001983 struct intel_boot_params *params;
1984 const struct firmware *fw;
1985 const u8 *fw_ptr;
Marcel Holtmanne66890a92015-06-07 09:47:08 +02001986 u32 frag_len;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001987 char fwname[64];
1988 ktime_t calltime, delta, rettime;
1989 unsigned long long duration;
1990 int err;
1991
1992 BT_DBG("%s", hdev->name);
1993
1994 calltime = ktime_get();
1995
1996 /* Read the Intel version information to determine if the device
1997 * is in bootloader mode or if it already has operational firmware
1998 * loaded.
1999 */
Loic Poulain6c483de2015-12-06 16:18:34 +01002000 err = btintel_read_version(hdev, &ver);
2001 if (err)
2002 return err;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002003
2004 /* The hardware platform number has a fixed value of 0x37 and
2005 * for now only accept this single value.
2006 */
Loic Poulain6c483de2015-12-06 16:18:34 +01002007 if (ver.hw_platform != 0x37) {
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002008 BT_ERR("%s: Unsupported Intel hardware platform (%u)",
Loic Poulain6c483de2015-12-06 16:18:34 +01002009 hdev->name, ver.hw_platform);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002010 return -EINVAL;
2011 }
2012
Tedd Ho-Jeong Ana0af53b2016-05-06 11:53:46 -07002013 /* At the moment the iBT 3.0 hardware variants 0x0b (LnP/SfP)
2014 * and 0x0c (WsP) are supported by this firmware loading method.
2015 *
2016 * This check has been put in place to ensure correct forward
2017 * compatibility options when newer hardware variants come along.
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002018 */
Tedd Ho-Jeong Ana0af53b2016-05-06 11:53:46 -07002019 if (ver.hw_variant != 0x0b && ver.hw_variant != 0x0c) {
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002020 BT_ERR("%s: Unsupported Intel hardware variant (%u)",
Loic Poulain6c483de2015-12-06 16:18:34 +01002021 hdev->name, ver.hw_variant);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002022 return -EINVAL;
2023 }
2024
Loic Poulain6c483de2015-12-06 16:18:34 +01002025 btintel_version_info(hdev, &ver);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002026
2027 /* The firmware variant determines if the device is in bootloader
2028 * mode or is running operational firmware. The value 0x06 identifies
2029 * the bootloader and the value 0x23 identifies the operational
2030 * firmware.
2031 *
2032 * When the operational firmware is already present, then only
2033 * the check for valid Bluetooth device address is needed. This
2034 * determines if the device will be added as configured or
2035 * unconfigured controller.
2036 *
2037 * It is not possible to use the Secure Boot Parameters in this
2038 * case since that command is only available in bootloader mode.
2039 */
Loic Poulain6c483de2015-12-06 16:18:34 +01002040 if (ver.fw_variant == 0x23) {
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002041 clear_bit(BTUSB_BOOTLOADER, &data->flags);
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07002042 btintel_check_bdaddr(hdev);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002043 return 0;
2044 }
2045
2046 /* If the device is not in bootloader mode, then the only possible
2047 * choice is to return an error and abort the device initialization.
2048 */
Loic Poulain6c483de2015-12-06 16:18:34 +01002049 if (ver.fw_variant != 0x06) {
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002050 BT_ERR("%s: Unsupported Intel firmware variant (%u)",
Loic Poulain6c483de2015-12-06 16:18:34 +01002051 hdev->name, ver.fw_variant);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002052 return -ENODEV;
2053 }
2054
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002055 /* Read the secure boot parameters to identify the operating
2056 * details of the bootloader.
2057 */
2058 skb = __hci_cmd_sync(hdev, 0xfc0d, 0, NULL, HCI_INIT_TIMEOUT);
2059 if (IS_ERR(skb)) {
2060 BT_ERR("%s: Reading Intel boot parameters failed (%ld)",
2061 hdev->name, PTR_ERR(skb));
2062 return PTR_ERR(skb);
2063 }
2064
2065 if (skb->len != sizeof(*params)) {
2066 BT_ERR("%s: Intel boot parameters size mismatch", hdev->name);
2067 kfree_skb(skb);
2068 return -EILSEQ;
2069 }
2070
2071 params = (struct intel_boot_params *)skb->data;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002072
2073 BT_INFO("%s: Device revision is %u", hdev->name,
2074 le16_to_cpu(params->dev_revid));
2075
2076 BT_INFO("%s: Secure boot is %s", hdev->name,
2077 params->secure_boot ? "enabled" : "disabled");
2078
Marcel Holtmann2220994e2015-10-13 13:54:55 +02002079 BT_INFO("%s: OTP lock is %s", hdev->name,
2080 params->otp_lock ? "enabled" : "disabled");
2081
2082 BT_INFO("%s: API lock is %s", hdev->name,
2083 params->api_lock ? "enabled" : "disabled");
2084
2085 BT_INFO("%s: Debug lock is %s", hdev->name,
2086 params->debug_lock ? "enabled" : "disabled");
2087
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002088 BT_INFO("%s: Minimum firmware build %u week %u %u", hdev->name,
2089 params->min_fw_build_nn, params->min_fw_build_cw,
2090 2000 + params->min_fw_build_yy);
2091
2092 /* It is required that every single firmware fragment is acknowledged
2093 * with a command complete event. If the boot parameters indicate
2094 * that this bootloader does not send them, then abort the setup.
2095 */
2096 if (params->limited_cce != 0x00) {
2097 BT_ERR("%s: Unsupported Intel firmware loading method (%u)",
2098 hdev->name, params->limited_cce);
2099 kfree_skb(skb);
2100 return -EINVAL;
2101 }
2102
2103 /* If the OTP has no valid Bluetooth device address, then there will
2104 * also be no valid address for the operational firmware.
2105 */
2106 if (!bacmp(&params->otp_bdaddr, BDADDR_ANY)) {
2107 BT_INFO("%s: No device address configured", hdev->name);
2108 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
2109 }
2110
2111 /* With this Intel bootloader only the hardware variant and device
2112 * revision information are used to select the right firmware.
2113 *
Tedd Ho-Jeong An230b04a2016-06-28 08:56:39 -07002114 * The firmware filename is ibt-<hw_variant>-<dev_revid>.sfi.
2115 *
2116 * Currently the supported hardware variants are:
2117 * 11 (0x0b) for iBT3.0 (LnP/SfP)
2118 * 12 (0x0c) for iBT3.5 (WsP)
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002119 */
Tedd Ho-Jeong An230b04a2016-06-28 08:56:39 -07002120 snprintf(fwname, sizeof(fwname), "intel/ibt-%u-%u.sfi",
2121 le16_to_cpu(ver.hw_variant),
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002122 le16_to_cpu(params->dev_revid));
2123
2124 err = request_firmware(&fw, fwname, &hdev->dev);
2125 if (err < 0) {
2126 BT_ERR("%s: Failed to load Intel firmware file (%d)",
2127 hdev->name, err);
2128 kfree_skb(skb);
2129 return err;
2130 }
2131
2132 BT_INFO("%s: Found device firmware: %s", hdev->name, fwname);
2133
Tedd Ho-Jeong An52cc9162015-06-12 16:20:05 -07002134 /* Save the DDC file name for later use to apply once the firmware
2135 * downloading is done.
2136 */
Tedd Ho-Jeong An230b04a2016-06-28 08:56:39 -07002137 snprintf(fwname, sizeof(fwname), "intel/ibt-%u-%u.ddc",
2138 le16_to_cpu(ver.hw_variant),
Tedd Ho-Jeong An52cc9162015-06-12 16:20:05 -07002139 le16_to_cpu(params->dev_revid));
2140
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002141 kfree_skb(skb);
2142
2143 if (fw->size < 644) {
2144 BT_ERR("%s: Invalid size of firmware file (%zu)",
2145 hdev->name, fw->size);
2146 err = -EBADF;
2147 goto done;
2148 }
2149
2150 set_bit(BTUSB_DOWNLOADING, &data->flags);
2151
2152 /* Start the firmware download transaction with the Init fragment
2153 * represented by the 128 bytes of CSS header.
2154 */
Marcel Holtmann09df1232015-07-05 14:55:36 +02002155 err = btintel_secure_send(hdev, 0x00, 128, fw->data);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002156 if (err < 0) {
2157 BT_ERR("%s: Failed to send firmware header (%d)",
2158 hdev->name, err);
2159 goto done;
2160 }
2161
2162 /* Send the 256 bytes of public key information from the firmware
2163 * as the PKey fragment.
2164 */
Marcel Holtmann09df1232015-07-05 14:55:36 +02002165 err = btintel_secure_send(hdev, 0x03, 256, fw->data + 128);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002166 if (err < 0) {
2167 BT_ERR("%s: Failed to send firmware public key (%d)",
2168 hdev->name, err);
2169 goto done;
2170 }
2171
2172 /* Send the 256 bytes of signature information from the firmware
2173 * as the Sign fragment.
2174 */
Marcel Holtmann09df1232015-07-05 14:55:36 +02002175 err = btintel_secure_send(hdev, 0x02, 256, fw->data + 388);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002176 if (err < 0) {
2177 BT_ERR("%s: Failed to send firmware signature (%d)",
2178 hdev->name, err);
2179 goto done;
2180 }
2181
2182 fw_ptr = fw->data + 644;
Marcel Holtmanne66890a92015-06-07 09:47:08 +02002183 frag_len = 0;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002184
2185 while (fw_ptr - fw->data < fw->size) {
Marcel Holtmanne66890a92015-06-07 09:47:08 +02002186 struct hci_command_hdr *cmd = (void *)(fw_ptr + frag_len);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002187
Marcel Holtmanne66890a92015-06-07 09:47:08 +02002188 frag_len += sizeof(*cmd) + cmd->plen;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002189
Minjune Kim5075eda2015-08-27 13:21:52 +09002190 /* The parameter length of the secure send command requires
Marcel Holtmanne66890a92015-06-07 09:47:08 +02002191 * a 4 byte alignment. It happens so that the firmware file
2192 * contains proper Intel_NOP commands to align the fragments
2193 * as needed.
2194 *
2195 * Send set of commands with 4 byte alignment from the
2196 * firmware data buffer as a single Data fragement.
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002197 */
Marcel Holtmanne66890a92015-06-07 09:47:08 +02002198 if (!(frag_len % 4)) {
Marcel Holtmann09df1232015-07-05 14:55:36 +02002199 err = btintel_secure_send(hdev, 0x01, frag_len, fw_ptr);
Marcel Holtmanne66890a92015-06-07 09:47:08 +02002200 if (err < 0) {
2201 BT_ERR("%s: Failed to send firmware data (%d)",
2202 hdev->name, err);
2203 goto done;
2204 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002205
Marcel Holtmanne66890a92015-06-07 09:47:08 +02002206 fw_ptr += frag_len;
2207 frag_len = 0;
2208 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002209 }
2210
Marcel Holtmannce6bb922015-01-28 01:58:40 -08002211 set_bit(BTUSB_FIRMWARE_LOADED, &data->flags);
2212
Johan Hedberga087a982015-01-30 10:58:54 +02002213 BT_INFO("%s: Waiting for firmware download to complete", hdev->name);
2214
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002215 /* Before switching the device into operational mode and with that
2216 * booting the loaded firmware, wait for the bootloader notification
2217 * that all fragments have been successfully received.
2218 *
Johan Hedberga087a982015-01-30 10:58:54 +02002219 * When the event processing receives the notification, then the
2220 * BTUSB_DOWNLOADING flag will be cleared.
2221 *
2222 * The firmware loading should not take longer than 5 seconds
2223 * and thus just timeout if that happens and fail the setup
2224 * of this device.
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002225 */
Johan Hedberg129a7692015-02-14 09:33:35 +02002226 err = wait_on_bit_timeout(&data->flags, BTUSB_DOWNLOADING,
2227 TASK_INTERRUPTIBLE,
2228 msecs_to_jiffies(5000));
Bart Van Asschef0a70a02016-08-11 16:02:44 -07002229 if (err == -EINTR) {
Johan Hedberga087a982015-01-30 10:58:54 +02002230 BT_ERR("%s: Firmware loading interrupted", hdev->name);
Johan Hedberga087a982015-01-30 10:58:54 +02002231 goto done;
2232 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002233
Johan Hedberga087a982015-01-30 10:58:54 +02002234 if (err) {
2235 BT_ERR("%s: Firmware loading timeout", hdev->name);
2236 err = -ETIMEDOUT;
2237 goto done;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002238 }
2239
2240 if (test_bit(BTUSB_FIRMWARE_FAILED, &data->flags)) {
2241 BT_ERR("%s: Firmware loading failed", hdev->name);
2242 err = -ENOEXEC;
2243 goto done;
2244 }
2245
2246 rettime = ktime_get();
2247 delta = ktime_sub(rettime, calltime);
2248 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
2249
2250 BT_INFO("%s: Firmware loaded in %llu usecs", hdev->name, duration);
2251
2252done:
2253 release_firmware(fw);
2254
2255 if (err < 0)
2256 return err;
2257
2258 calltime = ktime_get();
2259
2260 set_bit(BTUSB_BOOTING, &data->flags);
2261
2262 skb = __hci_cmd_sync(hdev, 0xfc01, sizeof(reset_param), reset_param,
2263 HCI_INIT_TIMEOUT);
2264 if (IS_ERR(skb))
2265 return PTR_ERR(skb);
2266
2267 kfree_skb(skb);
2268
2269 /* The bootloader will not indicate when the device is ready. This
2270 * is done by the operational firmware sending bootup notification.
Johan Hedbergfad70972015-01-30 10:58:55 +02002271 *
2272 * Booting into operational firmware should not take longer than
2273 * 1 second. However if that happens, then just fail the setup
2274 * since something went wrong.
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002275 */
Johan Hedbergfad70972015-01-30 10:58:55 +02002276 BT_INFO("%s: Waiting for device to boot", hdev->name);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002277
Johan Hedberg129a7692015-02-14 09:33:35 +02002278 err = wait_on_bit_timeout(&data->flags, BTUSB_BOOTING,
2279 TASK_INTERRUPTIBLE,
2280 msecs_to_jiffies(1000));
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002281
Bart Van Asschef0a70a02016-08-11 16:02:44 -07002282 if (err == -EINTR) {
Johan Hedbergfad70972015-01-30 10:58:55 +02002283 BT_ERR("%s: Device boot interrupted", hdev->name);
2284 return -EINTR;
2285 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002286
Johan Hedbergfad70972015-01-30 10:58:55 +02002287 if (err) {
2288 BT_ERR("%s: Device boot timeout", hdev->name);
2289 return -ETIMEDOUT;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002290 }
2291
2292 rettime = ktime_get();
2293 delta = ktime_sub(rettime, calltime);
2294 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
2295
2296 BT_INFO("%s: Device booted in %llu usecs", hdev->name, duration);
2297
2298 clear_bit(BTUSB_BOOTLOADER, &data->flags);
2299
Tedd Ho-Jeong An52cc9162015-06-12 16:20:05 -07002300 /* Once the device is running in operational mode, it needs to apply
2301 * the device configuration (DDC) parameters.
2302 *
2303 * The device can work without DDC parameters, so even if it fails
2304 * to load the file, no need to fail the setup.
2305 */
Loic Poulaine924d3d2015-09-04 17:54:36 +02002306 btintel_load_ddc_config(hdev, fwname);
Tedd Ho-Jeong An52cc9162015-06-12 16:20:05 -07002307
Marcel Holtmann213445b2015-10-21 02:45:19 +02002308 /* Set the event mask for Intel specific vendor events. This enables
2309 * a few extra events that are useful during general operation. It
2310 * does not enable any debugging related events.
2311 *
2312 * The device will function correctly without these events enabled
2313 * and thus no need to fail the setup.
2314 */
2315 btintel_set_event_mask(hdev, false);
2316
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002317 return 0;
2318}
2319
Tedd Ho-Jeong Anbfbd45e2015-02-13 09:20:52 -08002320static int btusb_shutdown_intel(struct hci_dev *hdev)
2321{
2322 struct sk_buff *skb;
2323 long ret;
2324
2325 /* Some platforms have an issue with BT LED when the interface is
2326 * down or BT radio is turned off, which takes 5 seconds to BT LED
2327 * goes off. This command turns off the BT LED immediately.
2328 */
2329 skb = __hci_cmd_sync(hdev, 0xfc3f, 0, NULL, HCI_INIT_TIMEOUT);
2330 if (IS_ERR(skb)) {
2331 ret = PTR_ERR(skb);
2332 BT_ERR("%s: turning off Intel device LED failed (%ld)",
2333 hdev->name, ret);
2334 return ret;
2335 }
2336 kfree_skb(skb);
2337
2338 return 0;
2339}
2340
Amitkumar Karwarae8df492014-07-18 14:47:06 -07002341static int btusb_set_bdaddr_marvell(struct hci_dev *hdev,
2342 const bdaddr_t *bdaddr)
2343{
2344 struct sk_buff *skb;
2345 u8 buf[8];
2346 long ret;
2347
2348 buf[0] = 0xfe;
2349 buf[1] = sizeof(bdaddr_t);
2350 memcpy(buf + 2, bdaddr, sizeof(bdaddr_t));
2351
2352 skb = __hci_cmd_sync(hdev, 0xfc22, sizeof(buf), buf, HCI_INIT_TIMEOUT);
2353 if (IS_ERR(skb)) {
2354 ret = PTR_ERR(skb);
2355 BT_ERR("%s: changing Marvell device address failed (%ld)",
2356 hdev->name, ret);
2357 return ret;
2358 }
2359 kfree_skb(skb);
2360
2361 return 0;
2362}
2363
Toshi Kikuchi58592232014-12-12 10:58:05 -08002364static int btusb_set_bdaddr_ath3012(struct hci_dev *hdev,
2365 const bdaddr_t *bdaddr)
2366{
2367 struct sk_buff *skb;
2368 u8 buf[10];
2369 long ret;
2370
2371 buf[0] = 0x01;
2372 buf[1] = 0x01;
2373 buf[2] = 0x00;
2374 buf[3] = sizeof(bdaddr_t);
2375 memcpy(buf + 4, bdaddr, sizeof(bdaddr_t));
2376
2377 skb = __hci_cmd_sync(hdev, 0xfc0b, sizeof(buf), buf, HCI_INIT_TIMEOUT);
2378 if (IS_ERR(skb)) {
2379 ret = PTR_ERR(skb);
2380 BT_ERR("%s: Change address command failed (%ld)",
2381 hdev->name, ret);
2382 return ret;
2383 }
2384 kfree_skb(skb);
2385
2386 return 0;
2387}
2388
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002389#define QCA_DFU_PACKET_LEN 4096
2390
2391#define QCA_GET_TARGET_VERSION 0x09
2392#define QCA_CHECK_STATUS 0x05
2393#define QCA_DFU_DOWNLOAD 0x01
2394
2395#define QCA_SYSCFG_UPDATED 0x40
2396#define QCA_PATCH_UPDATED 0x80
2397#define QCA_DFU_TIMEOUT 3000
2398
2399struct qca_version {
2400 __le32 rom_version;
2401 __le32 patch_version;
2402 __le32 ram_version;
2403 __le32 ref_clock;
2404 __u8 reserved[4];
2405} __packed;
2406
2407struct qca_rampatch_version {
2408 __le16 rom_version;
2409 __le16 patch_version;
2410} __packed;
2411
2412struct qca_device_info {
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002413 u32 rom_version;
2414 u8 rampatch_hdr; /* length of header in rampatch */
2415 u8 nvm_hdr; /* length of header in NVM */
2416 u8 ver_offset; /* offset of version structure in rampatch */
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002417};
2418
2419static const struct qca_device_info qca_devices_table[] = {
2420 { 0x00000100, 20, 4, 10 }, /* Rome 1.0 */
2421 { 0x00000101, 20, 4, 10 }, /* Rome 1.1 */
Chan-yeol Park7f6e6362015-05-21 11:24:27 +09002422 { 0x00000200, 28, 4, 18 }, /* Rome 2.0 */
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002423 { 0x00000201, 28, 4, 18 }, /* Rome 2.1 */
2424 { 0x00000300, 28, 4, 18 }, /* Rome 3.0 */
2425 { 0x00000302, 28, 4, 18 }, /* Rome 3.2 */
2426};
2427
2428static int btusb_qca_send_vendor_req(struct hci_dev *hdev, u8 request,
2429 void *data, u16 size)
2430{
2431 struct btusb_data *btdata = hci_get_drvdata(hdev);
2432 struct usb_device *udev = btdata->udev;
2433 int pipe, err;
2434 u8 *buf;
2435
2436 buf = kmalloc(size, GFP_KERNEL);
2437 if (!buf)
2438 return -ENOMEM;
2439
2440 /* Found some of USB hosts have IOT issues with ours so that we should
2441 * not wait until HCI layer is ready.
2442 */
2443 pipe = usb_rcvctrlpipe(udev, 0);
2444 err = usb_control_msg(udev, pipe, request, USB_TYPE_VENDOR | USB_DIR_IN,
2445 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
2446 if (err < 0) {
2447 BT_ERR("%s: Failed to access otp area (%d)", hdev->name, err);
2448 goto done;
2449 }
2450
2451 memcpy(data, buf, size);
2452
2453done:
2454 kfree(buf);
2455
2456 return err;
2457}
2458
2459static int btusb_setup_qca_download_fw(struct hci_dev *hdev,
2460 const struct firmware *firmware,
2461 size_t hdr_size)
2462{
2463 struct btusb_data *btdata = hci_get_drvdata(hdev);
2464 struct usb_device *udev = btdata->udev;
2465 size_t count, size, sent = 0;
2466 int pipe, len, err;
2467 u8 *buf;
2468
2469 buf = kmalloc(QCA_DFU_PACKET_LEN, GFP_KERNEL);
2470 if (!buf)
2471 return -ENOMEM;
2472
2473 count = firmware->size;
2474
2475 size = min_t(size_t, count, hdr_size);
2476 memcpy(buf, firmware->data, size);
2477
2478 /* USB patches should go down to controller through USB path
2479 * because binary format fits to go down through USB channel.
2480 * USB control path is for patching headers and USB bulk is for
2481 * patch body.
2482 */
2483 pipe = usb_sndctrlpipe(udev, 0);
2484 err = usb_control_msg(udev, pipe, QCA_DFU_DOWNLOAD, USB_TYPE_VENDOR,
2485 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
2486 if (err < 0) {
2487 BT_ERR("%s: Failed to send headers (%d)", hdev->name, err);
2488 goto done;
2489 }
2490
2491 sent += size;
2492 count -= size;
2493
2494 while (count) {
2495 size = min_t(size_t, count, QCA_DFU_PACKET_LEN);
2496
2497 memcpy(buf, firmware->data + sent, size);
2498
2499 pipe = usb_sndbulkpipe(udev, 0x02);
2500 err = usb_bulk_msg(udev, pipe, buf, size, &len,
2501 QCA_DFU_TIMEOUT);
2502 if (err < 0) {
2503 BT_ERR("%s: Failed to send body at %zd of %zd (%d)",
2504 hdev->name, sent, firmware->size, err);
2505 break;
2506 }
2507
2508 if (size != len) {
2509 BT_ERR("%s: Failed to get bulk buffer", hdev->name);
2510 err = -EILSEQ;
2511 break;
2512 }
2513
2514 sent += size;
2515 count -= size;
2516 }
2517
2518done:
2519 kfree(buf);
2520 return err;
2521}
2522
2523static int btusb_setup_qca_load_rampatch(struct hci_dev *hdev,
2524 struct qca_version *ver,
2525 const struct qca_device_info *info)
2526{
2527 struct qca_rampatch_version *rver;
2528 const struct firmware *fw;
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002529 u32 ver_rom, ver_patch;
2530 u16 rver_rom, rver_patch;
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002531 char fwname[64];
2532 int err;
2533
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002534 ver_rom = le32_to_cpu(ver->rom_version);
2535 ver_patch = le32_to_cpu(ver->patch_version);
2536
2537 snprintf(fwname, sizeof(fwname), "qca/rampatch_usb_%08x.bin", ver_rom);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002538
2539 err = request_firmware(&fw, fwname, &hdev->dev);
2540 if (err) {
2541 BT_ERR("%s: failed to request rampatch file: %s (%d)",
2542 hdev->name, fwname, err);
2543 return err;
2544 }
2545
2546 BT_INFO("%s: using rampatch file: %s", hdev->name, fwname);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002547
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002548 rver = (struct qca_rampatch_version *)(fw->data + info->ver_offset);
2549 rver_rom = le16_to_cpu(rver->rom_version);
2550 rver_patch = le16_to_cpu(rver->patch_version);
2551
2552 BT_INFO("%s: QCA: patch rome 0x%x build 0x%x, firmware rome 0x%x "
2553 "build 0x%x", hdev->name, rver_rom, rver_patch, ver_rom,
2554 ver_patch);
2555
2556 if (rver_rom != ver_rom || rver_patch <= ver_patch) {
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002557 BT_ERR("%s: rampatch file version did not match with firmware",
2558 hdev->name);
2559 err = -EINVAL;
2560 goto done;
2561 }
2562
2563 err = btusb_setup_qca_download_fw(hdev, fw, info->rampatch_hdr);
2564
2565done:
2566 release_firmware(fw);
2567
2568 return err;
2569}
2570
2571static int btusb_setup_qca_load_nvm(struct hci_dev *hdev,
2572 struct qca_version *ver,
2573 const struct qca_device_info *info)
2574{
2575 const struct firmware *fw;
2576 char fwname[64];
2577 int err;
2578
2579 snprintf(fwname, sizeof(fwname), "qca/nvm_usb_%08x.bin",
2580 le32_to_cpu(ver->rom_version));
2581
2582 err = request_firmware(&fw, fwname, &hdev->dev);
2583 if (err) {
2584 BT_ERR("%s: failed to request NVM file: %s (%d)",
2585 hdev->name, fwname, err);
2586 return err;
2587 }
2588
2589 BT_INFO("%s: using NVM file: %s", hdev->name, fwname);
2590
2591 err = btusb_setup_qca_download_fw(hdev, fw, info->nvm_hdr);
2592
2593 release_firmware(fw);
2594
2595 return err;
2596}
2597
2598static int btusb_setup_qca(struct hci_dev *hdev)
2599{
2600 const struct qca_device_info *info = NULL;
2601 struct qca_version ver;
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002602 u32 ver_rom;
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002603 u8 status;
2604 int i, err;
2605
2606 err = btusb_qca_send_vendor_req(hdev, QCA_GET_TARGET_VERSION, &ver,
Marcel Holtmanneb500422015-04-16 23:15:50 +02002607 sizeof(ver));
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002608 if (err < 0)
2609 return err;
2610
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002611 ver_rom = le32_to_cpu(ver.rom_version);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002612 for (i = 0; i < ARRAY_SIZE(qca_devices_table); i++) {
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002613 if (ver_rom == qca_devices_table[i].rom_version)
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002614 info = &qca_devices_table[i];
2615 }
2616 if (!info) {
2617 BT_ERR("%s: don't support firmware rome 0x%x", hdev->name,
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002618 ver_rom);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002619 return -ENODEV;
2620 }
2621
2622 err = btusb_qca_send_vendor_req(hdev, QCA_CHECK_STATUS, &status,
2623 sizeof(status));
2624 if (err < 0)
2625 return err;
2626
2627 if (!(status & QCA_PATCH_UPDATED)) {
2628 err = btusb_setup_qca_load_rampatch(hdev, &ver, info);
2629 if (err < 0)
2630 return err;
2631 }
2632
2633 if (!(status & QCA_SYSCFG_UPDATED)) {
2634 err = btusb_setup_qca_load_nvm(hdev, &ver, info);
2635 if (err < 0)
2636 return err;
2637 }
2638
2639 return 0;
2640}
2641
Marcel Holtmann9d08f502015-10-08 20:23:08 +02002642#ifdef CONFIG_BT_HCIBTUSB_BCM
2643static inline int __set_diag_interface(struct hci_dev *hdev)
2644{
2645 struct btusb_data *data = hci_get_drvdata(hdev);
2646 struct usb_interface *intf = data->diag;
2647 int i;
2648
2649 if (!data->diag)
2650 return -ENODEV;
2651
2652 data->diag_tx_ep = NULL;
2653 data->diag_rx_ep = NULL;
2654
2655 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
2656 struct usb_endpoint_descriptor *ep_desc;
2657
2658 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
2659
2660 if (!data->diag_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
2661 data->diag_tx_ep = ep_desc;
2662 continue;
2663 }
2664
2665 if (!data->diag_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
2666 data->diag_rx_ep = ep_desc;
2667 continue;
2668 }
2669 }
2670
2671 if (!data->diag_tx_ep || !data->diag_rx_ep) {
2672 BT_ERR("%s invalid diagnostic descriptors", hdev->name);
2673 return -ENODEV;
2674 }
2675
2676 return 0;
2677}
2678
2679static struct urb *alloc_diag_urb(struct hci_dev *hdev, bool enable)
2680{
2681 struct btusb_data *data = hci_get_drvdata(hdev);
2682 struct sk_buff *skb;
2683 struct urb *urb;
2684 unsigned int pipe;
2685
2686 if (!data->diag_tx_ep)
2687 return ERR_PTR(-ENODEV);
2688
2689 urb = usb_alloc_urb(0, GFP_KERNEL);
2690 if (!urb)
2691 return ERR_PTR(-ENOMEM);
2692
2693 skb = bt_skb_alloc(2, GFP_KERNEL);
2694 if (!skb) {
2695 usb_free_urb(urb);
2696 return ERR_PTR(-ENOMEM);
2697 }
2698
2699 *skb_put(skb, 1) = 0xf0;
2700 *skb_put(skb, 1) = enable;
2701
2702 pipe = usb_sndbulkpipe(data->udev, data->diag_tx_ep->bEndpointAddress);
2703
2704 usb_fill_bulk_urb(urb, data->udev, pipe,
2705 skb->data, skb->len, btusb_tx_complete, skb);
2706
2707 skb->dev = (void *)hdev;
2708
2709 return urb;
2710}
2711
2712static int btusb_bcm_set_diag(struct hci_dev *hdev, bool enable)
2713{
2714 struct btusb_data *data = hci_get_drvdata(hdev);
2715 struct urb *urb;
2716
2717 if (!data->diag)
2718 return -ENODEV;
2719
2720 if (!test_bit(HCI_RUNNING, &hdev->flags))
2721 return -ENETDOWN;
2722
2723 urb = alloc_diag_urb(hdev, enable);
2724 if (IS_ERR(urb))
2725 return PTR_ERR(urb);
2726
2727 return submit_or_queue_tx_urb(hdev, urb);
2728}
2729#endif
2730
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002731static int btusb_probe(struct usb_interface *intf,
Marcel Holtmann89e75332014-09-16 04:44:50 +02002732 const struct usb_device_id *id)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002733{
2734 struct usb_endpoint_descriptor *ep_desc;
2735 struct btusb_data *data;
2736 struct hci_dev *hdev;
Marcel Holtmann22f8e9d2015-10-20 00:53:33 +02002737 unsigned ifnum_base;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002738 int i, err;
2739
2740 BT_DBG("intf %p id %p", intf, id);
2741
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002742 /* interface numbers are hardcoded in the spec */
Marcel Holtmann22f8e9d2015-10-20 00:53:33 +02002743 if (intf->cur_altsetting->desc.bInterfaceNumber != 0) {
2744 if (!(id->driver_info & BTUSB_IFNUM_2))
2745 return -ENODEV;
2746 if (intf->cur_altsetting->desc.bInterfaceNumber != 2)
2747 return -ENODEV;
2748 }
2749
2750 ifnum_base = intf->cur_altsetting->desc.bInterfaceNumber;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002751
2752 if (!id->driver_info) {
2753 const struct usb_device_id *match;
Marcel Holtmann89e75332014-09-16 04:44:50 +02002754
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002755 match = usb_match_id(intf, blacklist_table);
2756 if (match)
2757 id = match;
2758 }
2759
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002760 if (id->driver_info == BTUSB_IGNORE)
2761 return -ENODEV;
2762
Steven.Li2d25f8b2011-07-01 14:02:36 +08002763 if (id->driver_info & BTUSB_ATH3012) {
2764 struct usb_device *udev = interface_to_usbdev(intf);
2765
2766 /* Old firmware would otherwise let ath3k driver load
2767 * patch and sysconfig files */
2768 if (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x0001)
2769 return -ENODEV;
2770 }
2771
Sachin Kamat98921db2012-07-27 12:38:39 +05302772 data = devm_kzalloc(&intf->dev, sizeof(*data), GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002773 if (!data)
2774 return -ENOMEM;
2775
2776 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
2777 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
2778
2779 if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
2780 data->intr_ep = ep_desc;
2781 continue;
2782 }
2783
2784 if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
2785 data->bulk_tx_ep = ep_desc;
2786 continue;
2787 }
2788
2789 if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
2790 data->bulk_rx_ep = ep_desc;
2791 continue;
2792 }
2793 }
2794
Sachin Kamat98921db2012-07-27 12:38:39 +05302795 if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002796 return -ENODEV;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002797
Marcel Holtmann893ba542015-01-28 20:27:34 -08002798 if (id->driver_info & BTUSB_AMP) {
2799 data->cmdreq_type = USB_TYPE_CLASS | 0x01;
2800 data->cmdreq = 0x2b;
2801 } else {
2802 data->cmdreq_type = USB_TYPE_CLASS;
2803 data->cmdreq = 0x00;
2804 }
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002805
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002806 data->udev = interface_to_usbdev(intf);
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02002807 data->intf = intf;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002808
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002809 INIT_WORK(&data->work, btusb_work);
Oliver Neukum7bee5492009-08-24 23:44:59 +02002810 INIT_WORK(&data->waker, btusb_waker);
Marcel Holtmann803b5832014-09-16 08:00:29 +02002811 init_usb_anchor(&data->deferred);
2812 init_usb_anchor(&data->tx_anchor);
Oliver Neukum7bee5492009-08-24 23:44:59 +02002813 spin_lock_init(&data->txlock);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002814
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002815 init_usb_anchor(&data->intr_anchor);
2816 init_usb_anchor(&data->bulk_anchor);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02002817 init_usb_anchor(&data->isoc_anchor);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02002818 init_usb_anchor(&data->diag_anchor);
Marcel Holtmann803b5832014-09-16 08:00:29 +02002819 spin_lock_init(&data->rxlock);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002820
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002821 if (id->driver_info & BTUSB_INTEL_NEW) {
2822 data->recv_event = btusb_recv_event_intel;
2823 data->recv_bulk = btusb_recv_bulk_intel;
2824 set_bit(BTUSB_BOOTLOADER, &data->flags);
2825 } else {
2826 data->recv_event = hci_recv_frame;
2827 data->recv_bulk = btusb_recv_bulk;
2828 }
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +01002829
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002830 hdev = hci_alloc_dev();
Sachin Kamat98921db2012-07-27 12:38:39 +05302831 if (!hdev)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002832 return -ENOMEM;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002833
Marcel Holtmannc13854c2010-02-08 15:27:07 +01002834 hdev->bus = HCI_USB;
David Herrmann155961e2012-02-09 21:58:32 +01002835 hci_set_drvdata(hdev, data);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002836
Marcel Holtmann893ba542015-01-28 20:27:34 -08002837 if (id->driver_info & BTUSB_AMP)
2838 hdev->dev_type = HCI_AMP;
2839 else
Marcel Holtmannca8bee52016-07-05 14:30:14 +02002840 hdev->dev_type = HCI_PRIMARY;
Marcel Holtmann893ba542015-01-28 20:27:34 -08002841
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002842 data->hdev = hdev;
2843
2844 SET_HCIDEV_DEV(hdev, &intf->dev);
2845
Marcel Holtmann9f8f9622013-04-10 08:11:35 -07002846 hdev->open = btusb_open;
2847 hdev->close = btusb_close;
2848 hdev->flush = btusb_flush;
2849 hdev->send = btusb_send_frame;
2850 hdev->notify = btusb_notify;
2851
Szymon Janc418678b2016-09-01 17:22:37 +02002852 if (id->driver_info & BTUSB_CW6622)
2853 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
2854
Marcel Holtmann6c9d4352015-10-17 14:39:27 +02002855 if (id->driver_info & BTUSB_BCM2045)
2856 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY, &hdev->quirks);
2857
Marcel Holtmann9f8f9622013-04-10 08:11:35 -07002858 if (id->driver_info & BTUSB_BCM92035)
2859 hdev->setup = btusb_setup_bcm92035;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002860
Marcel Holtmannc2bfb102015-04-05 22:52:14 -07002861#ifdef CONFIG_BT_HCIBTUSB_BCM
Marcel Holtmannabbaf502014-07-02 00:53:48 +02002862 if (id->driver_info & BTUSB_BCM_PATCHRAM) {
Marcel Holtmann49a5f782015-10-20 02:30:48 +02002863 hdev->manufacturer = 15;
Marcel Holtmannc2bfb102015-04-05 22:52:14 -07002864 hdev->setup = btbcm_setup_patchram;
Marcel Holtmann9d08f502015-10-08 20:23:08 +02002865 hdev->set_diag = btusb_bcm_set_diag;
Marcel Holtmann1df1f592015-04-05 22:52:11 -07002866 hdev->set_bdaddr = btbcm_set_bdaddr;
Marcel Holtmann9d08f502015-10-08 20:23:08 +02002867
2868 /* Broadcom LM_DIAG Interface numbers are hardcoded */
Marcel Holtmann22f8e9d2015-10-20 00:53:33 +02002869 data->diag = usb_ifnum_to_if(data->udev, ifnum_base + 2);
Marcel Holtmannabbaf502014-07-02 00:53:48 +02002870 }
Petri Gynther10d4c672014-05-08 15:50:01 -07002871
Marcel Holtmann9d08f502015-10-08 20:23:08 +02002872 if (id->driver_info & BTUSB_BCM_APPLE) {
Marcel Holtmann49a5f782015-10-20 02:30:48 +02002873 hdev->manufacturer = 15;
Marcel Holtmannc2bfb102015-04-05 22:52:14 -07002874 hdev->setup = btbcm_setup_apple;
Marcel Holtmann9d08f502015-10-08 20:23:08 +02002875 hdev->set_diag = btusb_bcm_set_diag;
2876
2877 /* Broadcom LM_DIAG Interface numbers are hardcoded */
Marcel Holtmann22f8e9d2015-10-20 00:53:33 +02002878 data->diag = usb_ifnum_to_if(data->udev, ifnum_base + 2);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02002879 }
Marcel Holtmannc2bfb102015-04-05 22:52:14 -07002880#endif
Marcel Holtmann17b27722015-03-22 15:52:38 +01002881
Marcel Holtmanncb8d6592014-07-02 11:25:25 +02002882 if (id->driver_info & BTUSB_INTEL) {
Marcel Holtmann49a5f782015-10-20 02:30:48 +02002883 hdev->manufacturer = 2;
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002884 hdev->setup = btusb_setup_intel;
Tedd Ho-Jeong Anbfbd45e2015-02-13 09:20:52 -08002885 hdev->shutdown = btusb_shutdown_intel;
Marcel Holtmann3e247672015-10-17 16:00:28 +02002886 hdev->set_diag = btintel_set_diag_mfg;
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07002887 hdev->set_bdaddr = btintel_set_bdaddr;
Jakub Pawlowskic33fb9b2015-01-30 18:55:58 -08002888 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
Jakub Pawlowskic1154842015-03-17 09:04:16 -07002889 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
Marcel Holtmann3e247672015-10-17 16:00:28 +02002890 set_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks);
Marcel Holtmanncb8d6592014-07-02 11:25:25 +02002891 }
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002892
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002893 if (id->driver_info & BTUSB_INTEL_NEW) {
Marcel Holtmann49a5f782015-10-20 02:30:48 +02002894 hdev->manufacturer = 2;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002895 hdev->send = btusb_send_frame_intel;
2896 hdev->setup = btusb_setup_intel_new;
Marcel Holtmanneeb6abe2015-07-05 14:37:39 +02002897 hdev->hw_error = btintel_hw_error;
Marcel Holtmann6d2e50d2015-10-09 14:42:08 +02002898 hdev->set_diag = btintel_set_diag;
Marcel Holtmann4185a0f2015-04-06 00:52:12 -07002899 hdev->set_bdaddr = btintel_set_bdaddr;
Marcel Holtmannb970c5b2015-02-01 23:57:18 -08002900 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
Marcel Holtmannd8270fb2015-10-17 16:00:27 +02002901 set_bit(HCI_QUIRK_NON_PERSISTENT_DIAG, &hdev->quirks);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002902 }
2903
Amitkumar Karwarae8df492014-07-18 14:47:06 -07002904 if (id->driver_info & BTUSB_MARVELL)
2905 hdev->set_bdaddr = btusb_set_bdaddr_marvell;
2906
Marcel Holtmann661cf882015-01-02 23:35:20 -08002907 if (id->driver_info & BTUSB_SWAVE) {
2908 set_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks);
Marcel Holtmannd57dbe72014-12-26 04:42:33 +01002909 set_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS, &hdev->quirks);
Marcel Holtmann661cf882015-01-02 23:35:20 -08002910 }
Marcel Holtmannd57dbe72014-12-26 04:42:33 +01002911
Marcel Holtmanne4c534b2015-10-21 01:31:45 +02002912 if (id->driver_info & BTUSB_INTEL_BOOT) {
2913 hdev->manufacturer = 2;
Marcel Holtmann40df7832014-07-06 13:29:58 +02002914 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
Marcel Holtmanne4c534b2015-10-21 01:31:45 +02002915 }
Marcel Holtmann40df7832014-07-06 13:29:58 +02002916
Jakub Pawlowski79f0c872015-01-29 10:38:34 -08002917 if (id->driver_info & BTUSB_ATH3012) {
Toshi Kikuchi58592232014-12-12 10:58:05 -08002918 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
Jakub Pawlowski3d50d512015-03-17 09:04:15 -07002919 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
Jakub Pawlowski79f0c872015-01-29 10:38:34 -08002920 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
2921 }
Toshi Kikuchi58592232014-12-12 10:58:05 -08002922
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002923 if (id->driver_info & BTUSB_QCA_ROME) {
2924 data->setup_on_usb = btusb_setup_qca;
2925 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
2926 }
2927
Carlo Caionedb33c772015-05-14 10:49:09 +02002928#ifdef CONFIG_BT_HCIBTUSB_RTL
Daniel Drake04b8c812015-05-21 08:23:50 -06002929 if (id->driver_info & BTUSB_REALTEK) {
Carlo Caionedb33c772015-05-14 10:49:09 +02002930 hdev->setup = btrtl_setup_realtek;
Daniel Drake04b8c812015-05-21 08:23:50 -06002931
2932 /* Realtek devices lose their updated firmware over suspend,
2933 * but the USB hub doesn't notice any status change.
2934 * Explicitly request a device reset on resume.
2935 */
2936 set_bit(BTUSB_RESET_RESUME, &data->flags);
2937 }
Carlo Caionedb33c772015-05-14 10:49:09 +02002938#endif
Daniel Drakea2698a92015-04-16 14:09:55 -06002939
Marcel Holtmann893ba542015-01-28 20:27:34 -08002940 if (id->driver_info & BTUSB_AMP) {
2941 /* AMP controllers do not support SCO packets */
2942 data->isoc = NULL;
2943 } else {
Marcel Holtmann22f8e9d2015-10-20 00:53:33 +02002944 /* Interface orders are hardcoded in the specification */
2945 data->isoc = usb_ifnum_to_if(data->udev, ifnum_base + 1);
Marcel Holtmann893ba542015-01-28 20:27:34 -08002946 }
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02002947
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002948 if (!reset)
Szymon Janca6c511c2012-05-23 12:35:46 +02002949 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002950
2951 if (force_scofix || id->driver_info & BTUSB_WRONG_SCO_MTU) {
2952 if (!disable_scofix)
2953 set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
2954 }
2955
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02002956 if (id->driver_info & BTUSB_BROKEN_ISOC)
2957 data->isoc = NULL;
2958
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002959 if (id->driver_info & BTUSB_DIGIANSWER) {
2960 data->cmdreq_type = USB_TYPE_VENDOR;
Szymon Janca6c511c2012-05-23 12:35:46 +02002961 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002962 }
2963
2964 if (id->driver_info & BTUSB_CSR) {
2965 struct usb_device *udev = data->udev;
Marcel Holtmann81cac642014-01-03 03:02:36 -08002966 u16 bcdDevice = le16_to_cpu(udev->descriptor.bcdDevice);
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002967
2968 /* Old firmware would otherwise execute USB reset */
Marcel Holtmann81cac642014-01-03 03:02:36 -08002969 if (bcdDevice < 0x117)
Szymon Janca6c511c2012-05-23 12:35:46 +02002970 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmann81cac642014-01-03 03:02:36 -08002971
2972 /* Fake CSR devices with broken commands */
Johan Hedberg6cafcd92015-08-30 21:47:21 +03002973 if (bcdDevice <= 0x100 || bcdDevice == 0x134)
Marcel Holtmann81cac642014-01-03 03:02:36 -08002974 hdev->setup = btusb_setup_csr;
Jakub Pawlowski49c989a2015-03-17 09:04:17 -07002975
2976 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002977 }
2978
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002979 if (id->driver_info & BTUSB_SNIFFER) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02002980 struct usb_device *udev = data->udev;
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002981
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002982 /* New sniffer firmware has crippled HCI interface */
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002983 if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
2984 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
2985 }
2986
Marcel Holtmann3a5ef202014-07-06 14:53:54 +02002987 if (id->driver_info & BTUSB_INTEL_BOOT) {
2988 /* A bug in the bootloader causes that interrupt interface is
2989 * only enabled after receiving SetInterface(0, AltSetting=0).
2990 */
2991 err = usb_set_interface(data->udev, 0, 0);
2992 if (err < 0) {
2993 BT_ERR("failed to set interface 0, alt 0 %d", err);
2994 hci_free_dev(hdev);
2995 return err;
2996 }
2997 }
2998
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02002999 if (data->isoc) {
3000 err = usb_driver_claim_interface(&btusb_driver,
Marcel Holtmann89e75332014-09-16 04:44:50 +02003001 data->isoc, data);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02003002 if (err < 0) {
3003 hci_free_dev(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02003004 return err;
3005 }
3006 }
3007
Marcel Holtmann9d08f502015-10-08 20:23:08 +02003008#ifdef CONFIG_BT_HCIBTUSB_BCM
3009 if (data->diag) {
3010 if (!usb_driver_claim_interface(&btusb_driver,
3011 data->diag, data))
3012 __set_diag_interface(hdev);
3013 else
3014 data->diag = NULL;
3015 }
3016#endif
3017
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003018 err = hci_register_dev(hdev);
3019 if (err < 0) {
3020 hci_free_dev(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003021 return err;
3022 }
3023
3024 usb_set_intfdata(intf, data);
3025
3026 return 0;
3027}
3028
3029static void btusb_disconnect(struct usb_interface *intf)
3030{
3031 struct btusb_data *data = usb_get_intfdata(intf);
3032 struct hci_dev *hdev;
3033
3034 BT_DBG("intf %p", intf);
3035
3036 if (!data)
3037 return;
3038
3039 hdev = data->hdev;
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02003040 usb_set_intfdata(data->intf, NULL);
3041
3042 if (data->isoc)
3043 usb_set_intfdata(data->isoc, NULL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003044
Marcel Holtmann9d08f502015-10-08 20:23:08 +02003045 if (data->diag)
3046 usb_set_intfdata(data->diag, NULL);
3047
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003048 hci_unregister_dev(hdev);
3049
Marcel Holtmann9d08f502015-10-08 20:23:08 +02003050 if (intf == data->intf) {
3051 if (data->isoc)
3052 usb_driver_release_interface(&btusb_driver, data->isoc);
3053 if (data->diag)
3054 usb_driver_release_interface(&btusb_driver, data->diag);
3055 } else if (intf == data->isoc) {
3056 if (data->diag)
3057 usb_driver_release_interface(&btusb_driver, data->diag);
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02003058 usb_driver_release_interface(&btusb_driver, data->intf);
Marcel Holtmann9d08f502015-10-08 20:23:08 +02003059 } else if (intf == data->diag) {
3060 usb_driver_release_interface(&btusb_driver, data->intf);
3061 if (data->isoc)
3062 usb_driver_release_interface(&btusb_driver, data->isoc);
3063 }
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02003064
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003065 hci_free_dev(hdev);
3066}
3067
Oliver Neukum7bee5492009-08-24 23:44:59 +02003068#ifdef CONFIG_PM
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003069static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
3070{
3071 struct btusb_data *data = usb_get_intfdata(intf);
3072
3073 BT_DBG("intf %p", intf);
3074
3075 if (data->suspend_count++)
3076 return 0;
3077
Oliver Neukum7bee5492009-08-24 23:44:59 +02003078 spin_lock_irq(&data->txlock);
Alan Stern5b1b0b82011-08-19 23:49:48 +02003079 if (!(PMSG_IS_AUTO(message) && data->tx_in_flight)) {
Oliver Neukum7bee5492009-08-24 23:44:59 +02003080 set_bit(BTUSB_SUSPENDING, &data->flags);
3081 spin_unlock_irq(&data->txlock);
3082 } else {
3083 spin_unlock_irq(&data->txlock);
3084 data->suspend_count--;
3085 return -EBUSY;
3086 }
3087
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003088 cancel_work_sync(&data->work);
3089
Oliver Neukum7bee5492009-08-24 23:44:59 +02003090 btusb_stop_traffic(data);
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003091 usb_kill_anchored_urbs(&data->tx_anchor);
3092
Daniel Drake04b8c812015-05-21 08:23:50 -06003093 /* Optionally request a device reset on resume, but only when
3094 * wakeups are disabled. If wakeups are enabled we assume the
3095 * device will stay powered up throughout suspend.
3096 */
3097 if (test_bit(BTUSB_RESET_RESUME, &data->flags) &&
3098 !device_may_wakeup(&data->udev->dev))
3099 data->udev->reset_resume = 1;
3100
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003101 return 0;
3102}
3103
Oliver Neukum7bee5492009-08-24 23:44:59 +02003104static void play_deferred(struct btusb_data *data)
3105{
3106 struct urb *urb;
3107 int err;
3108
3109 while ((urb = usb_get_from_anchor(&data->deferred))) {
3110 err = usb_submit_urb(urb, GFP_ATOMIC);
3111 if (err < 0)
3112 break;
3113
3114 data->tx_in_flight++;
3115 }
3116 usb_scuttle_anchored_urbs(&data->deferred);
3117}
3118
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003119static int btusb_resume(struct usb_interface *intf)
3120{
3121 struct btusb_data *data = usb_get_intfdata(intf);
3122 struct hci_dev *hdev = data->hdev;
Oliver Neukum7bee5492009-08-24 23:44:59 +02003123 int err = 0;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003124
3125 BT_DBG("intf %p", intf);
3126
3127 if (--data->suspend_count)
3128 return 0;
3129
3130 if (!test_bit(HCI_RUNNING, &hdev->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +02003131 goto done;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003132
3133 if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
3134 err = btusb_submit_intr_urb(hdev, GFP_NOIO);
3135 if (err < 0) {
3136 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02003137 goto failed;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003138 }
3139 }
3140
3141 if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) {
Marcel Holtmann43c2e572009-02-04 17:41:38 +01003142 err = btusb_submit_bulk_urb(hdev, GFP_NOIO);
3143 if (err < 0) {
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003144 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02003145 goto failed;
3146 }
3147
3148 btusb_submit_bulk_urb(hdev, GFP_NOIO);
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003149 }
3150
3151 if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
3152 if (btusb_submit_isoc_urb(hdev, GFP_NOIO) < 0)
3153 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
3154 else
3155 btusb_submit_isoc_urb(hdev, GFP_NOIO);
3156 }
3157
Oliver Neukum7bee5492009-08-24 23:44:59 +02003158 spin_lock_irq(&data->txlock);
3159 play_deferred(data);
3160 clear_bit(BTUSB_SUSPENDING, &data->flags);
3161 spin_unlock_irq(&data->txlock);
3162 schedule_work(&data->work);
3163
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003164 return 0;
Oliver Neukum7bee5492009-08-24 23:44:59 +02003165
3166failed:
3167 usb_scuttle_anchored_urbs(&data->deferred);
3168done:
3169 spin_lock_irq(&data->txlock);
3170 clear_bit(BTUSB_SUSPENDING, &data->flags);
3171 spin_unlock_irq(&data->txlock);
3172
3173 return err;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003174}
Oliver Neukum7bee5492009-08-24 23:44:59 +02003175#endif
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003176
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003177static struct usb_driver btusb_driver = {
3178 .name = "btusb",
3179 .probe = btusb_probe,
3180 .disconnect = btusb_disconnect,
Oliver Neukum7bee5492009-08-24 23:44:59 +02003181#ifdef CONFIG_PM
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003182 .suspend = btusb_suspend,
3183 .resume = btusb_resume,
Oliver Neukum7bee5492009-08-24 23:44:59 +02003184#endif
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003185 .id_table = btusb_table,
Oliver Neukum7bee5492009-08-24 23:44:59 +02003186 .supports_autosuspend = 1,
Sarah Sharpe1f12eb2012-04-23 10:08:51 -07003187 .disable_hub_initiated_lpm = 1,
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003188};
3189
Greg Kroah-Hartman93f15082011-11-18 09:47:34 -08003190module_usb_driver(btusb_driver);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003191
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02003192module_param(disable_scofix, bool, 0644);
3193MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
3194
3195module_param(force_scofix, bool, 0644);
3196MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
3197
3198module_param(reset, bool, 0644);
3199MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
3200
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003201MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
3202MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
3203MODULE_VERSION(VERSION);
3204MODULE_LICENSE("GPL");