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