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