From: Andy P. <at...@us...> - 2002-04-11 14:31:55
|
Update of /cvsroot/linux-vax/kernel-2.4/drivers/bluetooth In directory usw-pr-cvs1:/tmp/cvs-serv26834/drivers/bluetooth Added Files: Config.in Makefile hci_uart.c hci_usb.c hci_vhci.c Log Message: synch 2.4.15 commit 56 --- NEW FILE --- mainmenu_option next_comment comment 'Bluetooth device drivers' dep_tristate 'HCI USB driver' CONFIG_BLUEZ_HCIUSB $CONFIG_BLUEZ $CONFIG_USB dep_tristate 'HCI UART driver' CONFIG_BLUEZ_HCIUART $CONFIG_BLUEZ dep_tristate 'HCI VHCI virtual HCI device driver' CONFIG_BLUEZ_HCIVHCI $CONFIG_BLUEZ endmenu --- NEW FILE --- # # Makefile for Bluetooth HCI device drivers. # O_TARGET := bluetooth.o obj-$(CONFIG_BLUEZ_HCIUSB) += hci_usb.o obj-$(CONFIG_BLUEZ_HCIUART) += hci_uart.o obj-$(CONFIG_BLUEZ_HCIVHCI) += hci_vhci.o include $(TOPDIR)/Rules.make --- NEW FILE --- /* BlueZ - Bluetooth protocol stack for Linux Copyright (C) 2000-2001 Qualcomm Incorporated Written 2000,2001 by Maxim Krasnyansky <ma...@qu...> This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License version 2 as published by the Free Software Foundation; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS SOFTWARE IS DISCLAIMED. */ /* * BlueZ HCI UART driver. * * $Id: hci_uart.c,v 1.1 2002/04/11 14:26:59 atp Exp $ */ #define VERSION "1.0" #include <linux/config.h> #include <linux/module.h> #include <linux/version.h> #include <linux/config.h> #include <linux/kernel.h> #include <linux/init.h> #include <linux/sched.h> #include <linux/types.h> #include <linux/fcntl.h> #include <linux/interrupt.h> #include <linux/ptrace.h> #include <linux/poll.h> #include <linux/slab.h> #include <linux/tty.h> #include <linux/errno.h> #include <linux/string.h> #include <linux/signal.h> #include <linux/ioctl.h> #include <linux/skbuff.h> #include <net/bluetooth/bluetooth.h> #include <net/bluetooth/bluez.h> #include <net/bluetooth/hci_core.h> #include <net/bluetooth/hci_uart.h> #ifndef HCI_UART_DEBUG #undef DBG #define DBG( A... ) #undef DMP #define DMP( A... ) #endif /* ------- Interface to HCI layer ------ */ /* Initialize device */ int n_hci_open(struct hci_dev *hdev) { DBG("%s %p", hdev->name, hdev); /* Nothing to do for UART driver */ hdev->flags |= HCI_RUNNING; return 0; } /* Reset device */ int n_hci_flush(struct hci_dev *hdev) { struct n_hci *n_hci = (struct n_hci *) hdev->driver_data; struct tty_struct *tty = n_hci->tty; DBG("hdev %p tty %p", hdev, tty); /* Drop TX queue */ skb_queue_purge(&n_hci->txq); /* Flush any pending characters in the driver and discipline. */ if (tty->ldisc.flush_buffer) tty->ldisc.flush_buffer(tty); if (tty->driver.flush_buffer) tty->driver.flush_buffer(tty); return 0; } /* Close device */ int n_hci_close(struct hci_dev *hdev) { DBG("hdev %p", hdev); hdev->flags &= ~HCI_RUNNING; n_hci_flush(hdev); return 0; } int n_hci_tx_wakeup(struct n_hci *n_hci) { register struct tty_struct *tty = n_hci->tty; if (test_and_set_bit(TRANS_SENDING, &n_hci->tx_state)) { set_bit(TRANS_WAKEUP, &n_hci->tx_state); return 0; } DBG(""); do { register struct sk_buff *skb; register int len; clear_bit(TRANS_WAKEUP, &n_hci->tx_state); if (!(skb = skb_dequeue(&n_hci->txq))) break; DMP(skb->data, skb->len); /* Send frame to TTY driver */ tty->flags |= (1 << TTY_DO_WRITE_WAKEUP); len = tty->driver.write(tty, 0, skb->data, skb->len); n_hci->hdev.stat.byte_tx += len; DBG("sent %d", len); if (len == skb->len) { /* Full frame was sent */ kfree_skb(skb); } else { /* Subtract sent part and requeue */ skb_pull(skb, len); skb_queue_head(&n_hci->txq, skb); } } while (test_bit(TRANS_WAKEUP, &n_hci->tx_state)); clear_bit(TRANS_SENDING, &n_hci->tx_state); return 0; } /* Send frames from HCI layer */ int n_hci_send_frame(struct sk_buff *skb) { struct hci_dev* hdev = (struct hci_dev *) skb->dev; struct tty_struct *tty; struct n_hci *n_hci; if (!hdev) { ERR("Frame for uknown device (hdev=NULL)"); return -ENODEV; } if (!(hdev->flags & HCI_RUNNING)) return -EBUSY; n_hci = (struct n_hci *) hdev->driver_data; tty = n_hci2tty(n_hci); DBG("%s: type %d len %d", hdev->name, skb->pkt_type, skb->len); switch (skb->pkt_type) { case HCI_COMMAND_PKT: hdev->stat.cmd_tx++; break; case HCI_ACLDATA_PKT: hdev->stat.acl_tx++; break; case HCI_SCODATA_PKT: hdev->stat.cmd_tx++; break; }; /* Prepend skb with frame type and queue */ memcpy(skb_push(skb, 1), &skb->pkt_type, 1); skb_queue_tail(&n_hci->txq, skb); n_hci_tx_wakeup(n_hci); return 0; } /* ------ LDISC part ------ */ /* n_hci_tty_open * * Called when line discipline changed to N_HCI. * * Arguments: * tty pointer to tty info structure * Return Value: * 0 if success, otherwise error code */ static int n_hci_tty_open(struct tty_struct *tty) { struct n_hci *n_hci = tty2n_hci(tty); struct hci_dev *hdev; DBG("tty %p", tty); if (n_hci) return -EEXIST; if (!(n_hci = kmalloc(sizeof(struct n_hci), GFP_KERNEL))) { ERR("Can't allocate controll structure"); return -ENFILE; } memset(n_hci, 0, sizeof(struct n_hci)); /* Initialize and register HCI device */ hdev = &n_hci->hdev; hdev->type = HCI_UART; hdev->driver_data = n_hci; hdev->open = n_hci_open; hdev->close = n_hci_close; hdev->flush = n_hci_flush; hdev->send = n_hci_send_frame; if (hci_register_dev(hdev) < 0) { ERR("Can't register HCI device %s", hdev->name); kfree(n_hci); return -ENODEV; } tty->disc_data = n_hci; n_hci->tty = tty; spin_lock_init(&n_hci->rx_lock); n_hci->rx_state = WAIT_PACKET_TYPE; skb_queue_head_init(&n_hci->txq); MOD_INC_USE_COUNT; /* Flush any pending characters in the driver and discipline. */ if (tty->ldisc.flush_buffer) tty->ldisc.flush_buffer(tty); if (tty->driver.flush_buffer) tty->driver.flush_buffer(tty); return 0; } /* n_hci_tty_close() * * Called when the line discipline is changed to something * else, the tty is closed, or the tty detects a hangup. */ static void n_hci_tty_close(struct tty_struct *tty) { struct n_hci *n_hci = tty2n_hci(tty); struct hci_dev *hdev = &n_hci->hdev; DBG("tty %p hdev %p", tty, hdev); if (n_hci != NULL) { n_hci_close(hdev); if (hci_unregister_dev(hdev) < 0) { ERR("Can't unregister HCI device %s",hdev->name); } hdev->driver_data = NULL; tty->disc_data = NULL; kfree(n_hci); MOD_DEC_USE_COUNT; } } /* n_hci_tty_wakeup() * * Callback for transmit wakeup. Called when low level * device driver can accept more send data. * * Arguments: tty pointer to associated tty instance data * Return Value: None */ static void n_hci_tty_wakeup( struct tty_struct *tty ) { struct n_hci *n_hci = tty2n_hci(tty); DBG(""); if (!n_hci) return; tty->flags &= ~(1 << TTY_DO_WRITE_WAKEUP); if (tty != n_hci->tty) return; n_hci_tx_wakeup(n_hci); } /* n_hci_tty_room() * * Callback function from tty driver. Return the amount of * space left in the receiver's buffer to decide if remote * transmitter is to be throttled. * * Arguments: tty pointer to associated tty instance data * Return Value: number of bytes left in receive buffer */ static int n_hci_tty_room (struct tty_struct *tty) { return 65536; } static inline int n_hci_check_data_len(struct n_hci *n_hci, int len) { register int room = skb_tailroom(n_hci->rx_skb); DBG("len %d room %d", len, room); if (!len) { DMP(n_hci->rx_skb->data, n_hci->rx_skb->len); hci_recv_frame(n_hci->rx_skb); } else if (len > room) { ERR("Data length is to large"); kfree_skb(n_hci->rx_skb); n_hci->hdev.stat.err_rx++; } else { n_hci->rx_state = WAIT_DATA; n_hci->rx_count = len; return len; } n_hci->rx_state = WAIT_PACKET_TYPE; n_hci->rx_skb = NULL; n_hci->rx_count = 0; return 0; } static inline void n_hci_rx(struct n_hci *n_hci, const __u8 * data, char *flags, int count) { register const char *ptr; hci_event_hdr *eh; hci_acl_hdr *ah; hci_sco_hdr *sh; register int len, type, dlen; DBG("count %d state %ld rx_count %ld", count, n_hci->rx_state, n_hci->rx_count); n_hci->hdev.stat.byte_rx += count; ptr = data; while (count) { if (n_hci->rx_count) { len = MIN(n_hci->rx_count, count); memcpy(skb_put(n_hci->rx_skb, len), ptr, len); n_hci->rx_count -= len; count -= len; ptr += len; if (n_hci->rx_count) continue; switch (n_hci->rx_state) { case WAIT_DATA: DBG("Complete data"); DMP(n_hci->rx_skb->data, n_hci->rx_skb->len); hci_recv_frame(n_hci->rx_skb); n_hci->rx_state = WAIT_PACKET_TYPE; n_hci->rx_skb = NULL; continue; case WAIT_EVENT_HDR: eh = (hci_event_hdr *) n_hci->rx_skb->data; DBG("Event header: evt 0x%2.2x plen %d", eh->evt, eh->plen); n_hci_check_data_len(n_hci, eh->plen); continue; case WAIT_ACL_HDR: ah = (hci_acl_hdr *) n_hci->rx_skb->data; dlen = __le16_to_cpu(ah->dlen); DBG("ACL header: dlen %d", dlen); n_hci_check_data_len(n_hci, dlen); continue; case WAIT_SCO_HDR: sh = (hci_sco_hdr *) n_hci->rx_skb->data; DBG("SCO header: dlen %d", sh->dlen); n_hci_check_data_len(n_hci, sh->dlen); continue; }; } /* WAIT_PACKET_TYPE */ switch (*ptr) { case HCI_EVENT_PKT: DBG("Event packet"); n_hci->rx_state = WAIT_EVENT_HDR; n_hci->rx_count = HCI_EVENT_HDR_SIZE; type = HCI_EVENT_PKT; break; case HCI_ACLDATA_PKT: DBG("ACL packet"); n_hci->rx_state = WAIT_ACL_HDR; n_hci->rx_count = HCI_ACL_HDR_SIZE; type = HCI_ACLDATA_PKT; break; case HCI_SCODATA_PKT: DBG("SCO packet"); n_hci->rx_state = WAIT_SCO_HDR; n_hci->rx_count = HCI_SCO_HDR_SIZE; type = HCI_SCODATA_PKT; break; default: ERR("Unknown HCI packet type %2.2x", (__u8)*ptr); n_hci->hdev.stat.err_rx++; ptr++; count--; continue; }; ptr++; count--; /* Allocate packet */ if (!(n_hci->rx_skb = bluez_skb_alloc(HCI_MAX_FRAME_SIZE, GFP_ATOMIC))) { ERR("Can't allocate mem for new packet"); n_hci->rx_state = WAIT_PACKET_TYPE; n_hci->rx_count = 0; return; } n_hci->rx_skb->dev = (void *) &n_hci->hdev; n_hci->rx_skb->pkt_type = type; } } /* n_hci_tty_receive() * * Called by tty low level driver when receive data is * available. * * Arguments: tty pointer to tty isntance data * data pointer to received data * flags pointer to flags for data * count count of received data in bytes * * Return Value: None */ static void n_hci_tty_receive(struct tty_struct *tty, const __u8 * data, char *flags, int count) { struct n_hci *n_hci = tty2n_hci(tty); if (!n_hci || tty != n_hci->tty) return; spin_lock(&n_hci->rx_lock); n_hci_rx(n_hci, data, flags, count); spin_unlock(&n_hci->rx_lock); if (test_and_clear_bit(TTY_THROTTLED,&tty->flags) && tty->driver.unthrottle) tty->driver.unthrottle(tty); } /* n_hci_tty_ioctl() * * Process IOCTL system call for the tty device. * * Arguments: * * tty pointer to tty instance data * file pointer to open file object for device * cmd IOCTL command code * arg argument for IOCTL call (cmd dependent) * * Return Value: Command dependent */ static int n_hci_tty_ioctl (struct tty_struct *tty, struct file * file, unsigned int cmd, unsigned long arg) { struct n_hci *n_hci = tty2n_hci(tty); int error = 0; DBG(""); /* Verify the status of the device */ if (!n_hci) return -EBADF; switch (cmd) { default: error = n_tty_ioctl(tty, file, cmd, arg); break; }; return error; } /* * We don't provide read/write/poll interface for user space. */ static ssize_t n_hci_tty_read(struct tty_struct *tty, struct file *file, unsigned char *buf, size_t nr) { return 0; } static ssize_t n_hci_tty_write(struct tty_struct *tty, struct file *file, const unsigned char *data, size_t count) { return 0; } static unsigned int n_hci_tty_poll(struct tty_struct *tty, struct file *filp, poll_table *wait) { return 0; } int __init n_hci_init(void) { static struct tty_ldisc n_hci_ldisc; int err; INF("BlueZ HCI UART driver ver %s Copyright (C) 2000,2001 Qualcomm Inc", VERSION); INF("Written 2000,2001 by Maxim Krasnyansky <ma...@qu...>"); /* Register the tty discipline */ memset(&n_hci_ldisc, 0, sizeof (n_hci_ldisc)); n_hci_ldisc.magic = TTY_LDISC_MAGIC; n_hci_ldisc.name = "n_hci"; n_hci_ldisc.open = n_hci_tty_open; n_hci_ldisc.close = n_hci_tty_close; n_hci_ldisc.read = n_hci_tty_read; n_hci_ldisc.write = n_hci_tty_write; n_hci_ldisc.ioctl = n_hci_tty_ioctl; n_hci_ldisc.poll = n_hci_tty_poll; n_hci_ldisc.receive_room= n_hci_tty_room; n_hci_ldisc.receive_buf = n_hci_tty_receive; n_hci_ldisc.write_wakeup= n_hci_tty_wakeup; if ((err = tty_register_ldisc(N_HCI, &n_hci_ldisc))) { ERR("Can't register HCI line discipline (%d)", err); return err; } return 0; } void n_hci_cleanup(void) { int err; /* Release tty registration of line discipline */ if ((err = tty_register_ldisc(N_HCI, NULL))) ERR("Can't unregister HCI line discipline (%d)", err); } module_init(n_hci_init); module_exit(n_hci_cleanup); MODULE_AUTHOR("Maxim Krasnyansky <ma...@qu...>"); MODULE_DESCRIPTION("BlueZ HCI UART driver ver " VERSION); MODULE_LICENSE("GPL"); --- NEW FILE --- /* BlueZ - Bluetooth protocol stack for Linux Copyright (C) 2000-2001 Qualcomm Incorporated Written 2000,2001 by Maxim Krasnyansky <ma...@qu...> This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License version 2 as published by the Free Software Foundation; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS SOFTWARE IS DISCLAIMED. */ /* * BlueZ HCI USB driver. * Based on original USB Bluetooth driver for Linux kernel * Copyright (c) 2000 Greg Kroah-Hartman <gr...@kr...> * Copyright (c) 2000 Mark Douglas Corner <mc...@um...> * * $Id: hci_usb.c,v 1.1 2002/04/11 14:26:59 atp Exp $ */ #define VERSION "1.0" #include <linux/config.h> #include <linux/module.h> #include <linux/version.h> #include <linux/config.h> #include <linux/kernel.h> #include <linux/init.h> #include <linux/sched.h> #include <linux/types.h> #include <linux/fcntl.h> #include <linux/interrupt.h> #include <linux/ptrace.h> #include <linux/poll.h> #include <linux/slab.h> #include <linux/tty.h> #include <linux/errno.h> #include <linux/string.h> #include <linux/signal.h> #include <linux/ioctl.h> #include <linux/skbuff.h> #include <linux/usb.h> #include <net/bluetooth/bluetooth.h> #include <net/bluetooth/bluez.h> #include <net/bluetooth/hci_core.h> #include <net/bluetooth/hci_usb.h> #ifndef HCI_USB_DEBUG #undef DBG #define DBG( A... ) #undef DMP #define DMP( A... ) #endif static struct usb_device_id usb_bluetooth_ids [] = { { USB_DEVICE_INFO(HCI_DEV_CLASS, HCI_DEV_SUBCLASS, HCI_DEV_PROTOCOL) }, { } /* Terminating entry */ }; MODULE_DEVICE_TABLE (usb, usb_bluetooth_ids); static int hci_usb_ctrl_msg(struct hci_usb *husb, struct sk_buff *skb); static int hci_usb_write_msg(struct hci_usb *husb, struct sk_buff *skb); static void hci_usb_unlink_urbs(struct hci_usb *husb) { usb_unlink_urb(husb->read_urb); usb_unlink_urb(husb->intr_urb); usb_unlink_urb(husb->ctrl_urb); usb_unlink_urb(husb->write_urb); } static void hci_usb_free_bufs(struct hci_usb *husb) { if (husb->read_urb) { if (husb->read_urb->transfer_buffer) kfree(husb->read_urb->transfer_buffer); usb_free_urb(husb->read_urb); } if (husb->intr_urb) { if (husb->intr_urb->transfer_buffer) kfree(husb->intr_urb->transfer_buffer); usb_free_urb(husb->intr_urb); } if (husb->ctrl_urb) usb_free_urb(husb->ctrl_urb); if (husb->write_urb) usb_free_urb(husb->write_urb); if (husb->intr_skb) kfree_skb(husb->intr_skb); } /* ------- Interface to HCI layer ------ */ /* Initialize device */ int hci_usb_open(struct hci_dev *hdev) { struct hci_usb *husb = (struct hci_usb *) hdev->driver_data; int status; DBG("%s", hdev->name); husb->read_urb->dev = husb->udev; if ((status = usb_submit_urb(husb->read_urb))) DBG("read submit failed. %d", status); husb->intr_urb->dev = husb->udev; if ((status = usb_submit_urb(husb->intr_urb))) DBG("interrupt submit failed. %d", status); hdev->flags |= HCI_RUNNING; return 0; } /* Reset device */ int hci_usb_flush(struct hci_dev *hdev) { struct hci_usb *husb = (struct hci_usb *) hdev->driver_data; DBG("%s", hdev->name); /* Drop TX queues */ skb_queue_purge(&husb->tx_ctrl_q); skb_queue_purge(&husb->tx_write_q); return 0; } /* Close device */ int hci_usb_close(struct hci_dev *hdev) { struct hci_usb *husb = (struct hci_usb *) hdev->driver_data; DBG("%s", hdev->name); hdev->flags &= ~HCI_RUNNING; hci_usb_unlink_urbs(husb); hci_usb_flush(hdev); return 0; } void hci_usb_ctrl_wakeup(struct hci_usb *husb) { struct sk_buff *skb; if (test_and_set_bit(HCI_TX_CTRL, &husb->tx_state)) return; DBG("%s", husb->hdev.name); if (!(skb = skb_dequeue(&husb->tx_ctrl_q))) goto done; if (hci_usb_ctrl_msg(husb, skb)){ kfree_skb(skb); goto done; } DMP(skb->data, skb->len); husb->hdev.stat.byte_tx += skb->len; return; done: clear_bit(HCI_TX_CTRL, &husb->tx_state); return; } void hci_usb_write_wakeup(struct hci_usb *husb) { struct sk_buff *skb; if (test_and_set_bit(HCI_TX_WRITE, &husb->tx_state)) return; DBG("%s", husb->hdev.name); if (!(skb = skb_dequeue(&husb->tx_write_q))) goto done; if (hci_usb_write_msg(husb, skb)) { skb_queue_head(&husb->tx_write_q, skb); goto done; } DMP(skb->data, skb->len); husb->hdev.stat.byte_tx += skb->len; return; done: clear_bit(HCI_TX_WRITE, &husb->tx_state); return; } /* Send frames from HCI layer */ int hci_usb_send_frame(struct sk_buff *skb) { struct hci_dev *hdev = (struct hci_dev *) skb->dev; struct hci_usb *husb; if (!hdev) { ERR("frame for uknown device (hdev=NULL)"); return -ENODEV; } if (!(hdev->flags & HCI_RUNNING)) return 0; husb = (struct hci_usb *) hdev->driver_data; DBG("%s type %d len %d", hdev->name, skb->pkt_type, skb->len); switch (skb->pkt_type) { case HCI_COMMAND_PKT: skb_queue_tail(&husb->tx_ctrl_q, skb); hci_usb_ctrl_wakeup(husb); hdev->stat.cmd_tx++; return 0; case HCI_ACLDATA_PKT: skb_queue_tail(&husb->tx_write_q, skb); hci_usb_write_wakeup(husb); hdev->stat.acl_tx++; return 0; case HCI_SCODATA_PKT: return -EOPNOTSUPP; }; return 0; } /* ---------- USB ------------- */ static void hci_usb_ctrl(struct urb *urb) { struct sk_buff *skb = (struct sk_buff *) urb->context; struct hci_dev *hdev; struct hci_usb *husb; if (!skb) return; hdev = (struct hci_dev *) skb->dev; husb = (struct hci_usb *) hdev->driver_data; DBG("%s", hdev->name); if (urb->status) DBG("%s ctrl status: %d", hdev->name, urb->status); clear_bit(HCI_TX_CTRL, &husb->tx_state); kfree_skb(skb); /* Wake up device */ hci_usb_ctrl_wakeup(husb); } static void hci_usb_bulk_write(struct urb *urb) { struct sk_buff *skb = (struct sk_buff *) urb->context; struct hci_dev *hdev; struct hci_usb *husb; if (!skb) return; hdev = (struct hci_dev *) skb->dev; husb = (struct hci_usb *) hdev->driver_data; DBG("%s", hdev->name); if (urb->status) DBG("%s bulk write status: %d", hdev->name, urb->status); clear_bit(HCI_TX_WRITE, &husb->tx_state); kfree_skb(skb); /* Wake up device */ hci_usb_write_wakeup(husb); return; } static void hci_usb_intr(struct urb *urb) { struct hci_usb *husb = (struct hci_usb *) urb->context; unsigned char *data = urb->transfer_buffer; register int count = urb->actual_length; register struct sk_buff *skb = husb->intr_skb; hci_event_hdr *eh; register int len; if (!husb) return; DBG("%s count %d", husb->hdev.name, count); if (urb->status || !count) { DBG("%s intr status %d, count %d", husb->hdev.name, urb->status, count); return; } /* Do we really have to handle continuations here ? */ if (!skb) { /* New frame */ if (count < HCI_EVENT_HDR_SIZE) { DBG("%s bad frame len %d", husb->hdev.name, count); return; } eh = (hci_event_hdr *) data; len = eh->plen + HCI_EVENT_HDR_SIZE; if (count > len) { DBG("%s corrupted frame, len %d", husb->hdev.name, count); return; } /* Allocate skb */ if (!(skb = bluez_skb_alloc(len, GFP_ATOMIC))) { ERR("Can't allocate mem for new packet"); return; } skb->dev = (void *) &husb->hdev; skb->pkt_type = HCI_EVENT_PKT; husb->intr_skb = skb; husb->intr_count = len; } else { /* Continuation */ if (count > husb->intr_count) { ERR("%s bad frame len %d (expected %d)", husb->hdev.name, count, husb->intr_count); kfree_skb(skb); husb->intr_skb = NULL; husb->intr_count = 0; return; } } memcpy(skb_put(skb, count), data, count); husb->intr_count -= count; DMP(data, count); if (!husb->intr_count) { /* Got complete frame */ husb->hdev.stat.byte_rx += skb->len; hci_recv_frame(skb); husb->intr_skb = NULL; } } static void hci_usb_bulk_read(struct urb *urb) { struct hci_usb *husb = (struct hci_usb *) urb->context; unsigned char *data = urb->transfer_buffer; int count = urb->actual_length, status; struct sk_buff *skb; hci_acl_hdr *ah; register __u16 dlen; if (!husb) return; DBG("%s status %d, count %d, flags %x", husb->hdev.name, urb->status, count, urb->transfer_flags); if (urb->status) { /* Do not re-submit URB on critical errors */ switch (urb->status) { case -ENOENT: return; default: goto resubmit; }; } if (!count) goto resubmit; DMP(data, count); ah = (hci_acl_hdr *) data; dlen = le16_to_cpu(ah->dlen); /* Verify frame len and completeness */ if ((count - HCI_ACL_HDR_SIZE) != dlen) { ERR("%s corrupted ACL packet: count %d, plen %d", husb->hdev.name, count, dlen); goto resubmit; } /* Allocate packet */ if (!(skb = bluez_skb_alloc(count, GFP_ATOMIC))) { ERR("Can't allocate mem for new packet"); goto resubmit; } memcpy(skb_put(skb, count), data, count); skb->dev = (void *) &husb->hdev; skb->pkt_type = HCI_ACLDATA_PKT; husb->hdev.stat.byte_rx += skb->len; hci_recv_frame(skb); resubmit: husb->read_urb->dev = husb->udev; if ((status = usb_submit_urb(husb->read_urb))) DBG("%s read URB submit failed %d", husb->hdev.name, status); DBG("%s read URB re-submited", husb->hdev.name); } static int hci_usb_ctrl_msg(struct hci_usb *husb, struct sk_buff *skb) { struct urb *urb = husb->ctrl_urb; devrequest *dr = &husb->dev_req; int pipe, status; DBG("%s len %d", husb->hdev.name, skb->len); pipe = usb_sndctrlpipe(husb->udev, 0); dr->requesttype = HCI_CTRL_REQ; dr->request = 0; dr->index = 0; dr->value = 0; dr->length = cpu_to_le16(skb->len); FILL_CONTROL_URB(urb, husb->udev, pipe, (void*)dr, skb->data, skb->len, hci_usb_ctrl, skb); if ((status = usb_submit_urb(urb))) { DBG("%s control URB submit failed %d", husb->hdev.name, status); return status; } return 0; } static int hci_usb_write_msg(struct hci_usb *husb, struct sk_buff *skb) { struct urb *urb = husb->write_urb; int pipe, status; DBG("%s len %d", husb->hdev.name, skb->len); pipe = usb_sndbulkpipe(husb->udev, husb->bulk_out_ep_addr); FILL_BULK_URB(urb, husb->udev, pipe, skb->data, skb->len, hci_usb_bulk_write, skb); urb->transfer_flags |= USB_QUEUE_BULK; if ((status = usb_submit_urb(urb))) { DBG("%s write URB submit failed %d", husb->hdev.name, status); return status; } return 0; } static void * hci_usb_probe(struct usb_device *udev, unsigned int ifnum, const struct usb_device_id *id) { struct usb_endpoint_descriptor *bulk_out_ep, *intr_in_ep, *bulk_in_ep; struct usb_interface_descriptor *uif; struct usb_endpoint_descriptor *ep; struct hci_usb *husb; struct hci_dev *hdev; int i, size, pipe; __u8 * buf; DBG("udev %p ifnum %d", udev, ifnum); /* Check device signature */ if ((udev->descriptor.bDeviceClass != HCI_DEV_CLASS) || (udev->descriptor.bDeviceSubClass != HCI_DEV_SUBCLASS)|| (udev->descriptor.bDeviceProtocol != HCI_DEV_PROTOCOL) ) return NULL; MOD_INC_USE_COUNT; uif = &udev->actconfig->interface[ifnum].altsetting[0]; if (uif->bNumEndpoints != 3) { DBG("Wrong number of endpoints %d", uif->bNumEndpoints); MOD_DEC_USE_COUNT; return NULL; } bulk_out_ep = intr_in_ep = bulk_in_ep = NULL; /* Find endpoints that we need */ for ( i = 0; i < uif->bNumEndpoints; ++i) { ep = &uif->endpoint[i]; switch (ep->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) { case USB_ENDPOINT_XFER_BULK: if (ep->bEndpointAddress & USB_DIR_IN) bulk_in_ep = ep; else bulk_out_ep = ep; break; case USB_ENDPOINT_XFER_INT: intr_in_ep = ep; break; }; } if (!bulk_in_ep || !bulk_out_ep || !intr_in_ep) { DBG("Endpoints not found: %p %p %p", bulk_in_ep, bulk_out_ep, intr_in_ep); MOD_DEC_USE_COUNT; return NULL; } if (!(husb = kmalloc(sizeof(struct hci_usb), GFP_KERNEL))) { ERR("Can't allocate: control structure"); MOD_DEC_USE_COUNT; return NULL; } memset(husb, 0, sizeof(struct hci_usb)); husb->udev = udev; husb->bulk_out_ep_addr = bulk_out_ep->bEndpointAddress; if (!(husb->ctrl_urb = usb_alloc_urb(0))) { ERR("Can't allocate: control URB"); goto probe_error; } if (!(husb->write_urb = usb_alloc_urb(0))) { ERR("Can't allocate: write URB"); goto probe_error; } if (!(husb->read_urb = usb_alloc_urb(0))) { ERR("Can't allocate: read URB"); goto probe_error; } ep = bulk_in_ep; pipe = usb_rcvbulkpipe(udev, ep->bEndpointAddress); size = HCI_MAX_FRAME_SIZE; if (!(buf = kmalloc(size, GFP_KERNEL))) { ERR("Can't allocate: read buffer"); goto probe_error; } FILL_BULK_URB(husb->read_urb, udev, pipe, buf, size, hci_usb_bulk_read, husb); husb->read_urb->transfer_flags |= USB_QUEUE_BULK; ep = intr_in_ep; pipe = usb_rcvintpipe(udev, ep->bEndpointAddress); size = usb_maxpacket(udev, pipe, usb_pipeout(pipe)); if (!(husb->intr_urb = usb_alloc_urb(0))) { ERR("Can't allocate: interrupt URB"); goto probe_error; } if (!(buf = kmalloc(size, GFP_KERNEL))) { ERR("Can't allocate: interrupt buffer"); goto probe_error; } FILL_INT_URB(husb->intr_urb, udev, pipe, buf, size, hci_usb_intr, husb, ep->bInterval); skb_queue_head_init(&husb->tx_ctrl_q); skb_queue_head_init(&husb->tx_write_q); /* Initialize and register HCI device */ hdev = &husb->hdev; hdev->type = HCI_USB; hdev->driver_data = husb; hdev->open = hci_usb_open; hdev->close = hci_usb_close; hdev->flush = hci_usb_flush; hdev->send = hci_usb_send_frame; if (hci_register_dev(hdev) < 0) { ERR("Can't register HCI device %s", hdev->name); goto probe_error; } return husb; probe_error: hci_usb_free_bufs(husb); kfree(husb); MOD_DEC_USE_COUNT; return NULL; } static void hci_usb_disconnect(struct usb_device *udev, void *ptr) { struct hci_usb *husb = (struct hci_usb *) ptr; struct hci_dev *hdev = &husb->hdev; if (!husb) return; DBG("%s", hdev->name); hci_usb_close(hdev); if (hci_unregister_dev(hdev) < 0) { ERR("Can't unregister HCI device %s", hdev->name); } hci_usb_free_bufs(husb); kfree(husb); MOD_DEC_USE_COUNT; } static struct usb_driver hci_usb_driver = { name: "hci_usb", probe: hci_usb_probe, disconnect: hci_usb_disconnect, id_table: usb_bluetooth_ids, }; int hci_usb_init(void) { int err; INF("BlueZ HCI USB driver ver %s Copyright (C) 2000,2001 Qualcomm Inc", VERSION); INF("Written 2000,2001 by Maxim Krasnyansky <ma...@qu...>"); if ((err = usb_register(&hci_usb_driver)) < 0) ERR("Failed to register HCI USB driver"); return err; } void hci_usb_cleanup(void) { usb_deregister(&hci_usb_driver); } module_init(hci_usb_init); module_exit(hci_usb_cleanup); MODULE_AUTHOR("Maxim Krasnyansky <ma...@qu...>"); MODULE_DESCRIPTION("BlueZ HCI USB driver ver " VERSION); MODULE_LICENSE("GPL"); --- NEW FILE --- /* BlueZ - Bluetooth protocol stack for Linux Copyright (C) 2000-2001 Qualcomm Incorporated Written 2000,2001 by Maxim Krasnyansky <ma...@qu...> This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License version 2 as published by the Free Software Foundation; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS SOFTWARE IS DISCLAIMED. */ /* * BlueZ HCI virtual device driver. * * $Id: hci_vhci.c,v 1.1 2002/04/11 14:26:59 atp Exp $ */ #define VERSION "1.0" #include <linux/config.h> #include <linux/module.h> #include <linux/errno.h> #include <linux/kernel.h> #include <linux/major.h> #include <linux/sched.h> #include <linux/slab.h> #include <linux/poll.h> #include <linux/fcntl.h> #include <linux/init.h> #include <linux/random.h> #include <linux/skbuff.h> #include <linux/miscdevice.h> #include <asm/system.h> #include <asm/uaccess.h> #include <net/bluetooth/bluetooth.h> #include <net/bluetooth/bluez.h> #include <net/bluetooth/hci_core.h> #include <net/bluetooth/hci_vhci.h> /* HCI device part */ int hci_vhci_open(struct hci_dev *hdev) { hdev->flags |= HCI_RUNNING; return 0; } int hci_vhci_flush(struct hci_dev *hdev) { struct hci_vhci_struct *hci_vhci = (struct hci_vhci_struct *) hdev->driver_data; skb_queue_purge(&hci_vhci->readq); return 0; } int hci_vhci_close(struct hci_dev *hdev) { hdev->flags &= ~HCI_RUNNING; hci_vhci_flush(hdev); return 0; } int hci_vhci_send_frame(struct sk_buff *skb) { struct hci_dev* hdev = (struct hci_dev *) skb->dev; struct hci_vhci_struct *hci_vhci; if (!hdev) { ERR("Frame for uknown device (hdev=NULL)"); return -ENODEV; } if (!(hdev->flags & HCI_RUNNING)) return -EBUSY; hci_vhci = (struct hci_vhci_struct *) hdev->driver_data; memcpy(skb_push(skb, 1), &skb->pkt_type, 1); skb_queue_tail(&hci_vhci->readq, skb); if (hci_vhci->flags & VHCI_FASYNC) kill_fasync(&hci_vhci->fasync, SIGIO, POLL_IN); wake_up_interruptible(&hci_vhci->read_wait); return 0; } /* Character device part */ /* Poll */ static unsigned int hci_vhci_chr_poll(struct file *file, poll_table * wait) { struct hci_vhci_struct *hci_vhci = (struct hci_vhci_struct *) file->private_data; poll_wait(file, &hci_vhci->read_wait, wait); if (skb_queue_len(&hci_vhci->readq)) return POLLIN | POLLRDNORM; return POLLOUT | POLLWRNORM; } /* Get packet from user space buffer(already verified) */ static inline ssize_t hci_vhci_get_user(struct hci_vhci_struct *hci_vhci, const char *buf, size_t count) { struct sk_buff *skb; if (count > HCI_MAX_FRAME_SIZE) return -EINVAL; if (!(skb = bluez_skb_alloc(count, GFP_KERNEL))) return -ENOMEM; copy_from_user(skb_put(skb, count), buf, count); skb->dev = (void *) &hci_vhci->hdev; skb->pkt_type = *((__u8 *) skb->data); skb_pull(skb, 1); hci_recv_frame(skb); return count; } /* Write */ static ssize_t hci_vhci_chr_write(struct file * file, const char * buf, size_t count, loff_t *pos) { struct hci_vhci_struct *hci_vhci = (struct hci_vhci_struct *) file->private_data; if (verify_area(VERIFY_READ, buf, count)) return -EFAULT; return hci_vhci_get_user(hci_vhci, buf, count); } /* Put packet to user space buffer(already verified) */ static inline ssize_t hci_vhci_put_user(struct hci_vhci_struct *hci_vhci, struct sk_buff *skb, char *buf, int count) { int len = count, total = 0; char *ptr = buf; len = MIN(skb->len, len); copy_to_user(ptr, skb->data, len); total += len; hci_vhci->hdev.stat.byte_tx += len; switch (skb->pkt_type) { case HCI_COMMAND_PKT: hci_vhci->hdev.stat.cmd_tx++; break; case HCI_ACLDATA_PKT: hci_vhci->hdev.stat.acl_tx++; break; case HCI_SCODATA_PKT: hci_vhci->hdev.stat.cmd_tx++; break; }; return total; } /* Read */ static ssize_t hci_vhci_chr_read(struct file * file, char * buf, size_t count, loff_t *pos) { struct hci_vhci_struct *hci_vhci = (struct hci_vhci_struct *) file->private_data; DECLARE_WAITQUEUE(wait, current); struct sk_buff *skb; ssize_t ret = 0; add_wait_queue(&hci_vhci->read_wait, &wait); while (count) { current->state = TASK_INTERRUPTIBLE; /* Read frames from device queue */ if (!(skb = skb_dequeue(&hci_vhci->readq))) { if (file->f_flags & O_NONBLOCK) { ret = -EAGAIN; break; } if (signal_pending(current)) { ret = -ERESTARTSYS; break; } /* Nothing to read, let's sleep */ schedule(); continue; } if (!verify_area(VERIFY_WRITE, buf, count)) ret = hci_vhci_put_user(hci_vhci, skb, buf, count); else ret = -EFAULT; kfree_skb(skb); break; } current->state = TASK_RUNNING; remove_wait_queue(&hci_vhci->read_wait, &wait); return ret; } static loff_t hci_vhci_chr_lseek(struct file * file, loff_t offset, int origin) { return -ESPIPE; } static int hci_vhci_chr_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg) { return -EINVAL; } static int hci_vhci_chr_fasync(int fd, struct file *file, int on) { struct hci_vhci_struct *hci_vhci = (struct hci_vhci_struct *) file->private_data; int ret; if ((ret = fasync_helper(fd, file, on, &hci_vhci->fasync)) < 0) return ret; if (on) hci_vhci->flags |= VHCI_FASYNC; else hci_vhci->flags &= ~VHCI_FASYNC; return 0; } static int hci_vhci_chr_open(struct inode *inode, struct file * file) { struct hci_vhci_struct *hci_vhci = NULL; struct hci_dev *hdev; if (!(hci_vhci = kmalloc(sizeof(struct hci_vhci_struct), GFP_KERNEL))) return -ENOMEM; memset(hci_vhci, 0, sizeof(struct hci_vhci_struct)); skb_queue_head_init(&hci_vhci->readq); init_waitqueue_head(&hci_vhci->read_wait); /* Initialize and register HCI device */ hdev = &hci_vhci->hdev; hdev->type = HCI_VHCI; hdev->driver_data = hci_vhci; hdev->open = hci_vhci_open; hdev->close = hci_vhci_close; hdev->flush = hci_vhci_flush; hdev->send = hci_vhci_send_frame; if (hci_register_dev(hdev) < 0) { kfree(hci_vhci); return -EBUSY; } file->private_data = hci_vhci; return 0; } static int hci_vhci_chr_close(struct inode *inode, struct file *file) { struct hci_vhci_struct *hci_vhci = (struct hci_vhci_struct *) file->private_data; if (hci_unregister_dev(&hci_vhci->hdev) < 0) { ERR("Can't unregister HCI device %s", hci_vhci->hdev.name); } kfree(hci_vhci); file->private_data = NULL; return 0; } static struct file_operations hci_vhci_fops = { owner: THIS_MODULE, llseek: hci_vhci_chr_lseek, read: hci_vhci_chr_read, write: hci_vhci_chr_write, poll: hci_vhci_chr_poll, ioctl: hci_vhci_chr_ioctl, open: hci_vhci_chr_open, release:hci_vhci_chr_close, fasync: hci_vhci_chr_fasync }; static struct miscdevice hci_vhci_miscdev= { VHCI_MINOR, "hci_vhci", &hci_vhci_fops }; int __init hci_vhci_init(void) { INF("BlueZ VHCI driver ver %s Copyright (C) 2000,2001 Qualcomm Inc", VERSION); INF("Written 2000,2001 by Maxim Krasnyansky <ma...@qu...>"); if (misc_register(&hci_vhci_miscdev)) { ERR("Can't register misc device %d\n", VHCI_MINOR); return -EIO; } return 0; } void hci_vhci_cleanup(void) { misc_deregister(&hci_vhci_miscdev); } module_init(hci_vhci_init); module_exit(hci_vhci_cleanup); MODULE_AUTHOR("Maxim Krasnyansky <ma...@qu...>"); MODULE_DESCRIPTION("BlueZ VHCI driver ver " VERSION); MODULE_LICENSE("GPL"); |