linux V4L2子系统——v4l2的结构体(4)之ioctl_v4l2 ioctl

  • :ref:VIDIOC_G_EXT_CTRLS <vidioc_g_ext_ctrls> ioctl

  • @vidioc_s_ext_ctrls: pointer to the function that implements

  • :ref:VIDIOC_S_EXT_CTRLS <vidioc_g_ext_ctrls> ioctl

  • @vidioc_try_ext_ctrls: pointer to the function that implements

  • :ref:VIDIOC_TRY_EXT_CTRLS <vidioc_g_ext_ctrls> ioctl

  • @vidioc_querymenu: pointer to the function that implements

  • :ref:VIDIOC_QUERYMENU <vidioc_queryctrl> ioctl

  • @vidioc_enumaudio: pointer to the function that implements

  • :ref:VIDIOC_ENUMAUDIO <vidioc_enumaudio> ioctl

  • @vidioc_g_audio: pointer to the function that implements

  • :ref:VIDIOC_G_AUDIO <vidioc_g_audio> ioctl

  • @vidioc_s_audio: pointer to the function that implements

  • :ref:VIDIOC_S_AUDIO <vidioc_g_audio> ioctl

  • @vidioc_enumaudout: pointer to the function that implements

  • :ref:VIDIOC_ENUMAUDOUT <vidioc_enumaudout> ioctl

  • @vidioc_g_audout: pointer to the function that implements

  • :ref:VIDIOC_G_AUDOUT <vidioc_g_audout> ioctl

  • @vidioc_s_audout: pointer to the function that implements

  • :ref:VIDIOC_S_AUDOUT <vidioc_g_audout> ioctl

  • @vidioc_g_modulator: pointer to the function that implements

  • :ref:VIDIOC_G_MODULATOR <vidioc_g_modulator> ioctl

  • @vidioc_s_modulator: pointer to the function that implements

  • :ref:VIDIOC_S_MODULATOR <vidioc_g_modulator> ioctl

  • @vidioc_cropcap: pointer to the function that implements

  • :ref:VIDIOC_CROPCAP <vidioc_cropcap> ioctl

  • @vidioc_g_crop: pointer to the function that implements

  • :ref:VIDIOC_G_CROP <vidioc_g_crop> ioctl

  • @vidioc_s_crop: pointer to the function that implements

  • :ref:VIDIOC_S_CROP <vidioc_g_crop> ioctl

  • @vidioc_g_selection: pointer to the function that implements

  • :ref:VIDIOC_G_SELECTION <vidioc_g_selection> ioctl

  • @vidioc_s_selection: pointer to the function that implements

  • :ref:VIDIOC_S_SELECTION <vidioc_g_selection> ioctl

  • @vidioc_g_jpegcomp: pointer to the function that implements

  • :ref:VIDIOC_G_JPEGCOMP <vidioc_g_jpegcomp> ioctl

  • @vidioc_s_jpegcomp: pointer to the function that implements

  • :ref:VIDIOC_S_JPEGCOMP <vidioc_g_jpegcomp> ioctl

  • @vidioc_g_enc_index: pointer to the function that implements

  • :ref:VIDIOC_G_ENC_INDEX <vidioc_g_enc_index> ioctl

  • @vidioc_encoder_cmd: pointer to the function that implements

  • :ref:VIDIOC_ENCODER_CMD <vidioc_encoder_cmd> ioctl

  • @vidioc_try_encoder_cmd: pointer to the function that implements

  • :ref:VIDIOC_TRY_ENCODER_CMD <vidioc_encoder_cmd> ioctl

  • @vidioc_decoder_cmd: pointer to the function that implements

  • :ref:VIDIOC_DECODER_CMD <vidioc_decoder_cmd> ioctl

  • @vidioc_try_decoder_cmd: pointer to the function that implements

  • :ref:VIDIOC_TRY_DECODER_CMD <vidioc_decoder_cmd> ioctl

  • @vidioc_g_parm: pointer to the function that implements

  • :ref:VIDIOC_G_PARM <vidioc_g_parm> ioctl

  • @vidioc_s_parm: pointer to the function that implements

  • :ref:VIDIOC_S_PARM <vidioc_g_parm> ioctl

  • @vidioc_g_tuner: pointer to the function that implements

  • :ref:VIDIOC_G_TUNER <vidioc_g_tuner> ioctl

  • @vidioc_s_tuner: pointer to the function that implements

  • :ref:VIDIOC_S_TUNER <vidioc_g_tuner> ioctl

  • @vidioc_g_frequency: pointer to the function that implements

  • :ref:VIDIOC_G_FREQUENCY <vidioc_g_frequency> ioctl

  • @vidioc_s_frequency: pointer to the function that implements

  • :ref:VIDIOC_S_FREQUENCY <vidioc_g_frequency> ioctl

  • @vidioc_enum_freq_bands: pointer to the function that implements

  • :ref:VIDIOC_ENUM_FREQ_BANDS <vidioc_enum_freq_bands> ioctl

  • @vidioc_g_sliced_vbi_cap: pointer to the function that implements

  • :ref:VIDIOC_G_SLICED_VBI_CAP <vidioc_g_sliced_vbi_cap> ioctl

  • @vidioc_log_status: pointer to the function that implements

  • :ref:VIDIOC_LOG_STATUS <vidioc_log_status> ioctl

  • @vidioc_s_hw_freq_seek: pointer to the function that implements

  • :ref:VIDIOC_S_HW_FREQ_SEEK <vidioc_s_hw_freq_seek> ioctl

  • @vidioc_g_register: pointer to the function that implements

  • :ref:VIDIOC_DBG_G_REGISTER <vidioc_dbg_g_register> ioctl

  • @vidioc_s_register: pointer to the function that implements

  • :ref:VIDIOC_DBG_S_REGISTER <vidioc_dbg_g_register> ioctl

  • @vidioc_g_chip_info: pointer to the function that implements

  • :ref:VIDIOC_DBG_G_CHIP_INFO <vidioc_dbg_g_chip_info> ioctl

  • @vidioc_enum_framesizes: pointer to the function that implements

  • :ref:VIDIOC_ENUM_FRAMESIZES <vidioc_enum_framesizes> ioctl

  • @vidioc_enum_frameintervals: pointer to the function that implements

  • :ref:VIDIOC_ENUM_FRAMEINTERVALS <vidioc_enum_frameintervals> ioctl

  • @vidioc_s_dv_timings: pointer to the function that implements

  • :ref:VIDIOC_S_DV_TIMINGS <vidioc_g_dv_timings> ioctl

  • @vidioc_g_dv_timings: pointer to the function that implements

  • :ref:VIDIOC_G_DV_TIMINGS <vidioc_g_dv_timings> ioctl

  • @vidioc_query_dv_timings: pointer to the function that implements

  • :ref:VIDIOC_QUERY_DV_TIMINGS <vidioc_query_dv_timings> ioctl

  • @vidioc_enum_dv_timings: pointer to the function that implements

  • :ref:VIDIOC_ENUM_DV_TIMINGS <vidioc_enum_dv_timings> ioctl

  • @vidioc_dv_timings_cap: pointer to the function that implements

  • :ref:VIDIOC_DV_TIMINGS_CAP <vidioc_dv_timings_cap> ioctl

  • @vidioc_g_edid: pointer to the function that implements

  • :ref:VIDIOC_G_EDID <vidioc_g_edid> ioctl

  • @vidioc_s_edid: pointer to the function that implements

  • :ref:VIDIOC_S_EDID <vidioc_g_edid> ioctl

  • @vidioc_subscribe_event: pointer to the function that implements

  • :ref:VIDIOC_SUBSCRIBE_EVENT <vidioc_subscribe_event> ioctl

  • @vidioc_unsubscribe_event: pointer to the function that implements

  • :ref:VIDIOC_UNSUBSCRIBE_EVENT <vidioc_unsubscribe_event> ioctl

  • @vidioc_default: pointed used to allow other ioctls
    /
    struct v4l2_ioctl_ops {
    /
    ioctl callbacks */

    /* VIDIOC_QUERYCAP handler */
    int (*vidioc_querycap)(struct file *file, void *fh,
    struct v4l2_capability *cap);

    /* VIDIOC_ENUM_FMT handlers */
    int (*vidioc_enum_fmt_vid_cap)(struct file *file, void *fh,
    struct v4l2_fmtdesc *f);
    int (*vidioc_enum_fmt_vid_overlay)(struct file *file, void *fh,
    struct v4l2_fmtdesc *f);
    int (*vidioc_enum_fmt_vid_out)(struct file *file, void *fh,
    struct v4l2_fmtdesc *f);
    int (*vidioc_enum_fmt_vid_cap_mplane)(struct file *file, void *fh,
    struct v4l2_fmtdesc *f);
    int (*vidioc_enum_fmt_vid_out_mplane)(struct file *file, void *fh,
    struct v4l2_fmtdesc *f);
    int (*vidioc_enum_fmt_sdr_cap)(struct file *file, void *fh,
    struct v4l2_fmtdesc *f);
    int (*vidioc_enum_fmt_sdr_out)(struct file *file, void *fh,
    struct v4l2_fmtdesc *f);
    int (*vidioc_enum_fmt_meta_cap)(struct file *file, void *fh,
    struct v4l2_fmtdesc *f);

    /* VIDIOC_G_FMT handlers */
    int (*vidioc_g_fmt_vid_cap)(struct file *file, void *fh,
    struct v4l2_format *f);
    int (*vidioc_g_fmt_vid_overlay)(struct file *file, void *fh,
    struct v4l2_format *f);
    int (*vidioc_g_fmt_vid_out)(struct file *file, void *fh,
    struct v4l2_format *f);
    int (*vidioc_g_fmt_vid_out_overlay)(struct file *file, void *fh,
    struct v4l2_format *f);
    int (*vidioc_g_fmt_vbi_cap)(struct file *file, void *fh,
    struct v4l2_format *f);
    int (*vidioc_g_fmt_vbi_out)(struct file *file, void *fh,
    struct v4l2_format *f);
    int (*vidioc_g_fmt_sliced_vbi_cap)(struct file *file, void *fh,
    struct v4l2_format *f);
    int (*vidioc_g_fmt_sliced_vbi_out)(struct file *file, void *fh,
    struct v4l2_format *f);
    int (*vidioc_g_fmt_vid_cap_mplane)(struct file *file, void *fh,
    struct v4l2_format *f);
    int (*vidioc_g_fmt_vid_out_mplane)(struct file *file, void *fh,
    struct v4l2_format *f);
    int (*vidioc_g_fmt_sdr_cap)(struct file *file, void *fh,
    struct v4l2_format *f);
    int (*vidioc_g_fmt_sdr_out)(struct file *file, void *fh,
    struct v4l2_format *f);
    int (*vidioc_g_fmt_meta_cap)(struct file *file, void *fh,
    struct v4l2_format *f);

    /* VIDIOC_S_FMT handlers */
    int (*vidioc_s_fmt_vid_cap)(struct file *file, void *fh,
    struct v4l2_format *f);
    int (*vidioc_s_fmt_vid_overlay)(struct file *file, void *fh,
    struct v4l2_format *f);
    int (*vidioc_s_fmt_vid_out)(struct file *file, void *fh,
    struct v4l2_format *f);
    int (*vidioc_s_fmt_vid_out_overlay)(struct file *file, void *fh,
    struct v4l2_format *f);
    int (*vidioc_s_fmt_vbi_cap)(struct file *file, void *fh,
    struct v4l2_format *f);
    int (*vidioc_s_fmt_vbi_out)(struct file *file, void *fh,
    struct v4l2_format *f);
    int (*vidioc_s_fmt_sliced_vbi_cap)(struct file *file, void *fh,
    struct v4l2_format *f);
    int (*vidioc_s_fmt_sliced_vbi_out)(struct file *file, void *fh,
    struct v4l2_format *f);
    int (*vidioc_s_fmt_vid_cap_mplane)(struct file *file, void *fh,
    struct v4l2_format *f);
    int (*vidioc_s_fmt_vid_out_mplane)(struct file *file, void *fh,
    struct v4l2_format *f);
    int (*vidioc_s_fmt_sdr_cap)(struct file *file, void *fh,
    struct v4l2_format *f);
    int (*vidioc_s_fmt_sdr_out)(struct file *file, void *fh,
    struct v4l2_format *f);
    int (*vidioc_s_fmt_meta_cap)(struct file *file, void *fh,
    struct v4l2_format *f);

    /* VIDIOC_TRY_FMT handlers */
    int (*vidioc_try_fmt_vid_cap)(struct file *file, void *fh,
    struct v4l2_format *f);
    int (*vidioc_try_fmt_vid_overlay)(struct file *file, void *fh,
    struct v4l2_format *f);
    int (*vidioc_try_fmt_vid_out)(struct file *file, void *fh,
    struct v4l2_format *f);
    int (*vidioc_try_fmt_vid_out_overlay)(struct file *file, void *fh,
    struct v4l2_format *f);
    int (*vidioc_try_fmt_vbi_cap)(struct file *file, void *fh,
    struct v4l2_format *f);
    int (*vidioc_try_fmt_vbi_out)(struct file *file, void *fh,
    struct v4l2_format *f);
    int (*vidioc_try_fmt_sliced_vbi_cap)(struct file *file, void *fh,
    struct v4l2_format *f);
    int (*vidioc_try_fmt_sliced_vbi_out)(struct file *file, void *fh,
    struct v4l2_format *f);
    int (*vidioc_try_fmt_vid_cap_mplane)(struct file *file, void *fh,
    struct v4l2_format *f);
    int (*vidioc_try_fmt_vid_out_mplane)(struct file *file, void *fh,
    struct v4l2_format *f);
    int (*vidioc_try_fmt_sdr_cap)(struct file *file, void *fh,
    struct v4l2_format *f);
    int (*vidioc_try_fmt_sdr_out)(struct file *file, void *fh,
    struct v4l2_format *f);
    int (*vidioc_try_fmt_meta_cap)(struct file *file, void *fh,
    struct v4l2_format *f);

    /* Buffer handlers */
    int (*vidioc_reqbufs)(struct file *file, void *fh,
    struct v4l2_requestbuffers *b);
    int (*vidioc_querybuf)(struct file *file, void *fh,
    struct v4l2_buffer *b);
    int (*vidioc_qbuf)(struct file *file, void *fh,
    struct v4l2_buffer *b);
    int (*vidioc_expbuf)(struct file *file, void *fh,
    struct v4l2_exportbuffer *e);
    int (*vidioc_dqbuf)(struct file *file, void *fh,
    struct v4l2_buffer *b);

    int (*vidioc_create_bufs)(struct file *file, void *fh,
    struct v4l2_create_buffers *b);
    int (*vidioc_prepare_buf)(struct file *file, void *fh,
    struct v4l2_buffer *b);

    int (*vidioc_overlay)(struct file *file, void *fh, unsigned int i);
    int (*vidioc_g_fbuf)(struct file *file, void *fh,
    struct v4l2_framebuffer *a);
    int (*vidioc_s_fbuf)(struct file *file, void *fh,
    const struct v4l2_framebuffer *a);

     /* Stream on/off */
    

    int (*vidioc_streamon)(struct file *file, void *fh,
    enum v4l2_buf_type i);
    int (*vidioc_streamoff)(struct file *file, void *fh,
    enum v4l2_buf_type i);

     /*
      * Standard handling
      *
      * Note: ENUMSTD is handled by videodev.c
      */
    

    int (*vidioc_g_std)(struct file *file, void *fh, v4l2_std_id *norm);
    int (*vidioc_s_std)(struct file *file, void *fh, v4l2_std_id norm);
    int (*vidioc_querystd)(struct file *file, void *fh, v4l2_std_id *a);

     /* Input handling */
    

    int (*vidioc_enum_input)(struct file *file, void *fh,
    struct v4l2_input *inp);
    int (*vidioc_g_input)(struct file *file, void *fh, unsigned int *i);
    int (*vidioc_s_input)(struct file *file, void *fh, unsigned int i);

     /* Output handling */
    

    int (*vidioc_enum_output)(struct file *file, void *fh,
    struct v4l2_output *a);
    int (*vidioc_g_output)(struct file *file, void *fh, unsigned int *i);
    int (*vidioc_s_output)(struct file *file, void *fh, unsigned int i);

     /* Control handling */
    

    int (*vidioc_queryctrl)(struct file *file, void *fh,
    struct v4l2_queryctrl *a);
    int (*vidioc_query_ext_ctrl)(struct file *file, void *fh,
    struct v4l2_query_ext_ctrl *a);
    int (*vidioc_g_ctrl)(struct file *file, void *fh,
    struct v4l2_control *a);
    int (*vidioc_s_ctrl)(struct file *file, void *fh,
    struct v4l2_control *a);
    int (*vidioc_g_ext_ctrls)(struct file *file, void *fh,
    struct v4l2_ext_controls *a);
    int (*vidioc_s_ext_ctrls)(struct file *file, void *fh,
    struct v4l2_ext_controls *a);
    int (*vidioc_try_ext_ctrls)(struct file *file, void *fh,
    struct v4l2_ext_controls *a);
    int (*vidioc_querymenu)(struct file *file, void *fh,
    struct v4l2_querymenu *a);

    /* Audio ioctls */
    int (*vidioc_enumaudio)(struct file *file, void *fh,
    struct v4l2_audio *a);
    int (*vidioc_g_audio)(struct file *file, void *fh,
    struct v4l2_audio *a);
    int (*vidioc_s_audio)(struct file *file, void *fh,
    const struct v4l2_audio *a);

    /* Audio out ioctls */
    int (*vidioc_enumaudout)(struct file *file, void *fh,
    struct v4l2_audioout *a);
    int (*vidioc_g_audout)(struct file *file, void *fh,
    struct v4l2_audioout *a);
    int (*vidioc_s_audout)(struct file *file, void *fh,
    const struct v4l2_audioout *a);
    int (*vidioc_g_modulator)(struct file *file, void *fh,
    struct v4l2_modulator *a);
    int (*vidioc_s_modulator)(struct file *file, void *fh,
    const struct v4l2_modulator a);
    /
    Crop ioctls */
    int (*vidioc_cropcap)(struct file *file, void *fh,
    struct v4l2_cropcap *a);
    int (*vidioc_g_crop)(struct file *file, void *fh,
    struct v4l2_crop *a);
    int (*vidioc_s_crop)(struct file *file, void *fh,
    const struct v4l2_crop *a);
    int (*vidioc_g_selection)(struct file *file, void *fh,
    struct v4l2_selection *s);
    int (*vidioc_s_selection)(struct file *file, void *fh,
    struct v4l2_selection s);
    /
    Compression ioctls */
    int (*vidioc_g_jpegcomp)(struct file *file, void *fh,
    struct v4l2_jpegcompression *a);
    int (*vidioc_s_jpegcomp)(struct file *file, void *fh,
    const struct v4l2_jpegcompression *a);
    int (*vidioc_g_enc_index)(struct file *file, void *fh,
    struct v4l2_enc_idx *a);
    int (*vidioc_encoder_cmd)(struct file *file, void *fh,
    struct v4l2_encoder_cmd *a);
    int (*vidioc_try_encoder_cmd)(struct file *file, void *fh,
    struct v4l2_encoder_cmd *a);
    int (*vidioc_decoder_cmd)(struct file *file, void *fh,
    struct v4l2_decoder_cmd *a);
    int (*vidioc_try_decoder_cmd)(struct file *file, void *fh,
    struct v4l2_decoder_cmd *a);

    /* Stream type-dependent parameter ioctls */
    int (*vidioc_g_parm)(struct file *file, void *fh,
    struct v4l2_streamparm *a);
    int (*vidioc_s_parm)(struct file *file, void *fh,
    struct v4l2_streamparm *a);

    /* Tuner ioctls */
    int (*vidioc_g_tuner)(struct file *file, void *fh,
    struct v4l2_tuner *a);
    int (*vidioc_s_tuner)(struct file *file, void *fh,
    const struct v4l2_tuner *a);
    int (*vidioc_g_frequency)(struct file *file, void *fh,
    struct v4l2_frequency *a);
    int (*vidioc_s_frequency)(struct file *file, void *fh,
    const struct v4l2_frequency *a);
    int (*vidioc_enum_freq_bands)(struct file *file, void *fh,
    struct v4l2_frequency_band *band);

    /* Sliced VBI cap */
    int (*vidioc_g_sliced_vbi_cap)(struct file *file, void *fh,
    struct v4l2_sliced_vbi_cap *a);

    /* Log status ioctl */
    int (*vidioc_log_status)(struct file *file, void *fh);

    int (*vidioc_s_hw_freq_seek)(struct file *file, void *fh,
    const struct v4l2_hw_freq_seek *a);

    /* Debugging ioctls */
    #ifdef CONFIG_VIDEO_ADV_DEBUG
    int (*vidioc_g_register)(struct file *file, void *fh,
    struct v4l2_dbg_register *reg);
    int (*vidioc_s_register)(struct file *file, void *fh,
    const struct v4l2_dbg_register *reg);

    int (*vidioc_g_chip_info)(struct file *file, void *fh,
    struct v4l2_dbg_chip_info *chip);
    #endif

    int (*vidioc_enum_framesizes)(struct file *file, void *fh,
    struct v4l2_frmsizeenum *fsize);

    int (*vidioc_enum_frameintervals)(struct file *file, void *fh,
    struct v4l2_frmivalenum *fival);

    /* DV Timings IOCTLs */
    int (*vidioc_s_dv_timings)(struct file *file, void *fh,
    struct v4l2_dv_timings *timings);
    int (*vidioc_g_dv_timings)(struct file *file, void *fh,
    struct v4l2_dv_timings *timings);
    int (*vidioc_query_dv_timings)(struct file *file, void *fh,
    struct v4l2_dv_timings *timings);
    int (*vidioc_enum_dv_timings)(struct file *file, void *fh,
    struct v4l2_enum_dv_timings *timings);
    int (*vidioc_dv_timings_cap)(struct file *file, void *fh,
    struct v4l2_dv_timings_cap *cap);
    int (*vidioc_g_edid)(struct file *file, void *fh,
    struct v4l2_edid *edid);
    int (*vidioc_s_edid)(struct file *file, void *fh,
    struct v4l2_edid *edid);

    int (*vidioc_subscribe_event)(struct v4l2_fh *fh,
    const struct v4l2_event_subscription *sub);
    int (*vidioc_unsubscribe_event)(struct v4l2_fh *fh,
    const struct v4l2_event_subscription *sub);

    /* For other private ioctls */
    long (*vidioc_default)(struct file *file, void *fh,
    bool valid_prio, unsigned int cmd, void *arg);
    };


