]>
Commit | Line | Data |
---|---|---|
7f98639d PO |
1 | /* |
2 | * Memory-to-memory device framework for Video for Linux 2 and videobuf. | |
3 | * | |
4 | * Helper functions for devices that use videobuf buffers for both their | |
5 | * source and destination. | |
6 | * | |
7 | * Copyright (c) 2009-2010 Samsung Electronics Co., Ltd. | |
95072084 | 8 | * Pawel Osciak, <pawel@osciak.com> |
7f98639d PO |
9 | * Marek Szyprowski, <m.szyprowski@samsung.com> |
10 | * | |
11 | * This program is free software; you can redistribute it and/or modify | |
12 | * it under the terms of the GNU General Public License as published by the | |
13 | * Free Software Foundation; either version 2 of the License, or (at your | |
14 | * option) any later version. | |
15 | */ | |
16 | #include <linux/module.h> | |
17 | #include <linux/sched.h> | |
18 | #include <linux/slab.h> | |
19 | ||
908a0d7c | 20 | #include <media/videobuf2-core.h> |
7f98639d | 21 | #include <media/v4l2-mem2mem.h> |
08eb8510 HV |
22 | #include <media/v4l2-dev.h> |
23 | #include <media/v4l2-fh.h> | |
24 | #include <media/v4l2-event.h> | |
7f98639d PO |
25 | |
26 | MODULE_DESCRIPTION("Mem to mem device framework for videobuf"); | |
95072084 | 27 | MODULE_AUTHOR("Pawel Osciak, <pawel@osciak.com>"); |
7f98639d PO |
28 | MODULE_LICENSE("GPL"); |
29 | ||
30 | static bool debug; | |
31 | module_param(debug, bool, 0644); | |
32 | ||
33 | #define dprintk(fmt, arg...) \ | |
34 | do { \ | |
35 | if (debug) \ | |
36 | printk(KERN_DEBUG "%s: " fmt, __func__, ## arg);\ | |
37 | } while (0) | |
38 | ||
39 | ||
40 | /* Instance is already queued on the job_queue */ | |
41 | #define TRANS_QUEUED (1 << 0) | |
42 | /* Instance is currently running in hardware */ | |
43 | #define TRANS_RUNNING (1 << 1) | |
44 | ||
45 | ||
46 | /* Offset base for buffers on the destination queue - used to distinguish | |
47 | * between source and destination buffers when mmapping - they receive the same | |
48 | * offsets but for different queues */ | |
49 | #define DST_QUEUE_OFF_BASE (1 << 30) | |
50 | ||
51 | ||
52 | /** | |
53 | * struct v4l2_m2m_dev - per-device context | |
54 | * @curr_ctx: currently running instance | |
55 | * @job_queue: instances queued to run | |
56 | * @job_spinlock: protects job_queue | |
57 | * @m2m_ops: driver callbacks | |
58 | */ | |
59 | struct v4l2_m2m_dev { | |
60 | struct v4l2_m2m_ctx *curr_ctx; | |
61 | ||
62 | struct list_head job_queue; | |
63 | spinlock_t job_spinlock; | |
64 | ||
b1252eb8 | 65 | const struct v4l2_m2m_ops *m2m_ops; |
7f98639d PO |
66 | }; |
67 | ||
68 | static struct v4l2_m2m_queue_ctx *get_queue_ctx(struct v4l2_m2m_ctx *m2m_ctx, | |
69 | enum v4l2_buf_type type) | |
70 | { | |
908a0d7c | 71 | if (V4L2_TYPE_IS_OUTPUT(type)) |
7f98639d | 72 | return &m2m_ctx->out_q_ctx; |
908a0d7c MS |
73 | else |
74 | return &m2m_ctx->cap_q_ctx; | |
7f98639d PO |
75 | } |
76 | ||
77 | /** | |
908a0d7c | 78 | * v4l2_m2m_get_vq() - return vb2_queue for the given type |
7f98639d | 79 | */ |
908a0d7c | 80 | struct vb2_queue *v4l2_m2m_get_vq(struct v4l2_m2m_ctx *m2m_ctx, |
7f98639d PO |
81 | enum v4l2_buf_type type) |
82 | { | |
83 | struct v4l2_m2m_queue_ctx *q_ctx; | |
84 | ||
85 | q_ctx = get_queue_ctx(m2m_ctx, type); | |
86 | if (!q_ctx) | |
87 | return NULL; | |
88 | ||
89 | return &q_ctx->q; | |
90 | } | |
91 | EXPORT_SYMBOL(v4l2_m2m_get_vq); | |
92 | ||
93 | /** | |
94 | * v4l2_m2m_next_buf() - return next buffer from the list of ready buffers | |
95 | */ | |
908a0d7c | 96 | void *v4l2_m2m_next_buf(struct v4l2_m2m_queue_ctx *q_ctx) |
7f98639d | 97 | { |
908a0d7c | 98 | struct v4l2_m2m_buffer *b = NULL; |
7f98639d PO |
99 | unsigned long flags; |
100 | ||
908a0d7c | 101 | spin_lock_irqsave(&q_ctx->rdy_spinlock, flags); |
7f98639d | 102 | |
a6bd62be AP |
103 | if (list_empty(&q_ctx->rdy_queue)) { |
104 | spin_unlock_irqrestore(&q_ctx->rdy_spinlock, flags); | |
105 | return NULL; | |
106 | } | |
7f98639d | 107 | |
c392e9e1 | 108 | b = list_first_entry(&q_ctx->rdy_queue, struct v4l2_m2m_buffer, list); |
908a0d7c MS |
109 | spin_unlock_irqrestore(&q_ctx->rdy_spinlock, flags); |
110 | return &b->vb; | |
7f98639d PO |
111 | } |
112 | EXPORT_SYMBOL_GPL(v4l2_m2m_next_buf); | |
113 | ||
114 | /** | |
115 | * v4l2_m2m_buf_remove() - take off a buffer from the list of ready buffers and | |
116 | * return it | |
117 | */ | |
908a0d7c | 118 | void *v4l2_m2m_buf_remove(struct v4l2_m2m_queue_ctx *q_ctx) |
7f98639d | 119 | { |
908a0d7c | 120 | struct v4l2_m2m_buffer *b = NULL; |
7f98639d PO |
121 | unsigned long flags; |
122 | ||
908a0d7c | 123 | spin_lock_irqsave(&q_ctx->rdy_spinlock, flags); |
a6bd62be AP |
124 | if (list_empty(&q_ctx->rdy_queue)) { |
125 | spin_unlock_irqrestore(&q_ctx->rdy_spinlock, flags); | |
126 | return NULL; | |
7f98639d | 127 | } |
c392e9e1 | 128 | b = list_first_entry(&q_ctx->rdy_queue, struct v4l2_m2m_buffer, list); |
a6bd62be AP |
129 | list_del(&b->list); |
130 | q_ctx->num_rdy--; | |
908a0d7c | 131 | spin_unlock_irqrestore(&q_ctx->rdy_spinlock, flags); |
7f98639d | 132 | |
908a0d7c | 133 | return &b->vb; |
7f98639d PO |
134 | } |
135 | EXPORT_SYMBOL_GPL(v4l2_m2m_buf_remove); | |
136 | ||
137 | /* | |
138 | * Scheduling handlers | |
139 | */ | |
140 | ||
141 | /** | |
142 | * v4l2_m2m_get_curr_priv() - return driver private data for the currently | |
143 | * running instance or NULL if no instance is running | |
144 | */ | |
145 | void *v4l2_m2m_get_curr_priv(struct v4l2_m2m_dev *m2m_dev) | |
146 | { | |
147 | unsigned long flags; | |
148 | void *ret = NULL; | |
149 | ||
150 | spin_lock_irqsave(&m2m_dev->job_spinlock, flags); | |
151 | if (m2m_dev->curr_ctx) | |
152 | ret = m2m_dev->curr_ctx->priv; | |
153 | spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags); | |
154 | ||
155 | return ret; | |
156 | } | |
157 | EXPORT_SYMBOL(v4l2_m2m_get_curr_priv); | |
158 | ||
159 | /** | |
160 | * v4l2_m2m_try_run() - select next job to perform and run it if possible | |
161 | * | |
162 | * Get next transaction (if present) from the waiting jobs list and run it. | |
163 | */ | |
164 | static void v4l2_m2m_try_run(struct v4l2_m2m_dev *m2m_dev) | |
165 | { | |
166 | unsigned long flags; | |
167 | ||
168 | spin_lock_irqsave(&m2m_dev->job_spinlock, flags); | |
169 | if (NULL != m2m_dev->curr_ctx) { | |
170 | spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags); | |
171 | dprintk("Another instance is running, won't run now\n"); | |
172 | return; | |
173 | } | |
174 | ||
175 | if (list_empty(&m2m_dev->job_queue)) { | |
176 | spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags); | |
177 | dprintk("No job pending\n"); | |
178 | return; | |
179 | } | |
180 | ||
c392e9e1 | 181 | m2m_dev->curr_ctx = list_first_entry(&m2m_dev->job_queue, |
7f98639d PO |
182 | struct v4l2_m2m_ctx, queue); |
183 | m2m_dev->curr_ctx->job_flags |= TRANS_RUNNING; | |
184 | spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags); | |
185 | ||
186 | m2m_dev->m2m_ops->device_run(m2m_dev->curr_ctx->priv); | |
187 | } | |
188 | ||
189 | /** | |
190 | * v4l2_m2m_try_schedule() - check whether an instance is ready to be added to | |
191 | * the pending job queue and add it if so. | |
192 | * @m2m_ctx: m2m context assigned to the instance to be checked | |
193 | * | |
194 | * There are three basic requirements an instance has to meet to be able to run: | |
195 | * 1) at least one source buffer has to be queued, | |
196 | * 2) at least one destination buffer has to be queued, | |
197 | * 3) streaming has to be on. | |
198 | * | |
199 | * There may also be additional, custom requirements. In such case the driver | |
200 | * should supply a custom callback (job_ready in v4l2_m2m_ops) that should | |
201 | * return 1 if the instance is ready. | |
202 | * An example of the above could be an instance that requires more than one | |
203 | * src/dst buffer per transaction. | |
204 | */ | |
205 | static void v4l2_m2m_try_schedule(struct v4l2_m2m_ctx *m2m_ctx) | |
206 | { | |
207 | struct v4l2_m2m_dev *m2m_dev; | |
208 | unsigned long flags_job, flags; | |
209 | ||
210 | m2m_dev = m2m_ctx->m2m_dev; | |
211 | dprintk("Trying to schedule a job for m2m_ctx: %p\n", m2m_ctx); | |
212 | ||
213 | if (!m2m_ctx->out_q_ctx.q.streaming | |
214 | || !m2m_ctx->cap_q_ctx.q.streaming) { | |
215 | dprintk("Streaming needs to be on for both queues\n"); | |
216 | return; | |
217 | } | |
218 | ||
219 | spin_lock_irqsave(&m2m_dev->job_spinlock, flags_job); | |
220 | if (m2m_ctx->job_flags & TRANS_QUEUED) { | |
221 | spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags_job); | |
222 | dprintk("On job queue already\n"); | |
223 | return; | |
224 | } | |
225 | ||
908a0d7c | 226 | spin_lock_irqsave(&m2m_ctx->out_q_ctx.rdy_spinlock, flags); |
7f98639d | 227 | if (list_empty(&m2m_ctx->out_q_ctx.rdy_queue)) { |
908a0d7c | 228 | spin_unlock_irqrestore(&m2m_ctx->out_q_ctx.rdy_spinlock, flags); |
7f98639d PO |
229 | spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags_job); |
230 | dprintk("No input buffers available\n"); | |
231 | return; | |
232 | } | |
f1a0569b | 233 | spin_lock_irqsave(&m2m_ctx->cap_q_ctx.rdy_spinlock, flags); |
7f98639d | 234 | if (list_empty(&m2m_ctx->cap_q_ctx.rdy_queue)) { |
f1a0569b | 235 | spin_unlock_irqrestore(&m2m_ctx->cap_q_ctx.rdy_spinlock, flags); |
908a0d7c | 236 | spin_unlock_irqrestore(&m2m_ctx->out_q_ctx.rdy_spinlock, flags); |
7f98639d PO |
237 | spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags_job); |
238 | dprintk("No output buffers available\n"); | |
239 | return; | |
240 | } | |
f1a0569b | 241 | spin_unlock_irqrestore(&m2m_ctx->cap_q_ctx.rdy_spinlock, flags); |
908a0d7c | 242 | spin_unlock_irqrestore(&m2m_ctx->out_q_ctx.rdy_spinlock, flags); |
7f98639d PO |
243 | |
244 | if (m2m_dev->m2m_ops->job_ready | |
245 | && (!m2m_dev->m2m_ops->job_ready(m2m_ctx->priv))) { | |
246 | spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags_job); | |
247 | dprintk("Driver not ready\n"); | |
248 | return; | |
249 | } | |
250 | ||
251 | list_add_tail(&m2m_ctx->queue, &m2m_dev->job_queue); | |
252 | m2m_ctx->job_flags |= TRANS_QUEUED; | |
253 | ||
254 | spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags_job); | |
255 | ||
256 | v4l2_m2m_try_run(m2m_dev); | |
257 | } | |
258 | ||
259 | /** | |
260 | * v4l2_m2m_job_finish() - inform the framework that a job has been finished | |
261 | * and have it clean up | |
262 | * | |
263 | * Called by a driver to yield back the device after it has finished with it. | |
264 | * Should be called as soon as possible after reaching a state which allows | |
265 | * other instances to take control of the device. | |
266 | * | |
267 | * This function has to be called only after device_run() callback has been | |
268 | * called on the driver. To prevent recursion, it should not be called directly | |
269 | * from the device_run() callback though. | |
270 | */ | |
271 | void v4l2_m2m_job_finish(struct v4l2_m2m_dev *m2m_dev, | |
272 | struct v4l2_m2m_ctx *m2m_ctx) | |
273 | { | |
274 | unsigned long flags; | |
275 | ||
276 | spin_lock_irqsave(&m2m_dev->job_spinlock, flags); | |
277 | if (!m2m_dev->curr_ctx || m2m_dev->curr_ctx != m2m_ctx) { | |
278 | spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags); | |
279 | dprintk("Called by an instance not currently running\n"); | |
280 | return; | |
281 | } | |
282 | ||
283 | list_del(&m2m_dev->curr_ctx->queue); | |
284 | m2m_dev->curr_ctx->job_flags &= ~(TRANS_QUEUED | TRANS_RUNNING); | |
908a0d7c | 285 | wake_up(&m2m_dev->curr_ctx->finished); |
7f98639d PO |
286 | m2m_dev->curr_ctx = NULL; |
287 | ||
288 | spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags); | |
289 | ||
290 | /* This instance might have more buffers ready, but since we do not | |
291 | * allow more than one job on the job_queue per instance, each has | |
292 | * to be scheduled separately after the previous one finishes. */ | |
293 | v4l2_m2m_try_schedule(m2m_ctx); | |
294 | v4l2_m2m_try_run(m2m_dev); | |
295 | } | |
296 | EXPORT_SYMBOL(v4l2_m2m_job_finish); | |
297 | ||
298 | /** | |
299 | * v4l2_m2m_reqbufs() - multi-queue-aware REQBUFS multiplexer | |
300 | */ | |
301 | int v4l2_m2m_reqbufs(struct file *file, struct v4l2_m2m_ctx *m2m_ctx, | |
302 | struct v4l2_requestbuffers *reqbufs) | |
303 | { | |
908a0d7c | 304 | struct vb2_queue *vq; |
7f98639d PO |
305 | |
306 | vq = v4l2_m2m_get_vq(m2m_ctx, reqbufs->type); | |
908a0d7c | 307 | return vb2_reqbufs(vq, reqbufs); |
7f98639d PO |
308 | } |
309 | EXPORT_SYMBOL_GPL(v4l2_m2m_reqbufs); | |
310 | ||
311 | /** | |
312 | * v4l2_m2m_querybuf() - multi-queue-aware QUERYBUF multiplexer | |
313 | * | |
314 | * See v4l2_m2m_mmap() documentation for details. | |
315 | */ | |
316 | int v4l2_m2m_querybuf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx, | |
317 | struct v4l2_buffer *buf) | |
318 | { | |
908a0d7c MS |
319 | struct vb2_queue *vq; |
320 | int ret = 0; | |
321 | unsigned int i; | |
7f98639d PO |
322 | |
323 | vq = v4l2_m2m_get_vq(m2m_ctx, buf->type); | |
908a0d7c MS |
324 | ret = vb2_querybuf(vq, buf); |
325 | ||
326 | /* Adjust MMAP memory offsets for the CAPTURE queue */ | |
327 | if (buf->memory == V4L2_MEMORY_MMAP && !V4L2_TYPE_IS_OUTPUT(vq->type)) { | |
328 | if (V4L2_TYPE_IS_MULTIPLANAR(vq->type)) { | |
329 | for (i = 0; i < buf->length; ++i) | |
330 | buf->m.planes[i].m.mem_offset | |
331 | += DST_QUEUE_OFF_BASE; | |
332 | } else { | |
333 | buf->m.offset += DST_QUEUE_OFF_BASE; | |
334 | } | |
7f98639d PO |
335 | } |
336 | ||
337 | return ret; | |
338 | } | |
339 | EXPORT_SYMBOL_GPL(v4l2_m2m_querybuf); | |
340 | ||
341 | /** | |
342 | * v4l2_m2m_qbuf() - enqueue a source or destination buffer, depending on | |
343 | * the type | |
344 | */ | |
345 | int v4l2_m2m_qbuf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx, | |
346 | struct v4l2_buffer *buf) | |
347 | { | |
908a0d7c | 348 | struct vb2_queue *vq; |
7f98639d PO |
349 | int ret; |
350 | ||
351 | vq = v4l2_m2m_get_vq(m2m_ctx, buf->type); | |
908a0d7c | 352 | ret = vb2_qbuf(vq, buf); |
7f98639d PO |
353 | if (!ret) |
354 | v4l2_m2m_try_schedule(m2m_ctx); | |
355 | ||
356 | return ret; | |
357 | } | |
358 | EXPORT_SYMBOL_GPL(v4l2_m2m_qbuf); | |
359 | ||
360 | /** | |
361 | * v4l2_m2m_dqbuf() - dequeue a source or destination buffer, depending on | |
362 | * the type | |
363 | */ | |
364 | int v4l2_m2m_dqbuf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx, | |
365 | struct v4l2_buffer *buf) | |
366 | { | |
908a0d7c | 367 | struct vb2_queue *vq; |
7f98639d PO |
368 | |
369 | vq = v4l2_m2m_get_vq(m2m_ctx, buf->type); | |
908a0d7c | 370 | return vb2_dqbuf(vq, buf, file->f_flags & O_NONBLOCK); |
7f98639d PO |
371 | } |
372 | EXPORT_SYMBOL_GPL(v4l2_m2m_dqbuf); | |
373 | ||
83ae7c5a TS |
374 | /** |
375 | * v4l2_m2m_expbuf() - export a source or destination buffer, depending on | |
376 | * the type | |
377 | */ | |
378 | int v4l2_m2m_expbuf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx, | |
379 | struct v4l2_exportbuffer *eb) | |
380 | { | |
381 | struct vb2_queue *vq; | |
382 | ||
383 | vq = v4l2_m2m_get_vq(m2m_ctx, eb->type); | |
384 | return vb2_expbuf(vq, eb); | |
385 | } | |
386 | EXPORT_SYMBOL_GPL(v4l2_m2m_expbuf); | |
7f98639d PO |
387 | /** |
388 | * v4l2_m2m_streamon() - turn on streaming for a video queue | |
389 | */ | |
390 | int v4l2_m2m_streamon(struct file *file, struct v4l2_m2m_ctx *m2m_ctx, | |
391 | enum v4l2_buf_type type) | |
392 | { | |
908a0d7c | 393 | struct vb2_queue *vq; |
7f98639d PO |
394 | int ret; |
395 | ||
396 | vq = v4l2_m2m_get_vq(m2m_ctx, type); | |
908a0d7c | 397 | ret = vb2_streamon(vq, type); |
7f98639d PO |
398 | if (!ret) |
399 | v4l2_m2m_try_schedule(m2m_ctx); | |
400 | ||
401 | return ret; | |
402 | } | |
403 | EXPORT_SYMBOL_GPL(v4l2_m2m_streamon); | |
404 | ||
405 | /** | |
406 | * v4l2_m2m_streamoff() - turn off streaming for a video queue | |
407 | */ | |
408 | int v4l2_m2m_streamoff(struct file *file, struct v4l2_m2m_ctx *m2m_ctx, | |
409 | enum v4l2_buf_type type) | |
410 | { | |
908a0d7c | 411 | struct vb2_queue *vq; |
7f98639d PO |
412 | |
413 | vq = v4l2_m2m_get_vq(m2m_ctx, type); | |
908a0d7c | 414 | return vb2_streamoff(vq, type); |
7f98639d PO |
415 | } |
416 | EXPORT_SYMBOL_GPL(v4l2_m2m_streamoff); | |
417 | ||
418 | /** | |
419 | * v4l2_m2m_poll() - poll replacement, for destination buffers only | |
420 | * | |
421 | * Call from the driver's poll() function. Will poll both queues. If a buffer | |
422 | * is available to dequeue (with dqbuf) from the source queue, this will | |
423 | * indicate that a non-blocking write can be performed, while read will be | |
424 | * returned in case of the destination queue. | |
425 | */ | |
426 | unsigned int v4l2_m2m_poll(struct file *file, struct v4l2_m2m_ctx *m2m_ctx, | |
427 | struct poll_table_struct *wait) | |
428 | { | |
08eb8510 HV |
429 | struct video_device *vfd = video_devdata(file); |
430 | unsigned long req_events = poll_requested_events(wait); | |
908a0d7c MS |
431 | struct vb2_queue *src_q, *dst_q; |
432 | struct vb2_buffer *src_vb = NULL, *dst_vb = NULL; | |
7f98639d | 433 | unsigned int rc = 0; |
908a0d7c | 434 | unsigned long flags; |
7f98639d | 435 | |
08eb8510 HV |
436 | if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags)) { |
437 | struct v4l2_fh *fh = file->private_data; | |
438 | ||
439 | if (v4l2_event_pending(fh)) | |
440 | rc = POLLPRI; | |
441 | else if (req_events & POLLPRI) | |
442 | poll_wait(file, &fh->wait, wait); | |
443 | if (!(req_events & (POLLOUT | POLLWRNORM | POLLIN | POLLRDNORM))) | |
444 | return rc; | |
445 | } | |
446 | ||
7f98639d PO |
447 | src_q = v4l2_m2m_get_src_vq(m2m_ctx); |
448 | dst_q = v4l2_m2m_get_dst_vq(m2m_ctx); | |
449 | ||
908a0d7c MS |
450 | /* |
451 | * There has to be at least one buffer queued on each queued_list, which | |
452 | * means either in driver already or waiting for driver to claim it | |
453 | * and start processing. | |
454 | */ | |
455 | if ((!src_q->streaming || list_empty(&src_q->queued_list)) | |
456 | && (!dst_q->streaming || list_empty(&dst_q->queued_list))) { | |
08eb8510 | 457 | rc |= POLLERR; |
7f98639d PO |
458 | goto end; |
459 | } | |
460 | ||
908a0d7c MS |
461 | if (m2m_ctx->m2m_dev->m2m_ops->unlock) |
462 | m2m_ctx->m2m_dev->m2m_ops->unlock(m2m_ctx->priv); | |
463 | ||
464 | poll_wait(file, &src_q->done_wq, wait); | |
465 | poll_wait(file, &dst_q->done_wq, wait); | |
466 | ||
467 | if (m2m_ctx->m2m_dev->m2m_ops->lock) | |
468 | m2m_ctx->m2m_dev->m2m_ops->lock(m2m_ctx->priv); | |
469 | ||
470 | spin_lock_irqsave(&src_q->done_lock, flags); | |
471 | if (!list_empty(&src_q->done_list)) | |
472 | src_vb = list_first_entry(&src_q->done_list, struct vb2_buffer, | |
473 | done_entry); | |
474 | if (src_vb && (src_vb->state == VB2_BUF_STATE_DONE | |
475 | || src_vb->state == VB2_BUF_STATE_ERROR)) | |
476 | rc |= POLLOUT | POLLWRNORM; | |
477 | spin_unlock_irqrestore(&src_q->done_lock, flags); | |
478 | ||
479 | spin_lock_irqsave(&dst_q->done_lock, flags); | |
480 | if (!list_empty(&dst_q->done_list)) | |
481 | dst_vb = list_first_entry(&dst_q->done_list, struct vb2_buffer, | |
482 | done_entry); | |
483 | if (dst_vb && (dst_vb->state == VB2_BUF_STATE_DONE | |
484 | || dst_vb->state == VB2_BUF_STATE_ERROR)) | |
485 | rc |= POLLIN | POLLRDNORM; | |
486 | spin_unlock_irqrestore(&dst_q->done_lock, flags); | |
7f98639d PO |
487 | |
488 | end: | |
7f98639d PO |
489 | return rc; |
490 | } | |
491 | EXPORT_SYMBOL_GPL(v4l2_m2m_poll); | |
492 | ||
493 | /** | |
494 | * v4l2_m2m_mmap() - source and destination queues-aware mmap multiplexer | |
495 | * | |
496 | * Call from driver's mmap() function. Will handle mmap() for both queues | |
497 | * seamlessly for videobuffer, which will receive normal per-queue offsets and | |
498 | * proper videobuf queue pointers. The differentiation is made outside videobuf | |
499 | * by adding a predefined offset to buffers from one of the queues and | |
500 | * subtracting it before passing it back to videobuf. Only drivers (and | |
501 | * thus applications) receive modified offsets. | |
502 | */ | |
503 | int v4l2_m2m_mmap(struct file *file, struct v4l2_m2m_ctx *m2m_ctx, | |
504 | struct vm_area_struct *vma) | |
505 | { | |
506 | unsigned long offset = vma->vm_pgoff << PAGE_SHIFT; | |
908a0d7c | 507 | struct vb2_queue *vq; |
7f98639d PO |
508 | |
509 | if (offset < DST_QUEUE_OFF_BASE) { | |
510 | vq = v4l2_m2m_get_src_vq(m2m_ctx); | |
511 | } else { | |
512 | vq = v4l2_m2m_get_dst_vq(m2m_ctx); | |
513 | vma->vm_pgoff -= (DST_QUEUE_OFF_BASE >> PAGE_SHIFT); | |
514 | } | |
515 | ||
908a0d7c | 516 | return vb2_mmap(vq, vma); |
7f98639d PO |
517 | } |
518 | EXPORT_SYMBOL(v4l2_m2m_mmap); | |
519 | ||
520 | /** | |
521 | * v4l2_m2m_init() - initialize per-driver m2m data | |
522 | * | |
523 | * Usually called from driver's probe() function. | |
524 | */ | |
b1252eb8 | 525 | struct v4l2_m2m_dev *v4l2_m2m_init(const struct v4l2_m2m_ops *m2m_ops) |
7f98639d PO |
526 | { |
527 | struct v4l2_m2m_dev *m2m_dev; | |
528 | ||
3fac4eb3 NT |
529 | if (!m2m_ops || WARN_ON(!m2m_ops->device_run) || |
530 | WARN_ON(!m2m_ops->job_abort)) | |
7f98639d PO |
531 | return ERR_PTR(-EINVAL); |
532 | ||
7f98639d PO |
533 | m2m_dev = kzalloc(sizeof *m2m_dev, GFP_KERNEL); |
534 | if (!m2m_dev) | |
535 | return ERR_PTR(-ENOMEM); | |
536 | ||
537 | m2m_dev->curr_ctx = NULL; | |
538 | m2m_dev->m2m_ops = m2m_ops; | |
539 | INIT_LIST_HEAD(&m2m_dev->job_queue); | |
540 | spin_lock_init(&m2m_dev->job_spinlock); | |
541 | ||
542 | return m2m_dev; | |
543 | } | |
544 | EXPORT_SYMBOL_GPL(v4l2_m2m_init); | |
545 | ||
546 | /** | |
547 | * v4l2_m2m_release() - cleans up and frees a m2m_dev structure | |
548 | * | |
549 | * Usually called from driver's remove() function. | |
550 | */ | |
551 | void v4l2_m2m_release(struct v4l2_m2m_dev *m2m_dev) | |
552 | { | |
553 | kfree(m2m_dev); | |
554 | } | |
555 | EXPORT_SYMBOL_GPL(v4l2_m2m_release); | |
556 | ||
557 | /** | |
558 | * v4l2_m2m_ctx_init() - allocate and initialize a m2m context | |
559 | * @priv - driver's instance private data | |
560 | * @m2m_dev - a previously initialized m2m_dev struct | |
561 | * @vq_init - a callback for queue type-specific initialization function to be | |
562 | * used for initializing videobuf_queues | |
563 | * | |
564 | * Usually called from driver's open() function. | |
565 | */ | |
908a0d7c MS |
566 | struct v4l2_m2m_ctx *v4l2_m2m_ctx_init(struct v4l2_m2m_dev *m2m_dev, |
567 | void *drv_priv, | |
568 | int (*queue_init)(void *priv, struct vb2_queue *src_vq, struct vb2_queue *dst_vq)) | |
7f98639d PO |
569 | { |
570 | struct v4l2_m2m_ctx *m2m_ctx; | |
571 | struct v4l2_m2m_queue_ctx *out_q_ctx, *cap_q_ctx; | |
908a0d7c | 572 | int ret; |
7f98639d PO |
573 | |
574 | m2m_ctx = kzalloc(sizeof *m2m_ctx, GFP_KERNEL); | |
575 | if (!m2m_ctx) | |
576 | return ERR_PTR(-ENOMEM); | |
577 | ||
908a0d7c | 578 | m2m_ctx->priv = drv_priv; |
7f98639d | 579 | m2m_ctx->m2m_dev = m2m_dev; |
908a0d7c | 580 | init_waitqueue_head(&m2m_ctx->finished); |
7f98639d | 581 | |
908a0d7c MS |
582 | out_q_ctx = &m2m_ctx->out_q_ctx; |
583 | cap_q_ctx = &m2m_ctx->cap_q_ctx; | |
7f98639d PO |
584 | |
585 | INIT_LIST_HEAD(&out_q_ctx->rdy_queue); | |
586 | INIT_LIST_HEAD(&cap_q_ctx->rdy_queue); | |
908a0d7c MS |
587 | spin_lock_init(&out_q_ctx->rdy_spinlock); |
588 | spin_lock_init(&cap_q_ctx->rdy_spinlock); | |
7f98639d PO |
589 | |
590 | INIT_LIST_HEAD(&m2m_ctx->queue); | |
591 | ||
908a0d7c MS |
592 | ret = queue_init(drv_priv, &out_q_ctx->q, &cap_q_ctx->q); |
593 | ||
594 | if (ret) | |
595 | goto err; | |
7f98639d PO |
596 | |
597 | return m2m_ctx; | |
908a0d7c MS |
598 | err: |
599 | kfree(m2m_ctx); | |
600 | return ERR_PTR(ret); | |
7f98639d PO |
601 | } |
602 | EXPORT_SYMBOL_GPL(v4l2_m2m_ctx_init); | |
603 | ||
604 | /** | |
605 | * v4l2_m2m_ctx_release() - release m2m context | |
606 | * | |
607 | * Usually called from driver's release() function. | |
608 | */ | |
609 | void v4l2_m2m_ctx_release(struct v4l2_m2m_ctx *m2m_ctx) | |
610 | { | |
611 | struct v4l2_m2m_dev *m2m_dev; | |
7f98639d PO |
612 | unsigned long flags; |
613 | ||
614 | m2m_dev = m2m_ctx->m2m_dev; | |
615 | ||
616 | spin_lock_irqsave(&m2m_dev->job_spinlock, flags); | |
617 | if (m2m_ctx->job_flags & TRANS_RUNNING) { | |
618 | spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags); | |
619 | m2m_dev->m2m_ops->job_abort(m2m_ctx->priv); | |
620 | dprintk("m2m_ctx %p running, will wait to complete", m2m_ctx); | |
908a0d7c | 621 | wait_event(m2m_ctx->finished, !(m2m_ctx->job_flags & TRANS_RUNNING)); |
7f98639d PO |
622 | } else if (m2m_ctx->job_flags & TRANS_QUEUED) { |
623 | list_del(&m2m_ctx->queue); | |
624 | m2m_ctx->job_flags &= ~(TRANS_QUEUED | TRANS_RUNNING); | |
625 | spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags); | |
626 | dprintk("m2m_ctx: %p had been on queue and was removed\n", | |
627 | m2m_ctx); | |
628 | } else { | |
629 | /* Do nothing, was not on queue/running */ | |
630 | spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags); | |
631 | } | |
632 | ||
908a0d7c MS |
633 | vb2_queue_release(&m2m_ctx->cap_q_ctx.q); |
634 | vb2_queue_release(&m2m_ctx->out_q_ctx.q); | |
7f98639d PO |
635 | |
636 | kfree(m2m_ctx); | |
637 | } | |
638 | EXPORT_SYMBOL_GPL(v4l2_m2m_ctx_release); | |
639 | ||
640 | /** | |
641 | * v4l2_m2m_buf_queue() - add a buffer to the proper ready buffers list. | |
642 | * | |
643 | * Call from buf_queue(), videobuf_queue_ops callback. | |
7f98639d | 644 | */ |
908a0d7c | 645 | void v4l2_m2m_buf_queue(struct v4l2_m2m_ctx *m2m_ctx, struct vb2_buffer *vb) |
7f98639d | 646 | { |
908a0d7c | 647 | struct v4l2_m2m_buffer *b = container_of(vb, struct v4l2_m2m_buffer, vb); |
7f98639d | 648 | struct v4l2_m2m_queue_ctx *q_ctx; |
908a0d7c | 649 | unsigned long flags; |
7f98639d | 650 | |
908a0d7c | 651 | q_ctx = get_queue_ctx(m2m_ctx, vb->vb2_queue->type); |
7f98639d PO |
652 | if (!q_ctx) |
653 | return; | |
654 | ||
908a0d7c MS |
655 | spin_lock_irqsave(&q_ctx->rdy_spinlock, flags); |
656 | list_add_tail(&b->list, &q_ctx->rdy_queue); | |
7f98639d | 657 | q_ctx->num_rdy++; |
908a0d7c | 658 | spin_unlock_irqrestore(&q_ctx->rdy_spinlock, flags); |
7f98639d PO |
659 | } |
660 | EXPORT_SYMBOL_GPL(v4l2_m2m_buf_queue); | |
661 |