编写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");

测试程序
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;
}



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


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

板子操作

  1. 安装驱动:insmod hello_drv.ko

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

编写hello驱动程序_第1张图片

3.cat /proc/devices查看设备节点,看到hello节点已经被创建,这是再hello_drv.c文件里创建的节点,具体看hello_int函数
编写hello驱动程序_第2张图片

4.执行程序: 编写hello驱动程序_第3张图片

5.删除驱动程序 rmmod hello_drv

你可能感兴趣的:(1024程序员节)