1. 程式人生 > >linux v4l2入門(轉)

linux v4l2入門(轉)

3.2 函式繫結

其中v4l2_file_operations和v4l2_ioctl_ops是必須實現的。而v4l2_subdev_ops下的八類ops中,v4l2_subdev_core_ops是必須實現的,其餘需要根據裝置型別選擇實現的。比如video capture類裝置需要實現v4l2_subdev_core_ops, v4l2_subdev_video_ops。

  • v4l2_file_operations:實現檔案類操作,比如open,close,read,write,mmap等。但是ioctl是不需要實現的,一般都是用video_ioctl2代替。例如linux/drivers/media/video/soc_camera.c檔案中soc_camera_fops的實現:
static struct v4l2_file_operations soc_camera_fops = {
    .owner          = THIS_MODULE,
    .open           = soc_camera_open,
    .release        = soc_camera_close,
    .unlocked_ioctl = video_ioctl2,
    .read           = soc_camera_read,
    .mmap           = soc_camera_mmap,
    .poll           =
soc_camera_poll, };
  • v4l2_ioctl_ops:V4L2匯出給應用層使用的所有ioctl都是在這個地方實現的。但不必全部實現,只實現自己相關的ioctl即可。例如linux/drivers/media/video/soc_camera.c中soc_camera_ioctl_ops的實現:
static const struct v4l2_ioctl_ops soc_camera_ioctl_ops = {
    .vidioc_querycap         = soc_camera_querycap,
    .vidioc_try_fmt_vid_cap  = soc_camera_try_fmt_vid_cap
, .vidioc_g_fmt_vid_cap = soc_camera_g_fmt_vid_cap, .vidioc_s_fmt_vid_cap = soc_camera_s_fmt_vid_cap, .vidioc_enum_fmt_vid_cap = soc_camera_enum_fmt_vid_cap, .vidioc_enum_input = soc_camera_enum_input, .vidioc_g_input = soc_camera_g_input, .vidioc_s_input = soc_camera_s_input, .vidioc_s_std = soc_camera_s_std, .vidioc_g_std = soc_camera_g_std, .vidioc_enum_framesizes = soc_camera_enum_fsizes, .vidioc_reqbufs = soc_camera_reqbufs, .vidioc_querybuf = soc_camera_querybuf, .vidioc_qbuf = soc_camera_qbuf, .vidioc_dqbuf = soc_camera_dqbuf, .vidioc_create_bufs = soc_camera_create_bufs, .vidioc_prepare_buf = soc_camera_prepare_buf, .vidioc_streamon = soc_camera_streamon, .vidioc_streamoff = soc_camera_streamoff, .vidioc_cropcap = soc_camera_cropcap, .vidioc_g_crop = soc_camera_g_crop, .vidioc_s_crop = soc_camera_s_crop, .vidioc_g_parm = soc_camera_g_parm, .vidioc_s_parm = soc_camera_s_parm, .vidioc_g_chip_ident = soc_camera_g_chip_ident, #ifdef CONFIG_VIDEO_ADV_DEBUG .vidioc_g_register = soc_camera_g_register, .vidioc_s_register = soc_camera_s_register, #endif };
  • v4l2_subdev_ops:v4l2_subdev有可能需要實現的ops的總合。分為8類,core,audio,video,vbi,tuner......等。例如,

    linuxdriversmediavideosoc_camera_platform.c中platform_subdev_ops的實現

static struct v4l2_subdev_video_ops platform_subdev_video_ops = {
    .s_stream       = soc_camera_platform_s_stream,
    .enum_mbus_fmt  = soc_camera_platform_enum_fmt,
    .cropcap        = soc_camera_platform_cropcap,
    .g_crop         = soc_camera_platform_g_crop,
    .try_mbus_fmt   = soc_camera_platform_fill_fmt,
    .g_mbus_fmt     = soc_camera_platform_fill_fmt,
    .s_mbus_fmt     = soc_camera_platform_fill_fmt,
    .g_mbus_config  = soc_camera_platform_g_mbus_config,
};

static struct v4l2_subdev_ops platform_subdev_ops = {
    .core   = &platform_subdev_core_ops,
    .video  = &platform_subdev_video_ops,
};

函式繫結只是將驅動所實現的函式賦值給相關的變數即可。