1. 程式人生 > >UVC 攝像頭驅動(三)配置攝像頭,實時資料採集

UVC 攝像頭驅動(三)配置攝像頭,實時資料採集

  前面分析了 UVC 攝像頭的硬體模型和描述符,對於一個 usb 攝像頭來說,內部大概分為一個 VC 介面和一個 VS 介面,VC 介面內部有許多 unit 和 terminal 用來“控制”攝像頭,比如我們可以通過 Process unit 設定白平衡、曝光等等。對於 VS 介面來說,標準 VS 介面往往含有許多個設定,每一個設定都包含一個實時傳輸端點,雖然它們的端點地址可能相同,但是它們的最大傳輸包大小不同,在 Class specific VS 介面中,包含多個 Format ,每一個 Format 包含多個 Frame ,Format 指的 YUYV MJPG 等等,Frame 就是各種解析度 480*320 640 * 480 等等。以上這些資訊,都是通過分析描述符來獲得。

VideoStreaming Requests

  參考 UVC 1.5 Class specification 4.3 節
這裡寫圖片描述
我們需要使用控制傳輸來和VS通訊,Probe and commit 設定,請求格式參考上圖。

  • bmRequestType 請求型別,參考標準USB協議
  • bRequest 子類,定義在 Table A-8
  • CS ,Control Selector ,定義在 Table A-16 ,例如是probe 還是 commit
  • wIndex 高位元組為0,低位元組為介面號
  • wLength 和 Data 和標準USB協議一樣,為資料長度和資料

引數設定的過程需要主機和USB裝置進行協商, 協商的過程大致如下圖所示:
這裡寫圖片描述

  • Host 先將期望的設定傳送給USB裝置(PROBE)
  • 裝置將Host期望設定在自身能力範圍之內進行修改,返回給Host(PROBE)
  • Host 認為設定可行的話,Commit 提交(COMMIT)
  • 設定介面的當前設定為某一個設定
      
    那麼協商哪些資料?這些資料在哪裡定義?參考Table 4-75 ,裡面包含了使用哪一個Frame 哪一個 Frame 幀頻率,一次傳輸包大小等等的資訊。
    參考程式碼:
