这篇文章详细分析了V4L2用户空间和kernel层driver的交互过程,目的只有一个:
更清晰的理解V4L2视频驱动程序的系统结构,驱动编程方法,为以后开发视频驱动打好基础
V4L2用户空间和kernel层driver的交互过程_V4L2
既然从用户层出发探究驱动层,这里先贴出应用层code:
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <assert.h>
  5. #include <getopt.h> 
  6. #include <fcntl.h> 
  7. #include <unistd.h>
  8. #include <errno.h>
  9. #include <sys/stat.h>
  10. #include <sys/types.h>
  11. #include <sys/time.h>
  12. #include <sys/mman.h>
  13. #include <sys/ioctl.h>
  14. #include <asm/types.h>
  15. #include <linux/videodev2.h>
  16. #include <linux/fb.h>
  17. #define CLEAR(x) memset (&(x), 0, sizeof (x))
  18.  
  19. struct buffer {
  20.     void * start;
  21.     size_t length;
  22. };
  23.  
  24. static char * dev_name = NULL;
  25. static int fd = -1;
  26. struct buffer * buffers = NULL;
  27. static unsigned int n_buffers = 0;
  28. static int time_in_sec_capture=5;
  29. static int fbfd = -1;
  30. static struct fb_var_screeninfo vinfo;
  31. static struct fb_fix_screeninfo finfo;
  32. static char *fbp=NULL;
  33. static long screensize=0;
  34.  
  35. static void errno_exit (const char * s)
  36. {
  37.     fprintf (stderr, "%s error %d, %s/n",s, errno, strerror (errno));
  38.     exit (EXIT_FAILURE);
  39. }
  40.  
  41. static int xioctl (int fd,int request,void * arg)
  42. {
  43.     int r;
  44.     /* Here use this method to make sure cmd success*/
  45.     do r = ioctl (fd, request, arg);
  46.     while (-1 == r && EINTR == errno);
  47.     return r;
  48. }
  49.  
  50. inline int clip(int value, int min, int max) {
  51.     return (value > max ? max : value < min ? min : value);
  52.   }
  53.  
  54. static void process_image (const void * p){
  55.     //ConvertYUVToRGB321;
  56.     unsigned char* in=(char*)p;
  57.     int width=640;
  58.     int height=480;
  59.     int istride=1280;
  60.     int x,y,j;
  61.     int y0,u,y1,v,r,g,b;
  62.     long location=0;
  63.  
  64.     for ( y = 100; y < height + 100; ++y) {
  65.         for (j = 0, x=100; j < width * 2 ; j += 4,x +=2) {
  66.           location = (x+vinfo.xoffset) * (vinfo.bits_per_pixel/8) +
  67.             (y+vinfo.yoffset) * finfo.line_length;
  68.             
  69.           y0 = in[j];
  70.           u = in[j + 1] - 128; 
  71.           y1 = in[j + 2]; 
  72.           v = in[j + 3] - 128; 
  73.  
  74.           r = (298 * y0 + 409 * v + 128) >> 8;
  75.           g = (298 * y0 - 100 * u - 208 * v + 128) >> 8;
  76.           b = (298 * y0 + 516 * u + 128) >> 8;
  77.         
  78.           fbp[ location + 0] = clip(b, 0, 255);
  79.           fbp[ location + 1] = clip(g, 0, 255);
  80.           fbp[ location + 2] = clip(r, 0, 255); 
  81.           fbp[ location + 3] = 255; 
  82.  
  83.           r = (298 * y1 + 409 * v + 128) >> 8;
  84.           g = (298 * y1 - 100 * u - 208 * v + 128) >> 8;
  85.           b = (298 * y1 + 516 * u + 128) >> 8;
  86.  
  87.           fbp[ location + 4] = clip(b, 0, 255);
  88.           fbp[ location + 5] = clip(g, 0, 255);
  89.           fbp[ location + 6] = clip(r, 0, 255); 
  90.           fbp[ location + 7] = 255; 
  91.           }
  92.         in +=istride;
  93.       }
  94. }
  95.  
  96. static int read_frame (void)
  97. {
  98.     struct v4l2_buffer buf;
  99.     unsigned int i;
  100.  
  101.     CLEAR (buf);
  102.     buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  103.     buf.memory = V4L2_MEMORY_MMAP;
  104.      /* 11. VIDIOC_DQBUF把数据放回缓存队列*/
  105.     if (-1 == xioctl (fd, VIDIOC_DQBUF, &buf)) {
  106.         switch (errno) {
  107.         case EAGAIN:
  108.         return 0;
  109.         case EIO: 
  110.         default:
  111.             errno_exit ("VIDIOC_DQBUF");
  112.         }
  113.     }
  114.  
  115.     assert (buf.index < n_buffers);
  116.     printf("v4l2_pix_format->field(%d)/n", buf.field);
  117.     //assert (buf.field ==V4L2_FIELD_NONE);
  118.     process_image (buffers[buf.index].start);
  119.  
  120.     /*12. VIDIOC_QBUF把数据从缓存中读取出来*/
  121.     if (-1 == xioctl (fd, VIDIOC_QBUF, &buf))
  122.         errno_exit ("VIDIOC_QBUF");
  123.  
  124.     return 1;
  125. }
  126.  
  127. static void run (void)
  128. {
  129.     unsigned int count;
  130.     int frames;
  131.     frames = 30 * time_in_sec_capture;
  132.  
  133.     while (frames-- > 0) {
  134.         for (;;) {
  135.             fd_set fds;
  136.             struct timeval tv;
  137.             int r;
  138.             FD_ZERO (&fds);
  139.             FD_SET (fd, &fds);
  140.  
  141.             
  142.             tv.tv_sec = 2;
  143.             tv.tv_usec = 0;
  144.              /* 10. poll method*/
  145.             r = select (fd + 1, &fds, NULL, NULL, &tv);
  146.  
  147.             if (-1 == r) {
  148.                 if (EINTR == errno)
  149.                     continue;
  150.                 errno_exit ("select");
  151.             }
  152.  
  153.             if (0 == r) {
  154.                 fprintf (stderr, "select timeout/n");
  155.                 exit (EXIT_FAILURE);
  156.             }
  157.  
  158.             if (read_frame())
  159.                 break;
  160.             
  161.             }
  162.     }
  163. }
  164.  
  165. static void stop_capturing (void)
  166. {
  167.     enum v4l2_buf_type type;
  168.  
  169.     type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  170.     /*13. VIDIOC_STREAMOFF结束视频显示函数*/
  171.     if (-1 == xioctl (fd, VIDIOC_STREAMOFF, &type))
  172.         errno_exit ("VIDIOC_STREAMOFF");
  173. }
  174.  
  175. static void start_capturing (void)
  176. {
  177.     unsigned int i;
  178.     enum v4l2_buf_type type;
  179.  
  180.     for (i = 0; i < n_buffers; ++i) {
  181.         struct v4l2_buffer buf;
  182.         CLEAR (buf);
  183.  
  184.         buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  185.         buf.memory = V4L2_MEMORY_MMAP;
  186.         buf.index = i;
  187.          /* 8. VIDIOC_QBUF把数据从缓存中读取出来*/
  188.         if (-1 == xioctl (fd, VIDIOC_QBUF, &buf))
  189.             errno_exit ("VIDIOC_QBUF");
  190.     }
  191.  
  192.     type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  193.      /* 9. VIDIOC_STREAMON开始视频显示函数*/
  194.     if (-1 == xioctl (fd, VIDIOC_STREAMON, &type))
  195.         errno_exit ("VIDIOC_STREAMON");
  196.     
  197. }
  198.  
  199. static void uninit_device (void)
  200. {
  201.     unsigned int i;
  202.  
  203.     for (i = 0; i < n_buffers; ++i)
  204.         if (-1 == munmap (buffers[i].start, buffers[i].length))
  205.             errno_exit ("munmap");
  206.     
  207.     if (-1 == munmap(fbp, screensize)) {
  208.           printf(" Error: framebuffer device munmap() failed./n");
  209.           exit (EXIT_FAILURE) ;
  210.         } 
  211.     free (buffers);
  212. }
  213.  
  214.  
  215. static void init_mmap (void)
  216. {
  217.     struct v4l2_requestbuffers req;
  218.  
  219.     //mmap framebuffer
  220.     fbp = (char *)mmap(NULL,screensize,PROT_READ | PROT_WRITE,MAP_SHARED ,fbfd, 0);
  221.     if ((int)fbp == -1) {
  222.         printf("Error: failed to map framebuffer device to memory./n");
  223.         exit (EXIT_FAILURE) ;
  224.     }
  225.     memset(fbp, 0, screensize);
  226.     CLEAR (req);
  227.  
  228.     req.count = 4;
  229.     req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  230.     req.memory = V4L2_MEMORY_MMAP;
  231.      /* 6. VIDIOC_REQBUFS分配内存*/
  232.     if (-1 == xioctl (fd, VIDIOC_REQBUFS, &req)) {
  233.         if (EINVAL == errno) {
  234.             fprintf (stderr, "%s does not support memory mapping/n", dev_name);
  235.             exit (EXIT_FAILURE);
  236.         } else {
  237.             errno_exit ("VIDIOC_REQBUFS");
  238.         }
  239.     }
  240.  
  241.     if (req.count < 4) {
  242.         fprintf (stderr, "Insufficient buffer memory on %s/n",dev_name);
  243.         exit (EXIT_FAILURE);
  244.     }
  245.  
  246.     buffers = calloc (req.count, sizeof (*buffers));
  247.  
  248.     if (!buffers) {
  249.         fprintf (stderr, "Out of memory/n");
  250.         exit (EXIT_FAILURE);
  251.     }
  252.  
  253.     for (n_buffers = 0; n_buffers < req.count; ++n_buffers) {
  254.         struct v4l2_buffer buf;
  255.  
  256.         CLEAR (buf);
  257.  
  258.         buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  259.         buf.memory = V4L2_MEMORY_MMAP;
  260.         buf.index = n_buffers;
  261.          /* 7. VIDIOC_QUERYBUF把VIDIOC_REQBUFS中分配的数据缓存转换成物理地址*/
  262.         if (-1 == xioctl (fd, VIDIOC_QUERYBUF, &buf))
  263.             errno_exit ("VIDIOC_QUERYBUF");
  264.  
  265.         buffers[n_buffers].length = buf.length;
  266.         buffers[n_buffers].start =mmap (NULL,buf.length,PROT_READ | PROT_WRITE ,MAP_SHARED,fd, buf.m.offset);
  267.  
  268.         if (MAP_FAILED == buffers[n_buffers].start)
  269.             errno_exit ("mmap");
  270.     }
  271.  
  272. }
  273.  
  274.  
  275.  
  276. static void init_device (void)
  277. {
  278.     struct v4l2_capability cap;
  279.     struct v4l2_cropcap cropcap;
  280.     struct v4l2_crop crop;
  281.     struct v4l2_format fmt;
  282.     unsigned int min;
  283.  
  284.  
  285.     // Get fixed screen information
  286.     if (-1==xioctl(fbfd, FBIOGET_FSCREENINFO, &finfo)) {
  287.      printf("Error reading fixed information./n");
  288.      exit (EXIT_FAILURE);
  289.     }
  290.  
  291.     // Get variable screen information
  292.     if (-1==xioctl(fbfd, FBIOGET_VSCREENINFO, &vinfo)) {
  293.      printf("Error reading variable information./n");
  294.      exit (EXIT_FAILURE);
  295.     }
  296.     screensize = vinfo.xres * vinfo.yres * vinfo.bits_per_pixel / 8;
  297.  
  298.      /* 2. VIDIOC_QUERYCAP查询驱动功能*/
  299.     if (-1 == xioctl (fd, VIDIOC_QUERYCAP, &cap)) {
  300.         if (EINVAL == errno) {
  301.             fprintf (stderr, "%s is no V4L2 device/n",dev_name);
  302.             exit (EXIT_FAILURE);
  303.         } else {
  304.             errno_exit ("VIDIOC_QUERYCAP");
  305.         }
  306.     }
  307.  
  308.     /* Check if it is a video capture device*/
  309.     if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) {
  310.         fprintf (stderr, "%s is no video capture device/n",dev_name);
  311.         exit (EXIT_FAILURE);
  312.     }
  313.  
  314.      /* Check if support streaming I/O ioctls*/
  315.     if (!(cap.capabilities & V4L2_CAP_STREAMING)) {
  316.         fprintf (stderr, "%s does not support streaming i/o/n",dev_name);
  317.         exit (EXIT_FAILURE);
  318.     }
  319.  
  320.     CLEAR (cropcap);
  321.     /* Set type*/
  322.     cropcap.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  323.      /* 3. VIDIOC_CROPCAP查询驱动的修剪能力*/
  324.     /* 这里在vivi驱动中我们没有实现此方法,即不支持此操作*/
  325.     if (0 == xioctl (fd, VIDIOC_CROPCAP, &cropcap)) {
  326.         crop.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  327.         crop.c = cropcap.defrect;
  328.         /* 4. VIDIOC_S_CROP设置视频信号的边框*/
  329.         /* 同样不支持这个操作*/
  330.         if (-1 == xioctl (fd, VIDIOC_S_CROP, &crop)) {
  331.             switch (errno) {
  332.             case EINVAL: 
  333.             break;
  334.             default:
  335.             break;
  336.             }
  337.         }
  338.     }else { }
  339.  
  340.     CLEAR (fmt);
  341.  
  342.     fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  343.     fmt.fmt.pix.width = 640; 
  344.     fmt.fmt.pix.height = 480;
  345.     fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
  346.     fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
  347.      /* 5. VIDIOC_S_FMT设置当前驱动的频捕获格式*/
  348.     if (-1 == xioctl (fd, VIDIOC_S_FMT, &fmt))
  349.         errno_exit ("VIDIOC_S_FMT");
  350.  
  351.     init_mmap ();
  352.  
  353. }
  354.  
  355. static void close_device (void)
  356. {
  357.     if (-1 == close (fd))
  358.     errno_exit ("close");
  359.     fd = -1;
  360.     /*14. close method*/
  361.     close(fbfd);
  362. }
  363.  
  364. static void open_device (void)
  365. {
  366.     struct stat st; 
  367.  
  368.     if (-1 == stat (dev_name, &st)) {
  369.      fprintf (stderr, "Cannot identify '%s': %d, %s/n",dev_name, errno, strerror (errno));
  370.      exit (EXIT_FAILURE);
  371.     }
  372.  
  373.     if (!S_ISCHR (st.st_mode)) {
  374.      fprintf (stderr, "%s is no device/n", dev_name);
  375.      exit (EXIT_FAILURE);
  376.     }
  377.  
  378.     fbfd = open("/dev/fb0", O_RDWR);
  379.     if (fbfd==-1) {
  380.         printf("Error: cannot open framebuffer device./n");
  381.         exit (EXIT_FAILURE);
  382.     }
  383.  
  384.     /* 1. open the char device */
  385.     fd = open (dev_name, O_RDWR| O_NONBLOCK, 0);
  386.     if (-1 == fd) {
  387.      fprintf (stderr, "Cannot open '%s': %d, %s/n",dev_name, errno, strerror (errno));
  388.      exit (EXIT_FAILURE);
  389.     }
  390. }
  391.  
  392. static void usage (FILE * fp,int argc,char ** argv)
  393. {
  394.     fprintf (fp,
  395.     "Usage: %s [options]/n/n"
  396.     "Options:/n"
  397.     "-d | --device name Video device name [/dev/video]/n"
  398.     "-h | --help Print this message/n"
  399.     "-t | --how long will display in seconds/n"
  400.     "",
  401.     argv[0]);
  402. }
  403.  
  404. static const char short_options [] = "d:ht:";
  405. static const struct option long_options [] = {
  406.     { "device", required_argument, NULL, 'd' },
  407.     { "help", no_argument, NULL, 'h' },
  408.     { "time", no_argument, NULL, 't' },
  409.     { 0, 0, 0, 0 }
  410. };
  411.  
  412. int main (int argc,char ** argv)
  413. {
  414.     dev_name = "/dev/video0";
  415.     for (;;) 
  416.     {
  417.         int index;
  418.         int c;
  419.  
  420.         c = getopt_long (argc, argv,short_options, long_options,&index);
  421.         if (-1 == c)
  422.         break;
  423.  
  424.         switch (c) {
  425.         case 0:
  426.         break;
  427.  
  428.         case 'd':
  429.         dev_name = optarg;
  430.         break;
  431.  
  432.         case 'h':
  433.         usage (stdout, argc, argv);
  434.         exit (EXIT_SUCCESS);
  435.         case 't':
  436.         time_in_sec_capture = atoi(optarg);
  437.         break;
  438.  
  439.         default:
  440.         usage (stderr, argc, argv);
  441.         exit (EXIT_FAILURE);
  442.         }
  443.     }
  444.  
  445.     open_device();
  446.     init_device();
  447.     start_capturing();
  448.     run();
  449.     stop_capturing();
  450.     uninit_device();
  451.     close_device();
  452.     exit(EXIT_SUCCESS);
  453.     return 0;
  454. }
