2 * Memory-to-memory device framework for Video for Linux 2 and videobuf.
4 * Helper functions for devices that use videobuf buffers for both their
5 * source and destination.
7 * Copyright (c) 2009-2010 Samsung Electronics Co., Ltd.
8 * Pawel Osciak, <p.osciak@samsung.com>
9 * Marek Szyprowski, <m.szyprowski@samsung.com>
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.
16 #include <linux/module.h>
17 #include <linux/sched.h>
18 #include <linux/slab.h>
20 #include <media/videobuf-core.h>
21 #include <media/v4l2-mem2mem.h>
23 MODULE_DESCRIPTION("Mem to mem device framework for videobuf");
24 MODULE_AUTHOR("Pawel Osciak, <p.osciak@samsung.com>");
25 MODULE_LICENSE("GPL");
28 module_param(debug
, bool, 0644);
30 #define dprintk(fmt, arg...) \
33 printk(KERN_DEBUG "%s: " fmt, __func__, ## arg);\
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)
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)
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
57 struct v4l2_m2m_ctx
*curr_ctx
;
59 struct list_head job_queue
;
60 spinlock_t job_spinlock
;
62 struct v4l2_m2m_ops
*m2m_ops
;
65 static struct v4l2_m2m_queue_ctx
*get_queue_ctx(struct v4l2_m2m_ctx
*m2m_ctx
,
66 enum v4l2_buf_type type
)
69 case V4L2_BUF_TYPE_VIDEO_CAPTURE
:
70 return &m2m_ctx
->cap_q_ctx
;
71 case V4L2_BUF_TYPE_VIDEO_OUTPUT
:
72 return &m2m_ctx
->out_q_ctx
;
74 printk(KERN_ERR
"Invalid buffer type\n");
80 * v4l2_m2m_get_vq() - return videobuf_queue for the given type
82 struct videobuf_queue
*v4l2_m2m_get_vq(struct v4l2_m2m_ctx
*m2m_ctx
,
83 enum v4l2_buf_type type
)
85 struct v4l2_m2m_queue_ctx
*q_ctx
;
87 q_ctx
= get_queue_ctx(m2m_ctx
, type
);
93 EXPORT_SYMBOL(v4l2_m2m_get_vq
);
96 * v4l2_m2m_next_buf() - return next buffer from the list of ready buffers
98 void *v4l2_m2m_next_buf(struct v4l2_m2m_ctx
*m2m_ctx
, enum v4l2_buf_type type
)
100 struct v4l2_m2m_queue_ctx
*q_ctx
;
101 struct videobuf_buffer
*vb
= NULL
;
104 q_ctx
= get_queue_ctx(m2m_ctx
, type
);
108 spin_lock_irqsave(q_ctx
->q
.irqlock
, flags
);
110 if (list_empty(&q_ctx
->rdy_queue
))
113 vb
= list_entry(q_ctx
->rdy_queue
.next
, struct videobuf_buffer
, queue
);
114 vb
->state
= VIDEOBUF_ACTIVE
;
117 spin_unlock_irqrestore(q_ctx
->q
.irqlock
, flags
);
120 EXPORT_SYMBOL_GPL(v4l2_m2m_next_buf
);
123 * v4l2_m2m_buf_remove() - take off a buffer from the list of ready buffers and
126 void *v4l2_m2m_buf_remove(struct v4l2_m2m_ctx
*m2m_ctx
, enum v4l2_buf_type type
)
128 struct v4l2_m2m_queue_ctx
*q_ctx
;
129 struct videobuf_buffer
*vb
= NULL
;
132 q_ctx
= get_queue_ctx(m2m_ctx
, type
);
136 spin_lock_irqsave(q_ctx
->q
.irqlock
, flags
);
137 if (!list_empty(&q_ctx
->rdy_queue
)) {
138 vb
= list_entry(q_ctx
->rdy_queue
.next
, struct videobuf_buffer
,
140 list_del(&vb
->queue
);
143 spin_unlock_irqrestore(q_ctx
->q
.irqlock
, flags
);
147 EXPORT_SYMBOL_GPL(v4l2_m2m_buf_remove
);
150 * Scheduling handlers
154 * v4l2_m2m_get_curr_priv() - return driver private data for the currently
155 * running instance or NULL if no instance is running
157 void *v4l2_m2m_get_curr_priv(struct v4l2_m2m_dev
*m2m_dev
)
162 spin_lock_irqsave(&m2m_dev
->job_spinlock
, flags
);
163 if (m2m_dev
->curr_ctx
)
164 ret
= m2m_dev
->curr_ctx
->priv
;
165 spin_unlock_irqrestore(&m2m_dev
->job_spinlock
, flags
);
169 EXPORT_SYMBOL(v4l2_m2m_get_curr_priv
);
172 * v4l2_m2m_try_run() - select next job to perform and run it if possible
174 * Get next transaction (if present) from the waiting jobs list and run it.
176 static void v4l2_m2m_try_run(struct v4l2_m2m_dev
*m2m_dev
)
180 spin_lock_irqsave(&m2m_dev
->job_spinlock
, flags
);
181 if (NULL
!= m2m_dev
->curr_ctx
) {
182 spin_unlock_irqrestore(&m2m_dev
->job_spinlock
, flags
);
183 dprintk("Another instance is running, won't run now\n");
187 if (list_empty(&m2m_dev
->job_queue
)) {
188 spin_unlock_irqrestore(&m2m_dev
->job_spinlock
, flags
);
189 dprintk("No job pending\n");
193 m2m_dev
->curr_ctx
= list_entry(m2m_dev
->job_queue
.next
,
194 struct v4l2_m2m_ctx
, queue
);
195 m2m_dev
->curr_ctx
->job_flags
|= TRANS_RUNNING
;
196 spin_unlock_irqrestore(&m2m_dev
->job_spinlock
, flags
);
198 m2m_dev
->m2m_ops
->device_run(m2m_dev
->curr_ctx
->priv
);
202 * v4l2_m2m_try_schedule() - check whether an instance is ready to be added to
203 * the pending job queue and add it if so.
204 * @m2m_ctx: m2m context assigned to the instance to be checked
206 * There are three basic requirements an instance has to meet to be able to run:
207 * 1) at least one source buffer has to be queued,
208 * 2) at least one destination buffer has to be queued,
209 * 3) streaming has to be on.
211 * There may also be additional, custom requirements. In such case the driver
212 * should supply a custom callback (job_ready in v4l2_m2m_ops) that should
213 * return 1 if the instance is ready.
214 * An example of the above could be an instance that requires more than one
215 * src/dst buffer per transaction.
217 static void v4l2_m2m_try_schedule(struct v4l2_m2m_ctx
*m2m_ctx
)
219 struct v4l2_m2m_dev
*m2m_dev
;
220 unsigned long flags_job
, flags
;
222 m2m_dev
= m2m_ctx
->m2m_dev
;
223 dprintk("Trying to schedule a job for m2m_ctx: %p\n", m2m_ctx
);
225 if (!m2m_ctx
->out_q_ctx
.q
.streaming
226 || !m2m_ctx
->cap_q_ctx
.q
.streaming
) {
227 dprintk("Streaming needs to be on for both queues\n");
231 spin_lock_irqsave(&m2m_dev
->job_spinlock
, flags_job
);
232 if (m2m_ctx
->job_flags
& TRANS_QUEUED
) {
233 spin_unlock_irqrestore(&m2m_dev
->job_spinlock
, flags_job
);
234 dprintk("On job queue already\n");
238 spin_lock_irqsave(m2m_ctx
->out_q_ctx
.q
.irqlock
, flags
);
239 if (list_empty(&m2m_ctx
->out_q_ctx
.rdy_queue
)) {
240 spin_unlock_irqrestore(m2m_ctx
->out_q_ctx
.q
.irqlock
, flags
);
241 spin_unlock_irqrestore(&m2m_dev
->job_spinlock
, flags_job
);
242 dprintk("No input buffers available\n");
245 if (list_empty(&m2m_ctx
->cap_q_ctx
.rdy_queue
)) {
246 spin_unlock_irqrestore(m2m_ctx
->out_q_ctx
.q
.irqlock
, flags
);
247 spin_unlock_irqrestore(&m2m_dev
->job_spinlock
, flags_job
);
248 dprintk("No output buffers available\n");
251 spin_unlock_irqrestore(m2m_ctx
->out_q_ctx
.q
.irqlock
, flags
);
253 if (m2m_dev
->m2m_ops
->job_ready
254 && (!m2m_dev
->m2m_ops
->job_ready(m2m_ctx
->priv
))) {
255 spin_unlock_irqrestore(&m2m_dev
->job_spinlock
, flags_job
);
256 dprintk("Driver not ready\n");
260 list_add_tail(&m2m_ctx
->queue
, &m2m_dev
->job_queue
);
261 m2m_ctx
->job_flags
|= TRANS_QUEUED
;
263 spin_unlock_irqrestore(&m2m_dev
->job_spinlock
, flags_job
);
265 v4l2_m2m_try_run(m2m_dev
);
269 * v4l2_m2m_job_finish() - inform the framework that a job has been finished
270 * and have it clean up
272 * Called by a driver to yield back the device after it has finished with it.
273 * Should be called as soon as possible after reaching a state which allows
274 * other instances to take control of the device.
276 * This function has to be called only after device_run() callback has been
277 * called on the driver. To prevent recursion, it should not be called directly
278 * from the device_run() callback though.
280 void v4l2_m2m_job_finish(struct v4l2_m2m_dev
*m2m_dev
,
281 struct v4l2_m2m_ctx
*m2m_ctx
)
285 spin_lock_irqsave(&m2m_dev
->job_spinlock
, flags
);
286 if (!m2m_dev
->curr_ctx
|| m2m_dev
->curr_ctx
!= m2m_ctx
) {
287 spin_unlock_irqrestore(&m2m_dev
->job_spinlock
, flags
);
288 dprintk("Called by an instance not currently running\n");
292 list_del(&m2m_dev
->curr_ctx
->queue
);
293 m2m_dev
->curr_ctx
->job_flags
&= ~(TRANS_QUEUED
| TRANS_RUNNING
);
294 m2m_dev
->curr_ctx
= NULL
;
296 spin_unlock_irqrestore(&m2m_dev
->job_spinlock
, flags
);
298 /* This instance might have more buffers ready, but since we do not
299 * allow more than one job on the job_queue per instance, each has
300 * to be scheduled separately after the previous one finishes. */
301 v4l2_m2m_try_schedule(m2m_ctx
);
302 v4l2_m2m_try_run(m2m_dev
);
304 EXPORT_SYMBOL(v4l2_m2m_job_finish
);
307 * v4l2_m2m_reqbufs() - multi-queue-aware REQBUFS multiplexer
309 int v4l2_m2m_reqbufs(struct file
*file
, struct v4l2_m2m_ctx
*m2m_ctx
,
310 struct v4l2_requestbuffers
*reqbufs
)
312 struct videobuf_queue
*vq
;
314 vq
= v4l2_m2m_get_vq(m2m_ctx
, reqbufs
->type
);
315 return videobuf_reqbufs(vq
, reqbufs
);
317 EXPORT_SYMBOL_GPL(v4l2_m2m_reqbufs
);
320 * v4l2_m2m_querybuf() - multi-queue-aware QUERYBUF multiplexer
322 * See v4l2_m2m_mmap() documentation for details.
324 int v4l2_m2m_querybuf(struct file
*file
, struct v4l2_m2m_ctx
*m2m_ctx
,
325 struct v4l2_buffer
*buf
)
327 struct videobuf_queue
*vq
;
330 vq
= v4l2_m2m_get_vq(m2m_ctx
, buf
->type
);
331 ret
= videobuf_querybuf(vq
, buf
);
333 if (buf
->memory
== V4L2_MEMORY_MMAP
334 && vq
->type
== V4L2_BUF_TYPE_VIDEO_CAPTURE
) {
335 buf
->m
.offset
+= DST_QUEUE_OFF_BASE
;
340 EXPORT_SYMBOL_GPL(v4l2_m2m_querybuf
);
343 * v4l2_m2m_qbuf() - enqueue a source or destination buffer, depending on
346 int v4l2_m2m_qbuf(struct file
*file
, struct v4l2_m2m_ctx
*m2m_ctx
,
347 struct v4l2_buffer
*buf
)
349 struct videobuf_queue
*vq
;
352 vq
= v4l2_m2m_get_vq(m2m_ctx
, buf
->type
);
353 ret
= videobuf_qbuf(vq
, buf
);
355 v4l2_m2m_try_schedule(m2m_ctx
);
359 EXPORT_SYMBOL_GPL(v4l2_m2m_qbuf
);
362 * v4l2_m2m_dqbuf() - dequeue a source or destination buffer, depending on
365 int v4l2_m2m_dqbuf(struct file
*file
, struct v4l2_m2m_ctx
*m2m_ctx
,
366 struct v4l2_buffer
*buf
)
368 struct videobuf_queue
*vq
;
370 vq
= v4l2_m2m_get_vq(m2m_ctx
, buf
->type
);
371 return videobuf_dqbuf(vq
, buf
, file
->f_flags
& O_NONBLOCK
);
373 EXPORT_SYMBOL_GPL(v4l2_m2m_dqbuf
);
376 * v4l2_m2m_streamon() - turn on streaming for a video queue
378 int v4l2_m2m_streamon(struct file
*file
, struct v4l2_m2m_ctx
*m2m_ctx
,
379 enum v4l2_buf_type type
)
381 struct videobuf_queue
*vq
;
384 vq
= v4l2_m2m_get_vq(m2m_ctx
, type
);
385 ret
= videobuf_streamon(vq
);
387 v4l2_m2m_try_schedule(m2m_ctx
);
391 EXPORT_SYMBOL_GPL(v4l2_m2m_streamon
);
394 * v4l2_m2m_streamoff() - turn off streaming for a video queue
396 int v4l2_m2m_streamoff(struct file
*file
, struct v4l2_m2m_ctx
*m2m_ctx
,
397 enum v4l2_buf_type type
)
399 struct videobuf_queue
*vq
;
401 vq
= v4l2_m2m_get_vq(m2m_ctx
, type
);
402 return videobuf_streamoff(vq
);
404 EXPORT_SYMBOL_GPL(v4l2_m2m_streamoff
);
407 * v4l2_m2m_poll() - poll replacement, for destination buffers only
409 * Call from the driver's poll() function. Will poll both queues. If a buffer
410 * is available to dequeue (with dqbuf) from the source queue, this will
411 * indicate that a non-blocking write can be performed, while read will be
412 * returned in case of the destination queue.
414 unsigned int v4l2_m2m_poll(struct file
*file
, struct v4l2_m2m_ctx
*m2m_ctx
,
415 struct poll_table_struct
*wait
)
417 struct videobuf_queue
*src_q
, *dst_q
;
418 struct videobuf_buffer
*src_vb
= NULL
, *dst_vb
= NULL
;
421 src_q
= v4l2_m2m_get_src_vq(m2m_ctx
);
422 dst_q
= v4l2_m2m_get_dst_vq(m2m_ctx
);
424 videobuf_queue_lock(src_q
);
425 videobuf_queue_lock(dst_q
);
427 if (src_q
->streaming
&& !list_empty(&src_q
->stream
))
428 src_vb
= list_first_entry(&src_q
->stream
,
429 struct videobuf_buffer
, stream
);
430 if (dst_q
->streaming
&& !list_empty(&dst_q
->stream
))
431 dst_vb
= list_first_entry(&dst_q
->stream
,
432 struct videobuf_buffer
, stream
);
434 if (!src_vb
&& !dst_vb
) {
440 poll_wait(file
, &src_vb
->done
, wait
);
441 if (src_vb
->state
== VIDEOBUF_DONE
442 || src_vb
->state
== VIDEOBUF_ERROR
)
443 rc
|= POLLOUT
| POLLWRNORM
;
446 poll_wait(file
, &dst_vb
->done
, wait
);
447 if (dst_vb
->state
== VIDEOBUF_DONE
448 || dst_vb
->state
== VIDEOBUF_ERROR
)
449 rc
|= POLLIN
| POLLRDNORM
;
453 videobuf_queue_unlock(dst_q
);
454 videobuf_queue_unlock(src_q
);
457 EXPORT_SYMBOL_GPL(v4l2_m2m_poll
);
460 * v4l2_m2m_mmap() - source and destination queues-aware mmap multiplexer
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.
469 int v4l2_m2m_mmap(struct file
*file
, struct v4l2_m2m_ctx
*m2m_ctx
,
470 struct vm_area_struct
*vma
)
472 unsigned long offset
= vma
->vm_pgoff
<< PAGE_SHIFT
;
473 struct videobuf_queue
*vq
;
475 if (offset
< DST_QUEUE_OFF_BASE
) {
476 vq
= v4l2_m2m_get_src_vq(m2m_ctx
);
478 vq
= v4l2_m2m_get_dst_vq(m2m_ctx
);
479 vma
->vm_pgoff
-= (DST_QUEUE_OFF_BASE
>> PAGE_SHIFT
);
482 return videobuf_mmap_mapper(vq
, vma
);
484 EXPORT_SYMBOL(v4l2_m2m_mmap
);
487 * v4l2_m2m_init() - initialize per-driver m2m data
489 * Usually called from driver's probe() function.
491 struct v4l2_m2m_dev
*v4l2_m2m_init(struct v4l2_m2m_ops
*m2m_ops
)
493 struct v4l2_m2m_dev
*m2m_dev
;
496 return ERR_PTR(-EINVAL
);
498 BUG_ON(!m2m_ops
->device_run
);
499 BUG_ON(!m2m_ops
->job_abort
);
501 m2m_dev
= kzalloc(sizeof *m2m_dev
, GFP_KERNEL
);
503 return ERR_PTR(-ENOMEM
);
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
);
512 EXPORT_SYMBOL_GPL(v4l2_m2m_init
);
515 * v4l2_m2m_release() - cleans up and frees a m2m_dev structure
517 * Usually called from driver's remove() function.
519 void v4l2_m2m_release(struct v4l2_m2m_dev
*m2m_dev
)
523 EXPORT_SYMBOL_GPL(v4l2_m2m_release
);
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
532 * Usually called from driver's open() function.
534 struct v4l2_m2m_ctx
*v4l2_m2m_ctx_init(void *priv
, struct v4l2_m2m_dev
*m2m_dev
,
535 void (*vq_init
)(void *priv
, struct videobuf_queue
*,
538 struct v4l2_m2m_ctx
*m2m_ctx
;
539 struct v4l2_m2m_queue_ctx
*out_q_ctx
, *cap_q_ctx
;
542 return ERR_PTR(-EINVAL
);
544 m2m_ctx
= kzalloc(sizeof *m2m_ctx
, GFP_KERNEL
);
546 return ERR_PTR(-ENOMEM
);
548 m2m_ctx
->priv
= priv
;
549 m2m_ctx
->m2m_dev
= m2m_dev
;
551 out_q_ctx
= get_queue_ctx(m2m_ctx
, V4L2_BUF_TYPE_VIDEO_OUTPUT
);
552 cap_q_ctx
= get_queue_ctx(m2m_ctx
, V4L2_BUF_TYPE_VIDEO_CAPTURE
);
554 INIT_LIST_HEAD(&out_q_ctx
->rdy_queue
);
555 INIT_LIST_HEAD(&cap_q_ctx
->rdy_queue
);
557 INIT_LIST_HEAD(&m2m_ctx
->queue
);
559 vq_init(priv
, &out_q_ctx
->q
, V4L2_BUF_TYPE_VIDEO_OUTPUT
);
560 vq_init(priv
, &cap_q_ctx
->q
, V4L2_BUF_TYPE_VIDEO_CAPTURE
);
561 out_q_ctx
->q
.priv_data
= cap_q_ctx
->q
.priv_data
= priv
;
565 EXPORT_SYMBOL_GPL(v4l2_m2m_ctx_init
);
568 * v4l2_m2m_ctx_release() - release m2m context
570 * Usually called from driver's release() function.
572 void v4l2_m2m_ctx_release(struct v4l2_m2m_ctx
*m2m_ctx
)
574 struct v4l2_m2m_dev
*m2m_dev
;
575 struct videobuf_buffer
*vb
;
578 m2m_dev
= m2m_ctx
->m2m_dev
;
580 spin_lock_irqsave(&m2m_dev
->job_spinlock
, flags
);
581 if (m2m_ctx
->job_flags
& TRANS_RUNNING
) {
582 spin_unlock_irqrestore(&m2m_dev
->job_spinlock
, flags
);
583 m2m_dev
->m2m_ops
->job_abort(m2m_ctx
->priv
);
584 dprintk("m2m_ctx %p running, will wait to complete", m2m_ctx
);
585 vb
= v4l2_m2m_next_dst_buf(m2m_ctx
);
587 wait_event(vb
->done
, vb
->state
!= VIDEOBUF_ACTIVE
588 && vb
->state
!= VIDEOBUF_QUEUED
);
589 } else if (m2m_ctx
->job_flags
& TRANS_QUEUED
) {
590 list_del(&m2m_ctx
->queue
);
591 m2m_ctx
->job_flags
&= ~(TRANS_QUEUED
| TRANS_RUNNING
);
592 spin_unlock_irqrestore(&m2m_dev
->job_spinlock
, flags
);
593 dprintk("m2m_ctx: %p had been on queue and was removed\n",
596 /* Do nothing, was not on queue/running */
597 spin_unlock_irqrestore(&m2m_dev
->job_spinlock
, flags
);
600 videobuf_stop(&m2m_ctx
->cap_q_ctx
.q
);
601 videobuf_stop(&m2m_ctx
->out_q_ctx
.q
);
603 videobuf_mmap_free(&m2m_ctx
->cap_q_ctx
.q
);
604 videobuf_mmap_free(&m2m_ctx
->out_q_ctx
.q
);
608 EXPORT_SYMBOL_GPL(v4l2_m2m_ctx_release
);
611 * v4l2_m2m_buf_queue() - add a buffer to the proper ready buffers list.
613 * Call from buf_queue(), videobuf_queue_ops callback.
615 * Locking: Caller holds q->irqlock (taken by videobuf before calling buf_queue
616 * callback in the driver).
618 void v4l2_m2m_buf_queue(struct v4l2_m2m_ctx
*m2m_ctx
, struct videobuf_queue
*vq
,
619 struct videobuf_buffer
*vb
)
621 struct v4l2_m2m_queue_ctx
*q_ctx
;
623 q_ctx
= get_queue_ctx(m2m_ctx
, vq
->type
);
627 list_add_tail(&vb
->queue
, &q_ctx
->rdy_queue
);
630 vb
->state
= VIDEOBUF_QUEUED
;
632 EXPORT_SYMBOL_GPL(v4l2_m2m_buf_queue
);