uvc_video.c 9.7 KB

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