示例:



// 源码:drivers/media/platform/sunxi/sun6i-csi/sun6i_video.c
static const struct v4l2_ioctl_ops sun6i_video_ioctl_ops = {
.vidioc_querycap = vidioc_querycap,
.vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
.vidioc_g_fmt_vid_cap = vidioc_g_fmt_vid_cap,
.vidioc_s_fmt_vid_cap = vidioc_s_fmt_vid_cap,
.vidioc_try_fmt_vid_cap = vidioc_try_fmt_vid_cap,

.vidioc_enum_input		= vidioc_enum_input,
.vidioc_s_input			= vidioc_s_input,
.vidioc_g_input			= vidioc_g_input,

.vidioc_reqbufs			= vb2_ioctl_reqbufs,
.vidioc_querybuf		= vb2_ioctl_querybuf,
.vidioc_qbuf			= vb2_ioctl_qbuf,
.vidioc_expbuf			= vb2_ioctl_expbuf,
.vidioc_dqbuf			= vb2_ioctl_dqbuf,
.vidioc_create_bufs		= vb2_ioctl_create_bufs,
.vidioc_prepare_buf		= vb2_ioctl_prepare_buf,
.vidioc_streamon		= vb2_ioctl_streamon,
.vidioc_streamoff		= vb2_ioctl_streamoff,

.vidioc_log_status		= v4l2_ctrl_log_status,
.vidioc_subscribe_event		= v4l2_ctrl_subscribe_event,
.vidioc_unsubscribe_event	= v4l2_event_unsubscribe,

};