上面code中我已经标注出程序顺序指向的步骤1--14步,下面将一一说明应用从做这14步时驱动层是怎样响应,变化过程, 驱动加载初始化部分上一篇文章已经说过了
正式开始取经之路哇。。。。 V4L2用户空间和kernel层driver的交互过程_V4L2_02V4L2用户空间和kernel层driver的交互过程_V4L2。。。

STEP 1:
fd = open (dev_name, O_RDWR| O_NONBLOCK, 0);
打开字符设备,这个字符设备是video_device_register时创建的,code在v4l2_dev.c中,具体:
  1. static int v4l2_open(struct inode *inode, struct file *filp)
  2. {
  3.     struct video_device *vdev;
  4.     int ret = 0;
  5.  
  6.     /* Check if the video device is available */
  7.     mutex_lock(&videodev_lock);
  8.     vdev = video_devdata(filp);
  9.     /* return ENODEV if the video device has already been removed. */
  10.     if (vdev == NULL || !video_is_registered(vdev)) {
  11.         mutex_unlock(&videodev_lock);
  12.         return -ENODEV;
  13.     }
  14.     /* and increase the device refcount */
  15.     video_get(vdev);
  16.     mutex_unlock(&videodev_lock);
  17.  
  18.     /* 
  19.     * Here using the API you get the method you get the open() method write
  20.     * The other methods in fops use the same method to use you own code 
  21.     */
  22.     if (vdev->fops->open) {
  23.         if (vdev->lock && mutex_lock_interruptible(vdev->lock)) {
  24.             ret = -ERESTARTSYS;
  25.             goto err;
  26.         }
  27.         if (video_is_registered(vdev))
  28.             ret = vdev->fops->open(filp);
  29.         else
  30.             ret = -ENODEV;
  31.         if (vdev->lock)
  32.             mutex_unlock(vdev->lock);
  33.     }
  34.  
  35. err:
  36.     /* decrease the refcount in case of an error */
  37.     if (ret)
  38.         video_put(vdev);
  39.     return ret;
  40. }
