blob: 93cc828298c4d6385d80b8644bb7970036b7df11 [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
Oliver Neukumc510eae2011-09-21 11:41:45 +020063 /* Broadcom SoftSailing reporting vendor specific */
Don Zickus2e8b50632012-03-28 16:41:11 -040064 { USB_DEVICE(0x0a5c, 0x21e1) },
Oliver Neukumc510eae2011-09-21 11:41:45 +020065
Nobuhiro Iwamatsu3cd01972010-08-20 16:24:07 +090066 /* Apple MacBookPro 7,1 */
67 { USB_DEVICE(0x05ac, 0x8213) },
68
Cyril Lacoux0a79f672010-07-14 10:29:27 +040069 /* Apple iMac11,1 */
70 { USB_DEVICE(0x05ac, 0x8215) },
71
Nobuhiro Iwamatsu9c047152010-08-20 16:24:06 +090072 /* Apple MacBookPro6,2 */
73 { USB_DEVICE(0x05ac, 0x8218) },
74
Edgar (gimli) Hucek3e3ede72010-11-04 08:04:33 +010075 /* Apple MacBookAir3,1, MacBookAir3,2 */
76 { USB_DEVICE(0x05ac, 0x821b) },
77
Pieter-Augustijn Van Malleghema63b7232011-09-07 02:28:10 -040078 /* Apple MacBookAir4,1 */
79 { USB_DEVICE(0x05ac, 0x821f) },
80
Marc-Antoine Perennou88d377b2011-03-24 14:51:21 -030081 /* Apple MacBookPro8,2 */
82 { USB_DEVICE(0x05ac, 0x821a) },
83
Jurgen Kramerf78b68262011-09-04 18:01:42 +020084 /* Apple MacMini5,1 */
85 { USB_DEVICE(0x05ac, 0x8281) },
86
Marcel Holtmanncfeb4142008-08-07 22:26:56 +020087 /* AVM BlueFRITZ! USB v2.0 */
88 { USB_DEVICE(0x057c, 0x3800) },
89
90 /* Bluetooth Ultraport Module from IBM */
91 { USB_DEVICE(0x04bf, 0x030a) },
92
93 /* ALPS Modules with non-standard id */
94 { USB_DEVICE(0x044e, 0x3001) },
95 { USB_DEVICE(0x044e, 0x3002) },
96
97 /* Ericsson with non-standard id */
98 { USB_DEVICE(0x0bdb, 0x1002) },
99
100 /* Canyon CN-BTU1 with HID interfaces */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100101 { USB_DEVICE(0x0c10, 0x0000) },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200102
Wen-chien Jesse Sungd13431c2011-11-08 14:30:22 +0800103 /* Broadcom BCM20702A0 */
Manoj Iyer79cd7602012-04-09 09:22:28 -0500104 { USB_DEVICE(0x0489, 0xe042) },
Jesse Sungc0190922011-12-22 10:48:47 +0800105 { USB_DEVICE(0x0a5c, 0x21e3) },
James M. Leddy0a4eaee2012-03-06 02:41:33 +0200106 { USB_DEVICE(0x0a5c, 0x21e6) },
João Paulo Rechi Vita6dfc3262012-03-14 21:45:16 +0200107 { USB_DEVICE(0x0a5c, 0x21e8) },
Manoj Iyer37305cf2012-02-02 09:32:36 -0600108 { USB_DEVICE(0x0a5c, 0x21f3) },
Wen-chien Jesse Sungd13431c2011-11-08 14:30:22 +0800109 { USB_DEVICE(0x413c, 0x8197) },
110
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200111 { } /* Terminating entry */
112};
113
114MODULE_DEVICE_TABLE(usb, btusb_table);
115
116static struct usb_device_id blacklist_table[] = {
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200117 /* CSR BlueCore devices */
118 { USB_DEVICE(0x0a12, 0x0001), .driver_info = BTUSB_CSR },
119
120 /* Broadcom BCM2033 without firmware */
121 { USB_DEVICE(0x0a5c, 0x2033), .driver_info = BTUSB_IGNORE },
122
Bala Shanmugambe931122010-11-26 17:35:46 +0530123 /* Atheros 3011 with sflash firmware */
124 { USB_DEVICE(0x0cf3, 0x3002), .driver_info = BTUSB_IGNORE },
Andy Ross2a7bccc2011-05-09 16:11:16 -0700125 { USB_DEVICE(0x13d3, 0x3304), .driver_info = BTUSB_IGNORE },
Ricardo Mendoza8e7c3d22011-07-13 16:04:29 +0100126 { USB_DEVICE(0x0930, 0x0215), .driver_info = BTUSB_IGNORE },
Keng-Yu Lin6b6ba882011-11-30 18:32:37 +0800127 { USB_DEVICE(0x0489, 0xe03d), .driver_info = BTUSB_IGNORE },
Bala Shanmugambe931122010-11-26 17:35:46 +0530128
Cho, Yu-Chen509e7862011-01-26 17:10:59 +0800129 /* Atheros AR9285 Malbec with sflash firmware */
130 { USB_DEVICE(0x03f0, 0x311d), .driver_info = BTUSB_IGNORE },
131
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530132 /* Atheros 3012 with sflash firmware */
Steven.Li2d25f8b2011-07-01 14:02:36 +0800133 { USB_DEVICE(0x0cf3, 0x3004), .driver_info = BTUSB_ATH3012 },
Cho, Yu-Chen07c0ea82012-03-14 22:01:21 +0200134 { USB_DEVICE(0x0cf3, 0x311d), .driver_info = BTUSB_ATH3012 },
Eran9498ba72011-12-05 22:15:29 +0000135 { USB_DEVICE(0x13d3, 0x3375), .driver_info = BTUSB_ATH3012 },
AceLan Kao55ed7d42012-03-28 10:25:36 +0800136 { USB_DEVICE(0x04ca, 0x3005), .driver_info = BTUSB_ATH3012 },
AceLan Kao87522a42012-04-13 17:39:57 +0800137 { USB_DEVICE(0x13d3, 0x3362), .driver_info = BTUSB_ATH3012 },
AceLan Kaoac713112012-04-19 14:53:45 +0800138 { USB_DEVICE(0x0cf3, 0xe004), .driver_info = BTUSB_ATH3012 },
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530139
Cho, Yu-Chene9036e32011-02-15 10:20:07 +0800140 /* Atheros AR5BBU12 with sflash firmware */
141 { USB_DEVICE(0x0489, 0xe02c), .driver_info = BTUSB_IGNORE },
142
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200143 /* Broadcom BCM2035 */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100144 { USB_DEVICE(0x0a5c, 0x2035), .driver_info = BTUSB_WRONG_SCO_MTU },
145 { USB_DEVICE(0x0a5c, 0x200a), .driver_info = BTUSB_WRONG_SCO_MTU },
146 { USB_DEVICE(0x0a5c, 0x2009), .driver_info = BTUSB_BCM92035 },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200147
148 /* Broadcom BCM2045 */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100149 { USB_DEVICE(0x0a5c, 0x2039), .driver_info = BTUSB_WRONG_SCO_MTU },
150 { USB_DEVICE(0x0a5c, 0x2101), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmannbdbef3d2008-09-23 00:16:35 +0200151
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200152 /* IBM/Lenovo ThinkPad with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100153 { USB_DEVICE(0x0a5c, 0x201e), .driver_info = BTUSB_WRONG_SCO_MTU },
154 { USB_DEVICE(0x0a5c, 0x2110), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200155
156 /* HP laptop with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100157 { USB_DEVICE(0x03f0, 0x171d), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200158
159 /* Dell laptop with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100160 { USB_DEVICE(0x413c, 0x8126), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200161
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100162 /* Dell Wireless 370 and 410 devices */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100163 { USB_DEVICE(0x413c, 0x8152), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100164 { USB_DEVICE(0x413c, 0x8156), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200165
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100166 /* Belkin F8T012 and F8T013 devices */
167 { USB_DEVICE(0x050d, 0x0012), .driver_info = BTUSB_WRONG_SCO_MTU },
168 { USB_DEVICE(0x050d, 0x0013), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200169
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100170 /* Asus WL-BTD202 device */
171 { USB_DEVICE(0x0b05, 0x1715), .driver_info = BTUSB_WRONG_SCO_MTU },
172
173 /* Kensington Bluetooth USB adapter */
174 { USB_DEVICE(0x047d, 0x105e), .driver_info = BTUSB_WRONG_SCO_MTU },
175
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200176 /* RTX Telecom based adapters with buggy SCO support */
177 { USB_DEVICE(0x0400, 0x0807), .driver_info = BTUSB_BROKEN_ISOC },
178 { USB_DEVICE(0x0400, 0x080a), .driver_info = BTUSB_BROKEN_ISOC },
179
180 /* CONWISE Technology based adapters with buggy SCO support */
181 { USB_DEVICE(0x0e5e, 0x6622), .driver_info = BTUSB_BROKEN_ISOC },
182
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200183 /* Digianswer devices */
184 { USB_DEVICE(0x08fd, 0x0001), .driver_info = BTUSB_DIGIANSWER },
185 { USB_DEVICE(0x08fd, 0x0002), .driver_info = BTUSB_IGNORE },
186
187 /* CSR BlueCore Bluetooth Sniffer */
188 { USB_DEVICE(0x0a12, 0x0002), .driver_info = BTUSB_SNIFFER },
189
190 /* Frontline ComProbe Bluetooth Sniffer */
191 { USB_DEVICE(0x16d3, 0x0002), .driver_info = BTUSB_SNIFFER },
192
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200193 { } /* Terminating entry */
194};
195
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200196#define BTUSB_MAX_ISOC_FRAMES 10
197
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200198#define BTUSB_INTR_RUNNING 0
199#define BTUSB_BULK_RUNNING 1
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200200#define BTUSB_ISOC_RUNNING 2
Oliver Neukum7bee5492009-08-24 23:44:59 +0200201#define BTUSB_SUSPENDING 3
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -0300202#define BTUSB_DID_ISO_RESUME 4
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200203
204struct btusb_data {
205 struct hci_dev *hdev;
206 struct usb_device *udev;
Marcel Holtmann5fbcd262008-09-23 00:16:36 +0200207 struct usb_interface *intf;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200208 struct usb_interface *isoc;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200209
210 spinlock_t lock;
211
212 unsigned long flags;
213
214 struct work_struct work;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200215 struct work_struct waker;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200216
217 struct usb_anchor tx_anchor;
218 struct usb_anchor intr_anchor;
219 struct usb_anchor bulk_anchor;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200220 struct usb_anchor isoc_anchor;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200221 struct usb_anchor deferred;
222 int tx_in_flight;
223 spinlock_t txlock;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200224
225 struct usb_endpoint_descriptor *intr_ep;
226 struct usb_endpoint_descriptor *bulk_tx_ep;
227 struct usb_endpoint_descriptor *bulk_rx_ep;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200228 struct usb_endpoint_descriptor *isoc_tx_ep;
229 struct usb_endpoint_descriptor *isoc_rx_ep;
230
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100231 __u8 cmdreq_type;
232
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100233 unsigned int sco_num;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200234 int isoc_altsetting;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +0100235 int suspend_count;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200236};
237
Oliver Neukum7bee5492009-08-24 23:44:59 +0200238static int inc_tx(struct btusb_data *data)
239{
240 unsigned long flags;
241 int rv;
242
243 spin_lock_irqsave(&data->txlock, flags);
244 rv = test_bit(BTUSB_SUSPENDING, &data->flags);
245 if (!rv)
246 data->tx_in_flight++;
247 spin_unlock_irqrestore(&data->txlock, flags);
248
249 return rv;
250}
251
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200252static void btusb_intr_complete(struct urb *urb)
253{
254 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +0100255 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200256 int err;
257
258 BT_DBG("%s urb %p status %d count %d", hdev->name,
259 urb, urb->status, urb->actual_length);
260
261 if (!test_bit(HCI_RUNNING, &hdev->flags))
262 return;
263
264 if (urb->status == 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200265 hdev->stat.byte_rx += urb->actual_length;
266
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200267 if (hci_recv_fragment(hdev, HCI_EVENT_PKT,
268 urb->transfer_buffer,
269 urb->actual_length) < 0) {
270 BT_ERR("%s corrupted event packet", hdev->name);
271 hdev->stat.err_rx++;
272 }
273 }
274
275 if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
276 return;
277
Oliver Neukum7bee5492009-08-24 23:44:59 +0200278 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200279 usb_anchor_urb(urb, &data->intr_anchor);
280
281 err = usb_submit_urb(urb, GFP_ATOMIC);
282 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200283 /* -EPERM: urb is being killed;
284 * -ENODEV: device got disconnected */
285 if (err != -EPERM && err != -ENODEV)
Stefan Seyfried61faddf2010-11-30 21:49:08 +0100286 BT_ERR("%s urb %p failed to resubmit (%d)",
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200287 hdev->name, urb, -err);
288 usb_unanchor_urb(urb);
289 }
290}
291
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100292static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200293{
David Herrmann155961e2012-02-09 21:58:32 +0100294 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200295 struct urb *urb;
296 unsigned char *buf;
297 unsigned int pipe;
298 int err, size;
299
300 BT_DBG("%s", hdev->name);
301
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200302 if (!data->intr_ep)
303 return -ENODEV;
304
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100305 urb = usb_alloc_urb(0, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200306 if (!urb)
307 return -ENOMEM;
308
309 size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
310
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100311 buf = kmalloc(size, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200312 if (!buf) {
313 usb_free_urb(urb);
314 return -ENOMEM;
315 }
316
317 pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
318
319 usb_fill_int_urb(urb, data->udev, pipe, buf, size,
320 btusb_intr_complete, hdev,
321 data->intr_ep->bInterval);
322
323 urb->transfer_flags |= URB_FREE_BUFFER;
324
325 usb_anchor_urb(urb, &data->intr_anchor);
326
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100327 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200328 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +0200329 if (err != -EPERM && err != -ENODEV)
330 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200331 hdev->name, urb, -err);
332 usb_unanchor_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200333 }
334
335 usb_free_urb(urb);
336
337 return err;
338}
339
340static void btusb_bulk_complete(struct urb *urb)
341{
342 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +0100343 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200344 int err;
345
346 BT_DBG("%s urb %p status %d count %d", hdev->name,
347 urb, urb->status, urb->actual_length);
348
349 if (!test_bit(HCI_RUNNING, &hdev->flags))
350 return;
351
352 if (urb->status == 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200353 hdev->stat.byte_rx += urb->actual_length;
354
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200355 if (hci_recv_fragment(hdev, HCI_ACLDATA_PKT,
356 urb->transfer_buffer,
357 urb->actual_length) < 0) {
358 BT_ERR("%s corrupted ACL packet", hdev->name);
359 hdev->stat.err_rx++;
360 }
361 }
362
363 if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
364 return;
365
366 usb_anchor_urb(urb, &data->bulk_anchor);
Oliver Neukum652fd782009-12-16 19:23:43 +0100367 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200368
369 err = usb_submit_urb(urb, GFP_ATOMIC);
370 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200371 /* -EPERM: urb is being killed;
372 * -ENODEV: device got disconnected */
373 if (err != -EPERM && err != -ENODEV)
Stefan Seyfried61faddf2010-11-30 21:49:08 +0100374 BT_ERR("%s urb %p failed to resubmit (%d)",
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200375 hdev->name, urb, -err);
376 usb_unanchor_urb(urb);
377 }
378}
379
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100380static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200381{
David Herrmann155961e2012-02-09 21:58:32 +0100382 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200383 struct urb *urb;
384 unsigned char *buf;
385 unsigned int pipe;
Vikram Kandukuri290ba202009-07-02 14:31:59 +0530386 int err, size = HCI_MAX_FRAME_SIZE;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200387
388 BT_DBG("%s", hdev->name);
389
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200390 if (!data->bulk_rx_ep)
391 return -ENODEV;
392
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100393 urb = usb_alloc_urb(0, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200394 if (!urb)
395 return -ENOMEM;
396
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100397 buf = kmalloc(size, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200398 if (!buf) {
399 usb_free_urb(urb);
400 return -ENOMEM;
401 }
402
403 pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
404
405 usb_fill_bulk_urb(urb, data->udev, pipe,
406 buf, size, btusb_bulk_complete, hdev);
407
408 urb->transfer_flags |= URB_FREE_BUFFER;
409
Oliver Neukum7bee5492009-08-24 23:44:59 +0200410 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200411 usb_anchor_urb(urb, &data->bulk_anchor);
412
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100413 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200414 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +0200415 if (err != -EPERM && err != -ENODEV)
416 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200417 hdev->name, urb, -err);
418 usb_unanchor_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200419 }
420
421 usb_free_urb(urb);
422
423 return err;
424}
425
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200426static void btusb_isoc_complete(struct urb *urb)
427{
428 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +0100429 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200430 int i, err;
431
432 BT_DBG("%s urb %p status %d count %d", hdev->name,
433 urb, urb->status, urb->actual_length);
434
435 if (!test_bit(HCI_RUNNING, &hdev->flags))
436 return;
437
438 if (urb->status == 0) {
439 for (i = 0; i < urb->number_of_packets; i++) {
440 unsigned int offset = urb->iso_frame_desc[i].offset;
441 unsigned int length = urb->iso_frame_desc[i].actual_length;
442
443 if (urb->iso_frame_desc[i].status)
444 continue;
445
446 hdev->stat.byte_rx += length;
447
448 if (hci_recv_fragment(hdev, HCI_SCODATA_PKT,
449 urb->transfer_buffer + offset,
450 length) < 0) {
451 BT_ERR("%s corrupted SCO packet", hdev->name);
452 hdev->stat.err_rx++;
453 }
454 }
455 }
456
457 if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags))
458 return;
459
460 usb_anchor_urb(urb, &data->isoc_anchor);
461
462 err = usb_submit_urb(urb, GFP_ATOMIC);
463 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200464 /* -EPERM: urb is being killed;
465 * -ENODEV: device got disconnected */
466 if (err != -EPERM && err != -ENODEV)
Stefan Seyfried61faddf2010-11-30 21:49:08 +0100467 BT_ERR("%s urb %p failed to resubmit (%d)",
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200468 hdev->name, urb, -err);
469 usb_unanchor_urb(urb);
470 }
471}
472
Jesper Juhl42b16b32011-01-17 00:09:38 +0100473static inline void __fill_isoc_descriptor(struct urb *urb, int len, int mtu)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200474{
475 int i, offset = 0;
476
477 BT_DBG("len %d mtu %d", len, mtu);
478
479 for (i = 0; i < BTUSB_MAX_ISOC_FRAMES && len >= mtu;
480 i++, offset += mtu, len -= mtu) {
481 urb->iso_frame_desc[i].offset = offset;
482 urb->iso_frame_desc[i].length = mtu;
483 }
484
485 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
486 urb->iso_frame_desc[i].offset = offset;
487 urb->iso_frame_desc[i].length = len;
488 i++;
489 }
490
491 urb->number_of_packets = i;
492}
493
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100494static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200495{
David Herrmann155961e2012-02-09 21:58:32 +0100496 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200497 struct urb *urb;
498 unsigned char *buf;
499 unsigned int pipe;
500 int err, size;
501
502 BT_DBG("%s", hdev->name);
503
504 if (!data->isoc_rx_ep)
505 return -ENODEV;
506
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100507 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200508 if (!urb)
509 return -ENOMEM;
510
511 size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) *
512 BTUSB_MAX_ISOC_FRAMES;
513
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100514 buf = kmalloc(size, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200515 if (!buf) {
516 usb_free_urb(urb);
517 return -ENOMEM;
518 }
519
520 pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress);
521
Bing Zhaofa0fb932011-12-20 18:19:00 -0800522 usb_fill_int_urb(urb, data->udev, pipe, buf, size, btusb_isoc_complete,
523 hdev, data->isoc_rx_ep->bInterval);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200524
525 urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200526
527 __fill_isoc_descriptor(urb, size,
528 le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize));
529
530 usb_anchor_urb(urb, &data->isoc_anchor);
531
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100532 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200533 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +0200534 if (err != -EPERM && err != -ENODEV)
535 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200536 hdev->name, urb, -err);
537 usb_unanchor_urb(urb);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200538 }
539
540 usb_free_urb(urb);
541
542 return err;
543}
544
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200545static void btusb_tx_complete(struct urb *urb)
546{
547 struct sk_buff *skb = urb->context;
548 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
David Herrmann155961e2012-02-09 21:58:32 +0100549 struct btusb_data *data = hci_get_drvdata(hdev);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200550
551 BT_DBG("%s urb %p status %d count %d", hdev->name,
552 urb, urb->status, urb->actual_length);
553
554 if (!test_bit(HCI_RUNNING, &hdev->flags))
555 goto done;
556
557 if (!urb->status)
558 hdev->stat.byte_tx += urb->transfer_buffer_length;
559 else
560 hdev->stat.err_tx++;
561
562done:
563 spin_lock(&data->txlock);
564 data->tx_in_flight--;
565 spin_unlock(&data->txlock);
566
567 kfree(urb->setup_packet);
568
569 kfree_skb(skb);
570}
571
572static void btusb_isoc_tx_complete(struct urb *urb)
573{
574 struct sk_buff *skb = urb->context;
575 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200576
577 BT_DBG("%s urb %p status %d count %d", hdev->name,
578 urb, urb->status, urb->actual_length);
579
580 if (!test_bit(HCI_RUNNING, &hdev->flags))
581 goto done;
582
583 if (!urb->status)
584 hdev->stat.byte_tx += urb->transfer_buffer_length;
585 else
586 hdev->stat.err_tx++;
587
588done:
589 kfree(urb->setup_packet);
590
591 kfree_skb(skb);
592}
593
594static int btusb_open(struct hci_dev *hdev)
595{
David Herrmann155961e2012-02-09 21:58:32 +0100596 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200597 int err;
598
599 BT_DBG("%s", hdev->name);
600
Oliver Neukum7bee5492009-08-24 23:44:59 +0200601 err = usb_autopm_get_interface(data->intf);
602 if (err < 0)
603 return err;
604
605 data->intf->needs_remote_wakeup = 1;
606
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200607 if (test_and_set_bit(HCI_RUNNING, &hdev->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +0200608 goto done;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200609
610 if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +0200611 goto done;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200612
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100613 err = btusb_submit_intr_urb(hdev, GFP_KERNEL);
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100614 if (err < 0)
615 goto failed;
616
617 err = btusb_submit_bulk_urb(hdev, GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200618 if (err < 0) {
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100619 usb_kill_anchored_urbs(&data->intr_anchor);
620 goto failed;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200621 }
622
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100623 set_bit(BTUSB_BULK_RUNNING, &data->flags);
624 btusb_submit_bulk_urb(hdev, GFP_KERNEL);
625
Oliver Neukum7bee5492009-08-24 23:44:59 +0200626done:
627 usb_autopm_put_interface(data->intf);
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100628 return 0;
629
630failed:
631 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
632 clear_bit(HCI_RUNNING, &hdev->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200633 usb_autopm_put_interface(data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200634 return err;
635}
636
Oliver Neukum7bee5492009-08-24 23:44:59 +0200637static void btusb_stop_traffic(struct btusb_data *data)
638{
639 usb_kill_anchored_urbs(&data->intr_anchor);
640 usb_kill_anchored_urbs(&data->bulk_anchor);
641 usb_kill_anchored_urbs(&data->isoc_anchor);
642}
643
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200644static int btusb_close(struct hci_dev *hdev)
645{
David Herrmann155961e2012-02-09 21:58:32 +0100646 struct btusb_data *data = hci_get_drvdata(hdev);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200647 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200648
649 BT_DBG("%s", hdev->name);
650
651 if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
652 return 0;
653
Marcel Holtmanne8c3c3d2008-09-23 00:16:36 +0200654 cancel_work_sync(&data->work);
Linus Torvalds404291a2009-11-11 13:32:29 -0800655 cancel_work_sync(&data->waker);
Marcel Holtmanne8c3c3d2008-09-23 00:16:36 +0200656
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200657 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200658 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200659 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200660
661 btusb_stop_traffic(data);
662 err = usb_autopm_get_interface(data->intf);
663 if (err < 0)
Oliver Neukum7b8e2c12009-11-13 14:26:23 +0100664 goto failed;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200665
666 data->intf->needs_remote_wakeup = 0;
667 usb_autopm_put_interface(data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200668
Oliver Neukum7b8e2c12009-11-13 14:26:23 +0100669failed:
670 usb_scuttle_anchored_urbs(&data->deferred);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200671 return 0;
672}
673
674static int btusb_flush(struct hci_dev *hdev)
675{
David Herrmann155961e2012-02-09 21:58:32 +0100676 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200677
678 BT_DBG("%s", hdev->name);
679
680 usb_kill_anchored_urbs(&data->tx_anchor);
681
682 return 0;
683}
684
685static int btusb_send_frame(struct sk_buff *skb)
686{
687 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
David Herrmann155961e2012-02-09 21:58:32 +0100688 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200689 struct usb_ctrlrequest *dr;
690 struct urb *urb;
691 unsigned int pipe;
692 int err;
693
694 BT_DBG("%s", hdev->name);
695
696 if (!test_bit(HCI_RUNNING, &hdev->flags))
697 return -EBUSY;
698
699 switch (bt_cb(skb)->pkt_type) {
700 case HCI_COMMAND_PKT:
701 urb = usb_alloc_urb(0, GFP_ATOMIC);
702 if (!urb)
703 return -ENOMEM;
704
705 dr = kmalloc(sizeof(*dr), GFP_ATOMIC);
706 if (!dr) {
707 usb_free_urb(urb);
708 return -ENOMEM;
709 }
710
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100711 dr->bRequestType = data->cmdreq_type;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200712 dr->bRequest = 0;
713 dr->wIndex = 0;
714 dr->wValue = 0;
715 dr->wLength = __cpu_to_le16(skb->len);
716
717 pipe = usb_sndctrlpipe(data->udev, 0x00);
718
719 usb_fill_control_urb(urb, data->udev, pipe, (void *) dr,
720 skb->data, skb->len, btusb_tx_complete, skb);
721
722 hdev->stat.cmd_tx++;
723 break;
724
725 case HCI_ACLDATA_PKT:
Peter Hurley9fd481e2011-07-14 08:48:32 -0400726 if (!data->bulk_tx_ep)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200727 return -ENODEV;
728
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200729 urb = usb_alloc_urb(0, GFP_ATOMIC);
730 if (!urb)
731 return -ENOMEM;
732
733 pipe = usb_sndbulkpipe(data->udev,
734 data->bulk_tx_ep->bEndpointAddress);
735
736 usb_fill_bulk_urb(urb, data->udev, pipe,
737 skb->data, skb->len, btusb_tx_complete, skb);
738
739 hdev->stat.acl_tx++;
740 break;
741
742 case HCI_SCODATA_PKT:
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200743 if (!data->isoc_tx_ep || hdev->conn_hash.sco_num < 1)
744 return -ENODEV;
745
746 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_ATOMIC);
747 if (!urb)
748 return -ENOMEM;
749
750 pipe = usb_sndisocpipe(data->udev,
751 data->isoc_tx_ep->bEndpointAddress);
752
Gustavo F. Padovan03c2d0e2011-02-14 18:53:43 -0300753 usb_fill_int_urb(urb, data->udev, pipe,
754 skb->data, skb->len, btusb_isoc_tx_complete,
755 skb, data->isoc_tx_ep->bInterval);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200756
757 urb->transfer_flags = URB_ISO_ASAP;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200758
759 __fill_isoc_descriptor(urb, skb->len,
760 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
761
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200762 hdev->stat.sco_tx++;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200763 goto skip_waking;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200764
765 default:
766 return -EILSEQ;
767 }
768
Oliver Neukum7bee5492009-08-24 23:44:59 +0200769 err = inc_tx(data);
770 if (err) {
771 usb_anchor_urb(urb, &data->deferred);
772 schedule_work(&data->waker);
773 err = 0;
774 goto done;
775 }
776
777skip_waking:
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200778 usb_anchor_urb(urb, &data->tx_anchor);
779
780 err = usb_submit_urb(urb, GFP_ATOMIC);
781 if (err < 0) {
Paul Bolle5a9b80e2011-10-09 12:12:16 +0200782 if (err != -EPERM && err != -ENODEV)
783 BT_ERR("%s urb %p submission failed (%d)",
784 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200785 kfree(urb->setup_packet);
786 usb_unanchor_urb(urb);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200787 } else {
788 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200789 }
790
Oliver Neukum7bee5492009-08-24 23:44:59 +0200791done:
Cong Wang54a8a792011-11-22 09:32:57 +0800792 usb_free_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200793 return err;
794}
795
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200796static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
797{
David Herrmann155961e2012-02-09 21:58:32 +0100798 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200799
800 BT_DBG("%s evt %d", hdev->name, evt);
801
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100802 if (hdev->conn_hash.sco_num != data->sco_num) {
803 data->sco_num = hdev->conn_hash.sco_num;
804 schedule_work(&data->work);
Marcel Holtmanna780efa2008-11-30 12:17:12 +0100805 }
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200806}
807
Jesper Juhl42b16b32011-01-17 00:09:38 +0100808static inline int __set_isoc_interface(struct hci_dev *hdev, int altsetting)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200809{
David Herrmann155961e2012-02-09 21:58:32 +0100810 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200811 struct usb_interface *intf = data->isoc;
812 struct usb_endpoint_descriptor *ep_desc;
813 int i, err;
814
815 if (!data->isoc)
816 return -ENODEV;
817
818 err = usb_set_interface(data->udev, 1, altsetting);
819 if (err < 0) {
820 BT_ERR("%s setting interface failed (%d)", hdev->name, -err);
821 return err;
822 }
823
824 data->isoc_altsetting = altsetting;
825
826 data->isoc_tx_ep = NULL;
827 data->isoc_rx_ep = NULL;
828
829 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
830 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
831
832 if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) {
833 data->isoc_tx_ep = ep_desc;
834 continue;
835 }
836
837 if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) {
838 data->isoc_rx_ep = ep_desc;
839 continue;
840 }
841 }
842
843 if (!data->isoc_tx_ep || !data->isoc_rx_ep) {
844 BT_ERR("%s invalid SCO descriptors", hdev->name);
845 return -ENODEV;
846 }
847
848 return 0;
849}
850
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200851static void btusb_work(struct work_struct *work)
852{
853 struct btusb_data *data = container_of(work, struct btusb_data, work);
854 struct hci_dev *hdev = data->hdev;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200855 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200856
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200857 if (hdev->conn_hash.sco_num > 0) {
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -0300858 if (!test_bit(BTUSB_DID_ISO_RESUME, &data->flags)) {
Oliver Neukum8efdd0c2011-02-11 13:00:06 +0100859 err = usb_autopm_get_interface(data->isoc ? data->isoc : data->intf);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200860 if (err < 0) {
861 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
862 usb_kill_anchored_urbs(&data->isoc_anchor);
863 return;
864 }
865
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -0300866 set_bit(BTUSB_DID_ISO_RESUME, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200867 }
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200868 if (data->isoc_altsetting != 2) {
869 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
870 usb_kill_anchored_urbs(&data->isoc_anchor);
871
872 if (__set_isoc_interface(hdev, 2) < 0)
873 return;
874 }
875
876 if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100877 if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200878 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
879 else
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100880 btusb_submit_isoc_urb(hdev, GFP_KERNEL);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200881 }
882 } else {
883 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
884 usb_kill_anchored_urbs(&data->isoc_anchor);
885
886 __set_isoc_interface(hdev, 0);
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -0300887 if (test_and_clear_bit(BTUSB_DID_ISO_RESUME, &data->flags))
Oliver Neukum8efdd0c2011-02-11 13:00:06 +0100888 usb_autopm_put_interface(data->isoc ? data->isoc : data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200889 }
890}
891
Oliver Neukum7bee5492009-08-24 23:44:59 +0200892static void btusb_waker(struct work_struct *work)
893{
894 struct btusb_data *data = container_of(work, struct btusb_data, waker);
895 int err;
896
897 err = usb_autopm_get_interface(data->intf);
898 if (err < 0)
899 return;
900
901 usb_autopm_put_interface(data->intf);
902}
903
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200904static int btusb_probe(struct usb_interface *intf,
905 const struct usb_device_id *id)
906{
907 struct usb_endpoint_descriptor *ep_desc;
908 struct btusb_data *data;
909 struct hci_dev *hdev;
910 int i, err;
911
912 BT_DBG("intf %p id %p", intf, id);
913
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200914 /* interface numbers are hardcoded in the spec */
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200915 if (intf->cur_altsetting->desc.bInterfaceNumber != 0)
916 return -ENODEV;
917
918 if (!id->driver_info) {
919 const struct usb_device_id *match;
920 match = usb_match_id(intf, blacklist_table);
921 if (match)
922 id = match;
923 }
924
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200925 if (id->driver_info == BTUSB_IGNORE)
926 return -ENODEV;
927
928 if (ignore_dga && id->driver_info & BTUSB_DIGIANSWER)
929 return -ENODEV;
930
931 if (ignore_csr && id->driver_info & BTUSB_CSR)
932 return -ENODEV;
933
934 if (ignore_sniffer && id->driver_info & BTUSB_SNIFFER)
935 return -ENODEV;
936
Steven.Li2d25f8b2011-07-01 14:02:36 +0800937 if (id->driver_info & BTUSB_ATH3012) {
938 struct usb_device *udev = interface_to_usbdev(intf);
939
940 /* Old firmware would otherwise let ath3k driver load
941 * patch and sysconfig files */
942 if (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x0001)
943 return -ENODEV;
944 }
945
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200946 data = kzalloc(sizeof(*data), GFP_KERNEL);
947 if (!data)
948 return -ENOMEM;
949
950 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
951 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
952
953 if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
954 data->intr_ep = ep_desc;
955 continue;
956 }
957
958 if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
959 data->bulk_tx_ep = ep_desc;
960 continue;
961 }
962
963 if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
964 data->bulk_rx_ep = ep_desc;
965 continue;
966 }
967 }
968
969 if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep) {
970 kfree(data);
971 return -ENODEV;
972 }
973
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100974 data->cmdreq_type = USB_TYPE_CLASS;
975
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200976 data->udev = interface_to_usbdev(intf);
Marcel Holtmann5fbcd262008-09-23 00:16:36 +0200977 data->intf = intf;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200978
979 spin_lock_init(&data->lock);
980
981 INIT_WORK(&data->work, btusb_work);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200982 INIT_WORK(&data->waker, btusb_waker);
983 spin_lock_init(&data->txlock);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200984
985 init_usb_anchor(&data->tx_anchor);
986 init_usb_anchor(&data->intr_anchor);
987 init_usb_anchor(&data->bulk_anchor);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200988 init_usb_anchor(&data->isoc_anchor);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200989 init_usb_anchor(&data->deferred);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200990
991 hdev = hci_alloc_dev();
992 if (!hdev) {
993 kfree(data);
994 return -ENOMEM;
995 }
996
Marcel Holtmannc13854c2010-02-08 15:27:07 +0100997 hdev->bus = HCI_USB;
David Herrmann155961e2012-02-09 21:58:32 +0100998 hci_set_drvdata(hdev, data);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200999
1000 data->hdev = hdev;
1001
1002 SET_HCIDEV_DEV(hdev, &intf->dev);
1003
1004 hdev->open = btusb_open;
1005 hdev->close = btusb_close;
1006 hdev->flush = btusb_flush;
1007 hdev->send = btusb_send_frame;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001008 hdev->notify = btusb_notify;
1009
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01001010 /* Interface numbers are hardcoded in the specification */
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001011 data->isoc = usb_ifnum_to_if(data->udev, 1);
1012
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01001013 if (!reset)
1014 set_bit(HCI_QUIRK_NO_RESET, &hdev->quirks);
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02001015
1016 if (force_scofix || id->driver_info & BTUSB_WRONG_SCO_MTU) {
1017 if (!disable_scofix)
1018 set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
1019 }
1020
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001021 if (id->driver_info & BTUSB_BROKEN_ISOC)
1022 data->isoc = NULL;
1023
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01001024 if (id->driver_info & BTUSB_DIGIANSWER) {
1025 data->cmdreq_type = USB_TYPE_VENDOR;
1026 set_bit(HCI_QUIRK_NO_RESET, &hdev->quirks);
1027 }
1028
1029 if (id->driver_info & BTUSB_CSR) {
1030 struct usb_device *udev = data->udev;
1031
1032 /* Old firmware would otherwise execute USB reset */
1033 if (le16_to_cpu(udev->descriptor.bcdDevice) < 0x117)
1034 set_bit(HCI_QUIRK_NO_RESET, &hdev->quirks);
1035 }
1036
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02001037 if (id->driver_info & BTUSB_SNIFFER) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001038 struct usb_device *udev = data->udev;
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02001039
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01001040 /* New sniffer firmware has crippled HCI interface */
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02001041 if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
1042 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001043
1044 data->isoc = NULL;
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02001045 }
1046
1047 if (id->driver_info & BTUSB_BCM92035) {
1048 unsigned char cmd[] = { 0x3b, 0xfc, 0x01, 0x00 };
1049 struct sk_buff *skb;
1050
1051 skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL);
1052 if (skb) {
1053 memcpy(skb_put(skb, sizeof(cmd)), cmd, sizeof(cmd));
1054 skb_queue_tail(&hdev->driver_init, skb);
1055 }
1056 }
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001057
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001058 if (data->isoc) {
1059 err = usb_driver_claim_interface(&btusb_driver,
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02001060 data->isoc, data);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001061 if (err < 0) {
1062 hci_free_dev(hdev);
1063 kfree(data);
1064 return err;
1065 }
1066 }
1067
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001068 err = hci_register_dev(hdev);
1069 if (err < 0) {
1070 hci_free_dev(hdev);
1071 kfree(data);
1072 return err;
1073 }
1074
1075 usb_set_intfdata(intf, data);
1076
1077 return 0;
1078}
1079
1080static void btusb_disconnect(struct usb_interface *intf)
1081{
1082 struct btusb_data *data = usb_get_intfdata(intf);
1083 struct hci_dev *hdev;
1084
1085 BT_DBG("intf %p", intf);
1086
1087 if (!data)
1088 return;
1089
1090 hdev = data->hdev;
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02001091 usb_set_intfdata(data->intf, NULL);
1092
1093 if (data->isoc)
1094 usb_set_intfdata(data->isoc, NULL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001095
1096 hci_unregister_dev(hdev);
1097
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02001098 if (intf == data->isoc)
1099 usb_driver_release_interface(&btusb_driver, data->intf);
1100 else if (data->isoc)
1101 usb_driver_release_interface(&btusb_driver, data->isoc);
1102
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001103 hci_free_dev(hdev);
David Herrmann83810882012-01-07 15:47:16 +01001104 kfree(data);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001105}
1106
Oliver Neukum7bee5492009-08-24 23:44:59 +02001107#ifdef CONFIG_PM
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001108static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
1109{
1110 struct btusb_data *data = usb_get_intfdata(intf);
1111
1112 BT_DBG("intf %p", intf);
1113
1114 if (data->suspend_count++)
1115 return 0;
1116
Oliver Neukum7bee5492009-08-24 23:44:59 +02001117 spin_lock_irq(&data->txlock);
Alan Stern5b1b0b82011-08-19 23:49:48 +02001118 if (!(PMSG_IS_AUTO(message) && data->tx_in_flight)) {
Oliver Neukum7bee5492009-08-24 23:44:59 +02001119 set_bit(BTUSB_SUSPENDING, &data->flags);
1120 spin_unlock_irq(&data->txlock);
1121 } else {
1122 spin_unlock_irq(&data->txlock);
1123 data->suspend_count--;
1124 return -EBUSY;
1125 }
1126
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001127 cancel_work_sync(&data->work);
1128
Oliver Neukum7bee5492009-08-24 23:44:59 +02001129 btusb_stop_traffic(data);
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001130 usb_kill_anchored_urbs(&data->tx_anchor);
1131
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001132 return 0;
1133}
1134
Oliver Neukum7bee5492009-08-24 23:44:59 +02001135static void play_deferred(struct btusb_data *data)
1136{
1137 struct urb *urb;
1138 int err;
1139
1140 while ((urb = usb_get_from_anchor(&data->deferred))) {
1141 err = usb_submit_urb(urb, GFP_ATOMIC);
1142 if (err < 0)
1143 break;
1144
1145 data->tx_in_flight++;
1146 }
1147 usb_scuttle_anchored_urbs(&data->deferred);
1148}
1149
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001150static int btusb_resume(struct usb_interface *intf)
1151{
1152 struct btusb_data *data = usb_get_intfdata(intf);
1153 struct hci_dev *hdev = data->hdev;
Oliver Neukum7bee5492009-08-24 23:44:59 +02001154 int err = 0;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001155
1156 BT_DBG("intf %p", intf);
1157
1158 if (--data->suspend_count)
1159 return 0;
1160
1161 if (!test_bit(HCI_RUNNING, &hdev->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +02001162 goto done;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001163
1164 if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
1165 err = btusb_submit_intr_urb(hdev, GFP_NOIO);
1166 if (err < 0) {
1167 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001168 goto failed;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001169 }
1170 }
1171
1172 if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) {
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001173 err = btusb_submit_bulk_urb(hdev, GFP_NOIO);
1174 if (err < 0) {
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001175 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001176 goto failed;
1177 }
1178
1179 btusb_submit_bulk_urb(hdev, GFP_NOIO);
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001180 }
1181
1182 if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
1183 if (btusb_submit_isoc_urb(hdev, GFP_NOIO) < 0)
1184 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1185 else
1186 btusb_submit_isoc_urb(hdev, GFP_NOIO);
1187 }
1188
Oliver Neukum7bee5492009-08-24 23:44:59 +02001189 spin_lock_irq(&data->txlock);
1190 play_deferred(data);
1191 clear_bit(BTUSB_SUSPENDING, &data->flags);
1192 spin_unlock_irq(&data->txlock);
1193 schedule_work(&data->work);
1194
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001195 return 0;
Oliver Neukum7bee5492009-08-24 23:44:59 +02001196
1197failed:
1198 usb_scuttle_anchored_urbs(&data->deferred);
1199done:
1200 spin_lock_irq(&data->txlock);
1201 clear_bit(BTUSB_SUSPENDING, &data->flags);
1202 spin_unlock_irq(&data->txlock);
1203
1204 return err;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001205}
Oliver Neukum7bee5492009-08-24 23:44:59 +02001206#endif
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001207
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001208static struct usb_driver btusb_driver = {
1209 .name = "btusb",
1210 .probe = btusb_probe,
1211 .disconnect = btusb_disconnect,
Oliver Neukum7bee5492009-08-24 23:44:59 +02001212#ifdef CONFIG_PM
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001213 .suspend = btusb_suspend,
1214 .resume = btusb_resume,
Oliver Neukum7bee5492009-08-24 23:44:59 +02001215#endif
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001216 .id_table = btusb_table,
Oliver Neukum7bee5492009-08-24 23:44:59 +02001217 .supports_autosuspend = 1,
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001218};
1219
Greg Kroah-Hartman93f15082011-11-18 09:47:34 -08001220module_usb_driver(btusb_driver);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001221
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02001222module_param(ignore_dga, bool, 0644);
1223MODULE_PARM_DESC(ignore_dga, "Ignore devices with id 08fd:0001");
1224
1225module_param(ignore_csr, bool, 0644);
1226MODULE_PARM_DESC(ignore_csr, "Ignore devices with id 0a12:0001");
1227
1228module_param(ignore_sniffer, bool, 0644);
1229MODULE_PARM_DESC(ignore_sniffer, "Ignore devices with id 0a12:0002");
1230
1231module_param(disable_scofix, bool, 0644);
1232MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
1233
1234module_param(force_scofix, bool, 0644);
1235MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
1236
1237module_param(reset, bool, 0644);
1238MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
1239
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001240MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
1241MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
1242MODULE_VERSION(VERSION);
1243MODULE_LICENSE("GPL");