uvc_queue.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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. #include <linux/atomic.h>
  13. #include <linux/kernel.h>
  14. #include <linux/mm.h>
  15. #include <linux/list.h>
  16. #include <linux/module.h>
  17. #include <linux/usb.h>
  18. #include <linux/videodev2.h>
  19. #include <linux/vmalloc.h>
  20. #include <linux/wait.h>
  21. #include <media/videobuf2-vmalloc.h>
  22. #include "uvc.h"
  23. /* ------------------------------------------------------------------------
  24. * Video buffers queue management.
  25. *
  26. * Video queues is initialized by uvc_queue_init(). The function performs
  27. * basic initialization of the uvc_video_queue struct and never fails.
  28. *
  29. * Video buffers are managed by videobuf2. The driver uses a mutex to protect
  30. * the videobuf2 queue operations by serializing calls to videobuf2 and a
  31. * spinlock to protect the IRQ queue that holds the buffers to be processed by
  32. * the driver.
  33. */
  34. /* -----------------------------------------------------------------------------
  35. * videobuf2 queue operations
  36. */
  37. static int uvc_queue_setup(struct vb2_queue *vq, const struct v4l2_format *fmt,
  38. unsigned int *nbuffers, unsigned int *nplanes,
  39. unsigned int sizes[], void *alloc_ctxs[])
  40. {
  41. struct uvc_video_queue *queue = vb2_get_drv_priv(vq);
  42. struct uvc_video *video = container_of(queue, struct uvc_video, queue);
  43. if (*nbuffers > UVC_MAX_VIDEO_BUFFERS)
  44. *nbuffers = UVC_MAX_VIDEO_BUFFERS;
  45. *nplanes = 1;
  46. sizes[0] = video->imagesize;
  47. return 0;
  48. }
  49. static int uvc_buffer_prepare(struct vb2_buffer *vb)
  50. {
  51. struct uvc_video_queue *queue = vb2_get_drv_priv(vb->vb2_queue);
  52. struct uvc_buffer *buf = container_of(vb, struct uvc_buffer, buf);
  53. if (vb->v4l2_buf.type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
  54. vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0)) {
  55. uvc_trace(UVC_TRACE_CAPTURE, "[E] Bytes used out of bounds.\n");
  56. return -EINVAL;
  57. }
  58. if (unlikely(queue->flags & UVC_QUEUE_DISCONNECTED))
  59. return -ENODEV;
  60. buf->state = UVC_BUF_STATE_QUEUED;
  61. buf->mem = vb2_plane_vaddr(vb, 0);
  62. buf->length = vb2_plane_size(vb, 0);
  63. if (vb->v4l2_buf.type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  64. buf->bytesused = 0;
  65. else
  66. buf->bytesused = vb2_get_plane_payload(vb, 0);
  67. return 0;
  68. }
  69. static void uvc_buffer_queue(struct vb2_buffer *vb)
  70. {
  71. struct uvc_video_queue *queue = vb2_get_drv_priv(vb->vb2_queue);
  72. struct uvc_buffer *buf = container_of(vb, struct uvc_buffer, buf);
  73. unsigned long flags;
  74. spin_lock_irqsave(&queue->irqlock, flags);
  75. if (likely(!(queue->flags & UVC_QUEUE_DISCONNECTED))) {
  76. list_add_tail(&buf->queue, &queue->irqqueue);
  77. } else {
  78. /* If the device is disconnected return the buffer to userspace
  79. * directly. The next QBUF call will fail with -ENODEV.
  80. */
  81. buf->state = UVC_BUF_STATE_ERROR;
  82. vb2_buffer_done(&buf->buf, VB2_BUF_STATE_ERROR);
  83. }
  84. spin_unlock_irqrestore(&queue->irqlock, flags);
  85. }
  86. static void uvc_wait_prepare(struct vb2_queue *vq)
  87. {
  88. struct uvc_video_queue *queue = vb2_get_drv_priv(vq);
  89. mutex_unlock(&queue->mutex);
  90. }
  91. static void uvc_wait_finish(struct vb2_queue *vq)
  92. {
  93. struct uvc_video_queue *queue = vb2_get_drv_priv(vq);
  94. mutex_lock(&queue->mutex);
  95. }
  96. static struct vb2_ops uvc_queue_qops = {
  97. .queue_setup = uvc_queue_setup,
  98. .buf_prepare = uvc_buffer_prepare,
  99. .buf_queue = uvc_buffer_queue,
  100. .wait_prepare = uvc_wait_prepare,
  101. .wait_finish = uvc_wait_finish,
  102. };
  103. static int uvc_queue_init(struct uvc_video_queue *queue,
  104. enum v4l2_buf_type type)
  105. {
  106. int ret;
  107. queue->queue.type = type;
  108. queue->queue.io_modes = VB2_MMAP | VB2_USERPTR;
  109. queue->queue.drv_priv = queue;
  110. queue->queue.buf_struct_size = sizeof(struct uvc_buffer);
  111. queue->queue.ops = &uvc_queue_qops;
  112. queue->queue.mem_ops = &vb2_vmalloc_memops;
  113. ret = vb2_queue_init(&queue->queue);
  114. if (ret)
  115. return ret;
  116. mutex_init(&queue->mutex);
  117. spin_lock_init(&queue->irqlock);
  118. INIT_LIST_HEAD(&queue->irqqueue);
  119. queue->flags = 0;
  120. return 0;
  121. }
  122. /*
  123. * Free the video buffers.
  124. */
  125. static void uvc_free_buffers(struct uvc_video_queue *queue)
  126. {
  127. mutex_lock(&queue->mutex);
  128. vb2_queue_release(&queue->queue);
  129. mutex_unlock(&queue->mutex);
  130. }
  131. /*
  132. * Allocate the video buffers.
  133. */
  134. static int uvc_alloc_buffers(struct uvc_video_queue *queue,
  135. struct v4l2_requestbuffers *rb)
  136. {
  137. int ret;
  138. mutex_lock(&queue->mutex);
  139. ret = vb2_reqbufs(&queue->queue, rb);
  140. mutex_unlock(&queue->mutex);
  141. return ret ? ret : rb->count;
  142. }
  143. static int uvc_query_buffer(struct uvc_video_queue *queue,
  144. struct v4l2_buffer *buf)
  145. {
  146. int ret;
  147. mutex_lock(&queue->mutex);
  148. ret = vb2_querybuf(&queue->queue, buf);
  149. mutex_unlock(&queue->mutex);
  150. return ret;
  151. }
  152. static int uvc_queue_buffer(struct uvc_video_queue *queue,
  153. struct v4l2_buffer *buf)
  154. {
  155. unsigned long flags;
  156. int ret;
  157. mutex_lock(&queue->mutex);
  158. ret = vb2_qbuf(&queue->queue, buf);
  159. if (ret < 0)
  160. goto done;
  161. spin_lock_irqsave(&queue->irqlock, flags);
  162. ret = (queue->flags & UVC_QUEUE_PAUSED) != 0;
  163. queue->flags &= ~UVC_QUEUE_PAUSED;
  164. spin_unlock_irqrestore(&queue->irqlock, flags);
  165. done:
  166. mutex_unlock(&queue->mutex);
  167. return ret;
  168. }
  169. /*
  170. * Dequeue a video buffer. If nonblocking is false, block until a buffer is
  171. * available.
  172. */
  173. static int uvc_dequeue_buffer(struct uvc_video_queue *queue,
  174. struct v4l2_buffer *buf, int nonblocking)
  175. {
  176. int ret;
  177. mutex_lock(&queue->mutex);
  178. ret = vb2_dqbuf(&queue->queue, buf, nonblocking);
  179. mutex_unlock(&queue->mutex);
  180. return ret;
  181. }
  182. /*
  183. * Poll the video queue.
  184. *
  185. * This function implements video queue polling and is intended to be used by
  186. * the device poll handler.
  187. */
  188. static unsigned int uvc_queue_poll(struct uvc_video_queue *queue,
  189. struct file *file, poll_table *wait)
  190. {
  191. unsigned int ret;
  192. mutex_lock(&queue->mutex);
  193. ret = vb2_poll(&queue->queue, file, wait);
  194. mutex_unlock(&queue->mutex);
  195. return ret;
  196. }
  197. static int uvc_queue_mmap(struct uvc_video_queue *queue,
  198. struct vm_area_struct *vma)
  199. {
  200. int ret;
  201. mutex_lock(&queue->mutex);
  202. ret = vb2_mmap(&queue->queue, vma);
  203. mutex_unlock(&queue->mutex);
  204. return ret;
  205. }
  206. #ifndef CONFIG_MMU
  207. /*
  208. * Get unmapped area.
  209. *
  210. * NO-MMU arch need this function to make mmap() work correctly.
  211. */
  212. static unsigned long uvc_queue_get_unmapped_area(struct uvc_video_queue *queue,
  213. unsigned long pgoff)
  214. {
  215. unsigned long ret;
  216. mutex_lock(&queue->mutex);
  217. ret = vb2_get_unmapped_area(&queue->queue, 0, 0, pgoff, 0);
  218. mutex_unlock(&queue->mutex);
  219. return ret;
  220. }
  221. #endif
  222. /*
  223. * Cancel the video buffers queue.
  224. *
  225. * Cancelling the queue marks all buffers on the irq queue as erroneous,
  226. * wakes them up and removes them from the queue.
  227. *
  228. * If the disconnect parameter is set, further calls to uvc_queue_buffer will
  229. * fail with -ENODEV.
  230. *
  231. * This function acquires the irq spinlock and can be called from interrupt
  232. * context.
  233. */
  234. static void uvc_queue_cancel(struct uvc_video_queue *queue, int disconnect)
  235. {
  236. struct uvc_buffer *buf;
  237. unsigned long flags;
  238. spin_lock_irqsave(&queue->irqlock, flags);
  239. while (!list_empty(&queue->irqqueue)) {
  240. buf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
  241. queue);
  242. list_del(&buf->queue);
  243. buf->state = UVC_BUF_STATE_ERROR;
  244. vb2_buffer_done(&buf->buf, VB2_BUF_STATE_ERROR);
  245. }
  246. /* This must be protected by the irqlock spinlock to avoid race
  247. * conditions between uvc_queue_buffer and the disconnection event that
  248. * could result in an interruptible wait in uvc_dequeue_buffer. Do not
  249. * blindly replace this logic by checking for the UVC_DEV_DISCONNECTED
  250. * state outside the queue code.
  251. */
  252. if (disconnect)
  253. queue->flags |= UVC_QUEUE_DISCONNECTED;
  254. spin_unlock_irqrestore(&queue->irqlock, flags);
  255. }
  256. /*
  257. * Enable or disable the video buffers queue.
  258. *
  259. * The queue must be enabled before starting video acquisition and must be
  260. * disabled after stopping it. This ensures that the video buffers queue
  261. * state can be properly initialized before buffers are accessed from the
  262. * interrupt handler.
  263. *
  264. * Enabling the video queue initializes parameters (such as sequence number,
  265. * sync pattern, ...). If the queue is already enabled, return -EBUSY.
  266. *
  267. * Disabling the video queue cancels the queue and removes all buffers from
  268. * the main queue.
  269. *
  270. * This function can't be called from interrupt context. Use
  271. * uvc_queue_cancel() instead.
  272. */
  273. static int uvc_queue_enable(struct uvc_video_queue *queue, int enable)
  274. {
  275. unsigned long flags;
  276. int ret = 0;
  277. mutex_lock(&queue->mutex);
  278. if (enable) {
  279. ret = vb2_streamon(&queue->queue, queue->queue.type);
  280. if (ret < 0)
  281. goto done;
  282. queue->sequence = 0;
  283. queue->buf_used = 0;
  284. } else {
  285. ret = vb2_streamoff(&queue->queue, queue->queue.type);
  286. if (ret < 0)
  287. goto done;
  288. spin_lock_irqsave(&queue->irqlock, flags);
  289. INIT_LIST_HEAD(&queue->irqqueue);
  290. /*
  291. * FIXME: We need to clear the DISCONNECTED flag to ensure that
  292. * applications will be able to queue buffers for the next
  293. * streaming run. However, clearing it here doesn't guarantee
  294. * that the device will be reconnected in the meantime.
  295. */
  296. queue->flags &= ~UVC_QUEUE_DISCONNECTED;
  297. spin_unlock_irqrestore(&queue->irqlock, flags);
  298. }
  299. done:
  300. mutex_unlock(&queue->mutex);
  301. return ret;
  302. }
  303. /* called with &queue_irqlock held.. */
  304. static struct uvc_buffer *uvc_queue_next_buffer(struct uvc_video_queue *queue,
  305. struct uvc_buffer *buf)
  306. {
  307. struct uvc_buffer *nextbuf;
  308. if ((queue->flags & UVC_QUEUE_DROP_INCOMPLETE) &&
  309. buf->length != buf->bytesused) {
  310. buf->state = UVC_BUF_STATE_QUEUED;
  311. vb2_set_plane_payload(&buf->buf, 0, 0);
  312. return buf;
  313. }
  314. list_del(&buf->queue);
  315. if (!list_empty(&queue->irqqueue))
  316. nextbuf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
  317. queue);
  318. else
  319. nextbuf = NULL;
  320. /*
  321. * FIXME: with videobuf2, the sequence number or timestamp fields
  322. * are valid only for video capture devices and the UVC gadget usually
  323. * is a video output device. Keeping these until the specs are clear on
  324. * this aspect.
  325. */
  326. buf->buf.v4l2_buf.sequence = queue->sequence++;
  327. do_gettimeofday(&buf->buf.v4l2_buf.timestamp);
  328. vb2_set_plane_payload(&buf->buf, 0, buf->bytesused);
  329. vb2_buffer_done(&buf->buf, VB2_BUF_STATE_DONE);
  330. return nextbuf;
  331. }
  332. static struct uvc_buffer *uvc_queue_head(struct uvc_video_queue *queue)
  333. {
  334. struct uvc_buffer *buf = NULL;
  335. if (!list_empty(&queue->irqqueue))
  336. buf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
  337. queue);
  338. else
  339. queue->flags |= UVC_QUEUE_PAUSED;
  340. return buf;
  341. }