1、USB攝像頭驅動框架以下所示:
1.構造一個usb_driver
2.設置
probe:
2.1. 分配video_device:video_device_alloc
2.2. 設置
.fops
.ioctl_ops (裏面須要設置11項)
若是要用內核提供的緩衝區操做函數,還須要構造一個videobuf_queue_ops
2.3. 註冊: video_register_device
id_table: 表示支持哪些USB設備
3.註冊: usb_register
數組
2、分析UVC驅動app
UVC: USB Video Class
UVC驅動:drivers\media\video\uvc\框架
uvc_driver.c分析:
1. usb_register(&uvc_driver.driver);
2. uvc_probe
uvc_register_video
vdev = video_device_alloc();
vdev->fops = &uvc_fops;
video_register_deviceide
USB_Video_Example 1.5.pdf : 有示例
由拓撲圖可看出:
USB攝像頭內部分紅兩部分函數
VideoControl Interface:視頻控制接口 ——> 用於控制spa
VideoSteaming Interface:視頻流接口——>用於讀取視頻數據code
在USB Camera內部抽象出了個概念:orm
Units :單元,內部鏈接,提供具備大部分功能的模塊,如Selector Unit 、Process Unit視頻
Terminals :端點,用於「內」「外」的鏈接blog
1. open: uvc_v4l2_open //主要是一些狀態的設置 應用程序調用ioctl時,就會進入uvc_v4l2_ioctl .ioctl = uvc_v4l2_ioctl, //把應用程序提供的參數拷貝到內核態,而後調用uvc_v4l2_do_ioctl video_usercopy(file, cmd, arg, uvc_v4l2_do_ioctl) uvc_v4l2_do_ioctl case VIDIOC_QUERYCAP: ... //一系列ioctl case VIDIOC_G_INPUT: 2. VIDIOC_QUERYCAP // video->streaming->type 在設備被枚舉時分析描述符時初始化 if (video->streaming->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_STREAMING; else cap->capabilities = V4L2_CAP_VIDEO_OUTPUT | V4L2_CAP_STREAMING; 3. VIDIOC_ENUM_FMT // format數組應是在設備被枚舉時設置的 format = &video->streaming->format[fmt->index]; 4. VIDIOC_G_FMT uvc_v4l2_get_format // USB攝像頭支持多種格式fromat, 每種格式下有多種frame(好比分辨率) struct uvc_format *format = video->streaming->cur_format; struct uvc_frame *frame = video->streaming->cur_frame; 5. VIDIOC_TRY_FMT uvc_v4l2_try_format /* Check if the hardware supports the requested format. */ /* Find the closest image size. The distance between image sizes is * the size in pixels of the non-overlapping regions between the * requested size and the frame-specified size. */ 6. VIDIOC_S_FMT // 只是把參數保存起來,尚未發給USB攝像頭 uvc_v4l2_set_format uvc_v4l2_try_format video->streaming->cur_format = format; video->streaming->cur_frame = frame; 7. VIDIOC_REQBUFS uvc_alloc_buffers for (; nbuffers > 0; --nbuffers) { mem = vmalloc_32(nbuffers * bufsize); if (mem != NULL) break; } 8. VIDIOC_QUERYBUF uvc_query_buffer __uvc_query_buffer memcpy(v4l2_buf, &buf->buf, sizeof *v4l2_buf); // 複製參數 9. mmap uvc_v4l2_mmap 10. VIDIOC_QBUF uvc_queue_buffer list_add_tail(&buf->stream, &queue->mainqueue); list_add_tail(&buf->queue, &queue->irqqueue); /*應用程序把buf放入隊列後,會調用STREAMON作一些初始化,啓動傳輸,*/ 11. VIDIOC_STREAMON uvc_video_enable(video, 1) // 把所設置的參數發給硬件,而後啓動攝像頭 /* Commit the streaming parameters. */ uvc_commit_video uvc_set_video_ctrl /* 設置格式fromat, frame */ ret = __uvc_query_ctrl(video->dev /* 哪個USB設備 */, SET_CUR, 0, video->streaming->intfnum /* 哪個接口: VS */, probe ? VS_PROBE_CONTROL : VS_COMMIT_CONTROL, data, size, uvc_timeout_param); /* 啓動:初始化實時的USB請求塊,並分配傳輸buf. */ uvc_init_video(video, GFP_KERNEL); uvc_init_video_isoc / uvc_init_video_bulk urb->complete = uvc_video_complete; /*收到數據後此函數被調用,它又調用video->decode(urb, video, buf); *最終會調用到=> wake_up(&buf->wait);終止休眠等待*/ usb_submit_urb //初始完後提交urb結構體(USB Request Block) /* 調用poll查詢數據是否準備就緒,沒有的話進入休眠等待 */ 12. poll uvc_v4l2_poll uvc_queue_poll poll_wait(file, &buf->wait, wait); // 休眠等待有數據 /* poll返回以後就代表有可用數據,就從隊列中取出buf */ 13. VIDIOC_DQBUF uvc_dequeue_buffer list_del(&buf->stream); 14. VIDIOC_STREAMOFF uvc_video_enable(video, 0); usb_kill_urb(urb); usb_free_urb(urb); 分析設置亮度過程: ioctl: VIDIOC_S_CTRL uvc_ctrl_set uvc_ctrl_commit __uvc_ctrl_commit(video, 0); uvc_ctrl_commit_entity(video->dev, entity, rollback); ret = uvc_query_ctrl(dev /* 哪個USB設備 */, SET_CUR, ctrl->entity->id /* 哪個unit/terminal */, dev->intfnum /* 哪個接口: VC interface */, ctrl->info->selector, uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT), ctrl->info->size)
總結:
1. UVC設備有2個interface: VideoControl Interface, VideoStreaming Interface 2. VideoControl Interface用於控制,好比設置亮度。它內部有多個Unit/Terminal(在程序裏Unit/Terminal都稱爲entity:實體) 能夠經過相似的函數來訪問: ret = uvc_query_ctrl(dev /* 哪個USB設備 */, SET_CUR, ctrl->entity->id /* 哪個unit/terminal */, dev->intfnum /* 哪個接口: VC interface */, ctrl->info->selector, uvc_ctrl_data(ctrl, UVC_CTRL_DATA_CURRENT), ctrl->info->size); 3. VideoStreaming Interface用於得到視頻數據,也能夠用來選擇fromat/frame(VS可能有多種format, 一個format支持多種frame, frame用來表示分辨率等信息) 能夠經過相似的函數來訪問: ret = __uvc_query_ctrl(video->dev /* 哪個USB設備 */, SET_CUR, 0, video->streaming->intfnum /* 哪個接口: VS */, probe ? VS_PROBE_CONTROL : VS_COMMIT_CONTROL, data, size, uvc_timeout_param); 4. 咱們在設置FORMAT時只是簡單的使用video->streaming->format[fmt->index]等數據, 這些數據哪來的? 應是設備被枚舉時設置的,也就是分析它的描述符時設置的。 5. UVC驅動的重點在於: 描述符的分析 屬性的控制: 經過VideoControl Interface來設置 格式的選擇:經過VideoStreaming Interface來設置 數據的得到:經過VideoStreaming Interface的URB來得到