V4L2 -- Subdev

2.7 V4L2 sub-devices 许多 drivers 需要和 sub-devices 交流,这些子设备可以处理 audio, video muxing, encoding, decoding 等等。 对于 webcams, 常见的 sub-devices 有 sensors, camera controllers. 通过 v4l2_subdev_init(sd, &ops) 来初始化 v4l2_subdev. 接着需要设置 sd->name. 如果需要和 media framework 聚合,那么需要初始化 media_entity 成员,如果 entity 有 pads 还需要调用media_entity_pads_init. struct v4l2_subdev { #if defined(CONFIG_MEDIA_CONTROLLER) struct media_entity entity; #endif struct list_head list; struct module *owner; bool owner_v4l2_dev; u32 flags; struct v4l2_device *v4l2_dev; const struct v4l2_subdev_ops *ops; const struct v4l2_subdev_internal_ops *internal_ops; struct v4l2_ctrl_handler *ctrl_handler; char name[52]; u32 grp_id; void *dev_priv; void *host_priv; struct video_device *devnode; struct device *dev; struct fwnode_handle *fwnode; struct list_head async_list; struct list_head async_subdev_endpoint_list; struct v4l2_async_notifier *subdev_notifier; struct list_head asc_list; struct v4l2_subdev_platform_data *pdata; struct mutex *state_lock; struct led_classdev *privacy_led; struct v4l2_subdev_state *active_state; u64 enabled_streams; }; owner_v4l2_dev: 如果和 v4l2_dev->dev 的 owner 一致,则为 true....

2025-06-19 · 7 min

V4L2 -- File Handler

2.6 V4L2 File handlers struct v4l2_fh 提供了一种方式,使得 file 方便地处理 V4L2 中的一些 specific data. 初始化:v4l2_fh_init(), 必须在 driver 的 v4l2_file_operations->open() 回调中调用, 会置起 video_device 的 V4L2_FL_USES_V4L2_FH flag. 在 userspace open device node 后,调用到 v4l2_fh_open, 会把 file->private_data 设置为 fh. 许多情况下 v4l2_fh 都会嵌入在更大的结构体中,这时需要调用 v4l2_fh_init() 和 v4l2_fh_add 在.open() 回调 v4l2_fh_del() 和 v4l2_fh_exit() 在.release() 回调 struct v4l2_fh { struct list_head list; struct video_device *vdev; struct v4l2_ctrl_handler *ctrl_handler; enum v4l2_priority prio; wait_queue_head_t wait; struct mutex subscribe_lock; struct list_head subscribed; struct list_head available; unsigned int navailable; u32 sequence; struct v4l2_m2m_ctx *m2m_ctx; }; list: 链接到 video_device 的 fh_list....

2025-06-19 · 1 min

V4L2 -- V4L2 Device

2.5 V4L2 device v4l2 用来抽象最顶层的 v4l2 设备,包含一系列子设备。 通过v4l2_device_register(dev, v4l2_dev)注册。 还需要设置好 v4l2->mdev 为 media_device 结构体。 在调用 v4l2_device_register 之前如果没有设置 v4l2_device->name,那么会自动分配。 还可以提供一个 notify 回调,给 subdev 向 v4l2_device 发送 event,events 定义在v4l2-subdev.h,官方文档里写只能使用 v4l2-subdev.h 中定义的 event,但看到有的 driver 自定义了一些 event。 通过v4l2_device_unregister()注销,如果是 hotpluggable 设备,在此之前还需要调用v4l2_device_disconnect() v4l2-device.h: Data Structure and APIs struct v4l2_device { struct device *dev; struct media_device *mdev; struct list_head subdevs; spinlock_t lock; char name[36]; void (*notify)(struct v4l2_subdev *sd, unsigned int notification, void *arg); struct v4l2_ctrl_handler *ctrl_handler; struct v4l2_prio_state prio; struct kref ref; void (*release)(struct v4l2_device *v4l2_dev); }; dev: 底层 device 设备。...

2025-06-19 · 2 min

V4L2 -- Video Device

Introduction linux/samples/v4l/v4l2-pci-skeleton.c 有一个 driver 模板。 2.2 Structure of a V4L driver device instances | +-sub-device instances | \-V4L2 device nodes | \-filehandle instances 2.3 Structure of the V4L2 framework v4l2 framework 中分别使用以下几个结构体来依次代表上面的结构: struct v4l2_device struct v4l2_subdev struct video_device struct v4l2_fh 2.4 Video device video device 用于抽象系统注册的 v4l2 /dev 设备节点,以便用户空间可以进行交互。 可以通过 video_device_alloc() 分配,也可以嵌入在更大的结构体中,这样则需要自定义 release 函数。 如果是嵌入在更大的结构体中,并且没有要释放的资源,可以使用 video_device_release_empty()。 最后通过 video_register_device() 注册。 struct video_device 其中一些 fields 需要我们手动去初始化,包括: fops, device_caps, queue, ioctl_ops, lock, prio....

2025-06-19 · 4 min