我这里说的ioctl函数是指驱动程序里的,因为我不知道还有没有别的场合用到了它,所以就规定了我们讨论的范围。写这篇文章是因为我前一阵子被ioctl给搞混了,这几天才弄明白它,于是在这里清理一下头脑。

一、 什么是ioctl
    ioctl是设备驱动程序中对设备的I/O通道进行管理的函数。所谓对I/O通道进行管理,就是对设备的一些特性进行控制,例如串口的传输波特率、马达的转速等等。它的调用个数如下:
int ioctl(int fd, ind cmd, …);
    其中fd是用户程序打开设备时使用open函数返回的文件标示符,cmd是用户程序对设备的控制命令,至于后面的省略号,那是一些补充参数,一般最多一个,这个参数的有无和cmd的意义相关。
    ioctl函数是文件结构中的一个属性分量,就是说如果你的驱动程序提供了对ioctl的支持,用户就可以在用户程序中使用ioctl函数来控制设备的I/O通道。

二、 ioctl的必要性
    如果不用ioctl的话,也可以实现对设备I/O通道的控制,但那是蛮拧了。例如,我们可以在驱动程序中实现write的时候检查一下是否有特殊约定的数据流通过,如果有的话,那么后面就跟着控制命令(一般在socket编程中常常这样做)。但是如果这样做的话,会导致代码分工不明,程序结构混乱,程序员自己也会头昏眼花的。所以,我们就使用ioctl来实现控制的功能。要记住,用户程序所作的只是通过命令码(cmd)告诉驱动程序它想做什么,至于怎么解释这些命令和怎么实现这些命令,这都是驱动程序要做的事情。

三、 ioctl如何实现
    这是一个很麻烦的问题,我是能省则省。要说清楚它,没有四五千字是不行的,所以我这里是不可能把它说得非常清楚了,不过如果读者对用户程序是怎么和驱动程序联系起来感兴趣的话,可以看我前一阵子写的《write的奥秘》。读者只要把write换成ioctl,就知道用户程序的ioctl是怎么和驱动程序中的ioctl实现联系在一起的了。我这里说一个大概思路,因为我觉得《Linux设备驱动程序》这本书已经说的非常清楚了,但是得花一些时间来看。
    在驱动程序中实现的ioctl函数体内,实际上是有一个switch{case}结构,每一个case对应一个命令码,做出一些相应的操作。怎么实现这些操作,这是每一个程序员自己的事情。因为设备都是特定的,这里也没法说。关键在于怎样组织命令码,因为在ioctl中命令码是唯一联系用户程序命令和驱动程序支持的途径。命令码的组织是有一些讲究的,因为我们一定要做到命令和设备是一一对应的,这样才不会将正确的命令发给错误的设备,或者是把错误的命令发给正确的设备,或者是把错误的命令发给错误的设备。这些错误都会导致不可预料的事情发生,而当程序员发现了这些奇怪的事情的时候,再来调试程序查找错误,那将是非常困难的事情。所以在Linux核心中是这样定义一个命令码的:
____________________________________
| 设备类型 | 序列号 | 方向 |数据尺寸|
|----------|--------|------|--------|
| 8 bit      | 8 bit    |  2 bit |8~14 bit|
|----------|--------|------|--------|

    这样一来,一个命令就变成了一个整数形式的命令码;但是命令码非常的不直观,所以Linux Kernel中提供了一些宏。这些宏可根据便于理解的字符串生成命令码,或者是从命令码得到一些用户可以理解的字符串以标明这个命令对应的设备类型、设备序列号、数据传送方向和数据传输尺寸。

    这些宏我就不在这里解释了,具体的形式请读者察看Linux核心源代码中的宏,文件里给这些宏做了完整的定义。这里我只多说一个地方,那就是"幻数"。 "幻数"是一个字母,数据长度也是8,用一个特定的字母来标明设备类型,这和用一个数字是一样的,只是更加利于记忆和理解。就是这样,再没有更复杂的了。 更多的说了也没用,读者还是看一看源代码吧,推荐各位阅读《Linux 设备驱动程序》所带源代码中的short一例,因为它比较短小,功能比较简单,可以看明白ioctl的功能和细节。

