Commit | Line | Data |
---|---|---|
c0efd232 LP |
1 | /* |
2 | * uvc_queue.c -- USB Video Class driver - Buffers management | |
3 | * | |
11fc5baf LP |
4 | * Copyright (C) 2005-2010 |
5 | * Laurent Pinchart (laurent.pinchart@ideasonboard.com) | |
c0efd232 LP |
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 | ||
6998b6fb | 14 | #include <linux/atomic.h> |
c0efd232 | 15 | #include <linux/kernel.h> |
27ac792c | 16 | #include <linux/mm.h> |
c0efd232 LP |
17 | #include <linux/list.h> |
18 | #include <linux/module.h> | |
19 | #include <linux/usb.h> | |
20 | #include <linux/videodev2.h> | |
21 | #include <linux/vmalloc.h> | |
22 | #include <linux/wait.h> | |
6998b6fb | 23 | #include <media/videobuf2-vmalloc.h> |
c0efd232 LP |
24 | |
25 | #include "uvcvideo.h" | |
26 | ||
27 | /* ------------------------------------------------------------------------ | |
28 | * Video buffers queue management. | |
29 | * | |
30 | * Video queues is initialized by uvc_queue_init(). The function performs | |
31 | * basic initialization of the uvc_video_queue struct and never fails. | |
32 | * | |
6998b6fb LP |
33 | * Video buffers are managed by videobuf2. The driver uses a mutex to protect |
34 | * the videobuf2 queue operations by serializing calls to videobuf2 and a | |
35 | * spinlock to protect the IRQ queue that holds the buffers to be processed by | |
36 | * the driver. | |
c0efd232 LP |
37 | */ |
38 | ||
6998b6fb LP |
39 | /* ----------------------------------------------------------------------------- |
40 | * videobuf2 queue operations | |
8e815e17 | 41 | */ |
6998b6fb LP |
42 | |
43 | static int uvc_queue_setup(struct vb2_queue *vq, const struct v4l2_format *fmt, | |
44 | unsigned int *nbuffers, unsigned int *nplanes, | |
45 | unsigned int sizes[], void *alloc_ctxs[]) | |
8e815e17 | 46 | { |
6998b6fb LP |
47 | struct uvc_video_queue *queue = vb2_get_drv_priv(vq); |
48 | struct uvc_streaming *stream = | |
49 | container_of(queue, struct uvc_streaming, queue); | |
8e815e17 | 50 | |
6998b6fb LP |
51 | *nplanes = 1; |
52 | ||
53 | sizes[0] = stream->ctrl.dwMaxVideoFrameSize; | |
8e815e17 LP |
54 | |
55 | return 0; | |
56 | } | |
57 | ||
6998b6fb | 58 | static int uvc_buffer_prepare(struct vb2_buffer *vb) |
8e815e17 | 59 | { |
6998b6fb LP |
60 | struct uvc_video_queue *queue = vb2_get_drv_priv(vb->vb2_queue); |
61 | struct uvc_buffer *buf = container_of(vb, struct uvc_buffer, buf); | |
8e815e17 | 62 | |
6998b6fb LP |
63 | if (vb->v4l2_buf.type == V4L2_BUF_TYPE_VIDEO_OUTPUT && |
64 | vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0)) { | |
65 | uvc_trace(UVC_TRACE_CAPTURE, "[E] Bytes used out of bounds.\n"); | |
66 | return -EINVAL; | |
67 | } | |
c0efd232 | 68 | |
6998b6fb LP |
69 | if (unlikely(queue->flags & UVC_QUEUE_DISCONNECTED)) |
70 | return -ENODEV; | |
c0efd232 | 71 | |
6998b6fb LP |
72 | buf->state = UVC_BUF_STATE_QUEUED; |
73 | buf->error = 0; | |
74 | buf->mem = vb2_plane_vaddr(vb, 0); | |
75 | buf->length = vb2_plane_size(vb, 0); | |
76 | if (vb->v4l2_buf.type == V4L2_BUF_TYPE_VIDEO_CAPTURE) | |
77 | buf->bytesused = 0; | |
78 | else | |
79 | buf->bytesused = vb2_get_plane_payload(vb, 0); | |
c0efd232 | 80 | |
6998b6fb LP |
81 | return 0; |
82 | } | |
c0efd232 | 83 | |
6998b6fb LP |
84 | static void uvc_buffer_queue(struct vb2_buffer *vb) |
85 | { | |
86 | struct uvc_video_queue *queue = vb2_get_drv_priv(vb->vb2_queue); | |
87 | struct uvc_buffer *buf = container_of(vb, struct uvc_buffer, buf); | |
88 | unsigned long flags; | |
c0efd232 | 89 | |
6998b6fb LP |
90 | spin_lock_irqsave(&queue->irqlock, flags); |
91 | if (likely(!(queue->flags & UVC_QUEUE_DISCONNECTED))) { | |
92 | list_add_tail(&buf->queue, &queue->irqqueue); | |
93 | } else { | |
94 | /* If the device is disconnected return the buffer to userspace | |
95 | * directly. The next QBUF call will fail with -ENODEV. | |
96 | */ | |
97 | buf->state = UVC_BUF_STATE_ERROR; | |
98 | vb2_buffer_done(&buf->buf, VB2_BUF_STATE_ERROR); | |
c0efd232 LP |
99 | } |
100 | ||
6998b6fb LP |
101 | spin_unlock_irqrestore(&queue->irqlock, flags); |
102 | } | |
c0efd232 | 103 | |
66847ef0 LP |
104 | static int uvc_buffer_finish(struct vb2_buffer *vb) |
105 | { | |
106 | struct uvc_video_queue *queue = vb2_get_drv_priv(vb->vb2_queue); | |
107 | struct uvc_streaming *stream = | |
108 | container_of(queue, struct uvc_streaming, queue); | |
109 | struct uvc_buffer *buf = container_of(vb, struct uvc_buffer, buf); | |
110 | ||
111 | uvc_video_clock_update(stream, &vb->v4l2_buf, buf); | |
112 | return 0; | |
113 | } | |
114 | ||
05c79d0d LP |
115 | static void uvc_wait_prepare(struct vb2_queue *vq) |
116 | { | |
117 | struct uvc_video_queue *queue = vb2_get_drv_priv(vq); | |
118 | ||
119 | mutex_unlock(&queue->mutex); | |
120 | } | |
121 | ||
122 | static void uvc_wait_finish(struct vb2_queue *vq) | |
123 | { | |
124 | struct uvc_video_queue *queue = vb2_get_drv_priv(vq); | |
125 | ||
126 | mutex_lock(&queue->mutex); | |
127 | } | |
128 | ||
6998b6fb LP |
129 | static struct vb2_ops uvc_queue_qops = { |
130 | .queue_setup = uvc_queue_setup, | |
131 | .buf_prepare = uvc_buffer_prepare, | |
132 | .buf_queue = uvc_buffer_queue, | |
66847ef0 | 133 | .buf_finish = uvc_buffer_finish, |
05c79d0d LP |
134 | .wait_prepare = uvc_wait_prepare, |
135 | .wait_finish = uvc_wait_finish, | |
6998b6fb | 136 | }; |
c0efd232 | 137 | |
5712661d | 138 | int uvc_queue_init(struct uvc_video_queue *queue, enum v4l2_buf_type type, |
6998b6fb LP |
139 | int drop_corrupted) |
140 | { | |
5712661d EG |
141 | int ret; |
142 | ||
6998b6fb | 143 | queue->queue.type = type; |
ccc4c539 | 144 | queue->queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF; |
6998b6fb LP |
145 | queue->queue.drv_priv = queue; |
146 | queue->queue.buf_struct_size = sizeof(struct uvc_buffer); | |
147 | queue->queue.ops = &uvc_queue_qops; | |
148 | queue->queue.mem_ops = &vb2_vmalloc_memops; | |
6aa69f99 | 149 | queue->queue.timestamp_type = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC; |
5712661d EG |
150 | ret = vb2_queue_init(&queue->queue); |
151 | if (ret) | |
152 | return ret; | |
c0efd232 | 153 | |
6998b6fb LP |
154 | mutex_init(&queue->mutex); |
155 | spin_lock_init(&queue->irqlock); | |
156 | INIT_LIST_HEAD(&queue->irqqueue); | |
157 | queue->flags = drop_corrupted ? UVC_QUEUE_DROP_CORRUPTED : 0; | |
5712661d EG |
158 | |
159 | return 0; | |
c0efd232 LP |
160 | } |
161 | ||
6998b6fb LP |
162 | /* ----------------------------------------------------------------------------- |
163 | * V4L2 queue operations | |
23ff6043 | 164 | */ |
6998b6fb LP |
165 | |
166 | int uvc_alloc_buffers(struct uvc_video_queue *queue, | |
167 | struct v4l2_requestbuffers *rb) | |
23ff6043 | 168 | { |
6998b6fb | 169 | int ret; |
23ff6043 LP |
170 | |
171 | mutex_lock(&queue->mutex); | |
6998b6fb | 172 | ret = vb2_reqbufs(&queue->queue, rb); |
23ff6043 LP |
173 | mutex_unlock(&queue->mutex); |
174 | ||
6998b6fb | 175 | return ret ? ret : rb->count; |
23ff6043 LP |
176 | } |
177 | ||
6998b6fb | 178 | void uvc_free_buffers(struct uvc_video_queue *queue) |
c0efd232 | 179 | { |
6998b6fb LP |
180 | mutex_lock(&queue->mutex); |
181 | vb2_queue_release(&queue->queue); | |
182 | mutex_unlock(&queue->mutex); | |
c0efd232 LP |
183 | } |
184 | ||
6998b6fb | 185 | int uvc_query_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf) |
c0efd232 | 186 | { |
6998b6fb | 187 | int ret; |
c0efd232 LP |
188 | |
189 | mutex_lock(&queue->mutex); | |
6998b6fb | 190 | ret = vb2_querybuf(&queue->queue, buf); |
2c2d264b | 191 | mutex_unlock(&queue->mutex); |
6998b6fb | 192 | |
2c2d264b | 193 | return ret; |
c0efd232 LP |
194 | } |
195 | ||
6998b6fb | 196 | int uvc_queue_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf) |
c0efd232 | 197 | { |
6998b6fb | 198 | int ret; |
c0efd232 LP |
199 | |
200 | mutex_lock(&queue->mutex); | |
6998b6fb | 201 | ret = vb2_qbuf(&queue->queue, buf); |
c0efd232 | 202 | mutex_unlock(&queue->mutex); |
c0efd232 | 203 | |
6998b6fb | 204 | return ret; |
c0efd232 LP |
205 | } |
206 | ||
6998b6fb LP |
207 | int uvc_dequeue_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf, |
208 | int nonblocking) | |
c0efd232 | 209 | { |
6998b6fb | 210 | int ret; |
c0efd232 LP |
211 | |
212 | mutex_lock(&queue->mutex); | |
6998b6fb | 213 | ret = vb2_dqbuf(&queue->queue, buf, nonblocking); |
c0efd232 | 214 | mutex_unlock(&queue->mutex); |
c0efd232 | 215 | |
6998b6fb | 216 | return ret; |
4aa27597 LP |
217 | } |
218 | ||
4aa27597 LP |
219 | int uvc_queue_mmap(struct uvc_video_queue *queue, struct vm_area_struct *vma) |
220 | { | |
6998b6fb | 221 | int ret; |
4aa27597 LP |
222 | |
223 | mutex_lock(&queue->mutex); | |
6998b6fb LP |
224 | ret = vb2_mmap(&queue->queue, vma); |
225 | mutex_unlock(&queue->mutex); | |
4aa27597 | 226 | |
6998b6fb LP |
227 | return ret; |
228 | } | |
4aa27597 | 229 | |
4742c82e LP |
230 | #ifndef CONFIG_MMU |
231 | unsigned long uvc_queue_get_unmapped_area(struct uvc_video_queue *queue, | |
232 | unsigned long pgoff) | |
233 | { | |
234 | unsigned long ret; | |
235 | ||
236 | mutex_lock(&queue->mutex); | |
237 | ret = vb2_get_unmapped_area(&queue->queue, 0, 0, pgoff, 0); | |
238 | mutex_unlock(&queue->mutex); | |
239 | return ret; | |
240 | } | |
241 | #endif | |
242 | ||
6998b6fb LP |
243 | unsigned int uvc_queue_poll(struct uvc_video_queue *queue, struct file *file, |
244 | poll_table *wait) | |
245 | { | |
246 | unsigned int ret; | |
4aa27597 | 247 | |
6998b6fb LP |
248 | mutex_lock(&queue->mutex); |
249 | ret = vb2_poll(&queue->queue, file, wait); | |
4aa27597 | 250 | mutex_unlock(&queue->mutex); |
6998b6fb | 251 | |
4aa27597 LP |
252 | return ret; |
253 | } | |
254 | ||
6998b6fb | 255 | /* ----------------------------------------------------------------------------- |
c0efd232 | 256 | * |
c0efd232 | 257 | */ |
6998b6fb LP |
258 | |
259 | /* | |
260 | * Check if buffers have been allocated. | |
261 | */ | |
262 | int uvc_queue_allocated(struct uvc_video_queue *queue) | |
c0efd232 | 263 | { |
6998b6fb | 264 | int allocated; |
c0efd232 LP |
265 | |
266 | mutex_lock(&queue->mutex); | |
6998b6fb | 267 | allocated = vb2_is_busy(&queue->queue); |
c0efd232 | 268 | mutex_unlock(&queue->mutex); |
6998b6fb LP |
269 | |
270 | return allocated; | |
c0efd232 LP |
271 | } |
272 | ||
273 | /* | |
274 | * Enable or disable the video buffers queue. | |
275 | * | |
276 | * The queue must be enabled before starting video acquisition and must be | |
277 | * disabled after stopping it. This ensures that the video buffers queue | |
278 | * state can be properly initialized before buffers are accessed from the | |
279 | * interrupt handler. | |
280 | * | |
650b95fe | 281 | * Enabling the video queue returns -EBUSY if the queue is already enabled. |
c0efd232 LP |
282 | * |
283 | * Disabling the video queue cancels the queue and removes all buffers from | |
284 | * the main queue. | |
285 | * | |
286 | * This function can't be called from interrupt context. Use | |
287 | * uvc_queue_cancel() instead. | |
288 | */ | |
289 | int uvc_queue_enable(struct uvc_video_queue *queue, int enable) | |
290 | { | |
6998b6fb LP |
291 | unsigned long flags; |
292 | int ret; | |
c0efd232 LP |
293 | |
294 | mutex_lock(&queue->mutex); | |
295 | if (enable) { | |
6998b6fb LP |
296 | ret = vb2_streamon(&queue->queue, queue->queue.type); |
297 | if (ret < 0) | |
c0efd232 | 298 | goto done; |
6998b6fb | 299 | |
ff924203 | 300 | queue->buf_used = 0; |
c0efd232 | 301 | } else { |
6998b6fb LP |
302 | ret = vb2_streamoff(&queue->queue, queue->queue.type); |
303 | if (ret < 0) | |
304 | goto done; | |
c0efd232 | 305 | |
6998b6fb LP |
306 | spin_lock_irqsave(&queue->irqlock, flags); |
307 | INIT_LIST_HEAD(&queue->irqqueue); | |
308 | spin_unlock_irqrestore(&queue->irqlock, flags); | |
c0efd232 LP |
309 | } |
310 | ||
311 | done: | |
312 | mutex_unlock(&queue->mutex); | |
313 | return ret; | |
314 | } | |
315 | ||
316 | /* | |
317 | * Cancel the video buffers queue. | |
318 | * | |
319 | * Cancelling the queue marks all buffers on the irq queue as erroneous, | |
2c2d264b | 320 | * wakes them up and removes them from the queue. |
c0efd232 LP |
321 | * |
322 | * If the disconnect parameter is set, further calls to uvc_queue_buffer will | |
323 | * fail with -ENODEV. | |
324 | * | |
325 | * This function acquires the irq spinlock and can be called from interrupt | |
326 | * context. | |
327 | */ | |
328 | void uvc_queue_cancel(struct uvc_video_queue *queue, int disconnect) | |
329 | { | |
330 | struct uvc_buffer *buf; | |
331 | unsigned long flags; | |
332 | ||
333 | spin_lock_irqsave(&queue->irqlock, flags); | |
334 | while (!list_empty(&queue->irqqueue)) { | |
335 | buf = list_first_entry(&queue->irqqueue, struct uvc_buffer, | |
336 | queue); | |
337 | list_del(&buf->queue); | |
338 | buf->state = UVC_BUF_STATE_ERROR; | |
6998b6fb | 339 | vb2_buffer_done(&buf->buf, VB2_BUF_STATE_ERROR); |
c0efd232 LP |
340 | } |
341 | /* This must be protected by the irqlock spinlock to avoid race | |
6998b6fb | 342 | * conditions between uvc_buffer_queue and the disconnection event that |
c0efd232 | 343 | * could result in an interruptible wait in uvc_dequeue_buffer. Do not |
6998b6fb | 344 | * blindly replace this logic by checking for the UVC_QUEUE_DISCONNECTED |
c0efd232 LP |
345 | * state outside the queue code. |
346 | */ | |
347 | if (disconnect) | |
348 | queue->flags |= UVC_QUEUE_DISCONNECTED; | |
349 | spin_unlock_irqrestore(&queue->irqlock, flags); | |
350 | } | |
351 | ||
352 | struct uvc_buffer *uvc_queue_next_buffer(struct uvc_video_queue *queue, | |
353 | struct uvc_buffer *buf) | |
354 | { | |
355 | struct uvc_buffer *nextbuf; | |
356 | unsigned long flags; | |
357 | ||
9bde9f26 LP |
358 | if ((queue->flags & UVC_QUEUE_DROP_CORRUPTED) && buf->error) { |
359 | buf->error = 0; | |
c0efd232 | 360 | buf->state = UVC_BUF_STATE_QUEUED; |
8a3f0ede | 361 | buf->bytesused = 0; |
6998b6fb | 362 | vb2_set_plane_payload(&buf->buf, 0, 0); |
c0efd232 LP |
363 | return buf; |
364 | } | |
365 | ||
366 | spin_lock_irqsave(&queue->irqlock, flags); | |
367 | list_del(&buf->queue); | |
368 | if (!list_empty(&queue->irqqueue)) | |
369 | nextbuf = list_first_entry(&queue->irqqueue, struct uvc_buffer, | |
370 | queue); | |
371 | else | |
372 | nextbuf = NULL; | |
373 | spin_unlock_irqrestore(&queue->irqlock, flags); | |
374 | ||
6998b6fb LP |
375 | buf->state = buf->error ? VB2_BUF_STATE_ERROR : UVC_BUF_STATE_DONE; |
376 | vb2_set_plane_payload(&buf->buf, 0, buf->bytesused); | |
377 | vb2_buffer_done(&buf->buf, VB2_BUF_STATE_DONE); | |
378 | ||
c0efd232 LP |
379 | return nextbuf; |
380 | } |