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 * This program is free software; you can redistribute it and/or modify 9 * it under the terms of the GNU General Public License as published by 10 * the Free Software Foundation; either version 2 of the License, or 11 * (at your option) any later version. 12 */ 13 14 #include <linux/kernel.h> 15 #include <linux/device.h> 16 #include <linux/errno.h> 17 #include <linux/usb/ch9.h> 18 #include <linux/usb/gadget.h> 19 #include <linux/usb/video.h> 20 21 #include <media/v4l2-dev.h> 22 23 #include "uvc.h" 24 #include "uvc_queue.h" 25 #include "uvc_video.h" 26 27 /* -------------------------------------------------------------------------- 28 * Video codecs 29 */ 30 31 static int 32 uvc_video_encode_header(struct uvc_video *video, struct uvc_buffer *buf, 33 u8 *data, int len) 34 { 35 data[0] = 2; 36 data[1] = UVC_STREAM_EOH | video->fid; 37 38 if (buf->bytesused - video->queue.buf_used <= len - 2) 39 data[1] |= UVC_STREAM_EOF; 40 41 return 2; 42 } 43 44 static int 45 uvc_video_encode_data(struct uvc_video *video, struct uvc_buffer *buf, 46 u8 *data, int len) 47 { 48 struct uvc_video_queue *queue = &video->queue; 49 unsigned int nbytes; 50 void *mem; 51 52 /* Copy video data to the USB buffer. */ 53 mem = buf->mem + queue->buf_used; 54 nbytes = min((unsigned int)len, buf->bytesused - queue->buf_used); 55 56 memcpy(data, mem, nbytes); 57 queue->buf_used += nbytes; 58 59 return nbytes; 60 } 61 62 static void 63 uvc_video_encode_bulk(struct usb_request *req, struct uvc_video *video, 64 struct uvc_buffer *buf) 65 { 66 void *mem = req->buf; 67 int len = video->req_size; 68 int ret; 69 70 /* Add a header at the beginning of the payload. */ 71 if (video->payload_size == 0) { 72 ret = uvc_video_encode_header(video, buf, mem, len); 73 video->payload_size += ret; 74 mem += ret; 75 len -= ret; 76 } 77 78 /* Process video data. */ 79 len = min((int)(video->max_payload_size - video->payload_size), len); 80 ret = uvc_video_encode_data(video, buf, mem, len); 81 82 video->payload_size += ret; 83 len -= ret; 84 85 req->length = video->req_size - len; 86 req->zero = video->payload_size == video->max_payload_size; 87 88 if (buf->bytesused == video->queue.buf_used) { 89 video->queue.buf_used = 0; 90 buf->state = UVC_BUF_STATE_DONE; 91 uvcg_queue_next_buffer(&video->queue, buf); 92 video->fid ^= UVC_STREAM_FID; 93 94 video->payload_size = 0; 95 } 96 97 if (video->payload_size == video->max_payload_size || 98 buf->bytesused == video->queue.buf_used) 99 video->payload_size = 0; 100 } 101 102 static void 103 uvc_video_encode_isoc(struct usb_request *req, struct uvc_video *video, 104 struct uvc_buffer *buf) 105 { 106 void *mem = req->buf; 107 int len = video->req_size; 108 int ret; 109 110 /* Add the header. */ 111 ret = uvc_video_encode_header(video, buf, mem, len); 112 mem += ret; 113 len -= ret; 114 115 /* Process video data. */ 116 ret = uvc_video_encode_data(video, buf, mem, len); 117 len -= ret; 118 119 req->length = video->req_size - len; 120 121 if (buf->bytesused == video->queue.buf_used) { 122 video->queue.buf_used = 0; 123 buf->state = UVC_BUF_STATE_DONE; 124 uvcg_queue_next_buffer(&video->queue, buf); 125 video->fid ^= UVC_STREAM_FID; 126 } 127 } 128 129 /* -------------------------------------------------------------------------- 130 * Request handling 131 */ 132 133 /* 134 * I somehow feel that synchronisation won't be easy to achieve here. We have 135 * three events that control USB requests submission: 136 * 137 * - USB request completion: the completion handler will resubmit the request 138 * if a video buffer is available. 139 * 140 * - USB interface setting selection: in response to a SET_INTERFACE request, 141 * the handler will start streaming if a video buffer is available and if 142 * video is not currently streaming. 143 * 144 * - V4L2 buffer queueing: the driver will start streaming if video is not 145 * currently streaming. 146 * 147 * Race conditions between those 3 events might lead to deadlocks or other 148 * nasty side effects. 149 * 150 * The "video currently streaming" condition can't be detected by the irqqueue 151 * being empty, as a request can still be in flight. A separate "queue paused" 152 * flag is thus needed. 153 * 154 * The paused flag will be set when we try to retrieve the irqqueue head if the 155 * queue is empty, and cleared when we queue a buffer. 156 * 157 * The USB request completion handler will get the buffer at the irqqueue head 158 * under protection of the queue spinlock. If the queue is empty, the streaming 159 * paused flag will be set. Right after releasing the spinlock a userspace 160 * application can queue a buffer. The flag will then cleared, and the ioctl 161 * handler will restart the video stream. 162 */ 163 static void 164 uvc_video_complete(struct usb_ep *ep, struct usb_request *req) 165 { 166 struct uvc_video *video = req->context; 167 struct uvc_video_queue *queue = &video->queue; 168 struct uvc_buffer *buf; 169 unsigned long flags; 170 int ret; 171 172 switch (req->status) { 173 case 0: 174 break; 175 176 case -ESHUTDOWN: /* disconnect from host. */ 177 printk(KERN_DEBUG "VS request cancelled.\n"); 178 uvcg_queue_cancel(queue, 1); 179 goto requeue; 180 181 default: 182 printk(KERN_INFO "VS request completed with status %d.\n", 183 req->status); 184 uvcg_queue_cancel(queue, 0); 185 goto requeue; 186 } 187 188 spin_lock_irqsave(&video->queue.irqlock, flags); 189 buf = uvcg_queue_head(&video->queue); 190 if (buf == NULL) { 191 spin_unlock_irqrestore(&video->queue.irqlock, flags); 192 goto requeue; 193 } 194 195 video->encode(req, video, buf); 196 197 if ((ret = usb_ep_queue(ep, req, GFP_ATOMIC)) < 0) { 198 printk(KERN_INFO "Failed to queue request (%d).\n", ret); 199 usb_ep_set_halt(ep); 200 spin_unlock_irqrestore(&video->queue.irqlock, flags); 201 uvcg_queue_cancel(queue, 0); 202 goto requeue; 203 } 204 spin_unlock_irqrestore(&video->queue.irqlock, flags); 205 206 return; 207 208 requeue: 209 spin_lock_irqsave(&video->req_lock, flags); 210 list_add_tail(&req->list, &video->req_free); 211 spin_unlock_irqrestore(&video->req_lock, flags); 212 } 213 214 static int 215 uvc_video_free_requests(struct uvc_video *video) 216 { 217 unsigned int i; 218 219 for (i = 0; i < UVC_NUM_REQUESTS; ++i) { 220 if (video->req[i]) { 221 usb_ep_free_request(video->ep, video->req[i]); 222 video->req[i] = NULL; 223 } 224 225 if (video->req_buffer[i]) { 226 kfree(video->req_buffer[i]); 227 video->req_buffer[i] = NULL; 228 } 229 } 230 231 INIT_LIST_HEAD(&video->req_free); 232 video->req_size = 0; 233 return 0; 234 } 235 236 static int 237 uvc_video_alloc_requests(struct uvc_video *video) 238 { 239 unsigned int req_size; 240 unsigned int i; 241 int ret = -ENOMEM; 242 243 BUG_ON(video->req_size); 244 245 req_size = video->ep->maxpacket 246 * max_t(unsigned int, video->ep->maxburst, 1) 247 * (video->ep->mult); 248 249 for (i = 0; i < UVC_NUM_REQUESTS; ++i) { 250 video->req_buffer[i] = kmalloc(req_size, GFP_KERNEL); 251 if (video->req_buffer[i] == NULL) 252 goto error; 253 254 video->req[i] = usb_ep_alloc_request(video->ep, GFP_KERNEL); 255 if (video->req[i] == NULL) 256 goto error; 257 258 video->req[i]->buf = video->req_buffer[i]; 259 video->req[i]->length = 0; 260 video->req[i]->complete = uvc_video_complete; 261 video->req[i]->context = video; 262 263 list_add_tail(&video->req[i]->list, &video->req_free); 264 } 265 266 video->req_size = req_size; 267 268 return 0; 269 270 error: 271 uvc_video_free_requests(video); 272 return ret; 273 } 274 275 /* -------------------------------------------------------------------------- 276 * Video streaming 277 */ 278 279 /* 280 * uvcg_video_pump - Pump video data into the USB requests 281 * 282 * This function fills the available USB requests (listed in req_free) with 283 * video data from the queued buffers. 284 */ 285 int uvcg_video_pump(struct uvc_video *video) 286 { 287 struct uvc_video_queue *queue = &video->queue; 288 struct usb_request *req; 289 struct uvc_buffer *buf; 290 unsigned long flags; 291 int ret; 292 293 /* FIXME TODO Race between uvcg_video_pump and requests completion 294 * handler ??? 295 */ 296 297 while (1) { 298 /* Retrieve the first available USB request, protected by the 299 * request lock. 300 */ 301 spin_lock_irqsave(&video->req_lock, flags); 302 if (list_empty(&video->req_free)) { 303 spin_unlock_irqrestore(&video->req_lock, flags); 304 return 0; 305 } 306 req = list_first_entry(&video->req_free, struct usb_request, 307 list); 308 list_del(&req->list); 309 spin_unlock_irqrestore(&video->req_lock, flags); 310 311 /* Retrieve the first available video buffer and fill the 312 * request, protected by the video queue irqlock. 313 */ 314 spin_lock_irqsave(&queue->irqlock, flags); 315 buf = uvcg_queue_head(queue); 316 if (buf == NULL) { 317 spin_unlock_irqrestore(&queue->irqlock, flags); 318 break; 319 } 320 321 video->encode(req, video, buf); 322 323 /* Queue the USB request */ 324 ret = usb_ep_queue(video->ep, req, GFP_ATOMIC); 325 if (ret < 0) { 326 printk(KERN_INFO "Failed to queue request (%d)\n", ret); 327 usb_ep_set_halt(video->ep); 328 spin_unlock_irqrestore(&queue->irqlock, flags); 329 uvcg_queue_cancel(queue, 0); 330 break; 331 } 332 spin_unlock_irqrestore(&queue->irqlock, flags); 333 } 334 335 spin_lock_irqsave(&video->req_lock, flags); 336 list_add_tail(&req->list, &video->req_free); 337 spin_unlock_irqrestore(&video->req_lock, flags); 338 return 0; 339 } 340 341 /* 342 * Enable or disable the video stream. 343 */ 344 int uvcg_video_enable(struct uvc_video *video, int enable) 345 { 346 unsigned int i; 347 int ret; 348 349 if (video->ep == NULL) { 350 printk(KERN_INFO "Video enable failed, device is " 351 "uninitialized.\n"); 352 return -ENODEV; 353 } 354 355 if (!enable) { 356 for (i = 0; i < UVC_NUM_REQUESTS; ++i) 357 if (video->req[i]) 358 usb_ep_dequeue(video->ep, video->req[i]); 359 360 uvc_video_free_requests(video); 361 uvcg_queue_enable(&video->queue, 0); 362 return 0; 363 } 364 365 if ((ret = uvcg_queue_enable(&video->queue, 1)) < 0) 366 return ret; 367 368 if ((ret = uvc_video_alloc_requests(video)) < 0) 369 return ret; 370 371 if (video->max_payload_size) { 372 video->encode = uvc_video_encode_bulk; 373 video->payload_size = 0; 374 } else 375 video->encode = uvc_video_encode_isoc; 376 377 return uvcg_video_pump(video); 378 } 379 380 /* 381 * Initialize the UVC video stream. 382 */ 383 int uvcg_video_init(struct uvc_video *video) 384 { 385 INIT_LIST_HEAD(&video->req_free); 386 spin_lock_init(&video->req_lock); 387 388 video->fcc = V4L2_PIX_FMT_YUYV; 389 video->bpp = 16; 390 video->width = 320; 391 video->height = 240; 392 video->imagesize = 320 * 240 * 2; 393 394 /* Initialize the video buffers queue. */ 395 uvcg_queue_init(&video->queue, V4L2_BUF_TYPE_VIDEO_OUTPUT, 396 &video->mutex); 397 return 0; 398 } 399 400