v4l2架构专题模块handler分析 -- handler ctrl的注册2

本文解析Linux v4l2架构中ctrlclass的作用,重点讲解V4L2_CID_LINK_FREQ的注册与find_ref_lock函数,以及ImageProcessingControls类别下的link_frequency控件。

Linux v4l2架构学习总链接

上一篇文章中忽略了ctrl class,这里补上

static int handler_new_ref(struct v4l2_ctrl_handler *hdl,
			   struct v4l2_ctrl *ctrl)
{
	struct v4l2_ctrl_ref *ref;
	struct v4l2_ctrl_ref *new_ref;
	u32 id = ctrl->id;
	u32 class_ctrl = V4L2_CTRL_ID2WHICH(id) | 1;
	...

	/*
	 * Automatically add the control class if it is not yet present and
	 * the new control is not a compound control.
	 */
	if (ctrl->type < V4L2_CTRL_COMPOUND_TYPES &&
	    id != class_ctrl && find_ref_lock(hdl, class_ctrl) == NULL)
		if (!v4l2_ctrl_new_std(hdl, NULL, class_ctrl, 0, 0, 0, 0))
			return hdl->error;
            ...
        }
}

对于id为V4L2_CID_LINK_FREQ

#define V4L2_CID_IMAGE_PROC_CLASS		(V4L2_CTRL_CLASS_IMAGE_PROC | 1)
#define V4L2_CTRL_CLASS_IMAGE_PROC	0x009f0000	/* Image processing controls */
#define V4L2_CID_IMAGE_PROC_CLASS_BASE		(V4L2_CTRL_CLASS_IMAGE_PROC | 0x900)
#define V4L2_CID_LINK_FREQ			(V4L2_CID_IMAGE_PROC_CLASS_BASE + 1)

其ctrl class值为 V4L2_CID_IMAGE_PROC_CLASS

这里看一下代码中的 find_ref_lock函数

static struct v4l2_ctrl_ref *find_ref_lock(
		struct v4l2_ctrl_handler *hdl, u32 id)
{
	struct v4l2_ctrl_ref *ref = NULL;

	if (hdl) {
		mutex_lock(hdl->lock);
		ref = find_ref(hdl, id);
		mutex_unlock(hdl->lock);
	}
	return ref;
}

static struct v4l2_ctrl_ref *find_ref(struct v4l2_ctrl_handler *hdl, u32 id)
{
	struct v4l2_ctrl_ref *ref;
	int bucket;

	id &= V4L2_CTRL_ID_MASK;

	/* Old-style private controls need special handling */
	if (id >= V4L2_CID_PRIVATE_BASE)
		return find_private_ref(hdl, id);

        /*
         * 根据id判断所在的bucket编号
         */
	bucket = id % hdl->nr_of_buckets;

	/* Simple optimization: cache the last control found */


        /*
         * hdl->cached保存上一次查找到的ref
         * 如果本次查找和上次保存到一样,这里可以直接返回
         * 节省时间
         */

	if (hdl->cached && hdl->cached->ctrl->id == id)
		return hdl->cached;

	/* Not in cache, search the hash */
        
        /*
         * cache中没有找到,现在找hash
         * 根据bucket编号,找到buckets
         */

	ref = hdl->buckets ? hdl->buckets[bucket] : NULL;

        /*
         * 通过ref->next依次遍历当前buckets中的ref
         */
	while (ref && ref->ctrl->id != id)
		ref = ref->next;

        /*
         * 成功找到ref
         * 将这个ref保存到cached
         */
	if (ref)
		hdl->cached = ref; /* cache it! */
	return ref;
}

对于我们的分析来说,这里是不会找到ref,因为现在还没有ctrl class

对于ctl class来说,依然是通过v4l2_ctl_new_std这个函数来注册,这里就不分析了

使用v4l2-ctrl可以看到具体的ctrl

[root@RV1126_RV1109:/]# v4l2-ctl -d /dev/v4l-subdev3 -l

User Controls

                       exposure 0x00980911 (int)    : min=2 max=647 step=1 default=284 value=284
                horizontal_flip 0x00980914 (bool)   : default=0 value=0
                  vertical_flip 0x00980915 (bool)   : default=0 value=0

