uvc_queue.c 13 KB

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