blob: 3c85f9b078b5f4a97c0b7ffe337b9afbf09132ef [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
38//#define CONFIG_BT_HCIBTUSB_DEBUG
39#ifndef CONFIG_BT_HCIBTUSB_DEBUG
40#undef BT_DBG
41#define BT_DBG(D...)
42#endif
43
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +020044#define VERSION "0.3"
Marcel Holtmanncfeb4142008-08-07 22:26:56 +020045
46static int ignore_dga;
47static int ignore_csr;
48static int ignore_sniffer;
49static int disable_scofix;
50static int force_scofix;
51static int reset;
52
53static struct usb_driver btusb_driver;
54
55#define BTUSB_IGNORE 0x01
56#define BTUSB_RESET 0x02
57#define BTUSB_DIGIANSWER 0x04
58#define BTUSB_CSR 0x08
59#define BTUSB_SNIFFER 0x10
60#define BTUSB_BCM92035 0x20
61#define BTUSB_BROKEN_ISOC 0x40
62#define BTUSB_WRONG_SCO_MTU 0x80
Marcel Holtmann5e23b922007-10-20 14:12:34 +020063
64static struct usb_device_id btusb_table[] = {
65 /* Generic Bluetooth USB device */
66 { USB_DEVICE_INFO(0xe0, 0x01, 0x01) },
67
Marcel Holtmanncfeb4142008-08-07 22:26:56 +020068 /* AVM BlueFRITZ! USB v2.0 */
69 { USB_DEVICE(0x057c, 0x3800) },
70
71 /* Bluetooth Ultraport Module from IBM */
72 { USB_DEVICE(0x04bf, 0x030a) },
73
74 /* ALPS Modules with non-standard id */
75 { USB_DEVICE(0x044e, 0x3001) },
76 { USB_DEVICE(0x044e, 0x3002) },
77
78 /* Ericsson with non-standard id */
79 { USB_DEVICE(0x0bdb, 0x1002) },
80
81 /* Canyon CN-BTU1 with HID interfaces */
82 { USB_DEVICE(0x0c10, 0x0000), .driver_info = BTUSB_RESET },
83
Marcel Holtmann5e23b922007-10-20 14:12:34 +020084 { } /* Terminating entry */
85};
86
87MODULE_DEVICE_TABLE(usb, btusb_table);
88
89static struct usb_device_id blacklist_table[] = {
Marcel Holtmanncfeb4142008-08-07 22:26:56 +020090 /* CSR BlueCore devices */
91 { USB_DEVICE(0x0a12, 0x0001), .driver_info = BTUSB_CSR },
92
93 /* Broadcom BCM2033 without firmware */
94 { USB_DEVICE(0x0a5c, 0x2033), .driver_info = BTUSB_IGNORE },
95
96 /* Broadcom BCM2035 */
97 { USB_DEVICE(0x0a5c, 0x2035), .driver_info = BTUSB_RESET | BTUSB_WRONG_SCO_MTU },
98 { USB_DEVICE(0x0a5c, 0x200a), .driver_info = BTUSB_RESET | BTUSB_WRONG_SCO_MTU },
99
100 /* Broadcom BCM2045 */
101 { USB_DEVICE(0x0a5c, 0x2039), .driver_info = BTUSB_RESET | BTUSB_WRONG_SCO_MTU },
102 { USB_DEVICE(0x0a5c, 0x2101), .driver_info = BTUSB_RESET | BTUSB_WRONG_SCO_MTU },
103
104 /* Broadcom BCM2046 */
Marcel Holtmann1305e9e2008-10-06 12:22:52 +0200105 { USB_DEVICE(0x0a5c, 0x2146), .driver_info = BTUSB_RESET },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200106 { USB_DEVICE(0x0a5c, 0x2151), .driver_info = BTUSB_RESET },
107
Marcel Holtmannbdbef3d2008-09-23 00:16:35 +0200108 /* Apple MacBook Pro with Broadcom chip */
109 { USB_DEVICE(0x05ac, 0x820f), .driver_info = BTUSB_RESET },
110
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200111 /* IBM/Lenovo ThinkPad with Broadcom chip */
112 { USB_DEVICE(0x0a5c, 0x201e), .driver_info = BTUSB_RESET | BTUSB_WRONG_SCO_MTU },
113 { USB_DEVICE(0x0a5c, 0x2110), .driver_info = BTUSB_RESET | BTUSB_WRONG_SCO_MTU },
114
115 /* Targus ACB10US */
116 { USB_DEVICE(0x0a5c, 0x2100), .driver_info = BTUSB_RESET },
Marcel Holtmann4f62f6c2008-10-06 12:22:51 +0200117 { USB_DEVICE(0x0a5c, 0x2154), .driver_info = BTUSB_RESET },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200118
119 /* ANYCOM Bluetooth USB-200 and USB-250 */
120 { USB_DEVICE(0x0a5c, 0x2111), .driver_info = BTUSB_RESET },
121
122 /* HP laptop with Broadcom chip */
123 { USB_DEVICE(0x03f0, 0x171d), .driver_info = BTUSB_RESET | BTUSB_WRONG_SCO_MTU },
124
125 /* Dell laptop with Broadcom chip */
126 { USB_DEVICE(0x413c, 0x8126), .driver_info = BTUSB_RESET | BTUSB_WRONG_SCO_MTU },
127
128 /* Dell Wireless 370 */
129 { USB_DEVICE(0x413c, 0x8156), .driver_info = BTUSB_RESET | BTUSB_WRONG_SCO_MTU },
130
131 /* Dell Wireless 410 */
132 { USB_DEVICE(0x413c, 0x8152), .driver_info = BTUSB_RESET | BTUSB_WRONG_SCO_MTU },
133
134 /* Microsoft Wireless Transceiver for Bluetooth 2.0 */
135 { USB_DEVICE(0x045e, 0x009c), .driver_info = BTUSB_RESET },
136
137 /* Kensington Bluetooth USB adapter */
138 { USB_DEVICE(0x047d, 0x105d), .driver_info = BTUSB_RESET },
139 { USB_DEVICE(0x047d, 0x105e), .driver_info = BTUSB_RESET | BTUSB_WRONG_SCO_MTU },
140
141 /* ISSC Bluetooth Adapter v3.1 */
142 { USB_DEVICE(0x1131, 0x1001), .driver_info = BTUSB_RESET },
143
144 /* RTX Telecom based adapters with buggy SCO support */
145 { USB_DEVICE(0x0400, 0x0807), .driver_info = BTUSB_BROKEN_ISOC },
146 { USB_DEVICE(0x0400, 0x080a), .driver_info = BTUSB_BROKEN_ISOC },
147
148 /* CONWISE Technology based adapters with buggy SCO support */
149 { USB_DEVICE(0x0e5e, 0x6622), .driver_info = BTUSB_BROKEN_ISOC },
150
151 /* Belkin F8T012 and F8T013 devices */
152 { USB_DEVICE(0x050d, 0x0012), .driver_info = BTUSB_RESET | BTUSB_WRONG_SCO_MTU },
153 { USB_DEVICE(0x050d, 0x0013), .driver_info = BTUSB_RESET | BTUSB_WRONG_SCO_MTU },
154
Marcel Holtmann4f62f6c2008-10-06 12:22:51 +0200155 /* Belkin F8T016 device */
156 { USB_DEVICE(0x050d, 0x016a), .driver_info = BTUSB_RESET },
157
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200158 /* Digianswer devices */
159 { USB_DEVICE(0x08fd, 0x0001), .driver_info = BTUSB_DIGIANSWER },
160 { USB_DEVICE(0x08fd, 0x0002), .driver_info = BTUSB_IGNORE },
161
162 /* CSR BlueCore Bluetooth Sniffer */
163 { USB_DEVICE(0x0a12, 0x0002), .driver_info = BTUSB_SNIFFER },
164
165 /* Frontline ComProbe Bluetooth Sniffer */
166 { USB_DEVICE(0x16d3, 0x0002), .driver_info = BTUSB_SNIFFER },
167
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200168 { } /* Terminating entry */
169};
170
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200171#define BTUSB_MAX_ISOC_FRAMES 10
172
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200173#define BTUSB_INTR_RUNNING 0
174#define BTUSB_BULK_RUNNING 1
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200175#define BTUSB_ISOC_RUNNING 2
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200176
177struct btusb_data {
178 struct hci_dev *hdev;
179 struct usb_device *udev;
Marcel Holtmann5fbcd262008-09-23 00:16:36 +0200180 struct usb_interface *intf;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200181 struct usb_interface *isoc;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200182
183 spinlock_t lock;
184
185 unsigned long flags;
186
187 struct work_struct work;
188
189 struct usb_anchor tx_anchor;
190 struct usb_anchor intr_anchor;
191 struct usb_anchor bulk_anchor;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200192 struct usb_anchor isoc_anchor;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200193
194 struct usb_endpoint_descriptor *intr_ep;
195 struct usb_endpoint_descriptor *bulk_tx_ep;
196 struct usb_endpoint_descriptor *bulk_rx_ep;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200197 struct usb_endpoint_descriptor *isoc_tx_ep;
198 struct usb_endpoint_descriptor *isoc_rx_ep;
199
200 int isoc_altsetting;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200201};
202
203static void btusb_intr_complete(struct urb *urb)
204{
205 struct hci_dev *hdev = urb->context;
206 struct btusb_data *data = hdev->driver_data;
207 int err;
208
209 BT_DBG("%s urb %p status %d count %d", hdev->name,
210 urb, urb->status, urb->actual_length);
211
212 if (!test_bit(HCI_RUNNING, &hdev->flags))
213 return;
214
215 if (urb->status == 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200216 hdev->stat.byte_rx += urb->actual_length;
217
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200218 if (hci_recv_fragment(hdev, HCI_EVENT_PKT,
219 urb->transfer_buffer,
220 urb->actual_length) < 0) {
221 BT_ERR("%s corrupted event packet", hdev->name);
222 hdev->stat.err_rx++;
223 }
224 }
225
226 if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
227 return;
228
229 usb_anchor_urb(urb, &data->intr_anchor);
230
231 err = usb_submit_urb(urb, GFP_ATOMIC);
232 if (err < 0) {
233 BT_ERR("%s urb %p failed to resubmit (%d)",
234 hdev->name, urb, -err);
235 usb_unanchor_urb(urb);
236 }
237}
238
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100239static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200240{
241 struct btusb_data *data = hdev->driver_data;
242 struct urb *urb;
243 unsigned char *buf;
244 unsigned int pipe;
245 int err, size;
246
247 BT_DBG("%s", hdev->name);
248
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200249 if (!data->intr_ep)
250 return -ENODEV;
251
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100252 urb = usb_alloc_urb(0, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200253 if (!urb)
254 return -ENOMEM;
255
256 size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
257
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100258 buf = kmalloc(size, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200259 if (!buf) {
260 usb_free_urb(urb);
261 return -ENOMEM;
262 }
263
264 pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
265
266 usb_fill_int_urb(urb, data->udev, pipe, buf, size,
267 btusb_intr_complete, hdev,
268 data->intr_ep->bInterval);
269
270 urb->transfer_flags |= URB_FREE_BUFFER;
271
272 usb_anchor_urb(urb, &data->intr_anchor);
273
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100274 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200275 if (err < 0) {
276 BT_ERR("%s urb %p submission failed (%d)",
277 hdev->name, urb, -err);
278 usb_unanchor_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200279 }
280
281 usb_free_urb(urb);
282
283 return err;
284}
285
286static void btusb_bulk_complete(struct urb *urb)
287{
288 struct hci_dev *hdev = urb->context;
289 struct btusb_data *data = hdev->driver_data;
290 int err;
291
292 BT_DBG("%s urb %p status %d count %d", hdev->name,
293 urb, urb->status, urb->actual_length);
294
295 if (!test_bit(HCI_RUNNING, &hdev->flags))
296 return;
297
298 if (urb->status == 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200299 hdev->stat.byte_rx += urb->actual_length;
300
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200301 if (hci_recv_fragment(hdev, HCI_ACLDATA_PKT,
302 urb->transfer_buffer,
303 urb->actual_length) < 0) {
304 BT_ERR("%s corrupted ACL packet", hdev->name);
305 hdev->stat.err_rx++;
306 }
307 }
308
309 if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
310 return;
311
312 usb_anchor_urb(urb, &data->bulk_anchor);
313
314 err = usb_submit_urb(urb, GFP_ATOMIC);
315 if (err < 0) {
316 BT_ERR("%s urb %p failed to resubmit (%d)",
317 hdev->name, urb, -err);
318 usb_unanchor_urb(urb);
319 }
320}
321
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100322static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200323{
324 struct btusb_data *data = hdev->driver_data;
325 struct urb *urb;
326 unsigned char *buf;
327 unsigned int pipe;
328 int err, size;
329
330 BT_DBG("%s", hdev->name);
331
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200332 if (!data->bulk_rx_ep)
333 return -ENODEV;
334
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100335 urb = usb_alloc_urb(0, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200336 if (!urb)
337 return -ENOMEM;
338
339 size = le16_to_cpu(data->bulk_rx_ep->wMaxPacketSize);
340
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100341 buf = kmalloc(size, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200342 if (!buf) {
343 usb_free_urb(urb);
344 return -ENOMEM;
345 }
346
347 pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
348
349 usb_fill_bulk_urb(urb, data->udev, pipe,
350 buf, size, btusb_bulk_complete, hdev);
351
352 urb->transfer_flags |= URB_FREE_BUFFER;
353
354 usb_anchor_urb(urb, &data->bulk_anchor);
355
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100356 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200357 if (err < 0) {
358 BT_ERR("%s urb %p submission failed (%d)",
359 hdev->name, urb, -err);
360 usb_unanchor_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200361 }
362
363 usb_free_urb(urb);
364
365 return err;
366}
367
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200368static void btusb_isoc_complete(struct urb *urb)
369{
370 struct hci_dev *hdev = urb->context;
371 struct btusb_data *data = hdev->driver_data;
372 int i, err;
373
374 BT_DBG("%s urb %p status %d count %d", hdev->name,
375 urb, urb->status, urb->actual_length);
376
377 if (!test_bit(HCI_RUNNING, &hdev->flags))
378 return;
379
380 if (urb->status == 0) {
381 for (i = 0; i < urb->number_of_packets; i++) {
382 unsigned int offset = urb->iso_frame_desc[i].offset;
383 unsigned int length = urb->iso_frame_desc[i].actual_length;
384
385 if (urb->iso_frame_desc[i].status)
386 continue;
387
388 hdev->stat.byte_rx += length;
389
390 if (hci_recv_fragment(hdev, HCI_SCODATA_PKT,
391 urb->transfer_buffer + offset,
392 length) < 0) {
393 BT_ERR("%s corrupted SCO packet", hdev->name);
394 hdev->stat.err_rx++;
395 }
396 }
397 }
398
399 if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags))
400 return;
401
402 usb_anchor_urb(urb, &data->isoc_anchor);
403
404 err = usb_submit_urb(urb, GFP_ATOMIC);
405 if (err < 0) {
406 BT_ERR("%s urb %p failed to resubmit (%d)",
407 hdev->name, urb, -err);
408 usb_unanchor_urb(urb);
409 }
410}
411
412static void inline __fill_isoc_descriptor(struct urb *urb, int len, int mtu)
413{
414 int i, offset = 0;
415
416 BT_DBG("len %d mtu %d", len, mtu);
417
418 for (i = 0; i < BTUSB_MAX_ISOC_FRAMES && len >= mtu;
419 i++, offset += mtu, len -= mtu) {
420 urb->iso_frame_desc[i].offset = offset;
421 urb->iso_frame_desc[i].length = mtu;
422 }
423
424 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
425 urb->iso_frame_desc[i].offset = offset;
426 urb->iso_frame_desc[i].length = len;
427 i++;
428 }
429
430 urb->number_of_packets = i;
431}
432
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100433static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200434{
435 struct btusb_data *data = hdev->driver_data;
436 struct urb *urb;
437 unsigned char *buf;
438 unsigned int pipe;
439 int err, size;
440
441 BT_DBG("%s", hdev->name);
442
443 if (!data->isoc_rx_ep)
444 return -ENODEV;
445
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100446 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200447 if (!urb)
448 return -ENOMEM;
449
450 size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) *
451 BTUSB_MAX_ISOC_FRAMES;
452
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100453 buf = kmalloc(size, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200454 if (!buf) {
455 usb_free_urb(urb);
456 return -ENOMEM;
457 }
458
459 pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress);
460
461 urb->dev = data->udev;
462 urb->pipe = pipe;
463 urb->context = hdev;
464 urb->complete = btusb_isoc_complete;
465 urb->interval = data->isoc_rx_ep->bInterval;
466
467 urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP;
468 urb->transfer_buffer = buf;
469 urb->transfer_buffer_length = size;
470
471 __fill_isoc_descriptor(urb, size,
472 le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize));
473
474 usb_anchor_urb(urb, &data->isoc_anchor);
475
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100476 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200477 if (err < 0) {
478 BT_ERR("%s urb %p submission failed (%d)",
479 hdev->name, urb, -err);
480 usb_unanchor_urb(urb);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200481 }
482
483 usb_free_urb(urb);
484
485 return err;
486}
487
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200488static void btusb_tx_complete(struct urb *urb)
489{
490 struct sk_buff *skb = urb->context;
491 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
492
493 BT_DBG("%s urb %p status %d count %d", hdev->name,
494 urb, urb->status, urb->actual_length);
495
496 if (!test_bit(HCI_RUNNING, &hdev->flags))
497 goto done;
498
499 if (!urb->status)
500 hdev->stat.byte_tx += urb->transfer_buffer_length;
501 else
502 hdev->stat.err_tx++;
503
504done:
505 kfree(urb->setup_packet);
506
507 kfree_skb(skb);
508}
509
510static int btusb_open(struct hci_dev *hdev)
511{
512 struct btusb_data *data = hdev->driver_data;
513 int err;
514
515 BT_DBG("%s", hdev->name);
516
517 if (test_and_set_bit(HCI_RUNNING, &hdev->flags))
518 return 0;
519
520 if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
521 return 0;
522
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100523 err = btusb_submit_intr_urb(hdev, GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200524 if (err < 0) {
Marcel Holtmanne8c3c3d2008-09-23 00:16:36 +0200525 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200526 clear_bit(HCI_RUNNING, &hdev->flags);
527 }
528
529 return err;
530}
531
532static int btusb_close(struct hci_dev *hdev)
533{
534 struct btusb_data *data = hdev->driver_data;
535
536 BT_DBG("%s", hdev->name);
537
538 if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
539 return 0;
540
Marcel Holtmanne8c3c3d2008-09-23 00:16:36 +0200541 cancel_work_sync(&data->work);
542
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200543 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
Marcel Holtmanne8c3c3d2008-09-23 00:16:36 +0200544 usb_kill_anchored_urbs(&data->isoc_anchor);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200545
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200546 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
547 usb_kill_anchored_urbs(&data->bulk_anchor);
548
549 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
550 usb_kill_anchored_urbs(&data->intr_anchor);
551
552 return 0;
553}
554
555static int btusb_flush(struct hci_dev *hdev)
556{
557 struct btusb_data *data = hdev->driver_data;
558
559 BT_DBG("%s", hdev->name);
560
561 usb_kill_anchored_urbs(&data->tx_anchor);
562
563 return 0;
564}
565
566static int btusb_send_frame(struct sk_buff *skb)
567{
568 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
569 struct btusb_data *data = hdev->driver_data;
570 struct usb_ctrlrequest *dr;
571 struct urb *urb;
572 unsigned int pipe;
573 int err;
574
575 BT_DBG("%s", hdev->name);
576
577 if (!test_bit(HCI_RUNNING, &hdev->flags))
578 return -EBUSY;
579
580 switch (bt_cb(skb)->pkt_type) {
581 case HCI_COMMAND_PKT:
582 urb = usb_alloc_urb(0, GFP_ATOMIC);
583 if (!urb)
584 return -ENOMEM;
585
586 dr = kmalloc(sizeof(*dr), GFP_ATOMIC);
587 if (!dr) {
588 usb_free_urb(urb);
589 return -ENOMEM;
590 }
591
592 dr->bRequestType = USB_TYPE_CLASS;
593 dr->bRequest = 0;
594 dr->wIndex = 0;
595 dr->wValue = 0;
596 dr->wLength = __cpu_to_le16(skb->len);
597
598 pipe = usb_sndctrlpipe(data->udev, 0x00);
599
600 usb_fill_control_urb(urb, data->udev, pipe, (void *) dr,
601 skb->data, skb->len, btusb_tx_complete, skb);
602
603 hdev->stat.cmd_tx++;
604 break;
605
606 case HCI_ACLDATA_PKT:
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200607 if (!data->bulk_tx_ep || hdev->conn_hash.acl_num < 1)
608 return -ENODEV;
609
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200610 urb = usb_alloc_urb(0, GFP_ATOMIC);
611 if (!urb)
612 return -ENOMEM;
613
614 pipe = usb_sndbulkpipe(data->udev,
615 data->bulk_tx_ep->bEndpointAddress);
616
617 usb_fill_bulk_urb(urb, data->udev, pipe,
618 skb->data, skb->len, btusb_tx_complete, skb);
619
620 hdev->stat.acl_tx++;
621 break;
622
623 case HCI_SCODATA_PKT:
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200624 if (!data->isoc_tx_ep || hdev->conn_hash.sco_num < 1)
625 return -ENODEV;
626
627 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_ATOMIC);
628 if (!urb)
629 return -ENOMEM;
630
631 pipe = usb_sndisocpipe(data->udev,
632 data->isoc_tx_ep->bEndpointAddress);
633
634 urb->dev = data->udev;
635 urb->pipe = pipe;
636 urb->context = skb;
637 urb->complete = btusb_tx_complete;
638 urb->interval = data->isoc_tx_ep->bInterval;
639
640 urb->transfer_flags = URB_ISO_ASAP;
641 urb->transfer_buffer = skb->data;
642 urb->transfer_buffer_length = skb->len;
643
644 __fill_isoc_descriptor(urb, skb->len,
645 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
646
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200647 hdev->stat.sco_tx++;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200648 break;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200649
650 default:
651 return -EILSEQ;
652 }
653
654 usb_anchor_urb(urb, &data->tx_anchor);
655
656 err = usb_submit_urb(urb, GFP_ATOMIC);
657 if (err < 0) {
658 BT_ERR("%s urb %p submission failed", hdev->name, urb);
659 kfree(urb->setup_packet);
660 usb_unanchor_urb(urb);
661 }
662
663 usb_free_urb(urb);
664
665 return err;
666}
667
668static void btusb_destruct(struct hci_dev *hdev)
669{
670 struct btusb_data *data = hdev->driver_data;
671
672 BT_DBG("%s", hdev->name);
673
674 kfree(data);
675}
676
677static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
678{
679 struct btusb_data *data = hdev->driver_data;
680
681 BT_DBG("%s evt %d", hdev->name, evt);
682
683 if (evt == HCI_NOTIFY_CONN_ADD || evt == HCI_NOTIFY_CONN_DEL)
684 schedule_work(&data->work);
685}
686
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200687static int inline __set_isoc_interface(struct hci_dev *hdev, int altsetting)
688{
689 struct btusb_data *data = hdev->driver_data;
690 struct usb_interface *intf = data->isoc;
691 struct usb_endpoint_descriptor *ep_desc;
692 int i, err;
693
694 if (!data->isoc)
695 return -ENODEV;
696
697 err = usb_set_interface(data->udev, 1, altsetting);
698 if (err < 0) {
699 BT_ERR("%s setting interface failed (%d)", hdev->name, -err);
700 return err;
701 }
702
703 data->isoc_altsetting = altsetting;
704
705 data->isoc_tx_ep = NULL;
706 data->isoc_rx_ep = NULL;
707
708 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
709 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
710
711 if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) {
712 data->isoc_tx_ep = ep_desc;
713 continue;
714 }
715
716 if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) {
717 data->isoc_rx_ep = ep_desc;
718 continue;
719 }
720 }
721
722 if (!data->isoc_tx_ep || !data->isoc_rx_ep) {
723 BT_ERR("%s invalid SCO descriptors", hdev->name);
724 return -ENODEV;
725 }
726
727 return 0;
728}
729
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200730static void btusb_work(struct work_struct *work)
731{
732 struct btusb_data *data = container_of(work, struct btusb_data, work);
733 struct hci_dev *hdev = data->hdev;
734
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200735 if (hdev->conn_hash.acl_num > 0) {
736 if (!test_and_set_bit(BTUSB_BULK_RUNNING, &data->flags)) {
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100737 if (btusb_submit_bulk_urb(hdev, GFP_KERNEL) < 0)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200738 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
739 else
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100740 btusb_submit_bulk_urb(hdev, GFP_KERNEL);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200741 }
742 } else {
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200743 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
744 usb_kill_anchored_urbs(&data->bulk_anchor);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200745 }
746
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200747 if (hdev->conn_hash.sco_num > 0) {
748 if (data->isoc_altsetting != 2) {
749 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
750 usb_kill_anchored_urbs(&data->isoc_anchor);
751
752 if (__set_isoc_interface(hdev, 2) < 0)
753 return;
754 }
755
756 if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100757 if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200758 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
759 else
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100760 btusb_submit_isoc_urb(hdev, GFP_KERNEL);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200761 }
762 } else {
763 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
764 usb_kill_anchored_urbs(&data->isoc_anchor);
765
766 __set_isoc_interface(hdev, 0);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200767 }
768}
769
770static int btusb_probe(struct usb_interface *intf,
771 const struct usb_device_id *id)
772{
773 struct usb_endpoint_descriptor *ep_desc;
774 struct btusb_data *data;
775 struct hci_dev *hdev;
776 int i, err;
777
778 BT_DBG("intf %p id %p", intf, id);
779
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200780 /* interface numbers are hardcoded in the spec */
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200781 if (intf->cur_altsetting->desc.bInterfaceNumber != 0)
782 return -ENODEV;
783
784 if (!id->driver_info) {
785 const struct usb_device_id *match;
786 match = usb_match_id(intf, blacklist_table);
787 if (match)
788 id = match;
789 }
790
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200791 if (id->driver_info == BTUSB_IGNORE)
792 return -ENODEV;
793
794 if (ignore_dga && id->driver_info & BTUSB_DIGIANSWER)
795 return -ENODEV;
796
797 if (ignore_csr && id->driver_info & BTUSB_CSR)
798 return -ENODEV;
799
800 if (ignore_sniffer && id->driver_info & BTUSB_SNIFFER)
801 return -ENODEV;
802
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200803 data = kzalloc(sizeof(*data), GFP_KERNEL);
804 if (!data)
805 return -ENOMEM;
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->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
811 data->intr_ep = ep_desc;
812 continue;
813 }
814
815 if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
816 data->bulk_tx_ep = ep_desc;
817 continue;
818 }
819
820 if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
821 data->bulk_rx_ep = ep_desc;
822 continue;
823 }
824 }
825
826 if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep) {
827 kfree(data);
828 return -ENODEV;
829 }
830
831 data->udev = interface_to_usbdev(intf);
Marcel Holtmann5fbcd262008-09-23 00:16:36 +0200832 data->intf = intf;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200833
834 spin_lock_init(&data->lock);
835
836 INIT_WORK(&data->work, btusb_work);
837
838 init_usb_anchor(&data->tx_anchor);
839 init_usb_anchor(&data->intr_anchor);
840 init_usb_anchor(&data->bulk_anchor);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200841 init_usb_anchor(&data->isoc_anchor);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200842
843 hdev = hci_alloc_dev();
844 if (!hdev) {
845 kfree(data);
846 return -ENOMEM;
847 }
848
849 hdev->type = HCI_USB;
850 hdev->driver_data = data;
851
852 data->hdev = hdev;
853
854 SET_HCIDEV_DEV(hdev, &intf->dev);
855
856 hdev->open = btusb_open;
857 hdev->close = btusb_close;
858 hdev->flush = btusb_flush;
859 hdev->send = btusb_send_frame;
860 hdev->destruct = btusb_destruct;
861 hdev->notify = btusb_notify;
862
863 hdev->owner = THIS_MODULE;
864
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200865 /* interface numbers are hardcoded in the spec */
866 data->isoc = usb_ifnum_to_if(data->udev, 1);
867
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200868 if (reset || id->driver_info & BTUSB_RESET)
869 set_bit(HCI_QUIRK_RESET_ON_INIT, &hdev->quirks);
870
871 if (force_scofix || id->driver_info & BTUSB_WRONG_SCO_MTU) {
872 if (!disable_scofix)
873 set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
874 }
875
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200876 if (id->driver_info & BTUSB_BROKEN_ISOC)
877 data->isoc = NULL;
878
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200879 if (id->driver_info & BTUSB_SNIFFER) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200880 struct usb_device *udev = data->udev;
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200881
882 if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
883 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200884
885 data->isoc = NULL;
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200886 }
887
888 if (id->driver_info & BTUSB_BCM92035) {
889 unsigned char cmd[] = { 0x3b, 0xfc, 0x01, 0x00 };
890 struct sk_buff *skb;
891
892 skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL);
893 if (skb) {
894 memcpy(skb_put(skb, sizeof(cmd)), cmd, sizeof(cmd));
895 skb_queue_tail(&hdev->driver_init, skb);
896 }
897 }
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200898
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200899 if (data->isoc) {
900 err = usb_driver_claim_interface(&btusb_driver,
Marcel Holtmann5fbcd262008-09-23 00:16:36 +0200901 data->isoc, data);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200902 if (err < 0) {
903 hci_free_dev(hdev);
904 kfree(data);
905 return err;
906 }
907 }
908
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200909 err = hci_register_dev(hdev);
910 if (err < 0) {
911 hci_free_dev(hdev);
912 kfree(data);
913 return err;
914 }
915
916 usb_set_intfdata(intf, data);
917
918 return 0;
919}
920
921static void btusb_disconnect(struct usb_interface *intf)
922{
923 struct btusb_data *data = usb_get_intfdata(intf);
924 struct hci_dev *hdev;
925
926 BT_DBG("intf %p", intf);
927
928 if (!data)
929 return;
930
931 hdev = data->hdev;
932
Marcel Holtmann5fbcd262008-09-23 00:16:36 +0200933 __hci_dev_hold(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200934
Marcel Holtmann5fbcd262008-09-23 00:16:36 +0200935 usb_set_intfdata(data->intf, NULL);
936
937 if (data->isoc)
938 usb_set_intfdata(data->isoc, NULL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200939
940 hci_unregister_dev(hdev);
941
Marcel Holtmann5fbcd262008-09-23 00:16:36 +0200942 if (intf == data->isoc)
943 usb_driver_release_interface(&btusb_driver, data->intf);
944 else if (data->isoc)
945 usb_driver_release_interface(&btusb_driver, data->isoc);
946
947 __hci_dev_put(hdev);
948
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200949 hci_free_dev(hdev);
950}
951
952static struct usb_driver btusb_driver = {
953 .name = "btusb",
954 .probe = btusb_probe,
955 .disconnect = btusb_disconnect,
956 .id_table = btusb_table,
957};
958
959static int __init btusb_init(void)
960{
961 BT_INFO("Generic Bluetooth USB driver ver %s", VERSION);
962
963 return usb_register(&btusb_driver);
964}
965
966static void __exit btusb_exit(void)
967{
968 usb_deregister(&btusb_driver);
969}
970
971module_init(btusb_init);
972module_exit(btusb_exit);
973
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200974module_param(ignore_dga, bool, 0644);
975MODULE_PARM_DESC(ignore_dga, "Ignore devices with id 08fd:0001");
976
977module_param(ignore_csr, bool, 0644);
978MODULE_PARM_DESC(ignore_csr, "Ignore devices with id 0a12:0001");
979
980module_param(ignore_sniffer, bool, 0644);
981MODULE_PARM_DESC(ignore_sniffer, "Ignore devices with id 0a12:0002");
982
983module_param(disable_scofix, bool, 0644);
984MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
985
986module_param(force_scofix, bool, 0644);
987MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
988
989module_param(reset, bool, 0644);
990MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
991
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200992MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
993MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
994MODULE_VERSION(VERSION);
995MODULE_LICENSE("GPL");