blob: 7cefd58efb40182bf770408e373e5443fb36072e [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
24#include <linux/kernel.h>
25#include <linux/module.h>
26#include <linux/init.h>
27#include <linux/slab.h>
28#include <linux/types.h>
29#include <linux/sched.h>
30#include <linux/errno.h>
31#include <linux/skbuff.h>
32
33#include <linux/usb.h>
34
35#include <net/bluetooth/bluetooth.h>
36#include <net/bluetooth/hci_core.h>
37
Oliver Neukum7bee5492009-08-24 23:44:59 +020038#define VERSION "0.6"
Marcel Holtmanncfeb4142008-08-07 22:26:56 +020039
Rusty Russell90ab5ee2012-01-13 09:32:20 +103040static bool ignore_dga;
41static bool ignore_csr;
42static bool ignore_sniffer;
43static bool disable_scofix;
44static bool force_scofix;
Marcel Holtmann7a9d4022008-11-30 12:17:26 +010045
Rusty Russell90ab5ee2012-01-13 09:32:20 +103046static bool reset = 1;
Marcel Holtmanncfeb4142008-08-07 22:26:56 +020047
48static struct usb_driver btusb_driver;
49
50#define BTUSB_IGNORE 0x01
Marcel Holtmann7a9d4022008-11-30 12:17:26 +010051#define BTUSB_DIGIANSWER 0x02
52#define BTUSB_CSR 0x04
53#define BTUSB_SNIFFER 0x08
54#define BTUSB_BCM92035 0x10
55#define BTUSB_BROKEN_ISOC 0x20
56#define BTUSB_WRONG_SCO_MTU 0x40
Steven.Li2d25f8b2011-07-01 14:02:36 +080057#define BTUSB_ATH3012 0x80
Marcel Holtmann5e23b922007-10-20 14:12:34 +020058
59static struct usb_device_id btusb_table[] = {
60 /* Generic Bluetooth USB device */
61 { USB_DEVICE_INFO(0xe0, 0x01, 0x01) },
62
Henrik Rydberg403c9eb2012-08-25 19:28:06 +020063 /* Apple-specific (Broadcom) devices */
64 { USB_VENDOR_AND_INTERFACE_INFO(0x05ac, 0xff, 0x01, 0x01) },
65
Cho, Yu-Chen5ff17152013-06-04 21:40:26 +080066 /* MediaTek MT76x0E */
67 { USB_DEVICE(0x0e8d, 0x763f) },
68
Oliver Neukumc510eae2011-09-21 11:41:45 +020069 /* Broadcom SoftSailing reporting vendor specific */
Don Zickus2e8b5062012-03-28 16:41:11 -040070 { USB_DEVICE(0x0a5c, 0x21e1) },
Oliver Neukumc510eae2011-09-21 11:41:45 +020071
Nobuhiro Iwamatsu3cd01972010-08-20 16:24:07 +090072 /* Apple MacBookPro 7,1 */
73 { USB_DEVICE(0x05ac, 0x8213) },
74
Cyril Lacoux0a79f672010-07-14 10:29:27 +040075 /* Apple iMac11,1 */
76 { USB_DEVICE(0x05ac, 0x8215) },
77
Nobuhiro Iwamatsu9c047152010-08-20 16:24:06 +090078 /* Apple MacBookPro6,2 */
79 { USB_DEVICE(0x05ac, 0x8218) },
80
Edgar (gimli) Hucek3e3ede72010-11-04 08:04:33 +010081 /* Apple MacBookAir3,1, MacBookAir3,2 */
82 { USB_DEVICE(0x05ac, 0x821b) },
83
Pieter-Augustijn Van Malleghema63b7232011-09-07 02:28:10 -040084 /* Apple MacBookAir4,1 */
85 { USB_DEVICE(0x05ac, 0x821f) },
86
Marc-Antoine Perennou88d377b2011-03-24 14:51:21 -030087 /* Apple MacBookPro8,2 */
88 { USB_DEVICE(0x05ac, 0x821a) },
89
Jurgen Kramerf78b6822011-09-04 18:01:42 +020090 /* Apple MacMini5,1 */
91 { USB_DEVICE(0x05ac, 0x8281) },
92
Marcel Holtmanncfeb4142008-08-07 22:26:56 +020093 /* AVM BlueFRITZ! USB v2.0 */
94 { USB_DEVICE(0x057c, 0x3800) },
95
96 /* Bluetooth Ultraport Module from IBM */
97 { USB_DEVICE(0x04bf, 0x030a) },
98
99 /* ALPS Modules with non-standard id */
100 { USB_DEVICE(0x044e, 0x3001) },
101 { USB_DEVICE(0x044e, 0x3002) },
102
103 /* Ericsson with non-standard id */
104 { USB_DEVICE(0x0bdb, 0x1002) },
105
106 /* Canyon CN-BTU1 with HID interfaces */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100107 { USB_DEVICE(0x0c10, 0x0000) },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200108
Wen-chien Jesse Sungd13431c2011-11-08 14:30:22 +0800109 /* Broadcom BCM20702A0 */
Jeff Cook4a990f62012-11-09 16:39:48 -0700110 { USB_DEVICE(0x0b05, 0x17b5) },
Jaroslav Resleradebaa72012-09-11 17:25:32 +0800111 { USB_DEVICE(0x04ca, 0x2003) },
Manoj Iyer79cd7602012-04-09 09:22:28 -0500112 { USB_DEVICE(0x0489, 0xe042) },
Wen-chien Jesse Sungd13431c2011-11-08 14:30:22 +0800113 { USB_DEVICE(0x413c, 0x8197) },
114
Steven Harms98514032012-04-13 14:45:55 -0400115 /* Foxconn - Hon Hai */
Gustavo Padovanec5bf542012-08-15 01:38:11 -0300116 { USB_VENDOR_AND_INTERFACE_INFO(0x0489, 0xff, 0x01, 0x01) },
Steven Harms98514032012-04-13 14:45:55 -0400117
Gustavo Padovan004251b2012-08-06 15:36:49 -0300118 /*Broadcom devices with vendor specific id */
119 { USB_VENDOR_AND_INTERFACE_INFO(0x0a5c, 0xff, 0x01, 0x01) },
120
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200121 { } /* Terminating entry */
122};
123
124MODULE_DEVICE_TABLE(usb, btusb_table);
125
126static struct usb_device_id blacklist_table[] = {
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200127 /* CSR BlueCore devices */
128 { USB_DEVICE(0x0a12, 0x0001), .driver_info = BTUSB_CSR },
129
130 /* Broadcom BCM2033 without firmware */
131 { USB_DEVICE(0x0a5c, 0x2033), .driver_info = BTUSB_IGNORE },
132
Bala Shanmugambe931122010-11-26 17:35:46 +0530133 /* Atheros 3011 with sflash firmware */
134 { USB_DEVICE(0x0cf3, 0x3002), .driver_info = BTUSB_IGNORE },
Marek Vasut7f801fc2012-06-08 14:32:50 +0200135 { USB_DEVICE(0x0cf3, 0xe019), .driver_info = BTUSB_IGNORE },
Andy Ross2a7bccc2011-05-09 16:11:16 -0700136 { USB_DEVICE(0x13d3, 0x3304), .driver_info = BTUSB_IGNORE },
Ricardo Mendoza8e7c3d22011-07-13 16:04:29 +0100137 { USB_DEVICE(0x0930, 0x0215), .driver_info = BTUSB_IGNORE },
Keng-Yu Lin6b6ba882011-11-30 18:32:37 +0800138 { USB_DEVICE(0x0489, 0xe03d), .driver_info = BTUSB_IGNORE },
Marcos Chaparro2145cea2012-11-06 16:19:11 -0300139 { USB_DEVICE(0x0489, 0xe027), .driver_info = BTUSB_IGNORE },
Bala Shanmugambe931122010-11-26 17:35:46 +0530140
Cho, Yu-Chen509e7862011-01-26 17:10:59 +0800141 /* Atheros AR9285 Malbec with sflash firmware */
142 { USB_DEVICE(0x03f0, 0x311d), .driver_info = BTUSB_IGNORE },
143
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530144 /* Atheros 3012 with sflash firmware */
Ming Lei42c4b742013-03-15 11:00:39 +0800145 { USB_DEVICE(0x0cf3, 0x0036), .driver_info = BTUSB_ATH3012 },
Steven.Li2d25f8b2011-07-01 14:02:36 +0800146 { USB_DEVICE(0x0cf3, 0x3004), .driver_info = BTUSB_ATH3012 },
Sunguk Lee1a182cc2013-03-12 04:41:58 +0900147 { USB_DEVICE(0x0cf3, 0x3008), .driver_info = BTUSB_ATH3012 },
Cho, Yu-Chen07c0ea82012-03-14 22:01:21 +0200148 { USB_DEVICE(0x0cf3, 0x311d), .driver_info = BTUSB_ATH3012 },
Ming Leic643e012013-03-18 23:45:11 +0800149 { USB_DEVICE(0x0cf3, 0x817a), .driver_info = BTUSB_ATH3012 },
Eran9498ba72011-12-05 22:15:29 +0000150 { USB_DEVICE(0x13d3, 0x3375), .driver_info = BTUSB_ATH3012 },
Josh Boyerd83ff8c2013-02-19 11:54:16 -0500151 { USB_DEVICE(0x04ca, 0x3004), .driver_info = BTUSB_ATH3012 },
AceLan Kao55ed7d42012-03-28 10:25:36 +0800152 { USB_DEVICE(0x04ca, 0x3005), .driver_info = BTUSB_ATH3012 },
Daniel Schaal7a8c99e2012-12-29 11:14:34 +0100153 { USB_DEVICE(0x04ca, 0x3006), .driver_info = BTUSB_ATH3012 },
Sergio Cambrae98f05e2013-01-10 01:06:55 +0100154 { USB_DEVICE(0x04ca, 0x3008), .driver_info = BTUSB_ATH3012 },
AceLan Kao87522a42012-04-13 17:39:57 +0800155 { USB_DEVICE(0x13d3, 0x3362), .driver_info = BTUSB_ATH3012 },
AceLan Kaoac713112012-04-19 14:53:45 +0800156 { USB_DEVICE(0x0cf3, 0xe004), .driver_info = BTUSB_ATH3012 },
Peng Chen140deab2013-08-30 17:41:40 +0800157 { USB_DEVICE(0x0cf3, 0xe005), .driver_info = BTUSB_ATH3012 },
Giancarlo Formicucciac2f15c32012-06-10 08:33:11 +0200158 { USB_DEVICE(0x0930, 0x0219), .driver_info = BTUSB_ATH3012 },
Peng Chen4126da52012-08-01 10:11:59 +0800159 { USB_DEVICE(0x0489, 0xe057), .driver_info = BTUSB_ATH3012 },
AceLan Kao3b36e952012-12-11 11:41:20 +0800160 { USB_DEVICE(0x13d3, 0x3393), .driver_info = BTUSB_ATH3012 },
AceLan Kao340733c2013-01-03 12:24:59 +0800161 { USB_DEVICE(0x0489, 0xe04e), .driver_info = BTUSB_ATH3012 },
AceLan Kao7a344a222013-01-03 12:25:00 +0800162 { USB_DEVICE(0x0489, 0xe056), .driver_info = BTUSB_ATH3012 },
Noguchi Kazutosi27370722013-03-24 23:41:10 +0900163 { USB_DEVICE(0x0489, 0xe04d), .driver_info = BTUSB_ATH3012 },
Thomas Loo61286bc2013-07-03 02:53:54 +0200164 { USB_DEVICE(0x04c5, 0x1330), .driver_info = BTUSB_ATH3012 },
Sujith Manoharanfa6e6072013-07-15 09:29:03 +0530165 { USB_DEVICE(0x13d3, 0x3402), .driver_info = BTUSB_ATH3012 },
AceLan Kaod9b03b22013-07-17 11:27:40 +0800166 { USB_DEVICE(0x0cf3, 0x3121), .driver_info = BTUSB_ATH3012 },
AceLan Kaoae1c7272013-06-20 13:38:45 +0800167 { USB_DEVICE(0x0cf3, 0xe003), .driver_info = BTUSB_ATH3012 },
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530168
Cho, Yu-Chene9036e32011-02-15 10:20:07 +0800169 /* Atheros AR5BBU12 with sflash firmware */
170 { USB_DEVICE(0x0489, 0xe02c), .driver_info = BTUSB_IGNORE },
171
Michael Gruetznera1144ca2012-05-02 22:33:40 +0200172 /* Atheros AR5BBU12 with sflash firmware */
173 { USB_DEVICE(0x0489, 0xe03c), .driver_info = BTUSB_ATH3012 },
Yevgeniy Melnichuk1403b362012-08-07 19:48:10 +0530174 { USB_DEVICE(0x0489, 0xe036), .driver_info = BTUSB_ATH3012 },
Michael Gruetznera1144ca2012-05-02 22:33:40 +0200175
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200176 /* Broadcom BCM2035 */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100177 { USB_DEVICE(0x0a5c, 0x2035), .driver_info = BTUSB_WRONG_SCO_MTU },
178 { USB_DEVICE(0x0a5c, 0x200a), .driver_info = BTUSB_WRONG_SCO_MTU },
179 { USB_DEVICE(0x0a5c, 0x2009), .driver_info = BTUSB_BCM92035 },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200180
181 /* Broadcom BCM2045 */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100182 { USB_DEVICE(0x0a5c, 0x2039), .driver_info = BTUSB_WRONG_SCO_MTU },
183 { USB_DEVICE(0x0a5c, 0x2101), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmannbdbef3d2008-09-23 00:16:35 +0200184
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200185 /* IBM/Lenovo ThinkPad with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100186 { USB_DEVICE(0x0a5c, 0x201e), .driver_info = BTUSB_WRONG_SCO_MTU },
187 { USB_DEVICE(0x0a5c, 0x2110), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200188
189 /* HP laptop with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100190 { USB_DEVICE(0x03f0, 0x171d), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200191
192 /* Dell laptop with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100193 { USB_DEVICE(0x413c, 0x8126), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200194
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100195 /* Dell Wireless 370 and 410 devices */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100196 { USB_DEVICE(0x413c, 0x8152), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100197 { USB_DEVICE(0x413c, 0x8156), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200198
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100199 /* Belkin F8T012 and F8T013 devices */
200 { USB_DEVICE(0x050d, 0x0012), .driver_info = BTUSB_WRONG_SCO_MTU },
201 { USB_DEVICE(0x050d, 0x0013), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200202
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100203 /* Asus WL-BTD202 device */
204 { USB_DEVICE(0x0b05, 0x1715), .driver_info = BTUSB_WRONG_SCO_MTU },
205
206 /* Kensington Bluetooth USB adapter */
207 { USB_DEVICE(0x047d, 0x105e), .driver_info = BTUSB_WRONG_SCO_MTU },
208
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200209 /* RTX Telecom based adapters with buggy SCO support */
210 { USB_DEVICE(0x0400, 0x0807), .driver_info = BTUSB_BROKEN_ISOC },
211 { USB_DEVICE(0x0400, 0x080a), .driver_info = BTUSB_BROKEN_ISOC },
212
213 /* CONWISE Technology based adapters with buggy SCO support */
214 { USB_DEVICE(0x0e5e, 0x6622), .driver_info = BTUSB_BROKEN_ISOC },
215
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200216 /* Digianswer devices */
217 { USB_DEVICE(0x08fd, 0x0001), .driver_info = BTUSB_DIGIANSWER },
218 { USB_DEVICE(0x08fd, 0x0002), .driver_info = BTUSB_IGNORE },
219
220 /* CSR BlueCore Bluetooth Sniffer */
221 { USB_DEVICE(0x0a12, 0x0002), .driver_info = BTUSB_SNIFFER },
222
223 /* Frontline ComProbe Bluetooth Sniffer */
224 { USB_DEVICE(0x16d3, 0x0002), .driver_info = BTUSB_SNIFFER },
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;
David Herrmann155961e2012-02-09 21:58:32 +0100288 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) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200316 /* -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{
David Herrmann155961e2012-02-09 21:58:32 +0100327 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) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +0200362 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;
David Herrmann155961e2012-02-09 21:58:32 +0100376 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) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200404 /* -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{
David Herrmann155961e2012-02-09 21:58:32 +0100415 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) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +0200448 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;
David Herrmann155961e2012-02-09 21:58:32 +0100462 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) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200497 /* -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
Jesper Juhl42b16b32011-01-17 00:09:38 +0100506static 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{
David Herrmann155961e2012-02-09 21:58:32 +0100529 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
Bing Zhaofa0fb932011-12-20 18:19:00 -0800555 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) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +0200567 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;
David Herrmann155961e2012-02-09 21:58:32 +0100582 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{
David Herrmann155961e2012-02-09 21:58:32 +0100629 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{
David Herrmann155961e2012-02-09 21:58:32 +0100679 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{
David Herrmann155961e2012-02-09 21:58:32 +0100709 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;
David Herrmann155961e2012-02-09 21:58:32 +0100721 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:
Peter Hurley9fd481e2011-07-14 08:48:32 -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) {
Paul Bolle5a9b80e2011-10-09 12:12:16 +0200815 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:
Cong Wang54a8a792011-11-22 09:32:57 +0800825 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{
David Herrmann155961e2012-02-09 21:58:32 +0100831 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
Jesper Juhl42b16b32011-01-17 00:09:38 +0100841static inline int __set_isoc_interface(struct hci_dev *hdev, int altsetting)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200842{
David Herrmann155961e2012-02-09 21:58:32 +0100843 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;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200888 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200889
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200890 if (hdev->conn_hash.sco_num > 0) {
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -0300891 if (!test_bit(BTUSB_DID_ISO_RESUME, &data->flags)) {
Oliver Neukum8efdd0c2011-02-11 13:00:06 +0100892 err = usb_autopm_get_interface(data->isoc ? data->isoc : data->intf);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200893 if (err < 0) {
894 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
895 usb_kill_anchored_urbs(&data->isoc_anchor);
896 return;
897 }
898
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -0300899 set_bit(BTUSB_DID_ISO_RESUME, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200900 }
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200901 if (data->isoc_altsetting != 2) {
902 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
903 usb_kill_anchored_urbs(&data->isoc_anchor);
904
905 if (__set_isoc_interface(hdev, 2) < 0)
906 return;
907 }
908
909 if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100910 if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200911 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
912 else
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100913 btusb_submit_isoc_urb(hdev, GFP_KERNEL);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200914 }
915 } else {
916 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
917 usb_kill_anchored_urbs(&data->isoc_anchor);
918
919 __set_isoc_interface(hdev, 0);
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -0300920 if (test_and_clear_bit(BTUSB_DID_ISO_RESUME, &data->flags))
Oliver Neukum8efdd0c2011-02-11 13:00:06 +0100921 usb_autopm_put_interface(data->isoc ? data->isoc : data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200922 }
923}
924
Oliver Neukum7bee5492009-08-24 23:44:59 +0200925static void btusb_waker(struct work_struct *work)
926{
927 struct btusb_data *data = container_of(work, struct btusb_data, waker);
928 int err;
929
930 err = usb_autopm_get_interface(data->intf);
931 if (err < 0)
932 return;
933
934 usb_autopm_put_interface(data->intf);
935}
936
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200937static int btusb_probe(struct usb_interface *intf,
938 const struct usb_device_id *id)
939{
940 struct usb_endpoint_descriptor *ep_desc;
941 struct btusb_data *data;
942 struct hci_dev *hdev;
943 int i, err;
944
945 BT_DBG("intf %p id %p", intf, id);
946
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200947 /* interface numbers are hardcoded in the spec */
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200948 if (intf->cur_altsetting->desc.bInterfaceNumber != 0)
949 return -ENODEV;
950
951 if (!id->driver_info) {
952 const struct usb_device_id *match;
953 match = usb_match_id(intf, blacklist_table);
954 if (match)
955 id = match;
956 }
957
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200958 if (id->driver_info == BTUSB_IGNORE)
959 return -ENODEV;
960
961 if (ignore_dga && id->driver_info & BTUSB_DIGIANSWER)
962 return -ENODEV;
963
964 if (ignore_csr && id->driver_info & BTUSB_CSR)
965 return -ENODEV;
966
967 if (ignore_sniffer && id->driver_info & BTUSB_SNIFFER)
968 return -ENODEV;
969
Steven.Li2d25f8b2011-07-01 14:02:36 +0800970 if (id->driver_info & BTUSB_ATH3012) {
971 struct usb_device *udev = interface_to_usbdev(intf);
972
973 /* Old firmware would otherwise let ath3k driver load
974 * patch and sysconfig files */
975 if (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x0001)
976 return -ENODEV;
977 }
978
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200979 data = kzalloc(sizeof(*data), GFP_KERNEL);
980 if (!data)
981 return -ENOMEM;
982
983 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
984 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
985
986 if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
987 data->intr_ep = ep_desc;
988 continue;
989 }
990
991 if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
992 data->bulk_tx_ep = ep_desc;
993 continue;
994 }
995
996 if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
997 data->bulk_rx_ep = ep_desc;
998 continue;
999 }
1000 }
1001
1002 if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep) {
1003 kfree(data);
1004 return -ENODEV;
1005 }
1006
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01001007 data->cmdreq_type = USB_TYPE_CLASS;
1008
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001009 data->udev = interface_to_usbdev(intf);
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02001010 data->intf = intf;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001011
1012 spin_lock_init(&data->lock);
1013
1014 INIT_WORK(&data->work, btusb_work);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001015 INIT_WORK(&data->waker, btusb_waker);
1016 spin_lock_init(&data->txlock);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001017
1018 init_usb_anchor(&data->tx_anchor);
1019 init_usb_anchor(&data->intr_anchor);
1020 init_usb_anchor(&data->bulk_anchor);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001021 init_usb_anchor(&data->isoc_anchor);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001022 init_usb_anchor(&data->deferred);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001023
1024 hdev = hci_alloc_dev();
1025 if (!hdev) {
1026 kfree(data);
1027 return -ENOMEM;
1028 }
1029
Marcel Holtmannc13854ce2010-02-08 15:27:07 +01001030 hdev->bus = HCI_USB;
David Herrmann155961e2012-02-09 21:58:32 +01001031 hci_set_drvdata(hdev, data);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001032
1033 data->hdev = hdev;
1034
1035 SET_HCIDEV_DEV(hdev, &intf->dev);
1036
1037 hdev->open = btusb_open;
1038 hdev->close = btusb_close;
1039 hdev->flush = btusb_flush;
1040 hdev->send = btusb_send_frame;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001041 hdev->notify = btusb_notify;
1042
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01001043 /* Interface numbers are hardcoded in the specification */
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001044 data->isoc = usb_ifnum_to_if(data->udev, 1);
1045
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01001046 if (!reset)
1047 set_bit(HCI_QUIRK_NO_RESET, &hdev->quirks);
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02001048
1049 if (force_scofix || id->driver_info & BTUSB_WRONG_SCO_MTU) {
1050 if (!disable_scofix)
1051 set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
1052 }
1053
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001054 if (id->driver_info & BTUSB_BROKEN_ISOC)
1055 data->isoc = NULL;
1056
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01001057 if (id->driver_info & BTUSB_DIGIANSWER) {
1058 data->cmdreq_type = USB_TYPE_VENDOR;
1059 set_bit(HCI_QUIRK_NO_RESET, &hdev->quirks);
1060 }
1061
1062 if (id->driver_info & BTUSB_CSR) {
1063 struct usb_device *udev = data->udev;
1064
1065 /* Old firmware would otherwise execute USB reset */
1066 if (le16_to_cpu(udev->descriptor.bcdDevice) < 0x117)
1067 set_bit(HCI_QUIRK_NO_RESET, &hdev->quirks);
1068 }
1069
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02001070 if (id->driver_info & BTUSB_SNIFFER) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001071 struct usb_device *udev = data->udev;
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02001072
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01001073 /* New sniffer firmware has crippled HCI interface */
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02001074 if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
1075 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001076
1077 data->isoc = NULL;
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02001078 }
1079
1080 if (id->driver_info & BTUSB_BCM92035) {
1081 unsigned char cmd[] = { 0x3b, 0xfc, 0x01, 0x00 };
1082 struct sk_buff *skb;
1083
1084 skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL);
1085 if (skb) {
1086 memcpy(skb_put(skb, sizeof(cmd)), cmd, sizeof(cmd));
1087 skb_queue_tail(&hdev->driver_init, skb);
1088 }
1089 }
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001090
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001091 if (data->isoc) {
1092 err = usb_driver_claim_interface(&btusb_driver,
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02001093 data->isoc, data);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001094 if (err < 0) {
1095 hci_free_dev(hdev);
1096 kfree(data);
1097 return err;
1098 }
1099 }
1100
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001101 err = hci_register_dev(hdev);
1102 if (err < 0) {
1103 hci_free_dev(hdev);
1104 kfree(data);
1105 return err;
1106 }
1107
1108 usb_set_intfdata(intf, data);
1109
1110 return 0;
1111}
1112
1113static void btusb_disconnect(struct usb_interface *intf)
1114{
1115 struct btusb_data *data = usb_get_intfdata(intf);
1116 struct hci_dev *hdev;
1117
1118 BT_DBG("intf %p", intf);
1119
1120 if (!data)
1121 return;
1122
1123 hdev = data->hdev;
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02001124 usb_set_intfdata(data->intf, NULL);
1125
1126 if (data->isoc)
1127 usb_set_intfdata(data->isoc, NULL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001128
1129 hci_unregister_dev(hdev);
1130
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02001131 if (intf == data->isoc)
1132 usb_driver_release_interface(&btusb_driver, data->intf);
1133 else if (data->isoc)
1134 usb_driver_release_interface(&btusb_driver, data->isoc);
1135
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001136 hci_free_dev(hdev);
David Herrmann83810882012-01-07 15:47:16 +01001137 kfree(data);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001138}
1139
Oliver Neukum7bee5492009-08-24 23:44:59 +02001140#ifdef CONFIG_PM
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001141static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
1142{
1143 struct btusb_data *data = usb_get_intfdata(intf);
1144
1145 BT_DBG("intf %p", intf);
1146
1147 if (data->suspend_count++)
1148 return 0;
1149
Oliver Neukum7bee5492009-08-24 23:44:59 +02001150 spin_lock_irq(&data->txlock);
Alan Stern5b1b0b82011-08-19 23:49:48 +02001151 if (!(PMSG_IS_AUTO(message) && data->tx_in_flight)) {
Oliver Neukum7bee5492009-08-24 23:44:59 +02001152 set_bit(BTUSB_SUSPENDING, &data->flags);
1153 spin_unlock_irq(&data->txlock);
1154 } else {
1155 spin_unlock_irq(&data->txlock);
1156 data->suspend_count--;
1157 return -EBUSY;
1158 }
1159
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001160 cancel_work_sync(&data->work);
1161
Oliver Neukum7bee5492009-08-24 23:44:59 +02001162 btusb_stop_traffic(data);
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001163 usb_kill_anchored_urbs(&data->tx_anchor);
1164
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001165 return 0;
1166}
1167
Oliver Neukum7bee5492009-08-24 23:44:59 +02001168static void play_deferred(struct btusb_data *data)
1169{
1170 struct urb *urb;
1171 int err;
1172
1173 while ((urb = usb_get_from_anchor(&data->deferred))) {
1174 err = usb_submit_urb(urb, GFP_ATOMIC);
1175 if (err < 0)
1176 break;
1177
1178 data->tx_in_flight++;
1179 }
1180 usb_scuttle_anchored_urbs(&data->deferred);
1181}
1182
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001183static int btusb_resume(struct usb_interface *intf)
1184{
1185 struct btusb_data *data = usb_get_intfdata(intf);
1186 struct hci_dev *hdev = data->hdev;
Oliver Neukum7bee5492009-08-24 23:44:59 +02001187 int err = 0;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001188
1189 BT_DBG("intf %p", intf);
1190
1191 if (--data->suspend_count)
1192 return 0;
1193
1194 if (!test_bit(HCI_RUNNING, &hdev->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +02001195 goto done;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001196
1197 if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
1198 err = btusb_submit_intr_urb(hdev, GFP_NOIO);
1199 if (err < 0) {
1200 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001201 goto failed;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001202 }
1203 }
1204
1205 if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) {
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001206 err = btusb_submit_bulk_urb(hdev, GFP_NOIO);
1207 if (err < 0) {
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001208 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001209 goto failed;
1210 }
1211
1212 btusb_submit_bulk_urb(hdev, GFP_NOIO);
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001213 }
1214
1215 if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
1216 if (btusb_submit_isoc_urb(hdev, GFP_NOIO) < 0)
1217 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1218 else
1219 btusb_submit_isoc_urb(hdev, GFP_NOIO);
1220 }
1221
Oliver Neukum7bee5492009-08-24 23:44:59 +02001222 spin_lock_irq(&data->txlock);
1223 play_deferred(data);
1224 clear_bit(BTUSB_SUSPENDING, &data->flags);
1225 spin_unlock_irq(&data->txlock);
1226 schedule_work(&data->work);
1227
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001228 return 0;
Oliver Neukum7bee5492009-08-24 23:44:59 +02001229
1230failed:
1231 usb_scuttle_anchored_urbs(&data->deferred);
1232done:
1233 spin_lock_irq(&data->txlock);
1234 clear_bit(BTUSB_SUSPENDING, &data->flags);
1235 spin_unlock_irq(&data->txlock);
1236
1237 return err;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001238}
Oliver Neukum7bee5492009-08-24 23:44:59 +02001239#endif
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001240
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001241static struct usb_driver btusb_driver = {
1242 .name = "btusb",
1243 .probe = btusb_probe,
1244 .disconnect = btusb_disconnect,
Oliver Neukum7bee5492009-08-24 23:44:59 +02001245#ifdef CONFIG_PM
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001246 .suspend = btusb_suspend,
1247 .resume = btusb_resume,
Oliver Neukum7bee5492009-08-24 23:44:59 +02001248#endif
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001249 .id_table = btusb_table,
Oliver Neukum7bee5492009-08-24 23:44:59 +02001250 .supports_autosuspend = 1,
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001251};
1252
Greg Kroah-Hartman93f15082011-11-18 09:47:34 -08001253module_usb_driver(btusb_driver);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001254
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02001255module_param(ignore_dga, bool, 0644);
1256MODULE_PARM_DESC(ignore_dga, "Ignore devices with id 08fd:0001");
1257
1258module_param(ignore_csr, bool, 0644);
1259MODULE_PARM_DESC(ignore_csr, "Ignore devices with id 0a12:0001");
1260
1261module_param(ignore_sniffer, bool, 0644);
1262MODULE_PARM_DESC(ignore_sniffer, "Ignore devices with id 0a12:0002");
1263
1264module_param(disable_scofix, bool, 0644);
1265MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
1266
1267module_param(force_scofix, bool, 0644);
1268MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
1269
1270module_param(reset, bool, 0644);
1271MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
1272
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001273MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
1274MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
1275MODULE_VERSION(VERSION);
1276MODULE_LICENSE("GPL");