### v4l2 API介绍


大多数操作都是通过应用层调用ioctl实现的,可以将这些ioctl分为下面几类。


#### Query Capability:


**查询设备支持的功能**,由于V4L2涵盖了各种各样的设备,因此并非API的所有方面都适用于所有类型的设备,在使用v4l2设备时,必须调用此API,获得设备支持的功能(capture、output、overlay…)。




| ID | 描述 |
| --- | --- |
| VIDIOC\_QUERYCAP | 查询设备功能 |


#### Application Priority


**应用优先级**


当多个应用程序共享设备时,可能需要为它们分配不同的优先级。视频录制应用程序可以例如阻止其他应用程序改变视频控制或切换当前的电视频道。另一个目标是允许在后台工作的低优先级应用程序,这些应用程序可以被用户控制的应用程序抢占,并在以后自动重新获得对设备的控制




| ID | 描述 |
| --- | --- |
| VIDIOC\_G\_PRIORITY | 获取优先级 |
| VIDIOC\_S\_PRIORITY | 设置优先级 |


#### Input/Output


**数据的输入和输出**


内核中使用缓存队列对图像数据进行管理,用户空间获取图像数据有两种方式,一种是通过read、write方式读取内核空间的缓存,一种是将内核空间的缓存映射到用户空间。在操作v4l2设备时,通过VIDIOC\_QUERYCAP获取设备支持哪种方式。




