Linux字符设备通用模板
- Makefile
#!/bin/bash
obj-m += char_demo.o
PWD := $(shell pwd)
KDIR ?= /home/topeet/Desktop/ubootkernel2018/iTop4412_Kernel_3.0
all:
make -C $(KDIR) M=$(PWD) modules
.PHONY:clean
clean:
-rm -rf *.o *.mod.* *.order *.symvers
distclean:
-rm -rf *.ko *.o *.mod.
2.char_demo
/*
* 功能:字符设备Demo例程
*
*/
/* 包含头文件 */
#include <linux/init.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
#include <linux/stat.h>
#include <linux/fs.h>
#include <linux/kdev_t.h>
#include <linux/cdev.h>
#include <linux/slab.h>
#include <linux/device.h>
/* 作者和证书声明 */
MODULE_AUTHOR("zengxiangfeng");
MODULE_LICENSE("GPL");
/* 定义字符设备和驱动的名字 */
#define DEVICE_NAME "char_demo"
#define DRIVER_NAME "char_demo"
/* 定义设备号和设备数及大小 */
#define DEVICE_MAJOR 0
#define DRIVER_MINOR 0
#define DEVICE_NUM 2
#define DEVICE_SIZE 3000
/* 申明主设备号的次设备号 */
int major_num = DEVICE_MAJOR;
int minor_num = DRIVER_MINOR;
/* 定义模块参数 */
module_param(major_num, int , 0664);
module_param(minor_num, int, 0664);
/* 定义一个类 */
static struct class *myclass;
/* 设备结构体 */
struct reg_dev
{
char *data; // 设备数据
unsigned long size; //设备占用空间大小
struct cdev cdev; //设备结构体
};
struct reg_dev *my_devices;
/*打开操作*/
static int char_demo_open(struct inode *inode, struct file *file){
printk(KERN_EMERG "chardevnode_open is success!\n");
return 0;
}
/*关闭操作*/
static int char_demo_close(struct inode *inode, struct file *file){
printk(KERN_EMERG "chardevnode_release is success!\n");
return 0;
}
/*IO操作*/
static long char_demo_ioctl(struct file *file, unsigned int cmd, unsigned long arg){
printk(KERN_EMERG "chardevnode_ioctl is success! cmd is %d,arg is %ld \n",cmd,arg);
return 0;
}
ssize_t char_demo_read(struct file *file, char __user *buf, size_t count, loff_t *f_ops){
return 0;
}
ssize_t char_demo_write(struct file *file, const char __user *buf, size_t count, loff_t *f_ops){
return 0;
}
loff_t char_demo_llseek(struct file *file, loff_t offset, int ence){
return 0;
}
struct file_operations char_demo_ops = {
.owner = THIS_MODULE,
.open = char_demo_open,
.release = char_demo_close,
.unlocked_ioctl = char_demo_ioctl,
.read = char_demo_read,
.write = char_demo_write,
.llseek = char_demo_llseek,
};
/* 初始化模块 */
static int char_demo_init(void)
{
int ret = 0, i; //返回结果和for变量
dev_t num_dev; // 设备号
/* 提示信息 */
printk(KERN_INFO"char_demo_exit\n");
printk(KERN_INFO" major is %d \n",major_num);
printk(KERN_INFO"minor is %d \n",minor_num);
/* 如果主设备号不为零,静态注册设备,否则动态注册 */
if(major_num){
num_dev = MKDEV(major_num, minor_num);
ret = register_chrdev_region(major_num,DEVICE_NUM, DEVICE_NAME);
}
else
{
ret = alloc_chrdev_region(&num_dev, minor_num, DEVICE_NUM, DEVICE_NAME);
major_num = MAJOR(num_dev); //得到主设备号
minor_num = MINOR(num_dev); //得到次设备号
printk(KERN_INFO"alloc_char_demo , the major is %d ,the minor is %d \n",major_num,minor_num);
}// end if(major_num)
/* 判断是否注册成功,成功返回0 ,失败返回-1 */
if(ret < 0){
printk(KERN_EMERG"char_demo failed\n");
}
/* 创建一个类 */
myclass = class_create(THIS_MODULE, DEVICE_NAME);
/* 申请设备空间 */
my_devices = kmalloc(sizeof(struct reg_dev) * DEVICE_NUM, GFP_KERNEL);
if(!my_devices){
ret = -ENOMEM;
goto fail;
}
memset(my_devices, 0, DEVICE_NUM * sizeof(struct reg_dev)); //清空
/* 对设备依次初始化 */
for( i = 0 ; i < DEVICE_NUM ; i++){
my_devices[i].data = kmalloc(DEVICE_SIZE, GFP_KERNEL);
memset(my_devices[i].data, 0, sizeof(char) * DEVICE_SIZE);
/* 设备注册到系统 */
cdev_init(&my_devices[i].cdev, &char_demo_ops);
my_devices[i].cdev.owner = THIS_MODULE;
my_devices[i].cdev.ops = &char_demo_ops;
ret = cdev_add(&my_devices[i].cdev, MKDEV(major_num, minor_num +i), 1);
if(ret){
printk(KERN_EMERG"char_demo %d is fail ! %d\n",i,ret);
}else
{
printk(KERN_EMERG"char_demo add %d is success !\n",minor_num + i);
}
/* 创建设备节点 */
device_create(myclass, NULL, MKDEV(major_num, minor_num +i), NULL, DEVICE_NAME"%d",i);
}
printk(KERN_EMERG"char_demo init ok !\n");
return 0;
fail:
/* 注销设备号 */
unregister_chrdev_region(MKDEV(major_num,minor_num),DEVICE_NUM);
printk(KERN_EMERG "kmalloc is fail!\n");
return ret;
}
/* 退出模块 */
static void char_demo_exit(void)
{
int i;
/* 卸载字符设备 */
for(i = 0 ; i < DEVICE_NUM ; i++)
{
cdev_del(&(my_devices[i].cdev));
device_destroy(myclass, MKDEV(major_num, minor_num +i));
}
/* 卸载设备类 */
class_destroy(myclass);
/* 释放申请内存 */
kfree(my_devices);
/* 释放设备号 */
unregister_chrdev_region(MKDEV(major_num, minor_num), DEVICE_NUM);
}
module_init(char_demo_init);
module_exit(char_demo_exit);
测试程序
/*
* 功能:通用字符设备的测试程序
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
int main(int argc,char **argv)
{
int fd;
char *char_demo_node0 = "/dev/char_demo0";
char *char_demo_node1 = "/dev/char_demo1";
fd = open(char_demo_node0,O_RDWR|O_NONBLOCK);
if(fd < 0)
{
printf("%s open fail!!!\n",char_demo_node0);
exit(-1);
}
else
{
printf("%s open success !!!\n",char_demo_node0);
}
close(fd);
fd = open(char_demo_node1,O_RDWR|O_NONBLOCK);
if(fd < 0)
{
printf("%s open fail!!!\n",char_demo_node1);
exit(-1);
}
else
{
printf("%s open success !!!\n",char_demo_node1);
}
close(fd);
return 0;
}