重点在标注部分,通过这个V4L2的API调用我们自己驱动程序中定义的open方法,我们自己的open方法所属的fops是在vivi.c驱动程序的vivi_create_instance方法中 video_device_register之前关联进来的

  1. int v4l2_fh_open(struct file *filp)
  2. {
  3.     struct video_device *vdev = video_devdata(filp);
  4.     struct v4l2_fh *fh = kzalloc(sizeof(*fh), GFP_KERNEL);
  5.  
  6.     /*
  7.     * IN the open method, do only one job
  8.     * set v4l2_fh into filp->private_data for later use, and initial v4l2_fh
  9.     */
  10.     filp->private_data = fh;
  11.     if (fh == NULL)
  12.         return -ENOMEM;
  13.     v4l2_fh_init(fh, vdev);
  14.     v4l2_fh_add(fh);
  15.     return 0;
  16. }
  17. EXPORT_SYMBOL_GPL(v4l2_fh_open);
这个open方法只是初始化了一个v4l2_fh,并关联到filp->private中,方便以后使用
这里设置V4L2_FL_USES_V4L2_FH这个标志位,设置优先级为UNSET,如果我们的自己驱动程序实现了,支持
VIDIOC_SUBSCRIBE_EVENT,那么v4l2_event_init,在events初始化中初始化链表,并设置sequence为-1,如果不支持,则设置fh->events为NULL
最后add list

