2.5 USB攝像頭驅動程序框架

學習目標:根據vivi驅動架構和linux-2.6.31/linux-2.6.31.14/drivers/media/video/uvc/Uvc_driver.c驅動源碼,分析usb攝像頭驅動程序框架;linux

1、攝像頭拓撲結構數組

由數據手冊USB_Video_Example 1.5和UVC 1.5 Class specification分析攝像頭的拓撲結構,以下圖所示:架構

usb攝像頭功能由攝像頭接口完成,每一個攝像頭功能包括一個 VideoControl interface (VC)和幾個VideoStreaming interface (VS)。框架

1) CS主要用於控制,它又抽象出兩個概念:unit和termiral。unit用於裏面的相關控制。termiral用於內外鏈接。其中,unit中包括:a.  Selector Unit (SU) 選擇(CT (Camera Terminal) 和IT(Input Terminal))哪一路 。b. Processing Unit (PU) 用於亮度等屬性參數控制。ide

2) VS用於傳輸,來讀視頻數據,。須要設置類型type和格式format,選擇實時傳輸仍是批量傳輸Bulk。函數

2、usb攝像頭源碼框架分析 學習

在Linux內核裏自帶usb攝像頭驅動程序,它支持uvc規格的攝像頭(uvc即usb video class),進入源碼程序分析。spa

一、進入uvc_init入口函數code

      --> result = usb_register(&uvc_driver.driver);orm

struct uvc_driver uvc_driver = { .driver = { .name = "uvcvideo", .probe = uvc_probe, .disconnect = uvc_disconnect, .suspend = uvc_suspend, .resume = uvc_resume, .reset_resume = uvc_reset_resume, .id_table = uvc_ids, .supports_autosuspend = 1, }, };

註冊了 uvc_driver結構體,根據.id_table匹配後,進入uvc_probe函數:
uvc_register_video(dev)

  -->vdev = video_device_alloc();

   vdev->fops = &uvc_fops;

   vdev->release = video_device_release;

   video_register_device(vdev, VFL_TYPE_GRABBER, -1)

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, };

由此可知,寫一個USB攝像頭驅動程序須要如下步驟:
1)構造一個usb_driver結構體(uvc結構體)
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驅動調用過程

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) uvc_v4l2_ioctl函數

-->video_usercopy(file, cmd, arg, uvc_v4l2_do_ioctl);//將用戶提供的參數拷貝到內核態,並調用uvc_v4l2_do_ioctl函數

  -->uvc_v4l2_do_ioctl

    struct video_device *vdev = video_devdata(file);//根據次設備號,找到video_device

    switch (cmd) { //根據cmd執行一系列的ioctl函數
     /* Query capabilities */
     case VIDIOC_QUERYCAP:

     .......

接下來具體分析ioctl過程:

1)VIDIOC_QUERYCAP

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; break;

// video->streaming->type 應該是在設備被枚舉時分析描述符時設置的。(對用功能拓撲的VS)

2)VIDIOC_ENUM_FMT // format數組應是在設備被枚舉時設置的

  format = &video->streaming->format[fmt->index];

3)VIDIOC_G_FMT

  --> uvc_v4l2_get_format(video, arg); // USB攝像頭支持多種格式fromat, 每種格式下有多種frame(好比分辨率)

     struct uvc_format *format = video->streaming->cur_format;
     struct uvc_frame *frame = video->streaming->cur_frame;

4)VIDIOC_TRY_FMT

  -->uvc_v4l2_try_format(video, arg, &probe, NULL, NULL);

    /* Check if the hardware supports the requested format. */
    /* Find the closest image size.*/找到最接近的圖像格式

5)VIDIOC_S_FMT  // 只是把參數保存起來,尚未發給USB攝像頭

  -->uvc_v4l2_set_format(video, arg);

    uvc_v4l2_try_format(video, fmt, &probe, &format, &frame);

     memcpy(&video->streaming->ctrl, &probe, sizeof probe);
     video->streaming->cur_format = format;
     video->streaming->cur_frame = frame;

