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