blob: 4fac6927ee11b0a3942c119f1e7c2391e1eb97c8 [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 Dunn9a5abda2015-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 Holtmannd92f2df02014-07-06 14:53:55 +0200136 { USB_DEVICE(0x8087, 0x0a5a),
137 .driver_info = BTUSB_INTEL_BOOT | BTUSB_BROKEN_ISOC },
Marcel Holtmann40df7832014-07-06 13:29:58 +0200138
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200139 { } /* Terminating entry */
140};
141
142MODULE_DEVICE_TABLE(usb, btusb_table);
143
Marcel Holtmann54265202013-10-11 07:46:18 -0700144static const struct usb_device_id blacklist_table[] = {
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200145 /* CSR BlueCore devices */
146 { USB_DEVICE(0x0a12, 0x0001), .driver_info = BTUSB_CSR },
147
148 /* Broadcom BCM2033 without firmware */
149 { USB_DEVICE(0x0a5c, 0x2033), .driver_info = BTUSB_IGNORE },
150
Bala Shanmugambe931122010-11-26 17:35:46 +0530151 /* Atheros 3011 with sflash firmware */
Andy Shevchenko0b880062014-02-18 18:26:19 +0200152 { USB_DEVICE(0x0489, 0xe027), .driver_info = BTUSB_IGNORE },
153 { USB_DEVICE(0x0489, 0xe03d), .driver_info = BTUSB_IGNORE },
154 { USB_DEVICE(0x0930, 0x0215), .driver_info = BTUSB_IGNORE },
Bala Shanmugambe931122010-11-26 17:35:46 +0530155 { USB_DEVICE(0x0cf3, 0x3002), .driver_info = BTUSB_IGNORE },
Marek Vasut6eda5412012-06-08 14:32:50 +0200156 { USB_DEVICE(0x0cf3, 0xe019), .driver_info = BTUSB_IGNORE },
Andy Ross2a7bccc2011-05-09 16:11:16 -0700157 { USB_DEVICE(0x13d3, 0x3304), .driver_info = BTUSB_IGNORE },
Bala Shanmugambe931122010-11-26 17:35:46 +0530158
Cho, Yu-Chen509e7862011-01-26 17:10:59 +0800159 /* Atheros AR9285 Malbec with sflash firmware */
160 { USB_DEVICE(0x03f0, 0x311d), .driver_info = BTUSB_IGNORE },
161
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530162 /* Atheros 3012 with sflash firmware */
Andy Shevchenko0b880062014-02-18 18:26:19 +0200163 { USB_DEVICE(0x0489, 0xe04d), .driver_info = BTUSB_ATH3012 },
164 { USB_DEVICE(0x0489, 0xe04e), .driver_info = BTUSB_ATH3012 },
165 { USB_DEVICE(0x0489, 0xe056), .driver_info = BTUSB_ATH3012 },
166 { USB_DEVICE(0x0489, 0xe057), .driver_info = BTUSB_ATH3012 },
167 { USB_DEVICE(0x0489, 0xe05f), .driver_info = BTUSB_ATH3012 },
Anantha Krishnan4b552bc2014-10-06 16:31:49 +0530168 { USB_DEVICE(0x0489, 0xe078), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200169 { USB_DEVICE(0x04c5, 0x1330), .driver_info = BTUSB_ATH3012 },
170 { USB_DEVICE(0x04ca, 0x3004), .driver_info = BTUSB_ATH3012 },
171 { USB_DEVICE(0x04ca, 0x3005), .driver_info = BTUSB_ATH3012 },
172 { USB_DEVICE(0x04ca, 0x3006), .driver_info = BTUSB_ATH3012 },
Mohammed Habibulla1fb4e092014-04-17 11:37:13 -0700173 { USB_DEVICE(0x04ca, 0x3007), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200174 { USB_DEVICE(0x04ca, 0x3008), .driver_info = BTUSB_ATH3012 },
175 { USB_DEVICE(0x04ca, 0x300b), .driver_info = BTUSB_ATH3012 },
Janne Heikkinen134d3b32014-12-09 07:44:51 +0200176 { USB_DEVICE(0x04ca, 0x3010), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200177 { USB_DEVICE(0x0930, 0x0219), .driver_info = BTUSB_ATH3012 },
178 { USB_DEVICE(0x0930, 0x0220), .driver_info = BTUSB_ATH3012 },
Vincent Zwanenburg89d29752014-08-08 12:33:56 +0100179 { USB_DEVICE(0x0930, 0x0227), .driver_info = BTUSB_ATH3012 },
Andy Shevchenkoa735f9e2014-02-18 18:26:20 +0200180 { USB_DEVICE(0x0b05, 0x17d0), .driver_info = BTUSB_ATH3012 },
Ming Leid66629c2013-03-15 11:00:39 +0800181 { USB_DEVICE(0x0cf3, 0x0036), .driver_info = BTUSB_ATH3012 },
Steven.Li2d25f8b2011-07-01 14:02:36 +0800182 { USB_DEVICE(0x0cf3, 0x3004), .driver_info = BTUSB_ATH3012 },
Sunguk Lee94a32d102013-03-12 04:41:58 +0900183 { USB_DEVICE(0x0cf3, 0x3008), .driver_info = BTUSB_ATH3012 },
Cho, Yu-Chen07c0ea82012-03-14 22:01:21 +0200184 { USB_DEVICE(0x0cf3, 0x311d), .driver_info = BTUSB_ATH3012 },
Oliver Neukumb1312372014-01-16 15:37:11 +0100185 { USB_DEVICE(0x0cf3, 0x311e), .driver_info = BTUSB_ATH3012 },
Oliver Neukum1e56f1e2014-01-16 16:02:58 +0100186 { USB_DEVICE(0x0cf3, 0x311f), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200187 { USB_DEVICE(0x0cf3, 0x3121), .driver_info = BTUSB_ATH3012 },
Ming Leiebaf5792013-03-18 23:45:11 +0800188 { USB_DEVICE(0x0cf3, 0x817a), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200189 { USB_DEVICE(0x0cf3, 0xe003), .driver_info = BTUSB_ATH3012 },
AceLan Kaoac713112012-04-19 14:53:45 +0800190 { USB_DEVICE(0x0cf3, 0xe004), .driver_info = BTUSB_ATH3012 },
Peng Chen0a3658c2013-08-30 17:41:40 +0800191 { USB_DEVICE(0x0cf3, 0xe005), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200192 { USB_DEVICE(0x13d3, 0x3362), .driver_info = BTUSB_ATH3012 },
193 { USB_DEVICE(0x13d3, 0x3375), .driver_info = BTUSB_ATH3012 },
AceLan Kaoeed307e2012-12-11 11:41:20 +0800194 { USB_DEVICE(0x13d3, 0x3393), .driver_info = BTUSB_ATH3012 },
Sujith Manoharan5b77a1f2013-07-15 09:29:03 +0530195 { USB_DEVICE(0x13d3, 0x3402), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin3bb30a7c2014-11-25 20:19:52 +0300196 { USB_DEVICE(0x13d3, 0x3408), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin033efa92015-01-18 00:16:51 +0300197 { USB_DEVICE(0x13d3, 0x3423), .driver_info = BTUSB_ATH3012 },
Anantha Krishnanfa2f1392014-07-08 19:25:08 +0530198 { USB_DEVICE(0x13d3, 0x3432), .driver_info = BTUSB_ATH3012 },
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530199
Cho, Yu-Chene9036e32011-02-15 10:20:07 +0800200 /* Atheros AR5BBU12 with sflash firmware */
201 { USB_DEVICE(0x0489, 0xe02c), .driver_info = BTUSB_IGNORE },
202
Michael Gruetzner85d59722012-05-02 22:33:40 +0200203 /* Atheros AR5BBU12 with sflash firmware */
Yevgeniy Melnichukbc21fde2012-08-07 19:48:10 +0530204 { USB_DEVICE(0x0489, 0xe036), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200205 { USB_DEVICE(0x0489, 0xe03c), .driver_info = BTUSB_ATH3012 },
Michael Gruetzner85d59722012-05-02 22:33:40 +0200206
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200207 /* Broadcom BCM2035 */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100208 { USB_DEVICE(0x0a5c, 0x2009), .driver_info = BTUSB_BCM92035 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200209 { USB_DEVICE(0x0a5c, 0x200a), .driver_info = BTUSB_WRONG_SCO_MTU },
210 { USB_DEVICE(0x0a5c, 0x2035), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200211
212 /* Broadcom BCM2045 */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100213 { USB_DEVICE(0x0a5c, 0x2039), .driver_info = BTUSB_WRONG_SCO_MTU },
214 { USB_DEVICE(0x0a5c, 0x2101), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmannbdbef3d2008-09-23 00:16:35 +0200215
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200216 /* IBM/Lenovo ThinkPad with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100217 { USB_DEVICE(0x0a5c, 0x201e), .driver_info = BTUSB_WRONG_SCO_MTU },
218 { USB_DEVICE(0x0a5c, 0x2110), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200219
220 /* HP laptop with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100221 { USB_DEVICE(0x03f0, 0x171d), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200222
223 /* Dell laptop with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100224 { USB_DEVICE(0x413c, 0x8126), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200225
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100226 /* Dell Wireless 370 and 410 devices */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100227 { USB_DEVICE(0x413c, 0x8152), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100228 { USB_DEVICE(0x413c, 0x8156), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200229
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100230 /* Belkin F8T012 and F8T013 devices */
231 { USB_DEVICE(0x050d, 0x0012), .driver_info = BTUSB_WRONG_SCO_MTU },
232 { USB_DEVICE(0x050d, 0x0013), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200233
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100234 /* Asus WL-BTD202 device */
235 { USB_DEVICE(0x0b05, 0x1715), .driver_info = BTUSB_WRONG_SCO_MTU },
236
237 /* Kensington Bluetooth USB adapter */
238 { USB_DEVICE(0x047d, 0x105e), .driver_info = BTUSB_WRONG_SCO_MTU },
239
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200240 /* RTX Telecom based adapters with buggy SCO support */
241 { USB_DEVICE(0x0400, 0x0807), .driver_info = BTUSB_BROKEN_ISOC },
242 { USB_DEVICE(0x0400, 0x080a), .driver_info = BTUSB_BROKEN_ISOC },
243
244 /* CONWISE Technology based adapters with buggy SCO support */
245 { USB_DEVICE(0x0e5e, 0x6622), .driver_info = BTUSB_BROKEN_ISOC },
246
Marcel Holtmann4fcef8e2015-01-01 17:34:37 -0800247 /* Roper Class 1 Bluetooth Dongle (Silicon Wave based) */
248 { USB_DEVICE(0x1300, 0x0001), .driver_info = BTUSB_SWAVE },
249
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200250 /* Digianswer devices */
251 { USB_DEVICE(0x08fd, 0x0001), .driver_info = BTUSB_DIGIANSWER },
252 { USB_DEVICE(0x08fd, 0x0002), .driver_info = BTUSB_IGNORE },
253
254 /* CSR BlueCore Bluetooth Sniffer */
Marcel Holtmann4f64fa82014-07-07 00:12:04 +0200255 { USB_DEVICE(0x0a12, 0x0002),
256 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200257
258 /* Frontline ComProbe Bluetooth Sniffer */
Marcel Holtmann4f64fa82014-07-07 00:12:04 +0200259 { USB_DEVICE(0x16d3, 0x0002),
260 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200261
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -0700262 /* Intel Bluetooth device */
263 { USB_DEVICE(0x8087, 0x07dc), .driver_info = BTUSB_INTEL },
Tedd Ho-Jeong Anef4e5e42013-11-12 13:10:58 -0800264 { USB_DEVICE(0x8087, 0x0a2a), .driver_info = BTUSB_INTEL },
Marcel Holtmanncda0dd72015-01-26 21:33:48 -0800265 { USB_DEVICE(0x8087, 0x0a2b), .driver_info = BTUSB_INTEL_NEW },
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -0700266
Amitkumar Karwarae8df492014-07-18 14:47:06 -0700267 /* Marvell device */
268 { USB_DEVICE(0x1286, 0x2044), .driver_info = BTUSB_MARVELL },
269 { USB_DEVICE(0x1286, 0x2046), .driver_info = BTUSB_MARVELL },
270
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200271 { } /* Terminating entry */
272};
273
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200274#define BTUSB_MAX_ISOC_FRAMES 10
275
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200276#define BTUSB_INTR_RUNNING 0
277#define BTUSB_BULK_RUNNING 1
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200278#define BTUSB_ISOC_RUNNING 2
Oliver Neukum7bee5492009-08-24 23:44:59 +0200279#define BTUSB_SUSPENDING 3
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -0300280#define BTUSB_DID_ISO_RESUME 4
Marcel Holtmanncda0dd72015-01-26 21:33:48 -0800281#define BTUSB_BOOTLOADER 5
282#define BTUSB_DOWNLOADING 6
283#define BTUSB_BOOTING 7
284#define BTUSB_FIRMWARE_FAILED 8
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200285
286struct btusb_data {
287 struct hci_dev *hdev;
288 struct usb_device *udev;
Marcel Holtmann5fbcd262008-09-23 00:16:36 +0200289 struct usb_interface *intf;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200290 struct usb_interface *isoc;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200291
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200292 unsigned long flags;
293
294 struct work_struct work;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200295 struct work_struct waker;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200296
Marcel Holtmann803b5832014-09-16 08:00:29 +0200297 struct usb_anchor deferred;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200298 struct usb_anchor tx_anchor;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200299 int tx_in_flight;
300 spinlock_t txlock;
301
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200302 struct usb_anchor intr_anchor;
303 struct usb_anchor bulk_anchor;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200304 struct usb_anchor isoc_anchor;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200305 spinlock_t rxlock;
306
307 struct sk_buff *evt_skb;
308 struct sk_buff *acl_skb;
309 struct sk_buff *sco_skb;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200310
311 struct usb_endpoint_descriptor *intr_ep;
312 struct usb_endpoint_descriptor *bulk_tx_ep;
313 struct usb_endpoint_descriptor *bulk_rx_ep;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200314 struct usb_endpoint_descriptor *isoc_tx_ep;
315 struct usb_endpoint_descriptor *isoc_rx_ep;
316
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100317 __u8 cmdreq_type;
318
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100319 unsigned int sco_num;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200320 int isoc_altsetting;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +0100321 int suspend_count;
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +0100322
Marcel Holtmann97307f52015-01-12 13:51:10 -0800323 int (*recv_event)(struct hci_dev *hdev, struct sk_buff *skb);
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +0100324 int (*recv_bulk)(struct btusb_data *data, void *buffer, int count);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200325};
326
Marcel Holtmann803b5832014-09-16 08:00:29 +0200327static inline void btusb_free_frags(struct btusb_data *data)
328{
329 unsigned long flags;
330
331 spin_lock_irqsave(&data->rxlock, flags);
332
333 kfree_skb(data->evt_skb);
334 data->evt_skb = NULL;
335
336 kfree_skb(data->acl_skb);
337 data->acl_skb = NULL;
338
339 kfree_skb(data->sco_skb);
340 data->sco_skb = NULL;
341
342 spin_unlock_irqrestore(&data->rxlock, flags);
343}
344
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200345static int btusb_recv_intr(struct btusb_data *data, void *buffer, int count)
346{
Marcel Holtmann803b5832014-09-16 08:00:29 +0200347 struct sk_buff *skb;
348 int err = 0;
349
350 spin_lock(&data->rxlock);
351 skb = data->evt_skb;
352
353 while (count) {
354 int len;
355
356 if (!skb) {
357 skb = bt_skb_alloc(HCI_MAX_EVENT_SIZE, GFP_ATOMIC);
358 if (!skb) {
359 err = -ENOMEM;
360 break;
361 }
362
363 bt_cb(skb)->pkt_type = HCI_EVENT_PKT;
364 bt_cb(skb)->expect = HCI_EVENT_HDR_SIZE;
365 }
366
367 len = min_t(uint, bt_cb(skb)->expect, count);
368 memcpy(skb_put(skb, len), buffer, len);
369
370 count -= len;
371 buffer += len;
372 bt_cb(skb)->expect -= len;
373
374 if (skb->len == HCI_EVENT_HDR_SIZE) {
375 /* Complete event header */
376 bt_cb(skb)->expect = hci_event_hdr(skb)->plen;
377
378 if (skb_tailroom(skb) < bt_cb(skb)->expect) {
379 kfree_skb(skb);
380 skb = NULL;
381
382 err = -EILSEQ;
383 break;
384 }
385 }
386
387 if (bt_cb(skb)->expect == 0) {
388 /* Complete frame */
Marcel Holtmann97307f52015-01-12 13:51:10 -0800389 data->recv_event(data->hdev, skb);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200390 skb = NULL;
391 }
392 }
393
394 data->evt_skb = skb;
395 spin_unlock(&data->rxlock);
396
397 return err;
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200398}
399
400static int btusb_recv_bulk(struct btusb_data *data, void *buffer, int count)
401{
Marcel Holtmann803b5832014-09-16 08:00:29 +0200402 struct sk_buff *skb;
403 int err = 0;
404
405 spin_lock(&data->rxlock);
406 skb = data->acl_skb;
407
408 while (count) {
409 int len;
410
411 if (!skb) {
412 skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
413 if (!skb) {
414 err = -ENOMEM;
415 break;
416 }
417
418 bt_cb(skb)->pkt_type = HCI_ACLDATA_PKT;
419 bt_cb(skb)->expect = HCI_ACL_HDR_SIZE;
420 }
421
422 len = min_t(uint, bt_cb(skb)->expect, count);
423 memcpy(skb_put(skb, len), buffer, len);
424
425 count -= len;
426 buffer += len;
427 bt_cb(skb)->expect -= len;
428
429 if (skb->len == HCI_ACL_HDR_SIZE) {
430 __le16 dlen = hci_acl_hdr(skb)->dlen;
431
432 /* Complete ACL header */
433 bt_cb(skb)->expect = __le16_to_cpu(dlen);
434
435 if (skb_tailroom(skb) < bt_cb(skb)->expect) {
436 kfree_skb(skb);
437 skb = NULL;
438
439 err = -EILSEQ;
440 break;
441 }
442 }
443
444 if (bt_cb(skb)->expect == 0) {
445 /* Complete frame */
446 hci_recv_frame(data->hdev, skb);
447 skb = NULL;
448 }
449 }
450
451 data->acl_skb = skb;
452 spin_unlock(&data->rxlock);
453
454 return err;
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200455}
456
457static int btusb_recv_isoc(struct btusb_data *data, void *buffer, int count)
458{
Marcel Holtmann803b5832014-09-16 08:00:29 +0200459 struct sk_buff *skb;
460 int err = 0;
461
462 spin_lock(&data->rxlock);
463 skb = data->sco_skb;
464
465 while (count) {
466 int len;
467
468 if (!skb) {
469 skb = bt_skb_alloc(HCI_MAX_SCO_SIZE, GFP_ATOMIC);
470 if (!skb) {
471 err = -ENOMEM;
472 break;
473 }
474
475 bt_cb(skb)->pkt_type = HCI_SCODATA_PKT;
476 bt_cb(skb)->expect = HCI_SCO_HDR_SIZE;
477 }
478
479 len = min_t(uint, bt_cb(skb)->expect, count);
480 memcpy(skb_put(skb, len), buffer, len);
481
482 count -= len;
483 buffer += len;
484 bt_cb(skb)->expect -= len;
485
486 if (skb->len == HCI_SCO_HDR_SIZE) {
487 /* Complete SCO header */
488 bt_cb(skb)->expect = hci_sco_hdr(skb)->dlen;
489
490 if (skb_tailroom(skb) < bt_cb(skb)->expect) {
491 kfree_skb(skb);
492 skb = NULL;
493
494 err = -EILSEQ;
495 break;
496 }
497 }
498
499 if (bt_cb(skb)->expect == 0) {
500 /* Complete frame */
501 hci_recv_frame(data->hdev, skb);
502 skb = NULL;
503 }
504 }
505
506 data->sco_skb = skb;
507 spin_unlock(&data->rxlock);
508
509 return err;
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200510}
511
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200512static void btusb_intr_complete(struct urb *urb)
513{
514 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +0100515 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200516 int err;
517
Marcel Holtmann89e75332014-09-16 04:44:50 +0200518 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
519 urb->actual_length);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200520
521 if (!test_bit(HCI_RUNNING, &hdev->flags))
522 return;
523
524 if (urb->status == 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200525 hdev->stat.byte_rx += urb->actual_length;
526
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200527 if (btusb_recv_intr(data, urb->transfer_buffer,
528 urb->actual_length) < 0) {
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200529 BT_ERR("%s corrupted event packet", hdev->name);
530 hdev->stat.err_rx++;
531 }
Champion Chen85560c42014-09-06 14:06:08 -0500532 } else if (urb->status == -ENOENT) {
533 /* Avoid suspend failed when usb_kill_urb */
534 return;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200535 }
536
537 if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
538 return;
539
Oliver Neukum7bee5492009-08-24 23:44:59 +0200540 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200541 usb_anchor_urb(urb, &data->intr_anchor);
542
543 err = usb_submit_urb(urb, GFP_ATOMIC);
544 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200545 /* -EPERM: urb is being killed;
546 * -ENODEV: device got disconnected */
547 if (err != -EPERM && err != -ENODEV)
Stefan Seyfried61faddf2010-11-30 21:49:08 +0100548 BT_ERR("%s urb %p failed to resubmit (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200549 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200550 usb_unanchor_urb(urb);
551 }
552}
553
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100554static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200555{
David Herrmann155961e2012-02-09 21:58:32 +0100556 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200557 struct urb *urb;
558 unsigned char *buf;
559 unsigned int pipe;
560 int err, size;
561
562 BT_DBG("%s", hdev->name);
563
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200564 if (!data->intr_ep)
565 return -ENODEV;
566
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100567 urb = usb_alloc_urb(0, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200568 if (!urb)
569 return -ENOMEM;
570
571 size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
572
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100573 buf = kmalloc(size, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200574 if (!buf) {
575 usb_free_urb(urb);
576 return -ENOMEM;
577 }
578
579 pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
580
581 usb_fill_int_urb(urb, data->udev, pipe, buf, size,
Marcel Holtmann89e75332014-09-16 04:44:50 +0200582 btusb_intr_complete, hdev, data->intr_ep->bInterval);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200583
584 urb->transfer_flags |= URB_FREE_BUFFER;
585
586 usb_anchor_urb(urb, &data->intr_anchor);
587
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100588 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200589 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +0200590 if (err != -EPERM && err != -ENODEV)
591 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200592 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200593 usb_unanchor_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200594 }
595
596 usb_free_urb(urb);
597
598 return err;
599}
600
601static void btusb_bulk_complete(struct urb *urb)
602{
603 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +0100604 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200605 int err;
606
Marcel Holtmann89e75332014-09-16 04:44:50 +0200607 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
608 urb->actual_length);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200609
610 if (!test_bit(HCI_RUNNING, &hdev->flags))
611 return;
612
613 if (urb->status == 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200614 hdev->stat.byte_rx += urb->actual_length;
615
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +0100616 if (data->recv_bulk(data, urb->transfer_buffer,
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200617 urb->actual_length) < 0) {
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200618 BT_ERR("%s corrupted ACL packet", hdev->name);
619 hdev->stat.err_rx++;
620 }
Champion Chen85560c42014-09-06 14:06:08 -0500621 } else if (urb->status == -ENOENT) {
622 /* Avoid suspend failed when usb_kill_urb */
623 return;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200624 }
625
626 if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
627 return;
628
629 usb_anchor_urb(urb, &data->bulk_anchor);
Oliver Neukum652fd782009-12-16 19:23:43 +0100630 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200631
632 err = usb_submit_urb(urb, GFP_ATOMIC);
633 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200634 /* -EPERM: urb is being killed;
635 * -ENODEV: device got disconnected */
636 if (err != -EPERM && err != -ENODEV)
Stefan Seyfried61faddf2010-11-30 21:49:08 +0100637 BT_ERR("%s urb %p failed to resubmit (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200638 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200639 usb_unanchor_urb(urb);
640 }
641}
642
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100643static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200644{
David Herrmann155961e2012-02-09 21:58:32 +0100645 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200646 struct urb *urb;
647 unsigned char *buf;
648 unsigned int pipe;
Vikram Kandukuri290ba202009-07-02 14:31:59 +0530649 int err, size = HCI_MAX_FRAME_SIZE;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200650
651 BT_DBG("%s", hdev->name);
652
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200653 if (!data->bulk_rx_ep)
654 return -ENODEV;
655
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100656 urb = usb_alloc_urb(0, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200657 if (!urb)
658 return -ENOMEM;
659
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100660 buf = kmalloc(size, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200661 if (!buf) {
662 usb_free_urb(urb);
663 return -ENOMEM;
664 }
665
666 pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
667
Marcel Holtmann89e75332014-09-16 04:44:50 +0200668 usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
669 btusb_bulk_complete, hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200670
671 urb->transfer_flags |= URB_FREE_BUFFER;
672
Oliver Neukum7bee5492009-08-24 23:44:59 +0200673 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200674 usb_anchor_urb(urb, &data->bulk_anchor);
675
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100676 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200677 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +0200678 if (err != -EPERM && err != -ENODEV)
679 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200680 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200681 usb_unanchor_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200682 }
683
684 usb_free_urb(urb);
685
686 return err;
687}
688
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200689static void btusb_isoc_complete(struct urb *urb)
690{
691 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +0100692 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200693 int i, err;
694
Marcel Holtmann89e75332014-09-16 04:44:50 +0200695 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
696 urb->actual_length);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200697
698 if (!test_bit(HCI_RUNNING, &hdev->flags))
699 return;
700
701 if (urb->status == 0) {
702 for (i = 0; i < urb->number_of_packets; i++) {
703 unsigned int offset = urb->iso_frame_desc[i].offset;
704 unsigned int length = urb->iso_frame_desc[i].actual_length;
705
706 if (urb->iso_frame_desc[i].status)
707 continue;
708
709 hdev->stat.byte_rx += length;
710
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200711 if (btusb_recv_isoc(data, urb->transfer_buffer + offset,
712 length) < 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200713 BT_ERR("%s corrupted SCO packet", hdev->name);
714 hdev->stat.err_rx++;
715 }
716 }
Champion Chen85560c42014-09-06 14:06:08 -0500717 } else if (urb->status == -ENOENT) {
718 /* Avoid suspend failed when usb_kill_urb */
719 return;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200720 }
721
722 if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags))
723 return;
724
725 usb_anchor_urb(urb, &data->isoc_anchor);
726
727 err = usb_submit_urb(urb, GFP_ATOMIC);
728 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200729 /* -EPERM: urb is being killed;
730 * -ENODEV: device got disconnected */
731 if (err != -EPERM && err != -ENODEV)
Stefan Seyfried61faddf2010-11-30 21:49:08 +0100732 BT_ERR("%s urb %p failed to resubmit (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200733 hdev->name, urb, -err);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200734 usb_unanchor_urb(urb);
735 }
736}
737
Jesper Juhl42b16b32011-01-17 00:09:38 +0100738static inline void __fill_isoc_descriptor(struct urb *urb, int len, int mtu)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200739{
740 int i, offset = 0;
741
742 BT_DBG("len %d mtu %d", len, mtu);
743
744 for (i = 0; i < BTUSB_MAX_ISOC_FRAMES && len >= mtu;
745 i++, offset += mtu, len -= mtu) {
746 urb->iso_frame_desc[i].offset = offset;
747 urb->iso_frame_desc[i].length = mtu;
748 }
749
750 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
751 urb->iso_frame_desc[i].offset = offset;
752 urb->iso_frame_desc[i].length = len;
753 i++;
754 }
755
756 urb->number_of_packets = i;
757}
758
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100759static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200760{
David Herrmann155961e2012-02-09 21:58:32 +0100761 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200762 struct urb *urb;
763 unsigned char *buf;
764 unsigned int pipe;
765 int err, size;
766
767 BT_DBG("%s", hdev->name);
768
769 if (!data->isoc_rx_ep)
770 return -ENODEV;
771
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100772 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200773 if (!urb)
774 return -ENOMEM;
775
776 size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) *
777 BTUSB_MAX_ISOC_FRAMES;
778
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100779 buf = kmalloc(size, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200780 if (!buf) {
781 usb_free_urb(urb);
782 return -ENOMEM;
783 }
784
785 pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress);
786
Bing Zhaofa0fb932011-12-20 18:19:00 -0800787 usb_fill_int_urb(urb, data->udev, pipe, buf, size, btusb_isoc_complete,
Marcel Holtmann89e75332014-09-16 04:44:50 +0200788 hdev, data->isoc_rx_ep->bInterval);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200789
Marcel Holtmann89e75332014-09-16 04:44:50 +0200790 urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200791
792 __fill_isoc_descriptor(urb, size,
Marcel Holtmann89e75332014-09-16 04:44:50 +0200793 le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize));
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200794
795 usb_anchor_urb(urb, &data->isoc_anchor);
796
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100797 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200798 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +0200799 if (err != -EPERM && err != -ENODEV)
800 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200801 hdev->name, urb, -err);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200802 usb_unanchor_urb(urb);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200803 }
804
805 usb_free_urb(urb);
806
807 return err;
808}
809
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200810static void btusb_tx_complete(struct urb *urb)
811{
812 struct sk_buff *skb = urb->context;
Marcel Holtmann89e75332014-09-16 04:44:50 +0200813 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
David Herrmann155961e2012-02-09 21:58:32 +0100814 struct btusb_data *data = hci_get_drvdata(hdev);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200815
Marcel Holtmann89e75332014-09-16 04:44:50 +0200816 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
817 urb->actual_length);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200818
819 if (!test_bit(HCI_RUNNING, &hdev->flags))
820 goto done;
821
822 if (!urb->status)
823 hdev->stat.byte_tx += urb->transfer_buffer_length;
824 else
825 hdev->stat.err_tx++;
826
827done:
828 spin_lock(&data->txlock);
829 data->tx_in_flight--;
830 spin_unlock(&data->txlock);
831
832 kfree(urb->setup_packet);
833
834 kfree_skb(skb);
835}
836
837static void btusb_isoc_tx_complete(struct urb *urb)
838{
839 struct sk_buff *skb = urb->context;
Marcel Holtmann89e75332014-09-16 04:44:50 +0200840 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200841
Marcel Holtmann89e75332014-09-16 04:44:50 +0200842 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
843 urb->actual_length);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200844
845 if (!test_bit(HCI_RUNNING, &hdev->flags))
846 goto done;
847
848 if (!urb->status)
849 hdev->stat.byte_tx += urb->transfer_buffer_length;
850 else
851 hdev->stat.err_tx++;
852
853done:
854 kfree(urb->setup_packet);
855
856 kfree_skb(skb);
857}
858
859static int btusb_open(struct hci_dev *hdev)
860{
David Herrmann155961e2012-02-09 21:58:32 +0100861 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200862 int err;
863
864 BT_DBG("%s", hdev->name);
865
Oliver Neukum7bee5492009-08-24 23:44:59 +0200866 err = usb_autopm_get_interface(data->intf);
867 if (err < 0)
868 return err;
869
870 data->intf->needs_remote_wakeup = 1;
871
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200872 if (test_and_set_bit(HCI_RUNNING, &hdev->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +0200873 goto done;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200874
875 if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +0200876 goto done;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200877
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100878 err = btusb_submit_intr_urb(hdev, GFP_KERNEL);
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100879 if (err < 0)
880 goto failed;
881
882 err = btusb_submit_bulk_urb(hdev, GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200883 if (err < 0) {
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100884 usb_kill_anchored_urbs(&data->intr_anchor);
885 goto failed;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200886 }
887
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100888 set_bit(BTUSB_BULK_RUNNING, &data->flags);
889 btusb_submit_bulk_urb(hdev, GFP_KERNEL);
890
Oliver Neukum7bee5492009-08-24 23:44:59 +0200891done:
892 usb_autopm_put_interface(data->intf);
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100893 return 0;
894
895failed:
896 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
897 clear_bit(HCI_RUNNING, &hdev->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200898 usb_autopm_put_interface(data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200899 return err;
900}
901
Oliver Neukum7bee5492009-08-24 23:44:59 +0200902static void btusb_stop_traffic(struct btusb_data *data)
903{
904 usb_kill_anchored_urbs(&data->intr_anchor);
905 usb_kill_anchored_urbs(&data->bulk_anchor);
906 usb_kill_anchored_urbs(&data->isoc_anchor);
907}
908
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200909static int btusb_close(struct hci_dev *hdev)
910{
David Herrmann155961e2012-02-09 21:58:32 +0100911 struct btusb_data *data = hci_get_drvdata(hdev);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200912 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200913
914 BT_DBG("%s", hdev->name);
915
916 if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
917 return 0;
918
Marcel Holtmanne8c3c3d2008-09-23 00:16:36 +0200919 cancel_work_sync(&data->work);
Linus Torvalds404291a2009-11-11 13:32:29 -0800920 cancel_work_sync(&data->waker);
Marcel Holtmanne8c3c3d2008-09-23 00:16:36 +0200921
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200922 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200923 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200924 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200925
926 btusb_stop_traffic(data);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200927 btusb_free_frags(data);
928
Oliver Neukum7bee5492009-08-24 23:44:59 +0200929 err = usb_autopm_get_interface(data->intf);
930 if (err < 0)
Oliver Neukum7b8e2c12009-11-13 14:26:23 +0100931 goto failed;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200932
933 data->intf->needs_remote_wakeup = 0;
934 usb_autopm_put_interface(data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200935
Oliver Neukum7b8e2c12009-11-13 14:26:23 +0100936failed:
937 usb_scuttle_anchored_urbs(&data->deferred);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200938 return 0;
939}
940
941static int btusb_flush(struct hci_dev *hdev)
942{
David Herrmann155961e2012-02-09 21:58:32 +0100943 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200944
945 BT_DBG("%s", hdev->name);
946
947 usb_kill_anchored_urbs(&data->tx_anchor);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200948 btusb_free_frags(data);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200949
950 return 0;
951}
952
Marcel Holtmann047b2ec2014-09-14 09:11:06 +0200953static struct urb *alloc_ctrl_urb(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200954{
David Herrmann155961e2012-02-09 21:58:32 +0100955 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200956 struct usb_ctrlrequest *dr;
957 struct urb *urb;
958 unsigned int pipe;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200959
Marcel Holtmann047b2ec2014-09-14 09:11:06 +0200960 urb = usb_alloc_urb(0, GFP_KERNEL);
961 if (!urb)
962 return ERR_PTR(-ENOMEM);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200963
Marcel Holtmann047b2ec2014-09-14 09:11:06 +0200964 dr = kmalloc(sizeof(*dr), GFP_KERNEL);
965 if (!dr) {
966 usb_free_urb(urb);
967 return ERR_PTR(-ENOMEM);
968 }
969
970 dr->bRequestType = data->cmdreq_type;
971 dr->bRequest = 0;
972 dr->wIndex = 0;
973 dr->wValue = 0;
974 dr->wLength = __cpu_to_le16(skb->len);
975
976 pipe = usb_sndctrlpipe(data->udev, 0x00);
977
Marcel Holtmann89e75332014-09-16 04:44:50 +0200978 usb_fill_control_urb(urb, data->udev, pipe, (void *)dr,
Marcel Holtmann047b2ec2014-09-14 09:11:06 +0200979 skb->data, skb->len, btusb_tx_complete, skb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200980
Marcel Holtmann89e75332014-09-16 04:44:50 +0200981 skb->dev = (void *)hdev;
Marcel Holtmann7bd8f092013-10-11 06:19:18 -0700982
Marcel Holtmann047b2ec2014-09-14 09:11:06 +0200983 return urb;
984}
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200985
Marcel Holtmann047b2ec2014-09-14 09:11:06 +0200986static struct urb *alloc_bulk_urb(struct hci_dev *hdev, struct sk_buff *skb)
987{
988 struct btusb_data *data = hci_get_drvdata(hdev);
989 struct urb *urb;
990 unsigned int pipe;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200991
Marcel Holtmann047b2ec2014-09-14 09:11:06 +0200992 if (!data->bulk_tx_ep)
993 return ERR_PTR(-ENODEV);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200994
Marcel Holtmann047b2ec2014-09-14 09:11:06 +0200995 urb = usb_alloc_urb(0, GFP_KERNEL);
996 if (!urb)
997 return ERR_PTR(-ENOMEM);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200998
Marcel Holtmann047b2ec2014-09-14 09:11:06 +0200999 pipe = usb_sndbulkpipe(data->udev, data->bulk_tx_ep->bEndpointAddress);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001000
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001001 usb_fill_bulk_urb(urb, data->udev, pipe,
1002 skb->data, skb->len, btusb_tx_complete, skb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001003
Marcel Holtmann89e75332014-09-16 04:44:50 +02001004 skb->dev = (void *)hdev;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001005
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001006 return urb;
1007}
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001008
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001009static struct urb *alloc_isoc_urb(struct hci_dev *hdev, struct sk_buff *skb)
1010{
1011 struct btusb_data *data = hci_get_drvdata(hdev);
1012 struct urb *urb;
1013 unsigned int pipe;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001014
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001015 if (!data->isoc_tx_ep)
1016 return ERR_PTR(-ENODEV);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001017
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001018 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_KERNEL);
1019 if (!urb)
1020 return ERR_PTR(-ENOMEM);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001021
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001022 pipe = usb_sndisocpipe(data->udev, data->isoc_tx_ep->bEndpointAddress);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001023
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001024 usb_fill_int_urb(urb, data->udev, pipe,
1025 skb->data, skb->len, btusb_isoc_tx_complete,
1026 skb, data->isoc_tx_ep->bInterval);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001027
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001028 urb->transfer_flags = URB_ISO_ASAP;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001029
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001030 __fill_isoc_descriptor(urb, skb->len,
1031 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001032
Marcel Holtmann89e75332014-09-16 04:44:50 +02001033 skb->dev = (void *)hdev;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001034
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001035 return urb;
1036}
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001037
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001038static int submit_tx_urb(struct hci_dev *hdev, struct urb *urb)
1039{
1040 struct btusb_data *data = hci_get_drvdata(hdev);
1041 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001042
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001043 usb_anchor_urb(urb, &data->tx_anchor);
1044
Johan Hedberge9753ef2014-09-14 08:49:34 +03001045 err = usb_submit_urb(urb, GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001046 if (err < 0) {
Paul Bolle5a9b80e2011-10-09 12:12:16 +02001047 if (err != -EPERM && err != -ENODEV)
1048 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +02001049 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001050 kfree(urb->setup_packet);
1051 usb_unanchor_urb(urb);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001052 } else {
1053 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001054 }
1055
Cong Wang54a8a79c2011-11-22 09:32:57 +08001056 usb_free_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001057 return err;
1058}
1059
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001060static int submit_or_queue_tx_urb(struct hci_dev *hdev, struct urb *urb)
1061{
1062 struct btusb_data *data = hci_get_drvdata(hdev);
1063 unsigned long flags;
1064 bool suspending;
1065
1066 spin_lock_irqsave(&data->txlock, flags);
1067 suspending = test_bit(BTUSB_SUSPENDING, &data->flags);
1068 if (!suspending)
1069 data->tx_in_flight++;
1070 spin_unlock_irqrestore(&data->txlock, flags);
1071
1072 if (!suspending)
1073 return submit_tx_urb(hdev, urb);
1074
1075 usb_anchor_urb(urb, &data->deferred);
1076 schedule_work(&data->waker);
1077
1078 usb_free_urb(urb);
1079 return 0;
1080}
1081
1082static int btusb_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
1083{
1084 struct urb *urb;
1085
1086 BT_DBG("%s", hdev->name);
1087
1088 if (!test_bit(HCI_RUNNING, &hdev->flags))
1089 return -EBUSY;
1090
1091 switch (bt_cb(skb)->pkt_type) {
1092 case HCI_COMMAND_PKT:
1093 urb = alloc_ctrl_urb(hdev, skb);
1094 if (IS_ERR(urb))
1095 return PTR_ERR(urb);
1096
1097 hdev->stat.cmd_tx++;
1098 return submit_or_queue_tx_urb(hdev, urb);
1099
1100 case HCI_ACLDATA_PKT:
1101 urb = alloc_bulk_urb(hdev, skb);
1102 if (IS_ERR(urb))
1103 return PTR_ERR(urb);
1104
1105 hdev->stat.acl_tx++;
1106 return submit_or_queue_tx_urb(hdev, urb);
1107
1108 case HCI_SCODATA_PKT:
1109 if (hci_conn_num(hdev, SCO_LINK) < 1)
1110 return -ENODEV;
1111
1112 urb = alloc_isoc_urb(hdev, skb);
1113 if (IS_ERR(urb))
1114 return PTR_ERR(urb);
1115
1116 hdev->stat.sco_tx++;
1117 return submit_tx_urb(hdev, urb);
1118 }
1119
1120 return -EILSEQ;
1121}
1122
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001123static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
1124{
David Herrmann155961e2012-02-09 21:58:32 +01001125 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001126
1127 BT_DBG("%s evt %d", hdev->name, evt);
1128
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001129 if (hci_conn_num(hdev, SCO_LINK) != data->sco_num) {
1130 data->sco_num = hci_conn_num(hdev, SCO_LINK);
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001131 schedule_work(&data->work);
Marcel Holtmanna780efa2008-11-30 12:17:12 +01001132 }
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001133}
1134
Jesper Juhl42b16b32011-01-17 00:09:38 +01001135static inline int __set_isoc_interface(struct hci_dev *hdev, int altsetting)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001136{
David Herrmann155961e2012-02-09 21:58:32 +01001137 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001138 struct usb_interface *intf = data->isoc;
1139 struct usb_endpoint_descriptor *ep_desc;
1140 int i, err;
1141
1142 if (!data->isoc)
1143 return -ENODEV;
1144
1145 err = usb_set_interface(data->udev, 1, altsetting);
1146 if (err < 0) {
1147 BT_ERR("%s setting interface failed (%d)", hdev->name, -err);
1148 return err;
1149 }
1150
1151 data->isoc_altsetting = altsetting;
1152
1153 data->isoc_tx_ep = NULL;
1154 data->isoc_rx_ep = NULL;
1155
1156 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
1157 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
1158
1159 if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) {
1160 data->isoc_tx_ep = ep_desc;
1161 continue;
1162 }
1163
1164 if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) {
1165 data->isoc_rx_ep = ep_desc;
1166 continue;
1167 }
1168 }
1169
1170 if (!data->isoc_tx_ep || !data->isoc_rx_ep) {
1171 BT_ERR("%s invalid SCO descriptors", hdev->name);
1172 return -ENODEV;
1173 }
1174
1175 return 0;
1176}
1177
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001178static void btusb_work(struct work_struct *work)
1179{
1180 struct btusb_data *data = container_of(work, struct btusb_data, work);
1181 struct hci_dev *hdev = data->hdev;
Mikel Astizf4001d22012-04-11 08:48:51 +02001182 int new_alts;
Oliver Neukum7bee5492009-08-24 23:44:59 +02001183 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001184
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001185 if (data->sco_num > 0) {
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -03001186 if (!test_bit(BTUSB_DID_ISO_RESUME, &data->flags)) {
Oliver Neukum8efdd0c2011-02-11 13:00:06 +01001187 err = usb_autopm_get_interface(data->isoc ? data->isoc : data->intf);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001188 if (err < 0) {
1189 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1190 usb_kill_anchored_urbs(&data->isoc_anchor);
1191 return;
1192 }
1193
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -03001194 set_bit(BTUSB_DID_ISO_RESUME, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001195 }
Mikel Astizf4001d22012-04-11 08:48:51 +02001196
1197 if (hdev->voice_setting & 0x0020) {
1198 static const int alts[3] = { 2, 4, 5 };
Marcel Holtmann89e75332014-09-16 04:44:50 +02001199
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001200 new_alts = alts[data->sco_num - 1];
Mikel Astizf4001d22012-04-11 08:48:51 +02001201 } else {
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001202 new_alts = data->sco_num;
Mikel Astizf4001d22012-04-11 08:48:51 +02001203 }
1204
1205 if (data->isoc_altsetting != new_alts) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001206 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1207 usb_kill_anchored_urbs(&data->isoc_anchor);
1208
Mikel Astizf4001d22012-04-11 08:48:51 +02001209 if (__set_isoc_interface(hdev, new_alts) < 0)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001210 return;
1211 }
1212
1213 if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001214 if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001215 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1216 else
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001217 btusb_submit_isoc_urb(hdev, GFP_KERNEL);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001218 }
1219 } else {
1220 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1221 usb_kill_anchored_urbs(&data->isoc_anchor);
1222
1223 __set_isoc_interface(hdev, 0);
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -03001224 if (test_and_clear_bit(BTUSB_DID_ISO_RESUME, &data->flags))
Oliver Neukum8efdd0c2011-02-11 13:00:06 +01001225 usb_autopm_put_interface(data->isoc ? data->isoc : data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001226 }
1227}
1228
Oliver Neukum7bee5492009-08-24 23:44:59 +02001229static void btusb_waker(struct work_struct *work)
1230{
1231 struct btusb_data *data = container_of(work, struct btusb_data, waker);
1232 int err;
1233
1234 err = usb_autopm_get_interface(data->intf);
1235 if (err < 0)
1236 return;
1237
1238 usb_autopm_put_interface(data->intf);
1239}
1240
Marcel Holtmann9f8f9622013-04-10 08:11:35 -07001241static int btusb_setup_bcm92035(struct hci_dev *hdev)
1242{
1243 struct sk_buff *skb;
1244 u8 val = 0x00;
1245
1246 BT_DBG("%s", hdev->name);
1247
1248 skb = __hci_cmd_sync(hdev, 0xfc3b, 1, &val, HCI_INIT_TIMEOUT);
1249 if (IS_ERR(skb))
1250 BT_ERR("BCM92035 command failed (%ld)", -PTR_ERR(skb));
1251 else
1252 kfree_skb(skb);
1253
1254 return 0;
1255}
1256
Marcel Holtmann81cac642014-01-03 03:02:36 -08001257static int btusb_setup_csr(struct hci_dev *hdev)
1258{
1259 struct hci_rp_read_local_version *rp;
1260 struct sk_buff *skb;
1261 int ret;
1262
1263 BT_DBG("%s", hdev->name);
1264
1265 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
1266 HCI_INIT_TIMEOUT);
1267 if (IS_ERR(skb)) {
1268 BT_ERR("Reading local version failed (%ld)", -PTR_ERR(skb));
1269 return -PTR_ERR(skb);
1270 }
1271
Marcel Holtmann89e75332014-09-16 04:44:50 +02001272 rp = (struct hci_rp_read_local_version *)skb->data;
Marcel Holtmann81cac642014-01-03 03:02:36 -08001273
1274 if (!rp->status) {
1275 if (le16_to_cpu(rp->manufacturer) != 10) {
1276 /* Clear the reset quirk since this is not an actual
1277 * early Bluetooth 1.1 device from CSR.
1278 */
1279 clear_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
1280
1281 /* These fake CSR controllers have all a broken
1282 * stored link key handling and so just disable it.
1283 */
1284 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY,
1285 &hdev->quirks);
1286 }
1287 }
1288
1289 ret = -bt_to_errno(rp->status);
1290
1291 kfree_skb(skb);
1292
1293 return ret;
1294}
1295
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001296struct intel_version {
1297 u8 status;
1298 u8 hw_platform;
1299 u8 hw_variant;
1300 u8 hw_revision;
1301 u8 fw_variant;
1302 u8 fw_revision;
1303 u8 fw_build_num;
1304 u8 fw_build_ww;
1305 u8 fw_build_yy;
1306 u8 fw_patch_num;
1307} __packed;
1308
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001309struct intel_boot_params {
1310 __u8 status;
1311 __u8 otp_format;
1312 __u8 otp_content;
1313 __u8 otp_patch;
1314 __le16 dev_revid;
1315 __u8 secure_boot;
1316 __u8 key_from_hdr;
1317 __u8 key_type;
1318 __u8 otp_lock;
1319 __u8 api_lock;
1320 __u8 debug_lock;
1321 bdaddr_t otp_bdaddr;
1322 __u8 min_fw_build_nn;
1323 __u8 min_fw_build_cw;
1324 __u8 min_fw_build_yy;
1325 __u8 limited_cce;
1326 __u8 unlocked_state;
1327} __packed;
1328
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001329static const struct firmware *btusb_setup_intel_get_fw(struct hci_dev *hdev,
Marcel Holtmann89e75332014-09-16 04:44:50 +02001330 struct intel_version *ver)
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001331{
1332 const struct firmware *fw;
1333 char fwname[64];
1334 int ret;
1335
1336 snprintf(fwname, sizeof(fwname),
1337 "intel/ibt-hw-%x.%x.%x-fw-%x.%x.%x.%x.%x.bseq",
1338 ver->hw_platform, ver->hw_variant, ver->hw_revision,
1339 ver->fw_variant, ver->fw_revision, ver->fw_build_num,
1340 ver->fw_build_ww, ver->fw_build_yy);
1341
1342 ret = request_firmware(&fw, fwname, &hdev->dev);
1343 if (ret < 0) {
1344 if (ret == -EINVAL) {
1345 BT_ERR("%s Intel firmware file request failed (%d)",
1346 hdev->name, ret);
1347 return NULL;
1348 }
1349
1350 BT_ERR("%s failed to open Intel firmware file: %s(%d)",
1351 hdev->name, fwname, ret);
1352
1353 /* If the correct firmware patch file is not found, use the
1354 * default firmware patch file instead
1355 */
1356 snprintf(fwname, sizeof(fwname), "intel/ibt-hw-%x.%x.bseq",
1357 ver->hw_platform, ver->hw_variant);
1358 if (request_firmware(&fw, fwname, &hdev->dev) < 0) {
1359 BT_ERR("%s failed to open default Intel fw file: %s",
1360 hdev->name, fwname);
1361 return NULL;
1362 }
1363 }
1364
1365 BT_INFO("%s: Intel Bluetooth firmware file: %s", hdev->name, fwname);
1366
1367 return fw;
1368}
1369
1370static int btusb_setup_intel_patching(struct hci_dev *hdev,
1371 const struct firmware *fw,
1372 const u8 **fw_ptr, int *disable_patch)
1373{
1374 struct sk_buff *skb;
1375 struct hci_command_hdr *cmd;
1376 const u8 *cmd_param;
1377 struct hci_event_hdr *evt = NULL;
1378 const u8 *evt_param = NULL;
1379 int remain = fw->size - (*fw_ptr - fw->data);
1380
1381 /* The first byte indicates the types of the patch command or event.
1382 * 0x01 means HCI command and 0x02 is HCI event. If the first bytes
1383 * in the current firmware buffer doesn't start with 0x01 or
1384 * the size of remain buffer is smaller than HCI command header,
1385 * the firmware file is corrupted and it should stop the patching
1386 * process.
1387 */
1388 if (remain > HCI_COMMAND_HDR_SIZE && *fw_ptr[0] != 0x01) {
1389 BT_ERR("%s Intel fw corrupted: invalid cmd read", hdev->name);
1390 return -EINVAL;
1391 }
1392 (*fw_ptr)++;
1393 remain--;
1394
1395 cmd = (struct hci_command_hdr *)(*fw_ptr);
1396 *fw_ptr += sizeof(*cmd);
1397 remain -= sizeof(*cmd);
1398
1399 /* Ensure that the remain firmware data is long enough than the length
1400 * of command parameter. If not, the firmware file is corrupted.
1401 */
1402 if (remain < cmd->plen) {
1403 BT_ERR("%s Intel fw corrupted: invalid cmd len", hdev->name);
1404 return -EFAULT;
1405 }
1406
1407 /* If there is a command that loads a patch in the firmware
1408 * file, then enable the patch upon success, otherwise just
1409 * disable the manufacturer mode, for example patch activation
1410 * is not required when the default firmware patch file is used
1411 * because there are no patch data to load.
1412 */
1413 if (*disable_patch && le16_to_cpu(cmd->opcode) == 0xfc8e)
1414 *disable_patch = 0;
1415
1416 cmd_param = *fw_ptr;
1417 *fw_ptr += cmd->plen;
1418 remain -= cmd->plen;
1419
1420 /* This reads the expected events when the above command is sent to the
1421 * device. Some vendor commands expects more than one events, for
1422 * example command status event followed by vendor specific event.
1423 * For this case, it only keeps the last expected event. so the command
1424 * can be sent with __hci_cmd_sync_ev() which returns the sk_buff of
1425 * last expected event.
1426 */
1427 while (remain > HCI_EVENT_HDR_SIZE && *fw_ptr[0] == 0x02) {
1428 (*fw_ptr)++;
1429 remain--;
1430
1431 evt = (struct hci_event_hdr *)(*fw_ptr);
1432 *fw_ptr += sizeof(*evt);
1433 remain -= sizeof(*evt);
1434
1435 if (remain < evt->plen) {
1436 BT_ERR("%s Intel fw corrupted: invalid evt len",
1437 hdev->name);
1438 return -EFAULT;
1439 }
1440
1441 evt_param = *fw_ptr;
1442 *fw_ptr += evt->plen;
1443 remain -= evt->plen;
1444 }
1445
1446 /* Every HCI commands in the firmware file has its correspond event.
1447 * If event is not found or remain is smaller than zero, the firmware
1448 * file is corrupted.
1449 */
1450 if (!evt || !evt_param || remain < 0) {
1451 BT_ERR("%s Intel fw corrupted: invalid evt read", hdev->name);
1452 return -EFAULT;
1453 }
1454
1455 skb = __hci_cmd_sync_ev(hdev, le16_to_cpu(cmd->opcode), cmd->plen,
1456 cmd_param, evt->evt, HCI_INIT_TIMEOUT);
1457 if (IS_ERR(skb)) {
1458 BT_ERR("%s sending Intel patch command (0x%4.4x) failed (%ld)",
1459 hdev->name, cmd->opcode, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001460 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001461 }
1462
1463 /* It ensures that the returned event matches the event data read from
1464 * the firmware file. At fist, it checks the length and then
1465 * the contents of the event.
1466 */
1467 if (skb->len != evt->plen) {
1468 BT_ERR("%s mismatch event length (opcode 0x%4.4x)", hdev->name,
1469 le16_to_cpu(cmd->opcode));
1470 kfree_skb(skb);
1471 return -EFAULT;
1472 }
1473
1474 if (memcmp(skb->data, evt_param, evt->plen)) {
1475 BT_ERR("%s mismatch event parameter (opcode 0x%4.4x)",
1476 hdev->name, le16_to_cpu(cmd->opcode));
1477 kfree_skb(skb);
1478 return -EFAULT;
1479 }
1480 kfree_skb(skb);
1481
1482 return 0;
1483}
1484
Marcel Holtmann40cb0982014-07-02 12:06:45 +02001485#define BDADDR_INTEL (&(bdaddr_t) {{0x00, 0x8b, 0x9e, 0x19, 0x03, 0x00}})
1486
1487static int btusb_check_bdaddr_intel(struct hci_dev *hdev)
1488{
1489 struct sk_buff *skb;
1490 struct hci_rp_read_bd_addr *rp;
1491
1492 skb = __hci_cmd_sync(hdev, HCI_OP_READ_BD_ADDR, 0, NULL,
1493 HCI_INIT_TIMEOUT);
1494 if (IS_ERR(skb)) {
1495 BT_ERR("%s reading Intel device address failed (%ld)",
1496 hdev->name, PTR_ERR(skb));
1497 return PTR_ERR(skb);
1498 }
1499
1500 if (skb->len != sizeof(*rp)) {
1501 BT_ERR("%s Intel device address length mismatch", hdev->name);
1502 kfree_skb(skb);
1503 return -EIO;
1504 }
1505
Marcel Holtmann89e75332014-09-16 04:44:50 +02001506 rp = (struct hci_rp_read_bd_addr *)skb->data;
Marcel Holtmann40cb0982014-07-02 12:06:45 +02001507 if (rp->status) {
1508 BT_ERR("%s Intel device address result failed (%02x)",
1509 hdev->name, rp->status);
1510 kfree_skb(skb);
1511 return -bt_to_errno(rp->status);
1512 }
1513
1514 /* For some Intel based controllers, the default Bluetooth device
1515 * address 00:03:19:9E:8B:00 can be found. These controllers are
1516 * fully operational, but have the danger of duplicate addresses
1517 * and that in turn can cause problems with Bluetooth operation.
1518 */
Marcel Holtmann4739b5b2014-07-04 16:54:38 +02001519 if (!bacmp(&rp->bdaddr, BDADDR_INTEL)) {
Marcel Holtmann40cb0982014-07-02 12:06:45 +02001520 BT_ERR("%s found Intel default device address (%pMR)",
1521 hdev->name, &rp->bdaddr);
Marcel Holtmann4739b5b2014-07-04 16:54:38 +02001522 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
1523 }
Marcel Holtmann40cb0982014-07-02 12:06:45 +02001524
1525 kfree_skb(skb);
1526
1527 return 0;
1528}
1529
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001530static int btusb_setup_intel(struct hci_dev *hdev)
1531{
1532 struct sk_buff *skb;
1533 const struct firmware *fw;
1534 const u8 *fw_ptr;
1535 int disable_patch;
1536 struct intel_version *ver;
1537
1538 const u8 mfg_enable[] = { 0x01, 0x00 };
1539 const u8 mfg_disable[] = { 0x00, 0x00 };
1540 const u8 mfg_reset_deactivate[] = { 0x00, 0x01 };
1541 const u8 mfg_reset_activate[] = { 0x00, 0x02 };
1542
1543 BT_DBG("%s", hdev->name);
1544
1545 /* The controller has a bug with the first HCI command sent to it
1546 * returning number of completed commands as zero. This would stall the
1547 * command processing in the Bluetooth core.
1548 *
1549 * As a workaround, send HCI Reset command first which will reset the
1550 * number of completed commands and allow normal command processing
1551 * from now on.
1552 */
1553 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
1554 if (IS_ERR(skb)) {
1555 BT_ERR("%s sending initial HCI reset command failed (%ld)",
1556 hdev->name, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001557 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001558 }
1559 kfree_skb(skb);
1560
1561 /* Read Intel specific controller version first to allow selection of
1562 * which firmware file to load.
1563 *
1564 * The returned information are hardware variant and revision plus
1565 * firmware variant, revision and build number.
1566 */
1567 skb = __hci_cmd_sync(hdev, 0xfc05, 0, NULL, HCI_INIT_TIMEOUT);
1568 if (IS_ERR(skb)) {
1569 BT_ERR("%s reading Intel fw version command failed (%ld)",
1570 hdev->name, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001571 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001572 }
1573
1574 if (skb->len != sizeof(*ver)) {
1575 BT_ERR("%s Intel version event length mismatch", hdev->name);
1576 kfree_skb(skb);
1577 return -EIO;
1578 }
1579
1580 ver = (struct intel_version *)skb->data;
1581 if (ver->status) {
1582 BT_ERR("%s Intel fw version event failed (%02x)", hdev->name,
1583 ver->status);
1584 kfree_skb(skb);
1585 return -bt_to_errno(ver->status);
1586 }
1587
1588 BT_INFO("%s: read Intel version: %02x%02x%02x%02x%02x%02x%02x%02x%02x",
1589 hdev->name, ver->hw_platform, ver->hw_variant,
1590 ver->hw_revision, ver->fw_variant, ver->fw_revision,
1591 ver->fw_build_num, ver->fw_build_ww, ver->fw_build_yy,
1592 ver->fw_patch_num);
1593
1594 /* fw_patch_num indicates the version of patch the device currently
1595 * have. If there is no patch data in the device, it is always 0x00.
1596 * So, if it is other than 0x00, no need to patch the deivce again.
1597 */
1598 if (ver->fw_patch_num) {
1599 BT_INFO("%s: Intel device is already patched. patch num: %02x",
1600 hdev->name, ver->fw_patch_num);
1601 kfree_skb(skb);
Marcel Holtmann40cb0982014-07-02 12:06:45 +02001602 btusb_check_bdaddr_intel(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001603 return 0;
1604 }
1605
1606 /* Opens the firmware patch file based on the firmware version read
1607 * from the controller. If it fails to open the matching firmware
1608 * patch file, it tries to open the default firmware patch file.
1609 * If no patch file is found, allow the device to operate without
1610 * a patch.
1611 */
1612 fw = btusb_setup_intel_get_fw(hdev, ver);
1613 if (!fw) {
1614 kfree_skb(skb);
Marcel Holtmann40cb0982014-07-02 12:06:45 +02001615 btusb_check_bdaddr_intel(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001616 return 0;
1617 }
1618 fw_ptr = fw->data;
1619
1620 /* This Intel specific command enables the manufacturer mode of the
1621 * controller.
1622 *
1623 * Only while this mode is enabled, the driver can download the
1624 * firmware patch data and configuration parameters.
1625 */
1626 skb = __hci_cmd_sync(hdev, 0xfc11, 2, mfg_enable, HCI_INIT_TIMEOUT);
1627 if (IS_ERR(skb)) {
1628 BT_ERR("%s entering Intel manufacturer mode failed (%ld)",
1629 hdev->name, PTR_ERR(skb));
1630 release_firmware(fw);
Adam Leed9c78e92013-07-10 10:02:12 +08001631 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001632 }
1633
1634 if (skb->data[0]) {
1635 u8 evt_status = skb->data[0];
Marcel Holtmann89e75332014-09-16 04:44:50 +02001636
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001637 BT_ERR("%s enable Intel manufacturer mode event failed (%02x)",
1638 hdev->name, evt_status);
1639 kfree_skb(skb);
1640 release_firmware(fw);
1641 return -bt_to_errno(evt_status);
1642 }
1643 kfree_skb(skb);
1644
1645 disable_patch = 1;
1646
1647 /* The firmware data file consists of list of Intel specific HCI
1648 * commands and its expected events. The first byte indicates the
1649 * type of the message, either HCI command or HCI event.
1650 *
1651 * It reads the command and its expected event from the firmware file,
1652 * and send to the controller. Once __hci_cmd_sync_ev() returns,
1653 * the returned event is compared with the event read from the firmware
1654 * file and it will continue until all the messages are downloaded to
1655 * the controller.
1656 *
1657 * Once the firmware patching is completed successfully,
1658 * the manufacturer mode is disabled with reset and activating the
1659 * downloaded patch.
1660 *
1661 * If the firmware patching fails, the manufacturer mode is
1662 * disabled with reset and deactivating the patch.
1663 *
1664 * If the default patch file is used, no reset is done when disabling
1665 * the manufacturer.
1666 */
1667 while (fw->size > fw_ptr - fw->data) {
1668 int ret;
1669
1670 ret = btusb_setup_intel_patching(hdev, fw, &fw_ptr,
1671 &disable_patch);
1672 if (ret < 0)
1673 goto exit_mfg_deactivate;
1674 }
1675
1676 release_firmware(fw);
1677
1678 if (disable_patch)
1679 goto exit_mfg_disable;
1680
1681 /* Patching completed successfully and disable the manufacturer mode
1682 * with reset and activate the downloaded firmware patches.
1683 */
1684 skb = __hci_cmd_sync(hdev, 0xfc11, sizeof(mfg_reset_activate),
1685 mfg_reset_activate, HCI_INIT_TIMEOUT);
1686 if (IS_ERR(skb)) {
1687 BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
1688 hdev->name, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001689 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001690 }
1691 kfree_skb(skb);
1692
1693 BT_INFO("%s: Intel Bluetooth firmware patch completed and activated",
1694 hdev->name);
1695
Marcel Holtmann40cb0982014-07-02 12:06:45 +02001696 btusb_check_bdaddr_intel(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001697 return 0;
1698
1699exit_mfg_disable:
1700 /* Disable the manufacturer mode without reset */
1701 skb = __hci_cmd_sync(hdev, 0xfc11, sizeof(mfg_disable), mfg_disable,
1702 HCI_INIT_TIMEOUT);
1703 if (IS_ERR(skb)) {
1704 BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
1705 hdev->name, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001706 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001707 }
1708 kfree_skb(skb);
1709
1710 BT_INFO("%s: Intel Bluetooth firmware patch completed", hdev->name);
Marcel Holtmann40cb0982014-07-02 12:06:45 +02001711
1712 btusb_check_bdaddr_intel(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001713 return 0;
1714
1715exit_mfg_deactivate:
1716 release_firmware(fw);
1717
1718 /* Patching failed. Disable the manufacturer mode with reset and
1719 * deactivate the downloaded firmware patches.
1720 */
1721 skb = __hci_cmd_sync(hdev, 0xfc11, sizeof(mfg_reset_deactivate),
1722 mfg_reset_deactivate, HCI_INIT_TIMEOUT);
1723 if (IS_ERR(skb)) {
1724 BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
1725 hdev->name, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001726 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001727 }
1728 kfree_skb(skb);
1729
1730 BT_INFO("%s: Intel Bluetooth firmware patch completed and deactivated",
1731 hdev->name);
1732
Marcel Holtmann40cb0982014-07-02 12:06:45 +02001733 btusb_check_bdaddr_intel(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001734 return 0;
1735}
1736
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08001737static int inject_cmd_complete(struct hci_dev *hdev, __u16 opcode)
1738{
1739 struct sk_buff *skb;
1740 struct hci_event_hdr *hdr;
1741 struct hci_ev_cmd_complete *evt;
1742
1743 skb = bt_skb_alloc(sizeof(*hdr) + sizeof(*evt) + 1, GFP_ATOMIC);
1744 if (!skb)
1745 return -ENOMEM;
1746
1747 hdr = (struct hci_event_hdr *)skb_put(skb, sizeof(*hdr));
1748 hdr->evt = HCI_EV_CMD_COMPLETE;
1749 hdr->plen = sizeof(*evt) + 1;
1750
1751 evt = (struct hci_ev_cmd_complete *)skb_put(skb, sizeof(*evt));
1752 evt->ncmd = 0x01;
1753 evt->opcode = cpu_to_le16(opcode);
1754
1755 *skb_put(skb, 1) = 0x00;
1756
1757 bt_cb(skb)->pkt_type = HCI_EVENT_PKT;
1758
1759 return hci_recv_frame(hdev, skb);
1760}
1761
1762static int btusb_recv_bulk_intel(struct btusb_data *data, void *buffer,
1763 int count)
1764{
1765 /* When the device is in bootloader mode, then it can send
1766 * events via the bulk endpoint. These events are treated the
1767 * same way as the ones received from the interrupt endpoint.
1768 */
1769 if (test_bit(BTUSB_BOOTLOADER, &data->flags))
1770 return btusb_recv_intr(data, buffer, count);
1771
1772 return btusb_recv_bulk(data, buffer, count);
1773}
1774
1775static int btusb_recv_event_intel(struct hci_dev *hdev, struct sk_buff *skb)
1776{
1777 struct btusb_data *data = hci_get_drvdata(hdev);
1778
1779 if (test_bit(BTUSB_BOOTLOADER, &data->flags)) {
1780 struct hci_event_hdr *hdr = (void *)skb->data;
1781
1782 /* When the firmware loading completes the device sends
1783 * out a vendor specific event indicating the result of
1784 * the firmware loading.
1785 */
1786 if (skb->len == 7 && hdr->evt == 0xff && hdr->plen == 0x05 &&
1787 skb->data[2] == 0x06) {
1788 if (skb->data[3] != 0x00)
1789 test_bit(BTUSB_FIRMWARE_FAILED, &data->flags);
1790
1791 if (test_and_clear_bit(BTUSB_DOWNLOADING, &data->flags))
1792 wake_up_interruptible(&hdev->req_wait_q);
1793 }
1794
1795 /* When switching to the operational firmware the device
1796 * sends a vendor specific event indicating that the bootup
1797 * completed.
1798 */
1799 if (skb->len == 9 && hdr->evt == 0xff && hdr->plen == 0x07 &&
1800 skb->data[2] == 0x02) {
1801 if (test_and_clear_bit(BTUSB_BOOTING, &data->flags))
1802 wake_up_interruptible(&hdev->req_wait_q);
1803 }
1804 }
1805
1806 return hci_recv_frame(hdev, skb);
1807}
1808
1809static int btusb_send_frame_intel(struct hci_dev *hdev, struct sk_buff *skb)
1810{
1811 struct btusb_data *data = hci_get_drvdata(hdev);
1812 struct urb *urb;
1813
1814 BT_DBG("%s", hdev->name);
1815
1816 if (!test_bit(HCI_RUNNING, &hdev->flags))
1817 return -EBUSY;
1818
1819 switch (bt_cb(skb)->pkt_type) {
1820 case HCI_COMMAND_PKT:
1821 if (test_bit(BTUSB_BOOTLOADER, &data->flags)) {
1822 struct hci_command_hdr *cmd = (void *)skb->data;
1823 __u16 opcode = le16_to_cpu(cmd->opcode);
1824
1825 /* When in bootloader mode and the command 0xfc09
1826 * is received, it needs to be send down the
1827 * bulk endpoint. So allocate a bulk URB instead.
1828 */
1829 if (opcode == 0xfc09)
1830 urb = alloc_bulk_urb(hdev, skb);
1831 else
1832 urb = alloc_ctrl_urb(hdev, skb);
1833
1834 /* When the 0xfc01 command is issued to boot into
1835 * the operational firmware, it will actually not
1836 * send a command complete event. To keep the flow
1837 * control working inject that event here.
1838 */
1839 if (opcode == 0xfc01)
1840 inject_cmd_complete(hdev, opcode);
1841 } else {
1842 urb = alloc_ctrl_urb(hdev, skb);
1843 }
1844 if (IS_ERR(urb))
1845 return PTR_ERR(urb);
1846
1847 hdev->stat.cmd_tx++;
1848 return submit_or_queue_tx_urb(hdev, urb);
1849
1850 case HCI_ACLDATA_PKT:
1851 urb = alloc_bulk_urb(hdev, skb);
1852 if (IS_ERR(urb))
1853 return PTR_ERR(urb);
1854
1855 hdev->stat.acl_tx++;
1856 return submit_or_queue_tx_urb(hdev, urb);
1857
1858 case HCI_SCODATA_PKT:
1859 if (hci_conn_num(hdev, SCO_LINK) < 1)
1860 return -ENODEV;
1861
1862 urb = alloc_isoc_urb(hdev, skb);
1863 if (IS_ERR(urb))
1864 return PTR_ERR(urb);
1865
1866 hdev->stat.sco_tx++;
1867 return submit_tx_urb(hdev, urb);
1868 }
1869
1870 return -EILSEQ;
1871}
1872
1873static int btusb_intel_secure_send(struct hci_dev *hdev, u8 fragment_type,
1874 u32 plen, const void *param)
1875{
1876 while (plen > 0) {
1877 struct sk_buff *skb;
1878 u8 cmd_param[253], fragment_len = (plen > 252) ? 252 : plen;
1879
1880 cmd_param[0] = fragment_type;
1881 memcpy(cmd_param + 1, param, fragment_len);
1882
1883 skb = __hci_cmd_sync(hdev, 0xfc09, fragment_len + 1,
1884 cmd_param, HCI_INIT_TIMEOUT);
1885 if (IS_ERR(skb))
1886 return PTR_ERR(skb);
1887
1888 kfree_skb(skb);
1889
1890 plen -= fragment_len;
1891 param += fragment_len;
1892 }
1893
1894 return 0;
1895}
1896
1897static void btusb_intel_version_info(struct hci_dev *hdev,
1898 struct intel_version *ver)
1899{
1900 const char *variant;
1901
1902 switch (ver->fw_variant) {
1903 case 0x06:
1904 variant = "Bootloader";
1905 break;
1906 case 0x23:
1907 variant = "Firmware";
1908 break;
1909 default:
1910 return;
1911 }
1912
1913 BT_INFO("%s: %s revision %u.%u build %u week %u %u", hdev->name,
1914 variant, ver->fw_revision >> 4, ver->fw_revision & 0x0f,
1915 ver->fw_build_num, ver->fw_build_ww, 2000 + ver->fw_build_yy);
1916}
1917
1918static int btusb_setup_intel_new(struct hci_dev *hdev)
1919{
1920 static const u8 reset_param[] = { 0x00, 0x01, 0x00, 0x01,
1921 0x00, 0x08, 0x04, 0x00 };
1922 struct btusb_data *data = hci_get_drvdata(hdev);
1923 struct sk_buff *skb;
1924 struct intel_version *ver;
1925 struct intel_boot_params *params;
1926 const struct firmware *fw;
1927 const u8 *fw_ptr;
1928 char fwname[64];
1929 ktime_t calltime, delta, rettime;
1930 unsigned long long duration;
1931 int err;
1932
1933 BT_DBG("%s", hdev->name);
1934
1935 calltime = ktime_get();
1936
1937 /* Read the Intel version information to determine if the device
1938 * is in bootloader mode or if it already has operational firmware
1939 * loaded.
1940 */
1941 skb = __hci_cmd_sync(hdev, 0xfc05, 0, NULL, HCI_INIT_TIMEOUT);
1942 if (IS_ERR(skb)) {
1943 BT_ERR("%s: Reading Intel version information failed (%ld)",
1944 hdev->name, PTR_ERR(skb));
1945 return PTR_ERR(skb);
1946 }
1947
1948 if (skb->len != sizeof(*ver)) {
1949 BT_ERR("%s: Intel version event size mismatch", hdev->name);
1950 kfree_skb(skb);
1951 return -EILSEQ;
1952 }
1953
1954 ver = (struct intel_version *)skb->data;
1955 if (ver->status) {
1956 BT_ERR("%s: Intel version command failure (%02x)",
1957 hdev->name, ver->status);
1958 err = -bt_to_errno(ver->status);
1959 kfree_skb(skb);
1960 return err;
1961 }
1962
1963 /* The hardware platform number has a fixed value of 0x37 and
1964 * for now only accept this single value.
1965 */
1966 if (ver->hw_platform != 0x37) {
1967 BT_ERR("%s: Unsupported Intel hardware platform (%u)",
1968 hdev->name, ver->hw_platform);
1969 kfree_skb(skb);
1970 return -EINVAL;
1971 }
1972
1973 /* At the moment only the hardware variant iBT 3.0 (LnP/SfP) is
1974 * supported by this firmware loading method. This check has been
1975 * put in place to ensure correct forward compatibility options
1976 * when newer hardware variants come along.
1977 */
1978 if (ver->hw_variant != 0x0b) {
1979 BT_ERR("%s: Unsupported Intel hardware variant (%u)",
1980 hdev->name, ver->hw_variant);
1981 kfree_skb(skb);
1982 return -EINVAL;
1983 }
1984
1985 btusb_intel_version_info(hdev, ver);
1986
1987 /* The firmware variant determines if the device is in bootloader
1988 * mode or is running operational firmware. The value 0x06 identifies
1989 * the bootloader and the value 0x23 identifies the operational
1990 * firmware.
1991 *
1992 * When the operational firmware is already present, then only
1993 * the check for valid Bluetooth device address is needed. This
1994 * determines if the device will be added as configured or
1995 * unconfigured controller.
1996 *
1997 * It is not possible to use the Secure Boot Parameters in this
1998 * case since that command is only available in bootloader mode.
1999 */
2000 if (ver->fw_variant == 0x23) {
2001 kfree_skb(skb);
2002 clear_bit(BTUSB_BOOTLOADER, &data->flags);
2003 btusb_check_bdaddr_intel(hdev);
2004 return 0;
2005 }
2006
2007 /* If the device is not in bootloader mode, then the only possible
2008 * choice is to return an error and abort the device initialization.
2009 */
2010 if (ver->fw_variant != 0x06) {
2011 BT_ERR("%s: Unsupported Intel firmware variant (%u)",
2012 hdev->name, ver->fw_variant);
2013 kfree_skb(skb);
2014 return -ENODEV;
2015 }
2016
2017 kfree_skb(skb);
2018
2019 /* Read the secure boot parameters to identify the operating
2020 * details of the bootloader.
2021 */
2022 skb = __hci_cmd_sync(hdev, 0xfc0d, 0, NULL, HCI_INIT_TIMEOUT);
2023 if (IS_ERR(skb)) {
2024 BT_ERR("%s: Reading Intel boot parameters failed (%ld)",
2025 hdev->name, PTR_ERR(skb));
2026 return PTR_ERR(skb);
2027 }
2028
2029 if (skb->len != sizeof(*params)) {
2030 BT_ERR("%s: Intel boot parameters size mismatch", hdev->name);
2031 kfree_skb(skb);
2032 return -EILSEQ;
2033 }
2034
2035 params = (struct intel_boot_params *)skb->data;
2036 if (params->status) {
2037 BT_ERR("%s: Intel boot parameters command failure (%02x)",
2038 hdev->name, params->status);
2039 err = -bt_to_errno(params->status);
2040 kfree_skb(skb);
2041 return err;
2042 }
2043
2044 BT_INFO("%s: Device revision is %u", hdev->name,
2045 le16_to_cpu(params->dev_revid));
2046
2047 BT_INFO("%s: Secure boot is %s", hdev->name,
2048 params->secure_boot ? "enabled" : "disabled");
2049
2050 BT_INFO("%s: Minimum firmware build %u week %u %u", hdev->name,
2051 params->min_fw_build_nn, params->min_fw_build_cw,
2052 2000 + params->min_fw_build_yy);
2053
2054 /* It is required that every single firmware fragment is acknowledged
2055 * with a command complete event. If the boot parameters indicate
2056 * that this bootloader does not send them, then abort the setup.
2057 */
2058 if (params->limited_cce != 0x00) {
2059 BT_ERR("%s: Unsupported Intel firmware loading method (%u)",
2060 hdev->name, params->limited_cce);
2061 kfree_skb(skb);
2062 return -EINVAL;
2063 }
2064
2065 /* If the OTP has no valid Bluetooth device address, then there will
2066 * also be no valid address for the operational firmware.
2067 */
2068 if (!bacmp(&params->otp_bdaddr, BDADDR_ANY)) {
2069 BT_INFO("%s: No device address configured", hdev->name);
2070 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
2071 }
2072
2073 /* With this Intel bootloader only the hardware variant and device
2074 * revision information are used to select the right firmware.
2075 *
2076 * Currently this bootloader support is limited to hardware variant
2077 * iBT 3.0 (LnP/SfP) which is identified by the value 11 (0x0b).
2078 */
2079 snprintf(fwname, sizeof(fwname), "intel/ibt-11-%u.sfi",
2080 le16_to_cpu(params->dev_revid));
2081
2082 err = request_firmware(&fw, fwname, &hdev->dev);
2083 if (err < 0) {
2084 BT_ERR("%s: Failed to load Intel firmware file (%d)",
2085 hdev->name, err);
2086 kfree_skb(skb);
2087 return err;
2088 }
2089
2090 BT_INFO("%s: Found device firmware: %s", hdev->name, fwname);
2091
2092 kfree_skb(skb);
2093
2094 if (fw->size < 644) {
2095 BT_ERR("%s: Invalid size of firmware file (%zu)",
2096 hdev->name, fw->size);
2097 err = -EBADF;
2098 goto done;
2099 }
2100
2101 set_bit(BTUSB_DOWNLOADING, &data->flags);
2102
2103 /* Start the firmware download transaction with the Init fragment
2104 * represented by the 128 bytes of CSS header.
2105 */
2106 err = btusb_intel_secure_send(hdev, 0x00, 128, fw->data);
2107 if (err < 0) {
2108 BT_ERR("%s: Failed to send firmware header (%d)",
2109 hdev->name, err);
2110 goto done;
2111 }
2112
2113 /* Send the 256 bytes of public key information from the firmware
2114 * as the PKey fragment.
2115 */
2116 err = btusb_intel_secure_send(hdev, 0x03, 256, fw->data + 128);
2117 if (err < 0) {
2118 BT_ERR("%s: Failed to send firmware public key (%d)",
2119 hdev->name, err);
2120 goto done;
2121 }
2122
2123 /* Send the 256 bytes of signature information from the firmware
2124 * as the Sign fragment.
2125 */
2126 err = btusb_intel_secure_send(hdev, 0x02, 256, fw->data + 388);
2127 if (err < 0) {
2128 BT_ERR("%s: Failed to send firmware signature (%d)",
2129 hdev->name, err);
2130 goto done;
2131 }
2132
2133 fw_ptr = fw->data + 644;
2134
2135 while (fw_ptr - fw->data < fw->size) {
2136 struct hci_command_hdr *cmd = (void *)fw_ptr;
2137 u8 cmd_len;
2138
2139 cmd_len = sizeof(*cmd) + cmd->plen;
2140
2141 /* Send each command from the firmware data buffer as
2142 * a single Data fragment.
2143 */
2144 err = btusb_intel_secure_send(hdev, 0x01, cmd_len, fw_ptr);
2145 if (err < 0) {
2146 BT_ERR("%s: Failed to send firmware data (%d)",
2147 hdev->name, err);
2148 goto done;
2149 }
2150
2151 fw_ptr += cmd_len;
2152 }
2153
2154 /* Before switching the device into operational mode and with that
2155 * booting the loaded firmware, wait for the bootloader notification
2156 * that all fragments have been successfully received.
2157 *
2158 * When the event processing receives the notification, then this
2159 * flag will be cleared. So just in case that happens really quickly,
2160 * check it first before adding the wait queue.
2161 */
2162 if (test_bit(BTUSB_DOWNLOADING, &data->flags)) {
2163 DECLARE_WAITQUEUE(wait, current);
2164 signed long timeout;
2165
2166 BT_INFO("%s: Waiting for firmware download to complete",
2167 hdev->name);
2168
2169 add_wait_queue(&hdev->req_wait_q, &wait);
2170 set_current_state(TASK_INTERRUPTIBLE);
2171
2172 /* The firmware loading should not take longer than 5 seconds
2173 * and thus just timeout if that happens and fail the setup
2174 * of this device.
2175 */
2176 timeout = schedule_timeout(msecs_to_jiffies(5000));
2177
2178 remove_wait_queue(&hdev->req_wait_q, &wait);
2179
2180 if (signal_pending(current)) {
2181 BT_ERR("%s: Firmware loading interrupted", hdev->name);
2182 err = -EINTR;
2183 goto done;
2184 }
2185
2186 if (!timeout) {
2187 BT_ERR("%s: Firmware loading timeout", hdev->name);
2188 err = -ETIMEDOUT;
2189 goto done;
2190 }
2191 }
2192
2193 if (test_bit(BTUSB_FIRMWARE_FAILED, &data->flags)) {
2194 BT_ERR("%s: Firmware loading failed", hdev->name);
2195 err = -ENOEXEC;
2196 goto done;
2197 }
2198
2199 rettime = ktime_get();
2200 delta = ktime_sub(rettime, calltime);
2201 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
2202
2203 BT_INFO("%s: Firmware loaded in %llu usecs", hdev->name, duration);
2204
2205done:
2206 release_firmware(fw);
2207
2208 if (err < 0)
2209 return err;
2210
2211 calltime = ktime_get();
2212
2213 set_bit(BTUSB_BOOTING, &data->flags);
2214
2215 skb = __hci_cmd_sync(hdev, 0xfc01, sizeof(reset_param), reset_param,
2216 HCI_INIT_TIMEOUT);
2217 if (IS_ERR(skb))
2218 return PTR_ERR(skb);
2219
2220 kfree_skb(skb);
2221
2222 /* The bootloader will not indicate when the device is ready. This
2223 * is done by the operational firmware sending bootup notification.
2224 */
2225 if (test_bit(BTUSB_BOOTING, &data->flags)) {
2226 DECLARE_WAITQUEUE(wait, current);
2227 signed long timeout;
2228
2229 BT_INFO("%s: Waiting for device to boot", hdev->name);
2230
2231 add_wait_queue(&hdev->req_wait_q, &wait);
2232 set_current_state(TASK_INTERRUPTIBLE);
2233
2234 /* Booting into operational firmware should not take
2235 * longer than 1 second. However if that happens, then
2236 * just fail the setup since something went wrong.
2237 */
2238 timeout = schedule_timeout(msecs_to_jiffies(1000));
2239
2240 remove_wait_queue(&hdev->req_wait_q, &wait);
2241
2242 if (signal_pending(current)) {
2243 BT_ERR("%s: Device boot interrupted", hdev->name);
2244 return -EINTR;
2245 }
2246
2247 if (!timeout) {
2248 BT_ERR("%s: Device boot timeout", hdev->name);
2249 return -ETIMEDOUT;
2250 }
2251 }
2252
2253 rettime = ktime_get();
2254 delta = ktime_sub(rettime, calltime);
2255 duration = (unsigned long long) ktime_to_ns(delta) >> 10;
2256
2257 BT_INFO("%s: Device booted in %llu usecs", hdev->name, duration);
2258
2259 clear_bit(BTUSB_BOOTLOADER, &data->flags);
2260
2261 return 0;
2262}
2263
Marcel Holtmanncb8d6592014-07-02 11:25:25 +02002264static int btusb_set_bdaddr_intel(struct hci_dev *hdev, const bdaddr_t *bdaddr)
2265{
2266 struct sk_buff *skb;
2267 long ret;
2268
2269 skb = __hci_cmd_sync(hdev, 0xfc31, 6, bdaddr, HCI_INIT_TIMEOUT);
2270 if (IS_ERR(skb)) {
2271 ret = PTR_ERR(skb);
2272 BT_ERR("%s: changing Intel device address failed (%ld)",
Marcel Holtmann89e75332014-09-16 04:44:50 +02002273 hdev->name, ret);
Marcel Holtmanncb8d6592014-07-02 11:25:25 +02002274 return ret;
2275 }
2276 kfree_skb(skb);
2277
2278 return 0;
2279}
2280
Amitkumar Karwarae8df492014-07-18 14:47:06 -07002281static int btusb_set_bdaddr_marvell(struct hci_dev *hdev,
2282 const bdaddr_t *bdaddr)
2283{
2284 struct sk_buff *skb;
2285 u8 buf[8];
2286 long ret;
2287
2288 buf[0] = 0xfe;
2289 buf[1] = sizeof(bdaddr_t);
2290 memcpy(buf + 2, bdaddr, sizeof(bdaddr_t));
2291
2292 skb = __hci_cmd_sync(hdev, 0xfc22, sizeof(buf), buf, HCI_INIT_TIMEOUT);
2293 if (IS_ERR(skb)) {
2294 ret = PTR_ERR(skb);
2295 BT_ERR("%s: changing Marvell device address failed (%ld)",
2296 hdev->name, ret);
2297 return ret;
2298 }
2299 kfree_skb(skb);
2300
2301 return 0;
2302}
2303
Marcel Holtmannc8abb732014-07-02 12:38:22 +02002304#define BDADDR_BCM20702A0 (&(bdaddr_t) {{0x00, 0xa0, 0x02, 0x70, 0x20, 0x00}})
2305
Petri Gynther10d4c672014-05-08 15:50:01 -07002306static int btusb_setup_bcm_patchram(struct hci_dev *hdev)
2307{
2308 struct btusb_data *data = hci_get_drvdata(hdev);
2309 struct usb_device *udev = data->udev;
2310 char fw_name[64];
2311 const struct firmware *fw;
2312 const u8 *fw_ptr;
2313 size_t fw_size;
2314 const struct hci_command_hdr *cmd;
2315 const u8 *cmd_param;
2316 u16 opcode;
2317 struct sk_buff *skb;
2318 struct hci_rp_read_local_version *ver;
Marcel Holtmannc8abb732014-07-02 12:38:22 +02002319 struct hci_rp_read_bd_addr *bda;
Petri Gynther10d4c672014-05-08 15:50:01 -07002320 long ret;
2321
2322 snprintf(fw_name, sizeof(fw_name), "brcm/%s-%04x-%04x.hcd",
2323 udev->product ? udev->product : "BCM",
2324 le16_to_cpu(udev->descriptor.idVendor),
2325 le16_to_cpu(udev->descriptor.idProduct));
2326
2327 ret = request_firmware(&fw, fw_name, &hdev->dev);
2328 if (ret < 0) {
Marcel Holtmannc8abb732014-07-02 12:38:22 +02002329 BT_INFO("%s: BCM: patch %s not found", hdev->name, fw_name);
Petri Gynther10d4c672014-05-08 15:50:01 -07002330 return 0;
2331 }
2332
2333 /* Reset */
2334 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
2335 if (IS_ERR(skb)) {
2336 ret = PTR_ERR(skb);
2337 BT_ERR("%s: HCI_OP_RESET failed (%ld)", hdev->name, ret);
2338 goto done;
2339 }
2340 kfree_skb(skb);
2341
2342 /* Read Local Version Info */
2343 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
2344 HCI_INIT_TIMEOUT);
2345 if (IS_ERR(skb)) {
2346 ret = PTR_ERR(skb);
2347 BT_ERR("%s: HCI_OP_READ_LOCAL_VERSION failed (%ld)",
Marcel Holtmann89e75332014-09-16 04:44:50 +02002348 hdev->name, ret);
Petri Gynther10d4c672014-05-08 15:50:01 -07002349 goto done;
2350 }
2351
2352 if (skb->len != sizeof(*ver)) {
2353 BT_ERR("%s: HCI_OP_READ_LOCAL_VERSION event length mismatch",
Marcel Holtmann89e75332014-09-16 04:44:50 +02002354 hdev->name);
Petri Gynther10d4c672014-05-08 15:50:01 -07002355 kfree_skb(skb);
2356 ret = -EIO;
2357 goto done;
2358 }
2359
Marcel Holtmann89e75332014-09-16 04:44:50 +02002360 ver = (struct hci_rp_read_local_version *)skb->data;
Petri Gynther10d4c672014-05-08 15:50:01 -07002361 BT_INFO("%s: BCM: patching hci_ver=%02x hci_rev=%04x lmp_ver=%02x "
2362 "lmp_subver=%04x", hdev->name, ver->hci_ver, ver->hci_rev,
2363 ver->lmp_ver, ver->lmp_subver);
2364 kfree_skb(skb);
2365
2366 /* Start Download */
2367 skb = __hci_cmd_sync(hdev, 0xfc2e, 0, NULL, HCI_INIT_TIMEOUT);
2368 if (IS_ERR(skb)) {
2369 ret = PTR_ERR(skb);
2370 BT_ERR("%s: BCM: Download Minidrv command failed (%ld)",
Marcel Holtmann89e75332014-09-16 04:44:50 +02002371 hdev->name, ret);
Petri Gynther10d4c672014-05-08 15:50:01 -07002372 goto reset_fw;
2373 }
2374 kfree_skb(skb);
2375
2376 /* 50 msec delay after Download Minidrv completes */
2377 msleep(50);
2378
2379 fw_ptr = fw->data;
2380 fw_size = fw->size;
2381
2382 while (fw_size >= sizeof(*cmd)) {
Marcel Holtmann89e75332014-09-16 04:44:50 +02002383 cmd = (struct hci_command_hdr *)fw_ptr;
Petri Gynther10d4c672014-05-08 15:50:01 -07002384 fw_ptr += sizeof(*cmd);
2385 fw_size -= sizeof(*cmd);
2386
2387 if (fw_size < cmd->plen) {
2388 BT_ERR("%s: BCM: patch %s is corrupted",
Marcel Holtmann89e75332014-09-16 04:44:50 +02002389 hdev->name, fw_name);
Petri Gynther10d4c672014-05-08 15:50:01 -07002390 ret = -EINVAL;
2391 goto reset_fw;
2392 }
2393
2394 cmd_param = fw_ptr;
2395 fw_ptr += cmd->plen;
2396 fw_size -= cmd->plen;
2397
2398 opcode = le16_to_cpu(cmd->opcode);
2399
2400 skb = __hci_cmd_sync(hdev, opcode, cmd->plen, cmd_param,
2401 HCI_INIT_TIMEOUT);
2402 if (IS_ERR(skb)) {
2403 ret = PTR_ERR(skb);
2404 BT_ERR("%s: BCM: patch command %04x failed (%ld)",
Marcel Holtmann89e75332014-09-16 04:44:50 +02002405 hdev->name, opcode, ret);
Petri Gynther10d4c672014-05-08 15:50:01 -07002406 goto reset_fw;
2407 }
2408 kfree_skb(skb);
2409 }
2410
2411 /* 250 msec delay after Launch Ram completes */
2412 msleep(250);
2413
2414reset_fw:
2415 /* Reset */
2416 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
2417 if (IS_ERR(skb)) {
2418 ret = PTR_ERR(skb);
2419 BT_ERR("%s: HCI_OP_RESET failed (%ld)", hdev->name, ret);
2420 goto done;
2421 }
2422 kfree_skb(skb);
2423
2424 /* Read Local Version Info */
2425 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
2426 HCI_INIT_TIMEOUT);
2427 if (IS_ERR(skb)) {
2428 ret = PTR_ERR(skb);
2429 BT_ERR("%s: HCI_OP_READ_LOCAL_VERSION failed (%ld)",
Marcel Holtmann89e75332014-09-16 04:44:50 +02002430 hdev->name, ret);
Petri Gynther10d4c672014-05-08 15:50:01 -07002431 goto done;
2432 }
2433
2434 if (skb->len != sizeof(*ver)) {
2435 BT_ERR("%s: HCI_OP_READ_LOCAL_VERSION event length mismatch",
Marcel Holtmann89e75332014-09-16 04:44:50 +02002436 hdev->name);
Petri Gynther10d4c672014-05-08 15:50:01 -07002437 kfree_skb(skb);
2438 ret = -EIO;
2439 goto done;
2440 }
2441
Marcel Holtmann89e75332014-09-16 04:44:50 +02002442 ver = (struct hci_rp_read_local_version *)skb->data;
Petri Gynther10d4c672014-05-08 15:50:01 -07002443 BT_INFO("%s: BCM: firmware hci_ver=%02x hci_rev=%04x lmp_ver=%02x "
2444 "lmp_subver=%04x", hdev->name, ver->hci_ver, ver->hci_rev,
2445 ver->lmp_ver, ver->lmp_subver);
2446 kfree_skb(skb);
2447
Marcel Holtmannc8abb732014-07-02 12:38:22 +02002448 /* Read BD Address */
2449 skb = __hci_cmd_sync(hdev, HCI_OP_READ_BD_ADDR, 0, NULL,
2450 HCI_INIT_TIMEOUT);
2451 if (IS_ERR(skb)) {
2452 ret = PTR_ERR(skb);
2453 BT_ERR("%s: HCI_OP_READ_BD_ADDR failed (%ld)",
Marcel Holtmann89e75332014-09-16 04:44:50 +02002454 hdev->name, ret);
Marcel Holtmannc8abb732014-07-02 12:38:22 +02002455 goto done;
2456 }
2457
2458 if (skb->len != sizeof(*bda)) {
2459 BT_ERR("%s: HCI_OP_READ_BD_ADDR event length mismatch",
Marcel Holtmann89e75332014-09-16 04:44:50 +02002460 hdev->name);
Marcel Holtmannc8abb732014-07-02 12:38:22 +02002461 kfree_skb(skb);
2462 ret = -EIO;
2463 goto done;
2464 }
2465
Marcel Holtmann89e75332014-09-16 04:44:50 +02002466 bda = (struct hci_rp_read_bd_addr *)skb->data;
Marcel Holtmannc8abb732014-07-02 12:38:22 +02002467 if (bda->status) {
2468 BT_ERR("%s: HCI_OP_READ_BD_ADDR error status (%02x)",
2469 hdev->name, bda->status);
2470 kfree_skb(skb);
2471 ret = -bt_to_errno(bda->status);
2472 goto done;
2473 }
2474
2475 /* The address 00:20:70:02:A0:00 indicates a BCM20702A0 controller
2476 * with no configured address.
2477 */
Marcel Holtmann849e5082014-07-04 16:54:39 +02002478 if (!bacmp(&bda->bdaddr, BDADDR_BCM20702A0)) {
Marcel Holtmannc8abb732014-07-02 12:38:22 +02002479 BT_INFO("%s: BCM: using default device address (%pMR)",
2480 hdev->name, &bda->bdaddr);
Marcel Holtmann849e5082014-07-04 16:54:39 +02002481 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
2482 }
Marcel Holtmannc8abb732014-07-02 12:38:22 +02002483
2484 kfree_skb(skb);
2485
Petri Gynther10d4c672014-05-08 15:50:01 -07002486done:
2487 release_firmware(fw);
2488
2489 return ret;
2490}
2491
Marcel Holtmannabbaf502014-07-02 00:53:48 +02002492static int btusb_set_bdaddr_bcm(struct hci_dev *hdev, const bdaddr_t *bdaddr)
2493{
2494 struct sk_buff *skb;
2495 long ret;
2496
2497 skb = __hci_cmd_sync(hdev, 0xfc01, 6, bdaddr, HCI_INIT_TIMEOUT);
2498 if (IS_ERR(skb)) {
2499 ret = PTR_ERR(skb);
2500 BT_ERR("%s: BCM: Change address command failed (%ld)",
Marcel Holtmann89e75332014-09-16 04:44:50 +02002501 hdev->name, ret);
Marcel Holtmannabbaf502014-07-02 00:53:48 +02002502 return ret;
2503 }
2504 kfree_skb(skb);
2505
2506 return 0;
2507}
2508
Toshi Kikuchi58592232014-12-12 10:58:05 -08002509static int btusb_set_bdaddr_ath3012(struct hci_dev *hdev,
2510 const bdaddr_t *bdaddr)
2511{
2512 struct sk_buff *skb;
2513 u8 buf[10];
2514 long ret;
2515
2516 buf[0] = 0x01;
2517 buf[1] = 0x01;
2518 buf[2] = 0x00;
2519 buf[3] = sizeof(bdaddr_t);
2520 memcpy(buf + 4, bdaddr, sizeof(bdaddr_t));
2521
2522 skb = __hci_cmd_sync(hdev, 0xfc0b, sizeof(buf), buf, HCI_INIT_TIMEOUT);
2523 if (IS_ERR(skb)) {
2524 ret = PTR_ERR(skb);
2525 BT_ERR("%s: Change address command failed (%ld)",
2526 hdev->name, ret);
2527 return ret;
2528 }
2529 kfree_skb(skb);
2530
2531 return 0;
2532}
2533
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002534static int btusb_probe(struct usb_interface *intf,
Marcel Holtmann89e75332014-09-16 04:44:50 +02002535 const struct usb_device_id *id)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002536{
2537 struct usb_endpoint_descriptor *ep_desc;
2538 struct btusb_data *data;
2539 struct hci_dev *hdev;
2540 int i, err;
2541
2542 BT_DBG("intf %p id %p", intf, id);
2543
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002544 /* interface numbers are hardcoded in the spec */
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002545 if (intf->cur_altsetting->desc.bInterfaceNumber != 0)
2546 return -ENODEV;
2547
2548 if (!id->driver_info) {
2549 const struct usb_device_id *match;
Marcel Holtmann89e75332014-09-16 04:44:50 +02002550
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002551 match = usb_match_id(intf, blacklist_table);
2552 if (match)
2553 id = match;
2554 }
2555
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002556 if (id->driver_info == BTUSB_IGNORE)
2557 return -ENODEV;
2558
Steven.Li2d25f8b2011-07-01 14:02:36 +08002559 if (id->driver_info & BTUSB_ATH3012) {
2560 struct usb_device *udev = interface_to_usbdev(intf);
2561
2562 /* Old firmware would otherwise let ath3k driver load
2563 * patch and sysconfig files */
2564 if (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x0001)
2565 return -ENODEV;
2566 }
2567
Sachin Kamat98921db2012-07-27 12:38:39 +05302568 data = devm_kzalloc(&intf->dev, sizeof(*data), GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002569 if (!data)
2570 return -ENOMEM;
2571
2572 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
2573 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
2574
2575 if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
2576 data->intr_ep = ep_desc;
2577 continue;
2578 }
2579
2580 if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
2581 data->bulk_tx_ep = ep_desc;
2582 continue;
2583 }
2584
2585 if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
2586 data->bulk_rx_ep = ep_desc;
2587 continue;
2588 }
2589 }
2590
Sachin Kamat98921db2012-07-27 12:38:39 +05302591 if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002592 return -ENODEV;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002593
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002594 data->cmdreq_type = USB_TYPE_CLASS;
2595
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002596 data->udev = interface_to_usbdev(intf);
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02002597 data->intf = intf;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002598
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002599 INIT_WORK(&data->work, btusb_work);
Oliver Neukum7bee5492009-08-24 23:44:59 +02002600 INIT_WORK(&data->waker, btusb_waker);
Marcel Holtmann803b5832014-09-16 08:00:29 +02002601 init_usb_anchor(&data->deferred);
2602 init_usb_anchor(&data->tx_anchor);
Oliver Neukum7bee5492009-08-24 23:44:59 +02002603 spin_lock_init(&data->txlock);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002604
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002605 init_usb_anchor(&data->intr_anchor);
2606 init_usb_anchor(&data->bulk_anchor);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02002607 init_usb_anchor(&data->isoc_anchor);
Marcel Holtmann803b5832014-09-16 08:00:29 +02002608 spin_lock_init(&data->rxlock);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002609
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002610 if (id->driver_info & BTUSB_INTEL_NEW) {
2611 data->recv_event = btusb_recv_event_intel;
2612 data->recv_bulk = btusb_recv_bulk_intel;
2613 set_bit(BTUSB_BOOTLOADER, &data->flags);
2614 } else {
2615 data->recv_event = hci_recv_frame;
2616 data->recv_bulk = btusb_recv_bulk;
2617 }
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +01002618
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002619 hdev = hci_alloc_dev();
Sachin Kamat98921db2012-07-27 12:38:39 +05302620 if (!hdev)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002621 return -ENOMEM;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002622
Marcel Holtmannc13854c2010-02-08 15:27:07 +01002623 hdev->bus = HCI_USB;
David Herrmann155961e2012-02-09 21:58:32 +01002624 hci_set_drvdata(hdev, data);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002625
2626 data->hdev = hdev;
2627
2628 SET_HCIDEV_DEV(hdev, &intf->dev);
2629
Marcel Holtmann9f8f9622013-04-10 08:11:35 -07002630 hdev->open = btusb_open;
2631 hdev->close = btusb_close;
2632 hdev->flush = btusb_flush;
2633 hdev->send = btusb_send_frame;
2634 hdev->notify = btusb_notify;
2635
2636 if (id->driver_info & BTUSB_BCM92035)
2637 hdev->setup = btusb_setup_bcm92035;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002638
Marcel Holtmannabbaf502014-07-02 00:53:48 +02002639 if (id->driver_info & BTUSB_BCM_PATCHRAM) {
Petri Gynther10d4c672014-05-08 15:50:01 -07002640 hdev->setup = btusb_setup_bcm_patchram;
Marcel Holtmannabbaf502014-07-02 00:53:48 +02002641 hdev->set_bdaddr = btusb_set_bdaddr_bcm;
Marcel Holtmann27c3fbe2014-11-02 20:52:24 +01002642 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
Marcel Holtmannabbaf502014-07-02 00:53:48 +02002643 }
Petri Gynther10d4c672014-05-08 15:50:01 -07002644
Marcel Holtmanncb8d6592014-07-02 11:25:25 +02002645 if (id->driver_info & BTUSB_INTEL) {
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002646 hdev->setup = btusb_setup_intel;
Marcel Holtmanncb8d6592014-07-02 11:25:25 +02002647 hdev->set_bdaddr = btusb_set_bdaddr_intel;
2648 }
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002649
Marcel Holtmanncda0dd72015-01-26 21:33:48 -08002650 if (id->driver_info & BTUSB_INTEL_NEW) {
2651 hdev->send = btusb_send_frame_intel;
2652 hdev->setup = btusb_setup_intel_new;
2653 hdev->set_bdaddr = btusb_set_bdaddr_intel;
2654 }
2655
Amitkumar Karwarae8df492014-07-18 14:47:06 -07002656 if (id->driver_info & BTUSB_MARVELL)
2657 hdev->set_bdaddr = btusb_set_bdaddr_marvell;
2658
Marcel Holtmann661cf882015-01-02 23:35:20 -08002659 if (id->driver_info & BTUSB_SWAVE) {
2660 set_bit(HCI_QUIRK_FIXUP_INQUIRY_MODE, &hdev->quirks);
Marcel Holtmannd57dbe72014-12-26 04:42:33 +01002661 set_bit(HCI_QUIRK_BROKEN_LOCAL_COMMANDS, &hdev->quirks);
Marcel Holtmann661cf882015-01-02 23:35:20 -08002662 }
Marcel Holtmannd57dbe72014-12-26 04:42:33 +01002663
Marcel Holtmann40df7832014-07-06 13:29:58 +02002664 if (id->driver_info & BTUSB_INTEL_BOOT)
2665 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
2666
Toshi Kikuchi58592232014-12-12 10:58:05 -08002667 if (id->driver_info & BTUSB_ATH3012)
2668 hdev->set_bdaddr = btusb_set_bdaddr_ath3012;
2669
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002670 /* Interface numbers are hardcoded in the specification */
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02002671 data->isoc = usb_ifnum_to_if(data->udev, 1);
2672
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002673 if (!reset)
Szymon Janca6c511c2012-05-23 12:35:46 +02002674 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002675
2676 if (force_scofix || id->driver_info & BTUSB_WRONG_SCO_MTU) {
2677 if (!disable_scofix)
2678 set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
2679 }
2680
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02002681 if (id->driver_info & BTUSB_BROKEN_ISOC)
2682 data->isoc = NULL;
2683
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002684 if (id->driver_info & BTUSB_DIGIANSWER) {
2685 data->cmdreq_type = USB_TYPE_VENDOR;
Szymon Janca6c511c2012-05-23 12:35:46 +02002686 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002687 }
2688
2689 if (id->driver_info & BTUSB_CSR) {
2690 struct usb_device *udev = data->udev;
Marcel Holtmann81cac642014-01-03 03:02:36 -08002691 u16 bcdDevice = le16_to_cpu(udev->descriptor.bcdDevice);
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002692
2693 /* Old firmware would otherwise execute USB reset */
Marcel Holtmann81cac642014-01-03 03:02:36 -08002694 if (bcdDevice < 0x117)
Szymon Janca6c511c2012-05-23 12:35:46 +02002695 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmann81cac642014-01-03 03:02:36 -08002696
2697 /* Fake CSR devices with broken commands */
2698 if (bcdDevice <= 0x100)
2699 hdev->setup = btusb_setup_csr;
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002700 }
2701
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002702 if (id->driver_info & BTUSB_SNIFFER) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02002703 struct usb_device *udev = data->udev;
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002704
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002705 /* New sniffer firmware has crippled HCI interface */
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002706 if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
2707 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
2708 }
2709
Marcel Holtmann3a5ef202014-07-06 14:53:54 +02002710 if (id->driver_info & BTUSB_INTEL_BOOT) {
2711 /* A bug in the bootloader causes that interrupt interface is
2712 * only enabled after receiving SetInterface(0, AltSetting=0).
2713 */
2714 err = usb_set_interface(data->udev, 0, 0);
2715 if (err < 0) {
2716 BT_ERR("failed to set interface 0, alt 0 %d", err);
2717 hci_free_dev(hdev);
2718 return err;
2719 }
2720 }
2721
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02002722 if (data->isoc) {
2723 err = usb_driver_claim_interface(&btusb_driver,
Marcel Holtmann89e75332014-09-16 04:44:50 +02002724 data->isoc, data);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02002725 if (err < 0) {
2726 hci_free_dev(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02002727 return err;
2728 }
2729 }
2730
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002731 err = hci_register_dev(hdev);
2732 if (err < 0) {
2733 hci_free_dev(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002734 return err;
2735 }
2736
2737 usb_set_intfdata(intf, data);
2738
2739 return 0;
2740}
2741
2742static void btusb_disconnect(struct usb_interface *intf)
2743{
2744 struct btusb_data *data = usb_get_intfdata(intf);
2745 struct hci_dev *hdev;
2746
2747 BT_DBG("intf %p", intf);
2748
2749 if (!data)
2750 return;
2751
2752 hdev = data->hdev;
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02002753 usb_set_intfdata(data->intf, NULL);
2754
2755 if (data->isoc)
2756 usb_set_intfdata(data->isoc, NULL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002757
2758 hci_unregister_dev(hdev);
2759
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02002760 if (intf == data->isoc)
2761 usb_driver_release_interface(&btusb_driver, data->intf);
2762 else if (data->isoc)
2763 usb_driver_release_interface(&btusb_driver, data->isoc);
2764
Marcel Holtmann803b5832014-09-16 08:00:29 +02002765 btusb_free_frags(data);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002766 hci_free_dev(hdev);
2767}
2768
Oliver Neukum7bee5492009-08-24 23:44:59 +02002769#ifdef CONFIG_PM
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002770static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
2771{
2772 struct btusb_data *data = usb_get_intfdata(intf);
2773
2774 BT_DBG("intf %p", intf);
2775
2776 if (data->suspend_count++)
2777 return 0;
2778
Oliver Neukum7bee5492009-08-24 23:44:59 +02002779 spin_lock_irq(&data->txlock);
Alan Stern5b1b0b82011-08-19 23:49:48 +02002780 if (!(PMSG_IS_AUTO(message) && data->tx_in_flight)) {
Oliver Neukum7bee5492009-08-24 23:44:59 +02002781 set_bit(BTUSB_SUSPENDING, &data->flags);
2782 spin_unlock_irq(&data->txlock);
2783 } else {
2784 spin_unlock_irq(&data->txlock);
2785 data->suspend_count--;
2786 return -EBUSY;
2787 }
2788
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002789 cancel_work_sync(&data->work);
2790
Oliver Neukum7bee5492009-08-24 23:44:59 +02002791 btusb_stop_traffic(data);
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002792 usb_kill_anchored_urbs(&data->tx_anchor);
2793
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002794 return 0;
2795}
2796
Oliver Neukum7bee5492009-08-24 23:44:59 +02002797static void play_deferred(struct btusb_data *data)
2798{
2799 struct urb *urb;
2800 int err;
2801
2802 while ((urb = usb_get_from_anchor(&data->deferred))) {
2803 err = usb_submit_urb(urb, GFP_ATOMIC);
2804 if (err < 0)
2805 break;
2806
2807 data->tx_in_flight++;
2808 }
2809 usb_scuttle_anchored_urbs(&data->deferred);
2810}
2811
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002812static int btusb_resume(struct usb_interface *intf)
2813{
2814 struct btusb_data *data = usb_get_intfdata(intf);
2815 struct hci_dev *hdev = data->hdev;
Oliver Neukum7bee5492009-08-24 23:44:59 +02002816 int err = 0;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002817
2818 BT_DBG("intf %p", intf);
2819
2820 if (--data->suspend_count)
2821 return 0;
2822
2823 if (!test_bit(HCI_RUNNING, &hdev->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +02002824 goto done;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002825
2826 if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
2827 err = btusb_submit_intr_urb(hdev, GFP_NOIO);
2828 if (err < 0) {
2829 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02002830 goto failed;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002831 }
2832 }
2833
2834 if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) {
Marcel Holtmann43c2e572009-02-04 17:41:38 +01002835 err = btusb_submit_bulk_urb(hdev, GFP_NOIO);
2836 if (err < 0) {
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002837 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02002838 goto failed;
2839 }
2840
2841 btusb_submit_bulk_urb(hdev, GFP_NOIO);
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002842 }
2843
2844 if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
2845 if (btusb_submit_isoc_urb(hdev, GFP_NOIO) < 0)
2846 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
2847 else
2848 btusb_submit_isoc_urb(hdev, GFP_NOIO);
2849 }
2850
Oliver Neukum7bee5492009-08-24 23:44:59 +02002851 spin_lock_irq(&data->txlock);
2852 play_deferred(data);
2853 clear_bit(BTUSB_SUSPENDING, &data->flags);
2854 spin_unlock_irq(&data->txlock);
2855 schedule_work(&data->work);
2856
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002857 return 0;
Oliver Neukum7bee5492009-08-24 23:44:59 +02002858
2859failed:
2860 usb_scuttle_anchored_urbs(&data->deferred);
2861done:
2862 spin_lock_irq(&data->txlock);
2863 clear_bit(BTUSB_SUSPENDING, &data->flags);
2864 spin_unlock_irq(&data->txlock);
2865
2866 return err;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002867}
Oliver Neukum7bee5492009-08-24 23:44:59 +02002868#endif
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002869
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002870static struct usb_driver btusb_driver = {
2871 .name = "btusb",
2872 .probe = btusb_probe,
2873 .disconnect = btusb_disconnect,
Oliver Neukum7bee5492009-08-24 23:44:59 +02002874#ifdef CONFIG_PM
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002875 .suspend = btusb_suspend,
2876 .resume = btusb_resume,
Oliver Neukum7bee5492009-08-24 23:44:59 +02002877#endif
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002878 .id_table = btusb_table,
Oliver Neukum7bee5492009-08-24 23:44:59 +02002879 .supports_autosuspend = 1,
Sarah Sharpe1f12eb2012-04-23 10:08:51 -07002880 .disable_hub_initiated_lpm = 1,
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002881};
2882
Greg Kroah-Hartman93f15082011-11-18 09:47:34 -08002883module_usb_driver(btusb_driver);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002884
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002885module_param(disable_scofix, bool, 0644);
2886MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
2887
2888module_param(force_scofix, bool, 0644);
2889MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
2890
2891module_param(reset, bool, 0644);
2892MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
2893
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002894MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
2895MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
2896MODULE_VERSION(VERSION);
2897MODULE_LICENSE("GPL");