Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
| 5 | |
| 6 | Copyright (C) 2003 Maxim Krasnyansky <maxk@qualcomm.com> |
| 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 version 2 as |
| 10 | published by the Free Software Foundation; |
| 11 | |
| 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. |
| 20 | |
| 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. |
| 24 | */ |
| 25 | |
| 26 | /* |
| 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> |
| 31 | * |
| 32 | */ |
| 33 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #include <linux/module.h> |
| 35 | |
| 36 | #include <linux/kernel.h> |
| 37 | #include <linux/init.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 38 | #include <linux/unistd.h> |
| 39 | #include <linux/types.h> |
| 40 | #include <linux/interrupt.h> |
| 41 | #include <linux/moduleparam.h> |
| 42 | |
| 43 | #include <linux/slab.h> |
| 44 | #include <linux/errno.h> |
| 45 | #include <linux/string.h> |
| 46 | #include <linux/skbuff.h> |
| 47 | |
| 48 | #include <linux/usb.h> |
| 49 | |
| 50 | #include <net/bluetooth/bluetooth.h> |
| 51 | #include <net/bluetooth/hci_core.h> |
| 52 | |
| 53 | #include "hci_usb.h" |
| 54 | |
| 55 | #ifndef CONFIG_BT_HCIUSB_DEBUG |
| 56 | #undef BT_DBG |
| 57 | #define BT_DBG(D...) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | #endif |
| 59 | |
| 60 | #ifndef CONFIG_BT_HCIUSB_ZERO_PACKET |
| 61 | #undef URB_ZERO_PACKET |
| 62 | #define URB_ZERO_PACKET 0 |
| 63 | #endif |
| 64 | |
Pavel Machek | a882711 | 2008-04-19 18:13:40 -0700 | [diff] [blame] | 65 | static int ignore; |
| 66 | static int ignore_dga; |
| 67 | static int ignore_csr; |
| 68 | static int ignore_sniffer; |
| 69 | static int disable_scofix; |
| 70 | static int force_scofix; |
| 71 | static int reset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | |
| 73 | #ifdef CONFIG_BT_HCIUSB_SCO |
| 74 | static int isoc = 2; |
| 75 | #endif |
| 76 | |
Marcel Holtmann | 0915e88 | 2005-09-13 01:32:37 +0200 | [diff] [blame] | 77 | #define VERSION "2.9" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | |
| 79 | static struct usb_driver hci_usb_driver; |
| 80 | |
| 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) }, |
| 84 | |
| 85 | /* AVM BlueFRITZ! USB v2.0 */ |
| 86 | { USB_DEVICE(0x057c, 0x3800) }, |
| 87 | |
| 88 | /* Bluetooth Ultraport Module from IBM */ |
| 89 | { USB_DEVICE(0x04bf, 0x030a) }, |
| 90 | |
| 91 | /* ALPS Modules with non-standard id */ |
| 92 | { USB_DEVICE(0x044e, 0x3001) }, |
| 93 | { USB_DEVICE(0x044e, 0x3002) }, |
| 94 | |
| 95 | /* Ericsson with non-standard id */ |
| 96 | { USB_DEVICE(0x0bdb, 0x1002) }, |
| 97 | |
Marcel Holtmann | 62ae159 | 2006-09-21 16:19:55 +0200 | [diff] [blame] | 98 | /* Canyon CN-BTU1 with HID interfaces */ |
| 99 | { USB_DEVICE(0x0c10, 0x0000), .driver_info = HCI_RESET }, |
| 100 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | { } /* Terminating entry */ |
| 102 | }; |
| 103 | |
| 104 | MODULE_DEVICE_TABLE (usb, bluetooth_ids); |
| 105 | |
| 106 | static struct usb_device_id blacklist_ids[] = { |
Marcel Holtmann | 0915e88 | 2005-09-13 01:32:37 +0200 | [diff] [blame] | 107 | /* CSR BlueCore devices */ |
| 108 | { USB_DEVICE(0x0a12, 0x0001), .driver_info = HCI_CSR }, |
| 109 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | /* Broadcom BCM2033 without firmware */ |
| 111 | { USB_DEVICE(0x0a5c, 0x2033), .driver_info = HCI_IGNORE }, |
| 112 | |
| 113 | /* Broadcom BCM2035 */ |
Andy Shevchenko | 6e46c8c | 2008-02-05 03:10:02 -0800 | [diff] [blame] | 114 | { USB_DEVICE(0x0a5c, 0x2035), .driver_info = HCI_RESET | HCI_WRONG_SCO_MTU }, |
Marcel Holtmann | e9e9290 | 2006-07-18 18:32:33 +0200 | [diff] [blame] | 115 | { USB_DEVICE(0x0a5c, 0x200a), .driver_info = HCI_RESET | HCI_WRONG_SCO_MTU }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | { USB_DEVICE(0x0a5c, 0x2009), .driver_info = HCI_BCM92035 }, |
| 117 | |
Marcel Holtmann | 7bb078c | 2007-05-05 00:36:22 +0200 | [diff] [blame] | 118 | /* Broadcom BCM2045 */ |
Karsten Keil | 60d5bce | 2008-03-07 21:10:34 +0100 | [diff] [blame] | 119 | { USB_DEVICE(0x0a5c, 0x2039), .driver_info = HCI_RESET | HCI_WRONG_SCO_MTU }, |
Marcel Holtmann | fd184ab | 2007-05-24 14:26:15 +0200 | [diff] [blame] | 120 | { USB_DEVICE(0x0a5c, 0x2101), .driver_info = HCI_RESET | HCI_WRONG_SCO_MTU }, |
Marcel Holtmann | 7bb078c | 2007-05-05 00:36:22 +0200 | [diff] [blame] | 121 | |
Marcel Holtmann | 520ca78 | 2006-07-14 16:01:52 +0200 | [diff] [blame] | 122 | /* IBM/Lenovo ThinkPad with Broadcom chip */ |
Marcel Holtmann | fd184ab | 2007-05-24 14:26:15 +0200 | [diff] [blame] | 123 | { USB_DEVICE(0x0a5c, 0x201e), .driver_info = HCI_RESET | HCI_WRONG_SCO_MTU }, |
| 124 | { USB_DEVICE(0x0a5c, 0x2110), .driver_info = HCI_RESET | HCI_WRONG_SCO_MTU }, |
Marcel Holtmann | 520ca78 | 2006-07-14 16:01:52 +0200 | [diff] [blame] | 125 | |
Marcel Holtmann | c51bd3d | 2007-05-05 00:36:17 +0200 | [diff] [blame] | 126 | /* Targus ACB10US */ |
| 127 | { USB_DEVICE(0x0a5c, 0x2100), .driver_info = HCI_RESET }, |
| 128 | |
Marcel Holtmann | 0eab934 | 2006-10-20 08:55:29 +0200 | [diff] [blame] | 129 | /* ANYCOM Bluetooth USB-200 and USB-250 */ |
| 130 | { USB_DEVICE(0x0a5c, 0x2111), .driver_info = HCI_RESET }, |
| 131 | |
Marcel Holtmann | da94270 | 2007-01-08 02:16:50 +0100 | [diff] [blame] | 132 | /* HP laptop with Broadcom chip */ |
Marcel Holtmann | fd184ab | 2007-05-24 14:26:15 +0200 | [diff] [blame] | 133 | { USB_DEVICE(0x03f0, 0x171d), .driver_info = HCI_RESET | HCI_WRONG_SCO_MTU }, |
Marcel Holtmann | da94270 | 2007-01-08 02:16:50 +0100 | [diff] [blame] | 134 | |
Marcel Holtmann | 86112ff | 2007-01-08 02:16:53 +0100 | [diff] [blame] | 135 | /* Dell laptop with Broadcom chip */ |
Marcel Holtmann | fd184ab | 2007-05-24 14:26:15 +0200 | [diff] [blame] | 136 | { USB_DEVICE(0x413c, 0x8126), .driver_info = HCI_RESET | HCI_WRONG_SCO_MTU }, |
Michael Frey | 5aa10ca | 2008-07-30 16:59:15 -0700 | [diff] [blame^] | 137 | /* Dell Wireless 370 */ |
| 138 | { USB_DEVICE(0x413c, 0x8156), .driver_info = HCI_RESET | HCI_WRONG_SCO_MTU }, |
| 139 | /* Dell Wireless 410 */ |
| 140 | { USB_DEVICE(0x413c, 0x8152), .driver_info = HCI_RESET | HCI_WRONG_SCO_MTU }, |
| 141 | |
| 142 | /* Broadcom 2046 */ |
| 143 | { USB_DEVICE(0x0a5c, 0x2151), .driver_info = HCI_RESET }, |
Marcel Holtmann | 86112ff | 2007-01-08 02:16:53 +0100 | [diff] [blame] | 144 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | /* Microsoft Wireless Transceiver for Bluetooth 2.0 */ |
| 146 | { USB_DEVICE(0x045e, 0x009c), .driver_info = HCI_RESET }, |
| 147 | |
Marcel Holtmann | cad0f62 | 2005-08-06 12:36:36 +0200 | [diff] [blame] | 148 | /* Kensington Bluetooth USB adapter */ |
| 149 | { USB_DEVICE(0x047d, 0x105d), .driver_info = HCI_RESET }, |
Marcel Holtmann | fd184ab | 2007-05-24 14:26:15 +0200 | [diff] [blame] | 150 | { USB_DEVICE(0x047d, 0x105e), .driver_info = HCI_RESET | HCI_WRONG_SCO_MTU }, |
Marcel Holtmann | cad0f62 | 2005-08-06 12:36:36 +0200 | [diff] [blame] | 151 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | /* ISSC Bluetooth Adapter v3.1 */ |
| 153 | { USB_DEVICE(0x1131, 0x1001), .driver_info = HCI_RESET }, |
| 154 | |
Marcel Holtmann | 8e4f723 | 2006-07-18 18:04:59 +0200 | [diff] [blame] | 155 | /* RTX Telecom based adapters with buggy SCO support */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | { USB_DEVICE(0x0400, 0x0807), .driver_info = HCI_BROKEN_ISOC }, |
Marcel Holtmann | 8e4f723 | 2006-07-18 18:04:59 +0200 | [diff] [blame] | 157 | { USB_DEVICE(0x0400, 0x080a), .driver_info = HCI_BROKEN_ISOC }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | |
SDiZ | 09a7603 | 2008-03-05 18:43:50 -0800 | [diff] [blame] | 159 | /* CONWISE Technology based adapters with buggy SCO support */ |
| 160 | { USB_DEVICE(0x0e5e, 0x6622), .driver_info = HCI_BROKEN_ISOC }, |
| 161 | |
Marcel Holtmann | ea9727f | 2006-07-18 17:47:40 +0200 | [diff] [blame] | 162 | /* Belkin F8T012 and F8T013 devices */ |
Marcel Holtmann | fd184ab | 2007-05-24 14:26:15 +0200 | [diff] [blame] | 163 | { USB_DEVICE(0x050d, 0x0012), .driver_info = HCI_RESET | HCI_WRONG_SCO_MTU }, |
| 164 | { USB_DEVICE(0x050d, 0x0013), .driver_info = HCI_RESET | HCI_WRONG_SCO_MTU }, |
Marcel Holtmann | da1f519 | 2006-07-03 10:02:29 +0200 | [diff] [blame] | 165 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | /* Digianswer devices */ |
| 167 | { USB_DEVICE(0x08fd, 0x0001), .driver_info = HCI_DIGIANSWER }, |
| 168 | { USB_DEVICE(0x08fd, 0x0002), .driver_info = HCI_IGNORE }, |
| 169 | |
| 170 | /* CSR BlueCore Bluetooth Sniffer */ |
| 171 | { USB_DEVICE(0x0a12, 0x0002), .driver_info = HCI_SNIFFER }, |
| 172 | |
Marcel Holtmann | 2b86ad2 | 2006-07-03 10:02:18 +0200 | [diff] [blame] | 173 | /* Frontline ComProbe Bluetooth Sniffer */ |
| 174 | { USB_DEVICE(0x16d3, 0x0002), .driver_info = HCI_SNIFFER }, |
| 175 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 176 | { } /* Terminating entry */ |
| 177 | }; |
| 178 | |
Al Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 179 | static struct _urb *_urb_alloc(int isoc, gfp_t gfp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | { |
| 181 | struct _urb *_urb = kmalloc(sizeof(struct _urb) + |
| 182 | sizeof(struct usb_iso_packet_descriptor) * isoc, gfp); |
| 183 | if (_urb) { |
| 184 | memset(_urb, 0, sizeof(*_urb)); |
| 185 | usb_init_urb(&_urb->urb); |
| 186 | } |
| 187 | return _urb; |
| 188 | } |
| 189 | |
| 190 | static struct _urb *_urb_dequeue(struct _urb_queue *q) |
| 191 | { |
| 192 | struct _urb *_urb = NULL; |
| 193 | unsigned long flags; |
| 194 | spin_lock_irqsave(&q->lock, flags); |
| 195 | { |
| 196 | struct list_head *head = &q->head; |
| 197 | struct list_head *next = head->next; |
| 198 | if (next != head) { |
| 199 | _urb = list_entry(next, struct _urb, list); |
| 200 | list_del(next); _urb->queue = NULL; |
| 201 | } |
| 202 | } |
| 203 | spin_unlock_irqrestore(&q->lock, flags); |
| 204 | return _urb; |
| 205 | } |
| 206 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 207 | static void hci_usb_rx_complete(struct urb *urb); |
| 208 | static void hci_usb_tx_complete(struct urb *urb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | |
| 210 | #define __pending_tx(husb, type) (&husb->pending_tx[type-1]) |
| 211 | #define __pending_q(husb, type) (&husb->pending_q[type-1]) |
| 212 | #define __completed_q(husb, type) (&husb->completed_q[type-1]) |
| 213 | #define __transmit_q(husb, type) (&husb->transmit_q[type-1]) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | |
| 215 | static inline struct _urb *__get_completed(struct hci_usb *husb, int type) |
| 216 | { |
| 217 | return _urb_dequeue(__completed_q(husb, type)); |
| 218 | } |
| 219 | |
| 220 | #ifdef CONFIG_BT_HCIUSB_SCO |
| 221 | static void __fill_isoc_desc(struct urb *urb, int len, int mtu) |
| 222 | { |
| 223 | int offset = 0, i; |
| 224 | |
| 225 | BT_DBG("len %d mtu %d", len, mtu); |
| 226 | |
| 227 | for (i=0; i < HCI_MAX_ISOC_FRAMES && len >= mtu; i++, offset += mtu, len -= mtu) { |
| 228 | urb->iso_frame_desc[i].offset = offset; |
| 229 | urb->iso_frame_desc[i].length = mtu; |
| 230 | BT_DBG("desc %d offset %d len %d", i, offset, mtu); |
| 231 | } |
| 232 | if (len && i < HCI_MAX_ISOC_FRAMES) { |
| 233 | urb->iso_frame_desc[i].offset = offset; |
| 234 | urb->iso_frame_desc[i].length = len; |
| 235 | BT_DBG("desc %d offset %d len %d", i, offset, len); |
| 236 | i++; |
| 237 | } |
| 238 | urb->number_of_packets = i; |
| 239 | } |
| 240 | #endif |
| 241 | |
| 242 | static int hci_usb_intr_rx_submit(struct hci_usb *husb) |
| 243 | { |
| 244 | struct _urb *_urb; |
| 245 | struct urb *urb; |
| 246 | int err, pipe, interval, size; |
| 247 | void *buf; |
| 248 | |
| 249 | BT_DBG("%s", husb->hdev->name); |
| 250 | |
| 251 | size = le16_to_cpu(husb->intr_in_ep->desc.wMaxPacketSize); |
| 252 | |
| 253 | buf = kmalloc(size, GFP_ATOMIC); |
| 254 | if (!buf) |
| 255 | return -ENOMEM; |
| 256 | |
| 257 | _urb = _urb_alloc(0, GFP_ATOMIC); |
| 258 | if (!_urb) { |
| 259 | kfree(buf); |
| 260 | return -ENOMEM; |
| 261 | } |
| 262 | _urb->type = HCI_EVENT_PKT; |
| 263 | _urb_queue_tail(__pending_q(husb, _urb->type), _urb); |
| 264 | |
| 265 | urb = &_urb->urb; |
| 266 | pipe = usb_rcvintpipe(husb->udev, husb->intr_in_ep->desc.bEndpointAddress); |
| 267 | interval = husb->intr_in_ep->desc.bInterval; |
| 268 | usb_fill_int_urb(urb, husb->udev, pipe, buf, size, hci_usb_rx_complete, husb, interval); |
| 269 | |
| 270 | err = usb_submit_urb(urb, GFP_ATOMIC); |
| 271 | if (err) { |
| 272 | BT_ERR("%s intr rx submit failed urb %p err %d", |
| 273 | husb->hdev->name, urb, err); |
| 274 | _urb_unlink(_urb); |
Pavel Machek | d129f18 | 2008-04-19 18:17:26 -0700 | [diff] [blame] | 275 | kfree(_urb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | kfree(buf); |
| 277 | } |
| 278 | return err; |
| 279 | } |
| 280 | |
| 281 | static int hci_usb_bulk_rx_submit(struct hci_usb *husb) |
| 282 | { |
| 283 | struct _urb *_urb; |
| 284 | struct urb *urb; |
| 285 | int err, pipe, size = HCI_MAX_FRAME_SIZE; |
| 286 | void *buf; |
| 287 | |
| 288 | buf = kmalloc(size, GFP_ATOMIC); |
| 289 | if (!buf) |
| 290 | return -ENOMEM; |
| 291 | |
| 292 | _urb = _urb_alloc(0, GFP_ATOMIC); |
| 293 | if (!_urb) { |
| 294 | kfree(buf); |
| 295 | return -ENOMEM; |
| 296 | } |
| 297 | _urb->type = HCI_ACLDATA_PKT; |
| 298 | _urb_queue_tail(__pending_q(husb, _urb->type), _urb); |
| 299 | |
| 300 | urb = &_urb->urb; |
| 301 | pipe = usb_rcvbulkpipe(husb->udev, husb->bulk_in_ep->desc.bEndpointAddress); |
| 302 | usb_fill_bulk_urb(urb, husb->udev, pipe, buf, size, hci_usb_rx_complete, husb); |
| 303 | urb->transfer_flags = 0; |
| 304 | |
| 305 | BT_DBG("%s urb %p", husb->hdev->name, urb); |
| 306 | |
| 307 | err = usb_submit_urb(urb, GFP_ATOMIC); |
| 308 | if (err) { |
| 309 | BT_ERR("%s bulk rx submit failed urb %p err %d", |
| 310 | husb->hdev->name, urb, err); |
| 311 | _urb_unlink(_urb); |
Pavel Machek | d129f18 | 2008-04-19 18:17:26 -0700 | [diff] [blame] | 312 | kfree(_urb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | kfree(buf); |
| 314 | } |
| 315 | return err; |
| 316 | } |
| 317 | |
| 318 | #ifdef CONFIG_BT_HCIUSB_SCO |
| 319 | static int hci_usb_isoc_rx_submit(struct hci_usb *husb) |
| 320 | { |
| 321 | struct _urb *_urb; |
| 322 | struct urb *urb; |
| 323 | int err, mtu, size; |
| 324 | void *buf; |
| 325 | |
| 326 | mtu = le16_to_cpu(husb->isoc_in_ep->desc.wMaxPacketSize); |
| 327 | size = mtu * HCI_MAX_ISOC_FRAMES; |
| 328 | |
| 329 | buf = kmalloc(size, GFP_ATOMIC); |
| 330 | if (!buf) |
| 331 | return -ENOMEM; |
| 332 | |
| 333 | _urb = _urb_alloc(HCI_MAX_ISOC_FRAMES, GFP_ATOMIC); |
| 334 | if (!_urb) { |
| 335 | kfree(buf); |
| 336 | return -ENOMEM; |
| 337 | } |
| 338 | _urb->type = HCI_SCODATA_PKT; |
| 339 | _urb_queue_tail(__pending_q(husb, _urb->type), _urb); |
| 340 | |
| 341 | urb = &_urb->urb; |
| 342 | |
| 343 | urb->context = husb; |
| 344 | urb->dev = husb->udev; |
| 345 | urb->pipe = usb_rcvisocpipe(husb->udev, husb->isoc_in_ep->desc.bEndpointAddress); |
| 346 | urb->complete = hci_usb_rx_complete; |
| 347 | |
| 348 | urb->interval = husb->isoc_in_ep->desc.bInterval; |
| 349 | |
| 350 | urb->transfer_buffer_length = size; |
| 351 | urb->transfer_buffer = buf; |
| 352 | urb->transfer_flags = URB_ISO_ASAP; |
| 353 | |
| 354 | __fill_isoc_desc(urb, size, mtu); |
| 355 | |
| 356 | BT_DBG("%s urb %p", husb->hdev->name, urb); |
| 357 | |
| 358 | err = usb_submit_urb(urb, GFP_ATOMIC); |
| 359 | if (err) { |
| 360 | BT_ERR("%s isoc rx submit failed urb %p err %d", |
| 361 | husb->hdev->name, urb, err); |
| 362 | _urb_unlink(_urb); |
Pavel Machek | d129f18 | 2008-04-19 18:17:26 -0700 | [diff] [blame] | 363 | kfree(_urb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | kfree(buf); |
| 365 | } |
| 366 | return err; |
| 367 | } |
| 368 | #endif |
| 369 | |
| 370 | /* Initialize device */ |
| 371 | static int hci_usb_open(struct hci_dev *hdev) |
| 372 | { |
| 373 | struct hci_usb *husb = (struct hci_usb *) hdev->driver_data; |
| 374 | int i, err; |
| 375 | unsigned long flags; |
| 376 | |
| 377 | BT_DBG("%s", hdev->name); |
| 378 | |
| 379 | if (test_and_set_bit(HCI_RUNNING, &hdev->flags)) |
| 380 | return 0; |
| 381 | |
| 382 | write_lock_irqsave(&husb->completion_lock, flags); |
| 383 | |
| 384 | err = hci_usb_intr_rx_submit(husb); |
| 385 | if (!err) { |
| 386 | for (i = 0; i < HCI_MAX_BULK_RX; i++) |
| 387 | hci_usb_bulk_rx_submit(husb); |
| 388 | |
| 389 | #ifdef CONFIG_BT_HCIUSB_SCO |
| 390 | if (husb->isoc_iface) |
| 391 | for (i = 0; i < HCI_MAX_ISOC_RX; i++) |
| 392 | hci_usb_isoc_rx_submit(husb); |
| 393 | #endif |
| 394 | } else { |
| 395 | clear_bit(HCI_RUNNING, &hdev->flags); |
| 396 | } |
| 397 | |
| 398 | write_unlock_irqrestore(&husb->completion_lock, flags); |
| 399 | return err; |
| 400 | } |
| 401 | |
| 402 | /* Reset device */ |
| 403 | static int hci_usb_flush(struct hci_dev *hdev) |
| 404 | { |
| 405 | struct hci_usb *husb = (struct hci_usb *) hdev->driver_data; |
| 406 | int i; |
| 407 | |
| 408 | BT_DBG("%s", hdev->name); |
| 409 | |
| 410 | for (i = 0; i < 4; i++) |
| 411 | skb_queue_purge(&husb->transmit_q[i]); |
| 412 | return 0; |
| 413 | } |
| 414 | |
| 415 | static void hci_usb_unlink_urbs(struct hci_usb *husb) |
| 416 | { |
| 417 | int i; |
| 418 | |
| 419 | BT_DBG("%s", husb->hdev->name); |
| 420 | |
| 421 | for (i = 0; i < 4; i++) { |
| 422 | struct _urb *_urb; |
| 423 | struct urb *urb; |
| 424 | |
| 425 | /* Kill pending requests */ |
| 426 | while ((_urb = _urb_dequeue(&husb->pending_q[i]))) { |
| 427 | urb = &_urb->urb; |
| 428 | BT_DBG("%s unlinking _urb %p type %d urb %p", |
| 429 | husb->hdev->name, _urb, _urb->type, urb); |
| 430 | usb_kill_urb(urb); |
| 431 | _urb_queue_tail(__completed_q(husb, _urb->type), _urb); |
| 432 | } |
| 433 | |
| 434 | /* Release completed requests */ |
| 435 | while ((_urb = _urb_dequeue(&husb->completed_q[i]))) { |
| 436 | urb = &_urb->urb; |
| 437 | BT_DBG("%s freeing _urb %p type %d urb %p", |
| 438 | husb->hdev->name, _urb, _urb->type, urb); |
Marcel Holtmann | e9a3e67 | 2005-08-06 12:36:47 +0200 | [diff] [blame] | 439 | kfree(urb->setup_packet); |
| 440 | kfree(urb->transfer_buffer); |
Pavel Machek | d129f18 | 2008-04-19 18:17:26 -0700 | [diff] [blame] | 441 | kfree(_urb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | } |
| 444 | } |
| 445 | |
| 446 | /* Close device */ |
| 447 | static int hci_usb_close(struct hci_dev *hdev) |
| 448 | { |
| 449 | struct hci_usb *husb = (struct hci_usb *) hdev->driver_data; |
| 450 | unsigned long flags; |
| 451 | |
| 452 | if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags)) |
| 453 | return 0; |
| 454 | |
| 455 | BT_DBG("%s", hdev->name); |
| 456 | |
| 457 | /* Synchronize with completion handlers */ |
| 458 | write_lock_irqsave(&husb->completion_lock, flags); |
| 459 | write_unlock_irqrestore(&husb->completion_lock, flags); |
| 460 | |
| 461 | hci_usb_unlink_urbs(husb); |
| 462 | hci_usb_flush(hdev); |
| 463 | return 0; |
| 464 | } |
| 465 | |
| 466 | static int __tx_submit(struct hci_usb *husb, struct _urb *_urb) |
| 467 | { |
| 468 | struct urb *urb = &_urb->urb; |
| 469 | int err; |
| 470 | |
| 471 | BT_DBG("%s urb %p type %d", husb->hdev->name, urb, _urb->type); |
| 472 | |
| 473 | _urb_queue_tail(__pending_q(husb, _urb->type), _urb); |
| 474 | err = usb_submit_urb(urb, GFP_ATOMIC); |
| 475 | if (err) { |
| 476 | BT_ERR("%s tx submit failed urb %p type %d err %d", |
| 477 | husb->hdev->name, urb, _urb->type, err); |
| 478 | _urb_unlink(_urb); |
| 479 | _urb_queue_tail(__completed_q(husb, _urb->type), _urb); |
| 480 | } else |
| 481 | atomic_inc(__pending_tx(husb, _urb->type)); |
| 482 | |
| 483 | return err; |
| 484 | } |
| 485 | |
| 486 | static inline int hci_usb_send_ctrl(struct hci_usb *husb, struct sk_buff *skb) |
| 487 | { |
Marcel Holtmann | 0d48d93 | 2005-08-09 20:30:28 -0700 | [diff] [blame] | 488 | struct _urb *_urb = __get_completed(husb, bt_cb(skb)->pkt_type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | struct usb_ctrlrequest *dr; |
| 490 | struct urb *urb; |
| 491 | |
| 492 | if (!_urb) { |
| 493 | _urb = _urb_alloc(0, GFP_ATOMIC); |
| 494 | if (!_urb) |
| 495 | return -ENOMEM; |
Marcel Holtmann | 0d48d93 | 2005-08-09 20:30:28 -0700 | [diff] [blame] | 496 | _urb->type = bt_cb(skb)->pkt_type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 497 | |
| 498 | dr = kmalloc(sizeof(*dr), GFP_ATOMIC); |
| 499 | if (!dr) { |
Pavel Machek | d129f18 | 2008-04-19 18:17:26 -0700 | [diff] [blame] | 500 | kfree(_urb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | return -ENOMEM; |
| 502 | } |
| 503 | } else |
| 504 | dr = (void *) _urb->urb.setup_packet; |
| 505 | |
| 506 | dr->bRequestType = husb->ctrl_req; |
| 507 | dr->bRequest = 0; |
| 508 | dr->wIndex = 0; |
| 509 | dr->wValue = 0; |
| 510 | dr->wLength = __cpu_to_le16(skb->len); |
| 511 | |
| 512 | urb = &_urb->urb; |
| 513 | usb_fill_control_urb(urb, husb->udev, usb_sndctrlpipe(husb->udev, 0), |
| 514 | (void *) dr, skb->data, skb->len, hci_usb_tx_complete, husb); |
| 515 | |
| 516 | BT_DBG("%s skb %p len %d", husb->hdev->name, skb, skb->len); |
| 517 | |
| 518 | _urb->priv = skb; |
| 519 | return __tx_submit(husb, _urb); |
| 520 | } |
| 521 | |
| 522 | static inline int hci_usb_send_bulk(struct hci_usb *husb, struct sk_buff *skb) |
| 523 | { |
Marcel Holtmann | 0d48d93 | 2005-08-09 20:30:28 -0700 | [diff] [blame] | 524 | struct _urb *_urb = __get_completed(husb, bt_cb(skb)->pkt_type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | struct urb *urb; |
| 526 | int pipe; |
| 527 | |
| 528 | if (!_urb) { |
| 529 | _urb = _urb_alloc(0, GFP_ATOMIC); |
| 530 | if (!_urb) |
| 531 | return -ENOMEM; |
Marcel Holtmann | 0d48d93 | 2005-08-09 20:30:28 -0700 | [diff] [blame] | 532 | _urb->type = bt_cb(skb)->pkt_type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | urb = &_urb->urb; |
| 536 | pipe = usb_sndbulkpipe(husb->udev, husb->bulk_out_ep->desc.bEndpointAddress); |
| 537 | usb_fill_bulk_urb(urb, husb->udev, pipe, skb->data, skb->len, |
| 538 | hci_usb_tx_complete, husb); |
| 539 | urb->transfer_flags = URB_ZERO_PACKET; |
| 540 | |
| 541 | BT_DBG("%s skb %p len %d", husb->hdev->name, skb, skb->len); |
| 542 | |
| 543 | _urb->priv = skb; |
| 544 | return __tx_submit(husb, _urb); |
| 545 | } |
| 546 | |
| 547 | #ifdef CONFIG_BT_HCIUSB_SCO |
| 548 | static inline int hci_usb_send_isoc(struct hci_usb *husb, struct sk_buff *skb) |
| 549 | { |
Marcel Holtmann | 0d48d93 | 2005-08-09 20:30:28 -0700 | [diff] [blame] | 550 | struct _urb *_urb = __get_completed(husb, bt_cb(skb)->pkt_type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 551 | struct urb *urb; |
| 552 | |
| 553 | if (!_urb) { |
| 554 | _urb = _urb_alloc(HCI_MAX_ISOC_FRAMES, GFP_ATOMIC); |
| 555 | if (!_urb) |
| 556 | return -ENOMEM; |
Marcel Holtmann | 0d48d93 | 2005-08-09 20:30:28 -0700 | [diff] [blame] | 557 | _urb->type = bt_cb(skb)->pkt_type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | BT_DBG("%s skb %p len %d", husb->hdev->name, skb, skb->len); |
| 561 | |
| 562 | urb = &_urb->urb; |
| 563 | |
| 564 | urb->context = husb; |
| 565 | urb->dev = husb->udev; |
| 566 | urb->pipe = usb_sndisocpipe(husb->udev, husb->isoc_out_ep->desc.bEndpointAddress); |
| 567 | urb->complete = hci_usb_tx_complete; |
| 568 | urb->transfer_flags = URB_ISO_ASAP; |
| 569 | |
| 570 | urb->interval = husb->isoc_out_ep->desc.bInterval; |
| 571 | |
| 572 | urb->transfer_buffer = skb->data; |
| 573 | urb->transfer_buffer_length = skb->len; |
| 574 | |
| 575 | __fill_isoc_desc(urb, skb->len, le16_to_cpu(husb->isoc_out_ep->desc.wMaxPacketSize)); |
| 576 | |
| 577 | _urb->priv = skb; |
| 578 | return __tx_submit(husb, _urb); |
| 579 | } |
| 580 | #endif |
| 581 | |
| 582 | static void hci_usb_tx_process(struct hci_usb *husb) |
| 583 | { |
| 584 | struct sk_buff_head *q; |
| 585 | struct sk_buff *skb; |
| 586 | |
| 587 | BT_DBG("%s", husb->hdev->name); |
| 588 | |
| 589 | do { |
| 590 | clear_bit(HCI_USB_TX_WAKEUP, &husb->state); |
| 591 | |
| 592 | /* Process command queue */ |
| 593 | q = __transmit_q(husb, HCI_COMMAND_PKT); |
| 594 | if (!atomic_read(__pending_tx(husb, HCI_COMMAND_PKT)) && |
| 595 | (skb = skb_dequeue(q))) { |
| 596 | if (hci_usb_send_ctrl(husb, skb) < 0) |
| 597 | skb_queue_head(q, skb); |
| 598 | } |
| 599 | |
| 600 | #ifdef CONFIG_BT_HCIUSB_SCO |
| 601 | /* Process SCO queue */ |
| 602 | q = __transmit_q(husb, HCI_SCODATA_PKT); |
| 603 | if (atomic_read(__pending_tx(husb, HCI_SCODATA_PKT)) < HCI_MAX_ISOC_TX && |
| 604 | (skb = skb_dequeue(q))) { |
| 605 | if (hci_usb_send_isoc(husb, skb) < 0) |
| 606 | skb_queue_head(q, skb); |
| 607 | } |
| 608 | #endif |
| 609 | |
| 610 | /* Process ACL queue */ |
| 611 | q = __transmit_q(husb, HCI_ACLDATA_PKT); |
| 612 | while (atomic_read(__pending_tx(husb, HCI_ACLDATA_PKT)) < HCI_MAX_BULK_TX && |
| 613 | (skb = skb_dequeue(q))) { |
| 614 | if (hci_usb_send_bulk(husb, skb) < 0) { |
| 615 | skb_queue_head(q, skb); |
| 616 | break; |
| 617 | } |
| 618 | } |
| 619 | } while(test_bit(HCI_USB_TX_WAKEUP, &husb->state)); |
| 620 | } |
| 621 | |
| 622 | static inline void hci_usb_tx_wakeup(struct hci_usb *husb) |
| 623 | { |
| 624 | /* Serialize TX queue processing to avoid data reordering */ |
| 625 | if (!test_and_set_bit(HCI_USB_TX_PROCESS, &husb->state)) { |
| 626 | hci_usb_tx_process(husb); |
| 627 | clear_bit(HCI_USB_TX_PROCESS, &husb->state); |
| 628 | } else |
| 629 | set_bit(HCI_USB_TX_WAKEUP, &husb->state); |
| 630 | } |
| 631 | |
| 632 | /* Send frames from HCI layer */ |
| 633 | static int hci_usb_send_frame(struct sk_buff *skb) |
| 634 | { |
| 635 | struct hci_dev *hdev = (struct hci_dev *) skb->dev; |
| 636 | struct hci_usb *husb; |
| 637 | |
| 638 | if (!hdev) { |
| 639 | BT_ERR("frame for uknown device (hdev=NULL)"); |
| 640 | return -ENODEV; |
| 641 | } |
| 642 | |
| 643 | if (!test_bit(HCI_RUNNING, &hdev->flags)) |
| 644 | return -EBUSY; |
| 645 | |
Marcel Holtmann | 0d48d93 | 2005-08-09 20:30:28 -0700 | [diff] [blame] | 646 | BT_DBG("%s type %d len %d", hdev->name, bt_cb(skb)->pkt_type, skb->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 647 | |
| 648 | husb = (struct hci_usb *) hdev->driver_data; |
| 649 | |
Marcel Holtmann | 0d48d93 | 2005-08-09 20:30:28 -0700 | [diff] [blame] | 650 | switch (bt_cb(skb)->pkt_type) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | case HCI_COMMAND_PKT: |
| 652 | hdev->stat.cmd_tx++; |
| 653 | break; |
| 654 | |
| 655 | case HCI_ACLDATA_PKT: |
| 656 | hdev->stat.acl_tx++; |
| 657 | break; |
| 658 | |
| 659 | #ifdef CONFIG_BT_HCIUSB_SCO |
| 660 | case HCI_SCODATA_PKT: |
| 661 | hdev->stat.sco_tx++; |
| 662 | break; |
| 663 | #endif |
| 664 | |
| 665 | default: |
| 666 | kfree_skb(skb); |
| 667 | return 0; |
| 668 | } |
| 669 | |
| 670 | read_lock(&husb->completion_lock); |
| 671 | |
Marcel Holtmann | 0d48d93 | 2005-08-09 20:30:28 -0700 | [diff] [blame] | 672 | skb_queue_tail(__transmit_q(husb, bt_cb(skb)->pkt_type), skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 673 | hci_usb_tx_wakeup(husb); |
| 674 | |
| 675 | read_unlock(&husb->completion_lock); |
| 676 | return 0; |
| 677 | } |
| 678 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 679 | static void hci_usb_rx_complete(struct urb *urb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 680 | { |
| 681 | struct _urb *_urb = container_of(urb, struct _urb, urb); |
| 682 | struct hci_usb *husb = (void *) urb->context; |
| 683 | struct hci_dev *hdev = husb->hdev; |
| 684 | int err, count = urb->actual_length; |
| 685 | |
| 686 | BT_DBG("%s urb %p type %d status %d count %d flags %x", hdev->name, urb, |
| 687 | _urb->type, urb->status, count, urb->transfer_flags); |
| 688 | |
| 689 | read_lock(&husb->completion_lock); |
| 690 | |
| 691 | if (!test_bit(HCI_RUNNING, &hdev->flags)) |
| 692 | goto unlock; |
| 693 | |
| 694 | if (urb->status || !count) |
| 695 | goto resubmit; |
| 696 | |
| 697 | if (_urb->type == HCI_SCODATA_PKT) { |
| 698 | #ifdef CONFIG_BT_HCIUSB_SCO |
| 699 | int i; |
| 700 | for (i=0; i < urb->number_of_packets; i++) { |
| 701 | BT_DBG("desc %d status %d offset %d len %d", i, |
| 702 | urb->iso_frame_desc[i].status, |
| 703 | urb->iso_frame_desc[i].offset, |
| 704 | urb->iso_frame_desc[i].actual_length); |
| 705 | |
Marcel Holtmann | 26a4a06 | 2007-09-09 08:39:27 +0200 | [diff] [blame] | 706 | if (!urb->iso_frame_desc[i].status) { |
| 707 | husb->hdev->stat.byte_rx += urb->iso_frame_desc[i].actual_length; |
Marcel Holtmann | babf4d4 | 2007-07-11 06:42:35 +0200 | [diff] [blame] | 708 | hci_recv_fragment(husb->hdev, _urb->type, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | urb->transfer_buffer + urb->iso_frame_desc[i].offset, |
| 710 | urb->iso_frame_desc[i].actual_length); |
Marcel Holtmann | 26a4a06 | 2007-09-09 08:39:27 +0200 | [diff] [blame] | 711 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 | } |
| 713 | #else |
| 714 | ; |
| 715 | #endif |
| 716 | } else { |
Marcel Holtmann | 26a4a06 | 2007-09-09 08:39:27 +0200 | [diff] [blame] | 717 | husb->hdev->stat.byte_rx += count; |
Marcel Holtmann | babf4d4 | 2007-07-11 06:42:35 +0200 | [diff] [blame] | 718 | err = hci_recv_fragment(husb->hdev, _urb->type, urb->transfer_buffer, count); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 719 | if (err < 0) { |
| 720 | BT_ERR("%s corrupted packet: type %d count %d", |
| 721 | husb->hdev->name, _urb->type, count); |
| 722 | hdev->stat.err_rx++; |
| 723 | } |
| 724 | } |
| 725 | |
| 726 | resubmit: |
| 727 | urb->dev = husb->udev; |
| 728 | err = usb_submit_urb(urb, GFP_ATOMIC); |
| 729 | BT_DBG("%s urb %p type %d resubmit status %d", hdev->name, urb, |
| 730 | _urb->type, err); |
| 731 | |
| 732 | unlock: |
| 733 | read_unlock(&husb->completion_lock); |
| 734 | } |
| 735 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 736 | static void hci_usb_tx_complete(struct urb *urb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 737 | { |
| 738 | struct _urb *_urb = container_of(urb, struct _urb, urb); |
| 739 | struct hci_usb *husb = (void *) urb->context; |
| 740 | struct hci_dev *hdev = husb->hdev; |
| 741 | |
| 742 | BT_DBG("%s urb %p status %d flags %x", hdev->name, urb, |
| 743 | urb->status, urb->transfer_flags); |
| 744 | |
| 745 | atomic_dec(__pending_tx(husb, _urb->type)); |
| 746 | |
| 747 | urb->transfer_buffer = NULL; |
| 748 | kfree_skb((struct sk_buff *) _urb->priv); |
| 749 | |
| 750 | if (!test_bit(HCI_RUNNING, &hdev->flags)) |
| 751 | return; |
| 752 | |
| 753 | if (!urb->status) |
| 754 | hdev->stat.byte_tx += urb->transfer_buffer_length; |
| 755 | else |
| 756 | hdev->stat.err_tx++; |
| 757 | |
| 758 | read_lock(&husb->completion_lock); |
| 759 | |
| 760 | _urb_unlink(_urb); |
| 761 | _urb_queue_tail(__completed_q(husb, _urb->type), _urb); |
| 762 | |
| 763 | hci_usb_tx_wakeup(husb); |
| 764 | |
| 765 | read_unlock(&husb->completion_lock); |
| 766 | } |
| 767 | |
| 768 | static void hci_usb_destruct(struct hci_dev *hdev) |
| 769 | { |
| 770 | struct hci_usb *husb = (struct hci_usb *) hdev->driver_data; |
| 771 | |
| 772 | BT_DBG("%s", hdev->name); |
| 773 | |
| 774 | kfree(husb); |
| 775 | } |
| 776 | |
| 777 | static void hci_usb_notify(struct hci_dev *hdev, unsigned int evt) |
| 778 | { |
| 779 | BT_DBG("%s evt %d", hdev->name, evt); |
| 780 | } |
| 781 | |
| 782 | static int hci_usb_probe(struct usb_interface *intf, const struct usb_device_id *id) |
| 783 | { |
| 784 | struct usb_device *udev = interface_to_usbdev(intf); |
| 785 | struct usb_host_endpoint *bulk_out_ep = NULL; |
| 786 | struct usb_host_endpoint *bulk_in_ep = NULL; |
| 787 | struct usb_host_endpoint *intr_in_ep = NULL; |
| 788 | struct usb_host_endpoint *ep; |
| 789 | struct usb_host_interface *uif; |
| 790 | struct usb_interface *isoc_iface; |
| 791 | struct hci_usb *husb; |
| 792 | struct hci_dev *hdev; |
| 793 | int i, e, size, isoc_ifnum, isoc_alts; |
| 794 | |
| 795 | BT_DBG("udev %p intf %p", udev, intf); |
| 796 | |
| 797 | if (!id->driver_info) { |
| 798 | const struct usb_device_id *match; |
| 799 | match = usb_match_id(intf, blacklist_ids); |
| 800 | if (match) |
| 801 | id = match; |
| 802 | } |
| 803 | |
| 804 | if (ignore || id->driver_info & HCI_IGNORE) |
| 805 | return -ENODEV; |
| 806 | |
Marcel Holtmann | 7ef934b | 2005-11-08 09:57:05 -0800 | [diff] [blame] | 807 | if (ignore_dga && id->driver_info & HCI_DIGIANSWER) |
| 808 | return -ENODEV; |
| 809 | |
Marcel Holtmann | 0915e88 | 2005-09-13 01:32:37 +0200 | [diff] [blame] | 810 | if (ignore_csr && id->driver_info & HCI_CSR) |
| 811 | return -ENODEV; |
| 812 | |
| 813 | if (ignore_sniffer && id->driver_info & HCI_SNIFFER) |
| 814 | return -ENODEV; |
| 815 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 816 | if (intf->cur_altsetting->desc.bInterfaceNumber > 0) |
| 817 | return -ENODEV; |
| 818 | |
| 819 | /* Find endpoints that we need */ |
| 820 | uif = intf->cur_altsetting; |
| 821 | for (e = 0; e < uif->desc.bNumEndpoints; e++) { |
| 822 | ep = &uif->endpoint[e]; |
| 823 | |
| 824 | switch (ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) { |
| 825 | case USB_ENDPOINT_XFER_INT: |
| 826 | if (ep->desc.bEndpointAddress & USB_DIR_IN) |
| 827 | intr_in_ep = ep; |
| 828 | break; |
| 829 | |
| 830 | case USB_ENDPOINT_XFER_BULK: |
| 831 | if (ep->desc.bEndpointAddress & USB_DIR_IN) |
| 832 | bulk_in_ep = ep; |
| 833 | else |
| 834 | bulk_out_ep = ep; |
| 835 | break; |
| 836 | } |
| 837 | } |
| 838 | |
| 839 | if (!bulk_in_ep || !bulk_out_ep || !intr_in_ep) { |
| 840 | BT_DBG("Bulk endpoints not found"); |
| 841 | goto done; |
| 842 | } |
| 843 | |
Deepak Saxena | 089b1db | 2005-11-07 01:01:26 -0800 | [diff] [blame] | 844 | if (!(husb = kzalloc(sizeof(struct hci_usb), GFP_KERNEL))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 845 | BT_ERR("Can't allocate: control structure"); |
| 846 | goto done; |
| 847 | } |
| 848 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 849 | husb->udev = udev; |
| 850 | husb->bulk_out_ep = bulk_out_ep; |
| 851 | husb->bulk_in_ep = bulk_in_ep; |
| 852 | husb->intr_in_ep = intr_in_ep; |
| 853 | |
| 854 | if (id->driver_info & HCI_DIGIANSWER) |
| 855 | husb->ctrl_req = USB_TYPE_VENDOR; |
| 856 | else |
| 857 | husb->ctrl_req = USB_TYPE_CLASS; |
| 858 | |
| 859 | /* Find isochronous endpoints that we can use */ |
| 860 | size = 0; |
| 861 | isoc_iface = NULL; |
| 862 | isoc_alts = 0; |
| 863 | isoc_ifnum = 1; |
| 864 | |
| 865 | #ifdef CONFIG_BT_HCIUSB_SCO |
| 866 | if (isoc && !(id->driver_info & (HCI_BROKEN_ISOC | HCI_SNIFFER))) |
| 867 | isoc_iface = usb_ifnum_to_if(udev, isoc_ifnum); |
| 868 | |
| 869 | if (isoc_iface) { |
| 870 | int a; |
| 871 | struct usb_host_endpoint *isoc_out_ep = NULL; |
| 872 | struct usb_host_endpoint *isoc_in_ep = NULL; |
| 873 | |
| 874 | for (a = 0; a < isoc_iface->num_altsetting; a++) { |
| 875 | uif = &isoc_iface->altsetting[a]; |
| 876 | for (e = 0; e < uif->desc.bNumEndpoints; e++) { |
| 877 | ep = &uif->endpoint[e]; |
| 878 | |
| 879 | switch (ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) { |
| 880 | case USB_ENDPOINT_XFER_ISOC: |
| 881 | if (le16_to_cpu(ep->desc.wMaxPacketSize) < size || |
| 882 | uif->desc.bAlternateSetting != isoc) |
| 883 | break; |
| 884 | size = le16_to_cpu(ep->desc.wMaxPacketSize); |
| 885 | |
| 886 | isoc_alts = uif->desc.bAlternateSetting; |
| 887 | |
| 888 | if (ep->desc.bEndpointAddress & USB_DIR_IN) |
| 889 | isoc_in_ep = ep; |
| 890 | else |
| 891 | isoc_out_ep = ep; |
| 892 | break; |
| 893 | } |
| 894 | } |
| 895 | } |
| 896 | |
| 897 | if (!isoc_in_ep || !isoc_out_ep) |
| 898 | BT_DBG("Isoc endpoints not found"); |
| 899 | else { |
| 900 | BT_DBG("isoc ifnum %d alts %d", isoc_ifnum, isoc_alts); |
| 901 | if (usb_driver_claim_interface(&hci_usb_driver, isoc_iface, husb) != 0) |
| 902 | BT_ERR("Can't claim isoc interface"); |
| 903 | else if (usb_set_interface(udev, isoc_ifnum, isoc_alts)) { |
| 904 | BT_ERR("Can't set isoc interface settings"); |
| 905 | husb->isoc_iface = isoc_iface; |
| 906 | usb_driver_release_interface(&hci_usb_driver, isoc_iface); |
| 907 | husb->isoc_iface = NULL; |
| 908 | } else { |
| 909 | husb->isoc_iface = isoc_iface; |
| 910 | husb->isoc_in_ep = isoc_in_ep; |
| 911 | husb->isoc_out_ep = isoc_out_ep; |
| 912 | } |
| 913 | } |
| 914 | } |
| 915 | #endif |
| 916 | |
| 917 | rwlock_init(&husb->completion_lock); |
| 918 | |
| 919 | for (i = 0; i < 4; i++) { |
| 920 | skb_queue_head_init(&husb->transmit_q[i]); |
| 921 | _urb_queue_init(&husb->pending_q[i]); |
| 922 | _urb_queue_init(&husb->completed_q[i]); |
| 923 | } |
| 924 | |
| 925 | /* Initialize and register HCI device */ |
| 926 | hdev = hci_alloc_dev(); |
| 927 | if (!hdev) { |
| 928 | BT_ERR("Can't allocate HCI device"); |
| 929 | goto probe_error; |
| 930 | } |
| 931 | |
| 932 | husb->hdev = hdev; |
| 933 | |
| 934 | hdev->type = HCI_USB; |
| 935 | hdev->driver_data = husb; |
| 936 | SET_HCIDEV_DEV(hdev, &intf->dev); |
| 937 | |
| 938 | hdev->open = hci_usb_open; |
| 939 | hdev->close = hci_usb_close; |
| 940 | hdev->flush = hci_usb_flush; |
| 941 | hdev->send = hci_usb_send_frame; |
| 942 | hdev->destruct = hci_usb_destruct; |
| 943 | hdev->notify = hci_usb_notify; |
| 944 | |
| 945 | hdev->owner = THIS_MODULE; |
| 946 | |
| 947 | if (reset || id->driver_info & HCI_RESET) |
| 948 | set_bit(HCI_QUIRK_RESET_ON_INIT, &hdev->quirks); |
| 949 | |
Marcel Holtmann | 520ca78 | 2006-07-14 16:01:52 +0200 | [diff] [blame] | 950 | if (force_scofix || id->driver_info & HCI_WRONG_SCO_MTU) { |
| 951 | if (!disable_scofix) |
| 952 | set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks); |
| 953 | } |
Marcel Holtmann | da1f519 | 2006-07-03 10:02:29 +0200 | [diff] [blame] | 954 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 955 | if (id->driver_info & HCI_SNIFFER) { |
| 956 | if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997) |
| 957 | set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks); |
| 958 | } |
| 959 | |
| 960 | if (id->driver_info & HCI_BCM92035) { |
| 961 | unsigned char cmd[] = { 0x3b, 0xfc, 0x01, 0x00 }; |
| 962 | struct sk_buff *skb; |
| 963 | |
| 964 | skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL); |
| 965 | if (skb) { |
| 966 | memcpy(skb_put(skb, sizeof(cmd)), cmd, sizeof(cmd)); |
| 967 | skb_queue_tail(&hdev->driver_init, skb); |
| 968 | } |
| 969 | } |
| 970 | |
| 971 | if (hci_register_dev(hdev) < 0) { |
| 972 | BT_ERR("Can't register HCI device"); |
| 973 | hci_free_dev(hdev); |
| 974 | goto probe_error; |
| 975 | } |
| 976 | |
| 977 | usb_set_intfdata(intf, husb); |
| 978 | return 0; |
| 979 | |
| 980 | probe_error: |
| 981 | if (husb->isoc_iface) |
| 982 | usb_driver_release_interface(&hci_usb_driver, husb->isoc_iface); |
| 983 | kfree(husb); |
| 984 | |
| 985 | done: |
| 986 | return -EIO; |
| 987 | } |
| 988 | |
| 989 | static void hci_usb_disconnect(struct usb_interface *intf) |
| 990 | { |
| 991 | struct hci_usb *husb = usb_get_intfdata(intf); |
| 992 | struct hci_dev *hdev; |
| 993 | |
| 994 | if (!husb || intf == husb->isoc_iface) |
| 995 | return; |
| 996 | |
| 997 | usb_set_intfdata(intf, NULL); |
| 998 | hdev = husb->hdev; |
| 999 | |
| 1000 | BT_DBG("%s", hdev->name); |
| 1001 | |
| 1002 | hci_usb_close(hdev); |
| 1003 | |
| 1004 | if (husb->isoc_iface) |
| 1005 | usb_driver_release_interface(&hci_usb_driver, husb->isoc_iface); |
| 1006 | |
| 1007 | if (hci_unregister_dev(hdev) < 0) |
| 1008 | BT_ERR("Can't unregister HCI device %s", hdev->name); |
| 1009 | |
| 1010 | hci_free_dev(hdev); |
| 1011 | } |
| 1012 | |
Marcel Holtmann | dcdcf63 | 2006-07-03 10:02:24 +0200 | [diff] [blame] | 1013 | static int hci_usb_suspend(struct usb_interface *intf, pm_message_t message) |
| 1014 | { |
| 1015 | struct hci_usb *husb = usb_get_intfdata(intf); |
| 1016 | struct list_head killed; |
| 1017 | unsigned long flags; |
| 1018 | int i; |
| 1019 | |
| 1020 | if (!husb || intf == husb->isoc_iface) |
| 1021 | return 0; |
| 1022 | |
| 1023 | hci_suspend_dev(husb->hdev); |
| 1024 | |
| 1025 | INIT_LIST_HEAD(&killed); |
| 1026 | |
| 1027 | for (i = 0; i < 4; i++) { |
| 1028 | struct _urb_queue *q = &husb->pending_q[i]; |
| 1029 | struct _urb *_urb, *_tmp; |
| 1030 | |
| 1031 | while ((_urb = _urb_dequeue(q))) { |
| 1032 | /* reset queue since _urb_dequeue sets it to NULL */ |
| 1033 | _urb->queue = q; |
| 1034 | usb_kill_urb(&_urb->urb); |
| 1035 | list_add(&_urb->list, &killed); |
| 1036 | } |
| 1037 | |
| 1038 | spin_lock_irqsave(&q->lock, flags); |
| 1039 | |
| 1040 | list_for_each_entry_safe(_urb, _tmp, &killed, list) { |
| 1041 | list_move_tail(&_urb->list, &q->head); |
| 1042 | } |
| 1043 | |
| 1044 | spin_unlock_irqrestore(&q->lock, flags); |
| 1045 | } |
| 1046 | |
| 1047 | return 0; |
| 1048 | } |
| 1049 | |
| 1050 | static int hci_usb_resume(struct usb_interface *intf) |
| 1051 | { |
| 1052 | struct hci_usb *husb = usb_get_intfdata(intf); |
| 1053 | unsigned long flags; |
| 1054 | int i, err = 0; |
| 1055 | |
| 1056 | if (!husb || intf == husb->isoc_iface) |
| 1057 | return 0; |
| 1058 | |
| 1059 | for (i = 0; i < 4; i++) { |
| 1060 | struct _urb_queue *q = &husb->pending_q[i]; |
| 1061 | struct _urb *_urb; |
| 1062 | |
| 1063 | spin_lock_irqsave(&q->lock, flags); |
| 1064 | |
| 1065 | list_for_each_entry(_urb, &q->head, list) { |
| 1066 | err = usb_submit_urb(&_urb->urb, GFP_ATOMIC); |
| 1067 | if (err) |
| 1068 | break; |
| 1069 | } |
| 1070 | |
| 1071 | spin_unlock_irqrestore(&q->lock, flags); |
| 1072 | |
| 1073 | if (err) |
| 1074 | return -EIO; |
| 1075 | } |
| 1076 | |
| 1077 | hci_resume_dev(husb->hdev); |
| 1078 | |
| 1079 | return 0; |
| 1080 | } |
| 1081 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1082 | static struct usb_driver hci_usb_driver = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1083 | .name = "hci_usb", |
| 1084 | .probe = hci_usb_probe, |
| 1085 | .disconnect = hci_usb_disconnect, |
Marcel Holtmann | dcdcf63 | 2006-07-03 10:02:24 +0200 | [diff] [blame] | 1086 | .suspend = hci_usb_suspend, |
| 1087 | .resume = hci_usb_resume, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1088 | .id_table = bluetooth_ids, |
| 1089 | }; |
| 1090 | |
| 1091 | static int __init hci_usb_init(void) |
| 1092 | { |
| 1093 | int err; |
| 1094 | |
| 1095 | BT_INFO("HCI USB driver ver %s", VERSION); |
| 1096 | |
| 1097 | if ((err = usb_register(&hci_usb_driver)) < 0) |
| 1098 | BT_ERR("Failed to register HCI USB driver"); |
| 1099 | |
| 1100 | return err; |
| 1101 | } |
| 1102 | |
| 1103 | static void __exit hci_usb_exit(void) |
| 1104 | { |
| 1105 | usb_deregister(&hci_usb_driver); |
| 1106 | } |
| 1107 | |
| 1108 | module_init(hci_usb_init); |
| 1109 | module_exit(hci_usb_exit); |
| 1110 | |
| 1111 | module_param(ignore, bool, 0644); |
| 1112 | MODULE_PARM_DESC(ignore, "Ignore devices from the matching table"); |
| 1113 | |
Marcel Holtmann | 7ef934b | 2005-11-08 09:57:05 -0800 | [diff] [blame] | 1114 | module_param(ignore_dga, bool, 0644); |
| 1115 | MODULE_PARM_DESC(ignore_dga, "Ignore devices with id 08fd:0001"); |
| 1116 | |
Marcel Holtmann | 0915e88 | 2005-09-13 01:32:37 +0200 | [diff] [blame] | 1117 | module_param(ignore_csr, bool, 0644); |
| 1118 | MODULE_PARM_DESC(ignore_csr, "Ignore devices with id 0a12:0001"); |
| 1119 | |
| 1120 | module_param(ignore_sniffer, bool, 0644); |
| 1121 | MODULE_PARM_DESC(ignore_sniffer, "Ignore devices with id 0a12:0002"); |
| 1122 | |
Marcel Holtmann | 520ca78 | 2006-07-14 16:01:52 +0200 | [diff] [blame] | 1123 | module_param(disable_scofix, bool, 0644); |
| 1124 | MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size"); |
| 1125 | |
| 1126 | module_param(force_scofix, bool, 0644); |
| 1127 | MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size"); |
| 1128 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1129 | module_param(reset, bool, 0644); |
| 1130 | MODULE_PARM_DESC(reset, "Send HCI reset command on initialization"); |
| 1131 | |
| 1132 | #ifdef CONFIG_BT_HCIUSB_SCO |
| 1133 | module_param(isoc, int, 0644); |
| 1134 | MODULE_PARM_DESC(isoc, "Set isochronous transfers for SCO over HCI support"); |
| 1135 | #endif |
| 1136 | |
| 1137 | MODULE_AUTHOR("Maxim Krasnyansky <maxk@qualcomm.com>, Marcel Holtmann <marcel@holtmann.org>"); |
| 1138 | MODULE_DESCRIPTION("Bluetooth HCI USB driver ver " VERSION); |
| 1139 | MODULE_VERSION(VERSION); |
| 1140 | MODULE_LICENSE("GPL"); |