| ID | 描述 |
| --- | --- |
| VIDIOC\_REQBUFS | 向设备请求视频缓冲区,即初始化视频缓冲区 |
| VIDIOC\_QUERYBUF | 查询缓冲区的状态 |
| VIDIOC\_QBUF | 从设备获取一帧视频数据 |
| VIDIOC\_DQBUF | 将视频缓冲区归回给设备, |
| VIDIOC\_OVERLAY | 开始或者停止overlay |
| VIDIOC\_G\_FBUF | 获取video overlay设备或OSD设备的framebuffer参数 |
| VIDIOC\_S\_FBUF | 设置framebuffer参数 |
| VIDIOC\_STREAMON | 开始流I/O操作,capture or output device |
| VIDIOC\_STREAMOFF | 关闭流I/O操作 |


#### Image Format


**图像格式图像**  
 由多种格式YUV和RGB还有压缩格式等等,其中每种格式又分有多种格式,比如RGB:RGB565、RGB888…所以在使用设备时,需要对格式进行设置




| ID | 描述 |
| --- | --- |
| VIDIOC\_ENUM\_FMT | 枚举设备所支持的所有数据格式 |
| VIDIOC\_S\_FMT | 设置数据格式 |
| VIDIOC\_G\_FMT | 获取数据格式 |
| VIDIOC\_TRY\_FMT | 与VIDIOC\_S\_FMT一样,但不会改变设备的状态 |


#### TV视频标准


**TV Standard**




| ID | 描述 |
| --- | --- |
| VIDIOC\_ENUMSTD | 枚举设备支持的所有标准 |
| VIDIOC\_G\_STD | 获取当前正在使用的标准 |
| VIDIOC\_S\_STD | 设置视频标准 |
| VIDIOC\_QUERYSTD | 有的设备支持自动侦测输入源的视频标准,此时使用此ioctl查询侦测到的视频标准 |


