Linux3.5—視屏模塊學習與分析

插入USB攝像頭後,我看到了識別出的一些信息,在內核源碼中搜到了相關信息:linux

搜索以後,在uvc_driver.c git

幫助文檔:linux-3.5/Documentation/video4linux/v4l2-framework.txt github

分析驅動程序最好的方法就是跟蹤應用程序對他的調用過程。ubuntu

 開始分析  /linux-3.5/drivers/media/video/uvc/uvc_driver.c 數組

 

  usb_register(&uvc_driver.driver);app

uvc_ids 指所能支持的設備。框架

 

下面查看probe函數:ide

/* USB探針、斷開鏈接、掛起和恢復*/函數

  

  (uvc_register_chains(dev)測試

    

        

    

 1 static int uvc_register_video(struct uvc_device *dev,
 2                 struct uvc_streaming *stream)
 3 {
 4         struct video_device *vdev;
 5         int ret;
 6 
 7         /* Initialize the streaming interface with default streaming
 8          * parameters.
 9          */
10         ret = uvc_video_init(stream);
11         if (ret < 0) {
12                 uvc_printk(KERN_ERR, "Failed to initialize the device "
13                         "(%d).\n", ret);
14                 return ret;
15         }
16 
17         uvc_debugfs_init_stream(stream);
18 ////////////////////////////標記///////////////////////////// 19 /* Register the device with V4L. */ 20 vdev = video_device_alloc(); 21 if (vdev == NULL) { 22 uvc_printk(KERN_ERR, "Failed to allocate video device (%d).\n", 23 ret); 24 return -ENOMEM; 25 } 26 27 /* We already hold a reference to dev->udev. The video device will be 28 * unregistered before the reference is released, so we don't need to 29 * get another one. 30 */ 31 vdev->v4l2_dev = &dev->vdev;
///////////////////////標記/////////////////////////////////
32 vdev->fops = &uvc_fops; 33 34 vdev->release = uvc_release; 35 strlcpy(vdev->name, dev->name, sizeof vdev->name); 36 37 /* Set the driver data before calling video_register_device, otherwise 38 * uvc_v4l2_open might race us. 39 */ 40 stream->vdev = vdev; 41 video_set_drvdata(vdev, stream); 42
/////////////////////////////////標記///////////////////////////////////////////////// 43 ret = video_register_device(vdev, VFL_TYPE_GRABBER, -1); 44 if (ret < 0) { 45 uvc_printk(KERN_ERR, "Failed to register video device (%d).\n", 46 ret); 47 stream->vdev = NULL; 48 video_device_release(vdev); 49 return ret; 50 } 51 52 atomic_inc(&dev->nstreams); 53 return 0; 54 }

 經過 ubuntu 攝像頭測試,測試軟件使用 xawtv

執行xawtv 就能查看到USB攝像頭的數據,

 

測試虛擬攝像頭:vivi

下載 ubuntn 相關版本的內核源碼,複製整個 /media/video 整個目錄複製出來。

編譯最後生成vivi.ko,安裝時候會出現錯誤,能夠使用dmesg查看打印信息,安裝依賴項。

經過xawtv查看虛擬出來的攝像頭模塊。

 安裝時候還能夠用:sudo modprobe vivi 安裝ubuntu自帶的vivi,這樣全部依賴項都會被裝上

而後,rmmod vivi

安裝本身的vivi.ko    這樣能夠修改本身的模塊,用於調試。

 

用 strace 能夠得到應用所涉及的系統調用。

 

得到系統調用信息。

 

 

 

先了解USB攝像頭內部框架:uvc specification

參考: USB_Video_Example 1.5.pdf

    UVC 1.5Class specification.pdf

下載地址    https://github.com/Jason543716996/USB_class.git

 

 

 

分析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
    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);

分析設置亮度過程:
    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);

 

總結:
  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來得到

    

 未完。。。

相關文章
相關標籤/搜索