blob: 2af2e770f4bde1127c47c4fbb90c67e589baa7f8 [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
40static int ignore_dga;
41static int ignore_csr;
42static int ignore_sniffer;
43static int disable_scofix;
44static int force_scofix;
Marcel Holtmann7a9d4022008-11-30 12:17:26 +010045
46static int 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
Marcel Holtmann5e23b922007-10-20 14:12:34 +020057
58static struct usb_device_id btusb_table[] = {
59 /* Generic Bluetooth USB device */
60 { USB_DEVICE_INFO(0xe0, 0x01, 0x01) },
61
Nobuhiro Iwamatsu3cd01972010-08-20 16:24:07 +090062 /* Apple MacBookPro 7,1 */
63 { USB_DEVICE(0x05ac, 0x8213) },
64
Cyril Lacoux0a79f672010-07-14 10:29:27 +040065 /* Apple iMac11,1 */
66 { USB_DEVICE(0x05ac, 0x8215) },
67
Nobuhiro Iwamatsu9c047152010-08-20 16:24:06 +090068 /* Apple MacBookPro6,2 */
69 { USB_DEVICE(0x05ac, 0x8218) },
70
Edgar (gimli) Hucek3e3ede72010-11-04 08:04:33 +010071 /* Apple MacBookAir3,1, MacBookAir3,2 */
72 { USB_DEVICE(0x05ac, 0x821b) },
73
Marc-Antoine Perennou88d377b2011-03-24 14:51:21 -030074 /* Apple MacBookPro8,2 */
75 { USB_DEVICE(0x05ac, 0x821a) },
76
Marcel Holtmanncfeb4142008-08-07 22:26:56 +020077 /* AVM BlueFRITZ! USB v2.0 */
78 { USB_DEVICE(0x057c, 0x3800) },
79
80 /* Bluetooth Ultraport Module from IBM */
81 { USB_DEVICE(0x04bf, 0x030a) },
82
83 /* ALPS Modules with non-standard id */
84 { USB_DEVICE(0x044e, 0x3001) },
85 { USB_DEVICE(0x044e, 0x3002) },
86
87 /* Ericsson with non-standard id */
88 { USB_DEVICE(0x0bdb, 0x1002) },
89
90 /* Canyon CN-BTU1 with HID interfaces */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +010091 { USB_DEVICE(0x0c10, 0x0000) },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +020092
Marcel Holtmann5e23b922007-10-20 14:12:34 +020093 { } /* Terminating entry */
94};
95
96MODULE_DEVICE_TABLE(usb, btusb_table);
97
98static struct usb_device_id blacklist_table[] = {
Marcel Holtmanncfeb4142008-08-07 22:26:56 +020099 /* CSR BlueCore devices */
100 { USB_DEVICE(0x0a12, 0x0001), .driver_info = BTUSB_CSR },
101
102 /* Broadcom BCM2033 without firmware */
103 { USB_DEVICE(0x0a5c, 0x2033), .driver_info = BTUSB_IGNORE },
104
Bala Shanmugambe931122010-11-26 17:35:46 +0530105 /* Atheros 3011 with sflash firmware */
106 { USB_DEVICE(0x0cf3, 0x3002), .driver_info = BTUSB_IGNORE },
107
Cho, Yu-Chen509e7862011-01-26 17:10:59 +0800108 /* Atheros AR9285 Malbec with sflash firmware */
109 { USB_DEVICE(0x03f0, 0x311d), .driver_info = BTUSB_IGNORE },
110
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530111 /* Atheros 3012 with sflash firmware */
112 { USB_DEVICE(0x0cf3, 0x3004), .driver_info = BTUSB_IGNORE },
113
Cho, Yu-Chene9036e32011-02-15 10:20:07 +0800114 /* Atheros AR5BBU12 with sflash firmware */
115 { USB_DEVICE(0x0489, 0xe02c), .driver_info = BTUSB_IGNORE },
116
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200117 /* Broadcom BCM2035 */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100118 { USB_DEVICE(0x0a5c, 0x2035), .driver_info = BTUSB_WRONG_SCO_MTU },
119 { USB_DEVICE(0x0a5c, 0x200a), .driver_info = BTUSB_WRONG_SCO_MTU },
120 { USB_DEVICE(0x0a5c, 0x2009), .driver_info = BTUSB_BCM92035 },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200121
122 /* Broadcom BCM2045 */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100123 { USB_DEVICE(0x0a5c, 0x2039), .driver_info = BTUSB_WRONG_SCO_MTU },
124 { USB_DEVICE(0x0a5c, 0x2101), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmannbdbef3d2008-09-23 00:16:35 +0200125
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200126 /* IBM/Lenovo ThinkPad with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100127 { USB_DEVICE(0x0a5c, 0x201e), .driver_info = BTUSB_WRONG_SCO_MTU },
128 { USB_DEVICE(0x0a5c, 0x2110), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200129
130 /* HP laptop with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100131 { USB_DEVICE(0x03f0, 0x171d), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200132
133 /* Dell laptop with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100134 { USB_DEVICE(0x413c, 0x8126), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200135
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100136 /* Dell Wireless 370 and 410 devices */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100137 { USB_DEVICE(0x413c, 0x8152), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100138 { USB_DEVICE(0x413c, 0x8156), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200139
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100140 /* Belkin F8T012 and F8T013 devices */
141 { USB_DEVICE(0x050d, 0x0012), .driver_info = BTUSB_WRONG_SCO_MTU },
142 { USB_DEVICE(0x050d, 0x0013), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200143
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100144 /* Asus WL-BTD202 device */
145 { USB_DEVICE(0x0b05, 0x1715), .driver_info = BTUSB_WRONG_SCO_MTU },
146
147 /* Kensington Bluetooth USB adapter */
148 { USB_DEVICE(0x047d, 0x105e), .driver_info = BTUSB_WRONG_SCO_MTU },
149
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200150 /* RTX Telecom based adapters with buggy SCO support */
151 { USB_DEVICE(0x0400, 0x0807), .driver_info = BTUSB_BROKEN_ISOC },
152 { USB_DEVICE(0x0400, 0x080a), .driver_info = BTUSB_BROKEN_ISOC },
153
154 /* CONWISE Technology based adapters with buggy SCO support */
155 { USB_DEVICE(0x0e5e, 0x6622), .driver_info = BTUSB_BROKEN_ISOC },
156
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200157 /* Digianswer devices */
158 { USB_DEVICE(0x08fd, 0x0001), .driver_info = BTUSB_DIGIANSWER },
159 { USB_DEVICE(0x08fd, 0x0002), .driver_info = BTUSB_IGNORE },
160
161 /* CSR BlueCore Bluetooth Sniffer */
162 { USB_DEVICE(0x0a12, 0x0002), .driver_info = BTUSB_SNIFFER },
163
164 /* Frontline ComProbe Bluetooth Sniffer */
165 { USB_DEVICE(0x16d3, 0x0002), .driver_info = BTUSB_SNIFFER },
166
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200167 { } /* Terminating entry */
168};
169
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200170#define BTUSB_MAX_ISOC_FRAMES 10
171
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200172#define BTUSB_INTR_RUNNING 0
173#define BTUSB_BULK_RUNNING 1
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200174#define BTUSB_ISOC_RUNNING 2
Oliver Neukum7bee5492009-08-24 23:44:59 +0200175#define BTUSB_SUSPENDING 3
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -0300176#define BTUSB_DID_ISO_RESUME 4
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200177
178struct btusb_data {
179 struct hci_dev *hdev;
180 struct usb_device *udev;
Marcel Holtmann5fbcd262008-09-23 00:16:36 +0200181 struct usb_interface *intf;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200182 struct usb_interface *isoc;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200183
184 spinlock_t lock;
185
186 unsigned long flags;
187
188 struct work_struct work;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200189 struct work_struct waker;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200190
191 struct usb_anchor tx_anchor;
192 struct usb_anchor intr_anchor;
193 struct usb_anchor bulk_anchor;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200194 struct usb_anchor isoc_anchor;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200195 struct usb_anchor deferred;
196 int tx_in_flight;
197 spinlock_t txlock;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200198
199 struct usb_endpoint_descriptor *intr_ep;
200 struct usb_endpoint_descriptor *bulk_tx_ep;
201 struct usb_endpoint_descriptor *bulk_rx_ep;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200202 struct usb_endpoint_descriptor *isoc_tx_ep;
203 struct usb_endpoint_descriptor *isoc_rx_ep;
204
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100205 __u8 cmdreq_type;
206
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100207 unsigned int sco_num;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200208 int isoc_altsetting;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +0100209 int suspend_count;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200210};
211
Oliver Neukum7bee5492009-08-24 23:44:59 +0200212static int inc_tx(struct btusb_data *data)
213{
214 unsigned long flags;
215 int rv;
216
217 spin_lock_irqsave(&data->txlock, flags);
218 rv = test_bit(BTUSB_SUSPENDING, &data->flags);
219 if (!rv)
220 data->tx_in_flight++;
221 spin_unlock_irqrestore(&data->txlock, flags);
222
223 return rv;
224}
225
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200226static void btusb_intr_complete(struct urb *urb)
227{
228 struct hci_dev *hdev = urb->context;
229 struct btusb_data *data = hdev->driver_data;
230 int err;
231
232 BT_DBG("%s urb %p status %d count %d", hdev->name,
233 urb, urb->status, urb->actual_length);
234
235 if (!test_bit(HCI_RUNNING, &hdev->flags))
236 return;
237
238 if (urb->status == 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200239 hdev->stat.byte_rx += urb->actual_length;
240
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200241 if (hci_recv_fragment(hdev, HCI_EVENT_PKT,
242 urb->transfer_buffer,
243 urb->actual_length) < 0) {
244 BT_ERR("%s corrupted event packet", hdev->name);
245 hdev->stat.err_rx++;
246 }
247 }
248
249 if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
250 return;
251
Oliver Neukum7bee5492009-08-24 23:44:59 +0200252 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200253 usb_anchor_urb(urb, &data->intr_anchor);
254
255 err = usb_submit_urb(urb, GFP_ATOMIC);
256 if (err < 0) {
Stefan Seyfried61faddf2010-11-30 21:49:08 +0100257 if (err != -EPERM)
258 BT_ERR("%s urb %p failed to resubmit (%d)",
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200259 hdev->name, urb, -err);
260 usb_unanchor_urb(urb);
261 }
262}
263
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100264static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200265{
266 struct btusb_data *data = hdev->driver_data;
267 struct urb *urb;
268 unsigned char *buf;
269 unsigned int pipe;
270 int err, size;
271
272 BT_DBG("%s", hdev->name);
273
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200274 if (!data->intr_ep)
275 return -ENODEV;
276
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100277 urb = usb_alloc_urb(0, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200278 if (!urb)
279 return -ENOMEM;
280
281 size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
282
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100283 buf = kmalloc(size, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200284 if (!buf) {
285 usb_free_urb(urb);
286 return -ENOMEM;
287 }
288
289 pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
290
291 usb_fill_int_urb(urb, data->udev, pipe, buf, size,
292 btusb_intr_complete, hdev,
293 data->intr_ep->bInterval);
294
295 urb->transfer_flags |= URB_FREE_BUFFER;
296
297 usb_anchor_urb(urb, &data->intr_anchor);
298
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100299 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200300 if (err < 0) {
301 BT_ERR("%s urb %p submission failed (%d)",
302 hdev->name, urb, -err);
303 usb_unanchor_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200304 }
305
306 usb_free_urb(urb);
307
308 return err;
309}
310
311static void btusb_bulk_complete(struct urb *urb)
312{
313 struct hci_dev *hdev = urb->context;
314 struct btusb_data *data = hdev->driver_data;
315 int err;
316
317 BT_DBG("%s urb %p status %d count %d", hdev->name,
318 urb, urb->status, urb->actual_length);
319
320 if (!test_bit(HCI_RUNNING, &hdev->flags))
321 return;
322
323 if (urb->status == 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200324 hdev->stat.byte_rx += urb->actual_length;
325
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200326 if (hci_recv_fragment(hdev, HCI_ACLDATA_PKT,
327 urb->transfer_buffer,
328 urb->actual_length) < 0) {
329 BT_ERR("%s corrupted ACL packet", hdev->name);
330 hdev->stat.err_rx++;
331 }
332 }
333
334 if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
335 return;
336
337 usb_anchor_urb(urb, &data->bulk_anchor);
Oliver Neukum652fd782009-12-16 19:23:43 +0100338 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200339
340 err = usb_submit_urb(urb, GFP_ATOMIC);
341 if (err < 0) {
Stefan Seyfried61faddf2010-11-30 21:49:08 +0100342 if (err != -EPERM)
343 BT_ERR("%s urb %p failed to resubmit (%d)",
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200344 hdev->name, urb, -err);
345 usb_unanchor_urb(urb);
346 }
347}
348
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100349static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200350{
351 struct btusb_data *data = hdev->driver_data;
352 struct urb *urb;
353 unsigned char *buf;
354 unsigned int pipe;
Vikram Kandukuri290ba202009-07-02 14:31:59 +0530355 int err, size = HCI_MAX_FRAME_SIZE;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200356
357 BT_DBG("%s", hdev->name);
358
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200359 if (!data->bulk_rx_ep)
360 return -ENODEV;
361
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100362 urb = usb_alloc_urb(0, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200363 if (!urb)
364 return -ENOMEM;
365
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100366 buf = kmalloc(size, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200367 if (!buf) {
368 usb_free_urb(urb);
369 return -ENOMEM;
370 }
371
372 pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
373
374 usb_fill_bulk_urb(urb, data->udev, pipe,
375 buf, size, btusb_bulk_complete, hdev);
376
377 urb->transfer_flags |= URB_FREE_BUFFER;
378
Oliver Neukum7bee5492009-08-24 23:44:59 +0200379 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200380 usb_anchor_urb(urb, &data->bulk_anchor);
381
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100382 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200383 if (err < 0) {
384 BT_ERR("%s urb %p submission failed (%d)",
385 hdev->name, urb, -err);
386 usb_unanchor_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200387 }
388
389 usb_free_urb(urb);
390
391 return err;
392}
393
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200394static void btusb_isoc_complete(struct urb *urb)
395{
396 struct hci_dev *hdev = urb->context;
397 struct btusb_data *data = hdev->driver_data;
398 int i, err;
399
400 BT_DBG("%s urb %p status %d count %d", hdev->name,
401 urb, urb->status, urb->actual_length);
402
403 if (!test_bit(HCI_RUNNING, &hdev->flags))
404 return;
405
406 if (urb->status == 0) {
407 for (i = 0; i < urb->number_of_packets; i++) {
408 unsigned int offset = urb->iso_frame_desc[i].offset;
409 unsigned int length = urb->iso_frame_desc[i].actual_length;
410
411 if (urb->iso_frame_desc[i].status)
412 continue;
413
414 hdev->stat.byte_rx += length;
415
416 if (hci_recv_fragment(hdev, HCI_SCODATA_PKT,
417 urb->transfer_buffer + offset,
418 length) < 0) {
419 BT_ERR("%s corrupted SCO packet", hdev->name);
420 hdev->stat.err_rx++;
421 }
422 }
423 }
424
425 if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags))
426 return;
427
428 usb_anchor_urb(urb, &data->isoc_anchor);
429
430 err = usb_submit_urb(urb, GFP_ATOMIC);
431 if (err < 0) {
Stefan Seyfried61faddf2010-11-30 21:49:08 +0100432 if (err != -EPERM)
433 BT_ERR("%s urb %p failed to resubmit (%d)",
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200434 hdev->name, urb, -err);
435 usb_unanchor_urb(urb);
436 }
437}
438
Jesper Juhl42b16b32011-01-17 00:09:38 +0100439static inline void __fill_isoc_descriptor(struct urb *urb, int len, int mtu)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200440{
441 int i, offset = 0;
442
443 BT_DBG("len %d mtu %d", len, mtu);
444
445 for (i = 0; i < BTUSB_MAX_ISOC_FRAMES && len >= mtu;
446 i++, offset += mtu, len -= mtu) {
447 urb->iso_frame_desc[i].offset = offset;
448 urb->iso_frame_desc[i].length = mtu;
449 }
450
451 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
452 urb->iso_frame_desc[i].offset = offset;
453 urb->iso_frame_desc[i].length = len;
454 i++;
455 }
456
457 urb->number_of_packets = i;
458}
459
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100460static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200461{
462 struct btusb_data *data = hdev->driver_data;
463 struct urb *urb;
464 unsigned char *buf;
465 unsigned int pipe;
466 int err, size;
467
468 BT_DBG("%s", hdev->name);
469
470 if (!data->isoc_rx_ep)
471 return -ENODEV;
472
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100473 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200474 if (!urb)
475 return -ENOMEM;
476
477 size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) *
478 BTUSB_MAX_ISOC_FRAMES;
479
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100480 buf = kmalloc(size, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200481 if (!buf) {
482 usb_free_urb(urb);
483 return -ENOMEM;
484 }
485
486 pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress);
487
488 urb->dev = data->udev;
489 urb->pipe = pipe;
490 urb->context = hdev;
491 urb->complete = btusb_isoc_complete;
492 urb->interval = data->isoc_rx_ep->bInterval;
493
494 urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP;
495 urb->transfer_buffer = buf;
496 urb->transfer_buffer_length = size;
497
498 __fill_isoc_descriptor(urb, size,
499 le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize));
500
501 usb_anchor_urb(urb, &data->isoc_anchor);
502
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100503 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200504 if (err < 0) {
505 BT_ERR("%s urb %p submission failed (%d)",
506 hdev->name, urb, -err);
507 usb_unanchor_urb(urb);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200508 }
509
510 usb_free_urb(urb);
511
512 return err;
513}
514
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200515static void btusb_tx_complete(struct urb *urb)
516{
517 struct sk_buff *skb = urb->context;
518 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200519 struct btusb_data *data = hdev->driver_data;
520
521 BT_DBG("%s urb %p status %d count %d", hdev->name,
522 urb, urb->status, urb->actual_length);
523
524 if (!test_bit(HCI_RUNNING, &hdev->flags))
525 goto done;
526
527 if (!urb->status)
528 hdev->stat.byte_tx += urb->transfer_buffer_length;
529 else
530 hdev->stat.err_tx++;
531
532done:
533 spin_lock(&data->txlock);
534 data->tx_in_flight--;
535 spin_unlock(&data->txlock);
536
537 kfree(urb->setup_packet);
538
539 kfree_skb(skb);
540}
541
542static void btusb_isoc_tx_complete(struct urb *urb)
543{
544 struct sk_buff *skb = urb->context;
545 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200546
547 BT_DBG("%s urb %p status %d count %d", hdev->name,
548 urb, urb->status, urb->actual_length);
549
550 if (!test_bit(HCI_RUNNING, &hdev->flags))
551 goto done;
552
553 if (!urb->status)
554 hdev->stat.byte_tx += urb->transfer_buffer_length;
555 else
556 hdev->stat.err_tx++;
557
558done:
559 kfree(urb->setup_packet);
560
561 kfree_skb(skb);
562}
563
564static int btusb_open(struct hci_dev *hdev)
565{
566 struct btusb_data *data = hdev->driver_data;
567 int err;
568
569 BT_DBG("%s", hdev->name);
570
Oliver Neukum7bee5492009-08-24 23:44:59 +0200571 err = usb_autopm_get_interface(data->intf);
572 if (err < 0)
573 return err;
574
575 data->intf->needs_remote_wakeup = 1;
576
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200577 if (test_and_set_bit(HCI_RUNNING, &hdev->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +0200578 goto done;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200579
580 if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +0200581 goto done;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200582
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100583 err = btusb_submit_intr_urb(hdev, GFP_KERNEL);
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100584 if (err < 0)
585 goto failed;
586
587 err = btusb_submit_bulk_urb(hdev, GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200588 if (err < 0) {
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100589 usb_kill_anchored_urbs(&data->intr_anchor);
590 goto failed;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200591 }
592
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100593 set_bit(BTUSB_BULK_RUNNING, &data->flags);
594 btusb_submit_bulk_urb(hdev, GFP_KERNEL);
595
Oliver Neukum7bee5492009-08-24 23:44:59 +0200596done:
597 usb_autopm_put_interface(data->intf);
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100598 return 0;
599
600failed:
601 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
602 clear_bit(HCI_RUNNING, &hdev->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200603 usb_autopm_put_interface(data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200604 return err;
605}
606
Oliver Neukum7bee5492009-08-24 23:44:59 +0200607static void btusb_stop_traffic(struct btusb_data *data)
608{
609 usb_kill_anchored_urbs(&data->intr_anchor);
610 usb_kill_anchored_urbs(&data->bulk_anchor);
611 usb_kill_anchored_urbs(&data->isoc_anchor);
612}
613
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200614static int btusb_close(struct hci_dev *hdev)
615{
616 struct btusb_data *data = hdev->driver_data;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200617 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200618
619 BT_DBG("%s", hdev->name);
620
621 if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
622 return 0;
623
Marcel Holtmanne8c3c3d2008-09-23 00:16:36 +0200624 cancel_work_sync(&data->work);
Linus Torvalds404291a2009-11-11 13:32:29 -0800625 cancel_work_sync(&data->waker);
Marcel Holtmanne8c3c3d2008-09-23 00:16:36 +0200626
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200627 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200628 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200629 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200630
631 btusb_stop_traffic(data);
632 err = usb_autopm_get_interface(data->intf);
633 if (err < 0)
Oliver Neukum7b8e2c12009-11-13 14:26:23 +0100634 goto failed;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200635
636 data->intf->needs_remote_wakeup = 0;
637 usb_autopm_put_interface(data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200638
Oliver Neukum7b8e2c12009-11-13 14:26:23 +0100639failed:
640 usb_scuttle_anchored_urbs(&data->deferred);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200641 return 0;
642}
643
644static int btusb_flush(struct hci_dev *hdev)
645{
646 struct btusb_data *data = hdev->driver_data;
647
648 BT_DBG("%s", hdev->name);
649
650 usb_kill_anchored_urbs(&data->tx_anchor);
651
652 return 0;
653}
654
655static int btusb_send_frame(struct sk_buff *skb)
656{
657 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
658 struct btusb_data *data = hdev->driver_data;
659 struct usb_ctrlrequest *dr;
660 struct urb *urb;
661 unsigned int pipe;
662 int err;
663
664 BT_DBG("%s", hdev->name);
665
666 if (!test_bit(HCI_RUNNING, &hdev->flags))
667 return -EBUSY;
668
669 switch (bt_cb(skb)->pkt_type) {
670 case HCI_COMMAND_PKT:
671 urb = usb_alloc_urb(0, GFP_ATOMIC);
672 if (!urb)
673 return -ENOMEM;
674
675 dr = kmalloc(sizeof(*dr), GFP_ATOMIC);
676 if (!dr) {
677 usb_free_urb(urb);
678 return -ENOMEM;
679 }
680
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100681 dr->bRequestType = data->cmdreq_type;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200682 dr->bRequest = 0;
683 dr->wIndex = 0;
684 dr->wValue = 0;
685 dr->wLength = __cpu_to_le16(skb->len);
686
687 pipe = usb_sndctrlpipe(data->udev, 0x00);
688
689 usb_fill_control_urb(urb, data->udev, pipe, (void *) dr,
690 skb->data, skb->len, btusb_tx_complete, skb);
691
692 hdev->stat.cmd_tx++;
693 break;
694
695 case HCI_ACLDATA_PKT:
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200696 if (!data->bulk_tx_ep || hdev->conn_hash.acl_num < 1)
697 return -ENODEV;
698
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200699 urb = usb_alloc_urb(0, GFP_ATOMIC);
700 if (!urb)
701 return -ENOMEM;
702
703 pipe = usb_sndbulkpipe(data->udev,
704 data->bulk_tx_ep->bEndpointAddress);
705
706 usb_fill_bulk_urb(urb, data->udev, pipe,
707 skb->data, skb->len, btusb_tx_complete, skb);
708
709 hdev->stat.acl_tx++;
710 break;
711
712 case HCI_SCODATA_PKT:
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200713 if (!data->isoc_tx_ep || hdev->conn_hash.sco_num < 1)
714 return -ENODEV;
715
716 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_ATOMIC);
717 if (!urb)
718 return -ENOMEM;
719
720 pipe = usb_sndisocpipe(data->udev,
721 data->isoc_tx_ep->bEndpointAddress);
722
Gustavo F. Padovan03c2d0e2011-02-14 18:53:43 -0300723 usb_fill_int_urb(urb, data->udev, pipe,
724 skb->data, skb->len, btusb_isoc_tx_complete,
725 skb, data->isoc_tx_ep->bInterval);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200726
727 urb->transfer_flags = URB_ISO_ASAP;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200728
729 __fill_isoc_descriptor(urb, skb->len,
730 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
731
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200732 hdev->stat.sco_tx++;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200733 goto skip_waking;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200734
735 default:
736 return -EILSEQ;
737 }
738
Oliver Neukum7bee5492009-08-24 23:44:59 +0200739 err = inc_tx(data);
740 if (err) {
741 usb_anchor_urb(urb, &data->deferred);
742 schedule_work(&data->waker);
743 err = 0;
744 goto done;
745 }
746
747skip_waking:
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200748 usb_anchor_urb(urb, &data->tx_anchor);
749
750 err = usb_submit_urb(urb, GFP_ATOMIC);
751 if (err < 0) {
752 BT_ERR("%s urb %p submission failed", hdev->name, urb);
753 kfree(urb->setup_packet);
754 usb_unanchor_urb(urb);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200755 } else {
756 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200757 }
758
759 usb_free_urb(urb);
760
Oliver Neukum7bee5492009-08-24 23:44:59 +0200761done:
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200762 return err;
763}
764
765static void btusb_destruct(struct hci_dev *hdev)
766{
767 struct btusb_data *data = hdev->driver_data;
768
769 BT_DBG("%s", hdev->name);
770
771 kfree(data);
772}
773
774static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
775{
776 struct btusb_data *data = hdev->driver_data;
777
778 BT_DBG("%s evt %d", hdev->name, evt);
779
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100780 if (hdev->conn_hash.sco_num != data->sco_num) {
781 data->sco_num = hdev->conn_hash.sco_num;
782 schedule_work(&data->work);
Marcel Holtmanna780efa2008-11-30 12:17:12 +0100783 }
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200784}
785
Jesper Juhl42b16b32011-01-17 00:09:38 +0100786static inline int __set_isoc_interface(struct hci_dev *hdev, int altsetting)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200787{
788 struct btusb_data *data = hdev->driver_data;
789 struct usb_interface *intf = data->isoc;
790 struct usb_endpoint_descriptor *ep_desc;
791 int i, err;
792
793 if (!data->isoc)
794 return -ENODEV;
795
796 err = usb_set_interface(data->udev, 1, altsetting);
797 if (err < 0) {
798 BT_ERR("%s setting interface failed (%d)", hdev->name, -err);
799 return err;
800 }
801
802 data->isoc_altsetting = altsetting;
803
804 data->isoc_tx_ep = NULL;
805 data->isoc_rx_ep = NULL;
806
807 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
808 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
809
810 if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) {
811 data->isoc_tx_ep = ep_desc;
812 continue;
813 }
814
815 if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) {
816 data->isoc_rx_ep = ep_desc;
817 continue;
818 }
819 }
820
821 if (!data->isoc_tx_ep || !data->isoc_rx_ep) {
822 BT_ERR("%s invalid SCO descriptors", hdev->name);
823 return -ENODEV;
824 }
825
826 return 0;
827}
828
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200829static void btusb_work(struct work_struct *work)
830{
831 struct btusb_data *data = container_of(work, struct btusb_data, work);
832 struct hci_dev *hdev = data->hdev;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200833 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200834
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200835 if (hdev->conn_hash.sco_num > 0) {
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -0300836 if (!test_bit(BTUSB_DID_ISO_RESUME, &data->flags)) {
Oliver Neukum8efdd0c2011-02-11 13:00:06 +0100837 err = usb_autopm_get_interface(data->isoc ? data->isoc : data->intf);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200838 if (err < 0) {
839 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
840 usb_kill_anchored_urbs(&data->isoc_anchor);
841 return;
842 }
843
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -0300844 set_bit(BTUSB_DID_ISO_RESUME, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200845 }
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200846 if (data->isoc_altsetting != 2) {
847 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
848 usb_kill_anchored_urbs(&data->isoc_anchor);
849
850 if (__set_isoc_interface(hdev, 2) < 0)
851 return;
852 }
853
854 if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100855 if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200856 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
857 else
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100858 btusb_submit_isoc_urb(hdev, GFP_KERNEL);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200859 }
860 } else {
861 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
862 usb_kill_anchored_urbs(&data->isoc_anchor);
863
864 __set_isoc_interface(hdev, 0);
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -0300865 if (test_and_clear_bit(BTUSB_DID_ISO_RESUME, &data->flags))
Oliver Neukum8efdd0c2011-02-11 13:00:06 +0100866 usb_autopm_put_interface(data->isoc ? data->isoc : data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200867 }
868}
869
Oliver Neukum7bee5492009-08-24 23:44:59 +0200870static void btusb_waker(struct work_struct *work)
871{
872 struct btusb_data *data = container_of(work, struct btusb_data, waker);
873 int err;
874
875 err = usb_autopm_get_interface(data->intf);
876 if (err < 0)
877 return;
878
879 usb_autopm_put_interface(data->intf);
880}
881
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200882static int btusb_probe(struct usb_interface *intf,
883 const struct usb_device_id *id)
884{
885 struct usb_endpoint_descriptor *ep_desc;
886 struct btusb_data *data;
887 struct hci_dev *hdev;
888 int i, err;
889
890 BT_DBG("intf %p id %p", intf, id);
891
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200892 /* interface numbers are hardcoded in the spec */
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200893 if (intf->cur_altsetting->desc.bInterfaceNumber != 0)
894 return -ENODEV;
895
896 if (!id->driver_info) {
897 const struct usb_device_id *match;
898 match = usb_match_id(intf, blacklist_table);
899 if (match)
900 id = match;
901 }
902
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200903 if (id->driver_info == BTUSB_IGNORE)
904 return -ENODEV;
905
906 if (ignore_dga && id->driver_info & BTUSB_DIGIANSWER)
907 return -ENODEV;
908
909 if (ignore_csr && id->driver_info & BTUSB_CSR)
910 return -ENODEV;
911
912 if (ignore_sniffer && id->driver_info & BTUSB_SNIFFER)
913 return -ENODEV;
914
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200915 data = kzalloc(sizeof(*data), GFP_KERNEL);
916 if (!data)
917 return -ENOMEM;
918
919 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
920 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
921
922 if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
923 data->intr_ep = ep_desc;
924 continue;
925 }
926
927 if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
928 data->bulk_tx_ep = ep_desc;
929 continue;
930 }
931
932 if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
933 data->bulk_rx_ep = ep_desc;
934 continue;
935 }
936 }
937
938 if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep) {
939 kfree(data);
940 return -ENODEV;
941 }
942
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100943 data->cmdreq_type = USB_TYPE_CLASS;
944
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200945 data->udev = interface_to_usbdev(intf);
Marcel Holtmann5fbcd262008-09-23 00:16:36 +0200946 data->intf = intf;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200947
948 spin_lock_init(&data->lock);
949
950 INIT_WORK(&data->work, btusb_work);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200951 INIT_WORK(&data->waker, btusb_waker);
952 spin_lock_init(&data->txlock);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200953
954 init_usb_anchor(&data->tx_anchor);
955 init_usb_anchor(&data->intr_anchor);
956 init_usb_anchor(&data->bulk_anchor);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200957 init_usb_anchor(&data->isoc_anchor);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200958 init_usb_anchor(&data->deferred);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200959
960 hdev = hci_alloc_dev();
961 if (!hdev) {
962 kfree(data);
963 return -ENOMEM;
964 }
965
Marcel Holtmannc13854c2010-02-08 15:27:07 +0100966 hdev->bus = HCI_USB;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200967 hdev->driver_data = data;
968
969 data->hdev = hdev;
970
971 SET_HCIDEV_DEV(hdev, &intf->dev);
972
973 hdev->open = btusb_open;
974 hdev->close = btusb_close;
975 hdev->flush = btusb_flush;
976 hdev->send = btusb_send_frame;
977 hdev->destruct = btusb_destruct;
978 hdev->notify = btusb_notify;
979
980 hdev->owner = THIS_MODULE;
981
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100982 /* Interface numbers are hardcoded in the specification */
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200983 data->isoc = usb_ifnum_to_if(data->udev, 1);
984
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100985 if (!reset)
986 set_bit(HCI_QUIRK_NO_RESET, &hdev->quirks);
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200987
988 if (force_scofix || id->driver_info & BTUSB_WRONG_SCO_MTU) {
989 if (!disable_scofix)
990 set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
991 }
992
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200993 if (id->driver_info & BTUSB_BROKEN_ISOC)
994 data->isoc = NULL;
995
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100996 if (id->driver_info & BTUSB_DIGIANSWER) {
997 data->cmdreq_type = USB_TYPE_VENDOR;
998 set_bit(HCI_QUIRK_NO_RESET, &hdev->quirks);
999 }
1000
1001 if (id->driver_info & BTUSB_CSR) {
1002 struct usb_device *udev = data->udev;
1003
1004 /* Old firmware would otherwise execute USB reset */
1005 if (le16_to_cpu(udev->descriptor.bcdDevice) < 0x117)
1006 set_bit(HCI_QUIRK_NO_RESET, &hdev->quirks);
1007 }
1008
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02001009 if (id->driver_info & BTUSB_SNIFFER) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001010 struct usb_device *udev = data->udev;
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02001011
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01001012 /* New sniffer firmware has crippled HCI interface */
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02001013 if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
1014 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001015
1016 data->isoc = NULL;
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02001017 }
1018
1019 if (id->driver_info & BTUSB_BCM92035) {
1020 unsigned char cmd[] = { 0x3b, 0xfc, 0x01, 0x00 };
1021 struct sk_buff *skb;
1022
1023 skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL);
1024 if (skb) {
1025 memcpy(skb_put(skb, sizeof(cmd)), cmd, sizeof(cmd));
1026 skb_queue_tail(&hdev->driver_init, skb);
1027 }
1028 }
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001029
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001030 if (data->isoc) {
1031 err = usb_driver_claim_interface(&btusb_driver,
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02001032 data->isoc, data);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001033 if (err < 0) {
1034 hci_free_dev(hdev);
1035 kfree(data);
1036 return err;
1037 }
1038 }
1039
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001040 err = hci_register_dev(hdev);
1041 if (err < 0) {
1042 hci_free_dev(hdev);
1043 kfree(data);
1044 return err;
1045 }
1046
1047 usb_set_intfdata(intf, data);
1048
1049 return 0;
1050}
1051
1052static void btusb_disconnect(struct usb_interface *intf)
1053{
1054 struct btusb_data *data = usb_get_intfdata(intf);
1055 struct hci_dev *hdev;
1056
1057 BT_DBG("intf %p", intf);
1058
1059 if (!data)
1060 return;
1061
1062 hdev = data->hdev;
1063
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02001064 __hci_dev_hold(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001065
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02001066 usb_set_intfdata(data->intf, NULL);
1067
1068 if (data->isoc)
1069 usb_set_intfdata(data->isoc, NULL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001070
1071 hci_unregister_dev(hdev);
1072
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02001073 if (intf == data->isoc)
1074 usb_driver_release_interface(&btusb_driver, data->intf);
1075 else if (data->isoc)
1076 usb_driver_release_interface(&btusb_driver, data->isoc);
1077
1078 __hci_dev_put(hdev);
1079
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001080 hci_free_dev(hdev);
1081}
1082
Oliver Neukum7bee5492009-08-24 23:44:59 +02001083#ifdef CONFIG_PM
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001084static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
1085{
1086 struct btusb_data *data = usb_get_intfdata(intf);
1087
1088 BT_DBG("intf %p", intf);
1089
1090 if (data->suspend_count++)
1091 return 0;
1092
Oliver Neukum7bee5492009-08-24 23:44:59 +02001093 spin_lock_irq(&data->txlock);
Alan Sternfb34d532009-11-13 11:53:59 -05001094 if (!((message.event & PM_EVENT_AUTO) && data->tx_in_flight)) {
Oliver Neukum7bee5492009-08-24 23:44:59 +02001095 set_bit(BTUSB_SUSPENDING, &data->flags);
1096 spin_unlock_irq(&data->txlock);
1097 } else {
1098 spin_unlock_irq(&data->txlock);
1099 data->suspend_count--;
1100 return -EBUSY;
1101 }
1102
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001103 cancel_work_sync(&data->work);
1104
Oliver Neukum7bee5492009-08-24 23:44:59 +02001105 btusb_stop_traffic(data);
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001106 usb_kill_anchored_urbs(&data->tx_anchor);
1107
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001108 return 0;
1109}
1110
Oliver Neukum7bee5492009-08-24 23:44:59 +02001111static void play_deferred(struct btusb_data *data)
1112{
1113 struct urb *urb;
1114 int err;
1115
1116 while ((urb = usb_get_from_anchor(&data->deferred))) {
1117 err = usb_submit_urb(urb, GFP_ATOMIC);
1118 if (err < 0)
1119 break;
1120
1121 data->tx_in_flight++;
1122 }
1123 usb_scuttle_anchored_urbs(&data->deferred);
1124}
1125
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001126static int btusb_resume(struct usb_interface *intf)
1127{
1128 struct btusb_data *data = usb_get_intfdata(intf);
1129 struct hci_dev *hdev = data->hdev;
Oliver Neukum7bee5492009-08-24 23:44:59 +02001130 int err = 0;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001131
1132 BT_DBG("intf %p", intf);
1133
1134 if (--data->suspend_count)
1135 return 0;
1136
1137 if (!test_bit(HCI_RUNNING, &hdev->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +02001138 goto done;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001139
1140 if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
1141 err = btusb_submit_intr_urb(hdev, GFP_NOIO);
1142 if (err < 0) {
1143 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001144 goto failed;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001145 }
1146 }
1147
1148 if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) {
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001149 err = btusb_submit_bulk_urb(hdev, GFP_NOIO);
1150 if (err < 0) {
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001151 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001152 goto failed;
1153 }
1154
1155 btusb_submit_bulk_urb(hdev, GFP_NOIO);
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001156 }
1157
1158 if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
1159 if (btusb_submit_isoc_urb(hdev, GFP_NOIO) < 0)
1160 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1161 else
1162 btusb_submit_isoc_urb(hdev, GFP_NOIO);
1163 }
1164
Oliver Neukum7bee5492009-08-24 23:44:59 +02001165 spin_lock_irq(&data->txlock);
1166 play_deferred(data);
1167 clear_bit(BTUSB_SUSPENDING, &data->flags);
1168 spin_unlock_irq(&data->txlock);
1169 schedule_work(&data->work);
1170
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001171 return 0;
Oliver Neukum7bee5492009-08-24 23:44:59 +02001172
1173failed:
1174 usb_scuttle_anchored_urbs(&data->deferred);
1175done:
1176 spin_lock_irq(&data->txlock);
1177 clear_bit(BTUSB_SUSPENDING, &data->flags);
1178 spin_unlock_irq(&data->txlock);
1179
1180 return err;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001181}
Oliver Neukum7bee5492009-08-24 23:44:59 +02001182#endif
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001183
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001184static struct usb_driver btusb_driver = {
1185 .name = "btusb",
1186 .probe = btusb_probe,
1187 .disconnect = btusb_disconnect,
Oliver Neukum7bee5492009-08-24 23:44:59 +02001188#ifdef CONFIG_PM
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001189 .suspend = btusb_suspend,
1190 .resume = btusb_resume,
Oliver Neukum7bee5492009-08-24 23:44:59 +02001191#endif
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001192 .id_table = btusb_table,
Oliver Neukum7bee5492009-08-24 23:44:59 +02001193 .supports_autosuspend = 1,
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001194};
1195
1196static int __init btusb_init(void)
1197{
1198 BT_INFO("Generic Bluetooth USB driver ver %s", VERSION);
1199
1200 return usb_register(&btusb_driver);
1201}
1202
1203static void __exit btusb_exit(void)
1204{
1205 usb_deregister(&btusb_driver);
1206}
1207
1208module_init(btusb_init);
1209module_exit(btusb_exit);
1210
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02001211module_param(ignore_dga, bool, 0644);
1212MODULE_PARM_DESC(ignore_dga, "Ignore devices with id 08fd:0001");
1213
1214module_param(ignore_csr, bool, 0644);
1215MODULE_PARM_DESC(ignore_csr, "Ignore devices with id 0a12:0001");
1216
1217module_param(ignore_sniffer, bool, 0644);
1218MODULE_PARM_DESC(ignore_sniffer, "Ignore devices with id 0a12:0002");
1219
1220module_param(disable_scofix, bool, 0644);
1221MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
1222
1223module_param(force_scofix, bool, 0644);
1224MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
1225
1226module_param(reset, bool, 0644);
1227MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
1228
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001229MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
1230MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
1231MODULE_VERSION(VERSION);
1232MODULE_LICENSE("GPL");