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

📄 sa1100_ir.c

📁 《linux驱动程序设计从入门到精通》一书中所有的程序代码含驱动和相应的应用程序
💻 C
📖 第 1 页 / 共 2 页
字号:
/* *  linux/drivers/net/irda/sa1100_ir.c * *  Copyright (C) 2000-2001 Russell King * * 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. * *  Infra-red driver for the StrongARM SA1100 embedded microprocessor * *  Note that we don't have to worry about the SA1111's DMA bugs in here, *  so we use the straight forward dma_map_* functions with a null pointer. * *  This driver takes one kernel command line parameter, sa1100ir=, with *  the following options: *	max_rate:baudrate	- set the maximum baud rate *	power_leve:level	- set the transmitter power level *	tx_lpm:0|1		- set transmit low power mode */#include <linux/module.h>#include <linux/moduleparam.h>#include <linux/types.h>#include <linux/init.h>#include <linux/errno.h>#include <linux/netdevice.h>#include <linux/slab.h>#include <linux/rtnetlink.h>#include <linux/interrupt.h>#include <linux/delay.h>#include <linux/platform_device.h>#include <linux/dma-mapping.h>#include <net/irda/irda.h>#include <net/irda/wrapper.h>#include <net/irda/irda_device.h>#include <asm/irq.h>#include <asm/dma.h>#include <asm/hardware.h>#include <asm/mach/irda.h>static int power_level = 3;static int tx_lpm;static int max_rate = 4000000;struct sa1100_irda {	unsigned char		hscr0;	unsigned char		utcr4;	unsigned char		power;	unsigned char		open;	int			speed;	int			newspeed;	struct sk_buff		*txskb;	struct sk_buff		*rxskb;	dma_addr_t		txbuf_dma;	dma_addr_t		rxbuf_dma;	dma_regs_t		*txdma;	dma_regs_t		*rxdma;	struct net_device_stats	stats;	struct device		*dev;	struct irda_platform_data *pdata;	struct irlap_cb		*irlap;	struct qos_info		qos;	iobuff_t		tx_buff;	iobuff_t		rx_buff;};#define IS_FIR(si)		((si)->speed >= 4000000)#define HPSIR_MAX_RXLEN		2047/* * Allocate and map the receive buffer, unless it is already allocated. */static int sa1100_irda_rx_alloc(struct sa1100_irda *si){	if (si->rxskb)		return 0;	si->rxskb = alloc_skb(HPSIR_MAX_RXLEN + 1, GFP_ATOMIC);	if (!si->rxskb) {		printk(KERN_ERR "sa1100_ir: out of memory for RX SKB\n");		return -ENOMEM;	}	/*	 * Align any IP headers that may be contained	 * within the frame.	 */	skb_reserve(si->rxskb, 1);	si->rxbuf_dma = dma_map_single(si->dev, si->rxskb->data,					HPSIR_MAX_RXLEN,					DMA_FROM_DEVICE);	return 0;}/* * We want to get here as soon as possible, and get the receiver setup. * We use the existing buffer. */static void sa1100_irda_rx_dma_start(struct sa1100_irda *si){	if (!si->rxskb) {		printk(KERN_ERR "sa1100_ir: rx buffer went missing\n");		return;	}	/*	 * First empty receive FIFO	 */	Ser2HSCR0 = si->hscr0 | HSCR0_HSSP;	/*	 * Enable the DMA, receiver and receive interrupt.	 */	sa1100_clear_dma(si->rxdma);	sa1100_start_dma(si->rxdma, si->rxbuf_dma, HPSIR_MAX_RXLEN);	Ser2HSCR0 = si->hscr0 | HSCR0_HSSP | HSCR0_RXE;}/* * Set the IrDA communications speed. */static int sa1100_irda_set_speed(struct sa1100_irda *si, int speed){	unsigned long flags;	int brd, ret = -EINVAL;	switch (speed) {	case 9600:	case 19200:	case 38400:	case 57600:	case 115200:		brd = 3686400 / (16 * speed) - 1;		/*		 * Stop the receive DMA.		 */		if (IS_FIR(si))			sa1100_stop_dma(si->rxdma);		local_irq_save(flags);		Ser2UTCR3 = 0;		Ser2HSCR0 = HSCR0_UART;		Ser2UTCR1 = brd >> 8;		Ser2UTCR2 = brd;		/*		 * Clear status register		 */		Ser2UTSR0 = UTSR0_REB | UTSR0_RBB | UTSR0_RID;		Ser2UTCR3 = UTCR3_RIE | UTCR3_RXE | UTCR3_TXE;		if (si->pdata->set_speed)			si->pdata->set_speed(si->dev, speed);		si->speed = speed;		local_irq_restore(flags);		ret = 0;		break;	case 4000000:		local_irq_save(flags);		si->hscr0 = 0;		Ser2HSSR0 = 0xff;		Ser2HSCR0 = si->hscr0 | HSCR0_HSSP;		Ser2UTCR3 = 0;		si->speed = speed;		if (si->pdata->set_speed)			si->pdata->set_speed(si->dev, speed);		sa1100_irda_rx_alloc(si);		sa1100_irda_rx_dma_start(si);		local_irq_restore(flags);		break;	default:		break;	}	return ret;}/* * Control the power state of the IrDA transmitter. * State: *  0 - off *  1 - short range, lowest power *  2 - medium range, medium power *  3 - maximum range, high power * * Currently, only assabet is known to support this. */static int__sa1100_irda_set_power(struct sa1100_irda *si, unsigned int state){	int ret = 0;	if (si->pdata->set_power)		ret = si->pdata->set_power(si->dev, state);	return ret;}static inline intsa1100_set_power(struct sa1100_irda *si, unsigned int state){	int ret;	ret = __sa1100_irda_set_power(si, state);	if (ret == 0)		si->power = state;	return ret;}static int sa1100_irda_startup(struct sa1100_irda *si){	int ret;	/*	 * Ensure that the ports for this device are setup correctly.	 */	if (si->pdata->startup)		si->pdata->startup(si->dev);	/*	 * Configure PPC for IRDA - we want to drive TXD2 low.	 * We also want to drive this pin low during sleep.	 */	PPSR &= ~PPC_TXD2;	PSDR &= ~PPC_TXD2;	PPDR |= PPC_TXD2;	/*	 * Enable HP-SIR modulation, and ensure that the port is disabled.	 */	Ser2UTCR3 = 0;	Ser2HSCR0 = HSCR0_UART;	Ser2UTCR4 = si->utcr4;	Ser2UTCR0 = UTCR0_8BitData;	Ser2HSCR2 = HSCR2_TrDataH | HSCR2_RcDataL;	/*	 * Clear status register	 */	Ser2UTSR0 = UTSR0_REB | UTSR0_RBB | UTSR0_RID;	ret = sa1100_irda_set_speed(si, si->speed = 9600);	if (ret) {		Ser2UTCR3 = 0;		Ser2HSCR0 = 0;		if (si->pdata->shutdown)			si->pdata->shutdown(si->dev);	}	return ret;}static void sa1100_irda_shutdown(struct sa1100_irda *si){	/*	 * Stop all DMA activity.	 */	sa1100_stop_dma(si->rxdma);	sa1100_stop_dma(si->txdma);	/* Disable the port. */	Ser2UTCR3 = 0;	Ser2HSCR0 = 0;	if (si->pdata->shutdown)		si->pdata->shutdown(si->dev);}#ifdef CONFIG_PM/* * Suspend the IrDA interface. */static int sa1100_irda_suspend(struct platform_device *pdev, pm_message_t state){	struct net_device *dev = platform_get_drvdata(pdev);	struct sa1100_irda *si;	if (!dev)		return 0;	si = dev->priv;	if (si->open) {		/*		 * Stop the transmit queue		 */		netif_device_detach(dev);		disable_irq(dev->irq);		sa1100_irda_shutdown(si);		__sa1100_irda_set_power(si, 0);	}	return 0;}/* * Resume the IrDA interface. */static int sa1100_irda_resume(struct platform_device *pdev){	struct net_device *dev = platform_get_drvdata(pdev);	struct sa1100_irda *si;	if (!dev)		return 0;	si = dev->priv;	if (si->open) {		/*		 * If we missed a speed change, initialise at the new speed		 * directly.  It is debatable whether this is actually		 * required, but in the interests of continuing from where		 * we left off it is desireable.  The converse argument is		 * that we should re-negotiate at 9600 baud again.		 */		if (si->newspeed) {			si->speed = si->newspeed;			si->newspeed = 0;		}		sa1100_irda_startup(si);		__sa1100_irda_set_power(si, si->power);		enable_irq(dev->irq);		/*		 * This automatically wakes up the queue		 */		netif_device_attach(dev);	}	return 0;}#else#define sa1100_irda_suspend	NULL#define sa1100_irda_resume	NULL#endif/* * HP-SIR format interrupt service routines. */static void sa1100_irda_hpsir_irq(struct net_device *dev){	struct sa1100_irda *si = dev->priv;	int status;	status = Ser2UTSR0;	/*	 * Deal with any receive errors first.  The bytes in error may be	 * the only bytes in the receive FIFO, so we do this first.	 */	while (status & UTSR0_EIF) {		int stat, data;		stat = Ser2UTSR1;		data = Ser2UTDR;		if (stat & (UTSR1_FRE | UTSR1_ROR)) {			si->stats.rx_errors++;			if (stat & UTSR1_FRE)				si->stats.rx_frame_errors++;			if (stat & UTSR1_ROR)				si->stats.rx_fifo_errors++;		} else			async_unwrap_char(dev, &si->stats, &si->rx_buff, data);		status = Ser2UTSR0;	}	/*	 * We must clear certain bits.	 */	Ser2UTSR0 = status & (UTSR0_RID | UTSR0_RBB | UTSR0_REB);	if (status & UTSR0_RFS) {		/*		 * There are at least 4 bytes in the FIFO.  Read 3 bytes		 * and leave the rest to the block below.		 */		async_unwrap_char(dev, &si->stats, &si->rx_buff, Ser2UTDR);		async_unwrap_char(dev, &si->stats, &si->rx_buff, Ser2UTDR);		async_unwrap_char(dev, &si->stats, &si->rx_buff, Ser2UTDR);	}	if (status & (UTSR0_RFS | UTSR0_RID)) {		/*		 * Fifo contains more than 1 character.		 */		do {			async_unwrap_char(dev, &si->stats, &si->rx_buff,					  Ser2UTDR);		} while (Ser2UTSR1 & UTSR1_RNE);		dev->last_rx = jiffies;	}	if (status & UTSR0_TFS && si->tx_buff.len) {		/*		 * Transmitter FIFO is not full		 */		do {			Ser2UTDR = *si->tx_buff.data++;			si->tx_buff.len -= 1;		} while (Ser2UTSR1 & UTSR1_TNF && si->tx_buff.len);		if (si->tx_buff.len == 0) {			si->stats.tx_packets++;			si->stats.tx_bytes += si->tx_buff.data -					      si->tx_buff.head;			/*			 * We need to ensure that the transmitter has			 * finished.			 */			do				rmb();			while (Ser2UTSR1 & UTSR1_TBY);			/*			 * Ok, we've finished transmitting.  Now enable			 * the receiver.  Sometimes we get a receive IRQ			 * immediately after a transmit...			 */			Ser2UTSR0 = UTSR0_REB | UTSR0_RBB | UTSR0_RID;			Ser2UTCR3 = UTCR3_RIE | UTCR3_RXE | UTCR3_TXE;			if (si->newspeed) {				sa1100_irda_set_speed(si, si->newspeed);				si->newspeed = 0;			}			/* I'm hungry! */			netif_wake_queue(dev);		}	}}static void sa1100_irda_fir_error(struct sa1100_irda *si, struct net_device *dev){	struct sk_buff *skb = si->rxskb;	dma_addr_t dma_addr;	unsigned int len, stat, data;	if (!skb) {		printk(KERN_ERR "sa1100_ir: SKB is NULL!\n");		return;	}	/*	 * Get the current data position.	 */	dma_addr = sa1100_get_dma_pos(si->rxdma);	len = dma_addr - si->rxbuf_dma;	if (len > HPSIR_MAX_RXLEN)		len = HPSIR_MAX_RXLEN;	dma_unmap_single(si->dev, si->rxbuf_dma, len, DMA_FROM_DEVICE);	do {		/*		 * Read Status, and then Data.		 */		stat = Ser2HSSR1;		rmb();		data = Ser2HSDR;		if (stat & (HSSR1_CRE | HSSR1_ROR)) {			si->stats.rx_errors++;			if (stat & HSSR1_CRE)				si->stats.rx_crc_errors++;			if (stat & HSSR1_ROR)				si->stats.rx_frame_errors++;		} else			skb->data[len++] = data;		/*		 * If we hit the end of frame, there's		 * no point in continuing.		 */		if (stat & HSSR1_EOF)			break;	} while (Ser2HSSR0 & HSSR0_EIF);	if (stat & HSSR1_EOF) {		si->rxskb = NULL;		skb_put(skb, len);		skb->dev = dev;		skb->mac.raw = skb->data;		skb->protocol = htons(ETH_P_IRDA);		si->stats.rx_packets++;		si->stats.rx_bytes += len;		/*		 * Before we pass the buffer up, allocate a new one.		 */		sa1100_irda_rx_alloc(si);		netif_rx(skb);		dev->last_rx = jiffies;	} else {		/*		 * Remap the buffer.		 */

⌨️ 快捷键说明

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