uvc_queue.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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 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_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 uvc_buffer *buf = container_of(vb, struct uvc_buffer, buf);
  54. if (vb->v4l2_buf.type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
  55. vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0)) {
  56. uvc_trace(UVC_TRACE_CAPTURE, "[E] Bytes used out of bounds.\n");
  57. return -EINVAL;
  58. }
  59. if (unlikely(queue->flags & UVC_QUEUE_DISCONNECTED))
  60. return -ENODEV;
  61. buf->state = UVC_BUF_STATE_QUEUED;
  62. buf->mem = vb2_plane_vaddr(vb, 0);
  63. buf->length = vb2_plane_size(vb, 0);
  64. if (vb->v4l2_buf.type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  65. buf->bytesused = 0;
  66. else
  67. buf->bytesused = vb2_get_plane_payload(vb, 0);
  68. return 0;
  69. }
  70. static void uvc_buffer_queue(struct vb2_buffer *vb)
  71. {
  72. struct uvc_video_queue *queue = vb2_get_drv_priv(vb->vb2_queue);
  73. struct uvc_buffer *buf = container_of(vb, struct uvc_buffer, buf);
  74. unsigned long flags;
  75. spin_lock_irqsave(&queue->irqlock, flags);
  76. if (likely(!(queue->flags & UVC_QUEUE_DISCONNECTED))) {
  77. list_add_tail(&buf->queue, &queue->irqqueue);
  78. } else {
  79. /* If the device is disconnected return the buffer to userspace
  80. * directly. The next QBUF call will fail with -ENODEV.
  81. */
  82. buf->state = UVC_BUF_STATE_ERROR;
  83. vb2_buffer_done(&buf->buf, VB2_BUF_STATE_ERROR);
  84. }
  85. spin_unlock_irqrestore(&queue->irqlock, flags);
  86. }
  87. static void uvc_wait_prepare(struct vb2_queue *vq)
  88. {
  89. struct uvc_video_queue *queue = vb2_get_drv_priv(vq);
  90. mutex_unlock(&queue->mutex);
  91. }
  92. static void uvc_wait_finish(struct vb2_queue *vq)
  93. {
  94. struct uvc_video_queue *queue = vb2_get_drv_priv(vq);
  95. mutex_lock(&queue->mutex);
  96. }
  97. static struct vb2_ops uvc_queue_qops = {
  98. .queue_setup = uvc_queue_setup,
  99. .buf_prepare = uvc_buffer_prepare,
  100. .buf_queue = uvc_buffer_queue,
  101. .wait_prepare = uvc_wait_prepare,
  102. .wait_finish = uvc_wait_finish,
  103. };
  104. static int uvc_queue_init(struct uvc_video_queue *queue,
  105. enum v4l2_buf_type type)
  106. {
  107. int ret;
  108. queue->queue.type = type;
  109. queue->queue.io_modes = VB2_MMAP | VB2_USERPTR;
  110. queue->queue.drv_priv = queue;
  111. queue->queue.buf_struct_size = sizeof(struct uvc_buffer);
  112. queue->queue.ops = &uvc_queue_qops;
  113. queue->queue.mem_ops = &vb2_vmalloc_memops;
  114. queue->queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC
  115. | V4L2_BUF_FLAG_TSTAMP_SRC_EOF;
  116. ret = vb2_queue_init(&queue->queue);
  117. if (ret)
  118. return ret;
  119. mutex_init(&queue->mutex);
  120. spin_lock_init(&queue->irqlock);
  121. INIT_LIST_HEAD(&queue->irqqueue);
  122. queue->flags = 0;
  123. return 0;
  124. }
  125. /*
  126. * Free the video buffers.
  127. */
  128. static void uvc_free_buffers(struct uvc_video_queue *queue)
  129. {
  130. mutex_lock(&queue->mutex);
  131. vb2_queue_release(&queue->queue);
  132. mutex_unlock(&queue->mutex);
  133. }
  134. /*
  135. * Allocate the video buffers.
  136. */
  137. static int uvc_alloc_buffers(struct uvc_video_queue *queue,
  138. struct v4l2_requestbuffers *rb)
  139. {
  140. int ret;
  141. mutex_lock(&queue->mutex);
  142. ret = vb2_reqbufs(&queue->queue, rb);
  143. mutex_unlock(&queue->mutex);
  144. return ret ? ret : rb->count;
  145. }
  146. static int uvc_query_buffer(struct uvc_video_queue *queue,
  147. struct v4l2_buffer *buf)
  148. {
  149. int ret;
  150. mutex_lock(&queue->mutex);
  151. ret = vb2_querybuf(&queue->queue, buf);
  152. mutex_unlock(&queue->mutex);
  153. return ret;
  154. }
  155. static int uvc_queue_buffer(struct uvc_video_queue *queue,
  156. struct v4l2_buffer *buf)
  157. {
  158. unsigned long flags;
  159. int ret;
  160. mutex_lock(&queue->mutex);
  161. ret = vb2_qbuf(&queue->queue, buf);
  162. if (ret < 0)
  163. goto done;
  164. spin_lock_irqsave(&queue->irqlock, flags);
  165. ret = (queue->flags & UVC_QUEUE_PAUSED) != 0;
  166. queue->flags &= ~UVC_QUEUE_PAUSED;
  167. spin_unlock_irqrestore(&queue->irqlock, flags);
  168. done:
  169. mutex_unlock(&queue->mutex);
  170. return ret;
  171. }
  172. /*
  173. * Dequeue a video buffer. If nonblocking is false, block until a buffer is
  174. * available.
  175. */
  176. static int uvc_dequeue_buffer(struct uvc_video_queue *queue,
  177. struct v4l2_buffer *buf, int nonblocking)
  178. {
  179. int ret;
  180. mutex_lock(&queue->mutex);
  181. ret = vb2_dqbuf(&queue->queue, buf, nonblocking);
  182. mutex_unlock(&queue->mutex);
  183. return ret;
  184. }
  185. /*
  186. * Poll the video queue.
  187. *
  188. * This function implements video queue polling and is intended to be used by
  189. * the device poll handler.
  190. */
  191. static unsigned int uvc_queue_poll(struct uvc_video_queue *queue,
  192. struct file *file, poll_table *wait)
  193. {
  194. unsigned int ret;
  195. mutex_lock(&queue->mutex);
  196. ret = vb2_poll(&queue->queue, file, wait);
  197. mutex_unlock(&queue->mutex);
  198. return ret;
  199. }
  200. static int uvc_queue_mmap(struct uvc_video_queue *queue,
  201. struct vm_area_struct *vma)
  202. {
  203. int ret;
  204. mutex_lock(&queue->mutex);
  205. ret = vb2_mmap(&queue->queue, vma);
  206. mutex_unlock(&queue->mutex);
  207. return ret;
  208. }
  209. #ifndef CONFIG_MMU
  210. /*
  211. * Get unmapped area.
  212. *
  213. * NO-MMU arch need this function to make mmap() work correctly.
  214. */
  215. static unsigned long uvc_queue_get_unmapped_area(struct uvc_video_queue *queue,
  216. unsigned long pgoff)
  217. {
  218. unsigned long ret;
  219. mutex_lock(&queue->mutex);
  220. ret = vb2_get_unmapped_area(&queue->queue, 0, 0, pgoff, 0);
  221. mutex_unlock(&queue->mutex);
  222. return ret;
  223. }
  224. #endif
  225. /*
  226. * Cancel the video buffers queue.
  227. *
  228. * Cancelling the queue marks all buffers on the irq queue as erroneous,
  229. * wakes them up and removes them from the queue.
  230. *
  231. * If the disconnect parameter is set, further calls to uvc_queue_buffer will
  232. * fail with -ENODEV.
  233. *
  234. * This function acquires the irq spinlock and can be called from interrupt
  235. * context.
  236. */
  237. static void uvc_queue_cancel(struct uvc_video_queue *queue, int disconnect)
  238. {
  239. struct uvc_buffer *buf;
  240. unsigned long flags;
  241. spin_lock_irqsave(&queue->irqlock, flags);
  242. while (!list_empty(&queue->irqqueue)) {
  243. buf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
  244. queue);
  245. list_del(&buf->queue);
  246. buf->state = UVC_BUF_STATE_ERROR;
  247. vb2_buffer_done(&buf->buf, VB2_BUF_STATE_ERROR);
  248. }
  249. /* This must be protected by the irqlock spinlock to avoid race
  250. * conditions between uvc_queue_buffer and the disconnection event that
  251. * could result in an interruptible wait in uvc_dequeue_buffer. Do not
  252. * blindly replace this logic by checking for the UVC_DEV_DISCONNECTED
  253. * state outside the queue code.
  254. */
  255. if (disconnect)
  256. queue->flags |= UVC_QUEUE_DISCONNECTED;
  257. spin_unlock_irqrestore(&queue->irqlock, flags);
  258. }
  259. /*
  260. * Enable or disable the video buffers queue.
  261. *
  262. * The queue must be enabled before starting video acquisition and must be
  263. * disabled after stopping it. This ensures that the video buffers queue
  264. * state can be properly initialized before buffers are accessed from the
  265. * interrupt handler.
  266. *
  267. * Enabling the video queue initializes parameters (such as sequence number,
  268. * sync pattern, ...). If the queue is already enabled, return -EBUSY.
  269. *
  270. * Disabling the video queue cancels the queue and removes all buffers from
  271. * the main queue.
  272. *
  273. * This function can't be called from interrupt context. Use
  274. * uvc_queue_cancel() instead.
  275. */
  276. static int uvc_queue_enable(struct uvc_video_queue *queue, int enable)
  277. {
  278. unsigned long flags;
  279. int ret = 0;
  280. mutex_lock(&queue->mutex);
  281. if (enable) {
  282. ret = vb2_streamon(&queue->queue, queue->queue.type);
  283. if (ret < 0)
  284. goto done;
  285. queue->sequence = 0;
  286. queue->buf_used = 0;
  287. } else {
  288. ret = vb2_streamoff(&queue->queue, queue->queue.type);
  289. if (ret < 0)
  290. goto done;
  291. spin_lock_irqsave(&queue->irqlock, flags);
  292. INIT_LIST_HEAD(&queue->irqqueue);
  293. /*
  294. * FIXME: We need to clear the DISCONNECTED flag to ensure that
  295. * applications will be able to queue buffers for the next
  296. * streaming run. However, clearing it here doesn't guarantee
  297. * that the device will be reconnected in the meantime.
  298. */
  299. queue->flags &= ~UVC_QUEUE_DISCONNECTED;
  300. spin_unlock_irqrestore(&queue->irqlock, flags);
  301. }
  302. done:
  303. mutex_unlock(&queue->mutex);
  304. return ret;
  305. }
  306. /* called with &queue_irqlock held.. */
  307. static struct uvc_buffer *uvc_queue_next_buffer(struct uvc_video_queue *queue,
  308. struct uvc_buffer *buf)
  309. {
  310. struct uvc_buffer *nextbuf;
  311. if ((queue->flags & UVC_QUEUE_DROP_INCOMPLETE) &&
  312. buf->length != buf->bytesused) {
  313. buf->state = UVC_BUF_STATE_QUEUED;
  314. vb2_set_plane_payload(&buf->buf, 0, 0);
  315. return buf;
  316. }
  317. list_del(&buf->queue);
  318. if (!list_empty(&queue->irqqueue))
  319. nextbuf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
  320. queue);
  321. else
  322. nextbuf = NULL;
  323. buf->buf.v4l2_buf.field = V4L2_FIELD_NONE;
  324. buf->buf.v4l2_buf.sequence = queue->sequence++;
  325. v4l2_get_timestamp(&buf->buf.v4l2_buf.timestamp);
  326. vb2_set_plane_payload(&buf->buf, 0, buf->bytesused);
  327. vb2_buffer_done(&buf->buf, VB2_BUF_STATE_DONE);
  328. return nextbuf;
  329. }
  330. static struct uvc_buffer *uvc_queue_head(struct uvc_video_queue *queue)
  331. {
  332. struct uvc_buffer *buf = NULL;
  333. if (!list_empty(&queue->irqqueue))
  334. buf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
  335. queue);
  336. else
  337. queue->flags |= UVC_QUEUE_PAUSED;
  338. return buf;
  339. }