⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bfusb.c

📁 嵌入式Linux的蓝牙模块驱动
💻 C
📖 第 1 页 / 共 2 页
字号:
/* * *  AVM BlueFRITZ! USB driver * *  Copyright (C) 2003  Marcel Holtmann <marcel@holtmann.org> * * *  This program is free software; you can redistribute it and/or modify *  it under the terms of the GNU General Public License as published by *  the Free Software Foundation; either version 2 of the License, or *  (at your option) any later version. * *  This program is distributed in the hope that it will be useful, *  but WITHOUT ANY WARRANTY; without even the implied warranty of *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the *  GNU General Public License for more details. * *  You should have received a copy of the GNU General Public License *  along with this program; if not, write to the Free Software *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA * */#include <linux/config.h>#include <linux/module.h>#include <linux/kernel.h>#include <linux/init.h>#include <linux/slab.h>#include <linux/types.h>#include <linux/sched.h>#include <linux/errno.h>#include <linux/skbuff.h>#include <linux/device.h>#include <linux/firmware.h>#include <linux/usb.h>#include <net/bluetooth/bluetooth.h>#include <net/bluetooth/hci_core.h>#ifndef CONFIG_BT_HCIBFUSB_DEBUG#undef  BT_DBG#define BT_DBG(D...)#endif#define VERSION "1.1"static struct usb_driver bfusb_driver;static struct usb_device_id bfusb_table[] = {	/* AVM BlueFRITZ! USB */	{ USB_DEVICE(0x057c, 0x2200) },	{ }	/* Terminating entry */};MODULE_DEVICE_TABLE(usb, bfusb_table);#define BFUSB_MAX_BLOCK_SIZE	256#define BFUSB_BLOCK_TIMEOUT	(HZ * 3)#define BFUSB_TX_PROCESS	1#define BFUSB_TX_WAKEUP		2#define BFUSB_MAX_BULK_TX	2#define BFUSB_MAX_BULK_RX	2struct bfusb {	struct hci_dev		*hdev;	unsigned long		state;	struct usb_device	*udev;	unsigned int		bulk_in_ep;	unsigned int		bulk_out_ep;	unsigned int		bulk_pkt_size;	rwlock_t		lock;	struct sk_buff_head	transmit_q;	struct sk_buff		*reassembly;	atomic_t		pending_tx;	struct sk_buff_head	pending_q;	struct sk_buff_head	completed_q;};struct bfusb_scb {	struct urb *urb;};static void bfusb_tx_complete(struct urb *urb, struct pt_regs *regs);static void bfusb_rx_complete(struct urb *urb, struct pt_regs *regs);static struct urb *bfusb_get_completed(struct bfusb *bfusb){	struct sk_buff *skb;	struct urb *urb = NULL;	BT_DBG("bfusb %p", bfusb);	skb = skb_dequeue(&bfusb->completed_q);	if (skb) {		urb = ((struct bfusb_scb *) skb->cb)->urb;		kfree_skb(skb);	}	return urb;}static void bfusb_unlink_urbs(struct bfusb *bfusb){	struct sk_buff *skb;	struct urb *urb;	BT_DBG("bfusb %p", bfusb);	while ((skb = skb_dequeue(&bfusb->pending_q))) {		urb = ((struct bfusb_scb *) skb->cb)->urb;		usb_unlink_urb(urb);		skb_queue_tail(&bfusb->completed_q, skb);	}	while ((urb = bfusb_get_completed(bfusb)))		usb_free_urb(urb);}static int bfusb_send_bulk(struct bfusb *bfusb, struct sk_buff *skb){	struct bfusb_scb *scb = (void *) skb->cb;	struct urb *urb = bfusb_get_completed(bfusb);	int err, pipe;	BT_DBG("bfusb %p skb %p len %d", bfusb, skb, skb->len);	if (!urb && !(urb = usb_alloc_urb(0, GFP_ATOMIC)))		return -ENOMEM;	pipe = usb_sndbulkpipe(bfusb->udev, bfusb->bulk_out_ep);	usb_fill_bulk_urb(urb, bfusb->udev, pipe, skb->data, skb->len,			bfusb_tx_complete, skb);	scb->urb = urb;	skb_queue_tail(&bfusb->pending_q, skb);	err = usb_submit_urb(urb, GFP_ATOMIC);	if (err) {		BT_ERR("%s bulk tx submit failed urb %p err %d", 					bfusb->hdev->name, urb, err);		skb_unlink(skb);		usb_free_urb(urb);	} else		atomic_inc(&bfusb->pending_tx);	return err;}static void bfusb_tx_wakeup(struct bfusb *bfusb){	struct sk_buff *skb;	BT_DBG("bfusb %p", bfusb);	if (test_and_set_bit(BFUSB_TX_PROCESS, &bfusb->state)) {		set_bit(BFUSB_TX_WAKEUP, &bfusb->state);		return;	}	do {		clear_bit(BFUSB_TX_WAKEUP, &bfusb->state);		while ((atomic_read(&bfusb->pending_tx) < BFUSB_MAX_BULK_TX) &&				(skb = skb_dequeue(&bfusb->transmit_q))) {			if (bfusb_send_bulk(bfusb, skb) < 0) {				skb_queue_head(&bfusb->transmit_q, skb);				break;			}		}	} while (test_bit(BFUSB_TX_WAKEUP, &bfusb->state));	clear_bit(BFUSB_TX_PROCESS, &bfusb->state);}static void bfusb_tx_complete(struct urb *urb, struct pt_regs *regs){	struct sk_buff *skb = (struct sk_buff *) urb->context;	struct bfusb *bfusb = (struct bfusb *) skb->dev;	BT_DBG("bfusb %p urb %p skb %p len %d", bfusb, urb, skb, skb->len);	atomic_dec(&bfusb->pending_tx);	if (!test_bit(HCI_RUNNING, &bfusb->hdev->flags))		return;	if (!urb->status)		bfusb->hdev->stat.byte_tx += skb->len;	else		bfusb->hdev->stat.err_tx++;	read_lock(&bfusb->lock);	skb_unlink(skb);	skb_queue_tail(&bfusb->completed_q, skb);	bfusb_tx_wakeup(bfusb);	read_unlock(&bfusb->lock);}static int bfusb_rx_submit(struct bfusb *bfusb, struct urb *urb){	struct bfusb_scb *scb;	struct sk_buff *skb;	int err, pipe, size = HCI_MAX_FRAME_SIZE + 32;	BT_DBG("bfusb %p urb %p", bfusb, urb);	if (!urb && !(urb = usb_alloc_urb(0, GFP_ATOMIC)))		return -ENOMEM;	if (!(skb = bt_skb_alloc(size, GFP_ATOMIC))) {		usb_free_urb(urb);		return -ENOMEM;	}	skb->dev = (void *) bfusb;	scb = (struct bfusb_scb *) skb->cb;	scb->urb = urb;	pipe = usb_rcvbulkpipe(bfusb->udev, bfusb->bulk_in_ep);	usb_fill_bulk_urb(urb, bfusb->udev, pipe, skb->data, size,			bfusb_rx_complete, skb);	skb_queue_tail(&bfusb->pending_q, skb);	err = usb_submit_urb(urb, GFP_ATOMIC);	if (err) {		BT_ERR("%s bulk rx submit failed urb %p err %d",					bfusb->hdev->name, urb, err);		skb_unlink(skb);		kfree_skb(skb);		usb_free_urb(urb);	}	return err;}static inline int bfusb_recv_block(struct bfusb *bfusb, int hdr, unsigned char *data, int len){	BT_DBG("bfusb %p hdr 0x%02x data %p len %d", bfusb, hdr, data, len);	if (hdr & 0x10) {		BT_ERR("%s error in block", bfusb->hdev->name);		if (bfusb->reassembly)			kfree_skb(bfusb->reassembly);		bfusb->reassembly = NULL;		return -EIO;	}	if (hdr & 0x04) {		struct sk_buff *skb;		unsigned char pkt_type;		int pkt_len = 0;		if (bfusb->reassembly) {			BT_ERR("%s unexpected start block", bfusb->hdev->name);			kfree_skb(bfusb->reassembly);			bfusb->reassembly = NULL;		}		if (len < 1) {			BT_ERR("%s no packet type found", bfusb->hdev->name);			return -EPROTO;		}		pkt_type = *data++; len--;		switch (pkt_type) {		case HCI_EVENT_PKT:			if (len >= HCI_EVENT_HDR_SIZE) {				struct hci_event_hdr *hdr = (struct hci_event_hdr *) data;				pkt_len = HCI_EVENT_HDR_SIZE + hdr->plen;			} else {				BT_ERR("%s event block is too short", bfusb->hdev->name);				return -EILSEQ;			}			break;		case HCI_ACLDATA_PKT:			if (len >= HCI_ACL_HDR_SIZE) {				struct hci_acl_hdr *hdr = (struct hci_acl_hdr *) data;				pkt_len = HCI_ACL_HDR_SIZE + __le16_to_cpu(hdr->dlen);			} else {				BT_ERR("%s data block is too short", bfusb->hdev->name);				return -EILSEQ;			}			break;		case HCI_SCODATA_PKT:			if (len >= HCI_SCO_HDR_SIZE) {				struct hci_sco_hdr *hdr = (struct hci_sco_hdr *) data;				pkt_len = HCI_SCO_HDR_SIZE + hdr->dlen;			} else {				BT_ERR("%s audio block is too short", bfusb->hdev->name);				return -EILSEQ;			}			break;		}		skb = bt_skb_alloc(pkt_len, GFP_ATOMIC);		if (!skb) {			BT_ERR("%s no memory for the packet", bfusb->hdev->name);			return -ENOMEM;		}		skb->dev = (void *) bfusb->hdev;		skb->pkt_type = pkt_type;		bfusb->reassembly = skb;	} else {		if (!bfusb->reassembly) {			BT_ERR("%s unexpected continuation block", bfusb->hdev->name);			return -EIO;		}	}	if (len > 0)		memcpy(skb_put(bfusb->reassembly, len), data, len);	if (hdr & 0x08) {		hci_recv_frame(bfusb->reassembly);		bfusb->reassembly = NULL;	}	return 0;}static void bfusb_rx_complete(struct urb *urb, struct pt_regs *regs){	struct sk_buff *skb = (struct sk_buff *) urb->context;	struct bfusb *bfusb = (struct bfusb *) skb->dev;	unsigned char *buf = urb->transfer_buffer;	int count = urb->actual_length;	int err, hdr, len;	BT_DBG("bfusb %p urb %p skb %p len %d", bfusb, urb, skb, skb->len);	read_lock(&bfusb->lock);	if (!test_bit(HCI_RUNNING, &bfusb->hdev->flags))		goto unlock;	if (urb->status || !count)		goto resubmit;	bfusb->hdev->stat.byte_rx += count;	skb_put(skb, count);	while (count) {		hdr = buf[0] | (buf[1] << 8);		if (hdr & 0x4000) {			len = 0;			count -= 2;			buf   += 2;		} else {			len = (buf[2] == 0) ? 256 : buf[2];			count -= 3;			buf   += 3;		}		if (count < len) {			BT_ERR("%s block extends over URB buffer ranges",					bfusb->hdev->name);		}		if ((hdr & 0xe1) == 0xc1)			bfusb_recv_block(bfusb, hdr, buf, len);		count -= len;		buf   += len;	}	skb_unlink(skb);	kfree_skb(skb);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -