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