uvc_queue.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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_export_buffer(struct uvc_video_queue *queue,
  224. struct v4l2_exportbuffer *exp)
  225. {
  226. int ret;
  227. mutex_lock(&queue->mutex);
  228. ret = vb2_expbuf(&queue->queue, exp);
  229. mutex_unlock(&queue->mutex);
  230. return ret;
  231. }
  232. int uvc_dequeue_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf,
  233. int nonblocking)
  234. {
  235. int ret;
  236. mutex_lock(&queue->mutex);
  237. ret = vb2_dqbuf(&queue->queue, buf, nonblocking);
  238. mutex_unlock(&queue->mutex);
  239. return ret;
  240. }
  241. int uvc_queue_streamon(struct uvc_video_queue *queue, enum v4l2_buf_type type)
  242. {
  243. int ret;
  244. mutex_lock(&queue->mutex);
  245. ret = vb2_streamon(&queue->queue, type);
  246. mutex_unlock(&queue->mutex);
  247. return ret;
  248. }
  249. int uvc_queue_streamoff(struct uvc_video_queue *queue, enum v4l2_buf_type type)
  250. {
  251. int ret;
  252. mutex_lock(&queue->mutex);
  253. ret = vb2_streamoff(&queue->queue, type);
  254. mutex_unlock(&queue->mutex);
  255. return ret;
  256. }
  257. int uvc_queue_mmap(struct uvc_video_queue *queue, struct vm_area_struct *vma)
  258. {
  259. return vb2_mmap(&queue->queue, vma);
  260. }
  261. #ifndef CONFIG_MMU
  262. unsigned long uvc_queue_get_unmapped_area(struct uvc_video_queue *queue,
  263. unsigned long pgoff)
  264. {
  265. return vb2_get_unmapped_area(&queue->queue, 0, 0, pgoff, 0);
  266. }
  267. #endif
  268. unsigned int uvc_queue_poll(struct uvc_video_queue *queue, struct file *file,
  269. poll_table *wait)
  270. {
  271. unsigned int ret;
  272. mutex_lock(&queue->mutex);
  273. ret = vb2_poll(&queue->queue, file, wait);
  274. mutex_unlock(&queue->mutex);
  275. return ret;
  276. }
  277. /* -----------------------------------------------------------------------------
  278. *
  279. */
  280. /*
  281. * Check if buffers have been allocated.
  282. */
  283. int uvc_queue_allocated(struct uvc_video_queue *queue)
  284. {
  285. int allocated;
  286. mutex_lock(&queue->mutex);
  287. allocated = vb2_is_busy(&queue->queue);
  288. mutex_unlock(&queue->mutex);
  289. return allocated;
  290. }
  291. /*
  292. * Cancel the video buffers queue.
  293. *
  294. * Cancelling the queue marks all buffers on the irq queue as erroneous,
  295. * wakes them up and removes them from the queue.
  296. *
  297. * If the disconnect parameter is set, further calls to uvc_queue_buffer will
  298. * fail with -ENODEV.
  299. *
  300. * This function acquires the irq spinlock and can be called from interrupt
  301. * context.
  302. */
  303. void uvc_queue_cancel(struct uvc_video_queue *queue, int disconnect)
  304. {
  305. unsigned long flags;
  306. spin_lock_irqsave(&queue->irqlock, flags);
  307. uvc_queue_return_buffers(queue, UVC_BUF_STATE_ERROR);
  308. /* This must be protected by the irqlock spinlock to avoid race
  309. * conditions between uvc_buffer_queue and the disconnection event that
  310. * could result in an interruptible wait in uvc_dequeue_buffer. Do not
  311. * blindly replace this logic by checking for the UVC_QUEUE_DISCONNECTED
  312. * state outside the queue code.
  313. */
  314. if (disconnect)
  315. queue->flags |= UVC_QUEUE_DISCONNECTED;
  316. spin_unlock_irqrestore(&queue->irqlock, flags);
  317. }
  318. struct uvc_buffer *uvc_queue_next_buffer(struct uvc_video_queue *queue,
  319. struct uvc_buffer *buf)
  320. {
  321. struct uvc_buffer *nextbuf;
  322. unsigned long flags;
  323. if ((queue->flags & UVC_QUEUE_DROP_CORRUPTED) && buf->error) {
  324. buf->error = 0;
  325. buf->state = UVC_BUF_STATE_QUEUED;
  326. buf->bytesused = 0;
  327. vb2_set_plane_payload(&buf->buf, 0, 0);
  328. return buf;
  329. }
  330. spin_lock_irqsave(&queue->irqlock, flags);
  331. list_del(&buf->queue);
  332. if (!list_empty(&queue->irqqueue))
  333. nextbuf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
  334. queue);
  335. else
  336. nextbuf = NULL;
  337. spin_unlock_irqrestore(&queue->irqlock, flags);
  338. buf->state = buf->error ? VB2_BUF_STATE_ERROR : UVC_BUF_STATE_DONE;
  339. vb2_set_plane_payload(&buf->buf, 0, buf->bytesused);
  340. vb2_buffer_done(&buf->buf, VB2_BUF_STATE_DONE);
  341. return nextbuf;
  342. }