uvc_queue.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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. static inline struct uvc_buffer *uvc_vbuf_to_buffer(struct vb2_v4l2_buffer *buf)
  42. {
  43. return container_of(buf, struct uvc_buffer, buf);
  44. }
  45. /*
  46. * Return all queued buffers to videobuf2 in the requested state.
  47. *
  48. * This function must be called with the queue spinlock held.
  49. */
  50. static void uvc_queue_return_buffers(struct uvc_video_queue *queue,
  51. enum uvc_buffer_state state)
  52. {
  53. enum vb2_buffer_state vb2_state = state == UVC_BUF_STATE_ERROR
  54. ? VB2_BUF_STATE_ERROR
  55. : VB2_BUF_STATE_QUEUED;
  56. while (!list_empty(&queue->irqqueue)) {
  57. struct uvc_buffer *buf = list_first_entry(&queue->irqqueue,
  58. struct uvc_buffer,
  59. queue);
  60. list_del(&buf->queue);
  61. buf->state = state;
  62. vb2_buffer_done(&buf->buf.vb2_buf, vb2_state);
  63. }
  64. }
  65. /* -----------------------------------------------------------------------------
  66. * videobuf2 queue operations
  67. */
  68. static int uvc_queue_setup(struct vb2_queue *vq,
  69. unsigned int *nbuffers, unsigned int *nplanes,
  70. unsigned int sizes[], struct device *alloc_devs[])
  71. {
  72. struct uvc_video_queue *queue = vb2_get_drv_priv(vq);
  73. struct uvc_streaming *stream = uvc_queue_to_stream(queue);
  74. unsigned size = stream->ctrl.dwMaxVideoFrameSize;
  75. /* Make sure the image size is large enough. */
  76. if (*nplanes)
  77. return sizes[0] < size ? -EINVAL : 0;
  78. *nplanes = 1;
  79. sizes[0] = size;
  80. return 0;
  81. }
  82. static int uvc_buffer_prepare(struct vb2_buffer *vb)
  83. {
  84. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  85. struct uvc_video_queue *queue = vb2_get_drv_priv(vb->vb2_queue);
  86. struct uvc_buffer *buf = uvc_vbuf_to_buffer(vbuf);
  87. if (vb->type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
  88. vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0)) {
  89. uvc_trace(UVC_TRACE_CAPTURE, "[E] Bytes used out of bounds.\n");
  90. return -EINVAL;
  91. }
  92. if (unlikely(queue->flags & UVC_QUEUE_DISCONNECTED))
  93. return -ENODEV;
  94. buf->state = UVC_BUF_STATE_QUEUED;
  95. buf->error = 0;
  96. buf->mem = vb2_plane_vaddr(vb, 0);
  97. buf->length = vb2_plane_size(vb, 0);
  98. if (vb->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  99. buf->bytesused = 0;
  100. else
  101. buf->bytesused = vb2_get_plane_payload(vb, 0);
  102. return 0;
  103. }
  104. static void uvc_buffer_queue(struct vb2_buffer *vb)
  105. {
  106. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  107. struct uvc_video_queue *queue = vb2_get_drv_priv(vb->vb2_queue);
  108. struct uvc_buffer *buf = uvc_vbuf_to_buffer(vbuf);
  109. unsigned long flags;
  110. spin_lock_irqsave(&queue->irqlock, flags);
  111. if (likely(!(queue->flags & UVC_QUEUE_DISCONNECTED))) {
  112. list_add_tail(&buf->queue, &queue->irqqueue);
  113. } else {
  114. /* If the device is disconnected return the buffer to userspace
  115. * directly. The next QBUF call will fail with -ENODEV.
  116. */
  117. buf->state = UVC_BUF_STATE_ERROR;
  118. vb2_buffer_done(vb, VB2_BUF_STATE_ERROR);
  119. }
  120. spin_unlock_irqrestore(&queue->irqlock, flags);
  121. }
  122. static void uvc_buffer_finish(struct vb2_buffer *vb)
  123. {
  124. struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  125. struct uvc_video_queue *queue = vb2_get_drv_priv(vb->vb2_queue);
  126. struct uvc_streaming *stream = uvc_queue_to_stream(queue);
  127. struct uvc_buffer *buf = uvc_vbuf_to_buffer(vbuf);
  128. if (vb->state == VB2_BUF_STATE_DONE)
  129. uvc_video_clock_update(stream, vbuf, buf);
  130. }
  131. static int uvc_start_streaming(struct vb2_queue *vq, unsigned int count)
  132. {
  133. struct uvc_video_queue *queue = vb2_get_drv_priv(vq);
  134. struct uvc_streaming *stream = uvc_queue_to_stream(queue);
  135. unsigned long flags;
  136. int ret;
  137. queue->buf_used = 0;
  138. ret = uvc_video_enable(stream, 1);
  139. if (ret == 0)
  140. return 0;
  141. spin_lock_irqsave(&queue->irqlock, flags);
  142. uvc_queue_return_buffers(queue, UVC_BUF_STATE_QUEUED);
  143. spin_unlock_irqrestore(&queue->irqlock, flags);
  144. return ret;
  145. }
  146. static void uvc_stop_streaming(struct vb2_queue *vq)
  147. {
  148. struct uvc_video_queue *queue = vb2_get_drv_priv(vq);
  149. struct uvc_streaming *stream = uvc_queue_to_stream(queue);
  150. unsigned long flags;
  151. uvc_video_enable(stream, 0);
  152. spin_lock_irqsave(&queue->irqlock, flags);
  153. uvc_queue_return_buffers(queue, UVC_BUF_STATE_ERROR);
  154. spin_unlock_irqrestore(&queue->irqlock, flags);
  155. }
  156. static const struct vb2_ops uvc_queue_qops = {
  157. .queue_setup = uvc_queue_setup,
  158. .buf_prepare = uvc_buffer_prepare,
  159. .buf_queue = uvc_buffer_queue,
  160. .buf_finish = uvc_buffer_finish,
  161. .wait_prepare = vb2_ops_wait_prepare,
  162. .wait_finish = vb2_ops_wait_finish,
  163. .start_streaming = uvc_start_streaming,
  164. .stop_streaming = uvc_stop_streaming,
  165. };
  166. int uvc_queue_init(struct uvc_video_queue *queue, enum v4l2_buf_type type,
  167. int drop_corrupted)
  168. {
  169. int ret;
  170. queue->queue.type = type;
  171. queue->queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
  172. queue->queue.drv_priv = queue;
  173. queue->queue.buf_struct_size = sizeof(struct uvc_buffer);
  174. queue->queue.ops = &uvc_queue_qops;
  175. queue->queue.mem_ops = &vb2_vmalloc_memops;
  176. queue->queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC
  177. | V4L2_BUF_FLAG_TSTAMP_SRC_SOE;
  178. queue->queue.lock = &queue->mutex;
  179. ret = vb2_queue_init(&queue->queue);
  180. if (ret)
  181. return ret;
  182. mutex_init(&queue->mutex);
  183. spin_lock_init(&queue->irqlock);
  184. INIT_LIST_HEAD(&queue->irqqueue);
  185. queue->flags = drop_corrupted ? UVC_QUEUE_DROP_CORRUPTED : 0;
  186. return 0;
  187. }
  188. void uvc_queue_release(struct uvc_video_queue *queue)
  189. {
  190. mutex_lock(&queue->mutex);
  191. vb2_queue_release(&queue->queue);
  192. mutex_unlock(&queue->mutex);
  193. }
  194. /* -----------------------------------------------------------------------------
  195. * V4L2 queue operations
  196. */
  197. int uvc_request_buffers(struct uvc_video_queue *queue,
  198. struct v4l2_requestbuffers *rb)
  199. {
  200. int ret;
  201. mutex_lock(&queue->mutex);
  202. ret = vb2_reqbufs(&queue->queue, rb);
  203. mutex_unlock(&queue->mutex);
  204. return ret ? ret : rb->count;
  205. }
  206. int uvc_query_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf)
  207. {
  208. int ret;
  209. mutex_lock(&queue->mutex);
  210. ret = vb2_querybuf(&queue->queue, buf);
  211. mutex_unlock(&queue->mutex);
  212. return ret;
  213. }
  214. int uvc_create_buffers(struct uvc_video_queue *queue,
  215. struct v4l2_create_buffers *cb)
  216. {
  217. int ret;
  218. mutex_lock(&queue->mutex);
  219. ret = vb2_create_bufs(&queue->queue, cb);
  220. mutex_unlock(&queue->mutex);
  221. return ret;
  222. }
  223. int uvc_queue_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf)
  224. {
  225. int ret;
  226. mutex_lock(&queue->mutex);
  227. ret = vb2_qbuf(&queue->queue, buf);
  228. mutex_unlock(&queue->mutex);
  229. return ret;
  230. }
  231. int uvc_export_buffer(struct uvc_video_queue *queue,
  232. struct v4l2_exportbuffer *exp)
  233. {
  234. int ret;
  235. mutex_lock(&queue->mutex);
  236. ret = vb2_expbuf(&queue->queue, exp);
  237. mutex_unlock(&queue->mutex);
  238. return ret;
  239. }
  240. int uvc_dequeue_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf,
  241. int nonblocking)
  242. {
  243. int ret;
  244. mutex_lock(&queue->mutex);
  245. ret = vb2_dqbuf(&queue->queue, buf, nonblocking);
  246. mutex_unlock(&queue->mutex);
  247. return ret;
  248. }
  249. int uvc_queue_streamon(struct uvc_video_queue *queue, enum v4l2_buf_type type)
  250. {
  251. int ret;
  252. mutex_lock(&queue->mutex);
  253. ret = vb2_streamon(&queue->queue, type);
  254. mutex_unlock(&queue->mutex);
  255. return ret;
  256. }
  257. int uvc_queue_streamoff(struct uvc_video_queue *queue, enum v4l2_buf_type type)
  258. {
  259. int ret;
  260. mutex_lock(&queue->mutex);
  261. ret = vb2_streamoff(&queue->queue, type);
  262. mutex_unlock(&queue->mutex);
  263. return ret;
  264. }
  265. int uvc_queue_mmap(struct uvc_video_queue *queue, struct vm_area_struct *vma)
  266. {
  267. return vb2_mmap(&queue->queue, vma);
  268. }
  269. #ifndef CONFIG_MMU
  270. unsigned long uvc_queue_get_unmapped_area(struct uvc_video_queue *queue,
  271. unsigned long pgoff)
  272. {
  273. return vb2_get_unmapped_area(&queue->queue, 0, 0, pgoff, 0);
  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.vb2_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 ? UVC_BUF_STATE_ERROR : UVC_BUF_STATE_DONE;
  347. vb2_set_plane_payload(&buf->buf.vb2_buf, 0, buf->bytesused);
  348. vb2_buffer_done(&buf->buf.vb2_buf, VB2_BUF_STATE_DONE);
  349. return nextbuf;
  350. }