您当前的位置:五五电子网电子知识电子学习基础知识电脑-单片机-自动控制Linux网卡驱动程序编写 正文
Linux网卡驱动程序编写

Linux网卡驱动程序编写

点击数:7836 次   录入时间:03-04 11:41:32   整理:http://www.55dianzi.com   电脑-单片机-自动控制

    __u32tx_queue_len;/*Maxframesperqueueallowed*/

    /*Forloadbalancingdriverpairsupport*/

    unsignedlongpkt_queue;/*Packetsqueued*/

    structdevice*slave;/*Slavedevice*/

    structnet_alias_info*alias_info;/*maindevaliasinfo*/

    structnet_alias*my_alias;/*aliasdevs*/

    /*Pointertotheinterfacebuffers.*/

    structsk_buff_headbuffs[DEV_NUMBUFFS];

    /*Pointerstointerfaceserviceroutines.*/

    int(*open)(structdevice*dev);

    int(*stop)(structdevice*dev);

    int(*hard_start_xmit)(structsk_buff*skb,

    structdevice*dev);

    int(*hard_header)(structsk_buff*skb,

    structdevice*dev,

    unsignedshorttype,

    void*daddr,

    void*saddr,

    unsignedlen);

    int(*rebuild_header)(void*eth,structdevice*dev,

    unsignedlongraddr,structsk_buff*skb);

    #defineHAVE_MULTICAST

    void(*set_multicast_list)(structdevice*dev);

    #defineHAVE_SET_MAC_ADDR

    int(*set_mac_address)(structdevice*dev,void*addr);

    #defineHAVE_PRIVATE_IOCTL

    int(*do_ioctl)(structdevice*dev,structifreq*ifr,iNTCmd);

    #defineHAVE_SET_CONFIG

    int(*set_config)(structdevice*dev,structifmap*map);

    #defineHAVE_HEADER_CACHE

    void(*header_cache_bind)(structhh_cache**hhp,structdevice

    *dev,unsignedshorthtype,__u32daddr);

    void(*header_cache_update)(structhh_cache*hh,structdevice

    *dev,unsignedchar*haddr);

    #defineHAVE_CHANGE_MTU

    int(*change_mtu)(structdevice*dev,intnew_mtu);

    structiw_statistics*(*get_wireless_stats)(structdevice*dev);

    };

    2.4常用的系统支持

    2.4.1内存申请和释放

    include/linux/kernel.h里声明了kmalLOC()和kfree()。用于在内核模式下申请和释放内存。

    void*kmalloc(unsignedintlen,intpriority);

    voidkfree(void*__ptr);



www.55dianzi.com

    与用户模式下的malLOC()不同,kmalloc()申请空间有大小限制。长度是2的整次方。可以申请的最大长度也有限制。另外kmalloc()有priority参数,通常使用时可以为GFP_KERNEL,如果在中断里调用用GFP_ATOMIC参数,因为使用GFP_KERNEL则调用者可能进入sleep状态,在处理中断时是不允许的。

    kfree()释放的内存必须是kmalloc()申请的。如果知道内存的大小,也可以用kfree_s()释放。

    2.4.2request_IRQ()、free_irq()

    这是驱动程序申请中断和释放中断的调用。在include/linux/sched.h里声明。

    request_irq()调用的定义:

    intrequest_irq(unsignedintirq,

    void(*handler)(intirq,void*dev_id,structpt_regs*regs),

    unsignedlongirqflags,

    constchar*devname,

    void*dev_id);

    irq是要申请的硬件中断号。在Intel平台,范围0--15。handler是向系统登记的中断处理函数。这是一个回调函数,中断发生时,系统调用这个函数,传入的参数包括硬件中断号,deviceid,寄存器值。dev_id就是下面的request_irq时传递给系统的参数dev_id。irqflags是中断处理的一些属性。比较重要的有SA_INTERRUPT,

    标明中断处理程序是快速处理程序(设置SA_INTERRUPT)还是慢速处理程序(不设置SA_INTERRUPT)。快速处理程序被调用时屏蔽所有中断。慢速处理程序不屏蔽。还有一个SA_SHIRQ属性,设置了以后运行多个设备共享中断。dev_id在中断共享时会用到。一般设置为这个设备的device结构本身或者NULL。中断处理程序可以用dev_id找到相应的控制这个中断的设备,或者用irq2dev_map找到中断对应的设备。

    voidfree_irq(unsignedintirq,void*dev_id);

    2.4.3时钟

    时钟的处理类似中断,也是登记一个时间处理函数,在预定的时间过后,系统会调用这个函数。在include/linux/timer.h里声明。

    structtimer_list{

    structtimer_list*next;

    structtimer_list*prev;

    unsignedlongexpires;

    unsignedlongdata;

    void(*function)(unsignedlong);

    };

    voidadd_timer(structtimer_list*timer);

    intdel_timer(structtimer_list*timer);

    voidinit_timer(structtimer_list*timer);

    使用时钟,先声明一个timer_list结构,调用init_timer对它进行初始化。

    time_list结构里expires是标明这个时钟的周期,单位采用jiffies的单位。

    jiffies是Linux一个全局变量,代表时间。它的单位随硬件平台的不同而不同。

    系统里定义了一个常数Hz,代表每秒种最小时间间隔的数目。这样jiffies的单位就是1/HZ。Intel平台jiffies的单位是1/100秒,这就是系统所能分辨的最小时间间隔了。所以expires/HZ就是以秒为单位的这个时钟的周期。

    function就是时间到了以后的回调函数,它的参数就是timer_list中的data。data这个参数在初始化时钟的时候赋值,一般赋给它设备的device结构指针。

    在预置时间到系统调用function,同时系统把这个time_list从定时队列里清除。所以如果需要一直使用定时函数,要在function里再次调用add_timer()把这个timer_list加进定时队列。

    2.4.4I/O

    I/O端口的存取使用:

    inlineunsignedintinb(unsignedshortport);

    inlineunsignedintinb_p(unsignedshortport);

    inlinevoidoutb(charvalue,unsignedshortport);

    inlinevoidoutb_p(charvalue,unsignedshortport);

    在include/adm/io.h里定义。

    inb_p()、outb_p()与inb()、outb_p()的不同在于前者在存取I/O时有等待(pause)一适应慢速的I/O设备。

    为了防止存取I/O时发生冲突,Linux提供对端口使用情况的控制。在使用端口之前,可以检查需要的I/O是否正在被使用,如果没有,则把端口标记为正在使用,使用完后再释放。系统提供以下几个函数做这些工作。

    iNTCheck_region(unsignedintfrom,unsignedintextent);

    voidrequest_region(unsignedintfrom,unsignedintextent,constchar*name);

    voidrelease_region(unsignedintfrom,unsignedintextent);

    其中的参数from表示用到的I/O端口的起始地址,extent标明从from开始的端口数目。name为设备名称。

    2.4.5中断打开关闭

    系统提供给驱动程序开放和关闭响应中断的能力。是在include/asm/system.h中的两个定义。

    #definecli()__asm____volatile__("cli"::)

    #definesti()__asm____volatile__("sti"::)

    2.4.6打印信息

    类似普通程序里的printf(),驱动程序要输出信息使用printk()。在include/linux/kernel.h里声明。

    intprintk(constchar*FMt,...);

    其中fmt是格式化字符串。...是参数。都是和printf()格式一样的。

上一页  [1] [2] [3] [4] [5]  下一页


本文关键字:Linux  网卡驱动  程序编写  电脑-单片机-自动控制电子学习 - 基础知识 - 电脑-单片机-自动控制