Image Source Controls

              vertical_blanking 0x009e0901 (int)    : min=459 max=65343 step=1 default=459 value=459
            horizontal_blanking 0x009e0902 (int)    : min=30 max=30 step=1 default=30 value=30 flags=read-only
                  analogue_gain 0x009e0903 (int)    : min=16 max=3964 step=1 default=16 value=16

Image Processing Controls

                 link_frequency 0x009f0901 (intmenu): min=0 max=0 default=0 value=0
                     pixel_rate 0x009f0902 (int64)  : min=0 max=4410525 step=1 default=4410525 value=4410525 flags=read-only
                   test_pattern 0x009f0903 (menu)   : min=0 max=4 default=0 value=0

可以看到Image Process Controls类下面有link_frequency

 

 

 

### v4l2_ctrl_handler_init 函数的使用说明 `v4l2_ctrl_handler_init` 是 V4L2 子系统中的一个重要函数,用于初始化控制处理器 (control handler)。该函数的主要作用是分配并初始化一个 `v4l2_ctrl_handler` 对象,以便后续能够管理一组控件 (controls)[^1]。 #### 参数说明 `v4l2_ctrl_handler_init` 的原型如下: ```c int v4l2_ctrl_handler_init(struct v4l2_ctrl_handler *hdl, unsigned nr_of_controls); ``` - **参数 1 (`hdl`)**: 指向要初始化的 `v4l2_ctrl_handler` 结构体指针。 - **参数 2 (`nr_of_controls`)**: 预估的最大控件数量。这会影响内部内存分配大小,因此应尽可能接近实际需求的数量[^2]。 返回值为负数时表示错误码;成功时返回零。 --- #### 功能与用途 通过调用 `v4l2_ctrl_handler_init`,可以完成以下操作: 1. 分配必要的资源来存储控件及其关联的数据。 2. 设置初始状态以支持动态注册新的控件。 3. 提供基础框架,允许驱动程序实现自定义功能或扩展标准行为。 一旦完成了初始化过程,则可以通过一系列 API 来进一步配置和填充具体的控件实例,比如利用 `v4l2_ctrl_new_std()` 或者类似的变种方法添加标准化或者私有类型的选项[^3]。 需要注意的是,在释放不再使用的对象之前应当执行清理工作,即调用相应的销毁函数如 `v4l2_ctrl_handler_free(hdl)` ,从而避免潜在的泄漏风险以及保持系统的稳定性[^4]。 --- #### 示例代码 下面展示了一个典型的例子,演示如何正确地运用上述提到的技术要点: ```c #include <media/v4l2-ctrls.h> static int my_v4l2_control_setup(struct v4l2_device *v4l2_dev) { struct v4l2_ctrl_handler ctrl_hdl; int ret; /* Step A: Initialize the control handler */ memset(&ctrl_hdl, 0, sizeof(ctrl_hdl)); ret = v4l2_ctrl_handler_init(&ctrl_hdl, 10); // Estimate up to 10 controls. if (ret < 0) { pr_err("Failed to initialize control handler\n"); return ret; } /* Step B: Add standard or custom controls into this handler */ v4l2_ctrl_new_std(&ctrl_hdl, &my_ctrl_ops, // Operations defined elsewhere V4L2_CID_BRIGHTNESS,// Standard ID for brightness adjustment 0, // Minimum value allowed by hardware/software limits 255, // Maximum value accordingly set here as well 1, // Step size between each valid setting point 128 // Default initial level when no user input given yet ); /* Optional step C: Attach handlers onto main device structure */ v4l2_dev->ctrl_handler = &ctrl_hdl; /* Error handling during setup phase */ if (ctrl_hdl.error){ v4l2_ctrl_handler_free(&ctrl_hdl); return ctrl_hdl.error ; } return 0; } ``` 在此片段里可以看到完整的流程涵盖了从创建到绑定整个链条上的各个环节,并且还包含了基本异常捕获机制确保稳健运行环境下的兼容性和可靠性。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

dianlong_lee

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值