四、 V4L2 驅動框架
上述流程的各個操作都需要有底層 V4L2 驅動的支持。內核中有一些非常完善的例子。
比如: linux-2.6.26 內核目錄 /drivers/media/video//zc301/zc301_core.c 中的 ZC301 視頻驅動代碼。上面的 V4L2 操作流程涉及的功能在其中都有實現。
1 、 V4L2 驅動註冊、註銷函數
Video 核心層( drivers/media/video/videodev.c )提供了註冊函數
int video_register_device(struct video_device *vfd, int type, int nr)
video_device: 要構建的核心數據結構
Type: 表示設備類型,此設備號的基地址受此變量的影響
Nr: 如果 end-base>nr>0 :次設備號 =base (基準值,受 type 影響) +nr ;
否則:系統自動分配合適的次設備號
具體驅動只需要構建 video_device 結構,然後調用註冊函數既可。
如: zc301_core.c 中的
err = video_register_device(cam->v4ldev, VFL_TYPE_GRABBER,
video_nr[dev_nr]);
Video 核心層( drivers/media/video/videodev.c )提供了註銷函數
void video_unregister_device(struct video_device *vfd)
2 、 struct video_device 的構建
video_device 結構包含了視頻設備的屬性和操作方法。參見 zc301_core.c
strcpy(cam->v4ldev->name, "ZC0301[P] PC Camera");
cam->v4ldev->owner = THIS_MODULE;
cam->v4ldev->type = VID_TYPE_CAPTURE | VID_TYPE_SCALES;
cam->v4ldev->fops = &zc0301_fops;
cam->v4ldev->minor = video_nr[dev_nr];
cam->v4ldev->release = video_device_release;
video_set_drvdata(cam->v4ldev, cam);
大家發現在這個 zc301 的驅動中並沒有實現 struct video_device 中的很多操作函數 , 如 : vidioc_querycap 、vidioc_g_fmt_cap 等。主要原因是 struct file_operations zc0301_fops 中的 zc0301_ioctl 實現了前面的所有 ioctl 操作。所以就不需要在 struct video_device 再實現 struct video_device 中的那些操作了。
另一種實現方法如下:
static struct video_device camif_dev =
{
.name = "s3c2440 camif",
.type = VID_TYPE_CAPTURE|VID_TYPE_SCALES|VID_TYPE_SUBCAPTURE,
.fops = &camif_fops,
.minor = -1,
.release = camif_dev_release,
.vidioc_querycap = vidioc_querycap,
.vidioc_enum_fmt_cap = vidioc_enum_fmt_cap,
.vidioc_g_fmt_cap = vidioc_g_fmt_cap,
.vidioc_s_fmt_cap = vidioc_s_fmt_cap,
.vidioc_queryctrl = vidioc_queryctrl,
.vidioc_g_ctrl = vidioc_g_ctrl,
.vidioc_s_ctrl = vidioc_s_ctrl,
};
static struct file_operations camif_fops =
{
.owner = THIS_MODULE,
.open = camif_open,
.release = camif_release,
.read = camif_read,
.poll = camif_poll,
.ioctl = video_ioctl2, /* V4L2 ioctl handler */
.mmap = camif_mmap,
.llseek = no_llseek,
};
注意 : video_ioctl2 是 videodev.c 中是實現的。 video_ioctl2 中會根據 ioctl 不同的 cmd 來
調用 video_device 中的操作方法。