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

linux - 主次设备号自动申请

alloc_chrdev_region 原型如下,该函数向内核申请一个空闲的主设备号。

alloc_chrdev_region(&g_aputriger_dev, 0, APUTRIGER_MAX_NUM, "aputriger0");

第四个参数是我们使用cat /proc/devices 看到的名称

/*** alloc_chrdev_region() - register a range of char device numbers* @dev: output parameter for first assigned number* @baseminor: first of the requested range of minor numbers* @count: the number of minor numbers required* @name: the name of the associated device or driver** Allocates a range of char device numbers.  The major number will be* chosen dynamically, and returned (along with the first minor number)* in @dev.  Returns zero or a negative error code.*/
int alloc_chrdev_region(dev_t *dev, unsigned baseminor, unsigned count,const char *name)
{struct char_device_struct *cd;cd = __register_chrdev_region(0, baseminor, count, name);if (IS_ERR(cd))return PTR_ERR(cd);*dev = MKDEV(cd->major, cd->baseminor);return 0;
}

如果我们的设备不止一个,次设备号我们也希望是自动申请而不是人为指定的,那么可以使用ida_simple_get来申请,原型如下:

minor = ida_simple_get(&g_aputriger_dev, 0, APUTRIGER_MAX_NUM, GFP_KERNEL);

#define ida_simple_get(ida, start, end, gfp)	\ida_alloc_range(ida, start, (end) - 1, gfp)/*** ida_alloc_range() - Allocate an unused ID.* @ida: IDA handle.* @min: Lowest ID to allocate.* @max: Highest ID to allocate.* @gfp: Memory allocation flags.** Allocate an ID between @min and @max, inclusive.  The allocated ID will* not exceed %INT_MAX, even if @max is larger.** Context: Any context.* Return: The allocated ID, or %-ENOMEM if memory could not be allocated,* or %-ENOSPC if there are no free IDs.*/
int ida_alloc_range(struct ida *ida, unsigned int min, unsigned int max,gfp_t gfp)
{int id = 0;unsigned long flags;if ((int)min < 0)return -ENOSPC;if ((int)max < 0)max = INT_MAX;again:xa_lock_irqsave(&ida->ida_rt, flags);id = ida_get_new_above(ida, min);if (id > (int)max) {ida_remove(ida, id);id = -ENOSPC;}xa_unlock_irqrestore(&ida->ida_rt, flags);if (unlikely(id == -EAGAIN)) {if (!ida_pre_get(ida, gfp))return -ENOMEM;goto again;}return id;
}

创建设备示例:

	if (cdev_add(&cur_dev->cdev, MKDEV(g_aputriger_dev, minor), 1)) {pr_err("%s cdev add failed\n", cdev_name);goto err_class_destr;}/* devices create */cur_dev->dev = device_create(cur_dev->class, NULL, MKDEV(cur_dev->major, minor), NULL, cdev_name);

http://www.lryc.cn/news/342488.html

相关文章:

  • 我写了一套几乎无敌的参数校验组件!基于 SpEL 的参数校验组件「SpEL Validator」
  • 输入序列太长 gan CGAN
  • uni-app scroll-view隐藏滚动条的小细节 兼容主流浏览器
  • Java常用API之LinkedList类解读
  • 移动端自适应
  • 自动化运维工具-Ansible
  • 力扣:62. 不同路径
  • store内路由跳转router.push
  • ChatGPT Web Midjourney一键集成最新版
  • springboot mongodb分片集群事务
  • node报错——解决Error: error:0308010C:digital envelope routines::unsupported——亲测可用
  • golang系统内置函数整理
  • 武汉星起航:五对一服务体系,助力创业者成功进军跨境电商市场
  • C++常用库函数——strcmp、strchr
  • vue3怎么使用vant的IndexBar 索引栏
  • VMware常见问题(技巧)总结
  • VS Code 保存+格式化代码
  • word启动缓慢之Baidu Netdisk Word Addin
  • 获取波形极值与间距并显示
  • 视频素材哪个app好?8个视频素材库免费使用
  • 002 validation自定义校验器
  • SQL-Server数据库--视图
  • Flink 部署模式
  • 第十三节:Vben Admin实战-系统管理之菜单管理
  • 2024------MySQL数据库基础知识点总结
  • 机器学习之基于Jupyter中国环境治理投资数据分析及可视化
  • 【Word】写论文,参考文献涉及的上标、尾注、脚注 怎么用
  • 能将图片转为WebP格式的WebP Server Go
  • 省份数量00
  • Android Native内存泄漏检测方案详解