uvc_video.c 9.6 KB

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