blob: c7baee435ebdc52572e3e0bb7fe34bc04bd07177 [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
Oliver Neukumc510eae2011-09-21 11:41:45 +020066 /* Broadcom SoftSailing reporting vendor specific */
Don Zickus2e8b5062012-03-28 16:41:11 -040067 { USB_DEVICE(0x0a5c, 0x21e1) },
Oliver Neukumc510eae2011-09-21 11:41:45 +020068
Nobuhiro Iwamatsu3cd01972010-08-20 16:24:07 +090069 /* Apple MacBookPro 7,1 */
70 { USB_DEVICE(0x05ac, 0x8213) },
71
Cyril Lacoux0a79f672010-07-14 10:29:27 +040072 /* Apple iMac11,1 */
73 { USB_DEVICE(0x05ac, 0x8215) },
74
Nobuhiro Iwamatsu9c047152010-08-20 16:24:06 +090075 /* Apple MacBookPro6,2 */
76 { USB_DEVICE(0x05ac, 0x8218) },
77
Edgar (gimli) Hucek3e3ede72010-11-04 08:04:33 +010078 /* Apple MacBookAir3,1, MacBookAir3,2 */
79 { USB_DEVICE(0x05ac, 0x821b) },
80
Pieter-Augustijn Van Malleghema63b7232011-09-07 02:28:10 -040081 /* Apple MacBookAir4,1 */
82 { USB_DEVICE(0x05ac, 0x821f) },
83
Marc-Antoine Perennou88d377b2011-03-24 14:51:21 -030084 /* Apple MacBookPro8,2 */
85 { USB_DEVICE(0x05ac, 0x821a) },
86
Jurgen Kramerf78b6822011-09-04 18:01:42 +020087 /* Apple MacMini5,1 */
88 { USB_DEVICE(0x05ac, 0x8281) },
89
Marcel Holtmanncfeb4142008-08-07 22:26:56 +020090 /* AVM BlueFRITZ! USB v2.0 */
91 { USB_DEVICE(0x057c, 0x3800) },
92
93 /* Bluetooth Ultraport Module from IBM */
94 { USB_DEVICE(0x04bf, 0x030a) },
95
96 /* ALPS Modules with non-standard id */
97 { USB_DEVICE(0x044e, 0x3001) },
98 { USB_DEVICE(0x044e, 0x3002) },
99
100 /* Ericsson with non-standard id */
101 { USB_DEVICE(0x0bdb, 0x1002) },
102
103 /* Canyon CN-BTU1 with HID interfaces */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100104 { USB_DEVICE(0x0c10, 0x0000) },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200105
Wen-chien Jesse Sungd13431c2011-11-08 14:30:22 +0800106 /* Broadcom BCM20702A0 */
Jeff Cook4a990f62012-11-09 16:39:48 -0700107 { USB_DEVICE(0x0b05, 0x17b5) },
Jaroslav Resleradebaa72012-09-11 17:25:32 +0800108 { USB_DEVICE(0x04ca, 0x2003) },
Manoj Iyer79cd7602012-04-09 09:22:28 -0500109 { USB_DEVICE(0x0489, 0xe042) },
Wen-chien Jesse Sungd13431c2011-11-08 14:30:22 +0800110 { USB_DEVICE(0x413c, 0x8197) },
111
Steven Harms98514032012-04-13 14:45:55 -0400112 /* Foxconn - Hon Hai */
Gustavo Padovanec5bf542012-08-15 01:38:11 -0300113 { USB_VENDOR_AND_INTERFACE_INFO(0x0489, 0xff, 0x01, 0x01) },
Steven Harms98514032012-04-13 14:45:55 -0400114
Gustavo Padovan004251b2012-08-06 15:36:49 -0300115 /*Broadcom devices with vendor specific id */
116 { USB_VENDOR_AND_INTERFACE_INFO(0x0a5c, 0xff, 0x01, 0x01) },
117
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200118 { } /* Terminating entry */
119};
120
121MODULE_DEVICE_TABLE(usb, btusb_table);
122
123static struct usb_device_id blacklist_table[] = {
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200124 /* CSR BlueCore devices */
125 { USB_DEVICE(0x0a12, 0x0001), .driver_info = BTUSB_CSR },
126
127 /* Broadcom BCM2033 without firmware */
128 { USB_DEVICE(0x0a5c, 0x2033), .driver_info = BTUSB_IGNORE },
129
Bala Shanmugambe931122010-11-26 17:35:46 +0530130 /* Atheros 3011 with sflash firmware */
131 { USB_DEVICE(0x0cf3, 0x3002), .driver_info = BTUSB_IGNORE },
Marek Vasut7f801fc2012-06-08 14:32:50 +0200132 { USB_DEVICE(0x0cf3, 0xe019), .driver_info = BTUSB_IGNORE },
Andy Ross2a7bccc2011-05-09 16:11:16 -0700133 { USB_DEVICE(0x13d3, 0x3304), .driver_info = BTUSB_IGNORE },
Ricardo Mendoza8e7c3d22011-07-13 16:04:29 +0100134 { USB_DEVICE(0x0930, 0x0215), .driver_info = BTUSB_IGNORE },
Keng-Yu Lin6b6ba882011-11-30 18:32:37 +0800135 { USB_DEVICE(0x0489, 0xe03d), .driver_info = BTUSB_IGNORE },
Marcos Chaparro2145cea2012-11-06 16:19:11 -0300136 { USB_DEVICE(0x0489, 0xe027), .driver_info = BTUSB_IGNORE },
Bala Shanmugambe931122010-11-26 17:35:46 +0530137
Cho, Yu-Chen509e7862011-01-26 17:10:59 +0800138 /* Atheros AR9285 Malbec with sflash firmware */
139 { USB_DEVICE(0x03f0, 0x311d), .driver_info = BTUSB_IGNORE },
140
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530141 /* Atheros 3012 with sflash firmware */
Ming Lei42c4b742013-03-15 11:00:39 +0800142 { USB_DEVICE(0x0cf3, 0x0036), .driver_info = BTUSB_ATH3012 },
Steven.Li2d25f8b2011-07-01 14:02:36 +0800143 { USB_DEVICE(0x0cf3, 0x3004), .driver_info = BTUSB_ATH3012 },
Cho, Yu-Chen07c0ea82012-03-14 22:01:21 +0200144 { USB_DEVICE(0x0cf3, 0x311d), .driver_info = BTUSB_ATH3012 },
Ming Leic643e012013-03-18 23:45:11 +0800145 { USB_DEVICE(0x0cf3, 0x817a), .driver_info = BTUSB_ATH3012 },
Eran9498ba72011-12-05 22:15:29 +0000146 { USB_DEVICE(0x13d3, 0x3375), .driver_info = BTUSB_ATH3012 },
AceLan Kao55ed7d42012-03-28 10:25:36 +0800147 { USB_DEVICE(0x04ca, 0x3005), .driver_info = BTUSB_ATH3012 },
Daniel Schaal7a8c99e2012-12-29 11:14:34 +0100148 { USB_DEVICE(0x04ca, 0x3006), .driver_info = BTUSB_ATH3012 },
AceLan Kao87522a42012-04-13 17:39:57 +0800149 { USB_DEVICE(0x13d3, 0x3362), .driver_info = BTUSB_ATH3012 },
AceLan Kaoac713112012-04-19 14:53:45 +0800150 { USB_DEVICE(0x0cf3, 0xe004), .driver_info = BTUSB_ATH3012 },
Giancarlo Formicucciac2f15c32012-06-10 08:33:11 +0200151 { USB_DEVICE(0x0930, 0x0219), .driver_info = BTUSB_ATH3012 },
Peng Chen4126da52012-08-01 10:11:59 +0800152 { USB_DEVICE(0x0489, 0xe057), .driver_info = BTUSB_ATH3012 },
AceLan Kao3b36e952012-12-11 11:41:20 +0800153 { USB_DEVICE(0x13d3, 0x3393), .driver_info = BTUSB_ATH3012 },
AceLan Kao340733c2013-01-03 12:24:59 +0800154 { USB_DEVICE(0x0489, 0xe04e), .driver_info = BTUSB_ATH3012 },
AceLan Kao7a344a222013-01-03 12:25:00 +0800155 { USB_DEVICE(0x0489, 0xe056), .driver_info = BTUSB_ATH3012 },
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530156
Cho, Yu-Chene9036e32011-02-15 10:20:07 +0800157 /* Atheros AR5BBU12 with sflash firmware */
158 { USB_DEVICE(0x0489, 0xe02c), .driver_info = BTUSB_IGNORE },
159
Michael Gruetznera1144ca2012-05-02 22:33:40 +0200160 /* Atheros AR5BBU12 with sflash firmware */
161 { USB_DEVICE(0x0489, 0xe03c), .driver_info = BTUSB_ATH3012 },
Yevgeniy Melnichuk1403b362012-08-07 19:48:10 +0530162 { USB_DEVICE(0x0489, 0xe036), .driver_info = BTUSB_ATH3012 },
Michael Gruetznera1144ca2012-05-02 22:33:40 +0200163
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200164 /* Broadcom BCM2035 */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100165 { USB_DEVICE(0x0a5c, 0x2035), .driver_info = BTUSB_WRONG_SCO_MTU },
166 { USB_DEVICE(0x0a5c, 0x200a), .driver_info = BTUSB_WRONG_SCO_MTU },
167 { USB_DEVICE(0x0a5c, 0x2009), .driver_info = BTUSB_BCM92035 },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200168
169 /* Broadcom BCM2045 */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100170 { USB_DEVICE(0x0a5c, 0x2039), .driver_info = BTUSB_WRONG_SCO_MTU },
171 { USB_DEVICE(0x0a5c, 0x2101), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmannbdbef3d2008-09-23 00:16:35 +0200172
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200173 /* IBM/Lenovo ThinkPad with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100174 { USB_DEVICE(0x0a5c, 0x201e), .driver_info = BTUSB_WRONG_SCO_MTU },
175 { USB_DEVICE(0x0a5c, 0x2110), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200176
177 /* HP laptop with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100178 { USB_DEVICE(0x03f0, 0x171d), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200179
180 /* Dell laptop with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100181 { USB_DEVICE(0x413c, 0x8126), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200182
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100183 /* Dell Wireless 370 and 410 devices */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100184 { USB_DEVICE(0x413c, 0x8152), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100185 { USB_DEVICE(0x413c, 0x8156), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200186
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100187 /* Belkin F8T012 and F8T013 devices */
188 { USB_DEVICE(0x050d, 0x0012), .driver_info = BTUSB_WRONG_SCO_MTU },
189 { USB_DEVICE(0x050d, 0x0013), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200190
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100191 /* Asus WL-BTD202 device */
192 { USB_DEVICE(0x0b05, 0x1715), .driver_info = BTUSB_WRONG_SCO_MTU },
193
194 /* Kensington Bluetooth USB adapter */
195 { USB_DEVICE(0x047d, 0x105e), .driver_info = BTUSB_WRONG_SCO_MTU },
196
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200197 /* RTX Telecom based adapters with buggy SCO support */
198 { USB_DEVICE(0x0400, 0x0807), .driver_info = BTUSB_BROKEN_ISOC },
199 { USB_DEVICE(0x0400, 0x080a), .driver_info = BTUSB_BROKEN_ISOC },
200
201 /* CONWISE Technology based adapters with buggy SCO support */
202 { USB_DEVICE(0x0e5e, 0x6622), .driver_info = BTUSB_BROKEN_ISOC },
203
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200204 /* Digianswer devices */
205 { USB_DEVICE(0x08fd, 0x0001), .driver_info = BTUSB_DIGIANSWER },
206 { USB_DEVICE(0x08fd, 0x0002), .driver_info = BTUSB_IGNORE },
207
208 /* CSR BlueCore Bluetooth Sniffer */
209 { USB_DEVICE(0x0a12, 0x0002), .driver_info = BTUSB_SNIFFER },
210
211 /* Frontline ComProbe Bluetooth Sniffer */
212 { USB_DEVICE(0x16d3, 0x0002), .driver_info = BTUSB_SNIFFER },
213
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200214 { } /* Terminating entry */
215};
216
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200217#define BTUSB_MAX_ISOC_FRAMES 10
218
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200219#define BTUSB_INTR_RUNNING 0
220#define BTUSB_BULK_RUNNING 1
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200221#define BTUSB_ISOC_RUNNING 2
Oliver Neukum7bee5492009-08-24 23:44:59 +0200222#define BTUSB_SUSPENDING 3
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -0300223#define BTUSB_DID_ISO_RESUME 4
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200224
225struct btusb_data {
226 struct hci_dev *hdev;
227 struct usb_device *udev;
Marcel Holtmann5fbcd262008-09-23 00:16:36 +0200228 struct usb_interface *intf;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200229 struct usb_interface *isoc;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200230
231 spinlock_t lock;
232
233 unsigned long flags;
234
235 struct work_struct work;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200236 struct work_struct waker;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200237
238 struct usb_anchor tx_anchor;
239 struct usb_anchor intr_anchor;
240 struct usb_anchor bulk_anchor;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200241 struct usb_anchor isoc_anchor;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200242 struct usb_anchor deferred;
243 int tx_in_flight;
244 spinlock_t txlock;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200245
246 struct usb_endpoint_descriptor *intr_ep;
247 struct usb_endpoint_descriptor *bulk_tx_ep;
248 struct usb_endpoint_descriptor *bulk_rx_ep;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200249 struct usb_endpoint_descriptor *isoc_tx_ep;
250 struct usb_endpoint_descriptor *isoc_rx_ep;
251
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100252 __u8 cmdreq_type;
253
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100254 unsigned int sco_num;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200255 int isoc_altsetting;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +0100256 int suspend_count;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200257};
258
Oliver Neukum7bee5492009-08-24 23:44:59 +0200259static int inc_tx(struct btusb_data *data)
260{
261 unsigned long flags;
262 int rv;
263
264 spin_lock_irqsave(&data->txlock, flags);
265 rv = test_bit(BTUSB_SUSPENDING, &data->flags);
266 if (!rv)
267 data->tx_in_flight++;
268 spin_unlock_irqrestore(&data->txlock, flags);
269
270 return rv;
271}
272
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200273static void btusb_intr_complete(struct urb *urb)
274{
275 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +0100276 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200277 int err;
278
279 BT_DBG("%s urb %p status %d count %d", hdev->name,
280 urb, urb->status, urb->actual_length);
281
282 if (!test_bit(HCI_RUNNING, &hdev->flags))
283 return;
284
285 if (urb->status == 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200286 hdev->stat.byte_rx += urb->actual_length;
287
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200288 if (hci_recv_fragment(hdev, HCI_EVENT_PKT,
289 urb->transfer_buffer,
290 urb->actual_length) < 0) {
291 BT_ERR("%s corrupted event packet", hdev->name);
292 hdev->stat.err_rx++;
293 }
294 }
295
296 if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
297 return;
298
Oliver Neukum7bee5492009-08-24 23:44:59 +0200299 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200300 usb_anchor_urb(urb, &data->intr_anchor);
301
302 err = usb_submit_urb(urb, GFP_ATOMIC);
303 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200304 /* -EPERM: urb is being killed;
305 * -ENODEV: device got disconnected */
306 if (err != -EPERM && err != -ENODEV)
Stefan Seyfried61faddf2010-11-30 21:49:08 +0100307 BT_ERR("%s urb %p failed to resubmit (%d)",
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200308 hdev->name, urb, -err);
309 usb_unanchor_urb(urb);
310 }
311}
312
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100313static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200314{
David Herrmann155961e2012-02-09 21:58:32 +0100315 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200316 struct urb *urb;
317 unsigned char *buf;
318 unsigned int pipe;
319 int err, size;
320
321 BT_DBG("%s", hdev->name);
322
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200323 if (!data->intr_ep)
324 return -ENODEV;
325
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100326 urb = usb_alloc_urb(0, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200327 if (!urb)
328 return -ENOMEM;
329
330 size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
331
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100332 buf = kmalloc(size, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200333 if (!buf) {
334 usb_free_urb(urb);
335 return -ENOMEM;
336 }
337
338 pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
339
340 usb_fill_int_urb(urb, data->udev, pipe, buf, size,
341 btusb_intr_complete, hdev,
342 data->intr_ep->bInterval);
343
344 urb->transfer_flags |= URB_FREE_BUFFER;
345
346 usb_anchor_urb(urb, &data->intr_anchor);
347
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100348 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200349 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +0200350 if (err != -EPERM && err != -ENODEV)
351 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200352 hdev->name, urb, -err);
353 usb_unanchor_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200354 }
355
356 usb_free_urb(urb);
357
358 return err;
359}
360
361static void btusb_bulk_complete(struct urb *urb)
362{
363 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +0100364 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200365 int err;
366
367 BT_DBG("%s urb %p status %d count %d", hdev->name,
368 urb, urb->status, urb->actual_length);
369
370 if (!test_bit(HCI_RUNNING, &hdev->flags))
371 return;
372
373 if (urb->status == 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200374 hdev->stat.byte_rx += urb->actual_length;
375
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200376 if (hci_recv_fragment(hdev, HCI_ACLDATA_PKT,
377 urb->transfer_buffer,
378 urb->actual_length) < 0) {
379 BT_ERR("%s corrupted ACL packet", hdev->name);
380 hdev->stat.err_rx++;
381 }
382 }
383
384 if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
385 return;
386
387 usb_anchor_urb(urb, &data->bulk_anchor);
Oliver Neukum652fd782009-12-16 19:23:43 +0100388 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200389
390 err = usb_submit_urb(urb, GFP_ATOMIC);
391 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200392 /* -EPERM: urb is being killed;
393 * -ENODEV: device got disconnected */
394 if (err != -EPERM && err != -ENODEV)
Stefan Seyfried61faddf2010-11-30 21:49:08 +0100395 BT_ERR("%s urb %p failed to resubmit (%d)",
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200396 hdev->name, urb, -err);
397 usb_unanchor_urb(urb);
398 }
399}
400
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100401static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200402{
David Herrmann155961e2012-02-09 21:58:32 +0100403 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200404 struct urb *urb;
405 unsigned char *buf;
406 unsigned int pipe;
Vikram Kandukuri290ba202009-07-02 14:31:59 +0530407 int err, size = HCI_MAX_FRAME_SIZE;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200408
409 BT_DBG("%s", hdev->name);
410
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200411 if (!data->bulk_rx_ep)
412 return -ENODEV;
413
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100414 urb = usb_alloc_urb(0, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200415 if (!urb)
416 return -ENOMEM;
417
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100418 buf = kmalloc(size, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200419 if (!buf) {
420 usb_free_urb(urb);
421 return -ENOMEM;
422 }
423
424 pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
425
426 usb_fill_bulk_urb(urb, data->udev, pipe,
427 buf, size, btusb_bulk_complete, hdev);
428
429 urb->transfer_flags |= URB_FREE_BUFFER;
430
Oliver Neukum7bee5492009-08-24 23:44:59 +0200431 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200432 usb_anchor_urb(urb, &data->bulk_anchor);
433
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100434 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200435 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +0200436 if (err != -EPERM && err != -ENODEV)
437 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200438 hdev->name, urb, -err);
439 usb_unanchor_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200440 }
441
442 usb_free_urb(urb);
443
444 return err;
445}
446
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200447static void btusb_isoc_complete(struct urb *urb)
448{
449 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +0100450 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200451 int i, err;
452
453 BT_DBG("%s urb %p status %d count %d", hdev->name,
454 urb, urb->status, urb->actual_length);
455
456 if (!test_bit(HCI_RUNNING, &hdev->flags))
457 return;
458
459 if (urb->status == 0) {
460 for (i = 0; i < urb->number_of_packets; i++) {
461 unsigned int offset = urb->iso_frame_desc[i].offset;
462 unsigned int length = urb->iso_frame_desc[i].actual_length;
463
464 if (urb->iso_frame_desc[i].status)
465 continue;
466
467 hdev->stat.byte_rx += length;
468
469 if (hci_recv_fragment(hdev, HCI_SCODATA_PKT,
470 urb->transfer_buffer + offset,
471 length) < 0) {
472 BT_ERR("%s corrupted SCO packet", hdev->name);
473 hdev->stat.err_rx++;
474 }
475 }
476 }
477
478 if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags))
479 return;
480
481 usb_anchor_urb(urb, &data->isoc_anchor);
482
483 err = usb_submit_urb(urb, GFP_ATOMIC);
484 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200485 /* -EPERM: urb is being killed;
486 * -ENODEV: device got disconnected */
487 if (err != -EPERM && err != -ENODEV)
Stefan Seyfried61faddf2010-11-30 21:49:08 +0100488 BT_ERR("%s urb %p failed to resubmit (%d)",
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200489 hdev->name, urb, -err);
490 usb_unanchor_urb(urb);
491 }
492}
493
Jesper Juhl42b16b32011-01-17 00:09:38 +0100494static inline void __fill_isoc_descriptor(struct urb *urb, int len, int mtu)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200495{
496 int i, offset = 0;
497
498 BT_DBG("len %d mtu %d", len, mtu);
499
500 for (i = 0; i < BTUSB_MAX_ISOC_FRAMES && len >= mtu;
501 i++, offset += mtu, len -= mtu) {
502 urb->iso_frame_desc[i].offset = offset;
503 urb->iso_frame_desc[i].length = mtu;
504 }
505
506 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
507 urb->iso_frame_desc[i].offset = offset;
508 urb->iso_frame_desc[i].length = len;
509 i++;
510 }
511
512 urb->number_of_packets = i;
513}
514
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100515static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200516{
David Herrmann155961e2012-02-09 21:58:32 +0100517 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200518 struct urb *urb;
519 unsigned char *buf;
520 unsigned int pipe;
521 int err, size;
522
523 BT_DBG("%s", hdev->name);
524
525 if (!data->isoc_rx_ep)
526 return -ENODEV;
527
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100528 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200529 if (!urb)
530 return -ENOMEM;
531
532 size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) *
533 BTUSB_MAX_ISOC_FRAMES;
534
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100535 buf = kmalloc(size, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200536 if (!buf) {
537 usb_free_urb(urb);
538 return -ENOMEM;
539 }
540
541 pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress);
542
Bing Zhaofa0fb932011-12-20 18:19:00 -0800543 usb_fill_int_urb(urb, data->udev, pipe, buf, size, btusb_isoc_complete,
544 hdev, data->isoc_rx_ep->bInterval);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200545
546 urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200547
548 __fill_isoc_descriptor(urb, size,
549 le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize));
550
551 usb_anchor_urb(urb, &data->isoc_anchor);
552
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100553 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200554 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +0200555 if (err != -EPERM && err != -ENODEV)
556 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200557 hdev->name, urb, -err);
558 usb_unanchor_urb(urb);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200559 }
560
561 usb_free_urb(urb);
562
563 return err;
564}
565
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200566static void btusb_tx_complete(struct urb *urb)
567{
568 struct sk_buff *skb = urb->context;
569 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
David Herrmann155961e2012-02-09 21:58:32 +0100570 struct btusb_data *data = hci_get_drvdata(hdev);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200571
572 BT_DBG("%s urb %p status %d count %d", hdev->name,
573 urb, urb->status, urb->actual_length);
574
575 if (!test_bit(HCI_RUNNING, &hdev->flags))
576 goto done;
577
578 if (!urb->status)
579 hdev->stat.byte_tx += urb->transfer_buffer_length;
580 else
581 hdev->stat.err_tx++;
582
583done:
584 spin_lock(&data->txlock);
585 data->tx_in_flight--;
586 spin_unlock(&data->txlock);
587
588 kfree(urb->setup_packet);
589
590 kfree_skb(skb);
591}
592
593static void btusb_isoc_tx_complete(struct urb *urb)
594{
595 struct sk_buff *skb = urb->context;
596 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200597
598 BT_DBG("%s urb %p status %d count %d", hdev->name,
599 urb, urb->status, urb->actual_length);
600
601 if (!test_bit(HCI_RUNNING, &hdev->flags))
602 goto done;
603
604 if (!urb->status)
605 hdev->stat.byte_tx += urb->transfer_buffer_length;
606 else
607 hdev->stat.err_tx++;
608
609done:
610 kfree(urb->setup_packet);
611
612 kfree_skb(skb);
613}
614
615static int btusb_open(struct hci_dev *hdev)
616{
David Herrmann155961e2012-02-09 21:58:32 +0100617 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200618 int err;
619
620 BT_DBG("%s", hdev->name);
621
Oliver Neukum7bee5492009-08-24 23:44:59 +0200622 err = usb_autopm_get_interface(data->intf);
623 if (err < 0)
624 return err;
625
626 data->intf->needs_remote_wakeup = 1;
627
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200628 if (test_and_set_bit(HCI_RUNNING, &hdev->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +0200629 goto done;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200630
631 if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +0200632 goto done;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200633
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100634 err = btusb_submit_intr_urb(hdev, GFP_KERNEL);
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100635 if (err < 0)
636 goto failed;
637
638 err = btusb_submit_bulk_urb(hdev, GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200639 if (err < 0) {
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100640 usb_kill_anchored_urbs(&data->intr_anchor);
641 goto failed;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200642 }
643
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100644 set_bit(BTUSB_BULK_RUNNING, &data->flags);
645 btusb_submit_bulk_urb(hdev, GFP_KERNEL);
646
Oliver Neukum7bee5492009-08-24 23:44:59 +0200647done:
648 usb_autopm_put_interface(data->intf);
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100649 return 0;
650
651failed:
652 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
653 clear_bit(HCI_RUNNING, &hdev->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200654 usb_autopm_put_interface(data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200655 return err;
656}
657
Oliver Neukum7bee5492009-08-24 23:44:59 +0200658static void btusb_stop_traffic(struct btusb_data *data)
659{
660 usb_kill_anchored_urbs(&data->intr_anchor);
661 usb_kill_anchored_urbs(&data->bulk_anchor);
662 usb_kill_anchored_urbs(&data->isoc_anchor);
663}
664
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200665static int btusb_close(struct hci_dev *hdev)
666{
David Herrmann155961e2012-02-09 21:58:32 +0100667 struct btusb_data *data = hci_get_drvdata(hdev);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200668 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200669
670 BT_DBG("%s", hdev->name);
671
672 if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
673 return 0;
674
Marcel Holtmanne8c3c3d2008-09-23 00:16:36 +0200675 cancel_work_sync(&data->work);
Linus Torvalds404291a2009-11-11 13:32:29 -0800676 cancel_work_sync(&data->waker);
Marcel Holtmanne8c3c3d2008-09-23 00:16:36 +0200677
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200678 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200679 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200680 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200681
682 btusb_stop_traffic(data);
683 err = usb_autopm_get_interface(data->intf);
684 if (err < 0)
Oliver Neukum7b8e2c12009-11-13 14:26:23 +0100685 goto failed;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200686
687 data->intf->needs_remote_wakeup = 0;
688 usb_autopm_put_interface(data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200689
Oliver Neukum7b8e2c12009-11-13 14:26:23 +0100690failed:
691 usb_scuttle_anchored_urbs(&data->deferred);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200692 return 0;
693}
694
695static int btusb_flush(struct hci_dev *hdev)
696{
David Herrmann155961e2012-02-09 21:58:32 +0100697 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200698
699 BT_DBG("%s", hdev->name);
700
701 usb_kill_anchored_urbs(&data->tx_anchor);
702
703 return 0;
704}
705
706static int btusb_send_frame(struct sk_buff *skb)
707{
708 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
David Herrmann155961e2012-02-09 21:58:32 +0100709 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200710 struct usb_ctrlrequest *dr;
711 struct urb *urb;
712 unsigned int pipe;
713 int err;
714
715 BT_DBG("%s", hdev->name);
716
717 if (!test_bit(HCI_RUNNING, &hdev->flags))
718 return -EBUSY;
719
720 switch (bt_cb(skb)->pkt_type) {
721 case HCI_COMMAND_PKT:
722 urb = usb_alloc_urb(0, GFP_ATOMIC);
723 if (!urb)
724 return -ENOMEM;
725
726 dr = kmalloc(sizeof(*dr), GFP_ATOMIC);
727 if (!dr) {
728 usb_free_urb(urb);
729 return -ENOMEM;
730 }
731
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100732 dr->bRequestType = data->cmdreq_type;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200733 dr->bRequest = 0;
734 dr->wIndex = 0;
735 dr->wValue = 0;
736 dr->wLength = __cpu_to_le16(skb->len);
737
738 pipe = usb_sndctrlpipe(data->udev, 0x00);
739
740 usb_fill_control_urb(urb, data->udev, pipe, (void *) dr,
741 skb->data, skb->len, btusb_tx_complete, skb);
742
743 hdev->stat.cmd_tx++;
744 break;
745
746 case HCI_ACLDATA_PKT:
Peter Hurley9fd481e2011-07-14 08:48:32 -0400747 if (!data->bulk_tx_ep)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200748 return -ENODEV;
749
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200750 urb = usb_alloc_urb(0, GFP_ATOMIC);
751 if (!urb)
752 return -ENOMEM;
753
754 pipe = usb_sndbulkpipe(data->udev,
755 data->bulk_tx_ep->bEndpointAddress);
756
757 usb_fill_bulk_urb(urb, data->udev, pipe,
758 skb->data, skb->len, btusb_tx_complete, skb);
759
760 hdev->stat.acl_tx++;
761 break;
762
763 case HCI_SCODATA_PKT:
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200764 if (!data->isoc_tx_ep || hdev->conn_hash.sco_num < 1)
765 return -ENODEV;
766
767 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_ATOMIC);
768 if (!urb)
769 return -ENOMEM;
770
771 pipe = usb_sndisocpipe(data->udev,
772 data->isoc_tx_ep->bEndpointAddress);
773
Gustavo F. Padovan03c2d0e2011-02-14 18:53:43 -0300774 usb_fill_int_urb(urb, data->udev, pipe,
775 skb->data, skb->len, btusb_isoc_tx_complete,
776 skb, data->isoc_tx_ep->bInterval);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200777
778 urb->transfer_flags = URB_ISO_ASAP;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200779
780 __fill_isoc_descriptor(urb, skb->len,
781 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
782
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200783 hdev->stat.sco_tx++;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200784 goto skip_waking;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200785
786 default:
787 return -EILSEQ;
788 }
789
Oliver Neukum7bee5492009-08-24 23:44:59 +0200790 err = inc_tx(data);
791 if (err) {
792 usb_anchor_urb(urb, &data->deferred);
793 schedule_work(&data->waker);
794 err = 0;
795 goto done;
796 }
797
798skip_waking:
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200799 usb_anchor_urb(urb, &data->tx_anchor);
800
801 err = usb_submit_urb(urb, GFP_ATOMIC);
802 if (err < 0) {
Paul Bolle5a9b80e2011-10-09 12:12:16 +0200803 if (err != -EPERM && err != -ENODEV)
804 BT_ERR("%s urb %p submission failed (%d)",
805 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200806 kfree(urb->setup_packet);
807 usb_unanchor_urb(urb);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200808 } else {
809 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200810 }
811
Oliver Neukum7bee5492009-08-24 23:44:59 +0200812done:
Cong Wang54a8a792011-11-22 09:32:57 +0800813 usb_free_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200814 return err;
815}
816
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200817static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
818{
David Herrmann155961e2012-02-09 21:58:32 +0100819 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200820
821 BT_DBG("%s evt %d", hdev->name, evt);
822
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100823 if (hdev->conn_hash.sco_num != data->sco_num) {
824 data->sco_num = hdev->conn_hash.sco_num;
825 schedule_work(&data->work);
Marcel Holtmanna780efa2008-11-30 12:17:12 +0100826 }
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200827}
828
Jesper Juhl42b16b32011-01-17 00:09:38 +0100829static inline int __set_isoc_interface(struct hci_dev *hdev, int altsetting)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200830{
David Herrmann155961e2012-02-09 21:58:32 +0100831 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200832 struct usb_interface *intf = data->isoc;
833 struct usb_endpoint_descriptor *ep_desc;
834 int i, err;
835
836 if (!data->isoc)
837 return -ENODEV;
838
839 err = usb_set_interface(data->udev, 1, altsetting);
840 if (err < 0) {
841 BT_ERR("%s setting interface failed (%d)", hdev->name, -err);
842 return err;
843 }
844
845 data->isoc_altsetting = altsetting;
846
847 data->isoc_tx_ep = NULL;
848 data->isoc_rx_ep = NULL;
849
850 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
851 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
852
853 if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) {
854 data->isoc_tx_ep = ep_desc;
855 continue;
856 }
857
858 if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) {
859 data->isoc_rx_ep = ep_desc;
860 continue;
861 }
862 }
863
864 if (!data->isoc_tx_ep || !data->isoc_rx_ep) {
865 BT_ERR("%s invalid SCO descriptors", hdev->name);
866 return -ENODEV;
867 }
868
869 return 0;
870}
871
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200872static void btusb_work(struct work_struct *work)
873{
874 struct btusb_data *data = container_of(work, struct btusb_data, work);
875 struct hci_dev *hdev = data->hdev;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200876 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200877
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200878 if (hdev->conn_hash.sco_num > 0) {
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -0300879 if (!test_bit(BTUSB_DID_ISO_RESUME, &data->flags)) {
Oliver Neukum8efdd0c2011-02-11 13:00:06 +0100880 err = usb_autopm_get_interface(data->isoc ? data->isoc : data->intf);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200881 if (err < 0) {
882 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
883 usb_kill_anchored_urbs(&data->isoc_anchor);
884 return;
885 }
886
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -0300887 set_bit(BTUSB_DID_ISO_RESUME, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200888 }
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200889 if (data->isoc_altsetting != 2) {
890 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
891 usb_kill_anchored_urbs(&data->isoc_anchor);
892
893 if (__set_isoc_interface(hdev, 2) < 0)
894 return;
895 }
896
897 if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100898 if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200899 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
900 else
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100901 btusb_submit_isoc_urb(hdev, GFP_KERNEL);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200902 }
903 } else {
904 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
905 usb_kill_anchored_urbs(&data->isoc_anchor);
906
907 __set_isoc_interface(hdev, 0);
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -0300908 if (test_and_clear_bit(BTUSB_DID_ISO_RESUME, &data->flags))
Oliver Neukum8efdd0c2011-02-11 13:00:06 +0100909 usb_autopm_put_interface(data->isoc ? data->isoc : data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200910 }
911}
912
Oliver Neukum7bee5492009-08-24 23:44:59 +0200913static void btusb_waker(struct work_struct *work)
914{
915 struct btusb_data *data = container_of(work, struct btusb_data, waker);
916 int err;
917
918 err = usb_autopm_get_interface(data->intf);
919 if (err < 0)
920 return;
921
922 usb_autopm_put_interface(data->intf);
923}
924
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200925static int btusb_probe(struct usb_interface *intf,
926 const struct usb_device_id *id)
927{
928 struct usb_endpoint_descriptor *ep_desc;
929 struct btusb_data *data;
930 struct hci_dev *hdev;
931 int i, err;
932
933 BT_DBG("intf %p id %p", intf, id);
934
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200935 /* interface numbers are hardcoded in the spec */
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200936 if (intf->cur_altsetting->desc.bInterfaceNumber != 0)
937 return -ENODEV;
938
939 if (!id->driver_info) {
940 const struct usb_device_id *match;
941 match = usb_match_id(intf, blacklist_table);
942 if (match)
943 id = match;
944 }
945
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200946 if (id->driver_info == BTUSB_IGNORE)
947 return -ENODEV;
948
949 if (ignore_dga && id->driver_info & BTUSB_DIGIANSWER)
950 return -ENODEV;
951
952 if (ignore_csr && id->driver_info & BTUSB_CSR)
953 return -ENODEV;
954
955 if (ignore_sniffer && id->driver_info & BTUSB_SNIFFER)
956 return -ENODEV;
957
Steven.Li2d25f8b2011-07-01 14:02:36 +0800958 if (id->driver_info & BTUSB_ATH3012) {
959 struct usb_device *udev = interface_to_usbdev(intf);
960
961 /* Old firmware would otherwise let ath3k driver load
962 * patch and sysconfig files */
963 if (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x0001)
964 return -ENODEV;
965 }
966
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200967 data = kzalloc(sizeof(*data), GFP_KERNEL);
968 if (!data)
969 return -ENOMEM;
970
971 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
972 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
973
974 if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
975 data->intr_ep = ep_desc;
976 continue;
977 }
978
979 if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
980 data->bulk_tx_ep = ep_desc;
981 continue;
982 }
983
984 if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
985 data->bulk_rx_ep = ep_desc;
986 continue;
987 }
988 }
989
990 if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep) {
991 kfree(data);
992 return -ENODEV;
993 }
994
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100995 data->cmdreq_type = USB_TYPE_CLASS;
996
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200997 data->udev = interface_to_usbdev(intf);
Marcel Holtmann5fbcd262008-09-23 00:16:36 +0200998 data->intf = intf;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200999
1000 spin_lock_init(&data->lock);
1001
1002 INIT_WORK(&data->work, btusb_work);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001003 INIT_WORK(&data->waker, btusb_waker);
1004 spin_lock_init(&data->txlock);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001005
1006 init_usb_anchor(&data->tx_anchor);
1007 init_usb_anchor(&data->intr_anchor);
1008 init_usb_anchor(&data->bulk_anchor);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001009 init_usb_anchor(&data->isoc_anchor);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001010 init_usb_anchor(&data->deferred);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001011
1012 hdev = hci_alloc_dev();
1013 if (!hdev) {
1014 kfree(data);
1015 return -ENOMEM;
1016 }
1017
Marcel Holtmannc13854ce2010-02-08 15:27:07 +01001018 hdev->bus = HCI_USB;
David Herrmann155961e2012-02-09 21:58:32 +01001019 hci_set_drvdata(hdev, data);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001020
1021 data->hdev = hdev;
1022
1023 SET_HCIDEV_DEV(hdev, &intf->dev);
1024
1025 hdev->open = btusb_open;
1026 hdev->close = btusb_close;
1027 hdev->flush = btusb_flush;
1028 hdev->send = btusb_send_frame;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001029 hdev->notify = btusb_notify;
1030
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01001031 /* Interface numbers are hardcoded in the specification */
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001032 data->isoc = usb_ifnum_to_if(data->udev, 1);
1033
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01001034 if (!reset)
1035 set_bit(HCI_QUIRK_NO_RESET, &hdev->quirks);
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02001036
1037 if (force_scofix || id->driver_info & BTUSB_WRONG_SCO_MTU) {
1038 if (!disable_scofix)
1039 set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
1040 }
1041
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001042 if (id->driver_info & BTUSB_BROKEN_ISOC)
1043 data->isoc = NULL;
1044
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01001045 if (id->driver_info & BTUSB_DIGIANSWER) {
1046 data->cmdreq_type = USB_TYPE_VENDOR;
1047 set_bit(HCI_QUIRK_NO_RESET, &hdev->quirks);
1048 }
1049
1050 if (id->driver_info & BTUSB_CSR) {
1051 struct usb_device *udev = data->udev;
1052
1053 /* Old firmware would otherwise execute USB reset */
1054 if (le16_to_cpu(udev->descriptor.bcdDevice) < 0x117)
1055 set_bit(HCI_QUIRK_NO_RESET, &hdev->quirks);
1056 }
1057
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02001058 if (id->driver_info & BTUSB_SNIFFER) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001059 struct usb_device *udev = data->udev;
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02001060
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01001061 /* New sniffer firmware has crippled HCI interface */
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02001062 if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
1063 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001064
1065 data->isoc = NULL;
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02001066 }
1067
1068 if (id->driver_info & BTUSB_BCM92035) {
1069 unsigned char cmd[] = { 0x3b, 0xfc, 0x01, 0x00 };
1070 struct sk_buff *skb;
1071
1072 skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL);
1073 if (skb) {
1074 memcpy(skb_put(skb, sizeof(cmd)), cmd, sizeof(cmd));
1075 skb_queue_tail(&hdev->driver_init, skb);
1076 }
1077 }
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001078
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001079 if (data->isoc) {
1080 err = usb_driver_claim_interface(&btusb_driver,
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02001081 data->isoc, data);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001082 if (err < 0) {
1083 hci_free_dev(hdev);
1084 kfree(data);
1085 return err;
1086 }
1087 }
1088
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001089 err = hci_register_dev(hdev);
1090 if (err < 0) {
1091 hci_free_dev(hdev);
1092 kfree(data);
1093 return err;
1094 }
1095
1096 usb_set_intfdata(intf, data);
1097
1098 return 0;
1099}
1100
1101static void btusb_disconnect(struct usb_interface *intf)
1102{
1103 struct btusb_data *data = usb_get_intfdata(intf);
1104 struct hci_dev *hdev;
1105
1106 BT_DBG("intf %p", intf);
1107
1108 if (!data)
1109 return;
1110
1111 hdev = data->hdev;
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02001112 usb_set_intfdata(data->intf, NULL);
1113
1114 if (data->isoc)
1115 usb_set_intfdata(data->isoc, NULL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001116
1117 hci_unregister_dev(hdev);
1118
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02001119 if (intf == data->isoc)
1120 usb_driver_release_interface(&btusb_driver, data->intf);
1121 else if (data->isoc)
1122 usb_driver_release_interface(&btusb_driver, data->isoc);
1123
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001124 hci_free_dev(hdev);
David Herrmann83810882012-01-07 15:47:16 +01001125 kfree(data);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001126}
1127
Oliver Neukum7bee5492009-08-24 23:44:59 +02001128#ifdef CONFIG_PM
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001129static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
1130{
1131 struct btusb_data *data = usb_get_intfdata(intf);
1132
1133 BT_DBG("intf %p", intf);
1134
1135 if (data->suspend_count++)
1136 return 0;
1137
Oliver Neukum7bee5492009-08-24 23:44:59 +02001138 spin_lock_irq(&data->txlock);
Alan Stern5b1b0b82011-08-19 23:49:48 +02001139 if (!(PMSG_IS_AUTO(message) && data->tx_in_flight)) {
Oliver Neukum7bee5492009-08-24 23:44:59 +02001140 set_bit(BTUSB_SUSPENDING, &data->flags);
1141 spin_unlock_irq(&data->txlock);
1142 } else {
1143 spin_unlock_irq(&data->txlock);
1144 data->suspend_count--;
1145 return -EBUSY;
1146 }
1147
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001148 cancel_work_sync(&data->work);
1149
Oliver Neukum7bee5492009-08-24 23:44:59 +02001150 btusb_stop_traffic(data);
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001151 usb_kill_anchored_urbs(&data->tx_anchor);
1152
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001153 return 0;
1154}
1155
Oliver Neukum7bee5492009-08-24 23:44:59 +02001156static void play_deferred(struct btusb_data *data)
1157{
1158 struct urb *urb;
1159 int err;
1160
1161 while ((urb = usb_get_from_anchor(&data->deferred))) {
1162 err = usb_submit_urb(urb, GFP_ATOMIC);
1163 if (err < 0)
1164 break;
1165
1166 data->tx_in_flight++;
1167 }
1168 usb_scuttle_anchored_urbs(&data->deferred);
1169}
1170
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001171static int btusb_resume(struct usb_interface *intf)
1172{
1173 struct btusb_data *data = usb_get_intfdata(intf);
1174 struct hci_dev *hdev = data->hdev;
Oliver Neukum7bee5492009-08-24 23:44:59 +02001175 int err = 0;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001176
1177 BT_DBG("intf %p", intf);
1178
1179 if (--data->suspend_count)
1180 return 0;
1181
1182 if (!test_bit(HCI_RUNNING, &hdev->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +02001183 goto done;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001184
1185 if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
1186 err = btusb_submit_intr_urb(hdev, GFP_NOIO);
1187 if (err < 0) {
1188 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001189 goto failed;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001190 }
1191 }
1192
1193 if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) {
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001194 err = btusb_submit_bulk_urb(hdev, GFP_NOIO);
1195 if (err < 0) {
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001196 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001197 goto failed;
1198 }
1199
1200 btusb_submit_bulk_urb(hdev, GFP_NOIO);
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001201 }
1202
1203 if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
1204 if (btusb_submit_isoc_urb(hdev, GFP_NOIO) < 0)
1205 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1206 else
1207 btusb_submit_isoc_urb(hdev, GFP_NOIO);
1208 }
1209
Oliver Neukum7bee5492009-08-24 23:44:59 +02001210 spin_lock_irq(&data->txlock);
1211 play_deferred(data);
1212 clear_bit(BTUSB_SUSPENDING, &data->flags);
1213 spin_unlock_irq(&data->txlock);
1214 schedule_work(&data->work);
1215
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001216 return 0;
Oliver Neukum7bee5492009-08-24 23:44:59 +02001217
1218failed:
1219 usb_scuttle_anchored_urbs(&data->deferred);
1220done:
1221 spin_lock_irq(&data->txlock);
1222 clear_bit(BTUSB_SUSPENDING, &data->flags);
1223 spin_unlock_irq(&data->txlock);
1224
1225 return err;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001226}
Oliver Neukum7bee5492009-08-24 23:44:59 +02001227#endif
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001228
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001229static struct usb_driver btusb_driver = {
1230 .name = "btusb",
1231 .probe = btusb_probe,
1232 .disconnect = btusb_disconnect,
Oliver Neukum7bee5492009-08-24 23:44:59 +02001233#ifdef CONFIG_PM
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001234 .suspend = btusb_suspend,
1235 .resume = btusb_resume,
Oliver Neukum7bee5492009-08-24 23:44:59 +02001236#endif
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001237 .id_table = btusb_table,
Oliver Neukum7bee5492009-08-24 23:44:59 +02001238 .supports_autosuspend = 1,
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001239};
1240
Greg Kroah-Hartman93f15082011-11-18 09:47:34 -08001241module_usb_driver(btusb_driver);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001242
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02001243module_param(ignore_dga, bool, 0644);
1244MODULE_PARM_DESC(ignore_dga, "Ignore devices with id 08fd:0001");
1245
1246module_param(ignore_csr, bool, 0644);
1247MODULE_PARM_DESC(ignore_csr, "Ignore devices with id 0a12:0001");
1248
1249module_param(ignore_sniffer, bool, 0644);
1250MODULE_PARM_DESC(ignore_sniffer, "Ignore devices with id 0a12:0002");
1251
1252module_param(disable_scofix, bool, 0644);
1253MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
1254
1255module_param(force_scofix, bool, 0644);
1256MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
1257
1258module_param(reset, bool, 0644);
1259MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
1260
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001261MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
1262MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
1263MODULE_VERSION(VERSION);
1264MODULE_LICENSE("GPL");