uvc_queue.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. if (*nbuffers > UVC_MAX_VIDEO_BUFFERS)
  46. *nbuffers = UVC_MAX_VIDEO_BUFFERS;
  47. *nplanes = 1;
  48. sizes[0] = stream->ctrl.dwMaxVideoFrameSize;
  49. return 0;
  50. }
  51. static int uvc_buffer_prepare(struct vb2_buffer *vb)
  52. {
  53. struct uvc_video_queue *queue = vb2_get_drv_priv(vb->vb2_queue);
  54. struct uvc_buffer *buf = container_of(vb, struct uvc_buffer, buf);
  55. if (vb->v4l2_buf.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->error = 0;
  64. buf->mem = vb2_plane_vaddr(vb, 0);
  65. buf->length = vb2_plane_size(vb, 0);
  66. if (vb->v4l2_buf.type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  67. buf->bytesused = 0;
  68. else
  69. buf->bytesused = vb2_get_plane_payload(vb, 0);
  70. return 0;
  71. }
  72. static void uvc_buffer_queue(struct vb2_buffer *vb)
  73. {
  74. struct uvc_video_queue *queue = vb2_get_drv_priv(vb->vb2_queue);
  75. struct uvc_buffer *buf = container_of(vb, 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(&buf->buf, 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. };
  94. void uvc_queue_init(struct uvc_video_queue *queue, enum v4l2_buf_type type,
  95. int drop_corrupted)
  96. {
  97. queue->queue.type = type;
  98. queue->queue.io_modes = VB2_MMAP;
  99. queue->queue.drv_priv = queue;
  100. queue->queue.buf_struct_size = sizeof(struct uvc_buffer);
  101. queue->queue.ops = &uvc_queue_qops;
  102. queue->queue.mem_ops = &vb2_vmalloc_memops;
  103. vb2_queue_init(&queue->queue);
  104. mutex_init(&queue->mutex);
  105. spin_lock_init(&queue->irqlock);
  106. INIT_LIST_HEAD(&queue->irqqueue);
  107. queue->flags = drop_corrupted ? UVC_QUEUE_DROP_CORRUPTED : 0;
  108. }
  109. /* -----------------------------------------------------------------------------
  110. * V4L2 queue operations
  111. */
  112. int uvc_alloc_buffers(struct uvc_video_queue *queue,
  113. struct v4l2_requestbuffers *rb)
  114. {
  115. int ret;
  116. mutex_lock(&queue->mutex);
  117. ret = vb2_reqbufs(&queue->queue, rb);
  118. mutex_unlock(&queue->mutex);
  119. return ret ? ret : rb->count;
  120. }
  121. void uvc_free_buffers(struct uvc_video_queue *queue)
  122. {
  123. mutex_lock(&queue->mutex);
  124. vb2_queue_release(&queue->queue);
  125. mutex_unlock(&queue->mutex);
  126. }
  127. int uvc_query_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf)
  128. {
  129. int ret;
  130. mutex_lock(&queue->mutex);
  131. ret = vb2_querybuf(&queue->queue, buf);
  132. mutex_unlock(&queue->mutex);
  133. return ret;
  134. }
  135. int uvc_queue_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf)
  136. {
  137. int ret;
  138. mutex_lock(&queue->mutex);
  139. ret = vb2_qbuf(&queue->queue, buf);
  140. mutex_unlock(&queue->mutex);
  141. return ret;
  142. }
  143. int uvc_dequeue_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf,
  144. int nonblocking)
  145. {
  146. int ret;
  147. mutex_lock(&queue->mutex);
  148. ret = vb2_dqbuf(&queue->queue, buf, nonblocking);
  149. mutex_unlock(&queue->mutex);
  150. return ret;
  151. }
  152. int uvc_queue_mmap(struct uvc_video_queue *queue, struct vm_area_struct *vma)
  153. {
  154. int ret;
  155. mutex_lock(&queue->mutex);
  156. ret = vb2_mmap(&queue->queue, vma);
  157. mutex_unlock(&queue->mutex);
  158. return ret;
  159. }
  160. unsigned int uvc_queue_poll(struct uvc_video_queue *queue, struct file *file,
  161. poll_table *wait)
  162. {
  163. unsigned int ret;
  164. mutex_lock(&queue->mutex);
  165. ret = vb2_poll(&queue->queue, file, wait);
  166. mutex_unlock(&queue->mutex);
  167. return ret;
  168. }
  169. /* -----------------------------------------------------------------------------
  170. *
  171. */
  172. /*
  173. * Check if buffers have been allocated.
  174. */
  175. int uvc_queue_allocated(struct uvc_video_queue *queue)
  176. {
  177. int allocated;
  178. mutex_lock(&queue->mutex);
  179. allocated = vb2_is_busy(&queue->queue);
  180. mutex_unlock(&queue->mutex);
  181. return allocated;
  182. }
  183. #ifndef CONFIG_MMU
  184. /*
  185. * Get unmapped area.
  186. *
  187. * NO-MMU arch need this function to make mmap() work correctly.
  188. */
  189. unsigned long uvc_queue_get_unmapped_area(struct uvc_video_queue *queue,
  190. unsigned long pgoff)
  191. {
  192. struct uvc_buffer *buffer;
  193. unsigned int i;
  194. unsigned long ret;
  195. mutex_lock(&queue->mutex);
  196. for (i = 0; i < queue->count; ++i) {
  197. buffer = &queue->buffer[i];
  198. if ((buffer->buf.m.offset >> PAGE_SHIFT) == pgoff)
  199. break;
  200. }
  201. if (i == queue->count) {
  202. ret = -EINVAL;
  203. goto done;
  204. }
  205. ret = (unsigned long)buf->mem;
  206. done:
  207. mutex_unlock(&queue->mutex);
  208. return ret;
  209. }
  210. #endif
  211. /*
  212. * Enable or disable the video buffers queue.
  213. *
  214. * The queue must be enabled before starting video acquisition and must be
  215. * disabled after stopping it. This ensures that the video buffers queue
  216. * state can be properly initialized before buffers are accessed from the
  217. * interrupt handler.
  218. *
  219. * Enabling the video queue returns -EBUSY if the queue is already enabled.
  220. *
  221. * Disabling the video queue cancels the queue and removes all buffers from
  222. * the main queue.
  223. *
  224. * This function can't be called from interrupt context. Use
  225. * uvc_queue_cancel() instead.
  226. */
  227. int uvc_queue_enable(struct uvc_video_queue *queue, int enable)
  228. {
  229. unsigned long flags;
  230. int ret;
  231. mutex_lock(&queue->mutex);
  232. if (enable) {
  233. ret = vb2_streamon(&queue->queue, queue->queue.type);
  234. if (ret < 0)
  235. goto done;
  236. queue->buf_used = 0;
  237. } else {
  238. ret = vb2_streamoff(&queue->queue, queue->queue.type);
  239. if (ret < 0)
  240. goto done;
  241. spin_lock_irqsave(&queue->irqlock, flags);
  242. INIT_LIST_HEAD(&queue->irqqueue);
  243. spin_unlock_irqrestore(&queue->irqlock, flags);
  244. }
  245. done:
  246. mutex_unlock(&queue->mutex);
  247. return ret;
  248. }
  249. /*
  250. * Cancel the video buffers queue.
  251. *
  252. * Cancelling the queue marks all buffers on the irq queue as erroneous,
  253. * wakes them up and removes them from the queue.
  254. *
  255. * If the disconnect parameter is set, further calls to uvc_queue_buffer will
  256. * fail with -ENODEV.
  257. *
  258. * This function acquires the irq spinlock and can be called from interrupt
  259. * context.
  260. */
  261. void uvc_queue_cancel(struct uvc_video_queue *queue, int disconnect)
  262. {
  263. struct uvc_buffer *buf;
  264. unsigned long flags;
  265. spin_lock_irqsave(&queue->irqlock, flags);
  266. while (!list_empty(&queue->irqqueue)) {
  267. buf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
  268. queue);
  269. list_del(&buf->queue);
  270. buf->state = UVC_BUF_STATE_ERROR;
  271. vb2_buffer_done(&buf->buf, VB2_BUF_STATE_ERROR);
  272. }
  273. /* This must be protected by the irqlock spinlock to avoid race
  274. * conditions between uvc_buffer_queue and the disconnection event that
  275. * could result in an interruptible wait in uvc_dequeue_buffer. Do not
  276. * blindly replace this logic by checking for the UVC_QUEUE_DISCONNECTED
  277. * state outside the queue code.
  278. */
  279. if (disconnect)
  280. queue->flags |= UVC_QUEUE_DISCONNECTED;
  281. spin_unlock_irqrestore(&queue->irqlock, flags);
  282. }
  283. struct uvc_buffer *uvc_queue_next_buffer(struct uvc_video_queue *queue,
  284. struct uvc_buffer *buf)
  285. {
  286. struct uvc_buffer *nextbuf;
  287. unsigned long flags;
  288. if ((queue->flags & UVC_QUEUE_DROP_CORRUPTED) && buf->error) {
  289. buf->error = 0;
  290. buf->state = UVC_BUF_STATE_QUEUED;
  291. vb2_set_plane_payload(&buf->buf, 0, 0);
  292. return buf;
  293. }
  294. spin_lock_irqsave(&queue->irqlock, flags);
  295. list_del(&buf->queue);
  296. if (!list_empty(&queue->irqqueue))
  297. nextbuf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
  298. queue);
  299. else
  300. nextbuf = NULL;
  301. spin_unlock_irqrestore(&queue->irqlock, flags);
  302. buf->state = buf->error ? VB2_BUF_STATE_ERROR : UVC_BUF_STATE_DONE;
  303. vb2_set_plane_payload(&buf->buf, 0, buf->bytesused);
  304. vb2_buffer_done(&buf->buf, VB2_BUF_STATE_DONE);
  305. return nextbuf;
  306. }