uvc_queue.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. /*
  2. * uvc_queue.c -- USB Video Class driver - Buffers management
  3. *
  4. * Copyright (C) 2005-2008
  5. * Laurent Pinchart (laurent.pinchart@skynet.be)
  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/kernel.h>
  14. #include <linux/version.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 <asm/atomic.h>
  22. #include "uvcvideo.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 buffer allocation and freeing are performed by uvc_alloc_buffers and
  30. * uvc_free_buffers respectively. The former acquires the video queue lock,
  31. * while the later must be called with the lock held (so that allocation can
  32. * free previously allocated buffers). Trying to free buffers that are mapped
  33. * to user space will return -EBUSY.
  34. *
  35. * Video buffers are managed using two queues. However, unlike most USB video
  36. * drivers which use an in queue and an out queue, we use a main queue which
  37. * holds all queued buffers (both 'empty' and 'done' buffers), and an irq
  38. * queue which holds empty buffers. This design (copied from video-buf)
  39. * minimizes locking in interrupt, as only one queue is shared between
  40. * interrupt and user contexts.
  41. *
  42. * Use cases
  43. * ---------
  44. *
  45. * Unless stated otherwise, all operations which modify the irq buffers queue
  46. * are protected by the irq spinlock.
  47. *
  48. * 1. The user queues the buffers, starts streaming and dequeues a buffer.
  49. *
  50. * The buffers are added to the main and irq queues. Both operations are
  51. * protected by the queue lock, and the latert is protected by the irq
  52. * spinlock as well.
  53. *
  54. * The completion handler fetches a buffer from the irq queue and fills it
  55. * with video data. If no buffer is available (irq queue empty), the handler
  56. * returns immediately.
  57. *
  58. * When the buffer is full, the completion handler removes it from the irq
  59. * queue, marks it as ready (UVC_BUF_STATE_DONE) and wake its wait queue.
  60. * At that point, any process waiting on the buffer will be woken up. If a
  61. * process tries to dequeue a buffer after it has been marked ready, the
  62. * dequeing will succeed immediately.
  63. *
  64. * 2. Buffers are queued, user is waiting on a buffer and the device gets
  65. * disconnected.
  66. *
  67. * When the device is disconnected, the kernel calls the completion handler
  68. * with an appropriate status code. The handler marks all buffers in the
  69. * irq queue as being erroneous (UVC_BUF_STATE_ERROR) and wakes them up so
  70. * that any process waiting on a buffer gets woken up.
  71. *
  72. * Waking up up the first buffer on the irq list is not enough, as the
  73. * process waiting on the buffer might restart the dequeue operation
  74. * immediately.
  75. *
  76. */
  77. void uvc_queue_init(struct uvc_video_queue *queue)
  78. {
  79. mutex_init(&queue->mutex);
  80. spin_lock_init(&queue->irqlock);
  81. INIT_LIST_HEAD(&queue->mainqueue);
  82. INIT_LIST_HEAD(&queue->irqqueue);
  83. }
  84. /*
  85. * Allocate the video buffers.
  86. *
  87. * Pages are reserved to make sure they will not be swaped, as they will be
  88. * filled in URB completion handler.
  89. *
  90. * Buffers will be individually mapped, so they must all be page aligned.
  91. */
  92. int uvc_alloc_buffers(struct uvc_video_queue *queue, unsigned int nbuffers,
  93. unsigned int buflength)
  94. {
  95. unsigned int bufsize = PAGE_ALIGN(buflength);
  96. unsigned int i;
  97. void *mem = NULL;
  98. int ret;
  99. if (nbuffers > UVC_MAX_VIDEO_BUFFERS)
  100. nbuffers = UVC_MAX_VIDEO_BUFFERS;
  101. mutex_lock(&queue->mutex);
  102. if ((ret = uvc_free_buffers(queue)) < 0)
  103. goto done;
  104. /* Bail out if no buffers should be allocated. */
  105. if (nbuffers == 0)
  106. goto done;
  107. /* Decrement the number of buffers until allocation succeeds. */
  108. for (; nbuffers > 0; --nbuffers) {
  109. mem = vmalloc_32(nbuffers * bufsize);
  110. if (mem != NULL)
  111. break;
  112. }
  113. if (mem == NULL) {
  114. ret = -ENOMEM;
  115. goto done;
  116. }
  117. for (i = 0; i < nbuffers; ++i) {
  118. memset(&queue->buffer[i], 0, sizeof queue->buffer[i]);
  119. queue->buffer[i].buf.index = i;
  120. queue->buffer[i].buf.m.offset = i * bufsize;
  121. queue->buffer[i].buf.length = buflength;
  122. queue->buffer[i].buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
  123. queue->buffer[i].buf.sequence = 0;
  124. queue->buffer[i].buf.field = V4L2_FIELD_NONE;
  125. queue->buffer[i].buf.memory = V4L2_MEMORY_MMAP;
  126. queue->buffer[i].buf.flags = 0;
  127. init_waitqueue_head(&queue->buffer[i].wait);
  128. }
  129. queue->mem = mem;
  130. queue->count = nbuffers;
  131. queue->buf_size = bufsize;
  132. ret = nbuffers;
  133. done:
  134. mutex_unlock(&queue->mutex);
  135. return ret;
  136. }
  137. /*
  138. * Free the video buffers.
  139. *
  140. * This function must be called with the queue lock held.
  141. */
  142. int uvc_free_buffers(struct uvc_video_queue *queue)
  143. {
  144. unsigned int i;
  145. for (i = 0; i < queue->count; ++i) {
  146. if (queue->buffer[i].vma_use_count != 0)
  147. return -EBUSY;
  148. }
  149. if (queue->count) {
  150. vfree(queue->mem);
  151. queue->count = 0;
  152. }
  153. return 0;
  154. }
  155. static void __uvc_query_buffer(struct uvc_buffer *buf,
  156. struct v4l2_buffer *v4l2_buf)
  157. {
  158. memcpy(v4l2_buf, &buf->buf, sizeof *v4l2_buf);
  159. if (buf->vma_use_count)
  160. v4l2_buf->flags |= V4L2_BUF_FLAG_MAPPED;
  161. switch (buf->state) {
  162. case UVC_BUF_STATE_ERROR:
  163. case UVC_BUF_STATE_DONE:
  164. v4l2_buf->flags |= V4L2_BUF_FLAG_DONE;
  165. break;
  166. case UVC_BUF_STATE_QUEUED:
  167. case UVC_BUF_STATE_ACTIVE:
  168. v4l2_buf->flags |= V4L2_BUF_FLAG_QUEUED;
  169. break;
  170. case UVC_BUF_STATE_IDLE:
  171. default:
  172. break;
  173. }
  174. }
  175. int uvc_query_buffer(struct uvc_video_queue *queue,
  176. struct v4l2_buffer *v4l2_buf)
  177. {
  178. int ret = 0;
  179. mutex_lock(&queue->mutex);
  180. if (v4l2_buf->index >= queue->count) {
  181. ret = -EINVAL;
  182. goto done;
  183. }
  184. __uvc_query_buffer(&queue->buffer[v4l2_buf->index], v4l2_buf);
  185. done:
  186. mutex_unlock(&queue->mutex);
  187. return ret;
  188. }
  189. /*
  190. * Queue a video buffer. Attempting to queue a buffer that has already been
  191. * queued will return -EINVAL.
  192. */
  193. int uvc_queue_buffer(struct uvc_video_queue *queue,
  194. struct v4l2_buffer *v4l2_buf)
  195. {
  196. struct uvc_buffer *buf;
  197. unsigned long flags;
  198. int ret = 0;
  199. uvc_trace(UVC_TRACE_CAPTURE, "Queuing buffer %u.\n", v4l2_buf->index);
  200. if (v4l2_buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  201. v4l2_buf->memory != V4L2_MEMORY_MMAP) {
  202. uvc_trace(UVC_TRACE_CAPTURE, "[E] Invalid buffer type (%u) "
  203. "and/or memory (%u).\n", v4l2_buf->type,
  204. v4l2_buf->memory);
  205. return -EINVAL;
  206. }
  207. mutex_lock(&queue->mutex);
  208. if (v4l2_buf->index >= queue->count) {
  209. uvc_trace(UVC_TRACE_CAPTURE, "[E] Out of range index.\n");
  210. ret = -EINVAL;
  211. goto done;
  212. }
  213. buf = &queue->buffer[v4l2_buf->index];
  214. if (buf->state != UVC_BUF_STATE_IDLE) {
  215. uvc_trace(UVC_TRACE_CAPTURE, "[E] Invalid buffer state "
  216. "(%u).\n", buf->state);
  217. ret = -EINVAL;
  218. goto done;
  219. }
  220. spin_lock_irqsave(&queue->irqlock, flags);
  221. if (queue->flags & UVC_QUEUE_DISCONNECTED) {
  222. spin_unlock_irqrestore(&queue->irqlock, flags);
  223. ret = -ENODEV;
  224. goto done;
  225. }
  226. buf->state = UVC_BUF_STATE_QUEUED;
  227. buf->buf.bytesused = 0;
  228. list_add_tail(&buf->stream, &queue->mainqueue);
  229. list_add_tail(&buf->queue, &queue->irqqueue);
  230. spin_unlock_irqrestore(&queue->irqlock, flags);
  231. done:
  232. mutex_unlock(&queue->mutex);
  233. return ret;
  234. }
  235. static int uvc_queue_waiton(struct uvc_buffer *buf, int nonblocking)
  236. {
  237. if (nonblocking) {
  238. return (buf->state != UVC_BUF_STATE_QUEUED &&
  239. buf->state != UVC_BUF_STATE_ACTIVE)
  240. ? 0 : -EAGAIN;
  241. }
  242. return wait_event_interruptible(buf->wait,
  243. buf->state != UVC_BUF_STATE_QUEUED &&
  244. buf->state != UVC_BUF_STATE_ACTIVE);
  245. }
  246. /*
  247. * Dequeue a video buffer. If nonblocking is false, block until a buffer is
  248. * available.
  249. */
  250. int uvc_dequeue_buffer(struct uvc_video_queue *queue,
  251. struct v4l2_buffer *v4l2_buf, int nonblocking)
  252. {
  253. struct uvc_buffer *buf;
  254. int ret = 0;
  255. if (v4l2_buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE ||
  256. v4l2_buf->memory != V4L2_MEMORY_MMAP) {
  257. uvc_trace(UVC_TRACE_CAPTURE, "[E] Invalid buffer type (%u) "
  258. "and/or memory (%u).\n", v4l2_buf->type,
  259. v4l2_buf->memory);
  260. return -EINVAL;
  261. }
  262. mutex_lock(&queue->mutex);
  263. if (list_empty(&queue->mainqueue)) {
  264. uvc_trace(UVC_TRACE_CAPTURE, "[E] Empty buffer queue.\n");
  265. ret = -EINVAL;
  266. goto done;
  267. }
  268. buf = list_first_entry(&queue->mainqueue, struct uvc_buffer, stream);
  269. if ((ret = uvc_queue_waiton(buf, nonblocking)) < 0)
  270. goto done;
  271. uvc_trace(UVC_TRACE_CAPTURE, "Dequeuing buffer %u (%u, %u bytes).\n",
  272. buf->buf.index, buf->state, buf->buf.bytesused);
  273. switch (buf->state) {
  274. case UVC_BUF_STATE_ERROR:
  275. uvc_trace(UVC_TRACE_CAPTURE, "[W] Corrupted data "
  276. "(transmission error).\n");
  277. ret = -EIO;
  278. case UVC_BUF_STATE_DONE:
  279. buf->state = UVC_BUF_STATE_IDLE;
  280. break;
  281. case UVC_BUF_STATE_IDLE:
  282. case UVC_BUF_STATE_QUEUED:
  283. case UVC_BUF_STATE_ACTIVE:
  284. default:
  285. uvc_trace(UVC_TRACE_CAPTURE, "[E] Invalid buffer state %u "
  286. "(driver bug?).\n", buf->state);
  287. ret = -EINVAL;
  288. goto done;
  289. }
  290. list_del(&buf->stream);
  291. __uvc_query_buffer(buf, v4l2_buf);
  292. done:
  293. mutex_unlock(&queue->mutex);
  294. return ret;
  295. }
  296. /*
  297. * Poll the video queue.
  298. *
  299. * This function implements video queue polling and is intended to be used by
  300. * the device poll handler.
  301. */
  302. unsigned int uvc_queue_poll(struct uvc_video_queue *queue, struct file *file,
  303. poll_table *wait)
  304. {
  305. struct uvc_buffer *buf;
  306. unsigned int mask = 0;
  307. mutex_lock(&queue->mutex);
  308. if (list_empty(&queue->mainqueue)) {
  309. mask |= POLLERR;
  310. goto done;
  311. }
  312. buf = list_first_entry(&queue->mainqueue, struct uvc_buffer, stream);
  313. poll_wait(file, &buf->wait, wait);
  314. if (buf->state == UVC_BUF_STATE_DONE ||
  315. buf->state == UVC_BUF_STATE_ERROR)
  316. mask |= POLLIN | POLLRDNORM;
  317. done:
  318. mutex_unlock(&queue->mutex);
  319. return mask;
  320. }
  321. /*
  322. * Enable or disable the video buffers queue.
  323. *
  324. * The queue must be enabled before starting video acquisition and must be
  325. * disabled after stopping it. This ensures that the video buffers queue
  326. * state can be properly initialized before buffers are accessed from the
  327. * interrupt handler.
  328. *
  329. * Enabling the video queue initializes parameters (such as sequence number,
  330. * sync pattern, ...). If the queue is already enabled, return -EBUSY.
  331. *
  332. * Disabling the video queue cancels the queue and removes all buffers from
  333. * the main queue.
  334. *
  335. * This function can't be called from interrupt context. Use
  336. * uvc_queue_cancel() instead.
  337. */
  338. int uvc_queue_enable(struct uvc_video_queue *queue, int enable)
  339. {
  340. unsigned int i;
  341. int ret = 0;
  342. mutex_lock(&queue->mutex);
  343. if (enable) {
  344. if (uvc_queue_streaming(queue)) {
  345. ret = -EBUSY;
  346. goto done;
  347. }
  348. queue->sequence = 0;
  349. queue->flags |= UVC_QUEUE_STREAMING;
  350. } else {
  351. uvc_queue_cancel(queue, 0);
  352. INIT_LIST_HEAD(&queue->mainqueue);
  353. for (i = 0; i < queue->count; ++i)
  354. queue->buffer[i].state = UVC_BUF_STATE_IDLE;
  355. queue->flags &= ~UVC_QUEUE_STREAMING;
  356. }
  357. done:
  358. mutex_unlock(&queue->mutex);
  359. return ret;
  360. }
  361. /*
  362. * Cancel the video buffers queue.
  363. *
  364. * Cancelling the queue marks all buffers on the irq queue as erroneous,
  365. * wakes them up and remove them from the queue.
  366. *
  367. * If the disconnect parameter is set, further calls to uvc_queue_buffer will
  368. * fail with -ENODEV.
  369. *
  370. * This function acquires the irq spinlock and can be called from interrupt
  371. * context.
  372. */
  373. void uvc_queue_cancel(struct uvc_video_queue *queue, int disconnect)
  374. {
  375. struct uvc_buffer *buf;
  376. unsigned long flags;
  377. spin_lock_irqsave(&queue->irqlock, flags);
  378. while (!list_empty(&queue->irqqueue)) {
  379. buf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
  380. queue);
  381. list_del(&buf->queue);
  382. buf->state = UVC_BUF_STATE_ERROR;
  383. wake_up(&buf->wait);
  384. }
  385. /* This must be protected by the irqlock spinlock to avoid race
  386. * conditions between uvc_queue_buffer and the disconnection event that
  387. * could result in an interruptible wait in uvc_dequeue_buffer. Do not
  388. * blindly replace this logic by checking for the UVC_DEV_DISCONNECTED
  389. * state outside the queue code.
  390. */
  391. if (disconnect)
  392. queue->flags |= UVC_QUEUE_DISCONNECTED;
  393. spin_unlock_irqrestore(&queue->irqlock, flags);
  394. }
  395. struct uvc_buffer *uvc_queue_next_buffer(struct uvc_video_queue *queue,
  396. struct uvc_buffer *buf)
  397. {
  398. struct uvc_buffer *nextbuf;
  399. unsigned long flags;
  400. if ((queue->flags & UVC_QUEUE_DROP_INCOMPLETE) &&
  401. buf->buf.length != buf->buf.bytesused) {
  402. buf->state = UVC_BUF_STATE_QUEUED;
  403. buf->buf.bytesused = 0;
  404. return buf;
  405. }
  406. spin_lock_irqsave(&queue->irqlock, flags);
  407. list_del(&buf->queue);
  408. if (!list_empty(&queue->irqqueue))
  409. nextbuf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
  410. queue);
  411. else
  412. nextbuf = NULL;
  413. spin_unlock_irqrestore(&queue->irqlock, flags);
  414. buf->buf.sequence = queue->sequence++;
  415. do_gettimeofday(&buf->buf.timestamp);
  416. wake_up(&buf->wait);
  417. return nextbuf;
  418. }