系列文章
高通msm-V4L2-Camera驱动浅析1-初识
高通msm-V4L2-Camera驱动浅析2-框架详解
高通msm-V4L2-Camera驱动浅析3-session
高通msm-V4L2-Camera驱动浅析4-stream
高通msm-V4L2-Camera驱动浅析5-buffer
前言
一些推荐的文章
深入理解Android相机体系结构之八
msm8996平台的 camera 框架笔记
Camera Kernel 驱动层代码逻辑分析
上一篇文章结合V4L2框架的知识,对高通v4l2 camera驱动进行简单分析。
本文聊聊qcom的camera内核框架设计和一些相关概念。
一、Camera内核驱动框架
msm框架把Camera系统分为 msm-config 和 msm-sensor 两部分:
- msm-config:用于创建v4l2_device,用于管理所有的子设备,同时生成/dev/video0设备节点,暴露控制接口给用户空间使用,主要包括了session/stream的创建与销毁,event事件的分发,另外,还创建了media_device,用于暴露枚举接口给用户来轮询查找整个内核的子设备。
相关源码:kernel/msm-4.9/drivers/media/platform/msm/camera_v2/msm.c
- msm-sensor:创建一个v412_device,用于管理sensor,比如sensor 的初始化,启动流,关闭流,图像buf的管理等,同时创建video1/video2设备节点暴露接口给用户空间进行访问。
相关源码:kernel/msm-4.9/drivers/media/platform/msm/camera_v2/camera/camera.c
ps:我当前的机器有前摄和后摄,因此就有2个节点:video1,video2。
框架源码路径:kernel/msm-4.9/drivers/media/platform/msm/camera_v2/
camera/ fd/ ispif/ jpeg_dma/ Makefile msm.c msm_sd.h pproc/
common/ isp/ jpeg_10/ Kconfig msm_buf_mgr/ msm.h msm_vb2/ sensor/
我们主要关注以下模块:
- msm.c: 所有qcom camera相关设备的管理,提供session、stream、event、vb2_queue等接口的创建和删除
- camera/:用于控制sensor,提供了ioctl_ops,如启动流,qbuf和dqbuf,subscribe_event等
- sensor/:具体的硬件驱动,例如sensor、eeprom、actuator、ois等
其他的模块,如fd-人脸识别相关,jpeg编码相关等,未来有机会再分析!
二、msm.c
kernel/msm-4.9/drivers/media/platform/msm/camera_v2/msm.c
2.1 开机时加载msm模块
static int __init msm_init(void)
{
return platform_driver_register(&msm_driver);
}
static void __exit msm_exit(void)
{
platform_driver_unregister(&msm_driver);
}
module_init(msm_init);
module_exit(msm_exit);
MODULE_DESCRIPTION("MSM V4L2 Camera");
MODULE_LICENSE("GPL v2");
开机时,会加载msm模块,内核匹配设备和驱动,当设备和驱动匹配时,就会调用msm_probe函数。
2.2 msm_probe
static int msm_probe(struct platform_device *pdev)
{
···省略部分源码
//初始化media_device
media_device_init(msm_v4l2_dev->mdev);
strlcpy(msm_v4l2_dev->mdev->model, "msm-config",sizeof(msm_v4l2_dev->mdev->model));
//注册一个媒体设备,其名称(model)为:msm-config
rc = media_device_register(msm_v4l2_dev->mdev);
//注册一个v4l2_device实例
rc = v4l2_device_register(&(pdev->dev), pvdev->vdev->v4l2_dev);
//绑定fops
pvdev->vdev->fops = &msm_fops;
//绑定ioctl_ops
pvdev->vdev->ioctl_ops = &g_msm_ioctl_ops;
//v4l2_device实例名称为"msm-config"
strlcpy(pvdev->vdev->name, "msm-config", sizeof(pvdev->vdev->name));
//注册一个视频设备
rc = video_register_device(pvdev->vdev,VFL_TYPE_GRABBER, -1);
//初始化一个会话队列
msm_init_queue(msm_session_q);
}
-
注册了一个媒体设备,创建/dev/media0设备节点,这里我们注册的媒体设备名称(model)为:msm_config。
上层可以通过IOCTL指令:MEDIA_IOC_ENUM_ENTITIES,枚举所有的media设备,然后根据设备名称msm_config找到的/dev/video0节点。
注册了v4l2_device实例,该结构体里面有一个链表:struct list_head subdevs,所有的子设备都会添加都该subdevs链表里面,方便维护。
-
注册一个视频设备,创建/dev/video0设备节点,给用户空间使用,其本质上是一个字符设备,提供fops和ioctl_fops给上层使用。
初始化一个session队列,所有的session都添加到这个队列中,方便管理维护。
2.3 提供fops给用户空间使用
static struct v4l2_file_operations msm_fops = {
.owner = THIS_MODULE,
.open = msm_open,
.poll = msm_poll,
.release = msm_close,
.unlocked_ioctl = video_ioctl2,
#ifdef CONFIG_COMPAT
.compat_ioctl32 = video_ioctl2,
#endif
};
static const struct v4l2_ioctl_ops g_msm_ioctl_ops = {
.vidioc_subscribe_event = msm_subscribe_event,
.vidioc_unsubscribe_event = msm_unsubscribe_event,
.vidioc_default = msm_private_ioctl,
};
v4l2_file_operations:
- msm_open:创建event队列
- msm_poll:轮询event队列
- msm_close:释放相关资源
v4l2_ioctl_ops:
- msm_subscribe_event:订阅事件
- msm_unsubscribe_event:注销事件
2.4 msm_open
在msm_probe中,注册了一个媒体设备/dev/media0,
上层可以通过IOCTL指令:MEDIA_IOC_ENUM_ENTITIES,枚举所有的media设备,
然后根据设备名称msm_config找到的/dev/video0节点。
具体逻辑代码如下:
vendor/qcom/proprietary/mm-camera/mm-camera2/media-controller/mct_shim_layer/mct_shim_layer.c
int mct_shimlayer_process_module_init(mm_camera_shim_ops_t
*shim_ops_tbl)
{
//该函数通过MEDIA_IOC_ENUM_ENTITIES指令枚举所有媒体设备,
//然后通过媒体设备名称msm_config找到/dev/video0节点
get_config_node_name(config_node_name);
snprintf(dev_name, sizeof(dev_name), "/dev/%s", config_node_name);
//用户空间调用了open方法,dev_name = "/dev/video0"
config_fd = open(dev_name, O_RDWR | O_NONBLOCK);
}
用户空间调用open(dev_name, O_RDWR | O_NONBLOCK)打开设备节点/dev/video0,通过VFS文件系统,最终会调用到msm_open函数。
static int msm_open(struct file *filep)
{
int rc = -1;
/* create event queue */
rc = v4l2_fh_open(filep);
<=========================>
int v4l2_fh_open(struct file *filp)
{
struct video_device *vdev = video_devdata(filp);
struct v4l2_fh *fh = kzalloc(sizeof(*fh), GFP_KERNEL);
filp->private_data = fh;
if (fh == NULL)
return -ENOMEM;
v4l2_fh_init(fh, vdev);//初始化v4l2_fh
v4l2_fh_add(fh);//添加v4l2_fh到video_device
return 0;
}
<=========================>
/* register msm_v4l2_pm_qos_request */
msm_pm_qos_add_request();
return rc;
}
msm_open中,主要完成了v4l2_fh的初始化工作,后续所有的event都由v4l2_fh管理。
v4l2_fh结构体是用来描述event队列,通常在打开设备节点时进行v4l2_fh注册。
v4l2_fh_init(fh, vdev):初始化v4l2_fh,添加v4l2_ctrl_handler到v4l2_fh:fh->ctrl_handler = vdev->ctrl_handler;
v4l2_fh_add(fh):添加v4l2_fh到video_device,方便核心层调用到。
关于v4l2_fh,可以参考以下文章:
linux v4l2学习之-消息机制
2.5 流的管理
- msm_create_stream:创建流
- msm_delete_stream:删除流
- msm_get_stream :获取流
- __msm_queue_find_stream:查找流
当然还有其他流的操作,就不全部一一列出来了。
三、camera.c分析
kernel/msm-4.9/drivers/media/platform/msm/camera_v2/camera/camera.c
3.1 sensor_probe
vendor/qcom/proprietary/mm-camera/mm-camera2/media-controller/modules/sensors/module/sensor_init.c
static boolean sensor_probe(···)
{
struct sensor_init_cfg_data cfg;
/* Pass slave information to kernel and probe */
memset(&cfg, 0, sizeof(cfg));
cfg.cfgtype = CFG_SINIT_PROBE;
cfg.cfg.setting = slave_info;
if (ioctl(fd, VIDIOC_MSM_SENSOR_INIT_CFG, &cfg) < 0) {
SINFO("[%s]CFG_SINIT_PROBE failed",sensor_name);
ret = FALSE;
goto ERROR;
}
}
- 通过IOCTL命令:VIDIOC_MSM_SENSOR_INIT_CFG,调用到msm_sensor_driver_cmd(s_init, arg),
- 通过CFG_SINIT_PROBE调用到msm_sensor_driver_probe(···);
- msm_sensor_driver_probe函数会调用camera_init_v4l2进行初始化
3.2 camera_init_v4l2
int camera_init_v4l2(struct device *dev, unsigned int *session)
{
struct msm_video_device *pvdev;
struct v4l2_device *v4l2_dev;
int rc = 0;
···省略部分源码
#if defined(CONFIG_MEDIA_CONTROLLER)
//初始化media_device
media_device_init(v4l2_dev->mdev);
strlcpy(v4l2_dev->mdev->model, "msm_camera",
sizeof(v4l2_dev->mdev->model));
v4l2_dev->mdev->dev = dev;
//注册一个媒体设备,其名称(model)为:msm_camera
rc = media_device_register(v4l2_dev->mdev);
rc = media_entity_pads_init(&pvdev->vdev->entity, 0, NULL);
#endif
pvdev->vdev->v4l2_dev = v4l2_dev;
//注册一个v4l2_device实例
rc = v4l2_device_register(dev, pvdev->vdev->v4l2_dev);
strlcpy(pvdev->vdev->name, "msm-sensor", sizeof(pvdev->vdev->name));
//绑定fops
pvdev->vdev->fops = &camera_v4l2_fops;
//绑定ioctl_ops
pvdev->vdev->ioctl_ops = &camera_v4l2_ioctl_ops;
//设置视频设备的能力
pvdev->vdev->device_caps = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING;
//注册一个视频设备,名称为:msm-sensor
rc = video_register_device(pvdev->vdev,VFL_TYPE_GRABBER, -1);
return rc;
}
-
注册一个媒体设备,生成节点/dev/media1,其名称(model)为:msm-camera
注册一个v4l2_device实例
绑定fops和ioctl_ops
-
注册一个视频设备,生成节点/dev/video1,其名称为:msm-sensor
3.3 提供fops给用户空间使用
static struct v4l2_file_operations camera_v4l2_fops = {
.owner = THIS_MODULE,
.open = camera_v4l2_open,
.poll = camera_v4l2_poll,
.release = camera_v4l2_close,
.unlocked_ioctl = video_ioctl2,
#ifdef CONFIG_COMPAT
.compat_ioctl32 = camera_v4l2_compat_ioctl,
#endif
};
static const struct v4l2_ioctl_ops camera_v4l2_ioctl_ops = {
.vidioc_querycap = camera_v4l2_querycap,
.vidioc_s_crop = camera_v4l2_s_crop,
.vidioc_g_crop = camera_v4l2_g_crop,
.vidioc_queryctrl = camera_v4l2_queryctrl,
.vidioc_g_ctrl = camera_v4l2_g_ctrl,
.vidioc_s_ctrl = camera_v4l2_s_ctrl,
.vidioc_reqbufs = camera_v4l2_reqbufs,
.vidioc_querybuf = camera_v4l2_querybuf,
.vidioc_qbuf = camera_v4l2_qbuf,
.vidioc_dqbuf = camera_v4l2_dqbuf,
.vidioc_streamon = camera_v4l2_streamon,
.vidioc_streamoff = camera_v4l2_streamoff,
.vidioc_g_fmt_vid_cap_mplane = camera_v4l2_g_fmt_vid_cap_mplane,
.vidioc_s_fmt_vid_cap_mplane = camera_v4l2_s_fmt_vid_cap_mplane,
.vidioc_try_fmt_vid_cap_mplane = camera_v4l2_try_fmt_vid_cap_mplane,
/* Stream type-dependent parameter ioctls */
.vidioc_g_parm = camera_v4l2_g_parm,
.vidioc_s_parm = camera_v4l2_s_parm,
/* event subscribe/unsubscribe */
.vidioc_subscribe_event = camera_v4l2_subscribe_event,
.vidioc_unsubscribe_event = camera_v4l2_unsubscribe_event,
.vidioc_default = camera_v4l2_vidioc_private_ioctl,
};
3.4 会话的创建
应用会调用my_obj->ctrl_fd = open(dev_name, O_RDWR | O_NONBLOCK);
通过VFS文件系统,最终会调用到camera_v4l2_open。
static int camera_v4l2_open(struct file *filep)
{
...省略部分源码
//v4l2_fh的初始化工作,后续所有的event都由v4l2_fh管理
camera_v4l2_fh_open(filep);
/* every stream has a vb2 queue */
//初始化vbuffer2_queue
rc = camera_v4l2_vb2_q_init(filep)
if (!atomic_read(&pvdev->opened)) {
/* create a new session when first opened */
//创建一个会话,添加到会话队列中
rc = msm_create_session(pvdev->vdev->num, pvdev->vdev);
//创建一个cmd队列
rc = msm_create_command_ack_q(pvdev->vdev->num,
find_first_zero_bit((const unsigned long *)&opn_idx,
MSM_CAMERA_STREAM_CNT_BITS));
}
}
- 初始化v4l2_fh,所有的event都由v4l2_fh管理
- 始化vbuffer2_queue
- 创建一个会话,添加到会话队列中
- 创建一个cmd队列
3.5 stream的创建
static int camera_v4l2_s_parm(struct file *filep, void *fh,
struct v4l2_streamparm *parm)
{
int rc = 0;
struct v4l2_event event;
···省略部分源码
rc = msm_create_stream(event_data->session_id,event_data->stream_id, &sp->vb2_q);
return rc;
}
Stay Hungry,Stay Foolish!