Initial commit
[wrt350n-kernel.git] / drivers / bluetooth / hci_usb.c
blob372c7ef633dab53471c5d3e0dda7ac4dc49fdeff
1 /*
2 HCI USB driver for Linux Bluetooth protocol stack (BlueZ)
3 Copyright (C) 2000-2001 Qualcomm Incorporated
4 Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
6 Copyright (C) 2003 Maxim Krasnyansky <maxk@qualcomm.com>
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License version 2 as
10 published by the Free Software Foundation;
12 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
13 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
15 IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
16 CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
17 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
22 COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
23 SOFTWARE IS DISCLAIMED.
27 * Bluetooth HCI USB driver.
28 * Based on original USB Bluetooth driver for Linux kernel
29 * Copyright (c) 2000 Greg Kroah-Hartman <greg@kroah.com>
30 * Copyright (c) 2000 Mark Douglas Corner <mcorner@umich.edu>
34 #include <linux/module.h>
36 #include <linux/kernel.h>
37 #include <linux/init.h>
38 #include <linux/unistd.h>
39 #include <linux/types.h>
40 #include <linux/interrupt.h>
41 #include <linux/moduleparam.h>
43 #include <linux/slab.h>
44 #include <linux/errno.h>
45 #include <linux/string.h>
46 #include <linux/skbuff.h>
48 #include <linux/usb.h>
50 #include <net/bluetooth/bluetooth.h>
51 #include <net/bluetooth/hci_core.h>
53 #include "hci_usb.h"
55 #ifndef CONFIG_BT_HCIUSB_DEBUG
56 #undef BT_DBG
57 #define BT_DBG(D...)
58 #endif
60 #ifndef CONFIG_BT_HCIUSB_ZERO_PACKET
61 #undef URB_ZERO_PACKET
62 #define URB_ZERO_PACKET 0
63 #endif
65 static int ignore = 0;
66 static int ignore_dga = 0;
67 static int ignore_csr = 0;
68 static int ignore_sniffer = 0;
69 static int disable_scofix = 0;
70 static int force_scofix = 0;
71 static int reset = 0;
73 #ifdef CONFIG_BT_HCIUSB_SCO
74 static int isoc = 2;
75 #endif
77 #define VERSION "2.9"
79 static struct usb_driver hci_usb_driver;
81 static struct usb_device_id bluetooth_ids[] = {
82 /* Generic Bluetooth USB device */
83 { USB_DEVICE_INFO(HCI_DEV_CLASS, HCI_DEV_SUBCLASS, HCI_DEV_PROTOCOL) },
85 /* AVM BlueFRITZ! USB v2.0 */
86 { USB_DEVICE(0x057c, 0x3800) },
88 /* Bluetooth Ultraport Module from IBM */
89 { USB_DEVICE(0x04bf, 0x030a) },
91 /* ALPS Modules with non-standard id */
92 { USB_DEVICE(0x044e, 0x3001) },
93 { USB_DEVICE(0x044e, 0x3002) },
95 /* Ericsson with non-standard id */
96 { USB_DEVICE(0x0bdb, 0x1002) },
98 /* Canyon CN-BTU1 with HID interfaces */
99 { USB_DEVICE(0x0c10, 0x0000), .driver_info = HCI_RESET },
101 { } /* Terminating entry */
104 MODULE_DEVICE_TABLE (usb, bluetooth_ids);
106 static struct usb_device_id blacklist_ids[] = {
107 /* CSR BlueCore devices */
108 { USB_DEVICE(0x0a12, 0x0001), .driver_info = HCI_CSR },
110 /* Broadcom BCM2033 without firmware */
111 { USB_DEVICE(0x0a5c, 0x2033), .driver_info = HCI_IGNORE },
113 /* Broadcom BCM2035 */
114 { USB_DEVICE(0x0a5c, 0x2035), .driver_info = HCI_RESET | HCI_WRONG_SCO_MTU },
115 { USB_DEVICE(0x0a5c, 0x200a), .driver_info = HCI_RESET | HCI_WRONG_SCO_MTU },
116 { USB_DEVICE(0x0a5c, 0x2009), .driver_info = HCI_BCM92035 },
118 /* Broadcom BCM2045 */
119 { USB_DEVICE(0x0a5c, 0x2101), .driver_info = HCI_RESET | HCI_WRONG_SCO_MTU },
121 /* IBM/Lenovo ThinkPad with Broadcom chip */
122 { USB_DEVICE(0x0a5c, 0x201e), .driver_info = HCI_RESET | HCI_WRONG_SCO_MTU },
123 { USB_DEVICE(0x0a5c, 0x2110), .driver_info = HCI_RESET | HCI_WRONG_SCO_MTU },
125 /* Targus ACB10US */
126 { USB_DEVICE(0x0a5c, 0x2100), .driver_info = HCI_RESET },
128 /* ANYCOM Bluetooth USB-200 and USB-250 */
129 { USB_DEVICE(0x0a5c, 0x2111), .driver_info = HCI_RESET },
131 /* HP laptop with Broadcom chip */
132 { USB_DEVICE(0x03f0, 0x171d), .driver_info = HCI_RESET | HCI_WRONG_SCO_MTU },
134 /* Dell laptop with Broadcom chip */
135 { USB_DEVICE(0x413c, 0x8126), .driver_info = HCI_RESET | HCI_WRONG_SCO_MTU },
137 /* Microsoft Wireless Transceiver for Bluetooth 2.0 */
138 { USB_DEVICE(0x045e, 0x009c), .driver_info = HCI_RESET },
140 /* Kensington Bluetooth USB adapter */
141 { USB_DEVICE(0x047d, 0x105d), .driver_info = HCI_RESET },
142 { USB_DEVICE(0x047d, 0x105e), .driver_info = HCI_RESET | HCI_WRONG_SCO_MTU },
144 /* ISSC Bluetooth Adapter v3.1 */
145 { USB_DEVICE(0x1131, 0x1001), .driver_info = HCI_RESET },
147 /* RTX Telecom based adapters with buggy SCO support */
148 { USB_DEVICE(0x0400, 0x0807), .driver_info = HCI_BROKEN_ISOC },
149 { USB_DEVICE(0x0400, 0x080a), .driver_info = HCI_BROKEN_ISOC },
151 /* Belkin F8T012 and F8T013 devices */
152 { USB_DEVICE(0x050d, 0x0012), .driver_info = HCI_RESET | HCI_WRONG_SCO_MTU },
153 { USB_DEVICE(0x050d, 0x0013), .driver_info = HCI_RESET | HCI_WRONG_SCO_MTU },
155 /* Digianswer devices */
156 { USB_DEVICE(0x08fd, 0x0001), .driver_info = HCI_DIGIANSWER },
157 { USB_DEVICE(0x08fd, 0x0002), .driver_info = HCI_IGNORE },
159 /* CSR BlueCore Bluetooth Sniffer */
160 { USB_DEVICE(0x0a12, 0x0002), .driver_info = HCI_SNIFFER },
162 /* Frontline ComProbe Bluetooth Sniffer */
163 { USB_DEVICE(0x16d3, 0x0002), .driver_info = HCI_SNIFFER },
165 { } /* Terminating entry */
168 static struct _urb *_urb_alloc(int isoc, gfp_t gfp)
170 struct _urb *_urb = kmalloc(sizeof(struct _urb) +
171 sizeof(struct usb_iso_packet_descriptor) * isoc, gfp);
172 if (_urb) {
173 memset(_urb, 0, sizeof(*_urb));
174 usb_init_urb(&_urb->urb);
176 return _urb;
179 static struct _urb *_urb_dequeue(struct _urb_queue *q)
181 struct _urb *_urb = NULL;
182 unsigned long flags;
183 spin_lock_irqsave(&q->lock, flags);
185 struct list_head *head = &q->head;
186 struct list_head *next = head->next;
187 if (next != head) {
188 _urb = list_entry(next, struct _urb, list);
189 list_del(next); _urb->queue = NULL;
192 spin_unlock_irqrestore(&q->lock, flags);
193 return _urb;
196 static void hci_usb_rx_complete(struct urb *urb);
197 static void hci_usb_tx_complete(struct urb *urb);
199 #define __pending_tx(husb, type) (&husb->pending_tx[type-1])
200 #define __pending_q(husb, type) (&husb->pending_q[type-1])
201 #define __completed_q(husb, type) (&husb->completed_q[type-1])
202 #define __transmit_q(husb, type) (&husb->transmit_q[type-1])
204 static inline struct _urb *__get_completed(struct hci_usb *husb, int type)
206 return _urb_dequeue(__completed_q(husb, type));
209 #ifdef CONFIG_BT_HCIUSB_SCO
210 static void __fill_isoc_desc(struct urb *urb, int len, int mtu)
212 int offset = 0, i;
214 BT_DBG("len %d mtu %d", len, mtu);
216 for (i=0; i < HCI_MAX_ISOC_FRAMES && len >= mtu; i++, offset += mtu, len -= mtu) {
217 urb->iso_frame_desc[i].offset = offset;
218 urb->iso_frame_desc[i].length = mtu;
219 BT_DBG("desc %d offset %d len %d", i, offset, mtu);
221 if (len && i < HCI_MAX_ISOC_FRAMES) {
222 urb->iso_frame_desc[i].offset = offset;
223 urb->iso_frame_desc[i].length = len;
224 BT_DBG("desc %d offset %d len %d", i, offset, len);
225 i++;
227 urb->number_of_packets = i;
229 #endif
231 static int hci_usb_intr_rx_submit(struct hci_usb *husb)
233 struct _urb *_urb;
234 struct urb *urb;
235 int err, pipe, interval, size;
236 void *buf;
238 BT_DBG("%s", husb->hdev->name);
240 size = le16_to_cpu(husb->intr_in_ep->desc.wMaxPacketSize);
242 buf = kmalloc(size, GFP_ATOMIC);
243 if (!buf)
244 return -ENOMEM;
246 _urb = _urb_alloc(0, GFP_ATOMIC);
247 if (!_urb) {
248 kfree(buf);
249 return -ENOMEM;
251 _urb->type = HCI_EVENT_PKT;
252 _urb_queue_tail(__pending_q(husb, _urb->type), _urb);
254 urb = &_urb->urb;
255 pipe = usb_rcvintpipe(husb->udev, husb->intr_in_ep->desc.bEndpointAddress);
256 interval = husb->intr_in_ep->desc.bInterval;
257 usb_fill_int_urb(urb, husb->udev, pipe, buf, size, hci_usb_rx_complete, husb, interval);
259 err = usb_submit_urb(urb, GFP_ATOMIC);
260 if (err) {
261 BT_ERR("%s intr rx submit failed urb %p err %d",
262 husb->hdev->name, urb, err);
263 _urb_unlink(_urb);
264 _urb_free(_urb);
265 kfree(buf);
267 return err;
270 static int hci_usb_bulk_rx_submit(struct hci_usb *husb)
272 struct _urb *_urb;
273 struct urb *urb;
274 int err, pipe, size = HCI_MAX_FRAME_SIZE;
275 void *buf;
277 buf = kmalloc(size, GFP_ATOMIC);
278 if (!buf)
279 return -ENOMEM;
281 _urb = _urb_alloc(0, GFP_ATOMIC);
282 if (!_urb) {
283 kfree(buf);
284 return -ENOMEM;
286 _urb->type = HCI_ACLDATA_PKT;
287 _urb_queue_tail(__pending_q(husb, _urb->type), _urb);
289 urb = &_urb->urb;
290 pipe = usb_rcvbulkpipe(husb->udev, husb->bulk_in_ep->desc.bEndpointAddress);
291 usb_fill_bulk_urb(urb, husb->udev, pipe, buf, size, hci_usb_rx_complete, husb);
292 urb->transfer_flags = 0;
294 BT_DBG("%s urb %p", husb->hdev->name, urb);
296 err = usb_submit_urb(urb, GFP_ATOMIC);
297 if (err) {
298 BT_ERR("%s bulk rx submit failed urb %p err %d",
299 husb->hdev->name, urb, err);
300 _urb_unlink(_urb);
301 _urb_free(_urb);
302 kfree(buf);
304 return err;
307 #ifdef CONFIG_BT_HCIUSB_SCO
308 static int hci_usb_isoc_rx_submit(struct hci_usb *husb)
310 struct _urb *_urb;
311 struct urb *urb;
312 int err, mtu, size;
313 void *buf;
315 mtu = le16_to_cpu(husb->isoc_in_ep->desc.wMaxPacketSize);
316 size = mtu * HCI_MAX_ISOC_FRAMES;
318 buf = kmalloc(size, GFP_ATOMIC);
319 if (!buf)
320 return -ENOMEM;
322 _urb = _urb_alloc(HCI_MAX_ISOC_FRAMES, GFP_ATOMIC);
323 if (!_urb) {
324 kfree(buf);
325 return -ENOMEM;
327 _urb->type = HCI_SCODATA_PKT;
328 _urb_queue_tail(__pending_q(husb, _urb->type), _urb);
330 urb = &_urb->urb;
332 urb->context = husb;
333 urb->dev = husb->udev;
334 urb->pipe = usb_rcvisocpipe(husb->udev, husb->isoc_in_ep->desc.bEndpointAddress);
335 urb->complete = hci_usb_rx_complete;
337 urb->interval = husb->isoc_in_ep->desc.bInterval;
339 urb->transfer_buffer_length = size;
340 urb->transfer_buffer = buf;
341 urb->transfer_flags = URB_ISO_ASAP;
343 __fill_isoc_desc(urb, size, mtu);
345 BT_DBG("%s urb %p", husb->hdev->name, urb);
347 err = usb_submit_urb(urb, GFP_ATOMIC);
348 if (err) {
349 BT_ERR("%s isoc rx submit failed urb %p err %d",
350 husb->hdev->name, urb, err);
351 _urb_unlink(_urb);
352 _urb_free(_urb);
353 kfree(buf);
355 return err;
357 #endif
359 /* Initialize device */
360 static int hci_usb_open(struct hci_dev *hdev)
362 struct hci_usb *husb = (struct hci_usb *) hdev->driver_data;
363 int i, err;
364 unsigned long flags;
366 BT_DBG("%s", hdev->name);
368 if (test_and_set_bit(HCI_RUNNING, &hdev->flags))
369 return 0;
371 write_lock_irqsave(&husb->completion_lock, flags);
373 err = hci_usb_intr_rx_submit(husb);
374 if (!err) {
375 for (i = 0; i < HCI_MAX_BULK_RX; i++)
376 hci_usb_bulk_rx_submit(husb);
378 #ifdef CONFIG_BT_HCIUSB_SCO
379 if (husb->isoc_iface)
380 for (i = 0; i < HCI_MAX_ISOC_RX; i++)
381 hci_usb_isoc_rx_submit(husb);
382 #endif
383 } else {
384 clear_bit(HCI_RUNNING, &hdev->flags);
387 write_unlock_irqrestore(&husb->completion_lock, flags);
388 return err;
391 /* Reset device */
392 static int hci_usb_flush(struct hci_dev *hdev)
394 struct hci_usb *husb = (struct hci_usb *) hdev->driver_data;
395 int i;
397 BT_DBG("%s", hdev->name);
399 for (i = 0; i < 4; i++)
400 skb_queue_purge(&husb->transmit_q[i]);
401 return 0;
404 static void hci_usb_unlink_urbs(struct hci_usb *husb)
406 int i;
408 BT_DBG("%s", husb->hdev->name);
410 for (i = 0; i < 4; i++) {
411 struct _urb *_urb;
412 struct urb *urb;
414 /* Kill pending requests */
415 while ((_urb = _urb_dequeue(&husb->pending_q[i]))) {
416 urb = &_urb->urb;
417 BT_DBG("%s unlinking _urb %p type %d urb %p",
418 husb->hdev->name, _urb, _urb->type, urb);
419 usb_kill_urb(urb);
420 _urb_queue_tail(__completed_q(husb, _urb->type), _urb);
423 /* Release completed requests */
424 while ((_urb = _urb_dequeue(&husb->completed_q[i]))) {
425 urb = &_urb->urb;
426 BT_DBG("%s freeing _urb %p type %d urb %p",
427 husb->hdev->name, _urb, _urb->type, urb);
428 kfree(urb->setup_packet);
429 kfree(urb->transfer_buffer);
430 _urb_free(_urb);
435 /* Close device */
436 static int hci_usb_close(struct hci_dev *hdev)
438 struct hci_usb *husb = (struct hci_usb *) hdev->driver_data;
439 unsigned long flags;
441 if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
442 return 0;
444 BT_DBG("%s", hdev->name);
446 /* Synchronize with completion handlers */
447 write_lock_irqsave(&husb->completion_lock, flags);
448 write_unlock_irqrestore(&husb->completion_lock, flags);
450 hci_usb_unlink_urbs(husb);
451 hci_usb_flush(hdev);
452 return 0;
455 static int __tx_submit(struct hci_usb *husb, struct _urb *_urb)
457 struct urb *urb = &_urb->urb;
458 int err;
460 BT_DBG("%s urb %p type %d", husb->hdev->name, urb, _urb->type);
462 _urb_queue_tail(__pending_q(husb, _urb->type), _urb);
463 err = usb_submit_urb(urb, GFP_ATOMIC);
464 if (err) {
465 BT_ERR("%s tx submit failed urb %p type %d err %d",
466 husb->hdev->name, urb, _urb->type, err);
467 _urb_unlink(_urb);
468 _urb_queue_tail(__completed_q(husb, _urb->type), _urb);
469 } else
470 atomic_inc(__pending_tx(husb, _urb->type));
472 return err;
475 static inline int hci_usb_send_ctrl(struct hci_usb *husb, struct sk_buff *skb)
477 struct _urb *_urb = __get_completed(husb, bt_cb(skb)->pkt_type);
478 struct usb_ctrlrequest *dr;
479 struct urb *urb;
481 if (!_urb) {
482 _urb = _urb_alloc(0, GFP_ATOMIC);
483 if (!_urb)
484 return -ENOMEM;
485 _urb->type = bt_cb(skb)->pkt_type;
487 dr = kmalloc(sizeof(*dr), GFP_ATOMIC);
488 if (!dr) {
489 _urb_free(_urb);
490 return -ENOMEM;
492 } else
493 dr = (void *) _urb->urb.setup_packet;
495 dr->bRequestType = husb->ctrl_req;
496 dr->bRequest = 0;
497 dr->wIndex = 0;
498 dr->wValue = 0;
499 dr->wLength = __cpu_to_le16(skb->len);
501 urb = &_urb->urb;
502 usb_fill_control_urb(urb, husb->udev, usb_sndctrlpipe(husb->udev, 0),
503 (void *) dr, skb->data, skb->len, hci_usb_tx_complete, husb);
505 BT_DBG("%s skb %p len %d", husb->hdev->name, skb, skb->len);
507 _urb->priv = skb;
508 return __tx_submit(husb, _urb);
511 static inline int hci_usb_send_bulk(struct hci_usb *husb, struct sk_buff *skb)
513 struct _urb *_urb = __get_completed(husb, bt_cb(skb)->pkt_type);
514 struct urb *urb;
515 int pipe;
517 if (!_urb) {
518 _urb = _urb_alloc(0, GFP_ATOMIC);
519 if (!_urb)
520 return -ENOMEM;
521 _urb->type = bt_cb(skb)->pkt_type;
524 urb = &_urb->urb;
525 pipe = usb_sndbulkpipe(husb->udev, husb->bulk_out_ep->desc.bEndpointAddress);
526 usb_fill_bulk_urb(urb, husb->udev, pipe, skb->data, skb->len,
527 hci_usb_tx_complete, husb);
528 urb->transfer_flags = URB_ZERO_PACKET;
530 BT_DBG("%s skb %p len %d", husb->hdev->name, skb, skb->len);
532 _urb->priv = skb;
533 return __tx_submit(husb, _urb);
536 #ifdef CONFIG_BT_HCIUSB_SCO
537 static inline int hci_usb_send_isoc(struct hci_usb *husb, struct sk_buff *skb)
539 struct _urb *_urb = __get_completed(husb, bt_cb(skb)->pkt_type);
540 struct urb *urb;
542 if (!_urb) {
543 _urb = _urb_alloc(HCI_MAX_ISOC_FRAMES, GFP_ATOMIC);
544 if (!_urb)
545 return -ENOMEM;
546 _urb->type = bt_cb(skb)->pkt_type;
549 BT_DBG("%s skb %p len %d", husb->hdev->name, skb, skb->len);
551 urb = &_urb->urb;
553 urb->context = husb;
554 urb->dev = husb->udev;
555 urb->pipe = usb_sndisocpipe(husb->udev, husb->isoc_out_ep->desc.bEndpointAddress);
556 urb->complete = hci_usb_tx_complete;
557 urb->transfer_flags = URB_ISO_ASAP;
559 urb->interval = husb->isoc_out_ep->desc.bInterval;
561 urb->transfer_buffer = skb->data;
562 urb->transfer_buffer_length = skb->len;
564 __fill_isoc_desc(urb, skb->len, le16_to_cpu(husb->isoc_out_ep->desc.wMaxPacketSize));
566 _urb->priv = skb;
567 return __tx_submit(husb, _urb);
569 #endif
571 static void hci_usb_tx_process(struct hci_usb *husb)
573 struct sk_buff_head *q;
574 struct sk_buff *skb;
576 BT_DBG("%s", husb->hdev->name);
578 do {
579 clear_bit(HCI_USB_TX_WAKEUP, &husb->state);
581 /* Process command queue */
582 q = __transmit_q(husb, HCI_COMMAND_PKT);
583 if (!atomic_read(__pending_tx(husb, HCI_COMMAND_PKT)) &&
584 (skb = skb_dequeue(q))) {
585 if (hci_usb_send_ctrl(husb, skb) < 0)
586 skb_queue_head(q, skb);
589 #ifdef CONFIG_BT_HCIUSB_SCO
590 /* Process SCO queue */
591 q = __transmit_q(husb, HCI_SCODATA_PKT);
592 if (atomic_read(__pending_tx(husb, HCI_SCODATA_PKT)) < HCI_MAX_ISOC_TX &&
593 (skb = skb_dequeue(q))) {
594 if (hci_usb_send_isoc(husb, skb) < 0)
595 skb_queue_head(q, skb);
597 #endif
599 /* Process ACL queue */
600 q = __transmit_q(husb, HCI_ACLDATA_PKT);
601 while (atomic_read(__pending_tx(husb, HCI_ACLDATA_PKT)) < HCI_MAX_BULK_TX &&
602 (skb = skb_dequeue(q))) {
603 if (hci_usb_send_bulk(husb, skb) < 0) {
604 skb_queue_head(q, skb);
605 break;
608 } while(test_bit(HCI_USB_TX_WAKEUP, &husb->state));
611 static inline void hci_usb_tx_wakeup(struct hci_usb *husb)
613 /* Serialize TX queue processing to avoid data reordering */
614 if (!test_and_set_bit(HCI_USB_TX_PROCESS, &husb->state)) {
615 hci_usb_tx_process(husb);
616 clear_bit(HCI_USB_TX_PROCESS, &husb->state);
617 } else
618 set_bit(HCI_USB_TX_WAKEUP, &husb->state);
621 /* Send frames from HCI layer */
622 static int hci_usb_send_frame(struct sk_buff *skb)
624 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
625 struct hci_usb *husb;
627 if (!hdev) {
628 BT_ERR("frame for uknown device (hdev=NULL)");
629 return -ENODEV;
632 if (!test_bit(HCI_RUNNING, &hdev->flags))
633 return -EBUSY;
635 BT_DBG("%s type %d len %d", hdev->name, bt_cb(skb)->pkt_type, skb->len);
637 husb = (struct hci_usb *) hdev->driver_data;
639 switch (bt_cb(skb)->pkt_type) {
640 case HCI_COMMAND_PKT:
641 hdev->stat.cmd_tx++;
642 break;
644 case HCI_ACLDATA_PKT:
645 hdev->stat.acl_tx++;
646 break;
648 #ifdef CONFIG_BT_HCIUSB_SCO
649 case HCI_SCODATA_PKT:
650 hdev->stat.sco_tx++;
651 break;
652 #endif
654 default:
655 kfree_skb(skb);
656 return 0;
659 read_lock(&husb->completion_lock);
661 skb_queue_tail(__transmit_q(husb, bt_cb(skb)->pkt_type), skb);
662 hci_usb_tx_wakeup(husb);
664 read_unlock(&husb->completion_lock);
665 return 0;
668 static void hci_usb_rx_complete(struct urb *urb)
670 struct _urb *_urb = container_of(urb, struct _urb, urb);
671 struct hci_usb *husb = (void *) urb->context;
672 struct hci_dev *hdev = husb->hdev;
673 int err, count = urb->actual_length;
675 BT_DBG("%s urb %p type %d status %d count %d flags %x", hdev->name, urb,
676 _urb->type, urb->status, count, urb->transfer_flags);
678 read_lock(&husb->completion_lock);
680 if (!test_bit(HCI_RUNNING, &hdev->flags))
681 goto unlock;
683 if (urb->status || !count)
684 goto resubmit;
686 if (_urb->type == HCI_SCODATA_PKT) {
687 #ifdef CONFIG_BT_HCIUSB_SCO
688 int i;
689 for (i=0; i < urb->number_of_packets; i++) {
690 BT_DBG("desc %d status %d offset %d len %d", i,
691 urb->iso_frame_desc[i].status,
692 urb->iso_frame_desc[i].offset,
693 urb->iso_frame_desc[i].actual_length);
695 if (!urb->iso_frame_desc[i].status) {
696 husb->hdev->stat.byte_rx += urb->iso_frame_desc[i].actual_length;
697 hci_recv_fragment(husb->hdev, _urb->type,
698 urb->transfer_buffer + urb->iso_frame_desc[i].offset,
699 urb->iso_frame_desc[i].actual_length);
702 #else
704 #endif
705 } else {
706 husb->hdev->stat.byte_rx += count;
707 err = hci_recv_fragment(husb->hdev, _urb->type, urb->transfer_buffer, count);
708 if (err < 0) {
709 BT_ERR("%s corrupted packet: type %d count %d",
710 husb->hdev->name, _urb->type, count);
711 hdev->stat.err_rx++;
715 resubmit:
716 urb->dev = husb->udev;
717 err = usb_submit_urb(urb, GFP_ATOMIC);
718 BT_DBG("%s urb %p type %d resubmit status %d", hdev->name, urb,
719 _urb->type, err);
721 unlock:
722 read_unlock(&husb->completion_lock);
725 static void hci_usb_tx_complete(struct urb *urb)
727 struct _urb *_urb = container_of(urb, struct _urb, urb);
728 struct hci_usb *husb = (void *) urb->context;
729 struct hci_dev *hdev = husb->hdev;
731 BT_DBG("%s urb %p status %d flags %x", hdev->name, urb,
732 urb->status, urb->transfer_flags);
734 atomic_dec(__pending_tx(husb, _urb->type));
736 urb->transfer_buffer = NULL;
737 kfree_skb((struct sk_buff *) _urb->priv);
739 if (!test_bit(HCI_RUNNING, &hdev->flags))
740 return;
742 if (!urb->status)
743 hdev->stat.byte_tx += urb->transfer_buffer_length;
744 else
745 hdev->stat.err_tx++;
747 read_lock(&husb->completion_lock);
749 _urb_unlink(_urb);
750 _urb_queue_tail(__completed_q(husb, _urb->type), _urb);
752 hci_usb_tx_wakeup(husb);
754 read_unlock(&husb->completion_lock);
757 static void hci_usb_destruct(struct hci_dev *hdev)
759 struct hci_usb *husb = (struct hci_usb *) hdev->driver_data;
761 BT_DBG("%s", hdev->name);
763 kfree(husb);
766 static void hci_usb_notify(struct hci_dev *hdev, unsigned int evt)
768 BT_DBG("%s evt %d", hdev->name, evt);
771 static int hci_usb_probe(struct usb_interface *intf, const struct usb_device_id *id)
773 struct usb_device *udev = interface_to_usbdev(intf);
774 struct usb_host_endpoint *bulk_out_ep = NULL;
775 struct usb_host_endpoint *bulk_in_ep = NULL;
776 struct usb_host_endpoint *intr_in_ep = NULL;
777 struct usb_host_endpoint *ep;
778 struct usb_host_interface *uif;
779 struct usb_interface *isoc_iface;
780 struct hci_usb *husb;
781 struct hci_dev *hdev;
782 int i, e, size, isoc_ifnum, isoc_alts;
784 BT_DBG("udev %p intf %p", udev, intf);
786 if (!id->driver_info) {
787 const struct usb_device_id *match;
788 match = usb_match_id(intf, blacklist_ids);
789 if (match)
790 id = match;
793 if (ignore || id->driver_info & HCI_IGNORE)
794 return -ENODEV;
796 if (ignore_dga && id->driver_info & HCI_DIGIANSWER)
797 return -ENODEV;
799 if (ignore_csr && id->driver_info & HCI_CSR)
800 return -ENODEV;
802 if (ignore_sniffer && id->driver_info & HCI_SNIFFER)
803 return -ENODEV;
805 if (intf->cur_altsetting->desc.bInterfaceNumber > 0)
806 return -ENODEV;
808 /* Find endpoints that we need */
809 uif = intf->cur_altsetting;
810 for (e = 0; e < uif->desc.bNumEndpoints; e++) {
811 ep = &uif->endpoint[e];
813 switch (ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
814 case USB_ENDPOINT_XFER_INT:
815 if (ep->desc.bEndpointAddress & USB_DIR_IN)
816 intr_in_ep = ep;
817 break;
819 case USB_ENDPOINT_XFER_BULK:
820 if (ep->desc.bEndpointAddress & USB_DIR_IN)
821 bulk_in_ep = ep;
822 else
823 bulk_out_ep = ep;
824 break;
828 if (!bulk_in_ep || !bulk_out_ep || !intr_in_ep) {
829 BT_DBG("Bulk endpoints not found");
830 goto done;
833 if (!(husb = kzalloc(sizeof(struct hci_usb), GFP_KERNEL))) {
834 BT_ERR("Can't allocate: control structure");
835 goto done;
838 husb->udev = udev;
839 husb->bulk_out_ep = bulk_out_ep;
840 husb->bulk_in_ep = bulk_in_ep;
841 husb->intr_in_ep = intr_in_ep;
843 if (id->driver_info & HCI_DIGIANSWER)
844 husb->ctrl_req = USB_TYPE_VENDOR;
845 else
846 husb->ctrl_req = USB_TYPE_CLASS;
848 /* Find isochronous endpoints that we can use */
849 size = 0;
850 isoc_iface = NULL;
851 isoc_alts = 0;
852 isoc_ifnum = 1;
854 #ifdef CONFIG_BT_HCIUSB_SCO
855 if (isoc && !(id->driver_info & (HCI_BROKEN_ISOC | HCI_SNIFFER)))
856 isoc_iface = usb_ifnum_to_if(udev, isoc_ifnum);
858 if (isoc_iface) {
859 int a;
860 struct usb_host_endpoint *isoc_out_ep = NULL;
861 struct usb_host_endpoint *isoc_in_ep = NULL;
863 for (a = 0; a < isoc_iface->num_altsetting; a++) {
864 uif = &isoc_iface->altsetting[a];
865 for (e = 0; e < uif->desc.bNumEndpoints; e++) {
866 ep = &uif->endpoint[e];
868 switch (ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
869 case USB_ENDPOINT_XFER_ISOC:
870 if (le16_to_cpu(ep->desc.wMaxPacketSize) < size ||
871 uif->desc.bAlternateSetting != isoc)
872 break;
873 size = le16_to_cpu(ep->desc.wMaxPacketSize);
875 isoc_alts = uif->desc.bAlternateSetting;
877 if (ep->desc.bEndpointAddress & USB_DIR_IN)
878 isoc_in_ep = ep;
879 else
880 isoc_out_ep = ep;
881 break;
886 if (!isoc_in_ep || !isoc_out_ep)
887 BT_DBG("Isoc endpoints not found");
888 else {
889 BT_DBG("isoc ifnum %d alts %d", isoc_ifnum, isoc_alts);
890 if (usb_driver_claim_interface(&hci_usb_driver, isoc_iface, husb) != 0)
891 BT_ERR("Can't claim isoc interface");
892 else if (usb_set_interface(udev, isoc_ifnum, isoc_alts)) {
893 BT_ERR("Can't set isoc interface settings");
894 husb->isoc_iface = isoc_iface;
895 usb_driver_release_interface(&hci_usb_driver, isoc_iface);
896 husb->isoc_iface = NULL;
897 } else {
898 husb->isoc_iface = isoc_iface;
899 husb->isoc_in_ep = isoc_in_ep;
900 husb->isoc_out_ep = isoc_out_ep;
904 #endif
906 rwlock_init(&husb->completion_lock);
908 for (i = 0; i < 4; i++) {
909 skb_queue_head_init(&husb->transmit_q[i]);
910 _urb_queue_init(&husb->pending_q[i]);
911 _urb_queue_init(&husb->completed_q[i]);
914 /* Initialize and register HCI device */
915 hdev = hci_alloc_dev();
916 if (!hdev) {
917 BT_ERR("Can't allocate HCI device");
918 goto probe_error;
921 husb->hdev = hdev;
923 hdev->type = HCI_USB;
924 hdev->driver_data = husb;
925 SET_HCIDEV_DEV(hdev, &intf->dev);
927 hdev->open = hci_usb_open;
928 hdev->close = hci_usb_close;
929 hdev->flush = hci_usb_flush;
930 hdev->send = hci_usb_send_frame;
931 hdev->destruct = hci_usb_destruct;
932 hdev->notify = hci_usb_notify;
934 hdev->owner = THIS_MODULE;
936 if (reset || id->driver_info & HCI_RESET)
937 set_bit(HCI_QUIRK_RESET_ON_INIT, &hdev->quirks);
939 if (force_scofix || id->driver_info & HCI_WRONG_SCO_MTU) {
940 if (!disable_scofix)
941 set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
944 if (id->driver_info & HCI_SNIFFER) {
945 if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
946 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
949 if (id->driver_info & HCI_BCM92035) {
950 unsigned char cmd[] = { 0x3b, 0xfc, 0x01, 0x00 };
951 struct sk_buff *skb;
953 skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL);
954 if (skb) {
955 memcpy(skb_put(skb, sizeof(cmd)), cmd, sizeof(cmd));
956 skb_queue_tail(&hdev->driver_init, skb);
960 if (hci_register_dev(hdev) < 0) {
961 BT_ERR("Can't register HCI device");
962 hci_free_dev(hdev);
963 goto probe_error;
966 usb_set_intfdata(intf, husb);
967 return 0;
969 probe_error:
970 if (husb->isoc_iface)
971 usb_driver_release_interface(&hci_usb_driver, husb->isoc_iface);
972 kfree(husb);
974 done:
975 return -EIO;
978 static void hci_usb_disconnect(struct usb_interface *intf)
980 struct hci_usb *husb = usb_get_intfdata(intf);
981 struct hci_dev *hdev;
983 if (!husb || intf == husb->isoc_iface)
984 return;
986 usb_set_intfdata(intf, NULL);
987 hdev = husb->hdev;
989 BT_DBG("%s", hdev->name);
991 hci_usb_close(hdev);
993 if (husb->isoc_iface)
994 usb_driver_release_interface(&hci_usb_driver, husb->isoc_iface);
996 if (hci_unregister_dev(hdev) < 0)
997 BT_ERR("Can't unregister HCI device %s", hdev->name);
999 hci_free_dev(hdev);
1002 static int hci_usb_suspend(struct usb_interface *intf, pm_message_t message)
1004 struct hci_usb *husb = usb_get_intfdata(intf);
1005 struct list_head killed;
1006 unsigned long flags;
1007 int i;
1009 if (!husb || intf == husb->isoc_iface)
1010 return 0;
1012 hci_suspend_dev(husb->hdev);
1014 INIT_LIST_HEAD(&killed);
1016 for (i = 0; i < 4; i++) {
1017 struct _urb_queue *q = &husb->pending_q[i];
1018 struct _urb *_urb, *_tmp;
1020 while ((_urb = _urb_dequeue(q))) {
1021 /* reset queue since _urb_dequeue sets it to NULL */
1022 _urb->queue = q;
1023 usb_kill_urb(&_urb->urb);
1024 list_add(&_urb->list, &killed);
1027 spin_lock_irqsave(&q->lock, flags);
1029 list_for_each_entry_safe(_urb, _tmp, &killed, list) {
1030 list_move_tail(&_urb->list, &q->head);
1033 spin_unlock_irqrestore(&q->lock, flags);
1036 return 0;
1039 static int hci_usb_resume(struct usb_interface *intf)
1041 struct hci_usb *husb = usb_get_intfdata(intf);
1042 unsigned long flags;
1043 int i, err = 0;
1045 if (!husb || intf == husb->isoc_iface)
1046 return 0;
1048 for (i = 0; i < 4; i++) {
1049 struct _urb_queue *q = &husb->pending_q[i];
1050 struct _urb *_urb;
1052 spin_lock_irqsave(&q->lock, flags);
1054 list_for_each_entry(_urb, &q->head, list) {
1055 err = usb_submit_urb(&_urb->urb, GFP_ATOMIC);
1056 if (err)
1057 break;
1060 spin_unlock_irqrestore(&q->lock, flags);
1062 if (err)
1063 return -EIO;
1066 hci_resume_dev(husb->hdev);
1068 return 0;
1071 static struct usb_driver hci_usb_driver = {
1072 .name = "hci_usb",
1073 .probe = hci_usb_probe,
1074 .disconnect = hci_usb_disconnect,
1075 .suspend = hci_usb_suspend,
1076 .resume = hci_usb_resume,
1077 .id_table = bluetooth_ids,
1080 static int __init hci_usb_init(void)
1082 int err;
1084 BT_INFO("HCI USB driver ver %s", VERSION);
1086 if ((err = usb_register(&hci_usb_driver)) < 0)
1087 BT_ERR("Failed to register HCI USB driver");
1089 return err;
1092 static void __exit hci_usb_exit(void)
1094 usb_deregister(&hci_usb_driver);
1097 module_init(hci_usb_init);
1098 module_exit(hci_usb_exit);
1100 module_param(ignore, bool, 0644);
1101 MODULE_PARM_DESC(ignore, "Ignore devices from the matching table");
1103 module_param(ignore_dga, bool, 0644);
1104 MODULE_PARM_DESC(ignore_dga, "Ignore devices with id 08fd:0001");
1106 module_param(ignore_csr, bool, 0644);
1107 MODULE_PARM_DESC(ignore_csr, "Ignore devices with id 0a12:0001");
1109 module_param(ignore_sniffer, bool, 0644);
1110 MODULE_PARM_DESC(ignore_sniffer, "Ignore devices with id 0a12:0002");
1112 module_param(disable_scofix, bool, 0644);
1113 MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
1115 module_param(force_scofix, bool, 0644);
1116 MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
1118 module_param(reset, bool, 0644);
1119 MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
1121 #ifdef CONFIG_BT_HCIUSB_SCO
1122 module_param(isoc, int, 0644);
1123 MODULE_PARM_DESC(isoc, "Set isochronous transfers for SCO over HCI support");
1124 #endif
1126 MODULE_AUTHOR("Maxim Krasnyansky <maxk@qualcomm.com>, Marcel Holtmann <marcel@holtmann.org>");
1127 MODULE_DESCRIPTION("Bluetooth HCI USB driver ver " VERSION);
1128 MODULE_VERSION(VERSION);
1129 MODULE_LICENSE("GPL");