6)VIDIOC_REQBUFS

  -->uvc_alloc_buffers(&video->queue, rb->count, bufsize);

       unsigned int bufsize = video->streaming->ctrl.dwMaxVideoFrameSize; //bufsize已經設置並獲取,vmalloc時只需傳進來應用程序的rb->count數值

    /* 根據rb->count來分配多少個buf*/

    for (; nbuffers > 0; --nbuffers) {
      mem = vmalloc_32(nbuffers * bufsize);
      if (mem != NULL)
       break;
     }   

 7)VIDIOC_QUERYBUF

  -->uvc_query_buffer(&video->queue, buf);

            __uvc_query_buffer
                memcpy(v4l2_buf, &buf->buf, sizeof *v4l2_buf);  // 複製參數

8)mmap //查詢buf參數後使用mmap把buf映射給應用程序

  uvc_v4l2_mmap

9)VIDIOC_QBUF 

  -->uvc_queue_buffer(&video->queue, arg);

     list_add_tail(&buf->stream, &queue->mainqueue);
     list_add_tail(&buf->queue, &queue->irqqueue);

10)VIDIOC_STREAMON

  -->uvc_video_enable(video, 1)  // 把所設置的參數發給硬件,而後啓動攝像頭

    /* Commit the streaming parameters.  /* 傳輸
      uvc_commit_video(video, &video->streaming->ctrl) //*在前面VIDIOC_S_FMT賦值的video->streaming->ctrl 

      -->uvc_set_video_ctrl(video, probe, 0);///* 設置格式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);

注意:此處用到了__uvc_query_ctrl函數

        /* *啓動 Initialize isochronous/bulk URBs and allocate transfer buffers. */

        uvc_init_video(video, GFP_KERNEL);

        uvc_init_video_isoc(video, ep, gfp_flags);(實時傳輸)/uvc_init_video_bulk(video, ep, gfp_flags)(批量傳輸)

          -->usb_alloc_urb

          設置urb結構體(urb用於usb數據傳輸的結構體)

           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(video->urb[i], gfp_flags)) ; /* Submit the URBs. */
11) poll
        --> uvc_v4l2_poll           
            uvc_queue_poll
                poll_wait(file, &buf->wait, wait);  // 休眠等待有數據   

12)  VIDIOC_DQBUF //有數據時,把buf從隊列中取出,並從原始隊列裏面刪除

   --> uvc_dequeue_buffer(&video->queue, arg, file->f_flags & O_NONBLOCK);

      list_del(&buf->stream);  

13)VIDIOC_STREAMOFF 

   uvc_video_enable(video, 0); //0表示關閉

   uvc_uninit_video(video, 1);

  --> usb_kill_urb(urb);
     usb_free_urb(urb);

-----以上均涉及到VS(Video Streaming Interface)接口的操做,不涉及Video Control Interface-----

下面以設置亮度過程爲例,操做VC(Video Control Interface)接口(實質上是向VC發包):

ioctl: VIDIOC_S_CTRL

--->uvc_ctrl_set(video, &xctrl);//設置
     uvc_ctrl_commit(video); //傳送
                __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);

3、總結

1. UVC設備是基於2個interface: VideoControl Interface VC, VideoStreaming Interface VS 進行操做的;

2. 1) VC 用於控制屬性參數,好比設置亮度。它內部有多個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);

  2) VS用於得到視頻數據,也能夠用來選擇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);

3. 設置FORMAT時只是簡單的使用video->streaming->format[fmt->index]等數據,這些數據應是設備被枚舉時設置的,也就是分析它的描述符時設置的。

4. UVC驅動的重點在於: usb攝像頭描述符的分析
   屬性的控制: 經過VideoControl Interface(VC)來設置
   格式的選擇:經過VideoStreaming Interface(VS)來設置
   數據的得到:經過VideoStreaming Interface(VS)的URB-->(usb設備驅動數據傳輸的結構體格式)來得到。

 


參考:百問網(韋東山視頻)

相關文章
相關標籤/搜索