ARM-Linux驱动--DM9000网卡驱动分析(二)

硬件平台:FL2440(s3c2440)

内核版本:2.6.35

主机平台:Ubuntu 11.04

内核版本:2.6.39

原创作品,转载请标明出处http://blog.csdn.net/yming0221/article/details/6612623

下面开始分析具体的代码,这里由于使DM9000驱动更容易理解,在不影响基本的功能的前提下,这里将尽可能的简化该驱动(如:去掉该驱动中支持电源管理的功能)

分析该驱动

1、首先看一下该驱动的平台设备驱动的结构体定义

/*平台设备驱动的结构体定义
*在该结构体中可以定义有关Power Management的管理函数
*该驱动中将其省略,侧重分析dm9000的基本原理
*/
static struct platform_driver dm9000_driver = {
	.driver	= {
		.name    = "dm9000",/* 该名称和系统初始化中,平台设备的名称一致 */
		.owner	 = THIS_MODULE,
	},
	.probe   = dm9000_probe,/* 资源探测函数 */
	.remove  = __devexit_p(dm9000_drv_remove),/* 设备移除函数 */
};
在执行insmod后内核自动那个执行下面的函数
static int __init
dm9000_init(void)
{
	printk(KERN_INFO "%s Ethernet Driver, V%s\n", CARDNAME, DRV_VERSION);

	return platform_driver_register(&dm9000_driver);
}

调用函数platform_driver_register()函数注册驱动。

3、自动执行驱动的probe函数,进行资源的探测和申请资源。

其中BWSCON为总线宽度 等待控制寄存器


其中第[19:18]位的作用如下

ARM-Linux驱动--DM9000网卡驱动分析(二)_第1张图片

下面函数中将两位设置为11,也就是WAIT使能,bank4使用UB/LB。

alloc_etherdev()函数分配一个网络设备的结构体,原型在include/linux/etherdevice.h

原型如下:

extern struct net_device *alloc_etherdev_mq(int sizeof_priv, unsigned int queue_count);
#define alloc_etherdev(sizeof_priv) alloc_etherdev_mq(sizeof_priv, 1)
该函数中需要将获得的资源信息存储在一个结构体中,定义如下:

/* Structure/enum declaration ------------------------------- */
typedef struct board_info {

	void __iomem	*io_addr;	/* Register I/O base address */
	void __iomem	*io_data;	/* Data I/O address */
	u16		 irq;		/* IRQ */

	u16		tx_pkt_cnt;
	u16		queue_pkt_len;
	u16		queue_start_addr;
	u16		queue_ip_summed;
	u16		dbug_cnt;
	u8		io_mode;		/* 0:word, 2:byte */
	u8		phy_addr;
	u8		imr_all;

	unsigned int	flags;
	unsigned int	in_suspend :1;
	unsigned int	wake_supported :1;
	int		debug_level;

	enum dm9000_type type;

	void (*inblk)(void __iomem *port, void *data, int length);
	void (*outblk)(void __iomem *port, void *data, int length);
	void (*dumpblk)(void __iomem *port, int length);

	struct device	*dev;	     /* parent device */

	struct resource	*addr_res;   /* resources found */
	struct resource *data_res;
	struct resource	*addr_req;   /* resources requested */
	struct resource *data_req;
	struct resource *irq_res;

	int		 irq_wake;

	struct mutex	 addr_lock;	/* phy and eeprom access lock */

	struct delayed_work phy_poll;
	struct net_device  *ndev;

	spinlock_t	lock;

	struct mii_if_info mii;
	u32		msg_enable;
	u32		wake_state;

	int		rx_csum;
	int		can_csum;
	int		ip_summed;
} board_info_t;

下面是probe函数,

其中有个函数db = netdev_priv(ndev)

该函数实际上是返回网卡私有成员的数据结构地址

函数如下,定义在include/linux/net_device.h中

