uvc_queue.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  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 void uvc_wait_prepare(struct vb2_queue *vq)
  124. {
  125. struct uvc_video_queue *queue = vb2_get_drv_priv(vq);
  126. mutex_unlock(&queue->mutex);
  127. }
  128. static void uvc_wait_finish(struct vb2_queue *vq)
  129. {
  130. struct uvc_video_queue *queue = vb2_get_drv_priv(vq);
  131. mutex_lock(&queue->mutex);
  132. }
  133. static int uvc_start_streaming(struct vb2_queue *vq, unsigned int count)
  134. {
  135. struct uvc_video_queue *queue = vb2_get_drv_priv(vq);
  136. struct uvc_streaming *stream = uvc_queue_to_stream(queue);
  137. unsigned long flags;
  138. int ret;
  139. queue->buf_used = 0;
  140. ret = uvc_video_enable(stream, 1);
  141. if (ret == 0)
  142. return 0;
  143. spin_lock_irqsave(&queue->irqlock, flags);
  144. uvc_queue_return_buffers(queue, UVC_BUF_STATE_QUEUED);
  145. spin_unlock_irqrestore(&queue->irqlock, flags);
  146. return ret;
  147. }
  148. static void uvc_stop_streaming(struct vb2_queue *vq)
  149. {
  150. struct uvc_video_queue *queue = vb2_get_drv_priv(vq);
  151. struct uvc_streaming *stream = uvc_queue_to_stream(queue);
  152. unsigned long flags;
  153. uvc_video_enable(stream, 0);
  154. spin_lock_irqsave(&queue->irqlock, flags);
  155. uvc_queue_return_buffers(queue, UVC_BUF_STATE_ERROR);
  156. spin_unlock_irqrestore(&queue->irqlock, flags);
  157. }
  158. static struct vb2_ops uvc_queue_qops = {
  159. .queue_setup = uvc_queue_setup,
  160. .buf_prepare = uvc_buffer_prepare,
  161. .buf_queue = uvc_buffer_queue,
  162. .buf_finish = uvc_buffer_finish,
  163. .wait_prepare = uvc_wait_prepare,
  164. .wait_finish = uvc_wait_finish,
  165. .start_streaming = uvc_start_streaming,
  166. .stop_streaming = uvc_stop_streaming,
  167. };
  168. int uvc_queue_init(struct uvc_video_queue *queue, enum v4l2_buf_type type,
  169. int drop_corrupted)
  170. {
  171. int ret;
  172. queue->queue.type = type;
  173. queue->queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
  174. queue->queue.drv_priv = queue;
  175. queue->queue.buf_struct_size = sizeof(struct uvc_buffer);
  176. queue->queue.ops = &uvc_queue_qops;
  177. queue->queue.mem_ops = &vb2_vmalloc_memops;
  178. queue->queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC
  179. | V4L2_BUF_FLAG_TSTAMP_SRC_SOE;
  180. ret = vb2_queue_init(&queue->queue);
  181. if (ret)
  182. return ret;
  183. mutex_init(&queue->mutex);
  184. spin_lock_init(&queue->irqlock);
  185. INIT_LIST_HEAD(&queue->irqqueue);
  186. queue->flags = drop_corrupted ? UVC_QUEUE_DROP_CORRUPTED : 0;
  187. return 0;
  188. }
  189. void uvc_queue_release(struct uvc_video_queue *queue)
  190. {
  191. mutex_lock(&queue->mutex);
  192. vb2_queue_release(&queue->queue);
  193. mutex_unlock(&queue->mutex);
  194. }
  195. /* -----------------------------------------------------------------------------
  196. * V4L2 queue operations
  197. */
  198. int uvc_request_buffers(struct uvc_video_queue *queue,
  199. struct v4l2_requestbuffers *rb)
  200. {
  201. int ret;
  202. mutex_lock(&queue->mutex);
  203. ret = vb2_reqbufs(&queue->queue, rb);
  204. mutex_unlock(&queue->mutex);
  205. return ret ? ret : rb->count;
  206. }
  207. int uvc_query_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf)
  208. {
  209. int ret;
  210. mutex_lock(&queue->mutex);
  211. ret = vb2_querybuf(&queue->queue, buf);
  212. mutex_unlock(&queue->mutex);
  213. return ret;
  214. }
  215. int uvc_create_buffers(struct uvc_video_queue *queue,
  216. struct v4l2_create_buffers *cb)
  217. {
  218. int ret;
  219. mutex_lock(&queue->mutex);
  220. ret = vb2_create_bufs(&queue->queue, cb);
  221. mutex_unlock(&queue->mutex);
  222. return ret;
  223. }
  224. int uvc_queue_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf)
  225. {
  226. int ret;
  227. mutex_lock(&queue->mutex);
  228. ret = vb2_qbuf(&queue->queue, buf);
  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. int ret;
  260. mutex_lock(&queue->mutex);
  261. ret = vb2_mmap(&queue->queue, vma);
  262. mutex_unlock(&queue->mutex);
  263. return ret;
  264. }
  265. #ifndef CONFIG_MMU
  266. unsigned long uvc_queue_get_unmapped_area(struct uvc_video_queue *queue,
  267. unsigned long pgoff)
  268. {
  269. unsigned long ret;
  270. mutex_lock(&queue->mutex);
  271. ret = vb2_get_unmapped_area(&queue->queue, 0, 0, pgoff, 0);
  272. mutex_unlock(&queue->mutex);
  273. return ret;
  274. }
  275. #endif
  276. unsigned int uvc_queue_poll(struct uvc_video_queue *queue, struct file *file,
  277. poll_table *wait)
  278. {
  279. unsigned int ret;
  280. mutex_lock(&queue->mutex);
  281. ret = vb2_poll(&queue->queue, file, wait);
  282. mutex_unlock(&queue->mutex);
  283. return ret;
  284. }
  285. /* -----------------------------------------------------------------------------
  286. *
  287. */
  288. /*
  289. * Check if buffers have been allocated.
  290. */
  291. int uvc_queue_allocated(struct uvc_video_queue *queue)
  292. {
  293. int allocated;
  294. mutex_lock(&queue->mutex);
  295. allocated = vb2_is_busy(&queue->queue);
  296. mutex_unlock(&queue->mutex);
  297. return allocated;
  298. }
  299. /*
  300. * Cancel the video buffers queue.
  301. *
  302. * Cancelling the queue marks all buffers on the irq queue as erroneous,
  303. * wakes them up and removes them from the queue.
  304. *
  305. * If the disconnect parameter is set, further calls to uvc_queue_buffer will
  306. * fail with -ENODEV.
  307. *
  308. * This function acquires the irq spinlock and can be called from interrupt
  309. * context.
  310. */
  311. void uvc_queue_cancel(struct uvc_video_queue *queue, int disconnect)
  312. {
  313. unsigned long flags;
  314. spin_lock_irqsave(&queue->irqlock, flags);
  315. uvc_queue_return_buffers(queue, UVC_BUF_STATE_ERROR);
  316. /* This must be protected by the irqlock spinlock to avoid race
  317. * conditions between uvc_buffer_queue and the disconnection event that
  318. * could result in an interruptible wait in uvc_dequeue_buffer. Do not
  319. * blindly replace this logic by checking for the UVC_QUEUE_DISCONNECTED
  320. * state outside the queue code.
  321. */
  322. if (disconnect)
  323. queue->flags |= UVC_QUEUE_DISCONNECTED;
  324. spin_unlock_irqrestore(&queue->irqlock, flags);
  325. }
  326. struct uvc_buffer *uvc_queue_next_buffer(struct uvc_video_queue *queue,
  327. struct uvc_buffer *buf)
  328. {
  329. struct uvc_buffer *nextbuf;
  330. unsigned long flags;
  331. if ((queue->flags & UVC_QUEUE_DROP_CORRUPTED) && buf->error) {
  332. buf->error = 0;
  333. buf->state = UVC_BUF_STATE_QUEUED;
  334. buf->bytesused = 0;
  335. vb2_set_plane_payload(&buf->buf, 0, 0);
  336. return buf;
  337. }
  338. spin_lock_irqsave(&queue->irqlock, flags);
  339. list_del(&buf->queue);
  340. if (!list_empty(&queue->irqqueue))
  341. nextbuf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
  342. queue);
  343. else
  344. nextbuf = NULL;
  345. spin_unlock_irqrestore(&queue->irqlock, flags);
  346. buf->state = buf->error ? VB2_BUF_STATE_ERROR : UVC_BUF_STATE_DONE;
  347. vb2_set_plane_payload(&buf->buf, 0, buf->bytesused);
  348. vb2_buffer_done(&buf->buf, VB2_BUF_STATE_DONE);
  349. return nextbuf;
  350. }