uvc_queue.c 9.7 KB

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