blob: 0f3010122701ce6ef143cd9b8e517562b9a8c71e [file] [log] [blame]
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001/*
2 *
3 * Generic Bluetooth USB driver
4 *
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02005 * Copyright (C) 2005-2008 Marcel Holtmann <marcel@holtmann.org>
Marcel Holtmann5e23b922007-10-20 14:12:34 +02006 *
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 Holtmann5e23b922007-10-20 14:12:34 +020024#include <linux/module.h>
Marcel Holtmann5e23b922007-10-20 14:12:34 +020025#include <linux/usb.h>
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -070026#include <linux/firmware.h>
Marcel Holtmann5e23b922007-10-20 14:12:34 +020027
28#include <net/bluetooth/bluetooth.h>
29#include <net/bluetooth/hci_core.h>
30
Oliver Neukum7bee5492009-08-24 23:44:59 +020031#define VERSION "0.6"
Marcel Holtmanncfeb4142008-08-07 22:26:56 +020032
Rusty Russell90ab5ee2012-01-13 09:32:20 +103033static bool ignore_dga;
34static bool ignore_csr;
35static bool ignore_sniffer;
36static bool disable_scofix;
37static bool force_scofix;
Marcel Holtmann7a9d4022008-11-30 12:17:26 +010038
Rusty Russell90ab5ee2012-01-13 09:32:20 +103039static bool reset = 1;
Marcel Holtmanncfeb4142008-08-07 22:26:56 +020040
41static struct usb_driver btusb_driver;
42
43#define BTUSB_IGNORE 0x01
Marcel Holtmann7a9d4022008-11-30 12:17:26 +010044#define BTUSB_DIGIANSWER 0x02
45#define BTUSB_CSR 0x04
46#define BTUSB_SNIFFER 0x08
47#define BTUSB_BCM92035 0x10
48#define BTUSB_BROKEN_ISOC 0x20
49#define BTUSB_WRONG_SCO_MTU 0x40
Steven.Li2d25f8b2011-07-01 14:02:36 +080050#define BTUSB_ATH3012 0x80
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -070051#define BTUSB_INTEL 0x100
Marcel Holtmann5e23b922007-10-20 14:12:34 +020052
53static struct usb_device_id btusb_table[] = {
54 /* Generic Bluetooth USB device */
55 { USB_DEVICE_INFO(0xe0, 0x01, 0x01) },
56
Henrik Rydberg1fa65352012-08-25 19:28:06 +020057 /* Apple-specific (Broadcom) devices */
58 { USB_VENDOR_AND_INTERFACE_INFO(0x05ac, 0xff, 0x01, 0x01) },
59
Cho, Yu-Chenc5197742013-06-04 21:40:26 +080060 /* MediaTek MT76x0E */
61 { USB_DEVICE(0x0e8d, 0x763f) },
62
Oliver Neukumc510eae2011-09-21 11:41:45 +020063 /* Broadcom SoftSailing reporting vendor specific */
Don Zickus2e8b50632012-03-28 16:41:11 -040064 { USB_DEVICE(0x0a5c, 0x21e1) },
Oliver Neukumc510eae2011-09-21 11:41:45 +020065
Nobuhiro Iwamatsu3cd01972010-08-20 16:24:07 +090066 /* Apple MacBookPro 7,1 */
67 { USB_DEVICE(0x05ac, 0x8213) },
68
Cyril Lacoux0a79f672010-07-14 10:29:27 +040069 /* Apple iMac11,1 */
70 { USB_DEVICE(0x05ac, 0x8215) },
71
Nobuhiro Iwamatsu9c047152010-08-20 16:24:06 +090072 /* Apple MacBookPro6,2 */
73 { USB_DEVICE(0x05ac, 0x8218) },
74
Edgar (gimli) Hucek3e3ede72010-11-04 08:04:33 +010075 /* Apple MacBookAir3,1, MacBookAir3,2 */
76 { USB_DEVICE(0x05ac, 0x821b) },
77
Pieter-Augustijn Van Malleghema63b7232011-09-07 02:28:10 -040078 /* Apple MacBookAir4,1 */
79 { USB_DEVICE(0x05ac, 0x821f) },
80
Marc-Antoine Perennou88d377b2011-03-24 14:51:21 -030081 /* Apple MacBookPro8,2 */
82 { USB_DEVICE(0x05ac, 0x821a) },
83
Jurgen Kramerf78b68262011-09-04 18:01:42 +020084 /* Apple MacMini5,1 */
85 { USB_DEVICE(0x05ac, 0x8281) },
86
Marcel Holtmanncfeb4142008-08-07 22:26:56 +020087 /* AVM BlueFRITZ! USB v2.0 */
88 { USB_DEVICE(0x057c, 0x3800) },
89
90 /* Bluetooth Ultraport Module from IBM */
91 { USB_DEVICE(0x04bf, 0x030a) },
92
93 /* ALPS Modules with non-standard id */
94 { USB_DEVICE(0x044e, 0x3001) },
95 { USB_DEVICE(0x044e, 0x3002) },
96
97 /* Ericsson with non-standard id */
98 { USB_DEVICE(0x0bdb, 0x1002) },
99
100 /* Canyon CN-BTU1 with HID interfaces */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100101 { USB_DEVICE(0x0c10, 0x0000) },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200102
Wen-chien Jesse Sungd13431c2011-11-08 14:30:22 +0800103 /* Broadcom BCM20702A0 */
Jeff Cook1ee3ff62012-11-09 16:39:48 -0700104 { USB_DEVICE(0x0b05, 0x17b5) },
Raphael Kubo da Costaa7905fb2013-09-02 14:57:51 +0300105 { USB_DEVICE(0x0b05, 0x17cb) },
Jaroslav Resler0c1abbd2012-09-11 17:25:32 +0800106 { USB_DEVICE(0x04ca, 0x2003) },
Manoj Iyer79cd7602012-04-09 09:22:28 -0500107 { USB_DEVICE(0x0489, 0xe042) },
Wen-chien Jesse Sungd13431c2011-11-08 14:30:22 +0800108 { USB_DEVICE(0x413c, 0x8197) },
109
Steven Harms98514032012-04-13 14:45:55 -0400110 /* Foxconn - Hon Hai */
Gustavo Padovanee664012012-08-15 01:38:11 -0300111 { USB_VENDOR_AND_INTERFACE_INFO(0x0489, 0xff, 0x01, 0x01) },
Steven Harms98514032012-04-13 14:45:55 -0400112
Gustavo Padovan92c385f2012-08-06 15:36:49 -0300113 /*Broadcom devices with vendor specific id */
114 { USB_VENDOR_AND_INTERFACE_INFO(0x0a5c, 0xff, 0x01, 0x01) },
115
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200116 { } /* Terminating entry */
117};
118
119MODULE_DEVICE_TABLE(usb, btusb_table);
120
121static struct usb_device_id blacklist_table[] = {
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200122 /* CSR BlueCore devices */
123 { USB_DEVICE(0x0a12, 0x0001), .driver_info = BTUSB_CSR },
124
125 /* Broadcom BCM2033 without firmware */
126 { USB_DEVICE(0x0a5c, 0x2033), .driver_info = BTUSB_IGNORE },
127
Bala Shanmugambe931122010-11-26 17:35:46 +0530128 /* Atheros 3011 with sflash firmware */
129 { USB_DEVICE(0x0cf3, 0x3002), .driver_info = BTUSB_IGNORE },
Marek Vasut6eda5412012-06-08 14:32:50 +0200130 { USB_DEVICE(0x0cf3, 0xe019), .driver_info = BTUSB_IGNORE },
Andy Ross2a7bccc2011-05-09 16:11:16 -0700131 { USB_DEVICE(0x13d3, 0x3304), .driver_info = BTUSB_IGNORE },
Ricardo Mendoza8e7c3d22011-07-13 16:04:29 +0100132 { USB_DEVICE(0x0930, 0x0215), .driver_info = BTUSB_IGNORE },
Keng-Yu Lin6b6ba882011-11-30 18:32:37 +0800133 { USB_DEVICE(0x0489, 0xe03d), .driver_info = BTUSB_IGNORE },
Marcos Chaparroacd94542012-11-06 16:19:11 -0300134 { USB_DEVICE(0x0489, 0xe027), .driver_info = BTUSB_IGNORE },
Bala Shanmugambe931122010-11-26 17:35:46 +0530135
Cho, Yu-Chen509e7862011-01-26 17:10:59 +0800136 /* Atheros AR9285 Malbec with sflash firmware */
137 { USB_DEVICE(0x03f0, 0x311d), .driver_info = BTUSB_IGNORE },
138
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530139 /* Atheros 3012 with sflash firmware */
Ming Leid66629c2013-03-15 11:00:39 +0800140 { USB_DEVICE(0x0cf3, 0x0036), .driver_info = BTUSB_ATH3012 },
Steven.Li2d25f8b2011-07-01 14:02:36 +0800141 { USB_DEVICE(0x0cf3, 0x3004), .driver_info = BTUSB_ATH3012 },
Sunguk Lee94a32d102013-03-12 04:41:58 +0900142 { USB_DEVICE(0x0cf3, 0x3008), .driver_info = BTUSB_ATH3012 },
Cho, Yu-Chen07c0ea82012-03-14 22:01:21 +0200143 { USB_DEVICE(0x0cf3, 0x311d), .driver_info = BTUSB_ATH3012 },
Oliver Neukuma713ad62014-01-16 15:37:11 +0100144 { USB_DEVICE(0x0cf3, 0x311e), .driver_info = BTUSB_ATH3012 },
Ming Leiebaf5792013-03-18 23:45:11 +0800145 { USB_DEVICE(0x0cf3, 0x817a), .driver_info = BTUSB_ATH3012 },
Eran9498ba72011-12-05 22:15:29 +0000146 { USB_DEVICE(0x13d3, 0x3375), .driver_info = BTUSB_ATH3012 },
Josh Boyer3f63c342013-02-19 11:54:16 -0500147 { USB_DEVICE(0x04ca, 0x3004), .driver_info = BTUSB_ATH3012 },
AceLan Kao55ed7d42012-03-28 10:25:36 +0800148 { USB_DEVICE(0x04ca, 0x3005), .driver_info = BTUSB_ATH3012 },
Daniel Schaal2c262b22012-12-29 11:14:34 +0100149 { USB_DEVICE(0x04ca, 0x3006), .driver_info = BTUSB_ATH3012 },
Mohammed Habibulla0d38fa92014-04-17 11:37:13 -0700150 { USB_DEVICE(0x04ca, 0x3007), .driver_info = BTUSB_ATH3012 },
Sergio Cambraf4d6f7d2013-01-10 01:06:55 +0100151 { USB_DEVICE(0x04ca, 0x3008), .driver_info = BTUSB_ATH3012 },
AceLan Kao87522a42012-04-13 17:39:57 +0800152 { USB_DEVICE(0x13d3, 0x3362), .driver_info = BTUSB_ATH3012 },
AceLan Kaoac713112012-04-19 14:53:45 +0800153 { USB_DEVICE(0x0cf3, 0xe004), .driver_info = BTUSB_ATH3012 },
Peng Chen877ed5f2013-08-30 17:41:40 +0800154 { USB_DEVICE(0x0cf3, 0xe005), .driver_info = BTUSB_ATH3012 },
Giancarlo Formicuccia6c4ae5c2012-06-10 08:33:11 +0200155 { USB_DEVICE(0x0930, 0x0219), .driver_info = BTUSB_ATH3012 },
Peng Chen2096ae62012-08-01 10:11:59 +0800156 { USB_DEVICE(0x0489, 0xe057), .driver_info = BTUSB_ATH3012 },
AceLan Kaoeed307e2012-12-11 11:41:20 +0800157 { USB_DEVICE(0x13d3, 0x3393), .driver_info = BTUSB_ATH3012 },
AceLan Kao3a61eda2013-01-03 12:24:59 +0800158 { USB_DEVICE(0x0489, 0xe04e), .driver_info = BTUSB_ATH3012 },
AceLan Kao2582d522013-01-03 12:25:00 +0800159 { USB_DEVICE(0x0489, 0xe056), .driver_info = BTUSB_ATH3012 },
Noguchi Kazutosi0fc110f2013-03-24 23:41:10 +0900160 { USB_DEVICE(0x0489, 0xe04d), .driver_info = BTUSB_ATH3012 },
Thomas Loo4008b2c2013-07-03 02:53:54 +0200161 { USB_DEVICE(0x04c5, 0x1330), .driver_info = BTUSB_ATH3012 },
Sujith Manoharan44c41272013-07-15 09:29:03 +0530162 { USB_DEVICE(0x13d3, 0x3402), .driver_info = BTUSB_ATH3012 },
AceLan Kaodf235042013-07-17 11:27:40 +0800163 { USB_DEVICE(0x0cf3, 0x3121), .driver_info = BTUSB_ATH3012 },
AceLan Kaoeba6a132013-06-20 13:38:45 +0800164 { USB_DEVICE(0x0cf3, 0xe003), .driver_info = BTUSB_ATH3012 },
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530165
Cho, Yu-Chene9036e32011-02-15 10:20:07 +0800166 /* Atheros AR5BBU12 with sflash firmware */
167 { USB_DEVICE(0x0489, 0xe02c), .driver_info = BTUSB_IGNORE },
168
Michael Gruetzner85d59722012-05-02 22:33:40 +0200169 /* Atheros AR5BBU12 with sflash firmware */
170 { USB_DEVICE(0x0489, 0xe03c), .driver_info = BTUSB_ATH3012 },
Yevgeniy Melnichukbc21fde2012-08-07 19:48:10 +0530171 { USB_DEVICE(0x0489, 0xe036), .driver_info = BTUSB_ATH3012 },
Michael Gruetzner85d59722012-05-02 22:33:40 +0200172
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200173 /* Broadcom BCM2035 */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100174 { USB_DEVICE(0x0a5c, 0x2035), .driver_info = BTUSB_WRONG_SCO_MTU },
175 { USB_DEVICE(0x0a5c, 0x200a), .driver_info = BTUSB_WRONG_SCO_MTU },
176 { USB_DEVICE(0x0a5c, 0x2009), .driver_info = BTUSB_BCM92035 },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200177
178 /* Broadcom BCM2045 */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100179 { USB_DEVICE(0x0a5c, 0x2039), .driver_info = BTUSB_WRONG_SCO_MTU },
180 { USB_DEVICE(0x0a5c, 0x2101), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmannbdbef3d2008-09-23 00:16:35 +0200181
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200182 /* IBM/Lenovo ThinkPad with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100183 { USB_DEVICE(0x0a5c, 0x201e), .driver_info = BTUSB_WRONG_SCO_MTU },
184 { USB_DEVICE(0x0a5c, 0x2110), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200185
186 /* HP laptop with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100187 { USB_DEVICE(0x03f0, 0x171d), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200188
189 /* Dell laptop with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100190 { USB_DEVICE(0x413c, 0x8126), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200191
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100192 /* Dell Wireless 370 and 410 devices */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100193 { USB_DEVICE(0x413c, 0x8152), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100194 { USB_DEVICE(0x413c, 0x8156), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200195
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100196 /* Belkin F8T012 and F8T013 devices */
197 { USB_DEVICE(0x050d, 0x0012), .driver_info = BTUSB_WRONG_SCO_MTU },
198 { USB_DEVICE(0x050d, 0x0013), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200199
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100200 /* Asus WL-BTD202 device */
201 { USB_DEVICE(0x0b05, 0x1715), .driver_info = BTUSB_WRONG_SCO_MTU },
202
203 /* Kensington Bluetooth USB adapter */
204 { USB_DEVICE(0x047d, 0x105e), .driver_info = BTUSB_WRONG_SCO_MTU },
205
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200206 /* RTX Telecom based adapters with buggy SCO support */
207 { USB_DEVICE(0x0400, 0x0807), .driver_info = BTUSB_BROKEN_ISOC },
208 { USB_DEVICE(0x0400, 0x080a), .driver_info = BTUSB_BROKEN_ISOC },
209
210 /* CONWISE Technology based adapters with buggy SCO support */
211 { USB_DEVICE(0x0e5e, 0x6622), .driver_info = BTUSB_BROKEN_ISOC },
212
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200213 /* Digianswer devices */
214 { USB_DEVICE(0x08fd, 0x0001), .driver_info = BTUSB_DIGIANSWER },
215 { USB_DEVICE(0x08fd, 0x0002), .driver_info = BTUSB_IGNORE },
216
217 /* CSR BlueCore Bluetooth Sniffer */
218 { USB_DEVICE(0x0a12, 0x0002), .driver_info = BTUSB_SNIFFER },
219
220 /* Frontline ComProbe Bluetooth Sniffer */
221 { USB_DEVICE(0x16d3, 0x0002), .driver_info = BTUSB_SNIFFER },
222
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -0700223 /* Intel Bluetooth device */
224 { USB_DEVICE(0x8087, 0x07dc), .driver_info = BTUSB_INTEL },
225
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200226 { } /* Terminating entry */
227};
228
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200229#define BTUSB_MAX_ISOC_FRAMES 10
230
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200231#define BTUSB_INTR_RUNNING 0
232#define BTUSB_BULK_RUNNING 1
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200233#define BTUSB_ISOC_RUNNING 2
Oliver Neukum7bee5492009-08-24 23:44:59 +0200234#define BTUSB_SUSPENDING 3
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -0300235#define BTUSB_DID_ISO_RESUME 4
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200236
237struct btusb_data {
238 struct hci_dev *hdev;
239 struct usb_device *udev;
Marcel Holtmann5fbcd262008-09-23 00:16:36 +0200240 struct usb_interface *intf;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200241 struct usb_interface *isoc;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200242
243 spinlock_t lock;
244
245 unsigned long flags;
246
247 struct work_struct work;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200248 struct work_struct waker;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200249
250 struct usb_anchor tx_anchor;
251 struct usb_anchor intr_anchor;
252 struct usb_anchor bulk_anchor;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200253 struct usb_anchor isoc_anchor;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200254 struct usb_anchor deferred;
255 int tx_in_flight;
256 spinlock_t txlock;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200257
258 struct usb_endpoint_descriptor *intr_ep;
259 struct usb_endpoint_descriptor *bulk_tx_ep;
260 struct usb_endpoint_descriptor *bulk_rx_ep;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200261 struct usb_endpoint_descriptor *isoc_tx_ep;
262 struct usb_endpoint_descriptor *isoc_rx_ep;
263
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100264 __u8 cmdreq_type;
265
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100266 unsigned int sco_num;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200267 int isoc_altsetting;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +0100268 int suspend_count;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200269};
270
Oliver Neukum7bee5492009-08-24 23:44:59 +0200271static int inc_tx(struct btusb_data *data)
272{
273 unsigned long flags;
274 int rv;
275
276 spin_lock_irqsave(&data->txlock, flags);
277 rv = test_bit(BTUSB_SUSPENDING, &data->flags);
278 if (!rv)
279 data->tx_in_flight++;
280 spin_unlock_irqrestore(&data->txlock, flags);
281
282 return rv;
283}
284
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200285static void btusb_intr_complete(struct urb *urb)
286{
287 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +0100288 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200289 int err;
290
291 BT_DBG("%s urb %p status %d count %d", hdev->name,
292 urb, urb->status, urb->actual_length);
293
294 if (!test_bit(HCI_RUNNING, &hdev->flags))
295 return;
296
297 if (urb->status == 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200298 hdev->stat.byte_rx += urb->actual_length;
299
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200300 if (hci_recv_fragment(hdev, HCI_EVENT_PKT,
301 urb->transfer_buffer,
302 urb->actual_length) < 0) {
303 BT_ERR("%s corrupted event packet", hdev->name);
304 hdev->stat.err_rx++;
305 }
Champion Chen41ba8fa2014-09-06 14:06:08 -0500306 } else if (urb->status == -ENOENT) {
307 /* Avoid suspend failed when usb_kill_urb */
308 return;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200309 }
310
311 if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
312 return;
313
Oliver Neukum7bee5492009-08-24 23:44:59 +0200314 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200315 usb_anchor_urb(urb, &data->intr_anchor);
316
317 err = usb_submit_urb(urb, GFP_ATOMIC);
318 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200319 /* -EPERM: urb is being killed;
320 * -ENODEV: device got disconnected */
321 if (err != -EPERM && err != -ENODEV)
Stefan Seyfried61faddf2010-11-30 21:49:08 +0100322 BT_ERR("%s urb %p failed to resubmit (%d)",
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200323 hdev->name, urb, -err);
324 usb_unanchor_urb(urb);
325 }
326}
327
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100328static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200329{
David Herrmann155961e2012-02-09 21:58:32 +0100330 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200331 struct urb *urb;
332 unsigned char *buf;
333 unsigned int pipe;
334 int err, size;
335
336 BT_DBG("%s", hdev->name);
337
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200338 if (!data->intr_ep)
339 return -ENODEV;
340
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100341 urb = usb_alloc_urb(0, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200342 if (!urb)
343 return -ENOMEM;
344
345 size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
346
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100347 buf = kmalloc(size, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200348 if (!buf) {
349 usb_free_urb(urb);
350 return -ENOMEM;
351 }
352
353 pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
354
355 usb_fill_int_urb(urb, data->udev, pipe, buf, size,
356 btusb_intr_complete, hdev,
357 data->intr_ep->bInterval);
358
359 urb->transfer_flags |= URB_FREE_BUFFER;
360
361 usb_anchor_urb(urb, &data->intr_anchor);
362
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100363 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200364 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +0200365 if (err != -EPERM && err != -ENODEV)
366 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200367 hdev->name, urb, -err);
368 usb_unanchor_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200369 }
370
371 usb_free_urb(urb);
372
373 return err;
374}
375
376static void btusb_bulk_complete(struct urb *urb)
377{
378 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +0100379 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200380 int err;
381
382 BT_DBG("%s urb %p status %d count %d", hdev->name,
383 urb, urb->status, urb->actual_length);
384
385 if (!test_bit(HCI_RUNNING, &hdev->flags))
386 return;
387
388 if (urb->status == 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200389 hdev->stat.byte_rx += urb->actual_length;
390
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200391 if (hci_recv_fragment(hdev, HCI_ACLDATA_PKT,
392 urb->transfer_buffer,
393 urb->actual_length) < 0) {
394 BT_ERR("%s corrupted ACL packet", hdev->name);
395 hdev->stat.err_rx++;
396 }
Champion Chen41ba8fa2014-09-06 14:06:08 -0500397 } else if (urb->status == -ENOENT) {
398 /* Avoid suspend failed when usb_kill_urb */
399 return;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200400 }
401
402 if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
403 return;
404
405 usb_anchor_urb(urb, &data->bulk_anchor);
Oliver Neukum652fd782009-12-16 19:23:43 +0100406 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200407
408 err = usb_submit_urb(urb, GFP_ATOMIC);
409 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200410 /* -EPERM: urb is being killed;
411 * -ENODEV: device got disconnected */
412 if (err != -EPERM && err != -ENODEV)
Stefan Seyfried61faddf2010-11-30 21:49:08 +0100413 BT_ERR("%s urb %p failed to resubmit (%d)",
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200414 hdev->name, urb, -err);
415 usb_unanchor_urb(urb);
416 }
417}
418
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100419static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200420{
David Herrmann155961e2012-02-09 21:58:32 +0100421 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200422 struct urb *urb;
423 unsigned char *buf;
424 unsigned int pipe;
Vikram Kandukuri290ba202009-07-02 14:31:59 +0530425 int err, size = HCI_MAX_FRAME_SIZE;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200426
427 BT_DBG("%s", hdev->name);
428
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200429 if (!data->bulk_rx_ep)
430 return -ENODEV;
431
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100432 urb = usb_alloc_urb(0, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200433 if (!urb)
434 return -ENOMEM;
435
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100436 buf = kmalloc(size, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200437 if (!buf) {
438 usb_free_urb(urb);
439 return -ENOMEM;
440 }
441
442 pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
443
444 usb_fill_bulk_urb(urb, data->udev, pipe,
445 buf, size, btusb_bulk_complete, hdev);
446
447 urb->transfer_flags |= URB_FREE_BUFFER;
448
Oliver Neukum7bee5492009-08-24 23:44:59 +0200449 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200450 usb_anchor_urb(urb, &data->bulk_anchor);
451
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100452 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200453 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +0200454 if (err != -EPERM && err != -ENODEV)
455 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200456 hdev->name, urb, -err);
457 usb_unanchor_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200458 }
459
460 usb_free_urb(urb);
461
462 return err;
463}
464
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200465static void btusb_isoc_complete(struct urb *urb)
466{
467 struct hci_dev *hdev = urb->context;
David Herrmann155961e2012-02-09 21:58:32 +0100468 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200469 int i, err;
470
471 BT_DBG("%s urb %p status %d count %d", hdev->name,
472 urb, urb->status, urb->actual_length);
473
474 if (!test_bit(HCI_RUNNING, &hdev->flags))
475 return;
476
477 if (urb->status == 0) {
478 for (i = 0; i < urb->number_of_packets; i++) {
479 unsigned int offset = urb->iso_frame_desc[i].offset;
480 unsigned int length = urb->iso_frame_desc[i].actual_length;
481
482 if (urb->iso_frame_desc[i].status)
483 continue;
484
485 hdev->stat.byte_rx += length;
486
487 if (hci_recv_fragment(hdev, HCI_SCODATA_PKT,
488 urb->transfer_buffer + offset,
489 length) < 0) {
490 BT_ERR("%s corrupted SCO packet", hdev->name);
491 hdev->stat.err_rx++;
492 }
493 }
Champion Chen41ba8fa2014-09-06 14:06:08 -0500494 } else if (urb->status == -ENOENT) {
495 /* Avoid suspend failed when usb_kill_urb */
496 return;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200497 }
498
499 if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags))
500 return;
501
502 usb_anchor_urb(urb, &data->isoc_anchor);
503
504 err = usb_submit_urb(urb, GFP_ATOMIC);
505 if (err < 0) {
Paul Bolle4935f1c2011-08-09 17:16:28 +0200506 /* -EPERM: urb is being killed;
507 * -ENODEV: device got disconnected */
508 if (err != -EPERM && err != -ENODEV)
Stefan Seyfried61faddf2010-11-30 21:49:08 +0100509 BT_ERR("%s urb %p failed to resubmit (%d)",
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200510 hdev->name, urb, -err);
511 usb_unanchor_urb(urb);
512 }
513}
514
Jesper Juhl42b16b32011-01-17 00:09:38 +0100515static inline void __fill_isoc_descriptor(struct urb *urb, int len, int mtu)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200516{
517 int i, offset = 0;
518
519 BT_DBG("len %d mtu %d", len, mtu);
520
521 for (i = 0; i < BTUSB_MAX_ISOC_FRAMES && len >= mtu;
522 i++, offset += mtu, len -= mtu) {
523 urb->iso_frame_desc[i].offset = offset;
524 urb->iso_frame_desc[i].length = mtu;
525 }
526
527 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
528 urb->iso_frame_desc[i].offset = offset;
529 urb->iso_frame_desc[i].length = len;
530 i++;
531 }
532
533 urb->number_of_packets = i;
534}
535
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100536static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200537{
David Herrmann155961e2012-02-09 21:58:32 +0100538 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200539 struct urb *urb;
540 unsigned char *buf;
541 unsigned int pipe;
542 int err, size;
543
544 BT_DBG("%s", hdev->name);
545
546 if (!data->isoc_rx_ep)
547 return -ENODEV;
548
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100549 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200550 if (!urb)
551 return -ENOMEM;
552
553 size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) *
554 BTUSB_MAX_ISOC_FRAMES;
555
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100556 buf = kmalloc(size, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200557 if (!buf) {
558 usb_free_urb(urb);
559 return -ENOMEM;
560 }
561
562 pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress);
563
Bing Zhaofa0fb932011-12-20 18:19:00 -0800564 usb_fill_int_urb(urb, data->udev, pipe, buf, size, btusb_isoc_complete,
565 hdev, data->isoc_rx_ep->bInterval);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200566
567 urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200568
569 __fill_isoc_descriptor(urb, size,
570 le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize));
571
572 usb_anchor_urb(urb, &data->isoc_anchor);
573
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100574 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200575 if (err < 0) {
Paul Bolled4b8d1c2011-10-09 12:12:22 +0200576 if (err != -EPERM && err != -ENODEV)
577 BT_ERR("%s urb %p submission failed (%d)",
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200578 hdev->name, urb, -err);
579 usb_unanchor_urb(urb);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200580 }
581
582 usb_free_urb(urb);
583
584 return err;
585}
586
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200587static void btusb_tx_complete(struct urb *urb)
588{
589 struct sk_buff *skb = urb->context;
590 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
David Herrmann155961e2012-02-09 21:58:32 +0100591 struct btusb_data *data = hci_get_drvdata(hdev);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200592
593 BT_DBG("%s urb %p status %d count %d", hdev->name,
594 urb, urb->status, urb->actual_length);
595
596 if (!test_bit(HCI_RUNNING, &hdev->flags))
597 goto done;
598
599 if (!urb->status)
600 hdev->stat.byte_tx += urb->transfer_buffer_length;
601 else
602 hdev->stat.err_tx++;
603
604done:
605 spin_lock(&data->txlock);
606 data->tx_in_flight--;
607 spin_unlock(&data->txlock);
608
609 kfree(urb->setup_packet);
610
611 kfree_skb(skb);
612}
613
614static void btusb_isoc_tx_complete(struct urb *urb)
615{
616 struct sk_buff *skb = urb->context;
617 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200618
619 BT_DBG("%s urb %p status %d count %d", hdev->name,
620 urb, urb->status, urb->actual_length);
621
622 if (!test_bit(HCI_RUNNING, &hdev->flags))
623 goto done;
624
625 if (!urb->status)
626 hdev->stat.byte_tx += urb->transfer_buffer_length;
627 else
628 hdev->stat.err_tx++;
629
630done:
631 kfree(urb->setup_packet);
632
633 kfree_skb(skb);
634}
635
636static int btusb_open(struct hci_dev *hdev)
637{
David Herrmann155961e2012-02-09 21:58:32 +0100638 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200639 int err;
640
641 BT_DBG("%s", hdev->name);
642
Oliver Neukum7bee5492009-08-24 23:44:59 +0200643 err = usb_autopm_get_interface(data->intf);
644 if (err < 0)
645 return err;
646
647 data->intf->needs_remote_wakeup = 1;
648
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200649 if (test_and_set_bit(HCI_RUNNING, &hdev->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +0200650 goto done;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200651
652 if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +0200653 goto done;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200654
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100655 err = btusb_submit_intr_urb(hdev, GFP_KERNEL);
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100656 if (err < 0)
657 goto failed;
658
659 err = btusb_submit_bulk_urb(hdev, GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200660 if (err < 0) {
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100661 usb_kill_anchored_urbs(&data->intr_anchor);
662 goto failed;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200663 }
664
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100665 set_bit(BTUSB_BULK_RUNNING, &data->flags);
666 btusb_submit_bulk_urb(hdev, GFP_KERNEL);
667
Oliver Neukum7bee5492009-08-24 23:44:59 +0200668done:
669 usb_autopm_put_interface(data->intf);
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100670 return 0;
671
672failed:
673 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
674 clear_bit(HCI_RUNNING, &hdev->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200675 usb_autopm_put_interface(data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200676 return err;
677}
678
Oliver Neukum7bee5492009-08-24 23:44:59 +0200679static void btusb_stop_traffic(struct btusb_data *data)
680{
681 usb_kill_anchored_urbs(&data->intr_anchor);
682 usb_kill_anchored_urbs(&data->bulk_anchor);
683 usb_kill_anchored_urbs(&data->isoc_anchor);
684}
685
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200686static int btusb_close(struct hci_dev *hdev)
687{
David Herrmann155961e2012-02-09 21:58:32 +0100688 struct btusb_data *data = hci_get_drvdata(hdev);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200689 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200690
691 BT_DBG("%s", hdev->name);
692
693 if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
694 return 0;
695
Marcel Holtmanne8c3c3d2008-09-23 00:16:36 +0200696 cancel_work_sync(&data->work);
Linus Torvalds404291a2009-11-11 13:32:29 -0800697 cancel_work_sync(&data->waker);
Marcel Holtmanne8c3c3d2008-09-23 00:16:36 +0200698
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200699 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200700 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200701 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200702
703 btusb_stop_traffic(data);
704 err = usb_autopm_get_interface(data->intf);
705 if (err < 0)
Oliver Neukum7b8e2c12009-11-13 14:26:23 +0100706 goto failed;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200707
708 data->intf->needs_remote_wakeup = 0;
709 usb_autopm_put_interface(data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200710
Oliver Neukum7b8e2c12009-11-13 14:26:23 +0100711failed:
712 usb_scuttle_anchored_urbs(&data->deferred);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200713 return 0;
714}
715
716static int btusb_flush(struct hci_dev *hdev)
717{
David Herrmann155961e2012-02-09 21:58:32 +0100718 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200719
720 BT_DBG("%s", hdev->name);
721
722 usb_kill_anchored_urbs(&data->tx_anchor);
723
724 return 0;
725}
726
727static int btusb_send_frame(struct sk_buff *skb)
728{
729 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
David Herrmann155961e2012-02-09 21:58:32 +0100730 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200731 struct usb_ctrlrequest *dr;
732 struct urb *urb;
733 unsigned int pipe;
734 int err;
735
736 BT_DBG("%s", hdev->name);
737
738 if (!test_bit(HCI_RUNNING, &hdev->flags))
739 return -EBUSY;
740
741 switch (bt_cb(skb)->pkt_type) {
742 case HCI_COMMAND_PKT:
743 urb = usb_alloc_urb(0, GFP_ATOMIC);
744 if (!urb)
745 return -ENOMEM;
746
747 dr = kmalloc(sizeof(*dr), GFP_ATOMIC);
748 if (!dr) {
749 usb_free_urb(urb);
750 return -ENOMEM;
751 }
752
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100753 dr->bRequestType = data->cmdreq_type;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200754 dr->bRequest = 0;
755 dr->wIndex = 0;
756 dr->wValue = 0;
757 dr->wLength = __cpu_to_le16(skb->len);
758
759 pipe = usb_sndctrlpipe(data->udev, 0x00);
760
761 usb_fill_control_urb(urb, data->udev, pipe, (void *) dr,
762 skb->data, skb->len, btusb_tx_complete, skb);
763
764 hdev->stat.cmd_tx++;
765 break;
766
767 case HCI_ACLDATA_PKT:
Peter Hurley9fd481e2011-07-14 08:48:32 -0400768 if (!data->bulk_tx_ep)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200769 return -ENODEV;
770
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200771 urb = usb_alloc_urb(0, GFP_ATOMIC);
772 if (!urb)
773 return -ENOMEM;
774
775 pipe = usb_sndbulkpipe(data->udev,
776 data->bulk_tx_ep->bEndpointAddress);
777
778 usb_fill_bulk_urb(urb, data->udev, pipe,
779 skb->data, skb->len, btusb_tx_complete, skb);
780
781 hdev->stat.acl_tx++;
782 break;
783
784 case HCI_SCODATA_PKT:
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200785 if (!data->isoc_tx_ep || hdev->conn_hash.sco_num < 1)
786 return -ENODEV;
787
788 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_ATOMIC);
789 if (!urb)
790 return -ENOMEM;
791
792 pipe = usb_sndisocpipe(data->udev,
793 data->isoc_tx_ep->bEndpointAddress);
794
Gustavo F. Padovan03c2d0e2011-02-14 18:53:43 -0300795 usb_fill_int_urb(urb, data->udev, pipe,
796 skb->data, skb->len, btusb_isoc_tx_complete,
797 skb, data->isoc_tx_ep->bInterval);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200798
799 urb->transfer_flags = URB_ISO_ASAP;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200800
801 __fill_isoc_descriptor(urb, skb->len,
802 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
803
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200804 hdev->stat.sco_tx++;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200805 goto skip_waking;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200806
807 default:
808 return -EILSEQ;
809 }
810
Oliver Neukum7bee5492009-08-24 23:44:59 +0200811 err = inc_tx(data);
812 if (err) {
813 usb_anchor_urb(urb, &data->deferred);
814 schedule_work(&data->waker);
815 err = 0;
816 goto done;
817 }
818
819skip_waking:
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200820 usb_anchor_urb(urb, &data->tx_anchor);
821
822 err = usb_submit_urb(urb, GFP_ATOMIC);
823 if (err < 0) {
Paul Bolle5a9b80e2011-10-09 12:12:16 +0200824 if (err != -EPERM && err != -ENODEV)
825 BT_ERR("%s urb %p submission failed (%d)",
826 hdev->name, urb, -err);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200827 kfree(urb->setup_packet);
828 usb_unanchor_urb(urb);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200829 } else {
830 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200831 }
832
Oliver Neukum7bee5492009-08-24 23:44:59 +0200833done:
Cong Wang54a8a792011-11-22 09:32:57 +0800834 usb_free_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200835 return err;
836}
837
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200838static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
839{
David Herrmann155961e2012-02-09 21:58:32 +0100840 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200841
842 BT_DBG("%s evt %d", hdev->name, evt);
843
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100844 if (hdev->conn_hash.sco_num != data->sco_num) {
845 data->sco_num = hdev->conn_hash.sco_num;
846 schedule_work(&data->work);
Marcel Holtmanna780efa2008-11-30 12:17:12 +0100847 }
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200848}
849
Jesper Juhl42b16b32011-01-17 00:09:38 +0100850static inline int __set_isoc_interface(struct hci_dev *hdev, int altsetting)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200851{
David Herrmann155961e2012-02-09 21:58:32 +0100852 struct btusb_data *data = hci_get_drvdata(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200853 struct usb_interface *intf = data->isoc;
854 struct usb_endpoint_descriptor *ep_desc;
855 int i, err;
856
857 if (!data->isoc)
858 return -ENODEV;
859
860 err = usb_set_interface(data->udev, 1, altsetting);
861 if (err < 0) {
862 BT_ERR("%s setting interface failed (%d)", hdev->name, -err);
863 return err;
864 }
865
866 data->isoc_altsetting = altsetting;
867
868 data->isoc_tx_ep = NULL;
869 data->isoc_rx_ep = NULL;
870
871 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
872 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
873
874 if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) {
875 data->isoc_tx_ep = ep_desc;
876 continue;
877 }
878
879 if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) {
880 data->isoc_rx_ep = ep_desc;
881 continue;
882 }
883 }
884
885 if (!data->isoc_tx_ep || !data->isoc_rx_ep) {
886 BT_ERR("%s invalid SCO descriptors", hdev->name);
887 return -ENODEV;
888 }
889
890 return 0;
891}
892
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200893static void btusb_work(struct work_struct *work)
894{
895 struct btusb_data *data = container_of(work, struct btusb_data, work);
896 struct hci_dev *hdev = data->hdev;
Mikel Astizf4001d22012-04-11 08:48:51 +0200897 int new_alts;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200898 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200899
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200900 if (hdev->conn_hash.sco_num > 0) {
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -0300901 if (!test_bit(BTUSB_DID_ISO_RESUME, &data->flags)) {
Oliver Neukum8efdd0c2011-02-11 13:00:06 +0100902 err = usb_autopm_get_interface(data->isoc ? data->isoc : data->intf);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200903 if (err < 0) {
904 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
905 usb_kill_anchored_urbs(&data->isoc_anchor);
906 return;
907 }
908
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -0300909 set_bit(BTUSB_DID_ISO_RESUME, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200910 }
Mikel Astizf4001d22012-04-11 08:48:51 +0200911
912 if (hdev->voice_setting & 0x0020) {
913 static const int alts[3] = { 2, 4, 5 };
914 new_alts = alts[hdev->conn_hash.sco_num - 1];
915 } else {
916 new_alts = hdev->conn_hash.sco_num;
917 }
918
919 if (data->isoc_altsetting != new_alts) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200920 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
921 usb_kill_anchored_urbs(&data->isoc_anchor);
922
Mikel Astizf4001d22012-04-11 08:48:51 +0200923 if (__set_isoc_interface(hdev, new_alts) < 0)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200924 return;
925 }
926
927 if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100928 if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200929 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
930 else
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100931 btusb_submit_isoc_urb(hdev, GFP_KERNEL);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200932 }
933 } else {
934 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
935 usb_kill_anchored_urbs(&data->isoc_anchor);
936
937 __set_isoc_interface(hdev, 0);
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -0300938 if (test_and_clear_bit(BTUSB_DID_ISO_RESUME, &data->flags))
Oliver Neukum8efdd0c2011-02-11 13:00:06 +0100939 usb_autopm_put_interface(data->isoc ? data->isoc : data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200940 }
941}
942
Oliver Neukum7bee5492009-08-24 23:44:59 +0200943static void btusb_waker(struct work_struct *work)
944{
945 struct btusb_data *data = container_of(work, struct btusb_data, waker);
946 int err;
947
948 err = usb_autopm_get_interface(data->intf);
949 if (err < 0)
950 return;
951
952 usb_autopm_put_interface(data->intf);
953}
954
Marcel Holtmann9f8f9622013-04-10 08:11:35 -0700955static int btusb_setup_bcm92035(struct hci_dev *hdev)
956{
957 struct sk_buff *skb;
958 u8 val = 0x00;
959
960 BT_DBG("%s", hdev->name);
961
962 skb = __hci_cmd_sync(hdev, 0xfc3b, 1, &val, HCI_INIT_TIMEOUT);
963 if (IS_ERR(skb))
964 BT_ERR("BCM92035 command failed (%ld)", -PTR_ERR(skb));
965 else
966 kfree_skb(skb);
967
968 return 0;
969}
970
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -0700971struct intel_version {
972 u8 status;
973 u8 hw_platform;
974 u8 hw_variant;
975 u8 hw_revision;
976 u8 fw_variant;
977 u8 fw_revision;
978 u8 fw_build_num;
979 u8 fw_build_ww;
980 u8 fw_build_yy;
981 u8 fw_patch_num;
982} __packed;
983
984static const struct firmware *btusb_setup_intel_get_fw(struct hci_dev *hdev,
985 struct intel_version *ver)
986{
987 const struct firmware *fw;
988 char fwname[64];
989 int ret;
990
991 snprintf(fwname, sizeof(fwname),
992 "intel/ibt-hw-%x.%x.%x-fw-%x.%x.%x.%x.%x.bseq",
993 ver->hw_platform, ver->hw_variant, ver->hw_revision,
994 ver->fw_variant, ver->fw_revision, ver->fw_build_num,
995 ver->fw_build_ww, ver->fw_build_yy);
996
997 ret = request_firmware(&fw, fwname, &hdev->dev);
998 if (ret < 0) {
999 if (ret == -EINVAL) {
1000 BT_ERR("%s Intel firmware file request failed (%d)",
1001 hdev->name, ret);
1002 return NULL;
1003 }
1004
1005 BT_ERR("%s failed to open Intel firmware file: %s(%d)",
1006 hdev->name, fwname, ret);
1007
1008 /* If the correct firmware patch file is not found, use the
1009 * default firmware patch file instead
1010 */
1011 snprintf(fwname, sizeof(fwname), "intel/ibt-hw-%x.%x.bseq",
1012 ver->hw_platform, ver->hw_variant);
1013 if (request_firmware(&fw, fwname, &hdev->dev) < 0) {
1014 BT_ERR("%s failed to open default Intel fw file: %s",
1015 hdev->name, fwname);
1016 return NULL;
1017 }
1018 }
1019
1020 BT_INFO("%s: Intel Bluetooth firmware file: %s", hdev->name, fwname);
1021
1022 return fw;
1023}
1024
1025static int btusb_setup_intel_patching(struct hci_dev *hdev,
1026 const struct firmware *fw,
1027 const u8 **fw_ptr, int *disable_patch)
1028{
1029 struct sk_buff *skb;
1030 struct hci_command_hdr *cmd;
1031 const u8 *cmd_param;
1032 struct hci_event_hdr *evt = NULL;
1033 const u8 *evt_param = NULL;
1034 int remain = fw->size - (*fw_ptr - fw->data);
1035
1036 /* The first byte indicates the types of the patch command or event.
1037 * 0x01 means HCI command and 0x02 is HCI event. If the first bytes
1038 * in the current firmware buffer doesn't start with 0x01 or
1039 * the size of remain buffer is smaller than HCI command header,
1040 * the firmware file is corrupted and it should stop the patching
1041 * process.
1042 */
1043 if (remain > HCI_COMMAND_HDR_SIZE && *fw_ptr[0] != 0x01) {
1044 BT_ERR("%s Intel fw corrupted: invalid cmd read", hdev->name);
1045 return -EINVAL;
1046 }
1047 (*fw_ptr)++;
1048 remain--;
1049
1050 cmd = (struct hci_command_hdr *)(*fw_ptr);
1051 *fw_ptr += sizeof(*cmd);
1052 remain -= sizeof(*cmd);
1053
1054 /* Ensure that the remain firmware data is long enough than the length
1055 * of command parameter. If not, the firmware file is corrupted.
1056 */
1057 if (remain < cmd->plen) {
1058 BT_ERR("%s Intel fw corrupted: invalid cmd len", hdev->name);
1059 return -EFAULT;
1060 }
1061
1062 /* If there is a command that loads a patch in the firmware
1063 * file, then enable the patch upon success, otherwise just
1064 * disable the manufacturer mode, for example patch activation
1065 * is not required when the default firmware patch file is used
1066 * because there are no patch data to load.
1067 */
1068 if (*disable_patch && le16_to_cpu(cmd->opcode) == 0xfc8e)
1069 *disable_patch = 0;
1070
1071 cmd_param = *fw_ptr;
1072 *fw_ptr += cmd->plen;
1073 remain -= cmd->plen;
1074
1075 /* This reads the expected events when the above command is sent to the
1076 * device. Some vendor commands expects more than one events, for
1077 * example command status event followed by vendor specific event.
1078 * For this case, it only keeps the last expected event. so the command
1079 * can be sent with __hci_cmd_sync_ev() which returns the sk_buff of
1080 * last expected event.
1081 */
1082 while (remain > HCI_EVENT_HDR_SIZE && *fw_ptr[0] == 0x02) {
1083 (*fw_ptr)++;
1084 remain--;
1085
1086 evt = (struct hci_event_hdr *)(*fw_ptr);
1087 *fw_ptr += sizeof(*evt);
1088 remain -= sizeof(*evt);
1089
1090 if (remain < evt->plen) {
1091 BT_ERR("%s Intel fw corrupted: invalid evt len",
1092 hdev->name);
1093 return -EFAULT;
1094 }
1095
1096 evt_param = *fw_ptr;
1097 *fw_ptr += evt->plen;
1098 remain -= evt->plen;
1099 }
1100
1101 /* Every HCI commands in the firmware file has its correspond event.
1102 * If event is not found or remain is smaller than zero, the firmware
1103 * file is corrupted.
1104 */
1105 if (!evt || !evt_param || remain < 0) {
1106 BT_ERR("%s Intel fw corrupted: invalid evt read", hdev->name);
1107 return -EFAULT;
1108 }
1109
1110 skb = __hci_cmd_sync_ev(hdev, le16_to_cpu(cmd->opcode), cmd->plen,
1111 cmd_param, evt->evt, HCI_INIT_TIMEOUT);
1112 if (IS_ERR(skb)) {
1113 BT_ERR("%s sending Intel patch command (0x%4.4x) failed (%ld)",
1114 hdev->name, cmd->opcode, PTR_ERR(skb));
Adam Leeea788752013-07-10 10:02:12 +08001115 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001116 }
1117
1118 /* It ensures that the returned event matches the event data read from
1119 * the firmware file. At fist, it checks the length and then
1120 * the contents of the event.
1121 */
1122 if (skb->len != evt->plen) {
1123 BT_ERR("%s mismatch event length (opcode 0x%4.4x)", hdev->name,
1124 le16_to_cpu(cmd->opcode));
1125 kfree_skb(skb);
1126 return -EFAULT;
1127 }
1128
1129 if (memcmp(skb->data, evt_param, evt->plen)) {
1130 BT_ERR("%s mismatch event parameter (opcode 0x%4.4x)",
1131 hdev->name, le16_to_cpu(cmd->opcode));
1132 kfree_skb(skb);
1133 return -EFAULT;
1134 }
1135 kfree_skb(skb);
1136
1137 return 0;
1138}
1139
1140static int btusb_setup_intel(struct hci_dev *hdev)
1141{
1142 struct sk_buff *skb;
1143 const struct firmware *fw;
1144 const u8 *fw_ptr;
1145 int disable_patch;
1146 struct intel_version *ver;
1147
1148 const u8 mfg_enable[] = { 0x01, 0x00 };
1149 const u8 mfg_disable[] = { 0x00, 0x00 };
1150 const u8 mfg_reset_deactivate[] = { 0x00, 0x01 };
1151 const u8 mfg_reset_activate[] = { 0x00, 0x02 };
1152
1153 BT_DBG("%s", hdev->name);
1154
1155 /* The controller has a bug with the first HCI command sent to it
1156 * returning number of completed commands as zero. This would stall the
1157 * command processing in the Bluetooth core.
1158 *
1159 * As a workaround, send HCI Reset command first which will reset the
1160 * number of completed commands and allow normal command processing
1161 * from now on.
1162 */
1163 skb = __hci_cmd_sync(hdev, HCI_OP_RESET, 0, NULL, HCI_INIT_TIMEOUT);
1164 if (IS_ERR(skb)) {
1165 BT_ERR("%s sending initial HCI reset command failed (%ld)",
1166 hdev->name, PTR_ERR(skb));
Adam Leeea788752013-07-10 10:02:12 +08001167 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001168 }
1169 kfree_skb(skb);
1170
1171 /* Read Intel specific controller version first to allow selection of
1172 * which firmware file to load.
1173 *
1174 * The returned information are hardware variant and revision plus
1175 * firmware variant, revision and build number.
1176 */
1177 skb = __hci_cmd_sync(hdev, 0xfc05, 0, NULL, HCI_INIT_TIMEOUT);
1178 if (IS_ERR(skb)) {
1179 BT_ERR("%s reading Intel fw version command failed (%ld)",
1180 hdev->name, PTR_ERR(skb));
Adam Leeea788752013-07-10 10:02:12 +08001181 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001182 }
1183
1184 if (skb->len != sizeof(*ver)) {
1185 BT_ERR("%s Intel version event length mismatch", hdev->name);
1186 kfree_skb(skb);
1187 return -EIO;
1188 }
1189
1190 ver = (struct intel_version *)skb->data;
1191 if (ver->status) {
1192 BT_ERR("%s Intel fw version event failed (%02x)", hdev->name,
1193 ver->status);
1194 kfree_skb(skb);
1195 return -bt_to_errno(ver->status);
1196 }
1197
1198 BT_INFO("%s: read Intel version: %02x%02x%02x%02x%02x%02x%02x%02x%02x",
1199 hdev->name, ver->hw_platform, ver->hw_variant,
1200 ver->hw_revision, ver->fw_variant, ver->fw_revision,
1201 ver->fw_build_num, ver->fw_build_ww, ver->fw_build_yy,
1202 ver->fw_patch_num);
1203
1204 /* fw_patch_num indicates the version of patch the device currently
1205 * have. If there is no patch data in the device, it is always 0x00.
1206 * So, if it is other than 0x00, no need to patch the deivce again.
1207 */
1208 if (ver->fw_patch_num) {
1209 BT_INFO("%s: Intel device is already patched. patch num: %02x",
1210 hdev->name, ver->fw_patch_num);
1211 kfree_skb(skb);
1212 return 0;
1213 }
1214
1215 /* Opens the firmware patch file based on the firmware version read
1216 * from the controller. If it fails to open the matching firmware
1217 * patch file, it tries to open the default firmware patch file.
1218 * If no patch file is found, allow the device to operate without
1219 * a patch.
1220 */
1221 fw = btusb_setup_intel_get_fw(hdev, ver);
1222 if (!fw) {
1223 kfree_skb(skb);
1224 return 0;
1225 }
1226 fw_ptr = fw->data;
1227
1228 /* This Intel specific command enables the manufacturer mode of the
1229 * controller.
1230 *
1231 * Only while this mode is enabled, the driver can download the
1232 * firmware patch data and configuration parameters.
1233 */
1234 skb = __hci_cmd_sync(hdev, 0xfc11, 2, mfg_enable, HCI_INIT_TIMEOUT);
1235 if (IS_ERR(skb)) {
1236 BT_ERR("%s entering Intel manufacturer mode failed (%ld)",
1237 hdev->name, PTR_ERR(skb));
1238 release_firmware(fw);
Adam Leeea788752013-07-10 10:02:12 +08001239 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001240 }
1241
1242 if (skb->data[0]) {
1243 u8 evt_status = skb->data[0];
1244 BT_ERR("%s enable Intel manufacturer mode event failed (%02x)",
1245 hdev->name, evt_status);
1246 kfree_skb(skb);
1247 release_firmware(fw);
1248 return -bt_to_errno(evt_status);
1249 }
1250 kfree_skb(skb);
1251
1252 disable_patch = 1;
1253
1254 /* The firmware data file consists of list of Intel specific HCI
1255 * commands and its expected events. The first byte indicates the
1256 * type of the message, either HCI command or HCI event.
1257 *
1258 * It reads the command and its expected event from the firmware file,
1259 * and send to the controller. Once __hci_cmd_sync_ev() returns,
1260 * the returned event is compared with the event read from the firmware
1261 * file and it will continue until all the messages are downloaded to
1262 * the controller.
1263 *
1264 * Once the firmware patching is completed successfully,
1265 * the manufacturer mode is disabled with reset and activating the
1266 * downloaded patch.
1267 *
1268 * If the firmware patching fails, the manufacturer mode is
1269 * disabled with reset and deactivating the patch.
1270 *
1271 * If the default patch file is used, no reset is done when disabling
1272 * the manufacturer.
1273 */
1274 while (fw->size > fw_ptr - fw->data) {
1275 int ret;
1276
1277 ret = btusb_setup_intel_patching(hdev, fw, &fw_ptr,
1278 &disable_patch);
1279 if (ret < 0)
1280 goto exit_mfg_deactivate;
1281 }
1282
1283 release_firmware(fw);
1284
1285 if (disable_patch)
1286 goto exit_mfg_disable;
1287
1288 /* Patching completed successfully and disable the manufacturer mode
1289 * with reset and activate the downloaded firmware patches.
1290 */
1291 skb = __hci_cmd_sync(hdev, 0xfc11, sizeof(mfg_reset_activate),
1292 mfg_reset_activate, HCI_INIT_TIMEOUT);
1293 if (IS_ERR(skb)) {
1294 BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
1295 hdev->name, PTR_ERR(skb));
Adam Leeea788752013-07-10 10:02:12 +08001296 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001297 }
1298 kfree_skb(skb);
1299
1300 BT_INFO("%s: Intel Bluetooth firmware patch completed and activated",
1301 hdev->name);
1302
1303 return 0;
1304
1305exit_mfg_disable:
1306 /* Disable the manufacturer mode without reset */
1307 skb = __hci_cmd_sync(hdev, 0xfc11, sizeof(mfg_disable), mfg_disable,
1308 HCI_INIT_TIMEOUT);
1309 if (IS_ERR(skb)) {
1310 BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
1311 hdev->name, PTR_ERR(skb));
Adam Leeea788752013-07-10 10:02:12 +08001312 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001313 }
1314 kfree_skb(skb);
1315
1316 BT_INFO("%s: Intel Bluetooth firmware patch completed", hdev->name);
1317 return 0;
1318
1319exit_mfg_deactivate:
1320 release_firmware(fw);
1321
1322 /* Patching failed. Disable the manufacturer mode with reset and
1323 * deactivate the downloaded firmware patches.
1324 */
1325 skb = __hci_cmd_sync(hdev, 0xfc11, sizeof(mfg_reset_deactivate),
1326 mfg_reset_deactivate, HCI_INIT_TIMEOUT);
1327 if (IS_ERR(skb)) {
1328 BT_ERR("%s exiting Intel manufacturer mode failed (%ld)",
1329 hdev->name, PTR_ERR(skb));
Adam Leeea788752013-07-10 10:02:12 +08001330 return PTR_ERR(skb);
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001331 }
1332 kfree_skb(skb);
1333
1334 BT_INFO("%s: Intel Bluetooth firmware patch completed and deactivated",
1335 hdev->name);
1336
1337 return 0;
1338}
1339
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001340static int btusb_probe(struct usb_interface *intf,
1341 const struct usb_device_id *id)
1342{
1343 struct usb_endpoint_descriptor *ep_desc;
1344 struct btusb_data *data;
1345 struct hci_dev *hdev;
1346 int i, err;
1347
1348 BT_DBG("intf %p id %p", intf, id);
1349
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02001350 /* interface numbers are hardcoded in the spec */
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001351 if (intf->cur_altsetting->desc.bInterfaceNumber != 0)
1352 return -ENODEV;
1353
1354 if (!id->driver_info) {
1355 const struct usb_device_id *match;
1356 match = usb_match_id(intf, blacklist_table);
1357 if (match)
1358 id = match;
1359 }
1360
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02001361 if (id->driver_info == BTUSB_IGNORE)
1362 return -ENODEV;
1363
1364 if (ignore_dga && id->driver_info & BTUSB_DIGIANSWER)
1365 return -ENODEV;
1366
1367 if (ignore_csr && id->driver_info & BTUSB_CSR)
1368 return -ENODEV;
1369
1370 if (ignore_sniffer && id->driver_info & BTUSB_SNIFFER)
1371 return -ENODEV;
1372
Steven.Li2d25f8b2011-07-01 14:02:36 +08001373 if (id->driver_info & BTUSB_ATH3012) {
1374 struct usb_device *udev = interface_to_usbdev(intf);
1375
1376 /* Old firmware would otherwise let ath3k driver load
1377 * patch and sysconfig files */
1378 if (le16_to_cpu(udev->descriptor.bcdDevice) <= 0x0001)
1379 return -ENODEV;
1380 }
1381
Sachin Kamat98921db2012-07-27 12:38:39 +05301382 data = devm_kzalloc(&intf->dev, sizeof(*data), GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001383 if (!data)
1384 return -ENOMEM;
1385
1386 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
1387 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
1388
1389 if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
1390 data->intr_ep = ep_desc;
1391 continue;
1392 }
1393
1394 if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
1395 data->bulk_tx_ep = ep_desc;
1396 continue;
1397 }
1398
1399 if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
1400 data->bulk_rx_ep = ep_desc;
1401 continue;
1402 }
1403 }
1404
Sachin Kamat98921db2012-07-27 12:38:39 +05301405 if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001406 return -ENODEV;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001407
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01001408 data->cmdreq_type = USB_TYPE_CLASS;
1409
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001410 data->udev = interface_to_usbdev(intf);
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02001411 data->intf = intf;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001412
1413 spin_lock_init(&data->lock);
1414
1415 INIT_WORK(&data->work, btusb_work);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001416 INIT_WORK(&data->waker, btusb_waker);
1417 spin_lock_init(&data->txlock);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001418
1419 init_usb_anchor(&data->tx_anchor);
1420 init_usb_anchor(&data->intr_anchor);
1421 init_usb_anchor(&data->bulk_anchor);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001422 init_usb_anchor(&data->isoc_anchor);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001423 init_usb_anchor(&data->deferred);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001424
1425 hdev = hci_alloc_dev();
Sachin Kamat98921db2012-07-27 12:38:39 +05301426 if (!hdev)
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001427 return -ENOMEM;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001428
Marcel Holtmannc13854c2010-02-08 15:27:07 +01001429 hdev->bus = HCI_USB;
David Herrmann155961e2012-02-09 21:58:32 +01001430 hci_set_drvdata(hdev, data);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001431
1432 data->hdev = hdev;
1433
1434 SET_HCIDEV_DEV(hdev, &intf->dev);
1435
Marcel Holtmann9f8f9622013-04-10 08:11:35 -07001436 hdev->open = btusb_open;
1437 hdev->close = btusb_close;
1438 hdev->flush = btusb_flush;
1439 hdev->send = btusb_send_frame;
1440 hdev->notify = btusb_notify;
1441
1442 if (id->driver_info & BTUSB_BCM92035)
1443 hdev->setup = btusb_setup_bcm92035;
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001444
Tedd Ho-Jeong Andffd30e2013-04-19 09:57:43 -07001445 if (id->driver_info & BTUSB_INTEL)
1446 hdev->setup = btusb_setup_intel;
1447
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01001448 /* Interface numbers are hardcoded in the specification */
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001449 data->isoc = usb_ifnum_to_if(data->udev, 1);
1450
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01001451 if (!reset)
Szymon Janca6c511c2012-05-23 12:35:46 +02001452 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02001453
1454 if (force_scofix || id->driver_info & BTUSB_WRONG_SCO_MTU) {
1455 if (!disable_scofix)
1456 set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
1457 }
1458
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001459 if (id->driver_info & BTUSB_BROKEN_ISOC)
1460 data->isoc = NULL;
1461
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01001462 if (id->driver_info & BTUSB_DIGIANSWER) {
1463 data->cmdreq_type = USB_TYPE_VENDOR;
Szymon Janca6c511c2012-05-23 12:35:46 +02001464 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01001465 }
1466
1467 if (id->driver_info & BTUSB_CSR) {
1468 struct usb_device *udev = data->udev;
1469
1470 /* Old firmware would otherwise execute USB reset */
1471 if (le16_to_cpu(udev->descriptor.bcdDevice) < 0x117)
Szymon Janca6c511c2012-05-23 12:35:46 +02001472 set_bit(HCI_QUIRK_RESET_ON_CLOSE, &hdev->quirks);
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01001473 }
1474
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02001475 if (id->driver_info & BTUSB_SNIFFER) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001476 struct usb_device *udev = data->udev;
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02001477
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01001478 /* New sniffer firmware has crippled HCI interface */
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02001479 if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
1480 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001481
1482 data->isoc = NULL;
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02001483 }
1484
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001485 if (data->isoc) {
1486 err = usb_driver_claim_interface(&btusb_driver,
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02001487 data->isoc, data);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001488 if (err < 0) {
1489 hci_free_dev(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001490 return err;
1491 }
1492 }
1493
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001494 err = hci_register_dev(hdev);
1495 if (err < 0) {
1496 hci_free_dev(hdev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001497 return err;
1498 }
1499
1500 usb_set_intfdata(intf, data);
1501
1502 return 0;
1503}
1504
1505static void btusb_disconnect(struct usb_interface *intf)
1506{
1507 struct btusb_data *data = usb_get_intfdata(intf);
1508 struct hci_dev *hdev;
1509
1510 BT_DBG("intf %p", intf);
1511
1512 if (!data)
1513 return;
1514
1515 hdev = data->hdev;
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02001516 usb_set_intfdata(data->intf, NULL);
1517
1518 if (data->isoc)
1519 usb_set_intfdata(data->isoc, NULL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001520
1521 hci_unregister_dev(hdev);
1522
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02001523 if (intf == data->isoc)
1524 usb_driver_release_interface(&btusb_driver, data->intf);
1525 else if (data->isoc)
1526 usb_driver_release_interface(&btusb_driver, data->isoc);
1527
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001528 hci_free_dev(hdev);
1529}
1530
Oliver Neukum7bee5492009-08-24 23:44:59 +02001531#ifdef CONFIG_PM
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001532static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
1533{
1534 struct btusb_data *data = usb_get_intfdata(intf);
1535
1536 BT_DBG("intf %p", intf);
1537
1538 if (data->suspend_count++)
1539 return 0;
1540
Oliver Neukum7bee5492009-08-24 23:44:59 +02001541 spin_lock_irq(&data->txlock);
Alan Stern5b1b0b82011-08-19 23:49:48 +02001542 if (!(PMSG_IS_AUTO(message) && data->tx_in_flight)) {
Oliver Neukum7bee5492009-08-24 23:44:59 +02001543 set_bit(BTUSB_SUSPENDING, &data->flags);
1544 spin_unlock_irq(&data->txlock);
1545 } else {
1546 spin_unlock_irq(&data->txlock);
1547 data->suspend_count--;
1548 return -EBUSY;
1549 }
1550
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001551 cancel_work_sync(&data->work);
1552
Oliver Neukum7bee5492009-08-24 23:44:59 +02001553 btusb_stop_traffic(data);
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001554 usb_kill_anchored_urbs(&data->tx_anchor);
1555
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001556 return 0;
1557}
1558
Oliver Neukum7bee5492009-08-24 23:44:59 +02001559static void play_deferred(struct btusb_data *data)
1560{
1561 struct urb *urb;
1562 int err;
1563
1564 while ((urb = usb_get_from_anchor(&data->deferred))) {
1565 err = usb_submit_urb(urb, GFP_ATOMIC);
1566 if (err < 0)
1567 break;
1568
1569 data->tx_in_flight++;
1570 }
1571 usb_scuttle_anchored_urbs(&data->deferred);
1572}
1573
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001574static int btusb_resume(struct usb_interface *intf)
1575{
1576 struct btusb_data *data = usb_get_intfdata(intf);
1577 struct hci_dev *hdev = data->hdev;
Oliver Neukum7bee5492009-08-24 23:44:59 +02001578 int err = 0;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001579
1580 BT_DBG("intf %p", intf);
1581
1582 if (--data->suspend_count)
1583 return 0;
1584
1585 if (!test_bit(HCI_RUNNING, &hdev->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +02001586 goto done;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001587
1588 if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
1589 err = btusb_submit_intr_urb(hdev, GFP_NOIO);
1590 if (err < 0) {
1591 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001592 goto failed;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001593 }
1594 }
1595
1596 if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) {
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001597 err = btusb_submit_bulk_urb(hdev, GFP_NOIO);
1598 if (err < 0) {
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001599 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001600 goto failed;
1601 }
1602
1603 btusb_submit_bulk_urb(hdev, GFP_NOIO);
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001604 }
1605
1606 if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
1607 if (btusb_submit_isoc_urb(hdev, GFP_NOIO) < 0)
1608 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1609 else
1610 btusb_submit_isoc_urb(hdev, GFP_NOIO);
1611 }
1612
Oliver Neukum7bee5492009-08-24 23:44:59 +02001613 spin_lock_irq(&data->txlock);
1614 play_deferred(data);
1615 clear_bit(BTUSB_SUSPENDING, &data->flags);
1616 spin_unlock_irq(&data->txlock);
1617 schedule_work(&data->work);
1618
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001619 return 0;
Oliver Neukum7bee5492009-08-24 23:44:59 +02001620
1621failed:
1622 usb_scuttle_anchored_urbs(&data->deferred);
1623done:
1624 spin_lock_irq(&data->txlock);
1625 clear_bit(BTUSB_SUSPENDING, &data->flags);
1626 spin_unlock_irq(&data->txlock);
1627
1628 return err;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001629}
Oliver Neukum7bee5492009-08-24 23:44:59 +02001630#endif
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001631
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001632static struct usb_driver btusb_driver = {
1633 .name = "btusb",
1634 .probe = btusb_probe,
1635 .disconnect = btusb_disconnect,
Oliver Neukum7bee5492009-08-24 23:44:59 +02001636#ifdef CONFIG_PM
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001637 .suspend = btusb_suspend,
1638 .resume = btusb_resume,
Oliver Neukum7bee5492009-08-24 23:44:59 +02001639#endif
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001640 .id_table = btusb_table,
Oliver Neukum7bee5492009-08-24 23:44:59 +02001641 .supports_autosuspend = 1,
Sarah Sharpe1f12eb2012-04-23 10:08:51 -07001642 .disable_hub_initiated_lpm = 1,
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001643};
1644
Greg Kroah-Hartman93f15082011-11-18 09:47:34 -08001645module_usb_driver(btusb_driver);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001646
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02001647module_param(ignore_dga, bool, 0644);
1648MODULE_PARM_DESC(ignore_dga, "Ignore devices with id 08fd:0001");
1649
1650module_param(ignore_csr, bool, 0644);
1651MODULE_PARM_DESC(ignore_csr, "Ignore devices with id 0a12:0001");
1652
1653module_param(ignore_sniffer, bool, 0644);
1654MODULE_PARM_DESC(ignore_sniffer, "Ignore devices with id 0a12:0002");
1655
1656module_param(disable_scofix, bool, 0644);
1657MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
1658
1659module_param(force_scofix, bool, 0644);
1660MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
1661
1662module_param(reset, bool, 0644);
1663MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
1664
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001665MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
1666MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
1667MODULE_VERSION(VERSION);
1668MODULE_LICENSE("GPL");