STEP 2:
if (-1 == xioctl (fd, VIDIOC_QUERYCAP, &cap))
这么调用完成下面过程,不行的从驱动层获取cap。直到成功拿到我们想要的数据
  1. static int xioctl (int fd,int request,void * arg)
  2. {
  3.     int r;
  4.     /* Here use this method to make sure cmd success*/
  5.     do r = ioctl (fd, request, arg);
  6.     while (-1 == r && EINTR == errno);
  7.     return r;
  8. }
也就是调用驱动层的ioctl方法,从v4l2 api中的ictol 调用我们自己定义的ioctl ,这中间的过程不在多做说明,我们自己的驱动的控制过程由v4l2_ioctl.c这个文件中的方法实现,一个很庞大的switch
值得一提的是,慢慢后面你会明白的,这里 v4l2_ioctl.c这个文件中的方法实现其实只是会中转站,它接着就回调了我们自己驱动程序中定义的控制接口,后面再说吧
  1. long video_ioctl2(struct file *file,
  2.      unsigned int cmd, unsigned long arg)
  3. {
  4.     return video_usercopy(file, cmd, arg, __video_do_ioctl);
  5. }
这里这个 __video_do_ioctl 方法其实完全做了我们所有的控制过程,又为什么又要经过video_usercopy这个方法呢,不妨看一看这个方法
  1. long
  2. video_usercopy(struct file *file, unsigned int cmd, unsigned long arg,
  3.      v4l2_kioctl func)
  4. {
  5.     char    sbuf[128];
  6.     void *mbuf = NULL;
  7.     void    *parg = (void *)arg;
  8.     long    err = -EINVAL;
  9.     bool    has_array_args;
  10.     size_t array_size = 0;
  11.     void __user *user_ptr = NULL;
  12.     void    **kernel_ptr = NULL;
  13.  
  14.     /* Copy arguments into temp kernel buffer */
  15.     if (_IOC_DIR(cmd) != _IOC_NONE) {
  16. ........这里检查128个字节的大小是否够存放用户端发送来的数据,不够则需要重新申请一个新的内存用来存放,指向parg这个地址
  17.         if (_IOC_SIZE(cmd) <= sizeof(sbuf)) {
  18.             parg = sbuf;
  19.         } else {
  20.             /* too big to allocate from stack */
  21.             mbuf = kmalloc(_IOC_SIZE(cmd), GFP_KERNEL);
  22.             if (NULL == mbuf)
  23.                 return -ENOMEM;
  24.             parg = mbuf;
  25.         }
  26.  
  27.         err = -EFAULT;
  28.         if (_IOC_DIR(cmd) & _IOC_WRITE) {
  29.             unsigned long n = cmd_input_size(cmd);
  30.  
  31.             if (copy_from_user(parg, (void __user *)arg, n))
  32.                 goto out;
  33.  
  34.             /* zero out anything we don't copy from userspace */
  35.             if (n < _IOC_SIZE(cmd))
  36.                 memset((u8 *)parg + n, 0, _IOC_SIZE(cmd) - n);
  37.         } else {
  38.             /* read-only ioctl */
  39.             memset(parg, 0, _IOC_SIZE(cmd));
  40.         }
  41.     }
  42. ....check
  43.     err = check_array_args(cmd, parg, &array_size, &user_ptr, &kernel_ptr);
  44.     if (err < 0)
  45.         goto out;
  46.     has_array_args = err;
  47. ....这里这块如果用户端有数据写到kernel,这里负责数据拷贝
  48.     if (has_array_args) {
  49.         /*
  50.          * When adding new types of array args, make sure that the
  51.          * parent argument to ioctl (which contains the pointer to the
  52.          * array) fits into sbuf (so that mbuf will still remain
  53.          * unused up to here).
  54.          */
  55.         mbuf = kmalloc(array_size, GFP_KERNEL);
  56.         err = -ENOMEM;
  57.         if (NULL == mbuf)
  58.             goto out_array_args;
  59.         err = -EFAULT;
  60.         if (copy_from_user(mbuf, user_ptr, array_size))
  61.             goto out_array_args;
  62.         *kernel_ptr = mbuf;
  63.     }
  64.  
  65.     /* Handles IOCTL */
  66.     err = func(file, cmd, parg);
  67.     if (err == -ENOIOCTLCMD)
  68.         err = -EINVAL;
  1.     if (has_array_args) {
  2.         *kernel_ptr = user_ptr;
  3.         if (copy_to_user(user_ptr, mbuf, array_size))
  4.             err = -EFAULT;
  5.         goto out_array_args;
  6.     }
  7.     if (err < 0)
  8.         goto out;
  9.  
  10. out_array_args:
  11.     /* Copy results into user buffer */
  12.     switch (_IOC_DIR(cmd)) {
  13.     case _IOC_READ:
  14.     case (_IOC_WRITE | _IOC_READ):
  15.         if (copy_to_user((void __user *)arg, parg, _IOC_SIZE(cmd)))
  16.             err = -EFAULT;
  17.         break;
  18.     }
  19.  
  20. out:
  21.     kfree(mbuf);
  22.     return err;
  23. }
  24. EXPORT_SYMBOL(video_usercopy);
