uvc_queue.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. // SPDX-License-Identifier: GPL-2.0+
  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/v4l2-common.h>
  18. #include <media/videobuf2-vmalloc.h>
  19. #include "uvc.h"
  20. /* ------------------------------------------------------------------------
  21. * Video buffers queue management.
  22. *
  23. * Video queues is initialized by uvcg_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. /* -----------------------------------------------------------------------------
  32. * videobuf2 queue operations
  33. */
  34. static int uvc_queue_setup(struct vb2_queue *vq,
  35. unsigned int *nbuffers, unsigned int *nplanes,
  36. unsigned int sizes[], struct device *alloc_devs[])
  37. {
  38. struct uvc_video_queue *queue = vb2_get_drv_priv(vq);
  39. struct uvc_video *video = container_of(queue, struct uvc_video, queue);
  40. if (*nbuffers > UVC_MAX_VIDEO_BUFFERS)
  41. *nbuffers = UVC_MAX_VIDEO_BUFFERS;
  42. *nplanes = 1;
  43. sizes[0] = video->imagesize;
  44. return 0;
  45. }
  46. static int uvc_buffer_prepare(struct vb2_buffer *vb)
  47. {
  48. struct uvc_video_queue *queue = vb2_get_drv_priv(vb->vb2_queue);
  49. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  50. struct uvc_buffer *buf = container_of(vbuf, struct uvc_buffer, buf);
  51. if (vb->type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
  52. vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0)) {
  53. uvc_trace(UVC_TRACE_CAPTURE, "[E] Bytes used out of bounds.\n");
  54. return -EINVAL;
  55. }
  56. if (unlikely(queue->flags & UVC_QUEUE_DISCONNECTED))
  57. return -ENODEV;
  58. buf->state = UVC_BUF_STATE_QUEUED;
  59. buf->mem = vb2_plane_vaddr(vb, 0);
  60. buf->length = vb2_plane_size(vb, 0);
  61. if (vb->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  62. buf->bytesused = 0;
  63. else
  64. buf->bytesused = vb2_get_plane_payload(vb, 0);
  65. return 0;
  66. }
  67. static void uvc_buffer_queue(struct vb2_buffer *vb)
  68. {
  69. struct uvc_video_queue *queue = vb2_get_drv_priv(vb->vb2_queue);
  70. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  71. struct uvc_buffer *buf = container_of(vbuf, struct uvc_buffer, buf);
  72. unsigned long flags;
  73. spin_lock_irqsave(&queue->irqlock, flags);
  74. if (likely(!(queue->flags & UVC_QUEUE_DISCONNECTED))) {
  75. list_add_tail(&buf->queue, &queue->irqqueue);
  76. } else {
  77. /* If the device is disconnected return the buffer to userspace
  78. * directly. The next QBUF call will fail with -ENODEV.
  79. */
  80. buf->state = UVC_BUF_STATE_ERROR;
  81. vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
  82. }
  83. spin_unlock_irqrestore(&queue->irqlock, flags);
  84. }
  85. static struct vb2_ops uvc_queue_qops = {
  86. .queue_setup = uvc_queue_setup,
  87. .buf_prepare = uvc_buffer_prepare,
  88. .buf_queue = uvc_buffer_queue,
  89. .wait_prepare = vb2_ops_wait_prepare,
  90. .wait_finish = vb2_ops_wait_finish,
  91. };
  92. int uvcg_queue_init(struct uvc_video_queue *queue, enum v4l2_buf_type type,
  93. struct mutex *lock)
  94. {
  95. int ret;
  96. queue->queue.type = type;
  97. queue->queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
  98. queue->queue.drv_priv = queue;
  99. queue->queue.buf_struct_size = sizeof(struct uvc_buffer);
  100. queue->queue.ops = &uvc_queue_qops;
  101. queue->queue.lock = lock;
  102. queue->queue.mem_ops = &vb2_vmalloc_memops;
  103. queue->queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC
  104. | V4L2_BUF_FLAG_TSTAMP_SRC_EOF;
  105. ret = vb2_queue_init(&queue->queue);
  106. if (ret)
  107. return ret;
  108. spin_lock_init(&queue->irqlock);
  109. INIT_LIST_HEAD(&queue->irqqueue);
  110. queue->flags = 0;
  111. return 0;
  112. }
  113. /*
  114. * Free the video buffers.
  115. */
  116. void uvcg_free_buffers(struct uvc_video_queue *queue)
  117. {
  118. vb2_queue_release(&queue->queue);
  119. }
  120. /*
  121. * Allocate the video buffers.
  122. */
  123. int uvcg_alloc_buffers(struct uvc_video_queue *queue,
  124. struct v4l2_requestbuffers *rb)
  125. {
  126. int ret;
  127. ret = vb2_reqbufs(&queue->queue, rb);
  128. return ret ? ret : rb->count;
  129. }
  130. int uvcg_query_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf)
  131. {
  132. return vb2_querybuf(&queue->queue, buf);
  133. }
  134. int uvcg_queue_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf)
  135. {
  136. unsigned long flags;
  137. int ret;
  138. ret = vb2_qbuf(&queue->queue, buf);
  139. if (ret < 0)
  140. return ret;
  141. spin_lock_irqsave(&queue->irqlock, flags);
  142. ret = (queue->flags & UVC_QUEUE_PAUSED) != 0;
  143. queue->flags &= ~UVC_QUEUE_PAUSED;
  144. spin_unlock_irqrestore(&queue->irqlock, flags);
  145. return ret;
  146. }
  147. /*
  148. * Dequeue a video buffer. If nonblocking is false, block until a buffer is
  149. * available.
  150. */
  151. int uvcg_dequeue_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf,
  152. int nonblocking)
  153. {
  154. return vb2_dqbuf(&queue->queue, buf, nonblocking);
  155. }
  156. /*
  157. * Poll the video queue.
  158. *
  159. * This function implements video queue polling and is intended to be used by
  160. * the device poll handler.
  161. */
  162. __poll_t uvcg_queue_poll(struct uvc_video_queue *queue, struct file *file,
  163. poll_table *wait)
  164. {
  165. return vb2_poll(&queue->queue, file, wait);
  166. }
  167. int uvcg_queue_mmap(struct uvc_video_queue *queue, struct vm_area_struct *vma)
  168. {
  169. return vb2_mmap(&queue->queue, vma);
  170. }
  171. #ifndef CONFIG_MMU
  172. /*
  173. * Get unmapped area.
  174. *
  175. * NO-MMU arch need this function to make mmap() work correctly.
  176. */
  177. unsigned long uvcg_queue_get_unmapped_area(struct uvc_video_queue *queue,
  178. unsigned long pgoff)
  179. {
  180. return vb2_get_unmapped_area(&queue->queue, 0, 0, pgoff, 0);
  181. }
  182. #endif
  183. /*
  184. * Cancel the video buffers queue.
  185. *
  186. * Cancelling the queue marks all buffers on the irq queue as erroneous,
  187. * wakes them up and removes them from the queue.
  188. *
  189. * If the disconnect parameter is set, further calls to uvc_queue_buffer will
  190. * fail with -ENODEV.
  191. *
  192. * This function acquires the irq spinlock and can be called from interrupt
  193. * context.
  194. */
  195. void uvcg_queue_cancel(struct uvc_video_queue *queue, int disconnect)
  196. {
  197. struct uvc_buffer *buf;
  198. unsigned long flags;
  199. spin_lock_irqsave(&queue->irqlock, flags);
  200. while (!list_empty(&queue->irqqueue)) {
  201. buf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
  202. queue);
  203. list_del(&buf->queue);
  204. buf->state = UVC_BUF_STATE_ERROR;
  205. vb2_buffer_done(&buf->buf.vb2_buf, VB2_BUF_STATE_ERROR);
  206. }
  207. /* This must be protected by the irqlock spinlock to avoid race
  208. * conditions between uvc_queue_buffer and the disconnection event that
  209. * could result in an interruptible wait in uvc_dequeue_buffer. Do not
  210. * blindly replace this logic by checking for the UVC_DEV_DISCONNECTED
  211. * state outside the queue code.
  212. */
  213. if (disconnect)
  214. queue->flags |= UVC_QUEUE_DISCONNECTED;
  215. spin_unlock_irqrestore(&queue->irqlock, flags);
  216. }
  217. /*
  218. * Enable or disable the video buffers queue.
  219. *
  220. * The queue must be enabled before starting video acquisition and must be
  221. * disabled after stopping it. This ensures that the video buffers queue
  222. * state can be properly initialized before buffers are accessed from the
  223. * interrupt handler.
  224. *
  225. * Enabling the video queue initializes parameters (such as sequence number,
  226. * sync pattern, ...). If the queue is already enabled, return -EBUSY.
  227. *
  228. * Disabling the video queue cancels the queue and removes all buffers from
  229. * the main queue.
  230. *
  231. * This function can't be called from interrupt context. Use
  232. * uvcg_queue_cancel() instead.
  233. */
  234. int uvcg_queue_enable(struct uvc_video_queue *queue, int enable)
  235. {
  236. unsigned long flags;
  237. int ret = 0;
  238. if (enable) {
  239. ret = vb2_streamon(&queue->queue, queue->queue.type);
  240. if (ret < 0)
  241. return ret;
  242. queue->sequence = 0;
  243. queue->buf_used = 0;
  244. } else {
  245. ret = vb2_streamoff(&queue->queue, queue->queue.type);
  246. if (ret < 0)
  247. return ret;
  248. spin_lock_irqsave(&queue->irqlock, flags);
  249. INIT_LIST_HEAD(&queue->irqqueue);
  250. /*
  251. * FIXME: We need to clear the DISCONNECTED flag to ensure that
  252. * applications will be able to queue buffers for the next
  253. * streaming run. However, clearing it here doesn't guarantee
  254. * that the device will be reconnected in the meantime.
  255. */
  256. queue->flags &= ~UVC_QUEUE_DISCONNECTED;
  257. spin_unlock_irqrestore(&queue->irqlock, flags);
  258. }
  259. return ret;
  260. }
  261. /* called with &queue_irqlock held.. */
  262. struct uvc_buffer *uvcg_queue_next_buffer(struct uvc_video_queue *queue,
  263. struct uvc_buffer *buf)
  264. {
  265. struct uvc_buffer *nextbuf;
  266. if ((queue->flags & UVC_QUEUE_DROP_INCOMPLETE) &&
  267. buf->length != buf->bytesused) {
  268. buf->state = UVC_BUF_STATE_QUEUED;
  269. vb2_set_plane_payload(&buf->buf.vb2_buf, 0, 0);
  270. return buf;
  271. }
  272. list_del(&buf->queue);
  273. if (!list_empty(&queue->irqqueue))
  274. nextbuf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
  275. queue);
  276. else
  277. nextbuf = NULL;
  278. buf->buf.field = V4L2_FIELD_NONE;
  279. buf->buf.sequence = queue->sequence++;
  280. buf->buf.vb2_buf.timestamp = ktime_get_ns();
  281. vb2_set_plane_payload(&buf->buf.vb2_buf, 0, buf->bytesused);
  282. vb2_buffer_done(&buf->buf.vb2_buf, VB2_BUF_STATE_DONE);
  283. return nextbuf;
  284. }
  285. struct uvc_buffer *uvcg_queue_head(struct uvc_video_queue *queue)
  286. {
  287. struct uvc_buffer *buf = NULL;
  288. if (!list_empty(&queue->irqqueue))
  289. buf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
  290. queue);
  291. else
  292. queue->flags |= UVC_QUEUE_PAUSED;
  293. return buf;
  294. }