uvc_queue.c 11 KB

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