static inline void *netdev_priv(const struct net_device *dev)
{
	return (char *)dev + ALIGN(sizeof(struct net_device), NETDEV_ALIGN);
}

/*
 * Search DM9000 board, allocate space and register it
 */
static int __devinit
dm9000_probe(struct platform_device *pdev)
{
	struct dm9000_plat_data *pdata = pdev->dev.platform_data;
	struct board_info *db;	/* Point a board information structure */
	struct net_device *ndev;/* 网络设备 */
	const unsigned char *mac_src;
	int ret = 0;
	int iosize;
	int i;
	u32 id_val;

	unsigned char ne_def_eth_mac_addr[]={0x00,0x12,0x34,0x56,0x80,0x49};/* 设定默认的mac地址 */
    static void *bwscon;/* 保存ioremap返回的寄存器的虚拟地址,下同 */
    static void *gpfcon;
    static void *extint0;
    static void *intmsk;
    /*Added by yan*/
    #define BWSCON           (0x48000000)
    #define GPFCON           (0x56000050)
    #define EXTINT0           (0x56000088)
    #define INTMSK           (0x4A000008)

    bwscon=ioremap_nocache(BWSCON,0x0000004);
    gpfcon=ioremap_nocache(GPFCON,0x0000004);
    extint0=ioremap_nocache(EXTINT0,0x0000004);
    intmsk=ioremap_nocache(INTMSK,0x0000004);

    writel( readl(bwscon)|0xc0000,bwscon);/* 将BWSCON寄存器[19:18]设置为11 */
    writel( (readl(gpfcon) & ~(0x3 << 14)) | (0x2 << 14), gpfcon); /* 设置GPF寄存器 */
    writel( readl(gpfcon) | (0x1 << 7), gpfcon); // Disable pull-up,不使能上拉
    writel( (readl(extint0) & ~(0xf << 28)) | (0x4 << 28), extint0); //rising edge,设置上升沿触发中断
    writel( (readl(intmsk))  & ~0x80, intmsk);/* 设置中断屏蔽寄存器 */
        
    /*End of add*/
	/* Init network device */
	/* 使用alloc_etherdev()函数分配一个网络设备的结构体,原型在include/linux/etherdevice.h */
	ndev = alloc_etherdev(sizeof(struct board_info));
	if (!ndev) {
		dev_err(&pdev->dev, "could not allocate device.\n");
		return -ENOMEM;
	}

/*通过SET_NETDEV_DEV(netdev, &pdev->dev)宏设置net_device.device->parent为当前的pci_device->device
*(这儿net_device包含的是device结构,而不是指针)。这样,就建立起了net_device到device的联系。
*/
	SET_NETDEV_DEV(ndev, &pdev->dev);

	dev_dbg(&pdev->dev, "dm9000_probe()\n");

	/* setup board info structure */
	/* 下面都是设置board_info结构体 */
	db = netdev_priv(ndev);/* 返回dev->priv的地址 */

	db->dev = &pdev->dev;
	db->ndev = ndev;

	spin_lock_init(&db->lock);
	mutex_init(&db->addr_lock);

	INIT_DELAYED_WORK(&db->phy_poll, dm9000_poll_work);

	db->addr_res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	db->data_res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
	db->irq_res  = platform_get_resource(pdev, IORESOURCE_IRQ, 0);

	if (db->addr_res == NULL || db->data_res == NULL ||
	    db->irq_res == NULL) {
		dev_err(db->dev, "insufficient resources\n");
		ret = -ENOENT;
		goto out;
	}

	db->irq_wake = platform_get_irq(pdev, 1);
	if (db->irq_wake >= 0) {
		dev_dbg(db->dev, "wakeup irq %d\n", db->irq_wake);

		ret = request_irq(db->irq_wake, dm9000_wol_interrupt,
				  IRQF_SHARED, dev_name(db->dev), ndev);
		if (ret) {
			dev_err(db->dev, "cannot get wakeup irq (%d)\n", ret);
		} else {

			/* test to see if irq is really wakeup capable */
			ret = set_irq_wake(db->irq_wake, 1);
			if (ret) {
				dev_err(db->dev, "irq %d cannot set wakeup (%d)\n",
					db->irq_wake, ret);
				ret = 0;
			} else {
				set_irq_wake(db->irq_wake, 0);
				db->wake_supported = 1;
			}
		}
	}

	iosize = resource_size(db->addr_res);
	db->addr_req = request_mem_region(db->addr_res->start, iosize,
					  pdev->name);

	if (db->addr_req == NULL) {
		dev_err(db->dev, "cannot claim address reg area\n");
		ret = -EIO;
		goto out;
	}

	db->io_addr = ioremap(db->addr_res->start, iosize);

	if (db->io_addr == NULL) {
		dev_err(db->dev, "failed to ioremap address reg\n");
		ret = -EINVAL;
		goto out;
	}

	iosize = resource_size(db->data_res);
	db->data_req = request_mem_region(db->data_res->start, iosize,
					  pdev->name);

	if (db->data_req == NULL) {
		dev_err(db->dev, "cannot claim data reg area\n");
		ret = -EIO;
		goto out;
	}

	db->io_data = ioremap(db->data_res->start, iosize);

	if (db->io_data == NULL) {
		dev_err(db->dev, "failed to ioremap data reg\n");
		ret = -EINVAL;
		goto out;
	}
	/* 设置结构体board_info结束 */
	
	/* fill in parameters for net-dev structure */
	ndev->base_addr = (unsigned long)db->io_addr;/* 设置网络设备的地址 */
	ndev->irq	= db->irq_res->start;/* 设置网络设备的中断资源地址 */

	/* ensure at least we have a default set of IO routines */
	dm9000_set_io(db, iosize);

	/* check to see if anything is being over-ridden */
	
	/*根据pdev->dev.platform_data的信息判断IO的宽度并设置相应的宽度*/
	if (pdata != NULL) {
		/* check to see if the driver wants to over-ride the
		 * default IO width */

		if (pdata->flags & DM9000_PLATF_8BITONLY)
			dm9000_set_io(db, 1);

		if (pdata->flags & DM9000_PLATF_16BITONLY)
			dm9000_set_io(db, 2);

		if (pdata->flags & DM9000_PLATF_32BITONLY)
			dm9000_set_io(db, 4);

		/* check to see if there are any IO routine
		 * over-rides */

		if (pdata->inblk != NULL)
			db->inblk = pdata->inblk;

		if (pdata->outblk != NULL)
			db->outblk = pdata->outblk;

		if (pdata->dumpblk != NULL)
			db->dumpblk = pdata->dumpblk;

		db->flags = pdata->flags;
	}

#ifdef CONFIG_DM9000_FORCE_SIMPLE_PHY_POLL
	db->flags |= DM9000_PLATF_SIMPLE_PHY;
#endif

	dm9000_reset(db);/* 复位 */

	/* try multiple times, DM9000 sometimes gets the read wrong */
	for (i = 0; i < 8; i++) {
		id_val  = ior(db, DM9000_VIDL);
		id_val |= (u32)ior(db, DM9000_VIDH) << 8;
		id_val |= (u32)ior(db, DM9000_PIDL) << 16;
		id_val |= (u32)ior(db, DM9000_PIDH) << 24;

		if (id_val == DM9000_ID)
			break;
		dev_err(db->dev, "read wrong id 0x%08x\n", id_val);
	}

	if (id_val != DM9000_ID) {
		dev_err(db->dev, "wrong id: 0x%08x\n", id_val);
		ret = -ENODEV;
		goto out;
	}

	/* Identify what type of DM9000 we are working on */

	id_val = ior(db, DM9000_CHIPR);
	dev_dbg(db->dev, "dm9000 revision 0x%02x\n", id_val);

	switch (id_val) {
	case CHIPR_DM9000A:
		db->type = TYPE_DM9000A;
		break;
	case CHIPR_DM9000B:
		db->type = TYPE_DM9000B;
		break;
	default:
		dev_dbg(db->dev, "ID %02x => defaulting to DM9000E\n", id_val);
		db->type = TYPE_DM9000E;
	}

	/* dm9000a/b are capable of hardware checksum offload */
	if (db->type == TYPE_DM9000A || db->type == TYPE_DM9000B) {
		db->can_csum = 1;
		db->rx_csum = 1;
		ndev->features |= NETIF_F_IP_CSUM;
	}

	/* from this point we assume that we have found a DM9000 */

	/* driver system function */
	ether_setup(ndev);

	ndev->netdev_ops	= &dm9000_netdev_ops;
	ndev->watchdog_timeo	= msecs_to_jiffies(watchdog);
	ndev->ethtool_ops	= &dm9000_ethtool_ops;

	db->msg_enable       = NETIF_MSG_LINK;
	db->mii.phy_id_mask  = 0x1f;
	db->mii.reg_num_mask = 0x1f;
	db->mii.force_media  = 0;
	db->mii.full_duplex  = 0;
	db->mii.dev	     = ndev;
	db->mii.mdio_read    = dm9000_phy_read;
	db->mii.mdio_write   = dm9000_phy_write;

	mac_src = "eeprom";

	/* try reading the node address from the attached EEPROM */
	for (i = 0; i < 6; i += 2)
		dm9000_read_eeprom(db, i / 2, ndev->dev_addr+i);

	if (!is_valid_ether_addr(ndev->dev_addr) && pdata != NULL) {
		mac_src = "platform data";
		memcpy(ndev->dev_addr, pdata->dev_addr, 6);
	}

	if (!is_valid_ether_addr(ndev->dev_addr)) {
		/* try reading from mac */
		
		mac_src = "chip";
		for (i = 0; i < 6; i++)
			ndev->dev_addr[i] = ne_def_eth_mac_addr[i];
	}

	if (!is_valid_ether_addr(ndev->dev_addr))
		dev_warn(db->dev, "%s: Invalid ethernet MAC address. Please "
			 "set using ifconfig\n", ndev->name);

	/* 设置pdev->dev->driver_data为ndev,保存成平台设备总线上的数据,以后使用只需platform_get_drvdata()即可*/
	platform_set_drvdata(pdev, ndev);
	
	/* 注册该网络设备 */
	ret = register_netdev(ndev);

	if (ret == 0)
		printk(KERN_INFO "%s: dm9000%c at %p,%p IRQ %d MAC: %pM (%s)\n",
		       ndev->name, dm9000_type_to_char(db->type),
		       db->io_addr, db->io_data, ndev->irq,
		       ndev->dev_addr, mac_src);
	return 0;
	
/* 异常处理 */
out:
	dev_err(db->dev, "not found (%d).\n", ret);

	dm9000_release_board(pdev, db);
	free_netdev(ndev);

	return ret;
}
这样,最后完成了网络设备的数据保存到总线上,将网络设备注册到内核。

4、设备的移除函数

/* 该函数是将设备从内核中移除,释放资源,在移除设备驱动时执行 */
static int __devexit
dm9000_drv_remove(struct platform_device *pdev)
{
	struct net_device *ndev = platform_get_drvdata(pdev);/* 从总线获取probe函数保存到总线的设备信息 */

	platform_set_drvdata(pdev, NULL);/* 释放pdev资源 */

	unregister_netdev(ndev);/* 解除网络设备 */
	dm9000_release_board(pdev, (board_info_t *) netdev_priv(ndev));/* 释放该设备申请的IO资源 */
	free_netdev(ndev);		/* free device structure */

	dev_dbg(&pdev->dev, "released and freed device\n");
	return 0;
}

更多见ARM-Linux驱动--DM9000网卡驱动分析(一)

ARM-Linux驱动--DM9000网卡驱动分析(三)

你可能感兴趣的:(linux)