四、 cmd参数如何得出
    这里确实要说一说,cmd参数在用户程序端由一些宏根据设备类型、序列号、传送方向、数据尺寸等生成,这个整数通过系统调用传递到内核中的驱动程序,再由驱动程序使用解码宏从这个整数中得到设备的类型、序列号、传送方向、数据尺寸等信息,然后通过switch{case}结构进行相应的操作。要透彻理解,只能是通过阅读源代码,我这篇文章实际上只是一个引子。cmd参数的组织还是比较复杂的,我认为要搞熟它还是得花不少时间的,但是这是值得的,因为驱动程序中最难的是对中断的理解。

五、 小结
    ioctl其实没有什么很难的东西需要理解,关键是理解cmd命令码是怎么在用户程序里生成并在驱动程序里解析的,程序员最主要的工作量在switch{case}结构中,因为对设备的I/O控制都是通过这一部分的代码实现的。
 

下面以几个示例代码来说明问题:

程序1:检测接口的inet_addr, netmask, broad_addr

 
  
  1. #include <stdio.h>  
  2. #include <string.h>  
  3. #include <stdlib.h>  
  4. #include <errno.h>  
  5. #include <unistd.h>  
  6. #include <sys/types.h>  
  7. #include <sys/socket.h>  
  8. #include <netinet/in.h>  
  9. #include <arpa/inet.h>  
  10. #include <sys/ioctl.h>  
  11. #include <net/if.h>  
  12.  
  13. static void usage()  
  14. {  
  15.    printf("usage : ipconfig interface \n");  
  16.    exit(0);  
  17. }  
  18. int main(int argc,char **argv)  
  19. {  
  20.    struct sockaddr_in *addr;  
  21.    struct ifreq ifr;  
  22.    char *name,*address;  
  23.    int sockfd;  
  24.  
  25.    if(argc != 2)    
  26.          usage();  
  27.    else    
  28.         name = argv[1];  
  29.  
  30.    sockfd = socket(AF_INET,SOCK_DGRAM,0);  
  31.    strncpy(ifr.ifr_name,name,IFNAMSIZ-1);  
  32.  
  33.    if(ioctl(sockfd,SIOCGIFADDR,&ifr) == -1)  
  34.          perror("ioctl error");  
  35.     exit(1);  
  36.  
  37.    addr = (struct sockaddr_in *)&(ifr.ifr_addr);  
  38.    address = inet_ntoa(addr->sin_addr);  
  39.    printf("inet addr: %s ",address);  
  40.  
  41.    if(ioctl(sockfd,SIOCGIFBRDADDR,&ifr) == -1)  
  42.       perror("ioctl error"),exit(1);  
  43.  
  44.    addr = (struct sockaddr_in *)&ifr.ifr_broadaddr;  
  45.    address = inet_ntoa(addr->sin_addr);  
  46.    printf("broad addr: %s ",address);  
  47.  
  48.    if(ioctl(sockfd,SIOCGIFNETMASK,&ifr) == -1)  
  49.       perror("ioctl error"),exit(1);  
  50.    addr = (struct sockaddr_in *)&ifr.ifr_addr;  
  51.    address = inet_ntoa(addr->sin_addr);  
  52.    printf("inet mask: %s ",address);  
  53.  
  54.    printf("\n");  
  55.    exit(0);  
  56. }
  57. 程序2:检查接口的物理连接是否正常
     
        
    1. #include <stdio.h>  
    2. #include <string.h>  
    3. #include <errno.h>  
    4. #include <fcntl.h>  
    5. #include <getopt.h>  
    6. #include <sys/socket.h>  
    7. #include <sys/ioctl.h>  
    8. #include <net/if.h>  
    9. #include <stdlib.h>  
    10. #include <unistd.h>  
    11. typedef unsigned short u16;  
    12. typedef unsigned int u32;  
    13. typedef unsigned char u8;  
    14. #include <linux/ethtool.h>  
    15. #include <linux/sockios.h>  
    16.  
    17. int detect_mii(int skfd, char *ifname)  
    18. {  
    19.    struct ifreq ifr;  
    20.    u16 *data, mii_val;  
    21.    unsigned phy_id;  
    22.  
    23.    /* Get the vitals from the interface. */ 
    24.    strncpy(ifr.ifr_name, ifname, IFNAMSIZ);  
    25.  
    26.    if (ioctl(skfd, SIOCGMIIPHY, &ifr) < 0)  
    27.       {  
    28.          fprintf(stderr, "SIOCGMIIPHY on %s failed: %s\n", ifname, strerror(errno));  
    29.          (void) close(skfd);  
    30.          return 2;  
    31.       }  
    32.  
    33.    data = (u16 *)(&ifr.ifr_data);  
    34.    phy_id = data[0];  
    35.    data[1] = 1;  
    36.  
    37.    if (ioctl(skfd, SIOCGMIIREG, &ifr) < 0)  
    38.      {  
    39.         fprintf(stderr, "SIOCGMIIREG on %s failed: %s\n", ifr.ifr_name, strerror(errno));  
    40.         return 2;  
    41.      }  
    42.  
    43.    mii_val = data[3];  
    44.    return(((mii_val & 0x0016) == 0x0004) ? 0 : 1);  
    45. }  
    46.  
    47. int detect_ethtool(int skfd, char *ifname)  
    48. {  
    49.    struct ifreq ifr;  
    50.    struct ethtool_value edata;  
    51.    memset(&ifr, 0, sizeof(ifr));  
    52.    edata.cmd = ETHTOOL_GLINK;  
    53.  
    54.    strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name)-1);  
    55.    ifr.ifr_data = (char *) &edata;  
    56.  
    57.    if (ioctl(skfd, SIOCETHTOOL, &ifr) == -1)  
    58.      {  
    59.         printf("ETHTOOL_GLINK failed: %s\n", strerror(errno));  
    60.         return 2;  
    61.      }  
    62.  
    63.    return (edata.data ? 0 : 1);  
    64. }  
    65.  
    66. int main(int argc, char **argv)  
    67. {  
    68.    int skfd = -1;  
    69.    char *ifname;  
    70.    int retval;  
    71.  
    72.    if( argv[1] )  ifname = argv[1];  
    73.      else  ifname = "eth0";  
    74.  
    75.    /* Open a socket. */ 
    76.    if (( skfd = socket( AF_INET, SOCK_DGRAM, 0 ) ) < 0 )  
    77.       {  
    78.          printf("socket error\n");  
    79.          exit(-1);  
    80.       }  
    81.  
    82.    retval = detect_ethtool(skfd, ifname);  
    83.    if (retval == 2)  
    84.      retval = detect_mii(skfd, ifname);  
    85.  
    86.    close(skfd);  
    87.    
    88.    if (retval == 2)  
    89.      printf("Could not determine status\n");  
    90.    if (retval == 1)  
    91.      printf("Link down\n");  
    92.    if (retval == 0)  
    93.      printf("Link up\n");  
    94.  
    95.    return retval;  
    96. }  
    97.  程序3:测试物理连接
    98. *******************************程序3*****************************************************  
    99. #include <stdio.h>  
    100. #include <stdlib.h>  
    101. #include <string.h>  
    102. #include <errno.h>  
    103. #include <net/if.h>  
    104. #include <linux/sockios.h>  
    105. #include <sys/ioctl.h>  
    106.  
    107. #define LINKTEST_GLINK 0x0000000a  
    108.  
    109. struct linktest_value {  
    110.         unsigned int    cmd;  
    111.         unsigned int    data;  
    112. };  
    113.  
    114. static void usage(const char * pname)  
    115. {  
    116.    fprintf(stderr, "usage: %s <device>\n", pname);  
    117.    fprintf(stderr, "returns: \n");  
    118.    fprintf(stderr, "\t 0: link detected\n");  
    119.    fprintf(stderr, "\t%d: %s\n", ENODEV, strerror(ENODEV));  
    120.    fprintf(stderr, "\t%d: %s\n", ENONET, strerror(ENONET));  
    121.    fprintf(stderr, "\t%d: %s\n", EOPNOTSUPP, strerror(EOPNOTSUPP));  
    122.    exit(EXIT_FAILURE);  
    123. }  
    124. static int linktest(const char * devname)  
    125. {  
    126.    struct ifreq ifr;  
    127.    struct linktest_value edata;  
    128.    int fd;  
    129.    /* setup our control structures. */ 
    130.    memset(&ifr, 0, sizeof(ifr));  
    131.    strcpy(ifr.ifr_name, devname);  
    132.    /* open control socket. */ 
    133.    fd=socket(AF_INET, SOCK_DGRAM, 0);  
    134.    if(fd < 0 )   
    135.      return -ECOMM;  
    136.  
    137.    errno = 0;  
    138.    edata.cmd = LINKTEST_GLINK;  
    139.    ifr.ifr_data = (caddr_t)&edata;  
    140.  
    141.    if(!ioctl(fd, SIOCETHTOOL, &ifr))   
    142.       {  
    143.         if(edata.data)   
    144.         {  
    145.             fprintf(stdout, "link detected on %s\n", devname);  
    146.             return 0;  
    147.         }   
    148.         else   
    149.         {  
    150.                errno=ENONET;  
    151.         }  
    152.      }  
    153.    perror("linktest");  
    154.    return errno;  
    155. }  
    156. int main(int argc, char *argv[])  
    157. {  
    158.    if(argc != 2)   
    159.          usage(argv[0]);  
    160.    return linktest(argv[1]);  
    程序4:调节音量
 
  
  1. #include <sys/types.h>  
  2. #include <sys/stat.h>  
  3. #include <fcntl.h>  
  4. #include <sys/ioctl.h>  
  5. #include <sys/soundcard.h>  
  6. #include <stdio.h>  
  7. #include <unistd.h>  
  8. #include <math.h>  
  9. #include <string.h>  
  10. #include <stdlib.h>  
  11. #define  BASE_VALUE 257  
  12.  
  13. int main(int argc,char *argv[])  
  14. {  
  15.    int mixer_fd=0;  
  16.    char *names[SOUND_MIXER_NRDEVICES]=SOUND_DEVICE_LABELS;  
  17.    int value,i;  
  18.  
  19.    printf("\nusage:%s dev_no.[0..24] value[0..100]\n\n",argv[0]);  
  20.    printf("eg. %s 0 100\n",argv[0]);  
  21.    printf("will change the volume to MAX volume.\n\n");  
  22.    printf("The dev_no. are as below:\n");  
  23.  
  24.    for (i=0;i<SOUND_MIXER_NRDEVICES;i++)  
  25.      {  
  26.         if (i%3==0) printf("\n");  
  27.         printf("%s:%d\t\t",names[i],i);  
  28.      }  
  29.  
  30.    printf("\n\n");  
  31.  
  32.    if (argc<3)  exit(1);  
  33.  
  34.    if ((mixer_fd = open("/dev/mixer",O_RDWR)))  
  35.      {  
  36.          printf("Mixer opened successfully,working...\n");  
  37.          value=BASE_VALUE*atoi(argv[2]);  
  38.  
  39.          if (ioctl(mixer_fd,MIXER_WRITE(atoi(argv[1])),&value)==0)  
  40.             printf("successfully.....");  
  41.          else 
  42.             printf("unsuccessfully.....");  
  43.          
  44.          printf("done.\n");  
  45.      }  
  46.     else 
  47.       printf("can't open /dev/mixer error....\n");  
  48.      exit(0);