blob: b8483ac0c74df9ee383c377421ea58377f6eff38 [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
24#include <linux/kernel.h>
25#include <linux/module.h>
26#include <linux/init.h>
27#include <linux/slab.h>
28#include <linux/types.h>
29#include <linux/sched.h>
30#include <linux/errno.h>
31#include <linux/skbuff.h>
32
33#include <linux/usb.h>
34
35#include <net/bluetooth/bluetooth.h>
36#include <net/bluetooth/hci_core.h>
37
Oliver Neukum7bee5492009-08-24 23:44:59 +020038#define VERSION "0.6"
Marcel Holtmanncfeb4142008-08-07 22:26:56 +020039
40static int ignore_dga;
41static int ignore_csr;
42static int ignore_sniffer;
43static int disable_scofix;
44static int force_scofix;
Marcel Holtmann7a9d4022008-11-30 12:17:26 +010045
46static int reset = 1;
Marcel Holtmanncfeb4142008-08-07 22:26:56 +020047
48static struct usb_driver btusb_driver;
49
50#define BTUSB_IGNORE 0x01
Marcel Holtmann7a9d4022008-11-30 12:17:26 +010051#define BTUSB_DIGIANSWER 0x02
52#define BTUSB_CSR 0x04
53#define BTUSB_SNIFFER 0x08
54#define BTUSB_BCM92035 0x10
55#define BTUSB_BROKEN_ISOC 0x20
56#define BTUSB_WRONG_SCO_MTU 0x40
Marcel Holtmann5e23b922007-10-20 14:12:34 +020057
58static struct usb_device_id btusb_table[] = {
59 /* Generic Bluetooth USB device */
60 { USB_DEVICE_INFO(0xe0, 0x01, 0x01) },
61
Nobuhiro Iwamatsu3cd01972010-08-20 16:24:07 +090062 /* Apple MacBookPro 7,1 */
63 { USB_DEVICE(0x05ac, 0x8213) },
64
Cyril Lacoux0a79f672010-07-14 10:29:27 +040065 /* Apple iMac11,1 */
66 { USB_DEVICE(0x05ac, 0x8215) },
67
Nobuhiro Iwamatsu9c047152010-08-20 16:24:06 +090068 /* Apple MacBookPro6,2 */
69 { USB_DEVICE(0x05ac, 0x8218) },
70
Edgar (gimli) Hucek3e3ede72010-11-04 08:04:33 +010071 /* Apple MacBookAir3,1, MacBookAir3,2 */
72 { USB_DEVICE(0x05ac, 0x821b) },
73
Pieter-Augustijn Van Malleghemefb14972011-09-07 02:28:10 -040074 /* Apple MacBookAir4,1 */
75 { USB_DEVICE(0x05ac, 0x821f) },
76
Marc-Antoine Perennou88d377b2011-03-24 14:51:21 -030077 /* Apple MacBookPro8,2 */
78 { USB_DEVICE(0x05ac, 0x821a) },
79
Marcel Holtmanncfeb4142008-08-07 22:26:56 +020080 /* AVM BlueFRITZ! USB v2.0 */
81 { USB_DEVICE(0x057c, 0x3800) },
82
83 /* Bluetooth Ultraport Module from IBM */
84 { USB_DEVICE(0x04bf, 0x030a) },
85
86 /* ALPS Modules with non-standard id */
87 { USB_DEVICE(0x044e, 0x3001) },
88 { USB_DEVICE(0x044e, 0x3002) },
89
90 /* Ericsson with non-standard id */
91 { USB_DEVICE(0x0bdb, 0x1002) },
92
93 /* Canyon CN-BTU1 with HID interfaces */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +010094 { USB_DEVICE(0x0c10, 0x0000) },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +020095
Marcel Holtmann5e23b922007-10-20 14:12:34 +020096 { } /* Terminating entry */
97};
98
99MODULE_DEVICE_TABLE(usb, btusb_table);
100
101static struct usb_device_id blacklist_table[] = {
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200102 /* CSR BlueCore devices */
103 { USB_DEVICE(0x0a12, 0x0001), .driver_info = BTUSB_CSR },
104
105 /* Broadcom BCM2033 without firmware */
106 { USB_DEVICE(0x0a5c, 0x2033), .driver_info = BTUSB_IGNORE },
107
Bala Shanmugambe931122010-11-26 17:35:46 +0530108 /* Atheros 3011 with sflash firmware */
109 { USB_DEVICE(0x0cf3, 0x3002), .driver_info = BTUSB_IGNORE },
Andy Ross2a7bccc2011-05-09 16:11:16 -0700110 { USB_DEVICE(0x13d3, 0x3304), .driver_info = BTUSB_IGNORE },
Ricardo Mendoza39361fc2011-07-13 16:04:29 +0100111 { USB_DEVICE(0x0930, 0x0215), .driver_info = BTUSB_IGNORE },
Bala Shanmugambe931122010-11-26 17:35:46 +0530112
Cho, Yu-Chen509e7862011-01-26 17:10:59 +0800113 /* Atheros AR9285 Malbec with sflash firmware */
114 { USB_DEVICE(0x03f0, 0x311d), .driver_info = BTUSB_IGNORE },
115
Bala Shanmugamd9f51b52011-02-11 15:38:53 +0530116 /* Atheros 3012 with sflash firmware */
117 { USB_DEVICE(0x0cf3, 0x3004), .driver_info = BTUSB_IGNORE },
118
Cho, Yu-Chene9036e32011-02-15 10:20:07 +0800119 /* Atheros AR5BBU12 with sflash firmware */
120 { USB_DEVICE(0x0489, 0xe02c), .driver_info = BTUSB_IGNORE },
121
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200122 /* Broadcom BCM2035 */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100123 { USB_DEVICE(0x0a5c, 0x2035), .driver_info = BTUSB_WRONG_SCO_MTU },
124 { USB_DEVICE(0x0a5c, 0x200a), .driver_info = BTUSB_WRONG_SCO_MTU },
125 { USB_DEVICE(0x0a5c, 0x2009), .driver_info = BTUSB_BCM92035 },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200126
127 /* Broadcom BCM2045 */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100128 { USB_DEVICE(0x0a5c, 0x2039), .driver_info = BTUSB_WRONG_SCO_MTU },
129 { USB_DEVICE(0x0a5c, 0x2101), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmannbdbef3d2008-09-23 00:16:35 +0200130
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200131 /* IBM/Lenovo ThinkPad with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100132 { USB_DEVICE(0x0a5c, 0x201e), .driver_info = BTUSB_WRONG_SCO_MTU },
133 { USB_DEVICE(0x0a5c, 0x2110), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200134
135 /* HP laptop with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100136 { USB_DEVICE(0x03f0, 0x171d), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200137
138 /* Dell laptop with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100139 { USB_DEVICE(0x413c, 0x8126), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200140
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100141 /* Dell Wireless 370 and 410 devices */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100142 { USB_DEVICE(0x413c, 0x8152), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100143 { USB_DEVICE(0x413c, 0x8156), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200144
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100145 /* Belkin F8T012 and F8T013 devices */
146 { USB_DEVICE(0x050d, 0x0012), .driver_info = BTUSB_WRONG_SCO_MTU },
147 { USB_DEVICE(0x050d, 0x0013), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200148
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100149 /* Asus WL-BTD202 device */
150 { USB_DEVICE(0x0b05, 0x1715), .driver_info = BTUSB_WRONG_SCO_MTU },
151
152 /* Kensington Bluetooth USB adapter */
153 { USB_DEVICE(0x047d, 0x105e), .driver_info = BTUSB_WRONG_SCO_MTU },
154
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200155 /* RTX Telecom based adapters with buggy SCO support */
156 { USB_DEVICE(0x0400, 0x0807), .driver_info = BTUSB_BROKEN_ISOC },
157 { USB_DEVICE(0x0400, 0x080a), .driver_info = BTUSB_BROKEN_ISOC },
158
159 /* CONWISE Technology based adapters with buggy SCO support */
160 { USB_DEVICE(0x0e5e, 0x6622), .driver_info = BTUSB_BROKEN_ISOC },
161
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200162 /* Digianswer devices */
163 { USB_DEVICE(0x08fd, 0x0001), .driver_info = BTUSB_DIGIANSWER },
164 { USB_DEVICE(0x08fd, 0x0002), .driver_info = BTUSB_IGNORE },
165
166 /* CSR BlueCore Bluetooth Sniffer */
167 { USB_DEVICE(0x0a12, 0x0002), .driver_info = BTUSB_SNIFFER },
168
169 /* Frontline ComProbe Bluetooth Sniffer */
170 { USB_DEVICE(0x16d3, 0x0002), .driver_info = BTUSB_SNIFFER },
171
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200172 { } /* Terminating entry */
173};
174
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200175#define BTUSB_MAX_ISOC_FRAMES 10
176
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200177#define BTUSB_INTR_RUNNING 0
178#define BTUSB_BULK_RUNNING 1
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200179#define BTUSB_ISOC_RUNNING 2
Oliver Neukum7bee5492009-08-24 23:44:59 +0200180#define BTUSB_SUSPENDING 3
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -0300181#define BTUSB_DID_ISO_RESUME 4
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200182
183struct btusb_data {
184 struct hci_dev *hdev;
185 struct usb_device *udev;
Marcel Holtmann5fbcd262008-09-23 00:16:36 +0200186 struct usb_interface *intf;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200187 struct usb_interface *isoc;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200188
189 spinlock_t lock;
190
191 unsigned long flags;
192
193 struct work_struct work;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200194 struct work_struct waker;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200195
196 struct usb_anchor tx_anchor;
197 struct usb_anchor intr_anchor;
198 struct usb_anchor bulk_anchor;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200199 struct usb_anchor isoc_anchor;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200200 struct usb_anchor deferred;
201 int tx_in_flight;
202 spinlock_t txlock;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200203
204 struct usb_endpoint_descriptor *intr_ep;
205 struct usb_endpoint_descriptor *bulk_tx_ep;
206 struct usb_endpoint_descriptor *bulk_rx_ep;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200207 struct usb_endpoint_descriptor *isoc_tx_ep;
208 struct usb_endpoint_descriptor *isoc_rx_ep;
209
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100210 __u8 cmdreq_type;
211
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100212 unsigned int sco_num;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200213 int isoc_altsetting;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +0100214 int suspend_count;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200215};
216
Oliver Neukum7bee5492009-08-24 23:44:59 +0200217static int inc_tx(struct btusb_data *data)
218{
219 unsigned long flags;
220 int rv;
221
222 spin_lock_irqsave(&data->txlock, flags);
223 rv = test_bit(BTUSB_SUSPENDING, &data->flags);
224 if (!rv)
225 data->tx_in_flight++;
226 spin_unlock_irqrestore(&data->txlock, flags);
227
228 return rv;
229}
230
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200231static void btusb_intr_complete(struct urb *urb)
232{
233 struct hci_dev *hdev = urb->context;
234 struct btusb_data *data = hdev->driver_data;
235 int err;
236
237 BT_DBG("%s urb %p status %d count %d", hdev->name,
238 urb, urb->status, urb->actual_length);
239
240 if (!test_bit(HCI_RUNNING, &hdev->flags))
241 return;
242
243 if (urb->status == 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200244 hdev->stat.byte_rx += urb->actual_length;
245
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200246 if (hci_recv_fragment(hdev, HCI_EVENT_PKT,
247 urb->transfer_buffer,
248 urb->actual_length) < 0) {
249 BT_ERR("%s corrupted event packet", hdev->name);
250 hdev->stat.err_rx++;
251 }
252 }
253
254 if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
255 return;
256
Oliver Neukum7bee5492009-08-24 23:44:59 +0200257 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200258 usb_anchor_urb(urb, &data->intr_anchor);
259
260 err = usb_submit_urb(urb, GFP_ATOMIC);
261 if (err < 0) {
Stefan Seyfried61faddf2010-11-30 21:49:08 +0100262 if (err != -EPERM)
263 BT_ERR("%s urb %p failed to resubmit (%d)",
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200264 hdev->name, urb, -err);
265 usb_unanchor_urb(urb);
266 }
267}
268
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100269static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200270{
271 struct btusb_data *data = hdev->driver_data;
272 struct urb *urb;
273 unsigned char *buf;
274 unsigned int pipe;
275 int err, size;
276
277 BT_DBG("%s", hdev->name);
278
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200279 if (!data->intr_ep)
280 return -ENODEV;
281
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100282 urb = usb_alloc_urb(0, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200283 if (!urb)
284 return -ENOMEM;
285
286 size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
287
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100288 buf = kmalloc(size, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200289 if (!buf) {
290 usb_free_urb(urb);
291 return -ENOMEM;
292 }
293
294 pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
295
296 usb_fill_int_urb(urb, data->udev, pipe, buf, size,
297 btusb_intr_complete, hdev,
298 data->intr_ep->bInterval);
299
300 urb->transfer_flags |= URB_FREE_BUFFER;
301
302 usb_anchor_urb(urb, &data->intr_anchor);
303
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100304 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200305 if (err < 0) {
306 BT_ERR("%s urb %p submission failed (%d)",
307 hdev->name, urb, -err);
308 usb_unanchor_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200309 }
310
311 usb_free_urb(urb);
312
313 return err;
314}
315
316static void btusb_bulk_complete(struct urb *urb)
317{
318 struct hci_dev *hdev = urb->context;
319 struct btusb_data *data = hdev->driver_data;
320 int err;
321
322 BT_DBG("%s urb %p status %d count %d", hdev->name,
323 urb, urb->status, urb->actual_length);
324
325 if (!test_bit(HCI_RUNNING, &hdev->flags))
326 return;
327
328 if (urb->status == 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200329 hdev->stat.byte_rx += urb->actual_length;
330
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200331 if (hci_recv_fragment(hdev, HCI_ACLDATA_PKT,
332 urb->transfer_buffer,
333 urb->actual_length) < 0) {
334 BT_ERR("%s corrupted ACL packet", hdev->name);
335 hdev->stat.err_rx++;
336 }
337 }
338
339 if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
340 return;
341
342 usb_anchor_urb(urb, &data->bulk_anchor);
Oliver Neukum652fd782009-12-16 19:23:43 +0100343 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200344
345 err = usb_submit_urb(urb, GFP_ATOMIC);
346 if (err < 0) {
Stefan Seyfried61faddf2010-11-30 21:49:08 +0100347 if (err != -EPERM)
348 BT_ERR("%s urb %p failed to resubmit (%d)",
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200349 hdev->name, urb, -err);
350 usb_unanchor_urb(urb);
351 }
352}
353
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100354static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200355{
356 struct btusb_data *data = hdev->driver_data;
357 struct urb *urb;
358 unsigned char *buf;
359 unsigned int pipe;
Vikram Kandukuri290ba202009-07-02 14:31:59 +0530360 int err, size = HCI_MAX_FRAME_SIZE;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200361
362 BT_DBG("%s", hdev->name);
363
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200364 if (!data->bulk_rx_ep)
365 return -ENODEV;
366
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100367 urb = usb_alloc_urb(0, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200368 if (!urb)
369 return -ENOMEM;
370
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100371 buf = kmalloc(size, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200372 if (!buf) {
373 usb_free_urb(urb);
374 return -ENOMEM;
375 }
376
377 pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
378
379 usb_fill_bulk_urb(urb, data->udev, pipe,
380 buf, size, btusb_bulk_complete, hdev);
381
382 urb->transfer_flags |= URB_FREE_BUFFER;
383
Oliver Neukum7bee5492009-08-24 23:44:59 +0200384 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200385 usb_anchor_urb(urb, &data->bulk_anchor);
386
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100387 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200388 if (err < 0) {
389 BT_ERR("%s urb %p submission failed (%d)",
390 hdev->name, urb, -err);
391 usb_unanchor_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200392 }
393
394 usb_free_urb(urb);
395
396 return err;
397}
398
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200399static void btusb_isoc_complete(struct urb *urb)
400{
401 struct hci_dev *hdev = urb->context;
402 struct btusb_data *data = hdev->driver_data;
403 int i, err;
404
405 BT_DBG("%s urb %p status %d count %d", hdev->name,
406 urb, urb->status, urb->actual_length);
407
408 if (!test_bit(HCI_RUNNING, &hdev->flags))
409 return;
410
411 if (urb->status == 0) {
412 for (i = 0; i < urb->number_of_packets; i++) {
413 unsigned int offset = urb->iso_frame_desc[i].offset;
414 unsigned int length = urb->iso_frame_desc[i].actual_length;
415
416 if (urb->iso_frame_desc[i].status)
417 continue;
418
419 hdev->stat.byte_rx += length;
420
421 if (hci_recv_fragment(hdev, HCI_SCODATA_PKT,
422 urb->transfer_buffer + offset,
423 length) < 0) {
424 BT_ERR("%s corrupted SCO packet", hdev->name);
425 hdev->stat.err_rx++;
426 }
427 }
428 }
429
430 if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags))
431 return;
432
433 usb_anchor_urb(urb, &data->isoc_anchor);
434
435 err = usb_submit_urb(urb, GFP_ATOMIC);
436 if (err < 0) {
Stefan Seyfried61faddf2010-11-30 21:49:08 +0100437 if (err != -EPERM)
438 BT_ERR("%s urb %p failed to resubmit (%d)",
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200439 hdev->name, urb, -err);
440 usb_unanchor_urb(urb);
441 }
442}
443
Jesper Juhl42b16b32011-01-17 00:09:38 +0100444static inline void __fill_isoc_descriptor(struct urb *urb, int len, int mtu)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200445{
446 int i, offset = 0;
447
448 BT_DBG("len %d mtu %d", len, mtu);
449
450 for (i = 0; i < BTUSB_MAX_ISOC_FRAMES && len >= mtu;
451 i++, offset += mtu, len -= mtu) {
452 urb->iso_frame_desc[i].offset = offset;
453 urb->iso_frame_desc[i].length = mtu;
454 }
455
456 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
457 urb->iso_frame_desc[i].offset = offset;
458 urb->iso_frame_desc[i].length = len;
459 i++;
460 }
461
462 urb->number_of_packets = i;
463}
464
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100465static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200466{
467 struct btusb_data *data = hdev->driver_data;
468 struct urb *urb;
469 unsigned char *buf;
470 unsigned int pipe;
471 int err, size;
472
473 BT_DBG("%s", hdev->name);
474
475 if (!data->isoc_rx_ep)
476 return -ENODEV;
477
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100478 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200479 if (!urb)
480 return -ENOMEM;
481
482 size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) *
483 BTUSB_MAX_ISOC_FRAMES;
484
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100485 buf = kmalloc(size, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200486 if (!buf) {
487 usb_free_urb(urb);
488 return -ENOMEM;
489 }
490
491 pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress);
492
493 urb->dev = data->udev;
494 urb->pipe = pipe;
495 urb->context = hdev;
496 urb->complete = btusb_isoc_complete;
497 urb->interval = data->isoc_rx_ep->bInterval;
498
499 urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP;
500 urb->transfer_buffer = buf;
501 urb->transfer_buffer_length = size;
502
503 __fill_isoc_descriptor(urb, size,
504 le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize));
505
506 usb_anchor_urb(urb, &data->isoc_anchor);
507
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100508 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200509 if (err < 0) {
510 BT_ERR("%s urb %p submission failed (%d)",
511 hdev->name, urb, -err);
512 usb_unanchor_urb(urb);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200513 }
514
515 usb_free_urb(urb);
516
517 return err;
518}
519
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200520static void btusb_tx_complete(struct urb *urb)
521{
522 struct sk_buff *skb = urb->context;
523 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200524 struct btusb_data *data = hdev->driver_data;
525
526 BT_DBG("%s urb %p status %d count %d", hdev->name,
527 urb, urb->status, urb->actual_length);
528
529 if (!test_bit(HCI_RUNNING, &hdev->flags))
530 goto done;
531
532 if (!urb->status)
533 hdev->stat.byte_tx += urb->transfer_buffer_length;
534 else
535 hdev->stat.err_tx++;
536
537done:
538 spin_lock(&data->txlock);
539 data->tx_in_flight--;
540 spin_unlock(&data->txlock);
541
542 kfree(urb->setup_packet);
543
544 kfree_skb(skb);
545}
546
547static void btusb_isoc_tx_complete(struct urb *urb)
548{
549 struct sk_buff *skb = urb->context;
550 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200551
552 BT_DBG("%s urb %p status %d count %d", hdev->name,
553 urb, urb->status, urb->actual_length);
554
555 if (!test_bit(HCI_RUNNING, &hdev->flags))
556 goto done;
557
558 if (!urb->status)
559 hdev->stat.byte_tx += urb->transfer_buffer_length;
560 else
561 hdev->stat.err_tx++;
562
563done:
564 kfree(urb->setup_packet);
565
566 kfree_skb(skb);
567}
568
569static int btusb_open(struct hci_dev *hdev)
570{
571 struct btusb_data *data = hdev->driver_data;
572 int err;
573
574 BT_DBG("%s", hdev->name);
575
Oliver Neukum7bee5492009-08-24 23:44:59 +0200576 err = usb_autopm_get_interface(data->intf);
577 if (err < 0)
578 return err;
579
580 data->intf->needs_remote_wakeup = 1;
581
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200582 if (test_and_set_bit(HCI_RUNNING, &hdev->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +0200583 goto done;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200584
585 if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +0200586 goto done;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200587
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100588 err = btusb_submit_intr_urb(hdev, GFP_KERNEL);
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100589 if (err < 0)
590 goto failed;
591
592 err = btusb_submit_bulk_urb(hdev, GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200593 if (err < 0) {
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100594 usb_kill_anchored_urbs(&data->intr_anchor);
595 goto failed;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200596 }
597
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100598 set_bit(BTUSB_BULK_RUNNING, &data->flags);
599 btusb_submit_bulk_urb(hdev, GFP_KERNEL);
600
Oliver Neukum7bee5492009-08-24 23:44:59 +0200601done:
602 usb_autopm_put_interface(data->intf);
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100603 return 0;
604
605failed:
606 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
607 clear_bit(HCI_RUNNING, &hdev->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200608 usb_autopm_put_interface(data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200609 return err;
610}
611
Oliver Neukum7bee5492009-08-24 23:44:59 +0200612static void btusb_stop_traffic(struct btusb_data *data)
613{
614 usb_kill_anchored_urbs(&data->intr_anchor);
615 usb_kill_anchored_urbs(&data->bulk_anchor);
616 usb_kill_anchored_urbs(&data->isoc_anchor);
617}
618
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200619static int btusb_close(struct hci_dev *hdev)
620{
621 struct btusb_data *data = hdev->driver_data;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200622 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200623
624 BT_DBG("%s", hdev->name);
625
626 if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
627 return 0;
628
Marcel Holtmanne8c3c3d2008-09-23 00:16:36 +0200629 cancel_work_sync(&data->work);
Linus Torvalds404291a2009-11-11 13:32:29 -0800630 cancel_work_sync(&data->waker);
Marcel Holtmanne8c3c3d2008-09-23 00:16:36 +0200631
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200632 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200633 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200634 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200635
636 btusb_stop_traffic(data);
637 err = usb_autopm_get_interface(data->intf);
638 if (err < 0)
Oliver Neukum7b8e2c12009-11-13 14:26:23 +0100639 goto failed;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200640
641 data->intf->needs_remote_wakeup = 0;
642 usb_autopm_put_interface(data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200643
Oliver Neukum7b8e2c12009-11-13 14:26:23 +0100644failed:
645 usb_scuttle_anchored_urbs(&data->deferred);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200646 return 0;
647}
648
649static int btusb_flush(struct hci_dev *hdev)
650{
651 struct btusb_data *data = hdev->driver_data;
652
653 BT_DBG("%s", hdev->name);
654
655 usb_kill_anchored_urbs(&data->tx_anchor);
656
657 return 0;
658}
659
660static int btusb_send_frame(struct sk_buff *skb)
661{
662 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
663 struct btusb_data *data = hdev->driver_data;
664 struct usb_ctrlrequest *dr;
665 struct urb *urb;
666 unsigned int pipe;
667 int err;
668
669 BT_DBG("%s", hdev->name);
670
671 if (!test_bit(HCI_RUNNING, &hdev->flags))
672 return -EBUSY;
673
674 switch (bt_cb(skb)->pkt_type) {
675 case HCI_COMMAND_PKT:
676 urb = usb_alloc_urb(0, GFP_ATOMIC);
677 if (!urb)
678 return -ENOMEM;
679
680 dr = kmalloc(sizeof(*dr), GFP_ATOMIC);
681 if (!dr) {
682 usb_free_urb(urb);
683 return -ENOMEM;
684 }
685
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100686 dr->bRequestType = data->cmdreq_type;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200687 dr->bRequest = 0;
688 dr->wIndex = 0;
689 dr->wValue = 0;
690 dr->wLength = __cpu_to_le16(skb->len);
691
692 pipe = usb_sndctrlpipe(data->udev, 0x00);
693
694 usb_fill_control_urb(urb, data->udev, pipe, (void *) dr,
695 skb->data, skb->len, btusb_tx_complete, skb);
696
697 hdev->stat.cmd_tx++;
698 break;
699
700 case HCI_ACLDATA_PKT:
Vinicius Costa Gomes8693ac92011-03-14 18:20:33 -0300701 if (!data->bulk_tx_ep || (hdev->conn_hash.acl_num < 1 &&
702 hdev->conn_hash.le_num < 1))
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200703 return -ENODEV;
704
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200705 urb = usb_alloc_urb(0, GFP_ATOMIC);
706 if (!urb)
707 return -ENOMEM;
708
709 pipe = usb_sndbulkpipe(data->udev,
710 data->bulk_tx_ep->bEndpointAddress);
711
712 usb_fill_bulk_urb(urb, data->udev, pipe,
713 skb->data, skb->len, btusb_tx_complete, skb);
714
715 hdev->stat.acl_tx++;
716 break;
717
718 case HCI_SCODATA_PKT:
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200719 if (!data->isoc_tx_ep || hdev->conn_hash.sco_num < 1)
720 return -ENODEV;
721
722 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_ATOMIC);
723 if (!urb)
724 return -ENOMEM;
725
726 pipe = usb_sndisocpipe(data->udev,
727 data->isoc_tx_ep->bEndpointAddress);
728
Gustavo F. Padovan03c2d0e2011-02-14 18:53:43 -0300729 usb_fill_int_urb(urb, data->udev, pipe,
730 skb->data, skb->len, btusb_isoc_tx_complete,
731 skb, data->isoc_tx_ep->bInterval);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200732
733 urb->transfer_flags = URB_ISO_ASAP;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200734
735 __fill_isoc_descriptor(urb, skb->len,
736 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
737
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200738 hdev->stat.sco_tx++;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200739 goto skip_waking;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200740
741 default:
742 return -EILSEQ;
743 }
744
Oliver Neukum7bee5492009-08-24 23:44:59 +0200745 err = inc_tx(data);
746 if (err) {
747 usb_anchor_urb(urb, &data->deferred);
748 schedule_work(&data->waker);
749 err = 0;
750 goto done;
751 }
752
753skip_waking:
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200754 usb_anchor_urb(urb, &data->tx_anchor);
755
756 err = usb_submit_urb(urb, GFP_ATOMIC);
757 if (err < 0) {
758 BT_ERR("%s urb %p submission failed", hdev->name, urb);
759 kfree(urb->setup_packet);
760 usb_unanchor_urb(urb);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200761 } else {
762 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200763 }
764
765 usb_free_urb(urb);
766
Oliver Neukum7bee5492009-08-24 23:44:59 +0200767done:
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200768 return err;
769}
770
771static void btusb_destruct(struct hci_dev *hdev)
772{
773 struct btusb_data *data = hdev->driver_data;
774
775 BT_DBG("%s", hdev->name);
776
777 kfree(data);
778}
779
780static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
781{
782 struct btusb_data *data = hdev->driver_data;
783
784 BT_DBG("%s evt %d", hdev->name, evt);
785
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100786 if (hdev->conn_hash.sco_num != data->sco_num) {
787 data->sco_num = hdev->conn_hash.sco_num;
788 schedule_work(&data->work);
Marcel Holtmanna780efa2008-11-30 12:17:12 +0100789 }
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200790}
791
Jesper Juhl42b16b32011-01-17 00:09:38 +0100792static inline int __set_isoc_interface(struct hci_dev *hdev, int altsetting)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200793{
794 struct btusb_data *data = hdev->driver_data;
795 struct usb_interface *intf = data->isoc;
796 struct usb_endpoint_descriptor *ep_desc;
797 int i, err;
798
799 if (!data->isoc)
800 return -ENODEV;
801
802 err = usb_set_interface(data->udev, 1, altsetting);
803 if (err < 0) {
804 BT_ERR("%s setting interface failed (%d)", hdev->name, -err);
805 return err;
806 }
807
808 data->isoc_altsetting = altsetting;
809
810 data->isoc_tx_ep = NULL;
811 data->isoc_rx_ep = NULL;
812
813 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
814 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
815
816 if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) {
817 data->isoc_tx_ep = ep_desc;
818 continue;
819 }
820
821 if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) {
822 data->isoc_rx_ep = ep_desc;
823 continue;
824 }
825 }
826
827 if (!data->isoc_tx_ep || !data->isoc_rx_ep) {
828 BT_ERR("%s invalid SCO descriptors", hdev->name);
829 return -ENODEV;
830 }
831
832 return 0;
833}
834
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200835static void btusb_work(struct work_struct *work)
836{
837 struct btusb_data *data = container_of(work, struct btusb_data, work);
838 struct hci_dev *hdev = data->hdev;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200839 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200840
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200841 if (hdev->conn_hash.sco_num > 0) {
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -0300842 if (!test_bit(BTUSB_DID_ISO_RESUME, &data->flags)) {
Oliver Neukum8efdd0c2011-02-11 13:00:06 +0100843 err = usb_autopm_get_interface(data->isoc ? data->isoc : data->intf);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200844 if (err < 0) {
845 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
846 usb_kill_anchored_urbs(&data->isoc_anchor);
847 return;
848 }
849
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -0300850 set_bit(BTUSB_DID_ISO_RESUME, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200851 }
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200852 if (data->isoc_altsetting != 2) {
853 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
854 usb_kill_anchored_urbs(&data->isoc_anchor);
855
856 if (__set_isoc_interface(hdev, 2) < 0)
857 return;
858 }
859
860 if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100861 if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200862 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
863 else
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100864 btusb_submit_isoc_urb(hdev, GFP_KERNEL);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200865 }
866 } else {
867 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
868 usb_kill_anchored_urbs(&data->isoc_anchor);
869
870 __set_isoc_interface(hdev, 0);
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -0300871 if (test_and_clear_bit(BTUSB_DID_ISO_RESUME, &data->flags))
Oliver Neukum8efdd0c2011-02-11 13:00:06 +0100872 usb_autopm_put_interface(data->isoc ? data->isoc : data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200873 }
874}
875
Oliver Neukum7bee5492009-08-24 23:44:59 +0200876static void btusb_waker(struct work_struct *work)
877{
878 struct btusb_data *data = container_of(work, struct btusb_data, waker);
879 int err;
880
881 err = usb_autopm_get_interface(data->intf);
882 if (err < 0)
883 return;
884
885 usb_autopm_put_interface(data->intf);
886}
887
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200888static int btusb_probe(struct usb_interface *intf,
889 const struct usb_device_id *id)
890{
891 struct usb_endpoint_descriptor *ep_desc;
892 struct btusb_data *data;
893 struct hci_dev *hdev;
894 int i, err;
895
896 BT_DBG("intf %p id %p", intf, id);
897
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200898 /* interface numbers are hardcoded in the spec */
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200899 if (intf->cur_altsetting->desc.bInterfaceNumber != 0)
900 return -ENODEV;
901
902 if (!id->driver_info) {
903 const struct usb_device_id *match;
904 match = usb_match_id(intf, blacklist_table);
905 if (match)
906 id = match;
907 }
908
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200909 if (id->driver_info == BTUSB_IGNORE)
910 return -ENODEV;
911
912 if (ignore_dga && id->driver_info & BTUSB_DIGIANSWER)
913 return -ENODEV;
914
915 if (ignore_csr && id->driver_info & BTUSB_CSR)
916 return -ENODEV;
917
918 if (ignore_sniffer && id->driver_info & BTUSB_SNIFFER)
919 return -ENODEV;
920
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200921 data = kzalloc(sizeof(*data), GFP_KERNEL);
922 if (!data)
923 return -ENOMEM;
924
925 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
926 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
927
928 if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
929 data->intr_ep = ep_desc;
930 continue;
931 }
932
933 if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
934 data->bulk_tx_ep = ep_desc;
935 continue;
936 }
937
938 if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
939 data->bulk_rx_ep = ep_desc;
940 continue;
941 }
942 }
943
944 if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep) {
945 kfree(data);
946 return -ENODEV;
947 }
948
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100949 data->cmdreq_type = USB_TYPE_CLASS;
950
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200951 data->udev = interface_to_usbdev(intf);
Marcel Holtmann5fbcd262008-09-23 00:16:36 +0200952 data->intf = intf;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200953
954 spin_lock_init(&data->lock);
955
956 INIT_WORK(&data->work, btusb_work);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200957 INIT_WORK(&data->waker, btusb_waker);
958 spin_lock_init(&data->txlock);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200959
960 init_usb_anchor(&data->tx_anchor);
961 init_usb_anchor(&data->intr_anchor);
962 init_usb_anchor(&data->bulk_anchor);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200963 init_usb_anchor(&data->isoc_anchor);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200964 init_usb_anchor(&data->deferred);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200965
966 hdev = hci_alloc_dev();
967 if (!hdev) {
968 kfree(data);
969 return -ENOMEM;
970 }
971
Marcel Holtmannc13854ce2010-02-08 15:27:07 +0100972 hdev->bus = HCI_USB;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200973 hdev->driver_data = data;
974
975 data->hdev = hdev;
976
977 SET_HCIDEV_DEV(hdev, &intf->dev);
978
979 hdev->open = btusb_open;
980 hdev->close = btusb_close;
981 hdev->flush = btusb_flush;
982 hdev->send = btusb_send_frame;
983 hdev->destruct = btusb_destruct;
984 hdev->notify = btusb_notify;
985
986 hdev->owner = THIS_MODULE;
987
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100988 /* Interface numbers are hardcoded in the specification */
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200989 data->isoc = usb_ifnum_to_if(data->udev, 1);
990
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100991 if (!reset)
992 set_bit(HCI_QUIRK_NO_RESET, &hdev->quirks);
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200993
994 if (force_scofix || id->driver_info & BTUSB_WRONG_SCO_MTU) {
995 if (!disable_scofix)
996 set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
997 }
998
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200999 if (id->driver_info & BTUSB_BROKEN_ISOC)
1000 data->isoc = NULL;
1001
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01001002 if (id->driver_info & BTUSB_DIGIANSWER) {
1003 data->cmdreq_type = USB_TYPE_VENDOR;
1004 set_bit(HCI_QUIRK_NO_RESET, &hdev->quirks);
1005 }
1006
1007 if (id->driver_info & BTUSB_CSR) {
1008 struct usb_device *udev = data->udev;
1009
1010 /* Old firmware would otherwise execute USB reset */
1011 if (le16_to_cpu(udev->descriptor.bcdDevice) < 0x117)
1012 set_bit(HCI_QUIRK_NO_RESET, &hdev->quirks);
1013 }
1014
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02001015 if (id->driver_info & BTUSB_SNIFFER) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001016 struct usb_device *udev = data->udev;
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02001017
Marcel Holtmann7a9d4022008-11-30 12:17:26 +01001018 /* New sniffer firmware has crippled HCI interface */
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02001019 if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
1020 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001021
1022 data->isoc = NULL;
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02001023 }
1024
1025 if (id->driver_info & BTUSB_BCM92035) {
1026 unsigned char cmd[] = { 0x3b, 0xfc, 0x01, 0x00 };
1027 struct sk_buff *skb;
1028
1029 skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL);
1030 if (skb) {
1031 memcpy(skb_put(skb, sizeof(cmd)), cmd, sizeof(cmd));
1032 skb_queue_tail(&hdev->driver_init, skb);
1033 }
1034 }
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001035
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001036 if (data->isoc) {
1037 err = usb_driver_claim_interface(&btusb_driver,
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02001038 data->isoc, data);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001039 if (err < 0) {
1040 hci_free_dev(hdev);
1041 kfree(data);
1042 return err;
1043 }
1044 }
1045
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001046 err = hci_register_dev(hdev);
1047 if (err < 0) {
1048 hci_free_dev(hdev);
1049 kfree(data);
1050 return err;
1051 }
1052
1053 usb_set_intfdata(intf, data);
1054
1055 return 0;
1056}
1057
1058static void btusb_disconnect(struct usb_interface *intf)
1059{
1060 struct btusb_data *data = usb_get_intfdata(intf);
1061 struct hci_dev *hdev;
1062
1063 BT_DBG("intf %p", intf);
1064
1065 if (!data)
1066 return;
1067
1068 hdev = data->hdev;
1069
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02001070 __hci_dev_hold(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001071
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02001072 usb_set_intfdata(data->intf, NULL);
1073
1074 if (data->isoc)
1075 usb_set_intfdata(data->isoc, NULL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001076
1077 hci_unregister_dev(hdev);
1078
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02001079 if (intf == data->isoc)
1080 usb_driver_release_interface(&btusb_driver, data->intf);
1081 else if (data->isoc)
1082 usb_driver_release_interface(&btusb_driver, data->isoc);
1083
1084 __hci_dev_put(hdev);
1085
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001086 hci_free_dev(hdev);
1087}
1088
Oliver Neukum7bee5492009-08-24 23:44:59 +02001089#ifdef CONFIG_PM
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001090static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
1091{
1092 struct btusb_data *data = usb_get_intfdata(intf);
1093
1094 BT_DBG("intf %p", intf);
1095
1096 if (data->suspend_count++)
1097 return 0;
1098
Oliver Neukum7bee5492009-08-24 23:44:59 +02001099 spin_lock_irq(&data->txlock);
Alan Sternfb34d532009-11-13 11:53:59 -05001100 if (!((message.event & PM_EVENT_AUTO) && data->tx_in_flight)) {
Oliver Neukum7bee5492009-08-24 23:44:59 +02001101 set_bit(BTUSB_SUSPENDING, &data->flags);
1102 spin_unlock_irq(&data->txlock);
1103 } else {
1104 spin_unlock_irq(&data->txlock);
1105 data->suspend_count--;
1106 return -EBUSY;
1107 }
1108
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001109 cancel_work_sync(&data->work);
1110
Oliver Neukum7bee5492009-08-24 23:44:59 +02001111 btusb_stop_traffic(data);
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001112 usb_kill_anchored_urbs(&data->tx_anchor);
1113
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001114 return 0;
1115}
1116
Oliver Neukum7bee5492009-08-24 23:44:59 +02001117static void play_deferred(struct btusb_data *data)
1118{
1119 struct urb *urb;
1120 int err;
1121
1122 while ((urb = usb_get_from_anchor(&data->deferred))) {
1123 err = usb_submit_urb(urb, GFP_ATOMIC);
1124 if (err < 0)
1125 break;
1126
1127 data->tx_in_flight++;
1128 }
1129 usb_scuttle_anchored_urbs(&data->deferred);
1130}
1131
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001132static int btusb_resume(struct usb_interface *intf)
1133{
1134 struct btusb_data *data = usb_get_intfdata(intf);
1135 struct hci_dev *hdev = data->hdev;
Oliver Neukum7bee5492009-08-24 23:44:59 +02001136 int err = 0;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001137
1138 BT_DBG("intf %p", intf);
1139
1140 if (--data->suspend_count)
1141 return 0;
1142
1143 if (!test_bit(HCI_RUNNING, &hdev->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +02001144 goto done;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001145
1146 if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
1147 err = btusb_submit_intr_urb(hdev, GFP_NOIO);
1148 if (err < 0) {
1149 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001150 goto failed;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001151 }
1152 }
1153
1154 if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) {
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001155 err = btusb_submit_bulk_urb(hdev, GFP_NOIO);
1156 if (err < 0) {
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001157 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001158 goto failed;
1159 }
1160
1161 btusb_submit_bulk_urb(hdev, GFP_NOIO);
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001162 }
1163
1164 if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
1165 if (btusb_submit_isoc_urb(hdev, GFP_NOIO) < 0)
1166 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1167 else
1168 btusb_submit_isoc_urb(hdev, GFP_NOIO);
1169 }
1170
Oliver Neukum7bee5492009-08-24 23:44:59 +02001171 spin_lock_irq(&data->txlock);
1172 play_deferred(data);
1173 clear_bit(BTUSB_SUSPENDING, &data->flags);
1174 spin_unlock_irq(&data->txlock);
1175 schedule_work(&data->work);
1176
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001177 return 0;
Oliver Neukum7bee5492009-08-24 23:44:59 +02001178
1179failed:
1180 usb_scuttle_anchored_urbs(&data->deferred);
1181done:
1182 spin_lock_irq(&data->txlock);
1183 clear_bit(BTUSB_SUSPENDING, &data->flags);
1184 spin_unlock_irq(&data->txlock);
1185
1186 return err;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001187}
Oliver Neukum7bee5492009-08-24 23:44:59 +02001188#endif
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001189
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001190static struct usb_driver btusb_driver = {
1191 .name = "btusb",
1192 .probe = btusb_probe,
1193 .disconnect = btusb_disconnect,
Oliver Neukum7bee5492009-08-24 23:44:59 +02001194#ifdef CONFIG_PM
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001195 .suspend = btusb_suspend,
1196 .resume = btusb_resume,
Oliver Neukum7bee5492009-08-24 23:44:59 +02001197#endif
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001198 .id_table = btusb_table,
Oliver Neukum7bee5492009-08-24 23:44:59 +02001199 .supports_autosuspend = 1,
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001200};
1201
1202static int __init btusb_init(void)
1203{
1204 BT_INFO("Generic Bluetooth USB driver ver %s", VERSION);
1205
1206 return usb_register(&btusb_driver);
1207}
1208
1209static void __exit btusb_exit(void)
1210{
1211 usb_deregister(&btusb_driver);
1212}
1213
1214module_init(btusb_init);
1215module_exit(btusb_exit);
1216
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02001217module_param(ignore_dga, bool, 0644);
1218MODULE_PARM_DESC(ignore_dga, "Ignore devices with id 08fd:0001");
1219
1220module_param(ignore_csr, bool, 0644);
1221MODULE_PARM_DESC(ignore_csr, "Ignore devices with id 0a12:0001");
1222
1223module_param(ignore_sniffer, bool, 0644);
1224MODULE_PARM_DESC(ignore_sniffer, "Ignore devices with id 0a12:0002");
1225
1226module_param(disable_scofix, bool, 0644);
1227MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
1228
1229module_param(force_scofix, bool, 0644);
1230MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
1231
1232module_param(reset, bool, 0644);
1233MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
1234
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001235MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
1236MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
1237MODULE_VERSION(VERSION);
1238MODULE_LICENSE("GPL");