mm owner: fix race between swapoff and exit
[linux-2.6/next.git] / drivers / bluetooth / hci_usb.c
blob3c453924f8386c9b92050ae978967d188ec3b4da
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_dga;
66 static int ignore_csr;
67 static int ignore_sniffer;
68 static int disable_scofix;
69 static int force_scofix;
70 static int reset;
72 #ifdef CONFIG_BT_HCIUSB_SCO
73 static int isoc = 2;
74 #endif
76 #define VERSION "2.10"
78 static struct usb_driver hci_usb_driver;
80 static struct usb_device_id bluetooth_ids[] = {
81 /* Generic Bluetooth USB device */
82 { USB_DEVICE_INFO(HCI_DEV_CLASS, HCI_DEV_SUBCLASS, HCI_DEV_PROTOCOL) },
84 /* AVM BlueFRITZ! USB v2.0 */
85 { USB_DEVICE(0x057c, 0x3800) },
87 /* Bluetooth Ultraport Module from IBM */
88 { USB_DEVICE(0x04bf, 0x030a) },
90 /* ALPS Modules with non-standard id */
91 { USB_DEVICE(0x044e, 0x3001) },
92 { USB_DEVICE(0x044e, 0x3002) },
94 /* Ericsson with non-standard id */
95 { USB_DEVICE(0x0bdb, 0x1002) },
97 /* Canyon CN-BTU1 with HID interfaces */
98 { USB_DEVICE(0x0c10, 0x0000), .driver_info = HCI_RESET },
100 { } /* Terminating entry */
103 MODULE_DEVICE_TABLE (usb, bluetooth_ids);
105 static struct usb_device_id blacklist_ids[] = {
106 /* CSR BlueCore devices */
107 { USB_DEVICE(0x0a12, 0x0001), .driver_info = HCI_CSR },
109 /* Broadcom BCM2033 without firmware */
110 { USB_DEVICE(0x0a5c, 0x2033), .driver_info = HCI_IGNORE },
112 /* Broadcom BCM2035 */
113 { USB_DEVICE(0x0a5c, 0x2035), .driver_info = HCI_RESET | HCI_WRONG_SCO_MTU },
114 { USB_DEVICE(0x0a5c, 0x200a), .driver_info = HCI_RESET | HCI_WRONG_SCO_MTU },
115 { USB_DEVICE(0x0a5c, 0x2009), .driver_info = HCI_BCM92035 },
117 /* Broadcom BCM2045 */
118 { USB_DEVICE(0x0a5c, 0x2039), .driver_info = HCI_RESET | HCI_WRONG_SCO_MTU },
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 },
136 /* Dell Wireless 370 */
137 { USB_DEVICE(0x413c, 0x8156), .driver_info = HCI_RESET | HCI_WRONG_SCO_MTU },
138 /* Dell Wireless 410 */
139 { USB_DEVICE(0x413c, 0x8152), .driver_info = HCI_RESET | HCI_WRONG_SCO_MTU },
141 /* Broadcom 2046 */
142 { USB_DEVICE(0x0a5c, 0x2151), .driver_info = HCI_RESET },
144 /* Microsoft Wireless Transceiver for Bluetooth 2.0 */
145 { USB_DEVICE(0x045e, 0x009c), .driver_info = HCI_RESET },
147 /* Kensington Bluetooth USB adapter */
148 { USB_DEVICE(0x047d, 0x105d), .driver_info = HCI_RESET },
149 { USB_DEVICE(0x047d, 0x105e), .driver_info = HCI_RESET | HCI_WRONG_SCO_MTU },
151 /* ISSC Bluetooth Adapter v3.1 */
152 { USB_DEVICE(0x1131, 0x1001), .driver_info = HCI_RESET },
154 /* RTX Telecom based adapters with buggy SCO support */
155 { USB_DEVICE(0x0400, 0x0807), .driver_info = HCI_BROKEN_ISOC },
156 { USB_DEVICE(0x0400, 0x080a), .driver_info = HCI_BROKEN_ISOC },
158 /* CONWISE Technology based adapters with buggy SCO support */
159 { USB_DEVICE(0x0e5e, 0x6622), .driver_info = HCI_BROKEN_ISOC },
161 /* Belkin F8T012 and F8T013 devices */
162 { USB_DEVICE(0x050d, 0x0012), .driver_info = HCI_RESET | HCI_WRONG_SCO_MTU },
163 { USB_DEVICE(0x050d, 0x0013), .driver_info = HCI_RESET | HCI_WRONG_SCO_MTU },
165 /* Digianswer devices */
166 { USB_DEVICE(0x08fd, 0x0001), .driver_info = HCI_DIGIANSWER },
167 { USB_DEVICE(0x08fd, 0x0002), .driver_info = HCI_IGNORE },
169 /* CSR BlueCore Bluetooth Sniffer */
170 { USB_DEVICE(0x0a12, 0x0002), .driver_info = HCI_SNIFFER },
172 /* Frontline ComProbe Bluetooth Sniffer */
173 { USB_DEVICE(0x16d3, 0x0002), .driver_info = HCI_SNIFFER },
175 { } /* Terminating entry */
178 static struct _urb *_urb_alloc(int isoc, gfp_t gfp)
180 struct _urb *_urb = kmalloc(sizeof(struct _urb) +
181 sizeof(struct usb_iso_packet_descriptor) * isoc, gfp);
182 if (_urb) {
183 memset(_urb, 0, sizeof(*_urb));
184 usb_init_urb(&_urb->urb);
186 return _urb;
189 static struct _urb *_urb_dequeue(struct _urb_queue *q)
191 struct _urb *_urb = NULL;
192 unsigned long flags;
193 spin_lock_irqsave(&q->lock, flags);
195 struct list_head *head = &q->head;
196 struct list_head *next = head->next;
197 if (next != head) {
198 _urb = list_entry(next, struct _urb, list);
199 list_del(next); _urb->queue = NULL;
202 spin_unlock_irqrestore(&q->lock, flags);
203 return _urb;
206 static void hci_usb_rx_complete(struct urb *urb);
207 static void hci_usb_tx_complete(struct urb *urb);
209 #define __pending_tx(husb, type) (&husb->pending_tx[type-1])
210 #define __pending_q(husb, type) (&husb->pending_q[type-1])
211 #define __completed_q(husb, type) (&husb->completed_q[type-1])
212 #define __transmit_q(husb, type) (&husb->transmit_q[type-1])
214 static inline struct _urb *__get_completed(struct hci_usb *husb, int type)
216 return _urb_dequeue(__completed_q(husb, type));
219 #ifdef CONFIG_BT_HCIUSB_SCO
220 static void __fill_isoc_desc(struct urb *urb, int len, int mtu)
222 int offset = 0, i;
224 BT_DBG("len %d mtu %d", len, mtu);
226 for (i=0; i < HCI_MAX_ISOC_FRAMES && len >= mtu; i++, offset += mtu, len -= mtu) {
227 urb->iso_frame_desc[i].offset = offset;
228 urb->iso_frame_desc[i].length = mtu;
229 BT_DBG("desc %d offset %d len %d", i, offset, mtu);
231 if (len && i < HCI_MAX_ISOC_FRAMES) {
232 urb->iso_frame_desc[i].offset = offset;
233 urb->iso_frame_desc[i].length = len;
234 BT_DBG("desc %d offset %d len %d", i, offset, len);
235 i++;
237 urb->number_of_packets = i;
239 #endif
241 static int hci_usb_intr_rx_submit(struct hci_usb *husb)
243 struct _urb *_urb;
244 struct urb *urb;
245 int err, pipe, interval, size;
246 void *buf;
248 BT_DBG("%s", husb->hdev->name);
250 size = le16_to_cpu(husb->intr_in_ep->desc.wMaxPacketSize);
252 buf = kmalloc(size, GFP_ATOMIC);
253 if (!buf)
254 return -ENOMEM;
256 _urb = _urb_alloc(0, GFP_ATOMIC);
257 if (!_urb) {
258 kfree(buf);
259 return -ENOMEM;
261 _urb->type = HCI_EVENT_PKT;
262 _urb_queue_tail(__pending_q(husb, _urb->type), _urb);
264 urb = &_urb->urb;
265 pipe = usb_rcvintpipe(husb->udev, husb->intr_in_ep->desc.bEndpointAddress);
266 interval = husb->intr_in_ep->desc.bInterval;
267 usb_fill_int_urb(urb, husb->udev, pipe, buf, size, hci_usb_rx_complete, husb, interval);
269 err = usb_submit_urb(urb, GFP_ATOMIC);
270 if (err) {
271 BT_ERR("%s intr rx submit failed urb %p err %d",
272 husb->hdev->name, urb, err);
273 _urb_unlink(_urb);
274 kfree(_urb);
275 kfree(buf);
277 return err;
280 static int hci_usb_bulk_rx_submit(struct hci_usb *husb)
282 struct _urb *_urb;
283 struct urb *urb;
284 int err, pipe, size = HCI_MAX_FRAME_SIZE;
285 void *buf;
287 buf = kmalloc(size, GFP_ATOMIC);
288 if (!buf)
289 return -ENOMEM;
291 _urb = _urb_alloc(0, GFP_ATOMIC);
292 if (!_urb) {
293 kfree(buf);
294 return -ENOMEM;
296 _urb->type = HCI_ACLDATA_PKT;
297 _urb_queue_tail(__pending_q(husb, _urb->type), _urb);
299 urb = &_urb->urb;
300 pipe = usb_rcvbulkpipe(husb->udev, husb->bulk_in_ep->desc.bEndpointAddress);
301 usb_fill_bulk_urb(urb, husb->udev, pipe, buf, size, hci_usb_rx_complete, husb);
302 urb->transfer_flags = 0;
304 BT_DBG("%s urb %p", husb->hdev->name, urb);
306 err = usb_submit_urb(urb, GFP_ATOMIC);
307 if (err) {
308 BT_ERR("%s bulk rx submit failed urb %p err %d",
309 husb->hdev->name, urb, err);
310 _urb_unlink(_urb);
311 kfree(_urb);
312 kfree(buf);
314 return err;
317 #ifdef CONFIG_BT_HCIUSB_SCO
318 static int hci_usb_isoc_rx_submit(struct hci_usb *husb)
320 struct _urb *_urb;
321 struct urb *urb;
322 int err, mtu, size;
323 void *buf;
325 mtu = le16_to_cpu(husb->isoc_in_ep->desc.wMaxPacketSize);
326 size = mtu * HCI_MAX_ISOC_FRAMES;
328 buf = kmalloc(size, GFP_ATOMIC);
329 if (!buf)
330 return -ENOMEM;
332 _urb = _urb_alloc(HCI_MAX_ISOC_FRAMES, GFP_ATOMIC);
333 if (!_urb) {
334 kfree(buf);
335 return -ENOMEM;
337 _urb->type = HCI_SCODATA_PKT;
338 _urb_queue_tail(__pending_q(husb, _urb->type), _urb);
340 urb = &_urb->urb;
342 urb->context = husb;
343 urb->dev = husb->udev;
344 urb->pipe = usb_rcvisocpipe(husb->udev, husb->isoc_in_ep->desc.bEndpointAddress);
345 urb->complete = hci_usb_rx_complete;
347 urb->interval = husb->isoc_in_ep->desc.bInterval;
349 urb->transfer_buffer_length = size;
350 urb->transfer_buffer = buf;
351 urb->transfer_flags = URB_ISO_ASAP;
353 __fill_isoc_desc(urb, size, mtu);
355 BT_DBG("%s urb %p", husb->hdev->name, urb);
357 err = usb_submit_urb(urb, GFP_ATOMIC);
358 if (err) {
359 BT_ERR("%s isoc rx submit failed urb %p err %d",
360 husb->hdev->name, urb, err);
361 _urb_unlink(_urb);
362 kfree(_urb);
363 kfree(buf);
365 return err;
367 #endif
369 /* Initialize device */
370 static int hci_usb_open(struct hci_dev *hdev)
372 struct hci_usb *husb = (struct hci_usb *) hdev->driver_data;
373 int i, err;
374 unsigned long flags;
376 BT_DBG("%s", hdev->name);
378 if (test_and_set_bit(HCI_RUNNING, &hdev->flags))
379 return 0;
381 write_lock_irqsave(&husb->completion_lock, flags);
383 err = hci_usb_intr_rx_submit(husb);
384 if (!err) {
385 for (i = 0; i < HCI_MAX_BULK_RX; i++)
386 hci_usb_bulk_rx_submit(husb);
388 #ifdef CONFIG_BT_HCIUSB_SCO
389 if (husb->isoc_iface)
390 for (i = 0; i < HCI_MAX_ISOC_RX; i++)
391 hci_usb_isoc_rx_submit(husb);
392 #endif
393 } else {
394 clear_bit(HCI_RUNNING, &hdev->flags);
397 write_unlock_irqrestore(&husb->completion_lock, flags);
398 return err;
401 /* Reset device */
402 static int hci_usb_flush(struct hci_dev *hdev)
404 struct hci_usb *husb = (struct hci_usb *) hdev->driver_data;
405 int i;
407 BT_DBG("%s", hdev->name);
409 for (i = 0; i < 4; i++)
410 skb_queue_purge(&husb->transmit_q[i]);
411 return 0;
414 static void hci_usb_unlink_urbs(struct hci_usb *husb)
416 int i;
418 BT_DBG("%s", husb->hdev->name);
420 for (i = 0; i < 4; i++) {
421 struct _urb *_urb;
422 struct urb *urb;
424 /* Kill pending requests */
425 while ((_urb = _urb_dequeue(&husb->pending_q[i]))) {
426 urb = &_urb->urb;
427 BT_DBG("%s unlinking _urb %p type %d urb %p",
428 husb->hdev->name, _urb, _urb->type, urb);
429 usb_kill_urb(urb);
430 _urb_queue_tail(__completed_q(husb, _urb->type), _urb);
433 /* Release completed requests */
434 while ((_urb = _urb_dequeue(&husb->completed_q[i]))) {
435 urb = &_urb->urb;
436 BT_DBG("%s freeing _urb %p type %d urb %p",
437 husb->hdev->name, _urb, _urb->type, urb);
438 kfree(urb->setup_packet);
439 kfree(urb->transfer_buffer);
440 kfree(_urb);
445 /* Close device */
446 static int hci_usb_close(struct hci_dev *hdev)
448 struct hci_usb *husb = (struct hci_usb *) hdev->driver_data;
449 unsigned long flags;
451 if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
452 return 0;
454 BT_DBG("%s", hdev->name);
456 /* Synchronize with completion handlers */
457 write_lock_irqsave(&husb->completion_lock, flags);
458 write_unlock_irqrestore(&husb->completion_lock, flags);
460 hci_usb_unlink_urbs(husb);
461 hci_usb_flush(hdev);
462 return 0;
465 static int __tx_submit(struct hci_usb *husb, struct _urb *_urb)
467 struct urb *urb = &_urb->urb;
468 int err;
470 BT_DBG("%s urb %p type %d", husb->hdev->name, urb, _urb->type);
472 _urb_queue_tail(__pending_q(husb, _urb->type), _urb);
473 err = usb_submit_urb(urb, GFP_ATOMIC);
474 if (err) {
475 BT_ERR("%s tx submit failed urb %p type %d err %d",
476 husb->hdev->name, urb, _urb->type, err);
477 _urb_unlink(_urb);
478 _urb_queue_tail(__completed_q(husb, _urb->type), _urb);
479 } else
480 atomic_inc(__pending_tx(husb, _urb->type));
482 return err;
485 static inline int hci_usb_send_ctrl(struct hci_usb *husb, struct sk_buff *skb)
487 struct _urb *_urb = __get_completed(husb, bt_cb(skb)->pkt_type);
488 struct usb_ctrlrequest *dr;
489 struct urb *urb;
491 if (!_urb) {
492 _urb = _urb_alloc(0, GFP_ATOMIC);
493 if (!_urb)
494 return -ENOMEM;
495 _urb->type = bt_cb(skb)->pkt_type;
497 dr = kmalloc(sizeof(*dr), GFP_ATOMIC);
498 if (!dr) {
499 kfree(_urb);
500 return -ENOMEM;
502 } else
503 dr = (void *) _urb->urb.setup_packet;
505 dr->bRequestType = husb->ctrl_req;
506 dr->bRequest = 0;
507 dr->wIndex = 0;
508 dr->wValue = 0;
509 dr->wLength = __cpu_to_le16(skb->len);
511 urb = &_urb->urb;
512 usb_fill_control_urb(urb, husb->udev, usb_sndctrlpipe(husb->udev, 0),
513 (void *) dr, skb->data, skb->len, hci_usb_tx_complete, husb);
515 BT_DBG("%s skb %p len %d", husb->hdev->name, skb, skb->len);
517 _urb->priv = skb;
518 return __tx_submit(husb, _urb);
521 static inline int hci_usb_send_bulk(struct hci_usb *husb, struct sk_buff *skb)
523 struct _urb *_urb = __get_completed(husb, bt_cb(skb)->pkt_type);
524 struct urb *urb;
525 int pipe;
527 if (!_urb) {
528 _urb = _urb_alloc(0, GFP_ATOMIC);
529 if (!_urb)
530 return -ENOMEM;
531 _urb->type = bt_cb(skb)->pkt_type;
534 urb = &_urb->urb;
535 pipe = usb_sndbulkpipe(husb->udev, husb->bulk_out_ep->desc.bEndpointAddress);
536 usb_fill_bulk_urb(urb, husb->udev, pipe, skb->data, skb->len,
537 hci_usb_tx_complete, husb);
538 urb->transfer_flags = URB_ZERO_PACKET;
540 BT_DBG("%s skb %p len %d", husb->hdev->name, skb, skb->len);
542 _urb->priv = skb;
543 return __tx_submit(husb, _urb);
546 #ifdef CONFIG_BT_HCIUSB_SCO
547 static inline int hci_usb_send_isoc(struct hci_usb *husb, struct sk_buff *skb)
549 struct _urb *_urb = __get_completed(husb, bt_cb(skb)->pkt_type);
550 struct urb *urb;
552 if (!_urb) {
553 _urb = _urb_alloc(HCI_MAX_ISOC_FRAMES, GFP_ATOMIC);
554 if (!_urb)
555 return -ENOMEM;
556 _urb->type = bt_cb(skb)->pkt_type;
559 BT_DBG("%s skb %p len %d", husb->hdev->name, skb, skb->len);
561 urb = &_urb->urb;
563 urb->context = husb;
564 urb->dev = husb->udev;
565 urb->pipe = usb_sndisocpipe(husb->udev, husb->isoc_out_ep->desc.bEndpointAddress);
566 urb->complete = hci_usb_tx_complete;
567 urb->transfer_flags = URB_ISO_ASAP;
569 urb->interval = husb->isoc_out_ep->desc.bInterval;
571 urb->transfer_buffer = skb->data;
572 urb->transfer_buffer_length = skb->len;
574 __fill_isoc_desc(urb, skb->len, le16_to_cpu(husb->isoc_out_ep->desc.wMaxPacketSize));
576 _urb->priv = skb;
577 return __tx_submit(husb, _urb);
579 #endif
581 static void hci_usb_tx_process(struct hci_usb *husb)
583 struct sk_buff_head *q;
584 struct sk_buff *skb;
586 BT_DBG("%s", husb->hdev->name);
588 do {
589 clear_bit(HCI_USB_TX_WAKEUP, &husb->state);
591 /* Process command queue */
592 q = __transmit_q(husb, HCI_COMMAND_PKT);
593 if (!atomic_read(__pending_tx(husb, HCI_COMMAND_PKT)) &&
594 (skb = skb_dequeue(q))) {
595 if (hci_usb_send_ctrl(husb, skb) < 0)
596 skb_queue_head(q, skb);
599 #ifdef CONFIG_BT_HCIUSB_SCO
600 /* Process SCO queue */
601 q = __transmit_q(husb, HCI_SCODATA_PKT);
602 if (atomic_read(__pending_tx(husb, HCI_SCODATA_PKT)) < HCI_MAX_ISOC_TX &&
603 (skb = skb_dequeue(q))) {
604 if (hci_usb_send_isoc(husb, skb) < 0)
605 skb_queue_head(q, skb);
607 #endif
609 /* Process ACL queue */
610 q = __transmit_q(husb, HCI_ACLDATA_PKT);
611 while (atomic_read(__pending_tx(husb, HCI_ACLDATA_PKT)) < HCI_MAX_BULK_TX &&
612 (skb = skb_dequeue(q))) {
613 if (hci_usb_send_bulk(husb, skb) < 0) {
614 skb_queue_head(q, skb);
615 break;
618 } while(test_bit(HCI_USB_TX_WAKEUP, &husb->state));
621 static inline void hci_usb_tx_wakeup(struct hci_usb *husb)
623 /* Serialize TX queue processing to avoid data reordering */
624 if (!test_and_set_bit(HCI_USB_TX_PROCESS, &husb->state)) {
625 hci_usb_tx_process(husb);
626 clear_bit(HCI_USB_TX_PROCESS, &husb->state);
627 } else
628 set_bit(HCI_USB_TX_WAKEUP, &husb->state);
631 /* Send frames from HCI layer */
632 static int hci_usb_send_frame(struct sk_buff *skb)
634 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
635 struct hci_usb *husb;
637 if (!hdev) {
638 BT_ERR("frame for uknown device (hdev=NULL)");
639 return -ENODEV;
642 if (!test_bit(HCI_RUNNING, &hdev->flags))
643 return -EBUSY;
645 BT_DBG("%s type %d len %d", hdev->name, bt_cb(skb)->pkt_type, skb->len);
647 husb = (struct hci_usb *) hdev->driver_data;
649 switch (bt_cb(skb)->pkt_type) {
650 case HCI_COMMAND_PKT:
651 hdev->stat.cmd_tx++;
652 break;
654 case HCI_ACLDATA_PKT:
655 hdev->stat.acl_tx++;
656 break;
658 #ifdef CONFIG_BT_HCIUSB_SCO
659 case HCI_SCODATA_PKT:
660 hdev->stat.sco_tx++;
661 break;
662 #endif
664 default:
665 kfree_skb(skb);
666 return 0;
669 read_lock(&husb->completion_lock);
671 skb_queue_tail(__transmit_q(husb, bt_cb(skb)->pkt_type), skb);
672 hci_usb_tx_wakeup(husb);
674 read_unlock(&husb->completion_lock);
675 return 0;
678 static void hci_usb_rx_complete(struct urb *urb)
680 struct _urb *_urb = container_of(urb, struct _urb, urb);
681 struct hci_usb *husb = (void *) urb->context;
682 struct hci_dev *hdev = husb->hdev;
683 int err, count = urb->actual_length;
685 BT_DBG("%s urb %p type %d status %d count %d flags %x", hdev->name, urb,
686 _urb->type, urb->status, count, urb->transfer_flags);
688 read_lock(&husb->completion_lock);
690 if (!test_bit(HCI_RUNNING, &hdev->flags))
691 goto unlock;
693 if (urb->status || !count)
694 goto resubmit;
696 if (_urb->type == HCI_SCODATA_PKT) {
697 #ifdef CONFIG_BT_HCIUSB_SCO
698 int i;
699 for (i=0; i < urb->number_of_packets; i++) {
700 BT_DBG("desc %d status %d offset %d len %d", i,
701 urb->iso_frame_desc[i].status,
702 urb->iso_frame_desc[i].offset,
703 urb->iso_frame_desc[i].actual_length);
705 if (!urb->iso_frame_desc[i].status) {
706 husb->hdev->stat.byte_rx += urb->iso_frame_desc[i].actual_length;
707 hci_recv_fragment(husb->hdev, _urb->type,
708 urb->transfer_buffer + urb->iso_frame_desc[i].offset,
709 urb->iso_frame_desc[i].actual_length);
712 #else
714 #endif
715 } else {
716 husb->hdev->stat.byte_rx += count;
717 err = hci_recv_fragment(husb->hdev, _urb->type, urb->transfer_buffer, count);
718 if (err < 0) {
719 BT_ERR("%s corrupted packet: type %d count %d",
720 husb->hdev->name, _urb->type, count);
721 hdev->stat.err_rx++;
725 resubmit:
726 urb->dev = husb->udev;
727 err = usb_submit_urb(urb, GFP_ATOMIC);
728 BT_DBG("%s urb %p type %d resubmit status %d", hdev->name, urb,
729 _urb->type, err);
731 unlock:
732 read_unlock(&husb->completion_lock);
735 static void hci_usb_tx_complete(struct urb *urb)
737 struct _urb *_urb = container_of(urb, struct _urb, urb);
738 struct hci_usb *husb = (void *) urb->context;
739 struct hci_dev *hdev = husb->hdev;
741 BT_DBG("%s urb %p status %d flags %x", hdev->name, urb,
742 urb->status, urb->transfer_flags);
744 atomic_dec(__pending_tx(husb, _urb->type));
746 urb->transfer_buffer = NULL;
747 kfree_skb((struct sk_buff *) _urb->priv);
749 if (!test_bit(HCI_RUNNING, &hdev->flags))
750 return;
752 if (!urb->status)
753 hdev->stat.byte_tx += urb->transfer_buffer_length;
754 else
755 hdev->stat.err_tx++;
757 read_lock(&husb->completion_lock);
759 _urb_unlink(_urb);
760 _urb_queue_tail(__completed_q(husb, _urb->type), _urb);
762 hci_usb_tx_wakeup(husb);
764 read_unlock(&husb->completion_lock);
767 static void hci_usb_destruct(struct hci_dev *hdev)
769 struct hci_usb *husb = (struct hci_usb *) hdev->driver_data;
771 BT_DBG("%s", hdev->name);
773 kfree(husb);
776 static void hci_usb_notify(struct hci_dev *hdev, unsigned int evt)
778 BT_DBG("%s evt %d", hdev->name, evt);
781 static int hci_usb_probe(struct usb_interface *intf, const struct usb_device_id *id)
783 struct usb_device *udev = interface_to_usbdev(intf);
784 struct usb_host_endpoint *bulk_out_ep = NULL;
785 struct usb_host_endpoint *bulk_in_ep = NULL;
786 struct usb_host_endpoint *intr_in_ep = NULL;
787 struct usb_host_endpoint *ep;
788 struct usb_host_interface *uif;
789 struct usb_interface *isoc_iface;
790 struct hci_usb *husb;
791 struct hci_dev *hdev;
792 int i, e, size, isoc_ifnum, isoc_alts;
794 BT_DBG("udev %p intf %p", udev, intf);
796 if (!id->driver_info) {
797 const struct usb_device_id *match;
798 match = usb_match_id(intf, blacklist_ids);
799 if (match)
800 id = match;
803 if (id->driver_info & HCI_IGNORE)
804 return -ENODEV;
806 if (ignore_dga && id->driver_info & HCI_DIGIANSWER)
807 return -ENODEV;
809 if (ignore_csr && id->driver_info & HCI_CSR)
810 return -ENODEV;
812 if (ignore_sniffer && id->driver_info & HCI_SNIFFER)
813 return -ENODEV;
815 if (intf->cur_altsetting->desc.bInterfaceNumber > 0)
816 return -ENODEV;
818 /* Find endpoints that we need */
819 uif = intf->cur_altsetting;
820 for (e = 0; e < uif->desc.bNumEndpoints; e++) {
821 ep = &uif->endpoint[e];
823 switch (ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
824 case USB_ENDPOINT_XFER_INT:
825 if (ep->desc.bEndpointAddress & USB_DIR_IN)
826 intr_in_ep = ep;
827 break;
829 case USB_ENDPOINT_XFER_BULK:
830 if (ep->desc.bEndpointAddress & USB_DIR_IN)
831 bulk_in_ep = ep;
832 else
833 bulk_out_ep = ep;
834 break;
838 if (!bulk_in_ep || !bulk_out_ep || !intr_in_ep) {
839 BT_DBG("Bulk endpoints not found");
840 goto done;
843 if (!(husb = kzalloc(sizeof(struct hci_usb), GFP_KERNEL))) {
844 BT_ERR("Can't allocate: control structure");
845 goto done;
848 husb->udev = udev;
849 husb->bulk_out_ep = bulk_out_ep;
850 husb->bulk_in_ep = bulk_in_ep;
851 husb->intr_in_ep = intr_in_ep;
853 if (id->driver_info & HCI_DIGIANSWER)
854 husb->ctrl_req = USB_TYPE_VENDOR;
855 else
856 husb->ctrl_req = USB_TYPE_CLASS;
858 /* Find isochronous endpoints that we can use */
859 size = 0;
860 isoc_iface = NULL;
861 isoc_alts = 0;
862 isoc_ifnum = 1;
864 #ifdef CONFIG_BT_HCIUSB_SCO
865 if (isoc && !(id->driver_info & (HCI_BROKEN_ISOC | HCI_SNIFFER)))
866 isoc_iface = usb_ifnum_to_if(udev, isoc_ifnum);
868 if (isoc_iface) {
869 int a;
870 struct usb_host_endpoint *isoc_out_ep = NULL;
871 struct usb_host_endpoint *isoc_in_ep = NULL;
873 for (a = 0; a < isoc_iface->num_altsetting; a++) {
874 uif = &isoc_iface->altsetting[a];
875 for (e = 0; e < uif->desc.bNumEndpoints; e++) {
876 ep = &uif->endpoint[e];
878 switch (ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
879 case USB_ENDPOINT_XFER_ISOC:
880 if (le16_to_cpu(ep->desc.wMaxPacketSize) < size ||
881 uif->desc.bAlternateSetting != isoc)
882 break;
883 size = le16_to_cpu(ep->desc.wMaxPacketSize);
885 isoc_alts = uif->desc.bAlternateSetting;
887 if (ep->desc.bEndpointAddress & USB_DIR_IN)
888 isoc_in_ep = ep;
889 else
890 isoc_out_ep = ep;
891 break;
896 if (!isoc_in_ep || !isoc_out_ep)
897 BT_DBG("Isoc endpoints not found");
898 else {
899 BT_DBG("isoc ifnum %d alts %d", isoc_ifnum, isoc_alts);
900 if (usb_driver_claim_interface(&hci_usb_driver, isoc_iface, husb) != 0)
901 BT_ERR("Can't claim isoc interface");
902 else if (usb_set_interface(udev, isoc_ifnum, isoc_alts)) {
903 BT_ERR("Can't set isoc interface settings");
904 husb->isoc_iface = isoc_iface;
905 usb_driver_release_interface(&hci_usb_driver, isoc_iface);
906 husb->isoc_iface = NULL;
907 } else {
908 husb->isoc_iface = isoc_iface;
909 husb->isoc_in_ep = isoc_in_ep;
910 husb->isoc_out_ep = isoc_out_ep;
914 #endif
916 rwlock_init(&husb->completion_lock);
918 for (i = 0; i < 4; i++) {
919 skb_queue_head_init(&husb->transmit_q[i]);
920 _urb_queue_init(&husb->pending_q[i]);
921 _urb_queue_init(&husb->completed_q[i]);
924 /* Initialize and register HCI device */
925 hdev = hci_alloc_dev();
926 if (!hdev) {
927 BT_ERR("Can't allocate HCI device");
928 goto probe_error;
931 husb->hdev = hdev;
933 hdev->type = HCI_USB;
934 hdev->driver_data = husb;
935 SET_HCIDEV_DEV(hdev, &intf->dev);
937 hdev->open = hci_usb_open;
938 hdev->close = hci_usb_close;
939 hdev->flush = hci_usb_flush;
940 hdev->send = hci_usb_send_frame;
941 hdev->destruct = hci_usb_destruct;
942 hdev->notify = hci_usb_notify;
944 hdev->owner = THIS_MODULE;
946 if (reset || id->driver_info & HCI_RESET)
947 set_bit(HCI_QUIRK_RESET_ON_INIT, &hdev->quirks);
949 if (force_scofix || id->driver_info & HCI_WRONG_SCO_MTU) {
950 if (!disable_scofix)
951 set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
954 if (id->driver_info & HCI_SNIFFER) {
955 if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
956 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
959 if (id->driver_info & HCI_BCM92035) {
960 unsigned char cmd[] = { 0x3b, 0xfc, 0x01, 0x00 };
961 struct sk_buff *skb;
963 skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL);
964 if (skb) {
965 memcpy(skb_put(skb, sizeof(cmd)), cmd, sizeof(cmd));
966 skb_queue_tail(&hdev->driver_init, skb);
970 if (hci_register_dev(hdev) < 0) {
971 BT_ERR("Can't register HCI device");
972 hci_free_dev(hdev);
973 goto probe_error;
976 usb_set_intfdata(intf, husb);
977 return 0;
979 probe_error:
980 if (husb->isoc_iface)
981 usb_driver_release_interface(&hci_usb_driver, husb->isoc_iface);
982 kfree(husb);
984 done:
985 return -EIO;
988 static void hci_usb_disconnect(struct usb_interface *intf)
990 struct hci_usb *husb = usb_get_intfdata(intf);
991 struct hci_dev *hdev;
993 if (!husb || intf == husb->isoc_iface)
994 return;
996 usb_set_intfdata(intf, NULL);
997 hdev = husb->hdev;
999 BT_DBG("%s", hdev->name);
1001 hci_usb_close(hdev);
1003 if (husb->isoc_iface)
1004 usb_driver_release_interface(&hci_usb_driver, husb->isoc_iface);
1006 if (hci_unregister_dev(hdev) < 0)
1007 BT_ERR("Can't unregister HCI device %s", hdev->name);
1009 hci_free_dev(hdev);
1012 static int hci_usb_suspend(struct usb_interface *intf, pm_message_t message)
1014 struct hci_usb *husb = usb_get_intfdata(intf);
1015 struct list_head killed;
1016 unsigned long flags;
1017 int i;
1019 if (!husb || intf == husb->isoc_iface)
1020 return 0;
1022 hci_suspend_dev(husb->hdev);
1024 INIT_LIST_HEAD(&killed);
1026 for (i = 0; i < 4; i++) {
1027 struct _urb_queue *q = &husb->pending_q[i];
1028 struct _urb *_urb, *_tmp;
1030 while ((_urb = _urb_dequeue(q))) {
1031 /* reset queue since _urb_dequeue sets it to NULL */
1032 _urb->queue = q;
1033 usb_kill_urb(&_urb->urb);
1034 list_add(&_urb->list, &killed);
1037 spin_lock_irqsave(&q->lock, flags);
1039 list_for_each_entry_safe(_urb, _tmp, &killed, list) {
1040 list_move_tail(&_urb->list, &q->head);
1043 spin_unlock_irqrestore(&q->lock, flags);
1046 return 0;
1049 static int hci_usb_resume(struct usb_interface *intf)
1051 struct hci_usb *husb = usb_get_intfdata(intf);
1052 unsigned long flags;
1053 int i, err = 0;
1055 if (!husb || intf == husb->isoc_iface)
1056 return 0;
1058 for (i = 0; i < 4; i++) {
1059 struct _urb_queue *q = &husb->pending_q[i];
1060 struct _urb *_urb;
1062 spin_lock_irqsave(&q->lock, flags);
1064 list_for_each_entry(_urb, &q->head, list) {
1065 err = usb_submit_urb(&_urb->urb, GFP_ATOMIC);
1066 if (err)
1067 break;
1070 spin_unlock_irqrestore(&q->lock, flags);
1072 if (err)
1073 return -EIO;
1076 hci_resume_dev(husb->hdev);
1078 return 0;
1081 static struct usb_driver hci_usb_driver = {
1082 .name = "hci_usb",
1083 .probe = hci_usb_probe,
1084 .disconnect = hci_usb_disconnect,
1085 .suspend = hci_usb_suspend,
1086 .resume = hci_usb_resume,
1087 .id_table = bluetooth_ids,
1090 static int __init hci_usb_init(void)
1092 int err;
1094 BT_INFO("HCI USB driver ver %s", VERSION);
1096 if ((err = usb_register(&hci_usb_driver)) < 0)
1097 BT_ERR("Failed to register HCI USB driver");
1099 return err;
1102 static void __exit hci_usb_exit(void)
1104 usb_deregister(&hci_usb_driver);
1107 module_init(hci_usb_init);
1108 module_exit(hci_usb_exit);
1110 module_param(ignore_dga, bool, 0644);
1111 MODULE_PARM_DESC(ignore_dga, "Ignore devices with id 08fd:0001");
1113 module_param(ignore_csr, bool, 0644);
1114 MODULE_PARM_DESC(ignore_csr, "Ignore devices with id 0a12:0001");
1116 module_param(ignore_sniffer, bool, 0644);
1117 MODULE_PARM_DESC(ignore_sniffer, "Ignore devices with id 0a12:0002");
1119 module_param(disable_scofix, bool, 0644);
1120 MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
1122 module_param(force_scofix, bool, 0644);
1123 MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
1125 module_param(reset, bool, 0644);
1126 MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
1128 #ifdef CONFIG_BT_HCIUSB_SCO
1129 module_param(isoc, int, 0644);
1130 MODULE_PARM_DESC(isoc, "Set isochronous transfers for SCO over HCI support");
1131 #endif
1133 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
1134 MODULE_DESCRIPTION("Bluetooth HCI USB driver ver " VERSION);
1135 MODULE_VERSION(VERSION);
1136 MODULE_LICENSE("GPL");