blob: 73e1066cd38b78903a628b12a00d4abddec272ed [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 Holtmann5e23b922007-10-20 14:12:34 +020027
28#include <net/bluetooth/bluetooth.h>
29#include <net/bluetooth/hci_core.h>
30
Marcel Holtmanncda0dd72015-01-26 21:33:48 -080031#define VERSION "0.7"
Marcel Holtmanncfeb4142008-08-07 22:26:56 +020032
Rusty Russell90ab5ee2012-01-13 09:32:20 +103033static bool disable_scofix;
34static bool force_scofix;
Marcel Holtmann7a9d4022008-11-30 12:17:26 +010035
Rusty Russell90ab5ee2012-01-13 09:32:20 +103036static bool reset = 1;
Marcel Holtmanncfeb4142008-08-07 22:26:56 +020037
38static struct usb_driver btusb_driver;
39
40#define BTUSB_IGNORE 0x01
Marcel Holtmann7a9d4022008-11-30 12:17:26 +010041#define BTUSB_DIGIANSWER 0x02
42#define BTUSB_CSR 0x04
43#define BTUSB_SNIFFER 0x08
44#define BTUSB_BCM92035 0x10
45#define BTUSB_BROKEN_ISOC 0x20
46#define BTUSB_WRONG_SCO_MTU 0x40
Steven.Li2d25f8b2011-07-01 14:02:36 +080047#define BTUSB_ATH3012 0x80
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -070048#define BTUSB_INTEL 0x100
Marcel Holtmann40df7832014-07-06 13:29:58 +020049#define BTUSB_INTEL_BOOT 0x200
50#define BTUSB_BCM_PATCHRAM 0x400
Amitkumar Karwarae8df492014-07-18 14:47:06 -070051#define BTUSB_MARVELL 0x800
Marcel Holtmann4fcef8e2015-01-01 17:34:37 -080052#define BTUSB_SWAVE 0x1000
Marcel Holtmanncda0dd72015-01-26 21:33:48 -080053#define BTUSB_INTEL_NEW 0x2000
Marcel Holtmann893ba542015-01-28 20:27:34 -080054#define BTUSB_AMP 0x4000
Marcel Holtmann5e23b922007-10-20 14:12:34 +020055
Marcel Holtmann54265202013-10-11 07:46:18 -070056static const struct usb_device_id btusb_table[] = {
Marcel Holtmann5e23b922007-10-20 14:12:34 +020057 /* Generic Bluetooth USB device */
58 { USB_DEVICE_INFO(0xe0, 0x01, 0x01) },
59
Marcel Holtmann893ba542015-01-28 20:27:34 -080060 /* Generic Bluetooth AMP device */
61 { USB_DEVICE_INFO(0xe0, 0x01, 0x04), .driver_info = BTUSB_AMP },
62
Henrik Rydberg1fa65352012-08-25 19:28:06 +020063 /* Apple-specific (Broadcom) devices */
64 { USB_VENDOR_AND_INTERFACE_INFO(0x05ac, 0xff, 0x01, 0x01) },
65
Cho, Yu-Chen178c0592013-06-04 21:40:26 +080066 /* MediaTek MT76x0E */
67 { USB_DEVICE(0x0e8d, 0x763f) },
68
Oliver Neukumc510eae2011-09-21 11:41:45 +020069 /* Broadcom SoftSailing reporting vendor specific */
Don Zickus2e8b50632012-03-28 16:41:11 -040070 { USB_DEVICE(0x0a5c, 0x21e1) },
Oliver Neukumc510eae2011-09-21 11:41:45 +020071
Nobuhiro Iwamatsu3cd01972010-08-20 16:24:07 +090072 /* Apple MacBookPro 7,1 */
73 { USB_DEVICE(0x05ac, 0x8213) },
74
Cyril Lacoux0a79f672010-07-14 10:29:27 +040075 /* Apple iMac11,1 */
76 { USB_DEVICE(0x05ac, 0x8215) },
77
Nobuhiro Iwamatsu9c047152010-08-20 16:24:06 +090078 /* Apple MacBookPro6,2 */
79 { USB_DEVICE(0x05ac, 0x8218) },
80
Edgar (gimli) Hucek3e3ede72010-11-04 08:04:33 +010081 /* Apple MacBookAir3,1, MacBookAir3,2 */
82 { USB_DEVICE(0x05ac, 0x821b) },
83
Pieter-Augustijn Van Malleghema63b7232011-09-07 02:28:10 -040084 /* Apple MacBookAir4,1 */
85 { USB_DEVICE(0x05ac, 0x821f) },
86
Marc-Antoine Perennou88d377b2011-03-24 14:51:21 -030087 /* Apple MacBookPro8,2 */
88 { USB_DEVICE(0x05ac, 0x821a) },
89
Jurgen Kramerf78b68262011-09-04 18:01:42 +020090 /* Apple MacMini5,1 */
91 { USB_DEVICE(0x05ac, 0x8281) },
92
Marcel Holtmanncfeb4142008-08-07 22:26:56 +020093 /* AVM BlueFRITZ! USB v2.0 */
Marcel Holtmann4fcef8e2015-01-01 17:34:37 -080094 { USB_DEVICE(0x057c, 0x3800), .driver_info = BTUSB_SWAVE },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +020095
96 /* Bluetooth Ultraport Module from IBM */
97 { USB_DEVICE(0x04bf, 0x030a) },
98
99 /* ALPS Modules with non-standard id */
100 { USB_DEVICE(0x044e, 0x3001) },
101 { USB_DEVICE(0x044e, 0x3002) },
102
103 /* Ericsson with non-standard id */
104 { USB_DEVICE(0x0bdb, 0x1002) },
105
106 /* Canyon CN-BTU1 with HID interfaces */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100107 { USB_DEVICE(0x0c10, 0x0000) },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200108
Wen-chien Jesse Sungd13431c2011-11-08 14:30:22 +0800109 /* Broadcom BCM20702A0 */
Andy Shevchenko0b880062014-02-18 18:26:19 +0200110 { USB_DEVICE(0x0489, 0xe042) },
111 { USB_DEVICE(0x04ca, 0x2003) },
Jeff Cook1ee3ff62012-11-09 16:39:48 -0700112 { USB_DEVICE(0x0b05, 0x17b5) },
Raphael Kubo da Costa38a172b2013-09-02 14:57:51 +0300113 { USB_DEVICE(0x0b05, 0x17cb) },
Wen-chien Jesse Sungd13431c2011-11-08 14:30:22 +0800114 { USB_DEVICE(0x413c, 0x8197) },
Fabio Ka86c02e2014-11-18 00:46:28 -0200115 { USB_DEVICE(0x13d3, 0x3404),
116 .driver_info = BTUSB_BCM_PATCHRAM },
Wen-chien Jesse Sungd13431c2011-11-08 14:30:22 +0800117
Marcel Holtmannd049f4e2015-01-26 20:35:32 -0800118 /* Broadcom BCM20702B0 (Dynex/Insignia) */
119 { USB_DEVICE(0x19ff, 0x0239), .driver_info = BTUSB_BCM_PATCHRAM },
120
Steven Harms98514032012-04-13 14:45:55 -0400121 /* Foxconn - Hon Hai */
Heinrich Siebmanns6029ddc2014-12-03 19:32:22 +0100122 { USB_VENDOR_AND_INTERFACE_INFO(0x0489, 0xff, 0x01, 0x01),
123 .driver_info = BTUSB_BCM_PATCHRAM },
Steven Harms98514032012-04-13 14:45:55 -0400124
Matej Dubovy8f0c3042015-02-02 18:50:14 +0100125 /* Lite-On Technology - Broadcom based */
126 { USB_VENDOR_AND_INTERFACE_INFO(0x04ca, 0xff, 0x01, 0x01),
127 .driver_info = BTUSB_BCM_PATCHRAM },
128
Andy Shevchenko0b880062014-02-18 18:26:19 +0200129 /* Broadcom devices with vendor specific id */
Petri Gynther10d4c672014-05-08 15:50:01 -0700130 { USB_VENDOR_AND_INTERFACE_INFO(0x0a5c, 0xff, 0x01, 0x01),
131 .driver_info = BTUSB_BCM_PATCHRAM },
Gustavo Padovan92c385f2012-08-06 15:36:49 -0300132
Marcel Holtmannc2aef6e2014-07-21 14:02:33 +0200133 /* ASUSTek Computer - Broadcom based */
Rick Dunn9a5abdaa2015-01-17 05:29:12 +0100134 { USB_VENDOR_AND_INTERFACE_INFO(0x0b05, 0xff, 0x01, 0x01),
135 .driver_info = BTUSB_BCM_PATCHRAM },
Marcel Holtmannc2aef6e2014-07-21 14:02:33 +0200136
Ken O'Brien5bcecf32013-09-21 19:14:43 +0100137 /* Belkin F8065bf - Broadcom based */
138 { USB_VENDOR_AND_INTERFACE_INFO(0x050d, 0xff, 0x01, 0x01) },
139
Jurgen Kramer9113bfd2014-02-15 12:01:09 +0100140 /* IMC Networks - Broadcom based */
141 { USB_VENDOR_AND_INTERFACE_INFO(0x13d3, 0xff, 0x01, 0x01) },
142
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
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200216 /* Broadcom BCM2035 */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100217 { USB_DEVICE(0x0a5c, 0x2009), .driver_info = BTUSB_BCM92035 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200218 { USB_DEVICE(0x0a5c, 0x200a), .driver_info = BTUSB_WRONG_SCO_MTU },
219 { USB_DEVICE(0x0a5c, 0x2035), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200220
221 /* Broadcom BCM2045 */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100222 { USB_DEVICE(0x0a5c, 0x2039), .driver_info = BTUSB_WRONG_SCO_MTU },
223 { USB_DEVICE(0x0a5c, 0x2101), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmannbdbef3d2008-09-23 00:16:35 +0200224
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200225 /* IBM/Lenovo ThinkPad with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100226 { USB_DEVICE(0x0a5c, 0x201e), .driver_info = BTUSB_WRONG_SCO_MTU },
227 { USB_DEVICE(0x0a5c, 0x2110), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200228
229 /* HP laptop with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100230 { USB_DEVICE(0x03f0, 0x171d), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200231
232 /* Dell laptop with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100233 { USB_DEVICE(0x413c, 0x8126), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200234
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100235 /* Dell Wireless 370 and 410 devices */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100236 { USB_DEVICE(0x413c, 0x8152), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100237 { USB_DEVICE(0x413c, 0x8156), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200238
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100239 /* Belkin F8T012 and F8T013 devices */
240 { USB_DEVICE(0x050d, 0x0012), .driver_info = BTUSB_WRONG_SCO_MTU },
241 { USB_DEVICE(0x050d, 0x0013), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200242
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100243 /* Asus WL-BTD202 device */
244 { USB_DEVICE(0x0b05, 0x1715), .driver_info = BTUSB_WRONG_SCO_MTU },
245
246 /* Kensington Bluetooth USB adapter */
247 { USB_DEVICE(0x047d, 0x105e), .driver_info = BTUSB_WRONG_SCO_MTU },
248
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200249 /* RTX Telecom based adapters with buggy SCO support */
250 { USB_DEVICE(0x0400, 0x0807), .driver_info = BTUSB_BROKEN_ISOC },
251 { USB_DEVICE(0x0400, 0x080a), .driver_info = BTUSB_BROKEN_ISOC },
252
253 /* CONWISE Technology based adapters with buggy SCO support */
254 { USB_DEVICE(0x0e5e, 0x6622), .driver_info = BTUSB_BROKEN_ISOC },
255
Marcel Holtmann4fcef8e2015-01-01 17:34:37 -0800256 /* Roper Class 1 Bluetooth Dongle (Silicon Wave based) */
257 { USB_DEVICE(0x1300, 0x0001), .driver_info = BTUSB_SWAVE },
258
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200259 /* Digianswer devices */
260 { USB_DEVICE(0x08fd, 0x0001), .driver_info = BTUSB_DIGIANSWER },
261 { USB_DEVICE(0x08fd, 0x0002), .driver_info = BTUSB_IGNORE },
262
263 /* CSR BlueCore Bluetooth Sniffer */
Marcel Holtmann4f64fa82014-07-07 00:12:04 +0200264 { USB_DEVICE(0x0a12, 0x0002),
265 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200266
267 /* Frontline ComProbe Bluetooth Sniffer */
Marcel Holtmann4f64fa82014-07-07 00:12:04 +0200268 { USB_DEVICE(0x16d3, 0x0002),
269 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200270
Marcel Holtmanncb1ee892015-01-28 19:41:42 -0800271 /* Marvell Bluetooth devices */
272 { USB_DEVICE(0x1286, 0x2044), .driver_info = BTUSB_MARVELL },
273 { USB_DEVICE(0x1286, 0x2046), .driver_info = BTUSB_MARVELL },
274
Marcel Holtmannd0ac9eb2015-01-28 19:41:43 -0800275 /* Intel Bluetooth devices */
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -0700276 { USB_DEVICE(0x8087, 0x07dc), .driver_info = BTUSB_INTEL },
Tedd Ho-Jeong Anef4e5e42013-11-12 13:10:58 -0800277 { USB_DEVICE(0x8087, 0x0a2a), .driver_info = BTUSB_INTEL },
Marcel Holtmanncda0dd72015-01-26 21:33:48 -0800278 { USB_DEVICE(0x8087, 0x0a2b), .driver_info = BTUSB_INTEL_NEW },
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -0700279
Marcel Holtmannd0ac9eb2015-01-28 19:41:43 -0800280 /* Other Intel Bluetooth devices */
281 { USB_VENDOR_AND_INTERFACE_INFO(0x8087, 0xe0, 0x01, 0x01),
282 .driver_info = BTUSB_IGNORE },
Amitkumar Karwarae8df492014-07-18 14:47:06 -0700283
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200284 { } /* Terminating entry */
285};
286
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200287#define BTUSB_MAX_ISOC_FRAMES 10
288
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200289#define BTUSB_INTR_RUNNING 0
290#define BTUSB_BULK_RUNNING 1
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200291#define BTUSB_ISOC_RUNNING 2
Oliver Neukum7bee5492009-08-24 23:44:59 +0200292#define BTUSB_SUSPENDING 3
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -0300293#define BTUSB_DID_ISO_RESUME 4
Marcel Holtmanncda0dd72015-01-26 21:33:48 -0800294#define BTUSB_BOOTLOADER 5
295#define BTUSB_DOWNLOADING 6
Marcel Holtmannce6bb922015-01-28 01:58:40 -0800296#define BTUSB_FIRMWARE_LOADED 7
Marcel Holtmanncda0dd72015-01-26 21:33:48 -0800297#define BTUSB_FIRMWARE_FAILED 8
Marcel Holtmannce6bb922015-01-28 01:58:40 -0800298#define BTUSB_BOOTING 9
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200299
300struct btusb_data {
301 struct hci_dev *hdev;
302 struct usb_device *udev;
Marcel Holtmann5fbcd262008-09-23 00:16:36 +0200303 struct usb_interface *intf;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200304 struct usb_interface *isoc;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200305
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200306 unsigned long flags;
307
308 struct work_struct work;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200309 struct work_struct waker;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200310
Marcel Holtmann803b5832014-09-16 08:00:29 +0200311 struct usb_anchor deferred;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200312 struct usb_anchor tx_anchor;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200313 int tx_in_flight;
314 spinlock_t txlock;
315
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200316 struct usb_anchor intr_anchor;
317 struct usb_anchor bulk_anchor;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200318 struct usb_anchor isoc_anchor;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200319 spinlock_t rxlock;
320
321 struct sk_buff *evt_skb;
322 struct sk_buff *acl_skb;
323 struct sk_buff *sco_skb;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200324
325 struct usb_endpoint_descriptor *intr_ep;
326 struct usb_endpoint_descriptor *bulk_tx_ep;
327 struct usb_endpoint_descriptor *bulk_rx_ep;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200328 struct usb_endpoint_descriptor *isoc_tx_ep;
329 struct usb_endpoint_descriptor *isoc_rx_ep;
330
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100331 __u8 cmdreq_type;
Marcel Holtmann893ba542015-01-28 20:27:34 -0800332 __u8 cmdreq;
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100333
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100334 unsigned int sco_num;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200335 int isoc_altsetting;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +0100336 int suspend_count;
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +0100337
Marcel Holtmann97307f52015-01-12 13:51:10 -0800338 int (*recv_event)(struct hci_dev *hdev, struct sk_buff *skb);
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +0100339 int (*recv_bulk)(struct btusb_data *data, void *buffer, int count);
Kim, Ben Young Taeace31982015-02-15 23:06:14 +0000340
341 int (*setup_on_usb)(struct hci_dev *hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200342};
343
Marcel Holtmann803b5832014-09-16 08:00:29 +0200344static inline void btusb_free_frags(struct btusb_data *data)
345{
346 unsigned long flags;
347
348 spin_lock_irqsave(&data->rxlock, flags);
349
350 kfree_skb(data->evt_skb);
351 data->evt_skb = NULL;
352
353 kfree_skb(data->acl_skb);
354 data->acl_skb = NULL;
355
356 kfree_skb(data->sco_skb);
357 data->sco_skb = NULL;
358
359 spin_unlock_irqrestore(&data->rxlock, flags);
360}
361
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200362static int btusb_recv_intr(struct btusb_data *data, void *buffer, int count)
363{
Marcel Holtmann803b5832014-09-16 08:00:29 +0200364 struct sk_buff *skb;
365 int err = 0;
366
367 spin_lock(&data->rxlock);
368 skb = data->evt_skb;
369
370 while (count) {
371 int len;
372
373 if (!skb) {
374 skb = bt_skb_alloc(HCI_MAX_EVENT_SIZE, GFP_ATOMIC);
375 if (!skb) {
376 err = -ENOMEM;
377 break;
378 }
379
380 bt_cb(skb)->pkt_type = HCI_EVENT_PKT;
381 bt_cb(skb)->expect = HCI_EVENT_HDR_SIZE;
382 }
383
384 len = min_t(uint, bt_cb(skb)->expect, count);
385 memcpy(skb_put(skb, len), buffer, len);
386
387 count -= len;
388 buffer += len;
389 bt_cb(skb)->expect -= len;
390
391 if (skb->len == HCI_EVENT_HDR_SIZE) {
392 /* Complete event header */
393 bt_cb(skb)->expect = hci_event_hdr(skb)->plen;
394
395 if (skb_tailroom(skb) < bt_cb(skb)->expect) {
396 kfree_skb(skb);
397 skb = NULL;
398
399 err = -EILSEQ;
400 break;
401 }
402 }
403
404 if (bt_cb(skb)->expect == 0) {
405 /* Complete frame */
Marcel Holtmann97307f52015-01-12 13:51:10 -0800406 data->recv_event(data->hdev, skb);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200407 skb = NULL;
408 }
409 }
410
411 data->evt_skb = skb;
412 spin_unlock(&data->rxlock);
413
414 return err;
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200415}
416
417static int btusb_recv_bulk(struct btusb_data *data, void *buffer, int count)
418{
Marcel Holtmann803b5832014-09-16 08:00:29 +0200419 struct sk_buff *skb;
420 int err = 0;
421
422 spin_lock(&data->rxlock);
423 skb = data->acl_skb;
424
425 while (count) {
426 int len;
427
428 if (!skb) {
429 skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
430 if (!skb) {
431 err = -ENOMEM;
432 break;
433 }
434
435 bt_cb(skb)->pkt_type = HCI_ACLDATA_PKT;
436 bt_cb(skb)->expect = HCI_ACL_HDR_SIZE;
437 }
438
439 len = min_t(uint, bt_cb(skb)->expect, count);
440 memcpy(skb_put(skb, len), buffer, len);
441
442 count -= len;
443 buffer += len;
444 bt_cb(skb)->expect -= len;
445
446 if (skb->len == HCI_ACL_HDR_SIZE) {
447 __le16 dlen = hci_acl_hdr(skb)->dlen;
448
449 /* Complete ACL header */
450 bt_cb(skb)->expect = __le16_to_cpu(dlen);
451
452 if (skb_tailroom(skb) < bt_cb(skb)->expect) {
453 kfree_skb(skb);
454 skb = NULL;
455
456 err = -EILSEQ;
457 break;
458 }
459 }
460
461 if (bt_cb(skb)->expect == 0) {
462 /* Complete frame */
463 hci_recv_frame(data->hdev, skb);
464 skb = NULL;
465 }
466 }
467
468 data->acl_skb = skb;
469 spin_unlock(&data->rxlock);
470
471 return err;
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200472}
473
474static int btusb_recv_isoc(struct btusb_data *data, void *buffer, int count)
475{
Marcel Holtmann803b5832014-09-16 08:00:29 +0200476 struct sk_buff *skb;
477 int err = 0;
478
479 spin_lock(&data->rxlock);
480 skb = data->sco_skb;
481
482 while (count) {
483 int len;
484
485 if (!skb) {
486 skb = bt_skb_alloc(HCI_MAX_SCO_SIZE, GFP_ATOMIC);
487 if (!skb) {
488 err = -ENOMEM;
489 break;
490 }
491
492 bt_cb(skb)->pkt_type = HCI_SCODATA_PKT;
493 bt_cb(skb)->expect = HCI_SCO_HDR_SIZE;
494 }
495
496 len = min_t(uint, bt_cb(skb)->expect, count);
497 memcpy(skb_put(skb, len), buffer, len);
498
499 count -= len;
500 buffer += len;
501 bt_cb(skb)->expect -= len;
502
503 if (skb->len == HCI_SCO_HDR_SIZE) {
504 /* Complete SCO header */
505 bt_cb(skb)->expect = hci_sco_hdr(skb)->dlen;
506
507 if (skb_tailroom(skb) < bt_cb(skb)->expect) {
508 kfree_skb(skb);
509 skb = NULL;
510
511 err = -EILSEQ;
512 break;
513 }
514 }
515
516 if (bt_cb(skb)->expect == 0) {
517 /* Complete frame */
518 hci_recv_frame(data->hdev, skb);
519 skb = NULL;
520 }
521 }
522
523 data->sco_skb = skb;
524 spin_unlock(&data->rxlock);
525
526 return err;
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200527}
528
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200529static void btusb_intr_complete(struct urb *urb)
530{
531 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +0100532 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200533 int err;
534
Marcel Holtmann89e75332014-09-16 04:44:50 +0200535 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
536 urb->actual_length);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200537
538 if (!test_bit(HCI_RUNNING, &hdev->flags))
539 return;
540
541 if (urb->status == 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200542 hdev->stat.byte_rx += urb->actual_length;
543
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200544 if (btusb_recv_intr(data, urb->transfer_buffer,
545 urb->actual_length) < 0) {
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200546 BT_ERR("%s corrupted event packet", hdev->name);
547 hdev->stat.err_rx++;
548 }
Champion Chen85560c42014-09-06 14:06:08 -0500549 } else if (urb->status == -ENOENT) {
550 /* Avoid suspend failed when usb_kill_urb */
551 return;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200552 }
553
554 if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
555 return;
556
Oliver Neukum7bee5492009-08-24 23:44:59 +0200557 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200558 usb_anchor_urb(urb, &data->intr_anchor);
559
560 err = usb_submit_urb(urb, GFP_ATOMIC);
561 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200562 /* -EPERM: urb is being killed;
563 * -ENODEV: device got disconnected */
564 if (err != -EPERM && err != -ENODEV)
Stefan Seyfried61faddf2010-11-30 21:49:08 +0100565 BT_ERR("%s urb %p failed to resubmit (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200566 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200567 usb_unanchor_urb(urb);
568 }
569}
570
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100571static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200572{
David Herrmann155961e2012-02-09 21:58:32 +0100573 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200574 struct urb *urb;
575 unsigned char *buf;
576 unsigned int pipe;
577 int err, size;
578
579 BT_DBG("%s", hdev->name);
580
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200581 if (!data->intr_ep)
582 return -ENODEV;
583
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100584 urb = usb_alloc_urb(0, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200585 if (!urb)
586 return -ENOMEM;
587
588 size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
589
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100590 buf = kmalloc(size, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200591 if (!buf) {
592 usb_free_urb(urb);
593 return -ENOMEM;
594 }
595
596 pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
597
598 usb_fill_int_urb(urb, data->udev, pipe, buf, size,
Marcel Holtmann89e75332014-09-16 04:44:50 +0200599 btusb_intr_complete, hdev, data->intr_ep->bInterval);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200600
601 urb->transfer_flags |= URB_FREE_BUFFER;
602
603 usb_anchor_urb(urb, &data->intr_anchor);
604
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100605 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200606 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +0200607 if (err != -EPERM && err != -ENODEV)
608 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200609 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200610 usb_unanchor_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200611 }
612
613 usb_free_urb(urb);
614
615 return err;
616}
617
618static void btusb_bulk_complete(struct urb *urb)
619{
620 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +0100621 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200622 int err;
623
Marcel Holtmann89e75332014-09-16 04:44:50 +0200624 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
625 urb->actual_length);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200626
627 if (!test_bit(HCI_RUNNING, &hdev->flags))
628 return;
629
630 if (urb->status == 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200631 hdev->stat.byte_rx += urb->actual_length;
632
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +0100633 if (data->recv_bulk(data, urb->transfer_buffer,
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200634 urb->actual_length) < 0) {
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200635 BT_ERR("%s corrupted ACL packet", hdev->name);
636 hdev->stat.err_rx++;
637 }
Champion Chen85560c42014-09-06 14:06:08 -0500638 } else if (urb->status == -ENOENT) {
639 /* Avoid suspend failed when usb_kill_urb */
640 return;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200641 }
642
643 if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
644 return;
645
646 usb_anchor_urb(urb, &data->bulk_anchor);
Oliver Neukum652fd782009-12-16 19:23:43 +0100647 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200648
649 err = usb_submit_urb(urb, GFP_ATOMIC);
650 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200651 /* -EPERM: urb is being killed;
652 * -ENODEV: device got disconnected */
653 if (err != -EPERM && err != -ENODEV)
Stefan Seyfried61faddf2010-11-30 21:49:08 +0100654 BT_ERR("%s urb %p failed to resubmit (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200655 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200656 usb_unanchor_urb(urb);
657 }
658}
659
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100660static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200661{
David Herrmann155961e2012-02-09 21:58:32 +0100662 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200663 struct urb *urb;
664 unsigned char *buf;
665 unsigned int pipe;
Vikram Kandukuri290ba202009-07-02 14:31:59 +0530666 int err, size = HCI_MAX_FRAME_SIZE;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200667
668 BT_DBG("%s", hdev->name);
669
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200670 if (!data->bulk_rx_ep)
671 return -ENODEV;
672
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100673 urb = usb_alloc_urb(0, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200674 if (!urb)
675 return -ENOMEM;
676
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100677 buf = kmalloc(size, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200678 if (!buf) {
679 usb_free_urb(urb);
680 return -ENOMEM;
681 }
682
683 pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
684
Marcel Holtmann89e75332014-09-16 04:44:50 +0200685 usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
686 btusb_bulk_complete, hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200687
688 urb->transfer_flags |= URB_FREE_BUFFER;
689
Oliver Neukum7bee5492009-08-24 23:44:59 +0200690 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200691 usb_anchor_urb(urb, &data->bulk_anchor);
692
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100693 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200694 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +0200695 if (err != -EPERM && err != -ENODEV)
696 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200697 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200698 usb_unanchor_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200699 }
700
701 usb_free_urb(urb);
702
703 return err;
704}
705
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200706static void btusb_isoc_complete(struct urb *urb)
707{
708 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +0100709 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200710 int i, err;
711
Marcel Holtmann89e75332014-09-16 04:44:50 +0200712 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
713 urb->actual_length);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200714
715 if (!test_bit(HCI_RUNNING, &hdev->flags))
716 return;
717
718 if (urb->status == 0) {
719 for (i = 0; i < urb->number_of_packets; i++) {
720 unsigned int offset = urb->iso_frame_desc[i].offset;
721 unsigned int length = urb->iso_frame_desc[i].actual_length;
722
723 if (urb->iso_frame_desc[i].status)
724 continue;
725
726 hdev->stat.byte_rx += length;
727
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200728 if (btusb_recv_isoc(data, urb->transfer_buffer + offset,
729 length) < 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200730 BT_ERR("%s corrupted SCO packet", hdev->name);
731 hdev->stat.err_rx++;
732 }
733 }
Champion Chen85560c42014-09-06 14:06:08 -0500734 } else if (urb->status == -ENOENT) {
735 /* Avoid suspend failed when usb_kill_urb */
736 return;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200737 }
738
739 if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags))
740 return;
741
742 usb_anchor_urb(urb, &data->isoc_anchor);
743
744 err = usb_submit_urb(urb, GFP_ATOMIC);
745 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200746 /* -EPERM: urb is being killed;
747 * -ENODEV: device got disconnected */
748 if (err != -EPERM && err != -ENODEV)
Stefan Seyfried61faddf2010-11-30 21:49:08 +0100749 BT_ERR("%s urb %p failed to resubmit (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200750 hdev->name, urb, -err);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200751 usb_unanchor_urb(urb);
752 }
753}
754
Jesper Juhl42b16b32011-01-17 00:09:38 +0100755static inline void __fill_isoc_descriptor(struct urb *urb, int len, int mtu)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200756{
757 int i, offset = 0;
758
759 BT_DBG("len %d mtu %d", len, mtu);
760
761 for (i = 0; i < BTUSB_MAX_ISOC_FRAMES && len >= mtu;
762 i++, offset += mtu, len -= mtu) {
763 urb->iso_frame_desc[i].offset = offset;
764 urb->iso_frame_desc[i].length = mtu;
765 }
766
767 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
768 urb->iso_frame_desc[i].offset = offset;
769 urb->iso_frame_desc[i].length = len;
770 i++;
771 }
772
773 urb->number_of_packets = i;
774}
775
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100776static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200777{
David Herrmann155961e2012-02-09 21:58:32 +0100778 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200779 struct urb *urb;
780 unsigned char *buf;
781 unsigned int pipe;
782 int err, size;
783
784 BT_DBG("%s", hdev->name);
785
786 if (!data->isoc_rx_ep)
787 return -ENODEV;
788
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100789 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200790 if (!urb)
791 return -ENOMEM;
792
793 size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) *
794 BTUSB_MAX_ISOC_FRAMES;
795
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100796 buf = kmalloc(size, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200797 if (!buf) {
798 usb_free_urb(urb);
799 return -ENOMEM;
800 }
801
802 pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress);
803
Bing Zhaofa0fb932011-12-20 18:19:00 -0800804 usb_fill_int_urb(urb, data->udev, pipe, buf, size, btusb_isoc_complete,
Marcel Holtmann89e75332014-09-16 04:44:50 +0200805 hdev, data->isoc_rx_ep->bInterval);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200806
Marcel Holtmann89e75332014-09-16 04:44:50 +0200807 urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200808
809 __fill_isoc_descriptor(urb, size,
Marcel Holtmann89e75332014-09-16 04:44:50 +0200810 le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize));
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200811
812 usb_anchor_urb(urb, &data->isoc_anchor);
813
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100814 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200815 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +0200816 if (err != -EPERM && err != -ENODEV)
817 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200818 hdev->name, urb, -err);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200819 usb_unanchor_urb(urb);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200820 }
821
822 usb_free_urb(urb);
823
824 return err;
825}
826
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200827static void btusb_tx_complete(struct urb *urb)
828{
829 struct sk_buff *skb = urb->context;
Marcel Holtmann89e75332014-09-16 04:44:50 +0200830 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
David Herrmann155961e2012-02-09 21:58:32 +0100831 struct btusb_data *data = hci_get_drvdata(hdev);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200832
Marcel Holtmann89e75332014-09-16 04:44:50 +0200833 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
834 urb->actual_length);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200835
836 if (!test_bit(HCI_RUNNING, &hdev->flags))
837 goto done;
838
839 if (!urb->status)
840 hdev->stat.byte_tx += urb->transfer_buffer_length;
841 else
842 hdev->stat.err_tx++;
843
844done:
845 spin_lock(&data->txlock);
846 data->tx_in_flight--;
847 spin_unlock(&data->txlock);
848
849 kfree(urb->setup_packet);
850
851 kfree_skb(skb);
852}
853
854static void btusb_isoc_tx_complete(struct urb *urb)
855{
856 struct sk_buff *skb = urb->context;
Marcel Holtmann89e75332014-09-16 04:44:50 +0200857 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200858
Marcel Holtmann89e75332014-09-16 04:44:50 +0200859 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
860 urb->actual_length);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200861
862 if (!test_bit(HCI_RUNNING, &hdev->flags))
863 goto done;
864
865 if (!urb->status)
866 hdev->stat.byte_tx += urb->transfer_buffer_length;
867 else
868 hdev->stat.err_tx++;
869
870done:
871 kfree(urb->setup_packet);
872
873 kfree_skb(skb);
874}
875
876static int btusb_open(struct hci_dev *hdev)
877{
David Herrmann155961e2012-02-09 21:58:32 +0100878 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200879 int err;
880
881 BT_DBG("%s", hdev->name);
882
Kim, Ben Young Taeace31982015-02-15 23:06:14 +0000883 /* Patching USB firmware files prior to starting any URBs of HCI path
884 * It is more safe to use USB bulk channel for downloading USB patch
885 */
886 if (data->setup_on_usb) {
887 err = data->setup_on_usb(hdev);
888 if (err <0)
889 return err;
890 }
891
Oliver Neukum7bee5492009-08-24 23:44:59 +0200892 err = usb_autopm_get_interface(data->intf);
893 if (err < 0)
894 return err;
895
896 data->intf->needs_remote_wakeup = 1;
897
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200898 if (test_and_set_bit(HCI_RUNNING, &hdev->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +0200899 goto done;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200900
901 if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +0200902 goto done;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200903
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100904 err = btusb_submit_intr_urb(hdev, GFP_KERNEL);
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100905 if (err < 0)
906 goto failed;
907
908 err = btusb_submit_bulk_urb(hdev, GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200909 if (err < 0) {
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100910 usb_kill_anchored_urbs(&data->intr_anchor);
911 goto failed;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200912 }
913
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100914 set_bit(BTUSB_BULK_RUNNING, &data->flags);
915 btusb_submit_bulk_urb(hdev, GFP_KERNEL);
916
Oliver Neukum7bee5492009-08-24 23:44:59 +0200917done:
918 usb_autopm_put_interface(data->intf);
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100919 return 0;
920
921failed:
922 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
923 clear_bit(HCI_RUNNING, &hdev->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200924 usb_autopm_put_interface(data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200925 return err;
926}
927
Oliver Neukum7bee5492009-08-24 23:44:59 +0200928static void btusb_stop_traffic(struct btusb_data *data)
929{
930 usb_kill_anchored_urbs(&data->intr_anchor);
931 usb_kill_anchored_urbs(&data->bulk_anchor);
932 usb_kill_anchored_urbs(&data->isoc_anchor);
933}
934
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200935static int btusb_close(struct hci_dev *hdev)
936{
David Herrmann155961e2012-02-09 21:58:32 +0100937 struct btusb_data *data = hci_get_drvdata(hdev);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200938 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200939
940 BT_DBG("%s", hdev->name);
941
942 if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
943 return 0;
944
Marcel Holtmanne8c3c3d2008-09-23 00:16:36 +0200945 cancel_work_sync(&data->work);
Linus Torvalds404291a2009-11-11 13:32:29 -0800946 cancel_work_sync(&data->waker);
Marcel Holtmanne8c3c3d2008-09-23 00:16:36 +0200947
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200948 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200949 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200950 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200951
952 btusb_stop_traffic(data);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200953 btusb_free_frags(data);
954
Oliver Neukum7bee5492009-08-24 23:44:59 +0200955 err = usb_autopm_get_interface(data->intf);
956 if (err < 0)
Oliver Neukum7b8e2c12009-11-13 14:26:23 +0100957 goto failed;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200958
959 data->intf->needs_remote_wakeup = 0;
960 usb_autopm_put_interface(data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200961
Oliver Neukum7b8e2c12009-11-13 14:26:23 +0100962failed:
963 usb_scuttle_anchored_urbs(&data->deferred);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200964 return 0;
965}
966
967static int btusb_flush(struct hci_dev *hdev)
968{
David Herrmann155961e2012-02-09 21:58:32 +0100969 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200970
971 BT_DBG("%s", hdev->name);
972
973 usb_kill_anchored_urbs(&data->tx_anchor);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200974 btusb_free_frags(data);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200975
976 return 0;
977}
978
Marcel Holtmann047b2ec2014-09-14 09:11:06 +0200979static struct urb *alloc_ctrl_urb(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200980{
David Herrmann155961e2012-02-09 21:58:32 +0100981 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200982 struct usb_ctrlrequest *dr;
983 struct urb *urb;
984 unsigned int pipe;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200985
Marcel Holtmann047b2ec2014-09-14 09:11:06 +0200986 urb = usb_alloc_urb(0, GFP_KERNEL);
987 if (!urb)
988 return ERR_PTR(-ENOMEM);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200989
Marcel Holtmann047b2ec2014-09-14 09:11:06 +0200990 dr = kmalloc(sizeof(*dr), GFP_KERNEL);
991 if (!dr) {
992 usb_free_urb(urb);
993 return ERR_PTR(-ENOMEM);
994 }
995
996 dr->bRequestType = data->cmdreq_type;
Marcel Holtmann893ba542015-01-28 20:27:34 -0800997 dr->bRequest = data->cmdreq;
Marcel Holtmann047b2ec2014-09-14 09:11:06 +0200998 dr->wIndex = 0;
999 dr->wValue = 0;
1000 dr->wLength = __cpu_to_le16(skb->len);
1001
1002 pipe = usb_sndctrlpipe(data->udev, 0x00);
1003
Marcel Holtmann89e75332014-09-16 04:44:50 +02001004 usb_fill_control_urb(urb, data->udev, pipe, (void *)dr,
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001005 skb->data, skb->len, btusb_tx_complete, skb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001006
Marcel Holtmann89e75332014-09-16 04:44:50 +02001007 skb->dev = (void *)hdev;
Marcel Holtmann7bd8f092013-10-11 06:19:18 -07001008
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001009 return urb;
1010}
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001011
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001012static struct urb *alloc_bulk_urb(struct hci_dev *hdev, struct sk_buff *skb)
1013{
1014 struct btusb_data *data = hci_get_drvdata(hdev);
1015 struct urb *urb;
1016 unsigned int pipe;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001017
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001018 if (!data->bulk_tx_ep)
1019 return ERR_PTR(-ENODEV);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001020
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001021 urb = usb_alloc_urb(0, GFP_KERNEL);
1022 if (!urb)
1023 return ERR_PTR(-ENOMEM);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001024
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001025 pipe = usb_sndbulkpipe(data->udev, data->bulk_tx_ep->bEndpointAddress);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001026
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001027 usb_fill_bulk_urb(urb, data->udev, pipe,
1028 skb->data, skb->len, btusb_tx_complete, skb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001029
Marcel Holtmann89e75332014-09-16 04:44:50 +02001030 skb->dev = (void *)hdev;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001031
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001032 return urb;
1033}
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001034
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001035static struct urb *alloc_isoc_urb(struct hci_dev *hdev, struct sk_buff *skb)
1036{
1037 struct btusb_data *data = hci_get_drvdata(hdev);
1038 struct urb *urb;
1039 unsigned int pipe;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001040
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001041 if (!data->isoc_tx_ep)
1042 return ERR_PTR(-ENODEV);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001043
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001044 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_KERNEL);
1045 if (!urb)
1046 return ERR_PTR(-ENOMEM);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001047
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001048 pipe = usb_sndisocpipe(data->udev, data->isoc_tx_ep->bEndpointAddress);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001049
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001050 usb_fill_int_urb(urb, data->udev, pipe,
1051 skb->data, skb->len, btusb_isoc_tx_complete,
1052 skb, data->isoc_tx_ep->bInterval);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001053
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001054 urb->transfer_flags = URB_ISO_ASAP;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001055
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001056 __fill_isoc_descriptor(urb, skb->len,
1057 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001058
Marcel Holtmann89e75332014-09-16 04:44:50 +02001059 skb->dev = (void *)hdev;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001060
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001061 return urb;
1062}
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001063
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001064static int submit_tx_urb(struct hci_dev *hdev, struct urb *urb)
1065{
1066 struct btusb_data *data = hci_get_drvdata(hdev);
1067 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001068
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001069 usb_anchor_urb(urb, &data->tx_anchor);
1070
Johan Hedberge9753ef2014-09-14 08:49:34 +03001071 err = usb_submit_urb(urb, GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001072 if (err < 0) {
Paul Bolle5a9b80e2011-10-09 12:12:16 +02001073 if (err != -EPERM && err != -ENODEV)
1074 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +02001075 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001076 kfree(urb->setup_packet);
1077 usb_unanchor_urb(urb);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001078 } else {
1079 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001080 }
1081
Cong Wang54a8a792011-11-22 09:32:57 +08001082 usb_free_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001083 return err;
1084}
1085
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001086static int submit_or_queue_tx_urb(struct hci_dev *hdev, struct urb *urb)
1087{
1088 struct btusb_data *data = hci_get_drvdata(hdev);
1089 unsigned long flags;
1090 bool suspending;
1091
1092 spin_lock_irqsave(&data->txlock, flags);
1093 suspending = test_bit(BTUSB_SUSPENDING, &data->flags);
1094 if (!suspending)
1095 data->tx_in_flight++;
1096 spin_unlock_irqrestore(&data->txlock, flags);
1097
1098 if (!suspending)
1099 return submit_tx_urb(hdev, urb);
1100
1101 usb_anchor_urb(urb, &data->deferred);
1102 schedule_work(&data->waker);
1103
1104 usb_free_urb(urb);
1105 return 0;
1106}
1107
1108static int btusb_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
1109{
1110 struct urb *urb;
1111
1112 BT_DBG("%s", hdev->name);
1113
1114 if (!test_bit(HCI_RUNNING, &hdev->flags))
1115 return -EBUSY;
1116
1117 switch (bt_cb(skb)->pkt_type) {
1118 case HCI_COMMAND_PKT:
1119 urb = alloc_ctrl_urb(hdev, skb);
1120 if (IS_ERR(urb))
1121 return PTR_ERR(urb);
1122
1123 hdev->stat.cmd_tx++;
1124 return submit_or_queue_tx_urb(hdev, urb);
1125
1126 case HCI_ACLDATA_PKT:
1127 urb = alloc_bulk_urb(hdev, skb);
1128 if (IS_ERR(urb))
1129 return PTR_ERR(urb);
1130
1131 hdev->stat.acl_tx++;
1132 return submit_or_queue_tx_urb(hdev, urb);
1133
1134 case HCI_SCODATA_PKT:
1135 if (hci_conn_num(hdev, SCO_LINK) < 1)
1136 return -ENODEV;
1137
1138 urb = alloc_isoc_urb(hdev, skb);
1139 if (IS_ERR(urb))
1140 return PTR_ERR(urb);
1141
1142 hdev->stat.sco_tx++;
1143 return submit_tx_urb(hdev, urb);
1144 }
1145
1146 return -EILSEQ;
1147}
1148
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001149static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
1150{
David Herrmann155961e2012-02-09 21:58:32 +01001151 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001152
1153 BT_DBG("%s evt %d", hdev->name, evt);
1154
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001155 if (hci_conn_num(hdev, SCO_LINK) != data->sco_num) {
1156 data->sco_num = hci_conn_num(hdev, SCO_LINK);
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001157 schedule_work(&data->work);
Marcel Holtmanna780efa2008-11-30 12:17:12 +01001158 }
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001159}
1160
Jesper Juhl42b16b32011-01-17 00:09:38 +01001161static inline int __set_isoc_interface(struct hci_dev *hdev, int altsetting)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001162{
David Herrmann155961e2012-02-09 21:58:32 +01001163 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001164 struct usb_interface *intf = data->isoc;
1165 struct usb_endpoint_descriptor *ep_desc;
1166 int i, err;
1167
1168 if (!data->isoc)
1169 return -ENODEV;
1170
1171 err = usb_set_interface(data->udev, 1, altsetting);
1172 if (err < 0) {
1173 BT_ERR("%s setting interface failed (%d)", hdev->name, -err);
1174 return err;
1175 }
1176
1177 data->isoc_altsetting = altsetting;
1178
1179 data->isoc_tx_ep = NULL;
1180 data->isoc_rx_ep = NULL;
1181
1182 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
1183 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
1184
1185 if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) {
1186 data->isoc_tx_ep = ep_desc;
1187 continue;
1188 }
1189
1190 if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) {
1191 data->isoc_rx_ep = ep_desc;
1192 continue;
1193 }
1194 }
1195
1196 if (!data->isoc_tx_ep || !data->isoc_rx_ep) {
1197 BT_ERR("%s invalid SCO descriptors", hdev->name);
1198 return -ENODEV;
1199 }
1200
1201 return 0;
1202}
1203
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001204static void btusb_work(struct work_struct *work)
1205{
1206 struct btusb_data *data = container_of(work, struct btusb_data, work);
1207 struct hci_dev *hdev = data->hdev;
Mikel Astizf4001d22012-04-11 08:48:51 +02001208 int new_alts;
Oliver Neukum7bee5492009-08-24 23:44:59 +02001209 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001210
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001211 if (data->sco_num > 0) {
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -03001212 if (!test_bit(BTUSB_DID_ISO_RESUME, &data->flags)) {
Oliver Neukum8efdd0c2011-02-11 13:00:06 +01001213 err = usb_autopm_get_interface(data->isoc ? data->isoc : data->intf);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001214 if (err < 0) {
1215 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1216 usb_kill_anchored_urbs(&data->isoc_anchor);
1217 return;
1218 }
1219
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -03001220 set_bit(BTUSB_DID_ISO_RESUME, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001221 }
Mikel Astizf4001d22012-04-11 08:48:51 +02001222
1223 if (hdev->voice_setting & 0x0020) {
1224 static const int alts[3] = { 2, 4, 5 };
Marcel Holtmann89e75332014-09-16 04:44:50 +02001225
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001226 new_alts = alts[data->sco_num - 1];
Mikel Astizf4001d22012-04-11 08:48:51 +02001227 } else {
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001228 new_alts = data->sco_num;
Mikel Astizf4001d22012-04-11 08:48:51 +02001229 }
1230
1231 if (data->isoc_altsetting != new_alts) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001232 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1233 usb_kill_anchored_urbs(&data->isoc_anchor);
1234
Mikel Astizf4001d22012-04-11 08:48:51 +02001235 if (__set_isoc_interface(hdev, new_alts) < 0)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001236 return;
1237 }
1238
1239 if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001240 if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001241 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1242 else
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001243 btusb_submit_isoc_urb(hdev, GFP_KERNEL);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001244 }
1245 } else {
1246 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1247 usb_kill_anchored_urbs(&data->isoc_anchor);
1248
1249 __set_isoc_interface(hdev, 0);
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -03001250 if (test_and_clear_bit(BTUSB_DID_ISO_RESUME, &data->flags))
Oliver Neukum8efdd0c2011-02-11 13:00:06 +01001251 usb_autopm_put_interface(data->isoc ? data->isoc : data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001252 }
1253}
1254
Oliver Neukum7bee5492009-08-24 23:44:59 +02001255static void btusb_waker(struct work_struct *work)
1256{
1257 struct btusb_data *data = container_of(work, struct btusb_data, waker);
1258 int err;
1259
1260 err = usb_autopm_get_interface(data->intf);
1261 if (err < 0)
1262 return;
1263
1264 usb_autopm_put_interface(data->intf);
1265}
1266
Marcel Holtmann9f8f9622013-04-10 08:11:35 -07001267static int btusb_setup_bcm92035(struct hci_dev *hdev)
1268{
1269 struct sk_buff *skb;
1270 u8 val = 0x00;
1271
1272 BT_DBG("%s", hdev->name);
1273
1274 skb = __hci_cmd_sync(hdev, 0xfc3b, 1, &val, HCI_INIT_TIMEOUT);
1275 if (IS_ERR(skb))
1276 BT_ERR("BCM92035 command failed (%ld)", -PTR_ERR(skb));
1277 else
1278 kfree_skb(skb);
1279
1280 return 0;
1281}
1282
Marcel Holtmann81cac642014-01-03 03:02:36 -08001283static int btusb_setup_csr(struct hci_dev *hdev)
1284{
1285 struct hci_rp_read_local_version *rp;
1286 struct sk_buff *skb;
1287 int ret;
1288
1289 BT_DBG("%s", hdev->name);
1290
1291 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
1292 HCI_INIT_TIMEOUT);
1293 if (IS_ERR(skb)) {
1294 BT_ERR("Reading local version failed (%ld)", -PTR_ERR(skb));
1295 return -PTR_ERR(skb);
1296 }
1297
Marcel Holtmann89e75332014-09-16 04:44:50 +02001298 rp = (struct hci_rp_read_local_version *)skb->data;
Marcel Holtmann81cac642014-01-03 03:02:36 -08001299
1300 if (!rp->status) {
1301 if (le16_to_cpu(rp->manufacturer) != 10) {
1302 /* Clear the reset quirk since this is not an actual
1303 * early Bluetooth 1.1 device from CSR.
1304 */
1305 clear_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
1306
1307 /* These fake CSR controllers have all a broken
1308 * stored link key handling and so just disable it.
1309 */
1310 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY,
1311 &hdev->quirks);
1312 }
1313 }
1314
1315 ret = -bt_to_errno(rp->status);
1316
1317 kfree_skb(skb);
1318
1319 return ret;
1320}
1321
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001322struct intel_version {
1323 u8 status;
1324 u8 hw_platform;
1325 u8 hw_variant;
1326 u8 hw_revision;
1327 u8 fw_variant;
1328 u8 fw_revision;
1329 u8 fw_build_num;
1330 u8 fw_build_ww;
1331 u8 fw_build_yy;
1332 u8 fw_patch_num;
1333} __packed;
1334
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001335struct intel_boot_params {
1336 __u8 status;
1337 __u8 otp_format;
1338 __u8 otp_content;
1339 __u8 otp_patch;
1340 __le16 dev_revid;
1341 __u8 secure_boot;
1342 __u8 key_from_hdr;
1343 __u8 key_type;
1344 __u8 otp_lock;
1345 __u8 api_lock;
1346 __u8 debug_lock;
1347 bdaddr_t otp_bdaddr;
1348 __u8 min_fw_build_nn;
1349 __u8 min_fw_build_cw;
1350 __u8 min_fw_build_yy;
1351 __u8 limited_cce;
1352 __u8 unlocked_state;
1353} __packed;
1354
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001355static const struct firmware *btusb_setup_intel_get_fw(struct hci_dev *hdev,
Marcel Holtmann89e75332014-09-16 04:44:50 +02001356 struct intel_version *ver)
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001357{
1358 const struct firmware *fw;
1359 char fwname[64];
1360 int ret;
1361
1362 snprintf(fwname, sizeof(fwname),
1363 "intel/ibt-hw-%x.%x.%x-fw-%x.%x.%x.%x.%x.bseq",
1364 ver->hw_platform, ver->hw_variant, ver->hw_revision,
1365 ver->fw_variant, ver->fw_revision, ver->fw_build_num,
1366 ver->fw_build_ww, ver->fw_build_yy);
1367
1368 ret = request_firmware(&fw, fwname, &hdev->dev);
1369 if (ret < 0) {
1370 if (ret == -EINVAL) {
1371 BT_ERR("%s Intel firmware file request failed (%d)",
1372 hdev->name, ret);
1373 return NULL;
1374 }
1375
1376 BT_ERR("%s failed to open Intel firmware file: %s(%d)",
1377 hdev->name, fwname, ret);
1378
1379 /* If the correct firmware patch file is not found, use the
1380 * default firmware patch file instead
1381 */
1382 snprintf(fwname, sizeof(fwname), "intel/ibt-hw-%x.%x.bseq",
1383 ver->hw_platform, ver->hw_variant);
1384 if (request_firmware(&fw, fwname, &hdev->dev) < 0) {
1385 BT_ERR("%s failed to open default Intel fw file: %s",
1386 hdev->name, fwname);
1387 return NULL;
1388 }
1389 }
1390
1391 BT_INFO("%s: Intel Bluetooth firmware file: %s", hdev->name, fwname);
1392
1393 return fw;
1394}
1395
1396static int btusb_setup_intel_patching(struct hci_dev *hdev,
1397 const struct firmware *fw,
1398 const u8 **fw_ptr, int *disable_patch)
1399{
1400 struct sk_buff *skb;
1401 struct hci_command_hdr *cmd;
1402 const u8 *cmd_param;
1403 struct hci_event_hdr *evt = NULL;
1404 const u8 *evt_param = NULL;
1405 int remain = fw->size - (*fw_ptr - fw->data);
1406
1407 /* The first byte indicates the types of the patch command or event.
1408 * 0x01 means HCI command and 0x02 is HCI event. If the first bytes
1409 * in the current firmware buffer doesn't start with 0x01 or
1410 * the size of remain buffer is smaller than HCI command header,
1411 * the firmware file is corrupted and it should stop the patching
1412 * process.
1413 */
1414 if (remain > HCI_COMMAND_HDR_SIZE && *fw_ptr[0] != 0x01) {
1415 BT_ERR("%s Intel fw corrupted: invalid cmd read", hdev->name);
1416 return -EINVAL;
1417 }
1418 (*fw_ptr)++;
1419 remain--;
1420
1421 cmd = (struct hci_command_hdr *)(*fw_ptr);
1422 *fw_ptr += sizeof(*cmd);
1423 remain -= sizeof(*cmd);
1424
1425 /* Ensure that the remain firmware data is long enough than the length
1426 * of command parameter. If not, the firmware file is corrupted.
1427 */
1428 if (remain < cmd->plen) {
1429 BT_ERR("%s Intel fw corrupted: invalid cmd len", hdev->name);
1430 return -EFAULT;
1431 }
1432
1433 /* If there is a command that loads a patch in the firmware
1434 * file, then enable the patch upon success, otherwise just
1435 * disable the manufacturer mode, for example patch activation
1436 * is not required when the default firmware patch file is used
1437 * because there are no patch data to load.
1438 */
1439 if (*disable_patch && le16_to_cpu(cmd->opcode) == 0xfc8e)
1440 *disable_patch = 0;
1441
1442 cmd_param = *fw_ptr;
1443 *fw_ptr += cmd->plen;
1444 remain -= cmd->plen;
1445
1446 /* This reads the expected events when the above command is sent to the
1447 * device. Some vendor commands expects more than one events, for
1448 * example command status event followed by vendor specific event.
1449 * For this case, it only keeps the last expected event. so the command
1450 * can be sent with __hci_cmd_sync_ev() which returns the sk_buff of
1451 * last expected event.
1452 */
1453 while (remain > HCI_EVENT_HDR_SIZE && *fw_ptr[0] == 0x02) {
1454 (*fw_ptr)++;
1455 remain--;
1456
1457 evt = (struct hci_event_hdr *)(*fw_ptr);
1458 *fw_ptr += sizeof(*evt);
1459 remain -= sizeof(*evt);
1460
1461 if (remain < evt->plen) {
1462 BT_ERR("%s Intel fw corrupted: invalid evt len",
1463 hdev->name);
1464 return -EFAULT;
1465 }
1466
1467 evt_param = *fw_ptr;
1468 *fw_ptr += evt->plen;
1469 remain -= evt->plen;
1470 }
1471
1472 /* Every HCI commands in the firmware file has its correspond event.
1473 * If event is not found or remain is smaller than zero, the firmware
1474 * file is corrupted.
1475 */
1476 if (!evt || !evt_param || remain < 0) {
1477 BT_ERR("%s Intel fw corrupted: invalid evt read", hdev->name);
1478 return -EFAULT;
1479 }
1480
1481 skb = __hci_cmd_sync_ev(hdev, le16_to_cpu(cmd->opcode), cmd->plen,
1482 cmd_param, evt->evt, HCI_INIT_TIMEOUT);
1483 if (IS_ERR(skb)) {
1484 BT_ERR("%s sending Intel patch command (0x%4.4x) failed (%ld)",
1485 hdev->name, cmd->opcode, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001486 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001487 }
1488
1489 /* It ensures that the returned event matches the event data read from
1490 * the firmware file. At fist, it checks the length and then
1491 * the contents of the event.
1492 */
1493 if (skb->len != evt->plen) {
1494 BT_ERR("%s mismatch event length (opcode 0x%4.4x)", hdev->name,
1495 le16_to_cpu(cmd->opcode));
1496 kfree_skb(skb);
1497 return -EFAULT;
1498 }
1499
1500 if (memcmp(skb->data, evt_param, evt->plen)) {
1501 BT_ERR("%s mismatch event parameter (opcode 0x%4.4x)",
1502 hdev->name, le16_to_cpu(cmd->opcode));
1503 kfree_skb(skb);
1504 return -EFAULT;
1505 }
1506 kfree_skb(skb);
1507
1508 return 0;
1509}
1510
Marcel Holtmann40cb0982014-07-02 12:06:45 +02001511#define BDADDR_INTEL (&(bdaddr_t) {{0x00, 0x8b, 0x9e, 0x19, 0x03, 0x00}})
1512
1513static int btusb_check_bdaddr_intel(struct hci_dev *hdev)
1514{
1515 struct sk_buff *skb;
1516 struct hci_rp_read_bd_addr *rp;
1517
1518 skb = __hci_cmd_sync(hdev, HCI_OP_READ_BD_ADDR, 0, NULL,
1519 HCI_INIT_TIMEOUT);
1520 if (IS_ERR(skb)) {
1521 BT_ERR("%s reading Intel device address failed (%ld)",
1522 hdev->name, PTR_ERR(skb));
1523 return PTR_ERR(skb);
1524 }
1525
1526 if (skb->len != sizeof(*rp)) {
1527 BT_ERR("%s Intel device address length mismatch", hdev->name);
1528 kfree_skb(skb);
1529 return -EIO;
1530 }
1531
Marcel Holtmann89e75332014-09-16 04:44:50 +02001532 rp = (struct hci_rp_read_bd_addr *)skb->data;
Marcel Holtmann40cb0982014-07-02 12:06:45 +02001533 if (rp->status) {
1534 BT_ERR("%s Intel device address result failed (%02x)",
1535 hdev->name, rp->status);
1536 kfree_skb(skb);
1537 return -bt_to_errno(rp->status);
1538 }
1539
1540 /* For some Intel based controllers, the default Bluetooth device
1541 * address 00:03:19:9E:8B:00 can be found. These controllers are
1542 * fully operational, but have the danger of duplicate addresses
1543 * and that in turn can cause problems with Bluetooth operation.
1544 */
Marcel Holtmann4739b5b2014-07-04 16:54:38 +02001545 if (!bacmp(&rp->bdaddr, BDADDR_INTEL)) {
Marcel Holtmann40cb0982014-07-02 12:06:45 +02001546 BT_ERR("%s found Intel default device address (%pMR)",
1547 hdev->name, &rp->bdaddr);
Marcel Holtmann4739b5b2014-07-04 16:54:38 +02001548 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
1549 }
Marcel Holtmann40cb0982014-07-02 12:06:45 +02001550
1551 kfree_skb(skb);
1552
1553 return 0;
1554}
1555
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001556static int btusb_setup_intel(struct hci_dev *hdev)
1557{
1558 struct sk_buff *skb;
1559 const struct firmware *fw;
1560 const u8 *fw_ptr;
1561 int disable_patch;
1562 struct intel_version *ver;
1563
1564 const u8 mfg_enable[] = { 0x01, 0x00 };
1565 const u8 mfg_disable[] = { 0x00, 0x00 };
1566 const u8 mfg_reset_deactivate[] = { 0x00, 0x01 };
1567 const u8 mfg_reset_activate[] = { 0x00, 0x02 };
1568
1569 BT_DBG("%s", hdev->name);
1570
1571 /* The controller has a bug with the first HCI command sent to it
1572 * returning number of completed commands as zero. This would stall the
1573 * command processing in the Bluetooth core.
1574 *
1575 * As a workaround, send HCI Reset command first which will reset the
1576 * number of completed commands and allow normal command processing
1577 * from now on.
1578 */
1579 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
1580 if (IS_ERR(skb)) {
1581 BT_ERR("%s sending initial HCI reset command failed (%ld)",
1582 hdev->name, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001583 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001584 }
1585 kfree_skb(skb);
1586
1587 /* Read Intel specific controller version first to allow selection of
1588 * which firmware file to load.
1589 *
1590 * The returned information are hardware variant and revision plus
1591 * firmware variant, revision and build number.
1592 */
1593 skb = __hci_cmd_sync(hdev, 0xfc05, 0, NULL, HCI_INIT_TIMEOUT);
1594 if (IS_ERR(skb)) {
1595 BT_ERR("%s reading Intel fw version command failed (%ld)",
1596 hdev->name, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001597 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001598 }
1599
1600 if (skb->len != sizeof(*ver)) {
1601 BT_ERR("%s Intel version event length mismatch", hdev->name);
1602 kfree_skb(skb);
1603 return -EIO;
1604 }
1605
1606 ver = (struct intel_version *)skb->data;
1607 if (ver->status) {
1608 BT_ERR("%s Intel fw version event failed (%02x)", hdev->name,
1609 ver->status);
1610 kfree_skb(skb);
1611 return -bt_to_errno(ver->status);
1612 }
1613
1614 BT_INFO("%s: read Intel version: %02x%02x%02x%02x%02x%02x%02x%02x%02x",
1615 hdev->name, ver->hw_platform, ver->hw_variant,
1616 ver->hw_revision, ver->fw_variant, ver->fw_revision,
1617 ver->fw_build_num, ver->fw_build_ww, ver->fw_build_yy,
1618 ver->fw_patch_num);
1619
1620 /* fw_patch_num indicates the version of patch the device currently
1621 * have. If there is no patch data in the device, it is always 0x00.
1622 * So, if it is other than 0x00, no need to patch the deivce again.
1623 */
1624 if (ver->fw_patch_num) {
1625 BT_INFO("%s: Intel device is already patched. patch num: %02x",
1626 hdev->name, ver->fw_patch_num);
1627 kfree_skb(skb);
Marcel Holtmann40cb0982014-07-02 12:06:45 +02001628 btusb_check_bdaddr_intel(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001629 return 0;
1630 }
1631
1632 /* Opens the firmware patch file based on the firmware version read
1633 * from the controller. If it fails to open the matching firmware
1634 * patch file, it tries to open the default firmware patch file.
1635 * If no patch file is found, allow the device to operate without
1636 * a patch.
1637 */
1638 fw = btusb_setup_intel_get_fw(hdev, ver);
1639 if (!fw) {
1640 kfree_skb(skb);
Marcel Holtmann40cb0982014-07-02 12:06:45 +02001641 btusb_check_bdaddr_intel(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001642 return 0;
1643 }
1644 fw_ptr = fw->data;
1645
1646 /* This Intel specific command enables the manufacturer mode of the
1647 * controller.
1648 *
1649 * Only while this mode is enabled, the driver can download the
1650 * firmware patch data and configuration parameters.
1651 */
1652 skb = __hci_cmd_sync(hdev, 0xfc11, 2, mfg_enable, HCI_INIT_TIMEOUT);
1653 if (IS_ERR(skb)) {
1654 BT_ERR("%s entering Intel manufacturer mode failed (%ld)",
1655 hdev->name, PTR_ERR(skb));
1656 release_firmware(fw);
Adam Leed9c78e92013-07-10 10:02:12 +08001657 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001658 }
1659
1660 if (skb->data[0]) {
1661 u8 evt_status = skb->data[0];
Marcel Holtmann89e75332014-09-16 04:44:50 +02001662
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001663 BT_ERR("%s enable Intel manufacturer mode event failed (%02x)",
1664 hdev->name, evt_status);
1665 kfree_skb(skb);
1666 release_firmware(fw);
1667 return -bt_to_errno(evt_status);
1668 }
1669 kfree_skb(skb);
1670
1671 disable_patch = 1;
1672
1673 /* The firmware data file consists of list of Intel specific HCI
1674 * commands and its expected events. The first byte indicates the
1675 * type of the message, either HCI command or HCI event.
1676 *
1677 * It reads the command and its expected event from the firmware file,
1678 * and send to the controller. Once __hci_cmd_sync_ev() returns,
1679 * the returned event is compared with the event read from the firmware
1680 * file and it will continue until all the messages are downloaded to
1681 * the controller.
1682 *
1683 * Once the firmware patching is completed successfully,
1684 * the manufacturer mode is disabled with reset and activating the
1685 * downloaded patch.
1686 *
1687 * If the firmware patching fails, the manufacturer mode is
1688 * disabled with reset and deactivating the patch.
1689 *
1690 * If the default patch file is used, no reset is done when disabling
1691 * the manufacturer.
1692 */
1693 while (fw->size > fw_ptr - fw->data) {
1694 int ret;
1695
1696 ret = btusb_setup_intel_patching(hdev, fw, &fw_ptr,
1697 &disable_patch);
1698 if (ret < 0)
1699 goto exit_mfg_deactivate;
1700 }
1701
1702 release_firmware(fw);
1703
1704 if (disable_patch)
1705 goto exit_mfg_disable;
1706
1707 /* Patching completed successfully and disable the manufacturer mode
1708 * with reset and activate the downloaded firmware patches.
1709 */
1710 skb = __hci_cmd_sync(hdev, 0xfc11, sizeof(mfg_reset_activate),
1711 mfg_reset_activate, HCI_INIT_TIMEOUT);
1712 if (IS_ERR(skb)) {
1713 BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
1714 hdev->name, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001715 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001716 }
1717 kfree_skb(skb);
1718
1719 BT_INFO("%s: Intel Bluetooth firmware patch completed and activated",
1720 hdev->name);
1721
Marcel Holtmann40cb0982014-07-02 12:06:45 +02001722 btusb_check_bdaddr_intel(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001723 return 0;
1724
1725exit_mfg_disable:
1726 /* Disable the manufacturer mode without reset */
1727 skb = __hci_cmd_sync(hdev, 0xfc11, sizeof(mfg_disable), mfg_disable,
1728 HCI_INIT_TIMEOUT);
1729 if (IS_ERR(skb)) {
1730 BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
1731 hdev->name, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001732 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001733 }
1734 kfree_skb(skb);
1735
1736 BT_INFO("%s: Intel Bluetooth firmware patch completed", hdev->name);
Marcel Holtmann40cb0982014-07-02 12:06:45 +02001737
1738 btusb_check_bdaddr_intel(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001739 return 0;
1740
1741exit_mfg_deactivate:
1742 release_firmware(fw);
1743
1744 /* Patching failed. Disable the manufacturer mode with reset and
1745 * deactivate the downloaded firmware patches.
1746 */
1747 skb = __hci_cmd_sync(hdev, 0xfc11, sizeof(mfg_reset_deactivate),
1748 mfg_reset_deactivate, HCI_INIT_TIMEOUT);
1749 if (IS_ERR(skb)) {
1750 BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
1751 hdev->name, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001752 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001753 }
1754 kfree_skb(skb);
1755
1756 BT_INFO("%s: Intel Bluetooth firmware patch completed and deactivated",
1757 hdev->name);
1758
Marcel Holtmann40cb0982014-07-02 12:06:45 +02001759 btusb_check_bdaddr_intel(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001760 return 0;
1761}
1762
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001763static int inject_cmd_complete(struct hci_dev *hdev, __u16 opcode)
1764{
1765 struct sk_buff *skb;
1766 struct hci_event_hdr *hdr;
1767 struct hci_ev_cmd_complete *evt;
1768
1769 skb = bt_skb_alloc(sizeof(*hdr) + sizeof(*evt) + 1, GFP_ATOMIC);
1770 if (!skb)
1771 return -ENOMEM;
1772
1773 hdr = (struct hci_event_hdr *)skb_put(skb, sizeof(*hdr));
1774 hdr->evt = HCI_EV_CMD_COMPLETE;
1775 hdr->plen = sizeof(*evt) + 1;
1776
1777 evt = (struct hci_ev_cmd_complete *)skb_put(skb, sizeof(*evt));
1778 evt->ncmd = 0x01;
1779 evt->opcode = cpu_to_le16(opcode);
1780
1781 *skb_put(skb, 1) = 0x00;
1782
1783 bt_cb(skb)->pkt_type = HCI_EVENT_PKT;
1784
1785 return hci_recv_frame(hdev, skb);
1786}
1787
1788static int btusb_recv_bulk_intel(struct btusb_data *data, void *buffer,
1789 int count)
1790{
1791 /* When the device is in bootloader mode, then it can send
1792 * events via the bulk endpoint. These events are treated the
1793 * same way as the ones received from the interrupt endpoint.
1794 */
1795 if (test_bit(BTUSB_BOOTLOADER, &data->flags))
1796 return btusb_recv_intr(data, buffer, count);
1797
1798 return btusb_recv_bulk(data, buffer, count);
1799}
1800
1801static int btusb_recv_event_intel(struct hci_dev *hdev, struct sk_buff *skb)
1802{
1803 struct btusb_data *data = hci_get_drvdata(hdev);
1804
1805 if (test_bit(BTUSB_BOOTLOADER, &data->flags)) {
1806 struct hci_event_hdr *hdr = (void *)skb->data;
1807
1808 /* When the firmware loading completes the device sends
1809 * out a vendor specific event indicating the result of
1810 * the firmware loading.
1811 */
1812 if (skb->len == 7 && hdr->evt == 0xff && hdr->plen == 0x05 &&
1813 skb->data[2] == 0x06) {
1814 if (skb->data[3] != 0x00)
1815 test_bit(BTUSB_FIRMWARE_FAILED, &data->flags);
1816
Marcel Holtmannce6bb922015-01-28 01:58:40 -08001817 if (test_and_clear_bit(BTUSB_DOWNLOADING,
1818 &data->flags) &&
Johan Hedberga087a982015-01-30 10:58:54 +02001819 test_bit(BTUSB_FIRMWARE_LOADED, &data->flags)) {
1820 smp_mb__after_atomic();
1821 wake_up_bit(&data->flags, BTUSB_DOWNLOADING);
1822 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001823 }
1824
1825 /* When switching to the operational firmware the device
1826 * sends a vendor specific event indicating that the bootup
1827 * completed.
1828 */
1829 if (skb->len == 9 && hdr->evt == 0xff && hdr->plen == 0x07 &&
1830 skb->data[2] == 0x02) {
Johan Hedbergfad70972015-01-30 10:58:55 +02001831 if (test_and_clear_bit(BTUSB_BOOTING, &data->flags)) {
1832 smp_mb__after_atomic();
1833 wake_up_bit(&data->flags, BTUSB_BOOTING);
1834 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001835 }
1836 }
1837
1838 return hci_recv_frame(hdev, skb);
1839}
1840
1841static int btusb_send_frame_intel(struct hci_dev *hdev, struct sk_buff *skb)
1842{
1843 struct btusb_data *data = hci_get_drvdata(hdev);
1844 struct urb *urb;
1845
1846 BT_DBG("%s", hdev->name);
1847
1848 if (!test_bit(HCI_RUNNING, &hdev->flags))
1849 return -EBUSY;
1850
1851 switch (bt_cb(skb)->pkt_type) {
1852 case HCI_COMMAND_PKT:
1853 if (test_bit(BTUSB_BOOTLOADER, &data->flags)) {
1854 struct hci_command_hdr *cmd = (void *)skb->data;
1855 __u16 opcode = le16_to_cpu(cmd->opcode);
1856
1857 /* When in bootloader mode and the command 0xfc09
1858 * is received, it needs to be send down the
1859 * bulk endpoint. So allocate a bulk URB instead.
1860 */
1861 if (opcode == 0xfc09)
1862 urb = alloc_bulk_urb(hdev, skb);
1863 else
1864 urb = alloc_ctrl_urb(hdev, skb);
1865
1866 /* When the 0xfc01 command is issued to boot into
1867 * the operational firmware, it will actually not
1868 * send a command complete event. To keep the flow
1869 * control working inject that event here.
1870 */
1871 if (opcode == 0xfc01)
1872 inject_cmd_complete(hdev, opcode);
1873 } else {
1874 urb = alloc_ctrl_urb(hdev, skb);
1875 }
1876 if (IS_ERR(urb))
1877 return PTR_ERR(urb);
1878
1879 hdev->stat.cmd_tx++;
1880 return submit_or_queue_tx_urb(hdev, urb);
1881
1882 case HCI_ACLDATA_PKT:
1883 urb = alloc_bulk_urb(hdev, skb);
1884 if (IS_ERR(urb))
1885 return PTR_ERR(urb);
1886
1887 hdev->stat.acl_tx++;
1888 return submit_or_queue_tx_urb(hdev, urb);
1889
1890 case HCI_SCODATA_PKT:
1891 if (hci_conn_num(hdev, SCO_LINK) < 1)
1892 return -ENODEV;
1893
1894 urb = alloc_isoc_urb(hdev, skb);
1895 if (IS_ERR(urb))
1896 return PTR_ERR(urb);
1897
1898 hdev->stat.sco_tx++;
1899 return submit_tx_urb(hdev, urb);
1900 }
1901
1902 return -EILSEQ;
1903}
1904
1905static int btusb_intel_secure_send(struct hci_dev *hdev, u8 fragment_type,
1906 u32 plen, const void *param)
1907{
1908 while (plen > 0) {
1909 struct sk_buff *skb;
1910 u8 cmd_param[253], fragment_len = (plen > 252) ? 252 : plen;
1911
1912 cmd_param[0] = fragment_type;
1913 memcpy(cmd_param + 1, param, fragment_len);
1914
1915 skb = __hci_cmd_sync(hdev, 0xfc09, fragment_len + 1,
1916 cmd_param, HCI_INIT_TIMEOUT);
1917 if (IS_ERR(skb))
1918 return PTR_ERR(skb);
1919
1920 kfree_skb(skb);
1921
1922 plen -= fragment_len;
1923 param += fragment_len;
1924 }
1925
1926 return 0;
1927}
1928
1929static void btusb_intel_version_info(struct hci_dev *hdev,
1930 struct intel_version *ver)
1931{
1932 const char *variant;
1933
1934 switch (ver->fw_variant) {
1935 case 0x06:
1936 variant = "Bootloader";
1937 break;
1938 case 0x23:
1939 variant = "Firmware";
1940 break;
1941 default:
1942 return;
1943 }
1944
1945 BT_INFO("%s: %s revision %u.%u build %u week %u %u", hdev->name,
1946 variant, ver->fw_revision >> 4, ver->fw_revision & 0x0f,
1947 ver->fw_build_num, ver->fw_build_ww, 2000 + ver->fw_build_yy);
1948}
1949
1950static int btusb_setup_intel_new(struct hci_dev *hdev)
1951{
1952 static const u8 reset_param[] = { 0x00, 0x01, 0x00, 0x01,
1953 0x00, 0x08, 0x04, 0x00 };
1954 struct btusb_data *data = hci_get_drvdata(hdev);
1955 struct sk_buff *skb;
1956 struct intel_version *ver;
1957 struct intel_boot_params *params;
1958 const struct firmware *fw;
1959 const u8 *fw_ptr;
1960 char fwname[64];
1961 ktime_t calltime, delta, rettime;
1962 unsigned long long duration;
1963 int err;
1964
1965 BT_DBG("%s", hdev->name);
1966
1967 calltime = ktime_get();
1968
1969 /* Read the Intel version information to determine if the device
1970 * is in bootloader mode or if it already has operational firmware
1971 * loaded.
1972 */
1973 skb = __hci_cmd_sync(hdev, 0xfc05, 0, NULL, HCI_INIT_TIMEOUT);
1974 if (IS_ERR(skb)) {
1975 BT_ERR("%s: Reading Intel version information failed (%ld)",
1976 hdev->name, PTR_ERR(skb));
1977 return PTR_ERR(skb);
1978 }
1979
1980 if (skb->len != sizeof(*ver)) {
1981 BT_ERR("%s: Intel version event size mismatch", hdev->name);
1982 kfree_skb(skb);
1983 return -EILSEQ;
1984 }
1985
1986 ver = (struct intel_version *)skb->data;
1987 if (ver->status) {
1988 BT_ERR("%s: Intel version command failure (%02x)",
1989 hdev->name, ver->status);
1990 err = -bt_to_errno(ver->status);
1991 kfree_skb(skb);
1992 return err;
1993 }
1994
1995 /* The hardware platform number has a fixed value of 0x37 and
1996 * for now only accept this single value.
1997 */
1998 if (ver->hw_platform != 0x37) {
1999 BT_ERR("%s: Unsupported Intel hardware platform (%u)",
2000 hdev->name, ver->hw_platform);
2001 kfree_skb(skb);
2002 return -EINVAL;
2003 }
2004
2005 /* At the moment only the hardware variant iBT 3.0 (LnP/SfP) is
2006 * supported by this firmware loading method. This check has been
2007 * put in place to ensure correct forward compatibility options
2008 * when newer hardware variants come along.
2009 */
2010 if (ver->hw_variant != 0x0b) {
2011 BT_ERR("%s: Unsupported Intel hardware variant (%u)",
2012 hdev->name, ver->hw_variant);
2013 kfree_skb(skb);
2014 return -EINVAL;
2015 }
2016
2017 btusb_intel_version_info(hdev, ver);
2018
2019 /* The firmware variant determines if the device is in bootloader
2020 * mode or is running operational firmware. The value 0x06 identifies
2021 * the bootloader and the value 0x23 identifies the operational
2022 * firmware.
2023 *
2024 * When the operational firmware is already present, then only
2025 * the check for valid Bluetooth device address is needed. This
2026 * determines if the device will be added as configured or
2027 * unconfigured controller.
2028 *
2029 * It is not possible to use the Secure Boot Parameters in this
2030 * case since that command is only available in bootloader mode.
2031 */
2032 if (ver->fw_variant == 0x23) {
2033 kfree_skb(skb);
2034 clear_bit(BTUSB_BOOTLOADER, &data->flags);
2035 btusb_check_bdaddr_intel(hdev);
2036 return 0;
2037 }
2038
2039 /* If the device is not in bootloader mode, then the only possible
2040 * choice is to return an error and abort the device initialization.
2041 */
2042 if (ver->fw_variant != 0x06) {
2043 BT_ERR("%s: Unsupported Intel firmware variant (%u)",
2044 hdev->name, ver->fw_variant);
2045 kfree_skb(skb);
2046 return -ENODEV;
2047 }
2048
2049 kfree_skb(skb);
2050
2051 /* Read the secure boot parameters to identify the operating
2052 * details of the bootloader.
2053 */
2054 skb = __hci_cmd_sync(hdev, 0xfc0d, 0, NULL, HCI_INIT_TIMEOUT);
2055 if (IS_ERR(skb)) {
2056 BT_ERR("%s: Reading Intel boot parameters failed (%ld)",
2057 hdev->name, PTR_ERR(skb));
2058 return PTR_ERR(skb);
2059 }
2060
2061 if (skb->len != sizeof(*params)) {
2062 BT_ERR("%s: Intel boot parameters size mismatch", hdev->name);
2063 kfree_skb(skb);
2064 return -EILSEQ;
2065 }
2066
2067 params = (struct intel_boot_params *)skb->data;
2068 if (params->status) {
2069 BT_ERR("%s: Intel boot parameters command failure (%02x)",
2070 hdev->name, params->status);
2071 err = -bt_to_errno(params->status);
2072 kfree_skb(skb);
2073 return err;
2074 }
2075
2076 BT_INFO("%s: Device revision is %u", hdev->name,
2077 le16_to_cpu(params->dev_revid));
2078
2079 BT_INFO("%s: Secure boot is %s", hdev->name,
2080 params->secure_boot ? "enabled" : "disabled");
2081
2082 BT_INFO("%s: Minimum firmware build %u week %u %u", hdev->name,
2083 params->min_fw_build_nn, params->min_fw_build_cw,
2084 2000 + params->min_fw_build_yy);
2085
2086 /* It is required that every single firmware fragment is acknowledged
2087 * with a command complete event. If the boot parameters indicate
2088 * that this bootloader does not send them, then abort the setup.
2089 */
2090 if (params->limited_cce != 0x00) {
2091 BT_ERR("%s: Unsupported Intel firmware loading method (%u)",
2092 hdev->name, params->limited_cce);
2093 kfree_skb(skb);
2094 return -EINVAL;
2095 }
2096
2097 /* If the OTP has no valid Bluetooth device address, then there will
2098 * also be no valid address for the operational firmware.
2099 */
2100 if (!bacmp(&params->otp_bdaddr, BDADDR_ANY)) {
2101 BT_INFO("%s: No device address configured", hdev->name);
2102 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
2103 }
2104
2105 /* With this Intel bootloader only the hardware variant and device
2106 * revision information are used to select the right firmware.
2107 *
2108 * Currently this bootloader support is limited to hardware variant
2109 * iBT 3.0 (LnP/SfP) which is identified by the value 11 (0x0b).
2110 */
2111 snprintf(fwname, sizeof(fwname), "intel/ibt-11-%u.sfi",
2112 le16_to_cpu(params->dev_revid));
2113
2114 err = request_firmware(&fw, fwname, &hdev->dev);
2115 if (err < 0) {
2116 BT_ERR("%s: Failed to load Intel firmware file (%d)",
2117 hdev->name, err);
2118 kfree_skb(skb);
2119 return err;
2120 }
2121
2122 BT_INFO("%s: Found device firmware: %s", hdev->name, fwname);
2123
2124 kfree_skb(skb);
2125
2126 if (fw->size < 644) {
2127 BT_ERR("%s: Invalid size of firmware file (%zu)",
2128 hdev->name, fw->size);
2129 err = -EBADF;
2130 goto done;
2131 }
2132
2133 set_bit(BTUSB_DOWNLOADING, &data->flags);
2134
2135 /* Start the firmware download transaction with the Init fragment
2136 * represented by the 128 bytes of CSS header.
2137 */
2138 err = btusb_intel_secure_send(hdev, 0x00, 128, fw->data);
2139 if (err < 0) {
2140 BT_ERR("%s: Failed to send firmware header (%d)",
2141 hdev->name, err);
2142 goto done;
2143 }
2144
2145 /* Send the 256 bytes of public key information from the firmware
2146 * as the PKey fragment.
2147 */
2148 err = btusb_intel_secure_send(hdev, 0x03, 256, fw->data + 128);
2149 if (err < 0) {
2150 BT_ERR("%s: Failed to send firmware public key (%d)",
2151 hdev->name, err);
2152 goto done;
2153 }
2154
2155 /* Send the 256 bytes of signature information from the firmware
2156 * as the Sign fragment.
2157 */
2158 err = btusb_intel_secure_send(hdev, 0x02, 256, fw->data + 388);
2159 if (err < 0) {
2160 BT_ERR("%s: Failed to send firmware signature (%d)",
2161 hdev->name, err);
2162 goto done;
2163 }
2164
2165 fw_ptr = fw->data + 644;
2166
2167 while (fw_ptr - fw->data < fw->size) {
2168 struct hci_command_hdr *cmd = (void *)fw_ptr;
2169 u8 cmd_len;
2170
2171 cmd_len = sizeof(*cmd) + cmd->plen;
2172
2173 /* Send each command from the firmware data buffer as
2174 * a single Data fragment.
2175 */
2176 err = btusb_intel_secure_send(hdev, 0x01, cmd_len, fw_ptr);
2177 if (err < 0) {
2178 BT_ERR("%s: Failed to send firmware data (%d)",
2179 hdev->name, err);
2180 goto done;
2181 }
2182
2183 fw_ptr += cmd_len;
2184 }
2185
Marcel Holtmannce6bb922015-01-28 01:58:40 -08002186 set_bit(BTUSB_FIRMWARE_LOADED, &data->flags);
2187
Johan Hedberga087a982015-01-30 10:58:54 +02002188 BT_INFO("%s: Waiting for firmware download to complete", hdev->name);
2189
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002190 /* Before switching the device into operational mode and with that
2191 * booting the loaded firmware, wait for the bootloader notification
2192 * that all fragments have been successfully received.
2193 *
Johan Hedberga087a982015-01-30 10:58:54 +02002194 * When the event processing receives the notification, then the
2195 * BTUSB_DOWNLOADING flag will be cleared.
2196 *
2197 * The firmware loading should not take longer than 5 seconds
2198 * and thus just timeout if that happens and fail the setup
2199 * of this device.
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002200 */
Johan Hedberg129a7692015-02-14 09:33:35 +02002201 err = wait_on_bit_timeout(&data->flags, BTUSB_DOWNLOADING,
2202 TASK_INTERRUPTIBLE,
2203 msecs_to_jiffies(5000));
Johan Hedberga087a982015-01-30 10:58:54 +02002204 if (err == 1) {
2205 BT_ERR("%s: Firmware loading interrupted", hdev->name);
2206 err = -EINTR;
2207 goto done;
2208 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002209
Johan Hedberga087a982015-01-30 10:58:54 +02002210 if (err) {
2211 BT_ERR("%s: Firmware loading timeout", hdev->name);
2212 err = -ETIMEDOUT;
2213 goto done;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002214 }
2215
2216 if (test_bit(BTUSB_FIRMWARE_FAILED, &data->flags)) {
2217 BT_ERR("%s: Firmware loading failed", hdev->name);
2218 err = -ENOEXEC;
2219 goto done;
2220 }
2221
2222 rettime = ktime_get();
2223 delta = ktime_sub(rettime, calltime);
2224 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
2225
2226 BT_INFO("%s: Firmware loaded in %llu usecs", hdev->name, duration);
2227
2228done:
2229 release_firmware(fw);
2230
2231 if (err < 0)
2232 return err;
2233
2234 calltime = ktime_get();
2235
2236 set_bit(BTUSB_BOOTING, &data->flags);
2237
2238 skb = __hci_cmd_sync(hdev, 0xfc01, sizeof(reset_param), reset_param,
2239 HCI_INIT_TIMEOUT);
2240 if (IS_ERR(skb))
2241 return PTR_ERR(skb);
2242
2243 kfree_skb(skb);
2244
2245 /* The bootloader will not indicate when the device is ready. This
2246 * is done by the operational firmware sending bootup notification.
Johan Hedbergfad70972015-01-30 10:58:55 +02002247 *
2248 * Booting into operational firmware should not take longer than
2249 * 1 second. However if that happens, then just fail the setup
2250 * since something went wrong.
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002251 */
Johan Hedbergfad70972015-01-30 10:58:55 +02002252 BT_INFO("%s: Waiting for device to boot", hdev->name);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002253
Johan Hedberg129a7692015-02-14 09:33:35 +02002254 err = wait_on_bit_timeout(&data->flags, BTUSB_BOOTING,
2255 TASK_INTERRUPTIBLE,
2256 msecs_to_jiffies(1000));
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002257
Johan Hedbergfad70972015-01-30 10:58:55 +02002258 if (err == 1) {
2259 BT_ERR("%s: Device boot interrupted", hdev->name);
2260 return -EINTR;
2261 }
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002262
Johan Hedbergfad70972015-01-30 10:58:55 +02002263 if (err) {
2264 BT_ERR("%s: Device boot timeout", hdev->name);
2265 return -ETIMEDOUT;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002266 }
2267
2268 rettime = ktime_get();
2269 delta = ktime_sub(rettime, calltime);
2270 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
2271
2272 BT_INFO("%s: Device booted in %llu usecs", hdev->name, duration);
2273
2274 clear_bit(BTUSB_BOOTLOADER, &data->flags);
2275
2276 return 0;
2277}
2278
Marcel Holtmann385a7682015-01-28 11:09:56 -08002279static void btusb_hw_error_intel(struct hci_dev *hdev, u8 code)
2280{
2281 struct sk_buff *skb;
2282 u8 type = 0x00;
2283
2284 BT_ERR("%s: Hardware error 0x%2.2x", hdev->name, code);
2285
2286 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
2287 if (IS_ERR(skb)) {
2288 BT_ERR("%s: Reset after hardware error failed (%ld)",
2289 hdev->name, PTR_ERR(skb));
2290 return;
2291 }
2292 kfree_skb(skb);
2293
2294 skb = __hci_cmd_sync(hdev, 0xfc22, 1, &type, HCI_INIT_TIMEOUT);
2295 if (IS_ERR(skb)) {
2296 BT_ERR("%s: Retrieving Intel exception info failed (%ld)",
2297 hdev->name, PTR_ERR(skb));
2298 return;
2299 }
2300
2301 if (skb->len != 13) {
2302 BT_ERR("%s: Exception info size mismatch", hdev->name);
2303 kfree_skb(skb);
2304 return;
2305 }
2306
2307 if (skb->data[0] != 0x00) {
2308 BT_ERR("%s: Exception info command failure (%02x)",
2309 hdev->name, skb->data[0]);
2310 kfree_skb(skb);
2311 return;
2312 }
2313
2314 BT_ERR("%s: Exception info %s", hdev->name, (char *)(skb->data + 1));
2315
2316 kfree_skb(skb);
2317}
2318
Marcel Holtmanncb8d6592014-07-02 11:25:25 +02002319static int btusb_set_bdaddr_intel(struct hci_dev *hdev, const bdaddr_t *bdaddr)
2320{
2321 struct sk_buff *skb;
2322 long ret;
2323
2324 skb = __hci_cmd_sync(hdev, 0xfc31, 6, bdaddr, HCI_INIT_TIMEOUT);
2325 if (IS_ERR(skb)) {
2326 ret = PTR_ERR(skb);
2327 BT_ERR("%s: changing Intel device address failed (%ld)",
Marcel Holtmann89e75332014-09-16 04:44:50 +02002328 hdev->name, ret);
Marcel Holtmanncb8d6592014-07-02 11:25:25 +02002329 return ret;
2330 }
2331 kfree_skb(skb);
2332
2333 return 0;
2334}
2335
Tedd Ho-Jeong Anbfbd45e2015-02-13 09:20:52 -08002336static int btusb_shutdown_intel(struct hci_dev *hdev)
2337{
2338 struct sk_buff *skb;
2339 long ret;
2340
2341 /* Some platforms have an issue with BT LED when the interface is
2342 * down or BT radio is turned off, which takes 5 seconds to BT LED
2343 * goes off. This command turns off the BT LED immediately.
2344 */
2345 skb = __hci_cmd_sync(hdev, 0xfc3f, 0, NULL, HCI_INIT_TIMEOUT);
2346 if (IS_ERR(skb)) {
2347 ret = PTR_ERR(skb);
2348 BT_ERR("%s: turning off Intel device LED failed (%ld)",
2349 hdev->name, ret);
2350 return ret;
2351 }
2352 kfree_skb(skb);
2353
2354 return 0;
2355}
2356
Amitkumar Karwarae8df492014-07-18 14:47:06 -07002357static int btusb_set_bdaddr_marvell(struct hci_dev *hdev,
2358 const bdaddr_t *bdaddr)
2359{
2360 struct sk_buff *skb;
2361 u8 buf[8];
2362 long ret;
2363
2364 buf[0] = 0xfe;
2365 buf[1] = sizeof(bdaddr_t);
2366 memcpy(buf + 2, bdaddr, sizeof(bdaddr_t));
2367
2368 skb = __hci_cmd_sync(hdev, 0xfc22, sizeof(buf), buf, HCI_INIT_TIMEOUT);
2369 if (IS_ERR(skb)) {
2370 ret = PTR_ERR(skb);
2371 BT_ERR("%s: changing Marvell device address failed (%ld)",
2372 hdev->name, ret);
2373 return ret;
2374 }
2375 kfree_skb(skb);
2376
2377 return 0;
2378}
2379
Marcel Holtmann18835df2015-02-14 23:08:47 -08002380static const struct {
2381 u16 subver;
2382 const char *name;
2383} bcm_subver_table[] = {
2384 { 0x210b, "BCM43142A0" }, /* 001.001.011 */
2385 { 0x2112, "BCM4314A0" }, /* 001.001.018 */
2386 { 0x2118, "BCM20702A0" }, /* 001.001.024 */
2387 { 0x2126, "BCM4335A0" }, /* 001.001.038 */
2388 { 0x220e, "BCM20702A1" }, /* 001.002.014 */
2389 { 0x230f, "BCM4354A2" }, /* 001.003.015 */
2390 { 0x4106, "BCM4335B0" }, /* 002.001.006 */
2391 { 0x410e, "BCM20702B0" }, /* 002.001.014 */
2392 { 0x6109, "BCM4335C0" }, /* 003.001.009 */
2393 { 0x610c, "BCM4354" }, /* 003.001.012 */
2394 { }
2395};
2396
Marcel Holtmannc8abb732014-07-02 12:38:22 +02002397#define BDADDR_BCM20702A0 (&(bdaddr_t) {{0x00, 0xa0, 0x02, 0x70, 0x20, 0x00}})
2398
Petri Gynther10d4c672014-05-08 15:50:01 -07002399static int btusb_setup_bcm_patchram(struct hci_dev *hdev)
2400{
2401 struct btusb_data *data = hci_get_drvdata(hdev);
2402 struct usb_device *udev = data->udev;
2403 char fw_name[64];
2404 const struct firmware *fw;
2405 const u8 *fw_ptr;
2406 size_t fw_size;
2407 const struct hci_command_hdr *cmd;
2408 const u8 *cmd_param;
Marcel Holtmann18835df2015-02-14 23:08:47 -08002409 u16 opcode, subver, rev;
2410 const char *hw_name = NULL;
Petri Gynther10d4c672014-05-08 15:50:01 -07002411 struct sk_buff *skb;
2412 struct hci_rp_read_local_version *ver;
Marcel Holtmannc8abb732014-07-02 12:38:22 +02002413 struct hci_rp_read_bd_addr *bda;
Petri Gynther10d4c672014-05-08 15:50:01 -07002414 long ret;
Marcel Holtmann18835df2015-02-14 23:08:47 -08002415 int i;
Petri Gynther10d4c672014-05-08 15:50:01 -07002416
2417 /* Reset */
2418 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
2419 if (IS_ERR(skb)) {
2420 ret = PTR_ERR(skb);
2421 BT_ERR("%s: HCI_OP_RESET failed (%ld)", hdev->name, ret);
Marcel Holtmann18835df2015-02-14 23:08:47 -08002422 return ret;
Petri Gynther10d4c672014-05-08 15:50:01 -07002423 }
2424 kfree_skb(skb);
2425
2426 /* Read Local Version Info */
2427 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
2428 HCI_INIT_TIMEOUT);
2429 if (IS_ERR(skb)) {
2430 ret = PTR_ERR(skb);
2431 BT_ERR("%s: HCI_OP_READ_LOCAL_VERSION failed (%ld)",
Marcel Holtmann89e75332014-09-16 04:44:50 +02002432 hdev->name, ret);
Marcel Holtmann18835df2015-02-14 23:08:47 -08002433 return ret;
Petri Gynther10d4c672014-05-08 15:50:01 -07002434 }
2435
2436 if (skb->len != sizeof(*ver)) {
2437 BT_ERR("%s: HCI_OP_READ_LOCAL_VERSION event length mismatch",
Marcel Holtmann89e75332014-09-16 04:44:50 +02002438 hdev->name);
Petri Gynther10d4c672014-05-08 15:50:01 -07002439 kfree_skb(skb);
Marcel Holtmann18835df2015-02-14 23:08:47 -08002440 return -EIO;
Petri Gynther10d4c672014-05-08 15:50:01 -07002441 }
2442
Marcel Holtmann89e75332014-09-16 04:44:50 +02002443 ver = (struct hci_rp_read_local_version *)skb->data;
Marcel Holtmann18835df2015-02-14 23:08:47 -08002444 rev = le16_to_cpu(ver->hci_rev);
2445 subver = le16_to_cpu(ver->lmp_subver);
Petri Gynther10d4c672014-05-08 15:50:01 -07002446 kfree_skb(skb);
2447
Marcel Holtmann18835df2015-02-14 23:08:47 -08002448 for (i = 0; bcm_subver_table[i].name; i++) {
2449 if (subver == bcm_subver_table[i].subver) {
2450 hw_name = bcm_subver_table[i].name;
2451 break;
2452 }
2453 }
2454
2455 BT_INFO("%s: %s (%3.3u.%3.3u.%3.3u) build %4.4u", hdev->name,
2456 hw_name ? : "BCM", (subver & 0x7000) >> 13,
2457 (subver & 0x1f00) >> 8, (subver & 0x00ff), rev & 0x0fff);
2458
2459 snprintf(fw_name, sizeof(fw_name), "brcm/%s-%4.4x-%4.4x.hcd",
2460 hw_name ? : "BCM",
2461 le16_to_cpu(udev->descriptor.idVendor),
2462 le16_to_cpu(udev->descriptor.idProduct));
2463
2464 ret = request_firmware(&fw, fw_name, &hdev->dev);
2465 if (ret < 0) {
2466 BT_INFO("%s: BCM: patch %s not found", hdev->name, fw_name);
2467 return 0;
2468 }
2469
Petri Gynther10d4c672014-05-08 15:50:01 -07002470 /* Start Download */
2471 skb = __hci_cmd_sync(hdev, 0xfc2e, 0, NULL, HCI_INIT_TIMEOUT);
2472 if (IS_ERR(skb)) {
2473 ret = PTR_ERR(skb);
2474 BT_ERR("%s: BCM: Download Minidrv command failed (%ld)",
Marcel Holtmann89e75332014-09-16 04:44:50 +02002475 hdev->name, ret);
Petri Gynther10d4c672014-05-08 15:50:01 -07002476 goto reset_fw;
2477 }
2478 kfree_skb(skb);
2479
2480 /* 50 msec delay after Download Minidrv completes */
2481 msleep(50);
2482
2483 fw_ptr = fw->data;
2484 fw_size = fw->size;
2485
2486 while (fw_size >= sizeof(*cmd)) {
Marcel Holtmann89e75332014-09-16 04:44:50 +02002487 cmd = (struct hci_command_hdr *)fw_ptr;
Petri Gynther10d4c672014-05-08 15:50:01 -07002488 fw_ptr += sizeof(*cmd);
2489 fw_size -= sizeof(*cmd);
2490
2491 if (fw_size < cmd->plen) {
2492 BT_ERR("%s: BCM: patch %s is corrupted",
Marcel Holtmann89e75332014-09-16 04:44:50 +02002493 hdev->name, fw_name);
Petri Gynther10d4c672014-05-08 15:50:01 -07002494 ret = -EINVAL;
2495 goto reset_fw;
2496 }
2497
2498 cmd_param = fw_ptr;
2499 fw_ptr += cmd->plen;
2500 fw_size -= cmd->plen;
2501
2502 opcode = le16_to_cpu(cmd->opcode);
2503
2504 skb = __hci_cmd_sync(hdev, opcode, cmd->plen, cmd_param,
2505 HCI_INIT_TIMEOUT);
2506 if (IS_ERR(skb)) {
2507 ret = PTR_ERR(skb);
2508 BT_ERR("%s: BCM: patch command %04x failed (%ld)",
Marcel Holtmann89e75332014-09-16 04:44:50 +02002509 hdev->name, opcode, ret);
Petri Gynther10d4c672014-05-08 15:50:01 -07002510 goto reset_fw;
2511 }
2512 kfree_skb(skb);
2513 }
2514
2515 /* 250 msec delay after Launch Ram completes */
2516 msleep(250);
2517
2518reset_fw:
2519 /* Reset */
2520 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
2521 if (IS_ERR(skb)) {
2522 ret = PTR_ERR(skb);
2523 BT_ERR("%s: HCI_OP_RESET failed (%ld)", hdev->name, ret);
2524 goto done;
2525 }
2526 kfree_skb(skb);
2527
2528 /* Read Local Version Info */
2529 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
2530 HCI_INIT_TIMEOUT);
2531 if (IS_ERR(skb)) {
2532 ret = PTR_ERR(skb);
2533 BT_ERR("%s: HCI_OP_READ_LOCAL_VERSION failed (%ld)",
Marcel Holtmann89e75332014-09-16 04:44:50 +02002534 hdev->name, ret);
Petri Gynther10d4c672014-05-08 15:50:01 -07002535 goto done;
2536 }
2537
2538 if (skb->len != sizeof(*ver)) {
2539 BT_ERR("%s: HCI_OP_READ_LOCAL_VERSION event length mismatch",
Marcel Holtmann89e75332014-09-16 04:44:50 +02002540 hdev->name);
Petri Gynther10d4c672014-05-08 15:50:01 -07002541 kfree_skb(skb);
2542 ret = -EIO;
2543 goto done;
2544 }
2545
Marcel Holtmann89e75332014-09-16 04:44:50 +02002546 ver = (struct hci_rp_read_local_version *)skb->data;
Marcel Holtmann18835df2015-02-14 23:08:47 -08002547 rev = le16_to_cpu(ver->hci_rev);
2548 subver = le16_to_cpu(ver->lmp_subver);
Petri Gynther10d4c672014-05-08 15:50:01 -07002549 kfree_skb(skb);
2550
Marcel Holtmann18835df2015-02-14 23:08:47 -08002551 BT_INFO("%s: %s (%3.3u.%3.3u.%3.3u) build %4.4u", hdev->name,
2552 hw_name ? : "BCM", (subver & 0x7000) >> 13,
2553 (subver & 0x1f00) >> 8, (subver & 0x00ff), rev & 0x0fff);
2554
Marcel Holtmannc8abb732014-07-02 12:38:22 +02002555 /* Read BD Address */
2556 skb = __hci_cmd_sync(hdev, HCI_OP_READ_BD_ADDR, 0, NULL,
2557 HCI_INIT_TIMEOUT);
2558 if (IS_ERR(skb)) {
2559 ret = PTR_ERR(skb);
2560 BT_ERR("%s: HCI_OP_READ_BD_ADDR failed (%ld)",
Marcel Holtmann89e75332014-09-16 04:44:50 +02002561 hdev->name, ret);
Marcel Holtmannc8abb732014-07-02 12:38:22 +02002562 goto done;
2563 }
2564
2565 if (skb->len != sizeof(*bda)) {
2566 BT_ERR("%s: HCI_OP_READ_BD_ADDR event length mismatch",
Marcel Holtmann89e75332014-09-16 04:44:50 +02002567 hdev->name);
Marcel Holtmannc8abb732014-07-02 12:38:22 +02002568 kfree_skb(skb);
2569 ret = -EIO;
2570 goto done;
2571 }
2572
Marcel Holtmann89e75332014-09-16 04:44:50 +02002573 bda = (struct hci_rp_read_bd_addr *)skb->data;
Marcel Holtmannc8abb732014-07-02 12:38:22 +02002574 if (bda->status) {
2575 BT_ERR("%s: HCI_OP_READ_BD_ADDR error status (%02x)",
2576 hdev->name, bda->status);
2577 kfree_skb(skb);
2578 ret = -bt_to_errno(bda->status);
2579 goto done;
2580 }
2581
2582 /* The address 00:20:70:02:A0:00 indicates a BCM20702A0 controller
2583 * with no configured address.
2584 */
Marcel Holtmann849e5082014-07-04 16:54:39 +02002585 if (!bacmp(&bda->bdaddr, BDADDR_BCM20702A0)) {
Marcel Holtmannc8abb732014-07-02 12:38:22 +02002586 BT_INFO("%s: BCM: using default device address (%pMR)",
2587 hdev->name, &bda->bdaddr);
Marcel Holtmann849e5082014-07-04 16:54:39 +02002588 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
2589 }
Marcel Holtmannc8abb732014-07-02 12:38:22 +02002590
2591 kfree_skb(skb);
2592
Petri Gynther10d4c672014-05-08 15:50:01 -07002593done:
2594 release_firmware(fw);
2595
2596 return ret;
2597}
2598
Marcel Holtmannabbaf502014-07-02 00:53:48 +02002599static int btusb_set_bdaddr_bcm(struct hci_dev *hdev, const bdaddr_t *bdaddr)
2600{
2601 struct sk_buff *skb;
2602 long ret;
2603
2604 skb = __hci_cmd_sync(hdev, 0xfc01, 6, bdaddr, HCI_INIT_TIMEOUT);
2605 if (IS_ERR(skb)) {
2606 ret = PTR_ERR(skb);
2607 BT_ERR("%s: BCM: Change address command failed (%ld)",
Marcel Holtmann89e75332014-09-16 04:44:50 +02002608 hdev->name, ret);
Marcel Holtmannabbaf502014-07-02 00:53:48 +02002609 return ret;
2610 }
2611 kfree_skb(skb);
2612
2613 return 0;
2614}
2615
Toshi Kikuchi58592232014-12-12 10:58:05 -08002616static int btusb_set_bdaddr_ath3012(struct hci_dev *hdev,
2617 const bdaddr_t *bdaddr)
2618{
2619 struct sk_buff *skb;
2620 u8 buf[10];
2621 long ret;
2622
2623 buf[0] = 0x01;
2624 buf[1] = 0x01;
2625 buf[2] = 0x00;
2626 buf[3] = sizeof(bdaddr_t);
2627 memcpy(buf + 4, bdaddr, sizeof(bdaddr_t));
2628
2629 skb = __hci_cmd_sync(hdev, 0xfc0b, sizeof(buf), buf, HCI_INIT_TIMEOUT);
2630 if (IS_ERR(skb)) {
2631 ret = PTR_ERR(skb);
2632 BT_ERR("%s: Change address command failed (%ld)",
2633 hdev->name, ret);
2634 return ret;
2635 }
2636 kfree_skb(skb);
2637
2638 return 0;
2639}
2640
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002641static int btusb_probe(struct usb_interface *intf,
Marcel Holtmann89e75332014-09-16 04:44:50 +02002642 const struct usb_device_id *id)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002643{
2644 struct usb_endpoint_descriptor *ep_desc;
2645 struct btusb_data *data;
2646 struct hci_dev *hdev;
2647 int i, err;
2648
2649 BT_DBG("intf %p id %p", intf, id);
2650
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002651 /* interface numbers are hardcoded in the spec */
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002652 if (intf->cur_altsetting->desc.bInterfaceNumber != 0)
2653 return -ENODEV;
2654
2655 if (!id->driver_info) {
2656 const struct usb_device_id *match;
Marcel Holtmann89e75332014-09-16 04:44:50 +02002657
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002658 match = usb_match_id(intf, blacklist_table);
2659 if (match)
2660 id = match;
2661 }
2662
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002663 if (id->driver_info == BTUSB_IGNORE)
2664 return -ENODEV;
2665
Steven.Li2d25f8b2011-07-01 14:02:36 +08002666 if (id->driver_info & BTUSB_ATH3012) {
2667 struct usb_device *udev = interface_to_usbdev(intf);
2668
2669 /* Old firmware would otherwise let ath3k driver load
2670 * patch and sysconfig files */
2671 if (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x0001)
2672 return -ENODEV;
2673 }
2674
Sachin Kamat98921db2012-07-27 12:38:39 +05302675 data = devm_kzalloc(&intf->dev, sizeof(*data), GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002676 if (!data)
2677 return -ENOMEM;
2678
2679 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
2680 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
2681
2682 if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
2683 data->intr_ep = ep_desc;
2684 continue;
2685 }
2686
2687 if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
2688 data->bulk_tx_ep = ep_desc;
2689 continue;
2690 }
2691
2692 if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
2693 data->bulk_rx_ep = ep_desc;
2694 continue;
2695 }
2696 }
2697
Sachin Kamat98921db2012-07-27 12:38:39 +05302698 if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002699 return -ENODEV;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002700
Marcel Holtmann893ba542015-01-28 20:27:34 -08002701 if (id->driver_info & BTUSB_AMP) {
2702 data->cmdreq_type = USB_TYPE_CLASS | 0x01;
2703 data->cmdreq = 0x2b;
2704 } else {
2705 data->cmdreq_type = USB_TYPE_CLASS;
2706 data->cmdreq = 0x00;
2707 }
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002708
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002709 data->udev = interface_to_usbdev(intf);
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02002710 data->intf = intf;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002711
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002712 INIT_WORK(&data->work, btusb_work);
Oliver Neukum7bee5492009-08-24 23:44:59 +02002713 INIT_WORK(&data->waker, btusb_waker);
Marcel Holtmann803b5832014-09-16 08:00:29 +02002714 init_usb_anchor(&data->deferred);
2715 init_usb_anchor(&data->tx_anchor);
Oliver Neukum7bee5492009-08-24 23:44:59 +02002716 spin_lock_init(&data->txlock);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002717
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002718 init_usb_anchor(&data->intr_anchor);
2719 init_usb_anchor(&data->bulk_anchor);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02002720 init_usb_anchor(&data->isoc_anchor);
Marcel Holtmann803b5832014-09-16 08:00:29 +02002721 spin_lock_init(&data->rxlock);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002722
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002723 if (id->driver_info & BTUSB_INTEL_NEW) {
2724 data->recv_event = btusb_recv_event_intel;
2725 data->recv_bulk = btusb_recv_bulk_intel;
2726 set_bit(BTUSB_BOOTLOADER, &data->flags);
2727 } else {
2728 data->recv_event = hci_recv_frame;
2729 data->recv_bulk = btusb_recv_bulk;
2730 }
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +01002731
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002732 hdev = hci_alloc_dev();
Sachin Kamat98921db2012-07-27 12:38:39 +05302733 if (!hdev)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002734 return -ENOMEM;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002735
Marcel Holtmannc13854c2010-02-08 15:27:07 +01002736 hdev->bus = HCI_USB;
David Herrmann155961e2012-02-09 21:58:32 +01002737 hci_set_drvdata(hdev, data);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002738
Marcel Holtmann893ba542015-01-28 20:27:34 -08002739 if (id->driver_info & BTUSB_AMP)
2740 hdev->dev_type = HCI_AMP;
2741 else
2742 hdev->dev_type = HCI_BREDR;
2743
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002744 data->hdev = hdev;
2745
2746 SET_HCIDEV_DEV(hdev, &intf->dev);
2747
Marcel Holtmann9f8f9622013-04-10 08:11:35 -07002748 hdev->open = btusb_open;
2749 hdev->close = btusb_close;
2750 hdev->flush = btusb_flush;
2751 hdev->send = btusb_send_frame;
2752 hdev->notify = btusb_notify;
2753
2754 if (id->driver_info & BTUSB_BCM92035)
2755 hdev->setup = btusb_setup_bcm92035;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002756
Marcel Holtmannabbaf502014-07-02 00:53:48 +02002757 if (id->driver_info & BTUSB_BCM_PATCHRAM) {
Petri Gynther10d4c672014-05-08 15:50:01 -07002758 hdev->setup = btusb_setup_bcm_patchram;
Marcel Holtmannabbaf502014-07-02 00:53:48 +02002759 hdev->set_bdaddr = btusb_set_bdaddr_bcm;
Marcel Holtmann27c3fbe2014-11-02 20:52:24 +01002760 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
Marcel Holtmannabbaf502014-07-02 00:53:48 +02002761 }
Petri Gynther10d4c672014-05-08 15:50:01 -07002762
Marcel Holtmanncb8d6592014-07-02 11:25:25 +02002763 if (id->driver_info & BTUSB_INTEL) {
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002764 hdev->setup = btusb_setup_intel;
Tedd Ho-Jeong Anbfbd45e2015-02-13 09:20:52 -08002765 hdev->shutdown = btusb_shutdown_intel;
Marcel Holtmanncb8d6592014-07-02 11:25:25 +02002766 hdev->set_bdaddr = btusb_set_bdaddr_intel;
Jakub Pawlowskic33fb9b2015-01-30 18:55:58 -08002767 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
Marcel Holtmanncb8d6592014-07-02 11:25:25 +02002768 }
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002769
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002770 if (id->driver_info & BTUSB_INTEL_NEW) {
2771 hdev->send = btusb_send_frame_intel;
2772 hdev->setup = btusb_setup_intel_new;
Marcel Holtmann385a7682015-01-28 11:09:56 -08002773 hdev->hw_error = btusb_hw_error_intel;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002774 hdev->set_bdaddr = btusb_set_bdaddr_intel;
Marcel Holtmannb970c5b2015-02-01 23:57:18 -08002775 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002776 }
2777
Amitkumar Karwarae8df492014-07-18 14:47:06 -07002778 if (id->driver_info & BTUSB_MARVELL)
2779 hdev->set_bdaddr = btusb_set_bdaddr_marvell;
2780
Marcel Holtmann661cf882015-01-02 23:35:20 -08002781 if (id->driver_info & BTUSB_SWAVE) {
2782 set_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks);
Marcel Holtmannd57dbe72014-12-26 04:42:33 +01002783 set_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS, &hdev->quirks);
Marcel Holtmann661cf882015-01-02 23:35:20 -08002784 }
Marcel Holtmannd57dbe72014-12-26 04:42:33 +01002785
Marcel Holtmann40df7832014-07-06 13:29:58 +02002786 if (id->driver_info & BTUSB_INTEL_BOOT)
2787 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
2788
Jakub Pawlowski79f0c872015-01-29 10:38:34 -08002789 if (id->driver_info & BTUSB_ATH3012) {
Toshi Kikuchi58592232014-12-12 10:58:05 -08002790 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
Jakub Pawlowski79f0c872015-01-29 10:38:34 -08002791 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
2792 }
Toshi Kikuchi58592232014-12-12 10:58:05 -08002793
Marcel Holtmann893ba542015-01-28 20:27:34 -08002794 if (id->driver_info & BTUSB_AMP) {
2795 /* AMP controllers do not support SCO packets */
2796 data->isoc = NULL;
2797 } else {
2798 /* Interface numbers are hardcoded in the specification */
2799 data->isoc = usb_ifnum_to_if(data->udev, 1);
2800 }
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02002801
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002802 if (!reset)
Szymon Janca6c511c2012-05-23 12:35:46 +02002803 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002804
2805 if (force_scofix || id->driver_info & BTUSB_WRONG_SCO_MTU) {
2806 if (!disable_scofix)
2807 set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
2808 }
2809
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02002810 if (id->driver_info & BTUSB_BROKEN_ISOC)
2811 data->isoc = NULL;
2812
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002813 if (id->driver_info & BTUSB_DIGIANSWER) {
2814 data->cmdreq_type = USB_TYPE_VENDOR;
Szymon Janca6c511c2012-05-23 12:35:46 +02002815 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002816 }
2817
2818 if (id->driver_info & BTUSB_CSR) {
2819 struct usb_device *udev = data->udev;
Marcel Holtmann81cac642014-01-03 03:02:36 -08002820 u16 bcdDevice = le16_to_cpu(udev->descriptor.bcdDevice);
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002821
2822 /* Old firmware would otherwise execute USB reset */
Marcel Holtmann81cac642014-01-03 03:02:36 -08002823 if (bcdDevice < 0x117)
Szymon Janca6c511c2012-05-23 12:35:46 +02002824 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmann81cac642014-01-03 03:02:36 -08002825
2826 /* Fake CSR devices with broken commands */
2827 if (bcdDevice <= 0x100)
2828 hdev->setup = btusb_setup_csr;
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002829 }
2830
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002831 if (id->driver_info & BTUSB_SNIFFER) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02002832 struct usb_device *udev = data->udev;
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002833
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002834 /* New sniffer firmware has crippled HCI interface */
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002835 if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
2836 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
2837 }
2838
Marcel Holtmann3a5ef202014-07-06 14:53:54 +02002839 if (id->driver_info & BTUSB_INTEL_BOOT) {
2840 /* A bug in the bootloader causes that interrupt interface is
2841 * only enabled after receiving SetInterface(0, AltSetting=0).
2842 */
2843 err = usb_set_interface(data->udev, 0, 0);
2844 if (err < 0) {
2845 BT_ERR("failed to set interface 0, alt 0 %d", err);
2846 hci_free_dev(hdev);
2847 return err;
2848 }
2849 }
2850
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02002851 if (data->isoc) {
2852 err = usb_driver_claim_interface(&btusb_driver,
Marcel Holtmann89e75332014-09-16 04:44:50 +02002853 data->isoc, data);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02002854 if (err < 0) {
2855 hci_free_dev(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02002856 return err;
2857 }
2858 }
2859
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002860 err = hci_register_dev(hdev);
2861 if (err < 0) {
2862 hci_free_dev(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002863 return err;
2864 }
2865
2866 usb_set_intfdata(intf, data);
2867
2868 return 0;
2869}
2870
2871static void btusb_disconnect(struct usb_interface *intf)
2872{
2873 struct btusb_data *data = usb_get_intfdata(intf);
2874 struct hci_dev *hdev;
2875
2876 BT_DBG("intf %p", intf);
2877
2878 if (!data)
2879 return;
2880
2881 hdev = data->hdev;
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02002882 usb_set_intfdata(data->intf, NULL);
2883
2884 if (data->isoc)
2885 usb_set_intfdata(data->isoc, NULL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002886
2887 hci_unregister_dev(hdev);
2888
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02002889 if (intf == data->isoc)
2890 usb_driver_release_interface(&btusb_driver, data->intf);
2891 else if (data->isoc)
2892 usb_driver_release_interface(&btusb_driver, data->isoc);
2893
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002894 hci_free_dev(hdev);
2895}
2896
Oliver Neukum7bee5492009-08-24 23:44:59 +02002897#ifdef CONFIG_PM
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002898static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
2899{
2900 struct btusb_data *data = usb_get_intfdata(intf);
2901
2902 BT_DBG("intf %p", intf);
2903
2904 if (data->suspend_count++)
2905 return 0;
2906
Oliver Neukum7bee5492009-08-24 23:44:59 +02002907 spin_lock_irq(&data->txlock);
Alan Stern5b1b0b82011-08-19 23:49:48 +02002908 if (!(PMSG_IS_AUTO(message) && data->tx_in_flight)) {
Oliver Neukum7bee5492009-08-24 23:44:59 +02002909 set_bit(BTUSB_SUSPENDING, &data->flags);
2910 spin_unlock_irq(&data->txlock);
2911 } else {
2912 spin_unlock_irq(&data->txlock);
2913 data->suspend_count--;
2914 return -EBUSY;
2915 }
2916
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002917 cancel_work_sync(&data->work);
2918
Oliver Neukum7bee5492009-08-24 23:44:59 +02002919 btusb_stop_traffic(data);
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002920 usb_kill_anchored_urbs(&data->tx_anchor);
2921
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002922 return 0;
2923}
2924
Oliver Neukum7bee5492009-08-24 23:44:59 +02002925static void play_deferred(struct btusb_data *data)
2926{
2927 struct urb *urb;
2928 int err;
2929
2930 while ((urb = usb_get_from_anchor(&data->deferred))) {
2931 err = usb_submit_urb(urb, GFP_ATOMIC);
2932 if (err < 0)
2933 break;
2934
2935 data->tx_in_flight++;
2936 }
2937 usb_scuttle_anchored_urbs(&data->deferred);
2938}
2939
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002940static int btusb_resume(struct usb_interface *intf)
2941{
2942 struct btusb_data *data = usb_get_intfdata(intf);
2943 struct hci_dev *hdev = data->hdev;
Oliver Neukum7bee5492009-08-24 23:44:59 +02002944 int err = 0;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002945
2946 BT_DBG("intf %p", intf);
2947
2948 if (--data->suspend_count)
2949 return 0;
2950
2951 if (!test_bit(HCI_RUNNING, &hdev->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +02002952 goto done;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002953
2954 if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
2955 err = btusb_submit_intr_urb(hdev, GFP_NOIO);
2956 if (err < 0) {
2957 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02002958 goto failed;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002959 }
2960 }
2961
2962 if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) {
Marcel Holtmann43c2e572009-02-04 17:41:38 +01002963 err = btusb_submit_bulk_urb(hdev, GFP_NOIO);
2964 if (err < 0) {
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002965 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02002966 goto failed;
2967 }
2968
2969 btusb_submit_bulk_urb(hdev, GFP_NOIO);
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002970 }
2971
2972 if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
2973 if (btusb_submit_isoc_urb(hdev, GFP_NOIO) < 0)
2974 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
2975 else
2976 btusb_submit_isoc_urb(hdev, GFP_NOIO);
2977 }
2978
Oliver Neukum7bee5492009-08-24 23:44:59 +02002979 spin_lock_irq(&data->txlock);
2980 play_deferred(data);
2981 clear_bit(BTUSB_SUSPENDING, &data->flags);
2982 spin_unlock_irq(&data->txlock);
2983 schedule_work(&data->work);
2984
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002985 return 0;
Oliver Neukum7bee5492009-08-24 23:44:59 +02002986
2987failed:
2988 usb_scuttle_anchored_urbs(&data->deferred);
2989done:
2990 spin_lock_irq(&data->txlock);
2991 clear_bit(BTUSB_SUSPENDING, &data->flags);
2992 spin_unlock_irq(&data->txlock);
2993
2994 return err;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002995}
Oliver Neukum7bee5492009-08-24 23:44:59 +02002996#endif
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002997
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002998static struct usb_driver btusb_driver = {
2999 .name = "btusb",
3000 .probe = btusb_probe,
3001 .disconnect = btusb_disconnect,
Oliver Neukum7bee5492009-08-24 23:44:59 +02003002#ifdef CONFIG_PM
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01003003 .suspend = btusb_suspend,
3004 .resume = btusb_resume,
Oliver Neukum7bee5492009-08-24 23:44:59 +02003005#endif
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003006 .id_table = btusb_table,
Oliver Neukum7bee5492009-08-24 23:44:59 +02003007 .supports_autosuspend = 1,
Sarah Sharpe1f12eb2012-04-23 10:08:51 -07003008 .disable_hub_initiated_lpm = 1,
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003009};
3010
Greg Kroah-Hartman93f15082011-11-18 09:47:34 -08003011module_usb_driver(btusb_driver);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003012
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02003013module_param(disable_scofix, bool, 0644);
3014MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
3015
3016module_param(force_scofix, bool, 0644);
3017MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
3018
3019module_param(reset, bool, 0644);
3020MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
3021
Marcel Holtmann5e23b922007-10-20 14:12:34 +02003022MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
3023MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
3024MODULE_VERSION(VERSION);
3025MODULE_LICENSE("GPL");