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

📄 if_cs.c

📁 linux 内核源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/*  Driver for the Marvell 8385 based compact flash WLAN cards.  (C) 2007 by Holger Schurig <hs4233@mail.mn-solutions.de>  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; see the file COPYING.  If not, write to  the Free Software Foundation, Inc., 51 Franklin Steet, Fifth Floor,  Boston, MA 02110-1301, USA.*/#include <linux/module.h>#include <linux/delay.h>#include <linux/moduleparam.h>#include <linux/firmware.h>#include <linux/netdevice.h>#include <pcmcia/cs_types.h>#include <pcmcia/cs.h>#include <pcmcia/cistpl.h>#include <pcmcia/ds.h>#include <linux/io.h>#define DRV_NAME "libertas_cs"#include "decl.h"#include "defs.h"#include "dev.h"/********************************************************************//* Module stuff                                                     *//********************************************************************/MODULE_AUTHOR("Holger Schurig <hs4233@mail.mn-solutions.de>");MODULE_DESCRIPTION("Driver for Marvell 83xx compact flash WLAN cards");MODULE_LICENSE("GPL");/********************************************************************//* Data structures                                                  *//********************************************************************/struct if_cs_card {	struct pcmcia_device *p_dev;	wlan_private *priv;	void __iomem *iobase;};/********************************************************************//* Hardware access                                                  *//********************************************************************//* This define enables wrapper functions which allow you   to dump all register accesses. You normally won't this,   except for development *//* #define DEBUG_IO */#ifdef DEBUG_IOstatic int debug_output = 0;#else/* This way the compiler optimizes the printk's away */#define debug_output 0#endifstatic inline unsigned int if_cs_read8(struct if_cs_card *card, uint reg){	unsigned int val = ioread8(card->iobase + reg);	if (debug_output)		printk(KERN_INFO "##inb %08x<%02x\n", reg, val);	return val;}static inline unsigned int if_cs_read16(struct if_cs_card *card, uint reg){	unsigned int val = ioread16(card->iobase + reg);	if (debug_output)		printk(KERN_INFO "##inw %08x<%04x\n", reg, val);	return val;}static inline void if_cs_read16_rep(	struct if_cs_card *card,	uint reg,	void *buf,	unsigned long count){	if (debug_output)		printk(KERN_INFO "##insw %08x<(0x%lx words)\n",			reg, count);	ioread16_rep(card->iobase + reg, buf, count);}static inline void if_cs_write8(struct if_cs_card *card, uint reg, u8 val){	if (debug_output)		printk(KERN_INFO "##outb %08x>%02x\n", reg, val);	iowrite8(val, card->iobase + reg);}static inline void if_cs_write16(struct if_cs_card *card, uint reg, u16 val){	if (debug_output)		printk(KERN_INFO "##outw %08x>%04x\n", reg, val);	iowrite16(val, card->iobase + reg);}static inline void if_cs_write16_rep(	struct if_cs_card *card,	uint reg,	void *buf,	unsigned long count){	if (debug_output)		printk(KERN_INFO "##outsw %08x>(0x%lx words)\n",			reg, count);	iowrite16_rep(card->iobase + reg, buf, count);}/* * I know that polling/delaying is frowned upon. However, this procedure * with polling is needed while downloading the firmware. At this stage, * the hardware does unfortunately not create any interrupts. * * Fortunately, this function is never used once the firmware is in * the card. :-) * * As a reference, see the "Firmware Specification v5.1", page 18 * and 19. I did not follow their suggested timing to the word, * but this works nice & fast anyway. */static int if_cs_poll_while_fw_download(struct if_cs_card *card, uint addr, u8 reg){	int i;	for (i = 0; i < 1000; i++) {		u8 val = if_cs_read8(card, addr);		if (val == reg)			return i;		udelay(500);	}	return -ETIME;}/* Host control registers and their bit definitions */#define IF_CS_H_STATUS			0x00000000#define IF_CS_H_STATUS_TX_OVER		0x0001#define IF_CS_H_STATUS_RX_OVER		0x0002#define IF_CS_H_STATUS_DNLD_OVER	0x0004#define IF_CS_H_INT_CAUSE		0x00000002#define IF_CS_H_IC_TX_OVER		0x0001#define IF_CS_H_IC_RX_OVER		0x0002#define IF_CS_H_IC_DNLD_OVER		0x0004#define IF_CS_H_IC_POWER_DOWN		0x0008#define IF_CS_H_IC_HOST_EVENT		0x0010#define IF_CS_H_IC_MASK			0x001f#define IF_CS_H_INT_MASK		0x00000004#define	IF_CS_H_IM_MASK			0x001f#define IF_CS_H_WRITE_LEN		0x00000014#define IF_CS_H_WRITE			0x00000016#define IF_CS_H_CMD_LEN			0x00000018#define IF_CS_H_CMD			0x0000001A#define IF_CS_C_READ_LEN		0x00000024#define IF_CS_H_READ			0x00000010/* Card control registers and their bit definitions */#define IF_CS_C_STATUS			0x00000020#define IF_CS_C_S_TX_DNLD_RDY		0x0001#define IF_CS_C_S_RX_UPLD_RDY		0x0002#define IF_CS_C_S_CMD_DNLD_RDY		0x0004#define IF_CS_C_S_CMD_UPLD_RDY		0x0008#define IF_CS_C_S_CARDEVENT		0x0010#define IF_CS_C_S_MASK			0x001f#define IF_CS_C_S_STATUS_MASK		0x7f00/* The following definitions should be the same as the MRVDRV_ ones */#if MRVDRV_CMD_DNLD_RDY != IF_CS_C_S_CMD_DNLD_RDY#error MRVDRV_CMD_DNLD_RDY and IF_CS_C_S_CMD_DNLD_RDY not in sync#endif#if MRVDRV_CMD_UPLD_RDY != IF_CS_C_S_CMD_UPLD_RDY#error MRVDRV_CMD_UPLD_RDY and IF_CS_C_S_CMD_UPLD_RDY not in sync#endif#if MRVDRV_CARDEVENT != IF_CS_C_S_CARDEVENT#error MRVDRV_CARDEVENT and IF_CS_C_S_CARDEVENT not in sync#endif#define IF_CS_C_INT_CAUSE		0x00000022#define	IF_CS_C_IC_MASK			0x001f#define IF_CS_C_SQ_READ_LOW		0x00000028#define IF_CS_C_SQ_HELPER_OK		0x10#define IF_CS_C_CMD_LEN			0x00000030#define IF_CS_C_CMD			0x00000012#define IF_CS_SCRATCH			0x0000003F/********************************************************************//* Interrupts                                                       *//********************************************************************/static inline void if_cs_enable_ints(struct if_cs_card *card){	lbs_deb_enter(LBS_DEB_CS);	if_cs_write16(card, IF_CS_H_INT_MASK, 0);}static inline void if_cs_disable_ints(struct if_cs_card *card){	lbs_deb_enter(LBS_DEB_CS);	if_cs_write16(card, IF_CS_H_INT_MASK, IF_CS_H_IM_MASK);}static irqreturn_t if_cs_interrupt(int irq, void *data){	struct if_cs_card *card = (struct if_cs_card *)data;	u16 int_cause;	lbs_deb_enter(LBS_DEB_CS);	int_cause = if_cs_read16(card, IF_CS_C_INT_CAUSE);	if(int_cause == 0x0) {		/* Not for us */		return IRQ_NONE;	} else if(int_cause == 0xffff) {		/* Read in junk, the card has probably been removed */		card->priv->adapter->surpriseremoved = 1;	} else {		if(int_cause & IF_CS_H_IC_TX_OVER) {			card->priv->dnld_sent = DNLD_RES_RECEIVED;			if (!card->priv->adapter->cur_cmd)				wake_up_interruptible(&card->priv->waitq);			if (card->priv->adapter->connect_status == LIBERTAS_CONNECTED)				netif_wake_queue(card->priv->dev);		}		/* clear interrupt */		if_cs_write16(card, IF_CS_C_INT_CAUSE, int_cause & IF_CS_C_IC_MASK);	}	libertas_interrupt(card->priv->dev);	return IRQ_HANDLED;}/********************************************************************//* I/O                                                              *//********************************************************************//* * Called from if_cs_host_to_card to send a command to the hardware */static int if_cs_send_cmd(wlan_private *priv, u8 *buf, u16 nb){	struct if_cs_card *card = (struct if_cs_card *)priv->card;	int ret = -1;	int loops = 0;	lbs_deb_enter(LBS_DEB_CS);	/* Is hardware ready? */	while (1) {		u16 val = if_cs_read16(card, IF_CS_C_STATUS);		if (val & IF_CS_C_S_CMD_DNLD_RDY)			break;		if (++loops > 100) {			lbs_pr_err("card not ready for commands\n");			goto done;		}		mdelay(1);	}	if_cs_write16(card, IF_CS_H_CMD_LEN, nb);	if_cs_write16_rep(card, IF_CS_H_CMD, buf, nb / 2);	/* Are we supposed to transfer an odd amount of bytes? */	if (nb & 1)		if_cs_write8(card, IF_CS_H_CMD, buf[nb-1]);	/* "Assert the download over interrupt command in the Host	 * status register" */	if_cs_write16(card, IF_CS_H_STATUS, IF_CS_H_STATUS_DNLD_OVER);	/* "Assert the download over interrupt command in the Card	 * interrupt case register" */	if_cs_write16(card, IF_CS_H_INT_CAUSE, IF_CS_H_IC_DNLD_OVER);	ret = 0;done:	lbs_deb_leave_args(LBS_DEB_CS, "ret %d", ret);	return ret;}/* * Called from if_cs_host_to_card to send a data to the hardware */static void if_cs_send_data(wlan_private *priv, u8 *buf, u16 nb){	struct if_cs_card *card = (struct if_cs_card *)priv->card;	lbs_deb_enter(LBS_DEB_CS);	if_cs_write16(card, IF_CS_H_WRITE_LEN, nb);	/* write even number of bytes, then odd byte if necessary */	if_cs_write16_rep(card, IF_CS_H_WRITE, buf, nb / 2);	if (nb & 1)		if_cs_write8(card, IF_CS_H_WRITE, buf[nb-1]);	if_cs_write16(card, IF_CS_H_STATUS, IF_CS_H_STATUS_TX_OVER);	if_cs_write16(card, IF_CS_H_INT_CAUSE, IF_CS_H_STATUS_TX_OVER);	lbs_deb_leave(LBS_DEB_CS);}/* * Get the command result out of the card. */static int if_cs_receive_cmdres(wlan_private *priv, u8* data, u32 *len){	int ret = -1;	u16 val;	lbs_deb_enter(LBS_DEB_CS);	/* is hardware ready? */	val = if_cs_read16(priv->card, IF_CS_C_STATUS);	if ((val & IF_CS_C_S_CMD_UPLD_RDY) == 0) {		lbs_pr_err("card not ready for CMD\n");		goto out;	}	*len = if_cs_read16(priv->card, IF_CS_C_CMD_LEN);	if ((*len == 0) || (*len > MRVDRV_SIZE_OF_CMD_BUFFER)) {		lbs_pr_err("card cmd buffer has invalid # of bytes (%d)\n", *len);		goto out;	}	/* read even number of bytes, then odd byte if necessary */	if_cs_read16_rep(priv->card, IF_CS_C_CMD, data, *len/sizeof(u16));	if (*len & 1)		data[*len-1] = if_cs_read8(priv->card, IF_CS_C_CMD);	ret = 0;out:	lbs_deb_leave_args(LBS_DEB_CS, "ret %d, len %d", ret, *len);	return ret;}static struct sk_buff *if_cs_receive_data(wlan_private *priv){	struct sk_buff *skb = NULL;	u16 len;	u8 *data;	lbs_deb_enter(LBS_DEB_CS);	len = if_cs_read16(priv->card, IF_CS_C_READ_LEN);	if (len == 0 || len > MRVDRV_ETH_RX_PACKET_BUFFER_SIZE) {		lbs_pr_err("card data buffer has invalid # of bytes (%d)\n", len);		priv->stats.rx_dropped++;		printk(KERN_INFO "##HS %s:%d TODO\n", __FUNCTION__, __LINE__);		goto dat_err;	}	//TODO: skb = dev_alloc_skb(len+ETH_FRAME_LEN+MRVDRV_SNAP_HEADER_LEN+EXTRA_LEN);	skb = dev_alloc_skb(MRVDRV_ETH_RX_PACKET_BUFFER_SIZE + 2);	if (!skb)		goto out;	skb_put(skb, len);	skb_reserve(skb, 2);/* 16 byte align */	data = skb->data;	/* read even number of bytes, then odd byte if necessary */	if_cs_read16_rep(priv->card, IF_CS_H_READ, data, len/sizeof(u16));	if (len & 1)		data[len-1] = if_cs_read8(priv->card, IF_CS_H_READ);dat_err:	if_cs_write16(priv->card, IF_CS_H_STATUS, IF_CS_H_STATUS_RX_OVER);	if_cs_write16(priv->card, IF_CS_H_INT_CAUSE, IF_CS_H_IC_RX_OVER);out:	lbs_deb_leave_args(LBS_DEB_CS, "ret %p", skb);	return skb;}/********************************************************************//* Firmware                                                         *//********************************************************************//* * Tries to program the helper firmware. * * Return 0 on success */static int if_cs_prog_helper(struct if_cs_card *card){	int ret = 0;	int sent = 0;	u8  scratch;	const struct firmware *fw;	lbs_deb_enter(LBS_DEB_CS);	scratch = if_cs_read8(card, IF_CS_SCRATCH);	/* "If the value is 0x5a, the firmware is already	 * downloaded successfully"	 */	if (scratch == 0x5a)		goto done;	/* "If the value is != 00, it is invalid value of register */	if (scratch != 0x00) {		ret = -ENODEV;		goto done;	}	/* TODO: make firmware file configurable */	ret = request_firmware(&fw, "libertas_cs_helper.fw",		&handle_to_dev(card->p_dev));	if (ret) {		lbs_pr_err("can't load helper firmware\n");		ret = -ENODEV;		goto done;	}	lbs_deb_cs("helper size %td\n", fw->size);	/* "Set the 5 bytes of the helper image to 0" */	/* Not needed, this contains an ARM branch instruction */	for (;;) {		/* "the number of bytes to send is 256" */		int count = 256;		int remain = fw->size - sent;		if (remain < count)			count = remain;		/* printk(KERN_INFO "//HS %d loading %d of %d bytes\n",			__LINE__, sent, fw->size); */		/* "write the number of bytes to be sent to the I/O Command		 * write length register" */		if_cs_write16(card, IF_CS_H_CMD_LEN, count);

⌨️ 快捷键说明

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