Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 1 | /* |
| 2 | * Memory-to-memory device framework for Video for Linux 2 and videobuf. |
| 3 | * |
| 4 | * Helper functions for devices that use videobuf buffers for both their |
| 5 | * source and destination. |
| 6 | * |
| 7 | * Copyright (c) 2009-2010 Samsung Electronics Co., Ltd. |
Pawel Osciak | 9507208 | 2011-03-13 15:23:32 -0300 | [diff] [blame] | 8 | * Pawel Osciak, <pawel@osciak.com> |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 9 | * Marek Szyprowski, <m.szyprowski@samsung.com> |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License as published by the |
| 13 | * Free Software Foundation; either version 2 of the License, or (at your |
| 14 | * option) any later version. |
| 15 | */ |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/sched.h> |
| 18 | #include <linux/slab.h> |
| 19 | |
Junghak Sung | c139990 | 2015-09-22 10:30:29 -0300 | [diff] [blame^] | 20 | #include <media/videobuf2-v4l2.h> |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 21 | #include <media/v4l2-mem2mem.h> |
Hans Verkuil | 08eb851 | 2012-07-18 10:53:04 -0300 | [diff] [blame] | 22 | #include <media/v4l2-dev.h> |
| 23 | #include <media/v4l2-fh.h> |
| 24 | #include <media/v4l2-event.h> |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 25 | |
| 26 | MODULE_DESCRIPTION("Mem to mem device framework for videobuf"); |
Pawel Osciak | 9507208 | 2011-03-13 15:23:32 -0300 | [diff] [blame] | 27 | MODULE_AUTHOR("Pawel Osciak, <pawel@osciak.com>"); |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 28 | MODULE_LICENSE("GPL"); |
| 29 | |
| 30 | static bool debug; |
| 31 | module_param(debug, bool, 0644); |
| 32 | |
| 33 | #define dprintk(fmt, arg...) \ |
| 34 | do { \ |
| 35 | if (debug) \ |
| 36 | printk(KERN_DEBUG "%s: " fmt, __func__, ## arg);\ |
| 37 | } while (0) |
| 38 | |
| 39 | |
| 40 | /* Instance is already queued on the job_queue */ |
| 41 | #define TRANS_QUEUED (1 << 0) |
| 42 | /* Instance is currently running in hardware */ |
| 43 | #define TRANS_RUNNING (1 << 1) |
Shaik Ameer Basha | 2ad5389 | 2013-09-20 03:26:18 -0300 | [diff] [blame] | 44 | /* Instance is currently aborting */ |
| 45 | #define TRANS_ABORT (1 << 2) |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 46 | |
| 47 | |
| 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) |
| 52 | |
| 53 | |
| 54 | /** |
| 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 |
| 60 | */ |
| 61 | struct v4l2_m2m_dev { |
| 62 | struct v4l2_m2m_ctx *curr_ctx; |
| 63 | |
| 64 | struct list_head job_queue; |
| 65 | spinlock_t job_spinlock; |
| 66 | |
Guennadi Liakhovetski | b1252eb | 2012-09-11 06:32:17 -0300 | [diff] [blame] | 67 | const struct v4l2_m2m_ops *m2m_ops; |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 68 | }; |
| 69 | |
| 70 | static struct v4l2_m2m_queue_ctx *get_queue_ctx(struct v4l2_m2m_ctx *m2m_ctx, |
| 71 | enum v4l2_buf_type type) |
| 72 | { |
Marek Szyprowski | 908a0d7 | 2011-01-12 06:50:24 -0300 | [diff] [blame] | 73 | if (V4L2_TYPE_IS_OUTPUT(type)) |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 74 | return &m2m_ctx->out_q_ctx; |
Marek Szyprowski | 908a0d7 | 2011-01-12 06:50:24 -0300 | [diff] [blame] | 75 | else |
| 76 | return &m2m_ctx->cap_q_ctx; |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | /** |
Marek Szyprowski | 908a0d7 | 2011-01-12 06:50:24 -0300 | [diff] [blame] | 80 | * v4l2_m2m_get_vq() - return vb2_queue for the given type |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 81 | */ |
Marek Szyprowski | 908a0d7 | 2011-01-12 06:50:24 -0300 | [diff] [blame] | 82 | struct vb2_queue *v4l2_m2m_get_vq(struct v4l2_m2m_ctx *m2m_ctx, |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 83 | enum v4l2_buf_type type) |
| 84 | { |
| 85 | struct v4l2_m2m_queue_ctx *q_ctx; |
| 86 | |
| 87 | q_ctx = get_queue_ctx(m2m_ctx, type); |
| 88 | if (!q_ctx) |
| 89 | return NULL; |
| 90 | |
| 91 | return &q_ctx->q; |
| 92 | } |
| 93 | EXPORT_SYMBOL(v4l2_m2m_get_vq); |
| 94 | |
| 95 | /** |
| 96 | * v4l2_m2m_next_buf() - return next buffer from the list of ready buffers |
| 97 | */ |
Marek Szyprowski | 908a0d7 | 2011-01-12 06:50:24 -0300 | [diff] [blame] | 98 | void *v4l2_m2m_next_buf(struct v4l2_m2m_queue_ctx *q_ctx) |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 99 | { |
Philipp Zabel | d5451c1 | 2015-03-11 12:57:50 -0300 | [diff] [blame] | 100 | struct v4l2_m2m_buffer *b; |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 101 | unsigned long flags; |
| 102 | |
Marek Szyprowski | 908a0d7 | 2011-01-12 06:50:24 -0300 | [diff] [blame] | 103 | spin_lock_irqsave(&q_ctx->rdy_spinlock, flags); |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 104 | |
Andrzej Pietrasiewicz | a6bd62be | 2011-08-25 07:21:21 -0300 | [diff] [blame] | 105 | if (list_empty(&q_ctx->rdy_queue)) { |
| 106 | spin_unlock_irqrestore(&q_ctx->rdy_spinlock, flags); |
| 107 | return NULL; |
| 108 | } |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 109 | |
Sascha Hauer | c392e9e | 2012-08-31 09:18:03 -0300 | [diff] [blame] | 110 | b = list_first_entry(&q_ctx->rdy_queue, struct v4l2_m2m_buffer, list); |
Marek Szyprowski | 908a0d7 | 2011-01-12 06:50:24 -0300 | [diff] [blame] | 111 | spin_unlock_irqrestore(&q_ctx->rdy_spinlock, flags); |
| 112 | return &b->vb; |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 113 | } |
| 114 | EXPORT_SYMBOL_GPL(v4l2_m2m_next_buf); |
| 115 | |
| 116 | /** |
| 117 | * v4l2_m2m_buf_remove() - take off a buffer from the list of ready buffers and |
| 118 | * return it |
| 119 | */ |
Marek Szyprowski | 908a0d7 | 2011-01-12 06:50:24 -0300 | [diff] [blame] | 120 | void *v4l2_m2m_buf_remove(struct v4l2_m2m_queue_ctx *q_ctx) |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 121 | { |
Philipp Zabel | d5451c1 | 2015-03-11 12:57:50 -0300 | [diff] [blame] | 122 | struct v4l2_m2m_buffer *b; |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 123 | unsigned long flags; |
| 124 | |
Marek Szyprowski | 908a0d7 | 2011-01-12 06:50:24 -0300 | [diff] [blame] | 125 | spin_lock_irqsave(&q_ctx->rdy_spinlock, flags); |
Andrzej Pietrasiewicz | a6bd62be | 2011-08-25 07:21:21 -0300 | [diff] [blame] | 126 | if (list_empty(&q_ctx->rdy_queue)) { |
| 127 | spin_unlock_irqrestore(&q_ctx->rdy_spinlock, flags); |
| 128 | return NULL; |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 129 | } |
Sascha Hauer | c392e9e | 2012-08-31 09:18:03 -0300 | [diff] [blame] | 130 | b = list_first_entry(&q_ctx->rdy_queue, struct v4l2_m2m_buffer, list); |
Andrzej Pietrasiewicz | a6bd62be | 2011-08-25 07:21:21 -0300 | [diff] [blame] | 131 | list_del(&b->list); |
| 132 | q_ctx->num_rdy--; |
Marek Szyprowski | 908a0d7 | 2011-01-12 06:50:24 -0300 | [diff] [blame] | 133 | spin_unlock_irqrestore(&q_ctx->rdy_spinlock, flags); |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 134 | |
Marek Szyprowski | 908a0d7 | 2011-01-12 06:50:24 -0300 | [diff] [blame] | 135 | return &b->vb; |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 136 | } |
| 137 | EXPORT_SYMBOL_GPL(v4l2_m2m_buf_remove); |
| 138 | |
| 139 | /* |
| 140 | * Scheduling handlers |
| 141 | */ |
| 142 | |
| 143 | /** |
| 144 | * v4l2_m2m_get_curr_priv() - return driver private data for the currently |
| 145 | * running instance or NULL if no instance is running |
| 146 | */ |
| 147 | void *v4l2_m2m_get_curr_priv(struct v4l2_m2m_dev *m2m_dev) |
| 148 | { |
| 149 | unsigned long flags; |
| 150 | void *ret = NULL; |
| 151 | |
| 152 | spin_lock_irqsave(&m2m_dev->job_spinlock, flags); |
| 153 | if (m2m_dev->curr_ctx) |
| 154 | ret = m2m_dev->curr_ctx->priv; |
| 155 | spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags); |
| 156 | |
| 157 | return ret; |
| 158 | } |
| 159 | EXPORT_SYMBOL(v4l2_m2m_get_curr_priv); |
| 160 | |
| 161 | /** |
| 162 | * v4l2_m2m_try_run() - select next job to perform and run it if possible |
| 163 | * |
| 164 | * Get next transaction (if present) from the waiting jobs list and run it. |
| 165 | */ |
| 166 | static void v4l2_m2m_try_run(struct v4l2_m2m_dev *m2m_dev) |
| 167 | { |
| 168 | unsigned long flags; |
| 169 | |
| 170 | spin_lock_irqsave(&m2m_dev->job_spinlock, flags); |
| 171 | if (NULL != m2m_dev->curr_ctx) { |
| 172 | spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags); |
| 173 | dprintk("Another instance is running, won't run now\n"); |
| 174 | return; |
| 175 | } |
| 176 | |
| 177 | if (list_empty(&m2m_dev->job_queue)) { |
| 178 | spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags); |
| 179 | dprintk("No job pending\n"); |
| 180 | return; |
| 181 | } |
| 182 | |
Sascha Hauer | c392e9e | 2012-08-31 09:18:03 -0300 | [diff] [blame] | 183 | m2m_dev->curr_ctx = list_first_entry(&m2m_dev->job_queue, |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 184 | struct v4l2_m2m_ctx, queue); |
| 185 | m2m_dev->curr_ctx->job_flags |= TRANS_RUNNING; |
| 186 | spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags); |
| 187 | |
| 188 | m2m_dev->m2m_ops->device_run(m2m_dev->curr_ctx->priv); |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * v4l2_m2m_try_schedule() - check whether an instance is ready to be added to |
| 193 | * the pending job queue and add it if so. |
| 194 | * @m2m_ctx: m2m context assigned to the instance to be checked |
| 195 | * |
| 196 | * There are three basic requirements an instance has to meet to be able to run: |
| 197 | * 1) at least one source buffer has to be queued, |
| 198 | * 2) at least one destination buffer has to be queued, |
| 199 | * 3) streaming has to be on. |
| 200 | * |
Philipp Zabel | 33bdd5a | 2013-06-03 04:23:48 -0300 | [diff] [blame] | 201 | * If a queue is buffered (for example a decoder hardware ringbuffer that has |
| 202 | * to be drained before doing streamoff), allow scheduling without v4l2 buffers |
| 203 | * on that queue. |
| 204 | * |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 205 | * There may also be additional, custom requirements. In such case the driver |
| 206 | * should supply a custom callback (job_ready in v4l2_m2m_ops) that should |
| 207 | * return 1 if the instance is ready. |
| 208 | * An example of the above could be an instance that requires more than one |
| 209 | * src/dst buffer per transaction. |
| 210 | */ |
Michael Olbrich | 1190a41 | 2014-07-22 09:36:04 -0300 | [diff] [blame] | 211 | void v4l2_m2m_try_schedule(struct v4l2_m2m_ctx *m2m_ctx) |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 212 | { |
| 213 | struct v4l2_m2m_dev *m2m_dev; |
John Sheu | b730627 | 2013-05-23 20:41:48 -0300 | [diff] [blame] | 214 | unsigned long flags_job, flags_out, flags_cap; |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 215 | |
| 216 | m2m_dev = m2m_ctx->m2m_dev; |
| 217 | dprintk("Trying to schedule a job for m2m_ctx: %p\n", m2m_ctx); |
| 218 | |
| 219 | if (!m2m_ctx->out_q_ctx.q.streaming |
| 220 | || !m2m_ctx->cap_q_ctx.q.streaming) { |
| 221 | dprintk("Streaming needs to be on for both queues\n"); |
| 222 | return; |
| 223 | } |
| 224 | |
| 225 | spin_lock_irqsave(&m2m_dev->job_spinlock, flags_job); |
Shaik Ameer Basha | 2ad5389 | 2013-09-20 03:26:18 -0300 | [diff] [blame] | 226 | |
| 227 | /* If the context is aborted then don't schedule it */ |
| 228 | if (m2m_ctx->job_flags & TRANS_ABORT) { |
| 229 | spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags_job); |
| 230 | dprintk("Aborted context\n"); |
| 231 | return; |
| 232 | } |
| 233 | |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 234 | if (m2m_ctx->job_flags & TRANS_QUEUED) { |
| 235 | spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags_job); |
| 236 | dprintk("On job queue already\n"); |
| 237 | return; |
| 238 | } |
| 239 | |
John Sheu | b730627 | 2013-05-23 20:41:48 -0300 | [diff] [blame] | 240 | spin_lock_irqsave(&m2m_ctx->out_q_ctx.rdy_spinlock, flags_out); |
Philipp Zabel | 33bdd5a | 2013-06-03 04:23:48 -0300 | [diff] [blame] | 241 | if (list_empty(&m2m_ctx->out_q_ctx.rdy_queue) |
| 242 | && !m2m_ctx->out_q_ctx.buffered) { |
John Sheu | b730627 | 2013-05-23 20:41:48 -0300 | [diff] [blame] | 243 | spin_unlock_irqrestore(&m2m_ctx->out_q_ctx.rdy_spinlock, |
| 244 | flags_out); |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 245 | spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags_job); |
| 246 | dprintk("No input buffers available\n"); |
| 247 | return; |
| 248 | } |
John Sheu | b730627 | 2013-05-23 20:41:48 -0300 | [diff] [blame] | 249 | spin_lock_irqsave(&m2m_ctx->cap_q_ctx.rdy_spinlock, flags_cap); |
Philipp Zabel | 33bdd5a | 2013-06-03 04:23:48 -0300 | [diff] [blame] | 250 | if (list_empty(&m2m_ctx->cap_q_ctx.rdy_queue) |
| 251 | && !m2m_ctx->cap_q_ctx.buffered) { |
John Sheu | b730627 | 2013-05-23 20:41:48 -0300 | [diff] [blame] | 252 | spin_unlock_irqrestore(&m2m_ctx->cap_q_ctx.rdy_spinlock, |
| 253 | flags_cap); |
| 254 | spin_unlock_irqrestore(&m2m_ctx->out_q_ctx.rdy_spinlock, |
| 255 | flags_out); |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 256 | spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags_job); |
| 257 | dprintk("No output buffers available\n"); |
| 258 | return; |
| 259 | } |
John Sheu | b730627 | 2013-05-23 20:41:48 -0300 | [diff] [blame] | 260 | spin_unlock_irqrestore(&m2m_ctx->cap_q_ctx.rdy_spinlock, flags_cap); |
| 261 | spin_unlock_irqrestore(&m2m_ctx->out_q_ctx.rdy_spinlock, flags_out); |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 262 | |
| 263 | if (m2m_dev->m2m_ops->job_ready |
| 264 | && (!m2m_dev->m2m_ops->job_ready(m2m_ctx->priv))) { |
| 265 | spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags_job); |
| 266 | dprintk("Driver not ready\n"); |
| 267 | return; |
| 268 | } |
| 269 | |
| 270 | list_add_tail(&m2m_ctx->queue, &m2m_dev->job_queue); |
| 271 | m2m_ctx->job_flags |= TRANS_QUEUED; |
| 272 | |
| 273 | spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags_job); |
| 274 | |
| 275 | v4l2_m2m_try_run(m2m_dev); |
| 276 | } |
Michael Olbrich | 1190a41 | 2014-07-22 09:36:04 -0300 | [diff] [blame] | 277 | EXPORT_SYMBOL_GPL(v4l2_m2m_try_schedule); |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 278 | |
| 279 | /** |
Shaik Ameer Basha | fea564a | 2013-08-13 02:58:07 -0300 | [diff] [blame] | 280 | * v4l2_m2m_cancel_job() - cancel pending jobs for the context |
| 281 | * |
| 282 | * In case of streamoff or release called on any context, |
| 283 | * 1] If the context is currently running, then abort job will be called |
| 284 | * 2] If the context is queued, then the context will be removed from |
| 285 | * the job_queue |
| 286 | */ |
| 287 | static void v4l2_m2m_cancel_job(struct v4l2_m2m_ctx *m2m_ctx) |
| 288 | { |
| 289 | struct v4l2_m2m_dev *m2m_dev; |
| 290 | unsigned long flags; |
| 291 | |
| 292 | m2m_dev = m2m_ctx->m2m_dev; |
| 293 | spin_lock_irqsave(&m2m_dev->job_spinlock, flags); |
Shaik Ameer Basha | 2ad5389 | 2013-09-20 03:26:18 -0300 | [diff] [blame] | 294 | |
| 295 | m2m_ctx->job_flags |= TRANS_ABORT; |
Shaik Ameer Basha | fea564a | 2013-08-13 02:58:07 -0300 | [diff] [blame] | 296 | if (m2m_ctx->job_flags & TRANS_RUNNING) { |
| 297 | spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags); |
| 298 | m2m_dev->m2m_ops->job_abort(m2m_ctx->priv); |
| 299 | dprintk("m2m_ctx %p running, will wait to complete", m2m_ctx); |
| 300 | wait_event(m2m_ctx->finished, |
| 301 | !(m2m_ctx->job_flags & TRANS_RUNNING)); |
| 302 | } else if (m2m_ctx->job_flags & TRANS_QUEUED) { |
| 303 | list_del(&m2m_ctx->queue); |
| 304 | m2m_ctx->job_flags &= ~(TRANS_QUEUED | TRANS_RUNNING); |
| 305 | spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags); |
| 306 | dprintk("m2m_ctx: %p had been on queue and was removed\n", |
| 307 | m2m_ctx); |
| 308 | } else { |
| 309 | /* Do nothing, was not on queue/running */ |
| 310 | spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags); |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | /** |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 315 | * v4l2_m2m_job_finish() - inform the framework that a job has been finished |
| 316 | * and have it clean up |
| 317 | * |
| 318 | * Called by a driver to yield back the device after it has finished with it. |
| 319 | * Should be called as soon as possible after reaching a state which allows |
| 320 | * other instances to take control of the device. |
| 321 | * |
| 322 | * This function has to be called only after device_run() callback has been |
| 323 | * called on the driver. To prevent recursion, it should not be called directly |
| 324 | * from the device_run() callback though. |
| 325 | */ |
| 326 | void v4l2_m2m_job_finish(struct v4l2_m2m_dev *m2m_dev, |
| 327 | struct v4l2_m2m_ctx *m2m_ctx) |
| 328 | { |
| 329 | unsigned long flags; |
| 330 | |
| 331 | spin_lock_irqsave(&m2m_dev->job_spinlock, flags); |
| 332 | if (!m2m_dev->curr_ctx || m2m_dev->curr_ctx != m2m_ctx) { |
| 333 | spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags); |
| 334 | dprintk("Called by an instance not currently running\n"); |
| 335 | return; |
| 336 | } |
| 337 | |
| 338 | list_del(&m2m_dev->curr_ctx->queue); |
| 339 | m2m_dev->curr_ctx->job_flags &= ~(TRANS_QUEUED | TRANS_RUNNING); |
Marek Szyprowski | 908a0d7 | 2011-01-12 06:50:24 -0300 | [diff] [blame] | 340 | wake_up(&m2m_dev->curr_ctx->finished); |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 341 | m2m_dev->curr_ctx = NULL; |
| 342 | |
| 343 | spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags); |
| 344 | |
| 345 | /* This instance might have more buffers ready, but since we do not |
| 346 | * allow more than one job on the job_queue per instance, each has |
| 347 | * to be scheduled separately after the previous one finishes. */ |
| 348 | v4l2_m2m_try_schedule(m2m_ctx); |
| 349 | v4l2_m2m_try_run(m2m_dev); |
| 350 | } |
| 351 | EXPORT_SYMBOL(v4l2_m2m_job_finish); |
| 352 | |
| 353 | /** |
| 354 | * v4l2_m2m_reqbufs() - multi-queue-aware REQBUFS multiplexer |
| 355 | */ |
| 356 | int v4l2_m2m_reqbufs(struct file *file, struct v4l2_m2m_ctx *m2m_ctx, |
| 357 | struct v4l2_requestbuffers *reqbufs) |
| 358 | { |
Marek Szyprowski | 908a0d7 | 2011-01-12 06:50:24 -0300 | [diff] [blame] | 359 | struct vb2_queue *vq; |
Philipp Zabel | c13a5cc | 2015-07-10 10:49:25 -0300 | [diff] [blame] | 360 | int ret; |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 361 | |
| 362 | vq = v4l2_m2m_get_vq(m2m_ctx, reqbufs->type); |
Philipp Zabel | c13a5cc | 2015-07-10 10:49:25 -0300 | [diff] [blame] | 363 | ret = vb2_reqbufs(vq, reqbufs); |
| 364 | /* If count == 0, then the owner has released all buffers and he |
| 365 | is no longer owner of the queue. Otherwise we have an owner. */ |
| 366 | if (ret == 0) |
| 367 | vq->owner = reqbufs->count ? file->private_data : NULL; |
| 368 | |
| 369 | return ret; |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 370 | } |
| 371 | EXPORT_SYMBOL_GPL(v4l2_m2m_reqbufs); |
| 372 | |
| 373 | /** |
| 374 | * v4l2_m2m_querybuf() - multi-queue-aware QUERYBUF multiplexer |
| 375 | * |
| 376 | * See v4l2_m2m_mmap() documentation for details. |
| 377 | */ |
| 378 | int v4l2_m2m_querybuf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx, |
| 379 | struct v4l2_buffer *buf) |
| 380 | { |
Marek Szyprowski | 908a0d7 | 2011-01-12 06:50:24 -0300 | [diff] [blame] | 381 | struct vb2_queue *vq; |
| 382 | int ret = 0; |
| 383 | unsigned int i; |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 384 | |
| 385 | vq = v4l2_m2m_get_vq(m2m_ctx, buf->type); |
Marek Szyprowski | 908a0d7 | 2011-01-12 06:50:24 -0300 | [diff] [blame] | 386 | ret = vb2_querybuf(vq, buf); |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 387 | |
Marek Szyprowski | 908a0d7 | 2011-01-12 06:50:24 -0300 | [diff] [blame] | 388 | /* Adjust MMAP memory offsets for the CAPTURE queue */ |
| 389 | if (buf->memory == V4L2_MEMORY_MMAP && !V4L2_TYPE_IS_OUTPUT(vq->type)) { |
| 390 | if (V4L2_TYPE_IS_MULTIPLANAR(vq->type)) { |
| 391 | for (i = 0; i < buf->length; ++i) |
| 392 | buf->m.planes[i].m.mem_offset |
| 393 | += DST_QUEUE_OFF_BASE; |
| 394 | } else { |
| 395 | buf->m.offset += DST_QUEUE_OFF_BASE; |
| 396 | } |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | return ret; |
| 400 | } |
| 401 | EXPORT_SYMBOL_GPL(v4l2_m2m_querybuf); |
| 402 | |
| 403 | /** |
| 404 | * v4l2_m2m_qbuf() - enqueue a source or destination buffer, depending on |
| 405 | * the type |
| 406 | */ |
| 407 | int v4l2_m2m_qbuf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx, |
| 408 | struct v4l2_buffer *buf) |
| 409 | { |
Marek Szyprowski | 908a0d7 | 2011-01-12 06:50:24 -0300 | [diff] [blame] | 410 | struct vb2_queue *vq; |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 411 | int ret; |
| 412 | |
| 413 | vq = v4l2_m2m_get_vq(m2m_ctx, buf->type); |
Marek Szyprowski | 908a0d7 | 2011-01-12 06:50:24 -0300 | [diff] [blame] | 414 | ret = vb2_qbuf(vq, buf); |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 415 | if (!ret) |
| 416 | v4l2_m2m_try_schedule(m2m_ctx); |
| 417 | |
| 418 | return ret; |
| 419 | } |
| 420 | EXPORT_SYMBOL_GPL(v4l2_m2m_qbuf); |
| 421 | |
| 422 | /** |
| 423 | * v4l2_m2m_dqbuf() - dequeue a source or destination buffer, depending on |
| 424 | * the type |
| 425 | */ |
| 426 | int v4l2_m2m_dqbuf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx, |
| 427 | struct v4l2_buffer *buf) |
| 428 | { |
Marek Szyprowski | 908a0d7 | 2011-01-12 06:50:24 -0300 | [diff] [blame] | 429 | struct vb2_queue *vq; |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 430 | |
| 431 | vq = v4l2_m2m_get_vq(m2m_ctx, buf->type); |
Marek Szyprowski | 908a0d7 | 2011-01-12 06:50:24 -0300 | [diff] [blame] | 432 | return vb2_dqbuf(vq, buf, file->f_flags & O_NONBLOCK); |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 433 | } |
| 434 | EXPORT_SYMBOL_GPL(v4l2_m2m_dqbuf); |
| 435 | |
| 436 | /** |
Hans Verkuil | e68cf47 | 2015-06-05 11:28:50 -0300 | [diff] [blame] | 437 | * v4l2_m2m_prepare_buf() - prepare a source or destination buffer, depending on |
| 438 | * the type |
| 439 | */ |
| 440 | int v4l2_m2m_prepare_buf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx, |
| 441 | struct v4l2_buffer *buf) |
| 442 | { |
| 443 | struct vb2_queue *vq; |
| 444 | int ret; |
| 445 | |
| 446 | vq = v4l2_m2m_get_vq(m2m_ctx, buf->type); |
| 447 | ret = vb2_prepare_buf(vq, buf); |
| 448 | if (!ret) |
| 449 | v4l2_m2m_try_schedule(m2m_ctx); |
| 450 | |
| 451 | return ret; |
| 452 | } |
| 453 | EXPORT_SYMBOL_GPL(v4l2_m2m_prepare_buf); |
| 454 | |
| 455 | /** |
Philipp Zabel | 8b94ca6 | 2013-05-21 04:16:28 -0300 | [diff] [blame] | 456 | * v4l2_m2m_create_bufs() - create a source or destination buffer, depending |
| 457 | * on the type |
| 458 | */ |
| 459 | int v4l2_m2m_create_bufs(struct file *file, struct v4l2_m2m_ctx *m2m_ctx, |
| 460 | struct v4l2_create_buffers *create) |
| 461 | { |
| 462 | struct vb2_queue *vq; |
| 463 | |
| 464 | vq = v4l2_m2m_get_vq(m2m_ctx, create->format.type); |
| 465 | return vb2_create_bufs(vq, create); |
| 466 | } |
| 467 | EXPORT_SYMBOL_GPL(v4l2_m2m_create_bufs); |
| 468 | |
| 469 | /** |
Tomasz Stanislawski | 83ae7c5 | 2012-06-14 11:32:24 -0300 | [diff] [blame] | 470 | * v4l2_m2m_expbuf() - export a source or destination buffer, depending on |
| 471 | * the type |
| 472 | */ |
| 473 | int v4l2_m2m_expbuf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx, |
| 474 | struct v4l2_exportbuffer *eb) |
| 475 | { |
| 476 | struct vb2_queue *vq; |
| 477 | |
| 478 | vq = v4l2_m2m_get_vq(m2m_ctx, eb->type); |
| 479 | return vb2_expbuf(vq, eb); |
| 480 | } |
| 481 | EXPORT_SYMBOL_GPL(v4l2_m2m_expbuf); |
| 482 | /** |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 483 | * v4l2_m2m_streamon() - turn on streaming for a video queue |
| 484 | */ |
| 485 | int v4l2_m2m_streamon(struct file *file, struct v4l2_m2m_ctx *m2m_ctx, |
| 486 | enum v4l2_buf_type type) |
| 487 | { |
Marek Szyprowski | 908a0d7 | 2011-01-12 06:50:24 -0300 | [diff] [blame] | 488 | struct vb2_queue *vq; |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 489 | int ret; |
| 490 | |
| 491 | vq = v4l2_m2m_get_vq(m2m_ctx, type); |
Marek Szyprowski | 908a0d7 | 2011-01-12 06:50:24 -0300 | [diff] [blame] | 492 | ret = vb2_streamon(vq, type); |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 493 | if (!ret) |
| 494 | v4l2_m2m_try_schedule(m2m_ctx); |
| 495 | |
| 496 | return ret; |
| 497 | } |
| 498 | EXPORT_SYMBOL_GPL(v4l2_m2m_streamon); |
| 499 | |
| 500 | /** |
| 501 | * v4l2_m2m_streamoff() - turn off streaming for a video queue |
| 502 | */ |
| 503 | int v4l2_m2m_streamoff(struct file *file, struct v4l2_m2m_ctx *m2m_ctx, |
| 504 | enum v4l2_buf_type type) |
| 505 | { |
John Sheu | 401f6a2 | 2013-02-06 20:03:01 -0300 | [diff] [blame] | 506 | struct v4l2_m2m_dev *m2m_dev; |
| 507 | struct v4l2_m2m_queue_ctx *q_ctx; |
| 508 | unsigned long flags_job, flags; |
| 509 | int ret; |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 510 | |
Shaik Ameer Basha | fea564a | 2013-08-13 02:58:07 -0300 | [diff] [blame] | 511 | /* wait until the current context is dequeued from job_queue */ |
| 512 | v4l2_m2m_cancel_job(m2m_ctx); |
| 513 | |
John Sheu | 401f6a2 | 2013-02-06 20:03:01 -0300 | [diff] [blame] | 514 | q_ctx = get_queue_ctx(m2m_ctx, type); |
| 515 | ret = vb2_streamoff(&q_ctx->q, type); |
| 516 | if (ret) |
| 517 | return ret; |
| 518 | |
| 519 | m2m_dev = m2m_ctx->m2m_dev; |
| 520 | spin_lock_irqsave(&m2m_dev->job_spinlock, flags_job); |
| 521 | /* We should not be scheduled anymore, since we're dropping a queue. */ |
Philipp Zabel | d7bb0ce8 | 2013-09-19 04:40:32 -0300 | [diff] [blame] | 522 | if (m2m_ctx->job_flags & TRANS_QUEUED) |
| 523 | list_del(&m2m_ctx->queue); |
John Sheu | 401f6a2 | 2013-02-06 20:03:01 -0300 | [diff] [blame] | 524 | m2m_ctx->job_flags = 0; |
| 525 | |
| 526 | spin_lock_irqsave(&q_ctx->rdy_spinlock, flags); |
| 527 | /* Drop queue, since streamoff returns device to the same state as after |
| 528 | * calling reqbufs. */ |
| 529 | INIT_LIST_HEAD(&q_ctx->rdy_queue); |
Philipp Zabel | 84e6809 | 2013-09-19 04:53:21 -0300 | [diff] [blame] | 530 | q_ctx->num_rdy = 0; |
John Sheu | 401f6a2 | 2013-02-06 20:03:01 -0300 | [diff] [blame] | 531 | spin_unlock_irqrestore(&q_ctx->rdy_spinlock, flags); |
| 532 | |
| 533 | if (m2m_dev->curr_ctx == m2m_ctx) { |
| 534 | m2m_dev->curr_ctx = NULL; |
| 535 | wake_up(&m2m_ctx->finished); |
| 536 | } |
| 537 | spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags_job); |
| 538 | |
| 539 | return 0; |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 540 | } |
| 541 | EXPORT_SYMBOL_GPL(v4l2_m2m_streamoff); |
| 542 | |
| 543 | /** |
| 544 | * v4l2_m2m_poll() - poll replacement, for destination buffers only |
| 545 | * |
| 546 | * Call from the driver's poll() function. Will poll both queues. If a buffer |
| 547 | * is available to dequeue (with dqbuf) from the source queue, this will |
| 548 | * indicate that a non-blocking write can be performed, while read will be |
| 549 | * returned in case of the destination queue. |
| 550 | */ |
| 551 | unsigned int v4l2_m2m_poll(struct file *file, struct v4l2_m2m_ctx *m2m_ctx, |
| 552 | struct poll_table_struct *wait) |
| 553 | { |
Hans Verkuil | 08eb851 | 2012-07-18 10:53:04 -0300 | [diff] [blame] | 554 | struct video_device *vfd = video_devdata(file); |
| 555 | unsigned long req_events = poll_requested_events(wait); |
Marek Szyprowski | 908a0d7 | 2011-01-12 06:50:24 -0300 | [diff] [blame] | 556 | struct vb2_queue *src_q, *dst_q; |
| 557 | struct vb2_buffer *src_vb = NULL, *dst_vb = NULL; |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 558 | unsigned int rc = 0; |
Marek Szyprowski | 908a0d7 | 2011-01-12 06:50:24 -0300 | [diff] [blame] | 559 | unsigned long flags; |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 560 | |
Hans Verkuil | 08eb851 | 2012-07-18 10:53:04 -0300 | [diff] [blame] | 561 | if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags)) { |
| 562 | struct v4l2_fh *fh = file->private_data; |
| 563 | |
| 564 | if (v4l2_event_pending(fh)) |
| 565 | rc = POLLPRI; |
| 566 | else if (req_events & POLLPRI) |
| 567 | poll_wait(file, &fh->wait, wait); |
| 568 | if (!(req_events & (POLLOUT | POLLWRNORM | POLLIN | POLLRDNORM))) |
| 569 | return rc; |
| 570 | } |
| 571 | |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 572 | src_q = v4l2_m2m_get_src_vq(m2m_ctx); |
| 573 | dst_q = v4l2_m2m_get_dst_vq(m2m_ctx); |
| 574 | |
Marek Szyprowski | 908a0d7 | 2011-01-12 06:50:24 -0300 | [diff] [blame] | 575 | /* |
| 576 | * There has to be at least one buffer queued on each queued_list, which |
| 577 | * means either in driver already or waiting for driver to claim it |
| 578 | * and start processing. |
| 579 | */ |
| 580 | if ((!src_q->streaming || list_empty(&src_q->queued_list)) |
| 581 | && (!dst_q->streaming || list_empty(&dst_q->queued_list))) { |
Hans Verkuil | 08eb851 | 2012-07-18 10:53:04 -0300 | [diff] [blame] | 582 | rc |= POLLERR; |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 583 | goto end; |
| 584 | } |
| 585 | |
Zahari Doychev | f1a81af | 2015-08-17 07:13:53 -0300 | [diff] [blame] | 586 | spin_lock_irqsave(&src_q->done_lock, flags); |
Seung-Woo Kim | 5718346 | 2013-05-20 23:47:30 -0300 | [diff] [blame] | 587 | if (list_empty(&src_q->done_list)) |
| 588 | poll_wait(file, &src_q->done_wq, wait); |
Zahari Doychev | f1a81af | 2015-08-17 07:13:53 -0300 | [diff] [blame] | 589 | spin_unlock_irqrestore(&src_q->done_lock, flags); |
| 590 | |
| 591 | spin_lock_irqsave(&dst_q->done_lock, flags); |
Philipp Zabel | c162184 | 2015-05-04 07:51:06 -0300 | [diff] [blame] | 592 | if (list_empty(&dst_q->done_list)) { |
| 593 | /* |
| 594 | * If the last buffer was dequeued from the capture queue, |
| 595 | * return immediately. DQBUF will return -EPIPE. |
| 596 | */ |
Zahari Doychev | f1a81af | 2015-08-17 07:13:53 -0300 | [diff] [blame] | 597 | if (dst_q->last_buffer_dequeued) { |
| 598 | spin_unlock_irqrestore(&dst_q->done_lock, flags); |
Philipp Zabel | c162184 | 2015-05-04 07:51:06 -0300 | [diff] [blame] | 599 | return rc | POLLIN | POLLRDNORM; |
Zahari Doychev | f1a81af | 2015-08-17 07:13:53 -0300 | [diff] [blame] | 600 | } |
Philipp Zabel | c162184 | 2015-05-04 07:51:06 -0300 | [diff] [blame] | 601 | |
Seung-Woo Kim | 5718346 | 2013-05-20 23:47:30 -0300 | [diff] [blame] | 602 | poll_wait(file, &dst_q->done_wq, wait); |
Philipp Zabel | c162184 | 2015-05-04 07:51:06 -0300 | [diff] [blame] | 603 | } |
Zahari Doychev | f1a81af | 2015-08-17 07:13:53 -0300 | [diff] [blame] | 604 | spin_unlock_irqrestore(&dst_q->done_lock, flags); |
Marek Szyprowski | 908a0d7 | 2011-01-12 06:50:24 -0300 | [diff] [blame] | 605 | |
| 606 | spin_lock_irqsave(&src_q->done_lock, flags); |
| 607 | if (!list_empty(&src_q->done_list)) |
| 608 | src_vb = list_first_entry(&src_q->done_list, struct vb2_buffer, |
| 609 | done_entry); |
| 610 | if (src_vb && (src_vb->state == VB2_BUF_STATE_DONE |
| 611 | || src_vb->state == VB2_BUF_STATE_ERROR)) |
| 612 | rc |= POLLOUT | POLLWRNORM; |
| 613 | spin_unlock_irqrestore(&src_q->done_lock, flags); |
| 614 | |
| 615 | spin_lock_irqsave(&dst_q->done_lock, flags); |
| 616 | if (!list_empty(&dst_q->done_list)) |
| 617 | dst_vb = list_first_entry(&dst_q->done_list, struct vb2_buffer, |
| 618 | done_entry); |
| 619 | if (dst_vb && (dst_vb->state == VB2_BUF_STATE_DONE |
| 620 | || dst_vb->state == VB2_BUF_STATE_ERROR)) |
| 621 | rc |= POLLIN | POLLRDNORM; |
| 622 | spin_unlock_irqrestore(&dst_q->done_lock, flags); |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 623 | |
| 624 | end: |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 625 | return rc; |
| 626 | } |
| 627 | EXPORT_SYMBOL_GPL(v4l2_m2m_poll); |
| 628 | |
| 629 | /** |
| 630 | * v4l2_m2m_mmap() - source and destination queues-aware mmap multiplexer |
| 631 | * |
| 632 | * Call from driver's mmap() function. Will handle mmap() for both queues |
| 633 | * seamlessly for videobuffer, which will receive normal per-queue offsets and |
| 634 | * proper videobuf queue pointers. The differentiation is made outside videobuf |
| 635 | * by adding a predefined offset to buffers from one of the queues and |
| 636 | * subtracting it before passing it back to videobuf. Only drivers (and |
| 637 | * thus applications) receive modified offsets. |
| 638 | */ |
| 639 | int v4l2_m2m_mmap(struct file *file, struct v4l2_m2m_ctx *m2m_ctx, |
| 640 | struct vm_area_struct *vma) |
| 641 | { |
| 642 | unsigned long offset = vma->vm_pgoff << PAGE_SHIFT; |
Marek Szyprowski | 908a0d7 | 2011-01-12 06:50:24 -0300 | [diff] [blame] | 643 | struct vb2_queue *vq; |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 644 | |
| 645 | if (offset < DST_QUEUE_OFF_BASE) { |
| 646 | vq = v4l2_m2m_get_src_vq(m2m_ctx); |
| 647 | } else { |
| 648 | vq = v4l2_m2m_get_dst_vq(m2m_ctx); |
| 649 | vma->vm_pgoff -= (DST_QUEUE_OFF_BASE >> PAGE_SHIFT); |
| 650 | } |
| 651 | |
Marek Szyprowski | 908a0d7 | 2011-01-12 06:50:24 -0300 | [diff] [blame] | 652 | return vb2_mmap(vq, vma); |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 653 | } |
| 654 | EXPORT_SYMBOL(v4l2_m2m_mmap); |
| 655 | |
| 656 | /** |
| 657 | * v4l2_m2m_init() - initialize per-driver m2m data |
| 658 | * |
| 659 | * Usually called from driver's probe() function. |
| 660 | */ |
Guennadi Liakhovetski | b1252eb | 2012-09-11 06:32:17 -0300 | [diff] [blame] | 661 | struct v4l2_m2m_dev *v4l2_m2m_init(const struct v4l2_m2m_ops *m2m_ops) |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 662 | { |
| 663 | struct v4l2_m2m_dev *m2m_dev; |
| 664 | |
Nicolas THERY | 3fac4eb | 2012-10-23 04:47:19 -0300 | [diff] [blame] | 665 | if (!m2m_ops || WARN_ON(!m2m_ops->device_run) || |
| 666 | WARN_ON(!m2m_ops->job_abort)) |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 667 | return ERR_PTR(-EINVAL); |
| 668 | |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 669 | m2m_dev = kzalloc(sizeof *m2m_dev, GFP_KERNEL); |
| 670 | if (!m2m_dev) |
| 671 | return ERR_PTR(-ENOMEM); |
| 672 | |
| 673 | m2m_dev->curr_ctx = NULL; |
| 674 | m2m_dev->m2m_ops = m2m_ops; |
| 675 | INIT_LIST_HEAD(&m2m_dev->job_queue); |
| 676 | spin_lock_init(&m2m_dev->job_spinlock); |
| 677 | |
| 678 | return m2m_dev; |
| 679 | } |
| 680 | EXPORT_SYMBOL_GPL(v4l2_m2m_init); |
| 681 | |
| 682 | /** |
| 683 | * v4l2_m2m_release() - cleans up and frees a m2m_dev structure |
| 684 | * |
| 685 | * Usually called from driver's remove() function. |
| 686 | */ |
| 687 | void v4l2_m2m_release(struct v4l2_m2m_dev *m2m_dev) |
| 688 | { |
| 689 | kfree(m2m_dev); |
| 690 | } |
| 691 | EXPORT_SYMBOL_GPL(v4l2_m2m_release); |
| 692 | |
| 693 | /** |
| 694 | * v4l2_m2m_ctx_init() - allocate and initialize a m2m context |
| 695 | * @priv - driver's instance private data |
| 696 | * @m2m_dev - a previously initialized m2m_dev struct |
| 697 | * @vq_init - a callback for queue type-specific initialization function to be |
| 698 | * used for initializing videobuf_queues |
| 699 | * |
| 700 | * Usually called from driver's open() function. |
| 701 | */ |
Marek Szyprowski | 908a0d7 | 2011-01-12 06:50:24 -0300 | [diff] [blame] | 702 | struct v4l2_m2m_ctx *v4l2_m2m_ctx_init(struct v4l2_m2m_dev *m2m_dev, |
| 703 | void *drv_priv, |
| 704 | int (*queue_init)(void *priv, struct vb2_queue *src_vq, struct vb2_queue *dst_vq)) |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 705 | { |
| 706 | struct v4l2_m2m_ctx *m2m_ctx; |
| 707 | struct v4l2_m2m_queue_ctx *out_q_ctx, *cap_q_ctx; |
Marek Szyprowski | 908a0d7 | 2011-01-12 06:50:24 -0300 | [diff] [blame] | 708 | int ret; |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 709 | |
| 710 | m2m_ctx = kzalloc(sizeof *m2m_ctx, GFP_KERNEL); |
| 711 | if (!m2m_ctx) |
| 712 | return ERR_PTR(-ENOMEM); |
| 713 | |
Marek Szyprowski | 908a0d7 | 2011-01-12 06:50:24 -0300 | [diff] [blame] | 714 | m2m_ctx->priv = drv_priv; |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 715 | m2m_ctx->m2m_dev = m2m_dev; |
Marek Szyprowski | 908a0d7 | 2011-01-12 06:50:24 -0300 | [diff] [blame] | 716 | init_waitqueue_head(&m2m_ctx->finished); |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 717 | |
Marek Szyprowski | 908a0d7 | 2011-01-12 06:50:24 -0300 | [diff] [blame] | 718 | out_q_ctx = &m2m_ctx->out_q_ctx; |
| 719 | cap_q_ctx = &m2m_ctx->cap_q_ctx; |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 720 | |
| 721 | INIT_LIST_HEAD(&out_q_ctx->rdy_queue); |
| 722 | INIT_LIST_HEAD(&cap_q_ctx->rdy_queue); |
Marek Szyprowski | 908a0d7 | 2011-01-12 06:50:24 -0300 | [diff] [blame] | 723 | spin_lock_init(&out_q_ctx->rdy_spinlock); |
| 724 | spin_lock_init(&cap_q_ctx->rdy_spinlock); |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 725 | |
| 726 | INIT_LIST_HEAD(&m2m_ctx->queue); |
| 727 | |
Marek Szyprowski | 908a0d7 | 2011-01-12 06:50:24 -0300 | [diff] [blame] | 728 | ret = queue_init(drv_priv, &out_q_ctx->q, &cap_q_ctx->q); |
| 729 | |
| 730 | if (ret) |
| 731 | goto err; |
Sylwester Nawrocki | 8e6e8f9 | 2013-09-14 18:39:04 -0300 | [diff] [blame] | 732 | /* |
| 733 | * If both queues use same mutex assign it as the common buffer |
| 734 | * queues lock to the m2m context. This lock is used in the |
| 735 | * v4l2_m2m_ioctl_* helpers. |
| 736 | */ |
| 737 | if (out_q_ctx->q.lock == cap_q_ctx->q.lock) |
| 738 | m2m_ctx->q_lock = out_q_ctx->q.lock; |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 739 | |
| 740 | return m2m_ctx; |
Marek Szyprowski | 908a0d7 | 2011-01-12 06:50:24 -0300 | [diff] [blame] | 741 | err: |
| 742 | kfree(m2m_ctx); |
| 743 | return ERR_PTR(ret); |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 744 | } |
| 745 | EXPORT_SYMBOL_GPL(v4l2_m2m_ctx_init); |
| 746 | |
| 747 | /** |
| 748 | * v4l2_m2m_ctx_release() - release m2m context |
| 749 | * |
| 750 | * Usually called from driver's release() function. |
| 751 | */ |
| 752 | void v4l2_m2m_ctx_release(struct v4l2_m2m_ctx *m2m_ctx) |
| 753 | { |
Shaik Ameer Basha | fea564a | 2013-08-13 02:58:07 -0300 | [diff] [blame] | 754 | /* wait until the current context is dequeued from job_queue */ |
| 755 | v4l2_m2m_cancel_job(m2m_ctx); |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 756 | |
Marek Szyprowski | 908a0d7 | 2011-01-12 06:50:24 -0300 | [diff] [blame] | 757 | vb2_queue_release(&m2m_ctx->cap_q_ctx.q); |
| 758 | vb2_queue_release(&m2m_ctx->out_q_ctx.q); |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 759 | |
| 760 | kfree(m2m_ctx); |
| 761 | } |
| 762 | EXPORT_SYMBOL_GPL(v4l2_m2m_ctx_release); |
| 763 | |
| 764 | /** |
| 765 | * v4l2_m2m_buf_queue() - add a buffer to the proper ready buffers list. |
| 766 | * |
| 767 | * Call from buf_queue(), videobuf_queue_ops callback. |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 768 | */ |
Marek Szyprowski | 908a0d7 | 2011-01-12 06:50:24 -0300 | [diff] [blame] | 769 | void v4l2_m2m_buf_queue(struct v4l2_m2m_ctx *m2m_ctx, struct vb2_buffer *vb) |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 770 | { |
Marek Szyprowski | 908a0d7 | 2011-01-12 06:50:24 -0300 | [diff] [blame] | 771 | struct v4l2_m2m_buffer *b = container_of(vb, struct v4l2_m2m_buffer, vb); |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 772 | struct v4l2_m2m_queue_ctx *q_ctx; |
Marek Szyprowski | 908a0d7 | 2011-01-12 06:50:24 -0300 | [diff] [blame] | 773 | unsigned long flags; |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 774 | |
Marek Szyprowski | 908a0d7 | 2011-01-12 06:50:24 -0300 | [diff] [blame] | 775 | q_ctx = get_queue_ctx(m2m_ctx, vb->vb2_queue->type); |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 776 | if (!q_ctx) |
| 777 | return; |
| 778 | |
Marek Szyprowski | 908a0d7 | 2011-01-12 06:50:24 -0300 | [diff] [blame] | 779 | spin_lock_irqsave(&q_ctx->rdy_spinlock, flags); |
| 780 | list_add_tail(&b->list, &q_ctx->rdy_queue); |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 781 | q_ctx->num_rdy++; |
Marek Szyprowski | 908a0d7 | 2011-01-12 06:50:24 -0300 | [diff] [blame] | 782 | spin_unlock_irqrestore(&q_ctx->rdy_spinlock, flags); |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 783 | } |
| 784 | EXPORT_SYMBOL_GPL(v4l2_m2m_buf_queue); |
| 785 | |
Sylwester Nawrocki | 8e6e8f9 | 2013-09-14 18:39:04 -0300 | [diff] [blame] | 786 | /* Videobuf2 ioctl helpers */ |
| 787 | |
| 788 | int v4l2_m2m_ioctl_reqbufs(struct file *file, void *priv, |
| 789 | struct v4l2_requestbuffers *rb) |
| 790 | { |
| 791 | struct v4l2_fh *fh = file->private_data; |
| 792 | |
| 793 | return v4l2_m2m_reqbufs(file, fh->m2m_ctx, rb); |
| 794 | } |
| 795 | EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_reqbufs); |
| 796 | |
| 797 | int v4l2_m2m_ioctl_create_bufs(struct file *file, void *priv, |
| 798 | struct v4l2_create_buffers *create) |
| 799 | { |
| 800 | struct v4l2_fh *fh = file->private_data; |
| 801 | |
| 802 | return v4l2_m2m_create_bufs(file, fh->m2m_ctx, create); |
| 803 | } |
| 804 | EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_create_bufs); |
| 805 | |
| 806 | int v4l2_m2m_ioctl_querybuf(struct file *file, void *priv, |
| 807 | struct v4l2_buffer *buf) |
| 808 | { |
| 809 | struct v4l2_fh *fh = file->private_data; |
| 810 | |
| 811 | return v4l2_m2m_querybuf(file, fh->m2m_ctx, buf); |
| 812 | } |
| 813 | EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_querybuf); |
| 814 | |
| 815 | int v4l2_m2m_ioctl_qbuf(struct file *file, void *priv, |
| 816 | struct v4l2_buffer *buf) |
| 817 | { |
| 818 | struct v4l2_fh *fh = file->private_data; |
| 819 | |
| 820 | return v4l2_m2m_qbuf(file, fh->m2m_ctx, buf); |
| 821 | } |
| 822 | EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_qbuf); |
| 823 | |
| 824 | int v4l2_m2m_ioctl_dqbuf(struct file *file, void *priv, |
| 825 | struct v4l2_buffer *buf) |
| 826 | { |
| 827 | struct v4l2_fh *fh = file->private_data; |
| 828 | |
| 829 | return v4l2_m2m_dqbuf(file, fh->m2m_ctx, buf); |
| 830 | } |
| 831 | EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_dqbuf); |
| 832 | |
Hans Verkuil | e68cf47 | 2015-06-05 11:28:50 -0300 | [diff] [blame] | 833 | int v4l2_m2m_ioctl_prepare_buf(struct file *file, void *priv, |
| 834 | struct v4l2_buffer *buf) |
| 835 | { |
| 836 | struct v4l2_fh *fh = file->private_data; |
| 837 | |
| 838 | return v4l2_m2m_prepare_buf(file, fh->m2m_ctx, buf); |
| 839 | } |
| 840 | EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_prepare_buf); |
| 841 | |
Sylwester Nawrocki | 8e6e8f9 | 2013-09-14 18:39:04 -0300 | [diff] [blame] | 842 | int v4l2_m2m_ioctl_expbuf(struct file *file, void *priv, |
| 843 | struct v4l2_exportbuffer *eb) |
| 844 | { |
| 845 | struct v4l2_fh *fh = file->private_data; |
| 846 | |
| 847 | return v4l2_m2m_expbuf(file, fh->m2m_ctx, eb); |
| 848 | } |
| 849 | EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_expbuf); |
| 850 | |
| 851 | int v4l2_m2m_ioctl_streamon(struct file *file, void *priv, |
| 852 | enum v4l2_buf_type type) |
| 853 | { |
| 854 | struct v4l2_fh *fh = file->private_data; |
| 855 | |
| 856 | return v4l2_m2m_streamon(file, fh->m2m_ctx, type); |
| 857 | } |
| 858 | EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_streamon); |
| 859 | |
| 860 | int v4l2_m2m_ioctl_streamoff(struct file *file, void *priv, |
| 861 | enum v4l2_buf_type type) |
| 862 | { |
| 863 | struct v4l2_fh *fh = file->private_data; |
| 864 | |
| 865 | return v4l2_m2m_streamoff(file, fh->m2m_ctx, type); |
| 866 | } |
| 867 | EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_streamoff); |
| 868 | |
| 869 | /* |
| 870 | * v4l2_file_operations helpers. It is assumed here same lock is used |
| 871 | * for the output and the capture buffer queue. |
| 872 | */ |
| 873 | |
| 874 | int v4l2_m2m_fop_mmap(struct file *file, struct vm_area_struct *vma) |
| 875 | { |
| 876 | struct v4l2_fh *fh = file->private_data; |
Sylwester Nawrocki | 8e6e8f9 | 2013-09-14 18:39:04 -0300 | [diff] [blame] | 877 | |
Hans Verkuil | e752577 | 2015-07-20 04:58:24 -0300 | [diff] [blame] | 878 | return v4l2_m2m_mmap(file, fh->m2m_ctx, vma); |
Sylwester Nawrocki | 8e6e8f9 | 2013-09-14 18:39:04 -0300 | [diff] [blame] | 879 | } |
| 880 | EXPORT_SYMBOL_GPL(v4l2_m2m_fop_mmap); |
| 881 | |
| 882 | unsigned int v4l2_m2m_fop_poll(struct file *file, poll_table *wait) |
| 883 | { |
| 884 | struct v4l2_fh *fh = file->private_data; |
| 885 | struct v4l2_m2m_ctx *m2m_ctx = fh->m2m_ctx; |
| 886 | unsigned int ret; |
| 887 | |
| 888 | if (m2m_ctx->q_lock) |
| 889 | mutex_lock(m2m_ctx->q_lock); |
| 890 | |
| 891 | ret = v4l2_m2m_poll(file, m2m_ctx, wait); |
| 892 | |
| 893 | if (m2m_ctx->q_lock) |
| 894 | mutex_unlock(m2m_ctx->q_lock); |
| 895 | |
| 896 | return ret; |
| 897 | } |
| 898 | EXPORT_SYMBOL_GPL(v4l2_m2m_fop_poll); |
| 899 | |