uvc_queue.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  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/kernel.h>
  13. #include <linux/mm.h>
  14. #include <linux/list.h>
  15. #include <linux/module.h>
  16. #include <linux/usb.h>
  17. #include <linux/videodev2.h>
  18. #include <linux/vmalloc.h>
  19. #include <linux/wait.h>
  20. #include <linux/atomic.h>
  21. #include "uvc.h"
  22. /* ------------------------------------------------------------------------
  23. * Video buffers queue management.
  24. *
  25. * Video queues is initialized by uvc_queue_init(). The function performs
  26. * basic initialization of the uvc_video_queue struct and never fails.
  27. *
  28. * Video buffer allocation and freeing are performed by uvc_alloc_buffers and
  29. * uvc_free_buffers respectively. The former acquires the video queue lock,
  30. * while the later must be called with the lock held (so that allocation can
  31. * free previously allocated buffers). Trying to free buffers that are mapped
  32. * to user space will return -EBUSY.
  33. *
  34. * Video buffers are managed using two queues. However, unlike most USB video
  35. * drivers that use an in queue and an out queue, we use a main queue to hold
  36. * all queued buffers (both 'empty' and 'done' buffers), and an irq queue to
  37. * hold empty buffers. This design (copied from video-buf) minimizes locking
  38. * in interrupt, as only one queue is shared between interrupt and user
  39. * contexts.
  40. *
  41. * Use cases
  42. * ---------
  43. *
  44. * Unless stated otherwise, all operations that modify the irq buffers queue
  45. * are protected by the irq spinlock.
  46. *
  47. * 1. The user queues the buffers, starts streaming and dequeues a buffer.
  48. *
  49. * The buffers are added to the main and irq queues. Both operations are
  50. * protected by the queue lock, and the later is protected by the irq
  51. * spinlock as well.
  52. *
  53. * The completion handler fetches a buffer from the irq queue and fills it
  54. * with video data. If no buffer is available (irq queue empty), the handler
  55. * returns immediately.
  56. *
  57. * When the buffer is full, the completion handler removes it from the irq
  58. * queue, marks it as ready (UVC_BUF_STATE_DONE) and wakes its wait queue.
  59. * At that point, any process waiting on the buffer will be woken up. If a
  60. * process tries to dequeue a buffer after it has been marked ready, the
  61. * dequeing will succeed immediately.
  62. *
  63. * 2. Buffers are queued, user is waiting on a buffer and the device gets
  64. * disconnected.
  65. *
  66. * When the device is disconnected, the kernel calls the completion handler
  67. * with an appropriate status code. The handler marks all buffers in the
  68. * irq queue as being erroneous (UVC_BUF_STATE_ERROR) and wakes them up so
  69. * that any process waiting on a buffer gets woken up.
  70. *
  71. * Waking up up the first buffer on the irq list is not enough, as the
  72. * process waiting on the buffer might restart the dequeue operation
  73. * immediately.
  74. *
  75. */
  76. static void
  77. 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. * Free the video buffers.
  87. *
  88. * This function must be called with the queue lock held.
  89. */
  90. static int uvc_free_buffers(struct uvc_video_queue *queue)
  91. {
  92. unsigned int i;
  93. for (i = 0; i < queue->count; ++i) {
  94. if (queue->buffer[i].vma_use_count != 0)
  95. return -EBUSY;
  96. }
  97. if (queue->count) {
  98. vfree(queue->mem);
  99. queue->count = 0;
  100. }
  101. return 0;
  102. }
  103. /*
  104. * Allocate the video buffers.
  105. *
  106. * Pages are reserved to make sure they will not be swapped, as they will be
  107. * filled in the URB completion handler.
  108. *
  109. * Buffers will be individually mapped, so they must all be page aligned.
  110. */
  111. static int
  112. uvc_alloc_buffers(struct uvc_video_queue *queue, unsigned int nbuffers,
  113. unsigned int buflength)
  114. {
  115. unsigned int bufsize = PAGE_ALIGN(buflength);
  116. unsigned int i;
  117. void *mem = NULL;
  118. int ret;
  119. if (nbuffers > UVC_MAX_VIDEO_BUFFERS)
  120. nbuffers = UVC_MAX_VIDEO_BUFFERS;
  121. mutex_lock(&queue->mutex);
  122. if ((ret = uvc_free_buffers(queue)) < 0)
  123. goto done;
  124. /* Bail out if no buffers should be allocated. */
  125. if (nbuffers == 0)
  126. goto done;
  127. /* Decrement the number of buffers until allocation succeeds. */
  128. for (; nbuffers > 0; --nbuffers) {
  129. mem = vmalloc_32(nbuffers * bufsize);
  130. if (mem != NULL)
  131. break;
  132. }
  133. if (mem == NULL) {
  134. ret = -ENOMEM;
  135. goto done;
  136. }
  137. for (i = 0; i < nbuffers; ++i) {
  138. memset(&queue->buffer[i], 0, sizeof queue->buffer[i]);
  139. queue->buffer[i].buf.index = i;
  140. queue->buffer[i].buf.m.offset = i * bufsize;
  141. queue->buffer[i].buf.length = buflength;
  142. queue->buffer[i].buf.type = queue->type;
  143. queue->buffer[i].buf.sequence = 0;
  144. queue->buffer[i].buf.field = V4L2_FIELD_NONE;
  145. queue->buffer[i].buf.memory = V4L2_MEMORY_MMAP;
  146. queue->buffer[i].buf.flags = 0;
  147. init_waitqueue_head(&queue->buffer[i].wait);
  148. }
  149. queue->mem = mem;
  150. queue->count = nbuffers;
  151. queue->buf_size = bufsize;
  152. ret = nbuffers;
  153. done:
  154. mutex_unlock(&queue->mutex);
  155. return ret;
  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. static int
  178. uvc_query_buffer(struct uvc_video_queue *queue, 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. static int
  196. uvc_queue_buffer(struct uvc_video_queue *queue, 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. if (v4l2_buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  229. buf->buf.bytesused = 0;
  230. else
  231. buf->buf.bytesused = v4l2_buf->bytesused;
  232. spin_lock_irqsave(&queue->irqlock, flags);
  233. if (queue->flags & UVC_QUEUE_DISCONNECTED) {
  234. spin_unlock_irqrestore(&queue->irqlock, flags);
  235. ret = -ENODEV;
  236. goto done;
  237. }
  238. buf->state = UVC_BUF_STATE_QUEUED;
  239. ret = (queue->flags & UVC_QUEUE_PAUSED) != 0;
  240. queue->flags &= ~UVC_QUEUE_PAUSED;
  241. list_add_tail(&buf->stream, &queue->mainqueue);
  242. list_add_tail(&buf->queue, &queue->irqqueue);
  243. spin_unlock_irqrestore(&queue->irqlock, flags);
  244. done:
  245. mutex_unlock(&queue->mutex);
  246. return ret;
  247. }
  248. static int uvc_queue_waiton(struct uvc_buffer *buf, int nonblocking)
  249. {
  250. if (nonblocking) {
  251. return (buf->state != UVC_BUF_STATE_QUEUED &&
  252. buf->state != UVC_BUF_STATE_ACTIVE)
  253. ? 0 : -EAGAIN;
  254. }
  255. return wait_event_interruptible(buf->wait,
  256. buf->state != UVC_BUF_STATE_QUEUED &&
  257. buf->state != UVC_BUF_STATE_ACTIVE);
  258. }
  259. /*
  260. * Dequeue a video buffer. If nonblocking is false, block until a buffer is
  261. * available.
  262. */
  263. static int
  264. uvc_dequeue_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *v4l2_buf,
  265. int nonblocking)
  266. {
  267. struct uvc_buffer *buf;
  268. int ret = 0;
  269. if (v4l2_buf->type != queue->type ||
  270. v4l2_buf->memory != V4L2_MEMORY_MMAP) {
  271. uvc_trace(UVC_TRACE_CAPTURE, "[E] Invalid buffer type (%u) "
  272. "and/or memory (%u).\n", v4l2_buf->type,
  273. v4l2_buf->memory);
  274. return -EINVAL;
  275. }
  276. mutex_lock(&queue->mutex);
  277. if (list_empty(&queue->mainqueue)) {
  278. uvc_trace(UVC_TRACE_CAPTURE, "[E] Empty buffer queue.\n");
  279. ret = -EINVAL;
  280. goto done;
  281. }
  282. buf = list_first_entry(&queue->mainqueue, struct uvc_buffer, stream);
  283. if ((ret = uvc_queue_waiton(buf, nonblocking)) < 0)
  284. goto done;
  285. uvc_trace(UVC_TRACE_CAPTURE, "Dequeuing buffer %u (%u, %u bytes).\n",
  286. buf->buf.index, buf->state, buf->buf.bytesused);
  287. switch (buf->state) {
  288. case UVC_BUF_STATE_ERROR:
  289. uvc_trace(UVC_TRACE_CAPTURE, "[W] Corrupted data "
  290. "(transmission error).\n");
  291. ret = -EIO;
  292. case UVC_BUF_STATE_DONE:
  293. buf->state = UVC_BUF_STATE_IDLE;
  294. break;
  295. case UVC_BUF_STATE_IDLE:
  296. case UVC_BUF_STATE_QUEUED:
  297. case UVC_BUF_STATE_ACTIVE:
  298. default:
  299. uvc_trace(UVC_TRACE_CAPTURE, "[E] Invalid buffer state %u "
  300. "(driver bug?).\n", buf->state);
  301. ret = -EINVAL;
  302. goto done;
  303. }
  304. list_del(&buf->stream);
  305. __uvc_query_buffer(buf, v4l2_buf);
  306. done:
  307. mutex_unlock(&queue->mutex);
  308. return ret;
  309. }
  310. /*
  311. * Poll the video queue.
  312. *
  313. * This function implements video queue polling and is intended to be used by
  314. * the device poll handler.
  315. */
  316. static unsigned int
  317. uvc_queue_poll(struct uvc_video_queue *queue, struct file *file,
  318. poll_table *wait)
  319. {
  320. struct uvc_buffer *buf;
  321. unsigned int mask = 0;
  322. mutex_lock(&queue->mutex);
  323. if (list_empty(&queue->mainqueue))
  324. goto done;
  325. buf = list_first_entry(&queue->mainqueue, struct uvc_buffer, stream);
  326. poll_wait(file, &buf->wait, wait);
  327. if (buf->state == UVC_BUF_STATE_DONE ||
  328. buf->state == UVC_BUF_STATE_ERROR)
  329. mask |= POLLOUT | POLLWRNORM;
  330. done:
  331. mutex_unlock(&queue->mutex);
  332. return mask;
  333. }
  334. /*
  335. * VMA operations.
  336. */
  337. static void uvc_vm_open(struct vm_area_struct *vma)
  338. {
  339. struct uvc_buffer *buffer = vma->vm_private_data;
  340. buffer->vma_use_count++;
  341. }
  342. static void uvc_vm_close(struct vm_area_struct *vma)
  343. {
  344. struct uvc_buffer *buffer = vma->vm_private_data;
  345. buffer->vma_use_count--;
  346. }
  347. static struct vm_operations_struct uvc_vm_ops = {
  348. .open = uvc_vm_open,
  349. .close = uvc_vm_close,
  350. };
  351. /*
  352. * Memory-map a buffer.
  353. *
  354. * This function implements video buffer memory mapping and is intended to be
  355. * used by the device mmap handler.
  356. */
  357. static int
  358. uvc_queue_mmap(struct uvc_video_queue *queue, struct vm_area_struct *vma)
  359. {
  360. struct uvc_buffer *uninitialized_var(buffer);
  361. struct page *page;
  362. unsigned long addr, start, size;
  363. unsigned int i;
  364. int ret = 0;
  365. start = vma->vm_start;
  366. size = vma->vm_end - vma->vm_start;
  367. mutex_lock(&queue->mutex);
  368. for (i = 0; i < queue->count; ++i) {
  369. buffer = &queue->buffer[i];
  370. if ((buffer->buf.m.offset >> PAGE_SHIFT) == vma->vm_pgoff)
  371. break;
  372. }
  373. if (i == queue->count || size != queue->buf_size) {
  374. ret = -EINVAL;
  375. goto done;
  376. }
  377. /*
  378. * VM_IO marks the area as being an mmaped region for I/O to a
  379. * device. It also prevents the region from being core dumped.
  380. */
  381. vma->vm_flags |= VM_IO;
  382. addr = (unsigned long)queue->mem + buffer->buf.m.offset;
  383. while (size > 0) {
  384. page = vmalloc_to_page((void *)addr);
  385. if ((ret = vm_insert_page(vma, start, page)) < 0)
  386. goto done;
  387. start += PAGE_SIZE;
  388. addr += PAGE_SIZE;
  389. size -= PAGE_SIZE;
  390. }
  391. vma->vm_ops = &uvc_vm_ops;
  392. vma->vm_private_data = buffer;
  393. uvc_vm_open(vma);
  394. done:
  395. mutex_unlock(&queue->mutex);
  396. return ret;
  397. }
  398. /*
  399. * Cancel the video buffers queue.
  400. *
  401. * Cancelling the queue marks all buffers on the irq queue as erroneous,
  402. * wakes them up and removes them from the queue.
  403. *
  404. * If the disconnect parameter is set, further calls to uvc_queue_buffer will
  405. * fail with -ENODEV.
  406. *
  407. * This function acquires the irq spinlock and can be called from interrupt
  408. * context.
  409. */
  410. static void uvc_queue_cancel(struct uvc_video_queue *queue, int disconnect)
  411. {
  412. struct uvc_buffer *buf;
  413. unsigned long flags;
  414. spin_lock_irqsave(&queue->irqlock, flags);
  415. while (!list_empty(&queue->irqqueue)) {
  416. buf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
  417. queue);
  418. list_del(&buf->queue);
  419. buf->state = UVC_BUF_STATE_ERROR;
  420. wake_up(&buf->wait);
  421. }
  422. /* This must be protected by the irqlock spinlock to avoid race
  423. * conditions between uvc_queue_buffer and the disconnection event that
  424. * could result in an interruptible wait in uvc_dequeue_buffer. Do not
  425. * blindly replace this logic by checking for the UVC_DEV_DISCONNECTED
  426. * state outside the queue code.
  427. */
  428. if (disconnect)
  429. queue->flags |= UVC_QUEUE_DISCONNECTED;
  430. spin_unlock_irqrestore(&queue->irqlock, flags);
  431. }
  432. /*
  433. * Enable or disable the video buffers queue.
  434. *
  435. * The queue must be enabled before starting video acquisition and must be
  436. * disabled after stopping it. This ensures that the video buffers queue
  437. * state can be properly initialized before buffers are accessed from the
  438. * interrupt handler.
  439. *
  440. * Enabling the video queue initializes parameters (such as sequence number,
  441. * sync pattern, ...). If the queue is already enabled, return -EBUSY.
  442. *
  443. * Disabling the video queue cancels the queue and removes all buffers from
  444. * the main queue.
  445. *
  446. * This function can't be called from interrupt context. Use
  447. * uvc_queue_cancel() instead.
  448. */
  449. static int uvc_queue_enable(struct uvc_video_queue *queue, int enable)
  450. {
  451. unsigned int i;
  452. int ret = 0;
  453. mutex_lock(&queue->mutex);
  454. if (enable) {
  455. if (uvc_queue_streaming(queue)) {
  456. ret = -EBUSY;
  457. goto done;
  458. }
  459. queue->sequence = 0;
  460. queue->flags |= UVC_QUEUE_STREAMING;
  461. queue->buf_used = 0;
  462. } else {
  463. uvc_queue_cancel(queue, 0);
  464. INIT_LIST_HEAD(&queue->mainqueue);
  465. for (i = 0; i < queue->count; ++i)
  466. queue->buffer[i].state = UVC_BUF_STATE_IDLE;
  467. queue->flags &= ~UVC_QUEUE_STREAMING;
  468. }
  469. done:
  470. mutex_unlock(&queue->mutex);
  471. return ret;
  472. }
  473. static struct uvc_buffer *
  474. uvc_queue_next_buffer(struct uvc_video_queue *queue, struct uvc_buffer *buf)
  475. {
  476. struct uvc_buffer *nextbuf;
  477. unsigned long flags;
  478. if ((queue->flags & UVC_QUEUE_DROP_INCOMPLETE) &&
  479. buf->buf.length != buf->buf.bytesused) {
  480. buf->state = UVC_BUF_STATE_QUEUED;
  481. buf->buf.bytesused = 0;
  482. return buf;
  483. }
  484. spin_lock_irqsave(&queue->irqlock, flags);
  485. list_del(&buf->queue);
  486. if (!list_empty(&queue->irqqueue))
  487. nextbuf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
  488. queue);
  489. else
  490. nextbuf = NULL;
  491. spin_unlock_irqrestore(&queue->irqlock, flags);
  492. buf->buf.sequence = queue->sequence++;
  493. do_gettimeofday(&buf->buf.timestamp);
  494. wake_up(&buf->wait);
  495. return nextbuf;
  496. }
  497. static struct uvc_buffer *uvc_queue_head(struct uvc_video_queue *queue)
  498. {
  499. struct uvc_buffer *buf = NULL;
  500. if (!list_empty(&queue->irqqueue))
  501. buf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
  502. queue);
  503. else
  504. queue->flags |= UVC_QUEUE_PAUSED;
  505. return buf;
  506. }