字符驱动模块程序
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/cdev.h>
static struct cdev chr_dev;
static dev_t ndev;
static int chr_open(struct inode* nd, struct file* filp)
{
int major ;
int minor;
major = MAJOR(nd->i_rdev);
minor = MINOR(nd->i_rdev);
printk("chr_open, major = %d, minor = %d\n", major, minor);
return 0;
}
static ssize_t chr_read(struct file* filp, char __user* u, size_t sz, loff_t* off)
{
printk("chr_read process!\n");
memcpy(u, "hello world", sz);
return 0;
}
struct file_operations chr_ops = {
.owner = THIS_MODULE,
.open = chr_open,
.read = chr_read
};
static int demo_init(void)
{
int ret;
cdev_init(&chr_dev, &chr_ops);
ret = alloc_chrdev_region(&ndev, 0, 1, "chr_dev");
if(ret < 0 )
{
return ret;
}
printk("demo_init(): major = %d, minor = %d\n", MAJOR(ndev), MINOR(ndev));
ret = cdev_add(&chr_dev, ndev, 1);
if(ret < 0)
{
return ret;
}
return 0;
}
static void demo_exit(void)
{
printk("demo_exit process!\n");
cdev_del(&chr_dev);
unregister_chrdev_region(ndev, 1);
}
module_init(demo_init);
module_exit(demo_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("xxxx");
MODULE_DESCRIPTION("A simple device example!");
创建编译字符驱动模块Makefile
ifneq ($(KERNELRELEASE),)
obj-m := demo.o
else
PWD := $(shell pwd)
KVER := $(shell uname -r)
KDIR := /lib/modules/$(KVER)/build
all:
$(MAKE) -C $(KDIR) M=$(PWD) modules
clean:
rm -rf .*.cmd *.o *.mod.c *.ko .tmp_versions modules.* Module.*
endif
编译字符驱动模块程序
make
加载驱动模块
insmod demo
设置驱动模块的主从设备号
mknod /dev/demo 250 0
测试程序
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#define CHAR_DEV_NAME "/dev/demo"
int main(int argc, char const *argv[])
{
int fd = 0;
char buf[32] = {'\0'};
fd = open(CHAR_DEV_NAME, O_RDONLY|O_NDELAY);
if(0 > fd)
{
printf("open failed\n");
return -1;
}
read(fd, buf, 32);
printf("buf = (%s)\n", buf);
close(fd);
return 0;
}
更多内容
Linux 驱动开发