drm: SiS 315 Awareness.
[linux-2.6] / drivers / bluetooth / bpa10x.c
1 /*
2  *
3  *  Digianswer Bluetooth USB driver
4  *
5  *  Copyright (C) 2004-2005  Marcel Holtmann <marcel@holtmann.org>
6  *
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 as published by
10  *  the Free Software Foundation; either version 2 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, write to the Free Software
20  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  *
22  */
23
24 #include <linux/module.h>
25
26 #include <linux/kernel.h>
27 #include <linux/init.h>
28 #include <linux/slab.h>
29 #include <linux/types.h>
30 #include <linux/errno.h>
31
32 #include <linux/usb.h>
33
34 #include <net/bluetooth/bluetooth.h>
35 #include <net/bluetooth/hci_core.h>
36
37 #ifndef CONFIG_BT_HCIBPA10X_DEBUG
38 #undef  BT_DBG
39 #define BT_DBG(D...)
40 #endif
41
42 #define VERSION "0.8"
43
44 static int ignore = 0;
45
46 static struct usb_device_id bpa10x_table[] = {
47         /* Tektronix BPA 100/105 (Digianswer) */
48         { USB_DEVICE(0x08fd, 0x0002) },
49
50         { }     /* Terminating entry */
51 };
52
53 MODULE_DEVICE_TABLE(usb, bpa10x_table);
54
55 #define BPA10X_CMD_EP           0x00
56 #define BPA10X_EVT_EP           0x81
57 #define BPA10X_TX_EP            0x02
58 #define BPA10X_RX_EP            0x82
59
60 #define BPA10X_CMD_BUF_SIZE     252
61 #define BPA10X_EVT_BUF_SIZE     16
62 #define BPA10X_TX_BUF_SIZE      384
63 #define BPA10X_RX_BUF_SIZE      384
64
65 struct bpa10x_data {
66         struct hci_dev          *hdev;
67         struct usb_device       *udev;
68
69         rwlock_t                lock;
70
71         struct sk_buff_head     cmd_queue;
72         struct urb              *cmd_urb;
73         struct urb              *evt_urb;
74         struct sk_buff          *evt_skb;
75         unsigned int            evt_len;
76
77         struct sk_buff_head     tx_queue;
78         struct urb              *tx_urb;
79         struct urb              *rx_urb;
80 };
81
82 #define HCI_VENDOR_HDR_SIZE     5
83
84 struct hci_vendor_hdr {
85         __u8    type;
86         __le16  snum;
87         __le16  dlen;
88 } __attribute__ ((packed));
89
90 static void bpa10x_recv_bulk(struct bpa10x_data *data, unsigned char *buf, int count)
91 {
92         struct hci_acl_hdr *ah;
93         struct hci_sco_hdr *sh;
94         struct hci_vendor_hdr *vh;
95         struct sk_buff *skb;
96         int len;
97
98         while (count) {
99                 switch (*buf++) {
100                 case HCI_ACLDATA_PKT:
101                         ah = (struct hci_acl_hdr *) buf;
102                         len = HCI_ACL_HDR_SIZE + __le16_to_cpu(ah->dlen);
103                         skb = bt_skb_alloc(len, GFP_ATOMIC);
104                         if (skb) {
105                                 memcpy(skb_put(skb, len), buf, len);
106                                 skb->dev = (void *) data->hdev;
107                                 bt_cb(skb)->pkt_type = HCI_ACLDATA_PKT;
108                                 hci_recv_frame(skb);
109                         }
110                         break;
111
112                 case HCI_SCODATA_PKT:
113                         sh = (struct hci_sco_hdr *) buf;
114                         len = HCI_SCO_HDR_SIZE + sh->dlen;
115                         skb = bt_skb_alloc(len, GFP_ATOMIC);
116                         if (skb) {
117                                 memcpy(skb_put(skb, len), buf, len);
118                                 skb->dev = (void *) data->hdev;
119                                 bt_cb(skb)->pkt_type = HCI_SCODATA_PKT;
120                                 hci_recv_frame(skb);
121                         }
122                         break;
123
124                 case HCI_VENDOR_PKT:
125                         vh = (struct hci_vendor_hdr *) buf;
126                         len = HCI_VENDOR_HDR_SIZE + __le16_to_cpu(vh->dlen);
127                         skb = bt_skb_alloc(len, GFP_ATOMIC);
128                         if (skb) {
129                                 memcpy(skb_put(skb, len), buf, len);
130                                 skb->dev = (void *) data->hdev;
131                                 bt_cb(skb)->pkt_type = HCI_VENDOR_PKT;
132                                 hci_recv_frame(skb);
133                         }
134                         break;
135
136                 default:
137                         len = count - 1;
138                         break;
139                 }
140
141                 buf   += len;
142                 count -= (len + 1);
143         }
144 }
145
146 static int bpa10x_recv_event(struct bpa10x_data *data, unsigned char *buf, int size)
147 {
148         BT_DBG("data %p buf %p size %d", data, buf, size);
149
150         if (data->evt_skb) {
151                 struct sk_buff *skb = data->evt_skb;
152
153                 memcpy(skb_put(skb, size), buf, size);
154
155                 if (skb->len == data->evt_len) {
156                         data->evt_skb = NULL;
157                         data->evt_len = 0;
158                         hci_recv_frame(skb);
159                 }
160         } else {
161                 struct sk_buff *skb;
162                 struct hci_event_hdr *hdr;
163                 unsigned char pkt_type;
164                 int pkt_len = 0;
165
166                 if (size < HCI_EVENT_HDR_SIZE + 1) {
167                         BT_ERR("%s event packet block with size %d is too short",
168                                                         data->hdev->name, size);
169                         return -EILSEQ;
170                 }
171
172                 pkt_type = *buf++;
173                 size--;
174
175                 if (pkt_type != HCI_EVENT_PKT) {
176                         BT_ERR("%s unexpected event packet start byte 0x%02x",
177                                                         data->hdev->name, pkt_type);
178                         return -EPROTO;
179                 }
180
181                 hdr = (struct hci_event_hdr *) buf;
182                 pkt_len = HCI_EVENT_HDR_SIZE + hdr->plen;
183
184                 skb = bt_skb_alloc(pkt_len, GFP_ATOMIC);
185                 if (!skb) {
186                         BT_ERR("%s no memory for new event packet",
187                                                         data->hdev->name);
188                         return -ENOMEM;
189                 }
190
191                 skb->dev = (void *) data->hdev;
192                 bt_cb(skb)->pkt_type = pkt_type;
193
194                 memcpy(skb_put(skb, size), buf, size);
195
196                 if (pkt_len == size) {
197                         hci_recv_frame(skb);
198                 } else {
199                         data->evt_skb = skb;
200                         data->evt_len = pkt_len;
201                 }
202         }
203
204         return 0;
205 }
206
207 static void bpa10x_wakeup(struct bpa10x_data *data)
208 {
209         struct urb *urb;
210         struct sk_buff *skb;
211         int err;
212
213         BT_DBG("data %p", data);
214
215         urb = data->cmd_urb;
216         if (urb->status == -EINPROGRESS)
217                 skb = NULL;
218         else
219                 skb = skb_dequeue(&data->cmd_queue);
220
221         if (skb) {
222                 struct usb_ctrlrequest *cr;
223
224                 if (skb->len > BPA10X_CMD_BUF_SIZE) {
225                         BT_ERR("%s command packet with size %d is too big",
226                                                         data->hdev->name, skb->len);
227                         kfree_skb(skb);
228                         return;
229                 }
230
231                 cr = (struct usb_ctrlrequest *) urb->setup_packet;
232                 cr->wLength = __cpu_to_le16(skb->len);
233
234                 memcpy(urb->transfer_buffer, skb->data, skb->len);
235                 urb->transfer_buffer_length = skb->len;
236
237                 err = usb_submit_urb(urb, GFP_ATOMIC);
238                 if (err < 0 && err != -ENODEV) {
239                         BT_ERR("%s submit failed for command urb %p with error %d",
240                                                         data->hdev->name, urb, err);
241                         skb_queue_head(&data->cmd_queue, skb);
242                 } else
243                         kfree_skb(skb);
244         }
245
246         urb = data->tx_urb;
247         if (urb->status == -EINPROGRESS)
248                 skb = NULL;
249         else
250                 skb = skb_dequeue(&data->tx_queue);
251
252         if (skb) {
253                 memcpy(urb->transfer_buffer, skb->data, skb->len);
254                 urb->transfer_buffer_length = skb->len;
255
256                 err = usb_submit_urb(urb, GFP_ATOMIC);
257                 if (err < 0 && err != -ENODEV) {
258                         BT_ERR("%s submit failed for command urb %p with error %d",
259                                                         data->hdev->name, urb, err);
260                         skb_queue_head(&data->tx_queue, skb);
261                 } else
262                         kfree_skb(skb);
263         }
264 }
265
266 static void bpa10x_complete(struct urb *urb, struct pt_regs *regs)
267 {
268         struct bpa10x_data *data = urb->context;
269         unsigned char *buf = urb->transfer_buffer;
270         int err, count = urb->actual_length;
271
272         BT_DBG("data %p urb %p buf %p count %d", data, urb, buf, count);
273
274         read_lock(&data->lock);
275
276         if (!test_bit(HCI_RUNNING, &data->hdev->flags))
277                 goto unlock;
278
279         if (urb->status < 0 || !count)
280                 goto resubmit;
281
282         if (usb_pipein(urb->pipe)) {
283                 data->hdev->stat.byte_rx += count;
284
285                 if (usb_pipetype(urb->pipe) == PIPE_INTERRUPT)
286                         bpa10x_recv_event(data, buf, count);
287
288                 if (usb_pipetype(urb->pipe) == PIPE_BULK)
289                         bpa10x_recv_bulk(data, buf, count);
290         } else {
291                 data->hdev->stat.byte_tx += count;
292
293                 bpa10x_wakeup(data);
294         }
295
296 resubmit:
297         if (usb_pipein(urb->pipe)) {
298                 err = usb_submit_urb(urb, GFP_ATOMIC);
299                 if (err < 0 && err != -ENODEV) {
300                         BT_ERR("%s urb %p type %d resubmit status %d",
301                                 data->hdev->name, urb, usb_pipetype(urb->pipe), err);
302                 }
303         }
304
305 unlock:
306         read_unlock(&data->lock);
307 }
308
309 static inline struct urb *bpa10x_alloc_urb(struct usb_device *udev, unsigned int pipe,
310                                         size_t size, gfp_t flags, void *data)
311 {
312         struct urb *urb;
313         struct usb_ctrlrequest *cr;
314         unsigned char *buf;
315
316         BT_DBG("udev %p data %p", udev, data);
317
318         urb = usb_alloc_urb(0, flags);
319         if (!urb)
320                 return NULL;
321
322         buf = kmalloc(size, flags);
323         if (!buf) {
324                 usb_free_urb(urb);
325                 return NULL;
326         }
327
328         switch (usb_pipetype(pipe)) {
329         case PIPE_CONTROL:
330                 cr = kmalloc(sizeof(*cr), flags);
331                 if (!cr) {
332                         kfree(buf);
333                         usb_free_urb(urb);
334                         return NULL;
335                 }
336
337                 cr->bRequestType = USB_TYPE_VENDOR;
338                 cr->bRequest     = 0;
339                 cr->wIndex       = 0;
340                 cr->wValue       = 0;
341                 cr->wLength      = __cpu_to_le16(0);
342
343                 usb_fill_control_urb(urb, udev, pipe, (void *) cr, buf, 0, bpa10x_complete, data);
344                 break;
345
346         case PIPE_INTERRUPT:
347                 usb_fill_int_urb(urb, udev, pipe, buf, size, bpa10x_complete, data, 1);
348                 break;
349
350         case PIPE_BULK:
351                 usb_fill_bulk_urb(urb, udev, pipe, buf, size, bpa10x_complete, data);
352                 break;
353
354         default:
355                 kfree(buf);
356                 usb_free_urb(urb);
357                 return NULL;
358         }
359
360         return urb;
361 }
362
363 static inline void bpa10x_free_urb(struct urb *urb)
364 {
365         BT_DBG("urb %p", urb);
366
367         if (!urb)
368                 return;
369
370         kfree(urb->setup_packet);
371         kfree(urb->transfer_buffer);
372
373         usb_free_urb(urb);
374 }
375
376 static int bpa10x_open(struct hci_dev *hdev)
377 {
378         struct bpa10x_data *data = hdev->driver_data;
379         struct usb_device *udev = data->udev;
380         unsigned long flags;
381         int err;
382
383         BT_DBG("hdev %p data %p", hdev, data);
384
385         if (test_and_set_bit(HCI_RUNNING, &hdev->flags))
386                 return 0;
387
388         data->cmd_urb = bpa10x_alloc_urb(udev, usb_sndctrlpipe(udev, BPA10X_CMD_EP),
389                                         BPA10X_CMD_BUF_SIZE, GFP_KERNEL, data);
390         if (!data->cmd_urb) {
391                 err = -ENOMEM;
392                 goto done;
393         }
394
395         data->evt_urb = bpa10x_alloc_urb(udev, usb_rcvintpipe(udev, BPA10X_EVT_EP),
396                                         BPA10X_EVT_BUF_SIZE, GFP_KERNEL, data);
397         if (!data->evt_urb) {
398                 bpa10x_free_urb(data->cmd_urb);
399                 err = -ENOMEM;
400                 goto done;
401         }
402
403         data->rx_urb = bpa10x_alloc_urb(udev, usb_rcvbulkpipe(udev, BPA10X_RX_EP),
404                                         BPA10X_RX_BUF_SIZE, GFP_KERNEL, data);
405         if (!data->rx_urb) {
406                 bpa10x_free_urb(data->evt_urb);
407                 bpa10x_free_urb(data->cmd_urb);
408                 err = -ENOMEM;
409                 goto done;
410         }
411
412         data->tx_urb = bpa10x_alloc_urb(udev, usb_sndbulkpipe(udev, BPA10X_TX_EP),
413                                         BPA10X_TX_BUF_SIZE, GFP_KERNEL, data);
414         if (!data->rx_urb) {
415                 bpa10x_free_urb(data->rx_urb);
416                 bpa10x_free_urb(data->evt_urb);
417                 bpa10x_free_urb(data->cmd_urb);
418                 err = -ENOMEM;
419                 goto done;
420         }
421
422         write_lock_irqsave(&data->lock, flags);
423
424         err = usb_submit_urb(data->evt_urb, GFP_ATOMIC);
425         if (err < 0) {
426                 BT_ERR("%s submit failed for event urb %p with error %d",
427                                         data->hdev->name, data->evt_urb, err);
428         } else {
429                 err = usb_submit_urb(data->rx_urb, GFP_ATOMIC);
430                 if (err < 0) {
431                         BT_ERR("%s submit failed for rx urb %p with error %d",
432                                         data->hdev->name, data->evt_urb, err);
433                         usb_kill_urb(data->evt_urb);
434                 }
435         }
436
437         write_unlock_irqrestore(&data->lock, flags);
438
439 done:
440         if (err < 0)
441                 clear_bit(HCI_RUNNING, &hdev->flags);
442
443         return err;
444 }
445
446 static int bpa10x_close(struct hci_dev *hdev)
447 {
448         struct bpa10x_data *data = hdev->driver_data;
449         unsigned long flags;
450
451         BT_DBG("hdev %p data %p", hdev, data);
452
453         if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
454                 return 0;
455
456         write_lock_irqsave(&data->lock, flags);
457
458         skb_queue_purge(&data->cmd_queue);
459         usb_kill_urb(data->cmd_urb);
460         usb_kill_urb(data->evt_urb);
461         usb_kill_urb(data->rx_urb);
462         usb_kill_urb(data->tx_urb);
463
464         write_unlock_irqrestore(&data->lock, flags);
465
466         bpa10x_free_urb(data->cmd_urb);
467         bpa10x_free_urb(data->evt_urb);
468         bpa10x_free_urb(data->rx_urb);
469         bpa10x_free_urb(data->tx_urb);
470
471         return 0;
472 }
473
474 static int bpa10x_flush(struct hci_dev *hdev)
475 {
476         struct bpa10x_data *data = hdev->driver_data;
477
478         BT_DBG("hdev %p data %p", hdev, data);
479
480         skb_queue_purge(&data->cmd_queue);
481
482         return 0;
483 }
484
485 static int bpa10x_send_frame(struct sk_buff *skb)
486 {
487         struct hci_dev *hdev = (struct hci_dev *) skb->dev;
488         struct bpa10x_data *data;
489
490         BT_DBG("hdev %p skb %p type %d len %d", hdev, skb, bt_cb(skb)->pkt_type, skb->len);
491
492         if (!hdev) {
493                 BT_ERR("Frame for unknown HCI device");
494                 return -ENODEV;
495         }
496
497         if (!test_bit(HCI_RUNNING, &hdev->flags))
498                 return -EBUSY;
499
500         data = hdev->driver_data;
501
502         /* Prepend skb with frame type */
503         memcpy(skb_push(skb, 1), &bt_cb(skb)->pkt_type, 1);
504
505         switch (bt_cb(skb)->pkt_type) {
506         case HCI_COMMAND_PKT:
507                 hdev->stat.cmd_tx++;
508                 skb_queue_tail(&data->cmd_queue, skb);
509                 break;
510
511         case HCI_ACLDATA_PKT:
512                 hdev->stat.acl_tx++;
513                 skb_queue_tail(&data->tx_queue, skb);
514                 break;
515
516         case HCI_SCODATA_PKT:
517                 hdev->stat.sco_tx++;
518                 skb_queue_tail(&data->tx_queue, skb);
519                 break;
520         };
521
522         read_lock(&data->lock);
523
524         bpa10x_wakeup(data);
525
526         read_unlock(&data->lock);
527
528         return 0;
529 }
530
531 static void bpa10x_destruct(struct hci_dev *hdev)
532 {
533         struct bpa10x_data *data = hdev->driver_data;
534
535         BT_DBG("hdev %p data %p", hdev, data);
536
537         kfree(data);
538 }
539
540 static int bpa10x_probe(struct usb_interface *intf, const struct usb_device_id *id)
541 {
542         struct usb_device *udev = interface_to_usbdev(intf);
543         struct hci_dev *hdev;
544         struct bpa10x_data *data;
545         int err;
546
547         BT_DBG("intf %p id %p", intf, id);
548
549         if (ignore)
550                 return -ENODEV;
551
552         if (intf->cur_altsetting->desc.bInterfaceNumber > 0)
553                 return -ENODEV;
554
555         data = kzalloc(sizeof(*data), GFP_KERNEL);
556         if (!data) {
557                 BT_ERR("Can't allocate data structure");
558                 return -ENOMEM;
559         }
560
561         data->udev = udev;
562
563         rwlock_init(&data->lock);
564
565         skb_queue_head_init(&data->cmd_queue);
566         skb_queue_head_init(&data->tx_queue);
567
568         hdev = hci_alloc_dev();
569         if (!hdev) {
570                 BT_ERR("Can't allocate HCI device");
571                 kfree(data);
572                 return -ENOMEM;
573         }
574
575         data->hdev = hdev;
576
577         hdev->type = HCI_USB;
578         hdev->driver_data = data;
579         SET_HCIDEV_DEV(hdev, &intf->dev);
580
581         hdev->open      = bpa10x_open;
582         hdev->close     = bpa10x_close;
583         hdev->flush     = bpa10x_flush;
584         hdev->send      = bpa10x_send_frame;
585         hdev->destruct  = bpa10x_destruct;
586
587         hdev->owner = THIS_MODULE;
588
589         err = hci_register_dev(hdev);
590         if (err < 0) {
591                 BT_ERR("Can't register HCI device");
592                 kfree(data);
593                 hci_free_dev(hdev);
594                 return err;
595         }
596
597         usb_set_intfdata(intf, data);
598
599         return 0;
600 }
601
602 static void bpa10x_disconnect(struct usb_interface *intf)
603 {
604         struct bpa10x_data *data = usb_get_intfdata(intf);
605         struct hci_dev *hdev = data->hdev;
606
607         BT_DBG("intf %p", intf);
608
609         if (!hdev)
610                 return;
611
612         usb_set_intfdata(intf, NULL);
613
614         if (hci_unregister_dev(hdev) < 0)
615                 BT_ERR("Can't unregister HCI device %s", hdev->name);
616
617         hci_free_dev(hdev);
618 }
619
620 static struct usb_driver bpa10x_driver = {
621         .name           = "bpa10x",
622         .probe          = bpa10x_probe,
623         .disconnect     = bpa10x_disconnect,
624         .id_table       = bpa10x_table,
625 };
626
627 static int __init bpa10x_init(void)
628 {
629         int err;
630
631         BT_INFO("Digianswer Bluetooth USB driver ver %s", VERSION);
632
633         err = usb_register(&bpa10x_driver);
634         if (err < 0)
635                 BT_ERR("Failed to register USB driver");
636
637         return err;
638 }
639
640 static void __exit bpa10x_exit(void)
641 {
642         usb_deregister(&bpa10x_driver);
643 }
644
645 module_init(bpa10x_init);
646 module_exit(bpa10x_exit);
647
648 module_param(ignore, bool, 0644);
649 MODULE_PARM_DESC(ignore, "Ignore devices from the matching table");
650
651 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
652 MODULE_DESCRIPTION("Digianswer Bluetooth USB driver ver " VERSION);
653 MODULE_VERSION(VERSION);
654 MODULE_LICENSE("GPL");