自我感觉这个方法还是有很多精妙之处的,主要的控制过程是在我标注的地方调用完成的,这个调用之前做check动作,检查用户端发来的命令是否合法,
最重要的是把用户端的数据copy到kernel 端;而这个调用之后,则是我们处理完我们的动作之后,我们在这里吧用户端请求的数据从kernel 端copy到用户端
这样做的好处是显而易见的,任务明确,控制只做控制,用户空间和kernel空间数据的copy在所有控制之前,控制之后进行
以上动作做完之后,进入庞大的控制中枢,这来开始至贴出具体到某一个控制的代码,否则code过大,不易分析:

  1. case VIDIOC_QUERYCAP://查询视频设备的功能
  2.     {
  3.         struct v4l2_capability *cap = (struct v4l2_capability *)arg;
  4.  
  5.         if (!ops->vidioc_querycap)
  6.             break;
  7.  
  8.         ret = ops->vidioc_querycap(file, fh, cap);
  9.         if (!ret)/* i don't think here need to check */
  10.             dbgarg(cmd, "driver=%s, card=%s, bus=%s, "
  11.                     "version=0x%08x, "
  12.                     "capabilities=0x%08x\n",
  13.                     cap->driver, cap->card, cap->bus_info,
  14.                     cap->version,
  15.                     cap->capabilities);
  16.         break;
  17.     }
这来调用了我们自己驱动中填充的v4l2_ioctl_ops结构体,从这里开始,我上面说到的话得到了验证,这就是linux 中API 的强大之处
作为中间层的这个控制中枢又回调驱动自己定义编写的控制
  1. /* ------------------------------------------------------------------
  2.     IOCTL vidioc handling
  3.    ------------------------------------------------------------------*/
  4. static int vidioc_querycap(struct file *file, void *priv,
  5.                     struct v4l2_capability *cap)
  6. {
  7.     struct vivi_dev *dev = video_drvdata(file);
  8.  
  9.     strcpy(cap->driver, "vivi");
  10.     strcpy(cap->card, "vivi");
  11.     strlcpy(cap->bus_info, dev->v4l2_dev.name, sizeof(cap->bus_info));
  12.     cap->version = VIVI_VERSION;
  13.     cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING | \
  14.              V4L2_CAP_READWRITE;
  15.     return 0;
  16. }
这来做的事情很简单,只是将配置信息保存到cap这个变量中,之后上传给用户空间

STEP 3:
/* 3. VIDIOC_CROPCAP查询驱动的修剪能力*/
/* 这里在vivi 驱动中我们没有实现此方法,即不支持此操作*/
if (0 == xioctl (fd, VIDIOC_CROPCAP, &cropcap))
这个判断在中间层控制中枢中进行的,check到我们自己的驱动中没有这个控制功能的支持
所以这里的 STEP 4同样不会进行

