blob: 1c2a2cd8d2f80dbad498a1ac46414d5c8a72a532 [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 },
131 { USB_DEVICE(0x0930, 0x0215), .driver_info = BTUSB_IGNORE },
Bala Shanmugambe931122010-11-26 17:35:46 +0530132 { USB_DEVICE(0x0cf3, 0x3002), .driver_info = BTUSB_IGNORE },
Marek Vasut7f801fc2012-06-08 14:32:50 +0200133 { USB_DEVICE(0x0cf3, 0xe019), .driver_info = BTUSB_IGNORE },
Andy Ross2a7bccc2011-05-09 16:11:16 -0700134 { USB_DEVICE(0x13d3, 0x3304), .driver_info = BTUSB_IGNORE },
Bala Shanmugambe931122010-11-26 17:35:46 +0530135
Cho, Yu-Chen509e7862011-01-26 17:10:59 +0800136 /* Atheros AR9285 Malbec with sflash firmware */
137 { USB_DEVICE(0x03f0, 0x311d), .driver_info = BTUSB_IGNORE },
138
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530139 /* Atheros 3012 with sflash firmware */
Andy Shevchenko7baba822014-02-18 18:26:19 +0200140 { USB_DEVICE(0x0489, 0xe04d), .driver_info = BTUSB_ATH3012 },
141 { USB_DEVICE(0x0489, 0xe04e), .driver_info = BTUSB_ATH3012 },
142 { USB_DEVICE(0x0489, 0xe056), .driver_info = BTUSB_ATH3012 },
143 { USB_DEVICE(0x0489, 0xe057), .driver_info = BTUSB_ATH3012 },
144 { USB_DEVICE(0x04c5, 0x1330), .driver_info = BTUSB_ATH3012 },
Josh Boyerd83ff8c2013-02-19 11:54:16 -0500145 { USB_DEVICE(0x04ca, 0x3004), .driver_info = BTUSB_ATH3012 },
AceLan Kao55ed7d42012-03-28 10:25:36 +0800146 { USB_DEVICE(0x04ca, 0x3005), .driver_info = BTUSB_ATH3012 },
Daniel Schaal7a8c99e2012-12-29 11:14:34 +0100147 { USB_DEVICE(0x04ca, 0x3006), .driver_info = BTUSB_ATH3012 },
Sergio Cambrae98f05e2013-01-10 01:06:55 +0100148 { USB_DEVICE(0x04ca, 0x3008), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko7baba822014-02-18 18:26:19 +0200149 { USB_DEVICE(0x0930, 0x0219), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko9ed78382014-02-18 18:26:20 +0200150 { USB_DEVICE(0x0b05, 0x17d0), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko7baba822014-02-18 18:26:19 +0200151 { USB_DEVICE(0x0cf3, 0x0036), .driver_info = BTUSB_ATH3012 },
152 { USB_DEVICE(0x0cf3, 0x3004), .driver_info = BTUSB_ATH3012 },
153 { USB_DEVICE(0x0cf3, 0x3008), .driver_info = BTUSB_ATH3012 },
154 { USB_DEVICE(0x0cf3, 0x311d), .driver_info = BTUSB_ATH3012 },
155 { USB_DEVICE(0x0cf3, 0x3121), .driver_info = BTUSB_ATH3012 },
156 { USB_DEVICE(0x0cf3, 0x817a), .driver_info = BTUSB_ATH3012 },
157 { USB_DEVICE(0x0cf3, 0xe003), .driver_info = BTUSB_ATH3012 },
AceLan Kaoac713112012-04-19 14:53:45 +0800158 { USB_DEVICE(0x0cf3, 0xe004), .driver_info = BTUSB_ATH3012 },
Peng Chen140deab2013-08-30 17:41:40 +0800159 { USB_DEVICE(0x0cf3, 0xe005), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko7baba822014-02-18 18:26:19 +0200160 { USB_DEVICE(0x13d3, 0x3362), .driver_info = BTUSB_ATH3012 },
161 { USB_DEVICE(0x13d3, 0x3375), .driver_info = BTUSB_ATH3012 },
AceLan Kao3b36e952012-12-11 11:41:20 +0800162 { USB_DEVICE(0x13d3, 0x3393), .driver_info = BTUSB_ATH3012 },
Sujith Manoharanfa6e6072013-07-15 09:29:03 +0530163 { USB_DEVICE(0x13d3, 0x3402), .driver_info = BTUSB_ATH3012 },
Anantha Krishnan1bd6a902014-07-08 19:25:08 +0530164 { USB_DEVICE(0x13d3, 0x3432), .driver_info = BTUSB_ATH3012 },
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530165
Cho, Yu-Chene9036e32011-02-15 10:20:07 +0800166 /* Atheros AR5BBU12 with sflash firmware */
167 { USB_DEVICE(0x0489, 0xe02c), .driver_info = BTUSB_IGNORE },
168
Michael Gruetznera1144ca2012-05-02 22:33:40 +0200169 /* Atheros AR5BBU12 with sflash firmware */
Yevgeniy Melnichuk1403b362012-08-07 19:48:10 +0530170 { USB_DEVICE(0x0489, 0xe036), .driver_info = BTUSB_ATH3012 },
Andy Shevchenko7baba822014-02-18 18:26:19 +0200171 { USB_DEVICE(0x0489, 0xe03c), .driver_info = BTUSB_ATH3012 },
Michael Gruetznera1144ca2012-05-02 22:33:40 +0200172
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200173 /* Broadcom BCM2035 */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100174 { USB_DEVICE(0x0a5c, 0x2009), .driver_info = BTUSB_BCM92035 },
Andy Shevchenko7baba822014-02-18 18:26:19 +0200175 { USB_DEVICE(0x0a5c, 0x200a), .driver_info = BTUSB_WRONG_SCO_MTU },
176 { USB_DEVICE(0x0a5c, 0x2035), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200177
178 /* Broadcom BCM2045 */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100179 { USB_DEVICE(0x0a5c, 0x2039), .driver_info = BTUSB_WRONG_SCO_MTU },
180 { USB_DEVICE(0x0a5c, 0x2101), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmannbdbef3d2008-09-23 00:16:35 +0200181
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200182 /* IBM/Lenovo ThinkPad with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100183 { USB_DEVICE(0x0a5c, 0x201e), .driver_info = BTUSB_WRONG_SCO_MTU },
184 { USB_DEVICE(0x0a5c, 0x2110), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200185
186 /* HP laptop with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100187 { USB_DEVICE(0x03f0, 0x171d), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200188
189 /* Dell laptop with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100190 { USB_DEVICE(0x413c, 0x8126), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200191
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100192 /* Dell Wireless 370 and 410 devices */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100193 { USB_DEVICE(0x413c, 0x8152), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100194 { USB_DEVICE(0x413c, 0x8156), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200195
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100196 /* Belkin F8T012 and F8T013 devices */
197 { USB_DEVICE(0x050d, 0x0012), .driver_info = BTUSB_WRONG_SCO_MTU },
198 { USB_DEVICE(0x050d, 0x0013), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200199
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100200 /* Asus WL-BTD202 device */
201 { USB_DEVICE(0x0b05, 0x1715), .driver_info = BTUSB_WRONG_SCO_MTU },
202
203 /* Kensington Bluetooth USB adapter */
204 { USB_DEVICE(0x047d, 0x105e), .driver_info = BTUSB_WRONG_SCO_MTU },
205
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200206 /* RTX Telecom based adapters with buggy SCO support */
207 { USB_DEVICE(0x0400, 0x0807), .driver_info = BTUSB_BROKEN_ISOC },
208 { USB_DEVICE(0x0400, 0x080a), .driver_info = BTUSB_BROKEN_ISOC },
209
210 /* CONWISE Technology based adapters with buggy SCO support */
211 { USB_DEVICE(0x0e5e, 0x6622), .driver_info = BTUSB_BROKEN_ISOC },
212
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200213 /* Digianswer devices */
214 { USB_DEVICE(0x08fd, 0x0001), .driver_info = BTUSB_DIGIANSWER },
215 { USB_DEVICE(0x08fd, 0x0002), .driver_info = BTUSB_IGNORE },
216
217 /* CSR BlueCore Bluetooth Sniffer */
218 { USB_DEVICE(0x0a12, 0x0002), .driver_info = BTUSB_SNIFFER },
219
220 /* Frontline ComProbe Bluetooth Sniffer */
221 { USB_DEVICE(0x16d3, 0x0002), .driver_info = BTUSB_SNIFFER },
222
flintman9892c402015-09-23 06:14:10 -0400223 /* Intel Bluetooth device */
224 { USB_DEVICE(0x8087, 0x07dc), .driver_info = BTUSB_INTEL },
225
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200226 { } /* Terminating entry */
227};
228
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200229#define BTUSB_MAX_ISOC_FRAMES 10
230
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200231#define BTUSB_INTR_RUNNING 0
232#define BTUSB_BULK_RUNNING 1
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200233#define BTUSB_ISOC_RUNNING 2
Oliver Neukum7bee5492009-08-24 23:44:59 +0200234#define BTUSB_SUSPENDING 3
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -0300235#define BTUSB_DID_ISO_RESUME 4
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200236
237struct btusb_data {
238 struct hci_dev *hdev;
239 struct usb_device *udev;
Marcel Holtmann5fbcd262008-09-23 00:16:36 +0200240 struct usb_interface *intf;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200241 struct usb_interface *isoc;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200242
243 spinlock_t lock;
244
245 unsigned long flags;
246
247 struct work_struct work;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200248 struct work_struct waker;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200249
250 struct usb_anchor tx_anchor;
251 struct usb_anchor intr_anchor;
252 struct usb_anchor bulk_anchor;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200253 struct usb_anchor isoc_anchor;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200254 struct usb_anchor deferred;
255 int tx_in_flight;
256 spinlock_t txlock;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200257
258 struct usb_endpoint_descriptor *intr_ep;
259 struct usb_endpoint_descriptor *bulk_tx_ep;
260 struct usb_endpoint_descriptor *bulk_rx_ep;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200261 struct usb_endpoint_descriptor *isoc_tx_ep;
262 struct usb_endpoint_descriptor *isoc_rx_ep;
263
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100264 __u8 cmdreq_type;
265
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100266 unsigned int sco_num;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200267 int isoc_altsetting;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +0100268 int suspend_count;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200269};
270
Oliver Neukum7bee5492009-08-24 23:44:59 +0200271static int inc_tx(struct btusb_data *data)
272{
273 unsigned long flags;
274 int rv;
275
276 spin_lock_irqsave(&data->txlock, flags);
277 rv = test_bit(BTUSB_SUSPENDING, &data->flags);
278 if (!rv)
279 data->tx_in_flight++;
280 spin_unlock_irqrestore(&data->txlock, flags);
281
282 return rv;
283}
284
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200285static void btusb_intr_complete(struct urb *urb)
286{
287 struct hci_dev *hdev = urb->context;
flintman9892c402015-09-23 06:14:10 -0400288 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200289 int err;
290
291 BT_DBG("%s urb %p status %d count %d", hdev->name,
292 urb, urb->status, urb->actual_length);
293
294 if (!test_bit(HCI_RUNNING, &hdev->flags))
295 return;
296
297 if (urb->status == 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200298 hdev->stat.byte_rx += urb->actual_length;
299
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200300 if (hci_recv_fragment(hdev, HCI_EVENT_PKT,
301 urb->transfer_buffer,
302 urb->actual_length) < 0) {
303 BT_ERR("%s corrupted event packet", hdev->name);
304 hdev->stat.err_rx++;
305 }
306 }
307
308 if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
309 return;
310
Oliver Neukum7bee5492009-08-24 23:44:59 +0200311 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200312 usb_anchor_urb(urb, &data->intr_anchor);
313
314 err = usb_submit_urb(urb, GFP_ATOMIC);
315 if (err < 0) {
flintman9892c402015-09-23 06:14:10 -0400316 /* -EPERM: urb is being killed;
317 * -ENODEV: device got disconnected */
318 if (err != -EPERM && err != -ENODEV)
Stefan Seyfried61faddf2010-11-30 21:49:08 +0100319 BT_ERR("%s urb %p failed to resubmit (%d)",
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200320 hdev->name, urb, -err);
321 usb_unanchor_urb(urb);
322 }
323}
324
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100325static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200326{
flintman9892c402015-09-23 06:14:10 -0400327 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200328 struct urb *urb;
329 unsigned char *buf;
330 unsigned int pipe;
331 int err, size;
332
333 BT_DBG("%s", hdev->name);
334
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200335 if (!data->intr_ep)
336 return -ENODEV;
337
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100338 urb = usb_alloc_urb(0, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200339 if (!urb)
340 return -ENOMEM;
341
342 size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
343
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100344 buf = kmalloc(size, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200345 if (!buf) {
346 usb_free_urb(urb);
347 return -ENOMEM;
348 }
349
350 pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
351
352 usb_fill_int_urb(urb, data->udev, pipe, buf, size,
353 btusb_intr_complete, hdev,
354 data->intr_ep->bInterval);
355
356 urb->transfer_flags |= URB_FREE_BUFFER;
357
358 usb_anchor_urb(urb, &data->intr_anchor);
359
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100360 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200361 if (err < 0) {
flintman9892c402015-09-23 06:14:10 -0400362 if (err != -EPERM && err != -ENODEV)
363 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200364 hdev->name, urb, -err);
365 usb_unanchor_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200366 }
367
368 usb_free_urb(urb);
369
370 return err;
371}
372
373static void btusb_bulk_complete(struct urb *urb)
374{
375 struct hci_dev *hdev = urb->context;
flintman9892c402015-09-23 06:14:10 -0400376 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200377 int err;
378
379 BT_DBG("%s urb %p status %d count %d", hdev->name,
380 urb, urb->status, urb->actual_length);
381
382 if (!test_bit(HCI_RUNNING, &hdev->flags))
383 return;
384
385 if (urb->status == 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200386 hdev->stat.byte_rx += urb->actual_length;
387
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200388 if (hci_recv_fragment(hdev, HCI_ACLDATA_PKT,
389 urb->transfer_buffer,
390 urb->actual_length) < 0) {
391 BT_ERR("%s corrupted ACL packet", hdev->name);
392 hdev->stat.err_rx++;
393 }
394 }
395
396 if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
397 return;
398
399 usb_anchor_urb(urb, &data->bulk_anchor);
Oliver Neukum652fd782009-12-16 19:23:43 +0100400 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200401
402 err = usb_submit_urb(urb, GFP_ATOMIC);
403 if (err < 0) {
flintman9892c402015-09-23 06:14:10 -0400404 /* -EPERM: urb is being killed;
405 * -ENODEV: device got disconnected */
406 if (err != -EPERM && err != -ENODEV)
Stefan Seyfried61faddf2010-11-30 21:49:08 +0100407 BT_ERR("%s urb %p failed to resubmit (%d)",
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200408 hdev->name, urb, -err);
409 usb_unanchor_urb(urb);
410 }
411}
412
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100413static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200414{
flintman9892c402015-09-23 06:14:10 -0400415 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200416 struct urb *urb;
417 unsigned char *buf;
418 unsigned int pipe;
Vikram Kandukuri290ba202009-07-02 14:31:59 +0530419 int err, size = HCI_MAX_FRAME_SIZE;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200420
421 BT_DBG("%s", hdev->name);
422
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200423 if (!data->bulk_rx_ep)
424 return -ENODEV;
425
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100426 urb = usb_alloc_urb(0, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200427 if (!urb)
428 return -ENOMEM;
429
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100430 buf = kmalloc(size, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200431 if (!buf) {
432 usb_free_urb(urb);
433 return -ENOMEM;
434 }
435
436 pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
437
438 usb_fill_bulk_urb(urb, data->udev, pipe,
439 buf, size, btusb_bulk_complete, hdev);
440
441 urb->transfer_flags |= URB_FREE_BUFFER;
442
Oliver Neukum7bee5492009-08-24 23:44:59 +0200443 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200444 usb_anchor_urb(urb, &data->bulk_anchor);
445
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100446 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200447 if (err < 0) {
flintman9892c402015-09-23 06:14:10 -0400448 if (err != -EPERM && err != -ENODEV)
449 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200450 hdev->name, urb, -err);
451 usb_unanchor_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200452 }
453
454 usb_free_urb(urb);
455
456 return err;
457}
458
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200459static void btusb_isoc_complete(struct urb *urb)
460{
461 struct hci_dev *hdev = urb->context;
flintman9892c402015-09-23 06:14:10 -0400462 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200463 int i, err;
464
465 BT_DBG("%s urb %p status %d count %d", hdev->name,
466 urb, urb->status, urb->actual_length);
467
468 if (!test_bit(HCI_RUNNING, &hdev->flags))
469 return;
470
471 if (urb->status == 0) {
472 for (i = 0; i < urb->number_of_packets; i++) {
473 unsigned int offset = urb->iso_frame_desc[i].offset;
474 unsigned int length = urb->iso_frame_desc[i].actual_length;
475
476 if (urb->iso_frame_desc[i].status)
477 continue;
478
479 hdev->stat.byte_rx += length;
480
481 if (hci_recv_fragment(hdev, HCI_SCODATA_PKT,
482 urb->transfer_buffer + offset,
483 length) < 0) {
484 BT_ERR("%s corrupted SCO packet", hdev->name);
485 hdev->stat.err_rx++;
486 }
487 }
488 }
489
490 if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags))
491 return;
492
493 usb_anchor_urb(urb, &data->isoc_anchor);
494
495 err = usb_submit_urb(urb, GFP_ATOMIC);
496 if (err < 0) {
flintman9892c402015-09-23 06:14:10 -0400497 /* -EPERM: urb is being killed;
498 * -ENODEV: device got disconnected */
499 if (err != -EPERM && err != -ENODEV)
Stefan Seyfried61faddf2010-11-30 21:49:08 +0100500 BT_ERR("%s urb %p failed to resubmit (%d)",
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200501 hdev->name, urb, -err);
502 usb_unanchor_urb(urb);
503 }
504}
505
flintman9892c402015-09-23 06:14:10 -0400506static inline void __fill_isoc_descriptor(struct urb *urb, int len, int mtu)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200507{
508 int i, offset = 0;
509
510 BT_DBG("len %d mtu %d", len, mtu);
511
512 for (i = 0; i < BTUSB_MAX_ISOC_FRAMES && len >= mtu;
513 i++, offset += mtu, len -= mtu) {
514 urb->iso_frame_desc[i].offset = offset;
515 urb->iso_frame_desc[i].length = mtu;
516 }
517
518 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
519 urb->iso_frame_desc[i].offset = offset;
520 urb->iso_frame_desc[i].length = len;
521 i++;
522 }
523
524 urb->number_of_packets = i;
525}
526
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100527static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200528{
flintman9892c402015-09-23 06:14:10 -0400529 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200530 struct urb *urb;
531 unsigned char *buf;
532 unsigned int pipe;
533 int err, size;
534
535 BT_DBG("%s", hdev->name);
536
537 if (!data->isoc_rx_ep)
538 return -ENODEV;
539
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100540 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200541 if (!urb)
542 return -ENOMEM;
543
544 size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) *
545 BTUSB_MAX_ISOC_FRAMES;
546
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100547 buf = kmalloc(size, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200548 if (!buf) {
549 usb_free_urb(urb);
550 return -ENOMEM;
551 }
552
553 pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress);
554
flintman9892c402015-09-23 06:14:10 -0400555 usb_fill_int_urb(urb, data->udev, pipe, buf, size, btusb_isoc_complete,
556 hdev, data->isoc_rx_ep->bInterval);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200557
558 urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200559
560 __fill_isoc_descriptor(urb, size,
561 le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize));
562
563 usb_anchor_urb(urb, &data->isoc_anchor);
564
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100565 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200566 if (err < 0) {
flintman9892c402015-09-23 06:14:10 -0400567 if (err != -EPERM && err != -ENODEV)
568 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200569 hdev->name, urb, -err);
570 usb_unanchor_urb(urb);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200571 }
572
573 usb_free_urb(urb);
574
575 return err;
576}
577
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200578static void btusb_tx_complete(struct urb *urb)
579{
580 struct sk_buff *skb = urb->context;
581 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
flintman9892c402015-09-23 06:14:10 -0400582 struct btusb_data *data = hci_get_drvdata(hdev);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200583
584 BT_DBG("%s urb %p status %d count %d", hdev->name,
585 urb, urb->status, urb->actual_length);
586
587 if (!test_bit(HCI_RUNNING, &hdev->flags))
588 goto done;
589
590 if (!urb->status)
591 hdev->stat.byte_tx += urb->transfer_buffer_length;
592 else
593 hdev->stat.err_tx++;
594
595done:
596 spin_lock(&data->txlock);
597 data->tx_in_flight--;
598 spin_unlock(&data->txlock);
599
600 kfree(urb->setup_packet);
601
602 kfree_skb(skb);
603}
604
605static void btusb_isoc_tx_complete(struct urb *urb)
606{
607 struct sk_buff *skb = urb->context;
608 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200609
610 BT_DBG("%s urb %p status %d count %d", hdev->name,
611 urb, urb->status, urb->actual_length);
612
613 if (!test_bit(HCI_RUNNING, &hdev->flags))
614 goto done;
615
616 if (!urb->status)
617 hdev->stat.byte_tx += urb->transfer_buffer_length;
618 else
619 hdev->stat.err_tx++;
620
621done:
622 kfree(urb->setup_packet);
623
624 kfree_skb(skb);
625}
626
627static int btusb_open(struct hci_dev *hdev)
628{
flintman9892c402015-09-23 06:14:10 -0400629 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200630 int err;
631
632 BT_DBG("%s", hdev->name);
633
Oliver Neukum7bee5492009-08-24 23:44:59 +0200634 err = usb_autopm_get_interface(data->intf);
635 if (err < 0)
636 return err;
637
638 data->intf->needs_remote_wakeup = 1;
639
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200640 if (test_and_set_bit(HCI_RUNNING, &hdev->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +0200641 goto done;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200642
643 if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +0200644 goto done;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200645
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100646 err = btusb_submit_intr_urb(hdev, GFP_KERNEL);
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100647 if (err < 0)
648 goto failed;
649
650 err = btusb_submit_bulk_urb(hdev, GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200651 if (err < 0) {
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100652 usb_kill_anchored_urbs(&data->intr_anchor);
653 goto failed;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200654 }
655
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100656 set_bit(BTUSB_BULK_RUNNING, &data->flags);
657 btusb_submit_bulk_urb(hdev, GFP_KERNEL);
658
Oliver Neukum7bee5492009-08-24 23:44:59 +0200659done:
660 usb_autopm_put_interface(data->intf);
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100661 return 0;
662
663failed:
664 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
665 clear_bit(HCI_RUNNING, &hdev->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200666 usb_autopm_put_interface(data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200667 return err;
668}
669
Oliver Neukum7bee5492009-08-24 23:44:59 +0200670static void btusb_stop_traffic(struct btusb_data *data)
671{
672 usb_kill_anchored_urbs(&data->intr_anchor);
673 usb_kill_anchored_urbs(&data->bulk_anchor);
674 usb_kill_anchored_urbs(&data->isoc_anchor);
675}
676
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200677static int btusb_close(struct hci_dev *hdev)
678{
flintman9892c402015-09-23 06:14:10 -0400679 struct btusb_data *data = hci_get_drvdata(hdev);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200680 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200681
682 BT_DBG("%s", hdev->name);
683
684 if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
685 return 0;
686
Marcel Holtmanne8c3c3d2008-09-23 00:16:36 +0200687 cancel_work_sync(&data->work);
Linus Torvalds404291a2009-11-11 13:32:29 -0800688 cancel_work_sync(&data->waker);
Marcel Holtmanne8c3c3d2008-09-23 00:16:36 +0200689
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200690 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200691 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200692 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200693
694 btusb_stop_traffic(data);
695 err = usb_autopm_get_interface(data->intf);
696 if (err < 0)
Oliver Neukum7b8e2c12009-11-13 14:26:23 +0100697 goto failed;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200698
699 data->intf->needs_remote_wakeup = 0;
700 usb_autopm_put_interface(data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200701
Oliver Neukum7b8e2c12009-11-13 14:26:23 +0100702failed:
703 usb_scuttle_anchored_urbs(&data->deferred);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200704 return 0;
705}
706
707static int btusb_flush(struct hci_dev *hdev)
708{
flintman9892c402015-09-23 06:14:10 -0400709 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200710
711 BT_DBG("%s", hdev->name);
712
713 usb_kill_anchored_urbs(&data->tx_anchor);
714
715 return 0;
716}
717
718static int btusb_send_frame(struct sk_buff *skb)
719{
720 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
flintman9892c402015-09-23 06:14:10 -0400721 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200722 struct usb_ctrlrequest *dr;
723 struct urb *urb;
724 unsigned int pipe;
725 int err;
726
727 BT_DBG("%s", hdev->name);
728
729 if (!test_bit(HCI_RUNNING, &hdev->flags))
730 return -EBUSY;
731
732 switch (bt_cb(skb)->pkt_type) {
733 case HCI_COMMAND_PKT:
734 urb = usb_alloc_urb(0, GFP_ATOMIC);
735 if (!urb)
736 return -ENOMEM;
737
738 dr = kmalloc(sizeof(*dr), GFP_ATOMIC);
739 if (!dr) {
740 usb_free_urb(urb);
741 return -ENOMEM;
742 }
743
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100744 dr->bRequestType = data->cmdreq_type;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200745 dr->bRequest = 0;
746 dr->wIndex = 0;
747 dr->wValue = 0;
748 dr->wLength = __cpu_to_le16(skb->len);
749
750 pipe = usb_sndctrlpipe(data->udev, 0x00);
751
752 usb_fill_control_urb(urb, data->udev, pipe, (void *) dr,
753 skb->data, skb->len, btusb_tx_complete, skb);
754
755 hdev->stat.cmd_tx++;
756 break;
757
758 case HCI_ACLDATA_PKT:
flintman9892c402015-09-23 06:14:10 -0400759 if (!data->bulk_tx_ep)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200760 return -ENODEV;
761
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200762 urb = usb_alloc_urb(0, GFP_ATOMIC);
763 if (!urb)
764 return -ENOMEM;
765
766 pipe = usb_sndbulkpipe(data->udev,
767 data->bulk_tx_ep->bEndpointAddress);
768
769 usb_fill_bulk_urb(urb, data->udev, pipe,
770 skb->data, skb->len, btusb_tx_complete, skb);
771
772 hdev->stat.acl_tx++;
773 break;
774
775 case HCI_SCODATA_PKT:
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200776 if (!data->isoc_tx_ep || hdev->conn_hash.sco_num < 1)
777 return -ENODEV;
778
779 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_ATOMIC);
780 if (!urb)
781 return -ENOMEM;
782
783 pipe = usb_sndisocpipe(data->udev,
784 data->isoc_tx_ep->bEndpointAddress);
785
Gustavo F. Padovan03c2d0e2011-02-14 18:53:43 -0300786 usb_fill_int_urb(urb, data->udev, pipe,
787 skb->data, skb->len, btusb_isoc_tx_complete,
788 skb, data->isoc_tx_ep->bInterval);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200789
790 urb->transfer_flags = URB_ISO_ASAP;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200791
792 __fill_isoc_descriptor(urb, skb->len,
793 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
794
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200795 hdev->stat.sco_tx++;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200796 goto skip_waking;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200797
798 default:
799 return -EILSEQ;
800 }
801
Oliver Neukum7bee5492009-08-24 23:44:59 +0200802 err = inc_tx(data);
803 if (err) {
804 usb_anchor_urb(urb, &data->deferred);
805 schedule_work(&data->waker);
806 err = 0;
807 goto done;
808 }
809
810skip_waking:
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200811 usb_anchor_urb(urb, &data->tx_anchor);
812
813 err = usb_submit_urb(urb, GFP_ATOMIC);
814 if (err < 0) {
flintman9892c402015-09-23 06:14:10 -0400815 if (err != -EPERM && err != -ENODEV)
816 BT_ERR("%s urb %p submission failed (%d)",
817 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200818 kfree(urb->setup_packet);
819 usb_unanchor_urb(urb);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200820 } else {
821 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200822 }
823
Oliver Neukum7bee5492009-08-24 23:44:59 +0200824done:
flintman9892c402015-09-23 06:14:10 -0400825 usb_free_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200826 return err;
827}
828
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200829static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
830{
flintman9892c402015-09-23 06:14:10 -0400831 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200832
833 BT_DBG("%s evt %d", hdev->name, evt);
834
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100835 if (hdev->conn_hash.sco_num != data->sco_num) {
836 data->sco_num = hdev->conn_hash.sco_num;
837 schedule_work(&data->work);
Marcel Holtmanna780efa2008-11-30 12:17:12 +0100838 }
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200839}
840
flintman9892c402015-09-23 06:14:10 -0400841static inline int __set_isoc_interface(struct hci_dev *hdev, int altsetting)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200842{
flintman9892c402015-09-23 06:14:10 -0400843 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200844 struct usb_interface *intf = data->isoc;
845 struct usb_endpoint_descriptor *ep_desc;
846 int i, err;
847
848 if (!data->isoc)
849 return -ENODEV;
850
851 err = usb_set_interface(data->udev, 1, altsetting);
852 if (err < 0) {
853 BT_ERR("%s setting interface failed (%d)", hdev->name, -err);
854 return err;
855 }
856
857 data->isoc_altsetting = altsetting;
858
859 data->isoc_tx_ep = NULL;
860 data->isoc_rx_ep = NULL;
861
862 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
863 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
864
865 if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) {
866 data->isoc_tx_ep = ep_desc;
867 continue;
868 }
869
870 if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) {
871 data->isoc_rx_ep = ep_desc;
872 continue;
873 }
874 }
875
876 if (!data->isoc_tx_ep || !data->isoc_rx_ep) {
877 BT_ERR("%s invalid SCO descriptors", hdev->name);
878 return -ENODEV;
879 }
880
881 return 0;
882}
883
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200884static void btusb_work(struct work_struct *work)
885{
886 struct btusb_data *data = container_of(work, struct btusb_data, work);
887 struct hci_dev *hdev = data->hdev;
flintman9892c402015-09-23 06:14:10 -0400888 int new_alts;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200889 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200890
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200891 if (hdev->conn_hash.sco_num > 0) {
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -0300892 if (!test_bit(BTUSB_DID_ISO_RESUME, &data->flags)) {
Oliver Neukum8efdd0c2011-02-11 13:00:06 +0100893 err = usb_autopm_get_interface(data->isoc ? data->isoc : data->intf);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200894 if (err < 0) {
895 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
896 usb_kill_anchored_urbs(&data->isoc_anchor);
897 return;
898 }
899
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -0300900 set_bit(BTUSB_DID_ISO_RESUME, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200901 }
flintman9892c402015-09-23 06:14:10 -0400902
903 if (hdev->voice_setting & 0x0020) {
904 static const int alts[3] = { 2, 4, 5 };
905 new_alts = alts[hdev->conn_hash.sco_num - 1];
906 } else {
907 new_alts = hdev->conn_hash.sco_num;
908 }
909
910 if (data->isoc_altsetting != new_alts) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200911 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
912 usb_kill_anchored_urbs(&data->isoc_anchor);
913
flintman9892c402015-09-23 06:14:10 -0400914 if (__set_isoc_interface(hdev, new_alts) < 0)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200915 return;
916 }
917
918 if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100919 if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200920 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
921 else
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100922 btusb_submit_isoc_urb(hdev, GFP_KERNEL);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200923 }
924 } else {
925 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
926 usb_kill_anchored_urbs(&data->isoc_anchor);
927
928 __set_isoc_interface(hdev, 0);
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -0300929 if (test_and_clear_bit(BTUSB_DID_ISO_RESUME, &data->flags))
Oliver Neukum8efdd0c2011-02-11 13:00:06 +0100930 usb_autopm_put_interface(data->isoc ? data->isoc : data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200931 }
932}
933
Oliver Neukum7bee5492009-08-24 23:44:59 +0200934static void btusb_waker(struct work_struct *work)
935{
936 struct btusb_data *data = container_of(work, struct btusb_data, waker);
937 int err;
938
939 err = usb_autopm_get_interface(data->intf);
940 if (err < 0)
941 return;
942
943 usb_autopm_put_interface(data->intf);
944}
945
flintman9892c402015-09-23 06:14:10 -0400946static int btusb_setup_bcm92035(struct hci_dev *hdev)
947{
948 struct sk_buff *skb;
949 u8 val = 0x00;
950
951 BT_DBG("%s", hdev->name);
952
953 skb = __hci_cmd_sync(hdev, 0xfc3b, 1, &val, HCI_INIT_TIMEOUT);
954 if (IS_ERR(skb))
955 BT_ERR("BCM92035 command failed (%ld)", -PTR_ERR(skb));
956 else
957 kfree_skb(skb);
958
959 return 0;
960}
961
962struct intel_version {
963 u8 status;
964 u8 hw_platform;
965 u8 hw_variant;
966 u8 hw_revision;
967 u8 fw_variant;
968 u8 fw_revision;
969 u8 fw_build_num;
970 u8 fw_build_ww;
971 u8 fw_build_yy;
972 u8 fw_patch_num;
973} __packed;
974
975static const struct firmware *btusb_setup_intel_get_fw(struct hci_dev *hdev,
976 struct intel_version *ver)
977{
978 const struct firmware *fw;
979 char fwname[64];
980 int ret;
981
982 snprintf(fwname, sizeof(fwname),
983 "intel/ibt-hw-%x.%x.%x-fw-%x.%x.%x.%x.%x.bseq",
984 ver->hw_platform, ver->hw_variant, ver->hw_revision,
985 ver->fw_variant, ver->fw_revision, ver->fw_build_num,
986 ver->fw_build_ww, ver->fw_build_yy);
987
988 ret = request_firmware(&fw, fwname, &hdev->dev);
989 if (ret < 0) {
990 if (ret == -EINVAL) {
991 BT_ERR("%s Intel firmware file request failed (%d)",
992 hdev->name, ret);
993 return NULL;
994 }
995
996 BT_ERR("%s failed to open Intel firmware file: %s(%d)",
997 hdev->name, fwname, ret);
998
999 /* If the correct firmware patch file is not found, use the
1000 * default firmware patch file instead
1001 */
1002 snprintf(fwname, sizeof(fwname), "intel/ibt-hw-%x.%x.bseq",
1003 ver->hw_platform, ver->hw_variant);
1004 if (request_firmware(&fw, fwname, &hdev->dev) < 0) {
1005 BT_ERR("%s failed to open default Intel fw file: %s",
1006 hdev->name, fwname);
1007 return NULL;
1008 }
1009 }
1010
1011 BT_INFO("%s: Intel Bluetooth firmware file: %s", hdev->name, fwname);
1012
1013 return fw;
1014}
1015
1016static int btusb_setup_intel_patching(struct hci_dev *hdev,
1017 const struct firmware *fw,
1018 const u8 **fw_ptr, int *disable_patch)
1019{
1020 struct sk_buff *skb;
1021 struct hci_command_hdr *cmd;
1022 const u8 *cmd_param;
1023 struct hci_event_hdr *evt = NULL;
1024 const u8 *evt_param = NULL;
1025 int remain = fw->size - (*fw_ptr - fw->data);
1026
1027 /* The first byte indicates the types of the patch command or event.
1028 * 0x01 means HCI command and 0x02 is HCI event. If the first bytes
1029 * in the current firmware buffer doesn't start with 0x01 or
1030 * the size of remain buffer is smaller than HCI command header,
1031 * the firmware file is corrupted and it should stop the patching
1032 * process.
1033 */
1034 if (remain > HCI_COMMAND_HDR_SIZE && *fw_ptr[0] != 0x01) {
1035 BT_ERR("%s Intel fw corrupted: invalid cmd read", hdev->name);
1036 return -EINVAL;
1037 }
1038 (*fw_ptr)++;
1039 remain--;
1040
1041 cmd = (struct hci_command_hdr *)(*fw_ptr);
1042 *fw_ptr += sizeof(*cmd);
1043 remain -= sizeof(*cmd);
1044
1045 /* Ensure that the remain firmware data is long enough than the length
1046 * of command parameter. If not, the firmware file is corrupted.
1047 */
1048 if (remain < cmd->plen) {
1049 BT_ERR("%s Intel fw corrupted: invalid cmd len", hdev->name);
1050 return -EFAULT;
1051 }
1052
1053 /* If there is a command that loads a patch in the firmware
1054 * file, then enable the patch upon success, otherwise just
1055 * disable the manufacturer mode, for example patch activation
1056 * is not required when the default firmware patch file is used
1057 * because there are no patch data to load.
1058 */
1059 if (*disable_patch && le16_to_cpu(cmd->opcode) == 0xfc8e)
1060 *disable_patch = 0;
1061
1062 cmd_param = *fw_ptr;
1063 *fw_ptr += cmd->plen;
1064 remain -= cmd->plen;
1065
1066 /* This reads the expected events when the above command is sent to the
1067 * device. Some vendor commands expects more than one events, for
1068 * example command status event followed by vendor specific event.
1069 * For this case, it only keeps the last expected event. so the command
1070 * can be sent with __hci_cmd_sync_ev() which returns the sk_buff of
1071 * last expected event.
1072 */
1073 while (remain > HCI_EVENT_HDR_SIZE && *fw_ptr[0] == 0x02) {
1074 (*fw_ptr)++;
1075 remain--;
1076
1077 evt = (struct hci_event_hdr *)(*fw_ptr);
1078 *fw_ptr += sizeof(*evt);
1079 remain -= sizeof(*evt);
1080
1081 if (remain < evt->plen) {
1082 BT_ERR("%s Intel fw corrupted: invalid evt len",
1083 hdev->name);
1084 return -EFAULT;
1085 }
1086
1087 evt_param = *fw_ptr;
1088 *fw_ptr += evt->plen;
1089 remain -= evt->plen;
1090 }
1091
1092 /* Every HCI commands in the firmware file has its correspond event.
1093 * If event is not found or remain is smaller than zero, the firmware
1094 * file is corrupted.
1095 */
1096 if (!evt || !evt_param || remain < 0) {
1097 BT_ERR("%s Intel fw corrupted: invalid evt read", hdev->name);
1098 return -EFAULT;
1099 }
1100
1101 skb = __hci_cmd_sync_ev(hdev, le16_to_cpu(cmd->opcode), cmd->plen,
1102 cmd_param, evt->evt, HCI_INIT_TIMEOUT);
1103 if (IS_ERR(skb)) {
1104 BT_ERR("%s sending Intel patch command (0x%4.4x) failed (%ld)",
1105 hdev->name, cmd->opcode, PTR_ERR(skb));
1106 return PTR_ERR(skb);
1107 }
1108
1109 /* It ensures that the returned event matches the event data read from
1110 * the firmware file. At fist, it checks the length and then
1111 * the contents of the event.
1112 */
1113 if (skb->len != evt->plen) {
1114 BT_ERR("%s mismatch event length (opcode 0x%4.4x)", hdev->name,
1115 le16_to_cpu(cmd->opcode));
1116 kfree_skb(skb);
1117 return -EFAULT;
1118 }
1119
1120 if (memcmp(skb->data, evt_param, evt->plen)) {
1121 BT_ERR("%s mismatch event parameter (opcode 0x%4.4x)",
1122 hdev->name, le16_to_cpu(cmd->opcode));
1123 kfree_skb(skb);
1124 return -EFAULT;
1125 }
1126 kfree_skb(skb);
1127
1128 return 0;
1129}
1130
1131static int btusb_setup_intel(struct hci_dev *hdev)
1132{
1133 struct sk_buff *skb;
1134 const struct firmware *fw;
1135 const u8 *fw_ptr;
1136 int disable_patch;
1137 struct intel_version *ver;
1138
1139 const u8 mfg_enable[] = { 0x01, 0x00 };
1140 const u8 mfg_disable[] = { 0x00, 0x00 };
1141 const u8 mfg_reset_deactivate[] = { 0x00, 0x01 };
1142 const u8 mfg_reset_activate[] = { 0x00, 0x02 };
1143
1144 BT_DBG("%s", hdev->name);
1145
1146 /* The controller has a bug with the first HCI command sent to it
1147 * returning number of completed commands as zero. This would stall the
1148 * command processing in the Bluetooth core.
1149 *
1150 * As a workaround, send HCI Reset command first which will reset the
1151 * number of completed commands and allow normal command processing
1152 * from now on.
1153 */
1154 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
1155 if (IS_ERR(skb)) {
1156 BT_ERR("%s sending initial HCI reset command failed (%ld)",
1157 hdev->name, PTR_ERR(skb));
1158 return PTR_ERR(skb);
1159 }
1160 kfree_skb(skb);
1161
1162 /* Read Intel specific controller version first to allow selection of
1163 * which firmware file to load.
1164 *
1165 * The returned information are hardware variant and revision plus
1166 * firmware variant, revision and build number.
1167 */
1168 skb = __hci_cmd_sync(hdev, 0xfc05, 0, NULL, HCI_INIT_TIMEOUT);
1169 if (IS_ERR(skb)) {
1170 BT_ERR("%s reading Intel fw version command failed (%ld)",
1171 hdev->name, PTR_ERR(skb));
1172 return PTR_ERR(skb);
1173 }
1174
1175 if (skb->len != sizeof(*ver)) {
1176 BT_ERR("%s Intel version event length mismatch", hdev->name);
1177 kfree_skb(skb);
1178 return -EIO;
1179 }
1180
1181 ver = (struct intel_version *)skb->data;
1182 if (ver->status) {
1183 BT_ERR("%s Intel fw version event failed (%02x)", hdev->name,
1184 ver->status);
1185 kfree_skb(skb);
1186 return -bt_to_errno(ver->status);
1187 }
1188
1189 BT_INFO("%s: read Intel version: %02x%02x%02x%02x%02x%02x%02x%02x%02x",
1190 hdev->name, ver->hw_platform, ver->hw_variant,
1191 ver->hw_revision, ver->fw_variant, ver->fw_revision,
1192 ver->fw_build_num, ver->fw_build_ww, ver->fw_build_yy,
1193 ver->fw_patch_num);
1194
1195 /* fw_patch_num indicates the version of patch the device currently
1196 * have. If there is no patch data in the device, it is always 0x00.
1197 * So, if it is other than 0x00, no need to patch the deivce again.
1198 */
1199 if (ver->fw_patch_num) {
1200 BT_INFO("%s: Intel device is already patched. patch num: %02x",
1201 hdev->name, ver->fw_patch_num);
1202 kfree_skb(skb);
1203 return 0;
1204 }
1205
1206 /* Opens the firmware patch file based on the firmware version read
1207 * from the controller. If it fails to open the matching firmware
1208 * patch file, it tries to open the default firmware patch file.
1209 * If no patch file is found, allow the device to operate without
1210 * a patch.
1211 */
1212 fw = btusb_setup_intel_get_fw(hdev, ver);
1213 if (!fw) {
1214 kfree_skb(skb);
1215 return 0;
1216 }
1217 fw_ptr = fw->data;
1218
1219 /* This Intel specific command enables the manufacturer mode of the
1220 * controller.
1221 *
1222 * Only while this mode is enabled, the driver can download the
1223 * firmware patch data and configuration parameters.
1224 */
1225 skb = __hci_cmd_sync(hdev, 0xfc11, 2, mfg_enable, HCI_INIT_TIMEOUT);
1226 if (IS_ERR(skb)) {
1227 BT_ERR("%s entering Intel manufacturer mode failed (%ld)",
1228 hdev->name, PTR_ERR(skb));
1229 release_firmware(fw);
1230 return PTR_ERR(skb);
1231 }
1232
1233 if (skb->data[0]) {
1234 u8 evt_status = skb->data[0];
1235 BT_ERR("%s enable Intel manufacturer mode event failed (%02x)",
1236 hdev->name, evt_status);
1237 kfree_skb(skb);
1238 release_firmware(fw);
1239 return -bt_to_errno(evt_status);
1240 }
1241 kfree_skb(skb);
1242
1243 disable_patch = 1;
1244
1245 /* The firmware data file consists of list of Intel specific HCI
1246 * commands and its expected events. The first byte indicates the
1247 * type of the message, either HCI command or HCI event.
1248 *
1249 * It reads the command and its expected event from the firmware file,
1250 * and send to the controller. Once __hci_cmd_sync_ev() returns,
1251 * the returned event is compared with the event read from the firmware
1252 * file and it will continue until all the messages are downloaded to
1253 * the controller.
1254 *
1255 * Once the firmware patching is completed successfully,
1256 * the manufacturer mode is disabled with reset and activating the
1257 * downloaded patch.
1258 *
1259 * If the firmware patching fails, the manufacturer mode is
1260 * disabled with reset and deactivating the patch.
1261 *
1262 * If the default patch file is used, no reset is done when disabling
1263 * the manufacturer.
1264 */
1265 while (fw->size > fw_ptr - fw->data) {
1266 int ret;
1267
1268 ret = btusb_setup_intel_patching(hdev, fw, &fw_ptr,
1269 &disable_patch);
1270 if (ret < 0)
1271 goto exit_mfg_deactivate;
1272 }
1273
1274 release_firmware(fw);
1275
1276 if (disable_patch)
1277 goto exit_mfg_disable;
1278
1279 /* Patching completed successfully and disable the manufacturer mode
1280 * with reset and activate the downloaded firmware patches.
1281 */
1282 skb = __hci_cmd_sync(hdev, 0xfc11, sizeof(mfg_reset_activate),
1283 mfg_reset_activate, HCI_INIT_TIMEOUT);
1284 if (IS_ERR(skb)) {
1285 BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
1286 hdev->name, PTR_ERR(skb));
1287 return PTR_ERR(skb);
1288 }
1289 kfree_skb(skb);
1290
1291 BT_INFO("%s: Intel Bluetooth firmware patch completed and activated",
1292 hdev->name);
1293
1294 return 0;
1295
1296exit_mfg_disable:
1297 /* Disable the manufacturer mode without reset */
1298 skb = __hci_cmd_sync(hdev, 0xfc11, sizeof(mfg_disable), mfg_disable,
1299 HCI_INIT_TIMEOUT);
1300 if (IS_ERR(skb)) {
1301 BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
1302 hdev->name, PTR_ERR(skb));
1303 return PTR_ERR(skb);
1304 }
1305 kfree_skb(skb);
1306
1307 BT_INFO("%s: Intel Bluetooth firmware patch completed", hdev->name);
1308 return 0;
1309
1310exit_mfg_deactivate:
1311 release_firmware(fw);
1312
1313 /* Patching failed. Disable the manufacturer mode with reset and
1314 * deactivate the downloaded firmware patches.
1315 */
1316 skb = __hci_cmd_sync(hdev, 0xfc11, sizeof(mfg_reset_deactivate),
1317 mfg_reset_deactivate, HCI_INIT_TIMEOUT);
1318 if (IS_ERR(skb)) {
1319 BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
1320 hdev->name, PTR_ERR(skb));
1321 return PTR_ERR(skb);
1322 }
1323 kfree_skb(skb);
1324
1325 BT_INFO("%s: Intel Bluetooth firmware patch completed and deactivated",
1326 hdev->name);
1327
1328 return 0;
1329}
1330
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001331static int btusb_probe(struct usb_interface *intf,
1332 const struct usb_device_id *id)
1333{
1334 struct usb_endpoint_descriptor *ep_desc;
1335 struct btusb_data *data;
1336 struct hci_dev *hdev;
1337 int i, err;
1338
1339 BT_DBG("intf %p id %p", intf, id);
1340
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02001341 /* interface numbers are hardcoded in the spec */
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001342 if (intf->cur_altsetting->desc.bInterfaceNumber != 0)
1343 return -ENODEV;
1344
1345 if (!id->driver_info) {
1346 const struct usb_device_id *match;
1347 match = usb_match_id(intf, blacklist_table);
1348 if (match)
1349 id = match;
1350 }
1351
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02001352 if (id->driver_info == BTUSB_IGNORE)
1353 return -ENODEV;
1354
1355 if (ignore_dga && id->driver_info & BTUSB_DIGIANSWER)
1356 return -ENODEV;
1357
1358 if (ignore_csr && id->driver_info & BTUSB_CSR)
1359 return -ENODEV;
1360
1361 if (ignore_sniffer && id->driver_info & BTUSB_SNIFFER)
1362 return -ENODEV;
1363
Steven.Li4770ac22011-07-01 14:02:36 +08001364 if (id->driver_info & BTUSB_ATH3012) {
1365 struct usb_device *udev = interface_to_usbdev(intf);
1366
1367 /* Old firmware would otherwise let ath3k driver load
1368 * patch and sysconfig files */
1369 if (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x0001)
1370 return -ENODEV;
1371 }
1372
flintman9892c402015-09-23 06:14:10 -04001373 data = devm_kzalloc(&intf->dev, sizeof(*data), GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001374 if (!data)
1375 return -ENOMEM;
1376
1377 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
1378 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
1379
1380 if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
1381 data->intr_ep = ep_desc;
1382 continue;
1383 }
1384
1385 if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
1386 data->bulk_tx_ep = ep_desc;
1387 continue;
1388 }
1389
1390 if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
1391 data->bulk_rx_ep = ep_desc;
1392 continue;
1393 }
1394 }
1395
flintman9892c402015-09-23 06:14:10 -04001396 if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001397 return -ENODEV;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001398
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01001399 data->cmdreq_type = USB_TYPE_CLASS;
1400
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001401 data->udev = interface_to_usbdev(intf);
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02001402 data->intf = intf;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001403
1404 spin_lock_init(&data->lock);
1405
1406 INIT_WORK(&data->work, btusb_work);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001407 INIT_WORK(&data->waker, btusb_waker);
1408 spin_lock_init(&data->txlock);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001409
1410 init_usb_anchor(&data->tx_anchor);
1411 init_usb_anchor(&data->intr_anchor);
1412 init_usb_anchor(&data->bulk_anchor);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001413 init_usb_anchor(&data->isoc_anchor);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001414 init_usb_anchor(&data->deferred);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001415
1416 hdev = hci_alloc_dev();
flintman9892c402015-09-23 06:14:10 -04001417 if (!hdev)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001418 return -ENOMEM;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001419
Marcel Holtmannc13854ce2010-02-08 15:27:07 +01001420 hdev->bus = HCI_USB;
flintman9892c402015-09-23 06:14:10 -04001421 hci_set_drvdata(hdev, data);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001422
1423 data->hdev = hdev;
1424
1425 SET_HCIDEV_DEV(hdev, &intf->dev);
1426
flintman9892c402015-09-23 06:14:10 -04001427 hdev->open = btusb_open;
1428 hdev->close = btusb_close;
1429 hdev->flush = btusb_flush;
1430 hdev->send = btusb_send_frame;
1431 hdev->notify = btusb_notify;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001432
flintman9892c402015-09-23 06:14:10 -04001433 if (id->driver_info & BTUSB_BCM92035)
1434 hdev->setup = btusb_setup_bcm92035;
1435
1436 if (id->driver_info & BTUSB_INTEL)
1437 hdev->setup = btusb_setup_intel;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001438
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01001439 /* Interface numbers are hardcoded in the specification */
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001440 data->isoc = usb_ifnum_to_if(data->udev, 1);
1441
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01001442 if (!reset)
flintman9892c402015-09-23 06:14:10 -04001443 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02001444
1445 if (force_scofix || id->driver_info & BTUSB_WRONG_SCO_MTU) {
1446 if (!disable_scofix)
1447 set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
1448 }
1449
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001450 if (id->driver_info & BTUSB_BROKEN_ISOC)
1451 data->isoc = NULL;
1452
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01001453 if (id->driver_info & BTUSB_DIGIANSWER) {
1454 data->cmdreq_type = USB_TYPE_VENDOR;
flintman9892c402015-09-23 06:14:10 -04001455 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01001456 }
1457
1458 if (id->driver_info & BTUSB_CSR) {
1459 struct usb_device *udev = data->udev;
1460
1461 /* Old firmware would otherwise execute USB reset */
1462 if (le16_to_cpu(udev->descriptor.bcdDevice) < 0x117)
flintman9892c402015-09-23 06:14:10 -04001463 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01001464 }
1465
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02001466 if (id->driver_info & BTUSB_SNIFFER) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001467 struct usb_device *udev = data->udev;
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02001468
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01001469 /* New sniffer firmware has crippled HCI interface */
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02001470 if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
1471 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001472
1473 data->isoc = NULL;
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02001474 }
1475
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001476 if (data->isoc) {
1477 err = usb_driver_claim_interface(&btusb_driver,
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02001478 data->isoc, data);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001479 if (err < 0) {
1480 hci_free_dev(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001481 return err;
1482 }
1483 }
1484
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001485 err = hci_register_dev(hdev);
1486 if (err < 0) {
1487 hci_free_dev(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001488 return err;
1489 }
1490
1491 usb_set_intfdata(intf, data);
1492
1493 return 0;
1494}
1495
1496static void btusb_disconnect(struct usb_interface *intf)
1497{
1498 struct btusb_data *data = usb_get_intfdata(intf);
1499 struct hci_dev *hdev;
1500
1501 BT_DBG("intf %p", intf);
1502
1503 if (!data)
1504 return;
1505
1506 hdev = data->hdev;
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02001507 usb_set_intfdata(data->intf, NULL);
1508
1509 if (data->isoc)
1510 usb_set_intfdata(data->isoc, NULL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001511
1512 hci_unregister_dev(hdev);
1513
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02001514 if (intf == data->isoc)
1515 usb_driver_release_interface(&btusb_driver, data->intf);
1516 else if (data->isoc)
1517 usb_driver_release_interface(&btusb_driver, data->isoc);
1518
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001519 hci_free_dev(hdev);
1520}
1521
Oliver Neukum7bee5492009-08-24 23:44:59 +02001522#ifdef CONFIG_PM
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001523static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
1524{
1525 struct btusb_data *data = usb_get_intfdata(intf);
1526
1527 BT_DBG("intf %p", intf);
1528
1529 if (data->suspend_count++)
1530 return 0;
1531
Oliver Neukum7bee5492009-08-24 23:44:59 +02001532 spin_lock_irq(&data->txlock);
flintman9892c402015-09-23 06:14:10 -04001533 if (!(PMSG_IS_AUTO(message) && data->tx_in_flight)) {
Oliver Neukum7bee5492009-08-24 23:44:59 +02001534 set_bit(BTUSB_SUSPENDING, &data->flags);
1535 spin_unlock_irq(&data->txlock);
1536 } else {
1537 spin_unlock_irq(&data->txlock);
1538 data->suspend_count--;
1539 return -EBUSY;
1540 }
1541
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001542 cancel_work_sync(&data->work);
1543
Oliver Neukum7bee5492009-08-24 23:44:59 +02001544 btusb_stop_traffic(data);
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001545 usb_kill_anchored_urbs(&data->tx_anchor);
1546
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001547 return 0;
1548}
1549
Oliver Neukum7bee5492009-08-24 23:44:59 +02001550static void play_deferred(struct btusb_data *data)
1551{
1552 struct urb *urb;
1553 int err;
1554
1555 while ((urb = usb_get_from_anchor(&data->deferred))) {
1556 err = usb_submit_urb(urb, GFP_ATOMIC);
1557 if (err < 0)
1558 break;
1559
1560 data->tx_in_flight++;
1561 }
1562 usb_scuttle_anchored_urbs(&data->deferred);
1563}
1564
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001565static int btusb_resume(struct usb_interface *intf)
1566{
1567 struct btusb_data *data = usb_get_intfdata(intf);
1568 struct hci_dev *hdev = data->hdev;
Oliver Neukum7bee5492009-08-24 23:44:59 +02001569 int err = 0;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001570
1571 BT_DBG("intf %p", intf);
1572
1573 if (--data->suspend_count)
1574 return 0;
1575
1576 if (!test_bit(HCI_RUNNING, &hdev->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +02001577 goto done;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001578
1579 if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
1580 err = btusb_submit_intr_urb(hdev, GFP_NOIO);
1581 if (err < 0) {
1582 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001583 goto failed;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001584 }
1585 }
1586
1587 if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) {
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001588 err = btusb_submit_bulk_urb(hdev, GFP_NOIO);
1589 if (err < 0) {
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001590 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001591 goto failed;
1592 }
1593
1594 btusb_submit_bulk_urb(hdev, GFP_NOIO);
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001595 }
1596
1597 if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
1598 if (btusb_submit_isoc_urb(hdev, GFP_NOIO) < 0)
1599 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1600 else
1601 btusb_submit_isoc_urb(hdev, GFP_NOIO);
1602 }
1603
Oliver Neukum7bee5492009-08-24 23:44:59 +02001604 spin_lock_irq(&data->txlock);
1605 play_deferred(data);
1606 clear_bit(BTUSB_SUSPENDING, &data->flags);
1607 spin_unlock_irq(&data->txlock);
1608 schedule_work(&data->work);
1609
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001610 return 0;
Oliver Neukum7bee5492009-08-24 23:44:59 +02001611
1612failed:
1613 usb_scuttle_anchored_urbs(&data->deferred);
1614done:
1615 spin_lock_irq(&data->txlock);
1616 clear_bit(BTUSB_SUSPENDING, &data->flags);
1617 spin_unlock_irq(&data->txlock);
1618
1619 return err;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001620}
Oliver Neukum7bee5492009-08-24 23:44:59 +02001621#endif
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001622
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001623static struct usb_driver btusb_driver = {
1624 .name = "btusb",
1625 .probe = btusb_probe,
1626 .disconnect = btusb_disconnect,
Oliver Neukum7bee5492009-08-24 23:44:59 +02001627#ifdef CONFIG_PM
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001628 .suspend = btusb_suspend,
1629 .resume = btusb_resume,
Oliver Neukum7bee5492009-08-24 23:44:59 +02001630#endif
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001631 .id_table = btusb_table,
Oliver Neukum7bee5492009-08-24 23:44:59 +02001632 .supports_autosuspend = 1,
flintman9892c402015-09-23 06:14:10 -04001633 .disable_hub_initiated_lpm = 1,
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001634};
1635
flintman9892c402015-09-23 06:14:10 -04001636module_usb_driver(btusb_driver);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001637
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02001638module_param(ignore_dga, bool, 0644);
1639MODULE_PARM_DESC(ignore_dga, "Ignore devices with id 08fd:0001");
1640
1641module_param(ignore_csr, bool, 0644);
1642MODULE_PARM_DESC(ignore_csr, "Ignore devices with id 0a12:0001");
1643
1644module_param(ignore_sniffer, bool, 0644);
1645MODULE_PARM_DESC(ignore_sniffer, "Ignore devices with id 0a12:0002");
1646
1647module_param(disable_scofix, bool, 0644);
1648MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
1649
1650module_param(force_scofix, bool, 0644);
1651MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
1652
1653module_param(reset, bool, 0644);
1654MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
1655
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001656MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
1657MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
1658MODULE_VERSION(VERSION);
1659MODULE_LICENSE("GPL");