uvc_queue.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. /*
  2. * uvc_queue.c -- USB Video Class driver - Buffers management
  3. *
  4. * Copyright (C) 2005-2010
  5. * Laurent Pinchart (laurent.pinchart@ideasonboard.com)
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. */
  13. #include <linux/atomic.h>
  14. #include <linux/kernel.h>
  15. #include <linux/mm.h>
  16. #include <linux/list.h>
  17. #include <linux/module.h>
  18. #include <linux/usb.h>
  19. #include <linux/videodev2.h>
  20. #include <linux/vmalloc.h>
  21. #include <linux/wait.h>
  22. #include <media/videobuf2-vmalloc.h>
  23. #include "uvcvideo.h"
  24. /* ------------------------------------------------------------------------
  25. * Video buffers queue management.
  26. *
  27. * Video queues is initialized by uvc_queue_init(). The function performs
  28. * basic initialization of the uvc_video_queue struct and never fails.
  29. *
  30. * Video buffers are managed by videobuf2. The driver uses a mutex to protect
  31. * the videobuf2 queue operations by serializing calls to videobuf2 and a
  32. * spinlock to protect the IRQ queue that holds the buffers to be processed by
  33. * the driver.
  34. */
  35. /* -----------------------------------------------------------------------------
  36. * videobuf2 queue operations
  37. */
  38. static int uvc_queue_setup(struct vb2_queue *vq, const struct v4l2_format *fmt,
  39. unsigned int *nbuffers, unsigned int *nplanes,
  40. unsigned int sizes[], void *alloc_ctxs[])
  41. {
  42. struct uvc_video_queue *queue = vb2_get_drv_priv(vq);
  43. struct uvc_streaming *stream =
  44. container_of(queue, struct uvc_streaming, queue);
  45. /* Make sure the image size is large enough. */
  46. if (fmt && fmt->fmt.pix.sizeimage < stream->ctrl.dwMaxVideoFrameSize)
  47. return -EINVAL;
  48. *nplanes = 1;
  49. sizes[0] = fmt ? fmt->fmt.pix.sizeimage
  50. : stream->ctrl.dwMaxVideoFrameSize;
  51. return 0;
  52. }
  53. static int uvc_buffer_prepare(struct vb2_buffer *vb)
  54. {
  55. struct uvc_video_queue *queue = vb2_get_drv_priv(vb->vb2_queue);
  56. struct uvc_buffer *buf = container_of(vb, struct uvc_buffer, buf);
  57. if (vb->v4l2_buf.type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
  58. vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0)) {
  59. uvc_trace(UVC_TRACE_CAPTURE, "[E] Bytes used out of bounds.\n");
  60. return -EINVAL;
  61. }
  62. if (unlikely(queue->flags & UVC_QUEUE_DISCONNECTED))
  63. return -ENODEV;
  64. buf->state = UVC_BUF_STATE_QUEUED;
  65. buf->error = 0;
  66. buf->mem = vb2_plane_vaddr(vb, 0);
  67. buf->length = vb2_plane_size(vb, 0);
  68. if (vb->v4l2_buf.type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  69. buf->bytesused = 0;
  70. else
  71. buf->bytesused = vb2_get_plane_payload(vb, 0);
  72. return 0;
  73. }
  74. static void uvc_buffer_queue(struct vb2_buffer *vb)
  75. {
  76. struct uvc_video_queue *queue = vb2_get_drv_priv(vb->vb2_queue);
  77. struct uvc_buffer *buf = container_of(vb, struct uvc_buffer, buf);
  78. unsigned long flags;
  79. spin_lock_irqsave(&queue->irqlock, flags);
  80. if (likely(!(queue->flags & UVC_QUEUE_DISCONNECTED))) {
  81. list_add_tail(&buf->queue, &queue->irqqueue);
  82. } else {
  83. /* If the device is disconnected return the buffer to userspace
  84. * directly. The next QBUF call will fail with -ENODEV.
  85. */
  86. buf->state = UVC_BUF_STATE_ERROR;
  87. vb2_buffer_done(&buf->buf, VB2_BUF_STATE_ERROR);
  88. }
  89. spin_unlock_irqrestore(&queue->irqlock, flags);
  90. }
  91. static void uvc_buffer_finish(struct vb2_buffer *vb)
  92. {
  93. struct uvc_video_queue *queue = vb2_get_drv_priv(vb->vb2_queue);
  94. struct uvc_streaming *stream =
  95. container_of(queue, struct uvc_streaming, queue);
  96. struct uvc_buffer *buf = container_of(vb, struct uvc_buffer, buf);
  97. if (vb->state == VB2_BUF_STATE_DONE)
  98. uvc_video_clock_update(stream, &vb->v4l2_buf, buf);
  99. }
  100. static void uvc_wait_prepare(struct vb2_queue *vq)
  101. {
  102. struct uvc_video_queue *queue = vb2_get_drv_priv(vq);
  103. mutex_unlock(&queue->mutex);
  104. }
  105. static void uvc_wait_finish(struct vb2_queue *vq)
  106. {
  107. struct uvc_video_queue *queue = vb2_get_drv_priv(vq);
  108. mutex_lock(&queue->mutex);
  109. }
  110. static struct vb2_ops uvc_queue_qops = {
  111. .queue_setup = uvc_queue_setup,
  112. .buf_prepare = uvc_buffer_prepare,
  113. .buf_queue = uvc_buffer_queue,
  114. .buf_finish = uvc_buffer_finish,
  115. .wait_prepare = uvc_wait_prepare,
  116. .wait_finish = uvc_wait_finish,
  117. };
  118. int uvc_queue_init(struct uvc_video_queue *queue, enum v4l2_buf_type type,
  119. int drop_corrupted)
  120. {
  121. int ret;
  122. queue->queue.type = type;
  123. queue->queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
  124. queue->queue.drv_priv = queue;
  125. queue->queue.buf_struct_size = sizeof(struct uvc_buffer);
  126. queue->queue.ops = &uvc_queue_qops;
  127. queue->queue.mem_ops = &vb2_vmalloc_memops;
  128. queue->queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC
  129. | V4L2_BUF_FLAG_TSTAMP_SRC_SOE;
  130. ret = vb2_queue_init(&queue->queue);
  131. if (ret)
  132. return ret;
  133. mutex_init(&queue->mutex);
  134. spin_lock_init(&queue->irqlock);
  135. INIT_LIST_HEAD(&queue->irqqueue);
  136. queue->flags = drop_corrupted ? UVC_QUEUE_DROP_CORRUPTED : 0;
  137. return 0;
  138. }
  139. /* -----------------------------------------------------------------------------
  140. * V4L2 queue operations
  141. */
  142. int uvc_alloc_buffers(struct uvc_video_queue *queue,
  143. struct v4l2_requestbuffers *rb)
  144. {
  145. int ret;
  146. mutex_lock(&queue->mutex);
  147. ret = vb2_reqbufs(&queue->queue, rb);
  148. mutex_unlock(&queue->mutex);
  149. return ret ? ret : rb->count;
  150. }
  151. void uvc_free_buffers(struct uvc_video_queue *queue)
  152. {
  153. mutex_lock(&queue->mutex);
  154. vb2_queue_release(&queue->queue);
  155. mutex_unlock(&queue->mutex);
  156. }
  157. int uvc_query_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf)
  158. {
  159. int ret;
  160. mutex_lock(&queue->mutex);
  161. ret = vb2_querybuf(&queue->queue, buf);
  162. mutex_unlock(&queue->mutex);
  163. return ret;
  164. }
  165. int uvc_create_buffers(struct uvc_video_queue *queue,
  166. struct v4l2_create_buffers *cb)
  167. {
  168. int ret;
  169. mutex_lock(&queue->mutex);
  170. ret = vb2_create_bufs(&queue->queue, cb);
  171. mutex_unlock(&queue->mutex);
  172. return ret;
  173. }
  174. int uvc_queue_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf)
  175. {
  176. int ret;
  177. mutex_lock(&queue->mutex);
  178. ret = vb2_qbuf(&queue->queue, buf);
  179. mutex_unlock(&queue->mutex);
  180. return ret;
  181. }
  182. int uvc_dequeue_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf,
  183. int nonblocking)
  184. {
  185. int ret;
  186. mutex_lock(&queue->mutex);
  187. ret = vb2_dqbuf(&queue->queue, buf, nonblocking);
  188. mutex_unlock(&queue->mutex);
  189. return ret;
  190. }
  191. int uvc_queue_mmap(struct uvc_video_queue *queue, struct vm_area_struct *vma)
  192. {
  193. int ret;
  194. mutex_lock(&queue->mutex);
  195. ret = vb2_mmap(&queue->queue, vma);
  196. mutex_unlock(&queue->mutex);
  197. return ret;
  198. }
  199. #ifndef CONFIG_MMU
  200. unsigned long uvc_queue_get_unmapped_area(struct uvc_video_queue *queue,
  201. unsigned long pgoff)
  202. {
  203. unsigned long ret;
  204. mutex_lock(&queue->mutex);
  205. ret = vb2_get_unmapped_area(&queue->queue, 0, 0, pgoff, 0);
  206. mutex_unlock(&queue->mutex);
  207. return ret;
  208. }
  209. #endif
  210. unsigned int uvc_queue_poll(struct uvc_video_queue *queue, struct file *file,
  211. poll_table *wait)
  212. {
  213. unsigned int ret;
  214. mutex_lock(&queue->mutex);
  215. ret = vb2_poll(&queue->queue, file, wait);
  216. mutex_unlock(&queue->mutex);
  217. return ret;
  218. }
  219. /* -----------------------------------------------------------------------------
  220. *
  221. */
  222. /*
  223. * Check if buffers have been allocated.
  224. */
  225. int uvc_queue_allocated(struct uvc_video_queue *queue)
  226. {
  227. int allocated;
  228. mutex_lock(&queue->mutex);
  229. allocated = vb2_is_busy(&queue->queue);
  230. mutex_unlock(&queue->mutex);
  231. return allocated;
  232. }
  233. /*
  234. * Enable or disable the video buffers queue.
  235. *
  236. * The queue must be enabled before starting video acquisition and must be
  237. * disabled after stopping it. This ensures that the video buffers queue
  238. * state can be properly initialized before buffers are accessed from the
  239. * interrupt handler.
  240. *
  241. * Enabling the video queue returns -EBUSY if the queue is already enabled.
  242. *
  243. * Disabling the video queue cancels the queue and removes all buffers from
  244. * the main queue.
  245. *
  246. * This function can't be called from interrupt context. Use
  247. * uvc_queue_cancel() instead.
  248. */
  249. int uvc_queue_enable(struct uvc_video_queue *queue, int enable)
  250. {
  251. unsigned long flags;
  252. int ret;
  253. mutex_lock(&queue->mutex);
  254. if (enable) {
  255. ret = vb2_streamon(&queue->queue, queue->queue.type);
  256. if (ret < 0)
  257. goto done;
  258. queue->buf_used = 0;
  259. } else {
  260. ret = vb2_streamoff(&queue->queue, queue->queue.type);
  261. if (ret < 0)
  262. goto done;
  263. spin_lock_irqsave(&queue->irqlock, flags);
  264. INIT_LIST_HEAD(&queue->irqqueue);
  265. spin_unlock_irqrestore(&queue->irqlock, flags);
  266. }
  267. done:
  268. mutex_unlock(&queue->mutex);
  269. return ret;
  270. }
  271. /*
  272. * Cancel the video buffers queue.
  273. *
  274. * Cancelling the queue marks all buffers on the irq queue as erroneous,
  275. * wakes them up and removes them from the queue.
  276. *
  277. * If the disconnect parameter is set, further calls to uvc_queue_buffer will
  278. * fail with -ENODEV.
  279. *
  280. * This function acquires the irq spinlock and can be called from interrupt
  281. * context.
  282. */
  283. void uvc_queue_cancel(struct uvc_video_queue *queue, int disconnect)
  284. {
  285. struct uvc_buffer *buf;
  286. unsigned long flags;
  287. spin_lock_irqsave(&queue->irqlock, flags);
  288. while (!list_empty(&queue->irqqueue)) {
  289. buf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
  290. queue);
  291. list_del(&buf->queue);
  292. buf->state = UVC_BUF_STATE_ERROR;
  293. vb2_buffer_done(&buf->buf, VB2_BUF_STATE_ERROR);
  294. }
  295. /* This must be protected by the irqlock spinlock to avoid race
  296. * conditions between uvc_buffer_queue and the disconnection event that
  297. * could result in an interruptible wait in uvc_dequeue_buffer. Do not
  298. * blindly replace this logic by checking for the UVC_QUEUE_DISCONNECTED
  299. * state outside the queue code.
  300. */
  301. if (disconnect)
  302. queue->flags |= UVC_QUEUE_DISCONNECTED;
  303. spin_unlock_irqrestore(&queue->irqlock, flags);
  304. }
  305. struct uvc_buffer *uvc_queue_next_buffer(struct uvc_video_queue *queue,
  306. struct uvc_buffer *buf)
  307. {
  308. struct uvc_buffer *nextbuf;
  309. unsigned long flags;
  310. if ((queue->flags & UVC_QUEUE_DROP_CORRUPTED) && buf->error) {
  311. buf->error = 0;
  312. buf->state = UVC_BUF_STATE_QUEUED;
  313. buf->bytesused = 0;
  314. vb2_set_plane_payload(&buf->buf, 0, 0);
  315. return buf;
  316. }
  317. spin_lock_irqsave(&queue->irqlock, flags);
  318. list_del(&buf->queue);
  319. if (!list_empty(&queue->irqqueue))
  320. nextbuf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
  321. queue);
  322. else
  323. nextbuf = NULL;
  324. spin_unlock_irqrestore(&queue->irqlock, flags);
  325. buf->state = buf->error ? VB2_BUF_STATE_ERROR : UVC_BUF_STATE_DONE;
  326. vb2_set_plane_payload(&buf->buf, 0, buf->bytesused);
  327. vb2_buffer_done(&buf->buf, VB2_BUF_STATE_DONE);
  328. return nextbuf;
  329. }