#### Device Inputs and Outputs


**输入和输出设备**




| ID | 描述 |
| --- | --- |
| VIDIOC\_ENUMINPUT | 枚举所有input端口 |
| VIDIOC\_G\_INPUT | 获取当前正在使用的input端口 |
| VIDIOC\_S\_INPUT | 设置将要使用的input端口 |
| VIDIOC\_ENUMOUTPUT | 枚举所有output端口 |
| VIDIOC\_G\_OUTPUT | 获取当前正在使用的output端口 |
| VIDIOC\_S\_OUTPUT | 设置将要使用的output端口 |
| VIDIOC\_ENUMAUDIO | 枚举所有audio input端口 |
| VIDIOC\_G\_AUDIO | 获取当前正在使用的audio input端口 |
| VIDIOC\_S\_AUDIO | 设置将要使用的audio input端口 |
| VIDIOC\_ENUMAUDOUT | 枚举所有audio output端口 |
| VIDIOC\_G\_AUDOUT | 获取当前正在使用的audio output端口 |
| VIDIOC\_S\_AUDOUT | 设置将要使用的audio output端口 |


#### Camera Control Reference


**控制属性**  
 设备特定的控制,例如设置对比度,亮度




| ID | 描述 |
| --- | --- |
| VIDIOC\_QUERYCTRL | 查询指定control的详细信息 |
| VIDIOC\_G\_CTRL | 获取指定control的值 |
| VIDIOC\_S\_CTRL | 设置指定control的值 |
| VIDIOC\_G\_EXT\_CTRLS | 获取多个control的值 |
| VIDIOC\_S\_EXT\_CTRLS | 设置多个control的值 |
| VIDIOC\_TRY\_EXT\_CTRLS | 与VIDIOC\_S\_EXT\_CTRLS相同,但是不改变设备状态 |
| VIDIOC\_QUERYMENU | 查询menu |


#### Cropping, composing and scaling


**图像裁剪、插入与缩放**




| ID | 描述 |
| --- | --- |
| VIDIOC\_G\_CROP | 获取当前裁剪矩形 |
| VIDIOC\_S\_CROP | 设置当前裁剪矩形 |
| VIDIOC\_CROPCAP | 有关视频裁剪和缩放功能的信息 |


#### 其他杂项




| ID | 描述 |
| --- | --- |
| VIDIOC\_G\_MODULATOR |  |
| VIDIOC\_G\_SELECTION | 获取选择矩形 |
| VIDIOC\_S\_SELECTION | 设置选择矩形 |
| VIDIOC\_G\_ENC\_INDEX |  |
| VIDIOC\_ENCODER\_CMD |  |
| VIDIOC\_TRY\_ENCODER\_CMD |  |
| VIDIOC\_DECODER\_CMD |  |
| VIDIOC\_TRY\_DECODER\_CMD |  |
| VIDIOC\_G\_PARM | 获取数据流的参数,一般为帧率等参数 |
| VIDIOC\_S\_PARM | 设置数据流的参数,一般为帧率等参数 |
| VIDIOC\_G\_TUNER |  |
| VIDIOC\_S\_TUNER |  |
| VIDIOC\_G\_FREQUENCY |  |
| VIDIOC\_S\_FREQUENCY |  |
| VIDIOC\_G\_SLICED\_VBI\_CAP |  |
| VIDIOC\_LOG\_STATUS | 记录驱动程序状态信息 |
| VIDIOC\_DBG\_G\_CHIP\_IDENT |  |
| VIDIOC\_S\_HW\_FREQ\_SEEK |  |
| VIDIOC\_ENUM\_FRAMESIZES |  |
| VIDIOC\_ENUM\_FRAMEINTERVALS |  |
| VIDIOC\_ENUM\_DV\_PRESETS |  |
| VIDIOC\_S\_DV\_PRESET |  |
| VIDIOC\_G\_DV\_PRESET |  |
| VIDIOC\_QUERY\_DV\_PRESET |  |
| VIDIOC\_S\_DV\_TIMINGS |  |
| VIDIOC\_G\_DV\_TIMINGS |  |
| VIDIOC\_DQEVENT |  |
| VIDIOC\_SUBSCRIBE\_EVENT |  |
| VIDIOC\_UNSUBSCRIBE\_EVENT |  |
| VIDIOC\_CREATE\_BUFS | 为内存映射或用户指针或 DMA 缓冲区 I/O 创建缓冲区 |
| VIDIOC\_PREPARE\_BUF | 为 I/O 准备缓冲区 |




---


注:可参考Linux内核文档查找相关用法。  
 [Linux Media Subsystem Documentation]( )


### struct v4l2\_capability 与 VIDIOC\_QUERYCAP



// 源码:include/uapi/linux/videodev2.h
struct v4l2_capability {
__u8 driver[16]; // 驱动模块的名称,如"sun6i-video"
__u8 card[32]; // 品牌名称,如"sunxi WinTV"
__u8 bus_info[32]; // 总线名称,如"PCI:" + pci_name(pci_dev)
__u32 version; // 版本信息,KERNEL_VERSION
__u32 capabilities; // 设备整体的功能
__u32 device_caps;
__u32 reserved[3];
};


其中域 capabilities 代表设备支持的操作模式,常见的值有 V4L2\_CAP\_VIDEO\_CAPTURE | V4L2\_CAP\_STREAMING 表示是一个视频捕捉设备并且具有数据流控制模式;另外 driver 域需要和 struct video\_device 中的 name 匹配。  
 如下:



// 源码:include/uapi/linux/videodev2.h

/* Values for ‘capabilities’ field /
#define V4L2_CAP_VIDEO_CAPTURE 0x00000001 /
Is a video capture device 图像捕获设备 /
#define V4L2_CAP_VIDEO_OUTPUT 0x00000002 /
Is a video output device 图像输出设备 /
#define V4L2_CAP_VIDEO_OVERLAY 0x00000004 /
Can do video overlay 支持预览功能 /
#define V4L2_CAP_VBI_CAPTURE 0x00000010 /
Is a raw VBI capture device /
#define V4L2_CAP_VBI_OUTPUT 0x00000020 /
Is a raw VBI output device /
#define V4L2_CAP_SLICED_VBI_CAPTURE 0x00000040 /
Is a sliced VBI capture device /
#define V4L2_CAP_SLICED_VBI_OUTPUT 0x00000080 /
Is a sliced VBI output device /
#define V4L2_CAP_RDS_CAPTURE 0x00000100 /
RDS data capture /
#define V4L2_CAP_VIDEO_OUTPUT_OVERLAY 0x00000200 /
Can do video output overlay /
#define V4L2_CAP_HW_FREQ_SEEK 0x00000400 /
Can do hardware frequency seek /
#define V4L2_CAP_RDS_OUTPUT 0x00000800 /
Is an RDS encoder */

