Linux字符设备驱动模型
1. linux设备驱动程序框架
Linux将所有外部设备看成是一类特殊文件,称之为“设备文件”,设备驱动程序可以看成是Linux内核与外部设备之间的接口。
在Linux操作系统下有两类主要的设备文件:一类是字符设备,另一类则是块设备。字符设备是以字节为单位逐个进行I/O操作的设备,在对字符设备发出读写请求时,实际的硬件I/O紧接着就发生了,一般来说字符设备中的缓存是可有可无的,而且也不支持随机访问。块设备则是利用一块系统内存作为缓冲区,当用户进程对设备进行读写请求时,驱动程序先查看缓冲区中的内容,如果缓冲区中的数据能满足用户的要求就返回相应的数据,否则就调用相应的请求函数来进行实际的I/O操作。块设备主要是针对磁盘等慢速设备设计的,其目的是避免耗费过多的CPU时间来等待操作的完成。
2. 编写linux字符设备驱动
编写Linux字符设备驱动,主要有5步:
1)分配一个file_operation结构体;
2)设置file_operation结构体;
4)创建设备节点;
4)入口(模块加载);
5)出口(卸载函数);
具体代码hello_driver.c:
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <asm/io.h>
#include <linux/delay.h>
#include <linux/cdev.h>
#include <asm/uaccess.h>
#include <linux/fs.h>
#include <linux/of.h>
#include <linux/of_device.h>
static struct class *hello_driver_class;
static struct device *hello_driver_dev;
unsigned char test_data[3] = {0};
#define TEST_ONE 0x01
#define TEST_TWO 0x22
static int hello_driver_open(struct inode *inode, struct file *filp)
{
test_data[0] = 0x11;
test_data[1] = 0x22;
test_data[2] = 0x33;
printk("hello driver open......\r\n");
return 0;
}
static int hello_driver_read( struct file *filp,char __user *buf,size_t count,loff_t *f_pos)
{
//data是实际的数据,目前是测试数据
unsigned char data[3] = {0};
memcpy(data,test_data,3);
copy_to_user(buf,data,count);
printk("hello_driver_read......\r\n");
return sizeof(data)/sizeof(data[0]);
}
static int hello_driver_write(struct file *filp,const char *buf,size_t count,loff_t *f_pos)
{
unsigned char data[3] = {0};
copy_from_user(data,buf,count);
memcpy(test_data,data,3);
printk("hello_driver_write......\r\n");
return sizeof(test_data)/sizeof(test_data[0]);
}
static long hello_driver_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
{
switch(cmd)
{
case TEST_ONE:
printk("hello_driver_ioctl test one....\r\n");
break;
case TEST_TWO:
printk("hello_driver_ioctl test two....\r\n");
break;
default:
break;
}
return 0;
}
static int hello_driver_release(struct inode *inode, struct file *filp)
{
printk("hello_driver_release...\n");
return 0;
}
static const struct file_operations hello_driver_fops =
{
.owner = THIS_MODULE,
.read = hello_driver_read,
.write = hello_driver_write,
.open = hello_driver_open,
.unlocked_ioctl = hello_driver_ioctl,
.release = hello_driver_release,
};
#define DEVICE_NAME "hello_driver"
int result = 0;
static int __init hello_driver_init(void)
{
result = register_chrdev(0,DEVICE_NAME,&hello_driver_fops); //注册字符设备
if(result < 0)
{
printk(KERN_INFO"__hello_driver_init register failed\n");
return -ENODEV;
}
//自动为设备创建节点
hello_driver_class = class_create(THIS_MODULE,DEVICE_NAME);
hello_driver_dev = device_create(hello_driver_class, NULL, MKDEV(result, 0),NULL,DEVICE_NAME);
printk(KERN_ALERT "\nhello_driver_init linux driver\n");
return 0;
}
static void __exit hello_driver_exit(void)
{
unregister_chrdev(result,DEVICE_NAME);
device_destroy(hello_driver_class,MKDEV(result, 0));
class_destroy(hello_driver_class);
printk(KERN_ALERT "hello_driver Goodbye linux driver\n\n");
}
module_init(hello_driver_init);
module_exit(hello_driver_exit);
MODULE_LICENSE("GPL");
Makefile文件:
KERN_SRC := /lib/modules/$(shell uname -r)/build
obj-m := hello_driver.o
all:
make -C $(KERN_SRC) M=`pwd` modules
clean:
make -C $(KERN_SRC) M=`pwd` clean
通过make编译驱动模块,
insmod hello_driver.ko
通过cat /proc/device,可以看见注册的设备hello_driver
编写测试代码driver_test.c:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#define TEST_ONE 0X01
#define TEST_TWO 0X22
int main(void)
{
int fd;
unsigned char read_data[3] = {0};
unsigned char write_data[3] = {0};
int ret,i;
fd = open("/dev/hello_driver",O_RDWR);
if (fd<0)
{
printf("error, can't open /dev/hello_driver.....\n");
return 0;
}
ret = read(fd,read_data,3);
for(i = 0; i < ret;i++)
printf("read_data[%d]:0x%x\n",i,read_data[i]);
printf("\n");
write_data[0] = 0xaa;
write_data[1] = 0xab;
write_data[2] = 0xac;
write(fd,write_data,3);
ret = read(fd,read_data,3);
for(i = 0; i < ret;i++)
printf("read_data[%d]:0x%x\n",i,read_data[i]);
ioctl(fd,TEST_ONE);
ioctl(fd,TEST_TWO);
close(fd);
return 0 ;
}
编译测试程序:
gcc driver_test.c -o driver_test
执行测试程序:
./driver_test
运行结果
使用dmesg查看驱动底层的打印输出。
dmesg
源码连接地址:(https://download.csdn.net/download/sleeepmy/12299682)