• 编写hello驱动程序


    hello的驱动编写

    编写驱动程序的步骤

    1.确定主设备号,也可以让内核分配
    2.定义自己的 file_operations 结构
    3.实现对应的 drv_open/drv_read/drv_write 等函数,填入 file_operations 结构 体
    4.把 file_operations 结构体告诉内核:register_chrdev
    5.谁来注册驱动程序啊?得有一个入口函数:安装驱动程序时,就会去调用这 个入口函数
    6.有入口函数就应该有出口函数:卸载驱动程序时,出口函数调用unregister_chrdev
    7.其他完善:提供设备信息,自动创建设备节点:class_create,

    代码

    驱动代码
    hello_drv.c

    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    
    
    static ssize_t hello_read(struct file *file, char __user *buf, size_t count,loff_t *ppos);
    static ssize_t hello_write(struct file *file, const char __user *buf,size_t count, loff_t *ppos);
    static int hello_open(struct inode *inode, struct file *file);
    int hello_close(struct inode *inode, struct file *file);
    
    char kernel_buf[1024] = "www.ask100.com";
    static struct class *hello_class;
    #define MIN(a,b) (a<b ? a:b)
    
    
    //1确定主设备号,也可以让内核分配
    static unsigned int major = 0 ; 
    
    
    //2定义自己的 file_operations 结构体
    static const struct file_operations hello_fops = {
    	.owner	 = THIS_MODULE,
    	.open    = hello_open,
    	.read    = hello_read,
    	.write   = hello_write,
    	.release = hello_close,
    };
    
    
    //3.实现对应的 drv_open/drv_read/drv_write 等函数,填入 file_operations 结构体
    static int hello_open(struct inode *inode, struct file *file)
    {
    	printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
    	return 0;
    		
    }
    
    static ssize_t hello_write(struct file *file, const char __user *buf,size_t count, loff_t *ppos)
    {
    	int len;
    	printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
    	len = copy_from_user(kernel_buf, buf, MIN(1024,sizeof(kernel_buf)));
    	kernel_buf[len] = '\0';
    	return MIN(1024,sizeof(kernel_buf));
    	
    }
    
    
    static ssize_t hello_read(struct file *file, char __user *buf, size_t count,loff_t *ppos)
    {
    	int err;
    	printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
    	err = copy_to_user(buf, kernel_buf, MIN(1024,sizeof(kernel_buf)));
    	return MIN(1024,sizeof(kernel_buf));
    }
    
    int hello_close(struct inode *inode, struct file *file)
    {
    	printk("%s %s %d\n", __FILE__, __FUNCTION__, __LINE__);
    }
    
    
    //4.把 file_operations 结构体告诉内核:register_chrdev
    //5.谁来注册驱动程序啊?得有一个入口函数:安装驱动程序时,就会去调用这个入口函数
    static int __init hello_init(void)
    {
    	int err;
    
    	major = register_chrdev(0,"hello",&hello_fops);
    	
    	hello_class = class_create(THIS_MODULE, "hello");
    	err = PTR_ERR(hello_class);
    	if (IS_ERR(hello_class))
    		return -1;
    	device_create(hello_class, NULL, MKDEV(major, 0), NULL, "hello"); /*/dev/hello*/
    	return 0;
    
    }
    
    
    
    //6.有入口函数就应该有出口函数:卸载驱动程序时,出口函数调用unregister_chrdev
    
    static void __init hello_exit(void)
    {
    	
    	device_destroy(hello_class, MKDEV(major, 0));
    	class_destroy(hello_class);
        unregister_chrdev(major, "hello");
    
    }
    
    //7.其他完善:提供设备信息,自动创建设备节点:class_create,device_create
    module_init(hello_init);
    module_exit(hello_exit);
    MODULE_LICENSE("GPL");
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106

    测试程序
    hello_drv_test.c

    
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    
    /*
     * ./hello_drv_test -w abc
     * ./hello_drv_test -r
     */
    int main(int argc, char **argv)
    {
    	int fd;
    	char buf[1024];
    	int len;
    	
    	/* 1. 判断参数 */
    	if (argc < 2) 
    	{
    		printf("Usage: %s -w \n", argv[0]);
    		printf("       %s -r\n", argv[0]);
    		return -1;
    	}
    
    	/* 2. 打开文件 */
    	fd = open("/dev/hello", O_RDWR);
    	if (fd == -1)
    	{
    		printf("can not open file /dev/hello\n");
    		return -1;
    	}
    
    	/* 3. 写文件或读文件 */
    	if ((0 == strcmp(argv[1], "-w")) && (argc == 3))
    	{
    		len = strlen(argv[2]) + 1;
    		len = len < 1024 ? len : 1024;
    		write(fd, argv[2], len);
    	}
    	else
    	{
    		len = read(fd, buf, 1024);		
    		buf[1023] = '\0';
    		printf("APP read : %s\n", buf);
    	}
    	
    	close(fd);
    	
    	return 0;
    }
    
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55

    Makefile

    
    # 1. 使用不同的开发板内核时, 一定要修改KERN_DIR
    # 2. KERN_DIR中的内核要事先配置、编译, 为了能编译内核, 要先设置下列环境变量:
    # 2.1 ARCH,          比如: export ARCH=arm64
    # 2.2 CROSS_COMPILE, 比如: export CROSS_COMPILE=aarch64-linux-gnu-
    # 2.3 PATH,          比如: export PATH=$PATH:/home/book/100ask_roc-rk3399-pc/ToolChain-6.3.1/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin 
    # 注意: 不同的开发板不同的编译器上述3个环境变量不一定相同,
    #       请参考各开发板的高级用户使用手册
    
    KERN_DIR = /home/book/100ask_imx6ull-sdk/Linux-4.9.88
    
    all:
    	make -C $(KERN_DIR) M=`pwd` modules 
    	$(CROSS_COMPILE)gcc -o hello_drv_test hello_drv_test.c 
    
    clean:
    	make -C $(KERN_DIR) M=`pwd` modules clean
    	rm -rf modules.order
    	rm -f hello_test
    
    obj-m	+= hello_drv.o
    
    KERN_DIR = /home/book/100ask_imx6ull-sdk/Linux-4.9.88
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    用make命令编译后,将编译出的hello_drv.ko 和hello_drv_test传给板子

    板子操作

    1. 安装驱动:insmod hello_drv.ko

    2.lsmod : 查看已经安装的驱动,可以看到hello_drv已经安装

    在这里插入图片描述

    3.cat /proc/devices查看设备节点,看到hello节点已经被创建,这是再hello_drv.c文件里创建的节点,具体看hello_int函数
    在这里插入图片描述

    4.执行程序: 在这里插入图片描述

    5.删除驱动程序 rmmod hello_drv

  • 相关阅读:
    Nature子刊:用于阿尔茨海默病痴呆评估的多模态深度学习模型
    Linux 基础IO
    ISO20000认证流程是什么,ISO20000认证好处
    Robust Lane Detection from Continuous Driving
    【C++】C/C++内存管理
    贪心算法--概论
    Springboot写电商系统(2)
    some(),every()
    React - 01
    RuntimeError: ANTLR version mismatch
  • 原文地址:https://blog.csdn.net/m0_57678852/article/details/133985922