uvc_queue.c 9.7 KB

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