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