驱动开发-字符设备驱动的注册与注销

1.注册字符设备驱动

#include<fs.h>

int register_chrdev(unsigned int major,const char *name,const struct file_operations *fops)

函数功能:注册字符设备驱动

参数:major:主设备号

             major>0:静态指定主设备号,不可以和linux@ubuntu:~$ cat /proc/devices 主设备号重复

             major=0:动态指定设备号,操作系统随机分配

        name:字符设备驱动名字

        fops:操作方法结构体

        返回值:

                major>0:成功返回0,失败返回错误码

                major=0:成功返回主设备号,失败返回错误码

2.注销字符设备驱动

void unregister_chrdev(unsigned int major,const char *name)

函数功能:注销字符设备驱动

参数:

        major:主设备号的值

        name:字符设备驱动名字

驱动代码:

#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>

#define CNAME "myled"
unsigned int major;

int myled_open(struct inode *inode,struct file *file)
{
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    return 0;
}

ssize_t myled_read(struct file *file,char __user *ubuf,size_t size,loff_t *loff)
{
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    return 0;
}

ssize_t myled_write(struct file *file,const char __user *ubuf,size_t size,loff_t *loff)
{
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    return 0;
}

int myled_close(struct inode *inode,struct file *file)
{
    printk("%s:%s:%d\n",__FILE__,__func__,__LINE__);
    return 0;
}

//操作方法结构体
const struct file_operations fop = 
{
    .open = myled_open,
    .write = myled_write,
    .read = myled_read,
    .release = myled_close,
};

//入口 insmod
static int __init demo_init(void)
{
    //注册字符设备驱动
    major = register_chrdev(0,CNAME,&fop);
    if(major < 0)
    {
        printk("register chrdev is error\n");
        return -EIO;
    }
    printk("major = %d\n",major);//打印主设备号的值
    return 0;
}

//出口 rmmod
static void __exit demo_exit(void)
{
    //注销字符设备驱动
    unregister_chrdev(major,CNAME);
    
}

module_init(demo_init);//指定入口函数地址
module_exit(demo_exit);//指定出口函数地址
MODULE_LICENSE("GPL");//许可证

应用层代码:

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

int main(int argc,const char * argv[])
{
    int fd = -1;
    char buf[128] = "";
    fd = open("/dev/myled",O_RDWR);//打开设备文件
    if(fd == -1)
    {
        perror("open is error");
        exit(1);
    }
    write(fd,buf,sizeof(buf));//写入数据
    read(fd,buf,sizeof(buf));//读取数据
    close(fd);//关闭文件描述符
    return 0;
}

Makefile:

#指定arm架构内核路径
#KERNELDIR := /home/ubuntu/linux-5.10.61

#指定x86架构内核路径
KERNELDIR := /lib/modules/$(shell uname -r)/build

#表示开启一个终端,执行PWD命令
PWD := $(shell pwd)

all:
	@#-C:表示跳到内核路径,读取当前目录下的Makefile文件
	@#M:表示跳回当前目录下,执行当前目录下的Makefile文件
	make -C $(KERNELDIR) M=$(PWD) modules
clean:
	make -C $(KERNELDIR) M=$(PWD) clean
obj-m :=demo.o

测试:

  • 7
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值