整个uvc驱动,在内核中的路径是drivers/media/video/uvc
在kernel中配置uvc驱动,需要自己手动在make menuconfig中勾选如下路径:Device Drivers ---> Multimedia support ---> Video For LinuxDevice Drivers ---> Multimedia support ---> [*] Video capture adapters ---> [*] V4L USB devices ---> USB Video Class (UVC)我们先用tree看戏uvc目录下的文件组成:
guanguojin@WX-ASIC-S02-Android:uvc$ tree.├── Kconfig├── Makefile├── uvc_ctrl.c├── uvc_driver.c //uvc驱动的入口处├── uvc_isight.c├── uvc_queue.c├── uvc_status.c├── uvc_v4l2.c├── uvc_video.c└── uvcvideo.h
那我们就先从uvc_driver.c开始剖析整个uvc驱动的架构
首先我们找到驱动的加载入口module_init宏定义的函数:static int __init uvc_init(void){int result;INIT_LIST_HEAD(&uvc_driver.devices);INIT_LIST_HEAD(&uvc_driver.controls);mutex_init(&uvc_driver.ctrl_mutex);uvc_ctrl_init();result = usb_register(&uvc_driver.driver);if (result == 0)printk(KERN_INFO DRIVER_DESC " (" DRIVER_VERSION ")\n");return result;}module_init(uvc_init);
加载函数很简单,除了必要的初始化外,最重要的两步操作就是:uvc_ctrl_init和usb_register
uvc_ctrl_init
void uvc_ctrl_init(void){struct uvc_control_info *ctrl = uvc_ctrls;struct uvc_control_info *cend = ctrl + ARRAY_SIZE(uvc_ctrls);struct uvc_control_mapping *mapping = uvc_ctrl_mappings;struct uvc_control_mapping *mend =mapping + ARRAY_SIZE(uvc_ctrl_mappings);for (; ctrl < cend; ++ctrl)uvc_ctrl_add_info(ctrl);for (; mapping < mend; ++mapping)uvc_ctrl_add_mapping(mapping);}
这里面将uvc支持的GUID(即uvc支持的操作选项,包括亮度调节、灰度调节....)
struct uvc_control_info添加到uvc_driver的controls这个队列中去,其中主要关注这个结构体即可:struct uvc_control_info {struct list_head list;struct list_head mappings;----->添加结构体成员类型struct uvc_control_mapping__u8 entity[16];__u8 index;__u8 selector;__u16 size;__u32 flags;};
usb_register:
这是注册uvc驱动的接口,就是将
struct uvc_driver uvc_driver = {.driver = {.name = "uvcvideo",.probe = uvc_probe,.disconnect = uvc_disconnect,.suspend = uvc_suspend,.resume = uvc_resume,#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 23).reset_resume = uvc_reset_resume,#endif.id_table = uvc_ids,#if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 6, 19).supports_autosuspend = 1,#endif},};
这个结构体注册到usb驱动中,其中uvc_ids就是usb的过滤器,只有将支持uvc camera的VID和PID填入这个结构体,usb驱动才会在识别到camera后将其分发给uvc驱动,当然在uvc_ids中最后有这么个赋值:
/* Generic USB Video Class */ { USB_INTERFACE_INFO(USB_CLASS_VIDEO, 1, 0) }, 也即只要usb描述符中说明是USB_CLASS_VIDEO的,都会交由uvc处理,不必单独将其VID和PID填入了。