STEP 5:
/* 5. VIDIOC_S_FMT设置当前驱动的频捕获格式*/
if (-1 == xioctl (fd, VIDIOC_S_FMT, &fmt))
对应到控制中心是这样的

  1. case VIDIOC_S_FMT:
  2.     {
  3.         struct v4l2_format *f = (struct v4l2_format *)arg;
  4.  
  5.         /* FIXME: Should be one dump per type */
  6.         dbgarg(cmd, "type=%s\n", prt_names(f->type, v4l2_type_names));
  7.  
  8.         switch (f->type) {
  9.         case V4L2_BUF_TYPE_VIDEO_CAPTURE:
  10.             CLEAR_AFTER_FIELD(f, fmt.pix);
  11.             v4l_print_pix_fmt(vfd, &f->fmt.pix);
  12.             if (ops->vidioc_s_fmt_vid_cap) {
  13.                 ret = ops->vidioc_s_fmt_vid_cap(file, fh, f);
  14.             } else if (ops->vidioc_s_fmt_vid_cap_mplane) {
  15.                 if (fmt_sp_to_mp(f, &f_copy))
  16.                     break;
  17.                 ret = ops->vidioc_s_fmt_vid_cap_mplane(file, fh,
  18.                                     &f_copy);
  19.                 if (ret)
  20.                     break;
  21.  
  22.                 if (f_copy.fmt.pix_mp.num_planes > 1) {
  23.                     /* Drivers shouldn't adjust from 1-plane
  24.                      * to more than 1-plane formats */
  25.                     ret = -EBUSY;
  26.                     WARN_ON(1);
  27.                     break;
  28.                 }
  29.  
  30.                 ret = fmt_mp_to_sp(&f_copy, f);
  31.             }
  32.             break;
  33.         case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
  34.             CLEAR_AFTER_FIELD(f, fmt.pix_mp);
  35.             v4l_print_pix_fmt_mplane(vfd, &f->fmt.pix_mp);
  36.             if (ops->vidioc_s_fmt_vid_cap_mplane) {
  37.                 ret = ops->vidioc_s_fmt_vid_cap_mplane(file,
  38.                                     fh, f);
  39.             } else if (ops->vidioc_s_fmt_vid_cap &&
  40.                     f->fmt.pix_mp.num_planes == 1) {
  41.                 if (fmt_mp_to_sp(f, &f_copy))
  42.                     break;
  43.                 ret = ops->vidioc_s_fmt_vid_cap(file,
  44.                                 fh, &f_copy);
  45.                 if (ret)
  46.                     break;
  47.  
  48.                 ret = fmt_sp_to_mp(&f_copy, f);
  49.             }
  50.             break;
  51.         case V4L2_BUF_TYPE_VIDEO_OVERLAY:
  52.             CLEAR_AFTER_FIELD(f, fmt.win);
  53.             if (ops->vidioc_s_fmt_vid_overlay)
  54.                 ret = ops->vidioc_s_fmt_vid_overlay(file,
  55.                                  fh, f);
  56.             break;
  57.         case V4L2_BUF_TYPE_VIDEO_OUTPUT:
  58.             CLEAR_AFTER_FIELD(f, fmt.pix);
  59.             v4l_print_pix_fmt(vfd, &f->fmt.pix);
  60.             if (ops->vidioc_s_fmt_vid_out) {
  61.                 ret = ops->vidioc_s_fmt_vid_out(file, fh, f);
  62.             } else if (ops->vidioc_s_fmt_vid_out_mplane) {
  63.                 if (fmt_sp_to_mp(f, &f_copy))
  64.                     break;
  65.                 ret = ops->vidioc_s_fmt_vid_out_mplane(file, fh,
  66.                                     &f_copy);
  67.                 if (ret)
  68.                     break;
  69.  
  70.                 if (f_copy.fmt.pix_mp.num_planes > 1) {
  71.                     /* Drivers shouldn't adjust from 1-plane
  72.                      * to more than 1-plane formats */
  73.                     ret = -EBUSY;
  74.                     WARN_ON(1);
  75.                     break;
  76.                 }
  77.  
  78.                 ret = fmt_mp_to_sp(&f_copy, f);
  79.             }
  80.             break;
  81.         case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
  82.             CLEAR_AFTER_FIELD(f, fmt.pix_mp);
  83.             v4l_print_pix_fmt_mplane(vfd, &f->fmt.pix_mp);
  84.             if (ops->vidioc_s_fmt_vid_out_mplane) {
  85.                 ret = ops->vidioc_s_fmt_vid_out_mplane(file,
  86.                                     fh, f);
  87.             } else if (ops->vidioc_s_fmt_vid_out &&
  88.                     f->fmt.pix_mp.num_planes == 1) {
  89.                 if (fmt_mp_to_sp(f, &f_copy))
  90.                     break;
  91.                 ret = ops->vidioc_s_fmt_vid_out(file,
  92.                                 fh, &f_copy);
  93.                 if (ret)
  94.                     break;
  95.  
  96.                 ret = fmt_mp_to_sp(&f_copy, f);
  97.             }
  98.             break;
  99.         case V4L2_BUF_TYPE_VIDEO_OUTPUT_OVERLAY:
  100.             CLEAR_AFTER_FIELD(f, fmt.win);
  101.             if (ops->vidioc_s_fmt_vid_out_overlay)
  102.                 ret = ops->vidioc_s_fmt_vid_out_overlay(file,
  103.                     fh, f);
  104.             break;
  105.         case V4L2_BUF_TYPE_VBI_CAPTURE:
  106.             CLEAR_AFTER_FIELD(f, fmt.vbi);
  107.             if (ops->vidioc_s_fmt_vbi_cap)
  108.                 ret = ops->vidioc_s_fmt_vbi_cap(file, fh, f);
  109.             break;
  110.         case V4L2_BUF_TYPE_VBI_OUTPUT:
  111.             CLEAR_AFTER_FIELD(f, fmt.vbi);
  112.             if (ops->vidioc_s_fmt_vbi_out)
  113.                 ret = ops->vidioc_s_fmt_vbi_out(file, fh, f);
  114.             break;
  115.         case V4L2_BUF_TYPE_SLICED_VBI_CAPTURE:
  116.             CLEAR_AFTER_FIELD(f, fmt.sliced);
  117.             if (ops->vidioc_s_fmt_sliced_vbi_cap)
  118.                 ret = ops->vidioc_s_fmt_sliced_vbi_cap(file,
  119.                                     fh, f);
  120.             break;
  121.         case V4L2_BUF_TYPE_SLICED_VBI_OUTPUT:
  122.             CLEAR_AFTER_FIELD(f, fmt.sliced);
  123.             if (ops->vidioc_s_fmt_sliced_vbi_out)
  124.                 ret = ops->vidioc_s_fmt_sliced_vbi_out(file,
  125.                                     fh, f);
  126.             break;
  127.         case V4L2_BUF_TYPE_PRIVATE:
  128.             /* CLEAR_AFTER_FIELD(f, fmt.raw_data); <- does nothing */
  129.             if (ops->vidioc_s_fmt_type_private)
  130.                 ret = ops->vidioc_s_fmt_type_private(file,
  131.                                 fh, f);
  132.             break;
  133.         }
  134.         break;
  135.     }
以后根据不同的type 决定了我们自己驱动程序中不同的控制实现,这个type是根据用户空间的设置而定的,还包括其他几个参数,如下:
  1. fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  2. fmt.fmt.pix.width = 640; 
  3. fmt.fmt.pix.height = 480;
  4. fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
  5. fmt.fmt.pix.field = V4L2_FIELD_INTERLACED;
这里根据设定的type,所以驱动程序的处理过程如下:

  1. static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
  2.                     struct v4l2_format *f)
  3. {
  4.     struct vivi_dev *dev = video_drvdata(file);
  5.     struct vb2_queue *q = &dev->vb_vidq;
  6. ....在下面这个函数中,做了一些试探性的动作,如果试探失败则下面不会赋值,试探通过则后续正常设置即可,在这个试探函数中同时做了一些设置动作
  7.     int ret = vidioc_try_fmt_vid_cap(file, priv, f);
  8.     if (ret < 0)
  9.         return ret;
  10.  
  11.     if (vb2_is_streaming(q)) {
  12.         dprintk(dev, 1, "%s device busy\n", __func__);
  13.         return -EBUSY;
  14.     }
  15. ....按用户空间需求设置
  16.     dev->fmt = get_format(f);
  17.     dev->width = f->fmt.pix.width;
  18.     dev->height = f->fmt.pix.height;
  19.     dev->field = f->fmt.pix.field;
  20.  
  21.     return 0;
  22. }

