USB攝像頭分析


1.USB攝像頭結構:
     


2.PU的作用:
     


3.怎么寫USB攝像頭驅動程序
    1.構造一個usb_driver
    2.設置
       probe:
           2.1. 分配video_device:video_device_alloc
           2.2. 設置
              .fops
              .ioctl_ops (里面需要設置11項)
              如果要用內核提供的緩沖區操作函數,還需要構造一個videobuf_queue_ops
          2.3. 注冊: video_register_device      
              id_table: 表示支持哪些USB設備      
    3.注冊: usb_register
UVC: USB Video Class
UVC驅動:drivers\media\video\uvc\


4.
uvc_driver.c分析:
   1. usb_register(&uvc_driver.driver);
   2. uvc_probe
          uvc_register_video
              vdev = video_device_alloc();
              vdev->fops = &uvc_fops;
              video_register_device
    3.在www.usb.org下載 uvc specification,
       UVC 1.5 Class specification.pdf : 有詳細描述
       USB_Video_Example 1.5.pdf    : 有示例
    4.
通過VideoControl Interface來控制,
通過VideoStreaming Interface來讀視頻數據,
VC里含有多個Unit/Terminal等功能模塊,可以通過訪問這些模塊進行控制,比如調亮度


5.分析UVC驅動調用過程:
  const struct v4l2_file_operations uvc_fops = {
.owner = THIS_MODULE,
.open = uvc_v4l2_open,
.release = uvc_v4l2_release,
.ioctl = uvc_v4l2_ioctl,
.read = uvc_v4l2_read,
.mmap = uvc_v4l2_mmap,
.poll = uvc_v4l2_poll,
  };
1. open:
        uvc_v4l2_open
2. VIDIOC_QUERYCAP   // video->streaming->type 應該是在設備被枚舉時分析描述符時設置的
if (video->streaming->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
cap->capabilities = V4L2_CAP_VIDEO_CAPTURE
 | V4L2_CAP_STREAMING;
else
cap->capabilities = V4L2_CAP_VIDEO_OUTPUT
 | V4L2_CAP_STREAMING;
3. VIDIOC_ENUM_FMT // format數組應是在設備被枚舉時設置的
        format = &video->streaming->format[fmt->index];
4. VIDIOC_G_FMT
        uvc_v4l2_get_format   // USB攝像頭支持多種格式fromat, 每種格式下有多種frame(比如分辨率)
            struct uvc_format *format = video->streaming->cur_format;
            struct uvc_frame *frame = video->streaming->cur_frame;
5. VIDIOC_TRY_FMT
        uvc_v4l2_try_format
            /* Check if the hardware supports the requested format. */
                  ..............................................
        /* Find the closest image size. The distance between image sizes is
        * the size in pixels of the non-overlapping regions between the
        * requested size and the frame-specified size.
        */
6. VIDIOC_S_FMT  // 只是把參數保存起來,還沒有發給USB攝像頭
        uvc_v4l2_set_format
            uvc_v4l2_try_format
        video->streaming->cur_format = format;
        video->streaming->cur_frame = frame;
7. VIDIOC_REQBUFS
        uvc_alloc_buffers
            for (; nbuffers > 0; --nbuffers) {
        mem = vmalloc_32(nbuffers * bufsize);
        if (mem != NULL)
        break;
        }
8. VIDIOC_QUERYBUF
        uvc_query_buffer
            __uvc_query_buffer
                memcpy(v4l2_buf, &buf->buf, sizeof *v4l2_buf);   // 復制參數
9. mmap
        uvc_v4l2_mmap
           
10. VIDIOC_QBUF
        uvc_queue_buffer
        list_add_tail(&buf->stream, &queue->mainqueue);
        list_add_tail(&buf->queue, &queue->irqqueue);
11. VIDIOC_STREAMON   /*開啟USB攝像頭*/
        uvc_video_enable(video, 1 )  // 把所設置的參數發給硬件,然后啟動攝像頭
            /* Commit the streaming parameters. */
            uvc_commit_video
                uvc_set_video_ctrl   /* 設置格式fromat, frame */
                    ret = __uvc_query_ctrl(video->dev /* 哪一個USB設備 */, SET_CUR, 0,
                    video->streaming->intfnum  /* 哪一個接口: VS */,
                    probe ? VS_PROBE_CONTROL : VS_COMMIT_CONTROL, data, size,
                    uvc_timeout_param);
               
            /* 啟動:Initialize isochronous/bulk URBs and allocate transfer buffers. */
            uvc_init_video(video, GFP_KERNEL);
                    uvc_init_video_isoc / uvc_init_video_bulk
                        urb->complete = uvc_video_complete; (收到數據后此函數被調用,它又調用video->decode(urb, video, buf); ==> uvc_video_decode_isoc/uvc_video_encode_bulk => uvc_queue_next_buffer => wake_up(&buf->wait);)
            
                    usb_submit_urb                    
12. poll
        uvc_v4l2_poll            
            uvc_queue_poll
                poll_wait(file, &buf->wait, wait);  // 休眠等待有數據
13. VIDIOC_DQBUF  /*刪除隊列中的內存*/
        uvc_dequeue_buffer
        list_del(&buf->stream);
14. VIDIOC_STREAMOFF        /*關閉攝像頭*/
        uvc_video_enable(video, 0);
    usb_kill_urb(urb);
    usb_free_urb(urb);


6.分析設置亮度過程:
ioctl: VIDIOC_S_CTRL
            uvc_ctrl_set
            uvc_ctrl_commit
                __uvc_ctrl_commit(video, 0);
                    uvc_ctrl_commit_entity(video->dev, entity, rollback);
                ret = uvc_query_ctrl(dev  /* 哪一個USB設備 */, SET_CUR, ctrl->entity->id  /* 哪一個unit/terminal */,
                dev->intfnum /* 哪一個接口: VC interface */, ctrl->info->selector,
                uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
                ctrl->info->size);


7.總結:
   1. UVC設備有2個interface: VideoControl Interface, VideoStreaming Interface
   2. VideoControl Interface用於控制,比如設置亮度。它內部有多個Unit/Terminal(在程序里Unit/Terminal都稱為entity)
      可以通過類似的函數來訪問:
                ret = uvc_query_ctrl(dev  /* 哪一個USB設備 */, SET_CUR, ctrl->entity->id  /* 哪一個unit/terminal */,
                dev->intfnum /* 哪一個接口: VC interface */, ctrl->info->selector,
                uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT),
                ctrl->info->size);
    3. VideoStreaming Interface用於獲得視頻數據,也可以用來選擇fromat/frame(VS可能有多種format, 一個format支持多種frame, frame用來表示分辨率等信息)
   可以通過類似的函數來訪問:
                    ret = __uvc_query_ctrl(video->dev /* 哪一個USB設備 */, SET_CUR, 0,
                    video->streaming->intfnum  /* 哪一個接口: VS */,
                    probe ? VS_PROBE_CONTROL : VS_COMMIT_CONTROL, data, size,
                    uvc_timeout_param);
   4. 我們在設置FORMAT時只是簡單的使用video->streaming->format[fmt->index]等數據, 這些數據哪來的?
      應是設備被枚舉時設置的,也就是分析它的描述符時設置的。

   5. UVC驅動的重點在於:
      描述符的分析
      屬性的控制: 通過VideoControl Interface來設置
      格式的選擇:通過VideoStreaming Interface來設置
      數據的獲得:通過VideoStreaming Interface的URB來獲得









免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM