uvc_queue.c 11 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. */
  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. static inline struct uvc_streaming *
  36. uvc_queue_to_stream(struct uvc_video_queue *queue)
  37. {
  38. return container_of(queue, struct uvc_streaming, queue);
  39. }
  40. /*
  41. * Return all queued buffers to videobuf2 in the requested state.
  42. *
  43. * This function must be called with the queue spinlock held.
  44. */
  45. static void uvc_queue_return_buffers(struct uvc_video_queue *queue,
  46. enum uvc_buffer_state state)
  47. {
  48. enum vb2_buffer_state vb2_state = state == UVC_BUF_STATE_ERROR
  49. ? VB2_BUF_STATE_ERROR
  50. : VB2_BUF_STATE_QUEUED;
  51. while (!list_empty(&queue->irqqueue)) {
  52. struct uvc_buffer *buf = list_first_entry(&queue->irqqueue,
  53. struct uvc_buffer,
  54. queue);
  55. list_del(&buf->queue);
  56. buf->state = state;
  57. vb2_buffer_done(&buf->buf, vb2_state);
  58. }
  59. }
  60. /* -----------------------------------------------------------------------------
  61. * videobuf2 queue operations
  62. */
  63. static int uvc_queue_setup(struct vb2_queue *vq, const struct v4l2_format *fmt,
  64. unsigned int *nbuffers, unsigned int *nplanes,
  65. unsigned int sizes[], void *alloc_ctxs[])
  66. {
  67. struct uvc_video_queue *queue = vb2_get_drv_priv(vq);
  68. struct uvc_streaming *stream = uvc_queue_to_stream(queue);
  69. /* Make sure the image size is large enough. */
  70. if (fmt && fmt->fmt.pix.sizeimage < stream->ctrl.dwMaxVideoFrameSize)
  71. return -EINVAL;
  72. *nplanes = 1;
  73. sizes[0] = fmt ? fmt->fmt.pix.sizeimage
  74. : stream->ctrl.dwMaxVideoFrameSize;
  75. return 0;
  76. }
  77. static int uvc_buffer_prepare(struct vb2_buffer *vb)
  78. {
  79. struct uvc_video_queue *queue = vb2_get_drv_priv(vb->vb2_queue);
  80. struct uvc_buffer *buf = container_of(vb, struct uvc_buffer, buf);
  81. if (vb->v4l2_buf.type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
  82. vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0)) {
  83. uvc_trace(UVC_TRACE_CAPTURE, "[E] Bytes used out of bounds.\n");
  84. return -EINVAL;
  85. }
  86. if (unlikely(queue->flags & UVC_QUEUE_DISCONNECTED))
  87. return -ENODEV;
  88. buf->state = UVC_BUF_STATE_QUEUED;
  89. buf->error = 0;
  90. buf->mem = vb2_plane_vaddr(vb, 0);
  91. buf->length = vb2_plane_size(vb, 0);
  92. if (vb->v4l2_buf.type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  93. buf->bytesused = 0;
  94. else
  95. buf->bytesused = vb2_get_plane_payload(vb, 0);
  96. return 0;
  97. }
  98. static void uvc_buffer_queue(struct vb2_buffer *vb)
  99. {
  100. struct uvc_video_queue *queue = vb2_get_drv_priv(vb->vb2_queue);
  101. struct uvc_buffer *buf = container_of(vb, struct uvc_buffer, buf);
  102. unsigned long flags;
  103. spin_lock_irqsave(&queue->irqlock, flags);
  104. if (likely(!(queue->flags & UVC_QUEUE_DISCONNECTED))) {
  105. list_add_tail(&buf->queue, &queue->irqqueue);
  106. } else {
  107. /* If the device is disconnected return the buffer to userspace
  108. * directly. The next QBUF call will fail with -ENODEV.
  109. */
  110. buf->state = UVC_BUF_STATE_ERROR;
  111. vb2_buffer_done(&buf->buf, VB2_BUF_STATE_ERROR);
  112. }
  113. spin_unlock_irqrestore(&queue->irqlock, flags);
  114. }
  115. static void uvc_buffer_finish(struct vb2_buffer *vb)
  116. {
  117. struct uvc_video_queue *queue = vb2_get_drv_priv(vb->vb2_queue);
  118. struct uvc_streaming *stream = uvc_queue_to_stream(queue);
  119. struct uvc_buffer *buf = container_of(vb, struct uvc_buffer, buf);
  120. if (vb->state == VB2_BUF_STATE_DONE)
  121. uvc_video_clock_update(stream, &vb->v4l2_buf, buf);
  122. }
  123. static int uvc_start_streaming(struct vb2_queue *vq, unsigned int count)
  124. {
  125. struct uvc_video_queue *queue = vb2_get_drv_priv(vq);
  126. struct uvc_streaming *stream = uvc_queue_to_stream(queue);
  127. unsigned long flags;
  128. int ret;
  129. queue->buf_used = 0;
  130. ret = uvc_video_enable(stream, 1);
  131. if (ret == 0)
  132. return 0;
  133. spin_lock_irqsave(&queue->irqlock, flags);
  134. uvc_queue_return_buffers(queue, UVC_BUF_STATE_QUEUED);
  135. spin_unlock_irqrestore(&queue->irqlock, flags);
  136. return ret;
  137. }
  138. static void uvc_stop_streaming(struct vb2_queue *vq)
  139. {
  140. struct uvc_video_queue *queue = vb2_get_drv_priv(vq);
  141. struct uvc_streaming *stream = uvc_queue_to_stream(queue);
  142. unsigned long flags;
  143. uvc_video_enable(stream, 0);
  144. spin_lock_irqsave(&queue->irqlock, flags);
  145. uvc_queue_return_buffers(queue, UVC_BUF_STATE_ERROR);
  146. spin_unlock_irqrestore(&queue->irqlock, flags);
  147. }
  148. static struct vb2_ops uvc_queue_qops = {
  149. .queue_setup = uvc_queue_setup,
  150. .buf_prepare = uvc_buffer_prepare,
  151. .buf_queue = uvc_buffer_queue,
  152. .buf_finish = uvc_buffer_finish,
  153. .wait_prepare = vb2_ops_wait_prepare,
  154. .wait_finish = vb2_ops_wait_finish,
  155. .start_streaming = uvc_start_streaming,
  156. .stop_streaming = uvc_stop_streaming,
  157. };
  158. int uvc_queue_init(struct uvc_video_queue *queue, enum v4l2_buf_type type,
  159. int drop_corrupted)
  160. {
  161. int ret;
  162. queue->queue.type = type;
  163. queue->queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
  164. queue->queue.drv_priv = queue;
  165. queue->queue.buf_struct_size = sizeof(struct uvc_buffer);
  166. queue->queue.ops = &uvc_queue_qops;
  167. queue->queue.mem_ops = &vb2_vmalloc_memops;
  168. queue->queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC
  169. | V4L2_BUF_FLAG_TSTAMP_SRC_SOE;
  170. queue->queue.lock = &queue->mutex;
  171. ret = vb2_queue_init(&queue->queue);
  172. if (ret)
  173. return ret;
  174. mutex_init(&queue->mutex);
  175. spin_lock_init(&queue->irqlock);
  176. INIT_LIST_HEAD(&queue->irqqueue);
  177. queue->flags = drop_corrupted ? UVC_QUEUE_DROP_CORRUPTED : 0;
  178. return 0;
  179. }
  180. void uvc_queue_release(struct uvc_video_queue *queue)
  181. {
  182. mutex_lock(&queue->mutex);
  183. vb2_queue_release(&queue->queue);
  184. mutex_unlock(&queue->mutex);
  185. }
  186. /* -----------------------------------------------------------------------------
  187. * V4L2 queue operations
  188. */
  189. int uvc_request_buffers(struct uvc_video_queue *queue,
  190. struct v4l2_requestbuffers *rb)
  191. {
  192. int ret;
  193. mutex_lock(&queue->mutex);
  194. ret = vb2_reqbufs(&queue->queue, rb);
  195. mutex_unlock(&queue->mutex);
  196. return ret ? ret : rb->count;
  197. }
  198. int uvc_query_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf)
  199. {
  200. int ret;
  201. mutex_lock(&queue->mutex);
  202. ret = vb2_querybuf(&queue->queue, buf);
  203. mutex_unlock(&queue->mutex);
  204. return ret;
  205. }
  206. int uvc_create_buffers(struct uvc_video_queue *queue,
  207. struct v4l2_create_buffers *cb)
  208. {
  209. int ret;
  210. mutex_lock(&queue->mutex);
  211. ret = vb2_create_bufs(&queue->queue, cb);
  212. mutex_unlock(&queue->mutex);
  213. return ret;
  214. }
  215. int uvc_queue_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf)
  216. {
  217. int ret;
  218. mutex_lock(&queue->mutex);
  219. ret = vb2_qbuf(&queue->queue, buf);
  220. mutex_unlock(&queue->mutex);
  221. return ret;
  222. }
  223. int uvc_dequeue_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf,
  224. int nonblocking)
  225. {
  226. int ret;
  227. mutex_lock(&queue->mutex);
  228. ret = vb2_dqbuf(&queue->queue, buf, nonblocking);
  229. mutex_unlock(&queue->mutex);
  230. return ret;
  231. }
  232. int uvc_queue_streamon(struct uvc_video_queue *queue, enum v4l2_buf_type type)
  233. {
  234. int ret;
  235. mutex_lock(&queue->mutex);
  236. ret = vb2_streamon(&queue->queue, type);
  237. mutex_unlock(&queue->mutex);
  238. return ret;
  239. }
  240. int uvc_queue_streamoff(struct uvc_video_queue *queue, enum v4l2_buf_type type)
  241. {
  242. int ret;
  243. mutex_lock(&queue->mutex);
  244. ret = vb2_streamoff(&queue->queue, type);
  245. mutex_unlock(&queue->mutex);
  246. return ret;
  247. }
  248. int uvc_queue_mmap(struct uvc_video_queue *queue, struct vm_area_struct *vma)
  249. {
  250. return vb2_mmap(&queue->queue, vma);
  251. }
  252. #ifndef CONFIG_MMU
  253. unsigned long uvc_queue_get_unmapped_area(struct uvc_video_queue *queue,
  254. unsigned long pgoff)
  255. {
  256. return vb2_get_unmapped_area(&queue->queue, 0, 0, pgoff, 0);
  257. }
  258. #endif
  259. unsigned int uvc_queue_poll(struct uvc_video_queue *queue, struct file *file,
  260. poll_table *wait)
  261. {
  262. unsigned int ret;
  263. mutex_lock(&queue->mutex);
  264. ret = vb2_poll(&queue->queue, file, wait);
  265. mutex_unlock(&queue->mutex);
  266. return ret;
  267. }
  268. /* -----------------------------------------------------------------------------
  269. *
  270. */
  271. /*
  272. * Check if buffers have been allocated.
  273. */
  274. int uvc_queue_allocated(struct uvc_video_queue *queue)
  275. {
  276. int allocated;
  277. mutex_lock(&queue->mutex);
  278. allocated = vb2_is_busy(&queue->queue);
  279. mutex_unlock(&queue->mutex);
  280. return allocated;
  281. }
  282. /*
  283. * Cancel the video buffers queue.
  284. *
  285. * Cancelling the queue marks all buffers on the irq queue as erroneous,
  286. * wakes them up and removes them from the queue.
  287. *
  288. * If the disconnect parameter is set, further calls to uvc_queue_buffer will
  289. * fail with -ENODEV.
  290. *
  291. * This function acquires the irq spinlock and can be called from interrupt
  292. * context.
  293. */
  294. void uvc_queue_cancel(struct uvc_video_queue *queue, int disconnect)
  295. {
  296. unsigned long flags;
  297. spin_lock_irqsave(&queue->irqlock, flags);
  298. uvc_queue_return_buffers(queue, UVC_BUF_STATE_ERROR);
  299. /* This must be protected by the irqlock spinlock to avoid race
  300. * conditions between uvc_buffer_queue and the disconnection event that
  301. * could result in an interruptible wait in uvc_dequeue_buffer. Do not
  302. * blindly replace this logic by checking for the UVC_QUEUE_DISCONNECTED
  303. * state outside the queue code.
  304. */
  305. if (disconnect)
  306. queue->flags |= UVC_QUEUE_DISCONNECTED;
  307. spin_unlock_irqrestore(&queue->irqlock, flags);
  308. }
  309. struct uvc_buffer *uvc_queue_next_buffer(struct uvc_video_queue *queue,
  310. struct uvc_buffer *buf)
  311. {
  312. struct uvc_buffer *nextbuf;
  313. unsigned long flags;
  314. if ((queue->flags & UVC_QUEUE_DROP_CORRUPTED) && buf->error) {
  315. buf->error = 0;
  316. buf->state = UVC_BUF_STATE_QUEUED;
  317. buf->bytesused = 0;
  318. vb2_set_plane_payload(&buf->buf, 0, 0);
  319. return buf;
  320. }
  321. spin_lock_irqsave(&queue->irqlock, flags);
  322. list_del(&buf->queue);
  323. if (!list_empty(&queue->irqqueue))
  324. nextbuf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
  325. queue);
  326. else
  327. nextbuf = NULL;
  328. spin_unlock_irqrestore(&queue->irqlock, flags);
  329. buf->state = buf->error ? VB2_BUF_STATE_ERROR : UVC_BUF_STATE_DONE;
  330. vb2_set_plane_payload(&buf->buf, 0, buf->bytesused);
  331. vb2_buffer_done(&buf->buf, VB2_BUF_STATE_DONE);
  332. return nextbuf;
  333. }