Lines Matching full:video
3 * uvc_video.c -- USB Video Class Gadget driver
14 #include <linux/usb/video.h>
25 * Video codecs
29 uvc_video_encode_header(struct uvc_video *video, struct uvc_buffer *buf, in uvc_video_encode_header() argument
32 struct uvc_device *uvc = container_of(video, struct uvc_device, video); in uvc_video_encode_header()
37 data[1] = UVC_STREAM_EOH | video->fid; in uvc_video_encode_header()
39 if (video->queue.flags & UVC_QUEUE_DROP_INCOMPLETE) in uvc_video_encode_header()
42 if (video->queue.buf_used == 0 && ts.tv_sec) { in uvc_video_encode_header()
66 if (buf->bytesused - video->queue.buf_used <= len - pos) in uvc_video_encode_header()
73 uvc_video_encode_data(struct uvc_video *video, struct uvc_buffer *buf, in uvc_video_encode_data() argument
76 struct uvc_video_queue *queue = &video->queue; in uvc_video_encode_data()
80 /* Copy video data to the USB buffer. */ in uvc_video_encode_data()
91 uvc_video_encode_bulk(struct usb_request *req, struct uvc_video *video, in uvc_video_encode_bulk() argument
96 int len = video->req_size; in uvc_video_encode_bulk()
100 if (video->payload_size == 0) { in uvc_video_encode_bulk()
101 ret = uvc_video_encode_header(video, buf, mem, len); in uvc_video_encode_bulk()
102 video->payload_size += ret; in uvc_video_encode_bulk()
107 /* Process video data. */ in uvc_video_encode_bulk()
108 len = min_t(int, video->max_payload_size - video->payload_size, len); in uvc_video_encode_bulk()
109 ret = uvc_video_encode_data(video, buf, mem, len); in uvc_video_encode_bulk()
111 video->payload_size += ret; in uvc_video_encode_bulk()
114 req->length = video->req_size - len; in uvc_video_encode_bulk()
115 req->zero = video->payload_size == video->max_payload_size; in uvc_video_encode_bulk()
117 if (buf->bytesused == video->queue.buf_used) { in uvc_video_encode_bulk()
118 video->queue.buf_used = 0; in uvc_video_encode_bulk()
121 video->fid ^= UVC_STREAM_FID; in uvc_video_encode_bulk()
124 video->payload_size = 0; in uvc_video_encode_bulk()
127 if (video->payload_size == video->max_payload_size || in uvc_video_encode_bulk()
128 video->queue.flags & UVC_QUEUE_DROP_INCOMPLETE || in uvc_video_encode_bulk()
129 buf->bytesused == video->queue.buf_used) in uvc_video_encode_bulk()
130 video->payload_size = 0; in uvc_video_encode_bulk()
134 uvc_video_encode_isoc_sg(struct usb_request *req, struct uvc_video *video, in uvc_video_encode_isoc_sg() argument
137 unsigned int pending = buf->bytesused - video->queue.buf_used; in uvc_video_encode_isoc_sg()
149 header_len = uvc_video_encode_header(video, buf, ureq->header, in uvc_video_encode_isoc_sg()
181 /* Assign the video data with header. */ in uvc_video_encode_isoc_sg()
187 video->queue.buf_used += req->length - header_len; in uvc_video_encode_isoc_sg()
189 if (buf->bytesused == video->queue.buf_used || !buf->sg || in uvc_video_encode_isoc_sg()
190 video->queue.flags & UVC_QUEUE_DROP_INCOMPLETE) { in uvc_video_encode_isoc_sg()
191 video->queue.buf_used = 0; in uvc_video_encode_isoc_sg()
195 video->fid ^= UVC_STREAM_FID; in uvc_video_encode_isoc_sg()
201 uvc_video_encode_isoc(struct usb_request *req, struct uvc_video *video, in uvc_video_encode_isoc() argument
210 ret = uvc_video_encode_header(video, buf, mem, len); in uvc_video_encode_isoc()
214 /* Process video data. */ in uvc_video_encode_isoc()
215 ret = uvc_video_encode_data(video, buf, mem, len); in uvc_video_encode_isoc()
220 if (buf->bytesused == video->queue.buf_used || in uvc_video_encode_isoc()
221 video->queue.flags & UVC_QUEUE_DROP_INCOMPLETE) { in uvc_video_encode_isoc()
222 video->queue.buf_used = 0; in uvc_video_encode_isoc()
225 video->fid ^= UVC_STREAM_FID; in uvc_video_encode_isoc()
256 static int uvcg_video_ep_queue(struct uvc_video *video, struct usb_request *req) in uvcg_video_ep_queue() argument
260 ret = usb_ep_queue(video->ep, req, GFP_ATOMIC); in uvcg_video_ep_queue()
262 uvcg_err(&video->uvc->func, "Failed to queue request (%d).\n", in uvcg_video_ep_queue()
266 if (video->ep->desc) { in uvcg_video_ep_queue()
268 if (usb_endpoint_xfer_bulk(video->ep->desc)) in uvcg_video_ep_queue()
269 usb_ep_set_halt(video->ep); in uvcg_video_ep_queue()
273 atomic_inc(&video->queued); in uvcg_video_ep_queue()
275 trace_uvcg_video_queue(req, atomic_read(&video->queued)); in uvcg_video_ep_queue()
280 /* This function must be called with video->req_lock held. */
281 static int uvcg_video_usb_req_queue(struct uvc_video *video, in uvcg_video_usb_req_queue() argument
284 bool is_bulk = video->max_payload_size; in uvcg_video_usb_req_queue()
287 if (!video->is_enabled) in uvcg_video_usb_req_queue()
301 * - When this is request is the last request for the video in uvcg_video_usb_req_queue()
302 * buffer, as we want to start sending the next video buffer in uvcg_video_usb_req_queue()
307 * indicated by video->uvc_num_requests), as a trade-off in uvcg_video_usb_req_queue()
310 if (list_empty(&video->req_free) || ureq->last_buf || in uvcg_video_usb_req_queue()
311 !(video->req_int_count % in uvcg_video_usb_req_queue()
312 min(DIV_ROUND_UP(video->uvc_num_requests, 4), UVCG_REQ_MAX_INT_COUNT))) { in uvcg_video_usb_req_queue()
313 video->req_int_count = 0; in uvcg_video_usb_req_queue()
318 video->req_int_count++; in uvcg_video_usb_req_queue()
319 return uvcg_video_ep_queue(video, req); in uvcg_video_usb_req_queue()
325 list = is_bulk ? &video->req_free : &video->req_ready; in uvcg_video_usb_req_queue()
334 struct uvc_video *video = ureq->video; in uvc_video_complete() local
335 struct uvc_video_queue *queue = &video->queue; in uvc_video_complete()
339 spin_lock_irqsave(&video->req_lock, flags); in uvc_video_complete()
340 atomic_dec(&video->queued); in uvc_video_complete()
341 if (!video->is_enabled) { in uvc_video_complete()
349 spin_unlock_irqrestore(&video->req_lock, flags); in uvc_video_complete()
355 spin_unlock_irqrestore(&video->req_lock, flags); in uvc_video_complete()
362 uvcg_dbg(&video->uvc->func, "VS request missed xfer.\n"); in uvc_video_complete()
368 uvcg_dbg(&video->uvc->func, "VS request cancelled.\n"); in uvc_video_complete()
373 uvcg_warn(&video->uvc->func, in uvc_video_complete()
385 spin_lock_irqsave(&video->req_lock, flags); in uvc_video_complete()
387 * Video stream might have been disabled while we were in uvc_video_complete()
392 if (!video->is_enabled) { in uvc_video_complete()
394 spin_unlock_irqrestore(&video->req_lock, flags); in uvc_video_complete()
400 list_add_tail(&req->list, &video->req_free); in uvc_video_complete()
412 queue_work(video->async_wq, &video->pump); in uvc_video_complete()
414 trace_uvcg_video_complete(req, atomic_read(&video->queued)); in uvc_video_complete()
416 spin_unlock_irqrestore(&video->req_lock, flags); in uvc_video_complete()
418 kthread_queue_work(video->kworker, &video->hw_submit); in uvc_video_complete()
423 struct uvc_video *video = container_of(work, struct uvc_video, hw_submit); in uvcg_video_hw_submit() local
424 bool is_bulk = video->max_payload_size; in uvcg_video_hw_submit()
430 if (!video->ep->enabled) in uvcg_video_hw_submit()
432 spin_lock_irqsave(&video->req_lock, flags); in uvcg_video_hw_submit()
443 if (!list_empty(&video->req_ready)) { in uvcg_video_hw_submit()
444 req = list_first_entry(&video->req_ready, in uvcg_video_hw_submit()
447 if (list_empty(&video->req_free) || in uvcg_video_hw_submit()
448 (atomic_read(&video->queued) > UVCG_REQ_MAX_ZERO_COUNT)) { in uvcg_video_hw_submit()
449 spin_unlock_irqrestore(&video->req_lock, flags); in uvcg_video_hw_submit()
453 req = list_first_entry(&video->req_free, struct usb_request, in uvcg_video_hw_submit()
464 ret = uvcg_video_usb_req_queue(video, req, !is_bulk); in uvcg_video_hw_submit()
471 list_add_tail(&req->list, &video->req_free); in uvcg_video_hw_submit()
475 queue_work(video->async_wq, &video->pump); in uvcg_video_hw_submit()
479 spin_unlock_irqrestore(&video->req_lock, flags); in uvcg_video_hw_submit()
484 uvc_video_free_requests(struct uvc_video *video) in uvc_video_free_requests() argument
488 list_for_each_entry_safe(ureq, temp, &video->ureqs, list) in uvc_video_free_requests()
489 uvc_video_free_request(ureq, video->ep); in uvc_video_free_requests()
491 INIT_LIST_HEAD(&video->ureqs); in uvc_video_free_requests()
492 INIT_LIST_HEAD(&video->req_free); in uvc_video_free_requests()
493 INIT_LIST_HEAD(&video->req_ready); in uvc_video_free_requests()
498 uvc_video_prep_requests(struct uvc_video *video) in uvc_video_prep_requests() argument
500 struct uvc_device *uvc = container_of(video, struct uvc_device, video); in uvc_video_prep_requests()
502 unsigned int interval_duration = video->ep->desc->bInterval * 1250; in uvc_video_prep_requests()
506 max_req_size = video->ep->maxpacket in uvc_video_prep_requests()
507 * max_t(unsigned int, video->ep->maxburst, 1) in uvc_video_prep_requests()
508 * (video->ep->mult); in uvc_video_prep_requests()
510 if (!usb_endpoint_xfer_isoc(video->ep->desc)) { in uvc_video_prep_requests()
511 video->req_size = max_req_size; in uvc_video_prep_requests()
512 video->reqs_per_frame = video->uvc_num_requests = in uvc_video_prep_requests()
513 DIV_ROUND_UP(video->imagesize, max_req_size); in uvc_video_prep_requests()
519 interval_duration = video->ep->desc->bInterval * 10000; in uvc_video_prep_requests()
521 nreq = DIV_ROUND_UP(video->interval, interval_duration); in uvc_video_prep_requests()
525 req_size = DIV_ROUND_UP(video->imagesize + header_size, nreq); in uvc_video_prep_requests()
534 video->req_size = req_size; in uvc_video_prep_requests()
542 video->uvc_num_requests = nreq + UVCG_REQ_MAX_ZERO_COUNT; in uvc_video_prep_requests()
543 video->reqs_per_frame = nreq; in uvc_video_prep_requests()
547 uvc_video_alloc_requests(struct uvc_video *video) in uvc_video_alloc_requests() argument
555 * - video->uvc_num_requests in uvc_video_alloc_requests()
556 * - video->req_size in uvc_video_alloc_requests()
558 uvc_video_prep_requests(video); in uvc_video_alloc_requests()
560 for (i = 0; i < video->uvc_num_requests; i++) { in uvc_video_alloc_requests()
567 list_add_tail(&ureq->list, &video->ureqs); in uvc_video_alloc_requests()
569 ureq->req_buffer = kmalloc(video->req_size, GFP_KERNEL); in uvc_video_alloc_requests()
573 ureq->req = usb_ep_alloc_request(video->ep, GFP_KERNEL); in uvc_video_alloc_requests()
581 ureq->video = video; in uvc_video_alloc_requests()
584 list_add_tail(&ureq->req->list, &video->req_free); in uvc_video_alloc_requests()
587 DIV_ROUND_UP(video->req_size - UVCG_REQUEST_HEADER_LEN, in uvc_video_alloc_requests()
594 uvc_video_free_requests(video); in uvc_video_alloc_requests()
599 * Video streaming
603 * uvcg_video_pump - Pump video data into the USB requests
606 * video data from the queued buffers.
610 struct uvc_video *video = container_of(work, struct uvc_video, pump); in uvcg_video_pump() local
611 struct uvc_video_queue *queue = &video->queue; in uvcg_video_pump()
612 /* video->max_payload_size is only set when using bulk transfer */ in uvcg_video_pump()
613 bool is_bulk = video->max_payload_size; in uvcg_video_pump()
620 if (!video->ep->enabled) in uvcg_video_pump()
627 spin_lock_irqsave(&video->req_lock, flags); in uvcg_video_pump()
628 if (!video->is_enabled || list_empty(&video->req_free)) { in uvcg_video_pump()
629 spin_unlock_irqrestore(&video->req_lock, flags); in uvcg_video_pump()
632 req = list_first_entry(&video->req_free, struct usb_request, in uvcg_video_pump()
635 spin_unlock_irqrestore(&video->req_lock, flags); in uvcg_video_pump()
638 * Retrieve the first available video buffer and fill the in uvcg_video_pump()
639 * request, protected by the video queue irqlock. in uvcg_video_pump()
645 * Either the queue has been disconnected or no video buffer in uvcg_video_pump()
653 video->encode(req, video, buf); in uvcg_video_pump()
657 spin_lock_irqsave(&video->req_lock, flags); in uvcg_video_pump()
664 ret = uvcg_video_usb_req_queue(video, req, is_bulk); in uvcg_video_pump()
665 spin_unlock_irqrestore(&video->req_lock, flags); in uvcg_video_pump()
672 spin_lock_irqsave(&video->req_lock, flags); in uvcg_video_pump()
673 if (video->is_enabled) in uvcg_video_pump()
674 list_add_tail(&req->list, &video->req_free); in uvcg_video_pump()
676 uvc_video_free_request(req->context, video->ep); in uvcg_video_pump()
677 spin_unlock_irqrestore(&video->req_lock, flags); in uvcg_video_pump()
681 * Disable the video stream
684 uvcg_video_disable(struct uvc_video *video) in uvcg_video_disable() argument
692 if (video->ep == NULL) { in uvcg_video_disable()
693 uvcg_info(&video->uvc->func, in uvcg_video_disable()
694 "Video disable failed, device is uninitialized.\n"); in uvcg_video_disable()
699 spin_lock_irqsave(&video->req_lock, flags); in uvcg_video_disable()
700 video->is_enabled = false; in uvcg_video_disable()
709 list_for_each_entry(ureq, &video->ureqs, list) { in uvcg_video_disable()
715 spin_unlock_irqrestore(&video->req_lock, flags); in uvcg_video_disable()
717 cancel_work_sync(&video->pump); in uvcg_video_disable()
718 uvcg_queue_cancel(&video->queue, 0); in uvcg_video_disable()
720 spin_lock_irqsave(&video->req_lock, flags); in uvcg_video_disable()
727 list_for_each_entry_safe(ureq, utemp, &video->ureqs, list) in uvcg_video_disable()
730 list_for_each_entry_safe(req, temp, &video->req_free, list) { in uvcg_video_disable()
732 uvc_video_free_request(req->context, video->ep); in uvcg_video_disable()
735 list_for_each_entry_safe(req, temp, &video->req_ready, list) { in uvcg_video_disable()
737 uvc_video_free_request(req->context, video->ep); in uvcg_video_disable()
740 INIT_LIST_HEAD(&video->ureqs); in uvcg_video_disable()
741 INIT_LIST_HEAD(&video->req_free); in uvcg_video_disable()
742 INIT_LIST_HEAD(&video->req_ready); in uvcg_video_disable()
743 spin_unlock_irqrestore(&video->req_lock, flags); in uvcg_video_disable()
746 * Return all the video buffers before disabling the queue. in uvcg_video_disable()
748 spin_lock_irqsave(&video->queue.irqlock, flags); in uvcg_video_disable()
751 uvcg_complete_buffer(&video->queue, buf); in uvcg_video_disable()
753 spin_unlock_irqrestore(&video->queue.irqlock, flags); in uvcg_video_disable()
755 uvcg_queue_enable(&video->queue, 0); in uvcg_video_disable()
760 * Enable the video stream.
762 int uvcg_video_enable(struct uvc_video *video) in uvcg_video_enable() argument
766 if (video->ep == NULL) { in uvcg_video_enable()
767 uvcg_info(&video->uvc->func, in uvcg_video_enable()
768 "Video enable failed, device is uninitialized.\n"); in uvcg_video_enable()
778 video->is_enabled = true; in uvcg_video_enable()
780 if ((ret = uvcg_queue_enable(&video->queue, 1)) < 0) in uvcg_video_enable()
783 if ((ret = uvc_video_alloc_requests(video)) < 0) in uvcg_video_enable()
786 if (video->max_payload_size) { in uvcg_video_enable()
787 video->encode = uvc_video_encode_bulk; in uvcg_video_enable()
788 video->payload_size = 0; in uvcg_video_enable()
790 video->encode = video->queue.use_sg ? in uvcg_video_enable()
793 video->req_int_count = 0; in uvcg_video_enable()
795 atomic_set(&video->queued, 0); in uvcg_video_enable()
797 kthread_queue_work(video->kworker, &video->hw_submit); in uvcg_video_enable()
798 queue_work(video->async_wq, &video->pump); in uvcg_video_enable()
804 * Initialize the UVC video stream.
806 int uvcg_video_init(struct uvc_video *video, struct uvc_device *uvc) in uvcg_video_init() argument
808 video->is_enabled = false; in uvcg_video_init()
809 INIT_LIST_HEAD(&video->ureqs); in uvcg_video_init()
810 INIT_LIST_HEAD(&video->req_free); in uvcg_video_init()
811 INIT_LIST_HEAD(&video->req_ready); in uvcg_video_init()
812 spin_lock_init(&video->req_lock); in uvcg_video_init()
813 INIT_WORK(&video->pump, uvcg_video_pump); in uvcg_video_init()
815 /* Allocate a work queue for asynchronous video pump handler. */ in uvcg_video_init()
816 video->async_wq = alloc_workqueue("uvcgadget", WQ_UNBOUND | WQ_HIGHPRI, 0); in uvcg_video_init()
817 if (!video->async_wq) in uvcg_video_init()
821 video->kworker = kthread_run_worker(0, "UVCG"); in uvcg_video_init()
822 if (IS_ERR(video->kworker)) { in uvcg_video_init()
823 uvcg_err(&video->uvc->func, "failed to create UVCG kworker\n"); in uvcg_video_init()
824 return PTR_ERR(video->kworker); in uvcg_video_init()
827 kthread_init_work(&video->hw_submit, uvcg_video_hw_submit); in uvcg_video_init()
829 sched_set_fifo(video->kworker->task); in uvcg_video_init()
831 video->uvc = uvc; in uvcg_video_init()
832 video->fcc = V4L2_PIX_FMT_YUYV; in uvcg_video_init()
833 video->bpp = 16; in uvcg_video_init()
834 video->width = 320; in uvcg_video_init()
835 video->height = 240; in uvcg_video_init()
836 video->imagesize = 320 * 240 * 2; in uvcg_video_init()
837 video->interval = 666666; in uvcg_video_init()
839 /* Initialize the video buffers queue. */ in uvcg_video_init()
840 uvcg_queue_init(&video->queue, uvc->v4l2_dev.dev->parent, in uvcg_video_init()
841 V4L2_BUF_TYPE_VIDEO_OUTPUT, &video->mutex); in uvcg_video_init()