/* Is a video capture device that supports multiplanar formats /
#define V4L2_CAP_VIDEO_CAPTURE_MPLANE 0x00001000
/
Is a video output device that supports multiplanar formats /
#define V4L2_CAP_VIDEO_OUTPUT_MPLANE 0x00002000
/
Is a video mem-to-mem device that supports multiplanar formats /
#define V4L2_CAP_VIDEO_M2M_MPLANE 0x00004000
/
Is a video mem-to-mem device */
#define V4L2_CAP_VIDEO_M2M 0x00008000

#define V4L2_CAP_TUNER 0x00010000 /* has a tuner /
#define V4L2_CAP_AUDIO 0x00020000 /
has audio support /
#define V4L2_CAP_RADIO 0x00040000 /
is a radio device /
#define V4L2_CAP_MODULATOR 0x00080000 /
has a modulator */

#define V4L2_CAP_SDR_CAPTURE 0x00100000 /* Is a SDR capture device /
#define V4L2_CAP_EXT_PIX_FORMAT 0x00200000 /
Supports the extended pixel format /
#define V4L2_CAP_SDR_OUTPUT 0x00400000 /
Is a SDR output device /
#define V4L2_CAP_META_CAPTURE 0x00800000 /
Is a metadata capture device */

#define V4L2_CAP_READWRITE 0x01000000 /* read/write systemcalls /
#define V4L2_CAP_ASYNCIO 0x02000000 /
async I/O /
#define V4L2_CAP_STREAMING 0x04000000 /
streaming I/O ioctls /
#define V4L2_CAP_META_OUTPUT 0x08000000 /
Is a metadata output device */

#define V4L2_CAP_TOUCH 0x10000000 /* Is a touch device */

#define V4L2_CAP_DEVICE_CAPS 0x80000000 /* sets device capabilities field */


### struct v4l2\_format 与 VIDIOC\_G\_FMT、VIDIOC\_S\_FMT、VIDIOC\_TRY\_FMT



// 源码:include/uapi/linux/videodev2.h
struct v4l2_format {
__u32 type;
union {
struct v4l2_pix_format pix; /* V4L2_BUF_TYPE_VIDEO_CAPTURE /
struct v4l2_pix_format_mplane pix_mp; /
V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE /
struct v4l2_window win; /
V4L2_BUF_TYPE_VIDEO_OVERLAY /
struct v4l2_vbi_format vbi; /
V4L2_BUF_TYPE_VBI_CAPTURE /
struct v4l2_sliced_vbi_format sliced; /
V4L2_BUF_TYPE_SLICED_VBI_CAPTURE /
struct v4l2_sdr_format sdr; /
V4L2_BUF_TYPE_SDR_CAPTURE /
struct v4l2_meta_format meta; /
V4L2_BUF_TYPE_META_CAPTURE /
__u8 raw_data[200]; /
user-defined */
} fmt;
};

type如下:
enum v4l2_buf_type {
V4L2_BUF_TYPE_VIDEO_CAPTURE = 1,
V4L2_BUF_TYPE_VIDEO_OUTPUT = 2,
V4L2_BUF_TYPE_VIDEO_OVERLAY = 3,
V4L2_BUF_TYPE_VBI_CAPTURE = 4,
V4L2_BUF_TYPE_VBI_OUTPUT = 5,
V4L2_BUF_TYPE_SLICED_VBI_CAPTURE = 6,
V4L2_BUF_TYPE_SLICED_VBI_OUTPUT = 7,
V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY = 8,
V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE = 9,
V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE = 10,
V4L2_BUF_TYPE_SDR_CAPTURE = 11,
V4L2_BUF_TYPE_SDR_OUTPUT = 12,
V4L2_BUF_TYPE_META_CAPTURE = 13,
/* Deprecated, do not use */
V4L2_BUF_TYPE_PRIVATE = 0x80,
};

/*

  • V I D E O I M A G E F O R M A T
    /
    struct v4l2_pix_format {
    __u32 width;
    __u32 height;
    __u32 pixelformat;
    __u32 field; /
    enum v4l2_field /
    __u32 bytesperline; /
    for padding, zero if unused /
    __u32 sizeimage;
    __u32 colorspace; /
    enum v4l2_colorspace /
    __u32 priv; /
    private data, depends on pixelformat /
    __u32 flags; /
    format flags (V4L2_PIX_FMT_FLAG_*) /
    union {
    /
    enum v4l2_ycbcr_encoding /
    __u32 ycbcr_enc;
    /
    enum v4l2_hsv_encoding /
    __u32 hsv_enc;
    };
    __u32 quantization; /
    enum v4l2_quantization /
    __u32 xfer_func; /
    enum v4l2_xfer_func */
    };

struct v4l2_pix_format_mplane {
__u32 width;
__u32 height;
__u32 pixelformat;
__u32 field;
__u32 colorspace;

struct v4l2_plane_pix_format	plane_fmt[VIDEO_MAX_PLANES];
__u8				num_planes;
__u8				flags;
 union {
	__u8				ycbcr_enc;
	__u8				hsv_enc;
};
__u8				quantization;
__u8				xfer_func;
__u8				reserved[7];

} attribute ((packed));

struct v4l2_window {
struct v4l2_rect w;
__u32 field; /* enum v4l2_field */
__u32 chromakey;
struct v4l2_clip __user *clips;
__u32 clipcount;
void __user *bitmap;
__u8 global_alpha;
};

/* Raw VBI /
struct v4l2_vbi_format {
__u32 sampling_rate; /
in 1 Hz /
__u32 offset;
__u32 samples_per_line;
__u32 sample_format; /
V4L2_PIX_FMT_* /
__s32 start[2];
__u32 count[2];
__u32 flags; /
V4L2_VBI_* /
__u32 reserved[2]; /
must be zero */
};

struct v4l2_sliced_vbi_format {
__u16 service_set;
/* service_lines[0][…] specifies lines 0-23 (1-23 used) of the first field
service_lines[1][…] specifies lines 0-23 (1-23 used) of the second field
(equals frame lines 313-336 for 625 line video
standards, 263-286 for 525 line standards) /
__u16 service_lines[2][24];
__u32 io_size;
__u32 reserved[2]; /
must be zero */
};

struct v4l2_sdr_format {
__u32 pixelformat;
__u32 buffersize;
__u8 reserved[24];
} attribute ((packed));

struct v4l2_meta_format {
__u32 dataformat;
__u32 buffersize;
} attribute ((packed));


常见的捕获模式为 V4L2\_BUF\_TYPE\_VIDEO\_CAPTURE 即视频捕捉模式,在此模式下 fmt 联合体采用域 v4l2\_pix\_format:其中 width 为视频的宽、height 为视频的高、pixelformat 为视频数据格式(常见的值有 V4L2\_PIX\_FMT\_YUV422P | V4L2\_PIX\_FMT\_RGB565)、bytesperline 为一行图像占用的字节数、sizeimage 则为图像占用的总字节数、colorspace 指定设备的颜色空间。


