Linux v4l2架构学习总链接
video相关注册看这篇文章
基于RV1126平台imx291分析 --- video rkcif_mipi注册
这里不会分析调用过程,只给出一个最终函数rkcif_start_streaming
这里提一下vb2_core_streamon这个函数
int vb2_core_streamon(struct vb2_queue *q, unsigned int type)
{
...
if (q->queued_count >= q->min_buffers_needed) {
/*
* vivi中这里有media相关的,所以没有分析
* 这里分析一下
* 这里最终会调用mdev->enable_source
* 我们没有,所以还是不用分析
*/
ret = v4l_vb2q_enable_media_source(q);
if (ret)
return ret;
ret = vb2_start_streaming(q);
if (ret)
return ret;
}
...
}
rkcif_start_streaming()
static int rkcif_start_streaming(struct vb2_queue *queue, unsigned int count)
{
struct rkcif_stream *stream = queue->drv_priv;
struct rkcif_vdev_node *node = &stream->vnode;
struct rkcif_device *dev = stream->cifdev;
struct v4l2_device *v4l2_dev = &dev->v4l2_dev;
struct rkcif_sensor_info *sensor_info = dev->active_sensor;
struct rkcif_sensor_info *terminal_sensor = &dev->terminal_sensor;
struct rkmodule_hdr_cfg hdr_cfg;
int ret;
mutex_lock(&dev->stream_lock);
if (WARN_ON(stream->state != RKCIF_STATE_READY)) {
ret = -EBUSY;
v4l2_err(v4l2_dev, "stream in busy state\n");
goto destroy_buf;
}
/*
* 这里挺奇怪既然active_sensor有值,为什么还要再去检测一遍???
* rkcif_update_sensor_info 之前已经分析过了
* 基于RV1126平台imx291分析 --- open及media graph分析
* https://blog.csdn.net/ldl617/article/details/115862796
*/
if (dev->active_sensor) {
ret = rkcif_update_sensor_info(stream);
if (ret < 0) {
v4l2_err(v4l2_dev,
"update sensor info failed %d\n",
ret);
goto out;
}
}
/*
* 这里的terminal_sensor->sd 对应的就是imx291的subdev
*/
if (terminal_sensor->sd) {
/*
* 调用imx291的core->ioctl
*/
ret = v4l2_subdev_call(terminal_sensor->sd,
core, ioctl,
RKMODULE_GET_HDR_CFG,
&hdr_cfg);
if (!ret)
dev->hdr.mode = hdr_cfg.hdr_mode;
else
dev->hdr.mode = NO_HDR;
/*
* 调用imx291的video->g_frame_interval
*/
ret = v4l2_subdev_call(terminal_sensor->sd,
video, g_frame_interval, &terminal_sensor->fi);
if (ret)
terminal_sensor->fi.interval = (struct v4l2_fract) {1, 30};
rkcif_sync_crop_info(stream);
}
...
/*
* 基于RV1126平台imx291分析 --- rkcif_mipi注册
* https://blog.csdn.net/ldl617/article/details/115551981
* 上面的文章中有pipe相关的信息
* cif_dev->pipe.open = rkcif_pipeline_open;
* cif_dev->pipe.close = rkcif_pipeline_close;
* cif_dev->pipe.set_stream = rkcif_pipeline_set_stream;
* 分析一下rkcif_pipeline_open
*/
ret = dev->pipe.open(&dev->pipe, &node->vdev.entity, true);
if (ret < 0) {
v4l2_err(v4l2_dev, "open cif pipeline failed %d\n",
ret);
goto destroy_buf;
}
...
}
rkcif_start_streaming() -> rkcif_pipeline_open()
static int __cif_pipeline_prepare(struct rkcif_pipeline *p,
struct media_entity *me)
{
struct v4l2_subdev *sd;
int i;
p->num_subdevs = 0;
memset(p->subdevs, 0, sizeof(p->subdevs));
while (1) {
struct media_pad *pad = NULL;
/* Find remote source pad */
for (i = 0; i < me->num_pads; i++) {
struct media_pad *spad = &me->pads[i];
/*
* 注意这里只找sink pad
*/
if (!(spad->flags & MEDIA_PAD_FL_SINK))
continue;
/*
* media_entity_remote_pad 分析过了
* 现在这里只有backlink才符合
* 所以这里的pad依次是
* 1. mipi csi source pad
* 2. mipi csi phy source pad
* 3. imx291 source pad
*/
pad = media_entity_remote_pad(spad);
if (pad)
break;
}
if (!pad)
break;
/*
* sd依次为
* 1. mipi csi subdev
* 2. mipi csi phy subdev
* 3. imx291 subdev
* 保存到p->subdevs中
*/
sd = media_entity_to_v4l2_subdev(pad->entity);
p->subdevs[p->num_subdevs++] = sd;
me = &sd->entity;
/*
* 对于imx291 只有一个source pad
* 所以这里直接退出
*/
if (me->num_pads == 1)
break;
}
return 0;
}
static int rkcif_pipeline_open(struct rkcif_pipeline *p,
struct media_entity *me,
bool prepare)
{
int ret;
if (WARN_ON(!p || !me))
return -EINVAL;
if (atomic_inc_return(&p->power_cnt) > 1)
return 0;
/* go through media graphic and get subdevs */
if (prepare)
/*
* 分析看上面
*/
__cif_pipeline_prepare(p, me);
if (!p->num_subdevs)
return -EINVAL;
/*
* 空函数,return 0
*/
ret = __cif_pipeline_s_cif_clk(p);
if (ret < 0)
return ret;
return 0;
}
回到 rkcif_start_streaming()继续分析
ret = media_pipeline_start(&node->vdev.entity, &dev->pipe.pipe);
if (ret < 0) {
v4l2_err(&dev->v4l2_dev, "start pipeline failed %d\n",
ret);
goto pipe_stream_off;
}
rkcif_start_streaming() -> media_pipeline_start()
__must_check int __media_pipeline_start(struct media_entity *entity,
struct media_pipeline *pipe)
{
struct media_device *mdev = entity->graph_obj.mdev;
struct media_graph *graph = &pipe->graph;
struct media_entity *entity_err = entity;
struct media_link *link;
int ret;
/*
* 首次会进行初始化的操作
* 注意这里初始化的是pipe->graph
* 之前已经分析过2个graph了
* 1. 局部变量graph 用于update_info
* 2. mdev->pm_count_walk 用于pipeline_pm_power
*/
if (!pipe->streaming_count++) {
ret = media_graph_walk_init(&pipe->graph, mdev);
if (ret)
goto error_graph_walk_start;
}
/*
* 这里的entity是rkcif_mipi的entity
*/
media_graph_walk_start(&pipe->graph, entity);
while ((entity = media_graph_walk_next(graph))) {
/*
* #define DECLARE_BITMAP(name,bits) \
* unsigned long name[BITS_TO_LONGS(bits)]
* 注意下面的active和has_no_link都是局部变量
* 每次while时候都会重新定义
* 对于这里的entity看下面文章分析
* 基于RV1126平台imx291分析 --- v4l2_pipeline_pm_use
* https://blog.csdn.net/ldl617/article/details/115898236
* 可以知道依次出现的顺序是
* 1. m01_f_imx291 1-001a
* 2. rockchip-mipi-dphy-rx
* 3. stream_cif_mipi_id1
* 4. stream_cif_mipi_id2
* 5. stream_cif_mipi_id3
* 6. rockchip-mipi-csi2
*/
DECLARE_BITMAP(active, MEDIA_ENTITY_MAX_PADS);
DECLARE_BITMAP(has_no_links, MEDIA_ENTITY_MAX_PADS);
/*
* 更新stream流的计数
*/
entity->stream_count++;
if (WARN_ON(entity->pipe && entity->pipe != pipe)) {
ret = -EBUSY;
goto error;
}
/*
* 赋值pipe
*/
entity->pipe = pipe;
/* Already streaming --- no need to check. */
if (entity->stream_count > 1)
continue;
/*
* 调用link_validate
* 当前的环境没有link_validate
* 所以下面的代码都执行不到
*/
if (!entity->ops || !entity->ops->link_validate)
continue;
bitmap_zero(active, entity->num_pads);
bitmap_fill(has_no_links, entity->num_pads);
list_for_each_entry(link, &entity->links, list) {
struct media_pad *pad = link->sink->entity == entity
? link->sink : link->source;
/* Mark that a pad is connected by a link. */
bitmap_clear(has_no_links, pad->index, 1);
/*
* Pads that either do not need to connect or
* are connected through an enabled link are
* fine.
*/
if (!(pad->flags & MEDIA_PAD_FL_MUST_CONNECT) ||
link->flags & MEDIA_LNK_FL_ENABLED)
bitmap_set(active, pad->index, 1);
/*
* Link validation will only take place for
* sink ends of the link that are enabled.
*/
if (link->sink != pad ||
!(link->flags & MEDIA_LNK_FL_ENABLED))
continue;
ret = entity->ops->link_validate(link);
if (ret < 0 && ret != -ENOIOCTLCMD) {
dev_dbg(entity->graph_obj.mdev->dev,
"link validation failed for '%s':%u -> '%s':%u, error %d\n",
link->source->entity->name,
link->source->index,
entity->name, link->sink->index, ret);
goto error;
}
}
/* Either no links or validated links are fine. */
bitmap_or(active, active, has_no_links, entity->num_pads);
if (!bitmap_full(active, entity->num_pads)) {
ret = -ENOLINK;
dev_dbg(entity->graph_obj.mdev->dev,
"'%s':%u must be connected by an enabled link\n",
entity->name,
(unsigned)find_first_zero_bit(
active, entity->num_pads));
goto error;
}
}
return 0;
}
__must_check int media_pipeline_start(struct media_entity *entity,
struct media_pipeline *pipe)
{
struct media_device *mdev = entity->graph_obj.mdev;
int ret;
mutex_lock(&mdev->graph_mutex);
ret = __media_pipeline_start(entity, pipe);
mutex_unlock(&mdev->graph_mutex);
return ret;
}
rkcif_start_streaming()中我们关注的代码已经分析完成了,可以说VIDIOC_STREAMON这部分就分析到这里了。
但是对于media_pipeline_start() 看到一知半解,现在看来作用就是entity->stream_count++
有些不甘心,这里找找这个平台lvds的一个link_validate看看
static int
v4l2_subdev_link_validate_get_format(struct media_pad *pad,
struct v4l2_subdev_format *fmt)
{
if (is_media_entity_v4l2_subdev(pad->entity)) {
struct v4l2_subdev *sd =
media_entity_to_v4l2_subdev(pad->entity);
fmt->which = V4L2_SUBDEV_FORMAT_ACTIVE;
fmt->pad = pad->index;
return v4l2_subdev_call(sd, pad, get_fmt, NULL, fmt);
}
WARN(pad->entity->function != MEDIA_ENT_F_IO_V4L,
"Driver bug! Wrong media entity type 0x%08x, entity %s\n",
pad->entity->function, pad->entity->name);
return -EINVAL;
}
int v4l2_subdev_link_validate(struct media_link *link)
{
struct v4l2_subdev *sink;
struct v4l2_subdev_format sink_fmt, source_fmt;
int rval;
/*
* 找到source entity的subdev,调用pad->get_format获取格式
*/
rval = v4l2_subdev_link_validate_get_format(
link->source, &source_fmt);
if (rval < 0)
return 0;
/*
* 找到sink entity的subdev,调用pad->get_format获取格式
*/
rval = v4l2_subdev_link_validate_get_format(
link->sink, &sink_fmt);
if (rval < 0)
return 0;
/* sink entity的subdev */
sink = media_entity_to_v4l2_subdev(link->sink->entity);
/*
* 没有找到合适的link_validate
* 所以认为没有
*/
rval = v4l2_subdev_call(sink, pad, link_validate, link,
&source_fmt, &sink_fmt);
if (rval != -ENOIOCTLCMD)
return rval;
/*
* source 和sink的format通过一定的规则进行对比
*/
return v4l2_subdev_link_validate_default(
sink, link, &source_fmt, &sink_fmt);
}
看起来这个平台的link_validate只是进行了sink和source的format对比
感觉还是想的简单了,全局搜索entity->stream_count++
找到了关键代码,之前我们其实分析过
基于RV1126平台imx291分析 --- media部件连接 二
csi2_notifier_bound() -> media_entity_setup_link()
int __media_entity_setup_link(struct media_link *link, u32 flags)
{
const u32 mask = MEDIA_LNK_FL_ENABLED;
struct media_device *mdev;
struct media_entity *source, *sink;
int ret = -EBUSY;
if (link == NULL)
return -EINVAL;
/* The non-modifiable link flags must not be modified. */
/*
* link->flags 这里是0
* flag = MEDIA_LNK_FL_ENABLE
* 所以if不满足
*/
if ((link->flags & ~mask) != (flags & ~mask))
return -EINVAL;
if (link->flags & MEDIA_LNK_FL_IMMUTABLE)
return link->flags == flags ? 0 : -EINVAL;
if (link->flags == flags)
return 0;
/*
* 分别找到source entity
* 和sink entity
*/
source = link->source->entity;
sink = link->sink->entity;
/*
* stream_count值大于0,可以认为启动了数据流传输
* 这里没有,所以为0
*/
if (!(link->flags & MEDIA_LNK_FL_DYNAMIC) &&
(source->stream_count || sink->stream_count))
return -EBUSY;
...
}
有判断source->stream_count和sink->stream_count
所以这里可不可以这么认为,就是当前的entity已经在运行了,因为一些原因或者操作,这个entity参与到了别的链路创建
这里加上判断是不允许的,当然前提是当前运行的这个link不支持dynamic动态的。