[Bluetooth] Fix I/O errors on MacBooks with Broadcom chips
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / bluetooth / btusb.c
CommitLineData
5e23b923
MH
1/*
2 *
3 * Generic Bluetooth USB driver
4 *
9bfa35fe 5 * Copyright (C) 2005-2008 Marcel Holtmann <marcel@holtmann.org>
5e23b923
MH
6 *
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
9bfa35fe 44#define VERSION "0.3"
cfeb4145
MH
45
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
5e23b923
MH
63
64static struct usb_device_id btusb_table[] = {
65 /* Generic Bluetooth USB device */
66 { USB_DEVICE_INFO(0xe0, 0x01, 0x01) },
67
cfeb4145
MH
68 /* 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
5e23b923
MH
84 { } /* Terminating entry */
85};
86
87MODULE_DEVICE_TABLE(usb, btusb_table);
88
89static struct usb_device_id blacklist_table[] = {
cfeb4145
MH
90 /* 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 */
105 { USB_DEVICE(0x0a5c, 0x2151), .driver_info = BTUSB_RESET },
106
bdbef3d6
MH
107 /* Apple MacBook Pro with Broadcom chip */
108 { USB_DEVICE(0x05ac, 0x820f), .driver_info = BTUSB_RESET },
109
cfeb4145
MH
110 /* IBM/Lenovo ThinkPad with Broadcom chip */
111 { USB_DEVICE(0x0a5c, 0x201e), .driver_info = BTUSB_RESET | BTUSB_WRONG_SCO_MTU },
112 { USB_DEVICE(0x0a5c, 0x2110), .driver_info = BTUSB_RESET | BTUSB_WRONG_SCO_MTU },
113
114 /* Targus ACB10US */
115 { USB_DEVICE(0x0a5c, 0x2100), .driver_info = BTUSB_RESET },
116
117 /* ANYCOM Bluetooth USB-200 and USB-250 */
118 { USB_DEVICE(0x0a5c, 0x2111), .driver_info = BTUSB_RESET },
119
120 /* HP laptop with Broadcom chip */
121 { USB_DEVICE(0x03f0, 0x171d), .driver_info = BTUSB_RESET | BTUSB_WRONG_SCO_MTU },
122
123 /* Dell laptop with Broadcom chip */
124 { USB_DEVICE(0x413c, 0x8126), .driver_info = BTUSB_RESET | BTUSB_WRONG_SCO_MTU },
125
126 /* Dell Wireless 370 */
127 { USB_DEVICE(0x413c, 0x8156), .driver_info = BTUSB_RESET | BTUSB_WRONG_SCO_MTU },
128
129 /* Dell Wireless 410 */
130 { USB_DEVICE(0x413c, 0x8152), .driver_info = BTUSB_RESET | BTUSB_WRONG_SCO_MTU },
131
132 /* Microsoft Wireless Transceiver for Bluetooth 2.0 */
133 { USB_DEVICE(0x045e, 0x009c), .driver_info = BTUSB_RESET },
134
135 /* Kensington Bluetooth USB adapter */
136 { USB_DEVICE(0x047d, 0x105d), .driver_info = BTUSB_RESET },
137 { USB_DEVICE(0x047d, 0x105e), .driver_info = BTUSB_RESET | BTUSB_WRONG_SCO_MTU },
138
139 /* ISSC Bluetooth Adapter v3.1 */
140 { USB_DEVICE(0x1131, 0x1001), .driver_info = BTUSB_RESET },
141
142 /* RTX Telecom based adapters with buggy SCO support */
143 { USB_DEVICE(0x0400, 0x0807), .driver_info = BTUSB_BROKEN_ISOC },
144 { USB_DEVICE(0x0400, 0x080a), .driver_info = BTUSB_BROKEN_ISOC },
145
146 /* CONWISE Technology based adapters with buggy SCO support */
147 { USB_DEVICE(0x0e5e, 0x6622), .driver_info = BTUSB_BROKEN_ISOC },
148
149 /* Belkin F8T012 and F8T013 devices */
150 { USB_DEVICE(0x050d, 0x0012), .driver_info = BTUSB_RESET | BTUSB_WRONG_SCO_MTU },
151 { USB_DEVICE(0x050d, 0x0013), .driver_info = BTUSB_RESET | BTUSB_WRONG_SCO_MTU },
152
153 /* Digianswer devices */
154 { USB_DEVICE(0x08fd, 0x0001), .driver_info = BTUSB_DIGIANSWER },
155 { USB_DEVICE(0x08fd, 0x0002), .driver_info = BTUSB_IGNORE },
156
157 /* CSR BlueCore Bluetooth Sniffer */
158 { USB_DEVICE(0x0a12, 0x0002), .driver_info = BTUSB_SNIFFER },
159
160 /* Frontline ComProbe Bluetooth Sniffer */
161 { USB_DEVICE(0x16d3, 0x0002), .driver_info = BTUSB_SNIFFER },
162
5e23b923
MH
163 { } /* Terminating entry */
164};
165
9bfa35fe
MH
166#define BTUSB_MAX_ISOC_FRAMES 10
167
5e23b923
MH
168#define BTUSB_INTR_RUNNING 0
169#define BTUSB_BULK_RUNNING 1
9bfa35fe 170#define BTUSB_ISOC_RUNNING 2
5e23b923
MH
171
172struct btusb_data {
173 struct hci_dev *hdev;
174 struct usb_device *udev;
9bfa35fe 175 struct usb_interface *isoc;
5e23b923
MH
176
177 spinlock_t lock;
178
179 unsigned long flags;
180
181 struct work_struct work;
182
183 struct usb_anchor tx_anchor;
184 struct usb_anchor intr_anchor;
185 struct usb_anchor bulk_anchor;
9bfa35fe 186 struct usb_anchor isoc_anchor;
5e23b923
MH
187
188 struct usb_endpoint_descriptor *intr_ep;
189 struct usb_endpoint_descriptor *bulk_tx_ep;
190 struct usb_endpoint_descriptor *bulk_rx_ep;
9bfa35fe
MH
191 struct usb_endpoint_descriptor *isoc_tx_ep;
192 struct usb_endpoint_descriptor *isoc_rx_ep;
193
194 int isoc_altsetting;
5e23b923
MH
195};
196
197static void btusb_intr_complete(struct urb *urb)
198{
199 struct hci_dev *hdev = urb->context;
200 struct btusb_data *data = hdev->driver_data;
201 int err;
202
203 BT_DBG("%s urb %p status %d count %d", hdev->name,
204 urb, urb->status, urb->actual_length);
205
206 if (!test_bit(HCI_RUNNING, &hdev->flags))
207 return;
208
209 if (urb->status == 0) {
9bfa35fe
MH
210 hdev->stat.byte_rx += urb->actual_length;
211
5e23b923
MH
212 if (hci_recv_fragment(hdev, HCI_EVENT_PKT,
213 urb->transfer_buffer,
214 urb->actual_length) < 0) {
215 BT_ERR("%s corrupted event packet", hdev->name);
216 hdev->stat.err_rx++;
217 }
218 }
219
220 if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
221 return;
222
223 usb_anchor_urb(urb, &data->intr_anchor);
224
225 err = usb_submit_urb(urb, GFP_ATOMIC);
226 if (err < 0) {
227 BT_ERR("%s urb %p failed to resubmit (%d)",
228 hdev->name, urb, -err);
229 usb_unanchor_urb(urb);
230 }
231}
232
9bfa35fe 233static int btusb_submit_intr_urb(struct hci_dev *hdev)
5e23b923
MH
234{
235 struct btusb_data *data = hdev->driver_data;
236 struct urb *urb;
237 unsigned char *buf;
238 unsigned int pipe;
239 int err, size;
240
241 BT_DBG("%s", hdev->name);
242
9bfa35fe
MH
243 if (!data->intr_ep)
244 return -ENODEV;
245
5e23b923
MH
246 urb = usb_alloc_urb(0, GFP_ATOMIC);
247 if (!urb)
248 return -ENOMEM;
249
250 size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
251
252 buf = kmalloc(size, GFP_ATOMIC);
253 if (!buf) {
254 usb_free_urb(urb);
255 return -ENOMEM;
256 }
257
258 pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
259
260 usb_fill_int_urb(urb, data->udev, pipe, buf, size,
261 btusb_intr_complete, hdev,
262 data->intr_ep->bInterval);
263
264 urb->transfer_flags |= URB_FREE_BUFFER;
265
266 usb_anchor_urb(urb, &data->intr_anchor);
267
268 err = usb_submit_urb(urb, GFP_ATOMIC);
269 if (err < 0) {
270 BT_ERR("%s urb %p submission failed (%d)",
271 hdev->name, urb, -err);
272 usb_unanchor_urb(urb);
273 kfree(buf);
274 }
275
276 usb_free_urb(urb);
277
278 return err;
279}
280
281static void btusb_bulk_complete(struct urb *urb)
282{
283 struct hci_dev *hdev = urb->context;
284 struct btusb_data *data = hdev->driver_data;
285 int err;
286
287 BT_DBG("%s urb %p status %d count %d", hdev->name,
288 urb, urb->status, urb->actual_length);
289
290 if (!test_bit(HCI_RUNNING, &hdev->flags))
291 return;
292
293 if (urb->status == 0) {
9bfa35fe
MH
294 hdev->stat.byte_rx += urb->actual_length;
295
5e23b923
MH
296 if (hci_recv_fragment(hdev, HCI_ACLDATA_PKT,
297 urb->transfer_buffer,
298 urb->actual_length) < 0) {
299 BT_ERR("%s corrupted ACL packet", hdev->name);
300 hdev->stat.err_rx++;
301 }
302 }
303
304 if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
305 return;
306
307 usb_anchor_urb(urb, &data->bulk_anchor);
308
309 err = usb_submit_urb(urb, GFP_ATOMIC);
310 if (err < 0) {
311 BT_ERR("%s urb %p failed to resubmit (%d)",
312 hdev->name, urb, -err);
313 usb_unanchor_urb(urb);
314 }
315}
316
9bfa35fe 317static int btusb_submit_bulk_urb(struct hci_dev *hdev)
5e23b923
MH
318{
319 struct btusb_data *data = hdev->driver_data;
320 struct urb *urb;
321 unsigned char *buf;
322 unsigned int pipe;
323 int err, size;
324
325 BT_DBG("%s", hdev->name);
326
9bfa35fe
MH
327 if (!data->bulk_rx_ep)
328 return -ENODEV;
329
5e23b923
MH
330 urb = usb_alloc_urb(0, GFP_KERNEL);
331 if (!urb)
332 return -ENOMEM;
333
334 size = le16_to_cpu(data->bulk_rx_ep->wMaxPacketSize);
335
336 buf = kmalloc(size, GFP_KERNEL);
337 if (!buf) {
338 usb_free_urb(urb);
339 return -ENOMEM;
340 }
341
342 pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
343
344 usb_fill_bulk_urb(urb, data->udev, pipe,
345 buf, size, btusb_bulk_complete, hdev);
346
347 urb->transfer_flags |= URB_FREE_BUFFER;
348
349 usb_anchor_urb(urb, &data->bulk_anchor);
350
351 err = usb_submit_urb(urb, GFP_KERNEL);
352 if (err < 0) {
353 BT_ERR("%s urb %p submission failed (%d)",
354 hdev->name, urb, -err);
355 usb_unanchor_urb(urb);
356 kfree(buf);
357 }
358
359 usb_free_urb(urb);
360
361 return err;
362}
363
9bfa35fe
MH
364static void btusb_isoc_complete(struct urb *urb)
365{
366 struct hci_dev *hdev = urb->context;
367 struct btusb_data *data = hdev->driver_data;
368 int i, err;
369
370 BT_DBG("%s urb %p status %d count %d", hdev->name,
371 urb, urb->status, urb->actual_length);
372
373 if (!test_bit(HCI_RUNNING, &hdev->flags))
374 return;
375
376 if (urb->status == 0) {
377 for (i = 0; i < urb->number_of_packets; i++) {
378 unsigned int offset = urb->iso_frame_desc[i].offset;
379 unsigned int length = urb->iso_frame_desc[i].actual_length;
380
381 if (urb->iso_frame_desc[i].status)
382 continue;
383
384 hdev->stat.byte_rx += length;
385
386 if (hci_recv_fragment(hdev, HCI_SCODATA_PKT,
387 urb->transfer_buffer + offset,
388 length) < 0) {
389 BT_ERR("%s corrupted SCO packet", hdev->name);
390 hdev->stat.err_rx++;
391 }
392 }
393 }
394
395 if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags))
396 return;
397
398 usb_anchor_urb(urb, &data->isoc_anchor);
399
400 err = usb_submit_urb(urb, GFP_ATOMIC);
401 if (err < 0) {
402 BT_ERR("%s urb %p failed to resubmit (%d)",
403 hdev->name, urb, -err);
404 usb_unanchor_urb(urb);
405 }
406}
407
408static void inline __fill_isoc_descriptor(struct urb *urb, int len, int mtu)
409{
410 int i, offset = 0;
411
412 BT_DBG("len %d mtu %d", len, mtu);
413
414 for (i = 0; i < BTUSB_MAX_ISOC_FRAMES && len >= mtu;
415 i++, offset += mtu, len -= mtu) {
416 urb->iso_frame_desc[i].offset = offset;
417 urb->iso_frame_desc[i].length = mtu;
418 }
419
420 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
421 urb->iso_frame_desc[i].offset = offset;
422 urb->iso_frame_desc[i].length = len;
423 i++;
424 }
425
426 urb->number_of_packets = i;
427}
428
429static int btusb_submit_isoc_urb(struct hci_dev *hdev)
430{
431 struct btusb_data *data = hdev->driver_data;
432 struct urb *urb;
433 unsigned char *buf;
434 unsigned int pipe;
435 int err, size;
436
437 BT_DBG("%s", hdev->name);
438
439 if (!data->isoc_rx_ep)
440 return -ENODEV;
441
442 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_KERNEL);
443 if (!urb)
444 return -ENOMEM;
445
446 size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) *
447 BTUSB_MAX_ISOC_FRAMES;
448
449 buf = kmalloc(size, GFP_KERNEL);
450 if (!buf) {
451 usb_free_urb(urb);
452 return -ENOMEM;
453 }
454
455 pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress);
456
457 urb->dev = data->udev;
458 urb->pipe = pipe;
459 urb->context = hdev;
460 urb->complete = btusb_isoc_complete;
461 urb->interval = data->isoc_rx_ep->bInterval;
462
463 urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP;
464 urb->transfer_buffer = buf;
465 urb->transfer_buffer_length = size;
466
467 __fill_isoc_descriptor(urb, size,
468 le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize));
469
470 usb_anchor_urb(urb, &data->isoc_anchor);
471
472 err = usb_submit_urb(urb, GFP_KERNEL);
473 if (err < 0) {
474 BT_ERR("%s urb %p submission failed (%d)",
475 hdev->name, urb, -err);
476 usb_unanchor_urb(urb);
477 kfree(buf);
478 }
479
480 usb_free_urb(urb);
481
482 return err;
483}
484
5e23b923
MH
485static void btusb_tx_complete(struct urb *urb)
486{
487 struct sk_buff *skb = urb->context;
488 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
489
490 BT_DBG("%s urb %p status %d count %d", hdev->name,
491 urb, urb->status, urb->actual_length);
492
493 if (!test_bit(HCI_RUNNING, &hdev->flags))
494 goto done;
495
496 if (!urb->status)
497 hdev->stat.byte_tx += urb->transfer_buffer_length;
498 else
499 hdev->stat.err_tx++;
500
501done:
502 kfree(urb->setup_packet);
503
504 kfree_skb(skb);
505}
506
507static int btusb_open(struct hci_dev *hdev)
508{
509 struct btusb_data *data = hdev->driver_data;
510 int err;
511
512 BT_DBG("%s", hdev->name);
513
514 if (test_and_set_bit(HCI_RUNNING, &hdev->flags))
515 return 0;
516
517 if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
518 return 0;
519
520 err = btusb_submit_intr_urb(hdev);
521 if (err < 0) {
522 clear_bit(BTUSB_INTR_RUNNING, &hdev->flags);
523 clear_bit(HCI_RUNNING, &hdev->flags);
524 }
525
526 return err;
527}
528
529static int btusb_close(struct hci_dev *hdev)
530{
531 struct btusb_data *data = hdev->driver_data;
532
533 BT_DBG("%s", hdev->name);
534
535 if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
536 return 0;
537
9bfa35fe
MH
538 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
539 usb_kill_anchored_urbs(&data->intr_anchor);
540
5e23b923
MH
541 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
542 usb_kill_anchored_urbs(&data->bulk_anchor);
543
544 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
545 usb_kill_anchored_urbs(&data->intr_anchor);
546
547 return 0;
548}
549
550static int btusb_flush(struct hci_dev *hdev)
551{
552 struct btusb_data *data = hdev->driver_data;
553
554 BT_DBG("%s", hdev->name);
555
556 usb_kill_anchored_urbs(&data->tx_anchor);
557
558 return 0;
559}
560
561static int btusb_send_frame(struct sk_buff *skb)
562{
563 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
564 struct btusb_data *data = hdev->driver_data;
565 struct usb_ctrlrequest *dr;
566 struct urb *urb;
567 unsigned int pipe;
568 int err;
569
570 BT_DBG("%s", hdev->name);
571
572 if (!test_bit(HCI_RUNNING, &hdev->flags))
573 return -EBUSY;
574
575 switch (bt_cb(skb)->pkt_type) {
576 case HCI_COMMAND_PKT:
577 urb = usb_alloc_urb(0, GFP_ATOMIC);
578 if (!urb)
579 return -ENOMEM;
580
581 dr = kmalloc(sizeof(*dr), GFP_ATOMIC);
582 if (!dr) {
583 usb_free_urb(urb);
584 return -ENOMEM;
585 }
586
587 dr->bRequestType = USB_TYPE_CLASS;
588 dr->bRequest = 0;
589 dr->wIndex = 0;
590 dr->wValue = 0;
591 dr->wLength = __cpu_to_le16(skb->len);
592
593 pipe = usb_sndctrlpipe(data->udev, 0x00);
594
595 usb_fill_control_urb(urb, data->udev, pipe, (void *) dr,
596 skb->data, skb->len, btusb_tx_complete, skb);
597
598 hdev->stat.cmd_tx++;
599 break;
600
601 case HCI_ACLDATA_PKT:
9bfa35fe
MH
602 if (!data->bulk_tx_ep || hdev->conn_hash.acl_num < 1)
603 return -ENODEV;
604
5e23b923
MH
605 urb = usb_alloc_urb(0, GFP_ATOMIC);
606 if (!urb)
607 return -ENOMEM;
608
609 pipe = usb_sndbulkpipe(data->udev,
610 data->bulk_tx_ep->bEndpointAddress);
611
612 usb_fill_bulk_urb(urb, data->udev, pipe,
613 skb->data, skb->len, btusb_tx_complete, skb);
614
615 hdev->stat.acl_tx++;
616 break;
617
618 case HCI_SCODATA_PKT:
9bfa35fe
MH
619 if (!data->isoc_tx_ep || hdev->conn_hash.sco_num < 1)
620 return -ENODEV;
621
622 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_ATOMIC);
623 if (!urb)
624 return -ENOMEM;
625
626 pipe = usb_sndisocpipe(data->udev,
627 data->isoc_tx_ep->bEndpointAddress);
628
629 urb->dev = data->udev;
630 urb->pipe = pipe;
631 urb->context = skb;
632 urb->complete = btusb_tx_complete;
633 urb->interval = data->isoc_tx_ep->bInterval;
634
635 urb->transfer_flags = URB_ISO_ASAP;
636 urb->transfer_buffer = skb->data;
637 urb->transfer_buffer_length = skb->len;
638
639 __fill_isoc_descriptor(urb, skb->len,
640 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
641
5e23b923 642 hdev->stat.sco_tx++;
9bfa35fe 643 break;
5e23b923
MH
644
645 default:
646 return -EILSEQ;
647 }
648
649 usb_anchor_urb(urb, &data->tx_anchor);
650
651 err = usb_submit_urb(urb, GFP_ATOMIC);
652 if (err < 0) {
653 BT_ERR("%s urb %p submission failed", hdev->name, urb);
654 kfree(urb->setup_packet);
655 usb_unanchor_urb(urb);
656 }
657
658 usb_free_urb(urb);
659
660 return err;
661}
662
663static void btusb_destruct(struct hci_dev *hdev)
664{
665 struct btusb_data *data = hdev->driver_data;
666
667 BT_DBG("%s", hdev->name);
668
669 kfree(data);
670}
671
672static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
673{
674 struct btusb_data *data = hdev->driver_data;
675
676 BT_DBG("%s evt %d", hdev->name, evt);
677
678 if (evt == HCI_NOTIFY_CONN_ADD || evt == HCI_NOTIFY_CONN_DEL)
679 schedule_work(&data->work);
680}
681
9bfa35fe
MH
682static int inline __set_isoc_interface(struct hci_dev *hdev, int altsetting)
683{
684 struct btusb_data *data = hdev->driver_data;
685 struct usb_interface *intf = data->isoc;
686 struct usb_endpoint_descriptor *ep_desc;
687 int i, err;
688
689 if (!data->isoc)
690 return -ENODEV;
691
692 err = usb_set_interface(data->udev, 1, altsetting);
693 if (err < 0) {
694 BT_ERR("%s setting interface failed (%d)", hdev->name, -err);
695 return err;
696 }
697
698 data->isoc_altsetting = altsetting;
699
700 data->isoc_tx_ep = NULL;
701 data->isoc_rx_ep = NULL;
702
703 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
704 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
705
706 if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) {
707 data->isoc_tx_ep = ep_desc;
708 continue;
709 }
710
711 if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) {
712 data->isoc_rx_ep = ep_desc;
713 continue;
714 }
715 }
716
717 if (!data->isoc_tx_ep || !data->isoc_rx_ep) {
718 BT_ERR("%s invalid SCO descriptors", hdev->name);
719 return -ENODEV;
720 }
721
722 return 0;
723}
724
5e23b923
MH
725static void btusb_work(struct work_struct *work)
726{
727 struct btusb_data *data = container_of(work, struct btusb_data, work);
728 struct hci_dev *hdev = data->hdev;
729
9bfa35fe
MH
730 if (hdev->conn_hash.acl_num > 0) {
731 if (!test_and_set_bit(BTUSB_BULK_RUNNING, &data->flags)) {
732 if (btusb_submit_bulk_urb(hdev) < 0)
733 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
734 else
735 btusb_submit_bulk_urb(hdev);
736 }
737 } else {
5e23b923
MH
738 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
739 usb_kill_anchored_urbs(&data->bulk_anchor);
5e23b923
MH
740 }
741
9bfa35fe
MH
742 if (hdev->conn_hash.sco_num > 0) {
743 if (data->isoc_altsetting != 2) {
744 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
745 usb_kill_anchored_urbs(&data->isoc_anchor);
746
747 if (__set_isoc_interface(hdev, 2) < 0)
748 return;
749 }
750
751 if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
752 if (btusb_submit_isoc_urb(hdev) < 0)
753 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
754 else
755 btusb_submit_isoc_urb(hdev);
756 }
757 } else {
758 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
759 usb_kill_anchored_urbs(&data->isoc_anchor);
760
761 __set_isoc_interface(hdev, 0);
5e23b923
MH
762 }
763}
764
765static int btusb_probe(struct usb_interface *intf,
766 const struct usb_device_id *id)
767{
768 struct usb_endpoint_descriptor *ep_desc;
769 struct btusb_data *data;
770 struct hci_dev *hdev;
771 int i, err;
772
773 BT_DBG("intf %p id %p", intf, id);
774
cfeb4145 775 /* interface numbers are hardcoded in the spec */
5e23b923
MH
776 if (intf->cur_altsetting->desc.bInterfaceNumber != 0)
777 return -ENODEV;
778
779 if (!id->driver_info) {
780 const struct usb_device_id *match;
781 match = usb_match_id(intf, blacklist_table);
782 if (match)
783 id = match;
784 }
785
cfeb4145
MH
786 if (id->driver_info == BTUSB_IGNORE)
787 return -ENODEV;
788
789 if (ignore_dga && id->driver_info & BTUSB_DIGIANSWER)
790 return -ENODEV;
791
792 if (ignore_csr && id->driver_info & BTUSB_CSR)
793 return -ENODEV;
794
795 if (ignore_sniffer && id->driver_info & BTUSB_SNIFFER)
796 return -ENODEV;
797
5e23b923
MH
798 data = kzalloc(sizeof(*data), GFP_KERNEL);
799 if (!data)
800 return -ENOMEM;
801
802 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
803 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
804
805 if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
806 data->intr_ep = ep_desc;
807 continue;
808 }
809
810 if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
811 data->bulk_tx_ep = ep_desc;
812 continue;
813 }
814
815 if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
816 data->bulk_rx_ep = ep_desc;
817 continue;
818 }
819 }
820
821 if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep) {
822 kfree(data);
823 return -ENODEV;
824 }
825
826 data->udev = interface_to_usbdev(intf);
827
828 spin_lock_init(&data->lock);
829
830 INIT_WORK(&data->work, btusb_work);
831
832 init_usb_anchor(&data->tx_anchor);
833 init_usb_anchor(&data->intr_anchor);
834 init_usb_anchor(&data->bulk_anchor);
9bfa35fe 835 init_usb_anchor(&data->isoc_anchor);
5e23b923
MH
836
837 hdev = hci_alloc_dev();
838 if (!hdev) {
839 kfree(data);
840 return -ENOMEM;
841 }
842
843 hdev->type = HCI_USB;
844 hdev->driver_data = data;
845
846 data->hdev = hdev;
847
848 SET_HCIDEV_DEV(hdev, &intf->dev);
849
850 hdev->open = btusb_open;
851 hdev->close = btusb_close;
852 hdev->flush = btusb_flush;
853 hdev->send = btusb_send_frame;
854 hdev->destruct = btusb_destruct;
855 hdev->notify = btusb_notify;
856
857 hdev->owner = THIS_MODULE;
858
9bfa35fe
MH
859 /* interface numbers are hardcoded in the spec */
860 data->isoc = usb_ifnum_to_if(data->udev, 1);
861
cfeb4145
MH
862 if (reset || id->driver_info & BTUSB_RESET)
863 set_bit(HCI_QUIRK_RESET_ON_INIT, &hdev->quirks);
864
865 if (force_scofix || id->driver_info & BTUSB_WRONG_SCO_MTU) {
866 if (!disable_scofix)
867 set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
868 }
869
9bfa35fe
MH
870 if (id->driver_info & BTUSB_BROKEN_ISOC)
871 data->isoc = NULL;
872
cfeb4145 873 if (id->driver_info & BTUSB_SNIFFER) {
9bfa35fe 874 struct usb_device *udev = data->udev;
cfeb4145
MH
875
876 if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
877 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
9bfa35fe
MH
878
879 data->isoc = NULL;
cfeb4145
MH
880 }
881
882 if (id->driver_info & BTUSB_BCM92035) {
883 unsigned char cmd[] = { 0x3b, 0xfc, 0x01, 0x00 };
884 struct sk_buff *skb;
885
886 skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL);
887 if (skb) {
888 memcpy(skb_put(skb, sizeof(cmd)), cmd, sizeof(cmd));
889 skb_queue_tail(&hdev->driver_init, skb);
890 }
891 }
5e23b923 892
9bfa35fe
MH
893 if (data->isoc) {
894 err = usb_driver_claim_interface(&btusb_driver,
895 data->isoc, NULL);
896 if (err < 0) {
897 hci_free_dev(hdev);
898 kfree(data);
899 return err;
900 }
901 }
902
5e23b923
MH
903 err = hci_register_dev(hdev);
904 if (err < 0) {
905 hci_free_dev(hdev);
906 kfree(data);
907 return err;
908 }
909
910 usb_set_intfdata(intf, data);
911
912 return 0;
913}
914
915static void btusb_disconnect(struct usb_interface *intf)
916{
917 struct btusb_data *data = usb_get_intfdata(intf);
918 struct hci_dev *hdev;
919
920 BT_DBG("intf %p", intf);
921
922 if (!data)
923 return;
924
925 hdev = data->hdev;
926
9bfa35fe
MH
927 if (data->isoc)
928 usb_driver_release_interface(&btusb_driver, data->isoc);
929
5e23b923
MH
930 usb_set_intfdata(intf, NULL);
931
932 hci_unregister_dev(hdev);
933
934 hci_free_dev(hdev);
935}
936
937static struct usb_driver btusb_driver = {
938 .name = "btusb",
939 .probe = btusb_probe,
940 .disconnect = btusb_disconnect,
941 .id_table = btusb_table,
942};
943
944static int __init btusb_init(void)
945{
946 BT_INFO("Generic Bluetooth USB driver ver %s", VERSION);
947
948 return usb_register(&btusb_driver);
949}
950
951static void __exit btusb_exit(void)
952{
953 usb_deregister(&btusb_driver);
954}
955
956module_init(btusb_init);
957module_exit(btusb_exit);
958
cfeb4145
MH
959module_param(ignore_dga, bool, 0644);
960MODULE_PARM_DESC(ignore_dga, "Ignore devices with id 08fd:0001");
961
962module_param(ignore_csr, bool, 0644);
963MODULE_PARM_DESC(ignore_csr, "Ignore devices with id 0a12:0001");
964
965module_param(ignore_sniffer, bool, 0644);
966MODULE_PARM_DESC(ignore_sniffer, "Ignore devices with id 0a12:0002");
967
968module_param(disable_scofix, bool, 0644);
969MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
970
971module_param(force_scofix, bool, 0644);
972MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
973
974module_param(reset, bool, 0644);
975MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
976
5e23b923
MH
977MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
978MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
979MODULE_VERSION(VERSION);
980MODULE_LICENSE("GPL");