static int myuvc_try_streaming_params(struct myuvc_streaming_control *ctrl)
{
    __u8 *data
;
__u16 size; int ret; __u8 type = USB_TYPE_CLASS | USB_RECIP_INTERFACE; unsigned int pipe; memset(ctrl, 0, sizeof *ctrl); ctrl->bmHint = 1; /* dwFrameInterval */ ctrl->bFormatIndex = 1; ctrl->bFrameIndex = 1; ctrl->dwFrameInterval = 333333; ctrl->dwClockFrequency = 48000000; ctrl->wCompQuality = 61; size = 34; data = kzalloc(size, GFP_KERNEL); if (data == NULL) return -ENOMEM; *(__le16 *)&data[0] = cpu_to_le16(ctrl->bmHint); data[2] = ctrl->bFormatIndex; data[3] = ctrl->bFrameIndex; *(__le32 *)&data[4] = cpu_to_le32(ctrl->dwFrameInterval); *(__le16 *)&data[8] = cpu_to_le16(ctrl->wKeyFrameRate); *(__le16 *)&data[10] = cpu_to_le16(ctrl->wPFrameRate); *(__le16 *)&data[12] = cpu_to_le16(ctrl->wCompQuality); *(__le16 *)&data[14] = cpu_to_le16(ctrl->wCompWindowSize); *(__le16 *)&data[16] = cpu_to_le16(ctrl->wDelay); put_unaligned_le32(ctrl->dwMaxVideoFrameSize, &data[18]); put_unaligned_le32(ctrl->dwMaxPayloadTransferSize, &data[22]); if (size == 34) { put_unaligned_le32(ctrl->dwClockFrequency, &data[26]); data[30] = ctrl->bmFramingInfo; data[31] = ctrl->bPreferedVersion; data[32] = ctrl->bMinVersion; data[33] = ctrl->bMaxVersion; } pipe = usb_sndctrlpipe(myuvc_udev, 0); type |= USB_DIR_OUT; ret = usb_control_msg(myuvc_udev, pipe, 0x01, type, 0x01 << 8, 0 << 8 | myuvc_streaming_intf, data, size, 5000); kfree(data); return (ret < 0) ? ret : 0; } static int myuvc_get_streaming_params(struct myuvc_streaming_control *ctrl) { __u8 *data; __u16 size; int ret; __u8 type = USB_TYPE_CLASS | USB_RECIP_INTERFACE; unsigned int pipe; size = 34; data = kmalloc(size, GFP_KERNEL); if (data == NULL) return -ENOMEM; pipe = usb_rcvctrlpipe(myuvc_udev, 0); type |= USB_DIR_IN; ret = usb_control_msg(myuvc_udev, pipe, 0x81, type, 0x01 << 8, 0 << 8 | myuvc_streaming_intf, data, size, 5000); if (ret < 0) goto done; ctrl->bmHint = le16_to_cpup((__le16 *)&data[0]); ctrl->bFormatIndex = data[2]; ctrl->bFrameIndex = data[3]; ctrl->dwFrameInterval = le32_to_cpup((__le32 *)&data[4]); ctrl->wKeyFrameRate = le16_to_cpup((__le16 *)&data[8]); ctrl->wPFrameRate = le16_to_cpup((__le16 *)&data[10]); ctrl->wCompQuality = le16_to_cpup((__le16 *)&data[12]); ctrl->wCompWindowSize = le16_to_cpup((__le16 *)&data[14]); ctrl->wDelay = le16_to_cpup((__le16 *)&data[16]); ctrl->dwMaxVideoFrameSize = get_unaligned_le32(&data[18]); ctrl->dwMaxPayloadTransferSize = get_unaligned_le32(&data[22]); if (size == 34) { ctrl->dwClockFrequency = get_unaligned_le32(&data[26]); ctrl->bmFramingInfo = data[30]; ctrl->bPreferedVersion = data[31]; ctrl->bMinVersion = data[32]; ctrl->bMaxVersion = data[33]; } else { //ctrl->dwClockFrequency = video->dev->clock_frequency; ctrl->bmFramingInfo = 0; ctrl->bPreferedVersion = 0; ctrl->bMinVersion = 0; ctrl->bMaxVersion = 0; } done: kfree(data); return (ret < 0) ? ret : 0; } static int myuvc_set_streaming_params(struct myuvc_streaming_control *ctrl) { __u8 *data; __u16 size; int ret; __u8 type = USB_TYPE_CLASS | USB_RECIP_INTERFACE; unsigned int pipe; size = 34; data = kzalloc(size, GFP_KERNEL); if (data == NULL) return -ENOMEM; *(__le16 *)&data[0] = cpu_to_le16(ctrl->bmHint); data[2] = ctrl->bFormatIndex; data[3] = ctrl->bFrameIndex; *(__le32 *)&data[4] = cpu_to_le32(ctrl->dwFrameInterval); *(__le16 *)&data[8] = cpu_to_le16(ctrl->wKeyFrameRate); *(__le16 *)&data[10] = cpu_to_le16(ctrl->wPFrameRate); *(__le16 *)&data[12] = cpu_to_le16(ctrl->wCompQuality); *(__le16 *)&data[14] = cpu_to_le16(ctrl->wCompWindowSize); *(__le16 *)&data[16] = cpu_to_le16(ctrl->wDelay); put_unaligned_le32(ctrl->dwMaxVideoFrameSize, &data[18]); put_unaligned_le32(ctrl->dwMaxPayloadTransferSize, &data[22]); if (size == 34) { put_unaligned_le32(ctrl->dwClockFrequency, &data[26]); data[30] = ctrl->bmFramingInfo; data[31] = ctrl->bPreferedVersion; data[32] = ctrl->bMinVersion; data[33] = ctrl->bMaxVersion; } pipe = usb_sndctrlpipe(myuvc_udev, 0); type |= USB_DIR_OUT; ret = usb_control_msg(myuvc_udev, pipe, 0x01, type, 0x02 << 8, 0 << 8 | myuvc_streaming_intf, data, size, 5000); kfree(data); return (ret < 0) ? ret : 0; }

VideoControl Requests

  這裡我們主要分析 VC 接口裡的 Processing Unit Control Requests 以亮度為例:
這裡寫圖片描述
這裡寫圖片描述

static void myuvc_set_le_value(__s32 value, __u8 *data)
{
    int bits = 16;
    int offset = 0;
    __u8 mask;

    data += offset / 8;
    offset &= 7;

    for (; bits > 0; data++) {
        mask = ((1LL << bits) - 1) << offset;
        *data = (*data & ~mask) | ((value << offset) & mask);
        value >>= offset ? offset : 8;
        bits -= 8 - offset;
        offset = 0;
    }
}

static __s32 myuvc_get_le_value(const __u8 *data)
{
    int bits = 16;
    int offset = 0;
    __s32 value = 0;
    __u8 mask;

    data += offset / 8;
    offset &= 7;
    mask = ((1LL << bits) - 1) << offset;

    for (; bits > 0; data++) {
        __u8 byte = *data & mask;
        value |= offset > 0 ? (byte >> offset) : (byte << (-offset));
        bits -= 8 - (offset > 0 ? offset : 0);
        offset -= 8;
        mask = (1 << bits) - 1;
    }

    /* Sign-extend the value if needed. */
    value |= -(value & (1 << (16 - 1)));

    return value;
}

/* 參考:uvc_query_v4l2_ctrl */    
int myuvc_vidioc_queryctrl (struct file *file, void *fh,
                struct v4l2_queryctrl *ctrl)
{
    __u8 type = USB_TYPE_CLASS | USB_RECIP_INTERFACE;
    unsigned int pipe;
    int ret;
    u8 data[2];

    if (ctrl->id != V4L2_CID_BRIGHTNESS)
        return -EINVAL;

    memset(ctrl, 0, sizeof *ctrl);
    ctrl->id   = V4L2_CID_BRIGHTNESS;
    ctrl->type = V4L2_CTRL_TYPE_INTEGER;
    strcpy(ctrl->name, "MyUVC_BRIGHTNESS");
    ctrl->flags = 0;

    pipe = usb_rcvctrlpipe(udev, 0);
    type |= USB_DIR_IN;

    /* 發起USB傳輸確定這些值 */
    ret = usb_control_msg(udev, pipe, GET_MIN, type, PU_BRIGHTNESS_CONTROL << 8,
            ProcessingUnitID << 8 | myuvc_control_intf, data, 2, 5000);
    if (ret != 2)
        return -EIO;
    ctrl->minimum = myuvc_get_le_value(data);   /* Note signedness */


    ret = usb_control_msg(udev, pipe, GET_MAX, type,  PU_BRIGHTNESS_CONTROL << 8,
            ProcessingUnitID << 8 | myuvc_control_intf, data, 2, 5000);
    if (ret != 2)
        return -EIO;
    ctrl->maximum = myuvc_get_le_value(data);   /* Note signedness */

    ret = usb_control_msg(udev, pipe, GET_RES, type, PU_BRIGHTNESS_CONTROL << 8,
             ProcessingUnitID << 8 | myuvc_control_intf, data, 2, 5000);
    if (ret != 2)
        return -EIO;
    ctrl->step = myuvc_get_le_value(data);  /* Note signedness */

    ret = usb_control_msg(udev, pipe, GET_DEF, type, PU_BRIGHTNESS_CONTROL << 8,
            ProcessingUnitID << 8 | myuvc_control_intf, data, 2, 5000);
    if (ret != 2)
        return -EIO;
    ctrl->default_value = myuvc_get_le_value(data); /* Note signedness */

    printk("Brightness: min =%d, max = %d, step = %d, default = %d\n", ctrl->minimum, ctrl->maximum, ctrl->step, ctrl->default_value);

    return 0;
}

/* 參考 : uvc_ctrl_get */
int myuvc_vidioc_g_ctrl (struct file *file, void *fh,
                struct v4l2_control *ctrl)
{
    __u8 type = USB_TYPE_CLASS | USB_RECIP_INTERFACE;
    unsigned int pipe;
    int ret;
    u8 data[2];

    if (ctrl->id != V4L2_CID_BRIGHTNESS)
        return -EINVAL;

    pipe = usb_rcvctrlpipe(udev, 0);
    type |= USB_DIR_IN;

    ret = usb_control_msg(udev, pipe, GET_CUR, type, PU_BRIGHTNESS_CONTROL << 8,
            ProcessingUnitID << 8 | myuvc_control_intf, data, 2, 5000);
    if (ret != 2)
        return -EIO;
    ctrl->value = myuvc_get_le_value(data); /* Note signedness */

    return 0;
}

/* 參考: uvc_ctrl_set/uvc_ctrl_commit */
int myuvc_vidioc_s_ctrl (struct file *file, void *fh,
                struct v4l2_control *ctrl)
{
    __u8 type = USB_TYPE_CLASS | USB_RECIP_INTERFACE;
    unsigned int pipe;
    int ret;
    u8 data[2];

    if (ctrl->id != V4L2_CID_BRIGHTNESS)
        return -EINVAL;

    myuvc_set_le_value(ctrl->value, data);

    pipe = usb_sndctrlpipe(udev, 0);
    type |= USB_DIR_OUT;

    ret = usb_control_msg(udev, pipe, SET_CUR, type, PU_BRIGHTNESS_CONTROL << 8,
            ProcessingUnitID  << 8 | myuvc_control_intf, data, 2, 5000);
    if (ret != 2)
        return -EIO;

    return 0;
}

資料採集

  資料採集時,我們需要和標準VS介面設定裡的實時端點通訊獲取資料,分配、設定、提交URB。
  以 640*320 解析度的影象為例,每一個畫素16bit,因此一幀影象佔用空間 640*320*2位元組 ,對於我的攝像頭一次傳輸,3060位元組。需要注意的是,標準UVC攝像頭驅動中限制了一個URB的Buffer數量最大為32個,因此一個URB能承載的資料為 3060 * 32 ,經過計算,一幀影象資料需要多個 URB 來傳輸。因此在將影象資料拷貝到使用者空間Buffer時候,可能在某一個URB包含兩幀影象的資料,需要不要處理完前一幀就把第二幀影象資料丟棄了,那樣會造成影象資料丟失。

static int myuvc_alloc_init_urbs(void)
{
    u16 psize;
    u32 size;
    int npackets;
    int i,j;
    struct urb *urb;
    //struct urb *urb;

    psize = 3060; /* 實時傳輸端點一次能傳輸的最大位元組數 */
    size  = 614400;  /* 一幀資料的最大長度 */
    npackets = DIV_ROUND_UP(size, psize);
    if (npackets > 32)
        npackets = 32;
    myprintk("psize %d npackets %d\n",psize,npackets);
    size =  psize * npackets;
    for (i = 0; i < 5; ++i) { 
        /* 1. 分配usb_buffers */
        urb_buffer[i] = usb_alloc_coherent(
            myuvc_udev, size,
            GFP_KERNEL | __GFP_NOWARN, &urb_dma[i]);

        /* 2. 分配urb */
        myurb[i] = usb_alloc_urb(npackets, GFP_KERNEL);

        if (!urb_buffer[i] || !myurb[i])
        {
            //myuvc_uninit_urbs();
            myprintk("alloc buffer or urb failed\n");
            return -ENOMEM;
        }
    }

    /* 3. 設定urb */ 
    for (i = 0; i < 5; ++i) {
        urb = myurb[i];
        urb->dev = myuvc_udev;
        urb->context = NULL;
        urb->pipe = usb_rcvisocpipe(myuvc_udev, 0x81);
        urb->transfer_flags = URB_ISO_ASAP | URB_NO_TRANSFER_DMA_MAP;
        urb->interval = 1;
        urb->transfer_buffer = urb_buffer[i];
        urb->transfer_dma = urb_dma[i];
        urb->complete = myuvc_video_complete;
        urb->number_of_packets = npackets;
        urb->transfer_buffer_length = size;

        for (j = 0; j < npackets; ++j) {
            urb->iso_frame_desc[j].offset = j * psize;
            urb->iso_frame_desc[j].length = psize;
        }
    }  
    return 0;
}
static void myuvc_video_complete(struct urb *urb)
{
    u8 *src;
    //u8 *dest;
    int ret, i;
    int len;
    int maxlen;
//    int nbytes;
//    struct myuvc_buffer *buf;
    myprintk("video complete\n");
    switch (urb->status) {
    case 0:
        break;

    default:
        myprintk("Non-zero status (%d) in video "
            "completion handler.\n", urb->status);
        return;
    }

    for (i = 0; i < urb->number_of_packets; ++i) {
        if (urb->iso_frame_desc[i].status < 0) {
            myprintk("USB isochronous frame "
                "lost (%d).\n", urb->iso_frame_desc[i].status);
            continue;
        }

        src  = urb->transfer_buffer + urb->iso_frame_desc[i].offset;

        //dest = myuvc_queue.mem + buf->buf.m.offset + buf->buf.bytesused;

        len = urb->iso_frame_desc[i].actual_length;
        myprintk("len %d\n",urb->iso_frame_desc[i].actual_length);
        /* 判斷資料是否有效 */
        /* URB資料含義:
         * data[0] : 頭部長度
         * data[1] : 錯誤狀態
         */
        if (len < 2 || src[0] < 2 || src[0] > len)
            continue;

        /* Skip payloads marked with the error bit ("error frames"). */
        if (src[1] & UVC_STREAM_ERR) {
            myprintk("Dropping payload (error bit set).\n");
            continue;
        }

        /* 除去頭部後的資料長度 */
        len -= src[0];

        /* 緩衝區最多還能存多少資料 */
        //maxlen = buf->buf.length - buf->buf.bytesused;
        //nbytes = min(len, maxlen);

        /* 複製資料 */
        //memcpy(dest, src + src[0], nbytes);
        //buf->buf.bytesused += nbytes;

        /* 判斷一幀資料是否已經全部接收到 */
        if (len > maxlen) {
            //buf->state = VIDEOBUF_DONE;
        }

        /* Mark the buffer as done if the EOF marker is set. */
        if (src[1] & UVC_STREAM_EOF) {
            myprintk("Frame complete (EOF found).\n");
            if (len == 0)
                myprintk("EOF in empty payload.\n");
            //buf->state = VIDEOBUF_DONE;
        }

    }

    /* 再次提交URB */


    if ((ret = usb_submit_urb(urb, GFP_ATOMIC)) < 0) {
        myprintk("Failed to resubmit video URB (%d).\n", ret);
    }
}