]>
Commit | Line | Data |
---|---|---|
cdda479f LP |
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. | |
cdda479f LP |
11 | */ |
12 | ||
d6925225 | 13 | #include <linux/atomic.h> |
cdda479f LP |
14 | #include <linux/kernel.h> |
15 | #include <linux/mm.h> | |
16 | #include <linux/list.h> | |
17 | #include <linux/module.h> | |
18 | #include <linux/usb.h> | |
19 | #include <linux/videodev2.h> | |
20 | #include <linux/vmalloc.h> | |
21 | #include <linux/wait.h> | |
d6925225 | 22 | |
16bf900f | 23 | #include <media/v4l2-common.h> |
d6925225 | 24 | #include <media/videobuf2-vmalloc.h> |
cdda479f LP |
25 | |
26 | #include "uvc.h" | |
27 | ||
28 | /* ------------------------------------------------------------------------ | |
29 | * Video buffers queue management. | |
30 | * | |
7ea95b11 | 31 | * Video queues is initialized by uvcg_queue_init(). The function performs |
cdda479f LP |
32 | * basic initialization of the uvc_video_queue struct and never fails. |
33 | * | |
d6925225 BS |
34 | * Video buffers are managed by videobuf2. The driver uses a mutex to protect |
35 | * the videobuf2 queue operations by serializing calls to videobuf2 and a | |
36 | * spinlock to protect the IRQ queue that holds the buffers to be processed by | |
37 | * the driver. | |
cdda479f LP |
38 | */ |
39 | ||
d6925225 BS |
40 | /* ----------------------------------------------------------------------------- |
41 | * videobuf2 queue operations | |
5d9955f8 | 42 | */ |
d6925225 BS |
43 | |
44 | static int uvc_queue_setup(struct vb2_queue *vq, const struct v4l2_format *fmt, | |
45 | unsigned int *nbuffers, unsigned int *nplanes, | |
46 | unsigned int sizes[], void *alloc_ctxs[]) | |
5d9955f8 | 47 | { |
d6925225 BS |
48 | struct uvc_video_queue *queue = vb2_get_drv_priv(vq); |
49 | struct uvc_video *video = container_of(queue, struct uvc_video, queue); | |
5d9955f8 | 50 | |
d6925225 BS |
51 | if (*nbuffers > UVC_MAX_VIDEO_BUFFERS) |
52 | *nbuffers = UVC_MAX_VIDEO_BUFFERS; | |
5d9955f8 | 53 | |
d6925225 BS |
54 | *nplanes = 1; |
55 | ||
56 | sizes[0] = video->imagesize; | |
5d9955f8 LP |
57 | |
58 | return 0; | |
59 | } | |
60 | ||
d6925225 | 61 | static int uvc_buffer_prepare(struct vb2_buffer *vb) |
cdda479f | 62 | { |
d6925225 BS |
63 | struct uvc_video_queue *queue = vb2_get_drv_priv(vb->vb2_queue); |
64 | struct uvc_buffer *buf = container_of(vb, struct uvc_buffer, buf); | |
cdda479f | 65 | |
d6925225 BS |
66 | if (vb->v4l2_buf.type == V4L2_BUF_TYPE_VIDEO_OUTPUT && |
67 | vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0)) { | |
68 | uvc_trace(UVC_TRACE_CAPTURE, "[E] Bytes used out of bounds.\n"); | |
69 | return -EINVAL; | |
70 | } | |
cdda479f | 71 | |
d6925225 BS |
72 | if (unlikely(queue->flags & UVC_QUEUE_DISCONNECTED)) |
73 | return -ENODEV; | |
cdda479f | 74 | |
d6925225 BS |
75 | buf->state = UVC_BUF_STATE_QUEUED; |
76 | buf->mem = vb2_plane_vaddr(vb, 0); | |
77 | buf->length = vb2_plane_size(vb, 0); | |
78 | if (vb->v4l2_buf.type == V4L2_BUF_TYPE_VIDEO_CAPTURE) | |
79 | buf->bytesused = 0; | |
80 | else | |
81 | buf->bytesused = vb2_get_plane_payload(vb, 0); | |
cdda479f | 82 | |
d6925225 BS |
83 | return 0; |
84 | } | |
cdda479f | 85 | |
d6925225 BS |
86 | static void uvc_buffer_queue(struct vb2_buffer *vb) |
87 | { | |
88 | struct uvc_video_queue *queue = vb2_get_drv_priv(vb->vb2_queue); | |
89 | struct uvc_buffer *buf = container_of(vb, struct uvc_buffer, buf); | |
90 | unsigned long flags; | |
cdda479f | 91 | |
d6925225 | 92 | spin_lock_irqsave(&queue->irqlock, flags); |
cdda479f | 93 | |
d6925225 BS |
94 | if (likely(!(queue->flags & UVC_QUEUE_DISCONNECTED))) { |
95 | list_add_tail(&buf->queue, &queue->irqqueue); | |
96 | } else { | |
97 | /* If the device is disconnected return the buffer to userspace | |
98 | * directly. The next QBUF call will fail with -ENODEV. | |
99 | */ | |
100 | buf->state = UVC_BUF_STATE_ERROR; | |
101 | vb2_buffer_done(&buf->buf, VB2_BUF_STATE_ERROR); | |
cdda479f LP |
102 | } |
103 | ||
d6925225 | 104 | spin_unlock_irqrestore(&queue->irqlock, flags); |
cdda479f LP |
105 | } |
106 | ||
d6925225 BS |
107 | static struct vb2_ops uvc_queue_qops = { |
108 | .queue_setup = uvc_queue_setup, | |
109 | .buf_prepare = uvc_buffer_prepare, | |
110 | .buf_queue = uvc_buffer_queue, | |
d8e96c4b HV |
111 | .wait_prepare = vb2_ops_wait_prepare, |
112 | .wait_finish = vb2_ops_wait_finish, | |
d6925225 | 113 | }; |
cdda479f | 114 | |
d8e96c4b HV |
115 | int uvcg_queue_init(struct uvc_video_queue *queue, enum v4l2_buf_type type, |
116 | struct mutex *lock) | |
cdda479f | 117 | { |
d6925225 | 118 | int ret; |
cdda479f | 119 | |
d6925225 | 120 | queue->queue.type = type; |
ee7ec7f6 | 121 | queue->queue.io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF; |
d6925225 BS |
122 | queue->queue.drv_priv = queue; |
123 | queue->queue.buf_struct_size = sizeof(struct uvc_buffer); | |
124 | queue->queue.ops = &uvc_queue_qops; | |
d8e96c4b | 125 | queue->queue.lock = lock; |
d6925225 | 126 | queue->queue.mem_ops = &vb2_vmalloc_memops; |
c9e44b53 LP |
127 | queue->queue.timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC |
128 | | V4L2_BUF_FLAG_TSTAMP_SRC_EOF; | |
d6925225 BS |
129 | ret = vb2_queue_init(&queue->queue); |
130 | if (ret) | |
131 | return ret; | |
132 | ||
d6925225 BS |
133 | spin_lock_init(&queue->irqlock); |
134 | INIT_LIST_HEAD(&queue->irqqueue); | |
135 | queue->flags = 0; | |
cdda479f | 136 | |
d6925225 BS |
137 | return 0; |
138 | } | |
cdda479f | 139 | |
d6925225 BS |
140 | /* |
141 | * Free the video buffers. | |
142 | */ | |
3a83c16e | 143 | void uvcg_free_buffers(struct uvc_video_queue *queue) |
d6925225 | 144 | { |
d6925225 | 145 | vb2_queue_release(&queue->queue); |
cdda479f LP |
146 | } |
147 | ||
148 | /* | |
d6925225 | 149 | * Allocate the video buffers. |
cdda479f | 150 | */ |
3a83c16e | 151 | int uvcg_alloc_buffers(struct uvc_video_queue *queue, |
7ea95b11 | 152 | struct v4l2_requestbuffers *rb) |
cdda479f | 153 | { |
d6925225 | 154 | int ret; |
cdda479f | 155 | |
d6925225 | 156 | ret = vb2_reqbufs(&queue->queue, rb); |
cdda479f | 157 | |
d6925225 BS |
158 | return ret ? ret : rb->count; |
159 | } | |
cdda479f | 160 | |
3a83c16e | 161 | int uvcg_query_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf) |
d6925225 | 162 | { |
d8e96c4b | 163 | return vb2_querybuf(&queue->queue, buf); |
d6925225 | 164 | } |
cdda479f | 165 | |
3a83c16e | 166 | int uvcg_queue_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf) |
d6925225 BS |
167 | { |
168 | unsigned long flags; | |
169 | int ret; | |
cdda479f | 170 | |
d6925225 | 171 | ret = vb2_qbuf(&queue->queue, buf); |
ebe864a6 | 172 | if (ret < 0) |
d8e96c4b | 173 | return ret; |
ebe864a6 | 174 | |
cdda479f | 175 | spin_lock_irqsave(&queue->irqlock, flags); |
cdda479f LP |
176 | ret = (queue->flags & UVC_QUEUE_PAUSED) != 0; |
177 | queue->flags &= ~UVC_QUEUE_PAUSED; | |
cdda479f | 178 | spin_unlock_irqrestore(&queue->irqlock, flags); |
d6925225 | 179 | return ret; |
cdda479f LP |
180 | } |
181 | ||
182 | /* | |
183 | * Dequeue a video buffer. If nonblocking is false, block until a buffer is | |
184 | * available. | |
185 | */ | |
3a83c16e AP |
186 | int uvcg_dequeue_buffer(struct uvc_video_queue *queue, struct v4l2_buffer *buf, |
187 | int nonblocking) | |
cdda479f | 188 | { |
d8e96c4b | 189 | return vb2_dqbuf(&queue->queue, buf, nonblocking); |
cdda479f LP |
190 | } |
191 | ||
192 | /* | |
193 | * Poll the video queue. | |
194 | * | |
195 | * This function implements video queue polling and is intended to be used by | |
196 | * the device poll handler. | |
197 | */ | |
3a83c16e AP |
198 | unsigned int uvcg_queue_poll(struct uvc_video_queue *queue, struct file *file, |
199 | poll_table *wait) | |
cdda479f | 200 | { |
d8e96c4b | 201 | return vb2_poll(&queue->queue, file, wait); |
cdda479f LP |
202 | } |
203 | ||
3a83c16e | 204 | int uvcg_queue_mmap(struct uvc_video_queue *queue, struct vm_area_struct *vma) |
cdda479f | 205 | { |
d8e96c4b | 206 | return vb2_mmap(&queue->queue, vma); |
cdda479f LP |
207 | } |
208 | ||
2f1d5706 BS |
209 | #ifndef CONFIG_MMU |
210 | /* | |
211 | * Get unmapped area. | |
212 | * | |
213 | * NO-MMU arch need this function to make mmap() work correctly. | |
214 | */ | |
3a83c16e AP |
215 | unsigned long uvcg_queue_get_unmapped_area(struct uvc_video_queue *queue, |
216 | unsigned long pgoff) | |
2f1d5706 | 217 | { |
d8e96c4b | 218 | return vb2_get_unmapped_area(&queue->queue, 0, 0, pgoff, 0); |
2f1d5706 BS |
219 | } |
220 | #endif | |
221 | ||
5d9955f8 LP |
222 | /* |
223 | * Cancel the video buffers queue. | |
224 | * | |
225 | * Cancelling the queue marks all buffers on the irq queue as erroneous, | |
226 | * wakes them up and removes them from the queue. | |
227 | * | |
228 | * If the disconnect parameter is set, further calls to uvc_queue_buffer will | |
229 | * fail with -ENODEV. | |
230 | * | |
231 | * This function acquires the irq spinlock and can be called from interrupt | |
232 | * context. | |
233 | */ | |
3a83c16e | 234 | void uvcg_queue_cancel(struct uvc_video_queue *queue, int disconnect) |
5d9955f8 LP |
235 | { |
236 | struct uvc_buffer *buf; | |
237 | unsigned long flags; | |
238 | ||
239 | spin_lock_irqsave(&queue->irqlock, flags); | |
240 | while (!list_empty(&queue->irqqueue)) { | |
241 | buf = list_first_entry(&queue->irqqueue, struct uvc_buffer, | |
242 | queue); | |
243 | list_del(&buf->queue); | |
244 | buf->state = UVC_BUF_STATE_ERROR; | |
d6925225 | 245 | vb2_buffer_done(&buf->buf, VB2_BUF_STATE_ERROR); |
5d9955f8 LP |
246 | } |
247 | /* This must be protected by the irqlock spinlock to avoid race | |
248 | * conditions between uvc_queue_buffer and the disconnection event that | |
249 | * could result in an interruptible wait in uvc_dequeue_buffer. Do not | |
250 | * blindly replace this logic by checking for the UVC_DEV_DISCONNECTED | |
251 | * state outside the queue code. | |
252 | */ | |
253 | if (disconnect) | |
254 | queue->flags |= UVC_QUEUE_DISCONNECTED; | |
255 | spin_unlock_irqrestore(&queue->irqlock, flags); | |
256 | } | |
257 | ||
cdda479f LP |
258 | /* |
259 | * Enable or disable the video buffers queue. | |
260 | * | |
261 | * The queue must be enabled before starting video acquisition and must be | |
262 | * disabled after stopping it. This ensures that the video buffers queue | |
263 | * state can be properly initialized before buffers are accessed from the | |
264 | * interrupt handler. | |
265 | * | |
266 | * Enabling the video queue initializes parameters (such as sequence number, | |
267 | * sync pattern, ...). If the queue is already enabled, return -EBUSY. | |
268 | * | |
269 | * Disabling the video queue cancels the queue and removes all buffers from | |
270 | * the main queue. | |
271 | * | |
272 | * This function can't be called from interrupt context. Use | |
7ea95b11 | 273 | * uvcg_queue_cancel() instead. |
cdda479f | 274 | */ |
3a83c16e | 275 | int uvcg_queue_enable(struct uvc_video_queue *queue, int enable) |
cdda479f | 276 | { |
d6925225 | 277 | unsigned long flags; |
cdda479f LP |
278 | int ret = 0; |
279 | ||
cdda479f | 280 | if (enable) { |
d6925225 BS |
281 | ret = vb2_streamon(&queue->queue, queue->queue.type); |
282 | if (ret < 0) | |
d8e96c4b | 283 | return ret; |
d6925225 | 284 | |
cdda479f | 285 | queue->sequence = 0; |
cdda479f LP |
286 | queue->buf_used = 0; |
287 | } else { | |
d6925225 BS |
288 | ret = vb2_streamoff(&queue->queue, queue->queue.type); |
289 | if (ret < 0) | |
d8e96c4b | 290 | return ret; |
cdda479f | 291 | |
d6925225 BS |
292 | spin_lock_irqsave(&queue->irqlock, flags); |
293 | INIT_LIST_HEAD(&queue->irqqueue); | |
cdda479f | 294 | |
d6925225 BS |
295 | /* |
296 | * FIXME: We need to clear the DISCONNECTED flag to ensure that | |
297 | * applications will be able to queue buffers for the next | |
298 | * streaming run. However, clearing it here doesn't guarantee | |
299 | * that the device will be reconnected in the meantime. | |
300 | */ | |
301 | queue->flags &= ~UVC_QUEUE_DISCONNECTED; | |
302 | spin_unlock_irqrestore(&queue->irqlock, flags); | |
cdda479f LP |
303 | } |
304 | ||
cdda479f LP |
305 | return ret; |
306 | } | |
307 | ||
95faf82b | 308 | /* called with &queue_irqlock held.. */ |
3a83c16e AP |
309 | struct uvc_buffer *uvcg_queue_next_buffer(struct uvc_video_queue *queue, |
310 | struct uvc_buffer *buf) | |
cdda479f LP |
311 | { |
312 | struct uvc_buffer *nextbuf; | |
cdda479f LP |
313 | |
314 | if ((queue->flags & UVC_QUEUE_DROP_INCOMPLETE) && | |
d6925225 | 315 | buf->length != buf->bytesused) { |
cdda479f | 316 | buf->state = UVC_BUF_STATE_QUEUED; |
d6925225 | 317 | vb2_set_plane_payload(&buf->buf, 0, 0); |
cdda479f LP |
318 | return buf; |
319 | } | |
320 | ||
cdda479f LP |
321 | list_del(&buf->queue); |
322 | if (!list_empty(&queue->irqqueue)) | |
323 | nextbuf = list_first_entry(&queue->irqqueue, struct uvc_buffer, | |
324 | queue); | |
325 | else | |
326 | nextbuf = NULL; | |
cdda479f | 327 | |
f17388cc | 328 | buf->buf.v4l2_buf.field = V4L2_FIELD_NONE; |
d6925225 | 329 | buf->buf.v4l2_buf.sequence = queue->sequence++; |
16bf900f | 330 | v4l2_get_timestamp(&buf->buf.v4l2_buf.timestamp); |
d6925225 BS |
331 | |
332 | vb2_set_plane_payload(&buf->buf, 0, buf->bytesused); | |
333 | vb2_buffer_done(&buf->buf, VB2_BUF_STATE_DONE); | |
cdda479f | 334 | |
cdda479f LP |
335 | return nextbuf; |
336 | } | |
337 | ||
3a83c16e | 338 | struct uvc_buffer *uvcg_queue_head(struct uvc_video_queue *queue) |
cdda479f LP |
339 | { |
340 | struct uvc_buffer *buf = NULL; | |
341 | ||
342 | if (!list_empty(&queue->irqqueue)) | |
343 | buf = list_first_entry(&queue->irqqueue, struct uvc_buffer, | |
344 | queue); | |
345 | else | |
346 | queue->flags |= UVC_QUEUE_PAUSED; | |
347 | ||
348 | return buf; | |
349 | } | |
350 |