blob: 6e4ff16e487b06229346a9284e867962f70f0d5c [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>
Marcel Holtmann17b27722015-03-22 15:52:38 +010027#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 Holtmanncda0dd72015-01-26 21:33:48 -080032#define VERSION "0.7"
Marcel Holtmanncfeb4142008-08-07 22:26:56 +020033
Rusty Russell90ab5ee2012-01-13 09:32:20 +103034static bool disable_scofix;
35static bool force_scofix;
Marcel Holtmann7a9d4022008-11-30 12:17:26 +010036
Rusty Russell90ab5ee2012-01-13 09:32:20 +103037static bool reset = 1;
Marcel Holtmanncfeb4142008-08-07 22:26:56 +020038
39static struct usb_driver btusb_driver;
40
41#define BTUSB_IGNORE 0x01
Marcel Holtmann7a9d4022008-11-30 12:17:26 +010042#define BTUSB_DIGIANSWER 0x02
43#define BTUSB_CSR 0x04
44#define BTUSB_SNIFFER 0x08
45#define BTUSB_BCM92035 0x10
46#define BTUSB_BROKEN_ISOC 0x20
47#define BTUSB_WRONG_SCO_MTU 0x40
Steven.Li2d25f8b2011-07-01 14:02:36 +080048#define BTUSB_ATH3012 0x80
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -070049#define BTUSB_INTEL 0x100
Marcel Holtmann40df7832014-07-06 13:29:58 +020050#define BTUSB_INTEL_BOOT 0x200
51#define BTUSB_BCM_PATCHRAM 0x400
Amitkumar Karwarae8df492014-07-18 14:47:06 -070052#define BTUSB_MARVELL 0x800
Marcel Holtmann4fcef8e2015-01-01 17:34:37 -080053#define BTUSB_SWAVE 0x1000
Marcel Holtmanncda0dd72015-01-26 21:33:48 -080054#define BTUSB_INTEL_NEW 0x2000
Marcel Holtmann893ba542015-01-28 20:27:34 -080055#define BTUSB_AMP 0x4000
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +000056#define BTUSB_QCA_ROME 0x8000
Marcel Holtmann17b27722015-03-22 15:52:38 +010057#define BTUSB_BCM_APPLE 0x10000
Marcel Holtmann5e23b922007-10-20 14:12:34 +020058
Marcel Holtmann54265202013-10-11 07:46:18 -070059static const struct usb_device_id btusb_table[] = {
Marcel Holtmann5e23b922007-10-20 14:12:34 +020060 /* Generic Bluetooth USB device */
61 { USB_DEVICE_INFO(0xe0, 0x01, 0x01) },
62
Marcel Holtmann893ba542015-01-28 20:27:34 -080063 /* Generic Bluetooth AMP device */
64 { USB_DEVICE_INFO(0xe0, 0x01, 0x04), .driver_info = BTUSB_AMP },
65
Henrik Rydberg1fa65352012-08-25 19:28:06 +020066 /* Apple-specific (Broadcom) devices */
Marcel Holtmann17b27722015-03-22 15:52:38 +010067 { USB_VENDOR_AND_INTERFACE_INFO(0x05ac, 0xff, 0x01, 0x01),
68 .driver_info = BTUSB_BCM_APPLE },
Henrik Rydberg1fa65352012-08-25 19:28:06 +020069
Cho, Yu-Chen178c0592013-06-04 21:40:26 +080070 /* MediaTek MT76x0E */
71 { USB_DEVICE(0x0e8d, 0x763f) },
72
Oliver Neukumc510eae2011-09-21 11:41:45 +020073 /* Broadcom SoftSailing reporting vendor specific */
Don Zickus2e8b50632012-03-28 16:41:11 -040074 { USB_DEVICE(0x0a5c, 0x21e1) },
Oliver Neukumc510eae2011-09-21 11:41:45 +020075
Nobuhiro Iwamatsu3cd01972010-08-20 16:24:07 +090076 /* Apple MacBookPro 7,1 */
77 { USB_DEVICE(0x05ac, 0x8213) },
78
Cyril Lacoux0a79f672010-07-14 10:29:27 +040079 /* Apple iMac11,1 */
80 { USB_DEVICE(0x05ac, 0x8215) },
81
Nobuhiro Iwamatsu9c047152010-08-20 16:24:06 +090082 /* Apple MacBookPro6,2 */
83 { USB_DEVICE(0x05ac, 0x8218) },
84
Edgar (gimli) Hucek3e3ede72010-11-04 08:04:33 +010085 /* Apple MacBookAir3,1, MacBookAir3,2 */
86 { USB_DEVICE(0x05ac, 0x821b) },
87
Pieter-Augustijn Van Malleghema63b7232011-09-07 02:28:10 -040088 /* Apple MacBookAir4,1 */
89 { USB_DEVICE(0x05ac, 0x821f) },
90
Marc-Antoine Perennou88d377b2011-03-24 14:51:21 -030091 /* Apple MacBookPro8,2 */
92 { USB_DEVICE(0x05ac, 0x821a) },
93
Jurgen Kramerf78b68262011-09-04 18:01:42 +020094 /* Apple MacMini5,1 */
95 { USB_DEVICE(0x05ac, 0x8281) },
96
Marcel Holtmanncfeb4142008-08-07 22:26:56 +020097 /* AVM BlueFRITZ! USB v2.0 */
Marcel Holtmann4fcef8e2015-01-01 17:34:37 -080098 { USB_DEVICE(0x057c, 0x3800), .driver_info = BTUSB_SWAVE },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +020099
100 /* Bluetooth Ultraport Module from IBM */
101 { USB_DEVICE(0x04bf, 0x030a) },
102
103 /* ALPS Modules with non-standard id */
104 { USB_DEVICE(0x044e, 0x3001) },
105 { USB_DEVICE(0x044e, 0x3002) },
106
107 /* Ericsson with non-standard id */
108 { USB_DEVICE(0x0bdb, 0x1002) },
109
110 /* Canyon CN-BTU1 with HID interfaces */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100111 { USB_DEVICE(0x0c10, 0x0000) },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200112
Wen-chien Jesse Sungd13431c2011-11-08 14:30:22 +0800113 /* Broadcom BCM20702A0 */
114 { USB_DEVICE(0x413c, 0x8197) },
115
Marcel Holtmannd049f4e2015-01-26 20:35:32 -0800116 /* Broadcom BCM20702B0 (Dynex/Insignia) */
117 { USB_DEVICE(0x19ff, 0x0239), .driver_info = BTUSB_BCM_PATCHRAM },
118
Steven Harms98514032012-04-13 14:45:55 -0400119 /* Foxconn - Hon Hai */
Heinrich Siebmanns6029ddc2014-12-03 19:32:22 +0100120 { USB_VENDOR_AND_INTERFACE_INFO(0x0489, 0xff, 0x01, 0x01),
121 .driver_info = BTUSB_BCM_PATCHRAM },
Steven Harms98514032012-04-13 14:45:55 -0400122
Matej Dubovy8f0c3042015-02-02 18:50:14 +0100123 /* Lite-On Technology - Broadcom based */
124 { USB_VENDOR_AND_INTERFACE_INFO(0x04ca, 0xff, 0x01, 0x01),
125 .driver_info = BTUSB_BCM_PATCHRAM },
126
Andy Shevchenko0b880062014-02-18 18:26:19 +0200127 /* Broadcom devices with vendor specific id */
Petri Gynther10d4c672014-05-08 15:50:01 -0700128 { USB_VENDOR_AND_INTERFACE_INFO(0x0a5c, 0xff, 0x01, 0x01),
129 .driver_info = BTUSB_BCM_PATCHRAM },
Gustavo Padovan92c385f2012-08-06 15:36:49 -0300130
Marcel Holtmannc2aef6e2014-07-21 14:02:33 +0200131 /* ASUSTek Computer - Broadcom based */
Rick Dunn9a5abda2015-01-17 05:29:12 +0100132 { USB_VENDOR_AND_INTERFACE_INFO(0x0b05, 0xff, 0x01, 0x01),
133 .driver_info = BTUSB_BCM_PATCHRAM },
Marcel Holtmannc2aef6e2014-07-21 14:02:33 +0200134
Ken O'Brien5bcecf32013-09-21 19:14:43 +0100135 /* Belkin F8065bf - Broadcom based */
Marcel Holtmann6331c682015-03-27 15:11:41 -0700136 { USB_VENDOR_AND_INTERFACE_INFO(0x050d, 0xff, 0x01, 0x01),
137 .driver_info = BTUSB_BCM_PATCHRAM },
Ken O'Brien5bcecf32013-09-21 19:14:43 +0100138
Jurgen Kramer9113bfd2014-02-15 12:01:09 +0100139 /* IMC Networks - Broadcom based */
Marcel Holtmann6331c682015-03-27 15:11:41 -0700140 { USB_VENDOR_AND_INTERFACE_INFO(0x13d3, 0xff, 0x01, 0x01),
141 .driver_info = BTUSB_BCM_PATCHRAM },
Jurgen Kramer9113bfd2014-02-15 12:01:09 +0100142
Marcel Holtmann40df7832014-07-06 13:29:58 +0200143 /* Intel Bluetooth USB Bootloader (RAM module) */
Marcel Holtmannd92f2df2014-07-06 14:53:55 +0200144 { USB_DEVICE(0x8087, 0x0a5a),
145 .driver_info = BTUSB_INTEL_BOOT | BTUSB_BROKEN_ISOC },
Marcel Holtmann40df7832014-07-06 13:29:58 +0200146
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200147 { } /* Terminating entry */
148};
149
150MODULE_DEVICE_TABLE(usb, btusb_table);
151
Marcel Holtmann54265202013-10-11 07:46:18 -0700152static const struct usb_device_id blacklist_table[] = {
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200153 /* CSR BlueCore devices */
154 { USB_DEVICE(0x0a12, 0x0001), .driver_info = BTUSB_CSR },
155
156 /* Broadcom BCM2033 without firmware */
157 { USB_DEVICE(0x0a5c, 0x2033), .driver_info = BTUSB_IGNORE },
158
Bala Shanmugambe931122010-11-26 17:35:46 +0530159 /* Atheros 3011 with sflash firmware */
Andy Shevchenko0b880062014-02-18 18:26:19 +0200160 { USB_DEVICE(0x0489, 0xe027), .driver_info = BTUSB_IGNORE },
161 { USB_DEVICE(0x0489, 0xe03d), .driver_info = BTUSB_IGNORE },
Alexander Ploumistos2eeff0b2015-02-13 21:05:11 +0200162 { USB_DEVICE(0x04f2, 0xaff1), .driver_info = BTUSB_IGNORE },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200163 { USB_DEVICE(0x0930, 0x0215), .driver_info = BTUSB_IGNORE },
Bala Shanmugambe931122010-11-26 17:35:46 +0530164 { USB_DEVICE(0x0cf3, 0x3002), .driver_info = BTUSB_IGNORE },
Marek Vasut6eda5412012-06-08 14:32:50 +0200165 { USB_DEVICE(0x0cf3, 0xe019), .driver_info = BTUSB_IGNORE },
Andy Ross2a7bccc2011-05-09 16:11:16 -0700166 { USB_DEVICE(0x13d3, 0x3304), .driver_info = BTUSB_IGNORE },
Bala Shanmugambe931122010-11-26 17:35:46 +0530167
Cho, Yu-Chen509e7862011-01-26 17:10:59 +0800168 /* Atheros AR9285 Malbec with sflash firmware */
169 { USB_DEVICE(0x03f0, 0x311d), .driver_info = BTUSB_IGNORE },
170
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530171 /* Atheros 3012 with sflash firmware */
Andy Shevchenko0b880062014-02-18 18:26:19 +0200172 { USB_DEVICE(0x0489, 0xe04d), .driver_info = BTUSB_ATH3012 },
173 { USB_DEVICE(0x0489, 0xe04e), .driver_info = BTUSB_ATH3012 },
174 { USB_DEVICE(0x0489, 0xe056), .driver_info = BTUSB_ATH3012 },
175 { USB_DEVICE(0x0489, 0xe057), .driver_info = BTUSB_ATH3012 },
176 { USB_DEVICE(0x0489, 0xe05f), .driver_info = BTUSB_ATH3012 },
Anantha Krishnan4b552bc2014-10-06 16:31:49 +0530177 { USB_DEVICE(0x0489, 0xe078), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200178 { USB_DEVICE(0x04c5, 0x1330), .driver_info = BTUSB_ATH3012 },
179 { USB_DEVICE(0x04ca, 0x3004), .driver_info = BTUSB_ATH3012 },
180 { USB_DEVICE(0x04ca, 0x3005), .driver_info = BTUSB_ATH3012 },
181 { USB_DEVICE(0x04ca, 0x3006), .driver_info = BTUSB_ATH3012 },
Mohammed Habibulla1fb4e092014-04-17 11:37:13 -0700182 { USB_DEVICE(0x04ca, 0x3007), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200183 { USB_DEVICE(0x04ca, 0x3008), .driver_info = BTUSB_ATH3012 },
184 { USB_DEVICE(0x04ca, 0x300b), .driver_info = BTUSB_ATH3012 },
Janne Heikkinen134d3b32014-12-09 07:44:51 +0200185 { USB_DEVICE(0x04ca, 0x3010), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200186 { USB_DEVICE(0x0930, 0x0219), .driver_info = BTUSB_ATH3012 },
187 { USB_DEVICE(0x0930, 0x0220), .driver_info = BTUSB_ATH3012 },
Vincent Zwanenburg89d29752014-08-08 12:33:56 +0100188 { USB_DEVICE(0x0930, 0x0227), .driver_info = BTUSB_ATH3012 },
Andy Shevchenkoa735f9e2014-02-18 18:26:20 +0200189 { USB_DEVICE(0x0b05, 0x17d0), .driver_info = BTUSB_ATH3012 },
Ming Leid66629c2013-03-15 11:00:39 +0800190 { USB_DEVICE(0x0cf3, 0x0036), .driver_info = BTUSB_ATH3012 },
Steven.Li2d25f8b2011-07-01 14:02:36 +0800191 { USB_DEVICE(0x0cf3, 0x3004), .driver_info = BTUSB_ATH3012 },
Sunguk Lee94a32d102013-03-12 04:41:58 +0900192 { USB_DEVICE(0x0cf3, 0x3008), .driver_info = BTUSB_ATH3012 },
Cho, Yu-Chen07c0ea82012-03-14 22:01:21 +0200193 { USB_DEVICE(0x0cf3, 0x311d), .driver_info = BTUSB_ATH3012 },
Oliver Neukumb1312372014-01-16 15:37:11 +0100194 { USB_DEVICE(0x0cf3, 0x311e), .driver_info = BTUSB_ATH3012 },
Oliver Neukum1e56f1e2014-01-16 16:02:58 +0100195 { USB_DEVICE(0x0cf3, 0x311f), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200196 { USB_DEVICE(0x0cf3, 0x3121), .driver_info = BTUSB_ATH3012 },
Ming Leiebaf5792013-03-18 23:45:11 +0800197 { USB_DEVICE(0x0cf3, 0x817a), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200198 { USB_DEVICE(0x0cf3, 0xe003), .driver_info = BTUSB_ATH3012 },
AceLan Kaoac713112012-04-19 14:53:45 +0800199 { USB_DEVICE(0x0cf3, 0xe004), .driver_info = BTUSB_ATH3012 },
Peng Chen0a3658c2013-08-30 17:41:40 +0800200 { USB_DEVICE(0x0cf3, 0xe005), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200201 { USB_DEVICE(0x13d3, 0x3362), .driver_info = BTUSB_ATH3012 },
202 { USB_DEVICE(0x13d3, 0x3375), .driver_info = BTUSB_ATH3012 },
AceLan Kaoeed307e2012-12-11 11:41:20 +0800203 { USB_DEVICE(0x13d3, 0x3393), .driver_info = BTUSB_ATH3012 },
Sujith Manoharan5b77a1f2013-07-15 09:29:03 +0530204 { USB_DEVICE(0x13d3, 0x3402), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin3bb30a7c2014-11-25 20:19:52 +0300205 { USB_DEVICE(0x13d3, 0x3408), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin033efa92015-01-18 00:16:51 +0300206 { USB_DEVICE(0x13d3, 0x3423), .driver_info = BTUSB_ATH3012 },
Anantha Krishnanfa2f1392014-07-08 19:25:08 +0530207 { USB_DEVICE(0x13d3, 0x3432), .driver_info = BTUSB_ATH3012 },
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530208
Cho, Yu-Chene9036e32011-02-15 10:20:07 +0800209 /* Atheros AR5BBU12 with sflash firmware */
210 { USB_DEVICE(0x0489, 0xe02c), .driver_info = BTUSB_IGNORE },
211
Michael Gruetzner85d59722012-05-02 22:33:40 +0200212 /* Atheros AR5BBU12 with sflash firmware */
Yevgeniy Melnichukbc21fde2012-08-07 19:48:10 +0530213 { USB_DEVICE(0x0489, 0xe036), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200214 { USB_DEVICE(0x0489, 0xe03c), .driver_info = BTUSB_ATH3012 },
Michael Gruetzner85d59722012-05-02 22:33:40 +0200215
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +0000216 /* QCA ROME chipset */
Marcel Holtmannc9e44472015-03-16 23:56:04 -0700217 { USB_DEVICE(0x0cf3, 0xe300), .driver_info = BTUSB_QCA_ROME },
218 { USB_DEVICE(0x0cf3, 0xe360), .driver_info = BTUSB_QCA_ROME },
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +0000219
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200220 /* Broadcom BCM2035 */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100221 { USB_DEVICE(0x0a5c, 0x2009), .driver_info = BTUSB_BCM92035 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200222 { USB_DEVICE(0x0a5c, 0x200a), .driver_info = BTUSB_WRONG_SCO_MTU },
223 { USB_DEVICE(0x0a5c, 0x2035), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200224
225 /* Broadcom BCM2045 */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100226 { USB_DEVICE(0x0a5c, 0x2039), .driver_info = BTUSB_WRONG_SCO_MTU },
227 { USB_DEVICE(0x0a5c, 0x2101), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmannbdbef3d2008-09-23 00:16:35 +0200228
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200229 /* IBM/Lenovo ThinkPad with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100230 { USB_DEVICE(0x0a5c, 0x201e), .driver_info = BTUSB_WRONG_SCO_MTU },
231 { USB_DEVICE(0x0a5c, 0x2110), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200232
233 /* HP laptop with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100234 { USB_DEVICE(0x03f0, 0x171d), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200235
236 /* Dell laptop with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100237 { USB_DEVICE(0x413c, 0x8126), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200238
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100239 /* Dell Wireless 370 and 410 devices */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100240 { USB_DEVICE(0x413c, 0x8152), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100241 { USB_DEVICE(0x413c, 0x8156), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200242
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100243 /* Belkin F8T012 and F8T013 devices */
244 { USB_DEVICE(0x050d, 0x0012), .driver_info = BTUSB_WRONG_SCO_MTU },
245 { USB_DEVICE(0x050d, 0x0013), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200246
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100247 /* Asus WL-BTD202 device */
248 { USB_DEVICE(0x0b05, 0x1715), .driver_info = BTUSB_WRONG_SCO_MTU },
249
250 /* Kensington Bluetooth USB adapter */
251 { USB_DEVICE(0x047d, 0x105e), .driver_info = BTUSB_WRONG_SCO_MTU },
252
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200253 /* RTX Telecom based adapters with buggy SCO support */
254 { USB_DEVICE(0x0400, 0x0807), .driver_info = BTUSB_BROKEN_ISOC },
255 { USB_DEVICE(0x0400, 0x080a), .driver_info = BTUSB_BROKEN_ISOC },
256
257 /* CONWISE Technology based adapters with buggy SCO support */
258 { USB_DEVICE(0x0e5e, 0x6622), .driver_info = BTUSB_BROKEN_ISOC },
259
Marcel Holtmann4fcef8e2015-01-01 17:34:37 -0800260 /* Roper Class 1 Bluetooth Dongle (Silicon Wave based) */
261 { USB_DEVICE(0x1300, 0x0001), .driver_info = BTUSB_SWAVE },
262
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200263 /* Digianswer devices */
264 { USB_DEVICE(0x08fd, 0x0001), .driver_info = BTUSB_DIGIANSWER },
265 { USB_DEVICE(0x08fd, 0x0002), .driver_info = BTUSB_IGNORE },
266
267 /* CSR BlueCore Bluetooth Sniffer */
Marcel Holtmann4f64fa82014-07-07 00:12:04 +0200268 { USB_DEVICE(0x0a12, 0x0002),
269 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200270
271 /* Frontline ComProbe Bluetooth Sniffer */
Marcel Holtmann4f64fa82014-07-07 00:12:04 +0200272 { USB_DEVICE(0x16d3, 0x0002),
273 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200274
Marcel Holtmanncb1ee892015-01-28 19:41:42 -0800275 /* Marvell Bluetooth devices */
276 { USB_DEVICE(0x1286, 0x2044), .driver_info = BTUSB_MARVELL },
277 { USB_DEVICE(0x1286, 0x2046), .driver_info = BTUSB_MARVELL },
278
Marcel Holtmannd0ac9eb2015-01-28 19:41:43 -0800279 /* Intel Bluetooth devices */
Marcel Holtmann407550f2015-02-22 15:41:18 -0800280 { USB_DEVICE(0x8087, 0x07da), .driver_info = BTUSB_CSR },
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -0700281 { USB_DEVICE(0x8087, 0x07dc), .driver_info = BTUSB_INTEL },
Tedd Ho-Jeong Anef4e5e42013-11-12 13:10:58 -0800282 { USB_DEVICE(0x8087, 0x0a2a), .driver_info = BTUSB_INTEL },
Marcel Holtmanncda0dd72015-01-26 21:33:48 -0800283 { USB_DEVICE(0x8087, 0x0a2b), .driver_info = BTUSB_INTEL_NEW },
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -0700284
Marcel Holtmannd0ac9eb2015-01-28 19:41:43 -0800285 /* Other Intel Bluetooth devices */
286 { USB_VENDOR_AND_INTERFACE_INFO(0x8087, 0xe0, 0x01, 0x01),
287 .driver_info = BTUSB_IGNORE },
Amitkumar Karwarae8df492014-07-18 14:47:06 -0700288
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200289 { } /* Terminating entry */
290};
291
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200292#define BTUSB_MAX_ISOC_FRAMES 10
293
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200294#define BTUSB_INTR_RUNNING 0
295#define BTUSB_BULK_RUNNING 1
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200296#define BTUSB_ISOC_RUNNING 2
Oliver Neukum7bee5492009-08-24 23:44:59 +0200297#define BTUSB_SUSPENDING 3
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -0300298#define BTUSB_DID_ISO_RESUME 4
Marcel Holtmanncda0dd72015-01-26 21:33:48 -0800299#define BTUSB_BOOTLOADER 5
300#define BTUSB_DOWNLOADING 6
Marcel Holtmannce6bb922015-01-28 01:58:40 -0800301#define BTUSB_FIRMWARE_LOADED 7
Marcel Holtmanncda0dd72015-01-26 21:33:48 -0800302#define BTUSB_FIRMWARE_FAILED 8
Marcel Holtmannce6bb922015-01-28 01:58:40 -0800303#define BTUSB_BOOTING 9
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200304
305struct btusb_data {
306 struct hci_dev *hdev;
307 struct usb_device *udev;
Marcel Holtmann5fbcd262008-09-23 00:16:36 +0200308 struct usb_interface *intf;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200309 struct usb_interface *isoc;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200310
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200311 unsigned long flags;
312
313 struct work_struct work;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200314 struct work_struct waker;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200315
Marcel Holtmann803b5832014-09-16 08:00:29 +0200316 struct usb_anchor deferred;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200317 struct usb_anchor tx_anchor;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200318 int tx_in_flight;
319 spinlock_t txlock;
320
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200321 struct usb_anchor intr_anchor;
322 struct usb_anchor bulk_anchor;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200323 struct usb_anchor isoc_anchor;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200324 spinlock_t rxlock;
325
326 struct sk_buff *evt_skb;
327 struct sk_buff *acl_skb;
328 struct sk_buff *sco_skb;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200329
330 struct usb_endpoint_descriptor *intr_ep;
331 struct usb_endpoint_descriptor *bulk_tx_ep;
332 struct usb_endpoint_descriptor *bulk_rx_ep;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200333 struct usb_endpoint_descriptor *isoc_tx_ep;
334 struct usb_endpoint_descriptor *isoc_rx_ep;
335
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100336 __u8 cmdreq_type;
Marcel Holtmann893ba542015-01-28 20:27:34 -0800337 __u8 cmdreq;
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100338
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100339 unsigned int sco_num;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200340 int isoc_altsetting;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +0100341 int suspend_count;
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +0100342
Marcel Holtmann97307f52015-01-12 13:51:10 -0800343 int (*recv_event)(struct hci_dev *hdev, struct sk_buff *skb);
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +0100344 int (*recv_bulk)(struct btusb_data *data, void *buffer, int count);
Kim, Ben Young Taeace31982015-02-15 23:06:14 +0000345
346 int (*setup_on_usb)(struct hci_dev *hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200347};
348
Marcel Holtmann803b5832014-09-16 08:00:29 +0200349static inline void btusb_free_frags(struct btusb_data *data)
350{
351 unsigned long flags;
352
353 spin_lock_irqsave(&data->rxlock, flags);
354
355 kfree_skb(data->evt_skb);
356 data->evt_skb = NULL;
357
358 kfree_skb(data->acl_skb);
359 data->acl_skb = NULL;
360
361 kfree_skb(data->sco_skb);
362 data->sco_skb = NULL;
363
364 spin_unlock_irqrestore(&data->rxlock, flags);
365}
366
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200367static int btusb_recv_intr(struct btusb_data *data, void *buffer, int count)
368{
Marcel Holtmann803b5832014-09-16 08:00:29 +0200369 struct sk_buff *skb;
370 int err = 0;
371
372 spin_lock(&data->rxlock);
373 skb = data->evt_skb;
374
375 while (count) {
376 int len;
377
378 if (!skb) {
379 skb = bt_skb_alloc(HCI_MAX_EVENT_SIZE, GFP_ATOMIC);
380 if (!skb) {
381 err = -ENOMEM;
382 break;
383 }
384
385 bt_cb(skb)->pkt_type = HCI_EVENT_PKT;
386 bt_cb(skb)->expect = HCI_EVENT_HDR_SIZE;
387 }
388
389 len = min_t(uint, bt_cb(skb)->expect, count);
390 memcpy(skb_put(skb, len), buffer, len);
391
392 count -= len;
393 buffer += len;
394 bt_cb(skb)->expect -= len;
395
396 if (skb->len == HCI_EVENT_HDR_SIZE) {
397 /* Complete event header */
398 bt_cb(skb)->expect = hci_event_hdr(skb)->plen;
399
400 if (skb_tailroom(skb) < bt_cb(skb)->expect) {
401 kfree_skb(skb);
402 skb = NULL;
403
404 err = -EILSEQ;
405 break;
406 }
407 }
408
409 if (bt_cb(skb)->expect == 0) {
410 /* Complete frame */
Marcel Holtmann97307f52015-01-12 13:51:10 -0800411 data->recv_event(data->hdev, skb);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200412 skb = NULL;
413 }
414 }
415
416 data->evt_skb = skb;
417 spin_unlock(&data->rxlock);
418
419 return err;
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200420}
421
422static int btusb_recv_bulk(struct btusb_data *data, void *buffer, int count)
423{
Marcel Holtmann803b5832014-09-16 08:00:29 +0200424 struct sk_buff *skb;
425 int err = 0;
426
427 spin_lock(&data->rxlock);
428 skb = data->acl_skb;
429
430 while (count) {
431 int len;
432
433 if (!skb) {
434 skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
435 if (!skb) {
436 err = -ENOMEM;
437 break;
438 }
439
440 bt_cb(skb)->pkt_type = HCI_ACLDATA_PKT;
441 bt_cb(skb)->expect = HCI_ACL_HDR_SIZE;
442 }
443
444 len = min_t(uint, bt_cb(skb)->expect, count);
445 memcpy(skb_put(skb, len), buffer, len);
446
447 count -= len;
448 buffer += len;
449 bt_cb(skb)->expect -= len;
450
451 if (skb->len == HCI_ACL_HDR_SIZE) {
452 __le16 dlen = hci_acl_hdr(skb)->dlen;
453
454 /* Complete ACL header */
455 bt_cb(skb)->expect = __le16_to_cpu(dlen);
456
457 if (skb_tailroom(skb) < bt_cb(skb)->expect) {
458 kfree_skb(skb);
459 skb = NULL;
460
461 err = -EILSEQ;
462 break;
463 }
464 }
465
466 if (bt_cb(skb)->expect == 0) {
467 /* Complete frame */
468 hci_recv_frame(data->hdev, skb);
469 skb = NULL;
470 }
471 }
472
473 data->acl_skb = skb;
474 spin_unlock(&data->rxlock);
475
476 return err;
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200477}
478
479static int btusb_recv_isoc(struct btusb_data *data, void *buffer, int count)
480{
Marcel Holtmann803b5832014-09-16 08:00:29 +0200481 struct sk_buff *skb;
482 int err = 0;
483
484 spin_lock(&data->rxlock);
485 skb = data->sco_skb;
486
487 while (count) {
488 int len;
489
490 if (!skb) {
491 skb = bt_skb_alloc(HCI_MAX_SCO_SIZE, GFP_ATOMIC);
492 if (!skb) {
493 err = -ENOMEM;
494 break;
495 }
496
497 bt_cb(skb)->pkt_type = HCI_SCODATA_PKT;
498 bt_cb(skb)->expect = HCI_SCO_HDR_SIZE;
499 }
500
501 len = min_t(uint, bt_cb(skb)->expect, count);
502 memcpy(skb_put(skb, len), buffer, len);
503
504 count -= len;
505 buffer += len;
506 bt_cb(skb)->expect -= len;
507
508 if (skb->len == HCI_SCO_HDR_SIZE) {
509 /* Complete SCO header */
510 bt_cb(skb)->expect = hci_sco_hdr(skb)->dlen;
511
512 if (skb_tailroom(skb) < bt_cb(skb)->expect) {
513 kfree_skb(skb);
514 skb = NULL;
515
516 err = -EILSEQ;
517 break;
518 }
519 }
520
521 if (bt_cb(skb)->expect == 0) {
522 /* Complete frame */
523 hci_recv_frame(data->hdev, skb);
524 skb = NULL;
525 }
526 }
527
528 data->sco_skb = skb;
529 spin_unlock(&data->rxlock);
530
531 return err;
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200532}
533
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200534static void btusb_intr_complete(struct urb *urb)
535{
536 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +0100537 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200538 int err;
539
Marcel Holtmann89e75332014-09-16 04:44:50 +0200540 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
541 urb->actual_length);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200542
543 if (!test_bit(HCI_RUNNING, &hdev->flags))
544 return;
545
546 if (urb->status == 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200547 hdev->stat.byte_rx += urb->actual_length;
548
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200549 if (btusb_recv_intr(data, urb->transfer_buffer,
550 urb->actual_length) < 0) {
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200551 BT_ERR("%s corrupted event packet", hdev->name);
552 hdev->stat.err_rx++;
553 }
Champion Chen85560c42014-09-06 14:06:08 -0500554 } else if (urb->status == -ENOENT) {
555 /* Avoid suspend failed when usb_kill_urb */
556 return;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200557 }
558
559 if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
560 return;
561
Oliver Neukum7bee5492009-08-24 23:44:59 +0200562 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200563 usb_anchor_urb(urb, &data->intr_anchor);
564
565 err = usb_submit_urb(urb, GFP_ATOMIC);
566 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200567 /* -EPERM: urb is being killed;
568 * -ENODEV: device got disconnected */
569 if (err != -EPERM && err != -ENODEV)
Stefan Seyfried61faddf2010-11-30 21:49:08 +0100570 BT_ERR("%s urb %p failed to resubmit (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200571 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200572 usb_unanchor_urb(urb);
573 }
574}
575
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100576static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200577{
David Herrmann155961e2012-02-09 21:58:32 +0100578 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200579 struct urb *urb;
580 unsigned char *buf;
581 unsigned int pipe;
582 int err, size;
583
584 BT_DBG("%s", hdev->name);
585
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200586 if (!data->intr_ep)
587 return -ENODEV;
588
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100589 urb = usb_alloc_urb(0, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200590 if (!urb)
591 return -ENOMEM;
592
593 size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
594
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100595 buf = kmalloc(size, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200596 if (!buf) {
597 usb_free_urb(urb);
598 return -ENOMEM;
599 }
600
601 pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
602
603 usb_fill_int_urb(urb, data->udev, pipe, buf, size,
Marcel Holtmann89e75332014-09-16 04:44:50 +0200604 btusb_intr_complete, hdev, data->intr_ep->bInterval);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200605
606 urb->transfer_flags |= URB_FREE_BUFFER;
607
608 usb_anchor_urb(urb, &data->intr_anchor);
609
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100610 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200611 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +0200612 if (err != -EPERM && err != -ENODEV)
613 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200614 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200615 usb_unanchor_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200616 }
617
618 usb_free_urb(urb);
619
620 return err;
621}
622
623static void btusb_bulk_complete(struct urb *urb)
624{
625 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +0100626 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200627 int err;
628
Marcel Holtmann89e75332014-09-16 04:44:50 +0200629 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
630 urb->actual_length);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200631
632 if (!test_bit(HCI_RUNNING, &hdev->flags))
633 return;
634
635 if (urb->status == 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200636 hdev->stat.byte_rx += urb->actual_length;
637
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +0100638 if (data->recv_bulk(data, urb->transfer_buffer,
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200639 urb->actual_length) < 0) {
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200640 BT_ERR("%s corrupted ACL packet", hdev->name);
641 hdev->stat.err_rx++;
642 }
Champion Chen85560c42014-09-06 14:06:08 -0500643 } else if (urb->status == -ENOENT) {
644 /* Avoid suspend failed when usb_kill_urb */
645 return;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200646 }
647
648 if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
649 return;
650
651 usb_anchor_urb(urb, &data->bulk_anchor);
Oliver Neukum652fd782009-12-16 19:23:43 +0100652 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200653
654 err = usb_submit_urb(urb, GFP_ATOMIC);
655 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200656 /* -EPERM: urb is being killed;
657 * -ENODEV: device got disconnected */
658 if (err != -EPERM && err != -ENODEV)
Stefan Seyfried61faddf2010-11-30 21:49:08 +0100659 BT_ERR("%s urb %p failed to resubmit (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200660 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200661 usb_unanchor_urb(urb);
662 }
663}
664
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100665static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200666{
David Herrmann155961e2012-02-09 21:58:32 +0100667 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200668 struct urb *urb;
669 unsigned char *buf;
670 unsigned int pipe;
Vikram Kandukuri290ba202009-07-02 14:31:59 +0530671 int err, size = HCI_MAX_FRAME_SIZE;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200672
673 BT_DBG("%s", hdev->name);
674
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200675 if (!data->bulk_rx_ep)
676 return -ENODEV;
677
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100678 urb = usb_alloc_urb(0, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200679 if (!urb)
680 return -ENOMEM;
681
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100682 buf = kmalloc(size, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200683 if (!buf) {
684 usb_free_urb(urb);
685 return -ENOMEM;
686 }
687
688 pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
689
Marcel Holtmann89e75332014-09-16 04:44:50 +0200690 usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
691 btusb_bulk_complete, hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200692
693 urb->transfer_flags |= URB_FREE_BUFFER;
694
Oliver Neukum7bee5492009-08-24 23:44:59 +0200695 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200696 usb_anchor_urb(urb, &data->bulk_anchor);
697
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100698 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200699 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +0200700 if (err != -EPERM && err != -ENODEV)
701 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200702 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200703 usb_unanchor_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200704 }
705
706 usb_free_urb(urb);
707
708 return err;
709}
710
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200711static void btusb_isoc_complete(struct urb *urb)
712{
713 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +0100714 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200715 int i, err;
716
Marcel Holtmann89e75332014-09-16 04:44:50 +0200717 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
718 urb->actual_length);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200719
720 if (!test_bit(HCI_RUNNING, &hdev->flags))
721 return;
722
723 if (urb->status == 0) {
724 for (i = 0; i < urb->number_of_packets; i++) {
725 unsigned int offset = urb->iso_frame_desc[i].offset;
726 unsigned int length = urb->iso_frame_desc[i].actual_length;
727
728 if (urb->iso_frame_desc[i].status)
729 continue;
730
731 hdev->stat.byte_rx += length;
732
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200733 if (btusb_recv_isoc(data, urb->transfer_buffer + offset,
734 length) < 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200735 BT_ERR("%s corrupted SCO packet", hdev->name);
736 hdev->stat.err_rx++;
737 }
738 }
Champion Chen85560c42014-09-06 14:06:08 -0500739 } else if (urb->status == -ENOENT) {
740 /* Avoid suspend failed when usb_kill_urb */
741 return;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200742 }
743
744 if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags))
745 return;
746
747 usb_anchor_urb(urb, &data->isoc_anchor);
748
749 err = usb_submit_urb(urb, GFP_ATOMIC);
750 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200751 /* -EPERM: urb is being killed;
752 * -ENODEV: device got disconnected */
753 if (err != -EPERM && err != -ENODEV)
Stefan Seyfried61faddf2010-11-30 21:49:08 +0100754 BT_ERR("%s urb %p failed to resubmit (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200755 hdev->name, urb, -err);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200756 usb_unanchor_urb(urb);
757 }
758}
759
Jesper Juhl42b16b32011-01-17 00:09:38 +0100760static inline void __fill_isoc_descriptor(struct urb *urb, int len, int mtu)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200761{
762 int i, offset = 0;
763
764 BT_DBG("len %d mtu %d", len, mtu);
765
766 for (i = 0; i < BTUSB_MAX_ISOC_FRAMES && len >= mtu;
767 i++, offset += mtu, len -= mtu) {
768 urb->iso_frame_desc[i].offset = offset;
769 urb->iso_frame_desc[i].length = mtu;
770 }
771
772 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
773 urb->iso_frame_desc[i].offset = offset;
774 urb->iso_frame_desc[i].length = len;
775 i++;
776 }
777
778 urb->number_of_packets = i;
779}
780
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100781static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200782{
David Herrmann155961e2012-02-09 21:58:32 +0100783 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200784 struct urb *urb;
785 unsigned char *buf;
786 unsigned int pipe;
787 int err, size;
788
789 BT_DBG("%s", hdev->name);
790
791 if (!data->isoc_rx_ep)
792 return -ENODEV;
793
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100794 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200795 if (!urb)
796 return -ENOMEM;
797
798 size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) *
799 BTUSB_MAX_ISOC_FRAMES;
800
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100801 buf = kmalloc(size, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200802 if (!buf) {
803 usb_free_urb(urb);
804 return -ENOMEM;
805 }
806
807 pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress);
808
Bing Zhaofa0fb932011-12-20 18:19:00 -0800809 usb_fill_int_urb(urb, data->udev, pipe, buf, size, btusb_isoc_complete,
Marcel Holtmann89e75332014-09-16 04:44:50 +0200810 hdev, data->isoc_rx_ep->bInterval);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200811
Marcel Holtmann89e75332014-09-16 04:44:50 +0200812 urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200813
814 __fill_isoc_descriptor(urb, size,
Marcel Holtmann89e75332014-09-16 04:44:50 +0200815 le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize));
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200816
817 usb_anchor_urb(urb, &data->isoc_anchor);
818
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100819 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200820 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +0200821 if (err != -EPERM && err != -ENODEV)
822 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200823 hdev->name, urb, -err);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200824 usb_unanchor_urb(urb);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200825 }
826
827 usb_free_urb(urb);
828
829 return err;
830}
831
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200832static void btusb_tx_complete(struct urb *urb)
833{
834 struct sk_buff *skb = urb->context;
Marcel Holtmann89e75332014-09-16 04:44:50 +0200835 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
David Herrmann155961e2012-02-09 21:58:32 +0100836 struct btusb_data *data = hci_get_drvdata(hdev);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200837
Marcel Holtmann89e75332014-09-16 04:44:50 +0200838 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
839 urb->actual_length);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200840
841 if (!test_bit(HCI_RUNNING, &hdev->flags))
842 goto done;
843
844 if (!urb->status)
845 hdev->stat.byte_tx += urb->transfer_buffer_length;
846 else
847 hdev->stat.err_tx++;
848
849done:
850 spin_lock(&data->txlock);
851 data->tx_in_flight--;
852 spin_unlock(&data->txlock);
853
854 kfree(urb->setup_packet);
855
856 kfree_skb(skb);
857}
858
859static void btusb_isoc_tx_complete(struct urb *urb)
860{
861 struct sk_buff *skb = urb->context;
Marcel Holtmann89e75332014-09-16 04:44:50 +0200862 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200863
Marcel Holtmann89e75332014-09-16 04:44:50 +0200864 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
865 urb->actual_length);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200866
867 if (!test_bit(HCI_RUNNING, &hdev->flags))
868 goto done;
869
870 if (!urb->status)
871 hdev->stat.byte_tx += urb->transfer_buffer_length;
872 else
873 hdev->stat.err_tx++;
874
875done:
876 kfree(urb->setup_packet);
877
878 kfree_skb(skb);
879}
880
881static int btusb_open(struct hci_dev *hdev)
882{
David Herrmann155961e2012-02-09 21:58:32 +0100883 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200884 int err;
885
886 BT_DBG("%s", hdev->name);
887
Kim, Ben Young Taeace31982015-02-15 23:06:14 +0000888 /* Patching USB firmware files prior to starting any URBs of HCI path
889 * It is more safe to use USB bulk channel for downloading USB patch
890 */
891 if (data->setup_on_usb) {
892 err = data->setup_on_usb(hdev);
893 if (err <0)
894 return err;
895 }
896
Oliver Neukum7bee5492009-08-24 23:44:59 +0200897 err = usb_autopm_get_interface(data->intf);
898 if (err < 0)
899 return err;
900
901 data->intf->needs_remote_wakeup = 1;
902
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200903 if (test_and_set_bit(HCI_RUNNING, &hdev->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +0200904 goto done;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200905
906 if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +0200907 goto done;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200908
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100909 err = btusb_submit_intr_urb(hdev, GFP_KERNEL);
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100910 if (err < 0)
911 goto failed;
912
913 err = btusb_submit_bulk_urb(hdev, GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200914 if (err < 0) {
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100915 usb_kill_anchored_urbs(&data->intr_anchor);
916 goto failed;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200917 }
918
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100919 set_bit(BTUSB_BULK_RUNNING, &data->flags);
920 btusb_submit_bulk_urb(hdev, GFP_KERNEL);
921
Oliver Neukum7bee5492009-08-24 23:44:59 +0200922done:
923 usb_autopm_put_interface(data->intf);
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100924 return 0;
925
926failed:
927 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
928 clear_bit(HCI_RUNNING, &hdev->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200929 usb_autopm_put_interface(data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200930 return err;
931}
932
Oliver Neukum7bee5492009-08-24 23:44:59 +0200933static void btusb_stop_traffic(struct btusb_data *data)
934{
935 usb_kill_anchored_urbs(&data->intr_anchor);
936 usb_kill_anchored_urbs(&data->bulk_anchor);
937 usb_kill_anchored_urbs(&data->isoc_anchor);
938}
939
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200940static int btusb_close(struct hci_dev *hdev)
941{
David Herrmann155961e2012-02-09 21:58:32 +0100942 struct btusb_data *data = hci_get_drvdata(hdev);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200943 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200944
945 BT_DBG("%s", hdev->name);
946
947 if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
948 return 0;
949
Marcel Holtmanne8c3c3d2008-09-23 00:16:36 +0200950 cancel_work_sync(&data->work);
Linus Torvalds404291a2009-11-11 13:32:29 -0800951 cancel_work_sync(&data->waker);
Marcel Holtmanne8c3c3d2008-09-23 00:16:36 +0200952
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200953 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200954 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200955 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200956
957 btusb_stop_traffic(data);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200958 btusb_free_frags(data);
959
Oliver Neukum7bee5492009-08-24 23:44:59 +0200960 err = usb_autopm_get_interface(data->intf);
961 if (err < 0)
Oliver Neukum7b8e2c12009-11-13 14:26:23 +0100962 goto failed;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200963
964 data->intf->needs_remote_wakeup = 0;
965 usb_autopm_put_interface(data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200966
Oliver Neukum7b8e2c12009-11-13 14:26:23 +0100967failed:
968 usb_scuttle_anchored_urbs(&data->deferred);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200969 return 0;
970}
971
972static int btusb_flush(struct hci_dev *hdev)
973{
David Herrmann155961e2012-02-09 21:58:32 +0100974 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200975
976 BT_DBG("%s", hdev->name);
977
978 usb_kill_anchored_urbs(&data->tx_anchor);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200979 btusb_free_frags(data);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200980
981 return 0;
982}
983
Marcel Holtmann047b2ec2014-09-14 09:11:06 +0200984static struct urb *alloc_ctrl_urb(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200985{
David Herrmann155961e2012-02-09 21:58:32 +0100986 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200987 struct usb_ctrlrequest *dr;
988 struct urb *urb;
989 unsigned int pipe;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200990
Marcel Holtmann047b2ec2014-09-14 09:11:06 +0200991 urb = usb_alloc_urb(0, GFP_KERNEL);
992 if (!urb)
993 return ERR_PTR(-ENOMEM);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200994
Marcel Holtmann047b2ec2014-09-14 09:11:06 +0200995 dr = kmalloc(sizeof(*dr), GFP_KERNEL);
996 if (!dr) {
997 usb_free_urb(urb);
998 return ERR_PTR(-ENOMEM);
999 }
1000
1001 dr->bRequestType = data->cmdreq_type;
Marcel Holtmann893ba542015-01-28 20:27:34 -08001002 dr->bRequest = data->cmdreq;
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001003 dr->wIndex = 0;
1004 dr->wValue = 0;
1005 dr->wLength = __cpu_to_le16(skb->len);
1006
1007 pipe = usb_sndctrlpipe(data->udev, 0x00);
1008
Marcel Holtmann89e75332014-09-16 04:44:50 +02001009 usb_fill_control_urb(urb, data->udev, pipe, (void *)dr,
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001010 skb->data, skb->len, btusb_tx_complete, skb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001011
Marcel Holtmann89e75332014-09-16 04:44:50 +02001012 skb->dev = (void *)hdev;
Marcel Holtmann7bd8f092013-10-11 06:19:18 -07001013
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001014 return urb;
1015}
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001016
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001017static struct urb *alloc_bulk_urb(struct hci_dev *hdev, struct sk_buff *skb)
1018{
1019 struct btusb_data *data = hci_get_drvdata(hdev);
1020 struct urb *urb;
1021 unsigned int pipe;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001022
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001023 if (!data->bulk_tx_ep)
1024 return ERR_PTR(-ENODEV);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001025
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001026 urb = usb_alloc_urb(0, GFP_KERNEL);
1027 if (!urb)
1028 return ERR_PTR(-ENOMEM);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001029
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001030 pipe = usb_sndbulkpipe(data->udev, data->bulk_tx_ep->bEndpointAddress);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001031
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001032 usb_fill_bulk_urb(urb, data->udev, pipe,
1033 skb->data, skb->len, btusb_tx_complete, skb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001034
Marcel Holtmann89e75332014-09-16 04:44:50 +02001035 skb->dev = (void *)hdev;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001036
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001037 return urb;
1038}
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001039
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001040static struct urb *alloc_isoc_urb(struct hci_dev *hdev, struct sk_buff *skb)
1041{
1042 struct btusb_data *data = hci_get_drvdata(hdev);
1043 struct urb *urb;
1044 unsigned int pipe;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001045
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001046 if (!data->isoc_tx_ep)
1047 return ERR_PTR(-ENODEV);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001048
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001049 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_KERNEL);
1050 if (!urb)
1051 return ERR_PTR(-ENOMEM);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001052
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001053 pipe = usb_sndisocpipe(data->udev, data->isoc_tx_ep->bEndpointAddress);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001054
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001055 usb_fill_int_urb(urb, data->udev, pipe,
1056 skb->data, skb->len, btusb_isoc_tx_complete,
1057 skb, data->isoc_tx_ep->bInterval);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001058
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001059 urb->transfer_flags = URB_ISO_ASAP;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001060
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001061 __fill_isoc_descriptor(urb, skb->len,
1062 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001063
Marcel Holtmann89e75332014-09-16 04:44:50 +02001064 skb->dev = (void *)hdev;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001065
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001066 return urb;
1067}
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001068
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001069static int submit_tx_urb(struct hci_dev *hdev, struct urb *urb)
1070{
1071 struct btusb_data *data = hci_get_drvdata(hdev);
1072 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001073
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001074 usb_anchor_urb(urb, &data->tx_anchor);
1075
Johan Hedberge9753ef2014-09-14 08:49:34 +03001076 err = usb_submit_urb(urb, GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001077 if (err < 0) {
Paul Bolle5a9b80e2011-10-09 12:12:16 +02001078 if (err != -EPERM && err != -ENODEV)
1079 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +02001080 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001081 kfree(urb->setup_packet);
1082 usb_unanchor_urb(urb);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001083 } else {
1084 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001085 }
1086
Cong Wang54a8a792011-11-22 09:32:57 +08001087 usb_free_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001088 return err;
1089}
1090
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001091static int submit_or_queue_tx_urb(struct hci_dev *hdev, struct urb *urb)
1092{
1093 struct btusb_data *data = hci_get_drvdata(hdev);
1094 unsigned long flags;
1095 bool suspending;
1096
1097 spin_lock_irqsave(&data->txlock, flags);
1098 suspending = test_bit(BTUSB_SUSPENDING, &data->flags);
1099 if (!suspending)
1100 data->tx_in_flight++;
1101 spin_unlock_irqrestore(&data->txlock, flags);
1102
1103 if (!suspending)
1104 return submit_tx_urb(hdev, urb);
1105
1106 usb_anchor_urb(urb, &data->deferred);
1107 schedule_work(&data->waker);
1108
1109 usb_free_urb(urb);
1110 return 0;
1111}
1112
1113static int btusb_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
1114{
1115 struct urb *urb;
1116
1117 BT_DBG("%s", hdev->name);
1118
1119 if (!test_bit(HCI_RUNNING, &hdev->flags))
1120 return -EBUSY;
1121
1122 switch (bt_cb(skb)->pkt_type) {
1123 case HCI_COMMAND_PKT:
1124 urb = alloc_ctrl_urb(hdev, skb);
1125 if (IS_ERR(urb))
1126 return PTR_ERR(urb);
1127
1128 hdev->stat.cmd_tx++;
1129 return submit_or_queue_tx_urb(hdev, urb);
1130
1131 case HCI_ACLDATA_PKT:
1132 urb = alloc_bulk_urb(hdev, skb);
1133 if (IS_ERR(urb))
1134 return PTR_ERR(urb);
1135
1136 hdev->stat.acl_tx++;
1137 return submit_or_queue_tx_urb(hdev, urb);
1138
1139 case HCI_SCODATA_PKT:
1140 if (hci_conn_num(hdev, SCO_LINK) < 1)
1141 return -ENODEV;
1142
1143 urb = alloc_isoc_urb(hdev, skb);
1144 if (IS_ERR(urb))
1145 return PTR_ERR(urb);
1146
1147 hdev->stat.sco_tx++;
1148 return submit_tx_urb(hdev, urb);
1149 }
1150
1151 return -EILSEQ;
1152}
1153
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001154static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
1155{
David Herrmann155961e2012-02-09 21:58:32 +01001156 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001157
1158 BT_DBG("%s evt %d", hdev->name, evt);
1159
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001160 if (hci_conn_num(hdev, SCO_LINK) != data->sco_num) {
1161 data->sco_num = hci_conn_num(hdev, SCO_LINK);
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001162 schedule_work(&data->work);
Marcel Holtmanna780efa2008-11-30 12:17:12 +01001163 }
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001164}
1165
Jesper Juhl42b16b32011-01-17 00:09:38 +01001166static inline int __set_isoc_interface(struct hci_dev *hdev, int altsetting)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001167{
David Herrmann155961e2012-02-09 21:58:32 +01001168 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001169 struct usb_interface *intf = data->isoc;
1170 struct usb_endpoint_descriptor *ep_desc;
1171 int i, err;
1172
1173 if (!data->isoc)
1174 return -ENODEV;
1175
1176 err = usb_set_interface(data->udev, 1, altsetting);
1177 if (err < 0) {
1178 BT_ERR("%s setting interface failed (%d)", hdev->name, -err);
1179 return err;
1180 }
1181
1182 data->isoc_altsetting = altsetting;
1183
1184 data->isoc_tx_ep = NULL;
1185 data->isoc_rx_ep = NULL;
1186
1187 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
1188 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
1189
1190 if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) {
1191 data->isoc_tx_ep = ep_desc;
1192 continue;
1193 }
1194
1195 if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) {
1196 data->isoc_rx_ep = ep_desc;
1197 continue;
1198 }
1199 }
1200
1201 if (!data->isoc_tx_ep || !data->isoc_rx_ep) {
1202 BT_ERR("%s invalid SCO descriptors", hdev->name);
1203 return -ENODEV;
1204 }
1205
1206 return 0;
1207}
1208
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001209static void btusb_work(struct work_struct *work)
1210{
1211 struct btusb_data *data = container_of(work, struct btusb_data, work);
1212 struct hci_dev *hdev = data->hdev;
Mikel Astizf4001d22012-04-11 08:48:51 +02001213 int new_alts;
Oliver Neukum7bee5492009-08-24 23:44:59 +02001214 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001215
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001216 if (data->sco_num > 0) {
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -03001217 if (!test_bit(BTUSB_DID_ISO_RESUME, &data->flags)) {
Oliver Neukum8efdd0c2011-02-11 13:00:06 +01001218 err = usb_autopm_get_interface(data->isoc ? data->isoc : data->intf);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001219 if (err < 0) {
1220 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1221 usb_kill_anchored_urbs(&data->isoc_anchor);
1222 return;
1223 }
1224
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -03001225 set_bit(BTUSB_DID_ISO_RESUME, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001226 }
Mikel Astizf4001d22012-04-11 08:48:51 +02001227
1228 if (hdev->voice_setting & 0x0020) {
1229 static const int alts[3] = { 2, 4, 5 };
Marcel Holtmann89e75332014-09-16 04:44:50 +02001230
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001231 new_alts = alts[data->sco_num - 1];
Mikel Astizf4001d22012-04-11 08:48:51 +02001232 } else {
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001233 new_alts = data->sco_num;
Mikel Astizf4001d22012-04-11 08:48:51 +02001234 }
1235
1236 if (data->isoc_altsetting != new_alts) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001237 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1238 usb_kill_anchored_urbs(&data->isoc_anchor);
1239
Mikel Astizf4001d22012-04-11 08:48:51 +02001240 if (__set_isoc_interface(hdev, new_alts) < 0)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001241 return;
1242 }
1243
1244 if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001245 if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001246 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1247 else
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001248 btusb_submit_isoc_urb(hdev, GFP_KERNEL);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001249 }
1250 } else {
1251 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1252 usb_kill_anchored_urbs(&data->isoc_anchor);
1253
1254 __set_isoc_interface(hdev, 0);
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -03001255 if (test_and_clear_bit(BTUSB_DID_ISO_RESUME, &data->flags))
Oliver Neukum8efdd0c2011-02-11 13:00:06 +01001256 usb_autopm_put_interface(data->isoc ? data->isoc : data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001257 }
1258}
1259
Oliver Neukum7bee5492009-08-24 23:44:59 +02001260static void btusb_waker(struct work_struct *work)
1261{
1262 struct btusb_data *data = container_of(work, struct btusb_data, waker);
1263 int err;
1264
1265 err = usb_autopm_get_interface(data->intf);
1266 if (err < 0)
1267 return;
1268
1269 usb_autopm_put_interface(data->intf);
1270}
1271
Daniel Drake6576fe42015-02-23 18:16:47 -06001272static struct sk_buff *btusb_read_local_version(struct hci_dev *hdev)
1273{
1274 struct sk_buff *skb;
1275
1276 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
1277 HCI_INIT_TIMEOUT);
1278 if (IS_ERR(skb)) {
1279 BT_ERR("%s: HCI_OP_READ_LOCAL_VERSION failed (%ld)",
1280 hdev->name, PTR_ERR(skb));
1281 return skb;
1282 }
1283
1284 if (skb->len != sizeof(struct hci_rp_read_local_version)) {
1285 BT_ERR("%s: HCI_OP_READ_LOCAL_VERSION event length mismatch",
1286 hdev->name);
1287 kfree_skb(skb);
1288 return ERR_PTR(-EIO);
1289 }
1290
1291 return skb;
1292}
1293
Marcel Holtmann9f8f9622013-04-10 08:11:35 -07001294static int btusb_setup_bcm92035(struct hci_dev *hdev)
1295{
1296 struct sk_buff *skb;
1297 u8 val = 0x00;
1298
1299 BT_DBG("%s", hdev->name);
1300
1301 skb = __hci_cmd_sync(hdev, 0xfc3b, 1, &val, HCI_INIT_TIMEOUT);
1302 if (IS_ERR(skb))
1303 BT_ERR("BCM92035 command failed (%ld)", -PTR_ERR(skb));
1304 else
1305 kfree_skb(skb);
1306
1307 return 0;
1308}
1309
Marcel Holtmann81cac642014-01-03 03:02:36 -08001310static int btusb_setup_csr(struct hci_dev *hdev)
1311{
1312 struct hci_rp_read_local_version *rp;
1313 struct sk_buff *skb;
1314 int ret;
1315
1316 BT_DBG("%s", hdev->name);
1317
Daniel Drake6576fe42015-02-23 18:16:47 -06001318 skb = btusb_read_local_version(hdev);
1319 if (IS_ERR(skb))
Marcel Holtmann81cac642014-01-03 03:02:36 -08001320 return -PTR_ERR(skb);
Marcel Holtmann81cac642014-01-03 03:02:36 -08001321
Marcel Holtmann89e75332014-09-16 04:44:50 +02001322 rp = (struct hci_rp_read_local_version *)skb->data;
Marcel Holtmann81cac642014-01-03 03:02:36 -08001323
1324 if (!rp->status) {
1325 if (le16_to_cpu(rp->manufacturer) != 10) {
1326 /* Clear the reset quirk since this is not an actual
1327 * early Bluetooth 1.1 device from CSR.
1328 */
1329 clear_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
1330
1331 /* These fake CSR controllers have all a broken
1332 * stored link key handling and so just disable it.
1333 */
1334 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY,
1335 &hdev->quirks);
1336 }
1337 }
1338
1339 ret = -bt_to_errno(rp->status);
1340
1341 kfree_skb(skb);
1342
1343 return ret;
1344}
1345
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001346struct intel_version {
1347 u8 status;
1348 u8 hw_platform;
1349 u8 hw_variant;
1350 u8 hw_revision;
1351 u8 fw_variant;
1352 u8 fw_revision;
1353 u8 fw_build_num;
1354 u8 fw_build_ww;
1355 u8 fw_build_yy;
1356 u8 fw_patch_num;
1357} __packed;
1358
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001359struct intel_boot_params {
1360 __u8 status;
1361 __u8 otp_format;
1362 __u8 otp_content;
1363 __u8 otp_patch;
1364 __le16 dev_revid;
1365 __u8 secure_boot;
1366 __u8 key_from_hdr;
1367 __u8 key_type;
1368 __u8 otp_lock;
1369 __u8 api_lock;
1370 __u8 debug_lock;
1371 bdaddr_t otp_bdaddr;
1372 __u8 min_fw_build_nn;
1373 __u8 min_fw_build_cw;
1374 __u8 min_fw_build_yy;
1375 __u8 limited_cce;
1376 __u8 unlocked_state;
1377} __packed;
1378
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001379static const struct firmware *btusb_setup_intel_get_fw(struct hci_dev *hdev,
Marcel Holtmann89e75332014-09-16 04:44:50 +02001380 struct intel_version *ver)
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001381{
1382 const struct firmware *fw;
1383 char fwname[64];
1384 int ret;
1385
1386 snprintf(fwname, sizeof(fwname),
1387 "intel/ibt-hw-%x.%x.%x-fw-%x.%x.%x.%x.%x.bseq",
1388 ver->hw_platform, ver->hw_variant, ver->hw_revision,
1389 ver->fw_variant, ver->fw_revision, ver->fw_build_num,
1390 ver->fw_build_ww, ver->fw_build_yy);
1391
1392 ret = request_firmware(&fw, fwname, &hdev->dev);
1393 if (ret < 0) {
1394 if (ret == -EINVAL) {
1395 BT_ERR("%s Intel firmware file request failed (%d)",
1396 hdev->name, ret);
1397 return NULL;
1398 }
1399
1400 BT_ERR("%s failed to open Intel firmware file: %s(%d)",
1401 hdev->name, fwname, ret);
1402
1403 /* If the correct firmware patch file is not found, use the
1404 * default firmware patch file instead
1405 */
1406 snprintf(fwname, sizeof(fwname), "intel/ibt-hw-%x.%x.bseq",
1407 ver->hw_platform, ver->hw_variant);
1408 if (request_firmware(&fw, fwname, &hdev->dev) < 0) {
1409 BT_ERR("%s failed to open default Intel fw file: %s",
1410 hdev->name, fwname);
1411 return NULL;
1412 }
1413 }
1414
1415 BT_INFO("%s: Intel Bluetooth firmware file: %s", hdev->name, fwname);
1416
1417 return fw;
1418}
1419
1420static int btusb_setup_intel_patching(struct hci_dev *hdev,
1421 const struct firmware *fw,
1422 const u8 **fw_ptr, int *disable_patch)
1423{
1424 struct sk_buff *skb;
1425 struct hci_command_hdr *cmd;
1426 const u8 *cmd_param;
1427 struct hci_event_hdr *evt = NULL;
1428 const u8 *evt_param = NULL;
1429 int remain = fw->size - (*fw_ptr - fw->data);
1430
1431 /* The first byte indicates the types of the patch command or event.
1432 * 0x01 means HCI command and 0x02 is HCI event. If the first bytes
1433 * in the current firmware buffer doesn't start with 0x01 or
1434 * the size of remain buffer is smaller than HCI command header,
1435 * the firmware file is corrupted and it should stop the patching
1436 * process.
1437 */
1438 if (remain > HCI_COMMAND_HDR_SIZE && *fw_ptr[0] != 0x01) {
1439 BT_ERR("%s Intel fw corrupted: invalid cmd read", hdev->name);
1440 return -EINVAL;
1441 }
1442 (*fw_ptr)++;
1443 remain--;
1444
1445 cmd = (struct hci_command_hdr *)(*fw_ptr);
1446 *fw_ptr += sizeof(*cmd);
1447 remain -= sizeof(*cmd);
1448
1449 /* Ensure that the remain firmware data is long enough than the length
1450 * of command parameter. If not, the firmware file is corrupted.
1451 */
1452 if (remain < cmd->plen) {
1453 BT_ERR("%s Intel fw corrupted: invalid cmd len", hdev->name);
1454 return -EFAULT;
1455 }
1456
1457 /* If there is a command that loads a patch in the firmware
1458 * file, then enable the patch upon success, otherwise just
1459 * disable the manufacturer mode, for example patch activation
1460 * is not required when the default firmware patch file is used
1461 * because there are no patch data to load.
1462 */
1463 if (*disable_patch && le16_to_cpu(cmd->opcode) == 0xfc8e)
1464 *disable_patch = 0;
1465
1466 cmd_param = *fw_ptr;
1467 *fw_ptr += cmd->plen;
1468 remain -= cmd->plen;
1469
1470 /* This reads the expected events when the above command is sent to the
1471 * device. Some vendor commands expects more than one events, for
1472 * example command status event followed by vendor specific event.
1473 * For this case, it only keeps the last expected event. so the command
1474 * can be sent with __hci_cmd_sync_ev() which returns the sk_buff of
1475 * last expected event.
1476 */
1477 while (remain > HCI_EVENT_HDR_SIZE && *fw_ptr[0] == 0x02) {
1478 (*fw_ptr)++;
1479 remain--;
1480
1481 evt = (struct hci_event_hdr *)(*fw_ptr);
1482 *fw_ptr += sizeof(*evt);
1483 remain -= sizeof(*evt);
1484
1485 if (remain < evt->plen) {
1486 BT_ERR("%s Intel fw corrupted: invalid evt len",
1487 hdev->name);
1488 return -EFAULT;
1489 }
1490
1491 evt_param = *fw_ptr;
1492 *fw_ptr += evt->plen;
1493 remain -= evt->plen;
1494 }
1495
1496 /* Every HCI commands in the firmware file has its correspond event.
1497 * If event is not found or remain is smaller than zero, the firmware
1498 * file is corrupted.
1499 */
1500 if (!evt || !evt_param || remain < 0) {
1501 BT_ERR("%s Intel fw corrupted: invalid evt read", hdev->name);
1502 return -EFAULT;
1503 }
1504
1505 skb = __hci_cmd_sync_ev(hdev, le16_to_cpu(cmd->opcode), cmd->plen,
1506 cmd_param, evt->evt, HCI_INIT_TIMEOUT);
1507 if (IS_ERR(skb)) {
1508 BT_ERR("%s sending Intel patch command (0x%4.4x) failed (%ld)",
1509 hdev->name, cmd->opcode, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001510 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001511 }
1512
1513 /* It ensures that the returned event matches the event data read from
1514 * the firmware file. At fist, it checks the length and then
1515 * the contents of the event.
1516 */
1517 if (skb->len != evt->plen) {
1518 BT_ERR("%s mismatch event length (opcode 0x%4.4x)", hdev->name,
1519 le16_to_cpu(cmd->opcode));
1520 kfree_skb(skb);
1521 return -EFAULT;
1522 }
1523
1524 if (memcmp(skb->data, evt_param, evt->plen)) {
1525 BT_ERR("%s mismatch event parameter (opcode 0x%4.4x)",
1526 hdev->name, le16_to_cpu(cmd->opcode));
1527 kfree_skb(skb);
1528 return -EFAULT;
1529 }
1530 kfree_skb(skb);
1531
1532 return 0;
1533}
1534
Marcel Holtmann40cb0982014-07-02 12:06:45 +02001535#define BDADDR_INTEL (&(bdaddr_t) {{0x00, 0x8b, 0x9e, 0x19, 0x03, 0x00}})
1536
1537static int btusb_check_bdaddr_intel(struct hci_dev *hdev)
1538{
1539 struct sk_buff *skb;
1540 struct hci_rp_read_bd_addr *rp;
1541
1542 skb = __hci_cmd_sync(hdev, HCI_OP_READ_BD_ADDR, 0, NULL,
1543 HCI_INIT_TIMEOUT);
1544 if (IS_ERR(skb)) {
1545 BT_ERR("%s reading Intel device address failed (%ld)",
1546 hdev->name, PTR_ERR(skb));
1547 return PTR_ERR(skb);
1548 }
1549
1550 if (skb->len != sizeof(*rp)) {
1551 BT_ERR("%s Intel device address length mismatch", hdev->name);
1552 kfree_skb(skb);
1553 return -EIO;
1554 }
1555
Marcel Holtmann89e75332014-09-16 04:44:50 +02001556 rp = (struct hci_rp_read_bd_addr *)skb->data;
Marcel Holtmann40cb0982014-07-02 12:06:45 +02001557 if (rp->status) {
1558 BT_ERR("%s Intel device address result failed (%02x)",
1559 hdev->name, rp->status);
1560 kfree_skb(skb);
1561 return -bt_to_errno(rp->status);
1562 }
1563
1564 /* For some Intel based controllers, the default Bluetooth device
1565 * address 00:03:19:9E:8B:00 can be found. These controllers are
1566 * fully operational, but have the danger of duplicate addresses
1567 * and that in turn can cause problems with Bluetooth operation.
1568 */
Marcel Holtmann4739b5b2014-07-04 16:54:38 +02001569 if (!bacmp(&rp->bdaddr, BDADDR_INTEL)) {
Marcel Holtmann40cb0982014-07-02 12:06:45 +02001570 BT_ERR("%s found Intel default device address (%pMR)",
1571 hdev->name, &rp->bdaddr);
Marcel Holtmann4739b5b2014-07-04 16:54:38 +02001572 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
1573 }
Marcel Holtmann40cb0982014-07-02 12:06:45 +02001574
1575 kfree_skb(skb);
1576
1577 return 0;
1578}
1579
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001580static int btusb_setup_intel(struct hci_dev *hdev)
1581{
1582 struct sk_buff *skb;
1583 const struct firmware *fw;
1584 const u8 *fw_ptr;
1585 int disable_patch;
1586 struct intel_version *ver;
1587
1588 const u8 mfg_enable[] = { 0x01, 0x00 };
1589 const u8 mfg_disable[] = { 0x00, 0x00 };
1590 const u8 mfg_reset_deactivate[] = { 0x00, 0x01 };
1591 const u8 mfg_reset_activate[] = { 0x00, 0x02 };
1592
1593 BT_DBG("%s", hdev->name);
1594
1595 /* The controller has a bug with the first HCI command sent to it
1596 * returning number of completed commands as zero. This would stall the
1597 * command processing in the Bluetooth core.
1598 *
1599 * As a workaround, send HCI Reset command first which will reset the
1600 * number of completed commands and allow normal command processing
1601 * from now on.
1602 */
1603 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
1604 if (IS_ERR(skb)) {
1605 BT_ERR("%s sending initial HCI reset command failed (%ld)",
1606 hdev->name, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001607 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001608 }
1609 kfree_skb(skb);
1610
1611 /* Read Intel specific controller version first to allow selection of
1612 * which firmware file to load.
1613 *
1614 * The returned information are hardware variant and revision plus
1615 * firmware variant, revision and build number.
1616 */
1617 skb = __hci_cmd_sync(hdev, 0xfc05, 0, NULL, HCI_INIT_TIMEOUT);
1618 if (IS_ERR(skb)) {
1619 BT_ERR("%s reading Intel fw version command failed (%ld)",
1620 hdev->name, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001621 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001622 }
1623
1624 if (skb->len != sizeof(*ver)) {
1625 BT_ERR("%s Intel version event length mismatch", hdev->name);
1626 kfree_skb(skb);
1627 return -EIO;
1628 }
1629
1630 ver = (struct intel_version *)skb->data;
1631 if (ver->status) {
1632 BT_ERR("%s Intel fw version event failed (%02x)", hdev->name,
1633 ver->status);
1634 kfree_skb(skb);
1635 return -bt_to_errno(ver->status);
1636 }
1637
1638 BT_INFO("%s: read Intel version: %02x%02x%02x%02x%02x%02x%02x%02x%02x",
1639 hdev->name, ver->hw_platform, ver->hw_variant,
1640 ver->hw_revision, ver->fw_variant, ver->fw_revision,
1641 ver->fw_build_num, ver->fw_build_ww, ver->fw_build_yy,
1642 ver->fw_patch_num);
1643
1644 /* fw_patch_num indicates the version of patch the device currently
1645 * have. If there is no patch data in the device, it is always 0x00.
1646 * So, if it is other than 0x00, no need to patch the deivce again.
1647 */
1648 if (ver->fw_patch_num) {
1649 BT_INFO("%s: Intel device is already patched. patch num: %02x",
1650 hdev->name, ver->fw_patch_num);
1651 kfree_skb(skb);
Marcel Holtmann40cb0982014-07-02 12:06:45 +02001652 btusb_check_bdaddr_intel(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001653 return 0;
1654 }
1655
1656 /* Opens the firmware patch file based on the firmware version read
1657 * from the controller. If it fails to open the matching firmware
1658 * patch file, it tries to open the default firmware patch file.
1659 * If no patch file is found, allow the device to operate without
1660 * a patch.
1661 */
1662 fw = btusb_setup_intel_get_fw(hdev, ver);
1663 if (!fw) {
1664 kfree_skb(skb);
Marcel Holtmann40cb0982014-07-02 12:06:45 +02001665 btusb_check_bdaddr_intel(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001666 return 0;
1667 }
1668 fw_ptr = fw->data;
1669
1670 /* This Intel specific command enables the manufacturer mode of the
1671 * controller.
1672 *
1673 * Only while this mode is enabled, the driver can download the
1674 * firmware patch data and configuration parameters.
1675 */
1676 skb = __hci_cmd_sync(hdev, 0xfc11, 2, mfg_enable, HCI_INIT_TIMEOUT);
1677 if (IS_ERR(skb)) {
1678 BT_ERR("%s entering Intel manufacturer mode failed (%ld)",
1679 hdev->name, PTR_ERR(skb));
1680 release_firmware(fw);
Adam Leed9c78e92013-07-10 10:02:12 +08001681 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001682 }
1683
1684 if (skb->data[0]) {
1685 u8 evt_status = skb->data[0];
Marcel Holtmann89e75332014-09-16 04:44:50 +02001686
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001687 BT_ERR("%s enable Intel manufacturer mode event failed (%02x)",
1688 hdev->name, evt_status);
1689 kfree_skb(skb);
1690 release_firmware(fw);
1691 return -bt_to_errno(evt_status);
1692 }
1693 kfree_skb(skb);
1694
1695 disable_patch = 1;
1696
1697 /* The firmware data file consists of list of Intel specific HCI
1698 * commands and its expected events. The first byte indicates the
1699 * type of the message, either HCI command or HCI event.
1700 *
1701 * It reads the command and its expected event from the firmware file,
1702 * and send to the controller. Once __hci_cmd_sync_ev() returns,
1703 * the returned event is compared with the event read from the firmware
1704 * file and it will continue until all the messages are downloaded to
1705 * the controller.
1706 *
1707 * Once the firmware patching is completed successfully,
1708 * the manufacturer mode is disabled with reset and activating the
1709 * downloaded patch.
1710 *
1711 * If the firmware patching fails, the manufacturer mode is
1712 * disabled with reset and deactivating the patch.
1713 *
1714 * If the default patch file is used, no reset is done when disabling
1715 * the manufacturer.
1716 */
1717 while (fw->size > fw_ptr - fw->data) {
1718 int ret;
1719
1720 ret = btusb_setup_intel_patching(hdev, fw, &fw_ptr,
1721 &disable_patch);
1722 if (ret < 0)
1723 goto exit_mfg_deactivate;
1724 }
1725
1726 release_firmware(fw);
1727
1728 if (disable_patch)
1729 goto exit_mfg_disable;
1730
1731 /* Patching completed successfully and disable the manufacturer mode
1732 * with reset and activate the downloaded firmware patches.
1733 */
1734 skb = __hci_cmd_sync(hdev, 0xfc11, sizeof(mfg_reset_activate),
1735 mfg_reset_activate, HCI_INIT_TIMEOUT);
1736 if (IS_ERR(skb)) {
1737 BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
1738 hdev->name, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001739 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001740 }
1741 kfree_skb(skb);
1742
1743 BT_INFO("%s: Intel Bluetooth firmware patch completed and activated",
1744 hdev->name);
1745
Marcel Holtmann40cb0982014-07-02 12:06:45 +02001746 btusb_check_bdaddr_intel(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001747 return 0;
1748
1749exit_mfg_disable:
1750 /* Disable the manufacturer mode without reset */
1751 skb = __hci_cmd_sync(hdev, 0xfc11, sizeof(mfg_disable), mfg_disable,
1752 HCI_INIT_TIMEOUT);
1753 if (IS_ERR(skb)) {
1754 BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
1755 hdev->name, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001756 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001757 }
1758 kfree_skb(skb);
1759
1760 BT_INFO("%s: Intel Bluetooth firmware patch completed", hdev->name);
Marcel Holtmann40cb0982014-07-02 12:06:45 +02001761
1762 btusb_check_bdaddr_intel(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001763 return 0;
1764
1765exit_mfg_deactivate:
1766 release_firmware(fw);
1767
1768 /* Patching failed. Disable the manufacturer mode with reset and
1769 * deactivate the downloaded firmware patches.
1770 */
1771 skb = __hci_cmd_sync(hdev, 0xfc11, sizeof(mfg_reset_deactivate),
1772 mfg_reset_deactivate, HCI_INIT_TIMEOUT);
1773 if (IS_ERR(skb)) {
1774 BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
1775 hdev->name, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001776 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001777 }
1778 kfree_skb(skb);
1779
1780 BT_INFO("%s: Intel Bluetooth firmware patch completed and deactivated",
1781 hdev->name);
1782
Marcel Holtmann40cb0982014-07-02 12:06:45 +02001783 btusb_check_bdaddr_intel(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001784 return 0;
1785}
1786
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001787static int inject_cmd_complete(struct hci_dev *hdev, __u16 opcode)
1788{
1789 struct sk_buff *skb;
1790 struct hci_event_hdr *hdr;
1791 struct hci_ev_cmd_complete *evt;
1792
1793 skb = bt_skb_alloc(sizeof(*hdr) + sizeof(*evt) + 1, GFP_ATOMIC);
1794 if (!skb)
1795 return -ENOMEM;
1796
1797 hdr = (struct hci_event_hdr *)skb_put(skb, sizeof(*hdr));
1798 hdr->evt = HCI_EV_CMD_COMPLETE;
1799 hdr->plen = sizeof(*evt) + 1;
1800
1801 evt = (struct hci_ev_cmd_complete *)skb_put(skb, sizeof(*evt));
1802 evt->ncmd = 0x01;
1803 evt->opcode = cpu_to_le16(opcode);
1804
1805 *skb_put(skb, 1) = 0x00;
1806
1807 bt_cb(skb)->pkt_type = HCI_EVENT_PKT;
1808
1809 return hci_recv_frame(hdev, skb);
1810}
1811
1812static int btusb_recv_bulk_intel(struct btusb_data *data, void *buffer,
1813 int count)
1814{
1815 /* When the device is in bootloader mode, then it can send
1816 * events via the bulk endpoint. These events are treated the
1817 * same way as the ones received from the interrupt endpoint.
1818 */
1819 if (test_bit(BTUSB_BOOTLOADER, &data->flags))
1820 return btusb_recv_intr(data, buffer, count);
1821
1822 return btusb_recv_bulk(data, buffer, count);
1823}
1824
1825static int btusb_recv_event_intel(struct hci_dev *hdev, struct sk_buff *skb)
1826{
1827 struct btusb_data *data = hci_get_drvdata(hdev);
1828
1829 if (test_bit(BTUSB_BOOTLOADER, &data->flags)) {
1830 struct hci_event_hdr *hdr = (void *)skb->data;
1831
1832 /* When the firmware loading completes the device sends
1833 * out a vendor specific event indicating the result of
1834 * the firmware loading.
1835 */
1836 if (skb->len == 7 && hdr->evt == 0xff && hdr->plen == 0x05 &&
1837 skb->data[2] == 0x06) {
1838 if (skb->data[3] != 0x00)
1839 test_bit(BTUSB_FIRMWARE_FAILED, &data->flags);
1840
Marcel Holtmannce6bb922015-01-28 01:58:40 -08001841 if (test_and_clear_bit(BTUSB_DOWNLOADING,
1842 &data->flags) &&
Johan Hedberga087a982015-01-30 10:58:54 +02001843 test_bit(BTUSB_FIRMWARE_LOADED, &data->flags)) {
1844 smp_mb__after_atomic();
1845 wake_up_bit(&data->flags, BTUSB_DOWNLOADING);
1846 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001847 }
1848
1849 /* When switching to the operational firmware the device
1850 * sends a vendor specific event indicating that the bootup
1851 * completed.
1852 */
1853 if (skb->len == 9 && hdr->evt == 0xff && hdr->plen == 0x07 &&
1854 skb->data[2] == 0x02) {
Johan Hedbergfad70972015-01-30 10:58:55 +02001855 if (test_and_clear_bit(BTUSB_BOOTING, &data->flags)) {
1856 smp_mb__after_atomic();
1857 wake_up_bit(&data->flags, BTUSB_BOOTING);
1858 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001859 }
1860 }
1861
1862 return hci_recv_frame(hdev, skb);
1863}
1864
1865static int btusb_send_frame_intel(struct hci_dev *hdev, struct sk_buff *skb)
1866{
1867 struct btusb_data *data = hci_get_drvdata(hdev);
1868 struct urb *urb;
1869
1870 BT_DBG("%s", hdev->name);
1871
1872 if (!test_bit(HCI_RUNNING, &hdev->flags))
1873 return -EBUSY;
1874
1875 switch (bt_cb(skb)->pkt_type) {
1876 case HCI_COMMAND_PKT:
1877 if (test_bit(BTUSB_BOOTLOADER, &data->flags)) {
1878 struct hci_command_hdr *cmd = (void *)skb->data;
1879 __u16 opcode = le16_to_cpu(cmd->opcode);
1880
1881 /* When in bootloader mode and the command 0xfc09
1882 * is received, it needs to be send down the
1883 * bulk endpoint. So allocate a bulk URB instead.
1884 */
1885 if (opcode == 0xfc09)
1886 urb = alloc_bulk_urb(hdev, skb);
1887 else
1888 urb = alloc_ctrl_urb(hdev, skb);
1889
1890 /* When the 0xfc01 command is issued to boot into
1891 * the operational firmware, it will actually not
1892 * send a command complete event. To keep the flow
1893 * control working inject that event here.
1894 */
1895 if (opcode == 0xfc01)
1896 inject_cmd_complete(hdev, opcode);
1897 } else {
1898 urb = alloc_ctrl_urb(hdev, skb);
1899 }
1900 if (IS_ERR(urb))
1901 return PTR_ERR(urb);
1902
1903 hdev->stat.cmd_tx++;
1904 return submit_or_queue_tx_urb(hdev, urb);
1905
1906 case HCI_ACLDATA_PKT:
1907 urb = alloc_bulk_urb(hdev, skb);
1908 if (IS_ERR(urb))
1909 return PTR_ERR(urb);
1910
1911 hdev->stat.acl_tx++;
1912 return submit_or_queue_tx_urb(hdev, urb);
1913
1914 case HCI_SCODATA_PKT:
1915 if (hci_conn_num(hdev, SCO_LINK) < 1)
1916 return -ENODEV;
1917
1918 urb = alloc_isoc_urb(hdev, skb);
1919 if (IS_ERR(urb))
1920 return PTR_ERR(urb);
1921
1922 hdev->stat.sco_tx++;
1923 return submit_tx_urb(hdev, urb);
1924 }
1925
1926 return -EILSEQ;
1927}
1928
1929static int btusb_intel_secure_send(struct hci_dev *hdev, u8 fragment_type,
1930 u32 plen, const void *param)
1931{
1932 while (plen > 0) {
1933 struct sk_buff *skb;
1934 u8 cmd_param[253], fragment_len = (plen > 252) ? 252 : plen;
1935
1936 cmd_param[0] = fragment_type;
1937 memcpy(cmd_param + 1, param, fragment_len);
1938
1939 skb = __hci_cmd_sync(hdev, 0xfc09, fragment_len + 1,
1940 cmd_param, HCI_INIT_TIMEOUT);
1941 if (IS_ERR(skb))
1942 return PTR_ERR(skb);
1943
1944 kfree_skb(skb);
1945
1946 plen -= fragment_len;
1947 param += fragment_len;
1948 }
1949
1950 return 0;
1951}
1952
1953static void btusb_intel_version_info(struct hci_dev *hdev,
1954 struct intel_version *ver)
1955{
1956 const char *variant;
1957
1958 switch (ver->fw_variant) {
1959 case 0x06:
1960 variant = "Bootloader";
1961 break;
1962 case 0x23:
1963 variant = "Firmware";
1964 break;
1965 default:
1966 return;
1967 }
1968
1969 BT_INFO("%s: %s revision %u.%u build %u week %u %u", hdev->name,
1970 variant, ver->fw_revision >> 4, ver->fw_revision & 0x0f,
1971 ver->fw_build_num, ver->fw_build_ww, 2000 + ver->fw_build_yy);
1972}
1973
1974static int btusb_setup_intel_new(struct hci_dev *hdev)
1975{
1976 static const u8 reset_param[] = { 0x00, 0x01, 0x00, 0x01,
1977 0x00, 0x08, 0x04, 0x00 };
1978 struct btusb_data *data = hci_get_drvdata(hdev);
1979 struct sk_buff *skb;
1980 struct intel_version *ver;
1981 struct intel_boot_params *params;
1982 const struct firmware *fw;
1983 const u8 *fw_ptr;
1984 char fwname[64];
1985 ktime_t calltime, delta, rettime;
1986 unsigned long long duration;
1987 int err;
1988
1989 BT_DBG("%s", hdev->name);
1990
1991 calltime = ktime_get();
1992
1993 /* Read the Intel version information to determine if the device
1994 * is in bootloader mode or if it already has operational firmware
1995 * loaded.
1996 */
1997 skb = __hci_cmd_sync(hdev, 0xfc05, 0, NULL, HCI_INIT_TIMEOUT);
1998 if (IS_ERR(skb)) {
1999 BT_ERR("%s: Reading Intel version information failed (%ld)",
2000 hdev->name, PTR_ERR(skb));
2001 return PTR_ERR(skb);
2002 }
2003
2004 if (skb->len != sizeof(*ver)) {
2005 BT_ERR("%s: Intel version event size mismatch", hdev->name);
2006 kfree_skb(skb);
2007 return -EILSEQ;
2008 }
2009
2010 ver = (struct intel_version *)skb->data;
2011 if (ver->status) {
2012 BT_ERR("%s: Intel version command failure (%02x)",
2013 hdev->name, ver->status);
2014 err = -bt_to_errno(ver->status);
2015 kfree_skb(skb);
2016 return err;
2017 }
2018
2019 /* The hardware platform number has a fixed value of 0x37 and
2020 * for now only accept this single value.
2021 */
2022 if (ver->hw_platform != 0x37) {
2023 BT_ERR("%s: Unsupported Intel hardware platform (%u)",
2024 hdev->name, ver->hw_platform);
2025 kfree_skb(skb);
2026 return -EINVAL;
2027 }
2028
2029 /* At the moment only the hardware variant iBT 3.0 (LnP/SfP) is
2030 * supported by this firmware loading method. This check has been
2031 * put in place to ensure correct forward compatibility options
2032 * when newer hardware variants come along.
2033 */
2034 if (ver->hw_variant != 0x0b) {
2035 BT_ERR("%s: Unsupported Intel hardware variant (%u)",
2036 hdev->name, ver->hw_variant);
2037 kfree_skb(skb);
2038 return -EINVAL;
2039 }
2040
2041 btusb_intel_version_info(hdev, ver);
2042
2043 /* The firmware variant determines if the device is in bootloader
2044 * mode or is running operational firmware. The value 0x06 identifies
2045 * the bootloader and the value 0x23 identifies the operational
2046 * firmware.
2047 *
2048 * When the operational firmware is already present, then only
2049 * the check for valid Bluetooth device address is needed. This
2050 * determines if the device will be added as configured or
2051 * unconfigured controller.
2052 *
2053 * It is not possible to use the Secure Boot Parameters in this
2054 * case since that command is only available in bootloader mode.
2055 */
2056 if (ver->fw_variant == 0x23) {
2057 kfree_skb(skb);
2058 clear_bit(BTUSB_BOOTLOADER, &data->flags);
2059 btusb_check_bdaddr_intel(hdev);
2060 return 0;
2061 }
2062
2063 /* If the device is not in bootloader mode, then the only possible
2064 * choice is to return an error and abort the device initialization.
2065 */
2066 if (ver->fw_variant != 0x06) {
2067 BT_ERR("%s: Unsupported Intel firmware variant (%u)",
2068 hdev->name, ver->fw_variant);
2069 kfree_skb(skb);
2070 return -ENODEV;
2071 }
2072
2073 kfree_skb(skb);
2074
2075 /* Read the secure boot parameters to identify the operating
2076 * details of the bootloader.
2077 */
2078 skb = __hci_cmd_sync(hdev, 0xfc0d, 0, NULL, HCI_INIT_TIMEOUT);
2079 if (IS_ERR(skb)) {
2080 BT_ERR("%s: Reading Intel boot parameters failed (%ld)",
2081 hdev->name, PTR_ERR(skb));
2082 return PTR_ERR(skb);
2083 }
2084
2085 if (skb->len != sizeof(*params)) {
2086 BT_ERR("%s: Intel boot parameters size mismatch", hdev->name);
2087 kfree_skb(skb);
2088 return -EILSEQ;
2089 }
2090
2091 params = (struct intel_boot_params *)skb->data;
2092 if (params->status) {
2093 BT_ERR("%s: Intel boot parameters command failure (%02x)",
2094 hdev->name, params->status);
2095 err = -bt_to_errno(params->status);
2096 kfree_skb(skb);
2097 return err;
2098 }
2099
2100 BT_INFO("%s: Device revision is %u", hdev->name,
2101 le16_to_cpu(params->dev_revid));
2102
2103 BT_INFO("%s: Secure boot is %s", hdev->name,
2104 params->secure_boot ? "enabled" : "disabled");
2105
2106 BT_INFO("%s: Minimum firmware build %u week %u %u", hdev->name,
2107 params->min_fw_build_nn, params->min_fw_build_cw,
2108 2000 + params->min_fw_build_yy);
2109
2110 /* It is required that every single firmware fragment is acknowledged
2111 * with a command complete event. If the boot parameters indicate
2112 * that this bootloader does not send them, then abort the setup.
2113 */
2114 if (params->limited_cce != 0x00) {
2115 BT_ERR("%s: Unsupported Intel firmware loading method (%u)",
2116 hdev->name, params->limited_cce);
2117 kfree_skb(skb);
2118 return -EINVAL;
2119 }
2120
2121 /* If the OTP has no valid Bluetooth device address, then there will
2122 * also be no valid address for the operational firmware.
2123 */
2124 if (!bacmp(&params->otp_bdaddr, BDADDR_ANY)) {
2125 BT_INFO("%s: No device address configured", hdev->name);
2126 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
2127 }
2128
2129 /* With this Intel bootloader only the hardware variant and device
2130 * revision information are used to select the right firmware.
2131 *
2132 * Currently this bootloader support is limited to hardware variant
2133 * iBT 3.0 (LnP/SfP) which is identified by the value 11 (0x0b).
2134 */
2135 snprintf(fwname, sizeof(fwname), "intel/ibt-11-%u.sfi",
2136 le16_to_cpu(params->dev_revid));
2137
2138 err = request_firmware(&fw, fwname, &hdev->dev);
2139 if (err < 0) {
2140 BT_ERR("%s: Failed to load Intel firmware file (%d)",
2141 hdev->name, err);
2142 kfree_skb(skb);
2143 return err;
2144 }
2145
2146 BT_INFO("%s: Found device firmware: %s", hdev->name, fwname);
2147
2148 kfree_skb(skb);
2149
2150 if (fw->size < 644) {
2151 BT_ERR("%s: Invalid size of firmware file (%zu)",
2152 hdev->name, fw->size);
2153 err = -EBADF;
2154 goto done;
2155 }
2156
2157 set_bit(BTUSB_DOWNLOADING, &data->flags);
2158
2159 /* Start the firmware download transaction with the Init fragment
2160 * represented by the 128 bytes of CSS header.
2161 */
2162 err = btusb_intel_secure_send(hdev, 0x00, 128, fw->data);
2163 if (err < 0) {
2164 BT_ERR("%s: Failed to send firmware header (%d)",
2165 hdev->name, err);
2166 goto done;
2167 }
2168
2169 /* Send the 256 bytes of public key information from the firmware
2170 * as the PKey fragment.
2171 */
2172 err = btusb_intel_secure_send(hdev, 0x03, 256, fw->data + 128);
2173 if (err < 0) {
2174 BT_ERR("%s: Failed to send firmware public key (%d)",
2175 hdev->name, err);
2176 goto done;
2177 }
2178
2179 /* Send the 256 bytes of signature information from the firmware
2180 * as the Sign fragment.
2181 */
2182 err = btusb_intel_secure_send(hdev, 0x02, 256, fw->data + 388);
2183 if (err < 0) {
2184 BT_ERR("%s: Failed to send firmware signature (%d)",
2185 hdev->name, err);
2186 goto done;
2187 }
2188
2189 fw_ptr = fw->data + 644;
2190
2191 while (fw_ptr - fw->data < fw->size) {
2192 struct hci_command_hdr *cmd = (void *)fw_ptr;
2193 u8 cmd_len;
2194
2195 cmd_len = sizeof(*cmd) + cmd->plen;
2196
2197 /* Send each command from the firmware data buffer as
2198 * a single Data fragment.
2199 */
2200 err = btusb_intel_secure_send(hdev, 0x01, cmd_len, fw_ptr);
2201 if (err < 0) {
2202 BT_ERR("%s: Failed to send firmware data (%d)",
2203 hdev->name, err);
2204 goto done;
2205 }
2206
2207 fw_ptr += cmd_len;
2208 }
2209
Marcel Holtmannce6bb922015-01-28 01:58:40 -08002210 set_bit(BTUSB_FIRMWARE_LOADED, &data->flags);
2211
Johan Hedberga087a982015-01-30 10:58:54 +02002212 BT_INFO("%s: Waiting for firmware download to complete", hdev->name);
2213
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002214 /* Before switching the device into operational mode and with that
2215 * booting the loaded firmware, wait for the bootloader notification
2216 * that all fragments have been successfully received.
2217 *
Johan Hedberga087a982015-01-30 10:58:54 +02002218 * When the event processing receives the notification, then the
2219 * BTUSB_DOWNLOADING flag will be cleared.
2220 *
2221 * The firmware loading should not take longer than 5 seconds
2222 * and thus just timeout if that happens and fail the setup
2223 * of this device.
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002224 */
Johan Hedberg129a7692015-02-14 09:33:35 +02002225 err = wait_on_bit_timeout(&data->flags, BTUSB_DOWNLOADING,
2226 TASK_INTERRUPTIBLE,
2227 msecs_to_jiffies(5000));
Johan Hedberga087a982015-01-30 10:58:54 +02002228 if (err == 1) {
2229 BT_ERR("%s: Firmware loading interrupted", hdev->name);
2230 err = -EINTR;
2231 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
Johan Hedbergfad70972015-01-30 10:58:55 +02002282 if (err == 1) {
2283 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
2300 return 0;
2301}
2302
Marcel Holtmann385a7682015-01-28 11:09:56 -08002303static void btusb_hw_error_intel(struct hci_dev *hdev, u8 code)
2304{
2305 struct sk_buff *skb;
2306 u8 type = 0x00;
2307
2308 BT_ERR("%s: Hardware error 0x%2.2x", hdev->name, code);
2309
2310 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
2311 if (IS_ERR(skb)) {
2312 BT_ERR("%s: Reset after hardware error failed (%ld)",
2313 hdev->name, PTR_ERR(skb));
2314 return;
2315 }
2316 kfree_skb(skb);
2317
2318 skb = __hci_cmd_sync(hdev, 0xfc22, 1, &type, HCI_INIT_TIMEOUT);
2319 if (IS_ERR(skb)) {
2320 BT_ERR("%s: Retrieving Intel exception info failed (%ld)",
2321 hdev->name, PTR_ERR(skb));
2322 return;
2323 }
2324
2325 if (skb->len != 13) {
2326 BT_ERR("%s: Exception info size mismatch", hdev->name);
2327 kfree_skb(skb);
2328 return;
2329 }
2330
2331 if (skb->data[0] != 0x00) {
2332 BT_ERR("%s: Exception info command failure (%02x)",
2333 hdev->name, skb->data[0]);
2334 kfree_skb(skb);
2335 return;
2336 }
2337
2338 BT_ERR("%s: Exception info %s", hdev->name, (char *)(skb->data + 1));
2339
2340 kfree_skb(skb);
2341}
2342
Marcel Holtmanncb8d6592014-07-02 11:25:25 +02002343static int btusb_set_bdaddr_intel(struct hci_dev *hdev, const bdaddr_t *bdaddr)
2344{
2345 struct sk_buff *skb;
2346 long ret;
2347
2348 skb = __hci_cmd_sync(hdev, 0xfc31, 6, bdaddr, HCI_INIT_TIMEOUT);
2349 if (IS_ERR(skb)) {
2350 ret = PTR_ERR(skb);
2351 BT_ERR("%s: changing Intel device address failed (%ld)",
Marcel Holtmann89e75332014-09-16 04:44:50 +02002352 hdev->name, ret);
Marcel Holtmanncb8d6592014-07-02 11:25:25 +02002353 return ret;
2354 }
2355 kfree_skb(skb);
2356
2357 return 0;
2358}
2359
Tedd Ho-Jeong Anbfbd45e2015-02-13 09:20:52 -08002360static int btusb_shutdown_intel(struct hci_dev *hdev)
2361{
2362 struct sk_buff *skb;
2363 long ret;
2364
2365 /* Some platforms have an issue with BT LED when the interface is
2366 * down or BT radio is turned off, which takes 5 seconds to BT LED
2367 * goes off. This command turns off the BT LED immediately.
2368 */
2369 skb = __hci_cmd_sync(hdev, 0xfc3f, 0, NULL, HCI_INIT_TIMEOUT);
2370 if (IS_ERR(skb)) {
2371 ret = PTR_ERR(skb);
2372 BT_ERR("%s: turning off Intel device LED failed (%ld)",
2373 hdev->name, ret);
2374 return ret;
2375 }
2376 kfree_skb(skb);
2377
2378 return 0;
2379}
2380
Amitkumar Karwarae8df492014-07-18 14:47:06 -07002381static int btusb_set_bdaddr_marvell(struct hci_dev *hdev,
2382 const bdaddr_t *bdaddr)
2383{
2384 struct sk_buff *skb;
2385 u8 buf[8];
2386 long ret;
2387
2388 buf[0] = 0xfe;
2389 buf[1] = sizeof(bdaddr_t);
2390 memcpy(buf + 2, bdaddr, sizeof(bdaddr_t));
2391
2392 skb = __hci_cmd_sync(hdev, 0xfc22, sizeof(buf), buf, HCI_INIT_TIMEOUT);
2393 if (IS_ERR(skb)) {
2394 ret = PTR_ERR(skb);
2395 BT_ERR("%s: changing Marvell device address failed (%ld)",
2396 hdev->name, ret);
2397 return ret;
2398 }
2399 kfree_skb(skb);
2400
2401 return 0;
2402}
2403
Marcel Holtmann18835df2015-02-14 23:08:47 -08002404static const struct {
2405 u16 subver;
2406 const char *name;
2407} bcm_subver_table[] = {
2408 { 0x210b, "BCM43142A0" }, /* 001.001.011 */
2409 { 0x2112, "BCM4314A0" }, /* 001.001.018 */
2410 { 0x2118, "BCM20702A0" }, /* 001.001.024 */
2411 { 0x2126, "BCM4335A0" }, /* 001.001.038 */
2412 { 0x220e, "BCM20702A1" }, /* 001.002.014 */
2413 { 0x230f, "BCM4354A2" }, /* 001.003.015 */
2414 { 0x4106, "BCM4335B0" }, /* 002.001.006 */
2415 { 0x410e, "BCM20702B0" }, /* 002.001.014 */
2416 { 0x6109, "BCM4335C0" }, /* 003.001.009 */
2417 { 0x610c, "BCM4354" }, /* 003.001.012 */
2418 { }
2419};
2420
Marcel Holtmannc8abb732014-07-02 12:38:22 +02002421#define BDADDR_BCM20702A0 (&(bdaddr_t) {{0x00, 0xa0, 0x02, 0x70, 0x20, 0x00}})
2422
Petri Gynther10d4c672014-05-08 15:50:01 -07002423static int btusb_setup_bcm_patchram(struct hci_dev *hdev)
2424{
2425 struct btusb_data *data = hci_get_drvdata(hdev);
2426 struct usb_device *udev = data->udev;
2427 char fw_name[64];
2428 const struct firmware *fw;
2429 const u8 *fw_ptr;
2430 size_t fw_size;
2431 const struct hci_command_hdr *cmd;
2432 const u8 *cmd_param;
Marcel Holtmann18835df2015-02-14 23:08:47 -08002433 u16 opcode, subver, rev;
2434 const char *hw_name = NULL;
Petri Gynther10d4c672014-05-08 15:50:01 -07002435 struct sk_buff *skb;
2436 struct hci_rp_read_local_version *ver;
Marcel Holtmannc8abb732014-07-02 12:38:22 +02002437 struct hci_rp_read_bd_addr *bda;
Petri Gynther10d4c672014-05-08 15:50:01 -07002438 long ret;
Marcel Holtmann18835df2015-02-14 23:08:47 -08002439 int i;
Petri Gynther10d4c672014-05-08 15:50:01 -07002440
2441 /* Reset */
2442 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
2443 if (IS_ERR(skb)) {
2444 ret = PTR_ERR(skb);
2445 BT_ERR("%s: HCI_OP_RESET failed (%ld)", hdev->name, ret);
Marcel Holtmann18835df2015-02-14 23:08:47 -08002446 return ret;
Petri Gynther10d4c672014-05-08 15:50:01 -07002447 }
2448 kfree_skb(skb);
2449
2450 /* Read Local Version Info */
Daniel Drake6576fe42015-02-23 18:16:47 -06002451 skb = btusb_read_local_version(hdev);
2452 if (IS_ERR(skb))
2453 return PTR_ERR(skb);
Petri Gynther10d4c672014-05-08 15:50:01 -07002454
Marcel Holtmann89e75332014-09-16 04:44:50 +02002455 ver = (struct hci_rp_read_local_version *)skb->data;
Marcel Holtmann18835df2015-02-14 23:08:47 -08002456 rev = le16_to_cpu(ver->hci_rev);
2457 subver = le16_to_cpu(ver->lmp_subver);
Petri Gynther10d4c672014-05-08 15:50:01 -07002458 kfree_skb(skb);
2459
Marcel Holtmann15c32c52015-03-20 17:16:36 -07002460 /* Read Verbose Config Version Info */
2461 skb = __hci_cmd_sync(hdev, 0xfc79, 0, NULL, HCI_INIT_TIMEOUT);
2462 if (IS_ERR(skb)) {
2463 ret = PTR_ERR(skb);
2464 BT_ERR("%s: BCM: Read Verbose Version failed (%ld)",
2465 hdev->name, ret);
2466 return ret;
2467 }
2468
2469 if (skb->len != 7) {
2470 BT_ERR("%s: BCM: Read Verbose Version event length mismatch",
2471 hdev->name);
2472 kfree_skb(skb);
2473 return -EIO;
2474 }
2475
2476 BT_INFO("%s: BCM: chip id %u", hdev->name, skb->data[1]);
2477 kfree_skb(skb);
2478
Marcel Holtmann18835df2015-02-14 23:08:47 -08002479 for (i = 0; bcm_subver_table[i].name; i++) {
2480 if (subver == bcm_subver_table[i].subver) {
2481 hw_name = bcm_subver_table[i].name;
2482 break;
2483 }
2484 }
2485
2486 BT_INFO("%s: %s (%3.3u.%3.3u.%3.3u) build %4.4u", hdev->name,
2487 hw_name ? : "BCM", (subver & 0x7000) >> 13,
2488 (subver & 0x1f00) >> 8, (subver & 0x00ff), rev & 0x0fff);
2489
2490 snprintf(fw_name, sizeof(fw_name), "brcm/%s-%4.4x-%4.4x.hcd",
2491 hw_name ? : "BCM",
2492 le16_to_cpu(udev->descriptor.idVendor),
2493 le16_to_cpu(udev->descriptor.idProduct));
2494
2495 ret = request_firmware(&fw, fw_name, &hdev->dev);
2496 if (ret < 0) {
2497 BT_INFO("%s: BCM: patch %s not found", hdev->name, fw_name);
2498 return 0;
2499 }
2500
Petri Gynther10d4c672014-05-08 15:50:01 -07002501 /* Start Download */
2502 skb = __hci_cmd_sync(hdev, 0xfc2e, 0, NULL, HCI_INIT_TIMEOUT);
2503 if (IS_ERR(skb)) {
2504 ret = PTR_ERR(skb);
2505 BT_ERR("%s: BCM: Download Minidrv command failed (%ld)",
Marcel Holtmann89e75332014-09-16 04:44:50 +02002506 hdev->name, ret);
Petri Gynther10d4c672014-05-08 15:50:01 -07002507 goto reset_fw;
2508 }
2509 kfree_skb(skb);
2510
2511 /* 50 msec delay after Download Minidrv completes */
2512 msleep(50);
2513
2514 fw_ptr = fw->data;
2515 fw_size = fw->size;
2516
2517 while (fw_size >= sizeof(*cmd)) {
Marcel Holtmann89e75332014-09-16 04:44:50 +02002518 cmd = (struct hci_command_hdr *)fw_ptr;
Petri Gynther10d4c672014-05-08 15:50:01 -07002519 fw_ptr += sizeof(*cmd);
2520 fw_size -= sizeof(*cmd);
2521
2522 if (fw_size < cmd->plen) {
2523 BT_ERR("%s: BCM: patch %s is corrupted",
Marcel Holtmann89e75332014-09-16 04:44:50 +02002524 hdev->name, fw_name);
Petri Gynther10d4c672014-05-08 15:50:01 -07002525 ret = -EINVAL;
2526 goto reset_fw;
2527 }
2528
2529 cmd_param = fw_ptr;
2530 fw_ptr += cmd->plen;
2531 fw_size -= cmd->plen;
2532
2533 opcode = le16_to_cpu(cmd->opcode);
2534
2535 skb = __hci_cmd_sync(hdev, opcode, cmd->plen, cmd_param,
2536 HCI_INIT_TIMEOUT);
2537 if (IS_ERR(skb)) {
2538 ret = PTR_ERR(skb);
2539 BT_ERR("%s: BCM: patch command %04x failed (%ld)",
Marcel Holtmann89e75332014-09-16 04:44:50 +02002540 hdev->name, opcode, ret);
Petri Gynther10d4c672014-05-08 15:50:01 -07002541 goto reset_fw;
2542 }
2543 kfree_skb(skb);
2544 }
2545
2546 /* 250 msec delay after Launch Ram completes */
2547 msleep(250);
2548
2549reset_fw:
2550 /* Reset */
2551 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
2552 if (IS_ERR(skb)) {
2553 ret = PTR_ERR(skb);
2554 BT_ERR("%s: HCI_OP_RESET failed (%ld)", hdev->name, ret);
2555 goto done;
2556 }
2557 kfree_skb(skb);
2558
2559 /* Read Local Version Info */
Daniel Drake6576fe42015-02-23 18:16:47 -06002560 skb = btusb_read_local_version(hdev);
Petri Gynther10d4c672014-05-08 15:50:01 -07002561 if (IS_ERR(skb)) {
2562 ret = PTR_ERR(skb);
Petri Gynther10d4c672014-05-08 15:50:01 -07002563 goto done;
2564 }
2565
Marcel Holtmann89e75332014-09-16 04:44:50 +02002566 ver = (struct hci_rp_read_local_version *)skb->data;
Marcel Holtmann18835df2015-02-14 23:08:47 -08002567 rev = le16_to_cpu(ver->hci_rev);
2568 subver = le16_to_cpu(ver->lmp_subver);
Petri Gynther10d4c672014-05-08 15:50:01 -07002569 kfree_skb(skb);
2570
Marcel Holtmann18835df2015-02-14 23:08:47 -08002571 BT_INFO("%s: %s (%3.3u.%3.3u.%3.3u) build %4.4u", hdev->name,
2572 hw_name ? : "BCM", (subver & 0x7000) >> 13,
2573 (subver & 0x1f00) >> 8, (subver & 0x00ff), rev & 0x0fff);
2574
Marcel Holtmannc8abb732014-07-02 12:38:22 +02002575 /* Read BD Address */
2576 skb = __hci_cmd_sync(hdev, HCI_OP_READ_BD_ADDR, 0, NULL,
2577 HCI_INIT_TIMEOUT);
2578 if (IS_ERR(skb)) {
2579 ret = PTR_ERR(skb);
2580 BT_ERR("%s: HCI_OP_READ_BD_ADDR failed (%ld)",
Marcel Holtmann89e75332014-09-16 04:44:50 +02002581 hdev->name, ret);
Marcel Holtmannc8abb732014-07-02 12:38:22 +02002582 goto done;
2583 }
2584
2585 if (skb->len != sizeof(*bda)) {
2586 BT_ERR("%s: HCI_OP_READ_BD_ADDR event length mismatch",
Marcel Holtmann89e75332014-09-16 04:44:50 +02002587 hdev->name);
Marcel Holtmannc8abb732014-07-02 12:38:22 +02002588 kfree_skb(skb);
2589 ret = -EIO;
2590 goto done;
2591 }
2592
Marcel Holtmann89e75332014-09-16 04:44:50 +02002593 bda = (struct hci_rp_read_bd_addr *)skb->data;
Marcel Holtmannc8abb732014-07-02 12:38:22 +02002594 if (bda->status) {
2595 BT_ERR("%s: HCI_OP_READ_BD_ADDR error status (%02x)",
2596 hdev->name, bda->status);
2597 kfree_skb(skb);
2598 ret = -bt_to_errno(bda->status);
2599 goto done;
2600 }
2601
2602 /* The address 00:20:70:02:A0:00 indicates a BCM20702A0 controller
2603 * with no configured address.
2604 */
Marcel Holtmann849e5082014-07-04 16:54:39 +02002605 if (!bacmp(&bda->bdaddr, BDADDR_BCM20702A0)) {
Marcel Holtmannc8abb732014-07-02 12:38:22 +02002606 BT_INFO("%s: BCM: using default device address (%pMR)",
2607 hdev->name, &bda->bdaddr);
Marcel Holtmann849e5082014-07-04 16:54:39 +02002608 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
2609 }
Marcel Holtmannc8abb732014-07-02 12:38:22 +02002610
2611 kfree_skb(skb);
2612
Petri Gynther10d4c672014-05-08 15:50:01 -07002613done:
2614 release_firmware(fw);
2615
2616 return ret;
2617}
2618
Marcel Holtmannabbaf502014-07-02 00:53:48 +02002619static int btusb_set_bdaddr_bcm(struct hci_dev *hdev, const bdaddr_t *bdaddr)
2620{
2621 struct sk_buff *skb;
2622 long ret;
2623
2624 skb = __hci_cmd_sync(hdev, 0xfc01, 6, bdaddr, HCI_INIT_TIMEOUT);
2625 if (IS_ERR(skb)) {
2626 ret = PTR_ERR(skb);
2627 BT_ERR("%s: BCM: Change address command failed (%ld)",
Marcel Holtmann89e75332014-09-16 04:44:50 +02002628 hdev->name, ret);
Marcel Holtmannabbaf502014-07-02 00:53:48 +02002629 return ret;
2630 }
2631 kfree_skb(skb);
2632
2633 return 0;
2634}
2635
Marcel Holtmann17b27722015-03-22 15:52:38 +01002636static int btusb_setup_bcm_apple(struct hci_dev *hdev)
2637{
2638 struct sk_buff *skb;
2639 int err;
2640
2641 /* Read Verbose Config Version Info */
2642 skb = __hci_cmd_sync(hdev, 0xfc79, 0, NULL, HCI_INIT_TIMEOUT);
2643 if (IS_ERR(skb)) {
2644 err = PTR_ERR(skb);
2645 BT_ERR("%s: BCM: Read Verbose Version failed (%d)",
2646 hdev->name, err);
2647 return err;
2648 }
2649
2650 if (skb->len != 7) {
2651 BT_ERR("%s: BCM: Read Verbose Version event length mismatch",
2652 hdev->name);
2653 kfree_skb(skb);
2654 return -EIO;
2655 }
2656
2657 BT_INFO("%s: BCM: chip id %u build %4.4u", hdev->name, skb->data[1],
2658 get_unaligned_le16(skb->data + 5));
2659 kfree_skb(skb);
2660
2661 return 0;
2662}
2663
Toshi Kikuchi58592232014-12-12 10:58:05 -08002664static int btusb_set_bdaddr_ath3012(struct hci_dev *hdev,
2665 const bdaddr_t *bdaddr)
2666{
2667 struct sk_buff *skb;
2668 u8 buf[10];
2669 long ret;
2670
2671 buf[0] = 0x01;
2672 buf[1] = 0x01;
2673 buf[2] = 0x00;
2674 buf[3] = sizeof(bdaddr_t);
2675 memcpy(buf + 4, bdaddr, sizeof(bdaddr_t));
2676
2677 skb = __hci_cmd_sync(hdev, 0xfc0b, sizeof(buf), buf, HCI_INIT_TIMEOUT);
2678 if (IS_ERR(skb)) {
2679 ret = PTR_ERR(skb);
2680 BT_ERR("%s: Change address command failed (%ld)",
2681 hdev->name, ret);
2682 return ret;
2683 }
2684 kfree_skb(skb);
2685
2686 return 0;
2687}
2688
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002689#define QCA_DFU_PACKET_LEN 4096
2690
2691#define QCA_GET_TARGET_VERSION 0x09
2692#define QCA_CHECK_STATUS 0x05
2693#define QCA_DFU_DOWNLOAD 0x01
2694
2695#define QCA_SYSCFG_UPDATED 0x40
2696#define QCA_PATCH_UPDATED 0x80
2697#define QCA_DFU_TIMEOUT 3000
2698
2699struct qca_version {
2700 __le32 rom_version;
2701 __le32 patch_version;
2702 __le32 ram_version;
2703 __le32 ref_clock;
2704 __u8 reserved[4];
2705} __packed;
2706
2707struct qca_rampatch_version {
2708 __le16 rom_version;
2709 __le16 patch_version;
2710} __packed;
2711
2712struct qca_device_info {
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002713 u32 rom_version;
2714 u8 rampatch_hdr; /* length of header in rampatch */
2715 u8 nvm_hdr; /* length of header in NVM */
2716 u8 ver_offset; /* offset of version structure in rampatch */
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002717};
2718
2719static const struct qca_device_info qca_devices_table[] = {
2720 { 0x00000100, 20, 4, 10 }, /* Rome 1.0 */
2721 { 0x00000101, 20, 4, 10 }, /* Rome 1.1 */
2722 { 0x00000201, 28, 4, 18 }, /* Rome 2.1 */
2723 { 0x00000300, 28, 4, 18 }, /* Rome 3.0 */
2724 { 0x00000302, 28, 4, 18 }, /* Rome 3.2 */
2725};
2726
2727static int btusb_qca_send_vendor_req(struct hci_dev *hdev, u8 request,
2728 void *data, u16 size)
2729{
2730 struct btusb_data *btdata = hci_get_drvdata(hdev);
2731 struct usb_device *udev = btdata->udev;
2732 int pipe, err;
2733 u8 *buf;
2734
2735 buf = kmalloc(size, GFP_KERNEL);
2736 if (!buf)
2737 return -ENOMEM;
2738
2739 /* Found some of USB hosts have IOT issues with ours so that we should
2740 * not wait until HCI layer is ready.
2741 */
2742 pipe = usb_rcvctrlpipe(udev, 0);
2743 err = usb_control_msg(udev, pipe, request, USB_TYPE_VENDOR | USB_DIR_IN,
2744 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
2745 if (err < 0) {
2746 BT_ERR("%s: Failed to access otp area (%d)", hdev->name, err);
2747 goto done;
2748 }
2749
2750 memcpy(data, buf, size);
2751
2752done:
2753 kfree(buf);
2754
2755 return err;
2756}
2757
2758static int btusb_setup_qca_download_fw(struct hci_dev *hdev,
2759 const struct firmware *firmware,
2760 size_t hdr_size)
2761{
2762 struct btusb_data *btdata = hci_get_drvdata(hdev);
2763 struct usb_device *udev = btdata->udev;
2764 size_t count, size, sent = 0;
2765 int pipe, len, err;
2766 u8 *buf;
2767
2768 buf = kmalloc(QCA_DFU_PACKET_LEN, GFP_KERNEL);
2769 if (!buf)
2770 return -ENOMEM;
2771
2772 count = firmware->size;
2773
2774 size = min_t(size_t, count, hdr_size);
2775 memcpy(buf, firmware->data, size);
2776
2777 /* USB patches should go down to controller through USB path
2778 * because binary format fits to go down through USB channel.
2779 * USB control path is for patching headers and USB bulk is for
2780 * patch body.
2781 */
2782 pipe = usb_sndctrlpipe(udev, 0);
2783 err = usb_control_msg(udev, pipe, QCA_DFU_DOWNLOAD, USB_TYPE_VENDOR,
2784 0, 0, buf, size, USB_CTRL_SET_TIMEOUT);
2785 if (err < 0) {
2786 BT_ERR("%s: Failed to send headers (%d)", hdev->name, err);
2787 goto done;
2788 }
2789
2790 sent += size;
2791 count -= size;
2792
2793 while (count) {
2794 size = min_t(size_t, count, QCA_DFU_PACKET_LEN);
2795
2796 memcpy(buf, firmware->data + sent, size);
2797
2798 pipe = usb_sndbulkpipe(udev, 0x02);
2799 err = usb_bulk_msg(udev, pipe, buf, size, &len,
2800 QCA_DFU_TIMEOUT);
2801 if (err < 0) {
2802 BT_ERR("%s: Failed to send body at %zd of %zd (%d)",
2803 hdev->name, sent, firmware->size, err);
2804 break;
2805 }
2806
2807 if (size != len) {
2808 BT_ERR("%s: Failed to get bulk buffer", hdev->name);
2809 err = -EILSEQ;
2810 break;
2811 }
2812
2813 sent += size;
2814 count -= size;
2815 }
2816
2817done:
2818 kfree(buf);
2819 return err;
2820}
2821
2822static int btusb_setup_qca_load_rampatch(struct hci_dev *hdev,
2823 struct qca_version *ver,
2824 const struct qca_device_info *info)
2825{
2826 struct qca_rampatch_version *rver;
2827 const struct firmware *fw;
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002828 u32 ver_rom, ver_patch;
2829 u16 rver_rom, rver_patch;
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002830 char fwname[64];
2831 int err;
2832
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002833 ver_rom = le32_to_cpu(ver->rom_version);
2834 ver_patch = le32_to_cpu(ver->patch_version);
2835
2836 snprintf(fwname, sizeof(fwname), "qca/rampatch_usb_%08x.bin", ver_rom);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002837
2838 err = request_firmware(&fw, fwname, &hdev->dev);
2839 if (err) {
2840 BT_ERR("%s: failed to request rampatch file: %s (%d)",
2841 hdev->name, fwname, err);
2842 return err;
2843 }
2844
2845 BT_INFO("%s: using rampatch file: %s", hdev->name, fwname);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002846
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002847 rver = (struct qca_rampatch_version *)(fw->data + info->ver_offset);
2848 rver_rom = le16_to_cpu(rver->rom_version);
2849 rver_patch = le16_to_cpu(rver->patch_version);
2850
2851 BT_INFO("%s: QCA: patch rome 0x%x build 0x%x, firmware rome 0x%x "
2852 "build 0x%x", hdev->name, rver_rom, rver_patch, ver_rom,
2853 ver_patch);
2854
2855 if (rver_rom != ver_rom || rver_patch <= ver_patch) {
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002856 BT_ERR("%s: rampatch file version did not match with firmware",
2857 hdev->name);
2858 err = -EINVAL;
2859 goto done;
2860 }
2861
2862 err = btusb_setup_qca_download_fw(hdev, fw, info->rampatch_hdr);
2863
2864done:
2865 release_firmware(fw);
2866
2867 return err;
2868}
2869
2870static int btusb_setup_qca_load_nvm(struct hci_dev *hdev,
2871 struct qca_version *ver,
2872 const struct qca_device_info *info)
2873{
2874 const struct firmware *fw;
2875 char fwname[64];
2876 int err;
2877
2878 snprintf(fwname, sizeof(fwname), "qca/nvm_usb_%08x.bin",
2879 le32_to_cpu(ver->rom_version));
2880
2881 err = request_firmware(&fw, fwname, &hdev->dev);
2882 if (err) {
2883 BT_ERR("%s: failed to request NVM file: %s (%d)",
2884 hdev->name, fwname, err);
2885 return err;
2886 }
2887
2888 BT_INFO("%s: using NVM file: %s", hdev->name, fwname);
2889
2890 err = btusb_setup_qca_download_fw(hdev, fw, info->nvm_hdr);
2891
2892 release_firmware(fw);
2893
2894 return err;
2895}
2896
2897static int btusb_setup_qca(struct hci_dev *hdev)
2898{
2899 const struct qca_device_info *info = NULL;
2900 struct qca_version ver;
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002901 u32 ver_rom;
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002902 u8 status;
2903 int i, err;
2904
2905 err = btusb_qca_send_vendor_req(hdev, QCA_GET_TARGET_VERSION, &ver,
2906 sizeof(ver));
2907 if (err < 0)
2908 return err;
2909
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002910 ver_rom = le32_to_cpu(ver.rom_version);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002911 for (i = 0; i < ARRAY_SIZE(qca_devices_table); i++) {
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002912 if (ver_rom == qca_devices_table[i].rom_version)
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002913 info = &qca_devices_table[i];
2914 }
2915 if (!info) {
2916 BT_ERR("%s: don't support firmware rome 0x%x", hdev->name,
Kim, Ben Young Taebf906b32015-03-10 23:34:58 +00002917 ver_rom);
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00002918 return -ENODEV;
2919 }
2920
2921 err = btusb_qca_send_vendor_req(hdev, QCA_CHECK_STATUS, &status,
2922 sizeof(status));
2923 if (err < 0)
2924 return err;
2925
2926 if (!(status & QCA_PATCH_UPDATED)) {
2927 err = btusb_setup_qca_load_rampatch(hdev, &ver, info);
2928 if (err < 0)
2929 return err;
2930 }
2931
2932 if (!(status & QCA_SYSCFG_UPDATED)) {
2933 err = btusb_setup_qca_load_nvm(hdev, &ver, info);
2934 if (err < 0)
2935 return err;
2936 }
2937
2938 return 0;
2939}
2940
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002941static int btusb_probe(struct usb_interface *intf,
Marcel Holtmann89e75332014-09-16 04:44:50 +02002942 const struct usb_device_id *id)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002943{
2944 struct usb_endpoint_descriptor *ep_desc;
2945 struct btusb_data *data;
2946 struct hci_dev *hdev;
2947 int i, err;
2948
2949 BT_DBG("intf %p id %p", intf, id);
2950
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002951 /* interface numbers are hardcoded in the spec */
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002952 if (intf->cur_altsetting->desc.bInterfaceNumber != 0)
2953 return -ENODEV;
2954
2955 if (!id->driver_info) {
2956 const struct usb_device_id *match;
Marcel Holtmann89e75332014-09-16 04:44:50 +02002957
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002958 match = usb_match_id(intf, blacklist_table);
2959 if (match)
2960 id = match;
2961 }
2962
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002963 if (id->driver_info == BTUSB_IGNORE)
2964 return -ENODEV;
2965
Steven.Li2d25f8b2011-07-01 14:02:36 +08002966 if (id->driver_info & BTUSB_ATH3012) {
2967 struct usb_device *udev = interface_to_usbdev(intf);
2968
2969 /* Old firmware would otherwise let ath3k driver load
2970 * patch and sysconfig files */
2971 if (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x0001)
2972 return -ENODEV;
2973 }
2974
Sachin Kamat98921db2012-07-27 12:38:39 +05302975 data = devm_kzalloc(&intf->dev, sizeof(*data), GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002976 if (!data)
2977 return -ENOMEM;
2978
2979 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
2980 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
2981
2982 if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
2983 data->intr_ep = ep_desc;
2984 continue;
2985 }
2986
2987 if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
2988 data->bulk_tx_ep = ep_desc;
2989 continue;
2990 }
2991
2992 if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
2993 data->bulk_rx_ep = ep_desc;
2994 continue;
2995 }
2996 }
2997
Sachin Kamat98921db2012-07-27 12:38:39 +05302998 if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002999 return -ENODEV;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003000
Marcel Holtmann893ba542015-01-28 20:27:34 -08003001 if (id->driver_info & BTUSB_AMP) {
3002 data->cmdreq_type = USB_TYPE_CLASS | 0x01;
3003 data->cmdreq = 0x2b;
3004 } else {
3005 data->cmdreq_type = USB_TYPE_CLASS;
3006 data->cmdreq = 0x00;
3007 }
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01003008
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003009 data->udev = interface_to_usbdev(intf);
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02003010 data->intf = intf;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003011
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003012 INIT_WORK(&data->work, btusb_work);
Oliver Neukum7bee5492009-08-24 23:44:59 +02003013 INIT_WORK(&data->waker, btusb_waker);
Marcel Holtmann803b5832014-09-16 08:00:29 +02003014 init_usb_anchor(&data->deferred);
3015 init_usb_anchor(&data->tx_anchor);
Oliver Neukum7bee5492009-08-24 23:44:59 +02003016 spin_lock_init(&data->txlock);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003017
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003018 init_usb_anchor(&data->intr_anchor);
3019 init_usb_anchor(&data->bulk_anchor);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02003020 init_usb_anchor(&data->isoc_anchor);
Marcel Holtmann803b5832014-09-16 08:00:29 +02003021 spin_lock_init(&data->rxlock);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003022
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08003023 if (id->driver_info & BTUSB_INTEL_NEW) {
3024 data->recv_event = btusb_recv_event_intel;
3025 data->recv_bulk = btusb_recv_bulk_intel;
3026 set_bit(BTUSB_BOOTLOADER, &data->flags);
3027 } else {
3028 data->recv_event = hci_recv_frame;
3029 data->recv_bulk = btusb_recv_bulk;
3030 }
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +01003031
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003032 hdev = hci_alloc_dev();
Sachin Kamat98921db2012-07-27 12:38:39 +05303033 if (!hdev)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003034 return -ENOMEM;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003035
Marcel Holtmannc13854c2010-02-08 15:27:07 +01003036 hdev->bus = HCI_USB;
David Herrmann155961e2012-02-09 21:58:32 +01003037 hci_set_drvdata(hdev, data);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003038
Marcel Holtmann893ba542015-01-28 20:27:34 -08003039 if (id->driver_info & BTUSB_AMP)
3040 hdev->dev_type = HCI_AMP;
3041 else
3042 hdev->dev_type = HCI_BREDR;
3043
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003044 data->hdev = hdev;
3045
3046 SET_HCIDEV_DEV(hdev, &intf->dev);
3047
Marcel Holtmann9f8f9622013-04-10 08:11:35 -07003048 hdev->open = btusb_open;
3049 hdev->close = btusb_close;
3050 hdev->flush = btusb_flush;
3051 hdev->send = btusb_send_frame;
3052 hdev->notify = btusb_notify;
3053
3054 if (id->driver_info & BTUSB_BCM92035)
3055 hdev->setup = btusb_setup_bcm92035;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003056
Marcel Holtmannabbaf502014-07-02 00:53:48 +02003057 if (id->driver_info & BTUSB_BCM_PATCHRAM) {
Petri Gynther10d4c672014-05-08 15:50:01 -07003058 hdev->setup = btusb_setup_bcm_patchram;
Marcel Holtmannabbaf502014-07-02 00:53:48 +02003059 hdev->set_bdaddr = btusb_set_bdaddr_bcm;
Marcel Holtmann27c3fbe2014-11-02 20:52:24 +01003060 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
Marcel Holtmannabbaf502014-07-02 00:53:48 +02003061 }
Petri Gynther10d4c672014-05-08 15:50:01 -07003062
Marcel Holtmann17b27722015-03-22 15:52:38 +01003063 if (id->driver_info & BTUSB_BCM_APPLE) {
3064 hdev->setup = btusb_setup_bcm_apple;
3065 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
3066 }
3067
Marcel Holtmanncb8d6592014-07-02 11:25:25 +02003068 if (id->driver_info & BTUSB_INTEL) {
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07003069 hdev->setup = btusb_setup_intel;
Tedd Ho-Jeong Anbfbd45e2015-02-13 09:20:52 -08003070 hdev->shutdown = btusb_shutdown_intel;
Marcel Holtmanncb8d6592014-07-02 11:25:25 +02003071 hdev->set_bdaddr = btusb_set_bdaddr_intel;
Jakub Pawlowskic33fb9b2015-01-30 18:55:58 -08003072 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
Jakub Pawlowskic1154842015-03-17 09:04:16 -07003073 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
Marcel Holtmanncb8d6592014-07-02 11:25:25 +02003074 }
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07003075
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08003076 if (id->driver_info & BTUSB_INTEL_NEW) {
3077 hdev->send = btusb_send_frame_intel;
3078 hdev->setup = btusb_setup_intel_new;
Marcel Holtmann385a7682015-01-28 11:09:56 -08003079 hdev->hw_error = btusb_hw_error_intel;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08003080 hdev->set_bdaddr = btusb_set_bdaddr_intel;
Marcel Holtmannb970c5b2015-02-01 23:57:18 -08003081 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08003082 }
3083
Amitkumar Karwarae8df492014-07-18 14:47:06 -07003084 if (id->driver_info & BTUSB_MARVELL)
3085 hdev->set_bdaddr = btusb_set_bdaddr_marvell;
3086
Marcel Holtmann661cf882015-01-02 23:35:20 -08003087 if (id->driver_info & BTUSB_SWAVE) {
3088 set_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks);
Marcel Holtmannd57dbe72014-12-26 04:42:33 +01003089 set_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS, &hdev->quirks);
Marcel Holtmann661cf882015-01-02 23:35:20 -08003090 }
Marcel Holtmannd57dbe72014-12-26 04:42:33 +01003091
Marcel Holtmann40df7832014-07-06 13:29:58 +02003092 if (id->driver_info & BTUSB_INTEL_BOOT)
3093 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
3094
Jakub Pawlowski79f0c872015-01-29 10:38:34 -08003095 if (id->driver_info & BTUSB_ATH3012) {
Toshi Kikuchi58592232014-12-12 10:58:05 -08003096 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
Jakub Pawlowski3d50d512015-03-17 09:04:15 -07003097 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
Jakub Pawlowski79f0c872015-01-29 10:38:34 -08003098 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
3099 }
Toshi Kikuchi58592232014-12-12 10:58:05 -08003100
Kim, Ben Young Tae3267c882015-02-15 23:07:33 +00003101 if (id->driver_info & BTUSB_QCA_ROME) {
3102 data->setup_on_usb = btusb_setup_qca;
3103 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
3104 }
3105
Marcel Holtmann893ba542015-01-28 20:27:34 -08003106 if (id->driver_info & BTUSB_AMP) {
3107 /* AMP controllers do not support SCO packets */
3108 data->isoc = NULL;
3109 } else {
3110 /* Interface numbers are hardcoded in the specification */
3111 data->isoc = usb_ifnum_to_if(data->udev, 1);
3112 }
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02003113
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01003114 if (!reset)
Szymon Janca6c511c2012-05-23 12:35:46 +02003115 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02003116
3117 if (force_scofix || id->driver_info & BTUSB_WRONG_SCO_MTU) {
3118 if (!disable_scofix)
3119 set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
3120 }
3121
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02003122 if (id->driver_info & BTUSB_BROKEN_ISOC)
3123 data->isoc = NULL;
3124
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01003125 if (id->driver_info & BTUSB_DIGIANSWER) {
3126 data->cmdreq_type = USB_TYPE_VENDOR;
Szymon Janca6c511c2012-05-23 12:35:46 +02003127 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01003128 }
3129
3130 if (id->driver_info & BTUSB_CSR) {
3131 struct usb_device *udev = data->udev;
Marcel Holtmann81cac642014-01-03 03:02:36 -08003132 u16 bcdDevice = le16_to_cpu(udev->descriptor.bcdDevice);
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01003133
3134 /* Old firmware would otherwise execute USB reset */
Marcel Holtmann81cac642014-01-03 03:02:36 -08003135 if (bcdDevice < 0x117)
Szymon Janca6c511c2012-05-23 12:35:46 +02003136 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmann81cac642014-01-03 03:02:36 -08003137
3138 /* Fake CSR devices with broken commands */
3139 if (bcdDevice <= 0x100)
3140 hdev->setup = btusb_setup_csr;
Jakub Pawlowski49c989a2015-03-17 09:04:17 -07003141
3142 set_bit(HCI_QUIRK_SIMULTANEOUS_DISCOVERY, &hdev->quirks);
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01003143 }
3144
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02003145 if (id->driver_info & BTUSB_SNIFFER) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02003146 struct usb_device *udev = data->udev;
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02003147
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01003148 /* New sniffer firmware has crippled HCI interface */
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02003149 if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
3150 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
3151 }
3152
Marcel Holtmann3a5ef202014-07-06 14:53:54 +02003153 if (id->driver_info & BTUSB_INTEL_BOOT) {
3154 /* A bug in the bootloader causes that interrupt interface is
3155 * only enabled after receiving SetInterface(0, AltSetting=0).
3156 */
3157 err = usb_set_interface(data->udev, 0, 0);
3158 if (err < 0) {
3159 BT_ERR("failed to set interface 0, alt 0 %d", err);
3160 hci_free_dev(hdev);
3161 return err;
3162 }
3163 }
3164
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02003165 if (data->isoc) {
3166 err = usb_driver_claim_interface(&btusb_driver,
Marcel Holtmann89e75332014-09-16 04:44:50 +02003167 data->isoc, data);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02003168 if (err < 0) {
3169 hci_free_dev(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02003170 return err;
3171 }
3172 }
3173
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003174 err = hci_register_dev(hdev);
3175 if (err < 0) {
3176 hci_free_dev(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003177 return err;
3178 }
3179
3180 usb_set_intfdata(intf, data);
3181
3182 return 0;
3183}
3184
3185static void btusb_disconnect(struct usb_interface *intf)
3186{
3187 struct btusb_data *data = usb_get_intfdata(intf);
3188 struct hci_dev *hdev;
3189
3190 BT_DBG("intf %p", intf);
3191
3192 if (!data)
3193 return;
3194
3195 hdev = data->hdev;
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02003196 usb_set_intfdata(data->intf, NULL);
3197
3198 if (data->isoc)
3199 usb_set_intfdata(data->isoc, NULL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003200
3201 hci_unregister_dev(hdev);
3202
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02003203 if (intf == data->isoc)
3204 usb_driver_release_interface(&btusb_driver, data->intf);
3205 else if (data->isoc)
3206 usb_driver_release_interface(&btusb_driver, data->isoc);
3207
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003208 hci_free_dev(hdev);
3209}
3210
Oliver Neukum7bee5492009-08-24 23:44:59 +02003211#ifdef CONFIG_PM
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003212static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
3213{
3214 struct btusb_data *data = usb_get_intfdata(intf);
3215
3216 BT_DBG("intf %p", intf);
3217
3218 if (data->suspend_count++)
3219 return 0;
3220
Oliver Neukum7bee5492009-08-24 23:44:59 +02003221 spin_lock_irq(&data->txlock);
Alan Stern5b1b0b82011-08-19 23:49:48 +02003222 if (!(PMSG_IS_AUTO(message) && data->tx_in_flight)) {
Oliver Neukum7bee5492009-08-24 23:44:59 +02003223 set_bit(BTUSB_SUSPENDING, &data->flags);
3224 spin_unlock_irq(&data->txlock);
3225 } else {
3226 spin_unlock_irq(&data->txlock);
3227 data->suspend_count--;
3228 return -EBUSY;
3229 }
3230
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003231 cancel_work_sync(&data->work);
3232
Oliver Neukum7bee5492009-08-24 23:44:59 +02003233 btusb_stop_traffic(data);
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003234 usb_kill_anchored_urbs(&data->tx_anchor);
3235
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003236 return 0;
3237}
3238
Oliver Neukum7bee5492009-08-24 23:44:59 +02003239static void play_deferred(struct btusb_data *data)
3240{
3241 struct urb *urb;
3242 int err;
3243
3244 while ((urb = usb_get_from_anchor(&data->deferred))) {
3245 err = usb_submit_urb(urb, GFP_ATOMIC);
3246 if (err < 0)
3247 break;
3248
3249 data->tx_in_flight++;
3250 }
3251 usb_scuttle_anchored_urbs(&data->deferred);
3252}
3253
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003254static int btusb_resume(struct usb_interface *intf)
3255{
3256 struct btusb_data *data = usb_get_intfdata(intf);
3257 struct hci_dev *hdev = data->hdev;
Oliver Neukum7bee5492009-08-24 23:44:59 +02003258 int err = 0;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003259
3260 BT_DBG("intf %p", intf);
3261
3262 if (--data->suspend_count)
3263 return 0;
3264
3265 if (!test_bit(HCI_RUNNING, &hdev->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +02003266 goto done;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003267
3268 if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
3269 err = btusb_submit_intr_urb(hdev, GFP_NOIO);
3270 if (err < 0) {
3271 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02003272 goto failed;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003273 }
3274 }
3275
3276 if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) {
Marcel Holtmann43c2e572009-02-04 17:41:38 +01003277 err = btusb_submit_bulk_urb(hdev, GFP_NOIO);
3278 if (err < 0) {
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003279 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02003280 goto failed;
3281 }
3282
3283 btusb_submit_bulk_urb(hdev, GFP_NOIO);
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003284 }
3285
3286 if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
3287 if (btusb_submit_isoc_urb(hdev, GFP_NOIO) < 0)
3288 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
3289 else
3290 btusb_submit_isoc_urb(hdev, GFP_NOIO);
3291 }
3292
Oliver Neukum7bee5492009-08-24 23:44:59 +02003293 spin_lock_irq(&data->txlock);
3294 play_deferred(data);
3295 clear_bit(BTUSB_SUSPENDING, &data->flags);
3296 spin_unlock_irq(&data->txlock);
3297 schedule_work(&data->work);
3298
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003299 return 0;
Oliver Neukum7bee5492009-08-24 23:44:59 +02003300
3301failed:
3302 usb_scuttle_anchored_urbs(&data->deferred);
3303done:
3304 spin_lock_irq(&data->txlock);
3305 clear_bit(BTUSB_SUSPENDING, &data->flags);
3306 spin_unlock_irq(&data->txlock);
3307
3308 return err;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003309}
Oliver Neukum7bee5492009-08-24 23:44:59 +02003310#endif
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003311
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003312static struct usb_driver btusb_driver = {
3313 .name = "btusb",
3314 .probe = btusb_probe,
3315 .disconnect = btusb_disconnect,
Oliver Neukum7bee5492009-08-24 23:44:59 +02003316#ifdef CONFIG_PM
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003317 .suspend = btusb_suspend,
3318 .resume = btusb_resume,
Oliver Neukum7bee5492009-08-24 23:44:59 +02003319#endif
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003320 .id_table = btusb_table,
Oliver Neukum7bee5492009-08-24 23:44:59 +02003321 .supports_autosuspend = 1,
Sarah Sharpe1f12eb2012-04-23 10:08:51 -07003322 .disable_hub_initiated_lpm = 1,
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003323};
3324
Greg Kroah-Hartman93f15082011-11-18 09:47:34 -08003325module_usb_driver(btusb_driver);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003326
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02003327module_param(disable_scofix, bool, 0644);
3328MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
3329
3330module_param(force_scofix, bool, 0644);
3331MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
3332
3333module_param(reset, bool, 0644);
3334MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
3335
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003336MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
3337MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
3338MODULE_VERSION(VERSION);
3339MODULE_LICENSE("GPL");