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