]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/bluetooth/btusb.c
ptrace: cleanup arch_ptrace() on cris
[net-next-2.6.git] / drivers / bluetooth / btusb.c
CommitLineData
5e23b923
MH
1/*
2 *
3 * Generic Bluetooth USB driver
4 *
9bfa35fe 5 * Copyright (C) 2005-2008 Marcel Holtmann <marcel@holtmann.org>
5e23b923
MH
6 *
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 */
23
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
7bee549e 38#define VERSION "0.6"
cfeb4145
MH
39
40static int ignore_dga;
41static int ignore_csr;
42static int ignore_sniffer;
43static int disable_scofix;
44static int force_scofix;
7a9d4020
MH
45
46static int reset = 1;
cfeb4145
MH
47
48static struct usb_driver btusb_driver;
49
50#define BTUSB_IGNORE 0x01
7a9d4020
MH
51#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
5e23b923
MH
57
58static struct usb_device_id btusb_table[] = {
59 /* Generic Bluetooth USB device */
60 { USB_DEVICE_INFO(0xe0, 0x01, 0x01) },
61
3cd01976
NI
62 /* Apple MacBookPro 7,1 */
63 { USB_DEVICE(0x05ac, 0x8213) },
64
0a79f674
CL
65 /* Apple iMac11,1 */
66 { USB_DEVICE(0x05ac, 0x8215) },
67
9c047157
NI
68 /* Apple MacBookPro6,2 */
69 { USB_DEVICE(0x05ac, 0x8218) },
70
cfeb4145
MH
71 /* 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 */
7a9d4020 85 { USB_DEVICE(0x0c10, 0x0000) },
cfeb4145 86
5e23b923
MH
87 { } /* Terminating entry */
88};
89
90MODULE_DEVICE_TABLE(usb, btusb_table);
91
92static struct usb_device_id blacklist_table[] = {
cfeb4145
MH
93 /* 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 */
7a9d4020
MH
100 { 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 },
cfeb4145
MH
103
104 /* Broadcom BCM2045 */
7a9d4020
MH
105 { USB_DEVICE(0x0a5c, 0x2039), .driver_info = BTUSB_WRONG_SCO_MTU },
106 { USB_DEVICE(0x0a5c, 0x2101), .driver_info = BTUSB_WRONG_SCO_MTU },
bdbef3d6 107
cfeb4145 108 /* IBM/Lenovo ThinkPad with Broadcom chip */
7a9d4020
MH
109 { USB_DEVICE(0x0a5c, 0x201e), .driver_info = BTUSB_WRONG_SCO_MTU },
110 { USB_DEVICE(0x0a5c, 0x2110), .driver_info = BTUSB_WRONG_SCO_MTU },
cfeb4145
MH
111
112 /* HP laptop with Broadcom chip */
7a9d4020 113 { USB_DEVICE(0x03f0, 0x171d), .driver_info = BTUSB_WRONG_SCO_MTU },
cfeb4145
MH
114
115 /* Dell laptop with Broadcom chip */
7a9d4020 116 { USB_DEVICE(0x413c, 0x8126), .driver_info = BTUSB_WRONG_SCO_MTU },
cfeb4145 117
5ddd4a60 118 /* Dell Wireless 370 and 410 devices */
7a9d4020 119 { USB_DEVICE(0x413c, 0x8152), .driver_info = BTUSB_WRONG_SCO_MTU },
5ddd4a60 120 { USB_DEVICE(0x413c, 0x8156), .driver_info = BTUSB_WRONG_SCO_MTU },
cfeb4145 121
7a9d4020
MH
122 /* 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 },
cfeb4145 125
5ddd4a60
MH
126 /* 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
cfeb4145
MH
132 /* 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
cfeb4145
MH
139 /* 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
5e23b923
MH
149 { } /* Terminating entry */
150};
151
9bfa35fe
MH
152#define BTUSB_MAX_ISOC_FRAMES 10
153
5e23b923
MH
154#define BTUSB_INTR_RUNNING 0
155#define BTUSB_BULK_RUNNING 1
9bfa35fe 156#define BTUSB_ISOC_RUNNING 2
7bee549e 157#define BTUSB_SUSPENDING 3
08b8b6c4 158#define BTUSB_DID_ISO_RESUME 4
5e23b923
MH
159
160struct btusb_data {
161 struct hci_dev *hdev;
162 struct usb_device *udev;
5fbcd260 163 struct usb_interface *intf;
9bfa35fe 164 struct usb_interface *isoc;
5e23b923
MH
165
166 spinlock_t lock;
167
168 unsigned long flags;
169
170 struct work_struct work;
7bee549e 171 struct work_struct waker;
5e23b923
MH
172
173 struct usb_anchor tx_anchor;
174 struct usb_anchor intr_anchor;
175 struct usb_anchor bulk_anchor;
9bfa35fe 176 struct usb_anchor isoc_anchor;
7bee549e
ON
177 struct usb_anchor deferred;
178 int tx_in_flight;
179 spinlock_t txlock;
5e23b923
MH
180
181 struct usb_endpoint_descriptor *intr_ep;
182 struct usb_endpoint_descriptor *bulk_tx_ep;
183 struct usb_endpoint_descriptor *bulk_rx_ep;
9bfa35fe
MH
184 struct usb_endpoint_descriptor *isoc_tx_ep;
185 struct usb_endpoint_descriptor *isoc_rx_ep;
186
7a9d4020
MH
187 __u8 cmdreq_type;
188
43c2e57f 189 unsigned int sco_num;
9bfa35fe 190 int isoc_altsetting;
6a88adf2 191 int suspend_count;
5e23b923
MH
192};
193
7bee549e
ON
194static 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
5e23b923
MH
208static 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) {
9bfa35fe
MH
221 hdev->stat.byte_rx += urb->actual_length;
222
5e23b923
MH
223 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
7bee549e 234 usb_mark_last_busy(data->udev);
5e23b923
MH
235 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
2eda66f4 245static int btusb_submit_intr_urb(struct hci_dev *hdev, gfp_t mem_flags)
5e23b923
MH
246{
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
9bfa35fe
MH
255 if (!data->intr_ep)
256 return -ENODEV;
257
2eda66f4 258 urb = usb_alloc_urb(0, mem_flags);
5e23b923
MH
259 if (!urb)
260 return -ENOMEM;
261
262 size = le16_to_cpu(data->intr_ep->wMaxPacketSize);
263
2eda66f4 264 buf = kmalloc(size, mem_flags);
5e23b923
MH
265 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
2eda66f4 280 err = usb_submit_urb(urb, mem_flags);
5e23b923
MH
281 if (err < 0) {
282 BT_ERR("%s urb %p submission failed (%d)",
283 hdev->name, urb, -err);
284 usb_unanchor_urb(urb);
5e23b923
MH
285 }
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) {
9bfa35fe
MH
305 hdev->stat.byte_rx += urb->actual_length;
306
5e23b923
MH
307 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);
652fd781 319 usb_mark_last_busy(data->udev);
5e23b923
MH
320
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
2eda66f4 329static int btusb_submit_bulk_urb(struct hci_dev *hdev, gfp_t mem_flags)
5e23b923
MH
330{
331 struct btusb_data *data = hdev->driver_data;
332 struct urb *urb;
333 unsigned char *buf;
334 unsigned int pipe;
290ba200 335 int err, size = HCI_MAX_FRAME_SIZE;
5e23b923
MH
336
337 BT_DBG("%s", hdev->name);
338
9bfa35fe
MH
339 if (!data->bulk_rx_ep)
340 return -ENODEV;
341
2eda66f4 342 urb = usb_alloc_urb(0, mem_flags);
5e23b923
MH
343 if (!urb)
344 return -ENOMEM;
345
2eda66f4 346 buf = kmalloc(size, mem_flags);
5e23b923
MH
347 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
7bee549e 359 usb_mark_last_busy(data->udev);
5e23b923
MH
360 usb_anchor_urb(urb, &data->bulk_anchor);
361
2eda66f4 362 err = usb_submit_urb(urb, mem_flags);
5e23b923
MH
363 if (err < 0) {
364 BT_ERR("%s urb %p submission failed (%d)",
365 hdev->name, urb, -err);
366 usb_unanchor_urb(urb);
5e23b923
MH
367 }
368
369 usb_free_urb(urb);
370
371 return err;
372}
373
9bfa35fe
MH
374static 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
2eda66f4 439static int btusb_submit_isoc_urb(struct hci_dev *hdev, gfp_t mem_flags)
9bfa35fe
MH
440{
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
2eda66f4 452 urb = usb_alloc_urb(BTUSB_MAX_ISOC_FRAMES, mem_flags);
9bfa35fe
MH
453 if (!urb)
454 return -ENOMEM;
455
456 size = le16_to_cpu(data->isoc_rx_ep->wMaxPacketSize) *
457 BTUSB_MAX_ISOC_FRAMES;
458
2eda66f4 459 buf = kmalloc(size, mem_flags);
9bfa35fe
MH
460 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
2eda66f4 482 err = usb_submit_urb(urb, mem_flags);
9bfa35fe
MH
483 if (err < 0) {
484 BT_ERR("%s urb %p submission failed (%d)",
485 hdev->name, urb, -err);
486 usb_unanchor_urb(urb);
9bfa35fe
MH
487 }
488
489 usb_free_urb(urb);
490
491 return err;
492}
493
5e23b923 494static void btusb_tx_complete(struct urb *urb)
7bee549e
ON
495{
496 struct sk_buff *skb = urb->context;
497 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
498 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)
5e23b923
MH
522{
523 struct sk_buff *skb = urb->context;
524 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
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 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
7bee549e
ON
550 err = usb_autopm_get_interface(data->intf);
551 if (err < 0)
552 return err;
553
554 data->intf->needs_remote_wakeup = 1;
555
5e23b923 556 if (test_and_set_bit(HCI_RUNNING, &hdev->flags))
7bee549e 557 goto done;
5e23b923
MH
558
559 if (test_and_set_bit(BTUSB_INTR_RUNNING, &data->flags))
7bee549e 560 goto done;
5e23b923 561
2eda66f4 562 err = btusb_submit_intr_urb(hdev, GFP_KERNEL);
43c2e57f
MH
563 if (err < 0)
564 goto failed;
565
566 err = btusb_submit_bulk_urb(hdev, GFP_KERNEL);
5e23b923 567 if (err < 0) {
43c2e57f
MH
568 usb_kill_anchored_urbs(&data->intr_anchor);
569 goto failed;
5e23b923
MH
570 }
571
43c2e57f
MH
572 set_bit(BTUSB_BULK_RUNNING, &data->flags);
573 btusb_submit_bulk_urb(hdev, GFP_KERNEL);
574
7bee549e
ON
575done:
576 usb_autopm_put_interface(data->intf);
43c2e57f
MH
577 return 0;
578
579failed:
580 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
581 clear_bit(HCI_RUNNING, &hdev->flags);
7bee549e 582 usb_autopm_put_interface(data->intf);
5e23b923
MH
583 return err;
584}
585
7bee549e
ON
586static 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
5e23b923
MH
593static int btusb_close(struct hci_dev *hdev)
594{
595 struct btusb_data *data = hdev->driver_data;
7bee549e 596 int err;
5e23b923
MH
597
598 BT_DBG("%s", hdev->name);
599
600 if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
601 return 0;
602
e8c3c3d2 603 cancel_work_sync(&data->work);
404291ac 604 cancel_work_sync(&data->waker);
e8c3c3d2 605
9bfa35fe 606 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
5e23b923 607 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
5e23b923 608 clear_bit(BTUSB_INTR_RUNNING, &data->flags);
7bee549e
ON
609
610 btusb_stop_traffic(data);
611 err = usb_autopm_get_interface(data->intf);
612 if (err < 0)
7b8e2c1d 613 goto failed;
7bee549e
ON
614
615 data->intf->needs_remote_wakeup = 0;
616 usb_autopm_put_interface(data->intf);
5e23b923 617
7b8e2c1d
ON
618failed:
619 usb_scuttle_anchored_urbs(&data->deferred);
5e23b923
MH
620 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
7a9d4020 660 dr->bRequestType = data->cmdreq_type;
5e23b923
MH
661 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:
9bfa35fe
MH
675 if (!data->bulk_tx_ep || hdev->conn_hash.acl_num < 1)
676 return -ENODEV;
677
5e23b923
MH
678 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:
9bfa35fe
MH
692 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;
7bee549e 705 urb->complete = btusb_isoc_tx_complete;
9bfa35fe
MH
706 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
5e23b923 715 hdev->stat.sco_tx++;
7bee549e 716 goto skip_waking;
5e23b923
MH
717
718 default:
719 return -EILSEQ;
720 }
721
7bee549e
ON
722 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:
5e23b923
MH
731 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);
7bee549e
ON
738 } else {
739 usb_mark_last_busy(data->udev);
5e23b923
MH
740 }
741
742 usb_free_urb(urb);
743
7bee549e 744done:
5e23b923
MH
745 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
43c2e57f
MH
763 if (hdev->conn_hash.sco_num != data->sco_num) {
764 data->sco_num = hdev->conn_hash.sco_num;
765 schedule_work(&data->work);
a780efa8 766 }
5e23b923
MH
767}
768
9bfa35fe
MH
769static 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
5e23b923
MH
812static 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;
7bee549e 816 int err;
5e23b923 817
9bfa35fe 818 if (hdev->conn_hash.sco_num > 0) {
08b8b6c4 819 if (!test_bit(BTUSB_DID_ISO_RESUME, &data->flags)) {
7bee549e
ON
820 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
08b8b6c4 827 set_bit(BTUSB_DID_ISO_RESUME, &data->flags);
7bee549e 828 }
9bfa35fe
MH
829 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)) {
2eda66f4 838 if (btusb_submit_isoc_urb(hdev, GFP_KERNEL) < 0)
9bfa35fe
MH
839 clear_bit(BTUSB_ISOC_RUNNING, &data->flags);
840 else
2eda66f4 841 btusb_submit_isoc_urb(hdev, GFP_KERNEL);
9bfa35fe
MH
842 }
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);
08b8b6c4 848 if (test_and_clear_bit(BTUSB_DID_ISO_RESUME, &data->flags))
7bee549e 849 usb_autopm_put_interface(data->isoc);
5e23b923
MH
850 }
851}
852
7bee549e
ON
853static 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
5e23b923
MH
865static 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
cfeb4145 875 /* interface numbers are hardcoded in the spec */
5e23b923
MH
876 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
cfeb4145
MH
886 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
5e23b923
MH
898 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
7a9d4020
MH
926 data->cmdreq_type = USB_TYPE_CLASS;
927
5e23b923 928 data->udev = interface_to_usbdev(intf);
5fbcd260 929 data->intf = intf;
5e23b923
MH
930
931 spin_lock_init(&data->lock);
932
933 INIT_WORK(&data->work, btusb_work);
7bee549e
ON
934 INIT_WORK(&data->waker, btusb_waker);
935 spin_lock_init(&data->txlock);
5e23b923
MH
936
937 init_usb_anchor(&data->tx_anchor);
938 init_usb_anchor(&data->intr_anchor);
939 init_usb_anchor(&data->bulk_anchor);
9bfa35fe 940 init_usb_anchor(&data->isoc_anchor);
7bee549e 941 init_usb_anchor(&data->deferred);
5e23b923
MH
942
943 hdev = hci_alloc_dev();
944 if (!hdev) {
945 kfree(data);
946 return -ENOMEM;
947 }
948
c13854ce 949 hdev->bus = HCI_USB;
5e23b923
MH
950 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
7a9d4020 965 /* Interface numbers are hardcoded in the specification */
9bfa35fe
MH
966 data->isoc = usb_ifnum_to_if(data->udev, 1);
967
7a9d4020
MH
968 if (!reset)
969 set_bit(HCI_QUIRK_NO_RESET, &hdev->quirks);
cfeb4145
MH
970
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
9bfa35fe
MH
976 if (id->driver_info & BTUSB_BROKEN_ISOC)
977 data->isoc = NULL;
978
7a9d4020
MH
979 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
cfeb4145 992 if (id->driver_info & BTUSB_SNIFFER) {
9bfa35fe 993 struct usb_device *udev = data->udev;
cfeb4145 994
7a9d4020 995 /* New sniffer firmware has crippled HCI interface */
cfeb4145
MH
996 if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
997 set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
9bfa35fe
MH
998
999 data->isoc = NULL;
cfeb4145
MH
1000 }
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 }
5e23b923 1012
9bfa35fe
MH
1013 if (data->isoc) {
1014 err = usb_driver_claim_interface(&btusb_driver,
5fbcd260 1015 data->isoc, data);
9bfa35fe
MH
1016 if (err < 0) {
1017 hci_free_dev(hdev);
1018 kfree(data);
1019 return err;
1020 }
1021 }
1022
5e23b923
MH
1023 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
5fbcd260 1047 __hci_dev_hold(hdev);
9bfa35fe 1048
5fbcd260
MH
1049 usb_set_intfdata(data->intf, NULL);
1050
1051 if (data->isoc)
1052 usb_set_intfdata(data->isoc, NULL);
5e23b923
MH
1053
1054 hci_unregister_dev(hdev);
1055
5fbcd260
MH
1056 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
5e23b923
MH
1063 hci_free_dev(hdev);
1064}
1065
7bee549e 1066#ifdef CONFIG_PM
6a88adf2
MH
1067static 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
7bee549e 1076 spin_lock_irq(&data->txlock);
fb34d537 1077 if (!((message.event & PM_EVENT_AUTO) && data->tx_in_flight)) {
7bee549e
ON
1078 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
6a88adf2
MH
1086 cancel_work_sync(&data->work);
1087
7bee549e 1088 btusb_stop_traffic(data);
6a88adf2
MH
1089 usb_kill_anchored_urbs(&data->tx_anchor);
1090
6a88adf2
MH
1091 return 0;
1092}
1093
7bee549e
ON
1094static 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
6a88adf2
MH
1109static int btusb_resume(struct usb_interface *intf)
1110{
1111 struct btusb_data *data = usb_get_intfdata(intf);
1112 struct hci_dev *hdev = data->hdev;
7bee549e 1113 int err = 0;
6a88adf2
MH
1114
1115 BT_DBG("intf %p", intf);
1116
1117 if (--data->suspend_count)
1118 return 0;
1119
1120 if (!test_bit(HCI_RUNNING, &hdev->flags))
7bee549e 1121 goto done;
6a88adf2
MH
1122
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);
7bee549e 1127 goto failed;
6a88adf2
MH
1128 }
1129 }
1130
1131 if (test_bit(BTUSB_BULK_RUNNING, &data->flags)) {
43c2e57f
MH
1132 err = btusb_submit_bulk_urb(hdev, GFP_NOIO);
1133 if (err < 0) {
6a88adf2 1134 clear_bit(BTUSB_BULK_RUNNING, &data->flags);
7bee549e
ON
1135 goto failed;
1136 }
1137
1138 btusb_submit_bulk_urb(hdev, GFP_NOIO);
6a88adf2
MH
1139 }
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
7bee549e
ON
1148 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
6a88adf2 1154 return 0;
7bee549e
ON
1155
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;
6a88adf2 1164}
7bee549e 1165#endif
6a88adf2 1166
5e23b923
MH
1167static struct usb_driver btusb_driver = {
1168 .name = "btusb",
1169 .probe = btusb_probe,
1170 .disconnect = btusb_disconnect,
7bee549e 1171#ifdef CONFIG_PM
6a88adf2
MH
1172 .suspend = btusb_suspend,
1173 .resume = btusb_resume,
7bee549e 1174#endif
5e23b923 1175 .id_table = btusb_table,
7bee549e 1176 .supports_autosuspend = 1,
5e23b923
MH
1177};
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
cfeb4145
MH
1194module_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
5e23b923
MH
1212MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
1213MODULE_DESCRIPTION("Generic Bluetooth USB driver ver " VERSION);
1214MODULE_VERSION(VERSION);
1215MODULE_LICENSE("GPL");