### struct v4l2\_requestbuffers 与 VIDIOC\_REQBUFS



// 源码:include/uapi/linux/videodev2.h
/*

  • M E M O R Y - M A P P I N G B U F F E R S
    /
    struct v4l2_requestbuffers {
    __u32 count;
    __u32 type; /
    enum v4l2_buf_type /
    __u32 memory; /
    enum v4l2_memory */
    __u32 reserved[2];
    };

enum v4l2_buf_type {
V4L2_BUF_TYPE_VIDEO_CAPTURE = 1,
V4L2_BUF_TYPE_VIDEO_OUTPUT = 2,
V4L2_BUF_TYPE_VIDEO_OVERLAY = 3,
V4L2_BUF_TYPE_VBI_CAPTURE = 4,
V4L2_BUF_TYPE_VBI_OUTPUT = 5,
V4L2_BUF_TYPE_SLICED_VBI_CAPTURE = 6,
V4L2_BUF_TYPE_SLICED_VBI_OUTPUT = 7,
V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY = 8,
V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE = 9,
V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE = 10,
V4L2_BUF_TYPE_SDR_CAPTURE = 11,
V4L2_BUF_TYPE_SDR_OUTPUT = 12,
V4L2_BUF_TYPE_META_CAPTURE = 13,
/* Deprecated, do not use */
V4L2_BUF_TYPE_PRIVATE = 0x80,
};

enum v4l2_memory {
V4L2_MEMORY_MMAP = 1,
V4L2_MEMORY_USERPTR = 2,
V4L2_MEMORY_OVERLAY = 3,
V4L2_MEMORY_DMABUF = 4,
};


count 指定根据图像占用空间大小申请的缓存区个数,type 为视频捕获模式,memory 为内存区的使用方式。


### struct v4l2\_buffer与 VIDIOC\_QUERYBUF、VIDIOC\_QBUF、VIDIOC\_DQBUF


VIDIOC\_QUERYBUF 命令通过结构 v4l2\_buffer 查询驱动申请的内存区信息:



// 源码:include/uapi/linux/videodev2.h
struct v4l2_buffer {
__u32 index;
__u32 type;
__u32 bytesused;
__u32 flags;
__u32 field;
struct timeval timestamp;
struct v4l2_timecode timecode;
__u32 sequence;

/* memory location */
__u32			memory;
union {
	__u32           offset;
	unsigned long   userptr;
	struct v4l2_plane *planes;
	__s32		fd;
} m;
__u32			length;
__u32			reserved2;
__u32			reserved;

};


### enum v4l2\_buf\_type 与 VIDIOC\_STREAMON、VIDIOC\_STREAMOFF


这两个命令使用的只是一个整形数据,即 v4l2\_buf\_type,一般只要指定其值为 V4L2\_BUF\_TYPE\_VIDEO\_CAPTURE 即可。


### struct v4l2\_queryctrl 与 VIDIOC\_QUERYCTRL


VIDIOC\_QUERYCTRL 命令通过结构 v4l2\_queryctrl 查询驱动是否支持该 id 代表的命令,并返回该命令的各种参数:



// 源码:include/uapi/linux/videodev2.h
/* Used in the VIDIOC_QUERYCTRL ioctl for querying controls /
struct v4l2_queryctrl {
__u32 id; /
命令编号 /
__u32 type; /
enum v4l2_ctrl_type 命令值的类型 /
__u8 name[32]; /
命令名称*/
__s32 minimum; /* 最小的命令值 /
__s32 maximum; /
最大的命令值 /
__s32 step; /
命令值变化的步长 /
__s32 default_value; /
默认的命令值 /
__u32 flags; /
命令的标志 /
__u32 reserved[2]; /
命令值的位图表示 */
};

enum v4l2_ctrl_type {
V4L2_CTRL_TYPE_INTEGER = 1, /* 整形 /
V4L2_CTRL_TYPE_BOOLEAN = 2, /
真值 /
V4L2_CTRL_TYPE_MENU = 3, /
菜单 /
V4L2_CTRL_TYPE_BUTTON = 4, /
无值 /
V4L2_CTRL_TYPE_INTEGER64 = 5, /
后面三种不常用 */
V4L2_CTRL_TYPE_CTRL_CLASS = 6
V4L2_CTRL_TYPE_STRING = 7,
V4L2_CTRL_TYPE_BITMASK = 8,
V4L2_CTRL_TYPE_INTEGER_MENU = 9,

/* Compound types are >= 0x0100 */
V4L2_CTRL_COMPOUND_TYPES    = 0x0100,
V4L2_CTRL_TYPE_U8	     = 0x0100,
V4L2_CTRL_TYPE_U16	     = 0x0101,
V4L2_CTRL_TYPE_U32	     = 0x0102,

};


命令的标志取值如下:



// 源码:include/uapi/linux/videodev2.h
/* Control flags */
#define V4L2_CTRL_FLAG_DISABLED 0x0001
#define V4L2_CTRL_FLAG_GRABBED 0x0002
#define V4L2_CTRL_FLAG_READ_ONLY 0x0004
#define V4L2_CTRL_FLAG_UPDATE 0x0008
#define V4L2_CTRL_FLAG_INACTIVE 0x0010
#define V4L2_CTRL_FLAG_SLIDER 0x0020
#define V4L2_CTRL_FLAG_WRITE_ONLY 0x0040


id 是命令的编号,常见的命令有两种:一种以 V4L2\_CID\_BASE 为起始值,是公用命令;一种以 V4L2\_CID\_PRIVATE\_BASE 为起始值,是私有命令。在一般的应用中命令值可见如下:



// 源码:include/uapi/linux/v4l2-controls.h
#define V4L2_CID_BASE (V4L2_CTRL_CLASS_USER | 0x900)
#define V4L2_CID_USER_BASE V4L2_CID_BASE
#define V4L2_CID_USER_CLASS (V4L2_CTRL_CLASS_USER | 1)
#define V4L2_CID_BRIGHTNESS (V4L2_CID_BASE+0)
#define V4L2_CID_CONTRAST (V4L2_CID_BASE+1) /* 对比度调节 /
#define V4L2_CID_SATURATION (V4L2_CID_BASE+2) /
饱和度调节 /
#define V4L2_CID_HUE (V4L2_CID_BASE+3)
#define V4L2_CID_AUDIO_VOLUME (V4L2_CID_BASE+5) /
音量调节 /
#define V4L2_CID_AUDIO_BALANCE (V4L2_CID_BASE+6)
#define V4L2_CID_AUDIO_BASS (V4L2_CID_BASE+7)
#define V4L2_CID_AUDIO_TREBLE (V4L2_CID_BASE+8)
#define V4L2_CID_AUDIO_MUTE (V4L2_CID_BASE+9) /
静音设置 /
#define V4L2_CID_AUDIO_LOUDNESS (V4L2_CID_BASE+10)
#define V4L2_CID_BLACK_LEVEL (V4L2_CID_BASE+11) /
Deprecated /
#define V4L2_CID_AUTO_WHITE_BALANCE (V4L2_CID_BASE+12)
#define V4L2_CID_DO_WHITE_BALANCE (V4L2_CID_BASE+13) /
白平衡调节 /
#define V4L2_CID_RED_BALANCE (V4L2_CID_BASE+14)
#define V4L2_CID_BLUE_BALANCE (V4L2_CID_BASE+15)
#define V4L2_CID_GAMMA (V4L2_CID_BASE+16) /
伽马值调节 /
#define V4L2_CID_WHITENESS (V4L2_CID_GAMMA) /
Deprecated /
#define V4L2_CID_EXPOSURE (V4L2_CID_BASE+17) /
曝光度调节 */
#define V4L2_CID_AUTOGAIN (V4L2_CID_BASE+18)
#define V4L2_CID_GAIN (V4L2_CID_BASE+19)
#define V4L2_CID_HFLIP (V4L2_CID_BASE+20)
#define V4L2_CID_VFLIP (V4L2_CID_BASE+21)


