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