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;
相关推荐
一战成名996几秒前
深度解析 CANN 模型转换工具链:从 ONNX 到 OM
人工智能·学习·安全·开源
蒸蒸yyyyzwd20 分钟前
分布式算法学习笔记1.1-1.4
笔记·学习
匆匆那年96739 分钟前
llamafactory推理消除模型的随机性
linux·服务器·学习·ubuntu
好好学习天天向上~~44 分钟前
5_Linux学习总结_vim
linux·学习·vim
笨笨阿库娅44 分钟前
从零开始的算法基础学习
学习·算法
阿蒙Amon9 小时前
TypeScript学习-第10章:模块与命名空间
学习·ubuntu·typescript
AI绘画哇哒哒9 小时前
【干货收藏】深度解析AI Agent框架:设计原理+主流选型+项目实操,一站式学习指南
人工智能·学习·ai·程序员·大模型·产品经理·转行
戌中横10 小时前
JavaScript——预解析
前端·javascript·学习
●VON11 小时前
React Native for OpenHarmony:2048 小游戏的开发与跨平台适配实践
javascript·学习·react native·react.js·von
ZH154558913111 小时前
Flutter for OpenHarmony Python学习助手实战:自动化脚本开发的实现
python·学习·flutter