Embedded LINUX driver learning 5.ioctl character device driver programming (1) function description


Note: General character devices can use ioctl function instead of write() and read() functions

1. The corresponding function unlocked_ioct() used in kernel space

1.1 Function header file and prototype

//头文件位置:include/linux/fs.h
struct file_operations {
    
    
    ....忽略更多内容.....
    long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
    ....忽略更多内容.....
}

1.2 Initialize the unlocked_ioctl() function operation interface

#include <linux/fs.h>
 static long mycdev_ioctl (struct file * file, unsigned int kcmd, unsigned long kindex){
    
    
       /*   执行的操作   */
}
struct file_operations fops = {
    
    
    .owner = THIS_MODULE;
    .unlocked_ioctl = mycdev_ioctl;
}

2. Programming function in user space: ioctl(2)

// 可查看 :ioctl(2) 或查看内核源代码文件:Documentation/ input/ff.txt
#include <sys/ioctl.h>
int ioctl(int file_descriptor, int request, unsigned long *features);
参数说明:
          file_descriptor  : 文件描述符
          request          :一般传无符号整数,用于命令
          features         :无符号长整形,一般用于参数,
                             这个参数的好用之处在于:当内核空间执行copy_to_user时,相当于读操作;
                                                     当内核空间执行copy_from_user时,相当于写操作;
                                                     在ioctl字符设备驱动编程(二)中都有用到。

Guess you like

Origin blog.csdn.net/weixin_47273317/article/details/107848207