uvc_queue.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634
  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 <linux/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. * Free the video buffers.
  89. *
  90. * This function must be called with the queue lock held.
  91. */
  92. static int __uvc_free_buffers(struct uvc_video_queue *queue)
  93. {
  94. unsigned int i;
  95. for (i = 0; i < queue->count; ++i) {
  96. if (queue->buffer[i].vma_use_count != 0)
  97. return -EBUSY;
  98. }
  99. if (queue->count) {
  100. uvc_queue_cancel(queue, 0);
  101. INIT_LIST_HEAD(&queue->mainqueue);
  102. vfree(queue->mem);
  103. queue->count = 0;
  104. }
  105. return 0;
  106. }
  107. int uvc_free_buffers(struct uvc_video_queue *queue)
  108. {
  109. int ret;
  110. mutex_lock(&queue->mutex);
  111. ret = __uvc_free_buffers(queue);
  112. mutex_unlock(&queue->mutex);
  113. return ret;
  114. }
  115. /*
  116. * Allocate the video buffers.
  117. *
  118. * Pages are reserved to make sure they will not be swapped, as they will be
  119. * filled in the URB completion handler.
  120. *
  121. * Buffers will be individually mapped, so they must all be page aligned.
  122. */
  123. int uvc_alloc_buffers(struct uvc_video_queue *queue, unsigned int nbuffers,
  124. unsigned int buflength)
  125. {
  126. unsigned int bufsize = PAGE_ALIGN(buflength);
  127. unsigned int i;
  128. void *mem = NULL;
  129. int ret;
  130. if (nbuffers > UVC_MAX_VIDEO_BUFFERS)
  131. nbuffers = UVC_MAX_VIDEO_BUFFERS;
  132. mutex_lock(&queue->mutex);
  133. if ((ret = __uvc_free_buffers(queue)) < 0)
  134. goto done;
  135. /* Bail out if no buffers should be allocated. */
  136. if (nbuffers == 0)
  137. goto done;
  138. /* Decrement the number of buffers until allocation succeeds. */
  139. for (; nbuffers > 0; --nbuffers) {
  140. mem = vmalloc_32(nbuffers * bufsize);
  141. if (mem != NULL)
  142. break;
  143. }
  144. if (mem == NULL) {
  145. ret = -ENOMEM;
  146. goto done;
  147. }
  148. for (i = 0; i < nbuffers; ++i) {
  149. memset(&queue->buffer[i], 0, sizeof queue->buffer[i]);
  150. queue->buffer[i].buf.index = i;
  151. queue->buffer[i].buf.m.offset = i * bufsize;
  152. queue->buffer[i].buf.length = buflength;
  153. queue->buffer[i].buf.type = queue->type;
  154. queue->buffer[i].buf.field = V4L2_FIELD_NONE;
  155. queue->buffer[i].buf.memory = V4L2_MEMORY_MMAP;
  156. queue->buffer[i].buf.flags = 0;
  157. init_waitqueue_head(&queue->buffer[i].wait);
  158. }
  159. queue->mem = mem;
  160. queue->count = nbuffers;
  161. queue->buf_size = bufsize;
  162. ret = nbuffers;
  163. done:
  164. mutex_unlock(&queue->mutex);
  165. return ret;
  166. }
  167. /*
  168. * Check if buffers have been allocated.
  169. */
  170. int uvc_queue_allocated(struct uvc_video_queue *queue)
  171. {
  172. int allocated;
  173. mutex_lock(&queue->mutex);
  174. allocated = queue->count != 0;
  175. mutex_unlock(&queue->mutex);
  176. return allocated;
  177. }
  178. static void __uvc_query_buffer(struct uvc_buffer *buf,
  179. struct v4l2_buffer *v4l2_buf)
  180. {
  181. memcpy(v4l2_buf, &buf->buf, sizeof *v4l2_buf);
  182. if (buf->vma_use_count)
  183. v4l2_buf->flags |= V4L2_BUF_FLAG_MAPPED;
  184. switch (buf->state) {
  185. case UVC_BUF_STATE_ERROR:
  186. case UVC_BUF_STATE_DONE:
  187. v4l2_buf->flags |= V4L2_BUF_FLAG_DONE;
  188. break;
  189. case UVC_BUF_STATE_QUEUED:
  190. case UVC_BUF_STATE_ACTIVE:
  191. case UVC_BUF_STATE_READY:
  192. v4l2_buf->flags |= V4L2_BUF_FLAG_QUEUED;
  193. break;
  194. case UVC_BUF_STATE_IDLE:
  195. default:
  196. break;
  197. }
  198. }
  199. int uvc_query_buffer(struct uvc_video_queue *queue,
  200. struct v4l2_buffer *v4l2_buf)
  201. {
  202. int ret = 0;
  203. mutex_lock(&queue->mutex);
  204. if (v4l2_buf->index >= queue->count) {
  205. ret = -EINVAL;
  206. goto done;
  207. }
  208. __uvc_query_buffer(&queue->buffer[v4l2_buf->index], v4l2_buf);
  209. done:
  210. mutex_unlock(&queue->mutex);
  211. return ret;
  212. }
  213. /*
  214. * Queue a video buffer. Attempting to queue a buffer that has already been
  215. * queued will return -EINVAL.
  216. */
  217. int uvc_queue_buffer(struct uvc_video_queue *queue,
  218. struct v4l2_buffer *v4l2_buf)
  219. {
  220. struct uvc_buffer *buf;
  221. unsigned long flags;
  222. int ret = 0;
  223. uvc_trace(UVC_TRACE_CAPTURE, "Queuing buffer %u.\n", v4l2_buf->index);
  224. if (v4l2_buf->type != queue->type ||
  225. v4l2_buf->memory != V4L2_MEMORY_MMAP) {
  226. uvc_trace(UVC_TRACE_CAPTURE, "[E] Invalid buffer type (%u) "
  227. "and/or memory (%u).\n", v4l2_buf->type,
  228. v4l2_buf->memory);
  229. return -EINVAL;
  230. }
  231. mutex_lock(&queue->mutex);
  232. if (v4l2_buf->index >= queue->count) {
  233. uvc_trace(UVC_TRACE_CAPTURE, "[E] Out of range index.\n");
  234. ret = -EINVAL;
  235. goto done;
  236. }
  237. buf = &queue->buffer[v4l2_buf->index];
  238. if (buf->state != UVC_BUF_STATE_IDLE) {
  239. uvc_trace(UVC_TRACE_CAPTURE, "[E] Invalid buffer state "
  240. "(%u).\n", buf->state);
  241. ret = -EINVAL;
  242. goto done;
  243. }
  244. if (v4l2_buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT &&
  245. v4l2_buf->bytesused > buf->buf.length) {
  246. uvc_trace(UVC_TRACE_CAPTURE, "[E] Bytes used out of bounds.\n");
  247. ret = -EINVAL;
  248. goto done;
  249. }
  250. spin_lock_irqsave(&queue->irqlock, flags);
  251. if (queue->flags & UVC_QUEUE_DISCONNECTED) {
  252. spin_unlock_irqrestore(&queue->irqlock, flags);
  253. ret = -ENODEV;
  254. goto done;
  255. }
  256. buf->state = UVC_BUF_STATE_QUEUED;
  257. if (v4l2_buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  258. buf->buf.bytesused = 0;
  259. else
  260. buf->buf.bytesused = v4l2_buf->bytesused;
  261. list_add_tail(&buf->stream, &queue->mainqueue);
  262. list_add_tail(&buf->queue, &queue->irqqueue);
  263. spin_unlock_irqrestore(&queue->irqlock, flags);
  264. done:
  265. mutex_unlock(&queue->mutex);
  266. return ret;
  267. }
  268. static int uvc_queue_waiton(struct uvc_buffer *buf, int nonblocking)
  269. {
  270. if (nonblocking) {
  271. return (buf->state != UVC_BUF_STATE_QUEUED &&
  272. buf->state != UVC_BUF_STATE_ACTIVE &&
  273. buf->state != UVC_BUF_STATE_READY)
  274. ? 0 : -EAGAIN;
  275. }
  276. return wait_event_interruptible(buf->wait,
  277. buf->state != UVC_BUF_STATE_QUEUED &&
  278. buf->state != UVC_BUF_STATE_ACTIVE &&
  279. buf->state != UVC_BUF_STATE_READY);
  280. }
  281. /*
  282. * Dequeue a video buffer. If nonblocking is false, block until a buffer is
  283. * available.
  284. */
  285. int uvc_dequeue_buffer(struct uvc_video_queue *queue,
  286. struct v4l2_buffer *v4l2_buf, int nonblocking)
  287. {
  288. struct uvc_buffer *buf;
  289. int ret = 0;
  290. if (v4l2_buf->type != queue->type ||
  291. v4l2_buf->memory != V4L2_MEMORY_MMAP) {
  292. uvc_trace(UVC_TRACE_CAPTURE, "[E] Invalid buffer type (%u) "
  293. "and/or memory (%u).\n", v4l2_buf->type,
  294. v4l2_buf->memory);
  295. return -EINVAL;
  296. }
  297. mutex_lock(&queue->mutex);
  298. if (list_empty(&queue->mainqueue)) {
  299. uvc_trace(UVC_TRACE_CAPTURE, "[E] Empty buffer queue.\n");
  300. ret = -EINVAL;
  301. goto done;
  302. }
  303. buf = list_first_entry(&queue->mainqueue, struct uvc_buffer, stream);
  304. if ((ret = uvc_queue_waiton(buf, nonblocking)) < 0)
  305. goto done;
  306. uvc_trace(UVC_TRACE_CAPTURE, "Dequeuing buffer %u (%u, %u bytes).\n",
  307. buf->buf.index, buf->state, buf->buf.bytesused);
  308. switch (buf->state) {
  309. case UVC_BUF_STATE_ERROR:
  310. uvc_trace(UVC_TRACE_CAPTURE, "[W] Corrupted data "
  311. "(transmission error).\n");
  312. ret = -EIO;
  313. case UVC_BUF_STATE_DONE:
  314. buf->state = UVC_BUF_STATE_IDLE;
  315. break;
  316. case UVC_BUF_STATE_IDLE:
  317. case UVC_BUF_STATE_QUEUED:
  318. case UVC_BUF_STATE_ACTIVE:
  319. case UVC_BUF_STATE_READY:
  320. default:
  321. uvc_trace(UVC_TRACE_CAPTURE, "[E] Invalid buffer state %u "
  322. "(driver bug?).\n", buf->state);
  323. ret = -EINVAL;
  324. goto done;
  325. }
  326. list_del(&buf->stream);
  327. __uvc_query_buffer(buf, v4l2_buf);
  328. done:
  329. mutex_unlock(&queue->mutex);
  330. return ret;
  331. }
  332. /*
  333. * VMA operations.
  334. */
  335. static void uvc_vm_open(struct vm_area_struct *vma)
  336. {
  337. struct uvc_buffer *buffer = vma->vm_private_data;
  338. buffer->vma_use_count++;
  339. }
  340. static void uvc_vm_close(struct vm_area_struct *vma)
  341. {
  342. struct uvc_buffer *buffer = vma->vm_private_data;
  343. buffer->vma_use_count--;
  344. }
  345. static const struct vm_operations_struct uvc_vm_ops = {
  346. .open = uvc_vm_open,
  347. .close = uvc_vm_close,
  348. };
  349. /*
  350. * Memory-map a video buffer.
  351. *
  352. * This function implements video buffers memory mapping and is intended to be
  353. * used by the device mmap handler.
  354. */
  355. int uvc_queue_mmap(struct uvc_video_queue *queue, struct vm_area_struct *vma)
  356. {
  357. struct uvc_buffer *uninitialized_var(buffer);
  358. struct page *page;
  359. unsigned long addr, start, size;
  360. unsigned int i;
  361. int ret = 0;
  362. start = vma->vm_start;
  363. size = vma->vm_end - vma->vm_start;
  364. mutex_lock(&queue->mutex);
  365. for (i = 0; i < queue->count; ++i) {
  366. buffer = &queue->buffer[i];
  367. if ((buffer->buf.m.offset >> PAGE_SHIFT) == vma->vm_pgoff)
  368. break;
  369. }
  370. if (i == queue->count || PAGE_ALIGN(size) != queue->buf_size) {
  371. ret = -EINVAL;
  372. goto done;
  373. }
  374. /*
  375. * VM_IO marks the area as being an mmaped region for I/O to a
  376. * device. It also prevents the region from being core dumped.
  377. */
  378. vma->vm_flags |= VM_IO;
  379. addr = (unsigned long)queue->mem + buffer->buf.m.offset;
  380. #ifdef CONFIG_MMU
  381. while (size > 0) {
  382. page = vmalloc_to_page((void *)addr);
  383. if ((ret = vm_insert_page(vma, start, page)) < 0)
  384. goto done;
  385. start += PAGE_SIZE;
  386. addr += PAGE_SIZE;
  387. size -= PAGE_SIZE;
  388. }
  389. #endif
  390. vma->vm_ops = &uvc_vm_ops;
  391. vma->vm_private_data = buffer;
  392. uvc_vm_open(vma);
  393. done:
  394. mutex_unlock(&queue->mutex);
  395. return ret;
  396. }
  397. /*
  398. * Poll the video queue.
  399. *
  400. * This function implements video queue polling and is intended to be used by
  401. * the device poll handler.
  402. */
  403. unsigned int uvc_queue_poll(struct uvc_video_queue *queue, struct file *file,
  404. poll_table *wait)
  405. {
  406. struct uvc_buffer *buf;
  407. unsigned int mask = 0;
  408. mutex_lock(&queue->mutex);
  409. if (list_empty(&queue->mainqueue)) {
  410. mask |= POLLERR;
  411. goto done;
  412. }
  413. buf = list_first_entry(&queue->mainqueue, struct uvc_buffer, stream);
  414. poll_wait(file, &buf->wait, wait);
  415. if (buf->state == UVC_BUF_STATE_DONE ||
  416. buf->state == UVC_BUF_STATE_ERROR) {
  417. if (queue->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
  418. mask |= POLLIN | POLLRDNORM;
  419. else
  420. mask |= POLLOUT | POLLWRNORM;
  421. }
  422. done:
  423. mutex_unlock(&queue->mutex);
  424. return mask;
  425. }
  426. #ifndef CONFIG_MMU
  427. /*
  428. * Get unmapped area.
  429. *
  430. * NO-MMU arch need this function to make mmap() work correctly.
  431. */
  432. unsigned long uvc_queue_get_unmapped_area(struct uvc_video_queue *queue,
  433. unsigned long pgoff)
  434. {
  435. struct uvc_buffer *buffer;
  436. unsigned int i;
  437. unsigned long ret;
  438. mutex_lock(&queue->mutex);
  439. for (i = 0; i < queue->count; ++i) {
  440. buffer = &queue->buffer[i];
  441. if ((buffer->buf.m.offset >> PAGE_SHIFT) == pgoff)
  442. break;
  443. }
  444. if (i == queue->count) {
  445. ret = -EINVAL;
  446. goto done;
  447. }
  448. ret = (unsigned long)queue->mem + buffer->buf.m.offset;
  449. done:
  450. mutex_unlock(&queue->mutex);
  451. return ret;
  452. }
  453. #endif
  454. /*
  455. * Enable or disable the video buffers queue.
  456. *
  457. * The queue must be enabled before starting video acquisition and must be
  458. * disabled after stopping it. This ensures that the video buffers queue
  459. * state can be properly initialized before buffers are accessed from the
  460. * interrupt handler.
  461. *
  462. * Enabling the video queue returns -EBUSY if the queue is already enabled.
  463. *
  464. * Disabling the video queue cancels the queue and removes all buffers from
  465. * the main queue.
  466. *
  467. * This function can't be called from interrupt context. Use
  468. * uvc_queue_cancel() instead.
  469. */
  470. int uvc_queue_enable(struct uvc_video_queue *queue, int enable)
  471. {
  472. unsigned int i;
  473. int ret = 0;
  474. mutex_lock(&queue->mutex);
  475. if (enable) {
  476. if (uvc_queue_streaming(queue)) {
  477. ret = -EBUSY;
  478. goto done;
  479. }
  480. queue->flags |= UVC_QUEUE_STREAMING;
  481. queue->buf_used = 0;
  482. } else {
  483. uvc_queue_cancel(queue, 0);
  484. INIT_LIST_HEAD(&queue->mainqueue);
  485. for (i = 0; i < queue->count; ++i) {
  486. queue->buffer[i].error = 0;
  487. queue->buffer[i].state = UVC_BUF_STATE_IDLE;
  488. }
  489. queue->flags &= ~UVC_QUEUE_STREAMING;
  490. }
  491. done:
  492. mutex_unlock(&queue->mutex);
  493. return ret;
  494. }
  495. /*
  496. * Cancel the video buffers queue.
  497. *
  498. * Cancelling the queue marks all buffers on the irq queue as erroneous,
  499. * wakes them up and removes them from the queue.
  500. *
  501. * If the disconnect parameter is set, further calls to uvc_queue_buffer will
  502. * fail with -ENODEV.
  503. *
  504. * This function acquires the irq spinlock and can be called from interrupt
  505. * context.
  506. */
  507. void uvc_queue_cancel(struct uvc_video_queue *queue, int disconnect)
  508. {
  509. struct uvc_buffer *buf;
  510. unsigned long flags;
  511. spin_lock_irqsave(&queue->irqlock, flags);
  512. while (!list_empty(&queue->irqqueue)) {
  513. buf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
  514. queue);
  515. list_del(&buf->queue);
  516. buf->state = UVC_BUF_STATE_ERROR;
  517. wake_up(&buf->wait);
  518. }
  519. /* This must be protected by the irqlock spinlock to avoid race
  520. * conditions between uvc_queue_buffer and the disconnection event that
  521. * could result in an interruptible wait in uvc_dequeue_buffer. Do not
  522. * blindly replace this logic by checking for the UVC_DEV_DISCONNECTED
  523. * state outside the queue code.
  524. */
  525. if (disconnect)
  526. queue->flags |= UVC_QUEUE_DISCONNECTED;
  527. spin_unlock_irqrestore(&queue->irqlock, flags);
  528. }
  529. struct uvc_buffer *uvc_queue_next_buffer(struct uvc_video_queue *queue,
  530. struct uvc_buffer *buf)
  531. {
  532. struct uvc_buffer *nextbuf;
  533. unsigned long flags;
  534. if ((queue->flags & UVC_QUEUE_DROP_CORRUPTED) && buf->error) {
  535. buf->error = 0;
  536. buf->state = UVC_BUF_STATE_QUEUED;
  537. buf->buf.bytesused = 0;
  538. return buf;
  539. }
  540. spin_lock_irqsave(&queue->irqlock, flags);
  541. list_del(&buf->queue);
  542. buf->error = 0;
  543. buf->state = UVC_BUF_STATE_DONE;
  544. if (!list_empty(&queue->irqqueue))
  545. nextbuf = list_first_entry(&queue->irqqueue, struct uvc_buffer,
  546. queue);
  547. else
  548. nextbuf = NULL;
  549. spin_unlock_irqrestore(&queue->irqlock, flags);
  550. wake_up(&buf->wait);
  551. return nextbuf;
  552. }