cleanup
[linux-2.4.21-pre4.git] / drivers / bluetooth / hci_usb.c
1 /* 
2    BlueZ - Bluetooth protocol stack for Linux
3    Copyright (C) 2000-2001 Qualcomm Incorporated
4
5    Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License version 2 as
9    published by the Free Software Foundation;
10
11    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
12    OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
14    IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
15    CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES 
16    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 
17    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 
18    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19
20    ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, 
21    COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS 
22    SOFTWARE IS DISCLAIMED.
23 */
24
25 /*
26  * BlueZ HCI USB driver.
27  * Based on original USB Bluetooth driver for Linux kernel
28  *    Copyright (c) 2000 Greg Kroah-Hartman        <greg@kroah.com>
29  *    Copyright (c) 2000 Mark Douglas Corner       <mcorner@umich.edu>
30  *
31  * $Id: hci_usb.c,v 1.1.1.1 2005/04/11 02:50:17 jack Exp $    
32  */
33 #define VERSION "2.1"
34
35 #include <linux/config.h>
36 #include <linux/module.h>
37
38 #define __KERNEL_SYSCALLS__
39
40 #include <linux/version.h>
41 #include <linux/kernel.h>
42 #include <linux/init.h>
43 #include <linux/sched.h>
44 #include <linux/unistd.h>
45 #include <linux/types.h>
46 #include <linux/interrupt.h>
47
48 #include <linux/slab.h>
49 #include <linux/errno.h>
50 #include <linux/string.h>
51 #include <linux/skbuff.h>
52 #include <linux/kmod.h>
53
54 #include <linux/usb.h>
55
56 #include <net/bluetooth/bluetooth.h>
57 #include <net/bluetooth/hci_core.h>
58 #include "hci_usb.h"
59
60 #define HCI_MAX_PENDING (HCI_MAX_BULK_RX + HCI_MAX_BULK_TX + 1)
61
62 #ifndef HCI_USB_DEBUG
63 #undef  BT_DBG
64 #define BT_DBG( A... )
65 #undef  BT_DMP
66 #define BT_DMP( A... )
67 #endif
68
69 #ifndef CONFIG_BLUEZ_USB_ZERO_PACKET
70 #undef  USB_ZERO_PACKET
71 #define USB_ZERO_PACKET 0
72 #endif
73
74 static struct usb_driver hci_usb_driver; 
75
76 static struct usb_device_id bluetooth_ids[] = {
77         /* Generic Bluetooth USB device */
78         { USB_DEVICE_INFO(HCI_DEV_CLASS, HCI_DEV_SUBCLASS, HCI_DEV_PROTOCOL) },
79
80         /* Ericsson with non-standard id */
81         { USB_DEVICE(0x0bdb, 0x1002) },
82
83         { }     /* Terminating entry */
84 };
85
86 MODULE_DEVICE_TABLE (usb, bluetooth_ids);
87
88 static struct usb_device_id ignore_ids[] = {
89         /* Broadcom BCM2033 without firmware */
90         { USB_DEVICE(0x0a5c, 0x2033) },
91
92         { }     /* Terminating entry */
93 };
94
95 static void hci_usb_interrupt(struct urb *urb);
96 static void hci_usb_rx_complete(struct urb *urb);
97 static void hci_usb_tx_complete(struct urb *urb);
98
99 static struct urb *hci_usb_get_completed(struct hci_usb *husb)
100 {
101         struct sk_buff *skb;
102         struct urb *urb = NULL;
103
104         skb = skb_dequeue(&husb->completed_q);
105         if (skb) {
106                 urb = ((struct hci_usb_scb *) skb->cb)->urb;
107                 kfree_skb(skb);
108         }
109
110         BT_DBG("%s urb %p", husb->hdev.name, urb);
111         return urb;
112 }
113
114 static int hci_usb_enable_intr(struct hci_usb *husb)
115 {
116         struct urb *urb;
117         int pipe, size;
118         void *buf;
119
120         BT_DBG("%s", husb->hdev.name);
121
122         if (!(urb = usb_alloc_urb(0)))
123                 return -ENOMEM;
124
125         if (!(buf = kmalloc(HCI_MAX_EVENT_SIZE, GFP_KERNEL))) {
126                 usb_free_urb(urb);
127                 return -ENOMEM;
128         }
129
130         husb->intr_urb = urb;
131         
132         pipe = usb_rcvintpipe(husb->udev, husb->intr_ep);
133         size = usb_maxpacket(husb->udev, pipe, usb_pipeout(pipe));
134         FILL_INT_URB(urb, husb->udev, pipe, buf, size, 
135                         hci_usb_interrupt, husb, husb->intr_interval);
136         
137         return usb_submit_urb(urb);
138 }
139
140 static int hci_usb_disable_intr(struct hci_usb *husb)
141 {
142         struct urb *urb = husb->intr_urb;
143         struct sk_buff *skb;
144
145         BT_DBG("%s", husb->hdev.name);
146
147         usb_unlink_urb(urb); usb_free_urb(urb);
148         husb->intr_urb = NULL;
149
150         skb = husb->intr_skb;
151         if (skb) {
152                 husb->intr_skb = NULL;
153                 kfree_skb(skb);
154         }
155
156         return 0;
157 }
158
159 static int hci_usb_rx_submit(struct hci_usb *husb, struct urb *urb)
160 {
161         struct hci_usb_scb *scb;
162         struct sk_buff *skb;
163         int    pipe, size, err;
164
165         if (!urb && !(urb = usb_alloc_urb(0)))
166                 return -ENOMEM;
167
168         size = HCI_MAX_FRAME_SIZE;
169
170         if (!(skb = bluez_skb_alloc(size, GFP_ATOMIC))) {
171                 usb_free_urb(urb);
172                 return -ENOMEM;
173         }
174         
175         BT_DBG("%s urb %p", husb->hdev.name, urb);
176
177         skb->dev = (void *) &husb->hdev;
178         skb->pkt_type = HCI_ACLDATA_PKT;
179
180         scb = (struct hci_usb_scb *) skb->cb;
181         scb->urb = urb;
182
183         pipe = usb_rcvbulkpipe(husb->udev, husb->bulk_in_ep);
184
185         FILL_BULK_URB(urb, husb->udev, pipe, skb->data, size, hci_usb_rx_complete, skb);
186         urb->transfer_flags = USB_QUEUE_BULK;
187
188         skb_queue_tail(&husb->pending_q, skb);
189         err = usb_submit_urb(urb);
190         if (err) {
191                 BT_ERR("%s bulk rx submit failed urb %p err %d",
192                                 husb->hdev.name, urb, err);
193                 skb_unlink(skb);
194                 usb_free_urb(urb);
195         }
196         return err;
197 }
198
199 /* Initialize device */
200 static int hci_usb_open(struct hci_dev *hdev)
201 {
202         struct hci_usb *husb = (struct hci_usb *) hdev->driver_data;
203         int i, err;
204         unsigned long flags;
205
206         BT_DBG("%s", hdev->name);
207
208         if (test_and_set_bit(HCI_RUNNING, &hdev->flags))
209                 return 0;
210
211         MOD_INC_USE_COUNT;
212
213         write_lock_irqsave(&husb->completion_lock, flags);
214
215         err = hci_usb_enable_intr(husb);
216         if (!err) {
217                 for (i = 0; i < HCI_MAX_BULK_RX; i++)
218                         hci_usb_rx_submit(husb, NULL);
219         } else {
220                 clear_bit(HCI_RUNNING, &hdev->flags);
221                 MOD_DEC_USE_COUNT;
222         }
223
224         write_unlock_irqrestore(&husb->completion_lock, flags);
225         return err;
226 }
227
228 /* Reset device */
229 static int hci_usb_flush(struct hci_dev *hdev)
230 {
231         struct hci_usb *husb = (struct hci_usb *) hdev->driver_data;
232
233         BT_DBG("%s", hdev->name);
234
235         skb_queue_purge(&husb->cmd_q);
236         skb_queue_purge(&husb->acl_q);
237         return 0;
238 }
239
240 static inline void hci_usb_unlink_urbs(struct hci_usb *husb)
241 {
242         struct sk_buff *skb;
243         struct urb *urb;
244
245         BT_DBG("%s", husb->hdev.name);
246
247         while ((skb = skb_dequeue(&husb->pending_q))) {
248                 urb = ((struct hci_usb_scb *) skb->cb)->urb;
249                 usb_unlink_urb(urb);
250                 kfree_skb(skb);
251         }
252
253         while ((urb = hci_usb_get_completed(husb)))
254                 usb_free_urb(urb);
255 }
256
257 /* Close device */
258 static int hci_usb_close(struct hci_dev *hdev)
259 {
260         struct hci_usb *husb = (struct hci_usb *) hdev->driver_data;
261         unsigned long flags;
262         
263         if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
264                 return 0;
265
266         BT_DBG("%s", hdev->name);
267
268         write_lock_irqsave(&husb->completion_lock, flags);
269         
270         hci_usb_disable_intr(husb);
271         hci_usb_unlink_urbs(husb);
272         hci_usb_flush(hdev);
273
274         write_unlock_irqrestore(&husb->completion_lock, flags);
275
276         MOD_DEC_USE_COUNT;
277         return 0;
278 }
279
280 static inline int hci_usb_send_ctrl(struct hci_usb *husb, struct sk_buff *skb)
281 {
282         struct hci_usb_scb *scb = (void *) skb->cb;
283         struct urb *urb = hci_usb_get_completed(husb);
284         struct usb_ctrlrequest *dr;
285         int pipe, err;
286
287         if (!urb && !(urb = usb_alloc_urb(0)))
288                 return -ENOMEM;
289
290         if (!(dr = kmalloc(sizeof(*dr), GFP_ATOMIC))) {
291                 usb_free_urb(urb);
292                 return -ENOMEM;
293         }
294         
295         pipe = usb_sndctrlpipe(husb->udev, 0);
296
297         dr->bRequestType = HCI_CTRL_REQ;
298         dr->bRequest = 0;
299         dr->wIndex   = 0;
300         dr->wValue   = 0;
301         dr->wLength  = __cpu_to_le16(skb->len);
302
303         FILL_CONTROL_URB(urb, husb->udev, pipe, (void *) dr,
304                         skb->data, skb->len, hci_usb_tx_complete, skb);
305
306         BT_DBG("%s urb %p len %d", husb->hdev.name, urb, skb->len);
307
308         scb->urb = urb;
309
310         skb_queue_tail(&husb->pending_q, skb);
311         err = usb_submit_urb(urb);
312         if (err) {
313                 BT_ERR("%s ctrl tx submit failed urb %p err %d", 
314                                 husb->hdev.name, urb, err);
315                 skb_unlink(skb);
316                 usb_free_urb(urb); kfree(dr);
317         }
318         return err;
319 }
320
321 static inline int hci_usb_send_bulk(struct hci_usb *husb, struct sk_buff *skb)
322 {
323         struct hci_usb_scb *scb = (void *) skb->cb;
324         struct urb *urb = hci_usb_get_completed(husb);
325         int pipe, err;
326
327         if (!urb && !(urb = usb_alloc_urb(0)))
328                 return -ENOMEM;
329
330         pipe = usb_sndbulkpipe(husb->udev, husb->bulk_out_ep);
331         
332         FILL_BULK_URB(urb, husb->udev, pipe, skb->data, skb->len,
333                       hci_usb_tx_complete, skb);
334         urb->transfer_flags = USB_QUEUE_BULK | USB_ZERO_PACKET;
335
336         BT_DBG("%s urb %p len %d", husb->hdev.name, urb, skb->len);
337
338         scb->urb = urb;
339
340         skb_queue_tail(&husb->pending_q, skb);
341         err = usb_submit_urb(urb);
342         if (err) {
343                 BT_ERR("%s bulk tx submit failed urb %p err %d", 
344                                 husb->hdev.name, urb, err);
345                 skb_unlink(skb);
346                 usb_free_urb(urb);
347         }
348         return err;
349 }
350
351 static void hci_usb_tx_process(struct hci_usb *husb)
352 {
353         struct sk_buff *skb;
354
355         BT_DBG("%s", husb->hdev.name);
356
357         do {
358                 clear_bit(HCI_USB_TX_WAKEUP, &husb->state);
359                 
360                 /* Process ACL queue */
361                 while (skb_queue_len(&husb->pending_q) < HCI_MAX_PENDING &&
362                                 (skb = skb_dequeue(&husb->acl_q))) {
363                         if (hci_usb_send_bulk(husb, skb) < 0) {
364                                 skb_queue_head(&husb->acl_q, skb);
365                                 break;
366                         }
367                 }
368
369                 /* Process command queue */
370                 if (!test_bit(HCI_USB_CTRL_TX, &husb->state) &&
371                         (skb = skb_dequeue(&husb->cmd_q)) != NULL) {
372                         set_bit(HCI_USB_CTRL_TX, &husb->state);
373                         if (hci_usb_send_ctrl(husb, skb) < 0) {
374                                 skb_queue_head(&husb->cmd_q, skb);
375                                 clear_bit(HCI_USB_CTRL_TX, &husb->state);
376                         }
377                 }
378         } while(test_bit(HCI_USB_TX_WAKEUP, &husb->state));
379 }
380
381 static inline void hci_usb_tx_wakeup(struct hci_usb *husb)
382 {
383         /* Serialize TX queue processing to avoid data reordering */
384         if (!test_and_set_bit(HCI_USB_TX_PROCESS, &husb->state)) {
385                 hci_usb_tx_process(husb);
386                 clear_bit(HCI_USB_TX_PROCESS, &husb->state);
387         } else
388                 set_bit(HCI_USB_TX_WAKEUP, &husb->state);
389 }
390
391 /* Send frames from HCI layer */
392 static int hci_usb_send_frame(struct sk_buff *skb)
393 {
394         struct hci_dev *hdev = (struct hci_dev *) skb->dev;
395         struct hci_usb *husb;
396
397         if (!hdev) {
398                 BT_ERR("frame for uknown device (hdev=NULL)");
399                 return -ENODEV;
400         }
401
402         if (!test_bit(HCI_RUNNING, &hdev->flags))
403                 return -EBUSY;
404
405         husb = (struct hci_usb *) hdev->driver_data;
406
407         BT_DBG("%s type %d len %d", hdev->name, skb->pkt_type, skb->len);
408
409         read_lock(&husb->completion_lock);
410
411         switch (skb->pkt_type) {
412         case HCI_COMMAND_PKT:
413                 skb_queue_tail(&husb->cmd_q, skb);
414                 hdev->stat.cmd_tx++;
415                 break;
416
417         case HCI_ACLDATA_PKT:
418                 skb_queue_tail(&husb->acl_q, skb);
419                 hdev->stat.acl_tx++;
420                 break;
421
422         case HCI_SCODATA_PKT:
423         default:
424                 kfree_skb(skb);
425                 break;
426         }
427         hci_usb_tx_wakeup(husb);
428
429         read_unlock(&husb->completion_lock);
430         return 0;
431 }
432
433 static void hci_usb_interrupt(struct urb *urb)
434 {
435         struct hci_usb *husb = (void *) urb->context;
436         struct hci_usb_scb *scb;
437         struct sk_buff *skb;
438         hci_event_hdr *eh;
439         __u8 *data = urb->transfer_buffer;
440         int count = urb->actual_length;
441         int len = HCI_EVENT_HDR_SIZE;
442
443         BT_DBG("%s urb %p count %d", husb->hdev.name, urb, count);
444
445         if (!test_bit(HCI_RUNNING, &husb->hdev.flags))
446                 return;
447
448         if (urb->status || !count) {
449                 BT_DBG("%s intr status %d, count %d", 
450                                 husb->hdev.name, urb->status, count);
451                 return;
452         }
453
454         read_lock(&husb->completion_lock);
455         
456         husb->hdev.stat.byte_rx += count;
457
458         if (!(skb = husb->intr_skb)) {
459                 /* Start of the frame */
460                 if (count < HCI_EVENT_HDR_SIZE)
461                         goto bad_len;
462
463                 eh  = (hci_event_hdr *) data;
464                 len = eh->plen + HCI_EVENT_HDR_SIZE;
465
466                 if (count > len)
467                         goto bad_len;
468
469                 skb = bluez_skb_alloc(len, GFP_ATOMIC);
470                 if (!skb) {
471                         BT_ERR("%s no memory for event packet", husb->hdev.name);
472                         goto done;
473                 }
474                 scb = (void *) skb->cb;
475
476                 skb->dev = (void *) &husb->hdev;
477                 skb->pkt_type = HCI_EVENT_PKT;
478
479                 husb->intr_skb = skb;
480                 scb->intr_len  = len;
481         } else {
482                 /* Continuation */
483                 scb = (void *) skb->cb;
484                 len = scb->intr_len;
485                 if (count > len) {
486                         husb->intr_skb = NULL;
487                         kfree_skb(skb);
488                         goto bad_len;
489                 }
490         }
491
492         memcpy(skb_put(skb, count), data, count);
493         scb->intr_len -= count;
494
495         if (!scb->intr_len) {
496                 /* Complete frame */
497                 husb->intr_skb = NULL;
498                 hci_recv_frame(skb);
499         }
500
501 done:
502         read_unlock(&husb->completion_lock);
503         return;
504
505 bad_len:
506         BT_ERR("%s bad frame len %d expected %d", husb->hdev.name, count, len);
507         husb->hdev.stat.err_rx++;
508         read_unlock(&husb->completion_lock);
509 }
510
511 static void hci_usb_tx_complete(struct urb *urb)
512 {
513         struct sk_buff *skb  = (struct sk_buff *) urb->context;
514         struct hci_dev *hdev = (struct hci_dev *) skb->dev;
515         struct hci_usb *husb = (struct hci_usb *) hdev->driver_data;
516
517         BT_DBG("%s urb %p status %d flags %x", husb->hdev.name, urb,
518                         urb->status, urb->transfer_flags);
519
520         if (urb->pipe == usb_sndctrlpipe(husb->udev, 0)) {
521                 kfree(urb->setup_packet);
522                 clear_bit(HCI_USB_CTRL_TX, &husb->state);
523         }
524
525         if (!test_bit(HCI_RUNNING, &hdev->flags))
526                 return;
527
528         read_lock(&husb->completion_lock);
529         
530         if (!urb->status)
531                 husb->hdev.stat.byte_tx += skb->len;
532         else
533                 husb->hdev.stat.err_tx++;
534
535         skb_unlink(skb);
536         skb_queue_tail(&husb->completed_q, skb);
537         hci_usb_tx_wakeup(husb);
538         
539         read_unlock(&husb->completion_lock);
540         return;
541 }
542
543 static void hci_usb_rx_complete(struct urb *urb)
544 {
545         struct sk_buff *skb  = (struct sk_buff *) urb->context;
546         struct hci_dev *hdev = (struct hci_dev *) skb->dev;
547         struct hci_usb *husb = (struct hci_usb *) hdev->driver_data;
548         int status, count = urb->actual_length;
549         hci_acl_hdr *ah;
550         int dlen, size;
551
552         BT_DBG("%s urb %p status %d count %d flags %x", husb->hdev.name, urb,
553                         urb->status, count, urb->transfer_flags);
554
555         if (!test_bit(HCI_RUNNING, &hdev->flags))
556                 return;
557
558         read_lock(&husb->completion_lock);
559
560         if (urb->status || !count)
561                 goto resubmit;
562
563         husb->hdev.stat.byte_rx += count;
564
565         ah   = (hci_acl_hdr *) skb->data;
566         dlen = __le16_to_cpu(ah->dlen);
567         size = HCI_ACL_HDR_SIZE + dlen;
568
569         /* Verify frame len and completeness */
570         if (count != size) {
571                 BT_ERR("%s corrupted ACL packet: count %d, dlen %d",
572                                 husb->hdev.name, count, dlen);
573                 bluez_dump("hci_usb", skb->data, count);
574                 husb->hdev.stat.err_rx++;
575                 goto resubmit;
576         }
577
578         skb_unlink(skb);
579         skb_put(skb, count);
580         hci_recv_frame(skb);
581
582         hci_usb_rx_submit(husb, urb);
583
584         read_unlock(&husb->completion_lock);
585         return;
586                 
587 resubmit:
588         urb->dev = husb->udev;
589         status   = usb_submit_urb(urb);
590         BT_DBG("%s URB resubmit status %d", husb->hdev.name, status);
591         read_unlock(&husb->completion_lock);
592 }
593
594 static void hci_usb_destruct(struct hci_dev *hdev)
595 {
596         struct hci_usb *husb;
597
598         if (!hdev) return;
599
600         BT_DBG("%s", hdev->name);
601
602         husb = (struct hci_usb *) hdev->driver_data;
603         kfree(husb);
604 }
605
606 static void *hci_usb_probe(struct usb_device *udev, unsigned int ifnum, const struct usb_device_id *id)
607 {
608         struct usb_endpoint_descriptor *bulk_out_ep[HCI_MAX_IFACE_NUM];
609         struct usb_endpoint_descriptor *isoc_out_ep[HCI_MAX_IFACE_NUM];
610         struct usb_endpoint_descriptor *bulk_in_ep[HCI_MAX_IFACE_NUM];
611         struct usb_endpoint_descriptor *isoc_in_ep[HCI_MAX_IFACE_NUM];
612         struct usb_endpoint_descriptor *intr_in_ep[HCI_MAX_IFACE_NUM];
613         struct usb_interface_descriptor *uif;
614         struct usb_endpoint_descriptor *ep;
615         struct usb_interface *iface, *isoc_iface;
616         struct hci_usb *husb;
617         struct hci_dev *hdev;
618         int i, a, e, size, ifn, isoc_ifnum, isoc_alts;
619
620         BT_DBG("udev %p ifnum %d", udev, ifnum);
621
622         iface = &udev->actconfig->interface[0];
623
624         /* Check our black list */
625         if (usb_match_id(udev, iface, ignore_ids))
626                 return NULL;
627
628         /* Check number of endpoints */
629         if (udev->actconfig->interface[ifnum].altsetting[0].bNumEndpoints < 3)
630                 return NULL;
631
632         memset(bulk_out_ep, 0, sizeof(bulk_out_ep));
633         memset(isoc_out_ep, 0, sizeof(isoc_out_ep));
634         memset(bulk_in_ep,  0, sizeof(bulk_in_ep));
635         memset(isoc_in_ep,  0, sizeof(isoc_in_ep));
636         memset(intr_in_ep,  0, sizeof(intr_in_ep));
637
638         size = 0; 
639         isoc_iface = NULL;
640         isoc_alts  = isoc_ifnum = 0;
641         
642         /* Find endpoints that we need */
643
644         ifn = MIN(udev->actconfig->bNumInterfaces, HCI_MAX_IFACE_NUM);
645         for (i = 0; i < ifn; i++) {
646                 iface = &udev->actconfig->interface[i];
647                 for (a = 0; a < iface->num_altsetting; a++) {
648                         uif = &iface->altsetting[a];
649                         for (e = 0; e < uif->bNumEndpoints; e++) {
650                                 ep = &uif->endpoint[e];
651
652                                 switch (ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
653                                 case USB_ENDPOINT_XFER_INT:
654                                         if (ep->bEndpointAddress & USB_DIR_IN)
655                                                 intr_in_ep[i] = ep;
656                                         break;
657
658                                 case USB_ENDPOINT_XFER_BULK:
659                                         if (ep->bEndpointAddress & USB_DIR_IN)
660                                                 bulk_in_ep[i]  = ep;
661                                         else
662                                                 bulk_out_ep[i] = ep;
663                                         break;
664
665                                 case USB_ENDPOINT_XFER_ISOC:
666                                         if (ep->wMaxPacketSize < size)
667                                                 break;
668                                         size = ep->wMaxPacketSize;
669
670                                         isoc_iface = iface;
671                                         isoc_alts  = a;
672                                         isoc_ifnum = i;
673
674                                         if (ep->bEndpointAddress & USB_DIR_IN)
675                                                 isoc_in_ep[i]  = ep;
676                                         else
677                                                 isoc_out_ep[i] = ep;
678                                         break;
679                                 }
680                         }
681                 }
682         }
683
684         if (!bulk_in_ep[0] || !bulk_out_ep[0] || !intr_in_ep[0]) {
685                 BT_DBG("Bulk endpoints not found");
686                 goto done;
687         }
688
689         if (!isoc_in_ep[1] || !isoc_out_ep[1]) {
690                 BT_DBG("Isoc endpoints not found");
691                 isoc_iface = NULL;
692         }
693
694         if (!(husb = kmalloc(sizeof(struct hci_usb), GFP_KERNEL))) {
695                 BT_ERR("Can't allocate: control structure");
696                 goto done;
697         }
698
699         memset(husb, 0, sizeof(struct hci_usb));
700
701         husb->udev = udev;
702         husb->bulk_out_ep = bulk_out_ep[0]->bEndpointAddress;
703         husb->bulk_in_ep  = bulk_in_ep[0]->bEndpointAddress;
704
705         husb->intr_ep = intr_in_ep[0]->bEndpointAddress;
706         husb->intr_interval = intr_in_ep[0]->bInterval;
707
708         if (isoc_iface) {
709                 if (usb_set_interface(udev, isoc_ifnum, isoc_alts)) {
710                         BT_ERR("Can't set isoc interface settings");
711                         isoc_iface = NULL;
712                 }
713                 usb_driver_claim_interface(&hci_usb_driver, isoc_iface, husb);
714                 husb->isoc_iface  = isoc_iface;
715
716                 husb->isoc_in_ep  = isoc_in_ep[1]->bEndpointAddress;
717                 husb->isoc_out_ep = isoc_in_ep[1]->bEndpointAddress;
718         }
719
720         husb->completion_lock = RW_LOCK_UNLOCKED;
721         
722         skb_queue_head_init(&husb->acl_q);
723         skb_queue_head_init(&husb->cmd_q);
724         skb_queue_head_init(&husb->pending_q);
725         skb_queue_head_init(&husb->completed_q);
726
727         /* Initialize and register HCI device */
728         hdev = &husb->hdev;
729
730         hdev->type = HCI_USB;
731         hdev->driver_data = husb;
732
733         hdev->open  = hci_usb_open;
734         hdev->close = hci_usb_close;
735         hdev->flush = hci_usb_flush;
736         hdev->send  = hci_usb_send_frame;
737         hdev->destruct = hci_usb_destruct;
738
739         if (hci_register_dev(hdev) < 0) {
740                 BT_ERR("Can't register HCI device");
741                 goto probe_error;
742         }
743
744         return husb;
745
746 probe_error:
747         kfree(husb);
748
749 done:
750         return NULL;
751 }
752
753 static void hci_usb_disconnect(struct usb_device *udev, void *ptr)
754 {
755         struct hci_usb *husb = (struct hci_usb *) ptr;
756         struct hci_dev *hdev = &husb->hdev;
757
758         if (!husb)
759                 return;
760
761         BT_DBG("%s", hdev->name);
762
763         hci_usb_close(hdev);
764
765         if (husb->isoc_iface)
766                 usb_driver_release_interface(&hci_usb_driver, husb->isoc_iface);
767
768         if (hci_unregister_dev(hdev) < 0)
769                 BT_ERR("Can't unregister HCI device %s", hdev->name);
770 }
771
772 static struct usb_driver hci_usb_driver = {
773         name:           "hci_usb",
774         probe:          hci_usb_probe,
775         disconnect:     hci_usb_disconnect,
776         id_table:       bluetooth_ids,
777 };
778
779 int hci_usb_init(void)
780 {
781         int err;
782
783         BT_INFO("BlueZ HCI USB driver ver %s Copyright (C) 2000,2001 Qualcomm Inc",  
784                 VERSION);
785         BT_INFO("Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>");
786
787         if ((err = usb_register(&hci_usb_driver)) < 0)
788                 BT_ERR("Failed to register HCI USB driver");
789
790         return err;
791 }
792
793 void hci_usb_cleanup(void)
794 {
795         usb_deregister(&hci_usb_driver);
796 }
797
798 module_init(hci_usb_init);
799 module_exit(hci_usb_cleanup);
800
801 MODULE_AUTHOR("Maxim Krasnyansky <maxk@qualcomm.com>");
802 MODULE_DESCRIPTION("BlueZ HCI USB driver ver " VERSION);
803 MODULE_LICENSE("GPL");