blob: b2753d79edcf7f2c34858ff65376733383fd0931 [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 Holtmann5e23b922007-10-20 14:12:34 +020054
Marcel Holtmann54265202013-10-11 07:46:18 -070055static const struct usb_device_id btusb_table[] = {
Marcel Holtmann5e23b922007-10-20 14:12:34 +020056 /* Generic Bluetooth USB device */
57 { USB_DEVICE_INFO(0xe0, 0x01, 0x01) },
58
Henrik Rydberg1fa65352012-08-25 19:28:06 +020059 /* Apple-specific (Broadcom) devices */
60 { USB_VENDOR_AND_INTERFACE_INFO(0x05ac, 0xff, 0x01, 0x01) },
61
Cho, Yu-Chen178c0592013-06-04 21:40:26 +080062 /* MediaTek MT76x0E */
63 { USB_DEVICE(0x0e8d, 0x763f) },
64
Oliver Neukumc510eae2011-09-21 11:41:45 +020065 /* Broadcom SoftSailing reporting vendor specific */
Don Zickus2e8b50632012-03-28 16:41:11 -040066 { USB_DEVICE(0x0a5c, 0x21e1) },
Oliver Neukumc510eae2011-09-21 11:41:45 +020067
Nobuhiro Iwamatsu3cd01972010-08-20 16:24:07 +090068 /* Apple MacBookPro 7,1 */
69 { USB_DEVICE(0x05ac, 0x8213) },
70
Cyril Lacoux0a79f672010-07-14 10:29:27 +040071 /* Apple iMac11,1 */
72 { USB_DEVICE(0x05ac, 0x8215) },
73
Nobuhiro Iwamatsu9c047152010-08-20 16:24:06 +090074 /* Apple MacBookPro6,2 */
75 { USB_DEVICE(0x05ac, 0x8218) },
76
Edgar (gimli) Hucek3e3ede72010-11-04 08:04:33 +010077 /* Apple MacBookAir3,1, MacBookAir3,2 */
78 { USB_DEVICE(0x05ac, 0x821b) },
79
Pieter-Augustijn Van Malleghema63b7232011-09-07 02:28:10 -040080 /* Apple MacBookAir4,1 */
81 { USB_DEVICE(0x05ac, 0x821f) },
82
Marc-Antoine Perennou88d377b2011-03-24 14:51:21 -030083 /* Apple MacBookPro8,2 */
84 { USB_DEVICE(0x05ac, 0x821a) },
85
Jurgen Kramerf78b68262011-09-04 18:01:42 +020086 /* Apple MacMini5,1 */
87 { USB_DEVICE(0x05ac, 0x8281) },
88
Marcel Holtmanncfeb4142008-08-07 22:26:56 +020089 /* AVM BlueFRITZ! USB v2.0 */
Marcel Holtmann4fcef8e2015-01-01 17:34:37 -080090 { USB_DEVICE(0x057c, 0x3800), .driver_info = BTUSB_SWAVE },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +020091
92 /* Bluetooth Ultraport Module from IBM */
93 { USB_DEVICE(0x04bf, 0x030a) },
94
95 /* ALPS Modules with non-standard id */
96 { USB_DEVICE(0x044e, 0x3001) },
97 { USB_DEVICE(0x044e, 0x3002) },
98
99 /* Ericsson with non-standard id */
100 { USB_DEVICE(0x0bdb, 0x1002) },
101
102 /* Canyon CN-BTU1 with HID interfaces */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100103 { USB_DEVICE(0x0c10, 0x0000) },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200104
Wen-chien Jesse Sungd13431c2011-11-08 14:30:22 +0800105 /* Broadcom BCM20702A0 */
Andy Shevchenko0b880062014-02-18 18:26:19 +0200106 { USB_DEVICE(0x0489, 0xe042) },
107 { USB_DEVICE(0x04ca, 0x2003) },
Jeff Cook1ee3ff62012-11-09 16:39:48 -0700108 { USB_DEVICE(0x0b05, 0x17b5) },
Raphael Kubo da Costa38a172b2013-09-02 14:57:51 +0300109 { USB_DEVICE(0x0b05, 0x17cb) },
Wen-chien Jesse Sungd13431c2011-11-08 14:30:22 +0800110 { USB_DEVICE(0x413c, 0x8197) },
Fabio Ka86c02e2014-11-18 00:46:28 -0200111 { USB_DEVICE(0x13d3, 0x3404),
112 .driver_info = BTUSB_BCM_PATCHRAM },
Wen-chien Jesse Sungd13431c2011-11-08 14:30:22 +0800113
Marcel Holtmannd049f4e2015-01-26 20:35:32 -0800114 /* Broadcom BCM20702B0 (Dynex/Insignia) */
115 { USB_DEVICE(0x19ff, 0x0239), .driver_info = BTUSB_BCM_PATCHRAM },
116
Steven Harms98514032012-04-13 14:45:55 -0400117 /* Foxconn - Hon Hai */
Heinrich Siebmanns6029ddc2014-12-03 19:32:22 +0100118 { USB_VENDOR_AND_INTERFACE_INFO(0x0489, 0xff, 0x01, 0x01),
119 .driver_info = BTUSB_BCM_PATCHRAM },
Steven Harms98514032012-04-13 14:45:55 -0400120
Andy Shevchenko0b880062014-02-18 18:26:19 +0200121 /* Broadcom devices with vendor specific id */
Petri Gynther10d4c672014-05-08 15:50:01 -0700122 { USB_VENDOR_AND_INTERFACE_INFO(0x0a5c, 0xff, 0x01, 0x01),
123 .driver_info = BTUSB_BCM_PATCHRAM },
Gustavo Padovan92c385f2012-08-06 15:36:49 -0300124
Marcel Holtmannc2aef6e2014-07-21 14:02:33 +0200125 /* ASUSTek Computer - Broadcom based */
Rick Dunn9a5abdaa2015-01-17 05:29:12 +0100126 { USB_VENDOR_AND_INTERFACE_INFO(0x0b05, 0xff, 0x01, 0x01),
127 .driver_info = BTUSB_BCM_PATCHRAM },
Marcel Holtmannc2aef6e2014-07-21 14:02:33 +0200128
Ken O'Brien5bcecf32013-09-21 19:14:43 +0100129 /* Belkin F8065bf - Broadcom based */
130 { USB_VENDOR_AND_INTERFACE_INFO(0x050d, 0xff, 0x01, 0x01) },
131
Jurgen Kramer9113bfd2014-02-15 12:01:09 +0100132 /* IMC Networks - Broadcom based */
133 { USB_VENDOR_AND_INTERFACE_INFO(0x13d3, 0xff, 0x01, 0x01) },
134
Marcel Holtmann40df7832014-07-06 13:29:58 +0200135 /* Intel Bluetooth USB Bootloader (RAM module) */
Marcel Holtmannd92f2df2014-07-06 14:53:55 +0200136 { USB_DEVICE(0x8087, 0x0a5a),
137 .driver_info = BTUSB_INTEL_BOOT | BTUSB_BROKEN_ISOC },
Marcel Holtmann40df7832014-07-06 13:29:58 +0200138
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200139 { } /* Terminating entry */
140};
141
142MODULE_DEVICE_TABLE(usb, btusb_table);
143
Marcel Holtmann54265202013-10-11 07:46:18 -0700144static const struct usb_device_id blacklist_table[] = {
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200145 /* CSR BlueCore devices */
146 { USB_DEVICE(0x0a12, 0x0001), .driver_info = BTUSB_CSR },
147
148 /* Broadcom BCM2033 without firmware */
149 { USB_DEVICE(0x0a5c, 0x2033), .driver_info = BTUSB_IGNORE },
150
Bala Shanmugambe931122010-11-26 17:35:46 +0530151 /* Atheros 3011 with sflash firmware */
Andy Shevchenko0b880062014-02-18 18:26:19 +0200152 { USB_DEVICE(0x0489, 0xe027), .driver_info = BTUSB_IGNORE },
153 { USB_DEVICE(0x0489, 0xe03d), .driver_info = BTUSB_IGNORE },
154 { USB_DEVICE(0x0930, 0x0215), .driver_info = BTUSB_IGNORE },
Bala Shanmugambe931122010-11-26 17:35:46 +0530155 { USB_DEVICE(0x0cf3, 0x3002), .driver_info = BTUSB_IGNORE },
Marek Vasut6eda5412012-06-08 14:32:50 +0200156 { USB_DEVICE(0x0cf3, 0xe019), .driver_info = BTUSB_IGNORE },
Andy Ross2a7bccc2011-05-09 16:11:16 -0700157 { USB_DEVICE(0x13d3, 0x3304), .driver_info = BTUSB_IGNORE },
Bala Shanmugambe931122010-11-26 17:35:46 +0530158
Cho, Yu-Chen509e7862011-01-26 17:10:59 +0800159 /* Atheros AR9285 Malbec with sflash firmware */
160 { USB_DEVICE(0x03f0, 0x311d), .driver_info = BTUSB_IGNORE },
161
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530162 /* Atheros 3012 with sflash firmware */
Andy Shevchenko0b880062014-02-18 18:26:19 +0200163 { USB_DEVICE(0x0489, 0xe04d), .driver_info = BTUSB_ATH3012 },
164 { USB_DEVICE(0x0489, 0xe04e), .driver_info = BTUSB_ATH3012 },
165 { USB_DEVICE(0x0489, 0xe056), .driver_info = BTUSB_ATH3012 },
166 { USB_DEVICE(0x0489, 0xe057), .driver_info = BTUSB_ATH3012 },
167 { USB_DEVICE(0x0489, 0xe05f), .driver_info = BTUSB_ATH3012 },
Anantha Krishnan4b552bc2014-10-06 16:31:49 +0530168 { USB_DEVICE(0x0489, 0xe078), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200169 { USB_DEVICE(0x04c5, 0x1330), .driver_info = BTUSB_ATH3012 },
170 { USB_DEVICE(0x04ca, 0x3004), .driver_info = BTUSB_ATH3012 },
171 { USB_DEVICE(0x04ca, 0x3005), .driver_info = BTUSB_ATH3012 },
172 { USB_DEVICE(0x04ca, 0x3006), .driver_info = BTUSB_ATH3012 },
Mohammed Habibulla1fb4e092014-04-17 11:37:13 -0700173 { USB_DEVICE(0x04ca, 0x3007), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200174 { USB_DEVICE(0x04ca, 0x3008), .driver_info = BTUSB_ATH3012 },
175 { USB_DEVICE(0x04ca, 0x300b), .driver_info = BTUSB_ATH3012 },
Janne Heikkinen134d3b32014-12-09 07:44:51 +0200176 { USB_DEVICE(0x04ca, 0x3010), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200177 { USB_DEVICE(0x0930, 0x0219), .driver_info = BTUSB_ATH3012 },
178 { USB_DEVICE(0x0930, 0x0220), .driver_info = BTUSB_ATH3012 },
Vincent Zwanenburg89d29752014-08-08 12:33:56 +0100179 { USB_DEVICE(0x0930, 0x0227), .driver_info = BTUSB_ATH3012 },
Andy Shevchenkoa735f9e2014-02-18 18:26:20 +0200180 { USB_DEVICE(0x0b05, 0x17d0), .driver_info = BTUSB_ATH3012 },
Ming Leid66629c2013-03-15 11:00:39 +0800181 { USB_DEVICE(0x0cf3, 0x0036), .driver_info = BTUSB_ATH3012 },
Steven.Li2d25f8b2011-07-01 14:02:36 +0800182 { USB_DEVICE(0x0cf3, 0x3004), .driver_info = BTUSB_ATH3012 },
Sunguk Lee94a32d102013-03-12 04:41:58 +0900183 { USB_DEVICE(0x0cf3, 0x3008), .driver_info = BTUSB_ATH3012 },
Cho, Yu-Chen07c0ea82012-03-14 22:01:21 +0200184 { USB_DEVICE(0x0cf3, 0x311d), .driver_info = BTUSB_ATH3012 },
Oliver Neukumb1312372014-01-16 15:37:11 +0100185 { USB_DEVICE(0x0cf3, 0x311e), .driver_info = BTUSB_ATH3012 },
Oliver Neukum1e56f1e2014-01-16 16:02:58 +0100186 { USB_DEVICE(0x0cf3, 0x311f), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200187 { USB_DEVICE(0x0cf3, 0x3121), .driver_info = BTUSB_ATH3012 },
Ming Leiebaf5792013-03-18 23:45:11 +0800188 { USB_DEVICE(0x0cf3, 0x817a), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200189 { USB_DEVICE(0x0cf3, 0xe003), .driver_info = BTUSB_ATH3012 },
AceLan Kaoac713112012-04-19 14:53:45 +0800190 { USB_DEVICE(0x0cf3, 0xe004), .driver_info = BTUSB_ATH3012 },
Peng Chen0a3658c2013-08-30 17:41:40 +0800191 { USB_DEVICE(0x0cf3, 0xe005), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200192 { USB_DEVICE(0x13d3, 0x3362), .driver_info = BTUSB_ATH3012 },
193 { USB_DEVICE(0x13d3, 0x3375), .driver_info = BTUSB_ATH3012 },
AceLan Kaoeed307e2012-12-11 11:41:20 +0800194 { USB_DEVICE(0x13d3, 0x3393), .driver_info = BTUSB_ATH3012 },
Sujith Manoharan5b77a1f2013-07-15 09:29:03 +0530195 { USB_DEVICE(0x13d3, 0x3402), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin3bb30a7c2014-11-25 20:19:52 +0300196 { USB_DEVICE(0x13d3, 0x3408), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin033efa92015-01-18 00:16:51 +0300197 { USB_DEVICE(0x13d3, 0x3423), .driver_info = BTUSB_ATH3012 },
Anantha Krishnanfa2f1392014-07-08 19:25:08 +0530198 { USB_DEVICE(0x13d3, 0x3432), .driver_info = BTUSB_ATH3012 },
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530199
Cho, Yu-Chene9036e32011-02-15 10:20:07 +0800200 /* Atheros AR5BBU12 with sflash firmware */
201 { USB_DEVICE(0x0489, 0xe02c), .driver_info = BTUSB_IGNORE },
202
Michael Gruetzner85d59722012-05-02 22:33:40 +0200203 /* Atheros AR5BBU12 with sflash firmware */
Yevgeniy Melnichukbc21fde2012-08-07 19:48:10 +0530204 { USB_DEVICE(0x0489, 0xe036), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200205 { USB_DEVICE(0x0489, 0xe03c), .driver_info = BTUSB_ATH3012 },
Michael Gruetzner85d59722012-05-02 22:33:40 +0200206
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200207 /* Broadcom BCM2035 */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100208 { USB_DEVICE(0x0a5c, 0x2009), .driver_info = BTUSB_BCM92035 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200209 { USB_DEVICE(0x0a5c, 0x200a), .driver_info = BTUSB_WRONG_SCO_MTU },
210 { USB_DEVICE(0x0a5c, 0x2035), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200211
212 /* Broadcom BCM2045 */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100213 { USB_DEVICE(0x0a5c, 0x2039), .driver_info = BTUSB_WRONG_SCO_MTU },
214 { USB_DEVICE(0x0a5c, 0x2101), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmannbdbef3d2008-09-23 00:16:35 +0200215
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200216 /* IBM/Lenovo ThinkPad with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100217 { USB_DEVICE(0x0a5c, 0x201e), .driver_info = BTUSB_WRONG_SCO_MTU },
218 { USB_DEVICE(0x0a5c, 0x2110), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200219
220 /* HP laptop with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100221 { USB_DEVICE(0x03f0, 0x171d), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200222
223 /* Dell laptop with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100224 { USB_DEVICE(0x413c, 0x8126), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200225
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100226 /* Dell Wireless 370 and 410 devices */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100227 { USB_DEVICE(0x413c, 0x8152), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100228 { USB_DEVICE(0x413c, 0x8156), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200229
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100230 /* Belkin F8T012 and F8T013 devices */
231 { USB_DEVICE(0x050d, 0x0012), .driver_info = BTUSB_WRONG_SCO_MTU },
232 { USB_DEVICE(0x050d, 0x0013), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200233
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100234 /* Asus WL-BTD202 device */
235 { USB_DEVICE(0x0b05, 0x1715), .driver_info = BTUSB_WRONG_SCO_MTU },
236
237 /* Kensington Bluetooth USB adapter */
238 { USB_DEVICE(0x047d, 0x105e), .driver_info = BTUSB_WRONG_SCO_MTU },
239
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200240 /* RTX Telecom based adapters with buggy SCO support */
241 { USB_DEVICE(0x0400, 0x0807), .driver_info = BTUSB_BROKEN_ISOC },
242 { USB_DEVICE(0x0400, 0x080a), .driver_info = BTUSB_BROKEN_ISOC },
243
244 /* CONWISE Technology based adapters with buggy SCO support */
245 { USB_DEVICE(0x0e5e, 0x6622), .driver_info = BTUSB_BROKEN_ISOC },
246
Marcel Holtmann4fcef8e2015-01-01 17:34:37 -0800247 /* Roper Class 1 Bluetooth Dongle (Silicon Wave based) */
248 { USB_DEVICE(0x1300, 0x0001), .driver_info = BTUSB_SWAVE },
249
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200250 /* Digianswer devices */
251 { USB_DEVICE(0x08fd, 0x0001), .driver_info = BTUSB_DIGIANSWER },
252 { USB_DEVICE(0x08fd, 0x0002), .driver_info = BTUSB_IGNORE },
253
254 /* CSR BlueCore Bluetooth Sniffer */
Marcel Holtmann4f64fa82014-07-07 00:12:04 +0200255 { USB_DEVICE(0x0a12, 0x0002),
256 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200257
258 /* Frontline ComProbe Bluetooth Sniffer */
Marcel Holtmann4f64fa82014-07-07 00:12:04 +0200259 { USB_DEVICE(0x16d3, 0x0002),
260 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200261
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -0700262 /* Intel Bluetooth device */
263 { USB_DEVICE(0x8087, 0x07dc), .driver_info = BTUSB_INTEL },
Tedd Ho-Jeong Anef4e5e42013-11-12 13:10:58 -0800264 { USB_DEVICE(0x8087, 0x0a2a), .driver_info = BTUSB_INTEL },
Marcel Holtmanncda0dd72015-01-26 21:33:48 -0800265 { USB_DEVICE(0x8087, 0x0a2b), .driver_info = BTUSB_INTEL_NEW },
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -0700266
Amitkumar Karwarae8df492014-07-18 14:47:06 -0700267 /* Marvell device */
268 { USB_DEVICE(0x1286, 0x2044), .driver_info = BTUSB_MARVELL },
269 { USB_DEVICE(0x1286, 0x2046), .driver_info = BTUSB_MARVELL },
270
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200271 { } /* Terminating entry */
272};
273
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200274#define BTUSB_MAX_ISOC_FRAMES 10
275
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200276#define BTUSB_INTR_RUNNING 0
277#define BTUSB_BULK_RUNNING 1
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200278#define BTUSB_ISOC_RUNNING 2
Oliver Neukum7bee5492009-08-24 23:44:59 +0200279#define BTUSB_SUSPENDING 3
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -0300280#define BTUSB_DID_ISO_RESUME 4
Marcel Holtmanncda0dd72015-01-26 21:33:48 -0800281#define BTUSB_BOOTLOADER 5
282#define BTUSB_DOWNLOADING 6
Marcel Holtmannce6bb922015-01-28 01:58:40 -0800283#define BTUSB_FIRMWARE_LOADED 7
Marcel Holtmanncda0dd72015-01-26 21:33:48 -0800284#define BTUSB_FIRMWARE_FAILED 8
Marcel Holtmannce6bb922015-01-28 01:58:40 -0800285#define BTUSB_BOOTING 9
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200286
287struct btusb_data {
288 struct hci_dev *hdev;
289 struct usb_device *udev;
Marcel Holtmann5fbcd262008-09-23 00:16:36 +0200290 struct usb_interface *intf;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200291 struct usb_interface *isoc;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200292
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200293 unsigned long flags;
294
295 struct work_struct work;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200296 struct work_struct waker;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200297
Marcel Holtmann803b5832014-09-16 08:00:29 +0200298 struct usb_anchor deferred;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200299 struct usb_anchor tx_anchor;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200300 int tx_in_flight;
301 spinlock_t txlock;
302
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200303 struct usb_anchor intr_anchor;
304 struct usb_anchor bulk_anchor;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200305 struct usb_anchor isoc_anchor;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200306 spinlock_t rxlock;
307
308 struct sk_buff *evt_skb;
309 struct sk_buff *acl_skb;
310 struct sk_buff *sco_skb;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200311
312 struct usb_endpoint_descriptor *intr_ep;
313 struct usb_endpoint_descriptor *bulk_tx_ep;
314 struct usb_endpoint_descriptor *bulk_rx_ep;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200315 struct usb_endpoint_descriptor *isoc_tx_ep;
316 struct usb_endpoint_descriptor *isoc_rx_ep;
317
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100318 __u8 cmdreq_type;
319
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100320 unsigned int sco_num;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200321 int isoc_altsetting;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +0100322 int suspend_count;
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +0100323
Marcel Holtmann97307f52015-01-12 13:51:10 -0800324 int (*recv_event)(struct hci_dev *hdev, struct sk_buff *skb);
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +0100325 int (*recv_bulk)(struct btusb_data *data, void *buffer, int count);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200326};
327
Marcel Holtmann803b5832014-09-16 08:00:29 +0200328static inline void btusb_free_frags(struct btusb_data *data)
329{
330 unsigned long flags;
331
332 spin_lock_irqsave(&data->rxlock, flags);
333
334 kfree_skb(data->evt_skb);
335 data->evt_skb = NULL;
336
337 kfree_skb(data->acl_skb);
338 data->acl_skb = NULL;
339
340 kfree_skb(data->sco_skb);
341 data->sco_skb = NULL;
342
343 spin_unlock_irqrestore(&data->rxlock, flags);
344}
345
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200346static int btusb_recv_intr(struct btusb_data *data, void *buffer, int count)
347{
Marcel Holtmann803b5832014-09-16 08:00:29 +0200348 struct sk_buff *skb;
349 int err = 0;
350
351 spin_lock(&data->rxlock);
352 skb = data->evt_skb;
353
354 while (count) {
355 int len;
356
357 if (!skb) {
358 skb = bt_skb_alloc(HCI_MAX_EVENT_SIZE, GFP_ATOMIC);
359 if (!skb) {
360 err = -ENOMEM;
361 break;
362 }
363
364 bt_cb(skb)->pkt_type = HCI_EVENT_PKT;
365 bt_cb(skb)->expect = HCI_EVENT_HDR_SIZE;
366 }
367
368 len = min_t(uint, bt_cb(skb)->expect, count);
369 memcpy(skb_put(skb, len), buffer, len);
370
371 count -= len;
372 buffer += len;
373 bt_cb(skb)->expect -= len;
374
375 if (skb->len == HCI_EVENT_HDR_SIZE) {
376 /* Complete event header */
377 bt_cb(skb)->expect = hci_event_hdr(skb)->plen;
378
379 if (skb_tailroom(skb) < bt_cb(skb)->expect) {
380 kfree_skb(skb);
381 skb = NULL;
382
383 err = -EILSEQ;
384 break;
385 }
386 }
387
388 if (bt_cb(skb)->expect == 0) {
389 /* Complete frame */
Marcel Holtmann97307f52015-01-12 13:51:10 -0800390 data->recv_event(data->hdev, skb);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200391 skb = NULL;
392 }
393 }
394
395 data->evt_skb = skb;
396 spin_unlock(&data->rxlock);
397
398 return err;
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200399}
400
401static int btusb_recv_bulk(struct btusb_data *data, void *buffer, int count)
402{
Marcel Holtmann803b5832014-09-16 08:00:29 +0200403 struct sk_buff *skb;
404 int err = 0;
405
406 spin_lock(&data->rxlock);
407 skb = data->acl_skb;
408
409 while (count) {
410 int len;
411
412 if (!skb) {
413 skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
414 if (!skb) {
415 err = -ENOMEM;
416 break;
417 }
418
419 bt_cb(skb)->pkt_type = HCI_ACLDATA_PKT;
420 bt_cb(skb)->expect = HCI_ACL_HDR_SIZE;
421 }
422
423 len = min_t(uint, bt_cb(skb)->expect, count);
424 memcpy(skb_put(skb, len), buffer, len);
425
426 count -= len;
427 buffer += len;
428 bt_cb(skb)->expect -= len;
429
430 if (skb->len == HCI_ACL_HDR_SIZE) {
431 __le16 dlen = hci_acl_hdr(skb)->dlen;
432
433 /* Complete ACL header */
434 bt_cb(skb)->expect = __le16_to_cpu(dlen);
435
436 if (skb_tailroom(skb) < bt_cb(skb)->expect) {
437 kfree_skb(skb);
438 skb = NULL;
439
440 err = -EILSEQ;
441 break;
442 }
443 }
444
445 if (bt_cb(skb)->expect == 0) {
446 /* Complete frame */
447 hci_recv_frame(data->hdev, skb);
448 skb = NULL;
449 }
450 }
451
452 data->acl_skb = skb;
453 spin_unlock(&data->rxlock);
454
455 return err;
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200456}
457
458static int btusb_recv_isoc(struct btusb_data *data, void *buffer, int count)
459{
Marcel Holtmann803b5832014-09-16 08:00:29 +0200460 struct sk_buff *skb;
461 int err = 0;
462
463 spin_lock(&data->rxlock);
464 skb = data->sco_skb;
465
466 while (count) {
467 int len;
468
469 if (!skb) {
470 skb = bt_skb_alloc(HCI_MAX_SCO_SIZE, GFP_ATOMIC);
471 if (!skb) {
472 err = -ENOMEM;
473 break;
474 }
475
476 bt_cb(skb)->pkt_type = HCI_SCODATA_PKT;
477 bt_cb(skb)->expect = HCI_SCO_HDR_SIZE;
478 }
479
480 len = min_t(uint, bt_cb(skb)->expect, count);
481 memcpy(skb_put(skb, len), buffer, len);
482
483 count -= len;
484 buffer += len;
485 bt_cb(skb)->expect -= len;
486
487 if (skb->len == HCI_SCO_HDR_SIZE) {
488 /* Complete SCO header */
489 bt_cb(skb)->expect = hci_sco_hdr(skb)->dlen;
490
491 if (skb_tailroom(skb) < bt_cb(skb)->expect) {
492 kfree_skb(skb);
493 skb = NULL;
494
495 err = -EILSEQ;
496 break;
497 }
498 }
499
500 if (bt_cb(skb)->expect == 0) {
501 /* Complete frame */
502 hci_recv_frame(data->hdev, skb);
503 skb = NULL;
504 }
505 }
506
507 data->sco_skb = skb;
508 spin_unlock(&data->rxlock);
509
510 return err;
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200511}
512
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200513static void btusb_intr_complete(struct urb *urb)
514{
515 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +0100516 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200517 int err;
518
Marcel Holtmann89e75332014-09-16 04:44:50 +0200519 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
520 urb->actual_length);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200521
522 if (!test_bit(HCI_RUNNING, &hdev->flags))
523 return;
524
525 if (urb->status == 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200526 hdev->stat.byte_rx += urb->actual_length;
527
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200528 if (btusb_recv_intr(data, urb->transfer_buffer,
529 urb->actual_length) < 0) {
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200530 BT_ERR("%s corrupted event packet", hdev->name);
531 hdev->stat.err_rx++;
532 }
Champion Chen85560c42014-09-06 14:06:08 -0500533 } else if (urb->status == -ENOENT) {
534 /* Avoid suspend failed when usb_kill_urb */
535 return;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200536 }
537
538 if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
539 return;
540
Oliver Neukum7bee5492009-08-24 23:44:59 +0200541 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200542 usb_anchor_urb(urb, &data->intr_anchor);
543
544 err = usb_submit_urb(urb, GFP_ATOMIC);
545 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200546 /* -EPERM: urb is being killed;
547 * -ENODEV: device got disconnected */
548 if (err != -EPERM && err != -ENODEV)
Stefan Seyfried61faddf2010-11-30 21:49:08 +0100549 BT_ERR("%s urb %p failed to resubmit (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200550 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200551 usb_unanchor_urb(urb);
552 }
553}
554
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100555static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200556{
David Herrmann155961e2012-02-09 21:58:32 +0100557 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200558 struct urb *urb;
559 unsigned char *buf;
560 unsigned int pipe;
561 int err, size;
562
563 BT_DBG("%s", hdev->name);
564
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200565 if (!data->intr_ep)
566 return -ENODEV;
567
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100568 urb = usb_alloc_urb(0, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200569 if (!urb)
570 return -ENOMEM;
571
572 size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
573
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100574 buf = kmalloc(size, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200575 if (!buf) {
576 usb_free_urb(urb);
577 return -ENOMEM;
578 }
579
580 pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
581
582 usb_fill_int_urb(urb, data->udev, pipe, buf, size,
Marcel Holtmann89e75332014-09-16 04:44:50 +0200583 btusb_intr_complete, hdev, data->intr_ep->bInterval);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200584
585 urb->transfer_flags |= URB_FREE_BUFFER;
586
587 usb_anchor_urb(urb, &data->intr_anchor);
588
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100589 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200590 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +0200591 if (err != -EPERM && err != -ENODEV)
592 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200593 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200594 usb_unanchor_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200595 }
596
597 usb_free_urb(urb);
598
599 return err;
600}
601
602static void btusb_bulk_complete(struct urb *urb)
603{
604 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +0100605 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200606 int err;
607
Marcel Holtmann89e75332014-09-16 04:44:50 +0200608 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
609 urb->actual_length);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200610
611 if (!test_bit(HCI_RUNNING, &hdev->flags))
612 return;
613
614 if (urb->status == 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200615 hdev->stat.byte_rx += urb->actual_length;
616
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +0100617 if (data->recv_bulk(data, urb->transfer_buffer,
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200618 urb->actual_length) < 0) {
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200619 BT_ERR("%s corrupted ACL packet", hdev->name);
620 hdev->stat.err_rx++;
621 }
Champion Chen85560c42014-09-06 14:06:08 -0500622 } else if (urb->status == -ENOENT) {
623 /* Avoid suspend failed when usb_kill_urb */
624 return;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200625 }
626
627 if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
628 return;
629
630 usb_anchor_urb(urb, &data->bulk_anchor);
Oliver Neukum652fd782009-12-16 19:23:43 +0100631 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200632
633 err = usb_submit_urb(urb, GFP_ATOMIC);
634 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200635 /* -EPERM: urb is being killed;
636 * -ENODEV: device got disconnected */
637 if (err != -EPERM && err != -ENODEV)
Stefan Seyfried61faddf2010-11-30 21:49:08 +0100638 BT_ERR("%s urb %p failed to resubmit (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200639 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200640 usb_unanchor_urb(urb);
641 }
642}
643
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100644static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200645{
David Herrmann155961e2012-02-09 21:58:32 +0100646 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200647 struct urb *urb;
648 unsigned char *buf;
649 unsigned int pipe;
Vikram Kandukuri290ba202009-07-02 14:31:59 +0530650 int err, size = HCI_MAX_FRAME_SIZE;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200651
652 BT_DBG("%s", hdev->name);
653
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200654 if (!data->bulk_rx_ep)
655 return -ENODEV;
656
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100657 urb = usb_alloc_urb(0, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200658 if (!urb)
659 return -ENOMEM;
660
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100661 buf = kmalloc(size, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200662 if (!buf) {
663 usb_free_urb(urb);
664 return -ENOMEM;
665 }
666
667 pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
668
Marcel Holtmann89e75332014-09-16 04:44:50 +0200669 usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
670 btusb_bulk_complete, hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200671
672 urb->transfer_flags |= URB_FREE_BUFFER;
673
Oliver Neukum7bee5492009-08-24 23:44:59 +0200674 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200675 usb_anchor_urb(urb, &data->bulk_anchor);
676
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100677 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200678 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +0200679 if (err != -EPERM && err != -ENODEV)
680 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200681 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200682 usb_unanchor_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200683 }
684
685 usb_free_urb(urb);
686
687 return err;
688}
689
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200690static void btusb_isoc_complete(struct urb *urb)
691{
692 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +0100693 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200694 int i, err;
695
Marcel Holtmann89e75332014-09-16 04:44:50 +0200696 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
697 urb->actual_length);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200698
699 if (!test_bit(HCI_RUNNING, &hdev->flags))
700 return;
701
702 if (urb->status == 0) {
703 for (i = 0; i < urb->number_of_packets; i++) {
704 unsigned int offset = urb->iso_frame_desc[i].offset;
705 unsigned int length = urb->iso_frame_desc[i].actual_length;
706
707 if (urb->iso_frame_desc[i].status)
708 continue;
709
710 hdev->stat.byte_rx += length;
711
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200712 if (btusb_recv_isoc(data, urb->transfer_buffer + offset,
713 length) < 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200714 BT_ERR("%s corrupted SCO packet", hdev->name);
715 hdev->stat.err_rx++;
716 }
717 }
Champion Chen85560c42014-09-06 14:06:08 -0500718 } else if (urb->status == -ENOENT) {
719 /* Avoid suspend failed when usb_kill_urb */
720 return;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200721 }
722
723 if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags))
724 return;
725
726 usb_anchor_urb(urb, &data->isoc_anchor);
727
728 err = usb_submit_urb(urb, GFP_ATOMIC);
729 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200730 /* -EPERM: urb is being killed;
731 * -ENODEV: device got disconnected */
732 if (err != -EPERM && err != -ENODEV)
Stefan Seyfried61faddf2010-11-30 21:49:08 +0100733 BT_ERR("%s urb %p failed to resubmit (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200734 hdev->name, urb, -err);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200735 usb_unanchor_urb(urb);
736 }
737}
738
Jesper Juhl42b16b32011-01-17 00:09:38 +0100739static inline void __fill_isoc_descriptor(struct urb *urb, int len, int mtu)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200740{
741 int i, offset = 0;
742
743 BT_DBG("len %d mtu %d", len, mtu);
744
745 for (i = 0; i < BTUSB_MAX_ISOC_FRAMES && len >= mtu;
746 i++, offset += mtu, len -= mtu) {
747 urb->iso_frame_desc[i].offset = offset;
748 urb->iso_frame_desc[i].length = mtu;
749 }
750
751 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
752 urb->iso_frame_desc[i].offset = offset;
753 urb->iso_frame_desc[i].length = len;
754 i++;
755 }
756
757 urb->number_of_packets = i;
758}
759
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100760static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200761{
David Herrmann155961e2012-02-09 21:58:32 +0100762 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200763 struct urb *urb;
764 unsigned char *buf;
765 unsigned int pipe;
766 int err, size;
767
768 BT_DBG("%s", hdev->name);
769
770 if (!data->isoc_rx_ep)
771 return -ENODEV;
772
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100773 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200774 if (!urb)
775 return -ENOMEM;
776
777 size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) *
778 BTUSB_MAX_ISOC_FRAMES;
779
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100780 buf = kmalloc(size, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200781 if (!buf) {
782 usb_free_urb(urb);
783 return -ENOMEM;
784 }
785
786 pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress);
787
Bing Zhaofa0fb932011-12-20 18:19:00 -0800788 usb_fill_int_urb(urb, data->udev, pipe, buf, size, btusb_isoc_complete,
Marcel Holtmann89e75332014-09-16 04:44:50 +0200789 hdev, data->isoc_rx_ep->bInterval);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200790
Marcel Holtmann89e75332014-09-16 04:44:50 +0200791 urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200792
793 __fill_isoc_descriptor(urb, size,
Marcel Holtmann89e75332014-09-16 04:44:50 +0200794 le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize));
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200795
796 usb_anchor_urb(urb, &data->isoc_anchor);
797
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100798 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200799 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +0200800 if (err != -EPERM && err != -ENODEV)
801 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200802 hdev->name, urb, -err);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200803 usb_unanchor_urb(urb);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200804 }
805
806 usb_free_urb(urb);
807
808 return err;
809}
810
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200811static void btusb_tx_complete(struct urb *urb)
812{
813 struct sk_buff *skb = urb->context;
Marcel Holtmann89e75332014-09-16 04:44:50 +0200814 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
David Herrmann155961e2012-02-09 21:58:32 +0100815 struct btusb_data *data = hci_get_drvdata(hdev);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200816
Marcel Holtmann89e75332014-09-16 04:44:50 +0200817 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
818 urb->actual_length);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200819
820 if (!test_bit(HCI_RUNNING, &hdev->flags))
821 goto done;
822
823 if (!urb->status)
824 hdev->stat.byte_tx += urb->transfer_buffer_length;
825 else
826 hdev->stat.err_tx++;
827
828done:
829 spin_lock(&data->txlock);
830 data->tx_in_flight--;
831 spin_unlock(&data->txlock);
832
833 kfree(urb->setup_packet);
834
835 kfree_skb(skb);
836}
837
838static void btusb_isoc_tx_complete(struct urb *urb)
839{
840 struct sk_buff *skb = urb->context;
Marcel Holtmann89e75332014-09-16 04:44:50 +0200841 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200842
Marcel Holtmann89e75332014-09-16 04:44:50 +0200843 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
844 urb->actual_length);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200845
846 if (!test_bit(HCI_RUNNING, &hdev->flags))
847 goto done;
848
849 if (!urb->status)
850 hdev->stat.byte_tx += urb->transfer_buffer_length;
851 else
852 hdev->stat.err_tx++;
853
854done:
855 kfree(urb->setup_packet);
856
857 kfree_skb(skb);
858}
859
860static int btusb_open(struct hci_dev *hdev)
861{
David Herrmann155961e2012-02-09 21:58:32 +0100862 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200863 int err;
864
865 BT_DBG("%s", hdev->name);
866
Oliver Neukum7bee5492009-08-24 23:44:59 +0200867 err = usb_autopm_get_interface(data->intf);
868 if (err < 0)
869 return err;
870
871 data->intf->needs_remote_wakeup = 1;
872
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200873 if (test_and_set_bit(HCI_RUNNING, &hdev->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +0200874 goto done;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200875
876 if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +0200877 goto done;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200878
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100879 err = btusb_submit_intr_urb(hdev, GFP_KERNEL);
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100880 if (err < 0)
881 goto failed;
882
883 err = btusb_submit_bulk_urb(hdev, GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200884 if (err < 0) {
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100885 usb_kill_anchored_urbs(&data->intr_anchor);
886 goto failed;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200887 }
888
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100889 set_bit(BTUSB_BULK_RUNNING, &data->flags);
890 btusb_submit_bulk_urb(hdev, GFP_KERNEL);
891
Oliver Neukum7bee5492009-08-24 23:44:59 +0200892done:
893 usb_autopm_put_interface(data->intf);
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100894 return 0;
895
896failed:
897 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
898 clear_bit(HCI_RUNNING, &hdev->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200899 usb_autopm_put_interface(data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200900 return err;
901}
902
Oliver Neukum7bee5492009-08-24 23:44:59 +0200903static void btusb_stop_traffic(struct btusb_data *data)
904{
905 usb_kill_anchored_urbs(&data->intr_anchor);
906 usb_kill_anchored_urbs(&data->bulk_anchor);
907 usb_kill_anchored_urbs(&data->isoc_anchor);
908}
909
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200910static int btusb_close(struct hci_dev *hdev)
911{
David Herrmann155961e2012-02-09 21:58:32 +0100912 struct btusb_data *data = hci_get_drvdata(hdev);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200913 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200914
915 BT_DBG("%s", hdev->name);
916
917 if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
918 return 0;
919
Marcel Holtmanne8c3c3d2008-09-23 00:16:36 +0200920 cancel_work_sync(&data->work);
Linus Torvalds404291a2009-11-11 13:32:29 -0800921 cancel_work_sync(&data->waker);
Marcel Holtmanne8c3c3d2008-09-23 00:16:36 +0200922
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200923 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200924 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200925 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200926
927 btusb_stop_traffic(data);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200928 btusb_free_frags(data);
929
Oliver Neukum7bee5492009-08-24 23:44:59 +0200930 err = usb_autopm_get_interface(data->intf);
931 if (err < 0)
Oliver Neukum7b8e2c12009-11-13 14:26:23 +0100932 goto failed;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200933
934 data->intf->needs_remote_wakeup = 0;
935 usb_autopm_put_interface(data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200936
Oliver Neukum7b8e2c12009-11-13 14:26:23 +0100937failed:
938 usb_scuttle_anchored_urbs(&data->deferred);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200939 return 0;
940}
941
942static int btusb_flush(struct hci_dev *hdev)
943{
David Herrmann155961e2012-02-09 21:58:32 +0100944 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200945
946 BT_DBG("%s", hdev->name);
947
948 usb_kill_anchored_urbs(&data->tx_anchor);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200949 btusb_free_frags(data);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200950
951 return 0;
952}
953
Marcel Holtmann047b2ec2014-09-14 09:11:06 +0200954static struct urb *alloc_ctrl_urb(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200955{
David Herrmann155961e2012-02-09 21:58:32 +0100956 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200957 struct usb_ctrlrequest *dr;
958 struct urb *urb;
959 unsigned int pipe;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200960
Marcel Holtmann047b2ec2014-09-14 09:11:06 +0200961 urb = usb_alloc_urb(0, GFP_KERNEL);
962 if (!urb)
963 return ERR_PTR(-ENOMEM);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200964
Marcel Holtmann047b2ec2014-09-14 09:11:06 +0200965 dr = kmalloc(sizeof(*dr), GFP_KERNEL);
966 if (!dr) {
967 usb_free_urb(urb);
968 return ERR_PTR(-ENOMEM);
969 }
970
971 dr->bRequestType = data->cmdreq_type;
972 dr->bRequest = 0;
973 dr->wIndex = 0;
974 dr->wValue = 0;
975 dr->wLength = __cpu_to_le16(skb->len);
976
977 pipe = usb_sndctrlpipe(data->udev, 0x00);
978
Marcel Holtmann89e75332014-09-16 04:44:50 +0200979 usb_fill_control_urb(urb, data->udev, pipe, (void *)dr,
Marcel Holtmann047b2ec2014-09-14 09:11:06 +0200980 skb->data, skb->len, btusb_tx_complete, skb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200981
Marcel Holtmann89e75332014-09-16 04:44:50 +0200982 skb->dev = (void *)hdev;
Marcel Holtmann7bd8f092013-10-11 06:19:18 -0700983
Marcel Holtmann047b2ec2014-09-14 09:11:06 +0200984 return urb;
985}
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200986
Marcel Holtmann047b2ec2014-09-14 09:11:06 +0200987static struct urb *alloc_bulk_urb(struct hci_dev *hdev, struct sk_buff *skb)
988{
989 struct btusb_data *data = hci_get_drvdata(hdev);
990 struct urb *urb;
991 unsigned int pipe;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200992
Marcel Holtmann047b2ec2014-09-14 09:11:06 +0200993 if (!data->bulk_tx_ep)
994 return ERR_PTR(-ENODEV);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200995
Marcel Holtmann047b2ec2014-09-14 09:11:06 +0200996 urb = usb_alloc_urb(0, GFP_KERNEL);
997 if (!urb)
998 return ERR_PTR(-ENOMEM);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200999
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001000 pipe = usb_sndbulkpipe(data->udev, data->bulk_tx_ep->bEndpointAddress);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001001
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001002 usb_fill_bulk_urb(urb, data->udev, pipe,
1003 skb->data, skb->len, btusb_tx_complete, skb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001004
Marcel Holtmann89e75332014-09-16 04:44:50 +02001005 skb->dev = (void *)hdev;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001006
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001007 return urb;
1008}
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001009
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001010static struct urb *alloc_isoc_urb(struct hci_dev *hdev, struct sk_buff *skb)
1011{
1012 struct btusb_data *data = hci_get_drvdata(hdev);
1013 struct urb *urb;
1014 unsigned int pipe;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001015
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001016 if (!data->isoc_tx_ep)
1017 return ERR_PTR(-ENODEV);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001018
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001019 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_KERNEL);
1020 if (!urb)
1021 return ERR_PTR(-ENOMEM);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001022
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001023 pipe = usb_sndisocpipe(data->udev, data->isoc_tx_ep->bEndpointAddress);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001024
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001025 usb_fill_int_urb(urb, data->udev, pipe,
1026 skb->data, skb->len, btusb_isoc_tx_complete,
1027 skb, data->isoc_tx_ep->bInterval);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001028
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001029 urb->transfer_flags = URB_ISO_ASAP;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001030
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001031 __fill_isoc_descriptor(urb, skb->len,
1032 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001033
Marcel Holtmann89e75332014-09-16 04:44:50 +02001034 skb->dev = (void *)hdev;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001035
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001036 return urb;
1037}
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001038
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001039static int submit_tx_urb(struct hci_dev *hdev, struct urb *urb)
1040{
1041 struct btusb_data *data = hci_get_drvdata(hdev);
1042 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001043
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001044 usb_anchor_urb(urb, &data->tx_anchor);
1045
Johan Hedberge9753ef2014-09-14 08:49:34 +03001046 err = usb_submit_urb(urb, GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001047 if (err < 0) {
Paul Bolle5a9b80e2011-10-09 12:12:16 +02001048 if (err != -EPERM && err != -ENODEV)
1049 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +02001050 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001051 kfree(urb->setup_packet);
1052 usb_unanchor_urb(urb);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001053 } else {
1054 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001055 }
1056
Cong Wang54a8a792011-11-22 09:32:57 +08001057 usb_free_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001058 return err;
1059}
1060
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001061static int submit_or_queue_tx_urb(struct hci_dev *hdev, struct urb *urb)
1062{
1063 struct btusb_data *data = hci_get_drvdata(hdev);
1064 unsigned long flags;
1065 bool suspending;
1066
1067 spin_lock_irqsave(&data->txlock, flags);
1068 suspending = test_bit(BTUSB_SUSPENDING, &data->flags);
1069 if (!suspending)
1070 data->tx_in_flight++;
1071 spin_unlock_irqrestore(&data->txlock, flags);
1072
1073 if (!suspending)
1074 return submit_tx_urb(hdev, urb);
1075
1076 usb_anchor_urb(urb, &data->deferred);
1077 schedule_work(&data->waker);
1078
1079 usb_free_urb(urb);
1080 return 0;
1081}
1082
1083static int btusb_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
1084{
1085 struct urb *urb;
1086
1087 BT_DBG("%s", hdev->name);
1088
1089 if (!test_bit(HCI_RUNNING, &hdev->flags))
1090 return -EBUSY;
1091
1092 switch (bt_cb(skb)->pkt_type) {
1093 case HCI_COMMAND_PKT:
1094 urb = alloc_ctrl_urb(hdev, skb);
1095 if (IS_ERR(urb))
1096 return PTR_ERR(urb);
1097
1098 hdev->stat.cmd_tx++;
1099 return submit_or_queue_tx_urb(hdev, urb);
1100
1101 case HCI_ACLDATA_PKT:
1102 urb = alloc_bulk_urb(hdev, skb);
1103 if (IS_ERR(urb))
1104 return PTR_ERR(urb);
1105
1106 hdev->stat.acl_tx++;
1107 return submit_or_queue_tx_urb(hdev, urb);
1108
1109 case HCI_SCODATA_PKT:
1110 if (hci_conn_num(hdev, SCO_LINK) < 1)
1111 return -ENODEV;
1112
1113 urb = alloc_isoc_urb(hdev, skb);
1114 if (IS_ERR(urb))
1115 return PTR_ERR(urb);
1116
1117 hdev->stat.sco_tx++;
1118 return submit_tx_urb(hdev, urb);
1119 }
1120
1121 return -EILSEQ;
1122}
1123
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001124static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
1125{
David Herrmann155961e2012-02-09 21:58:32 +01001126 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001127
1128 BT_DBG("%s evt %d", hdev->name, evt);
1129
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001130 if (hci_conn_num(hdev, SCO_LINK) != data->sco_num) {
1131 data->sco_num = hci_conn_num(hdev, SCO_LINK);
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001132 schedule_work(&data->work);
Marcel Holtmanna780efa2008-11-30 12:17:12 +01001133 }
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001134}
1135
Jesper Juhl42b16b32011-01-17 00:09:38 +01001136static inline int __set_isoc_interface(struct hci_dev *hdev, int altsetting)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001137{
David Herrmann155961e2012-02-09 21:58:32 +01001138 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001139 struct usb_interface *intf = data->isoc;
1140 struct usb_endpoint_descriptor *ep_desc;
1141 int i, err;
1142
1143 if (!data->isoc)
1144 return -ENODEV;
1145
1146 err = usb_set_interface(data->udev, 1, altsetting);
1147 if (err < 0) {
1148 BT_ERR("%s setting interface failed (%d)", hdev->name, -err);
1149 return err;
1150 }
1151
1152 data->isoc_altsetting = altsetting;
1153
1154 data->isoc_tx_ep = NULL;
1155 data->isoc_rx_ep = NULL;
1156
1157 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
1158 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
1159
1160 if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) {
1161 data->isoc_tx_ep = ep_desc;
1162 continue;
1163 }
1164
1165 if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) {
1166 data->isoc_rx_ep = ep_desc;
1167 continue;
1168 }
1169 }
1170
1171 if (!data->isoc_tx_ep || !data->isoc_rx_ep) {
1172 BT_ERR("%s invalid SCO descriptors", hdev->name);
1173 return -ENODEV;
1174 }
1175
1176 return 0;
1177}
1178
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001179static void btusb_work(struct work_struct *work)
1180{
1181 struct btusb_data *data = container_of(work, struct btusb_data, work);
1182 struct hci_dev *hdev = data->hdev;
Mikel Astizf4001d22012-04-11 08:48:51 +02001183 int new_alts;
Oliver Neukum7bee5492009-08-24 23:44:59 +02001184 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001185
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001186 if (data->sco_num > 0) {
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -03001187 if (!test_bit(BTUSB_DID_ISO_RESUME, &data->flags)) {
Oliver Neukum8efdd0c2011-02-11 13:00:06 +01001188 err = usb_autopm_get_interface(data->isoc ? data->isoc : data->intf);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001189 if (err < 0) {
1190 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1191 usb_kill_anchored_urbs(&data->isoc_anchor);
1192 return;
1193 }
1194
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -03001195 set_bit(BTUSB_DID_ISO_RESUME, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001196 }
Mikel Astizf4001d22012-04-11 08:48:51 +02001197
1198 if (hdev->voice_setting & 0x0020) {
1199 static const int alts[3] = { 2, 4, 5 };
Marcel Holtmann89e75332014-09-16 04:44:50 +02001200
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001201 new_alts = alts[data->sco_num - 1];
Mikel Astizf4001d22012-04-11 08:48:51 +02001202 } else {
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001203 new_alts = data->sco_num;
Mikel Astizf4001d22012-04-11 08:48:51 +02001204 }
1205
1206 if (data->isoc_altsetting != new_alts) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001207 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1208 usb_kill_anchored_urbs(&data->isoc_anchor);
1209
Mikel Astizf4001d22012-04-11 08:48:51 +02001210 if (__set_isoc_interface(hdev, new_alts) < 0)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001211 return;
1212 }
1213
1214 if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001215 if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001216 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1217 else
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001218 btusb_submit_isoc_urb(hdev, GFP_KERNEL);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001219 }
1220 } else {
1221 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1222 usb_kill_anchored_urbs(&data->isoc_anchor);
1223
1224 __set_isoc_interface(hdev, 0);
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -03001225 if (test_and_clear_bit(BTUSB_DID_ISO_RESUME, &data->flags))
Oliver Neukum8efdd0c2011-02-11 13:00:06 +01001226 usb_autopm_put_interface(data->isoc ? data->isoc : data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001227 }
1228}
1229
Oliver Neukum7bee5492009-08-24 23:44:59 +02001230static void btusb_waker(struct work_struct *work)
1231{
1232 struct btusb_data *data = container_of(work, struct btusb_data, waker);
1233 int err;
1234
1235 err = usb_autopm_get_interface(data->intf);
1236 if (err < 0)
1237 return;
1238
1239 usb_autopm_put_interface(data->intf);
1240}
1241
Marcel Holtmann9f8f9622013-04-10 08:11:35 -07001242static int btusb_setup_bcm92035(struct hci_dev *hdev)
1243{
1244 struct sk_buff *skb;
1245 u8 val = 0x00;
1246
1247 BT_DBG("%s", hdev->name);
1248
1249 skb = __hci_cmd_sync(hdev, 0xfc3b, 1, &val, HCI_INIT_TIMEOUT);
1250 if (IS_ERR(skb))
1251 BT_ERR("BCM92035 command failed (%ld)", -PTR_ERR(skb));
1252 else
1253 kfree_skb(skb);
1254
1255 return 0;
1256}
1257
Marcel Holtmann81cac642014-01-03 03:02:36 -08001258static int btusb_setup_csr(struct hci_dev *hdev)
1259{
1260 struct hci_rp_read_local_version *rp;
1261 struct sk_buff *skb;
1262 int ret;
1263
1264 BT_DBG("%s", hdev->name);
1265
1266 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
1267 HCI_INIT_TIMEOUT);
1268 if (IS_ERR(skb)) {
1269 BT_ERR("Reading local version failed (%ld)", -PTR_ERR(skb));
1270 return -PTR_ERR(skb);
1271 }
1272
Marcel Holtmann89e75332014-09-16 04:44:50 +02001273 rp = (struct hci_rp_read_local_version *)skb->data;
Marcel Holtmann81cac642014-01-03 03:02:36 -08001274
1275 if (!rp->status) {
1276 if (le16_to_cpu(rp->manufacturer) != 10) {
1277 /* Clear the reset quirk since this is not an actual
1278 * early Bluetooth 1.1 device from CSR.
1279 */
1280 clear_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
1281
1282 /* These fake CSR controllers have all a broken
1283 * stored link key handling and so just disable it.
1284 */
1285 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY,
1286 &hdev->quirks);
1287 }
1288 }
1289
1290 ret = -bt_to_errno(rp->status);
1291
1292 kfree_skb(skb);
1293
1294 return ret;
1295}
1296
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001297struct intel_version {
1298 u8 status;
1299 u8 hw_platform;
1300 u8 hw_variant;
1301 u8 hw_revision;
1302 u8 fw_variant;
1303 u8 fw_revision;
1304 u8 fw_build_num;
1305 u8 fw_build_ww;
1306 u8 fw_build_yy;
1307 u8 fw_patch_num;
1308} __packed;
1309
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001310struct intel_boot_params {
1311 __u8 status;
1312 __u8 otp_format;
1313 __u8 otp_content;
1314 __u8 otp_patch;
1315 __le16 dev_revid;
1316 __u8 secure_boot;
1317 __u8 key_from_hdr;
1318 __u8 key_type;
1319 __u8 otp_lock;
1320 __u8 api_lock;
1321 __u8 debug_lock;
1322 bdaddr_t otp_bdaddr;
1323 __u8 min_fw_build_nn;
1324 __u8 min_fw_build_cw;
1325 __u8 min_fw_build_yy;
1326 __u8 limited_cce;
1327 __u8 unlocked_state;
1328} __packed;
1329
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001330static const struct firmware *btusb_setup_intel_get_fw(struct hci_dev *hdev,
Marcel Holtmann89e75332014-09-16 04:44:50 +02001331 struct intel_version *ver)
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001332{
1333 const struct firmware *fw;
1334 char fwname[64];
1335 int ret;
1336
1337 snprintf(fwname, sizeof(fwname),
1338 "intel/ibt-hw-%x.%x.%x-fw-%x.%x.%x.%x.%x.bseq",
1339 ver->hw_platform, ver->hw_variant, ver->hw_revision,
1340 ver->fw_variant, ver->fw_revision, ver->fw_build_num,
1341 ver->fw_build_ww, ver->fw_build_yy);
1342
1343 ret = request_firmware(&fw, fwname, &hdev->dev);
1344 if (ret < 0) {
1345 if (ret == -EINVAL) {
1346 BT_ERR("%s Intel firmware file request failed (%d)",
1347 hdev->name, ret);
1348 return NULL;
1349 }
1350
1351 BT_ERR("%s failed to open Intel firmware file: %s(%d)",
1352 hdev->name, fwname, ret);
1353
1354 /* If the correct firmware patch file is not found, use the
1355 * default firmware patch file instead
1356 */
1357 snprintf(fwname, sizeof(fwname), "intel/ibt-hw-%x.%x.bseq",
1358 ver->hw_platform, ver->hw_variant);
1359 if (request_firmware(&fw, fwname, &hdev->dev) < 0) {
1360 BT_ERR("%s failed to open default Intel fw file: %s",
1361 hdev->name, fwname);
1362 return NULL;
1363 }
1364 }
1365
1366 BT_INFO("%s: Intel Bluetooth firmware file: %s", hdev->name, fwname);
1367
1368 return fw;
1369}
1370
1371static int btusb_setup_intel_patching(struct hci_dev *hdev,
1372 const struct firmware *fw,
1373 const u8 **fw_ptr, int *disable_patch)
1374{
1375 struct sk_buff *skb;
1376 struct hci_command_hdr *cmd;
1377 const u8 *cmd_param;
1378 struct hci_event_hdr *evt = NULL;
1379 const u8 *evt_param = NULL;
1380 int remain = fw->size - (*fw_ptr - fw->data);
1381
1382 /* The first byte indicates the types of the patch command or event.
1383 * 0x01 means HCI command and 0x02 is HCI event. If the first bytes
1384 * in the current firmware buffer doesn't start with 0x01 or
1385 * the size of remain buffer is smaller than HCI command header,
1386 * the firmware file is corrupted and it should stop the patching
1387 * process.
1388 */
1389 if (remain > HCI_COMMAND_HDR_SIZE && *fw_ptr[0] != 0x01) {
1390 BT_ERR("%s Intel fw corrupted: invalid cmd read", hdev->name);
1391 return -EINVAL;
1392 }
1393 (*fw_ptr)++;
1394 remain--;
1395
1396 cmd = (struct hci_command_hdr *)(*fw_ptr);
1397 *fw_ptr += sizeof(*cmd);
1398 remain -= sizeof(*cmd);
1399
1400 /* Ensure that the remain firmware data is long enough than the length
1401 * of command parameter. If not, the firmware file is corrupted.
1402 */
1403 if (remain < cmd->plen) {
1404 BT_ERR("%s Intel fw corrupted: invalid cmd len", hdev->name);
1405 return -EFAULT;
1406 }
1407
1408 /* If there is a command that loads a patch in the firmware
1409 * file, then enable the patch upon success, otherwise just
1410 * disable the manufacturer mode, for example patch activation
1411 * is not required when the default firmware patch file is used
1412 * because there are no patch data to load.
1413 */
1414 if (*disable_patch && le16_to_cpu(cmd->opcode) == 0xfc8e)
1415 *disable_patch = 0;
1416
1417 cmd_param = *fw_ptr;
1418 *fw_ptr += cmd->plen;
1419 remain -= cmd->plen;
1420
1421 /* This reads the expected events when the above command is sent to the
1422 * device. Some vendor commands expects more than one events, for
1423 * example command status event followed by vendor specific event.
1424 * For this case, it only keeps the last expected event. so the command
1425 * can be sent with __hci_cmd_sync_ev() which returns the sk_buff of
1426 * last expected event.
1427 */
1428 while (remain > HCI_EVENT_HDR_SIZE && *fw_ptr[0] == 0x02) {
1429 (*fw_ptr)++;
1430 remain--;
1431
1432 evt = (struct hci_event_hdr *)(*fw_ptr);
1433 *fw_ptr += sizeof(*evt);
1434 remain -= sizeof(*evt);
1435
1436 if (remain < evt->plen) {
1437 BT_ERR("%s Intel fw corrupted: invalid evt len",
1438 hdev->name);
1439 return -EFAULT;
1440 }
1441
1442 evt_param = *fw_ptr;
1443 *fw_ptr += evt->plen;
1444 remain -= evt->plen;
1445 }
1446
1447 /* Every HCI commands in the firmware file has its correspond event.
1448 * If event is not found or remain is smaller than zero, the firmware
1449 * file is corrupted.
1450 */
1451 if (!evt || !evt_param || remain < 0) {
1452 BT_ERR("%s Intel fw corrupted: invalid evt read", hdev->name);
1453 return -EFAULT;
1454 }
1455
1456 skb = __hci_cmd_sync_ev(hdev, le16_to_cpu(cmd->opcode), cmd->plen,
1457 cmd_param, evt->evt, HCI_INIT_TIMEOUT);
1458 if (IS_ERR(skb)) {
1459 BT_ERR("%s sending Intel patch command (0x%4.4x) failed (%ld)",
1460 hdev->name, cmd->opcode, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001461 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001462 }
1463
1464 /* It ensures that the returned event matches the event data read from
1465 * the firmware file. At fist, it checks the length and then
1466 * the contents of the event.
1467 */
1468 if (skb->len != evt->plen) {
1469 BT_ERR("%s mismatch event length (opcode 0x%4.4x)", hdev->name,
1470 le16_to_cpu(cmd->opcode));
1471 kfree_skb(skb);
1472 return -EFAULT;
1473 }
1474
1475 if (memcmp(skb->data, evt_param, evt->plen)) {
1476 BT_ERR("%s mismatch event parameter (opcode 0x%4.4x)",
1477 hdev->name, le16_to_cpu(cmd->opcode));
1478 kfree_skb(skb);
1479 return -EFAULT;
1480 }
1481 kfree_skb(skb);
1482
1483 return 0;
1484}
1485
Marcel Holtmann40cb0982014-07-02 12:06:45 +02001486#define BDADDR_INTEL (&(bdaddr_t) {{0x00, 0x8b, 0x9e, 0x19, 0x03, 0x00}})
1487
1488static int btusb_check_bdaddr_intel(struct hci_dev *hdev)
1489{
1490 struct sk_buff *skb;
1491 struct hci_rp_read_bd_addr *rp;
1492
1493 skb = __hci_cmd_sync(hdev, HCI_OP_READ_BD_ADDR, 0, NULL,
1494 HCI_INIT_TIMEOUT);
1495 if (IS_ERR(skb)) {
1496 BT_ERR("%s reading Intel device address failed (%ld)",
1497 hdev->name, PTR_ERR(skb));
1498 return PTR_ERR(skb);
1499 }
1500
1501 if (skb->len != sizeof(*rp)) {
1502 BT_ERR("%s Intel device address length mismatch", hdev->name);
1503 kfree_skb(skb);
1504 return -EIO;
1505 }
1506
Marcel Holtmann89e75332014-09-16 04:44:50 +02001507 rp = (struct hci_rp_read_bd_addr *)skb->data;
Marcel Holtmann40cb0982014-07-02 12:06:45 +02001508 if (rp->status) {
1509 BT_ERR("%s Intel device address result failed (%02x)",
1510 hdev->name, rp->status);
1511 kfree_skb(skb);
1512 return -bt_to_errno(rp->status);
1513 }
1514
1515 /* For some Intel based controllers, the default Bluetooth device
1516 * address 00:03:19:9E:8B:00 can be found. These controllers are
1517 * fully operational, but have the danger of duplicate addresses
1518 * and that in turn can cause problems with Bluetooth operation.
1519 */
Marcel Holtmann4739b5b2014-07-04 16:54:38 +02001520 if (!bacmp(&rp->bdaddr, BDADDR_INTEL)) {
Marcel Holtmann40cb0982014-07-02 12:06:45 +02001521 BT_ERR("%s found Intel default device address (%pMR)",
1522 hdev->name, &rp->bdaddr);
Marcel Holtmann4739b5b2014-07-04 16:54:38 +02001523 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
1524 }
Marcel Holtmann40cb0982014-07-02 12:06:45 +02001525
1526 kfree_skb(skb);
1527
1528 return 0;
1529}
1530
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001531static int btusb_setup_intel(struct hci_dev *hdev)
1532{
1533 struct sk_buff *skb;
1534 const struct firmware *fw;
1535 const u8 *fw_ptr;
1536 int disable_patch;
1537 struct intel_version *ver;
1538
1539 const u8 mfg_enable[] = { 0x01, 0x00 };
1540 const u8 mfg_disable[] = { 0x00, 0x00 };
1541 const u8 mfg_reset_deactivate[] = { 0x00, 0x01 };
1542 const u8 mfg_reset_activate[] = { 0x00, 0x02 };
1543
1544 BT_DBG("%s", hdev->name);
1545
1546 /* The controller has a bug with the first HCI command sent to it
1547 * returning number of completed commands as zero. This would stall the
1548 * command processing in the Bluetooth core.
1549 *
1550 * As a workaround, send HCI Reset command first which will reset the
1551 * number of completed commands and allow normal command processing
1552 * from now on.
1553 */
1554 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
1555 if (IS_ERR(skb)) {
1556 BT_ERR("%s sending initial HCI reset command failed (%ld)",
1557 hdev->name, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001558 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001559 }
1560 kfree_skb(skb);
1561
1562 /* Read Intel specific controller version first to allow selection of
1563 * which firmware file to load.
1564 *
1565 * The returned information are hardware variant and revision plus
1566 * firmware variant, revision and build number.
1567 */
1568 skb = __hci_cmd_sync(hdev, 0xfc05, 0, NULL, HCI_INIT_TIMEOUT);
1569 if (IS_ERR(skb)) {
1570 BT_ERR("%s reading Intel fw version command failed (%ld)",
1571 hdev->name, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001572 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001573 }
1574
1575 if (skb->len != sizeof(*ver)) {
1576 BT_ERR("%s Intel version event length mismatch", hdev->name);
1577 kfree_skb(skb);
1578 return -EIO;
1579 }
1580
1581 ver = (struct intel_version *)skb->data;
1582 if (ver->status) {
1583 BT_ERR("%s Intel fw version event failed (%02x)", hdev->name,
1584 ver->status);
1585 kfree_skb(skb);
1586 return -bt_to_errno(ver->status);
1587 }
1588
1589 BT_INFO("%s: read Intel version: %02x%02x%02x%02x%02x%02x%02x%02x%02x",
1590 hdev->name, ver->hw_platform, ver->hw_variant,
1591 ver->hw_revision, ver->fw_variant, ver->fw_revision,
1592 ver->fw_build_num, ver->fw_build_ww, ver->fw_build_yy,
1593 ver->fw_patch_num);
1594
1595 /* fw_patch_num indicates the version of patch the device currently
1596 * have. If there is no patch data in the device, it is always 0x00.
1597 * So, if it is other than 0x00, no need to patch the deivce again.
1598 */
1599 if (ver->fw_patch_num) {
1600 BT_INFO("%s: Intel device is already patched. patch num: %02x",
1601 hdev->name, ver->fw_patch_num);
1602 kfree_skb(skb);
Marcel Holtmann40cb0982014-07-02 12:06:45 +02001603 btusb_check_bdaddr_intel(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001604 return 0;
1605 }
1606
1607 /* Opens the firmware patch file based on the firmware version read
1608 * from the controller. If it fails to open the matching firmware
1609 * patch file, it tries to open the default firmware patch file.
1610 * If no patch file is found, allow the device to operate without
1611 * a patch.
1612 */
1613 fw = btusb_setup_intel_get_fw(hdev, ver);
1614 if (!fw) {
1615 kfree_skb(skb);
Marcel Holtmann40cb0982014-07-02 12:06:45 +02001616 btusb_check_bdaddr_intel(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001617 return 0;
1618 }
1619 fw_ptr = fw->data;
1620
1621 /* This Intel specific command enables the manufacturer mode of the
1622 * controller.
1623 *
1624 * Only while this mode is enabled, the driver can download the
1625 * firmware patch data and configuration parameters.
1626 */
1627 skb = __hci_cmd_sync(hdev, 0xfc11, 2, mfg_enable, HCI_INIT_TIMEOUT);
1628 if (IS_ERR(skb)) {
1629 BT_ERR("%s entering Intel manufacturer mode failed (%ld)",
1630 hdev->name, PTR_ERR(skb));
1631 release_firmware(fw);
Adam Leed9c78e92013-07-10 10:02:12 +08001632 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001633 }
1634
1635 if (skb->data[0]) {
1636 u8 evt_status = skb->data[0];
Marcel Holtmann89e75332014-09-16 04:44:50 +02001637
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001638 BT_ERR("%s enable Intel manufacturer mode event failed (%02x)",
1639 hdev->name, evt_status);
1640 kfree_skb(skb);
1641 release_firmware(fw);
1642 return -bt_to_errno(evt_status);
1643 }
1644 kfree_skb(skb);
1645
1646 disable_patch = 1;
1647
1648 /* The firmware data file consists of list of Intel specific HCI
1649 * commands and its expected events. The first byte indicates the
1650 * type of the message, either HCI command or HCI event.
1651 *
1652 * It reads the command and its expected event from the firmware file,
1653 * and send to the controller. Once __hci_cmd_sync_ev() returns,
1654 * the returned event is compared with the event read from the firmware
1655 * file and it will continue until all the messages are downloaded to
1656 * the controller.
1657 *
1658 * Once the firmware patching is completed successfully,
1659 * the manufacturer mode is disabled with reset and activating the
1660 * downloaded patch.
1661 *
1662 * If the firmware patching fails, the manufacturer mode is
1663 * disabled with reset and deactivating the patch.
1664 *
1665 * If the default patch file is used, no reset is done when disabling
1666 * the manufacturer.
1667 */
1668 while (fw->size > fw_ptr - fw->data) {
1669 int ret;
1670
1671 ret = btusb_setup_intel_patching(hdev, fw, &fw_ptr,
1672 &disable_patch);
1673 if (ret < 0)
1674 goto exit_mfg_deactivate;
1675 }
1676
1677 release_firmware(fw);
1678
1679 if (disable_patch)
1680 goto exit_mfg_disable;
1681
1682 /* Patching completed successfully and disable the manufacturer mode
1683 * with reset and activate the downloaded firmware patches.
1684 */
1685 skb = __hci_cmd_sync(hdev, 0xfc11, sizeof(mfg_reset_activate),
1686 mfg_reset_activate, HCI_INIT_TIMEOUT);
1687 if (IS_ERR(skb)) {
1688 BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
1689 hdev->name, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001690 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001691 }
1692 kfree_skb(skb);
1693
1694 BT_INFO("%s: Intel Bluetooth firmware patch completed and activated",
1695 hdev->name);
1696
Marcel Holtmann40cb0982014-07-02 12:06:45 +02001697 btusb_check_bdaddr_intel(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001698 return 0;
1699
1700exit_mfg_disable:
1701 /* Disable the manufacturer mode without reset */
1702 skb = __hci_cmd_sync(hdev, 0xfc11, sizeof(mfg_disable), mfg_disable,
1703 HCI_INIT_TIMEOUT);
1704 if (IS_ERR(skb)) {
1705 BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
1706 hdev->name, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001707 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001708 }
1709 kfree_skb(skb);
1710
1711 BT_INFO("%s: Intel Bluetooth firmware patch completed", hdev->name);
Marcel Holtmann40cb0982014-07-02 12:06:45 +02001712
1713 btusb_check_bdaddr_intel(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001714 return 0;
1715
1716exit_mfg_deactivate:
1717 release_firmware(fw);
1718
1719 /* Patching failed. Disable the manufacturer mode with reset and
1720 * deactivate the downloaded firmware patches.
1721 */
1722 skb = __hci_cmd_sync(hdev, 0xfc11, sizeof(mfg_reset_deactivate),
1723 mfg_reset_deactivate, HCI_INIT_TIMEOUT);
1724 if (IS_ERR(skb)) {
1725 BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
1726 hdev->name, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001727 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001728 }
1729 kfree_skb(skb);
1730
1731 BT_INFO("%s: Intel Bluetooth firmware patch completed and deactivated",
1732 hdev->name);
1733
Marcel Holtmann40cb0982014-07-02 12:06:45 +02001734 btusb_check_bdaddr_intel(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001735 return 0;
1736}
1737
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001738static int inject_cmd_complete(struct hci_dev *hdev, __u16 opcode)
1739{
1740 struct sk_buff *skb;
1741 struct hci_event_hdr *hdr;
1742 struct hci_ev_cmd_complete *evt;
1743
1744 skb = bt_skb_alloc(sizeof(*hdr) + sizeof(*evt) + 1, GFP_ATOMIC);
1745 if (!skb)
1746 return -ENOMEM;
1747
1748 hdr = (struct hci_event_hdr *)skb_put(skb, sizeof(*hdr));
1749 hdr->evt = HCI_EV_CMD_COMPLETE;
1750 hdr->plen = sizeof(*evt) + 1;
1751
1752 evt = (struct hci_ev_cmd_complete *)skb_put(skb, sizeof(*evt));
1753 evt->ncmd = 0x01;
1754 evt->opcode = cpu_to_le16(opcode);
1755
1756 *skb_put(skb, 1) = 0x00;
1757
1758 bt_cb(skb)->pkt_type = HCI_EVENT_PKT;
1759
1760 return hci_recv_frame(hdev, skb);
1761}
1762
1763static int btusb_recv_bulk_intel(struct btusb_data *data, void *buffer,
1764 int count)
1765{
1766 /* When the device is in bootloader mode, then it can send
1767 * events via the bulk endpoint. These events are treated the
1768 * same way as the ones received from the interrupt endpoint.
1769 */
1770 if (test_bit(BTUSB_BOOTLOADER, &data->flags))
1771 return btusb_recv_intr(data, buffer, count);
1772
1773 return btusb_recv_bulk(data, buffer, count);
1774}
1775
1776static int btusb_recv_event_intel(struct hci_dev *hdev, struct sk_buff *skb)
1777{
1778 struct btusb_data *data = hci_get_drvdata(hdev);
1779
1780 if (test_bit(BTUSB_BOOTLOADER, &data->flags)) {
1781 struct hci_event_hdr *hdr = (void *)skb->data;
1782
1783 /* When the firmware loading completes the device sends
1784 * out a vendor specific event indicating the result of
1785 * the firmware loading.
1786 */
1787 if (skb->len == 7 && hdr->evt == 0xff && hdr->plen == 0x05 &&
1788 skb->data[2] == 0x06) {
1789 if (skb->data[3] != 0x00)
1790 test_bit(BTUSB_FIRMWARE_FAILED, &data->flags);
1791
Marcel Holtmannce6bb922015-01-28 01:58:40 -08001792 if (test_and_clear_bit(BTUSB_DOWNLOADING,
1793 &data->flags) &&
1794 test_bit(BTUSB_FIRMWARE_LOADED, &data->flags))
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001795 wake_up_interruptible(&hdev->req_wait_q);
1796 }
1797
1798 /* When switching to the operational firmware the device
1799 * sends a vendor specific event indicating that the bootup
1800 * completed.
1801 */
1802 if (skb->len == 9 && hdr->evt == 0xff && hdr->plen == 0x07 &&
1803 skb->data[2] == 0x02) {
1804 if (test_and_clear_bit(BTUSB_BOOTING, &data->flags))
1805 wake_up_interruptible(&hdev->req_wait_q);
1806 }
1807 }
1808
1809 return hci_recv_frame(hdev, skb);
1810}
1811
1812static int btusb_send_frame_intel(struct hci_dev *hdev, struct sk_buff *skb)
1813{
1814 struct btusb_data *data = hci_get_drvdata(hdev);
1815 struct urb *urb;
1816
1817 BT_DBG("%s", hdev->name);
1818
1819 if (!test_bit(HCI_RUNNING, &hdev->flags))
1820 return -EBUSY;
1821
1822 switch (bt_cb(skb)->pkt_type) {
1823 case HCI_COMMAND_PKT:
1824 if (test_bit(BTUSB_BOOTLOADER, &data->flags)) {
1825 struct hci_command_hdr *cmd = (void *)skb->data;
1826 __u16 opcode = le16_to_cpu(cmd->opcode);
1827
1828 /* When in bootloader mode and the command 0xfc09
1829 * is received, it needs to be send down the
1830 * bulk endpoint. So allocate a bulk URB instead.
1831 */
1832 if (opcode == 0xfc09)
1833 urb = alloc_bulk_urb(hdev, skb);
1834 else
1835 urb = alloc_ctrl_urb(hdev, skb);
1836
1837 /* When the 0xfc01 command is issued to boot into
1838 * the operational firmware, it will actually not
1839 * send a command complete event. To keep the flow
1840 * control working inject that event here.
1841 */
1842 if (opcode == 0xfc01)
1843 inject_cmd_complete(hdev, opcode);
1844 } else {
1845 urb = alloc_ctrl_urb(hdev, skb);
1846 }
1847 if (IS_ERR(urb))
1848 return PTR_ERR(urb);
1849
1850 hdev->stat.cmd_tx++;
1851 return submit_or_queue_tx_urb(hdev, urb);
1852
1853 case HCI_ACLDATA_PKT:
1854 urb = alloc_bulk_urb(hdev, skb);
1855 if (IS_ERR(urb))
1856 return PTR_ERR(urb);
1857
1858 hdev->stat.acl_tx++;
1859 return submit_or_queue_tx_urb(hdev, urb);
1860
1861 case HCI_SCODATA_PKT:
1862 if (hci_conn_num(hdev, SCO_LINK) < 1)
1863 return -ENODEV;
1864
1865 urb = alloc_isoc_urb(hdev, skb);
1866 if (IS_ERR(urb))
1867 return PTR_ERR(urb);
1868
1869 hdev->stat.sco_tx++;
1870 return submit_tx_urb(hdev, urb);
1871 }
1872
1873 return -EILSEQ;
1874}
1875
1876static int btusb_intel_secure_send(struct hci_dev *hdev, u8 fragment_type,
1877 u32 plen, const void *param)
1878{
1879 while (plen > 0) {
1880 struct sk_buff *skb;
1881 u8 cmd_param[253], fragment_len = (plen > 252) ? 252 : plen;
1882
1883 cmd_param[0] = fragment_type;
1884 memcpy(cmd_param + 1, param, fragment_len);
1885
1886 skb = __hci_cmd_sync(hdev, 0xfc09, fragment_len + 1,
1887 cmd_param, HCI_INIT_TIMEOUT);
1888 if (IS_ERR(skb))
1889 return PTR_ERR(skb);
1890
1891 kfree_skb(skb);
1892
1893 plen -= fragment_len;
1894 param += fragment_len;
1895 }
1896
1897 return 0;
1898}
1899
1900static void btusb_intel_version_info(struct hci_dev *hdev,
1901 struct intel_version *ver)
1902{
1903 const char *variant;
1904
1905 switch (ver->fw_variant) {
1906 case 0x06:
1907 variant = "Bootloader";
1908 break;
1909 case 0x23:
1910 variant = "Firmware";
1911 break;
1912 default:
1913 return;
1914 }
1915
1916 BT_INFO("%s: %s revision %u.%u build %u week %u %u", hdev->name,
1917 variant, ver->fw_revision >> 4, ver->fw_revision & 0x0f,
1918 ver->fw_build_num, ver->fw_build_ww, 2000 + ver->fw_build_yy);
1919}
1920
1921static int btusb_setup_intel_new(struct hci_dev *hdev)
1922{
1923 static const u8 reset_param[] = { 0x00, 0x01, 0x00, 0x01,
1924 0x00, 0x08, 0x04, 0x00 };
1925 struct btusb_data *data = hci_get_drvdata(hdev);
1926 struct sk_buff *skb;
1927 struct intel_version *ver;
1928 struct intel_boot_params *params;
1929 const struct firmware *fw;
1930 const u8 *fw_ptr;
1931 char fwname[64];
1932 ktime_t calltime, delta, rettime;
1933 unsigned long long duration;
1934 int err;
1935
1936 BT_DBG("%s", hdev->name);
1937
1938 calltime = ktime_get();
1939
1940 /* Read the Intel version information to determine if the device
1941 * is in bootloader mode or if it already has operational firmware
1942 * loaded.
1943 */
1944 skb = __hci_cmd_sync(hdev, 0xfc05, 0, NULL, HCI_INIT_TIMEOUT);
1945 if (IS_ERR(skb)) {
1946 BT_ERR("%s: Reading Intel version information failed (%ld)",
1947 hdev->name, PTR_ERR(skb));
1948 return PTR_ERR(skb);
1949 }
1950
1951 if (skb->len != sizeof(*ver)) {
1952 BT_ERR("%s: Intel version event size mismatch", hdev->name);
1953 kfree_skb(skb);
1954 return -EILSEQ;
1955 }
1956
1957 ver = (struct intel_version *)skb->data;
1958 if (ver->status) {
1959 BT_ERR("%s: Intel version command failure (%02x)",
1960 hdev->name, ver->status);
1961 err = -bt_to_errno(ver->status);
1962 kfree_skb(skb);
1963 return err;
1964 }
1965
1966 /* The hardware platform number has a fixed value of 0x37 and
1967 * for now only accept this single value.
1968 */
1969 if (ver->hw_platform != 0x37) {
1970 BT_ERR("%s: Unsupported Intel hardware platform (%u)",
1971 hdev->name, ver->hw_platform);
1972 kfree_skb(skb);
1973 return -EINVAL;
1974 }
1975
1976 /* At the moment only the hardware variant iBT 3.0 (LnP/SfP) is
1977 * supported by this firmware loading method. This check has been
1978 * put in place to ensure correct forward compatibility options
1979 * when newer hardware variants come along.
1980 */
1981 if (ver->hw_variant != 0x0b) {
1982 BT_ERR("%s: Unsupported Intel hardware variant (%u)",
1983 hdev->name, ver->hw_variant);
1984 kfree_skb(skb);
1985 return -EINVAL;
1986 }
1987
1988 btusb_intel_version_info(hdev, ver);
1989
1990 /* The firmware variant determines if the device is in bootloader
1991 * mode or is running operational firmware. The value 0x06 identifies
1992 * the bootloader and the value 0x23 identifies the operational
1993 * firmware.
1994 *
1995 * When the operational firmware is already present, then only
1996 * the check for valid Bluetooth device address is needed. This
1997 * determines if the device will be added as configured or
1998 * unconfigured controller.
1999 *
2000 * It is not possible to use the Secure Boot Parameters in this
2001 * case since that command is only available in bootloader mode.
2002 */
2003 if (ver->fw_variant == 0x23) {
2004 kfree_skb(skb);
2005 clear_bit(BTUSB_BOOTLOADER, &data->flags);
2006 btusb_check_bdaddr_intel(hdev);
2007 return 0;
2008 }
2009
2010 /* If the device is not in bootloader mode, then the only possible
2011 * choice is to return an error and abort the device initialization.
2012 */
2013 if (ver->fw_variant != 0x06) {
2014 BT_ERR("%s: Unsupported Intel firmware variant (%u)",
2015 hdev->name, ver->fw_variant);
2016 kfree_skb(skb);
2017 return -ENODEV;
2018 }
2019
2020 kfree_skb(skb);
2021
2022 /* Read the secure boot parameters to identify the operating
2023 * details of the bootloader.
2024 */
2025 skb = __hci_cmd_sync(hdev, 0xfc0d, 0, NULL, HCI_INIT_TIMEOUT);
2026 if (IS_ERR(skb)) {
2027 BT_ERR("%s: Reading Intel boot parameters failed (%ld)",
2028 hdev->name, PTR_ERR(skb));
2029 return PTR_ERR(skb);
2030 }
2031
2032 if (skb->len != sizeof(*params)) {
2033 BT_ERR("%s: Intel boot parameters size mismatch", hdev->name);
2034 kfree_skb(skb);
2035 return -EILSEQ;
2036 }
2037
2038 params = (struct intel_boot_params *)skb->data;
2039 if (params->status) {
2040 BT_ERR("%s: Intel boot parameters command failure (%02x)",
2041 hdev->name, params->status);
2042 err = -bt_to_errno(params->status);
2043 kfree_skb(skb);
2044 return err;
2045 }
2046
2047 BT_INFO("%s: Device revision is %u", hdev->name,
2048 le16_to_cpu(params->dev_revid));
2049
2050 BT_INFO("%s: Secure boot is %s", hdev->name,
2051 params->secure_boot ? "enabled" : "disabled");
2052
2053 BT_INFO("%s: Minimum firmware build %u week %u %u", hdev->name,
2054 params->min_fw_build_nn, params->min_fw_build_cw,
2055 2000 + params->min_fw_build_yy);
2056
2057 /* It is required that every single firmware fragment is acknowledged
2058 * with a command complete event. If the boot parameters indicate
2059 * that this bootloader does not send them, then abort the setup.
2060 */
2061 if (params->limited_cce != 0x00) {
2062 BT_ERR("%s: Unsupported Intel firmware loading method (%u)",
2063 hdev->name, params->limited_cce);
2064 kfree_skb(skb);
2065 return -EINVAL;
2066 }
2067
2068 /* If the OTP has no valid Bluetooth device address, then there will
2069 * also be no valid address for the operational firmware.
2070 */
2071 if (!bacmp(&params->otp_bdaddr, BDADDR_ANY)) {
2072 BT_INFO("%s: No device address configured", hdev->name);
2073 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
2074 }
2075
2076 /* With this Intel bootloader only the hardware variant and device
2077 * revision information are used to select the right firmware.
2078 *
2079 * Currently this bootloader support is limited to hardware variant
2080 * iBT 3.0 (LnP/SfP) which is identified by the value 11 (0x0b).
2081 */
2082 snprintf(fwname, sizeof(fwname), "intel/ibt-11-%u.sfi",
2083 le16_to_cpu(params->dev_revid));
2084
2085 err = request_firmware(&fw, fwname, &hdev->dev);
2086 if (err < 0) {
2087 BT_ERR("%s: Failed to load Intel firmware file (%d)",
2088 hdev->name, err);
2089 kfree_skb(skb);
2090 return err;
2091 }
2092
2093 BT_INFO("%s: Found device firmware: %s", hdev->name, fwname);
2094
2095 kfree_skb(skb);
2096
2097 if (fw->size < 644) {
2098 BT_ERR("%s: Invalid size of firmware file (%zu)",
2099 hdev->name, fw->size);
2100 err = -EBADF;
2101 goto done;
2102 }
2103
2104 set_bit(BTUSB_DOWNLOADING, &data->flags);
2105
2106 /* Start the firmware download transaction with the Init fragment
2107 * represented by the 128 bytes of CSS header.
2108 */
2109 err = btusb_intel_secure_send(hdev, 0x00, 128, fw->data);
2110 if (err < 0) {
2111 BT_ERR("%s: Failed to send firmware header (%d)",
2112 hdev->name, err);
2113 goto done;
2114 }
2115
2116 /* Send the 256 bytes of public key information from the firmware
2117 * as the PKey fragment.
2118 */
2119 err = btusb_intel_secure_send(hdev, 0x03, 256, fw->data + 128);
2120 if (err < 0) {
2121 BT_ERR("%s: Failed to send firmware public key (%d)",
2122 hdev->name, err);
2123 goto done;
2124 }
2125
2126 /* Send the 256 bytes of signature information from the firmware
2127 * as the Sign fragment.
2128 */
2129 err = btusb_intel_secure_send(hdev, 0x02, 256, fw->data + 388);
2130 if (err < 0) {
2131 BT_ERR("%s: Failed to send firmware signature (%d)",
2132 hdev->name, err);
2133 goto done;
2134 }
2135
2136 fw_ptr = fw->data + 644;
2137
2138 while (fw_ptr - fw->data < fw->size) {
2139 struct hci_command_hdr *cmd = (void *)fw_ptr;
2140 u8 cmd_len;
2141
2142 cmd_len = sizeof(*cmd) + cmd->plen;
2143
2144 /* Send each command from the firmware data buffer as
2145 * a single Data fragment.
2146 */
2147 err = btusb_intel_secure_send(hdev, 0x01, cmd_len, fw_ptr);
2148 if (err < 0) {
2149 BT_ERR("%s: Failed to send firmware data (%d)",
2150 hdev->name, err);
2151 goto done;
2152 }
2153
2154 fw_ptr += cmd_len;
2155 }
2156
Marcel Holtmannce6bb922015-01-28 01:58:40 -08002157 set_bit(BTUSB_FIRMWARE_LOADED, &data->flags);
2158
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002159 /* Before switching the device into operational mode and with that
2160 * booting the loaded firmware, wait for the bootloader notification
2161 * that all fragments have been successfully received.
2162 *
2163 * When the event processing receives the notification, then this
2164 * flag will be cleared. So just in case that happens really quickly,
2165 * check it first before adding the wait queue.
2166 */
2167 if (test_bit(BTUSB_DOWNLOADING, &data->flags)) {
2168 DECLARE_WAITQUEUE(wait, current);
2169 signed long timeout;
2170
2171 BT_INFO("%s: Waiting for firmware download to complete",
2172 hdev->name);
2173
2174 add_wait_queue(&hdev->req_wait_q, &wait);
2175 set_current_state(TASK_INTERRUPTIBLE);
2176
2177 /* The firmware loading should not take longer than 5 seconds
2178 * and thus just timeout if that happens and fail the setup
2179 * of this device.
2180 */
2181 timeout = schedule_timeout(msecs_to_jiffies(5000));
2182
2183 remove_wait_queue(&hdev->req_wait_q, &wait);
2184
2185 if (signal_pending(current)) {
2186 BT_ERR("%s: Firmware loading interrupted", hdev->name);
2187 err = -EINTR;
2188 goto done;
2189 }
2190
2191 if (!timeout) {
2192 BT_ERR("%s: Firmware loading timeout", hdev->name);
2193 err = -ETIMEDOUT;
2194 goto done;
2195 }
2196 }
2197
2198 if (test_bit(BTUSB_FIRMWARE_FAILED, &data->flags)) {
2199 BT_ERR("%s: Firmware loading failed", hdev->name);
2200 err = -ENOEXEC;
2201 goto done;
2202 }
2203
2204 rettime = ktime_get();
2205 delta = ktime_sub(rettime, calltime);
2206 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
2207
2208 BT_INFO("%s: Firmware loaded in %llu usecs", hdev->name, duration);
2209
2210done:
2211 release_firmware(fw);
2212
2213 if (err < 0)
2214 return err;
2215
2216 calltime = ktime_get();
2217
2218 set_bit(BTUSB_BOOTING, &data->flags);
2219
2220 skb = __hci_cmd_sync(hdev, 0xfc01, sizeof(reset_param), reset_param,
2221 HCI_INIT_TIMEOUT);
2222 if (IS_ERR(skb))
2223 return PTR_ERR(skb);
2224
2225 kfree_skb(skb);
2226
2227 /* The bootloader will not indicate when the device is ready. This
2228 * is done by the operational firmware sending bootup notification.
2229 */
2230 if (test_bit(BTUSB_BOOTING, &data->flags)) {
2231 DECLARE_WAITQUEUE(wait, current);
2232 signed long timeout;
2233
2234 BT_INFO("%s: Waiting for device to boot", hdev->name);
2235
2236 add_wait_queue(&hdev->req_wait_q, &wait);
2237 set_current_state(TASK_INTERRUPTIBLE);
2238
2239 /* Booting into operational firmware should not take
2240 * longer than 1 second. However if that happens, then
2241 * just fail the setup since something went wrong.
2242 */
2243 timeout = schedule_timeout(msecs_to_jiffies(1000));
2244
2245 remove_wait_queue(&hdev->req_wait_q, &wait);
2246
2247 if (signal_pending(current)) {
2248 BT_ERR("%s: Device boot interrupted", hdev->name);
2249 return -EINTR;
2250 }
2251
2252 if (!timeout) {
2253 BT_ERR("%s: Device boot timeout", hdev->name);
2254 return -ETIMEDOUT;
2255 }
2256 }
2257
2258 rettime = ktime_get();
2259 delta = ktime_sub(rettime, calltime);
2260 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
2261
2262 BT_INFO("%s: Device booted in %llu usecs", hdev->name, duration);
2263
2264 clear_bit(BTUSB_BOOTLOADER, &data->flags);
2265
2266 return 0;
2267}
2268
Marcel Holtmann385a7682015-01-28 11:09:56 -08002269static void btusb_hw_error_intel(struct hci_dev *hdev, u8 code)
2270{
2271 struct sk_buff *skb;
2272 u8 type = 0x00;
2273
2274 BT_ERR("%s: Hardware error 0x%2.2x", hdev->name, code);
2275
2276 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
2277 if (IS_ERR(skb)) {
2278 BT_ERR("%s: Reset after hardware error failed (%ld)",
2279 hdev->name, PTR_ERR(skb));
2280 return;
2281 }
2282 kfree_skb(skb);
2283
2284 skb = __hci_cmd_sync(hdev, 0xfc22, 1, &type, HCI_INIT_TIMEOUT);
2285 if (IS_ERR(skb)) {
2286 BT_ERR("%s: Retrieving Intel exception info failed (%ld)",
2287 hdev->name, PTR_ERR(skb));
2288 return;
2289 }
2290
2291 if (skb->len != 13) {
2292 BT_ERR("%s: Exception info size mismatch", hdev->name);
2293 kfree_skb(skb);
2294 return;
2295 }
2296
2297 if (skb->data[0] != 0x00) {
2298 BT_ERR("%s: Exception info command failure (%02x)",
2299 hdev->name, skb->data[0]);
2300 kfree_skb(skb);
2301 return;
2302 }
2303
2304 BT_ERR("%s: Exception info %s", hdev->name, (char *)(skb->data + 1));
2305
2306 kfree_skb(skb);
2307}
2308
Marcel Holtmanncb8d6592014-07-02 11:25:25 +02002309static int btusb_set_bdaddr_intel(struct hci_dev *hdev, const bdaddr_t *bdaddr)
2310{
2311 struct sk_buff *skb;
2312 long ret;
2313
2314 skb = __hci_cmd_sync(hdev, 0xfc31, 6, bdaddr, HCI_INIT_TIMEOUT);
2315 if (IS_ERR(skb)) {
2316 ret = PTR_ERR(skb);
2317 BT_ERR("%s: changing Intel device address failed (%ld)",
Marcel Holtmann89e75332014-09-16 04:44:50 +02002318 hdev->name, ret);
Marcel Holtmanncb8d6592014-07-02 11:25:25 +02002319 return ret;
2320 }
2321 kfree_skb(skb);
2322
2323 return 0;
2324}
2325
Amitkumar Karwarae8df492014-07-18 14:47:06 -07002326static int btusb_set_bdaddr_marvell(struct hci_dev *hdev,
2327 const bdaddr_t *bdaddr)
2328{
2329 struct sk_buff *skb;
2330 u8 buf[8];
2331 long ret;
2332
2333 buf[0] = 0xfe;
2334 buf[1] = sizeof(bdaddr_t);
2335 memcpy(buf + 2, bdaddr, sizeof(bdaddr_t));
2336
2337 skb = __hci_cmd_sync(hdev, 0xfc22, sizeof(buf), buf, HCI_INIT_TIMEOUT);
2338 if (IS_ERR(skb)) {
2339 ret = PTR_ERR(skb);
2340 BT_ERR("%s: changing Marvell device address failed (%ld)",
2341 hdev->name, ret);
2342 return ret;
2343 }
2344 kfree_skb(skb);
2345
2346 return 0;
2347}
2348
Marcel Holtmannc8abb732014-07-02 12:38:22 +02002349#define BDADDR_BCM20702A0 (&(bdaddr_t) {{0x00, 0xa0, 0x02, 0x70, 0x20, 0x00}})
2350
Petri Gynther10d4c672014-05-08 15:50:01 -07002351static int btusb_setup_bcm_patchram(struct hci_dev *hdev)
2352{
2353 struct btusb_data *data = hci_get_drvdata(hdev);
2354 struct usb_device *udev = data->udev;
2355 char fw_name[64];
2356 const struct firmware *fw;
2357 const u8 *fw_ptr;
2358 size_t fw_size;
2359 const struct hci_command_hdr *cmd;
2360 const u8 *cmd_param;
2361 u16 opcode;
2362 struct sk_buff *skb;
2363 struct hci_rp_read_local_version *ver;
Marcel Holtmannc8abb732014-07-02 12:38:22 +02002364 struct hci_rp_read_bd_addr *bda;
Petri Gynther10d4c672014-05-08 15:50:01 -07002365 long ret;
2366
2367 snprintf(fw_name, sizeof(fw_name), "brcm/%s-%04x-%04x.hcd",
2368 udev->product ? udev->product : "BCM",
2369 le16_to_cpu(udev->descriptor.idVendor),
2370 le16_to_cpu(udev->descriptor.idProduct));
2371
2372 ret = request_firmware(&fw, fw_name, &hdev->dev);
2373 if (ret < 0) {
Marcel Holtmannc8abb732014-07-02 12:38:22 +02002374 BT_INFO("%s: BCM: patch %s not found", hdev->name, fw_name);
Petri Gynther10d4c672014-05-08 15:50:01 -07002375 return 0;
2376 }
2377
2378 /* Reset */
2379 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
2380 if (IS_ERR(skb)) {
2381 ret = PTR_ERR(skb);
2382 BT_ERR("%s: HCI_OP_RESET failed (%ld)", hdev->name, ret);
2383 goto done;
2384 }
2385 kfree_skb(skb);
2386
2387 /* Read Local Version Info */
2388 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
2389 HCI_INIT_TIMEOUT);
2390 if (IS_ERR(skb)) {
2391 ret = PTR_ERR(skb);
2392 BT_ERR("%s: HCI_OP_READ_LOCAL_VERSION failed (%ld)",
Marcel Holtmann89e75332014-09-16 04:44:50 +02002393 hdev->name, ret);
Petri Gynther10d4c672014-05-08 15:50:01 -07002394 goto done;
2395 }
2396
2397 if (skb->len != sizeof(*ver)) {
2398 BT_ERR("%s: HCI_OP_READ_LOCAL_VERSION event length mismatch",
Marcel Holtmann89e75332014-09-16 04:44:50 +02002399 hdev->name);
Petri Gynther10d4c672014-05-08 15:50:01 -07002400 kfree_skb(skb);
2401 ret = -EIO;
2402 goto done;
2403 }
2404
Marcel Holtmann89e75332014-09-16 04:44:50 +02002405 ver = (struct hci_rp_read_local_version *)skb->data;
Petri Gynther10d4c672014-05-08 15:50:01 -07002406 BT_INFO("%s: BCM: patching hci_ver=%02x hci_rev=%04x lmp_ver=%02x "
2407 "lmp_subver=%04x", hdev->name, ver->hci_ver, ver->hci_rev,
2408 ver->lmp_ver, ver->lmp_subver);
2409 kfree_skb(skb);
2410
2411 /* Start Download */
2412 skb = __hci_cmd_sync(hdev, 0xfc2e, 0, NULL, HCI_INIT_TIMEOUT);
2413 if (IS_ERR(skb)) {
2414 ret = PTR_ERR(skb);
2415 BT_ERR("%s: BCM: Download Minidrv command failed (%ld)",
Marcel Holtmann89e75332014-09-16 04:44:50 +02002416 hdev->name, ret);
Petri Gynther10d4c672014-05-08 15:50:01 -07002417 goto reset_fw;
2418 }
2419 kfree_skb(skb);
2420
2421 /* 50 msec delay after Download Minidrv completes */
2422 msleep(50);
2423
2424 fw_ptr = fw->data;
2425 fw_size = fw->size;
2426
2427 while (fw_size >= sizeof(*cmd)) {
Marcel Holtmann89e75332014-09-16 04:44:50 +02002428 cmd = (struct hci_command_hdr *)fw_ptr;
Petri Gynther10d4c672014-05-08 15:50:01 -07002429 fw_ptr += sizeof(*cmd);
2430 fw_size -= sizeof(*cmd);
2431
2432 if (fw_size < cmd->plen) {
2433 BT_ERR("%s: BCM: patch %s is corrupted",
Marcel Holtmann89e75332014-09-16 04:44:50 +02002434 hdev->name, fw_name);
Petri Gynther10d4c672014-05-08 15:50:01 -07002435 ret = -EINVAL;
2436 goto reset_fw;
2437 }
2438
2439 cmd_param = fw_ptr;
2440 fw_ptr += cmd->plen;
2441 fw_size -= cmd->plen;
2442
2443 opcode = le16_to_cpu(cmd->opcode);
2444
2445 skb = __hci_cmd_sync(hdev, opcode, cmd->plen, cmd_param,
2446 HCI_INIT_TIMEOUT);
2447 if (IS_ERR(skb)) {
2448 ret = PTR_ERR(skb);
2449 BT_ERR("%s: BCM: patch command %04x failed (%ld)",
Marcel Holtmann89e75332014-09-16 04:44:50 +02002450 hdev->name, opcode, ret);
Petri Gynther10d4c672014-05-08 15:50:01 -07002451 goto reset_fw;
2452 }
2453 kfree_skb(skb);
2454 }
2455
2456 /* 250 msec delay after Launch Ram completes */
2457 msleep(250);
2458
2459reset_fw:
2460 /* Reset */
2461 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
2462 if (IS_ERR(skb)) {
2463 ret = PTR_ERR(skb);
2464 BT_ERR("%s: HCI_OP_RESET failed (%ld)", hdev->name, ret);
2465 goto done;
2466 }
2467 kfree_skb(skb);
2468
2469 /* Read Local Version Info */
2470 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
2471 HCI_INIT_TIMEOUT);
2472 if (IS_ERR(skb)) {
2473 ret = PTR_ERR(skb);
2474 BT_ERR("%s: HCI_OP_READ_LOCAL_VERSION failed (%ld)",
Marcel Holtmann89e75332014-09-16 04:44:50 +02002475 hdev->name, ret);
Petri Gynther10d4c672014-05-08 15:50:01 -07002476 goto done;
2477 }
2478
2479 if (skb->len != sizeof(*ver)) {
2480 BT_ERR("%s: HCI_OP_READ_LOCAL_VERSION event length mismatch",
Marcel Holtmann89e75332014-09-16 04:44:50 +02002481 hdev->name);
Petri Gynther10d4c672014-05-08 15:50:01 -07002482 kfree_skb(skb);
2483 ret = -EIO;
2484 goto done;
2485 }
2486
Marcel Holtmann89e75332014-09-16 04:44:50 +02002487 ver = (struct hci_rp_read_local_version *)skb->data;
Petri Gynther10d4c672014-05-08 15:50:01 -07002488 BT_INFO("%s: BCM: firmware hci_ver=%02x hci_rev=%04x lmp_ver=%02x "
2489 "lmp_subver=%04x", hdev->name, ver->hci_ver, ver->hci_rev,
2490 ver->lmp_ver, ver->lmp_subver);
2491 kfree_skb(skb);
2492
Marcel Holtmannc8abb732014-07-02 12:38:22 +02002493 /* Read BD Address */
2494 skb = __hci_cmd_sync(hdev, HCI_OP_READ_BD_ADDR, 0, NULL,
2495 HCI_INIT_TIMEOUT);
2496 if (IS_ERR(skb)) {
2497 ret = PTR_ERR(skb);
2498 BT_ERR("%s: HCI_OP_READ_BD_ADDR failed (%ld)",
Marcel Holtmann89e75332014-09-16 04:44:50 +02002499 hdev->name, ret);
Marcel Holtmannc8abb732014-07-02 12:38:22 +02002500 goto done;
2501 }
2502
2503 if (skb->len != sizeof(*bda)) {
2504 BT_ERR("%s: HCI_OP_READ_BD_ADDR event length mismatch",
Marcel Holtmann89e75332014-09-16 04:44:50 +02002505 hdev->name);
Marcel Holtmannc8abb732014-07-02 12:38:22 +02002506 kfree_skb(skb);
2507 ret = -EIO;
2508 goto done;
2509 }
2510
Marcel Holtmann89e75332014-09-16 04:44:50 +02002511 bda = (struct hci_rp_read_bd_addr *)skb->data;
Marcel Holtmannc8abb732014-07-02 12:38:22 +02002512 if (bda->status) {
2513 BT_ERR("%s: HCI_OP_READ_BD_ADDR error status (%02x)",
2514 hdev->name, bda->status);
2515 kfree_skb(skb);
2516 ret = -bt_to_errno(bda->status);
2517 goto done;
2518 }
2519
2520 /* The address 00:20:70:02:A0:00 indicates a BCM20702A0 controller
2521 * with no configured address.
2522 */
Marcel Holtmann849e5082014-07-04 16:54:39 +02002523 if (!bacmp(&bda->bdaddr, BDADDR_BCM20702A0)) {
Marcel Holtmannc8abb732014-07-02 12:38:22 +02002524 BT_INFO("%s: BCM: using default device address (%pMR)",
2525 hdev->name, &bda->bdaddr);
Marcel Holtmann849e5082014-07-04 16:54:39 +02002526 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
2527 }
Marcel Holtmannc8abb732014-07-02 12:38:22 +02002528
2529 kfree_skb(skb);
2530
Petri Gynther10d4c672014-05-08 15:50:01 -07002531done:
2532 release_firmware(fw);
2533
2534 return ret;
2535}
2536
Marcel Holtmannabbaf502014-07-02 00:53:48 +02002537static int btusb_set_bdaddr_bcm(struct hci_dev *hdev, const bdaddr_t *bdaddr)
2538{
2539 struct sk_buff *skb;
2540 long ret;
2541
2542 skb = __hci_cmd_sync(hdev, 0xfc01, 6, bdaddr, HCI_INIT_TIMEOUT);
2543 if (IS_ERR(skb)) {
2544 ret = PTR_ERR(skb);
2545 BT_ERR("%s: BCM: Change address command failed (%ld)",
Marcel Holtmann89e75332014-09-16 04:44:50 +02002546 hdev->name, ret);
Marcel Holtmannabbaf502014-07-02 00:53:48 +02002547 return ret;
2548 }
2549 kfree_skb(skb);
2550
2551 return 0;
2552}
2553
Toshi Kikuchi58592232014-12-12 10:58:05 -08002554static int btusb_set_bdaddr_ath3012(struct hci_dev *hdev,
2555 const bdaddr_t *bdaddr)
2556{
2557 struct sk_buff *skb;
2558 u8 buf[10];
2559 long ret;
2560
2561 buf[0] = 0x01;
2562 buf[1] = 0x01;
2563 buf[2] = 0x00;
2564 buf[3] = sizeof(bdaddr_t);
2565 memcpy(buf + 4, bdaddr, sizeof(bdaddr_t));
2566
2567 skb = __hci_cmd_sync(hdev, 0xfc0b, sizeof(buf), buf, HCI_INIT_TIMEOUT);
2568 if (IS_ERR(skb)) {
2569 ret = PTR_ERR(skb);
2570 BT_ERR("%s: Change address command failed (%ld)",
2571 hdev->name, ret);
2572 return ret;
2573 }
2574 kfree_skb(skb);
2575
2576 return 0;
2577}
2578
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002579static int btusb_probe(struct usb_interface *intf,
Marcel Holtmann89e75332014-09-16 04:44:50 +02002580 const struct usb_device_id *id)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002581{
2582 struct usb_endpoint_descriptor *ep_desc;
2583 struct btusb_data *data;
2584 struct hci_dev *hdev;
2585 int i, err;
2586
2587 BT_DBG("intf %p id %p", intf, id);
2588
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002589 /* interface numbers are hardcoded in the spec */
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002590 if (intf->cur_altsetting->desc.bInterfaceNumber != 0)
2591 return -ENODEV;
2592
2593 if (!id->driver_info) {
2594 const struct usb_device_id *match;
Marcel Holtmann89e75332014-09-16 04:44:50 +02002595
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002596 match = usb_match_id(intf, blacklist_table);
2597 if (match)
2598 id = match;
2599 }
2600
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002601 if (id->driver_info == BTUSB_IGNORE)
2602 return -ENODEV;
2603
Steven.Li2d25f8b2011-07-01 14:02:36 +08002604 if (id->driver_info & BTUSB_ATH3012) {
2605 struct usb_device *udev = interface_to_usbdev(intf);
2606
2607 /* Old firmware would otherwise let ath3k driver load
2608 * patch and sysconfig files */
2609 if (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x0001)
2610 return -ENODEV;
2611 }
2612
Sachin Kamat98921db2012-07-27 12:38:39 +05302613 data = devm_kzalloc(&intf->dev, sizeof(*data), GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002614 if (!data)
2615 return -ENOMEM;
2616
2617 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
2618 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
2619
2620 if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
2621 data->intr_ep = ep_desc;
2622 continue;
2623 }
2624
2625 if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
2626 data->bulk_tx_ep = ep_desc;
2627 continue;
2628 }
2629
2630 if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
2631 data->bulk_rx_ep = ep_desc;
2632 continue;
2633 }
2634 }
2635
Sachin Kamat98921db2012-07-27 12:38:39 +05302636 if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002637 return -ENODEV;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002638
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002639 data->cmdreq_type = USB_TYPE_CLASS;
2640
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002641 data->udev = interface_to_usbdev(intf);
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02002642 data->intf = intf;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002643
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002644 INIT_WORK(&data->work, btusb_work);
Oliver Neukum7bee5492009-08-24 23:44:59 +02002645 INIT_WORK(&data->waker, btusb_waker);
Marcel Holtmann803b5832014-09-16 08:00:29 +02002646 init_usb_anchor(&data->deferred);
2647 init_usb_anchor(&data->tx_anchor);
Oliver Neukum7bee5492009-08-24 23:44:59 +02002648 spin_lock_init(&data->txlock);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002649
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002650 init_usb_anchor(&data->intr_anchor);
2651 init_usb_anchor(&data->bulk_anchor);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02002652 init_usb_anchor(&data->isoc_anchor);
Marcel Holtmann803b5832014-09-16 08:00:29 +02002653 spin_lock_init(&data->rxlock);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002654
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002655 if (id->driver_info & BTUSB_INTEL_NEW) {
2656 data->recv_event = btusb_recv_event_intel;
2657 data->recv_bulk = btusb_recv_bulk_intel;
2658 set_bit(BTUSB_BOOTLOADER, &data->flags);
2659 } else {
2660 data->recv_event = hci_recv_frame;
2661 data->recv_bulk = btusb_recv_bulk;
2662 }
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +01002663
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002664 hdev = hci_alloc_dev();
Sachin Kamat98921db2012-07-27 12:38:39 +05302665 if (!hdev)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002666 return -ENOMEM;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002667
Marcel Holtmannc13854c2010-02-08 15:27:07 +01002668 hdev->bus = HCI_USB;
David Herrmann155961e2012-02-09 21:58:32 +01002669 hci_set_drvdata(hdev, data);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002670
2671 data->hdev = hdev;
2672
2673 SET_HCIDEV_DEV(hdev, &intf->dev);
2674
Marcel Holtmann9f8f9622013-04-10 08:11:35 -07002675 hdev->open = btusb_open;
2676 hdev->close = btusb_close;
2677 hdev->flush = btusb_flush;
2678 hdev->send = btusb_send_frame;
2679 hdev->notify = btusb_notify;
2680
2681 if (id->driver_info & BTUSB_BCM92035)
2682 hdev->setup = btusb_setup_bcm92035;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002683
Marcel Holtmannabbaf502014-07-02 00:53:48 +02002684 if (id->driver_info & BTUSB_BCM_PATCHRAM) {
Petri Gynther10d4c672014-05-08 15:50:01 -07002685 hdev->setup = btusb_setup_bcm_patchram;
Marcel Holtmannabbaf502014-07-02 00:53:48 +02002686 hdev->set_bdaddr = btusb_set_bdaddr_bcm;
Marcel Holtmann27c3fbe2014-11-02 20:52:24 +01002687 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
Marcel Holtmannabbaf502014-07-02 00:53:48 +02002688 }
Petri Gynther10d4c672014-05-08 15:50:01 -07002689
Marcel Holtmanncb8d6592014-07-02 11:25:25 +02002690 if (id->driver_info & BTUSB_INTEL) {
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002691 hdev->setup = btusb_setup_intel;
Marcel Holtmann385a7682015-01-28 11:09:56 -08002692 hdev->hw_error = btusb_hw_error_intel;
Marcel Holtmanncb8d6592014-07-02 11:25:25 +02002693 hdev->set_bdaddr = btusb_set_bdaddr_intel;
2694 }
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002695
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002696 if (id->driver_info & BTUSB_INTEL_NEW) {
2697 hdev->send = btusb_send_frame_intel;
2698 hdev->setup = btusb_setup_intel_new;
Marcel Holtmann385a7682015-01-28 11:09:56 -08002699 hdev->hw_error = btusb_hw_error_intel;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002700 hdev->set_bdaddr = btusb_set_bdaddr_intel;
2701 }
2702
Amitkumar Karwarae8df492014-07-18 14:47:06 -07002703 if (id->driver_info & BTUSB_MARVELL)
2704 hdev->set_bdaddr = btusb_set_bdaddr_marvell;
2705
Marcel Holtmann661cf882015-01-02 23:35:20 -08002706 if (id->driver_info & BTUSB_SWAVE) {
2707 set_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks);
Marcel Holtmannd57dbe72014-12-26 04:42:33 +01002708 set_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS, &hdev->quirks);
Marcel Holtmann661cf882015-01-02 23:35:20 -08002709 }
Marcel Holtmannd57dbe72014-12-26 04:42:33 +01002710
Marcel Holtmann40df7832014-07-06 13:29:58 +02002711 if (id->driver_info & BTUSB_INTEL_BOOT)
2712 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
2713
Toshi Kikuchi58592232014-12-12 10:58:05 -08002714 if (id->driver_info & BTUSB_ATH3012)
2715 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
2716
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002717 /* Interface numbers are hardcoded in the specification */
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02002718 data->isoc = usb_ifnum_to_if(data->udev, 1);
2719
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002720 if (!reset)
Szymon Janca6c511c2012-05-23 12:35:46 +02002721 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002722
2723 if (force_scofix || id->driver_info & BTUSB_WRONG_SCO_MTU) {
2724 if (!disable_scofix)
2725 set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
2726 }
2727
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02002728 if (id->driver_info & BTUSB_BROKEN_ISOC)
2729 data->isoc = NULL;
2730
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002731 if (id->driver_info & BTUSB_DIGIANSWER) {
2732 data->cmdreq_type = USB_TYPE_VENDOR;
Szymon Janca6c511c2012-05-23 12:35:46 +02002733 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002734 }
2735
2736 if (id->driver_info & BTUSB_CSR) {
2737 struct usb_device *udev = data->udev;
Marcel Holtmann81cac642014-01-03 03:02:36 -08002738 u16 bcdDevice = le16_to_cpu(udev->descriptor.bcdDevice);
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002739
2740 /* Old firmware would otherwise execute USB reset */
Marcel Holtmann81cac642014-01-03 03:02:36 -08002741 if (bcdDevice < 0x117)
Szymon Janca6c511c2012-05-23 12:35:46 +02002742 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmann81cac642014-01-03 03:02:36 -08002743
2744 /* Fake CSR devices with broken commands */
2745 if (bcdDevice <= 0x100)
2746 hdev->setup = btusb_setup_csr;
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002747 }
2748
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002749 if (id->driver_info & BTUSB_SNIFFER) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02002750 struct usb_device *udev = data->udev;
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002751
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002752 /* New sniffer firmware has crippled HCI interface */
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002753 if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
2754 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
2755 }
2756
Marcel Holtmann3a5ef202014-07-06 14:53:54 +02002757 if (id->driver_info & BTUSB_INTEL_BOOT) {
2758 /* A bug in the bootloader causes that interrupt interface is
2759 * only enabled after receiving SetInterface(0, AltSetting=0).
2760 */
2761 err = usb_set_interface(data->udev, 0, 0);
2762 if (err < 0) {
2763 BT_ERR("failed to set interface 0, alt 0 %d", err);
2764 hci_free_dev(hdev);
2765 return err;
2766 }
2767 }
2768
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02002769 if (data->isoc) {
2770 err = usb_driver_claim_interface(&btusb_driver,
Marcel Holtmann89e75332014-09-16 04:44:50 +02002771 data->isoc, data);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02002772 if (err < 0) {
2773 hci_free_dev(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02002774 return err;
2775 }
2776 }
2777
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002778 err = hci_register_dev(hdev);
2779 if (err < 0) {
2780 hci_free_dev(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002781 return err;
2782 }
2783
2784 usb_set_intfdata(intf, data);
2785
2786 return 0;
2787}
2788
2789static void btusb_disconnect(struct usb_interface *intf)
2790{
2791 struct btusb_data *data = usb_get_intfdata(intf);
2792 struct hci_dev *hdev;
2793
2794 BT_DBG("intf %p", intf);
2795
2796 if (!data)
2797 return;
2798
2799 hdev = data->hdev;
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02002800 usb_set_intfdata(data->intf, NULL);
2801
2802 if (data->isoc)
2803 usb_set_intfdata(data->isoc, NULL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002804
2805 hci_unregister_dev(hdev);
2806
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02002807 if (intf == data->isoc)
2808 usb_driver_release_interface(&btusb_driver, data->intf);
2809 else if (data->isoc)
2810 usb_driver_release_interface(&btusb_driver, data->isoc);
2811
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002812 hci_free_dev(hdev);
2813}
2814
Oliver Neukum7bee5492009-08-24 23:44:59 +02002815#ifdef CONFIG_PM
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002816static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
2817{
2818 struct btusb_data *data = usb_get_intfdata(intf);
2819
2820 BT_DBG("intf %p", intf);
2821
2822 if (data->suspend_count++)
2823 return 0;
2824
Oliver Neukum7bee5492009-08-24 23:44:59 +02002825 spin_lock_irq(&data->txlock);
Alan Stern5b1b0b82011-08-19 23:49:48 +02002826 if (!(PMSG_IS_AUTO(message) && data->tx_in_flight)) {
Oliver Neukum7bee5492009-08-24 23:44:59 +02002827 set_bit(BTUSB_SUSPENDING, &data->flags);
2828 spin_unlock_irq(&data->txlock);
2829 } else {
2830 spin_unlock_irq(&data->txlock);
2831 data->suspend_count--;
2832 return -EBUSY;
2833 }
2834
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002835 cancel_work_sync(&data->work);
2836
Oliver Neukum7bee5492009-08-24 23:44:59 +02002837 btusb_stop_traffic(data);
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002838 usb_kill_anchored_urbs(&data->tx_anchor);
2839
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002840 return 0;
2841}
2842
Oliver Neukum7bee5492009-08-24 23:44:59 +02002843static void play_deferred(struct btusb_data *data)
2844{
2845 struct urb *urb;
2846 int err;
2847
2848 while ((urb = usb_get_from_anchor(&data->deferred))) {
2849 err = usb_submit_urb(urb, GFP_ATOMIC);
2850 if (err < 0)
2851 break;
2852
2853 data->tx_in_flight++;
2854 }
2855 usb_scuttle_anchored_urbs(&data->deferred);
2856}
2857
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002858static int btusb_resume(struct usb_interface *intf)
2859{
2860 struct btusb_data *data = usb_get_intfdata(intf);
2861 struct hci_dev *hdev = data->hdev;
Oliver Neukum7bee5492009-08-24 23:44:59 +02002862 int err = 0;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002863
2864 BT_DBG("intf %p", intf);
2865
2866 if (--data->suspend_count)
2867 return 0;
2868
2869 if (!test_bit(HCI_RUNNING, &hdev->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +02002870 goto done;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002871
2872 if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
2873 err = btusb_submit_intr_urb(hdev, GFP_NOIO);
2874 if (err < 0) {
2875 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02002876 goto failed;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002877 }
2878 }
2879
2880 if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) {
Marcel Holtmann43c2e572009-02-04 17:41:38 +01002881 err = btusb_submit_bulk_urb(hdev, GFP_NOIO);
2882 if (err < 0) {
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002883 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02002884 goto failed;
2885 }
2886
2887 btusb_submit_bulk_urb(hdev, GFP_NOIO);
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002888 }
2889
2890 if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
2891 if (btusb_submit_isoc_urb(hdev, GFP_NOIO) < 0)
2892 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
2893 else
2894 btusb_submit_isoc_urb(hdev, GFP_NOIO);
2895 }
2896
Oliver Neukum7bee5492009-08-24 23:44:59 +02002897 spin_lock_irq(&data->txlock);
2898 play_deferred(data);
2899 clear_bit(BTUSB_SUSPENDING, &data->flags);
2900 spin_unlock_irq(&data->txlock);
2901 schedule_work(&data->work);
2902
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002903 return 0;
Oliver Neukum7bee5492009-08-24 23:44:59 +02002904
2905failed:
2906 usb_scuttle_anchored_urbs(&data->deferred);
2907done:
2908 spin_lock_irq(&data->txlock);
2909 clear_bit(BTUSB_SUSPENDING, &data->flags);
2910 spin_unlock_irq(&data->txlock);
2911
2912 return err;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002913}
Oliver Neukum7bee5492009-08-24 23:44:59 +02002914#endif
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002915
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002916static struct usb_driver btusb_driver = {
2917 .name = "btusb",
2918 .probe = btusb_probe,
2919 .disconnect = btusb_disconnect,
Oliver Neukum7bee5492009-08-24 23:44:59 +02002920#ifdef CONFIG_PM
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002921 .suspend = btusb_suspend,
2922 .resume = btusb_resume,
Oliver Neukum7bee5492009-08-24 23:44:59 +02002923#endif
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002924 .id_table = btusb_table,
Oliver Neukum7bee5492009-08-24 23:44:59 +02002925 .supports_autosuspend = 1,
Sarah Sharpe1f12eb2012-04-23 10:08:51 -07002926 .disable_hub_initiated_lpm = 1,
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002927};
2928
Greg Kroah-Hartman93f15082011-11-18 09:47:34 -08002929module_usb_driver(btusb_driver);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002930
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002931module_param(disable_scofix, bool, 0644);
2932MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
2933
2934module_param(force_scofix, bool, 0644);
2935MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
2936
2937module_param(reset, bool, 0644);
2938MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
2939
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002940MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
2941MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
2942MODULE_VERSION(VERSION);
2943MODULE_LICENSE("GPL");