uvc_queue.c 15 KB

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