blob: 3b15bf5892a803b4da4e7eb8af7e6a20d83a7455 [file] [log] [blame]
Pawel Osciak7f986392010-04-23 05:38:37 -03001/*
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.
Pawel Osciak95072082011-03-13 15:23:32 -03008 * Pawel Osciak, <pawel@osciak.com>
Pawel Osciak7f986392010-04-23 05:38:37 -03009 * 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
Marek Szyprowski908a0d72011-01-12 06:50:24 -030020#include <media/videobuf2-core.h>
Pawel Osciak7f986392010-04-23 05:38:37 -030021#include <media/v4l2-mem2mem.h>
22
23MODULE_DESCRIPTION("Mem to mem device framework for videobuf");
Pawel Osciak95072082011-03-13 15:23:32 -030024MODULE_AUTHOR("Pawel Osciak, <pawel@osciak.com>");
Pawel Osciak7f986392010-04-23 05:38:37 -030025MODULE_LICENSE("GPL");
26
27static bool debug;
28module_param(debug, bool, 0644);
29
30#define dprintk(fmt, arg...) \
31 do { \
32 if (debug) \
33 printk(KERN_DEBUG "%s: " fmt, __func__, ## arg);\
34 } while (0)
35
36
37/* Instance is already queued on the job_queue */
38#define TRANS_QUEUED (1 << 0)
39/* Instance is currently running in hardware */
40#define TRANS_RUNNING (1 << 1)
41
42
43/* Offset base for buffers on the destination queue - used to distinguish
44 * between source and destination buffers when mmapping - they receive the same
45 * offsets but for different queues */
46#define DST_QUEUE_OFF_BASE (1 << 30)
47
48
49/**
50 * struct v4l2_m2m_dev - per-device context
51 * @curr_ctx: currently running instance
52 * @job_queue: instances queued to run
53 * @job_spinlock: protects job_queue
54 * @m2m_ops: driver callbacks
55 */
56struct v4l2_m2m_dev {
57 struct v4l2_m2m_ctx *curr_ctx;
58
59 struct list_head job_queue;
60 spinlock_t job_spinlock;
61
62 struct v4l2_m2m_ops *m2m_ops;
63};
64
65static struct v4l2_m2m_queue_ctx *get_queue_ctx(struct v4l2_m2m_ctx *m2m_ctx,
66 enum v4l2_buf_type type)
67{
Marek Szyprowski908a0d72011-01-12 06:50:24 -030068 if (V4L2_TYPE_IS_OUTPUT(type))
Pawel Osciak7f986392010-04-23 05:38:37 -030069 return &m2m_ctx->out_q_ctx;
Marek Szyprowski908a0d72011-01-12 06:50:24 -030070 else
71 return &m2m_ctx->cap_q_ctx;
Pawel Osciak7f986392010-04-23 05:38:37 -030072}
73
74/**
Marek Szyprowski908a0d72011-01-12 06:50:24 -030075 * v4l2_m2m_get_vq() - return vb2_queue for the given type
Pawel Osciak7f986392010-04-23 05:38:37 -030076 */
Marek Szyprowski908a0d72011-01-12 06:50:24 -030077struct vb2_queue *v4l2_m2m_get_vq(struct v4l2_m2m_ctx *m2m_ctx,
Pawel Osciak7f986392010-04-23 05:38:37 -030078 enum v4l2_buf_type type)
79{
80 struct v4l2_m2m_queue_ctx *q_ctx;
81
82 q_ctx = get_queue_ctx(m2m_ctx, type);
83 if (!q_ctx)
84 return NULL;
85
86 return &q_ctx->q;
87}
88EXPORT_SYMBOL(v4l2_m2m_get_vq);
89
90/**
91 * v4l2_m2m_next_buf() - return next buffer from the list of ready buffers
92 */
Marek Szyprowski908a0d72011-01-12 06:50:24 -030093void *v4l2_m2m_next_buf(struct v4l2_m2m_queue_ctx *q_ctx)
Pawel Osciak7f986392010-04-23 05:38:37 -030094{
Marek Szyprowski908a0d72011-01-12 06:50:24 -030095 struct v4l2_m2m_buffer *b = NULL;
Pawel Osciak7f986392010-04-23 05:38:37 -030096 unsigned long flags;
97
Marek Szyprowski908a0d72011-01-12 06:50:24 -030098 spin_lock_irqsave(&q_ctx->rdy_spinlock, flags);
Pawel Osciak7f986392010-04-23 05:38:37 -030099
100 if (list_empty(&q_ctx->rdy_queue))
101 goto end;
102
Marek Szyprowski908a0d72011-01-12 06:50:24 -0300103 b = list_entry(q_ctx->rdy_queue.next, struct v4l2_m2m_buffer, list);
Pawel Osciak7f986392010-04-23 05:38:37 -0300104end:
Marek Szyprowski908a0d72011-01-12 06:50:24 -0300105 spin_unlock_irqrestore(&q_ctx->rdy_spinlock, flags);
106 return &b->vb;
Pawel Osciak7f986392010-04-23 05:38:37 -0300107}
108EXPORT_SYMBOL_GPL(v4l2_m2m_next_buf);
109
110/**
111 * v4l2_m2m_buf_remove() - take off a buffer from the list of ready buffers and
112 * return it
113 */
Marek Szyprowski908a0d72011-01-12 06:50:24 -0300114void *v4l2_m2m_buf_remove(struct v4l2_m2m_queue_ctx *q_ctx)
Pawel Osciak7f986392010-04-23 05:38:37 -0300115{
Marek Szyprowski908a0d72011-01-12 06:50:24 -0300116 struct v4l2_m2m_buffer *b = NULL;
Pawel Osciak7f986392010-04-23 05:38:37 -0300117 unsigned long flags;
118
Marek Szyprowski908a0d72011-01-12 06:50:24 -0300119 spin_lock_irqsave(&q_ctx->rdy_spinlock, flags);
Pawel Osciak7f986392010-04-23 05:38:37 -0300120 if (!list_empty(&q_ctx->rdy_queue)) {
Marek Szyprowski908a0d72011-01-12 06:50:24 -0300121 b = list_entry(q_ctx->rdy_queue.next, struct v4l2_m2m_buffer,
122 list);
123 list_del(&b->list);
Pawel Osciak7f986392010-04-23 05:38:37 -0300124 q_ctx->num_rdy--;
125 }
Marek Szyprowski908a0d72011-01-12 06:50:24 -0300126 spin_unlock_irqrestore(&q_ctx->rdy_spinlock, flags);
Pawel Osciak7f986392010-04-23 05:38:37 -0300127
Marek Szyprowski908a0d72011-01-12 06:50:24 -0300128 return &b->vb;
Pawel Osciak7f986392010-04-23 05:38:37 -0300129}
130EXPORT_SYMBOL_GPL(v4l2_m2m_buf_remove);
131
132/*
133 * Scheduling handlers
134 */
135
136/**
137 * v4l2_m2m_get_curr_priv() - return driver private data for the currently
138 * running instance or NULL if no instance is running
139 */
140void *v4l2_m2m_get_curr_priv(struct v4l2_m2m_dev *m2m_dev)
141{
142 unsigned long flags;
143 void *ret = NULL;
144
145 spin_lock_irqsave(&m2m_dev->job_spinlock, flags);
146 if (m2m_dev->curr_ctx)
147 ret = m2m_dev->curr_ctx->priv;
148 spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags);
149
150 return ret;
151}
152EXPORT_SYMBOL(v4l2_m2m_get_curr_priv);
153
154/**
155 * v4l2_m2m_try_run() - select next job to perform and run it if possible
156 *
157 * Get next transaction (if present) from the waiting jobs list and run it.
158 */
159static void v4l2_m2m_try_run(struct v4l2_m2m_dev *m2m_dev)
160{
161 unsigned long flags;
162
163 spin_lock_irqsave(&m2m_dev->job_spinlock, flags);
164 if (NULL != m2m_dev->curr_ctx) {
165 spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags);
166 dprintk("Another instance is running, won't run now\n");
167 return;
168 }
169
170 if (list_empty(&m2m_dev->job_queue)) {
171 spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags);
172 dprintk("No job pending\n");
173 return;
174 }
175
176 m2m_dev->curr_ctx = list_entry(m2m_dev->job_queue.next,
177 struct v4l2_m2m_ctx, queue);
178 m2m_dev->curr_ctx->job_flags |= TRANS_RUNNING;
179 spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags);
180
181 m2m_dev->m2m_ops->device_run(m2m_dev->curr_ctx->priv);
182}
183
184/**
185 * v4l2_m2m_try_schedule() - check whether an instance is ready to be added to
186 * the pending job queue and add it if so.
187 * @m2m_ctx: m2m context assigned to the instance to be checked
188 *
189 * There are three basic requirements an instance has to meet to be able to run:
190 * 1) at least one source buffer has to be queued,
191 * 2) at least one destination buffer has to be queued,
192 * 3) streaming has to be on.
193 *
194 * There may also be additional, custom requirements. In such case the driver
195 * should supply a custom callback (job_ready in v4l2_m2m_ops) that should
196 * return 1 if the instance is ready.
197 * An example of the above could be an instance that requires more than one
198 * src/dst buffer per transaction.
199 */
200static void v4l2_m2m_try_schedule(struct v4l2_m2m_ctx *m2m_ctx)
201{
202 struct v4l2_m2m_dev *m2m_dev;
203 unsigned long flags_job, flags;
204
205 m2m_dev = m2m_ctx->m2m_dev;
206 dprintk("Trying to schedule a job for m2m_ctx: %p\n", m2m_ctx);
207
208 if (!m2m_ctx->out_q_ctx.q.streaming
209 || !m2m_ctx->cap_q_ctx.q.streaming) {
210 dprintk("Streaming needs to be on for both queues\n");
211 return;
212 }
213
214 spin_lock_irqsave(&m2m_dev->job_spinlock, flags_job);
215 if (m2m_ctx->job_flags & TRANS_QUEUED) {
216 spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags_job);
217 dprintk("On job queue already\n");
218 return;
219 }
220
Marek Szyprowski908a0d72011-01-12 06:50:24 -0300221 spin_lock_irqsave(&m2m_ctx->out_q_ctx.rdy_spinlock, flags);
Pawel Osciak7f986392010-04-23 05:38:37 -0300222 if (list_empty(&m2m_ctx->out_q_ctx.rdy_queue)) {
Marek Szyprowski908a0d72011-01-12 06:50:24 -0300223 spin_unlock_irqrestore(&m2m_ctx->out_q_ctx.rdy_spinlock, flags);
Pawel Osciak7f986392010-04-23 05:38:37 -0300224 spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags_job);
225 dprintk("No input buffers available\n");
226 return;
227 }
228 if (list_empty(&m2m_ctx->cap_q_ctx.rdy_queue)) {
Marek Szyprowski908a0d72011-01-12 06:50:24 -0300229 spin_unlock_irqrestore(&m2m_ctx->out_q_ctx.rdy_spinlock, flags);
Pawel Osciak7f986392010-04-23 05:38:37 -0300230 spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags_job);
231 dprintk("No output buffers available\n");
232 return;
233 }
Marek Szyprowski908a0d72011-01-12 06:50:24 -0300234 spin_unlock_irqrestore(&m2m_ctx->out_q_ctx.rdy_spinlock, flags);
Pawel Osciak7f986392010-04-23 05:38:37 -0300235
236 if (m2m_dev->m2m_ops->job_ready
237 && (!m2m_dev->m2m_ops->job_ready(m2m_ctx->priv))) {
238 spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags_job);
239 dprintk("Driver not ready\n");
240 return;
241 }
242
243 list_add_tail(&m2m_ctx->queue, &m2m_dev->job_queue);
244 m2m_ctx->job_flags |= TRANS_QUEUED;
245
246 spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags_job);
247
248 v4l2_m2m_try_run(m2m_dev);
249}
250
251/**
252 * v4l2_m2m_job_finish() - inform the framework that a job has been finished
253 * and have it clean up
254 *
255 * Called by a driver to yield back the device after it has finished with it.
256 * Should be called as soon as possible after reaching a state which allows
257 * other instances to take control of the device.
258 *
259 * This function has to be called only after device_run() callback has been
260 * called on the driver. To prevent recursion, it should not be called directly
261 * from the device_run() callback though.
262 */
263void v4l2_m2m_job_finish(struct v4l2_m2m_dev *m2m_dev,
264 struct v4l2_m2m_ctx *m2m_ctx)
265{
266 unsigned long flags;
267
268 spin_lock_irqsave(&m2m_dev->job_spinlock, flags);
269 if (!m2m_dev->curr_ctx || m2m_dev->curr_ctx != m2m_ctx) {
270 spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags);
271 dprintk("Called by an instance not currently running\n");
272 return;
273 }
274
275 list_del(&m2m_dev->curr_ctx->queue);
276 m2m_dev->curr_ctx->job_flags &= ~(TRANS_QUEUED | TRANS_RUNNING);
Marek Szyprowski908a0d72011-01-12 06:50:24 -0300277 wake_up(&m2m_dev->curr_ctx->finished);
Pawel Osciak7f986392010-04-23 05:38:37 -0300278 m2m_dev->curr_ctx = NULL;
279
280 spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags);
281
282 /* This instance might have more buffers ready, but since we do not
283 * allow more than one job on the job_queue per instance, each has
284 * to be scheduled separately after the previous one finishes. */
285 v4l2_m2m_try_schedule(m2m_ctx);
286 v4l2_m2m_try_run(m2m_dev);
287}
288EXPORT_SYMBOL(v4l2_m2m_job_finish);
289
290/**
291 * v4l2_m2m_reqbufs() - multi-queue-aware REQBUFS multiplexer
292 */
293int v4l2_m2m_reqbufs(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
294 struct v4l2_requestbuffers *reqbufs)
295{
Marek Szyprowski908a0d72011-01-12 06:50:24 -0300296 struct vb2_queue *vq;
Pawel Osciak7f986392010-04-23 05:38:37 -0300297
298 vq = v4l2_m2m_get_vq(m2m_ctx, reqbufs->type);
Marek Szyprowski908a0d72011-01-12 06:50:24 -0300299 return vb2_reqbufs(vq, reqbufs);
Pawel Osciak7f986392010-04-23 05:38:37 -0300300}
301EXPORT_SYMBOL_GPL(v4l2_m2m_reqbufs);
302
303/**
304 * v4l2_m2m_querybuf() - multi-queue-aware QUERYBUF multiplexer
305 *
306 * See v4l2_m2m_mmap() documentation for details.
307 */
308int v4l2_m2m_querybuf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
309 struct v4l2_buffer *buf)
310{
Marek Szyprowski908a0d72011-01-12 06:50:24 -0300311 struct vb2_queue *vq;
312 int ret = 0;
313 unsigned int i;
Pawel Osciak7f986392010-04-23 05:38:37 -0300314
315 vq = v4l2_m2m_get_vq(m2m_ctx, buf->type);
Marek Szyprowski908a0d72011-01-12 06:50:24 -0300316 ret = vb2_querybuf(vq, buf);
Pawel Osciak7f986392010-04-23 05:38:37 -0300317
Marek Szyprowski908a0d72011-01-12 06:50:24 -0300318 /* Adjust MMAP memory offsets for the CAPTURE queue */
319 if (buf->memory == V4L2_MEMORY_MMAP && !V4L2_TYPE_IS_OUTPUT(vq->type)) {
320 if (V4L2_TYPE_IS_MULTIPLANAR(vq->type)) {
321 for (i = 0; i < buf->length; ++i)
322 buf->m.planes[i].m.mem_offset
323 += DST_QUEUE_OFF_BASE;
324 } else {
325 buf->m.offset += DST_QUEUE_OFF_BASE;
326 }
Pawel Osciak7f986392010-04-23 05:38:37 -0300327 }
328
329 return ret;
330}
331EXPORT_SYMBOL_GPL(v4l2_m2m_querybuf);
332
333/**
334 * v4l2_m2m_qbuf() - enqueue a source or destination buffer, depending on
335 * the type
336 */
337int v4l2_m2m_qbuf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
338 struct v4l2_buffer *buf)
339{
Marek Szyprowski908a0d72011-01-12 06:50:24 -0300340 struct vb2_queue *vq;
Pawel Osciak7f986392010-04-23 05:38:37 -0300341 int ret;
342
343 vq = v4l2_m2m_get_vq(m2m_ctx, buf->type);
Marek Szyprowski908a0d72011-01-12 06:50:24 -0300344 ret = vb2_qbuf(vq, buf);
Pawel Osciak7f986392010-04-23 05:38:37 -0300345 if (!ret)
346 v4l2_m2m_try_schedule(m2m_ctx);
347
348 return ret;
349}
350EXPORT_SYMBOL_GPL(v4l2_m2m_qbuf);
351
352/**
353 * v4l2_m2m_dqbuf() - dequeue a source or destination buffer, depending on
354 * the type
355 */
356int v4l2_m2m_dqbuf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
357 struct v4l2_buffer *buf)
358{
Marek Szyprowski908a0d72011-01-12 06:50:24 -0300359 struct vb2_queue *vq;
Pawel Osciak7f986392010-04-23 05:38:37 -0300360
361 vq = v4l2_m2m_get_vq(m2m_ctx, buf->type);
Marek Szyprowski908a0d72011-01-12 06:50:24 -0300362 return vb2_dqbuf(vq, buf, file->f_flags & O_NONBLOCK);
Pawel Osciak7f986392010-04-23 05:38:37 -0300363}
364EXPORT_SYMBOL_GPL(v4l2_m2m_dqbuf);
365
366/**
367 * v4l2_m2m_streamon() - turn on streaming for a video queue
368 */
369int v4l2_m2m_streamon(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
370 enum v4l2_buf_type type)
371{
Marek Szyprowski908a0d72011-01-12 06:50:24 -0300372 struct vb2_queue *vq;
Pawel Osciak7f986392010-04-23 05:38:37 -0300373 int ret;
374
375 vq = v4l2_m2m_get_vq(m2m_ctx, type);
Marek Szyprowski908a0d72011-01-12 06:50:24 -0300376 ret = vb2_streamon(vq, type);
Pawel Osciak7f986392010-04-23 05:38:37 -0300377 if (!ret)
378 v4l2_m2m_try_schedule(m2m_ctx);
379
380 return ret;
381}
382EXPORT_SYMBOL_GPL(v4l2_m2m_streamon);
383
384/**
385 * v4l2_m2m_streamoff() - turn off streaming for a video queue
386 */
387int v4l2_m2m_streamoff(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
388 enum v4l2_buf_type type)
389{
Marek Szyprowski908a0d72011-01-12 06:50:24 -0300390 struct vb2_queue *vq;
Pawel Osciak7f986392010-04-23 05:38:37 -0300391
392 vq = v4l2_m2m_get_vq(m2m_ctx, type);
Marek Szyprowski908a0d72011-01-12 06:50:24 -0300393 return vb2_streamoff(vq, type);
Pawel Osciak7f986392010-04-23 05:38:37 -0300394}
395EXPORT_SYMBOL_GPL(v4l2_m2m_streamoff);
396
397/**
398 * v4l2_m2m_poll() - poll replacement, for destination buffers only
399 *
400 * Call from the driver's poll() function. Will poll both queues. If a buffer
401 * is available to dequeue (with dqbuf) from the source queue, this will
402 * indicate that a non-blocking write can be performed, while read will be
403 * returned in case of the destination queue.
404 */
405unsigned int v4l2_m2m_poll(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
406 struct poll_table_struct *wait)
407{
Marek Szyprowski908a0d72011-01-12 06:50:24 -0300408 struct vb2_queue *src_q, *dst_q;
409 struct vb2_buffer *src_vb = NULL, *dst_vb = NULL;
Pawel Osciak7f986392010-04-23 05:38:37 -0300410 unsigned int rc = 0;
Marek Szyprowski908a0d72011-01-12 06:50:24 -0300411 unsigned long flags;
Pawel Osciak7f986392010-04-23 05:38:37 -0300412
413 src_q = v4l2_m2m_get_src_vq(m2m_ctx);
414 dst_q = v4l2_m2m_get_dst_vq(m2m_ctx);
415
Marek Szyprowski908a0d72011-01-12 06:50:24 -0300416 /*
417 * There has to be at least one buffer queued on each queued_list, which
418 * means either in driver already or waiting for driver to claim it
419 * and start processing.
420 */
421 if ((!src_q->streaming || list_empty(&src_q->queued_list))
422 && (!dst_q->streaming || list_empty(&dst_q->queued_list))) {
Pawel Osciak7f986392010-04-23 05:38:37 -0300423 rc = POLLERR;
424 goto end;
425 }
426
Marek Szyprowski908a0d72011-01-12 06:50:24 -0300427 if (m2m_ctx->m2m_dev->m2m_ops->unlock)
428 m2m_ctx->m2m_dev->m2m_ops->unlock(m2m_ctx->priv);
429
430 poll_wait(file, &src_q->done_wq, wait);
431 poll_wait(file, &dst_q->done_wq, wait);
432
433 if (m2m_ctx->m2m_dev->m2m_ops->lock)
434 m2m_ctx->m2m_dev->m2m_ops->lock(m2m_ctx->priv);
435
436 spin_lock_irqsave(&src_q->done_lock, flags);
437 if (!list_empty(&src_q->done_list))
438 src_vb = list_first_entry(&src_q->done_list, struct vb2_buffer,
439 done_entry);
440 if (src_vb && (src_vb->state == VB2_BUF_STATE_DONE
441 || src_vb->state == VB2_BUF_STATE_ERROR))
442 rc |= POLLOUT | POLLWRNORM;
443 spin_unlock_irqrestore(&src_q->done_lock, flags);
444
445 spin_lock_irqsave(&dst_q->done_lock, flags);
446 if (!list_empty(&dst_q->done_list))
447 dst_vb = list_first_entry(&dst_q->done_list, struct vb2_buffer,
448 done_entry);
449 if (dst_vb && (dst_vb->state == VB2_BUF_STATE_DONE
450 || dst_vb->state == VB2_BUF_STATE_ERROR))
451 rc |= POLLIN | POLLRDNORM;
452 spin_unlock_irqrestore(&dst_q->done_lock, flags);
Pawel Osciak7f986392010-04-23 05:38:37 -0300453
454end:
Pawel Osciak7f986392010-04-23 05:38:37 -0300455 return rc;
456}
457EXPORT_SYMBOL_GPL(v4l2_m2m_poll);
458
459/**
460 * v4l2_m2m_mmap() - source and destination queues-aware mmap multiplexer
461 *
462 * Call from driver's mmap() function. Will handle mmap() for both queues
463 * seamlessly for videobuffer, which will receive normal per-queue offsets and
464 * proper videobuf queue pointers. The differentiation is made outside videobuf
465 * by adding a predefined offset to buffers from one of the queues and
466 * subtracting it before passing it back to videobuf. Only drivers (and
467 * thus applications) receive modified offsets.
468 */
469int v4l2_m2m_mmap(struct file *file, struct v4l2_m2m_ctx *m2m_ctx,
470 struct vm_area_struct *vma)
471{
472 unsigned long offset = vma->vm_pgoff << PAGE_SHIFT;
Marek Szyprowski908a0d72011-01-12 06:50:24 -0300473 struct vb2_queue *vq;
Pawel Osciak7f986392010-04-23 05:38:37 -0300474
475 if (offset < DST_QUEUE_OFF_BASE) {
476 vq = v4l2_m2m_get_src_vq(m2m_ctx);
477 } else {
478 vq = v4l2_m2m_get_dst_vq(m2m_ctx);
479 vma->vm_pgoff -= (DST_QUEUE_OFF_BASE >> PAGE_SHIFT);
480 }
481
Marek Szyprowski908a0d72011-01-12 06:50:24 -0300482 return vb2_mmap(vq, vma);
Pawel Osciak7f986392010-04-23 05:38:37 -0300483}
484EXPORT_SYMBOL(v4l2_m2m_mmap);
485
486/**
487 * v4l2_m2m_init() - initialize per-driver m2m data
488 *
489 * Usually called from driver's probe() function.
490 */
491struct v4l2_m2m_dev *v4l2_m2m_init(struct v4l2_m2m_ops *m2m_ops)
492{
493 struct v4l2_m2m_dev *m2m_dev;
494
495 if (!m2m_ops)
496 return ERR_PTR(-EINVAL);
497
498 BUG_ON(!m2m_ops->device_run);
499 BUG_ON(!m2m_ops->job_abort);
500
501 m2m_dev = kzalloc(sizeof *m2m_dev, GFP_KERNEL);
502 if (!m2m_dev)
503 return ERR_PTR(-ENOMEM);
504
505 m2m_dev->curr_ctx = NULL;
506 m2m_dev->m2m_ops = m2m_ops;
507 INIT_LIST_HEAD(&m2m_dev->job_queue);
508 spin_lock_init(&m2m_dev->job_spinlock);
509
510 return m2m_dev;
511}
512EXPORT_SYMBOL_GPL(v4l2_m2m_init);
513
514/**
515 * v4l2_m2m_release() - cleans up and frees a m2m_dev structure
516 *
517 * Usually called from driver's remove() function.
518 */
519void v4l2_m2m_release(struct v4l2_m2m_dev *m2m_dev)
520{
521 kfree(m2m_dev);
522}
523EXPORT_SYMBOL_GPL(v4l2_m2m_release);
524
525/**
526 * v4l2_m2m_ctx_init() - allocate and initialize a m2m context
527 * @priv - driver's instance private data
528 * @m2m_dev - a previously initialized m2m_dev struct
529 * @vq_init - a callback for queue type-specific initialization function to be
530 * used for initializing videobuf_queues
531 *
532 * Usually called from driver's open() function.
533 */
Marek Szyprowski908a0d72011-01-12 06:50:24 -0300534struct v4l2_m2m_ctx *v4l2_m2m_ctx_init(struct v4l2_m2m_dev *m2m_dev,
535 void *drv_priv,
536 int (*queue_init)(void *priv, struct vb2_queue *src_vq, struct vb2_queue *dst_vq))
Pawel Osciak7f986392010-04-23 05:38:37 -0300537{
538 struct v4l2_m2m_ctx *m2m_ctx;
539 struct v4l2_m2m_queue_ctx *out_q_ctx, *cap_q_ctx;
Marek Szyprowski908a0d72011-01-12 06:50:24 -0300540 int ret;
Pawel Osciak7f986392010-04-23 05:38:37 -0300541
542 m2m_ctx = kzalloc(sizeof *m2m_ctx, GFP_KERNEL);
543 if (!m2m_ctx)
544 return ERR_PTR(-ENOMEM);
545
Marek Szyprowski908a0d72011-01-12 06:50:24 -0300546 m2m_ctx->priv = drv_priv;
Pawel Osciak7f986392010-04-23 05:38:37 -0300547 m2m_ctx->m2m_dev = m2m_dev;
Marek Szyprowski908a0d72011-01-12 06:50:24 -0300548 init_waitqueue_head(&m2m_ctx->finished);
Pawel Osciak7f986392010-04-23 05:38:37 -0300549
Marek Szyprowski908a0d72011-01-12 06:50:24 -0300550 out_q_ctx = &m2m_ctx->out_q_ctx;
551 cap_q_ctx = &m2m_ctx->cap_q_ctx;
Pawel Osciak7f986392010-04-23 05:38:37 -0300552
553 INIT_LIST_HEAD(&out_q_ctx->rdy_queue);
554 INIT_LIST_HEAD(&cap_q_ctx->rdy_queue);
Marek Szyprowski908a0d72011-01-12 06:50:24 -0300555 spin_lock_init(&out_q_ctx->rdy_spinlock);
556 spin_lock_init(&cap_q_ctx->rdy_spinlock);
Pawel Osciak7f986392010-04-23 05:38:37 -0300557
558 INIT_LIST_HEAD(&m2m_ctx->queue);
559
Marek Szyprowski908a0d72011-01-12 06:50:24 -0300560 ret = queue_init(drv_priv, &out_q_ctx->q, &cap_q_ctx->q);
561
562 if (ret)
563 goto err;
Pawel Osciak7f986392010-04-23 05:38:37 -0300564
565 return m2m_ctx;
Marek Szyprowski908a0d72011-01-12 06:50:24 -0300566err:
567 kfree(m2m_ctx);
568 return ERR_PTR(ret);
Pawel Osciak7f986392010-04-23 05:38:37 -0300569}
570EXPORT_SYMBOL_GPL(v4l2_m2m_ctx_init);
571
572/**
573 * v4l2_m2m_ctx_release() - release m2m context
574 *
575 * Usually called from driver's release() function.
576 */
577void v4l2_m2m_ctx_release(struct v4l2_m2m_ctx *m2m_ctx)
578{
579 struct v4l2_m2m_dev *m2m_dev;
Pawel Osciak7f986392010-04-23 05:38:37 -0300580 unsigned long flags;
581
582 m2m_dev = m2m_ctx->m2m_dev;
583
584 spin_lock_irqsave(&m2m_dev->job_spinlock, flags);
585 if (m2m_ctx->job_flags & TRANS_RUNNING) {
586 spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags);
587 m2m_dev->m2m_ops->job_abort(m2m_ctx->priv);
588 dprintk("m2m_ctx %p running, will wait to complete", m2m_ctx);
Marek Szyprowski908a0d72011-01-12 06:50:24 -0300589 wait_event(m2m_ctx->finished, !(m2m_ctx->job_flags & TRANS_RUNNING));
Pawel Osciak7f986392010-04-23 05:38:37 -0300590 } else if (m2m_ctx->job_flags & TRANS_QUEUED) {
591 list_del(&m2m_ctx->queue);
592 m2m_ctx->job_flags &= ~(TRANS_QUEUED | TRANS_RUNNING);
593 spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags);
594 dprintk("m2m_ctx: %p had been on queue and was removed\n",
595 m2m_ctx);
596 } else {
597 /* Do nothing, was not on queue/running */
598 spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags);
599 }
600
Marek Szyprowski908a0d72011-01-12 06:50:24 -0300601 vb2_queue_release(&m2m_ctx->cap_q_ctx.q);
602 vb2_queue_release(&m2m_ctx->out_q_ctx.q);
Pawel Osciak7f986392010-04-23 05:38:37 -0300603
604 kfree(m2m_ctx);
605}
606EXPORT_SYMBOL_GPL(v4l2_m2m_ctx_release);
607
608/**
609 * v4l2_m2m_buf_queue() - add a buffer to the proper ready buffers list.
610 *
611 * Call from buf_queue(), videobuf_queue_ops callback.
Pawel Osciak7f986392010-04-23 05:38:37 -0300612 */
Marek Szyprowski908a0d72011-01-12 06:50:24 -0300613void v4l2_m2m_buf_queue(struct v4l2_m2m_ctx *m2m_ctx, struct vb2_buffer *vb)
Pawel Osciak7f986392010-04-23 05:38:37 -0300614{
Marek Szyprowski908a0d72011-01-12 06:50:24 -0300615 struct v4l2_m2m_buffer *b = container_of(vb, struct v4l2_m2m_buffer, vb);
Pawel Osciak7f986392010-04-23 05:38:37 -0300616 struct v4l2_m2m_queue_ctx *q_ctx;
Marek Szyprowski908a0d72011-01-12 06:50:24 -0300617 unsigned long flags;
Pawel Osciak7f986392010-04-23 05:38:37 -0300618
Marek Szyprowski908a0d72011-01-12 06:50:24 -0300619 q_ctx = get_queue_ctx(m2m_ctx, vb->vb2_queue->type);
Pawel Osciak7f986392010-04-23 05:38:37 -0300620 if (!q_ctx)
621 return;
622
Marek Szyprowski908a0d72011-01-12 06:50:24 -0300623 spin_lock_irqsave(&q_ctx->rdy_spinlock, flags);
624 list_add_tail(&b->list, &q_ctx->rdy_queue);
Pawel Osciak7f986392010-04-23 05:38:37 -0300625 q_ctx->num_rdy++;
Marek Szyprowski908a0d72011-01-12 06:50:24 -0300626 spin_unlock_irqrestore(&q_ctx->rdy_spinlock, flags);
Pawel Osciak7f986392010-04-23 05:38:37 -0300627}
628EXPORT_SYMBOL_GPL(v4l2_m2m_buf_queue);
629