type 为命令值的类型(总共有7中类型的值),name 是命令的名称,reserved 则是命令值的位图表示,驱动会将所有的命令值都以 bit 的形式写到 64 位的域中,上层应用查询时可以根据位图判断命令支持的值。


### ioctl的实现video\_ioctl2



// 源码:drivers/media/v4l2-core/v4l2-ioctl.c
long video_ioctl2(struct file *file,
unsigned int cmd, unsigned long arg)
{
return video_usercopy(file, cmd, arg, __video_do_ioctl);
}



// 源码:drivers/media/v4l2-core/v4l2-ioctl.c

long
video_usercopy(struct file *file, unsigned int cmd, unsigned long arg,
v4l2_kioctl func)
{
char sbuf[128];
void *mbuf = NULL;
void *parg = (void *)arg;
long err = -EINVAL;
bool has_array_args;
bool always_copy = false;
size_t array_size = 0;
void __user *user_ptr = NULL;
void **kernel_ptr = NULL;
const size_t ioc_size = _IOC_SIZE(cmd);

/*  Copy arguments into temp kernel buffer  */
/* 判断是否包含读写命令,如果是则将用户空间的参数值拷贝到内核 */
if (_IOC_DIR(cmd) != _IOC_NONE) {
	 /* 判断参数大小是否超过128字节 */
	if (ioc_size <= sizeof(sbuf)) {
		parg = sbuf;
	} else {
		/* too big to allocate from stack */
		/* 如果超过128字节则从堆中申请 */
		mbuf = kvmalloc(ioc_size, GFP_KERNEL);
		if (NULL == mbuf)
			return -ENOMEM;
		parg = mbuf;
	}

	err = -EFAULT;
	if (_IOC_DIR(cmd) & _IOC_WRITE) {
		unsigned int n = ioc_size;

		/*
		 * In some cases, only a few fields are used as input,
		 * i.e. when the app sets "index" and then the driver
		 * fills in the rest of the structure for the thing
		 * with that index.  We only need to copy up the first
		 * non-input field.
		 */
		/* 确认命令是否为支持命令 */
		if (v4l2_is_known_ioctl(cmd)) {
			/* 计算需要拷贝的有效数据长度,有的命令不需要全部拷贝 */
			u32 flags = v4l2_ioctls[_IOC_NR(cmd)].flags;

			if (flags & INFO_FL_CLEAR_MASK)
				n = (flags & INFO_FL_CLEAR_MASK) >> 16;
			always_copy = flags & INFO_FL_ALWAYS_COPY;
		}

		/* 从用户空间拷贝参数值 */
		if (copy_from_user(parg, (void __user *)arg, n))
			goto out;

		/* zero out anything we don't copy from userspace */
		if (n < ioc_size)  /* 将剩下的空间清零 */
			memset((u8 *)parg + n, 0, ioc_size - n);
	} else {
		/* read-only ioctl */ /* 如果是只读命令则将整个buffer清零 */
		memset(parg, 0, ioc_size);
	}
}

/* 特殊命令个性化操作 */
err = check_array_args(cmd, parg, &array_size, &user_ptr, &kernel_ptr);
if (err < 0)
	goto out;
has_array_args = err;

if (has_array_args) {
	/*
	 * When adding new types of array args, make sure that the
	 * parent argument to ioctl (which contains the pointer to the
	 * array) fits into sbuf (so that mbuf will still remain
	 * unused up to here).
	 */
	mbuf = kvmalloc(array_size, GFP_KERNEL);
	err = -ENOMEM;
	if (NULL == mbuf)
		goto out_array_args;
	err = -EFAULT;
	if (copy_from_user(mbuf, user_ptr, array_size))
		goto out_array_args;
	*kernel_ptr = mbuf;
}

/* Handles IOCTL */
/* 调用 v4l2_ioctl_ops 的成员函数处理命令 */
err = func(file, cmd, parg);
if (err == -ENOTTY || err == -ENOIOCTLCMD) {
	err = -ENOTTY;
	goto out;
}

if (err == 0) {
	if (cmd == VIDIOC_DQBUF)
		trace_v4l2_dqbuf(video_devdata(file)->minor, parg);
	else if (cmd == VIDIOC_QBUF)
		trace_v4l2_qbuf(video_devdata(file)->minor, parg);
}

if (has_array_args) {
	*kernel_ptr = (void __force *)user_ptr;
	if (copy_to_user(user_ptr, mbuf, array_size))
		err = -EFAULT;
	goto out_array_args;
}
/*
 * Some ioctls can return an error, but still have valid
 * results that must be returned.
 */
if (err < 0 && !always_copy)
	goto out;

out_array_args:
/* Copy results into user buffer /
/
如果包含读命令则将参数值拷贝到用户空间 */
switch (_IOC_DIR(cmd)) {
case _IOC_READ:
case (_IOC_WRITE | _IOC_READ):
if (copy_to_user((void __user *)arg, parg, ioc_size))
err = -EFAULT;
break;
}

out:
kvfree(mbuf);
return err;
}



// 源码:drivers/media/v4l2-core/v4l2-ioctl.c

static int check_array_args(unsigned int cmd, void *parg, size_t *array_size,
void __user **user_ptr, void ***kernel_ptr)
{
int ret = 0;

switch (cmd) {
case VIDIOC_PREPARE_BUF:
case VIDIOC_QUERYBUF:
case VIDIOC_QBUF:
case VIDIOC_DQBUF: {
	struct v4l2_buffer *buf = parg;

	if (V4L2_TYPE_IS_MULTIPLANAR(buf->type) && buf->length > 0) {
		if (buf->length > VIDEO_MAX_PLANES) {
			ret = -EINVAL;
			break;
		}
		*user_ptr = (void __user *)buf->m.planes;
		*kernel_ptr = (void **)&buf->m.planes;
		*array_size = sizeof(struct v4l2_plane) * buf->length;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值