使用misc机制创建设备驱动

上一节所讲解的驱动是需要手动创建设备节点的,这节讲解的misc机制是自动创建设备节点的。通过这一机制创建的主设备号都是10,使用miscdevice数据结构来抽象这类设备,这类设备是不符合预先确定的字符设备,所以利用misc机制创建的设备属于杂项设备。
杂项设备的注册和取消注册使用以下方法:

extern int misc_register(struct miscdevice *misc);
extern void misc_deregister(struct miscdevice *misc);

使用misc机制改写上一节的内容

  1. 添加头文件
#include 
  1. 增加miscdevice
static struct miscdevice char_misc_device = {
        .minor = MISC_DYNAMIC_MINOR,
        .name = DEMO_NAME,
        .fops = &char_device_fops,
};
  1. 修改simple_char_device_init和simple_char_device_exit
static int __init simple_char_device_init(void)
{
    int ret;
    ret = misc_register(&char_misc_device);
    if (ret) {
        printk("failed register misc device\n");
        return ret;
    }
    printk("successed regitster char device: %s\n",DEMO_NAME);
    return 0;
}

static void __exit simple_char_device_exit(void) {
    printk("removing device\n");
    misc_deregister(&char_misc_device);
}
  1. 去掉不必要的预加载
#include 
#include 
#include 

#define DEMO_NAME "char_misc_device"
  1. 所有内容如下:
#include 
#include 
#include 

#define DEMO_NAME "char_misc_device"

static int char_device_open(struct inode *inode, struct file *file)
{
    int major = MAJOR(inode->i_rdev);
    int minor = MINOR(inode->i_rdev);
    printk("%s: major=%d, minor=%d\n", __func__, major, minor);
    return 0;
}

static ssize_t char_device_read(struct file *file, char __user *buf,size_t lbuf, loff_t *ppos)
{
    printk("%s enter\n",__func__);
    return 0;
}

static int char_device_release(struct inode *inode, struct file *file)
{
    return 0;
}

static ssize_t char_device_write(struct file *file, const char __user *buf, size_t count, loff_t *f_pos)
{
    printk("%s enter\n",__func__);
    return 0;
}

static const struct file_operations char_device_fops = {
    .owner = THIS_MODULE,
    .open = char_device_open,
    .release = char_device_release,
    .read = char_device_read,
    .write = char_device_write
};

static struct miscdevice char_misc_device = {
        .minor = MISC_DYNAMIC_MINOR,
        .name = DEMO_NAME,
        .fops = &char_device_fops,
};

static int __init simple_char_device_init(void)
{
    int ret;
    ret = misc_register(&char_misc_device);
    if (ret) {
        printk("failed register misc device\n");
        return ret;
    }
    printk("successed regitster char device: %s\n",DEMO_NAME);
    return 0;
}

static void __exit simple_char_device_exit(void) {
    printk("removing device\n");
    misc_deregister(&char_misc_device);
}

module_init(simple_char_device_init);
module_exit(simple_char_device_exit);

MODULE_AUTHOR("fkq");
MODULE_LICENSE("GPL v2");
MODULE_DESCRIPTION("simple character device");
  1. 加载模块
$make
make -C /lib/modules/4.15.0-142-generic/build M=/media/fukaiqiang/Disk960/Codes/Linux modules
make[1]: Entering directory '/usr/src/linux-headers-4.15.0-142-generic'
  Building modules, stage 2.
  MODPOST 1 modules
make[1]: Leaving directory '/usr/src/linux-headers-4.15.0-142-generic'
$sudo insmod char_device.ko
[sudo] fukaiqiang 的密码:
$ls -l /dev/ |grep char_misc
crw-------  1 root root     10,    56 8月   9 22:24 char_misc_device

自动创建设备节点,其中主设备号是10,次设备号由系统动态分配。

sudo chmod 777 /dev/char_misc_device

注意:这里需要赋予权限。不然测试程序会访问不到。

  1. 编译运行测试程序
#include 
#include 
#include 

#define DEMO_DEV_NAME "/dev/char_misc_device"

int main() {
    char buffer[64];
    int fd;

    fd = open(DEMO_DEV_NAME, O_RDONLY);
    if (fd < 0) {
        printf("open device %s failed\n", DEMO_DEV_NAME);
        return -1;
    }

    read(fd, buffer, 64);
    close(fd);
    return 0;
}
gcc char_device_test.c -o char_device_test
$./char_device_test 
&dmesg
...
[1395767.809789] char_device_open: major=10, minor=56
[1395767.809797] char_device_read enter

内核版本

Linux version 4.15.0-142-generic

参考

奔跑吧Linux内核入门篇 张天飞著

你可能感兴趣的:(使用misc机制创建设备驱动)