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