blob: 7f7526c186d07cb0f884b8c618fbc488520e64bb [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
38//#define CONFIG_BT_HCIBTUSB_DEBUG
39#ifndef CONFIG_BT_HCIBTUSB_DEBUG
40#undef BT_DBG
41#define BT_DBG(D...)
42#endif
43
Marcel Holtmann7a9d4022008-11-30 12:17:26 +010044#define VERSION "0.4"
Marcel Holtmanncfeb4142008-08-07 22:26:56 +020045
46static int ignore_dga;
47static int ignore_csr;
48static int ignore_sniffer;
49static int disable_scofix;
50static int force_scofix;
Marcel Holtmann7a9d4022008-11-30 12:17:26 +010051
52static int reset = 1;
Marcel Holtmanncfeb4142008-08-07 22:26:56 +020053
54static struct usb_driver btusb_driver;
55
56#define BTUSB_IGNORE 0x01
Marcel Holtmann7a9d4022008-11-30 12:17:26 +010057#define BTUSB_DIGIANSWER 0x02
58#define BTUSB_CSR 0x04
59#define BTUSB_SNIFFER 0x08
60#define BTUSB_BCM92035 0x10
61#define BTUSB_BROKEN_ISOC 0x20
62#define BTUSB_WRONG_SCO_MTU 0x40
Marcel Holtmann5e23b922007-10-20 14:12:34 +020063
64static struct usb_device_id btusb_table[] = {
65 /* Generic Bluetooth USB device */
66 { USB_DEVICE_INFO(0xe0, 0x01, 0x01) },
67
Marcel Holtmanncfeb4142008-08-07 22:26:56 +020068 /* AVM BlueFRITZ! USB v2.0 */
69 { USB_DEVICE(0x057c, 0x3800) },
70
71 /* Bluetooth Ultraport Module from IBM */
72 { USB_DEVICE(0x04bf, 0x030a) },
73
74 /* ALPS Modules with non-standard id */
75 { USB_DEVICE(0x044e, 0x3001) },
76 { USB_DEVICE(0x044e, 0x3002) },
77
78 /* Ericsson with non-standard id */
79 { USB_DEVICE(0x0bdb, 0x1002) },
80
81 /* Canyon CN-BTU1 with HID interfaces */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +010082 { USB_DEVICE(0x0c10, 0x0000) },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +020083
Marcel Holtmann5e23b922007-10-20 14:12:34 +020084 { } /* Terminating entry */
85};
86
87MODULE_DEVICE_TABLE(usb, btusb_table);
88
89static struct usb_device_id blacklist_table[] = {
Marcel Holtmanncfeb4142008-08-07 22:26:56 +020090 /* CSR BlueCore devices */
91 { USB_DEVICE(0x0a12, 0x0001), .driver_info = BTUSB_CSR },
92
93 /* Broadcom BCM2033 without firmware */
94 { USB_DEVICE(0x0a5c, 0x2033), .driver_info = BTUSB_IGNORE },
95
96 /* Broadcom BCM2035 */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +010097 { USB_DEVICE(0x0a5c, 0x2035), .driver_info = BTUSB_WRONG_SCO_MTU },
98 { USB_DEVICE(0x0a5c, 0x200a), .driver_info = BTUSB_WRONG_SCO_MTU },
99 { USB_DEVICE(0x0a5c, 0x2009), .driver_info = BTUSB_BCM92035 },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200100
101 /* Broadcom BCM2045 */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100102 { USB_DEVICE(0x0a5c, 0x2039), .driver_info = BTUSB_WRONG_SCO_MTU },
103 { USB_DEVICE(0x0a5c, 0x2101), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmannbdbef3d2008-09-23 00:16:35 +0200104
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200105 /* IBM/Lenovo ThinkPad with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100106 { USB_DEVICE(0x0a5c, 0x201e), .driver_info = BTUSB_WRONG_SCO_MTU },
107 { USB_DEVICE(0x0a5c, 0x2110), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200108
109 /* HP laptop with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100110 { USB_DEVICE(0x03f0, 0x171d), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200111
112 /* Dell laptop with Broadcom chip */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100113 { USB_DEVICE(0x413c, 0x8126), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200114
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100115 /* Dell Wireless 370 and 410 devices */
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100116 { USB_DEVICE(0x413c, 0x8152), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100117 { USB_DEVICE(0x413c, 0x8156), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200118
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100119 /* Belkin F8T012 and F8T013 devices */
120 { USB_DEVICE(0x050d, 0x0012), .driver_info = BTUSB_WRONG_SCO_MTU },
121 { USB_DEVICE(0x050d, 0x0013), .driver_info = BTUSB_WRONG_SCO_MTU },
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200122
Marcel Holtmann5ddd4a62008-11-30 12:17:27 +0100123 /* Asus WL-BTD202 device */
124 { USB_DEVICE(0x0b05, 0x1715), .driver_info = BTUSB_WRONG_SCO_MTU },
125
126 /* Kensington Bluetooth USB adapter */
127 { USB_DEVICE(0x047d, 0x105e), .driver_info = BTUSB_WRONG_SCO_MTU },
128
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200129 /* RTX Telecom based adapters with buggy SCO support */
130 { USB_DEVICE(0x0400, 0x0807), .driver_info = BTUSB_BROKEN_ISOC },
131 { USB_DEVICE(0x0400, 0x080a), .driver_info = BTUSB_BROKEN_ISOC },
132
133 /* CONWISE Technology based adapters with buggy SCO support */
134 { USB_DEVICE(0x0e5e, 0x6622), .driver_info = BTUSB_BROKEN_ISOC },
135
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200136 /* Digianswer devices */
137 { USB_DEVICE(0x08fd, 0x0001), .driver_info = BTUSB_DIGIANSWER },
138 { USB_DEVICE(0x08fd, 0x0002), .driver_info = BTUSB_IGNORE },
139
140 /* CSR BlueCore Bluetooth Sniffer */
141 { USB_DEVICE(0x0a12, 0x0002), .driver_info = BTUSB_SNIFFER },
142
143 /* Frontline ComProbe Bluetooth Sniffer */
144 { USB_DEVICE(0x16d3, 0x0002), .driver_info = BTUSB_SNIFFER },
145
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200146 { } /* Terminating entry */
147};
148
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200149#define BTUSB_MAX_ISOC_FRAMES 10
150
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200151#define BTUSB_INTR_RUNNING 0
152#define BTUSB_BULK_RUNNING 1
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200153#define BTUSB_ISOC_RUNNING 2
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200154
155struct btusb_data {
156 struct hci_dev *hdev;
157 struct usb_device *udev;
Marcel Holtmann5fbcd262008-09-23 00:16:36 +0200158 struct usb_interface *intf;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200159 struct usb_interface *isoc;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200160
161 spinlock_t lock;
162
163 unsigned long flags;
164
165 struct work_struct work;
166
167 struct usb_anchor tx_anchor;
168 struct usb_anchor intr_anchor;
169 struct usb_anchor bulk_anchor;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200170 struct usb_anchor isoc_anchor;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200171
172 struct usb_endpoint_descriptor *intr_ep;
173 struct usb_endpoint_descriptor *bulk_tx_ep;
174 struct usb_endpoint_descriptor *bulk_rx_ep;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200175 struct usb_endpoint_descriptor *isoc_tx_ep;
176 struct usb_endpoint_descriptor *isoc_rx_ep;
177
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100178 __u8 cmdreq_type;
179
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200180 int isoc_altsetting;
Marcel Holtmann6a88adf2008-11-30 12:17:14 +0100181 int suspend_count;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200182};
183
184static void btusb_intr_complete(struct urb *urb)
185{
186 struct hci_dev *hdev = urb->context;
187 struct btusb_data *data = hdev->driver_data;
188 int err;
189
190 BT_DBG("%s urb %p status %d count %d", hdev->name,
191 urb, urb->status, urb->actual_length);
192
193 if (!test_bit(HCI_RUNNING, &hdev->flags))
194 return;
195
196 if (urb->status == 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200197 hdev->stat.byte_rx += urb->actual_length;
198
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200199 if (hci_recv_fragment(hdev, HCI_EVENT_PKT,
200 urb->transfer_buffer,
201 urb->actual_length) < 0) {
202 BT_ERR("%s corrupted event packet", hdev->name);
203 hdev->stat.err_rx++;
204 }
205 }
206
207 if (!test_bit(BTUSB_INTR_RUNNING, &data->flags))
208 return;
209
210 usb_anchor_urb(urb, &data->intr_anchor);
211
212 err = usb_submit_urb(urb, GFP_ATOMIC);
213 if (err < 0) {
214 BT_ERR("%s urb %p failed to resubmit (%d)",
215 hdev->name, urb, -err);
216 usb_unanchor_urb(urb);
217 }
218}
219
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100220static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200221{
222 struct btusb_data *data = hdev->driver_data;
223 struct urb *urb;
224 unsigned char *buf;
225 unsigned int pipe;
226 int err, size;
227
228 BT_DBG("%s", hdev->name);
229
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200230 if (!data->intr_ep)
231 return -ENODEV;
232
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100233 urb = usb_alloc_urb(0, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200234 if (!urb)
235 return -ENOMEM;
236
237 size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
238
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100239 buf = kmalloc(size, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200240 if (!buf) {
241 usb_free_urb(urb);
242 return -ENOMEM;
243 }
244
245 pipe = usb_rcvintpipe(data->udev, data->intr_ep->bEndpointAddress);
246
247 usb_fill_int_urb(urb, data->udev, pipe, buf, size,
248 btusb_intr_complete, hdev,
249 data->intr_ep->bInterval);
250
251 urb->transfer_flags |= URB_FREE_BUFFER;
252
253 usb_anchor_urb(urb, &data->intr_anchor);
254
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100255 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200256 if (err < 0) {
257 BT_ERR("%s urb %p submission failed (%d)",
258 hdev->name, urb, -err);
259 usb_unanchor_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200260 }
261
262 usb_free_urb(urb);
263
264 return err;
265}
266
267static void btusb_bulk_complete(struct urb *urb)
268{
269 struct hci_dev *hdev = urb->context;
270 struct btusb_data *data = hdev->driver_data;
271 int err;
272
273 BT_DBG("%s urb %p status %d count %d", hdev->name,
274 urb, urb->status, urb->actual_length);
275
276 if (!test_bit(HCI_RUNNING, &hdev->flags))
277 return;
278
279 if (urb->status == 0) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200280 hdev->stat.byte_rx += urb->actual_length;
281
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200282 if (hci_recv_fragment(hdev, HCI_ACLDATA_PKT,
283 urb->transfer_buffer,
284 urb->actual_length) < 0) {
285 BT_ERR("%s corrupted ACL packet", hdev->name);
286 hdev->stat.err_rx++;
287 }
288 }
289
290 if (!test_bit(BTUSB_BULK_RUNNING, &data->flags))
291 return;
292
293 usb_anchor_urb(urb, &data->bulk_anchor);
294
295 err = usb_submit_urb(urb, GFP_ATOMIC);
296 if (err < 0) {
297 BT_ERR("%s urb %p failed to resubmit (%d)",
298 hdev->name, urb, -err);
299 usb_unanchor_urb(urb);
300 }
301}
302
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100303static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200304{
305 struct btusb_data *data = hdev->driver_data;
306 struct urb *urb;
307 unsigned char *buf;
308 unsigned int pipe;
309 int err, size;
310
311 BT_DBG("%s", hdev->name);
312
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200313 if (!data->bulk_rx_ep)
314 return -ENODEV;
315
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100316 urb = usb_alloc_urb(0, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200317 if (!urb)
318 return -ENOMEM;
319
320 size = le16_to_cpu(data->bulk_rx_ep->wMaxPacketSize);
321
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100322 buf = kmalloc(size, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200323 if (!buf) {
324 usb_free_urb(urb);
325 return -ENOMEM;
326 }
327
328 pipe = usb_rcvbulkpipe(data->udev, data->bulk_rx_ep->bEndpointAddress);
329
330 usb_fill_bulk_urb(urb, data->udev, pipe,
331 buf, size, btusb_bulk_complete, hdev);
332
333 urb->transfer_flags |= URB_FREE_BUFFER;
334
335 usb_anchor_urb(urb, &data->bulk_anchor);
336
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100337 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200338 if (err < 0) {
339 BT_ERR("%s urb %p submission failed (%d)",
340 hdev->name, urb, -err);
341 usb_unanchor_urb(urb);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200342 }
343
344 usb_free_urb(urb);
345
346 return err;
347}
348
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200349static void btusb_isoc_complete(struct urb *urb)
350{
351 struct hci_dev *hdev = urb->context;
352 struct btusb_data *data = hdev->driver_data;
353 int i, err;
354
355 BT_DBG("%s urb %p status %d count %d", hdev->name,
356 urb, urb->status, urb->actual_length);
357
358 if (!test_bit(HCI_RUNNING, &hdev->flags))
359 return;
360
361 if (urb->status == 0) {
362 for (i = 0; i < urb->number_of_packets; i++) {
363 unsigned int offset = urb->iso_frame_desc[i].offset;
364 unsigned int length = urb->iso_frame_desc[i].actual_length;
365
366 if (urb->iso_frame_desc[i].status)
367 continue;
368
369 hdev->stat.byte_rx += length;
370
371 if (hci_recv_fragment(hdev, HCI_SCODATA_PKT,
372 urb->transfer_buffer + offset,
373 length) < 0) {
374 BT_ERR("%s corrupted SCO packet", hdev->name);
375 hdev->stat.err_rx++;
376 }
377 }
378 }
379
380 if (!test_bit(BTUSB_ISOC_RUNNING, &data->flags))
381 return;
382
383 usb_anchor_urb(urb, &data->isoc_anchor);
384
385 err = usb_submit_urb(urb, GFP_ATOMIC);
386 if (err < 0) {
387 BT_ERR("%s urb %p failed to resubmit (%d)",
388 hdev->name, urb, -err);
389 usb_unanchor_urb(urb);
390 }
391}
392
393static void inline __fill_isoc_descriptor(struct urb *urb, int len, int mtu)
394{
395 int i, offset = 0;
396
397 BT_DBG("len %d mtu %d", len, mtu);
398
399 for (i = 0; i < BTUSB_MAX_ISOC_FRAMES && len >= mtu;
400 i++, offset += mtu, len -= mtu) {
401 urb->iso_frame_desc[i].offset = offset;
402 urb->iso_frame_desc[i].length = mtu;
403 }
404
405 if (len && i < BTUSB_MAX_ISOC_FRAMES) {
406 urb->iso_frame_desc[i].offset = offset;
407 urb->iso_frame_desc[i].length = len;
408 i++;
409 }
410
411 urb->number_of_packets = i;
412}
413
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100414static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200415{
416 struct btusb_data *data = hdev->driver_data;
417 struct urb *urb;
418 unsigned char *buf;
419 unsigned int pipe;
420 int err, size;
421
422 BT_DBG("%s", hdev->name);
423
424 if (!data->isoc_rx_ep)
425 return -ENODEV;
426
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100427 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200428 if (!urb)
429 return -ENOMEM;
430
431 size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) *
432 BTUSB_MAX_ISOC_FRAMES;
433
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100434 buf = kmalloc(size, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200435 if (!buf) {
436 usb_free_urb(urb);
437 return -ENOMEM;
438 }
439
440 pipe = usb_rcvisocpipe(data->udev, data->isoc_rx_ep->bEndpointAddress);
441
442 urb->dev = data->udev;
443 urb->pipe = pipe;
444 urb->context = hdev;
445 urb->complete = btusb_isoc_complete;
446 urb->interval = data->isoc_rx_ep->bInterval;
447
448 urb->transfer_flags = URB_FREE_BUFFER | URB_ISO_ASAP;
449 urb->transfer_buffer = buf;
450 urb->transfer_buffer_length = size;
451
452 __fill_isoc_descriptor(urb, size,
453 le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize));
454
455 usb_anchor_urb(urb, &data->isoc_anchor);
456
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100457 err = usb_submit_urb(urb, mem_flags);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200458 if (err < 0) {
459 BT_ERR("%s urb %p submission failed (%d)",
460 hdev->name, urb, -err);
461 usb_unanchor_urb(urb);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200462 }
463
464 usb_free_urb(urb);
465
466 return err;
467}
468
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200469static void btusb_tx_complete(struct urb *urb)
470{
471 struct sk_buff *skb = urb->context;
472 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
473
474 BT_DBG("%s urb %p status %d count %d", hdev->name,
475 urb, urb->status, urb->actual_length);
476
477 if (!test_bit(HCI_RUNNING, &hdev->flags))
478 goto done;
479
480 if (!urb->status)
481 hdev->stat.byte_tx += urb->transfer_buffer_length;
482 else
483 hdev->stat.err_tx++;
484
485done:
486 kfree(urb->setup_packet);
487
488 kfree_skb(skb);
489}
490
491static int btusb_open(struct hci_dev *hdev)
492{
493 struct btusb_data *data = hdev->driver_data;
494 int err;
495
496 BT_DBG("%s", hdev->name);
497
498 if (test_and_set_bit(HCI_RUNNING, &hdev->flags))
499 return 0;
500
501 if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
502 return 0;
503
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100504 err = btusb_submit_intr_urb(hdev, GFP_KERNEL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200505 if (err < 0) {
Marcel Holtmanne8c3c3d2008-09-23 00:16:36 +0200506 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200507 clear_bit(HCI_RUNNING, &hdev->flags);
508 }
509
510 return err;
511}
512
513static int btusb_close(struct hci_dev *hdev)
514{
515 struct btusb_data *data = hdev->driver_data;
516
517 BT_DBG("%s", hdev->name);
518
519 if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
520 return 0;
521
Marcel Holtmanne8c3c3d2008-09-23 00:16:36 +0200522 cancel_work_sync(&data->work);
523
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200524 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
Marcel Holtmanne8c3c3d2008-09-23 00:16:36 +0200525 usb_kill_anchored_urbs(&data->isoc_anchor);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200526
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200527 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
528 usb_kill_anchored_urbs(&data->bulk_anchor);
529
530 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
531 usb_kill_anchored_urbs(&data->intr_anchor);
532
533 return 0;
534}
535
536static int btusb_flush(struct hci_dev *hdev)
537{
538 struct btusb_data *data = hdev->driver_data;
539
540 BT_DBG("%s", hdev->name);
541
542 usb_kill_anchored_urbs(&data->tx_anchor);
543
544 return 0;
545}
546
547static int btusb_send_frame(struct sk_buff *skb)
548{
549 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
550 struct btusb_data *data = hdev->driver_data;
551 struct usb_ctrlrequest *dr;
552 struct urb *urb;
553 unsigned int pipe;
554 int err;
555
556 BT_DBG("%s", hdev->name);
557
558 if (!test_bit(HCI_RUNNING, &hdev->flags))
559 return -EBUSY;
560
561 switch (bt_cb(skb)->pkt_type) {
562 case HCI_COMMAND_PKT:
563 urb = usb_alloc_urb(0, GFP_ATOMIC);
564 if (!urb)
565 return -ENOMEM;
566
567 dr = kmalloc(sizeof(*dr), GFP_ATOMIC);
568 if (!dr) {
569 usb_free_urb(urb);
570 return -ENOMEM;
571 }
572
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100573 dr->bRequestType = data->cmdreq_type;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200574 dr->bRequest = 0;
575 dr->wIndex = 0;
576 dr->wValue = 0;
577 dr->wLength = __cpu_to_le16(skb->len);
578
579 pipe = usb_sndctrlpipe(data->udev, 0x00);
580
581 usb_fill_control_urb(urb, data->udev, pipe, (void *) dr,
582 skb->data, skb->len, btusb_tx_complete, skb);
583
584 hdev->stat.cmd_tx++;
585 break;
586
587 case HCI_ACLDATA_PKT:
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200588 if (!data->bulk_tx_ep || hdev->conn_hash.acl_num < 1)
589 return -ENODEV;
590
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200591 urb = usb_alloc_urb(0, GFP_ATOMIC);
592 if (!urb)
593 return -ENOMEM;
594
595 pipe = usb_sndbulkpipe(data->udev,
596 data->bulk_tx_ep->bEndpointAddress);
597
598 usb_fill_bulk_urb(urb, data->udev, pipe,
599 skb->data, skb->len, btusb_tx_complete, skb);
600
601 hdev->stat.acl_tx++;
602 break;
603
604 case HCI_SCODATA_PKT:
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200605 if (!data->isoc_tx_ep || hdev->conn_hash.sco_num < 1)
606 return -ENODEV;
607
608 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, GFP_ATOMIC);
609 if (!urb)
610 return -ENOMEM;
611
612 pipe = usb_sndisocpipe(data->udev,
613 data->isoc_tx_ep->bEndpointAddress);
614
615 urb->dev = data->udev;
616 urb->pipe = pipe;
617 urb->context = skb;
618 urb->complete = btusb_tx_complete;
619 urb->interval = data->isoc_tx_ep->bInterval;
620
621 urb->transfer_flags = URB_ISO_ASAP;
622 urb->transfer_buffer = skb->data;
623 urb->transfer_buffer_length = skb->len;
624
625 __fill_isoc_descriptor(urb, skb->len,
626 le16_to_cpu(data->isoc_tx_ep->wMaxPacketSize));
627
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200628 hdev->stat.sco_tx++;
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200629 break;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200630
631 default:
632 return -EILSEQ;
633 }
634
635 usb_anchor_urb(urb, &data->tx_anchor);
636
637 err = usb_submit_urb(urb, GFP_ATOMIC);
638 if (err < 0) {
639 BT_ERR("%s urb %p submission failed", hdev->name, urb);
640 kfree(urb->setup_packet);
641 usb_unanchor_urb(urb);
642 }
643
644 usb_free_urb(urb);
645
646 return err;
647}
648
649static void btusb_destruct(struct hci_dev *hdev)
650{
651 struct btusb_data *data = hdev->driver_data;
652
653 BT_DBG("%s", hdev->name);
654
655 kfree(data);
656}
657
658static void btusb_notify(struct hci_dev *hdev, unsigned int evt)
659{
660 struct btusb_data *data = hdev->driver_data;
661
662 BT_DBG("%s evt %d", hdev->name, evt);
663
Marcel Holtmanna780efa2008-11-30 12:17:12 +0100664 if (hdev->conn_hash.acl_num > 0) {
665 if (!test_and_set_bit(BTUSB_BULK_RUNNING, &data->flags)) {
666 if (btusb_submit_bulk_urb(hdev, GFP_ATOMIC) < 0)
667 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
668 else
669 btusb_submit_bulk_urb(hdev, GFP_ATOMIC);
670 }
671 } else {
672 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
673 usb_unlink_anchored_urbs(&data->bulk_anchor);
674 }
675
676 schedule_work(&data->work);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200677}
678
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200679static int inline __set_isoc_interface(struct hci_dev *hdev, int altsetting)
680{
681 struct btusb_data *data = hdev->driver_data;
682 struct usb_interface *intf = data->isoc;
683 struct usb_endpoint_descriptor *ep_desc;
684 int i, err;
685
686 if (!data->isoc)
687 return -ENODEV;
688
689 err = usb_set_interface(data->udev, 1, altsetting);
690 if (err < 0) {
691 BT_ERR("%s setting interface failed (%d)", hdev->name, -err);
692 return err;
693 }
694
695 data->isoc_altsetting = altsetting;
696
697 data->isoc_tx_ep = NULL;
698 data->isoc_rx_ep = NULL;
699
700 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
701 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
702
703 if (!data->isoc_tx_ep && usb_endpoint_is_isoc_out(ep_desc)) {
704 data->isoc_tx_ep = ep_desc;
705 continue;
706 }
707
708 if (!data->isoc_rx_ep && usb_endpoint_is_isoc_in(ep_desc)) {
709 data->isoc_rx_ep = ep_desc;
710 continue;
711 }
712 }
713
714 if (!data->isoc_tx_ep || !data->isoc_rx_ep) {
715 BT_ERR("%s invalid SCO descriptors", hdev->name);
716 return -ENODEV;
717 }
718
719 return 0;
720}
721
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200722static void btusb_work(struct work_struct *work)
723{
724 struct btusb_data *data = container_of(work, struct btusb_data, work);
725 struct hci_dev *hdev = data->hdev;
726
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200727 if (hdev->conn_hash.sco_num > 0) {
728 if (data->isoc_altsetting != 2) {
729 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
730 usb_kill_anchored_urbs(&data->isoc_anchor);
731
732 if (__set_isoc_interface(hdev, 2) < 0)
733 return;
734 }
735
736 if (!test_and_set_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100737 if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0)
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200738 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
739 else
Marcel Holtmann2eda66f2008-11-30 12:17:10 +0100740 btusb_submit_isoc_urb(hdev, GFP_KERNEL);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200741 }
742 } else {
743 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
744 usb_kill_anchored_urbs(&data->isoc_anchor);
745
746 __set_isoc_interface(hdev, 0);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200747 }
748}
749
750static int btusb_probe(struct usb_interface *intf,
751 const struct usb_device_id *id)
752{
753 struct usb_endpoint_descriptor *ep_desc;
754 struct btusb_data *data;
755 struct hci_dev *hdev;
756 int i, err;
757
758 BT_DBG("intf %p id %p", intf, id);
759
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200760 /* interface numbers are hardcoded in the spec */
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200761 if (intf->cur_altsetting->desc.bInterfaceNumber != 0)
762 return -ENODEV;
763
764 if (!id->driver_info) {
765 const struct usb_device_id *match;
766 match = usb_match_id(intf, blacklist_table);
767 if (match)
768 id = match;
769 }
770
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200771 if (id->driver_info == BTUSB_IGNORE)
772 return -ENODEV;
773
774 if (ignore_dga && id->driver_info & BTUSB_DIGIANSWER)
775 return -ENODEV;
776
777 if (ignore_csr && id->driver_info & BTUSB_CSR)
778 return -ENODEV;
779
780 if (ignore_sniffer && id->driver_info & BTUSB_SNIFFER)
781 return -ENODEV;
782
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200783 data = kzalloc(sizeof(*data), GFP_KERNEL);
784 if (!data)
785 return -ENOMEM;
786
787 for (i = 0; i < intf->cur_altsetting->desc.bNumEndpoints; i++) {
788 ep_desc = &intf->cur_altsetting->endpoint[i].desc;
789
790 if (!data->intr_ep && usb_endpoint_is_int_in(ep_desc)) {
791 data->intr_ep = ep_desc;
792 continue;
793 }
794
795 if (!data->bulk_tx_ep && usb_endpoint_is_bulk_out(ep_desc)) {
796 data->bulk_tx_ep = ep_desc;
797 continue;
798 }
799
800 if (!data->bulk_rx_ep && usb_endpoint_is_bulk_in(ep_desc)) {
801 data->bulk_rx_ep = ep_desc;
802 continue;
803 }
804 }
805
806 if (!data->intr_ep || !data->bulk_tx_ep || !data->bulk_rx_ep) {
807 kfree(data);
808 return -ENODEV;
809 }
810
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100811 data->cmdreq_type = USB_TYPE_CLASS;
812
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200813 data->udev = interface_to_usbdev(intf);
Marcel Holtmann5fbcd262008-09-23 00:16:36 +0200814 data->intf = intf;
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200815
816 spin_lock_init(&data->lock);
817
818 INIT_WORK(&data->work, btusb_work);
819
820 init_usb_anchor(&data->tx_anchor);
821 init_usb_anchor(&data->intr_anchor);
822 init_usb_anchor(&data->bulk_anchor);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200823 init_usb_anchor(&data->isoc_anchor);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200824
825 hdev = hci_alloc_dev();
826 if (!hdev) {
827 kfree(data);
828 return -ENOMEM;
829 }
830
831 hdev->type = HCI_USB;
832 hdev->driver_data = data;
833
834 data->hdev = hdev;
835
836 SET_HCIDEV_DEV(hdev, &intf->dev);
837
838 hdev->open = btusb_open;
839 hdev->close = btusb_close;
840 hdev->flush = btusb_flush;
841 hdev->send = btusb_send_frame;
842 hdev->destruct = btusb_destruct;
843 hdev->notify = btusb_notify;
844
845 hdev->owner = THIS_MODULE;
846
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100847 /* Interface numbers are hardcoded in the specification */
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200848 data->isoc = usb_ifnum_to_if(data->udev, 1);
849
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100850 if (!reset)
851 set_bit(HCI_QUIRK_NO_RESET, &hdev->quirks);
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200852
853 if (force_scofix || id->driver_info & BTUSB_WRONG_SCO_MTU) {
854 if (!disable_scofix)
855 set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
856 }
857
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200858 if (id->driver_info & BTUSB_BROKEN_ISOC)
859 data->isoc = NULL;
860
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100861 if (id->driver_info & BTUSB_DIGIANSWER) {
862 data->cmdreq_type = USB_TYPE_VENDOR;
863 set_bit(HCI_QUIRK_NO_RESET, &hdev->quirks);
864 }
865
866 if (id->driver_info & BTUSB_CSR) {
867 struct usb_device *udev = data->udev;
868
869 /* Old firmware would otherwise execute USB reset */
870 if (le16_to_cpu(udev->descriptor.bcdDevice) < 0x117)
871 set_bit(HCI_QUIRK_NO_RESET, &hdev->quirks);
872 }
873
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200874 if (id->driver_info & BTUSB_SNIFFER) {
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200875 struct usb_device *udev = data->udev;
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200876
Marcel Holtmann7a9d4022008-11-30 12:17:26 +0100877 /* New sniffer firmware has crippled HCI interface */
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200878 if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
879 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200880
881 data->isoc = NULL;
Marcel Holtmanncfeb4142008-08-07 22:26:56 +0200882 }
883
884 if (id->driver_info & BTUSB_BCM92035) {
885 unsigned char cmd[] = { 0x3b, 0xfc, 0x01, 0x00 };
886 struct sk_buff *skb;
887
888 skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL);
889 if (skb) {
890 memcpy(skb_put(skb, sizeof(cmd)), cmd, sizeof(cmd));
891 skb_queue_tail(&hdev->driver_init, skb);
892 }
893 }
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200894
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200895 if (data->isoc) {
896 err = usb_driver_claim_interface(&btusb_driver,
Marcel Holtmann5fbcd262008-09-23 00:16:36 +0200897 data->isoc, data);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200898 if (err < 0) {
899 hci_free_dev(hdev);
900 kfree(data);
901 return err;
902 }
903 }
904
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200905 err = hci_register_dev(hdev);
906 if (err < 0) {
907 hci_free_dev(hdev);
908 kfree(data);
909 return err;
910 }
911
912 usb_set_intfdata(intf, data);
913
914 return 0;
915}
916
917static void btusb_disconnect(struct usb_interface *intf)
918{
919 struct btusb_data *data = usb_get_intfdata(intf);
920 struct hci_dev *hdev;
921
922 BT_DBG("intf %p", intf);
923
924 if (!data)
925 return;
926
927 hdev = data->hdev;
928
Marcel Holtmann5fbcd262008-09-23 00:16:36 +0200929 __hci_dev_hold(hdev);
Marcel Holtmann9bfa35f2008-08-18 13:23:52 +0200930
Marcel Holtmann5fbcd262008-09-23 00:16:36 +0200931 usb_set_intfdata(data->intf, NULL);
932
933 if (data->isoc)
934 usb_set_intfdata(data->isoc, NULL);
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200935
936 hci_unregister_dev(hdev);
937
Marcel Holtmann5fbcd262008-09-23 00:16:36 +0200938 if (intf == data->isoc)
939 usb_driver_release_interface(&btusb_driver, data->intf);
940 else if (data->isoc)
941 usb_driver_release_interface(&btusb_driver, data->isoc);
942
943 __hci_dev_put(hdev);
944
Marcel Holtmann5e23b922007-10-20 14:12:34 +0200945 hci_free_dev(hdev);
946}
947
Marcel Holtmann6a88adf2008-11-30 12:17:14 +0100948static int btusb_suspend(struct usb_interface *intf, pm_message_t message)
949{
950 struct btusb_data *data = usb_get_intfdata(intf);
951
952 BT_DBG("intf %p", intf);
953
954 if (data->suspend_count++)
955 return 0;
956
957 cancel_work_sync(&data->work);
958
959 usb_kill_anchored_urbs(&data->tx_anchor);
960
961 usb_kill_anchored_urbs(&data->isoc_anchor);
962 usb_kill_anchored_urbs(&data->bulk_anchor);
963 usb_kill_anchored_urbs(&data->intr_anchor);
964
965 return 0;
966}
967
968static int btusb_resume(struct usb_interface *intf)
969{
970 struct btusb_data *data = usb_get_intfdata(intf);
971 struct hci_dev *hdev = data->hdev;
972 int err;
973
974 BT_DBG("intf %p", intf);
975
976 if (--data->suspend_count)
977 return 0;
978
979 if (!test_bit(HCI_RUNNING, &hdev->flags))
980 return 0;
981
982 if (test_bit(BTUSB_INTR_RUNNING, &data->flags)) {
983 err = btusb_submit_intr_urb(hdev, GFP_NOIO);
984 if (err < 0) {
985 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
986 return err;
987 }
988 }
989
990 if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) {
991 if (btusb_submit_bulk_urb(hdev, GFP_NOIO) < 0)
992 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
993 else
994 btusb_submit_bulk_urb(hdev, GFP_NOIO);
995 }
996
997 if (test_bit(BTUSB_ISOC_RUNNING, &data->flags)) {
998 if (btusb_submit_isoc_urb(hdev, GFP_NOIO) < 0)
999 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
1000 else
1001 btusb_submit_isoc_urb(hdev, GFP_NOIO);
1002 }
1003
1004 return 0;
1005}
1006
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001007static struct usb_driver btusb_driver = {
1008 .name = "btusb",
1009 .probe = btusb_probe,
1010 .disconnect = btusb_disconnect,
Marcel Holtmann6a88adf2008-11-30 12:17:14 +01001011 .suspend = btusb_suspend,
1012 .resume = btusb_resume,
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001013 .id_table = btusb_table,
1014};
1015
1016static int __init btusb_init(void)
1017{
1018 BT_INFO("Generic Bluetooth USB driver ver %s", VERSION);
1019
1020 return usb_register(&btusb_driver);
1021}
1022
1023static void __exit btusb_exit(void)
1024{
1025 usb_deregister(&btusb_driver);
1026}
1027
1028module_init(btusb_init);
1029module_exit(btusb_exit);
1030
Marcel Holtmanncfeb4142008-08-07 22:26:56 +02001031module_param(ignore_dga, bool, 0644);
1032MODULE_PARM_DESC(ignore_dga, "Ignore devices with id 08fd:0001");
1033
1034module_param(ignore_csr, bool, 0644);
1035MODULE_PARM_DESC(ignore_csr, "Ignore devices with id 0a12:0001");
1036
1037module_param(ignore_sniffer, bool, 0644);
1038MODULE_PARM_DESC(ignore_sniffer, "Ignore devices with id 0a12:0002");
1039
1040module_param(disable_scofix, bool, 0644);
1041MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
1042
1043module_param(force_scofix, bool, 0644);
1044MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
1045
1046module_param(reset, bool, 0644);
1047MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
1048
Marcel Holtmann5e23b922007-10-20 14:12:34 +02001049MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
1050MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
1051MODULE_VERSION(VERSION);
1052MODULE_LICENSE("GPL");