uvc_queue.c 9.5 KB

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