blob: 5d80caf5737b960d7f8fc392284ac498a6ab892b [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 },
Giancarlo Formicucciac2f15c32012-06-10 08:33:11 +0200157 { USB_DEVICE(0x0930, 0x0219), .driver_info = BTUSB_ATH3012 },
Peng Chen4126da52012-08-01 10:11:59 +0800158 { USB_DEVICE(0x0489, 0xe057), .driver_info = BTUSB_ATH3012 },
AceLan Kao3b36e952012-12-11 11:41:20 +0800159 { USB_DEVICE(0x13d3, 0x3393), .driver_info = BTUSB_ATH3012 },
AceLan Kao340733c2013-01-03 12:24:59 +0800160 { USB_DEVICE(0x0489, 0xe04e), .driver_info = BTUSB_ATH3012 },
AceLan Kao7a344a222013-01-03 12:25:00 +0800161 { USB_DEVICE(0x0489, 0xe056), .driver_info = BTUSB_ATH3012 },
Noguchi Kazutosi27370722013-03-24 23:41:10 +0900162 { USB_DEVICE(0x0489, 0xe04d), .driver_info = BTUSB_ATH3012 },
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530163
Cho, Yu-Chene9036e32011-02-15 10:20:07 +0800164 /* Atheros AR5BBU12 with sflash firmware */
165 { USB_DEVICE(0x0489, 0xe02c), .driver_info = BTUSB_IGNORE },
166
Michael Gruetznera1144ca2012-05-02 22:33:40 +0200167 /* Atheros AR5BBU12 with sflash firmware */
168 { USB_DEVICE(0x0489, 0xe03c), .driver_info = BTUSB_ATH3012 },
Yevgeniy Melnichuk1403b362012-08-07 19:48:10 +0530169 { USB_DEVICE(0x0489, 0xe036), .driver_info = BTUSB_ATH3012 },
Michael Gruetznera1144ca2012-05-02 22:33:40 +0200170
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200171 /* Broadcom BCM2035 */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100172 { USB_DEVICE(0x0a5c, 0x2035), .driver_info = BTUSB_WRONG_SCO_MTU },
173 { USB_DEVICE(0x0a5c, 0x200a), .driver_info = BTUSB_WRONG_SCO_MTU },
174 { USB_DEVICE(0x0a5c, 0x2009), .driver_info = BTUSB_BCM92035 },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200175
176 /* Broadcom BCM2045 */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100177 { USB_DEVICE(0x0a5c, 0x2039), .driver_info = BTUSB_WRONG_SCO_MTU },
178 { USB_DEVICE(0x0a5c, 0x2101), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmannbdbef3d2008-09-23 00:16:35 +0200179
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200180 /* IBM/Lenovo ThinkPad with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100181 { USB_DEVICE(0x0a5c, 0x201e), .driver_info = BTUSB_WRONG_SCO_MTU },
182 { USB_DEVICE(0x0a5c, 0x2110), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200183
184 /* HP laptop with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100185 { USB_DEVICE(0x03f0, 0x171d), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200186
187 /* Dell laptop with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100188 { USB_DEVICE(0x413c, 0x8126), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200189
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100190 /* Dell Wireless 370 and 410 devices */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100191 { USB_DEVICE(0x413c, 0x8152), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100192 { USB_DEVICE(0x413c, 0x8156), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200193
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100194 /* Belkin F8T012 and F8T013 devices */
195 { USB_DEVICE(0x050d, 0x0012), .driver_info = BTUSB_WRONG_SCO_MTU },
196 { USB_DEVICE(0x050d, 0x0013), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200197
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100198 /* Asus WL-BTD202 device */
199 { USB_DEVICE(0x0b05, 0x1715), .driver_info = BTUSB_WRONG_SCO_MTU },
200
201 /* Kensington Bluetooth USB adapter */
202 { USB_DEVICE(0x047d, 0x105e), .driver_info = BTUSB_WRONG_SCO_MTU },
203
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200204 /* RTX Telecom based adapters with buggy SCO support */
205 { USB_DEVICE(0x0400, 0x0807), .driver_info = BTUSB_BROKEN_ISOC },
206 { USB_DEVICE(0x0400, 0x080a), .driver_info = BTUSB_BROKEN_ISOC },
207
208 /* CONWISE Technology based adapters with buggy SCO support */
209 { USB_DEVICE(0x0e5e, 0x6622), .driver_info = BTUSB_BROKEN_ISOC },
210
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200211 /* Digianswer devices */
212 { USB_DEVICE(0x08fd, 0x0001), .driver_info = BTUSB_DIGIANSWER },
213 { USB_DEVICE(0x08fd, 0x0002), .driver_info = BTUSB_IGNORE },
214
215 /* CSR BlueCore Bluetooth Sniffer */
216 { USB_DEVICE(0x0a12, 0x0002), .driver_info = BTUSB_SNIFFER },
217
218 /* Frontline ComProbe Bluetooth Sniffer */
219 { USB_DEVICE(0x16d3, 0x0002), .driver_info = BTUSB_SNIFFER },
220
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200221 { } /* Terminating entry */
222};
223
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200224#define BTUSB_MAX_ISOC_FRAMES 10
225
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200226#define BTUSB_INTR_RUNNING 0
227#define BTUSB_BULK_RUNNING 1
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200228#define BTUSB_ISOC_RUNNING 2
Oliver Neukum7bee5492009-08-24 23:44:59 +0200229#define BTUSB_SUSPENDING 3
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -0300230#define BTUSB_DID_ISO_RESUME 4
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200231
232struct btusb_data {
233 struct hci_dev *hdev;
234 struct usb_device *udev;
Marcel Holtmann5fbcd262008-09-23 00:16:36 +0200235 struct usb_interface *intf;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200236 struct usb_interface *isoc;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200237
238 spinlock_t lock;
239
240 unsigned long flags;
241
242 struct work_struct work;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200243 struct work_struct waker;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200244
245 struct usb_anchor tx_anchor;
246 struct usb_anchor intr_anchor;
247 struct usb_anchor bulk_anchor;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200248 struct usb_anchor isoc_anchor;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200249 struct usb_anchor deferred;
250 int tx_in_flight;
251 spinlock_t txlock;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200252
253 struct usb_endpoint_descriptor *intr_ep;
254 struct usb_endpoint_descriptor *bulk_tx_ep;
255 struct usb_endpoint_descriptor *bulk_rx_ep;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200256 struct usb_endpoint_descriptor *isoc_tx_ep;
257 struct usb_endpoint_descriptor *isoc_rx_ep;
258
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100259 __u8 cmdreq_type;
260
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100261 unsigned int sco_num;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200262 int isoc_altsetting;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +0100263 int suspend_count;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200264};
265
Oliver Neukum7bee5492009-08-24 23:44:59 +0200266static int inc_tx(struct btusb_data *data)
267{
268 unsigned long flags;
269 int rv;
270
271 spin_lock_irqsave(&data->txlock, flags);
272 rv = test_bit(BTUSB_SUSPENDING, &data->flags);
273 if (!rv)
274 data->tx_in_flight++;
275 spin_unlock_irqrestore(&data->txlock, flags);
276
277 return rv;
278}
279
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200280static void btusb_intr_complete(struct urb *urb)
281{
282 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +0100283 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200284 int err;
285
286 BT_DBG("%s urb %p status %d count %d", hdev->name,
287 urb, urb->status, urb->actual_length);
288
289 if (!test_bit(HCI_RUNNING, &hdev->flags))
290 return;
291
292 if (urb->status == 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200293 hdev->stat.byte_rx += urb->actual_length;
294
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200295 if (hci_recv_fragment(hdev, HCI_EVENT_PKT,
296 urb->transfer_buffer,
297 urb->actual_length) < 0) {
298 BT_ERR("%s corrupted event packet", hdev->name);
299 hdev->stat.err_rx++;
300 }
301 }
302
303 if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
304 return;
305
Oliver Neukum7bee5492009-08-24 23:44:59 +0200306 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200307 usb_anchor_urb(urb, &data->intr_anchor);
308
309 err = usb_submit_urb(urb, GFP_ATOMIC);
310 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200311 /* -EPERM: urb is being killed;
312 * -ENODEV: device got disconnected */
313 if (err != -EPERM && err != -ENODEV)
Stefan Seyfried61faddf2010-11-30 21:49:08 +0100314 BT_ERR("%s urb %p failed to resubmit (%d)",
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200315 hdev->name, urb, -err);
316 usb_unanchor_urb(urb);
317 }
318}
319
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100320static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200321{
David Herrmann155961e2012-02-09 21:58:32 +0100322 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200323 struct urb *urb;
324 unsigned char *buf;
325 unsigned int pipe;
326 int err, size;
327
328 BT_DBG("%s", hdev->name);
329
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200330 if (!data->intr_ep)
331 return -ENODEV;
332
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100333 urb = usb_alloc_urb(0, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200334 if (!urb)
335 return -ENOMEM;
336
337 size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
338
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100339 buf = kmalloc(size, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200340 if (!buf) {
341 usb_free_urb(urb);
342 return -ENOMEM;
343 }
344
345 pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
346
347 usb_fill_int_urb(urb, data->udev, pipe, buf, size,
348 btusb_intr_complete, hdev,
349 data->intr_ep->bInterval);
350
351 urb->transfer_flags |= URB_FREE_BUFFER;
352
353 usb_anchor_urb(urb, &data->intr_anchor);
354
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100355 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200356 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +0200357 if (err != -EPERM && err != -ENODEV)
358 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200359 hdev->name, urb, -err);
360 usb_unanchor_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200361 }
362
363 usb_free_urb(urb);
364
365 return err;
366}
367
368static void btusb_bulk_complete(struct urb *urb)
369{
370 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +0100371 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200372 int err;
373
374 BT_DBG("%s urb %p status %d count %d", hdev->name,
375 urb, urb->status, urb->actual_length);
376
377 if (!test_bit(HCI_RUNNING, &hdev->flags))
378 return;
379
380 if (urb->status == 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200381 hdev->stat.byte_rx += urb->actual_length;
382
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200383 if (hci_recv_fragment(hdev, HCI_ACLDATA_PKT,
384 urb->transfer_buffer,
385 urb->actual_length) < 0) {
386 BT_ERR("%s corrupted ACL packet", hdev->name);
387 hdev->stat.err_rx++;
388 }
389 }
390
391 if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
392 return;
393
394 usb_anchor_urb(urb, &data->bulk_anchor);
Oliver Neukum652fd782009-12-16 19:23:43 +0100395 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200396
397 err = usb_submit_urb(urb, GFP_ATOMIC);
398 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200399 /* -EPERM: urb is being killed;
400 * -ENODEV: device got disconnected */
401 if (err != -EPERM && err != -ENODEV)
Stefan Seyfried61faddf2010-11-30 21:49:08 +0100402 BT_ERR("%s urb %p failed to resubmit (%d)",
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200403 hdev->name, urb, -err);
404 usb_unanchor_urb(urb);
405 }
406}
407
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100408static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200409{
David Herrmann155961e2012-02-09 21:58:32 +0100410 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200411 struct urb *urb;
412 unsigned char *buf;
413 unsigned int pipe;
Vikram Kandukuri290ba202009-07-02 14:31:59 +0530414 int err, size = HCI_MAX_FRAME_SIZE;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200415
416 BT_DBG("%s", hdev->name);
417
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200418 if (!data->bulk_rx_ep)
419 return -ENODEV;
420
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100421 urb = usb_alloc_urb(0, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200422 if (!urb)
423 return -ENOMEM;
424
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100425 buf = kmalloc(size, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200426 if (!buf) {
427 usb_free_urb(urb);
428 return -ENOMEM;
429 }
430
431 pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
432
433 usb_fill_bulk_urb(urb, data->udev, pipe,
434 buf, size, btusb_bulk_complete, hdev);
435
436 urb->transfer_flags |= URB_FREE_BUFFER;
437
Oliver Neukum7bee5492009-08-24 23:44:59 +0200438 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200439 usb_anchor_urb(urb, &data->bulk_anchor);
440
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100441 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200442 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +0200443 if (err != -EPERM && err != -ENODEV)
444 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200445 hdev->name, urb, -err);
446 usb_unanchor_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200447 }
448
449 usb_free_urb(urb);
450
451 return err;
452}
453
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200454static void btusb_isoc_complete(struct urb *urb)
455{
456 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +0100457 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200458 int i, err;
459
460 BT_DBG("%s urb %p status %d count %d", hdev->name,
461 urb, urb->status, urb->actual_length);
462
463 if (!test_bit(HCI_RUNNING, &hdev->flags))
464 return;
465
466 if (urb->status == 0) {
467 for (i = 0; i < urb->number_of_packets; i++) {
468 unsigned int offset = urb->iso_frame_desc[i].offset;
469 unsigned int length = urb->iso_frame_desc[i].actual_length;
470
471 if (urb->iso_frame_desc[i].status)
472 continue;
473
474 hdev->stat.byte_rx += length;
475
476 if (hci_recv_fragment(hdev, HCI_SCODATA_PKT,
477 urb->transfer_buffer + offset,
478 length) < 0) {
479 BT_ERR("%s corrupted SCO packet", hdev->name);
480 hdev->stat.err_rx++;
481 }
482 }
483 }
484
485 if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags))
486 return;
487
488 usb_anchor_urb(urb, &data->isoc_anchor);
489
490 err = usb_submit_urb(urb, GFP_ATOMIC);
491 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200492 /* -EPERM: urb is being killed;
493 * -ENODEV: device got disconnected */
494 if (err != -EPERM && err != -ENODEV)
Stefan Seyfried61faddf2010-11-30 21:49:08 +0100495 BT_ERR("%s urb %p failed to resubmit (%d)",
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200496 hdev->name, urb, -err);
497 usb_unanchor_urb(urb);
498 }
499}
500
Jesper Juhl42b16b32011-01-17 00:09:38 +0100501static inline void __fill_isoc_descriptor(struct urb *urb, int len, int mtu)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200502{
503 int i, offset = 0;
504
505 BT_DBG("len %d mtu %d", len, mtu);
506
507 for (i = 0; i < BTUSB_MAX_ISOC_FRAMES && len >= mtu;
508 i++, offset += mtu, len -= mtu) {
509 urb->iso_frame_desc[i].offset = offset;
510 urb->iso_frame_desc[i].length = mtu;
511 }
512
513 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
514 urb->iso_frame_desc[i].offset = offset;
515 urb->iso_frame_desc[i].length = len;
516 i++;
517 }
518
519 urb->number_of_packets = i;
520}
521
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100522static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200523{
David Herrmann155961e2012-02-09 21:58:32 +0100524 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200525 struct urb *urb;
526 unsigned char *buf;
527 unsigned int pipe;
528 int err, size;
529
530 BT_DBG("%s", hdev->name);
531
532 if (!data->isoc_rx_ep)
533 return -ENODEV;
534
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100535 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200536 if (!urb)
537 return -ENOMEM;
538
539 size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) *
540 BTUSB_MAX_ISOC_FRAMES;
541
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100542 buf = kmalloc(size, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200543 if (!buf) {
544 usb_free_urb(urb);
545 return -ENOMEM;
546 }
547
548 pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress);
549
Bing Zhaofa0fb932011-12-20 18:19:00 -0800550 usb_fill_int_urb(urb, data->udev, pipe, buf, size, btusb_isoc_complete,
551 hdev, data->isoc_rx_ep->bInterval);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200552
553 urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200554
555 __fill_isoc_descriptor(urb, size,
556 le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize));
557
558 usb_anchor_urb(urb, &data->isoc_anchor);
559
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100560 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200561 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +0200562 if (err != -EPERM && err != -ENODEV)
563 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200564 hdev->name, urb, -err);
565 usb_unanchor_urb(urb);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200566 }
567
568 usb_free_urb(urb);
569
570 return err;
571}
572
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200573static void btusb_tx_complete(struct urb *urb)
574{
575 struct sk_buff *skb = urb->context;
576 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
David Herrmann155961e2012-02-09 21:58:32 +0100577 struct btusb_data *data = hci_get_drvdata(hdev);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200578
579 BT_DBG("%s urb %p status %d count %d", hdev->name,
580 urb, urb->status, urb->actual_length);
581
582 if (!test_bit(HCI_RUNNING, &hdev->flags))
583 goto done;
584
585 if (!urb->status)
586 hdev->stat.byte_tx += urb->transfer_buffer_length;
587 else
588 hdev->stat.err_tx++;
589
590done:
591 spin_lock(&data->txlock);
592 data->tx_in_flight--;
593 spin_unlock(&data->txlock);
594
595 kfree(urb->setup_packet);
596
597 kfree_skb(skb);
598}
599
600static void btusb_isoc_tx_complete(struct urb *urb)
601{
602 struct sk_buff *skb = urb->context;
603 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200604
605 BT_DBG("%s urb %p status %d count %d", hdev->name,
606 urb, urb->status, urb->actual_length);
607
608 if (!test_bit(HCI_RUNNING, &hdev->flags))
609 goto done;
610
611 if (!urb->status)
612 hdev->stat.byte_tx += urb->transfer_buffer_length;
613 else
614 hdev->stat.err_tx++;
615
616done:
617 kfree(urb->setup_packet);
618
619 kfree_skb(skb);
620}
621
622static int btusb_open(struct hci_dev *hdev)
623{
David Herrmann155961e2012-02-09 21:58:32 +0100624 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200625 int err;
626
627 BT_DBG("%s", hdev->name);
628
Oliver Neukum7bee5492009-08-24 23:44:59 +0200629 err = usb_autopm_get_interface(data->intf);
630 if (err < 0)
631 return err;
632
633 data->intf->needs_remote_wakeup = 1;
634
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200635 if (test_and_set_bit(HCI_RUNNING, &hdev->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +0200636 goto done;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200637
638 if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +0200639 goto done;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200640
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100641 err = btusb_submit_intr_urb(hdev, GFP_KERNEL);
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100642 if (err < 0)
643 goto failed;
644
645 err = btusb_submit_bulk_urb(hdev, GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200646 if (err < 0) {
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100647 usb_kill_anchored_urbs(&data->intr_anchor);
648 goto failed;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200649 }
650
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100651 set_bit(BTUSB_BULK_RUNNING, &data->flags);
652 btusb_submit_bulk_urb(hdev, GFP_KERNEL);
653
Oliver Neukum7bee5492009-08-24 23:44:59 +0200654done:
655 usb_autopm_put_interface(data->intf);
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100656 return 0;
657
658failed:
659 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
660 clear_bit(HCI_RUNNING, &hdev->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200661 usb_autopm_put_interface(data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200662 return err;
663}
664
Oliver Neukum7bee5492009-08-24 23:44:59 +0200665static void btusb_stop_traffic(struct btusb_data *data)
666{
667 usb_kill_anchored_urbs(&data->intr_anchor);
668 usb_kill_anchored_urbs(&data->bulk_anchor);
669 usb_kill_anchored_urbs(&data->isoc_anchor);
670}
671
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200672static int btusb_close(struct hci_dev *hdev)
673{
David Herrmann155961e2012-02-09 21:58:32 +0100674 struct btusb_data *data = hci_get_drvdata(hdev);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200675 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200676
677 BT_DBG("%s", hdev->name);
678
679 if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
680 return 0;
681
Marcel Holtmanne8c3c3d2008-09-23 00:16:36 +0200682 cancel_work_sync(&data->work);
Linus Torvalds404291a2009-11-11 13:32:29 -0800683 cancel_work_sync(&data->waker);
Marcel Holtmanne8c3c3d2008-09-23 00:16:36 +0200684
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200685 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200686 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200687 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200688
689 btusb_stop_traffic(data);
690 err = usb_autopm_get_interface(data->intf);
691 if (err < 0)
Oliver Neukum7b8e2c12009-11-13 14:26:23 +0100692 goto failed;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200693
694 data->intf->needs_remote_wakeup = 0;
695 usb_autopm_put_interface(data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200696
Oliver Neukum7b8e2c12009-11-13 14:26:23 +0100697failed:
698 usb_scuttle_anchored_urbs(&data->deferred);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200699 return 0;
700}
701
702static int btusb_flush(struct hci_dev *hdev)
703{
David Herrmann155961e2012-02-09 21:58:32 +0100704 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200705
706 BT_DBG("%s", hdev->name);
707
708 usb_kill_anchored_urbs(&data->tx_anchor);
709
710 return 0;
711}
712
713static int btusb_send_frame(struct sk_buff *skb)
714{
715 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
David Herrmann155961e2012-02-09 21:58:32 +0100716 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200717 struct usb_ctrlrequest *dr;
718 struct urb *urb;
719 unsigned int pipe;
720 int err;
721
722 BT_DBG("%s", hdev->name);
723
724 if (!test_bit(HCI_RUNNING, &hdev->flags))
725 return -EBUSY;
726
727 switch (bt_cb(skb)->pkt_type) {
728 case HCI_COMMAND_PKT:
729 urb = usb_alloc_urb(0, GFP_ATOMIC);
730 if (!urb)
731 return -ENOMEM;
732
733 dr = kmalloc(sizeof(*dr), GFP_ATOMIC);
734 if (!dr) {
735 usb_free_urb(urb);
736 return -ENOMEM;
737 }
738
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100739 dr->bRequestType = data->cmdreq_type;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200740 dr->bRequest = 0;
741 dr->wIndex = 0;
742 dr->wValue = 0;
743 dr->wLength = __cpu_to_le16(skb->len);
744
745 pipe = usb_sndctrlpipe(data->udev, 0x00);
746
747 usb_fill_control_urb(urb, data->udev, pipe, (void *) dr,
748 skb->data, skb->len, btusb_tx_complete, skb);
749
750 hdev->stat.cmd_tx++;
751 break;
752
753 case HCI_ACLDATA_PKT:
Peter Hurley9fd481e2011-07-14 08:48:32 -0400754 if (!data->bulk_tx_ep)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200755 return -ENODEV;
756
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200757 urb = usb_alloc_urb(0, GFP_ATOMIC);
758 if (!urb)
759 return -ENOMEM;
760
761 pipe = usb_sndbulkpipe(data->udev,
762 data->bulk_tx_ep->bEndpointAddress);
763
764 usb_fill_bulk_urb(urb, data->udev, pipe,
765 skb->data, skb->len, btusb_tx_complete, skb);
766
767 hdev->stat.acl_tx++;
768 break;
769
770 case HCI_SCODATA_PKT:
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200771 if (!data->isoc_tx_ep || hdev->conn_hash.sco_num < 1)
772 return -ENODEV;
773
774 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_ATOMIC);
775 if (!urb)
776 return -ENOMEM;
777
778 pipe = usb_sndisocpipe(data->udev,
779 data->isoc_tx_ep->bEndpointAddress);
780
Gustavo F. Padovan03c2d0e2011-02-14 18:53:43 -0300781 usb_fill_int_urb(urb, data->udev, pipe,
782 skb->data, skb->len, btusb_isoc_tx_complete,
783 skb, data->isoc_tx_ep->bInterval);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200784
785 urb->transfer_flags = URB_ISO_ASAP;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200786
787 __fill_isoc_descriptor(urb, skb->len,
788 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
789
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200790 hdev->stat.sco_tx++;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200791 goto skip_waking;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200792
793 default:
794 return -EILSEQ;
795 }
796
Oliver Neukum7bee5492009-08-24 23:44:59 +0200797 err = inc_tx(data);
798 if (err) {
799 usb_anchor_urb(urb, &data->deferred);
800 schedule_work(&data->waker);
801 err = 0;
802 goto done;
803 }
804
805skip_waking:
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200806 usb_anchor_urb(urb, &data->tx_anchor);
807
808 err = usb_submit_urb(urb, GFP_ATOMIC);
809 if (err < 0) {
Paul Bolle5a9b80e2011-10-09 12:12:16 +0200810 if (err != -EPERM && err != -ENODEV)
811 BT_ERR("%s urb %p submission failed (%d)",
812 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200813 kfree(urb->setup_packet);
814 usb_unanchor_urb(urb);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200815 } else {
816 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200817 }
818
Oliver Neukum7bee5492009-08-24 23:44:59 +0200819done:
Cong Wang54a8a792011-11-22 09:32:57 +0800820 usb_free_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200821 return err;
822}
823
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200824static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
825{
David Herrmann155961e2012-02-09 21:58:32 +0100826 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200827
828 BT_DBG("%s evt %d", hdev->name, evt);
829
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100830 if (hdev->conn_hash.sco_num != data->sco_num) {
831 data->sco_num = hdev->conn_hash.sco_num;
832 schedule_work(&data->work);
Marcel Holtmanna780efa2008-11-30 12:17:12 +0100833 }
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200834}
835
Jesper Juhl42b16b32011-01-17 00:09:38 +0100836static inline int __set_isoc_interface(struct hci_dev *hdev, int altsetting)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200837{
David Herrmann155961e2012-02-09 21:58:32 +0100838 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200839 struct usb_interface *intf = data->isoc;
840 struct usb_endpoint_descriptor *ep_desc;
841 int i, err;
842
843 if (!data->isoc)
844 return -ENODEV;
845
846 err = usb_set_interface(data->udev, 1, altsetting);
847 if (err < 0) {
848 BT_ERR("%s setting interface failed (%d)", hdev->name, -err);
849 return err;
850 }
851
852 data->isoc_altsetting = altsetting;
853
854 data->isoc_tx_ep = NULL;
855 data->isoc_rx_ep = NULL;
856
857 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
858 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
859
860 if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) {
861 data->isoc_tx_ep = ep_desc;
862 continue;
863 }
864
865 if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) {
866 data->isoc_rx_ep = ep_desc;
867 continue;
868 }
869 }
870
871 if (!data->isoc_tx_ep || !data->isoc_rx_ep) {
872 BT_ERR("%s invalid SCO descriptors", hdev->name);
873 return -ENODEV;
874 }
875
876 return 0;
877}
878
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200879static void btusb_work(struct work_struct *work)
880{
881 struct btusb_data *data = container_of(work, struct btusb_data, work);
882 struct hci_dev *hdev = data->hdev;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200883 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200884
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200885 if (hdev->conn_hash.sco_num > 0) {
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -0300886 if (!test_bit(BTUSB_DID_ISO_RESUME, &data->flags)) {
Oliver Neukum8efdd0c2011-02-11 13:00:06 +0100887 err = usb_autopm_get_interface(data->isoc ? data->isoc : data->intf);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200888 if (err < 0) {
889 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
890 usb_kill_anchored_urbs(&data->isoc_anchor);
891 return;
892 }
893
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -0300894 set_bit(BTUSB_DID_ISO_RESUME, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200895 }
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200896 if (data->isoc_altsetting != 2) {
897 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
898 usb_kill_anchored_urbs(&data->isoc_anchor);
899
900 if (__set_isoc_interface(hdev, 2) < 0)
901 return;
902 }
903
904 if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100905 if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200906 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
907 else
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100908 btusb_submit_isoc_urb(hdev, GFP_KERNEL);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200909 }
910 } else {
911 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
912 usb_kill_anchored_urbs(&data->isoc_anchor);
913
914 __set_isoc_interface(hdev, 0);
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -0300915 if (test_and_clear_bit(BTUSB_DID_ISO_RESUME, &data->flags))
Oliver Neukum8efdd0c2011-02-11 13:00:06 +0100916 usb_autopm_put_interface(data->isoc ? data->isoc : data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200917 }
918}
919
Oliver Neukum7bee5492009-08-24 23:44:59 +0200920static void btusb_waker(struct work_struct *work)
921{
922 struct btusb_data *data = container_of(work, struct btusb_data, waker);
923 int err;
924
925 err = usb_autopm_get_interface(data->intf);
926 if (err < 0)
927 return;
928
929 usb_autopm_put_interface(data->intf);
930}
931
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200932static int btusb_probe(struct usb_interface *intf,
933 const struct usb_device_id *id)
934{
935 struct usb_endpoint_descriptor *ep_desc;
936 struct btusb_data *data;
937 struct hci_dev *hdev;
938 int i, err;
939
940 BT_DBG("intf %p id %p", intf, id);
941
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200942 /* interface numbers are hardcoded in the spec */
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200943 if (intf->cur_altsetting->desc.bInterfaceNumber != 0)
944 return -ENODEV;
945
946 if (!id->driver_info) {
947 const struct usb_device_id *match;
948 match = usb_match_id(intf, blacklist_table);
949 if (match)
950 id = match;
951 }
952
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200953 if (id->driver_info == BTUSB_IGNORE)
954 return -ENODEV;
955
956 if (ignore_dga && id->driver_info & BTUSB_DIGIANSWER)
957 return -ENODEV;
958
959 if (ignore_csr && id->driver_info & BTUSB_CSR)
960 return -ENODEV;
961
962 if (ignore_sniffer && id->driver_info & BTUSB_SNIFFER)
963 return -ENODEV;
964
Steven.Li2d25f8b2011-07-01 14:02:36 +0800965 if (id->driver_info & BTUSB_ATH3012) {
966 struct usb_device *udev = interface_to_usbdev(intf);
967
968 /* Old firmware would otherwise let ath3k driver load
969 * patch and sysconfig files */
970 if (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x0001)
971 return -ENODEV;
972 }
973
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200974 data = kzalloc(sizeof(*data), GFP_KERNEL);
975 if (!data)
976 return -ENOMEM;
977
978 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
979 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
980
981 if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
982 data->intr_ep = ep_desc;
983 continue;
984 }
985
986 if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
987 data->bulk_tx_ep = ep_desc;
988 continue;
989 }
990
991 if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
992 data->bulk_rx_ep = ep_desc;
993 continue;
994 }
995 }
996
997 if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep) {
998 kfree(data);
999 return -ENODEV;
1000 }
1001
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01001002 data->cmdreq_type = USB_TYPE_CLASS;
1003
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001004 data->udev = interface_to_usbdev(intf);
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02001005 data->intf = intf;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001006
1007 spin_lock_init(&data->lock);
1008
1009 INIT_WORK(&data->work, btusb_work);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001010 INIT_WORK(&data->waker, btusb_waker);
1011 spin_lock_init(&data->txlock);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001012
1013 init_usb_anchor(&data->tx_anchor);
1014 init_usb_anchor(&data->intr_anchor);
1015 init_usb_anchor(&data->bulk_anchor);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001016 init_usb_anchor(&data->isoc_anchor);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001017 init_usb_anchor(&data->deferred);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001018
1019 hdev = hci_alloc_dev();
1020 if (!hdev) {
1021 kfree(data);
1022 return -ENOMEM;
1023 }
1024
Marcel Holtmannc13854ce2010-02-08 15:27:07 +01001025 hdev->bus = HCI_USB;
David Herrmann155961e2012-02-09 21:58:32 +01001026 hci_set_drvdata(hdev, data);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001027
1028 data->hdev = hdev;
1029
1030 SET_HCIDEV_DEV(hdev, &intf->dev);
1031
1032 hdev->open = btusb_open;
1033 hdev->close = btusb_close;
1034 hdev->flush = btusb_flush;
1035 hdev->send = btusb_send_frame;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001036 hdev->notify = btusb_notify;
1037
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01001038 /* Interface numbers are hardcoded in the specification */
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001039 data->isoc = usb_ifnum_to_if(data->udev, 1);
1040
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01001041 if (!reset)
1042 set_bit(HCI_QUIRK_NO_RESET, &hdev->quirks);
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02001043
1044 if (force_scofix || id->driver_info & BTUSB_WRONG_SCO_MTU) {
1045 if (!disable_scofix)
1046 set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
1047 }
1048
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001049 if (id->driver_info & BTUSB_BROKEN_ISOC)
1050 data->isoc = NULL;
1051
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01001052 if (id->driver_info & BTUSB_DIGIANSWER) {
1053 data->cmdreq_type = USB_TYPE_VENDOR;
1054 set_bit(HCI_QUIRK_NO_RESET, &hdev->quirks);
1055 }
1056
1057 if (id->driver_info & BTUSB_CSR) {
1058 struct usb_device *udev = data->udev;
1059
1060 /* Old firmware would otherwise execute USB reset */
1061 if (le16_to_cpu(udev->descriptor.bcdDevice) < 0x117)
1062 set_bit(HCI_QUIRK_NO_RESET, &hdev->quirks);
1063 }
1064
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02001065 if (id->driver_info & BTUSB_SNIFFER) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001066 struct usb_device *udev = data->udev;
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02001067
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01001068 /* New sniffer firmware has crippled HCI interface */
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02001069 if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
1070 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001071
1072 data->isoc = NULL;
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02001073 }
1074
1075 if (id->driver_info & BTUSB_BCM92035) {
1076 unsigned char cmd[] = { 0x3b, 0xfc, 0x01, 0x00 };
1077 struct sk_buff *skb;
1078
1079 skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL);
1080 if (skb) {
1081 memcpy(skb_put(skb, sizeof(cmd)), cmd, sizeof(cmd));
1082 skb_queue_tail(&hdev->driver_init, skb);
1083 }
1084 }
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001085
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001086 if (data->isoc) {
1087 err = usb_driver_claim_interface(&btusb_driver,
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02001088 data->isoc, data);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001089 if (err < 0) {
1090 hci_free_dev(hdev);
1091 kfree(data);
1092 return err;
1093 }
1094 }
1095
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001096 err = hci_register_dev(hdev);
1097 if (err < 0) {
1098 hci_free_dev(hdev);
1099 kfree(data);
1100 return err;
1101 }
1102
1103 usb_set_intfdata(intf, data);
1104
1105 return 0;
1106}
1107
1108static void btusb_disconnect(struct usb_interface *intf)
1109{
1110 struct btusb_data *data = usb_get_intfdata(intf);
1111 struct hci_dev *hdev;
1112
1113 BT_DBG("intf %p", intf);
1114
1115 if (!data)
1116 return;
1117
1118 hdev = data->hdev;
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02001119 usb_set_intfdata(data->intf, NULL);
1120
1121 if (data->isoc)
1122 usb_set_intfdata(data->isoc, NULL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001123
1124 hci_unregister_dev(hdev);
1125
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02001126 if (intf == data->isoc)
1127 usb_driver_release_interface(&btusb_driver, data->intf);
1128 else if (data->isoc)
1129 usb_driver_release_interface(&btusb_driver, data->isoc);
1130
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001131 hci_free_dev(hdev);
David Herrmann83810882012-01-07 15:47:16 +01001132 kfree(data);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001133}
1134
Oliver Neukum7bee5492009-08-24 23:44:59 +02001135#ifdef CONFIG_PM
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001136static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
1137{
1138 struct btusb_data *data = usb_get_intfdata(intf);
1139
1140 BT_DBG("intf %p", intf);
1141
1142 if (data->suspend_count++)
1143 return 0;
1144
Oliver Neukum7bee5492009-08-24 23:44:59 +02001145 spin_lock_irq(&data->txlock);
Alan Stern5b1b0b82011-08-19 23:49:48 +02001146 if (!(PMSG_IS_AUTO(message) && data->tx_in_flight)) {
Oliver Neukum7bee5492009-08-24 23:44:59 +02001147 set_bit(BTUSB_SUSPENDING, &data->flags);
1148 spin_unlock_irq(&data->txlock);
1149 } else {
1150 spin_unlock_irq(&data->txlock);
1151 data->suspend_count--;
1152 return -EBUSY;
1153 }
1154
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001155 cancel_work_sync(&data->work);
1156
Oliver Neukum7bee5492009-08-24 23:44:59 +02001157 btusb_stop_traffic(data);
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001158 usb_kill_anchored_urbs(&data->tx_anchor);
1159
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001160 return 0;
1161}
1162
Oliver Neukum7bee5492009-08-24 23:44:59 +02001163static void play_deferred(struct btusb_data *data)
1164{
1165 struct urb *urb;
1166 int err;
1167
1168 while ((urb = usb_get_from_anchor(&data->deferred))) {
1169 err = usb_submit_urb(urb, GFP_ATOMIC);
1170 if (err < 0)
1171 break;
1172
1173 data->tx_in_flight++;
1174 }
1175 usb_scuttle_anchored_urbs(&data->deferred);
1176}
1177
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001178static int btusb_resume(struct usb_interface *intf)
1179{
1180 struct btusb_data *data = usb_get_intfdata(intf);
1181 struct hci_dev *hdev = data->hdev;
Oliver Neukum7bee5492009-08-24 23:44:59 +02001182 int err = 0;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001183
1184 BT_DBG("intf %p", intf);
1185
1186 if (--data->suspend_count)
1187 return 0;
1188
1189 if (!test_bit(HCI_RUNNING, &hdev->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +02001190 goto done;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001191
1192 if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
1193 err = btusb_submit_intr_urb(hdev, GFP_NOIO);
1194 if (err < 0) {
1195 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001196 goto failed;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001197 }
1198 }
1199
1200 if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) {
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001201 err = btusb_submit_bulk_urb(hdev, GFP_NOIO);
1202 if (err < 0) {
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001203 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001204 goto failed;
1205 }
1206
1207 btusb_submit_bulk_urb(hdev, GFP_NOIO);
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001208 }
1209
1210 if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
1211 if (btusb_submit_isoc_urb(hdev, GFP_NOIO) < 0)
1212 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1213 else
1214 btusb_submit_isoc_urb(hdev, GFP_NOIO);
1215 }
1216
Oliver Neukum7bee5492009-08-24 23:44:59 +02001217 spin_lock_irq(&data->txlock);
1218 play_deferred(data);
1219 clear_bit(BTUSB_SUSPENDING, &data->flags);
1220 spin_unlock_irq(&data->txlock);
1221 schedule_work(&data->work);
1222
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001223 return 0;
Oliver Neukum7bee5492009-08-24 23:44:59 +02001224
1225failed:
1226 usb_scuttle_anchored_urbs(&data->deferred);
1227done:
1228 spin_lock_irq(&data->txlock);
1229 clear_bit(BTUSB_SUSPENDING, &data->flags);
1230 spin_unlock_irq(&data->txlock);
1231
1232 return err;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001233}
Oliver Neukum7bee5492009-08-24 23:44:59 +02001234#endif
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001235
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001236static struct usb_driver btusb_driver = {
1237 .name = "btusb",
1238 .probe = btusb_probe,
1239 .disconnect = btusb_disconnect,
Oliver Neukum7bee5492009-08-24 23:44:59 +02001240#ifdef CONFIG_PM
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001241 .suspend = btusb_suspend,
1242 .resume = btusb_resume,
Oliver Neukum7bee5492009-08-24 23:44:59 +02001243#endif
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001244 .id_table = btusb_table,
Oliver Neukum7bee5492009-08-24 23:44:59 +02001245 .supports_autosuspend = 1,
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001246};
1247
Greg Kroah-Hartman93f15082011-11-18 09:47:34 -08001248module_usb_driver(btusb_driver);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001249
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02001250module_param(ignore_dga, bool, 0644);
1251MODULE_PARM_DESC(ignore_dga, "Ignore devices with id 08fd:0001");
1252
1253module_param(ignore_csr, bool, 0644);
1254MODULE_PARM_DESC(ignore_csr, "Ignore devices with id 0a12:0001");
1255
1256module_param(ignore_sniffer, bool, 0644);
1257MODULE_PARM_DESC(ignore_sniffer, "Ignore devices with id 0a12:0002");
1258
1259module_param(disable_scofix, bool, 0644);
1260MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
1261
1262module_param(force_scofix, bool, 0644);
1263MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
1264
1265module_param(reset, bool, 0644);
1266MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
1267
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001268MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
1269MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
1270MODULE_VERSION(VERSION);
1271MODULE_LICENSE("GPL");