1、启动传输
static int myuvc_vidioc_streamon(struct file *file, void *priv, enum v4l2_buf_type i)
{
int ret;
/* 1. 向USB摄像头设置参数: 比如使用哪个format, 使用这个format下的哪个frame(分辨率)
* 参考: uvc_set_video_ctrl(设置) / uvc_get_video_ctrl(获得)
关注的是结构体uvc_streaming_control
* 1.1 根据一个结构体uvc_streaming_control设置数据包: 可以手工设置(uvc_v4l2.c),也可以读出后(发起usb传输读进来)再修改(uvc_video.c)
* 1.2 调用usb_control_msg发出数据包
*/
/* a. 测试参数 */
发参数给USB设备,如果USB设备能够接收这个参数,会保存起来并做一些修正,我们try后只发出(设置)几个参数,很多其他参数并没有设置,如果发出的几个参数合适就把其他参数补齐
ret = myuvc_try_streaming_params(&myuvc_params);
printk("myuvc_try_streaming_params ret = %d\n", ret);
/* b. 取出参数 */
把补齐的所有参数都读取回来
ret = myuvc_get_streaming_params(&myuvc_params);
printk("myuvc_get_streaming_params ret = %d\n", ret);
/* c. 设置参数 */
进行设置
ret = myuvc_set_streaming_params(&myuvc_params);
printk("myuvc_set_streaming_params ret = %d\n", ret);
myuvc_print_streaming_params(&myuvc_params);
/* d. 设置VideoStreaming Interface所使用的setting(接口的设置)
* d.1 从myuvc_params确定带宽(计算机网络的带宽是指网络可通过的最高数据率,即每秒多少比特)
* d.2 根据setting的endpoint能传输的wMaxPacketSize(端点一次能传输多大的数据)
* 找到能满足该带宽的setting(根据带宽确定端点)
*/
/* 手工确定:
* bandwidth = myuvc_params.dwMaxPayloadTransferSize = 1024
* 观察lsusb -v -d 0x1e4e:的结果: (-v 列出描述符,-D 选项,你可以打印特定设备的详细信息)
* wMaxPacketSize 0x0320 1x 1024 bytes
* bAlternateSetting 8
*/
最大传输单元
myuvc_udev-》设备,myuvc_streaming_intf-》视频流接口,myuvc_streaming_bAlternateSetting-》设置
usb_set_interface(myuvc_udev, myuvc_streaming_intf, myuvc_streaming_bAlternateSetting);
/* 2. 分配设置URB */
ret = myuvc_alloc_init_urbs();
if (ret)
printk("myuvc_alloc_init_urbs err : ret = %d\n", ret);
/* 3. 提交URB以接收数据 */
for (i = 0; i < MYUVC_URBS; ++i) {
if ((ret = usb_submit_urb(myuvc_queue.urb[i], GFP_KERNEL)) < 0) {
printk("Failed to submit URB %u (%d).\n", i, ret);
myuvc_uninit_urbs();
return ret;
}
}
return 0;
}
struct myuvc_streaming_control {
__u16 bmHint; //参考UVC 1.5 Class specification.pdf,每1位都有含义
__u8 bFormatIndex;
__u8 bFrameIndex;
__u32 dwFrameInterval;
__u16 wKeyFrameRate;
__u16 wPFrameRate;
__u16 wCompQuality;
__u16 wCompWindowSize;
__u16 wDelay;
__u32 dwMaxVideoFrameSize;
__u32 dwMaxPayloadTransferSize;
__u32 dwClockFrequency;
__u8 bmFramingInfo;
__u8 bPreferedVersion;
__u8 bMinVersion;
__u8 bMaxVersion;
};
2、测试参数
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 = frame_idx + 1;//支持格式下的分辨率数
ctrl->dwFrameInterval = 333333;//帧的间隔(单位是100ns),1秒钟的帧数(30帧)
看文件USB_Video_Payload_Uncompressed_1.5.pdf
size = uvc_version >= 0x0110 ? 34 : 26;//根据不同版本看需要发送多少数据,利用lsusb中查找bcduvc,也可以在UVC 1.5 Class specification.pdf中查找bcduvc属于哪一个描述符
//分配data
data = kzalloc(size, GFP_KERNEL);//分配缓冲区,
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 = (SET_CUR & 0x80) ? usb_rcvctrlpipe(myuvc_udev, 0)
: usb_sndctrlpipe(myuvc_udev, 0);
type |= (SET_CUR & 0x80) ? USB_DIR_IN : USB_DIR_OUT;
//进行发送,ret是接收的字节数
//usb_control_msg是没有用到urb的在USB中简单进行发送和接收的一种机制,用于少量的数据通信
ret = usb_control_msg(myuvc_udev, pipe, SET_CUR, type, VS_PROBE_CONTROL << 8,
0 << 8 | myuvc_streaming_intf, data, size, 5000);
kfree(data);//释放缓冲区
return (ret < 0) ? ret : 0;
}
3、取出参数
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 = uvc_version >= 0x0110 ? 34 : 26;(因为版本号是0x0100,少于0x0110,也就是26)
//摄像头的uvc版本,用lsusb命令时可以查看,也可以看UVC 1.5 Class specification.pdf文档里的bcdUVC,可以看出来自VC接口描述符的头部的的第3个偏移字节,占两个字节(为0100)
data = kmalloc(size, GFP_KERNEL);//分配缓存
if (data == NULL)
return -ENOMEM;
//pipe实际上是设置好的端点,GET_CUR是0x81,从端点0获得
pipe = (GET_CUR & 0x80) ? usb_rcvctrlpipe(myuvc_udev, 0)//如果GET_CUR是0x81,,把指定USB设备指定端点设置为一个控制IN端点,参数从端点0获得
: usb_sndctrlpipe(myuvc_udev, 0);//把指定USB设备指定端点设置为一个控制OUT端点。
type |= (GET_CUR & 0x80) ? USB_DIR_IN : USB_DIR_OUT;//type是数据传输方向
//,这里myuvc_dev=dev
ret = usb_control_msg(myuvc_udev, pipe, GET_CUR, type, VS_PROBE_CONTROL << 8,//参数myuvc_udev是usb_device结构体;pipe是管道,对于usb传输对象是端点;GET_CUR是获取当前参数;probe是VS_PROBE_CONTROL枚举参数;VS_COMMIT_CONTROL(设置参数)
0 << 8 | myuvc_streaming_intf, data, size, 5000);//我们是去streaming interface获得参数(USB摄像头里面有两个interface:videocontrol interface和videostreaming interface,这些参数是在streaming interface里面的);5000是延时时间,以ms为单位;
//用usb_control_msg函数发起usb传输,读到数据,把当前数据存在data里面。
if (ret < 0)
goto done;
//把data数组里面的数据转存到myuvc_streaming_control结构体里面
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;
}
4、设置参数
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 = uvc_version >= 0x0110 ? 34 : 26;
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 = (SET_CUR & 0x80) ? usb_rcvctrlpipe(myuvc_udev, 0)
: usb_sndctrlpipe(myuvc_udev, 0);
type |= (SET_CUR & 0x80) ? USB_DIR_IN : USB_DIR_OUT;
//跟try不一样的是,刚才是 VS_PROBE_CONTROL(枚举尝试),现在是VS_COMMIT_CONTROL(设置参数)
ret = usb_control_msg(myuvc_udev, pipe, SET_CUR, type, VS_COMMIT_CONTROL << 8,
0 << 8 | myuvc_streaming_intf, data, size, 5000);
0 << 8 | myuvc_streaming_intf这里索引字段是2个字节,描述的是接口号,因为有两种接口,一种是视频流接口(这些参数存储的地方),一种是视频控制接口
kfree(data);
return (ret < 0) ? ret : 0;
}
4、probe函数()
static int myuvc_probe(struct usb_interface *intf,
const struct usb_device_id *id)
{
static int cnt = 0;
//interface_to_usbdev() -- 根据usb_interface指针intf获取usb_device的地址
struct usb_device *dev = interface_to_usbdev(intf);//根据接口找到对应的设备
int ret;
myuvc_udev = dev;
printk("myuvc_probe : cnt = %d\n", cnt++);
if (cnt == 1)
{
//控制接口的编号
myuvc_control_intf = intf->cur_altsetting->desc.bInterfaceNumber;
}
else if (cnt == 2)
{
//流接口的编号
myuvc_streaming_intf = intf->cur_altsetting->desc.bInterfaceNumber;
}
if (cnt == 2)
{
/* 1. 分配一个video_device结构体 */
myuvc_vdev = video_device_alloc();
/* 2. 设置 */
/* 2.1 */
myuvc_vdev->release = myuvc_release;
/* 2.2 */
myuvc_vdev->fops = &myuvc_fops;
/* 2.3 */
myuvc_vdev->ioctl_ops = &myuvc_ioctl_ops;
/* 3. 注册 */
video_register_device(myuvc_vdev, VFL_TYPE_GRABBER, -1);
其中:第一个参数struct video_device *vdev是想注册的video device的结构体;
第二个参数是视频设备类型,在此头文件(v4l2-dev.h中),定义了几种视频设备类型:
#defineVFL_TYPE_GRABBER 0 //图像采集设备
#defineVFL_TYPE_VBI 1 //从视频消隐的时间段获得信息的设备
#defineVFL_TYPE_RADIO 2 //无线电设备
#defineVFL_TYPE_SUBDEV 3 //视频设备(不确定)
#defineVFL_TYPE_MAX 4 //最大值
第三个参数是注册的设备节点号;
/* 为了确定带宽,使用哪一个setting */
/* a. 测试参数 */
ret = myuvc_try_streaming_params(&myuvc_params);
printk("myuvc_try_streaming_params ret = %d\n", ret);
/* b. 取出参数 */
ret = myuvc_get_streaming_params(&myuvc_params);
printk("myuvc_get_streaming_params ret = %d\n", ret);
/* c. 设置参数 */
ret = myuvc_set_streaming_params(&myuvc_params);
printk("myuvc_set_streaming_params ret = %d\n", ret);
myuvc_print_streaming_params(&myuvc_params);
}
return 0;
}