uvc_queue.c 12 KB

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