ARM驱动学习之9注册字符类设备

ARM驱动学习之9注册字符类设备

cpp 复制代码
• 分配内存空间函数kmalloc
-- 分配连续的虚拟地址,用于小内存分配。在include/linux/slab.h文件中。
-- 参数1:申请的内存大小(最大128K),
-- 参数2:GFP_KERNEL,代表优先权,内存不够可以延迟分配

• 分配内存空间函数kmalloc
-- 分配连续的虚拟地址,用于小内存分配。在include/linux/slab.h文件中。
-- 参数1:申请的内存大小(最大128K),
-- 参数2:GFP_KERNEL,代表优先权,内存不够可以延迟分配

• 字符设备初始化函数cdev_init
-- 在头文件include/linux/cdev.h中
-- 参数1:cdev字符设备文件结构体
-- 参数2:file_operations结构体
-- 注册设备本质是向linux设备文件中添加数据,这些数据需要初始化

/**
 * cdev_init() - initialize a cdev structure
 * @cdev: the structure to initialize
 * @fops: the file_operations for this device
 *
 * Initializes @cdev, remembering @fops, making it ready to add to the
 * system with cdev_add().
 */
void cdev_init(struct cdev *cdev, const struct file_operations *fops)
{
	memset(cdev, 0, sizeof *cdev);
	INIT_LIST_HEAD(&cdev->list);
	kobject_init(&cdev->kobj, &ktype_cdev_default);
	cdev->ops = fops;
}

• 字符设备注册函数cdev_add
-- 在头文件include/linux/cdev.h中
-- 参数1:cdev字符设备文件结构体
-- 参数2:设备号
-- 参数3:设备范围大小
-- 向系统注册设备,也就是向linux系统添加数据

/**
 * cdev_add() - add a char device to the system
 * @p: the cdev structure for the device
 * @dev: the first device number for which this device is responsible
 * @count: the number of consecutive minor numbers corresponding to this
 *         device
 *
 * cdev_add() adds the device represented by @p to the system, making it
 * live immediately.  A negative error code is returned on failure.
 */
int cdev_add(struct cdev *p, dev_t dev, unsigned count)
{
	p->dev = dev;
	p->count = count;
	return kobj_map(cdev_map, dev, count, NULL, exact_match, exact_lock, p);
}

static void cdev_unmap(dev_t dev, unsigned count)
{
	kobj_unmap(cdev_map, dev, count);
}

• 卸载设备函数cdev_del
-- 参数1:cdev结构体
-- 移除字符设备
/**
 * cdev_del() - remove a cdev from the system
 * @p: the cdev structure to be removed
 *
 * cdev_del() removes @p from the system, possibly freeing the structure
 * itself.
 */
void cdev_del(struct cdev *p)
{
	cdev_unmap(p->dev, p->count);
	kobject_put(&p->kobj);
}


1.文件名和Makefile修改为register_cdev
头文件:
/*三个字符设备函数*/
#include <linux/stat.h>
/*MKDEV转换设备号数据类型的宏定义*/
#include <linux/kdev_t.h>
/*定义字符设备的结构体*/
#include <linux/cdev.h>
/*分配内存空间函数头文件*/
#include <linux/slab.h>

#define REGDEV_SIZE 3000

2.定义结构体:
struct reg_deg
{
    char *data;
    unsigned long size;
    struct cdev cdev;
};
struct reg_dev *my_devices;

my_devices = kmalloc(DEVICE_MINOR_NUM * sizeof(struct reg_dev),GFP_KERNEL);
if(!my_devices){
    ret = -ENOMEM;
    goto fail;
}
memset(my_devices,0,DEVICE_MINOR_NUM * sizeof(struct reg_dev));//初始化缓存为0;

for(i = 0;i < DEVICE_MINOR_NUM;i ++){
    my_devices[i].data = kmalloc(REGDEV_SIZE,GFP_KERNEL);
    memset(&my_devices[i],0,REGDEV_SIZE);
}

fail:
    unregister_chrdev_region(MDKEV(numdev_major,numdev_minor),DEVICE_MINOR_NUM);
    printk(KERN_EMERG "kmalloc is fail\n");
    
    return ret;
相关推荐
西岸行者5 天前
学习笔记:SKILLS 能帮助更好的vibe coding
笔记·学习
悠哉悠哉愿意5 天前
【单片机学习笔记】串口、超声波、NE555的同时使用
笔记·单片机·学习
别催小唐敲代码5 天前
嵌入式学习路线
学习
毛小茛5 天前
计算机系统概论——校验码
学习
babe小鑫5 天前
大专经济信息管理专业学习数据分析的必要性
学习·数据挖掘·数据分析
winfreedoms5 天前
ROS2知识大白话
笔记·学习·ros2
在这habit之下5 天前
Linux Virtual Server(LVS)学习总结
linux·学习·lvs
我想我不够好。5 天前
2026.2.25监控学习
学习
im_AMBER5 天前
Leetcode 127 删除有序数组中的重复项 | 删除有序数组中的重复项 II
数据结构·学习·算法·leetcode
CodeJourney_J5 天前
从“Hello World“ 开始 C++
c语言·c++·学习