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 | |
Marek Szyprowski | 908a0d7 | 2011-01-12 06:50:24 -0300 | [diff] [blame] | 20 | #include <media/videobuf2-core.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 | { |
Marek Szyprowski | 908a0d7 | 2011-01-12 06:50:24 -0300 | [diff] [blame] | 100 | struct v4l2_m2m_buffer *b = NULL; |
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 | { |
Marek Szyprowski | 908a0d7 | 2011-01-12 06:50:24 -0300 | [diff] [blame] | 122 | struct v4l2_m2m_buffer *b = NULL; |
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 | */ |
| 211 | static void v4l2_m2m_try_schedule(struct v4l2_m2m_ctx *m2m_ctx) |
| 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 | } |
| 277 | |
| 278 | /** |
Shaik Ameer Basha | fea564a | 2013-08-13 02:58:07 -0300 | [diff] [blame] | 279 | * v4l2_m2m_cancel_job() - cancel pending jobs for the context |
| 280 | * |
| 281 | * In case of streamoff or release called on any context, |
| 282 | * 1] If the context is currently running, then abort job will be called |
| 283 | * 2] If the context is queued, then the context will be removed from |
| 284 | * the job_queue |
| 285 | */ |
| 286 | static void v4l2_m2m_cancel_job(struct v4l2_m2m_ctx *m2m_ctx) |
| 287 | { |
| 288 | struct v4l2_m2m_dev *m2m_dev; |
| 289 | unsigned long flags; |
| 290 | |
| 291 | m2m_dev = m2m_ctx->m2m_dev; |
| 292 | spin_lock_irqsave(&m2m_dev->job_spinlock, flags); |
Shaik Ameer Basha | 2ad5389 | 2013-09-20 03:26:18 -0300 | [diff] [blame] | 293 | |
| 294 | m2m_ctx->job_flags |= TRANS_ABORT; |
Shaik Ameer Basha | fea564a | 2013-08-13 02:58:07 -0300 | [diff] [blame] | 295 | if (m2m_ctx->job_flags & TRANS_RUNNING) { |
| 296 | spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags); |
| 297 | m2m_dev->m2m_ops->job_abort(m2m_ctx->priv); |
| 298 | dprintk("m2m_ctx %p running, will wait to complete", m2m_ctx); |
| 299 | wait_event(m2m_ctx->finished, |
| 300 | !(m2m_ctx->job_flags & TRANS_RUNNING)); |
| 301 | } else if (m2m_ctx->job_flags & TRANS_QUEUED) { |
| 302 | list_del(&m2m_ctx->queue); |
| 303 | m2m_ctx->job_flags &= ~(TRANS_QUEUED | TRANS_RUNNING); |
| 304 | spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags); |
| 305 | dprintk("m2m_ctx: %p had been on queue and was removed\n", |
| 306 | m2m_ctx); |
| 307 | } else { |
| 308 | /* Do nothing, was not on queue/running */ |
| 309 | spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags); |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | /** |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 314 | * v4l2_m2m_job_finish() - inform the framework that a job has been finished |
| 315 | * and have it clean up |
| 316 | * |
| 317 | * Called by a driver to yield back the device after it has finished with it. |
| 318 | * Should be called as soon as possible after reaching a state which allows |
| 319 | * other instances to take control of the device. |
| 320 | * |
| 321 | * This function has to be called only after device_run() callback has been |
| 322 | * called on the driver. To prevent recursion, it should not be called directly |
| 323 | * from the device_run() callback though. |
| 324 | */ |
| 325 | void v4l2_m2m_job_finish(struct v4l2_m2m_dev *m2m_dev, |
| 326 | struct v4l2_m2m_ctx *m2m_ctx) |
| 327 | { |
| 328 | unsigned long flags; |
| 329 | |
| 330 | spin_lock_irqsave(&m2m_dev->job_spinlock, flags); |
| 331 | if (!m2m_dev->curr_ctx || m2m_dev->curr_ctx != m2m_ctx) { |
| 332 | spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags); |
| 333 | dprintk("Called by an instance not currently running\n"); |
| 334 | return; |
| 335 | } |
| 336 | |
| 337 | list_del(&m2m_dev->curr_ctx->queue); |
| 338 | m2m_dev->curr_ctx->job_flags &= ~(TRANS_QUEUED | TRANS_RUNNING); |
Marek Szyprowski | 908a0d7 | 2011-01-12 06:50:24 -0300 | [diff] [blame] | 339 | wake_up(&m2m_dev->curr_ctx->finished); |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 340 | m2m_dev->curr_ctx = NULL; |
| 341 | |
| 342 | spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags); |
| 343 | |
| 344 | /* This instance might have more buffers ready, but since we do not |
| 345 | * allow more than one job on the job_queue per instance, each has |
| 346 | * to be scheduled separately after the previous one finishes. */ |
| 347 | v4l2_m2m_try_schedule(m2m_ctx); |
| 348 | v4l2_m2m_try_run(m2m_dev); |
| 349 | } |
| 350 | EXPORT_SYMBOL(v4l2_m2m_job_finish); |
| 351 | |
| 352 | /** |
| 353 | * v4l2_m2m_reqbufs() - multi-queue-aware REQBUFS multiplexer |
| 354 | */ |
| 355 | int v4l2_m2m_reqbufs(struct file *file, struct v4l2_m2m_ctx *m2m_ctx, |
| 356 | struct v4l2_requestbuffers *reqbufs) |
| 357 | { |
Marek Szyprowski | 908a0d7 | 2011-01-12 06:50:24 -0300 | [diff] [blame] | 358 | struct vb2_queue *vq; |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 359 | |
| 360 | vq = v4l2_m2m_get_vq(m2m_ctx, reqbufs->type); |
Marek Szyprowski | 908a0d7 | 2011-01-12 06:50:24 -0300 | [diff] [blame] | 361 | return vb2_reqbufs(vq, reqbufs); |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 362 | } |
| 363 | EXPORT_SYMBOL_GPL(v4l2_m2m_reqbufs); |
| 364 | |
| 365 | /** |
| 366 | * v4l2_m2m_querybuf() - multi-queue-aware QUERYBUF multiplexer |
| 367 | * |
| 368 | * See v4l2_m2m_mmap() documentation for details. |
| 369 | */ |
| 370 | int v4l2_m2m_querybuf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx, |
| 371 | struct v4l2_buffer *buf) |
| 372 | { |
Marek Szyprowski | 908a0d7 | 2011-01-12 06:50:24 -0300 | [diff] [blame] | 373 | struct vb2_queue *vq; |
| 374 | int ret = 0; |
| 375 | unsigned int i; |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 376 | |
| 377 | vq = v4l2_m2m_get_vq(m2m_ctx, buf->type); |
Marek Szyprowski | 908a0d7 | 2011-01-12 06:50:24 -0300 | [diff] [blame] | 378 | ret = vb2_querybuf(vq, buf); |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 379 | |
Marek Szyprowski | 908a0d7 | 2011-01-12 06:50:24 -0300 | [diff] [blame] | 380 | /* Adjust MMAP memory offsets for the CAPTURE queue */ |
| 381 | if (buf->memory == V4L2_MEMORY_MMAP && !V4L2_TYPE_IS_OUTPUT(vq->type)) { |
| 382 | if (V4L2_TYPE_IS_MULTIPLANAR(vq->type)) { |
| 383 | for (i = 0; i < buf->length; ++i) |
| 384 | buf->m.planes[i].m.mem_offset |
| 385 | += DST_QUEUE_OFF_BASE; |
| 386 | } else { |
| 387 | buf->m.offset += DST_QUEUE_OFF_BASE; |
| 388 | } |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | return ret; |
| 392 | } |
| 393 | EXPORT_SYMBOL_GPL(v4l2_m2m_querybuf); |
| 394 | |
| 395 | /** |
| 396 | * v4l2_m2m_qbuf() - enqueue a source or destination buffer, depending on |
| 397 | * the type |
| 398 | */ |
| 399 | int v4l2_m2m_qbuf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx, |
| 400 | struct v4l2_buffer *buf) |
| 401 | { |
Marek Szyprowski | 908a0d7 | 2011-01-12 06:50:24 -0300 | [diff] [blame] | 402 | struct vb2_queue *vq; |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 403 | int ret; |
| 404 | |
| 405 | vq = v4l2_m2m_get_vq(m2m_ctx, buf->type); |
Marek Szyprowski | 908a0d7 | 2011-01-12 06:50:24 -0300 | [diff] [blame] | 406 | ret = vb2_qbuf(vq, buf); |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 407 | if (!ret) |
| 408 | v4l2_m2m_try_schedule(m2m_ctx); |
| 409 | |
| 410 | return ret; |
| 411 | } |
| 412 | EXPORT_SYMBOL_GPL(v4l2_m2m_qbuf); |
| 413 | |
| 414 | /** |
| 415 | * v4l2_m2m_dqbuf() - dequeue a source or destination buffer, depending on |
| 416 | * the type |
| 417 | */ |
| 418 | int v4l2_m2m_dqbuf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx, |
| 419 | struct v4l2_buffer *buf) |
| 420 | { |
Marek Szyprowski | 908a0d7 | 2011-01-12 06:50:24 -0300 | [diff] [blame] | 421 | struct vb2_queue *vq; |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 422 | |
| 423 | vq = v4l2_m2m_get_vq(m2m_ctx, buf->type); |
Marek Szyprowski | 908a0d7 | 2011-01-12 06:50:24 -0300 | [diff] [blame] | 424 | return vb2_dqbuf(vq, buf, file->f_flags & O_NONBLOCK); |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 425 | } |
| 426 | EXPORT_SYMBOL_GPL(v4l2_m2m_dqbuf); |
| 427 | |
| 428 | /** |
Philipp Zabel | 8b94ca6 | 2013-05-21 04:16:28 -0300 | [diff] [blame] | 429 | * v4l2_m2m_create_bufs() - create a source or destination buffer, depending |
| 430 | * on the type |
| 431 | */ |
| 432 | int v4l2_m2m_create_bufs(struct file *file, struct v4l2_m2m_ctx *m2m_ctx, |
| 433 | struct v4l2_create_buffers *create) |
| 434 | { |
| 435 | struct vb2_queue *vq; |
| 436 | |
| 437 | vq = v4l2_m2m_get_vq(m2m_ctx, create->format.type); |
| 438 | return vb2_create_bufs(vq, create); |
| 439 | } |
| 440 | EXPORT_SYMBOL_GPL(v4l2_m2m_create_bufs); |
| 441 | |
| 442 | /** |
Tomasz Stanislawski | 83ae7c5 | 2012-06-14 11:32:24 -0300 | [diff] [blame] | 443 | * v4l2_m2m_expbuf() - export a source or destination buffer, depending on |
| 444 | * the type |
| 445 | */ |
| 446 | int v4l2_m2m_expbuf(struct file *file, struct v4l2_m2m_ctx *m2m_ctx, |
| 447 | struct v4l2_exportbuffer *eb) |
| 448 | { |
| 449 | struct vb2_queue *vq; |
| 450 | |
| 451 | vq = v4l2_m2m_get_vq(m2m_ctx, eb->type); |
| 452 | return vb2_expbuf(vq, eb); |
| 453 | } |
| 454 | EXPORT_SYMBOL_GPL(v4l2_m2m_expbuf); |
| 455 | /** |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 456 | * v4l2_m2m_streamon() - turn on streaming for a video queue |
| 457 | */ |
| 458 | int v4l2_m2m_streamon(struct file *file, struct v4l2_m2m_ctx *m2m_ctx, |
| 459 | enum v4l2_buf_type type) |
| 460 | { |
Marek Szyprowski | 908a0d7 | 2011-01-12 06:50:24 -0300 | [diff] [blame] | 461 | struct vb2_queue *vq; |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 462 | int ret; |
| 463 | |
| 464 | vq = v4l2_m2m_get_vq(m2m_ctx, type); |
Marek Szyprowski | 908a0d7 | 2011-01-12 06:50:24 -0300 | [diff] [blame] | 465 | ret = vb2_streamon(vq, type); |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 466 | if (!ret) |
| 467 | v4l2_m2m_try_schedule(m2m_ctx); |
| 468 | |
| 469 | return ret; |
| 470 | } |
| 471 | EXPORT_SYMBOL_GPL(v4l2_m2m_streamon); |
| 472 | |
| 473 | /** |
| 474 | * v4l2_m2m_streamoff() - turn off streaming for a video queue |
| 475 | */ |
| 476 | int v4l2_m2m_streamoff(struct file *file, struct v4l2_m2m_ctx *m2m_ctx, |
| 477 | enum v4l2_buf_type type) |
| 478 | { |
John Sheu | 401f6a2 | 2013-02-06 20:03:01 -0300 | [diff] [blame] | 479 | struct v4l2_m2m_dev *m2m_dev; |
| 480 | struct v4l2_m2m_queue_ctx *q_ctx; |
| 481 | unsigned long flags_job, flags; |
| 482 | int ret; |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 483 | |
Shaik Ameer Basha | fea564a | 2013-08-13 02:58:07 -0300 | [diff] [blame] | 484 | /* wait until the current context is dequeued from job_queue */ |
| 485 | v4l2_m2m_cancel_job(m2m_ctx); |
| 486 | |
John Sheu | 401f6a2 | 2013-02-06 20:03:01 -0300 | [diff] [blame] | 487 | q_ctx = get_queue_ctx(m2m_ctx, type); |
| 488 | ret = vb2_streamoff(&q_ctx->q, type); |
| 489 | if (ret) |
| 490 | return ret; |
| 491 | |
| 492 | m2m_dev = m2m_ctx->m2m_dev; |
| 493 | spin_lock_irqsave(&m2m_dev->job_spinlock, flags_job); |
| 494 | /* We should not be scheduled anymore, since we're dropping a queue. */ |
Philipp Zabel | d7bb0ce8 | 2013-09-19 04:40:32 -0300 | [diff] [blame] | 495 | if (m2m_ctx->job_flags & TRANS_QUEUED) |
| 496 | list_del(&m2m_ctx->queue); |
John Sheu | 401f6a2 | 2013-02-06 20:03:01 -0300 | [diff] [blame] | 497 | m2m_ctx->job_flags = 0; |
| 498 | |
| 499 | spin_lock_irqsave(&q_ctx->rdy_spinlock, flags); |
| 500 | /* Drop queue, since streamoff returns device to the same state as after |
| 501 | * calling reqbufs. */ |
| 502 | INIT_LIST_HEAD(&q_ctx->rdy_queue); |
Philipp Zabel | 84e6809 | 2013-09-19 04:53:21 -0300 | [diff] [blame] | 503 | q_ctx->num_rdy = 0; |
John Sheu | 401f6a2 | 2013-02-06 20:03:01 -0300 | [diff] [blame] | 504 | spin_unlock_irqrestore(&q_ctx->rdy_spinlock, flags); |
| 505 | |
| 506 | if (m2m_dev->curr_ctx == m2m_ctx) { |
| 507 | m2m_dev->curr_ctx = NULL; |
| 508 | wake_up(&m2m_ctx->finished); |
| 509 | } |
| 510 | spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags_job); |
| 511 | |
| 512 | return 0; |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 513 | } |
| 514 | EXPORT_SYMBOL_GPL(v4l2_m2m_streamoff); |
| 515 | |
| 516 | /** |
| 517 | * v4l2_m2m_poll() - poll replacement, for destination buffers only |
| 518 | * |
| 519 | * Call from the driver's poll() function. Will poll both queues. If a buffer |
| 520 | * is available to dequeue (with dqbuf) from the source queue, this will |
| 521 | * indicate that a non-blocking write can be performed, while read will be |
| 522 | * returned in case of the destination queue. |
| 523 | */ |
| 524 | unsigned int v4l2_m2m_poll(struct file *file, struct v4l2_m2m_ctx *m2m_ctx, |
| 525 | struct poll_table_struct *wait) |
| 526 | { |
Hans Verkuil | 08eb851 | 2012-07-18 10:53:04 -0300 | [diff] [blame] | 527 | struct video_device *vfd = video_devdata(file); |
| 528 | unsigned long req_events = poll_requested_events(wait); |
Marek Szyprowski | 908a0d7 | 2011-01-12 06:50:24 -0300 | [diff] [blame] | 529 | struct vb2_queue *src_q, *dst_q; |
| 530 | struct vb2_buffer *src_vb = NULL, *dst_vb = NULL; |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 531 | unsigned int rc = 0; |
Marek Szyprowski | 908a0d7 | 2011-01-12 06:50:24 -0300 | [diff] [blame] | 532 | unsigned long flags; |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 533 | |
Hans Verkuil | 08eb851 | 2012-07-18 10:53:04 -0300 | [diff] [blame] | 534 | if (test_bit(V4L2_FL_USES_V4L2_FH, &vfd->flags)) { |
| 535 | struct v4l2_fh *fh = file->private_data; |
| 536 | |
| 537 | if (v4l2_event_pending(fh)) |
| 538 | rc = POLLPRI; |
| 539 | else if (req_events & POLLPRI) |
| 540 | poll_wait(file, &fh->wait, wait); |
| 541 | if (!(req_events & (POLLOUT | POLLWRNORM | POLLIN | POLLRDNORM))) |
| 542 | return rc; |
| 543 | } |
| 544 | |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 545 | src_q = v4l2_m2m_get_src_vq(m2m_ctx); |
| 546 | dst_q = v4l2_m2m_get_dst_vq(m2m_ctx); |
| 547 | |
Marek Szyprowski | 908a0d7 | 2011-01-12 06:50:24 -0300 | [diff] [blame] | 548 | /* |
| 549 | * There has to be at least one buffer queued on each queued_list, which |
| 550 | * means either in driver already or waiting for driver to claim it |
| 551 | * and start processing. |
| 552 | */ |
| 553 | if ((!src_q->streaming || list_empty(&src_q->queued_list)) |
| 554 | && (!dst_q->streaming || list_empty(&dst_q->queued_list))) { |
Hans Verkuil | 08eb851 | 2012-07-18 10:53:04 -0300 | [diff] [blame] | 555 | rc |= POLLERR; |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 556 | goto end; |
| 557 | } |
| 558 | |
Marek Szyprowski | 908a0d7 | 2011-01-12 06:50:24 -0300 | [diff] [blame] | 559 | if (m2m_ctx->m2m_dev->m2m_ops->unlock) |
| 560 | m2m_ctx->m2m_dev->m2m_ops->unlock(m2m_ctx->priv); |
Sylwester Nawrocki | 8e6e8f9 | 2013-09-14 18:39:04 -0300 | [diff] [blame] | 561 | else if (m2m_ctx->q_lock) |
| 562 | mutex_unlock(m2m_ctx->q_lock); |
Marek Szyprowski | 908a0d7 | 2011-01-12 06:50:24 -0300 | [diff] [blame] | 563 | |
Seung-Woo Kim | 5718346 | 2013-05-20 23:47:30 -0300 | [diff] [blame] | 564 | if (list_empty(&src_q->done_list)) |
| 565 | poll_wait(file, &src_q->done_wq, wait); |
| 566 | if (list_empty(&dst_q->done_list)) |
| 567 | poll_wait(file, &dst_q->done_wq, wait); |
Marek Szyprowski | 908a0d7 | 2011-01-12 06:50:24 -0300 | [diff] [blame] | 568 | |
| 569 | if (m2m_ctx->m2m_dev->m2m_ops->lock) |
| 570 | m2m_ctx->m2m_dev->m2m_ops->lock(m2m_ctx->priv); |
Sylwester Nawrocki | 8e6e8f9 | 2013-09-14 18:39:04 -0300 | [diff] [blame] | 571 | else if (m2m_ctx->q_lock) |
| 572 | mutex_lock(m2m_ctx->q_lock); |
Marek Szyprowski | 908a0d7 | 2011-01-12 06:50:24 -0300 | [diff] [blame] | 573 | |
| 574 | spin_lock_irqsave(&src_q->done_lock, flags); |
| 575 | if (!list_empty(&src_q->done_list)) |
| 576 | src_vb = list_first_entry(&src_q->done_list, struct vb2_buffer, |
| 577 | done_entry); |
| 578 | if (src_vb && (src_vb->state == VB2_BUF_STATE_DONE |
| 579 | || src_vb->state == VB2_BUF_STATE_ERROR)) |
| 580 | rc |= POLLOUT | POLLWRNORM; |
| 581 | spin_unlock_irqrestore(&src_q->done_lock, flags); |
| 582 | |
| 583 | spin_lock_irqsave(&dst_q->done_lock, flags); |
| 584 | if (!list_empty(&dst_q->done_list)) |
| 585 | dst_vb = list_first_entry(&dst_q->done_list, struct vb2_buffer, |
| 586 | done_entry); |
| 587 | if (dst_vb && (dst_vb->state == VB2_BUF_STATE_DONE |
| 588 | || dst_vb->state == VB2_BUF_STATE_ERROR)) |
| 589 | rc |= POLLIN | POLLRDNORM; |
| 590 | spin_unlock_irqrestore(&dst_q->done_lock, flags); |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 591 | |
| 592 | end: |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 593 | return rc; |
| 594 | } |
| 595 | EXPORT_SYMBOL_GPL(v4l2_m2m_poll); |
| 596 | |
| 597 | /** |
| 598 | * v4l2_m2m_mmap() - source and destination queues-aware mmap multiplexer |
| 599 | * |
| 600 | * Call from driver's mmap() function. Will handle mmap() for both queues |
| 601 | * seamlessly for videobuffer, which will receive normal per-queue offsets and |
| 602 | * proper videobuf queue pointers. The differentiation is made outside videobuf |
| 603 | * by adding a predefined offset to buffers from one of the queues and |
| 604 | * subtracting it before passing it back to videobuf. Only drivers (and |
| 605 | * thus applications) receive modified offsets. |
| 606 | */ |
| 607 | int v4l2_m2m_mmap(struct file *file, struct v4l2_m2m_ctx *m2m_ctx, |
| 608 | struct vm_area_struct *vma) |
| 609 | { |
| 610 | unsigned long offset = vma->vm_pgoff << PAGE_SHIFT; |
Marek Szyprowski | 908a0d7 | 2011-01-12 06:50:24 -0300 | [diff] [blame] | 611 | struct vb2_queue *vq; |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 612 | |
| 613 | if (offset < DST_QUEUE_OFF_BASE) { |
| 614 | vq = v4l2_m2m_get_src_vq(m2m_ctx); |
| 615 | } else { |
| 616 | vq = v4l2_m2m_get_dst_vq(m2m_ctx); |
| 617 | vma->vm_pgoff -= (DST_QUEUE_OFF_BASE >> PAGE_SHIFT); |
| 618 | } |
| 619 | |
Marek Szyprowski | 908a0d7 | 2011-01-12 06:50:24 -0300 | [diff] [blame] | 620 | return vb2_mmap(vq, vma); |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 621 | } |
| 622 | EXPORT_SYMBOL(v4l2_m2m_mmap); |
| 623 | |
| 624 | /** |
| 625 | * v4l2_m2m_init() - initialize per-driver m2m data |
| 626 | * |
| 627 | * Usually called from driver's probe() function. |
| 628 | */ |
Guennadi Liakhovetski | b1252eb | 2012-09-11 06:32:17 -0300 | [diff] [blame] | 629 | 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] | 630 | { |
| 631 | struct v4l2_m2m_dev *m2m_dev; |
| 632 | |
Nicolas THERY | 3fac4eb | 2012-10-23 04:47:19 -0300 | [diff] [blame] | 633 | if (!m2m_ops || WARN_ON(!m2m_ops->device_run) || |
| 634 | WARN_ON(!m2m_ops->job_abort)) |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 635 | return ERR_PTR(-EINVAL); |
| 636 | |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 637 | m2m_dev = kzalloc(sizeof *m2m_dev, GFP_KERNEL); |
| 638 | if (!m2m_dev) |
| 639 | return ERR_PTR(-ENOMEM); |
| 640 | |
| 641 | m2m_dev->curr_ctx = NULL; |
| 642 | m2m_dev->m2m_ops = m2m_ops; |
| 643 | INIT_LIST_HEAD(&m2m_dev->job_queue); |
| 644 | spin_lock_init(&m2m_dev->job_spinlock); |
| 645 | |
| 646 | return m2m_dev; |
| 647 | } |
| 648 | EXPORT_SYMBOL_GPL(v4l2_m2m_init); |
| 649 | |
| 650 | /** |
| 651 | * v4l2_m2m_release() - cleans up and frees a m2m_dev structure |
| 652 | * |
| 653 | * Usually called from driver's remove() function. |
| 654 | */ |
| 655 | void v4l2_m2m_release(struct v4l2_m2m_dev *m2m_dev) |
| 656 | { |
| 657 | kfree(m2m_dev); |
| 658 | } |
| 659 | EXPORT_SYMBOL_GPL(v4l2_m2m_release); |
| 660 | |
| 661 | /** |
| 662 | * v4l2_m2m_ctx_init() - allocate and initialize a m2m context |
| 663 | * @priv - driver's instance private data |
| 664 | * @m2m_dev - a previously initialized m2m_dev struct |
| 665 | * @vq_init - a callback for queue type-specific initialization function to be |
| 666 | * used for initializing videobuf_queues |
| 667 | * |
| 668 | * Usually called from driver's open() function. |
| 669 | */ |
Marek Szyprowski | 908a0d7 | 2011-01-12 06:50:24 -0300 | [diff] [blame] | 670 | struct v4l2_m2m_ctx *v4l2_m2m_ctx_init(struct v4l2_m2m_dev *m2m_dev, |
| 671 | void *drv_priv, |
| 672 | 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] | 673 | { |
| 674 | struct v4l2_m2m_ctx *m2m_ctx; |
| 675 | struct v4l2_m2m_queue_ctx *out_q_ctx, *cap_q_ctx; |
Marek Szyprowski | 908a0d7 | 2011-01-12 06:50:24 -0300 | [diff] [blame] | 676 | int ret; |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 677 | |
| 678 | m2m_ctx = kzalloc(sizeof *m2m_ctx, GFP_KERNEL); |
| 679 | if (!m2m_ctx) |
| 680 | return ERR_PTR(-ENOMEM); |
| 681 | |
Marek Szyprowski | 908a0d7 | 2011-01-12 06:50:24 -0300 | [diff] [blame] | 682 | m2m_ctx->priv = drv_priv; |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 683 | m2m_ctx->m2m_dev = m2m_dev; |
Marek Szyprowski | 908a0d7 | 2011-01-12 06:50:24 -0300 | [diff] [blame] | 684 | init_waitqueue_head(&m2m_ctx->finished); |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 685 | |
Marek Szyprowski | 908a0d7 | 2011-01-12 06:50:24 -0300 | [diff] [blame] | 686 | out_q_ctx = &m2m_ctx->out_q_ctx; |
| 687 | cap_q_ctx = &m2m_ctx->cap_q_ctx; |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 688 | |
| 689 | INIT_LIST_HEAD(&out_q_ctx->rdy_queue); |
| 690 | INIT_LIST_HEAD(&cap_q_ctx->rdy_queue); |
Marek Szyprowski | 908a0d7 | 2011-01-12 06:50:24 -0300 | [diff] [blame] | 691 | spin_lock_init(&out_q_ctx->rdy_spinlock); |
| 692 | spin_lock_init(&cap_q_ctx->rdy_spinlock); |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 693 | |
| 694 | INIT_LIST_HEAD(&m2m_ctx->queue); |
| 695 | |
Marek Szyprowski | 908a0d7 | 2011-01-12 06:50:24 -0300 | [diff] [blame] | 696 | ret = queue_init(drv_priv, &out_q_ctx->q, &cap_q_ctx->q); |
| 697 | |
| 698 | if (ret) |
| 699 | goto err; |
Sylwester Nawrocki | 8e6e8f9 | 2013-09-14 18:39:04 -0300 | [diff] [blame] | 700 | /* |
| 701 | * If both queues use same mutex assign it as the common buffer |
| 702 | * queues lock to the m2m context. This lock is used in the |
| 703 | * v4l2_m2m_ioctl_* helpers. |
| 704 | */ |
| 705 | if (out_q_ctx->q.lock == cap_q_ctx->q.lock) |
| 706 | m2m_ctx->q_lock = out_q_ctx->q.lock; |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 707 | |
| 708 | return m2m_ctx; |
Marek Szyprowski | 908a0d7 | 2011-01-12 06:50:24 -0300 | [diff] [blame] | 709 | err: |
| 710 | kfree(m2m_ctx); |
| 711 | return ERR_PTR(ret); |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 712 | } |
| 713 | EXPORT_SYMBOL_GPL(v4l2_m2m_ctx_init); |
| 714 | |
| 715 | /** |
| 716 | * v4l2_m2m_ctx_release() - release m2m context |
| 717 | * |
| 718 | * Usually called from driver's release() function. |
| 719 | */ |
| 720 | void v4l2_m2m_ctx_release(struct v4l2_m2m_ctx *m2m_ctx) |
| 721 | { |
Shaik Ameer Basha | fea564a | 2013-08-13 02:58:07 -0300 | [diff] [blame] | 722 | /* wait until the current context is dequeued from job_queue */ |
| 723 | v4l2_m2m_cancel_job(m2m_ctx); |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 724 | |
Marek Szyprowski | 908a0d7 | 2011-01-12 06:50:24 -0300 | [diff] [blame] | 725 | vb2_queue_release(&m2m_ctx->cap_q_ctx.q); |
| 726 | vb2_queue_release(&m2m_ctx->out_q_ctx.q); |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 727 | |
| 728 | kfree(m2m_ctx); |
| 729 | } |
| 730 | EXPORT_SYMBOL_GPL(v4l2_m2m_ctx_release); |
| 731 | |
| 732 | /** |
| 733 | * v4l2_m2m_buf_queue() - add a buffer to the proper ready buffers list. |
| 734 | * |
| 735 | * Call from buf_queue(), videobuf_queue_ops callback. |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 736 | */ |
Marek Szyprowski | 908a0d7 | 2011-01-12 06:50:24 -0300 | [diff] [blame] | 737 | 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] | 738 | { |
Marek Szyprowski | 908a0d7 | 2011-01-12 06:50:24 -0300 | [diff] [blame] | 739 | 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] | 740 | struct v4l2_m2m_queue_ctx *q_ctx; |
Marek Szyprowski | 908a0d7 | 2011-01-12 06:50:24 -0300 | [diff] [blame] | 741 | unsigned long flags; |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 742 | |
Marek Szyprowski | 908a0d7 | 2011-01-12 06:50:24 -0300 | [diff] [blame] | 743 | q_ctx = get_queue_ctx(m2m_ctx, vb->vb2_queue->type); |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 744 | if (!q_ctx) |
| 745 | return; |
| 746 | |
Marek Szyprowski | 908a0d7 | 2011-01-12 06:50:24 -0300 | [diff] [blame] | 747 | spin_lock_irqsave(&q_ctx->rdy_spinlock, flags); |
| 748 | list_add_tail(&b->list, &q_ctx->rdy_queue); |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 749 | q_ctx->num_rdy++; |
Marek Szyprowski | 908a0d7 | 2011-01-12 06:50:24 -0300 | [diff] [blame] | 750 | spin_unlock_irqrestore(&q_ctx->rdy_spinlock, flags); |
Pawel Osciak | 7f98639 | 2010-04-23 05:38:37 -0300 | [diff] [blame] | 751 | } |
| 752 | EXPORT_SYMBOL_GPL(v4l2_m2m_buf_queue); |
| 753 | |
Sylwester Nawrocki | 8e6e8f9 | 2013-09-14 18:39:04 -0300 | [diff] [blame] | 754 | /* Videobuf2 ioctl helpers */ |
| 755 | |
| 756 | int v4l2_m2m_ioctl_reqbufs(struct file *file, void *priv, |
| 757 | struct v4l2_requestbuffers *rb) |
| 758 | { |
| 759 | struct v4l2_fh *fh = file->private_data; |
| 760 | |
| 761 | return v4l2_m2m_reqbufs(file, fh->m2m_ctx, rb); |
| 762 | } |
| 763 | EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_reqbufs); |
| 764 | |
| 765 | int v4l2_m2m_ioctl_create_bufs(struct file *file, void *priv, |
| 766 | struct v4l2_create_buffers *create) |
| 767 | { |
| 768 | struct v4l2_fh *fh = file->private_data; |
| 769 | |
| 770 | return v4l2_m2m_create_bufs(file, fh->m2m_ctx, create); |
| 771 | } |
| 772 | EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_create_bufs); |
| 773 | |
| 774 | int v4l2_m2m_ioctl_querybuf(struct file *file, void *priv, |
| 775 | struct v4l2_buffer *buf) |
| 776 | { |
| 777 | struct v4l2_fh *fh = file->private_data; |
| 778 | |
| 779 | return v4l2_m2m_querybuf(file, fh->m2m_ctx, buf); |
| 780 | } |
| 781 | EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_querybuf); |
| 782 | |
| 783 | int v4l2_m2m_ioctl_qbuf(struct file *file, void *priv, |
| 784 | struct v4l2_buffer *buf) |
| 785 | { |
| 786 | struct v4l2_fh *fh = file->private_data; |
| 787 | |
| 788 | return v4l2_m2m_qbuf(file, fh->m2m_ctx, buf); |
| 789 | } |
| 790 | EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_qbuf); |
| 791 | |
| 792 | int v4l2_m2m_ioctl_dqbuf(struct file *file, void *priv, |
| 793 | struct v4l2_buffer *buf) |
| 794 | { |
| 795 | struct v4l2_fh *fh = file->private_data; |
| 796 | |
| 797 | return v4l2_m2m_dqbuf(file, fh->m2m_ctx, buf); |
| 798 | } |
| 799 | EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_dqbuf); |
| 800 | |
| 801 | int v4l2_m2m_ioctl_expbuf(struct file *file, void *priv, |
| 802 | struct v4l2_exportbuffer *eb) |
| 803 | { |
| 804 | struct v4l2_fh *fh = file->private_data; |
| 805 | |
| 806 | return v4l2_m2m_expbuf(file, fh->m2m_ctx, eb); |
| 807 | } |
| 808 | EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_expbuf); |
| 809 | |
| 810 | int v4l2_m2m_ioctl_streamon(struct file *file, void *priv, |
| 811 | enum v4l2_buf_type type) |
| 812 | { |
| 813 | struct v4l2_fh *fh = file->private_data; |
| 814 | |
| 815 | return v4l2_m2m_streamon(file, fh->m2m_ctx, type); |
| 816 | } |
| 817 | EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_streamon); |
| 818 | |
| 819 | int v4l2_m2m_ioctl_streamoff(struct file *file, void *priv, |
| 820 | enum v4l2_buf_type type) |
| 821 | { |
| 822 | struct v4l2_fh *fh = file->private_data; |
| 823 | |
| 824 | return v4l2_m2m_streamoff(file, fh->m2m_ctx, type); |
| 825 | } |
| 826 | EXPORT_SYMBOL_GPL(v4l2_m2m_ioctl_streamoff); |
| 827 | |
| 828 | /* |
| 829 | * v4l2_file_operations helpers. It is assumed here same lock is used |
| 830 | * for the output and the capture buffer queue. |
| 831 | */ |
| 832 | |
| 833 | int v4l2_m2m_fop_mmap(struct file *file, struct vm_area_struct *vma) |
| 834 | { |
| 835 | struct v4l2_fh *fh = file->private_data; |
| 836 | struct v4l2_m2m_ctx *m2m_ctx = fh->m2m_ctx; |
| 837 | int ret; |
| 838 | |
| 839 | if (m2m_ctx->q_lock && mutex_lock_interruptible(m2m_ctx->q_lock)) |
| 840 | return -ERESTARTSYS; |
| 841 | |
| 842 | ret = v4l2_m2m_mmap(file, m2m_ctx, vma); |
| 843 | |
| 844 | if (m2m_ctx->q_lock) |
| 845 | mutex_unlock(m2m_ctx->q_lock); |
| 846 | |
| 847 | return ret; |
| 848 | } |
| 849 | EXPORT_SYMBOL_GPL(v4l2_m2m_fop_mmap); |
| 850 | |
| 851 | unsigned int v4l2_m2m_fop_poll(struct file *file, poll_table *wait) |
| 852 | { |
| 853 | struct v4l2_fh *fh = file->private_data; |
| 854 | struct v4l2_m2m_ctx *m2m_ctx = fh->m2m_ctx; |
| 855 | unsigned int ret; |
| 856 | |
| 857 | if (m2m_ctx->q_lock) |
| 858 | mutex_lock(m2m_ctx->q_lock); |
| 859 | |
| 860 | ret = v4l2_m2m_poll(file, m2m_ctx, wait); |
| 861 | |
| 862 | if (m2m_ctx->q_lock) |
| 863 | mutex_unlock(m2m_ctx->q_lock); |
| 864 | |
| 865 | return ret; |
| 866 | } |
| 867 | EXPORT_SYMBOL_GPL(v4l2_m2m_fop_poll); |
| 868 | |