STEP6 :
/* 6. VIDIOC_REQBUFS分配内存*/
if (-1 == xioctl (fd, VIDIOC_REQBUFS, &req))
中间层控制中枢:
  1. case VIDIOC_REQBUFS:
  2.     {
  3.         struct v4l2_requestbuffers *p = arg;
  4.  
  5.         if (!ops->vidioc_reqbufs)
  6.             break;
  7. ........这个方法check 驱动必须实现了fmt方法,看具体看代码
  8.         ret = check_fmt(ops, p->type);
  9.         if (ret)
  10.             break;
  11.  
  12.         if (p->type < V4L2_BUF_TYPE_PRIVATE)
  13.             CLEAR_AFTER_FIELD(p, memory);
  14.  
  15.         ret = ops->vidioc_reqbufs(file, fh, p);
  16.         dbgarg(cmd, "count=%d, type=%s, memory=%s\n",
  17.                 p->count,
  18.                 prt_names(p->type, v4l2_type_names),
  19.                 prt_names(p->memory, v4l2_memory_names));
  20.         break;
  21.     }
驱动中实现:
  1. static int vidioc_reqbufs(struct file *file, void *priv,
  2.              struct v4l2_requestbuffers *p)
  3. {
  4.     struct vivi_dev *dev = video_drvdata(file);
  5.     return vb2_reqbufs(&dev->vb_vidq, p);
  6. }
到了这里来到了这个全新的话题,实现
vb2_reqbufs ( & dev - > vb_vidq ,  p ) ;
这里暂且不讨论这个方法,相对较复杂,待日后研究,先把注释部分放到这里,包括其他内存操作,之后深入研究补充,专门作为一篇整理
/**
 * Should be called from vidioc_reqbufs ioctl handler of a driver.
 * This function:
 * 1) verifies streaming parameters passed from the userspace,
 * 2) sets up the queue,
 * 3) negotiates number of buffers and planes per buffer with the driver to be used during streaming,
 * 4) allocates internal buffer structures (struct vb2_buffer), according to the agreed parameters,
 * 5) for MMAP memory type, allocates actual video memory, using the memory handling/allocation routines provided during queue initialization
 * If req->count is 0, all the memory will be freed instead.
 * If the queue has been allocated previously (by a previous vb2_reqbufs) call
 * and the queue is not busy, memory will be reallocated.
 * The return values from this function are intended to be directly returned from vidioc_reqbufs handler in driver.
 */

STEP 7:
/* 7. VIDIOC_QUERYBUF把VIDIOC_REQBUFS中分配的数据缓存转换成物理地址*/
if (-1 == xioctl (fd, VIDIOC_QUERYBUF, &buf))
中间层控制中枢:
  1. case VIDIOC_QUERYBUF:
  2.     {
  3.         struct v4l2_buffer *p = arg;
  4.  
  5.         if (!ops->vidioc_querybuf)
  6.             break;
  7.         ret = check_fmt(ops, p->type);
  8.         if (ret)
  9.             break;
  10.  
  11.         ret = ops->vidioc_querybuf(file, fh, p);
  12.         if (!ret)
  13.             dbgbuf(cmd, vfd, p);
  14.         break;
  15.     }
驱动中控制实现:
  1. static int vidioc_querybuf(struct file *file, void *priv, struct v4l2_buffer *p)
  2. {
  3.     struct vivi_dev *dev = video_drvdata(file);
  4.     return vb2_querybuf(&dev->vb_vidq, p);
  5. }
/**
 * Should be called from vidioc_querybuf ioctl handler in driver.
 * This function will verify the passed v4l2_buffer structure and fill the
 * relevant information for the userspace.
 * The return values from this function are intended to be directly returned from vidioc_querybuf handler in driver.
 */

STEP 8:
/* 8. VIDIOC_QBUF把数据从缓存中读取出来*/
if (-1 == xioctl (fd, VIDIOC_QBUF, &buf))
中间层控制中枢:
  1. case VIDIOC_QBUF:
  2.     {
  3.         struct v4l2_buffer *p = arg;
  4.  
  5.         if (!ops->vidioc_qbuf)
  6.             break;
  7.         ret = check_fmt(ops, p->type);
  8.         if (ret)
  9.             break;
  10.  
  11.         ret = ops->vidioc_qbuf(file, fh, p);
  12.         if (!ret)
  13.             dbgbuf(cmd, vfd, p);
  14.         break;
  15.     }
驱动中控制实现:

  1. static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
  2. {
  3.     struct vivi_dev *dev = video_drvdata(file);
  4.     return vb2_qbuf(&dev->vb_vidq, p);
  5. }
/**
 * Should be called from vidioc_qbuf ioctl handler of a driver.
 * This function:
 * 1) verifies the passed buffer,
 * 2) calls buf_prepare callback in the driver (if provided), in which driver-specific buffer initialization can be performed,
 * 3) if streaming is on, queues the buffer in driver by the means of buf_queue callback for processing.
 * The return values from this function are intended to be directly returned from vidioc_qbuf handler in driver.
 */

STEP 9:
/* 9. VIDIOC_STREAMON开始视频显示函数*/
if (-1 == xioctl (fd, VIDIOC_STREAMON, &type))
中间层控制中枢:
  1. case VIDIOC_STREAMON:
  2.     {
  3.         enum v4l2_buf_type i = *(int *)arg;
  4.  
  5.         if (!ops->vidioc_streamon)
  6.             break;
  7.         dbgarg(cmd, "type=%s\n", prt_names(i, v4l2_type_names));
  8.         ret = ops->vidioc_streamon(file, fh, i);
  9.         break;
  10.     }
驱动控制实现;

  1. static int vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
  2. {
  3.     struct vivi_dev *dev = video_drvdata(file);
  4.     return vb2_streamon(&dev->vb_vidq, i);
  5. }
/**
 * Should be called from vidioc_streamon handler of a driver.
 * This function:
 * 1) verifies current state
 * 2) starts streaming and passes any previously queued buffers to the driver
 * The return values from this function are intended to be directly returned from vidioc_streamon handler in the driver.
 */

STEP 10:
/* 10. poll method*/
select (fd + 1, &fds, NULL, NULL, &tv);
从V4L2驱动API开始:
  1. static unsigned int v4l2_poll(struct file *filp, struct poll_table_struct *poll)
  2. {
  3.     struct video_device *vdev = video_devdata(filp);
  4.     int ret = POLLERR | POLLHUP;
  5.  
  6.     if (!vdev->fops->poll)
  7.         return DEFAULT_POLLMASK;
  8.     if (vdev->lock)
  9.         mutex_lock(vdev->lock);
  10.     if (video_is_registered(vdev))
  11.         ret = vdev->fops->poll(filp, poll);
  12.     if (vdev->lock)
  13.         mutex_unlock(vdev->lock);
  14.     return ret;
  15. }
