uvc_queue.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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/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 <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 that use an in queue and an out queue, we use a main queue to hold
  37. * all queued buffers (both 'empty' and 'done' buffers), and an irq queue to
  38. * hold empty buffers. This design (copied from video-buf) minimizes locking
  39. * in interrupt, as only one queue is shared between interrupt and user
  40. * contexts.
  41. *
  42. * Use cases
  43. * ---------
  44. *
  45. * Unless stated otherwise, all operations that 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 later 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 done (UVC_BUF_STATE_DONE) and wakes 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 done, 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, enum v4l2_buf_type type,
  78. int drop_corrupted)
  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->flags = drop_corrupted ? UVC_QUEUE_DROP_CORRUPTED : 0;
  85. queue->type = type;
  86. }
  87. /*
  88. * Allocate the video buffers.
  89. *
  90. * Pages are reserved to make sure they will not be swapped, as they will be
  91. * filled in the URB completion handler.
  92. *
  93. * Buffers will be individually mapped, so they must all be page aligned.
  94. */
  95. int uvc_alloc_buffers(struct uvc_video_queue *queue, unsigned int nbuffers,
  96. unsigned int buflength)
  97. {
  98. unsigned int bufsize = PAGE_ALIGN(buflength);
  99. unsigned int i;
  100. void *mem = NULL;
  101. int ret;
  102. if (nbuffers > UVC_MAX_VIDEO_BUFFERS)
  103. nbuffers = UVC_MAX_VIDEO_BUFFERS;
  104. mutex_lock(&queue->mutex);
  105. if ((ret = uvc_free_buffers(queue)) < 0)
  106. goto done;
  107. /* Bail out if no buffers should be allocated. */
  108. if (nbuffers == 0)
  109. goto done;
  110. /* Decrement the number of buffers until allocation succeeds. */
  111. for (; nbuffers > 0; --nbuffers) {
  112. mem = vmalloc_32(nbuffers * bufsize);
  113. if (mem != NULL)
  114. break;
  115. }
  116. if (mem == NULL) {
  117. ret = -ENOMEM;
  118. goto done;
  119. }
  120. for (i = 0; i < nbuffers; ++i) {
  121. memset(&queue->buffer[i], 0, sizeof queue->buffer[i]);
  122. queue->buffer[i].buf.index = i;
  123. queue->buffer[i].buf.m.offset = i * bufsize;
  124. queue->buffer[i].buf.length = buflength;
  125. queue->buffer[i].buf.type = queue->type;
  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. /*
  158. * Check if buffers have been allocated.
  159. */
  160. int uvc_queue_allocated(struct uvc_video_queue *queue)
  161. {
  162. int allocated;
  163. mutex_lock(&queue->mutex);
  164. allocated = queue->count != 0;
  165. mutex_unlock(&queue->mutex);
  166. return allocated;
  167. }
  168. static void __uvc_query_buffer(struct uvc_buffer *buf,
  169. struct v4l2_buffer *v4l2_buf)
  170. {
  171. memcpy(v4l2_buf, &buf->buf, sizeof *v4l2_buf);
  172. if (buf->vma_use_count)
  173. v4l2_buf->flags |= V4L2_BUF_FLAG_MAPPED;
  174. switch (buf->state) {
  175. case UVC_BUF_STATE_ERROR:
  176. case UVC_BUF_STATE_DONE:
  177. v4l2_buf->flags |= V4L2_BUF_FLAG_DONE;
  178. break;
  179. case UVC_BUF_STATE_QUEUED:
  180. case UVC_BUF_STATE_ACTIVE:
  181. case UVC_BUF_STATE_READY:
  182. v4l2_buf->flags |= V4L2_BUF_FLAG_QUEUED;
  183. break;
  184. case UVC_BUF_STATE_IDLE:
  185. default:
  186. break;
  187. }
  188. }
  189. int uvc_query_buffer(struct uvc_video_queue *queue,
  190. struct v4l2_buffer *v4l2_buf)
  191. {
  192. int ret = 0;
  193. mutex_lock(&queue->mutex);
  194. if (v4l2_buf->index >= queue->count) {
  195. ret = -EINVAL;
  196. goto done;
  197. }
  198. __uvc_query_buffer(&queue->buffer[v4l2_buf->index], v4l2_buf);
  199. done:
  200. mutex_unlock(&queue->mutex);
  201. return ret;
  202. }
  203. /*
  204. * Queue a video buffer. Attempting to queue a buffer that has already been
  205. * queued will return -EINVAL.
  206. */
  207. int uvc_queue_buffer(struct uvc_video_queue *queue,
  208. struct v4l2_buffer *v4l2_buf)
  209. {
  210. struct uvc_buffer *buf;
  211. unsigned long flags;
  212. int ret = 0;
  213. uvc_trace(UVC_TRACE_CAPTURE, "Queuing buffer %u.\n", v4l2_buf->index);
  214. if (v4l2_buf->type != queue->type ||
  215. v4l2_buf->memory != V4L2_MEMORY_MMAP) {
  216. uvc_trace(UVC_TRACE_CAPTURE, "[E] Invalid buffer type (%u) "
  217. "and/or memory (%u).\n", v4l2_buf->type,
  218. v4l2_buf->memory);
  219. return -EINVAL;
  220. }
  221. mutex_lock(&queue->mutex);
  222. if (v4l2_buf->index >= queue->count) {
  223. uvc_trace(UVC_TRACE_CAPTURE, "[E] Out of range index.\n");
  224. ret = -EINVAL;
  225. goto done;
  226. }
  227. buf = &queue->buffer[v4l2_buf->index];
  228. if (buf->state != UVC_BUF_STATE_IDLE) {
  229. uvc_trace(UVC_TRACE_CAPTURE, "[E] Invalid buffer state "
  230. "(%u).\n", buf->state);
  231. ret = -EINVAL;
  232. goto done;
  233. }
  234. if (v4l2_buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
  235. v4l2_buf->bytesused > buf->buf.length) {
  236. uvc_trace(UVC_TRACE_CAPTURE, "[E] Bytes used out of bounds.\n");
  237. ret = -EINVAL;
  238. goto done;
  239. }
  240. spin_lock_irqsave(&queue->irqlock, flags);
  241. if (queue->flags & UVC_QUEUE_DISCONNECTED) {
  242. spin_unlock_irqrestore(&queue->irqlock, flags);
  243. ret = -ENODEV;
  244. goto done;
  245. }
  246. buf->state = UVC_BUF_STATE_QUEUED;
  247. if (v4l2_buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  248. buf->buf.bytesused = 0;
  249. else
  250. buf->buf.bytesused = v4l2_buf->bytesused;
  251. list_add_tail(&buf->stream, &queue->mainqueue);
  252. list_add_tail(&buf->queue, &queue->irqqueue);
  253. spin_unlock_irqrestore(&queue->irqlock, flags);
  254. done:
  255. mutex_unlock(&queue->mutex);
  256. return ret;
  257. }
  258. static int uvc_queue_waiton(struct uvc_buffer *buf, int nonblocking)
  259. {
  260. if (nonblocking) {
  261. return (buf->state != UVC_BUF_STATE_QUEUED &&
  262. buf->state != UVC_BUF_STATE_ACTIVE &&
  263. buf->state != UVC_BUF_STATE_READY)
  264. ? 0 : -EAGAIN;
  265. }
  266. return wait_event_interruptible(buf->wait,
  267. buf->state != UVC_BUF_STATE_QUEUED &&
  268. buf->state != UVC_BUF_STATE_ACTIVE &&
  269. buf->state != UVC_BUF_STATE_READY);
  270. }
  271. /*
  272. * Dequeue a video buffer. If nonblocking is false, block until a buffer is
  273. * available.
  274. */
  275. int uvc_dequeue_buffer(struct uvc_video_queue *queue,
  276. struct v4l2_buffer *v4l2_buf, int nonblocking)
  277. {
  278. struct uvc_buffer *buf;
  279. int ret = 0;
  280. if (v4l2_buf->type != queue->type ||
  281. v4l2_buf->memory != V4L2_MEMORY_MMAP) {
  282. uvc_trace(UVC_TRACE_CAPTURE, "[E] Invalid buffer type (%u) "
  283. "and/or memory (%u).\n", v4l2_buf->type,
  284. v4l2_buf->memory);
  285. return -EINVAL;
  286. }
  287. mutex_lock(&queue->mutex);
  288. if (list_empty(&queue->mainqueue)) {
  289. uvc_trace(UVC_TRACE_CAPTURE, "[E] Empty buffer queue.\n");
  290. ret = -EINVAL;
  291. goto done;
  292. }
  293. buf = list_first_entry(&queue->mainqueue, struct uvc_buffer, stream);
  294. if ((ret = uvc_queue_waiton(buf, nonblocking)) < 0)
  295. goto done;
  296. uvc_trace(UVC_TRACE_CAPTURE, "Dequeuing buffer %u (%u, %u bytes).\n",
  297. buf->buf.index, buf->state, buf->buf.bytesused);
  298. switch (buf->state) {
  299. case UVC_BUF_STATE_ERROR:
  300. uvc_trace(UVC_TRACE_CAPTURE, "[W] Corrupted data "
  301. "(transmission error).\n");
  302. ret = -EIO;
  303. case UVC_BUF_STATE_DONE:
  304. buf->state = UVC_BUF_STATE_IDLE;
  305. break;
  306. case UVC_BUF_STATE_IDLE:
  307. case UVC_BUF_STATE_QUEUED:
  308. case UVC_BUF_STATE_ACTIVE:
  309. case UVC_BUF_STATE_READY:
  310. default:
  311. uvc_trace(UVC_TRACE_CAPTURE, "[E] Invalid buffer state %u "
  312. "(driver bug?).\n", buf->state);
  313. ret = -EINVAL;
  314. goto done;
  315. }
  316. list_del(&buf->stream);
  317. __uvc_query_buffer(buf, v4l2_buf);
  318. done:
  319. mutex_unlock(&queue->mutex);
  320. return ret;
  321. }
  322. /*
  323. * Poll the video queue.
  324. *
  325. * This function implements video queue polling and is intended to be used by
  326. * the device poll handler.
  327. */
  328. unsigned int uvc_queue_poll(struct uvc_video_queue *queue, struct file *file,
  329. poll_table *wait)
  330. {
  331. struct uvc_buffer *buf;
  332. unsigned int mask = 0;
  333. mutex_lock(&queue->mutex);
  334. if (list_empty(&queue->mainqueue)) {
  335. mask |= POLLERR;
  336. goto done;
  337. }
  338. buf = list_first_entry(&queue->mainqueue, struct uvc_buffer, stream);
  339. poll_wait(file, &buf->wait, wait);
  340. if (buf->state == UVC_BUF_STATE_DONE ||
  341. buf->state == UVC_BUF_STATE_ERROR) {
  342. if (queue->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  343. mask |= POLLIN | POLLRDNORM;
  344. else
  345. mask |= POLLOUT | POLLWRNORM;
  346. }
  347. done:
  348. mutex_unlock(&queue->mutex);
  349. return mask;
  350. }
  351. /*
  352. * Enable or disable the video buffers queue.
  353. *
  354. * The queue must be enabled before starting video acquisition and must be
  355. * disabled after stopping it. This ensures that the video buffers queue
  356. * state can be properly initialized before buffers are accessed from the
  357. * interrupt handler.
  358. *
  359. * Enabling the video queue returns -EBUSY if the queue is already enabled.
  360. *
  361. * Disabling the video queue cancels the queue and removes all buffers from
  362. * the main queue.
  363. *
  364. * This function can't be called from interrupt context. Use
  365. * uvc_queue_cancel() instead.
  366. */
  367. int uvc_queue_enable(struct uvc_video_queue *queue, int enable)
  368. {
  369. unsigned int i;
  370. int ret = 0;
  371. mutex_lock(&queue->mutex);
  372. if (enable) {
  373. if (uvc_queue_streaming(queue)) {
  374. ret = -EBUSY;
  375. goto done;
  376. }
  377. queue->flags |= UVC_QUEUE_STREAMING;
  378. queue->buf_used = 0;
  379. } else {
  380. uvc_queue_cancel(queue, 0);
  381. INIT_LIST_HEAD(&queue->mainqueue);
  382. for (i = 0; i < queue->count; ++i) {
  383. queue->buffer[i].error = 0;
  384. queue->buffer[i].state = UVC_BUF_STATE_IDLE;
  385. }
  386. queue->flags &= ~UVC_QUEUE_STREAMING;
  387. }
  388. done:
  389. mutex_unlock(&queue->mutex);
  390. return ret;
  391. }
  392. /*
  393. * Cancel the video buffers queue.
  394. *
  395. * Cancelling the queue marks all buffers on the irq queue as erroneous,
  396. * wakes them up and removes them from the queue.
  397. *
  398. * If the disconnect parameter is set, further calls to uvc_queue_buffer will
  399. * fail with -ENODEV.
  400. *
  401. * This function acquires the irq spinlock and can be called from interrupt
  402. * context.
  403. */
  404. void uvc_queue_cancel(struct uvc_video_queue *queue, int disconnect)
  405. {
  406. struct uvc_buffer *buf;
  407. unsigned long flags;
  408. spin_lock_irqsave(&queue->irqlock, flags);
  409. while (!list_empty(&queue->irqqueue)) {
  410. buf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
  411. queue);
  412. list_del(&buf->queue);
  413. buf->state = UVC_BUF_STATE_ERROR;
  414. wake_up(&buf->wait);
  415. }
  416. /* This must be protected by the irqlock spinlock to avoid race
  417. * conditions between uvc_queue_buffer and the disconnection event that
  418. * could result in an interruptible wait in uvc_dequeue_buffer. Do not
  419. * blindly replace this logic by checking for the UVC_DEV_DISCONNECTED
  420. * state outside the queue code.
  421. */
  422. if (disconnect)
  423. queue->flags |= UVC_QUEUE_DISCONNECTED;
  424. spin_unlock_irqrestore(&queue->irqlock, flags);
  425. }
  426. struct uvc_buffer *uvc_queue_next_buffer(struct uvc_video_queue *queue,
  427. struct uvc_buffer *buf)
  428. {
  429. struct uvc_buffer *nextbuf;
  430. unsigned long flags;
  431. if ((queue->flags & UVC_QUEUE_DROP_CORRUPTED) && buf->error) {
  432. buf->error = 0;
  433. buf->state = UVC_BUF_STATE_QUEUED;
  434. buf->buf.bytesused = 0;
  435. return buf;
  436. }
  437. spin_lock_irqsave(&queue->irqlock, flags);
  438. list_del(&buf->queue);
  439. buf->error = 0;
  440. buf->state = UVC_BUF_STATE_DONE;
  441. if (!list_empty(&queue->irqqueue))
  442. nextbuf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
  443. queue);
  444. else
  445. nextbuf = NULL;
  446. spin_unlock_irqrestore(&queue->irqlock, flags);
  447. wake_up(&buf->wait);
  448. return nextbuf;
  449. }