uvc_queue.c 11 KB

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