uvc_queue.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * uvc_queue.c -- USB Video Class driver - Buffers management
  4. *
  5. * Copyright (C) 2005-2010
  6. * Laurent Pinchart (laurent.pinchart@ideasonboard.com)
  7. */
  8. #include <linux/atomic.h>
  9. #include <linux/kernel.h>
  10. #include <linux/mm.h>
  11. #include <linux/list.h>
  12. #include <linux/module.h>
  13. #include <linux/usb.h>
  14. #include <linux/videodev2.h>
  15. #include <linux/vmalloc.h>
  16. #include <linux/wait.h>
  17. #include <media/videobuf2-v4l2.h>
  18. #include <media/videobuf2-vmalloc.h>
  19. #include "uvcvideo.h"
  20. /* ------------------------------------------------------------------------
  21. * Video buffers queue management.
  22. *
  23. * Video queues is initialized by uvc_queue_init(). The function performs
  24. * basic initialization of the uvc_video_queue struct and never fails.
  25. *
  26. * Video buffers are managed by videobuf2. The driver uses a mutex to protect
  27. * the videobuf2 queue operations by serializing calls to videobuf2 and a
  28. * spinlock to protect the IRQ queue that holds the buffers to be processed by
  29. * the driver.
  30. */
  31. static inline struct uvc_buffer *uvc_vbuf_to_buffer(struct vb2_v4l2_buffer *buf)
  32. {
  33. return container_of(buf, struct uvc_buffer, buf);
  34. }
  35. /*
  36. * Return all queued buffers to videobuf2 in the requested state.
  37. *
  38. * This function must be called with the queue spinlock held.
  39. */
  40. static void __uvc_queue_return_buffers(struct uvc_video_queue *queue,
  41. enum uvc_buffer_state state)
  42. {
  43. enum vb2_buffer_state vb2_state = state == UVC_BUF_STATE_ERROR
  44. ? VB2_BUF_STATE_ERROR
  45. : VB2_BUF_STATE_QUEUED;
  46. lockdep_assert_held(&queue->irqlock);
  47. while (!list_empty(&queue->irqqueue)) {
  48. struct uvc_buffer *buf = list_first_entry(&queue->irqqueue,
  49. struct uvc_buffer,
  50. queue);
  51. list_del(&buf->queue);
  52. buf->state = state;
  53. vb2_buffer_done(&buf->buf.vb2_buf, vb2_state);
  54. }
  55. }
  56. static void uvc_queue_return_buffers(struct uvc_video_queue *queue,
  57. enum uvc_buffer_state state)
  58. {
  59. spin_lock_irq(&queue->irqlock);
  60. __uvc_queue_return_buffers(queue, state);
  61. spin_unlock_irq(&queue->irqlock);
  62. }
  63. /* -----------------------------------------------------------------------------
  64. * videobuf2 queue operations
  65. */
  66. static int uvc_queue_setup(struct vb2_queue *vq,
  67. unsigned int *nbuffers, unsigned int *nplanes,
  68. unsigned int sizes[], struct device *alloc_devs[])
  69. {
  70. struct uvc_video_queue *queue = vb2_get_drv_priv(vq);
  71. struct uvc_streaming *stream;
  72. unsigned int size;
  73. switch (vq->type) {
  74. case V4L2_BUF_TYPE_META_CAPTURE:
  75. size = UVC_METADATA_BUF_SIZE;
  76. break;
  77. default:
  78. stream = uvc_queue_to_stream(queue);
  79. size = stream->ctrl.dwMaxVideoFrameSize;
  80. break;
  81. }
  82. /*
  83. * When called with plane sizes, validate them. The driver supports
  84. * single planar formats only, and requires buffers to be large enough
  85. * to store a complete frame.
  86. */
  87. if (*nplanes)
  88. return *nplanes != 1 || sizes[0] < size ? -EINVAL : 0;
  89. *nplanes = 1;
  90. sizes[0] = size;
  91. return 0;
  92. }
  93. static int uvc_buffer_prepare(struct vb2_buffer *vb)
  94. {
  95. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  96. struct uvc_video_queue *queue = vb2_get_drv_priv(vb->vb2_queue);
  97. struct uvc_buffer *buf = uvc_vbuf_to_buffer(vbuf);
  98. if (vb->type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
  99. vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0)) {
  100. uvc_dbg(uvc_queue_to_stream(queue)->dev, CAPTURE,
  101. "[E] Bytes used out of bounds\n");
  102. return -EINVAL;
  103. }
  104. if (unlikely(queue->flags & UVC_QUEUE_DISCONNECTED))
  105. return -ENODEV;
  106. buf->state = UVC_BUF_STATE_QUEUED;
  107. buf->error = 0;
  108. buf->mem = vb2_plane_vaddr(vb, 0);
  109. buf->length = vb2_plane_size(vb, 0);
  110. if (vb->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
  111. buf->bytesused = 0;
  112. else
  113. buf->bytesused = vb2_get_plane_payload(vb, 0);
  114. return 0;
  115. }
  116. static void uvc_buffer_queue(struct vb2_buffer *vb)
  117. {
  118. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  119. struct uvc_video_queue *queue = vb2_get_drv_priv(vb->vb2_queue);
  120. struct uvc_buffer *buf = uvc_vbuf_to_buffer(vbuf);
  121. unsigned long flags;
  122. spin_lock_irqsave(&queue->irqlock, flags);
  123. if (likely(!(queue->flags & UVC_QUEUE_DISCONNECTED))) {
  124. kref_init(&buf->ref);
  125. list_add_tail(&buf->queue, &queue->irqqueue);
  126. } else {
  127. /*
  128. * If the device is disconnected return the buffer to userspace
  129. * directly. The next QBUF call will fail with -ENODEV.
  130. */
  131. buf->state = UVC_BUF_STATE_ERROR;
  132. vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
  133. }
  134. spin_unlock_irqrestore(&queue->irqlock, flags);
  135. }
  136. static void uvc_buffer_finish(struct vb2_buffer *vb)
  137. {
  138. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  139. struct uvc_video_queue *queue = vb2_get_drv_priv(vb->vb2_queue);
  140. struct uvc_streaming *stream = uvc_queue_to_stream(queue);
  141. struct uvc_buffer *buf = uvc_vbuf_to_buffer(vbuf);
  142. if (vb->state == VB2_BUF_STATE_DONE)
  143. uvc_video_clock_update(stream, vbuf, buf);
  144. }
  145. static int uvc_start_streaming_video(struct vb2_queue *vq, unsigned int count)
  146. {
  147. struct uvc_video_queue *queue = vb2_get_drv_priv(vq);
  148. struct uvc_streaming *stream = uvc_queue_to_stream(queue);
  149. int ret;
  150. lockdep_assert_irqs_enabled();
  151. ret = uvc_pm_get(stream->dev);
  152. if (ret)
  153. goto err_buffers;
  154. queue->buf_used = 0;
  155. ret = uvc_video_start_streaming(stream);
  156. if (ret)
  157. goto err_pm;
  158. return 0;
  159. err_pm:
  160. uvc_pm_put(stream->dev);
  161. err_buffers:
  162. uvc_queue_return_buffers(queue, UVC_BUF_STATE_QUEUED);
  163. return ret;
  164. }
  165. static void uvc_stop_streaming_video(struct vb2_queue *vq)
  166. {
  167. struct uvc_video_queue *queue = vb2_get_drv_priv(vq);
  168. struct uvc_streaming *stream = uvc_queue_to_stream(queue);
  169. lockdep_assert_irqs_enabled();
  170. uvc_video_stop_streaming(uvc_queue_to_stream(queue));
  171. uvc_pm_put(stream->dev);
  172. uvc_queue_return_buffers(queue, UVC_BUF_STATE_ERROR);
  173. }
  174. static void uvc_stop_streaming_meta(struct vb2_queue *vq)
  175. {
  176. struct uvc_video_queue *queue = vb2_get_drv_priv(vq);
  177. lockdep_assert_irqs_enabled();
  178. uvc_queue_return_buffers(queue, UVC_BUF_STATE_ERROR);
  179. }
  180. static const struct vb2_ops uvc_queue_qops = {
  181. .queue_setup = uvc_queue_setup,
  182. .buf_prepare = uvc_buffer_prepare,
  183. .buf_queue = uvc_buffer_queue,
  184. .buf_finish = uvc_buffer_finish,
  185. .start_streaming = uvc_start_streaming_video,
  186. .stop_streaming = uvc_stop_streaming_video,
  187. };
  188. static const struct vb2_ops uvc_meta_queue_qops = {
  189. .queue_setup = uvc_queue_setup,
  190. .buf_prepare = uvc_buffer_prepare,
  191. .buf_queue = uvc_buffer_queue,
  192. /*
  193. * .start_streaming is not provided here. Metadata relies on video
  194. * streaming being active. If video isn't streaming, then no metadata
  195. * will arrive either.
  196. */
  197. .stop_streaming = uvc_stop_streaming_meta,
  198. };
  199. int uvc_queue_init(struct uvc_video_queue *queue, enum v4l2_buf_type type)
  200. {
  201. int ret;
  202. queue->queue.type = type;
  203. queue->queue.io_modes = VB2_MMAP | VB2_USERPTR;
  204. queue->queue.drv_priv = queue;
  205. queue->queue.buf_struct_size = sizeof(struct uvc_buffer);
  206. queue->queue.mem_ops = &vb2_vmalloc_memops;
  207. queue->queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC
  208. | V4L2_BUF_FLAG_TSTAMP_SRC_SOE;
  209. queue->queue.lock = &queue->mutex;
  210. switch (type) {
  211. case V4L2_BUF_TYPE_META_CAPTURE:
  212. queue->queue.ops = &uvc_meta_queue_qops;
  213. break;
  214. default:
  215. queue->queue.io_modes |= VB2_DMABUF;
  216. queue->queue.ops = &uvc_queue_qops;
  217. break;
  218. }
  219. ret = vb2_queue_init(&queue->queue);
  220. if (ret)
  221. return ret;
  222. mutex_init(&queue->mutex);
  223. spin_lock_init(&queue->irqlock);
  224. INIT_LIST_HEAD(&queue->irqqueue);
  225. return 0;
  226. }
  227. /* -----------------------------------------------------------------------------
  228. *
  229. */
  230. /*
  231. * Cancel the video buffers queue.
  232. *
  233. * Cancelling the queue marks all buffers on the irq queue as erroneous,
  234. * wakes them up and removes them from the queue.
  235. *
  236. * If the disconnect parameter is set, further calls to uvc_queue_buffer will
  237. * fail with -ENODEV.
  238. *
  239. * This function acquires the irq spinlock and can be called from interrupt
  240. * context.
  241. */
  242. void uvc_queue_cancel(struct uvc_video_queue *queue, int disconnect)
  243. {
  244. unsigned long flags;
  245. spin_lock_irqsave(&queue->irqlock, flags);
  246. __uvc_queue_return_buffers(queue, UVC_BUF_STATE_ERROR);
  247. /*
  248. * This must be protected by the irqlock spinlock to avoid race
  249. * conditions between uvc_buffer_queue and the disconnection event that
  250. * could result in an interruptible wait in uvc_dequeue_buffer. Do not
  251. * blindly replace this logic by checking for the UVC_QUEUE_DISCONNECTED
  252. * state outside the queue code.
  253. */
  254. if (disconnect)
  255. queue->flags |= UVC_QUEUE_DISCONNECTED;
  256. spin_unlock_irqrestore(&queue->irqlock, flags);
  257. }
  258. /*
  259. * uvc_queue_get_current_buffer: Obtain the current working output buffer
  260. *
  261. * Buffers may span multiple packets, and even URBs, therefore the active buffer
  262. * remains on the queue until the EOF marker.
  263. */
  264. static struct uvc_buffer *
  265. __uvc_queue_get_current_buffer(struct uvc_video_queue *queue)
  266. {
  267. if (list_empty(&queue->irqqueue))
  268. return NULL;
  269. return list_first_entry(&queue->irqqueue, struct uvc_buffer, queue);
  270. }
  271. struct uvc_buffer *uvc_queue_get_current_buffer(struct uvc_video_queue *queue)
  272. {
  273. struct uvc_buffer *nextbuf;
  274. unsigned long flags;
  275. spin_lock_irqsave(&queue->irqlock, flags);
  276. nextbuf = __uvc_queue_get_current_buffer(queue);
  277. spin_unlock_irqrestore(&queue->irqlock, flags);
  278. return nextbuf;
  279. }
  280. /*
  281. * uvc_queue_buffer_requeue: Requeue a buffer on our internal irqqueue
  282. *
  283. * Reuse a buffer through our internal queue without the need to 'prepare'.
  284. * The buffer will be returned to userspace through the uvc_buffer_queue call if
  285. * the device has been disconnected.
  286. */
  287. static void uvc_queue_buffer_requeue(struct uvc_video_queue *queue,
  288. struct uvc_buffer *buf)
  289. {
  290. buf->error = 0;
  291. buf->state = UVC_BUF_STATE_QUEUED;
  292. buf->bytesused = 0;
  293. vb2_set_plane_payload(&buf->buf.vb2_buf, 0, 0);
  294. uvc_buffer_queue(&buf->buf.vb2_buf);
  295. }
  296. static void uvc_queue_buffer_complete(struct kref *ref)
  297. {
  298. struct uvc_buffer *buf = container_of(ref, struct uvc_buffer, ref);
  299. struct vb2_buffer *vb = &buf->buf.vb2_buf;
  300. struct uvc_video_queue *queue = vb2_get_drv_priv(vb->vb2_queue);
  301. if (buf->error && !uvc_no_drop_param) {
  302. uvc_queue_buffer_requeue(queue, buf);
  303. return;
  304. }
  305. buf->state = buf->error ? UVC_BUF_STATE_ERROR : UVC_BUF_STATE_DONE;
  306. vb2_set_plane_payload(&buf->buf.vb2_buf, 0, buf->bytesused);
  307. vb2_buffer_done(&buf->buf.vb2_buf, buf->error ? VB2_BUF_STATE_ERROR :
  308. VB2_BUF_STATE_DONE);
  309. }
  310. /*
  311. * Release a reference on the buffer. Complete the buffer when the last
  312. * reference is released.
  313. */
  314. void uvc_queue_buffer_release(struct uvc_buffer *buf)
  315. {
  316. kref_put(&buf->ref, uvc_queue_buffer_complete);
  317. }
  318. /*
  319. * Remove this buffer from the queue. Lifetime will persist while async actions
  320. * are still running (if any), and uvc_queue_buffer_release will give the buffer
  321. * back to VB2 when all users have completed.
  322. */
  323. struct uvc_buffer *uvc_queue_next_buffer(struct uvc_video_queue *queue,
  324. struct uvc_buffer *buf)
  325. {
  326. struct uvc_buffer *nextbuf;
  327. unsigned long flags;
  328. spin_lock_irqsave(&queue->irqlock, flags);
  329. list_del(&buf->queue);
  330. nextbuf = __uvc_queue_get_current_buffer(queue);
  331. spin_unlock_irqrestore(&queue->irqlock, flags);
  332. uvc_queue_buffer_release(buf);
  333. return nextbuf;
  334. }