Linux-2.6.12-rc2
[linux-2.6/kvm.git] / drivers / bluetooth / bpa10x.c
blob2771c861f185295e120270cc0ac63fd0131df201
1 /*
3 * Digianswer Bluetooth USB driver
5 * Copyright (C) 2004-2005 Marcel Holtmann <marcel@holtmann.org>
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.
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.
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
24 #include <linux/config.h>
25 #include <linux/module.h>
27 #include <linux/kernel.h>
28 #include <linux/init.h>
29 #include <linux/slab.h>
30 #include <linux/types.h>
31 #include <linux/errno.h>
33 #include <linux/usb.h>
35 #include <net/bluetooth/bluetooth.h>
36 #include <net/bluetooth/hci_core.h>
38 #ifndef CONFIG_BT_HCIBPA10X_DEBUG
39 #undef BT_DBG
40 #define BT_DBG(D...)
41 #endif
43 #define VERSION "0.8"
45 static int ignore = 0;
47 static struct usb_device_id bpa10x_table[] = {
48 /* Tektronix BPA 100/105 (Digianswer) */
49 { USB_DEVICE(0x08fd, 0x0002) },
51 { } /* Terminating entry */
54 MODULE_DEVICE_TABLE(usb, bpa10x_table);
56 #define BPA10X_CMD_EP 0x00
57 #define BPA10X_EVT_EP 0x81
58 #define BPA10X_TX_EP 0x02
59 #define BPA10X_RX_EP 0x82
61 #define BPA10X_CMD_BUF_SIZE 252
62 #define BPA10X_EVT_BUF_SIZE 16
63 #define BPA10X_TX_BUF_SIZE 384
64 #define BPA10X_RX_BUF_SIZE 384
66 struct bpa10x_data {
67 struct hci_dev *hdev;
68 struct usb_device *udev;
70 rwlock_t lock;
72 struct sk_buff_head cmd_queue;
73 struct urb *cmd_urb;
74 struct urb *evt_urb;
75 struct sk_buff *evt_skb;
76 unsigned int evt_len;
78 struct sk_buff_head tx_queue;
79 struct urb *tx_urb;
80 struct urb *rx_urb;
83 #define HCI_VENDOR_HDR_SIZE 5
85 struct hci_vendor_hdr {
86 __u8 type;
87 __u16 snum;
88 __u16 dlen;
89 } __attribute__ ((packed));
91 static void bpa10x_recv_bulk(struct bpa10x_data *data, unsigned char *buf, int count)
93 struct hci_acl_hdr *ah;
94 struct hci_sco_hdr *sh;
95 struct hci_vendor_hdr *vh;
96 struct sk_buff *skb;
97 int len;
99 while (count) {
100 switch (*buf++) {
101 case HCI_ACLDATA_PKT:
102 ah = (struct hci_acl_hdr *) buf;
103 len = HCI_ACL_HDR_SIZE + __le16_to_cpu(ah->dlen);
104 skb = bt_skb_alloc(len, GFP_ATOMIC);
105 if (skb) {
106 memcpy(skb_put(skb, len), buf, len);
107 skb->dev = (void *) data->hdev;
108 skb->pkt_type = HCI_ACLDATA_PKT;
109 hci_recv_frame(skb);
111 break;
113 case HCI_SCODATA_PKT:
114 sh = (struct hci_sco_hdr *) buf;
115 len = HCI_SCO_HDR_SIZE + sh->dlen;
116 skb = bt_skb_alloc(len, GFP_ATOMIC);
117 if (skb) {
118 memcpy(skb_put(skb, len), buf, len);
119 skb->dev = (void *) data->hdev;
120 skb->pkt_type = HCI_SCODATA_PKT;
121 hci_recv_frame(skb);
123 break;
125 case HCI_VENDOR_PKT:
126 vh = (struct hci_vendor_hdr *) buf;
127 len = HCI_VENDOR_HDR_SIZE + __le16_to_cpu(vh->dlen);
128 skb = bt_skb_alloc(len, GFP_ATOMIC);
129 if (skb) {
130 memcpy(skb_put(skb, len), buf, len);
131 skb->dev = (void *) data->hdev;
132 skb->pkt_type = HCI_VENDOR_PKT;
133 hci_recv_frame(skb);
135 break;
137 default:
138 len = count - 1;
139 break;
142 buf += len;
143 count -= (len + 1);
147 static int bpa10x_recv_event(struct bpa10x_data *data, unsigned char *buf, int size)
149 BT_DBG("data %p buf %p size %d", data, buf, size);
151 if (data->evt_skb) {
152 struct sk_buff *skb = data->evt_skb;
154 memcpy(skb_put(skb, size), buf, size);
156 if (skb->len == data->evt_len) {
157 data->evt_skb = NULL;
158 data->evt_len = 0;
159 hci_recv_frame(skb);
161 } else {
162 struct sk_buff *skb;
163 struct hci_event_hdr *hdr;
164 unsigned char pkt_type;
165 int pkt_len = 0;
167 if (size < HCI_EVENT_HDR_SIZE + 1) {
168 BT_ERR("%s event packet block with size %d is too short",
169 data->hdev->name, size);
170 return -EILSEQ;
173 pkt_type = *buf++;
174 size--;
176 if (pkt_type != HCI_EVENT_PKT) {
177 BT_ERR("%s unexpected event packet start byte 0x%02x",
178 data->hdev->name, pkt_type);
179 return -EPROTO;
182 hdr = (struct hci_event_hdr *) buf;
183 pkt_len = HCI_EVENT_HDR_SIZE + hdr->plen;
185 skb = bt_skb_alloc(pkt_len, GFP_ATOMIC);
186 if (!skb) {
187 BT_ERR("%s no memory for new event packet",
188 data->hdev->name);
189 return -ENOMEM;
192 skb->dev = (void *) data->hdev;
193 skb->pkt_type = pkt_type;
195 memcpy(skb_put(skb, size), buf, size);
197 if (pkt_len == size) {
198 hci_recv_frame(skb);
199 } else {
200 data->evt_skb = skb;
201 data->evt_len = pkt_len;
205 return 0;
208 static void bpa10x_wakeup(struct bpa10x_data *data)
210 struct urb *urb;
211 struct sk_buff *skb;
212 int err;
214 BT_DBG("data %p", data);
216 urb = data->cmd_urb;
217 if (urb->status == -EINPROGRESS)
218 skb = NULL;
219 else
220 skb = skb_dequeue(&data->cmd_queue);
222 if (skb) {
223 struct usb_ctrlrequest *cr;
225 if (skb->len > BPA10X_CMD_BUF_SIZE) {
226 BT_ERR("%s command packet with size %d is too big",
227 data->hdev->name, skb->len);
228 kfree_skb(skb);
229 return;
232 cr = (struct usb_ctrlrequest *) urb->setup_packet;
233 cr->wLength = __cpu_to_le16(skb->len);
235 memcpy(urb->transfer_buffer, skb->data, skb->len);
236 urb->transfer_buffer_length = skb->len;
238 err = usb_submit_urb(urb, GFP_ATOMIC);
239 if (err < 0 && err != -ENODEV) {
240 BT_ERR("%s submit failed for command urb %p with error %d",
241 data->hdev->name, urb, err);
242 skb_queue_head(&data->cmd_queue, skb);
243 } else
244 kfree_skb(skb);
247 urb = data->tx_urb;
248 if (urb->status == -EINPROGRESS)
249 skb = NULL;
250 else
251 skb = skb_dequeue(&data->tx_queue);
253 if (skb) {
254 memcpy(urb->transfer_buffer, skb->data, skb->len);
255 urb->transfer_buffer_length = skb->len;
257 err = usb_submit_urb(urb, GFP_ATOMIC);
258 if (err < 0 && err != -ENODEV) {
259 BT_ERR("%s submit failed for command urb %p with error %d",
260 data->hdev->name, urb, err);
261 skb_queue_head(&data->tx_queue, skb);
262 } else
263 kfree_skb(skb);
267 static void bpa10x_complete(struct urb *urb, struct pt_regs *regs)
269 struct bpa10x_data *data = urb->context;
270 unsigned char *buf = urb->transfer_buffer;
271 int err, count = urb->actual_length;
273 BT_DBG("data %p urb %p buf %p count %d", data, urb, buf, count);
275 read_lock(&data->lock);
277 if (!test_bit(HCI_RUNNING, &data->hdev->flags))
278 goto unlock;
280 if (urb->status < 0 || !count)
281 goto resubmit;
283 if (usb_pipein(urb->pipe)) {
284 data->hdev->stat.byte_rx += count;
286 if (usb_pipetype(urb->pipe) == PIPE_INTERRUPT)
287 bpa10x_recv_event(data, buf, count);
289 if (usb_pipetype(urb->pipe) == PIPE_BULK)
290 bpa10x_recv_bulk(data, buf, count);
291 } else {
292 data->hdev->stat.byte_tx += count;
294 bpa10x_wakeup(data);
297 resubmit:
298 if (usb_pipein(urb->pipe)) {
299 err = usb_submit_urb(urb, GFP_ATOMIC);
300 if (err < 0 && err != -ENODEV) {
301 BT_ERR("%s urb %p type %d resubmit status %d",
302 data->hdev->name, urb, usb_pipetype(urb->pipe), err);
306 unlock:
307 read_unlock(&data->lock);
310 static inline struct urb *bpa10x_alloc_urb(struct usb_device *udev, unsigned int pipe, size_t size, int flags, void *data)
312 struct urb *urb;
313 struct usb_ctrlrequest *cr;
314 unsigned char *buf;
316 BT_DBG("udev %p data %p", udev, data);
318 urb = usb_alloc_urb(0, flags);
319 if (!urb)
320 return NULL;
322 buf = kmalloc(size, flags);
323 if (!buf) {
324 usb_free_urb(urb);
325 return NULL;
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;
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);
343 usb_fill_control_urb(urb, udev, pipe, (void *) cr, buf, 0, bpa10x_complete, data);
344 break;
346 case PIPE_INTERRUPT:
347 usb_fill_int_urb(urb, udev, pipe, buf, size, bpa10x_complete, data, 1);
348 break;
350 case PIPE_BULK:
351 usb_fill_bulk_urb(urb, udev, pipe, buf, size, bpa10x_complete, data);
352 break;
354 default:
355 kfree(buf);
356 usb_free_urb(urb);
357 return NULL;
360 return urb;
363 static inline void bpa10x_free_urb(struct urb *urb)
365 BT_DBG("urb %p", urb);
367 if (!urb)
368 return;
370 if (urb->setup_packet)
371 kfree(urb->setup_packet);
373 if (urb->transfer_buffer)
374 kfree(urb->transfer_buffer);
376 usb_free_urb(urb);
379 static int bpa10x_open(struct hci_dev *hdev)
381 struct bpa10x_data *data = hdev->driver_data;
382 struct usb_device *udev = data->udev;
383 unsigned long flags;
384 int err;
386 BT_DBG("hdev %p data %p", hdev, data);
388 if (test_and_set_bit(HCI_RUNNING, &hdev->flags))
389 return 0;
391 data->cmd_urb = bpa10x_alloc_urb(udev, usb_sndctrlpipe(udev, BPA10X_CMD_EP),
392 BPA10X_CMD_BUF_SIZE, GFP_KERNEL, data);
393 if (!data->cmd_urb) {
394 err = -ENOMEM;
395 goto done;
398 data->evt_urb = bpa10x_alloc_urb(udev, usb_rcvintpipe(udev, BPA10X_EVT_EP),
399 BPA10X_EVT_BUF_SIZE, GFP_KERNEL, data);
400 if (!data->evt_urb) {
401 bpa10x_free_urb(data->cmd_urb);
402 err = -ENOMEM;
403 goto done;
406 data->rx_urb = bpa10x_alloc_urb(udev, usb_rcvbulkpipe(udev, BPA10X_RX_EP),
407 BPA10X_RX_BUF_SIZE, GFP_KERNEL, data);
408 if (!data->rx_urb) {
409 bpa10x_free_urb(data->evt_urb);
410 bpa10x_free_urb(data->cmd_urb);
411 err = -ENOMEM;
412 goto done;
415 data->tx_urb = bpa10x_alloc_urb(udev, usb_sndbulkpipe(udev, BPA10X_TX_EP),
416 BPA10X_TX_BUF_SIZE, GFP_KERNEL, data);
417 if (!data->rx_urb) {
418 bpa10x_free_urb(data->rx_urb);
419 bpa10x_free_urb(data->evt_urb);
420 bpa10x_free_urb(data->cmd_urb);
421 err = -ENOMEM;
422 goto done;
425 write_lock_irqsave(&data->lock, flags);
427 err = usb_submit_urb(data->evt_urb, GFP_ATOMIC);
428 if (err < 0) {
429 BT_ERR("%s submit failed for event urb %p with error %d",
430 data->hdev->name, data->evt_urb, err);
431 } else {
432 err = usb_submit_urb(data->rx_urb, GFP_ATOMIC);
433 if (err < 0) {
434 BT_ERR("%s submit failed for rx urb %p with error %d",
435 data->hdev->name, data->evt_urb, err);
436 usb_kill_urb(data->evt_urb);
440 write_unlock_irqrestore(&data->lock, flags);
442 done:
443 if (err < 0)
444 clear_bit(HCI_RUNNING, &hdev->flags);
446 return err;
449 static int bpa10x_close(struct hci_dev *hdev)
451 struct bpa10x_data *data = hdev->driver_data;
452 unsigned long flags;
454 BT_DBG("hdev %p data %p", hdev, data);
456 if (!test_and_clear_bit(HCI_RUNNING, &hdev->flags))
457 return 0;
459 write_lock_irqsave(&data->lock, flags);
461 skb_queue_purge(&data->cmd_queue);
462 usb_kill_urb(data->cmd_urb);
463 usb_kill_urb(data->evt_urb);
464 usb_kill_urb(data->rx_urb);
465 usb_kill_urb(data->tx_urb);
467 write_unlock_irqrestore(&data->lock, flags);
469 bpa10x_free_urb(data->cmd_urb);
470 bpa10x_free_urb(data->evt_urb);
471 bpa10x_free_urb(data->rx_urb);
472 bpa10x_free_urb(data->tx_urb);
474 return 0;
477 static int bpa10x_flush(struct hci_dev *hdev)
479 struct bpa10x_data *data = hdev->driver_data;
481 BT_DBG("hdev %p data %p", hdev, data);
483 skb_queue_purge(&data->cmd_queue);
485 return 0;
488 static int bpa10x_send_frame(struct sk_buff *skb)
490 struct hci_dev *hdev = (struct hci_dev *) skb->dev;
491 struct bpa10x_data *data;
493 BT_DBG("hdev %p skb %p type %d len %d", hdev, skb, skb->pkt_type, skb->len);
495 if (!hdev) {
496 BT_ERR("Frame for unknown HCI device");
497 return -ENODEV;
500 if (!test_bit(HCI_RUNNING, &hdev->flags))
501 return -EBUSY;
503 data = hdev->driver_data;
505 /* Prepend skb with frame type */
506 memcpy(skb_push(skb, 1), &(skb->pkt_type), 1);
508 switch (skb->pkt_type) {
509 case HCI_COMMAND_PKT:
510 hdev->stat.cmd_tx++;
511 skb_queue_tail(&data->cmd_queue, skb);
512 break;
514 case HCI_ACLDATA_PKT:
515 hdev->stat.acl_tx++;
516 skb_queue_tail(&data->tx_queue, skb);
517 break;
519 case HCI_SCODATA_PKT:
520 hdev->stat.sco_tx++;
521 skb_queue_tail(&data->tx_queue, skb);
522 break;
525 read_lock(&data->lock);
527 bpa10x_wakeup(data);
529 read_unlock(&data->lock);
531 return 0;
534 static void bpa10x_destruct(struct hci_dev *hdev)
536 struct bpa10x_data *data = hdev->driver_data;
538 BT_DBG("hdev %p data %p", hdev, data);
540 kfree(data);
543 static int bpa10x_probe(struct usb_interface *intf, const struct usb_device_id *id)
545 struct usb_device *udev = interface_to_usbdev(intf);
546 struct hci_dev *hdev;
547 struct bpa10x_data *data;
548 int err;
550 BT_DBG("intf %p id %p", intf, id);
552 if (ignore)
553 return -ENODEV;
555 data = kmalloc(sizeof(*data), GFP_KERNEL);
556 if (!data) {
557 BT_ERR("Can't allocate data structure");
558 return -ENOMEM;
561 memset(data, 0, sizeof(*data));
563 data->udev = udev;
565 rwlock_init(&data->lock);
567 skb_queue_head_init(&data->cmd_queue);
568 skb_queue_head_init(&data->tx_queue);
570 hdev = hci_alloc_dev();
571 if (!hdev) {
572 BT_ERR("Can't allocate HCI device");
573 kfree(data);
574 return -ENOMEM;
577 data->hdev = hdev;
579 hdev->type = HCI_USB;
580 hdev->driver_data = data;
581 SET_HCIDEV_DEV(hdev, &intf->dev);
583 hdev->open = bpa10x_open;
584 hdev->close = bpa10x_close;
585 hdev->flush = bpa10x_flush;
586 hdev->send = bpa10x_send_frame;
587 hdev->destruct = bpa10x_destruct;
589 hdev->owner = THIS_MODULE;
591 err = hci_register_dev(hdev);
592 if (err < 0) {
593 BT_ERR("Can't register HCI device");
594 kfree(data);
595 hci_free_dev(hdev);
596 return err;
599 usb_set_intfdata(intf, data);
601 return 0;
604 static void bpa10x_disconnect(struct usb_interface *intf)
606 struct bpa10x_data *data = usb_get_intfdata(intf);
607 struct hci_dev *hdev = data->hdev;
609 BT_DBG("intf %p", intf);
611 if (!hdev)
612 return;
614 usb_set_intfdata(intf, NULL);
616 if (hci_unregister_dev(hdev) < 0)
617 BT_ERR("Can't unregister HCI device %s", hdev->name);
619 hci_free_dev(hdev);
622 static struct usb_driver bpa10x_driver = {
623 .owner = THIS_MODULE,
624 .name = "bpa10x",
625 .probe = bpa10x_probe,
626 .disconnect = bpa10x_disconnect,
627 .id_table = bpa10x_table,
630 static int __init bpa10x_init(void)
632 int err;
634 BT_INFO("Digianswer Bluetooth USB driver ver %s", VERSION);
636 err = usb_register(&bpa10x_driver);
637 if (err < 0)
638 BT_ERR("Failed to register USB driver");
640 return err;
643 static void __exit bpa10x_exit(void)
645 usb_deregister(&bpa10x_driver);
648 module_init(bpa10x_init);
649 module_exit(bpa10x_exit);
651 module_param(ignore, bool, 0644);
652 MODULE_PARM_DESC(ignore, "Ignore devices from the matching table");
654 MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
655 MODULE_DESCRIPTION("Digianswer Bluetooth USB driver ver " VERSION);
656 MODULE_VERSION(VERSION);
657 MODULE_LICENSE("GPL");