3 * Generic Bluetooth USB driver
5 * Copyright (C) 2005-2007 Marcel Holtmann <marcel@holtmann.org>
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.
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.
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
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>
33 #include <linux/usb.h>
35 #include <net/bluetooth/bluetooth.h>
36 #include <net/bluetooth/hci_core.h>
38 //#define CONFIG_BT_HCIBTUSB_DEBUG
39 #ifndef CONFIG_BT_HCIBTUSB_DEBUG
46 static struct usb_device_id btusb_table[] = {
47 /* Generic Bluetooth USB device */
48 { USB_DEVICE_INFO(0xe0, 0x01, 0x01) },
50 { } /* Terminating entry */
53 MODULE_DEVICE_TABLE(usb, btusb_table);
55 static struct usb_device_id blacklist_table[] = {
56 { } /* Terminating entry */
59 #define BTUSB_INTR_RUNNING 0
60 #define BTUSB_BULK_RUNNING 1
64 struct usb_device *udev;
70 struct work_struct work;
72 struct usb_anchor tx_anchor;
73 struct usb_anchor intr_anchor;
74 struct usb_anchor bulk_anchor;
76 struct usb_endpoint_descriptor *intr_ep;
77 struct usb_endpoint_descriptor *bulk_tx_ep;
78 struct usb_endpoint_descriptor *bulk_rx_ep;
81 static void btusb_intr_complete(struct urb *urb)
83 struct hci_dev *hdev = urb->context;
84 struct btusb_data *data = hdev->driver_data;
87 BT_DBG("%s urb %p status %d count %d", hdev->name,
88 urb, urb->status, urb->actual_length);
90 if (!test_bit(HCI_RUNNING, &hdev->flags))
93 if (urb->status == 0) {
94 if (hci_recv_fragment(hdev, HCI_EVENT_PKT,
96 urb->actual_length) < 0) {
97 BT_ERR("%s corrupted event packet", hdev->name);
102 if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
105 usb_anchor_urb(urb, &data->intr_anchor);
107 err = usb_submit_urb(urb, GFP_ATOMIC);
109 BT_ERR("%s urb %p failed to resubmit (%d)",
110 hdev->name, urb, -err);
111 usb_unanchor_urb(urb);
115 static inline int btusb_submit_intr_urb(struct hci_dev *hdev)
117 struct btusb_data *data = hdev->driver_data;
123 BT_DBG("%s", hdev->name);
125 urb = usb_alloc_urb(0, GFP_ATOMIC);
129 size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
131 buf = kmalloc(size, GFP_ATOMIC);
137 pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
139 usb_fill_int_urb(urb, data->udev, pipe, buf, size,
140 btusb_intr_complete, hdev,
141 data->intr_ep->bInterval);
143 urb->transfer_flags |= URB_FREE_BUFFER;
145 usb_anchor_urb(urb, &data->intr_anchor);
147 err = usb_submit_urb(urb, GFP_ATOMIC);
149 BT_ERR("%s urb %p submission failed (%d)",
150 hdev->name, urb, -err);
151 usb_unanchor_urb(urb);
160 static void btusb_bulk_complete(struct urb *urb)
162 struct hci_dev *hdev = urb->context;
163 struct btusb_data *data = hdev->driver_data;
166 BT_DBG("%s urb %p status %d count %d", hdev->name,
167 urb, urb->status, urb->actual_length);
169 if (!test_bit(HCI_RUNNING, &hdev->flags))
172 if (urb->status == 0) {
173 if (hci_recv_fragment(hdev, HCI_ACLDATA_PKT,
174 urb->transfer_buffer,
175 urb->actual_length) < 0) {
176 BT_ERR("%s corrupted ACL packet", hdev->name);
181 if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
184 usb_anchor_urb(urb, &data->bulk_anchor);
186 err = usb_submit_urb(urb, GFP_ATOMIC);
188 BT_ERR("%s urb %p failed to resubmit (%d)",
189 hdev->name, urb, -err);
190 usb_unanchor_urb(urb);
194 static inline int btusb_submit_bulk_urb(struct hci_dev *hdev)
196 struct btusb_data *data = hdev->driver_data;
202 BT_DBG("%s", hdev->name);
204 urb = usb_alloc_urb(0, GFP_KERNEL);
208 size = le16_to_cpu(data->bulk_rx_ep->wMaxPacketSize);
210 buf = kmalloc(size, GFP_KERNEL);
216 pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
218 usb_fill_bulk_urb(urb, data->udev, pipe,
219 buf, size, btusb_bulk_complete, hdev);
221 urb->transfer_flags |= URB_FREE_BUFFER;
223 usb_anchor_urb(urb, &data->bulk_anchor);
225 err = usb_submit_urb(urb, GFP_KERNEL);
227 BT_ERR("%s urb %p submission failed (%d)",
228 hdev->name, urb, -err);
229 usb_unanchor_urb(urb);
238 static void btusb_tx_complete(struct urb *urb)
240 struct sk_buff *skb = urb->context;
241 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
243 BT_DBG("%s urb %p status %d count %d", hdev->name,
244 urb, urb->status, urb->actual_length);
246 if (!test_bit(HCI_RUNNING, &hdev->flags))
250 hdev->stat.byte_tx += urb->transfer_buffer_length;
255 kfree(urb->setup_packet);
260 static int btusb_open(struct hci_dev *hdev)
262 struct btusb_data *data = hdev->driver_data;
265 BT_DBG("%s", hdev->name);
267 if (test_and_set_bit(HCI_RUNNING, &hdev->flags))
270 if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
273 err = btusb_submit_intr_urb(hdev);
275 clear_bit(BTUSB_INTR_RUNNING, &hdev->flags);
276 clear_bit(HCI_RUNNING, &hdev->flags);
282 static int btusb_close(struct hci_dev *hdev)
284 struct btusb_data *data = hdev->driver_data;
286 BT_DBG("%s", hdev->name);
288 if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
291 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
292 usb_kill_anchored_urbs(&data->bulk_anchor);
294 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
295 usb_kill_anchored_urbs(&data->intr_anchor);
300 static int btusb_flush(struct hci_dev *hdev)
302 struct btusb_data *data = hdev->driver_data;
304 BT_DBG("%s", hdev->name);
306 usb_kill_anchored_urbs(&data->tx_anchor);
311 static int btusb_send_frame(struct sk_buff *skb)
313 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
314 struct btusb_data *data = hdev->driver_data;
315 struct usb_ctrlrequest *dr;
320 BT_DBG("%s", hdev->name);
322 if (!test_bit(HCI_RUNNING, &hdev->flags))
325 switch (bt_cb(skb)->pkt_type) {
326 case HCI_COMMAND_PKT:
327 urb = usb_alloc_urb(0, GFP_ATOMIC);
331 dr = kmalloc(sizeof(*dr), GFP_ATOMIC);
337 dr->bRequestType = USB_TYPE_CLASS;
341 dr->wLength = __cpu_to_le16(skb->len);
343 pipe = usb_sndctrlpipe(data->udev, 0x00);
345 usb_fill_control_urb(urb, data->udev, pipe, (void *) dr,
346 skb->data, skb->len, btusb_tx_complete, skb);
351 case HCI_ACLDATA_PKT:
352 urb = usb_alloc_urb(0, GFP_ATOMIC);
356 pipe = usb_sndbulkpipe(data->udev,
357 data->bulk_tx_ep->bEndpointAddress);
359 usb_fill_bulk_urb(urb, data->udev, pipe,
360 skb->data, skb->len, btusb_tx_complete, skb);
365 case HCI_SCODATA_PKT:
374 usb_anchor_urb(urb, &data->tx_anchor);
376 err = usb_submit_urb(urb, GFP_ATOMIC);
378 BT_ERR("%s urb %p submission failed", hdev->name, urb);
379 kfree(urb->setup_packet);
380 usb_unanchor_urb(urb);
388 static void btusb_destruct(struct hci_dev *hdev)
390 struct btusb_data *data = hdev->driver_data;
392 BT_DBG("%s", hdev->name);
397 static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
399 struct btusb_data *data = hdev->driver_data;
401 BT_DBG("%s evt %d", hdev->name, evt);
403 if (evt == HCI_NOTIFY_CONN_ADD || evt == HCI_NOTIFY_CONN_DEL)
404 schedule_work(&data->work);
407 static void btusb_work(struct work_struct *work)
409 struct btusb_data *data = container_of(work, struct btusb_data, work);
410 struct hci_dev *hdev = data->hdev;
412 if (hdev->conn_hash.acl_num == 0) {
413 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
414 usb_kill_anchored_urbs(&data->bulk_anchor);
418 if (!test_and_set_bit(BTUSB_BULK_RUNNING, &data->flags)) {
419 if (btusb_submit_bulk_urb(hdev) < 0)
420 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
422 btusb_submit_bulk_urb(hdev);
426 static int btusb_probe(struct usb_interface *intf,
427 const struct usb_device_id *id)
429 struct usb_endpoint_descriptor *ep_desc;
430 struct btusb_data *data;
431 struct hci_dev *hdev;
434 BT_DBG("intf %p id %p", intf, id);
436 if (intf->cur_altsetting->desc.bInterfaceNumber != 0)
439 if (!id->driver_info) {
440 const struct usb_device_id *match;
441 match = usb_match_id(intf, blacklist_table);
446 data = kzalloc(sizeof(*data), GFP_KERNEL);
450 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
451 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
453 if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
454 data->intr_ep = ep_desc;
458 if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
459 data->bulk_tx_ep = ep_desc;
463 if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
464 data->bulk_rx_ep = ep_desc;
469 if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep) {
474 data->udev = interface_to_usbdev(intf);
476 spin_lock_init(&data->lock);
478 INIT_WORK(&data->work, btusb_work);
480 init_usb_anchor(&data->tx_anchor);
481 init_usb_anchor(&data->intr_anchor);
482 init_usb_anchor(&data->bulk_anchor);
484 hdev = hci_alloc_dev();
490 hdev->type = HCI_USB;
491 hdev->driver_data = data;
495 SET_HCIDEV_DEV(hdev, &intf->dev);
497 hdev->open = btusb_open;
498 hdev->close = btusb_close;
499 hdev->flush = btusb_flush;
500 hdev->send = btusb_send_frame;
501 hdev->destruct = btusb_destruct;
502 hdev->notify = btusb_notify;
504 hdev->owner = THIS_MODULE;
506 set_bit(HCI_QUIRK_RESET_ON_INIT, &hdev->quirks);
508 err = hci_register_dev(hdev);
515 usb_set_intfdata(intf, data);
520 static void btusb_disconnect(struct usb_interface *intf)
522 struct btusb_data *data = usb_get_intfdata(intf);
523 struct hci_dev *hdev;
525 BT_DBG("intf %p", intf);
532 usb_set_intfdata(intf, NULL);
534 hci_unregister_dev(hdev);
539 static struct usb_driver btusb_driver = {
541 .probe = btusb_probe,
542 .disconnect = btusb_disconnect,
543 .id_table = btusb_table,
546 static int __init btusb_init(void)
548 BT_INFO("Generic Bluetooth USB driver ver %s", VERSION);
550 return usb_register(&btusb_driver);
553 static void __exit btusb_exit(void)
555 usb_deregister(&btusb_driver);
558 module_init(btusb_init);
559 module_exit(btusb_exit);
561 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
562 MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
563 MODULE_VERSION(VERSION);
564 MODULE_LICENSE("GPL");