当前位置: 首页 > news >正文

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

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

• 分配内存空间函数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 30002.定义结构体:
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;


http://www.mrgr.cn/news/27162.html

相关文章:

  • Java 语法基础
  • 重拾java-------day1(基础计算机背景)
  • 基于SpringBoot+Vue的考务报名平台(带1w+文档)
  • 带你0到1之QT编程:十、一举击破开发中常用的Button按钮组
  • 【网络安全 | 代码审计】JFinal之DenyAccessJsp绕过
  • 代码随想录打卡Day32
  • 跨平台开发新视角:利用Android WebView实现Web内容的原生体验
  • 【时时三省】(C语言基础)指针进阶 例题7
  • 在线音乐系统网站源码
  • 《深入理解 C++移动语义与右值引用:性能提升与潜在陷阱》
  • 汽车租赁系统1.0版本
  • 【四】k8s部署 TDengine集群
  • TCP套接字【网络】
  • 2-94 基于matlab的最佳维纳滤波器的盲解卷积算法
  • 《论网络安全体系设计》写作框架,软考高级系统架构设计师
  • xmake vscode+clangd实现c/c++程序更精确跳转、补全
  • 小琳AI课堂:o1系列模型
  • Python 中的高并发 I/O
  • 什么是幂等
  • Coggle数据科学 | 科大讯飞AI大赛:人岗匹配挑战赛 赛季3