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 节 
UVC 摄像头驱动(三)配置摄像头,实时数据采集

我们需要使用控制传输来和VS通信,Probe and commit 设置,请求格式参考上图。

bmRequestType 请求类型,参考标准USB协议
bRequest 子类,定义在 Table A-8
CS ,Control Selector ,定义在 Table A-16 ,例如是probe 还是 commit
wIndex 高字节为0,低字节为接口号
wLength 和 Data 和标准USB协议一样,为数据长度和数据
参数设置的过程需要主机和USB设备进行协商, 协商的过程大致如下图所示: 
 

UVC 摄像头驱动(三)配置摄像头,实时数据采集

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

上一篇:Android 基于USB_BUIK 触摸驱动


下一篇:Linux 内核提交和控制一个 urb