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 分钟前
图像分类里的小样本学习(Few-shot Image Classification)
学习·分类·数据挖掘
●VON13 分钟前
React Native for OpenHarmony:猜数字游戏完整技术实现文档
学习·react native·react.js·游戏·开源鸿蒙·von
松涛和鸣17 分钟前
DAY69 Practical Guide to Linux Character Device Drivers
linux·服务器·arm开发·数据库·单片机·嵌入式硬件
民乐团扒谱机20 分钟前
【微实验】Zhang-Suen 快速并行细化算法与MATLAB实现
人工智能·学习·算法·计算机视觉·数学建模·matlab
GHL28427109023 分钟前
TeamTalk-msg_server学习
运维·服务器·c++·学习
试试勇气31 分钟前
Linux学习笔记(十三)--动静态库
笔记·学习
好好学习天天向上~~36 分钟前
3_Linux学习总结_基础指令
linux·服务器·学习
AI视觉网奇38 分钟前
ue metahuman自动绑定实战
笔记·学习·ue5
tb_first43 分钟前
万字超详细苍穹外卖学习笔记3
java·jvm·笔记·学习·spring·tomcat·maven
coldstarry44 分钟前
sheng的学习笔记-AI-adaboost(Adaptive Boosting)
人工智能·笔记·学习