Linux网卡驱动程序编写
{
/*
*Thisisthefirstfieldofthevisiblepartofthisstructure
*(i.e.asseenbyusersintheSpace.cfile).Itisthename
*theinterface.
*/
char*name;
/*I/Ospecificfields-FIXME:Mergetheseandstructifmapintoone*/
unsignedlongrmem_end;/*shmemrecvend*/
unsignedlongrmem_start;/*shmemrecvstart*/
unsignedlongmem_end;/*sharedmemend*/
unsignedlongmem_start;/*sharedmemstart*/
unsignedlongbase_addr;/*deviceI/Oaddress*/
unsignedcharirq;/*deviceIRQnumber*/
/*Low-levelstatusflags.*/
volatileunsignedcharstart,/*startanoperation*/
interrupt;/*interruptarrived*/
/*在处理中断时interrupt设为1,处理完清0。*/
unsignedlongtbusy;/*transmitterbusymustbelongfor
bitops*/
structdevice*next;
/*Thedeviceinitializationfunction.Calledonlyonce.*/
/*指向驱动程序的初始化方法。*/
int(*init)(structdevice*dev);
/*Somehardwarealsoneedsthesefields,buttheyarenotpartofthe
usualsetspecifiedinSpace.c.*/
/*一些硬件可以在一块板上支持多个接口,可能用到if_port。*/
unsignedcharif_port;/*SelectableAUI,TP,..*/
unsignedchardma;/*DMAchannel*/
structenet_statistics*(*get_stats)(structdevice*dev);
/*
*Thismarkstheendofthevisiblepartofthestructure.All
*fieldshereafterareinternaltothesystem,andmaychangeat
*will(read:maybecleanedupatwill).
*/
/*Thesemaybeneededforfuturenetwork-power-downcode.*/
/*trans_start记录最后一次成功发送的时间。可以用来确定硬件是否工作正常。*/
unsignedlongtrans_start;/*Time(injiffies)oflastTx*/
unsignedlonglast_rx;/*TimeoflastRx*/
/*flags里面有很多内容,定义在include/linux/if.h里。*/
unsignedshortflags;/*interfaceflags(alaBSD)*/
unsignedshortfamily;/*addressfamilyID(AF_INET)*/
unsignedshortmetric;/*routingmetric(notused)*/
unsignedshortmtu;/*interfaceMTUvalue*/
/*type标明物理硬件的类型。主要说明硬件是否需要arp。定义在
include/linux/if_arp.h里。*/
unsignedshorttype;/*interfacehardwaretype*/
/*上层协议层根据hard_header_len在发送数据缓冲区前面预留硬件帧头空间。*/
unsignedshorthard_header_len;/*hardwarehdrlength*/
/*priv指向驱动程序自己定义的一些参数。*/
void*priv;/*pointertoprivatedata*/
/*Interfaceaddressinfo.*/
unsignedcharbroadcast[MAX_ADDR_LEN];/*hwbcastadd*/
unsignedcharpad;/*makedev_addralignedto8
bytes*/
unsignedchardev_addr[MAX_ADDR_LEN];/*hwaddress*/
unsignedcharaddr_len;/*hardwareaddresslength*/
unsignedlongpa_addr;/*protocoladdress*/
unsignedlongpa_brdaddr;/*protocolbroadcastaddr*/
unsignedlongpa_dstaddr;/*protocolP-Pothersideaddr*/
unsignedlongpa_mask;/*protocolnetmask*/
unsignedshortpa_alen;/*protocoladdresslength*/
structdev_mc_list*mc_list;/*Multicastmacaddresses*/
intmc_count;/*Numberofinstalledmcasts*/
structip_mc_list*ip_mc_list;/*IPmulticastfilterchain*/
__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);
linux操作系统文章专题:linux操作系统详解(linux不再难懂)
评论