1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  *	uvc_video.c  --  USB Video Class Gadget driver
4  *
5  *	Copyright (C) 2009-2010
6  *	    Laurent Pinchart (laurent.pinchart@ideasonboard.com)
7  */
8 
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>
16 
17 #include <media/v4l2-dev.h>
18 
19 #include "uvc.h"
20 #include "uvc_queue.h"
21 #include "uvc_video.h"
22 
23 /* --------------------------------------------------------------------------
24  * Video codecs
25  */
26 
27 static int
28 uvc_video_encode_header(struct uvc_video *video, struct uvc_buffer *buf,
29 		u8 *data, int len)
30 {
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);
34 	int pos = 2;
35 
36 	data[1] = UVC_STREAM_EOH | video->fid;
37 
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;
41 
42 		data[1] |= UVC_STREAM_PTS;
43 		put_unaligned_le32(pts, &data[pos]);
44 		pos += 4;
45 	}
46 
47 	if (cdev->gadget->ops->get_frame) {
48 		u32 sof, stc;
49 
50 		sof = usb_gadget_frame_number(cdev->gadget);
51 		ktime_get_ts64(&ts);
52 		stc = ((u64)ts.tv_sec * USEC_PER_SEC + ts.tv_nsec / NSEC_PER_USEC) * 48;
53 
54 		data[1] |= UVC_STREAM_SCR;
55 		put_unaligned_le32(stc, &data[pos]);
56 		put_unaligned_le16(sof, &data[pos+4]);
57 		pos += 6;
58 	}
59 
60 	data[0] = pos;
61 
62 	if (buf->bytesused - video->queue.buf_used <= len - pos)
63 		data[1] |= UVC_STREAM_EOF;
64 
65 	return pos;
66 }
67 
68 static int
69 uvc_video_encode_data(struct uvc_video *video, struct uvc_buffer *buf,
70 		u8 *data, int len)
71 {
72 	struct uvc_video_queue *queue = &video->queue;
73 	unsigned int nbytes;
74 	void *mem;
75 
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);
79 
80 	memcpy(data, mem, nbytes);
81 	queue->buf_used += nbytes;
82 
83 	return nbytes;
84 }
85 
86 static void
87 uvc_video_encode_bulk(struct usb_request *req, struct uvc_video *video,
88 		struct uvc_buffer *buf)
89 {
90 	void *mem = req->buf;
91 	int len = video->req_size;
92 	int ret;
93 
94 	/* Add a header at the beginning of the payload. */
95 	if (video->payload_size == 0) {
96 		ret = uvc_video_encode_header(video, buf, mem, len);
97 		video->payload_size += ret;
98 		mem += ret;
99 		len -= ret;
100 	}
101 
102 	/* Process video data. */
103 	len = min((int)(video->max_payload_size - video->payload_size), len);
104 	ret = uvc_video_encode_data(video, buf, mem, len);
105 
106 	video->payload_size += ret;
107 	len -= ret;
108 
109 	req->length = video->req_size - len;
110 	req->zero = video->payload_size == video->max_payload_size;
111 
112 	if (buf->bytesused == video->queue.buf_used) {
113 		video->queue.buf_used = 0;
114 		buf->state = UVC_BUF_STATE_DONE;
115 		uvcg_queue_next_buffer(&video->queue, buf);
116 		video->fid ^= UVC_STREAM_FID;
117 
118 		video->payload_size = 0;
119 	}
120 
121 	if (video->payload_size == video->max_payload_size ||
122 	    buf->bytesused == video->queue.buf_used)
123 		video->payload_size = 0;
124 }
125 
126 static void
127 uvc_video_encode_isoc_sg(struct usb_request *req, struct uvc_video *video,
128 		struct uvc_buffer *buf)
129 {
130 	unsigned int pending = buf->bytesused - video->queue.buf_used;
131 	struct uvc_request *ureq = req->context;
132 	struct scatterlist *sg, *iter;
133 	unsigned int len = video->req_size;
134 	unsigned int sg_left, part = 0;
135 	unsigned int i;
136 	int header_len;
137 
138 	sg = ureq->sgt.sgl;
139 	sg_init_table(sg, ureq->sgt.nents);
140 
141 	/* Init the header. */
142 	header_len = uvc_video_encode_header(video, buf, ureq->header,
143 				      video->req_size);
144 	sg_set_buf(sg, ureq->header, header_len);
145 	len -= header_len;
146 
147 	if (pending <= len)
148 		len = pending;
149 
150 	req->length = (len == pending) ?
151 		len + header_len : video->req_size;
152 
153 	/* Init the pending sgs with payload */
154 	sg = sg_next(sg);
155 
156 	for_each_sg(sg, iter, ureq->sgt.nents - 1, i) {
157 		if (!len || !buf->sg)
158 			break;
159 
160 		sg_left = sg_dma_len(buf->sg) - buf->offset;
161 		part = min_t(unsigned int, len, sg_left);
162 
163 		sg_set_page(iter, sg_page(buf->sg), part, buf->offset);
164 
165 		if (part == sg_left) {
166 			buf->offset = 0;
167 			buf->sg = sg_next(buf->sg);
168 		} else {
169 			buf->offset += part;
170 		}
171 		len -= part;
172 	}
173 
174 	/* Assign the video data with header. */
175 	req->buf = NULL;
176 	req->sg	= ureq->sgt.sgl;
177 	req->num_sgs = i + 1;
178 
179 	req->length -= len;
180 	video->queue.buf_used += req->length - header_len;
181 
182 	if (buf->bytesused == video->queue.buf_used || !buf->sg) {
183 		video->queue.buf_used = 0;
184 		buf->state = UVC_BUF_STATE_DONE;
185 		buf->offset = 0;
186 		uvcg_queue_next_buffer(&video->queue, buf);
187 		video->fid ^= UVC_STREAM_FID;
188 	}
189 }
190 
191 static void
192 uvc_video_encode_isoc(struct usb_request *req, struct uvc_video *video,
193 		struct uvc_buffer *buf)
194 {
195 	void *mem = req->buf;
196 	int len = video->req_size;
197 	int ret;
198 
199 	/* Add the header. */
200 	ret = uvc_video_encode_header(video, buf, mem, len);
201 	mem += ret;
202 	len -= ret;
203 
204 	/* Process video data. */
205 	ret = uvc_video_encode_data(video, buf, mem, len);
206 	len -= ret;
207 
208 	req->length = video->req_size - len;
209 
210 	if (buf->bytesused == video->queue.buf_used) {
211 		video->queue.buf_used = 0;
212 		buf->state = UVC_BUF_STATE_DONE;
213 		uvcg_queue_next_buffer(&video->queue, buf);
214 		video->fid ^= UVC_STREAM_FID;
215 	}
216 }
217 
218 /* --------------------------------------------------------------------------
219  * Request handling
220  */
221 
222 static int uvcg_video_ep_queue(struct uvc_video *video, struct usb_request *req)
223 {
224 	int ret;
225 
226 	ret = usb_ep_queue(video->ep, req, GFP_ATOMIC);
227 	if (ret < 0) {
228 		uvcg_err(&video->uvc->func, "Failed to queue request (%d).\n",
229 			 ret);
230 
231 		/* If the endpoint is disabled the descriptor may be NULL. */
232 		if (video->ep->desc) {
233 			/* Isochronous endpoints can't be halted. */
234 			if (usb_endpoint_xfer_bulk(video->ep->desc))
235 				usb_ep_set_halt(video->ep);
236 		}
237 	}
238 
239 	return ret;
240 }
241 
242 static void
243 uvc_video_complete(struct usb_ep *ep, struct usb_request *req)
244 {
245 	struct uvc_request *ureq = req->context;
246 	struct uvc_video *video = ureq->video;
247 	struct uvc_video_queue *queue = &video->queue;
248 	struct uvc_device *uvc = video->uvc;
249 	unsigned long flags;
250 
251 	switch (req->status) {
252 	case 0:
253 		break;
254 
255 	case -ESHUTDOWN:	/* disconnect from host. */
256 		uvcg_dbg(&video->uvc->func, "VS request cancelled.\n");
257 		uvcg_queue_cancel(queue, 1);
258 		break;
259 
260 	default:
261 		uvcg_info(&video->uvc->func,
262 			  "VS request completed with status %d.\n",
263 			  req->status);
264 		uvcg_queue_cancel(queue, 0);
265 	}
266 
267 	spin_lock_irqsave(&video->req_lock, flags);
268 	list_add_tail(&req->list, &video->req_free);
269 	spin_unlock_irqrestore(&video->req_lock, flags);
270 
271 	if (uvc->state == UVC_STATE_STREAMING)
272 		schedule_work(&video->pump);
273 }
274 
275 static int
276 uvc_video_free_requests(struct uvc_video *video)
277 {
278 	unsigned int i;
279 
280 	if (video->ureq) {
281 		for (i = 0; i < video->uvc_num_requests; ++i) {
282 			sg_free_table(&video->ureq[i].sgt);
283 
284 			if (video->ureq[i].req) {
285 				usb_ep_free_request(video->ep, video->ureq[i].req);
286 				video->ureq[i].req = NULL;
287 			}
288 
289 			if (video->ureq[i].req_buffer) {
290 				kfree(video->ureq[i].req_buffer);
291 				video->ureq[i].req_buffer = NULL;
292 			}
293 		}
294 
295 		kfree(video->ureq);
296 		video->ureq = NULL;
297 	}
298 
299 	INIT_LIST_HEAD(&video->req_free);
300 	video->req_size = 0;
301 	return 0;
302 }
303 
304 static int
305 uvc_video_alloc_requests(struct uvc_video *video)
306 {
307 	unsigned int req_size;
308 	unsigned int i;
309 	int ret = -ENOMEM;
310 
311 	BUG_ON(video->req_size);
312 
313 	req_size = video->ep->maxpacket
314 		 * max_t(unsigned int, video->ep->maxburst, 1)
315 		 * (video->ep->mult);
316 
317 	video->ureq = kcalloc(video->uvc_num_requests, sizeof(struct uvc_request), GFP_KERNEL);
318 	if (video->ureq == NULL)
319 		return -ENOMEM;
320 
321 	for (i = 0; i < video->uvc_num_requests; ++i) {
322 		video->ureq[i].req_buffer = kmalloc(req_size, GFP_KERNEL);
323 		if (video->ureq[i].req_buffer == NULL)
324 			goto error;
325 
326 		video->ureq[i].req = usb_ep_alloc_request(video->ep, GFP_KERNEL);
327 		if (video->ureq[i].req == NULL)
328 			goto error;
329 
330 		video->ureq[i].req->buf = video->ureq[i].req_buffer;
331 		video->ureq[i].req->length = 0;
332 		video->ureq[i].req->complete = uvc_video_complete;
333 		video->ureq[i].req->context = &video->ureq[i];
334 		video->ureq[i].video = video;
335 
336 		list_add_tail(&video->ureq[i].req->list, &video->req_free);
337 		/* req_size/PAGE_SIZE + 1 for overruns and + 1 for header */
338 		sg_alloc_table(&video->ureq[i].sgt,
339 			       DIV_ROUND_UP(req_size - UVCG_REQUEST_HEADER_LEN,
340 					    PAGE_SIZE) + 2, GFP_KERNEL);
341 	}
342 
343 	video->req_size = req_size;
344 
345 	return 0;
346 
347 error:
348 	uvc_video_free_requests(video);
349 	return ret;
350 }
351 
352 /* --------------------------------------------------------------------------
353  * Video streaming
354  */
355 
356 /*
357  * uvcg_video_pump - Pump video data into the USB requests
358  *
359  * This function fills the available USB requests (listed in req_free) with
360  * video data from the queued buffers.
361  */
362 static void uvcg_video_pump(struct work_struct *work)
363 {
364 	struct uvc_video *video = container_of(work, struct uvc_video, pump);
365 	struct uvc_video_queue *queue = &video->queue;
366 	struct usb_request *req = NULL;
367 	struct uvc_buffer *buf;
368 	unsigned long flags;
369 	int ret;
370 
371 	while (video->ep->enabled) {
372 		/* Retrieve the first available USB request, protected by the
373 		 * request lock.
374 		 */
375 		spin_lock_irqsave(&video->req_lock, flags);
376 		if (list_empty(&video->req_free)) {
377 			spin_unlock_irqrestore(&video->req_lock, flags);
378 			return;
379 		}
380 		req = list_first_entry(&video->req_free, struct usb_request,
381 					list);
382 		list_del(&req->list);
383 		spin_unlock_irqrestore(&video->req_lock, flags);
384 
385 		/* Retrieve the first available video buffer and fill the
386 		 * request, protected by the video queue irqlock.
387 		 */
388 		spin_lock_irqsave(&queue->irqlock, flags);
389 		buf = uvcg_queue_head(queue);
390 		if (buf == NULL) {
391 			spin_unlock_irqrestore(&queue->irqlock, flags);
392 			break;
393 		}
394 
395 		video->encode(req, video, buf);
396 
397 		/* With usb3 we have more requests. This will decrease the
398 		 * interrupt load to a quarter but also catches the corner
399 		 * cases, which needs to be handled */
400 		if (list_empty(&video->req_free) ||
401 		    buf->state == UVC_BUF_STATE_DONE ||
402 		    !(video->req_int_count %
403 		       DIV_ROUND_UP(video->uvc_num_requests, 4))) {
404 			video->req_int_count = 0;
405 			req->no_interrupt = 0;
406 		} else {
407 			req->no_interrupt = 1;
408 		}
409 
410 		/* Queue the USB request */
411 		ret = uvcg_video_ep_queue(video, req);
412 		spin_unlock_irqrestore(&queue->irqlock, flags);
413 
414 		if (ret < 0) {
415 			uvcg_queue_cancel(queue, 0);
416 			break;
417 		}
418 		video->req_int_count++;
419 	}
420 
421 	if (!req)
422 		return;
423 
424 	spin_lock_irqsave(&video->req_lock, flags);
425 	list_add_tail(&req->list, &video->req_free);
426 	spin_unlock_irqrestore(&video->req_lock, flags);
427 	return;
428 }
429 
430 /*
431  * Enable or disable the video stream.
432  */
433 int uvcg_video_enable(struct uvc_video *video, int enable)
434 {
435 	unsigned int i;
436 	int ret;
437 
438 	if (video->ep == NULL) {
439 		uvcg_info(&video->uvc->func,
440 			  "Video enable failed, device is uninitialized.\n");
441 		return -ENODEV;
442 	}
443 
444 	if (!enable) {
445 		cancel_work_sync(&video->pump);
446 		uvcg_queue_cancel(&video->queue, 0);
447 
448 		for (i = 0; i < video->uvc_num_requests; ++i)
449 			if (video->ureq && video->ureq[i].req)
450 				usb_ep_dequeue(video->ep, video->ureq[i].req);
451 
452 		uvc_video_free_requests(video);
453 		uvcg_queue_enable(&video->queue, 0);
454 		return 0;
455 	}
456 
457 	if ((ret = uvcg_queue_enable(&video->queue, 1)) < 0)
458 		return ret;
459 
460 	if ((ret = uvc_video_alloc_requests(video)) < 0)
461 		return ret;
462 
463 	if (video->max_payload_size) {
464 		video->encode = uvc_video_encode_bulk;
465 		video->payload_size = 0;
466 	} else
467 		video->encode = video->queue.use_sg ?
468 			uvc_video_encode_isoc_sg : uvc_video_encode_isoc;
469 
470 	video->req_int_count = 0;
471 
472 	schedule_work(&video->pump);
473 
474 	return ret;
475 }
476 
477 /*
478  * Initialize the UVC video stream.
479  */
480 int uvcg_video_init(struct uvc_video *video, struct uvc_device *uvc)
481 {
482 	INIT_LIST_HEAD(&video->req_free);
483 	spin_lock_init(&video->req_lock);
484 	INIT_WORK(&video->pump, uvcg_video_pump);
485 
486 	video->uvc = uvc;
487 	video->fcc = V4L2_PIX_FMT_YUYV;
488 	video->bpp = 16;
489 	video->width = 320;
490 	video->height = 240;
491 	video->imagesize = 320 * 240 * 2;
492 
493 	/* Initialize the video buffers queue. */
494 	uvcg_queue_init(&video->queue, uvc->v4l2_dev.dev->parent,
495 			V4L2_BUF_TYPE_VIDEO_OUTPUT, &video->mutex);
496 	return 0;
497 }
498 
499