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