uvc_queue.c 13 KB

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