blob: 71b4eb36b34d5fdda0be11b09ff3ffd8e55eac85 [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>
flintman9892c402015-09-23 06:14:10 -040026#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
flintman9892c402015-09-23 06:14:10 -040033static bool ignore_dga;
34static bool ignore_csr;
35static bool ignore_sniffer;
36static bool disable_scofix;
37static bool force_scofix;
Marcel Holtmann7a9d4022008-11-30 12:17:26 +010038
flintman9892c402015-09-23 06:14:10 -040039static bool reset = 1;
Marcel Holtmanncfeb4142008-08-07 22:26:56 +020040
41static struct usb_driver btusb_driver;
42
43#define BTUSB_IGNORE 0x01
Marcel Holtmann7a9d4022008-11-30 12:17:26 +010044#define BTUSB_DIGIANSWER 0x02
45#define BTUSB_CSR 0x04
46#define BTUSB_SNIFFER 0x08
47#define BTUSB_BCM92035 0x10
48#define BTUSB_BROKEN_ISOC 0x20
49#define BTUSB_WRONG_SCO_MTU 0x40
Steven.Li4770ac22011-07-01 14:02:36 +080050#define BTUSB_ATH3012 0x80
flintman9892c402015-09-23 06:14:10 -040051#define BTUSB_INTEL 0x100
Marcel Holtmann5e23b922007-10-20 14:12:34 +020052
53static struct usb_device_id btusb_table[] = {
54 /* Generic Bluetooth USB device */
55 { USB_DEVICE_INFO(0xe0, 0x01, 0x01) },
56
Henrik Rydberg403c9eb2012-08-25 19:28:06 +020057 /* Apple-specific (Broadcom) devices */
58 { USB_VENDOR_AND_INTERFACE_INFO(0x05ac, 0xff, 0x01, 0x01) },
59
Cho, Yu-Chen5ff17152013-06-04 21:40:26 +080060 /* MediaTek MT76x0E */
61 { USB_DEVICE(0x0e8d, 0x763f) },
62
Oliver Neukumb7103652011-09-21 11:41:45 +020063 /* Broadcom SoftSailing reporting vendor specific */
flintman9892c402015-09-23 06:14:10 -040064 { USB_DEVICE(0x0a5c, 0x21e1) },
Oliver Neukumb7103652011-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 Malleghemefb14972011-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 Kramer661a0d92011-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 Shevchenko7baba822014-02-18 18:26:19 +0200104 { USB_DEVICE(0x0489, 0xe042) },
105 { USB_DEVICE(0x04ca, 0x2003) },
Jeff Cook4a990f62012-11-09 16:39:48 -0700106 { USB_DEVICE(0x0b05, 0x17b5) },
Raphael Kubo da Costa76b87de2013-09-02 14:57:51 +0300107 { USB_DEVICE(0x0b05, 0x17cb) },
Wen-chien Jesse Sungd13431c2011-11-08 14:30:22 +0800108 { USB_DEVICE(0x413c, 0x8197) },
109
Steven Harms98514032012-04-13 14:45:55 -0400110 /* Foxconn - Hon Hai */
Gustavo Padovanec5bf542012-08-15 01:38:11 -0300111 { USB_VENDOR_AND_INTERFACE_INFO(0x0489, 0xff, 0x01, 0x01) },
Steven Harms98514032012-04-13 14:45:55 -0400112
Andy Shevchenko7baba822014-02-18 18:26:19 +0200113 /* Broadcom devices with vendor specific id */
Gustavo Padovan004251b2012-08-06 15:36:49 -0300114 { USB_VENDOR_AND_INTERFACE_INFO(0x0a5c, 0xff, 0x01, 0x01) },
115
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200116 { } /* Terminating entry */
117};
118
119MODULE_DEVICE_TABLE(usb, btusb_table);
120
121static struct usb_device_id blacklist_table[] = {
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200122 /* CSR BlueCore devices */
123 { USB_DEVICE(0x0a12, 0x0001), .driver_info = BTUSB_CSR },
124
125 /* Broadcom BCM2033 without firmware */
126 { USB_DEVICE(0x0a5c, 0x2033), .driver_info = BTUSB_IGNORE },
127
Bala Shanmugambe931122010-11-26 17:35:46 +0530128 /* Atheros 3011 with sflash firmware */
Andy Shevchenko7baba822014-02-18 18:26:19 +0200129 { USB_DEVICE(0x0489, 0xe027), .driver_info = BTUSB_IGNORE },
130 { USB_DEVICE(0x0489, 0xe03d), .driver_info = BTUSB_IGNORE },
José Adolfo Galdámez64c36312015-09-21 22:00:27 -0600131 { USB_DEVICE(0x04f2, 0xaff1), .driver_info = BTUSB_IGNORE },
Andy Shevchenko7baba822014-02-18 18:26:19 +0200132 { USB_DEVICE(0x0930, 0x0215), .driver_info = BTUSB_IGNORE },
Bala Shanmugambe931122010-11-26 17:35:46 +0530133 { USB_DEVICE(0x0cf3, 0x3002), .driver_info = BTUSB_IGNORE },
Marek Vasut7f801fc2012-06-08 14:32:50 +0200134 { USB_DEVICE(0x0cf3, 0xe019), .driver_info = BTUSB_IGNORE },
Andy Ross2a7bccc2011-05-09 16:11:16 -0700135 { USB_DEVICE(0x13d3, 0x3304), .driver_info = BTUSB_IGNORE },
Bala Shanmugambe931122010-11-26 17:35:46 +0530136
Cho, Yu-Chen509e7862011-01-26 17:10:59 +0800137 /* Atheros AR9285 Malbec with sflash firmware */
138 { USB_DEVICE(0x03f0, 0x311d), .driver_info = BTUSB_IGNORE },
139
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530140 /* Atheros 3012 with sflash firmware */
Andy Shevchenko7baba822014-02-18 18:26:19 +0200141 { USB_DEVICE(0x0489, 0xe04d), .driver_info = BTUSB_ATH3012 },
142 { USB_DEVICE(0x0489, 0xe04e), .driver_info = BTUSB_ATH3012 },
143 { USB_DEVICE(0x0489, 0xe056), .driver_info = BTUSB_ATH3012 },
144 { USB_DEVICE(0x0489, 0xe057), .driver_info = BTUSB_ATH3012 },
Anantha Krishnane908f8b2014-10-06 16:31:49 +0530145 { USB_DEVICE(0x0489, 0xe078), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko7baba822014-02-18 18:26:19 +0200146 { USB_DEVICE(0x04c5, 0x1330), .driver_info = BTUSB_ATH3012 },
Josh Boyerd83ff8c2013-02-19 11:54:16 -0500147 { USB_DEVICE(0x04ca, 0x3004), .driver_info = BTUSB_ATH3012 },
AceLan Kao55ed7d42012-03-28 10:25:36 +0800148 { USB_DEVICE(0x04ca, 0x3005), .driver_info = BTUSB_ATH3012 },
Daniel Schaal7a8c99e2012-12-29 11:14:34 +0100149 { USB_DEVICE(0x04ca, 0x3006), .driver_info = BTUSB_ATH3012 },
Sergio Cambrae98f05e2013-01-10 01:06:55 +0100150 { USB_DEVICE(0x04ca, 0x3008), .driver_info = BTUSB_ATH3012 },
Janne Heikkinen6091a5b2014-12-09 07:44:51 +0200151 { USB_DEVICE(0x04ca, 0x3010), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko7baba822014-02-18 18:26:19 +0200152 { USB_DEVICE(0x0930, 0x0219), .driver_info = BTUSB_ATH3012 },
Vincent Zwanenburgf03f73f2014-08-08 12:33:56 +0100153 { USB_DEVICE(0x0930, 0x0227), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko9ed78382014-02-18 18:26:20 +0200154 { USB_DEVICE(0x0b05, 0x17d0), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko7baba822014-02-18 18:26:19 +0200155 { USB_DEVICE(0x0cf3, 0x0036), .driver_info = BTUSB_ATH3012 },
156 { USB_DEVICE(0x0cf3, 0x3004), .driver_info = BTUSB_ATH3012 },
157 { USB_DEVICE(0x0cf3, 0x3008), .driver_info = BTUSB_ATH3012 },
158 { USB_DEVICE(0x0cf3, 0x311d), .driver_info = BTUSB_ATH3012 },
159 { USB_DEVICE(0x0cf3, 0x3121), .driver_info = BTUSB_ATH3012 },
160 { USB_DEVICE(0x0cf3, 0x817a), .driver_info = BTUSB_ATH3012 },
161 { USB_DEVICE(0x0cf3, 0xe003), .driver_info = BTUSB_ATH3012 },
AceLan Kaoac713112012-04-19 14:53:45 +0800162 { USB_DEVICE(0x0cf3, 0xe004), .driver_info = BTUSB_ATH3012 },
Peng Chen140deab2013-08-30 17:41:40 +0800163 { USB_DEVICE(0x0cf3, 0xe005), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko7baba822014-02-18 18:26:19 +0200164 { USB_DEVICE(0x13d3, 0x3362), .driver_info = BTUSB_ATH3012 },
165 { USB_DEVICE(0x13d3, 0x3375), .driver_info = BTUSB_ATH3012 },
AceLan Kao3b36e952012-12-11 11:41:20 +0800166 { USB_DEVICE(0x13d3, 0x3393), .driver_info = BTUSB_ATH3012 },
Sujith Manoharanfa6e6072013-07-15 09:29:03 +0530167 { USB_DEVICE(0x13d3, 0x3402), .driver_info = BTUSB_ATH3012 },
Dmitry Tunin23635762014-11-25 20:19:52 +0300168 { USB_DEVICE(0x13d3, 0x3408), .driver_info = BTUSB_ATH3012 },
José Adolfo Galdámez900469d2015-06-20 23:45:36 -0600169 { USB_DEVICE(0x13d3, 0x3423), .driver_info = BTUSB_ATH3012 },
Anantha Krishnan1bd6a902014-07-08 19:25:08 +0530170 { USB_DEVICE(0x13d3, 0x3432), .driver_info = BTUSB_ATH3012 },
José Adolfo Galdámez9cc712e2015-10-21 21:52:13 -0600171 { USB_DEVICE(0x13d3, 0x3474), .driver_info = BTUSB_ATH3012 },
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530172
Cho, Yu-Chene9036e32011-02-15 10:20:07 +0800173 /* Atheros AR5BBU12 with sflash firmware */
174 { USB_DEVICE(0x0489, 0xe02c), .driver_info = BTUSB_IGNORE },
175
Michael Gruetznera1144ca2012-05-02 22:33:40 +0200176 /* Atheros AR5BBU12 with sflash firmware */
Yevgeniy Melnichuk1403b362012-08-07 19:48:10 +0530177 { USB_DEVICE(0x0489, 0xe036), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko7baba822014-02-18 18:26:19 +0200178 { USB_DEVICE(0x0489, 0xe03c), .driver_info = BTUSB_ATH3012 },
Michael Gruetznera1144ca2012-05-02 22:33:40 +0200179
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200180 /* Broadcom BCM2035 */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100181 { USB_DEVICE(0x0a5c, 0x2009), .driver_info = BTUSB_BCM92035 },
Andy Shevchenko7baba822014-02-18 18:26:19 +0200182 { USB_DEVICE(0x0a5c, 0x200a), .driver_info = BTUSB_WRONG_SCO_MTU },
183 { USB_DEVICE(0x0a5c, 0x2035), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200184
185 /* Broadcom BCM2045 */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100186 { USB_DEVICE(0x0a5c, 0x2039), .driver_info = BTUSB_WRONG_SCO_MTU },
187 { USB_DEVICE(0x0a5c, 0x2101), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmannbdbef3d2008-09-23 00:16:35 +0200188
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200189 /* IBM/Lenovo ThinkPad with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100190 { USB_DEVICE(0x0a5c, 0x201e), .driver_info = BTUSB_WRONG_SCO_MTU },
191 { USB_DEVICE(0x0a5c, 0x2110), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200192
193 /* HP laptop with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100194 { USB_DEVICE(0x03f0, 0x171d), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200195
196 /* Dell laptop with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100197 { USB_DEVICE(0x413c, 0x8126), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200198
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100199 /* Dell Wireless 370 and 410 devices */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100200 { USB_DEVICE(0x413c, 0x8152), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100201 { USB_DEVICE(0x413c, 0x8156), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200202
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100203 /* Belkin F8T012 and F8T013 devices */
204 { USB_DEVICE(0x050d, 0x0012), .driver_info = BTUSB_WRONG_SCO_MTU },
205 { USB_DEVICE(0x050d, 0x0013), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200206
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100207 /* Asus WL-BTD202 device */
208 { USB_DEVICE(0x0b05, 0x1715), .driver_info = BTUSB_WRONG_SCO_MTU },
209
210 /* Kensington Bluetooth USB adapter */
211 { USB_DEVICE(0x047d, 0x105e), .driver_info = BTUSB_WRONG_SCO_MTU },
212
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200213 /* RTX Telecom based adapters with buggy SCO support */
214 { USB_DEVICE(0x0400, 0x0807), .driver_info = BTUSB_BROKEN_ISOC },
215 { USB_DEVICE(0x0400, 0x080a), .driver_info = BTUSB_BROKEN_ISOC },
216
217 /* CONWISE Technology based adapters with buggy SCO support */
218 { USB_DEVICE(0x0e5e, 0x6622), .driver_info = BTUSB_BROKEN_ISOC },
219
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200220 /* Digianswer devices */
221 { USB_DEVICE(0x08fd, 0x0001), .driver_info = BTUSB_DIGIANSWER },
222 { USB_DEVICE(0x08fd, 0x0002), .driver_info = BTUSB_IGNORE },
223
224 /* CSR BlueCore Bluetooth Sniffer */
225 { USB_DEVICE(0x0a12, 0x0002), .driver_info = BTUSB_SNIFFER },
226
227 /* Frontline ComProbe Bluetooth Sniffer */
228 { USB_DEVICE(0x16d3, 0x0002), .driver_info = BTUSB_SNIFFER },
229
flintman9892c402015-09-23 06:14:10 -0400230 /* Intel Bluetooth device */
231 { USB_DEVICE(0x8087, 0x07dc), .driver_info = BTUSB_INTEL },
232
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200233 { } /* Terminating entry */
234};
235
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200236#define BTUSB_MAX_ISOC_FRAMES 10
237
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200238#define BTUSB_INTR_RUNNING 0
239#define BTUSB_BULK_RUNNING 1
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200240#define BTUSB_ISOC_RUNNING 2
Oliver Neukum7bee5492009-08-24 23:44:59 +0200241#define BTUSB_SUSPENDING 3
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -0300242#define BTUSB_DID_ISO_RESUME 4
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200243
244struct btusb_data {
245 struct hci_dev *hdev;
246 struct usb_device *udev;
Marcel Holtmann5fbcd262008-09-23 00:16:36 +0200247 struct usb_interface *intf;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200248 struct usb_interface *isoc;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200249
250 spinlock_t lock;
251
252 unsigned long flags;
253
254 struct work_struct work;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200255 struct work_struct waker;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200256
257 struct usb_anchor tx_anchor;
258 struct usb_anchor intr_anchor;
259 struct usb_anchor bulk_anchor;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200260 struct usb_anchor isoc_anchor;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200261 struct usb_anchor deferred;
262 int tx_in_flight;
263 spinlock_t txlock;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200264
265 struct usb_endpoint_descriptor *intr_ep;
266 struct usb_endpoint_descriptor *bulk_tx_ep;
267 struct usb_endpoint_descriptor *bulk_rx_ep;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200268 struct usb_endpoint_descriptor *isoc_tx_ep;
269 struct usb_endpoint_descriptor *isoc_rx_ep;
270
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100271 __u8 cmdreq_type;
272
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100273 unsigned int sco_num;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200274 int isoc_altsetting;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +0100275 int suspend_count;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200276};
277
Oliver Neukum7bee5492009-08-24 23:44:59 +0200278static int inc_tx(struct btusb_data *data)
279{
280 unsigned long flags;
281 int rv;
282
283 spin_lock_irqsave(&data->txlock, flags);
284 rv = test_bit(BTUSB_SUSPENDING, &data->flags);
285 if (!rv)
286 data->tx_in_flight++;
287 spin_unlock_irqrestore(&data->txlock, flags);
288
289 return rv;
290}
291
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200292static void btusb_intr_complete(struct urb *urb)
293{
294 struct hci_dev *hdev = urb->context;
flintman9892c402015-09-23 06:14:10 -0400295 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200296 int err;
297
298 BT_DBG("%s urb %p status %d count %d", hdev->name,
299 urb, urb->status, urb->actual_length);
300
301 if (!test_bit(HCI_RUNNING, &hdev->flags))
302 return;
303
304 if (urb->status == 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200305 hdev->stat.byte_rx += urb->actual_length;
306
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200307 if (hci_recv_fragment(hdev, HCI_EVENT_PKT,
308 urb->transfer_buffer,
309 urb->actual_length) < 0) {
310 BT_ERR("%s corrupted event packet", hdev->name);
311 hdev->stat.err_rx++;
312 }
313 }
314
315 if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
316 return;
317
Oliver Neukum7bee5492009-08-24 23:44:59 +0200318 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200319 usb_anchor_urb(urb, &data->intr_anchor);
320
321 err = usb_submit_urb(urb, GFP_ATOMIC);
322 if (err < 0) {
flintman9892c402015-09-23 06:14:10 -0400323 /* -EPERM: urb is being killed;
324 * -ENODEV: device got disconnected */
325 if (err != -EPERM && err != -ENODEV)
Stefan Seyfried61faddf2010-11-30 21:49:08 +0100326 BT_ERR("%s urb %p failed to resubmit (%d)",
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200327 hdev->name, urb, -err);
328 usb_unanchor_urb(urb);
329 }
330}
331
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100332static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200333{
flintman9892c402015-09-23 06:14:10 -0400334 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200335 struct urb *urb;
336 unsigned char *buf;
337 unsigned int pipe;
338 int err, size;
339
340 BT_DBG("%s", hdev->name);
341
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200342 if (!data->intr_ep)
343 return -ENODEV;
344
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100345 urb = usb_alloc_urb(0, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200346 if (!urb)
347 return -ENOMEM;
348
349 size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
350
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100351 buf = kmalloc(size, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200352 if (!buf) {
353 usb_free_urb(urb);
354 return -ENOMEM;
355 }
356
357 pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
358
359 usb_fill_int_urb(urb, data->udev, pipe, buf, size,
360 btusb_intr_complete, hdev,
361 data->intr_ep->bInterval);
362
363 urb->transfer_flags |= URB_FREE_BUFFER;
364
365 usb_anchor_urb(urb, &data->intr_anchor);
366
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100367 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200368 if (err < 0) {
flintman9892c402015-09-23 06:14:10 -0400369 if (err != -EPERM && err != -ENODEV)
370 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200371 hdev->name, urb, -err);
372 usb_unanchor_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200373 }
374
375 usb_free_urb(urb);
376
377 return err;
378}
379
380static void btusb_bulk_complete(struct urb *urb)
381{
382 struct hci_dev *hdev = urb->context;
flintman9892c402015-09-23 06:14:10 -0400383 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200384 int err;
385
386 BT_DBG("%s urb %p status %d count %d", hdev->name,
387 urb, urb->status, urb->actual_length);
388
389 if (!test_bit(HCI_RUNNING, &hdev->flags))
390 return;
391
392 if (urb->status == 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200393 hdev->stat.byte_rx += urb->actual_length;
394
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200395 if (hci_recv_fragment(hdev, HCI_ACLDATA_PKT,
396 urb->transfer_buffer,
397 urb->actual_length) < 0) {
398 BT_ERR("%s corrupted ACL packet", hdev->name);
399 hdev->stat.err_rx++;
400 }
401 }
402
403 if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
404 return;
405
406 usb_anchor_urb(urb, &data->bulk_anchor);
Oliver Neukum652fd782009-12-16 19:23:43 +0100407 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200408
409 err = usb_submit_urb(urb, GFP_ATOMIC);
410 if (err < 0) {
flintman9892c402015-09-23 06:14:10 -0400411 /* -EPERM: urb is being killed;
412 * -ENODEV: device got disconnected */
413 if (err != -EPERM && err != -ENODEV)
Stefan Seyfried61faddf2010-11-30 21:49:08 +0100414 BT_ERR("%s urb %p failed to resubmit (%d)",
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200415 hdev->name, urb, -err);
416 usb_unanchor_urb(urb);
417 }
418}
419
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100420static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200421{
flintman9892c402015-09-23 06:14:10 -0400422 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200423 struct urb *urb;
424 unsigned char *buf;
425 unsigned int pipe;
Vikram Kandukuri290ba202009-07-02 14:31:59 +0530426 int err, size = HCI_MAX_FRAME_SIZE;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200427
428 BT_DBG("%s", hdev->name);
429
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200430 if (!data->bulk_rx_ep)
431 return -ENODEV;
432
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100433 urb = usb_alloc_urb(0, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200434 if (!urb)
435 return -ENOMEM;
436
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100437 buf = kmalloc(size, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200438 if (!buf) {
439 usb_free_urb(urb);
440 return -ENOMEM;
441 }
442
443 pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
444
445 usb_fill_bulk_urb(urb, data->udev, pipe,
446 buf, size, btusb_bulk_complete, hdev);
447
448 urb->transfer_flags |= URB_FREE_BUFFER;
449
Oliver Neukum7bee5492009-08-24 23:44:59 +0200450 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200451 usb_anchor_urb(urb, &data->bulk_anchor);
452
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100453 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200454 if (err < 0) {
flintman9892c402015-09-23 06:14:10 -0400455 if (err != -EPERM && err != -ENODEV)
456 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200457 hdev->name, urb, -err);
458 usb_unanchor_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200459 }
460
461 usb_free_urb(urb);
462
463 return err;
464}
465
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200466static void btusb_isoc_complete(struct urb *urb)
467{
468 struct hci_dev *hdev = urb->context;
flintman9892c402015-09-23 06:14:10 -0400469 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200470 int i, err;
471
472 BT_DBG("%s urb %p status %d count %d", hdev->name,
473 urb, urb->status, urb->actual_length);
474
475 if (!test_bit(HCI_RUNNING, &hdev->flags))
476 return;
477
478 if (urb->status == 0) {
479 for (i = 0; i < urb->number_of_packets; i++) {
480 unsigned int offset = urb->iso_frame_desc[i].offset;
481 unsigned int length = urb->iso_frame_desc[i].actual_length;
482
483 if (urb->iso_frame_desc[i].status)
484 continue;
485
486 hdev->stat.byte_rx += length;
487
488 if (hci_recv_fragment(hdev, HCI_SCODATA_PKT,
489 urb->transfer_buffer + offset,
490 length) < 0) {
491 BT_ERR("%s corrupted SCO packet", hdev->name);
492 hdev->stat.err_rx++;
493 }
494 }
495 }
496
497 if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags))
498 return;
499
500 usb_anchor_urb(urb, &data->isoc_anchor);
501
502 err = usb_submit_urb(urb, GFP_ATOMIC);
503 if (err < 0) {
flintman9892c402015-09-23 06:14:10 -0400504 /* -EPERM: urb is being killed;
505 * -ENODEV: device got disconnected */
506 if (err != -EPERM && err != -ENODEV)
Stefan Seyfried61faddf2010-11-30 21:49:08 +0100507 BT_ERR("%s urb %p failed to resubmit (%d)",
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200508 hdev->name, urb, -err);
509 usb_unanchor_urb(urb);
510 }
511}
512
flintman9892c402015-09-23 06:14:10 -0400513static inline void __fill_isoc_descriptor(struct urb *urb, int len, int mtu)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200514{
515 int i, offset = 0;
516
517 BT_DBG("len %d mtu %d", len, mtu);
518
519 for (i = 0; i < BTUSB_MAX_ISOC_FRAMES && len >= mtu;
520 i++, offset += mtu, len -= mtu) {
521 urb->iso_frame_desc[i].offset = offset;
522 urb->iso_frame_desc[i].length = mtu;
523 }
524
525 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
526 urb->iso_frame_desc[i].offset = offset;
527 urb->iso_frame_desc[i].length = len;
528 i++;
529 }
530
531 urb->number_of_packets = i;
532}
533
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100534static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200535{
flintman9892c402015-09-23 06:14:10 -0400536 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200537 struct urb *urb;
538 unsigned char *buf;
539 unsigned int pipe;
540 int err, size;
541
542 BT_DBG("%s", hdev->name);
543
544 if (!data->isoc_rx_ep)
545 return -ENODEV;
546
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100547 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200548 if (!urb)
549 return -ENOMEM;
550
551 size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) *
552 BTUSB_MAX_ISOC_FRAMES;
553
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100554 buf = kmalloc(size, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200555 if (!buf) {
556 usb_free_urb(urb);
557 return -ENOMEM;
558 }
559
560 pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress);
561
flintman9892c402015-09-23 06:14:10 -0400562 usb_fill_int_urb(urb, data->udev, pipe, buf, size, btusb_isoc_complete,
563 hdev, data->isoc_rx_ep->bInterval);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200564
565 urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200566
567 __fill_isoc_descriptor(urb, size,
568 le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize));
569
570 usb_anchor_urb(urb, &data->isoc_anchor);
571
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100572 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200573 if (err < 0) {
flintman9892c402015-09-23 06:14:10 -0400574 if (err != -EPERM && err != -ENODEV)
575 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200576 hdev->name, urb, -err);
577 usb_unanchor_urb(urb);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200578 }
579
580 usb_free_urb(urb);
581
582 return err;
583}
584
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200585static void btusb_tx_complete(struct urb *urb)
586{
587 struct sk_buff *skb = urb->context;
588 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
flintman9892c402015-09-23 06:14:10 -0400589 struct btusb_data *data = hci_get_drvdata(hdev);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200590
591 BT_DBG("%s urb %p status %d count %d", hdev->name,
592 urb, urb->status, urb->actual_length);
593
594 if (!test_bit(HCI_RUNNING, &hdev->flags))
595 goto done;
596
597 if (!urb->status)
598 hdev->stat.byte_tx += urb->transfer_buffer_length;
599 else
600 hdev->stat.err_tx++;
601
602done:
603 spin_lock(&data->txlock);
604 data->tx_in_flight--;
605 spin_unlock(&data->txlock);
606
607 kfree(urb->setup_packet);
608
609 kfree_skb(skb);
610}
611
612static void btusb_isoc_tx_complete(struct urb *urb)
613{
614 struct sk_buff *skb = urb->context;
615 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200616
617 BT_DBG("%s urb %p status %d count %d", hdev->name,
618 urb, urb->status, urb->actual_length);
619
620 if (!test_bit(HCI_RUNNING, &hdev->flags))
621 goto done;
622
623 if (!urb->status)
624 hdev->stat.byte_tx += urb->transfer_buffer_length;
625 else
626 hdev->stat.err_tx++;
627
628done:
629 kfree(urb->setup_packet);
630
631 kfree_skb(skb);
632}
633
634static int btusb_open(struct hci_dev *hdev)
635{
flintman9892c402015-09-23 06:14:10 -0400636 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200637 int err;
638
639 BT_DBG("%s", hdev->name);
640
Oliver Neukum7bee5492009-08-24 23:44:59 +0200641 err = usb_autopm_get_interface(data->intf);
642 if (err < 0)
643 return err;
644
645 data->intf->needs_remote_wakeup = 1;
646
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200647 if (test_and_set_bit(HCI_RUNNING, &hdev->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +0200648 goto done;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200649
650 if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +0200651 goto done;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200652
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100653 err = btusb_submit_intr_urb(hdev, GFP_KERNEL);
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100654 if (err < 0)
655 goto failed;
656
657 err = btusb_submit_bulk_urb(hdev, GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200658 if (err < 0) {
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100659 usb_kill_anchored_urbs(&data->intr_anchor);
660 goto failed;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200661 }
662
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100663 set_bit(BTUSB_BULK_RUNNING, &data->flags);
664 btusb_submit_bulk_urb(hdev, GFP_KERNEL);
665
Oliver Neukum7bee5492009-08-24 23:44:59 +0200666done:
667 usb_autopm_put_interface(data->intf);
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100668 return 0;
669
670failed:
671 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
672 clear_bit(HCI_RUNNING, &hdev->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200673 usb_autopm_put_interface(data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200674 return err;
675}
676
Oliver Neukum7bee5492009-08-24 23:44:59 +0200677static void btusb_stop_traffic(struct btusb_data *data)
678{
679 usb_kill_anchored_urbs(&data->intr_anchor);
680 usb_kill_anchored_urbs(&data->bulk_anchor);
681 usb_kill_anchored_urbs(&data->isoc_anchor);
682}
683
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200684static int btusb_close(struct hci_dev *hdev)
685{
flintman9892c402015-09-23 06:14:10 -0400686 struct btusb_data *data = hci_get_drvdata(hdev);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200687 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200688
689 BT_DBG("%s", hdev->name);
690
691 if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
692 return 0;
693
Marcel Holtmanne8c3c3d2008-09-23 00:16:36 +0200694 cancel_work_sync(&data->work);
Linus Torvalds404291a2009-11-11 13:32:29 -0800695 cancel_work_sync(&data->waker);
Marcel Holtmanne8c3c3d2008-09-23 00:16:36 +0200696
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200697 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200698 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200699 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200700
701 btusb_stop_traffic(data);
702 err = usb_autopm_get_interface(data->intf);
703 if (err < 0)
Oliver Neukum7b8e2c12009-11-13 14:26:23 +0100704 goto failed;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200705
706 data->intf->needs_remote_wakeup = 0;
707 usb_autopm_put_interface(data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200708
Oliver Neukum7b8e2c12009-11-13 14:26:23 +0100709failed:
710 usb_scuttle_anchored_urbs(&data->deferred);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200711 return 0;
712}
713
714static int btusb_flush(struct hci_dev *hdev)
715{
flintman9892c402015-09-23 06:14:10 -0400716 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200717
718 BT_DBG("%s", hdev->name);
719
720 usb_kill_anchored_urbs(&data->tx_anchor);
721
722 return 0;
723}
724
725static int btusb_send_frame(struct sk_buff *skb)
726{
727 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
flintman9892c402015-09-23 06:14:10 -0400728 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200729 struct usb_ctrlrequest *dr;
730 struct urb *urb;
731 unsigned int pipe;
732 int err;
733
734 BT_DBG("%s", hdev->name);
735
736 if (!test_bit(HCI_RUNNING, &hdev->flags))
737 return -EBUSY;
738
739 switch (bt_cb(skb)->pkt_type) {
740 case HCI_COMMAND_PKT:
741 urb = usb_alloc_urb(0, GFP_ATOMIC);
742 if (!urb)
743 return -ENOMEM;
744
745 dr = kmalloc(sizeof(*dr), GFP_ATOMIC);
746 if (!dr) {
747 usb_free_urb(urb);
748 return -ENOMEM;
749 }
750
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100751 dr->bRequestType = data->cmdreq_type;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200752 dr->bRequest = 0;
753 dr->wIndex = 0;
754 dr->wValue = 0;
755 dr->wLength = __cpu_to_le16(skb->len);
756
757 pipe = usb_sndctrlpipe(data->udev, 0x00);
758
759 usb_fill_control_urb(urb, data->udev, pipe, (void *) dr,
760 skb->data, skb->len, btusb_tx_complete, skb);
761
762 hdev->stat.cmd_tx++;
763 break;
764
765 case HCI_ACLDATA_PKT:
flintman9892c402015-09-23 06:14:10 -0400766 if (!data->bulk_tx_ep)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200767 return -ENODEV;
768
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200769 urb = usb_alloc_urb(0, GFP_ATOMIC);
770 if (!urb)
771 return -ENOMEM;
772
773 pipe = usb_sndbulkpipe(data->udev,
774 data->bulk_tx_ep->bEndpointAddress);
775
776 usb_fill_bulk_urb(urb, data->udev, pipe,
777 skb->data, skb->len, btusb_tx_complete, skb);
778
779 hdev->stat.acl_tx++;
780 break;
781
782 case HCI_SCODATA_PKT:
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200783 if (!data->isoc_tx_ep || hdev->conn_hash.sco_num < 1)
784 return -ENODEV;
785
786 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_ATOMIC);
787 if (!urb)
788 return -ENOMEM;
789
790 pipe = usb_sndisocpipe(data->udev,
791 data->isoc_tx_ep->bEndpointAddress);
792
Gustavo F. Padovan03c2d0e2011-02-14 18:53:43 -0300793 usb_fill_int_urb(urb, data->udev, pipe,
794 skb->data, skb->len, btusb_isoc_tx_complete,
795 skb, data->isoc_tx_ep->bInterval);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200796
797 urb->transfer_flags = URB_ISO_ASAP;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200798
799 __fill_isoc_descriptor(urb, skb->len,
800 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
801
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200802 hdev->stat.sco_tx++;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200803 goto skip_waking;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200804
805 default:
806 return -EILSEQ;
807 }
808
Oliver Neukum7bee5492009-08-24 23:44:59 +0200809 err = inc_tx(data);
810 if (err) {
811 usb_anchor_urb(urb, &data->deferred);
812 schedule_work(&data->waker);
813 err = 0;
814 goto done;
815 }
816
817skip_waking:
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200818 usb_anchor_urb(urb, &data->tx_anchor);
819
820 err = usb_submit_urb(urb, GFP_ATOMIC);
821 if (err < 0) {
flintman9892c402015-09-23 06:14:10 -0400822 if (err != -EPERM && err != -ENODEV)
823 BT_ERR("%s urb %p submission failed (%d)",
824 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200825 kfree(urb->setup_packet);
826 usb_unanchor_urb(urb);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200827 } else {
828 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200829 }
830
Oliver Neukum7bee5492009-08-24 23:44:59 +0200831done:
flintman9892c402015-09-23 06:14:10 -0400832 usb_free_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200833 return err;
834}
835
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200836static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
837{
flintman9892c402015-09-23 06:14:10 -0400838 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200839
840 BT_DBG("%s evt %d", hdev->name, evt);
841
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100842 if (hdev->conn_hash.sco_num != data->sco_num) {
843 data->sco_num = hdev->conn_hash.sco_num;
844 schedule_work(&data->work);
Marcel Holtmanna780efa2008-11-30 12:17:12 +0100845 }
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200846}
847
flintman9892c402015-09-23 06:14:10 -0400848static inline int __set_isoc_interface(struct hci_dev *hdev, int altsetting)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200849{
flintman9892c402015-09-23 06:14:10 -0400850 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200851 struct usb_interface *intf = data->isoc;
852 struct usb_endpoint_descriptor *ep_desc;
853 int i, err;
854
855 if (!data->isoc)
856 return -ENODEV;
857
858 err = usb_set_interface(data->udev, 1, altsetting);
859 if (err < 0) {
860 BT_ERR("%s setting interface failed (%d)", hdev->name, -err);
861 return err;
862 }
863
864 data->isoc_altsetting = altsetting;
865
866 data->isoc_tx_ep = NULL;
867 data->isoc_rx_ep = NULL;
868
869 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
870 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
871
872 if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) {
873 data->isoc_tx_ep = ep_desc;
874 continue;
875 }
876
877 if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) {
878 data->isoc_rx_ep = ep_desc;
879 continue;
880 }
881 }
882
883 if (!data->isoc_tx_ep || !data->isoc_rx_ep) {
884 BT_ERR("%s invalid SCO descriptors", hdev->name);
885 return -ENODEV;
886 }
887
888 return 0;
889}
890
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200891static void btusb_work(struct work_struct *work)
892{
893 struct btusb_data *data = container_of(work, struct btusb_data, work);
894 struct hci_dev *hdev = data->hdev;
flintman9892c402015-09-23 06:14:10 -0400895 int new_alts;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200896 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200897
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200898 if (hdev->conn_hash.sco_num > 0) {
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -0300899 if (!test_bit(BTUSB_DID_ISO_RESUME, &data->flags)) {
Oliver Neukum8efdd0c2011-02-11 13:00:06 +0100900 err = usb_autopm_get_interface(data->isoc ? data->isoc : data->intf);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200901 if (err < 0) {
902 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
903 usb_kill_anchored_urbs(&data->isoc_anchor);
904 return;
905 }
906
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -0300907 set_bit(BTUSB_DID_ISO_RESUME, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200908 }
flintman9892c402015-09-23 06:14:10 -0400909
910 if (hdev->voice_setting & 0x0020) {
911 static const int alts[3] = { 2, 4, 5 };
912 new_alts = alts[hdev->conn_hash.sco_num - 1];
913 } else {
914 new_alts = hdev->conn_hash.sco_num;
915 }
916
917 if (data->isoc_altsetting != new_alts) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200918 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
919 usb_kill_anchored_urbs(&data->isoc_anchor);
920
flintman9892c402015-09-23 06:14:10 -0400921 if (__set_isoc_interface(hdev, new_alts) < 0)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200922 return;
923 }
924
925 if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100926 if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200927 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
928 else
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100929 btusb_submit_isoc_urb(hdev, GFP_KERNEL);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200930 }
931 } else {
932 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
933 usb_kill_anchored_urbs(&data->isoc_anchor);
934
935 __set_isoc_interface(hdev, 0);
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -0300936 if (test_and_clear_bit(BTUSB_DID_ISO_RESUME, &data->flags))
Oliver Neukum8efdd0c2011-02-11 13:00:06 +0100937 usb_autopm_put_interface(data->isoc ? data->isoc : data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200938 }
939}
940
Oliver Neukum7bee5492009-08-24 23:44:59 +0200941static void btusb_waker(struct work_struct *work)
942{
943 struct btusb_data *data = container_of(work, struct btusb_data, waker);
944 int err;
945
946 err = usb_autopm_get_interface(data->intf);
947 if (err < 0)
948 return;
949
950 usb_autopm_put_interface(data->intf);
951}
952
flintman9892c402015-09-23 06:14:10 -0400953static int btusb_setup_bcm92035(struct hci_dev *hdev)
954{
955 struct sk_buff *skb;
956 u8 val = 0x00;
957
958 BT_DBG("%s", hdev->name);
959
960 skb = __hci_cmd_sync(hdev, 0xfc3b, 1, &val, HCI_INIT_TIMEOUT);
961 if (IS_ERR(skb))
962 BT_ERR("BCM92035 command failed (%ld)", -PTR_ERR(skb));
963 else
964 kfree_skb(skb);
965
966 return 0;
967}
968
969struct intel_version {
970 u8 status;
971 u8 hw_platform;
972 u8 hw_variant;
973 u8 hw_revision;
974 u8 fw_variant;
975 u8 fw_revision;
976 u8 fw_build_num;
977 u8 fw_build_ww;
978 u8 fw_build_yy;
979 u8 fw_patch_num;
980} __packed;
981
982static const struct firmware *btusb_setup_intel_get_fw(struct hci_dev *hdev,
983 struct intel_version *ver)
984{
985 const struct firmware *fw;
986 char fwname[64];
987 int ret;
988
989 snprintf(fwname, sizeof(fwname),
990 "intel/ibt-hw-%x.%x.%x-fw-%x.%x.%x.%x.%x.bseq",
991 ver->hw_platform, ver->hw_variant, ver->hw_revision,
992 ver->fw_variant, ver->fw_revision, ver->fw_build_num,
993 ver->fw_build_ww, ver->fw_build_yy);
994
995 ret = request_firmware(&fw, fwname, &hdev->dev);
996 if (ret < 0) {
997 if (ret == -EINVAL) {
998 BT_ERR("%s Intel firmware file request failed (%d)",
999 hdev->name, ret);
1000 return NULL;
1001 }
1002
1003 BT_ERR("%s failed to open Intel firmware file: %s(%d)",
1004 hdev->name, fwname, ret);
1005
1006 /* If the correct firmware patch file is not found, use the
1007 * default firmware patch file instead
1008 */
1009 snprintf(fwname, sizeof(fwname), "intel/ibt-hw-%x.%x.bseq",
1010 ver->hw_platform, ver->hw_variant);
1011 if (request_firmware(&fw, fwname, &hdev->dev) < 0) {
1012 BT_ERR("%s failed to open default Intel fw file: %s",
1013 hdev->name, fwname);
1014 return NULL;
1015 }
1016 }
1017
1018 BT_INFO("%s: Intel Bluetooth firmware file: %s", hdev->name, fwname);
1019
1020 return fw;
1021}
1022
1023static int btusb_setup_intel_patching(struct hci_dev *hdev,
1024 const struct firmware *fw,
1025 const u8 **fw_ptr, int *disable_patch)
1026{
1027 struct sk_buff *skb;
1028 struct hci_command_hdr *cmd;
1029 const u8 *cmd_param;
1030 struct hci_event_hdr *evt = NULL;
1031 const u8 *evt_param = NULL;
1032 int remain = fw->size - (*fw_ptr - fw->data);
1033
1034 /* The first byte indicates the types of the patch command or event.
1035 * 0x01 means HCI command and 0x02 is HCI event. If the first bytes
1036 * in the current firmware buffer doesn't start with 0x01 or
1037 * the size of remain buffer is smaller than HCI command header,
1038 * the firmware file is corrupted and it should stop the patching
1039 * process.
1040 */
1041 if (remain > HCI_COMMAND_HDR_SIZE && *fw_ptr[0] != 0x01) {
1042 BT_ERR("%s Intel fw corrupted: invalid cmd read", hdev->name);
1043 return -EINVAL;
1044 }
1045 (*fw_ptr)++;
1046 remain--;
1047
1048 cmd = (struct hci_command_hdr *)(*fw_ptr);
1049 *fw_ptr += sizeof(*cmd);
1050 remain -= sizeof(*cmd);
1051
1052 /* Ensure that the remain firmware data is long enough than the length
1053 * of command parameter. If not, the firmware file is corrupted.
1054 */
1055 if (remain < cmd->plen) {
1056 BT_ERR("%s Intel fw corrupted: invalid cmd len", hdev->name);
1057 return -EFAULT;
1058 }
1059
1060 /* If there is a command that loads a patch in the firmware
1061 * file, then enable the patch upon success, otherwise just
1062 * disable the manufacturer mode, for example patch activation
1063 * is not required when the default firmware patch file is used
1064 * because there are no patch data to load.
1065 */
1066 if (*disable_patch && le16_to_cpu(cmd->opcode) == 0xfc8e)
1067 *disable_patch = 0;
1068
1069 cmd_param = *fw_ptr;
1070 *fw_ptr += cmd->plen;
1071 remain -= cmd->plen;
1072
1073 /* This reads the expected events when the above command is sent to the
1074 * device. Some vendor commands expects more than one events, for
1075 * example command status event followed by vendor specific event.
1076 * For this case, it only keeps the last expected event. so the command
1077 * can be sent with __hci_cmd_sync_ev() which returns the sk_buff of
1078 * last expected event.
1079 */
1080 while (remain > HCI_EVENT_HDR_SIZE && *fw_ptr[0] == 0x02) {
1081 (*fw_ptr)++;
1082 remain--;
1083
1084 evt = (struct hci_event_hdr *)(*fw_ptr);
1085 *fw_ptr += sizeof(*evt);
1086 remain -= sizeof(*evt);
1087
1088 if (remain < evt->plen) {
1089 BT_ERR("%s Intel fw corrupted: invalid evt len",
1090 hdev->name);
1091 return -EFAULT;
1092 }
1093
1094 evt_param = *fw_ptr;
1095 *fw_ptr += evt->plen;
1096 remain -= evt->plen;
1097 }
1098
1099 /* Every HCI commands in the firmware file has its correspond event.
1100 * If event is not found or remain is smaller than zero, the firmware
1101 * file is corrupted.
1102 */
1103 if (!evt || !evt_param || remain < 0) {
1104 BT_ERR("%s Intel fw corrupted: invalid evt read", hdev->name);
1105 return -EFAULT;
1106 }
1107
1108 skb = __hci_cmd_sync_ev(hdev, le16_to_cpu(cmd->opcode), cmd->plen,
1109 cmd_param, evt->evt, HCI_INIT_TIMEOUT);
1110 if (IS_ERR(skb)) {
1111 BT_ERR("%s sending Intel patch command (0x%4.4x) failed (%ld)",
1112 hdev->name, cmd->opcode, PTR_ERR(skb));
1113 return PTR_ERR(skb);
1114 }
1115
1116 /* It ensures that the returned event matches the event data read from
1117 * the firmware file. At fist, it checks the length and then
1118 * the contents of the event.
1119 */
1120 if (skb->len != evt->plen) {
1121 BT_ERR("%s mismatch event length (opcode 0x%4.4x)", hdev->name,
1122 le16_to_cpu(cmd->opcode));
1123 kfree_skb(skb);
1124 return -EFAULT;
1125 }
1126
1127 if (memcmp(skb->data, evt_param, evt->plen)) {
1128 BT_ERR("%s mismatch event parameter (opcode 0x%4.4x)",
1129 hdev->name, le16_to_cpu(cmd->opcode));
1130 kfree_skb(skb);
1131 return -EFAULT;
1132 }
1133 kfree_skb(skb);
1134
1135 return 0;
1136}
1137
1138static int btusb_setup_intel(struct hci_dev *hdev)
1139{
1140 struct sk_buff *skb;
1141 const struct firmware *fw;
1142 const u8 *fw_ptr;
1143 int disable_patch;
1144 struct intel_version *ver;
1145
1146 const u8 mfg_enable[] = { 0x01, 0x00 };
1147 const u8 mfg_disable[] = { 0x00, 0x00 };
1148 const u8 mfg_reset_deactivate[] = { 0x00, 0x01 };
1149 const u8 mfg_reset_activate[] = { 0x00, 0x02 };
1150
1151 BT_DBG("%s", hdev->name);
1152
1153 /* The controller has a bug with the first HCI command sent to it
1154 * returning number of completed commands as zero. This would stall the
1155 * command processing in the Bluetooth core.
1156 *
1157 * As a workaround, send HCI Reset command first which will reset the
1158 * number of completed commands and allow normal command processing
1159 * from now on.
1160 */
1161 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
1162 if (IS_ERR(skb)) {
1163 BT_ERR("%s sending initial HCI reset command failed (%ld)",
1164 hdev->name, PTR_ERR(skb));
1165 return PTR_ERR(skb);
1166 }
1167 kfree_skb(skb);
1168
1169 /* Read Intel specific controller version first to allow selection of
1170 * which firmware file to load.
1171 *
1172 * The returned information are hardware variant and revision plus
1173 * firmware variant, revision and build number.
1174 */
1175 skb = __hci_cmd_sync(hdev, 0xfc05, 0, NULL, HCI_INIT_TIMEOUT);
1176 if (IS_ERR(skb)) {
1177 BT_ERR("%s reading Intel fw version command failed (%ld)",
1178 hdev->name, PTR_ERR(skb));
1179 return PTR_ERR(skb);
1180 }
1181
1182 if (skb->len != sizeof(*ver)) {
1183 BT_ERR("%s Intel version event length mismatch", hdev->name);
1184 kfree_skb(skb);
1185 return -EIO;
1186 }
1187
1188 ver = (struct intel_version *)skb->data;
1189 if (ver->status) {
1190 BT_ERR("%s Intel fw version event failed (%02x)", hdev->name,
1191 ver->status);
1192 kfree_skb(skb);
1193 return -bt_to_errno(ver->status);
1194 }
1195
1196 BT_INFO("%s: read Intel version: %02x%02x%02x%02x%02x%02x%02x%02x%02x",
1197 hdev->name, ver->hw_platform, ver->hw_variant,
1198 ver->hw_revision, ver->fw_variant, ver->fw_revision,
1199 ver->fw_build_num, ver->fw_build_ww, ver->fw_build_yy,
1200 ver->fw_patch_num);
1201
1202 /* fw_patch_num indicates the version of patch the device currently
1203 * have. If there is no patch data in the device, it is always 0x00.
1204 * So, if it is other than 0x00, no need to patch the deivce again.
1205 */
1206 if (ver->fw_patch_num) {
1207 BT_INFO("%s: Intel device is already patched. patch num: %02x",
1208 hdev->name, ver->fw_patch_num);
1209 kfree_skb(skb);
1210 return 0;
1211 }
1212
1213 /* Opens the firmware patch file based on the firmware version read
1214 * from the controller. If it fails to open the matching firmware
1215 * patch file, it tries to open the default firmware patch file.
1216 * If no patch file is found, allow the device to operate without
1217 * a patch.
1218 */
1219 fw = btusb_setup_intel_get_fw(hdev, ver);
1220 if (!fw) {
1221 kfree_skb(skb);
1222 return 0;
1223 }
1224 fw_ptr = fw->data;
1225
1226 /* This Intel specific command enables the manufacturer mode of the
1227 * controller.
1228 *
1229 * Only while this mode is enabled, the driver can download the
1230 * firmware patch data and configuration parameters.
1231 */
1232 skb = __hci_cmd_sync(hdev, 0xfc11, 2, mfg_enable, HCI_INIT_TIMEOUT);
1233 if (IS_ERR(skb)) {
1234 BT_ERR("%s entering Intel manufacturer mode failed (%ld)",
1235 hdev->name, PTR_ERR(skb));
1236 release_firmware(fw);
1237 return PTR_ERR(skb);
1238 }
1239
1240 if (skb->data[0]) {
1241 u8 evt_status = skb->data[0];
1242 BT_ERR("%s enable Intel manufacturer mode event failed (%02x)",
1243 hdev->name, evt_status);
1244 kfree_skb(skb);
1245 release_firmware(fw);
1246 return -bt_to_errno(evt_status);
1247 }
1248 kfree_skb(skb);
1249
1250 disable_patch = 1;
1251
1252 /* The firmware data file consists of list of Intel specific HCI
1253 * commands and its expected events. The first byte indicates the
1254 * type of the message, either HCI command or HCI event.
1255 *
1256 * It reads the command and its expected event from the firmware file,
1257 * and send to the controller. Once __hci_cmd_sync_ev() returns,
1258 * the returned event is compared with the event read from the firmware
1259 * file and it will continue until all the messages are downloaded to
1260 * the controller.
1261 *
1262 * Once the firmware patching is completed successfully,
1263 * the manufacturer mode is disabled with reset and activating the
1264 * downloaded patch.
1265 *
1266 * If the firmware patching fails, the manufacturer mode is
1267 * disabled with reset and deactivating the patch.
1268 *
1269 * If the default patch file is used, no reset is done when disabling
1270 * the manufacturer.
1271 */
1272 while (fw->size > fw_ptr - fw->data) {
1273 int ret;
1274
1275 ret = btusb_setup_intel_patching(hdev, fw, &fw_ptr,
1276 &disable_patch);
1277 if (ret < 0)
1278 goto exit_mfg_deactivate;
1279 }
1280
1281 release_firmware(fw);
1282
1283 if (disable_patch)
1284 goto exit_mfg_disable;
1285
1286 /* Patching completed successfully and disable the manufacturer mode
1287 * with reset and activate the downloaded firmware patches.
1288 */
1289 skb = __hci_cmd_sync(hdev, 0xfc11, sizeof(mfg_reset_activate),
1290 mfg_reset_activate, HCI_INIT_TIMEOUT);
1291 if (IS_ERR(skb)) {
1292 BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
1293 hdev->name, PTR_ERR(skb));
1294 return PTR_ERR(skb);
1295 }
1296 kfree_skb(skb);
1297
1298 BT_INFO("%s: Intel Bluetooth firmware patch completed and activated",
1299 hdev->name);
1300
1301 return 0;
1302
1303exit_mfg_disable:
1304 /* Disable the manufacturer mode without reset */
1305 skb = __hci_cmd_sync(hdev, 0xfc11, sizeof(mfg_disable), mfg_disable,
1306 HCI_INIT_TIMEOUT);
1307 if (IS_ERR(skb)) {
1308 BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
1309 hdev->name, PTR_ERR(skb));
1310 return PTR_ERR(skb);
1311 }
1312 kfree_skb(skb);
1313
1314 BT_INFO("%s: Intel Bluetooth firmware patch completed", hdev->name);
1315 return 0;
1316
1317exit_mfg_deactivate:
1318 release_firmware(fw);
1319
1320 /* Patching failed. Disable the manufacturer mode with reset and
1321 * deactivate the downloaded firmware patches.
1322 */
1323 skb = __hci_cmd_sync(hdev, 0xfc11, sizeof(mfg_reset_deactivate),
1324 mfg_reset_deactivate, HCI_INIT_TIMEOUT);
1325 if (IS_ERR(skb)) {
1326 BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
1327 hdev->name, PTR_ERR(skb));
1328 return PTR_ERR(skb);
1329 }
1330 kfree_skb(skb);
1331
1332 BT_INFO("%s: Intel Bluetooth firmware patch completed and deactivated",
1333 hdev->name);
1334
1335 return 0;
1336}
1337
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001338static int btusb_probe(struct usb_interface *intf,
1339 const struct usb_device_id *id)
1340{
1341 struct usb_endpoint_descriptor *ep_desc;
1342 struct btusb_data *data;
1343 struct hci_dev *hdev;
1344 int i, err;
1345
1346 BT_DBG("intf %p id %p", intf, id);
1347
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02001348 /* interface numbers are hardcoded in the spec */
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001349 if (intf->cur_altsetting->desc.bInterfaceNumber != 0)
1350 return -ENODEV;
1351
1352 if (!id->driver_info) {
1353 const struct usb_device_id *match;
1354 match = usb_match_id(intf, blacklist_table);
1355 if (match)
1356 id = match;
1357 }
1358
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02001359 if (id->driver_info == BTUSB_IGNORE)
1360 return -ENODEV;
1361
1362 if (ignore_dga && id->driver_info & BTUSB_DIGIANSWER)
1363 return -ENODEV;
1364
1365 if (ignore_csr && id->driver_info & BTUSB_CSR)
1366 return -ENODEV;
1367
1368 if (ignore_sniffer && id->driver_info & BTUSB_SNIFFER)
1369 return -ENODEV;
1370
Steven.Li4770ac22011-07-01 14:02:36 +08001371 if (id->driver_info & BTUSB_ATH3012) {
1372 struct usb_device *udev = interface_to_usbdev(intf);
1373
1374 /* Old firmware would otherwise let ath3k driver load
1375 * patch and sysconfig files */
1376 if (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x0001)
1377 return -ENODEV;
1378 }
1379
flintman9892c402015-09-23 06:14:10 -04001380 data = devm_kzalloc(&intf->dev, sizeof(*data), GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001381 if (!data)
1382 return -ENOMEM;
1383
1384 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
1385 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
1386
1387 if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
1388 data->intr_ep = ep_desc;
1389 continue;
1390 }
1391
1392 if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
1393 data->bulk_tx_ep = ep_desc;
1394 continue;
1395 }
1396
1397 if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
1398 data->bulk_rx_ep = ep_desc;
1399 continue;
1400 }
1401 }
1402
flintman9892c402015-09-23 06:14:10 -04001403 if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001404 return -ENODEV;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001405
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01001406 data->cmdreq_type = USB_TYPE_CLASS;
1407
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001408 data->udev = interface_to_usbdev(intf);
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02001409 data->intf = intf;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001410
1411 spin_lock_init(&data->lock);
1412
1413 INIT_WORK(&data->work, btusb_work);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001414 INIT_WORK(&data->waker, btusb_waker);
1415 spin_lock_init(&data->txlock);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001416
1417 init_usb_anchor(&data->tx_anchor);
1418 init_usb_anchor(&data->intr_anchor);
1419 init_usb_anchor(&data->bulk_anchor);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001420 init_usb_anchor(&data->isoc_anchor);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001421 init_usb_anchor(&data->deferred);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001422
1423 hdev = hci_alloc_dev();
flintman9892c402015-09-23 06:14:10 -04001424 if (!hdev)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001425 return -ENOMEM;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001426
Marcel Holtmannc13854ce2010-02-08 15:27:07 +01001427 hdev->bus = HCI_USB;
flintman9892c402015-09-23 06:14:10 -04001428 hci_set_drvdata(hdev, data);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001429
1430 data->hdev = hdev;
1431
1432 SET_HCIDEV_DEV(hdev, &intf->dev);
1433
flintman9892c402015-09-23 06:14:10 -04001434 hdev->open = btusb_open;
1435 hdev->close = btusb_close;
1436 hdev->flush = btusb_flush;
1437 hdev->send = btusb_send_frame;
1438 hdev->notify = btusb_notify;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001439
flintman9892c402015-09-23 06:14:10 -04001440 if (id->driver_info & BTUSB_BCM92035)
1441 hdev->setup = btusb_setup_bcm92035;
1442
1443 if (id->driver_info & BTUSB_INTEL)
1444 hdev->setup = btusb_setup_intel;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001445
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01001446 /* Interface numbers are hardcoded in the specification */
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001447 data->isoc = usb_ifnum_to_if(data->udev, 1);
1448
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01001449 if (!reset)
flintman9892c402015-09-23 06:14:10 -04001450 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02001451
1452 if (force_scofix || id->driver_info & BTUSB_WRONG_SCO_MTU) {
1453 if (!disable_scofix)
1454 set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
1455 }
1456
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001457 if (id->driver_info & BTUSB_BROKEN_ISOC)
1458 data->isoc = NULL;
1459
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01001460 if (id->driver_info & BTUSB_DIGIANSWER) {
1461 data->cmdreq_type = USB_TYPE_VENDOR;
flintman9892c402015-09-23 06:14:10 -04001462 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01001463 }
1464
1465 if (id->driver_info & BTUSB_CSR) {
1466 struct usb_device *udev = data->udev;
1467
1468 /* Old firmware would otherwise execute USB reset */
1469 if (le16_to_cpu(udev->descriptor.bcdDevice) < 0x117)
flintman9892c402015-09-23 06:14:10 -04001470 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01001471 }
1472
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02001473 if (id->driver_info & BTUSB_SNIFFER) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001474 struct usb_device *udev = data->udev;
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02001475
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01001476 /* New sniffer firmware has crippled HCI interface */
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02001477 if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
1478 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001479
1480 data->isoc = NULL;
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02001481 }
1482
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001483 if (data->isoc) {
1484 err = usb_driver_claim_interface(&btusb_driver,
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02001485 data->isoc, data);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001486 if (err < 0) {
1487 hci_free_dev(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001488 return err;
1489 }
1490 }
1491
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001492 err = hci_register_dev(hdev);
1493 if (err < 0) {
1494 hci_free_dev(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001495 return err;
1496 }
1497
1498 usb_set_intfdata(intf, data);
1499
1500 return 0;
1501}
1502
1503static void btusb_disconnect(struct usb_interface *intf)
1504{
1505 struct btusb_data *data = usb_get_intfdata(intf);
1506 struct hci_dev *hdev;
1507
1508 BT_DBG("intf %p", intf);
1509
1510 if (!data)
1511 return;
1512
1513 hdev = data->hdev;
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02001514 usb_set_intfdata(data->intf, NULL);
1515
1516 if (data->isoc)
1517 usb_set_intfdata(data->isoc, NULL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001518
1519 hci_unregister_dev(hdev);
1520
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02001521 if (intf == data->isoc)
1522 usb_driver_release_interface(&btusb_driver, data->intf);
1523 else if (data->isoc)
1524 usb_driver_release_interface(&btusb_driver, data->isoc);
1525
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001526 hci_free_dev(hdev);
1527}
1528
Oliver Neukum7bee5492009-08-24 23:44:59 +02001529#ifdef CONFIG_PM
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001530static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
1531{
1532 struct btusb_data *data = usb_get_intfdata(intf);
1533
1534 BT_DBG("intf %p", intf);
1535
1536 if (data->suspend_count++)
1537 return 0;
1538
Oliver Neukum7bee5492009-08-24 23:44:59 +02001539 spin_lock_irq(&data->txlock);
flintman9892c402015-09-23 06:14:10 -04001540 if (!(PMSG_IS_AUTO(message) && data->tx_in_flight)) {
Oliver Neukum7bee5492009-08-24 23:44:59 +02001541 set_bit(BTUSB_SUSPENDING, &data->flags);
1542 spin_unlock_irq(&data->txlock);
1543 } else {
1544 spin_unlock_irq(&data->txlock);
1545 data->suspend_count--;
1546 return -EBUSY;
1547 }
1548
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001549 cancel_work_sync(&data->work);
1550
Oliver Neukum7bee5492009-08-24 23:44:59 +02001551 btusb_stop_traffic(data);
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001552 usb_kill_anchored_urbs(&data->tx_anchor);
1553
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001554 return 0;
1555}
1556
Oliver Neukum7bee5492009-08-24 23:44:59 +02001557static void play_deferred(struct btusb_data *data)
1558{
1559 struct urb *urb;
1560 int err;
1561
1562 while ((urb = usb_get_from_anchor(&data->deferred))) {
1563 err = usb_submit_urb(urb, GFP_ATOMIC);
1564 if (err < 0)
1565 break;
1566
1567 data->tx_in_flight++;
1568 }
1569 usb_scuttle_anchored_urbs(&data->deferred);
1570}
1571
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001572static int btusb_resume(struct usb_interface *intf)
1573{
1574 struct btusb_data *data = usb_get_intfdata(intf);
1575 struct hci_dev *hdev = data->hdev;
Oliver Neukum7bee5492009-08-24 23:44:59 +02001576 int err = 0;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001577
1578 BT_DBG("intf %p", intf);
1579
1580 if (--data->suspend_count)
1581 return 0;
1582
1583 if (!test_bit(HCI_RUNNING, &hdev->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +02001584 goto done;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001585
1586 if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
1587 err = btusb_submit_intr_urb(hdev, GFP_NOIO);
1588 if (err < 0) {
1589 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001590 goto failed;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001591 }
1592 }
1593
1594 if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) {
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001595 err = btusb_submit_bulk_urb(hdev, GFP_NOIO);
1596 if (err < 0) {
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001597 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001598 goto failed;
1599 }
1600
1601 btusb_submit_bulk_urb(hdev, GFP_NOIO);
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001602 }
1603
1604 if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
1605 if (btusb_submit_isoc_urb(hdev, GFP_NOIO) < 0)
1606 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1607 else
1608 btusb_submit_isoc_urb(hdev, GFP_NOIO);
1609 }
1610
Oliver Neukum7bee5492009-08-24 23:44:59 +02001611 spin_lock_irq(&data->txlock);
1612 play_deferred(data);
1613 clear_bit(BTUSB_SUSPENDING, &data->flags);
1614 spin_unlock_irq(&data->txlock);
1615 schedule_work(&data->work);
1616
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001617 return 0;
Oliver Neukum7bee5492009-08-24 23:44:59 +02001618
1619failed:
1620 usb_scuttle_anchored_urbs(&data->deferred);
1621done:
1622 spin_lock_irq(&data->txlock);
1623 clear_bit(BTUSB_SUSPENDING, &data->flags);
1624 spin_unlock_irq(&data->txlock);
1625
1626 return err;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001627}
Oliver Neukum7bee5492009-08-24 23:44:59 +02001628#endif
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001629
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001630static struct usb_driver btusb_driver = {
1631 .name = "btusb",
1632 .probe = btusb_probe,
1633 .disconnect = btusb_disconnect,
Oliver Neukum7bee5492009-08-24 23:44:59 +02001634#ifdef CONFIG_PM
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001635 .suspend = btusb_suspend,
1636 .resume = btusb_resume,
Oliver Neukum7bee5492009-08-24 23:44:59 +02001637#endif
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001638 .id_table = btusb_table,
Oliver Neukum7bee5492009-08-24 23:44:59 +02001639 .supports_autosuspend = 1,
flintman9892c402015-09-23 06:14:10 -04001640 .disable_hub_initiated_lpm = 1,
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001641};
1642
flintman9892c402015-09-23 06:14:10 -04001643module_usb_driver(btusb_driver);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001644
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02001645module_param(ignore_dga, bool, 0644);
1646MODULE_PARM_DESC(ignore_dga, "Ignore devices with id 08fd:0001");
1647
1648module_param(ignore_csr, bool, 0644);
1649MODULE_PARM_DESC(ignore_csr, "Ignore devices with id 0a12:0001");
1650
1651module_param(ignore_sniffer, bool, 0644);
1652MODULE_PARM_DESC(ignore_sniffer, "Ignore devices with id 0a12:0002");
1653
1654module_param(disable_scofix, bool, 0644);
1655MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
1656
1657module_param(force_scofix, bool, 0644);
1658MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
1659
1660module_param(reset, bool, 0644);
1661MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
1662
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001663MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
1664MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
1665MODULE_VERSION(VERSION);
1666MODULE_LICENSE("GPL");