File: /usr/src/linux/drivers/bluetooth/hci_usb.c
1 /*
2 BlueZ - Bluetooth protocol stack for Linux
3 Copyright (C) 2000-2001 Qualcomm Incorporated
4
5 Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License version 2 as
9 published by the Free Software Foundation;
10
11 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
12 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
14 IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
15 CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
16 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19
20 ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
21 COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
22 SOFTWARE IS DISCLAIMED.
23 */
24
25 /*
26 * BlueZ HCI USB driver.
27 * Based on original USB Bluetooth driver for Linux kernel
28 * Copyright (c) 2000 Greg Kroah-Hartman <greg@kroah.com>
29 * Copyright (c) 2000 Mark Douglas Corner <mcorner@umich.edu>
30 *
31 * $Id: hci_usb.c,v 1.5 2001/07/05 18:42:44 maxk Exp $
32 */
33 #define VERSION "1.0"
34
35 #include <linux/config.h>
36 #include <linux/module.h>
37
38 #include <linux/version.h>
39 #include <linux/config.h>
40 #include <linux/kernel.h>
41 #include <linux/init.h>
42 #include <linux/sched.h>
43 #include <linux/types.h>
44 #include <linux/fcntl.h>
45 #include <linux/interrupt.h>
46 #include <linux/ptrace.h>
47 #include <linux/poll.h>
48
49 #include <linux/slab.h>
50 #include <linux/tty.h>
51 #include <linux/errno.h>
52 #include <linux/string.h>
53 #include <linux/signal.h>
54 #include <linux/ioctl.h>
55 #include <linux/skbuff.h>
56
57 #include <linux/usb.h>
58
59 #include <net/bluetooth/bluetooth.h>
60 #include <net/bluetooth/bluez.h>
61 #include <net/bluetooth/hci_core.h>
62 #include <net/bluetooth/hci_usb.h>
63
64 #ifndef HCI_USB_DEBUG
65 #undef DBG
66 #define DBG( A... )
67 #undef DMP
68 #define DMP( A... )
69 #endif
70
71 static struct usb_device_id usb_bluetooth_ids [] = {
72 { USB_DEVICE_INFO(HCI_DEV_CLASS, HCI_DEV_SUBCLASS, HCI_DEV_PROTOCOL) },
73 { } /* Terminating entry */
74 };
75
76 MODULE_DEVICE_TABLE (usb, usb_bluetooth_ids);
77
78 static int hci_usb_ctrl_msg(struct hci_usb *husb, struct sk_buff *skb);
79 static int hci_usb_write_msg(struct hci_usb *husb, struct sk_buff *skb);
80
81 static void hci_usb_unlink_urbs(struct hci_usb *husb)
82 {
83 usb_unlink_urb(husb->read_urb);
84 usb_unlink_urb(husb->intr_urb);
85 usb_unlink_urb(husb->ctrl_urb);
86 usb_unlink_urb(husb->write_urb);
87 }
88
89 static void hci_usb_free_bufs(struct hci_usb *husb)
90 {
91 if (husb->read_urb) {
92 if (husb->read_urb->transfer_buffer)
93 kfree(husb->read_urb->transfer_buffer);
94 usb_free_urb(husb->read_urb);
95 }
96
97 if (husb->intr_urb) {
98 if (husb->intr_urb->transfer_buffer)
99 kfree(husb->intr_urb->transfer_buffer);
100 usb_free_urb(husb->intr_urb);
101 }
102
103 if (husb->ctrl_urb)
104 usb_free_urb(husb->ctrl_urb);
105
106 if (husb->write_urb)
107 usb_free_urb(husb->write_urb);
108
109 if (husb->intr_skb)
110 kfree_skb(husb->intr_skb);
111 }
112
113 /* ------- Interface to HCI layer ------ */
114 /* Initialize device */
115 int hci_usb_open(struct hci_dev *hdev)
116 {
117 struct hci_usb *husb = (struct hci_usb *) hdev->driver_data;
118 int status;
119
120 DBG("%s", hdev->name);
121
122 husb->read_urb->dev = husb->udev;
123 if ((status = usb_submit_urb(husb->read_urb)))
124 DBG("read submit failed. %d", status);
125
126 husb->intr_urb->dev = husb->udev;
127 if ((status = usb_submit_urb(husb->intr_urb)))
128 DBG("interrupt submit failed. %d", status);
129
130 hdev->flags |= HCI_RUNNING;
131
132 return 0;
133 }
134
135 /* Reset device */
136 int hci_usb_flush(struct hci_dev *hdev)
137 {
138 struct hci_usb *husb = (struct hci_usb *) hdev->driver_data;
139
140 DBG("%s", hdev->name);
141
142 /* Drop TX queues */
143 skb_queue_purge(&husb->tx_ctrl_q);
144 skb_queue_purge(&husb->tx_write_q);
145
146 return 0;
147 }
148
149 /* Close device */
150 int hci_usb_close(struct hci_dev *hdev)
151 {
152 struct hci_usb *husb = (struct hci_usb *) hdev->driver_data;
153
154 DBG("%s", hdev->name);
155
156 hdev->flags &= ~HCI_RUNNING;
157 hci_usb_unlink_urbs(husb);
158
159 hci_usb_flush(hdev);
160
161 return 0;
162 }
163
164 void hci_usb_ctrl_wakeup(struct hci_usb *husb)
165 {
166 struct sk_buff *skb;
167
168 if (test_and_set_bit(HCI_TX_CTRL, &husb->tx_state))
169 return;
170
171 DBG("%s", husb->hdev.name);
172
173 if (!(skb = skb_dequeue(&husb->tx_ctrl_q)))
174 goto done;
175
176 if (hci_usb_ctrl_msg(husb, skb)){
177 kfree_skb(skb);
178 goto done;
179 }
180
181 DMP(skb->data, skb->len);
182
183 husb->hdev.stat.byte_tx += skb->len;
184 return;
185
186 done:
187 clear_bit(HCI_TX_CTRL, &husb->tx_state);
188 return;
189 }
190
191 void hci_usb_write_wakeup(struct hci_usb *husb)
192 {
193 struct sk_buff *skb;
194
195 if (test_and_set_bit(HCI_TX_WRITE, &husb->tx_state))
196 return;
197
198 DBG("%s", husb->hdev.name);
199
200 if (!(skb = skb_dequeue(&husb->tx_write_q)))
201 goto done;
202
203 if (hci_usb_write_msg(husb, skb)) {
204 skb_queue_head(&husb->tx_write_q, skb);
205 goto done;
206 }
207
208 DMP(skb->data, skb->len);
209
210 husb->hdev.stat.byte_tx += skb->len;
211 return;
212
213 done:
214 clear_bit(HCI_TX_WRITE, &husb->tx_state);
215 return;
216 }
217
218 /* Send frames from HCI layer */
219 int hci_usb_send_frame(struct sk_buff *skb)
220 {
221 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
222 struct hci_usb *husb;
223
224 if (!hdev) {
225 ERR("frame for uknown device (hdev=NULL)");
226 return -ENODEV;
227 }
228
229 if (!(hdev->flags & HCI_RUNNING))
230 return 0;
231
232 husb = (struct hci_usb *) hdev->driver_data;
233
234 DBG("%s type %d len %d", hdev->name, skb->pkt_type, skb->len);
235
236 switch (skb->pkt_type) {
237 case HCI_COMMAND_PKT:
238 skb_queue_tail(&husb->tx_ctrl_q, skb);
239 hci_usb_ctrl_wakeup(husb);
240 hdev->stat.cmd_tx++;
241 return 0;
242
243 case HCI_ACLDATA_PKT:
244 skb_queue_tail(&husb->tx_write_q, skb);
245 hci_usb_write_wakeup(husb);
246 hdev->stat.acl_tx++;
247 return 0;
248
249 case HCI_SCODATA_PKT:
250 return -EOPNOTSUPP;
251 };
252
253 return 0;
254 }
255
256 /* ---------- USB ------------- */
257
258 static void hci_usb_ctrl(struct urb *urb)
259 {
260 struct sk_buff *skb = (struct sk_buff *) urb->context;
261 struct hci_dev *hdev;
262 struct hci_usb *husb;
263
264 if (!skb)
265 return;
266 hdev = (struct hci_dev *) skb->dev;
267 husb = (struct hci_usb *) hdev->driver_data;
268
269 DBG("%s", hdev->name);
270
271 if (urb->status)
272 DBG("%s ctrl status: %d", hdev->name, urb->status);
273
274 clear_bit(HCI_TX_CTRL, &husb->tx_state);
275 kfree_skb(skb);
276
277 /* Wake up device */
278 hci_usb_ctrl_wakeup(husb);
279 }
280
281 static void hci_usb_bulk_write(struct urb *urb)
282 {
283 struct sk_buff *skb = (struct sk_buff *) urb->context;
284 struct hci_dev *hdev;
285 struct hci_usb *husb;
286
287 if (!skb)
288 return;
289 hdev = (struct hci_dev *) skb->dev;
290 husb = (struct hci_usb *) hdev->driver_data;
291
292 DBG("%s", hdev->name);
293
294 if (urb->status)
295 DBG("%s bulk write status: %d", hdev->name, urb->status);
296
297 clear_bit(HCI_TX_WRITE, &husb->tx_state);
298 kfree_skb(skb);
299
300 /* Wake up device */
301 hci_usb_write_wakeup(husb);
302
303 return;
304 }
305
306 static void hci_usb_intr(struct urb *urb)
307 {
308 struct hci_usb *husb = (struct hci_usb *) urb->context;
309 unsigned char *data = urb->transfer_buffer;
310 register int count = urb->actual_length;
311 register struct sk_buff *skb = husb->intr_skb;
312 hci_event_hdr *eh;
313 register int len;
314
315 if (!husb)
316 return;
317
318 DBG("%s count %d", husb->hdev.name, count);
319
320 if (urb->status || !count) {
321 DBG("%s intr status %d, count %d", husb->hdev.name, urb->status, count);
322 return;
323 }
324
325 /* Do we really have to handle continuations here ? */
326 if (!skb) {
327 /* New frame */
328 if (count < HCI_EVENT_HDR_SIZE) {
329 DBG("%s bad frame len %d", husb->hdev.name, count);
330 return;
331 }
332
333 eh = (hci_event_hdr *) data;
334 len = eh->plen + HCI_EVENT_HDR_SIZE;
335
336 if (count > len) {
337 DBG("%s corrupted frame, len %d", husb->hdev.name, count);
338 return;
339 }
340
341 /* Allocate skb */
342 if (!(skb = bluez_skb_alloc(len, GFP_ATOMIC))) {
343 ERR("Can't allocate mem for new packet");
344 return;
345 }
346 skb->dev = (void *) &husb->hdev;
347 skb->pkt_type = HCI_EVENT_PKT;
348
349 husb->intr_skb = skb;
350 husb->intr_count = len;
351 } else {
352 /* Continuation */
353 if (count > husb->intr_count) {
354 ERR("%s bad frame len %d (expected %d)", husb->hdev.name, count, husb->intr_count);
355
356 kfree_skb(skb);
357 husb->intr_skb = NULL;
358 husb->intr_count = 0;
359 return;
360 }
361 }
362
363 memcpy(skb_put(skb, count), data, count);
364 husb->intr_count -= count;
365
366 DMP(data, count);
367
368 if (!husb->intr_count) {
369 /* Got complete frame */
370
371 husb->hdev.stat.byte_rx += skb->len;
372 hci_recv_frame(skb);
373
374 husb->intr_skb = NULL;
375 }
376 }
377
378 static void hci_usb_bulk_read(struct urb *urb)
379 {
380 struct hci_usb *husb = (struct hci_usb *) urb->context;
381 unsigned char *data = urb->transfer_buffer;
382 int count = urb->actual_length, status;
383 struct sk_buff *skb;
384 hci_acl_hdr *ah;
385 register __u16 dlen;
386
387 if (!husb)
388 return;
389
390 DBG("%s status %d, count %d, flags %x", husb->hdev.name, urb->status, count, urb->transfer_flags);
391
392 if (urb->status) {
393 /* Do not re-submit URB on critical errors */
394 switch (urb->status) {
395 case -ENOENT:
396 return;
397 default:
398 goto resubmit;
399 };
400 }
401 if (!count)
402 goto resubmit;
403
404 DMP(data, count);
405
406 ah = (hci_acl_hdr *) data;
407 dlen = le16_to_cpu(ah->dlen);
408
409 /* Verify frame len and completeness */
410 if ((count - HCI_ACL_HDR_SIZE) != dlen) {
411 ERR("%s corrupted ACL packet: count %d, plen %d", husb->hdev.name, count, dlen);
412 goto resubmit;
413 }
414
415 /* Allocate packet */
416 if (!(skb = bluez_skb_alloc(count, GFP_ATOMIC))) {
417 ERR("Can't allocate mem for new packet");
418 goto resubmit;
419 }
420
421 memcpy(skb_put(skb, count), data, count);
422 skb->dev = (void *) &husb->hdev;
423 skb->pkt_type = HCI_ACLDATA_PKT;
424
425 husb->hdev.stat.byte_rx += skb->len;
426
427 hci_recv_frame(skb);
428
429 resubmit:
430 husb->read_urb->dev = husb->udev;
431 if ((status = usb_submit_urb(husb->read_urb)))
432 DBG("%s read URB submit failed %d", husb->hdev.name, status);
433
434 DBG("%s read URB re-submited", husb->hdev.name);
435 }
436
437 static int hci_usb_ctrl_msg(struct hci_usb *husb, struct sk_buff *skb)
438 {
439 struct urb *urb = husb->ctrl_urb;
440 devrequest *dr = &husb->dev_req;
441 int pipe, status;
442
443 DBG("%s len %d", husb->hdev.name, skb->len);
444
445 pipe = usb_sndctrlpipe(husb->udev, 0);
446
447 dr->requesttype = HCI_CTRL_REQ;
448 dr->request = 0;
449 dr->index = 0;
450 dr->value = 0;
451 dr->length = cpu_to_le16(skb->len);
452
453 FILL_CONTROL_URB(urb, husb->udev, pipe, (void*)dr, skb->data, skb->len,
454 hci_usb_ctrl, skb);
455
456 if ((status = usb_submit_urb(urb))) {
457 DBG("%s control URB submit failed %d", husb->hdev.name, status);
458 return status;
459 }
460
461 return 0;
462 }
463
464 static int hci_usb_write_msg(struct hci_usb *husb, struct sk_buff *skb)
465 {
466 struct urb *urb = husb->write_urb;
467 int pipe, status;
468
469 DBG("%s len %d", husb->hdev.name, skb->len);
470
471 pipe = usb_sndbulkpipe(husb->udev, husb->bulk_out_ep_addr);
472
473 FILL_BULK_URB(urb, husb->udev, pipe, skb->data, skb->len,
474 hci_usb_bulk_write, skb);
475 urb->transfer_flags |= USB_QUEUE_BULK;
476
477 if ((status = usb_submit_urb(urb))) {
478 DBG("%s write URB submit failed %d", husb->hdev.name, status);
479 return status;
480 }
481
482 return 0;
483 }
484
485 static void * hci_usb_probe(struct usb_device *udev, unsigned int ifnum, const struct usb_device_id *id)
486 {
487 struct usb_endpoint_descriptor *bulk_out_ep, *intr_in_ep, *bulk_in_ep;
488 struct usb_interface_descriptor *uif;
489 struct usb_endpoint_descriptor *ep;
490 struct hci_usb *husb;
491 struct hci_dev *hdev;
492 int i, size, pipe;
493 __u8 * buf;
494
495 DBG("udev %p ifnum %d", udev, ifnum);
496
497 /* Check device signature */
498 if ((udev->descriptor.bDeviceClass != HCI_DEV_CLASS) ||
499 (udev->descriptor.bDeviceSubClass != HCI_DEV_SUBCLASS)||
500 (udev->descriptor.bDeviceProtocol != HCI_DEV_PROTOCOL) )
501 return NULL;
502
503 MOD_INC_USE_COUNT;
504
505 uif = &udev->actconfig->interface[ifnum].altsetting[0];
506
507 if (uif->bNumEndpoints != 3) {
508 DBG("Wrong number of endpoints %d", uif->bNumEndpoints);
509 MOD_DEC_USE_COUNT;
510 return NULL;
511 }
512
513 bulk_out_ep = intr_in_ep = bulk_in_ep = NULL;
514
515 /* Find endpoints that we need */
516 for ( i = 0; i < uif->bNumEndpoints; ++i) {
517 ep = &uif->endpoint[i];
518
519 switch (ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
520 case USB_ENDPOINT_XFER_BULK:
521 if (ep->bEndpointAddress & USB_DIR_IN)
522 bulk_in_ep = ep;
523 else
524 bulk_out_ep = ep;
525 break;
526
527 case USB_ENDPOINT_XFER_INT:
528 intr_in_ep = ep;
529 break;
530 };
531 }
532
533 if (!bulk_in_ep || !bulk_out_ep || !intr_in_ep) {
534 DBG("Endpoints not found: %p %p %p", bulk_in_ep, bulk_out_ep, intr_in_ep);
535 MOD_DEC_USE_COUNT;
536 return NULL;
537 }
538
539 if (!(husb = kmalloc(sizeof(struct hci_usb), GFP_KERNEL))) {
540 ERR("Can't allocate: control structure");
541 MOD_DEC_USE_COUNT;
542 return NULL;
543 }
544
545 memset(husb, 0, sizeof(struct hci_usb));
546
547 husb->udev = udev;
548 husb->bulk_out_ep_addr = bulk_out_ep->bEndpointAddress;
549
550 if (!(husb->ctrl_urb = usb_alloc_urb(0))) {
551 ERR("Can't allocate: control URB");
552 goto probe_error;
553 }
554
555 if (!(husb->write_urb = usb_alloc_urb(0))) {
556 ERR("Can't allocate: write URB");
557 goto probe_error;
558 }
559
560 if (!(husb->read_urb = usb_alloc_urb(0))) {
561 ERR("Can't allocate: read URB");
562 goto probe_error;
563 }
564
565 ep = bulk_in_ep;
566 pipe = usb_rcvbulkpipe(udev, ep->bEndpointAddress);
567 size = HCI_MAX_FRAME_SIZE;
568
569 if (!(buf = kmalloc(size, GFP_KERNEL))) {
570 ERR("Can't allocate: read buffer");
571 goto probe_error;
572 }
573
574 FILL_BULK_URB(husb->read_urb, udev, pipe, buf, size, hci_usb_bulk_read, husb);
575 husb->read_urb->transfer_flags |= USB_QUEUE_BULK;
576
577 ep = intr_in_ep;
578 pipe = usb_rcvintpipe(udev, ep->bEndpointAddress);
579 size = usb_maxpacket(udev, pipe, usb_pipeout(pipe));
580
581 if (!(husb->intr_urb = usb_alloc_urb(0))) {
582 ERR("Can't allocate: interrupt URB");
583 goto probe_error;
584 }
585
586 if (!(buf = kmalloc(size, GFP_KERNEL))) {
587 ERR("Can't allocate: interrupt buffer");
588 goto probe_error;
589 }
590
591 FILL_INT_URB(husb->intr_urb, udev, pipe, buf, size, hci_usb_intr, husb, ep->bInterval);
592
593 skb_queue_head_init(&husb->tx_ctrl_q);
594 skb_queue_head_init(&husb->tx_write_q);
595
596 /* Initialize and register HCI device */
597 hdev = &husb->hdev;
598
599 hdev->type = HCI_USB;
600 hdev->driver_data = husb;
601
602 hdev->open = hci_usb_open;
603 hdev->close = hci_usb_close;
604 hdev->flush = hci_usb_flush;
605 hdev->send = hci_usb_send_frame;
606
607 if (hci_register_dev(hdev) < 0) {
608 ERR("Can't register HCI device %s", hdev->name);
609 goto probe_error;
610 }
611
612 return husb;
613
614 probe_error:
615 hci_usb_free_bufs(husb);
616 kfree(husb);
617 MOD_DEC_USE_COUNT;
618 return NULL;
619 }
620
621 static void hci_usb_disconnect(struct usb_device *udev, void *ptr)
622 {
623 struct hci_usb *husb = (struct hci_usb *) ptr;
624 struct hci_dev *hdev = &husb->hdev;
625
626 if (!husb)
627 return;
628
629 DBG("%s", hdev->name);
630
631 hci_usb_close(hdev);
632
633 if (hci_unregister_dev(hdev) < 0) {
634 ERR("Can't unregister HCI device %s", hdev->name);
635 }
636
637 hci_usb_free_bufs(husb);
638 kfree(husb);
639
640 MOD_DEC_USE_COUNT;
641 }
642
643 static struct usb_driver hci_usb_driver =
644 {
645 name: "hci_usb",
646 probe: hci_usb_probe,
647 disconnect: hci_usb_disconnect,
648 id_table: usb_bluetooth_ids,
649 };
650
651 int hci_usb_init(void)
652 {
653 int err;
654
655 INF("BlueZ HCI USB driver ver %s Copyright (C) 2000,2001 Qualcomm Inc",
656 VERSION);
657 INF("Written 2000,2001 by Maxim Krasnyansky <maxk@qualcomm.com>");
658
659 if ((err = usb_register(&hci_usb_driver)) < 0)
660 ERR("Failed to register HCI USB driver");
661
662 return err;
663 }
664
665 void hci_usb_cleanup(void)
666 {
667 usb_deregister(&hci_usb_driver);
668 }
669
670 module_init(hci_usb_init);
671 module_exit(hci_usb_cleanup);
672
673 MODULE_AUTHOR("Maxim Krasnyansky <maxk@qualcomm.com>");
674 MODULE_DESCRIPTION("BlueZ HCI USB driver ver " VERSION);
675 MODULE_LICENSE("GPL");
676