1 // SPDX-License-Identifier: GPL-2.0+
3 * uvc_video.c -- USB Video Class Gadget driver
5 * Copyright (C) 2009-2010
6 * Laurent Pinchart (laurent.pinchart@ideasonboard.com)
9 #include <linux/kernel.h>
10 #include <linux/device.h>
11 #include <linux/errno.h>
12 #include <linux/usb/ch9.h>
13 #include <linux/usb/gadget.h>
14 #include <linux/usb/video.h>
15 #include <asm/unaligned.h>
17 #include <media/v4l2-dev.h>
20 #include "uvc_queue.h"
21 #include "uvc_video.h"
23 /* --------------------------------------------------------------------------
28 uvc_video_encode_header(struct uvc_video *video, struct uvc_buffer *buf,
31 struct uvc_device *uvc = container_of(video, struct uvc_device, video);
32 struct usb_composite_dev *cdev = uvc->func.config->cdev;
33 struct timespec64 ts = ns_to_timespec64(buf->buf.vb2_buf.timestamp);
36 data[1] = UVC_STREAM_EOH | video->fid;
38 if (video->queue.buf_used == 0 && ts.tv_sec) {
39 /* dwClockFrequency is 48 MHz */
40 u32 pts = ((u64)ts.tv_sec * USEC_PER_SEC + ts.tv_nsec / NSEC_PER_USEC) * 48;
42 data[1] |= UVC_STREAM_PTS;
43 put_unaligned_le32(pts, &data[pos]);
47 if (cdev->gadget->ops->get_frame) {
50 sof = usb_gadget_frame_number(cdev->gadget);
52 stc = ((u64)ts.tv_sec * USEC_PER_SEC + ts.tv_nsec / NSEC_PER_USEC) * 48;
54 data[1] |= UVC_STREAM_SCR;
55 put_unaligned_le32(stc, &data[pos]);
56 put_unaligned_le16(sof, &data[pos+4]);
62 if (buf->bytesused - video->queue.buf_used <= len - pos)
63 data[1] |= UVC_STREAM_EOF;
69 uvc_video_encode_data(struct uvc_video *video, struct uvc_buffer *buf,
72 struct uvc_video_queue *queue = &video->queue;
76 /* Copy video data to the USB buffer. */
77 mem = buf->mem + queue->buf_used;
78 nbytes = min((unsigned int)len, buf->bytesused - queue->buf_used);
80 memcpy(data, mem, nbytes);
81 queue->buf_used += nbytes;
87 uvc_video_encode_bulk(struct usb_request *req, struct uvc_video *video,
88 struct uvc_buffer *buf)
91 struct uvc_request *ureq = req->context;
92 int len = video->req_size;
95 /* Add a header at the beginning of the payload. */
96 if (video->payload_size == 0) {
97 ret = uvc_video_encode_header(video, buf, mem, len);
98 video->payload_size += ret;
103 /* Process video data. */
104 len = min((int)(video->max_payload_size - video->payload_size), len);
105 ret = uvc_video_encode_data(video, buf, mem, len);
107 video->payload_size += ret;
110 req->length = video->req_size - len;
111 req->zero = video->payload_size == video->max_payload_size;
113 if (buf->bytesused == video->queue.buf_used) {
114 video->queue.buf_used = 0;
115 buf->state = UVC_BUF_STATE_DONE;
116 list_del(&buf->queue);
117 video->fid ^= UVC_STREAM_FID;
118 ureq->last_buf = buf;
120 video->payload_size = 0;
123 if (video->payload_size == video->max_payload_size ||
124 video->queue.flags & UVC_QUEUE_DROP_INCOMPLETE ||
125 buf->bytesused == video->queue.buf_used)
126 video->payload_size = 0;
130 uvc_video_encode_isoc_sg(struct usb_request *req, struct uvc_video *video,
131 struct uvc_buffer *buf)
133 unsigned int pending = buf->bytesused - video->queue.buf_used;
134 struct uvc_request *ureq = req->context;
135 struct scatterlist *sg, *iter;
136 unsigned int len = video->req_size;
137 unsigned int sg_left, part = 0;
142 sg_init_table(sg, ureq->sgt.nents);
144 /* Init the header. */
145 header_len = uvc_video_encode_header(video, buf, ureq->header,
147 sg_set_buf(sg, ureq->header, header_len);
153 req->length = (len == pending) ?
154 len + header_len : video->req_size;
156 /* Init the pending sgs with payload */
159 for_each_sg(sg, iter, ureq->sgt.nents - 1, i) {
160 if (!len || !buf->sg || !buf->sg->length)
163 sg_left = buf->sg->length - buf->offset;
164 part = min_t(unsigned int, len, sg_left);
166 sg_set_page(iter, sg_page(buf->sg), part, buf->offset);
168 if (part == sg_left) {
170 buf->sg = sg_next(buf->sg);
177 /* Assign the video data with header. */
179 req->sg = ureq->sgt.sgl;
180 req->num_sgs = i + 1;
183 video->queue.buf_used += req->length - header_len;
185 if (buf->bytesused == video->queue.buf_used || !buf->sg ||
186 video->queue.flags & UVC_QUEUE_DROP_INCOMPLETE) {
187 video->queue.buf_used = 0;
188 buf->state = UVC_BUF_STATE_DONE;
190 list_del(&buf->queue);
191 video->fid ^= UVC_STREAM_FID;
192 ureq->last_buf = buf;
197 uvc_video_encode_isoc(struct usb_request *req, struct uvc_video *video,
198 struct uvc_buffer *buf)
200 void *mem = req->buf;
201 struct uvc_request *ureq = req->context;
202 int len = video->req_size;
205 /* Add the header. */
206 ret = uvc_video_encode_header(video, buf, mem, len);
210 /* Process video data. */
211 ret = uvc_video_encode_data(video, buf, mem, len);
214 req->length = video->req_size - len;
216 if (buf->bytesused == video->queue.buf_used ||
217 video->queue.flags & UVC_QUEUE_DROP_INCOMPLETE) {
218 video->queue.buf_used = 0;
219 buf->state = UVC_BUF_STATE_DONE;
220 list_del(&buf->queue);
221 video->fid ^= UVC_STREAM_FID;
222 ureq->last_buf = buf;
226 /* --------------------------------------------------------------------------
230 static int uvcg_video_ep_queue(struct uvc_video *video, struct usb_request *req)
234 ret = usb_ep_queue(video->ep, req, GFP_ATOMIC);
236 uvcg_err(&video->uvc->func, "Failed to queue request (%d).\n",
239 /* If the endpoint is disabled the descriptor may be NULL. */
240 if (video->ep->desc) {
241 /* Isochronous endpoints can't be halted. */
242 if (usb_endpoint_xfer_bulk(video->ep->desc))
243 usb_ep_set_halt(video->ep);
251 uvc_video_complete(struct usb_ep *ep, struct usb_request *req)
253 struct uvc_request *ureq = req->context;
254 struct uvc_video *video = ureq->video;
255 struct uvc_video_queue *queue = &video->queue;
256 struct uvc_device *uvc = video->uvc;
259 switch (req->status) {
264 uvcg_dbg(&video->uvc->func, "VS request missed xfer.\n");
265 queue->flags |= UVC_QUEUE_DROP_INCOMPLETE;
268 case -ESHUTDOWN: /* disconnect from host. */
269 uvcg_dbg(&video->uvc->func, "VS request cancelled.\n");
270 uvcg_queue_cancel(queue, 1);
274 uvcg_warn(&video->uvc->func,
275 "VS request completed with status %d.\n",
277 uvcg_queue_cancel(queue, 0);
280 if (ureq->last_buf) {
281 uvcg_complete_buffer(&video->queue, ureq->last_buf);
282 ureq->last_buf = NULL;
285 spin_lock_irqsave(&video->req_lock, flags);
286 list_add_tail(&req->list, &video->req_free);
287 spin_unlock_irqrestore(&video->req_lock, flags);
289 if (uvc->state == UVC_STATE_STREAMING)
290 queue_work(video->async_wq, &video->pump);
294 uvc_video_free_requests(struct uvc_video *video)
299 for (i = 0; i < video->uvc_num_requests; ++i) {
300 sg_free_table(&video->ureq[i].sgt);
302 if (video->ureq[i].req) {
303 usb_ep_free_request(video->ep, video->ureq[i].req);
304 video->ureq[i].req = NULL;
307 if (video->ureq[i].req_buffer) {
308 kfree(video->ureq[i].req_buffer);
309 video->ureq[i].req_buffer = NULL;
317 INIT_LIST_HEAD(&video->req_free);
323 uvc_video_alloc_requests(struct uvc_video *video)
325 unsigned int req_size;
329 BUG_ON(video->req_size);
331 req_size = video->ep->maxpacket
332 * max_t(unsigned int, video->ep->maxburst, 1)
335 video->ureq = kcalloc(video->uvc_num_requests, sizeof(struct uvc_request), GFP_KERNEL);
336 if (video->ureq == NULL)
339 for (i = 0; i < video->uvc_num_requests; ++i) {
340 video->ureq[i].req_buffer = kmalloc(req_size, GFP_KERNEL);
341 if (video->ureq[i].req_buffer == NULL)
344 video->ureq[i].req = usb_ep_alloc_request(video->ep, GFP_KERNEL);
345 if (video->ureq[i].req == NULL)
348 video->ureq[i].req->buf = video->ureq[i].req_buffer;
349 video->ureq[i].req->length = 0;
350 video->ureq[i].req->complete = uvc_video_complete;
351 video->ureq[i].req->context = &video->ureq[i];
352 video->ureq[i].video = video;
353 video->ureq[i].last_buf = NULL;
355 list_add_tail(&video->ureq[i].req->list, &video->req_free);
356 /* req_size/PAGE_SIZE + 1 for overruns and + 1 for header */
357 sg_alloc_table(&video->ureq[i].sgt,
358 DIV_ROUND_UP(req_size - UVCG_REQUEST_HEADER_LEN,
359 PAGE_SIZE) + 2, GFP_KERNEL);
362 video->req_size = req_size;
367 uvc_video_free_requests(video);
371 /* --------------------------------------------------------------------------
376 * uvcg_video_pump - Pump video data into the USB requests
378 * This function fills the available USB requests (listed in req_free) with
379 * video data from the queued buffers.
381 static void uvcg_video_pump(struct work_struct *work)
383 struct uvc_video *video = container_of(work, struct uvc_video, pump);
384 struct uvc_video_queue *queue = &video->queue;
385 /* video->max_payload_size is only set when using bulk transfer */
386 bool is_bulk = video->max_payload_size;
387 struct usb_request *req = NULL;
388 struct uvc_buffer *buf;
393 while (video->ep->enabled) {
395 * Retrieve the first available USB request, protected by the
398 spin_lock_irqsave(&video->req_lock, flags);
399 if (list_empty(&video->req_free)) {
400 spin_unlock_irqrestore(&video->req_lock, flags);
403 req = list_first_entry(&video->req_free, struct usb_request,
405 list_del(&req->list);
406 spin_unlock_irqrestore(&video->req_lock, flags);
409 * Retrieve the first available video buffer and fill the
410 * request, protected by the video queue irqlock.
412 spin_lock_irqsave(&queue->irqlock, flags);
413 buf = uvcg_queue_head(queue);
416 video->encode(req, video, buf);
417 buf_done = buf->state == UVC_BUF_STATE_DONE;
418 } else if (!(queue->flags & UVC_QUEUE_DISCONNECTED) && !is_bulk) {
420 * No video buffer available; the queue is still connected and
421 * we're transferring over ISOC. Queue a 0 length request to
422 * prevent missed ISOC transfers.
428 * Either the queue has been disconnected or no video buffer
429 * available for bulk transfer. Either way, stop processing
432 spin_unlock_irqrestore(&queue->irqlock, flags);
437 * With USB3 handling more requests at a higher speed, we can't
438 * afford to generate an interrupt for every request. Decide to
441 * - When no more requests are available in the free queue, as
442 * this may be our last chance to refill the endpoint's
445 * - When this is request is the last request for the video
446 * buffer, as we want to start sending the next video buffer
447 * ASAP in case it doesn't get started already in the next
448 * iteration of this loop.
450 * - Four times over the length of the requests queue (as
451 * indicated by video->uvc_num_requests), as a trade-off
452 * between latency and interrupt load.
454 if (list_empty(&video->req_free) || buf_done ||
455 !(video->req_int_count %
456 DIV_ROUND_UP(video->uvc_num_requests, 4))) {
457 video->req_int_count = 0;
458 req->no_interrupt = 0;
460 req->no_interrupt = 1;
463 /* Queue the USB request */
464 ret = uvcg_video_ep_queue(video, req);
465 spin_unlock_irqrestore(&queue->irqlock, flags);
468 uvcg_queue_cancel(queue, 0);
472 /* Endpoint now owns the request */
474 video->req_int_count++;
480 spin_lock_irqsave(&video->req_lock, flags);
481 list_add_tail(&req->list, &video->req_free);
482 spin_unlock_irqrestore(&video->req_lock, flags);
487 * Enable or disable the video stream.
489 int uvcg_video_enable(struct uvc_video *video, int enable)
494 if (video->ep == NULL) {
495 uvcg_info(&video->uvc->func,
496 "Video enable failed, device is uninitialized.\n");
501 cancel_work_sync(&video->pump);
502 uvcg_queue_cancel(&video->queue, 0);
504 for (i = 0; i < video->uvc_num_requests; ++i)
505 if (video->ureq && video->ureq[i].req)
506 usb_ep_dequeue(video->ep, video->ureq[i].req);
508 uvc_video_free_requests(video);
509 uvcg_queue_enable(&video->queue, 0);
513 if ((ret = uvcg_queue_enable(&video->queue, 1)) < 0)
516 if ((ret = uvc_video_alloc_requests(video)) < 0)
519 if (video->max_payload_size) {
520 video->encode = uvc_video_encode_bulk;
521 video->payload_size = 0;
523 video->encode = video->queue.use_sg ?
524 uvc_video_encode_isoc_sg : uvc_video_encode_isoc;
526 video->req_int_count = 0;
528 queue_work(video->async_wq, &video->pump);
534 * Initialize the UVC video stream.
536 int uvcg_video_init(struct uvc_video *video, struct uvc_device *uvc)
538 INIT_LIST_HEAD(&video->req_free);
539 spin_lock_init(&video->req_lock);
540 INIT_WORK(&video->pump, uvcg_video_pump);
542 /* Allocate a work queue for asynchronous video pump handler. */
543 video->async_wq = alloc_workqueue("uvcgadget", WQ_UNBOUND | WQ_HIGHPRI, 0);
544 if (!video->async_wq)
548 video->fcc = V4L2_PIX_FMT_YUYV;
552 video->imagesize = 320 * 240 * 2;
554 /* Initialize the video buffers queue. */
555 uvcg_queue_init(&video->queue, uvc->v4l2_dev.dev->parent,
556 V4L2_BUF_TYPE_VIDEO_OUTPUT, &video->mutex);