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/media-device.h>
21 #include <media/videobuf2-v4l2.h>
22 #include <media/v4l2-mem2mem.h>
23 #include <media/v4l2-dev.h>
24 #include <media/v4l2-device.h>
25 #include <media/v4l2-fh.h>
26 #include <media/v4l2-event.h>
28 MODULE_DESCRIPTION("Mem to mem device framework for videobuf");
29 MODULE_AUTHOR("Pawel Osciak, <pawel@osciak.com>");
30 MODULE_LICENSE("GPL");
33 module_param(debug
, bool, 0644);
35 #define dprintk(fmt, arg...) \
38 printk(KERN_DEBUG "%s: " fmt, __func__, ## arg);\
42 /* Instance is already queued on the job_queue */
43 #define TRANS_QUEUED (1 << 0)
44 /* Instance is currently running in hardware */
45 #define TRANS_RUNNING (1 << 1)
46 /* Instance is currently aborting */
47 #define TRANS_ABORT (1 << 2)
50 /* Offset base for buffers on the destination queue - used to distinguish
51 * between source and destination buffers when mmapping - they receive the same
52 * offsets but for different queues */
53 #define DST_QUEUE_OFF_BASE (1 << 30)
55 enum v4l2_m2m_entity_type
{
56 MEM2MEM_ENT_TYPE_SOURCE
,
57 MEM2MEM_ENT_TYPE_SINK
,
61 static const char * const m2m_entity_name
[] = {
68 * struct v4l2_m2m_dev - per-device context
69 * @source: &struct media_entity pointer with the source entity
70 * Used only when the M2M device is registered via
71 * v4l2_m2m_unregister_media_controller().
72 * @source_pad: &struct media_pad with the source pad.
73 * Used only when the M2M device is registered via
74 * v4l2_m2m_unregister_media_controller().
75 * @sink: &struct media_entity pointer with the sink entity
76 * Used only when the M2M device is registered via
77 * v4l2_m2m_unregister_media_controller().
78 * @sink_pad: &struct media_pad with the sink pad.
79 * Used only when the M2M device is registered via
80 * v4l2_m2m_unregister_media_controller().
81 * @proc: &struct media_entity pointer with the M2M device itself.
82 * @proc_pads: &struct media_pad with the @proc pads.
83 * Used only when the M2M device is registered via
84 * v4l2_m2m_unregister_media_controller().
85 * @intf_devnode: &struct media_intf devnode pointer with the interface
86 * with controls the M2M device.
87 * @curr_ctx: currently running instance
88 * @job_queue: instances queued to run
89 * @job_spinlock: protects job_queue
90 * @m2m_ops: driver callbacks
93 struct v4l2_m2m_ctx
*curr_ctx
;
94 #ifdef CONFIG_MEDIA_CONTROLLER
95 struct media_entity
*source
;
96 struct media_pad source_pad
;
97 struct media_entity sink
;
98 struct media_pad sink_pad
;
99 struct media_entity proc
;
100 struct media_pad proc_pads
[2];
101 struct media_intf_devnode
*intf_devnode
;
104 struct list_head job_queue
;
105 spinlock_t job_spinlock
;
107 const struct v4l2_m2m_ops
*m2m_ops
;
110 static struct v4l2_m2m_queue_ctx
*get_queue_ctx(struct v4l2_m2m_ctx
*m2m_ctx
,
111 enum v4l2_buf_type type
)
113 if (V4L2_TYPE_IS_OUTPUT(type
))
114 return &m2m_ctx
->out_q_ctx
;
116 return &m2m_ctx
->cap_q_ctx
;
119 struct vb2_queue
*v4l2_m2m_get_vq(struct v4l2_m2m_ctx
*m2m_ctx
,
120 enum v4l2_buf_type type
)
122 struct v4l2_m2m_queue_ctx
*q_ctx
;
124 q_ctx
= get_queue_ctx(m2m_ctx
, type
);
130 EXPORT_SYMBOL(v4l2_m2m_get_vq
);
132 void *v4l2_m2m_next_buf(struct v4l2_m2m_queue_ctx
*q_ctx
)
134 struct v4l2_m2m_buffer
*b
;
137 spin_lock_irqsave(&q_ctx
->rdy_spinlock
, flags
);
139 if (list_empty(&q_ctx
->rdy_queue
)) {
140 spin_unlock_irqrestore(&q_ctx
->rdy_spinlock
, flags
);
144 b
= list_first_entry(&q_ctx
->rdy_queue
, struct v4l2_m2m_buffer
, list
);
145 spin_unlock_irqrestore(&q_ctx
->rdy_spinlock
, flags
);
148 EXPORT_SYMBOL_GPL(v4l2_m2m_next_buf
);
150 void *v4l2_m2m_last_buf(struct v4l2_m2m_queue_ctx
*q_ctx
)
152 struct v4l2_m2m_buffer
*b
;
155 spin_lock_irqsave(&q_ctx
->rdy_spinlock
, flags
);
157 if (list_empty(&q_ctx
->rdy_queue
)) {
158 spin_unlock_irqrestore(&q_ctx
->rdy_spinlock
, flags
);
162 b
= list_last_entry(&q_ctx
->rdy_queue
, struct v4l2_m2m_buffer
, list
);
163 spin_unlock_irqrestore(&q_ctx
->rdy_spinlock
, flags
);
166 EXPORT_SYMBOL_GPL(v4l2_m2m_last_buf
);
168 void *v4l2_m2m_buf_remove(struct v4l2_m2m_queue_ctx
*q_ctx
)
170 struct v4l2_m2m_buffer
*b
;
173 spin_lock_irqsave(&q_ctx
->rdy_spinlock
, flags
);
174 if (list_empty(&q_ctx
->rdy_queue
)) {
175 spin_unlock_irqrestore(&q_ctx
->rdy_spinlock
, flags
);
178 b
= list_first_entry(&q_ctx
->rdy_queue
, struct v4l2_m2m_buffer
, list
);
181 spin_unlock_irqrestore(&q_ctx
->rdy_spinlock
, flags
);
185 EXPORT_SYMBOL_GPL(v4l2_m2m_buf_remove
);
187 void v4l2_m2m_buf_remove_by_buf(struct v4l2_m2m_queue_ctx
*q_ctx
,
188 struct vb2_v4l2_buffer
*vbuf
)
190 struct v4l2_m2m_buffer
*b
;
193 spin_lock_irqsave(&q_ctx
->rdy_spinlock
, flags
);
194 b
= container_of(vbuf
, struct v4l2_m2m_buffer
, vb
);
197 spin_unlock_irqrestore(&q_ctx
->rdy_spinlock
, flags
);
199 EXPORT_SYMBOL_GPL(v4l2_m2m_buf_remove_by_buf
);
201 struct vb2_v4l2_buffer
*
202 v4l2_m2m_buf_remove_by_idx(struct v4l2_m2m_queue_ctx
*q_ctx
, unsigned int idx
)
205 struct v4l2_m2m_buffer
*b
, *tmp
;
206 struct vb2_v4l2_buffer
*ret
= NULL
;
209 spin_lock_irqsave(&q_ctx
->rdy_spinlock
, flags
);
210 list_for_each_entry_safe(b
, tmp
, &q_ctx
->rdy_queue
, list
) {
211 if (b
->vb
.vb2_buf
.index
== idx
) {
218 spin_unlock_irqrestore(&q_ctx
->rdy_spinlock
, flags
);
222 EXPORT_SYMBOL_GPL(v4l2_m2m_buf_remove_by_idx
);
225 * Scheduling handlers
228 void *v4l2_m2m_get_curr_priv(struct v4l2_m2m_dev
*m2m_dev
)
233 spin_lock_irqsave(&m2m_dev
->job_spinlock
, flags
);
234 if (m2m_dev
->curr_ctx
)
235 ret
= m2m_dev
->curr_ctx
->priv
;
236 spin_unlock_irqrestore(&m2m_dev
->job_spinlock
, flags
);
240 EXPORT_SYMBOL(v4l2_m2m_get_curr_priv
);
243 * v4l2_m2m_try_run() - select next job to perform and run it if possible
244 * @m2m_dev: per-device context
246 * Get next transaction (if present) from the waiting jobs list and run it.
248 static void v4l2_m2m_try_run(struct v4l2_m2m_dev
*m2m_dev
)
252 spin_lock_irqsave(&m2m_dev
->job_spinlock
, flags
);
253 if (NULL
!= m2m_dev
->curr_ctx
) {
254 spin_unlock_irqrestore(&m2m_dev
->job_spinlock
, flags
);
255 dprintk("Another instance is running, won't run now\n");
259 if (list_empty(&m2m_dev
->job_queue
)) {
260 spin_unlock_irqrestore(&m2m_dev
->job_spinlock
, flags
);
261 dprintk("No job pending\n");
265 m2m_dev
->curr_ctx
= list_first_entry(&m2m_dev
->job_queue
,
266 struct v4l2_m2m_ctx
, queue
);
267 m2m_dev
->curr_ctx
->job_flags
|= TRANS_RUNNING
;
268 spin_unlock_irqrestore(&m2m_dev
->job_spinlock
, flags
);
270 dprintk("Running job on m2m_ctx: %p\n", m2m_dev
->curr_ctx
);
271 m2m_dev
->m2m_ops
->device_run(m2m_dev
->curr_ctx
->priv
);
275 * __v4l2_m2m_try_queue() - queue a job
276 * @m2m_dev: m2m device
277 * @m2m_ctx: m2m context
279 * Check if this context is ready to queue a job.
281 * This function can run in interrupt context.
283 static void __v4l2_m2m_try_queue(struct v4l2_m2m_dev
*m2m_dev
,
284 struct v4l2_m2m_ctx
*m2m_ctx
)
286 unsigned long flags_job
, flags_out
, flags_cap
;
288 dprintk("Trying to schedule a job for m2m_ctx: %p\n", m2m_ctx
);
290 if (!m2m_ctx
->out_q_ctx
.q
.streaming
291 || !m2m_ctx
->cap_q_ctx
.q
.streaming
) {
292 dprintk("Streaming needs to be on for both queues\n");
296 spin_lock_irqsave(&m2m_dev
->job_spinlock
, flags_job
);
298 /* If the context is aborted then don't schedule it */
299 if (m2m_ctx
->job_flags
& TRANS_ABORT
) {
300 spin_unlock_irqrestore(&m2m_dev
->job_spinlock
, flags_job
);
301 dprintk("Aborted context\n");
305 if (m2m_ctx
->job_flags
& TRANS_QUEUED
) {
306 spin_unlock_irqrestore(&m2m_dev
->job_spinlock
, flags_job
);
307 dprintk("On job queue already\n");
311 spin_lock_irqsave(&m2m_ctx
->out_q_ctx
.rdy_spinlock
, flags_out
);
312 if (list_empty(&m2m_ctx
->out_q_ctx
.rdy_queue
)
313 && !m2m_ctx
->out_q_ctx
.buffered
) {
314 spin_unlock_irqrestore(&m2m_ctx
->out_q_ctx
.rdy_spinlock
,
316 spin_unlock_irqrestore(&m2m_dev
->job_spinlock
, flags_job
);
317 dprintk("No input buffers available\n");
320 spin_lock_irqsave(&m2m_ctx
->cap_q_ctx
.rdy_spinlock
, flags_cap
);
321 if (list_empty(&m2m_ctx
->cap_q_ctx
.rdy_queue
)
322 && !m2m_ctx
->cap_q_ctx
.buffered
) {
323 spin_unlock_irqrestore(&m2m_ctx
->cap_q_ctx
.rdy_spinlock
,
325 spin_unlock_irqrestore(&m2m_ctx
->out_q_ctx
.rdy_spinlock
,
327 spin_unlock_irqrestore(&m2m_dev
->job_spinlock
, flags_job
);
328 dprintk("No output buffers available\n");
331 spin_unlock_irqrestore(&m2m_ctx
->cap_q_ctx
.rdy_spinlock
, flags_cap
);
332 spin_unlock_irqrestore(&m2m_ctx
->out_q_ctx
.rdy_spinlock
, flags_out
);
334 if (m2m_dev
->m2m_ops
->job_ready
335 && (!m2m_dev
->m2m_ops
->job_ready(m2m_ctx
->priv
))) {
336 spin_unlock_irqrestore(&m2m_dev
->job_spinlock
, flags_job
);
337 dprintk("Driver not ready\n");
341 list_add_tail(&m2m_ctx
->queue
, &m2m_dev
->job_queue
);
342 m2m_ctx
->job_flags
|= TRANS_QUEUED
;
344 spin_unlock_irqrestore(&m2m_dev
->job_spinlock
, flags_job
);
348 * v4l2_m2m_try_schedule() - schedule and possibly run a job for any context
349 * @m2m_ctx: m2m context
351 * Check if this context is ready to queue a job. If suitable,
352 * run the next queued job on the mem2mem device.
354 * This function shouldn't run in interrupt context.
356 * Note that v4l2_m2m_try_schedule() can schedule one job for this context,
357 * and then run another job for another context.
359 void v4l2_m2m_try_schedule(struct v4l2_m2m_ctx
*m2m_ctx
)
361 struct v4l2_m2m_dev
*m2m_dev
= m2m_ctx
->m2m_dev
;
363 __v4l2_m2m_try_queue(m2m_dev
, m2m_ctx
);
364 v4l2_m2m_try_run(m2m_dev
);
366 EXPORT_SYMBOL_GPL(v4l2_m2m_try_schedule
);
369 * v4l2_m2m_cancel_job() - cancel pending jobs for the context
370 * @m2m_ctx: m2m context with jobs to be canceled
372 * In case of streamoff or release called on any context,
373 * 1] If the context is currently running, then abort job will be called
374 * 2] If the context is queued, then the context will be removed from
377 static void v4l2_m2m_cancel_job(struct v4l2_m2m_ctx
*m2m_ctx
)
379 struct v4l2_m2m_dev
*m2m_dev
;
382 m2m_dev
= m2m_ctx
->m2m_dev
;
383 spin_lock_irqsave(&m2m_dev
->job_spinlock
, flags
);
385 m2m_ctx
->job_flags
|= TRANS_ABORT
;
386 if (m2m_ctx
->job_flags
& TRANS_RUNNING
) {
387 spin_unlock_irqrestore(&m2m_dev
->job_spinlock
, flags
);
388 if (m2m_dev
->m2m_ops
->job_abort
)
389 m2m_dev
->m2m_ops
->job_abort(m2m_ctx
->priv
);
390 dprintk("m2m_ctx %p running, will wait to complete", m2m_ctx
);
391 wait_event(m2m_ctx
->finished
,
392 !(m2m_ctx
->job_flags
& TRANS_RUNNING
));
393 } else if (m2m_ctx
->job_flags
& TRANS_QUEUED
) {
394 list_del(&m2m_ctx
->queue
);
395 m2m_ctx
->job_flags
&= ~(TRANS_QUEUED
| TRANS_RUNNING
);
396 spin_unlock_irqrestore(&m2m_dev
->job_spinlock
, flags
);
397 dprintk("m2m_ctx: %p had been on queue and was removed\n",
400 /* Do nothing, was not on queue/running */
401 spin_unlock_irqrestore(&m2m_dev
->job_spinlock
, flags
);
405 void v4l2_m2m_job_finish(struct v4l2_m2m_dev
*m2m_dev
,
406 struct v4l2_m2m_ctx
*m2m_ctx
)
410 spin_lock_irqsave(&m2m_dev
->job_spinlock
, flags
);
411 if (!m2m_dev
->curr_ctx
|| m2m_dev
->curr_ctx
!= m2m_ctx
) {
412 spin_unlock_irqrestore(&m2m_dev
->job_spinlock
, flags
);
413 dprintk("Called by an instance not currently running\n");
417 list_del(&m2m_dev
->curr_ctx
->queue
);
418 m2m_dev
->curr_ctx
->job_flags
&= ~(TRANS_QUEUED
| TRANS_RUNNING
);
419 wake_up(&m2m_dev
->curr_ctx
->finished
);
420 m2m_dev
->curr_ctx
= NULL
;
422 spin_unlock_irqrestore(&m2m_dev
->job_spinlock
, flags
);
424 /* This instance might have more buffers ready, but since we do not
425 * allow more than one job on the job_queue per instance, each has
426 * to be scheduled separately after the previous one finishes. */
427 v4l2_m2m_try_schedule(m2m_ctx
);
429 EXPORT_SYMBOL(v4l2_m2m_job_finish
);
431 int v4l2_m2m_reqbufs(struct file
*file
, struct v4l2_m2m_ctx
*m2m_ctx
,
432 struct v4l2_requestbuffers
*reqbufs
)
434 struct vb2_queue
*vq
;
437 vq
= v4l2_m2m_get_vq(m2m_ctx
, reqbufs
->type
);
438 ret
= vb2_reqbufs(vq
, reqbufs
);
439 /* If count == 0, then the owner has released all buffers and he
440 is no longer owner of the queue. Otherwise we have an owner. */
442 vq
->owner
= reqbufs
->count
? file
->private_data
: NULL
;
446 EXPORT_SYMBOL_GPL(v4l2_m2m_reqbufs
);
448 int v4l2_m2m_querybuf(struct file
*file
, struct v4l2_m2m_ctx
*m2m_ctx
,
449 struct v4l2_buffer
*buf
)
451 struct vb2_queue
*vq
;
455 vq
= v4l2_m2m_get_vq(m2m_ctx
, buf
->type
);
456 ret
= vb2_querybuf(vq
, buf
);
458 /* Adjust MMAP memory offsets for the CAPTURE queue */
459 if (buf
->memory
== V4L2_MEMORY_MMAP
&& !V4L2_TYPE_IS_OUTPUT(vq
->type
)) {
460 if (V4L2_TYPE_IS_MULTIPLANAR(vq
->type
)) {
461 for (i
= 0; i
< buf
->length
; ++i
)
462 buf
->m
.planes
[i
].m
.mem_offset
463 += DST_QUEUE_OFF_BASE
;
465 buf
->m
.offset
+= DST_QUEUE_OFF_BASE
;
471 EXPORT_SYMBOL_GPL(v4l2_m2m_querybuf
);
473 int v4l2_m2m_qbuf(struct file
*file
, struct v4l2_m2m_ctx
*m2m_ctx
,
474 struct v4l2_buffer
*buf
)
476 struct vb2_queue
*vq
;
479 vq
= v4l2_m2m_get_vq(m2m_ctx
, buf
->type
);
480 ret
= vb2_qbuf(vq
, buf
);
482 v4l2_m2m_try_schedule(m2m_ctx
);
486 EXPORT_SYMBOL_GPL(v4l2_m2m_qbuf
);
488 int v4l2_m2m_dqbuf(struct file
*file
, struct v4l2_m2m_ctx
*m2m_ctx
,
489 struct v4l2_buffer
*buf
)
491 struct vb2_queue
*vq
;
493 vq
= v4l2_m2m_get_vq(m2m_ctx
, buf
->type
);
494 return vb2_dqbuf(vq
, buf
, file
->f_flags
& O_NONBLOCK
);
496 EXPORT_SYMBOL_GPL(v4l2_m2m_dqbuf
);
498 int v4l2_m2m_prepare_buf(struct file
*file
, struct v4l2_m2m_ctx
*m2m_ctx
,
499 struct v4l2_buffer
*buf
)
501 struct vb2_queue
*vq
;
504 vq
= v4l2_m2m_get_vq(m2m_ctx
, buf
->type
);
505 ret
= vb2_prepare_buf(vq
, buf
);
507 v4l2_m2m_try_schedule(m2m_ctx
);
511 EXPORT_SYMBOL_GPL(v4l2_m2m_prepare_buf
);
513 int v4l2_m2m_create_bufs(struct file
*file
, struct v4l2_m2m_ctx
*m2m_ctx
,
514 struct v4l2_create_buffers
*create
)
516 struct vb2_queue
*vq
;
518 vq
= v4l2_m2m_get_vq(m2m_ctx
, create
->format
.type
);
519 return vb2_create_bufs(vq
, create
);
521 EXPORT_SYMBOL_GPL(v4l2_m2m_create_bufs
);
523 int v4l2_m2m_expbuf(struct file
*file
, struct v4l2_m2m_ctx
*m2m_ctx
,
524 struct v4l2_exportbuffer
*eb
)
526 struct vb2_queue
*vq
;
528 vq
= v4l2_m2m_get_vq(m2m_ctx
, eb
->type
);
529 return vb2_expbuf(vq
, eb
);
531 EXPORT_SYMBOL_GPL(v4l2_m2m_expbuf
);
533 int v4l2_m2m_streamon(struct file
*file
, struct v4l2_m2m_ctx
*m2m_ctx
,
534 enum v4l2_buf_type type
)
536 struct vb2_queue
*vq
;
539 vq
= v4l2_m2m_get_vq(m2m_ctx
, type
);
540 ret
= vb2_streamon(vq
, type
);
542 v4l2_m2m_try_schedule(m2m_ctx
);
546 EXPORT_SYMBOL_GPL(v4l2_m2m_streamon
);
548 int v4l2_m2m_streamoff(struct file
*file
, struct v4l2_m2m_ctx
*m2m_ctx
,
549 enum v4l2_buf_type type
)
551 struct v4l2_m2m_dev
*m2m_dev
;
552 struct v4l2_m2m_queue_ctx
*q_ctx
;
553 unsigned long flags_job
, flags
;
556 /* wait until the current context is dequeued from job_queue */
557 v4l2_m2m_cancel_job(m2m_ctx
);
559 q_ctx
= get_queue_ctx(m2m_ctx
, type
);
560 ret
= vb2_streamoff(&q_ctx
->q
, type
);
564 m2m_dev
= m2m_ctx
->m2m_dev
;
565 spin_lock_irqsave(&m2m_dev
->job_spinlock
, flags_job
);
566 /* We should not be scheduled anymore, since we're dropping a queue. */
567 if (m2m_ctx
->job_flags
& TRANS_QUEUED
)
568 list_del(&m2m_ctx
->queue
);
569 m2m_ctx
->job_flags
= 0;
571 spin_lock_irqsave(&q_ctx
->rdy_spinlock
, flags
);
572 /* Drop queue, since streamoff returns device to the same state as after
573 * calling reqbufs. */
574 INIT_LIST_HEAD(&q_ctx
->rdy_queue
);
576 spin_unlock_irqrestore(&q_ctx
->rdy_spinlock
, flags
);
578 if (m2m_dev
->curr_ctx
== m2m_ctx
) {
579 m2m_dev
->curr_ctx
= NULL
;
580 wake_up(&m2m_ctx
->finished
);
582 spin_unlock_irqrestore(&m2m_dev
->job_spinlock
, flags_job
);
586 EXPORT_SYMBOL_GPL(v4l2_m2m_streamoff
);
588 __poll_t
v4l2_m2m_poll(struct file
*file
, struct v4l2_m2m_ctx
*m2m_ctx
,
589 struct poll_table_struct
*wait
)
591 struct video_device
*vfd
= video_devdata(file
);
592 __poll_t req_events
= poll_requested_events(wait
);
593 struct vb2_queue
*src_q
, *dst_q
;
594 struct vb2_buffer
*src_vb
= NULL
, *dst_vb
= NULL
;
598 if (test_bit(V4L2_FL_USES_V4L2_FH
, &vfd
->flags
)) {
599 struct v4l2_fh
*fh
= file
->private_data
;
601 if (v4l2_event_pending(fh
))
603 else if (req_events
& EPOLLPRI
)
604 poll_wait(file
, &fh
->wait
, wait
);
605 if (!(req_events
& (EPOLLOUT
| EPOLLWRNORM
| EPOLLIN
| EPOLLRDNORM
)))
609 src_q
= v4l2_m2m_get_src_vq(m2m_ctx
);
610 dst_q
= v4l2_m2m_get_dst_vq(m2m_ctx
);
613 * There has to be at least one buffer queued on each queued_list, which
614 * means either in driver already or waiting for driver to claim it
615 * and start processing.
617 if ((!src_q
->streaming
|| list_empty(&src_q
->queued_list
))
618 && (!dst_q
->streaming
|| list_empty(&dst_q
->queued_list
))) {
623 spin_lock_irqsave(&src_q
->done_lock
, flags
);
624 if (list_empty(&src_q
->done_list
))
625 poll_wait(file
, &src_q
->done_wq
, wait
);
626 spin_unlock_irqrestore(&src_q
->done_lock
, flags
);
628 spin_lock_irqsave(&dst_q
->done_lock
, flags
);
629 if (list_empty(&dst_q
->done_list
)) {
631 * If the last buffer was dequeued from the capture queue,
632 * return immediately. DQBUF will return -EPIPE.
634 if (dst_q
->last_buffer_dequeued
) {
635 spin_unlock_irqrestore(&dst_q
->done_lock
, flags
);
636 return rc
| EPOLLIN
| EPOLLRDNORM
;
639 poll_wait(file
, &dst_q
->done_wq
, wait
);
641 spin_unlock_irqrestore(&dst_q
->done_lock
, flags
);
643 spin_lock_irqsave(&src_q
->done_lock
, flags
);
644 if (!list_empty(&src_q
->done_list
))
645 src_vb
= list_first_entry(&src_q
->done_list
, struct vb2_buffer
,
647 if (src_vb
&& (src_vb
->state
== VB2_BUF_STATE_DONE
648 || src_vb
->state
== VB2_BUF_STATE_ERROR
))
649 rc
|= EPOLLOUT
| EPOLLWRNORM
;
650 spin_unlock_irqrestore(&src_q
->done_lock
, flags
);
652 spin_lock_irqsave(&dst_q
->done_lock
, flags
);
653 if (!list_empty(&dst_q
->done_list
))
654 dst_vb
= list_first_entry(&dst_q
->done_list
, struct vb2_buffer
,
656 if (dst_vb
&& (dst_vb
->state
== VB2_BUF_STATE_DONE
657 || dst_vb
->state
== VB2_BUF_STATE_ERROR
))
658 rc
|= EPOLLIN
| EPOLLRDNORM
;
659 spin_unlock_irqrestore(&dst_q
->done_lock
, flags
);
664 EXPORT_SYMBOL_GPL(v4l2_m2m_poll
);
666 int v4l2_m2m_mmap(struct file
*file
, struct v4l2_m2m_ctx
*m2m_ctx
,
667 struct vm_area_struct
*vma
)
669 unsigned long offset
= vma
->vm_pgoff
<< PAGE_SHIFT
;
670 struct vb2_queue
*vq
;
672 if (offset
< DST_QUEUE_OFF_BASE
) {
673 vq
= v4l2_m2m_get_src_vq(m2m_ctx
);
675 vq
= v4l2_m2m_get_dst_vq(m2m_ctx
);
676 vma
->vm_pgoff
-= (DST_QUEUE_OFF_BASE
>> PAGE_SHIFT
);
679 return vb2_mmap(vq
, vma
);
681 EXPORT_SYMBOL(v4l2_m2m_mmap
);
683 #if defined(CONFIG_MEDIA_CONTROLLER)
684 void v4l2_m2m_unregister_media_controller(struct v4l2_m2m_dev
*m2m_dev
)
686 media_remove_intf_links(&m2m_dev
->intf_devnode
->intf
);
687 media_devnode_remove(m2m_dev
->intf_devnode
);
689 media_entity_remove_links(m2m_dev
->source
);
690 media_entity_remove_links(&m2m_dev
->sink
);
691 media_entity_remove_links(&m2m_dev
->proc
);
692 media_device_unregister_entity(m2m_dev
->source
);
693 media_device_unregister_entity(&m2m_dev
->sink
);
694 media_device_unregister_entity(&m2m_dev
->proc
);
695 kfree(m2m_dev
->source
->name
);
696 kfree(m2m_dev
->sink
.name
);
697 kfree(m2m_dev
->proc
.name
);
699 EXPORT_SYMBOL_GPL(v4l2_m2m_unregister_media_controller
);
701 static int v4l2_m2m_register_entity(struct media_device
*mdev
,
702 struct v4l2_m2m_dev
*m2m_dev
, enum v4l2_m2m_entity_type type
,
703 struct video_device
*vdev
, int function
)
705 struct media_entity
*entity
;
706 struct media_pad
*pads
;
713 case MEM2MEM_ENT_TYPE_SOURCE
:
714 entity
= m2m_dev
->source
;
715 pads
= &m2m_dev
->source_pad
;
716 pads
[0].flags
= MEDIA_PAD_FL_SOURCE
;
719 case MEM2MEM_ENT_TYPE_SINK
:
720 entity
= &m2m_dev
->sink
;
721 pads
= &m2m_dev
->sink_pad
;
722 pads
[0].flags
= MEDIA_PAD_FL_SINK
;
725 case MEM2MEM_ENT_TYPE_PROC
:
726 entity
= &m2m_dev
->proc
;
727 pads
= m2m_dev
->proc_pads
;
728 pads
[0].flags
= MEDIA_PAD_FL_SINK
;
729 pads
[1].flags
= MEDIA_PAD_FL_SOURCE
;
736 entity
->obj_type
= MEDIA_ENTITY_TYPE_BASE
;
737 if (type
!= MEM2MEM_ENT_TYPE_PROC
) {
738 entity
->info
.dev
.major
= VIDEO_MAJOR
;
739 entity
->info
.dev
.minor
= vdev
->minor
;
741 len
= strlen(vdev
->name
) + 2 + strlen(m2m_entity_name
[type
]);
742 name
= kmalloc(len
, GFP_KERNEL
);
745 snprintf(name
, len
, "%s-%s", vdev
->name
, m2m_entity_name
[type
]);
747 entity
->function
= function
;
749 ret
= media_entity_pads_init(entity
, num_pads
, pads
);
752 ret
= media_device_register_entity(mdev
, entity
);
759 int v4l2_m2m_register_media_controller(struct v4l2_m2m_dev
*m2m_dev
,
760 struct video_device
*vdev
, int function
)
762 struct media_device
*mdev
= vdev
->v4l2_dev
->mdev
;
763 struct media_link
*link
;
769 /* A memory-to-memory device consists in two
770 * DMA engine and one video processing entities.
771 * The DMA engine entities are linked to a V4L interface
774 /* Create the three entities with their pads */
775 m2m_dev
->source
= &vdev
->entity
;
776 ret
= v4l2_m2m_register_entity(mdev
, m2m_dev
,
777 MEM2MEM_ENT_TYPE_SOURCE
, vdev
, MEDIA_ENT_F_IO_V4L
);
780 ret
= v4l2_m2m_register_entity(mdev
, m2m_dev
,
781 MEM2MEM_ENT_TYPE_PROC
, vdev
, function
);
783 goto err_rel_entity0
;
784 ret
= v4l2_m2m_register_entity(mdev
, m2m_dev
,
785 MEM2MEM_ENT_TYPE_SINK
, vdev
, MEDIA_ENT_F_IO_V4L
);
787 goto err_rel_entity1
;
789 /* Connect the three entities */
790 ret
= media_create_pad_link(m2m_dev
->source
, 0, &m2m_dev
->proc
, 1,
791 MEDIA_LNK_FL_IMMUTABLE
| MEDIA_LNK_FL_ENABLED
);
793 goto err_rel_entity2
;
795 ret
= media_create_pad_link(&m2m_dev
->proc
, 0, &m2m_dev
->sink
, 0,
796 MEDIA_LNK_FL_IMMUTABLE
| MEDIA_LNK_FL_ENABLED
);
800 /* Create video interface */
801 m2m_dev
->intf_devnode
= media_devnode_create(mdev
,
802 MEDIA_INTF_T_V4L_VIDEO
, 0,
803 VIDEO_MAJOR
, vdev
->minor
);
804 if (!m2m_dev
->intf_devnode
) {
809 /* Connect the two DMA engines to the interface */
810 link
= media_create_intf_link(m2m_dev
->source
,
811 &m2m_dev
->intf_devnode
->intf
,
812 MEDIA_LNK_FL_IMMUTABLE
| MEDIA_LNK_FL_ENABLED
);
818 link
= media_create_intf_link(&m2m_dev
->sink
,
819 &m2m_dev
->intf_devnode
->intf
,
820 MEDIA_LNK_FL_IMMUTABLE
| MEDIA_LNK_FL_ENABLED
);
823 goto err_rm_intf_link
;
828 media_remove_intf_links(&m2m_dev
->intf_devnode
->intf
);
830 media_devnode_remove(m2m_dev
->intf_devnode
);
832 media_entity_remove_links(&m2m_dev
->sink
);
834 media_entity_remove_links(&m2m_dev
->proc
);
835 media_entity_remove_links(m2m_dev
->source
);
837 media_device_unregister_entity(&m2m_dev
->proc
);
838 kfree(m2m_dev
->proc
.name
);
840 media_device_unregister_entity(&m2m_dev
->sink
);
841 kfree(m2m_dev
->sink
.name
);
843 media_device_unregister_entity(m2m_dev
->source
);
844 kfree(m2m_dev
->source
->name
);
848 EXPORT_SYMBOL_GPL(v4l2_m2m_register_media_controller
);
851 struct v4l2_m2m_dev
*v4l2_m2m_init(const struct v4l2_m2m_ops
*m2m_ops
)
853 struct v4l2_m2m_dev
*m2m_dev
;
855 if (!m2m_ops
|| WARN_ON(!m2m_ops
->device_run
))
856 return ERR_PTR(-EINVAL
);
858 m2m_dev
= kzalloc(sizeof *m2m_dev
, GFP_KERNEL
);
860 return ERR_PTR(-ENOMEM
);
862 m2m_dev
->curr_ctx
= NULL
;
863 m2m_dev
->m2m_ops
= m2m_ops
;
864 INIT_LIST_HEAD(&m2m_dev
->job_queue
);
865 spin_lock_init(&m2m_dev
->job_spinlock
);
869 EXPORT_SYMBOL_GPL(v4l2_m2m_init
);
871 void v4l2_m2m_release(struct v4l2_m2m_dev
*m2m_dev
)
875 EXPORT_SYMBOL_GPL(v4l2_m2m_release
);
877 struct v4l2_m2m_ctx
*v4l2_m2m_ctx_init(struct v4l2_m2m_dev
*m2m_dev
,
879 int (*queue_init
)(void *priv
, struct vb2_queue
*src_vq
, struct vb2_queue
*dst_vq
))
881 struct v4l2_m2m_ctx
*m2m_ctx
;
882 struct v4l2_m2m_queue_ctx
*out_q_ctx
, *cap_q_ctx
;
885 m2m_ctx
= kzalloc(sizeof *m2m_ctx
, GFP_KERNEL
);
887 return ERR_PTR(-ENOMEM
);
889 m2m_ctx
->priv
= drv_priv
;
890 m2m_ctx
->m2m_dev
= m2m_dev
;
891 init_waitqueue_head(&m2m_ctx
->finished
);
893 out_q_ctx
= &m2m_ctx
->out_q_ctx
;
894 cap_q_ctx
= &m2m_ctx
->cap_q_ctx
;
896 INIT_LIST_HEAD(&out_q_ctx
->rdy_queue
);
897 INIT_LIST_HEAD(&cap_q_ctx
->rdy_queue
);
898 spin_lock_init(&out_q_ctx
->rdy_spinlock
);
899 spin_lock_init(&cap_q_ctx
->rdy_spinlock
);
901 INIT_LIST_HEAD(&m2m_ctx
->queue
);
903 ret
= queue_init(drv_priv
, &out_q_ctx
->q
, &cap_q_ctx
->q
);
908 * If both queues use same mutex assign it as the common buffer
909 * queues lock to the m2m context. This lock is used in the
910 * v4l2_m2m_ioctl_* helpers.
912 if (out_q_ctx
->q
.lock
== cap_q_ctx
->q
.lock
)
913 m2m_ctx
->q_lock
= out_q_ctx
->q
.lock
;
920 EXPORT_SYMBOL_GPL(v4l2_m2m_ctx_init
);
922 void v4l2_m2m_ctx_release(struct v4l2_m2m_ctx
*m2m_ctx
)
924 /* wait until the current context is dequeued from job_queue */
925 v4l2_m2m_cancel_job(m2m_ctx
);
927 vb2_queue_release(&m2m_ctx
->cap_q_ctx
.q
);
928 vb2_queue_release(&m2m_ctx
->out_q_ctx
.q
);
932 EXPORT_SYMBOL_GPL(v4l2_m2m_ctx_release
);
934 void v4l2_m2m_buf_queue(struct v4l2_m2m_ctx
*m2m_ctx
,
935 struct vb2_v4l2_buffer
*vbuf
)
937 struct v4l2_m2m_buffer
*b
= container_of(vbuf
,
938 struct v4l2_m2m_buffer
, vb
);
939 struct v4l2_m2m_queue_ctx
*q_ctx
;
942 q_ctx
= get_queue_ctx(m2m_ctx
, vbuf
->vb2_buf
.vb2_queue
->type
);
946 spin_lock_irqsave(&q_ctx
->rdy_spinlock
, flags
);
947 list_add_tail(&b
->list
, &q_ctx
->rdy_queue
);
949 spin_unlock_irqrestore(&q_ctx
->rdy_spinlock
, flags
);
951 EXPORT_SYMBOL_GPL(v4l2_m2m_buf_queue
);
953 /* Videobuf2 ioctl helpers */
955 int v4l2_m2m_ioctl_reqbufs(struct file
*file
, void *priv
,
956 struct v4l2_requestbuffers
*rb
)
958 struct v4l2_fh
*fh
= file
->private_data
;
960 return v4l2_m2m_reqbufs(file
, fh
->m2m_ctx
, rb
);
962 EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_reqbufs
);
964 int v4l2_m2m_ioctl_create_bufs(struct file
*file
, void *priv
,
965 struct v4l2_create_buffers
*create
)
967 struct v4l2_fh
*fh
= file
->private_data
;
969 return v4l2_m2m_create_bufs(file
, fh
->m2m_ctx
, create
);
971 EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_create_bufs
);
973 int v4l2_m2m_ioctl_querybuf(struct file
*file
, void *priv
,
974 struct v4l2_buffer
*buf
)
976 struct v4l2_fh
*fh
= file
->private_data
;
978 return v4l2_m2m_querybuf(file
, fh
->m2m_ctx
, buf
);
980 EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_querybuf
);
982 int v4l2_m2m_ioctl_qbuf(struct file
*file
, void *priv
,
983 struct v4l2_buffer
*buf
)
985 struct v4l2_fh
*fh
= file
->private_data
;
987 return v4l2_m2m_qbuf(file
, fh
->m2m_ctx
, buf
);
989 EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_qbuf
);
991 int v4l2_m2m_ioctl_dqbuf(struct file
*file
, void *priv
,
992 struct v4l2_buffer
*buf
)
994 struct v4l2_fh
*fh
= file
->private_data
;
996 return v4l2_m2m_dqbuf(file
, fh
->m2m_ctx
, buf
);
998 EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_dqbuf
);
1000 int v4l2_m2m_ioctl_prepare_buf(struct file
*file
, void *priv
,
1001 struct v4l2_buffer
*buf
)
1003 struct v4l2_fh
*fh
= file
->private_data
;
1005 return v4l2_m2m_prepare_buf(file
, fh
->m2m_ctx
, buf
);
1007 EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_prepare_buf
);
1009 int v4l2_m2m_ioctl_expbuf(struct file
*file
, void *priv
,
1010 struct v4l2_exportbuffer
*eb
)
1012 struct v4l2_fh
*fh
= file
->private_data
;
1014 return v4l2_m2m_expbuf(file
, fh
->m2m_ctx
, eb
);
1016 EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_expbuf
);
1018 int v4l2_m2m_ioctl_streamon(struct file
*file
, void *priv
,
1019 enum v4l2_buf_type type
)
1021 struct v4l2_fh
*fh
= file
->private_data
;
1023 return v4l2_m2m_streamon(file
, fh
->m2m_ctx
, type
);
1025 EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_streamon
);
1027 int v4l2_m2m_ioctl_streamoff(struct file
*file
, void *priv
,
1028 enum v4l2_buf_type type
)
1030 struct v4l2_fh
*fh
= file
->private_data
;
1032 return v4l2_m2m_streamoff(file
, fh
->m2m_ctx
, type
);
1034 EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_streamoff
);
1037 * v4l2_file_operations helpers. It is assumed here same lock is used
1038 * for the output and the capture buffer queue.
1041 int v4l2_m2m_fop_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1043 struct v4l2_fh
*fh
= file
->private_data
;
1045 return v4l2_m2m_mmap(file
, fh
->m2m_ctx
, vma
);
1047 EXPORT_SYMBOL_GPL(v4l2_m2m_fop_mmap
);
1049 __poll_t
v4l2_m2m_fop_poll(struct file
*file
, poll_table
*wait
)
1051 struct v4l2_fh
*fh
= file
->private_data
;
1052 struct v4l2_m2m_ctx
*m2m_ctx
= fh
->m2m_ctx
;
1055 if (m2m_ctx
->q_lock
)
1056 mutex_lock(m2m_ctx
->q_lock
);
1058 ret
= v4l2_m2m_poll(file
, m2m_ctx
, wait
);
1060 if (m2m_ctx
->q_lock
)
1061 mutex_unlock(m2m_ctx
->q_lock
);
1065 EXPORT_SYMBOL_GPL(v4l2_m2m_fop_poll
);