Chinaunix首页 | 论坛 | 博客
  • 博客访问: 185087
  • 博文数量: 37
  • 博客积分: 171
  • 博客等级: 入伍新兵
  • 技术积分: 315
  • 用 户 组: 普通用户
  • 注册时间: 2011-01-13 22:54
个人简介

寻找方向程序猿、攻城狮

文章存档

2023年(1)

2022年(4)

2019年(1)

2018年(1)

2017年(1)

2015年(2)

2014年(19)

2013年(2)

2012年(1)

2011年(5)

分类: LINUX

2014-03-19 10:13:20


  1. /*
  2.  * ddi.c    Implement the Device Driver Interface (DDI) routines.
  3.  *        Currently, this is only used by the NET layer of LINUX,
  4.  *        but it eventually might move to an upper directory of
  5.  *        the system.
  6.  *
  7.  * Version:    @(#)ddi.c    1.0.5    04/22/93
  8.  *
  9.  * Author:    Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org>
  10.  */
  11. /*1.0版本首次加入网络部分的代码,所以代码设计还不是很完善。ddi.c/h实现了设备驱动接口(DDI),后面计划把这部分代码移到更高的层次,作为所有设备的接口,而不是仅用于linux的网络层*/
  12. #include <asm/segment.h>
  13. #include <asm/system.h>
  14. #include <linux/types.h>
  15. #include <linux/config.h>
  16. #include <linux/kernel.h>
  17. #include <linux/sched.h>
  18. #include <linux/string.h>
  19. #include <linux/mm.h>
  20. #include <linux/socket.h>
  21. #include <linux/ddi.h>

  22. /*开启调试开关。使用printk来打印输出。*/
  23. #undef    DDI_DEBUG
  24. #ifdef    DDI_DEBUG
  25. # define PRINTK(x)    printk x
  26. #else
  27. # define PRINTK(x)    /**/
  28. #endif

  29. /*声明全局的DDI设备控制块数组*/
  30. extern struct ddi_device    devices[];    /* device driver map    */
  31. /*用于设备网络协议*/
  32. extern struct ddi_proto        protocols[];    /* network protocols    */
  33. /*struct ddi_device和struct ddi_proto在ddi.h中定义*/

  34. /*
  35.  * This function gets called with an ASCII string representing the
  36.  * ID of some DDI driver. We loop through the DDI Devices table
  37.  * and return the address of the control block that has a matching
  38.  * "name" field. It is used by upper-level layers that want to
  39.  * dynamically bind some UNIX-domain "/dev/XXXX" file name to a
  40.  * DDI device driver. The "iflink(8)" program is an example of
  41.  * this behaviour.
  42.  */
  43. /*这个函数主要用于上层动态绑定一些UNIX域到一个DDI设备驱动。传入参数是ASCII字符串,代表一些DDI驱动的ID。我们遍历DDI设备表并返回name字段匹配传入参数的控制块。iflink(8)程序是这个行为的一个例子。*/
  44. struct ddi_device *
  45. ddi_map(const char *id)
  46. {
  47.   register struct ddi_device *dev;

  48.   PRINTK (("DDI: MAP: looking for \"%s\": ", id));
  49.   dev = devices;
  50.   while (dev->title != NULL) {
  51.     if (strncmp(dev->name, id, DDI_MAXNAME) == 0) {
  52.         PRINTK (("OK at 0x%X\n", dev));
  53.         return(dev);
  54.     }
  55.     dev++;
  56.   }
  57.   PRINTK (("NOT FOUND\n"));
  58.   return(NULL);
  59. }


  60. /*
  61.  * This is the function that is called by a kernel routine during
  62.  * system startup. Its purpose is to walk trough the "devices"
  63.  * table (defined above), and to call all moduled defined in it.
  64.  */
  65. /*这个函数在内核启动的函数中调用。它的目标是遍历“devices”表(上面定义的),然后调用在“devices”表(上面定义的protocols和devices两个数组)里面定义的初始化函数对每个协议和设备进行初始化*/
  66. void
  67. ddi_init(void)
  68. {
  69.   struct ddi_proto *pro;
  70.   struct ddi_device *dev;

  71.   PRINTK (("DDI: Starting up!\n"));

  72.   /* First off, kick all configured protocols. */
  73.   pro = protocols;
  74.   while (pro->name != NULL) {
  75.     (*pro->init)(pro);
  76.     pro++;
  77.   }
  78.   
  79.   /* Done. Now kick all configured device drivers. */
  80.   dev = devices;
  81.   while (dev->title != NULL) {
  82.     (*dev->init)(dev);
  83.     dev++;
  84.   }

  85.   /* We're all done... */
  86. }

  1. /*
  2.  * ddi.h    Define the structure for linking in I/O drivers into the
  3.  *        operating system kernel. This method is currently only
  4.  *        used by NET layer drivers, but it will be expanded into
  5.  *        a link methos for ALL kernel-resident device drivers.
  6.  *
  7.  * Version:    @(#)ddi.h    1.0.2    04/22/93
  8.  *
  9.  * Author:    Fred N. van Kempen, <waltje@uwalt.nl.mugnet.org>
  10.  */
  11. /*ddi.h 定义了链接I/O驱动到操作系统核心的数据结构。目前只有内核网络层驱动使用这个方法,但以后这个方法会扩展成所有驻留内核的设备驱动的链接方法。*/
  12. #ifndef _LINUX_DDI_H
  13. #define _LINUX_DDI_H


  14. /* DDI control block flags. */
  15. /*DDI控制块标志位*/
  16. #define DDI_FREADY    0x10000000    /* device is initialized *//*设备被初始化*/
  17. #define DDI_FPRESENT    0x20000000    /* device hardware is present *//*设备硬件已存在*/
  18. #define DDI_FBLKDEV    0x00000001    /* device has a BLK spec. file *//*设备有一个BLK spec.文件*/
  19. #define DDI_FCHRDEV    0x00000002    /* device has a CHR spec. file *//*设备有一个CHR spec.文件*//*这些BLK/CHR spec.文件是什么?*/

  20. /* Various constants. */
  21. #define DDI_MAXNAME    16        /* length of a DDI ID string    */


  22. /* This structure is used to set up a DDI driver. */
  23. struct ddconf {
  24.   int        ioaddr;            /* main I/O (port) address    */
  25.   int        ioaux;            /* auxiliary I/O (HD, AST)    */
  26.   int        irq;            /* IRQ channel            */
  27.   int        dma;            /* DMA channel to use        */
  28.   unsigned long    memsize;        /* size of onboard memory    */
  29.   unsigned long    memaddr;        /* base address of memory    */
  30. };


  31. /* The DDI device control block. */
  32. struct ddi_device {
  33.   char        *title;            /* title of the driver        */
  34.   char        name[DDI_MAXNAME];    /* unit name of the I/O driver    */
  35.   short int    unit;            /* unit number of this driver    */
  36.   short int    nunits;            /* number of units in driver    */
  37.   int        (*init)(struct ddi_device *);    /* initialization func        */
  38.   int        (*handler)(int, ...);    /* command handler        */
  39.   short    int    major;            /* driver major dev number    */
  40.   short    int    minor;            /* driver minor dev number    */
  41.   unsigned long    flags;            /* various flags        */
  42.   struct ddconf config;            /* driver HW setup        */
  43. };


  44. /* This structure is used to set up networking protocols. */
  45. struct ddi_proto {
  46.   char        *name;            /* protocol name        */
  47.   void        (*init)(struct ddi_proto *);    /* initialization func    */
  48. };


  49. /* This structure is used to link a STREAMS interface. */
  50. struct iflink {
  51.   char        id[DDI_MAXNAME];    /* DDI ID string        */
  52.   char        stream[DDI_MAXNAME];    /* STREAMS interface name    */
  53.   int        family;            /* address (protocol) family    */
  54.   unsigned int    flags;            /* any flags needed (unused)    */
  55. };


  56. /* DDI control requests. */
  57. #define DDIOCSDBG    0x9000        /* set DDI debug level        */
  58. #define DDIOCGNAME    0x9001        /* get DDI ID name        */
  59. #define DDIOCGCONF    0x9002        /* get DDI HW config        */
  60. #define DDIOCSCONF    0x9003        /* set DDI HW config        */


  61. /* DDI global functions. */
  62. extern void            ddi_init(void);
  63. extern struct ddi_device    *ddi_map(const char *id);


  64. #endif    /* _LINUX_DDI_H */


阅读(2554) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~