Linux 网卡设备驱动

               

Linux 网卡驱动


刺猬@http://blog.csdn.net/littlehedgehog






网上一位前辈写的,时至今日,代码很多编译通不过(主要是Linux 内核变化实在太快),我把代码移植到我的ubuntu 8.10下测试成功,里面也加上了我的注解。不过还有不少东西没有搞懂,手头上也没有相关的硬件资料,就一份Realtek 的datasheet。 TNND,后面要备考备荒,手头的事情只能放一放。



  1. #include <linux/module.h>
  2. #include <linux/kernel.h>
  3. #include <linux/compiler.h>
  4. #include <linux/pci.h>
  5. #include <linux/init.h>
  6. #include <linux/ioport.h>
  7. #include <linux/netdevice.h>
  8. #include <linux/etherdevice.h>
  9. #include <linux/rtnetlink.h>
  10. #include <linux/delay.h>
  11. #include <linux/ethtool.h>
  12. #include <linux/completion.h>
  13. #include <linux/crc32.h>
  14. #include <linux/mii.h>
  15. #include <asm/io.h>
  16. #include <asm/uaccess.h>
  17. #include <asm/irq.h>
  18. #if defined(CONFIG_SH_DREAMCAST)
  19. #define RX_BUF_IDX  1   /* 16K ring */
  20. #else
  21. #define RX_BUF_IDX  2   /* 32K ring */
  22. #endif
  23. #define RX_BUF_LEN  (8192 << RX_BUF_IDX)
  24. #define RX_BUF_PAD  16
  25. #define RX_BUF_WRAP_PAD 2048 /* spare padding to handle lack of packet wrap */
  26. #if RX_BUF_LEN == 65536
  27. #define RX_BUF_TOT_LEN  RX_BUF_LEN
  28. #else
  29. #define RX_BUF_TOT_LEN  (RX_BUF_LEN + RX_BUF_PAD + RX_BUF_WRAP_PAD)
  30. #endif
  31. typedef enum
  32. {
  33.     RTL8139 = 0,
  34.     RTL8129,
  35. } board_t;
  36. static struct pci_device_id xc_id[] =
  37. {
  38.     {0x10ec, 0x8139, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },
  39.     {0x10ec, 0x8138, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },
  40.     {0x1113, 0x1211, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },
  41.     {0x1500, 0x1360, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },
  42.     {0x4033, 0x1360, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },
  43.     {0x1186, 0x1300, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },
  44.     {0x1186, 0x1340, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },
  45.     {0x13d1, 0xab06, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },
  46.     {0x1259, 0xa117, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },
  47.     {0x1259, 0xa11e, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },
  48.     {0x14ea, 0xab06, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },
  49.     {0x14ea, 0xab07, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },
  50.     {0x11db, 0x1234, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },
  51.     {0x1432, 0x9130, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },
  52.     {0x02ac, 0x1012, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },
  53.     {0x018a, 0x0106, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },
  54.     {0x126c, 0x1211, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },
  55.     {0x1743, 0x8139, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },
  56.     {0x021b, 0x8139, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },
  57. #ifdef CONFIG_SH_SECUREEDGE5410
  58.     /* Bogus 8139 silicon reports 8129 without external PROM :-*/
  59.     {0x10ec, 0x8129, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8139 },
  60. #endif
  61. #ifdef CONFIG_8139TOO_8129
  62.     {0x10ec, 0x8129, PCI_ANY_ID, PCI_ANY_ID, 0, 0, RTL8129 },
  63. #endif
  64.     {PCI_ANY_ID, 0x8139, 0x10ec, 0x8139, 0, 0, RTL8139 },
  65.     {PCI_ANY_ID, 0x8139, 0x1186, 0x1300, 0, 0, RTL8139 },
  66.     {PCI_ANY_ID, 0x8139, 0x13d1, 0xab06, 0, 0, RTL8139 },
  67.     {0,}
  68. };
  69. MODULE_DEVICE_TABLE (pci,xc_id);
  70. enum RTL8139_registers
  71. {
  72.     MAC0 = 0,       /* Ethernet hardware address. */
  73.     MAR0 = 8,       /* Multicast filter. */
  74.     TxStatus0 = 0x10,   /* Transmit status (Four 32bit registers). */
  75.     TxAddr0 = 0x20,     /*物理地址*/
  76.     RxBuf = 0x30,       /*物理地址*/
  77.     ChipCmd = 0x37,
  78.     RxBufPtr = 0x38,
  79.     IntrMask = 0x3C,
  80.     IntrStatus = 0x3E,
  81.     TxConfig = 0x40,
  82.     RxConfig = 0x44,
  83.     RxMissed = 0x4C,
  84.     Cfg9346 = 0x50,
  85.     Config1 = 0x52,
  86.     Config3 = 0x59,
  87.     Config4 = 0x5A,
  88.     HltClk = 0x5B,
  89.     MultiIntr = 0x5C,
  90.     /*MII*/
  91.     BasicModeCtrl = 0x62,
  92.     BasicModeStatus = 0x64,
  93.     NWayAdvert = 0x66,
  94.     NWayLPAR = 0x68,
  95.     NWayExpansion = 0x6A,
  96.     /*MII*/
  97.     CSCR = 0x74,
  98. };
  99. enum ChipCmdBits  /*ChipCmd = 0x37 register*/
  100. {
  101.     CmdReset = 0x10,/*chip重置*/
  102.     CmdRxEnb = 0x08,/*开启读*/
  103.     CmdTxEnb = 0x04,/*开启写*/
  104.     RxBufEmpty = 0x01,/*如果设置这个位表示接收缓冲区为空*/
  105. };
  106. enum IntrStatusBits
  107. {
  108.     PCIErr = 0x8000,
  109.     PCSTimeout = 0x4000,
  110.     RxFIFOOver = 0x40,
  111.     RxUnderrun = 0x20,
  112.     RxOverflow = 0x10,
  113.     TxErr = 0x08,
  114.     TxOK = 0x04,
  115.     RxErr = 0x02,
  116.     RxOK = 0x01,
  117.     RxAckBits = RxFIFOOver | RxOverflow | RxOK,
  118. };
  119. static const u16 xc_intr_mask =
  120.     PCIErr | PCSTimeout | RxUnderrun | RxOverflow | RxFIFOOver |
  121.     TxErr | TxOK | RxErr | RxOK;
  122. static const u16 xc_norx_intr_mask =/*不包含任何接收中断*/
  123.     PCIErr | PCSTimeout | RxUnderrun | TxErr | TxOK | RxErr ;
  124. enum Config1Bits  /*Config1 = 0x52*/
  125. {
  126.     Cfg1_PM_Enable = 0x01,/*开启电源管理(cfg9346要设置为可写)*/
  127.     LWAKE = 0x10,/*于Config4的LWPTN共同工作 同时设置为0为(active high)*/
  128. };
  129. enum Config3Bits  /*Config3 = 0x59*/
  130. {
  131.     Cfg3_Magic     = (1 << 5), /* 1 = wake up on Magic Packet (tm) */
  132. };
  133. enum Config4Bits  /*Config4 = 0x5A*/
  134. {
  135.     LWPTN = (1 << 2),/*于Config1的LWAKE共同工作 同时设置为0为(active high)*/
  136. };
  137. enum Cfg9346Bits
  138. {
  139.     Cfg9346_lock = 0x00,/*一般状态*/
  140.     Cfg9346_Unlock = 0xC0,/*可写状态*/
  141. };
  142. enum TxStatusBits  /*TxStatus0 = 0x10 共4个发送状态register 0x10-0x1f*/
  143. {
  144.     TxHostOwns = 0x2000,
  145.     TxUnderrun = 0x4000,/*在发送数据时如果,Tx FIFO耗尽时设置为1*/
  146.     TxStatOK = 0x8000,/*发送数据成功*/
  147.     TxOutOfWindow = 0x20000000,/*发生 "out of windown" 冲突*/
  148.     TxAborted = 0x40000000,/*设置为1表示发送被中止,该位是只读的*/
  149.     TxCarrierLost = 0x80000000,/*在发送数据包时,carrier丢失*/
  150. };
  151. /* Bits in RxConfig. */
  152. enum rx_mode_bits
  153. {
  154.     AcceptErr = 0x20,
  155.     AcceptRunt = 0x10,
  156.     AcceptBroadcast = 0x08,
  157.     AcceptMulticast = 0x04,
  158.     AcceptMyPhys = 0x02,
  159.     AcceptAllPhys = 0x01,
  160. };
  161. typedef enum
  162. {
  163.     CH_8139 = 0,
  164.     CH_8139_K,
  165.     CH_8139A,
  166.     CH_8139A_G,
  167.     CH_8139B,
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值