[Bluetooth] Add SCO support to btusb driver
[linux-2.6.git] / drivers / bluetooth / hci_usb.c
1 /* 
2    HCI USB driver for Linux Bluetooth protocol stack (BlueZ)
3    Copyright (C) 2000-2001 Qualcomm Incorporated
4    Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
5
6    Copyright (C) 2003 Maxim Krasnyansky <maxk@qualcomm.com>
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 version 2 as
10    published by the Free Software Foundation;
11
12    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
13    OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
15    IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
16    CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES 
17    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 
18    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 
19    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20
21    ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, 
22    COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS 
23    SOFTWARE IS DISCLAIMED.
24 */
25
26 /*
27  * Bluetooth HCI USB driver.
28  * Based on original USB Bluetooth driver for Linux kernel
29  *    Copyright (c) 2000 Greg Kroah-Hartman        <greg@kroah.com>
30  *    Copyright (c) 2000 Mark Douglas Corner       <mcorner@umich.edu>
31  *
32  */
33
34 #include <linux/module.h>
35
36 #include <linux/kernel.h>
37 #include <linux/init.h>
38 #include <linux/unistd.h>
39 #include <linux/types.h>
40 #include <linux/interrupt.h>
41 #include <linux/moduleparam.h>
42
43 #include <linux/slab.h>
44 #include <linux/errno.h>
45 #include <linux/string.h>
46 #include <linux/skbuff.h>
47
48 #include <linux/usb.h>
49
50 #include <net/bluetooth/bluetooth.h>
51 #include <net/bluetooth/hci_core.h>
52
53 #include "hci_usb.h"
54
55 #ifndef CONFIG_BT_HCIUSB_DEBUG
56 #undef  BT_DBG
57 #define BT_DBG(D...)
58 #endif
59
60 #ifndef CONFIG_BT_HCIUSB_ZERO_PACKET
61 #undef  URB_ZERO_PACKET
62 #define URB_ZERO_PACKET 0
63 #endif
64
65 static int ignore_dga;
66 static int ignore_csr;
67 static int ignore_sniffer;
68 static int disable_scofix;
69 static int force_scofix;
70 static int reset;
71
72 #ifdef CONFIG_BT_HCIUSB_SCO
73 static int isoc = 2;
74 #endif
75
76 #define VERSION "2.10"
77
78 static struct usb_driver hci_usb_driver; 
79
80 static struct usb_device_id bluetooth_ids[] = {
81         /* Generic Bluetooth USB device */
82         { USB_DEVICE_INFO(HCI_DEV_CLASS, HCI_DEV_SUBCLASS, HCI_DEV_PROTOCOL) },
83
84         /* AVM BlueFRITZ! USB v2.0 */
85         { USB_DEVICE(0x057c, 0x3800) },
86
87         /* Bluetooth Ultraport Module from IBM */
88         { USB_DEVICE(0x04bf, 0x030a) },
89
90         /* ALPS Modules with non-standard id */
91         { USB_DEVICE(0x044e, 0x3001) },
92         { USB_DEVICE(0x044e, 0x3002) },
93
94         /* Ericsson with non-standard id */
95         { USB_DEVICE(0x0bdb, 0x1002) },
96
97         /* Canyon CN-BTU1 with HID interfaces */
98         { USB_DEVICE(0x0c10, 0x0000), .driver_info = HCI_RESET },
99
100         { }     /* Terminating entry */
101 };
102
103 MODULE_DEVICE_TABLE (usb, bluetooth_ids);
104
105 static struct usb_device_id blacklist_ids[] = {
106         /* CSR BlueCore devices */
107         { USB_DEVICE(0x0a12, 0x0001), .driver_info = HCI_CSR },
108
109         /* Broadcom BCM2033 without firmware */
110         { USB_DEVICE(0x0a5c, 0x2033), .driver_info = HCI_IGNORE },
111
112         /* Broadcom BCM2035 */
113         { USB_DEVICE(0x0a5c, 0x2035), .driver_info = HCI_RESET | HCI_WRONG_SCO_MTU },
114         { USB_DEVICE(0x0a5c, 0x200a), .driver_info = HCI_RESET | HCI_WRONG_SCO_MTU },
115         { USB_DEVICE(0x0a5c, 0x2009), .driver_info = HCI_BCM92035 },
116
117         /* Broadcom BCM2045 */
118         { USB_DEVICE(0x0a5c, 0x2039), .driver_info = HCI_RESET | HCI_WRONG_SCO_MTU },
119         { USB_DEVICE(0x0a5c, 0x2101), .driver_info = HCI_RESET | HCI_WRONG_SCO_MTU },
120
121         /* IBM/Lenovo ThinkPad with Broadcom chip */
122         { USB_DEVICE(0x0a5c, 0x201e), .driver_info = HCI_RESET | HCI_WRONG_SCO_MTU },
123         { USB_DEVICE(0x0a5c, 0x2110), .driver_info = HCI_RESET | HCI_WRONG_SCO_MTU },
124
125         /* Targus ACB10US */
126         { USB_DEVICE(0x0a5c, 0x2100), .driver_info = HCI_RESET },
127
128         /* ANYCOM Bluetooth USB-200 and USB-250 */
129         { USB_DEVICE(0x0a5c, 0x2111), .driver_info = HCI_RESET },
130
131         /* HP laptop with Broadcom chip */
132         { USB_DEVICE(0x03f0, 0x171d), .driver_info = HCI_RESET | HCI_WRONG_SCO_MTU },
133
134         /* Dell laptop with Broadcom chip */
135         { USB_DEVICE(0x413c, 0x8126), .driver_info = HCI_RESET | HCI_WRONG_SCO_MTU },
136         /* Dell Wireless 370 */
137         { USB_DEVICE(0x413c, 0x8156), .driver_info = HCI_RESET | HCI_WRONG_SCO_MTU },
138         /* Dell Wireless 410 */
139         { USB_DEVICE(0x413c, 0x8152), .driver_info = HCI_RESET | HCI_WRONG_SCO_MTU },
140
141         /* Broadcom 2046 */
142         { USB_DEVICE(0x0a5c, 0x2151), .driver_info = HCI_RESET },
143
144         /* Microsoft Wireless Transceiver for Bluetooth 2.0 */
145         { USB_DEVICE(0x045e, 0x009c), .driver_info = HCI_RESET },
146
147         /* Kensington Bluetooth USB adapter */
148         { USB_DEVICE(0x047d, 0x105d), .driver_info = HCI_RESET },
149         { USB_DEVICE(0x047d, 0x105e), .driver_info = HCI_RESET | HCI_WRONG_SCO_MTU },
150
151         /* ISSC Bluetooth Adapter v3.1 */
152         { USB_DEVICE(0x1131, 0x1001), .driver_info = HCI_RESET },
153
154         /* RTX Telecom based adapters with buggy SCO support */
155         { USB_DEVICE(0x0400, 0x0807), .driver_info = HCI_BROKEN_ISOC },
156         { USB_DEVICE(0x0400, 0x080a), .driver_info = HCI_BROKEN_ISOC },
157
158         /* CONWISE Technology based adapters with buggy SCO support */
159         { USB_DEVICE(0x0e5e, 0x6622), .driver_info = HCI_BROKEN_ISOC },
160
161         /* Belkin F8T012 and F8T013 devices */
162         { USB_DEVICE(0x050d, 0x0012), .driver_info = HCI_RESET | HCI_WRONG_SCO_MTU },
163         { USB_DEVICE(0x050d, 0x0013), .driver_info = HCI_RESET | HCI_WRONG_SCO_MTU },
164
165         /* Digianswer devices */
166         { USB_DEVICE(0x08fd, 0x0001), .driver_info = HCI_DIGIANSWER },
167         { USB_DEVICE(0x08fd, 0x0002), .driver_info = HCI_IGNORE },
168
169         /* CSR BlueCore Bluetooth Sniffer */
170         { USB_DEVICE(0x0a12, 0x0002), .driver_info = HCI_SNIFFER },
171
172         /* Frontline ComProbe Bluetooth Sniffer */
173         { USB_DEVICE(0x16d3, 0x0002), .driver_info = HCI_SNIFFER },
174
175         { }     /* Terminating entry */
176 };
177
178 static struct _urb *_urb_alloc(int isoc, gfp_t gfp)
179 {
180         struct _urb *_urb = kmalloc(sizeof(struct _urb) +
181                                 sizeof(struct usb_iso_packet_descriptor) * isoc, gfp);
182         if (_urb) {
183                 memset(_urb, 0, sizeof(*_urb));
184                 usb_init_urb(&_urb->urb);
185         }
186         return _urb;
187 }
188
189 static struct _urb *_urb_dequeue(struct _urb_queue *q)
190 {
191         struct _urb *_urb = NULL;
192         unsigned long flags;
193         spin_lock_irqsave(&q->lock, flags);
194         {
195                 struct list_head *head = &q->head;
196                 struct list_head *next = head->next;
197                 if (next != head) {
198                         _urb = list_entry(next, struct _urb, list);
199                         list_del(next); _urb->queue = NULL;
200                 }
201         }
202         spin_unlock_irqrestore(&q->lock, flags);
203         return _urb;
204 }
205
206 static void hci_usb_rx_complete(struct urb *urb);
207 static void hci_usb_tx_complete(struct urb *urb);
208
209 #define __pending_tx(husb, type)  (&husb->pending_tx[type-1])
210 #define __pending_q(husb, type)   (&husb->pending_q[type-1])
211 #define __completed_q(husb, type) (&husb->completed_q[type-1])
212 #define __transmit_q(husb, type)  (&husb->transmit_q[type-1])
213
214 static inline struct _urb *__get_completed(struct hci_usb *husb, int type)
215 {
216         return _urb_dequeue(__completed_q(husb, type)); 
217 }
218
219 #ifdef CONFIG_BT_HCIUSB_SCO
220 static void __fill_isoc_desc(struct urb *urb, int len, int mtu)
221 {
222         int offset = 0, i;
223
224         BT_DBG("len %d mtu %d", len, mtu);
225
226         for (i=0; i < HCI_MAX_ISOC_FRAMES && len >= mtu; i++, offset += mtu, len -= mtu) {
227                 urb->iso_frame_desc[i].offset = offset;
228                 urb->iso_frame_desc[i].length = mtu;
229                 BT_DBG("desc %d offset %d len %d", i, offset, mtu);
230         }
231         if (len && i < HCI_MAX_ISOC_FRAMES) {
232                 urb->iso_frame_desc[i].offset = offset;
233                 urb->iso_frame_desc[i].length = len;
234                 BT_DBG("desc %d offset %d len %d", i, offset, len);
235                 i++;
236         }
237         urb->number_of_packets = i;
238 }
239 #endif
240
241 static int hci_usb_intr_rx_submit(struct hci_usb *husb)
242 {
243         struct _urb *_urb;
244         struct urb *urb;
245         int err, pipe, interval, size;
246         void *buf;
247
248         BT_DBG("%s", husb->hdev->name);
249
250         size = le16_to_cpu(husb->intr_in_ep->desc.wMaxPacketSize);
251
252         buf = kmalloc(size, GFP_ATOMIC);
253         if (!buf)
254                 return -ENOMEM;
255
256         _urb = _urb_alloc(0, GFP_ATOMIC);
257         if (!_urb) {
258                 kfree(buf);
259                 return -ENOMEM;
260         }
261         _urb->type = HCI_EVENT_PKT;
262         _urb_queue_tail(__pending_q(husb, _urb->type), _urb);
263
264         urb = &_urb->urb;
265         pipe     = usb_rcvintpipe(husb->udev, husb->intr_in_ep->desc.bEndpointAddress);
266         interval = husb->intr_in_ep->desc.bInterval;
267         usb_fill_int_urb(urb, husb->udev, pipe, buf, size, hci_usb_rx_complete, husb, interval);
268         
269         err = usb_submit_urb(urb, GFP_ATOMIC);
270         if (err) {
271                 BT_ERR("%s intr rx submit failed urb %p err %d",
272                                 husb->hdev->name, urb, err);
273                 _urb_unlink(_urb);
274                 kfree(_urb);
275                 kfree(buf);
276         }
277         return err;
278 }
279
280 static int hci_usb_bulk_rx_submit(struct hci_usb *husb)
281 {
282         struct _urb *_urb;
283         struct urb *urb;
284         int err, pipe, size = HCI_MAX_FRAME_SIZE;
285         void *buf;
286
287         buf = kmalloc(size, GFP_ATOMIC);
288         if (!buf)
289                 return -ENOMEM;
290
291         _urb = _urb_alloc(0, GFP_ATOMIC);
292         if (!_urb) {
293                 kfree(buf);
294                 return -ENOMEM;
295         }
296         _urb->type = HCI_ACLDATA_PKT;
297         _urb_queue_tail(__pending_q(husb, _urb->type), _urb);
298
299         urb  = &_urb->urb;
300         pipe = usb_rcvbulkpipe(husb->udev, husb->bulk_in_ep->desc.bEndpointAddress);
301         usb_fill_bulk_urb(urb, husb->udev, pipe, buf, size, hci_usb_rx_complete, husb);
302         urb->transfer_flags = 0;
303
304         BT_DBG("%s urb %p", husb->hdev->name, urb);
305
306         err = usb_submit_urb(urb, GFP_ATOMIC);
307         if (err) {
308                 BT_ERR("%s bulk rx submit failed urb %p err %d",
309                                 husb->hdev->name, urb, err);
310                 _urb_unlink(_urb);
311                 kfree(_urb);
312                 kfree(buf);
313         }
314         return err;
315 }
316
317 #ifdef CONFIG_BT_HCIUSB_SCO
318 static int hci_usb_isoc_rx_submit(struct hci_usb *husb)
319 {
320         struct _urb *_urb;
321         struct urb *urb;
322         int err, mtu, size;
323         void *buf;
324
325         mtu  = le16_to_cpu(husb->isoc_in_ep->desc.wMaxPacketSize);
326         size = mtu * HCI_MAX_ISOC_FRAMES;
327
328         buf = kmalloc(size, GFP_ATOMIC);
329         if (!buf)
330                 return -ENOMEM;
331
332         _urb = _urb_alloc(HCI_MAX_ISOC_FRAMES, GFP_ATOMIC);
333         if (!_urb) {
334                 kfree(buf);
335                 return -ENOMEM;
336         }
337         _urb->type = HCI_SCODATA_PKT;
338         _urb_queue_tail(__pending_q(husb, _urb->type), _urb);
339
340         urb = &_urb->urb;
341
342         urb->context  = husb;
343         urb->dev      = husb->udev;
344         urb->pipe     = usb_rcvisocpipe(husb->udev, husb->isoc_in_ep->desc.bEndpointAddress);
345         urb->complete = hci_usb_rx_complete;
346
347         urb->interval = husb->isoc_in_ep->desc.bInterval;
348
349         urb->transfer_buffer_length = size;
350         urb->transfer_buffer = buf;
351         urb->transfer_flags  = URB_ISO_ASAP;
352
353         __fill_isoc_desc(urb, size, mtu);
354
355         BT_DBG("%s urb %p", husb->hdev->name, urb);
356
357         err = usb_submit_urb(urb, GFP_ATOMIC);
358         if (err) {
359                 BT_ERR("%s isoc rx submit failed urb %p err %d",
360                                 husb->hdev->name, urb, err);
361                 _urb_unlink(_urb);
362                 kfree(_urb);
363                 kfree(buf);
364         }
365         return err;
366 }
367 #endif
368
369 /* Initialize device */
370 static int hci_usb_open(struct hci_dev *hdev)
371 {
372         struct hci_usb *husb = (struct hci_usb *) hdev->driver_data;
373         int i, err;
374         unsigned long flags;
375
376         BT_DBG("%s", hdev->name);
377
378         if (test_and_set_bit(HCI_RUNNING, &hdev->flags))
379                 return 0;
380
381         write_lock_irqsave(&husb->completion_lock, flags);
382
383         err = hci_usb_intr_rx_submit(husb);
384         if (!err) {
385                 for (i = 0; i < HCI_MAX_BULK_RX; i++)
386                         hci_usb_bulk_rx_submit(husb);
387
388 #ifdef CONFIG_BT_HCIUSB_SCO
389                 if (husb->isoc_iface)
390                         for (i = 0; i < HCI_MAX_ISOC_RX; i++)
391                                 hci_usb_isoc_rx_submit(husb);
392 #endif
393         } else {
394                 clear_bit(HCI_RUNNING, &hdev->flags);
395         }
396
397         write_unlock_irqrestore(&husb->completion_lock, flags);
398         return err;
399 }
400
401 /* Reset device */
402 static int hci_usb_flush(struct hci_dev *hdev)
403 {
404         struct hci_usb *husb = (struct hci_usb *) hdev->driver_data;
405         int i;
406
407         BT_DBG("%s", hdev->name);
408
409         for (i = 0; i < 4; i++)
410                 skb_queue_purge(&husb->transmit_q[i]);
411         return 0;
412 }
413
414 static void hci_usb_unlink_urbs(struct hci_usb *husb)
415 {
416         int i;
417
418         BT_DBG("%s", husb->hdev->name);
419
420         for (i = 0; i < 4; i++) {
421                 struct _urb *_urb;
422                 struct urb *urb;
423
424                 /* Kill pending requests */
425                 while ((_urb = _urb_dequeue(&husb->pending_q[i]))) {
426                         urb = &_urb->urb;
427                         BT_DBG("%s unlinking _urb %p type %d urb %p", 
428                                         husb->hdev->name, _urb, _urb->type, urb);
429                         usb_kill_urb(urb);
430                         _urb_queue_tail(__completed_q(husb, _urb->type), _urb);
431                 }
432
433                 /* Release completed requests */
434                 while ((_urb = _urb_dequeue(&husb->completed_q[i]))) {
435                         urb = &_urb->urb;
436                         BT_DBG("%s freeing _urb %p type %d urb %p",
437                                         husb->hdev->name, _urb, _urb->type, urb);
438                         kfree(urb->setup_packet);
439                         kfree(urb->transfer_buffer);
440                         kfree(_urb);
441                 }
442         }
443 }
444
445 /* Close device */
446 static int hci_usb_close(struct hci_dev *hdev)
447 {
448         struct hci_usb *husb = (struct hci_usb *) hdev->driver_data;
449         unsigned long flags;
450
451         if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
452                 return 0;
453
454         BT_DBG("%s", hdev->name);
455
456         /* Synchronize with completion handlers */
457         write_lock_irqsave(&husb->completion_lock, flags);
458         write_unlock_irqrestore(&husb->completion_lock, flags);
459
460         hci_usb_unlink_urbs(husb);
461         hci_usb_flush(hdev);
462         return 0;
463 }
464
465 static int __tx_submit(struct hci_usb *husb, struct _urb *_urb)
466 {
467         struct urb *urb = &_urb->urb;
468         int err;
469
470         BT_DBG("%s urb %p type %d", husb->hdev->name, urb, _urb->type);
471
472         _urb_queue_tail(__pending_q(husb, _urb->type), _urb);
473         err = usb_submit_urb(urb, GFP_ATOMIC);
474         if (err) {
475                 BT_ERR("%s tx submit failed urb %p type %d err %d",
476                                 husb->hdev->name, urb, _urb->type, err);
477                 _urb_unlink(_urb);
478                 _urb_queue_tail(__completed_q(husb, _urb->type), _urb);
479         } else
480                 atomic_inc(__pending_tx(husb, _urb->type));
481
482         return err;
483 }
484
485 static inline int hci_usb_send_ctrl(struct hci_usb *husb, struct sk_buff *skb)
486 {
487         struct _urb *_urb = __get_completed(husb, bt_cb(skb)->pkt_type);
488         struct usb_ctrlrequest *dr;
489         struct urb *urb;
490
491         if (!_urb) {
492                 _urb = _urb_alloc(0, GFP_ATOMIC);
493                 if (!_urb)
494                         return -ENOMEM;
495                 _urb->type = bt_cb(skb)->pkt_type;
496
497                 dr = kmalloc(sizeof(*dr), GFP_ATOMIC);
498                 if (!dr) {
499                         kfree(_urb);
500                         return -ENOMEM;
501                 }
502         } else
503                 dr = (void *) _urb->urb.setup_packet;
504
505         dr->bRequestType = husb->ctrl_req;
506         dr->bRequest = 0;
507         dr->wIndex   = 0;
508         dr->wValue   = 0;
509         dr->wLength  = __cpu_to_le16(skb->len);
510
511         urb = &_urb->urb;
512         usb_fill_control_urb(urb, husb->udev, usb_sndctrlpipe(husb->udev, 0),
513                 (void *) dr, skb->data, skb->len, hci_usb_tx_complete, husb);
514
515         BT_DBG("%s skb %p len %d", husb->hdev->name, skb, skb->len);
516         
517         _urb->priv = skb;
518         return __tx_submit(husb, _urb);
519 }
520
521 static inline int hci_usb_send_bulk(struct hci_usb *husb, struct sk_buff *skb)
522 {
523         struct _urb *_urb = __get_completed(husb, bt_cb(skb)->pkt_type);
524         struct urb *urb;
525         int pipe;
526
527         if (!_urb) {
528                 _urb = _urb_alloc(0, GFP_ATOMIC);
529                 if (!_urb)
530                         return -ENOMEM;
531                 _urb->type = bt_cb(skb)->pkt_type;
532         }
533
534         urb  = &_urb->urb;
535         pipe = usb_sndbulkpipe(husb->udev, husb->bulk_out_ep->desc.bEndpointAddress);
536         usb_fill_bulk_urb(urb, husb->udev, pipe, skb->data, skb->len, 
537                         hci_usb_tx_complete, husb);
538         urb->transfer_flags = URB_ZERO_PACKET;
539
540         BT_DBG("%s skb %p len %d", husb->hdev->name, skb, skb->len);
541
542         _urb->priv = skb;
543         return __tx_submit(husb, _urb);
544 }
545
546 #ifdef CONFIG_BT_HCIUSB_SCO
547 static inline int hci_usb_send_isoc(struct hci_usb *husb, struct sk_buff *skb)
548 {
549         struct _urb *_urb = __get_completed(husb, bt_cb(skb)->pkt_type);
550         struct urb *urb;
551
552         if (!_urb) {
553                 _urb = _urb_alloc(HCI_MAX_ISOC_FRAMES, GFP_ATOMIC);
554                 if (!_urb)
555                         return -ENOMEM;
556                 _urb->type = bt_cb(skb)->pkt_type;
557         }
558
559         BT_DBG("%s skb %p len %d", husb->hdev->name, skb, skb->len);
560
561         urb = &_urb->urb;
562
563         urb->context  = husb;
564         urb->dev      = husb->udev;
565         urb->pipe     = usb_sndisocpipe(husb->udev, husb->isoc_out_ep->desc.bEndpointAddress);
566         urb->complete = hci_usb_tx_complete;
567         urb->transfer_flags = URB_ISO_ASAP;
568
569         urb->interval = husb->isoc_out_ep->desc.bInterval;
570
571         urb->transfer_buffer = skb->data;
572         urb->transfer_buffer_length = skb->len;
573
574         __fill_isoc_desc(urb, skb->len, le16_to_cpu(husb->isoc_out_ep->desc.wMaxPacketSize));
575
576         _urb->priv = skb;
577         return __tx_submit(husb, _urb);
578 }
579 #endif
580
581 static void hci_usb_tx_process(struct hci_usb *husb)
582 {
583         struct sk_buff_head *q;
584         struct sk_buff *skb;
585
586         BT_DBG("%s", husb->hdev->name);
587
588         do {
589                 clear_bit(HCI_USB_TX_WAKEUP, &husb->state);
590
591                 /* Process command queue */
592                 q = __transmit_q(husb, HCI_COMMAND_PKT);
593                 if (!atomic_read(__pending_tx(husb, HCI_COMMAND_PKT)) &&
594                                 (skb = skb_dequeue(q))) {
595                         if (hci_usb_send_ctrl(husb, skb) < 0)
596                                 skb_queue_head(q, skb);
597                 }
598
599 #ifdef CONFIG_BT_HCIUSB_SCO
600                 /* Process SCO queue */
601                 q = __transmit_q(husb, HCI_SCODATA_PKT);
602                 if (atomic_read(__pending_tx(husb, HCI_SCODATA_PKT)) < HCI_MAX_ISOC_TX &&
603                                 (skb = skb_dequeue(q))) {
604                         if (hci_usb_send_isoc(husb, skb) < 0)
605                                 skb_queue_head(q, skb);
606                 }
607 #endif
608
609                 /* Process ACL queue */
610                 q = __transmit_q(husb, HCI_ACLDATA_PKT);
611                 while (atomic_read(__pending_tx(husb, HCI_ACLDATA_PKT)) < HCI_MAX_BULK_TX &&
612                                 (skb = skb_dequeue(q))) {
613                         if (hci_usb_send_bulk(husb, skb) < 0) {
614                                 skb_queue_head(q, skb);
615                                 break;
616                         }
617                 }
618         } while(test_bit(HCI_USB_TX_WAKEUP, &husb->state));
619 }
620
621 static inline void hci_usb_tx_wakeup(struct hci_usb *husb)
622 {
623         /* Serialize TX queue processing to avoid data reordering */
624         if (!test_and_set_bit(HCI_USB_TX_PROCESS, &husb->state)) {
625                 hci_usb_tx_process(husb);
626                 clear_bit(HCI_USB_TX_PROCESS, &husb->state);
627         } else
628                 set_bit(HCI_USB_TX_WAKEUP, &husb->state);
629 }
630
631 /* Send frames from HCI layer */
632 static int hci_usb_send_frame(struct sk_buff *skb)
633 {
634         struct hci_dev *hdev = (struct hci_dev *) skb->dev;
635         struct hci_usb *husb;
636
637         if (!hdev) {
638                 BT_ERR("frame for uknown device (hdev=NULL)");
639                 return -ENODEV;
640         }
641
642         if (!test_bit(HCI_RUNNING, &hdev->flags))
643                 return -EBUSY;
644
645         BT_DBG("%s type %d len %d", hdev->name, bt_cb(skb)->pkt_type, skb->len);
646
647         husb = (struct hci_usb *) hdev->driver_data;
648
649         switch (bt_cb(skb)->pkt_type) {
650         case HCI_COMMAND_PKT:
651                 hdev->stat.cmd_tx++;
652                 break;
653
654         case HCI_ACLDATA_PKT:
655                 hdev->stat.acl_tx++;
656                 break;
657
658 #ifdef CONFIG_BT_HCIUSB_SCO
659         case HCI_SCODATA_PKT:
660                 hdev->stat.sco_tx++;
661                 break;
662 #endif
663
664         default:
665                 kfree_skb(skb);
666                 return 0;
667         }
668
669         read_lock(&husb->completion_lock);
670
671         skb_queue_tail(__transmit_q(husb, bt_cb(skb)->pkt_type), skb);
672         hci_usb_tx_wakeup(husb);
673
674         read_unlock(&husb->completion_lock);
675         return 0;
676 }
677
678 static void hci_usb_rx_complete(struct urb *urb)
679 {
680         struct _urb *_urb = container_of(urb, struct _urb, urb);
681         struct hci_usb *husb = (void *) urb->context;
682         struct hci_dev *hdev = husb->hdev;
683         int err, count = urb->actual_length;
684
685         BT_DBG("%s urb %p type %d status %d count %d flags %x", hdev->name, urb,
686                         _urb->type, urb->status, count, urb->transfer_flags);
687
688         read_lock(&husb->completion_lock);
689
690         if (!test_bit(HCI_RUNNING, &hdev->flags))
691                 goto unlock;
692
693         if (urb->status || !count)
694                 goto resubmit;
695
696         if (_urb->type == HCI_SCODATA_PKT) {
697 #ifdef CONFIG_BT_HCIUSB_SCO
698                 int i;
699                 for (i=0; i < urb->number_of_packets; i++) {
700                         BT_DBG("desc %d status %d offset %d len %d", i,
701                                         urb->iso_frame_desc[i].status,
702                                         urb->iso_frame_desc[i].offset,
703                                         urb->iso_frame_desc[i].actual_length);
704         
705                         if (!urb->iso_frame_desc[i].status) {
706                                 husb->hdev->stat.byte_rx += urb->iso_frame_desc[i].actual_length;
707                                 hci_recv_fragment(husb->hdev, _urb->type, 
708                                         urb->transfer_buffer + urb->iso_frame_desc[i].offset,
709                                         urb->iso_frame_desc[i].actual_length);
710                         }
711                 }
712 #else
713                 ;
714 #endif
715         } else {
716                 husb->hdev->stat.byte_rx += count;
717                 err = hci_recv_fragment(husb->hdev, _urb->type, urb->transfer_buffer, count);
718                 if (err < 0) { 
719                         BT_ERR("%s corrupted packet: type %d count %d",
720                                         husb->hdev->name, _urb->type, count);
721                         hdev->stat.err_rx++;
722                 }
723         }
724
725 resubmit:
726         urb->dev = husb->udev;
727         err = usb_submit_urb(urb, GFP_ATOMIC);
728         BT_DBG("%s urb %p type %d resubmit status %d", hdev->name, urb,
729                         _urb->type, err);
730
731 unlock:
732         read_unlock(&husb->completion_lock);
733 }
734
735 static void hci_usb_tx_complete(struct urb *urb)
736 {
737         struct _urb *_urb = container_of(urb, struct _urb, urb);
738         struct hci_usb *husb = (void *) urb->context;
739         struct hci_dev *hdev = husb->hdev;
740
741         BT_DBG("%s urb %p status %d flags %x", hdev->name, urb,
742                         urb->status, urb->transfer_flags);
743
744         atomic_dec(__pending_tx(husb, _urb->type));
745
746         urb->transfer_buffer = NULL;
747         kfree_skb((struct sk_buff *) _urb->priv);
748
749         if (!test_bit(HCI_RUNNING, &hdev->flags))
750                 return;
751
752         if (!urb->status)
753                 hdev->stat.byte_tx += urb->transfer_buffer_length;
754         else
755                 hdev->stat.err_tx++;
756
757         read_lock(&husb->completion_lock);
758
759         _urb_unlink(_urb);
760         _urb_queue_tail(__completed_q(husb, _urb->type), _urb);
761
762         hci_usb_tx_wakeup(husb);
763
764         read_unlock(&husb->completion_lock);
765 }
766
767 static void hci_usb_destruct(struct hci_dev *hdev)
768 {
769         struct hci_usb *husb = (struct hci_usb *) hdev->driver_data;
770
771         BT_DBG("%s", hdev->name);
772
773         kfree(husb);
774 }
775
776 static void hci_usb_notify(struct hci_dev *hdev, unsigned int evt)
777 {
778         BT_DBG("%s evt %d", hdev->name, evt);
779 }
780
781 static int hci_usb_probe(struct usb_interface *intf, const struct usb_device_id *id)
782 {
783         struct usb_device *udev = interface_to_usbdev(intf);
784         struct usb_host_endpoint *bulk_out_ep = NULL;
785         struct usb_host_endpoint *bulk_in_ep = NULL;
786         struct usb_host_endpoint *intr_in_ep = NULL;
787         struct usb_host_endpoint  *ep;
788         struct usb_host_interface *uif;
789         struct usb_interface *isoc_iface;
790         struct hci_usb *husb;
791         struct hci_dev *hdev;
792         int i, e, size, isoc_ifnum, isoc_alts;
793
794         BT_DBG("udev %p intf %p", udev, intf);
795
796         if (!id->driver_info) {
797                 const struct usb_device_id *match;
798                 match = usb_match_id(intf, blacklist_ids);
799                 if (match)
800                         id = match;
801         }
802
803         if (id->driver_info & HCI_IGNORE)
804                 return -ENODEV;
805
806         if (ignore_dga && id->driver_info & HCI_DIGIANSWER)
807                 return -ENODEV;
808
809         if (ignore_csr && id->driver_info & HCI_CSR)
810                 return -ENODEV;
811
812         if (ignore_sniffer && id->driver_info & HCI_SNIFFER)
813                 return -ENODEV;
814
815         if (intf->cur_altsetting->desc.bInterfaceNumber > 0)
816                 return -ENODEV;
817
818         /* Find endpoints that we need */
819         uif = intf->cur_altsetting;
820         for (e = 0; e < uif->desc.bNumEndpoints; e++) {
821                 ep = &uif->endpoint[e];
822
823                 switch (ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
824                 case USB_ENDPOINT_XFER_INT:
825                         if (ep->desc.bEndpointAddress & USB_DIR_IN)
826                                 intr_in_ep = ep;
827                         break;
828
829                 case USB_ENDPOINT_XFER_BULK:
830                         if (ep->desc.bEndpointAddress & USB_DIR_IN)
831                                 bulk_in_ep  = ep;
832                         else
833                                 bulk_out_ep = ep;
834                         break;
835                 }
836         }
837
838         if (!bulk_in_ep || !bulk_out_ep || !intr_in_ep) {
839                 BT_DBG("Bulk endpoints not found");
840                 goto done;
841         }
842
843         if (!(husb = kzalloc(sizeof(struct hci_usb), GFP_KERNEL))) {
844                 BT_ERR("Can't allocate: control structure");
845                 goto done;
846         }
847
848         husb->udev = udev;
849         husb->bulk_out_ep = bulk_out_ep;
850         husb->bulk_in_ep  = bulk_in_ep;
851         husb->intr_in_ep  = intr_in_ep;
852
853         if (id->driver_info & HCI_DIGIANSWER)
854                 husb->ctrl_req = USB_TYPE_VENDOR;
855         else
856                 husb->ctrl_req = USB_TYPE_CLASS;
857
858         /* Find isochronous endpoints that we can use */
859         size = 0; 
860         isoc_iface = NULL;
861         isoc_alts  = 0;
862         isoc_ifnum = 1;
863
864 #ifdef CONFIG_BT_HCIUSB_SCO
865         if (isoc && !(id->driver_info & (HCI_BROKEN_ISOC | HCI_SNIFFER)))
866                 isoc_iface = usb_ifnum_to_if(udev, isoc_ifnum);
867
868         if (isoc_iface) {
869                 int a;
870                 struct usb_host_endpoint *isoc_out_ep = NULL;
871                 struct usb_host_endpoint *isoc_in_ep = NULL;
872
873                 for (a = 0; a < isoc_iface->num_altsetting; a++) {
874                         uif = &isoc_iface->altsetting[a];
875                         for (e = 0; e < uif->desc.bNumEndpoints; e++) {
876                                 ep = &uif->endpoint[e];
877
878                                 switch (ep->desc.bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
879                                 case USB_ENDPOINT_XFER_ISOC:
880                                         if (le16_to_cpu(ep->desc.wMaxPacketSize) < size ||
881                                                         uif->desc.bAlternateSetting != isoc)
882                                                 break;
883                                         size = le16_to_cpu(ep->desc.wMaxPacketSize);
884
885                                         isoc_alts = uif->desc.bAlternateSetting;
886
887                                         if (ep->desc.bEndpointAddress & USB_DIR_IN)
888                                                 isoc_in_ep  = ep;
889                                         else
890                                                 isoc_out_ep = ep;
891                                         break;
892                                 }
893                         }
894                 }
895
896                 if (!isoc_in_ep || !isoc_out_ep)
897                         BT_DBG("Isoc endpoints not found");
898                 else {
899                         BT_DBG("isoc ifnum %d alts %d", isoc_ifnum, isoc_alts);
900                         if (usb_driver_claim_interface(&hci_usb_driver, isoc_iface, husb) != 0)
901                                 BT_ERR("Can't claim isoc interface");
902                         else if (usb_set_interface(udev, isoc_ifnum, isoc_alts)) {
903                                 BT_ERR("Can't set isoc interface settings");
904                                 husb->isoc_iface = isoc_iface;
905                                 usb_driver_release_interface(&hci_usb_driver, isoc_iface);
906                                 husb->isoc_iface = NULL;
907                         } else {
908                                 husb->isoc_iface  = isoc_iface;
909                                 husb->isoc_in_ep  = isoc_in_ep;
910                                 husb->isoc_out_ep = isoc_out_ep;
911                         }
912                 }
913         }
914 #endif
915
916         rwlock_init(&husb->completion_lock);
917
918         for (i = 0; i < 4; i++) {
919                 skb_queue_head_init(&husb->transmit_q[i]);
920                 _urb_queue_init(&husb->pending_q[i]);
921                 _urb_queue_init(&husb->completed_q[i]);
922         }
923
924         /* Initialize and register HCI device */
925         hdev = hci_alloc_dev();
926         if (!hdev) {
927                 BT_ERR("Can't allocate HCI device");
928                 goto probe_error;
929         }
930
931         husb->hdev = hdev;
932
933         hdev->type = HCI_USB;
934         hdev->driver_data = husb;
935         SET_HCIDEV_DEV(hdev, &intf->dev);
936
937         hdev->open     = hci_usb_open;
938         hdev->close    = hci_usb_close;
939         hdev->flush    = hci_usb_flush;
940         hdev->send     = hci_usb_send_frame;
941         hdev->destruct = hci_usb_destruct;
942         hdev->notify   = hci_usb_notify;
943
944         hdev->owner = THIS_MODULE;
945
946         if (reset || id->driver_info & HCI_RESET)
947                 set_bit(HCI_QUIRK_RESET_ON_INIT, &hdev->quirks);
948
949         if (force_scofix || id->driver_info & HCI_WRONG_SCO_MTU) {
950                 if (!disable_scofix)
951                         set_bit(HCI_QUIRK_FIXUP_BUFFER_SIZE, &hdev->quirks);
952         }
953
954         if (id->driver_info & HCI_SNIFFER) {
955                 if (le16_to_cpu(udev->descriptor.bcdDevice) > 0x997)
956                         set_bit(HCI_QUIRK_RAW_DEVICE, &hdev->quirks);
957         }
958
959         if (id->driver_info & HCI_BCM92035) {
960                 unsigned char cmd[] = { 0x3b, 0xfc, 0x01, 0x00 };
961                 struct sk_buff *skb;
962
963                 skb = bt_skb_alloc(sizeof(cmd), GFP_KERNEL);
964                 if (skb) {
965                         memcpy(skb_put(skb, sizeof(cmd)), cmd, sizeof(cmd));
966                         skb_queue_tail(&hdev->driver_init, skb);
967                 }
968         }
969
970         if (hci_register_dev(hdev) < 0) {
971                 BT_ERR("Can't register HCI device");
972                 hci_free_dev(hdev);
973                 goto probe_error;
974         }
975
976         usb_set_intfdata(intf, husb);
977         return 0;
978
979 probe_error:
980         if (husb->isoc_iface)
981                 usb_driver_release_interface(&hci_usb_driver, husb->isoc_iface);
982         kfree(husb);
983
984 done:
985         return -EIO;
986 }
987
988 static void hci_usb_disconnect(struct usb_interface *intf)
989 {
990         struct hci_usb *husb = usb_get_intfdata(intf);
991         struct hci_dev *hdev;
992
993         if (!husb || intf == husb->isoc_iface)
994                 return;
995
996         usb_set_intfdata(intf, NULL);
997         hdev = husb->hdev;
998
999         BT_DBG("%s", hdev->name);
1000
1001         hci_usb_close(hdev);
1002
1003         if (husb->isoc_iface)
1004                 usb_driver_release_interface(&hci_usb_driver, husb->isoc_iface);
1005
1006         if (hci_unregister_dev(hdev) < 0)
1007                 BT_ERR("Can't unregister HCI device %s", hdev->name);
1008
1009         hci_free_dev(hdev);
1010 }
1011
1012 static int hci_usb_suspend(struct usb_interface *intf, pm_message_t message)
1013 {
1014         struct hci_usb *husb = usb_get_intfdata(intf);
1015         struct list_head killed;
1016         unsigned long flags;
1017         int i;
1018
1019         if (!husb || intf == husb->isoc_iface)
1020                 return 0;
1021
1022         hci_suspend_dev(husb->hdev);
1023
1024         INIT_LIST_HEAD(&killed);
1025
1026         for (i = 0; i < 4; i++) {
1027                 struct _urb_queue *q = &husb->pending_q[i];
1028                 struct _urb *_urb, *_tmp;
1029
1030                 while ((_urb = _urb_dequeue(q))) {
1031                         /* reset queue since _urb_dequeue sets it to NULL */
1032                         _urb->queue = q;
1033                         usb_kill_urb(&_urb->urb);
1034                         list_add(&_urb->list, &killed);
1035                 }
1036
1037                 spin_lock_irqsave(&q->lock, flags);
1038
1039                 list_for_each_entry_safe(_urb, _tmp, &killed, list) {
1040                         list_move_tail(&_urb->list, &q->head);
1041                 }
1042
1043                 spin_unlock_irqrestore(&q->lock, flags);
1044         }
1045
1046         return 0;
1047 }
1048
1049 static int hci_usb_resume(struct usb_interface *intf)
1050 {
1051         struct hci_usb *husb = usb_get_intfdata(intf);
1052         unsigned long flags;
1053         int i, err = 0;
1054
1055         if (!husb || intf == husb->isoc_iface)
1056                 return 0;
1057         
1058         for (i = 0; i < 4; i++) {
1059                 struct _urb_queue *q = &husb->pending_q[i];
1060                 struct _urb *_urb;
1061
1062                 spin_lock_irqsave(&q->lock, flags);
1063
1064                 list_for_each_entry(_urb, &q->head, list) {
1065                         err = usb_submit_urb(&_urb->urb, GFP_ATOMIC);
1066                         if (err)
1067                                 break;
1068                 }
1069
1070                 spin_unlock_irqrestore(&q->lock, flags);
1071
1072                 if (err)
1073                         return -EIO;
1074         }
1075
1076         hci_resume_dev(husb->hdev);
1077
1078         return 0;
1079 }
1080
1081 static struct usb_driver hci_usb_driver = {
1082         .name           = "hci_usb",
1083         .probe          = hci_usb_probe,
1084         .disconnect     = hci_usb_disconnect,
1085         .suspend        = hci_usb_suspend,
1086         .resume         = hci_usb_resume,
1087         .id_table       = bluetooth_ids,
1088 };
1089
1090 static int __init hci_usb_init(void)
1091 {
1092         int err;
1093
1094         BT_INFO("HCI USB driver ver %s", VERSION);
1095
1096         if ((err = usb_register(&hci_usb_driver)) < 0)
1097                 BT_ERR("Failed to register HCI USB driver");
1098
1099         return err;
1100 }
1101
1102 static void __exit hci_usb_exit(void)
1103 {
1104         usb_deregister(&hci_usb_driver);
1105 }
1106
1107 module_init(hci_usb_init);
1108 module_exit(hci_usb_exit);
1109
1110 module_param(ignore_dga, bool, 0644);
1111 MODULE_PARM_DESC(ignore_dga, "Ignore devices with id 08fd:0001");
1112
1113 module_param(ignore_csr, bool, 0644);
1114 MODULE_PARM_DESC(ignore_csr, "Ignore devices with id 0a12:0001");
1115
1116 module_param(ignore_sniffer, bool, 0644);
1117 MODULE_PARM_DESC(ignore_sniffer, "Ignore devices with id 0a12:0002");
1118
1119 module_param(disable_scofix, bool, 0644);
1120 MODULE_PARM_DESC(disable_scofix, "Disable fixup of wrong SCO buffer size");
1121
1122 module_param(force_scofix, bool, 0644);
1123 MODULE_PARM_DESC(force_scofix, "Force fixup of wrong SCO buffers size");
1124
1125 module_param(reset, bool, 0644);
1126 MODULE_PARM_DESC(reset, "Send HCI reset command on initialization");
1127
1128 #ifdef CONFIG_BT_HCIUSB_SCO
1129 module_param(isoc, int, 0644);
1130 MODULE_PARM_DESC(isoc, "Set isochronous transfers for SCO over HCI support");
1131 #endif
1132
1133 MODULE_AUTHOR("Maxim Krasnyansky <maxk@qualcomm.com>, Marcel Holtmann <marcel@holtmann.org>");
1134 MODULE_DESCRIPTION("Bluetooth HCI USB driver ver " VERSION);
1135 MODULE_VERSION(VERSION);
1136 MODULE_LICENSE("GPL");