驱动实现:
  1. static unsigned int
  2. vivi_poll(struct file *file, struct poll_table_struct *wait)
  3. {
  4.     struct vivi_dev *dev = video_drvdata(file);
  5.     struct vb2_queue *q = &dev->vb_vidq;
  6.  
  7.     dprintk(dev, 1, "%s\n", __func__);
  8.     return vb2_poll(q, file, wait);
  9. }
/**
 * This function implements poll file operation handler for a driver.
 * For CAPTURE queues, if a buffer is ready to be dequeued, the userspace will be informed that the file descriptor of a video device is available for reading.
 * For OUTPUT queues, if a buffer is ready to be dequeued, the file descriptor will be reported as available for writing.
 * The return values from this function are intended to be directly returned from poll handler in driver.
 */

STEP 11:
/* 11. VIDIOC_DQBUF把数据放回缓存队列*/
if (-1 == xioctl (fd, VIDIOC_DQBUF, &buf))
中间层控制中枢:
  1. case VIDIOC_DQBUF:
  2.     {
  3.         struct v4l2_buffer *p = arg;
  4.  
  5.         if (!ops->vidioc_dqbuf)
  6.             break;
  7.         ret = check_fmt(ops, p->type);
  8.         if (ret)
  9.             break;
  10.  
  11.         ret = ops->vidioc_dqbuf(file, fh, p);
  12.         if (!ret)
  13.             dbgbuf(cmd, vfd, p);
  14.         break;
  15.     }
驱动控制实现:

  1. static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *p)
  2. {
  3.     struct vivi_dev *dev = video_drvdata(file);
  4.     return vb2_dqbuf(&dev->vb_vidq, p, file->f_flags & O_NONBLOCK);
  5. }
/**
 * Should be called from vidioc_dqbuf ioctl handler of a driver.
 * This function:
 * 1) verifies the passed buffer,
 * 2) calls buf_finish callback in the driver (if provided), in which driver can perform any additional operations that may be required before returning the buffer to userspace, such as cache sync,
 * 3) the buffer struct members are filled with relevant information for the userspace.
 * The return values from this function are intended to be directly returned from vidioc_dqbuf handler in driver.
 */

STEP 12:
/*12. VIDIOC_QBUF把数据从缓存中读取出来*/
if (-1 == xioctl (fd, VIDIOC_QBUF, &buf))
中间层控制中枢:
  1. case VIDIOC_QBUF:
  2.     {
  3.         struct v4l2_buffer *p = arg;
  4.  
  5.         if (!ops->vidioc_qbuf)
  6.             break;
  7.         ret = check_fmt(ops, p->type);
  8.         if (ret)
  9.             break;
  10.  
  11.         ret = ops->vidioc_qbuf(file, fh, p);
  12.         if (!ret)
  13.             dbgbuf(cmd, vfd, p);
  14.         break;
  15.     }
驱动控制实现:

  1. static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *p)
  2. {
  3.     struct vivi_dev *dev = video_drvdata(file);
  4.     return vb2_qbuf(&dev->vb_vidq, p);
  5. }

STEP 13:
/*13. VIDIOC_STREAMOFF结束视频显示函数*/
if (-1 == xioctl (fd, VIDIOC_STREAMOFF, &type))
中间层控制中枢:
  1. case VIDIOC_STREAMOFF:
  2.     {
  3.         enum v4l2_buf_type i = *(int *)arg;
  4.  
  5.         if (!ops->vidioc_streamoff)
  6.             break;
  7.         dbgarg(cmd, "type=%s\n", prt_names(i, v4l2_type_names));
  8.         ret = ops->vidioc_streamoff(file, fh, i);
  9.         break;
  10.     }
驱动控制实现:

  1. static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
  2. {
  3.     struct vivi_dev *dev = video_drvdata(file);
  4.     return vb2_streamoff(&dev->vb_vidq, i);
  5. }

STEP 13:
/*13. VIDIOC_STREAMOFF结束视频显示函数*/
if (-1 == xioctl (fd, VIDIOC_STREAMOFF, &type))
中间层控制中枢:
  1. case VIDIOC_STREAMOFF:
  2.     {
  3.         enum v4l2_buf_type i = *(int *)arg;
  4.  
  5.         if (!ops->vidioc_streamoff)
  6.             break;
  7.         dbgarg(cmd, "type=%s\n", prt_names(i, v4l2_type_names));
  8.         ret = ops->vidioc_streamoff(file, fh, i);
  9.         break;
  10.     }
驱动控制实现:

  1. static int vidioc_streamoff(struct file *file, void *priv, enum v4l2_buf_type i)
  2. {
  3.     struct vivi_dev *dev = video_drvdata(file);
  4.     return vb2_streamoff(&dev->vb_vidq, i);
  5. }

STEP 14:
/*14. close method*/
close(fbfd);

  1. static int v4l2_release(struct inode *inode, struct file *filp)
  2. {
  3.     struct video_device *vdev = video_devdata(filp);
  4.     int ret = 0;
  5.  
  6.     if (vdev->fops->release) {
  7.         if (vdev->lock)
  8.             mutex_lock(vdev->lock);
  9.         vdev->fops->release(filp);
  10.         if (vdev->lock)
  11.             mutex_unlock(vdev->lock);
  12.     }
  13.     /* decrease the refcount unconditionally since the release()
  14.      return value is ignored. */
  15.     video_put(vdev);
  16.     return ret;
  17. }

  1. static int vivi_close(struct file *file)
  2. {
  3.     struct video_device *vdev = video_devdata(file);
  4.     struct vivi_dev *dev = video_drvdata(file);
  5.  
  6.     dprintk(dev, 1, "close called (dev=%s), file %p\n",
  7.         video_device_node_name(vdev), file);
  8.  
  9.     if (v4l2_fh_is_singular_file(file))
  10.         vb2_queue_release(&dev->vb_vidq);
  11.     return v4l2_fh_release(file);
  12. }
到此为止,整个过程算是基本完结了,不过其中videobuf2_core.c 在我看来自己必须专门钻研一下了
videobuf2_core .c 是视频数据传输的核心
也可以说是视频驱动的重中之重
待续。。。。。。