Marcel Holtmann | 5e23b92 | 2007-10-20 14:12:34 +0200 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Generic Bluetooth USB driver |
| 4 | * |
Marcel Holtmann | 9bfa35f | 2008-08-18 13:23:52 +0200 | [diff] [blame] | 5 | * Copyright (C) 2005-2008 Marcel Holtmann <marcel@holtmann.org> |
Marcel Holtmann | 5e23b92 | 2007-10-20 14:12:34 +0200 | [diff] [blame] | 6 | * |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program; if not, write to the Free Software |
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 21 | * |
| 22 | */ |
| 23 | |
| 24 | #include <linux/kernel.h> |
| 25 | #include <linux/module.h> |
| 26 | #include <linux/init.h> |
| 27 | #include <linux/slab.h> |
| 28 | #include <linux/types.h> |
| 29 | #include <linux/sched.h> |
| 30 | #include <linux/errno.h> |
| 31 | #include <linux/skbuff.h> |
| 32 | |
| 33 | #include <linux/usb.h> |
| 34 | |
| 35 | #include <net/bluetooth/bluetooth.h> |
| 36 | #include <net/bluetooth/hci_core.h> |
| 37 | |
Oliver Neukum | 7bee549 | 2009-08-24 23:44:59 +0200 | [diff] [blame] | 38 | #define VERSION "0.6" |
Marcel Holtmann | cfeb414 | 2008-08-07 22:26:56 +0200 | [diff] [blame] | 39 | |
| 40 | static int ignore_dga; |
| 41 | static int ignore_csr; |
| 42 | static int ignore_sniffer; |
| 43 | static int disable_scofix; |
| 44 | static int force_scofix; |
Marcel Holtmann | 7a9d402 | 2008-11-30 12:17:26 +0100 | [diff] [blame] | 45 | |
| 46 | static int reset = 1; |
Marcel Holtmann | cfeb414 | 2008-08-07 22:26:56 +0200 | [diff] [blame] | 47 | |
| 48 | static struct usb_driver btusb_driver; |
| 49 | |
| 50 | #define BTUSB_IGNORE 0x01 |
Marcel Holtmann | 7a9d402 | 2008-11-30 12:17:26 +0100 | [diff] [blame] | 51 | #define BTUSB_DIGIANSWER 0x02 |
| 52 | #define BTUSB_CSR 0x04 |
| 53 | #define BTUSB_SNIFFER 0x08 |
| 54 | #define BTUSB_BCM92035 0x10 |
| 55 | #define BTUSB_BROKEN_ISOC 0x20 |
| 56 | #define BTUSB_WRONG_SCO_MTU 0x40 |
Marcel Holtmann | 5e23b92 | 2007-10-20 14:12:34 +0200 | [diff] [blame] | 57 | |
| 58 | static struct usb_device_id btusb_table[] = { |
| 59 | /* Generic Bluetooth USB device */ |
| 60 | { USB_DEVICE_INFO(0xe0, 0x01, 0x01) }, |
| 61 | |
Nobuhiro Iwamatsu | 3cd0197 | 2010-08-20 16:24:07 +0900 | [diff] [blame] | 62 | /* Apple MacBookPro 7,1 */ |
| 63 | { USB_DEVICE(0x05ac, 0x8213) }, |
| 64 | |
Cyril Lacoux | 0a79f67 | 2010-07-14 10:29:27 +0400 | [diff] [blame] | 65 | /* Apple iMac11,1 */ |
| 66 | { USB_DEVICE(0x05ac, 0x8215) }, |
| 67 | |
Nobuhiro Iwamatsu | 9c04715 | 2010-08-20 16:24:06 +0900 | [diff] [blame] | 68 | /* Apple MacBookPro6,2 */ |
| 69 | { USB_DEVICE(0x05ac, 0x8218) }, |
| 70 | |
Edgar (gimli) Hucek | 3e3ede7 | 2010-11-04 08:04:33 +0100 | [diff] [blame] | 71 | /* Apple MacBookAir3,1, MacBookAir3,2 */ |
| 72 | { USB_DEVICE(0x05ac, 0x821b) }, |
| 73 | |
Marcel Holtmann | cfeb414 | 2008-08-07 22:26:56 +0200 | [diff] [blame] | 74 | /* AVM BlueFRITZ! USB v2.0 */ |
| 75 | { USB_DEVICE(0x057c, 0x3800) }, |
| 76 | |
| 77 | /* Bluetooth Ultraport Module from IBM */ |
| 78 | { USB_DEVICE(0x04bf, 0x030a) }, |
| 79 | |
| 80 | /* ALPS Modules with non-standard id */ |
| 81 | { USB_DEVICE(0x044e, 0x3001) }, |
| 82 | { USB_DEVICE(0x044e, 0x3002) }, |
| 83 | |
| 84 | /* Ericsson with non-standard id */ |
| 85 | { USB_DEVICE(0x0bdb, 0x1002) }, |
| 86 | |
| 87 | /* Canyon CN-BTU1 with HID interfaces */ |
Marcel Holtmann | 7a9d402 | 2008-11-30 12:17:26 +0100 | [diff] [blame] | 88 | { USB_DEVICE(0x0c10, 0x0000) }, |
Marcel Holtmann | cfeb414 | 2008-08-07 22:26:56 +0200 | [diff] [blame] | 89 | |
Marcel Holtmann | 5e23b92 | 2007-10-20 14:12:34 +0200 | [diff] [blame] | 90 | { } /* Terminating entry */ |
| 91 | }; |
| 92 | |
| 93 | MODULE_DEVICE_TABLE(usb, btusb_table); |
| 94 | |
| 95 | static struct usb_device_id blacklist_table[] = { |
Marcel Holtmann | cfeb414 | 2008-08-07 22:26:56 +0200 | [diff] [blame] | 96 | /* CSR BlueCore devices */ |
| 97 | { USB_DEVICE(0x0a12, 0x0001), .driver_info = BTUSB_CSR }, |
| 98 | |
| 99 | /* Broadcom BCM2033 without firmware */ |
| 100 | { USB_DEVICE(0x0a5c, 0x2033), .driver_info = BTUSB_IGNORE }, |
| 101 | |
Bala Shanmugam | be93112 | 2010-11-26 17:35:46 +0530 | [diff] [blame] | 102 | /* Atheros 3011 with sflash firmware */ |
| 103 | { USB_DEVICE(0x0cf3, 0x3002), .driver_info = BTUSB_IGNORE }, |
| 104 | |
Marcel Holtmann | cfeb414 | 2008-08-07 22:26:56 +0200 | [diff] [blame] | 105 | /* Broadcom BCM2035 */ |
Marcel Holtmann | 7a9d402 | 2008-11-30 12:17:26 +0100 | [diff] [blame] | 106 | { USB_DEVICE(0x0a5c, 0x2035), .driver_info = BTUSB_WRONG_SCO_MTU }, |
| 107 | { USB_DEVICE(0x0a5c, 0x200a), .driver_info = BTUSB_WRONG_SCO_MTU }, |
| 108 | { USB_DEVICE(0x0a5c, 0x2009), .driver_info = BTUSB_BCM92035 }, |
Marcel Holtmann | cfeb414 | 2008-08-07 22:26:56 +0200 | [diff] [blame] | 109 | |
| 110 | /* Broadcom BCM2045 */ |
Marcel Holtmann | 7a9d402 | 2008-11-30 12:17:26 +0100 | [diff] [blame] | 111 | { USB_DEVICE(0x0a5c, 0x2039), .driver_info = BTUSB_WRONG_SCO_MTU }, |
| 112 | { USB_DEVICE(0x0a5c, 0x2101), .driver_info = BTUSB_WRONG_SCO_MTU }, |
Marcel Holtmann | bdbef3d | 2008-09-23 00:16:35 +0200 | [diff] [blame] | 113 | |
Marcel Holtmann | cfeb414 | 2008-08-07 22:26:56 +0200 | [diff] [blame] | 114 | /* IBM/Lenovo ThinkPad with Broadcom chip */ |
Marcel Holtmann | 7a9d402 | 2008-11-30 12:17:26 +0100 | [diff] [blame] | 115 | { USB_DEVICE(0x0a5c, 0x201e), .driver_info = BTUSB_WRONG_SCO_MTU }, |
| 116 | { USB_DEVICE(0x0a5c, 0x2110), .driver_info = BTUSB_WRONG_SCO_MTU }, |
Marcel Holtmann | cfeb414 | 2008-08-07 22:26:56 +0200 | [diff] [blame] | 117 | |
| 118 | /* HP laptop with Broadcom chip */ |
Marcel Holtmann | 7a9d402 | 2008-11-30 12:17:26 +0100 | [diff] [blame] | 119 | { USB_DEVICE(0x03f0, 0x171d), .driver_info = BTUSB_WRONG_SCO_MTU }, |
Marcel Holtmann | cfeb414 | 2008-08-07 22:26:56 +0200 | [diff] [blame] | 120 | |
| 121 | /* Dell laptop with Broadcom chip */ |
Marcel Holtmann | 7a9d402 | 2008-11-30 12:17:26 +0100 | [diff] [blame] | 122 | { USB_DEVICE(0x413c, 0x8126), .driver_info = BTUSB_WRONG_SCO_MTU }, |
Marcel Holtmann | cfeb414 | 2008-08-07 22:26:56 +0200 | [diff] [blame] | 123 | |
Marcel Holtmann | 5ddd4a6 | 2008-11-30 12:17:27 +0100 | [diff] [blame] | 124 | /* Dell Wireless 370 and 410 devices */ |
Marcel Holtmann | 7a9d402 | 2008-11-30 12:17:26 +0100 | [diff] [blame] | 125 | { USB_DEVICE(0x413c, 0x8152), .driver_info = BTUSB_WRONG_SCO_MTU }, |
Marcel Holtmann | 5ddd4a6 | 2008-11-30 12:17:27 +0100 | [diff] [blame] | 126 | { USB_DEVICE(0x413c, 0x8156), .driver_info = BTUSB_WRONG_SCO_MTU }, |
Marcel Holtmann | cfeb414 | 2008-08-07 22:26:56 +0200 | [diff] [blame] | 127 | |
Marcel Holtmann | 7a9d402 | 2008-11-30 12:17:26 +0100 | [diff] [blame] | 128 | /* Belkin F8T012 and F8T013 devices */ |
| 129 | { USB_DEVICE(0x050d, 0x0012), .driver_info = BTUSB_WRONG_SCO_MTU }, |
| 130 | { USB_DEVICE(0x050d, 0x0013), .driver_info = BTUSB_WRONG_SCO_MTU }, |
Marcel Holtmann | cfeb414 | 2008-08-07 22:26:56 +0200 | [diff] [blame] | 131 | |
Marcel Holtmann | 5ddd4a6 | 2008-11-30 12:17:27 +0100 | [diff] [blame] | 132 | /* Asus WL-BTD202 device */ |
| 133 | { USB_DEVICE(0x0b05, 0x1715), .driver_info = BTUSB_WRONG_SCO_MTU }, |
| 134 | |
| 135 | /* Kensington Bluetooth USB adapter */ |
| 136 | { USB_DEVICE(0x047d, 0x105e), .driver_info = BTUSB_WRONG_SCO_MTU }, |
| 137 | |
Marcel Holtmann | cfeb414 | 2008-08-07 22:26:56 +0200 | [diff] [blame] | 138 | /* RTX Telecom based adapters with buggy SCO support */ |
| 139 | { USB_DEVICE(0x0400, 0x0807), .driver_info = BTUSB_BROKEN_ISOC }, |
| 140 | { USB_DEVICE(0x0400, 0x080a), .driver_info = BTUSB_BROKEN_ISOC }, |
| 141 | |
| 142 | /* CONWISE Technology based adapters with buggy SCO support */ |
| 143 | { USB_DEVICE(0x0e5e, 0x6622), .driver_info = BTUSB_BROKEN_ISOC }, |
| 144 | |
Marcel Holtmann | cfeb414 | 2008-08-07 22:26:56 +0200 | [diff] [blame] | 145 | /* Digianswer devices */ |
| 146 | { USB_DEVICE(0x08fd, 0x0001), .driver_info = BTUSB_DIGIANSWER }, |
| 147 | { USB_DEVICE(0x08fd, 0x0002), .driver_info = BTUSB_IGNORE }, |
| 148 | |
| 149 | /* CSR BlueCore Bluetooth Sniffer */ |
| 150 | { USB_DEVICE(0x0a12, 0x0002), .driver_info = BTUSB_SNIFFER }, |
| 151 | |
| 152 | /* Frontline ComProbe Bluetooth Sniffer */ |
| 153 | { USB_DEVICE(0x16d3, 0x0002), .driver_info = BTUSB_SNIFFER }, |
| 154 | |
Marcel Holtmann | 5e23b92 | 2007-10-20 14:12:34 +0200 | [diff] [blame] | 155 | { } /* Terminating entry */ |
| 156 | }; |
| 157 | |
Marcel Holtmann | 9bfa35f | 2008-08-18 13:23:52 +0200 | [diff] [blame] | 158 | #define BTUSB_MAX_ISOC_FRAMES 10 |
| 159 | |
Marcel Holtmann | 5e23b92 | 2007-10-20 14:12:34 +0200 | [diff] [blame] | 160 | #define BTUSB_INTR_RUNNING 0 |
| 161 | #define BTUSB_BULK_RUNNING 1 |
Marcel Holtmann | 9bfa35f | 2008-08-18 13:23:52 +0200 | [diff] [blame] | 162 | #define BTUSB_ISOC_RUNNING 2 |
Oliver Neukum | 7bee549 | 2009-08-24 23:44:59 +0200 | [diff] [blame] | 163 | #define BTUSB_SUSPENDING 3 |
Gustavo F. Padovan | 08b8b6c | 2010-07-16 17:20:33 -0300 | [diff] [blame] | 164 | #define BTUSB_DID_ISO_RESUME 4 |
Marcel Holtmann | 5e23b92 | 2007-10-20 14:12:34 +0200 | [diff] [blame] | 165 | |
| 166 | struct btusb_data { |
| 167 | struct hci_dev *hdev; |
| 168 | struct usb_device *udev; |
Marcel Holtmann | 5fbcd26 | 2008-09-23 00:16:36 +0200 | [diff] [blame] | 169 | struct usb_interface *intf; |
Marcel Holtmann | 9bfa35f | 2008-08-18 13:23:52 +0200 | [diff] [blame] | 170 | struct usb_interface *isoc; |
Marcel Holtmann | 5e23b92 | 2007-10-20 14:12:34 +0200 | [diff] [blame] | 171 | |
| 172 | spinlock_t lock; |
| 173 | |
| 174 | unsigned long flags; |
| 175 | |
| 176 | struct work_struct work; |
Oliver Neukum | 7bee549 | 2009-08-24 23:44:59 +0200 | [diff] [blame] | 177 | struct work_struct waker; |
Marcel Holtmann | 5e23b92 | 2007-10-20 14:12:34 +0200 | [diff] [blame] | 178 | |
| 179 | struct usb_anchor tx_anchor; |
| 180 | struct usb_anchor intr_anchor; |
| 181 | struct usb_anchor bulk_anchor; |
Marcel Holtmann | 9bfa35f | 2008-08-18 13:23:52 +0200 | [diff] [blame] | 182 | struct usb_anchor isoc_anchor; |
Oliver Neukum | 7bee549 | 2009-08-24 23:44:59 +0200 | [diff] [blame] | 183 | struct usb_anchor deferred; |
| 184 | int tx_in_flight; |
| 185 | spinlock_t txlock; |
Marcel Holtmann | 5e23b92 | 2007-10-20 14:12:34 +0200 | [diff] [blame] | 186 | |
| 187 | struct usb_endpoint_descriptor *intr_ep; |
| 188 | struct usb_endpoint_descriptor *bulk_tx_ep; |
| 189 | struct usb_endpoint_descriptor *bulk_rx_ep; |
Marcel Holtmann | 9bfa35f | 2008-08-18 13:23:52 +0200 | [diff] [blame] | 190 | struct usb_endpoint_descriptor *isoc_tx_ep; |
| 191 | struct usb_endpoint_descriptor *isoc_rx_ep; |
| 192 | |
Marcel Holtmann | 7a9d402 | 2008-11-30 12:17:26 +0100 | [diff] [blame] | 193 | __u8 cmdreq_type; |
| 194 | |
Marcel Holtmann | 43c2e57 | 2009-02-04 17:41:38 +0100 | [diff] [blame] | 195 | unsigned int sco_num; |
Marcel Holtmann | 9bfa35f | 2008-08-18 13:23:52 +0200 | [diff] [blame] | 196 | int isoc_altsetting; |
Marcel Holtmann | 6a88adf | 2008-11-30 12:17:14 +0100 | [diff] [blame] | 197 | int suspend_count; |
Marcel Holtmann | 5e23b92 | 2007-10-20 14:12:34 +0200 | [diff] [blame] | 198 | }; |
| 199 | |
Oliver Neukum | 7bee549 | 2009-08-24 23:44:59 +0200 | [diff] [blame] | 200 | static int inc_tx(struct btusb_data *data) |
| 201 | { |
| 202 | unsigned long flags; |
| 203 | int rv; |
| 204 | |
| 205 | spin_lock_irqsave(&data->txlock, flags); |
| 206 | rv = test_bit(BTUSB_SUSPENDING, &data->flags); |
| 207 | if (!rv) |
| 208 | data->tx_in_flight++; |
| 209 | spin_unlock_irqrestore(&data->txlock, flags); |
| 210 | |
| 211 | return rv; |
| 212 | } |
| 213 | |
Marcel Holtmann | 5e23b92 | 2007-10-20 14:12:34 +0200 | [diff] [blame] | 214 | static void btusb_intr_complete(struct urb *urb) |
| 215 | { |
| 216 | struct hci_dev *hdev = urb->context; |
| 217 | struct btusb_data *data = hdev->driver_data; |
| 218 | int err; |
| 219 | |
| 220 | BT_DBG("%s urb %p status %d count %d", hdev->name, |
| 221 | urb, urb->status, urb->actual_length); |
| 222 | |
| 223 | if (!test_bit(HCI_RUNNING, &hdev->flags)) |
| 224 | return; |
| 225 | |
| 226 | if (urb->status == 0) { |
Marcel Holtmann | 9bfa35f | 2008-08-18 13:23:52 +0200 | [diff] [blame] | 227 | hdev->stat.byte_rx += urb->actual_length; |
| 228 | |
Marcel Holtmann | 5e23b92 | 2007-10-20 14:12:34 +0200 | [diff] [blame] | 229 | if (hci_recv_fragment(hdev, HCI_EVENT_PKT, |
| 230 | urb->transfer_buffer, |
| 231 | urb->actual_length) < 0) { |
| 232 | BT_ERR("%s corrupted event packet", hdev->name); |
| 233 | hdev->stat.err_rx++; |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | if (!test_bit(BTUSB_INTR_RUNNING, &data->flags)) |
| 238 | return; |
| 239 | |
Oliver Neukum | 7bee549 | 2009-08-24 23:44:59 +0200 | [diff] [blame] | 240 | usb_mark_last_busy(data->udev); |
Marcel Holtmann | 5e23b92 | 2007-10-20 14:12:34 +0200 | [diff] [blame] | 241 | usb_anchor_urb(urb, &data->intr_anchor); |
| 242 | |
| 243 | err = usb_submit_urb(urb, GFP_ATOMIC); |
| 244 | if (err < 0) { |
Stefan Seyfried | 61faddf | 2010-11-30 21:49:08 +0100 | [diff] [blame] | 245 | if (err != -EPERM) |
| 246 | BT_ERR("%s urb %p failed to resubmit (%d)", |
Marcel Holtmann | 5e23b92 | 2007-10-20 14:12:34 +0200 | [diff] [blame] | 247 | hdev->name, urb, -err); |
| 248 | usb_unanchor_urb(urb); |
| 249 | } |
| 250 | } |
| 251 | |
Marcel Holtmann | 2eda66f | 2008-11-30 12:17:10 +0100 | [diff] [blame] | 252 | static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags) |
Marcel Holtmann | 5e23b92 | 2007-10-20 14:12:34 +0200 | [diff] [blame] | 253 | { |
| 254 | struct btusb_data *data = hdev->driver_data; |
| 255 | struct urb *urb; |
| 256 | unsigned char *buf; |
| 257 | unsigned int pipe; |
| 258 | int err, size; |
| 259 | |
| 260 | BT_DBG("%s", hdev->name); |
| 261 | |
Marcel Holtmann | 9bfa35f | 2008-08-18 13:23:52 +0200 | [diff] [blame] | 262 | if (!data->intr_ep) |
| 263 | return -ENODEV; |
| 264 | |
Marcel Holtmann | 2eda66f | 2008-11-30 12:17:10 +0100 | [diff] [blame] | 265 | urb = usb_alloc_urb(0, mem_flags); |
Marcel Holtmann | 5e23b92 | 2007-10-20 14:12:34 +0200 | [diff] [blame] | 266 | if (!urb) |
| 267 | return -ENOMEM; |
| 268 | |
| 269 | size = le16_to_cpu(data->intr_ep->wMaxPacketSize); |
| 270 | |
Marcel Holtmann | 2eda66f | 2008-11-30 12:17:10 +0100 | [diff] [blame] | 271 | buf = kmalloc(size, mem_flags); |
Marcel Holtmann | 5e23b92 | 2007-10-20 14:12:34 +0200 | [diff] [blame] | 272 | if (!buf) { |
| 273 | usb_free_urb(urb); |
| 274 | return -ENOMEM; |
| 275 | } |
| 276 | |
| 277 | pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress); |
| 278 | |
| 279 | usb_fill_int_urb(urb, data->udev, pipe, buf, size, |
| 280 | btusb_intr_complete, hdev, |
| 281 | data->intr_ep->bInterval); |
| 282 | |
| 283 | urb->transfer_flags |= URB_FREE_BUFFER; |
| 284 | |
| 285 | usb_anchor_urb(urb, &data->intr_anchor); |
| 286 | |
Marcel Holtmann | 2eda66f | 2008-11-30 12:17:10 +0100 | [diff] [blame] | 287 | err = usb_submit_urb(urb, mem_flags); |
Marcel Holtmann | 5e23b92 | 2007-10-20 14:12:34 +0200 | [diff] [blame] | 288 | if (err < 0) { |
| 289 | BT_ERR("%s urb %p submission failed (%d)", |
| 290 | hdev->name, urb, -err); |
| 291 | usb_unanchor_urb(urb); |
Marcel Holtmann | 5e23b92 | 2007-10-20 14:12:34 +0200 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | usb_free_urb(urb); |
| 295 | |
| 296 | return err; |
| 297 | } |
| 298 | |
| 299 | static void btusb_bulk_complete(struct urb *urb) |
| 300 | { |
| 301 | struct hci_dev *hdev = urb->context; |
| 302 | struct btusb_data *data = hdev->driver_data; |
| 303 | int err; |
| 304 | |
| 305 | BT_DBG("%s urb %p status %d count %d", hdev->name, |
| 306 | urb, urb->status, urb->actual_length); |
| 307 | |
| 308 | if (!test_bit(HCI_RUNNING, &hdev->flags)) |
| 309 | return; |
| 310 | |
| 311 | if (urb->status == 0) { |
Marcel Holtmann | 9bfa35f | 2008-08-18 13:23:52 +0200 | [diff] [blame] | 312 | hdev->stat.byte_rx += urb->actual_length; |
| 313 | |
Marcel Holtmann | 5e23b92 | 2007-10-20 14:12:34 +0200 | [diff] [blame] | 314 | if (hci_recv_fragment(hdev, HCI_ACLDATA_PKT, |
| 315 | urb->transfer_buffer, |
| 316 | urb->actual_length) < 0) { |
| 317 | BT_ERR("%s corrupted ACL packet", hdev->name); |
| 318 | hdev->stat.err_rx++; |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | if (!test_bit(BTUSB_BULK_RUNNING, &data->flags)) |
| 323 | return; |
| 324 | |
| 325 | usb_anchor_urb(urb, &data->bulk_anchor); |
Oliver Neukum | 652fd78 | 2009-12-16 19:23:43 +0100 | [diff] [blame] | 326 | usb_mark_last_busy(data->udev); |
Marcel Holtmann | 5e23b92 | 2007-10-20 14:12:34 +0200 | [diff] [blame] | 327 | |
| 328 | err = usb_submit_urb(urb, GFP_ATOMIC); |
| 329 | if (err < 0) { |
Stefan Seyfried | 61faddf | 2010-11-30 21:49:08 +0100 | [diff] [blame] | 330 | if (err != -EPERM) |
| 331 | BT_ERR("%s urb %p failed to resubmit (%d)", |
Marcel Holtmann | 5e23b92 | 2007-10-20 14:12:34 +0200 | [diff] [blame] | 332 | hdev->name, urb, -err); |
| 333 | usb_unanchor_urb(urb); |
| 334 | } |
| 335 | } |
| 336 | |
Marcel Holtmann | 2eda66f | 2008-11-30 12:17:10 +0100 | [diff] [blame] | 337 | static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags) |
Marcel Holtmann | 5e23b92 | 2007-10-20 14:12:34 +0200 | [diff] [blame] | 338 | { |
| 339 | struct btusb_data *data = hdev->driver_data; |
| 340 | struct urb *urb; |
| 341 | unsigned char *buf; |
| 342 | unsigned int pipe; |
Vikram Kandukuri | 290ba20 | 2009-07-02 14:31:59 +0530 | [diff] [blame] | 343 | int err, size = HCI_MAX_FRAME_SIZE; |
Marcel Holtmann | 5e23b92 | 2007-10-20 14:12:34 +0200 | [diff] [blame] | 344 | |
| 345 | BT_DBG("%s", hdev->name); |
| 346 | |
Marcel Holtmann | 9bfa35f | 2008-08-18 13:23:52 +0200 | [diff] [blame] | 347 | if (!data->bulk_rx_ep) |
| 348 | return -ENODEV; |
| 349 | |
Marcel Holtmann | 2eda66f | 2008-11-30 12:17:10 +0100 | [diff] [blame] | 350 | urb = usb_alloc_urb(0, mem_flags); |
Marcel Holtmann | 5e23b92 | 2007-10-20 14:12:34 +0200 | [diff] [blame] | 351 | if (!urb) |
| 352 | return -ENOMEM; |
| 353 | |
Marcel Holtmann | 2eda66f | 2008-11-30 12:17:10 +0100 | [diff] [blame] | 354 | buf = kmalloc(size, mem_flags); |
Marcel Holtmann | 5e23b92 | 2007-10-20 14:12:34 +0200 | [diff] [blame] | 355 | if (!buf) { |
| 356 | usb_free_urb(urb); |
| 357 | return -ENOMEM; |
| 358 | } |
| 359 | |
| 360 | pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress); |
| 361 | |
| 362 | usb_fill_bulk_urb(urb, data->udev, pipe, |
| 363 | buf, size, btusb_bulk_complete, hdev); |
| 364 | |
| 365 | urb->transfer_flags |= URB_FREE_BUFFER; |
| 366 | |
Oliver Neukum | 7bee549 | 2009-08-24 23:44:59 +0200 | [diff] [blame] | 367 | usb_mark_last_busy(data->udev); |
Marcel Holtmann | 5e23b92 | 2007-10-20 14:12:34 +0200 | [diff] [blame] | 368 | usb_anchor_urb(urb, &data->bulk_anchor); |
| 369 | |
Marcel Holtmann | 2eda66f | 2008-11-30 12:17:10 +0100 | [diff] [blame] | 370 | err = usb_submit_urb(urb, mem_flags); |
Marcel Holtmann | 5e23b92 | 2007-10-20 14:12:34 +0200 | [diff] [blame] | 371 | if (err < 0) { |
| 372 | BT_ERR("%s urb %p submission failed (%d)", |
| 373 | hdev->name, urb, -err); |
| 374 | usb_unanchor_urb(urb); |
Marcel Holtmann | 5e23b92 | 2007-10-20 14:12:34 +0200 | [diff] [blame] | 375 | } |
| 376 | |
| 377 | usb_free_urb(urb); |
| 378 | |
| 379 | return err; |
| 380 | } |
| 381 | |
Marcel Holtmann | 9bfa35f | 2008-08-18 13:23:52 +0200 | [diff] [blame] | 382 | static void btusb_isoc_complete(struct urb *urb) |
| 383 | { |
| 384 | struct hci_dev *hdev = urb->context; |
| 385 | struct btusb_data *data = hdev->driver_data; |
| 386 | int i, err; |
| 387 | |
| 388 | BT_DBG("%s urb %p status %d count %d", hdev->name, |
| 389 | urb, urb->status, urb->actual_length); |
| 390 | |
| 391 | if (!test_bit(HCI_RUNNING, &hdev->flags)) |
| 392 | return; |
| 393 | |
| 394 | if (urb->status == 0) { |
| 395 | for (i = 0; i < urb->number_of_packets; i++) { |
| 396 | unsigned int offset = urb->iso_frame_desc[i].offset; |
| 397 | unsigned int length = urb->iso_frame_desc[i].actual_length; |
| 398 | |
| 399 | if (urb->iso_frame_desc[i].status) |
| 400 | continue; |
| 401 | |
| 402 | hdev->stat.byte_rx += length; |
| 403 | |
| 404 | if (hci_recv_fragment(hdev, HCI_SCODATA_PKT, |
| 405 | urb->transfer_buffer + offset, |
| 406 | length) < 0) { |
| 407 | BT_ERR("%s corrupted SCO packet", hdev->name); |
| 408 | hdev->stat.err_rx++; |
| 409 | } |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags)) |
| 414 | return; |
| 415 | |
| 416 | usb_anchor_urb(urb, &data->isoc_anchor); |
| 417 | |
| 418 | err = usb_submit_urb(urb, GFP_ATOMIC); |
| 419 | if (err < 0) { |
Stefan Seyfried | 61faddf | 2010-11-30 21:49:08 +0100 | [diff] [blame] | 420 | if (err != -EPERM) |
| 421 | BT_ERR("%s urb %p failed to resubmit (%d)", |
Marcel Holtmann | 9bfa35f | 2008-08-18 13:23:52 +0200 | [diff] [blame] | 422 | hdev->name, urb, -err); |
| 423 | usb_unanchor_urb(urb); |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | static void inline __fill_isoc_descriptor(struct urb *urb, int len, int mtu) |
| 428 | { |
| 429 | int i, offset = 0; |
| 430 | |
| 431 | BT_DBG("len %d mtu %d", len, mtu); |
| 432 | |
| 433 | for (i = 0; i < BTUSB_MAX_ISOC_FRAMES && len >= mtu; |
| 434 | i++, offset += mtu, len -= mtu) { |
| 435 | urb->iso_frame_desc[i].offset = offset; |
| 436 | urb->iso_frame_desc[i].length = mtu; |
| 437 | } |
| 438 | |
| 439 | if (len && i < BTUSB_MAX_ISOC_FRAMES) { |
| 440 | urb->iso_frame_desc[i].offset = offset; |
| 441 | urb->iso_frame_desc[i].length = len; |
| 442 | i++; |
| 443 | } |
| 444 | |
| 445 | urb->number_of_packets = i; |
| 446 | } |
| 447 | |
Marcel Holtmann | 2eda66f | 2008-11-30 12:17:10 +0100 | [diff] [blame] | 448 | static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags) |
Marcel Holtmann | 9bfa35f | 2008-08-18 13:23:52 +0200 | [diff] [blame] | 449 | { |
| 450 | struct btusb_data *data = hdev->driver_data; |
| 451 | struct urb *urb; |
| 452 | unsigned char *buf; |
| 453 | unsigned int pipe; |
| 454 | int err, size; |
| 455 | |
| 456 | BT_DBG("%s", hdev->name); |
| 457 | |
| 458 | if (!data->isoc_rx_ep) |
| 459 | return -ENODEV; |
| 460 | |
Marcel Holtmann | 2eda66f | 2008-11-30 12:17:10 +0100 | [diff] [blame] | 461 | urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags); |
Marcel Holtmann | 9bfa35f | 2008-08-18 13:23:52 +0200 | [diff] [blame] | 462 | if (!urb) |
| 463 | return -ENOMEM; |
| 464 | |
| 465 | size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) * |
| 466 | BTUSB_MAX_ISOC_FRAMES; |
| 467 | |
Marcel Holtmann | 2eda66f | 2008-11-30 12:17:10 +0100 | [diff] [blame] | 468 | buf = kmalloc(size, mem_flags); |
Marcel Holtmann | 9bfa35f | 2008-08-18 13:23:52 +0200 | [diff] [blame] | 469 | if (!buf) { |
| 470 | usb_free_urb(urb); |
| 471 | return -ENOMEM; |
| 472 | } |
| 473 | |
| 474 | pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress); |
| 475 | |
| 476 | urb->dev = data->udev; |
| 477 | urb->pipe = pipe; |
| 478 | urb->context = hdev; |
| 479 | urb->complete = btusb_isoc_complete; |
| 480 | urb->interval = data->isoc_rx_ep->bInterval; |
| 481 | |
| 482 | urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP; |
| 483 | urb->transfer_buffer = buf; |
| 484 | urb->transfer_buffer_length = size; |
| 485 | |
| 486 | __fill_isoc_descriptor(urb, size, |
| 487 | le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize)); |
| 488 | |
| 489 | usb_anchor_urb(urb, &data->isoc_anchor); |
| 490 | |
Marcel Holtmann | 2eda66f | 2008-11-30 12:17:10 +0100 | [diff] [blame] | 491 | err = usb_submit_urb(urb, mem_flags); |
Marcel Holtmann | 9bfa35f | 2008-08-18 13:23:52 +0200 | [diff] [blame] | 492 | if (err < 0) { |
| 493 | BT_ERR("%s urb %p submission failed (%d)", |
| 494 | hdev->name, urb, -err); |
| 495 | usb_unanchor_urb(urb); |
Marcel Holtmann | 9bfa35f | 2008-08-18 13:23:52 +0200 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | usb_free_urb(urb); |
| 499 | |
| 500 | return err; |
| 501 | } |
| 502 | |
Marcel Holtmann | 5e23b92 | 2007-10-20 14:12:34 +0200 | [diff] [blame] | 503 | static void btusb_tx_complete(struct urb *urb) |
| 504 | { |
| 505 | struct sk_buff *skb = urb->context; |
| 506 | struct hci_dev *hdev = (struct hci_dev *) skb->dev; |
Oliver Neukum | 7bee549 | 2009-08-24 23:44:59 +0200 | [diff] [blame] | 507 | struct btusb_data *data = hdev->driver_data; |
| 508 | |
| 509 | BT_DBG("%s urb %p status %d count %d", hdev->name, |
| 510 | urb, urb->status, urb->actual_length); |
| 511 | |
| 512 | if (!test_bit(HCI_RUNNING, &hdev->flags)) |
| 513 | goto done; |
| 514 | |
| 515 | if (!urb->status) |
| 516 | hdev->stat.byte_tx += urb->transfer_buffer_length; |
| 517 | else |
| 518 | hdev->stat.err_tx++; |
| 519 | |
| 520 | done: |
| 521 | spin_lock(&data->txlock); |
| 522 | data->tx_in_flight--; |
| 523 | spin_unlock(&data->txlock); |
| 524 | |
| 525 | kfree(urb->setup_packet); |
| 526 | |
| 527 | kfree_skb(skb); |
| 528 | } |
| 529 | |
| 530 | static void btusb_isoc_tx_complete(struct urb *urb) |
| 531 | { |
| 532 | struct sk_buff *skb = urb->context; |
| 533 | struct hci_dev *hdev = (struct hci_dev *) skb->dev; |
Marcel Holtmann | 5e23b92 | 2007-10-20 14:12:34 +0200 | [diff] [blame] | 534 | |
| 535 | BT_DBG("%s urb %p status %d count %d", hdev->name, |
| 536 | urb, urb->status, urb->actual_length); |
| 537 | |
| 538 | if (!test_bit(HCI_RUNNING, &hdev->flags)) |
| 539 | goto done; |
| 540 | |
| 541 | if (!urb->status) |
| 542 | hdev->stat.byte_tx += urb->transfer_buffer_length; |
| 543 | else |
| 544 | hdev->stat.err_tx++; |
| 545 | |
| 546 | done: |
| 547 | kfree(urb->setup_packet); |
| 548 | |
| 549 | kfree_skb(skb); |
| 550 | } |
| 551 | |
| 552 | static int btusb_open(struct hci_dev *hdev) |
| 553 | { |
| 554 | struct btusb_data *data = hdev->driver_data; |
| 555 | int err; |
| 556 | |
| 557 | BT_DBG("%s", hdev->name); |
| 558 | |
Oliver Neukum | 7bee549 | 2009-08-24 23:44:59 +0200 | [diff] [blame] | 559 | err = usb_autopm_get_interface(data->intf); |
| 560 | if (err < 0) |
| 561 | return err; |
| 562 | |
| 563 | data->intf->needs_remote_wakeup = 1; |
| 564 | |
Marcel Holtmann | 5e23b92 | 2007-10-20 14:12:34 +0200 | [diff] [blame] | 565 | if (test_and_set_bit(HCI_RUNNING, &hdev->flags)) |
Oliver Neukum | 7bee549 | 2009-08-24 23:44:59 +0200 | [diff] [blame] | 566 | goto done; |
Marcel Holtmann | 5e23b92 | 2007-10-20 14:12:34 +0200 | [diff] [blame] | 567 | |
| 568 | if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags)) |
Oliver Neukum | 7bee549 | 2009-08-24 23:44:59 +0200 | [diff] [blame] | 569 | goto done; |
Marcel Holtmann | 5e23b92 | 2007-10-20 14:12:34 +0200 | [diff] [blame] | 570 | |
Marcel Holtmann | 2eda66f | 2008-11-30 12:17:10 +0100 | [diff] [blame] | 571 | err = btusb_submit_intr_urb(hdev, GFP_KERNEL); |
Marcel Holtmann | 43c2e57 | 2009-02-04 17:41:38 +0100 | [diff] [blame] | 572 | if (err < 0) |
| 573 | goto failed; |
| 574 | |
| 575 | err = btusb_submit_bulk_urb(hdev, GFP_KERNEL); |
Marcel Holtmann | 5e23b92 | 2007-10-20 14:12:34 +0200 | [diff] [blame] | 576 | if (err < 0) { |
Marcel Holtmann | 43c2e57 | 2009-02-04 17:41:38 +0100 | [diff] [blame] | 577 | usb_kill_anchored_urbs(&data->intr_anchor); |
| 578 | goto failed; |
Marcel Holtmann | 5e23b92 | 2007-10-20 14:12:34 +0200 | [diff] [blame] | 579 | } |
| 580 | |
Marcel Holtmann | 43c2e57 | 2009-02-04 17:41:38 +0100 | [diff] [blame] | 581 | set_bit(BTUSB_BULK_RUNNING, &data->flags); |
| 582 | btusb_submit_bulk_urb(hdev, GFP_KERNEL); |
| 583 | |
Oliver Neukum | 7bee549 | 2009-08-24 23:44:59 +0200 | [diff] [blame] | 584 | done: |
| 585 | usb_autopm_put_interface(data->intf); |
Marcel Holtmann | 43c2e57 | 2009-02-04 17:41:38 +0100 | [diff] [blame] | 586 | return 0; |
| 587 | |
| 588 | failed: |
| 589 | clear_bit(BTUSB_INTR_RUNNING, &data->flags); |
| 590 | clear_bit(HCI_RUNNING, &hdev->flags); |
Oliver Neukum | 7bee549 | 2009-08-24 23:44:59 +0200 | [diff] [blame] | 591 | usb_autopm_put_interface(data->intf); |
Marcel Holtmann | 5e23b92 | 2007-10-20 14:12:34 +0200 | [diff] [blame] | 592 | return err; |
| 593 | } |
| 594 | |
Oliver Neukum | 7bee549 | 2009-08-24 23:44:59 +0200 | [diff] [blame] | 595 | static void btusb_stop_traffic(struct btusb_data *data) |
| 596 | { |
| 597 | usb_kill_anchored_urbs(&data->intr_anchor); |
| 598 | usb_kill_anchored_urbs(&data->bulk_anchor); |
| 599 | usb_kill_anchored_urbs(&data->isoc_anchor); |
| 600 | } |
| 601 | |
Marcel Holtmann | 5e23b92 | 2007-10-20 14:12:34 +0200 | [diff] [blame] | 602 | static int btusb_close(struct hci_dev *hdev) |
| 603 | { |
| 604 | struct btusb_data *data = hdev->driver_data; |
Oliver Neukum | 7bee549 | 2009-08-24 23:44:59 +0200 | [diff] [blame] | 605 | int err; |
Marcel Holtmann | 5e23b92 | 2007-10-20 14:12:34 +0200 | [diff] [blame] | 606 | |
| 607 | BT_DBG("%s", hdev->name); |
| 608 | |
| 609 | if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags)) |
| 610 | return 0; |
| 611 | |
Marcel Holtmann | e8c3c3d | 2008-09-23 00:16:36 +0200 | [diff] [blame] | 612 | cancel_work_sync(&data->work); |
Linus Torvalds | 404291a | 2009-11-11 13:32:29 -0800 | [diff] [blame] | 613 | cancel_work_sync(&data->waker); |
Marcel Holtmann | e8c3c3d | 2008-09-23 00:16:36 +0200 | [diff] [blame] | 614 | |
Marcel Holtmann | 9bfa35f | 2008-08-18 13:23:52 +0200 | [diff] [blame] | 615 | clear_bit(BTUSB_ISOC_RUNNING, &data->flags); |
Marcel Holtmann | 5e23b92 | 2007-10-20 14:12:34 +0200 | [diff] [blame] | 616 | clear_bit(BTUSB_BULK_RUNNING, &data->flags); |
Marcel Holtmann | 5e23b92 | 2007-10-20 14:12:34 +0200 | [diff] [blame] | 617 | clear_bit(BTUSB_INTR_RUNNING, &data->flags); |
Oliver Neukum | 7bee549 | 2009-08-24 23:44:59 +0200 | [diff] [blame] | 618 | |
| 619 | btusb_stop_traffic(data); |
| 620 | err = usb_autopm_get_interface(data->intf); |
| 621 | if (err < 0) |
Oliver Neukum | 7b8e2c1 | 2009-11-13 14:26:23 +0100 | [diff] [blame] | 622 | goto failed; |
Oliver Neukum | 7bee549 | 2009-08-24 23:44:59 +0200 | [diff] [blame] | 623 | |
| 624 | data->intf->needs_remote_wakeup = 0; |
| 625 | usb_autopm_put_interface(data->intf); |
Marcel Holtmann | 5e23b92 | 2007-10-20 14:12:34 +0200 | [diff] [blame] | 626 | |
Oliver Neukum | 7b8e2c1 | 2009-11-13 14:26:23 +0100 | [diff] [blame] | 627 | failed: |
| 628 | usb_scuttle_anchored_urbs(&data->deferred); |
Marcel Holtmann | 5e23b92 | 2007-10-20 14:12:34 +0200 | [diff] [blame] | 629 | return 0; |
| 630 | } |
| 631 | |
| 632 | static int btusb_flush(struct hci_dev *hdev) |
| 633 | { |
| 634 | struct btusb_data *data = hdev->driver_data; |
| 635 | |
| 636 | BT_DBG("%s", hdev->name); |
| 637 | |
| 638 | usb_kill_anchored_urbs(&data->tx_anchor); |
| 639 | |
| 640 | return 0; |
| 641 | } |
| 642 | |
| 643 | static int btusb_send_frame(struct sk_buff *skb) |
| 644 | { |
| 645 | struct hci_dev *hdev = (struct hci_dev *) skb->dev; |
| 646 | struct btusb_data *data = hdev->driver_data; |
| 647 | struct usb_ctrlrequest *dr; |
| 648 | struct urb *urb; |
| 649 | unsigned int pipe; |
| 650 | int err; |
| 651 | |
| 652 | BT_DBG("%s", hdev->name); |
| 653 | |
| 654 | if (!test_bit(HCI_RUNNING, &hdev->flags)) |
| 655 | return -EBUSY; |
| 656 | |
| 657 | switch (bt_cb(skb)->pkt_type) { |
| 658 | case HCI_COMMAND_PKT: |
| 659 | urb = usb_alloc_urb(0, GFP_ATOMIC); |
| 660 | if (!urb) |
| 661 | return -ENOMEM; |
| 662 | |
| 663 | dr = kmalloc(sizeof(*dr), GFP_ATOMIC); |
| 664 | if (!dr) { |
| 665 | usb_free_urb(urb); |
| 666 | return -ENOMEM; |
| 667 | } |
| 668 | |
Marcel Holtmann | 7a9d402 | 2008-11-30 12:17:26 +0100 | [diff] [blame] | 669 | dr->bRequestType = data->cmdreq_type; |
Marcel Holtmann | 5e23b92 | 2007-10-20 14:12:34 +0200 | [diff] [blame] | 670 | dr->bRequest = 0; |
| 671 | dr->wIndex = 0; |
| 672 | dr->wValue = 0; |
| 673 | dr->wLength = __cpu_to_le16(skb->len); |
| 674 | |
| 675 | pipe = usb_sndctrlpipe(data->udev, 0x00); |
| 676 | |
| 677 | usb_fill_control_urb(urb, data->udev, pipe, (void *) dr, |
| 678 | skb->data, skb->len, btusb_tx_complete, skb); |
| 679 | |
| 680 | hdev->stat.cmd_tx++; |
| 681 | break; |
| 682 | |
| 683 | case HCI_ACLDATA_PKT: |
Marcel Holtmann | 9bfa35f | 2008-08-18 13:23:52 +0200 | [diff] [blame] | 684 | if (!data->bulk_tx_ep || hdev->conn_hash.acl_num < 1) |
| 685 | return -ENODEV; |
| 686 | |
Marcel Holtmann | 5e23b92 | 2007-10-20 14:12:34 +0200 | [diff] [blame] | 687 | urb = usb_alloc_urb(0, GFP_ATOMIC); |
| 688 | if (!urb) |
| 689 | return -ENOMEM; |
| 690 | |
| 691 | pipe = usb_sndbulkpipe(data->udev, |
| 692 | data->bulk_tx_ep->bEndpointAddress); |
| 693 | |
| 694 | usb_fill_bulk_urb(urb, data->udev, pipe, |
| 695 | skb->data, skb->len, btusb_tx_complete, skb); |
| 696 | |
| 697 | hdev->stat.acl_tx++; |
| 698 | break; |
| 699 | |
| 700 | case HCI_SCODATA_PKT: |
Marcel Holtmann | 9bfa35f | 2008-08-18 13:23:52 +0200 | [diff] [blame] | 701 | if (!data->isoc_tx_ep || hdev->conn_hash.sco_num < 1) |
| 702 | return -ENODEV; |
| 703 | |
| 704 | urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_ATOMIC); |
| 705 | if (!urb) |
| 706 | return -ENOMEM; |
| 707 | |
| 708 | pipe = usb_sndisocpipe(data->udev, |
| 709 | data->isoc_tx_ep->bEndpointAddress); |
| 710 | |
| 711 | urb->dev = data->udev; |
| 712 | urb->pipe = pipe; |
| 713 | urb->context = skb; |
Oliver Neukum | 7bee549 | 2009-08-24 23:44:59 +0200 | [diff] [blame] | 714 | urb->complete = btusb_isoc_tx_complete; |
Marcel Holtmann | 9bfa35f | 2008-08-18 13:23:52 +0200 | [diff] [blame] | 715 | urb->interval = data->isoc_tx_ep->bInterval; |
| 716 | |
| 717 | urb->transfer_flags = URB_ISO_ASAP; |
| 718 | urb->transfer_buffer = skb->data; |
| 719 | urb->transfer_buffer_length = skb->len; |
| 720 | |
| 721 | __fill_isoc_descriptor(urb, skb->len, |
| 722 | le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize)); |
| 723 | |
Marcel Holtmann | 5e23b92 | 2007-10-20 14:12:34 +0200 | [diff] [blame] | 724 | hdev->stat.sco_tx++; |
Oliver Neukum | 7bee549 | 2009-08-24 23:44:59 +0200 | [diff] [blame] | 725 | goto skip_waking; |
Marcel Holtmann | 5e23b92 | 2007-10-20 14:12:34 +0200 | [diff] [blame] | 726 | |
| 727 | default: |
| 728 | return -EILSEQ; |
| 729 | } |
| 730 | |
Oliver Neukum | 7bee549 | 2009-08-24 23:44:59 +0200 | [diff] [blame] | 731 | err = inc_tx(data); |
| 732 | if (err) { |
| 733 | usb_anchor_urb(urb, &data->deferred); |
| 734 | schedule_work(&data->waker); |
| 735 | err = 0; |
| 736 | goto done; |
| 737 | } |
| 738 | |
| 739 | skip_waking: |
Marcel Holtmann | 5e23b92 | 2007-10-20 14:12:34 +0200 | [diff] [blame] | 740 | usb_anchor_urb(urb, &data->tx_anchor); |
| 741 | |
| 742 | err = usb_submit_urb(urb, GFP_ATOMIC); |
| 743 | if (err < 0) { |
| 744 | BT_ERR("%s urb %p submission failed", hdev->name, urb); |
| 745 | kfree(urb->setup_packet); |
| 746 | usb_unanchor_urb(urb); |
Oliver Neukum | 7bee549 | 2009-08-24 23:44:59 +0200 | [diff] [blame] | 747 | } else { |
| 748 | usb_mark_last_busy(data->udev); |
Marcel Holtmann | 5e23b92 | 2007-10-20 14:12:34 +0200 | [diff] [blame] | 749 | } |
| 750 | |
| 751 | usb_free_urb(urb); |
| 752 | |
Oliver Neukum | 7bee549 | 2009-08-24 23:44:59 +0200 | [diff] [blame] | 753 | done: |
Marcel Holtmann | 5e23b92 | 2007-10-20 14:12:34 +0200 | [diff] [blame] | 754 | return err; |
| 755 | } |
| 756 | |
| 757 | static void btusb_destruct(struct hci_dev *hdev) |
| 758 | { |
| 759 | struct btusb_data *data = hdev->driver_data; |
| 760 | |
| 761 | BT_DBG("%s", hdev->name); |
| 762 | |
| 763 | kfree(data); |
| 764 | } |
| 765 | |
| 766 | static void btusb_notify(struct hci_dev *hdev, unsigned int evt) |
| 767 | { |
| 768 | struct btusb_data *data = hdev->driver_data; |
| 769 | |
| 770 | BT_DBG("%s evt %d", hdev->name, evt); |
| 771 | |
Marcel Holtmann | 43c2e57 | 2009-02-04 17:41:38 +0100 | [diff] [blame] | 772 | if (hdev->conn_hash.sco_num != data->sco_num) { |
| 773 | data->sco_num = hdev->conn_hash.sco_num; |
| 774 | schedule_work(&data->work); |
Marcel Holtmann | a780efa | 2008-11-30 12:17:12 +0100 | [diff] [blame] | 775 | } |
Marcel Holtmann | 5e23b92 | 2007-10-20 14:12:34 +0200 | [diff] [blame] | 776 | } |
| 777 | |
Marcel Holtmann | 9bfa35f | 2008-08-18 13:23:52 +0200 | [diff] [blame] | 778 | static int inline __set_isoc_interface(struct hci_dev *hdev, int altsetting) |
| 779 | { |
| 780 | struct btusb_data *data = hdev->driver_data; |
| 781 | struct usb_interface *intf = data->isoc; |
| 782 | struct usb_endpoint_descriptor *ep_desc; |
| 783 | int i, err; |
| 784 | |
| 785 | if (!data->isoc) |
| 786 | return -ENODEV; |
| 787 | |
| 788 | err = usb_set_interface(data->udev, 1, altsetting); |
| 789 | if (err < 0) { |
| 790 | BT_ERR("%s setting interface failed (%d)", hdev->name, -err); |
| 791 | return err; |
| 792 | } |
| 793 | |
| 794 | data->isoc_altsetting = altsetting; |
| 795 | |
| 796 | data->isoc_tx_ep = NULL; |
| 797 | data->isoc_rx_ep = NULL; |
| 798 | |
| 799 | for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) { |
| 800 | ep_desc = &intf->cur_altsetting->endpoint[i].desc; |
| 801 | |
| 802 | if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) { |
| 803 | data->isoc_tx_ep = ep_desc; |
| 804 | continue; |
| 805 | } |
| 806 | |
| 807 | if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) { |
| 808 | data->isoc_rx_ep = ep_desc; |
| 809 | continue; |
| 810 | } |
| 811 | } |
| 812 | |
| 813 | if (!data->isoc_tx_ep || !data->isoc_rx_ep) { |
| 814 | BT_ERR("%s invalid SCO descriptors", hdev->name); |
| 815 | return -ENODEV; |
| 816 | } |
| 817 | |
| 818 | return 0; |
| 819 | } |
| 820 | |
Marcel Holtmann | 5e23b92 | 2007-10-20 14:12:34 +0200 | [diff] [blame] | 821 | static void btusb_work(struct work_struct *work) |
| 822 | { |
| 823 | struct btusb_data *data = container_of(work, struct btusb_data, work); |
| 824 | struct hci_dev *hdev = data->hdev; |
Oliver Neukum | 7bee549 | 2009-08-24 23:44:59 +0200 | [diff] [blame] | 825 | int err; |
Marcel Holtmann | 5e23b92 | 2007-10-20 14:12:34 +0200 | [diff] [blame] | 826 | |
Marcel Holtmann | 9bfa35f | 2008-08-18 13:23:52 +0200 | [diff] [blame] | 827 | if (hdev->conn_hash.sco_num > 0) { |
Gustavo F. Padovan | 08b8b6c | 2010-07-16 17:20:33 -0300 | [diff] [blame] | 828 | if (!test_bit(BTUSB_DID_ISO_RESUME, &data->flags)) { |
Oliver Neukum | 7bee549 | 2009-08-24 23:44:59 +0200 | [diff] [blame] | 829 | err = usb_autopm_get_interface(data->isoc); |
| 830 | if (err < 0) { |
| 831 | clear_bit(BTUSB_ISOC_RUNNING, &data->flags); |
| 832 | usb_kill_anchored_urbs(&data->isoc_anchor); |
| 833 | return; |
| 834 | } |
| 835 | |
Gustavo F. Padovan | 08b8b6c | 2010-07-16 17:20:33 -0300 | [diff] [blame] | 836 | set_bit(BTUSB_DID_ISO_RESUME, &data->flags); |
Oliver Neukum | 7bee549 | 2009-08-24 23:44:59 +0200 | [diff] [blame] | 837 | } |
Marcel Holtmann | 9bfa35f | 2008-08-18 13:23:52 +0200 | [diff] [blame] | 838 | if (data->isoc_altsetting != 2) { |
| 839 | clear_bit(BTUSB_ISOC_RUNNING, &data->flags); |
| 840 | usb_kill_anchored_urbs(&data->isoc_anchor); |
| 841 | |
| 842 | if (__set_isoc_interface(hdev, 2) < 0) |
| 843 | return; |
| 844 | } |
| 845 | |
| 846 | if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) { |
Marcel Holtmann | 2eda66f | 2008-11-30 12:17:10 +0100 | [diff] [blame] | 847 | if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0) |
Marcel Holtmann | 9bfa35f | 2008-08-18 13:23:52 +0200 | [diff] [blame] | 848 | clear_bit(BTUSB_ISOC_RUNNING, &data->flags); |
| 849 | else |
Marcel Holtmann | 2eda66f | 2008-11-30 12:17:10 +0100 | [diff] [blame] | 850 | btusb_submit_isoc_urb(hdev, GFP_KERNEL); |
Marcel Holtmann | 9bfa35f | 2008-08-18 13:23:52 +0200 | [diff] [blame] | 851 | } |
| 852 | } else { |
| 853 | clear_bit(BTUSB_ISOC_RUNNING, &data->flags); |
| 854 | usb_kill_anchored_urbs(&data->isoc_anchor); |
| 855 | |
| 856 | __set_isoc_interface(hdev, 0); |
Gustavo F. Padovan | 08b8b6c | 2010-07-16 17:20:33 -0300 | [diff] [blame] | 857 | if (test_and_clear_bit(BTUSB_DID_ISO_RESUME, &data->flags)) |
Oliver Neukum | 7bee549 | 2009-08-24 23:44:59 +0200 | [diff] [blame] | 858 | usb_autopm_put_interface(data->isoc); |
Marcel Holtmann | 5e23b92 | 2007-10-20 14:12:34 +0200 | [diff] [blame] | 859 | } |
| 860 | } |
| 861 | |
Oliver Neukum | 7bee549 | 2009-08-24 23:44:59 +0200 | [diff] [blame] | 862 | static void btusb_waker(struct work_struct *work) |
| 863 | { |
| 864 | struct btusb_data *data = container_of(work, struct btusb_data, waker); |
| 865 | int err; |
| 866 | |
| 867 | err = usb_autopm_get_interface(data->intf); |
| 868 | if (err < 0) |
| 869 | return; |
| 870 | |
| 871 | usb_autopm_put_interface(data->intf); |
| 872 | } |
| 873 | |
Marcel Holtmann | 5e23b92 | 2007-10-20 14:12:34 +0200 | [diff] [blame] | 874 | static int btusb_probe(struct usb_interface *intf, |
| 875 | const struct usb_device_id *id) |
| 876 | { |
| 877 | struct usb_endpoint_descriptor *ep_desc; |
| 878 | struct btusb_data *data; |
| 879 | struct hci_dev *hdev; |
| 880 | int i, err; |
| 881 | |
| 882 | BT_DBG("intf %p id %p", intf, id); |
| 883 | |
Marcel Holtmann | cfeb414 | 2008-08-07 22:26:56 +0200 | [diff] [blame] | 884 | /* interface numbers are hardcoded in the spec */ |
Marcel Holtmann | 5e23b92 | 2007-10-20 14:12:34 +0200 | [diff] [blame] | 885 | if (intf->cur_altsetting->desc.bInterfaceNumber != 0) |
| 886 | return -ENODEV; |
| 887 | |
| 888 | if (!id->driver_info) { |
| 889 | const struct usb_device_id *match; |
| 890 | match = usb_match_id(intf, blacklist_table); |
| 891 | if (match) |
| 892 | id = match; |
| 893 | } |
| 894 | |
Marcel Holtmann | cfeb414 | 2008-08-07 22:26:56 +0200 | [diff] [blame] | 895 | if (id->driver_info == BTUSB_IGNORE) |
| 896 | return -ENODEV; |
| 897 | |
| 898 | if (ignore_dga && id->driver_info & BTUSB_DIGIANSWER) |
| 899 | return -ENODEV; |
| 900 | |
| 901 | if (ignore_csr && id->driver_info & BTUSB_CSR) |
| 902 | return -ENODEV; |
| 903 | |
| 904 | if (ignore_sniffer && id->driver_info & BTUSB_SNIFFER) |
| 905 | return -ENODEV; |
| 906 | |
Marcel Holtmann | 5e23b92 | 2007-10-20 14:12:34 +0200 | [diff] [blame] | 907 | data = kzalloc(sizeof(*data), GFP_KERNEL); |
| 908 | if (!data) |
| 909 | return -ENOMEM; |
| 910 | |
| 911 | for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) { |
| 912 | ep_desc = &intf->cur_altsetting->endpoint[i].desc; |
| 913 | |
| 914 | if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) { |
| 915 | data->intr_ep = ep_desc; |
| 916 | continue; |
| 917 | } |
| 918 | |
| 919 | if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) { |
| 920 | data->bulk_tx_ep = ep_desc; |
| 921 | continue; |
| 922 | } |
| 923 | |
| 924 | if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) { |
| 925 | data->bulk_rx_ep = ep_desc; |
| 926 | continue; |
| 927 | } |
| 928 | } |
| 929 | |
| 930 | if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep) { |
| 931 | kfree(data); |
| 932 | return -ENODEV; |
| 933 | } |
| 934 | |
Marcel Holtmann | 7a9d402 | 2008-11-30 12:17:26 +0100 | [diff] [blame] | 935 | data->cmdreq_type = USB_TYPE_CLASS; |
| 936 | |
Marcel Holtmann | 5e23b92 | 2007-10-20 14:12:34 +0200 | [diff] [blame] | 937 | data->udev = interface_to_usbdev(intf); |
Marcel Holtmann | 5fbcd26 | 2008-09-23 00:16:36 +0200 | [diff] [blame] | 938 | data->intf = intf; |
Marcel Holtmann | 5e23b92 | 2007-10-20 14:12:34 +0200 | [diff] [blame] | 939 | |
| 940 | spin_lock_init(&data->lock); |
| 941 | |
| 942 | INIT_WORK(&data->work, btusb_work); |
Oliver Neukum | 7bee549 | 2009-08-24 23:44:59 +0200 | [diff] [blame] | 943 | INIT_WORK(&data->waker, btusb_waker); |
| 944 | spin_lock_init(&data->txlock); |
Marcel Holtmann | 5e23b92 | 2007-10-20 14:12:34 +0200 | [diff] [blame] | 945 | |
| 946 | init_usb_anchor(&data->tx_anchor); |
| 947 | init_usb_anchor(&data->intr_anchor); |
| 948 | init_usb_anchor(&data->bulk_anchor); |
Marcel Holtmann | 9bfa35f | 2008-08-18 13:23:52 +0200 | [diff] [blame] | 949 | init_usb_anchor(&data->isoc_anchor); |
Oliver Neukum | 7bee549 | 2009-08-24 23:44:59 +0200 | [diff] [blame] | 950 | init_usb_anchor(&data->deferred); |
Marcel Holtmann | 5e23b92 | 2007-10-20 14:12:34 +0200 | [diff] [blame] | 951 | |
| 952 | hdev = hci_alloc_dev(); |
| 953 | if (!hdev) { |
| 954 | kfree(data); |
| 955 | return -ENOMEM; |
| 956 | } |
| 957 | |
Marcel Holtmann | c13854c | 2010-02-08 15:27:07 +0100 | [diff] [blame] | 958 | hdev->bus = HCI_USB; |
Marcel Holtmann | 5e23b92 | 2007-10-20 14:12:34 +0200 | [diff] [blame] | 959 | hdev->driver_data = data; |
| 960 | |
| 961 | data->hdev = hdev; |
| 962 | |
| 963 | SET_HCIDEV_DEV(hdev, &intf->dev); |
| 964 | |
| 965 | hdev->open = btusb_open; |
| 966 | hdev->close = btusb_close; |
| 967 | hdev->flush = btusb_flush; |
| 968 | hdev->send = btusb_send_frame; |
| 969 | hdev->destruct = btusb_destruct; |
| 970 | hdev->notify = btusb_notify; |
| 971 | |
| 972 | hdev->owner = THIS_MODULE; |
| 973 | |
Marcel Holtmann | 7a9d402 | 2008-11-30 12:17:26 +0100 | [diff] [blame] | 974 | /* Interface numbers are hardcoded in the specification */ |
Marcel Holtmann | 9bfa35f | 2008-08-18 13:23:52 +0200 | [diff] [blame] | 975 | data->isoc = usb_ifnum_to_if(data->udev, 1); |
| 976 | |
Marcel Holtmann | 7a9d402 | 2008-11-30 12:17:26 +0100 | [diff] [blame] | 977 | if (!reset) |
| 978 | set_bit(HCI_QUIRK_NO_RESET, &hdev->quirks); |
Marcel Holtmann | cfeb414 | 2008-08-07 22:26:56 +0200 | [diff] [blame] | 979 | |
| 980 | if (force_scofix || id->driver_info & BTUSB_WRONG_SCO_MTU) { |
| 981 | if (!disable_scofix) |
| 982 | set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks); |
| 983 | } |
| 984 | |
Marcel Holtmann | 9bfa35f | 2008-08-18 13:23:52 +0200 | [diff] [blame] | 985 | if (id->driver_info & BTUSB_BROKEN_ISOC) |
| 986 | data->isoc = NULL; |
| 987 | |
Marcel Holtmann | 7a9d402 | 2008-11-30 12:17:26 +0100 | [diff] [blame] | 988 | if (id->driver_info & BTUSB_DIGIANSWER) { |
| 989 | data->cmdreq_type = USB_TYPE_VENDOR; |
| 990 | set_bit(HCI_QUIRK_NO_RESET, &hdev->quirks); |
| 991 | } |
| 992 | |
| 993 | if (id->driver_info & BTUSB_CSR) { |
| 994 | struct usb_device *udev = data->udev; |
| 995 | |
| 996 | /* Old firmware would otherwise execute USB reset */ |
| 997 | if (le16_to_cpu(udev->descriptor.bcdDevice) < 0x117) |
| 998 | set_bit(HCI_QUIRK_NO_RESET, &hdev->quirks); |
| 999 | } |
| 1000 | |
Marcel Holtmann | cfeb414 | 2008-08-07 22:26:56 +0200 | [diff] [blame] | 1001 | if (id->driver_info & BTUSB_SNIFFER) { |
Marcel Holtmann | 9bfa35f | 2008-08-18 13:23:52 +0200 | [diff] [blame] | 1002 | struct usb_device *udev = data->udev; |
Marcel Holtmann | cfeb414 | 2008-08-07 22:26:56 +0200 | [diff] [blame] | 1003 | |
Marcel Holtmann | 7a9d402 | 2008-11-30 12:17:26 +0100 | [diff] [blame] | 1004 | /* New sniffer firmware has crippled HCI interface */ |
Marcel Holtmann | cfeb414 | 2008-08-07 22:26:56 +0200 | [diff] [blame] | 1005 | if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997) |
| 1006 | set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks); |
Marcel Holtmann | 9bfa35f | 2008-08-18 13:23:52 +0200 | [diff] [blame] | 1007 | |
| 1008 | data->isoc = NULL; |
Marcel Holtmann | cfeb414 | 2008-08-07 22:26:56 +0200 | [diff] [blame] | 1009 | } |
| 1010 | |
| 1011 | if (id->driver_info & BTUSB_BCM92035) { |
| 1012 | unsigned char cmd[] = { 0x3b, 0xfc, 0x01, 0x00 }; |
| 1013 | struct sk_buff *skb; |
| 1014 | |
| 1015 | skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL); |
| 1016 | if (skb) { |
| 1017 | memcpy(skb_put(skb, sizeof(cmd)), cmd, sizeof(cmd)); |
| 1018 | skb_queue_tail(&hdev->driver_init, skb); |
| 1019 | } |
| 1020 | } |
Marcel Holtmann | 5e23b92 | 2007-10-20 14:12:34 +0200 | [diff] [blame] | 1021 | |
Marcel Holtmann | 9bfa35f | 2008-08-18 13:23:52 +0200 | [diff] [blame] | 1022 | if (data->isoc) { |
| 1023 | err = usb_driver_claim_interface(&btusb_driver, |
Marcel Holtmann | 5fbcd26 | 2008-09-23 00:16:36 +0200 | [diff] [blame] | 1024 | data->isoc, data); |
Marcel Holtmann | 9bfa35f | 2008-08-18 13:23:52 +0200 | [diff] [blame] | 1025 | if (err < 0) { |
| 1026 | hci_free_dev(hdev); |
| 1027 | kfree(data); |
| 1028 | return err; |
| 1029 | } |
| 1030 | } |
| 1031 | |
Marcel Holtmann | 5e23b92 | 2007-10-20 14:12:34 +0200 | [diff] [blame] | 1032 | err = hci_register_dev(hdev); |
| 1033 | if (err < 0) { |
| 1034 | hci_free_dev(hdev); |
| 1035 | kfree(data); |
| 1036 | return err; |
| 1037 | } |
| 1038 | |
| 1039 | usb_set_intfdata(intf, data); |
| 1040 | |
Matthew Garrett | 556ea92 | 2010-09-16 13:58:15 -0400 | [diff] [blame] | 1041 | usb_enable_autosuspend(interface_to_usbdev(intf)); |
| 1042 | |
Marcel Holtmann | 5e23b92 | 2007-10-20 14:12:34 +0200 | [diff] [blame] | 1043 | return 0; |
| 1044 | } |
| 1045 | |
| 1046 | static void btusb_disconnect(struct usb_interface *intf) |
| 1047 | { |
| 1048 | struct btusb_data *data = usb_get_intfdata(intf); |
| 1049 | struct hci_dev *hdev; |
| 1050 | |
| 1051 | BT_DBG("intf %p", intf); |
| 1052 | |
| 1053 | if (!data) |
| 1054 | return; |
| 1055 | |
| 1056 | hdev = data->hdev; |
| 1057 | |
Marcel Holtmann | 5fbcd26 | 2008-09-23 00:16:36 +0200 | [diff] [blame] | 1058 | __hci_dev_hold(hdev); |
Marcel Holtmann | 9bfa35f | 2008-08-18 13:23:52 +0200 | [diff] [blame] | 1059 | |
Marcel Holtmann | 5fbcd26 | 2008-09-23 00:16:36 +0200 | [diff] [blame] | 1060 | usb_set_intfdata(data->intf, NULL); |
| 1061 | |
| 1062 | if (data->isoc) |
| 1063 | usb_set_intfdata(data->isoc, NULL); |
Marcel Holtmann | 5e23b92 | 2007-10-20 14:12:34 +0200 | [diff] [blame] | 1064 | |
| 1065 | hci_unregister_dev(hdev); |
| 1066 | |
Marcel Holtmann | 5fbcd26 | 2008-09-23 00:16:36 +0200 | [diff] [blame] | 1067 | if (intf == data->isoc) |
| 1068 | usb_driver_release_interface(&btusb_driver, data->intf); |
| 1069 | else if (data->isoc) |
| 1070 | usb_driver_release_interface(&btusb_driver, data->isoc); |
| 1071 | |
| 1072 | __hci_dev_put(hdev); |
| 1073 | |
Marcel Holtmann | 5e23b92 | 2007-10-20 14:12:34 +0200 | [diff] [blame] | 1074 | hci_free_dev(hdev); |
| 1075 | } |
| 1076 | |
Oliver Neukum | 7bee549 | 2009-08-24 23:44:59 +0200 | [diff] [blame] | 1077 | #ifdef CONFIG_PM |
Marcel Holtmann | 6a88adf | 2008-11-30 12:17:14 +0100 | [diff] [blame] | 1078 | static int btusb_suspend(struct usb_interface *intf, pm_message_t message) |
| 1079 | { |
| 1080 | struct btusb_data *data = usb_get_intfdata(intf); |
| 1081 | |
| 1082 | BT_DBG("intf %p", intf); |
| 1083 | |
| 1084 | if (data->suspend_count++) |
| 1085 | return 0; |
| 1086 | |
Oliver Neukum | 7bee549 | 2009-08-24 23:44:59 +0200 | [diff] [blame] | 1087 | spin_lock_irq(&data->txlock); |
Alan Stern | fb34d53 | 2009-11-13 11:53:59 -0500 | [diff] [blame] | 1088 | if (!((message.event & PM_EVENT_AUTO) && data->tx_in_flight)) { |
Oliver Neukum | 7bee549 | 2009-08-24 23:44:59 +0200 | [diff] [blame] | 1089 | set_bit(BTUSB_SUSPENDING, &data->flags); |
| 1090 | spin_unlock_irq(&data->txlock); |
| 1091 | } else { |
| 1092 | spin_unlock_irq(&data->txlock); |
| 1093 | data->suspend_count--; |
| 1094 | return -EBUSY; |
| 1095 | } |
| 1096 | |
Marcel Holtmann | 6a88adf | 2008-11-30 12:17:14 +0100 | [diff] [blame] | 1097 | cancel_work_sync(&data->work); |
| 1098 | |
Oliver Neukum | 7bee549 | 2009-08-24 23:44:59 +0200 | [diff] [blame] | 1099 | btusb_stop_traffic(data); |
Marcel Holtmann | 6a88adf | 2008-11-30 12:17:14 +0100 | [diff] [blame] | 1100 | usb_kill_anchored_urbs(&data->tx_anchor); |
| 1101 | |
Marcel Holtmann | 6a88adf | 2008-11-30 12:17:14 +0100 | [diff] [blame] | 1102 | return 0; |
| 1103 | } |
| 1104 | |
Oliver Neukum | 7bee549 | 2009-08-24 23:44:59 +0200 | [diff] [blame] | 1105 | static void play_deferred(struct btusb_data *data) |
| 1106 | { |
| 1107 | struct urb *urb; |
| 1108 | int err; |
| 1109 | |
| 1110 | while ((urb = usb_get_from_anchor(&data->deferred))) { |
| 1111 | err = usb_submit_urb(urb, GFP_ATOMIC); |
| 1112 | if (err < 0) |
| 1113 | break; |
| 1114 | |
| 1115 | data->tx_in_flight++; |
| 1116 | } |
| 1117 | usb_scuttle_anchored_urbs(&data->deferred); |
| 1118 | } |
| 1119 | |
Marcel Holtmann | 6a88adf | 2008-11-30 12:17:14 +0100 | [diff] [blame] | 1120 | static int btusb_resume(struct usb_interface *intf) |
| 1121 | { |
| 1122 | struct btusb_data *data = usb_get_intfdata(intf); |
| 1123 | struct hci_dev *hdev = data->hdev; |
Oliver Neukum | 7bee549 | 2009-08-24 23:44:59 +0200 | [diff] [blame] | 1124 | int err = 0; |
Marcel Holtmann | 6a88adf | 2008-11-30 12:17:14 +0100 | [diff] [blame] | 1125 | |
| 1126 | BT_DBG("intf %p", intf); |
| 1127 | |
| 1128 | if (--data->suspend_count) |
| 1129 | return 0; |
| 1130 | |
| 1131 | if (!test_bit(HCI_RUNNING, &hdev->flags)) |
Oliver Neukum | 7bee549 | 2009-08-24 23:44:59 +0200 | [diff] [blame] | 1132 | goto done; |
Marcel Holtmann | 6a88adf | 2008-11-30 12:17:14 +0100 | [diff] [blame] | 1133 | |
| 1134 | if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) { |
| 1135 | err = btusb_submit_intr_urb(hdev, GFP_NOIO); |
| 1136 | if (err < 0) { |
| 1137 | clear_bit(BTUSB_INTR_RUNNING, &data->flags); |
Oliver Neukum | 7bee549 | 2009-08-24 23:44:59 +0200 | [diff] [blame] | 1138 | goto failed; |
Marcel Holtmann | 6a88adf | 2008-11-30 12:17:14 +0100 | [diff] [blame] | 1139 | } |
| 1140 | } |
| 1141 | |
| 1142 | if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) { |
Marcel Holtmann | 43c2e57 | 2009-02-04 17:41:38 +0100 | [diff] [blame] | 1143 | err = btusb_submit_bulk_urb(hdev, GFP_NOIO); |
| 1144 | if (err < 0) { |
Marcel Holtmann | 6a88adf | 2008-11-30 12:17:14 +0100 | [diff] [blame] | 1145 | clear_bit(BTUSB_BULK_RUNNING, &data->flags); |
Oliver Neukum | 7bee549 | 2009-08-24 23:44:59 +0200 | [diff] [blame] | 1146 | goto failed; |
| 1147 | } |
| 1148 | |
| 1149 | btusb_submit_bulk_urb(hdev, GFP_NOIO); |
Marcel Holtmann | 6a88adf | 2008-11-30 12:17:14 +0100 | [diff] [blame] | 1150 | } |
| 1151 | |
| 1152 | if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) { |
| 1153 | if (btusb_submit_isoc_urb(hdev, GFP_NOIO) < 0) |
| 1154 | clear_bit(BTUSB_ISOC_RUNNING, &data->flags); |
| 1155 | else |
| 1156 | btusb_submit_isoc_urb(hdev, GFP_NOIO); |
| 1157 | } |
| 1158 | |
Oliver Neukum | 7bee549 | 2009-08-24 23:44:59 +0200 | [diff] [blame] | 1159 | spin_lock_irq(&data->txlock); |
| 1160 | play_deferred(data); |
| 1161 | clear_bit(BTUSB_SUSPENDING, &data->flags); |
| 1162 | spin_unlock_irq(&data->txlock); |
| 1163 | schedule_work(&data->work); |
| 1164 | |
Marcel Holtmann | 6a88adf | 2008-11-30 12:17:14 +0100 | [diff] [blame] | 1165 | return 0; |
Oliver Neukum | 7bee549 | 2009-08-24 23:44:59 +0200 | [diff] [blame] | 1166 | |
| 1167 | failed: |
| 1168 | usb_scuttle_anchored_urbs(&data->deferred); |
| 1169 | done: |
| 1170 | spin_lock_irq(&data->txlock); |
| 1171 | clear_bit(BTUSB_SUSPENDING, &data->flags); |
| 1172 | spin_unlock_irq(&data->txlock); |
| 1173 | |
| 1174 | return err; |
Marcel Holtmann | 6a88adf | 2008-11-30 12:17:14 +0100 | [diff] [blame] | 1175 | } |
Oliver Neukum | 7bee549 | 2009-08-24 23:44:59 +0200 | [diff] [blame] | 1176 | #endif |
Marcel Holtmann | 6a88adf | 2008-11-30 12:17:14 +0100 | [diff] [blame] | 1177 | |
Marcel Holtmann | 5e23b92 | 2007-10-20 14:12:34 +0200 | [diff] [blame] | 1178 | static struct usb_driver btusb_driver = { |
| 1179 | .name = "btusb", |
| 1180 | .probe = btusb_probe, |
| 1181 | .disconnect = btusb_disconnect, |
Oliver Neukum | 7bee549 | 2009-08-24 23:44:59 +0200 | [diff] [blame] | 1182 | #ifdef CONFIG_PM |
Marcel Holtmann | 6a88adf | 2008-11-30 12:17:14 +0100 | [diff] [blame] | 1183 | .suspend = btusb_suspend, |
| 1184 | .resume = btusb_resume, |
Oliver Neukum | 7bee549 | 2009-08-24 23:44:59 +0200 | [diff] [blame] | 1185 | #endif |
Marcel Holtmann | 5e23b92 | 2007-10-20 14:12:34 +0200 | [diff] [blame] | 1186 | .id_table = btusb_table, |
Oliver Neukum | 7bee549 | 2009-08-24 23:44:59 +0200 | [diff] [blame] | 1187 | .supports_autosuspend = 1, |
Marcel Holtmann | 5e23b92 | 2007-10-20 14:12:34 +0200 | [diff] [blame] | 1188 | }; |
| 1189 | |
| 1190 | static int __init btusb_init(void) |
| 1191 | { |
| 1192 | BT_INFO("Generic Bluetooth USB driver ver %s", VERSION); |
| 1193 | |
| 1194 | return usb_register(&btusb_driver); |
| 1195 | } |
| 1196 | |
| 1197 | static void __exit btusb_exit(void) |
| 1198 | { |
| 1199 | usb_deregister(&btusb_driver); |
| 1200 | } |
| 1201 | |
| 1202 | module_init(btusb_init); |
| 1203 | module_exit(btusb_exit); |
| 1204 | |
Marcel Holtmann | cfeb414 | 2008-08-07 22:26:56 +0200 | [diff] [blame] | 1205 | module_param(ignore_dga, bool, 0644); |
| 1206 | MODULE_PARM_DESC(ignore_dga, "Ignore devices with id 08fd:0001"); |
| 1207 | |
| 1208 | module_param(ignore_csr, bool, 0644); |
| 1209 | MODULE_PARM_DESC(ignore_csr, "Ignore devices with id 0a12:0001"); |
| 1210 | |
| 1211 | module_param(ignore_sniffer, bool, 0644); |
| 1212 | MODULE_PARM_DESC(ignore_sniffer, "Ignore devices with id 0a12:0002"); |
| 1213 | |
| 1214 | module_param(disable_scofix, bool, 0644); |
| 1215 | MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size"); |
| 1216 | |
| 1217 | module_param(force_scofix, bool, 0644); |
| 1218 | MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size"); |
| 1219 | |
| 1220 | module_param(reset, bool, 0644); |
| 1221 | MODULE_PARM_DESC(reset, "Send HCI reset command on initialization"); |
| 1222 | |
Marcel Holtmann | 5e23b92 | 2007-10-20 14:12:34 +0200 | [diff] [blame] | 1223 | MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>"); |
| 1224 | MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION); |
| 1225 | MODULE_VERSION(VERSION); |
| 1226 | MODULE_LICENSE("GPL"); |