字符设备驱动程序

重要变量

字符设备驱动程序_#include

重要函数

字符设备注册函数

int register_chrdev(unsigned int major,const char *name,const struct file_operations *fops);
参数1: 主设备号,一般填0,由内核自动分配。(主设备号范围:1-254)
参数2: 设备驱动名,注册成功可以使用cat /proc/device查看
参数3: 填充好的file_operations 结构体变量地址
返回值:分配好的主设备号(如果参数1=0,返回值是分配好的主设备号,如果参数1=指定号,返回值=0说明成功,<0说明失败)
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
字符设备注销函数
void unregister_chrdev(unsigned int major,const char name)
  • 1.
  • 2.

简单实现注册一个字符设备驱动:

字符设备驱动程序_linux_02

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/fs.h>
int demo_open(struct inode *pinode,struct file *pfile){
    printk(KERN_WARNING "demo_open is running\n");
    return 0;
}
int demo_close(struct inode *pinode,struct file *pfile){
    printk(KERN_WARNING "demo_close is running\n");
    return 0;
}
struct file_operations fops={
    .open=demo_open,
    .release=demo_close,
};
/*模块加载函数*/
int  __init  demo_module_init (void)  {
    printk(KERN_WARNING "demo_module_init is running\n");
    
    //注册驱动
    register_chrdev(243,"demo_driver",&fops);
    return 0;
}
/*模块卸载函数*/
void  __exit demo_module_exit (void)  { 

    printk("<4>" "demo_module_exit is running\n");
    //注销驱动
    unregister_chrdev(243,"demo_driver");
    
}
/*声明模块加载函数宏*/
module_init(demo_module_init);
/*声明模块卸载函数宏*/
module_exit(demo_module_exit);

/*声明模块作者*/
MODULE_AUTHOR("qf.net");
/*模块许可证明,描述内核模块的许可权限*/
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.

字符设备驱动程序_#include_03

命令创建字符设备:

字符设备驱动程序_linux_04

测试:

01_app.c

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
int main(){

    int fd=open("/dev/demo_dev",O_RDWR);

    sleep(5);

    close(fd);
    return 0;
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.

虚拟机编译:

字符设备驱动程序_字符设备_05

开发板执行:

字符设备驱动程序_字符设备_06

程序创建字符设备:

函数:

struct class *my_class;
my_class=class_create(THIS_MODULE, "demo_class");
class_destroy(my_class);

struct device *my_device;
my_device=device_create(my_class,  NULL,
 MKDEV(243,166),  NULL, "demo_dev_1");
device_destroy(my_class,MKDEV(243,166));
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.

案例:

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/device.h>
struct class *my_class;
struct device *my_device;
int demo_open(struct inode *pinode,struct file *pfile){
    printk(KERN_WARNING "demo_open is running\n");
    return 0;
}
int demo_close(struct inode *pinode,struct file *pfile){
    printk(KERN_WARNING "demo_close is running\n");
    return 0;
}
struct file_operations fops={
    .open=demo_open,
    .release=demo_close,
};
/*模块加载函数*/
int  __init  demo_module_init (void)  {
    printk(KERN_WARNING "demo_module_init is running\n");
    
    //注册驱动
    register_chrdev(243,"demo_driver",&fops);
    
    //创建设备类
    my_class=class_create(THIS_MODULE, "demo_class");
    //创建设备文件
    my_device=device_create(my_class,  NULL,MKDEV(243,166),  NULL, "demo_dev_1");
    return 0;
}
/*模块卸载函数*/
void  __exit demo_module_exit (void)  { 

    printk("<4>" "demo_module_exit is running\n");
    //删除设备文件
    device_destroy(my_class,MKDEV(243,166));
    //删除设备类
    class_destroy(my_class);
    //注销驱动
    unregister_chrdev(243,"demo_driver");        
}
/*声明模块加载函数宏*/
module_init(demo_module_init);
/*声明模块卸载函数宏*/
module_exit(demo_module_exit);

/*声明模块作者*/
MODULE_AUTHOR("qf.net");
/*模块许可证明,描述内核模块的许可权限*/
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.

测试:

字符设备驱动程序_linux_07