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

📄 stir4200.c

📁 《linux驱动程序设计从入门到精通》一书中所有的程序代码含驱动和相应的应用程序
💻 C
📖 第 1 页 / 共 2 页
字号:
/******************************************************************************* Filename:      stir4200.c* Version:       0.4* Description:   Irda SigmaTel USB Dongle* Status:        Experimental* Author:        Stephen Hemminger <shemminger@osdl.org>**  	Based on earlier driver by Paul Stewart <stewart@parc.com>**	Copyright (C) 2000, Roman Weissgaerber <weissg@vienna.at>*	Copyright (C) 2001, Dag Brattli <dag@brattli.net>*	Copyright (C) 2001, Jean Tourrilhes <jt@hpl.hp.com>*	Copyright (C) 2004, Stephen Hemminger <shemminger@osdl.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.**	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., 675 Mass Ave, Cambridge, MA 02139, USA.******************************************************************************//* * This dongle does no framing, and requires polling to receive the * data.  The STIr4200 has bulk in and out endpoints just like * usr-irda devices, but the data it sends and receives is raw; like * irtty, it needs to call the wrap and unwrap functions to add and * remove SOF/BOF and escape characters to/from the frame. */#include <linux/module.h>#include <linux/moduleparam.h>#include <linux/kernel.h>#include <linux/types.h>#include <linux/init.h>#include <linux/time.h>#include <linux/skbuff.h>#include <linux/netdevice.h>#include <linux/slab.h>#include <linux/delay.h>#include <linux/usb.h>#include <linux/crc32.h>#include <linux/kthread.h>#include <linux/freezer.h>#include <net/irda/irda.h>#include <net/irda/irlap.h>#include <net/irda/irda_device.h>#include <net/irda/wrapper.h>#include <net/irda/crc.h>#include <asm/byteorder.h>#include <asm/unaligned.h>MODULE_AUTHOR("Stephen Hemminger <shemminger@linux-foundation.org>");MODULE_DESCRIPTION("IrDA-USB Dongle Driver for SigmaTel STIr4200");MODULE_LICENSE("GPL");static int qos_mtt_bits = 0x07;	/* 1 ms or more */module_param(qos_mtt_bits, int, 0);MODULE_PARM_DESC(qos_mtt_bits, "Minimum Turn Time");static int rx_sensitivity = 1;	/* FIR 0..4, SIR 0..6 */module_param(rx_sensitivity, int, 0);MODULE_PARM_DESC(rx_sensitivity, "Set Receiver sensitivity (0-6, 0 is most sensitive)");static int tx_power = 0;	/* 0 = highest ... 3 = lowest */module_param(tx_power, int, 0);MODULE_PARM_DESC(tx_power, "Set Transmitter power (0-3, 0 is highest power)");#define STIR_IRDA_HEADER  	4#define CTRL_TIMEOUT		100	   /* milliseconds */#define TRANSMIT_TIMEOUT	200	   /* milliseconds */#define STIR_FIFO_SIZE		4096#define FIFO_REGS_SIZE		3enum FirChars {	FIR_CE   = 0x7d,	FIR_XBOF = 0x7f,	FIR_EOF  = 0x7e,};enum StirRequests {	REQ_WRITE_REG =		0x00,	REQ_READ_REG =		0x01,	REQ_READ_ROM =		0x02,	REQ_WRITE_SINGLE =	0x03,};/* Register offsets */enum StirRegs {	REG_RSVD=0,	REG_MODE,	REG_PDCLK,	REG_CTRL1,	REG_CTRL2,	REG_FIFOCTL,	REG_FIFOLSB,	REG_FIFOMSB,	REG_DPLL,	REG_IRDIG,	REG_TEST=15,};enum StirModeMask {	MODE_FIR = 0x80,	MODE_SIR = 0x20,	MODE_ASK = 0x10,	MODE_FASTRX = 0x08,	MODE_FFRSTEN = 0x04,	MODE_NRESET = 0x02,	MODE_2400 = 0x01,};enum StirPdclkMask {	PDCLK_4000000 = 0x02,	PDCLK_115200 = 0x09,	PDCLK_57600 = 0x13,	PDCLK_38400 = 0x1D,	PDCLK_19200 = 0x3B,	PDCLK_9600 = 0x77,	PDCLK_2400 = 0xDF,};enum StirCtrl1Mask {	CTRL1_SDMODE = 0x80,	CTRL1_RXSLOW = 0x40,	CTRL1_TXPWD = 0x10,	CTRL1_RXPWD = 0x08,	CTRL1_SRESET = 0x01,};enum StirCtrl2Mask {	CTRL2_SPWIDTH = 0x08,	CTRL2_REVID = 0x03,};enum StirFifoCtlMask {	FIFOCTL_EOF = 0x80,	FIFOCTL_UNDER = 0x40,	FIFOCTL_OVER = 0x20,	FIFOCTL_DIR = 0x10,	FIFOCTL_CLR = 0x08,	FIFOCTL_EMPTY = 0x04,};enum StirDiagMask {	IRDIG_RXHIGH = 0x80,	IRDIG_RXLOW = 0x40,};enum StirTestMask {	TEST_PLLDOWN = 0x80,	TEST_LOOPIR = 0x40,	TEST_LOOPUSB = 0x20,	TEST_TSTENA = 0x10,	TEST_TSTOSC = 0x0F,};struct stir_cb {        struct usb_device *usbdev;      /* init: probe_irda */        struct net_device *netdev;      /* network layer */        struct irlap_cb   *irlap;       /* The link layer we are binded to */        struct net_device_stats stats;	/* network statistics */        struct qos_info   qos;	unsigned 	  speed;	/* Current speed */        struct task_struct *thread;     /* transmit thread */	struct sk_buff	  *tx_pending;	void		  *io_buf;	/* transmit/receive buffer */	__u8		  *fifo_status;	iobuff_t  	  rx_buff;	/* receive unwrap state machine */	struct timeval	  rx_time;	int		  receiving;	struct urb	 *rx_urb;};/* These are the currently known USB ids */static struct usb_device_id dongles[] = {    /* SigmaTel, Inc,  STIr4200 IrDA/USB Bridge */    { USB_DEVICE(0x066f, 0x4200) },    { }};MODULE_DEVICE_TABLE(usb, dongles);/* Send control message to set dongle register */static int write_reg(struct stir_cb *stir, __u16 reg, __u8 value){	struct usb_device *dev = stir->usbdev;	pr_debug("%s: write reg %d = 0x%x\n",		 stir->netdev->name, reg, value);	return usb_control_msg(dev, usb_sndctrlpipe(dev, 0),			       REQ_WRITE_SINGLE,			       USB_DIR_OUT|USB_TYPE_VENDOR|USB_RECIP_DEVICE,			       value, reg, NULL, 0,			       CTRL_TIMEOUT);}/* Send control message to read multiple registers */static inline int read_reg(struct stir_cb *stir, __u16 reg,		    __u8 *data, __u16 count){	struct usb_device *dev = stir->usbdev;	return usb_control_msg(dev, usb_rcvctrlpipe(dev, 0),			       REQ_READ_REG,			       USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,			       0, reg, data, count,			       CTRL_TIMEOUT);}static inline int isfir(u32 speed){	return (speed == 4000000);}/* * Prepare a FIR IrDA frame for transmission to the USB dongle.  The * FIR transmit frame is documented in the datasheet.  It consists of * a two byte 0x55 0xAA sequence, two little-endian length bytes, a * sequence of exactly 16 XBOF bytes of 0x7E, two BOF bytes of 0x7E, * then the data escaped as follows: * *    0x7D -> 0x7D 0x5D *    0x7E -> 0x7D 0x5E *    0x7F -> 0x7D 0x5F * * Then, 4 bytes of little endian (stuffed) FCS follow, then two * trailing EOF bytes of 0x7E. */static inline __u8 *stuff_fir(__u8 *p, __u8 c){	switch(c) {	case 0x7d:	case 0x7e:	case 0x7f:		*p++ = 0x7d;		c ^= IRDA_TRANS;		/* fall through */	default:		*p++ = c;	}	return p;}/* Take raw data in skb and put it wrapped into buf */static unsigned wrap_fir_skb(const struct sk_buff *skb, __u8 *buf){	__u8 *ptr = buf;	__u32 fcs = ~(crc32_le(~0, skb->data, skb->len));	__u16 wraplen;	int i;	/* Header */	buf[0] = 0x55;	buf[1] = 0xAA;	ptr = buf + STIR_IRDA_HEADER;	memset(ptr, 0x7f, 16);	ptr += 16;	/* BOF */	*ptr++  = 0x7e;	*ptr++  = 0x7e;	/* Address / Control / Information */	for (i = 0; i < skb->len; i++)		ptr = stuff_fir(ptr, skb->data[i]);	/* FCS */	ptr = stuff_fir(ptr, fcs & 0xff);	ptr = stuff_fir(ptr, (fcs >> 8) & 0xff);	ptr = stuff_fir(ptr, (fcs >> 16) & 0xff);	ptr = stuff_fir(ptr, (fcs >> 24) & 0xff);	/* EOFs */	*ptr++ = 0x7e;	*ptr++ = 0x7e;	/* Total length, minus the header */	wraplen = (ptr - buf) - STIR_IRDA_HEADER;	buf[2] = wraplen & 0xff;	buf[3] = (wraplen >> 8) & 0xff;	return wraplen + STIR_IRDA_HEADER;}static unsigned wrap_sir_skb(struct sk_buff *skb, __u8 *buf){	__u16 wraplen;	wraplen = async_wrap_skb(skb, buf + STIR_IRDA_HEADER,				 STIR_FIFO_SIZE - STIR_IRDA_HEADER);	buf[0] = 0x55;	buf[1] = 0xAA;	buf[2] = wraplen & 0xff;	buf[3] = (wraplen >> 8) & 0xff;	return wraplen + STIR_IRDA_HEADER;}/* * Frame is fully formed in the rx_buff so check crc * and pass up to irlap * setup for next receive */static void fir_eof(struct stir_cb *stir){	iobuff_t *rx_buff = &stir->rx_buff;	int len = rx_buff->len - 4;	struct sk_buff *skb, *nskb;	__u32 fcs;	if (unlikely(len <= 0)) {		pr_debug("%s: short frame len %d\n",			 stir->netdev->name, len);		++stir->stats.rx_errors;		++stir->stats.rx_length_errors;		return;	}	fcs = ~(crc32_le(~0, rx_buff->data, len));	if (fcs != le32_to_cpu(get_unaligned((u32 *)(rx_buff->data+len)))) {		pr_debug("crc error calc 0x%x len %d\n", fcs, len);		stir->stats.rx_errors++;		stir->stats.rx_crc_errors++;		return;	}	/* if frame is short then just copy it */	if (len < IRDA_RX_COPY_THRESHOLD) {		nskb = dev_alloc_skb(len + 1);		if (unlikely(!nskb)) {			++stir->stats.rx_dropped;			return;		}		skb_reserve(nskb, 1);		skb = nskb;		memcpy(nskb->data, rx_buff->data, len);	} else {		nskb = dev_alloc_skb(rx_buff->truesize);		if (unlikely(!nskb)) {			++stir->stats.rx_dropped;			return;		}		skb_reserve(nskb, 1);		skb = rx_buff->skb;		rx_buff->skb = nskb;		rx_buff->head = nskb->data;	}	skb_put(skb, len);	skb->mac.raw  = skb->data;	skb->protocol = htons(ETH_P_IRDA);	skb->dev = stir->netdev;	netif_rx(skb);	stir->stats.rx_packets++;	stir->stats.rx_bytes += len;	rx_buff->data = rx_buff->head;	rx_buff->len = 0;}/* Unwrap FIR stuffed data and bump it to IrLAP */static void stir_fir_chars(struct stir_cb *stir,			    const __u8 *bytes, int len){	iobuff_t *rx_buff = &stir->rx_buff;	int	i;	for (i = 0; i < len; i++) {		__u8	byte = bytes[i];		switch(rx_buff->state) {		case OUTSIDE_FRAME:			/* ignore garbage till start of frame */			if (unlikely(byte != FIR_EOF))				continue;			/* Now receiving frame */			rx_buff->state = BEGIN_FRAME;			/* Time to initialize receive buffer */			rx_buff->data = rx_buff->head;			rx_buff->len = 0;			continue;		case LINK_ESCAPE:			if (byte == FIR_EOF) {				pr_debug("%s: got EOF after escape\n",					 stir->netdev->name);				goto frame_error;			}			rx_buff->state = INSIDE_FRAME;			byte ^= IRDA_TRANS;			break;		case BEGIN_FRAME:			/* ignore multiple BOF/EOF */			if (byte == FIR_EOF)				continue;			rx_buff->state = INSIDE_FRAME;			rx_buff->in_frame = TRUE;			/* fall through */		case INSIDE_FRAME:			switch(byte) {			case FIR_CE:				rx_buff->state = LINK_ESCAPE;				continue;			case FIR_XBOF:				/* 0x7f is not used in this framing */				pr_debug("%s: got XBOF without escape\n",					 stir->netdev->name);				goto frame_error;			case FIR_EOF:				rx_buff->state = OUTSIDE_FRAME;				rx_buff->in_frame = FALSE;				fir_eof(stir);				continue;			}			break;		}		/* add byte to rx buffer */		if (unlikely(rx_buff->len >= rx_buff->truesize)) {			pr_debug("%s: fir frame exceeds %d\n",				 stir->netdev->name, rx_buff->truesize);			++stir->stats.rx_over_errors;			goto error_recovery;		}		rx_buff->data[rx_buff->len++] = byte;		continue;	frame_error:		++stir->stats.rx_frame_errors;	error_recovery:		++stir->stats.rx_errors;		rx_buff->state = OUTSIDE_FRAME;		rx_buff->in_frame = FALSE;	}}/* Unwrap SIR stuffed data and bump it up to IrLAP */static void stir_sir_chars(struct stir_cb *stir,			    const __u8 *bytes, int len){	int i;	for (i = 0; i < len; i++)		async_unwrap_char(stir->netdev, &stir->stats,				  &stir->rx_buff, bytes[i]);}static inline void unwrap_chars(struct stir_cb *stir,				const __u8 *bytes, int length){	if (isfir(stir->speed))		stir_fir_chars(stir, bytes, length);	else		stir_sir_chars(stir, bytes, length);}/* Mode parameters for each speed */static const struct {	unsigned speed;	__u8 pdclk;} stir_modes[] = {        { 2400,    PDCLK_2400 },        { 9600,    PDCLK_9600 },        { 19200,   PDCLK_19200 },        { 38400,   PDCLK_38400 },        { 57600,   PDCLK_57600 },        { 115200,  PDCLK_115200 },        { 4000000, PDCLK_4000000 },};/* * Setup chip for speed. *  Called at startup to initialize the chip *  and on speed changes. * * Note: Write multiple registers doesn't appear to work */static int change_speed(struct stir_cb *stir, unsigned speed){	int i, err;	__u8 mode;	for (i = 0; i < ARRAY_SIZE(stir_modes); ++i) {		if (speed == stir_modes[i].speed)			goto found;	}	warn("%s: invalid speed %d", stir->netdev->name, speed);	return -EINVAL; found:	pr_debug("speed change from %d to %d\n", stir->speed, speed);	/* Reset modulator */	err = write_reg(stir, REG_CTRL1, CTRL1_SRESET);	if (err)		goto out;	/* Undocumented magic to tweak the DPLL */	err = write_reg(stir, REG_DPLL, 0x15);	if (err)		goto out;	/* Set clock */	err = write_reg(stir, REG_PDCLK, stir_modes[i].pdclk);	if (err)		goto out;	mode = MODE_NRESET | MODE_FASTRX;	if (isfir(speed))		mode |= MODE_FIR | MODE_FFRSTEN;	else		mode |= MODE_SIR;	if (speed == 2400)		mode |= MODE_2400;	err = write_reg(stir, REG_MODE, mode);	if (err)		goto out;	/* This resets TEMIC style transceiver if any. */	err = write_reg(stir, REG_CTRL1,			CTRL1_SDMODE | (tx_power & 3) << 1);	if (err)		goto out;	err = write_reg(stir, REG_CTRL1, (tx_power & 3) << 1);	if (err)		goto out;	/* Reset sensitivity */	err = write_reg(stir, REG_CTRL2, (rx_sensitivity & 7) << 5); out:	stir->speed = speed;	return err;}/* * Called from net/core when new frame is available. */static int stir_hard_xmit(struct sk_buff *skb, struct net_device *netdev){	struct stir_cb *stir = netdev_priv(netdev);	netif_stop_queue(netdev);	/* the IRDA wrapping routines don't deal with non linear skb */	SKB_LINEAR_ASSERT(skb);	skb = xchg(&stir->tx_pending, skb);        wake_up_process(stir->thread);		/* this should never happen unless stop/wakeup problem */

⌨️ 快捷键说明

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