blob: d120a5c1c0939b1061aae61b6c983da76cf37446 [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
Marcel Holtmanncfeb4142008-08-07 22:26:56 +020071 /* AVM BlueFRITZ! USB v2.0 */
72 { USB_DEVICE(0x057c, 0x3800) },
73
74 /* Bluetooth Ultraport Module from IBM */
75 { USB_DEVICE(0x04bf, 0x030a) },
76
77 /* ALPS Modules with non-standard id */
78 { USB_DEVICE(0x044e, 0x3001) },
79 { USB_DEVICE(0x044e, 0x3002) },
80
81 /* Ericsson with non-standard id */
82 { USB_DEVICE(0x0bdb, 0x1002) },
83
84 /* Canyon CN-BTU1 with HID interfaces */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +010085 { USB_DEVICE(0x0c10, 0x0000) },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +020086
Marcel Holtmann5e23b922007-10-20 14:12:34 +020087 { } /* Terminating entry */
88};
89
90MODULE_DEVICE_TABLE(usb, btusb_table);
91
92static struct usb_device_id blacklist_table[] = {
Marcel Holtmanncfeb4142008-08-07 22:26:56 +020093 /* CSR BlueCore devices */
94 { USB_DEVICE(0x0a12, 0x0001), .driver_info = BTUSB_CSR },
95
96 /* Broadcom BCM2033 without firmware */
97 { USB_DEVICE(0x0a5c, 0x2033), .driver_info = BTUSB_IGNORE },
98
99 /* Broadcom BCM2035 */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100100 { USB_DEVICE(0x0a5c, 0x2035), .driver_info = BTUSB_WRONG_SCO_MTU },
101 { USB_DEVICE(0x0a5c, 0x200a), .driver_info = BTUSB_WRONG_SCO_MTU },
102 { USB_DEVICE(0x0a5c, 0x2009), .driver_info = BTUSB_BCM92035 },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200103
104 /* Broadcom BCM2045 */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100105 { USB_DEVICE(0x0a5c, 0x2039), .driver_info = BTUSB_WRONG_SCO_MTU },
106 { USB_DEVICE(0x0a5c, 0x2101), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmannbdbef3d2008-09-23 00:16:35 +0200107
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200108 /* IBM/Lenovo ThinkPad with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100109 { USB_DEVICE(0x0a5c, 0x201e), .driver_info = BTUSB_WRONG_SCO_MTU },
110 { USB_DEVICE(0x0a5c, 0x2110), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200111
112 /* HP laptop with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100113 { USB_DEVICE(0x03f0, 0x171d), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200114
115 /* Dell laptop with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100116 { USB_DEVICE(0x413c, 0x8126), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200117
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100118 /* Dell Wireless 370 and 410 devices */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100119 { USB_DEVICE(0x413c, 0x8152), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100120 { USB_DEVICE(0x413c, 0x8156), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200121
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100122 /* Belkin F8T012 and F8T013 devices */
123 { USB_DEVICE(0x050d, 0x0012), .driver_info = BTUSB_WRONG_SCO_MTU },
124 { USB_DEVICE(0x050d, 0x0013), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200125
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100126 /* Asus WL-BTD202 device */
127 { USB_DEVICE(0x0b05, 0x1715), .driver_info = BTUSB_WRONG_SCO_MTU },
128
129 /* Kensington Bluetooth USB adapter */
130 { USB_DEVICE(0x047d, 0x105e), .driver_info = BTUSB_WRONG_SCO_MTU },
131
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200132 /* RTX Telecom based adapters with buggy SCO support */
133 { USB_DEVICE(0x0400, 0x0807), .driver_info = BTUSB_BROKEN_ISOC },
134 { USB_DEVICE(0x0400, 0x080a), .driver_info = BTUSB_BROKEN_ISOC },
135
136 /* CONWISE Technology based adapters with buggy SCO support */
137 { USB_DEVICE(0x0e5e, 0x6622), .driver_info = BTUSB_BROKEN_ISOC },
138
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200139 /* Digianswer devices */
140 { USB_DEVICE(0x08fd, 0x0001), .driver_info = BTUSB_DIGIANSWER },
141 { USB_DEVICE(0x08fd, 0x0002), .driver_info = BTUSB_IGNORE },
142
143 /* CSR BlueCore Bluetooth Sniffer */
144 { USB_DEVICE(0x0a12, 0x0002), .driver_info = BTUSB_SNIFFER },
145
146 /* Frontline ComProbe Bluetooth Sniffer */
147 { USB_DEVICE(0x16d3, 0x0002), .driver_info = BTUSB_SNIFFER },
148
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200149 { } /* Terminating entry */
150};
151
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200152#define BTUSB_MAX_ISOC_FRAMES 10
153
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200154#define BTUSB_INTR_RUNNING 0
155#define BTUSB_BULK_RUNNING 1
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200156#define BTUSB_ISOC_RUNNING 2
Oliver Neukum7bee5492009-08-24 23:44:59 +0200157#define BTUSB_SUSPENDING 3
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -0300158#define BTUSB_DID_ISO_RESUME 4
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200159
160struct btusb_data {
161 struct hci_dev *hdev;
162 struct usb_device *udev;
Marcel Holtmann5fbcd262008-09-23 00:16:36 +0200163 struct usb_interface *intf;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200164 struct usb_interface *isoc;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200165
166 spinlock_t lock;
167
168 unsigned long flags;
169
170 struct work_struct work;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200171 struct work_struct waker;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200172
173 struct usb_anchor tx_anchor;
174 struct usb_anchor intr_anchor;
175 struct usb_anchor bulk_anchor;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200176 struct usb_anchor isoc_anchor;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200177 struct usb_anchor deferred;
178 int tx_in_flight;
179 spinlock_t txlock;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200180
181 struct usb_endpoint_descriptor *intr_ep;
182 struct usb_endpoint_descriptor *bulk_tx_ep;
183 struct usb_endpoint_descriptor *bulk_rx_ep;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200184 struct usb_endpoint_descriptor *isoc_tx_ep;
185 struct usb_endpoint_descriptor *isoc_rx_ep;
186
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100187 __u8 cmdreq_type;
188
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100189 unsigned int sco_num;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200190 int isoc_altsetting;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +0100191 int suspend_count;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200192};
193
Oliver Neukum7bee5492009-08-24 23:44:59 +0200194static int inc_tx(struct btusb_data *data)
195{
196 unsigned long flags;
197 int rv;
198
199 spin_lock_irqsave(&data->txlock, flags);
200 rv = test_bit(BTUSB_SUSPENDING, &data->flags);
201 if (!rv)
202 data->tx_in_flight++;
203 spin_unlock_irqrestore(&data->txlock, flags);
204
205 return rv;
206}
207
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200208static void btusb_intr_complete(struct urb *urb)
209{
210 struct hci_dev *hdev = urb->context;
211 struct btusb_data *data = hdev->driver_data;
212 int err;
213
214 BT_DBG("%s urb %p status %d count %d", hdev->name,
215 urb, urb->status, urb->actual_length);
216
217 if (!test_bit(HCI_RUNNING, &hdev->flags))
218 return;
219
220 if (urb->status == 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200221 hdev->stat.byte_rx += urb->actual_length;
222
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200223 if (hci_recv_fragment(hdev, HCI_EVENT_PKT,
224 urb->transfer_buffer,
225 urb->actual_length) < 0) {
226 BT_ERR("%s corrupted event packet", hdev->name);
227 hdev->stat.err_rx++;
228 }
229 }
230
231 if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
232 return;
233
Oliver Neukum7bee5492009-08-24 23:44:59 +0200234 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200235 usb_anchor_urb(urb, &data->intr_anchor);
236
237 err = usb_submit_urb(urb, GFP_ATOMIC);
238 if (err < 0) {
239 BT_ERR("%s urb %p failed to resubmit (%d)",
240 hdev->name, urb, -err);
241 usb_unanchor_urb(urb);
242 }
243}
244
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100245static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200246{
247 struct btusb_data *data = hdev->driver_data;
248 struct urb *urb;
249 unsigned char *buf;
250 unsigned int pipe;
251 int err, size;
252
253 BT_DBG("%s", hdev->name);
254
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200255 if (!data->intr_ep)
256 return -ENODEV;
257
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100258 urb = usb_alloc_urb(0, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200259 if (!urb)
260 return -ENOMEM;
261
262 size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
263
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100264 buf = kmalloc(size, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200265 if (!buf) {
266 usb_free_urb(urb);
267 return -ENOMEM;
268 }
269
270 pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
271
272 usb_fill_int_urb(urb, data->udev, pipe, buf, size,
273 btusb_intr_complete, hdev,
274 data->intr_ep->bInterval);
275
276 urb->transfer_flags |= URB_FREE_BUFFER;
277
278 usb_anchor_urb(urb, &data->intr_anchor);
279
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100280 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200281 if (err < 0) {
282 BT_ERR("%s urb %p submission failed (%d)",
283 hdev->name, urb, -err);
284 usb_unanchor_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200285 }
286
287 usb_free_urb(urb);
288
289 return err;
290}
291
292static void btusb_bulk_complete(struct urb *urb)
293{
294 struct hci_dev *hdev = urb->context;
295 struct btusb_data *data = hdev->driver_data;
296 int err;
297
298 BT_DBG("%s urb %p status %d count %d", hdev->name,
299 urb, urb->status, urb->actual_length);
300
301 if (!test_bit(HCI_RUNNING, &hdev->flags))
302 return;
303
304 if (urb->status == 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200305 hdev->stat.byte_rx += urb->actual_length;
306
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200307 if (hci_recv_fragment(hdev, HCI_ACLDATA_PKT,
308 urb->transfer_buffer,
309 urb->actual_length) < 0) {
310 BT_ERR("%s corrupted ACL packet", hdev->name);
311 hdev->stat.err_rx++;
312 }
313 }
314
315 if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
316 return;
317
318 usb_anchor_urb(urb, &data->bulk_anchor);
Oliver Neukum652fd782009-12-16 19:23:43 +0100319 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200320
321 err = usb_submit_urb(urb, GFP_ATOMIC);
322 if (err < 0) {
323 BT_ERR("%s urb %p failed to resubmit (%d)",
324 hdev->name, urb, -err);
325 usb_unanchor_urb(urb);
326 }
327}
328
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100329static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200330{
331 struct btusb_data *data = hdev->driver_data;
332 struct urb *urb;
333 unsigned char *buf;
334 unsigned int pipe;
Vikram Kandukuri290ba202009-07-02 14:31:59 +0530335 int err, size = HCI_MAX_FRAME_SIZE;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200336
337 BT_DBG("%s", hdev->name);
338
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200339 if (!data->bulk_rx_ep)
340 return -ENODEV;
341
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100342 urb = usb_alloc_urb(0, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200343 if (!urb)
344 return -ENOMEM;
345
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100346 buf = kmalloc(size, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200347 if (!buf) {
348 usb_free_urb(urb);
349 return -ENOMEM;
350 }
351
352 pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
353
354 usb_fill_bulk_urb(urb, data->udev, pipe,
355 buf, size, btusb_bulk_complete, hdev);
356
357 urb->transfer_flags |= URB_FREE_BUFFER;
358
Oliver Neukum7bee5492009-08-24 23:44:59 +0200359 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200360 usb_anchor_urb(urb, &data->bulk_anchor);
361
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100362 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200363 if (err < 0) {
364 BT_ERR("%s urb %p submission failed (%d)",
365 hdev->name, urb, -err);
366 usb_unanchor_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200367 }
368
369 usb_free_urb(urb);
370
371 return err;
372}
373
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200374static void btusb_isoc_complete(struct urb *urb)
375{
376 struct hci_dev *hdev = urb->context;
377 struct btusb_data *data = hdev->driver_data;
378 int i, err;
379
380 BT_DBG("%s urb %p status %d count %d", hdev->name,
381 urb, urb->status, urb->actual_length);
382
383 if (!test_bit(HCI_RUNNING, &hdev->flags))
384 return;
385
386 if (urb->status == 0) {
387 for (i = 0; i < urb->number_of_packets; i++) {
388 unsigned int offset = urb->iso_frame_desc[i].offset;
389 unsigned int length = urb->iso_frame_desc[i].actual_length;
390
391 if (urb->iso_frame_desc[i].status)
392 continue;
393
394 hdev->stat.byte_rx += length;
395
396 if (hci_recv_fragment(hdev, HCI_SCODATA_PKT,
397 urb->transfer_buffer + offset,
398 length) < 0) {
399 BT_ERR("%s corrupted SCO packet", hdev->name);
400 hdev->stat.err_rx++;
401 }
402 }
403 }
404
405 if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags))
406 return;
407
408 usb_anchor_urb(urb, &data->isoc_anchor);
409
410 err = usb_submit_urb(urb, GFP_ATOMIC);
411 if (err < 0) {
412 BT_ERR("%s urb %p failed to resubmit (%d)",
413 hdev->name, urb, -err);
414 usb_unanchor_urb(urb);
415 }
416}
417
418static void inline __fill_isoc_descriptor(struct urb *urb, int len, int mtu)
419{
420 int i, offset = 0;
421
422 BT_DBG("len %d mtu %d", len, mtu);
423
424 for (i = 0; i < BTUSB_MAX_ISOC_FRAMES && len >= mtu;
425 i++, offset += mtu, len -= mtu) {
426 urb->iso_frame_desc[i].offset = offset;
427 urb->iso_frame_desc[i].length = mtu;
428 }
429
430 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
431 urb->iso_frame_desc[i].offset = offset;
432 urb->iso_frame_desc[i].length = len;
433 i++;
434 }
435
436 urb->number_of_packets = i;
437}
438
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100439static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200440{
441 struct btusb_data *data = hdev->driver_data;
442 struct urb *urb;
443 unsigned char *buf;
444 unsigned int pipe;
445 int err, size;
446
447 BT_DBG("%s", hdev->name);
448
449 if (!data->isoc_rx_ep)
450 return -ENODEV;
451
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100452 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200453 if (!urb)
454 return -ENOMEM;
455
456 size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) *
457 BTUSB_MAX_ISOC_FRAMES;
458
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100459 buf = kmalloc(size, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200460 if (!buf) {
461 usb_free_urb(urb);
462 return -ENOMEM;
463 }
464
465 pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress);
466
467 urb->dev = data->udev;
468 urb->pipe = pipe;
469 urb->context = hdev;
470 urb->complete = btusb_isoc_complete;
471 urb->interval = data->isoc_rx_ep->bInterval;
472
473 urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP;
474 urb->transfer_buffer = buf;
475 urb->transfer_buffer_length = size;
476
477 __fill_isoc_descriptor(urb, size,
478 le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize));
479
480 usb_anchor_urb(urb, &data->isoc_anchor);
481
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100482 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200483 if (err < 0) {
484 BT_ERR("%s urb %p submission failed (%d)",
485 hdev->name, urb, -err);
486 usb_unanchor_urb(urb);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200487 }
488
489 usb_free_urb(urb);
490
491 return err;
492}
493
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200494static void btusb_tx_complete(struct urb *urb)
495{
496 struct sk_buff *skb = urb->context;
497 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200498 struct btusb_data *data = hdev->driver_data;
499
500 BT_DBG("%s urb %p status %d count %d", hdev->name,
501 urb, urb->status, urb->actual_length);
502
503 if (!test_bit(HCI_RUNNING, &hdev->flags))
504 goto done;
505
506 if (!urb->status)
507 hdev->stat.byte_tx += urb->transfer_buffer_length;
508 else
509 hdev->stat.err_tx++;
510
511done:
512 spin_lock(&data->txlock);
513 data->tx_in_flight--;
514 spin_unlock(&data->txlock);
515
516 kfree(urb->setup_packet);
517
518 kfree_skb(skb);
519}
520
521static void btusb_isoc_tx_complete(struct urb *urb)
522{
523 struct sk_buff *skb = urb->context;
524 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200525
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 kfree(urb->setup_packet);
539
540 kfree_skb(skb);
541}
542
543static int btusb_open(struct hci_dev *hdev)
544{
545 struct btusb_data *data = hdev->driver_data;
546 int err;
547
548 BT_DBG("%s", hdev->name);
549
Oliver Neukum7bee5492009-08-24 23:44:59 +0200550 err = usb_autopm_get_interface(data->intf);
551 if (err < 0)
552 return err;
553
554 data->intf->needs_remote_wakeup = 1;
555
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200556 if (test_and_set_bit(HCI_RUNNING, &hdev->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +0200557 goto done;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200558
559 if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +0200560 goto done;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200561
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100562 err = btusb_submit_intr_urb(hdev, GFP_KERNEL);
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100563 if (err < 0)
564 goto failed;
565
566 err = btusb_submit_bulk_urb(hdev, GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200567 if (err < 0) {
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100568 usb_kill_anchored_urbs(&data->intr_anchor);
569 goto failed;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200570 }
571
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100572 set_bit(BTUSB_BULK_RUNNING, &data->flags);
573 btusb_submit_bulk_urb(hdev, GFP_KERNEL);
574
Oliver Neukum7bee5492009-08-24 23:44:59 +0200575done:
576 usb_autopm_put_interface(data->intf);
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100577 return 0;
578
579failed:
580 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
581 clear_bit(HCI_RUNNING, &hdev->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200582 usb_autopm_put_interface(data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200583 return err;
584}
585
Oliver Neukum7bee5492009-08-24 23:44:59 +0200586static void btusb_stop_traffic(struct btusb_data *data)
587{
588 usb_kill_anchored_urbs(&data->intr_anchor);
589 usb_kill_anchored_urbs(&data->bulk_anchor);
590 usb_kill_anchored_urbs(&data->isoc_anchor);
591}
592
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200593static int btusb_close(struct hci_dev *hdev)
594{
595 struct btusb_data *data = hdev->driver_data;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200596 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200597
598 BT_DBG("%s", hdev->name);
599
600 if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
601 return 0;
602
Marcel Holtmanne8c3c3d2008-09-23 00:16:36 +0200603 cancel_work_sync(&data->work);
Linus Torvalds404291a2009-11-11 13:32:29 -0800604 cancel_work_sync(&data->waker);
Marcel Holtmanne8c3c3d2008-09-23 00:16:36 +0200605
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200606 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200607 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200608 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200609
610 btusb_stop_traffic(data);
611 err = usb_autopm_get_interface(data->intf);
612 if (err < 0)
Oliver Neukum7b8e2c12009-11-13 14:26:23 +0100613 goto failed;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200614
615 data->intf->needs_remote_wakeup = 0;
616 usb_autopm_put_interface(data->intf);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200617
Oliver Neukum7b8e2c12009-11-13 14:26:23 +0100618failed:
619 usb_scuttle_anchored_urbs(&data->deferred);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200620 return 0;
621}
622
623static int btusb_flush(struct hci_dev *hdev)
624{
625 struct btusb_data *data = hdev->driver_data;
626
627 BT_DBG("%s", hdev->name);
628
629 usb_kill_anchored_urbs(&data->tx_anchor);
630
631 return 0;
632}
633
634static int btusb_send_frame(struct sk_buff *skb)
635{
636 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
637 struct btusb_data *data = hdev->driver_data;
638 struct usb_ctrlrequest *dr;
639 struct urb *urb;
640 unsigned int pipe;
641 int err;
642
643 BT_DBG("%s", hdev->name);
644
645 if (!test_bit(HCI_RUNNING, &hdev->flags))
646 return -EBUSY;
647
648 switch (bt_cb(skb)->pkt_type) {
649 case HCI_COMMAND_PKT:
650 urb = usb_alloc_urb(0, GFP_ATOMIC);
651 if (!urb)
652 return -ENOMEM;
653
654 dr = kmalloc(sizeof(*dr), GFP_ATOMIC);
655 if (!dr) {
656 usb_free_urb(urb);
657 return -ENOMEM;
658 }
659
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100660 dr->bRequestType = data->cmdreq_type;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200661 dr->bRequest = 0;
662 dr->wIndex = 0;
663 dr->wValue = 0;
664 dr->wLength = __cpu_to_le16(skb->len);
665
666 pipe = usb_sndctrlpipe(data->udev, 0x00);
667
668 usb_fill_control_urb(urb, data->udev, pipe, (void *) dr,
669 skb->data, skb->len, btusb_tx_complete, skb);
670
671 hdev->stat.cmd_tx++;
672 break;
673
674 case HCI_ACLDATA_PKT:
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200675 if (!data->bulk_tx_ep || hdev->conn_hash.acl_num < 1)
676 return -ENODEV;
677
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200678 urb = usb_alloc_urb(0, GFP_ATOMIC);
679 if (!urb)
680 return -ENOMEM;
681
682 pipe = usb_sndbulkpipe(data->udev,
683 data->bulk_tx_ep->bEndpointAddress);
684
685 usb_fill_bulk_urb(urb, data->udev, pipe,
686 skb->data, skb->len, btusb_tx_complete, skb);
687
688 hdev->stat.acl_tx++;
689 break;
690
691 case HCI_SCODATA_PKT:
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200692 if (!data->isoc_tx_ep || hdev->conn_hash.sco_num < 1)
693 return -ENODEV;
694
695 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_ATOMIC);
696 if (!urb)
697 return -ENOMEM;
698
699 pipe = usb_sndisocpipe(data->udev,
700 data->isoc_tx_ep->bEndpointAddress);
701
702 urb->dev = data->udev;
703 urb->pipe = pipe;
704 urb->context = skb;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200705 urb->complete = btusb_isoc_tx_complete;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200706 urb->interval = data->isoc_tx_ep->bInterval;
707
708 urb->transfer_flags = URB_ISO_ASAP;
709 urb->transfer_buffer = skb->data;
710 urb->transfer_buffer_length = skb->len;
711
712 __fill_isoc_descriptor(urb, skb->len,
713 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
714
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200715 hdev->stat.sco_tx++;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200716 goto skip_waking;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200717
718 default:
719 return -EILSEQ;
720 }
721
Oliver Neukum7bee5492009-08-24 23:44:59 +0200722 err = inc_tx(data);
723 if (err) {
724 usb_anchor_urb(urb, &data->deferred);
725 schedule_work(&data->waker);
726 err = 0;
727 goto done;
728 }
729
730skip_waking:
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200731 usb_anchor_urb(urb, &data->tx_anchor);
732
733 err = usb_submit_urb(urb, GFP_ATOMIC);
734 if (err < 0) {
735 BT_ERR("%s urb %p submission failed", hdev->name, urb);
736 kfree(urb->setup_packet);
737 usb_unanchor_urb(urb);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200738 } else {
739 usb_mark_last_busy(data->udev);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200740 }
741
742 usb_free_urb(urb);
743
Oliver Neukum7bee5492009-08-24 23:44:59 +0200744done:
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200745 return err;
746}
747
748static void btusb_destruct(struct hci_dev *hdev)
749{
750 struct btusb_data *data = hdev->driver_data;
751
752 BT_DBG("%s", hdev->name);
753
754 kfree(data);
755}
756
757static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
758{
759 struct btusb_data *data = hdev->driver_data;
760
761 BT_DBG("%s evt %d", hdev->name, evt);
762
Marcel Holtmann43c2e572009-02-04 17:41:38 +0100763 if (hdev->conn_hash.sco_num != data->sco_num) {
764 data->sco_num = hdev->conn_hash.sco_num;
765 schedule_work(&data->work);
Marcel Holtmanna780efa2008-11-30 12:17:12 +0100766 }
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200767}
768
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200769static int inline __set_isoc_interface(struct hci_dev *hdev, int altsetting)
770{
771 struct btusb_data *data = hdev->driver_data;
772 struct usb_interface *intf = data->isoc;
773 struct usb_endpoint_descriptor *ep_desc;
774 int i, err;
775
776 if (!data->isoc)
777 return -ENODEV;
778
779 err = usb_set_interface(data->udev, 1, altsetting);
780 if (err < 0) {
781 BT_ERR("%s setting interface failed (%d)", hdev->name, -err);
782 return err;
783 }
784
785 data->isoc_altsetting = altsetting;
786
787 data->isoc_tx_ep = NULL;
788 data->isoc_rx_ep = NULL;
789
790 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
791 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
792
793 if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) {
794 data->isoc_tx_ep = ep_desc;
795 continue;
796 }
797
798 if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) {
799 data->isoc_rx_ep = ep_desc;
800 continue;
801 }
802 }
803
804 if (!data->isoc_tx_ep || !data->isoc_rx_ep) {
805 BT_ERR("%s invalid SCO descriptors", hdev->name);
806 return -ENODEV;
807 }
808
809 return 0;
810}
811
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200812static void btusb_work(struct work_struct *work)
813{
814 struct btusb_data *data = container_of(work, struct btusb_data, work);
815 struct hci_dev *hdev = data->hdev;
Oliver Neukum7bee5492009-08-24 23:44:59 +0200816 int err;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200817
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200818 if (hdev->conn_hash.sco_num > 0) {
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -0300819 if (!test_bit(BTUSB_DID_ISO_RESUME, &data->flags)) {
Oliver Neukum7bee5492009-08-24 23:44:59 +0200820 err = usb_autopm_get_interface(data->isoc);
821 if (err < 0) {
822 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
823 usb_kill_anchored_urbs(&data->isoc_anchor);
824 return;
825 }
826
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -0300827 set_bit(BTUSB_DID_ISO_RESUME, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200828 }
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200829 if (data->isoc_altsetting != 2) {
830 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
831 usb_kill_anchored_urbs(&data->isoc_anchor);
832
833 if (__set_isoc_interface(hdev, 2) < 0)
834 return;
835 }
836
837 if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100838 if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200839 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
840 else
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100841 btusb_submit_isoc_urb(hdev, GFP_KERNEL);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200842 }
843 } else {
844 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
845 usb_kill_anchored_urbs(&data->isoc_anchor);
846
847 __set_isoc_interface(hdev, 0);
Gustavo F. Padovan08b8b6c2010-07-16 17:20:33 -0300848 if (test_and_clear_bit(BTUSB_DID_ISO_RESUME, &data->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +0200849 usb_autopm_put_interface(data->isoc);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200850 }
851}
852
Oliver Neukum7bee5492009-08-24 23:44:59 +0200853static void btusb_waker(struct work_struct *work)
854{
855 struct btusb_data *data = container_of(work, struct btusb_data, waker);
856 int err;
857
858 err = usb_autopm_get_interface(data->intf);
859 if (err < 0)
860 return;
861
862 usb_autopm_put_interface(data->intf);
863}
864
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200865static int btusb_probe(struct usb_interface *intf,
866 const struct usb_device_id *id)
867{
868 struct usb_endpoint_descriptor *ep_desc;
869 struct btusb_data *data;
870 struct hci_dev *hdev;
871 int i, err;
872
873 BT_DBG("intf %p id %p", intf, id);
874
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200875 /* interface numbers are hardcoded in the spec */
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200876 if (intf->cur_altsetting->desc.bInterfaceNumber != 0)
877 return -ENODEV;
878
879 if (!id->driver_info) {
880 const struct usb_device_id *match;
881 match = usb_match_id(intf, blacklist_table);
882 if (match)
883 id = match;
884 }
885
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200886 if (id->driver_info == BTUSB_IGNORE)
887 return -ENODEV;
888
889 if (ignore_dga && id->driver_info & BTUSB_DIGIANSWER)
890 return -ENODEV;
891
892 if (ignore_csr && id->driver_info & BTUSB_CSR)
893 return -ENODEV;
894
895 if (ignore_sniffer && id->driver_info & BTUSB_SNIFFER)
896 return -ENODEV;
897
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200898 data = kzalloc(sizeof(*data), GFP_KERNEL);
899 if (!data)
900 return -ENOMEM;
901
902 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
903 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
904
905 if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
906 data->intr_ep = ep_desc;
907 continue;
908 }
909
910 if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
911 data->bulk_tx_ep = ep_desc;
912 continue;
913 }
914
915 if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
916 data->bulk_rx_ep = ep_desc;
917 continue;
918 }
919 }
920
921 if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep) {
922 kfree(data);
923 return -ENODEV;
924 }
925
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100926 data->cmdreq_type = USB_TYPE_CLASS;
927
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200928 data->udev = interface_to_usbdev(intf);
Marcel Holtmann5fbcd262008-09-23 00:16:36 +0200929 data->intf = intf;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200930
931 spin_lock_init(&data->lock);
932
933 INIT_WORK(&data->work, btusb_work);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200934 INIT_WORK(&data->waker, btusb_waker);
935 spin_lock_init(&data->txlock);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200936
937 init_usb_anchor(&data->tx_anchor);
938 init_usb_anchor(&data->intr_anchor);
939 init_usb_anchor(&data->bulk_anchor);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200940 init_usb_anchor(&data->isoc_anchor);
Oliver Neukum7bee5492009-08-24 23:44:59 +0200941 init_usb_anchor(&data->deferred);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200942
943 hdev = hci_alloc_dev();
944 if (!hdev) {
945 kfree(data);
946 return -ENOMEM;
947 }
948
Marcel Holtmannc13854c2010-02-08 15:27:07 +0100949 hdev->bus = HCI_USB;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200950 hdev->driver_data = data;
951
952 data->hdev = hdev;
953
954 SET_HCIDEV_DEV(hdev, &intf->dev);
955
956 hdev->open = btusb_open;
957 hdev->close = btusb_close;
958 hdev->flush = btusb_flush;
959 hdev->send = btusb_send_frame;
960 hdev->destruct = btusb_destruct;
961 hdev->notify = btusb_notify;
962
963 hdev->owner = THIS_MODULE;
964
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100965 /* Interface numbers are hardcoded in the specification */
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200966 data->isoc = usb_ifnum_to_if(data->udev, 1);
967
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100968 if (!reset)
969 set_bit(HCI_QUIRK_NO_RESET, &hdev->quirks);
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200970
971 if (force_scofix || id->driver_info & BTUSB_WRONG_SCO_MTU) {
972 if (!disable_scofix)
973 set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
974 }
975
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200976 if (id->driver_info & BTUSB_BROKEN_ISOC)
977 data->isoc = NULL;
978
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100979 if (id->driver_info & BTUSB_DIGIANSWER) {
980 data->cmdreq_type = USB_TYPE_VENDOR;
981 set_bit(HCI_QUIRK_NO_RESET, &hdev->quirks);
982 }
983
984 if (id->driver_info & BTUSB_CSR) {
985 struct usb_device *udev = data->udev;
986
987 /* Old firmware would otherwise execute USB reset */
988 if (le16_to_cpu(udev->descriptor.bcdDevice) < 0x117)
989 set_bit(HCI_QUIRK_NO_RESET, &hdev->quirks);
990 }
991
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200992 if (id->driver_info & BTUSB_SNIFFER) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200993 struct usb_device *udev = data->udev;
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200994
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100995 /* New sniffer firmware has crippled HCI interface */
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200996 if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
997 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200998
999 data->isoc = NULL;
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02001000 }
1001
1002 if (id->driver_info & BTUSB_BCM92035) {
1003 unsigned char cmd[] = { 0x3b, 0xfc, 0x01, 0x00 };
1004 struct sk_buff *skb;
1005
1006 skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL);
1007 if (skb) {
1008 memcpy(skb_put(skb, sizeof(cmd)), cmd, sizeof(cmd));
1009 skb_queue_tail(&hdev->driver_init, skb);
1010 }
1011 }
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001012
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001013 if (data->isoc) {
1014 err = usb_driver_claim_interface(&btusb_driver,
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02001015 data->isoc, data);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001016 if (err < 0) {
1017 hci_free_dev(hdev);
1018 kfree(data);
1019 return err;
1020 }
1021 }
1022
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001023 err = hci_register_dev(hdev);
1024 if (err < 0) {
1025 hci_free_dev(hdev);
1026 kfree(data);
1027 return err;
1028 }
1029
1030 usb_set_intfdata(intf, data);
1031
1032 return 0;
1033}
1034
1035static void btusb_disconnect(struct usb_interface *intf)
1036{
1037 struct btusb_data *data = usb_get_intfdata(intf);
1038 struct hci_dev *hdev;
1039
1040 BT_DBG("intf %p", intf);
1041
1042 if (!data)
1043 return;
1044
1045 hdev = data->hdev;
1046
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02001047 __hci_dev_hold(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +02001048
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02001049 usb_set_intfdata(data->intf, NULL);
1050
1051 if (data->isoc)
1052 usb_set_intfdata(data->isoc, NULL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001053
1054 hci_unregister_dev(hdev);
1055
Marcel Holtmann5fbcd262008-09-23 00:16:36 +02001056 if (intf == data->isoc)
1057 usb_driver_release_interface(&btusb_driver, data->intf);
1058 else if (data->isoc)
1059 usb_driver_release_interface(&btusb_driver, data->isoc);
1060
1061 __hci_dev_put(hdev);
1062
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001063 hci_free_dev(hdev);
1064}
1065
Oliver Neukum7bee5492009-08-24 23:44:59 +02001066#ifdef CONFIG_PM
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001067static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
1068{
1069 struct btusb_data *data = usb_get_intfdata(intf);
1070
1071 BT_DBG("intf %p", intf);
1072
1073 if (data->suspend_count++)
1074 return 0;
1075
Oliver Neukum7bee5492009-08-24 23:44:59 +02001076 spin_lock_irq(&data->txlock);
Alan Sternfb34d532009-11-13 11:53:59 -05001077 if (!((message.event & PM_EVENT_AUTO) && data->tx_in_flight)) {
Oliver Neukum7bee5492009-08-24 23:44:59 +02001078 set_bit(BTUSB_SUSPENDING, &data->flags);
1079 spin_unlock_irq(&data->txlock);
1080 } else {
1081 spin_unlock_irq(&data->txlock);
1082 data->suspend_count--;
1083 return -EBUSY;
1084 }
1085
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001086 cancel_work_sync(&data->work);
1087
Oliver Neukum7bee5492009-08-24 23:44:59 +02001088 btusb_stop_traffic(data);
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001089 usb_kill_anchored_urbs(&data->tx_anchor);
1090
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001091 return 0;
1092}
1093
Oliver Neukum7bee5492009-08-24 23:44:59 +02001094static void play_deferred(struct btusb_data *data)
1095{
1096 struct urb *urb;
1097 int err;
1098
1099 while ((urb = usb_get_from_anchor(&data->deferred))) {
1100 err = usb_submit_urb(urb, GFP_ATOMIC);
1101 if (err < 0)
1102 break;
1103
1104 data->tx_in_flight++;
1105 }
1106 usb_scuttle_anchored_urbs(&data->deferred);
1107}
1108
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001109static int btusb_resume(struct usb_interface *intf)
1110{
1111 struct btusb_data *data = usb_get_intfdata(intf);
1112 struct hci_dev *hdev = data->hdev;
Oliver Neukum7bee5492009-08-24 23:44:59 +02001113 int err = 0;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001114
1115 BT_DBG("intf %p", intf);
1116
1117 if (--data->suspend_count)
1118 return 0;
1119
1120 if (!test_bit(HCI_RUNNING, &hdev->flags))
Oliver Neukum7bee5492009-08-24 23:44:59 +02001121 goto done;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001122
1123 if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
1124 err = btusb_submit_intr_urb(hdev, GFP_NOIO);
1125 if (err < 0) {
1126 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001127 goto failed;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001128 }
1129 }
1130
1131 if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) {
Marcel Holtmann43c2e572009-02-04 17:41:38 +01001132 err = btusb_submit_bulk_urb(hdev, GFP_NOIO);
1133 if (err < 0) {
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001134 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
Oliver Neukum7bee5492009-08-24 23:44:59 +02001135 goto failed;
1136 }
1137
1138 btusb_submit_bulk_urb(hdev, GFP_NOIO);
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001139 }
1140
1141 if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
1142 if (btusb_submit_isoc_urb(hdev, GFP_NOIO) < 0)
1143 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1144 else
1145 btusb_submit_isoc_urb(hdev, GFP_NOIO);
1146 }
1147
Oliver Neukum7bee5492009-08-24 23:44:59 +02001148 spin_lock_irq(&data->txlock);
1149 play_deferred(data);
1150 clear_bit(BTUSB_SUSPENDING, &data->flags);
1151 spin_unlock_irq(&data->txlock);
1152 schedule_work(&data->work);
1153
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001154 return 0;
Oliver Neukum7bee5492009-08-24 23:44:59 +02001155
1156failed:
1157 usb_scuttle_anchored_urbs(&data->deferred);
1158done:
1159 spin_lock_irq(&data->txlock);
1160 clear_bit(BTUSB_SUSPENDING, &data->flags);
1161 spin_unlock_irq(&data->txlock);
1162
1163 return err;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001164}
Oliver Neukum7bee5492009-08-24 23:44:59 +02001165#endif
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001166
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001167static struct usb_driver btusb_driver = {
1168 .name = "btusb",
1169 .probe = btusb_probe,
1170 .disconnect = btusb_disconnect,
Oliver Neukum7bee5492009-08-24 23:44:59 +02001171#ifdef CONFIG_PM
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001172 .suspend = btusb_suspend,
1173 .resume = btusb_resume,
Oliver Neukum7bee5492009-08-24 23:44:59 +02001174#endif
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001175 .id_table = btusb_table,
Oliver Neukum7bee5492009-08-24 23:44:59 +02001176 .supports_autosuspend = 1,
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001177};
1178
1179static int __init btusb_init(void)
1180{
1181 BT_INFO("Generic Bluetooth USB driver ver %s", VERSION);
1182
1183 return usb_register(&btusb_driver);
1184}
1185
1186static void __exit btusb_exit(void)
1187{
1188 usb_deregister(&btusb_driver);
1189}
1190
1191module_init(btusb_init);
1192module_exit(btusb_exit);
1193
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02001194module_param(ignore_dga, bool, 0644);
1195MODULE_PARM_DESC(ignore_dga, "Ignore devices with id 08fd:0001");
1196
1197module_param(ignore_csr, bool, 0644);
1198MODULE_PARM_DESC(ignore_csr, "Ignore devices with id 0a12:0001");
1199
1200module_param(ignore_sniffer, bool, 0644);
1201MODULE_PARM_DESC(ignore_sniffer, "Ignore devices with id 0a12:0002");
1202
1203module_param(disable_scofix, bool, 0644);
1204MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
1205
1206module_param(force_scofix, bool, 0644);
1207MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
1208
1209module_param(reset, bool, 0644);
1210MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
1211
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001212MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
1213MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
1214MODULE_VERSION(VERSION);
1215MODULE_LICENSE("GPL");