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;
相关推荐
kitsch0x9711 小时前
技术学习_Cowrie 蜜罐的响应机制
学习
kyrie_sakura12 小时前
大模型学习32 --大模型微调3 -- 分布式训练算法
分布式·学习·算法
xwz小王子12 小时前
Nature Custom Media 从手术台到厨房:下一代医疗机器人,正在学习“什么时候该放手”
人工智能·学习·机器人
Rabitebla12 小时前
【Linux】 文件系统(五):挂载与软硬链接,多个文件系统怎样组成统一目录树
linux·运维·服务器·c++·笔记·学习·算法
叼烟扛炮17 小时前
[LangGraph] 学习笔记(047):Pydantic 写的 State 也不校验,并行分支还互相看不见
笔记·学习
qq_2744751518 小时前
第 16 篇:类型检测方案汇总:typeof、instanceof、Object.prototype.toString、手写 isType
javascript·学习·原型模式
Brain 博览记18 小时前
给吗啡类镇痛药装上“光开关”:照一下开、再照一下关——Cell 冷冻电镜解析阿片效价切换
开发语言·网络·论文阅读·学习·php
Answer1st19 小时前
【FPGA学习】VSCode 写 Verilog 操作指南(XII)
vscode·学习·fpga开发
Nerd(BUG版)19 小时前
KMP学习day15--Compose副作用与动画
学习·kotlin·kmp
Monkey的自我迭代19 小时前
Kubernetes 声明式理念:只写“我要什么”
java·学习·kubernetes