uvc_queue.c 15 KB

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