blob: 4f5b93568eccc71496e1a4aa1efd23e5d10017a3 [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
Marcel Holtmanncb1ee892015-01-28 19:41:42 -0800262 /* Marvell Bluetooth devices */
263 { USB_DEVICE(0x1286, 0x2044), .driver_info = BTUSB_MARVELL },
264 { USB_DEVICE(0x1286, 0x2046), .driver_info = BTUSB_MARVELL },
265
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -0700266 /* Intel Bluetooth device */
267 { USB_DEVICE(0x8087, 0x07dc), .driver_info = BTUSB_INTEL },
Tedd Ho-Jeong Anef4e5e42013-11-12 13:10:58 -0800268 { USB_DEVICE(0x8087, 0x0a2a), .driver_info = BTUSB_INTEL },
Marcel Holtmanncda0dd72015-01-26 21:33:48 -0800269 { USB_DEVICE(0x8087, 0x0a2b), .driver_info = BTUSB_INTEL_NEW },
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -0700270
Amitkumar Karwarae8df492014-07-18 14:47:06 -0700271
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200272 { } /* Terminating entry */
273};
274
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200275#define BTUSB_MAX_ISOC_FRAMES 10
276
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200277#define BTUSB_INTR_RUNNING 0
278#define BTUSB_BULK_RUNNING 1
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200279#define BTUSB_ISOC_RUNNING 2
Oliver Neukum7bee5492009-08-24 23:44:59 +0200280#define BTUSB_SUSPENDING 3
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -0300281#define BTUSB_DID_ISO_RESUME 4
Marcel Holtmanncda0dd72015-01-26 21:33:48 -0800282#define BTUSB_BOOTLOADER 5
283#define BTUSB_DOWNLOADING 6
Marcel Holtmannce6bb922015-01-28 01:58:40 -0800284#define BTUSB_FIRMWARE_LOADED 7
Marcel Holtmanncda0dd72015-01-26 21:33:48 -0800285#define BTUSB_FIRMWARE_FAILED 8
Marcel Holtmannce6bb922015-01-28 01:58:40 -0800286#define BTUSB_BOOTING 9
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200287
288struct btusb_data {
289 struct hci_dev *hdev;
290 struct usb_device *udev;
Marcel Holtmann5fbcd262008-09-23 00:16:36 +0200291 struct usb_interface *intf;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200292 struct usb_interface *isoc;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200293
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200294 unsigned long flags;
295
296 struct work_struct work;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200297 struct work_struct waker;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200298
Marcel Holtmann803b5832014-09-16 08:00:29 +0200299 struct usb_anchor deferred;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200300 struct usb_anchor tx_anchor;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200301 int tx_in_flight;
302 spinlock_t txlock;
303
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200304 struct usb_anchor intr_anchor;
305 struct usb_anchor bulk_anchor;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200306 struct usb_anchor isoc_anchor;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200307 spinlock_t rxlock;
308
309 struct sk_buff *evt_skb;
310 struct sk_buff *acl_skb;
311 struct sk_buff *sco_skb;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200312
313 struct usb_endpoint_descriptor *intr_ep;
314 struct usb_endpoint_descriptor *bulk_tx_ep;
315 struct usb_endpoint_descriptor *bulk_rx_ep;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200316 struct usb_endpoint_descriptor *isoc_tx_ep;
317 struct usb_endpoint_descriptor *isoc_rx_ep;
318
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100319 __u8 cmdreq_type;
320
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100321 unsigned int sco_num;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200322 int isoc_altsetting;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +0100323 int suspend_count;
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +0100324
Marcel Holtmann97307f52015-01-12 13:51:10 -0800325 int (*recv_event)(struct hci_dev *hdev, struct sk_buff *skb);
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +0100326 int (*recv_bulk)(struct btusb_data *data, void *buffer, int count);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200327};
328
Marcel Holtmann803b5832014-09-16 08:00:29 +0200329static inline void btusb_free_frags(struct btusb_data *data)
330{
331 unsigned long flags;
332
333 spin_lock_irqsave(&data->rxlock, flags);
334
335 kfree_skb(data->evt_skb);
336 data->evt_skb = NULL;
337
338 kfree_skb(data->acl_skb);
339 data->acl_skb = NULL;
340
341 kfree_skb(data->sco_skb);
342 data->sco_skb = NULL;
343
344 spin_unlock_irqrestore(&data->rxlock, flags);
345}
346
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200347static int btusb_recv_intr(struct btusb_data *data, void *buffer, int count)
348{
Marcel Holtmann803b5832014-09-16 08:00:29 +0200349 struct sk_buff *skb;
350 int err = 0;
351
352 spin_lock(&data->rxlock);
353 skb = data->evt_skb;
354
355 while (count) {
356 int len;
357
358 if (!skb) {
359 skb = bt_skb_alloc(HCI_MAX_EVENT_SIZE, GFP_ATOMIC);
360 if (!skb) {
361 err = -ENOMEM;
362 break;
363 }
364
365 bt_cb(skb)->pkt_type = HCI_EVENT_PKT;
366 bt_cb(skb)->expect = HCI_EVENT_HDR_SIZE;
367 }
368
369 len = min_t(uint, bt_cb(skb)->expect, count);
370 memcpy(skb_put(skb, len), buffer, len);
371
372 count -= len;
373 buffer += len;
374 bt_cb(skb)->expect -= len;
375
376 if (skb->len == HCI_EVENT_HDR_SIZE) {
377 /* Complete event header */
378 bt_cb(skb)->expect = hci_event_hdr(skb)->plen;
379
380 if (skb_tailroom(skb) < bt_cb(skb)->expect) {
381 kfree_skb(skb);
382 skb = NULL;
383
384 err = -EILSEQ;
385 break;
386 }
387 }
388
389 if (bt_cb(skb)->expect == 0) {
390 /* Complete frame */
Marcel Holtmann97307f52015-01-12 13:51:10 -0800391 data->recv_event(data->hdev, skb);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200392 skb = NULL;
393 }
394 }
395
396 data->evt_skb = skb;
397 spin_unlock(&data->rxlock);
398
399 return err;
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200400}
401
402static int btusb_recv_bulk(struct btusb_data *data, void *buffer, int count)
403{
Marcel Holtmann803b5832014-09-16 08:00:29 +0200404 struct sk_buff *skb;
405 int err = 0;
406
407 spin_lock(&data->rxlock);
408 skb = data->acl_skb;
409
410 while (count) {
411 int len;
412
413 if (!skb) {
414 skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
415 if (!skb) {
416 err = -ENOMEM;
417 break;
418 }
419
420 bt_cb(skb)->pkt_type = HCI_ACLDATA_PKT;
421 bt_cb(skb)->expect = HCI_ACL_HDR_SIZE;
422 }
423
424 len = min_t(uint, bt_cb(skb)->expect, count);
425 memcpy(skb_put(skb, len), buffer, len);
426
427 count -= len;
428 buffer += len;
429 bt_cb(skb)->expect -= len;
430
431 if (skb->len == HCI_ACL_HDR_SIZE) {
432 __le16 dlen = hci_acl_hdr(skb)->dlen;
433
434 /* Complete ACL header */
435 bt_cb(skb)->expect = __le16_to_cpu(dlen);
436
437 if (skb_tailroom(skb) < bt_cb(skb)->expect) {
438 kfree_skb(skb);
439 skb = NULL;
440
441 err = -EILSEQ;
442 break;
443 }
444 }
445
446 if (bt_cb(skb)->expect == 0) {
447 /* Complete frame */
448 hci_recv_frame(data->hdev, skb);
449 skb = NULL;
450 }
451 }
452
453 data->acl_skb = skb;
454 spin_unlock(&data->rxlock);
455
456 return err;
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200457}
458
459static int btusb_recv_isoc(struct btusb_data *data, void *buffer, int count)
460{
Marcel Holtmann803b5832014-09-16 08:00:29 +0200461 struct sk_buff *skb;
462 int err = 0;
463
464 spin_lock(&data->rxlock);
465 skb = data->sco_skb;
466
467 while (count) {
468 int len;
469
470 if (!skb) {
471 skb = bt_skb_alloc(HCI_MAX_SCO_SIZE, GFP_ATOMIC);
472 if (!skb) {
473 err = -ENOMEM;
474 break;
475 }
476
477 bt_cb(skb)->pkt_type = HCI_SCODATA_PKT;
478 bt_cb(skb)->expect = HCI_SCO_HDR_SIZE;
479 }
480
481 len = min_t(uint, bt_cb(skb)->expect, count);
482 memcpy(skb_put(skb, len), buffer, len);
483
484 count -= len;
485 buffer += len;
486 bt_cb(skb)->expect -= len;
487
488 if (skb->len == HCI_SCO_HDR_SIZE) {
489 /* Complete SCO header */
490 bt_cb(skb)->expect = hci_sco_hdr(skb)->dlen;
491
492 if (skb_tailroom(skb) < bt_cb(skb)->expect) {
493 kfree_skb(skb);
494 skb = NULL;
495
496 err = -EILSEQ;
497 break;
498 }
499 }
500
501 if (bt_cb(skb)->expect == 0) {
502 /* Complete frame */
503 hci_recv_frame(data->hdev, skb);
504 skb = NULL;
505 }
506 }
507
508 data->sco_skb = skb;
509 spin_unlock(&data->rxlock);
510
511 return err;
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200512}
513
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200514static void btusb_intr_complete(struct urb *urb)
515{
516 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +0100517 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200518 int err;
519
Marcel Holtmann89e75332014-09-16 04:44:50 +0200520 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
521 urb->actual_length);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200522
523 if (!test_bit(HCI_RUNNING, &hdev->flags))
524 return;
525
526 if (urb->status == 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200527 hdev->stat.byte_rx += urb->actual_length;
528
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200529 if (btusb_recv_intr(data, urb->transfer_buffer,
530 urb->actual_length) < 0) {
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200531 BT_ERR("%s corrupted event packet", hdev->name);
532 hdev->stat.err_rx++;
533 }
Champion Chen85560c42014-09-06 14:06:08 -0500534 } else if (urb->status == -ENOENT) {
535 /* Avoid suspend failed when usb_kill_urb */
536 return;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200537 }
538
539 if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
540 return;
541
Oliver Neukum7bee5492009-08-24 23:44:59 +0200542 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200543 usb_anchor_urb(urb, &data->intr_anchor);
544
545 err = usb_submit_urb(urb, GFP_ATOMIC);
546 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200547 /* -EPERM: urb is being killed;
548 * -ENODEV: device got disconnected */
549 if (err != -EPERM && err != -ENODEV)
Stefan Seyfried61faddf2010-11-30 21:49:08 +0100550 BT_ERR("%s urb %p failed to resubmit (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200551 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200552 usb_unanchor_urb(urb);
553 }
554}
555
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100556static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200557{
David Herrmann155961e2012-02-09 21:58:32 +0100558 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200559 struct urb *urb;
560 unsigned char *buf;
561 unsigned int pipe;
562 int err, size;
563
564 BT_DBG("%s", hdev->name);
565
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200566 if (!data->intr_ep)
567 return -ENODEV;
568
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100569 urb = usb_alloc_urb(0, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200570 if (!urb)
571 return -ENOMEM;
572
573 size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
574
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100575 buf = kmalloc(size, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200576 if (!buf) {
577 usb_free_urb(urb);
578 return -ENOMEM;
579 }
580
581 pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
582
583 usb_fill_int_urb(urb, data->udev, pipe, buf, size,
Marcel Holtmann89e75332014-09-16 04:44:50 +0200584 btusb_intr_complete, hdev, data->intr_ep->bInterval);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200585
586 urb->transfer_flags |= URB_FREE_BUFFER;
587
588 usb_anchor_urb(urb, &data->intr_anchor);
589
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100590 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200591 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +0200592 if (err != -EPERM && err != -ENODEV)
593 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200594 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200595 usb_unanchor_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200596 }
597
598 usb_free_urb(urb);
599
600 return err;
601}
602
603static void btusb_bulk_complete(struct urb *urb)
604{
605 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +0100606 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200607 int err;
608
Marcel Holtmann89e75332014-09-16 04:44:50 +0200609 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
610 urb->actual_length);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200611
612 if (!test_bit(HCI_RUNNING, &hdev->flags))
613 return;
614
615 if (urb->status == 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200616 hdev->stat.byte_rx += urb->actual_length;
617
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +0100618 if (data->recv_bulk(data, urb->transfer_buffer,
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200619 urb->actual_length) < 0) {
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200620 BT_ERR("%s corrupted ACL packet", hdev->name);
621 hdev->stat.err_rx++;
622 }
Champion Chen85560c42014-09-06 14:06:08 -0500623 } else if (urb->status == -ENOENT) {
624 /* Avoid suspend failed when usb_kill_urb */
625 return;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200626 }
627
628 if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
629 return;
630
631 usb_anchor_urb(urb, &data->bulk_anchor);
Oliver Neukum652fd782009-12-16 19:23:43 +0100632 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200633
634 err = usb_submit_urb(urb, GFP_ATOMIC);
635 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200636 /* -EPERM: urb is being killed;
637 * -ENODEV: device got disconnected */
638 if (err != -EPERM && err != -ENODEV)
Stefan Seyfried61faddf2010-11-30 21:49:08 +0100639 BT_ERR("%s urb %p failed to resubmit (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200640 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200641 usb_unanchor_urb(urb);
642 }
643}
644
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100645static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200646{
David Herrmann155961e2012-02-09 21:58:32 +0100647 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200648 struct urb *urb;
649 unsigned char *buf;
650 unsigned int pipe;
Vikram Kandukuri290ba202009-07-02 14:31:59 +0530651 int err, size = HCI_MAX_FRAME_SIZE;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200652
653 BT_DBG("%s", hdev->name);
654
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200655 if (!data->bulk_rx_ep)
656 return -ENODEV;
657
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100658 urb = usb_alloc_urb(0, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200659 if (!urb)
660 return -ENOMEM;
661
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100662 buf = kmalloc(size, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200663 if (!buf) {
664 usb_free_urb(urb);
665 return -ENOMEM;
666 }
667
668 pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
669
Marcel Holtmann89e75332014-09-16 04:44:50 +0200670 usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
671 btusb_bulk_complete, hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200672
673 urb->transfer_flags |= URB_FREE_BUFFER;
674
Oliver Neukum7bee5492009-08-24 23:44:59 +0200675 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200676 usb_anchor_urb(urb, &data->bulk_anchor);
677
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100678 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200679 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +0200680 if (err != -EPERM && err != -ENODEV)
681 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200682 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200683 usb_unanchor_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200684 }
685
686 usb_free_urb(urb);
687
688 return err;
689}
690
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200691static void btusb_isoc_complete(struct urb *urb)
692{
693 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +0100694 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200695 int i, err;
696
Marcel Holtmann89e75332014-09-16 04:44:50 +0200697 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
698 urb->actual_length);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200699
700 if (!test_bit(HCI_RUNNING, &hdev->flags))
701 return;
702
703 if (urb->status == 0) {
704 for (i = 0; i < urb->number_of_packets; i++) {
705 unsigned int offset = urb->iso_frame_desc[i].offset;
706 unsigned int length = urb->iso_frame_desc[i].actual_length;
707
708 if (urb->iso_frame_desc[i].status)
709 continue;
710
711 hdev->stat.byte_rx += length;
712
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200713 if (btusb_recv_isoc(data, urb->transfer_buffer + offset,
714 length) < 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200715 BT_ERR("%s corrupted SCO packet", hdev->name);
716 hdev->stat.err_rx++;
717 }
718 }
Champion Chen85560c42014-09-06 14:06:08 -0500719 } else if (urb->status == -ENOENT) {
720 /* Avoid suspend failed when usb_kill_urb */
721 return;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200722 }
723
724 if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags))
725 return;
726
727 usb_anchor_urb(urb, &data->isoc_anchor);
728
729 err = usb_submit_urb(urb, GFP_ATOMIC);
730 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200731 /* -EPERM: urb is being killed;
732 * -ENODEV: device got disconnected */
733 if (err != -EPERM && err != -ENODEV)
Stefan Seyfried61faddf2010-11-30 21:49:08 +0100734 BT_ERR("%s urb %p failed to resubmit (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200735 hdev->name, urb, -err);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200736 usb_unanchor_urb(urb);
737 }
738}
739
Jesper Juhl42b16b32011-01-17 00:09:38 +0100740static inline void __fill_isoc_descriptor(struct urb *urb, int len, int mtu)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200741{
742 int i, offset = 0;
743
744 BT_DBG("len %d mtu %d", len, mtu);
745
746 for (i = 0; i < BTUSB_MAX_ISOC_FRAMES && len >= mtu;
747 i++, offset += mtu, len -= mtu) {
748 urb->iso_frame_desc[i].offset = offset;
749 urb->iso_frame_desc[i].length = mtu;
750 }
751
752 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
753 urb->iso_frame_desc[i].offset = offset;
754 urb->iso_frame_desc[i].length = len;
755 i++;
756 }
757
758 urb->number_of_packets = i;
759}
760
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100761static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200762{
David Herrmann155961e2012-02-09 21:58:32 +0100763 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200764 struct urb *urb;
765 unsigned char *buf;
766 unsigned int pipe;
767 int err, size;
768
769 BT_DBG("%s", hdev->name);
770
771 if (!data->isoc_rx_ep)
772 return -ENODEV;
773
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100774 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200775 if (!urb)
776 return -ENOMEM;
777
778 size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) *
779 BTUSB_MAX_ISOC_FRAMES;
780
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100781 buf = kmalloc(size, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200782 if (!buf) {
783 usb_free_urb(urb);
784 return -ENOMEM;
785 }
786
787 pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress);
788
Bing Zhaofa0fb932011-12-20 18:19:00 -0800789 usb_fill_int_urb(urb, data->udev, pipe, buf, size, btusb_isoc_complete,
Marcel Holtmann89e75332014-09-16 04:44:50 +0200790 hdev, data->isoc_rx_ep->bInterval);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200791
Marcel Holtmann89e75332014-09-16 04:44:50 +0200792 urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200793
794 __fill_isoc_descriptor(urb, size,
Marcel Holtmann89e75332014-09-16 04:44:50 +0200795 le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize));
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200796
797 usb_anchor_urb(urb, &data->isoc_anchor);
798
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100799 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200800 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +0200801 if (err != -EPERM && err != -ENODEV)
802 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200803 hdev->name, urb, -err);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200804 usb_unanchor_urb(urb);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200805 }
806
807 usb_free_urb(urb);
808
809 return err;
810}
811
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200812static void btusb_tx_complete(struct urb *urb)
813{
814 struct sk_buff *skb = urb->context;
Marcel Holtmann89e75332014-09-16 04:44:50 +0200815 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
David Herrmann155961e2012-02-09 21:58:32 +0100816 struct btusb_data *data = hci_get_drvdata(hdev);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200817
Marcel Holtmann89e75332014-09-16 04:44:50 +0200818 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
819 urb->actual_length);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200820
821 if (!test_bit(HCI_RUNNING, &hdev->flags))
822 goto done;
823
824 if (!urb->status)
825 hdev->stat.byte_tx += urb->transfer_buffer_length;
826 else
827 hdev->stat.err_tx++;
828
829done:
830 spin_lock(&data->txlock);
831 data->tx_in_flight--;
832 spin_unlock(&data->txlock);
833
834 kfree(urb->setup_packet);
835
836 kfree_skb(skb);
837}
838
839static void btusb_isoc_tx_complete(struct urb *urb)
840{
841 struct sk_buff *skb = urb->context;
Marcel Holtmann89e75332014-09-16 04:44:50 +0200842 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200843
Marcel Holtmann89e75332014-09-16 04:44:50 +0200844 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
845 urb->actual_length);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200846
847 if (!test_bit(HCI_RUNNING, &hdev->flags))
848 goto done;
849
850 if (!urb->status)
851 hdev->stat.byte_tx += urb->transfer_buffer_length;
852 else
853 hdev->stat.err_tx++;
854
855done:
856 kfree(urb->setup_packet);
857
858 kfree_skb(skb);
859}
860
861static int btusb_open(struct hci_dev *hdev)
862{
David Herrmann155961e2012-02-09 21:58:32 +0100863 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200864 int err;
865
866 BT_DBG("%s", hdev->name);
867
Oliver Neukum7bee5492009-08-24 23:44:59 +0200868 err = usb_autopm_get_interface(data->intf);
869 if (err < 0)
870 return err;
871
872 data->intf->needs_remote_wakeup = 1;
873
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200874 if (test_and_set_bit(HCI_RUNNING, &hdev->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +0200875 goto done;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200876
877 if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +0200878 goto done;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200879
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100880 err = btusb_submit_intr_urb(hdev, GFP_KERNEL);
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100881 if (err < 0)
882 goto failed;
883
884 err = btusb_submit_bulk_urb(hdev, GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200885 if (err < 0) {
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100886 usb_kill_anchored_urbs(&data->intr_anchor);
887 goto failed;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200888 }
889
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100890 set_bit(BTUSB_BULK_RUNNING, &data->flags);
891 btusb_submit_bulk_urb(hdev, GFP_KERNEL);
892
Oliver Neukum7bee5492009-08-24 23:44:59 +0200893done:
894 usb_autopm_put_interface(data->intf);
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100895 return 0;
896
897failed:
898 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
899 clear_bit(HCI_RUNNING, &hdev->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200900 usb_autopm_put_interface(data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200901 return err;
902}
903
Oliver Neukum7bee5492009-08-24 23:44:59 +0200904static void btusb_stop_traffic(struct btusb_data *data)
905{
906 usb_kill_anchored_urbs(&data->intr_anchor);
907 usb_kill_anchored_urbs(&data->bulk_anchor);
908 usb_kill_anchored_urbs(&data->isoc_anchor);
909}
910
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200911static int btusb_close(struct hci_dev *hdev)
912{
David Herrmann155961e2012-02-09 21:58:32 +0100913 struct btusb_data *data = hci_get_drvdata(hdev);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200914 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200915
916 BT_DBG("%s", hdev->name);
917
918 if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
919 return 0;
920
Marcel Holtmanne8c3c3d2008-09-23 00:16:36 +0200921 cancel_work_sync(&data->work);
Linus Torvalds404291a2009-11-11 13:32:29 -0800922 cancel_work_sync(&data->waker);
Marcel Holtmanne8c3c3d2008-09-23 00:16:36 +0200923
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200924 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200925 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200926 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200927
928 btusb_stop_traffic(data);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200929 btusb_free_frags(data);
930
Oliver Neukum7bee5492009-08-24 23:44:59 +0200931 err = usb_autopm_get_interface(data->intf);
932 if (err < 0)
Oliver Neukum7b8e2c12009-11-13 14:26:23 +0100933 goto failed;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200934
935 data->intf->needs_remote_wakeup = 0;
936 usb_autopm_put_interface(data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200937
Oliver Neukum7b8e2c12009-11-13 14:26:23 +0100938failed:
939 usb_scuttle_anchored_urbs(&data->deferred);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200940 return 0;
941}
942
943static int btusb_flush(struct hci_dev *hdev)
944{
David Herrmann155961e2012-02-09 21:58:32 +0100945 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200946
947 BT_DBG("%s", hdev->name);
948
949 usb_kill_anchored_urbs(&data->tx_anchor);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200950 btusb_free_frags(data);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200951
952 return 0;
953}
954
Marcel Holtmann047b2ec2014-09-14 09:11:06 +0200955static struct urb *alloc_ctrl_urb(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200956{
David Herrmann155961e2012-02-09 21:58:32 +0100957 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200958 struct usb_ctrlrequest *dr;
959 struct urb *urb;
960 unsigned int pipe;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200961
Marcel Holtmann047b2ec2014-09-14 09:11:06 +0200962 urb = usb_alloc_urb(0, GFP_KERNEL);
963 if (!urb)
964 return ERR_PTR(-ENOMEM);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200965
Marcel Holtmann047b2ec2014-09-14 09:11:06 +0200966 dr = kmalloc(sizeof(*dr), GFP_KERNEL);
967 if (!dr) {
968 usb_free_urb(urb);
969 return ERR_PTR(-ENOMEM);
970 }
971
972 dr->bRequestType = data->cmdreq_type;
973 dr->bRequest = 0;
974 dr->wIndex = 0;
975 dr->wValue = 0;
976 dr->wLength = __cpu_to_le16(skb->len);
977
978 pipe = usb_sndctrlpipe(data->udev, 0x00);
979
Marcel Holtmann89e75332014-09-16 04:44:50 +0200980 usb_fill_control_urb(urb, data->udev, pipe, (void *)dr,
Marcel Holtmann047b2ec2014-09-14 09:11:06 +0200981 skb->data, skb->len, btusb_tx_complete, skb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200982
Marcel Holtmann89e75332014-09-16 04:44:50 +0200983 skb->dev = (void *)hdev;
Marcel Holtmann7bd8f092013-10-11 06:19:18 -0700984
Marcel Holtmann047b2ec2014-09-14 09:11:06 +0200985 return urb;
986}
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200987
Marcel Holtmann047b2ec2014-09-14 09:11:06 +0200988static struct urb *alloc_bulk_urb(struct hci_dev *hdev, struct sk_buff *skb)
989{
990 struct btusb_data *data = hci_get_drvdata(hdev);
991 struct urb *urb;
992 unsigned int pipe;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200993
Marcel Holtmann047b2ec2014-09-14 09:11:06 +0200994 if (!data->bulk_tx_ep)
995 return ERR_PTR(-ENODEV);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200996
Marcel Holtmann047b2ec2014-09-14 09:11:06 +0200997 urb = usb_alloc_urb(0, GFP_KERNEL);
998 if (!urb)
999 return ERR_PTR(-ENOMEM);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001000
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001001 pipe = usb_sndbulkpipe(data->udev, data->bulk_tx_ep->bEndpointAddress);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001002
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001003 usb_fill_bulk_urb(urb, data->udev, pipe,
1004 skb->data, skb->len, btusb_tx_complete, skb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001005
Marcel Holtmann89e75332014-09-16 04:44:50 +02001006 skb->dev = (void *)hdev;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001007
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001008 return urb;
1009}
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001010
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001011static struct urb *alloc_isoc_urb(struct hci_dev *hdev, struct sk_buff *skb)
1012{
1013 struct btusb_data *data = hci_get_drvdata(hdev);
1014 struct urb *urb;
1015 unsigned int pipe;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001016
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001017 if (!data->isoc_tx_ep)
1018 return ERR_PTR(-ENODEV);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001019
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001020 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_KERNEL);
1021 if (!urb)
1022 return ERR_PTR(-ENOMEM);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001023
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001024 pipe = usb_sndisocpipe(data->udev, data->isoc_tx_ep->bEndpointAddress);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001025
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001026 usb_fill_int_urb(urb, data->udev, pipe,
1027 skb->data, skb->len, btusb_isoc_tx_complete,
1028 skb, data->isoc_tx_ep->bInterval);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001029
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001030 urb->transfer_flags = URB_ISO_ASAP;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001031
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001032 __fill_isoc_descriptor(urb, skb->len,
1033 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001034
Marcel Holtmann89e75332014-09-16 04:44:50 +02001035 skb->dev = (void *)hdev;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001036
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001037 return urb;
1038}
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001039
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001040static int submit_tx_urb(struct hci_dev *hdev, struct urb *urb)
1041{
1042 struct btusb_data *data = hci_get_drvdata(hdev);
1043 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001044
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001045 usb_anchor_urb(urb, &data->tx_anchor);
1046
Johan Hedberge9753ef2014-09-14 08:49:34 +03001047 err = usb_submit_urb(urb, GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001048 if (err < 0) {
Paul Bolle5a9b80e2011-10-09 12:12:16 +02001049 if (err != -EPERM && err != -ENODEV)
1050 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +02001051 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001052 kfree(urb->setup_packet);
1053 usb_unanchor_urb(urb);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001054 } else {
1055 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001056 }
1057
Cong Wang54a8a792011-11-22 09:32:57 +08001058 usb_free_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001059 return err;
1060}
1061
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001062static int submit_or_queue_tx_urb(struct hci_dev *hdev, struct urb *urb)
1063{
1064 struct btusb_data *data = hci_get_drvdata(hdev);
1065 unsigned long flags;
1066 bool suspending;
1067
1068 spin_lock_irqsave(&data->txlock, flags);
1069 suspending = test_bit(BTUSB_SUSPENDING, &data->flags);
1070 if (!suspending)
1071 data->tx_in_flight++;
1072 spin_unlock_irqrestore(&data->txlock, flags);
1073
1074 if (!suspending)
1075 return submit_tx_urb(hdev, urb);
1076
1077 usb_anchor_urb(urb, &data->deferred);
1078 schedule_work(&data->waker);
1079
1080 usb_free_urb(urb);
1081 return 0;
1082}
1083
1084static int btusb_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
1085{
1086 struct urb *urb;
1087
1088 BT_DBG("%s", hdev->name);
1089
1090 if (!test_bit(HCI_RUNNING, &hdev->flags))
1091 return -EBUSY;
1092
1093 switch (bt_cb(skb)->pkt_type) {
1094 case HCI_COMMAND_PKT:
1095 urb = alloc_ctrl_urb(hdev, skb);
1096 if (IS_ERR(urb))
1097 return PTR_ERR(urb);
1098
1099 hdev->stat.cmd_tx++;
1100 return submit_or_queue_tx_urb(hdev, urb);
1101
1102 case HCI_ACLDATA_PKT:
1103 urb = alloc_bulk_urb(hdev, skb);
1104 if (IS_ERR(urb))
1105 return PTR_ERR(urb);
1106
1107 hdev->stat.acl_tx++;
1108 return submit_or_queue_tx_urb(hdev, urb);
1109
1110 case HCI_SCODATA_PKT:
1111 if (hci_conn_num(hdev, SCO_LINK) < 1)
1112 return -ENODEV;
1113
1114 urb = alloc_isoc_urb(hdev, skb);
1115 if (IS_ERR(urb))
1116 return PTR_ERR(urb);
1117
1118 hdev->stat.sco_tx++;
1119 return submit_tx_urb(hdev, urb);
1120 }
1121
1122 return -EILSEQ;
1123}
1124
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001125static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
1126{
David Herrmann155961e2012-02-09 21:58:32 +01001127 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001128
1129 BT_DBG("%s evt %d", hdev->name, evt);
1130
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001131 if (hci_conn_num(hdev, SCO_LINK) != data->sco_num) {
1132 data->sco_num = hci_conn_num(hdev, SCO_LINK);
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001133 schedule_work(&data->work);
Marcel Holtmanna780efa2008-11-30 12:17:12 +01001134 }
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001135}
1136
Jesper Juhl42b16b32011-01-17 00:09:38 +01001137static inline int __set_isoc_interface(struct hci_dev *hdev, int altsetting)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001138{
David Herrmann155961e2012-02-09 21:58:32 +01001139 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001140 struct usb_interface *intf = data->isoc;
1141 struct usb_endpoint_descriptor *ep_desc;
1142 int i, err;
1143
1144 if (!data->isoc)
1145 return -ENODEV;
1146
1147 err = usb_set_interface(data->udev, 1, altsetting);
1148 if (err < 0) {
1149 BT_ERR("%s setting interface failed (%d)", hdev->name, -err);
1150 return err;
1151 }
1152
1153 data->isoc_altsetting = altsetting;
1154
1155 data->isoc_tx_ep = NULL;
1156 data->isoc_rx_ep = NULL;
1157
1158 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
1159 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
1160
1161 if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) {
1162 data->isoc_tx_ep = ep_desc;
1163 continue;
1164 }
1165
1166 if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) {
1167 data->isoc_rx_ep = ep_desc;
1168 continue;
1169 }
1170 }
1171
1172 if (!data->isoc_tx_ep || !data->isoc_rx_ep) {
1173 BT_ERR("%s invalid SCO descriptors", hdev->name);
1174 return -ENODEV;
1175 }
1176
1177 return 0;
1178}
1179
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001180static void btusb_work(struct work_struct *work)
1181{
1182 struct btusb_data *data = container_of(work, struct btusb_data, work);
1183 struct hci_dev *hdev = data->hdev;
Mikel Astizf4001d22012-04-11 08:48:51 +02001184 int new_alts;
Oliver Neukum7bee5492009-08-24 23:44:59 +02001185 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001186
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001187 if (data->sco_num > 0) {
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -03001188 if (!test_bit(BTUSB_DID_ISO_RESUME, &data->flags)) {
Oliver Neukum8efdd0c2011-02-11 13:00:06 +01001189 err = usb_autopm_get_interface(data->isoc ? data->isoc : data->intf);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001190 if (err < 0) {
1191 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1192 usb_kill_anchored_urbs(&data->isoc_anchor);
1193 return;
1194 }
1195
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -03001196 set_bit(BTUSB_DID_ISO_RESUME, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001197 }
Mikel Astizf4001d22012-04-11 08:48:51 +02001198
1199 if (hdev->voice_setting & 0x0020) {
1200 static const int alts[3] = { 2, 4, 5 };
Marcel Holtmann89e75332014-09-16 04:44:50 +02001201
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001202 new_alts = alts[data->sco_num - 1];
Mikel Astizf4001d22012-04-11 08:48:51 +02001203 } else {
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001204 new_alts = data->sco_num;
Mikel Astizf4001d22012-04-11 08:48:51 +02001205 }
1206
1207 if (data->isoc_altsetting != new_alts) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001208 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1209 usb_kill_anchored_urbs(&data->isoc_anchor);
1210
Mikel Astizf4001d22012-04-11 08:48:51 +02001211 if (__set_isoc_interface(hdev, new_alts) < 0)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001212 return;
1213 }
1214
1215 if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001216 if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001217 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1218 else
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001219 btusb_submit_isoc_urb(hdev, GFP_KERNEL);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001220 }
1221 } else {
1222 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1223 usb_kill_anchored_urbs(&data->isoc_anchor);
1224
1225 __set_isoc_interface(hdev, 0);
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -03001226 if (test_and_clear_bit(BTUSB_DID_ISO_RESUME, &data->flags))
Oliver Neukum8efdd0c2011-02-11 13:00:06 +01001227 usb_autopm_put_interface(data->isoc ? data->isoc : data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001228 }
1229}
1230
Oliver Neukum7bee5492009-08-24 23:44:59 +02001231static void btusb_waker(struct work_struct *work)
1232{
1233 struct btusb_data *data = container_of(work, struct btusb_data, waker);
1234 int err;
1235
1236 err = usb_autopm_get_interface(data->intf);
1237 if (err < 0)
1238 return;
1239
1240 usb_autopm_put_interface(data->intf);
1241}
1242
Marcel Holtmann9f8f9622013-04-10 08:11:35 -07001243static int btusb_setup_bcm92035(struct hci_dev *hdev)
1244{
1245 struct sk_buff *skb;
1246 u8 val = 0x00;
1247
1248 BT_DBG("%s", hdev->name);
1249
1250 skb = __hci_cmd_sync(hdev, 0xfc3b, 1, &val, HCI_INIT_TIMEOUT);
1251 if (IS_ERR(skb))
1252 BT_ERR("BCM92035 command failed (%ld)", -PTR_ERR(skb));
1253 else
1254 kfree_skb(skb);
1255
1256 return 0;
1257}
1258
Marcel Holtmann81cac642014-01-03 03:02:36 -08001259static int btusb_setup_csr(struct hci_dev *hdev)
1260{
1261 struct hci_rp_read_local_version *rp;
1262 struct sk_buff *skb;
1263 int ret;
1264
1265 BT_DBG("%s", hdev->name);
1266
1267 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
1268 HCI_INIT_TIMEOUT);
1269 if (IS_ERR(skb)) {
1270 BT_ERR("Reading local version failed (%ld)", -PTR_ERR(skb));
1271 return -PTR_ERR(skb);
1272 }
1273
Marcel Holtmann89e75332014-09-16 04:44:50 +02001274 rp = (struct hci_rp_read_local_version *)skb->data;
Marcel Holtmann81cac642014-01-03 03:02:36 -08001275
1276 if (!rp->status) {
1277 if (le16_to_cpu(rp->manufacturer) != 10) {
1278 /* Clear the reset quirk since this is not an actual
1279 * early Bluetooth 1.1 device from CSR.
1280 */
1281 clear_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
1282
1283 /* These fake CSR controllers have all a broken
1284 * stored link key handling and so just disable it.
1285 */
1286 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY,
1287 &hdev->quirks);
1288 }
1289 }
1290
1291 ret = -bt_to_errno(rp->status);
1292
1293 kfree_skb(skb);
1294
1295 return ret;
1296}
1297
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001298struct intel_version {
1299 u8 status;
1300 u8 hw_platform;
1301 u8 hw_variant;
1302 u8 hw_revision;
1303 u8 fw_variant;
1304 u8 fw_revision;
1305 u8 fw_build_num;
1306 u8 fw_build_ww;
1307 u8 fw_build_yy;
1308 u8 fw_patch_num;
1309} __packed;
1310
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001311struct intel_boot_params {
1312 __u8 status;
1313 __u8 otp_format;
1314 __u8 otp_content;
1315 __u8 otp_patch;
1316 __le16 dev_revid;
1317 __u8 secure_boot;
1318 __u8 key_from_hdr;
1319 __u8 key_type;
1320 __u8 otp_lock;
1321 __u8 api_lock;
1322 __u8 debug_lock;
1323 bdaddr_t otp_bdaddr;
1324 __u8 min_fw_build_nn;
1325 __u8 min_fw_build_cw;
1326 __u8 min_fw_build_yy;
1327 __u8 limited_cce;
1328 __u8 unlocked_state;
1329} __packed;
1330
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001331static const struct firmware *btusb_setup_intel_get_fw(struct hci_dev *hdev,
Marcel Holtmann89e75332014-09-16 04:44:50 +02001332 struct intel_version *ver)
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001333{
1334 const struct firmware *fw;
1335 char fwname[64];
1336 int ret;
1337
1338 snprintf(fwname, sizeof(fwname),
1339 "intel/ibt-hw-%x.%x.%x-fw-%x.%x.%x.%x.%x.bseq",
1340 ver->hw_platform, ver->hw_variant, ver->hw_revision,
1341 ver->fw_variant, ver->fw_revision, ver->fw_build_num,
1342 ver->fw_build_ww, ver->fw_build_yy);
1343
1344 ret = request_firmware(&fw, fwname, &hdev->dev);
1345 if (ret < 0) {
1346 if (ret == -EINVAL) {
1347 BT_ERR("%s Intel firmware file request failed (%d)",
1348 hdev->name, ret);
1349 return NULL;
1350 }
1351
1352 BT_ERR("%s failed to open Intel firmware file: %s(%d)",
1353 hdev->name, fwname, ret);
1354
1355 /* If the correct firmware patch file is not found, use the
1356 * default firmware patch file instead
1357 */
1358 snprintf(fwname, sizeof(fwname), "intel/ibt-hw-%x.%x.bseq",
1359 ver->hw_platform, ver->hw_variant);
1360 if (request_firmware(&fw, fwname, &hdev->dev) < 0) {
1361 BT_ERR("%s failed to open default Intel fw file: %s",
1362 hdev->name, fwname);
1363 return NULL;
1364 }
1365 }
1366
1367 BT_INFO("%s: Intel Bluetooth firmware file: %s", hdev->name, fwname);
1368
1369 return fw;
1370}
1371
1372static int btusb_setup_intel_patching(struct hci_dev *hdev,
1373 const struct firmware *fw,
1374 const u8 **fw_ptr, int *disable_patch)
1375{
1376 struct sk_buff *skb;
1377 struct hci_command_hdr *cmd;
1378 const u8 *cmd_param;
1379 struct hci_event_hdr *evt = NULL;
1380 const u8 *evt_param = NULL;
1381 int remain = fw->size - (*fw_ptr - fw->data);
1382
1383 /* The first byte indicates the types of the patch command or event.
1384 * 0x01 means HCI command and 0x02 is HCI event. If the first bytes
1385 * in the current firmware buffer doesn't start with 0x01 or
1386 * the size of remain buffer is smaller than HCI command header,
1387 * the firmware file is corrupted and it should stop the patching
1388 * process.
1389 */
1390 if (remain > HCI_COMMAND_HDR_SIZE && *fw_ptr[0] != 0x01) {
1391 BT_ERR("%s Intel fw corrupted: invalid cmd read", hdev->name);
1392 return -EINVAL;
1393 }
1394 (*fw_ptr)++;
1395 remain--;
1396
1397 cmd = (struct hci_command_hdr *)(*fw_ptr);
1398 *fw_ptr += sizeof(*cmd);
1399 remain -= sizeof(*cmd);
1400
1401 /* Ensure that the remain firmware data is long enough than the length
1402 * of command parameter. If not, the firmware file is corrupted.
1403 */
1404 if (remain < cmd->plen) {
1405 BT_ERR("%s Intel fw corrupted: invalid cmd len", hdev->name);
1406 return -EFAULT;
1407 }
1408
1409 /* If there is a command that loads a patch in the firmware
1410 * file, then enable the patch upon success, otherwise just
1411 * disable the manufacturer mode, for example patch activation
1412 * is not required when the default firmware patch file is used
1413 * because there are no patch data to load.
1414 */
1415 if (*disable_patch && le16_to_cpu(cmd->opcode) == 0xfc8e)
1416 *disable_patch = 0;
1417
1418 cmd_param = *fw_ptr;
1419 *fw_ptr += cmd->plen;
1420 remain -= cmd->plen;
1421
1422 /* This reads the expected events when the above command is sent to the
1423 * device. Some vendor commands expects more than one events, for
1424 * example command status event followed by vendor specific event.
1425 * For this case, it only keeps the last expected event. so the command
1426 * can be sent with __hci_cmd_sync_ev() which returns the sk_buff of
1427 * last expected event.
1428 */
1429 while (remain > HCI_EVENT_HDR_SIZE && *fw_ptr[0] == 0x02) {
1430 (*fw_ptr)++;
1431 remain--;
1432
1433 evt = (struct hci_event_hdr *)(*fw_ptr);
1434 *fw_ptr += sizeof(*evt);
1435 remain -= sizeof(*evt);
1436
1437 if (remain < evt->plen) {
1438 BT_ERR("%s Intel fw corrupted: invalid evt len",
1439 hdev->name);
1440 return -EFAULT;
1441 }
1442
1443 evt_param = *fw_ptr;
1444 *fw_ptr += evt->plen;
1445 remain -= evt->plen;
1446 }
1447
1448 /* Every HCI commands in the firmware file has its correspond event.
1449 * If event is not found or remain is smaller than zero, the firmware
1450 * file is corrupted.
1451 */
1452 if (!evt || !evt_param || remain < 0) {
1453 BT_ERR("%s Intel fw corrupted: invalid evt read", hdev->name);
1454 return -EFAULT;
1455 }
1456
1457 skb = __hci_cmd_sync_ev(hdev, le16_to_cpu(cmd->opcode), cmd->plen,
1458 cmd_param, evt->evt, HCI_INIT_TIMEOUT);
1459 if (IS_ERR(skb)) {
1460 BT_ERR("%s sending Intel patch command (0x%4.4x) failed (%ld)",
1461 hdev->name, cmd->opcode, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001462 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001463 }
1464
1465 /* It ensures that the returned event matches the event data read from
1466 * the firmware file. At fist, it checks the length and then
1467 * the contents of the event.
1468 */
1469 if (skb->len != evt->plen) {
1470 BT_ERR("%s mismatch event length (opcode 0x%4.4x)", hdev->name,
1471 le16_to_cpu(cmd->opcode));
1472 kfree_skb(skb);
1473 return -EFAULT;
1474 }
1475
1476 if (memcmp(skb->data, evt_param, evt->plen)) {
1477 BT_ERR("%s mismatch event parameter (opcode 0x%4.4x)",
1478 hdev->name, le16_to_cpu(cmd->opcode));
1479 kfree_skb(skb);
1480 return -EFAULT;
1481 }
1482 kfree_skb(skb);
1483
1484 return 0;
1485}
1486
Marcel Holtmann40cb0982014-07-02 12:06:45 +02001487#define BDADDR_INTEL (&(bdaddr_t) {{0x00, 0x8b, 0x9e, 0x19, 0x03, 0x00}})
1488
1489static int btusb_check_bdaddr_intel(struct hci_dev *hdev)
1490{
1491 struct sk_buff *skb;
1492 struct hci_rp_read_bd_addr *rp;
1493
1494 skb = __hci_cmd_sync(hdev, HCI_OP_READ_BD_ADDR, 0, NULL,
1495 HCI_INIT_TIMEOUT);
1496 if (IS_ERR(skb)) {
1497 BT_ERR("%s reading Intel device address failed (%ld)",
1498 hdev->name, PTR_ERR(skb));
1499 return PTR_ERR(skb);
1500 }
1501
1502 if (skb->len != sizeof(*rp)) {
1503 BT_ERR("%s Intel device address length mismatch", hdev->name);
1504 kfree_skb(skb);
1505 return -EIO;
1506 }
1507
Marcel Holtmann89e75332014-09-16 04:44:50 +02001508 rp = (struct hci_rp_read_bd_addr *)skb->data;
Marcel Holtmann40cb0982014-07-02 12:06:45 +02001509 if (rp->status) {
1510 BT_ERR("%s Intel device address result failed (%02x)",
1511 hdev->name, rp->status);
1512 kfree_skb(skb);
1513 return -bt_to_errno(rp->status);
1514 }
1515
1516 /* For some Intel based controllers, the default Bluetooth device
1517 * address 00:03:19:9E:8B:00 can be found. These controllers are
1518 * fully operational, but have the danger of duplicate addresses
1519 * and that in turn can cause problems with Bluetooth operation.
1520 */
Marcel Holtmann4739b5b2014-07-04 16:54:38 +02001521 if (!bacmp(&rp->bdaddr, BDADDR_INTEL)) {
Marcel Holtmann40cb0982014-07-02 12:06:45 +02001522 BT_ERR("%s found Intel default device address (%pMR)",
1523 hdev->name, &rp->bdaddr);
Marcel Holtmann4739b5b2014-07-04 16:54:38 +02001524 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
1525 }
Marcel Holtmann40cb0982014-07-02 12:06:45 +02001526
1527 kfree_skb(skb);
1528
1529 return 0;
1530}
1531
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001532static int btusb_setup_intel(struct hci_dev *hdev)
1533{
1534 struct sk_buff *skb;
1535 const struct firmware *fw;
1536 const u8 *fw_ptr;
1537 int disable_patch;
1538 struct intel_version *ver;
1539
1540 const u8 mfg_enable[] = { 0x01, 0x00 };
1541 const u8 mfg_disable[] = { 0x00, 0x00 };
1542 const u8 mfg_reset_deactivate[] = { 0x00, 0x01 };
1543 const u8 mfg_reset_activate[] = { 0x00, 0x02 };
1544
1545 BT_DBG("%s", hdev->name);
1546
1547 /* The controller has a bug with the first HCI command sent to it
1548 * returning number of completed commands as zero. This would stall the
1549 * command processing in the Bluetooth core.
1550 *
1551 * As a workaround, send HCI Reset command first which will reset the
1552 * number of completed commands and allow normal command processing
1553 * from now on.
1554 */
1555 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
1556 if (IS_ERR(skb)) {
1557 BT_ERR("%s sending initial HCI reset command failed (%ld)",
1558 hdev->name, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001559 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001560 }
1561 kfree_skb(skb);
1562
1563 /* Read Intel specific controller version first to allow selection of
1564 * which firmware file to load.
1565 *
1566 * The returned information are hardware variant and revision plus
1567 * firmware variant, revision and build number.
1568 */
1569 skb = __hci_cmd_sync(hdev, 0xfc05, 0, NULL, HCI_INIT_TIMEOUT);
1570 if (IS_ERR(skb)) {
1571 BT_ERR("%s reading Intel fw version command failed (%ld)",
1572 hdev->name, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001573 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001574 }
1575
1576 if (skb->len != sizeof(*ver)) {
1577 BT_ERR("%s Intel version event length mismatch", hdev->name);
1578 kfree_skb(skb);
1579 return -EIO;
1580 }
1581
1582 ver = (struct intel_version *)skb->data;
1583 if (ver->status) {
1584 BT_ERR("%s Intel fw version event failed (%02x)", hdev->name,
1585 ver->status);
1586 kfree_skb(skb);
1587 return -bt_to_errno(ver->status);
1588 }
1589
1590 BT_INFO("%s: read Intel version: %02x%02x%02x%02x%02x%02x%02x%02x%02x",
1591 hdev->name, ver->hw_platform, ver->hw_variant,
1592 ver->hw_revision, ver->fw_variant, ver->fw_revision,
1593 ver->fw_build_num, ver->fw_build_ww, ver->fw_build_yy,
1594 ver->fw_patch_num);
1595
1596 /* fw_patch_num indicates the version of patch the device currently
1597 * have. If there is no patch data in the device, it is always 0x00.
1598 * So, if it is other than 0x00, no need to patch the deivce again.
1599 */
1600 if (ver->fw_patch_num) {
1601 BT_INFO("%s: Intel device is already patched. patch num: %02x",
1602 hdev->name, ver->fw_patch_num);
1603 kfree_skb(skb);
Marcel Holtmann40cb0982014-07-02 12:06:45 +02001604 btusb_check_bdaddr_intel(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001605 return 0;
1606 }
1607
1608 /* Opens the firmware patch file based on the firmware version read
1609 * from the controller. If it fails to open the matching firmware
1610 * patch file, it tries to open the default firmware patch file.
1611 * If no patch file is found, allow the device to operate without
1612 * a patch.
1613 */
1614 fw = btusb_setup_intel_get_fw(hdev, ver);
1615 if (!fw) {
1616 kfree_skb(skb);
Marcel Holtmann40cb0982014-07-02 12:06:45 +02001617 btusb_check_bdaddr_intel(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001618 return 0;
1619 }
1620 fw_ptr = fw->data;
1621
1622 /* This Intel specific command enables the manufacturer mode of the
1623 * controller.
1624 *
1625 * Only while this mode is enabled, the driver can download the
1626 * firmware patch data and configuration parameters.
1627 */
1628 skb = __hci_cmd_sync(hdev, 0xfc11, 2, mfg_enable, HCI_INIT_TIMEOUT);
1629 if (IS_ERR(skb)) {
1630 BT_ERR("%s entering Intel manufacturer mode failed (%ld)",
1631 hdev->name, PTR_ERR(skb));
1632 release_firmware(fw);
Adam Leed9c78e92013-07-10 10:02:12 +08001633 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001634 }
1635
1636 if (skb->data[0]) {
1637 u8 evt_status = skb->data[0];
Marcel Holtmann89e75332014-09-16 04:44:50 +02001638
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001639 BT_ERR("%s enable Intel manufacturer mode event failed (%02x)",
1640 hdev->name, evt_status);
1641 kfree_skb(skb);
1642 release_firmware(fw);
1643 return -bt_to_errno(evt_status);
1644 }
1645 kfree_skb(skb);
1646
1647 disable_patch = 1;
1648
1649 /* The firmware data file consists of list of Intel specific HCI
1650 * commands and its expected events. The first byte indicates the
1651 * type of the message, either HCI command or HCI event.
1652 *
1653 * It reads the command and its expected event from the firmware file,
1654 * and send to the controller. Once __hci_cmd_sync_ev() returns,
1655 * the returned event is compared with the event read from the firmware
1656 * file and it will continue until all the messages are downloaded to
1657 * the controller.
1658 *
1659 * Once the firmware patching is completed successfully,
1660 * the manufacturer mode is disabled with reset and activating the
1661 * downloaded patch.
1662 *
1663 * If the firmware patching fails, the manufacturer mode is
1664 * disabled with reset and deactivating the patch.
1665 *
1666 * If the default patch file is used, no reset is done when disabling
1667 * the manufacturer.
1668 */
1669 while (fw->size > fw_ptr - fw->data) {
1670 int ret;
1671
1672 ret = btusb_setup_intel_patching(hdev, fw, &fw_ptr,
1673 &disable_patch);
1674 if (ret < 0)
1675 goto exit_mfg_deactivate;
1676 }
1677
1678 release_firmware(fw);
1679
1680 if (disable_patch)
1681 goto exit_mfg_disable;
1682
1683 /* Patching completed successfully and disable the manufacturer mode
1684 * with reset and activate the downloaded firmware patches.
1685 */
1686 skb = __hci_cmd_sync(hdev, 0xfc11, sizeof(mfg_reset_activate),
1687 mfg_reset_activate, HCI_INIT_TIMEOUT);
1688 if (IS_ERR(skb)) {
1689 BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
1690 hdev->name, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001691 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001692 }
1693 kfree_skb(skb);
1694
1695 BT_INFO("%s: Intel Bluetooth firmware patch completed and activated",
1696 hdev->name);
1697
Marcel Holtmann40cb0982014-07-02 12:06:45 +02001698 btusb_check_bdaddr_intel(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001699 return 0;
1700
1701exit_mfg_disable:
1702 /* Disable the manufacturer mode without reset */
1703 skb = __hci_cmd_sync(hdev, 0xfc11, sizeof(mfg_disable), mfg_disable,
1704 HCI_INIT_TIMEOUT);
1705 if (IS_ERR(skb)) {
1706 BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
1707 hdev->name, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001708 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001709 }
1710 kfree_skb(skb);
1711
1712 BT_INFO("%s: Intel Bluetooth firmware patch completed", hdev->name);
Marcel Holtmann40cb0982014-07-02 12:06:45 +02001713
1714 btusb_check_bdaddr_intel(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001715 return 0;
1716
1717exit_mfg_deactivate:
1718 release_firmware(fw);
1719
1720 /* Patching failed. Disable the manufacturer mode with reset and
1721 * deactivate the downloaded firmware patches.
1722 */
1723 skb = __hci_cmd_sync(hdev, 0xfc11, sizeof(mfg_reset_deactivate),
1724 mfg_reset_deactivate, HCI_INIT_TIMEOUT);
1725 if (IS_ERR(skb)) {
1726 BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
1727 hdev->name, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001728 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001729 }
1730 kfree_skb(skb);
1731
1732 BT_INFO("%s: Intel Bluetooth firmware patch completed and deactivated",
1733 hdev->name);
1734
Marcel Holtmann40cb0982014-07-02 12:06:45 +02001735 btusb_check_bdaddr_intel(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001736 return 0;
1737}
1738
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001739static int inject_cmd_complete(struct hci_dev *hdev, __u16 opcode)
1740{
1741 struct sk_buff *skb;
1742 struct hci_event_hdr *hdr;
1743 struct hci_ev_cmd_complete *evt;
1744
1745 skb = bt_skb_alloc(sizeof(*hdr) + sizeof(*evt) + 1, GFP_ATOMIC);
1746 if (!skb)
1747 return -ENOMEM;
1748
1749 hdr = (struct hci_event_hdr *)skb_put(skb, sizeof(*hdr));
1750 hdr->evt = HCI_EV_CMD_COMPLETE;
1751 hdr->plen = sizeof(*evt) + 1;
1752
1753 evt = (struct hci_ev_cmd_complete *)skb_put(skb, sizeof(*evt));
1754 evt->ncmd = 0x01;
1755 evt->opcode = cpu_to_le16(opcode);
1756
1757 *skb_put(skb, 1) = 0x00;
1758
1759 bt_cb(skb)->pkt_type = HCI_EVENT_PKT;
1760
1761 return hci_recv_frame(hdev, skb);
1762}
1763
1764static int btusb_recv_bulk_intel(struct btusb_data *data, void *buffer,
1765 int count)
1766{
1767 /* When the device is in bootloader mode, then it can send
1768 * events via the bulk endpoint. These events are treated the
1769 * same way as the ones received from the interrupt endpoint.
1770 */
1771 if (test_bit(BTUSB_BOOTLOADER, &data->flags))
1772 return btusb_recv_intr(data, buffer, count);
1773
1774 return btusb_recv_bulk(data, buffer, count);
1775}
1776
1777static int btusb_recv_event_intel(struct hci_dev *hdev, struct sk_buff *skb)
1778{
1779 struct btusb_data *data = hci_get_drvdata(hdev);
1780
1781 if (test_bit(BTUSB_BOOTLOADER, &data->flags)) {
1782 struct hci_event_hdr *hdr = (void *)skb->data;
1783
1784 /* When the firmware loading completes the device sends
1785 * out a vendor specific event indicating the result of
1786 * the firmware loading.
1787 */
1788 if (skb->len == 7 && hdr->evt == 0xff && hdr->plen == 0x05 &&
1789 skb->data[2] == 0x06) {
1790 if (skb->data[3] != 0x00)
1791 test_bit(BTUSB_FIRMWARE_FAILED, &data->flags);
1792
Marcel Holtmannce6bb922015-01-28 01:58:40 -08001793 if (test_and_clear_bit(BTUSB_DOWNLOADING,
1794 &data->flags) &&
1795 test_bit(BTUSB_FIRMWARE_LOADED, &data->flags))
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001796 wake_up_interruptible(&hdev->req_wait_q);
1797 }
1798
1799 /* When switching to the operational firmware the device
1800 * sends a vendor specific event indicating that the bootup
1801 * completed.
1802 */
1803 if (skb->len == 9 && hdr->evt == 0xff && hdr->plen == 0x07 &&
1804 skb->data[2] == 0x02) {
1805 if (test_and_clear_bit(BTUSB_BOOTING, &data->flags))
1806 wake_up_interruptible(&hdev->req_wait_q);
1807 }
1808 }
1809
1810 return hci_recv_frame(hdev, skb);
1811}
1812
1813static int btusb_send_frame_intel(struct hci_dev *hdev, struct sk_buff *skb)
1814{
1815 struct btusb_data *data = hci_get_drvdata(hdev);
1816 struct urb *urb;
1817
1818 BT_DBG("%s", hdev->name);
1819
1820 if (!test_bit(HCI_RUNNING, &hdev->flags))
1821 return -EBUSY;
1822
1823 switch (bt_cb(skb)->pkt_type) {
1824 case HCI_COMMAND_PKT:
1825 if (test_bit(BTUSB_BOOTLOADER, &data->flags)) {
1826 struct hci_command_hdr *cmd = (void *)skb->data;
1827 __u16 opcode = le16_to_cpu(cmd->opcode);
1828
1829 /* When in bootloader mode and the command 0xfc09
1830 * is received, it needs to be send down the
1831 * bulk endpoint. So allocate a bulk URB instead.
1832 */
1833 if (opcode == 0xfc09)
1834 urb = alloc_bulk_urb(hdev, skb);
1835 else
1836 urb = alloc_ctrl_urb(hdev, skb);
1837
1838 /* When the 0xfc01 command is issued to boot into
1839 * the operational firmware, it will actually not
1840 * send a command complete event. To keep the flow
1841 * control working inject that event here.
1842 */
1843 if (opcode == 0xfc01)
1844 inject_cmd_complete(hdev, opcode);
1845 } else {
1846 urb = alloc_ctrl_urb(hdev, skb);
1847 }
1848 if (IS_ERR(urb))
1849 return PTR_ERR(urb);
1850
1851 hdev->stat.cmd_tx++;
1852 return submit_or_queue_tx_urb(hdev, urb);
1853
1854 case HCI_ACLDATA_PKT:
1855 urb = alloc_bulk_urb(hdev, skb);
1856 if (IS_ERR(urb))
1857 return PTR_ERR(urb);
1858
1859 hdev->stat.acl_tx++;
1860 return submit_or_queue_tx_urb(hdev, urb);
1861
1862 case HCI_SCODATA_PKT:
1863 if (hci_conn_num(hdev, SCO_LINK) < 1)
1864 return -ENODEV;
1865
1866 urb = alloc_isoc_urb(hdev, skb);
1867 if (IS_ERR(urb))
1868 return PTR_ERR(urb);
1869
1870 hdev->stat.sco_tx++;
1871 return submit_tx_urb(hdev, urb);
1872 }
1873
1874 return -EILSEQ;
1875}
1876
1877static int btusb_intel_secure_send(struct hci_dev *hdev, u8 fragment_type,
1878 u32 plen, const void *param)
1879{
1880 while (plen > 0) {
1881 struct sk_buff *skb;
1882 u8 cmd_param[253], fragment_len = (plen > 252) ? 252 : plen;
1883
1884 cmd_param[0] = fragment_type;
1885 memcpy(cmd_param + 1, param, fragment_len);
1886
1887 skb = __hci_cmd_sync(hdev, 0xfc09, fragment_len + 1,
1888 cmd_param, HCI_INIT_TIMEOUT);
1889 if (IS_ERR(skb))
1890 return PTR_ERR(skb);
1891
1892 kfree_skb(skb);
1893
1894 plen -= fragment_len;
1895 param += fragment_len;
1896 }
1897
1898 return 0;
1899}
1900
1901static void btusb_intel_version_info(struct hci_dev *hdev,
1902 struct intel_version *ver)
1903{
1904 const char *variant;
1905
1906 switch (ver->fw_variant) {
1907 case 0x06:
1908 variant = "Bootloader";
1909 break;
1910 case 0x23:
1911 variant = "Firmware";
1912 break;
1913 default:
1914 return;
1915 }
1916
1917 BT_INFO("%s: %s revision %u.%u build %u week %u %u", hdev->name,
1918 variant, ver->fw_revision >> 4, ver->fw_revision & 0x0f,
1919 ver->fw_build_num, ver->fw_build_ww, 2000 + ver->fw_build_yy);
1920}
1921
1922static int btusb_setup_intel_new(struct hci_dev *hdev)
1923{
1924 static const u8 reset_param[] = { 0x00, 0x01, 0x00, 0x01,
1925 0x00, 0x08, 0x04, 0x00 };
1926 struct btusb_data *data = hci_get_drvdata(hdev);
1927 struct sk_buff *skb;
1928 struct intel_version *ver;
1929 struct intel_boot_params *params;
1930 const struct firmware *fw;
1931 const u8 *fw_ptr;
1932 char fwname[64];
1933 ktime_t calltime, delta, rettime;
1934 unsigned long long duration;
1935 int err;
1936
1937 BT_DBG("%s", hdev->name);
1938
1939 calltime = ktime_get();
1940
1941 /* Read the Intel version information to determine if the device
1942 * is in bootloader mode or if it already has operational firmware
1943 * loaded.
1944 */
1945 skb = __hci_cmd_sync(hdev, 0xfc05, 0, NULL, HCI_INIT_TIMEOUT);
1946 if (IS_ERR(skb)) {
1947 BT_ERR("%s: Reading Intel version information failed (%ld)",
1948 hdev->name, PTR_ERR(skb));
1949 return PTR_ERR(skb);
1950 }
1951
1952 if (skb->len != sizeof(*ver)) {
1953 BT_ERR("%s: Intel version event size mismatch", hdev->name);
1954 kfree_skb(skb);
1955 return -EILSEQ;
1956 }
1957
1958 ver = (struct intel_version *)skb->data;
1959 if (ver->status) {
1960 BT_ERR("%s: Intel version command failure (%02x)",
1961 hdev->name, ver->status);
1962 err = -bt_to_errno(ver->status);
1963 kfree_skb(skb);
1964 return err;
1965 }
1966
1967 /* The hardware platform number has a fixed value of 0x37 and
1968 * for now only accept this single value.
1969 */
1970 if (ver->hw_platform != 0x37) {
1971 BT_ERR("%s: Unsupported Intel hardware platform (%u)",
1972 hdev->name, ver->hw_platform);
1973 kfree_skb(skb);
1974 return -EINVAL;
1975 }
1976
1977 /* At the moment only the hardware variant iBT 3.0 (LnP/SfP) is
1978 * supported by this firmware loading method. This check has been
1979 * put in place to ensure correct forward compatibility options
1980 * when newer hardware variants come along.
1981 */
1982 if (ver->hw_variant != 0x0b) {
1983 BT_ERR("%s: Unsupported Intel hardware variant (%u)",
1984 hdev->name, ver->hw_variant);
1985 kfree_skb(skb);
1986 return -EINVAL;
1987 }
1988
1989 btusb_intel_version_info(hdev, ver);
1990
1991 /* The firmware variant determines if the device is in bootloader
1992 * mode or is running operational firmware. The value 0x06 identifies
1993 * the bootloader and the value 0x23 identifies the operational
1994 * firmware.
1995 *
1996 * When the operational firmware is already present, then only
1997 * the check for valid Bluetooth device address is needed. This
1998 * determines if the device will be added as configured or
1999 * unconfigured controller.
2000 *
2001 * It is not possible to use the Secure Boot Parameters in this
2002 * case since that command is only available in bootloader mode.
2003 */
2004 if (ver->fw_variant == 0x23) {
2005 kfree_skb(skb);
2006 clear_bit(BTUSB_BOOTLOADER, &data->flags);
2007 btusb_check_bdaddr_intel(hdev);
2008 return 0;
2009 }
2010
2011 /* If the device is not in bootloader mode, then the only possible
2012 * choice is to return an error and abort the device initialization.
2013 */
2014 if (ver->fw_variant != 0x06) {
2015 BT_ERR("%s: Unsupported Intel firmware variant (%u)",
2016 hdev->name, ver->fw_variant);
2017 kfree_skb(skb);
2018 return -ENODEV;
2019 }
2020
2021 kfree_skb(skb);
2022
2023 /* Read the secure boot parameters to identify the operating
2024 * details of the bootloader.
2025 */
2026 skb = __hci_cmd_sync(hdev, 0xfc0d, 0, NULL, HCI_INIT_TIMEOUT);
2027 if (IS_ERR(skb)) {
2028 BT_ERR("%s: Reading Intel boot parameters failed (%ld)",
2029 hdev->name, PTR_ERR(skb));
2030 return PTR_ERR(skb);
2031 }
2032
2033 if (skb->len != sizeof(*params)) {
2034 BT_ERR("%s: Intel boot parameters size mismatch", hdev->name);
2035 kfree_skb(skb);
2036 return -EILSEQ;
2037 }
2038
2039 params = (struct intel_boot_params *)skb->data;
2040 if (params->status) {
2041 BT_ERR("%s: Intel boot parameters command failure (%02x)",
2042 hdev->name, params->status);
2043 err = -bt_to_errno(params->status);
2044 kfree_skb(skb);
2045 return err;
2046 }
2047
2048 BT_INFO("%s: Device revision is %u", hdev->name,
2049 le16_to_cpu(params->dev_revid));
2050
2051 BT_INFO("%s: Secure boot is %s", hdev->name,
2052 params->secure_boot ? "enabled" : "disabled");
2053
2054 BT_INFO("%s: Minimum firmware build %u week %u %u", hdev->name,
2055 params->min_fw_build_nn, params->min_fw_build_cw,
2056 2000 + params->min_fw_build_yy);
2057
2058 /* It is required that every single firmware fragment is acknowledged
2059 * with a command complete event. If the boot parameters indicate
2060 * that this bootloader does not send them, then abort the setup.
2061 */
2062 if (params->limited_cce != 0x00) {
2063 BT_ERR("%s: Unsupported Intel firmware loading method (%u)",
2064 hdev->name, params->limited_cce);
2065 kfree_skb(skb);
2066 return -EINVAL;
2067 }
2068
2069 /* If the OTP has no valid Bluetooth device address, then there will
2070 * also be no valid address for the operational firmware.
2071 */
2072 if (!bacmp(&params->otp_bdaddr, BDADDR_ANY)) {
2073 BT_INFO("%s: No device address configured", hdev->name);
2074 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
2075 }
2076
2077 /* With this Intel bootloader only the hardware variant and device
2078 * revision information are used to select the right firmware.
2079 *
2080 * Currently this bootloader support is limited to hardware variant
2081 * iBT 3.0 (LnP/SfP) which is identified by the value 11 (0x0b).
2082 */
2083 snprintf(fwname, sizeof(fwname), "intel/ibt-11-%u.sfi",
2084 le16_to_cpu(params->dev_revid));
2085
2086 err = request_firmware(&fw, fwname, &hdev->dev);
2087 if (err < 0) {
2088 BT_ERR("%s: Failed to load Intel firmware file (%d)",
2089 hdev->name, err);
2090 kfree_skb(skb);
2091 return err;
2092 }
2093
2094 BT_INFO("%s: Found device firmware: %s", hdev->name, fwname);
2095
2096 kfree_skb(skb);
2097
2098 if (fw->size < 644) {
2099 BT_ERR("%s: Invalid size of firmware file (%zu)",
2100 hdev->name, fw->size);
2101 err = -EBADF;
2102 goto done;
2103 }
2104
2105 set_bit(BTUSB_DOWNLOADING, &data->flags);
2106
2107 /* Start the firmware download transaction with the Init fragment
2108 * represented by the 128 bytes of CSS header.
2109 */
2110 err = btusb_intel_secure_send(hdev, 0x00, 128, fw->data);
2111 if (err < 0) {
2112 BT_ERR("%s: Failed to send firmware header (%d)",
2113 hdev->name, err);
2114 goto done;
2115 }
2116
2117 /* Send the 256 bytes of public key information from the firmware
2118 * as the PKey fragment.
2119 */
2120 err = btusb_intel_secure_send(hdev, 0x03, 256, fw->data + 128);
2121 if (err < 0) {
2122 BT_ERR("%s: Failed to send firmware public key (%d)",
2123 hdev->name, err);
2124 goto done;
2125 }
2126
2127 /* Send the 256 bytes of signature information from the firmware
2128 * as the Sign fragment.
2129 */
2130 err = btusb_intel_secure_send(hdev, 0x02, 256, fw->data + 388);
2131 if (err < 0) {
2132 BT_ERR("%s: Failed to send firmware signature (%d)",
2133 hdev->name, err);
2134 goto done;
2135 }
2136
2137 fw_ptr = fw->data + 644;
2138
2139 while (fw_ptr - fw->data < fw->size) {
2140 struct hci_command_hdr *cmd = (void *)fw_ptr;
2141 u8 cmd_len;
2142
2143 cmd_len = sizeof(*cmd) + cmd->plen;
2144
2145 /* Send each command from the firmware data buffer as
2146 * a single Data fragment.
2147 */
2148 err = btusb_intel_secure_send(hdev, 0x01, cmd_len, fw_ptr);
2149 if (err < 0) {
2150 BT_ERR("%s: Failed to send firmware data (%d)",
2151 hdev->name, err);
2152 goto done;
2153 }
2154
2155 fw_ptr += cmd_len;
2156 }
2157
Marcel Holtmannce6bb922015-01-28 01:58:40 -08002158 set_bit(BTUSB_FIRMWARE_LOADED, &data->flags);
2159
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002160 /* Before switching the device into operational mode and with that
2161 * booting the loaded firmware, wait for the bootloader notification
2162 * that all fragments have been successfully received.
2163 *
2164 * When the event processing receives the notification, then this
2165 * flag will be cleared. So just in case that happens really quickly,
2166 * check it first before adding the wait queue.
2167 */
2168 if (test_bit(BTUSB_DOWNLOADING, &data->flags)) {
2169 DECLARE_WAITQUEUE(wait, current);
2170 signed long timeout;
2171
2172 BT_INFO("%s: Waiting for firmware download to complete",
2173 hdev->name);
2174
2175 add_wait_queue(&hdev->req_wait_q, &wait);
2176 set_current_state(TASK_INTERRUPTIBLE);
2177
2178 /* The firmware loading should not take longer than 5 seconds
2179 * and thus just timeout if that happens and fail the setup
2180 * of this device.
2181 */
2182 timeout = schedule_timeout(msecs_to_jiffies(5000));
2183
2184 remove_wait_queue(&hdev->req_wait_q, &wait);
2185
2186 if (signal_pending(current)) {
2187 BT_ERR("%s: Firmware loading interrupted", hdev->name);
2188 err = -EINTR;
2189 goto done;
2190 }
2191
2192 if (!timeout) {
2193 BT_ERR("%s: Firmware loading timeout", hdev->name);
2194 err = -ETIMEDOUT;
2195 goto done;
2196 }
2197 }
2198
2199 if (test_bit(BTUSB_FIRMWARE_FAILED, &data->flags)) {
2200 BT_ERR("%s: Firmware loading failed", hdev->name);
2201 err = -ENOEXEC;
2202 goto done;
2203 }
2204
2205 rettime = ktime_get();
2206 delta = ktime_sub(rettime, calltime);
2207 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
2208
2209 BT_INFO("%s: Firmware loaded in %llu usecs", hdev->name, duration);
2210
2211done:
2212 release_firmware(fw);
2213
2214 if (err < 0)
2215 return err;
2216
2217 calltime = ktime_get();
2218
2219 set_bit(BTUSB_BOOTING, &data->flags);
2220
2221 skb = __hci_cmd_sync(hdev, 0xfc01, sizeof(reset_param), reset_param,
2222 HCI_INIT_TIMEOUT);
2223 if (IS_ERR(skb))
2224 return PTR_ERR(skb);
2225
2226 kfree_skb(skb);
2227
2228 /* The bootloader will not indicate when the device is ready. This
2229 * is done by the operational firmware sending bootup notification.
2230 */
2231 if (test_bit(BTUSB_BOOTING, &data->flags)) {
2232 DECLARE_WAITQUEUE(wait, current);
2233 signed long timeout;
2234
2235 BT_INFO("%s: Waiting for device to boot", hdev->name);
2236
2237 add_wait_queue(&hdev->req_wait_q, &wait);
2238 set_current_state(TASK_INTERRUPTIBLE);
2239
2240 /* Booting into operational firmware should not take
2241 * longer than 1 second. However if that happens, then
2242 * just fail the setup since something went wrong.
2243 */
2244 timeout = schedule_timeout(msecs_to_jiffies(1000));
2245
2246 remove_wait_queue(&hdev->req_wait_q, &wait);
2247
2248 if (signal_pending(current)) {
2249 BT_ERR("%s: Device boot interrupted", hdev->name);
2250 return -EINTR;
2251 }
2252
2253 if (!timeout) {
2254 BT_ERR("%s: Device boot timeout", hdev->name);
2255 return -ETIMEDOUT;
2256 }
2257 }
2258
2259 rettime = ktime_get();
2260 delta = ktime_sub(rettime, calltime);
2261 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
2262
2263 BT_INFO("%s: Device booted in %llu usecs", hdev->name, duration);
2264
2265 clear_bit(BTUSB_BOOTLOADER, &data->flags);
2266
2267 return 0;
2268}
2269
Marcel Holtmann385a7682015-01-28 11:09:56 -08002270static void btusb_hw_error_intel(struct hci_dev *hdev, u8 code)
2271{
2272 struct sk_buff *skb;
2273 u8 type = 0x00;
2274
2275 BT_ERR("%s: Hardware error 0x%2.2x", hdev->name, code);
2276
2277 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
2278 if (IS_ERR(skb)) {
2279 BT_ERR("%s: Reset after hardware error failed (%ld)",
2280 hdev->name, PTR_ERR(skb));
2281 return;
2282 }
2283 kfree_skb(skb);
2284
2285 skb = __hci_cmd_sync(hdev, 0xfc22, 1, &type, HCI_INIT_TIMEOUT);
2286 if (IS_ERR(skb)) {
2287 BT_ERR("%s: Retrieving Intel exception info failed (%ld)",
2288 hdev->name, PTR_ERR(skb));
2289 return;
2290 }
2291
2292 if (skb->len != 13) {
2293 BT_ERR("%s: Exception info size mismatch", hdev->name);
2294 kfree_skb(skb);
2295 return;
2296 }
2297
2298 if (skb->data[0] != 0x00) {
2299 BT_ERR("%s: Exception info command failure (%02x)",
2300 hdev->name, skb->data[0]);
2301 kfree_skb(skb);
2302 return;
2303 }
2304
2305 BT_ERR("%s: Exception info %s", hdev->name, (char *)(skb->data + 1));
2306
2307 kfree_skb(skb);
2308}
2309
Marcel Holtmanncb8d6592014-07-02 11:25:25 +02002310static int btusb_set_bdaddr_intel(struct hci_dev *hdev, const bdaddr_t *bdaddr)
2311{
2312 struct sk_buff *skb;
2313 long ret;
2314
2315 skb = __hci_cmd_sync(hdev, 0xfc31, 6, bdaddr, HCI_INIT_TIMEOUT);
2316 if (IS_ERR(skb)) {
2317 ret = PTR_ERR(skb);
2318 BT_ERR("%s: changing Intel device address failed (%ld)",
Marcel Holtmann89e75332014-09-16 04:44:50 +02002319 hdev->name, ret);
Marcel Holtmanncb8d6592014-07-02 11:25:25 +02002320 return ret;
2321 }
2322 kfree_skb(skb);
2323
2324 return 0;
2325}
2326
Amitkumar Karwarae8df492014-07-18 14:47:06 -07002327static int btusb_set_bdaddr_marvell(struct hci_dev *hdev,
2328 const bdaddr_t *bdaddr)
2329{
2330 struct sk_buff *skb;
2331 u8 buf[8];
2332 long ret;
2333
2334 buf[0] = 0xfe;
2335 buf[1] = sizeof(bdaddr_t);
2336 memcpy(buf + 2, bdaddr, sizeof(bdaddr_t));
2337
2338 skb = __hci_cmd_sync(hdev, 0xfc22, sizeof(buf), buf, HCI_INIT_TIMEOUT);
2339 if (IS_ERR(skb)) {
2340 ret = PTR_ERR(skb);
2341 BT_ERR("%s: changing Marvell device address failed (%ld)",
2342 hdev->name, ret);
2343 return ret;
2344 }
2345 kfree_skb(skb);
2346
2347 return 0;
2348}
2349
Marcel Holtmannc8abb732014-07-02 12:38:22 +02002350#define BDADDR_BCM20702A0 (&(bdaddr_t) {{0x00, 0xa0, 0x02, 0x70, 0x20, 0x00}})
2351
Petri Gynther10d4c672014-05-08 15:50:01 -07002352static int btusb_setup_bcm_patchram(struct hci_dev *hdev)
2353{
2354 struct btusb_data *data = hci_get_drvdata(hdev);
2355 struct usb_device *udev = data->udev;
2356 char fw_name[64];
2357 const struct firmware *fw;
2358 const u8 *fw_ptr;
2359 size_t fw_size;
2360 const struct hci_command_hdr *cmd;
2361 const u8 *cmd_param;
2362 u16 opcode;
2363 struct sk_buff *skb;
2364 struct hci_rp_read_local_version *ver;
Marcel Holtmannc8abb732014-07-02 12:38:22 +02002365 struct hci_rp_read_bd_addr *bda;
Petri Gynther10d4c672014-05-08 15:50:01 -07002366 long ret;
2367
2368 snprintf(fw_name, sizeof(fw_name), "brcm/%s-%04x-%04x.hcd",
2369 udev->product ? udev->product : "BCM",
2370 le16_to_cpu(udev->descriptor.idVendor),
2371 le16_to_cpu(udev->descriptor.idProduct));
2372
2373 ret = request_firmware(&fw, fw_name, &hdev->dev);
2374 if (ret < 0) {
Marcel Holtmannc8abb732014-07-02 12:38:22 +02002375 BT_INFO("%s: BCM: patch %s not found", hdev->name, fw_name);
Petri Gynther10d4c672014-05-08 15:50:01 -07002376 return 0;
2377 }
2378
2379 /* Reset */
2380 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
2381 if (IS_ERR(skb)) {
2382 ret = PTR_ERR(skb);
2383 BT_ERR("%s: HCI_OP_RESET failed (%ld)", hdev->name, ret);
2384 goto done;
2385 }
2386 kfree_skb(skb);
2387
2388 /* Read Local Version Info */
2389 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
2390 HCI_INIT_TIMEOUT);
2391 if (IS_ERR(skb)) {
2392 ret = PTR_ERR(skb);
2393 BT_ERR("%s: HCI_OP_READ_LOCAL_VERSION failed (%ld)",
Marcel Holtmann89e75332014-09-16 04:44:50 +02002394 hdev->name, ret);
Petri Gynther10d4c672014-05-08 15:50:01 -07002395 goto done;
2396 }
2397
2398 if (skb->len != sizeof(*ver)) {
2399 BT_ERR("%s: HCI_OP_READ_LOCAL_VERSION event length mismatch",
Marcel Holtmann89e75332014-09-16 04:44:50 +02002400 hdev->name);
Petri Gynther10d4c672014-05-08 15:50:01 -07002401 kfree_skb(skb);
2402 ret = -EIO;
2403 goto done;
2404 }
2405
Marcel Holtmann89e75332014-09-16 04:44:50 +02002406 ver = (struct hci_rp_read_local_version *)skb->data;
Petri Gynther10d4c672014-05-08 15:50:01 -07002407 BT_INFO("%s: BCM: patching hci_ver=%02x hci_rev=%04x lmp_ver=%02x "
2408 "lmp_subver=%04x", hdev->name, ver->hci_ver, ver->hci_rev,
2409 ver->lmp_ver, ver->lmp_subver);
2410 kfree_skb(skb);
2411
2412 /* Start Download */
2413 skb = __hci_cmd_sync(hdev, 0xfc2e, 0, NULL, HCI_INIT_TIMEOUT);
2414 if (IS_ERR(skb)) {
2415 ret = PTR_ERR(skb);
2416 BT_ERR("%s: BCM: Download Minidrv command failed (%ld)",
Marcel Holtmann89e75332014-09-16 04:44:50 +02002417 hdev->name, ret);
Petri Gynther10d4c672014-05-08 15:50:01 -07002418 goto reset_fw;
2419 }
2420 kfree_skb(skb);
2421
2422 /* 50 msec delay after Download Minidrv completes */
2423 msleep(50);
2424
2425 fw_ptr = fw->data;
2426 fw_size = fw->size;
2427
2428 while (fw_size >= sizeof(*cmd)) {
Marcel Holtmann89e75332014-09-16 04:44:50 +02002429 cmd = (struct hci_command_hdr *)fw_ptr;
Petri Gynther10d4c672014-05-08 15:50:01 -07002430 fw_ptr += sizeof(*cmd);
2431 fw_size -= sizeof(*cmd);
2432
2433 if (fw_size < cmd->plen) {
2434 BT_ERR("%s: BCM: patch %s is corrupted",
Marcel Holtmann89e75332014-09-16 04:44:50 +02002435 hdev->name, fw_name);
Petri Gynther10d4c672014-05-08 15:50:01 -07002436 ret = -EINVAL;
2437 goto reset_fw;
2438 }
2439
2440 cmd_param = fw_ptr;
2441 fw_ptr += cmd->plen;
2442 fw_size -= cmd->plen;
2443
2444 opcode = le16_to_cpu(cmd->opcode);
2445
2446 skb = __hci_cmd_sync(hdev, opcode, cmd->plen, cmd_param,
2447 HCI_INIT_TIMEOUT);
2448 if (IS_ERR(skb)) {
2449 ret = PTR_ERR(skb);
2450 BT_ERR("%s: BCM: patch command %04x failed (%ld)",
Marcel Holtmann89e75332014-09-16 04:44:50 +02002451 hdev->name, opcode, ret);
Petri Gynther10d4c672014-05-08 15:50:01 -07002452 goto reset_fw;
2453 }
2454 kfree_skb(skb);
2455 }
2456
2457 /* 250 msec delay after Launch Ram completes */
2458 msleep(250);
2459
2460reset_fw:
2461 /* Reset */
2462 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
2463 if (IS_ERR(skb)) {
2464 ret = PTR_ERR(skb);
2465 BT_ERR("%s: HCI_OP_RESET failed (%ld)", hdev->name, ret);
2466 goto done;
2467 }
2468 kfree_skb(skb);
2469
2470 /* Read Local Version Info */
2471 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
2472 HCI_INIT_TIMEOUT);
2473 if (IS_ERR(skb)) {
2474 ret = PTR_ERR(skb);
2475 BT_ERR("%s: HCI_OP_READ_LOCAL_VERSION failed (%ld)",
Marcel Holtmann89e75332014-09-16 04:44:50 +02002476 hdev->name, ret);
Petri Gynther10d4c672014-05-08 15:50:01 -07002477 goto done;
2478 }
2479
2480 if (skb->len != sizeof(*ver)) {
2481 BT_ERR("%s: HCI_OP_READ_LOCAL_VERSION event length mismatch",
Marcel Holtmann89e75332014-09-16 04:44:50 +02002482 hdev->name);
Petri Gynther10d4c672014-05-08 15:50:01 -07002483 kfree_skb(skb);
2484 ret = -EIO;
2485 goto done;
2486 }
2487
Marcel Holtmann89e75332014-09-16 04:44:50 +02002488 ver = (struct hci_rp_read_local_version *)skb->data;
Petri Gynther10d4c672014-05-08 15:50:01 -07002489 BT_INFO("%s: BCM: firmware hci_ver=%02x hci_rev=%04x lmp_ver=%02x "
2490 "lmp_subver=%04x", hdev->name, ver->hci_ver, ver->hci_rev,
2491 ver->lmp_ver, ver->lmp_subver);
2492 kfree_skb(skb);
2493
Marcel Holtmannc8abb732014-07-02 12:38:22 +02002494 /* Read BD Address */
2495 skb = __hci_cmd_sync(hdev, HCI_OP_READ_BD_ADDR, 0, NULL,
2496 HCI_INIT_TIMEOUT);
2497 if (IS_ERR(skb)) {
2498 ret = PTR_ERR(skb);
2499 BT_ERR("%s: HCI_OP_READ_BD_ADDR failed (%ld)",
Marcel Holtmann89e75332014-09-16 04:44:50 +02002500 hdev->name, ret);
Marcel Holtmannc8abb732014-07-02 12:38:22 +02002501 goto done;
2502 }
2503
2504 if (skb->len != sizeof(*bda)) {
2505 BT_ERR("%s: HCI_OP_READ_BD_ADDR event length mismatch",
Marcel Holtmann89e75332014-09-16 04:44:50 +02002506 hdev->name);
Marcel Holtmannc8abb732014-07-02 12:38:22 +02002507 kfree_skb(skb);
2508 ret = -EIO;
2509 goto done;
2510 }
2511
Marcel Holtmann89e75332014-09-16 04:44:50 +02002512 bda = (struct hci_rp_read_bd_addr *)skb->data;
Marcel Holtmannc8abb732014-07-02 12:38:22 +02002513 if (bda->status) {
2514 BT_ERR("%s: HCI_OP_READ_BD_ADDR error status (%02x)",
2515 hdev->name, bda->status);
2516 kfree_skb(skb);
2517 ret = -bt_to_errno(bda->status);
2518 goto done;
2519 }
2520
2521 /* The address 00:20:70:02:A0:00 indicates a BCM20702A0 controller
2522 * with no configured address.
2523 */
Marcel Holtmann849e5082014-07-04 16:54:39 +02002524 if (!bacmp(&bda->bdaddr, BDADDR_BCM20702A0)) {
Marcel Holtmannc8abb732014-07-02 12:38:22 +02002525 BT_INFO("%s: BCM: using default device address (%pMR)",
2526 hdev->name, &bda->bdaddr);
Marcel Holtmann849e5082014-07-04 16:54:39 +02002527 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
2528 }
Marcel Holtmannc8abb732014-07-02 12:38:22 +02002529
2530 kfree_skb(skb);
2531
Petri Gynther10d4c672014-05-08 15:50:01 -07002532done:
2533 release_firmware(fw);
2534
2535 return ret;
2536}
2537
Marcel Holtmannabbaf502014-07-02 00:53:48 +02002538static int btusb_set_bdaddr_bcm(struct hci_dev *hdev, const bdaddr_t *bdaddr)
2539{
2540 struct sk_buff *skb;
2541 long ret;
2542
2543 skb = __hci_cmd_sync(hdev, 0xfc01, 6, bdaddr, HCI_INIT_TIMEOUT);
2544 if (IS_ERR(skb)) {
2545 ret = PTR_ERR(skb);
2546 BT_ERR("%s: BCM: Change address command failed (%ld)",
Marcel Holtmann89e75332014-09-16 04:44:50 +02002547 hdev->name, ret);
Marcel Holtmannabbaf502014-07-02 00:53:48 +02002548 return ret;
2549 }
2550 kfree_skb(skb);
2551
2552 return 0;
2553}
2554
Toshi Kikuchi58592232014-12-12 10:58:05 -08002555static int btusb_set_bdaddr_ath3012(struct hci_dev *hdev,
2556 const bdaddr_t *bdaddr)
2557{
2558 struct sk_buff *skb;
2559 u8 buf[10];
2560 long ret;
2561
2562 buf[0] = 0x01;
2563 buf[1] = 0x01;
2564 buf[2] = 0x00;
2565 buf[3] = sizeof(bdaddr_t);
2566 memcpy(buf + 4, bdaddr, sizeof(bdaddr_t));
2567
2568 skb = __hci_cmd_sync(hdev, 0xfc0b, sizeof(buf), buf, HCI_INIT_TIMEOUT);
2569 if (IS_ERR(skb)) {
2570 ret = PTR_ERR(skb);
2571 BT_ERR("%s: Change address command failed (%ld)",
2572 hdev->name, ret);
2573 return ret;
2574 }
2575 kfree_skb(skb);
2576
2577 return 0;
2578}
2579
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002580static int btusb_probe(struct usb_interface *intf,
Marcel Holtmann89e75332014-09-16 04:44:50 +02002581 const struct usb_device_id *id)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002582{
2583 struct usb_endpoint_descriptor *ep_desc;
2584 struct btusb_data *data;
2585 struct hci_dev *hdev;
2586 int i, err;
2587
2588 BT_DBG("intf %p id %p", intf, id);
2589
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002590 /* interface numbers are hardcoded in the spec */
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002591 if (intf->cur_altsetting->desc.bInterfaceNumber != 0)
2592 return -ENODEV;
2593
2594 if (!id->driver_info) {
2595 const struct usb_device_id *match;
Marcel Holtmann89e75332014-09-16 04:44:50 +02002596
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002597 match = usb_match_id(intf, blacklist_table);
2598 if (match)
2599 id = match;
2600 }
2601
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002602 if (id->driver_info == BTUSB_IGNORE)
2603 return -ENODEV;
2604
Steven.Li2d25f8b2011-07-01 14:02:36 +08002605 if (id->driver_info & BTUSB_ATH3012) {
2606 struct usb_device *udev = interface_to_usbdev(intf);
2607
2608 /* Old firmware would otherwise let ath3k driver load
2609 * patch and sysconfig files */
2610 if (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x0001)
2611 return -ENODEV;
2612 }
2613
Sachin Kamat98921db2012-07-27 12:38:39 +05302614 data = devm_kzalloc(&intf->dev, sizeof(*data), GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002615 if (!data)
2616 return -ENOMEM;
2617
2618 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
2619 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
2620
2621 if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
2622 data->intr_ep = ep_desc;
2623 continue;
2624 }
2625
2626 if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
2627 data->bulk_tx_ep = ep_desc;
2628 continue;
2629 }
2630
2631 if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
2632 data->bulk_rx_ep = ep_desc;
2633 continue;
2634 }
2635 }
2636
Sachin Kamat98921db2012-07-27 12:38:39 +05302637 if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002638 return -ENODEV;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002639
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002640 data->cmdreq_type = USB_TYPE_CLASS;
2641
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002642 data->udev = interface_to_usbdev(intf);
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02002643 data->intf = intf;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002644
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002645 INIT_WORK(&data->work, btusb_work);
Oliver Neukum7bee5492009-08-24 23:44:59 +02002646 INIT_WORK(&data->waker, btusb_waker);
Marcel Holtmann803b5832014-09-16 08:00:29 +02002647 init_usb_anchor(&data->deferred);
2648 init_usb_anchor(&data->tx_anchor);
Oliver Neukum7bee5492009-08-24 23:44:59 +02002649 spin_lock_init(&data->txlock);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002650
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002651 init_usb_anchor(&data->intr_anchor);
2652 init_usb_anchor(&data->bulk_anchor);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02002653 init_usb_anchor(&data->isoc_anchor);
Marcel Holtmann803b5832014-09-16 08:00:29 +02002654 spin_lock_init(&data->rxlock);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002655
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002656 if (id->driver_info & BTUSB_INTEL_NEW) {
2657 data->recv_event = btusb_recv_event_intel;
2658 data->recv_bulk = btusb_recv_bulk_intel;
2659 set_bit(BTUSB_BOOTLOADER, &data->flags);
2660 } else {
2661 data->recv_event = hci_recv_frame;
2662 data->recv_bulk = btusb_recv_bulk;
2663 }
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +01002664
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002665 hdev = hci_alloc_dev();
Sachin Kamat98921db2012-07-27 12:38:39 +05302666 if (!hdev)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002667 return -ENOMEM;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002668
Marcel Holtmannc13854c2010-02-08 15:27:07 +01002669 hdev->bus = HCI_USB;
David Herrmann155961e2012-02-09 21:58:32 +01002670 hci_set_drvdata(hdev, data);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002671
2672 data->hdev = hdev;
2673
2674 SET_HCIDEV_DEV(hdev, &intf->dev);
2675
Marcel Holtmann9f8f9622013-04-10 08:11:35 -07002676 hdev->open = btusb_open;
2677 hdev->close = btusb_close;
2678 hdev->flush = btusb_flush;
2679 hdev->send = btusb_send_frame;
2680 hdev->notify = btusb_notify;
2681
2682 if (id->driver_info & BTUSB_BCM92035)
2683 hdev->setup = btusb_setup_bcm92035;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002684
Marcel Holtmannabbaf502014-07-02 00:53:48 +02002685 if (id->driver_info & BTUSB_BCM_PATCHRAM) {
Petri Gynther10d4c672014-05-08 15:50:01 -07002686 hdev->setup = btusb_setup_bcm_patchram;
Marcel Holtmannabbaf502014-07-02 00:53:48 +02002687 hdev->set_bdaddr = btusb_set_bdaddr_bcm;
Marcel Holtmann27c3fbe2014-11-02 20:52:24 +01002688 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
Marcel Holtmannabbaf502014-07-02 00:53:48 +02002689 }
Petri Gynther10d4c672014-05-08 15:50:01 -07002690
Marcel Holtmanncb8d6592014-07-02 11:25:25 +02002691 if (id->driver_info & BTUSB_INTEL) {
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002692 hdev->setup = btusb_setup_intel;
Marcel Holtmann385a7682015-01-28 11:09:56 -08002693 hdev->hw_error = btusb_hw_error_intel;
Marcel Holtmanncb8d6592014-07-02 11:25:25 +02002694 hdev->set_bdaddr = btusb_set_bdaddr_intel;
2695 }
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002696
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002697 if (id->driver_info & BTUSB_INTEL_NEW) {
2698 hdev->send = btusb_send_frame_intel;
2699 hdev->setup = btusb_setup_intel_new;
Marcel Holtmann385a7682015-01-28 11:09:56 -08002700 hdev->hw_error = btusb_hw_error_intel;
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002701 hdev->set_bdaddr = btusb_set_bdaddr_intel;
2702 }
2703
Amitkumar Karwarae8df492014-07-18 14:47:06 -07002704 if (id->driver_info & BTUSB_MARVELL)
2705 hdev->set_bdaddr = btusb_set_bdaddr_marvell;
2706
Marcel Holtmann661cf882015-01-02 23:35:20 -08002707 if (id->driver_info & BTUSB_SWAVE) {
2708 set_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks);
Marcel Holtmannd57dbe72014-12-26 04:42:33 +01002709 set_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS, &hdev->quirks);
Marcel Holtmann661cf882015-01-02 23:35:20 -08002710 }
Marcel Holtmannd57dbe72014-12-26 04:42:33 +01002711
Marcel Holtmann40df7832014-07-06 13:29:58 +02002712 if (id->driver_info & BTUSB_INTEL_BOOT)
2713 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
2714
Toshi Kikuchi58592232014-12-12 10:58:05 -08002715 if (id->driver_info & BTUSB_ATH3012)
2716 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
2717
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002718 /* Interface numbers are hardcoded in the specification */
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02002719 data->isoc = usb_ifnum_to_if(data->udev, 1);
2720
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002721 if (!reset)
Szymon Janca6c511c2012-05-23 12:35:46 +02002722 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002723
2724 if (force_scofix || id->driver_info & BTUSB_WRONG_SCO_MTU) {
2725 if (!disable_scofix)
2726 set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
2727 }
2728
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02002729 if (id->driver_info & BTUSB_BROKEN_ISOC)
2730 data->isoc = NULL;
2731
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002732 if (id->driver_info & BTUSB_DIGIANSWER) {
2733 data->cmdreq_type = USB_TYPE_VENDOR;
Szymon Janca6c511c2012-05-23 12:35:46 +02002734 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002735 }
2736
2737 if (id->driver_info & BTUSB_CSR) {
2738 struct usb_device *udev = data->udev;
Marcel Holtmann81cac642014-01-03 03:02:36 -08002739 u16 bcdDevice = le16_to_cpu(udev->descriptor.bcdDevice);
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002740
2741 /* Old firmware would otherwise execute USB reset */
Marcel Holtmann81cac642014-01-03 03:02:36 -08002742 if (bcdDevice < 0x117)
Szymon Janca6c511c2012-05-23 12:35:46 +02002743 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmann81cac642014-01-03 03:02:36 -08002744
2745 /* Fake CSR devices with broken commands */
2746 if (bcdDevice <= 0x100)
2747 hdev->setup = btusb_setup_csr;
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002748 }
2749
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002750 if (id->driver_info & BTUSB_SNIFFER) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02002751 struct usb_device *udev = data->udev;
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002752
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002753 /* New sniffer firmware has crippled HCI interface */
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002754 if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
2755 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
2756 }
2757
Marcel Holtmann3a5ef202014-07-06 14:53:54 +02002758 if (id->driver_info & BTUSB_INTEL_BOOT) {
2759 /* A bug in the bootloader causes that interrupt interface is
2760 * only enabled after receiving SetInterface(0, AltSetting=0).
2761 */
2762 err = usb_set_interface(data->udev, 0, 0);
2763 if (err < 0) {
2764 BT_ERR("failed to set interface 0, alt 0 %d", err);
2765 hci_free_dev(hdev);
2766 return err;
2767 }
2768 }
2769
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02002770 if (data->isoc) {
2771 err = usb_driver_claim_interface(&btusb_driver,
Marcel Holtmann89e75332014-09-16 04:44:50 +02002772 data->isoc, data);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02002773 if (err < 0) {
2774 hci_free_dev(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02002775 return err;
2776 }
2777 }
2778
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002779 err = hci_register_dev(hdev);
2780 if (err < 0) {
2781 hci_free_dev(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002782 return err;
2783 }
2784
2785 usb_set_intfdata(intf, data);
2786
2787 return 0;
2788}
2789
2790static void btusb_disconnect(struct usb_interface *intf)
2791{
2792 struct btusb_data *data = usb_get_intfdata(intf);
2793 struct hci_dev *hdev;
2794
2795 BT_DBG("intf %p", intf);
2796
2797 if (!data)
2798 return;
2799
2800 hdev = data->hdev;
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02002801 usb_set_intfdata(data->intf, NULL);
2802
2803 if (data->isoc)
2804 usb_set_intfdata(data->isoc, NULL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002805
2806 hci_unregister_dev(hdev);
2807
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02002808 if (intf == data->isoc)
2809 usb_driver_release_interface(&btusb_driver, data->intf);
2810 else if (data->isoc)
2811 usb_driver_release_interface(&btusb_driver, data->isoc);
2812
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002813 hci_free_dev(hdev);
2814}
2815
Oliver Neukum7bee5492009-08-24 23:44:59 +02002816#ifdef CONFIG_PM
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002817static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
2818{
2819 struct btusb_data *data = usb_get_intfdata(intf);
2820
2821 BT_DBG("intf %p", intf);
2822
2823 if (data->suspend_count++)
2824 return 0;
2825
Oliver Neukum7bee5492009-08-24 23:44:59 +02002826 spin_lock_irq(&data->txlock);
Alan Stern5b1b0b82011-08-19 23:49:48 +02002827 if (!(PMSG_IS_AUTO(message) && data->tx_in_flight)) {
Oliver Neukum7bee5492009-08-24 23:44:59 +02002828 set_bit(BTUSB_SUSPENDING, &data->flags);
2829 spin_unlock_irq(&data->txlock);
2830 } else {
2831 spin_unlock_irq(&data->txlock);
2832 data->suspend_count--;
2833 return -EBUSY;
2834 }
2835
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002836 cancel_work_sync(&data->work);
2837
Oliver Neukum7bee5492009-08-24 23:44:59 +02002838 btusb_stop_traffic(data);
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002839 usb_kill_anchored_urbs(&data->tx_anchor);
2840
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002841 return 0;
2842}
2843
Oliver Neukum7bee5492009-08-24 23:44:59 +02002844static void play_deferred(struct btusb_data *data)
2845{
2846 struct urb *urb;
2847 int err;
2848
2849 while ((urb = usb_get_from_anchor(&data->deferred))) {
2850 err = usb_submit_urb(urb, GFP_ATOMIC);
2851 if (err < 0)
2852 break;
2853
2854 data->tx_in_flight++;
2855 }
2856 usb_scuttle_anchored_urbs(&data->deferred);
2857}
2858
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002859static int btusb_resume(struct usb_interface *intf)
2860{
2861 struct btusb_data *data = usb_get_intfdata(intf);
2862 struct hci_dev *hdev = data->hdev;
Oliver Neukum7bee5492009-08-24 23:44:59 +02002863 int err = 0;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002864
2865 BT_DBG("intf %p", intf);
2866
2867 if (--data->suspend_count)
2868 return 0;
2869
2870 if (!test_bit(HCI_RUNNING, &hdev->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +02002871 goto done;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002872
2873 if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
2874 err = btusb_submit_intr_urb(hdev, GFP_NOIO);
2875 if (err < 0) {
2876 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02002877 goto failed;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002878 }
2879 }
2880
2881 if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) {
Marcel Holtmann43c2e572009-02-04 17:41:38 +01002882 err = btusb_submit_bulk_urb(hdev, GFP_NOIO);
2883 if (err < 0) {
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002884 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02002885 goto failed;
2886 }
2887
2888 btusb_submit_bulk_urb(hdev, GFP_NOIO);
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002889 }
2890
2891 if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
2892 if (btusb_submit_isoc_urb(hdev, GFP_NOIO) < 0)
2893 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
2894 else
2895 btusb_submit_isoc_urb(hdev, GFP_NOIO);
2896 }
2897
Oliver Neukum7bee5492009-08-24 23:44:59 +02002898 spin_lock_irq(&data->txlock);
2899 play_deferred(data);
2900 clear_bit(BTUSB_SUSPENDING, &data->flags);
2901 spin_unlock_irq(&data->txlock);
2902 schedule_work(&data->work);
2903
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002904 return 0;
Oliver Neukum7bee5492009-08-24 23:44:59 +02002905
2906failed:
2907 usb_scuttle_anchored_urbs(&data->deferred);
2908done:
2909 spin_lock_irq(&data->txlock);
2910 clear_bit(BTUSB_SUSPENDING, &data->flags);
2911 spin_unlock_irq(&data->txlock);
2912
2913 return err;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002914}
Oliver Neukum7bee5492009-08-24 23:44:59 +02002915#endif
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002916
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002917static struct usb_driver btusb_driver = {
2918 .name = "btusb",
2919 .probe = btusb_probe,
2920 .disconnect = btusb_disconnect,
Oliver Neukum7bee5492009-08-24 23:44:59 +02002921#ifdef CONFIG_PM
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002922 .suspend = btusb_suspend,
2923 .resume = btusb_resume,
Oliver Neukum7bee5492009-08-24 23:44:59 +02002924#endif
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002925 .id_table = btusb_table,
Oliver Neukum7bee5492009-08-24 23:44:59 +02002926 .supports_autosuspend = 1,
Sarah Sharpe1f12eb2012-04-23 10:08:51 -07002927 .disable_hub_initiated_lpm = 1,
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002928};
2929
Greg Kroah-Hartman93f15082011-11-18 09:47:34 -08002930module_usb_driver(btusb_driver);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002931
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002932module_param(disable_scofix, bool, 0644);
2933MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
2934
2935module_param(force_scofix, bool, 0644);
2936MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
2937
2938module_param(reset, bool, 0644);
2939MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
2940
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002941MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
2942MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
2943MODULE_VERSION(VERSION);
2944MODULE_LICENSE("GPL");