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