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