blob: 9efdace85e153721af2ca134a2634005dd12fd0e [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
Oliver Neukum7bee5492009-08-24 23:44:59 +020031#define VERSION "0.6"
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 Holtmann5e23b922007-10-20 14:12:34 +020052
Marcel Holtmann54265202013-10-11 07:46:18 -070053static const struct usb_device_id btusb_table[] = {
Marcel Holtmann5e23b922007-10-20 14:12:34 +020054 /* Generic Bluetooth USB device */
55 { USB_DEVICE_INFO(0xe0, 0x01, 0x01) },
56
Henrik Rydberg1fa65352012-08-25 19:28:06 +020057 /* Apple-specific (Broadcom) devices */
58 { USB_VENDOR_AND_INTERFACE_INFO(0x05ac, 0xff, 0x01, 0x01) },
59
Cho, Yu-Chen178c0592013-06-04 21:40:26 +080060 /* MediaTek MT76x0E */
61 { USB_DEVICE(0x0e8d, 0x763f) },
62
Oliver Neukumc510eae2011-09-21 11:41:45 +020063 /* Broadcom SoftSailing reporting vendor specific */
Don Zickus2e8b50632012-03-28 16:41:11 -040064 { USB_DEVICE(0x0a5c, 0x21e1) },
Oliver Neukumc510eae2011-09-21 11:41:45 +020065
Nobuhiro Iwamatsu3cd01972010-08-20 16:24:07 +090066 /* Apple MacBookPro 7,1 */
67 { USB_DEVICE(0x05ac, 0x8213) },
68
Cyril Lacoux0a79f672010-07-14 10:29:27 +040069 /* Apple iMac11,1 */
70 { USB_DEVICE(0x05ac, 0x8215) },
71
Nobuhiro Iwamatsu9c047152010-08-20 16:24:06 +090072 /* Apple MacBookPro6,2 */
73 { USB_DEVICE(0x05ac, 0x8218) },
74
Edgar (gimli) Hucek3e3ede72010-11-04 08:04:33 +010075 /* Apple MacBookAir3,1, MacBookAir3,2 */
76 { USB_DEVICE(0x05ac, 0x821b) },
77
Pieter-Augustijn Van Malleghema63b7232011-09-07 02:28:10 -040078 /* Apple MacBookAir4,1 */
79 { USB_DEVICE(0x05ac, 0x821f) },
80
Marc-Antoine Perennou88d377b2011-03-24 14:51:21 -030081 /* Apple MacBookPro8,2 */
82 { USB_DEVICE(0x05ac, 0x821a) },
83
Jurgen Kramerf78b68262011-09-04 18:01:42 +020084 /* Apple MacMini5,1 */
85 { USB_DEVICE(0x05ac, 0x8281) },
86
Marcel Holtmanncfeb4142008-08-07 22:26:56 +020087 /* AVM BlueFRITZ! USB v2.0 */
88 { USB_DEVICE(0x057c, 0x3800) },
89
90 /* Bluetooth Ultraport Module from IBM */
91 { USB_DEVICE(0x04bf, 0x030a) },
92
93 /* ALPS Modules with non-standard id */
94 { USB_DEVICE(0x044e, 0x3001) },
95 { USB_DEVICE(0x044e, 0x3002) },
96
97 /* Ericsson with non-standard id */
98 { USB_DEVICE(0x0bdb, 0x1002) },
99
100 /* Canyon CN-BTU1 with HID interfaces */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100101 { USB_DEVICE(0x0c10, 0x0000) },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200102
Wen-chien Jesse Sungd13431c2011-11-08 14:30:22 +0800103 /* Broadcom BCM20702A0 */
Andy Shevchenko0b880062014-02-18 18:26:19 +0200104 { USB_DEVICE(0x0489, 0xe042) },
105 { USB_DEVICE(0x04ca, 0x2003) },
Jeff Cook1ee3ff62012-11-09 16:39:48 -0700106 { USB_DEVICE(0x0b05, 0x17b5) },
Raphael Kubo da Costa38a172b2013-09-02 14:57:51 +0300107 { USB_DEVICE(0x0b05, 0x17cb) },
Wen-chien Jesse Sungd13431c2011-11-08 14:30:22 +0800108 { USB_DEVICE(0x413c, 0x8197) },
Fabio Ka86c02e2014-11-18 00:46:28 -0200109 { USB_DEVICE(0x13d3, 0x3404),
110 .driver_info = BTUSB_BCM_PATCHRAM },
Wen-chien Jesse Sungd13431c2011-11-08 14:30:22 +0800111
Steven Harms98514032012-04-13 14:45:55 -0400112 /* Foxconn - Hon Hai */
Gustavo Padovanee664012012-08-15 01:38:11 -0300113 { USB_VENDOR_AND_INTERFACE_INFO(0x0489, 0xff, 0x01, 0x01) },
Steven Harms98514032012-04-13 14:45:55 -0400114
Andy Shevchenko0b880062014-02-18 18:26:19 +0200115 /* Broadcom devices with vendor specific id */
Petri Gynther10d4c672014-05-08 15:50:01 -0700116 { USB_VENDOR_AND_INTERFACE_INFO(0x0a5c, 0xff, 0x01, 0x01),
117 .driver_info = BTUSB_BCM_PATCHRAM },
Gustavo Padovan92c385f2012-08-06 15:36:49 -0300118
Marcel Holtmannc2aef6e2014-07-21 14:02:33 +0200119 /* ASUSTek Computer - Broadcom based */
120 { USB_VENDOR_AND_INTERFACE_INFO(0x0b05, 0xff, 0x01, 0x01) },
121
Ken O'Brien5bcecf32013-09-21 19:14:43 +0100122 /* Belkin F8065bf - Broadcom based */
123 { USB_VENDOR_AND_INTERFACE_INFO(0x050d, 0xff, 0x01, 0x01) },
124
Jurgen Kramer9113bfd2014-02-15 12:01:09 +0100125 /* IMC Networks - Broadcom based */
126 { USB_VENDOR_AND_INTERFACE_INFO(0x13d3, 0xff, 0x01, 0x01) },
127
Marcel Holtmann40df7832014-07-06 13:29:58 +0200128 /* Intel Bluetooth USB Bootloader (RAM module) */
Marcel Holtmannd92f2df2014-07-06 14:53:55 +0200129 { USB_DEVICE(0x8087, 0x0a5a),
130 .driver_info = BTUSB_INTEL_BOOT | BTUSB_BROKEN_ISOC },
Marcel Holtmann40df7832014-07-06 13:29:58 +0200131
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200132 { } /* Terminating entry */
133};
134
135MODULE_DEVICE_TABLE(usb, btusb_table);
136
Marcel Holtmann54265202013-10-11 07:46:18 -0700137static const struct usb_device_id blacklist_table[] = {
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200138 /* CSR BlueCore devices */
139 { USB_DEVICE(0x0a12, 0x0001), .driver_info = BTUSB_CSR },
140
141 /* Broadcom BCM2033 without firmware */
142 { USB_DEVICE(0x0a5c, 0x2033), .driver_info = BTUSB_IGNORE },
143
Bala Shanmugambe931122010-11-26 17:35:46 +0530144 /* Atheros 3011 with sflash firmware */
Andy Shevchenko0b880062014-02-18 18:26:19 +0200145 { USB_DEVICE(0x0489, 0xe027), .driver_info = BTUSB_IGNORE },
146 { USB_DEVICE(0x0489, 0xe03d), .driver_info = BTUSB_IGNORE },
147 { USB_DEVICE(0x0930, 0x0215), .driver_info = BTUSB_IGNORE },
Bala Shanmugambe931122010-11-26 17:35:46 +0530148 { USB_DEVICE(0x0cf3, 0x3002), .driver_info = BTUSB_IGNORE },
Marek Vasut6eda5412012-06-08 14:32:50 +0200149 { USB_DEVICE(0x0cf3, 0xe019), .driver_info = BTUSB_IGNORE },
Andy Ross2a7bccc2011-05-09 16:11:16 -0700150 { USB_DEVICE(0x13d3, 0x3304), .driver_info = BTUSB_IGNORE },
Bala Shanmugambe931122010-11-26 17:35:46 +0530151
Cho, Yu-Chen509e7862011-01-26 17:10:59 +0800152 /* Atheros AR9285 Malbec with sflash firmware */
153 { USB_DEVICE(0x03f0, 0x311d), .driver_info = BTUSB_IGNORE },
154
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530155 /* Atheros 3012 with sflash firmware */
Andy Shevchenko0b880062014-02-18 18:26:19 +0200156 { USB_DEVICE(0x0489, 0xe04d), .driver_info = BTUSB_ATH3012 },
157 { USB_DEVICE(0x0489, 0xe04e), .driver_info = BTUSB_ATH3012 },
158 { USB_DEVICE(0x0489, 0xe056), .driver_info = BTUSB_ATH3012 },
159 { USB_DEVICE(0x0489, 0xe057), .driver_info = BTUSB_ATH3012 },
160 { USB_DEVICE(0x0489, 0xe05f), .driver_info = BTUSB_ATH3012 },
Anantha Krishnan4b552bc2014-10-06 16:31:49 +0530161 { USB_DEVICE(0x0489, 0xe078), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200162 { USB_DEVICE(0x04c5, 0x1330), .driver_info = BTUSB_ATH3012 },
163 { USB_DEVICE(0x04ca, 0x3004), .driver_info = BTUSB_ATH3012 },
164 { USB_DEVICE(0x04ca, 0x3005), .driver_info = BTUSB_ATH3012 },
165 { USB_DEVICE(0x04ca, 0x3006), .driver_info = BTUSB_ATH3012 },
Mohammed Habibulla1fb4e092014-04-17 11:37:13 -0700166 { USB_DEVICE(0x04ca, 0x3007), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200167 { USB_DEVICE(0x04ca, 0x3008), .driver_info = BTUSB_ATH3012 },
168 { USB_DEVICE(0x04ca, 0x300b), .driver_info = BTUSB_ATH3012 },
169 { USB_DEVICE(0x0930, 0x0219), .driver_info = BTUSB_ATH3012 },
170 { USB_DEVICE(0x0930, 0x0220), .driver_info = BTUSB_ATH3012 },
Vincent Zwanenburg89d29752014-08-08 12:33:56 +0100171 { USB_DEVICE(0x0930, 0x0227), .driver_info = BTUSB_ATH3012 },
Andy Shevchenkoa735f9e2014-02-18 18:26:20 +0200172 { USB_DEVICE(0x0b05, 0x17d0), .driver_info = BTUSB_ATH3012 },
Ming Leid66629c2013-03-15 11:00:39 +0800173 { USB_DEVICE(0x0cf3, 0x0036), .driver_info = BTUSB_ATH3012 },
Steven.Li2d25f8b2011-07-01 14:02:36 +0800174 { USB_DEVICE(0x0cf3, 0x3004), .driver_info = BTUSB_ATH3012 },
Sunguk Lee94a32d102013-03-12 04:41:58 +0900175 { USB_DEVICE(0x0cf3, 0x3008), .driver_info = BTUSB_ATH3012 },
Cho, Yu-Chen07c0ea82012-03-14 22:01:21 +0200176 { USB_DEVICE(0x0cf3, 0x311d), .driver_info = BTUSB_ATH3012 },
Oliver Neukumb1312372014-01-16 15:37:11 +0100177 { USB_DEVICE(0x0cf3, 0x311e), .driver_info = BTUSB_ATH3012 },
Oliver Neukum1e56f1e2014-01-16 16:02:58 +0100178 { USB_DEVICE(0x0cf3, 0x311f), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200179 { USB_DEVICE(0x0cf3, 0x3121), .driver_info = BTUSB_ATH3012 },
Ming Leiebaf5792013-03-18 23:45:11 +0800180 { USB_DEVICE(0x0cf3, 0x817a), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200181 { USB_DEVICE(0x0cf3, 0xe003), .driver_info = BTUSB_ATH3012 },
AceLan Kaoac713112012-04-19 14:53:45 +0800182 { USB_DEVICE(0x0cf3, 0xe004), .driver_info = BTUSB_ATH3012 },
Peng Chen0a3658c2013-08-30 17:41:40 +0800183 { USB_DEVICE(0x0cf3, 0xe005), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200184 { USB_DEVICE(0x13d3, 0x3362), .driver_info = BTUSB_ATH3012 },
185 { USB_DEVICE(0x13d3, 0x3375), .driver_info = BTUSB_ATH3012 },
AceLan Kaoeed307e2012-12-11 11:41:20 +0800186 { USB_DEVICE(0x13d3, 0x3393), .driver_info = BTUSB_ATH3012 },
Sujith Manoharan5b77a1f2013-07-15 09:29:03 +0530187 { USB_DEVICE(0x13d3, 0x3402), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin3bb30a7c2014-11-25 20:19:52 +0300188 { USB_DEVICE(0x13d3, 0x3408), .driver_info = BTUSB_ATH3012 },
Anantha Krishnanfa2f1392014-07-08 19:25:08 +0530189 { USB_DEVICE(0x13d3, 0x3432), .driver_info = BTUSB_ATH3012 },
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530190
Cho, Yu-Chene9036e32011-02-15 10:20:07 +0800191 /* Atheros AR5BBU12 with sflash firmware */
192 { USB_DEVICE(0x0489, 0xe02c), .driver_info = BTUSB_IGNORE },
193
Michael Gruetzner85d59722012-05-02 22:33:40 +0200194 /* Atheros AR5BBU12 with sflash firmware */
Yevgeniy Melnichukbc21fde2012-08-07 19:48:10 +0530195 { USB_DEVICE(0x0489, 0xe036), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200196 { USB_DEVICE(0x0489, 0xe03c), .driver_info = BTUSB_ATH3012 },
Michael Gruetzner85d59722012-05-02 22:33:40 +0200197
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200198 /* Broadcom BCM2035 */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100199 { USB_DEVICE(0x0a5c, 0x2009), .driver_info = BTUSB_BCM92035 },
Andy Shevchenko0b880062014-02-18 18:26:19 +0200200 { USB_DEVICE(0x0a5c, 0x200a), .driver_info = BTUSB_WRONG_SCO_MTU },
201 { USB_DEVICE(0x0a5c, 0x2035), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200202
203 /* Broadcom BCM2045 */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100204 { USB_DEVICE(0x0a5c, 0x2039), .driver_info = BTUSB_WRONG_SCO_MTU },
205 { USB_DEVICE(0x0a5c, 0x2101), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmannbdbef3d2008-09-23 00:16:35 +0200206
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200207 /* IBM/Lenovo ThinkPad with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100208 { USB_DEVICE(0x0a5c, 0x201e), .driver_info = BTUSB_WRONG_SCO_MTU },
209 { USB_DEVICE(0x0a5c, 0x2110), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200210
211 /* HP laptop with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100212 { USB_DEVICE(0x03f0, 0x171d), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200213
214 /* Dell laptop with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100215 { USB_DEVICE(0x413c, 0x8126), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200216
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100217 /* Dell Wireless 370 and 410 devices */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100218 { USB_DEVICE(0x413c, 0x8152), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100219 { USB_DEVICE(0x413c, 0x8156), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200220
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100221 /* Belkin F8T012 and F8T013 devices */
222 { USB_DEVICE(0x050d, 0x0012), .driver_info = BTUSB_WRONG_SCO_MTU },
223 { USB_DEVICE(0x050d, 0x0013), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200224
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100225 /* Asus WL-BTD202 device */
226 { USB_DEVICE(0x0b05, 0x1715), .driver_info = BTUSB_WRONG_SCO_MTU },
227
228 /* Kensington Bluetooth USB adapter */
229 { USB_DEVICE(0x047d, 0x105e), .driver_info = BTUSB_WRONG_SCO_MTU },
230
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200231 /* RTX Telecom based adapters with buggy SCO support */
232 { USB_DEVICE(0x0400, 0x0807), .driver_info = BTUSB_BROKEN_ISOC },
233 { USB_DEVICE(0x0400, 0x080a), .driver_info = BTUSB_BROKEN_ISOC },
234
235 /* CONWISE Technology based adapters with buggy SCO support */
236 { USB_DEVICE(0x0e5e, 0x6622), .driver_info = BTUSB_BROKEN_ISOC },
237
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200238 /* Digianswer devices */
239 { USB_DEVICE(0x08fd, 0x0001), .driver_info = BTUSB_DIGIANSWER },
240 { USB_DEVICE(0x08fd, 0x0002), .driver_info = BTUSB_IGNORE },
241
242 /* CSR BlueCore Bluetooth Sniffer */
Marcel Holtmann4f64fa82014-07-07 00:12:04 +0200243 { USB_DEVICE(0x0a12, 0x0002),
244 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200245
246 /* Frontline ComProbe Bluetooth Sniffer */
Marcel Holtmann4f64fa82014-07-07 00:12:04 +0200247 { USB_DEVICE(0x16d3, 0x0002),
248 .driver_info = BTUSB_SNIFFER | BTUSB_BROKEN_ISOC },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200249
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -0700250 /* Intel Bluetooth device */
251 { USB_DEVICE(0x8087, 0x07dc), .driver_info = BTUSB_INTEL },
Tedd Ho-Jeong Anef4e5e42013-11-12 13:10:58 -0800252 { USB_DEVICE(0x8087, 0x0a2a), .driver_info = BTUSB_INTEL },
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -0700253
Amitkumar Karwarae8df492014-07-18 14:47:06 -0700254 /* Marvell device */
255 { USB_DEVICE(0x1286, 0x2044), .driver_info = BTUSB_MARVELL },
256 { USB_DEVICE(0x1286, 0x2046), .driver_info = BTUSB_MARVELL },
257
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200258 { } /* Terminating entry */
259};
260
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200261#define BTUSB_MAX_ISOC_FRAMES 10
262
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200263#define BTUSB_INTR_RUNNING 0
264#define BTUSB_BULK_RUNNING 1
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200265#define BTUSB_ISOC_RUNNING 2
Oliver Neukum7bee5492009-08-24 23:44:59 +0200266#define BTUSB_SUSPENDING 3
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -0300267#define BTUSB_DID_ISO_RESUME 4
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200268
269struct btusb_data {
270 struct hci_dev *hdev;
271 struct usb_device *udev;
Marcel Holtmann5fbcd262008-09-23 00:16:36 +0200272 struct usb_interface *intf;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200273 struct usb_interface *isoc;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200274
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200275 unsigned long flags;
276
277 struct work_struct work;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200278 struct work_struct waker;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200279
Marcel Holtmann803b5832014-09-16 08:00:29 +0200280 struct usb_anchor deferred;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200281 struct usb_anchor tx_anchor;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200282 int tx_in_flight;
283 spinlock_t txlock;
284
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200285 struct usb_anchor intr_anchor;
286 struct usb_anchor bulk_anchor;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200287 struct usb_anchor isoc_anchor;
Marcel Holtmann803b5832014-09-16 08:00:29 +0200288 spinlock_t rxlock;
289
290 struct sk_buff *evt_skb;
291 struct sk_buff *acl_skb;
292 struct sk_buff *sco_skb;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200293
294 struct usb_endpoint_descriptor *intr_ep;
295 struct usb_endpoint_descriptor *bulk_tx_ep;
296 struct usb_endpoint_descriptor *bulk_rx_ep;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200297 struct usb_endpoint_descriptor *isoc_tx_ep;
298 struct usb_endpoint_descriptor *isoc_rx_ep;
299
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100300 __u8 cmdreq_type;
301
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100302 unsigned int sco_num;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200303 int isoc_altsetting;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +0100304 int suspend_count;
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +0100305
306 int (*recv_bulk)(struct btusb_data *data, void *buffer, int count);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200307};
308
Marcel Holtmann803b5832014-09-16 08:00:29 +0200309static inline void btusb_free_frags(struct btusb_data *data)
310{
311 unsigned long flags;
312
313 spin_lock_irqsave(&data->rxlock, flags);
314
315 kfree_skb(data->evt_skb);
316 data->evt_skb = NULL;
317
318 kfree_skb(data->acl_skb);
319 data->acl_skb = NULL;
320
321 kfree_skb(data->sco_skb);
322 data->sco_skb = NULL;
323
324 spin_unlock_irqrestore(&data->rxlock, flags);
325}
326
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200327static int btusb_recv_intr(struct btusb_data *data, void *buffer, int count)
328{
Marcel Holtmann803b5832014-09-16 08:00:29 +0200329 struct sk_buff *skb;
330 int err = 0;
331
332 spin_lock(&data->rxlock);
333 skb = data->evt_skb;
334
335 while (count) {
336 int len;
337
338 if (!skb) {
339 skb = bt_skb_alloc(HCI_MAX_EVENT_SIZE, GFP_ATOMIC);
340 if (!skb) {
341 err = -ENOMEM;
342 break;
343 }
344
345 bt_cb(skb)->pkt_type = HCI_EVENT_PKT;
346 bt_cb(skb)->expect = HCI_EVENT_HDR_SIZE;
347 }
348
349 len = min_t(uint, bt_cb(skb)->expect, count);
350 memcpy(skb_put(skb, len), buffer, len);
351
352 count -= len;
353 buffer += len;
354 bt_cb(skb)->expect -= len;
355
356 if (skb->len == HCI_EVENT_HDR_SIZE) {
357 /* Complete event header */
358 bt_cb(skb)->expect = hci_event_hdr(skb)->plen;
359
360 if (skb_tailroom(skb) < bt_cb(skb)->expect) {
361 kfree_skb(skb);
362 skb = NULL;
363
364 err = -EILSEQ;
365 break;
366 }
367 }
368
369 if (bt_cb(skb)->expect == 0) {
370 /* Complete frame */
371 hci_recv_frame(data->hdev, skb);
372 skb = NULL;
373 }
374 }
375
376 data->evt_skb = skb;
377 spin_unlock(&data->rxlock);
378
379 return err;
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200380}
381
382static int btusb_recv_bulk(struct btusb_data *data, void *buffer, int count)
383{
Marcel Holtmann803b5832014-09-16 08:00:29 +0200384 struct sk_buff *skb;
385 int err = 0;
386
387 spin_lock(&data->rxlock);
388 skb = data->acl_skb;
389
390 while (count) {
391 int len;
392
393 if (!skb) {
394 skb = bt_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC);
395 if (!skb) {
396 err = -ENOMEM;
397 break;
398 }
399
400 bt_cb(skb)->pkt_type = HCI_ACLDATA_PKT;
401 bt_cb(skb)->expect = HCI_ACL_HDR_SIZE;
402 }
403
404 len = min_t(uint, bt_cb(skb)->expect, count);
405 memcpy(skb_put(skb, len), buffer, len);
406
407 count -= len;
408 buffer += len;
409 bt_cb(skb)->expect -= len;
410
411 if (skb->len == HCI_ACL_HDR_SIZE) {
412 __le16 dlen = hci_acl_hdr(skb)->dlen;
413
414 /* Complete ACL header */
415 bt_cb(skb)->expect = __le16_to_cpu(dlen);
416
417 if (skb_tailroom(skb) < bt_cb(skb)->expect) {
418 kfree_skb(skb);
419 skb = NULL;
420
421 err = -EILSEQ;
422 break;
423 }
424 }
425
426 if (bt_cb(skb)->expect == 0) {
427 /* Complete frame */
428 hci_recv_frame(data->hdev, skb);
429 skb = NULL;
430 }
431 }
432
433 data->acl_skb = skb;
434 spin_unlock(&data->rxlock);
435
436 return err;
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200437}
438
439static int btusb_recv_isoc(struct btusb_data *data, void *buffer, int count)
440{
Marcel Holtmann803b5832014-09-16 08:00:29 +0200441 struct sk_buff *skb;
442 int err = 0;
443
444 spin_lock(&data->rxlock);
445 skb = data->sco_skb;
446
447 while (count) {
448 int len;
449
450 if (!skb) {
451 skb = bt_skb_alloc(HCI_MAX_SCO_SIZE, GFP_ATOMIC);
452 if (!skb) {
453 err = -ENOMEM;
454 break;
455 }
456
457 bt_cb(skb)->pkt_type = HCI_SCODATA_PKT;
458 bt_cb(skb)->expect = HCI_SCO_HDR_SIZE;
459 }
460
461 len = min_t(uint, bt_cb(skb)->expect, count);
462 memcpy(skb_put(skb, len), buffer, len);
463
464 count -= len;
465 buffer += len;
466 bt_cb(skb)->expect -= len;
467
468 if (skb->len == HCI_SCO_HDR_SIZE) {
469 /* Complete SCO header */
470 bt_cb(skb)->expect = hci_sco_hdr(skb)->dlen;
471
472 if (skb_tailroom(skb) < bt_cb(skb)->expect) {
473 kfree_skb(skb);
474 skb = NULL;
475
476 err = -EILSEQ;
477 break;
478 }
479 }
480
481 if (bt_cb(skb)->expect == 0) {
482 /* Complete frame */
483 hci_recv_frame(data->hdev, skb);
484 skb = NULL;
485 }
486 }
487
488 data->sco_skb = skb;
489 spin_unlock(&data->rxlock);
490
491 return err;
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200492}
493
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200494static void btusb_intr_complete(struct urb *urb)
495{
496 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +0100497 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200498 int err;
499
Marcel Holtmann89e75332014-09-16 04:44:50 +0200500 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
501 urb->actual_length);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200502
503 if (!test_bit(HCI_RUNNING, &hdev->flags))
504 return;
505
506 if (urb->status == 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200507 hdev->stat.byte_rx += urb->actual_length;
508
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200509 if (btusb_recv_intr(data, urb->transfer_buffer,
510 urb->actual_length) < 0) {
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200511 BT_ERR("%s corrupted event packet", hdev->name);
512 hdev->stat.err_rx++;
513 }
Champion Chen85560c42014-09-06 14:06:08 -0500514 } else if (urb->status == -ENOENT) {
515 /* Avoid suspend failed when usb_kill_urb */
516 return;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200517 }
518
519 if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
520 return;
521
Oliver Neukum7bee5492009-08-24 23:44:59 +0200522 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200523 usb_anchor_urb(urb, &data->intr_anchor);
524
525 err = usb_submit_urb(urb, GFP_ATOMIC);
526 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200527 /* -EPERM: urb is being killed;
528 * -ENODEV: device got disconnected */
529 if (err != -EPERM && err != -ENODEV)
Stefan Seyfried61faddf2010-11-30 21:49:08 +0100530 BT_ERR("%s urb %p failed to resubmit (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200531 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200532 usb_unanchor_urb(urb);
533 }
534}
535
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100536static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200537{
David Herrmann155961e2012-02-09 21:58:32 +0100538 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200539 struct urb *urb;
540 unsigned char *buf;
541 unsigned int pipe;
542 int err, size;
543
544 BT_DBG("%s", hdev->name);
545
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200546 if (!data->intr_ep)
547 return -ENODEV;
548
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100549 urb = usb_alloc_urb(0, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200550 if (!urb)
551 return -ENOMEM;
552
553 size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
554
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100555 buf = kmalloc(size, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200556 if (!buf) {
557 usb_free_urb(urb);
558 return -ENOMEM;
559 }
560
561 pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
562
563 usb_fill_int_urb(urb, data->udev, pipe, buf, size,
Marcel Holtmann89e75332014-09-16 04:44:50 +0200564 btusb_intr_complete, hdev, data->intr_ep->bInterval);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200565
566 urb->transfer_flags |= URB_FREE_BUFFER;
567
568 usb_anchor_urb(urb, &data->intr_anchor);
569
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100570 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200571 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +0200572 if (err != -EPERM && err != -ENODEV)
573 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200574 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200575 usb_unanchor_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200576 }
577
578 usb_free_urb(urb);
579
580 return err;
581}
582
583static void btusb_bulk_complete(struct urb *urb)
584{
585 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +0100586 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200587 int err;
588
Marcel Holtmann89e75332014-09-16 04:44:50 +0200589 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
590 urb->actual_length);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200591
592 if (!test_bit(HCI_RUNNING, &hdev->flags))
593 return;
594
595 if (urb->status == 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200596 hdev->stat.byte_rx += urb->actual_length;
597
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +0100598 if (data->recv_bulk(data, urb->transfer_buffer,
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200599 urb->actual_length) < 0) {
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200600 BT_ERR("%s corrupted ACL packet", hdev->name);
601 hdev->stat.err_rx++;
602 }
Champion Chen85560c42014-09-06 14:06:08 -0500603 } else if (urb->status == -ENOENT) {
604 /* Avoid suspend failed when usb_kill_urb */
605 return;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200606 }
607
608 if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
609 return;
610
611 usb_anchor_urb(urb, &data->bulk_anchor);
Oliver Neukum652fd782009-12-16 19:23:43 +0100612 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200613
614 err = usb_submit_urb(urb, GFP_ATOMIC);
615 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200616 /* -EPERM: urb is being killed;
617 * -ENODEV: device got disconnected */
618 if (err != -EPERM && err != -ENODEV)
Stefan Seyfried61faddf2010-11-30 21:49:08 +0100619 BT_ERR("%s urb %p failed to resubmit (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200620 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200621 usb_unanchor_urb(urb);
622 }
623}
624
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100625static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200626{
David Herrmann155961e2012-02-09 21:58:32 +0100627 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200628 struct urb *urb;
629 unsigned char *buf;
630 unsigned int pipe;
Vikram Kandukuri290ba202009-07-02 14:31:59 +0530631 int err, size = HCI_MAX_FRAME_SIZE;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200632
633 BT_DBG("%s", hdev->name);
634
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200635 if (!data->bulk_rx_ep)
636 return -ENODEV;
637
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100638 urb = usb_alloc_urb(0, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200639 if (!urb)
640 return -ENOMEM;
641
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100642 buf = kmalloc(size, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200643 if (!buf) {
644 usb_free_urb(urb);
645 return -ENOMEM;
646 }
647
648 pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
649
Marcel Holtmann89e75332014-09-16 04:44:50 +0200650 usb_fill_bulk_urb(urb, data->udev, pipe, buf, size,
651 btusb_bulk_complete, hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200652
653 urb->transfer_flags |= URB_FREE_BUFFER;
654
Oliver Neukum7bee5492009-08-24 23:44:59 +0200655 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200656 usb_anchor_urb(urb, &data->bulk_anchor);
657
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100658 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200659 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +0200660 if (err != -EPERM && err != -ENODEV)
661 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200662 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200663 usb_unanchor_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200664 }
665
666 usb_free_urb(urb);
667
668 return err;
669}
670
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200671static void btusb_isoc_complete(struct urb *urb)
672{
673 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +0100674 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200675 int i, err;
676
Marcel Holtmann89e75332014-09-16 04:44:50 +0200677 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
678 urb->actual_length);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200679
680 if (!test_bit(HCI_RUNNING, &hdev->flags))
681 return;
682
683 if (urb->status == 0) {
684 for (i = 0; i < urb->number_of_packets; i++) {
685 unsigned int offset = urb->iso_frame_desc[i].offset;
686 unsigned int length = urb->iso_frame_desc[i].actual_length;
687
688 if (urb->iso_frame_desc[i].status)
689 continue;
690
691 hdev->stat.byte_rx += length;
692
Marcel Holtmann1ffa4ad2014-09-16 05:33:33 +0200693 if (btusb_recv_isoc(data, urb->transfer_buffer + offset,
694 length) < 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200695 BT_ERR("%s corrupted SCO packet", hdev->name);
696 hdev->stat.err_rx++;
697 }
698 }
Champion Chen85560c42014-09-06 14:06:08 -0500699 } else if (urb->status == -ENOENT) {
700 /* Avoid suspend failed when usb_kill_urb */
701 return;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200702 }
703
704 if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags))
705 return;
706
707 usb_anchor_urb(urb, &data->isoc_anchor);
708
709 err = usb_submit_urb(urb, GFP_ATOMIC);
710 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200711 /* -EPERM: urb is being killed;
712 * -ENODEV: device got disconnected */
713 if (err != -EPERM && err != -ENODEV)
Stefan Seyfried61faddf2010-11-30 21:49:08 +0100714 BT_ERR("%s urb %p failed to resubmit (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200715 hdev->name, urb, -err);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200716 usb_unanchor_urb(urb);
717 }
718}
719
Jesper Juhl42b16b32011-01-17 00:09:38 +0100720static inline void __fill_isoc_descriptor(struct urb *urb, int len, int mtu)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200721{
722 int i, offset = 0;
723
724 BT_DBG("len %d mtu %d", len, mtu);
725
726 for (i = 0; i < BTUSB_MAX_ISOC_FRAMES && len >= mtu;
727 i++, offset += mtu, len -= mtu) {
728 urb->iso_frame_desc[i].offset = offset;
729 urb->iso_frame_desc[i].length = mtu;
730 }
731
732 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
733 urb->iso_frame_desc[i].offset = offset;
734 urb->iso_frame_desc[i].length = len;
735 i++;
736 }
737
738 urb->number_of_packets = i;
739}
740
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100741static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200742{
David Herrmann155961e2012-02-09 21:58:32 +0100743 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200744 struct urb *urb;
745 unsigned char *buf;
746 unsigned int pipe;
747 int err, size;
748
749 BT_DBG("%s", hdev->name);
750
751 if (!data->isoc_rx_ep)
752 return -ENODEV;
753
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100754 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200755 if (!urb)
756 return -ENOMEM;
757
758 size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) *
759 BTUSB_MAX_ISOC_FRAMES;
760
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100761 buf = kmalloc(size, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200762 if (!buf) {
763 usb_free_urb(urb);
764 return -ENOMEM;
765 }
766
767 pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress);
768
Bing Zhaofa0fb932011-12-20 18:19:00 -0800769 usb_fill_int_urb(urb, data->udev, pipe, buf, size, btusb_isoc_complete,
Marcel Holtmann89e75332014-09-16 04:44:50 +0200770 hdev, data->isoc_rx_ep->bInterval);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200771
Marcel Holtmann89e75332014-09-16 04:44:50 +0200772 urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200773
774 __fill_isoc_descriptor(urb, size,
Marcel Holtmann89e75332014-09-16 04:44:50 +0200775 le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize));
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200776
777 usb_anchor_urb(urb, &data->isoc_anchor);
778
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100779 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200780 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +0200781 if (err != -EPERM && err != -ENODEV)
782 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +0200783 hdev->name, urb, -err);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200784 usb_unanchor_urb(urb);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200785 }
786
787 usb_free_urb(urb);
788
789 return err;
790}
791
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200792static void btusb_tx_complete(struct urb *urb)
793{
794 struct sk_buff *skb = urb->context;
Marcel Holtmann89e75332014-09-16 04:44:50 +0200795 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
David Herrmann155961e2012-02-09 21:58:32 +0100796 struct btusb_data *data = hci_get_drvdata(hdev);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200797
Marcel Holtmann89e75332014-09-16 04:44:50 +0200798 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
799 urb->actual_length);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200800
801 if (!test_bit(HCI_RUNNING, &hdev->flags))
802 goto done;
803
804 if (!urb->status)
805 hdev->stat.byte_tx += urb->transfer_buffer_length;
806 else
807 hdev->stat.err_tx++;
808
809done:
810 spin_lock(&data->txlock);
811 data->tx_in_flight--;
812 spin_unlock(&data->txlock);
813
814 kfree(urb->setup_packet);
815
816 kfree_skb(skb);
817}
818
819static void btusb_isoc_tx_complete(struct urb *urb)
820{
821 struct sk_buff *skb = urb->context;
Marcel Holtmann89e75332014-09-16 04:44:50 +0200822 struct hci_dev *hdev = (struct hci_dev *)skb->dev;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200823
Marcel Holtmann89e75332014-09-16 04:44:50 +0200824 BT_DBG("%s urb %p status %d count %d", hdev->name, urb, urb->status,
825 urb->actual_length);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200826
827 if (!test_bit(HCI_RUNNING, &hdev->flags))
828 goto done;
829
830 if (!urb->status)
831 hdev->stat.byte_tx += urb->transfer_buffer_length;
832 else
833 hdev->stat.err_tx++;
834
835done:
836 kfree(urb->setup_packet);
837
838 kfree_skb(skb);
839}
840
841static int btusb_open(struct hci_dev *hdev)
842{
David Herrmann155961e2012-02-09 21:58:32 +0100843 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200844 int err;
845
846 BT_DBG("%s", hdev->name);
847
Oliver Neukum7bee5492009-08-24 23:44:59 +0200848 err = usb_autopm_get_interface(data->intf);
849 if (err < 0)
850 return err;
851
852 data->intf->needs_remote_wakeup = 1;
853
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200854 if (test_and_set_bit(HCI_RUNNING, &hdev->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +0200855 goto done;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200856
857 if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +0200858 goto done;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200859
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100860 err = btusb_submit_intr_urb(hdev, GFP_KERNEL);
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100861 if (err < 0)
862 goto failed;
863
864 err = btusb_submit_bulk_urb(hdev, GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200865 if (err < 0) {
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100866 usb_kill_anchored_urbs(&data->intr_anchor);
867 goto failed;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200868 }
869
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100870 set_bit(BTUSB_BULK_RUNNING, &data->flags);
871 btusb_submit_bulk_urb(hdev, GFP_KERNEL);
872
Oliver Neukum7bee5492009-08-24 23:44:59 +0200873done:
874 usb_autopm_put_interface(data->intf);
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100875 return 0;
876
877failed:
878 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
879 clear_bit(HCI_RUNNING, &hdev->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200880 usb_autopm_put_interface(data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200881 return err;
882}
883
Oliver Neukum7bee5492009-08-24 23:44:59 +0200884static void btusb_stop_traffic(struct btusb_data *data)
885{
886 usb_kill_anchored_urbs(&data->intr_anchor);
887 usb_kill_anchored_urbs(&data->bulk_anchor);
888 usb_kill_anchored_urbs(&data->isoc_anchor);
889}
890
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200891static int btusb_close(struct hci_dev *hdev)
892{
David Herrmann155961e2012-02-09 21:58:32 +0100893 struct btusb_data *data = hci_get_drvdata(hdev);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200894 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200895
896 BT_DBG("%s", hdev->name);
897
898 if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
899 return 0;
900
Marcel Holtmanne8c3c3d2008-09-23 00:16:36 +0200901 cancel_work_sync(&data->work);
Linus Torvalds404291a2009-11-11 13:32:29 -0800902 cancel_work_sync(&data->waker);
Marcel Holtmanne8c3c3d2008-09-23 00:16:36 +0200903
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200904 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200905 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200906 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200907
908 btusb_stop_traffic(data);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200909 btusb_free_frags(data);
910
Oliver Neukum7bee5492009-08-24 23:44:59 +0200911 err = usb_autopm_get_interface(data->intf);
912 if (err < 0)
Oliver Neukum7b8e2c12009-11-13 14:26:23 +0100913 goto failed;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200914
915 data->intf->needs_remote_wakeup = 0;
916 usb_autopm_put_interface(data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200917
Oliver Neukum7b8e2c12009-11-13 14:26:23 +0100918failed:
919 usb_scuttle_anchored_urbs(&data->deferred);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200920 return 0;
921}
922
923static int btusb_flush(struct hci_dev *hdev)
924{
David Herrmann155961e2012-02-09 21:58:32 +0100925 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200926
927 BT_DBG("%s", hdev->name);
928
929 usb_kill_anchored_urbs(&data->tx_anchor);
Marcel Holtmann803b5832014-09-16 08:00:29 +0200930 btusb_free_frags(data);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200931
932 return 0;
933}
934
Marcel Holtmann047b2ec2014-09-14 09:11:06 +0200935static struct urb *alloc_ctrl_urb(struct hci_dev *hdev, struct sk_buff *skb)
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200936{
David Herrmann155961e2012-02-09 21:58:32 +0100937 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200938 struct usb_ctrlrequest *dr;
939 struct urb *urb;
940 unsigned int pipe;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200941
Marcel Holtmann047b2ec2014-09-14 09:11:06 +0200942 urb = usb_alloc_urb(0, GFP_KERNEL);
943 if (!urb)
944 return ERR_PTR(-ENOMEM);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200945
Marcel Holtmann047b2ec2014-09-14 09:11:06 +0200946 dr = kmalloc(sizeof(*dr), GFP_KERNEL);
947 if (!dr) {
948 usb_free_urb(urb);
949 return ERR_PTR(-ENOMEM);
950 }
951
952 dr->bRequestType = data->cmdreq_type;
953 dr->bRequest = 0;
954 dr->wIndex = 0;
955 dr->wValue = 0;
956 dr->wLength = __cpu_to_le16(skb->len);
957
958 pipe = usb_sndctrlpipe(data->udev, 0x00);
959
Marcel Holtmann89e75332014-09-16 04:44:50 +0200960 usb_fill_control_urb(urb, data->udev, pipe, (void *)dr,
Marcel Holtmann047b2ec2014-09-14 09:11:06 +0200961 skb->data, skb->len, btusb_tx_complete, skb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200962
Marcel Holtmann89e75332014-09-16 04:44:50 +0200963 skb->dev = (void *)hdev;
Marcel Holtmann7bd8f092013-10-11 06:19:18 -0700964
Marcel Holtmann047b2ec2014-09-14 09:11:06 +0200965 return urb;
966}
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200967
Marcel Holtmann047b2ec2014-09-14 09:11:06 +0200968static struct urb *alloc_bulk_urb(struct hci_dev *hdev, struct sk_buff *skb)
969{
970 struct btusb_data *data = hci_get_drvdata(hdev);
971 struct urb *urb;
972 unsigned int pipe;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200973
Marcel Holtmann047b2ec2014-09-14 09:11:06 +0200974 if (!data->bulk_tx_ep)
975 return ERR_PTR(-ENODEV);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200976
Marcel Holtmann047b2ec2014-09-14 09:11:06 +0200977 urb = usb_alloc_urb(0, GFP_KERNEL);
978 if (!urb)
979 return ERR_PTR(-ENOMEM);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200980
Marcel Holtmann047b2ec2014-09-14 09:11:06 +0200981 pipe = usb_sndbulkpipe(data->udev, data->bulk_tx_ep->bEndpointAddress);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200982
Marcel Holtmann047b2ec2014-09-14 09:11:06 +0200983 usb_fill_bulk_urb(urb, data->udev, pipe,
984 skb->data, skb->len, btusb_tx_complete, skb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200985
Marcel Holtmann89e75332014-09-16 04:44:50 +0200986 skb->dev = (void *)hdev;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200987
Marcel Holtmann047b2ec2014-09-14 09:11:06 +0200988 return urb;
989}
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200990
Marcel Holtmann047b2ec2014-09-14 09:11:06 +0200991static struct urb *alloc_isoc_urb(struct hci_dev *hdev, struct sk_buff *skb)
992{
993 struct btusb_data *data = hci_get_drvdata(hdev);
994 struct urb *urb;
995 unsigned int pipe;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200996
Marcel Holtmann047b2ec2014-09-14 09:11:06 +0200997 if (!data->isoc_tx_ep)
998 return ERR_PTR(-ENODEV);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200999
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001000 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_KERNEL);
1001 if (!urb)
1002 return ERR_PTR(-ENOMEM);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001003
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001004 pipe = usb_sndisocpipe(data->udev, data->isoc_tx_ep->bEndpointAddress);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001005
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001006 usb_fill_int_urb(urb, data->udev, pipe,
1007 skb->data, skb->len, btusb_isoc_tx_complete,
1008 skb, data->isoc_tx_ep->bInterval);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001009
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001010 urb->transfer_flags = URB_ISO_ASAP;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001011
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001012 __fill_isoc_descriptor(urb, skb->len,
1013 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001014
Marcel Holtmann89e75332014-09-16 04:44:50 +02001015 skb->dev = (void *)hdev;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001016
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001017 return urb;
1018}
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001019
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001020static int submit_tx_urb(struct hci_dev *hdev, struct urb *urb)
1021{
1022 struct btusb_data *data = hci_get_drvdata(hdev);
1023 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001024
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001025 usb_anchor_urb(urb, &data->tx_anchor);
1026
Johan Hedberge9753ef2014-09-14 08:49:34 +03001027 err = usb_submit_urb(urb, GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001028 if (err < 0) {
Paul Bolle5a9b80e2011-10-09 12:12:16 +02001029 if (err != -EPERM && err != -ENODEV)
1030 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann89e75332014-09-16 04:44:50 +02001031 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001032 kfree(urb->setup_packet);
1033 usb_unanchor_urb(urb);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001034 } else {
1035 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001036 }
1037
Cong Wang54a8a792011-11-22 09:32:57 +08001038 usb_free_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001039 return err;
1040}
1041
Marcel Holtmann047b2ec2014-09-14 09:11:06 +02001042static int submit_or_queue_tx_urb(struct hci_dev *hdev, struct urb *urb)
1043{
1044 struct btusb_data *data = hci_get_drvdata(hdev);
1045 unsigned long flags;
1046 bool suspending;
1047
1048 spin_lock_irqsave(&data->txlock, flags);
1049 suspending = test_bit(BTUSB_SUSPENDING, &data->flags);
1050 if (!suspending)
1051 data->tx_in_flight++;
1052 spin_unlock_irqrestore(&data->txlock, flags);
1053
1054 if (!suspending)
1055 return submit_tx_urb(hdev, urb);
1056
1057 usb_anchor_urb(urb, &data->deferred);
1058 schedule_work(&data->waker);
1059
1060 usb_free_urb(urb);
1061 return 0;
1062}
1063
1064static int btusb_send_frame(struct hci_dev *hdev, struct sk_buff *skb)
1065{
1066 struct urb *urb;
1067
1068 BT_DBG("%s", hdev->name);
1069
1070 if (!test_bit(HCI_RUNNING, &hdev->flags))
1071 return -EBUSY;
1072
1073 switch (bt_cb(skb)->pkt_type) {
1074 case HCI_COMMAND_PKT:
1075 urb = alloc_ctrl_urb(hdev, skb);
1076 if (IS_ERR(urb))
1077 return PTR_ERR(urb);
1078
1079 hdev->stat.cmd_tx++;
1080 return submit_or_queue_tx_urb(hdev, urb);
1081
1082 case HCI_ACLDATA_PKT:
1083 urb = alloc_bulk_urb(hdev, skb);
1084 if (IS_ERR(urb))
1085 return PTR_ERR(urb);
1086
1087 hdev->stat.acl_tx++;
1088 return submit_or_queue_tx_urb(hdev, urb);
1089
1090 case HCI_SCODATA_PKT:
1091 if (hci_conn_num(hdev, SCO_LINK) < 1)
1092 return -ENODEV;
1093
1094 urb = alloc_isoc_urb(hdev, skb);
1095 if (IS_ERR(urb))
1096 return PTR_ERR(urb);
1097
1098 hdev->stat.sco_tx++;
1099 return submit_tx_urb(hdev, urb);
1100 }
1101
1102 return -EILSEQ;
1103}
1104
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001105static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
1106{
David Herrmann155961e2012-02-09 21:58:32 +01001107 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001108
1109 BT_DBG("%s evt %d", hdev->name, evt);
1110
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001111 if (hci_conn_num(hdev, SCO_LINK) != data->sco_num) {
1112 data->sco_num = hci_conn_num(hdev, SCO_LINK);
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001113 schedule_work(&data->work);
Marcel Holtmanna780efa2008-11-30 12:17:12 +01001114 }
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001115}
1116
Jesper Juhl42b16b32011-01-17 00:09:38 +01001117static inline int __set_isoc_interface(struct hci_dev *hdev, int altsetting)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001118{
David Herrmann155961e2012-02-09 21:58:32 +01001119 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001120 struct usb_interface *intf = data->isoc;
1121 struct usb_endpoint_descriptor *ep_desc;
1122 int i, err;
1123
1124 if (!data->isoc)
1125 return -ENODEV;
1126
1127 err = usb_set_interface(data->udev, 1, altsetting);
1128 if (err < 0) {
1129 BT_ERR("%s setting interface failed (%d)", hdev->name, -err);
1130 return err;
1131 }
1132
1133 data->isoc_altsetting = altsetting;
1134
1135 data->isoc_tx_ep = NULL;
1136 data->isoc_rx_ep = NULL;
1137
1138 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
1139 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
1140
1141 if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) {
1142 data->isoc_tx_ep = ep_desc;
1143 continue;
1144 }
1145
1146 if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) {
1147 data->isoc_rx_ep = ep_desc;
1148 continue;
1149 }
1150 }
1151
1152 if (!data->isoc_tx_ep || !data->isoc_rx_ep) {
1153 BT_ERR("%s invalid SCO descriptors", hdev->name);
1154 return -ENODEV;
1155 }
1156
1157 return 0;
1158}
1159
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001160static void btusb_work(struct work_struct *work)
1161{
1162 struct btusb_data *data = container_of(work, struct btusb_data, work);
1163 struct hci_dev *hdev = data->hdev;
Mikel Astizf4001d22012-04-11 08:48:51 +02001164 int new_alts;
Oliver Neukum7bee5492009-08-24 23:44:59 +02001165 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001166
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001167 if (data->sco_num > 0) {
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -03001168 if (!test_bit(BTUSB_DID_ISO_RESUME, &data->flags)) {
Oliver Neukum8efdd0c2011-02-11 13:00:06 +01001169 err = usb_autopm_get_interface(data->isoc ? data->isoc : data->intf);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001170 if (err < 0) {
1171 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1172 usb_kill_anchored_urbs(&data->isoc_anchor);
1173 return;
1174 }
1175
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -03001176 set_bit(BTUSB_DID_ISO_RESUME, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001177 }
Mikel Astizf4001d22012-04-11 08:48:51 +02001178
1179 if (hdev->voice_setting & 0x0020) {
1180 static const int alts[3] = { 2, 4, 5 };
Marcel Holtmann89e75332014-09-16 04:44:50 +02001181
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001182 new_alts = alts[data->sco_num - 1];
Mikel Astizf4001d22012-04-11 08:48:51 +02001183 } else {
Marcel Holtmann014f7bc2013-10-10 09:47:55 -07001184 new_alts = data->sco_num;
Mikel Astizf4001d22012-04-11 08:48:51 +02001185 }
1186
1187 if (data->isoc_altsetting != new_alts) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001188 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1189 usb_kill_anchored_urbs(&data->isoc_anchor);
1190
Mikel Astizf4001d22012-04-11 08:48:51 +02001191 if (__set_isoc_interface(hdev, new_alts) < 0)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001192 return;
1193 }
1194
1195 if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001196 if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001197 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1198 else
Marcel Holtmann2eda66f2008-11-30 12:17:10 +01001199 btusb_submit_isoc_urb(hdev, GFP_KERNEL);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001200 }
1201 } else {
1202 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1203 usb_kill_anchored_urbs(&data->isoc_anchor);
1204
1205 __set_isoc_interface(hdev, 0);
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -03001206 if (test_and_clear_bit(BTUSB_DID_ISO_RESUME, &data->flags))
Oliver Neukum8efdd0c2011-02-11 13:00:06 +01001207 usb_autopm_put_interface(data->isoc ? data->isoc : data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001208 }
1209}
1210
Oliver Neukum7bee5492009-08-24 23:44:59 +02001211static void btusb_waker(struct work_struct *work)
1212{
1213 struct btusb_data *data = container_of(work, struct btusb_data, waker);
1214 int err;
1215
1216 err = usb_autopm_get_interface(data->intf);
1217 if (err < 0)
1218 return;
1219
1220 usb_autopm_put_interface(data->intf);
1221}
1222
Marcel Holtmann9f8f9622013-04-10 08:11:35 -07001223static int btusb_setup_bcm92035(struct hci_dev *hdev)
1224{
1225 struct sk_buff *skb;
1226 u8 val = 0x00;
1227
1228 BT_DBG("%s", hdev->name);
1229
1230 skb = __hci_cmd_sync(hdev, 0xfc3b, 1, &val, HCI_INIT_TIMEOUT);
1231 if (IS_ERR(skb))
1232 BT_ERR("BCM92035 command failed (%ld)", -PTR_ERR(skb));
1233 else
1234 kfree_skb(skb);
1235
1236 return 0;
1237}
1238
Marcel Holtmann81cac642014-01-03 03:02:36 -08001239static int btusb_setup_csr(struct hci_dev *hdev)
1240{
1241 struct hci_rp_read_local_version *rp;
1242 struct sk_buff *skb;
1243 int ret;
1244
1245 BT_DBG("%s", hdev->name);
1246
1247 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
1248 HCI_INIT_TIMEOUT);
1249 if (IS_ERR(skb)) {
1250 BT_ERR("Reading local version failed (%ld)", -PTR_ERR(skb));
1251 return -PTR_ERR(skb);
1252 }
1253
Marcel Holtmann89e75332014-09-16 04:44:50 +02001254 rp = (struct hci_rp_read_local_version *)skb->data;
Marcel Holtmann81cac642014-01-03 03:02:36 -08001255
1256 if (!rp->status) {
1257 if (le16_to_cpu(rp->manufacturer) != 10) {
1258 /* Clear the reset quirk since this is not an actual
1259 * early Bluetooth 1.1 device from CSR.
1260 */
1261 clear_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
1262
1263 /* These fake CSR controllers have all a broken
1264 * stored link key handling and so just disable it.
1265 */
1266 set_bit(HCI_QUIRK_BROKEN_STORED_LINK_KEY,
1267 &hdev->quirks);
1268 }
1269 }
1270
1271 ret = -bt_to_errno(rp->status);
1272
1273 kfree_skb(skb);
1274
1275 return ret;
1276}
1277
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001278struct intel_version {
1279 u8 status;
1280 u8 hw_platform;
1281 u8 hw_variant;
1282 u8 hw_revision;
1283 u8 fw_variant;
1284 u8 fw_revision;
1285 u8 fw_build_num;
1286 u8 fw_build_ww;
1287 u8 fw_build_yy;
1288 u8 fw_patch_num;
1289} __packed;
1290
1291static const struct firmware *btusb_setup_intel_get_fw(struct hci_dev *hdev,
Marcel Holtmann89e75332014-09-16 04:44:50 +02001292 struct intel_version *ver)
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001293{
1294 const struct firmware *fw;
1295 char fwname[64];
1296 int ret;
1297
1298 snprintf(fwname, sizeof(fwname),
1299 "intel/ibt-hw-%x.%x.%x-fw-%x.%x.%x.%x.%x.bseq",
1300 ver->hw_platform, ver->hw_variant, ver->hw_revision,
1301 ver->fw_variant, ver->fw_revision, ver->fw_build_num,
1302 ver->fw_build_ww, ver->fw_build_yy);
1303
1304 ret = request_firmware(&fw, fwname, &hdev->dev);
1305 if (ret < 0) {
1306 if (ret == -EINVAL) {
1307 BT_ERR("%s Intel firmware file request failed (%d)",
1308 hdev->name, ret);
1309 return NULL;
1310 }
1311
1312 BT_ERR("%s failed to open Intel firmware file: %s(%d)",
1313 hdev->name, fwname, ret);
1314
1315 /* If the correct firmware patch file is not found, use the
1316 * default firmware patch file instead
1317 */
1318 snprintf(fwname, sizeof(fwname), "intel/ibt-hw-%x.%x.bseq",
1319 ver->hw_platform, ver->hw_variant);
1320 if (request_firmware(&fw, fwname, &hdev->dev) < 0) {
1321 BT_ERR("%s failed to open default Intel fw file: %s",
1322 hdev->name, fwname);
1323 return NULL;
1324 }
1325 }
1326
1327 BT_INFO("%s: Intel Bluetooth firmware file: %s", hdev->name, fwname);
1328
1329 return fw;
1330}
1331
1332static int btusb_setup_intel_patching(struct hci_dev *hdev,
1333 const struct firmware *fw,
1334 const u8 **fw_ptr, int *disable_patch)
1335{
1336 struct sk_buff *skb;
1337 struct hci_command_hdr *cmd;
1338 const u8 *cmd_param;
1339 struct hci_event_hdr *evt = NULL;
1340 const u8 *evt_param = NULL;
1341 int remain = fw->size - (*fw_ptr - fw->data);
1342
1343 /* The first byte indicates the types of the patch command or event.
1344 * 0x01 means HCI command and 0x02 is HCI event. If the first bytes
1345 * in the current firmware buffer doesn't start with 0x01 or
1346 * the size of remain buffer is smaller than HCI command header,
1347 * the firmware file is corrupted and it should stop the patching
1348 * process.
1349 */
1350 if (remain > HCI_COMMAND_HDR_SIZE && *fw_ptr[0] != 0x01) {
1351 BT_ERR("%s Intel fw corrupted: invalid cmd read", hdev->name);
1352 return -EINVAL;
1353 }
1354 (*fw_ptr)++;
1355 remain--;
1356
1357 cmd = (struct hci_command_hdr *)(*fw_ptr);
1358 *fw_ptr += sizeof(*cmd);
1359 remain -= sizeof(*cmd);
1360
1361 /* Ensure that the remain firmware data is long enough than the length
1362 * of command parameter. If not, the firmware file is corrupted.
1363 */
1364 if (remain < cmd->plen) {
1365 BT_ERR("%s Intel fw corrupted: invalid cmd len", hdev->name);
1366 return -EFAULT;
1367 }
1368
1369 /* If there is a command that loads a patch in the firmware
1370 * file, then enable the patch upon success, otherwise just
1371 * disable the manufacturer mode, for example patch activation
1372 * is not required when the default firmware patch file is used
1373 * because there are no patch data to load.
1374 */
1375 if (*disable_patch && le16_to_cpu(cmd->opcode) == 0xfc8e)
1376 *disable_patch = 0;
1377
1378 cmd_param = *fw_ptr;
1379 *fw_ptr += cmd->plen;
1380 remain -= cmd->plen;
1381
1382 /* This reads the expected events when the above command is sent to the
1383 * device. Some vendor commands expects more than one events, for
1384 * example command status event followed by vendor specific event.
1385 * For this case, it only keeps the last expected event. so the command
1386 * can be sent with __hci_cmd_sync_ev() which returns the sk_buff of
1387 * last expected event.
1388 */
1389 while (remain > HCI_EVENT_HDR_SIZE && *fw_ptr[0] == 0x02) {
1390 (*fw_ptr)++;
1391 remain--;
1392
1393 evt = (struct hci_event_hdr *)(*fw_ptr);
1394 *fw_ptr += sizeof(*evt);
1395 remain -= sizeof(*evt);
1396
1397 if (remain < evt->plen) {
1398 BT_ERR("%s Intel fw corrupted: invalid evt len",
1399 hdev->name);
1400 return -EFAULT;
1401 }
1402
1403 evt_param = *fw_ptr;
1404 *fw_ptr += evt->plen;
1405 remain -= evt->plen;
1406 }
1407
1408 /* Every HCI commands in the firmware file has its correspond event.
1409 * If event is not found or remain is smaller than zero, the firmware
1410 * file is corrupted.
1411 */
1412 if (!evt || !evt_param || remain < 0) {
1413 BT_ERR("%s Intel fw corrupted: invalid evt read", hdev->name);
1414 return -EFAULT;
1415 }
1416
1417 skb = __hci_cmd_sync_ev(hdev, le16_to_cpu(cmd->opcode), cmd->plen,
1418 cmd_param, evt->evt, HCI_INIT_TIMEOUT);
1419 if (IS_ERR(skb)) {
1420 BT_ERR("%s sending Intel patch command (0x%4.4x) failed (%ld)",
1421 hdev->name, cmd->opcode, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001422 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001423 }
1424
1425 /* It ensures that the returned event matches the event data read from
1426 * the firmware file. At fist, it checks the length and then
1427 * the contents of the event.
1428 */
1429 if (skb->len != evt->plen) {
1430 BT_ERR("%s mismatch event length (opcode 0x%4.4x)", hdev->name,
1431 le16_to_cpu(cmd->opcode));
1432 kfree_skb(skb);
1433 return -EFAULT;
1434 }
1435
1436 if (memcmp(skb->data, evt_param, evt->plen)) {
1437 BT_ERR("%s mismatch event parameter (opcode 0x%4.4x)",
1438 hdev->name, le16_to_cpu(cmd->opcode));
1439 kfree_skb(skb);
1440 return -EFAULT;
1441 }
1442 kfree_skb(skb);
1443
1444 return 0;
1445}
1446
Marcel Holtmann40cb0982014-07-02 12:06:45 +02001447#define BDADDR_INTEL (&(bdaddr_t) {{0x00, 0x8b, 0x9e, 0x19, 0x03, 0x00}})
1448
1449static int btusb_check_bdaddr_intel(struct hci_dev *hdev)
1450{
1451 struct sk_buff *skb;
1452 struct hci_rp_read_bd_addr *rp;
1453
1454 skb = __hci_cmd_sync(hdev, HCI_OP_READ_BD_ADDR, 0, NULL,
1455 HCI_INIT_TIMEOUT);
1456 if (IS_ERR(skb)) {
1457 BT_ERR("%s reading Intel device address failed (%ld)",
1458 hdev->name, PTR_ERR(skb));
1459 return PTR_ERR(skb);
1460 }
1461
1462 if (skb->len != sizeof(*rp)) {
1463 BT_ERR("%s Intel device address length mismatch", hdev->name);
1464 kfree_skb(skb);
1465 return -EIO;
1466 }
1467
Marcel Holtmann89e75332014-09-16 04:44:50 +02001468 rp = (struct hci_rp_read_bd_addr *)skb->data;
Marcel Holtmann40cb0982014-07-02 12:06:45 +02001469 if (rp->status) {
1470 BT_ERR("%s Intel device address result failed (%02x)",
1471 hdev->name, rp->status);
1472 kfree_skb(skb);
1473 return -bt_to_errno(rp->status);
1474 }
1475
1476 /* For some Intel based controllers, the default Bluetooth device
1477 * address 00:03:19:9E:8B:00 can be found. These controllers are
1478 * fully operational, but have the danger of duplicate addresses
1479 * and that in turn can cause problems with Bluetooth operation.
1480 */
Marcel Holtmann4739b5b2014-07-04 16:54:38 +02001481 if (!bacmp(&rp->bdaddr, BDADDR_INTEL)) {
Marcel Holtmann40cb0982014-07-02 12:06:45 +02001482 BT_ERR("%s found Intel default device address (%pMR)",
1483 hdev->name, &rp->bdaddr);
Marcel Holtmann4739b5b2014-07-04 16:54:38 +02001484 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
1485 }
Marcel Holtmann40cb0982014-07-02 12:06:45 +02001486
1487 kfree_skb(skb);
1488
1489 return 0;
1490}
1491
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001492static int btusb_setup_intel(struct hci_dev *hdev)
1493{
1494 struct sk_buff *skb;
1495 const struct firmware *fw;
1496 const u8 *fw_ptr;
1497 int disable_patch;
1498 struct intel_version *ver;
1499
1500 const u8 mfg_enable[] = { 0x01, 0x00 };
1501 const u8 mfg_disable[] = { 0x00, 0x00 };
1502 const u8 mfg_reset_deactivate[] = { 0x00, 0x01 };
1503 const u8 mfg_reset_activate[] = { 0x00, 0x02 };
1504
1505 BT_DBG("%s", hdev->name);
1506
1507 /* The controller has a bug with the first HCI command sent to it
1508 * returning number of completed commands as zero. This would stall the
1509 * command processing in the Bluetooth core.
1510 *
1511 * As a workaround, send HCI Reset command first which will reset the
1512 * number of completed commands and allow normal command processing
1513 * from now on.
1514 */
1515 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
1516 if (IS_ERR(skb)) {
1517 BT_ERR("%s sending initial HCI reset command failed (%ld)",
1518 hdev->name, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001519 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001520 }
1521 kfree_skb(skb);
1522
1523 /* Read Intel specific controller version first to allow selection of
1524 * which firmware file to load.
1525 *
1526 * The returned information are hardware variant and revision plus
1527 * firmware variant, revision and build number.
1528 */
1529 skb = __hci_cmd_sync(hdev, 0xfc05, 0, NULL, HCI_INIT_TIMEOUT);
1530 if (IS_ERR(skb)) {
1531 BT_ERR("%s reading Intel fw version command failed (%ld)",
1532 hdev->name, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001533 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001534 }
1535
1536 if (skb->len != sizeof(*ver)) {
1537 BT_ERR("%s Intel version event length mismatch", hdev->name);
1538 kfree_skb(skb);
1539 return -EIO;
1540 }
1541
1542 ver = (struct intel_version *)skb->data;
1543 if (ver->status) {
1544 BT_ERR("%s Intel fw version event failed (%02x)", hdev->name,
1545 ver->status);
1546 kfree_skb(skb);
1547 return -bt_to_errno(ver->status);
1548 }
1549
1550 BT_INFO("%s: read Intel version: %02x%02x%02x%02x%02x%02x%02x%02x%02x",
1551 hdev->name, ver->hw_platform, ver->hw_variant,
1552 ver->hw_revision, ver->fw_variant, ver->fw_revision,
1553 ver->fw_build_num, ver->fw_build_ww, ver->fw_build_yy,
1554 ver->fw_patch_num);
1555
1556 /* fw_patch_num indicates the version of patch the device currently
1557 * have. If there is no patch data in the device, it is always 0x00.
1558 * So, if it is other than 0x00, no need to patch the deivce again.
1559 */
1560 if (ver->fw_patch_num) {
1561 BT_INFO("%s: Intel device is already patched. patch num: %02x",
1562 hdev->name, ver->fw_patch_num);
1563 kfree_skb(skb);
Marcel Holtmann40cb0982014-07-02 12:06:45 +02001564 btusb_check_bdaddr_intel(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001565 return 0;
1566 }
1567
1568 /* Opens the firmware patch file based on the firmware version read
1569 * from the controller. If it fails to open the matching firmware
1570 * patch file, it tries to open the default firmware patch file.
1571 * If no patch file is found, allow the device to operate without
1572 * a patch.
1573 */
1574 fw = btusb_setup_intel_get_fw(hdev, ver);
1575 if (!fw) {
1576 kfree_skb(skb);
Marcel Holtmann40cb0982014-07-02 12:06:45 +02001577 btusb_check_bdaddr_intel(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001578 return 0;
1579 }
1580 fw_ptr = fw->data;
1581
1582 /* This Intel specific command enables the manufacturer mode of the
1583 * controller.
1584 *
1585 * Only while this mode is enabled, the driver can download the
1586 * firmware patch data and configuration parameters.
1587 */
1588 skb = __hci_cmd_sync(hdev, 0xfc11, 2, mfg_enable, HCI_INIT_TIMEOUT);
1589 if (IS_ERR(skb)) {
1590 BT_ERR("%s entering Intel manufacturer mode failed (%ld)",
1591 hdev->name, PTR_ERR(skb));
1592 release_firmware(fw);
Adam Leed9c78e92013-07-10 10:02:12 +08001593 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001594 }
1595
1596 if (skb->data[0]) {
1597 u8 evt_status = skb->data[0];
Marcel Holtmann89e75332014-09-16 04:44:50 +02001598
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001599 BT_ERR("%s enable Intel manufacturer mode event failed (%02x)",
1600 hdev->name, evt_status);
1601 kfree_skb(skb);
1602 release_firmware(fw);
1603 return -bt_to_errno(evt_status);
1604 }
1605 kfree_skb(skb);
1606
1607 disable_patch = 1;
1608
1609 /* The firmware data file consists of list of Intel specific HCI
1610 * commands and its expected events. The first byte indicates the
1611 * type of the message, either HCI command or HCI event.
1612 *
1613 * It reads the command and its expected event from the firmware file,
1614 * and send to the controller. Once __hci_cmd_sync_ev() returns,
1615 * the returned event is compared with the event read from the firmware
1616 * file and it will continue until all the messages are downloaded to
1617 * the controller.
1618 *
1619 * Once the firmware patching is completed successfully,
1620 * the manufacturer mode is disabled with reset and activating the
1621 * downloaded patch.
1622 *
1623 * If the firmware patching fails, the manufacturer mode is
1624 * disabled with reset and deactivating the patch.
1625 *
1626 * If the default patch file is used, no reset is done when disabling
1627 * the manufacturer.
1628 */
1629 while (fw->size > fw_ptr - fw->data) {
1630 int ret;
1631
1632 ret = btusb_setup_intel_patching(hdev, fw, &fw_ptr,
1633 &disable_patch);
1634 if (ret < 0)
1635 goto exit_mfg_deactivate;
1636 }
1637
1638 release_firmware(fw);
1639
1640 if (disable_patch)
1641 goto exit_mfg_disable;
1642
1643 /* Patching completed successfully and disable the manufacturer mode
1644 * with reset and activate the downloaded firmware patches.
1645 */
1646 skb = __hci_cmd_sync(hdev, 0xfc11, sizeof(mfg_reset_activate),
1647 mfg_reset_activate, HCI_INIT_TIMEOUT);
1648 if (IS_ERR(skb)) {
1649 BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
1650 hdev->name, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001651 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001652 }
1653 kfree_skb(skb);
1654
1655 BT_INFO("%s: Intel Bluetooth firmware patch completed and activated",
1656 hdev->name);
1657
Marcel Holtmann40cb0982014-07-02 12:06:45 +02001658 btusb_check_bdaddr_intel(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001659 return 0;
1660
1661exit_mfg_disable:
1662 /* Disable the manufacturer mode without reset */
1663 skb = __hci_cmd_sync(hdev, 0xfc11, sizeof(mfg_disable), mfg_disable,
1664 HCI_INIT_TIMEOUT);
1665 if (IS_ERR(skb)) {
1666 BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
1667 hdev->name, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001668 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001669 }
1670 kfree_skb(skb);
1671
1672 BT_INFO("%s: Intel Bluetooth firmware patch completed", hdev->name);
Marcel Holtmann40cb0982014-07-02 12:06:45 +02001673
1674 btusb_check_bdaddr_intel(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001675 return 0;
1676
1677exit_mfg_deactivate:
1678 release_firmware(fw);
1679
1680 /* Patching failed. Disable the manufacturer mode with reset and
1681 * deactivate the downloaded firmware patches.
1682 */
1683 skb = __hci_cmd_sync(hdev, 0xfc11, sizeof(mfg_reset_deactivate),
1684 mfg_reset_deactivate, HCI_INIT_TIMEOUT);
1685 if (IS_ERR(skb)) {
1686 BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
1687 hdev->name, PTR_ERR(skb));
Adam Leed9c78e92013-07-10 10:02:12 +08001688 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001689 }
1690 kfree_skb(skb);
1691
1692 BT_INFO("%s: Intel Bluetooth firmware patch completed and deactivated",
1693 hdev->name);
1694
Marcel Holtmann40cb0982014-07-02 12:06:45 +02001695 btusb_check_bdaddr_intel(hdev);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001696 return 0;
1697}
1698
Marcel Holtmanncb8d6592014-07-02 11:25:25 +02001699static int btusb_set_bdaddr_intel(struct hci_dev *hdev, const bdaddr_t *bdaddr)
1700{
1701 struct sk_buff *skb;
1702 long ret;
1703
1704 skb = __hci_cmd_sync(hdev, 0xfc31, 6, bdaddr, HCI_INIT_TIMEOUT);
1705 if (IS_ERR(skb)) {
1706 ret = PTR_ERR(skb);
1707 BT_ERR("%s: changing Intel device address failed (%ld)",
Marcel Holtmann89e75332014-09-16 04:44:50 +02001708 hdev->name, ret);
Marcel Holtmanncb8d6592014-07-02 11:25:25 +02001709 return ret;
1710 }
1711 kfree_skb(skb);
1712
1713 return 0;
1714}
1715
Amitkumar Karwarae8df492014-07-18 14:47:06 -07001716static int btusb_set_bdaddr_marvell(struct hci_dev *hdev,
1717 const bdaddr_t *bdaddr)
1718{
1719 struct sk_buff *skb;
1720 u8 buf[8];
1721 long ret;
1722
1723 buf[0] = 0xfe;
1724 buf[1] = sizeof(bdaddr_t);
1725 memcpy(buf + 2, bdaddr, sizeof(bdaddr_t));
1726
1727 skb = __hci_cmd_sync(hdev, 0xfc22, sizeof(buf), buf, HCI_INIT_TIMEOUT);
1728 if (IS_ERR(skb)) {
1729 ret = PTR_ERR(skb);
1730 BT_ERR("%s: changing Marvell device address failed (%ld)",
1731 hdev->name, ret);
1732 return ret;
1733 }
1734 kfree_skb(skb);
1735
1736 return 0;
1737}
1738
Marcel Holtmannc8abb732014-07-02 12:38:22 +02001739#define BDADDR_BCM20702A0 (&(bdaddr_t) {{0x00, 0xa0, 0x02, 0x70, 0x20, 0x00}})
1740
Petri Gynther10d4c672014-05-08 15:50:01 -07001741static int btusb_setup_bcm_patchram(struct hci_dev *hdev)
1742{
1743 struct btusb_data *data = hci_get_drvdata(hdev);
1744 struct usb_device *udev = data->udev;
1745 char fw_name[64];
1746 const struct firmware *fw;
1747 const u8 *fw_ptr;
1748 size_t fw_size;
1749 const struct hci_command_hdr *cmd;
1750 const u8 *cmd_param;
1751 u16 opcode;
1752 struct sk_buff *skb;
1753 struct hci_rp_read_local_version *ver;
Marcel Holtmannc8abb732014-07-02 12:38:22 +02001754 struct hci_rp_read_bd_addr *bda;
Petri Gynther10d4c672014-05-08 15:50:01 -07001755 long ret;
1756
1757 snprintf(fw_name, sizeof(fw_name), "brcm/%s-%04x-%04x.hcd",
1758 udev->product ? udev->product : "BCM",
1759 le16_to_cpu(udev->descriptor.idVendor),
1760 le16_to_cpu(udev->descriptor.idProduct));
1761
1762 ret = request_firmware(&fw, fw_name, &hdev->dev);
1763 if (ret < 0) {
Marcel Holtmannc8abb732014-07-02 12:38:22 +02001764 BT_INFO("%s: BCM: patch %s not found", hdev->name, fw_name);
Petri Gynther10d4c672014-05-08 15:50:01 -07001765 return 0;
1766 }
1767
1768 /* Reset */
1769 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
1770 if (IS_ERR(skb)) {
1771 ret = PTR_ERR(skb);
1772 BT_ERR("%s: HCI_OP_RESET failed (%ld)", hdev->name, ret);
1773 goto done;
1774 }
1775 kfree_skb(skb);
1776
1777 /* Read Local Version Info */
1778 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
1779 HCI_INIT_TIMEOUT);
1780 if (IS_ERR(skb)) {
1781 ret = PTR_ERR(skb);
1782 BT_ERR("%s: HCI_OP_READ_LOCAL_VERSION failed (%ld)",
Marcel Holtmann89e75332014-09-16 04:44:50 +02001783 hdev->name, ret);
Petri Gynther10d4c672014-05-08 15:50:01 -07001784 goto done;
1785 }
1786
1787 if (skb->len != sizeof(*ver)) {
1788 BT_ERR("%s: HCI_OP_READ_LOCAL_VERSION event length mismatch",
Marcel Holtmann89e75332014-09-16 04:44:50 +02001789 hdev->name);
Petri Gynther10d4c672014-05-08 15:50:01 -07001790 kfree_skb(skb);
1791 ret = -EIO;
1792 goto done;
1793 }
1794
Marcel Holtmann89e75332014-09-16 04:44:50 +02001795 ver = (struct hci_rp_read_local_version *)skb->data;
Petri Gynther10d4c672014-05-08 15:50:01 -07001796 BT_INFO("%s: BCM: patching hci_ver=%02x hci_rev=%04x lmp_ver=%02x "
1797 "lmp_subver=%04x", hdev->name, ver->hci_ver, ver->hci_rev,
1798 ver->lmp_ver, ver->lmp_subver);
1799 kfree_skb(skb);
1800
1801 /* Start Download */
1802 skb = __hci_cmd_sync(hdev, 0xfc2e, 0, NULL, HCI_INIT_TIMEOUT);
1803 if (IS_ERR(skb)) {
1804 ret = PTR_ERR(skb);
1805 BT_ERR("%s: BCM: Download Minidrv command failed (%ld)",
Marcel Holtmann89e75332014-09-16 04:44:50 +02001806 hdev->name, ret);
Petri Gynther10d4c672014-05-08 15:50:01 -07001807 goto reset_fw;
1808 }
1809 kfree_skb(skb);
1810
1811 /* 50 msec delay after Download Minidrv completes */
1812 msleep(50);
1813
1814 fw_ptr = fw->data;
1815 fw_size = fw->size;
1816
1817 while (fw_size >= sizeof(*cmd)) {
Marcel Holtmann89e75332014-09-16 04:44:50 +02001818 cmd = (struct hci_command_hdr *)fw_ptr;
Petri Gynther10d4c672014-05-08 15:50:01 -07001819 fw_ptr += sizeof(*cmd);
1820 fw_size -= sizeof(*cmd);
1821
1822 if (fw_size < cmd->plen) {
1823 BT_ERR("%s: BCM: patch %s is corrupted",
Marcel Holtmann89e75332014-09-16 04:44:50 +02001824 hdev->name, fw_name);
Petri Gynther10d4c672014-05-08 15:50:01 -07001825 ret = -EINVAL;
1826 goto reset_fw;
1827 }
1828
1829 cmd_param = fw_ptr;
1830 fw_ptr += cmd->plen;
1831 fw_size -= cmd->plen;
1832
1833 opcode = le16_to_cpu(cmd->opcode);
1834
1835 skb = __hci_cmd_sync(hdev, opcode, cmd->plen, cmd_param,
1836 HCI_INIT_TIMEOUT);
1837 if (IS_ERR(skb)) {
1838 ret = PTR_ERR(skb);
1839 BT_ERR("%s: BCM: patch command %04x failed (%ld)",
Marcel Holtmann89e75332014-09-16 04:44:50 +02001840 hdev->name, opcode, ret);
Petri Gynther10d4c672014-05-08 15:50:01 -07001841 goto reset_fw;
1842 }
1843 kfree_skb(skb);
1844 }
1845
1846 /* 250 msec delay after Launch Ram completes */
1847 msleep(250);
1848
1849reset_fw:
1850 /* Reset */
1851 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
1852 if (IS_ERR(skb)) {
1853 ret = PTR_ERR(skb);
1854 BT_ERR("%s: HCI_OP_RESET failed (%ld)", hdev->name, ret);
1855 goto done;
1856 }
1857 kfree_skb(skb);
1858
1859 /* Read Local Version Info */
1860 skb = __hci_cmd_sync(hdev, HCI_OP_READ_LOCAL_VERSION, 0, NULL,
1861 HCI_INIT_TIMEOUT);
1862 if (IS_ERR(skb)) {
1863 ret = PTR_ERR(skb);
1864 BT_ERR("%s: HCI_OP_READ_LOCAL_VERSION failed (%ld)",
Marcel Holtmann89e75332014-09-16 04:44:50 +02001865 hdev->name, ret);
Petri Gynther10d4c672014-05-08 15:50:01 -07001866 goto done;
1867 }
1868
1869 if (skb->len != sizeof(*ver)) {
1870 BT_ERR("%s: HCI_OP_READ_LOCAL_VERSION event length mismatch",
Marcel Holtmann89e75332014-09-16 04:44:50 +02001871 hdev->name);
Petri Gynther10d4c672014-05-08 15:50:01 -07001872 kfree_skb(skb);
1873 ret = -EIO;
1874 goto done;
1875 }
1876
Marcel Holtmann89e75332014-09-16 04:44:50 +02001877 ver = (struct hci_rp_read_local_version *)skb->data;
Petri Gynther10d4c672014-05-08 15:50:01 -07001878 BT_INFO("%s: BCM: firmware hci_ver=%02x hci_rev=%04x lmp_ver=%02x "
1879 "lmp_subver=%04x", hdev->name, ver->hci_ver, ver->hci_rev,
1880 ver->lmp_ver, ver->lmp_subver);
1881 kfree_skb(skb);
1882
Marcel Holtmannc8abb732014-07-02 12:38:22 +02001883 /* Read BD Address */
1884 skb = __hci_cmd_sync(hdev, HCI_OP_READ_BD_ADDR, 0, NULL,
1885 HCI_INIT_TIMEOUT);
1886 if (IS_ERR(skb)) {
1887 ret = PTR_ERR(skb);
1888 BT_ERR("%s: HCI_OP_READ_BD_ADDR failed (%ld)",
Marcel Holtmann89e75332014-09-16 04:44:50 +02001889 hdev->name, ret);
Marcel Holtmannc8abb732014-07-02 12:38:22 +02001890 goto done;
1891 }
1892
1893 if (skb->len != sizeof(*bda)) {
1894 BT_ERR("%s: HCI_OP_READ_BD_ADDR event length mismatch",
Marcel Holtmann89e75332014-09-16 04:44:50 +02001895 hdev->name);
Marcel Holtmannc8abb732014-07-02 12:38:22 +02001896 kfree_skb(skb);
1897 ret = -EIO;
1898 goto done;
1899 }
1900
Marcel Holtmann89e75332014-09-16 04:44:50 +02001901 bda = (struct hci_rp_read_bd_addr *)skb->data;
Marcel Holtmannc8abb732014-07-02 12:38:22 +02001902 if (bda->status) {
1903 BT_ERR("%s: HCI_OP_READ_BD_ADDR error status (%02x)",
1904 hdev->name, bda->status);
1905 kfree_skb(skb);
1906 ret = -bt_to_errno(bda->status);
1907 goto done;
1908 }
1909
1910 /* The address 00:20:70:02:A0:00 indicates a BCM20702A0 controller
1911 * with no configured address.
1912 */
Marcel Holtmann849e5082014-07-04 16:54:39 +02001913 if (!bacmp(&bda->bdaddr, BDADDR_BCM20702A0)) {
Marcel Holtmannc8abb732014-07-02 12:38:22 +02001914 BT_INFO("%s: BCM: using default device address (%pMR)",
1915 hdev->name, &bda->bdaddr);
Marcel Holtmann849e5082014-07-04 16:54:39 +02001916 set_bit(HCI_QUIRK_INVALID_BDADDR, &hdev->quirks);
1917 }
Marcel Holtmannc8abb732014-07-02 12:38:22 +02001918
1919 kfree_skb(skb);
1920
Petri Gynther10d4c672014-05-08 15:50:01 -07001921done:
1922 release_firmware(fw);
1923
1924 return ret;
1925}
1926
Marcel Holtmannabbaf502014-07-02 00:53:48 +02001927static int btusb_set_bdaddr_bcm(struct hci_dev *hdev, const bdaddr_t *bdaddr)
1928{
1929 struct sk_buff *skb;
1930 long ret;
1931
1932 skb = __hci_cmd_sync(hdev, 0xfc01, 6, bdaddr, HCI_INIT_TIMEOUT);
1933 if (IS_ERR(skb)) {
1934 ret = PTR_ERR(skb);
1935 BT_ERR("%s: BCM: Change address command failed (%ld)",
Marcel Holtmann89e75332014-09-16 04:44:50 +02001936 hdev->name, ret);
Marcel Holtmannabbaf502014-07-02 00:53:48 +02001937 return ret;
1938 }
1939 kfree_skb(skb);
1940
1941 return 0;
1942}
1943
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001944static int btusb_probe(struct usb_interface *intf,
Marcel Holtmann89e75332014-09-16 04:44:50 +02001945 const struct usb_device_id *id)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001946{
1947 struct usb_endpoint_descriptor *ep_desc;
1948 struct btusb_data *data;
1949 struct hci_dev *hdev;
1950 int i, err;
1951
1952 BT_DBG("intf %p id %p", intf, id);
1953
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02001954 /* interface numbers are hardcoded in the spec */
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001955 if (intf->cur_altsetting->desc.bInterfaceNumber != 0)
1956 return -ENODEV;
1957
1958 if (!id->driver_info) {
1959 const struct usb_device_id *match;
Marcel Holtmann89e75332014-09-16 04:44:50 +02001960
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001961 match = usb_match_id(intf, blacklist_table);
1962 if (match)
1963 id = match;
1964 }
1965
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02001966 if (id->driver_info == BTUSB_IGNORE)
1967 return -ENODEV;
1968
Steven.Li2d25f8b2011-07-01 14:02:36 +08001969 if (id->driver_info & BTUSB_ATH3012) {
1970 struct usb_device *udev = interface_to_usbdev(intf);
1971
1972 /* Old firmware would otherwise let ath3k driver load
1973 * patch and sysconfig files */
1974 if (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x0001)
1975 return -ENODEV;
1976 }
1977
Sachin Kamat98921db2012-07-27 12:38:39 +05301978 data = devm_kzalloc(&intf->dev, sizeof(*data), GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001979 if (!data)
1980 return -ENOMEM;
1981
1982 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
1983 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
1984
1985 if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
1986 data->intr_ep = ep_desc;
1987 continue;
1988 }
1989
1990 if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
1991 data->bulk_tx_ep = ep_desc;
1992 continue;
1993 }
1994
1995 if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
1996 data->bulk_rx_ep = ep_desc;
1997 continue;
1998 }
1999 }
2000
Sachin Kamat98921db2012-07-27 12:38:39 +05302001 if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002002 return -ENODEV;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002003
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002004 data->cmdreq_type = USB_TYPE_CLASS;
2005
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002006 data->udev = interface_to_usbdev(intf);
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02002007 data->intf = intf;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002008
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002009 INIT_WORK(&data->work, btusb_work);
Oliver Neukum7bee5492009-08-24 23:44:59 +02002010 INIT_WORK(&data->waker, btusb_waker);
Marcel Holtmann803b5832014-09-16 08:00:29 +02002011 init_usb_anchor(&data->deferred);
2012 init_usb_anchor(&data->tx_anchor);
Oliver Neukum7bee5492009-08-24 23:44:59 +02002013 spin_lock_init(&data->txlock);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002014
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002015 init_usb_anchor(&data->intr_anchor);
2016 init_usb_anchor(&data->bulk_anchor);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02002017 init_usb_anchor(&data->isoc_anchor);
Marcel Holtmann803b5832014-09-16 08:00:29 +02002018 spin_lock_init(&data->rxlock);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002019
Marcel Holtmann2cbd3f52014-11-03 05:16:07 +01002020 data->recv_bulk = btusb_recv_bulk;
2021
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002022 hdev = hci_alloc_dev();
Sachin Kamat98921db2012-07-27 12:38:39 +05302023 if (!hdev)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002024 return -ENOMEM;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002025
Marcel Holtmannc13854c2010-02-08 15:27:07 +01002026 hdev->bus = HCI_USB;
David Herrmann155961e2012-02-09 21:58:32 +01002027 hci_set_drvdata(hdev, data);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002028
2029 data->hdev = hdev;
2030
2031 SET_HCIDEV_DEV(hdev, &intf->dev);
2032
Marcel Holtmann9f8f9622013-04-10 08:11:35 -07002033 hdev->open = btusb_open;
2034 hdev->close = btusb_close;
2035 hdev->flush = btusb_flush;
2036 hdev->send = btusb_send_frame;
2037 hdev->notify = btusb_notify;
2038
2039 if (id->driver_info & BTUSB_BCM92035)
2040 hdev->setup = btusb_setup_bcm92035;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002041
Marcel Holtmannabbaf502014-07-02 00:53:48 +02002042 if (id->driver_info & BTUSB_BCM_PATCHRAM) {
Petri Gynther10d4c672014-05-08 15:50:01 -07002043 hdev->setup = btusb_setup_bcm_patchram;
Marcel Holtmannabbaf502014-07-02 00:53:48 +02002044 hdev->set_bdaddr = btusb_set_bdaddr_bcm;
Marcel Holtmann27c3fbe2014-11-02 20:52:24 +01002045 set_bit(HCI_QUIRK_STRICT_DUPLICATE_FILTER, &hdev->quirks);
Marcel Holtmannabbaf502014-07-02 00:53:48 +02002046 }
Petri Gynther10d4c672014-05-08 15:50:01 -07002047
Marcel Holtmanncb8d6592014-07-02 11:25:25 +02002048 if (id->driver_info & BTUSB_INTEL) {
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002049 hdev->setup = btusb_setup_intel;
Marcel Holtmanncb8d6592014-07-02 11:25:25 +02002050 hdev->set_bdaddr = btusb_set_bdaddr_intel;
2051 }
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07002052
Amitkumar Karwarae8df492014-07-18 14:47:06 -07002053 if (id->driver_info & BTUSB_MARVELL)
2054 hdev->set_bdaddr = btusb_set_bdaddr_marvell;
2055
Marcel Holtmann40df7832014-07-06 13:29:58 +02002056 if (id->driver_info & BTUSB_INTEL_BOOT)
2057 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
2058
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002059 /* Interface numbers are hardcoded in the specification */
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02002060 data->isoc = usb_ifnum_to_if(data->udev, 1);
2061
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002062 if (!reset)
Szymon Janca6c511c2012-05-23 12:35:46 +02002063 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002064
2065 if (force_scofix || id->driver_info & BTUSB_WRONG_SCO_MTU) {
2066 if (!disable_scofix)
2067 set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
2068 }
2069
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02002070 if (id->driver_info & BTUSB_BROKEN_ISOC)
2071 data->isoc = NULL;
2072
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002073 if (id->driver_info & BTUSB_DIGIANSWER) {
2074 data->cmdreq_type = USB_TYPE_VENDOR;
Szymon Janca6c511c2012-05-23 12:35:46 +02002075 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002076 }
2077
2078 if (id->driver_info & BTUSB_CSR) {
2079 struct usb_device *udev = data->udev;
Marcel Holtmann81cac642014-01-03 03:02:36 -08002080 u16 bcdDevice = le16_to_cpu(udev->descriptor.bcdDevice);
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002081
2082 /* Old firmware would otherwise execute USB reset */
Marcel Holtmann81cac642014-01-03 03:02:36 -08002083 if (bcdDevice < 0x117)
Szymon Janca6c511c2012-05-23 12:35:46 +02002084 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmann81cac642014-01-03 03:02:36 -08002085
2086 /* Fake CSR devices with broken commands */
2087 if (bcdDevice <= 0x100)
2088 hdev->setup = btusb_setup_csr;
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002089 }
2090
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002091 if (id->driver_info & BTUSB_SNIFFER) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02002092 struct usb_device *udev = data->udev;
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002093
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01002094 /* New sniffer firmware has crippled HCI interface */
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002095 if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
2096 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
2097 }
2098
Marcel Holtmann3a5ef202014-07-06 14:53:54 +02002099 if (id->driver_info & BTUSB_INTEL_BOOT) {
2100 /* A bug in the bootloader causes that interrupt interface is
2101 * only enabled after receiving SetInterface(0, AltSetting=0).
2102 */
2103 err = usb_set_interface(data->udev, 0, 0);
2104 if (err < 0) {
2105 BT_ERR("failed to set interface 0, alt 0 %d", err);
2106 hci_free_dev(hdev);
2107 return err;
2108 }
2109 }
2110
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02002111 if (data->isoc) {
2112 err = usb_driver_claim_interface(&btusb_driver,
Marcel Holtmann89e75332014-09-16 04:44:50 +02002113 data->isoc, data);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02002114 if (err < 0) {
2115 hci_free_dev(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02002116 return err;
2117 }
2118 }
2119
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002120 err = hci_register_dev(hdev);
2121 if (err < 0) {
2122 hci_free_dev(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002123 return err;
2124 }
2125
2126 usb_set_intfdata(intf, data);
2127
2128 return 0;
2129}
2130
2131static void btusb_disconnect(struct usb_interface *intf)
2132{
2133 struct btusb_data *data = usb_get_intfdata(intf);
2134 struct hci_dev *hdev;
2135
2136 BT_DBG("intf %p", intf);
2137
2138 if (!data)
2139 return;
2140
2141 hdev = data->hdev;
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02002142 usb_set_intfdata(data->intf, NULL);
2143
2144 if (data->isoc)
2145 usb_set_intfdata(data->isoc, NULL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002146
2147 hci_unregister_dev(hdev);
2148
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02002149 if (intf == data->isoc)
2150 usb_driver_release_interface(&btusb_driver, data->intf);
2151 else if (data->isoc)
2152 usb_driver_release_interface(&btusb_driver, data->isoc);
2153
Marcel Holtmann803b5832014-09-16 08:00:29 +02002154 btusb_free_frags(data);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002155 hci_free_dev(hdev);
2156}
2157
Oliver Neukum7bee5492009-08-24 23:44:59 +02002158#ifdef CONFIG_PM
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002159static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
2160{
2161 struct btusb_data *data = usb_get_intfdata(intf);
2162
2163 BT_DBG("intf %p", intf);
2164
2165 if (data->suspend_count++)
2166 return 0;
2167
Oliver Neukum7bee5492009-08-24 23:44:59 +02002168 spin_lock_irq(&data->txlock);
Alan Stern5b1b0b82011-08-19 23:49:48 +02002169 if (!(PMSG_IS_AUTO(message) && data->tx_in_flight)) {
Oliver Neukum7bee5492009-08-24 23:44:59 +02002170 set_bit(BTUSB_SUSPENDING, &data->flags);
2171 spin_unlock_irq(&data->txlock);
2172 } else {
2173 spin_unlock_irq(&data->txlock);
2174 data->suspend_count--;
2175 return -EBUSY;
2176 }
2177
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002178 cancel_work_sync(&data->work);
2179
Oliver Neukum7bee5492009-08-24 23:44:59 +02002180 btusb_stop_traffic(data);
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002181 usb_kill_anchored_urbs(&data->tx_anchor);
2182
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002183 return 0;
2184}
2185
Oliver Neukum7bee5492009-08-24 23:44:59 +02002186static void play_deferred(struct btusb_data *data)
2187{
2188 struct urb *urb;
2189 int err;
2190
2191 while ((urb = usb_get_from_anchor(&data->deferred))) {
2192 err = usb_submit_urb(urb, GFP_ATOMIC);
2193 if (err < 0)
2194 break;
2195
2196 data->tx_in_flight++;
2197 }
2198 usb_scuttle_anchored_urbs(&data->deferred);
2199}
2200
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002201static int btusb_resume(struct usb_interface *intf)
2202{
2203 struct btusb_data *data = usb_get_intfdata(intf);
2204 struct hci_dev *hdev = data->hdev;
Oliver Neukum7bee5492009-08-24 23:44:59 +02002205 int err = 0;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002206
2207 BT_DBG("intf %p", intf);
2208
2209 if (--data->suspend_count)
2210 return 0;
2211
2212 if (!test_bit(HCI_RUNNING, &hdev->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +02002213 goto done;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002214
2215 if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
2216 err = btusb_submit_intr_urb(hdev, GFP_NOIO);
2217 if (err < 0) {
2218 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02002219 goto failed;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002220 }
2221 }
2222
2223 if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) {
Marcel Holtmann43c2e572009-02-04 17:41:38 +01002224 err = btusb_submit_bulk_urb(hdev, GFP_NOIO);
2225 if (err < 0) {
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002226 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02002227 goto failed;
2228 }
2229
2230 btusb_submit_bulk_urb(hdev, GFP_NOIO);
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002231 }
2232
2233 if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
2234 if (btusb_submit_isoc_urb(hdev, GFP_NOIO) < 0)
2235 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
2236 else
2237 btusb_submit_isoc_urb(hdev, GFP_NOIO);
2238 }
2239
Oliver Neukum7bee5492009-08-24 23:44:59 +02002240 spin_lock_irq(&data->txlock);
2241 play_deferred(data);
2242 clear_bit(BTUSB_SUSPENDING, &data->flags);
2243 spin_unlock_irq(&data->txlock);
2244 schedule_work(&data->work);
2245
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002246 return 0;
Oliver Neukum7bee5492009-08-24 23:44:59 +02002247
2248failed:
2249 usb_scuttle_anchored_urbs(&data->deferred);
2250done:
2251 spin_lock_irq(&data->txlock);
2252 clear_bit(BTUSB_SUSPENDING, &data->flags);
2253 spin_unlock_irq(&data->txlock);
2254
2255 return err;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002256}
Oliver Neukum7bee5492009-08-24 23:44:59 +02002257#endif
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002258
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002259static struct usb_driver btusb_driver = {
2260 .name = "btusb",
2261 .probe = btusb_probe,
2262 .disconnect = btusb_disconnect,
Oliver Neukum7bee5492009-08-24 23:44:59 +02002263#ifdef CONFIG_PM
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01002264 .suspend = btusb_suspend,
2265 .resume = btusb_resume,
Oliver Neukum7bee5492009-08-24 23:44:59 +02002266#endif
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002267 .id_table = btusb_table,
Oliver Neukum7bee5492009-08-24 23:44:59 +02002268 .supports_autosuspend = 1,
Sarah Sharpe1f12eb2012-04-23 10:08:51 -07002269 .disable_hub_initiated_lpm = 1,
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002270};
2271
Greg Kroah-Hartman93f15082011-11-18 09:47:34 -08002272module_usb_driver(btusb_driver);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002273
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02002274module_param(disable_scofix, bool, 0644);
2275MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
2276
2277module_param(force_scofix, bool, 0644);
2278MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
2279
2280module_param(reset, bool, 0644);
2281MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
2282
Marcel Holtmann5e23b922007-10-20 14:12:34 +02002283MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
2284MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
2285MODULE_VERSION(VERSION);
2286MODULE_LICENSE("GPL");