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, <pawel@osciak.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/videobuf2-v4l2.h>
21 #include <media/v4l2-mem2mem.h>
22 #include <media/v4l2-dev.h>
23 #include <media/v4l2-fh.h>
24 #include <media/v4l2-event.h>
26 MODULE_DESCRIPTION("Mem to mem device framework for videobuf");
27 MODULE_AUTHOR("Pawel Osciak, <pawel@osciak.com>");
28 MODULE_LICENSE("GPL");
31 module_param(debug
, bool, 0644);
33 #define dprintk(fmt, arg...) \
36 printk(KERN_DEBUG "%s: " fmt, __func__, ## arg);\
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 /* Instance is currently aborting */
45 #define TRANS_ABORT (1 << 2)
48 /* Offset base for buffers on the destination queue - used to distinguish
49 * between source and destination buffers when mmapping - they receive the same
50 * offsets but for different queues */
51 #define DST_QUEUE_OFF_BASE (1 << 30)
55 * struct v4l2_m2m_dev - per-device context
56 * @curr_ctx: currently running instance
57 * @job_queue: instances queued to run
58 * @job_spinlock: protects job_queue
59 * @m2m_ops: driver callbacks
62 struct v4l2_m2m_ctx
*curr_ctx
;
64 struct list_head job_queue
;
65 spinlock_t job_spinlock
;
67 const struct v4l2_m2m_ops
*m2m_ops
;
70 static struct v4l2_m2m_queue_ctx
*get_queue_ctx(struct v4l2_m2m_ctx
*m2m_ctx
,
71 enum v4l2_buf_type type
)
73 if (V4L2_TYPE_IS_OUTPUT(type
))
74 return &m2m_ctx
->out_q_ctx
;
76 return &m2m_ctx
->cap_q_ctx
;
79 struct vb2_queue
*v4l2_m2m_get_vq(struct v4l2_m2m_ctx
*m2m_ctx
,
80 enum v4l2_buf_type type
)
82 struct v4l2_m2m_queue_ctx
*q_ctx
;
84 q_ctx
= get_queue_ctx(m2m_ctx
, type
);
90 EXPORT_SYMBOL(v4l2_m2m_get_vq
);
92 void *v4l2_m2m_next_buf(struct v4l2_m2m_queue_ctx
*q_ctx
)
94 struct v4l2_m2m_buffer
*b
;
97 spin_lock_irqsave(&q_ctx
->rdy_spinlock
, flags
);
99 if (list_empty(&q_ctx
->rdy_queue
)) {
100 spin_unlock_irqrestore(&q_ctx
->rdy_spinlock
, flags
);
104 b
= list_first_entry(&q_ctx
->rdy_queue
, struct v4l2_m2m_buffer
, list
);
105 spin_unlock_irqrestore(&q_ctx
->rdy_spinlock
, flags
);
108 EXPORT_SYMBOL_GPL(v4l2_m2m_next_buf
);
110 void *v4l2_m2m_buf_remove(struct v4l2_m2m_queue_ctx
*q_ctx
)
112 struct v4l2_m2m_buffer
*b
;
115 spin_lock_irqsave(&q_ctx
->rdy_spinlock
, flags
);
116 if (list_empty(&q_ctx
->rdy_queue
)) {
117 spin_unlock_irqrestore(&q_ctx
->rdy_spinlock
, flags
);
120 b
= list_first_entry(&q_ctx
->rdy_queue
, struct v4l2_m2m_buffer
, list
);
123 spin_unlock_irqrestore(&q_ctx
->rdy_spinlock
, flags
);
127 EXPORT_SYMBOL_GPL(v4l2_m2m_buf_remove
);
129 void v4l2_m2m_buf_remove_by_buf(struct v4l2_m2m_queue_ctx
*q_ctx
,
130 struct vb2_v4l2_buffer
*vbuf
)
132 struct v4l2_m2m_buffer
*b
;
135 spin_lock_irqsave(&q_ctx
->rdy_spinlock
, flags
);
136 b
= container_of(vbuf
, struct v4l2_m2m_buffer
, vb
);
139 spin_unlock_irqrestore(&q_ctx
->rdy_spinlock
, flags
);
141 EXPORT_SYMBOL_GPL(v4l2_m2m_buf_remove_by_buf
);
143 struct vb2_v4l2_buffer
*
144 v4l2_m2m_buf_remove_by_idx(struct v4l2_m2m_queue_ctx
*q_ctx
, unsigned int idx
)
147 struct v4l2_m2m_buffer
*b
, *tmp
;
148 struct vb2_v4l2_buffer
*ret
= NULL
;
151 spin_lock_irqsave(&q_ctx
->rdy_spinlock
, flags
);
152 list_for_each_entry_safe(b
, tmp
, &q_ctx
->rdy_queue
, list
) {
153 if (b
->vb
.vb2_buf
.index
== idx
) {
160 spin_unlock_irqrestore(&q_ctx
->rdy_spinlock
, flags
);
164 EXPORT_SYMBOL_GPL(v4l2_m2m_buf_remove_by_idx
);
167 * Scheduling handlers
170 void *v4l2_m2m_get_curr_priv(struct v4l2_m2m_dev
*m2m_dev
)
175 spin_lock_irqsave(&m2m_dev
->job_spinlock
, flags
);
176 if (m2m_dev
->curr_ctx
)
177 ret
= m2m_dev
->curr_ctx
->priv
;
178 spin_unlock_irqrestore(&m2m_dev
->job_spinlock
, flags
);
182 EXPORT_SYMBOL(v4l2_m2m_get_curr_priv
);
185 * v4l2_m2m_try_run() - select next job to perform and run it if possible
187 * Get next transaction (if present) from the waiting jobs list and run it.
189 static void v4l2_m2m_try_run(struct v4l2_m2m_dev
*m2m_dev
)
193 spin_lock_irqsave(&m2m_dev
->job_spinlock
, flags
);
194 if (NULL
!= m2m_dev
->curr_ctx
) {
195 spin_unlock_irqrestore(&m2m_dev
->job_spinlock
, flags
);
196 dprintk("Another instance is running, won't run now\n");
200 if (list_empty(&m2m_dev
->job_queue
)) {
201 spin_unlock_irqrestore(&m2m_dev
->job_spinlock
, flags
);
202 dprintk("No job pending\n");
206 m2m_dev
->curr_ctx
= list_first_entry(&m2m_dev
->job_queue
,
207 struct v4l2_m2m_ctx
, queue
);
208 m2m_dev
->curr_ctx
->job_flags
|= TRANS_RUNNING
;
209 spin_unlock_irqrestore(&m2m_dev
->job_spinlock
, flags
);
211 m2m_dev
->m2m_ops
->device_run(m2m_dev
->curr_ctx
->priv
);
214 void v4l2_m2m_try_schedule(struct v4l2_m2m_ctx
*m2m_ctx
)
216 struct v4l2_m2m_dev
*m2m_dev
;
217 unsigned long flags_job
, flags_out
, flags_cap
;
219 m2m_dev
= m2m_ctx
->m2m_dev
;
220 dprintk("Trying to schedule a job for m2m_ctx: %p\n", m2m_ctx
);
222 if (!m2m_ctx
->out_q_ctx
.q
.streaming
223 || !m2m_ctx
->cap_q_ctx
.q
.streaming
) {
224 dprintk("Streaming needs to be on for both queues\n");
228 spin_lock_irqsave(&m2m_dev
->job_spinlock
, flags_job
);
230 /* If the context is aborted then don't schedule it */
231 if (m2m_ctx
->job_flags
& TRANS_ABORT
) {
232 spin_unlock_irqrestore(&m2m_dev
->job_spinlock
, flags_job
);
233 dprintk("Aborted context\n");
237 if (m2m_ctx
->job_flags
& TRANS_QUEUED
) {
238 spin_unlock_irqrestore(&m2m_dev
->job_spinlock
, flags_job
);
239 dprintk("On job queue already\n");
243 spin_lock_irqsave(&m2m_ctx
->out_q_ctx
.rdy_spinlock
, flags_out
);
244 if (list_empty(&m2m_ctx
->out_q_ctx
.rdy_queue
)
245 && !m2m_ctx
->out_q_ctx
.buffered
) {
246 spin_unlock_irqrestore(&m2m_ctx
->out_q_ctx
.rdy_spinlock
,
248 spin_unlock_irqrestore(&m2m_dev
->job_spinlock
, flags_job
);
249 dprintk("No input buffers available\n");
252 spin_lock_irqsave(&m2m_ctx
->cap_q_ctx
.rdy_spinlock
, flags_cap
);
253 if (list_empty(&m2m_ctx
->cap_q_ctx
.rdy_queue
)
254 && !m2m_ctx
->cap_q_ctx
.buffered
) {
255 spin_unlock_irqrestore(&m2m_ctx
->cap_q_ctx
.rdy_spinlock
,
257 spin_unlock_irqrestore(&m2m_ctx
->out_q_ctx
.rdy_spinlock
,
259 spin_unlock_irqrestore(&m2m_dev
->job_spinlock
, flags_job
);
260 dprintk("No output buffers available\n");
263 spin_unlock_irqrestore(&m2m_ctx
->cap_q_ctx
.rdy_spinlock
, flags_cap
);
264 spin_unlock_irqrestore(&m2m_ctx
->out_q_ctx
.rdy_spinlock
, flags_out
);
266 if (m2m_dev
->m2m_ops
->job_ready
267 && (!m2m_dev
->m2m_ops
->job_ready(m2m_ctx
->priv
))) {
268 spin_unlock_irqrestore(&m2m_dev
->job_spinlock
, flags_job
);
269 dprintk("Driver not ready\n");
273 list_add_tail(&m2m_ctx
->queue
, &m2m_dev
->job_queue
);
274 m2m_ctx
->job_flags
|= TRANS_QUEUED
;
276 spin_unlock_irqrestore(&m2m_dev
->job_spinlock
, flags_job
);
278 v4l2_m2m_try_run(m2m_dev
);
280 EXPORT_SYMBOL_GPL(v4l2_m2m_try_schedule
);
283 * v4l2_m2m_cancel_job() - cancel pending jobs for the context
285 * In case of streamoff or release called on any context,
286 * 1] If the context is currently running, then abort job will be called
287 * 2] If the context is queued, then the context will be removed from
290 static void v4l2_m2m_cancel_job(struct v4l2_m2m_ctx
*m2m_ctx
)
292 struct v4l2_m2m_dev
*m2m_dev
;
295 m2m_dev
= m2m_ctx
->m2m_dev
;
296 spin_lock_irqsave(&m2m_dev
->job_spinlock
, flags
);
298 m2m_ctx
->job_flags
|= TRANS_ABORT
;
299 if (m2m_ctx
->job_flags
& TRANS_RUNNING
) {
300 spin_unlock_irqrestore(&m2m_dev
->job_spinlock
, flags
);
301 m2m_dev
->m2m_ops
->job_abort(m2m_ctx
->priv
);
302 dprintk("m2m_ctx %p running, will wait to complete", m2m_ctx
);
303 wait_event(m2m_ctx
->finished
,
304 !(m2m_ctx
->job_flags
& TRANS_RUNNING
));
305 } else if (m2m_ctx
->job_flags
& TRANS_QUEUED
) {
306 list_del(&m2m_ctx
->queue
);
307 m2m_ctx
->job_flags
&= ~(TRANS_QUEUED
| TRANS_RUNNING
);
308 spin_unlock_irqrestore(&m2m_dev
->job_spinlock
, flags
);
309 dprintk("m2m_ctx: %p had been on queue and was removed\n",
312 /* Do nothing, was not on queue/running */
313 spin_unlock_irqrestore(&m2m_dev
->job_spinlock
, flags
);
317 void v4l2_m2m_job_finish(struct v4l2_m2m_dev
*m2m_dev
,
318 struct v4l2_m2m_ctx
*m2m_ctx
)
322 spin_lock_irqsave(&m2m_dev
->job_spinlock
, flags
);
323 if (!m2m_dev
->curr_ctx
|| m2m_dev
->curr_ctx
!= m2m_ctx
) {
324 spin_unlock_irqrestore(&m2m_dev
->job_spinlock
, flags
);
325 dprintk("Called by an instance not currently running\n");
329 list_del(&m2m_dev
->curr_ctx
->queue
);
330 m2m_dev
->curr_ctx
->job_flags
&= ~(TRANS_QUEUED
| TRANS_RUNNING
);
331 wake_up(&m2m_dev
->curr_ctx
->finished
);
332 m2m_dev
->curr_ctx
= NULL
;
334 spin_unlock_irqrestore(&m2m_dev
->job_spinlock
, flags
);
336 /* This instance might have more buffers ready, but since we do not
337 * allow more than one job on the job_queue per instance, each has
338 * to be scheduled separately after the previous one finishes. */
339 v4l2_m2m_try_schedule(m2m_ctx
);
340 v4l2_m2m_try_run(m2m_dev
);
342 EXPORT_SYMBOL(v4l2_m2m_job_finish
);
344 int v4l2_m2m_reqbufs(struct file
*file
, struct v4l2_m2m_ctx
*m2m_ctx
,
345 struct v4l2_requestbuffers
*reqbufs
)
347 struct vb2_queue
*vq
;
350 vq
= v4l2_m2m_get_vq(m2m_ctx
, reqbufs
->type
);
351 ret
= vb2_reqbufs(vq
, reqbufs
);
352 /* If count == 0, then the owner has released all buffers and he
353 is no longer owner of the queue. Otherwise we have an owner. */
355 vq
->owner
= reqbufs
->count
? file
->private_data
: NULL
;
359 EXPORT_SYMBOL_GPL(v4l2_m2m_reqbufs
);
361 int v4l2_m2m_querybuf(struct file
*file
, struct v4l2_m2m_ctx
*m2m_ctx
,
362 struct v4l2_buffer
*buf
)
364 struct vb2_queue
*vq
;
368 vq
= v4l2_m2m_get_vq(m2m_ctx
, buf
->type
);
369 ret
= vb2_querybuf(vq
, buf
);
371 /* Adjust MMAP memory offsets for the CAPTURE queue */
372 if (buf
->memory
== V4L2_MEMORY_MMAP
&& !V4L2_TYPE_IS_OUTPUT(vq
->type
)) {
373 if (V4L2_TYPE_IS_MULTIPLANAR(vq
->type
)) {
374 for (i
= 0; i
< buf
->length
; ++i
)
375 buf
->m
.planes
[i
].m
.mem_offset
376 += DST_QUEUE_OFF_BASE
;
378 buf
->m
.offset
+= DST_QUEUE_OFF_BASE
;
384 EXPORT_SYMBOL_GPL(v4l2_m2m_querybuf
);
386 int v4l2_m2m_qbuf(struct file
*file
, struct v4l2_m2m_ctx
*m2m_ctx
,
387 struct v4l2_buffer
*buf
)
389 struct vb2_queue
*vq
;
392 vq
= v4l2_m2m_get_vq(m2m_ctx
, buf
->type
);
393 ret
= vb2_qbuf(vq
, buf
);
395 v4l2_m2m_try_schedule(m2m_ctx
);
399 EXPORT_SYMBOL_GPL(v4l2_m2m_qbuf
);
401 int v4l2_m2m_dqbuf(struct file
*file
, struct v4l2_m2m_ctx
*m2m_ctx
,
402 struct v4l2_buffer
*buf
)
404 struct vb2_queue
*vq
;
406 vq
= v4l2_m2m_get_vq(m2m_ctx
, buf
->type
);
407 return vb2_dqbuf(vq
, buf
, file
->f_flags
& O_NONBLOCK
);
409 EXPORT_SYMBOL_GPL(v4l2_m2m_dqbuf
);
411 int v4l2_m2m_prepare_buf(struct file
*file
, struct v4l2_m2m_ctx
*m2m_ctx
,
412 struct v4l2_buffer
*buf
)
414 struct vb2_queue
*vq
;
417 vq
= v4l2_m2m_get_vq(m2m_ctx
, buf
->type
);
418 ret
= vb2_prepare_buf(vq
, buf
);
420 v4l2_m2m_try_schedule(m2m_ctx
);
424 EXPORT_SYMBOL_GPL(v4l2_m2m_prepare_buf
);
426 int v4l2_m2m_create_bufs(struct file
*file
, struct v4l2_m2m_ctx
*m2m_ctx
,
427 struct v4l2_create_buffers
*create
)
429 struct vb2_queue
*vq
;
431 vq
= v4l2_m2m_get_vq(m2m_ctx
, create
->format
.type
);
432 return vb2_create_bufs(vq
, create
);
434 EXPORT_SYMBOL_GPL(v4l2_m2m_create_bufs
);
436 int v4l2_m2m_expbuf(struct file
*file
, struct v4l2_m2m_ctx
*m2m_ctx
,
437 struct v4l2_exportbuffer
*eb
)
439 struct vb2_queue
*vq
;
441 vq
= v4l2_m2m_get_vq(m2m_ctx
, eb
->type
);
442 return vb2_expbuf(vq
, eb
);
444 EXPORT_SYMBOL_GPL(v4l2_m2m_expbuf
);
446 int v4l2_m2m_streamon(struct file
*file
, struct v4l2_m2m_ctx
*m2m_ctx
,
447 enum v4l2_buf_type type
)
449 struct vb2_queue
*vq
;
452 vq
= v4l2_m2m_get_vq(m2m_ctx
, type
);
453 ret
= vb2_streamon(vq
, type
);
455 v4l2_m2m_try_schedule(m2m_ctx
);
459 EXPORT_SYMBOL_GPL(v4l2_m2m_streamon
);
461 int v4l2_m2m_streamoff(struct file
*file
, struct v4l2_m2m_ctx
*m2m_ctx
,
462 enum v4l2_buf_type type
)
464 struct v4l2_m2m_dev
*m2m_dev
;
465 struct v4l2_m2m_queue_ctx
*q_ctx
;
466 unsigned long flags_job
, flags
;
469 /* wait until the current context is dequeued from job_queue */
470 v4l2_m2m_cancel_job(m2m_ctx
);
472 q_ctx
= get_queue_ctx(m2m_ctx
, type
);
473 ret
= vb2_streamoff(&q_ctx
->q
, type
);
477 m2m_dev
= m2m_ctx
->m2m_dev
;
478 spin_lock_irqsave(&m2m_dev
->job_spinlock
, flags_job
);
479 /* We should not be scheduled anymore, since we're dropping a queue. */
480 if (m2m_ctx
->job_flags
& TRANS_QUEUED
)
481 list_del(&m2m_ctx
->queue
);
482 m2m_ctx
->job_flags
= 0;
484 spin_lock_irqsave(&q_ctx
->rdy_spinlock
, flags
);
485 /* Drop queue, since streamoff returns device to the same state as after
486 * calling reqbufs. */
487 INIT_LIST_HEAD(&q_ctx
->rdy_queue
);
489 spin_unlock_irqrestore(&q_ctx
->rdy_spinlock
, flags
);
491 if (m2m_dev
->curr_ctx
== m2m_ctx
) {
492 m2m_dev
->curr_ctx
= NULL
;
493 wake_up(&m2m_ctx
->finished
);
495 spin_unlock_irqrestore(&m2m_dev
->job_spinlock
, flags_job
);
499 EXPORT_SYMBOL_GPL(v4l2_m2m_streamoff
);
501 unsigned int v4l2_m2m_poll(struct file
*file
, struct v4l2_m2m_ctx
*m2m_ctx
,
502 struct poll_table_struct
*wait
)
504 struct video_device
*vfd
= video_devdata(file
);
505 unsigned long req_events
= poll_requested_events(wait
);
506 struct vb2_queue
*src_q
, *dst_q
;
507 struct vb2_buffer
*src_vb
= NULL
, *dst_vb
= NULL
;
511 if (test_bit(V4L2_FL_USES_V4L2_FH
, &vfd
->flags
)) {
512 struct v4l2_fh
*fh
= file
->private_data
;
514 if (v4l2_event_pending(fh
))
516 else if (req_events
& POLLPRI
)
517 poll_wait(file
, &fh
->wait
, wait
);
518 if (!(req_events
& (POLLOUT
| POLLWRNORM
| POLLIN
| POLLRDNORM
)))
522 src_q
= v4l2_m2m_get_src_vq(m2m_ctx
);
523 dst_q
= v4l2_m2m_get_dst_vq(m2m_ctx
);
526 * There has to be at least one buffer queued on each queued_list, which
527 * means either in driver already or waiting for driver to claim it
528 * and start processing.
530 if ((!src_q
->streaming
|| list_empty(&src_q
->queued_list
))
531 && (!dst_q
->streaming
|| list_empty(&dst_q
->queued_list
))) {
536 spin_lock_irqsave(&src_q
->done_lock
, flags
);
537 if (list_empty(&src_q
->done_list
))
538 poll_wait(file
, &src_q
->done_wq
, wait
);
539 spin_unlock_irqrestore(&src_q
->done_lock
, flags
);
541 spin_lock_irqsave(&dst_q
->done_lock
, flags
);
542 if (list_empty(&dst_q
->done_list
)) {
544 * If the last buffer was dequeued from the capture queue,
545 * return immediately. DQBUF will return -EPIPE.
547 if (dst_q
->last_buffer_dequeued
) {
548 spin_unlock_irqrestore(&dst_q
->done_lock
, flags
);
549 return rc
| POLLIN
| POLLRDNORM
;
552 poll_wait(file
, &dst_q
->done_wq
, wait
);
554 spin_unlock_irqrestore(&dst_q
->done_lock
, flags
);
556 spin_lock_irqsave(&src_q
->done_lock
, flags
);
557 if (!list_empty(&src_q
->done_list
))
558 src_vb
= list_first_entry(&src_q
->done_list
, struct vb2_buffer
,
560 if (src_vb
&& (src_vb
->state
== VB2_BUF_STATE_DONE
561 || src_vb
->state
== VB2_BUF_STATE_ERROR
))
562 rc
|= POLLOUT
| POLLWRNORM
;
563 spin_unlock_irqrestore(&src_q
->done_lock
, flags
);
565 spin_lock_irqsave(&dst_q
->done_lock
, flags
);
566 if (!list_empty(&dst_q
->done_list
))
567 dst_vb
= list_first_entry(&dst_q
->done_list
, struct vb2_buffer
,
569 if (dst_vb
&& (dst_vb
->state
== VB2_BUF_STATE_DONE
570 || dst_vb
->state
== VB2_BUF_STATE_ERROR
))
571 rc
|= POLLIN
| POLLRDNORM
;
572 spin_unlock_irqrestore(&dst_q
->done_lock
, flags
);
577 EXPORT_SYMBOL_GPL(v4l2_m2m_poll
);
579 int v4l2_m2m_mmap(struct file
*file
, struct v4l2_m2m_ctx
*m2m_ctx
,
580 struct vm_area_struct
*vma
)
582 unsigned long offset
= vma
->vm_pgoff
<< PAGE_SHIFT
;
583 struct vb2_queue
*vq
;
585 if (offset
< DST_QUEUE_OFF_BASE
) {
586 vq
= v4l2_m2m_get_src_vq(m2m_ctx
);
588 vq
= v4l2_m2m_get_dst_vq(m2m_ctx
);
589 vma
->vm_pgoff
-= (DST_QUEUE_OFF_BASE
>> PAGE_SHIFT
);
592 return vb2_mmap(vq
, vma
);
594 EXPORT_SYMBOL(v4l2_m2m_mmap
);
596 struct v4l2_m2m_dev
*v4l2_m2m_init(const struct v4l2_m2m_ops
*m2m_ops
)
598 struct v4l2_m2m_dev
*m2m_dev
;
600 if (!m2m_ops
|| WARN_ON(!m2m_ops
->device_run
) ||
601 WARN_ON(!m2m_ops
->job_abort
))
602 return ERR_PTR(-EINVAL
);
604 m2m_dev
= kzalloc(sizeof *m2m_dev
, GFP_KERNEL
);
606 return ERR_PTR(-ENOMEM
);
608 m2m_dev
->curr_ctx
= NULL
;
609 m2m_dev
->m2m_ops
= m2m_ops
;
610 INIT_LIST_HEAD(&m2m_dev
->job_queue
);
611 spin_lock_init(&m2m_dev
->job_spinlock
);
615 EXPORT_SYMBOL_GPL(v4l2_m2m_init
);
617 void v4l2_m2m_release(struct v4l2_m2m_dev
*m2m_dev
)
621 EXPORT_SYMBOL_GPL(v4l2_m2m_release
);
623 struct v4l2_m2m_ctx
*v4l2_m2m_ctx_init(struct v4l2_m2m_dev
*m2m_dev
,
625 int (*queue_init
)(void *priv
, struct vb2_queue
*src_vq
, struct vb2_queue
*dst_vq
))
627 struct v4l2_m2m_ctx
*m2m_ctx
;
628 struct v4l2_m2m_queue_ctx
*out_q_ctx
, *cap_q_ctx
;
631 m2m_ctx
= kzalloc(sizeof *m2m_ctx
, GFP_KERNEL
);
633 return ERR_PTR(-ENOMEM
);
635 m2m_ctx
->priv
= drv_priv
;
636 m2m_ctx
->m2m_dev
= m2m_dev
;
637 init_waitqueue_head(&m2m_ctx
->finished
);
639 out_q_ctx
= &m2m_ctx
->out_q_ctx
;
640 cap_q_ctx
= &m2m_ctx
->cap_q_ctx
;
642 INIT_LIST_HEAD(&out_q_ctx
->rdy_queue
);
643 INIT_LIST_HEAD(&cap_q_ctx
->rdy_queue
);
644 spin_lock_init(&out_q_ctx
->rdy_spinlock
);
645 spin_lock_init(&cap_q_ctx
->rdy_spinlock
);
647 INIT_LIST_HEAD(&m2m_ctx
->queue
);
649 ret
= queue_init(drv_priv
, &out_q_ctx
->q
, &cap_q_ctx
->q
);
654 * If both queues use same mutex assign it as the common buffer
655 * queues lock to the m2m context. This lock is used in the
656 * v4l2_m2m_ioctl_* helpers.
658 if (out_q_ctx
->q
.lock
== cap_q_ctx
->q
.lock
)
659 m2m_ctx
->q_lock
= out_q_ctx
->q
.lock
;
666 EXPORT_SYMBOL_GPL(v4l2_m2m_ctx_init
);
668 void v4l2_m2m_ctx_release(struct v4l2_m2m_ctx
*m2m_ctx
)
670 /* wait until the current context is dequeued from job_queue */
671 v4l2_m2m_cancel_job(m2m_ctx
);
673 vb2_queue_release(&m2m_ctx
->cap_q_ctx
.q
);
674 vb2_queue_release(&m2m_ctx
->out_q_ctx
.q
);
678 EXPORT_SYMBOL_GPL(v4l2_m2m_ctx_release
);
680 void v4l2_m2m_buf_queue(struct v4l2_m2m_ctx
*m2m_ctx
,
681 struct vb2_v4l2_buffer
*vbuf
)
683 struct v4l2_m2m_buffer
*b
= container_of(vbuf
,
684 struct v4l2_m2m_buffer
, vb
);
685 struct v4l2_m2m_queue_ctx
*q_ctx
;
688 q_ctx
= get_queue_ctx(m2m_ctx
, vbuf
->vb2_buf
.vb2_queue
->type
);
692 spin_lock_irqsave(&q_ctx
->rdy_spinlock
, flags
);
693 list_add_tail(&b
->list
, &q_ctx
->rdy_queue
);
695 spin_unlock_irqrestore(&q_ctx
->rdy_spinlock
, flags
);
697 EXPORT_SYMBOL_GPL(v4l2_m2m_buf_queue
);
699 /* Videobuf2 ioctl helpers */
701 int v4l2_m2m_ioctl_reqbufs(struct file
*file
, void *priv
,
702 struct v4l2_requestbuffers
*rb
)
704 struct v4l2_fh
*fh
= file
->private_data
;
706 return v4l2_m2m_reqbufs(file
, fh
->m2m_ctx
, rb
);
708 EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_reqbufs
);
710 int v4l2_m2m_ioctl_create_bufs(struct file
*file
, void *priv
,
711 struct v4l2_create_buffers
*create
)
713 struct v4l2_fh
*fh
= file
->private_data
;
715 return v4l2_m2m_create_bufs(file
, fh
->m2m_ctx
, create
);
717 EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_create_bufs
);
719 int v4l2_m2m_ioctl_querybuf(struct file
*file
, void *priv
,
720 struct v4l2_buffer
*buf
)
722 struct v4l2_fh
*fh
= file
->private_data
;
724 return v4l2_m2m_querybuf(file
, fh
->m2m_ctx
, buf
);
726 EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_querybuf
);
728 int v4l2_m2m_ioctl_qbuf(struct file
*file
, void *priv
,
729 struct v4l2_buffer
*buf
)
731 struct v4l2_fh
*fh
= file
->private_data
;
733 return v4l2_m2m_qbuf(file
, fh
->m2m_ctx
, buf
);
735 EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_qbuf
);
737 int v4l2_m2m_ioctl_dqbuf(struct file
*file
, void *priv
,
738 struct v4l2_buffer
*buf
)
740 struct v4l2_fh
*fh
= file
->private_data
;
742 return v4l2_m2m_dqbuf(file
, fh
->m2m_ctx
, buf
);
744 EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_dqbuf
);
746 int v4l2_m2m_ioctl_prepare_buf(struct file
*file
, void *priv
,
747 struct v4l2_buffer
*buf
)
749 struct v4l2_fh
*fh
= file
->private_data
;
751 return v4l2_m2m_prepare_buf(file
, fh
->m2m_ctx
, buf
);
753 EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_prepare_buf
);
755 int v4l2_m2m_ioctl_expbuf(struct file
*file
, void *priv
,
756 struct v4l2_exportbuffer
*eb
)
758 struct v4l2_fh
*fh
= file
->private_data
;
760 return v4l2_m2m_expbuf(file
, fh
->m2m_ctx
, eb
);
762 EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_expbuf
);
764 int v4l2_m2m_ioctl_streamon(struct file
*file
, void *priv
,
765 enum v4l2_buf_type type
)
767 struct v4l2_fh
*fh
= file
->private_data
;
769 return v4l2_m2m_streamon(file
, fh
->m2m_ctx
, type
);
771 EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_streamon
);
773 int v4l2_m2m_ioctl_streamoff(struct file
*file
, void *priv
,
774 enum v4l2_buf_type type
)
776 struct v4l2_fh
*fh
= file
->private_data
;
778 return v4l2_m2m_streamoff(file
, fh
->m2m_ctx
, type
);
780 EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_streamoff
);
783 * v4l2_file_operations helpers. It is assumed here same lock is used
784 * for the output and the capture buffer queue.
787 int v4l2_m2m_fop_mmap(struct file
*file
, struct vm_area_struct
*vma
)
789 struct v4l2_fh
*fh
= file
->private_data
;
791 return v4l2_m2m_mmap(file
, fh
->m2m_ctx
, vma
);
793 EXPORT_SYMBOL_GPL(v4l2_m2m_fop_mmap
);
795 unsigned int v4l2_m2m_fop_poll(struct file
*file
, poll_table
*wait
)
797 struct v4l2_fh
*fh
= file
->private_data
;
798 struct v4l2_m2m_ctx
*m2m_ctx
= fh
->m2m_ctx
;
802 mutex_lock(m2m_ctx
->q_lock
);
804 ret
= v4l2_m2m_poll(file
, m2m_ctx
, wait
);
807 mutex_unlock(m2m_ctx
->q_lock
);
811 EXPORT_SYMBOL_GPL(v4l2_m2m_fop_poll
);