Linux驱动----设备文件自动生成

平台:树莓派4B
编译工具:ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu-

自动创建设备节点:利用udev(mdev)来实现设备文件的自动创建,首先应保证支持udev(mdev),由busybox配置。
步骤:在驱动初始化的代码里调用class_create(…)为该设备创建一个class,再为每个设备调用device_create(…)创建对应的设备。

驱动代码
#include <linux/module.h>   
#include <linux/init.h>  
#include <linux/fs.h>   
#include <linux/device.h>   
#include <linux/slab.h>  
#include <linux/cdev.h>  
#include <linux/err.h>  
#include <linux/mm_types.h>  
#include <asm/uaccess.h>  
#include <linux/io.h>  
#include <linux/platform_device.h>
#include <linux/kern_levels.h>
#include <linux/ioport.h>      
#include <linux/of_address.h>
#include <linux/of_irq.h>
#include <linux/interrupt.h>
#include <linux/device.h>
#include <linux/uaccess.h>


static struct class *test_class;
static struct device *test_class_dev;

static dev_t devno;
static int major = 231;
static int minor = 0;
static char *module_name = "test";

static ssize_t test_write(struct file* file, const char __user * buf, size_t size, loff_t* ppos)
{
    printk("test_write\n");
    return 0;
}

static ssize_t test_read(struct file* file, char __user * buf, size_t size, loff_t* ppos)
{
    printk("test_read\n");
    return 0;
}

static int test_open(struct inode* inode, struct file* filp)
{
    printk("test_open\n");//内核的打印函数
    return 0;
}

//在内核源码查找struct file_operations看结构体成员,添加用到的函数
static struct file_operations test_fops = {

    .owner = THIS_MODULE,
    .write = test_write,//函数指针
    .open  = test_open,
    .read  = test_read,
};

static int __init test_init(void)//驱动入口
{

    int ret;
	pr_info("test init");
    devno = MKDEV(major,minor);//创建设备号
    ret = register_chrdev(major,module_name,&test_fops);//注册驱动,把这个驱动加入到内核链表
    test_class = class_create(THIS_MODULE,"myfirstdemo");//代码自动生成设备
    test_class_dev = device_create(test_class,NULL,devno,NULL,module_name);//创建设备文件

    return 0;
}

static void __exit test_exit(void)
{
	pr_info("test exit");
    device_destroy(test_class,devno);//销毁设备
    class_destroy(test_class);//销毁类
    unregister_chrdev(major,module_name);//卸载设备
}

module_init(test_init);//入口,是个宏
module_exit(test_exit);
MODULE_LICENSE("GPL v2");
用户程序:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>

int main()
{

    int fd;

    fd = open("/dev/test",O_RDWR);
    if(fd < 0){
            printf("open failed\n");
    }else{
            printf("open success\n");
    }
    fd = write(fd,'1',1);
    close(fd);
    return 0;
}
编译

aarch64-linux-gnu-gcc test.c -o test

在/dev下生成设备节点:

/mnt # ls -l /dev/test
crw-rw----    1 root     0         231,   0 Jan  1 04:07 /dev/test

运行:./text
查看内核打印:dmesg

删除驱动

列出驱动模块:lsmod
删除驱动模块:rmmod 驱动模块名(lsmod出来的模块名)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Linux 系统中,驱动程序的链接通常是在编译时完成的,使用 Makefile 工具可以简化这个过程。假设您已经编写了一个驱动程序的源代码,并且已经生成了一个目标文件(例如,driver.o),您可以通过以下方式将其链接到 ldrm 库: 1. 创建一个 Makefile 文件,并在其中指定链接器命令。例如,以下是一个简单的 Makefile 文件示例: ``` obj-m := driver.o KDIR := /lib/modules/$(shell uname -r)/build PWD := $(shell pwd) all: $(MAKE) -C $(KDIR) M=$(PWD) modules clean: $(MAKE) -C $(KDIR) M=$(PWD) clean ``` 2. 在 Makefile 文件中,使用 obj-m 变量指定目标文件的名称(例如,driver.o)。 3. 在 Makefile 文件中,使用 KDIR 和 PWD 变量指定 Linux 内核源代码的位置和当前工作目录。 4. 在 Makefile 文件中,使用 all 和 clean 两个规则来编译和清除目标文件。 5. 在终端中,使用 make 命令来编译驱动程序。例如,运行以下命令: ``` make ``` 6. 在编译过程中,链接器命令自动将目标文件 driver.o 链接到 ldrm 库中。如果链接成功,则生成一个名为 driver.ko 的内核模块文件。 7. 将生成的内核模块文件加载到系统中。例如,运行以下命令: ``` insmod driver.ko ``` 8. 如果驱动程序需要卸载,可以使用以下命令: ``` rmmod driver ``` 请注意,上述步骤仅适用于 Linux 系统中的驱动程序链接。在其他操作系统或嵌入式系统中,链接过程可能有所不同。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值