uvc_video.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  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. /*
  103. * I somehow feel that synchronisation won't be easy to achieve here. We have
  104. * three events that control USB requests submission:
  105. *
  106. * - USB request completion: the completion handler will resubmit the request
  107. * if a video buffer is available.
  108. *
  109. * - USB interface setting selection: in response to a SET_INTERFACE request,
  110. * the handler will start streaming if a video buffer is available and if
  111. * video is not currently streaming.
  112. *
  113. * - V4L2 buffer queueing: the driver will start streaming if video is not
  114. * currently streaming.
  115. *
  116. * Race conditions between those 3 events might lead to deadlocks or other
  117. * nasty side effects.
  118. *
  119. * The "video currently streaming" condition can't be detected by the irqqueue
  120. * being empty, as a request can still be in flight. A separate "queue paused"
  121. * flag is thus needed.
  122. *
  123. * The paused flag will be set when we try to retrieve the irqqueue head if the
  124. * queue is empty, and cleared when we queue a buffer.
  125. *
  126. * The USB request completion handler will get the buffer at the irqqueue head
  127. * under protection of the queue spinlock. If the queue is empty, the streaming
  128. * paused flag will be set. Right after releasing the spinlock a userspace
  129. * application can queue a buffer. The flag will then cleared, and the ioctl
  130. * handler will restart the video stream.
  131. */
  132. static void
  133. uvc_video_complete(struct usb_ep *ep, struct usb_request *req)
  134. {
  135. struct uvc_video *video = req->context;
  136. struct uvc_video_queue *queue = &video->queue;
  137. struct uvc_buffer *buf;
  138. unsigned long flags;
  139. int ret;
  140. switch (req->status) {
  141. case 0:
  142. break;
  143. case -ESHUTDOWN: /* disconnect from host. */
  144. printk(KERN_DEBUG "VS request cancelled.\n");
  145. uvcg_queue_cancel(queue, 1);
  146. goto requeue;
  147. default:
  148. printk(KERN_INFO "VS request completed with status %d.\n",
  149. req->status);
  150. uvcg_queue_cancel(queue, 0);
  151. goto requeue;
  152. }
  153. spin_lock_irqsave(&video->queue.irqlock, flags);
  154. buf = uvcg_queue_head(&video->queue);
  155. if (buf == NULL) {
  156. spin_unlock_irqrestore(&video->queue.irqlock, flags);
  157. goto requeue;
  158. }
  159. video->encode(req, video, buf);
  160. if ((ret = usb_ep_queue(ep, req, GFP_ATOMIC)) < 0) {
  161. printk(KERN_INFO "Failed to queue request (%d).\n", ret);
  162. usb_ep_set_halt(ep);
  163. spin_unlock_irqrestore(&video->queue.irqlock, flags);
  164. uvcg_queue_cancel(queue, 0);
  165. goto requeue;
  166. }
  167. spin_unlock_irqrestore(&video->queue.irqlock, flags);
  168. return;
  169. requeue:
  170. spin_lock_irqsave(&video->req_lock, flags);
  171. list_add_tail(&req->list, &video->req_free);
  172. spin_unlock_irqrestore(&video->req_lock, flags);
  173. }
  174. static int
  175. uvc_video_free_requests(struct uvc_video *video)
  176. {
  177. unsigned int i;
  178. for (i = 0; i < UVC_NUM_REQUESTS; ++i) {
  179. if (video->req[i]) {
  180. usb_ep_free_request(video->ep, video->req[i]);
  181. video->req[i] = NULL;
  182. }
  183. if (video->req_buffer[i]) {
  184. kfree(video->req_buffer[i]);
  185. video->req_buffer[i] = NULL;
  186. }
  187. }
  188. INIT_LIST_HEAD(&video->req_free);
  189. video->req_size = 0;
  190. return 0;
  191. }
  192. static int
  193. uvc_video_alloc_requests(struct uvc_video *video)
  194. {
  195. unsigned int req_size;
  196. unsigned int i;
  197. int ret = -ENOMEM;
  198. BUG_ON(video->req_size);
  199. req_size = video->ep->maxpacket
  200. * max_t(unsigned int, video->ep->maxburst, 1)
  201. * (video->ep->mult);
  202. for (i = 0; i < UVC_NUM_REQUESTS; ++i) {
  203. video->req_buffer[i] = kmalloc(req_size, GFP_KERNEL);
  204. if (video->req_buffer[i] == NULL)
  205. goto error;
  206. video->req[i] = usb_ep_alloc_request(video->ep, GFP_KERNEL);
  207. if (video->req[i] == NULL)
  208. goto error;
  209. video->req[i]->buf = video->req_buffer[i];
  210. video->req[i]->length = 0;
  211. video->req[i]->complete = uvc_video_complete;
  212. video->req[i]->context = video;
  213. list_add_tail(&video->req[i]->list, &video->req_free);
  214. }
  215. video->req_size = req_size;
  216. return 0;
  217. error:
  218. uvc_video_free_requests(video);
  219. return ret;
  220. }
  221. /* --------------------------------------------------------------------------
  222. * Video streaming
  223. */
  224. /*
  225. * uvcg_video_pump - Pump video data into the USB requests
  226. *
  227. * This function fills the available USB requests (listed in req_free) with
  228. * video data from the queued buffers.
  229. */
  230. int uvcg_video_pump(struct uvc_video *video)
  231. {
  232. struct uvc_video_queue *queue = &video->queue;
  233. struct usb_request *req;
  234. struct uvc_buffer *buf;
  235. unsigned long flags;
  236. int ret;
  237. /* FIXME TODO Race between uvcg_video_pump and requests completion
  238. * handler ???
  239. */
  240. while (1) {
  241. /* Retrieve the first available USB request, protected by the
  242. * request lock.
  243. */
  244. spin_lock_irqsave(&video->req_lock, flags);
  245. if (list_empty(&video->req_free)) {
  246. spin_unlock_irqrestore(&video->req_lock, flags);
  247. return 0;
  248. }
  249. req = list_first_entry(&video->req_free, struct usb_request,
  250. list);
  251. list_del(&req->list);
  252. spin_unlock_irqrestore(&video->req_lock, flags);
  253. /* Retrieve the first available video buffer and fill the
  254. * request, protected by the video queue irqlock.
  255. */
  256. spin_lock_irqsave(&queue->irqlock, flags);
  257. buf = uvcg_queue_head(queue);
  258. if (buf == NULL) {
  259. spin_unlock_irqrestore(&queue->irqlock, flags);
  260. break;
  261. }
  262. video->encode(req, video, buf);
  263. /* Queue the USB request */
  264. ret = usb_ep_queue(video->ep, req, GFP_ATOMIC);
  265. if (ret < 0) {
  266. printk(KERN_INFO "Failed to queue request (%d)\n", ret);
  267. usb_ep_set_halt(video->ep);
  268. spin_unlock_irqrestore(&queue->irqlock, flags);
  269. uvcg_queue_cancel(queue, 0);
  270. break;
  271. }
  272. spin_unlock_irqrestore(&queue->irqlock, flags);
  273. }
  274. spin_lock_irqsave(&video->req_lock, flags);
  275. list_add_tail(&req->list, &video->req_free);
  276. spin_unlock_irqrestore(&video->req_lock, flags);
  277. return 0;
  278. }
  279. /*
  280. * Enable or disable the video stream.
  281. */
  282. int uvcg_video_enable(struct uvc_video *video, int enable)
  283. {
  284. unsigned int i;
  285. int ret;
  286. if (video->ep == NULL) {
  287. printk(KERN_INFO "Video enable failed, device is "
  288. "uninitialized.\n");
  289. return -ENODEV;
  290. }
  291. if (!enable) {
  292. for (i = 0; i < UVC_NUM_REQUESTS; ++i)
  293. if (video->req[i])
  294. usb_ep_dequeue(video->ep, video->req[i]);
  295. uvc_video_free_requests(video);
  296. uvcg_queue_enable(&video->queue, 0);
  297. return 0;
  298. }
  299. if ((ret = uvcg_queue_enable(&video->queue, 1)) < 0)
  300. return ret;
  301. if ((ret = uvc_video_alloc_requests(video)) < 0)
  302. return ret;
  303. if (video->max_payload_size) {
  304. video->encode = uvc_video_encode_bulk;
  305. video->payload_size = 0;
  306. } else
  307. video->encode = uvc_video_encode_isoc;
  308. return uvcg_video_pump(video);
  309. }
  310. /*
  311. * Initialize the UVC video stream.
  312. */
  313. int uvcg_video_init(struct uvc_video *video)
  314. {
  315. INIT_LIST_HEAD(&video->req_free);
  316. spin_lock_init(&video->req_lock);
  317. video->fcc = V4L2_PIX_FMT_YUYV;
  318. video->bpp = 16;
  319. video->width = 320;
  320. video->height = 240;
  321. video->imagesize = 320 * 240 * 2;
  322. /* Initialize the video buffers queue. */
  323. uvcg_queue_init(&video->queue, V4L2_BUF_TYPE_VIDEO_OUTPUT,
  324. &video->mutex);
  325. return 0;
  326. }