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