硬件平台:FL2440(
s3c2440)
内核版本:2.6.35
主机平台:Ubuntu 11.04
内核版本:2.6.39
原创作品,转载请标明出处http://blog.csdn.net/yming0221/article/details/6612623
下面开始分析具体的代码,这里由于使DM9000驱动更容易理解,在不影响基本的功能的前提下,这里将尽可能的简化该驱动(如:去掉该驱动中支持电源管理的功能)
分析该驱动
1、首先看一下该驱动的平台设备驱动的结构体定义
view plainprint?
static struct platform_driver dm9000_driver = {
.driver = {
.name = "dm9000",
.owner = THIS_MODULE,
},
.probe = dm9000_probe,
.remove = __devexit_p(dm9000_drv_remove),
};
在执行insmod后内核自动那个执行下面的函数
view plainprint?
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]位的作用如下
下面函数中将两位设置为11,也就是WAIT使能,bank4使用UB/LB。
alloc_etherdev()函数分配一个网络设备的结构体,原型在include/linux/etherdevice.h
原型如下:
view plainprint?
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)
该函数中需要将获得的资源信息存储在一个结构体中,定义如下:
view plainprint?
typedef struct board_info {
void __iomem *io_addr;
void __iomem *io_data;
u16 irq;
u16 tx_pkt_cnt;
u16 queue_pkt_len;
u16 queue_start_addr;
u16 queue_ip_summed;
u16 dbug_cnt;
u8 io_mode;
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;
struct resource *addr_res;
struct resource *data_res;
struct resource *addr_req;
struct resource *data_req;
struct resource *irq_res;
int irq_wake;
struct mutex addr_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中
view plainprint?
static inline void *netdev_priv(const struct net_device *dev)
{
return (char *)dev + ALIGN(sizeof(struct net_device), NETDEV_ALIGN);
}
view plainprint?
static int __devinit
dm9000_probe(struct platform_device *pdev)
{
struct dm9000_plat_data *pdata = pdev->dev.platform_data;
struct board_info *db;
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};
static void *bwscon;
static void *gpfcon;
static void *extint0;
static void *intmsk;
#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);
writel( (readl(gpfcon) & ~(0x3 < 14)) | (0x2 < 14), gpfcon);
writel( readl(gpfcon) | (0x1 < 7), gpfcon); // Disable pull-up,不使能上拉
writel( (readl(extint0) & ~(0xf < 28)) | (0x4 < 28), extint0); //rising edge,设置上升沿触发中断