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