In the application to obtain video data flow, are interacting with the driver via ioctl command, common ioctl commands are:
1 VIDIOC_QUERYCAP / * Get device supports operation * / 2 VIDIOC_G_FMT / * Get set video format supported * / . 3 VIDIOC_S_FMT / * format capturing video * / . 4 VIDIOC_REQBUFS / * request for memory to drive * / . 5 VIDIOC_QUERYBUF / * drive to check the application to the memory * / 6 VIDIOC_QBUF / * free memory will be added to capture video of the queue * / 7 VIDIOC_DQBUF / * to capture good video memory has been pulled out of the captured video queue * / 8 VIDIOC_STREAMON / * open the video stream * / . 9 VIDIOC_STREAMOFF / * Close the video stream * / 10 VIDIOC_QUERYCTRL / * query driver support this command * / . 11 VIDIOC_G_CTRL / * Get the current command value * / 12 is VIDIOC_S_CTRL / * set the new command value * / 13 is VIDIOC_G_TUNER / tuner * Get information * / 14 VIDIOC_S_TUNER / * a tuner information * / 15 VIDIOC_G_FREQUENCY / * Get tuner frequencies * / 16 VIDIOC_S_FREQUENCY / * set the tuner frequencies * /
1、struct v4l2_capability 与 VIDIOC_QUERYCAP
VIDIOC_QUERYCAP v4l2_capability acquisition command operation mode of the device supported by the structure:
1 struct v4l2_capability { 2 __u8 driver[16]; /* i.e. "bttv" */ 3 __u8 card[32]; /* i.e. "Hauppauge WinTV" */ 4 __u8 bus_info[32]; /* "PCI:" + pci_name(pci_dev) */ 5 __u32 version; /* should use KERNEL_VERSION() */ 6 __u32 capabilities; /* Device capabilities */ 7 __u32 reserved[4]; 8 };
Wherein the domain representatives capabilities supported by the device operating mode, common values are V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING is represented by a video capture device and a data flow control mode; additional matching driver name and domain requires the struct video_device.
2、struct v4l2_format 与 VIDIOC_G_FMT、VIDIOC_S_FMT、VIDIOC_TRY_FMT
Typically by treatment with VIDIOC_S_FMT initialization command structure v4l2_format captured video format, if the format is to be changed with the command VIDIOC_TRY_FMT:
1 struct v4l2_format { 2 enum v4l2_buf_type type; 3 union { 4 struct v4l2_pix_format pix; /* V4L2_BUF_TYPE_VIDEO_CAPTURE */ 5 struct v4l2_window win; /* V4L2_BUF_TYPE_VIDEO_OVERLAY */ 6 struct v4l2_vbi_format vbi; /* V4L2_BUF_TYPE_VBI_CAPTURE */ 7 struct v4l2_sliced_vbi_format sliced; /* V4L2_BUF_TYPE_SLICED_VBI_CAPTURE */ 8 __u8 raw_data[200]; /* user-defined */ 9 } fmt; 10 }; 11 其中 12 enum v4l2_buf_type { 13 V4L2_BUF_TYPE_VIDEO_CAPTURE = 1, 14 V4L2_BUF_TYPE_VIDEO_OUTPUT = 2, 15 V4L2_BUF_TYPE_VIDEO_OVERLAY = 3, 16 ... 17 V4L2_BUF_TYPE_PRIVATE = 0x80, 18 }; 19 20 struct v4l2_pix_format { 21 __u32 width; 22 __u32 height; 23 __u32 pixelformat; 24 enum v4l2_field field; 25 __u32 bytesperline; /* for padding, zero if unused */ 26 __u32 sizeimage; 27 enum v4l2_colorspace colorspace; 28 __u32 priv; /* private data, depends on pixelformat */ 29 };
3、struct v4l2_requestbuffers 与 VIDIOC_REQBUFS
VIDIOC_REQBUFS command requests the drive to apply a continuous memory for buffering the video information structure v4l2_requestbuffers:
1 struct v4l2_requestbuffers { 2 __u32 count; 3 enum v4l2_buf_type type; 4 enum v4l2_memory memory; 5 __u32 reserved[2]; 6 }; 7 其中 8 enum v4l2_memory { 9 V4L2_MEMORY_MMAP = 1, 10 V4L2_MEMORY_USERPTR = 2, 11 V4L2_MEMORY_OVERLAY = 3, 12 };
Count specifies the number of buffer space size of the image according to the application, type of video capture mode, memory to use the memory area.
4、struct v4l2_buffer与 VIDIOC_QUERYBUF
VIDIOC_QUERYBUF command structure v4l2_buffer query-driven application memory area information:
1 struct v4l2_buffer { 2 __u32 index; 3 enum v4l2_buf_type type; 4 __u32 bytesused; 5 __u32 flags; 6 enum v4l2_field field; 7 struct timeval timestamp; 8 struct v4l2_timecode timecode; 9 __u32 sequence; 10 11 /* memory location */ 12 enum v4l2_memory memory; 13 union { 14 __u32 offset; 15 unsigned long userptr; 16 } m; 17 __u32 length; 18 __u32 input; 19 __u32 reserved; 20 };
5、enum v4l2_buf_type 与 VIDIOC_STREAMON、VIDIOC_STREAMOFF
Just a shaping data using these two commands, i.e. v4l2_buf_type, designated generally as long as its value can V4L2_BUF_TYPE_VIDEO_CAPTURE.
6、struct v4l2_queryctrl 与 VIDIOC_QUERYCTRL
VIDIOC_QUERYCTRL command to check whether the driver of the id command represented by the support structure v4l2_queryctrl, and returns the command of various parameters:
. 1 struct v4l2_queryctrl { 2 __u32 ID; / * command number * / . 3 enum v4l2_ctrl_type type; / * order type values * / . 4 __u8 name [32]; / * command name * / . 5 __s32 Minimum; / * minimum command value * / . 6 __s32 maximum; / * maximum command value * / . 7 __s32 sTEP; / * command value changes in steps * / . 8 __s32 default_value; / * default command value * / . 9 __u32 the flags; / * flag command * / 10 __u32 Reserved [2]; / * bitmap indicates a command value * / 11}; 12 wherein 13 is v4l2_ctrl_type enum { 14 V4L2_CTRL_TYPE_INTEGER =. 1, / * * shaping / 15 V4L2_CTRL_TYPE_BOOLEAN = 2, / * true value * / 16 = V4L2_CTRL_TYPE_MENU. 3, / * Menu * / . 17 = V4L2_CTRL_TYPE_BUTTON. 4, / * no value * / 18 is V4L2_CTRL_TYPE_INTEGER64 =. 5, / * last three are not used * / . 19. 6 = V4L2_CTRL_TYPE_CTRL_CLASS , 20 is V4L2_CTRL_TYPE_STRING =. 7, 21 is}; flag command values below 22: 23 is / * Control the flags * / 24 #define V4L2_CTRL_FLAG_DISABLED 0x0001 25 0x0002 #define V4L2_CTRL_FLAG_GRABBED 26 is 0x0004 #define V4L2_CTRL_FLAG_READ_ONLY 27 #define V4L2_CTRL_FLAG_UPDATE 0x0008 28 0x0010 #define V4L2_CTRL_FLAG_INACTIVE 29 #define V4L2_CTRL_FLAG_SLIDER 0x0020 30 #define V4L2_CTRL_FLAG_WRITE_ONLY 0x0040 31 32 /* Query flag, to be ORed with the control ID */ 33 #define V4L2_CTRL_FLAG_NEXT_CTRL 0x80000000
id is the number of commands, common commands are two: one to V4L2_CID_BASE as the start value, is common commands; a kind of V4L2_CID_PRIVATE_BASE as the start value, is a private command. In a typical application can be seen command values as follows:
1 V4L2_CID_CONTRAST (V4L2_CID_BASE + 1) / * Contrast Adjustment * / 2 V4L2_CID_SATURATION (V4L2_CID_BASE + 2) / * the saturation adjustment * / . 3 V4L2_CID_AUDIO_VOLUME (V4L2_CID_BASE +. 5) / * Volume adjustment * / . 4 V4L2_CID_AUDIO_MUTE (V4L2_CID_BASE +. 9) / * Mute set * / . 5 V4L2_CID_DO_WHITE_BALANCE (+ 13 is V4L2_CID_BASE) / white balance adjustment * * / . 6 V4L2_CID_GAMMA (V4L2_CID_BASE + 16) / * adjust the gamma value * / . 7 V4L2_CID_EXPOSURE (V4L2_CID_BASE +. 17) / * exposure adjustment * / . 8 . 9 V4L2_CID_PRIVATE_ATXX_FLASH ( V4L2_CID_PRIVATE_BASE + 2) / * flash control * / 10 V4L2_CID_PRIVATE_ATXX_FRAME (+ 12 is V4L2_CID_PRIVATE_BASE) / * frame rate adjustment * /
type is the type of command value (a total value of type 7), name is the name of the command, the bitmap Reserved command value indicates that the drive will write all command values are in the form of 64-bit bit field , the value of the command based on the upper bit map supported application queries.
7、struct v4l2_control 与 VIDIOC_G_CTRL、VIDIOC_S_CTRL
VIDIOC_S_CTRL or VIDIOC_G_CTRL command value id command set or get through structural v4l2_control:
1 struct v4l2_control { 2 __u32 id; 3 __s32 value; 4 };
8, struct v4l2_tuner 与 VIDIOC_G_TUNER, VIDIOC_S_TUNER
VIDIOC_S_TUNER VIDIOC_G_TUNER command or information provided by the tuner structure v4l2_tuner:
1 struct v4l2_tuner {
2 __u32 index; / * number tuner, set by the application * /
3 __u8 name [32]; / * * Name Tuner /
4 enum v4l2_tuner_type type; / * tuner type * /
5 __u32 capability; / * * Supported operating Tuner /
6 __u32 rangelow; / * lowest frequency value, or unit 62.5Hz 62.5KHz * /
7 __u32 rangehigh; / * maximum frequency value * /
8 __u32 rxsubchans; / * type of the audio signal received * /
9 __u32 audmode; / * current form of audio playback * /
10 __s32 signal; / * signal intensity * /
11 __s32 afc; / * automatic frequency control * /
12 __u32 reserved [4]; / * Reserved * /
13 };
14 in which
15 enum v4l2_tuner_type {
16 V4L2_TUNER_RADIO = 1, / * FM radio * /
17 V4L2_TUNER_ANALOG_TV = 2, / * * analog TV tuner /
18 V4L2_TUNER_DIGITAL_TV = 3, / * * Digital TV tuner /
19 };
Wherein the type field has three types; Capability field generally V4L2_TUNER_CAP_LOW, show frequency adjustment step is 62.5Hz, if this flag is not in steps of 62.5KHz; rangelow rangehigh with the highest and lowest values of frequency tuner may tune , but are represented in units of steps; rxsubchans indicates the type of the audio signal received by the tuner, common values are V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_STEREO i.e. mono and stereo; audmode showing the manner in which play a sound, common values are V4L2_TUNER_MODE_MONO | V4L2_TUNER_MODE_STEREO, i.e. in mono or in stereo playback; signal current signal strength, typically in the range of 0 - 65535.
9、struct v4l2_frequency 与 VIDIOC_G_FREQUENCY、VIDIOC_S_FREQUENCY
VIDIOC_S_FREQUENCY or VIDIOC_G_FREQUENCY v4l2_frequency command set or retrieves the current frequency value by the structure:
v4l2_frequency {struct __u32 Tuner; / * number tuner * / enum type v4l2_tuner_type; / * tuner type * / __u32 Frequency; / tuner frequencies * * / __u32 Reserved [. 8]; };
Note: The value of frequency is 62.5Hz or 62.5KHZ units.
Annex: _IO, _IOR, _IOW, _IOWR macro instructions
Cmd variable driver ioctl function is transmitted command processing application requests to the driver. cmd addition to distinguish different command values may comprise several processing information can help. cmd size of 32 bit, divided into four domains:
~ bit31 Bit29: 3bit as "read-write" area, is to distinguish between a read command or write command.
~ bit28 Bit16: 13bit "Data Size" field, data indicating the size of the ioctl arg variable transmission; 14bit coming bit29 is sometimes covered.
~ bit15 of bit8: 8bit as a "magic number" (also called "magic number") region, the ioctl command value to the other device driver to distinguish.
~ bit7 bit0: 8bit as "number" region is a sequence of commands to distinguish the command number.
Sum (magic number)
magic number in the range 0 to 255. Typically, the English character 'A' ~ 'Z' or 'a' ~ 'z' is represented. Device driver acquires from the command passed in the magic number, the magic number is then want to compare their own processing, if the same process is not different treatment. Magic number is rejected misuse preliminary auxiliary parameters. Device drivers may be acquired through the macro magic number _IOC_TYPE (cmd). Different device drivers for the best set different magic number, but not an absolute requirement, but also can use the magic number other device drivers are used.
Cardinality (number)
base for distinguishing between various commands. Typically, from 0 starts incrementing that value can be reused on the same device driver. For example, read and write commands used in the same base, the device driver will be able to tell, because when using the switch command to the device driver to distinguish between, and directly use the command cmd variable value. Create Macro command value generated from a combination of a plurality of fields, even if the base is the same, different command is also determined. Device drivers want to get from the base command, use the macro _IOC_NR (cmd).
Here we look at the above macro prototype in the kernel:
Here we look at the above macro prototype in the kernel
:
1 /* 2 * Our DIR and SIZE overlap in order to simulteneously provide 3 * a non-zero _IOC_NONE (for binary compatibility) and 4 * 14 bits of size as on i386. Here's the layout: 5 * 6 * 0xE0000000 DIR 3bit 7 * 0x80000000 DIR = WRITE bit31 8 * 0x40000000 DIR = READ bit30 9 * 0x20000000 DIR = NONE bit29 10 * 0x3FFF0000 SIZE (overlaps NONE bit) 13bit 11 * 0x0000FF00 TYPE 8bit 12 * 0x000000FF NR (CMD) 8bit 13 */ 14 /* 各个域的长度 */ 15 #define _IOC_NRBITS 8 16 #define _IOC_TYPEBITS 8 17 #define _IOC_SIZEBITS 13 /* Actually 14, see below. */ 18 #define _IOC_DIRBITS 3 19 /* 各个域的掩码 */ 20 #define _IOC_NRMASK ((1 << _IOC_NRBITS)-1) 21 #define _IOC_TYPEMASK ((1 << _IOC_TYPEBITS)-1) 22 #define _IOC_SIZEMASK ((1 << _IOC_SIZEBITS)-1) 23 #define _IOC_XSIZEMASK ((1 << (_IOC_SIZEBITS+1))-1) 24 #define _IOC_DIRMASK ((1 << _IOC_DIRBITS)-1) 25 /* 各个域的偏移 */ 26 #define _IOC_NRSHIFT 0 27 #define _IOC_TYPESHIFT (_IOC_NRSHIFT + _IOC_NRBITS) /* 8 */ 28 #define _IOC_SIZESHIFT (_IOC_TYPESHIFT + _IOC_TYPEBITS) /* 16 */ 29 #define _IOC_DIRSHIFT (_IOC_SIZESHIFT + _IOC_SIZEBITS) /* 29 */ 30 /* 读写域的值 */ 31 #define _IOC_NONE 1U 32 #define _IOC_READ 2U #Define _IOC_WRITE 4U 33 is 34 is 35 #define _IOC (the dir, type, NR, size) \ 36 (((the dir) << _IOC_DIRSHIFT) | \ / * * read direction to the left 29bit / 37 [((type) << _IOC_TYPESHIFT ) | \ / * left magic number * 8bit / 38 is ((NR) << _IOC_NRSHIFT) | \ / * instruction number * / 39 ((size) << _IOC_SIZESHIFT)) / * * size parameter left 16bit / 40 / * macro prototype, this data type will be passed whichever length * / 41 is #define _IO (type, NR) _IOC (_IOC_NONE, (type), (NR), 0) 42 is #define _IOR (type, NR, size ) _IOC (_IOC_READ, (type), (NR), the sizeof (size)) 43 is #define _IOW (type, NR, size) _IOC (_IOC_WRITE, (type), (NR), the sizeof (size)) 44 is #define _IOWR (of the type, nr, size) _IOC (_IOC_READ | _IOC_WRITE, (of the type), (nr), sizeof (size)) 45 / * get the value of each domain * / 46 #define _IOC_DIR(nr) (((nr) >> _IOC_DIRSHIFT) & _IOC_DIRMASK) 47 #define _IOC_TYPE(nr) (((nr) >> _IOC_TYPESHIFT) & _IOC_TYPEMASK) 48 #define _IOC_NR(nr) (((nr) >> _IOC_NRSHIFT) & _IOC_NRMASK) 49 #define _IOC_SIZE(nr) (((nr) >> _IOC_SIZESHIFT) & _IOC_SIZEMASK)
Here particular explain variable _IO macro that no transmission, only for transmitting commands. This is because the variable data variable needed, just as a command (such as reset) used, there is no need to determine whether the data on the device, so the device driver does not need to perform processing related to the file. V4l2 used in the examples are as follows:
#define VIDIOC_QUERYCAP _IOR('V', 0, struct v4l2_capability) #define VIDIOC_RESERVED _IO('V', 1) #define VIDIOC_S_FMT _IOWR('V', 5, struct v4l2_format) #define VIDIOC_STREAMON _IOW('V', 18, int)
In the above-described process v4l2 macro commands in video_ioctl2 function:
1 static unsigned long cmd_input_size(unsigned int cmd) 2 { 3 #define CMDINSIZE(cmd, type, field) \ 4 case VIDIOC_##cmd: \ 5 return offsetof(struct v4l2_##type, field) + \ /* 域的偏移 */ 6 sizeof(((struct v4l2_##type *)0)->field); /* 域的长度 */ 7 8 switch (cmd) { 9 CMDINSIZE(ENUM_FMT, fmtdesc, type); 10 CMDINSIZE(G_FMT, format, type); 11 ... 12 CMDINSIZE(ENUM_FRAMESIZES, frmsizeenum, pixel_format); 13 CMDINSIZE(ENUM_FRAMEINTERVALS, frmivalenum, height); 14 default: 15 return _IOC_SIZE (cmd); / * all copies of the rest of the command is the need * / 16} . 17} 18 is . 19 Long video_ioctl2 (struct * File File, unsigned int cmd, unsigned Long Arg) 20 is { 21 is SBUF char [128] ; / * allocate 128 bytes of stack space for storing command parameters * / 22 is the mbuf void * = NULL; 23 is PARG void * = NULL; / * parameter stored in the first address * / 24 = ERR Long -EINVAL; int is_ext_ctrl 25; 26 is size_t ctrls_size = 0; 27 * user_ptr void __user = NULL; 28 29 ... 30 / * read and write commands comprises determining whether, if the parameter value is then copied to the user space kernel * / 31 is IF ( _IOC_DIR (cmd)! = _IOC_NONE) { 32 / * size is determined whether the parameter exceeds 128 bytes * / 33 is iF (_IOC_SIZE (cmd) <= the sizeof (SBUF)) { PARG SBUF = 34 is; 35 the else {} 36 / * from the stack if the application exceeds 128 bytes * / 37 [kmalloc the mbuf = (_IOC_SIZE (cmd), GFP_KERNEL); 38 is IF (the mbuf == NULL) 39 return -ENOMEM; PARG the mbuf = 40; 41 is} 42 is 43 is ERR = -EFAULT; 44 is / * If the write command contains * / 45 IF (_IOC_DIR (cmd) & _IOC_WRITE) { 46 is / * calculate the required length of copying data is valid, some commands do not need all copies * / 47 unsigned Long n-= cmd_input_size (cmd); 48 / * from user space copy parameter value * / 49 IF (copy_from_user (PARG, (void __user *) Arg, n-)) 50 GOTO OUT; 51 is 52 is / * the rest of the space is cleared * / IF 53 is (n-<_IOC_SIZE (cmd)) 54 is Memset ((U8 *) + n-PARG, 0, _IOC_SIZE (cmd) - n-); 55 the else {} 56 is / * If the command is read then the entire buffer is cleared * / 57 is Memset (PARG, 0, _IOC_SIZE (cmd)); 58} 59} 60 61 is ... 62 is / * invoke the function member v4l2_ioctl_ops processing command * / 63 is __video_do_ioctl ERR = (File, cmd, PARG); 64 IF ( == -ENOIOCTLCMD ERR) 65 = ERR -EINVAL; 66 ... 67 IF (ERR <0) 68 GOTO OUT; 69 70 out_ext_ctrl: 71 is / * If the read command contains the parameter value will be copied to the user space * / 72 Switch (_IOC_DIR (cmd)) { 73 is _IOC_READ Case: 74 case (_IOC_WRITE | _IOC_READ): 75 if (copy_to_user((void __user *)arg, parg, _IOC_SIZE(cmd))) 76 err = -EFAULT; 77 break; 78 } 79 80 out: 81 kfree(mbuf); 82 return err; 83 } 84 EXPORT_SYMBOL(video_ioctl2);
Then we set video_ioctl2 in ioctl struct members can be in the v4l2_file_operations