1.uvc驅動模塊入口 [cpp] module_init(uvc_init); //1.模塊入口 2.初始化函數 [cpp] static int __init uvc_init(void) // 2.初始化函數 { int result; result = usb_register(&uvc_driver.driver); // 3.注冊usb設備驅動(usb攝像頭設備) if (result == 0) //注冊失敗 printk(KERN_INFO DRIVER_DESC " (" DRIVER_VERSION ")\n"); return result; } 3.注冊usb設備驅動(usb攝像頭設備) 3.1 usb攝像頭驅動 [cpp] struct uvc_driver uvc_driver = { // 3.1 usb攝像頭設備 .driver = { .name = "uvcvideo", .probe = uvc_probe, // 4. probe方法 .disconnect = uvc_disconnect, .suspend = uvc_suspend, .resume = uvc_resume, .reset_resume = uvc_reset_resume, .id_table = uvc_ids, //3.2 支持的設備id列表 .supports_autosuspend = 1, }, }; 3.2 支持的設備id列表uvc_ids [cpp] static struct usb_device_id uvc_ids[] = { /* Genius eFace 2025 */ { .match_flags = USB_DEVICE_ID_MATCH_DEVICE | USB_DEVICE_ID_MATCH_INT_INFO, .idVendor = 0x0458, .idProduct = 0x706e, .bInterfaceClass = USB_CLASS_VIDEO, //uvc接口類 0x0e .bInterfaceSubClass = 1, .bInterfaceProtocol = 0, .driver_info = UVC_QUIRK_PROBE_MINMAX }, ... ... ... /* SiGma Micro USB Web Camera */ { .match_flags = USB_DEVICE_ID_MATCH_DEVICE | USB_DEVICE_ID_MATCH_INT_INFO, .idVendor = 0x1c4f, .idProduct = 0x3000, .bInterfaceClass = USB_CLASS_VIDEO, .bInterfaceSubClass = 1, .bInterfaceProtocol = 0, .driver_info = UVC_QUIRK_PROBE_MINMAX | UVC_QUIRK_IGNORE_SELECTOR_UNIT }, /* Generic USB Video Class */ //通用usb視頻類 { USB_INTERFACE_INFO(USB_CLASS_VIDEO, 1, 0) }, //匹配方法:uvc類 {} }; 4.probe方法 [cpp] static int uvc_probe(struct usb_interface *intf,const struct usb_device_id *id) { struct usb_device *udev = interface_to_usbdev(intf); //通過usb接口獲取usb設備 struct uvc_device *dev; //聲明uvc設備 int ret; if (id->idVendor && id->idProduct) //有廠商id和商品id(知名設備) uvc_trace(UVC_TRACE_PROBE, "Probing known UVC device %s (%04x:%04x)\n", udev->devpath, id->idVendor,id->idProduct); else //通用uvc設備 uvc_trace(UVC_TRACE_PROBE, "Probing generic UVC device %s\n",udev->devpath); /* Allocate memory for the device and initialize it. */ if ((dev = kzalloc(sizeof *dev, GFP_KERNEL)) == NULL) //分配uvc設備內存 return -ENOMEM; INIT_LIST_HEAD(&dev->entities); //初始化entities(實體)鏈表 Terminal或Unit INIT_LIST_HEAD(&dev->chains); //初始化chains(鏈)鏈表 INIT_LIST_HEAD(&dev->streams); //初始化streams(視頻流)鏈表 atomic_set(&dev->nstreams, 0); atomic_set(&dev->users, 0); atomic_set(&dev->nmappings, 0); dev->udev = usb_get_dev(udev); //捆綁usb設備,並增加其引用計數 dev->intf = usb_get_intf(intf); //捆綁usb接口,並增加其引用計數 dev->intfnum = intf->cur_altsetting->desc.bInterfaceNumber; //獲取usb接口描述符接口數 dev->quirks = (uvc_quirks_param == -1) ? id->driver_info : uvc_quirks_param; if (udev->product != NULL) //存在產品名 strlcpy(dev->name, udev->product, sizeof dev->name); //設置uvc設備名字為其產品名 else //通用的uvc設備名 snprintf(dev->name, sizeof dev->name,"UVC Camera (%04x:%04x)",le16_to_cpu(udev->descriptor.idVendor),le16_to_cpu(udev->descriptor.idProduct)); /* Parse the Video Class control descriptor. */ if (uvc_parse_control(dev) < 0) { //-->5 uvc解析usb視頻類控制描述符 uvc_trace(UVC_TRACE_PROBE, "Unable to parse UVC descriptors.\n"); goto error; } uvc_printk(KERN_INFO, "Found UVC %u.%02x device %s (%04x:%04x)\n",dev->uvc_version >> 8, dev->uvc_version & 0xff, udev->product ? udev->product : "<unnamed>",le16_to_cpu(udev->descriptor.idVendor),le16_to_cpu(udev->descriptor.idProduct)); if (dev->quirks != id->driver_info) { uvc_printk(KERN_INFO, "Forcing device quirks to 0x%x by module parameter for testing purpose.\n", dev->quirks); uvc_printk(KERN_INFO, "Please report required quirks to the linux-uvc-devel mailing list.\n"); } /* Initialize controls. */ if (uvc_ctrl_init_device(dev) < 0) //8.uvc初始化控制 goto error; /* Scan the device for video chains. */ if (uvc_scan_device(dev) < 0) //10.uvc掃描視頻鏈 goto error; /* Register video devices. */ if (uvc_register_chains(dev) < 0) //11.uvc注冊視頻設備 goto error; /* Save our data pointer in the interface data. */ usb_set_intfdata(intf, dev); //設置uvc設備為usb接口的數據 /* Initialize the interrupt URB. */ if ((ret = uvc_status_init(dev)) < 0) { //12 uvc設備狀態初始化 uvc_printk(KERN_INFO, "Unable to initialize the status endpoint (%d), status interrupt will not be supported.\n", ret); } uvc_trace(UVC_TRACE_PROBE, "UVC device initialized.\n"); usb_enable_autosuspend(udev); //使能自動掛起 return 0; error: uvc_unregister_video(dev); return -ENODEV; } 4.1 uvc設備結構體 [cpp] struct uvc_device { struct usb_device *udev; //usb設備指針 struct usb_interface *intf; //usb接口指針 unsigned long warnings; __u32 quirks; int intfnum; //接口數 char name[32]; //設備名 enum uvc_device_state state; //uvc設備狀態 atomic_t users; atomic_t nmappings; /* Video control interface */ __u16 uvc_version; //UVC協議版本 __u32 clock_frequency; //時鐘頻率 struct list_head entities; //uvc實體鏈表頭(掛著uvc設備的Terminal和Unit) struct list_head chains; //uvc視頻鏈鏈表頭 /* Video Streaming interfaces */ struct list_head streams; //uvc視頻流鏈表頭 atomic_t nstreams;//uvc視頻流個數 /* Status Interrupt Endpoint */ struct usb_host_endpoint *int_ep; //usb_host_endpoint對象 struct urb *int_urb; //中斷urb __u8 *status; //uvc設備狀態標志 struct input_dev *input; //輸入設備 char input_phys[64]; //輸入設備設備節點路徑 };